1139823Simp/*-
2189592Sbms * Copyright (c) 2007-2009 Bruce Simpson.
31541Srgrimes * Copyright (c) 1988 Stephen Deering.
41541Srgrimes * Copyright (c) 1992, 1993
51541Srgrimes *	The Regents of the University of California.  All rights reserved.
61541Srgrimes *
71541Srgrimes * This code is derived from software contributed to Berkeley by
81541Srgrimes * Stephen Deering of Stanford University.
91541Srgrimes *
101541Srgrimes * Redistribution and use in source and binary forms, with or without
111541Srgrimes * modification, are permitted provided that the following conditions
121541Srgrimes * are met:
131541Srgrimes * 1. Redistributions of source code must retain the above copyright
141541Srgrimes *    notice, this list of conditions and the following disclaimer.
151541Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
161541Srgrimes *    notice, this list of conditions and the following disclaimer in the
171541Srgrimes *    documentation and/or other materials provided with the distribution.
181541Srgrimes * 4. Neither the name of the University nor the names of its contributors
191541Srgrimes *    may be used to endorse or promote products derived from this software
201541Srgrimes *    without specific prior written permission.
211541Srgrimes *
221541Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
231541Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
241541Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
251541Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
261541Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
271541Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
281541Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
291541Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
301541Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
311541Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
321541Srgrimes * SUCH DAMAGE.
331541Srgrimes *
341541Srgrimes *	@(#)igmp.c	8.1 (Berkeley) 7/19/93
351541Srgrimes */
361541Srgrimes
372531Swollman/*
382531Swollman * Internet Group Management Protocol (IGMP) routines.
39189592Sbms * [RFC1112, RFC2236, RFC3376]
402531Swollman *
412531Swollman * Written by Steve Deering, Stanford, May 1988.
422531Swollman * Modified by Rosen Sharma, Stanford, Aug 1994.
439209Swollman * Modified by Bill Fenner, Xerox PARC, Feb 1995.
4414622Sfenner * Modified to fully comply to IGMPv2 by Bill Fenner, Oct 1995.
45189592Sbms * Significantly rewritten for IGMPv3, VIMAGE, and SMP by Bruce Simpson.
462531Swollman *
4714622Sfenner * MULTICAST Revision: 3.5.1.4
482531Swollman */
491541Srgrimes
50172467Ssilby#include <sys/cdefs.h>
51172467Ssilby__FBSDID("$FreeBSD$");
52172467Ssilby
531541Srgrimes#include <sys/param.h>
541549Srgrimes#include <sys/systm.h>
55189592Sbms#include <sys/module.h>
5629024Sbde#include <sys/malloc.h>
571541Srgrimes#include <sys/mbuf.h>
581541Srgrimes#include <sys/socket.h>
591541Srgrimes#include <sys/protosw.h>
6012296Sphk#include <sys/kernel.h>
616472Swollman#include <sys/sysctl.h>
62189592Sbms#include <sys/ktr.h>
63189592Sbms#include <sys/condvar.h>
641541Srgrimes
651541Srgrimes#include <net/if.h>
66189592Sbms#include <net/netisr.h>
67185571Sbz#include <net/vnet.h>
681541Srgrimes
691541Srgrimes#include <netinet/in.h>
701541Srgrimes#include <netinet/in_var.h>
711541Srgrimes#include <netinet/in_systm.h>
721541Srgrimes#include <netinet/ip.h>
731541Srgrimes#include <netinet/ip_var.h>
74152592Sandre#include <netinet/ip_options.h>
751541Srgrimes#include <netinet/igmp.h>
761541Srgrimes#include <netinet/igmp_var.h>
771541Srgrimes
7860105Sjlemon#include <machine/in_cksum.h>
7960105Sjlemon
80163606Srwatson#include <security/mac/mac_framework.h>
81163606Srwatson
82189592Sbms#ifndef KTR_IGMPV3
83191657Sbms#define KTR_IGMPV3 KTR_INET
84189592Sbms#endif
8530309Sphk
86189592Sbmsstatic struct igmp_ifinfo *
87189592Sbms		igi_alloc_locked(struct ifnet *);
88189592Sbmsstatic void	igi_delete_locked(const struct ifnet *);
89189592Sbmsstatic void	igmp_dispatch_queue(struct ifqueue *, int, const int);
90189592Sbmsstatic void	igmp_fasttimo_vnet(void);
91189592Sbmsstatic void	igmp_final_leave(struct in_multi *, struct igmp_ifinfo *);
92189592Sbmsstatic int	igmp_handle_state_change(struct in_multi *,
93189592Sbms		    struct igmp_ifinfo *);
94189592Sbmsstatic int	igmp_initial_join(struct in_multi *, struct igmp_ifinfo *);
95193231Sbmsstatic int	igmp_input_v1_query(struct ifnet *, const struct ip *,
96193231Sbms		    const struct igmp *);
97189592Sbmsstatic int	igmp_input_v2_query(struct ifnet *, const struct ip *,
98189592Sbms		    const struct igmp *);
99189592Sbmsstatic int	igmp_input_v3_query(struct ifnet *, const struct ip *,
100189592Sbms		    /*const*/ struct igmpv3 *);
101189592Sbmsstatic int	igmp_input_v3_group_query(struct in_multi *,
102189592Sbms		    struct igmp_ifinfo *, int, /*const*/ struct igmpv3 *);
103189592Sbmsstatic int	igmp_input_v1_report(struct ifnet *, /*const*/ struct ip *,
104189592Sbms		    /*const*/ struct igmp *);
105189592Sbmsstatic int	igmp_input_v2_report(struct ifnet *, /*const*/ struct ip *,
106189592Sbms		    /*const*/ struct igmp *);
107189592Sbmsstatic void	igmp_intr(struct mbuf *);
108189592Sbmsstatic int	igmp_isgroupreported(const struct in_addr);
109189592Sbmsstatic struct mbuf *
110189592Sbms		igmp_ra_alloc(void);
111189592Sbms#ifdef KTR
112189592Sbmsstatic char *	igmp_rec_type_to_str(const int);
113185088Szec#endif
114189592Sbmsstatic void	igmp_set_version(struct igmp_ifinfo *, const int);
115189592Sbmsstatic void	igmp_slowtimo_vnet(void);
116189592Sbmsstatic int	igmp_v1v2_queue_report(struct in_multi *, const int);
117189592Sbmsstatic void	igmp_v1v2_process_group_timer(struct in_multi *, const int);
118189592Sbmsstatic void	igmp_v1v2_process_querier_timers(struct igmp_ifinfo *);
119189592Sbmsstatic void	igmp_v2_update_group(struct in_multi *, const int);
120189592Sbmsstatic void	igmp_v3_cancel_link_timers(struct igmp_ifinfo *);
121189592Sbmsstatic void	igmp_v3_dispatch_general_query(struct igmp_ifinfo *);
122189592Sbmsstatic struct mbuf *
123189592Sbms		igmp_v3_encap_report(struct ifnet *, struct mbuf *);
124189592Sbmsstatic int	igmp_v3_enqueue_group_record(struct ifqueue *,
125189592Sbms		    struct in_multi *, const int, const int, const int);
126189592Sbmsstatic int	igmp_v3_enqueue_filter_change(struct ifqueue *,
127189592Sbms		    struct in_multi *);
128189592Sbmsstatic void	igmp_v3_process_group_timers(struct igmp_ifinfo *,
129189592Sbms		    struct ifqueue *, struct ifqueue *, struct in_multi *,
130189592Sbms		    const int);
131189592Sbmsstatic int	igmp_v3_merge_state_changes(struct in_multi *,
132189592Sbms		    struct ifqueue *);
133189592Sbmsstatic void	igmp_v3_suppress_group_record(struct in_multi *);
134189592Sbmsstatic int	sysctl_igmp_default_version(SYSCTL_HANDLER_ARGS);
135189592Sbmsstatic int	sysctl_igmp_gsr(SYSCTL_HANDLER_ARGS);
136189592Sbmsstatic int	sysctl_igmp_ifinfo(SYSCTL_HANDLER_ARGS);
1372531Swollman
138193219Srwatsonstatic const struct netisr_handler igmp_nh = {
139193219Srwatson	.nh_name = "igmp",
140193219Srwatson	.nh_handler = igmp_intr,
141193219Srwatson	.nh_proto = NETISR_IGMP,
142193219Srwatson	.nh_policy = NETISR_POLICY_SOURCE,
143193219Srwatson};
144193219Srwatson
145130333Srwatson/*
146189592Sbms * System-wide globals.
147189592Sbms *
148189592Sbms * Unlocked access to these is OK, except for the global IGMP output
149189592Sbms * queue. The IGMP subsystem lock ends up being system-wide for the moment,
150189592Sbms * because all VIMAGEs have to share a global output queue, as netisrs
151189592Sbms * themselves are not virtualized.
152189592Sbms *
153189592Sbms * Locking:
154189592Sbms *  * The permitted lock order is: IN_MULTI_LOCK, IGMP_LOCK, IF_ADDR_LOCK.
155189592Sbms *    Any may be taken independently; if any are held at the same
156189592Sbms *    time, the above lock order must be followed.
157191264Sbms *  * All output is delegated to the netisr.
158191264Sbms *    Now that Giant has been eliminated, the netisr may be inlined.
159189592Sbms *  * IN_MULTI_LOCK covers in_multi.
160189592Sbms *  * IGMP_LOCK covers igmp_ifinfo and any global variables in this file,
161189592Sbms *    including the output queue.
162189592Sbms *  * IF_ADDR_LOCK covers if_multiaddrs, which is used for a variety of
163189592Sbms *    per-link state iterators.
164189592Sbms *  * igmp_ifinfo is valid as long as PF_INET is attached to the interface,
165189592Sbms *    therefore it is not refcounted.
166189592Sbms *    We allow unlocked reads of igmp_ifinfo when accessed via in_multi.
167189592Sbms *
168189592Sbms * Reference counting
169189592Sbms *  * IGMP acquires its own reference every time an in_multi is passed to
170189592Sbms *    it and the group is being joined for the first time.
171189592Sbms *  * IGMP releases its reference(s) on in_multi in a deferred way,
172189592Sbms *    because the operations which process the release run as part of
173189592Sbms *    a loop whose control variables are directly affected by the release
174189592Sbms *    (that, and not recursing on the IF_ADDR_LOCK).
175189592Sbms *
176189592Sbms * VIMAGE: Each in_multi corresponds to an ifp, and each ifp corresponds
177189592Sbms * to a vnet in ifp->if_vnet.
178189592Sbms *
179189931Sbms * SMPng: XXX We may potentially race operations on ifma_protospec.
180189931Sbms * The problem is that we currently lack a clean way of taking the
181189931Sbms * IF_ADDR_LOCK() between the ifnet and in layers w/o recursing,
182189931Sbms * as anything which modifies ifma needs to be covered by that lock.
183189931Sbms * So check for ifma_protospec being NULL before proceeding.
184130333Srwatson */
185189592Sbmsstruct mtx		 igmp_mtx;
186189592Sbms
187189592Sbmsstruct mbuf		*m_raopt;		 /* Router Alert option */
188249132Smavstatic MALLOC_DEFINE(M_IGMP, "igmp", "igmp state");
189189592Sbms
190189592Sbms/*
191189592Sbms * VIMAGE-wide globals.
192189592Sbms *
193189592Sbms * The IGMPv3 timers themselves need to run per-image, however,
194189592Sbms * protosw timers run globally (see tcp).
195189592Sbms * An ifnet can only be in one vimage at a time, and the loopback
196189592Sbms * ifnet, loif, is itself virtualized.
197189592Sbms * It would otherwise be possible to seriously hose IGMP state,
198189592Sbms * and create inconsistencies in upstream multicast routing, if you have
199189592Sbms * multiple VIMAGEs running on the same link joining different multicast
200189592Sbms * groups, UNLESS the "primary IP address" is different. This is because
201189592Sbms * IGMP for IPv4 does not force link-local addresses to be used for each
202189592Sbms * node, unlike MLD for IPv6.
203189592Sbms * Obviously the IGMPv3 per-interface state has per-vimage granularity
204189592Sbms * also as a result.
205189592Sbms *
206189592Sbms * FUTURE: Stop using IFP_TO_IA/INADDR_ANY, and use source address selection
207189592Sbms * policy to control the address used by IGMP on the link.
208189592Sbms */
209215701Sdimstatic VNET_DEFINE(int, interface_timers_running);	/* IGMPv3 general
210195699Srwatson							 * query response */
211215701Sdimstatic VNET_DEFINE(int, state_change_timers_running);	/* IGMPv3 state-change
212195699Srwatson							 * retransmit */
213215701Sdimstatic VNET_DEFINE(int, current_state_timers_running);	/* IGMPv1/v2 host
214195699Srwatson							 * report; IGMPv3 g/sg
215195699Srwatson							 * query response */
216130333Srwatson
217195727Srwatson#define	V_interface_timers_running	VNET(interface_timers_running)
218195727Srwatson#define	V_state_change_timers_running	VNET(state_change_timers_running)
219195727Srwatson#define	V_current_state_timers_running	VNET(current_state_timers_running)
220189592Sbms
221215701Sdimstatic VNET_DEFINE(LIST_HEAD(, igmp_ifinfo), igi_head);
222215701Sdimstatic VNET_DEFINE(struct igmpstat, igmpstat) = {
223195782Srwatson	.igps_version = IGPS_VERSION_3,
224195782Srwatson	.igps_len = sizeof(struct igmpstat),
225195782Srwatson};
226215701Sdimstatic VNET_DEFINE(struct timeval, igmp_gsrdelay) = {10, 0};
227189592Sbms
228195727Srwatson#define	V_igi_head			VNET(igi_head)
229195727Srwatson#define	V_igmpstat			VNET(igmpstat)
230195727Srwatson#define	V_igmp_gsrdelay			VNET(igmp_gsrdelay)
231195699Srwatson
232215701Sdimstatic VNET_DEFINE(int, igmp_recvifkludge) = 1;
233215701Sdimstatic VNET_DEFINE(int, igmp_sendra) = 1;
234215701Sdimstatic VNET_DEFINE(int, igmp_sendlocal) = 1;
235215701Sdimstatic VNET_DEFINE(int, igmp_v1enable) = 1;
236215701Sdimstatic VNET_DEFINE(int, igmp_v2enable) = 1;
237215701Sdimstatic VNET_DEFINE(int, igmp_legacysupp);
238215701Sdimstatic VNET_DEFINE(int, igmp_default_version) = IGMP_VERSION_3;
239195699Srwatson
240195727Srwatson#define	V_igmp_recvifkludge		VNET(igmp_recvifkludge)
241195727Srwatson#define	V_igmp_sendra			VNET(igmp_sendra)
242195727Srwatson#define	V_igmp_sendlocal		VNET(igmp_sendlocal)
243195727Srwatson#define	V_igmp_v1enable			VNET(igmp_v1enable)
244195727Srwatson#define	V_igmp_v2enable			VNET(igmp_v2enable)
245195727Srwatson#define	V_igmp_legacysupp		VNET(igmp_legacysupp)
246195727Srwatson#define	V_igmp_default_version		VNET(igmp_default_version)
247195699Srwatson
248130333Srwatson/*
249189592Sbms * Virtualized sysctls.
250130333Srwatson */
251195699SrwatsonSYSCTL_VNET_STRUCT(_net_inet_igmp, IGMPCTL_STATS, stats, CTLFLAG_RW,
252195699Srwatson    &VNET_NAME(igmpstat), igmpstat, "");
253195699SrwatsonSYSCTL_VNET_INT(_net_inet_igmp, OID_AUTO, recvifkludge, CTLFLAG_RW,
254195699Srwatson    &VNET_NAME(igmp_recvifkludge), 0,
255189592Sbms    "Rewrite IGMPv1/v2 reports from 0.0.0.0 to contain subnet address");
256195699SrwatsonSYSCTL_VNET_INT(_net_inet_igmp, OID_AUTO, sendra, CTLFLAG_RW,
257195699Srwatson    &VNET_NAME(igmp_sendra), 0,
258189592Sbms    "Send IP Router Alert option in IGMPv2/v3 messages");
259195699SrwatsonSYSCTL_VNET_INT(_net_inet_igmp, OID_AUTO, sendlocal, CTLFLAG_RW,
260195699Srwatson    &VNET_NAME(igmp_sendlocal), 0,
261189592Sbms    "Send IGMP membership reports for 224.0.0.0/24 groups");
262195699SrwatsonSYSCTL_VNET_INT(_net_inet_igmp, OID_AUTO, v1enable, CTLFLAG_RW,
263195699Srwatson    &VNET_NAME(igmp_v1enable), 0,
264189592Sbms    "Enable backwards compatibility with IGMPv1");
265195699SrwatsonSYSCTL_VNET_INT(_net_inet_igmp, OID_AUTO, v2enable, CTLFLAG_RW,
266195699Srwatson    &VNET_NAME(igmp_v2enable), 0,
267189592Sbms    "Enable backwards compatibility with IGMPv2");
268195699SrwatsonSYSCTL_VNET_INT(_net_inet_igmp, OID_AUTO, legacysupp, CTLFLAG_RW,
269195699Srwatson    &VNET_NAME(igmp_legacysupp), 0,
270189592Sbms    "Allow v1/v2 reports to suppress v3 group responses");
271195699SrwatsonSYSCTL_VNET_PROC(_net_inet_igmp, OID_AUTO, default_version,
272195699Srwatson    CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
273195699Srwatson    &VNET_NAME(igmp_default_version), 0, sysctl_igmp_default_version, "I",
274189592Sbms    "Default version of IGMP to run on each interface");
275195699SrwatsonSYSCTL_VNET_PROC(_net_inet_igmp, OID_AUTO, gsrdelay,
276195699Srwatson    CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
277195699Srwatson    &VNET_NAME(igmp_gsrdelay.tv_sec), 0, sysctl_igmp_gsr, "I",
278189592Sbms    "Rate limit for IGMPv3 Group-and-Source queries in seconds");
279130333Srwatson
280189592Sbms/*
281189592Sbms * Non-virtualized sysctls.
282189592Sbms */
283248085Smariusstatic SYSCTL_NODE(_net_inet_igmp, OID_AUTO, ifinfo,
284248085Smarius    CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_igmp_ifinfo,
285248085Smarius    "Per-interface IGMPv3 state");
2861541Srgrimes
287189592Sbmsstatic __inline void
288189592Sbmsigmp_save_context(struct mbuf *m, struct ifnet *ifp)
289189592Sbms{
290189592Sbms
291189592Sbms#ifdef VIMAGE
292189592Sbms	m->m_pkthdr.header = ifp->if_vnet;
293189592Sbms#endif /* VIMAGE */
294189592Sbms	m->m_pkthdr.flowid = ifp->if_index;
295189592Sbms}
296189592Sbms
297189592Sbmsstatic __inline void
298189592Sbmsigmp_scrub_context(struct mbuf *m)
299189592Sbms{
300189592Sbms
301189592Sbms	m->m_pkthdr.header = NULL;
302189592Sbms	m->m_pkthdr.flowid = 0;
303189592Sbms}
304189592Sbms
305189592Sbms#ifdef KTR
306189592Sbmsstatic __inline char *
307189592Sbmsinet_ntoa_haddr(in_addr_t haddr)
308189592Sbms{
309189592Sbms	struct in_addr ia;
310189592Sbms
311189592Sbms	ia.s_addr = htonl(haddr);
312189592Sbms	return (inet_ntoa(ia));
313189592Sbms}
314119180Srwatson#endif
315119180Srwatson
316189592Sbms/*
317189592Sbms * Restore context from a queued IGMP output chain.
318189592Sbms * Return saved ifindex.
319189592Sbms *
320189592Sbms * VIMAGE: The assertion is there to make sure that we
321189592Sbms * actually called CURVNET_SET() with what's in the mbuf chain.
322189592Sbms */
323189592Sbmsstatic __inline uint32_t
324189592Sbmsigmp_restore_context(struct mbuf *m)
3251541Srgrimes{
32614622Sfenner
327189592Sbms#ifdef notyet
328189592Sbms#if defined(VIMAGE) && defined(INVARIANTS)
329189592Sbms	KASSERT(curvnet == (m->m_pkthdr.header),
330189592Sbms	    ("%s: called when curvnet was not restored", __func__));
331189592Sbms#endif
332189592Sbms#endif
333189592Sbms	return (m->m_pkthdr.flowid);
334189592Sbms}
335189592Sbms
336189592Sbms/*
337189592Sbms * Retrieve or set default IGMP version.
338189592Sbms *
339189592Sbms * VIMAGE: Assume curvnet set by caller.
340189592Sbms * SMPng: NOTE: Serialized by IGMP lock.
341189592Sbms */
342189592Sbmsstatic int
343189592Sbmssysctl_igmp_default_version(SYSCTL_HANDLER_ARGS)
344189592Sbms{
345189592Sbms	int	 error;
346189592Sbms	int	 new;
347189592Sbms
348189592Sbms	error = sysctl_wire_old_buffer(req, sizeof(int));
349189592Sbms	if (error)
350189592Sbms		return (error);
351189592Sbms
352189592Sbms	IGMP_LOCK();
353189592Sbms
354189592Sbms	new = V_igmp_default_version;
355189592Sbms
356189592Sbms	error = sysctl_handle_int(oidp, &new, 0, req);
357189592Sbms	if (error || !req->newptr)
358189592Sbms		goto out_locked;
359189592Sbms
360189592Sbms	if (new < IGMP_VERSION_1 || new > IGMP_VERSION_3) {
361189592Sbms		error = EINVAL;
362189592Sbms		goto out_locked;
363189592Sbms	}
364189592Sbms
365189592Sbms	CTR2(KTR_IGMPV3, "change igmp_default_version from %d to %d",
366189592Sbms	     V_igmp_default_version, new);
367189592Sbms
368189592Sbms	V_igmp_default_version = new;
369189592Sbms
370189592Sbmsout_locked:
371189592Sbms	IGMP_UNLOCK();
372189592Sbms	return (error);
373189592Sbms}
374189592Sbms
375189592Sbms/*
376189592Sbms * Retrieve or set threshold between group-source queries in seconds.
377189592Sbms *
378189592Sbms * VIMAGE: Assume curvnet set by caller.
379189592Sbms * SMPng: NOTE: Serialized by IGMP lock.
380189592Sbms */
381189592Sbmsstatic int
382189592Sbmssysctl_igmp_gsr(SYSCTL_HANDLER_ARGS)
383189592Sbms{
384189592Sbms	int error;
385189592Sbms	int i;
386189592Sbms
387189592Sbms	error = sysctl_wire_old_buffer(req, sizeof(int));
388189592Sbms	if (error)
389189592Sbms		return (error);
390189592Sbms
391189592Sbms	IGMP_LOCK();
392189592Sbms
393189592Sbms	i = V_igmp_gsrdelay.tv_sec;
394189592Sbms
395189592Sbms	error = sysctl_handle_int(oidp, &i, 0, req);
396189592Sbms	if (error || !req->newptr)
397189592Sbms		goto out_locked;
398189592Sbms
399189592Sbms	if (i < -1 || i >= 60) {
400189592Sbms		error = EINVAL;
401189592Sbms		goto out_locked;
402189592Sbms	}
403189592Sbms
404189592Sbms	CTR2(KTR_IGMPV3, "change igmp_gsrdelay from %d to %d",
405189592Sbms	     V_igmp_gsrdelay.tv_sec, i);
406189592Sbms	V_igmp_gsrdelay.tv_sec = i;
407189592Sbms
408189592Sbmsout_locked:
409189592Sbms	IGMP_UNLOCK();
410189592Sbms	return (error);
411189592Sbms}
412189592Sbms
413189592Sbms/*
414189592Sbms * Expose struct igmp_ifinfo to userland, keyed by ifindex.
415189592Sbms * For use by ifmcstat(8).
416189592Sbms *
417189592Sbms * SMPng: NOTE: Does an unlocked ifindex space read.
418189592Sbms * VIMAGE: Assume curvnet set by caller. The node handler itself
419189592Sbms * is not directly virtualized.
420189592Sbms */
421189592Sbmsstatic int
422189592Sbmssysctl_igmp_ifinfo(SYSCTL_HANDLER_ARGS)
423189592Sbms{
424189592Sbms	int			*name;
425189592Sbms	int			 error;
426189592Sbms	u_int			 namelen;
427189592Sbms	struct ifnet		*ifp;
428189592Sbms	struct igmp_ifinfo	*igi;
429189592Sbms
430189592Sbms	name = (int *)arg1;
431189592Sbms	namelen = arg2;
432189592Sbms
433189592Sbms	if (req->newptr != NULL)
434189592Sbms		return (EPERM);
435189592Sbms
436189592Sbms	if (namelen != 1)
437189592Sbms		return (EINVAL);
438189592Sbms
439189592Sbms	error = sysctl_wire_old_buffer(req, sizeof(struct igmp_ifinfo));
440189592Sbms	if (error)
441189592Sbms		return (error);
442189592Sbms
443189592Sbms	IN_MULTI_LOCK();
444189592Sbms	IGMP_LOCK();
445189592Sbms
446189592Sbms	if (name[0] <= 0 || name[0] > V_if_index) {
447189592Sbms		error = ENOENT;
448189592Sbms		goto out_locked;
449189592Sbms	}
450189592Sbms
451189592Sbms	error = ENOENT;
452189592Sbms
453189592Sbms	ifp = ifnet_byindex(name[0]);
454189592Sbms	if (ifp == NULL)
455189592Sbms		goto out_locked;
456189592Sbms
457189592Sbms	LIST_FOREACH(igi, &V_igi_head, igi_link) {
458189592Sbms		if (ifp == igi->igi_ifp) {
459189592Sbms			error = SYSCTL_OUT(req, igi,
460189592Sbms			    sizeof(struct igmp_ifinfo));
461189592Sbms			break;
462189592Sbms		}
463189592Sbms	}
464189592Sbms
465189592Sbmsout_locked:
466189592Sbms	IGMP_UNLOCK();
467189592Sbms	IN_MULTI_UNLOCK();
468189592Sbms	return (error);
469189592Sbms}
470189592Sbms
471189592Sbms/*
472189592Sbms * Dispatch an entire queue of pending packet chains
473189592Sbms * using the netisr.
474189592Sbms * VIMAGE: Assumes the vnet pointer has been set.
475189592Sbms */
476189592Sbmsstatic void
477189592Sbmsigmp_dispatch_queue(struct ifqueue *ifq, int limit, const int loop)
478189592Sbms{
479189592Sbms	struct mbuf *m;
480189592Sbms
481189592Sbms	for (;;) {
482189592Sbms		_IF_DEQUEUE(ifq, m);
483189592Sbms		if (m == NULL)
484189592Sbms			break;
485189592Sbms		CTR3(KTR_IGMPV3, "%s: dispatch %p from %p", __func__, ifq, m);
486189592Sbms		if (loop)
487189592Sbms			m->m_flags |= M_IGMP_LOOP;
488189592Sbms		netisr_dispatch(NETISR_IGMP, m);
489189592Sbms		if (--limit == 0)
490189592Sbms			break;
491189592Sbms	}
492189592Sbms}
493189592Sbms
494189592Sbms/*
495189592Sbms * Filter outgoing IGMP report state by group.
496189592Sbms *
497189592Sbms * Reports are ALWAYS suppressed for ALL-HOSTS (224.0.0.1).
498189592Sbms * If the net.inet.igmp.sendlocal sysctl is 0, then IGMP reports are
499189592Sbms * disabled for all groups in the 224.0.0.0/24 link-local scope. However,
500189592Sbms * this may break certain IGMP snooping switches which rely on the old
501189592Sbms * report behaviour.
502189592Sbms *
503189592Sbms * Return zero if the given group is one for which IGMP reports
504189592Sbms * should be suppressed, or non-zero if reports should be issued.
505189592Sbms */
506189592Sbmsstatic __inline int
507189592Sbmsigmp_isgroupreported(const struct in_addr addr)
508189592Sbms{
509189592Sbms
510189592Sbms	if (in_allhosts(addr) ||
511189592Sbms	    ((!V_igmp_sendlocal && IN_LOCAL_GROUP(ntohl(addr.s_addr)))))
512189592Sbms		return (0);
513189592Sbms
514189592Sbms	return (1);
515189592Sbms}
516189592Sbms
517189592Sbms/*
518189592Sbms * Construct a Router Alert option to use in outgoing packets.
519189592Sbms */
520189592Sbmsstatic struct mbuf *
521189592Sbmsigmp_ra_alloc(void)
522189592Sbms{
523189592Sbms	struct mbuf	*m;
524189592Sbms	struct ipoption	*p;
525189592Sbms
526189592Sbms	MGET(m, M_DONTWAIT, MT_DATA);
527189592Sbms	p = mtod(m, struct ipoption *);
528189592Sbms	p->ipopt_dst.s_addr = INADDR_ANY;
529189592Sbms	p->ipopt_list[0] = IPOPT_RA;	/* Router Alert Option */
530189592Sbms	p->ipopt_list[1] = 0x04;	/* 4 bytes long */
531189592Sbms	p->ipopt_list[2] = IPOPT_EOL;	/* End of IP option list */
532189592Sbms	p->ipopt_list[3] = 0x00;	/* pad byte */
533189592Sbms	m->m_len = sizeof(p->ipopt_dst) + p->ipopt_list[1];
534189592Sbms
535189592Sbms	return (m);
536189592Sbms}
537189592Sbms
538189592Sbms/*
539189592Sbms * Attach IGMP when PF_INET is attached to an interface.
540189592Sbms */
541189592Sbmsstruct igmp_ifinfo *
542189592Sbmsigmp_domifattach(struct ifnet *ifp)
543189592Sbms{
544189592Sbms	struct igmp_ifinfo *igi;
545189592Sbms
546189592Sbms	CTR3(KTR_IGMPV3, "%s: called for ifp %p(%s)",
547189592Sbms	    __func__, ifp, ifp->if_xname);
548189592Sbms
549189592Sbms	IGMP_LOCK();
550189592Sbms
551189592Sbms	igi = igi_alloc_locked(ifp);
552189592Sbms	if (!(ifp->if_flags & IFF_MULTICAST))
553189592Sbms		igi->igi_flags |= IGIF_SILENT;
554189592Sbms
555189592Sbms	IGMP_UNLOCK();
556189592Sbms
557189592Sbms	return (igi);
558189592Sbms}
559189592Sbms
560189592Sbms/*
561189592Sbms * VIMAGE: assume curvnet set by caller.
562189592Sbms */
563189592Sbmsstatic struct igmp_ifinfo *
564189592Sbmsigi_alloc_locked(/*const*/ struct ifnet *ifp)
565189592Sbms{
566189592Sbms	struct igmp_ifinfo *igi;
567189592Sbms
568189592Sbms	IGMP_LOCK_ASSERT();
569189592Sbms
570189592Sbms	igi = malloc(sizeof(struct igmp_ifinfo), M_IGMP, M_NOWAIT|M_ZERO);
571189592Sbms	if (igi == NULL)
572189592Sbms		goto out;
573189592Sbms
574189592Sbms	igi->igi_ifp = ifp;
575189592Sbms	igi->igi_version = V_igmp_default_version;
576189592Sbms	igi->igi_flags = 0;
577189592Sbms	igi->igi_rv = IGMP_RV_INIT;
578189592Sbms	igi->igi_qi = IGMP_QI_INIT;
579189592Sbms	igi->igi_qri = IGMP_QRI_INIT;
580189592Sbms	igi->igi_uri = IGMP_URI_INIT;
581189592Sbms
582189592Sbms	SLIST_INIT(&igi->igi_relinmhead);
583189592Sbms
5841541Srgrimes	/*
585189592Sbms	 * Responses to general queries are subject to bounds.
5861541Srgrimes	 */
587189592Sbms	IFQ_SET_MAXLEN(&igi->igi_gq, IGMP_MAX_RESPONSE_PACKETS);
5889209Swollman
589189592Sbms	LIST_INSERT_HEAD(&V_igi_head, igi, igi_link);
5909209Swollman
591189592Sbms	CTR2(KTR_IGMPV3, "allocate igmp_ifinfo for ifp %p(%s)",
592189592Sbms	     ifp, ifp->if_xname);
593189592Sbms
594189592Sbmsout:
595189592Sbms	return (igi);
596189592Sbms}
597189592Sbms
598189592Sbms/*
599189592Sbms * Hook for ifdetach.
600189592Sbms *
601189592Sbms * NOTE: Some finalization tasks need to run before the protocol domain
602189592Sbms * is detached, but also before the link layer does its cleanup.
603189592Sbms *
604189592Sbms * SMPNG: igmp_ifdetach() needs to take IF_ADDR_LOCK().
605189931Sbms * XXX This is also bitten by unlocked ifma_protospec access.
606189592Sbms */
607189592Sbmsvoid
608189592Sbmsigmp_ifdetach(struct ifnet *ifp)
609189592Sbms{
610189592Sbms	struct igmp_ifinfo	*igi;
611189592Sbms	struct ifmultiaddr	*ifma;
612189592Sbms	struct in_multi		*inm, *tinm;
613189592Sbms
614189592Sbms	CTR3(KTR_IGMPV3, "%s: called for ifp %p(%s)", __func__, ifp,
615189592Sbms	    ifp->if_xname);
616189592Sbms
617189592Sbms	IGMP_LOCK();
618189592Sbms
619189592Sbms	igi = ((struct in_ifinfo *)ifp->if_afdata[AF_INET])->ii_igmp;
620189592Sbms	if (igi->igi_version == IGMP_VERSION_3) {
621233200Sjhb		IF_ADDR_RLOCK(ifp);
622189592Sbms		TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
623189931Sbms			if (ifma->ifma_addr->sa_family != AF_INET ||
624189931Sbms			    ifma->ifma_protospec == NULL)
625189592Sbms				continue;
626189931Sbms#if 0
627189931Sbms			KASSERT(ifma->ifma_protospec != NULL,
628189931Sbms			    ("%s: ifma_protospec is NULL", __func__));
629189931Sbms#endif
630189592Sbms			inm = (struct in_multi *)ifma->ifma_protospec;
631189592Sbms			if (inm->inm_state == IGMP_LEAVING_MEMBER) {
632189592Sbms				SLIST_INSERT_HEAD(&igi->igi_relinmhead,
633189592Sbms				    inm, inm_nrele);
634189592Sbms			}
635189592Sbms			inm_clear_recorded(inm);
636189592Sbms		}
637233200Sjhb		IF_ADDR_RUNLOCK(ifp);
638189592Sbms		/*
639189592Sbms		 * Free the in_multi reference(s) for this IGMP lifecycle.
640189592Sbms		 */
641189592Sbms		SLIST_FOREACH_SAFE(inm, &igi->igi_relinmhead, inm_nrele,
642189592Sbms		    tinm) {
643189592Sbms			SLIST_REMOVE_HEAD(&igi->igi_relinmhead, inm_nrele);
644189592Sbms			inm_release_locked(inm);
645189592Sbms		}
646189592Sbms	}
647189592Sbms
648189592Sbms	IGMP_UNLOCK();
6491541Srgrimes}
6501541Srgrimes
651189592Sbms/*
652189592Sbms * Hook for domifdetach.
653189592Sbms */
654189592Sbmsvoid
655189592Sbmsigmp_domifdetach(struct ifnet *ifp)
6562531Swollman{
657189592Sbms	struct igmp_ifinfo *igi;
658189592Sbms
659189592Sbms	CTR3(KTR_IGMPV3, "%s: called for ifp %p(%s)",
660189592Sbms	    __func__, ifp, ifp->if_xname);
661189592Sbms
662189592Sbms	IGMP_LOCK();
663189592Sbms
664189592Sbms	igi = ((struct in_ifinfo *)ifp->if_afdata[AF_INET])->ii_igmp;
665189592Sbms	igi_delete_locked(ifp);
666189592Sbms
667189592Sbms	IGMP_UNLOCK();
668189592Sbms}
669189592Sbms
670189592Sbmsstatic void
671189592Sbmsigi_delete_locked(const struct ifnet *ifp)
672189592Sbms{
673189592Sbms	struct igmp_ifinfo *igi, *tigi;
674189592Sbms
675189592Sbms	CTR3(KTR_IGMPV3, "%s: freeing igmp_ifinfo for ifp %p(%s)",
676189592Sbms	    __func__, ifp, ifp->if_xname);
677189592Sbms
678189592Sbms	IGMP_LOCK_ASSERT();
679189592Sbms
680189592Sbms	LIST_FOREACH_SAFE(igi, &V_igi_head, igi_link, tigi) {
681189592Sbms		if (igi->igi_ifp == ifp) {
682189592Sbms			/*
683189592Sbms			 * Free deferred General Query responses.
684189592Sbms			 */
685189592Sbms			_IF_DRAIN(&igi->igi_gq);
686189592Sbms
687189592Sbms			LIST_REMOVE(igi, igi_link);
688189592Sbms
689189592Sbms			KASSERT(SLIST_EMPTY(&igi->igi_relinmhead),
690189592Sbms			    ("%s: there are dangling in_multi references",
691189592Sbms			    __func__));
692189592Sbms
693189592Sbms			free(igi, M_IGMP);
694189592Sbms			return;
695189592Sbms		}
696189592Sbms	}
697189592Sbms
698189592Sbms#ifdef INVARIANTS
699189592Sbms	panic("%s: igmp_ifinfo not found for ifp %p\n", __func__,  ifp);
700189592Sbms#endif
701189592Sbms}
702189592Sbms
703189592Sbms/*
704189592Sbms * Process a received IGMPv1 query.
705189592Sbms * Return non-zero if the message should be dropped.
706189592Sbms *
707189592Sbms * VIMAGE: The curvnet pointer is derived from the input ifp.
708189592Sbms */
709189592Sbmsstatic int
710193231Sbmsigmp_input_v1_query(struct ifnet *ifp, const struct ip *ip,
711193231Sbms    const struct igmp *igmp)
712189592Sbms{
713189592Sbms	struct ifmultiaddr	*ifma;
714189592Sbms	struct igmp_ifinfo	*igi;
715189592Sbms	struct in_multi		*inm;
7162531Swollman
717189592Sbms	/*
718193231Sbms	 * IGMPv1 Host Mmembership Queries SHOULD always be addressed to
719193231Sbms	 * 224.0.0.1. They are always treated as General Queries.
720189592Sbms	 * igmp_group is always ignored. Do not drop it as a userland
721189592Sbms	 * daemon may wish to see it.
722193231Sbms	 * XXX SMPng: unlocked increments in igmpstat assumed atomic.
723189592Sbms	 */
724193231Sbms	if (!in_allhosts(ip->ip_dst) || !in_nullhost(igmp->igmp_group)) {
725190965Srwatson		IGMPSTAT_INC(igps_rcv_badqueries);
726189592Sbms		return (0);
727189592Sbms	}
728190965Srwatson	IGMPSTAT_INC(igps_rcv_gen_queries);
729189592Sbms
730189592Sbms	IN_MULTI_LOCK();
731189592Sbms	IGMP_LOCK();
732189592Sbms
733189592Sbms	igi = ((struct in_ifinfo *)ifp->if_afdata[AF_INET])->ii_igmp;
734189592Sbms	KASSERT(igi != NULL, ("%s: no igmp_ifinfo for ifp %p", __func__, ifp));
735189592Sbms
736189592Sbms	if (igi->igi_flags & IGIF_LOOPBACK) {
737189592Sbms		CTR2(KTR_IGMPV3, "ignore v1 query on IGIF_LOOPBACK ifp %p(%s)",
738189592Sbms		    ifp, ifp->if_xname);
739189592Sbms		goto out_locked;
740189592Sbms	}
741189592Sbms
742193231Sbms	/*
743193231Sbms	 * Switch to IGMPv1 host compatibility mode.
744193231Sbms	 */
745189592Sbms	igmp_set_version(igi, IGMP_VERSION_1);
746189592Sbms
747189592Sbms	CTR2(KTR_IGMPV3, "process v1 query on ifp %p(%s)", ifp, ifp->if_xname);
748189592Sbms
749189592Sbms	/*
750189592Sbms	 * Start the timers in all of our group records
751189592Sbms	 * for the interface on which the query arrived,
752189592Sbms	 * except those which are already running.
753189592Sbms	 */
754233200Sjhb	IF_ADDR_RLOCK(ifp);
755189592Sbms	TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
756189931Sbms		if (ifma->ifma_addr->sa_family != AF_INET ||
757189931Sbms		    ifma->ifma_protospec == NULL)
758189592Sbms			continue;
759189592Sbms		inm = (struct in_multi *)ifma->ifma_protospec;
760189592Sbms		if (inm->inm_timer != 0)
761189592Sbms			continue;
762189592Sbms		switch (inm->inm_state) {
763189592Sbms		case IGMP_NOT_MEMBER:
764189592Sbms		case IGMP_SILENT_MEMBER:
765189592Sbms			break;
766189592Sbms		case IGMP_G_QUERY_PENDING_MEMBER:
767189592Sbms		case IGMP_SG_QUERY_PENDING_MEMBER:
768189592Sbms		case IGMP_REPORTING_MEMBER:
769189592Sbms		case IGMP_IDLE_MEMBER:
770189592Sbms		case IGMP_LAZY_MEMBER:
771189592Sbms		case IGMP_SLEEPING_MEMBER:
772189592Sbms		case IGMP_AWAKENING_MEMBER:
773189592Sbms			inm->inm_state = IGMP_REPORTING_MEMBER;
774189592Sbms			inm->inm_timer = IGMP_RANDOM_DELAY(
775189592Sbms			    IGMP_V1V2_MAX_RI * PR_FASTHZ);
776189592Sbms			V_current_state_timers_running = 1;
777189592Sbms			break;
778189592Sbms		case IGMP_LEAVING_MEMBER:
779189592Sbms			break;
780119181Srwatson		}
781119181Srwatson	}
782233200Sjhb	IF_ADDR_RUNLOCK(ifp);
783189592Sbms
784189592Sbmsout_locked:
785189592Sbms	IGMP_UNLOCK();
786189592Sbms	IN_MULTI_UNLOCK();
787189592Sbms
788189592Sbms	return (0);
789189592Sbms}
790189592Sbms
791189592Sbms/*
792189592Sbms * Process a received IGMPv2 general or group-specific query.
793189592Sbms */
794189592Sbmsstatic int
795189592Sbmsigmp_input_v2_query(struct ifnet *ifp, const struct ip *ip,
796189592Sbms    const struct igmp *igmp)
797189592Sbms{
798189592Sbms	struct ifmultiaddr	*ifma;
799189592Sbms	struct igmp_ifinfo	*igi;
800189592Sbms	struct in_multi		*inm;
801193231Sbms	int			 is_general_query;
802189592Sbms	uint16_t		 timer;
803189592Sbms
804193231Sbms	is_general_query = 0;
805193231Sbms
806189592Sbms	/*
807193231Sbms	 * Validate address fields upfront.
808193231Sbms	 * XXX SMPng: unlocked increments in igmpstat assumed atomic.
809189592Sbms	 */
810193231Sbms	if (in_nullhost(igmp->igmp_group)) {
811193231Sbms		/*
812193231Sbms		 * IGMPv2 General Query.
813193231Sbms		 * If this was not sent to the all-hosts group, ignore it.
814193231Sbms		 */
815193231Sbms		if (!in_allhosts(ip->ip_dst))
816193231Sbms			return (0);
817193231Sbms		IGMPSTAT_INC(igps_rcv_gen_queries);
818193231Sbms		is_general_query = 1;
819193231Sbms	} else {
820193231Sbms		/* IGMPv2 Group-Specific Query. */
821193231Sbms		IGMPSTAT_INC(igps_rcv_group_queries);
822193231Sbms	}
823193231Sbms
824189592Sbms	IN_MULTI_LOCK();
825189592Sbms	IGMP_LOCK();
826189592Sbms
827189592Sbms	igi = ((struct in_ifinfo *)ifp->if_afdata[AF_INET])->ii_igmp;
828189592Sbms	KASSERT(igi != NULL, ("%s: no igmp_ifinfo for ifp %p", __func__, ifp));
829189592Sbms
830189592Sbms	if (igi->igi_flags & IGIF_LOOPBACK) {
831189592Sbms		CTR2(KTR_IGMPV3, "ignore v2 query on IGIF_LOOPBACK ifp %p(%s)",
832189592Sbms		    ifp, ifp->if_xname);
833189592Sbms		goto out_locked;
834144163Ssam	}
835189592Sbms
836193231Sbms	/*
837193231Sbms	 * Ignore v2 query if in v1 Compatibility Mode.
838193231Sbms	 */
839193231Sbms	if (igi->igi_version == IGMP_VERSION_1)
840193231Sbms		goto out_locked;
841193231Sbms
842189592Sbms	igmp_set_version(igi, IGMP_VERSION_2);
843189592Sbms
844189592Sbms	timer = igmp->igmp_code * PR_FASTHZ / IGMP_TIMER_SCALE;
845189592Sbms	if (timer == 0)
846189592Sbms		timer = 1;
847189592Sbms
848193231Sbms	if (is_general_query) {
849189592Sbms		/*
850193231Sbms		 * For each reporting group joined on this
851193231Sbms		 * interface, kick the report timer.
852193231Sbms		 */
853193231Sbms		CTR2(KTR_IGMPV3, "process v2 general query on ifp %p(%s)",
854193231Sbms		    ifp, ifp->if_xname);
855233200Sjhb		IF_ADDR_RLOCK(ifp);
856193231Sbms		TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
857193231Sbms			if (ifma->ifma_addr->sa_family != AF_INET ||
858193231Sbms			    ifma->ifma_protospec == NULL)
859193231Sbms				continue;
860193231Sbms			inm = (struct in_multi *)ifma->ifma_protospec;
861193231Sbms			igmp_v2_update_group(inm, timer);
862193231Sbms		}
863233200Sjhb		IF_ADDR_RUNLOCK(ifp);
864193231Sbms	} else {
865193231Sbms		/*
866193231Sbms		 * Group-specific IGMPv2 query, we need only
867189592Sbms		 * look up the single group to process it.
868189592Sbms		 */
869189592Sbms		inm = inm_lookup(ifp, igmp->igmp_group);
870189592Sbms		if (inm != NULL) {
871189592Sbms			CTR3(KTR_IGMPV3, "process v2 query %s on ifp %p(%s)",
872189592Sbms			    inet_ntoa(igmp->igmp_group), ifp, ifp->if_xname);
873189592Sbms			igmp_v2_update_group(inm, timer);
874189592Sbms		}
875189592Sbms	}
876189592Sbms
877189592Sbmsout_locked:
878189592Sbms	IGMP_UNLOCK();
879189592Sbms	IN_MULTI_UNLOCK();
880189592Sbms
881189592Sbms	return (0);
8822531Swollman}
8832531Swollman
884189592Sbms/*
885189592Sbms * Update the report timer on a group in response to an IGMPv2 query.
886189592Sbms *
887189592Sbms * If we are becoming the reporting member for this group, start the timer.
888189592Sbms * If we already are the reporting member for this group, and timer is
889189592Sbms * below the threshold, reset it.
890189592Sbms *
891189592Sbms * We may be updating the group for the first time since we switched
892189592Sbms * to IGMPv3. If we are, then we must clear any recorded source lists,
893189592Sbms * and transition to REPORTING state; the group timer is overloaded
894189592Sbms * for group and group-source query responses.
895189592Sbms *
896189592Sbms * Unlike IGMPv3, the delay per group should be jittered
897189592Sbms * to avoid bursts of IGMPv2 reports.
898189592Sbms */
899189592Sbmsstatic void
900189592Sbmsigmp_v2_update_group(struct in_multi *inm, const int timer)
901189592Sbms{
902189592Sbms
903189592Sbms	CTR4(KTR_IGMPV3, "%s: %s/%s timer=%d", __func__,
904189592Sbms	    inet_ntoa(inm->inm_addr), inm->inm_ifp->if_xname, timer);
905189592Sbms
906189592Sbms	IN_MULTI_LOCK_ASSERT();
907189592Sbms
908189592Sbms	switch (inm->inm_state) {
909189592Sbms	case IGMP_NOT_MEMBER:
910189592Sbms	case IGMP_SILENT_MEMBER:
911189592Sbms		break;
912189592Sbms	case IGMP_REPORTING_MEMBER:
913189592Sbms		if (inm->inm_timer != 0 &&
914189592Sbms		    inm->inm_timer <= timer) {
915189592Sbms			CTR1(KTR_IGMPV3, "%s: REPORTING and timer running, "
916189592Sbms			    "skipping.", __func__);
917189592Sbms			break;
918189592Sbms		}
919189592Sbms		/* FALLTHROUGH */
920189592Sbms	case IGMP_SG_QUERY_PENDING_MEMBER:
921189592Sbms	case IGMP_G_QUERY_PENDING_MEMBER:
922189592Sbms	case IGMP_IDLE_MEMBER:
923189592Sbms	case IGMP_LAZY_MEMBER:
924189592Sbms	case IGMP_AWAKENING_MEMBER:
925189592Sbms		CTR1(KTR_IGMPV3, "%s: ->REPORTING", __func__);
926189592Sbms		inm->inm_state = IGMP_REPORTING_MEMBER;
927189592Sbms		inm->inm_timer = IGMP_RANDOM_DELAY(timer);
928189592Sbms		V_current_state_timers_running = 1;
929189592Sbms		break;
930189592Sbms	case IGMP_SLEEPING_MEMBER:
931189592Sbms		CTR1(KTR_IGMPV3, "%s: ->AWAKENING", __func__);
932189592Sbms		inm->inm_state = IGMP_AWAKENING_MEMBER;
933189592Sbms		break;
934189592Sbms	case IGMP_LEAVING_MEMBER:
935189592Sbms		break;
936189592Sbms	}
937189592Sbms}
938189592Sbms
939189592Sbms/*
940189592Sbms * Process a received IGMPv3 general, group-specific or
941189592Sbms * group-and-source-specific query.
942189592Sbms * Assumes m has already been pulled up to the full IGMP message length.
943189592Sbms * Return 0 if successful, otherwise an appropriate error code is returned.
944189592Sbms */
945189592Sbmsstatic int
946189592Sbmsigmp_input_v3_query(struct ifnet *ifp, const struct ip *ip,
947189592Sbms    /*const*/ struct igmpv3 *igmpv3)
948189592Sbms{
949189592Sbms	struct igmp_ifinfo	*igi;
950189592Sbms	struct in_multi		*inm;
951193231Sbms	int			 is_general_query;
952189592Sbms	uint32_t		 maxresp, nsrc, qqi;
953189592Sbms	uint16_t		 timer;
954189592Sbms	uint8_t			 qrv;
955189592Sbms
956193231Sbms	is_general_query = 0;
957193231Sbms
958189592Sbms	CTR2(KTR_IGMPV3, "process v3 query on ifp %p(%s)", ifp, ifp->if_xname);
959189592Sbms
960189592Sbms	maxresp = igmpv3->igmp_code;	/* in 1/10ths of a second */
961189592Sbms	if (maxresp >= 128) {
962189592Sbms		maxresp = IGMP_MANT(igmpv3->igmp_code) <<
963189592Sbms			  (IGMP_EXP(igmpv3->igmp_code) + 3);
964189592Sbms	}
965189592Sbms
966189592Sbms	/*
967189592Sbms	 * Robustness must never be less than 2 for on-wire IGMPv3.
968193231Sbms	 * FUTURE: Check if ifp has IGIF_LOOPBACK set, as we will make
969189592Sbms	 * an exception for interfaces whose IGMPv3 state changes
970189592Sbms	 * are redirected to loopback (e.g. MANET).
971189592Sbms	 */
972189592Sbms	qrv = IGMP_QRV(igmpv3->igmp_misc);
973189592Sbms	if (qrv < 2) {
974189592Sbms		CTR3(KTR_IGMPV3, "%s: clamping qrv %d to %d", __func__,
975189592Sbms		    qrv, IGMP_RV_INIT);
976189592Sbms		qrv = IGMP_RV_INIT;
977189592Sbms	}
978189592Sbms
979189592Sbms	qqi = igmpv3->igmp_qqi;
980189592Sbms	if (qqi >= 128) {
981190691Sbms		qqi = IGMP_MANT(igmpv3->igmp_qqi) <<
982190691Sbms		     (IGMP_EXP(igmpv3->igmp_qqi) + 3);
983189592Sbms	}
984189592Sbms
985189592Sbms	timer = maxresp * PR_FASTHZ / IGMP_TIMER_SCALE;
986189592Sbms	if (timer == 0)
987189592Sbms		timer = 1;
988189592Sbms
989189592Sbms	nsrc = ntohs(igmpv3->igmp_numsrc);
990189592Sbms
991193231Sbms	/*
992193231Sbms	 * Validate address fields and versions upfront before
993193231Sbms	 * accepting v3 query.
994193231Sbms	 * XXX SMPng: Unlocked access to igmpstat counters here.
995193231Sbms	 */
996193231Sbms	if (in_nullhost(igmpv3->igmp_group)) {
997193231Sbms		/*
998193231Sbms		 * IGMPv3 General Query.
999193231Sbms		 *
1000193231Sbms		 * General Queries SHOULD be directed to 224.0.0.1.
1001193231Sbms		 * A general query with a source list has undefined
1002193231Sbms		 * behaviour; discard it.
1003193231Sbms		 */
1004193231Sbms		IGMPSTAT_INC(igps_rcv_gen_queries);
1005193231Sbms		if (!in_allhosts(ip->ip_dst) || nsrc > 0) {
1006193231Sbms			IGMPSTAT_INC(igps_rcv_badqueries);
1007193231Sbms			return (0);
1008193231Sbms		}
1009193231Sbms		is_general_query = 1;
1010193231Sbms	} else {
1011193231Sbms		/* Group or group-source specific query. */
1012193231Sbms		if (nsrc == 0)
1013193231Sbms			IGMPSTAT_INC(igps_rcv_group_queries);
1014193231Sbms		else
1015193231Sbms			IGMPSTAT_INC(igps_rcv_gsr_queries);
1016193231Sbms	}
1017193231Sbms
1018189592Sbms	IN_MULTI_LOCK();
1019189592Sbms	IGMP_LOCK();
1020189592Sbms
1021189592Sbms	igi = ((struct in_ifinfo *)ifp->if_afdata[AF_INET])->ii_igmp;
1022189592Sbms	KASSERT(igi != NULL, ("%s: no igmp_ifinfo for ifp %p", __func__, ifp));
1023189592Sbms
1024189592Sbms	if (igi->igi_flags & IGIF_LOOPBACK) {
1025189592Sbms		CTR2(KTR_IGMPV3, "ignore v3 query on IGIF_LOOPBACK ifp %p(%s)",
1026189592Sbms		    ifp, ifp->if_xname);
1027189592Sbms		goto out_locked;
1028189592Sbms	}
1029189592Sbms
1030193231Sbms	/*
1031193231Sbms	 * Discard the v3 query if we're in Compatibility Mode.
1032193231Sbms	 * The RFC is not obviously worded that hosts need to stay in
1033193231Sbms	 * compatibility mode until the Old Version Querier Present
1034193231Sbms	 * timer expires.
1035193231Sbms	 */
1036193231Sbms	if (igi->igi_version != IGMP_VERSION_3) {
1037193231Sbms		CTR3(KTR_IGMPV3, "ignore v3 query in v%d mode on ifp %p(%s)",
1038193231Sbms		    igi->igi_version, ifp, ifp->if_xname);
1039193231Sbms		goto out_locked;
1040193231Sbms	}
1041193231Sbms
1042189592Sbms	igmp_set_version(igi, IGMP_VERSION_3);
1043189592Sbms	igi->igi_rv = qrv;
1044189592Sbms	igi->igi_qi = qqi;
1045189592Sbms	igi->igi_qri = maxresp;
1046189592Sbms
1047189592Sbms	CTR4(KTR_IGMPV3, "%s: qrv %d qi %d qri %d", __func__, qrv, qqi,
1048189592Sbms	    maxresp);
1049189592Sbms
1050193231Sbms	if (is_general_query) {
1051189592Sbms		/*
1052189592Sbms		 * Schedule a current-state report on this ifp for
1053189592Sbms		 * all groups, possibly containing source lists.
1054189592Sbms		 * If there is a pending General Query response
1055189592Sbms		 * scheduled earlier than the selected delay, do
1056189592Sbms		 * not schedule any other reports.
1057189592Sbms		 * Otherwise, reset the interface timer.
1058189592Sbms		 */
1059193231Sbms		CTR2(KTR_IGMPV3, "process v3 general query on ifp %p(%s)",
1060193231Sbms		    ifp, ifp->if_xname);
1061189592Sbms		if (igi->igi_v3_timer == 0 || igi->igi_v3_timer >= timer) {
1062189592Sbms			igi->igi_v3_timer = IGMP_RANDOM_DELAY(timer);
1063189592Sbms			V_interface_timers_running = 1;
1064189592Sbms		}
1065189592Sbms	} else {
1066189592Sbms		/*
1067189592Sbms		 * Group-source-specific queries are throttled on
1068189592Sbms		 * a per-group basis to defeat denial-of-service attempts.
1069189592Sbms		 * Queries for groups we are not a member of on this
1070189592Sbms		 * link are simply ignored.
1071189592Sbms		 */
1072189592Sbms		inm = inm_lookup(ifp, igmpv3->igmp_group);
1073189592Sbms		if (inm == NULL)
1074189592Sbms			goto out_locked;
1075189592Sbms		if (nsrc > 0) {
1076189592Sbms			if (!ratecheck(&inm->inm_lastgsrtv,
1077189592Sbms			    &V_igmp_gsrdelay)) {
1078189592Sbms				CTR1(KTR_IGMPV3, "%s: GS query throttled.",
1079189592Sbms				    __func__);
1080190965Srwatson				IGMPSTAT_INC(igps_drop_gsr_queries);
1081189592Sbms				goto out_locked;
1082189592Sbms			}
1083189592Sbms		}
1084189592Sbms		CTR3(KTR_IGMPV3, "process v3 %s query on ifp %p(%s)",
1085189592Sbms		     inet_ntoa(igmpv3->igmp_group), ifp, ifp->if_xname);
1086189592Sbms		/*
1087189592Sbms		 * If there is a pending General Query response
1088189592Sbms		 * scheduled sooner than the selected delay, no
1089189592Sbms		 * further report need be scheduled.
1090189592Sbms		 * Otherwise, prepare to respond to the
1091189592Sbms		 * group-specific or group-and-source query.
1092189592Sbms		 */
1093189592Sbms		if (igi->igi_v3_timer == 0 || igi->igi_v3_timer >= timer)
1094189592Sbms			igmp_input_v3_group_query(inm, igi, timer, igmpv3);
1095189592Sbms	}
1096189592Sbms
1097189592Sbmsout_locked:
1098189592Sbms	IGMP_UNLOCK();
1099189592Sbms	IN_MULTI_UNLOCK();
1100189592Sbms
1101189592Sbms	return (0);
1102189592Sbms}
1103189592Sbms
1104189592Sbms/*
1105189592Sbms * Process a recieved IGMPv3 group-specific or group-and-source-specific
1106189592Sbms * query.
1107189592Sbms * Return <0 if any error occured. Currently this is ignored.
1108189592Sbms */
1109189592Sbmsstatic int
1110189592Sbmsigmp_input_v3_group_query(struct in_multi *inm, struct igmp_ifinfo *igi,
1111189592Sbms    int timer, /*const*/ struct igmpv3 *igmpv3)
1112189592Sbms{
1113189592Sbms	int			 retval;
1114189592Sbms	uint16_t		 nsrc;
1115189592Sbms
1116189592Sbms	IN_MULTI_LOCK_ASSERT();
1117189592Sbms	IGMP_LOCK_ASSERT();
1118189592Sbms
1119189592Sbms	retval = 0;
1120189592Sbms
1121189592Sbms	switch (inm->inm_state) {
1122189592Sbms	case IGMP_NOT_MEMBER:
1123189592Sbms	case IGMP_SILENT_MEMBER:
1124189592Sbms	case IGMP_SLEEPING_MEMBER:
1125189592Sbms	case IGMP_LAZY_MEMBER:
1126189592Sbms	case IGMP_AWAKENING_MEMBER:
1127189592Sbms	case IGMP_IDLE_MEMBER:
1128189592Sbms	case IGMP_LEAVING_MEMBER:
1129189592Sbms		return (retval);
1130189592Sbms		break;
1131189592Sbms	case IGMP_REPORTING_MEMBER:
1132189592Sbms	case IGMP_G_QUERY_PENDING_MEMBER:
1133189592Sbms	case IGMP_SG_QUERY_PENDING_MEMBER:
1134189592Sbms		break;
1135189592Sbms	}
1136189592Sbms
1137189592Sbms	nsrc = ntohs(igmpv3->igmp_numsrc);
1138189592Sbms
1139189592Sbms	/*
1140189592Sbms	 * Deal with group-specific queries upfront.
1141189592Sbms	 * If any group query is already pending, purge any recorded
1142189592Sbms	 * source-list state if it exists, and schedule a query response
1143189592Sbms	 * for this group-specific query.
1144189592Sbms	 */
1145189592Sbms	if (nsrc == 0) {
1146189592Sbms		if (inm->inm_state == IGMP_G_QUERY_PENDING_MEMBER ||
1147189592Sbms		    inm->inm_state == IGMP_SG_QUERY_PENDING_MEMBER) {
1148189592Sbms			inm_clear_recorded(inm);
1149189592Sbms			timer = min(inm->inm_timer, timer);
1150189592Sbms		}
1151189592Sbms		inm->inm_state = IGMP_G_QUERY_PENDING_MEMBER;
1152189592Sbms		inm->inm_timer = IGMP_RANDOM_DELAY(timer);
1153189592Sbms		V_current_state_timers_running = 1;
1154189592Sbms		return (retval);
1155189592Sbms	}
1156189592Sbms
1157189592Sbms	/*
1158189592Sbms	 * Deal with the case where a group-and-source-specific query has
1159189592Sbms	 * been received but a group-specific query is already pending.
1160189592Sbms	 */
1161189592Sbms	if (inm->inm_state == IGMP_G_QUERY_PENDING_MEMBER) {
1162189592Sbms		timer = min(inm->inm_timer, timer);
1163189592Sbms		inm->inm_timer = IGMP_RANDOM_DELAY(timer);
1164189592Sbms		V_current_state_timers_running = 1;
1165189592Sbms		return (retval);
1166189592Sbms	}
1167189592Sbms
1168189592Sbms	/*
1169189592Sbms	 * Finally, deal with the case where a group-and-source-specific
1170189592Sbms	 * query has been received, where a response to a previous g-s-r
1171189592Sbms	 * query exists, or none exists.
1172189592Sbms	 * In this case, we need to parse the source-list which the Querier
1173189592Sbms	 * has provided us with and check if we have any source list filter
1174189592Sbms	 * entries at T1 for these sources. If we do not, there is no need
1175189592Sbms	 * schedule a report and the query may be dropped.
1176189592Sbms	 * If we do, we must record them and schedule a current-state
1177189592Sbms	 * report for those sources.
1178189592Sbms	 * FIXME: Handling source lists larger than 1 mbuf requires that
1179189592Sbms	 * we pass the mbuf chain pointer down to this function, and use
1180189592Sbms	 * m_getptr() to walk the chain.
1181189592Sbms	 */
1182189592Sbms	if (inm->inm_nsrc > 0) {
1183189592Sbms		const struct in_addr	*ap;
1184189592Sbms		int			 i, nrecorded;
1185189592Sbms
1186189592Sbms		ap = (const struct in_addr *)(igmpv3 + 1);
1187189592Sbms		nrecorded = 0;
1188189592Sbms		for (i = 0; i < nsrc; i++, ap++) {
1189189592Sbms			retval = inm_record_source(inm, ap->s_addr);
1190189592Sbms			if (retval < 0)
1191189592Sbms				break;
1192189592Sbms			nrecorded += retval;
1193189592Sbms		}
1194189592Sbms		if (nrecorded > 0) {
1195189592Sbms			CTR1(KTR_IGMPV3,
1196189592Sbms			    "%s: schedule response to SG query", __func__);
1197189592Sbms			inm->inm_state = IGMP_SG_QUERY_PENDING_MEMBER;
1198189592Sbms			inm->inm_timer = IGMP_RANDOM_DELAY(timer);
1199189592Sbms			V_current_state_timers_running = 1;
1200189592Sbms		}
1201189592Sbms	}
1202189592Sbms
1203189592Sbms	return (retval);
1204189592Sbms}
1205189592Sbms
1206189592Sbms/*
1207189592Sbms * Process a received IGMPv1 host membership report.
1208189592Sbms *
1209189592Sbms * NOTE: 0.0.0.0 workaround breaks const correctness.
1210189592Sbms */
1211189592Sbmsstatic int
1212189592Sbmsigmp_input_v1_report(struct ifnet *ifp, /*const*/ struct ip *ip,
1213189592Sbms    /*const*/ struct igmp *igmp)
1214189592Sbms{
1215189592Sbms	struct in_ifaddr *ia;
1216189592Sbms	struct in_multi *inm;
1217189592Sbms
1218190965Srwatson	IGMPSTAT_INC(igps_rcv_reports);
1219189592Sbms
1220189592Sbms	if (ifp->if_flags & IFF_LOOPBACK)
1221189592Sbms		return (0);
1222189592Sbms
1223213325Sbz	if (!IN_MULTICAST(ntohl(igmp->igmp_group.s_addr)) ||
1224213325Sbz	    !in_hosteq(igmp->igmp_group, ip->ip_dst)) {
1225190965Srwatson		IGMPSTAT_INC(igps_rcv_badreports);
1226189592Sbms		return (EINVAL);
1227189592Sbms	}
1228189592Sbms
1229189592Sbms	/*
1230189592Sbms	 * RFC 3376, Section 4.2.13, 9.2, 9.3:
1231189592Sbms	 * Booting clients may use the source address 0.0.0.0. Some
1232189592Sbms	 * IGMP daemons may not know how to use IP_RECVIF to determine
1233189592Sbms	 * the interface upon which this message was received.
1234189592Sbms	 * Replace 0.0.0.0 with the subnet address if told to do so.
1235189592Sbms	 */
1236189592Sbms	if (V_igmp_recvifkludge && in_nullhost(ip->ip_src)) {
1237189592Sbms		IFP_TO_IA(ifp, ia);
1238194760Srwatson		if (ia != NULL) {
1239189592Sbms			ip->ip_src.s_addr = htonl(ia->ia_subnet);
1240194760Srwatson			ifa_free(&ia->ia_ifa);
1241194760Srwatson		}
1242189592Sbms	}
1243189592Sbms
1244189592Sbms	CTR3(KTR_IGMPV3, "process v1 report %s on ifp %p(%s)",
1245189592Sbms	     inet_ntoa(igmp->igmp_group), ifp, ifp->if_xname);
1246189592Sbms
1247189592Sbms	/*
1248189592Sbms	 * IGMPv1 report suppression.
1249189592Sbms	 * If we are a member of this group, and our membership should be
1250189592Sbms	 * reported, stop our group timer and transition to the 'lazy' state.
1251189592Sbms	 */
1252189592Sbms	IN_MULTI_LOCK();
1253189592Sbms	inm = inm_lookup(ifp, igmp->igmp_group);
1254189592Sbms	if (inm != NULL) {
1255189592Sbms		struct igmp_ifinfo *igi;
1256189592Sbms
1257189592Sbms		igi = inm->inm_igi;
1258189592Sbms		if (igi == NULL) {
1259189592Sbms			KASSERT(igi != NULL,
1260189592Sbms			    ("%s: no igi for ifp %p", __func__, ifp));
1261189592Sbms			goto out_locked;
1262189592Sbms		}
1263189592Sbms
1264190965Srwatson		IGMPSTAT_INC(igps_rcv_ourreports);
1265189592Sbms
1266189592Sbms		/*
1267189592Sbms		 * If we are in IGMPv3 host mode, do not allow the
1268189592Sbms		 * other host's IGMPv1 report to suppress our reports
1269189592Sbms		 * unless explicitly configured to do so.
1270189592Sbms		 */
1271189592Sbms		if (igi->igi_version == IGMP_VERSION_3) {
1272189592Sbms			if (V_igmp_legacysupp)
1273189592Sbms				igmp_v3_suppress_group_record(inm);
1274189592Sbms			goto out_locked;
1275189592Sbms		}
1276189592Sbms
1277189592Sbms		inm->inm_timer = 0;
1278189592Sbms
1279189592Sbms		switch (inm->inm_state) {
1280189592Sbms		case IGMP_NOT_MEMBER:
1281189592Sbms		case IGMP_SILENT_MEMBER:
1282189592Sbms			break;
1283189592Sbms		case IGMP_IDLE_MEMBER:
1284189592Sbms		case IGMP_LAZY_MEMBER:
1285189592Sbms		case IGMP_AWAKENING_MEMBER:
1286189592Sbms			CTR3(KTR_IGMPV3,
1287189592Sbms			    "report suppressed for %s on ifp %p(%s)",
1288189592Sbms			    inet_ntoa(igmp->igmp_group), ifp, ifp->if_xname);
1289189592Sbms		case IGMP_SLEEPING_MEMBER:
1290189592Sbms			inm->inm_state = IGMP_SLEEPING_MEMBER;
1291189592Sbms			break;
1292189592Sbms		case IGMP_REPORTING_MEMBER:
1293189592Sbms			CTR3(KTR_IGMPV3,
1294189592Sbms			    "report suppressed for %s on ifp %p(%s)",
1295189592Sbms			    inet_ntoa(igmp->igmp_group), ifp, ifp->if_xname);
1296189592Sbms			if (igi->igi_version == IGMP_VERSION_1)
1297189592Sbms				inm->inm_state = IGMP_LAZY_MEMBER;
1298189592Sbms			else if (igi->igi_version == IGMP_VERSION_2)
1299189592Sbms				inm->inm_state = IGMP_SLEEPING_MEMBER;
1300189592Sbms			break;
1301189592Sbms		case IGMP_G_QUERY_PENDING_MEMBER:
1302189592Sbms		case IGMP_SG_QUERY_PENDING_MEMBER:
1303189592Sbms		case IGMP_LEAVING_MEMBER:
1304189592Sbms			break;
1305189592Sbms		}
1306189592Sbms	}
1307189592Sbms
1308189592Sbmsout_locked:
1309189592Sbms	IN_MULTI_UNLOCK();
1310189592Sbms
1311189592Sbms	return (0);
1312189592Sbms}
1313189592Sbms
1314189592Sbms/*
1315189592Sbms * Process a received IGMPv2 host membership report.
1316189592Sbms *
1317189592Sbms * NOTE: 0.0.0.0 workaround breaks const correctness.
1318189592Sbms */
1319189592Sbmsstatic int
1320189592Sbmsigmp_input_v2_report(struct ifnet *ifp, /*const*/ struct ip *ip,
1321189592Sbms    /*const*/ struct igmp *igmp)
1322189592Sbms{
1323189592Sbms	struct in_ifaddr *ia;
1324189592Sbms	struct in_multi *inm;
1325189592Sbms
1326189592Sbms	/*
1327189592Sbms	 * Make sure we don't hear our own membership report.  Fast
1328189592Sbms	 * leave requires knowing that we are the only member of a
1329189592Sbms	 * group.
1330189592Sbms	 */
1331189592Sbms	IFP_TO_IA(ifp, ia);
1332194760Srwatson	if (ia != NULL && in_hosteq(ip->ip_src, IA_SIN(ia)->sin_addr)) {
1333194760Srwatson		ifa_free(&ia->ia_ifa);
1334189592Sbms		return (0);
1335194760Srwatson	}
1336189592Sbms
1337190965Srwatson	IGMPSTAT_INC(igps_rcv_reports);
1338189592Sbms
1339194760Srwatson	if (ifp->if_flags & IFF_LOOPBACK) {
1340194760Srwatson		if (ia != NULL)
1341194760Srwatson			ifa_free(&ia->ia_ifa);
1342189592Sbms		return (0);
1343194760Srwatson	}
1344189592Sbms
1345189592Sbms	if (!IN_MULTICAST(ntohl(igmp->igmp_group.s_addr)) ||
1346189592Sbms	    !in_hosteq(igmp->igmp_group, ip->ip_dst)) {
1347194760Srwatson		if (ia != NULL)
1348194760Srwatson			ifa_free(&ia->ia_ifa);
1349190965Srwatson		IGMPSTAT_INC(igps_rcv_badreports);
1350189592Sbms		return (EINVAL);
1351189592Sbms	}
1352189592Sbms
1353189592Sbms	/*
1354189592Sbms	 * RFC 3376, Section 4.2.13, 9.2, 9.3:
1355189592Sbms	 * Booting clients may use the source address 0.0.0.0. Some
1356189592Sbms	 * IGMP daemons may not know how to use IP_RECVIF to determine
1357189592Sbms	 * the interface upon which this message was received.
1358189592Sbms	 * Replace 0.0.0.0 with the subnet address if told to do so.
1359189592Sbms	 */
1360189592Sbms	if (V_igmp_recvifkludge && in_nullhost(ip->ip_src)) {
1361189592Sbms		if (ia != NULL)
1362189592Sbms			ip->ip_src.s_addr = htonl(ia->ia_subnet);
1363189592Sbms	}
1364194760Srwatson	if (ia != NULL)
1365194760Srwatson		ifa_free(&ia->ia_ifa);
1366189592Sbms
1367189592Sbms	CTR3(KTR_IGMPV3, "process v2 report %s on ifp %p(%s)",
1368189592Sbms	     inet_ntoa(igmp->igmp_group), ifp, ifp->if_xname);
1369189592Sbms
1370189592Sbms	/*
1371189592Sbms	 * IGMPv2 report suppression.
1372189592Sbms	 * If we are a member of this group, and our membership should be
1373189592Sbms	 * reported, and our group timer is pending or about to be reset,
1374189592Sbms	 * stop our group timer by transitioning to the 'lazy' state.
1375189592Sbms	 */
1376189592Sbms	IN_MULTI_LOCK();
1377189592Sbms	inm = inm_lookup(ifp, igmp->igmp_group);
1378189592Sbms	if (inm != NULL) {
1379189592Sbms		struct igmp_ifinfo *igi;
1380189592Sbms
1381189592Sbms		igi = inm->inm_igi;
1382189592Sbms		KASSERT(igi != NULL, ("%s: no igi for ifp %p", __func__, ifp));
1383189592Sbms
1384190965Srwatson		IGMPSTAT_INC(igps_rcv_ourreports);
1385189592Sbms
1386189592Sbms		/*
1387189592Sbms		 * If we are in IGMPv3 host mode, do not allow the
1388189592Sbms		 * other host's IGMPv1 report to suppress our reports
1389189592Sbms		 * unless explicitly configured to do so.
1390189592Sbms		 */
1391189592Sbms		if (igi->igi_version == IGMP_VERSION_3) {
1392189592Sbms			if (V_igmp_legacysupp)
1393189592Sbms				igmp_v3_suppress_group_record(inm);
1394189592Sbms			goto out_locked;
1395189592Sbms		}
1396189592Sbms
1397189592Sbms		inm->inm_timer = 0;
1398189592Sbms
1399189592Sbms		switch (inm->inm_state) {
1400189592Sbms		case IGMP_NOT_MEMBER:
1401189592Sbms		case IGMP_SILENT_MEMBER:
1402189592Sbms		case IGMP_SLEEPING_MEMBER:
1403189592Sbms			break;
1404189592Sbms		case IGMP_REPORTING_MEMBER:
1405189592Sbms		case IGMP_IDLE_MEMBER:
1406189592Sbms		case IGMP_AWAKENING_MEMBER:
1407189592Sbms			CTR3(KTR_IGMPV3,
1408189592Sbms			    "report suppressed for %s on ifp %p(%s)",
1409189592Sbms			    inet_ntoa(igmp->igmp_group), ifp, ifp->if_xname);
1410189592Sbms		case IGMP_LAZY_MEMBER:
1411189592Sbms			inm->inm_state = IGMP_LAZY_MEMBER;
1412189592Sbms			break;
1413189592Sbms		case IGMP_G_QUERY_PENDING_MEMBER:
1414189592Sbms		case IGMP_SG_QUERY_PENDING_MEMBER:
1415189592Sbms		case IGMP_LEAVING_MEMBER:
1416189592Sbms			break;
1417189592Sbms		}
1418189592Sbms	}
1419189592Sbms
1420189592Sbmsout_locked:
1421189592Sbms	IN_MULTI_UNLOCK();
1422189592Sbms
1423189592Sbms	return (0);
1424189592Sbms}
1425189592Sbms
14261541Srgrimesvoid
1427189592Sbmsigmp_input(struct mbuf *m, int off)
14281541Srgrimes{
1429189592Sbms	int iphlen;
1430189592Sbms	struct ifnet *ifp;
1431189592Sbms	struct igmp *igmp;
1432189592Sbms	struct ip *ip;
1433189592Sbms	int igmplen;
1434189592Sbms	int minlen;
1435189592Sbms	int queryver;
1436189592Sbms
1437189592Sbms	CTR3(KTR_IGMPV3, "%s: called w/mbuf (%p,%d)", __func__, m, off);
1438189592Sbms
1439189592Sbms	ifp = m->m_pkthdr.rcvif;
14401541Srgrimes
1441190965Srwatson	IGMPSTAT_INC(igps_rcv_total);
14421541Srgrimes
14431541Srgrimes	ip = mtod(m, struct ip *);
1444189592Sbms	iphlen = off;
14451541Srgrimes	igmplen = ip->ip_len;
14461541Srgrimes
14471541Srgrimes	/*
1448164863Srwatson	 * Validate lengths.
14491541Srgrimes	 */
14501541Srgrimes	if (igmplen < IGMP_MINLEN) {
1451190965Srwatson		IGMPSTAT_INC(igps_rcv_tooshort);
14521541Srgrimes		m_freem(m);
14531541Srgrimes		return;
14541541Srgrimes	}
1455189592Sbms
1456189592Sbms	/*
1457189592Sbms	 * Always pullup to the minimum size for v1/v2 or v3
1458189592Sbms	 * to amortize calls to m_pullup().
1459189592Sbms	 */
1460189592Sbms	minlen = iphlen;
1461189592Sbms	if (igmplen >= IGMP_V3_QUERY_MINLEN)
1462189592Sbms		minlen += IGMP_V3_QUERY_MINLEN;
1463189592Sbms	else
1464189592Sbms		minlen += IGMP_MINLEN;
14651541Srgrimes	if ((m->m_flags & M_EXT || m->m_len < minlen) &&
14661541Srgrimes	    (m = m_pullup(m, minlen)) == 0) {
1467190965Srwatson		IGMPSTAT_INC(igps_rcv_tooshort);
14681541Srgrimes		return;
14691541Srgrimes	}
1470189592Sbms	ip = mtod(m, struct ip *);
14711541Srgrimes
14721541Srgrimes	/*
1473164863Srwatson	 * Validate checksum.
14741541Srgrimes	 */
14751541Srgrimes	m->m_data += iphlen;
14761541Srgrimes	m->m_len -= iphlen;
14771541Srgrimes	igmp = mtod(m, struct igmp *);
14781541Srgrimes	if (in_cksum(m, igmplen)) {
1479190965Srwatson		IGMPSTAT_INC(igps_rcv_badsum);
14801541Srgrimes		m_freem(m);
14811541Srgrimes		return;
14821541Srgrimes	}
14831541Srgrimes	m->m_data -= iphlen;
14841541Srgrimes	m->m_len += iphlen;
14852531Swollman
1486207275Sbms	/*
1487207275Sbms	 * IGMP control traffic is link-scope, and must have a TTL of 1.
1488207275Sbms	 * DVMRP traffic (e.g. mrinfo, mtrace) is an exception;
1489207275Sbms	 * probe packets may come from beyond the LAN.
1490207275Sbms	 */
1491207275Sbms	if (igmp->igmp_type != IGMP_DVMRP && ip->ip_ttl != 1) {
1492207275Sbms		IGMPSTAT_INC(igps_rcv_badttl);
1493207275Sbms		m_freem(m);
1494207275Sbms		return;
1495207275Sbms	}
1496207275Sbms
14971541Srgrimes	switch (igmp->igmp_type) {
1498189592Sbms	case IGMP_HOST_MEMBERSHIP_QUERY:
1499189592Sbms		if (igmplen == IGMP_MINLEN) {
1500189592Sbms			if (igmp->igmp_code == 0)
1501189592Sbms				queryver = IGMP_VERSION_1;
1502189592Sbms			else
1503189592Sbms				queryver = IGMP_VERSION_2;
1504189592Sbms		} else if (igmplen >= IGMP_V3_QUERY_MINLEN) {
1505189592Sbms			queryver = IGMP_VERSION_3;
1506189592Sbms		} else {
1507190965Srwatson			IGMPSTAT_INC(igps_rcv_tooshort);
1508189592Sbms			m_freem(m);
1509189592Sbms			return;
1510189592Sbms		}
15111541Srgrimes
1512189592Sbms		switch (queryver) {
1513189592Sbms		case IGMP_VERSION_1:
1514190965Srwatson			IGMPSTAT_INC(igps_rcv_v1v2_queries);
1515189592Sbms			if (!V_igmp_v1enable)
1516189592Sbms				break;
1517193231Sbms			if (igmp_input_v1_query(ifp, ip, igmp) != 0) {
1518144163Ssam				m_freem(m);
1519144163Ssam				return;
1520144163Ssam			}
1521189592Sbms			break;
15224028Spst
1523189592Sbms		case IGMP_VERSION_2:
1524190965Srwatson			IGMPSTAT_INC(igps_rcv_v1v2_queries);
1525189592Sbms			if (!V_igmp_v2enable)
1526189592Sbms				break;
1527189592Sbms			if (igmp_input_v2_query(ifp, ip, igmp) != 0) {
15282531Swollman				m_freem(m);
15292531Swollman				return;
15302531Swollman			}
1531189592Sbms			break;
15322531Swollman
1533189592Sbms		case IGMP_VERSION_3: {
1534189592Sbms				struct igmpv3 *igmpv3;
1535189592Sbms				uint16_t igmpv3len;
1536279265Sdelphij				uint16_t nsrc;
1537189592Sbms
1538190965Srwatson				IGMPSTAT_INC(igps_rcv_v3_queries);
1539189592Sbms				igmpv3 = (struct igmpv3 *)igmp;
1540189592Sbms				/*
1541189592Sbms				 * Validate length based on source count.
1542189592Sbms				 */
1543189592Sbms				nsrc = ntohs(igmpv3->igmp_numsrc);
1544281233Sdelphij				if (nsrc * sizeof(in_addr_t) >
1545281233Sdelphij				    UINT16_MAX - iphlen - IGMP_V3_QUERY_MINLEN) {
1546190965Srwatson					IGMPSTAT_INC(igps_rcv_tooshort);
1547189592Sbms					return;
15482531Swollman				}
1549189592Sbms				/*
1550189592Sbms				 * m_pullup() may modify m, so pullup in
1551189592Sbms				 * this scope.
1552189592Sbms				 */
1553189592Sbms				igmpv3len = iphlen + IGMP_V3_QUERY_MINLEN +
1554281233Sdelphij				    sizeof(struct in_addr) * nsrc;
1555189592Sbms				if ((m->m_flags & M_EXT ||
1556189592Sbms				     m->m_len < igmpv3len) &&
1557189592Sbms				    (m = m_pullup(m, igmpv3len)) == NULL) {
1558190965Srwatson					IGMPSTAT_INC(igps_rcv_tooshort);
1559189592Sbms					return;
1560189592Sbms				}
1561189592Sbms				igmpv3 = (struct igmpv3 *)(mtod(m, uint8_t *)
1562189592Sbms				    + iphlen);
1563189592Sbms				if (igmp_input_v3_query(ifp, ip, igmpv3) != 0) {
1564189592Sbms					m_freem(m);
1565189592Sbms					return;
1566189592Sbms				}
15671541Srgrimes			}
1568189592Sbms			break;
15691541Srgrimes		}
15701541Srgrimes		break;
15711541Srgrimes
1572189592Sbms	case IGMP_v1_HOST_MEMBERSHIP_REPORT:
1573189592Sbms		if (!V_igmp_v1enable)
157414622Sfenner			break;
1575189592Sbms		if (igmp_input_v1_report(ifp, ip, igmp) != 0) {
1576189592Sbms			m_freem(m);
1577189592Sbms			return;
1578189592Sbms		}
1579189592Sbms		break;
158014622Sfenner
1581189592Sbms	case IGMP_v2_HOST_MEMBERSHIP_REPORT:
1582189592Sbms		if (!V_igmp_v2enable)
15831541Srgrimes			break;
1584189592Sbms		if (!ip_checkrouteralert(m))
1585190965Srwatson			IGMPSTAT_INC(igps_rcv_nora);
1586189592Sbms		if (igmp_input_v2_report(ifp, ip, igmp) != 0) {
15871541Srgrimes			m_freem(m);
15881541Srgrimes			return;
15891541Srgrimes		}
1590189592Sbms		break;
15911541Srgrimes
1592189592Sbms	case IGMP_v3_HOST_MEMBERSHIP_REPORT:
15931541Srgrimes		/*
1594189592Sbms		 * Hosts do not need to process IGMPv3 membership reports,
1595189592Sbms		 * as report suppression is no longer required.
15961541Srgrimes		 */
1597189592Sbms		if (!ip_checkrouteralert(m))
1598190965Srwatson			IGMPSTAT_INC(igps_rcv_nora);
1599189592Sbms		break;
16001541Srgrimes
1601189592Sbms	default:
16021541Srgrimes		break;
16031541Srgrimes	}
16041541Srgrimes
16051541Srgrimes	/*
1606164863Srwatson	 * Pass all valid IGMP packets up to any process(es) listening on a
1607164863Srwatson	 * raw IGMP socket.
16081541Srgrimes	 */
160982890Sjulian	rip_input(m, off);
16101541Srgrimes}
16111541Srgrimes
1612189592Sbms
1613189592Sbms/*
1614189592Sbms * Fast timeout handler (global).
1615189592Sbms * VIMAGE: Timeout handlers are expected to service all vimages.
1616189592Sbms */
16171541Srgrimesvoid
1618189592Sbmsigmp_fasttimo(void)
16191541Srgrimes{
1620189592Sbms	VNET_ITERATOR_DECL(vnet_iter);
16211541Srgrimes
1622195760Srwatson	VNET_LIST_RLOCK_NOSLEEP();
1623189592Sbms	VNET_FOREACH(vnet_iter) {
1624189592Sbms		CURVNET_SET(vnet_iter);
1625189592Sbms		igmp_fasttimo_vnet();
1626189592Sbms		CURVNET_RESTORE();
1627189592Sbms	}
1628195760Srwatson	VNET_LIST_RUNLOCK_NOSLEEP();
1629189592Sbms}
1630189592Sbms
1631189592Sbms/*
1632189592Sbms * Fast timeout handler (per-vnet).
1633189592Sbms * Sends are shuffled off to a netisr to deal with Giant.
1634189592Sbms *
1635189592Sbms * VIMAGE: Assume caller has set up our curvnet.
1636189592Sbms */
1637189592Sbmsstatic void
1638189592Sbmsigmp_fasttimo_vnet(void)
1639189592Sbms{
1640189592Sbms	struct ifqueue		 scq;	/* State-change packets */
1641189592Sbms	struct ifqueue		 qrq;	/* Query response packets */
1642189592Sbms	struct ifnet		*ifp;
1643189592Sbms	struct igmp_ifinfo	*igi;
1644230076Sjhb	struct ifmultiaddr	*ifma;
1645189592Sbms	struct in_multi		*inm;
1646189592Sbms	int			 loop, uri_fasthz;
1647189592Sbms
1648189592Sbms	loop = 0;
1649189592Sbms	uri_fasthz = 0;
1650189592Sbms
1651189592Sbms	/*
1652189592Sbms	 * Quick check to see if any work needs to be done, in order to
1653189592Sbms	 * minimize the overhead of fasttimo processing.
1654189592Sbms	 * SMPng: XXX Unlocked reads.
1655189592Sbms	 */
1656189592Sbms	if (!V_current_state_timers_running &&
1657189592Sbms	    !V_interface_timers_running &&
1658189592Sbms	    !V_state_change_timers_running)
1659189592Sbms		return;
1660189592Sbms
1661189592Sbms	IN_MULTI_LOCK();
1662189592Sbms	IGMP_LOCK();
1663189592Sbms
1664189592Sbms	/*
1665189592Sbms	 * IGMPv3 General Query response timer processing.
1666189592Sbms	 */
1667189592Sbms	if (V_interface_timers_running) {
1668189592Sbms		CTR1(KTR_IGMPV3, "%s: interface timers running", __func__);
1669189592Sbms
1670189592Sbms		V_interface_timers_running = 0;
1671189592Sbms		LIST_FOREACH(igi, &V_igi_head, igi_link) {
1672189592Sbms			if (igi->igi_v3_timer == 0) {
1673189592Sbms				/* Do nothing. */
1674189592Sbms			} else if (--igi->igi_v3_timer == 0) {
1675189592Sbms				igmp_v3_dispatch_general_query(igi);
1676189592Sbms			} else {
1677189592Sbms				V_interface_timers_running = 1;
1678189592Sbms			}
1679189592Sbms		}
1680189592Sbms	}
1681189592Sbms
1682189592Sbms	if (!V_current_state_timers_running &&
1683189592Sbms	    !V_state_change_timers_running)
1684189592Sbms		goto out_locked;
1685189592Sbms
1686189592Sbms	V_current_state_timers_running = 0;
1687189592Sbms	V_state_change_timers_running = 0;
1688189592Sbms
1689189592Sbms	CTR1(KTR_IGMPV3, "%s: state change timers running", __func__);
1690189592Sbms
1691189592Sbms	/*
1692189592Sbms	 * IGMPv1/v2/v3 host report and state-change timer processing.
1693189592Sbms	 * Note: Processing a v3 group timer may remove a node.
1694189592Sbms	 */
1695189592Sbms	LIST_FOREACH(igi, &V_igi_head, igi_link) {
1696189592Sbms		ifp = igi->igi_ifp;
1697189592Sbms
1698189592Sbms		if (igi->igi_version == IGMP_VERSION_3) {
1699189592Sbms			loop = (igi->igi_flags & IGIF_LOOPBACK) ? 1 : 0;
1700189592Sbms			uri_fasthz = IGMP_RANDOM_DELAY(igi->igi_uri *
1701189592Sbms			    PR_FASTHZ);
1702189592Sbms
1703189592Sbms			memset(&qrq, 0, sizeof(struct ifqueue));
1704189592Sbms			IFQ_SET_MAXLEN(&qrq, IGMP_MAX_G_GS_PACKETS);
1705189592Sbms
1706189592Sbms			memset(&scq, 0, sizeof(struct ifqueue));
1707189592Sbms			IFQ_SET_MAXLEN(&scq, IGMP_MAX_STATE_CHANGE_PACKETS);
1708189592Sbms		}
1709189592Sbms
1710233200Sjhb		IF_ADDR_RLOCK(ifp);
1711230076Sjhb		TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
1712189931Sbms			if (ifma->ifma_addr->sa_family != AF_INET ||
1713189931Sbms			    ifma->ifma_protospec == NULL)
1714189592Sbms				continue;
1715189592Sbms			inm = (struct in_multi *)ifma->ifma_protospec;
1716189592Sbms			switch (igi->igi_version) {
1717189592Sbms			case IGMP_VERSION_1:
1718189592Sbms			case IGMP_VERSION_2:
1719189592Sbms				igmp_v1v2_process_group_timer(inm,
1720189592Sbms				    igi->igi_version);
1721189592Sbms				break;
1722189592Sbms			case IGMP_VERSION_3:
1723189592Sbms				igmp_v3_process_group_timers(igi, &qrq,
1724189592Sbms				    &scq, inm, uri_fasthz);
1725189592Sbms				break;
1726189592Sbms			}
1727189592Sbms		}
1728233200Sjhb		IF_ADDR_RUNLOCK(ifp);
1729189592Sbms
1730189592Sbms		if (igi->igi_version == IGMP_VERSION_3) {
1731189592Sbms			struct in_multi		*tinm;
1732189592Sbms
1733189592Sbms			igmp_dispatch_queue(&qrq, 0, loop);
1734189592Sbms			igmp_dispatch_queue(&scq, 0, loop);
1735189592Sbms
1736189592Sbms			/*
1737189592Sbms			 * Free the in_multi reference(s) for this
1738189592Sbms			 * IGMP lifecycle.
1739189592Sbms			 */
1740189592Sbms			SLIST_FOREACH_SAFE(inm, &igi->igi_relinmhead,
1741189592Sbms			    inm_nrele, tinm) {
1742189592Sbms				SLIST_REMOVE_HEAD(&igi->igi_relinmhead,
1743189592Sbms				    inm_nrele);
1744189592Sbms				inm_release_locked(inm);
1745189592Sbms			}
1746189592Sbms		}
1747189592Sbms	}
1748189592Sbms
1749189592Sbmsout_locked:
1750189592Sbms	IGMP_UNLOCK();
1751189592Sbms	IN_MULTI_UNLOCK();
1752189592Sbms}
1753189592Sbms
1754189592Sbms/*
1755189592Sbms * Update host report group timer for IGMPv1/v2.
1756189592Sbms * Will update the global pending timer flags.
1757189592Sbms */
1758189592Sbmsstatic void
1759189592Sbmsigmp_v1v2_process_group_timer(struct in_multi *inm, const int version)
1760189592Sbms{
1761189592Sbms	int report_timer_expired;
1762189592Sbms
1763148682Srwatson	IN_MULTI_LOCK_ASSERT();
1764189592Sbms	IGMP_LOCK_ASSERT();
1765148682Srwatson
1766189592Sbms	if (inm->inm_timer == 0) {
1767189592Sbms		report_timer_expired = 0;
1768189592Sbms	} else if (--inm->inm_timer == 0) {
1769189592Sbms		report_timer_expired = 1;
177014622Sfenner	} else {
1771189592Sbms		V_current_state_timers_running = 1;
1772189592Sbms		return;
1773189592Sbms	}
1774189592Sbms
1775189592Sbms	switch (inm->inm_state) {
1776189592Sbms	case IGMP_NOT_MEMBER:
1777189592Sbms	case IGMP_SILENT_MEMBER:
1778189592Sbms	case IGMP_IDLE_MEMBER:
1779189592Sbms	case IGMP_LAZY_MEMBER:
1780189592Sbms	case IGMP_SLEEPING_MEMBER:
1781189592Sbms	case IGMP_AWAKENING_MEMBER:
1782189592Sbms		break;
1783189592Sbms	case IGMP_REPORTING_MEMBER:
1784189592Sbms		if (report_timer_expired) {
1785189592Sbms			inm->inm_state = IGMP_IDLE_MEMBER;
1786189592Sbms			(void)igmp_v1v2_queue_report(inm,
1787189592Sbms			    (version == IGMP_VERSION_2) ?
1788189592Sbms			     IGMP_v2_HOST_MEMBERSHIP_REPORT :
1789189592Sbms			     IGMP_v1_HOST_MEMBERSHIP_REPORT);
1790144163Ssam		}
1791189592Sbms		break;
1792189592Sbms	case IGMP_G_QUERY_PENDING_MEMBER:
1793189592Sbms	case IGMP_SG_QUERY_PENDING_MEMBER:
1794189592Sbms	case IGMP_LEAVING_MEMBER:
1795189592Sbms		break;
17961541Srgrimes	}
17971541Srgrimes}
17981541Srgrimes
1799189592Sbms/*
1800189592Sbms * Update a group's timers for IGMPv3.
1801189592Sbms * Will update the global pending timer flags.
1802189592Sbms * Note: Unlocked read from igi.
1803189592Sbms */
1804189592Sbmsstatic void
1805189592Sbmsigmp_v3_process_group_timers(struct igmp_ifinfo *igi,
1806189592Sbms    struct ifqueue *qrq, struct ifqueue *scq,
1807189592Sbms    struct in_multi *inm, const int uri_fasthz)
18081541Srgrimes{
1809189592Sbms	int query_response_timer_expired;
1810189592Sbms	int state_change_retransmit_timer_expired;
1811119181Srwatson
1812148682Srwatson	IN_MULTI_LOCK_ASSERT();
1813189592Sbms	IGMP_LOCK_ASSERT();
1814148682Srwatson
1815189592Sbms	query_response_timer_expired = 0;
1816189592Sbms	state_change_retransmit_timer_expired = 0;
1817189592Sbms
1818189592Sbms	/*
1819189592Sbms	 * During a transition from v1/v2 compatibility mode back to v3,
1820189592Sbms	 * a group record in REPORTING state may still have its group
1821189592Sbms	 * timer active. This is a no-op in this function; it is easier
1822189592Sbms	 * to deal with it here than to complicate the slow-timeout path.
1823189592Sbms	 */
1824189592Sbms	if (inm->inm_timer == 0) {
1825189592Sbms		query_response_timer_expired = 0;
1826189592Sbms	} else if (--inm->inm_timer == 0) {
1827189592Sbms		query_response_timer_expired = 1;
1828189592Sbms	} else {
1829189592Sbms		V_current_state_timers_running = 1;
1830189592Sbms	}
1831189592Sbms
1832189592Sbms	if (inm->inm_sctimer == 0) {
1833189592Sbms		state_change_retransmit_timer_expired = 0;
1834189592Sbms	} else if (--inm->inm_sctimer == 0) {
1835189592Sbms		state_change_retransmit_timer_expired = 1;
1836189592Sbms	} else {
1837189592Sbms		V_state_change_timers_running = 1;
1838189592Sbms	}
1839189592Sbms
1840189592Sbms	/* We are in fasttimo, so be quick about it. */
1841189592Sbms	if (!state_change_retransmit_timer_expired &&
1842189592Sbms	    !query_response_timer_expired)
1843189592Sbms		return;
1844189592Sbms
1845189592Sbms	switch (inm->inm_state) {
1846189592Sbms	case IGMP_NOT_MEMBER:
1847189592Sbms	case IGMP_SILENT_MEMBER:
1848189592Sbms	case IGMP_SLEEPING_MEMBER:
1849189592Sbms	case IGMP_LAZY_MEMBER:
1850189592Sbms	case IGMP_AWAKENING_MEMBER:
1851189592Sbms	case IGMP_IDLE_MEMBER:
1852189592Sbms		break;
1853189592Sbms	case IGMP_G_QUERY_PENDING_MEMBER:
1854189592Sbms	case IGMP_SG_QUERY_PENDING_MEMBER:
1855189592Sbms		/*
1856189592Sbms		 * Respond to a previously pending Group-Specific
1857189592Sbms		 * or Group-and-Source-Specific query by enqueueing
1858189592Sbms		 * the appropriate Current-State report for
1859189592Sbms		 * immediate transmission.
1860189592Sbms		 */
1861189592Sbms		if (query_response_timer_expired) {
1862189592Sbms			int retval;
1863189592Sbms
1864189592Sbms			retval = igmp_v3_enqueue_group_record(qrq, inm, 0, 1,
1865189592Sbms			    (inm->inm_state == IGMP_SG_QUERY_PENDING_MEMBER));
1866189592Sbms			CTR2(KTR_IGMPV3, "%s: enqueue record = %d",
1867189592Sbms			    __func__, retval);
1868189592Sbms			inm->inm_state = IGMP_REPORTING_MEMBER;
1869189592Sbms			/* XXX Clear recorded sources for next time. */
1870189592Sbms			inm_clear_recorded(inm);
1871189592Sbms		}
1872189592Sbms		/* FALLTHROUGH */
1873189592Sbms	case IGMP_REPORTING_MEMBER:
1874189592Sbms	case IGMP_LEAVING_MEMBER:
1875189592Sbms		if (state_change_retransmit_timer_expired) {
1876189592Sbms			/*
1877189592Sbms			 * State-change retransmission timer fired.
1878189592Sbms			 * If there are any further pending retransmissions,
1879189592Sbms			 * set the global pending state-change flag, and
1880189592Sbms			 * reset the timer.
1881189592Sbms			 */
1882189592Sbms			if (--inm->inm_scrv > 0) {
1883189592Sbms				inm->inm_sctimer = uri_fasthz;
1884189592Sbms				V_state_change_timers_running = 1;
1885189592Sbms			}
1886189592Sbms			/*
1887189592Sbms			 * Retransmit the previously computed state-change
1888189592Sbms			 * report. If there are no further pending
1889189592Sbms			 * retransmissions, the mbuf queue will be consumed.
1890189592Sbms			 * Update T0 state to T1 as we have now sent
1891189592Sbms			 * a state-change.
1892189592Sbms			 */
1893189592Sbms			(void)igmp_v3_merge_state_changes(inm, scq);
1894189592Sbms
1895189592Sbms			inm_commit(inm);
1896189592Sbms			CTR3(KTR_IGMPV3, "%s: T1 -> T0 for %s/%s", __func__,
1897189592Sbms			    inet_ntoa(inm->inm_addr), inm->inm_ifp->if_xname);
1898189592Sbms
1899189592Sbms			/*
1900189592Sbms			 * If we are leaving the group for good, make sure
1901189592Sbms			 * we release IGMP's reference to it.
1902189592Sbms			 * This release must be deferred using a SLIST,
1903189592Sbms			 * as we are called from a loop which traverses
1904189592Sbms			 * the in_ifmultiaddr TAILQ.
1905189592Sbms			 */
1906189592Sbms			if (inm->inm_state == IGMP_LEAVING_MEMBER &&
1907189592Sbms			    inm->inm_scrv == 0) {
1908189592Sbms				inm->inm_state = IGMP_NOT_MEMBER;
1909189592Sbms				SLIST_INSERT_HEAD(&igi->igi_relinmhead,
1910189592Sbms				    inm, inm_nrele);
1911189592Sbms			}
1912189592Sbms		}
1913189592Sbms		break;
1914189592Sbms	}
19151541Srgrimes}
19161541Srgrimes
1917189592Sbms
1918189592Sbms/*
1919189592Sbms * Suppress a group's pending response to a group or source/group query.
1920189592Sbms *
1921189592Sbms * Do NOT suppress state changes. This leads to IGMPv3 inconsistency.
1922189592Sbms * Do NOT update ST1/ST0 as this operation merely suppresses
1923189592Sbms * the currently pending group record.
1924189592Sbms * Do NOT suppress the response to a general query. It is possible but
1925189592Sbms * it would require adding another state or flag.
1926189592Sbms */
1927189592Sbmsstatic void
1928189592Sbmsigmp_v3_suppress_group_record(struct in_multi *inm)
19291541Srgrimes{
19301541Srgrimes
1931189592Sbms	IN_MULTI_LOCK_ASSERT();
1932189592Sbms
1933189592Sbms	KASSERT(inm->inm_igi->igi_version == IGMP_VERSION_3,
1934189592Sbms		("%s: not IGMPv3 mode on link", __func__));
1935189592Sbms
1936189592Sbms	if (inm->inm_state != IGMP_G_QUERY_PENDING_MEMBER ||
1937189592Sbms	    inm->inm_state != IGMP_SG_QUERY_PENDING_MEMBER)
1938189592Sbms		return;
1939189592Sbms
1940189592Sbms	if (inm->inm_state == IGMP_SG_QUERY_PENDING_MEMBER)
1941189592Sbms		inm_clear_recorded(inm);
1942189592Sbms
1943189592Sbms	inm->inm_timer = 0;
1944189592Sbms	inm->inm_state = IGMP_REPORTING_MEMBER;
1945189592Sbms}
1946189592Sbms
1947189592Sbms/*
1948189592Sbms * Switch to a different IGMP version on the given interface,
1949189592Sbms * as per Section 7.2.1.
1950189592Sbms */
1951189592Sbmsstatic void
1952189592Sbmsigmp_set_version(struct igmp_ifinfo *igi, const int version)
1953189592Sbms{
1954193231Sbms	int old_version_timer;
1955189592Sbms
1956189592Sbms	IGMP_LOCK_ASSERT();
1957189592Sbms
1958189592Sbms	CTR4(KTR_IGMPV3, "%s: switching to v%d on ifp %p(%s)", __func__,
1959189592Sbms	    version, igi->igi_ifp, igi->igi_ifp->if_xname);
1960189592Sbms
1961189592Sbms	if (version == IGMP_VERSION_1 || version == IGMP_VERSION_2) {
1962189592Sbms		/*
1963189592Sbms		 * Compute the "Older Version Querier Present" timer as per
1964189592Sbms		 * Section 8.12.
1965189592Sbms		 */
1966189592Sbms		old_version_timer = igi->igi_rv * igi->igi_qi + igi->igi_qri;
1967189592Sbms		old_version_timer *= PR_SLOWHZ;
1968189592Sbms
1969189592Sbms		if (version == IGMP_VERSION_1) {
1970189592Sbms			igi->igi_v1_timer = old_version_timer;
1971189592Sbms			igi->igi_v2_timer = 0;
1972189592Sbms		} else if (version == IGMP_VERSION_2) {
1973189592Sbms			igi->igi_v1_timer = 0;
1974189592Sbms			igi->igi_v2_timer = old_version_timer;
1975189592Sbms		}
1976189592Sbms	}
1977189592Sbms
1978189592Sbms	if (igi->igi_v1_timer == 0 && igi->igi_v2_timer > 0) {
1979189592Sbms		if (igi->igi_version != IGMP_VERSION_2) {
1980189592Sbms			igi->igi_version = IGMP_VERSION_2;
1981189592Sbms			igmp_v3_cancel_link_timers(igi);
1982189592Sbms		}
1983189592Sbms	} else if (igi->igi_v1_timer > 0) {
1984189592Sbms		if (igi->igi_version != IGMP_VERSION_1) {
1985189592Sbms			igi->igi_version = IGMP_VERSION_1;
1986189592Sbms			igmp_v3_cancel_link_timers(igi);
1987189592Sbms		}
1988189592Sbms	}
1989189592Sbms}
1990189592Sbms
1991189592Sbms/*
1992189592Sbms * Cancel pending IGMPv3 timers for the given link and all groups
1993189592Sbms * joined on it; state-change, general-query, and group-query timers.
1994193231Sbms *
1995193231Sbms * Only ever called on a transition from v3 to Compatibility mode. Kill
1996193231Sbms * the timers stone dead (this may be expensive for large N groups), they
1997193231Sbms * will be restarted if Compatibility Mode deems that they must be due to
1998193231Sbms * query processing.
1999189592Sbms */
2000189592Sbmsstatic void
2001189592Sbmsigmp_v3_cancel_link_timers(struct igmp_ifinfo *igi)
2002189592Sbms{
2003189592Sbms	struct ifmultiaddr	*ifma;
2004189592Sbms	struct ifnet		*ifp;
2005230076Sjhb	struct in_multi		*inm, *tinm;
2006189592Sbms
2007189592Sbms	CTR3(KTR_IGMPV3, "%s: cancel v3 timers on ifp %p(%s)", __func__,
2008189592Sbms	    igi->igi_ifp, igi->igi_ifp->if_xname);
2009189592Sbms
2010189592Sbms	IN_MULTI_LOCK_ASSERT();
2011189592Sbms	IGMP_LOCK_ASSERT();
2012189592Sbms
20131541Srgrimes	/*
2014193231Sbms	 * Stop the v3 General Query Response on this link stone dead.
2015193231Sbms	 * If fasttimo is woken up due to V_interface_timers_running,
2016193231Sbms	 * the flag will be cleared if there are no pending link timers.
20171541Srgrimes	 */
2018189592Sbms	igi->igi_v3_timer = 0;
2019189592Sbms
2020193231Sbms	/*
2021193231Sbms	 * Now clear the current-state and state-change report timers
2022193231Sbms	 * for all memberships scoped to this link.
2023193231Sbms	 */
2024189592Sbms	ifp = igi->igi_ifp;
2025233200Sjhb	IF_ADDR_RLOCK(ifp);
2026189592Sbms	TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
2027193231Sbms		if (ifma->ifma_addr->sa_family != AF_INET ||
2028193231Sbms		    ifma->ifma_protospec == NULL)
2029189592Sbms			continue;
2030189592Sbms		inm = (struct in_multi *)ifma->ifma_protospec;
2031189592Sbms		switch (inm->inm_state) {
2032189592Sbms		case IGMP_NOT_MEMBER:
2033189592Sbms		case IGMP_SILENT_MEMBER:
2034189592Sbms		case IGMP_IDLE_MEMBER:
2035189592Sbms		case IGMP_LAZY_MEMBER:
2036189592Sbms		case IGMP_SLEEPING_MEMBER:
2037189592Sbms		case IGMP_AWAKENING_MEMBER:
2038193231Sbms			/*
2039193231Sbms			 * These states are either not relevant in v3 mode,
2040193231Sbms			 * or are unreported. Do nothing.
2041193231Sbms			 */
2042189592Sbms			break;
2043189592Sbms		case IGMP_LEAVING_MEMBER:
2044189592Sbms			/*
2045193231Sbms			 * If we are leaving the group and switching to
2046193231Sbms			 * compatibility mode, we need to release the final
2047193231Sbms			 * reference held for issuing the INCLUDE {}, and
2048193231Sbms			 * transition to REPORTING to ensure the host leave
2049193231Sbms			 * message is sent upstream to the old querier --
2050193231Sbms			 * transition to NOT would lose the leave and race.
2051189592Sbms			 */
2052230076Sjhb			SLIST_INSERT_HEAD(&igi->igi_relinmhead, inm, inm_nrele);
2053189592Sbms			/* FALLTHROUGH */
2054189592Sbms		case IGMP_G_QUERY_PENDING_MEMBER:
2055189592Sbms		case IGMP_SG_QUERY_PENDING_MEMBER:
2056189592Sbms			inm_clear_recorded(inm);
2057189592Sbms			/* FALLTHROUGH */
2058189592Sbms		case IGMP_REPORTING_MEMBER:
2059189592Sbms			inm->inm_state = IGMP_REPORTING_MEMBER;
2060189592Sbms			break;
2061189592Sbms		}
2062193231Sbms		/*
2063193231Sbms		 * Always clear state-change and group report timers.
2064193231Sbms		 * Free any pending IGMPv3 state-change records.
2065193231Sbms		 */
2066193231Sbms		inm->inm_sctimer = 0;
2067193231Sbms		inm->inm_timer = 0;
2068193231Sbms		_IF_DRAIN(&inm->inm_scq);
2069189592Sbms	}
2070233200Sjhb	IF_ADDR_RUNLOCK(ifp);
2071230076Sjhb	SLIST_FOREACH_SAFE(inm, &igi->igi_relinmhead, inm_nrele, tinm) {
2072230076Sjhb		SLIST_REMOVE_HEAD(&igi->igi_relinmhead, inm_nrele);
2073230076Sjhb		inm_release_locked(inm);
2074230076Sjhb	}
2075189592Sbms}
2076189592Sbms
2077189592Sbms/*
2078189592Sbms * Update the Older Version Querier Present timers for a link.
2079189592Sbms * See Section 7.2.1 of RFC 3376.
2080189592Sbms */
2081189592Sbmsstatic void
2082189592Sbmsigmp_v1v2_process_querier_timers(struct igmp_ifinfo *igi)
2083189592Sbms{
2084189592Sbms
2085189592Sbms	IGMP_LOCK_ASSERT();
2086189592Sbms
2087189592Sbms	if (igi->igi_v1_timer == 0 && igi->igi_v2_timer == 0) {
2088189592Sbms		/*
2089189592Sbms		 * IGMPv1 and IGMPv2 Querier Present timers expired.
2090189592Sbms		 *
2091189592Sbms		 * Revert to IGMPv3.
2092189592Sbms		 */
2093189592Sbms		if (igi->igi_version != IGMP_VERSION_3) {
2094189592Sbms			CTR5(KTR_IGMPV3,
2095189592Sbms			    "%s: transition from v%d -> v%d on %p(%s)",
2096189592Sbms			    __func__, igi->igi_version, IGMP_VERSION_3,
2097189592Sbms			    igi->igi_ifp, igi->igi_ifp->if_xname);
2098189592Sbms			igi->igi_version = IGMP_VERSION_3;
2099189592Sbms		}
2100189592Sbms	} else if (igi->igi_v1_timer == 0 && igi->igi_v2_timer > 0) {
2101189592Sbms		/*
2102189592Sbms		 * IGMPv1 Querier Present timer expired,
2103189592Sbms		 * IGMPv2 Querier Present timer running.
2104189592Sbms		 * If IGMPv2 was disabled since last timeout,
2105189592Sbms		 * revert to IGMPv3.
2106189592Sbms		 * If IGMPv2 is enabled, revert to IGMPv2.
2107189592Sbms		 */
2108189592Sbms		if (!V_igmp_v2enable) {
2109189592Sbms			CTR5(KTR_IGMPV3,
2110189592Sbms			    "%s: transition from v%d -> v%d on %p(%s)",
2111189592Sbms			    __func__, igi->igi_version, IGMP_VERSION_3,
2112189592Sbms			    igi->igi_ifp, igi->igi_ifp->if_xname);
2113189592Sbms			igi->igi_v2_timer = 0;
2114189592Sbms			igi->igi_version = IGMP_VERSION_3;
2115189592Sbms		} else {
2116189592Sbms			--igi->igi_v2_timer;
2117189592Sbms			if (igi->igi_version != IGMP_VERSION_2) {
2118189592Sbms				CTR5(KTR_IGMPV3,
2119189592Sbms				    "%s: transition from v%d -> v%d on %p(%s)",
2120189592Sbms				    __func__, igi->igi_version, IGMP_VERSION_2,
2121189592Sbms				    igi->igi_ifp, igi->igi_ifp->if_xname);
2122189592Sbms				igi->igi_version = IGMP_VERSION_2;
2123183550Szec			}
21241541Srgrimes		}
2125189592Sbms	} else if (igi->igi_v1_timer > 0) {
2126189592Sbms		/*
2127189592Sbms		 * IGMPv1 Querier Present timer running.
2128189592Sbms		 * Stop IGMPv2 timer if running.
2129189592Sbms		 *
2130189592Sbms		 * If IGMPv1 was disabled since last timeout,
2131189592Sbms		 * revert to IGMPv3.
2132189592Sbms		 * If IGMPv1 is enabled, reset IGMPv2 timer if running.
2133189592Sbms		 */
2134189592Sbms		if (!V_igmp_v1enable) {
2135189592Sbms			CTR5(KTR_IGMPV3,
2136189592Sbms			    "%s: transition from v%d -> v%d on %p(%s)",
2137189592Sbms			    __func__, igi->igi_version, IGMP_VERSION_3,
2138189592Sbms			    igi->igi_ifp, igi->igi_ifp->if_xname);
2139189592Sbms			igi->igi_v1_timer = 0;
2140189592Sbms			igi->igi_version = IGMP_VERSION_3;
2141189592Sbms		} else {
2142189592Sbms			--igi->igi_v1_timer;
2143189592Sbms		}
2144189592Sbms		if (igi->igi_v2_timer > 0) {
2145189592Sbms			CTR3(KTR_IGMPV3,
2146189592Sbms			    "%s: cancel v2 timer on %p(%s)",
2147189592Sbms			    __func__, igi->igi_ifp, igi->igi_ifp->if_xname);
2148189592Sbms			igi->igi_v2_timer = 0;
2149189592Sbms		}
21501541Srgrimes	}
21511541Srgrimes}
21521541Srgrimes
2153189592Sbms/*
2154189592Sbms * Global slowtimo handler.
2155189592Sbms * VIMAGE: Timeout handlers are expected to service all vimages.
2156189592Sbms */
21572531Swollmanvoid
2158119181Srwatsonigmp_slowtimo(void)
21592531Swollman{
2160183550Szec	VNET_ITERATOR_DECL(vnet_iter);
21612531Swollman
2162195760Srwatson	VNET_LIST_RLOCK_NOSLEEP();
2163183550Szec	VNET_FOREACH(vnet_iter) {
2164183550Szec		CURVNET_SET(vnet_iter);
2165189592Sbms		igmp_slowtimo_vnet();
2166183550Szec		CURVNET_RESTORE();
21672531Swollman	}
2168195760Srwatson	VNET_LIST_RUNLOCK_NOSLEEP();
21692531Swollman}
21702531Swollman
2171189592Sbms/*
2172189592Sbms * Per-vnet slowtimo handler.
2173189592Sbms */
21741541Srgrimesstatic void
2175189592Sbmsigmp_slowtimo_vnet(void)
21761541Srgrimes{
2177189592Sbms	struct igmp_ifinfo *igi;
21781541Srgrimes
2179189592Sbms	IGMP_LOCK();
2180189592Sbms
2181189592Sbms	LIST_FOREACH(igi, &V_igi_head, igi_link) {
2182189592Sbms		igmp_v1v2_process_querier_timers(igi);
2183189592Sbms	}
2184189592Sbms
2185189592Sbms	IGMP_UNLOCK();
2186189592Sbms}
2187189592Sbms
2188189592Sbms/*
2189189592Sbms * Dispatch an IGMPv1/v2 host report or leave message.
2190189592Sbms * These are always small enough to fit inside a single mbuf.
2191189592Sbms */
2192189592Sbmsstatic int
2193189592Sbmsigmp_v1v2_queue_report(struct in_multi *inm, const int type)
2194189592Sbms{
2195189592Sbms	struct ifnet		*ifp;
2196189592Sbms	struct igmp		*igmp;
2197189592Sbms	struct ip		*ip;
2198189592Sbms	struct mbuf		*m;
2199189592Sbms
2200148682Srwatson	IN_MULTI_LOCK_ASSERT();
2201189592Sbms	IGMP_LOCK_ASSERT();
2202148682Srwatson
2203189592Sbms	ifp = inm->inm_ifp;
2204189592Sbms
2205151967Sandre	MGETHDR(m, M_DONTWAIT, MT_DATA);
2206119181Srwatson	if (m == NULL)
2207189592Sbms		return (ENOMEM);
2208189592Sbms	MH_ALIGN(m, sizeof(struct ip) + sizeof(struct igmp));
22092531Swollman
2210189592Sbms	m->m_pkthdr.len = sizeof(struct ip) + sizeof(struct igmp);
2211189592Sbms
22122531Swollman	m->m_data += sizeof(struct ip);
2213189592Sbms	m->m_len = sizeof(struct igmp);
2214189592Sbms
2215119181Srwatson	igmp = mtod(m, struct igmp *);
2216119181Srwatson	igmp->igmp_type = type;
2217119181Srwatson	igmp->igmp_code = 0;
2218119181Srwatson	igmp->igmp_group = inm->inm_addr;
2219119181Srwatson	igmp->igmp_cksum = 0;
2220189592Sbms	igmp->igmp_cksum = in_cksum(m, sizeof(struct igmp));
22211541Srgrimes
2222119181Srwatson	m->m_data -= sizeof(struct ip);
2223119181Srwatson	m->m_len += sizeof(struct ip);
2224189592Sbms
2225119181Srwatson	ip = mtod(m, struct ip *);
2226119181Srwatson	ip->ip_tos = 0;
2227189592Sbms	ip->ip_len = sizeof(struct ip) + sizeof(struct igmp);
2228119181Srwatson	ip->ip_off = 0;
2229119181Srwatson	ip->ip_p = IPPROTO_IGMP;
2230119181Srwatson	ip->ip_src.s_addr = INADDR_ANY;
22311541Srgrimes
2232189592Sbms	if (type == IGMP_HOST_LEAVE_MESSAGE)
2233189592Sbms		ip->ip_dst.s_addr = htonl(INADDR_ALLRTRS_GROUP);
2234189592Sbms	else
2235189592Sbms		ip->ip_dst = inm->inm_addr;
2236189592Sbms
2237189592Sbms	igmp_save_context(m, ifp);
2238189592Sbms
2239189592Sbms	m->m_flags |= M_IGMPV2;
2240189592Sbms	if (inm->inm_igi->igi_flags & IGIF_LOOPBACK)
2241189592Sbms		m->m_flags |= M_IGMP_LOOP;
2242189592Sbms
2243189592Sbms	CTR2(KTR_IGMPV3, "%s: netisr_dispatch(NETISR_IGMP, %p)", __func__, m);
2244189592Sbms	netisr_dispatch(NETISR_IGMP, m);
2245189592Sbms
2246189592Sbms	return (0);
2247189592Sbms}
2248189592Sbms
2249189592Sbms/*
2250189592Sbms * Process a state change from the upper layer for the given IPv4 group.
2251189592Sbms *
2252189592Sbms * Each socket holds a reference on the in_multi in its own ip_moptions.
2253189592Sbms * The socket layer will have made the necessary updates to.the group
2254189592Sbms * state, it is now up to IGMP to issue a state change report if there
2255189592Sbms * has been any change between T0 (when the last state-change was issued)
2256189592Sbms * and T1 (now).
2257189592Sbms *
2258189592Sbms * We use the IGMPv3 state machine at group level. The IGMP module
2259189592Sbms * however makes the decision as to which IGMP protocol version to speak.
2260189592Sbms * A state change *from* INCLUDE {} always means an initial join.
2261189592Sbms * A state change *to* INCLUDE {} always means a final leave.
2262189592Sbms *
2263189592Sbms * FUTURE: If IGIF_V3LITE is enabled for this interface, then we can
2264189592Sbms * save ourselves a bunch of work; any exclusive mode groups need not
2265189592Sbms * compute source filter lists.
2266189592Sbms *
2267189592Sbms * VIMAGE: curvnet should have been set by caller, as this routine
2268189592Sbms * is called from the socket option handlers.
2269189592Sbms */
2270189592Sbmsint
2271189592Sbmsigmp_change_state(struct in_multi *inm)
2272189592Sbms{
2273189592Sbms	struct igmp_ifinfo *igi;
2274189592Sbms	struct ifnet *ifp;
2275189592Sbms	int error;
2276189592Sbms
2277189592Sbms	IN_MULTI_LOCK_ASSERT();
2278189592Sbms
2279189592Sbms	error = 0;
2280189592Sbms
2281189592Sbms	/*
2282189592Sbms	 * Try to detect if the upper layer just asked us to change state
2283189592Sbms	 * for an interface which has now gone away.
2284189592Sbms	 */
2285189592Sbms	KASSERT(inm->inm_ifma != NULL, ("%s: no ifma", __func__));
2286189592Sbms	ifp = inm->inm_ifma->ifma_ifp;
2287239976Strociny	/*
2288239976Strociny	 * Sanity check that netinet's notion of ifp is the
2289239976Strociny	 * same as net's.
2290239976Strociny	 */
2291239976Strociny	KASSERT(inm->inm_ifp == ifp, ("%s: bad ifp", __func__));
2292189592Sbms
2293189592Sbms	IGMP_LOCK();
2294189592Sbms
2295189592Sbms	igi = ((struct in_ifinfo *)ifp->if_afdata[AF_INET])->ii_igmp;
2296189592Sbms	KASSERT(igi != NULL, ("%s: no igmp_ifinfo for ifp %p", __func__, ifp));
2297189592Sbms
2298189592Sbms	/*
2299189592Sbms	 * If we detect a state transition to or from MCAST_UNDEFINED
2300189592Sbms	 * for this group, then we are starting or finishing an IGMP
2301189592Sbms	 * life cycle for this group.
2302189592Sbms	 */
2303189592Sbms	if (inm->inm_st[1].iss_fmode != inm->inm_st[0].iss_fmode) {
2304189592Sbms		CTR3(KTR_IGMPV3, "%s: inm transition %d -> %d", __func__,
2305189592Sbms		    inm->inm_st[0].iss_fmode, inm->inm_st[1].iss_fmode);
2306189592Sbms		if (inm->inm_st[0].iss_fmode == MCAST_UNDEFINED) {
2307189592Sbms			CTR1(KTR_IGMPV3, "%s: initial join", __func__);
2308189592Sbms			error = igmp_initial_join(inm, igi);
2309189592Sbms			goto out_locked;
2310189592Sbms		} else if (inm->inm_st[1].iss_fmode == MCAST_UNDEFINED) {
2311189592Sbms			CTR1(KTR_IGMPV3, "%s: final leave", __func__);
2312189592Sbms			igmp_final_leave(inm, igi);
2313189592Sbms			goto out_locked;
2314189592Sbms		}
2315189592Sbms	} else {
2316189592Sbms		CTR1(KTR_IGMPV3, "%s: filter set change", __func__);
2317189592Sbms	}
2318189592Sbms
2319189592Sbms	error = igmp_handle_state_change(inm, igi);
2320189592Sbms
2321189592Sbmsout_locked:
2322189592Sbms	IGMP_UNLOCK();
2323189592Sbms	return (error);
2324189592Sbms}
2325189592Sbms
2326189592Sbms/*
2327189592Sbms * Perform the initial join for an IGMP group.
2328189592Sbms *
2329189592Sbms * When joining a group:
2330189592Sbms *  If the group should have its IGMP traffic suppressed, do nothing.
2331189592Sbms *  IGMPv1 starts sending IGMPv1 host membership reports.
2332189592Sbms *  IGMPv2 starts sending IGMPv2 host membership reports.
2333189592Sbms *  IGMPv3 will schedule an IGMPv3 state-change report containing the
2334189592Sbms *  initial state of the membership.
2335189592Sbms */
2336189592Sbmsstatic int
2337189592Sbmsigmp_initial_join(struct in_multi *inm, struct igmp_ifinfo *igi)
2338189592Sbms{
2339189592Sbms	struct ifnet		*ifp;
2340189592Sbms	struct ifqueue		*ifq;
2341189592Sbms	int			 error, retval, syncstates;
2342189592Sbms
2343189592Sbms	CTR4(KTR_IGMPV3, "%s: initial join %s on ifp %p(%s)",
2344189592Sbms	    __func__, inet_ntoa(inm->inm_addr), inm->inm_ifp,
2345189592Sbms	    inm->inm_ifp->if_xname);
2346189592Sbms
2347189592Sbms	error = 0;
2348189592Sbms	syncstates = 1;
2349189592Sbms
2350189592Sbms	ifp = inm->inm_ifp;
2351189592Sbms
2352189592Sbms	IN_MULTI_LOCK_ASSERT();
2353189592Sbms	IGMP_LOCK_ASSERT();
2354189592Sbms
2355189592Sbms	KASSERT(igi && igi->igi_ifp == ifp, ("%s: inconsistent ifp", __func__));
2356189592Sbms
2357189592Sbms	/*
2358189592Sbms	 * Groups joined on loopback or marked as 'not reported',
2359189592Sbms	 * e.g. 224.0.0.1, enter the IGMP_SILENT_MEMBER state and
2360189592Sbms	 * are never reported in any IGMP protocol exchanges.
2361189592Sbms	 * All other groups enter the appropriate IGMP state machine
2362189592Sbms	 * for the version in use on this link.
2363189592Sbms	 * A link marked as IGIF_SILENT causes IGMP to be completely
2364189592Sbms	 * disabled for the link.
2365189592Sbms	 */
2366189592Sbms	if ((ifp->if_flags & IFF_LOOPBACK) ||
2367189592Sbms	    (igi->igi_flags & IGIF_SILENT) ||
2368189592Sbms	    !igmp_isgroupreported(inm->inm_addr)) {
2369189592Sbms		CTR1(KTR_IGMPV3,
2370189592Sbms"%s: not kicking state machine for silent group", __func__);
2371189592Sbms		inm->inm_state = IGMP_SILENT_MEMBER;
2372189592Sbms		inm->inm_timer = 0;
2373189592Sbms	} else {
2374189592Sbms		/*
2375189592Sbms		 * Deal with overlapping in_multi lifecycle.
2376189592Sbms		 * If this group was LEAVING, then make sure
2377189592Sbms		 * we drop the reference we picked up to keep the
2378189592Sbms		 * group around for the final INCLUDE {} enqueue.
2379189592Sbms		 */
2380189592Sbms		if (igi->igi_version == IGMP_VERSION_3 &&
2381189592Sbms		    inm->inm_state == IGMP_LEAVING_MEMBER)
2382189592Sbms			inm_release_locked(inm);
2383189592Sbms
2384189592Sbms		inm->inm_state = IGMP_REPORTING_MEMBER;
2385189592Sbms
2386189592Sbms		switch (igi->igi_version) {
2387189592Sbms		case IGMP_VERSION_1:
2388189592Sbms		case IGMP_VERSION_2:
2389189592Sbms			inm->inm_state = IGMP_IDLE_MEMBER;
2390189592Sbms			error = igmp_v1v2_queue_report(inm,
2391189592Sbms			    (igi->igi_version == IGMP_VERSION_2) ?
2392189592Sbms			     IGMP_v2_HOST_MEMBERSHIP_REPORT :
2393189592Sbms			     IGMP_v1_HOST_MEMBERSHIP_REPORT);
2394189592Sbms			if (error == 0) {
2395189592Sbms				inm->inm_timer = IGMP_RANDOM_DELAY(
2396189592Sbms				    IGMP_V1V2_MAX_RI * PR_FASTHZ);
2397189592Sbms				V_current_state_timers_running = 1;
2398189592Sbms			}
2399189592Sbms			break;
2400189592Sbms
2401189592Sbms		case IGMP_VERSION_3:
2402189592Sbms			/*
2403189592Sbms			 * Defer update of T0 to T1, until the first copy
2404189592Sbms			 * of the state change has been transmitted.
2405189592Sbms			 */
2406189592Sbms			syncstates = 0;
2407189592Sbms
2408189592Sbms			/*
2409189592Sbms			 * Immediately enqueue a State-Change Report for
2410189592Sbms			 * this interface, freeing any previous reports.
2411189592Sbms			 * Don't kick the timers if there is nothing to do,
2412189592Sbms			 * or if an error occurred.
2413189592Sbms			 */
2414189592Sbms			ifq = &inm->inm_scq;
2415189592Sbms			_IF_DRAIN(ifq);
2416189592Sbms			retval = igmp_v3_enqueue_group_record(ifq, inm, 1,
2417189592Sbms			    0, 0);
2418189592Sbms			CTR2(KTR_IGMPV3, "%s: enqueue record = %d",
2419189592Sbms			    __func__, retval);
2420189592Sbms			if (retval <= 0) {
2421189592Sbms				error = retval * -1;
2422189592Sbms				break;
2423189592Sbms			}
2424189592Sbms
2425189592Sbms			/*
2426189592Sbms			 * Schedule transmission of pending state-change
2427189592Sbms			 * report up to RV times for this link. The timer
2428189592Sbms			 * will fire at the next igmp_fasttimo (~200ms),
2429189592Sbms			 * giving us an opportunity to merge the reports.
2430189592Sbms			 */
2431189592Sbms			if (igi->igi_flags & IGIF_LOOPBACK) {
2432189592Sbms				inm->inm_scrv = 1;
2433189592Sbms			} else {
2434189592Sbms				KASSERT(igi->igi_rv > 1,
2435189592Sbms				   ("%s: invalid robustness %d", __func__,
2436189592Sbms				    igi->igi_rv));
2437189592Sbms				inm->inm_scrv = igi->igi_rv;
2438189592Sbms			}
2439189592Sbms			inm->inm_sctimer = 1;
2440189592Sbms			V_state_change_timers_running = 1;
2441189592Sbms
2442189592Sbms			error = 0;
2443189592Sbms			break;
2444189592Sbms		}
2445189592Sbms	}
2446189592Sbms
2447189592Sbms	/*
2448189592Sbms	 * Only update the T0 state if state change is atomic,
2449189592Sbms	 * i.e. we don't need to wait for a timer to fire before we
2450189592Sbms	 * can consider the state change to have been communicated.
2451189592Sbms	 */
2452189592Sbms	if (syncstates) {
2453189592Sbms		inm_commit(inm);
2454189592Sbms		CTR3(KTR_IGMPV3, "%s: T1 -> T0 for %s/%s", __func__,
2455189592Sbms		    inet_ntoa(inm->inm_addr), inm->inm_ifp->if_xname);
2456189592Sbms	}
2457189592Sbms
2458189592Sbms	return (error);
2459189592Sbms}
2460189592Sbms
2461189592Sbms/*
2462189592Sbms * Issue an intermediate state change during the IGMP life-cycle.
2463189592Sbms */
2464189592Sbmsstatic int
2465189592Sbmsigmp_handle_state_change(struct in_multi *inm, struct igmp_ifinfo *igi)
2466189592Sbms{
2467189592Sbms	struct ifnet		*ifp;
2468189592Sbms	int			 retval;
2469189592Sbms
2470189592Sbms	CTR4(KTR_IGMPV3, "%s: state change for %s on ifp %p(%s)",
2471189592Sbms	    __func__, inet_ntoa(inm->inm_addr), inm->inm_ifp,
2472189592Sbms	    inm->inm_ifp->if_xname);
2473189592Sbms
2474189592Sbms	ifp = inm->inm_ifp;
2475189592Sbms
2476189592Sbms	IN_MULTI_LOCK_ASSERT();
2477189592Sbms	IGMP_LOCK_ASSERT();
2478189592Sbms
2479189592Sbms	KASSERT(igi && igi->igi_ifp == ifp, ("%s: inconsistent ifp", __func__));
2480189592Sbms
2481189592Sbms	if ((ifp->if_flags & IFF_LOOPBACK) ||
2482189592Sbms	    (igi->igi_flags & IGIF_SILENT) ||
2483189592Sbms	    !igmp_isgroupreported(inm->inm_addr) ||
2484189592Sbms	    (igi->igi_version != IGMP_VERSION_3)) {
2485189592Sbms		if (!igmp_isgroupreported(inm->inm_addr)) {
2486189592Sbms			CTR1(KTR_IGMPV3,
2487189592Sbms"%s: not kicking state machine for silent group", __func__);
2488189592Sbms		}
2489189592Sbms		CTR1(KTR_IGMPV3, "%s: nothing to do", __func__);
2490189592Sbms		inm_commit(inm);
2491189592Sbms		CTR3(KTR_IGMPV3, "%s: T1 -> T0 for %s/%s", __func__,
2492189592Sbms		    inet_ntoa(inm->inm_addr), inm->inm_ifp->if_xname);
2493189592Sbms		return (0);
2494189592Sbms	}
2495189592Sbms
2496189592Sbms	_IF_DRAIN(&inm->inm_scq);
2497189592Sbms
2498189592Sbms	retval = igmp_v3_enqueue_group_record(&inm->inm_scq, inm, 1, 0, 0);
2499189592Sbms	CTR2(KTR_IGMPV3, "%s: enqueue record = %d", __func__, retval);
2500189592Sbms	if (retval <= 0)
2501189592Sbms		return (-retval);
2502189592Sbms
2503189592Sbms	/*
2504189592Sbms	 * If record(s) were enqueued, start the state-change
2505189592Sbms	 * report timer for this group.
2506189592Sbms	 */
2507189592Sbms	inm->inm_scrv = ((igi->igi_flags & IGIF_LOOPBACK) ? 1 : igi->igi_rv);
2508189592Sbms	inm->inm_sctimer = 1;
2509189592Sbms	V_state_change_timers_running = 1;
2510189592Sbms
2511189592Sbms	return (0);
2512189592Sbms}
2513189592Sbms
2514189592Sbms/*
2515189592Sbms * Perform the final leave for an IGMP group.
2516189592Sbms *
2517189592Sbms * When leaving a group:
2518189592Sbms *  IGMPv1 does nothing.
2519189592Sbms *  IGMPv2 sends a host leave message, if and only if we are the reporter.
2520189592Sbms *  IGMPv3 enqueues a state-change report containing a transition
2521189592Sbms *  to INCLUDE {} for immediate transmission.
2522189592Sbms */
2523189592Sbmsstatic void
2524189592Sbmsigmp_final_leave(struct in_multi *inm, struct igmp_ifinfo *igi)
2525189592Sbms{
2526189592Sbms	int syncstates;
2527189592Sbms
2528189592Sbms	syncstates = 1;
2529189592Sbms
2530189592Sbms	CTR4(KTR_IGMPV3, "%s: final leave %s on ifp %p(%s)",
2531189592Sbms	    __func__, inet_ntoa(inm->inm_addr), inm->inm_ifp,
2532189592Sbms	    inm->inm_ifp->if_xname);
2533189592Sbms
2534189592Sbms	IN_MULTI_LOCK_ASSERT();
2535189592Sbms	IGMP_LOCK_ASSERT();
2536189592Sbms
2537189592Sbms	switch (inm->inm_state) {
2538189592Sbms	case IGMP_NOT_MEMBER:
2539189592Sbms	case IGMP_SILENT_MEMBER:
2540189592Sbms	case IGMP_LEAVING_MEMBER:
2541189592Sbms		/* Already leaving or left; do nothing. */
2542189592Sbms		CTR1(KTR_IGMPV3,
2543189592Sbms"%s: not kicking state machine for silent group", __func__);
2544189592Sbms		break;
2545189592Sbms	case IGMP_REPORTING_MEMBER:
2546189592Sbms	case IGMP_IDLE_MEMBER:
2547189592Sbms	case IGMP_G_QUERY_PENDING_MEMBER:
2548189592Sbms	case IGMP_SG_QUERY_PENDING_MEMBER:
2549189592Sbms		if (igi->igi_version == IGMP_VERSION_2) {
2550189592Sbms#ifdef INVARIANTS
2551189592Sbms			if (inm->inm_state == IGMP_G_QUERY_PENDING_MEMBER ||
2552189592Sbms			    inm->inm_state == IGMP_SG_QUERY_PENDING_MEMBER)
2553189592Sbms			panic("%s: IGMPv3 state reached, not IGMPv3 mode",
2554189592Sbms			     __func__);
2555189592Sbms#endif
2556189592Sbms			igmp_v1v2_queue_report(inm, IGMP_HOST_LEAVE_MESSAGE);
2557189592Sbms			inm->inm_state = IGMP_NOT_MEMBER;
2558189592Sbms		} else if (igi->igi_version == IGMP_VERSION_3) {
2559189592Sbms			/*
2560189592Sbms			 * Stop group timer and all pending reports.
2561189592Sbms			 * Immediately enqueue a state-change report
2562189592Sbms			 * TO_IN {} to be sent on the next fast timeout,
2563189592Sbms			 * giving us an opportunity to merge reports.
2564189592Sbms			 */
2565189592Sbms			_IF_DRAIN(&inm->inm_scq);
2566189592Sbms			inm->inm_timer = 0;
2567189592Sbms			if (igi->igi_flags & IGIF_LOOPBACK) {
2568189592Sbms				inm->inm_scrv = 1;
2569189592Sbms			} else {
2570189592Sbms				inm->inm_scrv = igi->igi_rv;
2571189592Sbms			}
2572189592Sbms			CTR4(KTR_IGMPV3, "%s: Leaving %s/%s with %d "
2573189592Sbms			    "pending retransmissions.", __func__,
2574189592Sbms			    inet_ntoa(inm->inm_addr),
2575189592Sbms			    inm->inm_ifp->if_xname, inm->inm_scrv);
2576189592Sbms			if (inm->inm_scrv == 0) {
2577189592Sbms				inm->inm_state = IGMP_NOT_MEMBER;
2578189592Sbms				inm->inm_sctimer = 0;
2579189592Sbms			} else {
2580189592Sbms				int retval;
2581189592Sbms
2582189592Sbms				inm_acquire_locked(inm);
2583189592Sbms
2584189592Sbms				retval = igmp_v3_enqueue_group_record(
2585189592Sbms				    &inm->inm_scq, inm, 1, 0, 0);
2586189592Sbms				KASSERT(retval != 0,
2587189592Sbms				    ("%s: enqueue record = %d", __func__,
2588189592Sbms				     retval));
2589189592Sbms
2590189592Sbms				inm->inm_state = IGMP_LEAVING_MEMBER;
2591189592Sbms				inm->inm_sctimer = 1;
2592189592Sbms				V_state_change_timers_running = 1;
2593189592Sbms				syncstates = 0;
2594189592Sbms			}
2595189592Sbms			break;
2596189592Sbms		}
2597189592Sbms		break;
2598189592Sbms	case IGMP_LAZY_MEMBER:
2599189592Sbms	case IGMP_SLEEPING_MEMBER:
2600189592Sbms	case IGMP_AWAKENING_MEMBER:
2601189592Sbms		/* Our reports are suppressed; do nothing. */
2602189592Sbms		break;
2603189592Sbms	}
2604189592Sbms
2605189592Sbms	if (syncstates) {
2606189592Sbms		inm_commit(inm);
2607189592Sbms		CTR3(KTR_IGMPV3, "%s: T1 -> T0 for %s/%s", __func__,
2608189592Sbms		    inet_ntoa(inm->inm_addr), inm->inm_ifp->if_xname);
2609189592Sbms		inm->inm_st[1].iss_fmode = MCAST_UNDEFINED;
2610189592Sbms		CTR3(KTR_IGMPV3, "%s: T1 now MCAST_UNDEFINED for %s/%s",
2611189592Sbms		    __func__, inet_ntoa(inm->inm_addr), inm->inm_ifp->if_xname);
2612189592Sbms	}
2613189592Sbms}
2614189592Sbms
2615189592Sbms/*
2616189592Sbms * Enqueue an IGMPv3 group record to the given output queue.
2617189592Sbms *
2618189592Sbms * XXX This function could do with having the allocation code
2619189592Sbms * split out, and the multiple-tree-walks coalesced into a single
2620189592Sbms * routine as has been done in igmp_v3_enqueue_filter_change().
2621189592Sbms *
2622189592Sbms * If is_state_change is zero, a current-state record is appended.
2623189592Sbms * If is_state_change is non-zero, a state-change report is appended.
2624189592Sbms *
2625189592Sbms * If is_group_query is non-zero, an mbuf packet chain is allocated.
2626189592Sbms * If is_group_query is zero, and if there is a packet with free space
2627189592Sbms * at the tail of the queue, it will be appended to providing there
2628189592Sbms * is enough free space.
2629189592Sbms * Otherwise a new mbuf packet chain is allocated.
2630189592Sbms *
2631189592Sbms * If is_source_query is non-zero, each source is checked to see if
2632189592Sbms * it was recorded for a Group-Source query, and will be omitted if
2633189592Sbms * it is not both in-mode and recorded.
2634189592Sbms *
2635189592Sbms * The function will attempt to allocate leading space in the packet
2636189592Sbms * for the IP/IGMP header to be prepended without fragmenting the chain.
2637189592Sbms *
2638189592Sbms * If successful the size of all data appended to the queue is returned,
2639189592Sbms * otherwise an error code less than zero is returned, or zero if
2640189592Sbms * no record(s) were appended.
2641189592Sbms */
2642189592Sbmsstatic int
2643189592Sbmsigmp_v3_enqueue_group_record(struct ifqueue *ifq, struct in_multi *inm,
2644189592Sbms    const int is_state_change, const int is_group_query,
2645189592Sbms    const int is_source_query)
2646189592Sbms{
2647189592Sbms	struct igmp_grouprec	 ig;
2648189592Sbms	struct igmp_grouprec	*pig;
2649189592Sbms	struct ifnet		*ifp;
2650189592Sbms	struct ip_msource	*ims, *nims;
2651189592Sbms	struct mbuf		*m0, *m, *md;
2652189592Sbms	int			 error, is_filter_list_change;
2653189592Sbms	int			 minrec0len, m0srcs, msrcs, nbytes, off;
2654189592Sbms	int			 record_has_sources;
2655189592Sbms	int			 now;
2656189592Sbms	int			 type;
2657189592Sbms	in_addr_t		 naddr;
2658189592Sbms	uint8_t			 mode;
2659189592Sbms
2660189592Sbms	IN_MULTI_LOCK_ASSERT();
2661189592Sbms
2662189592Sbms	error = 0;
2663189592Sbms	ifp = inm->inm_ifp;
2664189592Sbms	is_filter_list_change = 0;
2665189592Sbms	m = NULL;
2666189592Sbms	m0 = NULL;
2667189592Sbms	m0srcs = 0;
2668189592Sbms	msrcs = 0;
2669189592Sbms	nbytes = 0;
2670189592Sbms	nims = NULL;
2671189592Sbms	record_has_sources = 1;
2672189592Sbms	pig = NULL;
2673189592Sbms	type = IGMP_DO_NOTHING;
2674189592Sbms	mode = inm->inm_st[1].iss_fmode;
2675189592Sbms
2676189592Sbms	/*
2677189592Sbms	 * If we did not transition out of ASM mode during t0->t1,
2678189592Sbms	 * and there are no source nodes to process, we can skip
2679189592Sbms	 * the generation of source records.
2680189592Sbms	 */
2681189592Sbms	if (inm->inm_st[0].iss_asm > 0 && inm->inm_st[1].iss_asm > 0 &&
2682189592Sbms	    inm->inm_nsrc == 0)
2683189592Sbms		record_has_sources = 0;
2684189592Sbms
2685189592Sbms	if (is_state_change) {
2686189592Sbms		/*
2687189592Sbms		 * Queue a state change record.
2688189592Sbms		 * If the mode did not change, and there are non-ASM
2689189592Sbms		 * listeners or source filters present,
2690189592Sbms		 * we potentially need to issue two records for the group.
2691189592Sbms		 * If we are transitioning to MCAST_UNDEFINED, we need
2692189592Sbms		 * not send any sources.
2693189592Sbms		 * If there are ASM listeners, and there was no filter
2694189592Sbms		 * mode transition of any kind, do nothing.
2695189592Sbms		 */
2696189592Sbms		if (mode != inm->inm_st[0].iss_fmode) {
2697189592Sbms			if (mode == MCAST_EXCLUDE) {
2698189592Sbms				CTR1(KTR_IGMPV3, "%s: change to EXCLUDE",
2699189592Sbms				    __func__);
2700189592Sbms				type = IGMP_CHANGE_TO_EXCLUDE_MODE;
2701189592Sbms			} else {
2702189592Sbms				CTR1(KTR_IGMPV3, "%s: change to INCLUDE",
2703189592Sbms				    __func__);
2704189592Sbms				type = IGMP_CHANGE_TO_INCLUDE_MODE;
2705189592Sbms				if (mode == MCAST_UNDEFINED)
2706189592Sbms					record_has_sources = 0;
2707189592Sbms			}
2708189592Sbms		} else {
2709189592Sbms			if (record_has_sources) {
2710189592Sbms				is_filter_list_change = 1;
2711189592Sbms			} else {
2712189592Sbms				type = IGMP_DO_NOTHING;
2713189592Sbms			}
2714189592Sbms		}
2715189592Sbms	} else {
2716189592Sbms		/*
2717189592Sbms		 * Queue a current state record.
2718189592Sbms		 */
2719189592Sbms		if (mode == MCAST_EXCLUDE) {
2720189592Sbms			type = IGMP_MODE_IS_EXCLUDE;
2721189592Sbms		} else if (mode == MCAST_INCLUDE) {
2722189592Sbms			type = IGMP_MODE_IS_INCLUDE;
2723189592Sbms			KASSERT(inm->inm_st[1].iss_asm == 0,
2724189592Sbms			    ("%s: inm %p is INCLUDE but ASM count is %d",
2725189592Sbms			     __func__, inm, inm->inm_st[1].iss_asm));
2726189592Sbms		}
2727189592Sbms	}
2728189592Sbms
2729189592Sbms	/*
2730189592Sbms	 * Generate the filter list changes using a separate function.
2731189592Sbms	 */
2732189592Sbms	if (is_filter_list_change)
2733189592Sbms		return (igmp_v3_enqueue_filter_change(ifq, inm));
2734189592Sbms
2735189592Sbms	if (type == IGMP_DO_NOTHING) {
2736189592Sbms		CTR3(KTR_IGMPV3, "%s: nothing to do for %s/%s",
2737189592Sbms		    __func__, inet_ntoa(inm->inm_addr),
2738189592Sbms		    inm->inm_ifp->if_xname);
2739189592Sbms		return (0);
2740189592Sbms	}
2741189592Sbms
2742189592Sbms	/*
2743189592Sbms	 * If any sources are present, we must be able to fit at least
2744189592Sbms	 * one in the trailing space of the tail packet's mbuf,
2745189592Sbms	 * ideally more.
2746189592Sbms	 */
2747189592Sbms	minrec0len = sizeof(struct igmp_grouprec);
2748189592Sbms	if (record_has_sources)
2749189592Sbms		minrec0len += sizeof(in_addr_t);
2750189592Sbms
2751189592Sbms	CTR4(KTR_IGMPV3, "%s: queueing %s for %s/%s", __func__,
2752189592Sbms	    igmp_rec_type_to_str(type), inet_ntoa(inm->inm_addr),
2753189592Sbms	    inm->inm_ifp->if_xname);
2754189592Sbms
2755189592Sbms	/*
2756189592Sbms	 * Check if we have a packet in the tail of the queue for this
2757189592Sbms	 * group into which the first group record for this group will fit.
2758189592Sbms	 * Otherwise allocate a new packet.
2759189592Sbms	 * Always allocate leading space for IP+RA_OPT+IGMP+REPORT.
2760189592Sbms	 * Note: Group records for G/GSR query responses MUST be sent
2761189592Sbms	 * in their own packet.
2762189592Sbms	 */
2763189592Sbms	m0 = ifq->ifq_tail;
2764189592Sbms	if (!is_group_query &&
2765189592Sbms	    m0 != NULL &&
2766189592Sbms	    (m0->m_pkthdr.PH_vt.vt_nrecs + 1 <= IGMP_V3_REPORT_MAXRECS) &&
2767189592Sbms	    (m0->m_pkthdr.len + minrec0len) <
2768189592Sbms	     (ifp->if_mtu - IGMP_LEADINGSPACE)) {
2769189592Sbms		m0srcs = (ifp->if_mtu - m0->m_pkthdr.len -
2770189592Sbms			    sizeof(struct igmp_grouprec)) / sizeof(in_addr_t);
2771189592Sbms		m = m0;
2772189592Sbms		CTR1(KTR_IGMPV3, "%s: use existing packet", __func__);
2773189592Sbms	} else {
2774189592Sbms		if (_IF_QFULL(ifq)) {
2775189592Sbms			CTR1(KTR_IGMPV3, "%s: outbound queue full", __func__);
2776189592Sbms			return (-ENOMEM);
2777189592Sbms		}
2778189592Sbms		m = NULL;
2779189592Sbms		m0srcs = (ifp->if_mtu - IGMP_LEADINGSPACE -
2780189592Sbms		    sizeof(struct igmp_grouprec)) / sizeof(in_addr_t);
2781190692Sbms		if (!is_state_change && !is_group_query) {
2782189592Sbms			m = m_getcl(M_DONTWAIT, MT_DATA, M_PKTHDR);
2783190692Sbms			if (m)
2784190692Sbms				m->m_data += IGMP_LEADINGSPACE;
2785190692Sbms		}
2786189592Sbms		if (m == NULL) {
2787189592Sbms			m = m_gethdr(M_DONTWAIT, MT_DATA);
2788189592Sbms			if (m)
2789189592Sbms				MH_ALIGN(m, IGMP_LEADINGSPACE);
2790189592Sbms		}
2791189592Sbms		if (m == NULL)
2792189592Sbms			return (-ENOMEM);
2793189592Sbms
2794189592Sbms		igmp_save_context(m, ifp);
2795189592Sbms
2796189592Sbms		CTR1(KTR_IGMPV3, "%s: allocated first packet", __func__);
2797189592Sbms	}
2798189592Sbms
2799189592Sbms	/*
2800189592Sbms	 * Append group record.
2801189592Sbms	 * If we have sources, we don't know how many yet.
2802189592Sbms	 */
2803189592Sbms	ig.ig_type = type;
2804189592Sbms	ig.ig_datalen = 0;
2805189592Sbms	ig.ig_numsrc = 0;
2806189592Sbms	ig.ig_group = inm->inm_addr;
2807189592Sbms	if (!m_append(m, sizeof(struct igmp_grouprec), (void *)&ig)) {
2808189592Sbms		if (m != m0)
2809189592Sbms			m_freem(m);
2810189592Sbms		CTR1(KTR_IGMPV3, "%s: m_append() failed.", __func__);
2811189592Sbms		return (-ENOMEM);
2812189592Sbms	}
2813189592Sbms	nbytes += sizeof(struct igmp_grouprec);
2814189592Sbms
2815189592Sbms	/*
2816189592Sbms	 * Append as many sources as will fit in the first packet.
2817189592Sbms	 * If we are appending to a new packet, the chain allocation
2818189592Sbms	 * may potentially use clusters; use m_getptr() in this case.
2819189592Sbms	 * If we are appending to an existing packet, we need to obtain
2820189592Sbms	 * a pointer to the group record after m_append(), in case a new
2821189592Sbms	 * mbuf was allocated.
2822189592Sbms	 * Only append sources which are in-mode at t1. If we are
2823189592Sbms	 * transitioning to MCAST_UNDEFINED state on the group, do not
2824189592Sbms	 * include source entries.
2825189592Sbms	 * Only report recorded sources in our filter set when responding
2826189592Sbms	 * to a group-source query.
2827189592Sbms	 */
2828189592Sbms	if (record_has_sources) {
2829189592Sbms		if (m == m0) {
2830189592Sbms			md = m_last(m);
2831189592Sbms			pig = (struct igmp_grouprec *)(mtod(md, uint8_t *) +
2832189592Sbms			    md->m_len - nbytes);
2833189592Sbms		} else {
2834189592Sbms			md = m_getptr(m, 0, &off);
2835189592Sbms			pig = (struct igmp_grouprec *)(mtod(md, uint8_t *) +
2836189592Sbms			    off);
2837189592Sbms		}
2838189592Sbms		msrcs = 0;
2839189592Sbms		RB_FOREACH_SAFE(ims, ip_msource_tree, &inm->inm_srcs, nims) {
2840189592Sbms			CTR2(KTR_IGMPV3, "%s: visit node %s", __func__,
2841189592Sbms			    inet_ntoa_haddr(ims->ims_haddr));
2842189592Sbms			now = ims_get_mode(inm, ims, 1);
2843189592Sbms			CTR2(KTR_IGMPV3, "%s: node is %d", __func__, now);
2844189592Sbms			if ((now != mode) ||
2845189592Sbms			    (now == mode && mode == MCAST_UNDEFINED)) {
2846189592Sbms				CTR1(KTR_IGMPV3, "%s: skip node", __func__);
2847189592Sbms				continue;
2848189592Sbms			}
2849189592Sbms			if (is_source_query && ims->ims_stp == 0) {
2850189592Sbms				CTR1(KTR_IGMPV3, "%s: skip unrecorded node",
2851189592Sbms				    __func__);
2852189592Sbms				continue;
2853189592Sbms			}
2854189592Sbms			CTR1(KTR_IGMPV3, "%s: append node", __func__);
2855189592Sbms			naddr = htonl(ims->ims_haddr);
2856189592Sbms			if (!m_append(m, sizeof(in_addr_t), (void *)&naddr)) {
2857189592Sbms				if (m != m0)
2858189592Sbms					m_freem(m);
2859189592Sbms				CTR1(KTR_IGMPV3, "%s: m_append() failed.",
2860189592Sbms				    __func__);
2861189592Sbms				return (-ENOMEM);
2862189592Sbms			}
2863189592Sbms			nbytes += sizeof(in_addr_t);
2864189592Sbms			++msrcs;
2865189592Sbms			if (msrcs == m0srcs)
2866189592Sbms				break;
2867189592Sbms		}
2868189592Sbms		CTR2(KTR_IGMPV3, "%s: msrcs is %d this packet", __func__,
2869189592Sbms		    msrcs);
2870189592Sbms		pig->ig_numsrc = htons(msrcs);
2871189592Sbms		nbytes += (msrcs * sizeof(in_addr_t));
2872189592Sbms	}
2873189592Sbms
2874189592Sbms	if (is_source_query && msrcs == 0) {
2875189592Sbms		CTR1(KTR_IGMPV3, "%s: no recorded sources to report", __func__);
2876189592Sbms		if (m != m0)
2877189592Sbms			m_freem(m);
2878189592Sbms		return (0);
2879189592Sbms	}
2880189592Sbms
2881189592Sbms	/*
2882189592Sbms	 * We are good to go with first packet.
2883189592Sbms	 */
2884189592Sbms	if (m != m0) {
2885189592Sbms		CTR1(KTR_IGMPV3, "%s: enqueueing first packet", __func__);
2886189592Sbms		m->m_pkthdr.PH_vt.vt_nrecs = 1;
2887189592Sbms		_IF_ENQUEUE(ifq, m);
2888189592Sbms	} else
2889189592Sbms		m->m_pkthdr.PH_vt.vt_nrecs++;
2890189592Sbms
2891189592Sbms	/*
2892189592Sbms	 * No further work needed if no source list in packet(s).
2893189592Sbms	 */
2894189592Sbms	if (!record_has_sources)
2895189592Sbms		return (nbytes);
2896189592Sbms
2897189592Sbms	/*
2898189592Sbms	 * Whilst sources remain to be announced, we need to allocate
2899189592Sbms	 * a new packet and fill out as many sources as will fit.
2900189592Sbms	 * Always try for a cluster first.
2901189592Sbms	 */
2902189592Sbms	while (nims != NULL) {
2903189592Sbms		if (_IF_QFULL(ifq)) {
2904189592Sbms			CTR1(KTR_IGMPV3, "%s: outbound queue full", __func__);
2905189592Sbms			return (-ENOMEM);
2906189592Sbms		}
2907189592Sbms		m = m_getcl(M_DONTWAIT, MT_DATA, M_PKTHDR);
2908190692Sbms		if (m)
2909190692Sbms			m->m_data += IGMP_LEADINGSPACE;
2910189592Sbms		if (m == NULL) {
2911189592Sbms			m = m_gethdr(M_DONTWAIT, MT_DATA);
2912189592Sbms			if (m)
2913189592Sbms				MH_ALIGN(m, IGMP_LEADINGSPACE);
2914189592Sbms		}
2915189592Sbms		if (m == NULL)
2916189592Sbms			return (-ENOMEM);
2917189592Sbms		igmp_save_context(m, ifp);
2918189592Sbms		md = m_getptr(m, 0, &off);
2919189592Sbms		pig = (struct igmp_grouprec *)(mtod(md, uint8_t *) + off);
2920189592Sbms		CTR1(KTR_IGMPV3, "%s: allocated next packet", __func__);
2921189592Sbms
2922189592Sbms		if (!m_append(m, sizeof(struct igmp_grouprec), (void *)&ig)) {
2923189592Sbms			if (m != m0)
2924189592Sbms				m_freem(m);
2925189592Sbms			CTR1(KTR_IGMPV3, "%s: m_append() failed.", __func__);
2926189592Sbms			return (-ENOMEM);
2927189592Sbms		}
2928189592Sbms		m->m_pkthdr.PH_vt.vt_nrecs = 1;
2929189592Sbms		nbytes += sizeof(struct igmp_grouprec);
2930189592Sbms
2931189592Sbms		m0srcs = (ifp->if_mtu - IGMP_LEADINGSPACE -
2932189592Sbms		    sizeof(struct igmp_grouprec)) / sizeof(in_addr_t);
2933189592Sbms
2934189592Sbms		msrcs = 0;
2935189592Sbms		RB_FOREACH_FROM(ims, ip_msource_tree, nims) {
2936189592Sbms			CTR2(KTR_IGMPV3, "%s: visit node %s", __func__,
2937189592Sbms			    inet_ntoa_haddr(ims->ims_haddr));
2938189592Sbms			now = ims_get_mode(inm, ims, 1);
2939189592Sbms			if ((now != mode) ||
2940189592Sbms			    (now == mode && mode == MCAST_UNDEFINED)) {
2941189592Sbms				CTR1(KTR_IGMPV3, "%s: skip node", __func__);
2942189592Sbms				continue;
2943189592Sbms			}
2944189592Sbms			if (is_source_query && ims->ims_stp == 0) {
2945189592Sbms				CTR1(KTR_IGMPV3, "%s: skip unrecorded node",
2946189592Sbms				    __func__);
2947189592Sbms				continue;
2948189592Sbms			}
2949189592Sbms			CTR1(KTR_IGMPV3, "%s: append node", __func__);
2950189592Sbms			naddr = htonl(ims->ims_haddr);
2951189592Sbms			if (!m_append(m, sizeof(in_addr_t), (void *)&naddr)) {
2952189592Sbms				if (m != m0)
2953189592Sbms					m_freem(m);
2954189592Sbms				CTR1(KTR_IGMPV3, "%s: m_append() failed.",
2955189592Sbms				    __func__);
2956189592Sbms				return (-ENOMEM);
2957189592Sbms			}
2958189592Sbms			++msrcs;
2959189592Sbms			if (msrcs == m0srcs)
2960189592Sbms				break;
2961189592Sbms		}
2962189592Sbms		pig->ig_numsrc = htons(msrcs);
2963189592Sbms		nbytes += (msrcs * sizeof(in_addr_t));
2964189592Sbms
2965189592Sbms		CTR1(KTR_IGMPV3, "%s: enqueueing next packet", __func__);
2966189592Sbms		_IF_ENQUEUE(ifq, m);
2967189592Sbms	}
2968189592Sbms
2969189592Sbms	return (nbytes);
2970189592Sbms}
2971189592Sbms
2972189592Sbms/*
2973189592Sbms * Type used to mark record pass completion.
2974189592Sbms * We exploit the fact we can cast to this easily from the
2975189592Sbms * current filter modes on each ip_msource node.
2976189592Sbms */
2977189592Sbmstypedef enum {
2978189592Sbms	REC_NONE = 0x00,	/* MCAST_UNDEFINED */
2979189592Sbms	REC_ALLOW = 0x01,	/* MCAST_INCLUDE */
2980189592Sbms	REC_BLOCK = 0x02,	/* MCAST_EXCLUDE */
2981189592Sbms	REC_FULL = REC_ALLOW | REC_BLOCK
2982189592Sbms} rectype_t;
2983189592Sbms
2984189592Sbms/*
2985189592Sbms * Enqueue an IGMPv3 filter list change to the given output queue.
2986189592Sbms *
2987189592Sbms * Source list filter state is held in an RB-tree. When the filter list
2988189592Sbms * for a group is changed without changing its mode, we need to compute
2989189592Sbms * the deltas between T0 and T1 for each source in the filter set,
2990189592Sbms * and enqueue the appropriate ALLOW_NEW/BLOCK_OLD records.
2991189592Sbms *
2992189592Sbms * As we may potentially queue two record types, and the entire R-B tree
2993189592Sbms * needs to be walked at once, we break this out into its own function
2994189592Sbms * so we can generate a tightly packed queue of packets.
2995189592Sbms *
2996189592Sbms * XXX This could be written to only use one tree walk, although that makes
2997189592Sbms * serializing into the mbuf chains a bit harder. For now we do two walks
2998189592Sbms * which makes things easier on us, and it may or may not be harder on
2999189592Sbms * the L2 cache.
3000189592Sbms *
3001189592Sbms * If successful the size of all data appended to the queue is returned,
3002189592Sbms * otherwise an error code less than zero is returned, or zero if
3003189592Sbms * no record(s) were appended.
3004189592Sbms */
3005189592Sbmsstatic int
3006189592Sbmsigmp_v3_enqueue_filter_change(struct ifqueue *ifq, struct in_multi *inm)
3007189592Sbms{
3008189592Sbms	static const int MINRECLEN =
3009189592Sbms	    sizeof(struct igmp_grouprec) + sizeof(in_addr_t);
3010189592Sbms	struct ifnet		*ifp;
3011189592Sbms	struct igmp_grouprec	 ig;
3012189592Sbms	struct igmp_grouprec	*pig;
3013189592Sbms	struct ip_msource	*ims, *nims;
3014189592Sbms	struct mbuf		*m, *m0, *md;
3015189592Sbms	in_addr_t		 naddr;
3016191657Sbms	int			 m0srcs, nbytes, npbytes, off, rsrcs, schanged;
3017189592Sbms	int			 nallow, nblock;
3018189592Sbms	uint8_t			 mode, now, then;
3019189592Sbms	rectype_t		 crt, drt, nrt;
3020189592Sbms
3021189592Sbms	IN_MULTI_LOCK_ASSERT();
3022189592Sbms
3023189592Sbms	if (inm->inm_nsrc == 0 ||
3024189592Sbms	    (inm->inm_st[0].iss_asm > 0 && inm->inm_st[1].iss_asm > 0))
3025189592Sbms		return (0);
3026189592Sbms
3027189592Sbms	ifp = inm->inm_ifp;			/* interface */
3028189592Sbms	mode = inm->inm_st[1].iss_fmode;	/* filter mode at t1 */
3029189592Sbms	crt = REC_NONE;	/* current group record type */
3030189592Sbms	drt = REC_NONE;	/* mask of completed group record types */
3031189592Sbms	nrt = REC_NONE;	/* record type for current node */
3032189592Sbms	m0srcs = 0;	/* # source which will fit in current mbuf chain */
3033189592Sbms	nbytes = 0;	/* # of bytes appended to group's state-change queue */
3034191657Sbms	npbytes = 0;	/* # of bytes appended this packet */
3035189592Sbms	rsrcs = 0;	/* # sources encoded in current record */
3036189592Sbms	schanged = 0;	/* # nodes encoded in overall filter change */
3037189592Sbms	nallow = 0;	/* # of source entries in ALLOW_NEW */
3038189592Sbms	nblock = 0;	/* # of source entries in BLOCK_OLD */
3039189592Sbms	nims = NULL;	/* next tree node pointer */
3040189592Sbms
3041189592Sbms	/*
3042189592Sbms	 * For each possible filter record mode.
3043189592Sbms	 * The first kind of source we encounter tells us which
3044189592Sbms	 * is the first kind of record we start appending.
3045189592Sbms	 * If a node transitioned to UNDEFINED at t1, its mode is treated
3046189592Sbms	 * as the inverse of the group's filter mode.
3047189592Sbms	 */
3048189592Sbms	while (drt != REC_FULL) {
3049189592Sbms		do {
3050189592Sbms			m0 = ifq->ifq_tail;
3051189592Sbms			if (m0 != NULL &&
3052189592Sbms			    (m0->m_pkthdr.PH_vt.vt_nrecs + 1 <=
3053189592Sbms			     IGMP_V3_REPORT_MAXRECS) &&
3054189592Sbms			    (m0->m_pkthdr.len + MINRECLEN) <
3055189592Sbms			     (ifp->if_mtu - IGMP_LEADINGSPACE)) {
3056189592Sbms				m = m0;
3057189592Sbms				m0srcs = (ifp->if_mtu - m0->m_pkthdr.len -
3058189592Sbms					    sizeof(struct igmp_grouprec)) /
3059189592Sbms				    sizeof(in_addr_t);
3060189592Sbms				CTR1(KTR_IGMPV3,
3061189592Sbms				    "%s: use previous packet", __func__);
3062189592Sbms			} else {
3063189592Sbms				m = m_getcl(M_DONTWAIT, MT_DATA, M_PKTHDR);
3064190692Sbms				if (m)
3065190692Sbms					m->m_data += IGMP_LEADINGSPACE;
3066189592Sbms				if (m == NULL) {
3067189592Sbms					m = m_gethdr(M_DONTWAIT, MT_DATA);
3068189592Sbms					if (m)
3069189592Sbms						MH_ALIGN(m, IGMP_LEADINGSPACE);
3070189592Sbms				}
3071189592Sbms				if (m == NULL) {
3072189592Sbms					CTR1(KTR_IGMPV3,
3073189592Sbms					    "%s: m_get*() failed", __func__);
3074189592Sbms					return (-ENOMEM);
3075189592Sbms				}
3076189592Sbms				m->m_pkthdr.PH_vt.vt_nrecs = 0;
3077189592Sbms				igmp_save_context(m, ifp);
3078189592Sbms				m0srcs = (ifp->if_mtu - IGMP_LEADINGSPACE -
3079189592Sbms				    sizeof(struct igmp_grouprec)) /
3080189592Sbms				    sizeof(in_addr_t);
3081191657Sbms				npbytes = 0;
3082189592Sbms				CTR1(KTR_IGMPV3,
3083189592Sbms				    "%s: allocated new packet", __func__);
3084189592Sbms			}
3085189592Sbms			/*
3086189592Sbms			 * Append the IGMP group record header to the
3087189592Sbms			 * current packet's data area.
3088189592Sbms			 * Recalculate pointer to free space for next
3089189592Sbms			 * group record, in case m_append() allocated
3090189592Sbms			 * a new mbuf or cluster.
3091189592Sbms			 */
3092189592Sbms			memset(&ig, 0, sizeof(ig));
3093189592Sbms			ig.ig_group = inm->inm_addr;
3094189592Sbms			if (!m_append(m, sizeof(ig), (void *)&ig)) {
3095189592Sbms				if (m != m0)
3096189592Sbms					m_freem(m);
3097189592Sbms				CTR1(KTR_IGMPV3,
3098189592Sbms				    "%s: m_append() failed", __func__);
3099189592Sbms				return (-ENOMEM);
3100189592Sbms			}
3101191657Sbms			npbytes += sizeof(struct igmp_grouprec);
3102191657Sbms			if (m != m0) {
3103191657Sbms				/* new packet; offset in c hain */
3104191657Sbms				md = m_getptr(m, npbytes -
3105191657Sbms				    sizeof(struct igmp_grouprec), &off);
3106189592Sbms				pig = (struct igmp_grouprec *)(mtod(md,
3107191657Sbms				    uint8_t *) + off);
3108189592Sbms			} else {
3109191657Sbms				/* current packet; offset from last append */
3110191657Sbms				md = m_last(m);
3111189592Sbms				pig = (struct igmp_grouprec *)(mtod(md,
3112191657Sbms				    uint8_t *) + md->m_len -
3113191657Sbms				    sizeof(struct igmp_grouprec));
3114189592Sbms			}
3115189592Sbms			/*
3116189592Sbms			 * Begin walking the tree for this record type
3117189592Sbms			 * pass, or continue from where we left off
3118189592Sbms			 * previously if we had to allocate a new packet.
3119189592Sbms			 * Only report deltas in-mode at t1.
3120189592Sbms			 * We need not report included sources as allowed
3121189592Sbms			 * if we are in inclusive mode on the group,
3122189592Sbms			 * however the converse is not true.
3123189592Sbms			 */
3124189592Sbms			rsrcs = 0;
3125189592Sbms			if (nims == NULL)
3126189592Sbms				nims = RB_MIN(ip_msource_tree, &inm->inm_srcs);
3127189592Sbms			RB_FOREACH_FROM(ims, ip_msource_tree, nims) {
3128189592Sbms				CTR2(KTR_IGMPV3, "%s: visit node %s",
3129189592Sbms				    __func__, inet_ntoa_haddr(ims->ims_haddr));
3130189592Sbms				now = ims_get_mode(inm, ims, 1);
3131189592Sbms				then = ims_get_mode(inm, ims, 0);
3132189592Sbms				CTR3(KTR_IGMPV3, "%s: mode: t0 %d, t1 %d",
3133189592Sbms				    __func__, then, now);
3134189592Sbms				if (now == then) {
3135189592Sbms					CTR1(KTR_IGMPV3,
3136189592Sbms					    "%s: skip unchanged", __func__);
3137189592Sbms					continue;
3138189592Sbms				}
3139189592Sbms				if (mode == MCAST_EXCLUDE &&
3140189592Sbms				    now == MCAST_INCLUDE) {
3141189592Sbms					CTR1(KTR_IGMPV3,
3142189592Sbms					    "%s: skip IN src on EX group",
3143189592Sbms					    __func__);
3144189592Sbms					continue;
3145189592Sbms				}
3146189592Sbms				nrt = (rectype_t)now;
3147189592Sbms				if (nrt == REC_NONE)
3148189592Sbms					nrt = (rectype_t)(~mode & REC_FULL);
3149189592Sbms				if (schanged++ == 0) {
3150189592Sbms					crt = nrt;
3151189592Sbms				} else if (crt != nrt)
3152189592Sbms					continue;
3153189592Sbms				naddr = htonl(ims->ims_haddr);
3154189592Sbms				if (!m_append(m, sizeof(in_addr_t),
3155189592Sbms				    (void *)&naddr)) {
3156189592Sbms					if (m != m0)
3157189592Sbms						m_freem(m);
3158189592Sbms					CTR1(KTR_IGMPV3,
3159189592Sbms					    "%s: m_append() failed", __func__);
3160189592Sbms					return (-ENOMEM);
3161189592Sbms				}
3162189592Sbms				nallow += !!(crt == REC_ALLOW);
3163189592Sbms				nblock += !!(crt == REC_BLOCK);
3164189592Sbms				if (++rsrcs == m0srcs)
3165189592Sbms					break;
3166189592Sbms			}
3167189592Sbms			/*
3168189592Sbms			 * If we did not append any tree nodes on this
3169189592Sbms			 * pass, back out of allocations.
3170189592Sbms			 */
3171189592Sbms			if (rsrcs == 0) {
3172191657Sbms				npbytes -= sizeof(struct igmp_grouprec);
3173189592Sbms				if (m != m0) {
3174189592Sbms					CTR1(KTR_IGMPV3,
3175189592Sbms					    "%s: m_free(m)", __func__);
3176189592Sbms					m_freem(m);
3177189592Sbms				} else {
3178189592Sbms					CTR1(KTR_IGMPV3,
3179189592Sbms					    "%s: m_adj(m, -ig)", __func__);
3180189592Sbms					m_adj(m, -((int)sizeof(
3181189592Sbms					    struct igmp_grouprec)));
3182189592Sbms				}
3183189592Sbms				continue;
3184189592Sbms			}
3185191657Sbms			npbytes += (rsrcs * sizeof(in_addr_t));
3186189592Sbms			if (crt == REC_ALLOW)
3187189592Sbms				pig->ig_type = IGMP_ALLOW_NEW_SOURCES;
3188189592Sbms			else if (crt == REC_BLOCK)
3189189592Sbms				pig->ig_type = IGMP_BLOCK_OLD_SOURCES;
3190189592Sbms			pig->ig_numsrc = htons(rsrcs);
3191189592Sbms			/*
3192189592Sbms			 * Count the new group record, and enqueue this
3193189592Sbms			 * packet if it wasn't already queued.
3194189592Sbms			 */
3195189592Sbms			m->m_pkthdr.PH_vt.vt_nrecs++;
3196189592Sbms			if (m != m0)
3197189592Sbms				_IF_ENQUEUE(ifq, m);
3198191657Sbms			nbytes += npbytes;
3199189592Sbms		} while (nims != NULL);
3200189592Sbms		drt |= crt;
3201189592Sbms		crt = (~crt & REC_FULL);
3202189592Sbms	}
3203189592Sbms
3204189592Sbms	CTR3(KTR_IGMPV3, "%s: queued %d ALLOW_NEW, %d BLOCK_OLD", __func__,
3205189592Sbms	    nallow, nblock);
3206189592Sbms
3207189592Sbms	return (nbytes);
3208189592Sbms}
3209189592Sbms
3210189592Sbmsstatic int
3211189592Sbmsigmp_v3_merge_state_changes(struct in_multi *inm, struct ifqueue *ifscq)
3212189592Sbms{
3213189592Sbms	struct ifqueue	*gq;
3214189592Sbms	struct mbuf	*m;		/* pending state-change */
3215189592Sbms	struct mbuf	*m0;		/* copy of pending state-change */
3216189592Sbms	struct mbuf	*mt;		/* last state-change in packet */
3217189592Sbms	int		 docopy, domerge;
3218189592Sbms	u_int		 recslen;
3219189592Sbms
3220189592Sbms	docopy = 0;
3221189592Sbms	domerge = 0;
3222189592Sbms	recslen = 0;
3223189592Sbms
3224189592Sbms	IN_MULTI_LOCK_ASSERT();
3225189592Sbms	IGMP_LOCK_ASSERT();
3226189592Sbms
3227189592Sbms	/*
3228189592Sbms	 * If there are further pending retransmissions, make a writable
3229189592Sbms	 * copy of each queued state-change message before merging.
3230189592Sbms	 */
3231189592Sbms	if (inm->inm_scrv > 0)
3232189592Sbms		docopy = 1;
3233189592Sbms
3234189592Sbms	gq = &inm->inm_scq;
3235189592Sbms#ifdef KTR
3236189592Sbms	if (gq->ifq_head == NULL) {
3237189592Sbms		CTR2(KTR_IGMPV3, "%s: WARNING: queue for inm %p is empty",
3238189592Sbms		    __func__, inm);
3239189592Sbms	}
3240189592Sbms#endif
3241189592Sbms
3242189592Sbms	m = gq->ifq_head;
3243189592Sbms	while (m != NULL) {
3244189592Sbms		/*
3245189592Sbms		 * Only merge the report into the current packet if
3246189592Sbms		 * there is sufficient space to do so; an IGMPv3 report
3247189592Sbms		 * packet may only contain 65,535 group records.
3248189592Sbms		 * Always use a simple mbuf chain concatentation to do this,
3249189592Sbms		 * as large state changes for single groups may have
3250189592Sbms		 * allocated clusters.
3251189592Sbms		 */
3252189592Sbms		domerge = 0;
3253189592Sbms		mt = ifscq->ifq_tail;
3254189592Sbms		if (mt != NULL) {
3255189592Sbms			recslen = m_length(m, NULL);
3256189592Sbms
3257189592Sbms			if ((mt->m_pkthdr.PH_vt.vt_nrecs +
3258189592Sbms			    m->m_pkthdr.PH_vt.vt_nrecs <=
3259189592Sbms			    IGMP_V3_REPORT_MAXRECS) &&
3260189592Sbms			    (mt->m_pkthdr.len + recslen <=
3261189592Sbms			    (inm->inm_ifp->if_mtu - IGMP_LEADINGSPACE)))
3262189592Sbms				domerge = 1;
3263189592Sbms		}
3264189592Sbms
3265189592Sbms		if (!domerge && _IF_QFULL(gq)) {
3266189592Sbms			CTR2(KTR_IGMPV3,
3267189592Sbms			    "%s: outbound queue full, skipping whole packet %p",
3268189592Sbms			    __func__, m);
3269189592Sbms			mt = m->m_nextpkt;
3270189592Sbms			if (!docopy)
3271189592Sbms				m_freem(m);
3272189592Sbms			m = mt;
3273189592Sbms			continue;
3274189592Sbms		}
3275189592Sbms
3276189592Sbms		if (!docopy) {
3277189592Sbms			CTR2(KTR_IGMPV3, "%s: dequeueing %p", __func__, m);
3278189592Sbms			_IF_DEQUEUE(gq, m0);
3279189592Sbms			m = m0->m_nextpkt;
3280189592Sbms		} else {
3281189592Sbms			CTR2(KTR_IGMPV3, "%s: copying %p", __func__, m);
3282189592Sbms			m0 = m_dup(m, M_NOWAIT);
3283189592Sbms			if (m0 == NULL)
3284189592Sbms				return (ENOMEM);
3285189592Sbms			m0->m_nextpkt = NULL;
3286189592Sbms			m = m->m_nextpkt;
3287189592Sbms		}
3288189592Sbms
3289189592Sbms		if (!domerge) {
3290189592Sbms			CTR3(KTR_IGMPV3, "%s: queueing %p to ifscq %p)",
3291189592Sbms			    __func__, m0, ifscq);
3292189592Sbms			_IF_ENQUEUE(ifscq, m0);
3293189592Sbms		} else {
3294189592Sbms			struct mbuf *mtl;	/* last mbuf of packet mt */
3295189592Sbms
3296189592Sbms			CTR3(KTR_IGMPV3, "%s: merging %p with ifscq tail %p)",
3297189592Sbms			    __func__, m0, mt);
3298189592Sbms
3299189592Sbms			mtl = m_last(mt);
3300189592Sbms			m0->m_flags &= ~M_PKTHDR;
3301189592Sbms			mt->m_pkthdr.len += recslen;
3302189592Sbms			mt->m_pkthdr.PH_vt.vt_nrecs +=
3303189592Sbms			    m0->m_pkthdr.PH_vt.vt_nrecs;
3304189592Sbms
3305189592Sbms			mtl->m_next = m0;
3306189592Sbms		}
3307189592Sbms	}
3308189592Sbms
3309189592Sbms	return (0);
3310189592Sbms}
3311189592Sbms
3312189592Sbms/*
3313189592Sbms * Respond to a pending IGMPv3 General Query.
3314189592Sbms */
3315189592Sbmsstatic void
3316189592Sbmsigmp_v3_dispatch_general_query(struct igmp_ifinfo *igi)
3317189592Sbms{
3318230076Sjhb	struct ifmultiaddr	*ifma;
3319189592Sbms	struct ifnet		*ifp;
3320189592Sbms	struct in_multi		*inm;
3321189592Sbms	int			 retval, loop;
3322189592Sbms
3323189592Sbms	IN_MULTI_LOCK_ASSERT();
3324189592Sbms	IGMP_LOCK_ASSERT();
3325189592Sbms
3326189592Sbms	KASSERT(igi->igi_version == IGMP_VERSION_3,
3327189592Sbms	    ("%s: called when version %d", __func__, igi->igi_version));
3328189592Sbms
3329189592Sbms	ifp = igi->igi_ifp;
3330189592Sbms
3331233200Sjhb	IF_ADDR_RLOCK(ifp);
3332230076Sjhb	TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
3333189931Sbms		if (ifma->ifma_addr->sa_family != AF_INET ||
3334189931Sbms		    ifma->ifma_protospec == NULL)
3335189592Sbms			continue;
3336189592Sbms
3337189592Sbms		inm = (struct in_multi *)ifma->ifma_protospec;
3338189592Sbms		KASSERT(ifp == inm->inm_ifp,
3339189592Sbms		    ("%s: inconsistent ifp", __func__));
3340189592Sbms
3341189592Sbms		switch (inm->inm_state) {
3342189592Sbms		case IGMP_NOT_MEMBER:
3343189592Sbms		case IGMP_SILENT_MEMBER:
3344189592Sbms			break;
3345189592Sbms		case IGMP_REPORTING_MEMBER:
3346189592Sbms		case IGMP_IDLE_MEMBER:
3347189592Sbms		case IGMP_LAZY_MEMBER:
3348189592Sbms		case IGMP_SLEEPING_MEMBER:
3349189592Sbms		case IGMP_AWAKENING_MEMBER:
3350189592Sbms			inm->inm_state = IGMP_REPORTING_MEMBER;
3351189592Sbms			retval = igmp_v3_enqueue_group_record(&igi->igi_gq,
3352189592Sbms			    inm, 0, 0, 0);
3353189592Sbms			CTR2(KTR_IGMPV3, "%s: enqueue record = %d",
3354189592Sbms			    __func__, retval);
3355189592Sbms			break;
3356189592Sbms		case IGMP_G_QUERY_PENDING_MEMBER:
3357189592Sbms		case IGMP_SG_QUERY_PENDING_MEMBER:
3358189592Sbms		case IGMP_LEAVING_MEMBER:
3359189592Sbms			break;
3360189592Sbms		}
3361189592Sbms	}
3362233200Sjhb	IF_ADDR_RUNLOCK(ifp);
3363189592Sbms
3364189592Sbms	loop = (igi->igi_flags & IGIF_LOOPBACK) ? 1 : 0;
3365189592Sbms	igmp_dispatch_queue(&igi->igi_gq, IGMP_MAX_RESPONSE_BURST, loop);
3366189592Sbms
3367189592Sbms	/*
3368189592Sbms	 * Slew transmission of bursts over 500ms intervals.
3369189592Sbms	 */
3370189592Sbms	if (igi->igi_gq.ifq_head != NULL) {
3371189592Sbms		igi->igi_v3_timer = 1 + IGMP_RANDOM_DELAY(
3372189592Sbms		    IGMP_RESPONSE_BURST_INTERVAL);
3373189592Sbms		V_interface_timers_running = 1;
3374189592Sbms	}
3375189592Sbms}
3376189592Sbms
3377189592Sbms/*
3378189592Sbms * Transmit the next pending IGMP message in the output queue.
3379189592Sbms *
3380189592Sbms * We get called from netisr_processqueue(). A mutex private to igmpoq
3381189592Sbms * will be acquired and released around this routine.
3382189592Sbms *
3383189592Sbms * VIMAGE: Needs to store/restore vnet pointer on a per-mbuf-chain basis.
3384189592Sbms * MRT: Nothing needs to be done, as IGMP traffic is always local to
3385189592Sbms * a link and uses a link-scope multicast address.
3386189592Sbms */
3387189592Sbmsstatic void
3388189592Sbmsigmp_intr(struct mbuf *m)
3389189592Sbms{
3390189592Sbms	struct ip_moptions	 imo;
3391189592Sbms	struct ifnet		*ifp;
3392189592Sbms	struct mbuf		*ipopts, *m0;
3393189592Sbms	int			 error;
3394189592Sbms	uint32_t		 ifindex;
3395189592Sbms
3396189592Sbms	CTR2(KTR_IGMPV3, "%s: transmit %p", __func__, m);
3397189592Sbms
3398189592Sbms	/*
3399191548Szec	 * Set VNET image pointer from enqueued mbuf chain
3400189592Sbms	 * before doing anything else. Whilst we use interface
3401189592Sbms	 * indexes to guard against interface detach, they are
3402189592Sbms	 * unique to each VIMAGE and must be retrieved.
3403189592Sbms	 */
3404191816Szec	CURVNET_SET((struct vnet *)(m->m_pkthdr.header));
3405189592Sbms	ifindex = igmp_restore_context(m);
3406189592Sbms
3407189592Sbms	/*
3408189592Sbms	 * Check if the ifnet still exists. This limits the scope of
3409189592Sbms	 * any race in the absence of a global ifp lock for low cost
3410189592Sbms	 * (an array lookup).
3411189592Sbms	 */
3412189592Sbms	ifp = ifnet_byindex(ifindex);
3413189592Sbms	if (ifp == NULL) {
3414189592Sbms		CTR3(KTR_IGMPV3, "%s: dropped %p as ifindex %u went away.",
3415189592Sbms		    __func__, m, ifindex);
3416189592Sbms		m_freem(m);
3417190951Srwatson		IPSTAT_INC(ips_noroute);
3418189592Sbms		goto out;
3419189592Sbms	}
3420189592Sbms
3421189592Sbms	ipopts = V_igmp_sendra ? m_raopt : NULL;
3422189592Sbms
3423119181Srwatson	imo.imo_multicast_ttl  = 1;
342415292Swollman	imo.imo_multicast_vif  = -1;
3425181803Sbz	imo.imo_multicast_loop = (V_ip_mrouter != NULL);
34261541Srgrimes
342715292Swollman	/*
3428189592Sbms	 * If the user requested that IGMP traffic be explicitly
3429189592Sbms	 * redirected to the loopback interface (e.g. they are running a
3430189592Sbms	 * MANET interface and the routing protocol needs to see the
3431189592Sbms	 * updates), handle this now.
343215292Swollman	 */
3433189592Sbms	if (m->m_flags & M_IGMP_LOOP)
3434189592Sbms		imo.imo_multicast_ifp = V_loif;
3435189592Sbms	else
3436189592Sbms		imo.imo_multicast_ifp = ifp;
34372531Swollman
3438189592Sbms	if (m->m_flags & M_IGMPV2) {
3439189592Sbms		m0 = m;
3440189592Sbms	} else {
3441189592Sbms		m0 = igmp_v3_encap_report(ifp, m);
3442189592Sbms		if (m0 == NULL) {
3443189592Sbms			CTR2(KTR_IGMPV3, "%s: dropped %p", __func__, m);
3444189592Sbms			m_freem(m);
3445190951Srwatson			IPSTAT_INC(ips_odropped);
3446189592Sbms			goto out;
3447189592Sbms		}
3448189592Sbms	}
3449189592Sbms
3450189592Sbms	igmp_scrub_context(m0);
3451189592Sbms	m->m_flags &= ~(M_PROTOFLAGS);
3452189592Sbms	m0->m_pkthdr.rcvif = V_loif;
3453189592Sbms#ifdef MAC
3454189592Sbms	mac_netinet_igmp_send(ifp, m0);
3455189592Sbms#endif
3456189592Sbms	error = ip_output(m0, ipopts, NULL, 0, &imo, NULL);
3457189592Sbms	if (error) {
3458189592Sbms		CTR3(KTR_IGMPV3, "%s: ip_output(%p) = %d", __func__, m0, error);
3459189592Sbms		goto out;
3460189592Sbms	}
3461189592Sbms
3462190965Srwatson	IGMPSTAT_INC(igps_snd_reports);
3463189592Sbms
3464189592Sbmsout:
3465189592Sbms	/*
3466189592Sbms	 * We must restore the existing vnet pointer before
3467189592Sbms	 * continuing as we are run from netisr context.
3468189592Sbms	 */
3469189592Sbms	CURVNET_RESTORE();
34701541Srgrimes}
3471189592Sbms
3472189592Sbms/*
3473189592Sbms * Encapsulate an IGMPv3 report.
3474189592Sbms *
3475189592Sbms * The internal mbuf flag M_IGMPV3_HDR is used to indicate that the mbuf
3476189592Sbms * chain has already had its IP/IGMPv3 header prepended. In this case
3477189592Sbms * the function will not attempt to prepend; the lengths and checksums
3478189592Sbms * will however be re-computed.
3479189592Sbms *
3480189592Sbms * Returns a pointer to the new mbuf chain head, or NULL if the
3481189592Sbms * allocation failed.
3482189592Sbms */
3483189592Sbmsstatic struct mbuf *
3484189592Sbmsigmp_v3_encap_report(struct ifnet *ifp, struct mbuf *m)
3485189592Sbms{
3486189592Sbms	struct igmp_report	*igmp;
3487189592Sbms	struct ip		*ip;
3488189592Sbms	int			 hdrlen, igmpreclen;
3489189592Sbms
3490189592Sbms	KASSERT((m->m_flags & M_PKTHDR),
3491189592Sbms	    ("%s: mbuf chain %p is !M_PKTHDR", __func__, m));
3492189592Sbms
3493189592Sbms	igmpreclen = m_length(m, NULL);
3494189592Sbms	hdrlen = sizeof(struct ip) + sizeof(struct igmp_report);
3495189592Sbms
3496189592Sbms	if (m->m_flags & M_IGMPV3_HDR) {
3497189592Sbms		igmpreclen -= hdrlen;
3498189592Sbms	} else {
3499189592Sbms		M_PREPEND(m, hdrlen, M_DONTWAIT);
3500189592Sbms		if (m == NULL)
3501189592Sbms			return (NULL);
3502189592Sbms		m->m_flags |= M_IGMPV3_HDR;
3503189592Sbms	}
3504189592Sbms
3505189592Sbms	CTR2(KTR_IGMPV3, "%s: igmpreclen is %d", __func__, igmpreclen);
3506189592Sbms
3507189592Sbms	m->m_data += sizeof(struct ip);
3508189592Sbms	m->m_len -= sizeof(struct ip);
3509189592Sbms
3510189592Sbms	igmp = mtod(m, struct igmp_report *);
3511189592Sbms	igmp->ir_type = IGMP_v3_HOST_MEMBERSHIP_REPORT;
3512189592Sbms	igmp->ir_rsv1 = 0;
3513189592Sbms	igmp->ir_rsv2 = 0;
3514189592Sbms	igmp->ir_numgrps = htons(m->m_pkthdr.PH_vt.vt_nrecs);
3515189592Sbms	igmp->ir_cksum = 0;
3516189592Sbms	igmp->ir_cksum = in_cksum(m, sizeof(struct igmp_report) + igmpreclen);
3517189592Sbms	m->m_pkthdr.PH_vt.vt_nrecs = 0;
3518189592Sbms
3519189592Sbms	m->m_data -= sizeof(struct ip);
3520189592Sbms	m->m_len += sizeof(struct ip);
3521189592Sbms
3522189592Sbms	ip = mtod(m, struct ip *);
3523189592Sbms	ip->ip_tos = IPTOS_PREC_INTERNETCONTROL;
3524189592Sbms	ip->ip_len = hdrlen + igmpreclen;
3525189592Sbms	ip->ip_off = IP_DF;
3526189592Sbms	ip->ip_p = IPPROTO_IGMP;
3527189592Sbms	ip->ip_sum = 0;
3528189592Sbms
3529189592Sbms	ip->ip_src.s_addr = INADDR_ANY;
3530189592Sbms
3531189592Sbms	if (m->m_flags & M_IGMP_LOOP) {
3532189592Sbms		struct in_ifaddr *ia;
3533189592Sbms
3534189592Sbms		IFP_TO_IA(ifp, ia);
3535194760Srwatson		if (ia != NULL) {
3536189592Sbms			ip->ip_src = ia->ia_addr.sin_addr;
3537194760Srwatson			ifa_free(&ia->ia_ifa);
3538194760Srwatson		}
3539189592Sbms	}
3540189592Sbms
3541189592Sbms	ip->ip_dst.s_addr = htonl(INADDR_ALLRPTS_GROUP);
3542189592Sbms
3543189592Sbms	return (m);
3544189592Sbms}
3545189592Sbms
3546189592Sbms#ifdef KTR
3547189592Sbmsstatic char *
3548189592Sbmsigmp_rec_type_to_str(const int type)
3549189592Sbms{
3550189592Sbms
3551189592Sbms	switch (type) {
3552189592Sbms		case IGMP_CHANGE_TO_EXCLUDE_MODE:
3553189592Sbms			return "TO_EX";
3554189592Sbms			break;
3555189592Sbms		case IGMP_CHANGE_TO_INCLUDE_MODE:
3556189592Sbms			return "TO_IN";
3557189592Sbms			break;
3558189592Sbms		case IGMP_MODE_IS_EXCLUDE:
3559189592Sbms			return "MODE_EX";
3560189592Sbms			break;
3561189592Sbms		case IGMP_MODE_IS_INCLUDE:
3562189592Sbms			return "MODE_IN";
3563189592Sbms			break;
3564189592Sbms		case IGMP_ALLOW_NEW_SOURCES:
3565189592Sbms			return "ALLOW_NEW";
3566189592Sbms			break;
3567189592Sbms		case IGMP_BLOCK_OLD_SOURCES:
3568189592Sbms			return "BLOCK_OLD";
3569189592Sbms			break;
3570189592Sbms		default:
3571189592Sbms			break;
3572189592Sbms	}
3573189592Sbms	return "unknown";
3574189592Sbms}
3575189592Sbms#endif
3576189592Sbms
3577189592Sbmsstatic void
3578195837Srwatsonigmp_init(void *unused __unused)
3579189592Sbms{
3580189592Sbms
3581189592Sbms	CTR1(KTR_IGMPV3, "%s: initializing", __func__);
3582189592Sbms
3583189592Sbms	IGMP_LOCK_INIT();
3584189592Sbms
3585189592Sbms	m_raopt = igmp_ra_alloc();
3586189592Sbms
3587193219Srwatson	netisr_register(&igmp_nh);
3588189592Sbms}
3589195837SrwatsonSYSINIT(igmp_init, SI_SUB_PSEUDO, SI_ORDER_MIDDLE, igmp_init, NULL);
3590189592Sbms
3591189592Sbmsstatic void
3592195837Srwatsonigmp_uninit(void *unused __unused)
3593189592Sbms{
3594189592Sbms
3595189592Sbms	CTR1(KTR_IGMPV3, "%s: tearing down", __func__);
3596189592Sbms
3597193219Srwatson	netisr_unregister(&igmp_nh);
3598189592Sbms
3599189592Sbms	m_free(m_raopt);
3600189592Sbms	m_raopt = NULL;
3601189592Sbms
3602189592Sbms	IGMP_LOCK_DESTROY();
3603189592Sbms}
3604195837SrwatsonSYSUNINIT(igmp_uninit, SI_SUB_PSEUDO, SI_ORDER_MIDDLE, igmp_uninit, NULL);
3605189592Sbms
3606195837Srwatsonstatic void
3607195837Srwatsonvnet_igmp_init(const void *unused __unused)
3608189592Sbms{
3609189592Sbms
3610189592Sbms	CTR1(KTR_IGMPV3, "%s: initializing", __func__);
3611189592Sbms
3612189592Sbms	LIST_INIT(&V_igi_head);
3613189592Sbms}
3614195837SrwatsonVNET_SYSINIT(vnet_igmp_init, SI_SUB_PSEUDO, SI_ORDER_ANY, vnet_igmp_init,
3615195837Srwatson    NULL);
3616189592Sbms
3617195837Srwatsonstatic void
3618195837Srwatsonvnet_igmp_uninit(const void *unused __unused)
3619189592Sbms{
3620189592Sbms
3621189592Sbms	CTR1(KTR_IGMPV3, "%s: tearing down", __func__);
3622189592Sbms
3623189592Sbms	KASSERT(LIST_EMPTY(&V_igi_head),
3624189592Sbms	    ("%s: igi list not empty; ifnets not detached?", __func__));
3625189592Sbms}
3626195837SrwatsonVNET_SYSUNINIT(vnet_igmp_uninit, SI_SUB_PSEUDO, SI_ORDER_ANY,
3627195837Srwatson    vnet_igmp_uninit, NULL);
3628189592Sbms
3629189592Sbmsstatic int
3630189592Sbmsigmp_modevent(module_t mod, int type, void *unused __unused)
3631189592Sbms{
3632189592Sbms
3633189592Sbms    switch (type) {
3634189592Sbms    case MOD_LOAD:
3635189592Sbms    case MOD_UNLOAD:
3636189592Sbms	break;
3637189592Sbms    default:
3638189592Sbms	return (EOPNOTSUPP);
3639189592Sbms    }
3640189592Sbms    return (0);
3641189592Sbms}
3642189592Sbms
3643189592Sbmsstatic moduledata_t igmp_mod = {
3644189592Sbms    "igmp",
3645189592Sbms    igmp_modevent,
3646189592Sbms    0
3647189592Sbms};
3648189592SbmsDECLARE_MODULE(igmp, igmp_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
3649