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