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