2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 2009 Bruce Simpson.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. The name of the author may not be used to endorse or promote
15 * products derived from this software without specific prior written
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * $KAME: mld6.c,v 1.27 2001/04/04 05:17:30 itojun Exp $
34 * Copyright (c) 1988 Stephen Deering.
35 * Copyright (c) 1992, 1993
36 * The Regents of the University of California. All rights reserved.
38 * This code is derived from software contributed to Berkeley by
39 * Stephen Deering of Stanford University.
41 * Redistribution and use in source and binary forms, with or without
42 * modification, are permitted provided that the following conditions
44 * 1. Redistributions of source code must retain the above copyright
45 * notice, this list of conditions and the following disclaimer.
46 * 2. Redistributions in binary form must reproduce the above copyright
47 * notice, this list of conditions and the following disclaimer in the
48 * documentation and/or other materials provided with the distribution.
49 * 3. Neither the name of the University nor the names of its contributors
50 * may be used to endorse or promote products derived from this software
51 * without specific prior written permission.
53 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
54 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
55 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
56 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
57 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
58 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
59 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
60 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
61 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
62 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
65 * @(#)igmp.c 8.1 (Berkeley) 7/19/93
68 #include <sys/cdefs.h>
69 __FBSDID("$FreeBSD$");
72 #include "opt_inet6.h"
74 #include <sys/param.h>
75 #include <sys/systm.h>
77 #include <sys/socket.h>
78 #include <sys/protosw.h>
79 #include <sys/sysctl.h>
80 #include <sys/kernel.h>
81 #include <sys/callout.h>
82 #include <sys/malloc.h>
83 #include <sys/module.h>
87 #include <net/if_var.h>
88 #include <net/route.h>
91 #include <netinet/in.h>
92 #include <netinet/in_var.h>
93 #include <netinet6/in6_var.h>
94 #include <netinet/ip6.h>
95 #include <netinet6/ip6_var.h>
96 #include <netinet6/scope6_var.h>
97 #include <netinet/icmp6.h>
98 #include <netinet6/mld6.h>
99 #include <netinet6/mld6_var.h>
101 #include <security/mac/mac_framework.h>
104 #define KTR_MLD KTR_INET6
107 static struct mld_ifsoftc *
108 mli_alloc_locked(struct ifnet *);
109 static void mli_delete_locked(const struct ifnet *);
110 static void mld_dispatch_packet(struct mbuf *);
111 static void mld_dispatch_queue(struct mbufq *, int);
112 static void mld_final_leave(struct in6_multi *, struct mld_ifsoftc *);
113 static void mld_fasttimo_vnet(void);
114 static int mld_handle_state_change(struct in6_multi *,
115 struct mld_ifsoftc *);
116 static int mld_initial_join(struct in6_multi *, struct mld_ifsoftc *,
119 static char * mld_rec_type_to_str(const int);
121 static void mld_set_version(struct mld_ifsoftc *, const int);
122 static void mld_slowtimo_vnet(void);
123 static int mld_v1_input_query(struct ifnet *, const struct ip6_hdr *,
124 /*const*/ struct mld_hdr *);
125 static int mld_v1_input_report(struct ifnet *, const struct ip6_hdr *,
126 /*const*/ struct mld_hdr *);
127 static void mld_v1_process_group_timer(struct mld_ifsoftc *,
129 static void mld_v1_process_querier_timers(struct mld_ifsoftc *);
130 static int mld_v1_transmit_report(struct in6_multi *, const int);
131 static void mld_v1_update_group(struct in6_multi *, const int);
132 static void mld_v2_cancel_link_timers(struct mld_ifsoftc *);
133 static void mld_v2_dispatch_general_query(struct mld_ifsoftc *);
135 mld_v2_encap_report(struct ifnet *, struct mbuf *);
136 static int mld_v2_enqueue_filter_change(struct mbufq *,
138 static int mld_v2_enqueue_group_record(struct mbufq *,
139 struct in6_multi *, const int, const int, const int,
141 static int mld_v2_input_query(struct ifnet *, const struct ip6_hdr *,
142 struct mbuf *, const int, const int);
143 static int mld_v2_merge_state_changes(struct in6_multi *,
145 static void mld_v2_process_group_timers(struct mld_ifsoftc *,
146 struct mbufq *, struct mbufq *,
147 struct in6_multi *, const int);
148 static int mld_v2_process_group_query(struct in6_multi *,
149 struct mld_ifsoftc *mli, int, struct mbuf *, const int);
150 static int sysctl_mld_gsr(SYSCTL_HANDLER_ARGS);
151 static int sysctl_mld_ifinfo(SYSCTL_HANDLER_ARGS);
154 * Normative references: RFC 2710, RFC 3590, RFC 3810.
157 * * The MLD subsystem lock ends up being system-wide for the moment,
158 * but could be per-VIMAGE later on.
159 * * The permitted lock order is: IN6_MULTI_LOCK, MLD_LOCK, IF_ADDR_LOCK.
160 * Any may be taken independently; if any are held at the same
161 * time, the above lock order must be followed.
162 * * IN6_MULTI_LOCK covers in_multi.
163 * * MLD_LOCK covers per-link state and any global variables in this file.
164 * * IF_ADDR_LOCK covers if_multiaddrs, which is used for a variety of
165 * per-link state iterators.
168 * A special case for IPv6 is the in6_setscope() routine. ip6_output()
169 * will not accept an ifp; it wants an embedded scope ID, unlike
170 * ip_output(), which happily takes the ifp given to it. The embedded
171 * scope ID is only used by MLD to select the outgoing interface.
173 * During interface attach and detach, MLD will take MLD_LOCK *after*
174 * the IF_AFDATA_LOCK.
175 * As in6_setscope() takes IF_AFDATA_LOCK then SCOPE_LOCK, we can't call
176 * it with MLD_LOCK held without triggering an LOR. A netisr with indirect
177 * dispatch could work around this, but we'd rather not do that, as it
178 * can introduce other races.
180 * As such, we exploit the fact that the scope ID is just the interface
181 * index, and embed it in the IPv6 destination address accordingly.
182 * This is potentially NOT VALID for MLDv1 reports, as they
183 * are always sent to the multicast group itself; as MLDv2
184 * reports are always sent to ff02::16, this is not an issue
185 * when MLDv2 is in use.
187 * This does not however eliminate the LOR when ip6_output() itself
188 * calls in6_setscope() internally whilst MLD_LOCK is held. This will
189 * trigger a LOR warning in WITNESS when the ifnet is detached.
191 * The right answer is probably to make IF_AFDATA_LOCK an rwlock, given
192 * how it's used across the network stack. Here we're simply exploiting
193 * the fact that MLD runs at a similar layer in the stack to scope6.c.
196 * * Each in6_multi corresponds to an ifp, and each ifp corresponds
197 * to a vnet in ifp->if_vnet.
199 static struct mtx mld_mtx;
200 static MALLOC_DEFINE(M_MLD, "mld", "mld state");
202 #define MLD_EMBEDSCOPE(pin6, zoneid) \
203 if (IN6_IS_SCOPE_LINKLOCAL(pin6) || \
204 IN6_IS_ADDR_MC_INTFACELOCAL(pin6)) \
205 (pin6)->s6_addr16[1] = htons((zoneid) & 0xFFFF) \
208 * VIMAGE-wide globals.
210 static VNET_DEFINE(struct timeval, mld_gsrdelay) = {10, 0};
211 static VNET_DEFINE(LIST_HEAD(, mld_ifsoftc), mli_head);
212 static VNET_DEFINE(int, interface_timers_running6);
213 static VNET_DEFINE(int, state_change_timers_running6);
214 static VNET_DEFINE(int, current_state_timers_running6);
216 #define V_mld_gsrdelay VNET(mld_gsrdelay)
217 #define V_mli_head VNET(mli_head)
218 #define V_interface_timers_running6 VNET(interface_timers_running6)
219 #define V_state_change_timers_running6 VNET(state_change_timers_running6)
220 #define V_current_state_timers_running6 VNET(current_state_timers_running6)
222 SYSCTL_DECL(_net_inet6); /* Note: Not in any common header. */
224 SYSCTL_NODE(_net_inet6, OID_AUTO, mld, CTLFLAG_RW, 0,
225 "IPv6 Multicast Listener Discovery");
228 * Virtualized sysctls.
230 SYSCTL_PROC(_net_inet6_mld, OID_AUTO, gsrdelay,
231 CTLFLAG_VNET | CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
232 &VNET_NAME(mld_gsrdelay.tv_sec), 0, sysctl_mld_gsr, "I",
233 "Rate limit for MLDv2 Group-and-Source queries in seconds");
236 * Non-virtualized sysctls.
238 static SYSCTL_NODE(_net_inet6_mld, OID_AUTO, ifinfo,
239 CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_mld_ifinfo,
240 "Per-interface MLDv2 state");
242 static int mld_v1enable = 1;
243 SYSCTL_INT(_net_inet6_mld, OID_AUTO, v1enable, CTLFLAG_RWTUN,
244 &mld_v1enable, 0, "Enable fallback to MLDv1");
246 static int mld_use_allow = 1;
247 SYSCTL_INT(_net_inet6_mld, OID_AUTO, use_allow, CTLFLAG_RWTUN,
248 &mld_use_allow, 0, "Use ALLOW/BLOCK for RFC 4604 SSM joins/leaves");
251 * Packed Router Alert option structure declaration.
256 struct ip6_opt_router ra;
260 * Router Alert hop-by-hop option header.
262 static struct mld_raopt mld_ra = {
264 .pad = { .ip6o_type = IP6OPT_PADN, 0 },
266 .ip6or_type = IP6OPT_ROUTER_ALERT,
267 .ip6or_len = IP6OPT_RTALERT_LEN - 2,
268 .ip6or_value[0] = ((IP6OPT_RTALERT_MLD >> 8) & 0xFF),
269 .ip6or_value[1] = (IP6OPT_RTALERT_MLD & 0xFF)
272 static struct ip6_pktopts mld_po;
275 mld_save_context(struct mbuf *m, struct ifnet *ifp)
279 m->m_pkthdr.PH_loc.ptr = ifp->if_vnet;
281 m->m_pkthdr.flowid = ifp->if_index;
285 mld_scrub_context(struct mbuf *m)
288 m->m_pkthdr.PH_loc.ptr = NULL;
289 m->m_pkthdr.flowid = 0;
293 * Restore context from a queued output chain.
294 * Return saved ifindex.
296 * VIMAGE: The assertion is there to make sure that we
297 * actually called CURVNET_SET() with what's in the mbuf chain.
299 static __inline uint32_t
300 mld_restore_context(struct mbuf *m)
303 #if defined(VIMAGE) && defined(INVARIANTS)
304 KASSERT(curvnet == m->m_pkthdr.PH_loc.ptr,
305 ("%s: called when curvnet was not restored: cuvnet %p m ptr %p",
306 __func__, curvnet, m->m_pkthdr.PH_loc.ptr));
308 return (m->m_pkthdr.flowid);
312 * Retrieve or set threshold between group-source queries in seconds.
314 * VIMAGE: Assume curvnet set by caller.
315 * SMPng: NOTE: Serialized by MLD lock.
318 sysctl_mld_gsr(SYSCTL_HANDLER_ARGS)
323 error = sysctl_wire_old_buffer(req, sizeof(int));
329 i = V_mld_gsrdelay.tv_sec;
331 error = sysctl_handle_int(oidp, &i, 0, req);
332 if (error || !req->newptr)
335 if (i < -1 || i >= 60) {
340 CTR2(KTR_MLD, "change mld_gsrdelay from %d to %d",
341 V_mld_gsrdelay.tv_sec, i);
342 V_mld_gsrdelay.tv_sec = i;
350 * Expose struct mld_ifsoftc to userland, keyed by ifindex.
351 * For use by ifmcstat(8).
353 * SMPng: NOTE: Does an unlocked ifindex space read.
354 * VIMAGE: Assume curvnet set by caller. The node handler itself
355 * is not directly virtualized.
358 sysctl_mld_ifinfo(SYSCTL_HANDLER_ARGS)
364 struct mld_ifsoftc *mli;
369 if (req->newptr != NULL)
375 error = sysctl_wire_old_buffer(req, sizeof(struct mld_ifinfo));
382 if (name[0] <= 0 || name[0] > V_if_index) {
389 ifp = ifnet_byindex(name[0]);
393 LIST_FOREACH(mli, &V_mli_head, mli_link) {
394 if (ifp == mli->mli_ifp) {
395 struct mld_ifinfo info;
397 info.mli_version = mli->mli_version;
398 info.mli_v1_timer = mli->mli_v1_timer;
399 info.mli_v2_timer = mli->mli_v2_timer;
400 info.mli_flags = mli->mli_flags;
401 info.mli_rv = mli->mli_rv;
402 info.mli_qi = mli->mli_qi;
403 info.mli_qri = mli->mli_qri;
404 info.mli_uri = mli->mli_uri;
405 error = SYSCTL_OUT(req, &info, sizeof(info));
417 * Dispatch an entire queue of pending packet chains.
418 * VIMAGE: Assumes the vnet pointer has been set.
421 mld_dispatch_queue(struct mbufq *mq, int limit)
425 while ((m = mbufq_dequeue(mq)) != NULL) {
426 CTR3(KTR_MLD, "%s: dispatch %p from %p", __func__, mq, m);
427 mld_dispatch_packet(m);
434 * Filter outgoing MLD report state by group.
436 * Reports are ALWAYS suppressed for ALL-HOSTS (ff02::1)
437 * and node-local addresses. However, kernel and socket consumers
438 * always embed the KAME scope ID in the address provided, so strip it
439 * when performing comparison.
440 * Note: This is not the same as the *multicast* scope.
442 * Return zero if the given group is one for which MLD reports
443 * should be suppressed, or non-zero if reports should be issued.
446 mld_is_addr_reported(const struct in6_addr *addr)
449 KASSERT(IN6_IS_ADDR_MULTICAST(addr), ("%s: not multicast", __func__));
451 if (IPV6_ADDR_MC_SCOPE(addr) == IPV6_ADDR_SCOPE_NODELOCAL)
454 if (IPV6_ADDR_MC_SCOPE(addr) == IPV6_ADDR_SCOPE_LINKLOCAL) {
455 struct in6_addr tmp = *addr;
456 in6_clearscope(&tmp);
457 if (IN6_ARE_ADDR_EQUAL(&tmp, &in6addr_linklocal_allnodes))
465 * Attach MLD when PF_INET6 is attached to an interface.
467 * SMPng: Normally called with IF_AFDATA_LOCK held.
470 mld_domifattach(struct ifnet *ifp)
472 struct mld_ifsoftc *mli;
474 CTR3(KTR_MLD, "%s: called for ifp %p(%s)",
475 __func__, ifp, if_name(ifp));
479 mli = mli_alloc_locked(ifp);
480 if (!(ifp->if_flags & IFF_MULTICAST))
481 mli->mli_flags |= MLIF_SILENT;
483 mli->mli_flags |= MLIF_USEALLOW;
491 * VIMAGE: assume curvnet set by caller.
493 static struct mld_ifsoftc *
494 mli_alloc_locked(/*const*/ struct ifnet *ifp)
496 struct mld_ifsoftc *mli;
500 mli = malloc(sizeof(struct mld_ifsoftc), M_MLD, M_NOWAIT|M_ZERO);
505 mli->mli_version = MLD_VERSION_2;
507 mli->mli_rv = MLD_RV_INIT;
508 mli->mli_qi = MLD_QI_INIT;
509 mli->mli_qri = MLD_QRI_INIT;
510 mli->mli_uri = MLD_URI_INIT;
511 SLIST_INIT(&mli->mli_relinmhead);
512 mbufq_init(&mli->mli_gq, MLD_MAX_RESPONSE_PACKETS);
514 LIST_INSERT_HEAD(&V_mli_head, mli, mli_link);
516 CTR2(KTR_MLD, "allocate mld_ifsoftc for ifp %p(%s)",
526 * NOTE: Some finalization tasks need to run before the protocol domain
527 * is detached, but also before the link layer does its cleanup.
528 * Run before link-layer cleanup; cleanup groups, but do not free MLD state.
530 * SMPng: Caller must hold IN6_MULTI_LOCK().
531 * Must take IF_ADDR_LOCK() to cover if_multiaddrs iterator.
532 * XXX This routine is also bitten by unlocked ifma_protospec access.
535 mld_ifdetach(struct ifnet *ifp)
537 struct mld_ifsoftc *mli;
538 struct ifmultiaddr *ifma;
539 struct in6_multi *inm, *tinm;
541 CTR3(KTR_MLD, "%s: called for ifp %p(%s)", __func__, ifp,
544 IN6_MULTI_LOCK_ASSERT();
547 mli = MLD_IFINFO(ifp);
548 if (mli->mli_version == MLD_VERSION_2) {
550 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
551 if (ifma->ifma_addr->sa_family != AF_INET6 ||
552 ifma->ifma_protospec == NULL)
554 inm = (struct in6_multi *)ifma->ifma_protospec;
555 if (inm->in6m_state == MLD_LEAVING_MEMBER) {
556 SLIST_INSERT_HEAD(&mli->mli_relinmhead,
559 in6m_clear_recorded(inm);
561 IF_ADDR_RUNLOCK(ifp);
562 SLIST_FOREACH_SAFE(inm, &mli->mli_relinmhead, in6m_nrele,
564 SLIST_REMOVE_HEAD(&mli->mli_relinmhead, in6m_nrele);
565 in6m_release_locked(inm);
573 * Hook for domifdetach.
574 * Runs after link-layer cleanup; free MLD state.
576 * SMPng: Normally called with IF_AFDATA_LOCK held.
579 mld_domifdetach(struct ifnet *ifp)
582 CTR3(KTR_MLD, "%s: called for ifp %p(%s)",
583 __func__, ifp, if_name(ifp));
586 mli_delete_locked(ifp);
591 mli_delete_locked(const struct ifnet *ifp)
593 struct mld_ifsoftc *mli, *tmli;
595 CTR3(KTR_MLD, "%s: freeing mld_ifsoftc for ifp %p(%s)",
596 __func__, ifp, if_name(ifp));
600 LIST_FOREACH_SAFE(mli, &V_mli_head, mli_link, tmli) {
601 if (mli->mli_ifp == ifp) {
603 * Free deferred General Query responses.
605 mbufq_drain(&mli->mli_gq);
607 LIST_REMOVE(mli, mli_link);
609 KASSERT(SLIST_EMPTY(&mli->mli_relinmhead),
610 ("%s: there are dangling in_multi references",
620 * Process a received MLDv1 general or address-specific query.
621 * Assumes that the query header has been pulled up to sizeof(mld_hdr).
623 * NOTE: Can't be fully const correct as we temporarily embed scope ID in
624 * mld_addr. This is OK as we own the mbuf chain.
627 mld_v1_input_query(struct ifnet *ifp, const struct ip6_hdr *ip6,
628 /*const*/ struct mld_hdr *mld)
630 struct ifmultiaddr *ifma;
631 struct mld_ifsoftc *mli;
632 struct in6_multi *inm;
633 int is_general_query;
636 char ip6tbuf[INET6_ADDRSTRLEN];
639 is_general_query = 0;
642 CTR3(KTR_MLD, "ignore v1 query %s on ifp %p(%s)",
643 ip6_sprintf(ip6tbuf, &mld->mld_addr),
649 * RFC3810 Section 6.2: MLD queries must originate from
650 * a router's link-local address.
652 if (!IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_src)) {
653 CTR3(KTR_MLD, "ignore v1 query src %s on ifp %p(%s)",
654 ip6_sprintf(ip6tbuf, &ip6->ip6_src),
660 * Do address field validation upfront before we accept
663 if (IN6_IS_ADDR_UNSPECIFIED(&mld->mld_addr)) {
665 * MLDv1 General Query.
666 * If this was not sent to the all-nodes group, ignore it.
671 in6_clearscope(&dst);
672 if (!IN6_ARE_ADDR_EQUAL(&dst, &in6addr_linklocal_allnodes))
674 is_general_query = 1;
677 * Embed scope ID of receiving interface in MLD query for
678 * lookup whilst we don't hold other locks.
680 in6_setscope(&mld->mld_addr, ifp, NULL);
687 * Switch to MLDv1 host compatibility mode.
689 mli = MLD_IFINFO(ifp);
690 KASSERT(mli != NULL, ("%s: no mld_ifsoftc for ifp %p", __func__, ifp));
691 mld_set_version(mli, MLD_VERSION_1);
693 timer = (ntohs(mld->mld_maxdelay) * PR_FASTHZ) / MLD_TIMER_SCALE;
698 if (is_general_query) {
700 * For each reporting group joined on this
701 * interface, kick the report timer.
703 CTR2(KTR_MLD, "process v1 general query on ifp %p(%s)",
705 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
706 if (ifma->ifma_addr->sa_family != AF_INET6 ||
707 ifma->ifma_protospec == NULL)
709 inm = (struct in6_multi *)ifma->ifma_protospec;
710 mld_v1_update_group(inm, timer);
714 * MLDv1 Group-Specific Query.
715 * If this is a group-specific MLDv1 query, we need only
716 * look up the single group to process it.
718 inm = in6m_lookup_locked(ifp, &mld->mld_addr);
720 CTR3(KTR_MLD, "process v1 query %s on ifp %p(%s)",
721 ip6_sprintf(ip6tbuf, &mld->mld_addr),
723 mld_v1_update_group(inm, timer);
725 /* XXX Clear embedded scope ID as userland won't expect it. */
726 in6_clearscope(&mld->mld_addr);
729 IF_ADDR_RUNLOCK(ifp);
737 * Update the report timer on a group in response to an MLDv1 query.
739 * If we are becoming the reporting member for this group, start the timer.
740 * If we already are the reporting member for this group, and timer is
741 * below the threshold, reset it.
743 * We may be updating the group for the first time since we switched
744 * to MLDv2. If we are, then we must clear any recorded source lists,
745 * and transition to REPORTING state; the group timer is overloaded
746 * for group and group-source query responses.
748 * Unlike MLDv2, the delay per group should be jittered
749 * to avoid bursts of MLDv1 reports.
752 mld_v1_update_group(struct in6_multi *inm, const int timer)
755 char ip6tbuf[INET6_ADDRSTRLEN];
758 CTR4(KTR_MLD, "%s: %s/%s timer=%d", __func__,
759 ip6_sprintf(ip6tbuf, &inm->in6m_addr),
760 if_name(inm->in6m_ifp), timer);
762 IN6_MULTI_LOCK_ASSERT();
764 switch (inm->in6m_state) {
766 case MLD_SILENT_MEMBER:
768 case MLD_REPORTING_MEMBER:
769 if (inm->in6m_timer != 0 &&
770 inm->in6m_timer <= timer) {
771 CTR1(KTR_MLD, "%s: REPORTING and timer running, "
772 "skipping.", __func__);
776 case MLD_SG_QUERY_PENDING_MEMBER:
777 case MLD_G_QUERY_PENDING_MEMBER:
778 case MLD_IDLE_MEMBER:
779 case MLD_LAZY_MEMBER:
780 case MLD_AWAKENING_MEMBER:
781 CTR1(KTR_MLD, "%s: ->REPORTING", __func__);
782 inm->in6m_state = MLD_REPORTING_MEMBER;
783 inm->in6m_timer = MLD_RANDOM_DELAY(timer);
784 V_current_state_timers_running6 = 1;
786 case MLD_SLEEPING_MEMBER:
787 CTR1(KTR_MLD, "%s: ->AWAKENING", __func__);
788 inm->in6m_state = MLD_AWAKENING_MEMBER;
790 case MLD_LEAVING_MEMBER:
796 * Process a received MLDv2 general, group-specific or
797 * group-and-source-specific query.
799 * Assumes that the query header has been pulled up to sizeof(mldv2_query).
801 * Return 0 if successful, otherwise an appropriate error code is returned.
804 mld_v2_input_query(struct ifnet *ifp, const struct ip6_hdr *ip6,
805 struct mbuf *m, const int off, const int icmp6len)
807 struct mld_ifsoftc *mli;
808 struct mldv2_query *mld;
809 struct in6_multi *inm;
810 uint32_t maxdelay, nsrc, qqi;
811 int is_general_query;
815 char ip6tbuf[INET6_ADDRSTRLEN];
818 is_general_query = 0;
821 * RFC3810 Section 6.2: MLD queries must originate from
822 * a router's link-local address.
824 if (!IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_src)) {
825 CTR3(KTR_MLD, "ignore v1 query src %s on ifp %p(%s)",
826 ip6_sprintf(ip6tbuf, &ip6->ip6_src),
831 CTR2(KTR_MLD, "input v2 query on ifp %p(%s)", ifp, if_name(ifp));
833 mld = (struct mldv2_query *)(mtod(m, uint8_t *) + off);
835 maxdelay = ntohs(mld->mld_maxdelay); /* in 1/10ths of a second */
836 if (maxdelay >= 32768) {
837 maxdelay = (MLD_MRC_MANT(maxdelay) | 0x1000) <<
838 (MLD_MRC_EXP(maxdelay) + 3);
840 timer = (maxdelay * PR_FASTHZ) / MLD_TIMER_SCALE;
844 qrv = MLD_QRV(mld->mld_misc);
846 CTR3(KTR_MLD, "%s: clamping qrv %d to %d", __func__,
853 qqi = MLD_QQIC_MANT(mld->mld_qqi) <<
854 (MLD_QQIC_EXP(mld->mld_qqi) + 3);
857 nsrc = ntohs(mld->mld_numsrc);
858 if (nsrc > MLD_MAX_GS_SOURCES)
860 if (icmp6len < sizeof(struct mldv2_query) +
861 (nsrc * sizeof(struct in6_addr)))
865 * Do further input validation upfront to avoid resetting timers
866 * should we need to discard this query.
868 if (IN6_IS_ADDR_UNSPECIFIED(&mld->mld_addr)) {
870 * A general query with a source list has undefined
871 * behaviour; discard it.
875 is_general_query = 1;
878 * Embed scope ID of receiving interface in MLD query for
879 * lookup whilst we don't hold other locks (due to KAME
880 * locking lameness). We own this mbuf chain just now.
882 in6_setscope(&mld->mld_addr, ifp, NULL);
888 mli = MLD_IFINFO(ifp);
889 KASSERT(mli != NULL, ("%s: no mld_ifsoftc for ifp %p", __func__, ifp));
892 * Discard the v2 query if we're in Compatibility Mode.
893 * The RFC is pretty clear that hosts need to stay in MLDv1 mode
894 * until the Old Version Querier Present timer expires.
896 if (mli->mli_version != MLD_VERSION_2)
899 mld_set_version(mli, MLD_VERSION_2);
902 mli->mli_qri = maxdelay;
904 CTR4(KTR_MLD, "%s: qrv %d qi %d maxdelay %d", __func__, qrv, qqi,
907 if (is_general_query) {
909 * MLDv2 General Query.
911 * Schedule a current-state report on this ifp for
912 * all groups, possibly containing source lists.
914 * If there is a pending General Query response
915 * scheduled earlier than the selected delay, do
916 * not schedule any other reports.
917 * Otherwise, reset the interface timer.
919 CTR2(KTR_MLD, "process v2 general query on ifp %p(%s)",
921 if (mli->mli_v2_timer == 0 || mli->mli_v2_timer >= timer) {
922 mli->mli_v2_timer = MLD_RANDOM_DELAY(timer);
923 V_interface_timers_running6 = 1;
927 * MLDv2 Group-specific or Group-and-source-specific Query.
929 * Group-source-specific queries are throttled on
930 * a per-group basis to defeat denial-of-service attempts.
931 * Queries for groups we are not a member of on this
932 * link are simply ignored.
935 inm = in6m_lookup_locked(ifp, &mld->mld_addr);
937 IF_ADDR_RUNLOCK(ifp);
941 if (!ratecheck(&inm->in6m_lastgsrtv,
943 CTR1(KTR_MLD, "%s: GS query throttled.",
945 IF_ADDR_RUNLOCK(ifp);
949 CTR2(KTR_MLD, "process v2 group query on ifp %p(%s)",
952 * If there is a pending General Query response
953 * scheduled sooner than the selected delay, no
954 * further report need be scheduled.
955 * Otherwise, prepare to respond to the
956 * group-specific or group-and-source query.
958 if (mli->mli_v2_timer == 0 || mli->mli_v2_timer >= timer)
959 mld_v2_process_group_query(inm, mli, timer, m, off);
961 /* XXX Clear embedded scope ID as userland won't expect it. */
962 in6_clearscope(&mld->mld_addr);
963 IF_ADDR_RUNLOCK(ifp);
974 * Process a received MLDv2 group-specific or group-and-source-specific
976 * Return <0 if any error occurred. Currently this is ignored.
979 mld_v2_process_group_query(struct in6_multi *inm, struct mld_ifsoftc *mli,
980 int timer, struct mbuf *m0, const int off)
982 struct mldv2_query *mld;
986 IN6_MULTI_LOCK_ASSERT();
990 mld = (struct mldv2_query *)(mtod(m0, uint8_t *) + off);
992 switch (inm->in6m_state) {
994 case MLD_SILENT_MEMBER:
995 case MLD_SLEEPING_MEMBER:
996 case MLD_LAZY_MEMBER:
997 case MLD_AWAKENING_MEMBER:
998 case MLD_IDLE_MEMBER:
999 case MLD_LEAVING_MEMBER:
1002 case MLD_REPORTING_MEMBER:
1003 case MLD_G_QUERY_PENDING_MEMBER:
1004 case MLD_SG_QUERY_PENDING_MEMBER:
1008 nsrc = ntohs(mld->mld_numsrc);
1011 * Deal with group-specific queries upfront.
1012 * If any group query is already pending, purge any recorded
1013 * source-list state if it exists, and schedule a query response
1014 * for this group-specific query.
1017 if (inm->in6m_state == MLD_G_QUERY_PENDING_MEMBER ||
1018 inm->in6m_state == MLD_SG_QUERY_PENDING_MEMBER) {
1019 in6m_clear_recorded(inm);
1020 timer = min(inm->in6m_timer, timer);
1022 inm->in6m_state = MLD_G_QUERY_PENDING_MEMBER;
1023 inm->in6m_timer = MLD_RANDOM_DELAY(timer);
1024 V_current_state_timers_running6 = 1;
1029 * Deal with the case where a group-and-source-specific query has
1030 * been received but a group-specific query is already pending.
1032 if (inm->in6m_state == MLD_G_QUERY_PENDING_MEMBER) {
1033 timer = min(inm->in6m_timer, timer);
1034 inm->in6m_timer = MLD_RANDOM_DELAY(timer);
1035 V_current_state_timers_running6 = 1;
1040 * Finally, deal with the case where a group-and-source-specific
1041 * query has been received, where a response to a previous g-s-r
1042 * query exists, or none exists.
1043 * In this case, we need to parse the source-list which the Querier
1044 * has provided us with and check if we have any source list filter
1045 * entries at T1 for these sources. If we do not, there is no need
1046 * schedule a report and the query may be dropped.
1047 * If we do, we must record them and schedule a current-state
1048 * report for those sources.
1050 if (inm->in6m_nsrc > 0) {
1057 soff = off + sizeof(struct mldv2_query);
1059 for (i = 0; i < nsrc; i++) {
1060 sp = mtod(m, uint8_t *) + soff;
1061 retval = in6m_record_source(inm,
1062 (const struct in6_addr *)sp);
1065 nrecorded += retval;
1066 soff += sizeof(struct in6_addr);
1067 if (soff >= m->m_len) {
1068 soff = soff - m->m_len;
1074 if (nrecorded > 0) {
1076 "%s: schedule response to SG query", __func__);
1077 inm->in6m_state = MLD_SG_QUERY_PENDING_MEMBER;
1078 inm->in6m_timer = MLD_RANDOM_DELAY(timer);
1079 V_current_state_timers_running6 = 1;
1087 * Process a received MLDv1 host membership report.
1088 * Assumes mld points to mld_hdr in pulled up mbuf chain.
1090 * NOTE: Can't be fully const correct as we temporarily embed scope ID in
1091 * mld_addr. This is OK as we own the mbuf chain.
1094 mld_v1_input_report(struct ifnet *ifp, const struct ip6_hdr *ip6,
1095 /*const*/ struct mld_hdr *mld)
1097 struct in6_addr src, dst;
1098 struct in6_ifaddr *ia;
1099 struct in6_multi *inm;
1101 char ip6tbuf[INET6_ADDRSTRLEN];
1104 if (!mld_v1enable) {
1105 CTR3(KTR_MLD, "ignore v1 report %s on ifp %p(%s)",
1106 ip6_sprintf(ip6tbuf, &mld->mld_addr),
1111 if (ifp->if_flags & IFF_LOOPBACK)
1115 * MLDv1 reports must originate from a host's link-local address,
1116 * or the unspecified address (when booting).
1119 in6_clearscope(&src);
1120 if (!IN6_IS_SCOPE_LINKLOCAL(&src) && !IN6_IS_ADDR_UNSPECIFIED(&src)) {
1121 CTR3(KTR_MLD, "ignore v1 query src %s on ifp %p(%s)",
1122 ip6_sprintf(ip6tbuf, &ip6->ip6_src),
1128 * RFC2710 Section 4: MLDv1 reports must pertain to a multicast
1129 * group, and must be directed to the group itself.
1132 in6_clearscope(&dst);
1133 if (!IN6_IS_ADDR_MULTICAST(&mld->mld_addr) ||
1134 !IN6_ARE_ADDR_EQUAL(&mld->mld_addr, &dst)) {
1135 CTR3(KTR_MLD, "ignore v1 query dst %s on ifp %p(%s)",
1136 ip6_sprintf(ip6tbuf, &ip6->ip6_dst),
1142 * Make sure we don't hear our own membership report, as fast
1143 * leave requires knowing that we are the only member of a
1144 * group. Assume we used the link-local address if available,
1145 * otherwise look for ::.
1147 * XXX Note that scope ID comparison is needed for the address
1148 * returned by in6ifa_ifpforlinklocal(), but SHOULD NOT be
1149 * performed for the on-wire address.
1151 ia = in6ifa_ifpforlinklocal(ifp, IN6_IFF_NOTREADY|IN6_IFF_ANYCAST);
1152 if ((ia && IN6_ARE_ADDR_EQUAL(&ip6->ip6_src, IA6_IN6(ia))) ||
1153 (ia == NULL && IN6_IS_ADDR_UNSPECIFIED(&src))) {
1155 ifa_free(&ia->ia_ifa);
1159 ifa_free(&ia->ia_ifa);
1161 CTR3(KTR_MLD, "process v1 report %s on ifp %p(%s)",
1162 ip6_sprintf(ip6tbuf, &mld->mld_addr), ifp, if_name(ifp));
1165 * Embed scope ID of receiving interface in MLD query for lookup
1166 * whilst we don't hold other locks (due to KAME locking lameness).
1168 if (!IN6_IS_ADDR_UNSPECIFIED(&mld->mld_addr))
1169 in6_setscope(&mld->mld_addr, ifp, NULL);
1176 * MLDv1 report suppression.
1177 * If we are a member of this group, and our membership should be
1178 * reported, and our group timer is pending or about to be reset,
1179 * stop our group timer by transitioning to the 'lazy' state.
1181 inm = in6m_lookup_locked(ifp, &mld->mld_addr);
1183 struct mld_ifsoftc *mli;
1185 mli = inm->in6m_mli;
1186 KASSERT(mli != NULL,
1187 ("%s: no mli for ifp %p", __func__, ifp));
1190 * If we are in MLDv2 host mode, do not allow the
1191 * other host's MLDv1 report to suppress our reports.
1193 if (mli->mli_version == MLD_VERSION_2)
1196 inm->in6m_timer = 0;
1198 switch (inm->in6m_state) {
1199 case MLD_NOT_MEMBER:
1200 case MLD_SILENT_MEMBER:
1201 case MLD_SLEEPING_MEMBER:
1203 case MLD_REPORTING_MEMBER:
1204 case MLD_IDLE_MEMBER:
1205 case MLD_AWAKENING_MEMBER:
1207 "report suppressed for %s on ifp %p(%s)",
1208 ip6_sprintf(ip6tbuf, &mld->mld_addr),
1210 case MLD_LAZY_MEMBER:
1211 inm->in6m_state = MLD_LAZY_MEMBER;
1213 case MLD_G_QUERY_PENDING_MEMBER:
1214 case MLD_SG_QUERY_PENDING_MEMBER:
1215 case MLD_LEAVING_MEMBER:
1221 IF_ADDR_RUNLOCK(ifp);
1225 /* XXX Clear embedded scope ID as userland won't expect it. */
1226 in6_clearscope(&mld->mld_addr);
1234 * Assume query messages which fit in a single ICMPv6 message header
1235 * have been pulled up.
1236 * Assume that userland will want to see the message, even if it
1237 * otherwise fails kernel input validation; do not free it.
1238 * Pullup may however free the mbuf chain m if it fails.
1240 * Return IPPROTO_DONE if we freed m. Otherwise, return 0.
1243 mld_input(struct mbuf *m, int off, int icmp6len)
1246 struct ip6_hdr *ip6;
1247 struct mld_hdr *mld;
1250 CTR3(KTR_MLD, "%s: called w/mbuf (%p,%d)", __func__, m, off);
1252 ifp = m->m_pkthdr.rcvif;
1254 ip6 = mtod(m, struct ip6_hdr *);
1256 /* Pullup to appropriate size. */
1257 mld = (struct mld_hdr *)(mtod(m, uint8_t *) + off);
1258 if (mld->mld_type == MLD_LISTENER_QUERY &&
1259 icmp6len >= sizeof(struct mldv2_query)) {
1260 mldlen = sizeof(struct mldv2_query);
1262 mldlen = sizeof(struct mld_hdr);
1264 IP6_EXTHDR_GET(mld, struct mld_hdr *, m, off, mldlen);
1266 ICMP6STAT_INC(icp6s_badlen);
1267 return (IPPROTO_DONE);
1271 * Userland needs to see all of this traffic for implementing
1272 * the endpoint discovery portion of multicast routing.
1274 switch (mld->mld_type) {
1275 case MLD_LISTENER_QUERY:
1276 icmp6_ifstat_inc(ifp, ifs6_in_mldquery);
1277 if (icmp6len == sizeof(struct mld_hdr)) {
1278 if (mld_v1_input_query(ifp, ip6, mld) != 0)
1280 } else if (icmp6len >= sizeof(struct mldv2_query)) {
1281 if (mld_v2_input_query(ifp, ip6, m, off,
1286 case MLD_LISTENER_REPORT:
1287 icmp6_ifstat_inc(ifp, ifs6_in_mldreport);
1288 if (mld_v1_input_report(ifp, ip6, mld) != 0)
1291 case MLDV2_LISTENER_REPORT:
1292 icmp6_ifstat_inc(ifp, ifs6_in_mldreport);
1294 case MLD_LISTENER_DONE:
1295 icmp6_ifstat_inc(ifp, ifs6_in_mlddone);
1305 * Fast timeout handler (global).
1306 * VIMAGE: Timeout handlers are expected to service all vimages.
1311 VNET_ITERATOR_DECL(vnet_iter);
1313 VNET_LIST_RLOCK_NOSLEEP();
1314 VNET_FOREACH(vnet_iter) {
1315 CURVNET_SET(vnet_iter);
1316 mld_fasttimo_vnet();
1319 VNET_LIST_RUNLOCK_NOSLEEP();
1323 * Fast timeout handler (per-vnet).
1325 * VIMAGE: Assume caller has set up our curvnet.
1328 mld_fasttimo_vnet(void)
1330 struct mbufq scq; /* State-change packets */
1331 struct mbufq qrq; /* Query response packets */
1333 struct mld_ifsoftc *mli;
1334 struct ifmultiaddr *ifma;
1335 struct in6_multi *inm, *tinm;
1341 * Quick check to see if any work needs to be done, in order to
1342 * minimize the overhead of fasttimo processing.
1343 * SMPng: XXX Unlocked reads.
1345 if (!V_current_state_timers_running6 &&
1346 !V_interface_timers_running6 &&
1347 !V_state_change_timers_running6)
1354 * MLDv2 General Query response timer processing.
1356 if (V_interface_timers_running6) {
1357 CTR1(KTR_MLD, "%s: interface timers running", __func__);
1359 V_interface_timers_running6 = 0;
1360 LIST_FOREACH(mli, &V_mli_head, mli_link) {
1361 if (mli->mli_v2_timer == 0) {
1363 } else if (--mli->mli_v2_timer == 0) {
1364 mld_v2_dispatch_general_query(mli);
1366 V_interface_timers_running6 = 1;
1371 if (!V_current_state_timers_running6 &&
1372 !V_state_change_timers_running6)
1375 V_current_state_timers_running6 = 0;
1376 V_state_change_timers_running6 = 0;
1378 CTR1(KTR_MLD, "%s: state change timers running", __func__);
1381 * MLD host report and state-change timer processing.
1382 * Note: Processing a v2 group timer may remove a node.
1384 LIST_FOREACH(mli, &V_mli_head, mli_link) {
1387 if (mli->mli_version == MLD_VERSION_2) {
1388 uri_fasthz = MLD_RANDOM_DELAY(mli->mli_uri *
1390 mbufq_init(&qrq, MLD_MAX_G_GS_PACKETS);
1391 mbufq_init(&scq, MLD_MAX_STATE_CHANGE_PACKETS);
1395 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
1396 if (ifma->ifma_addr->sa_family != AF_INET6 ||
1397 ifma->ifma_protospec == NULL)
1399 inm = (struct in6_multi *)ifma->ifma_protospec;
1400 switch (mli->mli_version) {
1402 mld_v1_process_group_timer(mli, inm);
1405 mld_v2_process_group_timers(mli, &qrq,
1406 &scq, inm, uri_fasthz);
1410 IF_ADDR_RUNLOCK(ifp);
1412 switch (mli->mli_version) {
1415 * Transmit reports for this lifecycle. This
1416 * is done while not holding IF_ADDR_LOCK
1417 * since this can call
1418 * in6ifa_ifpforlinklocal() which locks
1419 * IF_ADDR_LOCK internally as well as
1420 * ip6_output() to transmit a packet.
1422 SLIST_FOREACH_SAFE(inm, &mli->mli_relinmhead,
1424 SLIST_REMOVE_HEAD(&mli->mli_relinmhead,
1426 (void)mld_v1_transmit_report(inm,
1427 MLD_LISTENER_REPORT);
1431 mld_dispatch_queue(&qrq, 0);
1432 mld_dispatch_queue(&scq, 0);
1435 * Free the in_multi reference(s) for
1438 SLIST_FOREACH_SAFE(inm, &mli->mli_relinmhead,
1440 SLIST_REMOVE_HEAD(&mli->mli_relinmhead,
1442 in6m_release_locked(inm);
1454 * Update host report group timer.
1455 * Will update the global pending timer flags.
1458 mld_v1_process_group_timer(struct mld_ifsoftc *mli, struct in6_multi *inm)
1460 int report_timer_expired;
1462 IN6_MULTI_LOCK_ASSERT();
1465 if (inm->in6m_timer == 0) {
1466 report_timer_expired = 0;
1467 } else if (--inm->in6m_timer == 0) {
1468 report_timer_expired = 1;
1470 V_current_state_timers_running6 = 1;
1474 switch (inm->in6m_state) {
1475 case MLD_NOT_MEMBER:
1476 case MLD_SILENT_MEMBER:
1477 case MLD_IDLE_MEMBER:
1478 case MLD_LAZY_MEMBER:
1479 case MLD_SLEEPING_MEMBER:
1480 case MLD_AWAKENING_MEMBER:
1482 case MLD_REPORTING_MEMBER:
1483 if (report_timer_expired) {
1484 inm->in6m_state = MLD_IDLE_MEMBER;
1485 SLIST_INSERT_HEAD(&mli->mli_relinmhead, inm,
1489 case MLD_G_QUERY_PENDING_MEMBER:
1490 case MLD_SG_QUERY_PENDING_MEMBER:
1491 case MLD_LEAVING_MEMBER:
1497 * Update a group's timers for MLDv2.
1498 * Will update the global pending timer flags.
1499 * Note: Unlocked read from mli.
1502 mld_v2_process_group_timers(struct mld_ifsoftc *mli,
1503 struct mbufq *qrq, struct mbufq *scq,
1504 struct in6_multi *inm, const int uri_fasthz)
1506 int query_response_timer_expired;
1507 int state_change_retransmit_timer_expired;
1509 char ip6tbuf[INET6_ADDRSTRLEN];
1512 IN6_MULTI_LOCK_ASSERT();
1515 query_response_timer_expired = 0;
1516 state_change_retransmit_timer_expired = 0;
1519 * During a transition from compatibility mode back to MLDv2,
1520 * a group record in REPORTING state may still have its group
1521 * timer active. This is a no-op in this function; it is easier
1522 * to deal with it here than to complicate the slow-timeout path.
1524 if (inm->in6m_timer == 0) {
1525 query_response_timer_expired = 0;
1526 } else if (--inm->in6m_timer == 0) {
1527 query_response_timer_expired = 1;
1529 V_current_state_timers_running6 = 1;
1532 if (inm->in6m_sctimer == 0) {
1533 state_change_retransmit_timer_expired = 0;
1534 } else if (--inm->in6m_sctimer == 0) {
1535 state_change_retransmit_timer_expired = 1;
1537 V_state_change_timers_running6 = 1;
1540 /* We are in fasttimo, so be quick about it. */
1541 if (!state_change_retransmit_timer_expired &&
1542 !query_response_timer_expired)
1545 switch (inm->in6m_state) {
1546 case MLD_NOT_MEMBER:
1547 case MLD_SILENT_MEMBER:
1548 case MLD_SLEEPING_MEMBER:
1549 case MLD_LAZY_MEMBER:
1550 case MLD_AWAKENING_MEMBER:
1551 case MLD_IDLE_MEMBER:
1553 case MLD_G_QUERY_PENDING_MEMBER:
1554 case MLD_SG_QUERY_PENDING_MEMBER:
1556 * Respond to a previously pending Group-Specific
1557 * or Group-and-Source-Specific query by enqueueing
1558 * the appropriate Current-State report for
1559 * immediate transmission.
1561 if (query_response_timer_expired) {
1564 retval = mld_v2_enqueue_group_record(qrq, inm, 0, 1,
1565 (inm->in6m_state == MLD_SG_QUERY_PENDING_MEMBER),
1567 CTR2(KTR_MLD, "%s: enqueue record = %d",
1569 inm->in6m_state = MLD_REPORTING_MEMBER;
1570 in6m_clear_recorded(inm);
1573 case MLD_REPORTING_MEMBER:
1574 case MLD_LEAVING_MEMBER:
1575 if (state_change_retransmit_timer_expired) {
1577 * State-change retransmission timer fired.
1578 * If there are any further pending retransmissions,
1579 * set the global pending state-change flag, and
1582 if (--inm->in6m_scrv > 0) {
1583 inm->in6m_sctimer = uri_fasthz;
1584 V_state_change_timers_running6 = 1;
1587 * Retransmit the previously computed state-change
1588 * report. If there are no further pending
1589 * retransmissions, the mbuf queue will be consumed.
1590 * Update T0 state to T1 as we have now sent
1593 (void)mld_v2_merge_state_changes(inm, scq);
1596 CTR3(KTR_MLD, "%s: T1 -> T0 for %s/%s", __func__,
1597 ip6_sprintf(ip6tbuf, &inm->in6m_addr),
1598 if_name(inm->in6m_ifp));
1601 * If we are leaving the group for good, make sure
1602 * we release MLD's reference to it.
1603 * This release must be deferred using a SLIST,
1604 * as we are called from a loop which traverses
1605 * the in_ifmultiaddr TAILQ.
1607 if (inm->in6m_state == MLD_LEAVING_MEMBER &&
1608 inm->in6m_scrv == 0) {
1609 inm->in6m_state = MLD_NOT_MEMBER;
1610 SLIST_INSERT_HEAD(&mli->mli_relinmhead,
1619 * Switch to a different version on the given interface,
1620 * as per Section 9.12.
1623 mld_set_version(struct mld_ifsoftc *mli, const int version)
1625 int old_version_timer;
1629 CTR4(KTR_MLD, "%s: switching to v%d on ifp %p(%s)", __func__,
1630 version, mli->mli_ifp, if_name(mli->mli_ifp));
1632 if (version == MLD_VERSION_1) {
1634 * Compute the "Older Version Querier Present" timer as per
1637 old_version_timer = (mli->mli_rv * mli->mli_qi) + mli->mli_qri;
1638 old_version_timer *= PR_SLOWHZ;
1639 mli->mli_v1_timer = old_version_timer;
1642 if (mli->mli_v1_timer > 0 && mli->mli_version != MLD_VERSION_1) {
1643 mli->mli_version = MLD_VERSION_1;
1644 mld_v2_cancel_link_timers(mli);
1649 * Cancel pending MLDv2 timers for the given link and all groups
1650 * joined on it; state-change, general-query, and group-query timers.
1653 mld_v2_cancel_link_timers(struct mld_ifsoftc *mli)
1655 struct ifmultiaddr *ifma;
1657 struct in6_multi *inm, *tinm;
1659 CTR3(KTR_MLD, "%s: cancel v2 timers on ifp %p(%s)", __func__,
1660 mli->mli_ifp, if_name(mli->mli_ifp));
1662 IN6_MULTI_LOCK_ASSERT();
1666 * Fast-track this potentially expensive operation
1667 * by checking all the global 'timer pending' flags.
1669 if (!V_interface_timers_running6 &&
1670 !V_state_change_timers_running6 &&
1671 !V_current_state_timers_running6)
1674 mli->mli_v2_timer = 0;
1679 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
1680 if (ifma->ifma_addr->sa_family != AF_INET6)
1682 inm = (struct in6_multi *)ifma->ifma_protospec;
1683 switch (inm->in6m_state) {
1684 case MLD_NOT_MEMBER:
1685 case MLD_SILENT_MEMBER:
1686 case MLD_IDLE_MEMBER:
1687 case MLD_LAZY_MEMBER:
1688 case MLD_SLEEPING_MEMBER:
1689 case MLD_AWAKENING_MEMBER:
1691 case MLD_LEAVING_MEMBER:
1693 * If we are leaving the group and switching
1694 * version, we need to release the final
1695 * reference held for issuing the INCLUDE {}.
1697 SLIST_INSERT_HEAD(&mli->mli_relinmhead, inm,
1700 case MLD_G_QUERY_PENDING_MEMBER:
1701 case MLD_SG_QUERY_PENDING_MEMBER:
1702 in6m_clear_recorded(inm);
1704 case MLD_REPORTING_MEMBER:
1705 inm->in6m_sctimer = 0;
1706 inm->in6m_timer = 0;
1707 inm->in6m_state = MLD_REPORTING_MEMBER;
1709 * Free any pending MLDv2 state-change records.
1711 mbufq_drain(&inm->in6m_scq);
1715 IF_ADDR_RUNLOCK(ifp);
1716 SLIST_FOREACH_SAFE(inm, &mli->mli_relinmhead, in6m_nrele, tinm) {
1717 SLIST_REMOVE_HEAD(&mli->mli_relinmhead, in6m_nrele);
1718 in6m_release_locked(inm);
1723 * Global slowtimo handler.
1724 * VIMAGE: Timeout handlers are expected to service all vimages.
1729 VNET_ITERATOR_DECL(vnet_iter);
1731 VNET_LIST_RLOCK_NOSLEEP();
1732 VNET_FOREACH(vnet_iter) {
1733 CURVNET_SET(vnet_iter);
1734 mld_slowtimo_vnet();
1737 VNET_LIST_RUNLOCK_NOSLEEP();
1741 * Per-vnet slowtimo handler.
1744 mld_slowtimo_vnet(void)
1746 struct mld_ifsoftc *mli;
1750 LIST_FOREACH(mli, &V_mli_head, mli_link) {
1751 mld_v1_process_querier_timers(mli);
1758 * Update the Older Version Querier Present timers for a link.
1759 * See Section 9.12 of RFC 3810.
1762 mld_v1_process_querier_timers(struct mld_ifsoftc *mli)
1767 if (mli->mli_version != MLD_VERSION_2 && --mli->mli_v1_timer == 0) {
1769 * MLDv1 Querier Present timer expired; revert to MLDv2.
1772 "%s: transition from v%d -> v%d on %p(%s)",
1773 __func__, mli->mli_version, MLD_VERSION_2,
1774 mli->mli_ifp, if_name(mli->mli_ifp));
1775 mli->mli_version = MLD_VERSION_2;
1780 * Transmit an MLDv1 report immediately.
1783 mld_v1_transmit_report(struct in6_multi *in6m, const int type)
1786 struct in6_ifaddr *ia;
1787 struct ip6_hdr *ip6;
1788 struct mbuf *mh, *md;
1789 struct mld_hdr *mld;
1791 IN6_MULTI_LOCK_ASSERT();
1794 ifp = in6m->in6m_ifp;
1795 ia = in6ifa_ifpforlinklocal(ifp, IN6_IFF_NOTREADY|IN6_IFF_ANYCAST);
1796 /* ia may be NULL if link-local address is tentative. */
1798 mh = m_gethdr(M_NOWAIT, MT_DATA);
1801 ifa_free(&ia->ia_ifa);
1804 md = m_get(M_NOWAIT, MT_DATA);
1808 ifa_free(&ia->ia_ifa);
1814 * FUTURE: Consider increasing alignment by ETHER_HDR_LEN, so
1815 * that ether_output() does not need to allocate another mbuf
1816 * for the header in the most common case.
1818 M_ALIGN(mh, sizeof(struct ip6_hdr));
1819 mh->m_pkthdr.len = sizeof(struct ip6_hdr) + sizeof(struct mld_hdr);
1820 mh->m_len = sizeof(struct ip6_hdr);
1822 ip6 = mtod(mh, struct ip6_hdr *);
1824 ip6->ip6_vfc &= ~IPV6_VERSION_MASK;
1825 ip6->ip6_vfc |= IPV6_VERSION;
1826 ip6->ip6_nxt = IPPROTO_ICMPV6;
1827 ip6->ip6_src = ia ? ia->ia_addr.sin6_addr : in6addr_any;
1828 ip6->ip6_dst = in6m->in6m_addr;
1830 md->m_len = sizeof(struct mld_hdr);
1831 mld = mtod(md, struct mld_hdr *);
1832 mld->mld_type = type;
1835 mld->mld_maxdelay = 0;
1836 mld->mld_reserved = 0;
1837 mld->mld_addr = in6m->in6m_addr;
1838 in6_clearscope(&mld->mld_addr);
1839 mld->mld_cksum = in6_cksum(mh, IPPROTO_ICMPV6,
1840 sizeof(struct ip6_hdr), sizeof(struct mld_hdr));
1842 mld_save_context(mh, ifp);
1843 mh->m_flags |= M_MLDV1;
1845 mld_dispatch_packet(mh);
1848 ifa_free(&ia->ia_ifa);
1853 * Process a state change from the upper layer for the given IPv6 group.
1855 * Each socket holds a reference on the in_multi in its own ip_moptions.
1856 * The socket layer will have made the necessary updates to.the group
1857 * state, it is now up to MLD to issue a state change report if there
1858 * has been any change between T0 (when the last state-change was issued)
1861 * We use the MLDv2 state machine at group level. The MLd module
1862 * however makes the decision as to which MLD protocol version to speak.
1863 * A state change *from* INCLUDE {} always means an initial join.
1864 * A state change *to* INCLUDE {} always means a final leave.
1866 * If delay is non-zero, and the state change is an initial multicast
1867 * join, the state change report will be delayed by 'delay' ticks
1868 * in units of PR_FASTHZ if MLDv1 is active on the link; otherwise
1869 * the initial MLDv2 state change report will be delayed by whichever
1870 * is sooner, a pending state-change timer or delay itself.
1872 * VIMAGE: curvnet should have been set by caller, as this routine
1873 * is called from the socket option handlers.
1876 mld_change_state(struct in6_multi *inm, const int delay)
1878 struct mld_ifsoftc *mli;
1882 IN6_MULTI_LOCK_ASSERT();
1887 * Try to detect if the upper layer just asked us to change state
1888 * for an interface which has now gone away.
1890 KASSERT(inm->in6m_ifma != NULL, ("%s: no ifma", __func__));
1891 ifp = inm->in6m_ifma->ifma_ifp;
1894 * Sanity check that netinet6's notion of ifp is the
1897 KASSERT(inm->in6m_ifp == ifp, ("%s: bad ifp", __func__));
1902 mli = MLD_IFINFO(ifp);
1903 KASSERT(mli != NULL, ("%s: no mld_ifsoftc for ifp %p", __func__, ifp));
1906 * If we detect a state transition to or from MCAST_UNDEFINED
1907 * for this group, then we are starting or finishing an MLD
1908 * life cycle for this group.
1910 if (inm->in6m_st[1].iss_fmode != inm->in6m_st[0].iss_fmode) {
1911 CTR3(KTR_MLD, "%s: inm transition %d -> %d", __func__,
1912 inm->in6m_st[0].iss_fmode, inm->in6m_st[1].iss_fmode);
1913 if (inm->in6m_st[0].iss_fmode == MCAST_UNDEFINED) {
1914 CTR1(KTR_MLD, "%s: initial join", __func__);
1915 error = mld_initial_join(inm, mli, delay);
1917 } else if (inm->in6m_st[1].iss_fmode == MCAST_UNDEFINED) {
1918 CTR1(KTR_MLD, "%s: final leave", __func__);
1919 mld_final_leave(inm, mli);
1923 CTR1(KTR_MLD, "%s: filter set change", __func__);
1926 error = mld_handle_state_change(inm, mli);
1934 * Perform the initial join for an MLD group.
1936 * When joining a group:
1937 * If the group should have its MLD traffic suppressed, do nothing.
1938 * MLDv1 starts sending MLDv1 host membership reports.
1939 * MLDv2 will schedule an MLDv2 state-change report containing the
1940 * initial state of the membership.
1942 * If the delay argument is non-zero, then we must delay sending the
1943 * initial state change for delay ticks (in units of PR_FASTHZ).
1946 mld_initial_join(struct in6_multi *inm, struct mld_ifsoftc *mli,
1951 int error, retval, syncstates;
1954 char ip6tbuf[INET6_ADDRSTRLEN];
1957 CTR4(KTR_MLD, "%s: initial join %s on ifp %p(%s)",
1958 __func__, ip6_sprintf(ip6tbuf, &inm->in6m_addr),
1959 inm->in6m_ifp, if_name(inm->in6m_ifp));
1964 ifp = inm->in6m_ifp;
1966 IN6_MULTI_LOCK_ASSERT();
1969 KASSERT(mli && mli->mli_ifp == ifp, ("%s: inconsistent ifp", __func__));
1972 * Groups joined on loopback or marked as 'not reported',
1973 * enter the MLD_SILENT_MEMBER state and
1974 * are never reported in any protocol exchanges.
1975 * All other groups enter the appropriate state machine
1976 * for the version in use on this link.
1977 * A link marked as MLIF_SILENT causes MLD to be completely
1978 * disabled for the link.
1980 if ((ifp->if_flags & IFF_LOOPBACK) ||
1981 (mli->mli_flags & MLIF_SILENT) ||
1982 !mld_is_addr_reported(&inm->in6m_addr)) {
1984 "%s: not kicking state machine for silent group", __func__);
1985 inm->in6m_state = MLD_SILENT_MEMBER;
1986 inm->in6m_timer = 0;
1989 * Deal with overlapping in_multi lifecycle.
1990 * If this group was LEAVING, then make sure
1991 * we drop the reference we picked up to keep the
1992 * group around for the final INCLUDE {} enqueue.
1994 if (mli->mli_version == MLD_VERSION_2 &&
1995 inm->in6m_state == MLD_LEAVING_MEMBER)
1996 in6m_release_locked(inm);
1998 inm->in6m_state = MLD_REPORTING_MEMBER;
2000 switch (mli->mli_version) {
2003 * If a delay was provided, only use it if
2004 * it is greater than the delay normally
2005 * used for an MLDv1 state change report,
2006 * and delay sending the initial MLDv1 report
2007 * by not transitioning to the IDLE state.
2009 odelay = MLD_RANDOM_DELAY(MLD_V1_MAX_RI * PR_FASTHZ);
2011 inm->in6m_timer = max(delay, odelay);
2012 V_current_state_timers_running6 = 1;
2014 inm->in6m_state = MLD_IDLE_MEMBER;
2015 error = mld_v1_transmit_report(inm,
2016 MLD_LISTENER_REPORT);
2018 inm->in6m_timer = odelay;
2019 V_current_state_timers_running6 = 1;
2026 * Defer update of T0 to T1, until the first copy
2027 * of the state change has been transmitted.
2032 * Immediately enqueue a State-Change Report for
2033 * this interface, freeing any previous reports.
2034 * Don't kick the timers if there is nothing to do,
2035 * or if an error occurred.
2037 mq = &inm->in6m_scq;
2039 retval = mld_v2_enqueue_group_record(mq, inm, 1,
2040 0, 0, (mli->mli_flags & MLIF_USEALLOW));
2041 CTR2(KTR_MLD, "%s: enqueue record = %d",
2044 error = retval * -1;
2049 * Schedule transmission of pending state-change
2050 * report up to RV times for this link. The timer
2051 * will fire at the next mld_fasttimo (~200ms),
2052 * giving us an opportunity to merge the reports.
2054 * If a delay was provided to this function, only
2055 * use this delay if sooner than the existing one.
2057 KASSERT(mli->mli_rv > 1,
2058 ("%s: invalid robustness %d", __func__,
2060 inm->in6m_scrv = mli->mli_rv;
2062 if (inm->in6m_sctimer > 1) {
2064 min(inm->in6m_sctimer, delay);
2066 inm->in6m_sctimer = delay;
2068 inm->in6m_sctimer = 1;
2069 V_state_change_timers_running6 = 1;
2077 * Only update the T0 state if state change is atomic,
2078 * i.e. we don't need to wait for a timer to fire before we
2079 * can consider the state change to have been communicated.
2083 CTR3(KTR_MLD, "%s: T1 -> T0 for %s/%s", __func__,
2084 ip6_sprintf(ip6tbuf, &inm->in6m_addr),
2085 if_name(inm->in6m_ifp));
2092 * Issue an intermediate state change during the life-cycle.
2095 mld_handle_state_change(struct in6_multi *inm, struct mld_ifsoftc *mli)
2100 char ip6tbuf[INET6_ADDRSTRLEN];
2103 CTR4(KTR_MLD, "%s: state change for %s on ifp %p(%s)",
2104 __func__, ip6_sprintf(ip6tbuf, &inm->in6m_addr),
2105 inm->in6m_ifp, if_name(inm->in6m_ifp));
2107 ifp = inm->in6m_ifp;
2109 IN6_MULTI_LOCK_ASSERT();
2112 KASSERT(mli && mli->mli_ifp == ifp,
2113 ("%s: inconsistent ifp", __func__));
2115 if ((ifp->if_flags & IFF_LOOPBACK) ||
2116 (mli->mli_flags & MLIF_SILENT) ||
2117 !mld_is_addr_reported(&inm->in6m_addr) ||
2118 (mli->mli_version != MLD_VERSION_2)) {
2119 if (!mld_is_addr_reported(&inm->in6m_addr)) {
2121 "%s: not kicking state machine for silent group", __func__);
2123 CTR1(KTR_MLD, "%s: nothing to do", __func__);
2125 CTR3(KTR_MLD, "%s: T1 -> T0 for %s/%s", __func__,
2126 ip6_sprintf(ip6tbuf, &inm->in6m_addr),
2127 if_name(inm->in6m_ifp));
2131 mbufq_drain(&inm->in6m_scq);
2133 retval = mld_v2_enqueue_group_record(&inm->in6m_scq, inm, 1, 0, 0,
2134 (mli->mli_flags & MLIF_USEALLOW));
2135 CTR2(KTR_MLD, "%s: enqueue record = %d", __func__, retval);
2140 * If record(s) were enqueued, start the state-change
2141 * report timer for this group.
2143 inm->in6m_scrv = mli->mli_rv;
2144 inm->in6m_sctimer = 1;
2145 V_state_change_timers_running6 = 1;
2151 * Perform the final leave for a multicast address.
2153 * When leaving a group:
2154 * MLDv1 sends a DONE message, if and only if we are the reporter.
2155 * MLDv2 enqueues a state-change report containing a transition
2156 * to INCLUDE {} for immediate transmission.
2159 mld_final_leave(struct in6_multi *inm, struct mld_ifsoftc *mli)
2163 char ip6tbuf[INET6_ADDRSTRLEN];
2168 CTR4(KTR_MLD, "%s: final leave %s on ifp %p(%s)",
2169 __func__, ip6_sprintf(ip6tbuf, &inm->in6m_addr),
2170 inm->in6m_ifp, if_name(inm->in6m_ifp));
2172 IN6_MULTI_LOCK_ASSERT();
2175 switch (inm->in6m_state) {
2176 case MLD_NOT_MEMBER:
2177 case MLD_SILENT_MEMBER:
2178 case MLD_LEAVING_MEMBER:
2179 /* Already leaving or left; do nothing. */
2181 "%s: not kicking state machine for silent group", __func__);
2183 case MLD_REPORTING_MEMBER:
2184 case MLD_IDLE_MEMBER:
2185 case MLD_G_QUERY_PENDING_MEMBER:
2186 case MLD_SG_QUERY_PENDING_MEMBER:
2187 if (mli->mli_version == MLD_VERSION_1) {
2189 if (inm->in6m_state == MLD_G_QUERY_PENDING_MEMBER ||
2190 inm->in6m_state == MLD_SG_QUERY_PENDING_MEMBER)
2191 panic("%s: MLDv2 state reached, not MLDv2 mode",
2194 mld_v1_transmit_report(inm, MLD_LISTENER_DONE);
2195 inm->in6m_state = MLD_NOT_MEMBER;
2196 V_current_state_timers_running6 = 1;
2197 } else if (mli->mli_version == MLD_VERSION_2) {
2199 * Stop group timer and all pending reports.
2200 * Immediately enqueue a state-change report
2201 * TO_IN {} to be sent on the next fast timeout,
2202 * giving us an opportunity to merge reports.
2204 mbufq_drain(&inm->in6m_scq);
2205 inm->in6m_timer = 0;
2206 inm->in6m_scrv = mli->mli_rv;
2207 CTR4(KTR_MLD, "%s: Leaving %s/%s with %d "
2208 "pending retransmissions.", __func__,
2209 ip6_sprintf(ip6tbuf, &inm->in6m_addr),
2210 if_name(inm->in6m_ifp), inm->in6m_scrv);
2211 if (inm->in6m_scrv == 0) {
2212 inm->in6m_state = MLD_NOT_MEMBER;
2213 inm->in6m_sctimer = 0;
2217 in6m_acquire_locked(inm);
2219 retval = mld_v2_enqueue_group_record(
2220 &inm->in6m_scq, inm, 1, 0, 0,
2221 (mli->mli_flags & MLIF_USEALLOW));
2222 KASSERT(retval != 0,
2223 ("%s: enqueue record = %d", __func__,
2226 inm->in6m_state = MLD_LEAVING_MEMBER;
2227 inm->in6m_sctimer = 1;
2228 V_state_change_timers_running6 = 1;
2234 case MLD_LAZY_MEMBER:
2235 case MLD_SLEEPING_MEMBER:
2236 case MLD_AWAKENING_MEMBER:
2237 /* Our reports are suppressed; do nothing. */
2243 CTR3(KTR_MLD, "%s: T1 -> T0 for %s/%s", __func__,
2244 ip6_sprintf(ip6tbuf, &inm->in6m_addr),
2245 if_name(inm->in6m_ifp));
2246 inm->in6m_st[1].iss_fmode = MCAST_UNDEFINED;
2247 CTR3(KTR_MLD, "%s: T1 now MCAST_UNDEFINED for %p/%s",
2248 __func__, &inm->in6m_addr, if_name(inm->in6m_ifp));
2253 * Enqueue an MLDv2 group record to the given output queue.
2255 * If is_state_change is zero, a current-state record is appended.
2256 * If is_state_change is non-zero, a state-change report is appended.
2258 * If is_group_query is non-zero, an mbuf packet chain is allocated.
2259 * If is_group_query is zero, and if there is a packet with free space
2260 * at the tail of the queue, it will be appended to providing there
2261 * is enough free space.
2262 * Otherwise a new mbuf packet chain is allocated.
2264 * If is_source_query is non-zero, each source is checked to see if
2265 * it was recorded for a Group-Source query, and will be omitted if
2266 * it is not both in-mode and recorded.
2268 * If use_block_allow is non-zero, state change reports for initial join
2269 * and final leave, on an inclusive mode group with a source list, will be
2270 * rewritten to use the ALLOW_NEW and BLOCK_OLD record types, respectively.
2272 * The function will attempt to allocate leading space in the packet
2273 * for the IPv6+ICMP headers to be prepended without fragmenting the chain.
2275 * If successful the size of all data appended to the queue is returned,
2276 * otherwise an error code less than zero is returned, or zero if
2277 * no record(s) were appended.
2280 mld_v2_enqueue_group_record(struct mbufq *mq, struct in6_multi *inm,
2281 const int is_state_change, const int is_group_query,
2282 const int is_source_query, const int use_block_allow)
2284 struct mldv2_record mr;
2285 struct mldv2_record *pmr;
2287 struct ip6_msource *ims, *nims;
2288 struct mbuf *m0, *m, *md;
2289 int is_filter_list_change;
2290 int minrec0len, m0srcs, msrcs, nbytes, off;
2291 int record_has_sources;
2296 char ip6tbuf[INET6_ADDRSTRLEN];
2299 IN6_MULTI_LOCK_ASSERT();
2301 ifp = inm->in6m_ifp;
2302 is_filter_list_change = 0;
2309 record_has_sources = 1;
2311 type = MLD_DO_NOTHING;
2312 mode = inm->in6m_st[1].iss_fmode;
2315 * If we did not transition out of ASM mode during t0->t1,
2316 * and there are no source nodes to process, we can skip
2317 * the generation of source records.
2319 if (inm->in6m_st[0].iss_asm > 0 && inm->in6m_st[1].iss_asm > 0 &&
2320 inm->in6m_nsrc == 0)
2321 record_has_sources = 0;
2323 if (is_state_change) {
2325 * Queue a state change record.
2326 * If the mode did not change, and there are non-ASM
2327 * listeners or source filters present,
2328 * we potentially need to issue two records for the group.
2329 * If there are ASM listeners, and there was no filter
2330 * mode transition of any kind, do nothing.
2332 * If we are transitioning to MCAST_UNDEFINED, we need
2333 * not send any sources. A transition to/from this state is
2334 * considered inclusive with some special treatment.
2336 * If we are rewriting initial joins/leaves to use
2337 * ALLOW/BLOCK, and the group's membership is inclusive,
2338 * we need to send sources in all cases.
2340 if (mode != inm->in6m_st[0].iss_fmode) {
2341 if (mode == MCAST_EXCLUDE) {
2342 CTR1(KTR_MLD, "%s: change to EXCLUDE",
2344 type = MLD_CHANGE_TO_EXCLUDE_MODE;
2346 CTR1(KTR_MLD, "%s: change to INCLUDE",
2348 if (use_block_allow) {
2351 * Here we're interested in state
2352 * edges either direction between
2353 * MCAST_UNDEFINED and MCAST_INCLUDE.
2354 * Perhaps we should just check
2355 * the group state, rather than
2358 if (mode == MCAST_UNDEFINED) {
2359 type = MLD_BLOCK_OLD_SOURCES;
2361 type = MLD_ALLOW_NEW_SOURCES;
2364 type = MLD_CHANGE_TO_INCLUDE_MODE;
2365 if (mode == MCAST_UNDEFINED)
2366 record_has_sources = 0;
2370 if (record_has_sources) {
2371 is_filter_list_change = 1;
2373 type = MLD_DO_NOTHING;
2378 * Queue a current state record.
2380 if (mode == MCAST_EXCLUDE) {
2381 type = MLD_MODE_IS_EXCLUDE;
2382 } else if (mode == MCAST_INCLUDE) {
2383 type = MLD_MODE_IS_INCLUDE;
2384 KASSERT(inm->in6m_st[1].iss_asm == 0,
2385 ("%s: inm %p is INCLUDE but ASM count is %d",
2386 __func__, inm, inm->in6m_st[1].iss_asm));
2391 * Generate the filter list changes using a separate function.
2393 if (is_filter_list_change)
2394 return (mld_v2_enqueue_filter_change(mq, inm));
2396 if (type == MLD_DO_NOTHING) {
2397 CTR3(KTR_MLD, "%s: nothing to do for %s/%s",
2398 __func__, ip6_sprintf(ip6tbuf, &inm->in6m_addr),
2399 if_name(inm->in6m_ifp));
2404 * If any sources are present, we must be able to fit at least
2405 * one in the trailing space of the tail packet's mbuf,
2408 minrec0len = sizeof(struct mldv2_record);
2409 if (record_has_sources)
2410 minrec0len += sizeof(struct in6_addr);
2412 CTR4(KTR_MLD, "%s: queueing %s for %s/%s", __func__,
2413 mld_rec_type_to_str(type),
2414 ip6_sprintf(ip6tbuf, &inm->in6m_addr),
2415 if_name(inm->in6m_ifp));
2418 * Check if we have a packet in the tail of the queue for this
2419 * group into which the first group record for this group will fit.
2420 * Otherwise allocate a new packet.
2421 * Always allocate leading space for IP6+RA+ICMPV6+REPORT.
2422 * Note: Group records for G/GSR query responses MUST be sent
2423 * in their own packet.
2425 m0 = mbufq_last(mq);
2426 if (!is_group_query &&
2428 (m0->m_pkthdr.PH_vt.vt_nrecs + 1 <= MLD_V2_REPORT_MAXRECS) &&
2429 (m0->m_pkthdr.len + minrec0len) <
2430 (ifp->if_mtu - MLD_MTUSPACE)) {
2431 m0srcs = (ifp->if_mtu - m0->m_pkthdr.len -
2432 sizeof(struct mldv2_record)) /
2433 sizeof(struct in6_addr);
2435 CTR1(KTR_MLD, "%s: use existing packet", __func__);
2437 if (mbufq_full(mq)) {
2438 CTR1(KTR_MLD, "%s: outbound queue full", __func__);
2442 m0srcs = (ifp->if_mtu - MLD_MTUSPACE -
2443 sizeof(struct mldv2_record)) / sizeof(struct in6_addr);
2444 if (!is_state_change && !is_group_query)
2445 m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
2447 m = m_gethdr(M_NOWAIT, MT_DATA);
2451 mld_save_context(m, ifp);
2453 CTR1(KTR_MLD, "%s: allocated first packet", __func__);
2457 * Append group record.
2458 * If we have sources, we don't know how many yet.
2463 mr.mr_addr = inm->in6m_addr;
2464 in6_clearscope(&mr.mr_addr);
2465 if (!m_append(m, sizeof(struct mldv2_record), (void *)&mr)) {
2468 CTR1(KTR_MLD, "%s: m_append() failed.", __func__);
2471 nbytes += sizeof(struct mldv2_record);
2474 * Append as many sources as will fit in the first packet.
2475 * If we are appending to a new packet, the chain allocation
2476 * may potentially use clusters; use m_getptr() in this case.
2477 * If we are appending to an existing packet, we need to obtain
2478 * a pointer to the group record after m_append(), in case a new
2479 * mbuf was allocated.
2481 * Only append sources which are in-mode at t1. If we are
2482 * transitioning to MCAST_UNDEFINED state on the group, and
2483 * use_block_allow is zero, do not include source entries.
2484 * Otherwise, we need to include this source in the report.
2486 * Only report recorded sources in our filter set when responding
2487 * to a group-source query.
2489 if (record_has_sources) {
2492 pmr = (struct mldv2_record *)(mtod(md, uint8_t *) +
2493 md->m_len - nbytes);
2495 md = m_getptr(m, 0, &off);
2496 pmr = (struct mldv2_record *)(mtod(md, uint8_t *) +
2500 RB_FOREACH_SAFE(ims, ip6_msource_tree, &inm->in6m_srcs,
2502 CTR2(KTR_MLD, "%s: visit node %s", __func__,
2503 ip6_sprintf(ip6tbuf, &ims->im6s_addr));
2504 now = im6s_get_mode(inm, ims, 1);
2505 CTR2(KTR_MLD, "%s: node is %d", __func__, now);
2506 if ((now != mode) ||
2508 (!use_block_allow && mode == MCAST_UNDEFINED))) {
2509 CTR1(KTR_MLD, "%s: skip node", __func__);
2512 if (is_source_query && ims->im6s_stp == 0) {
2513 CTR1(KTR_MLD, "%s: skip unrecorded node",
2517 CTR1(KTR_MLD, "%s: append node", __func__);
2518 if (!m_append(m, sizeof(struct in6_addr),
2519 (void *)&ims->im6s_addr)) {
2522 CTR1(KTR_MLD, "%s: m_append() failed.",
2526 nbytes += sizeof(struct in6_addr);
2528 if (msrcs == m0srcs)
2531 CTR2(KTR_MLD, "%s: msrcs is %d this packet", __func__,
2533 pmr->mr_numsrc = htons(msrcs);
2534 nbytes += (msrcs * sizeof(struct in6_addr));
2537 if (is_source_query && msrcs == 0) {
2538 CTR1(KTR_MLD, "%s: no recorded sources to report", __func__);
2545 * We are good to go with first packet.
2548 CTR1(KTR_MLD, "%s: enqueueing first packet", __func__);
2549 m->m_pkthdr.PH_vt.vt_nrecs = 1;
2550 mbufq_enqueue(mq, m);
2552 m->m_pkthdr.PH_vt.vt_nrecs++;
2555 * No further work needed if no source list in packet(s).
2557 if (!record_has_sources)
2561 * Whilst sources remain to be announced, we need to allocate
2562 * a new packet and fill out as many sources as will fit.
2563 * Always try for a cluster first.
2565 while (nims != NULL) {
2566 if (mbufq_full(mq)) {
2567 CTR1(KTR_MLD, "%s: outbound queue full", __func__);
2570 m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
2572 m = m_gethdr(M_NOWAIT, MT_DATA);
2575 mld_save_context(m, ifp);
2576 md = m_getptr(m, 0, &off);
2577 pmr = (struct mldv2_record *)(mtod(md, uint8_t *) + off);
2578 CTR1(KTR_MLD, "%s: allocated next packet", __func__);
2580 if (!m_append(m, sizeof(struct mldv2_record), (void *)&mr)) {
2583 CTR1(KTR_MLD, "%s: m_append() failed.", __func__);
2586 m->m_pkthdr.PH_vt.vt_nrecs = 1;
2587 nbytes += sizeof(struct mldv2_record);
2589 m0srcs = (ifp->if_mtu - MLD_MTUSPACE -
2590 sizeof(struct mldv2_record)) / sizeof(struct in6_addr);
2593 RB_FOREACH_FROM(ims, ip6_msource_tree, nims) {
2594 CTR2(KTR_MLD, "%s: visit node %s",
2595 __func__, ip6_sprintf(ip6tbuf, &ims->im6s_addr));
2596 now = im6s_get_mode(inm, ims, 1);
2597 if ((now != mode) ||
2599 (!use_block_allow && mode == MCAST_UNDEFINED))) {
2600 CTR1(KTR_MLD, "%s: skip node", __func__);
2603 if (is_source_query && ims->im6s_stp == 0) {
2604 CTR1(KTR_MLD, "%s: skip unrecorded node",
2608 CTR1(KTR_MLD, "%s: append node", __func__);
2609 if (!m_append(m, sizeof(struct in6_addr),
2610 (void *)&ims->im6s_addr)) {
2613 CTR1(KTR_MLD, "%s: m_append() failed.",
2618 if (msrcs == m0srcs)
2621 pmr->mr_numsrc = htons(msrcs);
2622 nbytes += (msrcs * sizeof(struct in6_addr));
2624 CTR1(KTR_MLD, "%s: enqueueing next packet", __func__);
2625 mbufq_enqueue(mq, m);
2632 * Type used to mark record pass completion.
2633 * We exploit the fact we can cast to this easily from the
2634 * current filter modes on each ip_msource node.
2637 REC_NONE = 0x00, /* MCAST_UNDEFINED */
2638 REC_ALLOW = 0x01, /* MCAST_INCLUDE */
2639 REC_BLOCK = 0x02, /* MCAST_EXCLUDE */
2640 REC_FULL = REC_ALLOW | REC_BLOCK
2644 * Enqueue an MLDv2 filter list change to the given output queue.
2646 * Source list filter state is held in an RB-tree. When the filter list
2647 * for a group is changed without changing its mode, we need to compute
2648 * the deltas between T0 and T1 for each source in the filter set,
2649 * and enqueue the appropriate ALLOW_NEW/BLOCK_OLD records.
2651 * As we may potentially queue two record types, and the entire R-B tree
2652 * needs to be walked at once, we break this out into its own function
2653 * so we can generate a tightly packed queue of packets.
2655 * XXX This could be written to only use one tree walk, although that makes
2656 * serializing into the mbuf chains a bit harder. For now we do two walks
2657 * which makes things easier on us, and it may or may not be harder on
2660 * If successful the size of all data appended to the queue is returned,
2661 * otherwise an error code less than zero is returned, or zero if
2662 * no record(s) were appended.
2665 mld_v2_enqueue_filter_change(struct mbufq *mq, struct in6_multi *inm)
2667 static const int MINRECLEN =
2668 sizeof(struct mldv2_record) + sizeof(struct in6_addr);
2670 struct mldv2_record mr;
2671 struct mldv2_record *pmr;
2672 struct ip6_msource *ims, *nims;
2673 struct mbuf *m, *m0, *md;
2674 int m0srcs, nbytes, npbytes, off, rsrcs, schanged;
2676 uint8_t mode, now, then;
2677 rectype_t crt, drt, nrt;
2679 char ip6tbuf[INET6_ADDRSTRLEN];
2682 IN6_MULTI_LOCK_ASSERT();
2684 if (inm->in6m_nsrc == 0 ||
2685 (inm->in6m_st[0].iss_asm > 0 && inm->in6m_st[1].iss_asm > 0))
2688 ifp = inm->in6m_ifp; /* interface */
2689 mode = inm->in6m_st[1].iss_fmode; /* filter mode at t1 */
2690 crt = REC_NONE; /* current group record type */
2691 drt = REC_NONE; /* mask of completed group record types */
2692 nrt = REC_NONE; /* record type for current node */
2693 m0srcs = 0; /* # source which will fit in current mbuf chain */
2694 npbytes = 0; /* # of bytes appended this packet */
2695 nbytes = 0; /* # of bytes appended to group's state-change queue */
2696 rsrcs = 0; /* # sources encoded in current record */
2697 schanged = 0; /* # nodes encoded in overall filter change */
2698 nallow = 0; /* # of source entries in ALLOW_NEW */
2699 nblock = 0; /* # of source entries in BLOCK_OLD */
2700 nims = NULL; /* next tree node pointer */
2703 * For each possible filter record mode.
2704 * The first kind of source we encounter tells us which
2705 * is the first kind of record we start appending.
2706 * If a node transitioned to UNDEFINED at t1, its mode is treated
2707 * as the inverse of the group's filter mode.
2709 while (drt != REC_FULL) {
2711 m0 = mbufq_last(mq);
2713 (m0->m_pkthdr.PH_vt.vt_nrecs + 1 <=
2714 MLD_V2_REPORT_MAXRECS) &&
2715 (m0->m_pkthdr.len + MINRECLEN) <
2716 (ifp->if_mtu - MLD_MTUSPACE)) {
2718 m0srcs = (ifp->if_mtu - m0->m_pkthdr.len -
2719 sizeof(struct mldv2_record)) /
2720 sizeof(struct in6_addr);
2722 "%s: use previous packet", __func__);
2724 m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
2726 m = m_gethdr(M_NOWAIT, MT_DATA);
2729 "%s: m_get*() failed", __func__);
2732 m->m_pkthdr.PH_vt.vt_nrecs = 0;
2733 mld_save_context(m, ifp);
2734 m0srcs = (ifp->if_mtu - MLD_MTUSPACE -
2735 sizeof(struct mldv2_record)) /
2736 sizeof(struct in6_addr);
2739 "%s: allocated new packet", __func__);
2742 * Append the MLD group record header to the
2743 * current packet's data area.
2744 * Recalculate pointer to free space for next
2745 * group record, in case m_append() allocated
2746 * a new mbuf or cluster.
2748 memset(&mr, 0, sizeof(mr));
2749 mr.mr_addr = inm->in6m_addr;
2750 in6_clearscope(&mr.mr_addr);
2751 if (!m_append(m, sizeof(mr), (void *)&mr)) {
2755 "%s: m_append() failed", __func__);
2758 npbytes += sizeof(struct mldv2_record);
2760 /* new packet; offset in chain */
2761 md = m_getptr(m, npbytes -
2762 sizeof(struct mldv2_record), &off);
2763 pmr = (struct mldv2_record *)(mtod(md,
2766 /* current packet; offset from last append */
2768 pmr = (struct mldv2_record *)(mtod(md,
2769 uint8_t *) + md->m_len -
2770 sizeof(struct mldv2_record));
2773 * Begin walking the tree for this record type
2774 * pass, or continue from where we left off
2775 * previously if we had to allocate a new packet.
2776 * Only report deltas in-mode at t1.
2777 * We need not report included sources as allowed
2778 * if we are in inclusive mode on the group,
2779 * however the converse is not true.
2783 nims = RB_MIN(ip6_msource_tree,
2786 RB_FOREACH_FROM(ims, ip6_msource_tree, nims) {
2787 CTR2(KTR_MLD, "%s: visit node %s", __func__,
2788 ip6_sprintf(ip6tbuf, &ims->im6s_addr));
2789 now = im6s_get_mode(inm, ims, 1);
2790 then = im6s_get_mode(inm, ims, 0);
2791 CTR3(KTR_MLD, "%s: mode: t0 %d, t1 %d",
2792 __func__, then, now);
2795 "%s: skip unchanged", __func__);
2798 if (mode == MCAST_EXCLUDE &&
2799 now == MCAST_INCLUDE) {
2801 "%s: skip IN src on EX group",
2805 nrt = (rectype_t)now;
2806 if (nrt == REC_NONE)
2807 nrt = (rectype_t)(~mode & REC_FULL);
2808 if (schanged++ == 0) {
2810 } else if (crt != nrt)
2812 if (!m_append(m, sizeof(struct in6_addr),
2813 (void *)&ims->im6s_addr)) {
2817 "%s: m_append() failed", __func__);
2820 nallow += !!(crt == REC_ALLOW);
2821 nblock += !!(crt == REC_BLOCK);
2822 if (++rsrcs == m0srcs)
2826 * If we did not append any tree nodes on this
2827 * pass, back out of allocations.
2830 npbytes -= sizeof(struct mldv2_record);
2833 "%s: m_free(m)", __func__);
2837 "%s: m_adj(m, -mr)", __func__);
2838 m_adj(m, -((int)sizeof(
2839 struct mldv2_record)));
2843 npbytes += (rsrcs * sizeof(struct in6_addr));
2844 if (crt == REC_ALLOW)
2845 pmr->mr_type = MLD_ALLOW_NEW_SOURCES;
2846 else if (crt == REC_BLOCK)
2847 pmr->mr_type = MLD_BLOCK_OLD_SOURCES;
2848 pmr->mr_numsrc = htons(rsrcs);
2850 * Count the new group record, and enqueue this
2851 * packet if it wasn't already queued.
2853 m->m_pkthdr.PH_vt.vt_nrecs++;
2855 mbufq_enqueue(mq, m);
2857 } while (nims != NULL);
2859 crt = (~crt & REC_FULL);
2862 CTR3(KTR_MLD, "%s: queued %d ALLOW_NEW, %d BLOCK_OLD", __func__,
2869 mld_v2_merge_state_changes(struct in6_multi *inm, struct mbufq *scq)
2872 struct mbuf *m; /* pending state-change */
2873 struct mbuf *m0; /* copy of pending state-change */
2874 struct mbuf *mt; /* last state-change in packet */
2875 int docopy, domerge;
2882 IN6_MULTI_LOCK_ASSERT();
2886 * If there are further pending retransmissions, make a writable
2887 * copy of each queued state-change message before merging.
2889 if (inm->in6m_scrv > 0)
2892 gq = &inm->in6m_scq;
2894 if (mbufq_first(gq) == NULL) {
2895 CTR2(KTR_MLD, "%s: WARNING: queue for inm %p is empty",
2900 m = mbufq_first(gq);
2903 * Only merge the report into the current packet if
2904 * there is sufficient space to do so; an MLDv2 report
2905 * packet may only contain 65,535 group records.
2906 * Always use a simple mbuf chain concatentation to do this,
2907 * as large state changes for single groups may have
2908 * allocated clusters.
2911 mt = mbufq_last(scq);
2913 recslen = m_length(m, NULL);
2915 if ((mt->m_pkthdr.PH_vt.vt_nrecs +
2916 m->m_pkthdr.PH_vt.vt_nrecs <=
2917 MLD_V2_REPORT_MAXRECS) &&
2918 (mt->m_pkthdr.len + recslen <=
2919 (inm->in6m_ifp->if_mtu - MLD_MTUSPACE)))
2923 if (!domerge && mbufq_full(gq)) {
2925 "%s: outbound queue full, skipping whole packet %p",
2935 CTR2(KTR_MLD, "%s: dequeueing %p", __func__, m);
2936 m0 = mbufq_dequeue(gq);
2939 CTR2(KTR_MLD, "%s: copying %p", __func__, m);
2940 m0 = m_dup(m, M_NOWAIT);
2943 m0->m_nextpkt = NULL;
2948 CTR3(KTR_MLD, "%s: queueing %p to scq %p)",
2950 mbufq_enqueue(scq, m0);
2952 struct mbuf *mtl; /* last mbuf of packet mt */
2954 CTR3(KTR_MLD, "%s: merging %p with ifscq tail %p)",
2958 m0->m_flags &= ~M_PKTHDR;
2959 mt->m_pkthdr.len += recslen;
2960 mt->m_pkthdr.PH_vt.vt_nrecs +=
2961 m0->m_pkthdr.PH_vt.vt_nrecs;
2971 * Respond to a pending MLDv2 General Query.
2974 mld_v2_dispatch_general_query(struct mld_ifsoftc *mli)
2976 struct ifmultiaddr *ifma;
2978 struct in6_multi *inm;
2981 IN6_MULTI_LOCK_ASSERT();
2984 KASSERT(mli->mli_version == MLD_VERSION_2,
2985 ("%s: called when version %d", __func__, mli->mli_version));
2988 * Check that there are some packets queued. If so, send them first.
2989 * For large number of groups the reply to general query can take
2990 * many packets, we should finish sending them before starting of
2991 * queuing the new reply.
2993 if (mbufq_len(&mli->mli_gq) != 0)
2999 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
3000 if (ifma->ifma_addr->sa_family != AF_INET6 ||
3001 ifma->ifma_protospec == NULL)
3004 inm = (struct in6_multi *)ifma->ifma_protospec;
3005 KASSERT(ifp == inm->in6m_ifp,
3006 ("%s: inconsistent ifp", __func__));
3008 switch (inm->in6m_state) {
3009 case MLD_NOT_MEMBER:
3010 case MLD_SILENT_MEMBER:
3012 case MLD_REPORTING_MEMBER:
3013 case MLD_IDLE_MEMBER:
3014 case MLD_LAZY_MEMBER:
3015 case MLD_SLEEPING_MEMBER:
3016 case MLD_AWAKENING_MEMBER:
3017 inm->in6m_state = MLD_REPORTING_MEMBER;
3018 retval = mld_v2_enqueue_group_record(&mli->mli_gq,
3020 CTR2(KTR_MLD, "%s: enqueue record = %d",
3023 case MLD_G_QUERY_PENDING_MEMBER:
3024 case MLD_SG_QUERY_PENDING_MEMBER:
3025 case MLD_LEAVING_MEMBER:
3029 IF_ADDR_RUNLOCK(ifp);
3032 mld_dispatch_queue(&mli->mli_gq, MLD_MAX_RESPONSE_BURST);
3035 * Slew transmission of bursts over 500ms intervals.
3037 if (mbufq_first(&mli->mli_gq) != NULL) {
3038 mli->mli_v2_timer = 1 + MLD_RANDOM_DELAY(
3039 MLD_RESPONSE_BURST_INTERVAL);
3040 V_interface_timers_running6 = 1;
3045 * Transmit the next pending message in the output queue.
3047 * VIMAGE: Needs to store/restore vnet pointer on a per-mbuf-chain basis.
3048 * MRT: Nothing needs to be done, as MLD traffic is always local to
3049 * a link and uses a link-scope multicast address.
3052 mld_dispatch_packet(struct mbuf *m)
3054 struct ip6_moptions im6o;
3059 struct ip6_hdr *ip6;
3060 struct mld_hdr *mld;
3066 CTR2(KTR_MLD, "%s: transmit %p", __func__, m);
3069 * Set VNET image pointer from enqueued mbuf chain
3070 * before doing anything else. Whilst we use interface
3071 * indexes to guard against interface detach, they are
3072 * unique to each VIMAGE and must be retrieved.
3074 ifindex = mld_restore_context(m);
3077 * Check if the ifnet still exists. This limits the scope of
3078 * any race in the absence of a global ifp lock for low cost
3079 * (an array lookup).
3081 ifp = ifnet_byindex(ifindex);
3083 CTR3(KTR_MLD, "%s: dropped %p as ifindex %u went away.",
3084 __func__, m, ifindex);
3086 IP6STAT_INC(ip6s_noroute);
3090 im6o.im6o_multicast_hlim = 1;
3091 im6o.im6o_multicast_loop = (V_ip6_mrouter != NULL);
3092 im6o.im6o_multicast_ifp = ifp;
3094 if (m->m_flags & M_MLDV1) {
3097 m0 = mld_v2_encap_report(ifp, m);
3099 CTR2(KTR_MLD, "%s: dropped %p", __func__, m);
3100 IP6STAT_INC(ip6s_odropped);
3105 mld_scrub_context(m0);
3107 m0->m_pkthdr.rcvif = V_loif;
3109 ip6 = mtod(m0, struct ip6_hdr *);
3111 (void)in6_setscope(&ip6->ip6_dst, ifp, NULL); /* XXX LOR */
3114 * XXX XXX Break some KPI rules to prevent an LOR which would
3115 * occur if we called in6_setscope() at transmission.
3116 * See comments at top of file.
3118 MLD_EMBEDSCOPE(&ip6->ip6_dst, ifp->if_index);
3122 * Retrieve the ICMPv6 type before handoff to ip6_output(),
3123 * so we can bump the stats.
3125 md = m_getptr(m0, sizeof(struct ip6_hdr), &off);
3126 mld = (struct mld_hdr *)(mtod(md, uint8_t *) + off);
3127 type = mld->mld_type;
3129 error = ip6_output(m0, &mld_po, NULL, IPV6_UNSPECSRC, &im6o,
3132 CTR3(KTR_MLD, "%s: ip6_output(%p) = %d", __func__, m0, error);
3135 ICMP6STAT_INC(icp6s_outhist[type]);
3137 icmp6_ifstat_inc(oifp, ifs6_out_msg);
3139 case MLD_LISTENER_REPORT:
3140 case MLDV2_LISTENER_REPORT:
3141 icmp6_ifstat_inc(oifp, ifs6_out_mldreport);
3143 case MLD_LISTENER_DONE:
3144 icmp6_ifstat_inc(oifp, ifs6_out_mlddone);
3153 * Encapsulate an MLDv2 report.
3155 * KAME IPv6 requires that hop-by-hop options be passed separately,
3156 * and that the IPv6 header be prepended in a separate mbuf.
3158 * Returns a pointer to the new mbuf chain head, or NULL if the
3159 * allocation failed.
3161 static struct mbuf *
3162 mld_v2_encap_report(struct ifnet *ifp, struct mbuf *m)
3165 struct mldv2_report *mld;
3166 struct ip6_hdr *ip6;
3167 struct in6_ifaddr *ia;
3170 KASSERT(ifp != NULL, ("%s: null ifp", __func__));
3171 KASSERT((m->m_flags & M_PKTHDR),
3172 ("%s: mbuf chain %p is !M_PKTHDR", __func__, m));
3175 * RFC3590: OK to send as :: or tentative during DAD.
3177 ia = in6ifa_ifpforlinklocal(ifp, IN6_IFF_NOTREADY|IN6_IFF_ANYCAST);
3179 CTR1(KTR_MLD, "%s: warning: ia is NULL", __func__);
3181 mh = m_gethdr(M_NOWAIT, MT_DATA);
3184 ifa_free(&ia->ia_ifa);
3188 M_ALIGN(mh, sizeof(struct ip6_hdr) + sizeof(struct mldv2_report));
3190 mldreclen = m_length(m, NULL);
3191 CTR2(KTR_MLD, "%s: mldreclen is %d", __func__, mldreclen);
3193 mh->m_len = sizeof(struct ip6_hdr) + sizeof(struct mldv2_report);
3194 mh->m_pkthdr.len = sizeof(struct ip6_hdr) +
3195 sizeof(struct mldv2_report) + mldreclen;
3197 ip6 = mtod(mh, struct ip6_hdr *);
3199 ip6->ip6_vfc &= ~IPV6_VERSION_MASK;
3200 ip6->ip6_vfc |= IPV6_VERSION;
3201 ip6->ip6_nxt = IPPROTO_ICMPV6;
3202 ip6->ip6_src = ia ? ia->ia_addr.sin6_addr : in6addr_any;
3204 ifa_free(&ia->ia_ifa);
3205 ip6->ip6_dst = in6addr_linklocal_allv2routers;
3206 /* scope ID will be set in netisr */
3208 mld = (struct mldv2_report *)(ip6 + 1);
3209 mld->mld_type = MLDV2_LISTENER_REPORT;
3212 mld->mld_v2_reserved = 0;
3213 mld->mld_v2_numrecs = htons(m->m_pkthdr.PH_vt.vt_nrecs);
3214 m->m_pkthdr.PH_vt.vt_nrecs = 0;
3217 mld->mld_cksum = in6_cksum(mh, IPPROTO_ICMPV6,
3218 sizeof(struct ip6_hdr), sizeof(struct mldv2_report) + mldreclen);
3224 mld_rec_type_to_str(const int type)
3228 case MLD_CHANGE_TO_EXCLUDE_MODE:
3231 case MLD_CHANGE_TO_INCLUDE_MODE:
3234 case MLD_MODE_IS_EXCLUDE:
3237 case MLD_MODE_IS_INCLUDE:
3240 case MLD_ALLOW_NEW_SOURCES:
3243 case MLD_BLOCK_OLD_SOURCES:
3254 mld_init(void *unused __unused)
3257 CTR1(KTR_MLD, "%s: initializing", __func__);
3260 ip6_initpktopts(&mld_po);
3261 mld_po.ip6po_hlim = 1;
3262 mld_po.ip6po_hbh = &mld_ra.hbh;
3263 mld_po.ip6po_prefer_tempaddr = IP6PO_TEMPADDR_NOTPREFER;
3264 mld_po.ip6po_flags = IP6PO_DONTFRAG;
3266 SYSINIT(mld_init, SI_SUB_PROTO_MC, SI_ORDER_MIDDLE, mld_init, NULL);
3269 mld_uninit(void *unused __unused)
3272 CTR1(KTR_MLD, "%s: tearing down", __func__);
3275 SYSUNINIT(mld_uninit, SI_SUB_PROTO_MC, SI_ORDER_MIDDLE, mld_uninit, NULL);
3278 vnet_mld_init(const void *unused __unused)
3281 CTR1(KTR_MLD, "%s: initializing", __func__);
3283 LIST_INIT(&V_mli_head);
3285 VNET_SYSINIT(vnet_mld_init, SI_SUB_PROTO_MC, SI_ORDER_ANY, vnet_mld_init,
3289 vnet_mld_uninit(const void *unused __unused)
3292 /* This can happen if we shutdown the network stack. */
3293 CTR1(KTR_MLD, "%s: tearing down", __func__);
3295 VNET_SYSUNINIT(vnet_mld_uninit, SI_SUB_PROTO_MC, SI_ORDER_ANY, vnet_mld_uninit,
3299 mld_modevent(module_t mod, int type, void *unused __unused)
3307 return (EOPNOTSUPP);
3312 static moduledata_t mld_mod = {
3317 DECLARE_MODULE(mld, mld_mod, SI_SUB_PROTO_MC, SI_ORDER_ANY);