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