mld6.c revision 237992
1217309Snwhitehorn/*-
2251843Sbapt * Copyright (c) 2009 Bruce Simpson.
3217309Snwhitehorn *
4217309Snwhitehorn * Redistribution and use in source and binary forms, with or without
5217309Snwhitehorn * modification, are permitted provided that the following conditions
6251843Sbapt * are met:
7217309Snwhitehorn * 1. Redistributions of source code must retain the above copyright
8217309Snwhitehorn *    notice, this list of conditions and the following disclaimer.
9217309Snwhitehorn * 2. Redistributions in binary form must reproduce the above copyright
10217309Snwhitehorn *    notice, this list of conditions and the following disclaimer in the
11217309Snwhitehorn *    documentation and/or other materials provided with the distribution.
12217309Snwhitehorn * 3. The name of the author may not be used to endorse or promote
13217309Snwhitehorn *    products derived from this software without specific prior written
14217309Snwhitehorn *    permission.
15217309Snwhitehorn *
16217309Snwhitehorn * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17217309Snwhitehorn * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18217309Snwhitehorn * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19217309Snwhitehorn * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20217309Snwhitehorn * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21217309Snwhitehorn * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22217309Snwhitehorn * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23217309Snwhitehorn * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24217309Snwhitehorn * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25217309Snwhitehorn * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26217309Snwhitehorn * SUCH DAMAGE.
27217309Snwhitehorn *
28217309Snwhitehorn *	$KAME: mld6.c,v 1.27 2001/04/04 05:17:30 itojun Exp $
29217309Snwhitehorn */
30217309Snwhitehorn
31217309Snwhitehorn/*-
32217309Snwhitehorn * Copyright (c) 1988 Stephen Deering.
33217309Snwhitehorn * Copyright (c) 1992, 1993
34217309Snwhitehorn *	The Regents of the University of California.  All rights reserved.
35217309Snwhitehorn *
36217309Snwhitehorn * This code is derived from software contributed to Berkeley by
37217309Snwhitehorn * Stephen Deering of Stanford University.
38217309Snwhitehorn *
39217309Snwhitehorn * Redistribution and use in source and binary forms, with or without
40217309Snwhitehorn * modification, are permitted provided that the following conditions
41217309Snwhitehorn * are met:
42217309Snwhitehorn * 1. Redistributions of source code must retain the above copyright
43217309Snwhitehorn *    notice, this list of conditions and the following disclaimer.
44217309Snwhitehorn * 2. Redistributions in binary form must reproduce the above copyright
45217309Snwhitehorn *    notice, this list of conditions and the following disclaimer in the
46217309Snwhitehorn *    documentation and/or other materials provided with the distribution.
47217309Snwhitehorn * 4. Neither the name of the University nor the names of its contributors
48217309Snwhitehorn *    may be used to endorse or promote products derived from this software
49217309Snwhitehorn *    without specific prior written permission.
50217309Snwhitehorn *
51217309Snwhitehorn * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
52217309Snwhitehorn * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
53217309Snwhitehorn * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
54217309Snwhitehorn * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
55217309Snwhitehorn * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
56217309Snwhitehorn * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
57217309Snwhitehorn * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
58217309Snwhitehorn * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
59217309Snwhitehorn * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
60217309Snwhitehorn * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
61217309Snwhitehorn * SUCH DAMAGE.
62217309Snwhitehorn *
63217309Snwhitehorn *	@(#)igmp.c	8.1 (Berkeley) 7/19/93
64217309Snwhitehorn */
65217309Snwhitehorn
66217309Snwhitehorn#include <sys/cdefs.h>
67217309Snwhitehorn__FBSDID("$FreeBSD: stable/9/sys/netinet6/mld6.c 237992 2012-07-02 10:07:32Z bms $");
68217309Snwhitehorn
69217309Snwhitehorn#include "opt_inet.h"
70217309Snwhitehorn#include "opt_inet6.h"
71217309Snwhitehorn
72217309Snwhitehorn#include <sys/param.h>
73217309Snwhitehorn#include <sys/systm.h>
74217309Snwhitehorn#include <sys/mbuf.h>
75217309Snwhitehorn#include <sys/socket.h>
76217309Snwhitehorn#include <sys/protosw.h>
77217309Snwhitehorn#include <sys/sysctl.h>
78217309Snwhitehorn#include <sys/kernel.h>
79217309Snwhitehorn#include <sys/callout.h>
80217309Snwhitehorn#include <sys/malloc.h>
81217309Snwhitehorn#include <sys/module.h>
82217309Snwhitehorn#include <sys/ktr.h>
83217309Snwhitehorn
84217309Snwhitehorn#include <net/if.h>
85217309Snwhitehorn#include <net/route.h>
86217309Snwhitehorn#include <net/vnet.h>
87217309Snwhitehorn
88217309Snwhitehorn#include <netinet/in.h>
89217309Snwhitehorn#include <netinet/in_var.h>
90217309Snwhitehorn#include <netinet6/in6_var.h>
91217309Snwhitehorn#include <netinet/ip6.h>
92217309Snwhitehorn#include <netinet6/ip6_var.h>
93217309Snwhitehorn#include <netinet6/scope6_var.h>
94217309Snwhitehorn#include <netinet/icmp6.h>
95217309Snwhitehorn#include <netinet6/mld6.h>
96217309Snwhitehorn#include <netinet6/mld6_var.h>
97217309Snwhitehorn
98217309Snwhitehorn#include <security/mac/mac_framework.h>
99217309Snwhitehorn
100217309Snwhitehorn#ifndef KTR_MLD
101217309Snwhitehorn#define KTR_MLD KTR_INET6
102217309Snwhitehorn#endif
103217309Snwhitehorn
104217309Snwhitehornstatic struct mld_ifinfo *
105217309Snwhitehorn		mli_alloc_locked(struct ifnet *);
106217309Snwhitehornstatic void	mli_delete_locked(const struct ifnet *);
107217309Snwhitehornstatic void	mld_dispatch_packet(struct mbuf *);
108217309Snwhitehornstatic void	mld_dispatch_queue(struct ifqueue *, int);
109217309Snwhitehornstatic void	mld_final_leave(struct in6_multi *, struct mld_ifinfo *);
110217309Snwhitehornstatic void	mld_fasttimo_vnet(void);
111217309Snwhitehornstatic int	mld_handle_state_change(struct in6_multi *,
112217309Snwhitehorn		    struct mld_ifinfo *);
113217309Snwhitehornstatic int	mld_initial_join(struct in6_multi *, struct mld_ifinfo *,
114217309Snwhitehorn		    const int);
115217309Snwhitehorn#ifdef KTR
116217309Snwhitehornstatic char *	mld_rec_type_to_str(const int);
117217309Snwhitehorn#endif
118217309Snwhitehornstatic void	mld_set_version(struct mld_ifinfo *, const int);
119217309Snwhitehornstatic void	mld_slowtimo_vnet(void);
120217309Snwhitehornstatic int	mld_v1_input_query(struct ifnet *, const struct ip6_hdr *,
121217309Snwhitehorn		    /*const*/ struct mld_hdr *);
122217309Snwhitehornstatic int	mld_v1_input_report(struct ifnet *, const struct ip6_hdr *,
123217309Snwhitehorn		    /*const*/ struct mld_hdr *);
124217309Snwhitehornstatic void	mld_v1_process_group_timer(struct mld_ifinfo *,
125217309Snwhitehorn		    struct in6_multi *);
126217309Snwhitehornstatic void	mld_v1_process_querier_timers(struct mld_ifinfo *);
127217309Snwhitehornstatic int	mld_v1_transmit_report(struct in6_multi *, const int);
128217309Snwhitehornstatic void	mld_v1_update_group(struct in6_multi *, const int);
129217309Snwhitehornstatic void	mld_v2_cancel_link_timers(struct mld_ifinfo *);
130217309Snwhitehornstatic void	mld_v2_dispatch_general_query(struct mld_ifinfo *);
131217309Snwhitehornstatic struct mbuf *
132217309Snwhitehorn		mld_v2_encap_report(struct ifnet *, struct mbuf *);
133217309Snwhitehornstatic int	mld_v2_enqueue_filter_change(struct ifqueue *,
134217309Snwhitehorn		    struct in6_multi *);
135217309Snwhitehornstatic int	mld_v2_enqueue_group_record(struct ifqueue *,
136217309Snwhitehorn		    struct in6_multi *, const int, const int, const int,
137217309Snwhitehorn		    const int);
138217309Snwhitehornstatic int	mld_v2_input_query(struct ifnet *, const struct ip6_hdr *,
139217309Snwhitehorn		    struct mbuf *, const int, const int);
140217309Snwhitehornstatic int	mld_v2_merge_state_changes(struct in6_multi *,
141217309Snwhitehorn		    struct ifqueue *);
142217309Snwhitehornstatic void	mld_v2_process_group_timers(struct mld_ifinfo *,
143217309Snwhitehorn		    struct ifqueue *, struct ifqueue *,
144217309Snwhitehorn		    struct in6_multi *, const int);
145217309Snwhitehornstatic int	mld_v2_process_group_query(struct in6_multi *,
146217309Snwhitehorn		    struct mld_ifinfo *mli, int, struct mbuf *, const int);
147217309Snwhitehornstatic int	sysctl_mld_gsr(SYSCTL_HANDLER_ARGS);
148217309Snwhitehornstatic int	sysctl_mld_ifinfo(SYSCTL_HANDLER_ARGS);
149217309Snwhitehorn
150217309Snwhitehorn/*
151217309Snwhitehorn * Normative references: RFC 2710, RFC 3590, RFC 3810.
152217309Snwhitehorn *
153217309Snwhitehorn * Locking:
154217309Snwhitehorn *  * The MLD subsystem lock ends up being system-wide for the moment,
155217309Snwhitehorn *    but could be per-VIMAGE later on.
156217309Snwhitehorn *  * The permitted lock order is: IN6_MULTI_LOCK, MLD_LOCK, IF_ADDR_LOCK.
157217309Snwhitehorn *    Any may be taken independently; if any are held at the same
158217309Snwhitehorn *    time, the above lock order must be followed.
159217309Snwhitehorn *  * IN6_MULTI_LOCK covers in_multi.
160217309Snwhitehorn *  * MLD_LOCK covers per-link state and any global variables in this file.
161217309Snwhitehorn *  * IF_ADDR_LOCK covers if_multiaddrs, which is used for a variety of
162217309Snwhitehorn *    per-link state iterators.
163217309Snwhitehorn *
164217309Snwhitehorn *  XXX LOR PREVENTION
165217309Snwhitehorn *  A special case for IPv6 is the in6_setscope() routine. ip6_output()
166217309Snwhitehorn *  will not accept an ifp; it wants an embedded scope ID, unlike
167217309Snwhitehorn *  ip_output(), which happily takes the ifp given to it. The embedded
168217309Snwhitehorn *  scope ID is only used by MLD to select the outgoing interface.
169217309Snwhitehorn *
170217309Snwhitehorn *  During interface attach and detach, MLD will take MLD_LOCK *after*
171217309Snwhitehorn *  the IF_AFDATA_LOCK.
172217309Snwhitehorn *  As in6_setscope() takes IF_AFDATA_LOCK then SCOPE_LOCK, we can't call
173217309Snwhitehorn *  it with MLD_LOCK held without triggering an LOR. A netisr with indirect
174217309Snwhitehorn *  dispatch could work around this, but we'd rather not do that, as it
175217309Snwhitehorn *  can introduce other races.
176217309Snwhitehorn *
177217309Snwhitehorn *  As such, we exploit the fact that the scope ID is just the interface
178217309Snwhitehorn *  index, and embed it in the IPv6 destination address accordingly.
179217309Snwhitehorn *  This is potentially NOT VALID for MLDv1 reports, as they
180217309Snwhitehorn *  are always sent to the multicast group itself; as MLDv2
181217309Snwhitehorn *  reports are always sent to ff02::16, this is not an issue
182217309Snwhitehorn *  when MLDv2 is in use.
183217309Snwhitehorn *
184217309Snwhitehorn *  This does not however eliminate the LOR when ip6_output() itself
185217309Snwhitehorn *  calls in6_setscope() internally whilst MLD_LOCK is held. This will
186217309Snwhitehorn *  trigger a LOR warning in WITNESS when the ifnet is detached.
187217309Snwhitehorn *
188217309Snwhitehorn *  The right answer is probably to make IF_AFDATA_LOCK an rwlock, given
189217309Snwhitehorn *  how it's used across the network stack. Here we're simply exploiting
190217309Snwhitehorn *  the fact that MLD runs at a similar layer in the stack to scope6.c.
191217309Snwhitehorn *
192217309Snwhitehorn * VIMAGE:
193217309Snwhitehorn *  * Each in6_multi corresponds to an ifp, and each ifp corresponds
194217309Snwhitehorn *    to a vnet in ifp->if_vnet.
195217309Snwhitehorn */
196217309Snwhitehornstatic struct mtx		 mld_mtx;
197217309SnwhitehornMALLOC_DEFINE(M_MLD, "mld", "mld state");
198217309Snwhitehorn
199217309Snwhitehorn#define	MLD_EMBEDSCOPE(pin6, zoneid)					\
200217309Snwhitehorn	if (IN6_IS_SCOPE_LINKLOCAL(pin6) ||				\
201217309Snwhitehorn	    IN6_IS_ADDR_MC_INTFACELOCAL(pin6))				\
202217309Snwhitehorn		(pin6)->s6_addr16[1] = htons((zoneid) & 0xFFFF)		\
203217309Snwhitehorn
204217309Snwhitehorn/*
205217309Snwhitehorn * VIMAGE-wide globals.
206217309Snwhitehorn */
207217309Snwhitehornstatic VNET_DEFINE(struct timeval, mld_gsrdelay) = {10, 0};
208217309Snwhitehornstatic VNET_DEFINE(LIST_HEAD(, mld_ifinfo), mli_head);
209217309Snwhitehornstatic VNET_DEFINE(int, interface_timers_running6);
210217309Snwhitehornstatic VNET_DEFINE(int, state_change_timers_running6);
211217309Snwhitehornstatic VNET_DEFINE(int, current_state_timers_running6);
212217309Snwhitehorn
213217309Snwhitehorn#define	V_mld_gsrdelay			VNET(mld_gsrdelay)
214217309Snwhitehorn#define	V_mli_head			VNET(mli_head)
215217309Snwhitehorn#define	V_interface_timers_running6	VNET(interface_timers_running6)
216217309Snwhitehorn#define	V_state_change_timers_running6	VNET(state_change_timers_running6)
217217309Snwhitehorn#define	V_current_state_timers_running6	VNET(current_state_timers_running6)
218217309Snwhitehorn
219217309SnwhitehornSYSCTL_DECL(_net_inet6);	/* Note: Not in any common header. */
220217309Snwhitehorn
221217309SnwhitehornSYSCTL_NODE(_net_inet6, OID_AUTO, mld, CTLFLAG_RW, 0,
222217309Snwhitehorn    "IPv6 Multicast Listener Discovery");
223217309Snwhitehorn
224217309Snwhitehorn/*
225217309Snwhitehorn * Virtualized sysctls.
226217309Snwhitehorn */
227217309SnwhitehornSYSCTL_VNET_PROC(_net_inet6_mld, OID_AUTO, gsrdelay,
228251843Sbapt    CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
229251843Sbapt    &VNET_NAME(mld_gsrdelay.tv_sec), 0, sysctl_mld_gsr, "I",
230251843Sbapt    "Rate limit for MLDv2 Group-and-Source queries in seconds");
231251843Sbapt
232251843Sbapt/*
233251843Sbapt * Non-virtualized sysctls.
234217309Snwhitehorn */
235251843SbaptSYSCTL_NODE(_net_inet6_mld, OID_AUTO, ifinfo, CTLFLAG_RD | CTLFLAG_MPSAFE,
236217309Snwhitehorn    sysctl_mld_ifinfo, "Per-interface MLDv2 state");
237217309Snwhitehorn
238217309Snwhitehornstatic int	mld_v1enable = 1;
239217309SnwhitehornSYSCTL_INT(_net_inet6_mld, OID_AUTO, v1enable, CTLFLAG_RW,
240217309Snwhitehorn    &mld_v1enable, 0, "Enable fallback to MLDv1");
241217309SnwhitehornTUNABLE_INT("net.inet6.mld.v1enable", &mld_v1enable);
242217309Snwhitehorn
243217309Snwhitehornstatic int	mld_use_allow = 1;
244217309SnwhitehornSYSCTL_INT(_net_inet6_mld, OID_AUTO, use_allow, CTLFLAG_RW,
245217309Snwhitehorn    &mld_use_allow, 0, "Use ALLOW/BLOCK for RFC 4604 SSM joins/leaves");
246217309SnwhitehornTUNABLE_INT("net.inet6.mld.use_allow", &mld_use_allow);
247217309Snwhitehorn
248217309Snwhitehorn/*
249217309Snwhitehorn * Packed Router Alert option structure declaration.
250217309Snwhitehorn */
251251843Sbaptstruct mld_raopt {
252217309Snwhitehorn	struct ip6_hbh		hbh;
253217309Snwhitehorn	struct ip6_opt		pad;
254217309Snwhitehorn	struct ip6_opt_router	ra;
255217309Snwhitehorn} __packed;
256217309Snwhitehorn
257217309Snwhitehorn/*
258217309Snwhitehorn * Router Alert hop-by-hop option header.
259217309Snwhitehorn */
260217309Snwhitehornstatic struct mld_raopt mld_ra = {
261217309Snwhitehorn	.hbh = { 0, 0 },
262251843Sbapt	.pad = { .ip6o_type = IP6OPT_PADN, 0 },
263217309Snwhitehorn	.ra = {
264251843Sbapt	    .ip6or_type = IP6OPT_ROUTER_ALERT,
265217309Snwhitehorn	    .ip6or_len = IP6OPT_RTALERT_LEN - 2,
266217309Snwhitehorn	    .ip6or_value[0] = ((IP6OPT_RTALERT_MLD >> 8) & 0xFF),
267217309Snwhitehorn	    .ip6or_value[1] = (IP6OPT_RTALERT_MLD & 0xFF)
268217309Snwhitehorn	}
269217309Snwhitehorn};
270217309Snwhitehornstatic struct ip6_pktopts mld_po;
271217309Snwhitehorn
272217309Snwhitehornstatic __inline void
273217309Snwhitehornmld_save_context(struct mbuf *m, struct ifnet *ifp)
274217309Snwhitehorn{
275217309Snwhitehorn
276217309Snwhitehorn#ifdef VIMAGE
277217309Snwhitehorn	m->m_pkthdr.header = ifp->if_vnet;
278217309Snwhitehorn#endif /* VIMAGE */
279217309Snwhitehorn	m->m_pkthdr.flowid = ifp->if_index;
280217309Snwhitehorn}
281217309Snwhitehorn
282217309Snwhitehornstatic __inline void
283217309Snwhitehornmld_scrub_context(struct mbuf *m)
284217309Snwhitehorn{
285217309Snwhitehorn
286217309Snwhitehorn	m->m_pkthdr.header = NULL;
287217309Snwhitehorn	m->m_pkthdr.flowid = 0;
288217309Snwhitehorn}
289217309Snwhitehorn
290217309Snwhitehorn/*
291217309Snwhitehorn * Restore context from a queued output chain.
292217309Snwhitehorn * Return saved ifindex.
293217309Snwhitehorn *
294217309Snwhitehorn * VIMAGE: The assertion is there to make sure that we
295217309Snwhitehorn * actually called CURVNET_SET() with what's in the mbuf chain.
296217309Snwhitehorn */
297217309Snwhitehornstatic __inline uint32_t
298217309Snwhitehornmld_restore_context(struct mbuf *m)
299251843Sbapt{
300217309Snwhitehorn
301251843Sbapt#if defined(VIMAGE) && defined(INVARIANTS)
302251843Sbapt	KASSERT(curvnet == m->m_pkthdr.header,
303251843Sbapt	    ("%s: called when curvnet was not restored", __func__));
304251843Sbapt#endif
305251843Sbapt	return (m->m_pkthdr.flowid);
306251843Sbapt}
307251843Sbapt
308217309Snwhitehorn/*
309217309Snwhitehorn * Retrieve or set threshold between group-source queries in seconds.
310217309Snwhitehorn *
311217309Snwhitehorn * VIMAGE: Assume curvnet set by caller.
312217309Snwhitehorn * SMPng: NOTE: Serialized by MLD lock.
313217309Snwhitehorn */
314217309Snwhitehornstatic int
315217309Snwhitehornsysctl_mld_gsr(SYSCTL_HANDLER_ARGS)
316217309Snwhitehorn{
317217309Snwhitehorn	int error;
318217309Snwhitehorn	int i;
319217309Snwhitehorn
320217309Snwhitehorn	error = sysctl_wire_old_buffer(req, sizeof(int));
321251843Sbapt	if (error)
322217309Snwhitehorn		return (error);
323251843Sbapt
324251843Sbapt	MLD_LOCK();
325251843Sbapt
326251843Sbapt	i = V_mld_gsrdelay.tv_sec;
327251843Sbapt
328251843Sbapt	error = sysctl_handle_int(oidp, &i, 0, req);
329251843Sbapt	if (error || !req->newptr)
330217309Snwhitehorn		goto out_locked;
331217309Snwhitehorn
332217309Snwhitehorn	if (i < -1 || i >= 60) {
333217309Snwhitehorn		error = EINVAL;
334217309Snwhitehorn		goto out_locked;
335217309Snwhitehorn	}
336217309Snwhitehorn
337217309Snwhitehorn	CTR2(KTR_MLD, "change mld_gsrdelay from %d to %d",
338217309Snwhitehorn	     V_mld_gsrdelay.tv_sec, i);
339217309Snwhitehorn	V_mld_gsrdelay.tv_sec = i;
340217309Snwhitehorn
341217309Snwhitehornout_locked:
342217309Snwhitehorn	MLD_UNLOCK();
343217309Snwhitehorn	return (error);
344217309Snwhitehorn}
345217309Snwhitehorn
346217309Snwhitehorn/*
347217309Snwhitehorn * Expose struct mld_ifinfo to userland, keyed by ifindex.
348217309Snwhitehorn * For use by ifmcstat(8).
349217309Snwhitehorn *
350217309Snwhitehorn * SMPng: NOTE: Does an unlocked ifindex space read.
351217309Snwhitehorn * VIMAGE: Assume curvnet set by caller. The node handler itself
352217309Snwhitehorn * is not directly virtualized.
353217309Snwhitehorn */
354217309Snwhitehornstatic int
355217309Snwhitehornsysctl_mld_ifinfo(SYSCTL_HANDLER_ARGS)
356217309Snwhitehorn{
357217309Snwhitehorn	int			*name;
358217309Snwhitehorn	int			 error;
359217309Snwhitehorn	u_int			 namelen;
360217309Snwhitehorn	struct ifnet		*ifp;
361217309Snwhitehorn	struct mld_ifinfo	*mli;
362217309Snwhitehorn
363217309Snwhitehorn	name = (int *)arg1;
364217309Snwhitehorn	namelen = arg2;
365217309Snwhitehorn
366217309Snwhitehorn	if (req->newptr != NULL)
367217309Snwhitehorn		return (EPERM);
368217309Snwhitehorn
369217309Snwhitehorn	if (namelen != 1)
370217309Snwhitehorn		return (EINVAL);
371217309Snwhitehorn
372217309Snwhitehorn	error = sysctl_wire_old_buffer(req, sizeof(struct mld_ifinfo));
373217309Snwhitehorn	if (error)
374217309Snwhitehorn		return (error);
375217309Snwhitehorn
376217309Snwhitehorn	IN6_MULTI_LOCK();
377217309Snwhitehorn	MLD_LOCK();
378217309Snwhitehorn
379217309Snwhitehorn	if (name[0] <= 0 || name[0] > V_if_index) {
380217309Snwhitehorn		error = ENOENT;
381217309Snwhitehorn		goto out_locked;
382217309Snwhitehorn	}
383217309Snwhitehorn
384217309Snwhitehorn	error = ENOENT;
385217309Snwhitehorn
386217309Snwhitehorn	ifp = ifnet_byindex(name[0]);
387217309Snwhitehorn	if (ifp == NULL)
388217309Snwhitehorn		goto out_locked;
389217309Snwhitehorn
390217309Snwhitehorn	LIST_FOREACH(mli, &V_mli_head, mli_link) {
391217309Snwhitehorn		if (ifp == mli->mli_ifp) {
392217309Snwhitehorn			error = SYSCTL_OUT(req, mli,
393217309Snwhitehorn			    sizeof(struct mld_ifinfo));
394217309Snwhitehorn			break;
395217309Snwhitehorn		}
396217309Snwhitehorn	}
397224014Snwhitehorn
398217309Snwhitehornout_locked:
399217309Snwhitehorn	MLD_UNLOCK();
400217309Snwhitehorn	IN6_MULTI_UNLOCK();
401217309Snwhitehorn	return (error);
402217309Snwhitehorn}
403217309Snwhitehorn
404217309Snwhitehorn/*
405217309Snwhitehorn * Dispatch an entire queue of pending packet chains.
406217309Snwhitehorn * VIMAGE: Assumes the vnet pointer has been set.
407217309Snwhitehorn */
408217309Snwhitehornstatic void
409217309Snwhitehornmld_dispatch_queue(struct ifqueue *ifq, int limit)
410217309Snwhitehorn{
411217309Snwhitehorn	struct mbuf *m;
412217309Snwhitehorn
413217309Snwhitehorn	for (;;) {
414217309Snwhitehorn		_IF_DEQUEUE(ifq, m);
415217309Snwhitehorn		if (m == NULL)
416217309Snwhitehorn			break;
417217309Snwhitehorn		CTR3(KTR_MLD, "%s: dispatch %p from %p", __func__, ifq, m);
418217309Snwhitehorn		mld_dispatch_packet(m);
419217309Snwhitehorn		if (--limit == 0)
420217309Snwhitehorn			break;
421217309Snwhitehorn	}
422217309Snwhitehorn}
423217309Snwhitehorn
424217309Snwhitehorn/*
425217309Snwhitehorn * Filter outgoing MLD report state by group.
426217309Snwhitehorn *
427217309Snwhitehorn * Reports are ALWAYS suppressed for ALL-HOSTS (ff02::1)
428217309Snwhitehorn * and node-local addresses. However, kernel and socket consumers
429217309Snwhitehorn * always embed the KAME scope ID in the address provided, so strip it
430217309Snwhitehorn * when performing comparison.
431217309Snwhitehorn * Note: This is not the same as the *multicast* scope.
432217309Snwhitehorn *
433217309Snwhitehorn * Return zero if the given group is one for which MLD reports
434217309Snwhitehorn * should be suppressed, or non-zero if reports should be issued.
435217309Snwhitehorn */
436217309Snwhitehornstatic __inline int
437217309Snwhitehornmld_is_addr_reported(const struct in6_addr *addr)
438217309Snwhitehorn{
439251843Sbapt
440217309Snwhitehorn	KASSERT(IN6_IS_ADDR_MULTICAST(addr), ("%s: not multicast", __func__));
441217309Snwhitehorn
442217309Snwhitehorn	if (IPV6_ADDR_MC_SCOPE(addr) == IPV6_ADDR_SCOPE_NODELOCAL)
443217309Snwhitehorn		return (0);
444217309Snwhitehorn
445217309Snwhitehorn	if (IPV6_ADDR_MC_SCOPE(addr) == IPV6_ADDR_SCOPE_LINKLOCAL) {
446217309Snwhitehorn		struct in6_addr tmp = *addr;
447217309Snwhitehorn		in6_clearscope(&tmp);
448217309Snwhitehorn		if (IN6_ARE_ADDR_EQUAL(&tmp, &in6addr_linklocal_allnodes))
449217309Snwhitehorn			return (0);
450217309Snwhitehorn	}
451217309Snwhitehorn
452217309Snwhitehorn	return (1);
453217309Snwhitehorn}
454217309Snwhitehorn
455217309Snwhitehorn/*
456217309Snwhitehorn * Attach MLD when PF_INET6 is attached to an interface.
457217309Snwhitehorn *
458217309Snwhitehorn * SMPng: Normally called with IF_AFDATA_LOCK held.
459217309Snwhitehorn */
460217309Snwhitehornstruct mld_ifinfo *
461217309Snwhitehornmld_domifattach(struct ifnet *ifp)
462217309Snwhitehorn{
463217309Snwhitehorn	struct mld_ifinfo *mli;
464217309Snwhitehorn
465217309Snwhitehorn	CTR3(KTR_MLD, "%s: called for ifp %p(%s)",
466217309Snwhitehorn	    __func__, ifp, ifp->if_xname);
467217309Snwhitehorn
468217309Snwhitehorn	MLD_LOCK();
469217309Snwhitehorn
470217309Snwhitehorn	mli = mli_alloc_locked(ifp);
471217309Snwhitehorn	if (!(ifp->if_flags & IFF_MULTICAST))
472217309Snwhitehorn		mli->mli_flags |= MLIF_SILENT;
473217309Snwhitehorn	if (mld_use_allow)
474217309Snwhitehorn		mli->mli_flags |= MLIF_USEALLOW;
475217309Snwhitehorn
476217309Snwhitehorn	MLD_UNLOCK();
477217309Snwhitehorn
478217309Snwhitehorn	return (mli);
479217309Snwhitehorn}
480217309Snwhitehorn
481217309Snwhitehorn/*
482217309Snwhitehorn * VIMAGE: assume curvnet set by caller.
483217309Snwhitehorn */
484217309Snwhitehornstatic struct mld_ifinfo *
485217309Snwhitehornmli_alloc_locked(/*const*/ struct ifnet *ifp)
486217309Snwhitehorn{
487217309Snwhitehorn	struct mld_ifinfo *mli;
488217309Snwhitehorn
489217309Snwhitehorn	MLD_LOCK_ASSERT();
490217309Snwhitehorn
491217309Snwhitehorn	mli = malloc(sizeof(struct mld_ifinfo), M_MLD, M_NOWAIT|M_ZERO);
492217309Snwhitehorn	if (mli == NULL)
493217309Snwhitehorn		goto out;
494217309Snwhitehorn
495217309Snwhitehorn	mli->mli_ifp = ifp;
496217309Snwhitehorn	mli->mli_version = MLD_VERSION_2;
497217309Snwhitehorn	mli->mli_flags = 0;
498217309Snwhitehorn	mli->mli_rv = MLD_RV_INIT;
499217309Snwhitehorn	mli->mli_qi = MLD_QI_INIT;
500251843Sbapt	mli->mli_qri = MLD_QRI_INIT;
501251843Sbapt	mli->mli_uri = MLD_URI_INIT;
502217309Snwhitehorn
503217309Snwhitehorn	SLIST_INIT(&mli->mli_relinmhead);
504251843Sbapt
505217309Snwhitehorn	/*
506217309Snwhitehorn	 * Responses to general queries are subject to bounds.
507217309Snwhitehorn	 */
508217309Snwhitehorn	IFQ_SET_MAXLEN(&mli->mli_gq, MLD_MAX_RESPONSE_PACKETS);
509217309Snwhitehorn
510217309Snwhitehorn	LIST_INSERT_HEAD(&V_mli_head, mli, mli_link);
511217309Snwhitehorn
512217309Snwhitehorn	CTR2(KTR_MLD, "allocate mld_ifinfo for ifp %p(%s)",
513217309Snwhitehorn	     ifp, ifp->if_xname);
514217309Snwhitehorn
515217309Snwhitehornout:
516217309Snwhitehorn	return (mli);
517217309Snwhitehorn}
518217309Snwhitehorn
519217309Snwhitehorn/*
520217309Snwhitehorn * Hook for ifdetach.
521217309Snwhitehorn *
522217309Snwhitehorn * NOTE: Some finalization tasks need to run before the protocol domain
523217309Snwhitehorn * is detached, but also before the link layer does its cleanup.
524217309Snwhitehorn * Run before link-layer cleanup; cleanup groups, but do not free MLD state.
525217309Snwhitehorn *
526217309Snwhitehorn * SMPng: Caller must hold IN6_MULTI_LOCK().
527217309Snwhitehorn * Must take IF_ADDR_LOCK() to cover if_multiaddrs iterator.
528217309Snwhitehorn * XXX This routine is also bitten by unlocked ifma_protospec access.
529217309Snwhitehorn */
530217309Snwhitehornvoid
531217309Snwhitehornmld_ifdetach(struct ifnet *ifp)
532217309Snwhitehorn{
533217309Snwhitehorn	struct mld_ifinfo	*mli;
534217309Snwhitehorn	struct ifmultiaddr	*ifma;
535217309Snwhitehorn	struct in6_multi	*inm, *tinm;
536217309Snwhitehorn
537217309Snwhitehorn	CTR3(KTR_MLD, "%s: called for ifp %p(%s)", __func__, ifp,
538217309Snwhitehorn	    ifp->if_xname);
539217309Snwhitehorn
540217309Snwhitehorn	IN6_MULTI_LOCK_ASSERT();
541217309Snwhitehorn	MLD_LOCK();
542217309Snwhitehorn
543217309Snwhitehorn	mli = MLD_IFINFO(ifp);
544217309Snwhitehorn	if (mli->mli_version == MLD_VERSION_2) {
545217309Snwhitehorn		IF_ADDR_RLOCK(ifp);
546217309Snwhitehorn		TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
547217309Snwhitehorn			if (ifma->ifma_addr->sa_family != AF_INET6 ||
548251843Sbapt			    ifma->ifma_protospec == NULL)
549217309Snwhitehorn				continue;
550217309Snwhitehorn			inm = (struct in6_multi *)ifma->ifma_protospec;
551217309Snwhitehorn			if (inm->in6m_state == MLD_LEAVING_MEMBER) {
552217309Snwhitehorn				SLIST_INSERT_HEAD(&mli->mli_relinmhead,
553217309Snwhitehorn				    inm, in6m_nrele);
554217309Snwhitehorn			}
555217309Snwhitehorn			in6m_clear_recorded(inm);
556217309Snwhitehorn		}
557217309Snwhitehorn		IF_ADDR_RUNLOCK(ifp);
558217309Snwhitehorn		SLIST_FOREACH_SAFE(inm, &mli->mli_relinmhead, in6m_nrele,
559217309Snwhitehorn		    tinm) {
560217309Snwhitehorn			SLIST_REMOVE_HEAD(&mli->mli_relinmhead, in6m_nrele);
561217309Snwhitehorn			in6m_release_locked(inm);
562217309Snwhitehorn		}
563217309Snwhitehorn	}
564217309Snwhitehorn
565217309Snwhitehorn	MLD_UNLOCK();
566217309Snwhitehorn}
567217309Snwhitehorn
568217309Snwhitehorn/*
569217309Snwhitehorn * Hook for domifdetach.
570217309Snwhitehorn * Runs after link-layer cleanup; free MLD state.
571217309Snwhitehorn *
572217309Snwhitehorn * SMPng: Normally called with IF_AFDATA_LOCK held.
573217309Snwhitehorn */
574217309Snwhitehornvoid
575217309Snwhitehornmld_domifdetach(struct ifnet *ifp)
576217309Snwhitehorn{
577217309Snwhitehorn
578217309Snwhitehorn	CTR3(KTR_MLD, "%s: called for ifp %p(%s)",
579217309Snwhitehorn	    __func__, ifp, ifp->if_xname);
580217309Snwhitehorn
581217309Snwhitehorn	MLD_LOCK();
582224014Snwhitehorn	mli_delete_locked(ifp);
583217309Snwhitehorn	MLD_UNLOCK();
584217309Snwhitehorn}
585217309Snwhitehorn
586217309Snwhitehornstatic void
587217309Snwhitehornmli_delete_locked(const struct ifnet *ifp)
588217309Snwhitehorn{
589217309Snwhitehorn	struct mld_ifinfo *mli, *tmli;
590217309Snwhitehorn
591217309Snwhitehorn	CTR3(KTR_MLD, "%s: freeing mld_ifinfo for ifp %p(%s)",
592217309Snwhitehorn	    __func__, ifp, ifp->if_xname);
593217309Snwhitehorn
594217309Snwhitehorn	MLD_LOCK_ASSERT();
595217309Snwhitehorn
596217309Snwhitehorn	LIST_FOREACH_SAFE(mli, &V_mli_head, mli_link, tmli) {
597217309Snwhitehorn		if (mli->mli_ifp == ifp) {
598217309Snwhitehorn			/*
599217309Snwhitehorn			 * Free deferred General Query responses.
600217309Snwhitehorn			 */
601217309Snwhitehorn			_IF_DRAIN(&mli->mli_gq);
602217309Snwhitehorn
603217309Snwhitehorn			LIST_REMOVE(mli, mli_link);
604217309Snwhitehorn
605217309Snwhitehorn			KASSERT(SLIST_EMPTY(&mli->mli_relinmhead),
606217309Snwhitehorn			    ("%s: there are dangling in_multi references",
607217309Snwhitehorn			    __func__));
608217309Snwhitehorn
609217309Snwhitehorn			free(mli, M_MLD);
610217309Snwhitehorn			return;
611217309Snwhitehorn		}
612217309Snwhitehorn	}
613217309Snwhitehorn#ifdef INVARIANTS
614217309Snwhitehorn	panic("%s: mld_ifinfo not found for ifp %p\n", __func__,  ifp);
615217309Snwhitehorn#endif
616217309Snwhitehorn}
617217309Snwhitehorn
618217309Snwhitehorn/*
619217309Snwhitehorn * Process a received MLDv1 general or address-specific query.
620217309Snwhitehorn * Assumes that the query header has been pulled up to sizeof(mld_hdr).
621217309Snwhitehorn *
622217309Snwhitehorn * NOTE: Can't be fully const correct as we temporarily embed scope ID in
623217309Snwhitehorn * mld_addr. This is OK as we own the mbuf chain.
624217309Snwhitehorn */
625217309Snwhitehornstatic int
626217309Snwhitehornmld_v1_input_query(struct ifnet *ifp, const struct ip6_hdr *ip6,
627217309Snwhitehorn    /*const*/ struct mld_hdr *mld)
628217309Snwhitehorn{
629217309Snwhitehorn	struct ifmultiaddr	*ifma;
630217309Snwhitehorn	struct mld_ifinfo	*mli;
631217309Snwhitehorn	struct in6_multi	*inm;
632217309Snwhitehorn	int			 is_general_query;
633217309Snwhitehorn	uint16_t		 timer;
634217309Snwhitehorn#ifdef KTR
635217309Snwhitehorn	char			 ip6tbuf[INET6_ADDRSTRLEN];
636217309Snwhitehorn#endif
637217309Snwhitehorn
638217309Snwhitehorn	is_general_query = 0;
639217309Snwhitehorn
640217309Snwhitehorn	if (!mld_v1enable) {
641217309Snwhitehorn		CTR3(KTR_MLD, "ignore v1 query %s on ifp %p(%s)",
642217309Snwhitehorn		    ip6_sprintf(ip6tbuf, &mld->mld_addr),
643217309Snwhitehorn		    ifp, ifp->if_xname);
644217309Snwhitehorn		return (0);
645217309Snwhitehorn	}
646217309Snwhitehorn
647217309Snwhitehorn	/*
648217309Snwhitehorn	 * RFC3810 Section 6.2: MLD queries must originate from
649217309Snwhitehorn	 * a router's link-local address.
650217309Snwhitehorn	 */
651217309Snwhitehorn	if (!IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_src)) {
652217309Snwhitehorn		CTR3(KTR_MLD, "ignore v1 query src %s on ifp %p(%s)",
653217309Snwhitehorn		    ip6_sprintf(ip6tbuf, &ip6->ip6_src),
654217309Snwhitehorn		    ifp, ifp->if_xname);
655217309Snwhitehorn		return (0);
656217309Snwhitehorn	}
657217309Snwhitehorn
658217309Snwhitehorn	/*
659217309Snwhitehorn	 * Do address field validation upfront before we accept
660217309Snwhitehorn	 * the query.
661217309Snwhitehorn	 */
662217309Snwhitehorn	if (IN6_IS_ADDR_UNSPECIFIED(&mld->mld_addr)) {
663217309Snwhitehorn		/*
664217309Snwhitehorn		 * MLDv1 General Query.
665217309Snwhitehorn		 * If this was not sent to the all-nodes group, ignore it.
666217309Snwhitehorn		 */
667217309Snwhitehorn		struct in6_addr		 dst;
668217309Snwhitehorn
669217309Snwhitehorn		dst = ip6->ip6_dst;
670217309Snwhitehorn		in6_clearscope(&dst);
671217309Snwhitehorn		if (!IN6_ARE_ADDR_EQUAL(&dst, &in6addr_linklocal_allnodes))
672220749Snwhitehorn			return (EINVAL);
673220749Snwhitehorn		is_general_query = 1;
674220749Snwhitehorn	} else {
675220749Snwhitehorn		/*
676217309Snwhitehorn		 * Embed scope ID of receiving interface in MLD query for
677217309Snwhitehorn		 * lookup whilst we don't hold other locks.
678217309Snwhitehorn		 */
679217309Snwhitehorn		in6_setscope(&mld->mld_addr, ifp, NULL);
680217309Snwhitehorn	}
681217309Snwhitehorn
682217309Snwhitehorn	IN6_MULTI_LOCK();
683217309Snwhitehorn	MLD_LOCK();
684251843Sbapt
685217309Snwhitehorn	/*
686217309Snwhitehorn	 * Switch to MLDv1 host compatibility mode.
687217309Snwhitehorn	 */
688	mli = MLD_IFINFO(ifp);
689	KASSERT(mli != NULL, ("%s: no mld_ifinfo for ifp %p", __func__, ifp));
690	mld_set_version(mli, MLD_VERSION_1);
691
692	timer = (ntohs(mld->mld_maxdelay) * PR_FASTHZ) / MLD_TIMER_SCALE;
693	if (timer == 0)
694		timer = 1;
695
696	IF_ADDR_RLOCK(ifp);
697	if (is_general_query) {
698		/*
699		 * For each reporting group joined on this
700		 * interface, kick the report timer.
701		 */
702		CTR2(KTR_MLD, "process v1 general query on ifp %p(%s)",
703		    ifp, ifp->if_xname);
704		TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
705			if (ifma->ifma_addr->sa_family != AF_INET6 ||
706			    ifma->ifma_protospec == NULL)
707				continue;
708			inm = (struct in6_multi *)ifma->ifma_protospec;
709			mld_v1_update_group(inm, timer);
710		}
711	} else {
712		/*
713		 * MLDv1 Group-Specific Query.
714		 * If this is a group-specific MLDv1 query, we need only
715		 * look up the single group to process it.
716		 */
717		inm = in6m_lookup_locked(ifp, &mld->mld_addr);
718		if (inm != NULL) {
719			CTR3(KTR_MLD, "process v1 query %s on ifp %p(%s)",
720			    ip6_sprintf(ip6tbuf, &mld->mld_addr),
721			    ifp, ifp->if_xname);
722			mld_v1_update_group(inm, timer);
723		}
724		/* XXX Clear embedded scope ID as userland won't expect it. */
725		in6_clearscope(&mld->mld_addr);
726	}
727
728	IF_ADDR_RUNLOCK(ifp);
729	MLD_UNLOCK();
730	IN6_MULTI_UNLOCK();
731
732	return (0);
733}
734
735/*
736 * Update the report timer on a group in response to an MLDv1 query.
737 *
738 * If we are becoming the reporting member for this group, start the timer.
739 * If we already are the reporting member for this group, and timer is
740 * below the threshold, reset it.
741 *
742 * We may be updating the group for the first time since we switched
743 * to MLDv2. If we are, then we must clear any recorded source lists,
744 * and transition to REPORTING state; the group timer is overloaded
745 * for group and group-source query responses.
746 *
747 * Unlike MLDv2, the delay per group should be jittered
748 * to avoid bursts of MLDv1 reports.
749 */
750static void
751mld_v1_update_group(struct in6_multi *inm, const int timer)
752{
753#ifdef KTR
754	char			 ip6tbuf[INET6_ADDRSTRLEN];
755#endif
756
757	CTR4(KTR_MLD, "%s: %s/%s timer=%d", __func__,
758	    ip6_sprintf(ip6tbuf, &inm->in6m_addr),
759	    inm->in6m_ifp->if_xname, timer);
760
761	IN6_MULTI_LOCK_ASSERT();
762
763	switch (inm->in6m_state) {
764	case MLD_NOT_MEMBER:
765	case MLD_SILENT_MEMBER:
766		break;
767	case MLD_REPORTING_MEMBER:
768		if (inm->in6m_timer != 0 &&
769		    inm->in6m_timer <= timer) {
770			CTR1(KTR_MLD, "%s: REPORTING and timer running, "
771			    "skipping.", __func__);
772			break;
773		}
774		/* FALLTHROUGH */
775	case MLD_SG_QUERY_PENDING_MEMBER:
776	case MLD_G_QUERY_PENDING_MEMBER:
777	case MLD_IDLE_MEMBER:
778	case MLD_LAZY_MEMBER:
779	case MLD_AWAKENING_MEMBER:
780		CTR1(KTR_MLD, "%s: ->REPORTING", __func__);
781		inm->in6m_state = MLD_REPORTING_MEMBER;
782		inm->in6m_timer = MLD_RANDOM_DELAY(timer);
783		V_current_state_timers_running6 = 1;
784		break;
785	case MLD_SLEEPING_MEMBER:
786		CTR1(KTR_MLD, "%s: ->AWAKENING", __func__);
787		inm->in6m_state = MLD_AWAKENING_MEMBER;
788		break;
789	case MLD_LEAVING_MEMBER:
790		break;
791	}
792}
793
794/*
795 * Process a received MLDv2 general, group-specific or
796 * group-and-source-specific query.
797 *
798 * Assumes that the query header has been pulled up to sizeof(mldv2_query).
799 *
800 * Return 0 if successful, otherwise an appropriate error code is returned.
801 */
802static int
803mld_v2_input_query(struct ifnet *ifp, const struct ip6_hdr *ip6,
804    struct mbuf *m, const int off, const int icmp6len)
805{
806	struct mld_ifinfo	*mli;
807	struct mldv2_query	*mld;
808	struct in6_multi	*inm;
809	uint32_t		 maxdelay, nsrc, qqi;
810	int			 is_general_query;
811	uint16_t		 timer;
812	uint8_t			 qrv;
813#ifdef KTR
814	char			 ip6tbuf[INET6_ADDRSTRLEN];
815#endif
816
817	is_general_query = 0;
818
819	/*
820	 * RFC3810 Section 6.2: MLD queries must originate from
821	 * a router's link-local address.
822	 */
823	if (!IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_src)) {
824		CTR3(KTR_MLD, "ignore v1 query src %s on ifp %p(%s)",
825		    ip6_sprintf(ip6tbuf, &ip6->ip6_src),
826		    ifp, ifp->if_xname);
827		return (0);
828	}
829
830	CTR2(KTR_MLD, "input v2 query on ifp %p(%s)", ifp, ifp->if_xname);
831
832	mld = (struct mldv2_query *)(mtod(m, uint8_t *) + off);
833
834	maxdelay = ntohs(mld->mld_maxdelay);	/* in 1/10ths of a second */
835	if (maxdelay >= 32768) {
836		maxdelay = (MLD_MRC_MANT(maxdelay) | 0x1000) <<
837			   (MLD_MRC_EXP(maxdelay) + 3);
838	}
839	timer = (maxdelay * PR_FASTHZ) / MLD_TIMER_SCALE;
840	if (timer == 0)
841		timer = 1;
842
843	qrv = MLD_QRV(mld->mld_misc);
844	if (qrv < 2) {
845		CTR3(KTR_MLD, "%s: clamping qrv %d to %d", __func__,
846		    qrv, MLD_RV_INIT);
847		qrv = MLD_RV_INIT;
848	}
849
850	qqi = mld->mld_qqi;
851	if (qqi >= 128) {
852		qqi = MLD_QQIC_MANT(mld->mld_qqi) <<
853		     (MLD_QQIC_EXP(mld->mld_qqi) + 3);
854	}
855
856	nsrc = ntohs(mld->mld_numsrc);
857	if (nsrc > MLD_MAX_GS_SOURCES)
858		return (EMSGSIZE);
859	if (icmp6len < sizeof(struct mldv2_query) +
860	    (nsrc * sizeof(struct in6_addr)))
861		return (EMSGSIZE);
862
863	/*
864	 * Do further input validation upfront to avoid resetting timers
865	 * should we need to discard this query.
866	 */
867	if (IN6_IS_ADDR_UNSPECIFIED(&mld->mld_addr)) {
868		/*
869		 * A general query with a source list has undefined
870		 * behaviour; discard it.
871		 */
872		if (nsrc > 0)
873			return (EINVAL);
874		is_general_query = 1;
875	} else {
876		/*
877		 * Embed scope ID of receiving interface in MLD query for
878		 * lookup whilst we don't hold other locks (due to KAME
879		 * locking lameness). We own this mbuf chain just now.
880		 */
881		in6_setscope(&mld->mld_addr, ifp, NULL);
882	}
883
884	IN6_MULTI_LOCK();
885	MLD_LOCK();
886
887	mli = MLD_IFINFO(ifp);
888	KASSERT(mli != NULL, ("%s: no mld_ifinfo for ifp %p", __func__, ifp));
889
890	/*
891	 * Discard the v2 query if we're in Compatibility Mode.
892	 * The RFC is pretty clear that hosts need to stay in MLDv1 mode
893	 * until the Old Version Querier Present timer expires.
894	 */
895	if (mli->mli_version != MLD_VERSION_2)
896		goto out_locked;
897
898	mld_set_version(mli, MLD_VERSION_2);
899	mli->mli_rv = qrv;
900	mli->mli_qi = qqi;
901	mli->mli_qri = maxdelay;
902
903	CTR4(KTR_MLD, "%s: qrv %d qi %d maxdelay %d", __func__, qrv, qqi,
904	    maxdelay);
905
906	if (is_general_query) {
907		/*
908		 * MLDv2 General Query.
909		 *
910		 * Schedule a current-state report on this ifp for
911		 * all groups, possibly containing source lists.
912		 *
913		 * If there is a pending General Query response
914		 * scheduled earlier than the selected delay, do
915		 * not schedule any other reports.
916		 * Otherwise, reset the interface timer.
917		 */
918		CTR2(KTR_MLD, "process v2 general query on ifp %p(%s)",
919		    ifp, ifp->if_xname);
920		if (mli->mli_v2_timer == 0 || mli->mli_v2_timer >= timer) {
921			mli->mli_v2_timer = MLD_RANDOM_DELAY(timer);
922			V_interface_timers_running6 = 1;
923		}
924	} else {
925		/*
926		 * MLDv2 Group-specific or Group-and-source-specific Query.
927		 *
928		 * Group-source-specific queries are throttled on
929		 * a per-group basis to defeat denial-of-service attempts.
930		 * Queries for groups we are not a member of on this
931		 * link are simply ignored.
932		 */
933		IF_ADDR_RLOCK(ifp);
934		inm = in6m_lookup_locked(ifp, &mld->mld_addr);
935		if (inm == NULL) {
936			IF_ADDR_RUNLOCK(ifp);
937			goto out_locked;
938		}
939		if (nsrc > 0) {
940			if (!ratecheck(&inm->in6m_lastgsrtv,
941			    &V_mld_gsrdelay)) {
942				CTR1(KTR_MLD, "%s: GS query throttled.",
943				    __func__);
944				IF_ADDR_RUNLOCK(ifp);
945				goto out_locked;
946			}
947		}
948		CTR2(KTR_MLD, "process v2 group query on ifp %p(%s)",
949		     ifp, ifp->if_xname);
950		/*
951		 * If there is a pending General Query response
952		 * scheduled sooner than the selected delay, no
953		 * further report need be scheduled.
954		 * Otherwise, prepare to respond to the
955		 * group-specific or group-and-source query.
956		 */
957		if (mli->mli_v2_timer == 0 || mli->mli_v2_timer >= timer)
958			mld_v2_process_group_query(inm, mli, timer, m, off);
959
960		/* XXX Clear embedded scope ID as userland won't expect it. */
961		in6_clearscope(&mld->mld_addr);
962		IF_ADDR_RUNLOCK(ifp);
963	}
964
965out_locked:
966	MLD_UNLOCK();
967	IN6_MULTI_UNLOCK();
968
969	return (0);
970}
971
972/*
973 * Process a recieved MLDv2 group-specific or group-and-source-specific
974 * query.
975 * Return <0 if any error occured. Currently this is ignored.
976 */
977static int
978mld_v2_process_group_query(struct in6_multi *inm, struct mld_ifinfo *mli,
979    int timer, struct mbuf *m0, const int off)
980{
981	struct mldv2_query	*mld;
982	int			 retval;
983	uint16_t		 nsrc;
984
985	IN6_MULTI_LOCK_ASSERT();
986	MLD_LOCK_ASSERT();
987
988	retval = 0;
989	mld = (struct mldv2_query *)(mtod(m0, uint8_t *) + off);
990
991	switch (inm->in6m_state) {
992	case MLD_NOT_MEMBER:
993	case MLD_SILENT_MEMBER:
994	case MLD_SLEEPING_MEMBER:
995	case MLD_LAZY_MEMBER:
996	case MLD_AWAKENING_MEMBER:
997	case MLD_IDLE_MEMBER:
998	case MLD_LEAVING_MEMBER:
999		return (retval);
1000		break;
1001	case MLD_REPORTING_MEMBER:
1002	case MLD_G_QUERY_PENDING_MEMBER:
1003	case MLD_SG_QUERY_PENDING_MEMBER:
1004		break;
1005	}
1006
1007	nsrc = ntohs(mld->mld_numsrc);
1008
1009	/*
1010	 * Deal with group-specific queries upfront.
1011	 * If any group query is already pending, purge any recorded
1012	 * source-list state if it exists, and schedule a query response
1013	 * for this group-specific query.
1014	 */
1015	if (nsrc == 0) {
1016		if (inm->in6m_state == MLD_G_QUERY_PENDING_MEMBER ||
1017		    inm->in6m_state == MLD_SG_QUERY_PENDING_MEMBER) {
1018			in6m_clear_recorded(inm);
1019			timer = min(inm->in6m_timer, timer);
1020		}
1021		inm->in6m_state = MLD_G_QUERY_PENDING_MEMBER;
1022		inm->in6m_timer = MLD_RANDOM_DELAY(timer);
1023		V_current_state_timers_running6 = 1;
1024		return (retval);
1025	}
1026
1027	/*
1028	 * Deal with the case where a group-and-source-specific query has
1029	 * been received but a group-specific query is already pending.
1030	 */
1031	if (inm->in6m_state == MLD_G_QUERY_PENDING_MEMBER) {
1032		timer = min(inm->in6m_timer, timer);
1033		inm->in6m_timer = MLD_RANDOM_DELAY(timer);
1034		V_current_state_timers_running6 = 1;
1035		return (retval);
1036	}
1037
1038	/*
1039	 * Finally, deal with the case where a group-and-source-specific
1040	 * query has been received, where a response to a previous g-s-r
1041	 * query exists, or none exists.
1042	 * In this case, we need to parse the source-list which the Querier
1043	 * has provided us with and check if we have any source list filter
1044	 * entries at T1 for these sources. If we do not, there is no need
1045	 * schedule a report and the query may be dropped.
1046	 * If we do, we must record them and schedule a current-state
1047	 * report for those sources.
1048	 */
1049	if (inm->in6m_nsrc > 0) {
1050		struct mbuf		*m;
1051		uint8_t			*sp;
1052		int			 i, nrecorded;
1053		int			 soff;
1054
1055		m = m0;
1056		soff = off + sizeof(struct mldv2_query);
1057		nrecorded = 0;
1058		for (i = 0; i < nsrc; i++) {
1059			sp = mtod(m, uint8_t *) + soff;
1060			retval = in6m_record_source(inm,
1061			    (const struct in6_addr *)sp);
1062			if (retval < 0)
1063				break;
1064			nrecorded += retval;
1065			soff += sizeof(struct in6_addr);
1066			if (soff >= m->m_len) {
1067				soff = soff - m->m_len;
1068				m = m->m_next;
1069				if (m == NULL)
1070					break;
1071			}
1072		}
1073		if (nrecorded > 0) {
1074			CTR1(KTR_MLD,
1075			    "%s: schedule response to SG query", __func__);
1076			inm->in6m_state = MLD_SG_QUERY_PENDING_MEMBER;
1077			inm->in6m_timer = MLD_RANDOM_DELAY(timer);
1078			V_current_state_timers_running6 = 1;
1079		}
1080	}
1081
1082	return (retval);
1083}
1084
1085/*
1086 * Process a received MLDv1 host membership report.
1087 * Assumes mld points to mld_hdr in pulled up mbuf chain.
1088 *
1089 * NOTE: Can't be fully const correct as we temporarily embed scope ID in
1090 * mld_addr. This is OK as we own the mbuf chain.
1091 */
1092static int
1093mld_v1_input_report(struct ifnet *ifp, const struct ip6_hdr *ip6,
1094    /*const*/ struct mld_hdr *mld)
1095{
1096	struct in6_addr		 src, dst;
1097	struct in6_ifaddr	*ia;
1098	struct in6_multi	*inm;
1099#ifdef KTR
1100	char			 ip6tbuf[INET6_ADDRSTRLEN];
1101#endif
1102
1103	if (!mld_v1enable) {
1104		CTR3(KTR_MLD, "ignore v1 report %s on ifp %p(%s)",
1105		    ip6_sprintf(ip6tbuf, &mld->mld_addr),
1106		    ifp, ifp->if_xname);
1107		return (0);
1108	}
1109
1110	if (ifp->if_flags & IFF_LOOPBACK)
1111		return (0);
1112
1113	/*
1114	 * MLDv1 reports must originate from a host's link-local address,
1115	 * or the unspecified address (when booting).
1116	 */
1117	src = ip6->ip6_src;
1118	in6_clearscope(&src);
1119	if (!IN6_IS_SCOPE_LINKLOCAL(&src) && !IN6_IS_ADDR_UNSPECIFIED(&src)) {
1120		CTR3(KTR_MLD, "ignore v1 query src %s on ifp %p(%s)",
1121		    ip6_sprintf(ip6tbuf, &ip6->ip6_src),
1122		    ifp, ifp->if_xname);
1123		return (EINVAL);
1124	}
1125
1126	/*
1127	 * RFC2710 Section 4: MLDv1 reports must pertain to a multicast
1128	 * group, and must be directed to the group itself.
1129	 */
1130	dst = ip6->ip6_dst;
1131	in6_clearscope(&dst);
1132	if (!IN6_IS_ADDR_MULTICAST(&mld->mld_addr) ||
1133	    !IN6_ARE_ADDR_EQUAL(&mld->mld_addr, &dst)) {
1134		CTR3(KTR_MLD, "ignore v1 query dst %s on ifp %p(%s)",
1135		    ip6_sprintf(ip6tbuf, &ip6->ip6_dst),
1136		    ifp, ifp->if_xname);
1137		return (EINVAL);
1138	}
1139
1140	/*
1141	 * Make sure we don't hear our own membership report, as fast
1142	 * leave requires knowing that we are the only member of a
1143	 * group. Assume we used the link-local address if available,
1144	 * otherwise look for ::.
1145	 *
1146	 * XXX Note that scope ID comparison is needed for the address
1147	 * returned by in6ifa_ifpforlinklocal(), but SHOULD NOT be
1148	 * performed for the on-wire address.
1149	 */
1150	ia = in6ifa_ifpforlinklocal(ifp, IN6_IFF_NOTREADY|IN6_IFF_ANYCAST);
1151	if ((ia && IN6_ARE_ADDR_EQUAL(&ip6->ip6_src, IA6_IN6(ia))) ||
1152	    (ia == NULL && IN6_IS_ADDR_UNSPECIFIED(&src))) {
1153		if (ia != NULL)
1154			ifa_free(&ia->ia_ifa);
1155		return (0);
1156	}
1157	if (ia != NULL)
1158		ifa_free(&ia->ia_ifa);
1159
1160	CTR3(KTR_MLD, "process v1 report %s on ifp %p(%s)",
1161	    ip6_sprintf(ip6tbuf, &mld->mld_addr), ifp, ifp->if_xname);
1162
1163	/*
1164	 * Embed scope ID of receiving interface in MLD query for lookup
1165	 * whilst we don't hold other locks (due to KAME locking lameness).
1166	 */
1167	if (!IN6_IS_ADDR_UNSPECIFIED(&mld->mld_addr))
1168		in6_setscope(&mld->mld_addr, ifp, NULL);
1169
1170	IN6_MULTI_LOCK();
1171	MLD_LOCK();
1172	IF_ADDR_RLOCK(ifp);
1173
1174	/*
1175	 * MLDv1 report suppression.
1176	 * If we are a member of this group, and our membership should be
1177	 * reported, and our group timer is pending or about to be reset,
1178	 * stop our group timer by transitioning to the 'lazy' state.
1179	 */
1180	inm = in6m_lookup_locked(ifp, &mld->mld_addr);
1181	if (inm != NULL) {
1182		struct mld_ifinfo *mli;
1183
1184		mli = inm->in6m_mli;
1185		KASSERT(mli != NULL,
1186		    ("%s: no mli for ifp %p", __func__, ifp));
1187
1188		/*
1189		 * If we are in MLDv2 host mode, do not allow the
1190		 * other host's MLDv1 report to suppress our reports.
1191		 */
1192		if (mli->mli_version == MLD_VERSION_2)
1193			goto out_locked;
1194
1195		inm->in6m_timer = 0;
1196
1197		switch (inm->in6m_state) {
1198		case MLD_NOT_MEMBER:
1199		case MLD_SILENT_MEMBER:
1200		case MLD_SLEEPING_MEMBER:
1201			break;
1202		case MLD_REPORTING_MEMBER:
1203		case MLD_IDLE_MEMBER:
1204		case MLD_AWAKENING_MEMBER:
1205			CTR3(KTR_MLD,
1206			    "report suppressed for %s on ifp %p(%s)",
1207			    ip6_sprintf(ip6tbuf, &mld->mld_addr),
1208			    ifp, ifp->if_xname);
1209		case MLD_LAZY_MEMBER:
1210			inm->in6m_state = MLD_LAZY_MEMBER;
1211			break;
1212		case MLD_G_QUERY_PENDING_MEMBER:
1213		case MLD_SG_QUERY_PENDING_MEMBER:
1214		case MLD_LEAVING_MEMBER:
1215			break;
1216		}
1217	}
1218
1219out_locked:
1220	IF_ADDR_RUNLOCK(ifp);
1221	MLD_UNLOCK();
1222	IN6_MULTI_UNLOCK();
1223
1224	/* XXX Clear embedded scope ID as userland won't expect it. */
1225	in6_clearscope(&mld->mld_addr);
1226
1227	return (0);
1228}
1229
1230/*
1231 * MLD input path.
1232 *
1233 * Assume query messages which fit in a single ICMPv6 message header
1234 * have been pulled up.
1235 * Assume that userland will want to see the message, even if it
1236 * otherwise fails kernel input validation; do not free it.
1237 * Pullup may however free the mbuf chain m if it fails.
1238 *
1239 * Return IPPROTO_DONE if we freed m. Otherwise, return 0.
1240 */
1241int
1242mld_input(struct mbuf *m, int off, int icmp6len)
1243{
1244	struct ifnet	*ifp;
1245	struct ip6_hdr	*ip6;
1246	struct mld_hdr	*mld;
1247	int		 mldlen;
1248
1249	CTR3(KTR_MLD, "%s: called w/mbuf (%p,%d)", __func__, m, off);
1250
1251	ifp = m->m_pkthdr.rcvif;
1252
1253	ip6 = mtod(m, struct ip6_hdr *);
1254
1255	/* Pullup to appropriate size. */
1256	mld = (struct mld_hdr *)(mtod(m, uint8_t *) + off);
1257	if (mld->mld_type == MLD_LISTENER_QUERY &&
1258	    icmp6len >= sizeof(struct mldv2_query)) {
1259		mldlen = sizeof(struct mldv2_query);
1260	} else {
1261		mldlen = sizeof(struct mld_hdr);
1262	}
1263	IP6_EXTHDR_GET(mld, struct mld_hdr *, m, off, mldlen);
1264	if (mld == NULL) {
1265		ICMP6STAT_INC(icp6s_badlen);
1266		return (IPPROTO_DONE);
1267	}
1268
1269	/*
1270	 * Userland needs to see all of this traffic for implementing
1271	 * the endpoint discovery portion of multicast routing.
1272	 */
1273	switch (mld->mld_type) {
1274	case MLD_LISTENER_QUERY:
1275		icmp6_ifstat_inc(ifp, ifs6_in_mldquery);
1276		if (icmp6len == sizeof(struct mld_hdr)) {
1277			if (mld_v1_input_query(ifp, ip6, mld) != 0)
1278				return (0);
1279		} else if (icmp6len >= sizeof(struct mldv2_query)) {
1280			if (mld_v2_input_query(ifp, ip6, m, off,
1281			    icmp6len) != 0)
1282				return (0);
1283		}
1284		break;
1285	case MLD_LISTENER_REPORT:
1286		icmp6_ifstat_inc(ifp, ifs6_in_mldreport);
1287		if (mld_v1_input_report(ifp, ip6, mld) != 0)
1288			return (0);
1289		break;
1290	case MLDV2_LISTENER_REPORT:
1291		icmp6_ifstat_inc(ifp, ifs6_in_mldreport);
1292		break;
1293	case MLD_LISTENER_DONE:
1294		icmp6_ifstat_inc(ifp, ifs6_in_mlddone);
1295		break;
1296	default:
1297		break;
1298	}
1299
1300	return (0);
1301}
1302
1303/*
1304 * Fast timeout handler (global).
1305 * VIMAGE: Timeout handlers are expected to service all vimages.
1306 */
1307void
1308mld_fasttimo(void)
1309{
1310	VNET_ITERATOR_DECL(vnet_iter);
1311
1312	VNET_LIST_RLOCK_NOSLEEP();
1313	VNET_FOREACH(vnet_iter) {
1314		CURVNET_SET(vnet_iter);
1315		mld_fasttimo_vnet();
1316		CURVNET_RESTORE();
1317	}
1318	VNET_LIST_RUNLOCK_NOSLEEP();
1319}
1320
1321/*
1322 * Fast timeout handler (per-vnet).
1323 *
1324 * VIMAGE: Assume caller has set up our curvnet.
1325 */
1326static void
1327mld_fasttimo_vnet(void)
1328{
1329	struct ifqueue		 scq;	/* State-change packets */
1330	struct ifqueue		 qrq;	/* Query response packets */
1331	struct ifnet		*ifp;
1332	struct mld_ifinfo	*mli;
1333	struct ifmultiaddr	*ifma;
1334	struct in6_multi	*inm, *tinm;
1335	int			 uri_fasthz;
1336
1337	uri_fasthz = 0;
1338
1339	/*
1340	 * Quick check to see if any work needs to be done, in order to
1341	 * minimize the overhead of fasttimo processing.
1342	 * SMPng: XXX Unlocked reads.
1343	 */
1344	if (!V_current_state_timers_running6 &&
1345	    !V_interface_timers_running6 &&
1346	    !V_state_change_timers_running6)
1347		return;
1348
1349	IN6_MULTI_LOCK();
1350	MLD_LOCK();
1351
1352	/*
1353	 * MLDv2 General Query response timer processing.
1354	 */
1355	if (V_interface_timers_running6) {
1356		CTR1(KTR_MLD, "%s: interface timers running", __func__);
1357
1358		V_interface_timers_running6 = 0;
1359		LIST_FOREACH(mli, &V_mli_head, mli_link) {
1360			if (mli->mli_v2_timer == 0) {
1361				/* Do nothing. */
1362			} else if (--mli->mli_v2_timer == 0) {
1363				mld_v2_dispatch_general_query(mli);
1364			} else {
1365				V_interface_timers_running6 = 1;
1366			}
1367		}
1368	}
1369
1370	if (!V_current_state_timers_running6 &&
1371	    !V_state_change_timers_running6)
1372		goto out_locked;
1373
1374	V_current_state_timers_running6 = 0;
1375	V_state_change_timers_running6 = 0;
1376
1377	CTR1(KTR_MLD, "%s: state change timers running", __func__);
1378
1379	/*
1380	 * MLD host report and state-change timer processing.
1381	 * Note: Processing a v2 group timer may remove a node.
1382	 */
1383	LIST_FOREACH(mli, &V_mli_head, mli_link) {
1384		ifp = mli->mli_ifp;
1385
1386		if (mli->mli_version == MLD_VERSION_2) {
1387			uri_fasthz = MLD_RANDOM_DELAY(mli->mli_uri *
1388			    PR_FASTHZ);
1389
1390			memset(&qrq, 0, sizeof(struct ifqueue));
1391			IFQ_SET_MAXLEN(&qrq, MLD_MAX_G_GS_PACKETS);
1392
1393			memset(&scq, 0, sizeof(struct ifqueue));
1394			IFQ_SET_MAXLEN(&scq, MLD_MAX_STATE_CHANGE_PACKETS);
1395		}
1396
1397		IF_ADDR_RLOCK(ifp);
1398		TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
1399			if (ifma->ifma_addr->sa_family != AF_INET6 ||
1400			    ifma->ifma_protospec == NULL)
1401				continue;
1402			inm = (struct in6_multi *)ifma->ifma_protospec;
1403			switch (mli->mli_version) {
1404			case MLD_VERSION_1:
1405				mld_v1_process_group_timer(mli, inm);
1406				break;
1407			case MLD_VERSION_2:
1408				mld_v2_process_group_timers(mli, &qrq,
1409				    &scq, inm, uri_fasthz);
1410				break;
1411			}
1412		}
1413		IF_ADDR_RUNLOCK(ifp);
1414
1415		switch (mli->mli_version) {
1416		case MLD_VERSION_1:
1417			/*
1418			 * Transmit reports for this lifecycle.  This
1419			 * is done while not holding IF_ADDR_LOCK
1420			 * since this can call
1421			 * in6ifa_ifpforlinklocal() which locks
1422			 * IF_ADDR_LOCK internally as well as
1423			 * ip6_output() to transmit a packet.
1424			 */
1425			SLIST_FOREACH_SAFE(inm, &mli->mli_relinmhead,
1426			    in6m_nrele, tinm) {
1427				SLIST_REMOVE_HEAD(&mli->mli_relinmhead,
1428				    in6m_nrele);
1429				(void)mld_v1_transmit_report(inm,
1430				    MLD_LISTENER_REPORT);
1431			}
1432			break;
1433		case MLD_VERSION_2:
1434			mld_dispatch_queue(&qrq, 0);
1435			mld_dispatch_queue(&scq, 0);
1436
1437			/*
1438			 * Free the in_multi reference(s) for
1439			 * this lifecycle.
1440			 */
1441			SLIST_FOREACH_SAFE(inm, &mli->mli_relinmhead,
1442			    in6m_nrele, tinm) {
1443				SLIST_REMOVE_HEAD(&mli->mli_relinmhead,
1444				    in6m_nrele);
1445				in6m_release_locked(inm);
1446			}
1447			break;
1448		}
1449	}
1450
1451out_locked:
1452	MLD_UNLOCK();
1453	IN6_MULTI_UNLOCK();
1454}
1455
1456/*
1457 * Update host report group timer.
1458 * Will update the global pending timer flags.
1459 */
1460static void
1461mld_v1_process_group_timer(struct mld_ifinfo *mli, struct in6_multi *inm)
1462{
1463	int report_timer_expired;
1464
1465	IN6_MULTI_LOCK_ASSERT();
1466	MLD_LOCK_ASSERT();
1467
1468	if (inm->in6m_timer == 0) {
1469		report_timer_expired = 0;
1470	} else if (--inm->in6m_timer == 0) {
1471		report_timer_expired = 1;
1472	} else {
1473		V_current_state_timers_running6 = 1;
1474		return;
1475	}
1476
1477	switch (inm->in6m_state) {
1478	case MLD_NOT_MEMBER:
1479	case MLD_SILENT_MEMBER:
1480	case MLD_IDLE_MEMBER:
1481	case MLD_LAZY_MEMBER:
1482	case MLD_SLEEPING_MEMBER:
1483	case MLD_AWAKENING_MEMBER:
1484		break;
1485	case MLD_REPORTING_MEMBER:
1486		if (report_timer_expired) {
1487			inm->in6m_state = MLD_IDLE_MEMBER;
1488			SLIST_INSERT_HEAD(&mli->mli_relinmhead, inm,
1489			    in6m_nrele);
1490		}
1491		break;
1492	case MLD_G_QUERY_PENDING_MEMBER:
1493	case MLD_SG_QUERY_PENDING_MEMBER:
1494	case MLD_LEAVING_MEMBER:
1495		break;
1496	}
1497}
1498
1499/*
1500 * Update a group's timers for MLDv2.
1501 * Will update the global pending timer flags.
1502 * Note: Unlocked read from mli.
1503 */
1504static void
1505mld_v2_process_group_timers(struct mld_ifinfo *mli,
1506    struct ifqueue *qrq, struct ifqueue *scq,
1507    struct in6_multi *inm, const int uri_fasthz)
1508{
1509	int query_response_timer_expired;
1510	int state_change_retransmit_timer_expired;
1511#ifdef KTR
1512	char ip6tbuf[INET6_ADDRSTRLEN];
1513#endif
1514
1515	IN6_MULTI_LOCK_ASSERT();
1516	MLD_LOCK_ASSERT();
1517
1518	query_response_timer_expired = 0;
1519	state_change_retransmit_timer_expired = 0;
1520
1521	/*
1522	 * During a transition from compatibility mode back to MLDv2,
1523	 * a group record in REPORTING state may still have its group
1524	 * timer active. This is a no-op in this function; it is easier
1525	 * to deal with it here than to complicate the slow-timeout path.
1526	 */
1527	if (inm->in6m_timer == 0) {
1528		query_response_timer_expired = 0;
1529	} else if (--inm->in6m_timer == 0) {
1530		query_response_timer_expired = 1;
1531	} else {
1532		V_current_state_timers_running6 = 1;
1533	}
1534
1535	if (inm->in6m_sctimer == 0) {
1536		state_change_retransmit_timer_expired = 0;
1537	} else if (--inm->in6m_sctimer == 0) {
1538		state_change_retransmit_timer_expired = 1;
1539	} else {
1540		V_state_change_timers_running6 = 1;
1541	}
1542
1543	/* We are in fasttimo, so be quick about it. */
1544	if (!state_change_retransmit_timer_expired &&
1545	    !query_response_timer_expired)
1546		return;
1547
1548	switch (inm->in6m_state) {
1549	case MLD_NOT_MEMBER:
1550	case MLD_SILENT_MEMBER:
1551	case MLD_SLEEPING_MEMBER:
1552	case MLD_LAZY_MEMBER:
1553	case MLD_AWAKENING_MEMBER:
1554	case MLD_IDLE_MEMBER:
1555		break;
1556	case MLD_G_QUERY_PENDING_MEMBER:
1557	case MLD_SG_QUERY_PENDING_MEMBER:
1558		/*
1559		 * Respond to a previously pending Group-Specific
1560		 * or Group-and-Source-Specific query by enqueueing
1561		 * the appropriate Current-State report for
1562		 * immediate transmission.
1563		 */
1564		if (query_response_timer_expired) {
1565			int retval;
1566
1567			retval = mld_v2_enqueue_group_record(qrq, inm, 0, 1,
1568			    (inm->in6m_state == MLD_SG_QUERY_PENDING_MEMBER),
1569			    0);
1570			CTR2(KTR_MLD, "%s: enqueue record = %d",
1571			    __func__, retval);
1572			inm->in6m_state = MLD_REPORTING_MEMBER;
1573			in6m_clear_recorded(inm);
1574		}
1575		/* FALLTHROUGH */
1576	case MLD_REPORTING_MEMBER:
1577	case MLD_LEAVING_MEMBER:
1578		if (state_change_retransmit_timer_expired) {
1579			/*
1580			 * State-change retransmission timer fired.
1581			 * If there are any further pending retransmissions,
1582			 * set the global pending state-change flag, and
1583			 * reset the timer.
1584			 */
1585			if (--inm->in6m_scrv > 0) {
1586				inm->in6m_sctimer = uri_fasthz;
1587				V_state_change_timers_running6 = 1;
1588			}
1589			/*
1590			 * Retransmit the previously computed state-change
1591			 * report. If there are no further pending
1592			 * retransmissions, the mbuf queue will be consumed.
1593			 * Update T0 state to T1 as we have now sent
1594			 * a state-change.
1595			 */
1596			(void)mld_v2_merge_state_changes(inm, scq);
1597
1598			in6m_commit(inm);
1599			CTR3(KTR_MLD, "%s: T1 -> T0 for %s/%s", __func__,
1600			    ip6_sprintf(ip6tbuf, &inm->in6m_addr),
1601			    inm->in6m_ifp->if_xname);
1602
1603			/*
1604			 * If we are leaving the group for good, make sure
1605			 * we release MLD's reference to it.
1606			 * This release must be deferred using a SLIST,
1607			 * as we are called from a loop which traverses
1608			 * the in_ifmultiaddr TAILQ.
1609			 */
1610			if (inm->in6m_state == MLD_LEAVING_MEMBER &&
1611			    inm->in6m_scrv == 0) {
1612				inm->in6m_state = MLD_NOT_MEMBER;
1613				SLIST_INSERT_HEAD(&mli->mli_relinmhead,
1614				    inm, in6m_nrele);
1615			}
1616		}
1617		break;
1618	}
1619}
1620
1621/*
1622 * Switch to a different version on the given interface,
1623 * as per Section 9.12.
1624 */
1625static void
1626mld_set_version(struct mld_ifinfo *mli, const int version)
1627{
1628	int old_version_timer;
1629
1630	MLD_LOCK_ASSERT();
1631
1632	CTR4(KTR_MLD, "%s: switching to v%d on ifp %p(%s)", __func__,
1633	    version, mli->mli_ifp, mli->mli_ifp->if_xname);
1634
1635	if (version == MLD_VERSION_1) {
1636		/*
1637		 * Compute the "Older Version Querier Present" timer as per
1638		 * Section 9.12.
1639		 */
1640		old_version_timer = (mli->mli_rv * mli->mli_qi) + mli->mli_qri;
1641		old_version_timer *= PR_SLOWHZ;
1642		mli->mli_v1_timer = old_version_timer;
1643	}
1644
1645	if (mli->mli_v1_timer > 0 && mli->mli_version != MLD_VERSION_1) {
1646		mli->mli_version = MLD_VERSION_1;
1647		mld_v2_cancel_link_timers(mli);
1648	}
1649}
1650
1651/*
1652 * Cancel pending MLDv2 timers for the given link and all groups
1653 * joined on it; state-change, general-query, and group-query timers.
1654 */
1655static void
1656mld_v2_cancel_link_timers(struct mld_ifinfo *mli)
1657{
1658	struct ifmultiaddr	*ifma;
1659	struct ifnet		*ifp;
1660	struct in6_multi	*inm, *tinm;
1661
1662	CTR3(KTR_MLD, "%s: cancel v2 timers on ifp %p(%s)", __func__,
1663	    mli->mli_ifp, mli->mli_ifp->if_xname);
1664
1665	IN6_MULTI_LOCK_ASSERT();
1666	MLD_LOCK_ASSERT();
1667
1668	/*
1669	 * Fast-track this potentially expensive operation
1670	 * by checking all the global 'timer pending' flags.
1671	 */
1672	if (!V_interface_timers_running6 &&
1673	    !V_state_change_timers_running6 &&
1674	    !V_current_state_timers_running6)
1675		return;
1676
1677	mli->mli_v2_timer = 0;
1678
1679	ifp = mli->mli_ifp;
1680
1681	IF_ADDR_RLOCK(ifp);
1682	TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
1683		if (ifma->ifma_addr->sa_family != AF_INET6)
1684			continue;
1685		inm = (struct in6_multi *)ifma->ifma_protospec;
1686		switch (inm->in6m_state) {
1687		case MLD_NOT_MEMBER:
1688		case MLD_SILENT_MEMBER:
1689		case MLD_IDLE_MEMBER:
1690		case MLD_LAZY_MEMBER:
1691		case MLD_SLEEPING_MEMBER:
1692		case MLD_AWAKENING_MEMBER:
1693			break;
1694		case MLD_LEAVING_MEMBER:
1695			/*
1696			 * If we are leaving the group and switching
1697			 * version, we need to release the final
1698			 * reference held for issuing the INCLUDE {}.
1699			 */
1700			SLIST_INSERT_HEAD(&mli->mli_relinmhead, inm,
1701			    in6m_nrele);
1702			/* FALLTHROUGH */
1703		case MLD_G_QUERY_PENDING_MEMBER:
1704		case MLD_SG_QUERY_PENDING_MEMBER:
1705			in6m_clear_recorded(inm);
1706			/* FALLTHROUGH */
1707		case MLD_REPORTING_MEMBER:
1708			inm->in6m_sctimer = 0;
1709			inm->in6m_timer = 0;
1710			inm->in6m_state = MLD_REPORTING_MEMBER;
1711			/*
1712			 * Free any pending MLDv2 state-change records.
1713			 */
1714			_IF_DRAIN(&inm->in6m_scq);
1715			break;
1716		}
1717	}
1718	IF_ADDR_RUNLOCK(ifp);
1719	SLIST_FOREACH_SAFE(inm, &mli->mli_relinmhead, in6m_nrele, tinm) {
1720		SLIST_REMOVE_HEAD(&mli->mli_relinmhead, in6m_nrele);
1721		in6m_release_locked(inm);
1722	}
1723}
1724
1725/*
1726 * Global slowtimo handler.
1727 * VIMAGE: Timeout handlers are expected to service all vimages.
1728 */
1729void
1730mld_slowtimo(void)
1731{
1732	VNET_ITERATOR_DECL(vnet_iter);
1733
1734	VNET_LIST_RLOCK_NOSLEEP();
1735	VNET_FOREACH(vnet_iter) {
1736		CURVNET_SET(vnet_iter);
1737		mld_slowtimo_vnet();
1738		CURVNET_RESTORE();
1739	}
1740	VNET_LIST_RUNLOCK_NOSLEEP();
1741}
1742
1743/*
1744 * Per-vnet slowtimo handler.
1745 */
1746static void
1747mld_slowtimo_vnet(void)
1748{
1749	struct mld_ifinfo *mli;
1750
1751	MLD_LOCK();
1752
1753	LIST_FOREACH(mli, &V_mli_head, mli_link) {
1754		mld_v1_process_querier_timers(mli);
1755	}
1756
1757	MLD_UNLOCK();
1758}
1759
1760/*
1761 * Update the Older Version Querier Present timers for a link.
1762 * See Section 9.12 of RFC 3810.
1763 */
1764static void
1765mld_v1_process_querier_timers(struct mld_ifinfo *mli)
1766{
1767
1768	MLD_LOCK_ASSERT();
1769
1770	if (mli->mli_version != MLD_VERSION_2 && --mli->mli_v1_timer == 0) {
1771		/*
1772		 * MLDv1 Querier Present timer expired; revert to MLDv2.
1773		 */
1774		CTR5(KTR_MLD,
1775		    "%s: transition from v%d -> v%d on %p(%s)",
1776		    __func__, mli->mli_version, MLD_VERSION_2,
1777		    mli->mli_ifp, mli->mli_ifp->if_xname);
1778		mli->mli_version = MLD_VERSION_2;
1779	}
1780}
1781
1782/*
1783 * Transmit an MLDv1 report immediately.
1784 */
1785static int
1786mld_v1_transmit_report(struct in6_multi *in6m, const int type)
1787{
1788	struct ifnet		*ifp;
1789	struct in6_ifaddr	*ia;
1790	struct ip6_hdr		*ip6;
1791	struct mbuf		*mh, *md;
1792	struct mld_hdr		*mld;
1793
1794	IN6_MULTI_LOCK_ASSERT();
1795	MLD_LOCK_ASSERT();
1796
1797	ifp = in6m->in6m_ifp;
1798	ia = in6ifa_ifpforlinklocal(ifp, IN6_IFF_NOTREADY|IN6_IFF_ANYCAST);
1799	/* ia may be NULL if link-local address is tentative. */
1800
1801	MGETHDR(mh, M_DONTWAIT, MT_HEADER);
1802	if (mh == NULL) {
1803		if (ia != NULL)
1804			ifa_free(&ia->ia_ifa);
1805		return (ENOMEM);
1806	}
1807	MGET(md, M_DONTWAIT, MT_DATA);
1808	if (md == NULL) {
1809		m_free(mh);
1810		if (ia != NULL)
1811			ifa_free(&ia->ia_ifa);
1812		return (ENOMEM);
1813	}
1814	mh->m_next = md;
1815
1816	/*
1817	 * FUTURE: Consider increasing alignment by ETHER_HDR_LEN, so
1818	 * that ether_output() does not need to allocate another mbuf
1819	 * for the header in the most common case.
1820	 */
1821	MH_ALIGN(mh, sizeof(struct ip6_hdr));
1822	mh->m_pkthdr.len = sizeof(struct ip6_hdr) + sizeof(struct mld_hdr);
1823	mh->m_len = sizeof(struct ip6_hdr);
1824
1825	ip6 = mtod(mh, struct ip6_hdr *);
1826	ip6->ip6_flow = 0;
1827	ip6->ip6_vfc &= ~IPV6_VERSION_MASK;
1828	ip6->ip6_vfc |= IPV6_VERSION;
1829	ip6->ip6_nxt = IPPROTO_ICMPV6;
1830	ip6->ip6_src = ia ? ia->ia_addr.sin6_addr : in6addr_any;
1831	ip6->ip6_dst = in6m->in6m_addr;
1832
1833	md->m_len = sizeof(struct mld_hdr);
1834	mld = mtod(md, struct mld_hdr *);
1835	mld->mld_type = type;
1836	mld->mld_code = 0;
1837	mld->mld_cksum = 0;
1838	mld->mld_maxdelay = 0;
1839	mld->mld_reserved = 0;
1840	mld->mld_addr = in6m->in6m_addr;
1841	in6_clearscope(&mld->mld_addr);
1842	mld->mld_cksum = in6_cksum(mh, IPPROTO_ICMPV6,
1843	    sizeof(struct ip6_hdr), sizeof(struct mld_hdr));
1844
1845	mld_save_context(mh, ifp);
1846	mh->m_flags |= M_MLDV1;
1847
1848	mld_dispatch_packet(mh);
1849
1850	if (ia != NULL)
1851		ifa_free(&ia->ia_ifa);
1852	return (0);
1853}
1854
1855/*
1856 * Process a state change from the upper layer for the given IPv6 group.
1857 *
1858 * Each socket holds a reference on the in_multi in its own ip_moptions.
1859 * The socket layer will have made the necessary updates to.the group
1860 * state, it is now up to MLD to issue a state change report if there
1861 * has been any change between T0 (when the last state-change was issued)
1862 * and T1 (now).
1863 *
1864 * We use the MLDv2 state machine at group level. The MLd module
1865 * however makes the decision as to which MLD protocol version to speak.
1866 * A state change *from* INCLUDE {} always means an initial join.
1867 * A state change *to* INCLUDE {} always means a final leave.
1868 *
1869 * If delay is non-zero, and the state change is an initial multicast
1870 * join, the state change report will be delayed by 'delay' ticks
1871 * in units of PR_FASTHZ if MLDv1 is active on the link; otherwise
1872 * the initial MLDv2 state change report will be delayed by whichever
1873 * is sooner, a pending state-change timer or delay itself.
1874 *
1875 * VIMAGE: curvnet should have been set by caller, as this routine
1876 * is called from the socket option handlers.
1877 */
1878int
1879mld_change_state(struct in6_multi *inm, const int delay)
1880{
1881	struct mld_ifinfo *mli;
1882	struct ifnet *ifp;
1883	int error;
1884
1885	IN6_MULTI_LOCK_ASSERT();
1886
1887	error = 0;
1888
1889	/*
1890	 * Try to detect if the upper layer just asked us to change state
1891	 * for an interface which has now gone away.
1892	 */
1893	KASSERT(inm->in6m_ifma != NULL, ("%s: no ifma", __func__));
1894	ifp = inm->in6m_ifma->ifma_ifp;
1895	if (ifp != NULL) {
1896		/*
1897		 * Sanity check that netinet6's notion of ifp is the
1898		 * same as net's.
1899		 */
1900		KASSERT(inm->in6m_ifp == ifp, ("%s: bad ifp", __func__));
1901	}
1902
1903	MLD_LOCK();
1904
1905	mli = MLD_IFINFO(ifp);
1906	KASSERT(mli != NULL, ("%s: no mld_ifinfo for ifp %p", __func__, ifp));
1907
1908	/*
1909	 * If we detect a state transition to or from MCAST_UNDEFINED
1910	 * for this group, then we are starting or finishing an MLD
1911	 * life cycle for this group.
1912	 */
1913	if (inm->in6m_st[1].iss_fmode != inm->in6m_st[0].iss_fmode) {
1914		CTR3(KTR_MLD, "%s: inm transition %d -> %d", __func__,
1915		    inm->in6m_st[0].iss_fmode, inm->in6m_st[1].iss_fmode);
1916		if (inm->in6m_st[0].iss_fmode == MCAST_UNDEFINED) {
1917			CTR1(KTR_MLD, "%s: initial join", __func__);
1918			error = mld_initial_join(inm, mli, delay);
1919			goto out_locked;
1920		} else if (inm->in6m_st[1].iss_fmode == MCAST_UNDEFINED) {
1921			CTR1(KTR_MLD, "%s: final leave", __func__);
1922			mld_final_leave(inm, mli);
1923			goto out_locked;
1924		}
1925	} else {
1926		CTR1(KTR_MLD, "%s: filter set change", __func__);
1927	}
1928
1929	error = mld_handle_state_change(inm, mli);
1930
1931out_locked:
1932	MLD_UNLOCK();
1933	return (error);
1934}
1935
1936/*
1937 * Perform the initial join for an MLD group.
1938 *
1939 * When joining a group:
1940 *  If the group should have its MLD traffic suppressed, do nothing.
1941 *  MLDv1 starts sending MLDv1 host membership reports.
1942 *  MLDv2 will schedule an MLDv2 state-change report containing the
1943 *  initial state of the membership.
1944 *
1945 * If the delay argument is non-zero, then we must delay sending the
1946 * initial state change for delay ticks (in units of PR_FASTHZ).
1947 */
1948static int
1949mld_initial_join(struct in6_multi *inm, struct mld_ifinfo *mli,
1950    const int delay)
1951{
1952	struct ifnet		*ifp;
1953	struct ifqueue		*ifq;
1954	int			 error, retval, syncstates;
1955	int			 odelay;
1956#ifdef KTR
1957	char			 ip6tbuf[INET6_ADDRSTRLEN];
1958#endif
1959
1960	CTR4(KTR_MLD, "%s: initial join %s on ifp %p(%s)",
1961	    __func__, ip6_sprintf(ip6tbuf, &inm->in6m_addr),
1962	    inm->in6m_ifp, inm->in6m_ifp->if_xname);
1963
1964	error = 0;
1965	syncstates = 1;
1966
1967	ifp = inm->in6m_ifp;
1968
1969	IN6_MULTI_LOCK_ASSERT();
1970	MLD_LOCK_ASSERT();
1971
1972	KASSERT(mli && mli->mli_ifp == ifp, ("%s: inconsistent ifp", __func__));
1973
1974	/*
1975	 * Groups joined on loopback or marked as 'not reported',
1976	 * enter the MLD_SILENT_MEMBER state and
1977	 * are never reported in any protocol exchanges.
1978	 * All other groups enter the appropriate state machine
1979	 * for the version in use on this link.
1980	 * A link marked as MLIF_SILENT causes MLD to be completely
1981	 * disabled for the link.
1982	 */
1983	if ((ifp->if_flags & IFF_LOOPBACK) ||
1984	    (mli->mli_flags & MLIF_SILENT) ||
1985	    !mld_is_addr_reported(&inm->in6m_addr)) {
1986		CTR1(KTR_MLD,
1987"%s: not kicking state machine for silent group", __func__);
1988		inm->in6m_state = MLD_SILENT_MEMBER;
1989		inm->in6m_timer = 0;
1990	} else {
1991		/*
1992		 * Deal with overlapping in_multi lifecycle.
1993		 * If this group was LEAVING, then make sure
1994		 * we drop the reference we picked up to keep the
1995		 * group around for the final INCLUDE {} enqueue.
1996		 */
1997		if (mli->mli_version == MLD_VERSION_2 &&
1998		    inm->in6m_state == MLD_LEAVING_MEMBER)
1999			in6m_release_locked(inm);
2000
2001		inm->in6m_state = MLD_REPORTING_MEMBER;
2002
2003		switch (mli->mli_version) {
2004		case MLD_VERSION_1:
2005			/*
2006			 * If a delay was provided, only use it if
2007			 * it is greater than the delay normally
2008			 * used for an MLDv1 state change report,
2009			 * and delay sending the initial MLDv1 report
2010			 * by not transitioning to the IDLE state.
2011			 */
2012			odelay = MLD_RANDOM_DELAY(MLD_V1_MAX_RI * PR_FASTHZ);
2013			if (delay) {
2014				inm->in6m_timer = max(delay, odelay);
2015				V_current_state_timers_running6 = 1;
2016			} else {
2017				inm->in6m_state = MLD_IDLE_MEMBER;
2018				error = mld_v1_transmit_report(inm,
2019				     MLD_LISTENER_REPORT);
2020				if (error == 0) {
2021					inm->in6m_timer = odelay;
2022					V_current_state_timers_running6 = 1;
2023				}
2024			}
2025			break;
2026
2027		case MLD_VERSION_2:
2028			/*
2029			 * Defer update of T0 to T1, until the first copy
2030			 * of the state change has been transmitted.
2031			 */
2032			syncstates = 0;
2033
2034			/*
2035			 * Immediately enqueue a State-Change Report for
2036			 * this interface, freeing any previous reports.
2037			 * Don't kick the timers if there is nothing to do,
2038			 * or if an error occurred.
2039			 */
2040			ifq = &inm->in6m_scq;
2041			_IF_DRAIN(ifq);
2042			retval = mld_v2_enqueue_group_record(ifq, inm, 1,
2043			    0, 0, (mli->mli_flags & MLIF_USEALLOW));
2044			CTR2(KTR_MLD, "%s: enqueue record = %d",
2045			    __func__, retval);
2046			if (retval <= 0) {
2047				error = retval * -1;
2048				break;
2049			}
2050
2051			/*
2052			 * Schedule transmission of pending state-change
2053			 * report up to RV times for this link. The timer
2054			 * will fire at the next mld_fasttimo (~200ms),
2055			 * giving us an opportunity to merge the reports.
2056			 *
2057			 * If a delay was provided to this function, only
2058			 * use this delay if sooner than the existing one.
2059			 */
2060			KASSERT(mli->mli_rv > 1,
2061			   ("%s: invalid robustness %d", __func__,
2062			    mli->mli_rv));
2063			inm->in6m_scrv = mli->mli_rv;
2064			if (delay) {
2065				if (inm->in6m_sctimer > 1) {
2066					inm->in6m_sctimer =
2067					    min(inm->in6m_sctimer, delay);
2068				} else
2069					inm->in6m_sctimer = delay;
2070			} else
2071				inm->in6m_sctimer = 1;
2072			V_state_change_timers_running6 = 1;
2073
2074			error = 0;
2075			break;
2076		}
2077	}
2078
2079	/*
2080	 * Only update the T0 state if state change is atomic,
2081	 * i.e. we don't need to wait for a timer to fire before we
2082	 * can consider the state change to have been communicated.
2083	 */
2084	if (syncstates) {
2085		in6m_commit(inm);
2086		CTR3(KTR_MLD, "%s: T1 -> T0 for %s/%s", __func__,
2087		    ip6_sprintf(ip6tbuf, &inm->in6m_addr),
2088		    inm->in6m_ifp->if_xname);
2089	}
2090
2091	return (error);
2092}
2093
2094/*
2095 * Issue an intermediate state change during the life-cycle.
2096 */
2097static int
2098mld_handle_state_change(struct in6_multi *inm, struct mld_ifinfo *mli)
2099{
2100	struct ifnet		*ifp;
2101	int			 retval;
2102#ifdef KTR
2103	char			 ip6tbuf[INET6_ADDRSTRLEN];
2104#endif
2105
2106	CTR4(KTR_MLD, "%s: state change for %s on ifp %p(%s)",
2107	    __func__, ip6_sprintf(ip6tbuf, &inm->in6m_addr),
2108	    inm->in6m_ifp, inm->in6m_ifp->if_xname);
2109
2110	ifp = inm->in6m_ifp;
2111
2112	IN6_MULTI_LOCK_ASSERT();
2113	MLD_LOCK_ASSERT();
2114
2115	KASSERT(mli && mli->mli_ifp == ifp,
2116	    ("%s: inconsistent ifp", __func__));
2117
2118	if ((ifp->if_flags & IFF_LOOPBACK) ||
2119	    (mli->mli_flags & MLIF_SILENT) ||
2120	    !mld_is_addr_reported(&inm->in6m_addr) ||
2121	    (mli->mli_version != MLD_VERSION_2)) {
2122		if (!mld_is_addr_reported(&inm->in6m_addr)) {
2123			CTR1(KTR_MLD,
2124"%s: not kicking state machine for silent group", __func__);
2125		}
2126		CTR1(KTR_MLD, "%s: nothing to do", __func__);
2127		in6m_commit(inm);
2128		CTR3(KTR_MLD, "%s: T1 -> T0 for %s/%s", __func__,
2129		    ip6_sprintf(ip6tbuf, &inm->in6m_addr),
2130		    inm->in6m_ifp->if_xname);
2131		return (0);
2132	}
2133
2134	_IF_DRAIN(&inm->in6m_scq);
2135
2136	retval = mld_v2_enqueue_group_record(&inm->in6m_scq, inm, 1, 0, 0,
2137	    (mli->mli_flags & MLIF_USEALLOW));
2138	CTR2(KTR_MLD, "%s: enqueue record = %d", __func__, retval);
2139	if (retval <= 0)
2140		return (-retval);
2141
2142	/*
2143	 * If record(s) were enqueued, start the state-change
2144	 * report timer for this group.
2145	 */
2146	inm->in6m_scrv = mli->mli_rv;
2147	inm->in6m_sctimer = 1;
2148	V_state_change_timers_running6 = 1;
2149
2150	return (0);
2151}
2152
2153/*
2154 * Perform the final leave for a multicast address.
2155 *
2156 * When leaving a group:
2157 *  MLDv1 sends a DONE message, if and only if we are the reporter.
2158 *  MLDv2 enqueues a state-change report containing a transition
2159 *  to INCLUDE {} for immediate transmission.
2160 */
2161static void
2162mld_final_leave(struct in6_multi *inm, struct mld_ifinfo *mli)
2163{
2164	int syncstates;
2165#ifdef KTR
2166	char ip6tbuf[INET6_ADDRSTRLEN];
2167#endif
2168
2169	syncstates = 1;
2170
2171	CTR4(KTR_MLD, "%s: final leave %s on ifp %p(%s)",
2172	    __func__, ip6_sprintf(ip6tbuf, &inm->in6m_addr),
2173	    inm->in6m_ifp, inm->in6m_ifp->if_xname);
2174
2175	IN6_MULTI_LOCK_ASSERT();
2176	MLD_LOCK_ASSERT();
2177
2178	switch (inm->in6m_state) {
2179	case MLD_NOT_MEMBER:
2180	case MLD_SILENT_MEMBER:
2181	case MLD_LEAVING_MEMBER:
2182		/* Already leaving or left; do nothing. */
2183		CTR1(KTR_MLD,
2184"%s: not kicking state machine for silent group", __func__);
2185		break;
2186	case MLD_REPORTING_MEMBER:
2187	case MLD_IDLE_MEMBER:
2188	case MLD_G_QUERY_PENDING_MEMBER:
2189	case MLD_SG_QUERY_PENDING_MEMBER:
2190		if (mli->mli_version == MLD_VERSION_1) {
2191#ifdef INVARIANTS
2192			if (inm->in6m_state == MLD_G_QUERY_PENDING_MEMBER ||
2193			    inm->in6m_state == MLD_SG_QUERY_PENDING_MEMBER)
2194			panic("%s: MLDv2 state reached, not MLDv2 mode",
2195			     __func__);
2196#endif
2197			mld_v1_transmit_report(inm, MLD_LISTENER_DONE);
2198			inm->in6m_state = MLD_NOT_MEMBER;
2199		} else if (mli->mli_version == MLD_VERSION_2) {
2200			/*
2201			 * Stop group timer and all pending reports.
2202			 * Immediately enqueue a state-change report
2203			 * TO_IN {} to be sent on the next fast timeout,
2204			 * giving us an opportunity to merge reports.
2205			 */
2206			_IF_DRAIN(&inm->in6m_scq);
2207			inm->in6m_timer = 0;
2208			inm->in6m_scrv = mli->mli_rv;
2209			CTR4(KTR_MLD, "%s: Leaving %s/%s with %d "
2210			    "pending retransmissions.", __func__,
2211			    ip6_sprintf(ip6tbuf, &inm->in6m_addr),
2212			    inm->in6m_ifp->if_xname, inm->in6m_scrv);
2213			if (inm->in6m_scrv == 0) {
2214				inm->in6m_state = MLD_NOT_MEMBER;
2215				inm->in6m_sctimer = 0;
2216			} else {
2217				int retval;
2218
2219				in6m_acquire_locked(inm);
2220
2221				retval = mld_v2_enqueue_group_record(
2222				    &inm->in6m_scq, inm, 1, 0, 0,
2223				    (mli->mli_flags & MLIF_USEALLOW));
2224				KASSERT(retval != 0,
2225				    ("%s: enqueue record = %d", __func__,
2226				     retval));
2227
2228				inm->in6m_state = MLD_LEAVING_MEMBER;
2229				inm->in6m_sctimer = 1;
2230				V_state_change_timers_running6 = 1;
2231				syncstates = 0;
2232			}
2233			break;
2234		}
2235		break;
2236	case MLD_LAZY_MEMBER:
2237	case MLD_SLEEPING_MEMBER:
2238	case MLD_AWAKENING_MEMBER:
2239		/* Our reports are suppressed; do nothing. */
2240		break;
2241	}
2242
2243	if (syncstates) {
2244		in6m_commit(inm);
2245		CTR3(KTR_MLD, "%s: T1 -> T0 for %s/%s", __func__,
2246		    ip6_sprintf(ip6tbuf, &inm->in6m_addr),
2247		    inm->in6m_ifp->if_xname);
2248		inm->in6m_st[1].iss_fmode = MCAST_UNDEFINED;
2249		CTR3(KTR_MLD, "%s: T1 now MCAST_UNDEFINED for %p/%s",
2250		    __func__, &inm->in6m_addr, inm->in6m_ifp->if_xname);
2251	}
2252}
2253
2254/*
2255 * Enqueue an MLDv2 group record to the given output queue.
2256 *
2257 * If is_state_change is zero, a current-state record is appended.
2258 * If is_state_change is non-zero, a state-change report is appended.
2259 *
2260 * If is_group_query is non-zero, an mbuf packet chain is allocated.
2261 * If is_group_query is zero, and if there is a packet with free space
2262 * at the tail of the queue, it will be appended to providing there
2263 * is enough free space.
2264 * Otherwise a new mbuf packet chain is allocated.
2265 *
2266 * If is_source_query is non-zero, each source is checked to see if
2267 * it was recorded for a Group-Source query, and will be omitted if
2268 * it is not both in-mode and recorded.
2269 *
2270 * If use_block_allow is non-zero, state change reports for initial join
2271 * and final leave, on an inclusive mode group with a source list, will be
2272 * rewritten to use the ALLOW_NEW and BLOCK_OLD record types, respectively.
2273 *
2274 * The function will attempt to allocate leading space in the packet
2275 * for the IPv6+ICMP headers to be prepended without fragmenting the chain.
2276 *
2277 * If successful the size of all data appended to the queue is returned,
2278 * otherwise an error code less than zero is returned, or zero if
2279 * no record(s) were appended.
2280 */
2281static int
2282mld_v2_enqueue_group_record(struct ifqueue *ifq, struct in6_multi *inm,
2283    const int is_state_change, const int is_group_query,
2284    const int is_source_query, const int use_block_allow)
2285{
2286	struct mldv2_record	 mr;
2287	struct mldv2_record	*pmr;
2288	struct ifnet		*ifp;
2289	struct ip6_msource	*ims, *nims;
2290	struct mbuf		*m0, *m, *md;
2291	int			 error, is_filter_list_change;
2292	int			 minrec0len, m0srcs, msrcs, nbytes, off;
2293	int			 record_has_sources;
2294	int			 now;
2295	int			 type;
2296	uint8_t			 mode;
2297#ifdef KTR
2298	char			 ip6tbuf[INET6_ADDRSTRLEN];
2299#endif
2300
2301	IN6_MULTI_LOCK_ASSERT();
2302
2303	error = 0;
2304	ifp = inm->in6m_ifp;
2305	is_filter_list_change = 0;
2306	m = NULL;
2307	m0 = NULL;
2308	m0srcs = 0;
2309	msrcs = 0;
2310	nbytes = 0;
2311	nims = NULL;
2312	record_has_sources = 1;
2313	pmr = NULL;
2314	type = MLD_DO_NOTHING;
2315	mode = inm->in6m_st[1].iss_fmode;
2316
2317	/*
2318	 * If we did not transition out of ASM mode during t0->t1,
2319	 * and there are no source nodes to process, we can skip
2320	 * the generation of source records.
2321	 */
2322	if (inm->in6m_st[0].iss_asm > 0 && inm->in6m_st[1].iss_asm > 0 &&
2323	    inm->in6m_nsrc == 0)
2324		record_has_sources = 0;
2325
2326	if (is_state_change) {
2327		/*
2328		 * Queue a state change record.
2329		 * If the mode did not change, and there are non-ASM
2330		 * listeners or source filters present,
2331		 * we potentially need to issue two records for the group.
2332		 * If there are ASM listeners, and there was no filter
2333		 * mode transition of any kind, do nothing.
2334		 *
2335		 * If we are transitioning to MCAST_UNDEFINED, we need
2336		 * not send any sources. A transition to/from this state is
2337		 * considered inclusive with some special treatment.
2338		 *
2339		 * If we are rewriting initial joins/leaves to use
2340		 * ALLOW/BLOCK, and the group's membership is inclusive,
2341		 * we need to send sources in all cases.
2342		 */
2343		if (mode != inm->in6m_st[0].iss_fmode) {
2344			if (mode == MCAST_EXCLUDE) {
2345				CTR1(KTR_MLD, "%s: change to EXCLUDE",
2346				    __func__);
2347				type = MLD_CHANGE_TO_EXCLUDE_MODE;
2348			} else {
2349				CTR1(KTR_MLD, "%s: change to INCLUDE",
2350				    __func__);
2351				if (use_block_allow) {
2352					/*
2353					 * XXX
2354					 * Here we're interested in state
2355					 * edges either direction between
2356					 * MCAST_UNDEFINED and MCAST_INCLUDE.
2357					 * Perhaps we should just check
2358					 * the group state, rather than
2359					 * the filter mode.
2360					 */
2361					if (mode == MCAST_UNDEFINED) {
2362						type = MLD_BLOCK_OLD_SOURCES;
2363					} else {
2364						type = MLD_ALLOW_NEW_SOURCES;
2365					}
2366				} else {
2367					type = MLD_CHANGE_TO_INCLUDE_MODE;
2368					if (mode == MCAST_UNDEFINED)
2369						record_has_sources = 0;
2370				}
2371			}
2372		} else {
2373			if (record_has_sources) {
2374				is_filter_list_change = 1;
2375			} else {
2376				type = MLD_DO_NOTHING;
2377			}
2378		}
2379	} else {
2380		/*
2381		 * Queue a current state record.
2382		 */
2383		if (mode == MCAST_EXCLUDE) {
2384			type = MLD_MODE_IS_EXCLUDE;
2385		} else if (mode == MCAST_INCLUDE) {
2386			type = MLD_MODE_IS_INCLUDE;
2387			KASSERT(inm->in6m_st[1].iss_asm == 0,
2388			    ("%s: inm %p is INCLUDE but ASM count is %d",
2389			     __func__, inm, inm->in6m_st[1].iss_asm));
2390		}
2391	}
2392
2393	/*
2394	 * Generate the filter list changes using a separate function.
2395	 */
2396	if (is_filter_list_change)
2397		return (mld_v2_enqueue_filter_change(ifq, inm));
2398
2399	if (type == MLD_DO_NOTHING) {
2400		CTR3(KTR_MLD, "%s: nothing to do for %s/%s",
2401		    __func__, ip6_sprintf(ip6tbuf, &inm->in6m_addr),
2402		    inm->in6m_ifp->if_xname);
2403		return (0);
2404	}
2405
2406	/*
2407	 * If any sources are present, we must be able to fit at least
2408	 * one in the trailing space of the tail packet's mbuf,
2409	 * ideally more.
2410	 */
2411	minrec0len = sizeof(struct mldv2_record);
2412	if (record_has_sources)
2413		minrec0len += sizeof(struct in6_addr);
2414
2415	CTR4(KTR_MLD, "%s: queueing %s for %s/%s", __func__,
2416	    mld_rec_type_to_str(type),
2417	    ip6_sprintf(ip6tbuf, &inm->in6m_addr),
2418	    inm->in6m_ifp->if_xname);
2419
2420	/*
2421	 * Check if we have a packet in the tail of the queue for this
2422	 * group into which the first group record for this group will fit.
2423	 * Otherwise allocate a new packet.
2424	 * Always allocate leading space for IP6+RA+ICMPV6+REPORT.
2425	 * Note: Group records for G/GSR query responses MUST be sent
2426	 * in their own packet.
2427	 */
2428	m0 = ifq->ifq_tail;
2429	if (!is_group_query &&
2430	    m0 != NULL &&
2431	    (m0->m_pkthdr.PH_vt.vt_nrecs + 1 <= MLD_V2_REPORT_MAXRECS) &&
2432	    (m0->m_pkthdr.len + minrec0len) <
2433	     (ifp->if_mtu - MLD_MTUSPACE)) {
2434		m0srcs = (ifp->if_mtu - m0->m_pkthdr.len -
2435			    sizeof(struct mldv2_record)) /
2436			    sizeof(struct in6_addr);
2437		m = m0;
2438		CTR1(KTR_MLD, "%s: use existing packet", __func__);
2439	} else {
2440		if (_IF_QFULL(ifq)) {
2441			CTR1(KTR_MLD, "%s: outbound queue full", __func__);
2442			return (-ENOMEM);
2443		}
2444		m = NULL;
2445		m0srcs = (ifp->if_mtu - MLD_MTUSPACE -
2446		    sizeof(struct mldv2_record)) / sizeof(struct in6_addr);
2447		if (!is_state_change && !is_group_query)
2448			m = m_getcl(M_DONTWAIT, MT_DATA, M_PKTHDR);
2449		if (m == NULL)
2450			m = m_gethdr(M_DONTWAIT, MT_DATA);
2451		if (m == NULL)
2452			return (-ENOMEM);
2453
2454		mld_save_context(m, ifp);
2455
2456		CTR1(KTR_MLD, "%s: allocated first packet", __func__);
2457	}
2458
2459	/*
2460	 * Append group record.
2461	 * If we have sources, we don't know how many yet.
2462	 */
2463	mr.mr_type = type;
2464	mr.mr_datalen = 0;
2465	mr.mr_numsrc = 0;
2466	mr.mr_addr = inm->in6m_addr;
2467	in6_clearscope(&mr.mr_addr);
2468	if (!m_append(m, sizeof(struct mldv2_record), (void *)&mr)) {
2469		if (m != m0)
2470			m_freem(m);
2471		CTR1(KTR_MLD, "%s: m_append() failed.", __func__);
2472		return (-ENOMEM);
2473	}
2474	nbytes += sizeof(struct mldv2_record);
2475
2476	/*
2477	 * Append as many sources as will fit in the first packet.
2478	 * If we are appending to a new packet, the chain allocation
2479	 * may potentially use clusters; use m_getptr() in this case.
2480	 * If we are appending to an existing packet, we need to obtain
2481	 * a pointer to the group record after m_append(), in case a new
2482	 * mbuf was allocated.
2483	 *
2484	 * Only append sources which are in-mode at t1. If we are
2485	 * transitioning to MCAST_UNDEFINED state on the group, and
2486	 * use_block_allow is zero, do not include source entries.
2487	 * Otherwise, we need to include this source in the report.
2488	 *
2489	 * Only report recorded sources in our filter set when responding
2490	 * to a group-source query.
2491	 */
2492	if (record_has_sources) {
2493		if (m == m0) {
2494			md = m_last(m);
2495			pmr = (struct mldv2_record *)(mtod(md, uint8_t *) +
2496			    md->m_len - nbytes);
2497		} else {
2498			md = m_getptr(m, 0, &off);
2499			pmr = (struct mldv2_record *)(mtod(md, uint8_t *) +
2500			    off);
2501		}
2502		msrcs = 0;
2503		RB_FOREACH_SAFE(ims, ip6_msource_tree, &inm->in6m_srcs,
2504		    nims) {
2505			CTR2(KTR_MLD, "%s: visit node %s", __func__,
2506			    ip6_sprintf(ip6tbuf, &ims->im6s_addr));
2507			now = im6s_get_mode(inm, ims, 1);
2508			CTR2(KTR_MLD, "%s: node is %d", __func__, now);
2509			if ((now != mode) ||
2510			    (now == mode &&
2511			     (!use_block_allow && mode == MCAST_UNDEFINED))) {
2512				CTR1(KTR_MLD, "%s: skip node", __func__);
2513				continue;
2514			}
2515			if (is_source_query && ims->im6s_stp == 0) {
2516				CTR1(KTR_MLD, "%s: skip unrecorded node",
2517				    __func__);
2518				continue;
2519			}
2520			CTR1(KTR_MLD, "%s: append node", __func__);
2521			if (!m_append(m, sizeof(struct in6_addr),
2522			    (void *)&ims->im6s_addr)) {
2523				if (m != m0)
2524					m_freem(m);
2525				CTR1(KTR_MLD, "%s: m_append() failed.",
2526				    __func__);
2527				return (-ENOMEM);
2528			}
2529			nbytes += sizeof(struct in6_addr);
2530			++msrcs;
2531			if (msrcs == m0srcs)
2532				break;
2533		}
2534		CTR2(KTR_MLD, "%s: msrcs is %d this packet", __func__,
2535		    msrcs);
2536		pmr->mr_numsrc = htons(msrcs);
2537		nbytes += (msrcs * sizeof(struct in6_addr));
2538	}
2539
2540	if (is_source_query && msrcs == 0) {
2541		CTR1(KTR_MLD, "%s: no recorded sources to report", __func__);
2542		if (m != m0)
2543			m_freem(m);
2544		return (0);
2545	}
2546
2547	/*
2548	 * We are good to go with first packet.
2549	 */
2550	if (m != m0) {
2551		CTR1(KTR_MLD, "%s: enqueueing first packet", __func__);
2552		m->m_pkthdr.PH_vt.vt_nrecs = 1;
2553		_IF_ENQUEUE(ifq, m);
2554	} else
2555		m->m_pkthdr.PH_vt.vt_nrecs++;
2556
2557	/*
2558	 * No further work needed if no source list in packet(s).
2559	 */
2560	if (!record_has_sources)
2561		return (nbytes);
2562
2563	/*
2564	 * Whilst sources remain to be announced, we need to allocate
2565	 * a new packet and fill out as many sources as will fit.
2566	 * Always try for a cluster first.
2567	 */
2568	while (nims != NULL) {
2569		if (_IF_QFULL(ifq)) {
2570			CTR1(KTR_MLD, "%s: outbound queue full", __func__);
2571			return (-ENOMEM);
2572		}
2573		m = m_getcl(M_DONTWAIT, MT_DATA, M_PKTHDR);
2574		if (m == NULL)
2575			m = m_gethdr(M_DONTWAIT, MT_DATA);
2576		if (m == NULL)
2577			return (-ENOMEM);
2578		mld_save_context(m, ifp);
2579		md = m_getptr(m, 0, &off);
2580		pmr = (struct mldv2_record *)(mtod(md, uint8_t *) + off);
2581		CTR1(KTR_MLD, "%s: allocated next packet", __func__);
2582
2583		if (!m_append(m, sizeof(struct mldv2_record), (void *)&mr)) {
2584			if (m != m0)
2585				m_freem(m);
2586			CTR1(KTR_MLD, "%s: m_append() failed.", __func__);
2587			return (-ENOMEM);
2588		}
2589		m->m_pkthdr.PH_vt.vt_nrecs = 1;
2590		nbytes += sizeof(struct mldv2_record);
2591
2592		m0srcs = (ifp->if_mtu - MLD_MTUSPACE -
2593		    sizeof(struct mldv2_record)) / sizeof(struct in6_addr);
2594
2595		msrcs = 0;
2596		RB_FOREACH_FROM(ims, ip6_msource_tree, nims) {
2597			CTR2(KTR_MLD, "%s: visit node %s",
2598			    __func__, ip6_sprintf(ip6tbuf, &ims->im6s_addr));
2599			now = im6s_get_mode(inm, ims, 1);
2600			if ((now != mode) ||
2601			    (now == mode &&
2602			     (!use_block_allow && mode == MCAST_UNDEFINED))) {
2603				CTR1(KTR_MLD, "%s: skip node", __func__);
2604				continue;
2605			}
2606			if (is_source_query && ims->im6s_stp == 0) {
2607				CTR1(KTR_MLD, "%s: skip unrecorded node",
2608				    __func__);
2609				continue;
2610			}
2611			CTR1(KTR_MLD, "%s: append node", __func__);
2612			if (!m_append(m, sizeof(struct in6_addr),
2613			    (void *)&ims->im6s_addr)) {
2614				if (m != m0)
2615					m_freem(m);
2616				CTR1(KTR_MLD, "%s: m_append() failed.",
2617				    __func__);
2618				return (-ENOMEM);
2619			}
2620			++msrcs;
2621			if (msrcs == m0srcs)
2622				break;
2623		}
2624		pmr->mr_numsrc = htons(msrcs);
2625		nbytes += (msrcs * sizeof(struct in6_addr));
2626
2627		CTR1(KTR_MLD, "%s: enqueueing next packet", __func__);
2628		_IF_ENQUEUE(ifq, m);
2629	}
2630
2631	return (nbytes);
2632}
2633
2634/*
2635 * Type used to mark record pass completion.
2636 * We exploit the fact we can cast to this easily from the
2637 * current filter modes on each ip_msource node.
2638 */
2639typedef enum {
2640	REC_NONE = 0x00,	/* MCAST_UNDEFINED */
2641	REC_ALLOW = 0x01,	/* MCAST_INCLUDE */
2642	REC_BLOCK = 0x02,	/* MCAST_EXCLUDE */
2643	REC_FULL = REC_ALLOW | REC_BLOCK
2644} rectype_t;
2645
2646/*
2647 * Enqueue an MLDv2 filter list change to the given output queue.
2648 *
2649 * Source list filter state is held in an RB-tree. When the filter list
2650 * for a group is changed without changing its mode, we need to compute
2651 * the deltas between T0 and T1 for each source in the filter set,
2652 * and enqueue the appropriate ALLOW_NEW/BLOCK_OLD records.
2653 *
2654 * As we may potentially queue two record types, and the entire R-B tree
2655 * needs to be walked at once, we break this out into its own function
2656 * so we can generate a tightly packed queue of packets.
2657 *
2658 * XXX This could be written to only use one tree walk, although that makes
2659 * serializing into the mbuf chains a bit harder. For now we do two walks
2660 * which makes things easier on us, and it may or may not be harder on
2661 * the L2 cache.
2662 *
2663 * If successful the size of all data appended to the queue is returned,
2664 * otherwise an error code less than zero is returned, or zero if
2665 * no record(s) were appended.
2666 */
2667static int
2668mld_v2_enqueue_filter_change(struct ifqueue *ifq, struct in6_multi *inm)
2669{
2670	static const int MINRECLEN =
2671	    sizeof(struct mldv2_record) + sizeof(struct in6_addr);
2672	struct ifnet		*ifp;
2673	struct mldv2_record	 mr;
2674	struct mldv2_record	*pmr;
2675	struct ip6_msource	*ims, *nims;
2676	struct mbuf		*m, *m0, *md;
2677	int			 m0srcs, nbytes, npbytes, off, rsrcs, schanged;
2678	int			 nallow, nblock;
2679	uint8_t			 mode, now, then;
2680	rectype_t		 crt, drt, nrt;
2681#ifdef KTR
2682	char			 ip6tbuf[INET6_ADDRSTRLEN];
2683#endif
2684
2685	IN6_MULTI_LOCK_ASSERT();
2686
2687	if (inm->in6m_nsrc == 0 ||
2688	    (inm->in6m_st[0].iss_asm > 0 && inm->in6m_st[1].iss_asm > 0))
2689		return (0);
2690
2691	ifp = inm->in6m_ifp;			/* interface */
2692	mode = inm->in6m_st[1].iss_fmode;	/* filter mode at t1 */
2693	crt = REC_NONE;	/* current group record type */
2694	drt = REC_NONE;	/* mask of completed group record types */
2695	nrt = REC_NONE;	/* record type for current node */
2696	m0srcs = 0;	/* # source which will fit in current mbuf chain */
2697	npbytes = 0;	/* # of bytes appended this packet */
2698	nbytes = 0;	/* # of bytes appended to group's state-change queue */
2699	rsrcs = 0;	/* # sources encoded in current record */
2700	schanged = 0;	/* # nodes encoded in overall filter change */
2701	nallow = 0;	/* # of source entries in ALLOW_NEW */
2702	nblock = 0;	/* # of source entries in BLOCK_OLD */
2703	nims = NULL;	/* next tree node pointer */
2704
2705	/*
2706	 * For each possible filter record mode.
2707	 * The first kind of source we encounter tells us which
2708	 * is the first kind of record we start appending.
2709	 * If a node transitioned to UNDEFINED at t1, its mode is treated
2710	 * as the inverse of the group's filter mode.
2711	 */
2712	while (drt != REC_FULL) {
2713		do {
2714			m0 = ifq->ifq_tail;
2715			if (m0 != NULL &&
2716			    (m0->m_pkthdr.PH_vt.vt_nrecs + 1 <=
2717			     MLD_V2_REPORT_MAXRECS) &&
2718			    (m0->m_pkthdr.len + MINRECLEN) <
2719			     (ifp->if_mtu - MLD_MTUSPACE)) {
2720				m = m0;
2721				m0srcs = (ifp->if_mtu - m0->m_pkthdr.len -
2722					    sizeof(struct mldv2_record)) /
2723					    sizeof(struct in6_addr);
2724				CTR1(KTR_MLD,
2725				    "%s: use previous packet", __func__);
2726			} else {
2727				m = m_getcl(M_DONTWAIT, MT_DATA, M_PKTHDR);
2728				if (m == NULL)
2729					m = m_gethdr(M_DONTWAIT, MT_DATA);
2730				if (m == NULL) {
2731					CTR1(KTR_MLD,
2732					    "%s: m_get*() failed", __func__);
2733					return (-ENOMEM);
2734				}
2735				m->m_pkthdr.PH_vt.vt_nrecs = 0;
2736				mld_save_context(m, ifp);
2737				m0srcs = (ifp->if_mtu - MLD_MTUSPACE -
2738				    sizeof(struct mldv2_record)) /
2739				    sizeof(struct in6_addr);
2740				npbytes = 0;
2741				CTR1(KTR_MLD,
2742				    "%s: allocated new packet", __func__);
2743			}
2744			/*
2745			 * Append the MLD group record header to the
2746			 * current packet's data area.
2747			 * Recalculate pointer to free space for next
2748			 * group record, in case m_append() allocated
2749			 * a new mbuf or cluster.
2750			 */
2751			memset(&mr, 0, sizeof(mr));
2752			mr.mr_addr = inm->in6m_addr;
2753			in6_clearscope(&mr.mr_addr);
2754			if (!m_append(m, sizeof(mr), (void *)&mr)) {
2755				if (m != m0)
2756					m_freem(m);
2757				CTR1(KTR_MLD,
2758				    "%s: m_append() failed", __func__);
2759				return (-ENOMEM);
2760			}
2761			npbytes += sizeof(struct mldv2_record);
2762			if (m != m0) {
2763				/* new packet; offset in chain */
2764				md = m_getptr(m, npbytes -
2765				    sizeof(struct mldv2_record), &off);
2766				pmr = (struct mldv2_record *)(mtod(md,
2767				    uint8_t *) + off);
2768			} else {
2769				/* current packet; offset from last append */
2770				md = m_last(m);
2771				pmr = (struct mldv2_record *)(mtod(md,
2772				    uint8_t *) + md->m_len -
2773				    sizeof(struct mldv2_record));
2774			}
2775			/*
2776			 * Begin walking the tree for this record type
2777			 * pass, or continue from where we left off
2778			 * previously if we had to allocate a new packet.
2779			 * Only report deltas in-mode at t1.
2780			 * We need not report included sources as allowed
2781			 * if we are in inclusive mode on the group,
2782			 * however the converse is not true.
2783			 */
2784			rsrcs = 0;
2785			if (nims == NULL) {
2786				nims = RB_MIN(ip6_msource_tree,
2787				    &inm->in6m_srcs);
2788			}
2789			RB_FOREACH_FROM(ims, ip6_msource_tree, nims) {
2790				CTR2(KTR_MLD, "%s: visit node %s", __func__,
2791				    ip6_sprintf(ip6tbuf, &ims->im6s_addr));
2792				now = im6s_get_mode(inm, ims, 1);
2793				then = im6s_get_mode(inm, ims, 0);
2794				CTR3(KTR_MLD, "%s: mode: t0 %d, t1 %d",
2795				    __func__, then, now);
2796				if (now == then) {
2797					CTR1(KTR_MLD,
2798					    "%s: skip unchanged", __func__);
2799					continue;
2800				}
2801				if (mode == MCAST_EXCLUDE &&
2802				    now == MCAST_INCLUDE) {
2803					CTR1(KTR_MLD,
2804					    "%s: skip IN src on EX group",
2805					    __func__);
2806					continue;
2807				}
2808				nrt = (rectype_t)now;
2809				if (nrt == REC_NONE)
2810					nrt = (rectype_t)(~mode & REC_FULL);
2811				if (schanged++ == 0) {
2812					crt = nrt;
2813				} else if (crt != nrt)
2814					continue;
2815				if (!m_append(m, sizeof(struct in6_addr),
2816				    (void *)&ims->im6s_addr)) {
2817					if (m != m0)
2818						m_freem(m);
2819					CTR1(KTR_MLD,
2820					    "%s: m_append() failed", __func__);
2821					return (-ENOMEM);
2822				}
2823				nallow += !!(crt == REC_ALLOW);
2824				nblock += !!(crt == REC_BLOCK);
2825				if (++rsrcs == m0srcs)
2826					break;
2827			}
2828			/*
2829			 * If we did not append any tree nodes on this
2830			 * pass, back out of allocations.
2831			 */
2832			if (rsrcs == 0) {
2833				npbytes -= sizeof(struct mldv2_record);
2834				if (m != m0) {
2835					CTR1(KTR_MLD,
2836					    "%s: m_free(m)", __func__);
2837					m_freem(m);
2838				} else {
2839					CTR1(KTR_MLD,
2840					    "%s: m_adj(m, -mr)", __func__);
2841					m_adj(m, -((int)sizeof(
2842					    struct mldv2_record)));
2843				}
2844				continue;
2845			}
2846			npbytes += (rsrcs * sizeof(struct in6_addr));
2847			if (crt == REC_ALLOW)
2848				pmr->mr_type = MLD_ALLOW_NEW_SOURCES;
2849			else if (crt == REC_BLOCK)
2850				pmr->mr_type = MLD_BLOCK_OLD_SOURCES;
2851			pmr->mr_numsrc = htons(rsrcs);
2852			/*
2853			 * Count the new group record, and enqueue this
2854			 * packet if it wasn't already queued.
2855			 */
2856			m->m_pkthdr.PH_vt.vt_nrecs++;
2857			if (m != m0)
2858				_IF_ENQUEUE(ifq, m);
2859			nbytes += npbytes;
2860		} while (nims != NULL);
2861		drt |= crt;
2862		crt = (~crt & REC_FULL);
2863	}
2864
2865	CTR3(KTR_MLD, "%s: queued %d ALLOW_NEW, %d BLOCK_OLD", __func__,
2866	    nallow, nblock);
2867
2868	return (nbytes);
2869}
2870
2871static int
2872mld_v2_merge_state_changes(struct in6_multi *inm, struct ifqueue *ifscq)
2873{
2874	struct ifqueue	*gq;
2875	struct mbuf	*m;		/* pending state-change */
2876	struct mbuf	*m0;		/* copy of pending state-change */
2877	struct mbuf	*mt;		/* last state-change in packet */
2878	int		 docopy, domerge;
2879	u_int		 recslen;
2880
2881	docopy = 0;
2882	domerge = 0;
2883	recslen = 0;
2884
2885	IN6_MULTI_LOCK_ASSERT();
2886	MLD_LOCK_ASSERT();
2887
2888	/*
2889	 * If there are further pending retransmissions, make a writable
2890	 * copy of each queued state-change message before merging.
2891	 */
2892	if (inm->in6m_scrv > 0)
2893		docopy = 1;
2894
2895	gq = &inm->in6m_scq;
2896#ifdef KTR
2897	if (gq->ifq_head == NULL) {
2898		CTR2(KTR_MLD, "%s: WARNING: queue for inm %p is empty",
2899		    __func__, inm);
2900	}
2901#endif
2902
2903	m = gq->ifq_head;
2904	while (m != NULL) {
2905		/*
2906		 * Only merge the report into the current packet if
2907		 * there is sufficient space to do so; an MLDv2 report
2908		 * packet may only contain 65,535 group records.
2909		 * Always use a simple mbuf chain concatentation to do this,
2910		 * as large state changes for single groups may have
2911		 * allocated clusters.
2912		 */
2913		domerge = 0;
2914		mt = ifscq->ifq_tail;
2915		if (mt != NULL) {
2916			recslen = m_length(m, NULL);
2917
2918			if ((mt->m_pkthdr.PH_vt.vt_nrecs +
2919			    m->m_pkthdr.PH_vt.vt_nrecs <=
2920			    MLD_V2_REPORT_MAXRECS) &&
2921			    (mt->m_pkthdr.len + recslen <=
2922			    (inm->in6m_ifp->if_mtu - MLD_MTUSPACE)))
2923				domerge = 1;
2924		}
2925
2926		if (!domerge && _IF_QFULL(gq)) {
2927			CTR2(KTR_MLD,
2928			    "%s: outbound queue full, skipping whole packet %p",
2929			    __func__, m);
2930			mt = m->m_nextpkt;
2931			if (!docopy)
2932				m_freem(m);
2933			m = mt;
2934			continue;
2935		}
2936
2937		if (!docopy) {
2938			CTR2(KTR_MLD, "%s: dequeueing %p", __func__, m);
2939			_IF_DEQUEUE(gq, m0);
2940			m = m0->m_nextpkt;
2941		} else {
2942			CTR2(KTR_MLD, "%s: copying %p", __func__, m);
2943			m0 = m_dup(m, M_NOWAIT);
2944			if (m0 == NULL)
2945				return (ENOMEM);
2946			m0->m_nextpkt = NULL;
2947			m = m->m_nextpkt;
2948		}
2949
2950		if (!domerge) {
2951			CTR3(KTR_MLD, "%s: queueing %p to ifscq %p)",
2952			    __func__, m0, ifscq);
2953			_IF_ENQUEUE(ifscq, m0);
2954		} else {
2955			struct mbuf *mtl;	/* last mbuf of packet mt */
2956
2957			CTR3(KTR_MLD, "%s: merging %p with ifscq tail %p)",
2958			    __func__, m0, mt);
2959
2960			mtl = m_last(mt);
2961			m0->m_flags &= ~M_PKTHDR;
2962			mt->m_pkthdr.len += recslen;
2963			mt->m_pkthdr.PH_vt.vt_nrecs +=
2964			    m0->m_pkthdr.PH_vt.vt_nrecs;
2965
2966			mtl->m_next = m0;
2967		}
2968	}
2969
2970	return (0);
2971}
2972
2973/*
2974 * Respond to a pending MLDv2 General Query.
2975 */
2976static void
2977mld_v2_dispatch_general_query(struct mld_ifinfo *mli)
2978{
2979	struct ifmultiaddr	*ifma;
2980	struct ifnet		*ifp;
2981	struct in6_multi	*inm;
2982	int			 retval;
2983
2984	IN6_MULTI_LOCK_ASSERT();
2985	MLD_LOCK_ASSERT();
2986
2987	KASSERT(mli->mli_version == MLD_VERSION_2,
2988	    ("%s: called when version %d", __func__, mli->mli_version));
2989
2990	ifp = mli->mli_ifp;
2991
2992	IF_ADDR_RLOCK(ifp);
2993	TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
2994		if (ifma->ifma_addr->sa_family != AF_INET6 ||
2995		    ifma->ifma_protospec == NULL)
2996			continue;
2997
2998		inm = (struct in6_multi *)ifma->ifma_protospec;
2999		KASSERT(ifp == inm->in6m_ifp,
3000		    ("%s: inconsistent ifp", __func__));
3001
3002		switch (inm->in6m_state) {
3003		case MLD_NOT_MEMBER:
3004		case MLD_SILENT_MEMBER:
3005			break;
3006		case MLD_REPORTING_MEMBER:
3007		case MLD_IDLE_MEMBER:
3008		case MLD_LAZY_MEMBER:
3009		case MLD_SLEEPING_MEMBER:
3010		case MLD_AWAKENING_MEMBER:
3011			inm->in6m_state = MLD_REPORTING_MEMBER;
3012			retval = mld_v2_enqueue_group_record(&mli->mli_gq,
3013			    inm, 0, 0, 0, 0);
3014			CTR2(KTR_MLD, "%s: enqueue record = %d",
3015			    __func__, retval);
3016			break;
3017		case MLD_G_QUERY_PENDING_MEMBER:
3018		case MLD_SG_QUERY_PENDING_MEMBER:
3019		case MLD_LEAVING_MEMBER:
3020			break;
3021		}
3022	}
3023	IF_ADDR_RUNLOCK(ifp);
3024
3025	mld_dispatch_queue(&mli->mli_gq, MLD_MAX_RESPONSE_BURST);
3026
3027	/*
3028	 * Slew transmission of bursts over 500ms intervals.
3029	 */
3030	if (mli->mli_gq.ifq_head != NULL) {
3031		mli->mli_v2_timer = 1 + MLD_RANDOM_DELAY(
3032		    MLD_RESPONSE_BURST_INTERVAL);
3033		V_interface_timers_running6 = 1;
3034	}
3035}
3036
3037/*
3038 * Transmit the next pending message in the output queue.
3039 *
3040 * VIMAGE: Needs to store/restore vnet pointer on a per-mbuf-chain basis.
3041 * MRT: Nothing needs to be done, as MLD traffic is always local to
3042 * a link and uses a link-scope multicast address.
3043 */
3044static void
3045mld_dispatch_packet(struct mbuf *m)
3046{
3047	struct ip6_moptions	 im6o;
3048	struct ifnet		*ifp;
3049	struct ifnet		*oifp;
3050	struct mbuf		*m0;
3051	struct mbuf		*md;
3052	struct ip6_hdr		*ip6;
3053	struct mld_hdr		*mld;
3054	int			 error;
3055	int			 off;
3056	int			 type;
3057	uint32_t		 ifindex;
3058
3059	CTR2(KTR_MLD, "%s: transmit %p", __func__, m);
3060
3061	/*
3062	 * Set VNET image pointer from enqueued mbuf chain
3063	 * before doing anything else. Whilst we use interface
3064	 * indexes to guard against interface detach, they are
3065	 * unique to each VIMAGE and must be retrieved.
3066	 */
3067	ifindex = mld_restore_context(m);
3068
3069	/*
3070	 * Check if the ifnet still exists. This limits the scope of
3071	 * any race in the absence of a global ifp lock for low cost
3072	 * (an array lookup).
3073	 */
3074	ifp = ifnet_byindex(ifindex);
3075	if (ifp == NULL) {
3076		CTR3(KTR_MLD, "%s: dropped %p as ifindex %u went away.",
3077		    __func__, m, ifindex);
3078		m_freem(m);
3079		IP6STAT_INC(ip6s_noroute);
3080		goto out;
3081	}
3082
3083	im6o.im6o_multicast_hlim  = 1;
3084	im6o.im6o_multicast_loop = (V_ip6_mrouter != NULL);
3085	im6o.im6o_multicast_ifp = ifp;
3086
3087	if (m->m_flags & M_MLDV1) {
3088		m0 = m;
3089	} else {
3090		m0 = mld_v2_encap_report(ifp, m);
3091		if (m0 == NULL) {
3092			CTR2(KTR_MLD, "%s: dropped %p", __func__, m);
3093			IP6STAT_INC(ip6s_odropped);
3094			goto out;
3095		}
3096	}
3097
3098	mld_scrub_context(m0);
3099	m->m_flags &= ~(M_PROTOFLAGS);
3100	m0->m_pkthdr.rcvif = V_loif;
3101
3102	ip6 = mtod(m0, struct ip6_hdr *);
3103#if 0
3104	(void)in6_setscope(&ip6->ip6_dst, ifp, NULL);	/* XXX LOR */
3105#else
3106	/*
3107	 * XXX XXX Break some KPI rules to prevent an LOR which would
3108	 * occur if we called in6_setscope() at transmission.
3109	 * See comments at top of file.
3110	 */
3111	MLD_EMBEDSCOPE(&ip6->ip6_dst, ifp->if_index);
3112#endif
3113
3114	/*
3115	 * Retrieve the ICMPv6 type before handoff to ip6_output(),
3116	 * so we can bump the stats.
3117	 */
3118	md = m_getptr(m0, sizeof(struct ip6_hdr), &off);
3119	mld = (struct mld_hdr *)(mtod(md, uint8_t *) + off);
3120	type = mld->mld_type;
3121
3122	error = ip6_output(m0, &mld_po, NULL, IPV6_UNSPECSRC, &im6o,
3123	    &oifp, NULL);
3124	if (error) {
3125		CTR3(KTR_MLD, "%s: ip6_output(%p) = %d", __func__, m0, error);
3126		goto out;
3127	}
3128	ICMP6STAT_INC(icp6s_outhist[type]);
3129	if (oifp != NULL) {
3130		icmp6_ifstat_inc(oifp, ifs6_out_msg);
3131		switch (type) {
3132		case MLD_LISTENER_REPORT:
3133		case MLDV2_LISTENER_REPORT:
3134			icmp6_ifstat_inc(oifp, ifs6_out_mldreport);
3135			break;
3136		case MLD_LISTENER_DONE:
3137			icmp6_ifstat_inc(oifp, ifs6_out_mlddone);
3138			break;
3139		}
3140	}
3141out:
3142	return;
3143}
3144
3145/*
3146 * Encapsulate an MLDv2 report.
3147 *
3148 * KAME IPv6 requires that hop-by-hop options be passed separately,
3149 * and that the IPv6 header be prepended in a separate mbuf.
3150 *
3151 * Returns a pointer to the new mbuf chain head, or NULL if the
3152 * allocation failed.
3153 */
3154static struct mbuf *
3155mld_v2_encap_report(struct ifnet *ifp, struct mbuf *m)
3156{
3157	struct mbuf		*mh;
3158	struct mldv2_report	*mld;
3159	struct ip6_hdr		*ip6;
3160	struct in6_ifaddr	*ia;
3161	int			 mldreclen;
3162
3163	KASSERT(ifp != NULL, ("%s: null ifp", __func__));
3164	KASSERT((m->m_flags & M_PKTHDR),
3165	    ("%s: mbuf chain %p is !M_PKTHDR", __func__, m));
3166
3167	/*
3168	 * RFC3590: OK to send as :: or tentative during DAD.
3169	 */
3170	ia = in6ifa_ifpforlinklocal(ifp, IN6_IFF_NOTREADY|IN6_IFF_ANYCAST);
3171	if (ia == NULL)
3172		CTR1(KTR_MLD, "%s: warning: ia is NULL", __func__);
3173
3174	MGETHDR(mh, M_DONTWAIT, MT_HEADER);
3175	if (mh == NULL) {
3176		if (ia != NULL)
3177			ifa_free(&ia->ia_ifa);
3178		m_freem(m);
3179		return (NULL);
3180	}
3181	MH_ALIGN(mh, sizeof(struct ip6_hdr) + sizeof(struct mldv2_report));
3182
3183	mldreclen = m_length(m, NULL);
3184	CTR2(KTR_MLD, "%s: mldreclen is %d", __func__, mldreclen);
3185
3186	mh->m_len = sizeof(struct ip6_hdr) + sizeof(struct mldv2_report);
3187	mh->m_pkthdr.len = sizeof(struct ip6_hdr) +
3188	    sizeof(struct mldv2_report) + mldreclen;
3189
3190	ip6 = mtod(mh, struct ip6_hdr *);
3191	ip6->ip6_flow = 0;
3192	ip6->ip6_vfc &= ~IPV6_VERSION_MASK;
3193	ip6->ip6_vfc |= IPV6_VERSION;
3194	ip6->ip6_nxt = IPPROTO_ICMPV6;
3195	ip6->ip6_src = ia ? ia->ia_addr.sin6_addr : in6addr_any;
3196	if (ia != NULL)
3197		ifa_free(&ia->ia_ifa);
3198	ip6->ip6_dst = in6addr_linklocal_allv2routers;
3199	/* scope ID will be set in netisr */
3200
3201	mld = (struct mldv2_report *)(ip6 + 1);
3202	mld->mld_type = MLDV2_LISTENER_REPORT;
3203	mld->mld_code = 0;
3204	mld->mld_cksum = 0;
3205	mld->mld_v2_reserved = 0;
3206	mld->mld_v2_numrecs = htons(m->m_pkthdr.PH_vt.vt_nrecs);
3207	m->m_pkthdr.PH_vt.vt_nrecs = 0;
3208
3209	mh->m_next = m;
3210	mld->mld_cksum = in6_cksum(mh, IPPROTO_ICMPV6,
3211	    sizeof(struct ip6_hdr), sizeof(struct mldv2_report) + mldreclen);
3212	return (mh);
3213}
3214
3215#ifdef KTR
3216static char *
3217mld_rec_type_to_str(const int type)
3218{
3219
3220	switch (type) {
3221		case MLD_CHANGE_TO_EXCLUDE_MODE:
3222			return "TO_EX";
3223			break;
3224		case MLD_CHANGE_TO_INCLUDE_MODE:
3225			return "TO_IN";
3226			break;
3227		case MLD_MODE_IS_EXCLUDE:
3228			return "MODE_EX";
3229			break;
3230		case MLD_MODE_IS_INCLUDE:
3231			return "MODE_IN";
3232			break;
3233		case MLD_ALLOW_NEW_SOURCES:
3234			return "ALLOW_NEW";
3235			break;
3236		case MLD_BLOCK_OLD_SOURCES:
3237			return "BLOCK_OLD";
3238			break;
3239		default:
3240			break;
3241	}
3242	return "unknown";
3243}
3244#endif
3245
3246static void
3247mld_init(void *unused __unused)
3248{
3249
3250	CTR1(KTR_MLD, "%s: initializing", __func__);
3251	MLD_LOCK_INIT();
3252
3253	ip6_initpktopts(&mld_po);
3254	mld_po.ip6po_hlim = 1;
3255	mld_po.ip6po_hbh = &mld_ra.hbh;
3256	mld_po.ip6po_prefer_tempaddr = IP6PO_TEMPADDR_NOTPREFER;
3257	mld_po.ip6po_flags = IP6PO_DONTFRAG;
3258}
3259SYSINIT(mld_init, SI_SUB_PSEUDO, SI_ORDER_MIDDLE, mld_init, NULL);
3260
3261static void
3262mld_uninit(void *unused __unused)
3263{
3264
3265	CTR1(KTR_MLD, "%s: tearing down", __func__);
3266	MLD_LOCK_DESTROY();
3267}
3268SYSUNINIT(mld_uninit, SI_SUB_PSEUDO, SI_ORDER_MIDDLE, mld_uninit, NULL);
3269
3270static void
3271vnet_mld_init(const void *unused __unused)
3272{
3273
3274	CTR1(KTR_MLD, "%s: initializing", __func__);
3275
3276	LIST_INIT(&V_mli_head);
3277}
3278VNET_SYSINIT(vnet_mld_init, SI_SUB_PSEUDO, SI_ORDER_ANY, vnet_mld_init,
3279    NULL);
3280
3281static void
3282vnet_mld_uninit(const void *unused __unused)
3283{
3284
3285	CTR1(KTR_MLD, "%s: tearing down", __func__);
3286
3287	KASSERT(LIST_EMPTY(&V_mli_head),
3288	    ("%s: mli list not empty; ifnets not detached?", __func__));
3289}
3290VNET_SYSUNINIT(vnet_mld_uninit, SI_SUB_PSEUDO, SI_ORDER_ANY, vnet_mld_uninit,
3291    NULL);
3292
3293static int
3294mld_modevent(module_t mod, int type, void *unused __unused)
3295{
3296
3297    switch (type) {
3298    case MOD_LOAD:
3299    case MOD_UNLOAD:
3300	break;
3301    default:
3302	return (EOPNOTSUPP);
3303    }
3304    return (0);
3305}
3306
3307static moduledata_t mld_mod = {
3308    "mld",
3309    mld_modevent,
3310    0
3311};
3312DECLARE_MODULE(mld, mld_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
3313