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(struct in6_multi_head *inmh);
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 in6_multi_head *,
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 in6_multi_head *,
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 VNET_DEFINE_STATIC(struct timeval, mld_gsrdelay) = {10, 0};
211 VNET_DEFINE_STATIC(LIST_HEAD(, mld_ifsoftc), mli_head);
212 VNET_DEFINE_STATIC(int, interface_timers_running6);
213 VNET_DEFINE_STATIC(int, state_change_timers_running6);
214 VNET_DEFINE_STATIC(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_v2enable = 1;
247 SYSCTL_INT(_net_inet6_mld, OID_AUTO, v2enable, CTLFLAG_RWTUN,
248 &mld_v2enable, 0, "Enable MLDv2");
250 static int mld_use_allow = 1;
251 SYSCTL_INT(_net_inet6_mld, OID_AUTO, use_allow, CTLFLAG_RWTUN,
252 &mld_use_allow, 0, "Use ALLOW/BLOCK for RFC 4604 SSM joins/leaves");
255 * Packed Router Alert option structure declaration.
260 struct ip6_opt_router ra;
264 * Router Alert hop-by-hop option header.
266 static struct mld_raopt mld_ra = {
268 .pad = { .ip6o_type = IP6OPT_PADN, 0 },
270 .ip6or_type = IP6OPT_ROUTER_ALERT,
271 .ip6or_len = IP6OPT_RTALERT_LEN - 2,
272 .ip6or_value[0] = ((IP6OPT_RTALERT_MLD >> 8) & 0xFF),
273 .ip6or_value[1] = (IP6OPT_RTALERT_MLD & 0xFF)
276 static struct ip6_pktopts mld_po;
279 mld_save_context(struct mbuf *m, struct ifnet *ifp)
283 m->m_pkthdr.PH_loc.ptr = ifp->if_vnet;
285 m->m_pkthdr.flowid = ifp->if_index;
289 mld_scrub_context(struct mbuf *m)
292 m->m_pkthdr.PH_loc.ptr = NULL;
293 m->m_pkthdr.flowid = 0;
297 * Restore context from a queued output chain.
298 * Return saved ifindex.
300 * VIMAGE: The assertion is there to make sure that we
301 * actually called CURVNET_SET() with what's in the mbuf chain.
303 static __inline uint32_t
304 mld_restore_context(struct mbuf *m)
307 #if defined(VIMAGE) && defined(INVARIANTS)
308 KASSERT(curvnet == m->m_pkthdr.PH_loc.ptr,
309 ("%s: called when curvnet was not restored: cuvnet %p m ptr %p",
310 __func__, curvnet, m->m_pkthdr.PH_loc.ptr));
312 return (m->m_pkthdr.flowid);
316 * Retrieve or set threshold between group-source queries in seconds.
318 * VIMAGE: Assume curvnet set by caller.
319 * SMPng: NOTE: Serialized by MLD lock.
322 sysctl_mld_gsr(SYSCTL_HANDLER_ARGS)
327 error = sysctl_wire_old_buffer(req, sizeof(int));
333 i = V_mld_gsrdelay.tv_sec;
335 error = sysctl_handle_int(oidp, &i, 0, req);
336 if (error || !req->newptr)
339 if (i < -1 || i >= 60) {
344 CTR2(KTR_MLD, "change mld_gsrdelay from %d to %d",
345 V_mld_gsrdelay.tv_sec, i);
346 V_mld_gsrdelay.tv_sec = i;
354 * Expose struct mld_ifsoftc to userland, keyed by ifindex.
355 * For use by ifmcstat(8).
357 * SMPng: NOTE: Does an unlocked ifindex space read.
358 * VIMAGE: Assume curvnet set by caller. The node handler itself
359 * is not directly virtualized.
362 sysctl_mld_ifinfo(SYSCTL_HANDLER_ARGS)
368 struct mld_ifsoftc *mli;
373 if (req->newptr != NULL)
379 error = sysctl_wire_old_buffer(req, sizeof(struct mld_ifinfo));
384 IN6_MULTI_LIST_LOCK();
387 if (name[0] <= 0 || name[0] > V_if_index) {
394 ifp = ifnet_byindex(name[0]);
398 LIST_FOREACH(mli, &V_mli_head, mli_link) {
399 if (ifp == mli->mli_ifp) {
400 struct mld_ifinfo info;
402 info.mli_version = mli->mli_version;
403 info.mli_v1_timer = mli->mli_v1_timer;
404 info.mli_v2_timer = mli->mli_v2_timer;
405 info.mli_flags = mli->mli_flags;
406 info.mli_rv = mli->mli_rv;
407 info.mli_qi = mli->mli_qi;
408 info.mli_qri = mli->mli_qri;
409 info.mli_uri = mli->mli_uri;
410 error = SYSCTL_OUT(req, &info, sizeof(info));
417 IN6_MULTI_LIST_UNLOCK();
423 * Dispatch an entire queue of pending packet chains.
424 * VIMAGE: Assumes the vnet pointer has been set.
427 mld_dispatch_queue(struct mbufq *mq, int limit)
431 while ((m = mbufq_dequeue(mq)) != NULL) {
432 CTR3(KTR_MLD, "%s: dispatch %p from %p", __func__, mq, m);
433 mld_dispatch_packet(m);
440 * Filter outgoing MLD report state by group.
442 * Reports are ALWAYS suppressed for ALL-HOSTS (ff02::1)
443 * and node-local addresses. However, kernel and socket consumers
444 * always embed the KAME scope ID in the address provided, so strip it
445 * when performing comparison.
446 * Note: This is not the same as the *multicast* scope.
448 * Return zero if the given group is one for which MLD reports
449 * should be suppressed, or non-zero if reports should be issued.
452 mld_is_addr_reported(const struct in6_addr *addr)
455 KASSERT(IN6_IS_ADDR_MULTICAST(addr), ("%s: not multicast", __func__));
457 if (IPV6_ADDR_MC_SCOPE(addr) == IPV6_ADDR_SCOPE_NODELOCAL)
460 if (IPV6_ADDR_MC_SCOPE(addr) == IPV6_ADDR_SCOPE_LINKLOCAL) {
461 struct in6_addr tmp = *addr;
462 in6_clearscope(&tmp);
463 if (IN6_ARE_ADDR_EQUAL(&tmp, &in6addr_linklocal_allnodes))
471 * Attach MLD when PF_INET6 is attached to an interface.
473 * SMPng: Normally called with IF_AFDATA_LOCK held.
476 mld_domifattach(struct ifnet *ifp)
478 struct mld_ifsoftc *mli;
480 CTR3(KTR_MLD, "%s: called for ifp %p(%s)",
481 __func__, ifp, if_name(ifp));
485 mli = mli_alloc_locked(ifp);
486 if (!(ifp->if_flags & IFF_MULTICAST))
487 mli->mli_flags |= MLIF_SILENT;
489 mli->mli_flags |= MLIF_USEALLOW;
497 * VIMAGE: assume curvnet set by caller.
499 static struct mld_ifsoftc *
500 mli_alloc_locked(/*const*/ struct ifnet *ifp)
502 struct mld_ifsoftc *mli;
506 mli = malloc(sizeof(struct mld_ifsoftc), M_MLD, M_NOWAIT|M_ZERO);
511 mli->mli_version = MLD_VERSION_2;
513 mli->mli_rv = MLD_RV_INIT;
514 mli->mli_qi = MLD_QI_INIT;
515 mli->mli_qri = MLD_QRI_INIT;
516 mli->mli_uri = MLD_URI_INIT;
517 mbufq_init(&mli->mli_gq, MLD_MAX_RESPONSE_PACKETS);
519 LIST_INSERT_HEAD(&V_mli_head, mli, mli_link);
521 CTR2(KTR_MLD, "allocate mld_ifsoftc for ifp %p(%s)",
531 * NOTE: Some finalization tasks need to run before the protocol domain
532 * is detached, but also before the link layer does its cleanup.
533 * Run before link-layer cleanup; cleanup groups, but do not free MLD state.
535 * SMPng: Caller must hold IN6_MULTI_LOCK().
536 * Must take IF_ADDR_LOCK() to cover if_multiaddrs iterator.
537 * XXX This routine is also bitten by unlocked ifma_protospec access.
540 mld_ifdetach(struct ifnet *ifp, struct in6_multi_head *inmh)
542 struct epoch_tracker et;
543 struct mld_ifsoftc *mli;
544 struct ifmultiaddr *ifma;
545 struct in6_multi *inm;
547 CTR3(KTR_MLD, "%s: called for ifp %p(%s)", __func__, ifp,
550 IN6_MULTI_LIST_LOCK_ASSERT();
553 mli = MLD_IFINFO(ifp);
556 * Extract list of in6_multi associated with the detaching ifp
557 * which the PF_INET6 layer is about to release.
560 CK_STAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
561 inm = in6m_ifmultiaddr_get_inm(ifma);
564 in6m_disconnect_locked(inmh, inm);
566 if (mli->mli_version == MLD_VERSION_2) {
567 in6m_clear_recorded(inm);
570 * We need to release the final reference held
571 * for issuing the INCLUDE {}.
573 if (inm->in6m_state == MLD_LEAVING_MEMBER) {
574 inm->in6m_state = MLD_NOT_MEMBER;
575 in6m_rele_locked(inmh, inm);
580 IF_ADDR_WUNLOCK(ifp);
585 * Hook for domifdetach.
586 * Runs after link-layer cleanup; free MLD state.
588 * SMPng: Normally called with IF_AFDATA_LOCK held.
591 mld_domifdetach(struct ifnet *ifp)
594 CTR3(KTR_MLD, "%s: called for ifp %p(%s)",
595 __func__, ifp, if_name(ifp));
598 mli_delete_locked(ifp);
603 mli_delete_locked(const struct ifnet *ifp)
605 struct mld_ifsoftc *mli, *tmli;
607 CTR3(KTR_MLD, "%s: freeing mld_ifsoftc for ifp %p(%s)",
608 __func__, ifp, if_name(ifp));
612 LIST_FOREACH_SAFE(mli, &V_mli_head, mli_link, tmli) {
613 if (mli->mli_ifp == ifp) {
615 * Free deferred General Query responses.
617 mbufq_drain(&mli->mli_gq);
619 LIST_REMOVE(mli, mli_link);
628 * Process a received MLDv1 general or address-specific query.
629 * Assumes that the query header has been pulled up to sizeof(mld_hdr).
631 * NOTE: Can't be fully const correct as we temporarily embed scope ID in
632 * mld_addr. This is OK as we own the mbuf chain.
635 mld_v1_input_query(struct ifnet *ifp, const struct ip6_hdr *ip6,
636 /*const*/ struct mld_hdr *mld)
638 struct epoch_tracker et;
639 struct ifmultiaddr *ifma;
640 struct mld_ifsoftc *mli;
641 struct in6_multi *inm;
642 int is_general_query;
645 char ip6tbuf[INET6_ADDRSTRLEN];
648 is_general_query = 0;
651 CTR3(KTR_MLD, "ignore v1 query %s on ifp %p(%s)",
652 ip6_sprintf(ip6tbuf, &mld->mld_addr),
658 * RFC3810 Section 6.2: MLD queries must originate from
659 * a router's link-local address.
661 if (!IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_src)) {
662 CTR3(KTR_MLD, "ignore v1 query src %s on ifp %p(%s)",
663 ip6_sprintf(ip6tbuf, &ip6->ip6_src),
669 * Do address field validation upfront before we accept
672 if (IN6_IS_ADDR_UNSPECIFIED(&mld->mld_addr)) {
674 * MLDv1 General Query.
675 * If this was not sent to the all-nodes group, ignore it.
680 in6_clearscope(&dst);
681 if (!IN6_ARE_ADDR_EQUAL(&dst, &in6addr_linklocal_allnodes))
683 is_general_query = 1;
686 * Embed scope ID of receiving interface in MLD query for
687 * lookup whilst we don't hold other locks.
689 in6_setscope(&mld->mld_addr, ifp, NULL);
692 IN6_MULTI_LIST_LOCK();
696 * Switch to MLDv1 host compatibility mode.
698 mli = MLD_IFINFO(ifp);
699 KASSERT(mli != NULL, ("%s: no mld_ifsoftc for ifp %p", __func__, ifp));
700 mld_set_version(mli, MLD_VERSION_1);
702 timer = (ntohs(mld->mld_maxdelay) * PR_FASTHZ) / MLD_TIMER_SCALE;
707 if (is_general_query) {
709 * For each reporting group joined on this
710 * interface, kick the report timer.
712 CTR2(KTR_MLD, "process v1 general query on ifp %p(%s)",
714 CK_STAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
715 inm = in6m_ifmultiaddr_get_inm(ifma);
718 mld_v1_update_group(inm, timer);
722 * MLDv1 Group-Specific Query.
723 * If this is a group-specific MLDv1 query, we need only
724 * look up the single group to process it.
726 inm = in6m_lookup_locked(ifp, &mld->mld_addr);
728 CTR3(KTR_MLD, "process v1 query %s on ifp %p(%s)",
729 ip6_sprintf(ip6tbuf, &mld->mld_addr),
731 mld_v1_update_group(inm, timer);
733 /* XXX Clear embedded scope ID as userland won't expect it. */
734 in6_clearscope(&mld->mld_addr);
739 IN6_MULTI_LIST_UNLOCK();
745 * Update the report timer on a group in response to an MLDv1 query.
747 * If we are becoming the reporting member for this group, start the timer.
748 * If we already are the reporting member for this group, and timer is
749 * below the threshold, reset it.
751 * We may be updating the group for the first time since we switched
752 * to MLDv2. If we are, then we must clear any recorded source lists,
753 * and transition to REPORTING state; the group timer is overloaded
754 * for group and group-source query responses.
756 * Unlike MLDv2, the delay per group should be jittered
757 * to avoid bursts of MLDv1 reports.
760 mld_v1_update_group(struct in6_multi *inm, const int timer)
763 char ip6tbuf[INET6_ADDRSTRLEN];
766 CTR4(KTR_MLD, "%s: %s/%s timer=%d", __func__,
767 ip6_sprintf(ip6tbuf, &inm->in6m_addr),
768 if_name(inm->in6m_ifp), timer);
770 IN6_MULTI_LIST_LOCK_ASSERT();
772 switch (inm->in6m_state) {
774 case MLD_SILENT_MEMBER:
776 case MLD_REPORTING_MEMBER:
777 if (inm->in6m_timer != 0 &&
778 inm->in6m_timer <= timer) {
779 CTR1(KTR_MLD, "%s: REPORTING and timer running, "
780 "skipping.", __func__);
784 case MLD_SG_QUERY_PENDING_MEMBER:
785 case MLD_G_QUERY_PENDING_MEMBER:
786 case MLD_IDLE_MEMBER:
787 case MLD_LAZY_MEMBER:
788 case MLD_AWAKENING_MEMBER:
789 CTR1(KTR_MLD, "%s: ->REPORTING", __func__);
790 inm->in6m_state = MLD_REPORTING_MEMBER;
791 inm->in6m_timer = MLD_RANDOM_DELAY(timer);
792 V_current_state_timers_running6 = 1;
794 case MLD_SLEEPING_MEMBER:
795 CTR1(KTR_MLD, "%s: ->AWAKENING", __func__);
796 inm->in6m_state = MLD_AWAKENING_MEMBER;
798 case MLD_LEAVING_MEMBER:
804 * Process a received MLDv2 general, group-specific or
805 * group-and-source-specific query.
807 * Assumes that the query header has been pulled up to sizeof(mldv2_query).
809 * Return 0 if successful, otherwise an appropriate error code is returned.
812 mld_v2_input_query(struct ifnet *ifp, const struct ip6_hdr *ip6,
813 struct mbuf *m, const int off, const int icmp6len)
815 struct mld_ifsoftc *mli;
816 struct mldv2_query *mld;
817 struct in6_multi *inm;
818 uint32_t maxdelay, nsrc, qqi;
819 int is_general_query;
823 char ip6tbuf[INET6_ADDRSTRLEN];
827 CTR3(KTR_MLD, "ignore v2 query src %s on ifp %p(%s)",
828 ip6_sprintf(ip6tbuf, &ip6->ip6_src),
834 * RFC3810 Section 6.2: MLD queries must originate from
835 * a router's link-local address.
837 if (!IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_src)) {
838 CTR3(KTR_MLD, "ignore v1 query src %s on ifp %p(%s)",
839 ip6_sprintf(ip6tbuf, &ip6->ip6_src),
844 is_general_query = 0;
846 CTR2(KTR_MLD, "input v2 query on ifp %p(%s)", ifp, if_name(ifp));
848 mld = (struct mldv2_query *)(mtod(m, uint8_t *) + off);
850 maxdelay = ntohs(mld->mld_maxdelay); /* in 1/10ths of a second */
851 if (maxdelay >= 32768) {
852 maxdelay = (MLD_MRC_MANT(maxdelay) | 0x1000) <<
853 (MLD_MRC_EXP(maxdelay) + 3);
855 timer = (maxdelay * PR_FASTHZ) / MLD_TIMER_SCALE;
859 qrv = MLD_QRV(mld->mld_misc);
861 CTR3(KTR_MLD, "%s: clamping qrv %d to %d", __func__,
868 qqi = MLD_QQIC_MANT(mld->mld_qqi) <<
869 (MLD_QQIC_EXP(mld->mld_qqi) + 3);
872 nsrc = ntohs(mld->mld_numsrc);
873 if (nsrc > MLD_MAX_GS_SOURCES)
875 if (icmp6len < sizeof(struct mldv2_query) +
876 (nsrc * sizeof(struct in6_addr)))
880 * Do further input validation upfront to avoid resetting timers
881 * should we need to discard this query.
883 if (IN6_IS_ADDR_UNSPECIFIED(&mld->mld_addr)) {
885 * A general query with a source list has undefined
886 * behaviour; discard it.
890 is_general_query = 1;
893 * Embed scope ID of receiving interface in MLD query for
894 * lookup whilst we don't hold other locks (due to KAME
895 * locking lameness). We own this mbuf chain just now.
897 in6_setscope(&mld->mld_addr, ifp, NULL);
900 IN6_MULTI_LIST_LOCK();
903 mli = MLD_IFINFO(ifp);
904 KASSERT(mli != NULL, ("%s: no mld_ifsoftc for ifp %p", __func__, ifp));
907 * Discard the v2 query if we're in Compatibility Mode.
908 * The RFC is pretty clear that hosts need to stay in MLDv1 mode
909 * until the Old Version Querier Present timer expires.
911 if (mli->mli_version != MLD_VERSION_2)
914 mld_set_version(mli, MLD_VERSION_2);
917 mli->mli_qri = maxdelay;
919 CTR4(KTR_MLD, "%s: qrv %d qi %d maxdelay %d", __func__, qrv, qqi,
922 if (is_general_query) {
924 * MLDv2 General Query.
926 * Schedule a current-state report on this ifp for
927 * all groups, possibly containing source lists.
929 * If there is a pending General Query response
930 * scheduled earlier than the selected delay, do
931 * not schedule any other reports.
932 * Otherwise, reset the interface timer.
934 CTR2(KTR_MLD, "process v2 general query on ifp %p(%s)",
936 if (mli->mli_v2_timer == 0 || mli->mli_v2_timer >= timer) {
937 mli->mli_v2_timer = MLD_RANDOM_DELAY(timer);
938 V_interface_timers_running6 = 1;
941 struct epoch_tracker et;
944 * MLDv2 Group-specific or Group-and-source-specific Query.
946 * Group-source-specific queries are throttled on
947 * a per-group basis to defeat denial-of-service attempts.
948 * Queries for groups we are not a member of on this
949 * link are simply ignored.
952 inm = in6m_lookup_locked(ifp, &mld->mld_addr);
958 if (!ratecheck(&inm->in6m_lastgsrtv,
960 CTR1(KTR_MLD, "%s: GS query throttled.",
966 CTR2(KTR_MLD, "process v2 group query on ifp %p(%s)",
969 * If there is a pending General Query response
970 * scheduled sooner than the selected delay, no
971 * further report need be scheduled.
972 * Otherwise, prepare to respond to the
973 * group-specific or group-and-source query.
975 if (mli->mli_v2_timer == 0 || mli->mli_v2_timer >= timer)
976 mld_v2_process_group_query(inm, mli, timer, m, off);
978 /* XXX Clear embedded scope ID as userland won't expect it. */
979 in6_clearscope(&mld->mld_addr);
985 IN6_MULTI_LIST_UNLOCK();
991 * Process a received MLDv2 group-specific or group-and-source-specific
993 * Return <0 if any error occurred. Currently this is ignored.
996 mld_v2_process_group_query(struct in6_multi *inm, struct mld_ifsoftc *mli,
997 int timer, struct mbuf *m0, const int off)
999 struct mldv2_query *mld;
1003 IN6_MULTI_LIST_LOCK_ASSERT();
1007 mld = (struct mldv2_query *)(mtod(m0, uint8_t *) + off);
1009 switch (inm->in6m_state) {
1010 case MLD_NOT_MEMBER:
1011 case MLD_SILENT_MEMBER:
1012 case MLD_SLEEPING_MEMBER:
1013 case MLD_LAZY_MEMBER:
1014 case MLD_AWAKENING_MEMBER:
1015 case MLD_IDLE_MEMBER:
1016 case MLD_LEAVING_MEMBER:
1019 case MLD_REPORTING_MEMBER:
1020 case MLD_G_QUERY_PENDING_MEMBER:
1021 case MLD_SG_QUERY_PENDING_MEMBER:
1025 nsrc = ntohs(mld->mld_numsrc);
1028 * Deal with group-specific queries upfront.
1029 * If any group query is already pending, purge any recorded
1030 * source-list state if it exists, and schedule a query response
1031 * for this group-specific query.
1034 if (inm->in6m_state == MLD_G_QUERY_PENDING_MEMBER ||
1035 inm->in6m_state == MLD_SG_QUERY_PENDING_MEMBER) {
1036 in6m_clear_recorded(inm);
1037 timer = min(inm->in6m_timer, timer);
1039 inm->in6m_state = MLD_G_QUERY_PENDING_MEMBER;
1040 inm->in6m_timer = MLD_RANDOM_DELAY(timer);
1041 V_current_state_timers_running6 = 1;
1046 * Deal with the case where a group-and-source-specific query has
1047 * been received but a group-specific query is already pending.
1049 if (inm->in6m_state == MLD_G_QUERY_PENDING_MEMBER) {
1050 timer = min(inm->in6m_timer, timer);
1051 inm->in6m_timer = MLD_RANDOM_DELAY(timer);
1052 V_current_state_timers_running6 = 1;
1057 * Finally, deal with the case where a group-and-source-specific
1058 * query has been received, where a response to a previous g-s-r
1059 * query exists, or none exists.
1060 * In this case, we need to parse the source-list which the Querier
1061 * has provided us with and check if we have any source list filter
1062 * entries at T1 for these sources. If we do not, there is no need
1063 * schedule a report and the query may be dropped.
1064 * If we do, we must record them and schedule a current-state
1065 * report for those sources.
1067 if (inm->in6m_nsrc > 0) {
1074 soff = off + sizeof(struct mldv2_query);
1076 for (i = 0; i < nsrc; i++) {
1077 sp = mtod(m, uint8_t *) + soff;
1078 retval = in6m_record_source(inm,
1079 (const struct in6_addr *)sp);
1082 nrecorded += retval;
1083 soff += sizeof(struct in6_addr);
1084 if (soff >= m->m_len) {
1085 soff = soff - m->m_len;
1091 if (nrecorded > 0) {
1093 "%s: schedule response to SG query", __func__);
1094 inm->in6m_state = MLD_SG_QUERY_PENDING_MEMBER;
1095 inm->in6m_timer = MLD_RANDOM_DELAY(timer);
1096 V_current_state_timers_running6 = 1;
1104 * Process a received MLDv1 host membership report.
1105 * Assumes mld points to mld_hdr in pulled up mbuf chain.
1107 * NOTE: Can't be fully const correct as we temporarily embed scope ID in
1108 * mld_addr. This is OK as we own the mbuf chain.
1111 mld_v1_input_report(struct ifnet *ifp, const struct ip6_hdr *ip6,
1112 /*const*/ struct mld_hdr *mld)
1114 struct in6_addr src, dst;
1115 struct epoch_tracker et;
1116 struct in6_ifaddr *ia;
1117 struct in6_multi *inm;
1119 char ip6tbuf[INET6_ADDRSTRLEN];
1122 if (!mld_v1enable) {
1123 CTR3(KTR_MLD, "ignore v1 report %s on ifp %p(%s)",
1124 ip6_sprintf(ip6tbuf, &mld->mld_addr),
1129 if (ifp->if_flags & IFF_LOOPBACK)
1133 * MLDv1 reports must originate from a host's link-local address,
1134 * or the unspecified address (when booting).
1137 in6_clearscope(&src);
1138 if (!IN6_IS_SCOPE_LINKLOCAL(&src) && !IN6_IS_ADDR_UNSPECIFIED(&src)) {
1139 CTR3(KTR_MLD, "ignore v1 query src %s on ifp %p(%s)",
1140 ip6_sprintf(ip6tbuf, &ip6->ip6_src),
1146 * RFC2710 Section 4: MLDv1 reports must pertain to a multicast
1147 * group, and must be directed to the group itself.
1150 in6_clearscope(&dst);
1151 if (!IN6_IS_ADDR_MULTICAST(&mld->mld_addr) ||
1152 !IN6_ARE_ADDR_EQUAL(&mld->mld_addr, &dst)) {
1153 CTR3(KTR_MLD, "ignore v1 query dst %s on ifp %p(%s)",
1154 ip6_sprintf(ip6tbuf, &ip6->ip6_dst),
1160 * Make sure we don't hear our own membership report, as fast
1161 * leave requires knowing that we are the only member of a
1162 * group. Assume we used the link-local address if available,
1163 * otherwise look for ::.
1165 * XXX Note that scope ID comparison is needed for the address
1166 * returned by in6ifa_ifpforlinklocal(), but SHOULD NOT be
1167 * performed for the on-wire address.
1169 ia = in6ifa_ifpforlinklocal(ifp, IN6_IFF_NOTREADY|IN6_IFF_ANYCAST);
1170 if ((ia && IN6_ARE_ADDR_EQUAL(&ip6->ip6_src, IA6_IN6(ia))) ||
1171 (ia == NULL && IN6_IS_ADDR_UNSPECIFIED(&src))) {
1173 ifa_free(&ia->ia_ifa);
1177 ifa_free(&ia->ia_ifa);
1179 CTR3(KTR_MLD, "process v1 report %s on ifp %p(%s)",
1180 ip6_sprintf(ip6tbuf, &mld->mld_addr), ifp, if_name(ifp));
1183 * Embed scope ID of receiving interface in MLD query for lookup
1184 * whilst we don't hold other locks (due to KAME locking lameness).
1186 if (!IN6_IS_ADDR_UNSPECIFIED(&mld->mld_addr))
1187 in6_setscope(&mld->mld_addr, ifp, NULL);
1189 IN6_MULTI_LIST_LOCK();
1191 NET_EPOCH_ENTER(et);
1194 * MLDv1 report suppression.
1195 * If we are a member of this group, and our membership should be
1196 * reported, and our group timer is pending or about to be reset,
1197 * stop our group timer by transitioning to the 'lazy' state.
1199 inm = in6m_lookup_locked(ifp, &mld->mld_addr);
1201 struct mld_ifsoftc *mli;
1203 mli = inm->in6m_mli;
1204 KASSERT(mli != NULL,
1205 ("%s: no mli for ifp %p", __func__, ifp));
1208 * If we are in MLDv2 host mode, do not allow the
1209 * other host's MLDv1 report to suppress our reports.
1211 if (mli->mli_version == MLD_VERSION_2)
1214 inm->in6m_timer = 0;
1216 switch (inm->in6m_state) {
1217 case MLD_NOT_MEMBER:
1218 case MLD_SILENT_MEMBER:
1219 case MLD_SLEEPING_MEMBER:
1221 case MLD_REPORTING_MEMBER:
1222 case MLD_IDLE_MEMBER:
1223 case MLD_AWAKENING_MEMBER:
1225 "report suppressed for %s on ifp %p(%s)",
1226 ip6_sprintf(ip6tbuf, &mld->mld_addr),
1228 case MLD_LAZY_MEMBER:
1229 inm->in6m_state = MLD_LAZY_MEMBER;
1231 case MLD_G_QUERY_PENDING_MEMBER:
1232 case MLD_SG_QUERY_PENDING_MEMBER:
1233 case MLD_LEAVING_MEMBER:
1241 IN6_MULTI_LIST_UNLOCK();
1243 /* XXX Clear embedded scope ID as userland won't expect it. */
1244 in6_clearscope(&mld->mld_addr);
1252 * Assume query messages which fit in a single ICMPv6 message header
1253 * have been pulled up.
1254 * Assume that userland will want to see the message, even if it
1255 * otherwise fails kernel input validation; do not free it.
1256 * Pullup may however free the mbuf chain m if it fails.
1258 * Return IPPROTO_DONE if we freed m. Otherwise, return 0.
1261 mld_input(struct mbuf *m, int off, int icmp6len)
1264 struct ip6_hdr *ip6;
1265 struct mld_hdr *mld;
1268 CTR3(KTR_MLD, "%s: called w/mbuf (%p,%d)", __func__, m, off);
1270 ifp = m->m_pkthdr.rcvif;
1272 ip6 = mtod(m, struct ip6_hdr *);
1274 /* Pullup to appropriate size. */
1275 mld = (struct mld_hdr *)(mtod(m, uint8_t *) + off);
1276 if (mld->mld_type == MLD_LISTENER_QUERY &&
1277 icmp6len >= sizeof(struct mldv2_query)) {
1278 mldlen = sizeof(struct mldv2_query);
1280 mldlen = sizeof(struct mld_hdr);
1282 IP6_EXTHDR_GET(mld, struct mld_hdr *, m, off, mldlen);
1284 ICMP6STAT_INC(icp6s_badlen);
1285 return (IPPROTO_DONE);
1289 * Userland needs to see all of this traffic for implementing
1290 * the endpoint discovery portion of multicast routing.
1292 switch (mld->mld_type) {
1293 case MLD_LISTENER_QUERY:
1294 icmp6_ifstat_inc(ifp, ifs6_in_mldquery);
1295 if (icmp6len == sizeof(struct mld_hdr)) {
1296 if (mld_v1_input_query(ifp, ip6, mld) != 0)
1298 } else if (icmp6len >= sizeof(struct mldv2_query)) {
1299 if (mld_v2_input_query(ifp, ip6, m, off,
1304 case MLD_LISTENER_REPORT:
1305 icmp6_ifstat_inc(ifp, ifs6_in_mldreport);
1306 if (mld_v1_input_report(ifp, ip6, mld) != 0)
1309 case MLDV2_LISTENER_REPORT:
1310 icmp6_ifstat_inc(ifp, ifs6_in_mldreport);
1312 case MLD_LISTENER_DONE:
1313 icmp6_ifstat_inc(ifp, ifs6_in_mlddone);
1323 * Fast timeout handler (global).
1324 * VIMAGE: Timeout handlers are expected to service all vimages.
1329 struct in6_multi_head inmh;
1330 VNET_ITERATOR_DECL(vnet_iter);
1334 VNET_LIST_RLOCK_NOSLEEP();
1335 VNET_FOREACH(vnet_iter) {
1336 CURVNET_SET(vnet_iter);
1337 mld_fasttimo_vnet(&inmh);
1340 VNET_LIST_RUNLOCK_NOSLEEP();
1341 in6m_release_list_deferred(&inmh);
1345 * Fast timeout handler (per-vnet).
1347 * VIMAGE: Assume caller has set up our curvnet.
1350 mld_fasttimo_vnet(struct in6_multi_head *inmh)
1352 struct epoch_tracker et;
1353 struct mbufq scq; /* State-change packets */
1354 struct mbufq qrq; /* Query response packets */
1356 struct mld_ifsoftc *mli;
1357 struct ifmultiaddr *ifma;
1358 struct in6_multi *inm;
1364 * Quick check to see if any work needs to be done, in order to
1365 * minimize the overhead of fasttimo processing.
1366 * SMPng: XXX Unlocked reads.
1368 if (!V_current_state_timers_running6 &&
1369 !V_interface_timers_running6 &&
1370 !V_state_change_timers_running6)
1373 IN6_MULTI_LIST_LOCK();
1377 * MLDv2 General Query response timer processing.
1379 if (V_interface_timers_running6) {
1380 CTR1(KTR_MLD, "%s: interface timers running", __func__);
1382 V_interface_timers_running6 = 0;
1383 LIST_FOREACH(mli, &V_mli_head, mli_link) {
1384 if (mli->mli_v2_timer == 0) {
1386 } else if (--mli->mli_v2_timer == 0) {
1387 mld_v2_dispatch_general_query(mli);
1389 V_interface_timers_running6 = 1;
1394 if (!V_current_state_timers_running6 &&
1395 !V_state_change_timers_running6)
1398 V_current_state_timers_running6 = 0;
1399 V_state_change_timers_running6 = 0;
1401 CTR1(KTR_MLD, "%s: state change timers running", __func__);
1404 * MLD host report and state-change timer processing.
1405 * Note: Processing a v2 group timer may remove a node.
1407 LIST_FOREACH(mli, &V_mli_head, mli_link) {
1410 if (mli->mli_version == MLD_VERSION_2) {
1411 uri_fasthz = MLD_RANDOM_DELAY(mli->mli_uri *
1413 mbufq_init(&qrq, MLD_MAX_G_GS_PACKETS);
1414 mbufq_init(&scq, MLD_MAX_STATE_CHANGE_PACKETS);
1418 NET_EPOCH_ENTER(et);
1419 CK_STAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
1420 inm = in6m_ifmultiaddr_get_inm(ifma);
1423 switch (mli->mli_version) {
1425 mld_v1_process_group_timer(inmh, inm);
1428 mld_v2_process_group_timers(inmh, &qrq,
1429 &scq, inm, uri_fasthz);
1433 IF_ADDR_WUNLOCK(ifp);
1435 switch (mli->mli_version) {
1438 * Transmit reports for this lifecycle. This
1439 * is done while not holding IF_ADDR_LOCK
1440 * since this can call
1441 * in6ifa_ifpforlinklocal() which locks
1442 * IF_ADDR_LOCK internally as well as
1443 * ip6_output() to transmit a packet.
1445 while ((inm = SLIST_FIRST(inmh)) != NULL) {
1446 SLIST_REMOVE_HEAD(inmh, in6m_defer);
1447 (void)mld_v1_transmit_report(inm,
1448 MLD_LISTENER_REPORT);
1452 mld_dispatch_queue(&qrq, 0);
1453 mld_dispatch_queue(&scq, 0);
1461 IN6_MULTI_LIST_UNLOCK();
1465 * Update host report group timer.
1466 * Will update the global pending timer flags.
1469 mld_v1_process_group_timer(struct in6_multi_head *inmh, struct in6_multi *inm)
1471 int report_timer_expired;
1473 IN6_MULTI_LIST_LOCK_ASSERT();
1476 if (inm->in6m_timer == 0) {
1477 report_timer_expired = 0;
1478 } else if (--inm->in6m_timer == 0) {
1479 report_timer_expired = 1;
1481 V_current_state_timers_running6 = 1;
1485 switch (inm->in6m_state) {
1486 case MLD_NOT_MEMBER:
1487 case MLD_SILENT_MEMBER:
1488 case MLD_IDLE_MEMBER:
1489 case MLD_LAZY_MEMBER:
1490 case MLD_SLEEPING_MEMBER:
1491 case MLD_AWAKENING_MEMBER:
1493 case MLD_REPORTING_MEMBER:
1494 if (report_timer_expired) {
1495 inm->in6m_state = MLD_IDLE_MEMBER;
1496 SLIST_INSERT_HEAD(inmh, inm, in6m_defer);
1499 case MLD_G_QUERY_PENDING_MEMBER:
1500 case MLD_SG_QUERY_PENDING_MEMBER:
1501 case MLD_LEAVING_MEMBER:
1507 * Update a group's timers for MLDv2.
1508 * Will update the global pending timer flags.
1509 * Note: Unlocked read from mli.
1512 mld_v2_process_group_timers(struct in6_multi_head *inmh,
1513 struct mbufq *qrq, struct mbufq *scq,
1514 struct in6_multi *inm, const int uri_fasthz)
1516 int query_response_timer_expired;
1517 int state_change_retransmit_timer_expired;
1519 char ip6tbuf[INET6_ADDRSTRLEN];
1522 IN6_MULTI_LIST_LOCK_ASSERT();
1525 query_response_timer_expired = 0;
1526 state_change_retransmit_timer_expired = 0;
1529 * During a transition from compatibility mode back to MLDv2,
1530 * a group record in REPORTING state may still have its group
1531 * timer active. This is a no-op in this function; it is easier
1532 * to deal with it here than to complicate the slow-timeout path.
1534 if (inm->in6m_timer == 0) {
1535 query_response_timer_expired = 0;
1536 } else if (--inm->in6m_timer == 0) {
1537 query_response_timer_expired = 1;
1539 V_current_state_timers_running6 = 1;
1542 if (inm->in6m_sctimer == 0) {
1543 state_change_retransmit_timer_expired = 0;
1544 } else if (--inm->in6m_sctimer == 0) {
1545 state_change_retransmit_timer_expired = 1;
1547 V_state_change_timers_running6 = 1;
1550 /* We are in fasttimo, so be quick about it. */
1551 if (!state_change_retransmit_timer_expired &&
1552 !query_response_timer_expired)
1555 switch (inm->in6m_state) {
1556 case MLD_NOT_MEMBER:
1557 case MLD_SILENT_MEMBER:
1558 case MLD_SLEEPING_MEMBER:
1559 case MLD_LAZY_MEMBER:
1560 case MLD_AWAKENING_MEMBER:
1561 case MLD_IDLE_MEMBER:
1563 case MLD_G_QUERY_PENDING_MEMBER:
1564 case MLD_SG_QUERY_PENDING_MEMBER:
1566 * Respond to a previously pending Group-Specific
1567 * or Group-and-Source-Specific query by enqueueing
1568 * the appropriate Current-State report for
1569 * immediate transmission.
1571 if (query_response_timer_expired) {
1574 retval = mld_v2_enqueue_group_record(qrq, inm, 0, 1,
1575 (inm->in6m_state == MLD_SG_QUERY_PENDING_MEMBER),
1577 CTR2(KTR_MLD, "%s: enqueue record = %d",
1579 inm->in6m_state = MLD_REPORTING_MEMBER;
1580 in6m_clear_recorded(inm);
1583 case MLD_REPORTING_MEMBER:
1584 case MLD_LEAVING_MEMBER:
1585 if (state_change_retransmit_timer_expired) {
1587 * State-change retransmission timer fired.
1588 * If there are any further pending retransmissions,
1589 * set the global pending state-change flag, and
1592 if (--inm->in6m_scrv > 0) {
1593 inm->in6m_sctimer = uri_fasthz;
1594 V_state_change_timers_running6 = 1;
1597 * Retransmit the previously computed state-change
1598 * report. If there are no further pending
1599 * retransmissions, the mbuf queue will be consumed.
1600 * Update T0 state to T1 as we have now sent
1603 (void)mld_v2_merge_state_changes(inm, scq);
1606 CTR3(KTR_MLD, "%s: T1 -> T0 for %s/%s", __func__,
1607 ip6_sprintf(ip6tbuf, &inm->in6m_addr),
1608 if_name(inm->in6m_ifp));
1611 * If we are leaving the group for good, make sure
1612 * we release MLD's reference to it.
1613 * This release must be deferred using a SLIST,
1614 * as we are called from a loop which traverses
1615 * the in_ifmultiaddr TAILQ.
1617 if (inm->in6m_state == MLD_LEAVING_MEMBER &&
1618 inm->in6m_scrv == 0) {
1619 inm->in6m_state = MLD_NOT_MEMBER;
1620 in6m_disconnect_locked(inmh, inm);
1621 in6m_rele_locked(inmh, inm);
1629 * Switch to a different version on the given interface,
1630 * as per Section 9.12.
1633 mld_set_version(struct mld_ifsoftc *mli, const int version)
1635 int old_version_timer;
1639 CTR4(KTR_MLD, "%s: switching to v%d on ifp %p(%s)", __func__,
1640 version, mli->mli_ifp, if_name(mli->mli_ifp));
1642 if (version == MLD_VERSION_1) {
1644 * Compute the "Older Version Querier Present" timer as per
1647 old_version_timer = (mli->mli_rv * mli->mli_qi) + mli->mli_qri;
1648 old_version_timer *= PR_SLOWHZ;
1649 mli->mli_v1_timer = old_version_timer;
1652 if (mli->mli_v1_timer > 0 && mli->mli_version != MLD_VERSION_1) {
1653 mli->mli_version = MLD_VERSION_1;
1654 mld_v2_cancel_link_timers(mli);
1659 * Cancel pending MLDv2 timers for the given link and all groups
1660 * joined on it; state-change, general-query, and group-query timers.
1663 mld_v2_cancel_link_timers(struct mld_ifsoftc *mli)
1665 struct epoch_tracker et;
1666 struct in6_multi_head inmh;
1667 struct ifmultiaddr *ifma;
1669 struct in6_multi *inm;
1671 CTR3(KTR_MLD, "%s: cancel v2 timers on ifp %p(%s)", __func__,
1672 mli->mli_ifp, if_name(mli->mli_ifp));
1675 IN6_MULTI_LIST_LOCK_ASSERT();
1679 * Fast-track this potentially expensive operation
1680 * by checking all the global 'timer pending' flags.
1682 if (!V_interface_timers_running6 &&
1683 !V_state_change_timers_running6 &&
1684 !V_current_state_timers_running6)
1687 mli->mli_v2_timer = 0;
1692 NET_EPOCH_ENTER(et);
1693 CK_STAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
1694 inm = in6m_ifmultiaddr_get_inm(ifma);
1697 switch (inm->in6m_state) {
1698 case MLD_NOT_MEMBER:
1699 case MLD_SILENT_MEMBER:
1700 case MLD_IDLE_MEMBER:
1701 case MLD_LAZY_MEMBER:
1702 case MLD_SLEEPING_MEMBER:
1703 case MLD_AWAKENING_MEMBER:
1705 case MLD_LEAVING_MEMBER:
1707 * If we are leaving the group and switching
1708 * version, we need to release the final
1709 * reference held for issuing the INCLUDE {}.
1711 if (inm->in6m_refcount == 1)
1712 in6m_disconnect_locked(&inmh, inm);
1713 in6m_rele_locked(&inmh, inm);
1715 case MLD_G_QUERY_PENDING_MEMBER:
1716 case MLD_SG_QUERY_PENDING_MEMBER:
1717 in6m_clear_recorded(inm);
1719 case MLD_REPORTING_MEMBER:
1720 inm->in6m_sctimer = 0;
1721 inm->in6m_timer = 0;
1722 inm->in6m_state = MLD_REPORTING_MEMBER;
1724 * Free any pending MLDv2 state-change records.
1726 mbufq_drain(&inm->in6m_scq);
1731 IF_ADDR_WUNLOCK(ifp);
1732 in6m_release_list_deferred(&inmh);
1736 * Global slowtimo handler.
1737 * VIMAGE: Timeout handlers are expected to service all vimages.
1742 VNET_ITERATOR_DECL(vnet_iter);
1744 VNET_LIST_RLOCK_NOSLEEP();
1745 VNET_FOREACH(vnet_iter) {
1746 CURVNET_SET(vnet_iter);
1747 mld_slowtimo_vnet();
1750 VNET_LIST_RUNLOCK_NOSLEEP();
1754 * Per-vnet slowtimo handler.
1757 mld_slowtimo_vnet(void)
1759 struct mld_ifsoftc *mli;
1763 LIST_FOREACH(mli, &V_mli_head, mli_link) {
1764 mld_v1_process_querier_timers(mli);
1771 * Update the Older Version Querier Present timers for a link.
1772 * See Section 9.12 of RFC 3810.
1775 mld_v1_process_querier_timers(struct mld_ifsoftc *mli)
1780 if (mli->mli_version != MLD_VERSION_2 && --mli->mli_v1_timer == 0) {
1782 * MLDv1 Querier Present timer expired; revert to MLDv2.
1785 "%s: transition from v%d -> v%d on %p(%s)",
1786 __func__, mli->mli_version, MLD_VERSION_2,
1787 mli->mli_ifp, if_name(mli->mli_ifp));
1788 mli->mli_version = MLD_VERSION_2;
1793 * Transmit an MLDv1 report immediately.
1796 mld_v1_transmit_report(struct in6_multi *in6m, const int type)
1799 struct in6_ifaddr *ia;
1800 struct ip6_hdr *ip6;
1801 struct mbuf *mh, *md;
1802 struct mld_hdr *mld;
1804 IN6_MULTI_LIST_LOCK_ASSERT();
1807 ifp = in6m->in6m_ifp;
1808 /* in process of being freed */
1811 ia = in6ifa_ifpforlinklocal(ifp, IN6_IFF_NOTREADY|IN6_IFF_ANYCAST);
1812 /* ia may be NULL if link-local address is tentative. */
1814 mh = m_gethdr(M_NOWAIT, MT_DATA);
1817 ifa_free(&ia->ia_ifa);
1820 md = m_get(M_NOWAIT, MT_DATA);
1824 ifa_free(&ia->ia_ifa);
1830 * FUTURE: Consider increasing alignment by ETHER_HDR_LEN, so
1831 * that ether_output() does not need to allocate another mbuf
1832 * for the header in the most common case.
1834 M_ALIGN(mh, sizeof(struct ip6_hdr));
1835 mh->m_pkthdr.len = sizeof(struct ip6_hdr) + sizeof(struct mld_hdr);
1836 mh->m_len = sizeof(struct ip6_hdr);
1838 ip6 = mtod(mh, struct ip6_hdr *);
1840 ip6->ip6_vfc &= ~IPV6_VERSION_MASK;
1841 ip6->ip6_vfc |= IPV6_VERSION;
1842 ip6->ip6_nxt = IPPROTO_ICMPV6;
1843 ip6->ip6_src = ia ? ia->ia_addr.sin6_addr : in6addr_any;
1844 ip6->ip6_dst = in6m->in6m_addr;
1846 md->m_len = sizeof(struct mld_hdr);
1847 mld = mtod(md, struct mld_hdr *);
1848 mld->mld_type = type;
1851 mld->mld_maxdelay = 0;
1852 mld->mld_reserved = 0;
1853 mld->mld_addr = in6m->in6m_addr;
1854 in6_clearscope(&mld->mld_addr);
1855 mld->mld_cksum = in6_cksum(mh, IPPROTO_ICMPV6,
1856 sizeof(struct ip6_hdr), sizeof(struct mld_hdr));
1858 mld_save_context(mh, ifp);
1859 mh->m_flags |= M_MLDV1;
1861 mld_dispatch_packet(mh);
1864 ifa_free(&ia->ia_ifa);
1869 * Process a state change from the upper layer for the given IPv6 group.
1871 * Each socket holds a reference on the in_multi in its own ip_moptions.
1872 * The socket layer will have made the necessary updates to.the group
1873 * state, it is now up to MLD to issue a state change report if there
1874 * has been any change between T0 (when the last state-change was issued)
1877 * We use the MLDv2 state machine at group level. The MLd module
1878 * however makes the decision as to which MLD protocol version to speak.
1879 * A state change *from* INCLUDE {} always means an initial join.
1880 * A state change *to* INCLUDE {} always means a final leave.
1882 * If delay is non-zero, and the state change is an initial multicast
1883 * join, the state change report will be delayed by 'delay' ticks
1884 * in units of PR_FASTHZ if MLDv1 is active on the link; otherwise
1885 * the initial MLDv2 state change report will be delayed by whichever
1886 * is sooner, a pending state-change timer or delay itself.
1888 * VIMAGE: curvnet should have been set by caller, as this routine
1889 * is called from the socket option handlers.
1892 mld_change_state(struct in6_multi *inm, const int delay)
1894 struct mld_ifsoftc *mli;
1898 IN6_MULTI_LIST_LOCK_ASSERT();
1903 * Check if the in6_multi has already been disconnected.
1905 if (inm->in6m_ifp == NULL) {
1906 CTR1(KTR_MLD, "%s: inm is disconnected", __func__);
1911 * Try to detect if the upper layer just asked us to change state
1912 * for an interface which has now gone away.
1914 KASSERT(inm->in6m_ifma != NULL, ("%s: no ifma", __func__));
1915 ifp = inm->in6m_ifma->ifma_ifp;
1919 * Sanity check that netinet6's notion of ifp is the
1922 KASSERT(inm->in6m_ifp == ifp, ("%s: bad ifp", __func__));
1925 mli = MLD_IFINFO(ifp);
1926 KASSERT(mli != NULL, ("%s: no mld_ifsoftc for ifp %p", __func__, ifp));
1929 * If we detect a state transition to or from MCAST_UNDEFINED
1930 * for this group, then we are starting or finishing an MLD
1931 * life cycle for this group.
1933 if (inm->in6m_st[1].iss_fmode != inm->in6m_st[0].iss_fmode) {
1934 CTR3(KTR_MLD, "%s: inm transition %d -> %d", __func__,
1935 inm->in6m_st[0].iss_fmode, inm->in6m_st[1].iss_fmode);
1936 if (inm->in6m_st[0].iss_fmode == MCAST_UNDEFINED) {
1937 CTR1(KTR_MLD, "%s: initial join", __func__);
1938 error = mld_initial_join(inm, mli, delay);
1940 } else if (inm->in6m_st[1].iss_fmode == MCAST_UNDEFINED) {
1941 CTR1(KTR_MLD, "%s: final leave", __func__);
1942 mld_final_leave(inm, mli);
1946 CTR1(KTR_MLD, "%s: filter set change", __func__);
1949 error = mld_handle_state_change(inm, mli);
1957 * Perform the initial join for an MLD group.
1959 * When joining a group:
1960 * If the group should have its MLD traffic suppressed, do nothing.
1961 * MLDv1 starts sending MLDv1 host membership reports.
1962 * MLDv2 will schedule an MLDv2 state-change report containing the
1963 * initial state of the membership.
1965 * If the delay argument is non-zero, then we must delay sending the
1966 * initial state change for delay ticks (in units of PR_FASTHZ).
1969 mld_initial_join(struct in6_multi *inm, struct mld_ifsoftc *mli,
1974 int error, retval, syncstates;
1977 char ip6tbuf[INET6_ADDRSTRLEN];
1980 CTR4(KTR_MLD, "%s: initial join %s on ifp %p(%s)",
1981 __func__, ip6_sprintf(ip6tbuf, &inm->in6m_addr),
1982 inm->in6m_ifp, if_name(inm->in6m_ifp));
1987 ifp = inm->in6m_ifp;
1989 IN6_MULTI_LIST_LOCK_ASSERT();
1992 KASSERT(mli && mli->mli_ifp == ifp, ("%s: inconsistent ifp", __func__));
1995 * Groups joined on loopback or marked as 'not reported',
1996 * enter the MLD_SILENT_MEMBER state and
1997 * are never reported in any protocol exchanges.
1998 * All other groups enter the appropriate state machine
1999 * for the version in use on this link.
2000 * A link marked as MLIF_SILENT causes MLD to be completely
2001 * disabled for the link.
2003 if ((ifp->if_flags & IFF_LOOPBACK) ||
2004 (mli->mli_flags & MLIF_SILENT) ||
2005 !mld_is_addr_reported(&inm->in6m_addr)) {
2007 "%s: not kicking state machine for silent group", __func__);
2008 inm->in6m_state = MLD_SILENT_MEMBER;
2009 inm->in6m_timer = 0;
2012 * Deal with overlapping in_multi lifecycle.
2013 * If this group was LEAVING, then make sure
2014 * we drop the reference we picked up to keep the
2015 * group around for the final INCLUDE {} enqueue.
2017 if (mli->mli_version == MLD_VERSION_2 &&
2018 inm->in6m_state == MLD_LEAVING_MEMBER) {
2019 inm->in6m_refcount--;
2020 MPASS(inm->in6m_refcount > 0);
2022 inm->in6m_state = MLD_REPORTING_MEMBER;
2024 switch (mli->mli_version) {
2027 * If a delay was provided, only use it if
2028 * it is greater than the delay normally
2029 * used for an MLDv1 state change report,
2030 * and delay sending the initial MLDv1 report
2031 * by not transitioning to the IDLE state.
2033 odelay = MLD_RANDOM_DELAY(MLD_V1_MAX_RI * PR_FASTHZ);
2035 inm->in6m_timer = max(delay, odelay);
2036 V_current_state_timers_running6 = 1;
2038 inm->in6m_state = MLD_IDLE_MEMBER;
2039 error = mld_v1_transmit_report(inm,
2040 MLD_LISTENER_REPORT);
2042 inm->in6m_timer = odelay;
2043 V_current_state_timers_running6 = 1;
2050 * Defer update of T0 to T1, until the first copy
2051 * of the state change has been transmitted.
2056 * Immediately enqueue a State-Change Report for
2057 * this interface, freeing any previous reports.
2058 * Don't kick the timers if there is nothing to do,
2059 * or if an error occurred.
2061 mq = &inm->in6m_scq;
2063 retval = mld_v2_enqueue_group_record(mq, inm, 1,
2064 0, 0, (mli->mli_flags & MLIF_USEALLOW));
2065 CTR2(KTR_MLD, "%s: enqueue record = %d",
2068 error = retval * -1;
2073 * Schedule transmission of pending state-change
2074 * report up to RV times for this link. The timer
2075 * will fire at the next mld_fasttimo (~200ms),
2076 * giving us an opportunity to merge the reports.
2078 * If a delay was provided to this function, only
2079 * use this delay if sooner than the existing one.
2081 KASSERT(mli->mli_rv > 1,
2082 ("%s: invalid robustness %d", __func__,
2084 inm->in6m_scrv = mli->mli_rv;
2086 if (inm->in6m_sctimer > 1) {
2088 min(inm->in6m_sctimer, delay);
2090 inm->in6m_sctimer = delay;
2092 inm->in6m_sctimer = 1;
2093 V_state_change_timers_running6 = 1;
2101 * Only update the T0 state if state change is atomic,
2102 * i.e. we don't need to wait for a timer to fire before we
2103 * can consider the state change to have been communicated.
2107 CTR3(KTR_MLD, "%s: T1 -> T0 for %s/%s", __func__,
2108 ip6_sprintf(ip6tbuf, &inm->in6m_addr),
2109 if_name(inm->in6m_ifp));
2116 * Issue an intermediate state change during the life-cycle.
2119 mld_handle_state_change(struct in6_multi *inm, struct mld_ifsoftc *mli)
2124 char ip6tbuf[INET6_ADDRSTRLEN];
2127 CTR4(KTR_MLD, "%s: state change for %s on ifp %p(%s)",
2128 __func__, ip6_sprintf(ip6tbuf, &inm->in6m_addr),
2129 inm->in6m_ifp, if_name(inm->in6m_ifp));
2131 ifp = inm->in6m_ifp;
2133 IN6_MULTI_LIST_LOCK_ASSERT();
2136 KASSERT(mli && mli->mli_ifp == ifp,
2137 ("%s: inconsistent ifp", __func__));
2139 if ((ifp->if_flags & IFF_LOOPBACK) ||
2140 (mli->mli_flags & MLIF_SILENT) ||
2141 !mld_is_addr_reported(&inm->in6m_addr) ||
2142 (mli->mli_version != MLD_VERSION_2)) {
2143 if (!mld_is_addr_reported(&inm->in6m_addr)) {
2145 "%s: not kicking state machine for silent group", __func__);
2147 CTR1(KTR_MLD, "%s: nothing to do", __func__);
2149 CTR3(KTR_MLD, "%s: T1 -> T0 for %s/%s", __func__,
2150 ip6_sprintf(ip6tbuf, &inm->in6m_addr),
2151 if_name(inm->in6m_ifp));
2155 mbufq_drain(&inm->in6m_scq);
2157 retval = mld_v2_enqueue_group_record(&inm->in6m_scq, inm, 1, 0, 0,
2158 (mli->mli_flags & MLIF_USEALLOW));
2159 CTR2(KTR_MLD, "%s: enqueue record = %d", __func__, retval);
2164 * If record(s) were enqueued, start the state-change
2165 * report timer for this group.
2167 inm->in6m_scrv = mli->mli_rv;
2168 inm->in6m_sctimer = 1;
2169 V_state_change_timers_running6 = 1;
2175 * Perform the final leave for a multicast address.
2177 * When leaving a group:
2178 * MLDv1 sends a DONE message, if and only if we are the reporter.
2179 * MLDv2 enqueues a state-change report containing a transition
2180 * to INCLUDE {} for immediate transmission.
2183 mld_final_leave(struct in6_multi *inm, struct mld_ifsoftc *mli)
2187 char ip6tbuf[INET6_ADDRSTRLEN];
2192 CTR4(KTR_MLD, "%s: final leave %s on ifp %p(%s)",
2193 __func__, ip6_sprintf(ip6tbuf, &inm->in6m_addr),
2194 inm->in6m_ifp, if_name(inm->in6m_ifp));
2196 IN6_MULTI_LIST_LOCK_ASSERT();
2199 switch (inm->in6m_state) {
2200 case MLD_NOT_MEMBER:
2201 case MLD_SILENT_MEMBER:
2202 case MLD_LEAVING_MEMBER:
2203 /* Already leaving or left; do nothing. */
2205 "%s: not kicking state machine for silent group", __func__);
2207 case MLD_REPORTING_MEMBER:
2208 case MLD_IDLE_MEMBER:
2209 case MLD_G_QUERY_PENDING_MEMBER:
2210 case MLD_SG_QUERY_PENDING_MEMBER:
2211 if (mli->mli_version == MLD_VERSION_1) {
2213 if (inm->in6m_state == MLD_G_QUERY_PENDING_MEMBER ||
2214 inm->in6m_state == MLD_SG_QUERY_PENDING_MEMBER)
2215 panic("%s: MLDv2 state reached, not MLDv2 mode",
2218 mld_v1_transmit_report(inm, MLD_LISTENER_DONE);
2219 inm->in6m_state = MLD_NOT_MEMBER;
2220 V_current_state_timers_running6 = 1;
2221 } else if (mli->mli_version == MLD_VERSION_2) {
2223 * Stop group timer and all pending reports.
2224 * Immediately enqueue a state-change report
2225 * TO_IN {} to be sent on the next fast timeout,
2226 * giving us an opportunity to merge reports.
2228 mbufq_drain(&inm->in6m_scq);
2229 inm->in6m_timer = 0;
2230 inm->in6m_scrv = mli->mli_rv;
2231 CTR4(KTR_MLD, "%s: Leaving %s/%s with %d "
2232 "pending retransmissions.", __func__,
2233 ip6_sprintf(ip6tbuf, &inm->in6m_addr),
2234 if_name(inm->in6m_ifp), inm->in6m_scrv);
2235 if (inm->in6m_scrv == 0) {
2236 inm->in6m_state = MLD_NOT_MEMBER;
2237 inm->in6m_sctimer = 0;
2241 in6m_acquire_locked(inm);
2243 retval = mld_v2_enqueue_group_record(
2244 &inm->in6m_scq, inm, 1, 0, 0,
2245 (mli->mli_flags & MLIF_USEALLOW));
2246 KASSERT(retval != 0,
2247 ("%s: enqueue record = %d", __func__,
2250 inm->in6m_state = MLD_LEAVING_MEMBER;
2251 inm->in6m_sctimer = 1;
2252 V_state_change_timers_running6 = 1;
2258 case MLD_LAZY_MEMBER:
2259 case MLD_SLEEPING_MEMBER:
2260 case MLD_AWAKENING_MEMBER:
2261 /* Our reports are suppressed; do nothing. */
2267 CTR3(KTR_MLD, "%s: T1 -> T0 for %s/%s", __func__,
2268 ip6_sprintf(ip6tbuf, &inm->in6m_addr),
2269 if_name(inm->in6m_ifp));
2270 inm->in6m_st[1].iss_fmode = MCAST_UNDEFINED;
2271 CTR3(KTR_MLD, "%s: T1 now MCAST_UNDEFINED for %p/%s",
2272 __func__, &inm->in6m_addr, if_name(inm->in6m_ifp));
2277 * Enqueue an MLDv2 group record to the given output queue.
2279 * If is_state_change is zero, a current-state record is appended.
2280 * If is_state_change is non-zero, a state-change report is appended.
2282 * If is_group_query is non-zero, an mbuf packet chain is allocated.
2283 * If is_group_query is zero, and if there is a packet with free space
2284 * at the tail of the queue, it will be appended to providing there
2285 * is enough free space.
2286 * Otherwise a new mbuf packet chain is allocated.
2288 * If is_source_query is non-zero, each source is checked to see if
2289 * it was recorded for a Group-Source query, and will be omitted if
2290 * it is not both in-mode and recorded.
2292 * If use_block_allow is non-zero, state change reports for initial join
2293 * and final leave, on an inclusive mode group with a source list, will be
2294 * rewritten to use the ALLOW_NEW and BLOCK_OLD record types, respectively.
2296 * The function will attempt to allocate leading space in the packet
2297 * for the IPv6+ICMP headers to be prepended without fragmenting the chain.
2299 * If successful the size of all data appended to the queue is returned,
2300 * otherwise an error code less than zero is returned, or zero if
2301 * no record(s) were appended.
2304 mld_v2_enqueue_group_record(struct mbufq *mq, struct in6_multi *inm,
2305 const int is_state_change, const int is_group_query,
2306 const int is_source_query, const int use_block_allow)
2308 struct mldv2_record mr;
2309 struct mldv2_record *pmr;
2311 struct ip6_msource *ims, *nims;
2312 struct mbuf *m0, *m, *md;
2313 int is_filter_list_change;
2314 int minrec0len, m0srcs, msrcs, nbytes, off;
2315 int record_has_sources;
2320 char ip6tbuf[INET6_ADDRSTRLEN];
2323 IN6_MULTI_LIST_LOCK_ASSERT();
2325 ifp = inm->in6m_ifp;
2326 is_filter_list_change = 0;
2333 record_has_sources = 1;
2335 type = MLD_DO_NOTHING;
2336 mode = inm->in6m_st[1].iss_fmode;
2339 * If we did not transition out of ASM mode during t0->t1,
2340 * and there are no source nodes to process, we can skip
2341 * the generation of source records.
2343 if (inm->in6m_st[0].iss_asm > 0 && inm->in6m_st[1].iss_asm > 0 &&
2344 inm->in6m_nsrc == 0)
2345 record_has_sources = 0;
2347 if (is_state_change) {
2349 * Queue a state change record.
2350 * If the mode did not change, and there are non-ASM
2351 * listeners or source filters present,
2352 * we potentially need to issue two records for the group.
2353 * If there are ASM listeners, and there was no filter
2354 * mode transition of any kind, do nothing.
2356 * If we are transitioning to MCAST_UNDEFINED, we need
2357 * not send any sources. A transition to/from this state is
2358 * considered inclusive with some special treatment.
2360 * If we are rewriting initial joins/leaves to use
2361 * ALLOW/BLOCK, and the group's membership is inclusive,
2362 * we need to send sources in all cases.
2364 if (mode != inm->in6m_st[0].iss_fmode) {
2365 if (mode == MCAST_EXCLUDE) {
2366 CTR1(KTR_MLD, "%s: change to EXCLUDE",
2368 type = MLD_CHANGE_TO_EXCLUDE_MODE;
2370 CTR1(KTR_MLD, "%s: change to INCLUDE",
2372 if (use_block_allow) {
2375 * Here we're interested in state
2376 * edges either direction between
2377 * MCAST_UNDEFINED and MCAST_INCLUDE.
2378 * Perhaps we should just check
2379 * the group state, rather than
2382 if (mode == MCAST_UNDEFINED) {
2383 type = MLD_BLOCK_OLD_SOURCES;
2385 type = MLD_ALLOW_NEW_SOURCES;
2388 type = MLD_CHANGE_TO_INCLUDE_MODE;
2389 if (mode == MCAST_UNDEFINED)
2390 record_has_sources = 0;
2394 if (record_has_sources) {
2395 is_filter_list_change = 1;
2397 type = MLD_DO_NOTHING;
2402 * Queue a current state record.
2404 if (mode == MCAST_EXCLUDE) {
2405 type = MLD_MODE_IS_EXCLUDE;
2406 } else if (mode == MCAST_INCLUDE) {
2407 type = MLD_MODE_IS_INCLUDE;
2408 KASSERT(inm->in6m_st[1].iss_asm == 0,
2409 ("%s: inm %p is INCLUDE but ASM count is %d",
2410 __func__, inm, inm->in6m_st[1].iss_asm));
2415 * Generate the filter list changes using a separate function.
2417 if (is_filter_list_change)
2418 return (mld_v2_enqueue_filter_change(mq, inm));
2420 if (type == MLD_DO_NOTHING) {
2421 CTR3(KTR_MLD, "%s: nothing to do for %s/%s",
2422 __func__, ip6_sprintf(ip6tbuf, &inm->in6m_addr),
2423 if_name(inm->in6m_ifp));
2428 * If any sources are present, we must be able to fit at least
2429 * one in the trailing space of the tail packet's mbuf,
2432 minrec0len = sizeof(struct mldv2_record);
2433 if (record_has_sources)
2434 minrec0len += sizeof(struct in6_addr);
2436 CTR4(KTR_MLD, "%s: queueing %s for %s/%s", __func__,
2437 mld_rec_type_to_str(type),
2438 ip6_sprintf(ip6tbuf, &inm->in6m_addr),
2439 if_name(inm->in6m_ifp));
2442 * Check if we have a packet in the tail of the queue for this
2443 * group into which the first group record for this group will fit.
2444 * Otherwise allocate a new packet.
2445 * Always allocate leading space for IP6+RA+ICMPV6+REPORT.
2446 * Note: Group records for G/GSR query responses MUST be sent
2447 * in their own packet.
2449 m0 = mbufq_last(mq);
2450 if (!is_group_query &&
2452 (m0->m_pkthdr.PH_vt.vt_nrecs + 1 <= MLD_V2_REPORT_MAXRECS) &&
2453 (m0->m_pkthdr.len + minrec0len) <
2454 (ifp->if_mtu - MLD_MTUSPACE)) {
2455 m0srcs = (ifp->if_mtu - m0->m_pkthdr.len -
2456 sizeof(struct mldv2_record)) /
2457 sizeof(struct in6_addr);
2459 CTR1(KTR_MLD, "%s: use existing packet", __func__);
2461 if (mbufq_full(mq)) {
2462 CTR1(KTR_MLD, "%s: outbound queue full", __func__);
2466 m0srcs = (ifp->if_mtu - MLD_MTUSPACE -
2467 sizeof(struct mldv2_record)) / sizeof(struct in6_addr);
2468 if (!is_state_change && !is_group_query)
2469 m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
2471 m = m_gethdr(M_NOWAIT, MT_DATA);
2475 mld_save_context(m, ifp);
2477 CTR1(KTR_MLD, "%s: allocated first packet", __func__);
2481 * Append group record.
2482 * If we have sources, we don't know how many yet.
2487 mr.mr_addr = inm->in6m_addr;
2488 in6_clearscope(&mr.mr_addr);
2489 if (!m_append(m, sizeof(struct mldv2_record), (void *)&mr)) {
2492 CTR1(KTR_MLD, "%s: m_append() failed.", __func__);
2495 nbytes += sizeof(struct mldv2_record);
2498 * Append as many sources as will fit in the first packet.
2499 * If we are appending to a new packet, the chain allocation
2500 * may potentially use clusters; use m_getptr() in this case.
2501 * If we are appending to an existing packet, we need to obtain
2502 * a pointer to the group record after m_append(), in case a new
2503 * mbuf was allocated.
2505 * Only append sources which are in-mode at t1. If we are
2506 * transitioning to MCAST_UNDEFINED state on the group, and
2507 * use_block_allow is zero, do not include source entries.
2508 * Otherwise, we need to include this source in the report.
2510 * Only report recorded sources in our filter set when responding
2511 * to a group-source query.
2513 if (record_has_sources) {
2516 pmr = (struct mldv2_record *)(mtod(md, uint8_t *) +
2517 md->m_len - nbytes);
2519 md = m_getptr(m, 0, &off);
2520 pmr = (struct mldv2_record *)(mtod(md, uint8_t *) +
2524 RB_FOREACH_SAFE(ims, ip6_msource_tree, &inm->in6m_srcs,
2526 CTR2(KTR_MLD, "%s: visit node %s", __func__,
2527 ip6_sprintf(ip6tbuf, &ims->im6s_addr));
2528 now = im6s_get_mode(inm, ims, 1);
2529 CTR2(KTR_MLD, "%s: node is %d", __func__, now);
2530 if ((now != mode) ||
2532 (!use_block_allow && mode == MCAST_UNDEFINED))) {
2533 CTR1(KTR_MLD, "%s: skip node", __func__);
2536 if (is_source_query && ims->im6s_stp == 0) {
2537 CTR1(KTR_MLD, "%s: skip unrecorded node",
2541 CTR1(KTR_MLD, "%s: append node", __func__);
2542 if (!m_append(m, sizeof(struct in6_addr),
2543 (void *)&ims->im6s_addr)) {
2546 CTR1(KTR_MLD, "%s: m_append() failed.",
2550 nbytes += sizeof(struct in6_addr);
2552 if (msrcs == m0srcs)
2555 CTR2(KTR_MLD, "%s: msrcs is %d this packet", __func__,
2557 pmr->mr_numsrc = htons(msrcs);
2558 nbytes += (msrcs * sizeof(struct in6_addr));
2561 if (is_source_query && msrcs == 0) {
2562 CTR1(KTR_MLD, "%s: no recorded sources to report", __func__);
2569 * We are good to go with first packet.
2572 CTR1(KTR_MLD, "%s: enqueueing first packet", __func__);
2573 m->m_pkthdr.PH_vt.vt_nrecs = 1;
2574 mbufq_enqueue(mq, m);
2576 m->m_pkthdr.PH_vt.vt_nrecs++;
2579 * No further work needed if no source list in packet(s).
2581 if (!record_has_sources)
2585 * Whilst sources remain to be announced, we need to allocate
2586 * a new packet and fill out as many sources as will fit.
2587 * Always try for a cluster first.
2589 while (nims != NULL) {
2590 if (mbufq_full(mq)) {
2591 CTR1(KTR_MLD, "%s: outbound queue full", __func__);
2594 m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
2596 m = m_gethdr(M_NOWAIT, MT_DATA);
2599 mld_save_context(m, ifp);
2600 md = m_getptr(m, 0, &off);
2601 pmr = (struct mldv2_record *)(mtod(md, uint8_t *) + off);
2602 CTR1(KTR_MLD, "%s: allocated next packet", __func__);
2604 if (!m_append(m, sizeof(struct mldv2_record), (void *)&mr)) {
2607 CTR1(KTR_MLD, "%s: m_append() failed.", __func__);
2610 m->m_pkthdr.PH_vt.vt_nrecs = 1;
2611 nbytes += sizeof(struct mldv2_record);
2613 m0srcs = (ifp->if_mtu - MLD_MTUSPACE -
2614 sizeof(struct mldv2_record)) / sizeof(struct in6_addr);
2617 RB_FOREACH_FROM(ims, ip6_msource_tree, nims) {
2618 CTR2(KTR_MLD, "%s: visit node %s",
2619 __func__, ip6_sprintf(ip6tbuf, &ims->im6s_addr));
2620 now = im6s_get_mode(inm, ims, 1);
2621 if ((now != mode) ||
2623 (!use_block_allow && mode == MCAST_UNDEFINED))) {
2624 CTR1(KTR_MLD, "%s: skip node", __func__);
2627 if (is_source_query && ims->im6s_stp == 0) {
2628 CTR1(KTR_MLD, "%s: skip unrecorded node",
2632 CTR1(KTR_MLD, "%s: append node", __func__);
2633 if (!m_append(m, sizeof(struct in6_addr),
2634 (void *)&ims->im6s_addr)) {
2637 CTR1(KTR_MLD, "%s: m_append() failed.",
2642 if (msrcs == m0srcs)
2645 pmr->mr_numsrc = htons(msrcs);
2646 nbytes += (msrcs * sizeof(struct in6_addr));
2648 CTR1(KTR_MLD, "%s: enqueueing next packet", __func__);
2649 mbufq_enqueue(mq, m);
2656 * Type used to mark record pass completion.
2657 * We exploit the fact we can cast to this easily from the
2658 * current filter modes on each ip_msource node.
2661 REC_NONE = 0x00, /* MCAST_UNDEFINED */
2662 REC_ALLOW = 0x01, /* MCAST_INCLUDE */
2663 REC_BLOCK = 0x02, /* MCAST_EXCLUDE */
2664 REC_FULL = REC_ALLOW | REC_BLOCK
2668 * Enqueue an MLDv2 filter list change to the given output queue.
2670 * Source list filter state is held in an RB-tree. When the filter list
2671 * for a group is changed without changing its mode, we need to compute
2672 * the deltas between T0 and T1 for each source in the filter set,
2673 * and enqueue the appropriate ALLOW_NEW/BLOCK_OLD records.
2675 * As we may potentially queue two record types, and the entire R-B tree
2676 * needs to be walked at once, we break this out into its own function
2677 * so we can generate a tightly packed queue of packets.
2679 * XXX This could be written to only use one tree walk, although that makes
2680 * serializing into the mbuf chains a bit harder. For now we do two walks
2681 * which makes things easier on us, and it may or may not be harder on
2684 * If successful the size of all data appended to the queue is returned,
2685 * otherwise an error code less than zero is returned, or zero if
2686 * no record(s) were appended.
2689 mld_v2_enqueue_filter_change(struct mbufq *mq, struct in6_multi *inm)
2691 static const int MINRECLEN =
2692 sizeof(struct mldv2_record) + sizeof(struct in6_addr);
2694 struct mldv2_record mr;
2695 struct mldv2_record *pmr;
2696 struct ip6_msource *ims, *nims;
2697 struct mbuf *m, *m0, *md;
2698 int m0srcs, nbytes, npbytes, off, rsrcs, schanged;
2700 uint8_t mode, now, then;
2701 rectype_t crt, drt, nrt;
2703 char ip6tbuf[INET6_ADDRSTRLEN];
2706 IN6_MULTI_LIST_LOCK_ASSERT();
2708 if (inm->in6m_nsrc == 0 ||
2709 (inm->in6m_st[0].iss_asm > 0 && inm->in6m_st[1].iss_asm > 0))
2712 ifp = inm->in6m_ifp; /* interface */
2713 mode = inm->in6m_st[1].iss_fmode; /* filter mode at t1 */
2714 crt = REC_NONE; /* current group record type */
2715 drt = REC_NONE; /* mask of completed group record types */
2716 nrt = REC_NONE; /* record type for current node */
2717 m0srcs = 0; /* # source which will fit in current mbuf chain */
2718 npbytes = 0; /* # of bytes appended this packet */
2719 nbytes = 0; /* # of bytes appended to group's state-change queue */
2720 rsrcs = 0; /* # sources encoded in current record */
2721 schanged = 0; /* # nodes encoded in overall filter change */
2722 nallow = 0; /* # of source entries in ALLOW_NEW */
2723 nblock = 0; /* # of source entries in BLOCK_OLD */
2724 nims = NULL; /* next tree node pointer */
2727 * For each possible filter record mode.
2728 * The first kind of source we encounter tells us which
2729 * is the first kind of record we start appending.
2730 * If a node transitioned to UNDEFINED at t1, its mode is treated
2731 * as the inverse of the group's filter mode.
2733 while (drt != REC_FULL) {
2735 m0 = mbufq_last(mq);
2737 (m0->m_pkthdr.PH_vt.vt_nrecs + 1 <=
2738 MLD_V2_REPORT_MAXRECS) &&
2739 (m0->m_pkthdr.len + MINRECLEN) <
2740 (ifp->if_mtu - MLD_MTUSPACE)) {
2742 m0srcs = (ifp->if_mtu - m0->m_pkthdr.len -
2743 sizeof(struct mldv2_record)) /
2744 sizeof(struct in6_addr);
2746 "%s: use previous packet", __func__);
2748 m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
2750 m = m_gethdr(M_NOWAIT, MT_DATA);
2753 "%s: m_get*() failed", __func__);
2756 m->m_pkthdr.PH_vt.vt_nrecs = 0;
2757 mld_save_context(m, ifp);
2758 m0srcs = (ifp->if_mtu - MLD_MTUSPACE -
2759 sizeof(struct mldv2_record)) /
2760 sizeof(struct in6_addr);
2763 "%s: allocated new packet", __func__);
2766 * Append the MLD group record header to the
2767 * current packet's data area.
2768 * Recalculate pointer to free space for next
2769 * group record, in case m_append() allocated
2770 * a new mbuf or cluster.
2772 memset(&mr, 0, sizeof(mr));
2773 mr.mr_addr = inm->in6m_addr;
2774 in6_clearscope(&mr.mr_addr);
2775 if (!m_append(m, sizeof(mr), (void *)&mr)) {
2779 "%s: m_append() failed", __func__);
2782 npbytes += sizeof(struct mldv2_record);
2784 /* new packet; offset in chain */
2785 md = m_getptr(m, npbytes -
2786 sizeof(struct mldv2_record), &off);
2787 pmr = (struct mldv2_record *)(mtod(md,
2790 /* current packet; offset from last append */
2792 pmr = (struct mldv2_record *)(mtod(md,
2793 uint8_t *) + md->m_len -
2794 sizeof(struct mldv2_record));
2797 * Begin walking the tree for this record type
2798 * pass, or continue from where we left off
2799 * previously if we had to allocate a new packet.
2800 * Only report deltas in-mode at t1.
2801 * We need not report included sources as allowed
2802 * if we are in inclusive mode on the group,
2803 * however the converse is not true.
2807 nims = RB_MIN(ip6_msource_tree,
2810 RB_FOREACH_FROM(ims, ip6_msource_tree, nims) {
2811 CTR2(KTR_MLD, "%s: visit node %s", __func__,
2812 ip6_sprintf(ip6tbuf, &ims->im6s_addr));
2813 now = im6s_get_mode(inm, ims, 1);
2814 then = im6s_get_mode(inm, ims, 0);
2815 CTR3(KTR_MLD, "%s: mode: t0 %d, t1 %d",
2816 __func__, then, now);
2819 "%s: skip unchanged", __func__);
2822 if (mode == MCAST_EXCLUDE &&
2823 now == MCAST_INCLUDE) {
2825 "%s: skip IN src on EX group",
2829 nrt = (rectype_t)now;
2830 if (nrt == REC_NONE)
2831 nrt = (rectype_t)(~mode & REC_FULL);
2832 if (schanged++ == 0) {
2834 } else if (crt != nrt)
2836 if (!m_append(m, sizeof(struct in6_addr),
2837 (void *)&ims->im6s_addr)) {
2841 "%s: m_append() failed", __func__);
2844 nallow += !!(crt == REC_ALLOW);
2845 nblock += !!(crt == REC_BLOCK);
2846 if (++rsrcs == m0srcs)
2850 * If we did not append any tree nodes on this
2851 * pass, back out of allocations.
2854 npbytes -= sizeof(struct mldv2_record);
2857 "%s: m_free(m)", __func__);
2861 "%s: m_adj(m, -mr)", __func__);
2862 m_adj(m, -((int)sizeof(
2863 struct mldv2_record)));
2867 npbytes += (rsrcs * sizeof(struct in6_addr));
2868 if (crt == REC_ALLOW)
2869 pmr->mr_type = MLD_ALLOW_NEW_SOURCES;
2870 else if (crt == REC_BLOCK)
2871 pmr->mr_type = MLD_BLOCK_OLD_SOURCES;
2872 pmr->mr_numsrc = htons(rsrcs);
2874 * Count the new group record, and enqueue this
2875 * packet if it wasn't already queued.
2877 m->m_pkthdr.PH_vt.vt_nrecs++;
2879 mbufq_enqueue(mq, m);
2881 } while (nims != NULL);
2883 crt = (~crt & REC_FULL);
2886 CTR3(KTR_MLD, "%s: queued %d ALLOW_NEW, %d BLOCK_OLD", __func__,
2893 mld_v2_merge_state_changes(struct in6_multi *inm, struct mbufq *scq)
2896 struct mbuf *m; /* pending state-change */
2897 struct mbuf *m0; /* copy of pending state-change */
2898 struct mbuf *mt; /* last state-change in packet */
2899 int docopy, domerge;
2906 IN6_MULTI_LIST_LOCK_ASSERT();
2910 * If there are further pending retransmissions, make a writable
2911 * copy of each queued state-change message before merging.
2913 if (inm->in6m_scrv > 0)
2916 gq = &inm->in6m_scq;
2918 if (mbufq_first(gq) == NULL) {
2919 CTR2(KTR_MLD, "%s: WARNING: queue for inm %p is empty",
2924 m = mbufq_first(gq);
2927 * Only merge the report into the current packet if
2928 * there is sufficient space to do so; an MLDv2 report
2929 * packet may only contain 65,535 group records.
2930 * Always use a simple mbuf chain concatentation to do this,
2931 * as large state changes for single groups may have
2932 * allocated clusters.
2935 mt = mbufq_last(scq);
2937 recslen = m_length(m, NULL);
2939 if ((mt->m_pkthdr.PH_vt.vt_nrecs +
2940 m->m_pkthdr.PH_vt.vt_nrecs <=
2941 MLD_V2_REPORT_MAXRECS) &&
2942 (mt->m_pkthdr.len + recslen <=
2943 (inm->in6m_ifp->if_mtu - MLD_MTUSPACE)))
2947 if (!domerge && mbufq_full(gq)) {
2949 "%s: outbound queue full, skipping whole packet %p",
2959 CTR2(KTR_MLD, "%s: dequeueing %p", __func__, m);
2960 m0 = mbufq_dequeue(gq);
2963 CTR2(KTR_MLD, "%s: copying %p", __func__, m);
2964 m0 = m_dup(m, M_NOWAIT);
2967 m0->m_nextpkt = NULL;
2972 CTR3(KTR_MLD, "%s: queueing %p to scq %p)",
2974 mbufq_enqueue(scq, m0);
2976 struct mbuf *mtl; /* last mbuf of packet mt */
2978 CTR3(KTR_MLD, "%s: merging %p with ifscq tail %p)",
2982 m0->m_flags &= ~M_PKTHDR;
2983 mt->m_pkthdr.len += recslen;
2984 mt->m_pkthdr.PH_vt.vt_nrecs +=
2985 m0->m_pkthdr.PH_vt.vt_nrecs;
2995 * Respond to a pending MLDv2 General Query.
2998 mld_v2_dispatch_general_query(struct mld_ifsoftc *mli)
3000 struct epoch_tracker et;
3001 struct ifmultiaddr *ifma;
3003 struct in6_multi *inm;
3006 IN6_MULTI_LIST_LOCK_ASSERT();
3009 KASSERT(mli->mli_version == MLD_VERSION_2,
3010 ("%s: called when version %d", __func__, mli->mli_version));
3013 * Check that there are some packets queued. If so, send them first.
3014 * For large number of groups the reply to general query can take
3015 * many packets, we should finish sending them before starting of
3016 * queuing the new reply.
3018 if (mbufq_len(&mli->mli_gq) != 0)
3023 NET_EPOCH_ENTER(et);
3024 CK_STAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
3025 inm = in6m_ifmultiaddr_get_inm(ifma);
3028 KASSERT(ifp == inm->in6m_ifp,
3029 ("%s: inconsistent ifp", __func__));
3031 switch (inm->in6m_state) {
3032 case MLD_NOT_MEMBER:
3033 case MLD_SILENT_MEMBER:
3035 case MLD_REPORTING_MEMBER:
3036 case MLD_IDLE_MEMBER:
3037 case MLD_LAZY_MEMBER:
3038 case MLD_SLEEPING_MEMBER:
3039 case MLD_AWAKENING_MEMBER:
3040 inm->in6m_state = MLD_REPORTING_MEMBER;
3041 retval = mld_v2_enqueue_group_record(&mli->mli_gq,
3043 CTR2(KTR_MLD, "%s: enqueue record = %d",
3046 case MLD_G_QUERY_PENDING_MEMBER:
3047 case MLD_SG_QUERY_PENDING_MEMBER:
3048 case MLD_LEAVING_MEMBER:
3055 mld_dispatch_queue(&mli->mli_gq, MLD_MAX_RESPONSE_BURST);
3058 * Slew transmission of bursts over 500ms intervals.
3060 if (mbufq_first(&mli->mli_gq) != NULL) {
3061 mli->mli_v2_timer = 1 + MLD_RANDOM_DELAY(
3062 MLD_RESPONSE_BURST_INTERVAL);
3063 V_interface_timers_running6 = 1;
3068 * Transmit the next pending message in the output queue.
3070 * VIMAGE: Needs to store/restore vnet pointer on a per-mbuf-chain basis.
3071 * MRT: Nothing needs to be done, as MLD traffic is always local to
3072 * a link and uses a link-scope multicast address.
3075 mld_dispatch_packet(struct mbuf *m)
3077 struct ip6_moptions im6o;
3082 struct ip6_hdr *ip6;
3083 struct mld_hdr *mld;
3089 CTR2(KTR_MLD, "%s: transmit %p", __func__, m);
3092 * Set VNET image pointer from enqueued mbuf chain
3093 * before doing anything else. Whilst we use interface
3094 * indexes to guard against interface detach, they are
3095 * unique to each VIMAGE and must be retrieved.
3097 ifindex = mld_restore_context(m);
3100 * Check if the ifnet still exists. This limits the scope of
3101 * any race in the absence of a global ifp lock for low cost
3102 * (an array lookup).
3104 ifp = ifnet_byindex(ifindex);
3106 CTR3(KTR_MLD, "%s: dropped %p as ifindex %u went away.",
3107 __func__, m, ifindex);
3109 IP6STAT_INC(ip6s_noroute);
3113 im6o.im6o_multicast_hlim = 1;
3114 im6o.im6o_multicast_loop = (V_ip6_mrouter != NULL);
3115 im6o.im6o_multicast_ifp = ifp;
3117 if (m->m_flags & M_MLDV1) {
3120 m0 = mld_v2_encap_report(ifp, m);
3122 CTR2(KTR_MLD, "%s: dropped %p", __func__, m);
3123 IP6STAT_INC(ip6s_odropped);
3128 mld_scrub_context(m0);
3130 m0->m_pkthdr.rcvif = V_loif;
3132 ip6 = mtod(m0, struct ip6_hdr *);
3134 (void)in6_setscope(&ip6->ip6_dst, ifp, NULL); /* XXX LOR */
3137 * XXX XXX Break some KPI rules to prevent an LOR which would
3138 * occur if we called in6_setscope() at transmission.
3139 * See comments at top of file.
3141 MLD_EMBEDSCOPE(&ip6->ip6_dst, ifp->if_index);
3145 * Retrieve the ICMPv6 type before handoff to ip6_output(),
3146 * so we can bump the stats.
3148 md = m_getptr(m0, sizeof(struct ip6_hdr), &off);
3149 mld = (struct mld_hdr *)(mtod(md, uint8_t *) + off);
3150 type = mld->mld_type;
3152 error = ip6_output(m0, &mld_po, NULL, IPV6_UNSPECSRC, &im6o,
3155 CTR3(KTR_MLD, "%s: ip6_output(%p) = %d", __func__, m0, error);
3158 ICMP6STAT_INC(icp6s_outhist[type]);
3160 icmp6_ifstat_inc(oifp, ifs6_out_msg);
3162 case MLD_LISTENER_REPORT:
3163 case MLDV2_LISTENER_REPORT:
3164 icmp6_ifstat_inc(oifp, ifs6_out_mldreport);
3166 case MLD_LISTENER_DONE:
3167 icmp6_ifstat_inc(oifp, ifs6_out_mlddone);
3176 * Encapsulate an MLDv2 report.
3178 * KAME IPv6 requires that hop-by-hop options be passed separately,
3179 * and that the IPv6 header be prepended in a separate mbuf.
3181 * Returns a pointer to the new mbuf chain head, or NULL if the
3182 * allocation failed.
3184 static struct mbuf *
3185 mld_v2_encap_report(struct ifnet *ifp, struct mbuf *m)
3188 struct mldv2_report *mld;
3189 struct ip6_hdr *ip6;
3190 struct in6_ifaddr *ia;
3193 KASSERT(ifp != NULL, ("%s: null ifp", __func__));
3194 KASSERT((m->m_flags & M_PKTHDR),
3195 ("%s: mbuf chain %p is !M_PKTHDR", __func__, m));
3198 * RFC3590: OK to send as :: or tentative during DAD.
3200 ia = in6ifa_ifpforlinklocal(ifp, IN6_IFF_NOTREADY|IN6_IFF_ANYCAST);
3202 CTR1(KTR_MLD, "%s: warning: ia is NULL", __func__);
3204 mh = m_gethdr(M_NOWAIT, MT_DATA);
3207 ifa_free(&ia->ia_ifa);
3211 M_ALIGN(mh, sizeof(struct ip6_hdr) + sizeof(struct mldv2_report));
3213 mldreclen = m_length(m, NULL);
3214 CTR2(KTR_MLD, "%s: mldreclen is %d", __func__, mldreclen);
3216 mh->m_len = sizeof(struct ip6_hdr) + sizeof(struct mldv2_report);
3217 mh->m_pkthdr.len = sizeof(struct ip6_hdr) +
3218 sizeof(struct mldv2_report) + mldreclen;
3220 ip6 = mtod(mh, struct ip6_hdr *);
3222 ip6->ip6_vfc &= ~IPV6_VERSION_MASK;
3223 ip6->ip6_vfc |= IPV6_VERSION;
3224 ip6->ip6_nxt = IPPROTO_ICMPV6;
3225 ip6->ip6_src = ia ? ia->ia_addr.sin6_addr : in6addr_any;
3227 ifa_free(&ia->ia_ifa);
3228 ip6->ip6_dst = in6addr_linklocal_allv2routers;
3229 /* scope ID will be set in netisr */
3231 mld = (struct mldv2_report *)(ip6 + 1);
3232 mld->mld_type = MLDV2_LISTENER_REPORT;
3235 mld->mld_v2_reserved = 0;
3236 mld->mld_v2_numrecs = htons(m->m_pkthdr.PH_vt.vt_nrecs);
3237 m->m_pkthdr.PH_vt.vt_nrecs = 0;
3240 mld->mld_cksum = in6_cksum(mh, IPPROTO_ICMPV6,
3241 sizeof(struct ip6_hdr), sizeof(struct mldv2_report) + mldreclen);
3247 mld_rec_type_to_str(const int type)
3251 case MLD_CHANGE_TO_EXCLUDE_MODE:
3254 case MLD_CHANGE_TO_INCLUDE_MODE:
3257 case MLD_MODE_IS_EXCLUDE:
3260 case MLD_MODE_IS_INCLUDE:
3263 case MLD_ALLOW_NEW_SOURCES:
3266 case MLD_BLOCK_OLD_SOURCES:
3277 mld_init(void *unused __unused)
3280 CTR1(KTR_MLD, "%s: initializing", __func__);
3283 ip6_initpktopts(&mld_po);
3284 mld_po.ip6po_hlim = 1;
3285 mld_po.ip6po_hbh = &mld_ra.hbh;
3286 mld_po.ip6po_prefer_tempaddr = IP6PO_TEMPADDR_NOTPREFER;
3287 mld_po.ip6po_flags = IP6PO_DONTFRAG;
3289 SYSINIT(mld_init, SI_SUB_PROTO_MC, SI_ORDER_MIDDLE, mld_init, NULL);
3292 mld_uninit(void *unused __unused)
3295 CTR1(KTR_MLD, "%s: tearing down", __func__);
3298 SYSUNINIT(mld_uninit, SI_SUB_PROTO_MC, SI_ORDER_MIDDLE, mld_uninit, NULL);
3301 vnet_mld_init(const void *unused __unused)
3304 CTR1(KTR_MLD, "%s: initializing", __func__);
3306 LIST_INIT(&V_mli_head);
3308 VNET_SYSINIT(vnet_mld_init, SI_SUB_PROTO_MC, SI_ORDER_ANY, vnet_mld_init,
3312 vnet_mld_uninit(const void *unused __unused)
3315 /* This can happen if we shutdown the network stack. */
3316 CTR1(KTR_MLD, "%s: tearing down", __func__);
3318 VNET_SYSUNINIT(vnet_mld_uninit, SI_SUB_PROTO_MC, SI_ORDER_ANY, vnet_mld_uninit,
3322 mld_modevent(module_t mod, int type, void *unused __unused)
3330 return (EOPNOTSUPP);
3335 static moduledata_t mld_mod = {
3340 DECLARE_MODULE(mld, mld_mod, SI_SUB_PROTO_MC, SI_ORDER_ANY);