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