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 */
188227293Sedstatic 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 */
283227309Sedstatic SYSCTL_NODE(_net_inet_igmp, OID_AUTO, ifinfo,
284227309Sed    CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_igmp_ifinfo,
285227309Sed    "Per-interface IGMPv3 state");
2861541Srgrimes
287189592Sbmsstatic __inline void
288189592Sbmsigmp_save_context(struct mbuf *m, struct ifnet *ifp)
289189592Sbms{
290189592Sbms
291189592Sbms#ifdef VIMAGE
292254804Sandre	m->m_pkthdr.PH_loc.ptr = 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
301254804Sandre	m->m_pkthdr.PH_loc.ptr = 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)
329254804Sandre	KASSERT(curvnet == (m->m_pkthdr.PH_loc.ptr),
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
526248326Sglebius	m = m_get(M_WAITOK, MT_DATA);
527189592Sbms	p = mtod(m, struct ipoption *);
528189592Sbms	p->ipopt_dst.s_addr = INADDR_ANY;
529305558Sdim	p->ipopt_list[0] = (char)IPOPT_RA;	/* Router Alert Option */
530305558Sdim	p->ipopt_list[1] = 0x04;		/* 4 bytes long */
531305558Sdim	p->ipopt_list[2] = IPOPT_EOL;		/* End of IP option list */
532305558Sdim	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) {
621229621Sjhb		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		}
637229621Sjhb		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
658189592Sbms	CTR3(KTR_IGMPV3, "%s: called for ifp %p(%s)",
659189592Sbms	    __func__, ifp, ifp->if_xname);
660189592Sbms
661189592Sbms	IGMP_LOCK();
662189592Sbms	igi_delete_locked(ifp);
663189592Sbms	IGMP_UNLOCK();
664189592Sbms}
665189592Sbms
666189592Sbmsstatic void
667189592Sbmsigi_delete_locked(const struct ifnet *ifp)
668189592Sbms{
669189592Sbms	struct igmp_ifinfo *igi, *tigi;
670189592Sbms
671189592Sbms	CTR3(KTR_IGMPV3, "%s: freeing igmp_ifinfo for ifp %p(%s)",
672189592Sbms	    __func__, ifp, ifp->if_xname);
673189592Sbms
674189592Sbms	IGMP_LOCK_ASSERT();
675189592Sbms
676189592Sbms	LIST_FOREACH_SAFE(igi, &V_igi_head, igi_link, tigi) {
677189592Sbms		if (igi->igi_ifp == ifp) {
678189592Sbms			/*
679189592Sbms			 * Free deferred General Query responses.
680189592Sbms			 */
681189592Sbms			_IF_DRAIN(&igi->igi_gq);
682189592Sbms
683189592Sbms			LIST_REMOVE(igi, igi_link);
684189592Sbms
685189592Sbms			KASSERT(SLIST_EMPTY(&igi->igi_relinmhead),
686189592Sbms			    ("%s: there are dangling in_multi references",
687189592Sbms			    __func__));
688189592Sbms
689189592Sbms			free(igi, M_IGMP);
690189592Sbms			return;
691189592Sbms		}
692189592Sbms	}
693189592Sbms
694189592Sbms#ifdef INVARIANTS
695189592Sbms	panic("%s: igmp_ifinfo not found for ifp %p\n", __func__,  ifp);
696189592Sbms#endif
697189592Sbms}
698189592Sbms
699189592Sbms/*
700189592Sbms * Process a received IGMPv1 query.
701189592Sbms * Return non-zero if the message should be dropped.
702189592Sbms *
703189592Sbms * VIMAGE: The curvnet pointer is derived from the input ifp.
704189592Sbms */
705189592Sbmsstatic int
706193231Sbmsigmp_input_v1_query(struct ifnet *ifp, const struct ip *ip,
707193231Sbms    const struct igmp *igmp)
708189592Sbms{
709189592Sbms	struct ifmultiaddr	*ifma;
710189592Sbms	struct igmp_ifinfo	*igi;
711189592Sbms	struct in_multi		*inm;
7122531Swollman
713189592Sbms	/*
714193231Sbms	 * IGMPv1 Host Mmembership Queries SHOULD always be addressed to
715193231Sbms	 * 224.0.0.1. They are always treated as General Queries.
716189592Sbms	 * igmp_group is always ignored. Do not drop it as a userland
717189592Sbms	 * daemon may wish to see it.
718193231Sbms	 * XXX SMPng: unlocked increments in igmpstat assumed atomic.
719189592Sbms	 */
720193231Sbms	if (!in_allhosts(ip->ip_dst) || !in_nullhost(igmp->igmp_group)) {
721190965Srwatson		IGMPSTAT_INC(igps_rcv_badqueries);
722189592Sbms		return (0);
723189592Sbms	}
724190965Srwatson	IGMPSTAT_INC(igps_rcv_gen_queries);
725189592Sbms
726189592Sbms	IN_MULTI_LOCK();
727189592Sbms	IGMP_LOCK();
728189592Sbms
729189592Sbms	igi = ((struct in_ifinfo *)ifp->if_afdata[AF_INET])->ii_igmp;
730189592Sbms	KASSERT(igi != NULL, ("%s: no igmp_ifinfo for ifp %p", __func__, ifp));
731189592Sbms
732189592Sbms	if (igi->igi_flags & IGIF_LOOPBACK) {
733189592Sbms		CTR2(KTR_IGMPV3, "ignore v1 query on IGIF_LOOPBACK ifp %p(%s)",
734189592Sbms		    ifp, ifp->if_xname);
735189592Sbms		goto out_locked;
736189592Sbms	}
737189592Sbms
738193231Sbms	/*
739193231Sbms	 * Switch to IGMPv1 host compatibility mode.
740193231Sbms	 */
741189592Sbms	igmp_set_version(igi, IGMP_VERSION_1);
742189592Sbms
743189592Sbms	CTR2(KTR_IGMPV3, "process v1 query on ifp %p(%s)", ifp, ifp->if_xname);
744189592Sbms
745189592Sbms	/*
746189592Sbms	 * Start the timers in all of our group records
747189592Sbms	 * for the interface on which the query arrived,
748189592Sbms	 * except those which are already running.
749189592Sbms	 */
750229621Sjhb	IF_ADDR_RLOCK(ifp);
751189592Sbms	TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
752189931Sbms		if (ifma->ifma_addr->sa_family != AF_INET ||
753189931Sbms		    ifma->ifma_protospec == NULL)
754189592Sbms			continue;
755189592Sbms		inm = (struct in_multi *)ifma->ifma_protospec;
756189592Sbms		if (inm->inm_timer != 0)
757189592Sbms			continue;
758189592Sbms		switch (inm->inm_state) {
759189592Sbms		case IGMP_NOT_MEMBER:
760189592Sbms		case IGMP_SILENT_MEMBER:
761189592Sbms			break;
762189592Sbms		case IGMP_G_QUERY_PENDING_MEMBER:
763189592Sbms		case IGMP_SG_QUERY_PENDING_MEMBER:
764189592Sbms		case IGMP_REPORTING_MEMBER:
765189592Sbms		case IGMP_IDLE_MEMBER:
766189592Sbms		case IGMP_LAZY_MEMBER:
767189592Sbms		case IGMP_SLEEPING_MEMBER:
768189592Sbms		case IGMP_AWAKENING_MEMBER:
769189592Sbms			inm->inm_state = IGMP_REPORTING_MEMBER;
770189592Sbms			inm->inm_timer = IGMP_RANDOM_DELAY(
771189592Sbms			    IGMP_V1V2_MAX_RI * PR_FASTHZ);
772189592Sbms			V_current_state_timers_running = 1;
773189592Sbms			break;
774189592Sbms		case IGMP_LEAVING_MEMBER:
775189592Sbms			break;
776119181Srwatson		}
777119181Srwatson	}
778229621Sjhb	IF_ADDR_RUNLOCK(ifp);
779189592Sbms
780189592Sbmsout_locked:
781189592Sbms	IGMP_UNLOCK();
782189592Sbms	IN_MULTI_UNLOCK();
783189592Sbms
784189592Sbms	return (0);
785189592Sbms}
786189592Sbms
787189592Sbms/*
788189592Sbms * Process a received IGMPv2 general or group-specific query.
789189592Sbms */
790189592Sbmsstatic int
791189592Sbmsigmp_input_v2_query(struct ifnet *ifp, const struct ip *ip,
792189592Sbms    const struct igmp *igmp)
793189592Sbms{
794189592Sbms	struct ifmultiaddr	*ifma;
795189592Sbms	struct igmp_ifinfo	*igi;
796189592Sbms	struct in_multi		*inm;
797193231Sbms	int			 is_general_query;
798189592Sbms	uint16_t		 timer;
799189592Sbms
800193231Sbms	is_general_query = 0;
801193231Sbms
802189592Sbms	/*
803193231Sbms	 * Validate address fields upfront.
804193231Sbms	 * XXX SMPng: unlocked increments in igmpstat assumed atomic.
805189592Sbms	 */
806193231Sbms	if (in_nullhost(igmp->igmp_group)) {
807193231Sbms		/*
808193231Sbms		 * IGMPv2 General Query.
809193231Sbms		 * If this was not sent to the all-hosts group, ignore it.
810193231Sbms		 */
811193231Sbms		if (!in_allhosts(ip->ip_dst))
812193231Sbms			return (0);
813193231Sbms		IGMPSTAT_INC(igps_rcv_gen_queries);
814193231Sbms		is_general_query = 1;
815193231Sbms	} else {
816193231Sbms		/* IGMPv2 Group-Specific Query. */
817193231Sbms		IGMPSTAT_INC(igps_rcv_group_queries);
818193231Sbms	}
819193231Sbms
820189592Sbms	IN_MULTI_LOCK();
821189592Sbms	IGMP_LOCK();
822189592Sbms
823189592Sbms	igi = ((struct in_ifinfo *)ifp->if_afdata[AF_INET])->ii_igmp;
824189592Sbms	KASSERT(igi != NULL, ("%s: no igmp_ifinfo for ifp %p", __func__, ifp));
825189592Sbms
826189592Sbms	if (igi->igi_flags & IGIF_LOOPBACK) {
827189592Sbms		CTR2(KTR_IGMPV3, "ignore v2 query on IGIF_LOOPBACK ifp %p(%s)",
828189592Sbms		    ifp, ifp->if_xname);
829189592Sbms		goto out_locked;
830144163Ssam	}
831189592Sbms
832193231Sbms	/*
833193231Sbms	 * Ignore v2 query if in v1 Compatibility Mode.
834193231Sbms	 */
835193231Sbms	if (igi->igi_version == IGMP_VERSION_1)
836193231Sbms		goto out_locked;
837193231Sbms
838189592Sbms	igmp_set_version(igi, IGMP_VERSION_2);
839189592Sbms
840189592Sbms	timer = igmp->igmp_code * PR_FASTHZ / IGMP_TIMER_SCALE;
841189592Sbms	if (timer == 0)
842189592Sbms		timer = 1;
843189592Sbms
844193231Sbms	if (is_general_query) {
845189592Sbms		/*
846193231Sbms		 * For each reporting group joined on this
847193231Sbms		 * interface, kick the report timer.
848193231Sbms		 */
849193231Sbms		CTR2(KTR_IGMPV3, "process v2 general query on ifp %p(%s)",
850193231Sbms		    ifp, ifp->if_xname);
851229621Sjhb		IF_ADDR_RLOCK(ifp);
852193231Sbms		TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
853193231Sbms			if (ifma->ifma_addr->sa_family != AF_INET ||
854193231Sbms			    ifma->ifma_protospec == NULL)
855193231Sbms				continue;
856193231Sbms			inm = (struct in_multi *)ifma->ifma_protospec;
857193231Sbms			igmp_v2_update_group(inm, timer);
858193231Sbms		}
859229621Sjhb		IF_ADDR_RUNLOCK(ifp);
860193231Sbms	} else {
861193231Sbms		/*
862193231Sbms		 * Group-specific IGMPv2 query, we need only
863189592Sbms		 * look up the single group to process it.
864189592Sbms		 */
865189592Sbms		inm = inm_lookup(ifp, igmp->igmp_group);
866189592Sbms		if (inm != NULL) {
867189592Sbms			CTR3(KTR_IGMPV3, "process v2 query %s on ifp %p(%s)",
868189592Sbms			    inet_ntoa(igmp->igmp_group), ifp, ifp->if_xname);
869189592Sbms			igmp_v2_update_group(inm, timer);
870189592Sbms		}
871189592Sbms	}
872189592Sbms
873189592Sbmsout_locked:
874189592Sbms	IGMP_UNLOCK();
875189592Sbms	IN_MULTI_UNLOCK();
876189592Sbms
877189592Sbms	return (0);
8782531Swollman}
8792531Swollman
880189592Sbms/*
881189592Sbms * Update the report timer on a group in response to an IGMPv2 query.
882189592Sbms *
883189592Sbms * If we are becoming the reporting member for this group, start the timer.
884189592Sbms * If we already are the reporting member for this group, and timer is
885189592Sbms * below the threshold, reset it.
886189592Sbms *
887189592Sbms * We may be updating the group for the first time since we switched
888189592Sbms * to IGMPv3. If we are, then we must clear any recorded source lists,
889189592Sbms * and transition to REPORTING state; the group timer is overloaded
890189592Sbms * for group and group-source query responses.
891189592Sbms *
892189592Sbms * Unlike IGMPv3, the delay per group should be jittered
893189592Sbms * to avoid bursts of IGMPv2 reports.
894189592Sbms */
895189592Sbmsstatic void
896189592Sbmsigmp_v2_update_group(struct in_multi *inm, const int timer)
897189592Sbms{
898189592Sbms
899189592Sbms	CTR4(KTR_IGMPV3, "%s: %s/%s timer=%d", __func__,
900189592Sbms	    inet_ntoa(inm->inm_addr), inm->inm_ifp->if_xname, timer);
901189592Sbms
902189592Sbms	IN_MULTI_LOCK_ASSERT();
903189592Sbms
904189592Sbms	switch (inm->inm_state) {
905189592Sbms	case IGMP_NOT_MEMBER:
906189592Sbms	case IGMP_SILENT_MEMBER:
907189592Sbms		break;
908189592Sbms	case IGMP_REPORTING_MEMBER:
909189592Sbms		if (inm->inm_timer != 0 &&
910189592Sbms		    inm->inm_timer <= timer) {
911189592Sbms			CTR1(KTR_IGMPV3, "%s: REPORTING and timer running, "
912189592Sbms			    "skipping.", __func__);
913189592Sbms			break;
914189592Sbms		}
915189592Sbms		/* FALLTHROUGH */
916189592Sbms	case IGMP_SG_QUERY_PENDING_MEMBER:
917189592Sbms	case IGMP_G_QUERY_PENDING_MEMBER:
918189592Sbms	case IGMP_IDLE_MEMBER:
919189592Sbms	case IGMP_LAZY_MEMBER:
920189592Sbms	case IGMP_AWAKENING_MEMBER:
921189592Sbms		CTR1(KTR_IGMPV3, "%s: ->REPORTING", __func__);
922189592Sbms		inm->inm_state = IGMP_REPORTING_MEMBER;
923189592Sbms		inm->inm_timer = IGMP_RANDOM_DELAY(timer);
924189592Sbms		V_current_state_timers_running = 1;
925189592Sbms		break;
926189592Sbms	case IGMP_SLEEPING_MEMBER:
927189592Sbms		CTR1(KTR_IGMPV3, "%s: ->AWAKENING", __func__);
928189592Sbms		inm->inm_state = IGMP_AWAKENING_MEMBER;
929189592Sbms		break;
930189592Sbms	case IGMP_LEAVING_MEMBER:
931189592Sbms		break;
932189592Sbms	}
933189592Sbms}
934189592Sbms
935189592Sbms/*
936189592Sbms * Process a received IGMPv3 general, group-specific or
937189592Sbms * group-and-source-specific query.
938189592Sbms * Assumes m has already been pulled up to the full IGMP message length.
939189592Sbms * Return 0 if successful, otherwise an appropriate error code is returned.
940189592Sbms */
941189592Sbmsstatic int
942189592Sbmsigmp_input_v3_query(struct ifnet *ifp, const struct ip *ip,
943189592Sbms    /*const*/ struct igmpv3 *igmpv3)
944189592Sbms{
945189592Sbms	struct igmp_ifinfo	*igi;
946189592Sbms	struct in_multi		*inm;
947193231Sbms	int			 is_general_query;
948189592Sbms	uint32_t		 maxresp, nsrc, qqi;
949189592Sbms	uint16_t		 timer;
950189592Sbms	uint8_t			 qrv;
951189592Sbms
952193231Sbms	is_general_query = 0;
953193231Sbms
954189592Sbms	CTR2(KTR_IGMPV3, "process v3 query on ifp %p(%s)", ifp, ifp->if_xname);
955189592Sbms
956189592Sbms	maxresp = igmpv3->igmp_code;	/* in 1/10ths of a second */
957189592Sbms	if (maxresp >= 128) {
958189592Sbms		maxresp = IGMP_MANT(igmpv3->igmp_code) <<
959189592Sbms			  (IGMP_EXP(igmpv3->igmp_code) + 3);
960189592Sbms	}
961189592Sbms
962189592Sbms	/*
963189592Sbms	 * Robustness must never be less than 2 for on-wire IGMPv3.
964193231Sbms	 * FUTURE: Check if ifp has IGIF_LOOPBACK set, as we will make
965189592Sbms	 * an exception for interfaces whose IGMPv3 state changes
966189592Sbms	 * are redirected to loopback (e.g. MANET).
967189592Sbms	 */
968189592Sbms	qrv = IGMP_QRV(igmpv3->igmp_misc);
969189592Sbms	if (qrv < 2) {
970189592Sbms		CTR3(KTR_IGMPV3, "%s: clamping qrv %d to %d", __func__,
971189592Sbms		    qrv, IGMP_RV_INIT);
972189592Sbms		qrv = IGMP_RV_INIT;
973189592Sbms	}
974189592Sbms
975189592Sbms	qqi = igmpv3->igmp_qqi;
976189592Sbms	if (qqi >= 128) {
977190691Sbms		qqi = IGMP_MANT(igmpv3->igmp_qqi) <<
978190691Sbms		     (IGMP_EXP(igmpv3->igmp_qqi) + 3);
979189592Sbms	}
980189592Sbms
981189592Sbms	timer = maxresp * PR_FASTHZ / IGMP_TIMER_SCALE;
982189592Sbms	if (timer == 0)
983189592Sbms		timer = 1;
984189592Sbms
985189592Sbms	nsrc = ntohs(igmpv3->igmp_numsrc);
986189592Sbms
987193231Sbms	/*
988193231Sbms	 * Validate address fields and versions upfront before
989193231Sbms	 * accepting v3 query.
990193231Sbms	 * XXX SMPng: Unlocked access to igmpstat counters here.
991193231Sbms	 */
992193231Sbms	if (in_nullhost(igmpv3->igmp_group)) {
993193231Sbms		/*
994193231Sbms		 * IGMPv3 General Query.
995193231Sbms		 *
996193231Sbms		 * General Queries SHOULD be directed to 224.0.0.1.
997193231Sbms		 * A general query with a source list has undefined
998193231Sbms		 * behaviour; discard it.
999193231Sbms		 */
1000193231Sbms		IGMPSTAT_INC(igps_rcv_gen_queries);
1001193231Sbms		if (!in_allhosts(ip->ip_dst) || nsrc > 0) {
1002193231Sbms			IGMPSTAT_INC(igps_rcv_badqueries);
1003193231Sbms			return (0);
1004193231Sbms		}
1005193231Sbms		is_general_query = 1;
1006193231Sbms	} else {
1007193231Sbms		/* Group or group-source specific query. */
1008193231Sbms		if (nsrc == 0)
1009193231Sbms			IGMPSTAT_INC(igps_rcv_group_queries);
1010193231Sbms		else
1011193231Sbms			IGMPSTAT_INC(igps_rcv_gsr_queries);
1012193231Sbms	}
1013193231Sbms
1014189592Sbms	IN_MULTI_LOCK();
1015189592Sbms	IGMP_LOCK();
1016189592Sbms
1017189592Sbms	igi = ((struct in_ifinfo *)ifp->if_afdata[AF_INET])->ii_igmp;
1018189592Sbms	KASSERT(igi != NULL, ("%s: no igmp_ifinfo for ifp %p", __func__, ifp));
1019189592Sbms
1020189592Sbms	if (igi->igi_flags & IGIF_LOOPBACK) {
1021189592Sbms		CTR2(KTR_IGMPV3, "ignore v3 query on IGIF_LOOPBACK ifp %p(%s)",
1022189592Sbms		    ifp, ifp->if_xname);
1023189592Sbms		goto out_locked;
1024189592Sbms	}
1025189592Sbms
1026193231Sbms	/*
1027193231Sbms	 * Discard the v3 query if we're in Compatibility Mode.
1028193231Sbms	 * The RFC is not obviously worded that hosts need to stay in
1029193231Sbms	 * compatibility mode until the Old Version Querier Present
1030193231Sbms	 * timer expires.
1031193231Sbms	 */
1032193231Sbms	if (igi->igi_version != IGMP_VERSION_3) {
1033193231Sbms		CTR3(KTR_IGMPV3, "ignore v3 query in v%d mode on ifp %p(%s)",
1034193231Sbms		    igi->igi_version, ifp, ifp->if_xname);
1035193231Sbms		goto out_locked;
1036193231Sbms	}
1037193231Sbms
1038189592Sbms	igmp_set_version(igi, IGMP_VERSION_3);
1039189592Sbms	igi->igi_rv = qrv;
1040189592Sbms	igi->igi_qi = qqi;
1041189592Sbms	igi->igi_qri = maxresp;
1042189592Sbms
1043189592Sbms	CTR4(KTR_IGMPV3, "%s: qrv %d qi %d qri %d", __func__, qrv, qqi,
1044189592Sbms	    maxresp);
1045189592Sbms
1046193231Sbms	if (is_general_query) {
1047189592Sbms		/*
1048189592Sbms		 * Schedule a current-state report on this ifp for
1049189592Sbms		 * all groups, possibly containing source lists.
1050189592Sbms		 * If there is a pending General Query response
1051189592Sbms		 * scheduled earlier than the selected delay, do
1052189592Sbms		 * not schedule any other reports.
1053189592Sbms		 * Otherwise, reset the interface timer.
1054189592Sbms		 */
1055193231Sbms		CTR2(KTR_IGMPV3, "process v3 general query on ifp %p(%s)",
1056193231Sbms		    ifp, ifp->if_xname);
1057189592Sbms		if (igi->igi_v3_timer == 0 || igi->igi_v3_timer >= timer) {
1058189592Sbms			igi->igi_v3_timer = IGMP_RANDOM_DELAY(timer);
1059189592Sbms			V_interface_timers_running = 1;
1060189592Sbms		}
1061189592Sbms	} else {
1062189592Sbms		/*
1063189592Sbms		 * Group-source-specific queries are throttled on
1064189592Sbms		 * a per-group basis to defeat denial-of-service attempts.
1065189592Sbms		 * Queries for groups we are not a member of on this
1066189592Sbms		 * link are simply ignored.
1067189592Sbms		 */
1068189592Sbms		inm = inm_lookup(ifp, igmpv3->igmp_group);
1069189592Sbms		if (inm == NULL)
1070189592Sbms			goto out_locked;
1071189592Sbms		if (nsrc > 0) {
1072189592Sbms			if (!ratecheck(&inm->inm_lastgsrtv,
1073189592Sbms			    &V_igmp_gsrdelay)) {
1074189592Sbms				CTR1(KTR_IGMPV3, "%s: GS query throttled.",
1075189592Sbms				    __func__);
1076190965Srwatson				IGMPSTAT_INC(igps_drop_gsr_queries);
1077189592Sbms				goto out_locked;
1078189592Sbms			}
1079189592Sbms		}
1080189592Sbms		CTR3(KTR_IGMPV3, "process v3 %s query on ifp %p(%s)",
1081189592Sbms		     inet_ntoa(igmpv3->igmp_group), ifp, ifp->if_xname);
1082189592Sbms		/*
1083189592Sbms		 * If there is a pending General Query response
1084189592Sbms		 * scheduled sooner than the selected delay, no
1085189592Sbms		 * further report need be scheduled.
1086189592Sbms		 * Otherwise, prepare to respond to the
1087189592Sbms		 * group-specific or group-and-source query.
1088189592Sbms		 */
1089189592Sbms		if (igi->igi_v3_timer == 0 || igi->igi_v3_timer >= timer)
1090189592Sbms			igmp_input_v3_group_query(inm, igi, timer, igmpv3);
1091189592Sbms	}
1092189592Sbms
1093189592Sbmsout_locked:
1094189592Sbms	IGMP_UNLOCK();
1095189592Sbms	IN_MULTI_UNLOCK();
1096189592Sbms
1097189592Sbms	return (0);
1098189592Sbms}
1099189592Sbms
1100189592Sbms/*
1101189592Sbms * Process a recieved IGMPv3 group-specific or group-and-source-specific
1102189592Sbms * query.
1103189592Sbms * Return <0 if any error occured. Currently this is ignored.
1104189592Sbms */
1105189592Sbmsstatic int
1106189592Sbmsigmp_input_v3_group_query(struct in_multi *inm, struct igmp_ifinfo *igi,
1107189592Sbms    int timer, /*const*/ struct igmpv3 *igmpv3)
1108189592Sbms{
1109189592Sbms	int			 retval;
1110189592Sbms	uint16_t		 nsrc;
1111189592Sbms
1112189592Sbms	IN_MULTI_LOCK_ASSERT();
1113189592Sbms	IGMP_LOCK_ASSERT();
1114189592Sbms
1115189592Sbms	retval = 0;
1116189592Sbms
1117189592Sbms	switch (inm->inm_state) {
1118189592Sbms	case IGMP_NOT_MEMBER:
1119189592Sbms	case IGMP_SILENT_MEMBER:
1120189592Sbms	case IGMP_SLEEPING_MEMBER:
1121189592Sbms	case IGMP_LAZY_MEMBER:
1122189592Sbms	case IGMP_AWAKENING_MEMBER:
1123189592Sbms	case IGMP_IDLE_MEMBER:
1124189592Sbms	case IGMP_LEAVING_MEMBER:
1125189592Sbms		return (retval);
1126189592Sbms		break;
1127189592Sbms	case IGMP_REPORTING_MEMBER:
1128189592Sbms	case IGMP_G_QUERY_PENDING_MEMBER:
1129189592Sbms	case IGMP_SG_QUERY_PENDING_MEMBER:
1130189592Sbms		break;
1131189592Sbms	}
1132189592Sbms
1133189592Sbms	nsrc = ntohs(igmpv3->igmp_numsrc);
1134189592Sbms
1135189592Sbms	/*
1136189592Sbms	 * Deal with group-specific queries upfront.
1137189592Sbms	 * If any group query is already pending, purge any recorded
1138189592Sbms	 * source-list state if it exists, and schedule a query response
1139189592Sbms	 * for this group-specific query.
1140189592Sbms	 */
1141189592Sbms	if (nsrc == 0) {
1142189592Sbms		if (inm->inm_state == IGMP_G_QUERY_PENDING_MEMBER ||
1143189592Sbms		    inm->inm_state == IGMP_SG_QUERY_PENDING_MEMBER) {
1144189592Sbms			inm_clear_recorded(inm);
1145189592Sbms			timer = min(inm->inm_timer, timer);
1146189592Sbms		}
1147189592Sbms		inm->inm_state = IGMP_G_QUERY_PENDING_MEMBER;
1148189592Sbms		inm->inm_timer = IGMP_RANDOM_DELAY(timer);
1149189592Sbms		V_current_state_timers_running = 1;
1150189592Sbms		return (retval);
1151189592Sbms	}
1152189592Sbms
1153189592Sbms	/*
1154189592Sbms	 * Deal with the case where a group-and-source-specific query has
1155189592Sbms	 * been received but a group-specific query is already pending.
1156189592Sbms	 */
1157189592Sbms	if (inm->inm_state == IGMP_G_QUERY_PENDING_MEMBER) {
1158189592Sbms		timer = min(inm->inm_timer, timer);
1159189592Sbms		inm->inm_timer = IGMP_RANDOM_DELAY(timer);
1160189592Sbms		V_current_state_timers_running = 1;
1161189592Sbms		return (retval);
1162189592Sbms	}
1163189592Sbms
1164189592Sbms	/*
1165189592Sbms	 * Finally, deal with the case where a group-and-source-specific
1166189592Sbms	 * query has been received, where a response to a previous g-s-r
1167189592Sbms	 * query exists, or none exists.
1168189592Sbms	 * In this case, we need to parse the source-list which the Querier
1169189592Sbms	 * has provided us with and check if we have any source list filter
1170189592Sbms	 * entries at T1 for these sources. If we do not, there is no need
1171189592Sbms	 * schedule a report and the query may be dropped.
1172189592Sbms	 * If we do, we must record them and schedule a current-state
1173189592Sbms	 * report for those sources.
1174189592Sbms	 * FIXME: Handling source lists larger than 1 mbuf requires that
1175189592Sbms	 * we pass the mbuf chain pointer down to this function, and use
1176189592Sbms	 * m_getptr() to walk the chain.
1177189592Sbms	 */
1178189592Sbms	if (inm->inm_nsrc > 0) {
1179189592Sbms		const struct in_addr	*ap;
1180189592Sbms		int			 i, nrecorded;
1181189592Sbms
1182189592Sbms		ap = (const struct in_addr *)(igmpv3 + 1);
1183189592Sbms		nrecorded = 0;
1184189592Sbms		for (i = 0; i < nsrc; i++, ap++) {
1185189592Sbms			retval = inm_record_source(inm, ap->s_addr);
1186189592Sbms			if (retval < 0)
1187189592Sbms				break;
1188189592Sbms			nrecorded += retval;
1189189592Sbms		}
1190189592Sbms		if (nrecorded > 0) {
1191189592Sbms			CTR1(KTR_IGMPV3,
1192189592Sbms			    "%s: schedule response to SG query", __func__);
1193189592Sbms			inm->inm_state = IGMP_SG_QUERY_PENDING_MEMBER;
1194189592Sbms			inm->inm_timer = IGMP_RANDOM_DELAY(timer);
1195189592Sbms			V_current_state_timers_running = 1;
1196189592Sbms		}
1197189592Sbms	}
1198189592Sbms
1199189592Sbms	return (retval);
1200189592Sbms}
1201189592Sbms
1202189592Sbms/*
1203189592Sbms * Process a received IGMPv1 host membership report.
1204189592Sbms *
1205189592Sbms * NOTE: 0.0.0.0 workaround breaks const correctness.
1206189592Sbms */
1207189592Sbmsstatic int
1208189592Sbmsigmp_input_v1_report(struct ifnet *ifp, /*const*/ struct ip *ip,
1209189592Sbms    /*const*/ struct igmp *igmp)
1210189592Sbms{
1211189592Sbms	struct in_ifaddr *ia;
1212189592Sbms	struct in_multi *inm;
1213189592Sbms
1214190965Srwatson	IGMPSTAT_INC(igps_rcv_reports);
1215189592Sbms
1216189592Sbms	if (ifp->if_flags & IFF_LOOPBACK)
1217189592Sbms		return (0);
1218189592Sbms
1219213325Sbz	if (!IN_MULTICAST(ntohl(igmp->igmp_group.s_addr)) ||
1220213325Sbz	    !in_hosteq(igmp->igmp_group, ip->ip_dst)) {
1221190965Srwatson		IGMPSTAT_INC(igps_rcv_badreports);
1222189592Sbms		return (EINVAL);
1223189592Sbms	}
1224189592Sbms
1225189592Sbms	/*
1226189592Sbms	 * RFC 3376, Section 4.2.13, 9.2, 9.3:
1227189592Sbms	 * Booting clients may use the source address 0.0.0.0. Some
1228189592Sbms	 * IGMP daemons may not know how to use IP_RECVIF to determine
1229189592Sbms	 * the interface upon which this message was received.
1230189592Sbms	 * Replace 0.0.0.0 with the subnet address if told to do so.
1231189592Sbms	 */
1232189592Sbms	if (V_igmp_recvifkludge && in_nullhost(ip->ip_src)) {
1233189592Sbms		IFP_TO_IA(ifp, ia);
1234194760Srwatson		if (ia != NULL) {
1235189592Sbms			ip->ip_src.s_addr = htonl(ia->ia_subnet);
1236194760Srwatson			ifa_free(&ia->ia_ifa);
1237194760Srwatson		}
1238189592Sbms	}
1239189592Sbms
1240189592Sbms	CTR3(KTR_IGMPV3, "process v1 report %s on ifp %p(%s)",
1241189592Sbms	     inet_ntoa(igmp->igmp_group), ifp, ifp->if_xname);
1242189592Sbms
1243189592Sbms	/*
1244189592Sbms	 * IGMPv1 report suppression.
1245189592Sbms	 * If we are a member of this group, and our membership should be
1246189592Sbms	 * reported, stop our group timer and transition to the 'lazy' state.
1247189592Sbms	 */
1248189592Sbms	IN_MULTI_LOCK();
1249189592Sbms	inm = inm_lookup(ifp, igmp->igmp_group);
1250189592Sbms	if (inm != NULL) {
1251189592Sbms		struct igmp_ifinfo *igi;
1252189592Sbms
1253189592Sbms		igi = inm->inm_igi;
1254189592Sbms		if (igi == NULL) {
1255189592Sbms			KASSERT(igi != NULL,
1256189592Sbms			    ("%s: no igi for ifp %p", __func__, ifp));
1257189592Sbms			goto out_locked;
1258189592Sbms		}
1259189592Sbms
1260190965Srwatson		IGMPSTAT_INC(igps_rcv_ourreports);
1261189592Sbms
1262189592Sbms		/*
1263189592Sbms		 * If we are in IGMPv3 host mode, do not allow the
1264189592Sbms		 * other host's IGMPv1 report to suppress our reports
1265189592Sbms		 * unless explicitly configured to do so.
1266189592Sbms		 */
1267189592Sbms		if (igi->igi_version == IGMP_VERSION_3) {
1268189592Sbms			if (V_igmp_legacysupp)
1269189592Sbms				igmp_v3_suppress_group_record(inm);
1270189592Sbms			goto out_locked;
1271189592Sbms		}
1272189592Sbms
1273189592Sbms		inm->inm_timer = 0;
1274189592Sbms
1275189592Sbms		switch (inm->inm_state) {
1276189592Sbms		case IGMP_NOT_MEMBER:
1277189592Sbms		case IGMP_SILENT_MEMBER:
1278189592Sbms			break;
1279189592Sbms		case IGMP_IDLE_MEMBER:
1280189592Sbms		case IGMP_LAZY_MEMBER:
1281189592Sbms		case IGMP_AWAKENING_MEMBER:
1282189592Sbms			CTR3(KTR_IGMPV3,
1283189592Sbms			    "report suppressed for %s on ifp %p(%s)",
1284189592Sbms			    inet_ntoa(igmp->igmp_group), ifp, ifp->if_xname);
1285189592Sbms		case IGMP_SLEEPING_MEMBER:
1286189592Sbms			inm->inm_state = IGMP_SLEEPING_MEMBER;
1287189592Sbms			break;
1288189592Sbms		case IGMP_REPORTING_MEMBER:
1289189592Sbms			CTR3(KTR_IGMPV3,
1290189592Sbms			    "report suppressed for %s on ifp %p(%s)",
1291189592Sbms			    inet_ntoa(igmp->igmp_group), ifp, ifp->if_xname);
1292189592Sbms			if (igi->igi_version == IGMP_VERSION_1)
1293189592Sbms				inm->inm_state = IGMP_LAZY_MEMBER;
1294189592Sbms			else if (igi->igi_version == IGMP_VERSION_2)
1295189592Sbms				inm->inm_state = IGMP_SLEEPING_MEMBER;
1296189592Sbms			break;
1297189592Sbms		case IGMP_G_QUERY_PENDING_MEMBER:
1298189592Sbms		case IGMP_SG_QUERY_PENDING_MEMBER:
1299189592Sbms		case IGMP_LEAVING_MEMBER:
1300189592Sbms			break;
1301189592Sbms		}
1302189592Sbms	}
1303189592Sbms
1304189592Sbmsout_locked:
1305189592Sbms	IN_MULTI_UNLOCK();
1306189592Sbms
1307189592Sbms	return (0);
1308189592Sbms}
1309189592Sbms
1310189592Sbms/*
1311189592Sbms * Process a received IGMPv2 host membership report.
1312189592Sbms *
1313189592Sbms * NOTE: 0.0.0.0 workaround breaks const correctness.
1314189592Sbms */
1315189592Sbmsstatic int
1316189592Sbmsigmp_input_v2_report(struct ifnet *ifp, /*const*/ struct ip *ip,
1317189592Sbms    /*const*/ struct igmp *igmp)
1318189592Sbms{
1319189592Sbms	struct in_ifaddr *ia;
1320189592Sbms	struct in_multi *inm;
1321189592Sbms
1322189592Sbms	/*
1323189592Sbms	 * Make sure we don't hear our own membership report.  Fast
1324189592Sbms	 * leave requires knowing that we are the only member of a
1325189592Sbms	 * group.
1326189592Sbms	 */
1327189592Sbms	IFP_TO_IA(ifp, ia);
1328194760Srwatson	if (ia != NULL && in_hosteq(ip->ip_src, IA_SIN(ia)->sin_addr)) {
1329194760Srwatson		ifa_free(&ia->ia_ifa);
1330189592Sbms		return (0);
1331194760Srwatson	}
1332189592Sbms
1333190965Srwatson	IGMPSTAT_INC(igps_rcv_reports);
1334189592Sbms
1335194760Srwatson	if (ifp->if_flags & IFF_LOOPBACK) {
1336194760Srwatson		if (ia != NULL)
1337194760Srwatson			ifa_free(&ia->ia_ifa);
1338189592Sbms		return (0);
1339194760Srwatson	}
1340189592Sbms
1341189592Sbms	if (!IN_MULTICAST(ntohl(igmp->igmp_group.s_addr)) ||
1342189592Sbms	    !in_hosteq(igmp->igmp_group, ip->ip_dst)) {
1343194760Srwatson		if (ia != NULL)
1344194760Srwatson			ifa_free(&ia->ia_ifa);
1345190965Srwatson		IGMPSTAT_INC(igps_rcv_badreports);
1346189592Sbms		return (EINVAL);
1347189592Sbms	}
1348189592Sbms
1349189592Sbms	/*
1350189592Sbms	 * RFC 3376, Section 4.2.13, 9.2, 9.3:
1351189592Sbms	 * Booting clients may use the source address 0.0.0.0. Some
1352189592Sbms	 * IGMP daemons may not know how to use IP_RECVIF to determine
1353189592Sbms	 * the interface upon which this message was received.
1354189592Sbms	 * Replace 0.0.0.0 with the subnet address if told to do so.
1355189592Sbms	 */
1356189592Sbms	if (V_igmp_recvifkludge && in_nullhost(ip->ip_src)) {
1357189592Sbms		if (ia != NULL)
1358189592Sbms			ip->ip_src.s_addr = htonl(ia->ia_subnet);
1359189592Sbms	}
1360194760Srwatson	if (ia != NULL)
1361194760Srwatson		ifa_free(&ia->ia_ifa);
1362189592Sbms
1363189592Sbms	CTR3(KTR_IGMPV3, "process v2 report %s on ifp %p(%s)",
1364189592Sbms	     inet_ntoa(igmp->igmp_group), ifp, ifp->if_xname);
1365189592Sbms
1366189592Sbms	/*
1367189592Sbms	 * IGMPv2 report suppression.
1368189592Sbms	 * If we are a member of this group, and our membership should be
1369189592Sbms	 * reported, and our group timer is pending or about to be reset,
1370189592Sbms	 * stop our group timer by transitioning to the 'lazy' state.
1371189592Sbms	 */
1372189592Sbms	IN_MULTI_LOCK();
1373189592Sbms	inm = inm_lookup(ifp, igmp->igmp_group);
1374189592Sbms	if (inm != NULL) {
1375189592Sbms		struct igmp_ifinfo *igi;
1376189592Sbms
1377189592Sbms		igi = inm->inm_igi;
1378189592Sbms		KASSERT(igi != NULL, ("%s: no igi for ifp %p", __func__, ifp));
1379189592Sbms
1380190965Srwatson		IGMPSTAT_INC(igps_rcv_ourreports);
1381189592Sbms
1382189592Sbms		/*
1383189592Sbms		 * If we are in IGMPv3 host mode, do not allow the
1384189592Sbms		 * other host's IGMPv1 report to suppress our reports
1385189592Sbms		 * unless explicitly configured to do so.
1386189592Sbms		 */
1387189592Sbms		if (igi->igi_version == IGMP_VERSION_3) {
1388189592Sbms			if (V_igmp_legacysupp)
1389189592Sbms				igmp_v3_suppress_group_record(inm);
1390189592Sbms			goto out_locked;
1391189592Sbms		}
1392189592Sbms
1393189592Sbms		inm->inm_timer = 0;
1394189592Sbms
1395189592Sbms		switch (inm->inm_state) {
1396189592Sbms		case IGMP_NOT_MEMBER:
1397189592Sbms		case IGMP_SILENT_MEMBER:
1398189592Sbms		case IGMP_SLEEPING_MEMBER:
1399189592Sbms			break;
1400189592Sbms		case IGMP_REPORTING_MEMBER:
1401189592Sbms		case IGMP_IDLE_MEMBER:
1402189592Sbms		case IGMP_AWAKENING_MEMBER:
1403189592Sbms			CTR3(KTR_IGMPV3,
1404189592Sbms			    "report suppressed for %s on ifp %p(%s)",
1405189592Sbms			    inet_ntoa(igmp->igmp_group), ifp, ifp->if_xname);
1406189592Sbms		case IGMP_LAZY_MEMBER:
1407189592Sbms			inm->inm_state = IGMP_LAZY_MEMBER;
1408189592Sbms			break;
1409189592Sbms		case IGMP_G_QUERY_PENDING_MEMBER:
1410189592Sbms		case IGMP_SG_QUERY_PENDING_MEMBER:
1411189592Sbms		case IGMP_LEAVING_MEMBER:
1412189592Sbms			break;
1413189592Sbms		}
1414189592Sbms	}
1415189592Sbms
1416189592Sbmsout_locked:
1417189592Sbms	IN_MULTI_UNLOCK();
1418189592Sbms
1419189592Sbms	return (0);
1420189592Sbms}
1421189592Sbms
14221541Srgrimesvoid
1423189592Sbmsigmp_input(struct mbuf *m, int off)
14241541Srgrimes{
1425189592Sbms	int iphlen;
1426189592Sbms	struct ifnet *ifp;
1427189592Sbms	struct igmp *igmp;
1428189592Sbms	struct ip *ip;
1429189592Sbms	int igmplen;
1430189592Sbms	int minlen;
1431189592Sbms	int queryver;
1432189592Sbms
1433189592Sbms	CTR3(KTR_IGMPV3, "%s: called w/mbuf (%p,%d)", __func__, m, off);
1434189592Sbms
1435189592Sbms	ifp = m->m_pkthdr.rcvif;
14361541Srgrimes
1437190965Srwatson	IGMPSTAT_INC(igps_rcv_total);
14381541Srgrimes
14391541Srgrimes	ip = mtod(m, struct ip *);
1440189592Sbms	iphlen = off;
1441241923Sglebius	igmplen = ntohs(ip->ip_len) - off;
14421541Srgrimes
14431541Srgrimes	/*
1444164863Srwatson	 * Validate lengths.
14451541Srgrimes	 */
14461541Srgrimes	if (igmplen < IGMP_MINLEN) {
1447190965Srwatson		IGMPSTAT_INC(igps_rcv_tooshort);
14481541Srgrimes		m_freem(m);
14491541Srgrimes		return;
14501541Srgrimes	}
1451189592Sbms
1452189592Sbms	/*
1453189592Sbms	 * Always pullup to the minimum size for v1/v2 or v3
1454189592Sbms	 * to amortize calls to m_pullup().
1455189592Sbms	 */
1456189592Sbms	minlen = iphlen;
1457189592Sbms	if (igmplen >= IGMP_V3_QUERY_MINLEN)
1458189592Sbms		minlen += IGMP_V3_QUERY_MINLEN;
1459189592Sbms	else
1460189592Sbms		minlen += IGMP_MINLEN;
14611541Srgrimes	if ((m->m_flags & M_EXT || m->m_len < minlen) &&
14621541Srgrimes	    (m = m_pullup(m, minlen)) == 0) {
1463190965Srwatson		IGMPSTAT_INC(igps_rcv_tooshort);
14641541Srgrimes		return;
14651541Srgrimes	}
1466189592Sbms	ip = mtod(m, struct ip *);
14671541Srgrimes
14681541Srgrimes	/*
1469164863Srwatson	 * Validate checksum.
14701541Srgrimes	 */
14711541Srgrimes	m->m_data += iphlen;
14721541Srgrimes	m->m_len -= iphlen;
14731541Srgrimes	igmp = mtod(m, struct igmp *);
14741541Srgrimes	if (in_cksum(m, igmplen)) {
1475190965Srwatson		IGMPSTAT_INC(igps_rcv_badsum);
14761541Srgrimes		m_freem(m);
14771541Srgrimes		return;
14781541Srgrimes	}
14791541Srgrimes	m->m_data -= iphlen;
14801541Srgrimes	m->m_len += iphlen;
14812531Swollman
1482207275Sbms	/*
1483207275Sbms	 * IGMP control traffic is link-scope, and must have a TTL of 1.
1484207275Sbms	 * DVMRP traffic (e.g. mrinfo, mtrace) is an exception;
1485207275Sbms	 * probe packets may come from beyond the LAN.
1486207275Sbms	 */
1487207275Sbms	if (igmp->igmp_type != IGMP_DVMRP && ip->ip_ttl != 1) {
1488207275Sbms		IGMPSTAT_INC(igps_rcv_badttl);
1489207275Sbms		m_freem(m);
1490207275Sbms		return;
1491207275Sbms	}
1492207275Sbms
14931541Srgrimes	switch (igmp->igmp_type) {
1494189592Sbms	case IGMP_HOST_MEMBERSHIP_QUERY:
1495189592Sbms		if (igmplen == IGMP_MINLEN) {
1496189592Sbms			if (igmp->igmp_code == 0)
1497189592Sbms				queryver = IGMP_VERSION_1;
1498189592Sbms			else
1499189592Sbms				queryver = IGMP_VERSION_2;
1500189592Sbms		} else if (igmplen >= IGMP_V3_QUERY_MINLEN) {
1501189592Sbms			queryver = IGMP_VERSION_3;
1502189592Sbms		} else {
1503190965Srwatson			IGMPSTAT_INC(igps_rcv_tooshort);
1504189592Sbms			m_freem(m);
1505189592Sbms			return;
1506189592Sbms		}
15071541Srgrimes
1508189592Sbms		switch (queryver) {
1509189592Sbms		case IGMP_VERSION_1:
1510190965Srwatson			IGMPSTAT_INC(igps_rcv_v1v2_queries);
1511189592Sbms			if (!V_igmp_v1enable)
1512189592Sbms				break;
1513193231Sbms			if (igmp_input_v1_query(ifp, ip, igmp) != 0) {
1514144163Ssam				m_freem(m);
1515144163Ssam				return;
1516144163Ssam			}
1517189592Sbms			break;
15184028Spst
1519189592Sbms		case IGMP_VERSION_2:
1520190965Srwatson			IGMPSTAT_INC(igps_rcv_v1v2_queries);
1521189592Sbms			if (!V_igmp_v2enable)
1522189592Sbms				break;
1523189592Sbms			if (igmp_input_v2_query(ifp, ip, igmp) != 0) {
15242531Swollman				m_freem(m);
15252531Swollman				return;
15262531Swollman			}
1527189592Sbms			break;
15282531Swollman
1529189592Sbms		case IGMP_VERSION_3: {
1530189592Sbms				struct igmpv3 *igmpv3;
1531189592Sbms				uint16_t igmpv3len;
1532279263Sdelphij				uint16_t nsrc;
1533189592Sbms
1534190965Srwatson				IGMPSTAT_INC(igps_rcv_v3_queries);
1535189592Sbms				igmpv3 = (struct igmpv3 *)igmp;
1536189592Sbms				/*
1537189592Sbms				 * Validate length based on source count.
1538189592Sbms				 */
1539189592Sbms				nsrc = ntohs(igmpv3->igmp_numsrc);
1540281230Sdelphij				if (nsrc * sizeof(in_addr_t) >
1541281230Sdelphij				    UINT16_MAX - iphlen - IGMP_V3_QUERY_MINLEN) {
1542190965Srwatson					IGMPSTAT_INC(igps_rcv_tooshort);
1543189592Sbms					return;
15442531Swollman				}
1545189592Sbms				/*
1546189592Sbms				 * m_pullup() may modify m, so pullup in
1547189592Sbms				 * this scope.
1548189592Sbms				 */
1549189592Sbms				igmpv3len = iphlen + IGMP_V3_QUERY_MINLEN +
1550281230Sdelphij				    sizeof(struct in_addr) * nsrc;
1551189592Sbms				if ((m->m_flags & M_EXT ||
1552189592Sbms				     m->m_len < igmpv3len) &&
1553189592Sbms				    (m = m_pullup(m, igmpv3len)) == NULL) {
1554190965Srwatson					IGMPSTAT_INC(igps_rcv_tooshort);
1555189592Sbms					return;
1556189592Sbms				}
1557189592Sbms				igmpv3 = (struct igmpv3 *)(mtod(m, uint8_t *)
1558189592Sbms				    + iphlen);
1559189592Sbms				if (igmp_input_v3_query(ifp, ip, igmpv3) != 0) {
1560189592Sbms					m_freem(m);
1561189592Sbms					return;
1562189592Sbms				}
15631541Srgrimes			}
1564189592Sbms			break;
15651541Srgrimes		}
15661541Srgrimes		break;
15671541Srgrimes
1568189592Sbms	case IGMP_v1_HOST_MEMBERSHIP_REPORT:
1569189592Sbms		if (!V_igmp_v1enable)
157014622Sfenner			break;
1571189592Sbms		if (igmp_input_v1_report(ifp, ip, igmp) != 0) {
1572189592Sbms			m_freem(m);
1573189592Sbms			return;
1574189592Sbms		}
1575189592Sbms		break;
157614622Sfenner
1577189592Sbms	case IGMP_v2_HOST_MEMBERSHIP_REPORT:
1578189592Sbms		if (!V_igmp_v2enable)
15791541Srgrimes			break;
1580189592Sbms		if (!ip_checkrouteralert(m))
1581190965Srwatson			IGMPSTAT_INC(igps_rcv_nora);
1582189592Sbms		if (igmp_input_v2_report(ifp, ip, igmp) != 0) {
15831541Srgrimes			m_freem(m);
15841541Srgrimes			return;
15851541Srgrimes		}
1586189592Sbms		break;
15871541Srgrimes
1588189592Sbms	case IGMP_v3_HOST_MEMBERSHIP_REPORT:
15891541Srgrimes		/*
1590189592Sbms		 * Hosts do not need to process IGMPv3 membership reports,
1591189592Sbms		 * as report suppression is no longer required.
15921541Srgrimes		 */
1593189592Sbms		if (!ip_checkrouteralert(m))
1594190965Srwatson			IGMPSTAT_INC(igps_rcv_nora);
1595189592Sbms		break;
15961541Srgrimes
1597189592Sbms	default:
15981541Srgrimes		break;
15991541Srgrimes	}
16001541Srgrimes
16011541Srgrimes	/*
1602164863Srwatson	 * Pass all valid IGMP packets up to any process(es) listening on a
1603164863Srwatson	 * raw IGMP socket.
16041541Srgrimes	 */
160582890Sjulian	rip_input(m, off);
16061541Srgrimes}
16071541Srgrimes
1608189592Sbms
1609189592Sbms/*
1610189592Sbms * Fast timeout handler (global).
1611189592Sbms * VIMAGE: Timeout handlers are expected to service all vimages.
1612189592Sbms */
16131541Srgrimesvoid
1614189592Sbmsigmp_fasttimo(void)
16151541Srgrimes{
1616189592Sbms	VNET_ITERATOR_DECL(vnet_iter);
16171541Srgrimes
1618195760Srwatson	VNET_LIST_RLOCK_NOSLEEP();
1619189592Sbms	VNET_FOREACH(vnet_iter) {
1620189592Sbms		CURVNET_SET(vnet_iter);
1621189592Sbms		igmp_fasttimo_vnet();
1622189592Sbms		CURVNET_RESTORE();
1623189592Sbms	}
1624195760Srwatson	VNET_LIST_RUNLOCK_NOSLEEP();
1625189592Sbms}
1626189592Sbms
1627189592Sbms/*
1628189592Sbms * Fast timeout handler (per-vnet).
1629189592Sbms * Sends are shuffled off to a netisr to deal with Giant.
1630189592Sbms *
1631189592Sbms * VIMAGE: Assume caller has set up our curvnet.
1632189592Sbms */
1633189592Sbmsstatic void
1634189592Sbmsigmp_fasttimo_vnet(void)
1635189592Sbms{
1636189592Sbms	struct ifqueue		 scq;	/* State-change packets */
1637189592Sbms	struct ifqueue		 qrq;	/* Query response packets */
1638189592Sbms	struct ifnet		*ifp;
1639189592Sbms	struct igmp_ifinfo	*igi;
1640229390Sjhb	struct ifmultiaddr	*ifma;
1641189592Sbms	struct in_multi		*inm;
1642189592Sbms	int			 loop, uri_fasthz;
1643189592Sbms
1644189592Sbms	loop = 0;
1645189592Sbms	uri_fasthz = 0;
1646189592Sbms
1647189592Sbms	/*
1648189592Sbms	 * Quick check to see if any work needs to be done, in order to
1649189592Sbms	 * minimize the overhead of fasttimo processing.
1650189592Sbms	 * SMPng: XXX Unlocked reads.
1651189592Sbms	 */
1652189592Sbms	if (!V_current_state_timers_running &&
1653189592Sbms	    !V_interface_timers_running &&
1654189592Sbms	    !V_state_change_timers_running)
1655189592Sbms		return;
1656189592Sbms
1657189592Sbms	IN_MULTI_LOCK();
1658189592Sbms	IGMP_LOCK();
1659189592Sbms
1660189592Sbms	/*
1661189592Sbms	 * IGMPv3 General Query response timer processing.
1662189592Sbms	 */
1663189592Sbms	if (V_interface_timers_running) {
1664189592Sbms		CTR1(KTR_IGMPV3, "%s: interface timers running", __func__);
1665189592Sbms
1666189592Sbms		V_interface_timers_running = 0;
1667189592Sbms		LIST_FOREACH(igi, &V_igi_head, igi_link) {
1668189592Sbms			if (igi->igi_v3_timer == 0) {
1669189592Sbms				/* Do nothing. */
1670189592Sbms			} else if (--igi->igi_v3_timer == 0) {
1671189592Sbms				igmp_v3_dispatch_general_query(igi);
1672189592Sbms			} else {
1673189592Sbms				V_interface_timers_running = 1;
1674189592Sbms			}
1675189592Sbms		}
1676189592Sbms	}
1677189592Sbms
1678189592Sbms	if (!V_current_state_timers_running &&
1679189592Sbms	    !V_state_change_timers_running)
1680189592Sbms		goto out_locked;
1681189592Sbms
1682189592Sbms	V_current_state_timers_running = 0;
1683189592Sbms	V_state_change_timers_running = 0;
1684189592Sbms
1685189592Sbms	CTR1(KTR_IGMPV3, "%s: state change timers running", __func__);
1686189592Sbms
1687189592Sbms	/*
1688189592Sbms	 * IGMPv1/v2/v3 host report and state-change timer processing.
1689189592Sbms	 * Note: Processing a v3 group timer may remove a node.
1690189592Sbms	 */
1691189592Sbms	LIST_FOREACH(igi, &V_igi_head, igi_link) {
1692189592Sbms		ifp = igi->igi_ifp;
1693189592Sbms
1694189592Sbms		if (igi->igi_version == IGMP_VERSION_3) {
1695189592Sbms			loop = (igi->igi_flags & IGIF_LOOPBACK) ? 1 : 0;
1696189592Sbms			uri_fasthz = IGMP_RANDOM_DELAY(igi->igi_uri *
1697189592Sbms			    PR_FASTHZ);
1698189592Sbms
1699189592Sbms			memset(&qrq, 0, sizeof(struct ifqueue));
1700189592Sbms			IFQ_SET_MAXLEN(&qrq, IGMP_MAX_G_GS_PACKETS);
1701189592Sbms
1702189592Sbms			memset(&scq, 0, sizeof(struct ifqueue));
1703189592Sbms			IFQ_SET_MAXLEN(&scq, IGMP_MAX_STATE_CHANGE_PACKETS);
1704189592Sbms		}
1705189592Sbms
1706229621Sjhb		IF_ADDR_RLOCK(ifp);
1707229390Sjhb		TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
1708189931Sbms			if (ifma->ifma_addr->sa_family != AF_INET ||
1709189931Sbms			    ifma->ifma_protospec == NULL)
1710189592Sbms				continue;
1711189592Sbms			inm = (struct in_multi *)ifma->ifma_protospec;
1712189592Sbms			switch (igi->igi_version) {
1713189592Sbms			case IGMP_VERSION_1:
1714189592Sbms			case IGMP_VERSION_2:
1715189592Sbms				igmp_v1v2_process_group_timer(inm,
1716189592Sbms				    igi->igi_version);
1717189592Sbms				break;
1718189592Sbms			case IGMP_VERSION_3:
1719189592Sbms				igmp_v3_process_group_timers(igi, &qrq,
1720189592Sbms				    &scq, inm, uri_fasthz);
1721189592Sbms				break;
1722189592Sbms			}
1723189592Sbms		}
1724229621Sjhb		IF_ADDR_RUNLOCK(ifp);
1725189592Sbms
1726189592Sbms		if (igi->igi_version == IGMP_VERSION_3) {
1727189592Sbms			struct in_multi		*tinm;
1728189592Sbms
1729189592Sbms			igmp_dispatch_queue(&qrq, 0, loop);
1730189592Sbms			igmp_dispatch_queue(&scq, 0, loop);
1731189592Sbms
1732189592Sbms			/*
1733189592Sbms			 * Free the in_multi reference(s) for this
1734189592Sbms			 * IGMP lifecycle.
1735189592Sbms			 */
1736189592Sbms			SLIST_FOREACH_SAFE(inm, &igi->igi_relinmhead,
1737189592Sbms			    inm_nrele, tinm) {
1738189592Sbms				SLIST_REMOVE_HEAD(&igi->igi_relinmhead,
1739189592Sbms				    inm_nrele);
1740189592Sbms				inm_release_locked(inm);
1741189592Sbms			}
1742189592Sbms		}
1743189592Sbms	}
1744189592Sbms
1745189592Sbmsout_locked:
1746189592Sbms	IGMP_UNLOCK();
1747189592Sbms	IN_MULTI_UNLOCK();
1748189592Sbms}
1749189592Sbms
1750189592Sbms/*
1751189592Sbms * Update host report group timer for IGMPv1/v2.
1752189592Sbms * Will update the global pending timer flags.
1753189592Sbms */
1754189592Sbmsstatic void
1755189592Sbmsigmp_v1v2_process_group_timer(struct in_multi *inm, const int version)
1756189592Sbms{
1757189592Sbms	int report_timer_expired;
1758189592Sbms
1759148682Srwatson	IN_MULTI_LOCK_ASSERT();
1760189592Sbms	IGMP_LOCK_ASSERT();
1761148682Srwatson
1762189592Sbms	if (inm->inm_timer == 0) {
1763189592Sbms		report_timer_expired = 0;
1764189592Sbms	} else if (--inm->inm_timer == 0) {
1765189592Sbms		report_timer_expired = 1;
176614622Sfenner	} else {
1767189592Sbms		V_current_state_timers_running = 1;
1768189592Sbms		return;
1769189592Sbms	}
1770189592Sbms
1771189592Sbms	switch (inm->inm_state) {
1772189592Sbms	case IGMP_NOT_MEMBER:
1773189592Sbms	case IGMP_SILENT_MEMBER:
1774189592Sbms	case IGMP_IDLE_MEMBER:
1775189592Sbms	case IGMP_LAZY_MEMBER:
1776189592Sbms	case IGMP_SLEEPING_MEMBER:
1777189592Sbms	case IGMP_AWAKENING_MEMBER:
1778189592Sbms		break;
1779189592Sbms	case IGMP_REPORTING_MEMBER:
1780189592Sbms		if (report_timer_expired) {
1781189592Sbms			inm->inm_state = IGMP_IDLE_MEMBER;
1782189592Sbms			(void)igmp_v1v2_queue_report(inm,
1783189592Sbms			    (version == IGMP_VERSION_2) ?
1784189592Sbms			     IGMP_v2_HOST_MEMBERSHIP_REPORT :
1785189592Sbms			     IGMP_v1_HOST_MEMBERSHIP_REPORT);
1786144163Ssam		}
1787189592Sbms		break;
1788189592Sbms	case IGMP_G_QUERY_PENDING_MEMBER:
1789189592Sbms	case IGMP_SG_QUERY_PENDING_MEMBER:
1790189592Sbms	case IGMP_LEAVING_MEMBER:
1791189592Sbms		break;
17921541Srgrimes	}
17931541Srgrimes}
17941541Srgrimes
1795189592Sbms/*
1796189592Sbms * Update a group's timers for IGMPv3.
1797189592Sbms * Will update the global pending timer flags.
1798189592Sbms * Note: Unlocked read from igi.
1799189592Sbms */
1800189592Sbmsstatic void
1801189592Sbmsigmp_v3_process_group_timers(struct igmp_ifinfo *igi,
1802189592Sbms    struct ifqueue *qrq, struct ifqueue *scq,
1803189592Sbms    struct in_multi *inm, const int uri_fasthz)
18041541Srgrimes{
1805189592Sbms	int query_response_timer_expired;
1806189592Sbms	int state_change_retransmit_timer_expired;
1807119181Srwatson
1808148682Srwatson	IN_MULTI_LOCK_ASSERT();
1809189592Sbms	IGMP_LOCK_ASSERT();
1810148682Srwatson
1811189592Sbms	query_response_timer_expired = 0;
1812189592Sbms	state_change_retransmit_timer_expired = 0;
1813189592Sbms
1814189592Sbms	/*
1815189592Sbms	 * During a transition from v1/v2 compatibility mode back to v3,
1816189592Sbms	 * a group record in REPORTING state may still have its group
1817189592Sbms	 * timer active. This is a no-op in this function; it is easier
1818189592Sbms	 * to deal with it here than to complicate the slow-timeout path.
1819189592Sbms	 */
1820189592Sbms	if (inm->inm_timer == 0) {
1821189592Sbms		query_response_timer_expired = 0;
1822189592Sbms	} else if (--inm->inm_timer == 0) {
1823189592Sbms		query_response_timer_expired = 1;
1824189592Sbms	} else {
1825189592Sbms		V_current_state_timers_running = 1;
1826189592Sbms	}
1827189592Sbms
1828189592Sbms	if (inm->inm_sctimer == 0) {
1829189592Sbms		state_change_retransmit_timer_expired = 0;
1830189592Sbms	} else if (--inm->inm_sctimer == 0) {
1831189592Sbms		state_change_retransmit_timer_expired = 1;
1832189592Sbms	} else {
1833189592Sbms		V_state_change_timers_running = 1;
1834189592Sbms	}
1835189592Sbms
1836189592Sbms	/* We are in fasttimo, so be quick about it. */
1837189592Sbms	if (!state_change_retransmit_timer_expired &&
1838189592Sbms	    !query_response_timer_expired)
1839189592Sbms		return;
1840189592Sbms
1841189592Sbms	switch (inm->inm_state) {
1842189592Sbms	case IGMP_NOT_MEMBER:
1843189592Sbms	case IGMP_SILENT_MEMBER:
1844189592Sbms	case IGMP_SLEEPING_MEMBER:
1845189592Sbms	case IGMP_LAZY_MEMBER:
1846189592Sbms	case IGMP_AWAKENING_MEMBER:
1847189592Sbms	case IGMP_IDLE_MEMBER:
1848189592Sbms		break;
1849189592Sbms	case IGMP_G_QUERY_PENDING_MEMBER:
1850189592Sbms	case IGMP_SG_QUERY_PENDING_MEMBER:
1851189592Sbms		/*
1852189592Sbms		 * Respond to a previously pending Group-Specific
1853189592Sbms		 * or Group-and-Source-Specific query by enqueueing
1854189592Sbms		 * the appropriate Current-State report for
1855189592Sbms		 * immediate transmission.
1856189592Sbms		 */
1857189592Sbms		if (query_response_timer_expired) {
1858189592Sbms			int retval;
1859189592Sbms
1860189592Sbms			retval = igmp_v3_enqueue_group_record(qrq, inm, 0, 1,
1861189592Sbms			    (inm->inm_state == IGMP_SG_QUERY_PENDING_MEMBER));
1862189592Sbms			CTR2(KTR_IGMPV3, "%s: enqueue record = %d",
1863189592Sbms			    __func__, retval);
1864189592Sbms			inm->inm_state = IGMP_REPORTING_MEMBER;
1865189592Sbms			/* XXX Clear recorded sources for next time. */
1866189592Sbms			inm_clear_recorded(inm);
1867189592Sbms		}
1868189592Sbms		/* FALLTHROUGH */
1869189592Sbms	case IGMP_REPORTING_MEMBER:
1870189592Sbms	case IGMP_LEAVING_MEMBER:
1871189592Sbms		if (state_change_retransmit_timer_expired) {
1872189592Sbms			/*
1873189592Sbms			 * State-change retransmission timer fired.
1874189592Sbms			 * If there are any further pending retransmissions,
1875189592Sbms			 * set the global pending state-change flag, and
1876189592Sbms			 * reset the timer.
1877189592Sbms			 */
1878189592Sbms			if (--inm->inm_scrv > 0) {
1879189592Sbms				inm->inm_sctimer = uri_fasthz;
1880189592Sbms				V_state_change_timers_running = 1;
1881189592Sbms			}
1882189592Sbms			/*
1883189592Sbms			 * Retransmit the previously computed state-change
1884189592Sbms			 * report. If there are no further pending
1885189592Sbms			 * retransmissions, the mbuf queue will be consumed.
1886189592Sbms			 * Update T0 state to T1 as we have now sent
1887189592Sbms			 * a state-change.
1888189592Sbms			 */
1889189592Sbms			(void)igmp_v3_merge_state_changes(inm, scq);
1890189592Sbms
1891189592Sbms			inm_commit(inm);
1892189592Sbms			CTR3(KTR_IGMPV3, "%s: T1 -> T0 for %s/%s", __func__,
1893189592Sbms			    inet_ntoa(inm->inm_addr), inm->inm_ifp->if_xname);
1894189592Sbms
1895189592Sbms			/*
1896189592Sbms			 * If we are leaving the group for good, make sure
1897189592Sbms			 * we release IGMP's reference to it.
1898189592Sbms			 * This release must be deferred using a SLIST,
1899189592Sbms			 * as we are called from a loop which traverses
1900189592Sbms			 * the in_ifmultiaddr TAILQ.
1901189592Sbms			 */
1902189592Sbms			if (inm->inm_state == IGMP_LEAVING_MEMBER &&
1903189592Sbms			    inm->inm_scrv == 0) {
1904189592Sbms				inm->inm_state = IGMP_NOT_MEMBER;
1905189592Sbms				SLIST_INSERT_HEAD(&igi->igi_relinmhead,
1906189592Sbms				    inm, inm_nrele);
1907189592Sbms			}
1908189592Sbms		}
1909189592Sbms		break;
1910189592Sbms	}
19111541Srgrimes}
19121541Srgrimes
1913189592Sbms
1914189592Sbms/*
1915189592Sbms * Suppress a group's pending response to a group or source/group query.
1916189592Sbms *
1917189592Sbms * Do NOT suppress state changes. This leads to IGMPv3 inconsistency.
1918189592Sbms * Do NOT update ST1/ST0 as this operation merely suppresses
1919189592Sbms * the currently pending group record.
1920189592Sbms * Do NOT suppress the response to a general query. It is possible but
1921189592Sbms * it would require adding another state or flag.
1922189592Sbms */
1923189592Sbmsstatic void
1924189592Sbmsigmp_v3_suppress_group_record(struct in_multi *inm)
19251541Srgrimes{
19261541Srgrimes
1927189592Sbms	IN_MULTI_LOCK_ASSERT();
1928189592Sbms
1929189592Sbms	KASSERT(inm->inm_igi->igi_version == IGMP_VERSION_3,
1930189592Sbms		("%s: not IGMPv3 mode on link", __func__));
1931189592Sbms
1932189592Sbms	if (inm->inm_state != IGMP_G_QUERY_PENDING_MEMBER ||
1933189592Sbms	    inm->inm_state != IGMP_SG_QUERY_PENDING_MEMBER)
1934189592Sbms		return;
1935189592Sbms
1936189592Sbms	if (inm->inm_state == IGMP_SG_QUERY_PENDING_MEMBER)
1937189592Sbms		inm_clear_recorded(inm);
1938189592Sbms
1939189592Sbms	inm->inm_timer = 0;
1940189592Sbms	inm->inm_state = IGMP_REPORTING_MEMBER;
1941189592Sbms}
1942189592Sbms
1943189592Sbms/*
1944189592Sbms * Switch to a different IGMP version on the given interface,
1945189592Sbms * as per Section 7.2.1.
1946189592Sbms */
1947189592Sbmsstatic void
1948189592Sbmsigmp_set_version(struct igmp_ifinfo *igi, const int version)
1949189592Sbms{
1950193231Sbms	int old_version_timer;
1951189592Sbms
1952189592Sbms	IGMP_LOCK_ASSERT();
1953189592Sbms
1954189592Sbms	CTR4(KTR_IGMPV3, "%s: switching to v%d on ifp %p(%s)", __func__,
1955189592Sbms	    version, igi->igi_ifp, igi->igi_ifp->if_xname);
1956189592Sbms
1957189592Sbms	if (version == IGMP_VERSION_1 || version == IGMP_VERSION_2) {
1958189592Sbms		/*
1959189592Sbms		 * Compute the "Older Version Querier Present" timer as per
1960189592Sbms		 * Section 8.12.
1961189592Sbms		 */
1962189592Sbms		old_version_timer = igi->igi_rv * igi->igi_qi + igi->igi_qri;
1963189592Sbms		old_version_timer *= PR_SLOWHZ;
1964189592Sbms
1965189592Sbms		if (version == IGMP_VERSION_1) {
1966189592Sbms			igi->igi_v1_timer = old_version_timer;
1967189592Sbms			igi->igi_v2_timer = 0;
1968189592Sbms		} else if (version == IGMP_VERSION_2) {
1969189592Sbms			igi->igi_v1_timer = 0;
1970189592Sbms			igi->igi_v2_timer = old_version_timer;
1971189592Sbms		}
1972189592Sbms	}
1973189592Sbms
1974189592Sbms	if (igi->igi_v1_timer == 0 && igi->igi_v2_timer > 0) {
1975189592Sbms		if (igi->igi_version != IGMP_VERSION_2) {
1976189592Sbms			igi->igi_version = IGMP_VERSION_2;
1977189592Sbms			igmp_v3_cancel_link_timers(igi);
1978189592Sbms		}
1979189592Sbms	} else if (igi->igi_v1_timer > 0) {
1980189592Sbms		if (igi->igi_version != IGMP_VERSION_1) {
1981189592Sbms			igi->igi_version = IGMP_VERSION_1;
1982189592Sbms			igmp_v3_cancel_link_timers(igi);
1983189592Sbms		}
1984189592Sbms	}
1985189592Sbms}
1986189592Sbms
1987189592Sbms/*
1988189592Sbms * Cancel pending IGMPv3 timers for the given link and all groups
1989189592Sbms * joined on it; state-change, general-query, and group-query timers.
1990193231Sbms *
1991193231Sbms * Only ever called on a transition from v3 to Compatibility mode. Kill
1992193231Sbms * the timers stone dead (this may be expensive for large N groups), they
1993193231Sbms * will be restarted if Compatibility Mode deems that they must be due to
1994193231Sbms * query processing.
1995189592Sbms */
1996189592Sbmsstatic void
1997189592Sbmsigmp_v3_cancel_link_timers(struct igmp_ifinfo *igi)
1998189592Sbms{
1999189592Sbms	struct ifmultiaddr	*ifma;
2000189592Sbms	struct ifnet		*ifp;
2001229420Sjhb	struct in_multi		*inm, *tinm;
2002189592Sbms
2003189592Sbms	CTR3(KTR_IGMPV3, "%s: cancel v3 timers on ifp %p(%s)", __func__,
2004189592Sbms	    igi->igi_ifp, igi->igi_ifp->if_xname);
2005189592Sbms
2006189592Sbms	IN_MULTI_LOCK_ASSERT();
2007189592Sbms	IGMP_LOCK_ASSERT();
2008189592Sbms
20091541Srgrimes	/*
2010193231Sbms	 * Stop the v3 General Query Response on this link stone dead.
2011193231Sbms	 * If fasttimo is woken up due to V_interface_timers_running,
2012193231Sbms	 * the flag will be cleared if there are no pending link timers.
20131541Srgrimes	 */
2014189592Sbms	igi->igi_v3_timer = 0;
2015189592Sbms
2016193231Sbms	/*
2017193231Sbms	 * Now clear the current-state and state-change report timers
2018193231Sbms	 * for all memberships scoped to this link.
2019193231Sbms	 */
2020189592Sbms	ifp = igi->igi_ifp;
2021229621Sjhb	IF_ADDR_RLOCK(ifp);
2022189592Sbms	TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
2023193231Sbms		if (ifma->ifma_addr->sa_family != AF_INET ||
2024193231Sbms		    ifma->ifma_protospec == NULL)
2025189592Sbms			continue;
2026189592Sbms		inm = (struct in_multi *)ifma->ifma_protospec;
2027189592Sbms		switch (inm->inm_state) {
2028189592Sbms		case IGMP_NOT_MEMBER:
2029189592Sbms		case IGMP_SILENT_MEMBER:
2030189592Sbms		case IGMP_IDLE_MEMBER:
2031189592Sbms		case IGMP_LAZY_MEMBER:
2032189592Sbms		case IGMP_SLEEPING_MEMBER:
2033189592Sbms		case IGMP_AWAKENING_MEMBER:
2034193231Sbms			/*
2035193231Sbms			 * These states are either not relevant in v3 mode,
2036193231Sbms			 * or are unreported. Do nothing.
2037193231Sbms			 */
2038189592Sbms			break;
2039189592Sbms		case IGMP_LEAVING_MEMBER:
2040189592Sbms			/*
2041193231Sbms			 * If we are leaving the group and switching to
2042193231Sbms			 * compatibility mode, we need to release the final
2043193231Sbms			 * reference held for issuing the INCLUDE {}, and
2044193231Sbms			 * transition to REPORTING to ensure the host leave
2045193231Sbms			 * message is sent upstream to the old querier --
2046193231Sbms			 * transition to NOT would lose the leave and race.
2047189592Sbms			 */
2048229420Sjhb			SLIST_INSERT_HEAD(&igi->igi_relinmhead, inm, inm_nrele);
2049189592Sbms			/* FALLTHROUGH */
2050189592Sbms		case IGMP_G_QUERY_PENDING_MEMBER:
2051189592Sbms		case IGMP_SG_QUERY_PENDING_MEMBER:
2052189592Sbms			inm_clear_recorded(inm);
2053189592Sbms			/* FALLTHROUGH */
2054189592Sbms		case IGMP_REPORTING_MEMBER:
2055189592Sbms			inm->inm_state = IGMP_REPORTING_MEMBER;
2056189592Sbms			break;
2057189592Sbms		}
2058193231Sbms		/*
2059193231Sbms		 * Always clear state-change and group report timers.
2060193231Sbms		 * Free any pending IGMPv3 state-change records.
2061193231Sbms		 */
2062193231Sbms		inm->inm_sctimer = 0;
2063193231Sbms		inm->inm_timer = 0;
2064193231Sbms		_IF_DRAIN(&inm->inm_scq);
2065189592Sbms	}
2066229621Sjhb	IF_ADDR_RUNLOCK(ifp);
2067229420Sjhb	SLIST_FOREACH_SAFE(inm, &igi->igi_relinmhead, inm_nrele, tinm) {
2068229420Sjhb		SLIST_REMOVE_HEAD(&igi->igi_relinmhead, inm_nrele);
2069229420Sjhb		inm_release_locked(inm);
2070229420Sjhb	}
2071189592Sbms}
2072189592Sbms
2073189592Sbms/*
2074189592Sbms * Update the Older Version Querier Present timers for a link.
2075189592Sbms * See Section 7.2.1 of RFC 3376.
2076189592Sbms */
2077189592Sbmsstatic void
2078189592Sbmsigmp_v1v2_process_querier_timers(struct igmp_ifinfo *igi)
2079189592Sbms{
2080189592Sbms
2081189592Sbms	IGMP_LOCK_ASSERT();
2082189592Sbms
2083189592Sbms	if (igi->igi_v1_timer == 0 && igi->igi_v2_timer == 0) {
2084189592Sbms		/*
2085189592Sbms		 * IGMPv1 and IGMPv2 Querier Present timers expired.
2086189592Sbms		 *
2087189592Sbms		 * Revert to IGMPv3.
2088189592Sbms		 */
2089189592Sbms		if (igi->igi_version != IGMP_VERSION_3) {
2090189592Sbms			CTR5(KTR_IGMPV3,
2091189592Sbms			    "%s: transition from v%d -> v%d on %p(%s)",
2092189592Sbms			    __func__, igi->igi_version, IGMP_VERSION_3,
2093189592Sbms			    igi->igi_ifp, igi->igi_ifp->if_xname);
2094189592Sbms			igi->igi_version = IGMP_VERSION_3;
2095189592Sbms		}
2096189592Sbms	} else if (igi->igi_v1_timer == 0 && igi->igi_v2_timer > 0) {
2097189592Sbms		/*
2098189592Sbms		 * IGMPv1 Querier Present timer expired,
2099189592Sbms		 * IGMPv2 Querier Present timer running.
2100189592Sbms		 * If IGMPv2 was disabled since last timeout,
2101189592Sbms		 * revert to IGMPv3.
2102189592Sbms		 * If IGMPv2 is enabled, revert to IGMPv2.
2103189592Sbms		 */
2104189592Sbms		if (!V_igmp_v2enable) {
2105189592Sbms			CTR5(KTR_IGMPV3,
2106189592Sbms			    "%s: transition from v%d -> v%d on %p(%s)",
2107189592Sbms			    __func__, igi->igi_version, IGMP_VERSION_3,
2108189592Sbms			    igi->igi_ifp, igi->igi_ifp->if_xname);
2109189592Sbms			igi->igi_v2_timer = 0;
2110189592Sbms			igi->igi_version = IGMP_VERSION_3;
2111189592Sbms		} else {
2112189592Sbms			--igi->igi_v2_timer;
2113189592Sbms			if (igi->igi_version != IGMP_VERSION_2) {
2114189592Sbms				CTR5(KTR_IGMPV3,
2115189592Sbms				    "%s: transition from v%d -> v%d on %p(%s)",
2116189592Sbms				    __func__, igi->igi_version, IGMP_VERSION_2,
2117189592Sbms				    igi->igi_ifp, igi->igi_ifp->if_xname);
2118189592Sbms				igi->igi_version = IGMP_VERSION_2;
2119251502Sbms				igmp_v3_cancel_link_timers(igi);
2120183550Szec			}
21211541Srgrimes		}
2122189592Sbms	} else if (igi->igi_v1_timer > 0) {
2123189592Sbms		/*
2124189592Sbms		 * IGMPv1 Querier Present timer running.
2125189592Sbms		 * Stop IGMPv2 timer if running.
2126189592Sbms		 *
2127189592Sbms		 * If IGMPv1 was disabled since last timeout,
2128189592Sbms		 * revert to IGMPv3.
2129189592Sbms		 * If IGMPv1 is enabled, reset IGMPv2 timer if running.
2130189592Sbms		 */
2131189592Sbms		if (!V_igmp_v1enable) {
2132189592Sbms			CTR5(KTR_IGMPV3,
2133189592Sbms			    "%s: transition from v%d -> v%d on %p(%s)",
2134189592Sbms			    __func__, igi->igi_version, IGMP_VERSION_3,
2135189592Sbms			    igi->igi_ifp, igi->igi_ifp->if_xname);
2136189592Sbms			igi->igi_v1_timer = 0;
2137189592Sbms			igi->igi_version = IGMP_VERSION_3;
2138189592Sbms		} else {
2139189592Sbms			--igi->igi_v1_timer;
2140189592Sbms		}
2141189592Sbms		if (igi->igi_v2_timer > 0) {
2142189592Sbms			CTR3(KTR_IGMPV3,
2143189592Sbms			    "%s: cancel v2 timer on %p(%s)",
2144189592Sbms			    __func__, igi->igi_ifp, igi->igi_ifp->if_xname);
2145189592Sbms			igi->igi_v2_timer = 0;
2146189592Sbms		}
21471541Srgrimes	}
21481541Srgrimes}
21491541Srgrimes
2150189592Sbms/*
2151189592Sbms * Global slowtimo handler.
2152189592Sbms * VIMAGE: Timeout handlers are expected to service all vimages.
2153189592Sbms */
21542531Swollmanvoid
2155119181Srwatsonigmp_slowtimo(void)
21562531Swollman{
2157183550Szec	VNET_ITERATOR_DECL(vnet_iter);
21582531Swollman
2159195760Srwatson	VNET_LIST_RLOCK_NOSLEEP();
2160183550Szec	VNET_FOREACH(vnet_iter) {
2161183550Szec		CURVNET_SET(vnet_iter);
2162189592Sbms		igmp_slowtimo_vnet();
2163183550Szec		CURVNET_RESTORE();
21642531Swollman	}
2165195760Srwatson	VNET_LIST_RUNLOCK_NOSLEEP();
21662531Swollman}
21672531Swollman
2168189592Sbms/*
2169189592Sbms * Per-vnet slowtimo handler.
2170189592Sbms */
21711541Srgrimesstatic void
2172189592Sbmsigmp_slowtimo_vnet(void)
21731541Srgrimes{
2174189592Sbms	struct igmp_ifinfo *igi;
21751541Srgrimes
2176189592Sbms	IGMP_LOCK();
2177189592Sbms
2178189592Sbms	LIST_FOREACH(igi, &V_igi_head, igi_link) {
2179189592Sbms		igmp_v1v2_process_querier_timers(igi);
2180189592Sbms	}
2181189592Sbms
2182189592Sbms	IGMP_UNLOCK();
2183189592Sbms}
2184189592Sbms
2185189592Sbms/*
2186189592Sbms * Dispatch an IGMPv1/v2 host report or leave message.
2187189592Sbms * These are always small enough to fit inside a single mbuf.
2188189592Sbms */
2189189592Sbmsstatic int
2190189592Sbmsigmp_v1v2_queue_report(struct in_multi *inm, const int type)
2191189592Sbms{
2192189592Sbms	struct ifnet		*ifp;
2193189592Sbms	struct igmp		*igmp;
2194189592Sbms	struct ip		*ip;
2195189592Sbms	struct mbuf		*m;
2196189592Sbms
2197148682Srwatson	IN_MULTI_LOCK_ASSERT();
2198189592Sbms	IGMP_LOCK_ASSERT();
2199148682Srwatson
2200189592Sbms	ifp = inm->inm_ifp;
2201189592Sbms
2202248373Sglebius	m = m_gethdr(M_NOWAIT, MT_DATA);
2203119181Srwatson	if (m == NULL)
2204189592Sbms		return (ENOMEM);
2205189592Sbms	MH_ALIGN(m, sizeof(struct ip) + sizeof(struct igmp));
22062531Swollman
2207189592Sbms	m->m_pkthdr.len = sizeof(struct ip) + sizeof(struct igmp);
2208189592Sbms
22092531Swollman	m->m_data += sizeof(struct ip);
2210189592Sbms	m->m_len = sizeof(struct igmp);
2211189592Sbms
2212119181Srwatson	igmp = mtod(m, struct igmp *);
2213119181Srwatson	igmp->igmp_type = type;
2214119181Srwatson	igmp->igmp_code = 0;
2215119181Srwatson	igmp->igmp_group = inm->inm_addr;
2216119181Srwatson	igmp->igmp_cksum = 0;
2217189592Sbms	igmp->igmp_cksum = in_cksum(m, sizeof(struct igmp));
22181541Srgrimes
2219119181Srwatson	m->m_data -= sizeof(struct ip);
2220119181Srwatson	m->m_len += sizeof(struct ip);
2221189592Sbms
2222119181Srwatson	ip = mtod(m, struct ip *);
2223119181Srwatson	ip->ip_tos = 0;
2224241913Sglebius	ip->ip_len = htons(sizeof(struct ip) + sizeof(struct igmp));
2225119181Srwatson	ip->ip_off = 0;
2226119181Srwatson	ip->ip_p = IPPROTO_IGMP;
2227119181Srwatson	ip->ip_src.s_addr = INADDR_ANY;
22281541Srgrimes
2229189592Sbms	if (type == IGMP_HOST_LEAVE_MESSAGE)
2230189592Sbms		ip->ip_dst.s_addr = htonl(INADDR_ALLRTRS_GROUP);
2231189592Sbms	else
2232189592Sbms		ip->ip_dst = inm->inm_addr;
2233189592Sbms
2234189592Sbms	igmp_save_context(m, ifp);
2235189592Sbms
2236189592Sbms	m->m_flags |= M_IGMPV2;
2237189592Sbms	if (inm->inm_igi->igi_flags & IGIF_LOOPBACK)
2238189592Sbms		m->m_flags |= M_IGMP_LOOP;
2239189592Sbms
2240189592Sbms	CTR2(KTR_IGMPV3, "%s: netisr_dispatch(NETISR_IGMP, %p)", __func__, m);
2241189592Sbms	netisr_dispatch(NETISR_IGMP, m);
2242189592Sbms
2243189592Sbms	return (0);
2244189592Sbms}
2245189592Sbms
2246189592Sbms/*
2247189592Sbms * Process a state change from the upper layer for the given IPv4 group.
2248189592Sbms *
2249189592Sbms * Each socket holds a reference on the in_multi in its own ip_moptions.
2250189592Sbms * The socket layer will have made the necessary updates to.the group
2251189592Sbms * state, it is now up to IGMP to issue a state change report if there
2252189592Sbms * has been any change between T0 (when the last state-change was issued)
2253189592Sbms * and T1 (now).
2254189592Sbms *
2255189592Sbms * We use the IGMPv3 state machine at group level. The IGMP module
2256189592Sbms * however makes the decision as to which IGMP protocol version to speak.
2257189592Sbms * A state change *from* INCLUDE {} always means an initial join.
2258189592Sbms * A state change *to* INCLUDE {} always means a final leave.
2259189592Sbms *
2260189592Sbms * FUTURE: If IGIF_V3LITE is enabled for this interface, then we can
2261189592Sbms * save ourselves a bunch of work; any exclusive mode groups need not
2262189592Sbms * compute source filter lists.
2263189592Sbms *
2264189592Sbms * VIMAGE: curvnet should have been set by caller, as this routine
2265189592Sbms * is called from the socket option handlers.
2266189592Sbms */
2267189592Sbmsint
2268189592Sbmsigmp_change_state(struct in_multi *inm)
2269189592Sbms{
2270189592Sbms	struct igmp_ifinfo *igi;
2271189592Sbms	struct ifnet *ifp;
2272189592Sbms	int error;
2273189592Sbms
2274189592Sbms	IN_MULTI_LOCK_ASSERT();
2275189592Sbms
2276189592Sbms	error = 0;
2277189592Sbms
2278189592Sbms	/*
2279189592Sbms	 * Try to detect if the upper layer just asked us to change state
2280189592Sbms	 * for an interface which has now gone away.
2281189592Sbms	 */
2282189592Sbms	KASSERT(inm->inm_ifma != NULL, ("%s: no ifma", __func__));
2283189592Sbms	ifp = inm->inm_ifma->ifma_ifp;
2284238084Strociny	/*
2285238084Strociny	 * Sanity check that netinet's notion of ifp is the
2286238084Strociny	 * same as net's.
2287238084Strociny	 */
2288238084Strociny	KASSERT(inm->inm_ifp == ifp, ("%s: bad ifp", __func__));
2289189592Sbms
2290189592Sbms	IGMP_LOCK();
2291189592Sbms
2292189592Sbms	igi = ((struct in_ifinfo *)ifp->if_afdata[AF_INET])->ii_igmp;
2293189592Sbms	KASSERT(igi != NULL, ("%s: no igmp_ifinfo for ifp %p", __func__, ifp));
2294189592Sbms
2295189592Sbms	/*
2296189592Sbms	 * If we detect a state transition to or from MCAST_UNDEFINED
2297189592Sbms	 * for this group, then we are starting or finishing an IGMP
2298189592Sbms	 * life cycle for this group.
2299189592Sbms	 */
2300189592Sbms	if (inm->inm_st[1].iss_fmode != inm->inm_st[0].iss_fmode) {
2301189592Sbms		CTR3(KTR_IGMPV3, "%s: inm transition %d -> %d", __func__,
2302189592Sbms		    inm->inm_st[0].iss_fmode, inm->inm_st[1].iss_fmode);
2303189592Sbms		if (inm->inm_st[0].iss_fmode == MCAST_UNDEFINED) {
2304189592Sbms			CTR1(KTR_IGMPV3, "%s: initial join", __func__);
2305189592Sbms			error = igmp_initial_join(inm, igi);
2306189592Sbms			goto out_locked;
2307189592Sbms		} else if (inm->inm_st[1].iss_fmode == MCAST_UNDEFINED) {
2308189592Sbms			CTR1(KTR_IGMPV3, "%s: final leave", __func__);
2309189592Sbms			igmp_final_leave(inm, igi);
2310189592Sbms			goto out_locked;
2311189592Sbms		}
2312189592Sbms	} else {
2313189592Sbms		CTR1(KTR_IGMPV3, "%s: filter set change", __func__);
2314189592Sbms	}
2315189592Sbms
2316189592Sbms	error = igmp_handle_state_change(inm, igi);
2317189592Sbms
2318189592Sbmsout_locked:
2319189592Sbms	IGMP_UNLOCK();
2320189592Sbms	return (error);
2321189592Sbms}
2322189592Sbms
2323189592Sbms/*
2324189592Sbms * Perform the initial join for an IGMP group.
2325189592Sbms *
2326189592Sbms * When joining a group:
2327189592Sbms *  If the group should have its IGMP traffic suppressed, do nothing.
2328189592Sbms *  IGMPv1 starts sending IGMPv1 host membership reports.
2329189592Sbms *  IGMPv2 starts sending IGMPv2 host membership reports.
2330189592Sbms *  IGMPv3 will schedule an IGMPv3 state-change report containing the
2331189592Sbms *  initial state of the membership.
2332189592Sbms */
2333189592Sbmsstatic int
2334189592Sbmsigmp_initial_join(struct in_multi *inm, struct igmp_ifinfo *igi)
2335189592Sbms{
2336189592Sbms	struct ifnet		*ifp;
2337189592Sbms	struct ifqueue		*ifq;
2338189592Sbms	int			 error, retval, syncstates;
2339189592Sbms
2340189592Sbms	CTR4(KTR_IGMPV3, "%s: initial join %s on ifp %p(%s)",
2341189592Sbms	    __func__, inet_ntoa(inm->inm_addr), inm->inm_ifp,
2342189592Sbms	    inm->inm_ifp->if_xname);
2343189592Sbms
2344189592Sbms	error = 0;
2345189592Sbms	syncstates = 1;
2346189592Sbms
2347189592Sbms	ifp = inm->inm_ifp;
2348189592Sbms
2349189592Sbms	IN_MULTI_LOCK_ASSERT();
2350189592Sbms	IGMP_LOCK_ASSERT();
2351189592Sbms
2352189592Sbms	KASSERT(igi && igi->igi_ifp == ifp, ("%s: inconsistent ifp", __func__));
2353189592Sbms
2354189592Sbms	/*
2355189592Sbms	 * Groups joined on loopback or marked as 'not reported',
2356189592Sbms	 * e.g. 224.0.0.1, enter the IGMP_SILENT_MEMBER state and
2357189592Sbms	 * are never reported in any IGMP protocol exchanges.
2358189592Sbms	 * All other groups enter the appropriate IGMP state machine
2359189592Sbms	 * for the version in use on this link.
2360189592Sbms	 * A link marked as IGIF_SILENT causes IGMP to be completely
2361189592Sbms	 * disabled for the link.
2362189592Sbms	 */
2363189592Sbms	if ((ifp->if_flags & IFF_LOOPBACK) ||
2364189592Sbms	    (igi->igi_flags & IGIF_SILENT) ||
2365189592Sbms	    !igmp_isgroupreported(inm->inm_addr)) {
2366189592Sbms		CTR1(KTR_IGMPV3,
2367189592Sbms"%s: not kicking state machine for silent group", __func__);
2368189592Sbms		inm->inm_state = IGMP_SILENT_MEMBER;
2369189592Sbms		inm->inm_timer = 0;
2370189592Sbms	} else {
2371189592Sbms		/*
2372189592Sbms		 * Deal with overlapping in_multi lifecycle.
2373189592Sbms		 * If this group was LEAVING, then make sure
2374189592Sbms		 * we drop the reference we picked up to keep the
2375189592Sbms		 * group around for the final INCLUDE {} enqueue.
2376189592Sbms		 */
2377189592Sbms		if (igi->igi_version == IGMP_VERSION_3 &&
2378189592Sbms		    inm->inm_state == IGMP_LEAVING_MEMBER)
2379189592Sbms			inm_release_locked(inm);
2380189592Sbms
2381189592Sbms		inm->inm_state = IGMP_REPORTING_MEMBER;
2382189592Sbms
2383189592Sbms		switch (igi->igi_version) {
2384189592Sbms		case IGMP_VERSION_1:
2385189592Sbms		case IGMP_VERSION_2:
2386189592Sbms			inm->inm_state = IGMP_IDLE_MEMBER;
2387189592Sbms			error = igmp_v1v2_queue_report(inm,
2388189592Sbms			    (igi->igi_version == IGMP_VERSION_2) ?
2389189592Sbms			     IGMP_v2_HOST_MEMBERSHIP_REPORT :
2390189592Sbms			     IGMP_v1_HOST_MEMBERSHIP_REPORT);
2391189592Sbms			if (error == 0) {
2392189592Sbms				inm->inm_timer = IGMP_RANDOM_DELAY(
2393189592Sbms				    IGMP_V1V2_MAX_RI * PR_FASTHZ);
2394189592Sbms				V_current_state_timers_running = 1;
2395189592Sbms			}
2396189592Sbms			break;
2397189592Sbms
2398189592Sbms		case IGMP_VERSION_3:
2399189592Sbms			/*
2400189592Sbms			 * Defer update of T0 to T1, until the first copy
2401189592Sbms			 * of the state change has been transmitted.
2402189592Sbms			 */
2403189592Sbms			syncstates = 0;
2404189592Sbms
2405189592Sbms			/*
2406189592Sbms			 * Immediately enqueue a State-Change Report for
2407189592Sbms			 * this interface, freeing any previous reports.
2408189592Sbms			 * Don't kick the timers if there is nothing to do,
2409189592Sbms			 * or if an error occurred.
2410189592Sbms			 */
2411189592Sbms			ifq = &inm->inm_scq;
2412189592Sbms			_IF_DRAIN(ifq);
2413189592Sbms			retval = igmp_v3_enqueue_group_record(ifq, inm, 1,
2414189592Sbms			    0, 0);
2415189592Sbms			CTR2(KTR_IGMPV3, "%s: enqueue record = %d",
2416189592Sbms			    __func__, retval);
2417189592Sbms			if (retval <= 0) {
2418189592Sbms				error = retval * -1;
2419189592Sbms				break;
2420189592Sbms			}
2421189592Sbms
2422189592Sbms			/*
2423189592Sbms			 * Schedule transmission of pending state-change
2424189592Sbms			 * report up to RV times for this link. The timer
2425189592Sbms			 * will fire at the next igmp_fasttimo (~200ms),
2426189592Sbms			 * giving us an opportunity to merge the reports.
2427189592Sbms			 */
2428189592Sbms			if (igi->igi_flags & IGIF_LOOPBACK) {
2429189592Sbms				inm->inm_scrv = 1;
2430189592Sbms			} else {
2431189592Sbms				KASSERT(igi->igi_rv > 1,
2432189592Sbms				   ("%s: invalid robustness %d", __func__,
2433189592Sbms				    igi->igi_rv));
2434189592Sbms				inm->inm_scrv = igi->igi_rv;
2435189592Sbms			}
2436189592Sbms			inm->inm_sctimer = 1;
2437189592Sbms			V_state_change_timers_running = 1;
2438189592Sbms
2439189592Sbms			error = 0;
2440189592Sbms			break;
2441189592Sbms		}
2442189592Sbms	}
2443189592Sbms
2444189592Sbms	/*
2445189592Sbms	 * Only update the T0 state if state change is atomic,
2446189592Sbms	 * i.e. we don't need to wait for a timer to fire before we
2447189592Sbms	 * can consider the state change to have been communicated.
2448189592Sbms	 */
2449189592Sbms	if (syncstates) {
2450189592Sbms		inm_commit(inm);
2451189592Sbms		CTR3(KTR_IGMPV3, "%s: T1 -> T0 for %s/%s", __func__,
2452189592Sbms		    inet_ntoa(inm->inm_addr), inm->inm_ifp->if_xname);
2453189592Sbms	}
2454189592Sbms
2455189592Sbms	return (error);
2456189592Sbms}
2457189592Sbms
2458189592Sbms/*
2459189592Sbms * Issue an intermediate state change during the IGMP life-cycle.
2460189592Sbms */
2461189592Sbmsstatic int
2462189592Sbmsigmp_handle_state_change(struct in_multi *inm, struct igmp_ifinfo *igi)
2463189592Sbms{
2464189592Sbms	struct ifnet		*ifp;
2465189592Sbms	int			 retval;
2466189592Sbms
2467189592Sbms	CTR4(KTR_IGMPV3, "%s: state change for %s on ifp %p(%s)",
2468189592Sbms	    __func__, inet_ntoa(inm->inm_addr), inm->inm_ifp,
2469189592Sbms	    inm->inm_ifp->if_xname);
2470189592Sbms
2471189592Sbms	ifp = inm->inm_ifp;
2472189592Sbms
2473189592Sbms	IN_MULTI_LOCK_ASSERT();
2474189592Sbms	IGMP_LOCK_ASSERT();
2475189592Sbms
2476189592Sbms	KASSERT(igi && igi->igi_ifp == ifp, ("%s: inconsistent ifp", __func__));
2477189592Sbms
2478189592Sbms	if ((ifp->if_flags & IFF_LOOPBACK) ||
2479189592Sbms	    (igi->igi_flags & IGIF_SILENT) ||
2480189592Sbms	    !igmp_isgroupreported(inm->inm_addr) ||
2481189592Sbms	    (igi->igi_version != IGMP_VERSION_3)) {
2482189592Sbms		if (!igmp_isgroupreported(inm->inm_addr)) {
2483189592Sbms			CTR1(KTR_IGMPV3,
2484189592Sbms"%s: not kicking state machine for silent group", __func__);
2485189592Sbms		}
2486189592Sbms		CTR1(KTR_IGMPV3, "%s: nothing to do", __func__);
2487189592Sbms		inm_commit(inm);
2488189592Sbms		CTR3(KTR_IGMPV3, "%s: T1 -> T0 for %s/%s", __func__,
2489189592Sbms		    inet_ntoa(inm->inm_addr), inm->inm_ifp->if_xname);
2490189592Sbms		return (0);
2491189592Sbms	}
2492189592Sbms
2493189592Sbms	_IF_DRAIN(&inm->inm_scq);
2494189592Sbms
2495189592Sbms	retval = igmp_v3_enqueue_group_record(&inm->inm_scq, inm, 1, 0, 0);
2496189592Sbms	CTR2(KTR_IGMPV3, "%s: enqueue record = %d", __func__, retval);
2497189592Sbms	if (retval <= 0)
2498189592Sbms		return (-retval);
2499189592Sbms
2500189592Sbms	/*
2501189592Sbms	 * If record(s) were enqueued, start the state-change
2502189592Sbms	 * report timer for this group.
2503189592Sbms	 */
2504189592Sbms	inm->inm_scrv = ((igi->igi_flags & IGIF_LOOPBACK) ? 1 : igi->igi_rv);
2505189592Sbms	inm->inm_sctimer = 1;
2506189592Sbms	V_state_change_timers_running = 1;
2507189592Sbms
2508189592Sbms	return (0);
2509189592Sbms}
2510189592Sbms
2511189592Sbms/*
2512189592Sbms * Perform the final leave for an IGMP group.
2513189592Sbms *
2514189592Sbms * When leaving a group:
2515189592Sbms *  IGMPv1 does nothing.
2516189592Sbms *  IGMPv2 sends a host leave message, if and only if we are the reporter.
2517189592Sbms *  IGMPv3 enqueues a state-change report containing a transition
2518189592Sbms *  to INCLUDE {} for immediate transmission.
2519189592Sbms */
2520189592Sbmsstatic void
2521189592Sbmsigmp_final_leave(struct in_multi *inm, struct igmp_ifinfo *igi)
2522189592Sbms{
2523189592Sbms	int syncstates;
2524189592Sbms
2525189592Sbms	syncstates = 1;
2526189592Sbms
2527189592Sbms	CTR4(KTR_IGMPV3, "%s: final leave %s on ifp %p(%s)",
2528189592Sbms	    __func__, inet_ntoa(inm->inm_addr), inm->inm_ifp,
2529189592Sbms	    inm->inm_ifp->if_xname);
2530189592Sbms
2531189592Sbms	IN_MULTI_LOCK_ASSERT();
2532189592Sbms	IGMP_LOCK_ASSERT();
2533189592Sbms
2534189592Sbms	switch (inm->inm_state) {
2535189592Sbms	case IGMP_NOT_MEMBER:
2536189592Sbms	case IGMP_SILENT_MEMBER:
2537189592Sbms	case IGMP_LEAVING_MEMBER:
2538189592Sbms		/* Already leaving or left; do nothing. */
2539189592Sbms		CTR1(KTR_IGMPV3,
2540189592Sbms"%s: not kicking state machine for silent group", __func__);
2541189592Sbms		break;
2542189592Sbms	case IGMP_REPORTING_MEMBER:
2543189592Sbms	case IGMP_IDLE_MEMBER:
2544189592Sbms	case IGMP_G_QUERY_PENDING_MEMBER:
2545189592Sbms	case IGMP_SG_QUERY_PENDING_MEMBER:
2546189592Sbms		if (igi->igi_version == IGMP_VERSION_2) {
2547189592Sbms#ifdef INVARIANTS
2548189592Sbms			if (inm->inm_state == IGMP_G_QUERY_PENDING_MEMBER ||
2549189592Sbms			    inm->inm_state == IGMP_SG_QUERY_PENDING_MEMBER)
2550189592Sbms			panic("%s: IGMPv3 state reached, not IGMPv3 mode",
2551189592Sbms			     __func__);
2552189592Sbms#endif
2553189592Sbms			igmp_v1v2_queue_report(inm, IGMP_HOST_LEAVE_MESSAGE);
2554189592Sbms			inm->inm_state = IGMP_NOT_MEMBER;
2555189592Sbms		} else if (igi->igi_version == IGMP_VERSION_3) {
2556189592Sbms			/*
2557189592Sbms			 * Stop group timer and all pending reports.
2558189592Sbms			 * Immediately enqueue a state-change report
2559189592Sbms			 * TO_IN {} to be sent on the next fast timeout,
2560189592Sbms			 * giving us an opportunity to merge reports.
2561189592Sbms			 */
2562189592Sbms			_IF_DRAIN(&inm->inm_scq);
2563189592Sbms			inm->inm_timer = 0;
2564189592Sbms			if (igi->igi_flags & IGIF_LOOPBACK) {
2565189592Sbms				inm->inm_scrv = 1;
2566189592Sbms			} else {
2567189592Sbms				inm->inm_scrv = igi->igi_rv;
2568189592Sbms			}
2569189592Sbms			CTR4(KTR_IGMPV3, "%s: Leaving %s/%s with %d "
2570189592Sbms			    "pending retransmissions.", __func__,
2571189592Sbms			    inet_ntoa(inm->inm_addr),
2572189592Sbms			    inm->inm_ifp->if_xname, inm->inm_scrv);
2573189592Sbms			if (inm->inm_scrv == 0) {
2574189592Sbms				inm->inm_state = IGMP_NOT_MEMBER;
2575189592Sbms				inm->inm_sctimer = 0;
2576189592Sbms			} else {
2577189592Sbms				int retval;
2578189592Sbms
2579189592Sbms				inm_acquire_locked(inm);
2580189592Sbms
2581189592Sbms				retval = igmp_v3_enqueue_group_record(
2582189592Sbms				    &inm->inm_scq, inm, 1, 0, 0);
2583189592Sbms				KASSERT(retval != 0,
2584189592Sbms				    ("%s: enqueue record = %d", __func__,
2585189592Sbms				     retval));
2586189592Sbms
2587189592Sbms				inm->inm_state = IGMP_LEAVING_MEMBER;
2588189592Sbms				inm->inm_sctimer = 1;
2589189592Sbms				V_state_change_timers_running = 1;
2590189592Sbms				syncstates = 0;
2591189592Sbms			}
2592189592Sbms			break;
2593189592Sbms		}
2594189592Sbms		break;
2595189592Sbms	case IGMP_LAZY_MEMBER:
2596189592Sbms	case IGMP_SLEEPING_MEMBER:
2597189592Sbms	case IGMP_AWAKENING_MEMBER:
2598189592Sbms		/* Our reports are suppressed; do nothing. */
2599189592Sbms		break;
2600189592Sbms	}
2601189592Sbms
2602189592Sbms	if (syncstates) {
2603189592Sbms		inm_commit(inm);
2604189592Sbms		CTR3(KTR_IGMPV3, "%s: T1 -> T0 for %s/%s", __func__,
2605189592Sbms		    inet_ntoa(inm->inm_addr), inm->inm_ifp->if_xname);
2606189592Sbms		inm->inm_st[1].iss_fmode = MCAST_UNDEFINED;
2607189592Sbms		CTR3(KTR_IGMPV3, "%s: T1 now MCAST_UNDEFINED for %s/%s",
2608189592Sbms		    __func__, inet_ntoa(inm->inm_addr), inm->inm_ifp->if_xname);
2609189592Sbms	}
2610189592Sbms}
2611189592Sbms
2612189592Sbms/*
2613189592Sbms * Enqueue an IGMPv3 group record to the given output queue.
2614189592Sbms *
2615189592Sbms * XXX This function could do with having the allocation code
2616189592Sbms * split out, and the multiple-tree-walks coalesced into a single
2617189592Sbms * routine as has been done in igmp_v3_enqueue_filter_change().
2618189592Sbms *
2619189592Sbms * If is_state_change is zero, a current-state record is appended.
2620189592Sbms * If is_state_change is non-zero, a state-change report is appended.
2621189592Sbms *
2622189592Sbms * If is_group_query is non-zero, an mbuf packet chain is allocated.
2623189592Sbms * If is_group_query is zero, and if there is a packet with free space
2624189592Sbms * at the tail of the queue, it will be appended to providing there
2625189592Sbms * is enough free space.
2626189592Sbms * Otherwise a new mbuf packet chain is allocated.
2627189592Sbms *
2628189592Sbms * If is_source_query is non-zero, each source is checked to see if
2629189592Sbms * it was recorded for a Group-Source query, and will be omitted if
2630189592Sbms * it is not both in-mode and recorded.
2631189592Sbms *
2632189592Sbms * The function will attempt to allocate leading space in the packet
2633189592Sbms * for the IP/IGMP header to be prepended without fragmenting the chain.
2634189592Sbms *
2635189592Sbms * If successful the size of all data appended to the queue is returned,
2636189592Sbms * otherwise an error code less than zero is returned, or zero if
2637189592Sbms * no record(s) were appended.
2638189592Sbms */
2639189592Sbmsstatic int
2640189592Sbmsigmp_v3_enqueue_group_record(struct ifqueue *ifq, struct in_multi *inm,
2641189592Sbms    const int is_state_change, const int is_group_query,
2642189592Sbms    const int is_source_query)
2643189592Sbms{
2644189592Sbms	struct igmp_grouprec	 ig;
2645189592Sbms	struct igmp_grouprec	*pig;
2646189592Sbms	struct ifnet		*ifp;
2647189592Sbms	struct ip_msource	*ims, *nims;
2648189592Sbms	struct mbuf		*m0, *m, *md;
2649189592Sbms	int			 error, is_filter_list_change;
2650189592Sbms	int			 minrec0len, m0srcs, msrcs, nbytes, off;
2651189592Sbms	int			 record_has_sources;
2652189592Sbms	int			 now;
2653189592Sbms	int			 type;
2654189592Sbms	in_addr_t		 naddr;
2655189592Sbms	uint8_t			 mode;
2656189592Sbms
2657189592Sbms	IN_MULTI_LOCK_ASSERT();
2658189592Sbms
2659189592Sbms	error = 0;
2660189592Sbms	ifp = inm->inm_ifp;
2661189592Sbms	is_filter_list_change = 0;
2662189592Sbms	m = NULL;
2663189592Sbms	m0 = NULL;
2664189592Sbms	m0srcs = 0;
2665189592Sbms	msrcs = 0;
2666189592Sbms	nbytes = 0;
2667189592Sbms	nims = NULL;
2668189592Sbms	record_has_sources = 1;
2669189592Sbms	pig = NULL;
2670189592Sbms	type = IGMP_DO_NOTHING;
2671189592Sbms	mode = inm->inm_st[1].iss_fmode;
2672189592Sbms
2673189592Sbms	/*
2674189592Sbms	 * If we did not transition out of ASM mode during t0->t1,
2675189592Sbms	 * and there are no source nodes to process, we can skip
2676189592Sbms	 * the generation of source records.
2677189592Sbms	 */
2678189592Sbms	if (inm->inm_st[0].iss_asm > 0 && inm->inm_st[1].iss_asm > 0 &&
2679189592Sbms	    inm->inm_nsrc == 0)
2680189592Sbms		record_has_sources = 0;
2681189592Sbms
2682189592Sbms	if (is_state_change) {
2683189592Sbms		/*
2684189592Sbms		 * Queue a state change record.
2685189592Sbms		 * If the mode did not change, and there are non-ASM
2686189592Sbms		 * listeners or source filters present,
2687189592Sbms		 * we potentially need to issue two records for the group.
2688189592Sbms		 * If we are transitioning to MCAST_UNDEFINED, we need
2689189592Sbms		 * not send any sources.
2690189592Sbms		 * If there are ASM listeners, and there was no filter
2691189592Sbms		 * mode transition of any kind, do nothing.
2692189592Sbms		 */
2693189592Sbms		if (mode != inm->inm_st[0].iss_fmode) {
2694189592Sbms			if (mode == MCAST_EXCLUDE) {
2695189592Sbms				CTR1(KTR_IGMPV3, "%s: change to EXCLUDE",
2696189592Sbms				    __func__);
2697189592Sbms				type = IGMP_CHANGE_TO_EXCLUDE_MODE;
2698189592Sbms			} else {
2699189592Sbms				CTR1(KTR_IGMPV3, "%s: change to INCLUDE",
2700189592Sbms				    __func__);
2701189592Sbms				type = IGMP_CHANGE_TO_INCLUDE_MODE;
2702189592Sbms				if (mode == MCAST_UNDEFINED)
2703189592Sbms					record_has_sources = 0;
2704189592Sbms			}
2705189592Sbms		} else {
2706189592Sbms			if (record_has_sources) {
2707189592Sbms				is_filter_list_change = 1;
2708189592Sbms			} else {
2709189592Sbms				type = IGMP_DO_NOTHING;
2710189592Sbms			}
2711189592Sbms		}
2712189592Sbms	} else {
2713189592Sbms		/*
2714189592Sbms		 * Queue a current state record.
2715189592Sbms		 */
2716189592Sbms		if (mode == MCAST_EXCLUDE) {
2717189592Sbms			type = IGMP_MODE_IS_EXCLUDE;
2718189592Sbms		} else if (mode == MCAST_INCLUDE) {
2719189592Sbms			type = IGMP_MODE_IS_INCLUDE;
2720189592Sbms			KASSERT(inm->inm_st[1].iss_asm == 0,
2721189592Sbms			    ("%s: inm %p is INCLUDE but ASM count is %d",
2722189592Sbms			     __func__, inm, inm->inm_st[1].iss_asm));
2723189592Sbms		}
2724189592Sbms	}
2725189592Sbms
2726189592Sbms	/*
2727189592Sbms	 * Generate the filter list changes using a separate function.
2728189592Sbms	 */
2729189592Sbms	if (is_filter_list_change)
2730189592Sbms		return (igmp_v3_enqueue_filter_change(ifq, inm));
2731189592Sbms
2732189592Sbms	if (type == IGMP_DO_NOTHING) {
2733189592Sbms		CTR3(KTR_IGMPV3, "%s: nothing to do for %s/%s",
2734189592Sbms		    __func__, inet_ntoa(inm->inm_addr),
2735189592Sbms		    inm->inm_ifp->if_xname);
2736189592Sbms		return (0);
2737189592Sbms	}
2738189592Sbms
2739189592Sbms	/*
2740189592Sbms	 * If any sources are present, we must be able to fit at least
2741189592Sbms	 * one in the trailing space of the tail packet's mbuf,
2742189592Sbms	 * ideally more.
2743189592Sbms	 */
2744189592Sbms	minrec0len = sizeof(struct igmp_grouprec);
2745189592Sbms	if (record_has_sources)
2746189592Sbms		minrec0len += sizeof(in_addr_t);
2747189592Sbms
2748189592Sbms	CTR4(KTR_IGMPV3, "%s: queueing %s for %s/%s", __func__,
2749189592Sbms	    igmp_rec_type_to_str(type), inet_ntoa(inm->inm_addr),
2750189592Sbms	    inm->inm_ifp->if_xname);
2751189592Sbms
2752189592Sbms	/*
2753189592Sbms	 * Check if we have a packet in the tail of the queue for this
2754189592Sbms	 * group into which the first group record for this group will fit.
2755189592Sbms	 * Otherwise allocate a new packet.
2756189592Sbms	 * Always allocate leading space for IP+RA_OPT+IGMP+REPORT.
2757189592Sbms	 * Note: Group records for G/GSR query responses MUST be sent
2758189592Sbms	 * in their own packet.
2759189592Sbms	 */
2760189592Sbms	m0 = ifq->ifq_tail;
2761189592Sbms	if (!is_group_query &&
2762189592Sbms	    m0 != NULL &&
2763189592Sbms	    (m0->m_pkthdr.PH_vt.vt_nrecs + 1 <= IGMP_V3_REPORT_MAXRECS) &&
2764189592Sbms	    (m0->m_pkthdr.len + minrec0len) <
2765189592Sbms	     (ifp->if_mtu - IGMP_LEADINGSPACE)) {
2766189592Sbms		m0srcs = (ifp->if_mtu - m0->m_pkthdr.len -
2767189592Sbms			    sizeof(struct igmp_grouprec)) / sizeof(in_addr_t);
2768189592Sbms		m = m0;
2769189592Sbms		CTR1(KTR_IGMPV3, "%s: use existing packet", __func__);
2770189592Sbms	} else {
2771189592Sbms		if (_IF_QFULL(ifq)) {
2772189592Sbms			CTR1(KTR_IGMPV3, "%s: outbound queue full", __func__);
2773189592Sbms			return (-ENOMEM);
2774189592Sbms		}
2775189592Sbms		m = NULL;
2776189592Sbms		m0srcs = (ifp->if_mtu - IGMP_LEADINGSPACE -
2777189592Sbms		    sizeof(struct igmp_grouprec)) / sizeof(in_addr_t);
2778190692Sbms		if (!is_state_change && !is_group_query) {
2779243882Sglebius			m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
2780190692Sbms			if (m)
2781190692Sbms				m->m_data += IGMP_LEADINGSPACE;
2782190692Sbms		}
2783189592Sbms		if (m == NULL) {
2784243882Sglebius			m = m_gethdr(M_NOWAIT, MT_DATA);
2785189592Sbms			if (m)
2786189592Sbms				MH_ALIGN(m, IGMP_LEADINGSPACE);
2787189592Sbms		}
2788189592Sbms		if (m == NULL)
2789189592Sbms			return (-ENOMEM);
2790189592Sbms
2791189592Sbms		igmp_save_context(m, ifp);
2792189592Sbms
2793189592Sbms		CTR1(KTR_IGMPV3, "%s: allocated first packet", __func__);
2794189592Sbms	}
2795189592Sbms
2796189592Sbms	/*
2797189592Sbms	 * Append group record.
2798189592Sbms	 * If we have sources, we don't know how many yet.
2799189592Sbms	 */
2800189592Sbms	ig.ig_type = type;
2801189592Sbms	ig.ig_datalen = 0;
2802189592Sbms	ig.ig_numsrc = 0;
2803189592Sbms	ig.ig_group = inm->inm_addr;
2804189592Sbms	if (!m_append(m, sizeof(struct igmp_grouprec), (void *)&ig)) {
2805189592Sbms		if (m != m0)
2806189592Sbms			m_freem(m);
2807189592Sbms		CTR1(KTR_IGMPV3, "%s: m_append() failed.", __func__);
2808189592Sbms		return (-ENOMEM);
2809189592Sbms	}
2810189592Sbms	nbytes += sizeof(struct igmp_grouprec);
2811189592Sbms
2812189592Sbms	/*
2813189592Sbms	 * Append as many sources as will fit in the first packet.
2814189592Sbms	 * If we are appending to a new packet, the chain allocation
2815189592Sbms	 * may potentially use clusters; use m_getptr() in this case.
2816189592Sbms	 * If we are appending to an existing packet, we need to obtain
2817189592Sbms	 * a pointer to the group record after m_append(), in case a new
2818189592Sbms	 * mbuf was allocated.
2819189592Sbms	 * Only append sources which are in-mode at t1. If we are
2820189592Sbms	 * transitioning to MCAST_UNDEFINED state on the group, do not
2821189592Sbms	 * include source entries.
2822189592Sbms	 * Only report recorded sources in our filter set when responding
2823189592Sbms	 * to a group-source query.
2824189592Sbms	 */
2825189592Sbms	if (record_has_sources) {
2826189592Sbms		if (m == m0) {
2827189592Sbms			md = m_last(m);
2828189592Sbms			pig = (struct igmp_grouprec *)(mtod(md, uint8_t *) +
2829189592Sbms			    md->m_len - nbytes);
2830189592Sbms		} else {
2831189592Sbms			md = m_getptr(m, 0, &off);
2832189592Sbms			pig = (struct igmp_grouprec *)(mtod(md, uint8_t *) +
2833189592Sbms			    off);
2834189592Sbms		}
2835189592Sbms		msrcs = 0;
2836189592Sbms		RB_FOREACH_SAFE(ims, ip_msource_tree, &inm->inm_srcs, nims) {
2837189592Sbms			CTR2(KTR_IGMPV3, "%s: visit node %s", __func__,
2838189592Sbms			    inet_ntoa_haddr(ims->ims_haddr));
2839189592Sbms			now = ims_get_mode(inm, ims, 1);
2840189592Sbms			CTR2(KTR_IGMPV3, "%s: node is %d", __func__, now);
2841189592Sbms			if ((now != mode) ||
2842189592Sbms			    (now == mode && mode == MCAST_UNDEFINED)) {
2843189592Sbms				CTR1(KTR_IGMPV3, "%s: skip node", __func__);
2844189592Sbms				continue;
2845189592Sbms			}
2846189592Sbms			if (is_source_query && ims->ims_stp == 0) {
2847189592Sbms				CTR1(KTR_IGMPV3, "%s: skip unrecorded node",
2848189592Sbms				    __func__);
2849189592Sbms				continue;
2850189592Sbms			}
2851189592Sbms			CTR1(KTR_IGMPV3, "%s: append node", __func__);
2852189592Sbms			naddr = htonl(ims->ims_haddr);
2853189592Sbms			if (!m_append(m, sizeof(in_addr_t), (void *)&naddr)) {
2854189592Sbms				if (m != m0)
2855189592Sbms					m_freem(m);
2856189592Sbms				CTR1(KTR_IGMPV3, "%s: m_append() failed.",
2857189592Sbms				    __func__);
2858189592Sbms				return (-ENOMEM);
2859189592Sbms			}
2860189592Sbms			nbytes += sizeof(in_addr_t);
2861189592Sbms			++msrcs;
2862189592Sbms			if (msrcs == m0srcs)
2863189592Sbms				break;
2864189592Sbms		}
2865189592Sbms		CTR2(KTR_IGMPV3, "%s: msrcs is %d this packet", __func__,
2866189592Sbms		    msrcs);
2867189592Sbms		pig->ig_numsrc = htons(msrcs);
2868189592Sbms		nbytes += (msrcs * sizeof(in_addr_t));
2869189592Sbms	}
2870189592Sbms
2871189592Sbms	if (is_source_query && msrcs == 0) {
2872189592Sbms		CTR1(KTR_IGMPV3, "%s: no recorded sources to report", __func__);
2873189592Sbms		if (m != m0)
2874189592Sbms			m_freem(m);
2875189592Sbms		return (0);
2876189592Sbms	}
2877189592Sbms
2878189592Sbms	/*
2879189592Sbms	 * We are good to go with first packet.
2880189592Sbms	 */
2881189592Sbms	if (m != m0) {
2882189592Sbms		CTR1(KTR_IGMPV3, "%s: enqueueing first packet", __func__);
2883189592Sbms		m->m_pkthdr.PH_vt.vt_nrecs = 1;
2884189592Sbms		_IF_ENQUEUE(ifq, m);
2885189592Sbms	} else
2886189592Sbms		m->m_pkthdr.PH_vt.vt_nrecs++;
2887189592Sbms
2888189592Sbms	/*
2889189592Sbms	 * No further work needed if no source list in packet(s).
2890189592Sbms	 */
2891189592Sbms	if (!record_has_sources)
2892189592Sbms		return (nbytes);
2893189592Sbms
2894189592Sbms	/*
2895189592Sbms	 * Whilst sources remain to be announced, we need to allocate
2896189592Sbms	 * a new packet and fill out as many sources as will fit.
2897189592Sbms	 * Always try for a cluster first.
2898189592Sbms	 */
2899189592Sbms	while (nims != NULL) {
2900189592Sbms		if (_IF_QFULL(ifq)) {
2901189592Sbms			CTR1(KTR_IGMPV3, "%s: outbound queue full", __func__);
2902189592Sbms			return (-ENOMEM);
2903189592Sbms		}
2904243882Sglebius		m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
2905190692Sbms		if (m)
2906190692Sbms			m->m_data += IGMP_LEADINGSPACE;
2907189592Sbms		if (m == NULL) {
2908243882Sglebius			m = m_gethdr(M_NOWAIT, MT_DATA);
2909189592Sbms			if (m)
2910189592Sbms				MH_ALIGN(m, IGMP_LEADINGSPACE);
2911189592Sbms		}
2912189592Sbms		if (m == NULL)
2913189592Sbms			return (-ENOMEM);
2914189592Sbms		igmp_save_context(m, ifp);
2915189592Sbms		md = m_getptr(m, 0, &off);
2916189592Sbms		pig = (struct igmp_grouprec *)(mtod(md, uint8_t *) + off);
2917189592Sbms		CTR1(KTR_IGMPV3, "%s: allocated next packet", __func__);
2918189592Sbms
2919189592Sbms		if (!m_append(m, sizeof(struct igmp_grouprec), (void *)&ig)) {
2920189592Sbms			if (m != m0)
2921189592Sbms				m_freem(m);
2922189592Sbms			CTR1(KTR_IGMPV3, "%s: m_append() failed.", __func__);
2923189592Sbms			return (-ENOMEM);
2924189592Sbms		}
2925189592Sbms		m->m_pkthdr.PH_vt.vt_nrecs = 1;
2926189592Sbms		nbytes += sizeof(struct igmp_grouprec);
2927189592Sbms
2928189592Sbms		m0srcs = (ifp->if_mtu - IGMP_LEADINGSPACE -
2929189592Sbms		    sizeof(struct igmp_grouprec)) / sizeof(in_addr_t);
2930189592Sbms
2931189592Sbms		msrcs = 0;
2932189592Sbms		RB_FOREACH_FROM(ims, ip_msource_tree, nims) {
2933189592Sbms			CTR2(KTR_IGMPV3, "%s: visit node %s", __func__,
2934189592Sbms			    inet_ntoa_haddr(ims->ims_haddr));
2935189592Sbms			now = ims_get_mode(inm, ims, 1);
2936189592Sbms			if ((now != mode) ||
2937189592Sbms			    (now == mode && mode == MCAST_UNDEFINED)) {
2938189592Sbms				CTR1(KTR_IGMPV3, "%s: skip node", __func__);
2939189592Sbms				continue;
2940189592Sbms			}
2941189592Sbms			if (is_source_query && ims->ims_stp == 0) {
2942189592Sbms				CTR1(KTR_IGMPV3, "%s: skip unrecorded node",
2943189592Sbms				    __func__);
2944189592Sbms				continue;
2945189592Sbms			}
2946189592Sbms			CTR1(KTR_IGMPV3, "%s: append node", __func__);
2947189592Sbms			naddr = htonl(ims->ims_haddr);
2948189592Sbms			if (!m_append(m, sizeof(in_addr_t), (void *)&naddr)) {
2949189592Sbms				if (m != m0)
2950189592Sbms					m_freem(m);
2951189592Sbms				CTR1(KTR_IGMPV3, "%s: m_append() failed.",
2952189592Sbms				    __func__);
2953189592Sbms				return (-ENOMEM);
2954189592Sbms			}
2955189592Sbms			++msrcs;
2956189592Sbms			if (msrcs == m0srcs)
2957189592Sbms				break;
2958189592Sbms		}
2959189592Sbms		pig->ig_numsrc = htons(msrcs);
2960189592Sbms		nbytes += (msrcs * sizeof(in_addr_t));
2961189592Sbms
2962189592Sbms		CTR1(KTR_IGMPV3, "%s: enqueueing next packet", __func__);
2963189592Sbms		_IF_ENQUEUE(ifq, m);
2964189592Sbms	}
2965189592Sbms
2966189592Sbms	return (nbytes);
2967189592Sbms}
2968189592Sbms
2969189592Sbms/*
2970189592Sbms * Type used to mark record pass completion.
2971189592Sbms * We exploit the fact we can cast to this easily from the
2972189592Sbms * current filter modes on each ip_msource node.
2973189592Sbms */
2974189592Sbmstypedef enum {
2975189592Sbms	REC_NONE = 0x00,	/* MCAST_UNDEFINED */
2976189592Sbms	REC_ALLOW = 0x01,	/* MCAST_INCLUDE */
2977189592Sbms	REC_BLOCK = 0x02,	/* MCAST_EXCLUDE */
2978189592Sbms	REC_FULL = REC_ALLOW | REC_BLOCK
2979189592Sbms} rectype_t;
2980189592Sbms
2981189592Sbms/*
2982189592Sbms * Enqueue an IGMPv3 filter list change to the given output queue.
2983189592Sbms *
2984189592Sbms * Source list filter state is held in an RB-tree. When the filter list
2985189592Sbms * for a group is changed without changing its mode, we need to compute
2986189592Sbms * the deltas between T0 and T1 for each source in the filter set,
2987189592Sbms * and enqueue the appropriate ALLOW_NEW/BLOCK_OLD records.
2988189592Sbms *
2989189592Sbms * As we may potentially queue two record types, and the entire R-B tree
2990189592Sbms * needs to be walked at once, we break this out into its own function
2991189592Sbms * so we can generate a tightly packed queue of packets.
2992189592Sbms *
2993189592Sbms * XXX This could be written to only use one tree walk, although that makes
2994189592Sbms * serializing into the mbuf chains a bit harder. For now we do two walks
2995189592Sbms * which makes things easier on us, and it may or may not be harder on
2996189592Sbms * the L2 cache.
2997189592Sbms *
2998189592Sbms * If successful the size of all data appended to the queue is returned,
2999189592Sbms * otherwise an error code less than zero is returned, or zero if
3000189592Sbms * no record(s) were appended.
3001189592Sbms */
3002189592Sbmsstatic int
3003189592Sbmsigmp_v3_enqueue_filter_change(struct ifqueue *ifq, struct in_multi *inm)
3004189592Sbms{
3005189592Sbms	static const int MINRECLEN =
3006189592Sbms	    sizeof(struct igmp_grouprec) + sizeof(in_addr_t);
3007189592Sbms	struct ifnet		*ifp;
3008189592Sbms	struct igmp_grouprec	 ig;
3009189592Sbms	struct igmp_grouprec	*pig;
3010189592Sbms	struct ip_msource	*ims, *nims;
3011189592Sbms	struct mbuf		*m, *m0, *md;
3012189592Sbms	in_addr_t		 naddr;
3013191657Sbms	int			 m0srcs, nbytes, npbytes, off, rsrcs, schanged;
3014189592Sbms	int			 nallow, nblock;
3015189592Sbms	uint8_t			 mode, now, then;
3016189592Sbms	rectype_t		 crt, drt, nrt;
3017189592Sbms
3018189592Sbms	IN_MULTI_LOCK_ASSERT();
3019189592Sbms
3020189592Sbms	if (inm->inm_nsrc == 0 ||
3021189592Sbms	    (inm->inm_st[0].iss_asm > 0 && inm->inm_st[1].iss_asm > 0))
3022189592Sbms		return (0);
3023189592Sbms
3024189592Sbms	ifp = inm->inm_ifp;			/* interface */
3025189592Sbms	mode = inm->inm_st[1].iss_fmode;	/* filter mode at t1 */
3026189592Sbms	crt = REC_NONE;	/* current group record type */
3027189592Sbms	drt = REC_NONE;	/* mask of completed group record types */
3028189592Sbms	nrt = REC_NONE;	/* record type for current node */
3029189592Sbms	m0srcs = 0;	/* # source which will fit in current mbuf chain */
3030189592Sbms	nbytes = 0;	/* # of bytes appended to group's state-change queue */
3031191657Sbms	npbytes = 0;	/* # of bytes appended this packet */
3032189592Sbms	rsrcs = 0;	/* # sources encoded in current record */
3033189592Sbms	schanged = 0;	/* # nodes encoded in overall filter change */
3034189592Sbms	nallow = 0;	/* # of source entries in ALLOW_NEW */
3035189592Sbms	nblock = 0;	/* # of source entries in BLOCK_OLD */
3036189592Sbms	nims = NULL;	/* next tree node pointer */
3037189592Sbms
3038189592Sbms	/*
3039189592Sbms	 * For each possible filter record mode.
3040189592Sbms	 * The first kind of source we encounter tells us which
3041189592Sbms	 * is the first kind of record we start appending.
3042189592Sbms	 * If a node transitioned to UNDEFINED at t1, its mode is treated
3043189592Sbms	 * as the inverse of the group's filter mode.
3044189592Sbms	 */
3045189592Sbms	while (drt != REC_FULL) {
3046189592Sbms		do {
3047189592Sbms			m0 = ifq->ifq_tail;
3048189592Sbms			if (m0 != NULL &&
3049189592Sbms			    (m0->m_pkthdr.PH_vt.vt_nrecs + 1 <=
3050189592Sbms			     IGMP_V3_REPORT_MAXRECS) &&
3051189592Sbms			    (m0->m_pkthdr.len + MINRECLEN) <
3052189592Sbms			     (ifp->if_mtu - IGMP_LEADINGSPACE)) {
3053189592Sbms				m = m0;
3054189592Sbms				m0srcs = (ifp->if_mtu - m0->m_pkthdr.len -
3055189592Sbms					    sizeof(struct igmp_grouprec)) /
3056189592Sbms				    sizeof(in_addr_t);
3057189592Sbms				CTR1(KTR_IGMPV3,
3058189592Sbms				    "%s: use previous packet", __func__);
3059189592Sbms			} else {
3060243882Sglebius				m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
3061190692Sbms				if (m)
3062190692Sbms					m->m_data += IGMP_LEADINGSPACE;
3063189592Sbms				if (m == NULL) {
3064243882Sglebius					m = m_gethdr(M_NOWAIT, MT_DATA);
3065189592Sbms					if (m)
3066189592Sbms						MH_ALIGN(m, IGMP_LEADINGSPACE);
3067189592Sbms				}
3068189592Sbms				if (m == NULL) {
3069189592Sbms					CTR1(KTR_IGMPV3,
3070189592Sbms					    "%s: m_get*() failed", __func__);
3071189592Sbms					return (-ENOMEM);
3072189592Sbms				}
3073189592Sbms				m->m_pkthdr.PH_vt.vt_nrecs = 0;
3074189592Sbms				igmp_save_context(m, ifp);
3075189592Sbms				m0srcs = (ifp->if_mtu - IGMP_LEADINGSPACE -
3076189592Sbms				    sizeof(struct igmp_grouprec)) /
3077189592Sbms				    sizeof(in_addr_t);
3078191657Sbms				npbytes = 0;
3079189592Sbms				CTR1(KTR_IGMPV3,
3080189592Sbms				    "%s: allocated new packet", __func__);
3081189592Sbms			}
3082189592Sbms			/*
3083189592Sbms			 * Append the IGMP group record header to the
3084189592Sbms			 * current packet's data area.
3085189592Sbms			 * Recalculate pointer to free space for next
3086189592Sbms			 * group record, in case m_append() allocated
3087189592Sbms			 * a new mbuf or cluster.
3088189592Sbms			 */
3089189592Sbms			memset(&ig, 0, sizeof(ig));
3090189592Sbms			ig.ig_group = inm->inm_addr;
3091189592Sbms			if (!m_append(m, sizeof(ig), (void *)&ig)) {
3092189592Sbms				if (m != m0)
3093189592Sbms					m_freem(m);
3094189592Sbms				CTR1(KTR_IGMPV3,
3095189592Sbms				    "%s: m_append() failed", __func__);
3096189592Sbms				return (-ENOMEM);
3097189592Sbms			}
3098191657Sbms			npbytes += sizeof(struct igmp_grouprec);
3099191657Sbms			if (m != m0) {
3100191657Sbms				/* new packet; offset in c hain */
3101191657Sbms				md = m_getptr(m, npbytes -
3102191657Sbms				    sizeof(struct igmp_grouprec), &off);
3103189592Sbms				pig = (struct igmp_grouprec *)(mtod(md,
3104191657Sbms				    uint8_t *) + off);
3105189592Sbms			} else {
3106191657Sbms				/* current packet; offset from last append */
3107191657Sbms				md = m_last(m);
3108189592Sbms				pig = (struct igmp_grouprec *)(mtod(md,
3109191657Sbms				    uint8_t *) + md->m_len -
3110191657Sbms				    sizeof(struct igmp_grouprec));
3111189592Sbms			}
3112189592Sbms			/*
3113189592Sbms			 * Begin walking the tree for this record type
3114189592Sbms			 * pass, or continue from where we left off
3115189592Sbms			 * previously if we had to allocate a new packet.
3116189592Sbms			 * Only report deltas in-mode at t1.
3117189592Sbms			 * We need not report included sources as allowed
3118189592Sbms			 * if we are in inclusive mode on the group,
3119189592Sbms			 * however the converse is not true.
3120189592Sbms			 */
3121189592Sbms			rsrcs = 0;
3122189592Sbms			if (nims == NULL)
3123189592Sbms				nims = RB_MIN(ip_msource_tree, &inm->inm_srcs);
3124189592Sbms			RB_FOREACH_FROM(ims, ip_msource_tree, nims) {
3125189592Sbms				CTR2(KTR_IGMPV3, "%s: visit node %s",
3126189592Sbms				    __func__, inet_ntoa_haddr(ims->ims_haddr));
3127189592Sbms				now = ims_get_mode(inm, ims, 1);
3128189592Sbms				then = ims_get_mode(inm, ims, 0);
3129189592Sbms				CTR3(KTR_IGMPV3, "%s: mode: t0 %d, t1 %d",
3130189592Sbms				    __func__, then, now);
3131189592Sbms				if (now == then) {
3132189592Sbms					CTR1(KTR_IGMPV3,
3133189592Sbms					    "%s: skip unchanged", __func__);
3134189592Sbms					continue;
3135189592Sbms				}
3136189592Sbms				if (mode == MCAST_EXCLUDE &&
3137189592Sbms				    now == MCAST_INCLUDE) {
3138189592Sbms					CTR1(KTR_IGMPV3,
3139189592Sbms					    "%s: skip IN src on EX group",
3140189592Sbms					    __func__);
3141189592Sbms					continue;
3142189592Sbms				}
3143189592Sbms				nrt = (rectype_t)now;
3144189592Sbms				if (nrt == REC_NONE)
3145189592Sbms					nrt = (rectype_t)(~mode & REC_FULL);
3146189592Sbms				if (schanged++ == 0) {
3147189592Sbms					crt = nrt;
3148189592Sbms				} else if (crt != nrt)
3149189592Sbms					continue;
3150189592Sbms				naddr = htonl(ims->ims_haddr);
3151189592Sbms				if (!m_append(m, sizeof(in_addr_t),
3152189592Sbms				    (void *)&naddr)) {
3153189592Sbms					if (m != m0)
3154189592Sbms						m_freem(m);
3155189592Sbms					CTR1(KTR_IGMPV3,
3156189592Sbms					    "%s: m_append() failed", __func__);
3157189592Sbms					return (-ENOMEM);
3158189592Sbms				}
3159189592Sbms				nallow += !!(crt == REC_ALLOW);
3160189592Sbms				nblock += !!(crt == REC_BLOCK);
3161189592Sbms				if (++rsrcs == m0srcs)
3162189592Sbms					break;
3163189592Sbms			}
3164189592Sbms			/*
3165189592Sbms			 * If we did not append any tree nodes on this
3166189592Sbms			 * pass, back out of allocations.
3167189592Sbms			 */
3168189592Sbms			if (rsrcs == 0) {
3169191657Sbms				npbytes -= sizeof(struct igmp_grouprec);
3170189592Sbms				if (m != m0) {
3171189592Sbms					CTR1(KTR_IGMPV3,
3172189592Sbms					    "%s: m_free(m)", __func__);
3173189592Sbms					m_freem(m);
3174189592Sbms				} else {
3175189592Sbms					CTR1(KTR_IGMPV3,
3176189592Sbms					    "%s: m_adj(m, -ig)", __func__);
3177189592Sbms					m_adj(m, -((int)sizeof(
3178189592Sbms					    struct igmp_grouprec)));
3179189592Sbms				}
3180189592Sbms				continue;
3181189592Sbms			}
3182191657Sbms			npbytes += (rsrcs * sizeof(in_addr_t));
3183189592Sbms			if (crt == REC_ALLOW)
3184189592Sbms				pig->ig_type = IGMP_ALLOW_NEW_SOURCES;
3185189592Sbms			else if (crt == REC_BLOCK)
3186189592Sbms				pig->ig_type = IGMP_BLOCK_OLD_SOURCES;
3187189592Sbms			pig->ig_numsrc = htons(rsrcs);
3188189592Sbms			/*
3189189592Sbms			 * Count the new group record, and enqueue this
3190189592Sbms			 * packet if it wasn't already queued.
3191189592Sbms			 */
3192189592Sbms			m->m_pkthdr.PH_vt.vt_nrecs++;
3193189592Sbms			if (m != m0)
3194189592Sbms				_IF_ENQUEUE(ifq, m);
3195191657Sbms			nbytes += npbytes;
3196189592Sbms		} while (nims != NULL);
3197189592Sbms		drt |= crt;
3198189592Sbms		crt = (~crt & REC_FULL);
3199189592Sbms	}
3200189592Sbms
3201189592Sbms	CTR3(KTR_IGMPV3, "%s: queued %d ALLOW_NEW, %d BLOCK_OLD", __func__,
3202189592Sbms	    nallow, nblock);
3203189592Sbms
3204189592Sbms	return (nbytes);
3205189592Sbms}
3206189592Sbms
3207189592Sbmsstatic int
3208189592Sbmsigmp_v3_merge_state_changes(struct in_multi *inm, struct ifqueue *ifscq)
3209189592Sbms{
3210189592Sbms	struct ifqueue	*gq;
3211189592Sbms	struct mbuf	*m;		/* pending state-change */
3212189592Sbms	struct mbuf	*m0;		/* copy of pending state-change */
3213189592Sbms	struct mbuf	*mt;		/* last state-change in packet */
3214189592Sbms	int		 docopy, domerge;
3215189592Sbms	u_int		 recslen;
3216189592Sbms
3217189592Sbms	docopy = 0;
3218189592Sbms	domerge = 0;
3219189592Sbms	recslen = 0;
3220189592Sbms
3221189592Sbms	IN_MULTI_LOCK_ASSERT();
3222189592Sbms	IGMP_LOCK_ASSERT();
3223189592Sbms
3224189592Sbms	/*
3225189592Sbms	 * If there are further pending retransmissions, make a writable
3226189592Sbms	 * copy of each queued state-change message before merging.
3227189592Sbms	 */
3228189592Sbms	if (inm->inm_scrv > 0)
3229189592Sbms		docopy = 1;
3230189592Sbms
3231189592Sbms	gq = &inm->inm_scq;
3232189592Sbms#ifdef KTR
3233189592Sbms	if (gq->ifq_head == NULL) {
3234189592Sbms		CTR2(KTR_IGMPV3, "%s: WARNING: queue for inm %p is empty",
3235189592Sbms		    __func__, inm);
3236189592Sbms	}
3237189592Sbms#endif
3238189592Sbms
3239189592Sbms	m = gq->ifq_head;
3240189592Sbms	while (m != NULL) {
3241189592Sbms		/*
3242189592Sbms		 * Only merge the report into the current packet if
3243189592Sbms		 * there is sufficient space to do so; an IGMPv3 report
3244189592Sbms		 * packet may only contain 65,535 group records.
3245189592Sbms		 * Always use a simple mbuf chain concatentation to do this,
3246189592Sbms		 * as large state changes for single groups may have
3247189592Sbms		 * allocated clusters.
3248189592Sbms		 */
3249189592Sbms		domerge = 0;
3250189592Sbms		mt = ifscq->ifq_tail;
3251189592Sbms		if (mt != NULL) {
3252189592Sbms			recslen = m_length(m, NULL);
3253189592Sbms
3254189592Sbms			if ((mt->m_pkthdr.PH_vt.vt_nrecs +
3255189592Sbms			    m->m_pkthdr.PH_vt.vt_nrecs <=
3256189592Sbms			    IGMP_V3_REPORT_MAXRECS) &&
3257189592Sbms			    (mt->m_pkthdr.len + recslen <=
3258189592Sbms			    (inm->inm_ifp->if_mtu - IGMP_LEADINGSPACE)))
3259189592Sbms				domerge = 1;
3260189592Sbms		}
3261189592Sbms
3262189592Sbms		if (!domerge && _IF_QFULL(gq)) {
3263189592Sbms			CTR2(KTR_IGMPV3,
3264189592Sbms			    "%s: outbound queue full, skipping whole packet %p",
3265189592Sbms			    __func__, m);
3266189592Sbms			mt = m->m_nextpkt;
3267189592Sbms			if (!docopy)
3268189592Sbms				m_freem(m);
3269189592Sbms			m = mt;
3270189592Sbms			continue;
3271189592Sbms		}
3272189592Sbms
3273189592Sbms		if (!docopy) {
3274189592Sbms			CTR2(KTR_IGMPV3, "%s: dequeueing %p", __func__, m);
3275189592Sbms			_IF_DEQUEUE(gq, m0);
3276189592Sbms			m = m0->m_nextpkt;
3277189592Sbms		} else {
3278189592Sbms			CTR2(KTR_IGMPV3, "%s: copying %p", __func__, m);
3279189592Sbms			m0 = m_dup(m, M_NOWAIT);
3280189592Sbms			if (m0 == NULL)
3281189592Sbms				return (ENOMEM);
3282189592Sbms			m0->m_nextpkt = NULL;
3283189592Sbms			m = m->m_nextpkt;
3284189592Sbms		}
3285189592Sbms
3286189592Sbms		if (!domerge) {
3287189592Sbms			CTR3(KTR_IGMPV3, "%s: queueing %p to ifscq %p)",
3288189592Sbms			    __func__, m0, ifscq);
3289189592Sbms			_IF_ENQUEUE(ifscq, m0);
3290189592Sbms		} else {
3291189592Sbms			struct mbuf *mtl;	/* last mbuf of packet mt */
3292189592Sbms
3293189592Sbms			CTR3(KTR_IGMPV3, "%s: merging %p with ifscq tail %p)",
3294189592Sbms			    __func__, m0, mt);
3295189592Sbms
3296189592Sbms			mtl = m_last(mt);
3297189592Sbms			m0->m_flags &= ~M_PKTHDR;
3298189592Sbms			mt->m_pkthdr.len += recslen;
3299189592Sbms			mt->m_pkthdr.PH_vt.vt_nrecs +=
3300189592Sbms			    m0->m_pkthdr.PH_vt.vt_nrecs;
3301189592Sbms
3302189592Sbms			mtl->m_next = m0;
3303189592Sbms		}
3304189592Sbms	}
3305189592Sbms
3306189592Sbms	return (0);
3307189592Sbms}
3308189592Sbms
3309189592Sbms/*
3310189592Sbms * Respond to a pending IGMPv3 General Query.
3311189592Sbms */
3312189592Sbmsstatic void
3313189592Sbmsigmp_v3_dispatch_general_query(struct igmp_ifinfo *igi)
3314189592Sbms{
3315229390Sjhb	struct ifmultiaddr	*ifma;
3316189592Sbms	struct ifnet		*ifp;
3317189592Sbms	struct in_multi		*inm;
3318189592Sbms	int			 retval, loop;
3319189592Sbms
3320189592Sbms	IN_MULTI_LOCK_ASSERT();
3321189592Sbms	IGMP_LOCK_ASSERT();
3322189592Sbms
3323189592Sbms	KASSERT(igi->igi_version == IGMP_VERSION_3,
3324189592Sbms	    ("%s: called when version %d", __func__, igi->igi_version));
3325189592Sbms
3326291990Sae	/*
3327291990Sae	 * Check that there are some packets queued. If so, send them first.
3328291990Sae	 * For large number of groups the reply to general query can take
3329291990Sae	 * many packets, we should finish sending them before starting of
3330291990Sae	 * queuing the new reply.
3331291990Sae	 */
3332291990Sae	if (igi->igi_gq.ifq_head != NULL)
3333291990Sae		goto send;
3334291990Sae
3335189592Sbms	ifp = igi->igi_ifp;
3336189592Sbms
3337229621Sjhb	IF_ADDR_RLOCK(ifp);
3338229390Sjhb	TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
3339189931Sbms		if (ifma->ifma_addr->sa_family != AF_INET ||
3340189931Sbms		    ifma->ifma_protospec == NULL)
3341189592Sbms			continue;
3342189592Sbms
3343189592Sbms		inm = (struct in_multi *)ifma->ifma_protospec;
3344189592Sbms		KASSERT(ifp == inm->inm_ifp,
3345189592Sbms		    ("%s: inconsistent ifp", __func__));
3346189592Sbms
3347189592Sbms		switch (inm->inm_state) {
3348189592Sbms		case IGMP_NOT_MEMBER:
3349189592Sbms		case IGMP_SILENT_MEMBER:
3350189592Sbms			break;
3351189592Sbms		case IGMP_REPORTING_MEMBER:
3352189592Sbms		case IGMP_IDLE_MEMBER:
3353189592Sbms		case IGMP_LAZY_MEMBER:
3354189592Sbms		case IGMP_SLEEPING_MEMBER:
3355189592Sbms		case IGMP_AWAKENING_MEMBER:
3356189592Sbms			inm->inm_state = IGMP_REPORTING_MEMBER;
3357189592Sbms			retval = igmp_v3_enqueue_group_record(&igi->igi_gq,
3358189592Sbms			    inm, 0, 0, 0);
3359189592Sbms			CTR2(KTR_IGMPV3, "%s: enqueue record = %d",
3360189592Sbms			    __func__, retval);
3361189592Sbms			break;
3362189592Sbms		case IGMP_G_QUERY_PENDING_MEMBER:
3363189592Sbms		case IGMP_SG_QUERY_PENDING_MEMBER:
3364189592Sbms		case IGMP_LEAVING_MEMBER:
3365189592Sbms			break;
3366189592Sbms		}
3367189592Sbms	}
3368229621Sjhb	IF_ADDR_RUNLOCK(ifp);
3369189592Sbms
3370291990Saesend:
3371189592Sbms	loop = (igi->igi_flags & IGIF_LOOPBACK) ? 1 : 0;
3372189592Sbms	igmp_dispatch_queue(&igi->igi_gq, IGMP_MAX_RESPONSE_BURST, loop);
3373189592Sbms
3374189592Sbms	/*
3375189592Sbms	 * Slew transmission of bursts over 500ms intervals.
3376189592Sbms	 */
3377189592Sbms	if (igi->igi_gq.ifq_head != NULL) {
3378189592Sbms		igi->igi_v3_timer = 1 + IGMP_RANDOM_DELAY(
3379189592Sbms		    IGMP_RESPONSE_BURST_INTERVAL);
3380189592Sbms		V_interface_timers_running = 1;
3381189592Sbms	}
3382189592Sbms}
3383189592Sbms
3384189592Sbms/*
3385189592Sbms * Transmit the next pending IGMP message in the output queue.
3386189592Sbms *
3387189592Sbms * We get called from netisr_processqueue(). A mutex private to igmpoq
3388189592Sbms * will be acquired and released around this routine.
3389189592Sbms *
3390189592Sbms * VIMAGE: Needs to store/restore vnet pointer on a per-mbuf-chain basis.
3391189592Sbms * MRT: Nothing needs to be done, as IGMP traffic is always local to
3392189592Sbms * a link and uses a link-scope multicast address.
3393189592Sbms */
3394189592Sbmsstatic void
3395189592Sbmsigmp_intr(struct mbuf *m)
3396189592Sbms{
3397189592Sbms	struct ip_moptions	 imo;
3398189592Sbms	struct ifnet		*ifp;
3399189592Sbms	struct mbuf		*ipopts, *m0;
3400189592Sbms	int			 error;
3401189592Sbms	uint32_t		 ifindex;
3402189592Sbms
3403189592Sbms	CTR2(KTR_IGMPV3, "%s: transmit %p", __func__, m);
3404189592Sbms
3405189592Sbms	/*
3406191548Szec	 * Set VNET image pointer from enqueued mbuf chain
3407189592Sbms	 * before doing anything else. Whilst we use interface
3408189592Sbms	 * indexes to guard against interface detach, they are
3409189592Sbms	 * unique to each VIMAGE and must be retrieved.
3410189592Sbms	 */
3411254804Sandre	CURVNET_SET((struct vnet *)(m->m_pkthdr.PH_loc.ptr));
3412189592Sbms	ifindex = igmp_restore_context(m);
3413189592Sbms
3414189592Sbms	/*
3415189592Sbms	 * Check if the ifnet still exists. This limits the scope of
3416189592Sbms	 * any race in the absence of a global ifp lock for low cost
3417189592Sbms	 * (an array lookup).
3418189592Sbms	 */
3419189592Sbms	ifp = ifnet_byindex(ifindex);
3420189592Sbms	if (ifp == NULL) {
3421189592Sbms		CTR3(KTR_IGMPV3, "%s: dropped %p as ifindex %u went away.",
3422189592Sbms		    __func__, m, ifindex);
3423189592Sbms		m_freem(m);
3424190951Srwatson		IPSTAT_INC(ips_noroute);
3425189592Sbms		goto out;
3426189592Sbms	}
3427189592Sbms
3428189592Sbms	ipopts = V_igmp_sendra ? m_raopt : NULL;
3429189592Sbms
3430119181Srwatson	imo.imo_multicast_ttl  = 1;
343115292Swollman	imo.imo_multicast_vif  = -1;
3432181803Sbz	imo.imo_multicast_loop = (V_ip_mrouter != NULL);
34331541Srgrimes
343415292Swollman	/*
3435189592Sbms	 * If the user requested that IGMP traffic be explicitly
3436189592Sbms	 * redirected to the loopback interface (e.g. they are running a
3437189592Sbms	 * MANET interface and the routing protocol needs to see the
3438189592Sbms	 * updates), handle this now.
343915292Swollman	 */
3440189592Sbms	if (m->m_flags & M_IGMP_LOOP)
3441189592Sbms		imo.imo_multicast_ifp = V_loif;
3442189592Sbms	else
3443189592Sbms		imo.imo_multicast_ifp = ifp;
34442531Swollman
3445189592Sbms	if (m->m_flags & M_IGMPV2) {
3446189592Sbms		m0 = m;
3447189592Sbms	} else {
3448189592Sbms		m0 = igmp_v3_encap_report(ifp, m);
3449189592Sbms		if (m0 == NULL) {
3450189592Sbms			CTR2(KTR_IGMPV3, "%s: dropped %p", __func__, m);
3451189592Sbms			m_freem(m);
3452190951Srwatson			IPSTAT_INC(ips_odropped);
3453189592Sbms			goto out;
3454189592Sbms		}
3455189592Sbms	}
3456189592Sbms
3457189592Sbms	igmp_scrub_context(m0);
3458254523Sandre	m_clrprotoflags(m);
3459189592Sbms	m0->m_pkthdr.rcvif = V_loif;
3460189592Sbms#ifdef MAC
3461189592Sbms	mac_netinet_igmp_send(ifp, m0);
3462189592Sbms#endif
3463189592Sbms	error = ip_output(m0, ipopts, NULL, 0, &imo, NULL);
3464189592Sbms	if (error) {
3465189592Sbms		CTR3(KTR_IGMPV3, "%s: ip_output(%p) = %d", __func__, m0, error);
3466189592Sbms		goto out;
3467189592Sbms	}
3468189592Sbms
3469190965Srwatson	IGMPSTAT_INC(igps_snd_reports);
3470189592Sbms
3471189592Sbmsout:
3472189592Sbms	/*
3473189592Sbms	 * We must restore the existing vnet pointer before
3474189592Sbms	 * continuing as we are run from netisr context.
3475189592Sbms	 */
3476189592Sbms	CURVNET_RESTORE();
34771541Srgrimes}
3478189592Sbms
3479189592Sbms/*
3480189592Sbms * Encapsulate an IGMPv3 report.
3481189592Sbms *
3482189592Sbms * The internal mbuf flag M_IGMPV3_HDR is used to indicate that the mbuf
3483189592Sbms * chain has already had its IP/IGMPv3 header prepended. In this case
3484189592Sbms * the function will not attempt to prepend; the lengths and checksums
3485189592Sbms * will however be re-computed.
3486189592Sbms *
3487189592Sbms * Returns a pointer to the new mbuf chain head, or NULL if the
3488189592Sbms * allocation failed.
3489189592Sbms */
3490189592Sbmsstatic struct mbuf *
3491189592Sbmsigmp_v3_encap_report(struct ifnet *ifp, struct mbuf *m)
3492189592Sbms{
3493189592Sbms	struct igmp_report	*igmp;
3494189592Sbms	struct ip		*ip;
3495189592Sbms	int			 hdrlen, igmpreclen;
3496189592Sbms
3497189592Sbms	KASSERT((m->m_flags & M_PKTHDR),
3498189592Sbms	    ("%s: mbuf chain %p is !M_PKTHDR", __func__, m));
3499189592Sbms
3500189592Sbms	igmpreclen = m_length(m, NULL);
3501189592Sbms	hdrlen = sizeof(struct ip) + sizeof(struct igmp_report);
3502189592Sbms
3503189592Sbms	if (m->m_flags & M_IGMPV3_HDR) {
3504189592Sbms		igmpreclen -= hdrlen;
3505189592Sbms	} else {
3506243882Sglebius		M_PREPEND(m, hdrlen, M_NOWAIT);
3507189592Sbms		if (m == NULL)
3508189592Sbms			return (NULL);
3509189592Sbms		m->m_flags |= M_IGMPV3_HDR;
3510189592Sbms	}
3511189592Sbms
3512189592Sbms	CTR2(KTR_IGMPV3, "%s: igmpreclen is %d", __func__, igmpreclen);
3513189592Sbms
3514189592Sbms	m->m_data += sizeof(struct ip);
3515189592Sbms	m->m_len -= sizeof(struct ip);
3516189592Sbms
3517189592Sbms	igmp = mtod(m, struct igmp_report *);
3518189592Sbms	igmp->ir_type = IGMP_v3_HOST_MEMBERSHIP_REPORT;
3519189592Sbms	igmp->ir_rsv1 = 0;
3520189592Sbms	igmp->ir_rsv2 = 0;
3521189592Sbms	igmp->ir_numgrps = htons(m->m_pkthdr.PH_vt.vt_nrecs);
3522189592Sbms	igmp->ir_cksum = 0;
3523189592Sbms	igmp->ir_cksum = in_cksum(m, sizeof(struct igmp_report) + igmpreclen);
3524189592Sbms	m->m_pkthdr.PH_vt.vt_nrecs = 0;
3525189592Sbms
3526189592Sbms	m->m_data -= sizeof(struct ip);
3527189592Sbms	m->m_len += sizeof(struct ip);
3528189592Sbms
3529189592Sbms	ip = mtod(m, struct ip *);
3530189592Sbms	ip->ip_tos = IPTOS_PREC_INTERNETCONTROL;
3531241913Sglebius	ip->ip_len = htons(hdrlen + igmpreclen);
3532241913Sglebius	ip->ip_off = htons(IP_DF);
3533189592Sbms	ip->ip_p = IPPROTO_IGMP;
3534189592Sbms	ip->ip_sum = 0;
3535189592Sbms
3536189592Sbms	ip->ip_src.s_addr = INADDR_ANY;
3537189592Sbms
3538189592Sbms	if (m->m_flags & M_IGMP_LOOP) {
3539189592Sbms		struct in_ifaddr *ia;
3540189592Sbms
3541189592Sbms		IFP_TO_IA(ifp, ia);
3542194760Srwatson		if (ia != NULL) {
3543189592Sbms			ip->ip_src = ia->ia_addr.sin_addr;
3544194760Srwatson			ifa_free(&ia->ia_ifa);
3545194760Srwatson		}
3546189592Sbms	}
3547189592Sbms
3548189592Sbms	ip->ip_dst.s_addr = htonl(INADDR_ALLRPTS_GROUP);
3549189592Sbms
3550189592Sbms	return (m);
3551189592Sbms}
3552189592Sbms
3553189592Sbms#ifdef KTR
3554189592Sbmsstatic char *
3555189592Sbmsigmp_rec_type_to_str(const int type)
3556189592Sbms{
3557189592Sbms
3558189592Sbms	switch (type) {
3559189592Sbms		case IGMP_CHANGE_TO_EXCLUDE_MODE:
3560189592Sbms			return "TO_EX";
3561189592Sbms			break;
3562189592Sbms		case IGMP_CHANGE_TO_INCLUDE_MODE:
3563189592Sbms			return "TO_IN";
3564189592Sbms			break;
3565189592Sbms		case IGMP_MODE_IS_EXCLUDE:
3566189592Sbms			return "MODE_EX";
3567189592Sbms			break;
3568189592Sbms		case IGMP_MODE_IS_INCLUDE:
3569189592Sbms			return "MODE_IN";
3570189592Sbms			break;
3571189592Sbms		case IGMP_ALLOW_NEW_SOURCES:
3572189592Sbms			return "ALLOW_NEW";
3573189592Sbms			break;
3574189592Sbms		case IGMP_BLOCK_OLD_SOURCES:
3575189592Sbms			return "BLOCK_OLD";
3576189592Sbms			break;
3577189592Sbms		default:
3578189592Sbms			break;
3579189592Sbms	}
3580189592Sbms	return "unknown";
3581189592Sbms}
3582189592Sbms#endif
3583189592Sbms
3584189592Sbmsstatic void
3585195837Srwatsonigmp_init(void *unused __unused)
3586189592Sbms{
3587189592Sbms
3588189592Sbms	CTR1(KTR_IGMPV3, "%s: initializing", __func__);
3589189592Sbms
3590189592Sbms	IGMP_LOCK_INIT();
3591189592Sbms
3592189592Sbms	m_raopt = igmp_ra_alloc();
3593189592Sbms
3594193219Srwatson	netisr_register(&igmp_nh);
3595189592Sbms}
3596195837SrwatsonSYSINIT(igmp_init, SI_SUB_PSEUDO, SI_ORDER_MIDDLE, igmp_init, NULL);
3597189592Sbms
3598189592Sbmsstatic void
3599195837Srwatsonigmp_uninit(void *unused __unused)
3600189592Sbms{
3601189592Sbms
3602189592Sbms	CTR1(KTR_IGMPV3, "%s: tearing down", __func__);
3603189592Sbms
3604193219Srwatson	netisr_unregister(&igmp_nh);
3605189592Sbms
3606189592Sbms	m_free(m_raopt);
3607189592Sbms	m_raopt = NULL;
3608189592Sbms
3609189592Sbms	IGMP_LOCK_DESTROY();
3610189592Sbms}
3611195837SrwatsonSYSUNINIT(igmp_uninit, SI_SUB_PSEUDO, SI_ORDER_MIDDLE, igmp_uninit, NULL);
3612189592Sbms
3613195837Srwatsonstatic void
3614195837Srwatsonvnet_igmp_init(const void *unused __unused)
3615189592Sbms{
3616189592Sbms
3617189592Sbms	CTR1(KTR_IGMPV3, "%s: initializing", __func__);
3618189592Sbms
3619189592Sbms	LIST_INIT(&V_igi_head);
3620189592Sbms}
3621195837SrwatsonVNET_SYSINIT(vnet_igmp_init, SI_SUB_PSEUDO, SI_ORDER_ANY, vnet_igmp_init,
3622195837Srwatson    NULL);
3623189592Sbms
3624195837Srwatsonstatic void
3625195837Srwatsonvnet_igmp_uninit(const void *unused __unused)
3626189592Sbms{
3627189592Sbms
3628189592Sbms	CTR1(KTR_IGMPV3, "%s: tearing down", __func__);
3629189592Sbms
3630189592Sbms	KASSERT(LIST_EMPTY(&V_igi_head),
3631189592Sbms	    ("%s: igi list not empty; ifnets not detached?", __func__));
3632189592Sbms}
3633195837SrwatsonVNET_SYSUNINIT(vnet_igmp_uninit, SI_SUB_PSEUDO, SI_ORDER_ANY,
3634195837Srwatson    vnet_igmp_uninit, NULL);
3635189592Sbms
3636189592Sbmsstatic int
3637189592Sbmsigmp_modevent(module_t mod, int type, void *unused __unused)
3638189592Sbms{
3639189592Sbms
3640189592Sbms    switch (type) {
3641189592Sbms    case MOD_LOAD:
3642189592Sbms    case MOD_UNLOAD:
3643189592Sbms	break;
3644189592Sbms    default:
3645189592Sbms	return (EOPNOTSUPP);
3646189592Sbms    }
3647189592Sbms    return (0);
3648189592Sbms}
3649189592Sbms
3650189592Sbmsstatic moduledata_t igmp_mod = {
3651189592Sbms    "igmp",
3652189592Sbms    igmp_modevent,
3653241394Skevlo    0
3654189592Sbms};
3655189592SbmsDECLARE_MODULE(igmp, igmp_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
3656