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