The Design and Implementation of the FreeBSD Operating System, Second Edition
Now available: The Design and Implementation of the FreeBSD Operating System (Second Edition)


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/netinet6/in6_mcast.c

Version: -  FREEBSD  -  FREEBSD-13-STABLE  -  FREEBSD-13-0  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  l41  -  OPENBSD  -  linux-2.6  -  MK84  -  PLAN9  -  xnu-8792 
SearchContext: -  none  -  3  -  10 

    1 /*-
    2  * SPDX-License-Identifier: BSD-3-Clause
    3  *
    4  * Copyright (c) 2009 Bruce Simpson.
    5  * All rights reserved.
    6  *
    7  * Redistribution and use in source and binary forms, with or without
    8  * modification, are permitted provided that the following conditions
    9  * are met:
   10  * 1. Redistributions of source code must retain the above copyright
   11  *    notice, this list of conditions and the following disclaimer.
   12  * 2. Redistributions in binary form must reproduce the above copyright
   13  *    notice, this list of conditions and the following disclaimer in the
   14  *    documentation and/or other materials provided with the distribution.
   15  * 3. The name of the author may not be used to endorse or promote
   16  *    products derived from this software without specific prior written
   17  *    permission.
   18  *
   19  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
   20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
   23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   29  * SUCH DAMAGE.
   30  */
   31 
   32 /*
   33  * IPv6 multicast socket, group, and socket option processing module.
   34  * Normative references: RFC 2292, RFC 3492, RFC 3542, RFC 3678, RFC 3810.
   35  */
   36 
   37 #include <sys/cdefs.h>
   38 __FBSDID("$FreeBSD$");
   39 
   40 #include "opt_inet6.h"
   41 
   42 #include <sys/param.h>
   43 #include <sys/systm.h>
   44 #include <sys/kernel.h>
   45 #include <sys/ktr.h>
   46 #include <sys/malloc.h>
   47 #include <sys/mbuf.h>
   48 #include <sys/protosw.h>
   49 #include <sys/socket.h>
   50 #include <sys/socketvar.h>
   51 #include <sys/sysctl.h>
   52 #include <sys/priv.h>
   53 #include <sys/taskqueue.h>
   54 #include <sys/tree.h>
   55 
   56 #include <net/if.h>
   57 #include <net/if_var.h>
   58 #include <net/if_dl.h>
   59 #include <net/route.h>
   60 #include <net/vnet.h>
   61 
   62 #include <netinet/in.h>
   63 #include <netinet/udp.h>
   64 #include <netinet/in_var.h>
   65 #include <netinet/ip_var.h>
   66 #include <netinet/udp_var.h>
   67 #include <netinet6/in6_fib.h>
   68 #include <netinet6/in6_var.h>
   69 #include <netinet/ip6.h>
   70 #include <netinet/icmp6.h>
   71 #include <netinet6/ip6_var.h>
   72 #include <netinet/in_pcb.h>
   73 #include <netinet/tcp_var.h>
   74 #include <netinet6/nd6.h>
   75 #include <netinet6/mld6_var.h>
   76 #include <netinet6/scope6_var.h>
   77 
   78 #ifndef KTR_MLD
   79 #define KTR_MLD KTR_INET6
   80 #endif
   81 
   82 #ifndef __SOCKUNION_DECLARED
   83 union sockunion {
   84         struct sockaddr_storage ss;
   85         struct sockaddr         sa;
   86         struct sockaddr_dl      sdl;
   87         struct sockaddr_in6     sin6;
   88 };
   89 typedef union sockunion sockunion_t;
   90 #define __SOCKUNION_DECLARED
   91 #endif /* __SOCKUNION_DECLARED */
   92 
   93 static MALLOC_DEFINE(M_IN6MFILTER, "in6_mfilter",
   94     "IPv6 multicast PCB-layer source filter");
   95 MALLOC_DEFINE(M_IP6MADDR, "in6_multi", "IPv6 multicast group");
   96 static MALLOC_DEFINE(M_IP6MOPTS, "ip6_moptions", "IPv6 multicast options");
   97 static MALLOC_DEFINE(M_IP6MSOURCE, "ip6_msource",
   98     "IPv6 multicast MLD-layer source filter");
   99 
  100 RB_GENERATE(ip6_msource_tree, ip6_msource, im6s_link, ip6_msource_cmp);
  101 
  102 /*
  103  * Locking:
  104  * - Lock order is: Giant, IN6_MULTI_LOCK, INP_WLOCK,
  105  *   IN6_MULTI_LIST_LOCK, MLD_LOCK, IF_ADDR_LOCK.
  106  * - The IF_ADDR_LOCK is implicitly taken by in6m_lookup() earlier, however
  107  *   it can be taken by code in net/if.c also.
  108  * - ip6_moptions and in6_mfilter are covered by the INP_WLOCK.
  109  *
  110  * struct in6_multi is covered by IN6_MULTI_LOCK. There isn't strictly
  111  * any need for in6_multi itself to be virtualized -- it is bound to an ifp
  112  * anyway no matter what happens.
  113  */
  114 struct mtx in6_multi_list_mtx;
  115 MTX_SYSINIT(in6_multi_mtx, &in6_multi_list_mtx, "in6_multi_list_mtx", MTX_DEF);
  116 
  117 struct mtx in6_multi_free_mtx;
  118 MTX_SYSINIT(in6_multi_free_mtx, &in6_multi_free_mtx, "in6_multi_free_mtx", MTX_DEF);
  119 
  120 struct sx in6_multi_sx;
  121 SX_SYSINIT(in6_multi_sx, &in6_multi_sx, "in6_multi_sx");
  122 
  123 
  124 
  125 static void     im6f_commit(struct in6_mfilter *);
  126 static int      im6f_get_source(struct in6_mfilter *imf,
  127                     const struct sockaddr_in6 *psin,
  128                     struct in6_msource **);
  129 static struct in6_msource *
  130                 im6f_graft(struct in6_mfilter *, const uint8_t,
  131                     const struct sockaddr_in6 *);
  132 static void     im6f_leave(struct in6_mfilter *);
  133 static int      im6f_prune(struct in6_mfilter *, const struct sockaddr_in6 *);
  134 static void     im6f_purge(struct in6_mfilter *);
  135 static void     im6f_rollback(struct in6_mfilter *);
  136 static void     im6f_reap(struct in6_mfilter *);
  137 static struct in6_mfilter *
  138                 im6o_match_group(const struct ip6_moptions *,
  139                     const struct ifnet *, const struct sockaddr *);
  140 static struct in6_msource *
  141                 im6o_match_source(struct in6_mfilter *, const struct sockaddr *);
  142 static void     im6s_merge(struct ip6_msource *ims,
  143                     const struct in6_msource *lims, const int rollback);
  144 static int      in6_getmulti(struct ifnet *, const struct in6_addr *,
  145                     struct in6_multi **);
  146 static int      in6m_get_source(struct in6_multi *inm,
  147                     const struct in6_addr *addr, const int noalloc,
  148                     struct ip6_msource **pims);
  149 #ifdef KTR
  150 static int      in6m_is_ifp_detached(const struct in6_multi *);
  151 #endif
  152 static int      in6m_merge(struct in6_multi *, /*const*/ struct in6_mfilter *);
  153 static void     in6m_purge(struct in6_multi *);
  154 static void     in6m_reap(struct in6_multi *);
  155 static struct ip6_moptions *
  156                 in6p_findmoptions(struct inpcb *);
  157 static int      in6p_get_source_filters(struct inpcb *, struct sockopt *);
  158 static int      in6p_join_group(struct inpcb *, struct sockopt *);
  159 static int      in6p_leave_group(struct inpcb *, struct sockopt *);
  160 static struct ifnet *
  161                 in6p_lookup_mcast_ifp(const struct inpcb *,
  162                     const struct sockaddr_in6 *);
  163 static int      in6p_block_unblock_source(struct inpcb *, struct sockopt *);
  164 static int      in6p_set_multicast_if(struct inpcb *, struct sockopt *);
  165 static int      in6p_set_source_filters(struct inpcb *, struct sockopt *);
  166 static int      sysctl_ip6_mcast_filters(SYSCTL_HANDLER_ARGS);
  167 
  168 SYSCTL_DECL(_net_inet6_ip6);    /* XXX Not in any common header. */
  169 
  170 static SYSCTL_NODE(_net_inet6_ip6, OID_AUTO, mcast, CTLFLAG_RW, 0,
  171     "IPv6 multicast");
  172 
  173 static u_long in6_mcast_maxgrpsrc = IPV6_MAX_GROUP_SRC_FILTER;
  174 SYSCTL_ULONG(_net_inet6_ip6_mcast, OID_AUTO, maxgrpsrc,
  175     CTLFLAG_RWTUN, &in6_mcast_maxgrpsrc, 0,
  176     "Max source filters per group");
  177 
  178 static u_long in6_mcast_maxsocksrc = IPV6_MAX_SOCK_SRC_FILTER;
  179 SYSCTL_ULONG(_net_inet6_ip6_mcast, OID_AUTO, maxsocksrc,
  180     CTLFLAG_RWTUN, &in6_mcast_maxsocksrc, 0,
  181     "Max source filters per socket");
  182 
  183 /* TODO Virtualize this switch. */
  184 int in6_mcast_loop = IPV6_DEFAULT_MULTICAST_LOOP;
  185 SYSCTL_INT(_net_inet6_ip6_mcast, OID_AUTO, loop, CTLFLAG_RWTUN,
  186     &in6_mcast_loop, 0, "Loopback multicast datagrams by default");
  187 
  188 static SYSCTL_NODE(_net_inet6_ip6_mcast, OID_AUTO, filters,
  189     CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_ip6_mcast_filters,
  190     "Per-interface stack-wide source filters");
  191 
  192 #ifdef KTR
  193 /*
  194  * Inline function which wraps assertions for a valid ifp.
  195  * The ifnet layer will set the ifma's ifp pointer to NULL if the ifp
  196  * is detached.
  197  */
  198 static int __inline
  199 in6m_is_ifp_detached(const struct in6_multi *inm)
  200 {
  201         struct ifnet *ifp;
  202 
  203         KASSERT(inm->in6m_ifma != NULL, ("%s: no ifma", __func__));
  204         ifp = inm->in6m_ifma->ifma_ifp;
  205         if (ifp != NULL) {
  206                 /*
  207                  * Sanity check that network-layer notion of ifp is the
  208                  * same as that of link-layer.
  209                  */
  210                 KASSERT(inm->in6m_ifp == ifp, ("%s: bad ifp", __func__));
  211         }
  212 
  213         return (ifp == NULL);
  214 }
  215 #endif
  216 
  217 /*
  218  * Initialize an in6_mfilter structure to a known state at t0, t1
  219  * with an empty source filter list.
  220  */
  221 static __inline void
  222 im6f_init(struct in6_mfilter *imf, const int st0, const int st1)
  223 {
  224         memset(imf, 0, sizeof(struct in6_mfilter));
  225         RB_INIT(&imf->im6f_sources);
  226         imf->im6f_st[0] = st0;
  227         imf->im6f_st[1] = st1;
  228 }
  229 
  230 struct in6_mfilter *
  231 ip6_mfilter_alloc(const int mflags, const int st0, const int st1)
  232 {
  233         struct in6_mfilter *imf;
  234 
  235         imf = malloc(sizeof(*imf), M_IN6MFILTER, mflags);
  236 
  237         if (imf != NULL)
  238                 im6f_init(imf, st0, st1);
  239 
  240         return (imf);
  241 }
  242 
  243 void
  244 ip6_mfilter_free(struct in6_mfilter *imf)
  245 {
  246 
  247         im6f_purge(imf);
  248         free(imf, M_IN6MFILTER);
  249 }
  250 
  251 /*
  252  * Find an IPv6 multicast group entry for this ip6_moptions instance
  253  * which matches the specified group, and optionally an interface.
  254  * Return its index into the array, or -1 if not found.
  255  */
  256 static struct in6_mfilter *
  257 im6o_match_group(const struct ip6_moptions *imo, const struct ifnet *ifp,
  258     const struct sockaddr *group)
  259 {
  260         const struct sockaddr_in6 *gsin6;
  261         struct in6_mfilter *imf;
  262         struct in6_multi *inm;
  263 
  264         gsin6 = (const struct sockaddr_in6 *)group;
  265 
  266         IP6_MFILTER_FOREACH(imf, &imo->im6o_head) {
  267                 inm = imf->im6f_in6m;
  268                 if (inm == NULL)
  269                         continue;
  270                 if ((ifp == NULL || (inm->in6m_ifp == ifp)) &&
  271                     IN6_ARE_ADDR_EQUAL(&inm->in6m_addr,
  272                     &gsin6->sin6_addr)) {
  273                         break;
  274                 }
  275         }
  276         return (imf);
  277 }
  278 
  279 /*
  280  * Find an IPv6 multicast source entry for this imo which matches
  281  * the given group index for this socket, and source address.
  282  *
  283  * XXX TODO: The scope ID, if present in src, is stripped before
  284  * any comparison. We SHOULD enforce scope/zone checks where the source
  285  * filter entry has a link scope.
  286  *
  287  * NOTE: This does not check if the entry is in-mode, merely if
  288  * it exists, which may not be the desired behaviour.
  289  */
  290 static struct in6_msource *
  291 im6o_match_source(struct in6_mfilter *imf, const struct sockaddr *src)
  292 {
  293         struct ip6_msource       find;
  294         struct ip6_msource      *ims;
  295         const sockunion_t       *psa;
  296 
  297         KASSERT(src->sa_family == AF_INET6, ("%s: !AF_INET6", __func__));
  298 
  299         psa = (const sockunion_t *)src;
  300         find.im6s_addr = psa->sin6.sin6_addr;
  301         in6_clearscope(&find.im6s_addr);                /* XXX */
  302         ims = RB_FIND(ip6_msource_tree, &imf->im6f_sources, &find);
  303 
  304         return ((struct in6_msource *)ims);
  305 }
  306 
  307 /*
  308  * Perform filtering for multicast datagrams on a socket by group and source.
  309  *
  310  * Returns 0 if a datagram should be allowed through, or various error codes
  311  * if the socket was not a member of the group, or the source was muted, etc.
  312  */
  313 int
  314 im6o_mc_filter(const struct ip6_moptions *imo, const struct ifnet *ifp,
  315     const struct sockaddr *group, const struct sockaddr *src)
  316 {
  317         struct in6_mfilter *imf;
  318         struct in6_msource *ims;
  319         int mode;
  320 
  321         KASSERT(ifp != NULL, ("%s: null ifp", __func__));
  322 
  323         imf = im6o_match_group(imo, ifp, group);
  324         if (imf == NULL)
  325                 return (MCAST_NOTGMEMBER);
  326 
  327         /*
  328          * Check if the source was included in an (S,G) join.
  329          * Allow reception on exclusive memberships by default,
  330          * reject reception on inclusive memberships by default.
  331          * Exclude source only if an in-mode exclude filter exists.
  332          * Include source only if an in-mode include filter exists.
  333          * NOTE: We are comparing group state here at MLD t1 (now)
  334          * with socket-layer t0 (since last downcall).
  335          */
  336         mode = imf->im6f_st[1];
  337         ims = im6o_match_source(imf, src);
  338 
  339         if ((ims == NULL && mode == MCAST_INCLUDE) ||
  340             (ims != NULL && ims->im6sl_st[0] != mode))
  341                 return (MCAST_NOTSMEMBER);
  342 
  343         return (MCAST_PASS);
  344 }
  345 
  346 /*
  347  * Find and return a reference to an in6_multi record for (ifp, group),
  348  * and bump its reference count.
  349  * If one does not exist, try to allocate it, and update link-layer multicast
  350  * filters on ifp to listen for group.
  351  * Assumes the IN6_MULTI lock is held across the call.
  352  * Return 0 if successful, otherwise return an appropriate error code.
  353  */
  354 static int
  355 in6_getmulti(struct ifnet *ifp, const struct in6_addr *group,
  356     struct in6_multi **pinm)
  357 {
  358         struct epoch_tracker     et;
  359         struct sockaddr_in6      gsin6;
  360         struct ifmultiaddr      *ifma;
  361         struct in6_multi        *inm;
  362         int                      error;
  363 
  364         error = 0;
  365 
  366         /*
  367          * XXX: Accesses to ifma_protospec must be covered by IF_ADDR_LOCK;
  368          * if_addmulti() takes this mutex itself, so we must drop and
  369          * re-acquire around the call.
  370          */
  371         IN6_MULTI_LOCK_ASSERT();
  372         IN6_MULTI_LIST_LOCK();
  373         IF_ADDR_WLOCK(ifp);
  374         NET_EPOCH_ENTER_ET(et);
  375         /*
  376          * Does ifp support IPv6 multicasts?
  377          */
  378         if (ifp->if_afdata[AF_INET6] == NULL)
  379                 error = ENODEV;
  380         else
  381                 inm = in6m_lookup_locked(ifp, group);
  382         NET_EPOCH_EXIT_ET(et);
  383 
  384         if (error != 0)
  385                 goto out_locked;
  386 
  387         if (inm != NULL) {
  388                 /*
  389                  * If we already joined this group, just bump the
  390                  * refcount and return it.
  391                  */
  392                 KASSERT(inm->in6m_refcount >= 1,
  393                     ("%s: bad refcount %d", __func__, inm->in6m_refcount));
  394                 in6m_acquire_locked(inm);
  395                 *pinm = inm;
  396                 goto out_locked;
  397         }
  398 
  399         memset(&gsin6, 0, sizeof(gsin6));
  400         gsin6.sin6_family = AF_INET6;
  401         gsin6.sin6_len = sizeof(struct sockaddr_in6);
  402         gsin6.sin6_addr = *group;
  403 
  404         /*
  405          * Check if a link-layer group is already associated
  406          * with this network-layer group on the given ifnet.
  407          */
  408         IN6_MULTI_LIST_UNLOCK();
  409         IF_ADDR_WUNLOCK(ifp);
  410         error = if_addmulti(ifp, (struct sockaddr *)&gsin6, &ifma);
  411         if (error != 0)
  412                 return (error);
  413         IN6_MULTI_LIST_LOCK();
  414         IF_ADDR_WLOCK(ifp);
  415 
  416         /*
  417          * If something other than netinet6 is occupying the link-layer
  418          * group, print a meaningful error message and back out of
  419          * the allocation.
  420          * Otherwise, bump the refcount on the existing network-layer
  421          * group association and return it.
  422          */
  423         if (ifma->ifma_protospec != NULL) {
  424                 inm = (struct in6_multi *)ifma->ifma_protospec;
  425 #ifdef INVARIANTS
  426                 KASSERT(ifma->ifma_addr != NULL, ("%s: no ifma_addr",
  427                     __func__));
  428                 KASSERT(ifma->ifma_addr->sa_family == AF_INET6,
  429                     ("%s: ifma not AF_INET6", __func__));
  430                 KASSERT(inm != NULL, ("%s: no ifma_protospec", __func__));
  431                 if (inm->in6m_ifma != ifma || inm->in6m_ifp != ifp ||
  432                     !IN6_ARE_ADDR_EQUAL(&inm->in6m_addr, group))
  433                         panic("%s: ifma %p is inconsistent with %p (%p)",
  434                             __func__, ifma, inm, group);
  435 #endif
  436                 in6m_acquire_locked(inm);
  437                 *pinm = inm;
  438                 goto out_locked;
  439         }
  440 
  441         IF_ADDR_WLOCK_ASSERT(ifp);
  442 
  443         /*
  444          * A new in6_multi record is needed; allocate and initialize it.
  445          * We DO NOT perform an MLD join as the in6_ layer may need to
  446          * push an initial source list down to MLD to support SSM.
  447          *
  448          * The initial source filter state is INCLUDE, {} as per the RFC.
  449          * Pending state-changes per group are subject to a bounds check.
  450          */
  451         inm = malloc(sizeof(*inm), M_IP6MADDR, M_NOWAIT | M_ZERO);
  452         if (inm == NULL) {
  453                 IN6_MULTI_LIST_UNLOCK();
  454                 IF_ADDR_WUNLOCK(ifp);
  455                 if_delmulti_ifma(ifma);
  456                 return (ENOMEM);
  457         }
  458         inm->in6m_addr = *group;
  459         inm->in6m_ifp = ifp;
  460         inm->in6m_mli = MLD_IFINFO(ifp);
  461         inm->in6m_ifma = ifma;
  462         inm->in6m_refcount = 1;
  463         inm->in6m_state = MLD_NOT_MEMBER;
  464         mbufq_init(&inm->in6m_scq, MLD_MAX_STATE_CHANGES);
  465 
  466         inm->in6m_st[0].iss_fmode = MCAST_UNDEFINED;
  467         inm->in6m_st[1].iss_fmode = MCAST_UNDEFINED;
  468         RB_INIT(&inm->in6m_srcs);
  469 
  470         ifma->ifma_protospec = inm;
  471         *pinm = inm;
  472 
  473  out_locked:
  474         IN6_MULTI_LIST_UNLOCK();
  475         IF_ADDR_WUNLOCK(ifp);
  476         return (error);
  477 }
  478 
  479 /*
  480  * Drop a reference to an in6_multi record.
  481  *
  482  * If the refcount drops to 0, free the in6_multi record and
  483  * delete the underlying link-layer membership.
  484  */
  485 static void
  486 in6m_release(struct in6_multi *inm)
  487 {
  488         struct ifmultiaddr *ifma;
  489         struct ifnet *ifp;
  490 
  491         CTR2(KTR_MLD, "%s: refcount is %d", __func__, inm->in6m_refcount);
  492 
  493         MPASS(inm->in6m_refcount == 0);
  494         CTR2(KTR_MLD, "%s: freeing inm %p", __func__, inm);
  495 
  496         ifma = inm->in6m_ifma;
  497         ifp = inm->in6m_ifp;
  498         MPASS(ifma->ifma_llifma == NULL);
  499 
  500         /* XXX this access is not covered by IF_ADDR_LOCK */
  501         CTR2(KTR_MLD, "%s: purging ifma %p", __func__, ifma);
  502         KASSERT(ifma->ifma_protospec == NULL,
  503             ("%s: ifma_protospec != NULL", __func__));
  504         if (ifp == NULL)
  505                 ifp = ifma->ifma_ifp;
  506 
  507         if (ifp != NULL) {
  508                 CURVNET_SET(ifp->if_vnet);
  509                 in6m_purge(inm);
  510                 free(inm, M_IP6MADDR);
  511                 if_delmulti_ifma_flags(ifma, 1);
  512                 CURVNET_RESTORE();
  513                 if_rele(ifp);
  514         } else {
  515                 in6m_purge(inm);
  516                 free(inm, M_IP6MADDR);
  517                 if_delmulti_ifma_flags(ifma, 1);
  518         }
  519 }
  520 
  521 /*
  522  * Interface detach can happen in a taskqueue thread context, so we must use a
  523  * dedicated thread to avoid deadlocks when draining in6m_release tasks.
  524  */
  525 TASKQUEUE_DEFINE_THREAD(in6m_free);
  526 static struct in6_multi_head in6m_free_list = SLIST_HEAD_INITIALIZER();
  527 static void in6m_release_task(void *arg __unused, int pending __unused);
  528 static struct task in6m_free_task = TASK_INITIALIZER(0, in6m_release_task, NULL);
  529 
  530 void
  531 in6m_release_list_deferred(struct in6_multi_head *inmh)
  532 {
  533         if (SLIST_EMPTY(inmh))
  534                 return;
  535         mtx_lock(&in6_multi_free_mtx);
  536         SLIST_CONCAT(&in6m_free_list, inmh, in6_multi, in6m_nrele);
  537         mtx_unlock(&in6_multi_free_mtx);
  538         taskqueue_enqueue(taskqueue_in6m_free, &in6m_free_task);
  539 }
  540 
  541 void
  542 in6m_release_wait(void *arg __unused)
  543 {
  544 
  545         /*
  546          * Make sure all pending multicast addresses are freed before
  547          * the VNET or network device is destroyed:
  548          */
  549         taskqueue_drain_all(taskqueue_in6m_free);
  550 }
  551 #ifdef VIMAGE
  552 VNET_SYSUNINIT(in6m_release_wait, SI_SUB_PROTO_DOMAIN, SI_ORDER_FIRST, in6m_release_wait, NULL);
  553 #endif
  554 
  555 void
  556 in6m_disconnect_locked(struct in6_multi_head *inmh, struct in6_multi *inm)
  557 {
  558         struct ifnet *ifp;
  559         struct ifaddr *ifa;
  560         struct in6_ifaddr *ifa6;
  561         struct in6_multi_mship *imm, *imm_tmp;
  562         struct ifmultiaddr *ifma, *ll_ifma;
  563 
  564         IN6_MULTI_LIST_LOCK_ASSERT();
  565 
  566         ifp = inm->in6m_ifp;
  567         if (ifp == NULL)
  568                 return;         /* already called */
  569 
  570         inm->in6m_ifp = NULL;
  571         IF_ADDR_WLOCK_ASSERT(ifp);
  572         ifma = inm->in6m_ifma;
  573         if (ifma == NULL)
  574                 return;
  575 
  576         if_ref(ifp);
  577         if (ifma->ifma_flags & IFMA_F_ENQUEUED) {
  578                 CK_STAILQ_REMOVE(&ifp->if_multiaddrs, ifma, ifmultiaddr, ifma_link);
  579                 ifma->ifma_flags &= ~IFMA_F_ENQUEUED;
  580         }
  581         MCDPRINTF("removed ifma: %p from %s\n", ifma, ifp->if_xname);
  582         if ((ll_ifma = ifma->ifma_llifma) != NULL) {
  583                 MPASS(ifma != ll_ifma);
  584                 ifma->ifma_llifma = NULL;
  585                 MPASS(ll_ifma->ifma_llifma == NULL);
  586                 MPASS(ll_ifma->ifma_ifp == ifp);
  587                 if (--ll_ifma->ifma_refcount == 0) {
  588                         if (ll_ifma->ifma_flags & IFMA_F_ENQUEUED) {
  589                                 CK_STAILQ_REMOVE(&ifp->if_multiaddrs, ll_ifma, ifmultiaddr, ifma_link);
  590                                 ll_ifma->ifma_flags &= ~IFMA_F_ENQUEUED;
  591                         }
  592                         MCDPRINTF("removed ll_ifma: %p from %s\n", ll_ifma, ifp->if_xname);
  593                         if_freemulti(ll_ifma);
  594                 }
  595         }
  596         CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
  597                 if (ifa->ifa_addr->sa_family != AF_INET6)
  598                         continue;
  599                 ifa6 = (void *)ifa;
  600                 LIST_FOREACH_SAFE(imm, &ifa6->ia6_memberships,
  601                     i6mm_chain, imm_tmp) {
  602                         if (inm == imm->i6mm_maddr) {
  603                                 LIST_REMOVE(imm, i6mm_chain);
  604                                 free(imm, M_IP6MADDR);
  605                                 in6m_rele_locked(inmh, inm);
  606                         }
  607                 }
  608         }
  609 }
  610 
  611 static void
  612 in6m_release_task(void *arg __unused, int pending __unused)
  613 {
  614         struct in6_multi_head in6m_free_tmp;
  615         struct in6_multi *inm, *tinm;
  616 
  617         SLIST_INIT(&in6m_free_tmp);
  618         mtx_lock(&in6_multi_free_mtx);
  619         SLIST_CONCAT(&in6m_free_tmp, &in6m_free_list, in6_multi, in6m_nrele);
  620         mtx_unlock(&in6_multi_free_mtx);
  621         IN6_MULTI_LOCK();
  622         SLIST_FOREACH_SAFE(inm, &in6m_free_tmp, in6m_nrele, tinm) {
  623                 SLIST_REMOVE_HEAD(&in6m_free_tmp, in6m_nrele);
  624                 in6m_release(inm);
  625         }
  626         IN6_MULTI_UNLOCK();
  627 }
  628 
  629 /*
  630  * Clear recorded source entries for a group.
  631  * Used by the MLD code. Caller must hold the IN6_MULTI lock.
  632  * FIXME: Should reap.
  633  */
  634 void
  635 in6m_clear_recorded(struct in6_multi *inm)
  636 {
  637         struct ip6_msource      *ims;
  638 
  639         IN6_MULTI_LIST_LOCK_ASSERT();
  640 
  641         RB_FOREACH(ims, ip6_msource_tree, &inm->in6m_srcs) {
  642                 if (ims->im6s_stp) {
  643                         ims->im6s_stp = 0;
  644                         --inm->in6m_st[1].iss_rec;
  645                 }
  646         }
  647         KASSERT(inm->in6m_st[1].iss_rec == 0,
  648             ("%s: iss_rec %d not 0", __func__, inm->in6m_st[1].iss_rec));
  649 }
  650 
  651 /*
  652  * Record a source as pending for a Source-Group MLDv2 query.
  653  * This lives here as it modifies the shared tree.
  654  *
  655  * inm is the group descriptor.
  656  * naddr is the address of the source to record in network-byte order.
  657  *
  658  * If the net.inet6.mld.sgalloc sysctl is non-zero, we will
  659  * lazy-allocate a source node in response to an SG query.
  660  * Otherwise, no allocation is performed. This saves some memory
  661  * with the trade-off that the source will not be reported to the
  662  * router if joined in the window between the query response and
  663  * the group actually being joined on the local host.
  664  *
  665  * VIMAGE: XXX: Currently the mld_sgalloc feature has been removed.
  666  * This turns off the allocation of a recorded source entry if
  667  * the group has not been joined.
  668  *
  669  * Return 0 if the source didn't exist or was already marked as recorded.
  670  * Return 1 if the source was marked as recorded by this function.
  671  * Return <0 if any error occurred (negated errno code).
  672  */
  673 int
  674 in6m_record_source(struct in6_multi *inm, const struct in6_addr *addr)
  675 {
  676         struct ip6_msource       find;
  677         struct ip6_msource      *ims, *nims;
  678 
  679         IN6_MULTI_LIST_LOCK_ASSERT();
  680 
  681         find.im6s_addr = *addr;
  682         ims = RB_FIND(ip6_msource_tree, &inm->in6m_srcs, &find);
  683         if (ims && ims->im6s_stp)
  684                 return (0);
  685         if (ims == NULL) {
  686                 if (inm->in6m_nsrc == in6_mcast_maxgrpsrc)
  687                         return (-ENOSPC);
  688                 nims = malloc(sizeof(struct ip6_msource), M_IP6MSOURCE,
  689                     M_NOWAIT | M_ZERO);
  690                 if (nims == NULL)
  691                         return (-ENOMEM);
  692                 nims->im6s_addr = find.im6s_addr;
  693                 RB_INSERT(ip6_msource_tree, &inm->in6m_srcs, nims);
  694                 ++inm->in6m_nsrc;
  695                 ims = nims;
  696         }
  697 
  698         /*
  699          * Mark the source as recorded and update the recorded
  700          * source count.
  701          */
  702         ++ims->im6s_stp;
  703         ++inm->in6m_st[1].iss_rec;
  704 
  705         return (1);
  706 }
  707 
  708 /*
  709  * Return a pointer to an in6_msource owned by an in6_mfilter,
  710  * given its source address.
  711  * Lazy-allocate if needed. If this is a new entry its filter state is
  712  * undefined at t0.
  713  *
  714  * imf is the filter set being modified.
  715  * addr is the source address.
  716  *
  717  * SMPng: May be called with locks held; malloc must not block.
  718  */
  719 static int
  720 im6f_get_source(struct in6_mfilter *imf, const struct sockaddr_in6 *psin,
  721     struct in6_msource **plims)
  722 {
  723         struct ip6_msource       find;
  724         struct ip6_msource      *ims, *nims;
  725         struct in6_msource      *lims;
  726         int                      error;
  727 
  728         error = 0;
  729         ims = NULL;
  730         lims = NULL;
  731 
  732         find.im6s_addr = psin->sin6_addr;
  733         ims = RB_FIND(ip6_msource_tree, &imf->im6f_sources, &find);
  734         lims = (struct in6_msource *)ims;
  735         if (lims == NULL) {
  736                 if (imf->im6f_nsrc == in6_mcast_maxsocksrc)
  737                         return (ENOSPC);
  738                 nims = malloc(sizeof(struct in6_msource), M_IN6MFILTER,
  739                     M_NOWAIT | M_ZERO);
  740                 if (nims == NULL)
  741                         return (ENOMEM);
  742                 lims = (struct in6_msource *)nims;
  743                 lims->im6s_addr = find.im6s_addr;
  744                 lims->im6sl_st[0] = MCAST_UNDEFINED;
  745                 RB_INSERT(ip6_msource_tree, &imf->im6f_sources, nims);
  746                 ++imf->im6f_nsrc;
  747         }
  748 
  749         *plims = lims;
  750 
  751         return (error);
  752 }
  753 
  754 /*
  755  * Graft a source entry into an existing socket-layer filter set,
  756  * maintaining any required invariants and checking allocations.
  757  *
  758  * The source is marked as being in the new filter mode at t1.
  759  *
  760  * Return the pointer to the new node, otherwise return NULL.
  761  */
  762 static struct in6_msource *
  763 im6f_graft(struct in6_mfilter *imf, const uint8_t st1,
  764     const struct sockaddr_in6 *psin)
  765 {
  766         struct ip6_msource      *nims;
  767         struct in6_msource      *lims;
  768 
  769         nims = malloc(sizeof(struct in6_msource), M_IN6MFILTER,
  770             M_NOWAIT | M_ZERO);
  771         if (nims == NULL)
  772                 return (NULL);
  773         lims = (struct in6_msource *)nims;
  774         lims->im6s_addr = psin->sin6_addr;
  775         lims->im6sl_st[0] = MCAST_UNDEFINED;
  776         lims->im6sl_st[1] = st1;
  777         RB_INSERT(ip6_msource_tree, &imf->im6f_sources, nims);
  778         ++imf->im6f_nsrc;
  779 
  780         return (lims);
  781 }
  782 
  783 /*
  784  * Prune a source entry from an existing socket-layer filter set,
  785  * maintaining any required invariants and checking allocations.
  786  *
  787  * The source is marked as being left at t1, it is not freed.
  788  *
  789  * Return 0 if no error occurred, otherwise return an errno value.
  790  */
  791 static int
  792 im6f_prune(struct in6_mfilter *imf, const struct sockaddr_in6 *psin)
  793 {
  794         struct ip6_msource       find;
  795         struct ip6_msource      *ims;
  796         struct in6_msource      *lims;
  797 
  798         find.im6s_addr = psin->sin6_addr;
  799         ims = RB_FIND(ip6_msource_tree, &imf->im6f_sources, &find);
  800         if (ims == NULL)
  801                 return (ENOENT);
  802         lims = (struct in6_msource *)ims;
  803         lims->im6sl_st[1] = MCAST_UNDEFINED;
  804         return (0);
  805 }
  806 
  807 /*
  808  * Revert socket-layer filter set deltas at t1 to t0 state.
  809  */
  810 static void
  811 im6f_rollback(struct in6_mfilter *imf)
  812 {
  813         struct ip6_msource      *ims, *tims;
  814         struct in6_msource      *lims;
  815 
  816         RB_FOREACH_SAFE(ims, ip6_msource_tree, &imf->im6f_sources, tims) {
  817                 lims = (struct in6_msource *)ims;
  818                 if (lims->im6sl_st[0] == lims->im6sl_st[1]) {
  819                         /* no change at t1 */
  820                         continue;
  821                 } else if (lims->im6sl_st[0] != MCAST_UNDEFINED) {
  822                         /* revert change to existing source at t1 */
  823                         lims->im6sl_st[1] = lims->im6sl_st[0];
  824                 } else {
  825                         /* revert source added t1 */
  826                         CTR2(KTR_MLD, "%s: free ims %p", __func__, ims);
  827                         RB_REMOVE(ip6_msource_tree, &imf->im6f_sources, ims);
  828                         free(ims, M_IN6MFILTER);
  829                         imf->im6f_nsrc--;
  830                 }
  831         }
  832         imf->im6f_st[1] = imf->im6f_st[0];
  833 }
  834 
  835 /*
  836  * Mark socket-layer filter set as INCLUDE {} at t1.
  837  */
  838 static void
  839 im6f_leave(struct in6_mfilter *imf)
  840 {
  841         struct ip6_msource      *ims;
  842         struct in6_msource      *lims;
  843 
  844         RB_FOREACH(ims, ip6_msource_tree, &imf->im6f_sources) {
  845                 lims = (struct in6_msource *)ims;
  846                 lims->im6sl_st[1] = MCAST_UNDEFINED;
  847         }
  848         imf->im6f_st[1] = MCAST_INCLUDE;
  849 }
  850 
  851 /*
  852  * Mark socket-layer filter set deltas as committed.
  853  */
  854 static void
  855 im6f_commit(struct in6_mfilter *imf)
  856 {
  857         struct ip6_msource      *ims;
  858         struct in6_msource      *lims;
  859 
  860         RB_FOREACH(ims, ip6_msource_tree, &imf->im6f_sources) {
  861                 lims = (struct in6_msource *)ims;
  862                 lims->im6sl_st[0] = lims->im6sl_st[1];
  863         }
  864         imf->im6f_st[0] = imf->im6f_st[1];
  865 }
  866 
  867 /*
  868  * Reap unreferenced sources from socket-layer filter set.
  869  */
  870 static void
  871 im6f_reap(struct in6_mfilter *imf)
  872 {
  873         struct ip6_msource      *ims, *tims;
  874         struct in6_msource      *lims;
  875 
  876         RB_FOREACH_SAFE(ims, ip6_msource_tree, &imf->im6f_sources, tims) {
  877                 lims = (struct in6_msource *)ims;
  878                 if ((lims->im6sl_st[0] == MCAST_UNDEFINED) &&
  879                     (lims->im6sl_st[1] == MCAST_UNDEFINED)) {
  880                         CTR2(KTR_MLD, "%s: free lims %p", __func__, ims);
  881                         RB_REMOVE(ip6_msource_tree, &imf->im6f_sources, ims);
  882                         free(ims, M_IN6MFILTER);
  883                         imf->im6f_nsrc--;
  884                 }
  885         }
  886 }
  887 
  888 /*
  889  * Purge socket-layer filter set.
  890  */
  891 static void
  892 im6f_purge(struct in6_mfilter *imf)
  893 {
  894         struct ip6_msource      *ims, *tims;
  895 
  896         RB_FOREACH_SAFE(ims, ip6_msource_tree, &imf->im6f_sources, tims) {
  897                 CTR2(KTR_MLD, "%s: free ims %p", __func__, ims);
  898                 RB_REMOVE(ip6_msource_tree, &imf->im6f_sources, ims);
  899                 free(ims, M_IN6MFILTER);
  900                 imf->im6f_nsrc--;
  901         }
  902         imf->im6f_st[0] = imf->im6f_st[1] = MCAST_UNDEFINED;
  903         KASSERT(RB_EMPTY(&imf->im6f_sources),
  904             ("%s: im6f_sources not empty", __func__));
  905 }
  906 
  907 /*
  908  * Look up a source filter entry for a multicast group.
  909  *
  910  * inm is the group descriptor to work with.
  911  * addr is the IPv6 address to look up.
  912  * noalloc may be non-zero to suppress allocation of sources.
  913  * *pims will be set to the address of the retrieved or allocated source.
  914  *
  915  * SMPng: NOTE: may be called with locks held.
  916  * Return 0 if successful, otherwise return a non-zero error code.
  917  */
  918 static int
  919 in6m_get_source(struct in6_multi *inm, const struct in6_addr *addr,
  920     const int noalloc, struct ip6_msource **pims)
  921 {
  922         struct ip6_msource       find;
  923         struct ip6_msource      *ims, *nims;
  924 #ifdef KTR
  925         char                     ip6tbuf[INET6_ADDRSTRLEN];
  926 #endif
  927 
  928         find.im6s_addr = *addr;
  929         ims = RB_FIND(ip6_msource_tree, &inm->in6m_srcs, &find);
  930         if (ims == NULL && !noalloc) {
  931                 if (inm->in6m_nsrc == in6_mcast_maxgrpsrc)
  932                         return (ENOSPC);
  933                 nims = malloc(sizeof(struct ip6_msource), M_IP6MSOURCE,
  934                     M_NOWAIT | M_ZERO);
  935                 if (nims == NULL)
  936                         return (ENOMEM);
  937                 nims->im6s_addr = *addr;
  938                 RB_INSERT(ip6_msource_tree, &inm->in6m_srcs, nims);
  939                 ++inm->in6m_nsrc;
  940                 ims = nims;
  941                 CTR3(KTR_MLD, "%s: allocated %s as %p", __func__,
  942                     ip6_sprintf(ip6tbuf, addr), ims);
  943         }
  944 
  945         *pims = ims;
  946         return (0);
  947 }
  948 
  949 /*
  950  * Merge socket-layer source into MLD-layer source.
  951  * If rollback is non-zero, perform the inverse of the merge.
  952  */
  953 static void
  954 im6s_merge(struct ip6_msource *ims, const struct in6_msource *lims,
  955     const int rollback)
  956 {
  957         int n = rollback ? -1 : 1;
  958 #ifdef KTR
  959         char ip6tbuf[INET6_ADDRSTRLEN];
  960 
  961         ip6_sprintf(ip6tbuf, &lims->im6s_addr);
  962 #endif
  963 
  964         if (lims->im6sl_st[0] == MCAST_EXCLUDE) {
  965                 CTR3(KTR_MLD, "%s: t1 ex -= %d on %s", __func__, n, ip6tbuf);
  966                 ims->im6s_st[1].ex -= n;
  967         } else if (lims->im6sl_st[0] == MCAST_INCLUDE) {
  968                 CTR3(KTR_MLD, "%s: t1 in -= %d on %s", __func__, n, ip6tbuf);
  969                 ims->im6s_st[1].in -= n;
  970         }
  971 
  972         if (lims->im6sl_st[1] == MCAST_EXCLUDE) {
  973                 CTR3(KTR_MLD, "%s: t1 ex += %d on %s", __func__, n, ip6tbuf);
  974                 ims->im6s_st[1].ex += n;
  975         } else if (lims->im6sl_st[1] == MCAST_INCLUDE) {
  976                 CTR3(KTR_MLD, "%s: t1 in += %d on %s", __func__, n, ip6tbuf);
  977                 ims->im6s_st[1].in += n;
  978         }
  979 }
  980 
  981 /*
  982  * Atomically update the global in6_multi state, when a membership's
  983  * filter list is being updated in any way.
  984  *
  985  * imf is the per-inpcb-membership group filter pointer.
  986  * A fake imf may be passed for in-kernel consumers.
  987  *
  988  * XXX This is a candidate for a set-symmetric-difference style loop
  989  * which would eliminate the repeated lookup from root of ims nodes,
  990  * as they share the same key space.
  991  *
  992  * If any error occurred this function will back out of refcounts
  993  * and return a non-zero value.
  994  */
  995 static int
  996 in6m_merge(struct in6_multi *inm, /*const*/ struct in6_mfilter *imf)
  997 {
  998         struct ip6_msource      *ims, *nims;
  999         struct in6_msource      *lims;
 1000         int                      schanged, error;
 1001         int                      nsrc0, nsrc1;
 1002 
 1003         schanged = 0;
 1004         error = 0;
 1005         nsrc1 = nsrc0 = 0;
 1006         IN6_MULTI_LIST_LOCK_ASSERT();
 1007 
 1008         /*
 1009          * Update the source filters first, as this may fail.
 1010          * Maintain count of in-mode filters at t0, t1. These are
 1011          * used to work out if we transition into ASM mode or not.
 1012          * Maintain a count of source filters whose state was
 1013          * actually modified by this operation.
 1014          */
 1015         RB_FOREACH(ims, ip6_msource_tree, &imf->im6f_sources) {
 1016                 lims = (struct in6_msource *)ims;
 1017                 if (lims->im6sl_st[0] == imf->im6f_st[0]) nsrc0++;
 1018                 if (lims->im6sl_st[1] == imf->im6f_st[1]) nsrc1++;
 1019                 if (lims->im6sl_st[0] == lims->im6sl_st[1]) continue;
 1020                 error = in6m_get_source(inm, &lims->im6s_addr, 0, &nims);
 1021                 ++schanged;
 1022                 if (error)
 1023                         break;
 1024                 im6s_merge(nims, lims, 0);
 1025         }
 1026         if (error) {
 1027                 struct ip6_msource *bims;
 1028 
 1029                 RB_FOREACH_REVERSE_FROM(ims, ip6_msource_tree, nims) {
 1030                         lims = (struct in6_msource *)ims;
 1031                         if (lims->im6sl_st[0] == lims->im6sl_st[1])
 1032                                 continue;
 1033                         (void)in6m_get_source(inm, &lims->im6s_addr, 1, &bims);
 1034                         if (bims == NULL)
 1035                                 continue;
 1036                         im6s_merge(bims, lims, 1);
 1037                 }
 1038                 goto out_reap;
 1039         }
 1040 
 1041         CTR3(KTR_MLD, "%s: imf filters in-mode: %d at t0, %d at t1",
 1042             __func__, nsrc0, nsrc1);
 1043 
 1044         /* Handle transition between INCLUDE {n} and INCLUDE {} on socket. */
 1045         if (imf->im6f_st[0] == imf->im6f_st[1] &&
 1046             imf->im6f_st[1] == MCAST_INCLUDE) {
 1047                 if (nsrc1 == 0) {
 1048                         CTR1(KTR_MLD, "%s: --in on inm at t1", __func__);
 1049                         --inm->in6m_st[1].iss_in;
 1050                 }
 1051         }
 1052 
 1053         /* Handle filter mode transition on socket. */
 1054         if (imf->im6f_st[0] != imf->im6f_st[1]) {
 1055                 CTR3(KTR_MLD, "%s: imf transition %d to %d",
 1056                     __func__, imf->im6f_st[0], imf->im6f_st[1]);
 1057 
 1058                 if (imf->im6f_st[0] == MCAST_EXCLUDE) {
 1059                         CTR1(KTR_MLD, "%s: --ex on inm at t1", __func__);
 1060                         --inm->in6m_st[1].iss_ex;
 1061                 } else if (imf->im6f_st[0] == MCAST_INCLUDE) {
 1062                         CTR1(KTR_MLD, "%s: --in on inm at t1", __func__);
 1063                         --inm->in6m_st[1].iss_in;
 1064                 }
 1065 
 1066                 if (imf->im6f_st[1] == MCAST_EXCLUDE) {
 1067                         CTR1(KTR_MLD, "%s: ex++ on inm at t1", __func__);
 1068                         inm->in6m_st[1].iss_ex++;
 1069                 } else if (imf->im6f_st[1] == MCAST_INCLUDE && nsrc1 > 0) {
 1070                         CTR1(KTR_MLD, "%s: in++ on inm at t1", __func__);
 1071                         inm->in6m_st[1].iss_in++;
 1072                 }
 1073         }
 1074 
 1075         /*
 1076          * Track inm filter state in terms of listener counts.
 1077          * If there are any exclusive listeners, stack-wide
 1078          * membership is exclusive.
 1079          * Otherwise, if only inclusive listeners, stack-wide is inclusive.
 1080          * If no listeners remain, state is undefined at t1,
 1081          * and the MLD lifecycle for this group should finish.
 1082          */
 1083         if (inm->in6m_st[1].iss_ex > 0) {
 1084                 CTR1(KTR_MLD, "%s: transition to EX", __func__);
 1085                 inm->in6m_st[1].iss_fmode = MCAST_EXCLUDE;
 1086         } else if (inm->in6m_st[1].iss_in > 0) {
 1087                 CTR1(KTR_MLD, "%s: transition to IN", __func__);
 1088                 inm->in6m_st[1].iss_fmode = MCAST_INCLUDE;
 1089         } else {
 1090                 CTR1(KTR_MLD, "%s: transition to UNDEF", __func__);
 1091                 inm->in6m_st[1].iss_fmode = MCAST_UNDEFINED;
 1092         }
 1093 
 1094         /* Decrement ASM listener count on transition out of ASM mode. */
 1095         if (imf->im6f_st[0] == MCAST_EXCLUDE && nsrc0 == 0) {
 1096                 if ((imf->im6f_st[1] != MCAST_EXCLUDE) ||
 1097                     (imf->im6f_st[1] == MCAST_EXCLUDE && nsrc1 > 0)) {
 1098                         CTR1(KTR_MLD, "%s: --asm on inm at t1", __func__);
 1099                         --inm->in6m_st[1].iss_asm;
 1100                 }
 1101         }
 1102 
 1103         /* Increment ASM listener count on transition to ASM mode. */
 1104         if (imf->im6f_st[1] == MCAST_EXCLUDE && nsrc1 == 0) {
 1105                 CTR1(KTR_MLD, "%s: asm++ on inm at t1", __func__);
 1106                 inm->in6m_st[1].iss_asm++;
 1107         }
 1108 
 1109         CTR3(KTR_MLD, "%s: merged imf %p to inm %p", __func__, imf, inm);
 1110         in6m_print(inm);
 1111 
 1112 out_reap:
 1113         if (schanged > 0) {
 1114                 CTR1(KTR_MLD, "%s: sources changed; reaping", __func__);
 1115                 in6m_reap(inm);
 1116         }
 1117         return (error);
 1118 }
 1119 
 1120 /*
 1121  * Mark an in6_multi's filter set deltas as committed.
 1122  * Called by MLD after a state change has been enqueued.
 1123  */
 1124 void
 1125 in6m_commit(struct in6_multi *inm)
 1126 {
 1127         struct ip6_msource      *ims;
 1128 
 1129         CTR2(KTR_MLD, "%s: commit inm %p", __func__, inm);
 1130         CTR1(KTR_MLD, "%s: pre commit:", __func__);
 1131         in6m_print(inm);
 1132 
 1133         RB_FOREACH(ims, ip6_msource_tree, &inm->in6m_srcs) {
 1134                 ims->im6s_st[0] = ims->im6s_st[1];
 1135         }
 1136         inm->in6m_st[0] = inm->in6m_st[1];
 1137 }
 1138 
 1139 /*
 1140  * Reap unreferenced nodes from an in6_multi's filter set.
 1141  */
 1142 static void
 1143 in6m_reap(struct in6_multi *inm)
 1144 {
 1145         struct ip6_msource      *ims, *tims;
 1146 
 1147         RB_FOREACH_SAFE(ims, ip6_msource_tree, &inm->in6m_srcs, tims) {
 1148                 if (ims->im6s_st[0].ex > 0 || ims->im6s_st[0].in > 0 ||
 1149                     ims->im6s_st[1].ex > 0 || ims->im6s_st[1].in > 0 ||
 1150                     ims->im6s_stp != 0)
 1151                         continue;
 1152                 CTR2(KTR_MLD, "%s: free ims %p", __func__, ims);
 1153                 RB_REMOVE(ip6_msource_tree, &inm->in6m_srcs, ims);
 1154                 free(ims, M_IP6MSOURCE);
 1155                 inm->in6m_nsrc--;
 1156         }
 1157 }
 1158 
 1159 /*
 1160  * Purge all source nodes from an in6_multi's filter set.
 1161  */
 1162 static void
 1163 in6m_purge(struct in6_multi *inm)
 1164 {
 1165         struct ip6_msource      *ims, *tims;
 1166 
 1167         RB_FOREACH_SAFE(ims, ip6_msource_tree, &inm->in6m_srcs, tims) {
 1168                 CTR2(KTR_MLD, "%s: free ims %p", __func__, ims);
 1169                 RB_REMOVE(ip6_msource_tree, &inm->in6m_srcs, ims);
 1170                 free(ims, M_IP6MSOURCE);
 1171                 inm->in6m_nsrc--;
 1172         }
 1173         /* Free state-change requests that might be queued. */
 1174         mbufq_drain(&inm->in6m_scq);
 1175 }
 1176 
 1177 /*
 1178  * Join a multicast address w/o sources.
 1179  * KAME compatibility entry point.
 1180  *
 1181  * SMPng: Assume no mc locks held by caller.
 1182  */
 1183 int
 1184 in6_joingroup(struct ifnet *ifp, const struct in6_addr *mcaddr,
 1185     /*const*/ struct in6_mfilter *imf, struct in6_multi **pinm,
 1186     const int delay)
 1187 {
 1188         int error;
 1189 
 1190         IN6_MULTI_LOCK();
 1191         error = in6_joingroup_locked(ifp, mcaddr, NULL, pinm, delay);
 1192         IN6_MULTI_UNLOCK();
 1193         return (error);
 1194 }
 1195 
 1196 /*
 1197  * Join a multicast group; real entry point.
 1198  *
 1199  * Only preserves atomicity at inm level.
 1200  * NOTE: imf argument cannot be const due to sys/tree.h limitations.
 1201  *
 1202  * If the MLD downcall fails, the group is not joined, and an error
 1203  * code is returned.
 1204  */
 1205 int
 1206 in6_joingroup_locked(struct ifnet *ifp, const struct in6_addr *mcaddr,
 1207     /*const*/ struct in6_mfilter *imf, struct in6_multi **pinm,
 1208     const int delay)
 1209 {
 1210         struct in6_multi_head    inmh;
 1211         struct in6_mfilter       timf;
 1212         struct in6_multi        *inm;
 1213         struct ifmultiaddr *ifma;
 1214         int                      error;
 1215 #ifdef KTR
 1216         char                     ip6tbuf[INET6_ADDRSTRLEN];
 1217 #endif
 1218 
 1219 #ifdef INVARIANTS
 1220         /*
 1221          * Sanity: Check scope zone ID was set for ifp, if and
 1222          * only if group is scoped to an interface.
 1223          */
 1224         KASSERT(IN6_IS_ADDR_MULTICAST(mcaddr),
 1225             ("%s: not a multicast address", __func__));
 1226         if (IN6_IS_ADDR_MC_LINKLOCAL(mcaddr) ||
 1227             IN6_IS_ADDR_MC_INTFACELOCAL(mcaddr)) {
 1228                 KASSERT(mcaddr->s6_addr16[1] != 0,
 1229                     ("%s: scope zone ID not set", __func__));
 1230         }
 1231 #endif
 1232 
 1233         IN6_MULTI_LOCK_ASSERT();
 1234         IN6_MULTI_LIST_UNLOCK_ASSERT();
 1235 
 1236         CTR4(KTR_MLD, "%s: join %s on %p(%s))", __func__,
 1237             ip6_sprintf(ip6tbuf, mcaddr), ifp, if_name(ifp));
 1238 
 1239         error = 0;
 1240         inm = NULL;
 1241 
 1242         /*
 1243          * If no imf was specified (i.e. kernel consumer),
 1244          * fake one up and assume it is an ASM join.
 1245          */
 1246         if (imf == NULL) {
 1247                 im6f_init(&timf, MCAST_UNDEFINED, MCAST_EXCLUDE);
 1248                 imf = &timf;
 1249         }
 1250         error = in6_getmulti(ifp, mcaddr, &inm);
 1251         if (error) {
 1252                 CTR1(KTR_MLD, "%s: in6_getmulti() failure", __func__);
 1253                 return (error);
 1254         }
 1255 
 1256         IN6_MULTI_LIST_LOCK();
 1257         CTR1(KTR_MLD, "%s: merge inm state", __func__);
 1258         error = in6m_merge(inm, imf);
 1259         if (error) {
 1260                 CTR1(KTR_MLD, "%s: failed to merge inm state", __func__);
 1261                 goto out_in6m_release;
 1262         }
 1263 
 1264         CTR1(KTR_MLD, "%s: doing mld downcall", __func__);
 1265         error = mld_change_state(inm, delay);
 1266         if (error) {
 1267                 CTR1(KTR_MLD, "%s: failed to update source", __func__);
 1268                 goto out_in6m_release;
 1269         }
 1270 
 1271 out_in6m_release:
 1272         SLIST_INIT(&inmh);
 1273         if (error) {
 1274                 CTR2(KTR_MLD, "%s: dropping ref on %p", __func__, inm);
 1275                 IF_ADDR_RLOCK(ifp);
 1276                 CK_STAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
 1277                         if (ifma->ifma_protospec == inm) {
 1278                                 ifma->ifma_protospec = NULL;
 1279                                 break;
 1280                         }
 1281                 }
 1282                 in6m_disconnect_locked(&inmh, inm);
 1283                 in6m_rele_locked(&inmh, inm);
 1284                 IF_ADDR_RUNLOCK(ifp);
 1285         } else {
 1286                 *pinm = inm;
 1287         }
 1288         IN6_MULTI_LIST_UNLOCK();
 1289         in6m_release_list_deferred(&inmh);
 1290         return (error);
 1291 }
 1292 
 1293 /*
 1294  * Leave a multicast group; unlocked entry point.
 1295  */
 1296 int
 1297 in6_leavegroup(struct in6_multi *inm, /*const*/ struct in6_mfilter *imf)
 1298 {
 1299         int error;
 1300 
 1301         IN6_MULTI_LOCK();
 1302         error = in6_leavegroup_locked(inm, imf);
 1303         IN6_MULTI_UNLOCK();
 1304         return (error);
 1305 }
 1306 
 1307 /*
 1308  * Leave a multicast group; real entry point.
 1309  * All source filters will be expunged.
 1310  *
 1311  * Only preserves atomicity at inm level.
 1312  *
 1313  * Holding the write lock for the INP which contains imf
 1314  * is highly advisable. We can't assert for it as imf does not
 1315  * contain a back-pointer to the owning inp.
 1316  *
 1317  * Note: This is not the same as in6m_release(*) as this function also
 1318  * makes a state change downcall into MLD.
 1319  */
 1320 int
 1321 in6_leavegroup_locked(struct in6_multi *inm, /*const*/ struct in6_mfilter *imf)
 1322 {
 1323         struct in6_multi_head    inmh;
 1324         struct in6_mfilter       timf;
 1325         struct ifnet *ifp;
 1326         int                      error;
 1327 #ifdef KTR
 1328         char                     ip6tbuf[INET6_ADDRSTRLEN];
 1329 #endif
 1330 
 1331         error = 0;
 1332 
 1333         IN6_MULTI_LOCK_ASSERT();
 1334 
 1335         CTR5(KTR_MLD, "%s: leave inm %p, %s/%s, imf %p", __func__,
 1336             inm, ip6_sprintf(ip6tbuf, &inm->in6m_addr),
 1337             (in6m_is_ifp_detached(inm) ? "null" : if_name(inm->in6m_ifp)),
 1338             imf);
 1339 
 1340         /*
 1341          * If no imf was specified (i.e. kernel consumer),
 1342          * fake one up and assume it is an ASM join.
 1343          */
 1344         if (imf == NULL) {
 1345                 im6f_init(&timf, MCAST_EXCLUDE, MCAST_UNDEFINED);
 1346                 imf = &timf;
 1347         }
 1348 
 1349         /*
 1350          * Begin state merge transaction at MLD layer.
 1351          *
 1352          * As this particular invocation should not cause any memory
 1353          * to be allocated, and there is no opportunity to roll back
 1354          * the transaction, it MUST NOT fail.
 1355          */
 1356 
 1357         ifp = inm->in6m_ifp;
 1358         IN6_MULTI_LIST_LOCK();
 1359         CTR1(KTR_MLD, "%s: merge inm state", __func__);
 1360         error = in6m_merge(inm, imf);
 1361         KASSERT(error == 0, ("%s: failed to merge inm state", __func__));
 1362 
 1363         CTR1(KTR_MLD, "%s: doing mld downcall", __func__);
 1364         error = 0;
 1365         if (ifp)
 1366                 error = mld_change_state(inm, 0);
 1367         if (error)
 1368                 CTR1(KTR_MLD, "%s: failed mld downcall", __func__);
 1369 
 1370         CTR2(KTR_MLD, "%s: dropping ref on %p", __func__, inm);
 1371         if (ifp)
 1372                 IF_ADDR_WLOCK(ifp);
 1373 
 1374         SLIST_INIT(&inmh);
 1375         if (inm->in6m_refcount == 1)
 1376                 in6m_disconnect_locked(&inmh, inm);
 1377         in6m_rele_locked(&inmh, inm);
 1378         if (ifp)
 1379                 IF_ADDR_WUNLOCK(ifp);
 1380         IN6_MULTI_LIST_UNLOCK();
 1381         in6m_release_list_deferred(&inmh);
 1382         return (error);
 1383 }
 1384 
 1385 
 1386 /*
 1387  * Block or unblock an ASM multicast source on an inpcb.
 1388  * This implements the delta-based API described in RFC 3678.
 1389  *
 1390  * The delta-based API applies only to exclusive-mode memberships.
 1391  * An MLD downcall will be performed.
 1392  *
 1393  * SMPng: NOTE: Must take Giant as a join may create a new ifma.
 1394  *
 1395  * Return 0 if successful, otherwise return an appropriate error code.
 1396  */
 1397 static int
 1398 in6p_block_unblock_source(struct inpcb *inp, struct sockopt *sopt)
 1399 {
 1400         struct group_source_req          gsr;
 1401         sockunion_t                     *gsa, *ssa;
 1402         struct ifnet                    *ifp;
 1403         struct in6_mfilter              *imf;
 1404         struct ip6_moptions             *imo;
 1405         struct in6_msource              *ims;
 1406         struct in6_multi                        *inm;
 1407         uint16_t                         fmode;
 1408         int                              error, doblock;
 1409 #ifdef KTR
 1410         char                             ip6tbuf[INET6_ADDRSTRLEN];
 1411 #endif
 1412 
 1413         ifp = NULL;
 1414         error = 0;
 1415         doblock = 0;
 1416 
 1417         memset(&gsr, 0, sizeof(struct group_source_req));
 1418         gsa = (sockunion_t *)&gsr.gsr_group;
 1419         ssa = (sockunion_t *)&gsr.gsr_source;
 1420 
 1421         switch (sopt->sopt_name) {
 1422         case MCAST_BLOCK_SOURCE:
 1423         case MCAST_UNBLOCK_SOURCE:
 1424                 error = sooptcopyin(sopt, &gsr,
 1425                     sizeof(struct group_source_req),
 1426                     sizeof(struct group_source_req));
 1427                 if (error)
 1428                         return (error);
 1429 
 1430                 if (gsa->sin6.sin6_family != AF_INET6 ||
 1431                     gsa->sin6.sin6_len != sizeof(struct sockaddr_in6))
 1432                         return (EINVAL);
 1433 
 1434                 if (ssa->sin6.sin6_family != AF_INET6 ||
 1435                     ssa->sin6.sin6_len != sizeof(struct sockaddr_in6))
 1436                         return (EINVAL);
 1437 
 1438                 if (gsr.gsr_interface == 0 || V_if_index < gsr.gsr_interface)
 1439                         return (EADDRNOTAVAIL);
 1440 
 1441                 ifp = ifnet_byindex(gsr.gsr_interface);
 1442 
 1443                 if (sopt->sopt_name == MCAST_BLOCK_SOURCE)
 1444                         doblock = 1;
 1445                 break;
 1446 
 1447         default:
 1448                 CTR2(KTR_MLD, "%s: unknown sopt_name %d",
 1449                     __func__, sopt->sopt_name);
 1450                 return (EOPNOTSUPP);
 1451                 break;
 1452         }
 1453 
 1454         if (!IN6_IS_ADDR_MULTICAST(&gsa->sin6.sin6_addr))
 1455                 return (EINVAL);
 1456 
 1457         (void)in6_setscope(&gsa->sin6.sin6_addr, ifp, NULL);
 1458 
 1459         /*
 1460          * Check if we are actually a member of this group.
 1461          */
 1462         imo = in6p_findmoptions(inp);
 1463         imf = im6o_match_group(imo, ifp, &gsa->sa);
 1464         if (imf == NULL) {
 1465                 error = EADDRNOTAVAIL;
 1466                 goto out_in6p_locked;
 1467         }
 1468         inm = imf->im6f_in6m;
 1469 
 1470         /*
 1471          * Attempting to use the delta-based API on an
 1472          * non exclusive-mode membership is an error.
 1473          */
 1474         fmode = imf->im6f_st[0];
 1475         if (fmode != MCAST_EXCLUDE) {
 1476                 error = EINVAL;
 1477                 goto out_in6p_locked;
 1478         }
 1479 
 1480         /*
 1481          * Deal with error cases up-front:
 1482          *  Asked to block, but already blocked; or
 1483          *  Asked to unblock, but nothing to unblock.
 1484          * If adding a new block entry, allocate it.
 1485          */
 1486         ims = im6o_match_source(imf, &ssa->sa);
 1487         if ((ims != NULL && doblock) || (ims == NULL && !doblock)) {
 1488                 CTR3(KTR_MLD, "%s: source %s %spresent", __func__,
 1489                     ip6_sprintf(ip6tbuf, &ssa->sin6.sin6_addr),
 1490                     doblock ? "" : "not ");
 1491                 error = EADDRNOTAVAIL;
 1492                 goto out_in6p_locked;
 1493         }
 1494 
 1495         INP_WLOCK_ASSERT(inp);
 1496 
 1497         /*
 1498          * Begin state merge transaction at socket layer.
 1499          */
 1500         if (doblock) {
 1501                 CTR2(KTR_MLD, "%s: %s source", __func__, "block");
 1502                 ims = im6f_graft(imf, fmode, &ssa->sin6);
 1503                 if (ims == NULL)
 1504                         error = ENOMEM;
 1505         } else {
 1506                 CTR2(KTR_MLD, "%s: %s source", __func__, "allow");
 1507                 error = im6f_prune(imf, &ssa->sin6);
 1508         }
 1509 
 1510         if (error) {
 1511                 CTR1(KTR_MLD, "%s: merge imf state failed", __func__);
 1512                 goto out_im6f_rollback;
 1513         }
 1514 
 1515         /*
 1516          * Begin state merge transaction at MLD layer.
 1517          */
 1518         IN6_MULTI_LIST_LOCK();
 1519         CTR1(KTR_MLD, "%s: merge inm state", __func__);
 1520         error = in6m_merge(inm, imf);
 1521         if (error)
 1522                 CTR1(KTR_MLD, "%s: failed to merge inm state", __func__);
 1523         else {
 1524                 CTR1(KTR_MLD, "%s: doing mld downcall", __func__);
 1525                 error = mld_change_state(inm, 0);
 1526                 if (error)
 1527                         CTR1(KTR_MLD, "%s: failed mld downcall", __func__);
 1528         }
 1529 
 1530         IN6_MULTI_LIST_UNLOCK();
 1531 
 1532 out_im6f_rollback:
 1533         if (error)
 1534                 im6f_rollback(imf);
 1535         else
 1536                 im6f_commit(imf);
 1537 
 1538         im6f_reap(imf);
 1539 
 1540 out_in6p_locked:
 1541         INP_WUNLOCK(inp);
 1542         return (error);
 1543 }
 1544 
 1545 /*
 1546  * Given an inpcb, return its multicast options structure pointer.  Accepts
 1547  * an unlocked inpcb pointer, but will return it locked.  May sleep.
 1548  *
 1549  * SMPng: NOTE: Potentially calls malloc(M_WAITOK) with Giant held.
 1550  * SMPng: NOTE: Returns with the INP write lock held.
 1551  */
 1552 static struct ip6_moptions *
 1553 in6p_findmoptions(struct inpcb *inp)
 1554 {
 1555         struct ip6_moptions      *imo;
 1556 
 1557         INP_WLOCK(inp);
 1558         if (inp->in6p_moptions != NULL)
 1559                 return (inp->in6p_moptions);
 1560 
 1561         INP_WUNLOCK(inp);
 1562 
 1563         imo = malloc(sizeof(*imo), M_IP6MOPTS, M_WAITOK);
 1564 
 1565         imo->im6o_multicast_ifp = NULL;
 1566         imo->im6o_multicast_hlim = V_ip6_defmcasthlim;
 1567         imo->im6o_multicast_loop = in6_mcast_loop;
 1568         STAILQ_INIT(&imo->im6o_head);
 1569 
 1570         INP_WLOCK(inp);
 1571         if (inp->in6p_moptions != NULL) {
 1572                 free(imo, M_IP6MOPTS);
 1573                 return (inp->in6p_moptions);
 1574         }
 1575         inp->in6p_moptions = imo;
 1576         return (imo);
 1577 }
 1578 
 1579 /*
 1580  * Discard the IPv6 multicast options (and source filters).
 1581  *
 1582  * SMPng: NOTE: assumes INP write lock is held.
 1583  *
 1584  * XXX can all be safely deferred to epoch_call
 1585  *
 1586  */
 1587 
 1588 static void
 1589 inp_gcmoptions(struct ip6_moptions *imo)
 1590 {
 1591         struct in6_mfilter *imf;
 1592         struct in6_multi *inm;
 1593         struct ifnet *ifp;
 1594 
 1595         while ((imf = ip6_mfilter_first(&imo->im6o_head)) != NULL) {
 1596                 ip6_mfilter_remove(&imo->im6o_head, imf);
 1597 
 1598                 im6f_leave(imf);
 1599                 if ((inm = imf->im6f_in6m) != NULL) {
 1600                         if ((ifp = inm->in6m_ifp) != NULL) {
 1601                                 CURVNET_SET(ifp->if_vnet);
 1602                                 (void)in6_leavegroup(inm, imf);
 1603                                 CURVNET_RESTORE();
 1604                         } else {
 1605                                 (void)in6_leavegroup(inm, imf);
 1606                         }
 1607                 }
 1608                 ip6_mfilter_free(imf);
 1609         }
 1610         free(imo, M_IP6MOPTS);
 1611 }
 1612 
 1613 void
 1614 ip6_freemoptions(struct ip6_moptions *imo)
 1615 {
 1616         if (imo == NULL)
 1617                 return;
 1618         inp_gcmoptions(imo);
 1619 }
 1620 
 1621 /*
 1622  * Atomically get source filters on a socket for an IPv6 multicast group.
 1623  * Called with INP lock held; returns with lock released.
 1624  */
 1625 static int
 1626 in6p_get_source_filters(struct inpcb *inp, struct sockopt *sopt)
 1627 {
 1628         struct __msfilterreq     msfr;
 1629         sockunion_t             *gsa;
 1630         struct ifnet            *ifp;
 1631         struct ip6_moptions     *imo;
 1632         struct in6_mfilter      *imf;
 1633         struct ip6_msource      *ims;
 1634         struct in6_msource      *lims;
 1635         struct sockaddr_in6     *psin;
 1636         struct sockaddr_storage *ptss;
 1637         struct sockaddr_storage *tss;
 1638         int                      error;
 1639         size_t                   nsrcs, ncsrcs;
 1640 
 1641         INP_WLOCK_ASSERT(inp);
 1642 
 1643         imo = inp->in6p_moptions;
 1644         KASSERT(imo != NULL, ("%s: null ip6_moptions", __func__));
 1645 
 1646         INP_WUNLOCK(inp);
 1647 
 1648         error = sooptcopyin(sopt, &msfr, sizeof(struct __msfilterreq),
 1649             sizeof(struct __msfilterreq));
 1650         if (error)
 1651                 return (error);
 1652 
 1653         if (msfr.msfr_group.ss_family != AF_INET6 ||
 1654             msfr.msfr_group.ss_len != sizeof(struct sockaddr_in6))
 1655                 return (EINVAL);
 1656 
 1657         gsa = (sockunion_t *)&msfr.msfr_group;
 1658         if (!IN6_IS_ADDR_MULTICAST(&gsa->sin6.sin6_addr))
 1659                 return (EINVAL);
 1660 
 1661         if (msfr.msfr_ifindex == 0 || V_if_index < msfr.msfr_ifindex)
 1662                 return (EADDRNOTAVAIL);
 1663         ifp = ifnet_byindex(msfr.msfr_ifindex);
 1664         if (ifp == NULL)
 1665                 return (EADDRNOTAVAIL);
 1666         (void)in6_setscope(&gsa->sin6.sin6_addr, ifp, NULL);
 1667 
 1668         INP_WLOCK(inp);
 1669 
 1670         /*
 1671          * Lookup group on the socket.
 1672          */
 1673         imf = im6o_match_group(imo, ifp, &gsa->sa);
 1674         if (imf == NULL) {
 1675                 INP_WUNLOCK(inp);
 1676                 return (EADDRNOTAVAIL);
 1677         }
 1678 
 1679         /*
 1680          * Ignore memberships which are in limbo.
 1681          */
 1682         if (imf->im6f_st[1] == MCAST_UNDEFINED) {
 1683                 INP_WUNLOCK(inp);
 1684                 return (EAGAIN);
 1685         }
 1686         msfr.msfr_fmode = imf->im6f_st[1];
 1687 
 1688         /*
 1689          * If the user specified a buffer, copy out the source filter
 1690          * entries to userland gracefully.
 1691          * We only copy out the number of entries which userland
 1692          * has asked for, but we always tell userland how big the
 1693          * buffer really needs to be.
 1694          */
 1695         if (msfr.msfr_nsrcs > in6_mcast_maxsocksrc)
 1696                 msfr.msfr_nsrcs = in6_mcast_maxsocksrc;
 1697         tss = NULL;
 1698         if (msfr.msfr_srcs != NULL && msfr.msfr_nsrcs > 0) {
 1699                 tss = malloc(sizeof(struct sockaddr_storage) * msfr.msfr_nsrcs,
 1700                     M_TEMP, M_NOWAIT | M_ZERO);
 1701                 if (tss == NULL) {
 1702                         INP_WUNLOCK(inp);
 1703                         return (ENOBUFS);
 1704                 }
 1705         }
 1706 
 1707         /*
 1708          * Count number of sources in-mode at t0.
 1709          * If buffer space exists and remains, copy out source entries.
 1710          */
 1711         nsrcs = msfr.msfr_nsrcs;
 1712         ncsrcs = 0;
 1713         ptss = tss;
 1714         RB_FOREACH(ims, ip6_msource_tree, &imf->im6f_sources) {
 1715                 lims = (struct in6_msource *)ims;
 1716                 if (lims->im6sl_st[0] == MCAST_UNDEFINED ||
 1717                     lims->im6sl_st[0] != imf->im6f_st[0])
 1718                         continue;
 1719                 ++ncsrcs;
 1720                 if (tss != NULL && nsrcs > 0) {
 1721                         psin = (struct sockaddr_in6 *)ptss;
 1722                         psin->sin6_family = AF_INET6;
 1723                         psin->sin6_len = sizeof(struct sockaddr_in6);
 1724                         psin->sin6_addr = lims->im6s_addr;
 1725                         psin->sin6_port = 0;
 1726                         --nsrcs;
 1727                         ++ptss;
 1728                 }
 1729         }
 1730 
 1731         INP_WUNLOCK(inp);
 1732 
 1733         if (tss != NULL) {
 1734                 error = copyout(tss, msfr.msfr_srcs,
 1735                     sizeof(struct sockaddr_storage) * msfr.msfr_nsrcs);
 1736                 free(tss, M_TEMP);
 1737                 if (error)
 1738                         return (error);
 1739         }
 1740 
 1741         msfr.msfr_nsrcs = ncsrcs;
 1742         error = sooptcopyout(sopt, &msfr, sizeof(struct __msfilterreq));
 1743 
 1744         return (error);
 1745 }
 1746 
 1747 /*
 1748  * Return the IP multicast options in response to user getsockopt().
 1749  */
 1750 int
 1751 ip6_getmoptions(struct inpcb *inp, struct sockopt *sopt)
 1752 {
 1753         struct ip6_moptions     *im6o;
 1754         int                      error;
 1755         u_int                    optval;
 1756 
 1757         INP_WLOCK(inp);
 1758         im6o = inp->in6p_moptions;
 1759         /*
 1760          * If socket is neither of type SOCK_RAW or SOCK_DGRAM,
 1761          * or is a divert socket, reject it.
 1762          */
 1763         if (inp->inp_socket->so_proto->pr_protocol == IPPROTO_DIVERT ||
 1764             (inp->inp_socket->so_proto->pr_type != SOCK_RAW &&
 1765             inp->inp_socket->so_proto->pr_type != SOCK_DGRAM)) {
 1766                 INP_WUNLOCK(inp);
 1767                 return (EOPNOTSUPP);
 1768         }
 1769 
 1770         error = 0;
 1771         switch (sopt->sopt_name) {
 1772         case IPV6_MULTICAST_IF:
 1773                 if (im6o == NULL || im6o->im6o_multicast_ifp == NULL) {
 1774                         optval = 0;
 1775                 } else {
 1776                         optval = im6o->im6o_multicast_ifp->if_index;
 1777                 }
 1778                 INP_WUNLOCK(inp);
 1779                 error = sooptcopyout(sopt, &optval, sizeof(u_int));
 1780                 break;
 1781 
 1782         case IPV6_MULTICAST_HOPS:
 1783                 if (im6o == NULL)
 1784                         optval = V_ip6_defmcasthlim;
 1785                 else
 1786                         optval = im6o->im6o_multicast_hlim;
 1787                 INP_WUNLOCK(inp);
 1788                 error = sooptcopyout(sopt, &optval, sizeof(u_int));
 1789                 break;
 1790 
 1791         case IPV6_MULTICAST_LOOP:
 1792                 if (im6o == NULL)
 1793                         optval = in6_mcast_loop; /* XXX VIMAGE */
 1794                 else
 1795                         optval = im6o->im6o_multicast_loop;
 1796                 INP_WUNLOCK(inp);
 1797                 error = sooptcopyout(sopt, &optval, sizeof(u_int));
 1798                 break;
 1799 
 1800         case IPV6_MSFILTER:
 1801                 if (im6o == NULL) {
 1802                         error = EADDRNOTAVAIL;
 1803                         INP_WUNLOCK(inp);
 1804                 } else {
 1805                         error = in6p_get_source_filters(inp, sopt);
 1806                 }
 1807                 break;
 1808 
 1809         default:
 1810                 INP_WUNLOCK(inp);
 1811                 error = ENOPROTOOPT;
 1812                 break;
 1813         }
 1814 
 1815         INP_UNLOCK_ASSERT(inp);
 1816 
 1817         return (error);
 1818 }
 1819 
 1820 /*
 1821  * Look up the ifnet to use for a multicast group membership,
 1822  * given the address of an IPv6 group.
 1823  *
 1824  * This routine exists to support legacy IPv6 multicast applications.
 1825  *
 1826  * Use the socket's current FIB number for any required FIB lookup. Look up the
 1827  * group address in the unicast FIB, and use its ifp; usually, this points to
 1828  * the default next-hop.  If the FIB lookup fails, return NULL.
 1829  *
 1830  * FUTURE: Support multiple forwarding tables for IPv6.
 1831  *
 1832  * Returns NULL if no ifp could be found.
 1833  */
 1834 static struct ifnet *
 1835 in6p_lookup_mcast_ifp(const struct inpcb *inp, const struct sockaddr_in6 *gsin6)
 1836 {
 1837         struct nhop6_basic      nh6;
 1838         struct in6_addr         dst;
 1839         uint32_t                scopeid;
 1840         uint32_t                fibnum;
 1841 
 1842         KASSERT(gsin6->sin6_family == AF_INET6,
 1843             ("%s: not AF_INET6 group", __func__));
 1844 
 1845         in6_splitscope(&gsin6->sin6_addr, &dst, &scopeid);
 1846         fibnum = inp->inp_inc.inc_fibnum;
 1847         if (fib6_lookup_nh_basic(fibnum, &dst, scopeid, 0, 0, &nh6) != 0)
 1848                 return (NULL);
 1849 
 1850         return (nh6.nh_ifp);
 1851 }
 1852 
 1853 /*
 1854  * Join an IPv6 multicast group, possibly with a source.
 1855  *
 1856  * FIXME: The KAME use of the unspecified address (::)
 1857  * to join *all* multicast groups is currently unsupported.
 1858  */
 1859 static int
 1860 in6p_join_group(struct inpcb *inp, struct sockopt *sopt)
 1861 {
 1862         struct in6_multi_head            inmh;
 1863         struct group_source_req          gsr;
 1864         sockunion_t                     *gsa, *ssa;
 1865         struct ifnet                    *ifp;
 1866         struct in6_mfilter              *imf;
 1867         struct ip6_moptions             *imo;
 1868         struct in6_multi                *inm;
 1869         struct in6_msource              *lims;
 1870         int                              error, is_new;
 1871 
 1872         SLIST_INIT(&inmh);
 1873         ifp = NULL;
 1874         lims = NULL;
 1875         error = 0;
 1876 
 1877         memset(&gsr, 0, sizeof(struct group_source_req));
 1878         gsa = (sockunion_t *)&gsr.gsr_group;
 1879         gsa->ss.ss_family = AF_UNSPEC;
 1880         ssa = (sockunion_t *)&gsr.gsr_source;
 1881         ssa->ss.ss_family = AF_UNSPEC;
 1882 
 1883         /*
 1884          * Chew everything into struct group_source_req.
 1885          * Overwrite the port field if present, as the sockaddr
 1886          * being copied in may be matched with a binary comparison.
 1887          * Ignore passed-in scope ID.
 1888          */
 1889         switch (sopt->sopt_name) {
 1890         case IPV6_JOIN_GROUP: {
 1891                 struct ipv6_mreq mreq;
 1892 
 1893                 error = sooptcopyin(sopt, &mreq, sizeof(struct ipv6_mreq),
 1894                     sizeof(struct ipv6_mreq));
 1895                 if (error)
 1896                         return (error);
 1897 
 1898                 gsa->sin6.sin6_family = AF_INET6;
 1899                 gsa->sin6.sin6_len = sizeof(struct sockaddr_in6);
 1900                 gsa->sin6.sin6_addr = mreq.ipv6mr_multiaddr;
 1901 
 1902                 if (mreq.ipv6mr_interface == 0) {
 1903                         ifp = in6p_lookup_mcast_ifp(inp, &gsa->sin6);
 1904                 } else {
 1905                         if (V_if_index < mreq.ipv6mr_interface)
 1906                                 return (EADDRNOTAVAIL);
 1907                         ifp = ifnet_byindex(mreq.ipv6mr_interface);
 1908                 }
 1909                 CTR3(KTR_MLD, "%s: ipv6mr_interface = %d, ifp = %p",
 1910                     __func__, mreq.ipv6mr_interface, ifp);
 1911         } break;
 1912 
 1913         case MCAST_JOIN_GROUP:
 1914         case MCAST_JOIN_SOURCE_GROUP:
 1915                 if (sopt->sopt_name == MCAST_JOIN_GROUP) {
 1916                         error = sooptcopyin(sopt, &gsr,
 1917                             sizeof(struct group_req),
 1918                             sizeof(struct group_req));
 1919                 } else if (sopt->sopt_name == MCAST_JOIN_SOURCE_GROUP) {
 1920                         error = sooptcopyin(sopt, &gsr,
 1921                             sizeof(struct group_source_req),
 1922                             sizeof(struct group_source_req));
 1923                 }
 1924                 if (error)
 1925                         return (error);
 1926 
 1927                 if (gsa->sin6.sin6_family != AF_INET6 ||
 1928                     gsa->sin6.sin6_len != sizeof(struct sockaddr_in6))
 1929                         return (EINVAL);
 1930 
 1931                 if (sopt->sopt_name == MCAST_JOIN_SOURCE_GROUP) {
 1932                         if (ssa->sin6.sin6_family != AF_INET6 ||
 1933                             ssa->sin6.sin6_len != sizeof(struct sockaddr_in6))
 1934                                 return (EINVAL);
 1935                         if (IN6_IS_ADDR_MULTICAST(&ssa->sin6.sin6_addr))
 1936                                 return (EINVAL);
 1937                         /*
 1938                          * TODO: Validate embedded scope ID in source
 1939                          * list entry against passed-in ifp, if and only
 1940                          * if source list filter entry is iface or node local.
 1941                          */
 1942                         in6_clearscope(&ssa->sin6.sin6_addr);
 1943                         ssa->sin6.sin6_port = 0;
 1944                         ssa->sin6.sin6_scope_id = 0;
 1945                 }
 1946 
 1947                 if (gsr.gsr_interface == 0 || V_if_index < gsr.gsr_interface)
 1948                         return (EADDRNOTAVAIL);
 1949                 ifp = ifnet_byindex(gsr.gsr_interface);
 1950                 break;
 1951 
 1952         default:
 1953                 CTR2(KTR_MLD, "%s: unknown sopt_name %d",
 1954                     __func__, sopt->sopt_name);
 1955                 return (EOPNOTSUPP);
 1956                 break;
 1957         }
 1958 
 1959         if (!IN6_IS_ADDR_MULTICAST(&gsa->sin6.sin6_addr))
 1960                 return (EINVAL);
 1961 
 1962         if (ifp == NULL || (ifp->if_flags & IFF_MULTICAST) == 0)
 1963                 return (EADDRNOTAVAIL);
 1964 
 1965         gsa->sin6.sin6_port = 0;
 1966         gsa->sin6.sin6_scope_id = 0;
 1967 
 1968         /*
 1969          * Always set the scope zone ID on memberships created from userland.
 1970          * Use the passed-in ifp to do this.
 1971          * XXX The in6_setscope() return value is meaningless.
 1972          * XXX SCOPE6_LOCK() is taken by in6_setscope().
 1973          */
 1974         (void)in6_setscope(&gsa->sin6.sin6_addr, ifp, NULL);
 1975 
 1976         IN6_MULTI_LOCK();
 1977 
 1978         /*
 1979          * Find the membership in the membership list.
 1980          */
 1981         imo = in6p_findmoptions(inp);
 1982         imf = im6o_match_group(imo, ifp, &gsa->sa);
 1983         if (imf == NULL) {
 1984                 is_new = 1;
 1985                 inm = NULL;
 1986 
 1987                 if (ip6_mfilter_count(&imo->im6o_head) >= IPV6_MAX_MEMBERSHIPS) {
 1988                         error = ENOMEM;
 1989                         goto out_in6p_locked;
 1990                 }
 1991         } else {
 1992                 is_new = 0;
 1993                 inm = imf->im6f_in6m;
 1994 
 1995                 if (ssa->ss.ss_family != AF_UNSPEC) {
 1996                         /*
 1997                          * MCAST_JOIN_SOURCE_GROUP on an exclusive membership
 1998                          * is an error. On an existing inclusive membership,
 1999                          * it just adds the source to the filter list.
 2000                          */
 2001                         if (imf->im6f_st[1] != MCAST_INCLUDE) {
 2002                                 error = EINVAL;
 2003                                 goto out_in6p_locked;
 2004                         }
 2005                         /*
 2006                          * Throw out duplicates.
 2007                          *
 2008                          * XXX FIXME: This makes a naive assumption that
 2009                          * even if entries exist for *ssa in this imf,
 2010                          * they will be rejected as dupes, even if they
 2011                          * are not valid in the current mode (in-mode).
 2012                          *
 2013                          * in6_msource is transactioned just as for anything
 2014                          * else in SSM -- but note naive use of in6m_graft()
 2015                          * below for allocating new filter entries.
 2016                          *
 2017                          * This is only an issue if someone mixes the
 2018                          * full-state SSM API with the delta-based API,
 2019                          * which is discouraged in the relevant RFCs.
 2020                          */
 2021                         lims = im6o_match_source(imf, &ssa->sa);
 2022                         if (lims != NULL /*&&
 2023                             lims->im6sl_st[1] == MCAST_INCLUDE*/) {
 2024                                 error = EADDRNOTAVAIL;
 2025                                 goto out_in6p_locked;
 2026                         }
 2027                 } else {
 2028                         /*
 2029                          * MCAST_JOIN_GROUP alone, on any existing membership,
 2030                          * is rejected, to stop the same inpcb tying up
 2031                          * multiple refs to the in_multi.
 2032                          * On an existing inclusive membership, this is also
 2033                          * an error; if you want to change filter mode,
 2034                          * you must use the userland API setsourcefilter().
 2035                          * XXX We don't reject this for imf in UNDEFINED
 2036                          * state at t1, because allocation of a filter
 2037                          * is atomic with allocation of a membership.
 2038                          */
 2039                         error = EADDRINUSE;
 2040                         goto out_in6p_locked;
 2041                 }
 2042         }
 2043 
 2044         /*
 2045          * Begin state merge transaction at socket layer.
 2046          */
 2047         INP_WLOCK_ASSERT(inp);
 2048 
 2049         /*
 2050          * Graft new source into filter list for this inpcb's
 2051          * membership of the group. The in6_multi may not have
 2052          * been allocated yet if this is a new membership, however,
 2053          * the in_mfilter slot will be allocated and must be initialized.
 2054          *
 2055          * Note: Grafting of exclusive mode filters doesn't happen
 2056          * in this path.
 2057          * XXX: Should check for non-NULL lims (node exists but may
 2058          * not be in-mode) for interop with full-state API.
 2059          */
 2060         if (ssa->ss.ss_family != AF_UNSPEC) {
 2061                 /* Membership starts in IN mode */
 2062                 if (is_new) {
 2063                         CTR1(KTR_MLD, "%s: new join w/source", __func__);
 2064                         imf = ip6_mfilter_alloc(M_NOWAIT, MCAST_UNDEFINED, MCAST_INCLUDE);
 2065                         if (imf == NULL) {
 2066                                 error = ENOMEM;
 2067                                 goto out_in6p_locked;
 2068                         }
 2069                 } else {
 2070                         CTR2(KTR_MLD, "%s: %s source", __func__, "allow");
 2071                 }
 2072                 lims = im6f_graft(imf, MCAST_INCLUDE, &ssa->sin6);
 2073                 if (lims == NULL) {
 2074                         CTR1(KTR_MLD, "%s: merge imf state failed",
 2075                             __func__);
 2076                         error = ENOMEM;
 2077                         goto out_in6p_locked;
 2078                 }
 2079         } else {
 2080                 /* No address specified; Membership starts in EX mode */
 2081                 if (is_new) {
 2082                         CTR1(KTR_MLD, "%s: new join w/o source", __func__);
 2083                         imf = ip6_mfilter_alloc(M_NOWAIT, MCAST_UNDEFINED, MCAST_EXCLUDE);
 2084                         if (imf == NULL) {
 2085                                 error = ENOMEM;
 2086                                 goto out_in6p_locked;
 2087                         }
 2088                 }
 2089         }
 2090 
 2091         /*
 2092          * Begin state merge transaction at MLD layer.
 2093          */
 2094         if (is_new) {
 2095                 in_pcbref(inp);
 2096                 INP_WUNLOCK(inp);
 2097 
 2098                 error = in6_joingroup_locked(ifp, &gsa->sin6.sin6_addr, imf,
 2099                     &imf->im6f_in6m, 0);
 2100 
 2101                 INP_WLOCK(inp);
 2102                 if (in_pcbrele_wlocked(inp)) {
 2103                         error = ENXIO;
 2104                         goto out_in6p_unlocked;
 2105                 }
 2106                 if (error) {
 2107                         goto out_in6p_locked;
 2108                 }
 2109                 /*
 2110                  * NOTE: Refcount from in6_joingroup_locked()
 2111                  * is protecting membership.
 2112                  */
 2113                 ip6_mfilter_insert(&imo->im6o_head, imf);
 2114         } else {
 2115                 CTR1(KTR_MLD, "%s: merge inm state", __func__);
 2116                 IN6_MULTI_LIST_LOCK();
 2117                 error = in6m_merge(inm, imf);
 2118                 if (error) {
 2119                         CTR1(KTR_MLD, "%s: failed to merge inm state",
 2120                             __func__);
 2121                         IN6_MULTI_LIST_UNLOCK();
 2122                         im6f_rollback(imf);
 2123                         im6f_reap(imf);
 2124                         goto out_in6p_locked;
 2125                 }
 2126                 CTR1(KTR_MLD, "%s: doing mld downcall", __func__);
 2127                 error = mld_change_state(inm, 0);
 2128                 IN6_MULTI_LIST_UNLOCK();
 2129 
 2130                 if (error) {
 2131                         CTR1(KTR_MLD, "%s: failed mld downcall",
 2132                              __func__);
 2133                         im6f_rollback(imf);
 2134                         im6f_reap(imf);
 2135                         goto out_in6p_locked;
 2136                 }
 2137         }
 2138 
 2139         im6f_commit(imf);
 2140         imf = NULL;
 2141 
 2142 out_in6p_locked:
 2143         INP_WUNLOCK(inp);
 2144 out_in6p_unlocked:
 2145         IN6_MULTI_UNLOCK();
 2146 
 2147         if (is_new && imf) {
 2148                 if (imf->im6f_in6m != NULL) {
 2149                         struct in6_multi_head inmh;
 2150 
 2151                         SLIST_INIT(&inmh);
 2152                         SLIST_INSERT_HEAD(&inmh, imf->im6f_in6m, in6m_defer);
 2153                         in6m_release_list_deferred(&inmh);
 2154                 }
 2155                 ip6_mfilter_free(imf);
 2156         }
 2157         return (error);
 2158 }
 2159 
 2160 /*
 2161  * Leave an IPv6 multicast group on an inpcb, possibly with a source.
 2162  */
 2163 static int
 2164 in6p_leave_group(struct inpcb *inp, struct sockopt *sopt)
 2165 {
 2166         struct ipv6_mreq                 mreq;
 2167         struct group_source_req          gsr;
 2168         sockunion_t                     *gsa, *ssa;
 2169         struct ifnet                    *ifp;
 2170         struct in6_mfilter              *imf;
 2171         struct ip6_moptions             *imo;
 2172         struct in6_msource              *ims;
 2173         struct in6_multi                *inm;
 2174         uint32_t                         ifindex;
 2175         int                              error;
 2176         bool                             is_final;
 2177 #ifdef KTR
 2178         char                             ip6tbuf[INET6_ADDRSTRLEN];
 2179 #endif
 2180 
 2181         ifp = NULL;
 2182         ifindex = 0;
 2183         error = 0;
 2184         is_final = true;
 2185 
 2186         memset(&gsr, 0, sizeof(struct group_source_req));
 2187         gsa = (sockunion_t *)&gsr.gsr_group;
 2188         gsa->ss.ss_family = AF_UNSPEC;
 2189         ssa = (sockunion_t *)&gsr.gsr_source;
 2190         ssa->ss.ss_family = AF_UNSPEC;
 2191 
 2192         /*
 2193          * Chew everything passed in up into a struct group_source_req
 2194          * as that is easier to process.
 2195          * Note: Any embedded scope ID in the multicast group passed
 2196          * in by userland is ignored, the interface index is the recommended
 2197          * mechanism to specify an interface; see below.
 2198          */
 2199         switch (sopt->sopt_name) {
 2200         case IPV6_LEAVE_GROUP:
 2201                 error = sooptcopyin(sopt, &mreq, sizeof(struct ipv6_mreq),
 2202                     sizeof(struct ipv6_mreq));
 2203                 if (error)
 2204                         return (error);
 2205                 gsa->sin6.sin6_family = AF_INET6;
 2206                 gsa->sin6.sin6_len = sizeof(struct sockaddr_in6);
 2207                 gsa->sin6.sin6_addr = mreq.ipv6mr_multiaddr;
 2208                 gsa->sin6.sin6_port = 0;
 2209                 gsa->sin6.sin6_scope_id = 0;
 2210                 ifindex = mreq.ipv6mr_interface;
 2211                 break;
 2212 
 2213         case MCAST_LEAVE_GROUP:
 2214         case MCAST_LEAVE_SOURCE_GROUP:
 2215                 if (sopt->sopt_name == MCAST_LEAVE_GROUP) {
 2216                         error = sooptcopyin(sopt, &gsr,
 2217                             sizeof(struct group_req),
 2218                             sizeof(struct group_req));
 2219                 } else if (sopt->sopt_name == MCAST_LEAVE_SOURCE_GROUP) {
 2220                         error = sooptcopyin(sopt, &gsr,
 2221                             sizeof(struct group_source_req),
 2222                             sizeof(struct group_source_req));
 2223                 }
 2224                 if (error)
 2225                         return (error);
 2226 
 2227                 if (gsa->sin6.sin6_family != AF_INET6 ||
 2228                     gsa->sin6.sin6_len != sizeof(struct sockaddr_in6))
 2229                         return (EINVAL);
 2230                 if (sopt->sopt_name == MCAST_LEAVE_SOURCE_GROUP) {
 2231                         if (ssa->sin6.sin6_family != AF_INET6 ||
 2232                             ssa->sin6.sin6_len != sizeof(struct sockaddr_in6))
 2233                                 return (EINVAL);
 2234                         if (IN6_IS_ADDR_MULTICAST(&ssa->sin6.sin6_addr))
 2235                                 return (EINVAL);
 2236                         /*
 2237                          * TODO: Validate embedded scope ID in source
 2238                          * list entry against passed-in ifp, if and only
 2239                          * if source list filter entry is iface or node local.
 2240                          */
 2241                         in6_clearscope(&ssa->sin6.sin6_addr);
 2242                 }
 2243                 gsa->sin6.sin6_port = 0;
 2244                 gsa->sin6.sin6_scope_id = 0;
 2245                 ifindex = gsr.gsr_interface;
 2246                 break;
 2247 
 2248         default:
 2249                 CTR2(KTR_MLD, "%s: unknown sopt_name %d",
 2250                     __func__, sopt->sopt_name);
 2251                 return (EOPNOTSUPP);
 2252                 break;
 2253         }
 2254 
 2255         if (!IN6_IS_ADDR_MULTICAST(&gsa->sin6.sin6_addr))
 2256                 return (EINVAL);
 2257 
 2258         /*
 2259          * Validate interface index if provided. If no interface index
 2260          * was provided separately, attempt to look the membership up
 2261          * from the default scope as a last resort to disambiguate
 2262          * the membership we are being asked to leave.
 2263          * XXX SCOPE6 lock potentially taken here.
 2264          */
 2265         if (ifindex != 0) {
 2266                 if (V_if_index < ifindex)
 2267                         return (EADDRNOTAVAIL);
 2268                 ifp = ifnet_byindex(ifindex);
 2269                 if (ifp == NULL)
 2270                         return (EADDRNOTAVAIL);
 2271                 (void)in6_setscope(&gsa->sin6.sin6_addr, ifp, NULL);
 2272         } else {
 2273                 error = sa6_embedscope(&gsa->sin6, V_ip6_use_defzone);
 2274                 if (error)
 2275                         return (EADDRNOTAVAIL);
 2276                 /*
 2277                  * Some badly behaved applications don't pass an ifindex
 2278                  * or a scope ID, which is an API violation. In this case,
 2279                  * perform a lookup as per a v6 join.
 2280                  *
 2281                  * XXX For now, stomp on zone ID for the corner case.
 2282                  * This is not the 'KAME way', but we need to see the ifp
 2283                  * directly until such time as this implementation is
 2284                  * refactored, assuming the scope IDs are the way to go.
 2285                  */
 2286                 ifindex = ntohs(gsa->sin6.sin6_addr.s6_addr16[1]);
 2287                 if (ifindex == 0) {
 2288                         CTR2(KTR_MLD, "%s: warning: no ifindex, looking up "
 2289                             "ifp for group %s.", __func__,
 2290                             ip6_sprintf(ip6tbuf, &gsa->sin6.sin6_addr));
 2291                         ifp = in6p_lookup_mcast_ifp(inp, &gsa->sin6);
 2292                 } else {
 2293                         ifp = ifnet_byindex(ifindex);
 2294                 }
 2295                 if (ifp == NULL)
 2296                         return (EADDRNOTAVAIL);
 2297         }
 2298 
 2299         CTR2(KTR_MLD, "%s: ifp = %p", __func__, ifp);
 2300         KASSERT(ifp != NULL, ("%s: ifp did not resolve", __func__));
 2301 
 2302         IN6_MULTI_LOCK();
 2303 
 2304         /*
 2305          * Find the membership in the membership list.
 2306          */
 2307         imo = in6p_findmoptions(inp);
 2308         imf = im6o_match_group(imo, ifp, &gsa->sa);
 2309         if (imf == NULL) {
 2310                 error = EADDRNOTAVAIL;
 2311                 goto out_in6p_locked;
 2312         }
 2313         inm = imf->im6f_in6m;
 2314 
 2315         if (ssa->ss.ss_family != AF_UNSPEC)
 2316                 is_final = false;
 2317 
 2318         /*
 2319          * Begin state merge transaction at socket layer.
 2320          */
 2321         INP_WLOCK_ASSERT(inp);
 2322 
 2323         /*
 2324          * If we were instructed only to leave a given source, do so.
 2325          * MCAST_LEAVE_SOURCE_GROUP is only valid for inclusive memberships.
 2326          */
 2327         if (is_final) {
 2328                 ip6_mfilter_remove(&imo->im6o_head, imf);
 2329                 im6f_leave(imf);
 2330 
 2331                 /*
 2332                  * Give up the multicast address record to which
 2333                  * the membership points.
 2334                  */
 2335                 (void)in6_leavegroup_locked(inm, imf);
 2336         } else {
 2337                 if (imf->im6f_st[0] == MCAST_EXCLUDE) {
 2338                         error = EADDRNOTAVAIL;
 2339                         goto out_in6p_locked;
 2340                 }
 2341                 ims = im6o_match_source(imf, &ssa->sa);
 2342                 if (ims == NULL) {
 2343                         CTR3(KTR_MLD, "%s: source %p %spresent", __func__,
 2344                             ip6_sprintf(ip6tbuf, &ssa->sin6.sin6_addr),
 2345                             "not ");
 2346                         error = EADDRNOTAVAIL;
 2347                         goto out_in6p_locked;
 2348                 }
 2349                 CTR2(KTR_MLD, "%s: %s source", __func__, "block");
 2350                 error = im6f_prune(imf, &ssa->sin6);
 2351                 if (error) {
 2352                         CTR1(KTR_MLD, "%s: merge imf state failed",
 2353                             __func__);
 2354                         goto out_in6p_locked;
 2355                 }
 2356         }
 2357 
 2358         /*
 2359          * Begin state merge transaction at MLD layer.
 2360          */
 2361         if (!is_final) {
 2362                 CTR1(KTR_MLD, "%s: merge inm state", __func__);
 2363                 IN6_MULTI_LIST_LOCK();
 2364                 error = in6m_merge(inm, imf);
 2365                 if (error) {
 2366                         CTR1(KTR_MLD, "%s: failed to merge inm state",
 2367                             __func__);
 2368                         IN6_MULTI_LIST_UNLOCK();
 2369                         im6f_rollback(imf);
 2370                         im6f_reap(imf);
 2371                         goto out_in6p_locked;
 2372                 }
 2373 
 2374                 CTR1(KTR_MLD, "%s: doing mld downcall", __func__);
 2375                 error = mld_change_state(inm, 0);
 2376                 IN6_MULTI_LIST_UNLOCK();
 2377                 if (error) {
 2378                         CTR1(KTR_MLD, "%s: failed mld downcall",
 2379                              __func__);
 2380                         im6f_rollback(imf);
 2381                         im6f_reap(imf);
 2382                         goto out_in6p_locked;
 2383                 }
 2384         }
 2385 
 2386         im6f_commit(imf);
 2387         im6f_reap(imf);
 2388 
 2389 out_in6p_locked:
 2390         INP_WUNLOCK(inp);
 2391 
 2392         if (is_final && imf)
 2393                 ip6_mfilter_free(imf);
 2394 
 2395         IN6_MULTI_UNLOCK();
 2396         return (error);
 2397 }
 2398 
 2399 /*
 2400  * Select the interface for transmitting IPv6 multicast datagrams.
 2401  *
 2402  * Either an instance of struct in6_addr or an instance of struct ipv6_mreqn
 2403  * may be passed to this socket option. An address of in6addr_any or an
 2404  * interface index of 0 is used to remove a previous selection.
 2405  * When no interface is selected, one is chosen for every send.
 2406  */
 2407 static int
 2408 in6p_set_multicast_if(struct inpcb *inp, struct sockopt *sopt)
 2409 {
 2410         struct ifnet            *ifp;
 2411         struct ip6_moptions     *imo;
 2412         u_int                    ifindex;
 2413         int                      error;
 2414 
 2415         if (sopt->sopt_valsize != sizeof(u_int))
 2416                 return (EINVAL);
 2417 
 2418         error = sooptcopyin(sopt, &ifindex, sizeof(u_int), sizeof(u_int));
 2419         if (error)
 2420                 return (error);
 2421         if (V_if_index < ifindex)
 2422                 return (EINVAL);
 2423         if (ifindex == 0)
 2424                 ifp = NULL;
 2425         else {
 2426                 ifp = ifnet_byindex(ifindex);
 2427                 if (ifp == NULL)
 2428                         return (EINVAL);
 2429                 if ((ifp->if_flags & IFF_MULTICAST) == 0)
 2430                         return (EADDRNOTAVAIL);
 2431         }
 2432         imo = in6p_findmoptions(inp);
 2433         imo->im6o_multicast_ifp = ifp;
 2434         INP_WUNLOCK(inp);
 2435 
 2436         return (0);
 2437 }
 2438 
 2439 /*
 2440  * Atomically set source filters on a socket for an IPv6 multicast group.
 2441  *
 2442  * SMPng: NOTE: Potentially calls malloc(M_WAITOK) with Giant held.
 2443  */
 2444 static int
 2445 in6p_set_source_filters(struct inpcb *inp, struct sockopt *sopt)
 2446 {
 2447         struct __msfilterreq     msfr;
 2448         sockunion_t             *gsa;
 2449         struct ifnet            *ifp;
 2450         struct in6_mfilter      *imf;
 2451         struct ip6_moptions     *imo;
 2452         struct in6_multi                *inm;
 2453         int                      error;
 2454 
 2455         error = sooptcopyin(sopt, &msfr, sizeof(struct __msfilterreq),
 2456             sizeof(struct __msfilterreq));
 2457         if (error)
 2458                 return (error);
 2459 
 2460         if (msfr.msfr_nsrcs > in6_mcast_maxsocksrc)
 2461                 return (ENOBUFS);
 2462 
 2463         if (msfr.msfr_fmode != MCAST_EXCLUDE &&
 2464             msfr.msfr_fmode != MCAST_INCLUDE)
 2465                 return (EINVAL);
 2466 
 2467         if (msfr.msfr_group.ss_family != AF_INET6 ||
 2468             msfr.msfr_group.ss_len != sizeof(struct sockaddr_in6))
 2469                 return (EINVAL);
 2470 
 2471         gsa = (sockunion_t *)&msfr.msfr_group;
 2472         if (!IN6_IS_ADDR_MULTICAST(&gsa->sin6.sin6_addr))
 2473                 return (EINVAL);
 2474 
 2475         gsa->sin6.sin6_port = 0;        /* ignore port */
 2476 
 2477         if (msfr.msfr_ifindex == 0 || V_if_index < msfr.msfr_ifindex)
 2478                 return (EADDRNOTAVAIL);
 2479         ifp = ifnet_byindex(msfr.msfr_ifindex);
 2480         if (ifp == NULL)
 2481                 return (EADDRNOTAVAIL);
 2482         (void)in6_setscope(&gsa->sin6.sin6_addr, ifp, NULL);
 2483 
 2484         /*
 2485          * Take the INP write lock.
 2486          * Check if this socket is a member of this group.
 2487          */
 2488         imo = in6p_findmoptions(inp);
 2489         imf = im6o_match_group(imo, ifp, &gsa->sa);
 2490         if (imf == NULL) {
 2491                 error = EADDRNOTAVAIL;
 2492                 goto out_in6p_locked;
 2493         }
 2494         inm = imf->im6f_in6m;
 2495 
 2496         /*
 2497          * Begin state merge transaction at socket layer.
 2498          */
 2499         INP_WLOCK_ASSERT(inp);
 2500 
 2501         imf->im6f_st[1] = msfr.msfr_fmode;
 2502 
 2503         /*
 2504          * Apply any new source filters, if present.
 2505          * Make a copy of the user-space source vector so
 2506          * that we may copy them with a single copyin. This
 2507          * allows us to deal with page faults up-front.
 2508          */
 2509         if (msfr.msfr_nsrcs > 0) {
 2510                 struct in6_msource      *lims;
 2511                 struct sockaddr_in6     *psin;
 2512                 struct sockaddr_storage *kss, *pkss;
 2513                 int                      i;
 2514 
 2515                 INP_WUNLOCK(inp);
 2516  
 2517                 CTR2(KTR_MLD, "%s: loading %lu source list entries",
 2518                     __func__, (unsigned long)msfr.msfr_nsrcs);
 2519                 kss = malloc(sizeof(struct sockaddr_storage) * msfr.msfr_nsrcs,
 2520                     M_TEMP, M_WAITOK);
 2521                 error = copyin(msfr.msfr_srcs, kss,
 2522                     sizeof(struct sockaddr_storage) * msfr.msfr_nsrcs);
 2523                 if (error) {
 2524                         free(kss, M_TEMP);
 2525                         return (error);
 2526                 }
 2527 
 2528                 INP_WLOCK(inp);
 2529 
 2530                 /*
 2531                  * Mark all source filters as UNDEFINED at t1.
 2532                  * Restore new group filter mode, as im6f_leave()
 2533                  * will set it to INCLUDE.
 2534                  */
 2535                 im6f_leave(imf);
 2536                 imf->im6f_st[1] = msfr.msfr_fmode;
 2537 
 2538                 /*
 2539                  * Update socket layer filters at t1, lazy-allocating
 2540                  * new entries. This saves a bunch of memory at the
 2541                  * cost of one RB_FIND() per source entry; duplicate
 2542                  * entries in the msfr_nsrcs vector are ignored.
 2543                  * If we encounter an error, rollback transaction.
 2544                  *
 2545                  * XXX This too could be replaced with a set-symmetric
 2546                  * difference like loop to avoid walking from root
 2547                  * every time, as the key space is common.
 2548                  */
 2549                 for (i = 0, pkss = kss; i < msfr.msfr_nsrcs; i++, pkss++) {
 2550                         psin = (struct sockaddr_in6 *)pkss;
 2551                         if (psin->sin6_family != AF_INET6) {
 2552                                 error = EAFNOSUPPORT;
 2553                                 break;
 2554                         }
 2555                         if (psin->sin6_len != sizeof(struct sockaddr_in6)) {
 2556                                 error = EINVAL;
 2557                                 break;
 2558                         }
 2559                         if (IN6_IS_ADDR_MULTICAST(&psin->sin6_addr)) {
 2560                                 error = EINVAL;
 2561                                 break;
 2562                         }
 2563                         /*
 2564                          * TODO: Validate embedded scope ID in source
 2565                          * list entry against passed-in ifp, if and only
 2566                          * if source list filter entry is iface or node local.
 2567                          */
 2568                         in6_clearscope(&psin->sin6_addr);
 2569                         error = im6f_get_source(imf, psin, &lims);
 2570                         if (error)
 2571                                 break;
 2572                         lims->im6sl_st[1] = imf->im6f_st[1];
 2573                 }
 2574                 free(kss, M_TEMP);
 2575         }
 2576 
 2577         if (error)
 2578                 goto out_im6f_rollback;
 2579 
 2580         INP_WLOCK_ASSERT(inp);
 2581         IN6_MULTI_LIST_LOCK();
 2582 
 2583         /*
 2584          * Begin state merge transaction at MLD layer.
 2585          */
 2586         CTR1(KTR_MLD, "%s: merge inm state", __func__);
 2587         error = in6m_merge(inm, imf);
 2588         if (error)
 2589                 CTR1(KTR_MLD, "%s: failed to merge inm state", __func__);
 2590         else {
 2591                 CTR1(KTR_MLD, "%s: doing mld downcall", __func__);
 2592                 error = mld_change_state(inm, 0);
 2593                 if (error)
 2594                         CTR1(KTR_MLD, "%s: failed mld downcall", __func__);
 2595         }
 2596 
 2597         IN6_MULTI_LIST_UNLOCK();
 2598 
 2599 out_im6f_rollback:
 2600         if (error)
 2601                 im6f_rollback(imf);
 2602         else
 2603                 im6f_commit(imf);
 2604 
 2605         im6f_reap(imf);
 2606 
 2607 out_in6p_locked:
 2608         INP_WUNLOCK(inp);
 2609         return (error);
 2610 }
 2611 
 2612 /*
 2613  * Set the IP multicast options in response to user setsockopt().
 2614  *
 2615  * Many of the socket options handled in this function duplicate the
 2616  * functionality of socket options in the regular unicast API. However,
 2617  * it is not possible to merge the duplicate code, because the idempotence
 2618  * of the IPv6 multicast part of the BSD Sockets API must be preserved;
 2619  * the effects of these options must be treated as separate and distinct.
 2620  *
 2621  * SMPng: XXX: Unlocked read of inp_socket believed OK.
 2622  */
 2623 int
 2624 ip6_setmoptions(struct inpcb *inp, struct sockopt *sopt)
 2625 {
 2626         struct ip6_moptions     *im6o;
 2627         int                      error;
 2628 
 2629         error = 0;
 2630 
 2631         /*
 2632          * If socket is neither of type SOCK_RAW or SOCK_DGRAM,
 2633          * or is a divert socket, reject it.
 2634          */
 2635         if (inp->inp_socket->so_proto->pr_protocol == IPPROTO_DIVERT ||
 2636             (inp->inp_socket->so_proto->pr_type != SOCK_RAW &&
 2637              inp->inp_socket->so_proto->pr_type != SOCK_DGRAM))
 2638                 return (EOPNOTSUPP);
 2639 
 2640         switch (sopt->sopt_name) {
 2641         case IPV6_MULTICAST_IF:
 2642                 error = in6p_set_multicast_if(inp, sopt);
 2643                 break;
 2644 
 2645         case IPV6_MULTICAST_HOPS: {
 2646                 int hlim;
 2647 
 2648                 if (sopt->sopt_valsize != sizeof(int)) {
 2649                         error = EINVAL;
 2650                         break;
 2651                 }
 2652                 error = sooptcopyin(sopt, &hlim, sizeof(hlim), sizeof(int));
 2653                 if (error)
 2654                         break;
 2655                 if (hlim < -1 || hlim > 255) {
 2656                         error = EINVAL;
 2657                         break;
 2658                 } else if (hlim == -1) {
 2659                         hlim = V_ip6_defmcasthlim;
 2660                 }
 2661                 im6o = in6p_findmoptions(inp);
 2662                 im6o->im6o_multicast_hlim = hlim;
 2663                 INP_WUNLOCK(inp);
 2664                 break;
 2665         }
 2666 
 2667         case IPV6_MULTICAST_LOOP: {
 2668                 u_int loop;
 2669 
 2670                 /*
 2671                  * Set the loopback flag for outgoing multicast packets.
 2672                  * Must be zero or one.
 2673                  */
 2674                 if (sopt->sopt_valsize != sizeof(u_int)) {
 2675                         error = EINVAL;
 2676                         break;
 2677                 }
 2678                 error = sooptcopyin(sopt, &loop, sizeof(u_int), sizeof(u_int));
 2679                 if (error)
 2680                         break;
 2681                 if (loop > 1) {
 2682                         error = EINVAL;
 2683                         break;
 2684                 }
 2685                 im6o = in6p_findmoptions(inp);
 2686                 im6o->im6o_multicast_loop = loop;
 2687                 INP_WUNLOCK(inp);
 2688                 break;
 2689         }
 2690 
 2691         case IPV6_JOIN_GROUP:
 2692         case MCAST_JOIN_GROUP:
 2693         case MCAST_JOIN_SOURCE_GROUP:
 2694                 error = in6p_join_group(inp, sopt);
 2695                 break;
 2696 
 2697         case IPV6_LEAVE_GROUP:
 2698         case MCAST_LEAVE_GROUP:
 2699         case MCAST_LEAVE_SOURCE_GROUP:
 2700                 error = in6p_leave_group(inp, sopt);
 2701                 break;
 2702 
 2703         case MCAST_BLOCK_SOURCE:
 2704         case MCAST_UNBLOCK_SOURCE:
 2705                 error = in6p_block_unblock_source(inp, sopt);
 2706                 break;
 2707 
 2708         case IPV6_MSFILTER:
 2709                 error = in6p_set_source_filters(inp, sopt);
 2710                 break;
 2711 
 2712         default:
 2713                 error = EOPNOTSUPP;
 2714                 break;
 2715         }
 2716 
 2717         INP_UNLOCK_ASSERT(inp);
 2718 
 2719         return (error);
 2720 }
 2721 
 2722 /*
 2723  * Expose MLD's multicast filter mode and source list(s) to userland,
 2724  * keyed by (ifindex, group).
 2725  * The filter mode is written out as a uint32_t, followed by
 2726  * 0..n of struct in6_addr.
 2727  * For use by ifmcstat(8).
 2728  * SMPng: NOTE: unlocked read of ifindex space.
 2729  */
 2730 static int
 2731 sysctl_ip6_mcast_filters(SYSCTL_HANDLER_ARGS)
 2732 {
 2733         struct in6_addr                  mcaddr;
 2734         struct in6_addr                  src;
 2735         struct ifnet                    *ifp;
 2736         struct ifmultiaddr              *ifma;
 2737         struct in6_multi                *inm;
 2738         struct ip6_msource              *ims;
 2739         int                             *name;
 2740         int                              retval;
 2741         u_int                            namelen;
 2742         uint32_t                         fmode, ifindex;
 2743 #ifdef KTR
 2744         char                             ip6tbuf[INET6_ADDRSTRLEN];
 2745 #endif
 2746 
 2747         name = (int *)arg1;
 2748         namelen = arg2;
 2749 
 2750         if (req->newptr != NULL)
 2751                 return (EPERM);
 2752 
 2753         /* int: ifindex + 4 * 32 bits of IPv6 address */
 2754         if (namelen != 5)
 2755                 return (EINVAL);
 2756 
 2757         ifindex = name[0];
 2758         if (ifindex <= 0 || ifindex > V_if_index) {
 2759                 CTR2(KTR_MLD, "%s: ifindex %u out of range",
 2760                     __func__, ifindex);
 2761                 return (ENOENT);
 2762         }
 2763 
 2764         memcpy(&mcaddr, &name[1], sizeof(struct in6_addr));
 2765         if (!IN6_IS_ADDR_MULTICAST(&mcaddr)) {
 2766                 CTR2(KTR_MLD, "%s: group %s is not multicast",
 2767                     __func__, ip6_sprintf(ip6tbuf, &mcaddr));
 2768                 return (EINVAL);
 2769         }
 2770 
 2771         ifp = ifnet_byindex(ifindex);
 2772         if (ifp == NULL) {
 2773                 CTR2(KTR_MLD, "%s: no ifp for ifindex %u",
 2774                     __func__, ifindex);
 2775                 return (ENOENT);
 2776         }
 2777         /*
 2778          * Internal MLD lookups require that scope/zone ID is set.
 2779          */
 2780         (void)in6_setscope(&mcaddr, ifp, NULL);
 2781 
 2782         retval = sysctl_wire_old_buffer(req,
 2783             sizeof(uint32_t) + (in6_mcast_maxgrpsrc * sizeof(struct in6_addr)));
 2784         if (retval)
 2785                 return (retval);
 2786 
 2787         IN6_MULTI_LOCK();
 2788         IN6_MULTI_LIST_LOCK();
 2789         IF_ADDR_RLOCK(ifp);
 2790         CK_STAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
 2791                 inm = in6m_ifmultiaddr_get_inm(ifma);
 2792                 if (inm == NULL)
 2793                         continue;
 2794                 if (!IN6_ARE_ADDR_EQUAL(&inm->in6m_addr, &mcaddr))
 2795                         continue;
 2796                 fmode = inm->in6m_st[1].iss_fmode;
 2797                 retval = SYSCTL_OUT(req, &fmode, sizeof(uint32_t));
 2798                 if (retval != 0)
 2799                         break;
 2800                 RB_FOREACH(ims, ip6_msource_tree, &inm->in6m_srcs) {
 2801                         CTR2(KTR_MLD, "%s: visit node %p", __func__, ims);
 2802                         /*
 2803                          * Only copy-out sources which are in-mode.
 2804                          */
 2805                         if (fmode != im6s_get_mode(inm, ims, 1)) {
 2806                                 CTR1(KTR_MLD, "%s: skip non-in-mode",
 2807                                     __func__);
 2808                                 continue;
 2809                         }
 2810                         src = ims->im6s_addr;
 2811                         retval = SYSCTL_OUT(req, &src,
 2812                             sizeof(struct in6_addr));
 2813                         if (retval != 0)
 2814                                 break;
 2815                 }
 2816         }
 2817         IF_ADDR_RUNLOCK(ifp);
 2818 
 2819         IN6_MULTI_LIST_UNLOCK();
 2820         IN6_MULTI_UNLOCK();
 2821 
 2822         return (retval);
 2823 }
 2824 
 2825 #ifdef KTR
 2826 
 2827 static const char *in6m_modestrs[] = { "un", "in", "ex" };
 2828 
 2829 static const char *
 2830 in6m_mode_str(const int mode)
 2831 {
 2832 
 2833         if (mode >= MCAST_UNDEFINED && mode <= MCAST_EXCLUDE)
 2834                 return (in6m_modestrs[mode]);
 2835         return ("??");
 2836 }
 2837 
 2838 static const char *in6m_statestrs[] = {
 2839         "not-member",
 2840         "silent",
 2841         "idle",
 2842         "lazy",
 2843         "sleeping",
 2844         "awakening",
 2845         "query-pending",
 2846         "sg-query-pending",
 2847         "leaving"
 2848 };
 2849 
 2850 static const char *
 2851 in6m_state_str(const int state)
 2852 {
 2853 
 2854         if (state >= MLD_NOT_MEMBER && state <= MLD_LEAVING_MEMBER)
 2855                 return (in6m_statestrs[state]);
 2856         return ("??");
 2857 }
 2858 
 2859 /*
 2860  * Dump an in6_multi structure to the console.
 2861  */
 2862 void
 2863 in6m_print(const struct in6_multi *inm)
 2864 {
 2865         int t;
 2866         char ip6tbuf[INET6_ADDRSTRLEN];
 2867 
 2868         if ((ktr_mask & KTR_MLD) == 0)
 2869                 return;
 2870 
 2871         printf("%s: --- begin in6m %p ---\n", __func__, inm);
 2872         printf("addr %s ifp %p(%s) ifma %p\n",
 2873             ip6_sprintf(ip6tbuf, &inm->in6m_addr),
 2874             inm->in6m_ifp,
 2875             if_name(inm->in6m_ifp),
 2876             inm->in6m_ifma);
 2877         printf("timer %u state %s refcount %u scq.len %u\n",
 2878             inm->in6m_timer,
 2879             in6m_state_str(inm->in6m_state),
 2880             inm->in6m_refcount,
 2881             mbufq_len(&inm->in6m_scq));
 2882         printf("mli %p nsrc %lu sctimer %u scrv %u\n",
 2883             inm->in6m_mli,
 2884             inm->in6m_nsrc,
 2885             inm->in6m_sctimer,
 2886             inm->in6m_scrv);
 2887         for (t = 0; t < 2; t++) {
 2888                 printf("t%d: fmode %s asm %u ex %u in %u rec %u\n", t,
 2889                     in6m_mode_str(inm->in6m_st[t].iss_fmode),
 2890                     inm->in6m_st[t].iss_asm,
 2891                     inm->in6m_st[t].iss_ex,
 2892                     inm->in6m_st[t].iss_in,
 2893                     inm->in6m_st[t].iss_rec);
 2894         }
 2895         printf("%s: --- end in6m %p ---\n", __func__, inm);
 2896 }
 2897 
 2898 #else /* !KTR */
 2899 
 2900 void
 2901 in6m_print(const struct in6_multi *inm)
 2902 {
 2903 
 2904 }
 2905 
 2906 #endif /* KTR */

Cache object: afe08a594708f60d6e974834d1b17fc7


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]


This page is part of the FreeBSD/Linux Linux Kernel Cross-Reference, and was automatically generated using a modified version of the LXR engine.