2 * Copyright (c) 2009 Bruce Simpson.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 * 3. The name of the author may not be used to endorse or promote
13 * products derived from this software without specific prior written
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * $KAME: mld6.c,v 1.27 2001/04/04 05:17:30 itojun Exp $
32 * Copyright (c) 1988 Stephen Deering.
33 * Copyright (c) 1992, 1993
34 * The Regents of the University of California. All rights reserved.
36 * This code is derived from software contributed to Berkeley by
37 * Stephen Deering of Stanford University.
39 * Redistribution and use in source and binary forms, with or without
40 * modification, are permitted provided that the following conditions
42 * 1. Redistributions of source code must retain the above copyright
43 * notice, this list of conditions and the following disclaimer.
44 * 2. Redistributions in binary form must reproduce the above copyright
45 * notice, this list of conditions and the following disclaimer in the
46 * documentation and/or other materials provided with the distribution.
47 * 4. Neither the name of the University nor the names of its contributors
48 * may be used to endorse or promote products derived from this software
49 * without specific prior written permission.
51 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
52 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
53 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
54 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
55 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
56 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
57 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
58 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
59 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
60 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
63 * @(#)igmp.c 8.1 (Berkeley) 7/19/93
66 #include <sys/cdefs.h>
67 __FBSDID("$FreeBSD$");
70 #include "opt_inet6.h"
72 #include <sys/param.h>
73 #include <sys/systm.h>
75 #include <sys/socket.h>
76 #include <sys/protosw.h>
77 #include <sys/sysctl.h>
78 #include <sys/kernel.h>
79 #include <sys/callout.h>
80 #include <sys/malloc.h>
81 #include <sys/module.h>
85 #include <net/route.h>
88 #include <netinet/in.h>
89 #include <netinet/in_var.h>
90 #include <netinet6/in6_var.h>
91 #include <netinet/ip6.h>
92 #include <netinet6/ip6_var.h>
93 #include <netinet6/scope6_var.h>
94 #include <netinet/icmp6.h>
95 #include <netinet6/mld6.h>
96 #include <netinet6/mld6_var.h>
98 #include <security/mac/mac_framework.h>
101 #define KTR_MLD KTR_INET6
104 static struct mld_ifinfo *
105 mli_alloc_locked(struct ifnet *);
106 static void mli_delete_locked(const struct ifnet *);
107 static void mld_dispatch_packet(struct mbuf *);
108 static void mld_dispatch_queue(struct ifqueue *, int);
109 static void mld_final_leave(struct in6_multi *, struct mld_ifinfo *);
110 static void mld_fasttimo_vnet(void);
111 static int mld_handle_state_change(struct in6_multi *,
112 struct mld_ifinfo *);
113 static int mld_initial_join(struct in6_multi *, struct mld_ifinfo *,
116 static char * mld_rec_type_to_str(const int);
118 static void mld_set_version(struct mld_ifinfo *, const int);
119 static void mld_slowtimo_vnet(void);
120 static int mld_v1_input_query(struct ifnet *, const struct ip6_hdr *,
121 /*const*/ struct mld_hdr *);
122 static int mld_v1_input_report(struct ifnet *, const struct ip6_hdr *,
123 /*const*/ struct mld_hdr *);
124 static void mld_v1_process_group_timer(struct mld_ifinfo *,
126 static void mld_v1_process_querier_timers(struct mld_ifinfo *);
127 static int mld_v1_transmit_report(struct in6_multi *, const int);
128 static void mld_v1_update_group(struct in6_multi *, const int);
129 static void mld_v2_cancel_link_timers(struct mld_ifinfo *);
130 static void mld_v2_dispatch_general_query(struct mld_ifinfo *);
132 mld_v2_encap_report(struct ifnet *, struct mbuf *);
133 static int mld_v2_enqueue_filter_change(struct ifqueue *,
135 static int mld_v2_enqueue_group_record(struct ifqueue *,
136 struct in6_multi *, const int, const int, const int,
138 static int mld_v2_input_query(struct ifnet *, const struct ip6_hdr *,
139 struct mbuf *, const int, const int);
140 static int mld_v2_merge_state_changes(struct in6_multi *,
142 static void mld_v2_process_group_timers(struct mld_ifinfo *,
143 struct ifqueue *, struct ifqueue *,
144 struct in6_multi *, const int);
145 static int mld_v2_process_group_query(struct in6_multi *,
146 struct mld_ifinfo *mli, int, struct mbuf *, const int);
147 static int sysctl_mld_gsr(SYSCTL_HANDLER_ARGS);
148 static int sysctl_mld_ifinfo(SYSCTL_HANDLER_ARGS);
151 * Normative references: RFC 2710, RFC 3590, RFC 3810.
154 * * The MLD subsystem lock ends up being system-wide for the moment,
155 * but could be per-VIMAGE later on.
156 * * The permitted lock order is: IN6_MULTI_LOCK, MLD_LOCK, IF_ADDR_LOCK.
157 * Any may be taken independently; if any are held at the same
158 * time, the above lock order must be followed.
159 * * IN6_MULTI_LOCK covers in_multi.
160 * * MLD_LOCK covers per-link state and any global variables in this file.
161 * * IF_ADDR_LOCK covers if_multiaddrs, which is used for a variety of
162 * per-link state iterators.
165 * A special case for IPv6 is the in6_setscope() routine. ip6_output()
166 * will not accept an ifp; it wants an embedded scope ID, unlike
167 * ip_output(), which happily takes the ifp given to it. The embedded
168 * scope ID is only used by MLD to select the outgoing interface.
170 * During interface attach and detach, MLD will take MLD_LOCK *after*
171 * the IF_AFDATA_LOCK.
172 * As in6_setscope() takes IF_AFDATA_LOCK then SCOPE_LOCK, we can't call
173 * it with MLD_LOCK held without triggering an LOR. A netisr with indirect
174 * dispatch could work around this, but we'd rather not do that, as it
175 * can introduce other races.
177 * As such, we exploit the fact that the scope ID is just the interface
178 * index, and embed it in the IPv6 destination address accordingly.
179 * This is potentially NOT VALID for MLDv1 reports, as they
180 * are always sent to the multicast group itself; as MLDv2
181 * reports are always sent to ff02::16, this is not an issue
182 * when MLDv2 is in use.
184 * This does not however eliminate the LOR when ip6_output() itself
185 * calls in6_setscope() internally whilst MLD_LOCK is held. This will
186 * trigger a LOR warning in WITNESS when the ifnet is detached.
188 * The right answer is probably to make IF_AFDATA_LOCK an rwlock, given
189 * how it's used across the network stack. Here we're simply exploiting
190 * the fact that MLD runs at a similar layer in the stack to scope6.c.
193 * * Each in6_multi corresponds to an ifp, and each ifp corresponds
194 * to a vnet in ifp->if_vnet.
196 static struct mtx mld_mtx;
197 MALLOC_DEFINE(M_MLD, "mld", "mld state");
199 #define MLD_EMBEDSCOPE(pin6, zoneid) \
200 if (IN6_IS_SCOPE_LINKLOCAL(pin6) || \
201 IN6_IS_ADDR_MC_INTFACELOCAL(pin6)) \
202 (pin6)->s6_addr16[1] = htons((zoneid) & 0xFFFF) \
205 * VIMAGE-wide globals.
207 static VNET_DEFINE(struct timeval, mld_gsrdelay) = {10, 0};
208 static VNET_DEFINE(LIST_HEAD(, mld_ifinfo), mli_head);
209 static VNET_DEFINE(int, interface_timers_running6);
210 static VNET_DEFINE(int, state_change_timers_running6);
211 static VNET_DEFINE(int, current_state_timers_running6);
213 #define V_mld_gsrdelay VNET(mld_gsrdelay)
214 #define V_mli_head VNET(mli_head)
215 #define V_interface_timers_running6 VNET(interface_timers_running6)
216 #define V_state_change_timers_running6 VNET(state_change_timers_running6)
217 #define V_current_state_timers_running6 VNET(current_state_timers_running6)
219 SYSCTL_DECL(_net_inet6); /* Note: Not in any common header. */
221 SYSCTL_NODE(_net_inet6, OID_AUTO, mld, CTLFLAG_RW, 0,
222 "IPv6 Multicast Listener Discovery");
225 * Virtualized sysctls.
227 SYSCTL_VNET_PROC(_net_inet6_mld, OID_AUTO, gsrdelay,
228 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
229 &VNET_NAME(mld_gsrdelay.tv_sec), 0, sysctl_mld_gsr, "I",
230 "Rate limit for MLDv2 Group-and-Source queries in seconds");
233 * Non-virtualized sysctls.
235 SYSCTL_NODE(_net_inet6_mld, OID_AUTO, ifinfo, CTLFLAG_RD | CTLFLAG_MPSAFE,
236 sysctl_mld_ifinfo, "Per-interface MLDv2 state");
238 static int mld_v1enable = 1;
239 SYSCTL_INT(_net_inet6_mld, OID_AUTO, v1enable, CTLFLAG_RW,
240 &mld_v1enable, 0, "Enable fallback to MLDv1");
241 TUNABLE_INT("net.inet6.mld.v1enable", &mld_v1enable);
243 static int mld_use_allow = 1;
244 SYSCTL_INT(_net_inet6_mld, OID_AUTO, use_allow, CTLFLAG_RW,
245 &mld_use_allow, 0, "Use ALLOW/BLOCK for RFC 4604 SSM joins/leaves");
246 TUNABLE_INT("net.inet6.mld.use_allow", &mld_use_allow);
249 * Packed Router Alert option structure declaration.
254 struct ip6_opt_router ra;
258 * Router Alert hop-by-hop option header.
260 static struct mld_raopt mld_ra = {
262 .pad = { .ip6o_type = IP6OPT_PADN, 0 },
264 .ip6or_type = IP6OPT_ROUTER_ALERT,
265 .ip6or_len = IP6OPT_RTALERT_LEN - 2,
266 .ip6or_value[0] = ((IP6OPT_RTALERT_MLD >> 8) & 0xFF),
267 .ip6or_value[1] = (IP6OPT_RTALERT_MLD & 0xFF)
270 static struct ip6_pktopts mld_po;
273 mld_save_context(struct mbuf *m, struct ifnet *ifp)
277 m->m_pkthdr.header = ifp->if_vnet;
279 m->m_pkthdr.flowid = ifp->if_index;
283 mld_scrub_context(struct mbuf *m)
286 m->m_pkthdr.header = NULL;
287 m->m_pkthdr.flowid = 0;
291 * Restore context from a queued output chain.
292 * Return saved ifindex.
294 * VIMAGE: The assertion is there to make sure that we
295 * actually called CURVNET_SET() with what's in the mbuf chain.
297 static __inline uint32_t
298 mld_restore_context(struct mbuf *m)
301 #if defined(VIMAGE) && defined(INVARIANTS)
302 KASSERT(curvnet == m->m_pkthdr.header,
303 ("%s: called when curvnet was not restored", __func__));
305 return (m->m_pkthdr.flowid);
309 * Retrieve or set threshold between group-source queries in seconds.
311 * VIMAGE: Assume curvnet set by caller.
312 * SMPng: NOTE: Serialized by MLD lock.
315 sysctl_mld_gsr(SYSCTL_HANDLER_ARGS)
320 error = sysctl_wire_old_buffer(req, sizeof(int));
326 i = V_mld_gsrdelay.tv_sec;
328 error = sysctl_handle_int(oidp, &i, 0, req);
329 if (error || !req->newptr)
332 if (i < -1 || i >= 60) {
337 CTR2(KTR_MLD, "change mld_gsrdelay from %d to %d",
338 V_mld_gsrdelay.tv_sec, i);
339 V_mld_gsrdelay.tv_sec = i;
347 * Expose struct mld_ifinfo to userland, keyed by ifindex.
348 * For use by ifmcstat(8).
350 * SMPng: NOTE: Does an unlocked ifindex space read.
351 * VIMAGE: Assume curvnet set by caller. The node handler itself
352 * is not directly virtualized.
355 sysctl_mld_ifinfo(SYSCTL_HANDLER_ARGS)
361 struct mld_ifinfo *mli;
366 if (req->newptr != NULL)
372 error = sysctl_wire_old_buffer(req, sizeof(struct mld_ifinfo));
379 if (name[0] <= 0 || name[0] > V_if_index) {
386 ifp = ifnet_byindex(name[0]);
390 LIST_FOREACH(mli, &V_mli_head, mli_link) {
391 if (ifp == mli->mli_ifp) {
392 error = SYSCTL_OUT(req, mli,
393 sizeof(struct mld_ifinfo));
405 * Dispatch an entire queue of pending packet chains.
406 * VIMAGE: Assumes the vnet pointer has been set.
409 mld_dispatch_queue(struct ifqueue *ifq, int limit)
417 CTR3(KTR_MLD, "%s: dispatch %p from %p", __func__, ifq, m);
418 mld_dispatch_packet(m);
425 * Filter outgoing MLD report state by group.
427 * Reports are ALWAYS suppressed for ALL-HOSTS (ff02::1)
428 * and node-local addresses. However, kernel and socket consumers
429 * always embed the KAME scope ID in the address provided, so strip it
430 * when performing comparison.
431 * Note: This is not the same as the *multicast* scope.
433 * Return zero if the given group is one for which MLD reports
434 * should be suppressed, or non-zero if reports should be issued.
437 mld_is_addr_reported(const struct in6_addr *addr)
440 KASSERT(IN6_IS_ADDR_MULTICAST(addr), ("%s: not multicast", __func__));
442 if (IPV6_ADDR_MC_SCOPE(addr) == IPV6_ADDR_SCOPE_NODELOCAL)
445 if (IPV6_ADDR_MC_SCOPE(addr) == IPV6_ADDR_SCOPE_LINKLOCAL) {
446 struct in6_addr tmp = *addr;
447 in6_clearscope(&tmp);
448 if (IN6_ARE_ADDR_EQUAL(&tmp, &in6addr_linklocal_allnodes))
456 * Attach MLD when PF_INET6 is attached to an interface.
458 * SMPng: Normally called with IF_AFDATA_LOCK held.
461 mld_domifattach(struct ifnet *ifp)
463 struct mld_ifinfo *mli;
465 CTR3(KTR_MLD, "%s: called for ifp %p(%s)",
466 __func__, ifp, ifp->if_xname);
470 mli = mli_alloc_locked(ifp);
471 if (!(ifp->if_flags & IFF_MULTICAST))
472 mli->mli_flags |= MLIF_SILENT;
474 mli->mli_flags |= MLIF_USEALLOW;
482 * VIMAGE: assume curvnet set by caller.
484 static struct mld_ifinfo *
485 mli_alloc_locked(/*const*/ struct ifnet *ifp)
487 struct mld_ifinfo *mli;
491 mli = malloc(sizeof(struct mld_ifinfo), M_MLD, M_NOWAIT|M_ZERO);
496 mli->mli_version = MLD_VERSION_2;
498 mli->mli_rv = MLD_RV_INIT;
499 mli->mli_qi = MLD_QI_INIT;
500 mli->mli_qri = MLD_QRI_INIT;
501 mli->mli_uri = MLD_URI_INIT;
503 SLIST_INIT(&mli->mli_relinmhead);
506 * Responses to general queries are subject to bounds.
508 IFQ_SET_MAXLEN(&mli->mli_gq, MLD_MAX_RESPONSE_PACKETS);
510 LIST_INSERT_HEAD(&V_mli_head, mli, mli_link);
512 CTR2(KTR_MLD, "allocate mld_ifinfo for ifp %p(%s)",
522 * NOTE: Some finalization tasks need to run before the protocol domain
523 * is detached, but also before the link layer does its cleanup.
524 * Run before link-layer cleanup; cleanup groups, but do not free MLD state.
526 * SMPng: Caller must hold IN6_MULTI_LOCK().
527 * Must take IF_ADDR_LOCK() to cover if_multiaddrs iterator.
528 * XXX This routine is also bitten by unlocked ifma_protospec access.
531 mld_ifdetach(struct ifnet *ifp)
533 struct mld_ifinfo *mli;
534 struct ifmultiaddr *ifma;
535 struct in6_multi *inm, *tinm;
537 CTR3(KTR_MLD, "%s: called for ifp %p(%s)", __func__, ifp,
540 IN6_MULTI_LOCK_ASSERT();
543 mli = MLD_IFINFO(ifp);
544 if (mli->mli_version == MLD_VERSION_2) {
546 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
547 if (ifma->ifma_addr->sa_family != AF_INET6 ||
548 ifma->ifma_protospec == NULL)
550 inm = (struct in6_multi *)ifma->ifma_protospec;
551 if (inm->in6m_state == MLD_LEAVING_MEMBER) {
552 SLIST_INSERT_HEAD(&mli->mli_relinmhead,
555 in6m_clear_recorded(inm);
557 IF_ADDR_RUNLOCK(ifp);
558 SLIST_FOREACH_SAFE(inm, &mli->mli_relinmhead, in6m_nrele,
560 SLIST_REMOVE_HEAD(&mli->mli_relinmhead, in6m_nrele);
561 in6m_release_locked(inm);
569 * Hook for domifdetach.
570 * Runs after link-layer cleanup; free MLD state.
572 * SMPng: Normally called with IF_AFDATA_LOCK held.
575 mld_domifdetach(struct ifnet *ifp)
578 CTR3(KTR_MLD, "%s: called for ifp %p(%s)",
579 __func__, ifp, ifp->if_xname);
582 mli_delete_locked(ifp);
587 mli_delete_locked(const struct ifnet *ifp)
589 struct mld_ifinfo *mli, *tmli;
591 CTR3(KTR_MLD, "%s: freeing mld_ifinfo for ifp %p(%s)",
592 __func__, ifp, ifp->if_xname);
596 LIST_FOREACH_SAFE(mli, &V_mli_head, mli_link, tmli) {
597 if (mli->mli_ifp == ifp) {
599 * Free deferred General Query responses.
601 _IF_DRAIN(&mli->mli_gq);
603 LIST_REMOVE(mli, mli_link);
605 KASSERT(SLIST_EMPTY(&mli->mli_relinmhead),
606 ("%s: there are dangling in_multi references",
614 panic("%s: mld_ifinfo not found for ifp %p\n", __func__, ifp);
619 * Process a received MLDv1 general or address-specific query.
620 * Assumes that the query header has been pulled up to sizeof(mld_hdr).
622 * NOTE: Can't be fully const correct as we temporarily embed scope ID in
623 * mld_addr. This is OK as we own the mbuf chain.
626 mld_v1_input_query(struct ifnet *ifp, const struct ip6_hdr *ip6,
627 /*const*/ struct mld_hdr *mld)
629 struct ifmultiaddr *ifma;
630 struct mld_ifinfo *mli;
631 struct in6_multi *inm;
632 int is_general_query;
635 char ip6tbuf[INET6_ADDRSTRLEN];
638 is_general_query = 0;
641 CTR3(KTR_MLD, "ignore v1 query %s on ifp %p(%s)",
642 ip6_sprintf(ip6tbuf, &mld->mld_addr),
648 * RFC3810 Section 6.2: MLD queries must originate from
649 * a router's link-local address.
651 if (!IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_src)) {
652 CTR3(KTR_MLD, "ignore v1 query src %s on ifp %p(%s)",
653 ip6_sprintf(ip6tbuf, &ip6->ip6_src),
659 * Do address field validation upfront before we accept
662 if (IN6_IS_ADDR_UNSPECIFIED(&mld->mld_addr)) {
664 * MLDv1 General Query.
665 * If this was not sent to the all-nodes group, ignore it.
670 in6_clearscope(&dst);
671 if (!IN6_ARE_ADDR_EQUAL(&dst, &in6addr_linklocal_allnodes))
673 is_general_query = 1;
676 * Embed scope ID of receiving interface in MLD query for
677 * lookup whilst we don't hold other locks.
679 in6_setscope(&mld->mld_addr, ifp, NULL);
686 * Switch to MLDv1 host compatibility mode.
688 mli = MLD_IFINFO(ifp);
689 KASSERT(mli != NULL, ("%s: no mld_ifinfo for ifp %p", __func__, ifp));
690 mld_set_version(mli, MLD_VERSION_1);
692 timer = (ntohs(mld->mld_maxdelay) * PR_FASTHZ) / MLD_TIMER_SCALE;
697 if (is_general_query) {
699 * For each reporting group joined on this
700 * interface, kick the report timer.
702 CTR2(KTR_MLD, "process v1 general query on ifp %p(%s)",
704 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
705 if (ifma->ifma_addr->sa_family != AF_INET6 ||
706 ifma->ifma_protospec == NULL)
708 inm = (struct in6_multi *)ifma->ifma_protospec;
709 mld_v1_update_group(inm, timer);
713 * MLDv1 Group-Specific Query.
714 * If this is a group-specific MLDv1 query, we need only
715 * look up the single group to process it.
717 inm = in6m_lookup_locked(ifp, &mld->mld_addr);
719 CTR3(KTR_MLD, "process v1 query %s on ifp %p(%s)",
720 ip6_sprintf(ip6tbuf, &mld->mld_addr),
722 mld_v1_update_group(inm, timer);
724 /* XXX Clear embedded scope ID as userland won't expect it. */
725 in6_clearscope(&mld->mld_addr);
728 IF_ADDR_RUNLOCK(ifp);
736 * Update the report timer on a group in response to an MLDv1 query.
738 * If we are becoming the reporting member for this group, start the timer.
739 * If we already are the reporting member for this group, and timer is
740 * below the threshold, reset it.
742 * We may be updating the group for the first time since we switched
743 * to MLDv2. If we are, then we must clear any recorded source lists,
744 * and transition to REPORTING state; the group timer is overloaded
745 * for group and group-source query responses.
747 * Unlike MLDv2, the delay per group should be jittered
748 * to avoid bursts of MLDv1 reports.
751 mld_v1_update_group(struct in6_multi *inm, const int timer)
754 char ip6tbuf[INET6_ADDRSTRLEN];
757 CTR4(KTR_MLD, "%s: %s/%s timer=%d", __func__,
758 ip6_sprintf(ip6tbuf, &inm->in6m_addr),
759 inm->in6m_ifp->if_xname, timer);
761 IN6_MULTI_LOCK_ASSERT();
763 switch (inm->in6m_state) {
765 case MLD_SILENT_MEMBER:
767 case MLD_REPORTING_MEMBER:
768 if (inm->in6m_timer != 0 &&
769 inm->in6m_timer <= timer) {
770 CTR1(KTR_MLD, "%s: REPORTING and timer running, "
771 "skipping.", __func__);
775 case MLD_SG_QUERY_PENDING_MEMBER:
776 case MLD_G_QUERY_PENDING_MEMBER:
777 case MLD_IDLE_MEMBER:
778 case MLD_LAZY_MEMBER:
779 case MLD_AWAKENING_MEMBER:
780 CTR1(KTR_MLD, "%s: ->REPORTING", __func__);
781 inm->in6m_state = MLD_REPORTING_MEMBER;
782 inm->in6m_timer = MLD_RANDOM_DELAY(timer);
783 V_current_state_timers_running6 = 1;
785 case MLD_SLEEPING_MEMBER:
786 CTR1(KTR_MLD, "%s: ->AWAKENING", __func__);
787 inm->in6m_state = MLD_AWAKENING_MEMBER;
789 case MLD_LEAVING_MEMBER:
795 * Process a received MLDv2 general, group-specific or
796 * group-and-source-specific query.
798 * Assumes that the query header has been pulled up to sizeof(mldv2_query).
800 * Return 0 if successful, otherwise an appropriate error code is returned.
803 mld_v2_input_query(struct ifnet *ifp, const struct ip6_hdr *ip6,
804 struct mbuf *m, const int off, const int icmp6len)
806 struct mld_ifinfo *mli;
807 struct mldv2_query *mld;
808 struct in6_multi *inm;
809 uint32_t maxdelay, nsrc, qqi;
810 int is_general_query;
814 char ip6tbuf[INET6_ADDRSTRLEN];
817 is_general_query = 0;
820 * RFC3810 Section 6.2: MLD queries must originate from
821 * a router's link-local address.
823 if (!IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_src)) {
824 CTR3(KTR_MLD, "ignore v1 query src %s on ifp %p(%s)",
825 ip6_sprintf(ip6tbuf, &ip6->ip6_src),
830 CTR2(KTR_MLD, "input v2 query on ifp %p(%s)", ifp, ifp->if_xname);
832 mld = (struct mldv2_query *)(mtod(m, uint8_t *) + off);
834 maxdelay = ntohs(mld->mld_maxdelay); /* in 1/10ths of a second */
835 if (maxdelay >= 32678) {
836 maxdelay = (MLD_MRC_MANT(maxdelay) | 0x1000) <<
837 (MLD_MRC_EXP(maxdelay) + 3);
839 timer = (maxdelay * PR_FASTHZ) / MLD_TIMER_SCALE;
843 qrv = MLD_QRV(mld->mld_misc);
845 CTR3(KTR_MLD, "%s: clamping qrv %d to %d", __func__,
852 qqi = MLD_QQIC_MANT(mld->mld_qqi) <<
853 (MLD_QQIC_EXP(mld->mld_qqi) + 3);
856 nsrc = ntohs(mld->mld_numsrc);
857 if (nsrc > MLD_MAX_GS_SOURCES)
859 if (icmp6len < sizeof(struct mldv2_query) +
860 (nsrc * sizeof(struct in6_addr)))
864 * Do further input validation upfront to avoid resetting timers
865 * should we need to discard this query.
867 if (IN6_IS_ADDR_UNSPECIFIED(&mld->mld_addr)) {
869 * General Queries SHOULD be directed to ff02::1.
870 * A general query with a source list has undefined
871 * behaviour; discard it.
876 in6_clearscope(&dst);
877 if (!IN6_ARE_ADDR_EQUAL(&dst, &in6addr_linklocal_allnodes) ||
880 is_general_query = 1;
883 * Embed scope ID of receiving interface in MLD query for
884 * lookup whilst we don't hold other locks (due to KAME
885 * locking lameness). We own this mbuf chain just now.
887 in6_setscope(&mld->mld_addr, ifp, NULL);
893 mli = MLD_IFINFO(ifp);
894 KASSERT(mli != NULL, ("%s: no mld_ifinfo for ifp %p", __func__, ifp));
897 * Discard the v2 query if we're in Compatibility Mode.
898 * The RFC is pretty clear that hosts need to stay in MLDv1 mode
899 * until the Old Version Querier Present timer expires.
901 if (mli->mli_version != MLD_VERSION_2)
904 mld_set_version(mli, MLD_VERSION_2);
907 mli->mli_qri = maxdelay;
909 CTR4(KTR_MLD, "%s: qrv %d qi %d maxdelay %d", __func__, qrv, qqi,
912 if (is_general_query) {
914 * MLDv2 General Query.
916 * Schedule a current-state report on this ifp for
917 * all groups, possibly containing source lists.
919 * If there is a pending General Query response
920 * scheduled earlier than the selected delay, do
921 * not schedule any other reports.
922 * Otherwise, reset the interface timer.
924 CTR2(KTR_MLD, "process v2 general query on ifp %p(%s)",
926 if (mli->mli_v2_timer == 0 || mli->mli_v2_timer >= timer) {
927 mli->mli_v2_timer = MLD_RANDOM_DELAY(timer);
928 V_interface_timers_running6 = 1;
932 * MLDv2 Group-specific or Group-and-source-specific Query.
934 * Group-source-specific queries are throttled on
935 * a per-group basis to defeat denial-of-service attempts.
936 * Queries for groups we are not a member of on this
937 * link are simply ignored.
940 inm = in6m_lookup_locked(ifp, &mld->mld_addr);
942 IF_ADDR_RUNLOCK(ifp);
946 if (!ratecheck(&inm->in6m_lastgsrtv,
948 CTR1(KTR_MLD, "%s: GS query throttled.",
950 IF_ADDR_RUNLOCK(ifp);
954 CTR2(KTR_MLD, "process v2 group query on ifp %p(%s)",
957 * If there is a pending General Query response
958 * scheduled sooner than the selected delay, no
959 * further report need be scheduled.
960 * Otherwise, prepare to respond to the
961 * group-specific or group-and-source query.
963 if (mli->mli_v2_timer == 0 || mli->mli_v2_timer >= timer)
964 mld_v2_process_group_query(inm, mli, timer, m, off);
966 /* XXX Clear embedded scope ID as userland won't expect it. */
967 in6_clearscope(&mld->mld_addr);
968 IF_ADDR_RUNLOCK(ifp);
979 * Process a recieved MLDv2 group-specific or group-and-source-specific
981 * Return <0 if any error occured. Currently this is ignored.
984 mld_v2_process_group_query(struct in6_multi *inm, struct mld_ifinfo *mli,
985 int timer, struct mbuf *m0, const int off)
987 struct mldv2_query *mld;
991 IN6_MULTI_LOCK_ASSERT();
995 mld = (struct mldv2_query *)(mtod(m0, uint8_t *) + off);
997 switch (inm->in6m_state) {
999 case MLD_SILENT_MEMBER:
1000 case MLD_SLEEPING_MEMBER:
1001 case MLD_LAZY_MEMBER:
1002 case MLD_AWAKENING_MEMBER:
1003 case MLD_IDLE_MEMBER:
1004 case MLD_LEAVING_MEMBER:
1007 case MLD_REPORTING_MEMBER:
1008 case MLD_G_QUERY_PENDING_MEMBER:
1009 case MLD_SG_QUERY_PENDING_MEMBER:
1013 nsrc = ntohs(mld->mld_numsrc);
1016 * Deal with group-specific queries upfront.
1017 * If any group query is already pending, purge any recorded
1018 * source-list state if it exists, and schedule a query response
1019 * for this group-specific query.
1022 if (inm->in6m_state == MLD_G_QUERY_PENDING_MEMBER ||
1023 inm->in6m_state == MLD_SG_QUERY_PENDING_MEMBER) {
1024 in6m_clear_recorded(inm);
1025 timer = min(inm->in6m_timer, timer);
1027 inm->in6m_state = MLD_G_QUERY_PENDING_MEMBER;
1028 inm->in6m_timer = MLD_RANDOM_DELAY(timer);
1029 V_current_state_timers_running6 = 1;
1034 * Deal with the case where a group-and-source-specific query has
1035 * been received but a group-specific query is already pending.
1037 if (inm->in6m_state == MLD_G_QUERY_PENDING_MEMBER) {
1038 timer = min(inm->in6m_timer, timer);
1039 inm->in6m_timer = MLD_RANDOM_DELAY(timer);
1040 V_current_state_timers_running6 = 1;
1045 * Finally, deal with the case where a group-and-source-specific
1046 * query has been received, where a response to a previous g-s-r
1047 * query exists, or none exists.
1048 * In this case, we need to parse the source-list which the Querier
1049 * has provided us with and check if we have any source list filter
1050 * entries at T1 for these sources. If we do not, there is no need
1051 * schedule a report and the query may be dropped.
1052 * If we do, we must record them and schedule a current-state
1053 * report for those sources.
1055 if (inm->in6m_nsrc > 0) {
1062 soff = off + sizeof(struct mldv2_query);
1064 for (i = 0; i < nsrc; i++) {
1065 sp = mtod(m, uint8_t *) + soff;
1066 retval = in6m_record_source(inm,
1067 (const struct in6_addr *)sp);
1070 nrecorded += retval;
1071 soff += sizeof(struct in6_addr);
1072 if (soff >= m->m_len) {
1073 soff = soff - m->m_len;
1079 if (nrecorded > 0) {
1081 "%s: schedule response to SG query", __func__);
1082 inm->in6m_state = MLD_SG_QUERY_PENDING_MEMBER;
1083 inm->in6m_timer = MLD_RANDOM_DELAY(timer);
1084 V_current_state_timers_running6 = 1;
1092 * Process a received MLDv1 host membership report.
1093 * Assumes mld points to mld_hdr in pulled up mbuf chain.
1095 * NOTE: Can't be fully const correct as we temporarily embed scope ID in
1096 * mld_addr. This is OK as we own the mbuf chain.
1099 mld_v1_input_report(struct ifnet *ifp, const struct ip6_hdr *ip6,
1100 /*const*/ struct mld_hdr *mld)
1102 struct in6_addr src, dst;
1103 struct in6_ifaddr *ia;
1104 struct in6_multi *inm;
1106 char ip6tbuf[INET6_ADDRSTRLEN];
1109 if (!mld_v1enable) {
1110 CTR3(KTR_MLD, "ignore v1 report %s on ifp %p(%s)",
1111 ip6_sprintf(ip6tbuf, &mld->mld_addr),
1112 ifp, ifp->if_xname);
1116 if (ifp->if_flags & IFF_LOOPBACK)
1120 * MLDv1 reports must originate from a host's link-local address,
1121 * or the unspecified address (when booting).
1124 in6_clearscope(&src);
1125 if (!IN6_IS_SCOPE_LINKLOCAL(&src) && !IN6_IS_ADDR_UNSPECIFIED(&src)) {
1126 CTR3(KTR_MLD, "ignore v1 query src %s on ifp %p(%s)",
1127 ip6_sprintf(ip6tbuf, &ip6->ip6_src),
1128 ifp, ifp->if_xname);
1133 * RFC2710 Section 4: MLDv1 reports must pertain to a multicast
1134 * group, and must be directed to the group itself.
1137 in6_clearscope(&dst);
1138 if (!IN6_IS_ADDR_MULTICAST(&mld->mld_addr) ||
1139 !IN6_ARE_ADDR_EQUAL(&mld->mld_addr, &dst)) {
1140 CTR3(KTR_MLD, "ignore v1 query dst %s on ifp %p(%s)",
1141 ip6_sprintf(ip6tbuf, &ip6->ip6_dst),
1142 ifp, ifp->if_xname);
1147 * Make sure we don't hear our own membership report, as fast
1148 * leave requires knowing that we are the only member of a
1149 * group. Assume we used the link-local address if available,
1150 * otherwise look for ::.
1152 * XXX Note that scope ID comparison is needed for the address
1153 * returned by in6ifa_ifpforlinklocal(), but SHOULD NOT be
1154 * performed for the on-wire address.
1156 ia = in6ifa_ifpforlinklocal(ifp, IN6_IFF_NOTREADY|IN6_IFF_ANYCAST);
1157 if ((ia && IN6_ARE_ADDR_EQUAL(&ip6->ip6_src, IA6_IN6(ia))) ||
1158 (ia == NULL && IN6_IS_ADDR_UNSPECIFIED(&src))) {
1160 ifa_free(&ia->ia_ifa);
1164 ifa_free(&ia->ia_ifa);
1166 CTR3(KTR_MLD, "process v1 report %s on ifp %p(%s)",
1167 ip6_sprintf(ip6tbuf, &mld->mld_addr), ifp, ifp->if_xname);
1170 * Embed scope ID of receiving interface in MLD query for lookup
1171 * whilst we don't hold other locks (due to KAME locking lameness).
1173 if (!IN6_IS_ADDR_UNSPECIFIED(&mld->mld_addr))
1174 in6_setscope(&mld->mld_addr, ifp, NULL);
1181 * MLDv1 report suppression.
1182 * If we are a member of this group, and our membership should be
1183 * reported, and our group timer is pending or about to be reset,
1184 * stop our group timer by transitioning to the 'lazy' state.
1186 inm = in6m_lookup_locked(ifp, &mld->mld_addr);
1188 struct mld_ifinfo *mli;
1190 mli = inm->in6m_mli;
1191 KASSERT(mli != NULL,
1192 ("%s: no mli for ifp %p", __func__, ifp));
1195 * If we are in MLDv2 host mode, do not allow the
1196 * other host's MLDv1 report to suppress our reports.
1198 if (mli->mli_version == MLD_VERSION_2)
1201 inm->in6m_timer = 0;
1203 switch (inm->in6m_state) {
1204 case MLD_NOT_MEMBER:
1205 case MLD_SILENT_MEMBER:
1206 case MLD_SLEEPING_MEMBER:
1208 case MLD_REPORTING_MEMBER:
1209 case MLD_IDLE_MEMBER:
1210 case MLD_AWAKENING_MEMBER:
1212 "report suppressed for %s on ifp %p(%s)",
1213 ip6_sprintf(ip6tbuf, &mld->mld_addr),
1214 ifp, ifp->if_xname);
1215 case MLD_LAZY_MEMBER:
1216 inm->in6m_state = MLD_LAZY_MEMBER;
1218 case MLD_G_QUERY_PENDING_MEMBER:
1219 case MLD_SG_QUERY_PENDING_MEMBER:
1220 case MLD_LEAVING_MEMBER:
1226 IF_ADDR_RUNLOCK(ifp);
1230 /* XXX Clear embedded scope ID as userland won't expect it. */
1231 in6_clearscope(&mld->mld_addr);
1239 * Assume query messages which fit in a single ICMPv6 message header
1240 * have been pulled up.
1241 * Assume that userland will want to see the message, even if it
1242 * otherwise fails kernel input validation; do not free it.
1243 * Pullup may however free the mbuf chain m if it fails.
1245 * Return IPPROTO_DONE if we freed m. Otherwise, return 0.
1248 mld_input(struct mbuf *m, int off, int icmp6len)
1251 struct ip6_hdr *ip6;
1252 struct mld_hdr *mld;
1255 CTR3(KTR_MLD, "%s: called w/mbuf (%p,%d)", __func__, m, off);
1257 ifp = m->m_pkthdr.rcvif;
1259 ip6 = mtod(m, struct ip6_hdr *);
1261 /* Pullup to appropriate size. */
1262 mld = (struct mld_hdr *)(mtod(m, uint8_t *) + off);
1263 if (mld->mld_type == MLD_LISTENER_QUERY &&
1264 icmp6len >= sizeof(struct mldv2_query)) {
1265 mldlen = sizeof(struct mldv2_query);
1267 mldlen = sizeof(struct mld_hdr);
1269 IP6_EXTHDR_GET(mld, struct mld_hdr *, m, off, mldlen);
1271 ICMP6STAT_INC(icp6s_badlen);
1272 return (IPPROTO_DONE);
1276 * Userland needs to see all of this traffic for implementing
1277 * the endpoint discovery portion of multicast routing.
1279 switch (mld->mld_type) {
1280 case MLD_LISTENER_QUERY:
1281 icmp6_ifstat_inc(ifp, ifs6_in_mldquery);
1282 if (icmp6len == sizeof(struct mld_hdr)) {
1283 if (mld_v1_input_query(ifp, ip6, mld) != 0)
1285 } else if (icmp6len >= sizeof(struct mldv2_query)) {
1286 if (mld_v2_input_query(ifp, ip6, m, off,
1291 case MLD_LISTENER_REPORT:
1292 icmp6_ifstat_inc(ifp, ifs6_in_mldreport);
1293 if (mld_v1_input_report(ifp, ip6, mld) != 0)
1296 case MLDV2_LISTENER_REPORT:
1297 icmp6_ifstat_inc(ifp, ifs6_in_mldreport);
1299 case MLD_LISTENER_DONE:
1300 icmp6_ifstat_inc(ifp, ifs6_in_mlddone);
1310 * Fast timeout handler (global).
1311 * VIMAGE: Timeout handlers are expected to service all vimages.
1316 VNET_ITERATOR_DECL(vnet_iter);
1318 VNET_LIST_RLOCK_NOSLEEP();
1319 VNET_FOREACH(vnet_iter) {
1320 CURVNET_SET(vnet_iter);
1321 mld_fasttimo_vnet();
1324 VNET_LIST_RUNLOCK_NOSLEEP();
1328 * Fast timeout handler (per-vnet).
1330 * VIMAGE: Assume caller has set up our curvnet.
1333 mld_fasttimo_vnet(void)
1335 struct ifqueue scq; /* State-change packets */
1336 struct ifqueue qrq; /* Query response packets */
1338 struct mld_ifinfo *mli;
1339 struct ifmultiaddr *ifma;
1340 struct in6_multi *inm, *tinm;
1346 * Quick check to see if any work needs to be done, in order to
1347 * minimize the overhead of fasttimo processing.
1348 * SMPng: XXX Unlocked reads.
1350 if (!V_current_state_timers_running6 &&
1351 !V_interface_timers_running6 &&
1352 !V_state_change_timers_running6)
1359 * MLDv2 General Query response timer processing.
1361 if (V_interface_timers_running6) {
1362 CTR1(KTR_MLD, "%s: interface timers running", __func__);
1364 V_interface_timers_running6 = 0;
1365 LIST_FOREACH(mli, &V_mli_head, mli_link) {
1366 if (mli->mli_v2_timer == 0) {
1368 } else if (--mli->mli_v2_timer == 0) {
1369 mld_v2_dispatch_general_query(mli);
1371 V_interface_timers_running6 = 1;
1376 if (!V_current_state_timers_running6 &&
1377 !V_state_change_timers_running6)
1380 V_current_state_timers_running6 = 0;
1381 V_state_change_timers_running6 = 0;
1383 CTR1(KTR_MLD, "%s: state change timers running", __func__);
1386 * MLD host report and state-change timer processing.
1387 * Note: Processing a v2 group timer may remove a node.
1389 LIST_FOREACH(mli, &V_mli_head, mli_link) {
1392 if (mli->mli_version == MLD_VERSION_2) {
1393 uri_fasthz = MLD_RANDOM_DELAY(mli->mli_uri *
1396 memset(&qrq, 0, sizeof(struct ifqueue));
1397 IFQ_SET_MAXLEN(&qrq, MLD_MAX_G_GS_PACKETS);
1399 memset(&scq, 0, sizeof(struct ifqueue));
1400 IFQ_SET_MAXLEN(&scq, MLD_MAX_STATE_CHANGE_PACKETS);
1404 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
1405 if (ifma->ifma_addr->sa_family != AF_INET6 ||
1406 ifma->ifma_protospec == NULL)
1408 inm = (struct in6_multi *)ifma->ifma_protospec;
1409 switch (mli->mli_version) {
1411 mld_v1_process_group_timer(mli, inm);
1414 mld_v2_process_group_timers(mli, &qrq,
1415 &scq, inm, uri_fasthz);
1419 IF_ADDR_RUNLOCK(ifp);
1421 switch (mli->mli_version) {
1424 * Transmit reports for this lifecycle. This
1425 * is done while not holding IF_ADDR_LOCK
1426 * since this can call
1427 * in6ifa_ifpforlinklocal() which locks
1428 * IF_ADDR_LOCK internally as well as
1429 * ip6_output() to transmit a packet.
1431 SLIST_FOREACH_SAFE(inm, &mli->mli_relinmhead,
1433 SLIST_REMOVE_HEAD(&mli->mli_relinmhead,
1435 (void)mld_v1_transmit_report(inm,
1436 MLD_LISTENER_REPORT);
1440 mld_dispatch_queue(&qrq, 0);
1441 mld_dispatch_queue(&scq, 0);
1444 * Free the in_multi reference(s) for
1447 SLIST_FOREACH_SAFE(inm, &mli->mli_relinmhead,
1449 SLIST_REMOVE_HEAD(&mli->mli_relinmhead,
1451 in6m_release_locked(inm);
1463 * Update host report group timer.
1464 * Will update the global pending timer flags.
1467 mld_v1_process_group_timer(struct mld_ifinfo *mli, struct in6_multi *inm)
1469 int report_timer_expired;
1471 IN6_MULTI_LOCK_ASSERT();
1474 if (inm->in6m_timer == 0) {
1475 report_timer_expired = 0;
1476 } else if (--inm->in6m_timer == 0) {
1477 report_timer_expired = 1;
1479 V_current_state_timers_running6 = 1;
1483 switch (inm->in6m_state) {
1484 case MLD_NOT_MEMBER:
1485 case MLD_SILENT_MEMBER:
1486 case MLD_IDLE_MEMBER:
1487 case MLD_LAZY_MEMBER:
1488 case MLD_SLEEPING_MEMBER:
1489 case MLD_AWAKENING_MEMBER:
1491 case MLD_REPORTING_MEMBER:
1492 if (report_timer_expired) {
1493 inm->in6m_state = MLD_IDLE_MEMBER;
1494 SLIST_INSERT_HEAD(&mli->mli_relinmhead, inm,
1498 case MLD_G_QUERY_PENDING_MEMBER:
1499 case MLD_SG_QUERY_PENDING_MEMBER:
1500 case MLD_LEAVING_MEMBER:
1506 * Update a group's timers for MLDv2.
1507 * Will update the global pending timer flags.
1508 * Note: Unlocked read from mli.
1511 mld_v2_process_group_timers(struct mld_ifinfo *mli,
1512 struct ifqueue *qrq, struct ifqueue *scq,
1513 struct in6_multi *inm, const int uri_fasthz)
1515 int query_response_timer_expired;
1516 int state_change_retransmit_timer_expired;
1518 char ip6tbuf[INET6_ADDRSTRLEN];
1521 IN6_MULTI_LOCK_ASSERT();
1524 query_response_timer_expired = 0;
1525 state_change_retransmit_timer_expired = 0;
1528 * During a transition from compatibility mode back to MLDv2,
1529 * a group record in REPORTING state may still have its group
1530 * timer active. This is a no-op in this function; it is easier
1531 * to deal with it here than to complicate the slow-timeout path.
1533 if (inm->in6m_timer == 0) {
1534 query_response_timer_expired = 0;
1535 } else if (--inm->in6m_timer == 0) {
1536 query_response_timer_expired = 1;
1538 V_current_state_timers_running6 = 1;
1541 if (inm->in6m_sctimer == 0) {
1542 state_change_retransmit_timer_expired = 0;
1543 } else if (--inm->in6m_sctimer == 0) {
1544 state_change_retransmit_timer_expired = 1;
1546 V_state_change_timers_running6 = 1;
1549 /* We are in fasttimo, so be quick about it. */
1550 if (!state_change_retransmit_timer_expired &&
1551 !query_response_timer_expired)
1554 switch (inm->in6m_state) {
1555 case MLD_NOT_MEMBER:
1556 case MLD_SILENT_MEMBER:
1557 case MLD_SLEEPING_MEMBER:
1558 case MLD_LAZY_MEMBER:
1559 case MLD_AWAKENING_MEMBER:
1560 case MLD_IDLE_MEMBER:
1562 case MLD_G_QUERY_PENDING_MEMBER:
1563 case MLD_SG_QUERY_PENDING_MEMBER:
1565 * Respond to a previously pending Group-Specific
1566 * or Group-and-Source-Specific query by enqueueing
1567 * the appropriate Current-State report for
1568 * immediate transmission.
1570 if (query_response_timer_expired) {
1573 retval = mld_v2_enqueue_group_record(qrq, inm, 0, 1,
1574 (inm->in6m_state == MLD_SG_QUERY_PENDING_MEMBER),
1576 CTR2(KTR_MLD, "%s: enqueue record = %d",
1578 inm->in6m_state = MLD_REPORTING_MEMBER;
1579 in6m_clear_recorded(inm);
1582 case MLD_REPORTING_MEMBER:
1583 case MLD_LEAVING_MEMBER:
1584 if (state_change_retransmit_timer_expired) {
1586 * State-change retransmission timer fired.
1587 * If there are any further pending retransmissions,
1588 * set the global pending state-change flag, and
1591 if (--inm->in6m_scrv > 0) {
1592 inm->in6m_sctimer = uri_fasthz;
1593 V_state_change_timers_running6 = 1;
1596 * Retransmit the previously computed state-change
1597 * report. If there are no further pending
1598 * retransmissions, the mbuf queue will be consumed.
1599 * Update T0 state to T1 as we have now sent
1602 (void)mld_v2_merge_state_changes(inm, scq);
1605 CTR3(KTR_MLD, "%s: T1 -> T0 for %s/%s", __func__,
1606 ip6_sprintf(ip6tbuf, &inm->in6m_addr),
1607 inm->in6m_ifp->if_xname);
1610 * If we are leaving the group for good, make sure
1611 * we release MLD's reference to it.
1612 * This release must be deferred using a SLIST,
1613 * as we are called from a loop which traverses
1614 * the in_ifmultiaddr TAILQ.
1616 if (inm->in6m_state == MLD_LEAVING_MEMBER &&
1617 inm->in6m_scrv == 0) {
1618 inm->in6m_state = MLD_NOT_MEMBER;
1619 SLIST_INSERT_HEAD(&mli->mli_relinmhead,
1628 * Switch to a different version on the given interface,
1629 * as per Section 9.12.
1632 mld_set_version(struct mld_ifinfo *mli, const int version)
1634 int old_version_timer;
1638 CTR4(KTR_MLD, "%s: switching to v%d on ifp %p(%s)", __func__,
1639 version, mli->mli_ifp, mli->mli_ifp->if_xname);
1641 if (version == MLD_VERSION_1) {
1643 * Compute the "Older Version Querier Present" timer as per
1646 old_version_timer = (mli->mli_rv * mli->mli_qi) + mli->mli_qri;
1647 old_version_timer *= PR_SLOWHZ;
1648 mli->mli_v1_timer = old_version_timer;
1651 if (mli->mli_v1_timer > 0 && mli->mli_version != MLD_VERSION_1) {
1652 mli->mli_version = MLD_VERSION_1;
1653 mld_v2_cancel_link_timers(mli);
1658 * Cancel pending MLDv2 timers for the given link and all groups
1659 * joined on it; state-change, general-query, and group-query timers.
1662 mld_v2_cancel_link_timers(struct mld_ifinfo *mli)
1664 struct ifmultiaddr *ifma;
1666 struct in6_multi *inm, *tinm;
1668 CTR3(KTR_MLD, "%s: cancel v2 timers on ifp %p(%s)", __func__,
1669 mli->mli_ifp, mli->mli_ifp->if_xname);
1671 IN6_MULTI_LOCK_ASSERT();
1675 * Fast-track this potentially expensive operation
1676 * by checking all the global 'timer pending' flags.
1678 if (!V_interface_timers_running6 &&
1679 !V_state_change_timers_running6 &&
1680 !V_current_state_timers_running6)
1683 mli->mli_v2_timer = 0;
1688 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
1689 if (ifma->ifma_addr->sa_family != AF_INET6)
1691 inm = (struct in6_multi *)ifma->ifma_protospec;
1692 switch (inm->in6m_state) {
1693 case MLD_NOT_MEMBER:
1694 case MLD_SILENT_MEMBER:
1695 case MLD_IDLE_MEMBER:
1696 case MLD_LAZY_MEMBER:
1697 case MLD_SLEEPING_MEMBER:
1698 case MLD_AWAKENING_MEMBER:
1700 case MLD_LEAVING_MEMBER:
1702 * If we are leaving the group and switching
1703 * version, we need to release the final
1704 * reference held for issuing the INCLUDE {}.
1706 SLIST_INSERT_HEAD(&mli->mli_relinmhead, inm,
1709 case MLD_G_QUERY_PENDING_MEMBER:
1710 case MLD_SG_QUERY_PENDING_MEMBER:
1711 in6m_clear_recorded(inm);
1713 case MLD_REPORTING_MEMBER:
1714 inm->in6m_sctimer = 0;
1715 inm->in6m_timer = 0;
1716 inm->in6m_state = MLD_REPORTING_MEMBER;
1718 * Free any pending MLDv2 state-change records.
1720 _IF_DRAIN(&inm->in6m_scq);
1724 IF_ADDR_RUNLOCK(ifp);
1725 SLIST_FOREACH_SAFE(inm, &mli->mli_relinmhead, in6m_nrele, tinm) {
1726 SLIST_REMOVE_HEAD(&mli->mli_relinmhead, in6m_nrele);
1727 in6m_release_locked(inm);
1732 * Global slowtimo handler.
1733 * VIMAGE: Timeout handlers are expected to service all vimages.
1738 VNET_ITERATOR_DECL(vnet_iter);
1740 VNET_LIST_RLOCK_NOSLEEP();
1741 VNET_FOREACH(vnet_iter) {
1742 CURVNET_SET(vnet_iter);
1743 mld_slowtimo_vnet();
1746 VNET_LIST_RUNLOCK_NOSLEEP();
1750 * Per-vnet slowtimo handler.
1753 mld_slowtimo_vnet(void)
1755 struct mld_ifinfo *mli;
1759 LIST_FOREACH(mli, &V_mli_head, mli_link) {
1760 mld_v1_process_querier_timers(mli);
1767 * Update the Older Version Querier Present timers for a link.
1768 * See Section 9.12 of RFC 3810.
1771 mld_v1_process_querier_timers(struct mld_ifinfo *mli)
1776 if (mli->mli_version != MLD_VERSION_2 && --mli->mli_v1_timer == 0) {
1778 * MLDv1 Querier Present timer expired; revert to MLDv2.
1781 "%s: transition from v%d -> v%d on %p(%s)",
1782 __func__, mli->mli_version, MLD_VERSION_2,
1783 mli->mli_ifp, mli->mli_ifp->if_xname);
1784 mli->mli_version = MLD_VERSION_2;
1789 * Transmit an MLDv1 report immediately.
1792 mld_v1_transmit_report(struct in6_multi *in6m, const int type)
1795 struct in6_ifaddr *ia;
1796 struct ip6_hdr *ip6;
1797 struct mbuf *mh, *md;
1798 struct mld_hdr *mld;
1800 IN6_MULTI_LOCK_ASSERT();
1803 ifp = in6m->in6m_ifp;
1804 ia = in6ifa_ifpforlinklocal(ifp, IN6_IFF_NOTREADY|IN6_IFF_ANYCAST);
1805 /* ia may be NULL if link-local address is tentative. */
1807 MGETHDR(mh, M_DONTWAIT, MT_HEADER);
1810 ifa_free(&ia->ia_ifa);
1813 MGET(md, M_DONTWAIT, MT_DATA);
1817 ifa_free(&ia->ia_ifa);
1823 * FUTURE: Consider increasing alignment by ETHER_HDR_LEN, so
1824 * that ether_output() does not need to allocate another mbuf
1825 * for the header in the most common case.
1827 MH_ALIGN(mh, sizeof(struct ip6_hdr));
1828 mh->m_pkthdr.len = sizeof(struct ip6_hdr) + sizeof(struct mld_hdr);
1829 mh->m_len = sizeof(struct ip6_hdr);
1831 ip6 = mtod(mh, struct ip6_hdr *);
1833 ip6->ip6_vfc &= ~IPV6_VERSION_MASK;
1834 ip6->ip6_vfc |= IPV6_VERSION;
1835 ip6->ip6_nxt = IPPROTO_ICMPV6;
1836 ip6->ip6_src = ia ? ia->ia_addr.sin6_addr : in6addr_any;
1837 ip6->ip6_dst = in6m->in6m_addr;
1839 md->m_len = sizeof(struct mld_hdr);
1840 mld = mtod(md, struct mld_hdr *);
1841 mld->mld_type = type;
1844 mld->mld_maxdelay = 0;
1845 mld->mld_reserved = 0;
1846 mld->mld_addr = in6m->in6m_addr;
1847 in6_clearscope(&mld->mld_addr);
1848 mld->mld_cksum = in6_cksum(mh, IPPROTO_ICMPV6,
1849 sizeof(struct ip6_hdr), sizeof(struct mld_hdr));
1851 mld_save_context(mh, ifp);
1852 mh->m_flags |= M_MLDV1;
1854 mld_dispatch_packet(mh);
1857 ifa_free(&ia->ia_ifa);
1862 * Process a state change from the upper layer for the given IPv6 group.
1864 * Each socket holds a reference on the in_multi in its own ip_moptions.
1865 * The socket layer will have made the necessary updates to.the group
1866 * state, it is now up to MLD to issue a state change report if there
1867 * has been any change between T0 (when the last state-change was issued)
1870 * We use the MLDv2 state machine at group level. The MLd module
1871 * however makes the decision as to which MLD protocol version to speak.
1872 * A state change *from* INCLUDE {} always means an initial join.
1873 * A state change *to* INCLUDE {} always means a final leave.
1875 * If delay is non-zero, and the state change is an initial multicast
1876 * join, the state change report will be delayed by 'delay' ticks
1877 * in units of PR_FASTHZ if MLDv1 is active on the link; otherwise
1878 * the initial MLDv2 state change report will be delayed by whichever
1879 * is sooner, a pending state-change timer or delay itself.
1881 * VIMAGE: curvnet should have been set by caller, as this routine
1882 * is called from the socket option handlers.
1885 mld_change_state(struct in6_multi *inm, const int delay)
1887 struct mld_ifinfo *mli;
1891 IN6_MULTI_LOCK_ASSERT();
1896 * Try to detect if the upper layer just asked us to change state
1897 * for an interface which has now gone away.
1899 KASSERT(inm->in6m_ifma != NULL, ("%s: no ifma", __func__));
1900 ifp = inm->in6m_ifma->ifma_ifp;
1903 * Sanity check that netinet6's notion of ifp is the
1906 KASSERT(inm->in6m_ifp == ifp, ("%s: bad ifp", __func__));
1911 mli = MLD_IFINFO(ifp);
1912 KASSERT(mli != NULL, ("%s: no mld_ifinfo for ifp %p", __func__, ifp));
1915 * If we detect a state transition to or from MCAST_UNDEFINED
1916 * for this group, then we are starting or finishing an MLD
1917 * life cycle for this group.
1919 if (inm->in6m_st[1].iss_fmode != inm->in6m_st[0].iss_fmode) {
1920 CTR3(KTR_MLD, "%s: inm transition %d -> %d", __func__,
1921 inm->in6m_st[0].iss_fmode, inm->in6m_st[1].iss_fmode);
1922 if (inm->in6m_st[0].iss_fmode == MCAST_UNDEFINED) {
1923 CTR1(KTR_MLD, "%s: initial join", __func__);
1924 error = mld_initial_join(inm, mli, delay);
1926 } else if (inm->in6m_st[1].iss_fmode == MCAST_UNDEFINED) {
1927 CTR1(KTR_MLD, "%s: final leave", __func__);
1928 mld_final_leave(inm, mli);
1932 CTR1(KTR_MLD, "%s: filter set change", __func__);
1935 error = mld_handle_state_change(inm, mli);
1943 * Perform the initial join for an MLD group.
1945 * When joining a group:
1946 * If the group should have its MLD traffic suppressed, do nothing.
1947 * MLDv1 starts sending MLDv1 host membership reports.
1948 * MLDv2 will schedule an MLDv2 state-change report containing the
1949 * initial state of the membership.
1951 * If the delay argument is non-zero, then we must delay sending the
1952 * initial state change for delay ticks (in units of PR_FASTHZ).
1955 mld_initial_join(struct in6_multi *inm, struct mld_ifinfo *mli,
1959 struct ifqueue *ifq;
1960 int error, retval, syncstates;
1963 char ip6tbuf[INET6_ADDRSTRLEN];
1966 CTR4(KTR_MLD, "%s: initial join %s on ifp %p(%s)",
1967 __func__, ip6_sprintf(ip6tbuf, &inm->in6m_addr),
1968 inm->in6m_ifp, inm->in6m_ifp->if_xname);
1973 ifp = inm->in6m_ifp;
1975 IN6_MULTI_LOCK_ASSERT();
1978 KASSERT(mli && mli->mli_ifp == ifp, ("%s: inconsistent ifp", __func__));
1981 * Groups joined on loopback or marked as 'not reported',
1982 * enter the MLD_SILENT_MEMBER state and
1983 * are never reported in any protocol exchanges.
1984 * All other groups enter the appropriate state machine
1985 * for the version in use on this link.
1986 * A link marked as MLIF_SILENT causes MLD to be completely
1987 * disabled for the link.
1989 if ((ifp->if_flags & IFF_LOOPBACK) ||
1990 (mli->mli_flags & MLIF_SILENT) ||
1991 !mld_is_addr_reported(&inm->in6m_addr)) {
1993 "%s: not kicking state machine for silent group", __func__);
1994 inm->in6m_state = MLD_SILENT_MEMBER;
1995 inm->in6m_timer = 0;
1998 * Deal with overlapping in_multi lifecycle.
1999 * If this group was LEAVING, then make sure
2000 * we drop the reference we picked up to keep the
2001 * group around for the final INCLUDE {} enqueue.
2003 if (mli->mli_version == MLD_VERSION_2 &&
2004 inm->in6m_state == MLD_LEAVING_MEMBER)
2005 in6m_release_locked(inm);
2007 inm->in6m_state = MLD_REPORTING_MEMBER;
2009 switch (mli->mli_version) {
2012 * If a delay was provided, only use it if
2013 * it is greater than the delay normally
2014 * used for an MLDv1 state change report,
2015 * and delay sending the initial MLDv1 report
2016 * by not transitioning to the IDLE state.
2018 odelay = MLD_RANDOM_DELAY(MLD_V1_MAX_RI * PR_FASTHZ);
2020 inm->in6m_timer = max(delay, odelay);
2021 V_current_state_timers_running6 = 1;
2023 inm->in6m_state = MLD_IDLE_MEMBER;
2024 error = mld_v1_transmit_report(inm,
2025 MLD_LISTENER_REPORT);
2027 inm->in6m_timer = odelay;
2028 V_current_state_timers_running6 = 1;
2035 * Defer update of T0 to T1, until the first copy
2036 * of the state change has been transmitted.
2041 * Immediately enqueue a State-Change Report for
2042 * this interface, freeing any previous reports.
2043 * Don't kick the timers if there is nothing to do,
2044 * or if an error occurred.
2046 ifq = &inm->in6m_scq;
2048 retval = mld_v2_enqueue_group_record(ifq, inm, 1,
2049 0, 0, (mli->mli_flags & MLIF_USEALLOW));
2050 CTR2(KTR_MLD, "%s: enqueue record = %d",
2053 error = retval * -1;
2058 * Schedule transmission of pending state-change
2059 * report up to RV times for this link. The timer
2060 * will fire at the next mld_fasttimo (~200ms),
2061 * giving us an opportunity to merge the reports.
2063 * If a delay was provided to this function, only
2064 * use this delay if sooner than the existing one.
2066 KASSERT(mli->mli_rv > 1,
2067 ("%s: invalid robustness %d", __func__,
2069 inm->in6m_scrv = mli->mli_rv;
2071 if (inm->in6m_sctimer > 1) {
2073 min(inm->in6m_sctimer, delay);
2075 inm->in6m_sctimer = delay;
2077 inm->in6m_sctimer = 1;
2078 V_state_change_timers_running6 = 1;
2086 * Only update the T0 state if state change is atomic,
2087 * i.e. we don't need to wait for a timer to fire before we
2088 * can consider the state change to have been communicated.
2092 CTR3(KTR_MLD, "%s: T1 -> T0 for %s/%s", __func__,
2093 ip6_sprintf(ip6tbuf, &inm->in6m_addr),
2094 inm->in6m_ifp->if_xname);
2101 * Issue an intermediate state change during the life-cycle.
2104 mld_handle_state_change(struct in6_multi *inm, struct mld_ifinfo *mli)
2109 char ip6tbuf[INET6_ADDRSTRLEN];
2112 CTR4(KTR_MLD, "%s: state change for %s on ifp %p(%s)",
2113 __func__, ip6_sprintf(ip6tbuf, &inm->in6m_addr),
2114 inm->in6m_ifp, inm->in6m_ifp->if_xname);
2116 ifp = inm->in6m_ifp;
2118 IN6_MULTI_LOCK_ASSERT();
2121 KASSERT(mli && mli->mli_ifp == ifp,
2122 ("%s: inconsistent ifp", __func__));
2124 if ((ifp->if_flags & IFF_LOOPBACK) ||
2125 (mli->mli_flags & MLIF_SILENT) ||
2126 !mld_is_addr_reported(&inm->in6m_addr) ||
2127 (mli->mli_version != MLD_VERSION_2)) {
2128 if (!mld_is_addr_reported(&inm->in6m_addr)) {
2130 "%s: not kicking state machine for silent group", __func__);
2132 CTR1(KTR_MLD, "%s: nothing to do", __func__);
2134 CTR3(KTR_MLD, "%s: T1 -> T0 for %s/%s", __func__,
2135 ip6_sprintf(ip6tbuf, &inm->in6m_addr),
2136 inm->in6m_ifp->if_xname);
2140 _IF_DRAIN(&inm->in6m_scq);
2142 retval = mld_v2_enqueue_group_record(&inm->in6m_scq, inm, 1, 0, 0,
2143 (mli->mli_flags & MLIF_USEALLOW));
2144 CTR2(KTR_MLD, "%s: enqueue record = %d", __func__, retval);
2149 * If record(s) were enqueued, start the state-change
2150 * report timer for this group.
2152 inm->in6m_scrv = mli->mli_rv;
2153 inm->in6m_sctimer = 1;
2154 V_state_change_timers_running6 = 1;
2160 * Perform the final leave for a multicast address.
2162 * When leaving a group:
2163 * MLDv1 sends a DONE message, if and only if we are the reporter.
2164 * MLDv2 enqueues a state-change report containing a transition
2165 * to INCLUDE {} for immediate transmission.
2168 mld_final_leave(struct in6_multi *inm, struct mld_ifinfo *mli)
2172 char ip6tbuf[INET6_ADDRSTRLEN];
2177 CTR4(KTR_MLD, "%s: final leave %s on ifp %p(%s)",
2178 __func__, ip6_sprintf(ip6tbuf, &inm->in6m_addr),
2179 inm->in6m_ifp, inm->in6m_ifp->if_xname);
2181 IN6_MULTI_LOCK_ASSERT();
2184 switch (inm->in6m_state) {
2185 case MLD_NOT_MEMBER:
2186 case MLD_SILENT_MEMBER:
2187 case MLD_LEAVING_MEMBER:
2188 /* Already leaving or left; do nothing. */
2190 "%s: not kicking state machine for silent group", __func__);
2192 case MLD_REPORTING_MEMBER:
2193 case MLD_IDLE_MEMBER:
2194 case MLD_G_QUERY_PENDING_MEMBER:
2195 case MLD_SG_QUERY_PENDING_MEMBER:
2196 if (mli->mli_version == MLD_VERSION_1) {
2198 if (inm->in6m_state == MLD_G_QUERY_PENDING_MEMBER ||
2199 inm->in6m_state == MLD_SG_QUERY_PENDING_MEMBER)
2200 panic("%s: MLDv2 state reached, not MLDv2 mode",
2203 mld_v1_transmit_report(inm, MLD_LISTENER_DONE);
2204 inm->in6m_state = MLD_NOT_MEMBER;
2205 } else if (mli->mli_version == MLD_VERSION_2) {
2207 * Stop group timer and all pending reports.
2208 * Immediately enqueue a state-change report
2209 * TO_IN {} to be sent on the next fast timeout,
2210 * giving us an opportunity to merge reports.
2212 _IF_DRAIN(&inm->in6m_scq);
2213 inm->in6m_timer = 0;
2214 inm->in6m_scrv = mli->mli_rv;
2215 CTR4(KTR_MLD, "%s: Leaving %s/%s with %d "
2216 "pending retransmissions.", __func__,
2217 ip6_sprintf(ip6tbuf, &inm->in6m_addr),
2218 inm->in6m_ifp->if_xname, inm->in6m_scrv);
2219 if (inm->in6m_scrv == 0) {
2220 inm->in6m_state = MLD_NOT_MEMBER;
2221 inm->in6m_sctimer = 0;
2225 in6m_acquire_locked(inm);
2227 retval = mld_v2_enqueue_group_record(
2228 &inm->in6m_scq, inm, 1, 0, 0,
2229 (mli->mli_flags & MLIF_USEALLOW));
2230 KASSERT(retval != 0,
2231 ("%s: enqueue record = %d", __func__,
2234 inm->in6m_state = MLD_LEAVING_MEMBER;
2235 inm->in6m_sctimer = 1;
2236 V_state_change_timers_running6 = 1;
2242 case MLD_LAZY_MEMBER:
2243 case MLD_SLEEPING_MEMBER:
2244 case MLD_AWAKENING_MEMBER:
2245 /* Our reports are suppressed; do nothing. */
2251 CTR3(KTR_MLD, "%s: T1 -> T0 for %s/%s", __func__,
2252 ip6_sprintf(ip6tbuf, &inm->in6m_addr),
2253 inm->in6m_ifp->if_xname);
2254 inm->in6m_st[1].iss_fmode = MCAST_UNDEFINED;
2255 CTR3(KTR_MLD, "%s: T1 now MCAST_UNDEFINED for %p/%s",
2256 __func__, &inm->in6m_addr, inm->in6m_ifp->if_xname);
2261 * Enqueue an MLDv2 group record to the given output queue.
2263 * If is_state_change is zero, a current-state record is appended.
2264 * If is_state_change is non-zero, a state-change report is appended.
2266 * If is_group_query is non-zero, an mbuf packet chain is allocated.
2267 * If is_group_query is zero, and if there is a packet with free space
2268 * at the tail of the queue, it will be appended to providing there
2269 * is enough free space.
2270 * Otherwise a new mbuf packet chain is allocated.
2272 * If is_source_query is non-zero, each source is checked to see if
2273 * it was recorded for a Group-Source query, and will be omitted if
2274 * it is not both in-mode and recorded.
2276 * If use_block_allow is non-zero, state change reports for initial join
2277 * and final leave, on an inclusive mode group with a source list, will be
2278 * rewritten to use the ALLOW_NEW and BLOCK_OLD record types, respectively.
2280 * The function will attempt to allocate leading space in the packet
2281 * for the IPv6+ICMP headers to be prepended without fragmenting the chain.
2283 * If successful the size of all data appended to the queue is returned,
2284 * otherwise an error code less than zero is returned, or zero if
2285 * no record(s) were appended.
2288 mld_v2_enqueue_group_record(struct ifqueue *ifq, struct in6_multi *inm,
2289 const int is_state_change, const int is_group_query,
2290 const int is_source_query, const int use_block_allow)
2292 struct mldv2_record mr;
2293 struct mldv2_record *pmr;
2295 struct ip6_msource *ims, *nims;
2296 struct mbuf *m0, *m, *md;
2297 int error, is_filter_list_change;
2298 int minrec0len, m0srcs, msrcs, nbytes, off;
2299 int record_has_sources;
2304 char ip6tbuf[INET6_ADDRSTRLEN];
2307 IN6_MULTI_LOCK_ASSERT();
2310 ifp = inm->in6m_ifp;
2311 is_filter_list_change = 0;
2318 record_has_sources = 1;
2320 type = MLD_DO_NOTHING;
2321 mode = inm->in6m_st[1].iss_fmode;
2324 * If we did not transition out of ASM mode during t0->t1,
2325 * and there are no source nodes to process, we can skip
2326 * the generation of source records.
2328 if (inm->in6m_st[0].iss_asm > 0 && inm->in6m_st[1].iss_asm > 0 &&
2329 inm->in6m_nsrc == 0)
2330 record_has_sources = 0;
2332 if (is_state_change) {
2334 * Queue a state change record.
2335 * If the mode did not change, and there are non-ASM
2336 * listeners or source filters present,
2337 * we potentially need to issue two records for the group.
2338 * If there are ASM listeners, and there was no filter
2339 * mode transition of any kind, do nothing.
2341 * If we are transitioning to MCAST_UNDEFINED, we need
2342 * not send any sources. A transition to/from this state is
2343 * considered inclusive with some special treatment.
2345 * If we are rewriting initial joins/leaves to use
2346 * ALLOW/BLOCK, and the group's membership is inclusive,
2347 * we need to send sources in all cases.
2349 if (mode != inm->in6m_st[0].iss_fmode) {
2350 if (mode == MCAST_EXCLUDE) {
2351 CTR1(KTR_MLD, "%s: change to EXCLUDE",
2353 type = MLD_CHANGE_TO_EXCLUDE_MODE;
2355 CTR1(KTR_MLD, "%s: change to INCLUDE",
2357 if (use_block_allow) {
2360 * Here we're interested in state
2361 * edges either direction between
2362 * MCAST_UNDEFINED and MCAST_INCLUDE.
2363 * Perhaps we should just check
2364 * the group state, rather than
2367 if (mode == MCAST_UNDEFINED) {
2368 type = MLD_BLOCK_OLD_SOURCES;
2370 type = MLD_ALLOW_NEW_SOURCES;
2373 type = MLD_CHANGE_TO_INCLUDE_MODE;
2374 if (mode == MCAST_UNDEFINED)
2375 record_has_sources = 0;
2379 if (record_has_sources) {
2380 is_filter_list_change = 1;
2382 type = MLD_DO_NOTHING;
2387 * Queue a current state record.
2389 if (mode == MCAST_EXCLUDE) {
2390 type = MLD_MODE_IS_EXCLUDE;
2391 } else if (mode == MCAST_INCLUDE) {
2392 type = MLD_MODE_IS_INCLUDE;
2393 KASSERT(inm->in6m_st[1].iss_asm == 0,
2394 ("%s: inm %p is INCLUDE but ASM count is %d",
2395 __func__, inm, inm->in6m_st[1].iss_asm));
2400 * Generate the filter list changes using a separate function.
2402 if (is_filter_list_change)
2403 return (mld_v2_enqueue_filter_change(ifq, inm));
2405 if (type == MLD_DO_NOTHING) {
2406 CTR3(KTR_MLD, "%s: nothing to do for %s/%s",
2407 __func__, ip6_sprintf(ip6tbuf, &inm->in6m_addr),
2408 inm->in6m_ifp->if_xname);
2413 * If any sources are present, we must be able to fit at least
2414 * one in the trailing space of the tail packet's mbuf,
2417 minrec0len = sizeof(struct mldv2_record);
2418 if (record_has_sources)
2419 minrec0len += sizeof(struct in6_addr);
2421 CTR4(KTR_MLD, "%s: queueing %s for %s/%s", __func__,
2422 mld_rec_type_to_str(type),
2423 ip6_sprintf(ip6tbuf, &inm->in6m_addr),
2424 inm->in6m_ifp->if_xname);
2427 * Check if we have a packet in the tail of the queue for this
2428 * group into which the first group record for this group will fit.
2429 * Otherwise allocate a new packet.
2430 * Always allocate leading space for IP6+RA+ICMPV6+REPORT.
2431 * Note: Group records for G/GSR query responses MUST be sent
2432 * in their own packet.
2435 if (!is_group_query &&
2437 (m0->m_pkthdr.PH_vt.vt_nrecs + 1 <= MLD_V2_REPORT_MAXRECS) &&
2438 (m0->m_pkthdr.len + minrec0len) <
2439 (ifp->if_mtu - MLD_MTUSPACE)) {
2440 m0srcs = (ifp->if_mtu - m0->m_pkthdr.len -
2441 sizeof(struct mldv2_record)) /
2442 sizeof(struct in6_addr);
2444 CTR1(KTR_MLD, "%s: use existing packet", __func__);
2446 if (_IF_QFULL(ifq)) {
2447 CTR1(KTR_MLD, "%s: outbound queue full", __func__);
2451 m0srcs = (ifp->if_mtu - MLD_MTUSPACE -
2452 sizeof(struct mldv2_record)) / sizeof(struct in6_addr);
2453 if (!is_state_change && !is_group_query)
2454 m = m_getcl(M_DONTWAIT, MT_DATA, M_PKTHDR);
2456 m = m_gethdr(M_DONTWAIT, MT_DATA);
2460 mld_save_context(m, ifp);
2462 CTR1(KTR_MLD, "%s: allocated first packet", __func__);
2466 * Append group record.
2467 * If we have sources, we don't know how many yet.
2472 mr.mr_addr = inm->in6m_addr;
2473 in6_clearscope(&mr.mr_addr);
2474 if (!m_append(m, sizeof(struct mldv2_record), (void *)&mr)) {
2477 CTR1(KTR_MLD, "%s: m_append() failed.", __func__);
2480 nbytes += sizeof(struct mldv2_record);
2483 * Append as many sources as will fit in the first packet.
2484 * If we are appending to a new packet, the chain allocation
2485 * may potentially use clusters; use m_getptr() in this case.
2486 * If we are appending to an existing packet, we need to obtain
2487 * a pointer to the group record after m_append(), in case a new
2488 * mbuf was allocated.
2490 * Only append sources which are in-mode at t1. If we are
2491 * transitioning to MCAST_UNDEFINED state on the group, and
2492 * use_block_allow is zero, do not include source entries.
2493 * Otherwise, we need to include this source in the report.
2495 * Only report recorded sources in our filter set when responding
2496 * to a group-source query.
2498 if (record_has_sources) {
2501 pmr = (struct mldv2_record *)(mtod(md, uint8_t *) +
2502 md->m_len - nbytes);
2504 md = m_getptr(m, 0, &off);
2505 pmr = (struct mldv2_record *)(mtod(md, uint8_t *) +
2509 RB_FOREACH_SAFE(ims, ip6_msource_tree, &inm->in6m_srcs,
2511 CTR2(KTR_MLD, "%s: visit node %s", __func__,
2512 ip6_sprintf(ip6tbuf, &ims->im6s_addr));
2513 now = im6s_get_mode(inm, ims, 1);
2514 CTR2(KTR_MLD, "%s: node is %d", __func__, now);
2515 if ((now != mode) ||
2517 (!use_block_allow && mode == MCAST_UNDEFINED))) {
2518 CTR1(KTR_MLD, "%s: skip node", __func__);
2521 if (is_source_query && ims->im6s_stp == 0) {
2522 CTR1(KTR_MLD, "%s: skip unrecorded node",
2526 CTR1(KTR_MLD, "%s: append node", __func__);
2527 if (!m_append(m, sizeof(struct in6_addr),
2528 (void *)&ims->im6s_addr)) {
2531 CTR1(KTR_MLD, "%s: m_append() failed.",
2535 nbytes += sizeof(struct in6_addr);
2537 if (msrcs == m0srcs)
2540 CTR2(KTR_MLD, "%s: msrcs is %d this packet", __func__,
2542 pmr->mr_numsrc = htons(msrcs);
2543 nbytes += (msrcs * sizeof(struct in6_addr));
2546 if (is_source_query && msrcs == 0) {
2547 CTR1(KTR_MLD, "%s: no recorded sources to report", __func__);
2554 * We are good to go with first packet.
2557 CTR1(KTR_MLD, "%s: enqueueing first packet", __func__);
2558 m->m_pkthdr.PH_vt.vt_nrecs = 1;
2559 _IF_ENQUEUE(ifq, m);
2561 m->m_pkthdr.PH_vt.vt_nrecs++;
2564 * No further work needed if no source list in packet(s).
2566 if (!record_has_sources)
2570 * Whilst sources remain to be announced, we need to allocate
2571 * a new packet and fill out as many sources as will fit.
2572 * Always try for a cluster first.
2574 while (nims != NULL) {
2575 if (_IF_QFULL(ifq)) {
2576 CTR1(KTR_MLD, "%s: outbound queue full", __func__);
2579 m = m_getcl(M_DONTWAIT, MT_DATA, M_PKTHDR);
2581 m = m_gethdr(M_DONTWAIT, MT_DATA);
2584 mld_save_context(m, ifp);
2585 md = m_getptr(m, 0, &off);
2586 pmr = (struct mldv2_record *)(mtod(md, uint8_t *) + off);
2587 CTR1(KTR_MLD, "%s: allocated next packet", __func__);
2589 if (!m_append(m, sizeof(struct mldv2_record), (void *)&mr)) {
2592 CTR1(KTR_MLD, "%s: m_append() failed.", __func__);
2595 m->m_pkthdr.PH_vt.vt_nrecs = 1;
2596 nbytes += sizeof(struct mldv2_record);
2598 m0srcs = (ifp->if_mtu - MLD_MTUSPACE -
2599 sizeof(struct mldv2_record)) / sizeof(struct in6_addr);
2602 RB_FOREACH_FROM(ims, ip6_msource_tree, nims) {
2603 CTR2(KTR_MLD, "%s: visit node %s",
2604 __func__, ip6_sprintf(ip6tbuf, &ims->im6s_addr));
2605 now = im6s_get_mode(inm, ims, 1);
2606 if ((now != mode) ||
2608 (!use_block_allow && mode == MCAST_UNDEFINED))) {
2609 CTR1(KTR_MLD, "%s: skip node", __func__);
2612 if (is_source_query && ims->im6s_stp == 0) {
2613 CTR1(KTR_MLD, "%s: skip unrecorded node",
2617 CTR1(KTR_MLD, "%s: append node", __func__);
2618 if (!m_append(m, sizeof(struct in6_addr),
2619 (void *)&ims->im6s_addr)) {
2622 CTR1(KTR_MLD, "%s: m_append() failed.",
2627 if (msrcs == m0srcs)
2630 pmr->mr_numsrc = htons(msrcs);
2631 nbytes += (msrcs * sizeof(struct in6_addr));
2633 CTR1(KTR_MLD, "%s: enqueueing next packet", __func__);
2634 _IF_ENQUEUE(ifq, m);
2641 * Type used to mark record pass completion.
2642 * We exploit the fact we can cast to this easily from the
2643 * current filter modes on each ip_msource node.
2646 REC_NONE = 0x00, /* MCAST_UNDEFINED */
2647 REC_ALLOW = 0x01, /* MCAST_INCLUDE */
2648 REC_BLOCK = 0x02, /* MCAST_EXCLUDE */
2649 REC_FULL = REC_ALLOW | REC_BLOCK
2653 * Enqueue an MLDv2 filter list change to the given output queue.
2655 * Source list filter state is held in an RB-tree. When the filter list
2656 * for a group is changed without changing its mode, we need to compute
2657 * the deltas between T0 and T1 for each source in the filter set,
2658 * and enqueue the appropriate ALLOW_NEW/BLOCK_OLD records.
2660 * As we may potentially queue two record types, and the entire R-B tree
2661 * needs to be walked at once, we break this out into its own function
2662 * so we can generate a tightly packed queue of packets.
2664 * XXX This could be written to only use one tree walk, although that makes
2665 * serializing into the mbuf chains a bit harder. For now we do two walks
2666 * which makes things easier on us, and it may or may not be harder on
2669 * If successful the size of all data appended to the queue is returned,
2670 * otherwise an error code less than zero is returned, or zero if
2671 * no record(s) were appended.
2674 mld_v2_enqueue_filter_change(struct ifqueue *ifq, struct in6_multi *inm)
2676 static const int MINRECLEN =
2677 sizeof(struct mldv2_record) + sizeof(struct in6_addr);
2679 struct mldv2_record mr;
2680 struct mldv2_record *pmr;
2681 struct ip6_msource *ims, *nims;
2682 struct mbuf *m, *m0, *md;
2683 int m0srcs, nbytes, npbytes, off, rsrcs, schanged;
2685 uint8_t mode, now, then;
2686 rectype_t crt, drt, nrt;
2688 char ip6tbuf[INET6_ADDRSTRLEN];
2691 IN6_MULTI_LOCK_ASSERT();
2693 if (inm->in6m_nsrc == 0 ||
2694 (inm->in6m_st[0].iss_asm > 0 && inm->in6m_st[1].iss_asm > 0))
2697 ifp = inm->in6m_ifp; /* interface */
2698 mode = inm->in6m_st[1].iss_fmode; /* filter mode at t1 */
2699 crt = REC_NONE; /* current group record type */
2700 drt = REC_NONE; /* mask of completed group record types */
2701 nrt = REC_NONE; /* record type for current node */
2702 m0srcs = 0; /* # source which will fit in current mbuf chain */
2703 npbytes = 0; /* # of bytes appended this packet */
2704 nbytes = 0; /* # of bytes appended to group's state-change queue */
2705 rsrcs = 0; /* # sources encoded in current record */
2706 schanged = 0; /* # nodes encoded in overall filter change */
2707 nallow = 0; /* # of source entries in ALLOW_NEW */
2708 nblock = 0; /* # of source entries in BLOCK_OLD */
2709 nims = NULL; /* next tree node pointer */
2712 * For each possible filter record mode.
2713 * The first kind of source we encounter tells us which
2714 * is the first kind of record we start appending.
2715 * If a node transitioned to UNDEFINED at t1, its mode is treated
2716 * as the inverse of the group's filter mode.
2718 while (drt != REC_FULL) {
2722 (m0->m_pkthdr.PH_vt.vt_nrecs + 1 <=
2723 MLD_V2_REPORT_MAXRECS) &&
2724 (m0->m_pkthdr.len + MINRECLEN) <
2725 (ifp->if_mtu - MLD_MTUSPACE)) {
2727 m0srcs = (ifp->if_mtu - m0->m_pkthdr.len -
2728 sizeof(struct mldv2_record)) /
2729 sizeof(struct in6_addr);
2731 "%s: use previous packet", __func__);
2733 m = m_getcl(M_DONTWAIT, MT_DATA, M_PKTHDR);
2735 m = m_gethdr(M_DONTWAIT, MT_DATA);
2738 "%s: m_get*() failed", __func__);
2741 m->m_pkthdr.PH_vt.vt_nrecs = 0;
2742 mld_save_context(m, ifp);
2743 m0srcs = (ifp->if_mtu - MLD_MTUSPACE -
2744 sizeof(struct mldv2_record)) /
2745 sizeof(struct in6_addr);
2748 "%s: allocated new packet", __func__);
2751 * Append the MLD group record header to the
2752 * current packet's data area.
2753 * Recalculate pointer to free space for next
2754 * group record, in case m_append() allocated
2755 * a new mbuf or cluster.
2757 memset(&mr, 0, sizeof(mr));
2758 mr.mr_addr = inm->in6m_addr;
2759 in6_clearscope(&mr.mr_addr);
2760 if (!m_append(m, sizeof(mr), (void *)&mr)) {
2764 "%s: m_append() failed", __func__);
2767 npbytes += sizeof(struct mldv2_record);
2769 /* new packet; offset in chain */
2770 md = m_getptr(m, npbytes -
2771 sizeof(struct mldv2_record), &off);
2772 pmr = (struct mldv2_record *)(mtod(md,
2775 /* current packet; offset from last append */
2777 pmr = (struct mldv2_record *)(mtod(md,
2778 uint8_t *) + md->m_len -
2779 sizeof(struct mldv2_record));
2782 * Begin walking the tree for this record type
2783 * pass, or continue from where we left off
2784 * previously if we had to allocate a new packet.
2785 * Only report deltas in-mode at t1.
2786 * We need not report included sources as allowed
2787 * if we are in inclusive mode on the group,
2788 * however the converse is not true.
2792 nims = RB_MIN(ip6_msource_tree,
2795 RB_FOREACH_FROM(ims, ip6_msource_tree, nims) {
2796 CTR2(KTR_MLD, "%s: visit node %s", __func__,
2797 ip6_sprintf(ip6tbuf, &ims->im6s_addr));
2798 now = im6s_get_mode(inm, ims, 1);
2799 then = im6s_get_mode(inm, ims, 0);
2800 CTR3(KTR_MLD, "%s: mode: t0 %d, t1 %d",
2801 __func__, then, now);
2804 "%s: skip unchanged", __func__);
2807 if (mode == MCAST_EXCLUDE &&
2808 now == MCAST_INCLUDE) {
2810 "%s: skip IN src on EX group",
2814 nrt = (rectype_t)now;
2815 if (nrt == REC_NONE)
2816 nrt = (rectype_t)(~mode & REC_FULL);
2817 if (schanged++ == 0) {
2819 } else if (crt != nrt)
2821 if (!m_append(m, sizeof(struct in6_addr),
2822 (void *)&ims->im6s_addr)) {
2826 "%s: m_append() failed", __func__);
2829 nallow += !!(crt == REC_ALLOW);
2830 nblock += !!(crt == REC_BLOCK);
2831 if (++rsrcs == m0srcs)
2835 * If we did not append any tree nodes on this
2836 * pass, back out of allocations.
2839 npbytes -= sizeof(struct mldv2_record);
2842 "%s: m_free(m)", __func__);
2846 "%s: m_adj(m, -mr)", __func__);
2847 m_adj(m, -((int)sizeof(
2848 struct mldv2_record)));
2852 npbytes += (rsrcs * sizeof(struct in6_addr));
2853 if (crt == REC_ALLOW)
2854 pmr->mr_type = MLD_ALLOW_NEW_SOURCES;
2855 else if (crt == REC_BLOCK)
2856 pmr->mr_type = MLD_BLOCK_OLD_SOURCES;
2857 pmr->mr_numsrc = htons(rsrcs);
2859 * Count the new group record, and enqueue this
2860 * packet if it wasn't already queued.
2862 m->m_pkthdr.PH_vt.vt_nrecs++;
2864 _IF_ENQUEUE(ifq, m);
2866 } while (nims != NULL);
2868 crt = (~crt & REC_FULL);
2871 CTR3(KTR_MLD, "%s: queued %d ALLOW_NEW, %d BLOCK_OLD", __func__,
2878 mld_v2_merge_state_changes(struct in6_multi *inm, struct ifqueue *ifscq)
2881 struct mbuf *m; /* pending state-change */
2882 struct mbuf *m0; /* copy of pending state-change */
2883 struct mbuf *mt; /* last state-change in packet */
2884 int docopy, domerge;
2891 IN6_MULTI_LOCK_ASSERT();
2895 * If there are further pending retransmissions, make a writable
2896 * copy of each queued state-change message before merging.
2898 if (inm->in6m_scrv > 0)
2901 gq = &inm->in6m_scq;
2903 if (gq->ifq_head == NULL) {
2904 CTR2(KTR_MLD, "%s: WARNING: queue for inm %p is empty",
2912 * Only merge the report into the current packet if
2913 * there is sufficient space to do so; an MLDv2 report
2914 * packet may only contain 65,535 group records.
2915 * Always use a simple mbuf chain concatentation to do this,
2916 * as large state changes for single groups may have
2917 * allocated clusters.
2920 mt = ifscq->ifq_tail;
2922 recslen = m_length(m, NULL);
2924 if ((mt->m_pkthdr.PH_vt.vt_nrecs +
2925 m->m_pkthdr.PH_vt.vt_nrecs <=
2926 MLD_V2_REPORT_MAXRECS) &&
2927 (mt->m_pkthdr.len + recslen <=
2928 (inm->in6m_ifp->if_mtu - MLD_MTUSPACE)))
2932 if (!domerge && _IF_QFULL(gq)) {
2934 "%s: outbound queue full, skipping whole packet %p",
2944 CTR2(KTR_MLD, "%s: dequeueing %p", __func__, m);
2945 _IF_DEQUEUE(gq, m0);
2948 CTR2(KTR_MLD, "%s: copying %p", __func__, m);
2949 m0 = m_dup(m, M_NOWAIT);
2952 m0->m_nextpkt = NULL;
2957 CTR3(KTR_MLD, "%s: queueing %p to ifscq %p)",
2958 __func__, m0, ifscq);
2959 _IF_ENQUEUE(ifscq, m0);
2961 struct mbuf *mtl; /* last mbuf of packet mt */
2963 CTR3(KTR_MLD, "%s: merging %p with ifscq tail %p)",
2967 m0->m_flags &= ~M_PKTHDR;
2968 mt->m_pkthdr.len += recslen;
2969 mt->m_pkthdr.PH_vt.vt_nrecs +=
2970 m0->m_pkthdr.PH_vt.vt_nrecs;
2980 * Respond to a pending MLDv2 General Query.
2983 mld_v2_dispatch_general_query(struct mld_ifinfo *mli)
2985 struct ifmultiaddr *ifma;
2987 struct in6_multi *inm;
2990 IN6_MULTI_LOCK_ASSERT();
2993 KASSERT(mli->mli_version == MLD_VERSION_2,
2994 ("%s: called when version %d", __func__, mli->mli_version));
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);
3031 mld_dispatch_queue(&mli->mli_gq, MLD_MAX_RESPONSE_BURST);
3034 * Slew transmission of bursts over 500ms intervals.
3036 if (mli->mli_gq.ifq_head != NULL) {
3037 mli->mli_v2_timer = 1 + MLD_RANDOM_DELAY(
3038 MLD_RESPONSE_BURST_INTERVAL);
3039 V_interface_timers_running6 = 1;
3044 * Transmit the next pending message in the output queue.
3046 * VIMAGE: Needs to store/restore vnet pointer on a per-mbuf-chain basis.
3047 * MRT: Nothing needs to be done, as MLD traffic is always local to
3048 * a link and uses a link-scope multicast address.
3051 mld_dispatch_packet(struct mbuf *m)
3053 struct ip6_moptions im6o;
3058 struct ip6_hdr *ip6;
3059 struct mld_hdr *mld;
3065 CTR2(KTR_MLD, "%s: transmit %p", __func__, m);
3068 * Set VNET image pointer from enqueued mbuf chain
3069 * before doing anything else. Whilst we use interface
3070 * indexes to guard against interface detach, they are
3071 * unique to each VIMAGE and must be retrieved.
3073 ifindex = mld_restore_context(m);
3076 * Check if the ifnet still exists. This limits the scope of
3077 * any race in the absence of a global ifp lock for low cost
3078 * (an array lookup).
3080 ifp = ifnet_byindex(ifindex);
3082 CTR3(KTR_MLD, "%s: dropped %p as ifindex %u went away.",
3083 __func__, m, ifindex);
3085 IP6STAT_INC(ip6s_noroute);
3089 im6o.im6o_multicast_hlim = 1;
3090 im6o.im6o_multicast_loop = (V_ip6_mrouter != NULL);
3091 im6o.im6o_multicast_ifp = ifp;
3093 if (m->m_flags & M_MLDV1) {
3096 m0 = mld_v2_encap_report(ifp, m);
3098 CTR2(KTR_MLD, "%s: dropped %p", __func__, m);
3100 IP6STAT_INC(ip6s_odropped);
3105 mld_scrub_context(m0);
3106 m->m_flags &= ~(M_PROTOFLAGS);
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 MGETHDR(mh, M_DONTWAIT, MT_HEADER);
3184 ifa_free(&ia->ia_ifa);
3188 MH_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_PSEUDO, 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_PSEUDO, 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_PSEUDO, SI_ORDER_ANY, vnet_mld_init,
3289 vnet_mld_uninit(const void *unused __unused)
3292 CTR1(KTR_MLD, "%s: tearing down", __func__);
3294 KASSERT(LIST_EMPTY(&V_mli_head),
3295 ("%s: mli list not empty; ifnets not detached?", __func__));
3297 VNET_SYSUNINIT(vnet_mld_uninit, SI_SUB_PSEUDO, SI_ORDER_ANY, vnet_mld_uninit,
3301 mld_modevent(module_t mod, int type, void *unused __unused)
3309 return (EOPNOTSUPP);
3314 static moduledata_t mld_mod = {
3319 DECLARE_MODULE(mld, mld_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);