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