xref: /freebsd/sys/netinet/igmp.c (revision 195ebc7e9e4b129de810833791a19dfb4349d6a9)
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 "opt_mac.h"
54 #include "opt_route.h"
55 
56 #include <sys/param.h>
57 #include <sys/systm.h>
58 #include <sys/module.h>
59 #include <sys/malloc.h>
60 #include <sys/mbuf.h>
61 #include <sys/socket.h>
62 #include <sys/protosw.h>
63 #include <sys/kernel.h>
64 #include <sys/sysctl.h>
65 #include <sys/vimage.h>
66 #include <sys/ktr.h>
67 #include <sys/condvar.h>
68 
69 #include <net/if.h>
70 #include <net/netisr.h>
71 #include <net/route.h>
72 #include <net/vnet.h>
73 
74 #include <netinet/in.h>
75 #include <netinet/in_var.h>
76 #include <netinet/in_systm.h>
77 #include <netinet/ip.h>
78 #include <netinet/ip_var.h>
79 #include <netinet/ip_options.h>
80 #include <netinet/igmp.h>
81 #include <netinet/igmp_var.h>
82 #include <netinet/vinet.h>
83 
84 #include <machine/in_cksum.h>
85 
86 #include <security/mac/mac_framework.h>
87 
88 #ifndef KTR_IGMPV3
89 #define KTR_IGMPV3 KTR_INET
90 #endif
91 
92 static struct igmp_ifinfo *
93 		igi_alloc_locked(struct ifnet *);
94 static void	igi_delete_locked(const struct ifnet *);
95 static void	igmp_dispatch_queue(struct ifqueue *, int, const int);
96 static void	igmp_fasttimo_vnet(void);
97 static void	igmp_final_leave(struct in_multi *, struct igmp_ifinfo *);
98 static int	igmp_handle_state_change(struct in_multi *,
99 		    struct igmp_ifinfo *);
100 static int	igmp_initial_join(struct in_multi *, struct igmp_ifinfo *);
101 static int	igmp_input_v1_query(struct ifnet *, const struct ip *,
102 		    const struct igmp *);
103 static int	igmp_input_v2_query(struct ifnet *, const struct ip *,
104 		    const struct igmp *);
105 static int	igmp_input_v3_query(struct ifnet *, const struct ip *,
106 		    /*const*/ struct igmpv3 *);
107 static int	igmp_input_v3_group_query(struct in_multi *,
108 		    struct igmp_ifinfo *, int, /*const*/ struct igmpv3 *);
109 static int	igmp_input_v1_report(struct ifnet *, /*const*/ struct ip *,
110 		    /*const*/ struct igmp *);
111 static int	igmp_input_v2_report(struct ifnet *, /*const*/ struct ip *,
112 		    /*const*/ struct igmp *);
113 static void	igmp_intr(struct mbuf *);
114 static int	igmp_isgroupreported(const struct in_addr);
115 static struct mbuf *
116 		igmp_ra_alloc(void);
117 #ifdef KTR
118 static char *	igmp_rec_type_to_str(const int);
119 #endif
120 static void	igmp_set_version(struct igmp_ifinfo *, const int);
121 static void	igmp_slowtimo_vnet(void);
122 static void	igmp_sysinit(void);
123 static int	igmp_v1v2_queue_report(struct in_multi *, const int);
124 static void	igmp_v1v2_process_group_timer(struct in_multi *, const int);
125 static void	igmp_v1v2_process_querier_timers(struct igmp_ifinfo *);
126 static void	igmp_v2_update_group(struct in_multi *, const int);
127 static void	igmp_v3_cancel_link_timers(struct igmp_ifinfo *);
128 static void	igmp_v3_dispatch_general_query(struct igmp_ifinfo *);
129 static struct mbuf *
130 		igmp_v3_encap_report(struct ifnet *, struct mbuf *);
131 static int	igmp_v3_enqueue_group_record(struct ifqueue *,
132 		    struct in_multi *, const int, const int, const int);
133 static int	igmp_v3_enqueue_filter_change(struct ifqueue *,
134 		    struct in_multi *);
135 static void	igmp_v3_process_group_timers(struct igmp_ifinfo *,
136 		    struct ifqueue *, struct ifqueue *, struct in_multi *,
137 		    const int);
138 static int	igmp_v3_merge_state_changes(struct in_multi *,
139 		    struct ifqueue *);
140 static void	igmp_v3_suppress_group_record(struct in_multi *);
141 static int	sysctl_igmp_default_version(SYSCTL_HANDLER_ARGS);
142 static int	sysctl_igmp_gsr(SYSCTL_HANDLER_ARGS);
143 static int	sysctl_igmp_ifinfo(SYSCTL_HANDLER_ARGS);
144 
145 static vnet_attach_fn	vnet_igmp_iattach;
146 static vnet_detach_fn	vnet_igmp_idetach;
147 
148 static const struct netisr_handler igmp_nh = {
149 	.nh_name = "igmp",
150 	.nh_handler = igmp_intr,
151 	.nh_proto = NETISR_IGMP,
152 	.nh_policy = NETISR_POLICY_SOURCE,
153 };
154 
155 /*
156  * System-wide globals.
157  *
158  * Unlocked access to these is OK, except for the global IGMP output
159  * queue. The IGMP subsystem lock ends up being system-wide for the moment,
160  * because all VIMAGEs have to share a global output queue, as netisrs
161  * themselves are not virtualized.
162  *
163  * Locking:
164  *  * The permitted lock order is: IN_MULTI_LOCK, IGMP_LOCK, IF_ADDR_LOCK.
165  *    Any may be taken independently; if any are held at the same
166  *    time, the above lock order must be followed.
167  *  * All output is delegated to the netisr.
168  *    Now that Giant has been eliminated, the netisr may be inlined.
169  *  * IN_MULTI_LOCK covers in_multi.
170  *  * IGMP_LOCK covers igmp_ifinfo and any global variables in this file,
171  *    including the output queue.
172  *  * IF_ADDR_LOCK covers if_multiaddrs, which is used for a variety of
173  *    per-link state iterators.
174  *  * igmp_ifinfo is valid as long as PF_INET is attached to the interface,
175  *    therefore it is not refcounted.
176  *    We allow unlocked reads of igmp_ifinfo when accessed via in_multi.
177  *
178  * Reference counting
179  *  * IGMP acquires its own reference every time an in_multi is passed to
180  *    it and the group is being joined for the first time.
181  *  * IGMP releases its reference(s) on in_multi in a deferred way,
182  *    because the operations which process the release run as part of
183  *    a loop whose control variables are directly affected by the release
184  *    (that, and not recursing on the IF_ADDR_LOCK).
185  *
186  * VIMAGE: Each in_multi corresponds to an ifp, and each ifp corresponds
187  * to a vnet in ifp->if_vnet.
188  *
189  * SMPng: XXX We may potentially race operations on ifma_protospec.
190  * The problem is that we currently lack a clean way of taking the
191  * IF_ADDR_LOCK() between the ifnet and in layers w/o recursing,
192  * as anything which modifies ifma needs to be covered by that lock.
193  * So check for ifma_protospec being NULL before proceeding.
194  */
195 struct mtx		 igmp_mtx;
196 
197 struct mbuf		*m_raopt;		 /* Router Alert option */
198 MALLOC_DEFINE(M_IGMP, "igmp", "igmp state");
199 
200 /*
201  * VIMAGE-wide globals.
202  *
203  * The IGMPv3 timers themselves need to run per-image, however,
204  * protosw timers run globally (see tcp).
205  * An ifnet can only be in one vimage at a time, and the loopback
206  * ifnet, loif, is itself virtualized.
207  * It would otherwise be possible to seriously hose IGMP state,
208  * and create inconsistencies in upstream multicast routing, if you have
209  * multiple VIMAGEs running on the same link joining different multicast
210  * groups, UNLESS the "primary IP address" is different. This is because
211  * IGMP for IPv4 does not force link-local addresses to be used for each
212  * node, unlike MLD for IPv6.
213  * Obviously the IGMPv3 per-interface state has per-vimage granularity
214  * also as a result.
215  *
216  * FUTURE: Stop using IFP_TO_IA/INADDR_ANY, and use source address selection
217  * policy to control the address used by IGMP on the link.
218  */
219 #ifdef VIMAGE_GLOBALS
220 int	 interface_timers_running;	 /* IGMPv3 general query response */
221 int	 state_change_timers_running;	 /* IGMPv3 state-change retransmit */
222 int	 current_state_timers_running;	 /* IGMPv1/v2 host report;
223 					  * IGMPv3 g/sg query response */
224 
225 LIST_HEAD(, igmp_ifinfo)	 igi_head;
226 struct igmpstat			 igmpstat;
227 struct timeval			 igmp_gsrdelay;
228 
229 int	 igmp_recvifkludge;
230 int	 igmp_sendra;
231 int	 igmp_sendlocal;
232 int	 igmp_v1enable;
233 int	 igmp_v2enable;
234 int	 igmp_legacysupp;
235 int	 igmp_default_version;
236 #endif /* VIMAGE_GLOBALS */
237 
238 /*
239  * Virtualized sysctls.
240  */
241 SYSCTL_V_STRUCT(V_NET, vnet_inet, _net_inet_igmp, IGMPCTL_STATS, stats,
242     CTLFLAG_RW, igmpstat, igmpstat, "");
243 SYSCTL_V_INT(V_NET, vnet_inet, _net_inet_igmp, OID_AUTO, recvifkludge,
244     CTLFLAG_RW, igmp_recvifkludge, 0,
245     "Rewrite IGMPv1/v2 reports from 0.0.0.0 to contain subnet address");
246 SYSCTL_V_INT(V_NET, vnet_inet, _net_inet_igmp, OID_AUTO, sendra,
247     CTLFLAG_RW, igmp_sendra, 0,
248     "Send IP Router Alert option in IGMPv2/v3 messages");
249 SYSCTL_V_INT(V_NET, vnet_inet, _net_inet_igmp, OID_AUTO, sendlocal,
250     CTLFLAG_RW, igmp_sendlocal, 0,
251     "Send IGMP membership reports for 224.0.0.0/24 groups");
252 SYSCTL_V_INT(V_NET, vnet_inet, _net_inet_igmp, OID_AUTO, v1enable,
253     CTLFLAG_RW, igmp_v1enable, 0,
254     "Enable backwards compatibility with IGMPv1");
255 SYSCTL_V_INT(V_NET, vnet_inet,  _net_inet_igmp, OID_AUTO, v2enable,
256     CTLFLAG_RW, igmp_v2enable, 0,
257     "Enable backwards compatibility with IGMPv2");
258 SYSCTL_V_INT(V_NET, vnet_inet, _net_inet_igmp, OID_AUTO, legacysupp,
259     CTLFLAG_RW, igmp_legacysupp, 0,
260     "Allow v1/v2 reports to suppress v3 group responses");
261 SYSCTL_V_PROC(V_NET, vnet_inet, _net_inet_igmp, OID_AUTO, default_version,
262     CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, igmp_default_version, 0,
263     sysctl_igmp_default_version, "I",
264     "Default version of IGMP to run on each interface");
265 SYSCTL_V_PROC(V_NET, vnet_inet, _net_inet_igmp, OID_AUTO, gsrdelay,
266     CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, igmp_gsrdelay.tv_sec, 0,
267     sysctl_igmp_gsr, "I",
268     "Rate limit for IGMPv3 Group-and-Source queries in seconds");
269 
270 /*
271  * Non-virtualized sysctls.
272  */
273 SYSCTL_NODE(_net_inet_igmp, OID_AUTO, ifinfo, CTLFLAG_RD | CTLFLAG_MPSAFE,
274     sysctl_igmp_ifinfo, "Per-interface IGMPv3 state");
275 
276 static __inline void
277 igmp_save_context(struct mbuf *m, struct ifnet *ifp)
278 {
279 
280 #ifdef VIMAGE
281 	m->m_pkthdr.header = ifp->if_vnet;
282 #endif /* VIMAGE */
283 	m->m_pkthdr.flowid = ifp->if_index;
284 }
285 
286 static __inline void
287 igmp_scrub_context(struct mbuf *m)
288 {
289 
290 	m->m_pkthdr.header = NULL;
291 	m->m_pkthdr.flowid = 0;
292 }
293 
294 #ifdef KTR
295 static __inline char *
296 inet_ntoa_haddr(in_addr_t haddr)
297 {
298 	struct in_addr ia;
299 
300 	ia.s_addr = htonl(haddr);
301 	return (inet_ntoa(ia));
302 }
303 #endif
304 
305 /*
306  * Restore context from a queued IGMP output chain.
307  * Return saved ifindex.
308  *
309  * VIMAGE: The assertion is there to make sure that we
310  * actually called CURVNET_SET() with what's in the mbuf chain.
311  */
312 static __inline uint32_t
313 igmp_restore_context(struct mbuf *m)
314 {
315 
316 #ifdef notyet
317 #if defined(VIMAGE) && defined(INVARIANTS)
318 	KASSERT(curvnet == (m->m_pkthdr.header),
319 	    ("%s: called when curvnet was not restored", __func__));
320 #endif
321 #endif
322 	return (m->m_pkthdr.flowid);
323 }
324 
325 /*
326  * Retrieve or set default IGMP version.
327  *
328  * VIMAGE: Assume curvnet set by caller.
329  * SMPng: NOTE: Serialized by IGMP lock.
330  */
331 static int
332 sysctl_igmp_default_version(SYSCTL_HANDLER_ARGS)
333 {
334 	INIT_VNET_INET(curvnet);
335 	int	 error;
336 	int	 new;
337 
338 	error = sysctl_wire_old_buffer(req, sizeof(int));
339 	if (error)
340 		return (error);
341 
342 	IGMP_LOCK();
343 
344 	new = V_igmp_default_version;
345 
346 	error = sysctl_handle_int(oidp, &new, 0, req);
347 	if (error || !req->newptr)
348 		goto out_locked;
349 
350 	if (new < IGMP_VERSION_1 || new > IGMP_VERSION_3) {
351 		error = EINVAL;
352 		goto out_locked;
353 	}
354 
355 	CTR2(KTR_IGMPV3, "change igmp_default_version from %d to %d",
356 	     V_igmp_default_version, new);
357 
358 	V_igmp_default_version = new;
359 
360 out_locked:
361 	IGMP_UNLOCK();
362 	return (error);
363 }
364 
365 /*
366  * Retrieve or set threshold between group-source queries in seconds.
367  *
368  * VIMAGE: Assume curvnet set by caller.
369  * SMPng: NOTE: Serialized by IGMP lock.
370  */
371 static int
372 sysctl_igmp_gsr(SYSCTL_HANDLER_ARGS)
373 {
374 	INIT_VNET_INET(curvnet);
375 	int error;
376 	int i;
377 
378 	error = sysctl_wire_old_buffer(req, sizeof(int));
379 	if (error)
380 		return (error);
381 
382 	IGMP_LOCK();
383 
384 	i = V_igmp_gsrdelay.tv_sec;
385 
386 	error = sysctl_handle_int(oidp, &i, 0, req);
387 	if (error || !req->newptr)
388 		goto out_locked;
389 
390 	if (i < -1 || i >= 60) {
391 		error = EINVAL;
392 		goto out_locked;
393 	}
394 
395 	CTR2(KTR_IGMPV3, "change igmp_gsrdelay from %d to %d",
396 	     V_igmp_gsrdelay.tv_sec, i);
397 	V_igmp_gsrdelay.tv_sec = i;
398 
399 out_locked:
400 	IGMP_UNLOCK();
401 	return (error);
402 }
403 
404 /*
405  * Expose struct igmp_ifinfo to userland, keyed by ifindex.
406  * For use by ifmcstat(8).
407  *
408  * SMPng: NOTE: Does an unlocked ifindex space read.
409  * VIMAGE: Assume curvnet set by caller. The node handler itself
410  * is not directly virtualized.
411  */
412 static int
413 sysctl_igmp_ifinfo(SYSCTL_HANDLER_ARGS)
414 {
415 	INIT_VNET_NET(curvnet);
416 	INIT_VNET_INET(curvnet);
417 	int			*name;
418 	int			 error;
419 	u_int			 namelen;
420 	struct ifnet		*ifp;
421 	struct igmp_ifinfo	*igi;
422 
423 	name = (int *)arg1;
424 	namelen = arg2;
425 
426 	if (req->newptr != NULL)
427 		return (EPERM);
428 
429 	if (namelen != 1)
430 		return (EINVAL);
431 
432 	error = sysctl_wire_old_buffer(req, sizeof(struct igmp_ifinfo));
433 	if (error)
434 		return (error);
435 
436 	IN_MULTI_LOCK();
437 	IGMP_LOCK();
438 
439 	if (name[0] <= 0 || name[0] > V_if_index) {
440 		error = ENOENT;
441 		goto out_locked;
442 	}
443 
444 	error = ENOENT;
445 
446 	ifp = ifnet_byindex(name[0]);
447 	if (ifp == NULL)
448 		goto out_locked;
449 
450 	LIST_FOREACH(igi, &V_igi_head, igi_link) {
451 		if (ifp == igi->igi_ifp) {
452 			error = SYSCTL_OUT(req, igi,
453 			    sizeof(struct igmp_ifinfo));
454 			break;
455 		}
456 	}
457 
458 out_locked:
459 	IGMP_UNLOCK();
460 	IN_MULTI_UNLOCK();
461 	return (error);
462 }
463 
464 /*
465  * Dispatch an entire queue of pending packet chains
466  * using the netisr.
467  * VIMAGE: Assumes the vnet pointer has been set.
468  */
469 static void
470 igmp_dispatch_queue(struct ifqueue *ifq, int limit, const int loop)
471 {
472 	struct mbuf *m;
473 
474 	for (;;) {
475 		_IF_DEQUEUE(ifq, m);
476 		if (m == NULL)
477 			break;
478 		CTR3(KTR_IGMPV3, "%s: dispatch %p from %p", __func__, ifq, m);
479 		if (loop)
480 			m->m_flags |= M_IGMP_LOOP;
481 		netisr_dispatch(NETISR_IGMP, m);
482 		if (--limit == 0)
483 			break;
484 	}
485 }
486 
487 /*
488  * Filter outgoing IGMP report state by group.
489  *
490  * Reports are ALWAYS suppressed for ALL-HOSTS (224.0.0.1).
491  * If the net.inet.igmp.sendlocal sysctl is 0, then IGMP reports are
492  * disabled for all groups in the 224.0.0.0/24 link-local scope. However,
493  * this may break certain IGMP snooping switches which rely on the old
494  * report behaviour.
495  *
496  * Return zero if the given group is one for which IGMP reports
497  * should be suppressed, or non-zero if reports should be issued.
498  */
499 static __inline int
500 igmp_isgroupreported(const struct in_addr addr)
501 {
502 	INIT_VNET_INET(curvnet);
503 
504 	if (in_allhosts(addr) ||
505 	    ((!V_igmp_sendlocal && IN_LOCAL_GROUP(ntohl(addr.s_addr)))))
506 		return (0);
507 
508 	return (1);
509 }
510 
511 /*
512  * Construct a Router Alert option to use in outgoing packets.
513  */
514 static struct mbuf *
515 igmp_ra_alloc(void)
516 {
517 	struct mbuf	*m;
518 	struct ipoption	*p;
519 
520 	MGET(m, M_DONTWAIT, MT_DATA);
521 	p = mtod(m, struct ipoption *);
522 	p->ipopt_dst.s_addr = INADDR_ANY;
523 	p->ipopt_list[0] = IPOPT_RA;	/* Router Alert Option */
524 	p->ipopt_list[1] = 0x04;	/* 4 bytes long */
525 	p->ipopt_list[2] = IPOPT_EOL;	/* End of IP option list */
526 	p->ipopt_list[3] = 0x00;	/* pad byte */
527 	m->m_len = sizeof(p->ipopt_dst) + p->ipopt_list[1];
528 
529 	return (m);
530 }
531 
532 /*
533  * Attach IGMP when PF_INET is attached to an interface.
534  */
535 struct igmp_ifinfo *
536 igmp_domifattach(struct ifnet *ifp)
537 {
538 	struct igmp_ifinfo *igi;
539 
540 	CTR3(KTR_IGMPV3, "%s: called for ifp %p(%s)",
541 	    __func__, ifp, ifp->if_xname);
542 
543 	IGMP_LOCK();
544 
545 	igi = igi_alloc_locked(ifp);
546 	if (!(ifp->if_flags & IFF_MULTICAST))
547 		igi->igi_flags |= IGIF_SILENT;
548 
549 	IGMP_UNLOCK();
550 
551 	return (igi);
552 }
553 
554 /*
555  * VIMAGE: assume curvnet set by caller.
556  */
557 static struct igmp_ifinfo *
558 igi_alloc_locked(/*const*/ struct ifnet *ifp)
559 {
560 	INIT_VNET_INET(ifp->if_vnet);
561 	struct igmp_ifinfo *igi;
562 
563 	IGMP_LOCK_ASSERT();
564 
565 	igi = malloc(sizeof(struct igmp_ifinfo), M_IGMP, M_NOWAIT|M_ZERO);
566 	if (igi == NULL)
567 		goto out;
568 
569 	igi->igi_ifp = ifp;
570 	igi->igi_version = V_igmp_default_version;
571 	igi->igi_flags = 0;
572 	igi->igi_rv = IGMP_RV_INIT;
573 	igi->igi_qi = IGMP_QI_INIT;
574 	igi->igi_qri = IGMP_QRI_INIT;
575 	igi->igi_uri = IGMP_URI_INIT;
576 
577 	SLIST_INIT(&igi->igi_relinmhead);
578 
579 	/*
580 	 * Responses to general queries are subject to bounds.
581 	 */
582 	IFQ_SET_MAXLEN(&igi->igi_gq, IGMP_MAX_RESPONSE_PACKETS);
583 
584 	LIST_INSERT_HEAD(&V_igi_head, igi, igi_link);
585 
586 	CTR2(KTR_IGMPV3, "allocate igmp_ifinfo for ifp %p(%s)",
587 	     ifp, ifp->if_xname);
588 
589 out:
590 	return (igi);
591 }
592 
593 /*
594  * Hook for ifdetach.
595  *
596  * NOTE: Some finalization tasks need to run before the protocol domain
597  * is detached, but also before the link layer does its cleanup.
598  *
599  * SMPNG: igmp_ifdetach() needs to take IF_ADDR_LOCK().
600  * XXX This is also bitten by unlocked ifma_protospec access.
601  */
602 void
603 igmp_ifdetach(struct ifnet *ifp)
604 {
605 	struct igmp_ifinfo	*igi;
606 	struct ifmultiaddr	*ifma;
607 	struct in_multi		*inm, *tinm;
608 
609 	CTR3(KTR_IGMPV3, "%s: called for ifp %p(%s)", __func__, ifp,
610 	    ifp->if_xname);
611 
612 	IGMP_LOCK();
613 
614 	igi = ((struct in_ifinfo *)ifp->if_afdata[AF_INET])->ii_igmp;
615 	if (igi->igi_version == IGMP_VERSION_3) {
616 		IF_ADDR_LOCK(ifp);
617 		TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
618 			if (ifma->ifma_addr->sa_family != AF_INET ||
619 			    ifma->ifma_protospec == NULL)
620 				continue;
621 #if 0
622 			KASSERT(ifma->ifma_protospec != NULL,
623 			    ("%s: ifma_protospec is NULL", __func__));
624 #endif
625 			inm = (struct in_multi *)ifma->ifma_protospec;
626 			if (inm->inm_state == IGMP_LEAVING_MEMBER) {
627 				SLIST_INSERT_HEAD(&igi->igi_relinmhead,
628 				    inm, inm_nrele);
629 			}
630 			inm_clear_recorded(inm);
631 		}
632 		IF_ADDR_UNLOCK(ifp);
633 		/*
634 		 * Free the in_multi reference(s) for this IGMP lifecycle.
635 		 */
636 		SLIST_FOREACH_SAFE(inm, &igi->igi_relinmhead, inm_nrele,
637 		    tinm) {
638 			SLIST_REMOVE_HEAD(&igi->igi_relinmhead, inm_nrele);
639 			inm_release_locked(inm);
640 		}
641 	}
642 
643 	IGMP_UNLOCK();
644 }
645 
646 /*
647  * Hook for domifdetach.
648  */
649 void
650 igmp_domifdetach(struct ifnet *ifp)
651 {
652 	struct igmp_ifinfo *igi;
653 
654 	CTR3(KTR_IGMPV3, "%s: called for ifp %p(%s)",
655 	    __func__, ifp, ifp->if_xname);
656 
657 	IGMP_LOCK();
658 
659 	igi = ((struct in_ifinfo *)ifp->if_afdata[AF_INET])->ii_igmp;
660 	igi_delete_locked(ifp);
661 
662 	IGMP_UNLOCK();
663 }
664 
665 static void
666 igi_delete_locked(const struct ifnet *ifp)
667 {
668 	INIT_VNET_INET(ifp->if_vnet);
669 	struct igmp_ifinfo *igi, *tigi;
670 
671 	CTR3(KTR_IGMPV3, "%s: freeing igmp_ifinfo for ifp %p(%s)",
672 	    __func__, ifp, ifp->if_xname);
673 
674 	IGMP_LOCK_ASSERT();
675 
676 	LIST_FOREACH_SAFE(igi, &V_igi_head, igi_link, tigi) {
677 		if (igi->igi_ifp == ifp) {
678 			/*
679 			 * Free deferred General Query responses.
680 			 */
681 			_IF_DRAIN(&igi->igi_gq);
682 
683 			LIST_REMOVE(igi, igi_link);
684 
685 			KASSERT(SLIST_EMPTY(&igi->igi_relinmhead),
686 			    ("%s: there are dangling in_multi references",
687 			    __func__));
688 
689 			free(igi, M_IGMP);
690 			return;
691 		}
692 	}
693 
694 #ifdef INVARIANTS
695 	panic("%s: igmp_ifinfo not found for ifp %p\n", __func__,  ifp);
696 #endif
697 }
698 
699 /*
700  * Process a received IGMPv1 query.
701  * Return non-zero if the message should be dropped.
702  *
703  * VIMAGE: The curvnet pointer is derived from the input ifp.
704  */
705 static int
706 igmp_input_v1_query(struct ifnet *ifp, const struct ip *ip,
707     const struct igmp *igmp)
708 {
709 	INIT_VNET_INET(ifp->if_vnet);
710 	struct ifmultiaddr	*ifma;
711 	struct igmp_ifinfo	*igi;
712 	struct in_multi		*inm;
713 
714 	/*
715 	 * IGMPv1 Host Mmembership Queries SHOULD always be addressed to
716 	 * 224.0.0.1. They are always treated as General Queries.
717 	 * igmp_group is always ignored. Do not drop it as a userland
718 	 * daemon may wish to see it.
719 	 * XXX SMPng: unlocked increments in igmpstat assumed atomic.
720 	 */
721 	if (!in_allhosts(ip->ip_dst) || !in_nullhost(igmp->igmp_group)) {
722 		IGMPSTAT_INC(igps_rcv_badqueries);
723 		return (0);
724 	}
725 	IGMPSTAT_INC(igps_rcv_gen_queries);
726 
727 	IN_MULTI_LOCK();
728 	IGMP_LOCK();
729 
730 	igi = ((struct in_ifinfo *)ifp->if_afdata[AF_INET])->ii_igmp;
731 	KASSERT(igi != NULL, ("%s: no igmp_ifinfo for ifp %p", __func__, ifp));
732 
733 	if (igi->igi_flags & IGIF_LOOPBACK) {
734 		CTR2(KTR_IGMPV3, "ignore v1 query on IGIF_LOOPBACK ifp %p(%s)",
735 		    ifp, ifp->if_xname);
736 		goto out_locked;
737 	}
738 
739 	/*
740 	 * Switch to IGMPv1 host compatibility mode.
741 	 */
742 	igmp_set_version(igi, IGMP_VERSION_1);
743 
744 	CTR2(KTR_IGMPV3, "process v1 query on ifp %p(%s)", ifp, ifp->if_xname);
745 
746 	/*
747 	 * Start the timers in all of our group records
748 	 * for the interface on which the query arrived,
749 	 * except those which are already running.
750 	 */
751 	IF_ADDR_LOCK(ifp);
752 	TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
753 		if (ifma->ifma_addr->sa_family != AF_INET ||
754 		    ifma->ifma_protospec == NULL)
755 			continue;
756 		inm = (struct in_multi *)ifma->ifma_protospec;
757 		if (inm->inm_timer != 0)
758 			continue;
759 		switch (inm->inm_state) {
760 		case IGMP_NOT_MEMBER:
761 		case IGMP_SILENT_MEMBER:
762 			break;
763 		case IGMP_G_QUERY_PENDING_MEMBER:
764 		case IGMP_SG_QUERY_PENDING_MEMBER:
765 		case IGMP_REPORTING_MEMBER:
766 		case IGMP_IDLE_MEMBER:
767 		case IGMP_LAZY_MEMBER:
768 		case IGMP_SLEEPING_MEMBER:
769 		case IGMP_AWAKENING_MEMBER:
770 			inm->inm_state = IGMP_REPORTING_MEMBER;
771 			inm->inm_timer = IGMP_RANDOM_DELAY(
772 			    IGMP_V1V2_MAX_RI * PR_FASTHZ);
773 			V_current_state_timers_running = 1;
774 			break;
775 		case IGMP_LEAVING_MEMBER:
776 			break;
777 		}
778 	}
779 	IF_ADDR_UNLOCK(ifp);
780 
781 out_locked:
782 	IGMP_UNLOCK();
783 	IN_MULTI_UNLOCK();
784 
785 	return (0);
786 }
787 
788 /*
789  * Process a received IGMPv2 general or group-specific query.
790  */
791 static int
792 igmp_input_v2_query(struct ifnet *ifp, const struct ip *ip,
793     const struct igmp *igmp)
794 {
795 	INIT_VNET_INET(ifp->if_vnet);
796 	struct ifmultiaddr	*ifma;
797 	struct igmp_ifinfo	*igi;
798 	struct in_multi		*inm;
799 	int			 is_general_query;
800 	uint16_t		 timer;
801 
802 	is_general_query = 0;
803 
804 	/*
805 	 * Validate address fields upfront.
806 	 * XXX SMPng: unlocked increments in igmpstat assumed atomic.
807 	 */
808 	if (in_nullhost(igmp->igmp_group)) {
809 		/*
810 		 * IGMPv2 General Query.
811 		 * If this was not sent to the all-hosts group, ignore it.
812 		 */
813 		if (!in_allhosts(ip->ip_dst))
814 			return (0);
815 		IGMPSTAT_INC(igps_rcv_gen_queries);
816 		is_general_query = 1;
817 	} else {
818 		/* IGMPv2 Group-Specific Query. */
819 		IGMPSTAT_INC(igps_rcv_group_queries);
820 	}
821 
822 	IN_MULTI_LOCK();
823 	IGMP_LOCK();
824 
825 	igi = ((struct in_ifinfo *)ifp->if_afdata[AF_INET])->ii_igmp;
826 	KASSERT(igi != NULL, ("%s: no igmp_ifinfo for ifp %p", __func__, ifp));
827 
828 	if (igi->igi_flags & IGIF_LOOPBACK) {
829 		CTR2(KTR_IGMPV3, "ignore v2 query on IGIF_LOOPBACK ifp %p(%s)",
830 		    ifp, ifp->if_xname);
831 		goto out_locked;
832 	}
833 
834 	/*
835 	 * Ignore v2 query if in v1 Compatibility Mode.
836 	 */
837 	if (igi->igi_version == IGMP_VERSION_1)
838 		goto out_locked;
839 
840 	igmp_set_version(igi, IGMP_VERSION_2);
841 
842 	timer = igmp->igmp_code * PR_FASTHZ / IGMP_TIMER_SCALE;
843 	if (timer == 0)
844 		timer = 1;
845 
846 	if (is_general_query) {
847 		/*
848 		 * For each reporting group joined on this
849 		 * interface, kick the report timer.
850 		 */
851 		CTR2(KTR_IGMPV3, "process v2 general query on ifp %p(%s)",
852 		    ifp, ifp->if_xname);
853 		IF_ADDR_LOCK(ifp);
854 		TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
855 			if (ifma->ifma_addr->sa_family != AF_INET ||
856 			    ifma->ifma_protospec == NULL)
857 				continue;
858 			inm = (struct in_multi *)ifma->ifma_protospec;
859 			igmp_v2_update_group(inm, timer);
860 		}
861 		IF_ADDR_UNLOCK(ifp);
862 	} else {
863 		/*
864 		 * Group-specific IGMPv2 query, we need only
865 		 * look up the single group to process it.
866 		 */
867 		inm = inm_lookup(ifp, igmp->igmp_group);
868 		if (inm != NULL) {
869 			CTR3(KTR_IGMPV3, "process v2 query %s on ifp %p(%s)",
870 			    inet_ntoa(igmp->igmp_group), ifp, ifp->if_xname);
871 			igmp_v2_update_group(inm, timer);
872 		}
873 	}
874 
875 out_locked:
876 	IGMP_UNLOCK();
877 	IN_MULTI_UNLOCK();
878 
879 	return (0);
880 }
881 
882 /*
883  * Update the report timer on a group in response to an IGMPv2 query.
884  *
885  * If we are becoming the reporting member for this group, start the timer.
886  * If we already are the reporting member for this group, and timer is
887  * below the threshold, reset it.
888  *
889  * We may be updating the group for the first time since we switched
890  * to IGMPv3. If we are, then we must clear any recorded source lists,
891  * and transition to REPORTING state; the group timer is overloaded
892  * for group and group-source query responses.
893  *
894  * Unlike IGMPv3, the delay per group should be jittered
895  * to avoid bursts of IGMPv2 reports.
896  */
897 static void
898 igmp_v2_update_group(struct in_multi *inm, const int timer)
899 {
900 	INIT_VNET_INET(curvnet);
901 
902 	CTR4(KTR_IGMPV3, "%s: %s/%s timer=%d", __func__,
903 	    inet_ntoa(inm->inm_addr), inm->inm_ifp->if_xname, timer);
904 
905 	IN_MULTI_LOCK_ASSERT();
906 
907 	switch (inm->inm_state) {
908 	case IGMP_NOT_MEMBER:
909 	case IGMP_SILENT_MEMBER:
910 		break;
911 	case IGMP_REPORTING_MEMBER:
912 		if (inm->inm_timer != 0 &&
913 		    inm->inm_timer <= timer) {
914 			CTR1(KTR_IGMPV3, "%s: REPORTING and timer running, "
915 			    "skipping.", __func__);
916 			break;
917 		}
918 		/* FALLTHROUGH */
919 	case IGMP_SG_QUERY_PENDING_MEMBER:
920 	case IGMP_G_QUERY_PENDING_MEMBER:
921 	case IGMP_IDLE_MEMBER:
922 	case IGMP_LAZY_MEMBER:
923 	case IGMP_AWAKENING_MEMBER:
924 		CTR1(KTR_IGMPV3, "%s: ->REPORTING", __func__);
925 		inm->inm_state = IGMP_REPORTING_MEMBER;
926 		inm->inm_timer = IGMP_RANDOM_DELAY(timer);
927 		V_current_state_timers_running = 1;
928 		break;
929 	case IGMP_SLEEPING_MEMBER:
930 		CTR1(KTR_IGMPV3, "%s: ->AWAKENING", __func__);
931 		inm->inm_state = IGMP_AWAKENING_MEMBER;
932 		break;
933 	case IGMP_LEAVING_MEMBER:
934 		break;
935 	}
936 }
937 
938 /*
939  * Process a received IGMPv3 general, group-specific or
940  * group-and-source-specific query.
941  * Assumes m has already been pulled up to the full IGMP message length.
942  * Return 0 if successful, otherwise an appropriate error code is returned.
943  */
944 static int
945 igmp_input_v3_query(struct ifnet *ifp, const struct ip *ip,
946     /*const*/ struct igmpv3 *igmpv3)
947 {
948 	INIT_VNET_INET(ifp->if_vnet);
949 	struct igmp_ifinfo	*igi;
950 	struct in_multi		*inm;
951 	int			 is_general_query;
952 	uint32_t		 maxresp, nsrc, qqi;
953 	uint16_t		 timer;
954 	uint8_t			 qrv;
955 
956 	is_general_query = 0;
957 
958 	CTR2(KTR_IGMPV3, "process v3 query on ifp %p(%s)", ifp, ifp->if_xname);
959 
960 	maxresp = igmpv3->igmp_code;	/* in 1/10ths of a second */
961 	if (maxresp >= 128) {
962 		maxresp = IGMP_MANT(igmpv3->igmp_code) <<
963 			  (IGMP_EXP(igmpv3->igmp_code) + 3);
964 	}
965 
966 	/*
967 	 * Robustness must never be less than 2 for on-wire IGMPv3.
968 	 * FUTURE: Check if ifp has IGIF_LOOPBACK set, as we will make
969 	 * an exception for interfaces whose IGMPv3 state changes
970 	 * are redirected to loopback (e.g. MANET).
971 	 */
972 	qrv = IGMP_QRV(igmpv3->igmp_misc);
973 	if (qrv < 2) {
974 		CTR3(KTR_IGMPV3, "%s: clamping qrv %d to %d", __func__,
975 		    qrv, IGMP_RV_INIT);
976 		qrv = IGMP_RV_INIT;
977 	}
978 
979 	qqi = igmpv3->igmp_qqi;
980 	if (qqi >= 128) {
981 		qqi = IGMP_MANT(igmpv3->igmp_qqi) <<
982 		     (IGMP_EXP(igmpv3->igmp_qqi) + 3);
983 	}
984 
985 	timer = maxresp * PR_FASTHZ / IGMP_TIMER_SCALE;
986 	if (timer == 0)
987 		timer = 1;
988 
989 	nsrc = ntohs(igmpv3->igmp_numsrc);
990 
991 	/*
992 	 * Validate address fields and versions upfront before
993 	 * accepting v3 query.
994 	 * XXX SMPng: Unlocked access to igmpstat counters here.
995 	 */
996 	if (in_nullhost(igmpv3->igmp_group)) {
997 		/*
998 		 * IGMPv3 General Query.
999 		 *
1000 		 * General Queries SHOULD be directed to 224.0.0.1.
1001 		 * A general query with a source list has undefined
1002 		 * behaviour; discard it.
1003 		 */
1004 		IGMPSTAT_INC(igps_rcv_gen_queries);
1005 		if (!in_allhosts(ip->ip_dst) || nsrc > 0) {
1006 			IGMPSTAT_INC(igps_rcv_badqueries);
1007 			return (0);
1008 		}
1009 		is_general_query = 1;
1010 	} else {
1011 		/* Group or group-source specific query. */
1012 		if (nsrc == 0)
1013 			IGMPSTAT_INC(igps_rcv_group_queries);
1014 		else
1015 			IGMPSTAT_INC(igps_rcv_gsr_queries);
1016 	}
1017 
1018 	IN_MULTI_LOCK();
1019 	IGMP_LOCK();
1020 
1021 	igi = ((struct in_ifinfo *)ifp->if_afdata[AF_INET])->ii_igmp;
1022 	KASSERT(igi != NULL, ("%s: no igmp_ifinfo for ifp %p", __func__, ifp));
1023 
1024 	if (igi->igi_flags & IGIF_LOOPBACK) {
1025 		CTR2(KTR_IGMPV3, "ignore v3 query on IGIF_LOOPBACK ifp %p(%s)",
1026 		    ifp, ifp->if_xname);
1027 		goto out_locked;
1028 	}
1029 
1030 	/*
1031 	 * Discard the v3 query if we're in Compatibility Mode.
1032 	 * The RFC is not obviously worded that hosts need to stay in
1033 	 * compatibility mode until the Old Version Querier Present
1034 	 * timer expires.
1035 	 */
1036 	if (igi->igi_version != IGMP_VERSION_3) {
1037 		CTR3(KTR_IGMPV3, "ignore v3 query in v%d mode on ifp %p(%s)",
1038 		    igi->igi_version, ifp, ifp->if_xname);
1039 		goto out_locked;
1040 	}
1041 
1042 	igmp_set_version(igi, IGMP_VERSION_3);
1043 	igi->igi_rv = qrv;
1044 	igi->igi_qi = qqi;
1045 	igi->igi_qri = maxresp;
1046 
1047 	CTR4(KTR_IGMPV3, "%s: qrv %d qi %d qri %d", __func__, qrv, qqi,
1048 	    maxresp);
1049 
1050 	if (is_general_query) {
1051 		/*
1052 		 * Schedule a current-state report on this ifp for
1053 		 * all groups, possibly containing source lists.
1054 		 * If there is a pending General Query response
1055 		 * scheduled earlier than the selected delay, do
1056 		 * not schedule any other reports.
1057 		 * Otherwise, reset the interface timer.
1058 		 */
1059 		CTR2(KTR_IGMPV3, "process v3 general query on ifp %p(%s)",
1060 		    ifp, ifp->if_xname);
1061 		if (igi->igi_v3_timer == 0 || igi->igi_v3_timer >= timer) {
1062 			igi->igi_v3_timer = IGMP_RANDOM_DELAY(timer);
1063 			V_interface_timers_running = 1;
1064 		}
1065 	} else {
1066 		/*
1067 		 * Group-source-specific queries are throttled on
1068 		 * a per-group basis to defeat denial-of-service attempts.
1069 		 * Queries for groups we are not a member of on this
1070 		 * link are simply ignored.
1071 		 */
1072 		inm = inm_lookup(ifp, igmpv3->igmp_group);
1073 		if (inm == NULL)
1074 			goto out_locked;
1075 		if (nsrc > 0) {
1076 			if (!ratecheck(&inm->inm_lastgsrtv,
1077 			    &V_igmp_gsrdelay)) {
1078 				CTR1(KTR_IGMPV3, "%s: GS query throttled.",
1079 				    __func__);
1080 				IGMPSTAT_INC(igps_drop_gsr_queries);
1081 				goto out_locked;
1082 			}
1083 		}
1084 		CTR3(KTR_IGMPV3, "process v3 %s query on ifp %p(%s)",
1085 		     inet_ntoa(igmpv3->igmp_group), ifp, ifp->if_xname);
1086 		/*
1087 		 * If there is a pending General Query response
1088 		 * scheduled sooner than the selected delay, no
1089 		 * further report need be scheduled.
1090 		 * Otherwise, prepare to respond to the
1091 		 * group-specific or group-and-source query.
1092 		 */
1093 		if (igi->igi_v3_timer == 0 || igi->igi_v3_timer >= timer)
1094 			igmp_input_v3_group_query(inm, igi, timer, igmpv3);
1095 	}
1096 
1097 out_locked:
1098 	IGMP_UNLOCK();
1099 	IN_MULTI_UNLOCK();
1100 
1101 	return (0);
1102 }
1103 
1104 /*
1105  * Process a recieved IGMPv3 group-specific or group-and-source-specific
1106  * query.
1107  * Return <0 if any error occured. Currently this is ignored.
1108  */
1109 static int
1110 igmp_input_v3_group_query(struct in_multi *inm, struct igmp_ifinfo *igi,
1111     int timer, /*const*/ struct igmpv3 *igmpv3)
1112 {
1113 	INIT_VNET_INET(curvnet);
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 	INIT_VNET_INET(ifp->if_vnet);
1217 	struct in_ifaddr *ia;
1218 	struct in_multi *inm;
1219 
1220 	IGMPSTAT_INC(igps_rcv_reports);
1221 
1222 	if (ifp->if_flags & IFF_LOOPBACK)
1223 		return (0);
1224 
1225 	if (!IN_MULTICAST(ntohl(igmp->igmp_group.s_addr) ||
1226 	    !in_hosteq(igmp->igmp_group, ip->ip_dst))) {
1227 		IGMPSTAT_INC(igps_rcv_badreports);
1228 		return (EINVAL);
1229 	}
1230 
1231 	/*
1232 	 * RFC 3376, Section 4.2.13, 9.2, 9.3:
1233 	 * Booting clients may use the source address 0.0.0.0. Some
1234 	 * IGMP daemons may not know how to use IP_RECVIF to determine
1235 	 * the interface upon which this message was received.
1236 	 * Replace 0.0.0.0 with the subnet address if told to do so.
1237 	 */
1238 	if (V_igmp_recvifkludge && in_nullhost(ip->ip_src)) {
1239 		IFP_TO_IA(ifp, ia);
1240 		if (ia != NULL)
1241 			ip->ip_src.s_addr = htonl(ia->ia_subnet);
1242 	}
1243 
1244 	CTR3(KTR_IGMPV3, "process v1 report %s on ifp %p(%s)",
1245 	     inet_ntoa(igmp->igmp_group), ifp, ifp->if_xname);
1246 
1247 	/*
1248 	 * IGMPv1 report suppression.
1249 	 * If we are a member of this group, and our membership should be
1250 	 * reported, stop our group timer and transition to the 'lazy' state.
1251 	 */
1252 	IN_MULTI_LOCK();
1253 	inm = inm_lookup(ifp, igmp->igmp_group);
1254 	if (inm != NULL) {
1255 		struct igmp_ifinfo *igi;
1256 
1257 		igi = inm->inm_igi;
1258 		if (igi == NULL) {
1259 			KASSERT(igi != NULL,
1260 			    ("%s: no igi for ifp %p", __func__, ifp));
1261 			goto out_locked;
1262 		}
1263 
1264 		IGMPSTAT_INC(igps_rcv_ourreports);
1265 
1266 		/*
1267 		 * If we are in IGMPv3 host mode, do not allow the
1268 		 * other host's IGMPv1 report to suppress our reports
1269 		 * unless explicitly configured to do so.
1270 		 */
1271 		if (igi->igi_version == IGMP_VERSION_3) {
1272 			if (V_igmp_legacysupp)
1273 				igmp_v3_suppress_group_record(inm);
1274 			goto out_locked;
1275 		}
1276 
1277 		inm->inm_timer = 0;
1278 
1279 		switch (inm->inm_state) {
1280 		case IGMP_NOT_MEMBER:
1281 		case IGMP_SILENT_MEMBER:
1282 			break;
1283 		case IGMP_IDLE_MEMBER:
1284 		case IGMP_LAZY_MEMBER:
1285 		case IGMP_AWAKENING_MEMBER:
1286 			CTR3(KTR_IGMPV3,
1287 			    "report suppressed for %s on ifp %p(%s)",
1288 			    inet_ntoa(igmp->igmp_group), ifp, ifp->if_xname);
1289 		case IGMP_SLEEPING_MEMBER:
1290 			inm->inm_state = IGMP_SLEEPING_MEMBER;
1291 			break;
1292 		case IGMP_REPORTING_MEMBER:
1293 			CTR3(KTR_IGMPV3,
1294 			    "report suppressed for %s on ifp %p(%s)",
1295 			    inet_ntoa(igmp->igmp_group), ifp, ifp->if_xname);
1296 			if (igi->igi_version == IGMP_VERSION_1)
1297 				inm->inm_state = IGMP_LAZY_MEMBER;
1298 			else if (igi->igi_version == IGMP_VERSION_2)
1299 				inm->inm_state = IGMP_SLEEPING_MEMBER;
1300 			break;
1301 		case IGMP_G_QUERY_PENDING_MEMBER:
1302 		case IGMP_SG_QUERY_PENDING_MEMBER:
1303 		case IGMP_LEAVING_MEMBER:
1304 			break;
1305 		}
1306 	}
1307 
1308 out_locked:
1309 	IN_MULTI_UNLOCK();
1310 
1311 	return (0);
1312 }
1313 
1314 /*
1315  * Process a received IGMPv2 host membership report.
1316  *
1317  * NOTE: 0.0.0.0 workaround breaks const correctness.
1318  */
1319 static int
1320 igmp_input_v2_report(struct ifnet *ifp, /*const*/ struct ip *ip,
1321     /*const*/ struct igmp *igmp)
1322 {
1323 	INIT_VNET_INET(ifp->if_vnet);
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 		return (0);
1335 
1336 	IGMPSTAT_INC(igps_rcv_reports);
1337 
1338 	if (ifp->if_flags & IFF_LOOPBACK)
1339 		return (0);
1340 
1341 	if (!IN_MULTICAST(ntohl(igmp->igmp_group.s_addr)) ||
1342 	    !in_hosteq(igmp->igmp_group, ip->ip_dst)) {
1343 		IGMPSTAT_INC(igps_rcv_badreports);
1344 		return (EINVAL);
1345 	}
1346 
1347 	/*
1348 	 * RFC 3376, Section 4.2.13, 9.2, 9.3:
1349 	 * Booting clients may use the source address 0.0.0.0. Some
1350 	 * IGMP daemons may not know how to use IP_RECVIF to determine
1351 	 * the interface upon which this message was received.
1352 	 * Replace 0.0.0.0 with the subnet address if told to do so.
1353 	 */
1354 	if (V_igmp_recvifkludge && in_nullhost(ip->ip_src)) {
1355 		if (ia != NULL)
1356 			ip->ip_src.s_addr = htonl(ia->ia_subnet);
1357 	}
1358 
1359 	CTR3(KTR_IGMPV3, "process v2 report %s on ifp %p(%s)",
1360 	     inet_ntoa(igmp->igmp_group), ifp, ifp->if_xname);
1361 
1362 	/*
1363 	 * IGMPv2 report suppression.
1364 	 * If we are a member of this group, and our membership should be
1365 	 * reported, and our group timer is pending or about to be reset,
1366 	 * stop our group timer by transitioning to the 'lazy' state.
1367 	 */
1368 	IN_MULTI_LOCK();
1369 	inm = inm_lookup(ifp, igmp->igmp_group);
1370 	if (inm != NULL) {
1371 		struct igmp_ifinfo *igi;
1372 
1373 		igi = inm->inm_igi;
1374 		KASSERT(igi != NULL, ("%s: no igi for ifp %p", __func__, ifp));
1375 
1376 		IGMPSTAT_INC(igps_rcv_ourreports);
1377 
1378 		/*
1379 		 * If we are in IGMPv3 host mode, do not allow the
1380 		 * other host's IGMPv1 report to suppress our reports
1381 		 * unless explicitly configured to do so.
1382 		 */
1383 		if (igi->igi_version == IGMP_VERSION_3) {
1384 			if (V_igmp_legacysupp)
1385 				igmp_v3_suppress_group_record(inm);
1386 			goto out_locked;
1387 		}
1388 
1389 		inm->inm_timer = 0;
1390 
1391 		switch (inm->inm_state) {
1392 		case IGMP_NOT_MEMBER:
1393 		case IGMP_SILENT_MEMBER:
1394 		case IGMP_SLEEPING_MEMBER:
1395 			break;
1396 		case IGMP_REPORTING_MEMBER:
1397 		case IGMP_IDLE_MEMBER:
1398 		case IGMP_AWAKENING_MEMBER:
1399 			CTR3(KTR_IGMPV3,
1400 			    "report suppressed for %s on ifp %p(%s)",
1401 			    inet_ntoa(igmp->igmp_group), ifp, ifp->if_xname);
1402 		case IGMP_LAZY_MEMBER:
1403 			inm->inm_state = IGMP_LAZY_MEMBER;
1404 			break;
1405 		case IGMP_G_QUERY_PENDING_MEMBER:
1406 		case IGMP_SG_QUERY_PENDING_MEMBER:
1407 		case IGMP_LEAVING_MEMBER:
1408 			break;
1409 		}
1410 	}
1411 
1412 out_locked:
1413 	IN_MULTI_UNLOCK();
1414 
1415 	return (0);
1416 }
1417 
1418 void
1419 igmp_input(struct mbuf *m, int off)
1420 {
1421 	int iphlen;
1422 	struct ifnet *ifp;
1423 	struct igmp *igmp;
1424 	struct ip *ip;
1425 	int igmplen;
1426 	int minlen;
1427 	int queryver;
1428 
1429 	CTR3(KTR_IGMPV3, "%s: called w/mbuf (%p,%d)", __func__, m, off);
1430 
1431 	ifp = m->m_pkthdr.rcvif;
1432 	INIT_VNET_INET(ifp->if_vnet);
1433 
1434 	IGMPSTAT_INC(igps_rcv_total);
1435 
1436 	ip = mtod(m, struct ip *);
1437 	iphlen = off;
1438 	igmplen = ip->ip_len;
1439 
1440 	/*
1441 	 * Validate lengths.
1442 	 */
1443 	if (igmplen < IGMP_MINLEN) {
1444 		IGMPSTAT_INC(igps_rcv_tooshort);
1445 		m_freem(m);
1446 		return;
1447 	}
1448 
1449 	/*
1450 	 * Always pullup to the minimum size for v1/v2 or v3
1451 	 * to amortize calls to m_pullup().
1452 	 */
1453 	minlen = iphlen;
1454 	if (igmplen >= IGMP_V3_QUERY_MINLEN)
1455 		minlen += IGMP_V3_QUERY_MINLEN;
1456 	else
1457 		minlen += IGMP_MINLEN;
1458 	if ((m->m_flags & M_EXT || m->m_len < minlen) &&
1459 	    (m = m_pullup(m, minlen)) == 0) {
1460 		IGMPSTAT_INC(igps_rcv_tooshort);
1461 		return;
1462 	}
1463 	ip = mtod(m, struct ip *);
1464 
1465 	if (ip->ip_ttl != 1) {
1466 		IGMPSTAT_INC(igps_rcv_badttl);
1467 		m_freem(m);
1468 		return;
1469 	}
1470 
1471 	/*
1472 	 * Validate checksum.
1473 	 */
1474 	m->m_data += iphlen;
1475 	m->m_len -= iphlen;
1476 	igmp = mtod(m, struct igmp *);
1477 	if (in_cksum(m, igmplen)) {
1478 		IGMPSTAT_INC(igps_rcv_badsum);
1479 		m_freem(m);
1480 		return;
1481 	}
1482 	m->m_data -= iphlen;
1483 	m->m_len += iphlen;
1484 
1485 	switch (igmp->igmp_type) {
1486 	case IGMP_HOST_MEMBERSHIP_QUERY:
1487 		if (igmplen == IGMP_MINLEN) {
1488 			if (igmp->igmp_code == 0)
1489 				queryver = IGMP_VERSION_1;
1490 			else
1491 				queryver = IGMP_VERSION_2;
1492 		} else if (igmplen >= IGMP_V3_QUERY_MINLEN) {
1493 			queryver = IGMP_VERSION_3;
1494 		} else {
1495 			IGMPSTAT_INC(igps_rcv_tooshort);
1496 			m_freem(m);
1497 			return;
1498 		}
1499 
1500 		switch (queryver) {
1501 		case IGMP_VERSION_1:
1502 			IGMPSTAT_INC(igps_rcv_v1v2_queries);
1503 			if (!V_igmp_v1enable)
1504 				break;
1505 			if (igmp_input_v1_query(ifp, ip, igmp) != 0) {
1506 				m_freem(m);
1507 				return;
1508 			}
1509 			break;
1510 
1511 		case IGMP_VERSION_2:
1512 			IGMPSTAT_INC(igps_rcv_v1v2_queries);
1513 			if (!V_igmp_v2enable)
1514 				break;
1515 			if (igmp_input_v2_query(ifp, ip, igmp) != 0) {
1516 				m_freem(m);
1517 				return;
1518 			}
1519 			break;
1520 
1521 		case IGMP_VERSION_3: {
1522 				struct igmpv3 *igmpv3;
1523 				uint16_t igmpv3len;
1524 				uint16_t srclen;
1525 				int nsrc;
1526 
1527 				IGMPSTAT_INC(igps_rcv_v3_queries);
1528 				igmpv3 = (struct igmpv3 *)igmp;
1529 				/*
1530 				 * Validate length based on source count.
1531 				 */
1532 				nsrc = ntohs(igmpv3->igmp_numsrc);
1533 				srclen = sizeof(struct in_addr) * nsrc;
1534 				if (nsrc * sizeof(in_addr_t) > srclen) {
1535 					IGMPSTAT_INC(igps_rcv_tooshort);
1536 					return;
1537 				}
1538 				/*
1539 				 * m_pullup() may modify m, so pullup in
1540 				 * this scope.
1541 				 */
1542 				igmpv3len = iphlen + IGMP_V3_QUERY_MINLEN +
1543 				    srclen;
1544 				if ((m->m_flags & M_EXT ||
1545 				     m->m_len < igmpv3len) &&
1546 				    (m = m_pullup(m, igmpv3len)) == NULL) {
1547 					IGMPSTAT_INC(igps_rcv_tooshort);
1548 					return;
1549 				}
1550 				igmpv3 = (struct igmpv3 *)(mtod(m, uint8_t *)
1551 				    + iphlen);
1552 				if (igmp_input_v3_query(ifp, ip, igmpv3) != 0) {
1553 					m_freem(m);
1554 					return;
1555 				}
1556 			}
1557 			break;
1558 		}
1559 		break;
1560 
1561 	case IGMP_v1_HOST_MEMBERSHIP_REPORT:
1562 		if (!V_igmp_v1enable)
1563 			break;
1564 		if (igmp_input_v1_report(ifp, ip, igmp) != 0) {
1565 			m_freem(m);
1566 			return;
1567 		}
1568 		break;
1569 
1570 	case IGMP_v2_HOST_MEMBERSHIP_REPORT:
1571 		if (!V_igmp_v2enable)
1572 			break;
1573 		if (!ip_checkrouteralert(m))
1574 			IGMPSTAT_INC(igps_rcv_nora);
1575 		if (igmp_input_v2_report(ifp, ip, igmp) != 0) {
1576 			m_freem(m);
1577 			return;
1578 		}
1579 		break;
1580 
1581 	case IGMP_v3_HOST_MEMBERSHIP_REPORT:
1582 		/*
1583 		 * Hosts do not need to process IGMPv3 membership reports,
1584 		 * as report suppression is no longer required.
1585 		 */
1586 		if (!ip_checkrouteralert(m))
1587 			IGMPSTAT_INC(igps_rcv_nora);
1588 		break;
1589 
1590 	default:
1591 		break;
1592 	}
1593 
1594 	/*
1595 	 * Pass all valid IGMP packets up to any process(es) listening on a
1596 	 * raw IGMP socket.
1597 	 */
1598 	rip_input(m, off);
1599 }
1600 
1601 
1602 /*
1603  * Fast timeout handler (global).
1604  * VIMAGE: Timeout handlers are expected to service all vimages.
1605  */
1606 void
1607 igmp_fasttimo(void)
1608 {
1609 	VNET_ITERATOR_DECL(vnet_iter);
1610 
1611 	VNET_LIST_RLOCK();
1612 	VNET_FOREACH(vnet_iter) {
1613 		CURVNET_SET(vnet_iter);
1614 		igmp_fasttimo_vnet();
1615 		CURVNET_RESTORE();
1616 	}
1617 	VNET_LIST_RUNLOCK();
1618 }
1619 
1620 /*
1621  * Fast timeout handler (per-vnet).
1622  * Sends are shuffled off to a netisr to deal with Giant.
1623  *
1624  * VIMAGE: Assume caller has set up our curvnet.
1625  */
1626 static void
1627 igmp_fasttimo_vnet(void)
1628 {
1629 	INIT_VNET_INET(curvnet);
1630 	struct ifqueue		 scq;	/* State-change packets */
1631 	struct ifqueue		 qrq;	/* Query response packets */
1632 	struct ifnet		*ifp;
1633 	struct igmp_ifinfo	*igi;
1634 	struct ifmultiaddr	*ifma, *tifma;
1635 	struct in_multi		*inm;
1636 	int			 loop, uri_fasthz;
1637 
1638 	loop = 0;
1639 	uri_fasthz = 0;
1640 
1641 	/*
1642 	 * Quick check to see if any work needs to be done, in order to
1643 	 * minimize the overhead of fasttimo processing.
1644 	 * SMPng: XXX Unlocked reads.
1645 	 */
1646 	if (!V_current_state_timers_running &&
1647 	    !V_interface_timers_running &&
1648 	    !V_state_change_timers_running)
1649 		return;
1650 
1651 	IN_MULTI_LOCK();
1652 	IGMP_LOCK();
1653 
1654 	/*
1655 	 * IGMPv3 General Query response timer processing.
1656 	 */
1657 	if (V_interface_timers_running) {
1658 		CTR1(KTR_IGMPV3, "%s: interface timers running", __func__);
1659 
1660 		V_interface_timers_running = 0;
1661 		LIST_FOREACH(igi, &V_igi_head, igi_link) {
1662 			if (igi->igi_v3_timer == 0) {
1663 				/* Do nothing. */
1664 			} else if (--igi->igi_v3_timer == 0) {
1665 				igmp_v3_dispatch_general_query(igi);
1666 			} else {
1667 				V_interface_timers_running = 1;
1668 			}
1669 		}
1670 	}
1671 
1672 	if (!V_current_state_timers_running &&
1673 	    !V_state_change_timers_running)
1674 		goto out_locked;
1675 
1676 	V_current_state_timers_running = 0;
1677 	V_state_change_timers_running = 0;
1678 
1679 	CTR1(KTR_IGMPV3, "%s: state change timers running", __func__);
1680 
1681 	/*
1682 	 * IGMPv1/v2/v3 host report and state-change timer processing.
1683 	 * Note: Processing a v3 group timer may remove a node.
1684 	 */
1685 	LIST_FOREACH(igi, &V_igi_head, igi_link) {
1686 		ifp = igi->igi_ifp;
1687 
1688 		if (igi->igi_version == IGMP_VERSION_3) {
1689 			loop = (igi->igi_flags & IGIF_LOOPBACK) ? 1 : 0;
1690 			uri_fasthz = IGMP_RANDOM_DELAY(igi->igi_uri *
1691 			    PR_FASTHZ);
1692 
1693 			memset(&qrq, 0, sizeof(struct ifqueue));
1694 			IFQ_SET_MAXLEN(&qrq, IGMP_MAX_G_GS_PACKETS);
1695 
1696 			memset(&scq, 0, sizeof(struct ifqueue));
1697 			IFQ_SET_MAXLEN(&scq, IGMP_MAX_STATE_CHANGE_PACKETS);
1698 		}
1699 
1700 		IF_ADDR_LOCK(ifp);
1701 		TAILQ_FOREACH_SAFE(ifma, &ifp->if_multiaddrs, ifma_link,
1702 		    tifma) {
1703 			if (ifma->ifma_addr->sa_family != AF_INET ||
1704 			    ifma->ifma_protospec == NULL)
1705 				continue;
1706 			inm = (struct in_multi *)ifma->ifma_protospec;
1707 			switch (igi->igi_version) {
1708 			case IGMP_VERSION_1:
1709 			case IGMP_VERSION_2:
1710 				igmp_v1v2_process_group_timer(inm,
1711 				    igi->igi_version);
1712 				break;
1713 			case IGMP_VERSION_3:
1714 				igmp_v3_process_group_timers(igi, &qrq,
1715 				    &scq, inm, uri_fasthz);
1716 				break;
1717 			}
1718 		}
1719 		IF_ADDR_UNLOCK(ifp);
1720 
1721 		if (igi->igi_version == IGMP_VERSION_3) {
1722 			struct in_multi		*tinm;
1723 
1724 			igmp_dispatch_queue(&qrq, 0, loop);
1725 			igmp_dispatch_queue(&scq, 0, loop);
1726 
1727 			/*
1728 			 * Free the in_multi reference(s) for this
1729 			 * IGMP lifecycle.
1730 			 */
1731 			SLIST_FOREACH_SAFE(inm, &igi->igi_relinmhead,
1732 			    inm_nrele, tinm) {
1733 				SLIST_REMOVE_HEAD(&igi->igi_relinmhead,
1734 				    inm_nrele);
1735 				inm_release_locked(inm);
1736 			}
1737 		}
1738 	}
1739 
1740 out_locked:
1741 	IGMP_UNLOCK();
1742 	IN_MULTI_UNLOCK();
1743 }
1744 
1745 /*
1746  * Update host report group timer for IGMPv1/v2.
1747  * Will update the global pending timer flags.
1748  */
1749 static void
1750 igmp_v1v2_process_group_timer(struct in_multi *inm, const int version)
1751 {
1752 	INIT_VNET_INET(curvnet);
1753 	int report_timer_expired;
1754 
1755 	IN_MULTI_LOCK_ASSERT();
1756 	IGMP_LOCK_ASSERT();
1757 
1758 	if (inm->inm_timer == 0) {
1759 		report_timer_expired = 0;
1760 	} else if (--inm->inm_timer == 0) {
1761 		report_timer_expired = 1;
1762 	} else {
1763 		V_current_state_timers_running = 1;
1764 		return;
1765 	}
1766 
1767 	switch (inm->inm_state) {
1768 	case IGMP_NOT_MEMBER:
1769 	case IGMP_SILENT_MEMBER:
1770 	case IGMP_IDLE_MEMBER:
1771 	case IGMP_LAZY_MEMBER:
1772 	case IGMP_SLEEPING_MEMBER:
1773 	case IGMP_AWAKENING_MEMBER:
1774 		break;
1775 	case IGMP_REPORTING_MEMBER:
1776 		if (report_timer_expired) {
1777 			inm->inm_state = IGMP_IDLE_MEMBER;
1778 			(void)igmp_v1v2_queue_report(inm,
1779 			    (version == IGMP_VERSION_2) ?
1780 			     IGMP_v2_HOST_MEMBERSHIP_REPORT :
1781 			     IGMP_v1_HOST_MEMBERSHIP_REPORT);
1782 		}
1783 		break;
1784 	case IGMP_G_QUERY_PENDING_MEMBER:
1785 	case IGMP_SG_QUERY_PENDING_MEMBER:
1786 	case IGMP_LEAVING_MEMBER:
1787 		break;
1788 	}
1789 }
1790 
1791 /*
1792  * Update a group's timers for IGMPv3.
1793  * Will update the global pending timer flags.
1794  * Note: Unlocked read from igi.
1795  */
1796 static void
1797 igmp_v3_process_group_timers(struct igmp_ifinfo *igi,
1798     struct ifqueue *qrq, struct ifqueue *scq,
1799     struct in_multi *inm, const int uri_fasthz)
1800 {
1801 	INIT_VNET_INET(curvnet);
1802 	int query_response_timer_expired;
1803 	int state_change_retransmit_timer_expired;
1804 
1805 	IN_MULTI_LOCK_ASSERT();
1806 	IGMP_LOCK_ASSERT();
1807 
1808 	query_response_timer_expired = 0;
1809 	state_change_retransmit_timer_expired = 0;
1810 
1811 	/*
1812 	 * During a transition from v1/v2 compatibility mode back to v3,
1813 	 * a group record in REPORTING state may still have its group
1814 	 * timer active. This is a no-op in this function; it is easier
1815 	 * to deal with it here than to complicate the slow-timeout path.
1816 	 */
1817 	if (inm->inm_timer == 0) {
1818 		query_response_timer_expired = 0;
1819 	} else if (--inm->inm_timer == 0) {
1820 		query_response_timer_expired = 1;
1821 	} else {
1822 		V_current_state_timers_running = 1;
1823 	}
1824 
1825 	if (inm->inm_sctimer == 0) {
1826 		state_change_retransmit_timer_expired = 0;
1827 	} else if (--inm->inm_sctimer == 0) {
1828 		state_change_retransmit_timer_expired = 1;
1829 	} else {
1830 		V_state_change_timers_running = 1;
1831 	}
1832 
1833 	/* We are in fasttimo, so be quick about it. */
1834 	if (!state_change_retransmit_timer_expired &&
1835 	    !query_response_timer_expired)
1836 		return;
1837 
1838 	switch (inm->inm_state) {
1839 	case IGMP_NOT_MEMBER:
1840 	case IGMP_SILENT_MEMBER:
1841 	case IGMP_SLEEPING_MEMBER:
1842 	case IGMP_LAZY_MEMBER:
1843 	case IGMP_AWAKENING_MEMBER:
1844 	case IGMP_IDLE_MEMBER:
1845 		break;
1846 	case IGMP_G_QUERY_PENDING_MEMBER:
1847 	case IGMP_SG_QUERY_PENDING_MEMBER:
1848 		/*
1849 		 * Respond to a previously pending Group-Specific
1850 		 * or Group-and-Source-Specific query by enqueueing
1851 		 * the appropriate Current-State report for
1852 		 * immediate transmission.
1853 		 */
1854 		if (query_response_timer_expired) {
1855 			int retval;
1856 
1857 			retval = igmp_v3_enqueue_group_record(qrq, inm, 0, 1,
1858 			    (inm->inm_state == IGMP_SG_QUERY_PENDING_MEMBER));
1859 			CTR2(KTR_IGMPV3, "%s: enqueue record = %d",
1860 			    __func__, retval);
1861 			inm->inm_state = IGMP_REPORTING_MEMBER;
1862 			/* XXX Clear recorded sources for next time. */
1863 			inm_clear_recorded(inm);
1864 		}
1865 		/* FALLTHROUGH */
1866 	case IGMP_REPORTING_MEMBER:
1867 	case IGMP_LEAVING_MEMBER:
1868 		if (state_change_retransmit_timer_expired) {
1869 			/*
1870 			 * State-change retransmission timer fired.
1871 			 * If there are any further pending retransmissions,
1872 			 * set the global pending state-change flag, and
1873 			 * reset the timer.
1874 			 */
1875 			if (--inm->inm_scrv > 0) {
1876 				inm->inm_sctimer = uri_fasthz;
1877 				V_state_change_timers_running = 1;
1878 			}
1879 			/*
1880 			 * Retransmit the previously computed state-change
1881 			 * report. If there are no further pending
1882 			 * retransmissions, the mbuf queue will be consumed.
1883 			 * Update T0 state to T1 as we have now sent
1884 			 * a state-change.
1885 			 */
1886 			(void)igmp_v3_merge_state_changes(inm, scq);
1887 
1888 			inm_commit(inm);
1889 			CTR3(KTR_IGMPV3, "%s: T1 -> T0 for %s/%s", __func__,
1890 			    inet_ntoa(inm->inm_addr), inm->inm_ifp->if_xname);
1891 
1892 			/*
1893 			 * If we are leaving the group for good, make sure
1894 			 * we release IGMP's reference to it.
1895 			 * This release must be deferred using a SLIST,
1896 			 * as we are called from a loop which traverses
1897 			 * the in_ifmultiaddr TAILQ.
1898 			 */
1899 			if (inm->inm_state == IGMP_LEAVING_MEMBER &&
1900 			    inm->inm_scrv == 0) {
1901 				inm->inm_state = IGMP_NOT_MEMBER;
1902 				SLIST_INSERT_HEAD(&igi->igi_relinmhead,
1903 				    inm, inm_nrele);
1904 			}
1905 		}
1906 		break;
1907 	}
1908 }
1909 
1910 
1911 /*
1912  * Suppress a group's pending response to a group or source/group query.
1913  *
1914  * Do NOT suppress state changes. This leads to IGMPv3 inconsistency.
1915  * Do NOT update ST1/ST0 as this operation merely suppresses
1916  * the currently pending group record.
1917  * Do NOT suppress the response to a general query. It is possible but
1918  * it would require adding another state or flag.
1919  */
1920 static void
1921 igmp_v3_suppress_group_record(struct in_multi *inm)
1922 {
1923 
1924 	IN_MULTI_LOCK_ASSERT();
1925 
1926 	KASSERT(inm->inm_igi->igi_version == IGMP_VERSION_3,
1927 		("%s: not IGMPv3 mode on link", __func__));
1928 
1929 	if (inm->inm_state != IGMP_G_QUERY_PENDING_MEMBER ||
1930 	    inm->inm_state != IGMP_SG_QUERY_PENDING_MEMBER)
1931 		return;
1932 
1933 	if (inm->inm_state == IGMP_SG_QUERY_PENDING_MEMBER)
1934 		inm_clear_recorded(inm);
1935 
1936 	inm->inm_timer = 0;
1937 	inm->inm_state = IGMP_REPORTING_MEMBER;
1938 }
1939 
1940 /*
1941  * Switch to a different IGMP version on the given interface,
1942  * as per Section 7.2.1.
1943  */
1944 static void
1945 igmp_set_version(struct igmp_ifinfo *igi, const int version)
1946 {
1947 	int old_version_timer;
1948 
1949 	IGMP_LOCK_ASSERT();
1950 
1951 	CTR4(KTR_IGMPV3, "%s: switching to v%d on ifp %p(%s)", __func__,
1952 	    version, igi->igi_ifp, igi->igi_ifp->if_xname);
1953 
1954 	if (version == IGMP_VERSION_1 || version == IGMP_VERSION_2) {
1955 		/*
1956 		 * Compute the "Older Version Querier Present" timer as per
1957 		 * Section 8.12.
1958 		 */
1959 		old_version_timer = igi->igi_rv * igi->igi_qi + igi->igi_qri;
1960 		old_version_timer *= PR_SLOWHZ;
1961 
1962 		if (version == IGMP_VERSION_1) {
1963 			igi->igi_v1_timer = old_version_timer;
1964 			igi->igi_v2_timer = 0;
1965 		} else if (version == IGMP_VERSION_2) {
1966 			igi->igi_v1_timer = 0;
1967 			igi->igi_v2_timer = old_version_timer;
1968 		}
1969 	}
1970 
1971 	if (igi->igi_v1_timer == 0 && igi->igi_v2_timer > 0) {
1972 		if (igi->igi_version != IGMP_VERSION_2) {
1973 			igi->igi_version = IGMP_VERSION_2;
1974 			igmp_v3_cancel_link_timers(igi);
1975 		}
1976 	} else if (igi->igi_v1_timer > 0) {
1977 		if (igi->igi_version != IGMP_VERSION_1) {
1978 			igi->igi_version = IGMP_VERSION_1;
1979 			igmp_v3_cancel_link_timers(igi);
1980 		}
1981 	}
1982 }
1983 
1984 /*
1985  * Cancel pending IGMPv3 timers for the given link and all groups
1986  * joined on it; state-change, general-query, and group-query timers.
1987  *
1988  * Only ever called on a transition from v3 to Compatibility mode. Kill
1989  * the timers stone dead (this may be expensive for large N groups), they
1990  * will be restarted if Compatibility Mode deems that they must be due to
1991  * query processing.
1992  */
1993 static void
1994 igmp_v3_cancel_link_timers(struct igmp_ifinfo *igi)
1995 {
1996 	INIT_VNET_INET(curvnet);
1997 	struct ifmultiaddr	*ifma;
1998 	struct ifnet		*ifp;
1999 	struct in_multi		*inm;
2000 
2001 	CTR3(KTR_IGMPV3, "%s: cancel v3 timers on ifp %p(%s)", __func__,
2002 	    igi->igi_ifp, igi->igi_ifp->if_xname);
2003 
2004 	IN_MULTI_LOCK_ASSERT();
2005 	IGMP_LOCK_ASSERT();
2006 
2007 	/*
2008 	 * Stop the v3 General Query Response on this link stone dead.
2009 	 * If fasttimo is woken up due to V_interface_timers_running,
2010 	 * the flag will be cleared if there are no pending link timers.
2011 	 */
2012 	igi->igi_v3_timer = 0;
2013 
2014 	/*
2015 	 * Now clear the current-state and state-change report timers
2016 	 * for all memberships scoped to this link.
2017 	 */
2018 	ifp = igi->igi_ifp;
2019 	IF_ADDR_LOCK(ifp);
2020 	TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
2021 		if (ifma->ifma_addr->sa_family != AF_INET ||
2022 		    ifma->ifma_protospec == NULL)
2023 			continue;
2024 		inm = (struct in_multi *)ifma->ifma_protospec;
2025 		switch (inm->inm_state) {
2026 		case IGMP_NOT_MEMBER:
2027 		case IGMP_SILENT_MEMBER:
2028 		case IGMP_IDLE_MEMBER:
2029 		case IGMP_LAZY_MEMBER:
2030 		case IGMP_SLEEPING_MEMBER:
2031 		case IGMP_AWAKENING_MEMBER:
2032 			/*
2033 			 * These states are either not relevant in v3 mode,
2034 			 * or are unreported. Do nothing.
2035 			 */
2036 			break;
2037 		case IGMP_LEAVING_MEMBER:
2038 			/*
2039 			 * If we are leaving the group and switching to
2040 			 * compatibility mode, we need to release the final
2041 			 * reference held for issuing the INCLUDE {}, and
2042 			 * transition to REPORTING to ensure the host leave
2043 			 * message is sent upstream to the old querier --
2044 			 * transition to NOT would lose the leave and race.
2045 			 *
2046 			 * SMPNG: Must drop and re-acquire IF_ADDR_LOCK
2047 			 * around inm_release_locked(), as it is not
2048 			 * a recursive mutex.
2049 			 */
2050 			IF_ADDR_UNLOCK(ifp);
2051 			inm_release_locked(inm);
2052 			IF_ADDR_LOCK(ifp);
2053 			/* FALLTHROUGH */
2054 		case IGMP_G_QUERY_PENDING_MEMBER:
2055 		case IGMP_SG_QUERY_PENDING_MEMBER:
2056 			inm_clear_recorded(inm);
2057 			/* FALLTHROUGH */
2058 		case IGMP_REPORTING_MEMBER:
2059 			inm->inm_state = IGMP_REPORTING_MEMBER;
2060 			break;
2061 		}
2062 		/*
2063 		 * Always clear state-change and group report timers.
2064 		 * Free any pending IGMPv3 state-change records.
2065 		 */
2066 		inm->inm_sctimer = 0;
2067 		inm->inm_timer = 0;
2068 		_IF_DRAIN(&inm->inm_scq);
2069 	}
2070 	IF_ADDR_UNLOCK(ifp);
2071 }
2072 
2073 /*
2074  * Update the Older Version Querier Present timers for a link.
2075  * See Section 7.2.1 of RFC 3376.
2076  */
2077 static void
2078 igmp_v1v2_process_querier_timers(struct igmp_ifinfo *igi)
2079 {
2080 	INIT_VNET_INET(curvnet);
2081 
2082 	IGMP_LOCK_ASSERT();
2083 
2084 	if (igi->igi_v1_timer == 0 && igi->igi_v2_timer == 0) {
2085 		/*
2086 		 * IGMPv1 and IGMPv2 Querier Present timers expired.
2087 		 *
2088 		 * Revert to IGMPv3.
2089 		 */
2090 		if (igi->igi_version != IGMP_VERSION_3) {
2091 			CTR5(KTR_IGMPV3,
2092 			    "%s: transition from v%d -> v%d on %p(%s)",
2093 			    __func__, igi->igi_version, IGMP_VERSION_3,
2094 			    igi->igi_ifp, igi->igi_ifp->if_xname);
2095 			igi->igi_version = IGMP_VERSION_3;
2096 		}
2097 	} else if (igi->igi_v1_timer == 0 && igi->igi_v2_timer > 0) {
2098 		/*
2099 		 * IGMPv1 Querier Present timer expired,
2100 		 * IGMPv2 Querier Present timer running.
2101 		 * If IGMPv2 was disabled since last timeout,
2102 		 * revert to IGMPv3.
2103 		 * If IGMPv2 is enabled, revert to IGMPv2.
2104 		 */
2105 		if (!V_igmp_v2enable) {
2106 			CTR5(KTR_IGMPV3,
2107 			    "%s: transition from v%d -> v%d on %p(%s)",
2108 			    __func__, igi->igi_version, IGMP_VERSION_3,
2109 			    igi->igi_ifp, igi->igi_ifp->if_xname);
2110 			igi->igi_v2_timer = 0;
2111 			igi->igi_version = IGMP_VERSION_3;
2112 		} else {
2113 			--igi->igi_v2_timer;
2114 			if (igi->igi_version != IGMP_VERSION_2) {
2115 				CTR5(KTR_IGMPV3,
2116 				    "%s: transition from v%d -> v%d on %p(%s)",
2117 				    __func__, igi->igi_version, IGMP_VERSION_2,
2118 				    igi->igi_ifp, igi->igi_ifp->if_xname);
2119 				igi->igi_version = IGMP_VERSION_2;
2120 			}
2121 		}
2122 	} else if (igi->igi_v1_timer > 0) {
2123 		/*
2124 		 * IGMPv1 Querier Present timer running.
2125 		 * Stop IGMPv2 timer if running.
2126 		 *
2127 		 * If IGMPv1 was disabled since last timeout,
2128 		 * revert to IGMPv3.
2129 		 * If IGMPv1 is enabled, reset IGMPv2 timer if running.
2130 		 */
2131 		if (!V_igmp_v1enable) {
2132 			CTR5(KTR_IGMPV3,
2133 			    "%s: transition from v%d -> v%d on %p(%s)",
2134 			    __func__, igi->igi_version, IGMP_VERSION_3,
2135 			    igi->igi_ifp, igi->igi_ifp->if_xname);
2136 			igi->igi_v1_timer = 0;
2137 			igi->igi_version = IGMP_VERSION_3;
2138 		} else {
2139 			--igi->igi_v1_timer;
2140 		}
2141 		if (igi->igi_v2_timer > 0) {
2142 			CTR3(KTR_IGMPV3,
2143 			    "%s: cancel v2 timer on %p(%s)",
2144 			    __func__, igi->igi_ifp, igi->igi_ifp->if_xname);
2145 			igi->igi_v2_timer = 0;
2146 		}
2147 	}
2148 }
2149 
2150 /*
2151  * Global slowtimo handler.
2152  * VIMAGE: Timeout handlers are expected to service all vimages.
2153  */
2154 void
2155 igmp_slowtimo(void)
2156 {
2157 	VNET_ITERATOR_DECL(vnet_iter);
2158 
2159 	VNET_LIST_RLOCK();
2160 	VNET_FOREACH(vnet_iter) {
2161 		CURVNET_SET(vnet_iter);
2162 		igmp_slowtimo_vnet();
2163 		CURVNET_RESTORE();
2164 	}
2165 	VNET_LIST_RUNLOCK();
2166 }
2167 
2168 /*
2169  * Per-vnet slowtimo handler.
2170  */
2171 static void
2172 igmp_slowtimo_vnet(void)
2173 {
2174 	INIT_VNET_INET(curvnet);
2175 	struct igmp_ifinfo *igi;
2176 
2177 	IGMP_LOCK();
2178 
2179 	LIST_FOREACH(igi, &V_igi_head, igi_link) {
2180 		igmp_v1v2_process_querier_timers(igi);
2181 	}
2182 
2183 	IGMP_UNLOCK();
2184 }
2185 
2186 /*
2187  * Dispatch an IGMPv1/v2 host report or leave message.
2188  * These are always small enough to fit inside a single mbuf.
2189  */
2190 static int
2191 igmp_v1v2_queue_report(struct in_multi *inm, const int type)
2192 {
2193 	struct ifnet		*ifp;
2194 	struct igmp		*igmp;
2195 	struct ip		*ip;
2196 	struct mbuf		*m;
2197 
2198 	IN_MULTI_LOCK_ASSERT();
2199 	IGMP_LOCK_ASSERT();
2200 
2201 	ifp = inm->inm_ifp;
2202 
2203 	MGETHDR(m, M_DONTWAIT, MT_DATA);
2204 	if (m == NULL)
2205 		return (ENOMEM);
2206 	MH_ALIGN(m, sizeof(struct ip) + sizeof(struct igmp));
2207 
2208 	m->m_pkthdr.len = sizeof(struct ip) + sizeof(struct igmp);
2209 
2210 	m->m_data += sizeof(struct ip);
2211 	m->m_len = sizeof(struct igmp);
2212 
2213 	igmp = mtod(m, struct igmp *);
2214 	igmp->igmp_type = type;
2215 	igmp->igmp_code = 0;
2216 	igmp->igmp_group = inm->inm_addr;
2217 	igmp->igmp_cksum = 0;
2218 	igmp->igmp_cksum = in_cksum(m, sizeof(struct igmp));
2219 
2220 	m->m_data -= sizeof(struct ip);
2221 	m->m_len += sizeof(struct ip);
2222 
2223 	ip = mtod(m, struct ip *);
2224 	ip->ip_tos = 0;
2225 	ip->ip_len = sizeof(struct ip) + sizeof(struct igmp);
2226 	ip->ip_off = 0;
2227 	ip->ip_p = IPPROTO_IGMP;
2228 	ip->ip_src.s_addr = INADDR_ANY;
2229 
2230 	if (type == IGMP_HOST_LEAVE_MESSAGE)
2231 		ip->ip_dst.s_addr = htonl(INADDR_ALLRTRS_GROUP);
2232 	else
2233 		ip->ip_dst = inm->inm_addr;
2234 
2235 	igmp_save_context(m, ifp);
2236 
2237 	m->m_flags |= M_IGMPV2;
2238 	if (inm->inm_igi->igi_flags & IGIF_LOOPBACK)
2239 		m->m_flags |= M_IGMP_LOOP;
2240 
2241 	CTR2(KTR_IGMPV3, "%s: netisr_dispatch(NETISR_IGMP, %p)", __func__, m);
2242 	netisr_dispatch(NETISR_IGMP, m);
2243 
2244 	return (0);
2245 }
2246 
2247 /*
2248  * Process a state change from the upper layer for the given IPv4 group.
2249  *
2250  * Each socket holds a reference on the in_multi in its own ip_moptions.
2251  * The socket layer will have made the necessary updates to.the group
2252  * state, it is now up to IGMP to issue a state change report if there
2253  * has been any change between T0 (when the last state-change was issued)
2254  * and T1 (now).
2255  *
2256  * We use the IGMPv3 state machine at group level. The IGMP module
2257  * however makes the decision as to which IGMP protocol version to speak.
2258  * A state change *from* INCLUDE {} always means an initial join.
2259  * A state change *to* INCLUDE {} always means a final leave.
2260  *
2261  * FUTURE: If IGIF_V3LITE is enabled for this interface, then we can
2262  * save ourselves a bunch of work; any exclusive mode groups need not
2263  * compute source filter lists.
2264  *
2265  * VIMAGE: curvnet should have been set by caller, as this routine
2266  * is called from the socket option handlers.
2267  */
2268 int
2269 igmp_change_state(struct in_multi *inm)
2270 {
2271 	struct igmp_ifinfo *igi;
2272 	struct ifnet *ifp;
2273 	int error;
2274 
2275 	IN_MULTI_LOCK_ASSERT();
2276 
2277 	error = 0;
2278 
2279 	/*
2280 	 * Try to detect if the upper layer just asked us to change state
2281 	 * for an interface which has now gone away.
2282 	 */
2283 	KASSERT(inm->inm_ifma != NULL, ("%s: no ifma", __func__));
2284 	ifp = inm->inm_ifma->ifma_ifp;
2285 	if (ifp != NULL) {
2286 		/*
2287 		 * Sanity check that netinet's notion of ifp is the
2288 		 * same as net's.
2289 		 */
2290 		KASSERT(inm->inm_ifp == ifp, ("%s: bad ifp", __func__));
2291 	}
2292 
2293 	IGMP_LOCK();
2294 
2295 	igi = ((struct in_ifinfo *)ifp->if_afdata[AF_INET])->ii_igmp;
2296 	KASSERT(igi != NULL, ("%s: no igmp_ifinfo for ifp %p", __func__, ifp));
2297 
2298 	/*
2299 	 * If we detect a state transition to or from MCAST_UNDEFINED
2300 	 * for this group, then we are starting or finishing an IGMP
2301 	 * life cycle for this group.
2302 	 */
2303 	if (inm->inm_st[1].iss_fmode != inm->inm_st[0].iss_fmode) {
2304 		CTR3(KTR_IGMPV3, "%s: inm transition %d -> %d", __func__,
2305 		    inm->inm_st[0].iss_fmode, inm->inm_st[1].iss_fmode);
2306 		if (inm->inm_st[0].iss_fmode == MCAST_UNDEFINED) {
2307 			CTR1(KTR_IGMPV3, "%s: initial join", __func__);
2308 			error = igmp_initial_join(inm, igi);
2309 			goto out_locked;
2310 		} else if (inm->inm_st[1].iss_fmode == MCAST_UNDEFINED) {
2311 			CTR1(KTR_IGMPV3, "%s: final leave", __func__);
2312 			igmp_final_leave(inm, igi);
2313 			goto out_locked;
2314 		}
2315 	} else {
2316 		CTR1(KTR_IGMPV3, "%s: filter set change", __func__);
2317 	}
2318 
2319 	error = igmp_handle_state_change(inm, igi);
2320 
2321 out_locked:
2322 	IGMP_UNLOCK();
2323 	return (error);
2324 }
2325 
2326 /*
2327  * Perform the initial join for an IGMP group.
2328  *
2329  * When joining a group:
2330  *  If the group should have its IGMP traffic suppressed, do nothing.
2331  *  IGMPv1 starts sending IGMPv1 host membership reports.
2332  *  IGMPv2 starts sending IGMPv2 host membership reports.
2333  *  IGMPv3 will schedule an IGMPv3 state-change report containing the
2334  *  initial state of the membership.
2335  */
2336 static int
2337 igmp_initial_join(struct in_multi *inm, struct igmp_ifinfo *igi)
2338 {
2339 	INIT_VNET_INET(curvnet);
2340 	struct ifnet		*ifp;
2341 	struct ifqueue		*ifq;
2342 	int			 error, retval, syncstates;
2343 
2344 	CTR4(KTR_IGMPV3, "%s: initial join %s on ifp %p(%s)",
2345 	    __func__, inet_ntoa(inm->inm_addr), inm->inm_ifp,
2346 	    inm->inm_ifp->if_xname);
2347 
2348 	error = 0;
2349 	syncstates = 1;
2350 
2351 	ifp = inm->inm_ifp;
2352 
2353 	IN_MULTI_LOCK_ASSERT();
2354 	IGMP_LOCK_ASSERT();
2355 
2356 	KASSERT(igi && igi->igi_ifp == ifp, ("%s: inconsistent ifp", __func__));
2357 
2358 	/*
2359 	 * Groups joined on loopback or marked as 'not reported',
2360 	 * e.g. 224.0.0.1, enter the IGMP_SILENT_MEMBER state and
2361 	 * are never reported in any IGMP protocol exchanges.
2362 	 * All other groups enter the appropriate IGMP state machine
2363 	 * for the version in use on this link.
2364 	 * A link marked as IGIF_SILENT causes IGMP to be completely
2365 	 * disabled for the link.
2366 	 */
2367 	if ((ifp->if_flags & IFF_LOOPBACK) ||
2368 	    (igi->igi_flags & IGIF_SILENT) ||
2369 	    !igmp_isgroupreported(inm->inm_addr)) {
2370 		CTR1(KTR_IGMPV3,
2371 "%s: not kicking state machine for silent group", __func__);
2372 		inm->inm_state = IGMP_SILENT_MEMBER;
2373 		inm->inm_timer = 0;
2374 	} else {
2375 		/*
2376 		 * Deal with overlapping in_multi lifecycle.
2377 		 * If this group was LEAVING, then make sure
2378 		 * we drop the reference we picked up to keep the
2379 		 * group around for the final INCLUDE {} enqueue.
2380 		 */
2381 		if (igi->igi_version == IGMP_VERSION_3 &&
2382 		    inm->inm_state == IGMP_LEAVING_MEMBER)
2383 			inm_release_locked(inm);
2384 
2385 		inm->inm_state = IGMP_REPORTING_MEMBER;
2386 
2387 		switch (igi->igi_version) {
2388 		case IGMP_VERSION_1:
2389 		case IGMP_VERSION_2:
2390 			inm->inm_state = IGMP_IDLE_MEMBER;
2391 			error = igmp_v1v2_queue_report(inm,
2392 			    (igi->igi_version == IGMP_VERSION_2) ?
2393 			     IGMP_v2_HOST_MEMBERSHIP_REPORT :
2394 			     IGMP_v1_HOST_MEMBERSHIP_REPORT);
2395 			if (error == 0) {
2396 				inm->inm_timer = IGMP_RANDOM_DELAY(
2397 				    IGMP_V1V2_MAX_RI * PR_FASTHZ);
2398 				V_current_state_timers_running = 1;
2399 			}
2400 			break;
2401 
2402 		case IGMP_VERSION_3:
2403 			/*
2404 			 * Defer update of T0 to T1, until the first copy
2405 			 * of the state change has been transmitted.
2406 			 */
2407 			syncstates = 0;
2408 
2409 			/*
2410 			 * Immediately enqueue a State-Change Report for
2411 			 * this interface, freeing any previous reports.
2412 			 * Don't kick the timers if there is nothing to do,
2413 			 * or if an error occurred.
2414 			 */
2415 			ifq = &inm->inm_scq;
2416 			_IF_DRAIN(ifq);
2417 			retval = igmp_v3_enqueue_group_record(ifq, inm, 1,
2418 			    0, 0);
2419 			CTR2(KTR_IGMPV3, "%s: enqueue record = %d",
2420 			    __func__, retval);
2421 			if (retval <= 0) {
2422 				error = retval * -1;
2423 				break;
2424 			}
2425 
2426 			/*
2427 			 * Schedule transmission of pending state-change
2428 			 * report up to RV times for this link. The timer
2429 			 * will fire at the next igmp_fasttimo (~200ms),
2430 			 * giving us an opportunity to merge the reports.
2431 			 */
2432 			if (igi->igi_flags & IGIF_LOOPBACK) {
2433 				inm->inm_scrv = 1;
2434 			} else {
2435 				KASSERT(igi->igi_rv > 1,
2436 				   ("%s: invalid robustness %d", __func__,
2437 				    igi->igi_rv));
2438 				inm->inm_scrv = igi->igi_rv;
2439 			}
2440 			inm->inm_sctimer = 1;
2441 			V_state_change_timers_running = 1;
2442 
2443 			error = 0;
2444 			break;
2445 		}
2446 	}
2447 
2448 	/*
2449 	 * Only update the T0 state if state change is atomic,
2450 	 * i.e. we don't need to wait for a timer to fire before we
2451 	 * can consider the state change to have been communicated.
2452 	 */
2453 	if (syncstates) {
2454 		inm_commit(inm);
2455 		CTR3(KTR_IGMPV3, "%s: T1 -> T0 for %s/%s", __func__,
2456 		    inet_ntoa(inm->inm_addr), inm->inm_ifp->if_xname);
2457 	}
2458 
2459 	return (error);
2460 }
2461 
2462 /*
2463  * Issue an intermediate state change during the IGMP life-cycle.
2464  */
2465 static int
2466 igmp_handle_state_change(struct in_multi *inm, struct igmp_ifinfo *igi)
2467 {
2468 	INIT_VNET_INET(curvnet);
2469 	struct ifnet		*ifp;
2470 	int			 retval;
2471 
2472 	CTR4(KTR_IGMPV3, "%s: state change for %s on ifp %p(%s)",
2473 	    __func__, inet_ntoa(inm->inm_addr), inm->inm_ifp,
2474 	    inm->inm_ifp->if_xname);
2475 
2476 	ifp = inm->inm_ifp;
2477 
2478 	IN_MULTI_LOCK_ASSERT();
2479 	IGMP_LOCK_ASSERT();
2480 
2481 	KASSERT(igi && igi->igi_ifp == ifp, ("%s: inconsistent ifp", __func__));
2482 
2483 	if ((ifp->if_flags & IFF_LOOPBACK) ||
2484 	    (igi->igi_flags & IGIF_SILENT) ||
2485 	    !igmp_isgroupreported(inm->inm_addr) ||
2486 	    (igi->igi_version != IGMP_VERSION_3)) {
2487 		if (!igmp_isgroupreported(inm->inm_addr)) {
2488 			CTR1(KTR_IGMPV3,
2489 "%s: not kicking state machine for silent group", __func__);
2490 		}
2491 		CTR1(KTR_IGMPV3, "%s: nothing to do", __func__);
2492 		inm_commit(inm);
2493 		CTR3(KTR_IGMPV3, "%s: T1 -> T0 for %s/%s", __func__,
2494 		    inet_ntoa(inm->inm_addr), inm->inm_ifp->if_xname);
2495 		return (0);
2496 	}
2497 
2498 	_IF_DRAIN(&inm->inm_scq);
2499 
2500 	retval = igmp_v3_enqueue_group_record(&inm->inm_scq, inm, 1, 0, 0);
2501 	CTR2(KTR_IGMPV3, "%s: enqueue record = %d", __func__, retval);
2502 	if (retval <= 0)
2503 		return (-retval);
2504 
2505 	/*
2506 	 * If record(s) were enqueued, start the state-change
2507 	 * report timer for this group.
2508 	 */
2509 	inm->inm_scrv = ((igi->igi_flags & IGIF_LOOPBACK) ? 1 : igi->igi_rv);
2510 	inm->inm_sctimer = 1;
2511 	V_state_change_timers_running = 1;
2512 
2513 	return (0);
2514 }
2515 
2516 /*
2517  * Perform the final leave for an IGMP group.
2518  *
2519  * When leaving a group:
2520  *  IGMPv1 does nothing.
2521  *  IGMPv2 sends a host leave message, if and only if we are the reporter.
2522  *  IGMPv3 enqueues a state-change report containing a transition
2523  *  to INCLUDE {} for immediate transmission.
2524  */
2525 static void
2526 igmp_final_leave(struct in_multi *inm, struct igmp_ifinfo *igi)
2527 {
2528 	INIT_VNET_INET(curvnet);
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_DONTWAIT, MT_DATA, M_PKTHDR);
2786 			if (m)
2787 				m->m_data += IGMP_LEADINGSPACE;
2788 		}
2789 		if (m == NULL) {
2790 			m = m_gethdr(M_DONTWAIT, 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_DONTWAIT, MT_DATA, M_PKTHDR);
2911 		if (m)
2912 			m->m_data += IGMP_LEADINGSPACE;
2913 		if (m == NULL) {
2914 			m = m_gethdr(M_DONTWAIT, 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_DONTWAIT, MT_DATA, M_PKTHDR);
3067 				if (m)
3068 					m->m_data += IGMP_LEADINGSPACE;
3069 				if (m == NULL) {
3070 					m = m_gethdr(M_DONTWAIT, 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 	INIT_VNET_INET(curvnet);
3322 	struct ifmultiaddr	*ifma, *tifma;
3323 	struct ifnet		*ifp;
3324 	struct in_multi		*inm;
3325 	int			 retval, loop;
3326 
3327 	IN_MULTI_LOCK_ASSERT();
3328 	IGMP_LOCK_ASSERT();
3329 
3330 	KASSERT(igi->igi_version == IGMP_VERSION_3,
3331 	    ("%s: called when version %d", __func__, igi->igi_version));
3332 
3333 	ifp = igi->igi_ifp;
3334 
3335 	IF_ADDR_LOCK(ifp);
3336 	TAILQ_FOREACH_SAFE(ifma, &ifp->if_multiaddrs, ifma_link, tifma) {
3337 		if (ifma->ifma_addr->sa_family != AF_INET ||
3338 		    ifma->ifma_protospec == NULL)
3339 			continue;
3340 
3341 		inm = (struct in_multi *)ifma->ifma_protospec;
3342 		KASSERT(ifp == inm->inm_ifp,
3343 		    ("%s: inconsistent ifp", __func__));
3344 
3345 		switch (inm->inm_state) {
3346 		case IGMP_NOT_MEMBER:
3347 		case IGMP_SILENT_MEMBER:
3348 			break;
3349 		case IGMP_REPORTING_MEMBER:
3350 		case IGMP_IDLE_MEMBER:
3351 		case IGMP_LAZY_MEMBER:
3352 		case IGMP_SLEEPING_MEMBER:
3353 		case IGMP_AWAKENING_MEMBER:
3354 			inm->inm_state = IGMP_REPORTING_MEMBER;
3355 			retval = igmp_v3_enqueue_group_record(&igi->igi_gq,
3356 			    inm, 0, 0, 0);
3357 			CTR2(KTR_IGMPV3, "%s: enqueue record = %d",
3358 			    __func__, retval);
3359 			break;
3360 		case IGMP_G_QUERY_PENDING_MEMBER:
3361 		case IGMP_SG_QUERY_PENDING_MEMBER:
3362 		case IGMP_LEAVING_MEMBER:
3363 			break;
3364 		}
3365 	}
3366 	IF_ADDR_UNLOCK(ifp);
3367 
3368 	loop = (igi->igi_flags & IGIF_LOOPBACK) ? 1 : 0;
3369 	igmp_dispatch_queue(&igi->igi_gq, IGMP_MAX_RESPONSE_BURST, loop);
3370 
3371 	/*
3372 	 * Slew transmission of bursts over 500ms intervals.
3373 	 */
3374 	if (igi->igi_gq.ifq_head != NULL) {
3375 		igi->igi_v3_timer = 1 + IGMP_RANDOM_DELAY(
3376 		    IGMP_RESPONSE_BURST_INTERVAL);
3377 		V_interface_timers_running = 1;
3378 	}
3379 }
3380 
3381 /*
3382  * Transmit the next pending IGMP message in the output queue.
3383  *
3384  * We get called from netisr_processqueue(). A mutex private to igmpoq
3385  * will be acquired and released around this routine.
3386  *
3387  * VIMAGE: Needs to store/restore vnet pointer on a per-mbuf-chain basis.
3388  * MRT: Nothing needs to be done, as IGMP traffic is always local to
3389  * a link and uses a link-scope multicast address.
3390  */
3391 static void
3392 igmp_intr(struct mbuf *m)
3393 {
3394 	struct ip_moptions	 imo;
3395 	struct ifnet		*ifp;
3396 	struct mbuf		*ipopts, *m0;
3397 	int			 error;
3398 	uint32_t		 ifindex;
3399 
3400 	CTR2(KTR_IGMPV3, "%s: transmit %p", __func__, m);
3401 
3402 	/*
3403 	 * Set VNET image pointer from enqueued mbuf chain
3404 	 * before doing anything else. Whilst we use interface
3405 	 * indexes to guard against interface detach, they are
3406 	 * unique to each VIMAGE and must be retrieved.
3407 	 */
3408 	CURVNET_SET((struct vnet *)(m->m_pkthdr.header));
3409 	INIT_VNET_NET(curvnet);
3410 	INIT_VNET_INET(curvnet);
3411 	ifindex = igmp_restore_context(m);
3412 
3413 	/*
3414 	 * Check if the ifnet still exists. This limits the scope of
3415 	 * any race in the absence of a global ifp lock for low cost
3416 	 * (an array lookup).
3417 	 */
3418 	ifp = ifnet_byindex(ifindex);
3419 	if (ifp == NULL) {
3420 		CTR3(KTR_IGMPV3, "%s: dropped %p as ifindex %u went away.",
3421 		    __func__, m, ifindex);
3422 		m_freem(m);
3423 		IPSTAT_INC(ips_noroute);
3424 		goto out;
3425 	}
3426 
3427 	ipopts = V_igmp_sendra ? m_raopt : NULL;
3428 
3429 	imo.imo_multicast_ttl  = 1;
3430 	imo.imo_multicast_vif  = -1;
3431 	imo.imo_multicast_loop = (V_ip_mrouter != NULL);
3432 
3433 	/*
3434 	 * If the user requested that IGMP traffic be explicitly
3435 	 * redirected to the loopback interface (e.g. they are running a
3436 	 * MANET interface and the routing protocol needs to see the
3437 	 * updates), handle this now.
3438 	 */
3439 	if (m->m_flags & M_IGMP_LOOP)
3440 		imo.imo_multicast_ifp = V_loif;
3441 	else
3442 		imo.imo_multicast_ifp = ifp;
3443 
3444 	if (m->m_flags & M_IGMPV2) {
3445 		m0 = m;
3446 	} else {
3447 		m0 = igmp_v3_encap_report(ifp, m);
3448 		if (m0 == NULL) {
3449 			CTR2(KTR_IGMPV3, "%s: dropped %p", __func__, m);
3450 			m_freem(m);
3451 			IPSTAT_INC(ips_odropped);
3452 			goto out;
3453 		}
3454 	}
3455 
3456 	igmp_scrub_context(m0);
3457 	m->m_flags &= ~(M_PROTOFLAGS);
3458 	m0->m_pkthdr.rcvif = V_loif;
3459 #ifdef MAC
3460 	mac_netinet_igmp_send(ifp, m0);
3461 #endif
3462 	error = ip_output(m0, ipopts, NULL, 0, &imo, NULL);
3463 	if (error) {
3464 		CTR3(KTR_IGMPV3, "%s: ip_output(%p) = %d", __func__, m0, error);
3465 		goto out;
3466 	}
3467 
3468 	IGMPSTAT_INC(igps_snd_reports);
3469 
3470 out:
3471 	/*
3472 	 * We must restore the existing vnet pointer before
3473 	 * continuing as we are run from netisr context.
3474 	 */
3475 	CURVNET_RESTORE();
3476 }
3477 
3478 /*
3479  * Encapsulate an IGMPv3 report.
3480  *
3481  * The internal mbuf flag M_IGMPV3_HDR is used to indicate that the mbuf
3482  * chain has already had its IP/IGMPv3 header prepended. In this case
3483  * the function will not attempt to prepend; the lengths and checksums
3484  * will however be re-computed.
3485  *
3486  * Returns a pointer to the new mbuf chain head, or NULL if the
3487  * allocation failed.
3488  */
3489 static struct mbuf *
3490 igmp_v3_encap_report(struct ifnet *ifp, struct mbuf *m)
3491 {
3492 	INIT_VNET_INET(curvnet);
3493 	struct igmp_report	*igmp;
3494 	struct ip		*ip;
3495 	int			 hdrlen, igmpreclen;
3496 
3497 	KASSERT((m->m_flags & M_PKTHDR),
3498 	    ("%s: mbuf chain %p is !M_PKTHDR", __func__, m));
3499 
3500 	igmpreclen = m_length(m, NULL);
3501 	hdrlen = sizeof(struct ip) + sizeof(struct igmp_report);
3502 
3503 	if (m->m_flags & M_IGMPV3_HDR) {
3504 		igmpreclen -= hdrlen;
3505 	} else {
3506 		M_PREPEND(m, hdrlen, M_DONTWAIT);
3507 		if (m == NULL)
3508 			return (NULL);
3509 		m->m_flags |= M_IGMPV3_HDR;
3510 	}
3511 
3512 	CTR2(KTR_IGMPV3, "%s: igmpreclen is %d", __func__, igmpreclen);
3513 
3514 	m->m_data += sizeof(struct ip);
3515 	m->m_len -= sizeof(struct ip);
3516 
3517 	igmp = mtod(m, struct igmp_report *);
3518 	igmp->ir_type = IGMP_v3_HOST_MEMBERSHIP_REPORT;
3519 	igmp->ir_rsv1 = 0;
3520 	igmp->ir_rsv2 = 0;
3521 	igmp->ir_numgrps = htons(m->m_pkthdr.PH_vt.vt_nrecs);
3522 	igmp->ir_cksum = 0;
3523 	igmp->ir_cksum = in_cksum(m, sizeof(struct igmp_report) + igmpreclen);
3524 	m->m_pkthdr.PH_vt.vt_nrecs = 0;
3525 
3526 	m->m_data -= sizeof(struct ip);
3527 	m->m_len += sizeof(struct ip);
3528 
3529 	ip = mtod(m, struct ip *);
3530 	ip->ip_tos = IPTOS_PREC_INTERNETCONTROL;
3531 	ip->ip_len = hdrlen + igmpreclen;
3532 	ip->ip_off = IP_DF;
3533 	ip->ip_p = IPPROTO_IGMP;
3534 	ip->ip_sum = 0;
3535 
3536 	ip->ip_src.s_addr = INADDR_ANY;
3537 
3538 	if (m->m_flags & M_IGMP_LOOP) {
3539 		struct in_ifaddr *ia;
3540 
3541 		IFP_TO_IA(ifp, ia);
3542 		if (ia != NULL)
3543 			ip->ip_src = ia->ia_addr.sin_addr;
3544 	}
3545 
3546 	ip->ip_dst.s_addr = htonl(INADDR_ALLRPTS_GROUP);
3547 
3548 	return (m);
3549 }
3550 
3551 #ifdef KTR
3552 static char *
3553 igmp_rec_type_to_str(const int type)
3554 {
3555 
3556 	switch (type) {
3557 		case IGMP_CHANGE_TO_EXCLUDE_MODE:
3558 			return "TO_EX";
3559 			break;
3560 		case IGMP_CHANGE_TO_INCLUDE_MODE:
3561 			return "TO_IN";
3562 			break;
3563 		case IGMP_MODE_IS_EXCLUDE:
3564 			return "MODE_EX";
3565 			break;
3566 		case IGMP_MODE_IS_INCLUDE:
3567 			return "MODE_IN";
3568 			break;
3569 		case IGMP_ALLOW_NEW_SOURCES:
3570 			return "ALLOW_NEW";
3571 			break;
3572 		case IGMP_BLOCK_OLD_SOURCES:
3573 			return "BLOCK_OLD";
3574 			break;
3575 		default:
3576 			break;
3577 	}
3578 	return "unknown";
3579 }
3580 #endif
3581 
3582 static void
3583 igmp_sysinit(void)
3584 {
3585 
3586 	CTR1(KTR_IGMPV3, "%s: initializing", __func__);
3587 
3588 	IGMP_LOCK_INIT();
3589 
3590 	m_raopt = igmp_ra_alloc();
3591 
3592 	netisr_register(&igmp_nh);
3593 }
3594 
3595 static void
3596 igmp_sysuninit(void)
3597 {
3598 
3599 	CTR1(KTR_IGMPV3, "%s: tearing down", __func__);
3600 
3601 	netisr_unregister(&igmp_nh);
3602 
3603 	m_free(m_raopt);
3604 	m_raopt = NULL;
3605 
3606 	IGMP_LOCK_DESTROY();
3607 }
3608 
3609 /*
3610  * Initialize an IGMPv3 instance.
3611  * VIMAGE: Assumes curvnet set by caller and called per vimage.
3612  */
3613 static int
3614 vnet_igmp_iattach(const void *unused __unused)
3615 {
3616 	INIT_VNET_INET(curvnet);
3617 
3618 	CTR1(KTR_IGMPV3, "%s: initializing", __func__);
3619 
3620 	LIST_INIT(&V_igi_head);
3621 
3622 	V_current_state_timers_running = 0;
3623 	V_state_change_timers_running = 0;
3624 	V_interface_timers_running = 0;
3625 
3626 	/*
3627 	 * Initialize sysctls to default values.
3628 	 */
3629 	V_igmp_recvifkludge = 1;
3630 	V_igmp_sendra = 1;
3631 	V_igmp_sendlocal = 1;
3632 	V_igmp_v1enable = 1;
3633 	V_igmp_v2enable = 1;
3634 	V_igmp_legacysupp = 0;
3635 	V_igmp_default_version = IGMP_VERSION_3;
3636 	V_igmp_gsrdelay.tv_sec = 10;
3637 	V_igmp_gsrdelay.tv_usec = 0;
3638 
3639 	memset(&V_igmpstat, 0, sizeof(struct igmpstat));
3640 	V_igmpstat.igps_version = IGPS_VERSION_3;
3641 	V_igmpstat.igps_len = sizeof(struct igmpstat);
3642 
3643 	return (0);
3644 }
3645 
3646 static int
3647 vnet_igmp_idetach(const void *unused __unused)
3648 {
3649 #ifdef INVARIANTS
3650 	INIT_VNET_INET(curvnet);
3651 #endif
3652 
3653 	CTR1(KTR_IGMPV3, "%s: tearing down", __func__);
3654 
3655 	KASSERT(LIST_EMPTY(&V_igi_head),
3656 	    ("%s: igi list not empty; ifnets not detached?", __func__));
3657 
3658 	return (0);
3659 }
3660 
3661 #ifndef VIMAGE_GLOBALS
3662 static vnet_modinfo_t vnet_igmp_modinfo = {
3663 	.vmi_id		= VNET_MOD_IGMP,
3664 	.vmi_name	= "igmp",
3665 	.vmi_dependson	= VNET_MOD_INET,
3666 	.vmi_iattach	= vnet_igmp_iattach,
3667 	.vmi_idetach	= vnet_igmp_idetach
3668 };
3669 #endif
3670 
3671 static int
3672 igmp_modevent(module_t mod, int type, void *unused __unused)
3673 {
3674 
3675     switch (type) {
3676     case MOD_LOAD:
3677 	igmp_sysinit();
3678 #ifndef VIMAGE_GLOBALS
3679 	vnet_mod_register(&vnet_igmp_modinfo);
3680 #else
3681 	vnet_igmp_iattach(NULL);
3682 #endif
3683 	break;
3684     case MOD_UNLOAD:
3685 #ifndef VIMAGE_GLOBALS
3686 	vnet_mod_deregister(&vnet_igmp_modinfo);
3687 #else
3688 	vnet_igmp_idetach(NULL);
3689 #endif
3690 	igmp_sysuninit();
3691 	break;
3692     default:
3693 	return (EOPNOTSUPP);
3694     }
3695     return (0);
3696 }
3697 
3698 static moduledata_t igmp_mod = {
3699     "igmp",
3700     igmp_modevent,
3701     0
3702 };
3703 DECLARE_MODULE(igmp, igmp_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
3704