xref: /freebsd/sys/netinet/igmp.c (revision 8d20be1e22095c27faf8fe8b2f0d089739cc742e)
1 /*-
2  * Copyright (c) 2007-2009 Bruce Simpson.
3  * Copyright (c) 1988 Stephen Deering.
4  * Copyright (c) 1992, 1993
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * This code is derived from software contributed to Berkeley by
8  * Stephen Deering of Stanford University.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 4. Neither the name of the University nor the names of its contributors
19  *    may be used to endorse or promote products derived from this software
20  *    without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  *
34  *	@(#)igmp.c	8.1 (Berkeley) 7/19/93
35  */
36 
37 /*
38  * Internet Group Management Protocol (IGMP) routines.
39  * [RFC1112, RFC2236, RFC3376]
40  *
41  * Written by Steve Deering, Stanford, May 1988.
42  * Modified by Rosen Sharma, Stanford, Aug 1994.
43  * Modified by Bill Fenner, Xerox PARC, Feb 1995.
44  * Modified to fully comply to IGMPv2 by Bill Fenner, Oct 1995.
45  * Significantly rewritten for IGMPv3, VIMAGE, and SMP by Bruce Simpson.
46  *
47  * MULTICAST Revision: 3.5.1.4
48  */
49 
50 #include <sys/cdefs.h>
51 __FBSDID("$FreeBSD$");
52 
53 #include <sys/param.h>
54 #include <sys/systm.h>
55 #include <sys/module.h>
56 #include <sys/malloc.h>
57 #include <sys/mbuf.h>
58 #include <sys/socket.h>
59 #include <sys/protosw.h>
60 #include <sys/kernel.h>
61 #include <sys/sysctl.h>
62 #include <sys/ktr.h>
63 #include <sys/condvar.h>
64 
65 #include <net/if.h>
66 #include <net/if_var.h>
67 #include <net/netisr.h>
68 #include <net/vnet.h>
69 
70 #include <netinet/in.h>
71 #include <netinet/in_var.h>
72 #include <netinet/in_systm.h>
73 #include <netinet/ip.h>
74 #include <netinet/ip_var.h>
75 #include <netinet/ip_options.h>
76 #include <netinet/igmp.h>
77 #include <netinet/igmp_var.h>
78 
79 #include <machine/in_cksum.h>
80 
81 #include <security/mac/mac_framework.h>
82 
83 #ifndef KTR_IGMPV3
84 #define KTR_IGMPV3 KTR_INET
85 #endif
86 
87 static struct igmp_ifinfo *
88 		igi_alloc_locked(struct ifnet *);
89 static void	igi_delete_locked(const struct ifnet *);
90 static void	igmp_dispatch_queue(struct ifqueue *, int, const int);
91 static void	igmp_fasttimo_vnet(void);
92 static void	igmp_final_leave(struct in_multi *, struct igmp_ifinfo *);
93 static int	igmp_handle_state_change(struct in_multi *,
94 		    struct igmp_ifinfo *);
95 static int	igmp_initial_join(struct in_multi *, struct igmp_ifinfo *);
96 static int	igmp_input_v1_query(struct ifnet *, const struct ip *,
97 		    const struct igmp *);
98 static int	igmp_input_v2_query(struct ifnet *, const struct ip *,
99 		    const struct igmp *);
100 static int	igmp_input_v3_query(struct ifnet *, const struct ip *,
101 		    /*const*/ struct igmpv3 *);
102 static int	igmp_input_v3_group_query(struct in_multi *,
103 		    struct igmp_ifinfo *, int, /*const*/ struct igmpv3 *);
104 static int	igmp_input_v1_report(struct ifnet *, /*const*/ struct ip *,
105 		    /*const*/ struct igmp *);
106 static int	igmp_input_v2_report(struct ifnet *, /*const*/ struct ip *,
107 		    /*const*/ struct igmp *);
108 static void	igmp_intr(struct mbuf *);
109 static int	igmp_isgroupreported(const struct in_addr);
110 static struct mbuf *
111 		igmp_ra_alloc(void);
112 #ifdef KTR
113 static char *	igmp_rec_type_to_str(const int);
114 #endif
115 static void	igmp_set_version(struct igmp_ifinfo *, const int);
116 static void	igmp_slowtimo_vnet(void);
117 static int	igmp_v1v2_queue_report(struct in_multi *, const int);
118 static void	igmp_v1v2_process_group_timer(struct in_multi *, const int);
119 static void	igmp_v1v2_process_querier_timers(struct igmp_ifinfo *);
120 static void	igmp_v2_update_group(struct in_multi *, const int);
121 static void	igmp_v3_cancel_link_timers(struct igmp_ifinfo *);
122 static void	igmp_v3_dispatch_general_query(struct igmp_ifinfo *);
123 static struct mbuf *
124 		igmp_v3_encap_report(struct ifnet *, struct mbuf *);
125 static int	igmp_v3_enqueue_group_record(struct ifqueue *,
126 		    struct in_multi *, const int, const int, const int);
127 static int	igmp_v3_enqueue_filter_change(struct ifqueue *,
128 		    struct in_multi *);
129 static void	igmp_v3_process_group_timers(struct igmp_ifinfo *,
130 		    struct ifqueue *, struct ifqueue *, struct in_multi *,
131 		    const int);
132 static int	igmp_v3_merge_state_changes(struct in_multi *,
133 		    struct ifqueue *);
134 static void	igmp_v3_suppress_group_record(struct in_multi *);
135 static int	sysctl_igmp_default_version(SYSCTL_HANDLER_ARGS);
136 static int	sysctl_igmp_gsr(SYSCTL_HANDLER_ARGS);
137 static int	sysctl_igmp_ifinfo(SYSCTL_HANDLER_ARGS);
138 
139 static const struct netisr_handler igmp_nh = {
140 	.nh_name = "igmp",
141 	.nh_handler = igmp_intr,
142 	.nh_proto = NETISR_IGMP,
143 	.nh_policy = NETISR_POLICY_SOURCE,
144 };
145 
146 /*
147  * System-wide globals.
148  *
149  * Unlocked access to these is OK, except for the global IGMP output
150  * queue. The IGMP subsystem lock ends up being system-wide for the moment,
151  * because all VIMAGEs have to share a global output queue, as netisrs
152  * themselves are not virtualized.
153  *
154  * Locking:
155  *  * The permitted lock order is: IN_MULTI_LOCK, IGMP_LOCK, IF_ADDR_LOCK.
156  *    Any may be taken independently; if any are held at the same
157  *    time, the above lock order must be followed.
158  *  * All output is delegated to the netisr.
159  *    Now that Giant has been eliminated, the netisr may be inlined.
160  *  * IN_MULTI_LOCK covers in_multi.
161  *  * IGMP_LOCK covers igmp_ifinfo and any global variables in this file,
162  *    including the output queue.
163  *  * IF_ADDR_LOCK covers if_multiaddrs, which is used for a variety of
164  *    per-link state iterators.
165  *  * igmp_ifinfo is valid as long as PF_INET is attached to the interface,
166  *    therefore it is not refcounted.
167  *    We allow unlocked reads of igmp_ifinfo when accessed via in_multi.
168  *
169  * Reference counting
170  *  * IGMP acquires its own reference every time an in_multi is passed to
171  *    it and the group is being joined for the first time.
172  *  * IGMP releases its reference(s) on in_multi in a deferred way,
173  *    because the operations which process the release run as part of
174  *    a loop whose control variables are directly affected by the release
175  *    (that, and not recursing on the IF_ADDR_LOCK).
176  *
177  * VIMAGE: Each in_multi corresponds to an ifp, and each ifp corresponds
178  * to a vnet in ifp->if_vnet.
179  *
180  * SMPng: XXX We may potentially race operations on ifma_protospec.
181  * The problem is that we currently lack a clean way of taking the
182  * IF_ADDR_LOCK() between the ifnet and in layers w/o recursing,
183  * as anything which modifies ifma needs to be covered by that lock.
184  * So check for ifma_protospec being NULL before proceeding.
185  */
186 struct mtx		 igmp_mtx;
187 
188 struct mbuf		*m_raopt;		 /* Router Alert option */
189 static MALLOC_DEFINE(M_IGMP, "igmp", "igmp state");
190 
191 /*
192  * VIMAGE-wide globals.
193  *
194  * The IGMPv3 timers themselves need to run per-image, however,
195  * protosw timers run globally (see tcp).
196  * An ifnet can only be in one vimage at a time, and the loopback
197  * ifnet, loif, is itself virtualized.
198  * It would otherwise be possible to seriously hose IGMP state,
199  * and create inconsistencies in upstream multicast routing, if you have
200  * multiple VIMAGEs running on the same link joining different multicast
201  * groups, UNLESS the "primary IP address" is different. This is because
202  * IGMP for IPv4 does not force link-local addresses to be used for each
203  * node, unlike MLD for IPv6.
204  * Obviously the IGMPv3 per-interface state has per-vimage granularity
205  * also as a result.
206  *
207  * FUTURE: Stop using IFP_TO_IA/INADDR_ANY, and use source address selection
208  * policy to control the address used by IGMP on the link.
209  */
210 static VNET_DEFINE(int, interface_timers_running);	/* IGMPv3 general
211 							 * query response */
212 static VNET_DEFINE(int, state_change_timers_running);	/* IGMPv3 state-change
213 							 * retransmit */
214 static VNET_DEFINE(int, current_state_timers_running);	/* IGMPv1/v2 host
215 							 * report; IGMPv3 g/sg
216 							 * query response */
217 
218 #define	V_interface_timers_running	VNET(interface_timers_running)
219 #define	V_state_change_timers_running	VNET(state_change_timers_running)
220 #define	V_current_state_timers_running	VNET(current_state_timers_running)
221 
222 static VNET_DEFINE(LIST_HEAD(, igmp_ifinfo), igi_head);
223 static VNET_DEFINE(struct igmpstat, igmpstat) = {
224 	.igps_version = IGPS_VERSION_3,
225 	.igps_len = sizeof(struct igmpstat),
226 };
227 static VNET_DEFINE(struct timeval, igmp_gsrdelay) = {10, 0};
228 
229 #define	V_igi_head			VNET(igi_head)
230 #define	V_igmpstat			VNET(igmpstat)
231 #define	V_igmp_gsrdelay			VNET(igmp_gsrdelay)
232 
233 static VNET_DEFINE(int, igmp_recvifkludge) = 1;
234 static VNET_DEFINE(int, igmp_sendra) = 1;
235 static VNET_DEFINE(int, igmp_sendlocal) = 1;
236 static VNET_DEFINE(int, igmp_v1enable) = 1;
237 static VNET_DEFINE(int, igmp_v2enable) = 1;
238 static VNET_DEFINE(int, igmp_legacysupp);
239 static VNET_DEFINE(int, igmp_default_version) = IGMP_VERSION_3;
240 
241 #define	V_igmp_recvifkludge		VNET(igmp_recvifkludge)
242 #define	V_igmp_sendra			VNET(igmp_sendra)
243 #define	V_igmp_sendlocal		VNET(igmp_sendlocal)
244 #define	V_igmp_v1enable			VNET(igmp_v1enable)
245 #define	V_igmp_v2enable			VNET(igmp_v2enable)
246 #define	V_igmp_legacysupp		VNET(igmp_legacysupp)
247 #define	V_igmp_default_version		VNET(igmp_default_version)
248 
249 /*
250  * Virtualized sysctls.
251  */
252 SYSCTL_VNET_STRUCT(_net_inet_igmp, IGMPCTL_STATS, stats, CTLFLAG_RW,
253     &VNET_NAME(igmpstat), igmpstat, "");
254 SYSCTL_VNET_INT(_net_inet_igmp, OID_AUTO, recvifkludge, CTLFLAG_RW,
255     &VNET_NAME(igmp_recvifkludge), 0,
256     "Rewrite IGMPv1/v2 reports from 0.0.0.0 to contain subnet address");
257 SYSCTL_VNET_INT(_net_inet_igmp, OID_AUTO, sendra, CTLFLAG_RW,
258     &VNET_NAME(igmp_sendra), 0,
259     "Send IP Router Alert option in IGMPv2/v3 messages");
260 SYSCTL_VNET_INT(_net_inet_igmp, OID_AUTO, sendlocal, CTLFLAG_RW,
261     &VNET_NAME(igmp_sendlocal), 0,
262     "Send IGMP membership reports for 224.0.0.0/24 groups");
263 SYSCTL_VNET_INT(_net_inet_igmp, OID_AUTO, v1enable, CTLFLAG_RW,
264     &VNET_NAME(igmp_v1enable), 0,
265     "Enable backwards compatibility with IGMPv1");
266 SYSCTL_VNET_INT(_net_inet_igmp, OID_AUTO, v2enable, CTLFLAG_RW,
267     &VNET_NAME(igmp_v2enable), 0,
268     "Enable backwards compatibility with IGMPv2");
269 SYSCTL_VNET_INT(_net_inet_igmp, OID_AUTO, legacysupp, CTLFLAG_RW,
270     &VNET_NAME(igmp_legacysupp), 0,
271     "Allow v1/v2 reports to suppress v3 group responses");
272 SYSCTL_VNET_PROC(_net_inet_igmp, OID_AUTO, default_version,
273     CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
274     &VNET_NAME(igmp_default_version), 0, sysctl_igmp_default_version, "I",
275     "Default version of IGMP to run on each interface");
276 SYSCTL_VNET_PROC(_net_inet_igmp, OID_AUTO, gsrdelay,
277     CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
278     &VNET_NAME(igmp_gsrdelay.tv_sec), 0, sysctl_igmp_gsr, "I",
279     "Rate limit for IGMPv3 Group-and-Source queries in seconds");
280 
281 /*
282  * Non-virtualized sysctls.
283  */
284 static SYSCTL_NODE(_net_inet_igmp, OID_AUTO, ifinfo,
285     CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_igmp_ifinfo,
286     "Per-interface IGMPv3 state");
287 
288 static __inline void
289 igmp_save_context(struct mbuf *m, struct ifnet *ifp)
290 {
291 
292 #ifdef VIMAGE
293 	m->m_pkthdr.PH_loc.ptr = ifp->if_vnet;
294 #endif /* VIMAGE */
295 	m->m_pkthdr.flowid = ifp->if_index;
296 }
297 
298 static __inline void
299 igmp_scrub_context(struct mbuf *m)
300 {
301 
302 	m->m_pkthdr.PH_loc.ptr = NULL;
303 	m->m_pkthdr.flowid = 0;
304 }
305 
306 #ifdef KTR
307 static __inline char *
308 inet_ntoa_haddr(in_addr_t haddr)
309 {
310 	struct in_addr ia;
311 
312 	ia.s_addr = htonl(haddr);
313 	return (inet_ntoa(ia));
314 }
315 #endif
316 
317 /*
318  * Restore context from a queued IGMP output chain.
319  * Return saved ifindex.
320  *
321  * VIMAGE: The assertion is there to make sure that we
322  * actually called CURVNET_SET() with what's in the mbuf chain.
323  */
324 static __inline uint32_t
325 igmp_restore_context(struct mbuf *m)
326 {
327 
328 #ifdef notyet
329 #if defined(VIMAGE) && defined(INVARIANTS)
330 	KASSERT(curvnet == (m->m_pkthdr.PH_loc.ptr),
331 	    ("%s: called when curvnet was not restored", __func__));
332 #endif
333 #endif
334 	return (m->m_pkthdr.flowid);
335 }
336 
337 /*
338  * Retrieve or set default IGMP version.
339  *
340  * VIMAGE: Assume curvnet set by caller.
341  * SMPng: NOTE: Serialized by IGMP lock.
342  */
343 static int
344 sysctl_igmp_default_version(SYSCTL_HANDLER_ARGS)
345 {
346 	int	 error;
347 	int	 new;
348 
349 	error = sysctl_wire_old_buffer(req, sizeof(int));
350 	if (error)
351 		return (error);
352 
353 	IGMP_LOCK();
354 
355 	new = V_igmp_default_version;
356 
357 	error = sysctl_handle_int(oidp, &new, 0, req);
358 	if (error || !req->newptr)
359 		goto out_locked;
360 
361 	if (new < IGMP_VERSION_1 || new > IGMP_VERSION_3) {
362 		error = EINVAL;
363 		goto out_locked;
364 	}
365 
366 	CTR2(KTR_IGMPV3, "change igmp_default_version from %d to %d",
367 	     V_igmp_default_version, new);
368 
369 	V_igmp_default_version = new;
370 
371 out_locked:
372 	IGMP_UNLOCK();
373 	return (error);
374 }
375 
376 /*
377  * Retrieve or set threshold between group-source queries in seconds.
378  *
379  * VIMAGE: Assume curvnet set by caller.
380  * SMPng: NOTE: Serialized by IGMP lock.
381  */
382 static int
383 sysctl_igmp_gsr(SYSCTL_HANDLER_ARGS)
384 {
385 	int error;
386 	int i;
387 
388 	error = sysctl_wire_old_buffer(req, sizeof(int));
389 	if (error)
390 		return (error);
391 
392 	IGMP_LOCK();
393 
394 	i = V_igmp_gsrdelay.tv_sec;
395 
396 	error = sysctl_handle_int(oidp, &i, 0, req);
397 	if (error || !req->newptr)
398 		goto out_locked;
399 
400 	if (i < -1 || i >= 60) {
401 		error = EINVAL;
402 		goto out_locked;
403 	}
404 
405 	CTR2(KTR_IGMPV3, "change igmp_gsrdelay from %d to %d",
406 	     V_igmp_gsrdelay.tv_sec, i);
407 	V_igmp_gsrdelay.tv_sec = i;
408 
409 out_locked:
410 	IGMP_UNLOCK();
411 	return (error);
412 }
413 
414 /*
415  * Expose struct igmp_ifinfo to userland, keyed by ifindex.
416  * For use by ifmcstat(8).
417  *
418  * SMPng: NOTE: Does an unlocked ifindex space read.
419  * VIMAGE: Assume curvnet set by caller. The node handler itself
420  * is not directly virtualized.
421  */
422 static int
423 sysctl_igmp_ifinfo(SYSCTL_HANDLER_ARGS)
424 {
425 	int			*name;
426 	int			 error;
427 	u_int			 namelen;
428 	struct ifnet		*ifp;
429 	struct igmp_ifinfo	*igi;
430 
431 	name = (int *)arg1;
432 	namelen = arg2;
433 
434 	if (req->newptr != NULL)
435 		return (EPERM);
436 
437 	if (namelen != 1)
438 		return (EINVAL);
439 
440 	error = sysctl_wire_old_buffer(req, sizeof(struct igmp_ifinfo));
441 	if (error)
442 		return (error);
443 
444 	IN_MULTI_LOCK();
445 	IGMP_LOCK();
446 
447 	if (name[0] <= 0 || name[0] > V_if_index) {
448 		error = ENOENT;
449 		goto out_locked;
450 	}
451 
452 	error = ENOENT;
453 
454 	ifp = ifnet_byindex(name[0]);
455 	if (ifp == NULL)
456 		goto out_locked;
457 
458 	LIST_FOREACH(igi, &V_igi_head, igi_link) {
459 		if (ifp == igi->igi_ifp) {
460 			error = SYSCTL_OUT(req, igi,
461 			    sizeof(struct igmp_ifinfo));
462 			break;
463 		}
464 	}
465 
466 out_locked:
467 	IGMP_UNLOCK();
468 	IN_MULTI_UNLOCK();
469 	return (error);
470 }
471 
472 /*
473  * Dispatch an entire queue of pending packet chains
474  * using the netisr.
475  * VIMAGE: Assumes the vnet pointer has been set.
476  */
477 static void
478 igmp_dispatch_queue(struct ifqueue *ifq, int limit, const int loop)
479 {
480 	struct mbuf *m;
481 
482 	for (;;) {
483 		_IF_DEQUEUE(ifq, m);
484 		if (m == NULL)
485 			break;
486 		CTR3(KTR_IGMPV3, "%s: dispatch %p from %p", __func__, ifq, m);
487 		if (loop)
488 			m->m_flags |= M_IGMP_LOOP;
489 		netisr_dispatch(NETISR_IGMP, m);
490 		if (--limit == 0)
491 			break;
492 	}
493 }
494 
495 /*
496  * Filter outgoing IGMP report state by group.
497  *
498  * Reports are ALWAYS suppressed for ALL-HOSTS (224.0.0.1).
499  * If the net.inet.igmp.sendlocal sysctl is 0, then IGMP reports are
500  * disabled for all groups in the 224.0.0.0/24 link-local scope. However,
501  * this may break certain IGMP snooping switches which rely on the old
502  * report behaviour.
503  *
504  * Return zero if the given group is one for which IGMP reports
505  * should be suppressed, or non-zero if reports should be issued.
506  */
507 static __inline int
508 igmp_isgroupreported(const struct in_addr addr)
509 {
510 
511 	if (in_allhosts(addr) ||
512 	    ((!V_igmp_sendlocal && IN_LOCAL_GROUP(ntohl(addr.s_addr)))))
513 		return (0);
514 
515 	return (1);
516 }
517 
518 /*
519  * Construct a Router Alert option to use in outgoing packets.
520  */
521 static struct mbuf *
522 igmp_ra_alloc(void)
523 {
524 	struct mbuf	*m;
525 	struct ipoption	*p;
526 
527 	m = m_get(M_WAITOK, MT_DATA);
528 	p = mtod(m, struct ipoption *);
529 	p->ipopt_dst.s_addr = INADDR_ANY;
530 	p->ipopt_list[0] = IPOPT_RA;	/* Router Alert Option */
531 	p->ipopt_list[1] = 0x04;	/* 4 bytes long */
532 	p->ipopt_list[2] = IPOPT_EOL;	/* End of IP option list */
533 	p->ipopt_list[3] = 0x00;	/* pad byte */
534 	m->m_len = sizeof(p->ipopt_dst) + p->ipopt_list[1];
535 
536 	return (m);
537 }
538 
539 /*
540  * Attach IGMP when PF_INET is attached to an interface.
541  */
542 struct igmp_ifinfo *
543 igmp_domifattach(struct ifnet *ifp)
544 {
545 	struct igmp_ifinfo *igi;
546 
547 	CTR3(KTR_IGMPV3, "%s: called for ifp %p(%s)",
548 	    __func__, ifp, ifp->if_xname);
549 
550 	IGMP_LOCK();
551 
552 	igi = igi_alloc_locked(ifp);
553 	if (!(ifp->if_flags & IFF_MULTICAST))
554 		igi->igi_flags |= IGIF_SILENT;
555 
556 	IGMP_UNLOCK();
557 
558 	return (igi);
559 }
560 
561 /*
562  * VIMAGE: assume curvnet set by caller.
563  */
564 static struct igmp_ifinfo *
565 igi_alloc_locked(/*const*/ struct ifnet *ifp)
566 {
567 	struct igmp_ifinfo *igi;
568 
569 	IGMP_LOCK_ASSERT();
570 
571 	igi = malloc(sizeof(struct igmp_ifinfo), M_IGMP, M_NOWAIT|M_ZERO);
572 	if (igi == NULL)
573 		goto out;
574 
575 	igi->igi_ifp = ifp;
576 	igi->igi_version = V_igmp_default_version;
577 	igi->igi_flags = 0;
578 	igi->igi_rv = IGMP_RV_INIT;
579 	igi->igi_qi = IGMP_QI_INIT;
580 	igi->igi_qri = IGMP_QRI_INIT;
581 	igi->igi_uri = IGMP_URI_INIT;
582 
583 	SLIST_INIT(&igi->igi_relinmhead);
584 
585 	/*
586 	 * Responses to general queries are subject to bounds.
587 	 */
588 	IFQ_SET_MAXLEN(&igi->igi_gq, IGMP_MAX_RESPONSE_PACKETS);
589 
590 	LIST_INSERT_HEAD(&V_igi_head, igi, igi_link);
591 
592 	CTR2(KTR_IGMPV3, "allocate igmp_ifinfo for ifp %p(%s)",
593 	     ifp, ifp->if_xname);
594 
595 out:
596 	return (igi);
597 }
598 
599 /*
600  * Hook for ifdetach.
601  *
602  * NOTE: Some finalization tasks need to run before the protocol domain
603  * is detached, but also before the link layer does its cleanup.
604  *
605  * SMPNG: igmp_ifdetach() needs to take IF_ADDR_LOCK().
606  * XXX This is also bitten by unlocked ifma_protospec access.
607  */
608 void
609 igmp_ifdetach(struct ifnet *ifp)
610 {
611 	struct igmp_ifinfo	*igi;
612 	struct ifmultiaddr	*ifma;
613 	struct in_multi		*inm, *tinm;
614 
615 	CTR3(KTR_IGMPV3, "%s: called for ifp %p(%s)", __func__, ifp,
616 	    ifp->if_xname);
617 
618 	IGMP_LOCK();
619 
620 	igi = ((struct in_ifinfo *)ifp->if_afdata[AF_INET])->ii_igmp;
621 	if (igi->igi_version == IGMP_VERSION_3) {
622 		IF_ADDR_RLOCK(ifp);
623 		TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
624 			if (ifma->ifma_addr->sa_family != AF_INET ||
625 			    ifma->ifma_protospec == NULL)
626 				continue;
627 #if 0
628 			KASSERT(ifma->ifma_protospec != NULL,
629 			    ("%s: ifma_protospec is NULL", __func__));
630 #endif
631 			inm = (struct in_multi *)ifma->ifma_protospec;
632 			if (inm->inm_state == IGMP_LEAVING_MEMBER) {
633 				SLIST_INSERT_HEAD(&igi->igi_relinmhead,
634 				    inm, inm_nrele);
635 			}
636 			inm_clear_recorded(inm);
637 		}
638 		IF_ADDR_RUNLOCK(ifp);
639 		/*
640 		 * Free the in_multi reference(s) for this IGMP lifecycle.
641 		 */
642 		SLIST_FOREACH_SAFE(inm, &igi->igi_relinmhead, inm_nrele,
643 		    tinm) {
644 			SLIST_REMOVE_HEAD(&igi->igi_relinmhead, inm_nrele);
645 			inm_release_locked(inm);
646 		}
647 	}
648 
649 	IGMP_UNLOCK();
650 }
651 
652 /*
653  * Hook for domifdetach.
654  */
655 void
656 igmp_domifdetach(struct ifnet *ifp)
657 {
658 	struct igmp_ifinfo *igi;
659 
660 	CTR3(KTR_IGMPV3, "%s: called for ifp %p(%s)",
661 	    __func__, ifp, ifp->if_xname);
662 
663 	IGMP_LOCK();
664 
665 	igi = ((struct in_ifinfo *)ifp->if_afdata[AF_INET])->ii_igmp;
666 	igi_delete_locked(ifp);
667 
668 	IGMP_UNLOCK();
669 }
670 
671 static void
672 igi_delete_locked(const struct ifnet *ifp)
673 {
674 	struct igmp_ifinfo *igi, *tigi;
675 
676 	CTR3(KTR_IGMPV3, "%s: freeing igmp_ifinfo for ifp %p(%s)",
677 	    __func__, ifp, ifp->if_xname);
678 
679 	IGMP_LOCK_ASSERT();
680 
681 	LIST_FOREACH_SAFE(igi, &V_igi_head, igi_link, tigi) {
682 		if (igi->igi_ifp == ifp) {
683 			/*
684 			 * Free deferred General Query responses.
685 			 */
686 			_IF_DRAIN(&igi->igi_gq);
687 
688 			LIST_REMOVE(igi, igi_link);
689 
690 			KASSERT(SLIST_EMPTY(&igi->igi_relinmhead),
691 			    ("%s: there are dangling in_multi references",
692 			    __func__));
693 
694 			free(igi, M_IGMP);
695 			return;
696 		}
697 	}
698 
699 #ifdef INVARIANTS
700 	panic("%s: igmp_ifinfo not found for ifp %p\n", __func__,  ifp);
701 #endif
702 }
703 
704 /*
705  * Process a received IGMPv1 query.
706  * Return non-zero if the message should be dropped.
707  *
708  * VIMAGE: The curvnet pointer is derived from the input ifp.
709  */
710 static int
711 igmp_input_v1_query(struct ifnet *ifp, const struct ip *ip,
712     const struct igmp *igmp)
713 {
714 	struct ifmultiaddr	*ifma;
715 	struct igmp_ifinfo	*igi;
716 	struct in_multi		*inm;
717 
718 	/*
719 	 * IGMPv1 Host Mmembership Queries SHOULD always be addressed to
720 	 * 224.0.0.1. They are always treated as General Queries.
721 	 * igmp_group is always ignored. Do not drop it as a userland
722 	 * daemon may wish to see it.
723 	 * XXX SMPng: unlocked increments in igmpstat assumed atomic.
724 	 */
725 	if (!in_allhosts(ip->ip_dst) || !in_nullhost(igmp->igmp_group)) {
726 		IGMPSTAT_INC(igps_rcv_badqueries);
727 		return (0);
728 	}
729 	IGMPSTAT_INC(igps_rcv_gen_queries);
730 
731 	IN_MULTI_LOCK();
732 	IGMP_LOCK();
733 
734 	igi = ((struct in_ifinfo *)ifp->if_afdata[AF_INET])->ii_igmp;
735 	KASSERT(igi != NULL, ("%s: no igmp_ifinfo for ifp %p", __func__, ifp));
736 
737 	if (igi->igi_flags & IGIF_LOOPBACK) {
738 		CTR2(KTR_IGMPV3, "ignore v1 query on IGIF_LOOPBACK ifp %p(%s)",
739 		    ifp, ifp->if_xname);
740 		goto out_locked;
741 	}
742 
743 	/*
744 	 * Switch to IGMPv1 host compatibility mode.
745 	 */
746 	igmp_set_version(igi, IGMP_VERSION_1);
747 
748 	CTR2(KTR_IGMPV3, "process v1 query on ifp %p(%s)", ifp, ifp->if_xname);
749 
750 	/*
751 	 * Start the timers in all of our group records
752 	 * for the interface on which the query arrived,
753 	 * except those which are already running.
754 	 */
755 	IF_ADDR_RLOCK(ifp);
756 	TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
757 		if (ifma->ifma_addr->sa_family != AF_INET ||
758 		    ifma->ifma_protospec == NULL)
759 			continue;
760 		inm = (struct in_multi *)ifma->ifma_protospec;
761 		if (inm->inm_timer != 0)
762 			continue;
763 		switch (inm->inm_state) {
764 		case IGMP_NOT_MEMBER:
765 		case IGMP_SILENT_MEMBER:
766 			break;
767 		case IGMP_G_QUERY_PENDING_MEMBER:
768 		case IGMP_SG_QUERY_PENDING_MEMBER:
769 		case IGMP_REPORTING_MEMBER:
770 		case IGMP_IDLE_MEMBER:
771 		case IGMP_LAZY_MEMBER:
772 		case IGMP_SLEEPING_MEMBER:
773 		case IGMP_AWAKENING_MEMBER:
774 			inm->inm_state = IGMP_REPORTING_MEMBER;
775 			inm->inm_timer = IGMP_RANDOM_DELAY(
776 			    IGMP_V1V2_MAX_RI * PR_FASTHZ);
777 			V_current_state_timers_running = 1;
778 			break;
779 		case IGMP_LEAVING_MEMBER:
780 			break;
781 		}
782 	}
783 	IF_ADDR_RUNLOCK(ifp);
784 
785 out_locked:
786 	IGMP_UNLOCK();
787 	IN_MULTI_UNLOCK();
788 
789 	return (0);
790 }
791 
792 /*
793  * Process a received IGMPv2 general or group-specific query.
794  */
795 static int
796 igmp_input_v2_query(struct ifnet *ifp, const struct ip *ip,
797     const struct igmp *igmp)
798 {
799 	struct ifmultiaddr	*ifma;
800 	struct igmp_ifinfo	*igi;
801 	struct in_multi		*inm;
802 	int			 is_general_query;
803 	uint16_t		 timer;
804 
805 	is_general_query = 0;
806 
807 	/*
808 	 * Validate address fields upfront.
809 	 * XXX SMPng: unlocked increments in igmpstat assumed atomic.
810 	 */
811 	if (in_nullhost(igmp->igmp_group)) {
812 		/*
813 		 * IGMPv2 General Query.
814 		 * If this was not sent to the all-hosts group, ignore it.
815 		 */
816 		if (!in_allhosts(ip->ip_dst))
817 			return (0);
818 		IGMPSTAT_INC(igps_rcv_gen_queries);
819 		is_general_query = 1;
820 	} else {
821 		/* IGMPv2 Group-Specific Query. */
822 		IGMPSTAT_INC(igps_rcv_group_queries);
823 	}
824 
825 	IN_MULTI_LOCK();
826 	IGMP_LOCK();
827 
828 	igi = ((struct in_ifinfo *)ifp->if_afdata[AF_INET])->ii_igmp;
829 	KASSERT(igi != NULL, ("%s: no igmp_ifinfo for ifp %p", __func__, ifp));
830 
831 	if (igi->igi_flags & IGIF_LOOPBACK) {
832 		CTR2(KTR_IGMPV3, "ignore v2 query on IGIF_LOOPBACK ifp %p(%s)",
833 		    ifp, ifp->if_xname);
834 		goto out_locked;
835 	}
836 
837 	/*
838 	 * Ignore v2 query if in v1 Compatibility Mode.
839 	 */
840 	if (igi->igi_version == IGMP_VERSION_1)
841 		goto out_locked;
842 
843 	igmp_set_version(igi, IGMP_VERSION_2);
844 
845 	timer = igmp->igmp_code * PR_FASTHZ / IGMP_TIMER_SCALE;
846 	if (timer == 0)
847 		timer = 1;
848 
849 	if (is_general_query) {
850 		/*
851 		 * For each reporting group joined on this
852 		 * interface, kick the report timer.
853 		 */
854 		CTR2(KTR_IGMPV3, "process v2 general query on ifp %p(%s)",
855 		    ifp, ifp->if_xname);
856 		IF_ADDR_RLOCK(ifp);
857 		TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
858 			if (ifma->ifma_addr->sa_family != AF_INET ||
859 			    ifma->ifma_protospec == NULL)
860 				continue;
861 			inm = (struct in_multi *)ifma->ifma_protospec;
862 			igmp_v2_update_group(inm, timer);
863 		}
864 		IF_ADDR_RUNLOCK(ifp);
865 	} else {
866 		/*
867 		 * Group-specific IGMPv2 query, we need only
868 		 * look up the single group to process it.
869 		 */
870 		inm = inm_lookup(ifp, igmp->igmp_group);
871 		if (inm != NULL) {
872 			CTR3(KTR_IGMPV3, "process v2 query %s on ifp %p(%s)",
873 			    inet_ntoa(igmp->igmp_group), ifp, ifp->if_xname);
874 			igmp_v2_update_group(inm, timer);
875 		}
876 	}
877 
878 out_locked:
879 	IGMP_UNLOCK();
880 	IN_MULTI_UNLOCK();
881 
882 	return (0);
883 }
884 
885 /*
886  * Update the report timer on a group in response to an IGMPv2 query.
887  *
888  * If we are becoming the reporting member for this group, start the timer.
889  * If we already are the reporting member for this group, and timer is
890  * below the threshold, reset it.
891  *
892  * We may be updating the group for the first time since we switched
893  * to IGMPv3. If we are, then we must clear any recorded source lists,
894  * and transition to REPORTING state; the group timer is overloaded
895  * for group and group-source query responses.
896  *
897  * Unlike IGMPv3, the delay per group should be jittered
898  * to avoid bursts of IGMPv2 reports.
899  */
900 static void
901 igmp_v2_update_group(struct in_multi *inm, const int timer)
902 {
903 
904 	CTR4(KTR_IGMPV3, "%s: %s/%s timer=%d", __func__,
905 	    inet_ntoa(inm->inm_addr), inm->inm_ifp->if_xname, timer);
906 
907 	IN_MULTI_LOCK_ASSERT();
908 
909 	switch (inm->inm_state) {
910 	case IGMP_NOT_MEMBER:
911 	case IGMP_SILENT_MEMBER:
912 		break;
913 	case IGMP_REPORTING_MEMBER:
914 		if (inm->inm_timer != 0 &&
915 		    inm->inm_timer <= timer) {
916 			CTR1(KTR_IGMPV3, "%s: REPORTING and timer running, "
917 			    "skipping.", __func__);
918 			break;
919 		}
920 		/* FALLTHROUGH */
921 	case IGMP_SG_QUERY_PENDING_MEMBER:
922 	case IGMP_G_QUERY_PENDING_MEMBER:
923 	case IGMP_IDLE_MEMBER:
924 	case IGMP_LAZY_MEMBER:
925 	case IGMP_AWAKENING_MEMBER:
926 		CTR1(KTR_IGMPV3, "%s: ->REPORTING", __func__);
927 		inm->inm_state = IGMP_REPORTING_MEMBER;
928 		inm->inm_timer = IGMP_RANDOM_DELAY(timer);
929 		V_current_state_timers_running = 1;
930 		break;
931 	case IGMP_SLEEPING_MEMBER:
932 		CTR1(KTR_IGMPV3, "%s: ->AWAKENING", __func__);
933 		inm->inm_state = IGMP_AWAKENING_MEMBER;
934 		break;
935 	case IGMP_LEAVING_MEMBER:
936 		break;
937 	}
938 }
939 
940 /*
941  * Process a received IGMPv3 general, group-specific or
942  * group-and-source-specific query.
943  * Assumes m has already been pulled up to the full IGMP message length.
944  * Return 0 if successful, otherwise an appropriate error code is returned.
945  */
946 static int
947 igmp_input_v3_query(struct ifnet *ifp, const struct ip *ip,
948     /*const*/ struct igmpv3 *igmpv3)
949 {
950 	struct igmp_ifinfo	*igi;
951 	struct in_multi		*inm;
952 	int			 is_general_query;
953 	uint32_t		 maxresp, nsrc, qqi;
954 	uint16_t		 timer;
955 	uint8_t			 qrv;
956 
957 	is_general_query = 0;
958 
959 	CTR2(KTR_IGMPV3, "process v3 query on ifp %p(%s)", ifp, ifp->if_xname);
960 
961 	maxresp = igmpv3->igmp_code;	/* in 1/10ths of a second */
962 	if (maxresp >= 128) {
963 		maxresp = IGMP_MANT(igmpv3->igmp_code) <<
964 			  (IGMP_EXP(igmpv3->igmp_code) + 3);
965 	}
966 
967 	/*
968 	 * Robustness must never be less than 2 for on-wire IGMPv3.
969 	 * FUTURE: Check if ifp has IGIF_LOOPBACK set, as we will make
970 	 * an exception for interfaces whose IGMPv3 state changes
971 	 * are redirected to loopback (e.g. MANET).
972 	 */
973 	qrv = IGMP_QRV(igmpv3->igmp_misc);
974 	if (qrv < 2) {
975 		CTR3(KTR_IGMPV3, "%s: clamping qrv %d to %d", __func__,
976 		    qrv, IGMP_RV_INIT);
977 		qrv = IGMP_RV_INIT;
978 	}
979 
980 	qqi = igmpv3->igmp_qqi;
981 	if (qqi >= 128) {
982 		qqi = IGMP_MANT(igmpv3->igmp_qqi) <<
983 		     (IGMP_EXP(igmpv3->igmp_qqi) + 3);
984 	}
985 
986 	timer = maxresp * PR_FASTHZ / IGMP_TIMER_SCALE;
987 	if (timer == 0)
988 		timer = 1;
989 
990 	nsrc = ntohs(igmpv3->igmp_numsrc);
991 
992 	/*
993 	 * Validate address fields and versions upfront before
994 	 * accepting v3 query.
995 	 * XXX SMPng: Unlocked access to igmpstat counters here.
996 	 */
997 	if (in_nullhost(igmpv3->igmp_group)) {
998 		/*
999 		 * IGMPv3 General Query.
1000 		 *
1001 		 * General Queries SHOULD be directed to 224.0.0.1.
1002 		 * A general query with a source list has undefined
1003 		 * behaviour; discard it.
1004 		 */
1005 		IGMPSTAT_INC(igps_rcv_gen_queries);
1006 		if (!in_allhosts(ip->ip_dst) || nsrc > 0) {
1007 			IGMPSTAT_INC(igps_rcv_badqueries);
1008 			return (0);
1009 		}
1010 		is_general_query = 1;
1011 	} else {
1012 		/* Group or group-source specific query. */
1013 		if (nsrc == 0)
1014 			IGMPSTAT_INC(igps_rcv_group_queries);
1015 		else
1016 			IGMPSTAT_INC(igps_rcv_gsr_queries);
1017 	}
1018 
1019 	IN_MULTI_LOCK();
1020 	IGMP_LOCK();
1021 
1022 	igi = ((struct in_ifinfo *)ifp->if_afdata[AF_INET])->ii_igmp;
1023 	KASSERT(igi != NULL, ("%s: no igmp_ifinfo for ifp %p", __func__, ifp));
1024 
1025 	if (igi->igi_flags & IGIF_LOOPBACK) {
1026 		CTR2(KTR_IGMPV3, "ignore v3 query on IGIF_LOOPBACK ifp %p(%s)",
1027 		    ifp, ifp->if_xname);
1028 		goto out_locked;
1029 	}
1030 
1031 	/*
1032 	 * Discard the v3 query if we're in Compatibility Mode.
1033 	 * The RFC is not obviously worded that hosts need to stay in
1034 	 * compatibility mode until the Old Version Querier Present
1035 	 * timer expires.
1036 	 */
1037 	if (igi->igi_version != IGMP_VERSION_3) {
1038 		CTR3(KTR_IGMPV3, "ignore v3 query in v%d mode on ifp %p(%s)",
1039 		    igi->igi_version, ifp, ifp->if_xname);
1040 		goto out_locked;
1041 	}
1042 
1043 	igmp_set_version(igi, IGMP_VERSION_3);
1044 	igi->igi_rv = qrv;
1045 	igi->igi_qi = qqi;
1046 	igi->igi_qri = maxresp;
1047 
1048 	CTR4(KTR_IGMPV3, "%s: qrv %d qi %d qri %d", __func__, qrv, qqi,
1049 	    maxresp);
1050 
1051 	if (is_general_query) {
1052 		/*
1053 		 * Schedule a current-state report on this ifp for
1054 		 * all groups, possibly containing source lists.
1055 		 * If there is a pending General Query response
1056 		 * scheduled earlier than the selected delay, do
1057 		 * not schedule any other reports.
1058 		 * Otherwise, reset the interface timer.
1059 		 */
1060 		CTR2(KTR_IGMPV3, "process v3 general query on ifp %p(%s)",
1061 		    ifp, ifp->if_xname);
1062 		if (igi->igi_v3_timer == 0 || igi->igi_v3_timer >= timer) {
1063 			igi->igi_v3_timer = IGMP_RANDOM_DELAY(timer);
1064 			V_interface_timers_running = 1;
1065 		}
1066 	} else {
1067 		/*
1068 		 * Group-source-specific queries are throttled on
1069 		 * a per-group basis to defeat denial-of-service attempts.
1070 		 * Queries for groups we are not a member of on this
1071 		 * link are simply ignored.
1072 		 */
1073 		inm = inm_lookup(ifp, igmpv3->igmp_group);
1074 		if (inm == NULL)
1075 			goto out_locked;
1076 		if (nsrc > 0) {
1077 			if (!ratecheck(&inm->inm_lastgsrtv,
1078 			    &V_igmp_gsrdelay)) {
1079 				CTR1(KTR_IGMPV3, "%s: GS query throttled.",
1080 				    __func__);
1081 				IGMPSTAT_INC(igps_drop_gsr_queries);
1082 				goto out_locked;
1083 			}
1084 		}
1085 		CTR3(KTR_IGMPV3, "process v3 %s query on ifp %p(%s)",
1086 		     inet_ntoa(igmpv3->igmp_group), ifp, ifp->if_xname);
1087 		/*
1088 		 * If there is a pending General Query response
1089 		 * scheduled sooner than the selected delay, no
1090 		 * further report need be scheduled.
1091 		 * Otherwise, prepare to respond to the
1092 		 * group-specific or group-and-source query.
1093 		 */
1094 		if (igi->igi_v3_timer == 0 || igi->igi_v3_timer >= timer)
1095 			igmp_input_v3_group_query(inm, igi, timer, igmpv3);
1096 	}
1097 
1098 out_locked:
1099 	IGMP_UNLOCK();
1100 	IN_MULTI_UNLOCK();
1101 
1102 	return (0);
1103 }
1104 
1105 /*
1106  * Process a recieved IGMPv3 group-specific or group-and-source-specific
1107  * query.
1108  * Return <0 if any error occured. Currently this is ignored.
1109  */
1110 static int
1111 igmp_input_v3_group_query(struct in_multi *inm, struct igmp_ifinfo *igi,
1112     int timer, /*const*/ struct igmpv3 *igmpv3)
1113 {
1114 	int			 retval;
1115 	uint16_t		 nsrc;
1116 
1117 	IN_MULTI_LOCK_ASSERT();
1118 	IGMP_LOCK_ASSERT();
1119 
1120 	retval = 0;
1121 
1122 	switch (inm->inm_state) {
1123 	case IGMP_NOT_MEMBER:
1124 	case IGMP_SILENT_MEMBER:
1125 	case IGMP_SLEEPING_MEMBER:
1126 	case IGMP_LAZY_MEMBER:
1127 	case IGMP_AWAKENING_MEMBER:
1128 	case IGMP_IDLE_MEMBER:
1129 	case IGMP_LEAVING_MEMBER:
1130 		return (retval);
1131 		break;
1132 	case IGMP_REPORTING_MEMBER:
1133 	case IGMP_G_QUERY_PENDING_MEMBER:
1134 	case IGMP_SG_QUERY_PENDING_MEMBER:
1135 		break;
1136 	}
1137 
1138 	nsrc = ntohs(igmpv3->igmp_numsrc);
1139 
1140 	/*
1141 	 * Deal with group-specific queries upfront.
1142 	 * If any group query is already pending, purge any recorded
1143 	 * source-list state if it exists, and schedule a query response
1144 	 * for this group-specific query.
1145 	 */
1146 	if (nsrc == 0) {
1147 		if (inm->inm_state == IGMP_G_QUERY_PENDING_MEMBER ||
1148 		    inm->inm_state == IGMP_SG_QUERY_PENDING_MEMBER) {
1149 			inm_clear_recorded(inm);
1150 			timer = min(inm->inm_timer, timer);
1151 		}
1152 		inm->inm_state = IGMP_G_QUERY_PENDING_MEMBER;
1153 		inm->inm_timer = IGMP_RANDOM_DELAY(timer);
1154 		V_current_state_timers_running = 1;
1155 		return (retval);
1156 	}
1157 
1158 	/*
1159 	 * Deal with the case where a group-and-source-specific query has
1160 	 * been received but a group-specific query is already pending.
1161 	 */
1162 	if (inm->inm_state == IGMP_G_QUERY_PENDING_MEMBER) {
1163 		timer = min(inm->inm_timer, timer);
1164 		inm->inm_timer = IGMP_RANDOM_DELAY(timer);
1165 		V_current_state_timers_running = 1;
1166 		return (retval);
1167 	}
1168 
1169 	/*
1170 	 * Finally, deal with the case where a group-and-source-specific
1171 	 * query has been received, where a response to a previous g-s-r
1172 	 * query exists, or none exists.
1173 	 * In this case, we need to parse the source-list which the Querier
1174 	 * has provided us with and check if we have any source list filter
1175 	 * entries at T1 for these sources. If we do not, there is no need
1176 	 * schedule a report and the query may be dropped.
1177 	 * If we do, we must record them and schedule a current-state
1178 	 * report for those sources.
1179 	 * FIXME: Handling source lists larger than 1 mbuf requires that
1180 	 * we pass the mbuf chain pointer down to this function, and use
1181 	 * m_getptr() to walk the chain.
1182 	 */
1183 	if (inm->inm_nsrc > 0) {
1184 		const struct in_addr	*ap;
1185 		int			 i, nrecorded;
1186 
1187 		ap = (const struct in_addr *)(igmpv3 + 1);
1188 		nrecorded = 0;
1189 		for (i = 0; i < nsrc; i++, ap++) {
1190 			retval = inm_record_source(inm, ap->s_addr);
1191 			if (retval < 0)
1192 				break;
1193 			nrecorded += retval;
1194 		}
1195 		if (nrecorded > 0) {
1196 			CTR1(KTR_IGMPV3,
1197 			    "%s: schedule response to SG query", __func__);
1198 			inm->inm_state = IGMP_SG_QUERY_PENDING_MEMBER;
1199 			inm->inm_timer = IGMP_RANDOM_DELAY(timer);
1200 			V_current_state_timers_running = 1;
1201 		}
1202 	}
1203 
1204 	return (retval);
1205 }
1206 
1207 /*
1208  * Process a received IGMPv1 host membership report.
1209  *
1210  * NOTE: 0.0.0.0 workaround breaks const correctness.
1211  */
1212 static int
1213 igmp_input_v1_report(struct ifnet *ifp, /*const*/ struct ip *ip,
1214     /*const*/ struct igmp *igmp)
1215 {
1216 	struct in_ifaddr *ia;
1217 	struct in_multi *inm;
1218 
1219 	IGMPSTAT_INC(igps_rcv_reports);
1220 
1221 	if (ifp->if_flags & IFF_LOOPBACK)
1222 		return (0);
1223 
1224 	if (!IN_MULTICAST(ntohl(igmp->igmp_group.s_addr)) ||
1225 	    !in_hosteq(igmp->igmp_group, ip->ip_dst)) {
1226 		IGMPSTAT_INC(igps_rcv_badreports);
1227 		return (EINVAL);
1228 	}
1229 
1230 	/*
1231 	 * RFC 3376, Section 4.2.13, 9.2, 9.3:
1232 	 * Booting clients may use the source address 0.0.0.0. Some
1233 	 * IGMP daemons may not know how to use IP_RECVIF to determine
1234 	 * the interface upon which this message was received.
1235 	 * Replace 0.0.0.0 with the subnet address if told to do so.
1236 	 */
1237 	if (V_igmp_recvifkludge && in_nullhost(ip->ip_src)) {
1238 		IFP_TO_IA(ifp, ia);
1239 		if (ia != NULL) {
1240 			ip->ip_src.s_addr = htonl(ia->ia_subnet);
1241 			ifa_free(&ia->ia_ifa);
1242 		}
1243 	}
1244 
1245 	CTR3(KTR_IGMPV3, "process v1 report %s on ifp %p(%s)",
1246 	     inet_ntoa(igmp->igmp_group), ifp, ifp->if_xname);
1247 
1248 	/*
1249 	 * IGMPv1 report suppression.
1250 	 * If we are a member of this group, and our membership should be
1251 	 * reported, stop our group timer and transition to the 'lazy' state.
1252 	 */
1253 	IN_MULTI_LOCK();
1254 	inm = inm_lookup(ifp, igmp->igmp_group);
1255 	if (inm != NULL) {
1256 		struct igmp_ifinfo *igi;
1257 
1258 		igi = inm->inm_igi;
1259 		if (igi == NULL) {
1260 			KASSERT(igi != NULL,
1261 			    ("%s: no igi for ifp %p", __func__, ifp));
1262 			goto out_locked;
1263 		}
1264 
1265 		IGMPSTAT_INC(igps_rcv_ourreports);
1266 
1267 		/*
1268 		 * If we are in IGMPv3 host mode, do not allow the
1269 		 * other host's IGMPv1 report to suppress our reports
1270 		 * unless explicitly configured to do so.
1271 		 */
1272 		if (igi->igi_version == IGMP_VERSION_3) {
1273 			if (V_igmp_legacysupp)
1274 				igmp_v3_suppress_group_record(inm);
1275 			goto out_locked;
1276 		}
1277 
1278 		inm->inm_timer = 0;
1279 
1280 		switch (inm->inm_state) {
1281 		case IGMP_NOT_MEMBER:
1282 		case IGMP_SILENT_MEMBER:
1283 			break;
1284 		case IGMP_IDLE_MEMBER:
1285 		case IGMP_LAZY_MEMBER:
1286 		case IGMP_AWAKENING_MEMBER:
1287 			CTR3(KTR_IGMPV3,
1288 			    "report suppressed for %s on ifp %p(%s)",
1289 			    inet_ntoa(igmp->igmp_group), ifp, ifp->if_xname);
1290 		case IGMP_SLEEPING_MEMBER:
1291 			inm->inm_state = IGMP_SLEEPING_MEMBER;
1292 			break;
1293 		case IGMP_REPORTING_MEMBER:
1294 			CTR3(KTR_IGMPV3,
1295 			    "report suppressed for %s on ifp %p(%s)",
1296 			    inet_ntoa(igmp->igmp_group), ifp, ifp->if_xname);
1297 			if (igi->igi_version == IGMP_VERSION_1)
1298 				inm->inm_state = IGMP_LAZY_MEMBER;
1299 			else if (igi->igi_version == IGMP_VERSION_2)
1300 				inm->inm_state = IGMP_SLEEPING_MEMBER;
1301 			break;
1302 		case IGMP_G_QUERY_PENDING_MEMBER:
1303 		case IGMP_SG_QUERY_PENDING_MEMBER:
1304 		case IGMP_LEAVING_MEMBER:
1305 			break;
1306 		}
1307 	}
1308 
1309 out_locked:
1310 	IN_MULTI_UNLOCK();
1311 
1312 	return (0);
1313 }
1314 
1315 /*
1316  * Process a received IGMPv2 host membership report.
1317  *
1318  * NOTE: 0.0.0.0 workaround breaks const correctness.
1319  */
1320 static int
1321 igmp_input_v2_report(struct ifnet *ifp, /*const*/ struct ip *ip,
1322     /*const*/ struct igmp *igmp)
1323 {
1324 	struct in_ifaddr *ia;
1325 	struct in_multi *inm;
1326 
1327 	/*
1328 	 * Make sure we don't hear our own membership report.  Fast
1329 	 * leave requires knowing that we are the only member of a
1330 	 * group.
1331 	 */
1332 	IFP_TO_IA(ifp, ia);
1333 	if (ia != NULL && in_hosteq(ip->ip_src, IA_SIN(ia)->sin_addr)) {
1334 		ifa_free(&ia->ia_ifa);
1335 		return (0);
1336 	}
1337 
1338 	IGMPSTAT_INC(igps_rcv_reports);
1339 
1340 	if (ifp->if_flags & IFF_LOOPBACK) {
1341 		if (ia != NULL)
1342 			ifa_free(&ia->ia_ifa);
1343 		return (0);
1344 	}
1345 
1346 	if (!IN_MULTICAST(ntohl(igmp->igmp_group.s_addr)) ||
1347 	    !in_hosteq(igmp->igmp_group, ip->ip_dst)) {
1348 		if (ia != NULL)
1349 			ifa_free(&ia->ia_ifa);
1350 		IGMPSTAT_INC(igps_rcv_badreports);
1351 		return (EINVAL);
1352 	}
1353 
1354 	/*
1355 	 * RFC 3376, Section 4.2.13, 9.2, 9.3:
1356 	 * Booting clients may use the source address 0.0.0.0. Some
1357 	 * IGMP daemons may not know how to use IP_RECVIF to determine
1358 	 * the interface upon which this message was received.
1359 	 * Replace 0.0.0.0 with the subnet address if told to do so.
1360 	 */
1361 	if (V_igmp_recvifkludge && in_nullhost(ip->ip_src)) {
1362 		if (ia != NULL)
1363 			ip->ip_src.s_addr = htonl(ia->ia_subnet);
1364 	}
1365 	if (ia != NULL)
1366 		ifa_free(&ia->ia_ifa);
1367 
1368 	CTR3(KTR_IGMPV3, "process v2 report %s on ifp %p(%s)",
1369 	     inet_ntoa(igmp->igmp_group), ifp, ifp->if_xname);
1370 
1371 	/*
1372 	 * IGMPv2 report suppression.
1373 	 * If we are a member of this group, and our membership should be
1374 	 * reported, and our group timer is pending or about to be reset,
1375 	 * stop our group timer by transitioning to the 'lazy' state.
1376 	 */
1377 	IN_MULTI_LOCK();
1378 	inm = inm_lookup(ifp, igmp->igmp_group);
1379 	if (inm != NULL) {
1380 		struct igmp_ifinfo *igi;
1381 
1382 		igi = inm->inm_igi;
1383 		KASSERT(igi != NULL, ("%s: no igi for ifp %p", __func__, ifp));
1384 
1385 		IGMPSTAT_INC(igps_rcv_ourreports);
1386 
1387 		/*
1388 		 * If we are in IGMPv3 host mode, do not allow the
1389 		 * other host's IGMPv1 report to suppress our reports
1390 		 * unless explicitly configured to do so.
1391 		 */
1392 		if (igi->igi_version == IGMP_VERSION_3) {
1393 			if (V_igmp_legacysupp)
1394 				igmp_v3_suppress_group_record(inm);
1395 			goto out_locked;
1396 		}
1397 
1398 		inm->inm_timer = 0;
1399 
1400 		switch (inm->inm_state) {
1401 		case IGMP_NOT_MEMBER:
1402 		case IGMP_SILENT_MEMBER:
1403 		case IGMP_SLEEPING_MEMBER:
1404 			break;
1405 		case IGMP_REPORTING_MEMBER:
1406 		case IGMP_IDLE_MEMBER:
1407 		case IGMP_AWAKENING_MEMBER:
1408 			CTR3(KTR_IGMPV3,
1409 			    "report suppressed for %s on ifp %p(%s)",
1410 			    inet_ntoa(igmp->igmp_group), ifp, ifp->if_xname);
1411 		case IGMP_LAZY_MEMBER:
1412 			inm->inm_state = IGMP_LAZY_MEMBER;
1413 			break;
1414 		case IGMP_G_QUERY_PENDING_MEMBER:
1415 		case IGMP_SG_QUERY_PENDING_MEMBER:
1416 		case IGMP_LEAVING_MEMBER:
1417 			break;
1418 		}
1419 	}
1420 
1421 out_locked:
1422 	IN_MULTI_UNLOCK();
1423 
1424 	return (0);
1425 }
1426 
1427 void
1428 igmp_input(struct mbuf *m, int off)
1429 {
1430 	int iphlen;
1431 	struct ifnet *ifp;
1432 	struct igmp *igmp;
1433 	struct ip *ip;
1434 	int igmplen;
1435 	int minlen;
1436 	int queryver;
1437 
1438 	CTR3(KTR_IGMPV3, "%s: called w/mbuf (%p,%d)", __func__, m, off);
1439 
1440 	ifp = m->m_pkthdr.rcvif;
1441 
1442 	IGMPSTAT_INC(igps_rcv_total);
1443 
1444 	ip = mtod(m, struct ip *);
1445 	iphlen = off;
1446 	igmplen = ntohs(ip->ip_len) - off;
1447 
1448 	/*
1449 	 * Validate lengths.
1450 	 */
1451 	if (igmplen < IGMP_MINLEN) {
1452 		IGMPSTAT_INC(igps_rcv_tooshort);
1453 		m_freem(m);
1454 		return;
1455 	}
1456 
1457 	/*
1458 	 * Always pullup to the minimum size for v1/v2 or v3
1459 	 * to amortize calls to m_pullup().
1460 	 */
1461 	minlen = iphlen;
1462 	if (igmplen >= IGMP_V3_QUERY_MINLEN)
1463 		minlen += IGMP_V3_QUERY_MINLEN;
1464 	else
1465 		minlen += IGMP_MINLEN;
1466 	if ((m->m_flags & M_EXT || m->m_len < minlen) &&
1467 	    (m = m_pullup(m, minlen)) == 0) {
1468 		IGMPSTAT_INC(igps_rcv_tooshort);
1469 		return;
1470 	}
1471 	ip = mtod(m, struct ip *);
1472 
1473 	/*
1474 	 * Validate checksum.
1475 	 */
1476 	m->m_data += iphlen;
1477 	m->m_len -= iphlen;
1478 	igmp = mtod(m, struct igmp *);
1479 	if (in_cksum(m, igmplen)) {
1480 		IGMPSTAT_INC(igps_rcv_badsum);
1481 		m_freem(m);
1482 		return;
1483 	}
1484 	m->m_data -= iphlen;
1485 	m->m_len += iphlen;
1486 
1487 	/*
1488 	 * IGMP control traffic is link-scope, and must have a TTL of 1.
1489 	 * DVMRP traffic (e.g. mrinfo, mtrace) is an exception;
1490 	 * probe packets may come from beyond the LAN.
1491 	 */
1492 	if (igmp->igmp_type != IGMP_DVMRP && ip->ip_ttl != 1) {
1493 		IGMPSTAT_INC(igps_rcv_badttl);
1494 		m_freem(m);
1495 		return;
1496 	}
1497 
1498 	switch (igmp->igmp_type) {
1499 	case IGMP_HOST_MEMBERSHIP_QUERY:
1500 		if (igmplen == IGMP_MINLEN) {
1501 			if (igmp->igmp_code == 0)
1502 				queryver = IGMP_VERSION_1;
1503 			else
1504 				queryver = IGMP_VERSION_2;
1505 		} else if (igmplen >= IGMP_V3_QUERY_MINLEN) {
1506 			queryver = IGMP_VERSION_3;
1507 		} else {
1508 			IGMPSTAT_INC(igps_rcv_tooshort);
1509 			m_freem(m);
1510 			return;
1511 		}
1512 
1513 		switch (queryver) {
1514 		case IGMP_VERSION_1:
1515 			IGMPSTAT_INC(igps_rcv_v1v2_queries);
1516 			if (!V_igmp_v1enable)
1517 				break;
1518 			if (igmp_input_v1_query(ifp, ip, igmp) != 0) {
1519 				m_freem(m);
1520 				return;
1521 			}
1522 			break;
1523 
1524 		case IGMP_VERSION_2:
1525 			IGMPSTAT_INC(igps_rcv_v1v2_queries);
1526 			if (!V_igmp_v2enable)
1527 				break;
1528 			if (igmp_input_v2_query(ifp, ip, igmp) != 0) {
1529 				m_freem(m);
1530 				return;
1531 			}
1532 			break;
1533 
1534 		case IGMP_VERSION_3: {
1535 				struct igmpv3 *igmpv3;
1536 				uint16_t igmpv3len;
1537 				uint16_t srclen;
1538 				int nsrc;
1539 
1540 				IGMPSTAT_INC(igps_rcv_v3_queries);
1541 				igmpv3 = (struct igmpv3 *)igmp;
1542 				/*
1543 				 * Validate length based on source count.
1544 				 */
1545 				nsrc = ntohs(igmpv3->igmp_numsrc);
1546 				srclen = sizeof(struct in_addr) * nsrc;
1547 				if (nsrc * sizeof(in_addr_t) > srclen) {
1548 					IGMPSTAT_INC(igps_rcv_tooshort);
1549 					return;
1550 				}
1551 				/*
1552 				 * m_pullup() may modify m, so pullup in
1553 				 * this scope.
1554 				 */
1555 				igmpv3len = iphlen + IGMP_V3_QUERY_MINLEN +
1556 				    srclen;
1557 				if ((m->m_flags & M_EXT ||
1558 				     m->m_len < igmpv3len) &&
1559 				    (m = m_pullup(m, igmpv3len)) == NULL) {
1560 					IGMPSTAT_INC(igps_rcv_tooshort);
1561 					return;
1562 				}
1563 				igmpv3 = (struct igmpv3 *)(mtod(m, uint8_t *)
1564 				    + iphlen);
1565 				if (igmp_input_v3_query(ifp, ip, igmpv3) != 0) {
1566 					m_freem(m);
1567 					return;
1568 				}
1569 			}
1570 			break;
1571 		}
1572 		break;
1573 
1574 	case IGMP_v1_HOST_MEMBERSHIP_REPORT:
1575 		if (!V_igmp_v1enable)
1576 			break;
1577 		if (igmp_input_v1_report(ifp, ip, igmp) != 0) {
1578 			m_freem(m);
1579 			return;
1580 		}
1581 		break;
1582 
1583 	case IGMP_v2_HOST_MEMBERSHIP_REPORT:
1584 		if (!V_igmp_v2enable)
1585 			break;
1586 		if (!ip_checkrouteralert(m))
1587 			IGMPSTAT_INC(igps_rcv_nora);
1588 		if (igmp_input_v2_report(ifp, ip, igmp) != 0) {
1589 			m_freem(m);
1590 			return;
1591 		}
1592 		break;
1593 
1594 	case IGMP_v3_HOST_MEMBERSHIP_REPORT:
1595 		/*
1596 		 * Hosts do not need to process IGMPv3 membership reports,
1597 		 * as report suppression is no longer required.
1598 		 */
1599 		if (!ip_checkrouteralert(m))
1600 			IGMPSTAT_INC(igps_rcv_nora);
1601 		break;
1602 
1603 	default:
1604 		break;
1605 	}
1606 
1607 	/*
1608 	 * Pass all valid IGMP packets up to any process(es) listening on a
1609 	 * raw IGMP socket.
1610 	 */
1611 	rip_input(m, off);
1612 }
1613 
1614 
1615 /*
1616  * Fast timeout handler (global).
1617  * VIMAGE: Timeout handlers are expected to service all vimages.
1618  */
1619 void
1620 igmp_fasttimo(void)
1621 {
1622 	VNET_ITERATOR_DECL(vnet_iter);
1623 
1624 	VNET_LIST_RLOCK_NOSLEEP();
1625 	VNET_FOREACH(vnet_iter) {
1626 		CURVNET_SET(vnet_iter);
1627 		igmp_fasttimo_vnet();
1628 		CURVNET_RESTORE();
1629 	}
1630 	VNET_LIST_RUNLOCK_NOSLEEP();
1631 }
1632 
1633 /*
1634  * Fast timeout handler (per-vnet).
1635  * Sends are shuffled off to a netisr to deal with Giant.
1636  *
1637  * VIMAGE: Assume caller has set up our curvnet.
1638  */
1639 static void
1640 igmp_fasttimo_vnet(void)
1641 {
1642 	struct ifqueue		 scq;	/* State-change packets */
1643 	struct ifqueue		 qrq;	/* Query response packets */
1644 	struct ifnet		*ifp;
1645 	struct igmp_ifinfo	*igi;
1646 	struct ifmultiaddr	*ifma;
1647 	struct in_multi		*inm;
1648 	int			 loop, uri_fasthz;
1649 
1650 	loop = 0;
1651 	uri_fasthz = 0;
1652 
1653 	/*
1654 	 * Quick check to see if any work needs to be done, in order to
1655 	 * minimize the overhead of fasttimo processing.
1656 	 * SMPng: XXX Unlocked reads.
1657 	 */
1658 	if (!V_current_state_timers_running &&
1659 	    !V_interface_timers_running &&
1660 	    !V_state_change_timers_running)
1661 		return;
1662 
1663 	IN_MULTI_LOCK();
1664 	IGMP_LOCK();
1665 
1666 	/*
1667 	 * IGMPv3 General Query response timer processing.
1668 	 */
1669 	if (V_interface_timers_running) {
1670 		CTR1(KTR_IGMPV3, "%s: interface timers running", __func__);
1671 
1672 		V_interface_timers_running = 0;
1673 		LIST_FOREACH(igi, &V_igi_head, igi_link) {
1674 			if (igi->igi_v3_timer == 0) {
1675 				/* Do nothing. */
1676 			} else if (--igi->igi_v3_timer == 0) {
1677 				igmp_v3_dispatch_general_query(igi);
1678 			} else {
1679 				V_interface_timers_running = 1;
1680 			}
1681 		}
1682 	}
1683 
1684 	if (!V_current_state_timers_running &&
1685 	    !V_state_change_timers_running)
1686 		goto out_locked;
1687 
1688 	V_current_state_timers_running = 0;
1689 	V_state_change_timers_running = 0;
1690 
1691 	CTR1(KTR_IGMPV3, "%s: state change timers running", __func__);
1692 
1693 	/*
1694 	 * IGMPv1/v2/v3 host report and state-change timer processing.
1695 	 * Note: Processing a v3 group timer may remove a node.
1696 	 */
1697 	LIST_FOREACH(igi, &V_igi_head, igi_link) {
1698 		ifp = igi->igi_ifp;
1699 
1700 		if (igi->igi_version == IGMP_VERSION_3) {
1701 			loop = (igi->igi_flags & IGIF_LOOPBACK) ? 1 : 0;
1702 			uri_fasthz = IGMP_RANDOM_DELAY(igi->igi_uri *
1703 			    PR_FASTHZ);
1704 
1705 			memset(&qrq, 0, sizeof(struct ifqueue));
1706 			IFQ_SET_MAXLEN(&qrq, IGMP_MAX_G_GS_PACKETS);
1707 
1708 			memset(&scq, 0, sizeof(struct ifqueue));
1709 			IFQ_SET_MAXLEN(&scq, IGMP_MAX_STATE_CHANGE_PACKETS);
1710 		}
1711 
1712 		IF_ADDR_RLOCK(ifp);
1713 		TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
1714 			if (ifma->ifma_addr->sa_family != AF_INET ||
1715 			    ifma->ifma_protospec == NULL)
1716 				continue;
1717 			inm = (struct in_multi *)ifma->ifma_protospec;
1718 			switch (igi->igi_version) {
1719 			case IGMP_VERSION_1:
1720 			case IGMP_VERSION_2:
1721 				igmp_v1v2_process_group_timer(inm,
1722 				    igi->igi_version);
1723 				break;
1724 			case IGMP_VERSION_3:
1725 				igmp_v3_process_group_timers(igi, &qrq,
1726 				    &scq, inm, uri_fasthz);
1727 				break;
1728 			}
1729 		}
1730 		IF_ADDR_RUNLOCK(ifp);
1731 
1732 		if (igi->igi_version == IGMP_VERSION_3) {
1733 			struct in_multi		*tinm;
1734 
1735 			igmp_dispatch_queue(&qrq, 0, loop);
1736 			igmp_dispatch_queue(&scq, 0, loop);
1737 
1738 			/*
1739 			 * Free the in_multi reference(s) for this
1740 			 * IGMP lifecycle.
1741 			 */
1742 			SLIST_FOREACH_SAFE(inm, &igi->igi_relinmhead,
1743 			    inm_nrele, tinm) {
1744 				SLIST_REMOVE_HEAD(&igi->igi_relinmhead,
1745 				    inm_nrele);
1746 				inm_release_locked(inm);
1747 			}
1748 		}
1749 	}
1750 
1751 out_locked:
1752 	IGMP_UNLOCK();
1753 	IN_MULTI_UNLOCK();
1754 }
1755 
1756 /*
1757  * Update host report group timer for IGMPv1/v2.
1758  * Will update the global pending timer flags.
1759  */
1760 static void
1761 igmp_v1v2_process_group_timer(struct in_multi *inm, const int version)
1762 {
1763 	int report_timer_expired;
1764 
1765 	IN_MULTI_LOCK_ASSERT();
1766 	IGMP_LOCK_ASSERT();
1767 
1768 	if (inm->inm_timer == 0) {
1769 		report_timer_expired = 0;
1770 	} else if (--inm->inm_timer == 0) {
1771 		report_timer_expired = 1;
1772 	} else {
1773 		V_current_state_timers_running = 1;
1774 		return;
1775 	}
1776 
1777 	switch (inm->inm_state) {
1778 	case IGMP_NOT_MEMBER:
1779 	case IGMP_SILENT_MEMBER:
1780 	case IGMP_IDLE_MEMBER:
1781 	case IGMP_LAZY_MEMBER:
1782 	case IGMP_SLEEPING_MEMBER:
1783 	case IGMP_AWAKENING_MEMBER:
1784 		break;
1785 	case IGMP_REPORTING_MEMBER:
1786 		if (report_timer_expired) {
1787 			inm->inm_state = IGMP_IDLE_MEMBER;
1788 			(void)igmp_v1v2_queue_report(inm,
1789 			    (version == IGMP_VERSION_2) ?
1790 			     IGMP_v2_HOST_MEMBERSHIP_REPORT :
1791 			     IGMP_v1_HOST_MEMBERSHIP_REPORT);
1792 		}
1793 		break;
1794 	case IGMP_G_QUERY_PENDING_MEMBER:
1795 	case IGMP_SG_QUERY_PENDING_MEMBER:
1796 	case IGMP_LEAVING_MEMBER:
1797 		break;
1798 	}
1799 }
1800 
1801 /*
1802  * Update a group's timers for IGMPv3.
1803  * Will update the global pending timer flags.
1804  * Note: Unlocked read from igi.
1805  */
1806 static void
1807 igmp_v3_process_group_timers(struct igmp_ifinfo *igi,
1808     struct ifqueue *qrq, struct ifqueue *scq,
1809     struct in_multi *inm, const int uri_fasthz)
1810 {
1811 	int query_response_timer_expired;
1812 	int state_change_retransmit_timer_expired;
1813 
1814 	IN_MULTI_LOCK_ASSERT();
1815 	IGMP_LOCK_ASSERT();
1816 
1817 	query_response_timer_expired = 0;
1818 	state_change_retransmit_timer_expired = 0;
1819 
1820 	/*
1821 	 * During a transition from v1/v2 compatibility mode back to v3,
1822 	 * a group record in REPORTING state may still have its group
1823 	 * timer active. This is a no-op in this function; it is easier
1824 	 * to deal with it here than to complicate the slow-timeout path.
1825 	 */
1826 	if (inm->inm_timer == 0) {
1827 		query_response_timer_expired = 0;
1828 	} else if (--inm->inm_timer == 0) {
1829 		query_response_timer_expired = 1;
1830 	} else {
1831 		V_current_state_timers_running = 1;
1832 	}
1833 
1834 	if (inm->inm_sctimer == 0) {
1835 		state_change_retransmit_timer_expired = 0;
1836 	} else if (--inm->inm_sctimer == 0) {
1837 		state_change_retransmit_timer_expired = 1;
1838 	} else {
1839 		V_state_change_timers_running = 1;
1840 	}
1841 
1842 	/* We are in fasttimo, so be quick about it. */
1843 	if (!state_change_retransmit_timer_expired &&
1844 	    !query_response_timer_expired)
1845 		return;
1846 
1847 	switch (inm->inm_state) {
1848 	case IGMP_NOT_MEMBER:
1849 	case IGMP_SILENT_MEMBER:
1850 	case IGMP_SLEEPING_MEMBER:
1851 	case IGMP_LAZY_MEMBER:
1852 	case IGMP_AWAKENING_MEMBER:
1853 	case IGMP_IDLE_MEMBER:
1854 		break;
1855 	case IGMP_G_QUERY_PENDING_MEMBER:
1856 	case IGMP_SG_QUERY_PENDING_MEMBER:
1857 		/*
1858 		 * Respond to a previously pending Group-Specific
1859 		 * or Group-and-Source-Specific query by enqueueing
1860 		 * the appropriate Current-State report for
1861 		 * immediate transmission.
1862 		 */
1863 		if (query_response_timer_expired) {
1864 			int retval;
1865 
1866 			retval = igmp_v3_enqueue_group_record(qrq, inm, 0, 1,
1867 			    (inm->inm_state == IGMP_SG_QUERY_PENDING_MEMBER));
1868 			CTR2(KTR_IGMPV3, "%s: enqueue record = %d",
1869 			    __func__, retval);
1870 			inm->inm_state = IGMP_REPORTING_MEMBER;
1871 			/* XXX Clear recorded sources for next time. */
1872 			inm_clear_recorded(inm);
1873 		}
1874 		/* FALLTHROUGH */
1875 	case IGMP_REPORTING_MEMBER:
1876 	case IGMP_LEAVING_MEMBER:
1877 		if (state_change_retransmit_timer_expired) {
1878 			/*
1879 			 * State-change retransmission timer fired.
1880 			 * If there are any further pending retransmissions,
1881 			 * set the global pending state-change flag, and
1882 			 * reset the timer.
1883 			 */
1884 			if (--inm->inm_scrv > 0) {
1885 				inm->inm_sctimer = uri_fasthz;
1886 				V_state_change_timers_running = 1;
1887 			}
1888 			/*
1889 			 * Retransmit the previously computed state-change
1890 			 * report. If there are no further pending
1891 			 * retransmissions, the mbuf queue will be consumed.
1892 			 * Update T0 state to T1 as we have now sent
1893 			 * a state-change.
1894 			 */
1895 			(void)igmp_v3_merge_state_changes(inm, scq);
1896 
1897 			inm_commit(inm);
1898 			CTR3(KTR_IGMPV3, "%s: T1 -> T0 for %s/%s", __func__,
1899 			    inet_ntoa(inm->inm_addr), inm->inm_ifp->if_xname);
1900 
1901 			/*
1902 			 * If we are leaving the group for good, make sure
1903 			 * we release IGMP's reference to it.
1904 			 * This release must be deferred using a SLIST,
1905 			 * as we are called from a loop which traverses
1906 			 * the in_ifmultiaddr TAILQ.
1907 			 */
1908 			if (inm->inm_state == IGMP_LEAVING_MEMBER &&
1909 			    inm->inm_scrv == 0) {
1910 				inm->inm_state = IGMP_NOT_MEMBER;
1911 				SLIST_INSERT_HEAD(&igi->igi_relinmhead,
1912 				    inm, inm_nrele);
1913 			}
1914 		}
1915 		break;
1916 	}
1917 }
1918 
1919 
1920 /*
1921  * Suppress a group's pending response to a group or source/group query.
1922  *
1923  * Do NOT suppress state changes. This leads to IGMPv3 inconsistency.
1924  * Do NOT update ST1/ST0 as this operation merely suppresses
1925  * the currently pending group record.
1926  * Do NOT suppress the response to a general query. It is possible but
1927  * it would require adding another state or flag.
1928  */
1929 static void
1930 igmp_v3_suppress_group_record(struct in_multi *inm)
1931 {
1932 
1933 	IN_MULTI_LOCK_ASSERT();
1934 
1935 	KASSERT(inm->inm_igi->igi_version == IGMP_VERSION_3,
1936 		("%s: not IGMPv3 mode on link", __func__));
1937 
1938 	if (inm->inm_state != IGMP_G_QUERY_PENDING_MEMBER ||
1939 	    inm->inm_state != IGMP_SG_QUERY_PENDING_MEMBER)
1940 		return;
1941 
1942 	if (inm->inm_state == IGMP_SG_QUERY_PENDING_MEMBER)
1943 		inm_clear_recorded(inm);
1944 
1945 	inm->inm_timer = 0;
1946 	inm->inm_state = IGMP_REPORTING_MEMBER;
1947 }
1948 
1949 /*
1950  * Switch to a different IGMP version on the given interface,
1951  * as per Section 7.2.1.
1952  */
1953 static void
1954 igmp_set_version(struct igmp_ifinfo *igi, const int version)
1955 {
1956 	int old_version_timer;
1957 
1958 	IGMP_LOCK_ASSERT();
1959 
1960 	CTR4(KTR_IGMPV3, "%s: switching to v%d on ifp %p(%s)", __func__,
1961 	    version, igi->igi_ifp, igi->igi_ifp->if_xname);
1962 
1963 	if (version == IGMP_VERSION_1 || version == IGMP_VERSION_2) {
1964 		/*
1965 		 * Compute the "Older Version Querier Present" timer as per
1966 		 * Section 8.12.
1967 		 */
1968 		old_version_timer = igi->igi_rv * igi->igi_qi + igi->igi_qri;
1969 		old_version_timer *= PR_SLOWHZ;
1970 
1971 		if (version == IGMP_VERSION_1) {
1972 			igi->igi_v1_timer = old_version_timer;
1973 			igi->igi_v2_timer = 0;
1974 		} else if (version == IGMP_VERSION_2) {
1975 			igi->igi_v1_timer = 0;
1976 			igi->igi_v2_timer = old_version_timer;
1977 		}
1978 	}
1979 
1980 	if (igi->igi_v1_timer == 0 && igi->igi_v2_timer > 0) {
1981 		if (igi->igi_version != IGMP_VERSION_2) {
1982 			igi->igi_version = IGMP_VERSION_2;
1983 			igmp_v3_cancel_link_timers(igi);
1984 		}
1985 	} else if (igi->igi_v1_timer > 0) {
1986 		if (igi->igi_version != IGMP_VERSION_1) {
1987 			igi->igi_version = IGMP_VERSION_1;
1988 			igmp_v3_cancel_link_timers(igi);
1989 		}
1990 	}
1991 }
1992 
1993 /*
1994  * Cancel pending IGMPv3 timers for the given link and all groups
1995  * joined on it; state-change, general-query, and group-query timers.
1996  *
1997  * Only ever called on a transition from v3 to Compatibility mode. Kill
1998  * the timers stone dead (this may be expensive for large N groups), they
1999  * will be restarted if Compatibility Mode deems that they must be due to
2000  * query processing.
2001  */
2002 static void
2003 igmp_v3_cancel_link_timers(struct igmp_ifinfo *igi)
2004 {
2005 	struct ifmultiaddr	*ifma;
2006 	struct ifnet		*ifp;
2007 	struct in_multi		*inm, *tinm;
2008 
2009 	CTR3(KTR_IGMPV3, "%s: cancel v3 timers on ifp %p(%s)", __func__,
2010 	    igi->igi_ifp, igi->igi_ifp->if_xname);
2011 
2012 	IN_MULTI_LOCK_ASSERT();
2013 	IGMP_LOCK_ASSERT();
2014 
2015 	/*
2016 	 * Stop the v3 General Query Response on this link stone dead.
2017 	 * If fasttimo is woken up due to V_interface_timers_running,
2018 	 * the flag will be cleared if there are no pending link timers.
2019 	 */
2020 	igi->igi_v3_timer = 0;
2021 
2022 	/*
2023 	 * Now clear the current-state and state-change report timers
2024 	 * for all memberships scoped to this link.
2025 	 */
2026 	ifp = igi->igi_ifp;
2027 	IF_ADDR_RLOCK(ifp);
2028 	TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
2029 		if (ifma->ifma_addr->sa_family != AF_INET ||
2030 		    ifma->ifma_protospec == NULL)
2031 			continue;
2032 		inm = (struct in_multi *)ifma->ifma_protospec;
2033 		switch (inm->inm_state) {
2034 		case IGMP_NOT_MEMBER:
2035 		case IGMP_SILENT_MEMBER:
2036 		case IGMP_IDLE_MEMBER:
2037 		case IGMP_LAZY_MEMBER:
2038 		case IGMP_SLEEPING_MEMBER:
2039 		case IGMP_AWAKENING_MEMBER:
2040 			/*
2041 			 * These states are either not relevant in v3 mode,
2042 			 * or are unreported. Do nothing.
2043 			 */
2044 			break;
2045 		case IGMP_LEAVING_MEMBER:
2046 			/*
2047 			 * If we are leaving the group and switching to
2048 			 * compatibility mode, we need to release the final
2049 			 * reference held for issuing the INCLUDE {}, and
2050 			 * transition to REPORTING to ensure the host leave
2051 			 * message is sent upstream to the old querier --
2052 			 * transition to NOT would lose the leave and race.
2053 			 */
2054 			SLIST_INSERT_HEAD(&igi->igi_relinmhead, inm, inm_nrele);
2055 			/* FALLTHROUGH */
2056 		case IGMP_G_QUERY_PENDING_MEMBER:
2057 		case IGMP_SG_QUERY_PENDING_MEMBER:
2058 			inm_clear_recorded(inm);
2059 			/* FALLTHROUGH */
2060 		case IGMP_REPORTING_MEMBER:
2061 			inm->inm_state = IGMP_REPORTING_MEMBER;
2062 			break;
2063 		}
2064 		/*
2065 		 * Always clear state-change and group report timers.
2066 		 * Free any pending IGMPv3 state-change records.
2067 		 */
2068 		inm->inm_sctimer = 0;
2069 		inm->inm_timer = 0;
2070 		_IF_DRAIN(&inm->inm_scq);
2071 	}
2072 	IF_ADDR_RUNLOCK(ifp);
2073 	SLIST_FOREACH_SAFE(inm, &igi->igi_relinmhead, inm_nrele, tinm) {
2074 		SLIST_REMOVE_HEAD(&igi->igi_relinmhead, inm_nrele);
2075 		inm_release_locked(inm);
2076 	}
2077 }
2078 
2079 /*
2080  * Update the Older Version Querier Present timers for a link.
2081  * See Section 7.2.1 of RFC 3376.
2082  */
2083 static void
2084 igmp_v1v2_process_querier_timers(struct igmp_ifinfo *igi)
2085 {
2086 
2087 	IGMP_LOCK_ASSERT();
2088 
2089 	if (igi->igi_v1_timer == 0 && igi->igi_v2_timer == 0) {
2090 		/*
2091 		 * IGMPv1 and IGMPv2 Querier Present timers expired.
2092 		 *
2093 		 * Revert to IGMPv3.
2094 		 */
2095 		if (igi->igi_version != IGMP_VERSION_3) {
2096 			CTR5(KTR_IGMPV3,
2097 			    "%s: transition from v%d -> v%d on %p(%s)",
2098 			    __func__, igi->igi_version, IGMP_VERSION_3,
2099 			    igi->igi_ifp, igi->igi_ifp->if_xname);
2100 			igi->igi_version = IGMP_VERSION_3;
2101 		}
2102 	} else if (igi->igi_v1_timer == 0 && igi->igi_v2_timer > 0) {
2103 		/*
2104 		 * IGMPv1 Querier Present timer expired,
2105 		 * IGMPv2 Querier Present timer running.
2106 		 * If IGMPv2 was disabled since last timeout,
2107 		 * revert to IGMPv3.
2108 		 * If IGMPv2 is enabled, revert to IGMPv2.
2109 		 */
2110 		if (!V_igmp_v2enable) {
2111 			CTR5(KTR_IGMPV3,
2112 			    "%s: transition from v%d -> v%d on %p(%s)",
2113 			    __func__, igi->igi_version, IGMP_VERSION_3,
2114 			    igi->igi_ifp, igi->igi_ifp->if_xname);
2115 			igi->igi_v2_timer = 0;
2116 			igi->igi_version = IGMP_VERSION_3;
2117 		} else {
2118 			--igi->igi_v2_timer;
2119 			if (igi->igi_version != IGMP_VERSION_2) {
2120 				CTR5(KTR_IGMPV3,
2121 				    "%s: transition from v%d -> v%d on %p(%s)",
2122 				    __func__, igi->igi_version, IGMP_VERSION_2,
2123 				    igi->igi_ifp, igi->igi_ifp->if_xname);
2124 				igi->igi_version = IGMP_VERSION_2;
2125 				igmp_v3_cancel_link_timers(igi);
2126 			}
2127 		}
2128 	} else if (igi->igi_v1_timer > 0) {
2129 		/*
2130 		 * IGMPv1 Querier Present timer running.
2131 		 * Stop IGMPv2 timer if running.
2132 		 *
2133 		 * If IGMPv1 was disabled since last timeout,
2134 		 * revert to IGMPv3.
2135 		 * If IGMPv1 is enabled, reset IGMPv2 timer if running.
2136 		 */
2137 		if (!V_igmp_v1enable) {
2138 			CTR5(KTR_IGMPV3,
2139 			    "%s: transition from v%d -> v%d on %p(%s)",
2140 			    __func__, igi->igi_version, IGMP_VERSION_3,
2141 			    igi->igi_ifp, igi->igi_ifp->if_xname);
2142 			igi->igi_v1_timer = 0;
2143 			igi->igi_version = IGMP_VERSION_3;
2144 		} else {
2145 			--igi->igi_v1_timer;
2146 		}
2147 		if (igi->igi_v2_timer > 0) {
2148 			CTR3(KTR_IGMPV3,
2149 			    "%s: cancel v2 timer on %p(%s)",
2150 			    __func__, igi->igi_ifp, igi->igi_ifp->if_xname);
2151 			igi->igi_v2_timer = 0;
2152 		}
2153 	}
2154 }
2155 
2156 /*
2157  * Global slowtimo handler.
2158  * VIMAGE: Timeout handlers are expected to service all vimages.
2159  */
2160 void
2161 igmp_slowtimo(void)
2162 {
2163 	VNET_ITERATOR_DECL(vnet_iter);
2164 
2165 	VNET_LIST_RLOCK_NOSLEEP();
2166 	VNET_FOREACH(vnet_iter) {
2167 		CURVNET_SET(vnet_iter);
2168 		igmp_slowtimo_vnet();
2169 		CURVNET_RESTORE();
2170 	}
2171 	VNET_LIST_RUNLOCK_NOSLEEP();
2172 }
2173 
2174 /*
2175  * Per-vnet slowtimo handler.
2176  */
2177 static void
2178 igmp_slowtimo_vnet(void)
2179 {
2180 	struct igmp_ifinfo *igi;
2181 
2182 	IGMP_LOCK();
2183 
2184 	LIST_FOREACH(igi, &V_igi_head, igi_link) {
2185 		igmp_v1v2_process_querier_timers(igi);
2186 	}
2187 
2188 	IGMP_UNLOCK();
2189 }
2190 
2191 /*
2192  * Dispatch an IGMPv1/v2 host report or leave message.
2193  * These are always small enough to fit inside a single mbuf.
2194  */
2195 static int
2196 igmp_v1v2_queue_report(struct in_multi *inm, const int type)
2197 {
2198 	struct ifnet		*ifp;
2199 	struct igmp		*igmp;
2200 	struct ip		*ip;
2201 	struct mbuf		*m;
2202 
2203 	IN_MULTI_LOCK_ASSERT();
2204 	IGMP_LOCK_ASSERT();
2205 
2206 	ifp = inm->inm_ifp;
2207 
2208 	m = m_gethdr(M_NOWAIT, MT_DATA);
2209 	if (m == NULL)
2210 		return (ENOMEM);
2211 	MH_ALIGN(m, sizeof(struct ip) + sizeof(struct igmp));
2212 
2213 	m->m_pkthdr.len = sizeof(struct ip) + sizeof(struct igmp);
2214 
2215 	m->m_data += sizeof(struct ip);
2216 	m->m_len = sizeof(struct igmp);
2217 
2218 	igmp = mtod(m, struct igmp *);
2219 	igmp->igmp_type = type;
2220 	igmp->igmp_code = 0;
2221 	igmp->igmp_group = inm->inm_addr;
2222 	igmp->igmp_cksum = 0;
2223 	igmp->igmp_cksum = in_cksum(m, sizeof(struct igmp));
2224 
2225 	m->m_data -= sizeof(struct ip);
2226 	m->m_len += sizeof(struct ip);
2227 
2228 	ip = mtod(m, struct ip *);
2229 	ip->ip_tos = 0;
2230 	ip->ip_len = htons(sizeof(struct ip) + sizeof(struct igmp));
2231 	ip->ip_off = 0;
2232 	ip->ip_p = IPPROTO_IGMP;
2233 	ip->ip_src.s_addr = INADDR_ANY;
2234 
2235 	if (type == IGMP_HOST_LEAVE_MESSAGE)
2236 		ip->ip_dst.s_addr = htonl(INADDR_ALLRTRS_GROUP);
2237 	else
2238 		ip->ip_dst = inm->inm_addr;
2239 
2240 	igmp_save_context(m, ifp);
2241 
2242 	m->m_flags |= M_IGMPV2;
2243 	if (inm->inm_igi->igi_flags & IGIF_LOOPBACK)
2244 		m->m_flags |= M_IGMP_LOOP;
2245 
2246 	CTR2(KTR_IGMPV3, "%s: netisr_dispatch(NETISR_IGMP, %p)", __func__, m);
2247 	netisr_dispatch(NETISR_IGMP, m);
2248 
2249 	return (0);
2250 }
2251 
2252 /*
2253  * Process a state change from the upper layer for the given IPv4 group.
2254  *
2255  * Each socket holds a reference on the in_multi in its own ip_moptions.
2256  * The socket layer will have made the necessary updates to.the group
2257  * state, it is now up to IGMP to issue a state change report if there
2258  * has been any change between T0 (when the last state-change was issued)
2259  * and T1 (now).
2260  *
2261  * We use the IGMPv3 state machine at group level. The IGMP module
2262  * however makes the decision as to which IGMP protocol version to speak.
2263  * A state change *from* INCLUDE {} always means an initial join.
2264  * A state change *to* INCLUDE {} always means a final leave.
2265  *
2266  * FUTURE: If IGIF_V3LITE is enabled for this interface, then we can
2267  * save ourselves a bunch of work; any exclusive mode groups need not
2268  * compute source filter lists.
2269  *
2270  * VIMAGE: curvnet should have been set by caller, as this routine
2271  * is called from the socket option handlers.
2272  */
2273 int
2274 igmp_change_state(struct in_multi *inm)
2275 {
2276 	struct igmp_ifinfo *igi;
2277 	struct ifnet *ifp;
2278 	int error;
2279 
2280 	IN_MULTI_LOCK_ASSERT();
2281 
2282 	error = 0;
2283 
2284 	/*
2285 	 * Try to detect if the upper layer just asked us to change state
2286 	 * for an interface which has now gone away.
2287 	 */
2288 	KASSERT(inm->inm_ifma != NULL, ("%s: no ifma", __func__));
2289 	ifp = inm->inm_ifma->ifma_ifp;
2290 	/*
2291 	 * Sanity check that netinet's notion of ifp is the
2292 	 * same as net's.
2293 	 */
2294 	KASSERT(inm->inm_ifp == ifp, ("%s: bad ifp", __func__));
2295 
2296 	IGMP_LOCK();
2297 
2298 	igi = ((struct in_ifinfo *)ifp->if_afdata[AF_INET])->ii_igmp;
2299 	KASSERT(igi != NULL, ("%s: no igmp_ifinfo for ifp %p", __func__, ifp));
2300 
2301 	/*
2302 	 * If we detect a state transition to or from MCAST_UNDEFINED
2303 	 * for this group, then we are starting or finishing an IGMP
2304 	 * life cycle for this group.
2305 	 */
2306 	if (inm->inm_st[1].iss_fmode != inm->inm_st[0].iss_fmode) {
2307 		CTR3(KTR_IGMPV3, "%s: inm transition %d -> %d", __func__,
2308 		    inm->inm_st[0].iss_fmode, inm->inm_st[1].iss_fmode);
2309 		if (inm->inm_st[0].iss_fmode == MCAST_UNDEFINED) {
2310 			CTR1(KTR_IGMPV3, "%s: initial join", __func__);
2311 			error = igmp_initial_join(inm, igi);
2312 			goto out_locked;
2313 		} else if (inm->inm_st[1].iss_fmode == MCAST_UNDEFINED) {
2314 			CTR1(KTR_IGMPV3, "%s: final leave", __func__);
2315 			igmp_final_leave(inm, igi);
2316 			goto out_locked;
2317 		}
2318 	} else {
2319 		CTR1(KTR_IGMPV3, "%s: filter set change", __func__);
2320 	}
2321 
2322 	error = igmp_handle_state_change(inm, igi);
2323 
2324 out_locked:
2325 	IGMP_UNLOCK();
2326 	return (error);
2327 }
2328 
2329 /*
2330  * Perform the initial join for an IGMP group.
2331  *
2332  * When joining a group:
2333  *  If the group should have its IGMP traffic suppressed, do nothing.
2334  *  IGMPv1 starts sending IGMPv1 host membership reports.
2335  *  IGMPv2 starts sending IGMPv2 host membership reports.
2336  *  IGMPv3 will schedule an IGMPv3 state-change report containing the
2337  *  initial state of the membership.
2338  */
2339 static int
2340 igmp_initial_join(struct in_multi *inm, struct igmp_ifinfo *igi)
2341 {
2342 	struct ifnet		*ifp;
2343 	struct ifqueue		*ifq;
2344 	int			 error, retval, syncstates;
2345 
2346 	CTR4(KTR_IGMPV3, "%s: initial join %s on ifp %p(%s)",
2347 	    __func__, inet_ntoa(inm->inm_addr), inm->inm_ifp,
2348 	    inm->inm_ifp->if_xname);
2349 
2350 	error = 0;
2351 	syncstates = 1;
2352 
2353 	ifp = inm->inm_ifp;
2354 
2355 	IN_MULTI_LOCK_ASSERT();
2356 	IGMP_LOCK_ASSERT();
2357 
2358 	KASSERT(igi && igi->igi_ifp == ifp, ("%s: inconsistent ifp", __func__));
2359 
2360 	/*
2361 	 * Groups joined on loopback or marked as 'not reported',
2362 	 * e.g. 224.0.0.1, enter the IGMP_SILENT_MEMBER state and
2363 	 * are never reported in any IGMP protocol exchanges.
2364 	 * All other groups enter the appropriate IGMP state machine
2365 	 * for the version in use on this link.
2366 	 * A link marked as IGIF_SILENT causes IGMP to be completely
2367 	 * disabled for the link.
2368 	 */
2369 	if ((ifp->if_flags & IFF_LOOPBACK) ||
2370 	    (igi->igi_flags & IGIF_SILENT) ||
2371 	    !igmp_isgroupreported(inm->inm_addr)) {
2372 		CTR1(KTR_IGMPV3,
2373 "%s: not kicking state machine for silent group", __func__);
2374 		inm->inm_state = IGMP_SILENT_MEMBER;
2375 		inm->inm_timer = 0;
2376 	} else {
2377 		/*
2378 		 * Deal with overlapping in_multi lifecycle.
2379 		 * If this group was LEAVING, then make sure
2380 		 * we drop the reference we picked up to keep the
2381 		 * group around for the final INCLUDE {} enqueue.
2382 		 */
2383 		if (igi->igi_version == IGMP_VERSION_3 &&
2384 		    inm->inm_state == IGMP_LEAVING_MEMBER)
2385 			inm_release_locked(inm);
2386 
2387 		inm->inm_state = IGMP_REPORTING_MEMBER;
2388 
2389 		switch (igi->igi_version) {
2390 		case IGMP_VERSION_1:
2391 		case IGMP_VERSION_2:
2392 			inm->inm_state = IGMP_IDLE_MEMBER;
2393 			error = igmp_v1v2_queue_report(inm,
2394 			    (igi->igi_version == IGMP_VERSION_2) ?
2395 			     IGMP_v2_HOST_MEMBERSHIP_REPORT :
2396 			     IGMP_v1_HOST_MEMBERSHIP_REPORT);
2397 			if (error == 0) {
2398 				inm->inm_timer = IGMP_RANDOM_DELAY(
2399 				    IGMP_V1V2_MAX_RI * PR_FASTHZ);
2400 				V_current_state_timers_running = 1;
2401 			}
2402 			break;
2403 
2404 		case IGMP_VERSION_3:
2405 			/*
2406 			 * Defer update of T0 to T1, until the first copy
2407 			 * of the state change has been transmitted.
2408 			 */
2409 			syncstates = 0;
2410 
2411 			/*
2412 			 * Immediately enqueue a State-Change Report for
2413 			 * this interface, freeing any previous reports.
2414 			 * Don't kick the timers if there is nothing to do,
2415 			 * or if an error occurred.
2416 			 */
2417 			ifq = &inm->inm_scq;
2418 			_IF_DRAIN(ifq);
2419 			retval = igmp_v3_enqueue_group_record(ifq, inm, 1,
2420 			    0, 0);
2421 			CTR2(KTR_IGMPV3, "%s: enqueue record = %d",
2422 			    __func__, retval);
2423 			if (retval <= 0) {
2424 				error = retval * -1;
2425 				break;
2426 			}
2427 
2428 			/*
2429 			 * Schedule transmission of pending state-change
2430 			 * report up to RV times for this link. The timer
2431 			 * will fire at the next igmp_fasttimo (~200ms),
2432 			 * giving us an opportunity to merge the reports.
2433 			 */
2434 			if (igi->igi_flags & IGIF_LOOPBACK) {
2435 				inm->inm_scrv = 1;
2436 			} else {
2437 				KASSERT(igi->igi_rv > 1,
2438 				   ("%s: invalid robustness %d", __func__,
2439 				    igi->igi_rv));
2440 				inm->inm_scrv = igi->igi_rv;
2441 			}
2442 			inm->inm_sctimer = 1;
2443 			V_state_change_timers_running = 1;
2444 
2445 			error = 0;
2446 			break;
2447 		}
2448 	}
2449 
2450 	/*
2451 	 * Only update the T0 state if state change is atomic,
2452 	 * i.e. we don't need to wait for a timer to fire before we
2453 	 * can consider the state change to have been communicated.
2454 	 */
2455 	if (syncstates) {
2456 		inm_commit(inm);
2457 		CTR3(KTR_IGMPV3, "%s: T1 -> T0 for %s/%s", __func__,
2458 		    inet_ntoa(inm->inm_addr), inm->inm_ifp->if_xname);
2459 	}
2460 
2461 	return (error);
2462 }
2463 
2464 /*
2465  * Issue an intermediate state change during the IGMP life-cycle.
2466  */
2467 static int
2468 igmp_handle_state_change(struct in_multi *inm, struct igmp_ifinfo *igi)
2469 {
2470 	struct ifnet		*ifp;
2471 	int			 retval;
2472 
2473 	CTR4(KTR_IGMPV3, "%s: state change for %s on ifp %p(%s)",
2474 	    __func__, inet_ntoa(inm->inm_addr), inm->inm_ifp,
2475 	    inm->inm_ifp->if_xname);
2476 
2477 	ifp = inm->inm_ifp;
2478 
2479 	IN_MULTI_LOCK_ASSERT();
2480 	IGMP_LOCK_ASSERT();
2481 
2482 	KASSERT(igi && igi->igi_ifp == ifp, ("%s: inconsistent ifp", __func__));
2483 
2484 	if ((ifp->if_flags & IFF_LOOPBACK) ||
2485 	    (igi->igi_flags & IGIF_SILENT) ||
2486 	    !igmp_isgroupreported(inm->inm_addr) ||
2487 	    (igi->igi_version != IGMP_VERSION_3)) {
2488 		if (!igmp_isgroupreported(inm->inm_addr)) {
2489 			CTR1(KTR_IGMPV3,
2490 "%s: not kicking state machine for silent group", __func__);
2491 		}
2492 		CTR1(KTR_IGMPV3, "%s: nothing to do", __func__);
2493 		inm_commit(inm);
2494 		CTR3(KTR_IGMPV3, "%s: T1 -> T0 for %s/%s", __func__,
2495 		    inet_ntoa(inm->inm_addr), inm->inm_ifp->if_xname);
2496 		return (0);
2497 	}
2498 
2499 	_IF_DRAIN(&inm->inm_scq);
2500 
2501 	retval = igmp_v3_enqueue_group_record(&inm->inm_scq, inm, 1, 0, 0);
2502 	CTR2(KTR_IGMPV3, "%s: enqueue record = %d", __func__, retval);
2503 	if (retval <= 0)
2504 		return (-retval);
2505 
2506 	/*
2507 	 * If record(s) were enqueued, start the state-change
2508 	 * report timer for this group.
2509 	 */
2510 	inm->inm_scrv = ((igi->igi_flags & IGIF_LOOPBACK) ? 1 : igi->igi_rv);
2511 	inm->inm_sctimer = 1;
2512 	V_state_change_timers_running = 1;
2513 
2514 	return (0);
2515 }
2516 
2517 /*
2518  * Perform the final leave for an IGMP group.
2519  *
2520  * When leaving a group:
2521  *  IGMPv1 does nothing.
2522  *  IGMPv2 sends a host leave message, if and only if we are the reporter.
2523  *  IGMPv3 enqueues a state-change report containing a transition
2524  *  to INCLUDE {} for immediate transmission.
2525  */
2526 static void
2527 igmp_final_leave(struct in_multi *inm, struct igmp_ifinfo *igi)
2528 {
2529 	int syncstates;
2530 
2531 	syncstates = 1;
2532 
2533 	CTR4(KTR_IGMPV3, "%s: final leave %s on ifp %p(%s)",
2534 	    __func__, inet_ntoa(inm->inm_addr), inm->inm_ifp,
2535 	    inm->inm_ifp->if_xname);
2536 
2537 	IN_MULTI_LOCK_ASSERT();
2538 	IGMP_LOCK_ASSERT();
2539 
2540 	switch (inm->inm_state) {
2541 	case IGMP_NOT_MEMBER:
2542 	case IGMP_SILENT_MEMBER:
2543 	case IGMP_LEAVING_MEMBER:
2544 		/* Already leaving or left; do nothing. */
2545 		CTR1(KTR_IGMPV3,
2546 "%s: not kicking state machine for silent group", __func__);
2547 		break;
2548 	case IGMP_REPORTING_MEMBER:
2549 	case IGMP_IDLE_MEMBER:
2550 	case IGMP_G_QUERY_PENDING_MEMBER:
2551 	case IGMP_SG_QUERY_PENDING_MEMBER:
2552 		if (igi->igi_version == IGMP_VERSION_2) {
2553 #ifdef INVARIANTS
2554 			if (inm->inm_state == IGMP_G_QUERY_PENDING_MEMBER ||
2555 			    inm->inm_state == IGMP_SG_QUERY_PENDING_MEMBER)
2556 			panic("%s: IGMPv3 state reached, not IGMPv3 mode",
2557 			     __func__);
2558 #endif
2559 			igmp_v1v2_queue_report(inm, IGMP_HOST_LEAVE_MESSAGE);
2560 			inm->inm_state = IGMP_NOT_MEMBER;
2561 		} else if (igi->igi_version == IGMP_VERSION_3) {
2562 			/*
2563 			 * Stop group timer and all pending reports.
2564 			 * Immediately enqueue a state-change report
2565 			 * TO_IN {} to be sent on the next fast timeout,
2566 			 * giving us an opportunity to merge reports.
2567 			 */
2568 			_IF_DRAIN(&inm->inm_scq);
2569 			inm->inm_timer = 0;
2570 			if (igi->igi_flags & IGIF_LOOPBACK) {
2571 				inm->inm_scrv = 1;
2572 			} else {
2573 				inm->inm_scrv = igi->igi_rv;
2574 			}
2575 			CTR4(KTR_IGMPV3, "%s: Leaving %s/%s with %d "
2576 			    "pending retransmissions.", __func__,
2577 			    inet_ntoa(inm->inm_addr),
2578 			    inm->inm_ifp->if_xname, inm->inm_scrv);
2579 			if (inm->inm_scrv == 0) {
2580 				inm->inm_state = IGMP_NOT_MEMBER;
2581 				inm->inm_sctimer = 0;
2582 			} else {
2583 				int retval;
2584 
2585 				inm_acquire_locked(inm);
2586 
2587 				retval = igmp_v3_enqueue_group_record(
2588 				    &inm->inm_scq, inm, 1, 0, 0);
2589 				KASSERT(retval != 0,
2590 				    ("%s: enqueue record = %d", __func__,
2591 				     retval));
2592 
2593 				inm->inm_state = IGMP_LEAVING_MEMBER;
2594 				inm->inm_sctimer = 1;
2595 				V_state_change_timers_running = 1;
2596 				syncstates = 0;
2597 			}
2598 			break;
2599 		}
2600 		break;
2601 	case IGMP_LAZY_MEMBER:
2602 	case IGMP_SLEEPING_MEMBER:
2603 	case IGMP_AWAKENING_MEMBER:
2604 		/* Our reports are suppressed; do nothing. */
2605 		break;
2606 	}
2607 
2608 	if (syncstates) {
2609 		inm_commit(inm);
2610 		CTR3(KTR_IGMPV3, "%s: T1 -> T0 for %s/%s", __func__,
2611 		    inet_ntoa(inm->inm_addr), inm->inm_ifp->if_xname);
2612 		inm->inm_st[1].iss_fmode = MCAST_UNDEFINED;
2613 		CTR3(KTR_IGMPV3, "%s: T1 now MCAST_UNDEFINED for %s/%s",
2614 		    __func__, inet_ntoa(inm->inm_addr), inm->inm_ifp->if_xname);
2615 	}
2616 }
2617 
2618 /*
2619  * Enqueue an IGMPv3 group record to the given output queue.
2620  *
2621  * XXX This function could do with having the allocation code
2622  * split out, and the multiple-tree-walks coalesced into a single
2623  * routine as has been done in igmp_v3_enqueue_filter_change().
2624  *
2625  * If is_state_change is zero, a current-state record is appended.
2626  * If is_state_change is non-zero, a state-change report is appended.
2627  *
2628  * If is_group_query is non-zero, an mbuf packet chain is allocated.
2629  * If is_group_query is zero, and if there is a packet with free space
2630  * at the tail of the queue, it will be appended to providing there
2631  * is enough free space.
2632  * Otherwise a new mbuf packet chain is allocated.
2633  *
2634  * If is_source_query is non-zero, each source is checked to see if
2635  * it was recorded for a Group-Source query, and will be omitted if
2636  * it is not both in-mode and recorded.
2637  *
2638  * The function will attempt to allocate leading space in the packet
2639  * for the IP/IGMP header to be prepended without fragmenting the chain.
2640  *
2641  * If successful the size of all data appended to the queue is returned,
2642  * otherwise an error code less than zero is returned, or zero if
2643  * no record(s) were appended.
2644  */
2645 static int
2646 igmp_v3_enqueue_group_record(struct ifqueue *ifq, struct in_multi *inm,
2647     const int is_state_change, const int is_group_query,
2648     const int is_source_query)
2649 {
2650 	struct igmp_grouprec	 ig;
2651 	struct igmp_grouprec	*pig;
2652 	struct ifnet		*ifp;
2653 	struct ip_msource	*ims, *nims;
2654 	struct mbuf		*m0, *m, *md;
2655 	int			 error, is_filter_list_change;
2656 	int			 minrec0len, m0srcs, msrcs, nbytes, off;
2657 	int			 record_has_sources;
2658 	int			 now;
2659 	int			 type;
2660 	in_addr_t		 naddr;
2661 	uint8_t			 mode;
2662 
2663 	IN_MULTI_LOCK_ASSERT();
2664 
2665 	error = 0;
2666 	ifp = inm->inm_ifp;
2667 	is_filter_list_change = 0;
2668 	m = NULL;
2669 	m0 = NULL;
2670 	m0srcs = 0;
2671 	msrcs = 0;
2672 	nbytes = 0;
2673 	nims = NULL;
2674 	record_has_sources = 1;
2675 	pig = NULL;
2676 	type = IGMP_DO_NOTHING;
2677 	mode = inm->inm_st[1].iss_fmode;
2678 
2679 	/*
2680 	 * If we did not transition out of ASM mode during t0->t1,
2681 	 * and there are no source nodes to process, we can skip
2682 	 * the generation of source records.
2683 	 */
2684 	if (inm->inm_st[0].iss_asm > 0 && inm->inm_st[1].iss_asm > 0 &&
2685 	    inm->inm_nsrc == 0)
2686 		record_has_sources = 0;
2687 
2688 	if (is_state_change) {
2689 		/*
2690 		 * Queue a state change record.
2691 		 * If the mode did not change, and there are non-ASM
2692 		 * listeners or source filters present,
2693 		 * we potentially need to issue two records for the group.
2694 		 * If we are transitioning to MCAST_UNDEFINED, we need
2695 		 * not send any sources.
2696 		 * If there are ASM listeners, and there was no filter
2697 		 * mode transition of any kind, do nothing.
2698 		 */
2699 		if (mode != inm->inm_st[0].iss_fmode) {
2700 			if (mode == MCAST_EXCLUDE) {
2701 				CTR1(KTR_IGMPV3, "%s: change to EXCLUDE",
2702 				    __func__);
2703 				type = IGMP_CHANGE_TO_EXCLUDE_MODE;
2704 			} else {
2705 				CTR1(KTR_IGMPV3, "%s: change to INCLUDE",
2706 				    __func__);
2707 				type = IGMP_CHANGE_TO_INCLUDE_MODE;
2708 				if (mode == MCAST_UNDEFINED)
2709 					record_has_sources = 0;
2710 			}
2711 		} else {
2712 			if (record_has_sources) {
2713 				is_filter_list_change = 1;
2714 			} else {
2715 				type = IGMP_DO_NOTHING;
2716 			}
2717 		}
2718 	} else {
2719 		/*
2720 		 * Queue a current state record.
2721 		 */
2722 		if (mode == MCAST_EXCLUDE) {
2723 			type = IGMP_MODE_IS_EXCLUDE;
2724 		} else if (mode == MCAST_INCLUDE) {
2725 			type = IGMP_MODE_IS_INCLUDE;
2726 			KASSERT(inm->inm_st[1].iss_asm == 0,
2727 			    ("%s: inm %p is INCLUDE but ASM count is %d",
2728 			     __func__, inm, inm->inm_st[1].iss_asm));
2729 		}
2730 	}
2731 
2732 	/*
2733 	 * Generate the filter list changes using a separate function.
2734 	 */
2735 	if (is_filter_list_change)
2736 		return (igmp_v3_enqueue_filter_change(ifq, inm));
2737 
2738 	if (type == IGMP_DO_NOTHING) {
2739 		CTR3(KTR_IGMPV3, "%s: nothing to do for %s/%s",
2740 		    __func__, inet_ntoa(inm->inm_addr),
2741 		    inm->inm_ifp->if_xname);
2742 		return (0);
2743 	}
2744 
2745 	/*
2746 	 * If any sources are present, we must be able to fit at least
2747 	 * one in the trailing space of the tail packet's mbuf,
2748 	 * ideally more.
2749 	 */
2750 	minrec0len = sizeof(struct igmp_grouprec);
2751 	if (record_has_sources)
2752 		minrec0len += sizeof(in_addr_t);
2753 
2754 	CTR4(KTR_IGMPV3, "%s: queueing %s for %s/%s", __func__,
2755 	    igmp_rec_type_to_str(type), inet_ntoa(inm->inm_addr),
2756 	    inm->inm_ifp->if_xname);
2757 
2758 	/*
2759 	 * Check if we have a packet in the tail of the queue for this
2760 	 * group into which the first group record for this group will fit.
2761 	 * Otherwise allocate a new packet.
2762 	 * Always allocate leading space for IP+RA_OPT+IGMP+REPORT.
2763 	 * Note: Group records for G/GSR query responses MUST be sent
2764 	 * in their own packet.
2765 	 */
2766 	m0 = ifq->ifq_tail;
2767 	if (!is_group_query &&
2768 	    m0 != NULL &&
2769 	    (m0->m_pkthdr.PH_vt.vt_nrecs + 1 <= IGMP_V3_REPORT_MAXRECS) &&
2770 	    (m0->m_pkthdr.len + minrec0len) <
2771 	     (ifp->if_mtu - IGMP_LEADINGSPACE)) {
2772 		m0srcs = (ifp->if_mtu - m0->m_pkthdr.len -
2773 			    sizeof(struct igmp_grouprec)) / sizeof(in_addr_t);
2774 		m = m0;
2775 		CTR1(KTR_IGMPV3, "%s: use existing packet", __func__);
2776 	} else {
2777 		if (_IF_QFULL(ifq)) {
2778 			CTR1(KTR_IGMPV3, "%s: outbound queue full", __func__);
2779 			return (-ENOMEM);
2780 		}
2781 		m = NULL;
2782 		m0srcs = (ifp->if_mtu - IGMP_LEADINGSPACE -
2783 		    sizeof(struct igmp_grouprec)) / sizeof(in_addr_t);
2784 		if (!is_state_change && !is_group_query) {
2785 			m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
2786 			if (m)
2787 				m->m_data += IGMP_LEADINGSPACE;
2788 		}
2789 		if (m == NULL) {
2790 			m = m_gethdr(M_NOWAIT, MT_DATA);
2791 			if (m)
2792 				MH_ALIGN(m, IGMP_LEADINGSPACE);
2793 		}
2794 		if (m == NULL)
2795 			return (-ENOMEM);
2796 
2797 		igmp_save_context(m, ifp);
2798 
2799 		CTR1(KTR_IGMPV3, "%s: allocated first packet", __func__);
2800 	}
2801 
2802 	/*
2803 	 * Append group record.
2804 	 * If we have sources, we don't know how many yet.
2805 	 */
2806 	ig.ig_type = type;
2807 	ig.ig_datalen = 0;
2808 	ig.ig_numsrc = 0;
2809 	ig.ig_group = inm->inm_addr;
2810 	if (!m_append(m, sizeof(struct igmp_grouprec), (void *)&ig)) {
2811 		if (m != m0)
2812 			m_freem(m);
2813 		CTR1(KTR_IGMPV3, "%s: m_append() failed.", __func__);
2814 		return (-ENOMEM);
2815 	}
2816 	nbytes += sizeof(struct igmp_grouprec);
2817 
2818 	/*
2819 	 * Append as many sources as will fit in the first packet.
2820 	 * If we are appending to a new packet, the chain allocation
2821 	 * may potentially use clusters; use m_getptr() in this case.
2822 	 * If we are appending to an existing packet, we need to obtain
2823 	 * a pointer to the group record after m_append(), in case a new
2824 	 * mbuf was allocated.
2825 	 * Only append sources which are in-mode at t1. If we are
2826 	 * transitioning to MCAST_UNDEFINED state on the group, do not
2827 	 * include source entries.
2828 	 * Only report recorded sources in our filter set when responding
2829 	 * to a group-source query.
2830 	 */
2831 	if (record_has_sources) {
2832 		if (m == m0) {
2833 			md = m_last(m);
2834 			pig = (struct igmp_grouprec *)(mtod(md, uint8_t *) +
2835 			    md->m_len - nbytes);
2836 		} else {
2837 			md = m_getptr(m, 0, &off);
2838 			pig = (struct igmp_grouprec *)(mtod(md, uint8_t *) +
2839 			    off);
2840 		}
2841 		msrcs = 0;
2842 		RB_FOREACH_SAFE(ims, ip_msource_tree, &inm->inm_srcs, nims) {
2843 			CTR2(KTR_IGMPV3, "%s: visit node %s", __func__,
2844 			    inet_ntoa_haddr(ims->ims_haddr));
2845 			now = ims_get_mode(inm, ims, 1);
2846 			CTR2(KTR_IGMPV3, "%s: node is %d", __func__, now);
2847 			if ((now != mode) ||
2848 			    (now == mode && mode == MCAST_UNDEFINED)) {
2849 				CTR1(KTR_IGMPV3, "%s: skip node", __func__);
2850 				continue;
2851 			}
2852 			if (is_source_query && ims->ims_stp == 0) {
2853 				CTR1(KTR_IGMPV3, "%s: skip unrecorded node",
2854 				    __func__);
2855 				continue;
2856 			}
2857 			CTR1(KTR_IGMPV3, "%s: append node", __func__);
2858 			naddr = htonl(ims->ims_haddr);
2859 			if (!m_append(m, sizeof(in_addr_t), (void *)&naddr)) {
2860 				if (m != m0)
2861 					m_freem(m);
2862 				CTR1(KTR_IGMPV3, "%s: m_append() failed.",
2863 				    __func__);
2864 				return (-ENOMEM);
2865 			}
2866 			nbytes += sizeof(in_addr_t);
2867 			++msrcs;
2868 			if (msrcs == m0srcs)
2869 				break;
2870 		}
2871 		CTR2(KTR_IGMPV3, "%s: msrcs is %d this packet", __func__,
2872 		    msrcs);
2873 		pig->ig_numsrc = htons(msrcs);
2874 		nbytes += (msrcs * sizeof(in_addr_t));
2875 	}
2876 
2877 	if (is_source_query && msrcs == 0) {
2878 		CTR1(KTR_IGMPV3, "%s: no recorded sources to report", __func__);
2879 		if (m != m0)
2880 			m_freem(m);
2881 		return (0);
2882 	}
2883 
2884 	/*
2885 	 * We are good to go with first packet.
2886 	 */
2887 	if (m != m0) {
2888 		CTR1(KTR_IGMPV3, "%s: enqueueing first packet", __func__);
2889 		m->m_pkthdr.PH_vt.vt_nrecs = 1;
2890 		_IF_ENQUEUE(ifq, m);
2891 	} else
2892 		m->m_pkthdr.PH_vt.vt_nrecs++;
2893 
2894 	/*
2895 	 * No further work needed if no source list in packet(s).
2896 	 */
2897 	if (!record_has_sources)
2898 		return (nbytes);
2899 
2900 	/*
2901 	 * Whilst sources remain to be announced, we need to allocate
2902 	 * a new packet and fill out as many sources as will fit.
2903 	 * Always try for a cluster first.
2904 	 */
2905 	while (nims != NULL) {
2906 		if (_IF_QFULL(ifq)) {
2907 			CTR1(KTR_IGMPV3, "%s: outbound queue full", __func__);
2908 			return (-ENOMEM);
2909 		}
2910 		m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
2911 		if (m)
2912 			m->m_data += IGMP_LEADINGSPACE;
2913 		if (m == NULL) {
2914 			m = m_gethdr(M_NOWAIT, MT_DATA);
2915 			if (m)
2916 				MH_ALIGN(m, IGMP_LEADINGSPACE);
2917 		}
2918 		if (m == NULL)
2919 			return (-ENOMEM);
2920 		igmp_save_context(m, ifp);
2921 		md = m_getptr(m, 0, &off);
2922 		pig = (struct igmp_grouprec *)(mtod(md, uint8_t *) + off);
2923 		CTR1(KTR_IGMPV3, "%s: allocated next packet", __func__);
2924 
2925 		if (!m_append(m, sizeof(struct igmp_grouprec), (void *)&ig)) {
2926 			if (m != m0)
2927 				m_freem(m);
2928 			CTR1(KTR_IGMPV3, "%s: m_append() failed.", __func__);
2929 			return (-ENOMEM);
2930 		}
2931 		m->m_pkthdr.PH_vt.vt_nrecs = 1;
2932 		nbytes += sizeof(struct igmp_grouprec);
2933 
2934 		m0srcs = (ifp->if_mtu - IGMP_LEADINGSPACE -
2935 		    sizeof(struct igmp_grouprec)) / sizeof(in_addr_t);
2936 
2937 		msrcs = 0;
2938 		RB_FOREACH_FROM(ims, ip_msource_tree, nims) {
2939 			CTR2(KTR_IGMPV3, "%s: visit node %s", __func__,
2940 			    inet_ntoa_haddr(ims->ims_haddr));
2941 			now = ims_get_mode(inm, ims, 1);
2942 			if ((now != mode) ||
2943 			    (now == mode && mode == MCAST_UNDEFINED)) {
2944 				CTR1(KTR_IGMPV3, "%s: skip node", __func__);
2945 				continue;
2946 			}
2947 			if (is_source_query && ims->ims_stp == 0) {
2948 				CTR1(KTR_IGMPV3, "%s: skip unrecorded node",
2949 				    __func__);
2950 				continue;
2951 			}
2952 			CTR1(KTR_IGMPV3, "%s: append node", __func__);
2953 			naddr = htonl(ims->ims_haddr);
2954 			if (!m_append(m, sizeof(in_addr_t), (void *)&naddr)) {
2955 				if (m != m0)
2956 					m_freem(m);
2957 				CTR1(KTR_IGMPV3, "%s: m_append() failed.",
2958 				    __func__);
2959 				return (-ENOMEM);
2960 			}
2961 			++msrcs;
2962 			if (msrcs == m0srcs)
2963 				break;
2964 		}
2965 		pig->ig_numsrc = htons(msrcs);
2966 		nbytes += (msrcs * sizeof(in_addr_t));
2967 
2968 		CTR1(KTR_IGMPV3, "%s: enqueueing next packet", __func__);
2969 		_IF_ENQUEUE(ifq, m);
2970 	}
2971 
2972 	return (nbytes);
2973 }
2974 
2975 /*
2976  * Type used to mark record pass completion.
2977  * We exploit the fact we can cast to this easily from the
2978  * current filter modes on each ip_msource node.
2979  */
2980 typedef enum {
2981 	REC_NONE = 0x00,	/* MCAST_UNDEFINED */
2982 	REC_ALLOW = 0x01,	/* MCAST_INCLUDE */
2983 	REC_BLOCK = 0x02,	/* MCAST_EXCLUDE */
2984 	REC_FULL = REC_ALLOW | REC_BLOCK
2985 } rectype_t;
2986 
2987 /*
2988  * Enqueue an IGMPv3 filter list change to the given output queue.
2989  *
2990  * Source list filter state is held in an RB-tree. When the filter list
2991  * for a group is changed without changing its mode, we need to compute
2992  * the deltas between T0 and T1 for each source in the filter set,
2993  * and enqueue the appropriate ALLOW_NEW/BLOCK_OLD records.
2994  *
2995  * As we may potentially queue two record types, and the entire R-B tree
2996  * needs to be walked at once, we break this out into its own function
2997  * so we can generate a tightly packed queue of packets.
2998  *
2999  * XXX This could be written to only use one tree walk, although that makes
3000  * serializing into the mbuf chains a bit harder. For now we do two walks
3001  * which makes things easier on us, and it may or may not be harder on
3002  * the L2 cache.
3003  *
3004  * If successful the size of all data appended to the queue is returned,
3005  * otherwise an error code less than zero is returned, or zero if
3006  * no record(s) were appended.
3007  */
3008 static int
3009 igmp_v3_enqueue_filter_change(struct ifqueue *ifq, struct in_multi *inm)
3010 {
3011 	static const int MINRECLEN =
3012 	    sizeof(struct igmp_grouprec) + sizeof(in_addr_t);
3013 	struct ifnet		*ifp;
3014 	struct igmp_grouprec	 ig;
3015 	struct igmp_grouprec	*pig;
3016 	struct ip_msource	*ims, *nims;
3017 	struct mbuf		*m, *m0, *md;
3018 	in_addr_t		 naddr;
3019 	int			 m0srcs, nbytes, npbytes, off, rsrcs, schanged;
3020 	int			 nallow, nblock;
3021 	uint8_t			 mode, now, then;
3022 	rectype_t		 crt, drt, nrt;
3023 
3024 	IN_MULTI_LOCK_ASSERT();
3025 
3026 	if (inm->inm_nsrc == 0 ||
3027 	    (inm->inm_st[0].iss_asm > 0 && inm->inm_st[1].iss_asm > 0))
3028 		return (0);
3029 
3030 	ifp = inm->inm_ifp;			/* interface */
3031 	mode = inm->inm_st[1].iss_fmode;	/* filter mode at t1 */
3032 	crt = REC_NONE;	/* current group record type */
3033 	drt = REC_NONE;	/* mask of completed group record types */
3034 	nrt = REC_NONE;	/* record type for current node */
3035 	m0srcs = 0;	/* # source which will fit in current mbuf chain */
3036 	nbytes = 0;	/* # of bytes appended to group's state-change queue */
3037 	npbytes = 0;	/* # of bytes appended this packet */
3038 	rsrcs = 0;	/* # sources encoded in current record */
3039 	schanged = 0;	/* # nodes encoded in overall filter change */
3040 	nallow = 0;	/* # of source entries in ALLOW_NEW */
3041 	nblock = 0;	/* # of source entries in BLOCK_OLD */
3042 	nims = NULL;	/* next tree node pointer */
3043 
3044 	/*
3045 	 * For each possible filter record mode.
3046 	 * The first kind of source we encounter tells us which
3047 	 * is the first kind of record we start appending.
3048 	 * If a node transitioned to UNDEFINED at t1, its mode is treated
3049 	 * as the inverse of the group's filter mode.
3050 	 */
3051 	while (drt != REC_FULL) {
3052 		do {
3053 			m0 = ifq->ifq_tail;
3054 			if (m0 != NULL &&
3055 			    (m0->m_pkthdr.PH_vt.vt_nrecs + 1 <=
3056 			     IGMP_V3_REPORT_MAXRECS) &&
3057 			    (m0->m_pkthdr.len + MINRECLEN) <
3058 			     (ifp->if_mtu - IGMP_LEADINGSPACE)) {
3059 				m = m0;
3060 				m0srcs = (ifp->if_mtu - m0->m_pkthdr.len -
3061 					    sizeof(struct igmp_grouprec)) /
3062 				    sizeof(in_addr_t);
3063 				CTR1(KTR_IGMPV3,
3064 				    "%s: use previous packet", __func__);
3065 			} else {
3066 				m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
3067 				if (m)
3068 					m->m_data += IGMP_LEADINGSPACE;
3069 				if (m == NULL) {
3070 					m = m_gethdr(M_NOWAIT, MT_DATA);
3071 					if (m)
3072 						MH_ALIGN(m, IGMP_LEADINGSPACE);
3073 				}
3074 				if (m == NULL) {
3075 					CTR1(KTR_IGMPV3,
3076 					    "%s: m_get*() failed", __func__);
3077 					return (-ENOMEM);
3078 				}
3079 				m->m_pkthdr.PH_vt.vt_nrecs = 0;
3080 				igmp_save_context(m, ifp);
3081 				m0srcs = (ifp->if_mtu - IGMP_LEADINGSPACE -
3082 				    sizeof(struct igmp_grouprec)) /
3083 				    sizeof(in_addr_t);
3084 				npbytes = 0;
3085 				CTR1(KTR_IGMPV3,
3086 				    "%s: allocated new packet", __func__);
3087 			}
3088 			/*
3089 			 * Append the IGMP group record header to the
3090 			 * current packet's data area.
3091 			 * Recalculate pointer to free space for next
3092 			 * group record, in case m_append() allocated
3093 			 * a new mbuf or cluster.
3094 			 */
3095 			memset(&ig, 0, sizeof(ig));
3096 			ig.ig_group = inm->inm_addr;
3097 			if (!m_append(m, sizeof(ig), (void *)&ig)) {
3098 				if (m != m0)
3099 					m_freem(m);
3100 				CTR1(KTR_IGMPV3,
3101 				    "%s: m_append() failed", __func__);
3102 				return (-ENOMEM);
3103 			}
3104 			npbytes += sizeof(struct igmp_grouprec);
3105 			if (m != m0) {
3106 				/* new packet; offset in c hain */
3107 				md = m_getptr(m, npbytes -
3108 				    sizeof(struct igmp_grouprec), &off);
3109 				pig = (struct igmp_grouprec *)(mtod(md,
3110 				    uint8_t *) + off);
3111 			} else {
3112 				/* current packet; offset from last append */
3113 				md = m_last(m);
3114 				pig = (struct igmp_grouprec *)(mtod(md,
3115 				    uint8_t *) + md->m_len -
3116 				    sizeof(struct igmp_grouprec));
3117 			}
3118 			/*
3119 			 * Begin walking the tree for this record type
3120 			 * pass, or continue from where we left off
3121 			 * previously if we had to allocate a new packet.
3122 			 * Only report deltas in-mode at t1.
3123 			 * We need not report included sources as allowed
3124 			 * if we are in inclusive mode on the group,
3125 			 * however the converse is not true.
3126 			 */
3127 			rsrcs = 0;
3128 			if (nims == NULL)
3129 				nims = RB_MIN(ip_msource_tree, &inm->inm_srcs);
3130 			RB_FOREACH_FROM(ims, ip_msource_tree, nims) {
3131 				CTR2(KTR_IGMPV3, "%s: visit node %s",
3132 				    __func__, inet_ntoa_haddr(ims->ims_haddr));
3133 				now = ims_get_mode(inm, ims, 1);
3134 				then = ims_get_mode(inm, ims, 0);
3135 				CTR3(KTR_IGMPV3, "%s: mode: t0 %d, t1 %d",
3136 				    __func__, then, now);
3137 				if (now == then) {
3138 					CTR1(KTR_IGMPV3,
3139 					    "%s: skip unchanged", __func__);
3140 					continue;
3141 				}
3142 				if (mode == MCAST_EXCLUDE &&
3143 				    now == MCAST_INCLUDE) {
3144 					CTR1(KTR_IGMPV3,
3145 					    "%s: skip IN src on EX group",
3146 					    __func__);
3147 					continue;
3148 				}
3149 				nrt = (rectype_t)now;
3150 				if (nrt == REC_NONE)
3151 					nrt = (rectype_t)(~mode & REC_FULL);
3152 				if (schanged++ == 0) {
3153 					crt = nrt;
3154 				} else if (crt != nrt)
3155 					continue;
3156 				naddr = htonl(ims->ims_haddr);
3157 				if (!m_append(m, sizeof(in_addr_t),
3158 				    (void *)&naddr)) {
3159 					if (m != m0)
3160 						m_freem(m);
3161 					CTR1(KTR_IGMPV3,
3162 					    "%s: m_append() failed", __func__);
3163 					return (-ENOMEM);
3164 				}
3165 				nallow += !!(crt == REC_ALLOW);
3166 				nblock += !!(crt == REC_BLOCK);
3167 				if (++rsrcs == m0srcs)
3168 					break;
3169 			}
3170 			/*
3171 			 * If we did not append any tree nodes on this
3172 			 * pass, back out of allocations.
3173 			 */
3174 			if (rsrcs == 0) {
3175 				npbytes -= sizeof(struct igmp_grouprec);
3176 				if (m != m0) {
3177 					CTR1(KTR_IGMPV3,
3178 					    "%s: m_free(m)", __func__);
3179 					m_freem(m);
3180 				} else {
3181 					CTR1(KTR_IGMPV3,
3182 					    "%s: m_adj(m, -ig)", __func__);
3183 					m_adj(m, -((int)sizeof(
3184 					    struct igmp_grouprec)));
3185 				}
3186 				continue;
3187 			}
3188 			npbytes += (rsrcs * sizeof(in_addr_t));
3189 			if (crt == REC_ALLOW)
3190 				pig->ig_type = IGMP_ALLOW_NEW_SOURCES;
3191 			else if (crt == REC_BLOCK)
3192 				pig->ig_type = IGMP_BLOCK_OLD_SOURCES;
3193 			pig->ig_numsrc = htons(rsrcs);
3194 			/*
3195 			 * Count the new group record, and enqueue this
3196 			 * packet if it wasn't already queued.
3197 			 */
3198 			m->m_pkthdr.PH_vt.vt_nrecs++;
3199 			if (m != m0)
3200 				_IF_ENQUEUE(ifq, m);
3201 			nbytes += npbytes;
3202 		} while (nims != NULL);
3203 		drt |= crt;
3204 		crt = (~crt & REC_FULL);
3205 	}
3206 
3207 	CTR3(KTR_IGMPV3, "%s: queued %d ALLOW_NEW, %d BLOCK_OLD", __func__,
3208 	    nallow, nblock);
3209 
3210 	return (nbytes);
3211 }
3212 
3213 static int
3214 igmp_v3_merge_state_changes(struct in_multi *inm, struct ifqueue *ifscq)
3215 {
3216 	struct ifqueue	*gq;
3217 	struct mbuf	*m;		/* pending state-change */
3218 	struct mbuf	*m0;		/* copy of pending state-change */
3219 	struct mbuf	*mt;		/* last state-change in packet */
3220 	int		 docopy, domerge;
3221 	u_int		 recslen;
3222 
3223 	docopy = 0;
3224 	domerge = 0;
3225 	recslen = 0;
3226 
3227 	IN_MULTI_LOCK_ASSERT();
3228 	IGMP_LOCK_ASSERT();
3229 
3230 	/*
3231 	 * If there are further pending retransmissions, make a writable
3232 	 * copy of each queued state-change message before merging.
3233 	 */
3234 	if (inm->inm_scrv > 0)
3235 		docopy = 1;
3236 
3237 	gq = &inm->inm_scq;
3238 #ifdef KTR
3239 	if (gq->ifq_head == NULL) {
3240 		CTR2(KTR_IGMPV3, "%s: WARNING: queue for inm %p is empty",
3241 		    __func__, inm);
3242 	}
3243 #endif
3244 
3245 	m = gq->ifq_head;
3246 	while (m != NULL) {
3247 		/*
3248 		 * Only merge the report into the current packet if
3249 		 * there is sufficient space to do so; an IGMPv3 report
3250 		 * packet may only contain 65,535 group records.
3251 		 * Always use a simple mbuf chain concatentation to do this,
3252 		 * as large state changes for single groups may have
3253 		 * allocated clusters.
3254 		 */
3255 		domerge = 0;
3256 		mt = ifscq->ifq_tail;
3257 		if (mt != NULL) {
3258 			recslen = m_length(m, NULL);
3259 
3260 			if ((mt->m_pkthdr.PH_vt.vt_nrecs +
3261 			    m->m_pkthdr.PH_vt.vt_nrecs <=
3262 			    IGMP_V3_REPORT_MAXRECS) &&
3263 			    (mt->m_pkthdr.len + recslen <=
3264 			    (inm->inm_ifp->if_mtu - IGMP_LEADINGSPACE)))
3265 				domerge = 1;
3266 		}
3267 
3268 		if (!domerge && _IF_QFULL(gq)) {
3269 			CTR2(KTR_IGMPV3,
3270 			    "%s: outbound queue full, skipping whole packet %p",
3271 			    __func__, m);
3272 			mt = m->m_nextpkt;
3273 			if (!docopy)
3274 				m_freem(m);
3275 			m = mt;
3276 			continue;
3277 		}
3278 
3279 		if (!docopy) {
3280 			CTR2(KTR_IGMPV3, "%s: dequeueing %p", __func__, m);
3281 			_IF_DEQUEUE(gq, m0);
3282 			m = m0->m_nextpkt;
3283 		} else {
3284 			CTR2(KTR_IGMPV3, "%s: copying %p", __func__, m);
3285 			m0 = m_dup(m, M_NOWAIT);
3286 			if (m0 == NULL)
3287 				return (ENOMEM);
3288 			m0->m_nextpkt = NULL;
3289 			m = m->m_nextpkt;
3290 		}
3291 
3292 		if (!domerge) {
3293 			CTR3(KTR_IGMPV3, "%s: queueing %p to ifscq %p)",
3294 			    __func__, m0, ifscq);
3295 			_IF_ENQUEUE(ifscq, m0);
3296 		} else {
3297 			struct mbuf *mtl;	/* last mbuf of packet mt */
3298 
3299 			CTR3(KTR_IGMPV3, "%s: merging %p with ifscq tail %p)",
3300 			    __func__, m0, mt);
3301 
3302 			mtl = m_last(mt);
3303 			m0->m_flags &= ~M_PKTHDR;
3304 			mt->m_pkthdr.len += recslen;
3305 			mt->m_pkthdr.PH_vt.vt_nrecs +=
3306 			    m0->m_pkthdr.PH_vt.vt_nrecs;
3307 
3308 			mtl->m_next = m0;
3309 		}
3310 	}
3311 
3312 	return (0);
3313 }
3314 
3315 /*
3316  * Respond to a pending IGMPv3 General Query.
3317  */
3318 static void
3319 igmp_v3_dispatch_general_query(struct igmp_ifinfo *igi)
3320 {
3321 	struct ifmultiaddr	*ifma;
3322 	struct ifnet		*ifp;
3323 	struct in_multi		*inm;
3324 	int			 retval, loop;
3325 
3326 	IN_MULTI_LOCK_ASSERT();
3327 	IGMP_LOCK_ASSERT();
3328 
3329 	KASSERT(igi->igi_version == IGMP_VERSION_3,
3330 	    ("%s: called when version %d", __func__, igi->igi_version));
3331 
3332 	ifp = igi->igi_ifp;
3333 
3334 	IF_ADDR_RLOCK(ifp);
3335 	TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
3336 		if (ifma->ifma_addr->sa_family != AF_INET ||
3337 		    ifma->ifma_protospec == NULL)
3338 			continue;
3339 
3340 		inm = (struct in_multi *)ifma->ifma_protospec;
3341 		KASSERT(ifp == inm->inm_ifp,
3342 		    ("%s: inconsistent ifp", __func__));
3343 
3344 		switch (inm->inm_state) {
3345 		case IGMP_NOT_MEMBER:
3346 		case IGMP_SILENT_MEMBER:
3347 			break;
3348 		case IGMP_REPORTING_MEMBER:
3349 		case IGMP_IDLE_MEMBER:
3350 		case IGMP_LAZY_MEMBER:
3351 		case IGMP_SLEEPING_MEMBER:
3352 		case IGMP_AWAKENING_MEMBER:
3353 			inm->inm_state = IGMP_REPORTING_MEMBER;
3354 			retval = igmp_v3_enqueue_group_record(&igi->igi_gq,
3355 			    inm, 0, 0, 0);
3356 			CTR2(KTR_IGMPV3, "%s: enqueue record = %d",
3357 			    __func__, retval);
3358 			break;
3359 		case IGMP_G_QUERY_PENDING_MEMBER:
3360 		case IGMP_SG_QUERY_PENDING_MEMBER:
3361 		case IGMP_LEAVING_MEMBER:
3362 			break;
3363 		}
3364 	}
3365 	IF_ADDR_RUNLOCK(ifp);
3366 
3367 	loop = (igi->igi_flags & IGIF_LOOPBACK) ? 1 : 0;
3368 	igmp_dispatch_queue(&igi->igi_gq, IGMP_MAX_RESPONSE_BURST, loop);
3369 
3370 	/*
3371 	 * Slew transmission of bursts over 500ms intervals.
3372 	 */
3373 	if (igi->igi_gq.ifq_head != NULL) {
3374 		igi->igi_v3_timer = 1 + IGMP_RANDOM_DELAY(
3375 		    IGMP_RESPONSE_BURST_INTERVAL);
3376 		V_interface_timers_running = 1;
3377 	}
3378 }
3379 
3380 /*
3381  * Transmit the next pending IGMP message in the output queue.
3382  *
3383  * We get called from netisr_processqueue(). A mutex private to igmpoq
3384  * will be acquired and released around this routine.
3385  *
3386  * VIMAGE: Needs to store/restore vnet pointer on a per-mbuf-chain basis.
3387  * MRT: Nothing needs to be done, as IGMP traffic is always local to
3388  * a link and uses a link-scope multicast address.
3389  */
3390 static void
3391 igmp_intr(struct mbuf *m)
3392 {
3393 	struct ip_moptions	 imo;
3394 	struct ifnet		*ifp;
3395 	struct mbuf		*ipopts, *m0;
3396 	int			 error;
3397 	uint32_t		 ifindex;
3398 
3399 	CTR2(KTR_IGMPV3, "%s: transmit %p", __func__, m);
3400 
3401 	/*
3402 	 * Set VNET image pointer from enqueued mbuf chain
3403 	 * before doing anything else. Whilst we use interface
3404 	 * indexes to guard against interface detach, they are
3405 	 * unique to each VIMAGE and must be retrieved.
3406 	 */
3407 	CURVNET_SET((struct vnet *)(m->m_pkthdr.PH_loc.ptr));
3408 	ifindex = igmp_restore_context(m);
3409 
3410 	/*
3411 	 * Check if the ifnet still exists. This limits the scope of
3412 	 * any race in the absence of a global ifp lock for low cost
3413 	 * (an array lookup).
3414 	 */
3415 	ifp = ifnet_byindex(ifindex);
3416 	if (ifp == NULL) {
3417 		CTR3(KTR_IGMPV3, "%s: dropped %p as ifindex %u went away.",
3418 		    __func__, m, ifindex);
3419 		m_freem(m);
3420 		IPSTAT_INC(ips_noroute);
3421 		goto out;
3422 	}
3423 
3424 	ipopts = V_igmp_sendra ? m_raopt : NULL;
3425 
3426 	imo.imo_multicast_ttl  = 1;
3427 	imo.imo_multicast_vif  = -1;
3428 	imo.imo_multicast_loop = (V_ip_mrouter != NULL);
3429 
3430 	/*
3431 	 * If the user requested that IGMP traffic be explicitly
3432 	 * redirected to the loopback interface (e.g. they are running a
3433 	 * MANET interface and the routing protocol needs to see the
3434 	 * updates), handle this now.
3435 	 */
3436 	if (m->m_flags & M_IGMP_LOOP)
3437 		imo.imo_multicast_ifp = V_loif;
3438 	else
3439 		imo.imo_multicast_ifp = ifp;
3440 
3441 	if (m->m_flags & M_IGMPV2) {
3442 		m0 = m;
3443 	} else {
3444 		m0 = igmp_v3_encap_report(ifp, m);
3445 		if (m0 == NULL) {
3446 			CTR2(KTR_IGMPV3, "%s: dropped %p", __func__, m);
3447 			m_freem(m);
3448 			IPSTAT_INC(ips_odropped);
3449 			goto out;
3450 		}
3451 	}
3452 
3453 	igmp_scrub_context(m0);
3454 	m_clrprotoflags(m);
3455 	m0->m_pkthdr.rcvif = V_loif;
3456 #ifdef MAC
3457 	mac_netinet_igmp_send(ifp, m0);
3458 #endif
3459 	error = ip_output(m0, ipopts, NULL, 0, &imo, NULL);
3460 	if (error) {
3461 		CTR3(KTR_IGMPV3, "%s: ip_output(%p) = %d", __func__, m0, error);
3462 		goto out;
3463 	}
3464 
3465 	IGMPSTAT_INC(igps_snd_reports);
3466 
3467 out:
3468 	/*
3469 	 * We must restore the existing vnet pointer before
3470 	 * continuing as we are run from netisr context.
3471 	 */
3472 	CURVNET_RESTORE();
3473 }
3474 
3475 /*
3476  * Encapsulate an IGMPv3 report.
3477  *
3478  * The internal mbuf flag M_IGMPV3_HDR is used to indicate that the mbuf
3479  * chain has already had its IP/IGMPv3 header prepended. In this case
3480  * the function will not attempt to prepend; the lengths and checksums
3481  * will however be re-computed.
3482  *
3483  * Returns a pointer to the new mbuf chain head, or NULL if the
3484  * allocation failed.
3485  */
3486 static struct mbuf *
3487 igmp_v3_encap_report(struct ifnet *ifp, struct mbuf *m)
3488 {
3489 	struct igmp_report	*igmp;
3490 	struct ip		*ip;
3491 	int			 hdrlen, igmpreclen;
3492 
3493 	KASSERT((m->m_flags & M_PKTHDR),
3494 	    ("%s: mbuf chain %p is !M_PKTHDR", __func__, m));
3495 
3496 	igmpreclen = m_length(m, NULL);
3497 	hdrlen = sizeof(struct ip) + sizeof(struct igmp_report);
3498 
3499 	if (m->m_flags & M_IGMPV3_HDR) {
3500 		igmpreclen -= hdrlen;
3501 	} else {
3502 		M_PREPEND(m, hdrlen, M_NOWAIT);
3503 		if (m == NULL)
3504 			return (NULL);
3505 		m->m_flags |= M_IGMPV3_HDR;
3506 	}
3507 
3508 	CTR2(KTR_IGMPV3, "%s: igmpreclen is %d", __func__, igmpreclen);
3509 
3510 	m->m_data += sizeof(struct ip);
3511 	m->m_len -= sizeof(struct ip);
3512 
3513 	igmp = mtod(m, struct igmp_report *);
3514 	igmp->ir_type = IGMP_v3_HOST_MEMBERSHIP_REPORT;
3515 	igmp->ir_rsv1 = 0;
3516 	igmp->ir_rsv2 = 0;
3517 	igmp->ir_numgrps = htons(m->m_pkthdr.PH_vt.vt_nrecs);
3518 	igmp->ir_cksum = 0;
3519 	igmp->ir_cksum = in_cksum(m, sizeof(struct igmp_report) + igmpreclen);
3520 	m->m_pkthdr.PH_vt.vt_nrecs = 0;
3521 
3522 	m->m_data -= sizeof(struct ip);
3523 	m->m_len += sizeof(struct ip);
3524 
3525 	ip = mtod(m, struct ip *);
3526 	ip->ip_tos = IPTOS_PREC_INTERNETCONTROL;
3527 	ip->ip_len = htons(hdrlen + igmpreclen);
3528 	ip->ip_off = htons(IP_DF);
3529 	ip->ip_p = IPPROTO_IGMP;
3530 	ip->ip_sum = 0;
3531 
3532 	ip->ip_src.s_addr = INADDR_ANY;
3533 
3534 	if (m->m_flags & M_IGMP_LOOP) {
3535 		struct in_ifaddr *ia;
3536 
3537 		IFP_TO_IA(ifp, ia);
3538 		if (ia != NULL) {
3539 			ip->ip_src = ia->ia_addr.sin_addr;
3540 			ifa_free(&ia->ia_ifa);
3541 		}
3542 	}
3543 
3544 	ip->ip_dst.s_addr = htonl(INADDR_ALLRPTS_GROUP);
3545 
3546 	return (m);
3547 }
3548 
3549 #ifdef KTR
3550 static char *
3551 igmp_rec_type_to_str(const int type)
3552 {
3553 
3554 	switch (type) {
3555 		case IGMP_CHANGE_TO_EXCLUDE_MODE:
3556 			return "TO_EX";
3557 			break;
3558 		case IGMP_CHANGE_TO_INCLUDE_MODE:
3559 			return "TO_IN";
3560 			break;
3561 		case IGMP_MODE_IS_EXCLUDE:
3562 			return "MODE_EX";
3563 			break;
3564 		case IGMP_MODE_IS_INCLUDE:
3565 			return "MODE_IN";
3566 			break;
3567 		case IGMP_ALLOW_NEW_SOURCES:
3568 			return "ALLOW_NEW";
3569 			break;
3570 		case IGMP_BLOCK_OLD_SOURCES:
3571 			return "BLOCK_OLD";
3572 			break;
3573 		default:
3574 			break;
3575 	}
3576 	return "unknown";
3577 }
3578 #endif
3579 
3580 static void
3581 igmp_init(void *unused __unused)
3582 {
3583 
3584 	CTR1(KTR_IGMPV3, "%s: initializing", __func__);
3585 
3586 	IGMP_LOCK_INIT();
3587 
3588 	m_raopt = igmp_ra_alloc();
3589 
3590 	netisr_register(&igmp_nh);
3591 }
3592 SYSINIT(igmp_init, SI_SUB_PSEUDO, SI_ORDER_MIDDLE, igmp_init, NULL);
3593 
3594 static void
3595 igmp_uninit(void *unused __unused)
3596 {
3597 
3598 	CTR1(KTR_IGMPV3, "%s: tearing down", __func__);
3599 
3600 	netisr_unregister(&igmp_nh);
3601 
3602 	m_free(m_raopt);
3603 	m_raopt = NULL;
3604 
3605 	IGMP_LOCK_DESTROY();
3606 }
3607 SYSUNINIT(igmp_uninit, SI_SUB_PSEUDO, SI_ORDER_MIDDLE, igmp_uninit, NULL);
3608 
3609 static void
3610 vnet_igmp_init(const void *unused __unused)
3611 {
3612 
3613 	CTR1(KTR_IGMPV3, "%s: initializing", __func__);
3614 
3615 	LIST_INIT(&V_igi_head);
3616 }
3617 VNET_SYSINIT(vnet_igmp_init, SI_SUB_PSEUDO, SI_ORDER_ANY, vnet_igmp_init,
3618     NULL);
3619 
3620 static void
3621 vnet_igmp_uninit(const void *unused __unused)
3622 {
3623 
3624 	CTR1(KTR_IGMPV3, "%s: tearing down", __func__);
3625 
3626 	KASSERT(LIST_EMPTY(&V_igi_head),
3627 	    ("%s: igi list not empty; ifnets not detached?", __func__));
3628 }
3629 VNET_SYSUNINIT(vnet_igmp_uninit, SI_SUB_PSEUDO, SI_ORDER_ANY,
3630     vnet_igmp_uninit, NULL);
3631 
3632 static int
3633 igmp_modevent(module_t mod, int type, void *unused __unused)
3634 {
3635 
3636     switch (type) {
3637     case MOD_LOAD:
3638     case MOD_UNLOAD:
3639 	break;
3640     default:
3641 	return (EOPNOTSUPP);
3642     }
3643     return (0);
3644 }
3645 
3646 static moduledata_t igmp_mod = {
3647     "igmp",
3648     igmp_modevent,
3649     0
3650 };
3651 DECLARE_MODULE(igmp, igmp_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
3652