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