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/if_var.h>
86 #include <net/route.h>
89 #include <netinet/in.h>
90 #include <netinet/in_var.h>
91 #include <netinet6/in6_var.h>
92 #include <netinet/ip6.h>
93 #include <netinet6/ip6_var.h>
94 #include <netinet6/scope6_var.h>
95 #include <netinet/icmp6.h>
96 #include <netinet6/mld6.h>
97 #include <netinet6/mld6_var.h>
99 #include <security/mac/mac_framework.h>
102 #define KTR_MLD KTR_INET6
105 static struct mld_ifinfo *
106 mli_alloc_locked(struct ifnet *);
107 static void mli_delete_locked(const struct ifnet *);
108 static void mld_dispatch_packet(struct mbuf *);
109 static void mld_dispatch_queue(struct ifqueue *, int);
110 static void mld_final_leave(struct in6_multi *, struct mld_ifinfo *);
111 static void mld_fasttimo_vnet(void);
112 static int mld_handle_state_change(struct in6_multi *,
113 struct mld_ifinfo *);
114 static int mld_initial_join(struct in6_multi *, struct mld_ifinfo *,
117 static char * mld_rec_type_to_str(const int);
119 static void mld_set_version(struct mld_ifinfo *, const int);
120 static void mld_slowtimo_vnet(void);
121 static int mld_v1_input_query(struct ifnet *, const struct ip6_hdr *,
122 /*const*/ struct mld_hdr *);
123 static int mld_v1_input_report(struct ifnet *, const struct ip6_hdr *,
124 /*const*/ struct mld_hdr *);
125 static void mld_v1_process_group_timer(struct mld_ifinfo *,
127 static void mld_v1_process_querier_timers(struct mld_ifinfo *);
128 static int mld_v1_transmit_report(struct in6_multi *, const int);
129 static void mld_v1_update_group(struct in6_multi *, const int);
130 static void mld_v2_cancel_link_timers(struct mld_ifinfo *);
131 static void mld_v2_dispatch_general_query(struct mld_ifinfo *);
133 mld_v2_encap_report(struct ifnet *, struct mbuf *);
134 static int mld_v2_enqueue_filter_change(struct ifqueue *,
136 static int mld_v2_enqueue_group_record(struct ifqueue *,
137 struct in6_multi *, const int, const int, const int,
139 static int mld_v2_input_query(struct ifnet *, const struct ip6_hdr *,
140 struct mbuf *, const int, const int);
141 static int mld_v2_merge_state_changes(struct in6_multi *,
143 static void mld_v2_process_group_timers(struct mld_ifinfo *,
144 struct ifqueue *, struct ifqueue *,
145 struct in6_multi *, const int);
146 static int mld_v2_process_group_query(struct in6_multi *,
147 struct mld_ifinfo *mli, int, struct mbuf *, const int);
148 static int sysctl_mld_gsr(SYSCTL_HANDLER_ARGS);
149 static int sysctl_mld_ifinfo(SYSCTL_HANDLER_ARGS);
152 * Normative references: RFC 2710, RFC 3590, RFC 3810.
155 * * The MLD subsystem lock ends up being system-wide for the moment,
156 * but could be per-VIMAGE later on.
157 * * The permitted lock order is: IN6_MULTI_LOCK, MLD_LOCK, IF_ADDR_LOCK.
158 * Any may be taken independently; if any are held at the same
159 * time, the above lock order must be followed.
160 * * IN6_MULTI_LOCK covers in_multi.
161 * * MLD_LOCK covers per-link state and any global variables in this file.
162 * * IF_ADDR_LOCK covers if_multiaddrs, which is used for a variety of
163 * per-link state iterators.
166 * A special case for IPv6 is the in6_setscope() routine. ip6_output()
167 * will not accept an ifp; it wants an embedded scope ID, unlike
168 * ip_output(), which happily takes the ifp given to it. The embedded
169 * scope ID is only used by MLD to select the outgoing interface.
171 * During interface attach and detach, MLD will take MLD_LOCK *after*
172 * the IF_AFDATA_LOCK.
173 * As in6_setscope() takes IF_AFDATA_LOCK then SCOPE_LOCK, we can't call
174 * it with MLD_LOCK held without triggering an LOR. A netisr with indirect
175 * dispatch could work around this, but we'd rather not do that, as it
176 * can introduce other races.
178 * As such, we exploit the fact that the scope ID is just the interface
179 * index, and embed it in the IPv6 destination address accordingly.
180 * This is potentially NOT VALID for MLDv1 reports, as they
181 * are always sent to the multicast group itself; as MLDv2
182 * reports are always sent to ff02::16, this is not an issue
183 * when MLDv2 is in use.
185 * This does not however eliminate the LOR when ip6_output() itself
186 * calls in6_setscope() internally whilst MLD_LOCK is held. This will
187 * trigger a LOR warning in WITNESS when the ifnet is detached.
189 * The right answer is probably to make IF_AFDATA_LOCK an rwlock, given
190 * how it's used across the network stack. Here we're simply exploiting
191 * the fact that MLD runs at a similar layer in the stack to scope6.c.
194 * * Each in6_multi corresponds to an ifp, and each ifp corresponds
195 * to a vnet in ifp->if_vnet.
197 static struct mtx mld_mtx;
198 static MALLOC_DEFINE(M_MLD, "mld", "mld state");
200 #define MLD_EMBEDSCOPE(pin6, zoneid) \
201 if (IN6_IS_SCOPE_LINKLOCAL(pin6) || \
202 IN6_IS_ADDR_MC_INTFACELOCAL(pin6)) \
203 (pin6)->s6_addr16[1] = htons((zoneid) & 0xFFFF) \
206 * VIMAGE-wide globals.
208 static VNET_DEFINE(struct timeval, mld_gsrdelay) = {10, 0};
209 static VNET_DEFINE(LIST_HEAD(, mld_ifinfo), mli_head);
210 static VNET_DEFINE(int, interface_timers_running6);
211 static VNET_DEFINE(int, state_change_timers_running6);
212 static VNET_DEFINE(int, current_state_timers_running6);
214 #define V_mld_gsrdelay VNET(mld_gsrdelay)
215 #define V_mli_head VNET(mli_head)
216 #define V_interface_timers_running6 VNET(interface_timers_running6)
217 #define V_state_change_timers_running6 VNET(state_change_timers_running6)
218 #define V_current_state_timers_running6 VNET(current_state_timers_running6)
220 SYSCTL_DECL(_net_inet6); /* Note: Not in any common header. */
222 SYSCTL_NODE(_net_inet6, OID_AUTO, mld, CTLFLAG_RW, 0,
223 "IPv6 Multicast Listener Discovery");
226 * Virtualized sysctls.
228 SYSCTL_PROC(_net_inet6_mld, OID_AUTO, gsrdelay,
229 CTLFLAG_VNET | CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
230 &VNET_NAME(mld_gsrdelay.tv_sec), 0, sysctl_mld_gsr, "I",
231 "Rate limit for MLDv2 Group-and-Source queries in seconds");
234 * Non-virtualized sysctls.
236 static SYSCTL_NODE(_net_inet6_mld, OID_AUTO, ifinfo,
237 CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_mld_ifinfo,
238 "Per-interface MLDv2 state");
240 static int mld_v1enable = 1;
241 SYSCTL_INT(_net_inet6_mld, OID_AUTO, v1enable, CTLFLAG_RWTUN,
242 &mld_v1enable, 0, "Enable fallback to MLDv1");
244 static int mld_use_allow = 1;
245 SYSCTL_INT(_net_inet6_mld, OID_AUTO, use_allow, CTLFLAG_RWTUN,
246 &mld_use_allow, 0, "Use ALLOW/BLOCK for RFC 4604 SSM joins/leaves");
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.PH_loc.ptr = ifp->if_vnet;
279 m->m_pkthdr.flowid = ifp->if_index;
283 mld_scrub_context(struct mbuf *m)
286 m->m_pkthdr.PH_loc.ptr = 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.PH_loc.ptr,
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, if_name(ifp));
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, if_name(ifp));
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, if_name(ifp));
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 if_name(inm->in6m_ifp), 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, if_name(ifp));
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 >= 32768) {
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 * A general query with a source list has undefined
870 * behaviour; discard it.
874 is_general_query = 1;
877 * Embed scope ID of receiving interface in MLD query for
878 * lookup whilst we don't hold other locks (due to KAME
879 * locking lameness). We own this mbuf chain just now.
881 in6_setscope(&mld->mld_addr, ifp, NULL);
887 mli = MLD_IFINFO(ifp);
888 KASSERT(mli != NULL, ("%s: no mld_ifinfo for ifp %p", __func__, ifp));
891 * Discard the v2 query if we're in Compatibility Mode.
892 * The RFC is pretty clear that hosts need to stay in MLDv1 mode
893 * until the Old Version Querier Present timer expires.
895 if (mli->mli_version != MLD_VERSION_2)
898 mld_set_version(mli, MLD_VERSION_2);
901 mli->mli_qri = maxdelay;
903 CTR4(KTR_MLD, "%s: qrv %d qi %d maxdelay %d", __func__, qrv, qqi,
906 if (is_general_query) {
908 * MLDv2 General Query.
910 * Schedule a current-state report on this ifp for
911 * all groups, possibly containing source lists.
913 * If there is a pending General Query response
914 * scheduled earlier than the selected delay, do
915 * not schedule any other reports.
916 * Otherwise, reset the interface timer.
918 CTR2(KTR_MLD, "process v2 general query on ifp %p(%s)",
920 if (mli->mli_v2_timer == 0 || mli->mli_v2_timer >= timer) {
921 mli->mli_v2_timer = MLD_RANDOM_DELAY(timer);
922 V_interface_timers_running6 = 1;
926 * MLDv2 Group-specific or Group-and-source-specific Query.
928 * Group-source-specific queries are throttled on
929 * a per-group basis to defeat denial-of-service attempts.
930 * Queries for groups we are not a member of on this
931 * link are simply ignored.
934 inm = in6m_lookup_locked(ifp, &mld->mld_addr);
936 IF_ADDR_RUNLOCK(ifp);
940 if (!ratecheck(&inm->in6m_lastgsrtv,
942 CTR1(KTR_MLD, "%s: GS query throttled.",
944 IF_ADDR_RUNLOCK(ifp);
948 CTR2(KTR_MLD, "process v2 group query on ifp %p(%s)",
951 * If there is a pending General Query response
952 * scheduled sooner than the selected delay, no
953 * further report need be scheduled.
954 * Otherwise, prepare to respond to the
955 * group-specific or group-and-source query.
957 if (mli->mli_v2_timer == 0 || mli->mli_v2_timer >= timer)
958 mld_v2_process_group_query(inm, mli, timer, m, off);
960 /* XXX Clear embedded scope ID as userland won't expect it. */
961 in6_clearscope(&mld->mld_addr);
962 IF_ADDR_RUNLOCK(ifp);
973 * Process a recieved MLDv2 group-specific or group-and-source-specific
975 * Return <0 if any error occured. Currently this is ignored.
978 mld_v2_process_group_query(struct in6_multi *inm, struct mld_ifinfo *mli,
979 int timer, struct mbuf *m0, const int off)
981 struct mldv2_query *mld;
985 IN6_MULTI_LOCK_ASSERT();
989 mld = (struct mldv2_query *)(mtod(m0, uint8_t *) + off);
991 switch (inm->in6m_state) {
993 case MLD_SILENT_MEMBER:
994 case MLD_SLEEPING_MEMBER:
995 case MLD_LAZY_MEMBER:
996 case MLD_AWAKENING_MEMBER:
997 case MLD_IDLE_MEMBER:
998 case MLD_LEAVING_MEMBER:
1001 case MLD_REPORTING_MEMBER:
1002 case MLD_G_QUERY_PENDING_MEMBER:
1003 case MLD_SG_QUERY_PENDING_MEMBER:
1007 nsrc = ntohs(mld->mld_numsrc);
1010 * Deal with group-specific queries upfront.
1011 * If any group query is already pending, purge any recorded
1012 * source-list state if it exists, and schedule a query response
1013 * for this group-specific query.
1016 if (inm->in6m_state == MLD_G_QUERY_PENDING_MEMBER ||
1017 inm->in6m_state == MLD_SG_QUERY_PENDING_MEMBER) {
1018 in6m_clear_recorded(inm);
1019 timer = min(inm->in6m_timer, timer);
1021 inm->in6m_state = MLD_G_QUERY_PENDING_MEMBER;
1022 inm->in6m_timer = MLD_RANDOM_DELAY(timer);
1023 V_current_state_timers_running6 = 1;
1028 * Deal with the case where a group-and-source-specific query has
1029 * been received but a group-specific query is already pending.
1031 if (inm->in6m_state == MLD_G_QUERY_PENDING_MEMBER) {
1032 timer = min(inm->in6m_timer, timer);
1033 inm->in6m_timer = MLD_RANDOM_DELAY(timer);
1034 V_current_state_timers_running6 = 1;
1039 * Finally, deal with the case where a group-and-source-specific
1040 * query has been received, where a response to a previous g-s-r
1041 * query exists, or none exists.
1042 * In this case, we need to parse the source-list which the Querier
1043 * has provided us with and check if we have any source list filter
1044 * entries at T1 for these sources. If we do not, there is no need
1045 * schedule a report and the query may be dropped.
1046 * If we do, we must record them and schedule a current-state
1047 * report for those sources.
1049 if (inm->in6m_nsrc > 0) {
1056 soff = off + sizeof(struct mldv2_query);
1058 for (i = 0; i < nsrc; i++) {
1059 sp = mtod(m, uint8_t *) + soff;
1060 retval = in6m_record_source(inm,
1061 (const struct in6_addr *)sp);
1064 nrecorded += retval;
1065 soff += sizeof(struct in6_addr);
1066 if (soff >= m->m_len) {
1067 soff = soff - m->m_len;
1073 if (nrecorded > 0) {
1075 "%s: schedule response to SG query", __func__);
1076 inm->in6m_state = MLD_SG_QUERY_PENDING_MEMBER;
1077 inm->in6m_timer = MLD_RANDOM_DELAY(timer);
1078 V_current_state_timers_running6 = 1;
1086 * Process a received MLDv1 host membership report.
1087 * Assumes mld points to mld_hdr in pulled up mbuf chain.
1089 * NOTE: Can't be fully const correct as we temporarily embed scope ID in
1090 * mld_addr. This is OK as we own the mbuf chain.
1093 mld_v1_input_report(struct ifnet *ifp, const struct ip6_hdr *ip6,
1094 /*const*/ struct mld_hdr *mld)
1096 struct in6_addr src, dst;
1097 struct in6_ifaddr *ia;
1098 struct in6_multi *inm;
1100 char ip6tbuf[INET6_ADDRSTRLEN];
1103 if (!mld_v1enable) {
1104 CTR3(KTR_MLD, "ignore v1 report %s on ifp %p(%s)",
1105 ip6_sprintf(ip6tbuf, &mld->mld_addr),
1110 if (ifp->if_flags & IFF_LOOPBACK)
1114 * MLDv1 reports must originate from a host's link-local address,
1115 * or the unspecified address (when booting).
1118 in6_clearscope(&src);
1119 if (!IN6_IS_SCOPE_LINKLOCAL(&src) && !IN6_IS_ADDR_UNSPECIFIED(&src)) {
1120 CTR3(KTR_MLD, "ignore v1 query src %s on ifp %p(%s)",
1121 ip6_sprintf(ip6tbuf, &ip6->ip6_src),
1127 * RFC2710 Section 4: MLDv1 reports must pertain to a multicast
1128 * group, and must be directed to the group itself.
1131 in6_clearscope(&dst);
1132 if (!IN6_IS_ADDR_MULTICAST(&mld->mld_addr) ||
1133 !IN6_ARE_ADDR_EQUAL(&mld->mld_addr, &dst)) {
1134 CTR3(KTR_MLD, "ignore v1 query dst %s on ifp %p(%s)",
1135 ip6_sprintf(ip6tbuf, &ip6->ip6_dst),
1141 * Make sure we don't hear our own membership report, as fast
1142 * leave requires knowing that we are the only member of a
1143 * group. Assume we used the link-local address if available,
1144 * otherwise look for ::.
1146 * XXX Note that scope ID comparison is needed for the address
1147 * returned by in6ifa_ifpforlinklocal(), but SHOULD NOT be
1148 * performed for the on-wire address.
1150 ia = in6ifa_ifpforlinklocal(ifp, IN6_IFF_NOTREADY|IN6_IFF_ANYCAST);
1151 if ((ia && IN6_ARE_ADDR_EQUAL(&ip6->ip6_src, IA6_IN6(ia))) ||
1152 (ia == NULL && IN6_IS_ADDR_UNSPECIFIED(&src))) {
1154 ifa_free(&ia->ia_ifa);
1158 ifa_free(&ia->ia_ifa);
1160 CTR3(KTR_MLD, "process v1 report %s on ifp %p(%s)",
1161 ip6_sprintf(ip6tbuf, &mld->mld_addr), ifp, if_name(ifp));
1164 * Embed scope ID of receiving interface in MLD query for lookup
1165 * whilst we don't hold other locks (due to KAME locking lameness).
1167 if (!IN6_IS_ADDR_UNSPECIFIED(&mld->mld_addr))
1168 in6_setscope(&mld->mld_addr, ifp, NULL);
1175 * MLDv1 report suppression.
1176 * If we are a member of this group, and our membership should be
1177 * reported, and our group timer is pending or about to be reset,
1178 * stop our group timer by transitioning to the 'lazy' state.
1180 inm = in6m_lookup_locked(ifp, &mld->mld_addr);
1182 struct mld_ifinfo *mli;
1184 mli = inm->in6m_mli;
1185 KASSERT(mli != NULL,
1186 ("%s: no mli for ifp %p", __func__, ifp));
1189 * If we are in MLDv2 host mode, do not allow the
1190 * other host's MLDv1 report to suppress our reports.
1192 if (mli->mli_version == MLD_VERSION_2)
1195 inm->in6m_timer = 0;
1197 switch (inm->in6m_state) {
1198 case MLD_NOT_MEMBER:
1199 case MLD_SILENT_MEMBER:
1200 case MLD_SLEEPING_MEMBER:
1202 case MLD_REPORTING_MEMBER:
1203 case MLD_IDLE_MEMBER:
1204 case MLD_AWAKENING_MEMBER:
1206 "report suppressed for %s on ifp %p(%s)",
1207 ip6_sprintf(ip6tbuf, &mld->mld_addr),
1209 case MLD_LAZY_MEMBER:
1210 inm->in6m_state = MLD_LAZY_MEMBER;
1212 case MLD_G_QUERY_PENDING_MEMBER:
1213 case MLD_SG_QUERY_PENDING_MEMBER:
1214 case MLD_LEAVING_MEMBER:
1220 IF_ADDR_RUNLOCK(ifp);
1224 /* XXX Clear embedded scope ID as userland won't expect it. */
1225 in6_clearscope(&mld->mld_addr);
1233 * Assume query messages which fit in a single ICMPv6 message header
1234 * have been pulled up.
1235 * Assume that userland will want to see the message, even if it
1236 * otherwise fails kernel input validation; do not free it.
1237 * Pullup may however free the mbuf chain m if it fails.
1239 * Return IPPROTO_DONE if we freed m. Otherwise, return 0.
1242 mld_input(struct mbuf *m, int off, int icmp6len)
1245 struct ip6_hdr *ip6;
1246 struct mld_hdr *mld;
1249 CTR3(KTR_MLD, "%s: called w/mbuf (%p,%d)", __func__, m, off);
1251 ifp = m->m_pkthdr.rcvif;
1253 ip6 = mtod(m, struct ip6_hdr *);
1255 /* Pullup to appropriate size. */
1256 mld = (struct mld_hdr *)(mtod(m, uint8_t *) + off);
1257 if (mld->mld_type == MLD_LISTENER_QUERY &&
1258 icmp6len >= sizeof(struct mldv2_query)) {
1259 mldlen = sizeof(struct mldv2_query);
1261 mldlen = sizeof(struct mld_hdr);
1263 IP6_EXTHDR_GET(mld, struct mld_hdr *, m, off, mldlen);
1265 ICMP6STAT_INC(icp6s_badlen);
1266 return (IPPROTO_DONE);
1270 * Userland needs to see all of this traffic for implementing
1271 * the endpoint discovery portion of multicast routing.
1273 switch (mld->mld_type) {
1274 case MLD_LISTENER_QUERY:
1275 icmp6_ifstat_inc(ifp, ifs6_in_mldquery);
1276 if (icmp6len == sizeof(struct mld_hdr)) {
1277 if (mld_v1_input_query(ifp, ip6, mld) != 0)
1279 } else if (icmp6len >= sizeof(struct mldv2_query)) {
1280 if (mld_v2_input_query(ifp, ip6, m, off,
1285 case MLD_LISTENER_REPORT:
1286 icmp6_ifstat_inc(ifp, ifs6_in_mldreport);
1287 if (mld_v1_input_report(ifp, ip6, mld) != 0)
1290 case MLDV2_LISTENER_REPORT:
1291 icmp6_ifstat_inc(ifp, ifs6_in_mldreport);
1293 case MLD_LISTENER_DONE:
1294 icmp6_ifstat_inc(ifp, ifs6_in_mlddone);
1304 * Fast timeout handler (global).
1305 * VIMAGE: Timeout handlers are expected to service all vimages.
1310 VNET_ITERATOR_DECL(vnet_iter);
1312 VNET_LIST_RLOCK_NOSLEEP();
1313 VNET_FOREACH(vnet_iter) {
1314 CURVNET_SET(vnet_iter);
1315 mld_fasttimo_vnet();
1318 VNET_LIST_RUNLOCK_NOSLEEP();
1322 * Fast timeout handler (per-vnet).
1324 * VIMAGE: Assume caller has set up our curvnet.
1327 mld_fasttimo_vnet(void)
1329 struct ifqueue scq; /* State-change packets */
1330 struct ifqueue qrq; /* Query response packets */
1332 struct mld_ifinfo *mli;
1333 struct ifmultiaddr *ifma;
1334 struct in6_multi *inm, *tinm;
1340 * Quick check to see if any work needs to be done, in order to
1341 * minimize the overhead of fasttimo processing.
1342 * SMPng: XXX Unlocked reads.
1344 if (!V_current_state_timers_running6 &&
1345 !V_interface_timers_running6 &&
1346 !V_state_change_timers_running6)
1353 * MLDv2 General Query response timer processing.
1355 if (V_interface_timers_running6) {
1356 CTR1(KTR_MLD, "%s: interface timers running", __func__);
1358 V_interface_timers_running6 = 0;
1359 LIST_FOREACH(mli, &V_mli_head, mli_link) {
1360 if (mli->mli_v2_timer == 0) {
1362 } else if (--mli->mli_v2_timer == 0) {
1363 mld_v2_dispatch_general_query(mli);
1365 V_interface_timers_running6 = 1;
1370 if (!V_current_state_timers_running6 &&
1371 !V_state_change_timers_running6)
1374 V_current_state_timers_running6 = 0;
1375 V_state_change_timers_running6 = 0;
1377 CTR1(KTR_MLD, "%s: state change timers running", __func__);
1380 * MLD host report and state-change timer processing.
1381 * Note: Processing a v2 group timer may remove a node.
1383 LIST_FOREACH(mli, &V_mli_head, mli_link) {
1386 if (mli->mli_version == MLD_VERSION_2) {
1387 uri_fasthz = MLD_RANDOM_DELAY(mli->mli_uri *
1390 memset(&qrq, 0, sizeof(struct ifqueue));
1391 IFQ_SET_MAXLEN(&qrq, MLD_MAX_G_GS_PACKETS);
1393 memset(&scq, 0, sizeof(struct ifqueue));
1394 IFQ_SET_MAXLEN(&scq, MLD_MAX_STATE_CHANGE_PACKETS);
1398 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
1399 if (ifma->ifma_addr->sa_family != AF_INET6 ||
1400 ifma->ifma_protospec == NULL)
1402 inm = (struct in6_multi *)ifma->ifma_protospec;
1403 switch (mli->mli_version) {
1405 mld_v1_process_group_timer(mli, inm);
1408 mld_v2_process_group_timers(mli, &qrq,
1409 &scq, inm, uri_fasthz);
1413 IF_ADDR_RUNLOCK(ifp);
1415 switch (mli->mli_version) {
1418 * Transmit reports for this lifecycle. This
1419 * is done while not holding IF_ADDR_LOCK
1420 * since this can call
1421 * in6ifa_ifpforlinklocal() which locks
1422 * IF_ADDR_LOCK internally as well as
1423 * ip6_output() to transmit a packet.
1425 SLIST_FOREACH_SAFE(inm, &mli->mli_relinmhead,
1427 SLIST_REMOVE_HEAD(&mli->mli_relinmhead,
1429 (void)mld_v1_transmit_report(inm,
1430 MLD_LISTENER_REPORT);
1434 mld_dispatch_queue(&qrq, 0);
1435 mld_dispatch_queue(&scq, 0);
1438 * Free the in_multi reference(s) for
1441 SLIST_FOREACH_SAFE(inm, &mli->mli_relinmhead,
1443 SLIST_REMOVE_HEAD(&mli->mli_relinmhead,
1445 in6m_release_locked(inm);
1457 * Update host report group timer.
1458 * Will update the global pending timer flags.
1461 mld_v1_process_group_timer(struct mld_ifinfo *mli, struct in6_multi *inm)
1463 int report_timer_expired;
1465 IN6_MULTI_LOCK_ASSERT();
1468 if (inm->in6m_timer == 0) {
1469 report_timer_expired = 0;
1470 } else if (--inm->in6m_timer == 0) {
1471 report_timer_expired = 1;
1473 V_current_state_timers_running6 = 1;
1477 switch (inm->in6m_state) {
1478 case MLD_NOT_MEMBER:
1479 case MLD_SILENT_MEMBER:
1480 case MLD_IDLE_MEMBER:
1481 case MLD_LAZY_MEMBER:
1482 case MLD_SLEEPING_MEMBER:
1483 case MLD_AWAKENING_MEMBER:
1485 case MLD_REPORTING_MEMBER:
1486 if (report_timer_expired) {
1487 inm->in6m_state = MLD_IDLE_MEMBER;
1488 SLIST_INSERT_HEAD(&mli->mli_relinmhead, inm,
1492 case MLD_G_QUERY_PENDING_MEMBER:
1493 case MLD_SG_QUERY_PENDING_MEMBER:
1494 case MLD_LEAVING_MEMBER:
1500 * Update a group's timers for MLDv2.
1501 * Will update the global pending timer flags.
1502 * Note: Unlocked read from mli.
1505 mld_v2_process_group_timers(struct mld_ifinfo *mli,
1506 struct ifqueue *qrq, struct ifqueue *scq,
1507 struct in6_multi *inm, const int uri_fasthz)
1509 int query_response_timer_expired;
1510 int state_change_retransmit_timer_expired;
1512 char ip6tbuf[INET6_ADDRSTRLEN];
1515 IN6_MULTI_LOCK_ASSERT();
1518 query_response_timer_expired = 0;
1519 state_change_retransmit_timer_expired = 0;
1522 * During a transition from compatibility mode back to MLDv2,
1523 * a group record in REPORTING state may still have its group
1524 * timer active. This is a no-op in this function; it is easier
1525 * to deal with it here than to complicate the slow-timeout path.
1527 if (inm->in6m_timer == 0) {
1528 query_response_timer_expired = 0;
1529 } else if (--inm->in6m_timer == 0) {
1530 query_response_timer_expired = 1;
1532 V_current_state_timers_running6 = 1;
1535 if (inm->in6m_sctimer == 0) {
1536 state_change_retransmit_timer_expired = 0;
1537 } else if (--inm->in6m_sctimer == 0) {
1538 state_change_retransmit_timer_expired = 1;
1540 V_state_change_timers_running6 = 1;
1543 /* We are in fasttimo, so be quick about it. */
1544 if (!state_change_retransmit_timer_expired &&
1545 !query_response_timer_expired)
1548 switch (inm->in6m_state) {
1549 case MLD_NOT_MEMBER:
1550 case MLD_SILENT_MEMBER:
1551 case MLD_SLEEPING_MEMBER:
1552 case MLD_LAZY_MEMBER:
1553 case MLD_AWAKENING_MEMBER:
1554 case MLD_IDLE_MEMBER:
1556 case MLD_G_QUERY_PENDING_MEMBER:
1557 case MLD_SG_QUERY_PENDING_MEMBER:
1559 * Respond to a previously pending Group-Specific
1560 * or Group-and-Source-Specific query by enqueueing
1561 * the appropriate Current-State report for
1562 * immediate transmission.
1564 if (query_response_timer_expired) {
1567 retval = mld_v2_enqueue_group_record(qrq, inm, 0, 1,
1568 (inm->in6m_state == MLD_SG_QUERY_PENDING_MEMBER),
1570 CTR2(KTR_MLD, "%s: enqueue record = %d",
1572 inm->in6m_state = MLD_REPORTING_MEMBER;
1573 in6m_clear_recorded(inm);
1576 case MLD_REPORTING_MEMBER:
1577 case MLD_LEAVING_MEMBER:
1578 if (state_change_retransmit_timer_expired) {
1580 * State-change retransmission timer fired.
1581 * If there are any further pending retransmissions,
1582 * set the global pending state-change flag, and
1585 if (--inm->in6m_scrv > 0) {
1586 inm->in6m_sctimer = uri_fasthz;
1587 V_state_change_timers_running6 = 1;
1590 * Retransmit the previously computed state-change
1591 * report. If there are no further pending
1592 * retransmissions, the mbuf queue will be consumed.
1593 * Update T0 state to T1 as we have now sent
1596 (void)mld_v2_merge_state_changes(inm, scq);
1599 CTR3(KTR_MLD, "%s: T1 -> T0 for %s/%s", __func__,
1600 ip6_sprintf(ip6tbuf, &inm->in6m_addr),
1601 if_name(inm->in6m_ifp));
1604 * If we are leaving the group for good, make sure
1605 * we release MLD's reference to it.
1606 * This release must be deferred using a SLIST,
1607 * as we are called from a loop which traverses
1608 * the in_ifmultiaddr TAILQ.
1610 if (inm->in6m_state == MLD_LEAVING_MEMBER &&
1611 inm->in6m_scrv == 0) {
1612 inm->in6m_state = MLD_NOT_MEMBER;
1613 SLIST_INSERT_HEAD(&mli->mli_relinmhead,
1622 * Switch to a different version on the given interface,
1623 * as per Section 9.12.
1626 mld_set_version(struct mld_ifinfo *mli, const int version)
1628 int old_version_timer;
1632 CTR4(KTR_MLD, "%s: switching to v%d on ifp %p(%s)", __func__,
1633 version, mli->mli_ifp, if_name(mli->mli_ifp));
1635 if (version == MLD_VERSION_1) {
1637 * Compute the "Older Version Querier Present" timer as per
1640 old_version_timer = (mli->mli_rv * mli->mli_qi) + mli->mli_qri;
1641 old_version_timer *= PR_SLOWHZ;
1642 mli->mli_v1_timer = old_version_timer;
1645 if (mli->mli_v1_timer > 0 && mli->mli_version != MLD_VERSION_1) {
1646 mli->mli_version = MLD_VERSION_1;
1647 mld_v2_cancel_link_timers(mli);
1652 * Cancel pending MLDv2 timers for the given link and all groups
1653 * joined on it; state-change, general-query, and group-query timers.
1656 mld_v2_cancel_link_timers(struct mld_ifinfo *mli)
1658 struct ifmultiaddr *ifma;
1660 struct in6_multi *inm, *tinm;
1662 CTR3(KTR_MLD, "%s: cancel v2 timers on ifp %p(%s)", __func__,
1663 mli->mli_ifp, if_name(mli->mli_ifp));
1665 IN6_MULTI_LOCK_ASSERT();
1669 * Fast-track this potentially expensive operation
1670 * by checking all the global 'timer pending' flags.
1672 if (!V_interface_timers_running6 &&
1673 !V_state_change_timers_running6 &&
1674 !V_current_state_timers_running6)
1677 mli->mli_v2_timer = 0;
1682 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
1683 if (ifma->ifma_addr->sa_family != AF_INET6)
1685 inm = (struct in6_multi *)ifma->ifma_protospec;
1686 switch (inm->in6m_state) {
1687 case MLD_NOT_MEMBER:
1688 case MLD_SILENT_MEMBER:
1689 case MLD_IDLE_MEMBER:
1690 case MLD_LAZY_MEMBER:
1691 case MLD_SLEEPING_MEMBER:
1692 case MLD_AWAKENING_MEMBER:
1694 case MLD_LEAVING_MEMBER:
1696 * If we are leaving the group and switching
1697 * version, we need to release the final
1698 * reference held for issuing the INCLUDE {}.
1700 SLIST_INSERT_HEAD(&mli->mli_relinmhead, inm,
1703 case MLD_G_QUERY_PENDING_MEMBER:
1704 case MLD_SG_QUERY_PENDING_MEMBER:
1705 in6m_clear_recorded(inm);
1707 case MLD_REPORTING_MEMBER:
1708 inm->in6m_sctimer = 0;
1709 inm->in6m_timer = 0;
1710 inm->in6m_state = MLD_REPORTING_MEMBER;
1712 * Free any pending MLDv2 state-change records.
1714 _IF_DRAIN(&inm->in6m_scq);
1718 IF_ADDR_RUNLOCK(ifp);
1719 SLIST_FOREACH_SAFE(inm, &mli->mli_relinmhead, in6m_nrele, tinm) {
1720 SLIST_REMOVE_HEAD(&mli->mli_relinmhead, in6m_nrele);
1721 in6m_release_locked(inm);
1726 * Global slowtimo handler.
1727 * VIMAGE: Timeout handlers are expected to service all vimages.
1732 VNET_ITERATOR_DECL(vnet_iter);
1734 VNET_LIST_RLOCK_NOSLEEP();
1735 VNET_FOREACH(vnet_iter) {
1736 CURVNET_SET(vnet_iter);
1737 mld_slowtimo_vnet();
1740 VNET_LIST_RUNLOCK_NOSLEEP();
1744 * Per-vnet slowtimo handler.
1747 mld_slowtimo_vnet(void)
1749 struct mld_ifinfo *mli;
1753 LIST_FOREACH(mli, &V_mli_head, mli_link) {
1754 mld_v1_process_querier_timers(mli);
1761 * Update the Older Version Querier Present timers for a link.
1762 * See Section 9.12 of RFC 3810.
1765 mld_v1_process_querier_timers(struct mld_ifinfo *mli)
1770 if (mli->mli_version != MLD_VERSION_2 && --mli->mli_v1_timer == 0) {
1772 * MLDv1 Querier Present timer expired; revert to MLDv2.
1775 "%s: transition from v%d -> v%d on %p(%s)",
1776 __func__, mli->mli_version, MLD_VERSION_2,
1777 mli->mli_ifp, if_name(mli->mli_ifp));
1778 mli->mli_version = MLD_VERSION_2;
1783 * Transmit an MLDv1 report immediately.
1786 mld_v1_transmit_report(struct in6_multi *in6m, const int type)
1789 struct in6_ifaddr *ia;
1790 struct ip6_hdr *ip6;
1791 struct mbuf *mh, *md;
1792 struct mld_hdr *mld;
1794 IN6_MULTI_LOCK_ASSERT();
1797 ifp = in6m->in6m_ifp;
1798 ia = in6ifa_ifpforlinklocal(ifp, IN6_IFF_NOTREADY|IN6_IFF_ANYCAST);
1799 /* ia may be NULL if link-local address is tentative. */
1801 mh = m_gethdr(M_NOWAIT, MT_DATA);
1804 ifa_free(&ia->ia_ifa);
1807 md = m_get(M_NOWAIT, MT_DATA);
1811 ifa_free(&ia->ia_ifa);
1817 * FUTURE: Consider increasing alignment by ETHER_HDR_LEN, so
1818 * that ether_output() does not need to allocate another mbuf
1819 * for the header in the most common case.
1821 MH_ALIGN(mh, sizeof(struct ip6_hdr));
1822 mh->m_pkthdr.len = sizeof(struct ip6_hdr) + sizeof(struct mld_hdr);
1823 mh->m_len = sizeof(struct ip6_hdr);
1825 ip6 = mtod(mh, struct ip6_hdr *);
1827 ip6->ip6_vfc &= ~IPV6_VERSION_MASK;
1828 ip6->ip6_vfc |= IPV6_VERSION;
1829 ip6->ip6_nxt = IPPROTO_ICMPV6;
1830 ip6->ip6_src = ia ? ia->ia_addr.sin6_addr : in6addr_any;
1831 ip6->ip6_dst = in6m->in6m_addr;
1833 md->m_len = sizeof(struct mld_hdr);
1834 mld = mtod(md, struct mld_hdr *);
1835 mld->mld_type = type;
1838 mld->mld_maxdelay = 0;
1839 mld->mld_reserved = 0;
1840 mld->mld_addr = in6m->in6m_addr;
1841 in6_clearscope(&mld->mld_addr);
1842 mld->mld_cksum = in6_cksum(mh, IPPROTO_ICMPV6,
1843 sizeof(struct ip6_hdr), sizeof(struct mld_hdr));
1845 mld_save_context(mh, ifp);
1846 mh->m_flags |= M_MLDV1;
1848 mld_dispatch_packet(mh);
1851 ifa_free(&ia->ia_ifa);
1856 * Process a state change from the upper layer for the given IPv6 group.
1858 * Each socket holds a reference on the in_multi in its own ip_moptions.
1859 * The socket layer will have made the necessary updates to.the group
1860 * state, it is now up to MLD to issue a state change report if there
1861 * has been any change between T0 (when the last state-change was issued)
1864 * We use the MLDv2 state machine at group level. The MLd module
1865 * however makes the decision as to which MLD protocol version to speak.
1866 * A state change *from* INCLUDE {} always means an initial join.
1867 * A state change *to* INCLUDE {} always means a final leave.
1869 * If delay is non-zero, and the state change is an initial multicast
1870 * join, the state change report will be delayed by 'delay' ticks
1871 * in units of PR_FASTHZ if MLDv1 is active on the link; otherwise
1872 * the initial MLDv2 state change report will be delayed by whichever
1873 * is sooner, a pending state-change timer or delay itself.
1875 * VIMAGE: curvnet should have been set by caller, as this routine
1876 * is called from the socket option handlers.
1879 mld_change_state(struct in6_multi *inm, const int delay)
1881 struct mld_ifinfo *mli;
1885 IN6_MULTI_LOCK_ASSERT();
1890 * Try to detect if the upper layer just asked us to change state
1891 * for an interface which has now gone away.
1893 KASSERT(inm->in6m_ifma != NULL, ("%s: no ifma", __func__));
1894 ifp = inm->in6m_ifma->ifma_ifp;
1897 * Sanity check that netinet6's notion of ifp is the
1900 KASSERT(inm->in6m_ifp == ifp, ("%s: bad ifp", __func__));
1905 mli = MLD_IFINFO(ifp);
1906 KASSERT(mli != NULL, ("%s: no mld_ifinfo for ifp %p", __func__, ifp));
1909 * If we detect a state transition to or from MCAST_UNDEFINED
1910 * for this group, then we are starting or finishing an MLD
1911 * life cycle for this group.
1913 if (inm->in6m_st[1].iss_fmode != inm->in6m_st[0].iss_fmode) {
1914 CTR3(KTR_MLD, "%s: inm transition %d -> %d", __func__,
1915 inm->in6m_st[0].iss_fmode, inm->in6m_st[1].iss_fmode);
1916 if (inm->in6m_st[0].iss_fmode == MCAST_UNDEFINED) {
1917 CTR1(KTR_MLD, "%s: initial join", __func__);
1918 error = mld_initial_join(inm, mli, delay);
1920 } else if (inm->in6m_st[1].iss_fmode == MCAST_UNDEFINED) {
1921 CTR1(KTR_MLD, "%s: final leave", __func__);
1922 mld_final_leave(inm, mli);
1926 CTR1(KTR_MLD, "%s: filter set change", __func__);
1929 error = mld_handle_state_change(inm, mli);
1937 * Perform the initial join for an MLD group.
1939 * When joining a group:
1940 * If the group should have its MLD traffic suppressed, do nothing.
1941 * MLDv1 starts sending MLDv1 host membership reports.
1942 * MLDv2 will schedule an MLDv2 state-change report containing the
1943 * initial state of the membership.
1945 * If the delay argument is non-zero, then we must delay sending the
1946 * initial state change for delay ticks (in units of PR_FASTHZ).
1949 mld_initial_join(struct in6_multi *inm, struct mld_ifinfo *mli,
1953 struct ifqueue *ifq;
1954 int error, retval, syncstates;
1957 char ip6tbuf[INET6_ADDRSTRLEN];
1960 CTR4(KTR_MLD, "%s: initial join %s on ifp %p(%s)",
1961 __func__, ip6_sprintf(ip6tbuf, &inm->in6m_addr),
1962 inm->in6m_ifp, if_name(inm->in6m_ifp));
1967 ifp = inm->in6m_ifp;
1969 IN6_MULTI_LOCK_ASSERT();
1972 KASSERT(mli && mli->mli_ifp == ifp, ("%s: inconsistent ifp", __func__));
1975 * Groups joined on loopback or marked as 'not reported',
1976 * enter the MLD_SILENT_MEMBER state and
1977 * are never reported in any protocol exchanges.
1978 * All other groups enter the appropriate state machine
1979 * for the version in use on this link.
1980 * A link marked as MLIF_SILENT causes MLD to be completely
1981 * disabled for the link.
1983 if ((ifp->if_flags & IFF_LOOPBACK) ||
1984 (mli->mli_flags & MLIF_SILENT) ||
1985 !mld_is_addr_reported(&inm->in6m_addr)) {
1987 "%s: not kicking state machine for silent group", __func__);
1988 inm->in6m_state = MLD_SILENT_MEMBER;
1989 inm->in6m_timer = 0;
1992 * Deal with overlapping in_multi lifecycle.
1993 * If this group was LEAVING, then make sure
1994 * we drop the reference we picked up to keep the
1995 * group around for the final INCLUDE {} enqueue.
1997 if (mli->mli_version == MLD_VERSION_2 &&
1998 inm->in6m_state == MLD_LEAVING_MEMBER)
1999 in6m_release_locked(inm);
2001 inm->in6m_state = MLD_REPORTING_MEMBER;
2003 switch (mli->mli_version) {
2006 * If a delay was provided, only use it if
2007 * it is greater than the delay normally
2008 * used for an MLDv1 state change report,
2009 * and delay sending the initial MLDv1 report
2010 * by not transitioning to the IDLE state.
2012 odelay = MLD_RANDOM_DELAY(MLD_V1_MAX_RI * PR_FASTHZ);
2014 inm->in6m_timer = max(delay, odelay);
2015 V_current_state_timers_running6 = 1;
2017 inm->in6m_state = MLD_IDLE_MEMBER;
2018 error = mld_v1_transmit_report(inm,
2019 MLD_LISTENER_REPORT);
2021 inm->in6m_timer = odelay;
2022 V_current_state_timers_running6 = 1;
2029 * Defer update of T0 to T1, until the first copy
2030 * of the state change has been transmitted.
2035 * Immediately enqueue a State-Change Report for
2036 * this interface, freeing any previous reports.
2037 * Don't kick the timers if there is nothing to do,
2038 * or if an error occurred.
2040 ifq = &inm->in6m_scq;
2042 retval = mld_v2_enqueue_group_record(ifq, inm, 1,
2043 0, 0, (mli->mli_flags & MLIF_USEALLOW));
2044 CTR2(KTR_MLD, "%s: enqueue record = %d",
2047 error = retval * -1;
2052 * Schedule transmission of pending state-change
2053 * report up to RV times for this link. The timer
2054 * will fire at the next mld_fasttimo (~200ms),
2055 * giving us an opportunity to merge the reports.
2057 * If a delay was provided to this function, only
2058 * use this delay if sooner than the existing one.
2060 KASSERT(mli->mli_rv > 1,
2061 ("%s: invalid robustness %d", __func__,
2063 inm->in6m_scrv = mli->mli_rv;
2065 if (inm->in6m_sctimer > 1) {
2067 min(inm->in6m_sctimer, delay);
2069 inm->in6m_sctimer = delay;
2071 inm->in6m_sctimer = 1;
2072 V_state_change_timers_running6 = 1;
2080 * Only update the T0 state if state change is atomic,
2081 * i.e. we don't need to wait for a timer to fire before we
2082 * can consider the state change to have been communicated.
2086 CTR3(KTR_MLD, "%s: T1 -> T0 for %s/%s", __func__,
2087 ip6_sprintf(ip6tbuf, &inm->in6m_addr),
2088 if_name(inm->in6m_ifp));
2095 * Issue an intermediate state change during the life-cycle.
2098 mld_handle_state_change(struct in6_multi *inm, struct mld_ifinfo *mli)
2103 char ip6tbuf[INET6_ADDRSTRLEN];
2106 CTR4(KTR_MLD, "%s: state change for %s on ifp %p(%s)",
2107 __func__, ip6_sprintf(ip6tbuf, &inm->in6m_addr),
2108 inm->in6m_ifp, if_name(inm->in6m_ifp));
2110 ifp = inm->in6m_ifp;
2112 IN6_MULTI_LOCK_ASSERT();
2115 KASSERT(mli && mli->mli_ifp == ifp,
2116 ("%s: inconsistent ifp", __func__));
2118 if ((ifp->if_flags & IFF_LOOPBACK) ||
2119 (mli->mli_flags & MLIF_SILENT) ||
2120 !mld_is_addr_reported(&inm->in6m_addr) ||
2121 (mli->mli_version != MLD_VERSION_2)) {
2122 if (!mld_is_addr_reported(&inm->in6m_addr)) {
2124 "%s: not kicking state machine for silent group", __func__);
2126 CTR1(KTR_MLD, "%s: nothing to do", __func__);
2128 CTR3(KTR_MLD, "%s: T1 -> T0 for %s/%s", __func__,
2129 ip6_sprintf(ip6tbuf, &inm->in6m_addr),
2130 if_name(inm->in6m_ifp));
2134 _IF_DRAIN(&inm->in6m_scq);
2136 retval = mld_v2_enqueue_group_record(&inm->in6m_scq, inm, 1, 0, 0,
2137 (mli->mli_flags & MLIF_USEALLOW));
2138 CTR2(KTR_MLD, "%s: enqueue record = %d", __func__, retval);
2143 * If record(s) were enqueued, start the state-change
2144 * report timer for this group.
2146 inm->in6m_scrv = mli->mli_rv;
2147 inm->in6m_sctimer = 1;
2148 V_state_change_timers_running6 = 1;
2154 * Perform the final leave for a multicast address.
2156 * When leaving a group:
2157 * MLDv1 sends a DONE message, if and only if we are the reporter.
2158 * MLDv2 enqueues a state-change report containing a transition
2159 * to INCLUDE {} for immediate transmission.
2162 mld_final_leave(struct in6_multi *inm, struct mld_ifinfo *mli)
2166 char ip6tbuf[INET6_ADDRSTRLEN];
2171 CTR4(KTR_MLD, "%s: final leave %s on ifp %p(%s)",
2172 __func__, ip6_sprintf(ip6tbuf, &inm->in6m_addr),
2173 inm->in6m_ifp, if_name(inm->in6m_ifp));
2175 IN6_MULTI_LOCK_ASSERT();
2178 switch (inm->in6m_state) {
2179 case MLD_NOT_MEMBER:
2180 case MLD_SILENT_MEMBER:
2181 case MLD_LEAVING_MEMBER:
2182 /* Already leaving or left; do nothing. */
2184 "%s: not kicking state machine for silent group", __func__);
2186 case MLD_REPORTING_MEMBER:
2187 case MLD_IDLE_MEMBER:
2188 case MLD_G_QUERY_PENDING_MEMBER:
2189 case MLD_SG_QUERY_PENDING_MEMBER:
2190 if (mli->mli_version == MLD_VERSION_1) {
2192 if (inm->in6m_state == MLD_G_QUERY_PENDING_MEMBER ||
2193 inm->in6m_state == MLD_SG_QUERY_PENDING_MEMBER)
2194 panic("%s: MLDv2 state reached, not MLDv2 mode",
2197 mld_v1_transmit_report(inm, MLD_LISTENER_DONE);
2198 inm->in6m_state = MLD_NOT_MEMBER;
2199 V_current_state_timers_running6 = 1;
2200 } else if (mli->mli_version == MLD_VERSION_2) {
2202 * Stop group timer and all pending reports.
2203 * Immediately enqueue a state-change report
2204 * TO_IN {} to be sent on the next fast timeout,
2205 * giving us an opportunity to merge reports.
2207 _IF_DRAIN(&inm->in6m_scq);
2208 inm->in6m_timer = 0;
2209 inm->in6m_scrv = mli->mli_rv;
2210 CTR4(KTR_MLD, "%s: Leaving %s/%s with %d "
2211 "pending retransmissions.", __func__,
2212 ip6_sprintf(ip6tbuf, &inm->in6m_addr),
2213 if_name(inm->in6m_ifp), inm->in6m_scrv);
2214 if (inm->in6m_scrv == 0) {
2215 inm->in6m_state = MLD_NOT_MEMBER;
2216 inm->in6m_sctimer = 0;
2220 in6m_acquire_locked(inm);
2222 retval = mld_v2_enqueue_group_record(
2223 &inm->in6m_scq, inm, 1, 0, 0,
2224 (mli->mli_flags & MLIF_USEALLOW));
2225 KASSERT(retval != 0,
2226 ("%s: enqueue record = %d", __func__,
2229 inm->in6m_state = MLD_LEAVING_MEMBER;
2230 inm->in6m_sctimer = 1;
2231 V_state_change_timers_running6 = 1;
2237 case MLD_LAZY_MEMBER:
2238 case MLD_SLEEPING_MEMBER:
2239 case MLD_AWAKENING_MEMBER:
2240 /* Our reports are suppressed; do nothing. */
2246 CTR3(KTR_MLD, "%s: T1 -> T0 for %s/%s", __func__,
2247 ip6_sprintf(ip6tbuf, &inm->in6m_addr),
2248 if_name(inm->in6m_ifp));
2249 inm->in6m_st[1].iss_fmode = MCAST_UNDEFINED;
2250 CTR3(KTR_MLD, "%s: T1 now MCAST_UNDEFINED for %p/%s",
2251 __func__, &inm->in6m_addr, if_name(inm->in6m_ifp));
2256 * Enqueue an MLDv2 group record to the given output queue.
2258 * If is_state_change is zero, a current-state record is appended.
2259 * If is_state_change is non-zero, a state-change report is appended.
2261 * If is_group_query is non-zero, an mbuf packet chain is allocated.
2262 * If is_group_query is zero, and if there is a packet with free space
2263 * at the tail of the queue, it will be appended to providing there
2264 * is enough free space.
2265 * Otherwise a new mbuf packet chain is allocated.
2267 * If is_source_query is non-zero, each source is checked to see if
2268 * it was recorded for a Group-Source query, and will be omitted if
2269 * it is not both in-mode and recorded.
2271 * If use_block_allow is non-zero, state change reports for initial join
2272 * and final leave, on an inclusive mode group with a source list, will be
2273 * rewritten to use the ALLOW_NEW and BLOCK_OLD record types, respectively.
2275 * The function will attempt to allocate leading space in the packet
2276 * for the IPv6+ICMP headers to be prepended without fragmenting the chain.
2278 * If successful the size of all data appended to the queue is returned,
2279 * otherwise an error code less than zero is returned, or zero if
2280 * no record(s) were appended.
2283 mld_v2_enqueue_group_record(struct ifqueue *ifq, struct in6_multi *inm,
2284 const int is_state_change, const int is_group_query,
2285 const int is_source_query, const int use_block_allow)
2287 struct mldv2_record mr;
2288 struct mldv2_record *pmr;
2290 struct ip6_msource *ims, *nims;
2291 struct mbuf *m0, *m, *md;
2292 int error, is_filter_list_change;
2293 int minrec0len, m0srcs, msrcs, nbytes, off;
2294 int record_has_sources;
2299 char ip6tbuf[INET6_ADDRSTRLEN];
2302 IN6_MULTI_LOCK_ASSERT();
2305 ifp = inm->in6m_ifp;
2306 is_filter_list_change = 0;
2313 record_has_sources = 1;
2315 type = MLD_DO_NOTHING;
2316 mode = inm->in6m_st[1].iss_fmode;
2319 * If we did not transition out of ASM mode during t0->t1,
2320 * and there are no source nodes to process, we can skip
2321 * the generation of source records.
2323 if (inm->in6m_st[0].iss_asm > 0 && inm->in6m_st[1].iss_asm > 0 &&
2324 inm->in6m_nsrc == 0)
2325 record_has_sources = 0;
2327 if (is_state_change) {
2329 * Queue a state change record.
2330 * If the mode did not change, and there are non-ASM
2331 * listeners or source filters present,
2332 * we potentially need to issue two records for the group.
2333 * If there are ASM listeners, and there was no filter
2334 * mode transition of any kind, do nothing.
2336 * If we are transitioning to MCAST_UNDEFINED, we need
2337 * not send any sources. A transition to/from this state is
2338 * considered inclusive with some special treatment.
2340 * If we are rewriting initial joins/leaves to use
2341 * ALLOW/BLOCK, and the group's membership is inclusive,
2342 * we need to send sources in all cases.
2344 if (mode != inm->in6m_st[0].iss_fmode) {
2345 if (mode == MCAST_EXCLUDE) {
2346 CTR1(KTR_MLD, "%s: change to EXCLUDE",
2348 type = MLD_CHANGE_TO_EXCLUDE_MODE;
2350 CTR1(KTR_MLD, "%s: change to INCLUDE",
2352 if (use_block_allow) {
2355 * Here we're interested in state
2356 * edges either direction between
2357 * MCAST_UNDEFINED and MCAST_INCLUDE.
2358 * Perhaps we should just check
2359 * the group state, rather than
2362 if (mode == MCAST_UNDEFINED) {
2363 type = MLD_BLOCK_OLD_SOURCES;
2365 type = MLD_ALLOW_NEW_SOURCES;
2368 type = MLD_CHANGE_TO_INCLUDE_MODE;
2369 if (mode == MCAST_UNDEFINED)
2370 record_has_sources = 0;
2374 if (record_has_sources) {
2375 is_filter_list_change = 1;
2377 type = MLD_DO_NOTHING;
2382 * Queue a current state record.
2384 if (mode == MCAST_EXCLUDE) {
2385 type = MLD_MODE_IS_EXCLUDE;
2386 } else if (mode == MCAST_INCLUDE) {
2387 type = MLD_MODE_IS_INCLUDE;
2388 KASSERT(inm->in6m_st[1].iss_asm == 0,
2389 ("%s: inm %p is INCLUDE but ASM count is %d",
2390 __func__, inm, inm->in6m_st[1].iss_asm));
2395 * Generate the filter list changes using a separate function.
2397 if (is_filter_list_change)
2398 return (mld_v2_enqueue_filter_change(ifq, inm));
2400 if (type == MLD_DO_NOTHING) {
2401 CTR3(KTR_MLD, "%s: nothing to do for %s/%s",
2402 __func__, ip6_sprintf(ip6tbuf, &inm->in6m_addr),
2403 if_name(inm->in6m_ifp));
2408 * If any sources are present, we must be able to fit at least
2409 * one in the trailing space of the tail packet's mbuf,
2412 minrec0len = sizeof(struct mldv2_record);
2413 if (record_has_sources)
2414 minrec0len += sizeof(struct in6_addr);
2416 CTR4(KTR_MLD, "%s: queueing %s for %s/%s", __func__,
2417 mld_rec_type_to_str(type),
2418 ip6_sprintf(ip6tbuf, &inm->in6m_addr),
2419 if_name(inm->in6m_ifp));
2422 * Check if we have a packet in the tail of the queue for this
2423 * group into which the first group record for this group will fit.
2424 * Otherwise allocate a new packet.
2425 * Always allocate leading space for IP6+RA+ICMPV6+REPORT.
2426 * Note: Group records for G/GSR query responses MUST be sent
2427 * in their own packet.
2430 if (!is_group_query &&
2432 (m0->m_pkthdr.PH_vt.vt_nrecs + 1 <= MLD_V2_REPORT_MAXRECS) &&
2433 (m0->m_pkthdr.len + minrec0len) <
2434 (ifp->if_mtu - MLD_MTUSPACE)) {
2435 m0srcs = (ifp->if_mtu - m0->m_pkthdr.len -
2436 sizeof(struct mldv2_record)) /
2437 sizeof(struct in6_addr);
2439 CTR1(KTR_MLD, "%s: use existing packet", __func__);
2441 if (_IF_QFULL(ifq)) {
2442 CTR1(KTR_MLD, "%s: outbound queue full", __func__);
2446 m0srcs = (ifp->if_mtu - MLD_MTUSPACE -
2447 sizeof(struct mldv2_record)) / sizeof(struct in6_addr);
2448 if (!is_state_change && !is_group_query)
2449 m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
2451 m = m_gethdr(M_NOWAIT, MT_DATA);
2455 mld_save_context(m, ifp);
2457 CTR1(KTR_MLD, "%s: allocated first packet", __func__);
2461 * Append group record.
2462 * If we have sources, we don't know how many yet.
2467 mr.mr_addr = inm->in6m_addr;
2468 in6_clearscope(&mr.mr_addr);
2469 if (!m_append(m, sizeof(struct mldv2_record), (void *)&mr)) {
2472 CTR1(KTR_MLD, "%s: m_append() failed.", __func__);
2475 nbytes += sizeof(struct mldv2_record);
2478 * Append as many sources as will fit in the first packet.
2479 * If we are appending to a new packet, the chain allocation
2480 * may potentially use clusters; use m_getptr() in this case.
2481 * If we are appending to an existing packet, we need to obtain
2482 * a pointer to the group record after m_append(), in case a new
2483 * mbuf was allocated.
2485 * Only append sources which are in-mode at t1. If we are
2486 * transitioning to MCAST_UNDEFINED state on the group, and
2487 * use_block_allow is zero, do not include source entries.
2488 * Otherwise, we need to include this source in the report.
2490 * Only report recorded sources in our filter set when responding
2491 * to a group-source query.
2493 if (record_has_sources) {
2496 pmr = (struct mldv2_record *)(mtod(md, uint8_t *) +
2497 md->m_len - nbytes);
2499 md = m_getptr(m, 0, &off);
2500 pmr = (struct mldv2_record *)(mtod(md, uint8_t *) +
2504 RB_FOREACH_SAFE(ims, ip6_msource_tree, &inm->in6m_srcs,
2506 CTR2(KTR_MLD, "%s: visit node %s", __func__,
2507 ip6_sprintf(ip6tbuf, &ims->im6s_addr));
2508 now = im6s_get_mode(inm, ims, 1);
2509 CTR2(KTR_MLD, "%s: node is %d", __func__, now);
2510 if ((now != mode) ||
2512 (!use_block_allow && mode == MCAST_UNDEFINED))) {
2513 CTR1(KTR_MLD, "%s: skip node", __func__);
2516 if (is_source_query && ims->im6s_stp == 0) {
2517 CTR1(KTR_MLD, "%s: skip unrecorded node",
2521 CTR1(KTR_MLD, "%s: append node", __func__);
2522 if (!m_append(m, sizeof(struct in6_addr),
2523 (void *)&ims->im6s_addr)) {
2526 CTR1(KTR_MLD, "%s: m_append() failed.",
2530 nbytes += sizeof(struct in6_addr);
2532 if (msrcs == m0srcs)
2535 CTR2(KTR_MLD, "%s: msrcs is %d this packet", __func__,
2537 pmr->mr_numsrc = htons(msrcs);
2538 nbytes += (msrcs * sizeof(struct in6_addr));
2541 if (is_source_query && msrcs == 0) {
2542 CTR1(KTR_MLD, "%s: no recorded sources to report", __func__);
2549 * We are good to go with first packet.
2552 CTR1(KTR_MLD, "%s: enqueueing first packet", __func__);
2553 m->m_pkthdr.PH_vt.vt_nrecs = 1;
2554 _IF_ENQUEUE(ifq, m);
2556 m->m_pkthdr.PH_vt.vt_nrecs++;
2559 * No further work needed if no source list in packet(s).
2561 if (!record_has_sources)
2565 * Whilst sources remain to be announced, we need to allocate
2566 * a new packet and fill out as many sources as will fit.
2567 * Always try for a cluster first.
2569 while (nims != NULL) {
2570 if (_IF_QFULL(ifq)) {
2571 CTR1(KTR_MLD, "%s: outbound queue full", __func__);
2574 m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
2576 m = m_gethdr(M_NOWAIT, MT_DATA);
2579 mld_save_context(m, ifp);
2580 md = m_getptr(m, 0, &off);
2581 pmr = (struct mldv2_record *)(mtod(md, uint8_t *) + off);
2582 CTR1(KTR_MLD, "%s: allocated next packet", __func__);
2584 if (!m_append(m, sizeof(struct mldv2_record), (void *)&mr)) {
2587 CTR1(KTR_MLD, "%s: m_append() failed.", __func__);
2590 m->m_pkthdr.PH_vt.vt_nrecs = 1;
2591 nbytes += sizeof(struct mldv2_record);
2593 m0srcs = (ifp->if_mtu - MLD_MTUSPACE -
2594 sizeof(struct mldv2_record)) / sizeof(struct in6_addr);
2597 RB_FOREACH_FROM(ims, ip6_msource_tree, nims) {
2598 CTR2(KTR_MLD, "%s: visit node %s",
2599 __func__, ip6_sprintf(ip6tbuf, &ims->im6s_addr));
2600 now = im6s_get_mode(inm, ims, 1);
2601 if ((now != mode) ||
2603 (!use_block_allow && mode == MCAST_UNDEFINED))) {
2604 CTR1(KTR_MLD, "%s: skip node", __func__);
2607 if (is_source_query && ims->im6s_stp == 0) {
2608 CTR1(KTR_MLD, "%s: skip unrecorded node",
2612 CTR1(KTR_MLD, "%s: append node", __func__);
2613 if (!m_append(m, sizeof(struct in6_addr),
2614 (void *)&ims->im6s_addr)) {
2617 CTR1(KTR_MLD, "%s: m_append() failed.",
2622 if (msrcs == m0srcs)
2625 pmr->mr_numsrc = htons(msrcs);
2626 nbytes += (msrcs * sizeof(struct in6_addr));
2628 CTR1(KTR_MLD, "%s: enqueueing next packet", __func__);
2629 _IF_ENQUEUE(ifq, m);
2636 * Type used to mark record pass completion.
2637 * We exploit the fact we can cast to this easily from the
2638 * current filter modes on each ip_msource node.
2641 REC_NONE = 0x00, /* MCAST_UNDEFINED */
2642 REC_ALLOW = 0x01, /* MCAST_INCLUDE */
2643 REC_BLOCK = 0x02, /* MCAST_EXCLUDE */
2644 REC_FULL = REC_ALLOW | REC_BLOCK
2648 * Enqueue an MLDv2 filter list change to the given output queue.
2650 * Source list filter state is held in an RB-tree. When the filter list
2651 * for a group is changed without changing its mode, we need to compute
2652 * the deltas between T0 and T1 for each source in the filter set,
2653 * and enqueue the appropriate ALLOW_NEW/BLOCK_OLD records.
2655 * As we may potentially queue two record types, and the entire R-B tree
2656 * needs to be walked at once, we break this out into its own function
2657 * so we can generate a tightly packed queue of packets.
2659 * XXX This could be written to only use one tree walk, although that makes
2660 * serializing into the mbuf chains a bit harder. For now we do two walks
2661 * which makes things easier on us, and it may or may not be harder on
2664 * If successful the size of all data appended to the queue is returned,
2665 * otherwise an error code less than zero is returned, or zero if
2666 * no record(s) were appended.
2669 mld_v2_enqueue_filter_change(struct ifqueue *ifq, struct in6_multi *inm)
2671 static const int MINRECLEN =
2672 sizeof(struct mldv2_record) + sizeof(struct in6_addr);
2674 struct mldv2_record mr;
2675 struct mldv2_record *pmr;
2676 struct ip6_msource *ims, *nims;
2677 struct mbuf *m, *m0, *md;
2678 int m0srcs, nbytes, npbytes, off, rsrcs, schanged;
2680 uint8_t mode, now, then;
2681 rectype_t crt, drt, nrt;
2683 char ip6tbuf[INET6_ADDRSTRLEN];
2686 IN6_MULTI_LOCK_ASSERT();
2688 if (inm->in6m_nsrc == 0 ||
2689 (inm->in6m_st[0].iss_asm > 0 && inm->in6m_st[1].iss_asm > 0))
2692 ifp = inm->in6m_ifp; /* interface */
2693 mode = inm->in6m_st[1].iss_fmode; /* filter mode at t1 */
2694 crt = REC_NONE; /* current group record type */
2695 drt = REC_NONE; /* mask of completed group record types */
2696 nrt = REC_NONE; /* record type for current node */
2697 m0srcs = 0; /* # source which will fit in current mbuf chain */
2698 npbytes = 0; /* # of bytes appended this packet */
2699 nbytes = 0; /* # of bytes appended to group's state-change queue */
2700 rsrcs = 0; /* # sources encoded in current record */
2701 schanged = 0; /* # nodes encoded in overall filter change */
2702 nallow = 0; /* # of source entries in ALLOW_NEW */
2703 nblock = 0; /* # of source entries in BLOCK_OLD */
2704 nims = NULL; /* next tree node pointer */
2707 * For each possible filter record mode.
2708 * The first kind of source we encounter tells us which
2709 * is the first kind of record we start appending.
2710 * If a node transitioned to UNDEFINED at t1, its mode is treated
2711 * as the inverse of the group's filter mode.
2713 while (drt != REC_FULL) {
2717 (m0->m_pkthdr.PH_vt.vt_nrecs + 1 <=
2718 MLD_V2_REPORT_MAXRECS) &&
2719 (m0->m_pkthdr.len + MINRECLEN) <
2720 (ifp->if_mtu - MLD_MTUSPACE)) {
2722 m0srcs = (ifp->if_mtu - m0->m_pkthdr.len -
2723 sizeof(struct mldv2_record)) /
2724 sizeof(struct in6_addr);
2726 "%s: use previous packet", __func__);
2728 m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
2730 m = m_gethdr(M_NOWAIT, MT_DATA);
2733 "%s: m_get*() failed", __func__);
2736 m->m_pkthdr.PH_vt.vt_nrecs = 0;
2737 mld_save_context(m, ifp);
2738 m0srcs = (ifp->if_mtu - MLD_MTUSPACE -
2739 sizeof(struct mldv2_record)) /
2740 sizeof(struct in6_addr);
2743 "%s: allocated new packet", __func__);
2746 * Append the MLD group record header to the
2747 * current packet's data area.
2748 * Recalculate pointer to free space for next
2749 * group record, in case m_append() allocated
2750 * a new mbuf or cluster.
2752 memset(&mr, 0, sizeof(mr));
2753 mr.mr_addr = inm->in6m_addr;
2754 in6_clearscope(&mr.mr_addr);
2755 if (!m_append(m, sizeof(mr), (void *)&mr)) {
2759 "%s: m_append() failed", __func__);
2762 npbytes += sizeof(struct mldv2_record);
2764 /* new packet; offset in chain */
2765 md = m_getptr(m, npbytes -
2766 sizeof(struct mldv2_record), &off);
2767 pmr = (struct mldv2_record *)(mtod(md,
2770 /* current packet; offset from last append */
2772 pmr = (struct mldv2_record *)(mtod(md,
2773 uint8_t *) + md->m_len -
2774 sizeof(struct mldv2_record));
2777 * Begin walking the tree for this record type
2778 * pass, or continue from where we left off
2779 * previously if we had to allocate a new packet.
2780 * Only report deltas in-mode at t1.
2781 * We need not report included sources as allowed
2782 * if we are in inclusive mode on the group,
2783 * however the converse is not true.
2787 nims = RB_MIN(ip6_msource_tree,
2790 RB_FOREACH_FROM(ims, ip6_msource_tree, nims) {
2791 CTR2(KTR_MLD, "%s: visit node %s", __func__,
2792 ip6_sprintf(ip6tbuf, &ims->im6s_addr));
2793 now = im6s_get_mode(inm, ims, 1);
2794 then = im6s_get_mode(inm, ims, 0);
2795 CTR3(KTR_MLD, "%s: mode: t0 %d, t1 %d",
2796 __func__, then, now);
2799 "%s: skip unchanged", __func__);
2802 if (mode == MCAST_EXCLUDE &&
2803 now == MCAST_INCLUDE) {
2805 "%s: skip IN src on EX group",
2809 nrt = (rectype_t)now;
2810 if (nrt == REC_NONE)
2811 nrt = (rectype_t)(~mode & REC_FULL);
2812 if (schanged++ == 0) {
2814 } else if (crt != nrt)
2816 if (!m_append(m, sizeof(struct in6_addr),
2817 (void *)&ims->im6s_addr)) {
2821 "%s: m_append() failed", __func__);
2824 nallow += !!(crt == REC_ALLOW);
2825 nblock += !!(crt == REC_BLOCK);
2826 if (++rsrcs == m0srcs)
2830 * If we did not append any tree nodes on this
2831 * pass, back out of allocations.
2834 npbytes -= sizeof(struct mldv2_record);
2837 "%s: m_free(m)", __func__);
2841 "%s: m_adj(m, -mr)", __func__);
2842 m_adj(m, -((int)sizeof(
2843 struct mldv2_record)));
2847 npbytes += (rsrcs * sizeof(struct in6_addr));
2848 if (crt == REC_ALLOW)
2849 pmr->mr_type = MLD_ALLOW_NEW_SOURCES;
2850 else if (crt == REC_BLOCK)
2851 pmr->mr_type = MLD_BLOCK_OLD_SOURCES;
2852 pmr->mr_numsrc = htons(rsrcs);
2854 * Count the new group record, and enqueue this
2855 * packet if it wasn't already queued.
2857 m->m_pkthdr.PH_vt.vt_nrecs++;
2859 _IF_ENQUEUE(ifq, m);
2861 } while (nims != NULL);
2863 crt = (~crt & REC_FULL);
2866 CTR3(KTR_MLD, "%s: queued %d ALLOW_NEW, %d BLOCK_OLD", __func__,
2873 mld_v2_merge_state_changes(struct in6_multi *inm, struct ifqueue *ifscq)
2876 struct mbuf *m; /* pending state-change */
2877 struct mbuf *m0; /* copy of pending state-change */
2878 struct mbuf *mt; /* last state-change in packet */
2879 int docopy, domerge;
2886 IN6_MULTI_LOCK_ASSERT();
2890 * If there are further pending retransmissions, make a writable
2891 * copy of each queued state-change message before merging.
2893 if (inm->in6m_scrv > 0)
2896 gq = &inm->in6m_scq;
2898 if (gq->ifq_head == NULL) {
2899 CTR2(KTR_MLD, "%s: WARNING: queue for inm %p is empty",
2907 * Only merge the report into the current packet if
2908 * there is sufficient space to do so; an MLDv2 report
2909 * packet may only contain 65,535 group records.
2910 * Always use a simple mbuf chain concatentation to do this,
2911 * as large state changes for single groups may have
2912 * allocated clusters.
2915 mt = ifscq->ifq_tail;
2917 recslen = m_length(m, NULL);
2919 if ((mt->m_pkthdr.PH_vt.vt_nrecs +
2920 m->m_pkthdr.PH_vt.vt_nrecs <=
2921 MLD_V2_REPORT_MAXRECS) &&
2922 (mt->m_pkthdr.len + recslen <=
2923 (inm->in6m_ifp->if_mtu - MLD_MTUSPACE)))
2927 if (!domerge && _IF_QFULL(gq)) {
2929 "%s: outbound queue full, skipping whole packet %p",
2939 CTR2(KTR_MLD, "%s: dequeueing %p", __func__, m);
2940 _IF_DEQUEUE(gq, m0);
2943 CTR2(KTR_MLD, "%s: copying %p", __func__, m);
2944 m0 = m_dup(m, M_NOWAIT);
2947 m0->m_nextpkt = NULL;
2952 CTR3(KTR_MLD, "%s: queueing %p to ifscq %p)",
2953 __func__, m0, ifscq);
2954 _IF_ENQUEUE(ifscq, m0);
2956 struct mbuf *mtl; /* last mbuf of packet mt */
2958 CTR3(KTR_MLD, "%s: merging %p with ifscq tail %p)",
2962 m0->m_flags &= ~M_PKTHDR;
2963 mt->m_pkthdr.len += recslen;
2964 mt->m_pkthdr.PH_vt.vt_nrecs +=
2965 m0->m_pkthdr.PH_vt.vt_nrecs;
2975 * Respond to a pending MLDv2 General Query.
2978 mld_v2_dispatch_general_query(struct mld_ifinfo *mli)
2980 struct ifmultiaddr *ifma;
2982 struct in6_multi *inm;
2985 IN6_MULTI_LOCK_ASSERT();
2988 KASSERT(mli->mli_version == MLD_VERSION_2,
2989 ("%s: called when version %d", __func__, mli->mli_version));
2994 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
2995 if (ifma->ifma_addr->sa_family != AF_INET6 ||
2996 ifma->ifma_protospec == NULL)
2999 inm = (struct in6_multi *)ifma->ifma_protospec;
3000 KASSERT(ifp == inm->in6m_ifp,
3001 ("%s: inconsistent ifp", __func__));
3003 switch (inm->in6m_state) {
3004 case MLD_NOT_MEMBER:
3005 case MLD_SILENT_MEMBER:
3007 case MLD_REPORTING_MEMBER:
3008 case MLD_IDLE_MEMBER:
3009 case MLD_LAZY_MEMBER:
3010 case MLD_SLEEPING_MEMBER:
3011 case MLD_AWAKENING_MEMBER:
3012 inm->in6m_state = MLD_REPORTING_MEMBER;
3013 retval = mld_v2_enqueue_group_record(&mli->mli_gq,
3015 CTR2(KTR_MLD, "%s: enqueue record = %d",
3018 case MLD_G_QUERY_PENDING_MEMBER:
3019 case MLD_SG_QUERY_PENDING_MEMBER:
3020 case MLD_LEAVING_MEMBER:
3024 IF_ADDR_RUNLOCK(ifp);
3026 mld_dispatch_queue(&mli->mli_gq, MLD_MAX_RESPONSE_BURST);
3029 * Slew transmission of bursts over 500ms intervals.
3031 if (mli->mli_gq.ifq_head != NULL) {
3032 mli->mli_v2_timer = 1 + MLD_RANDOM_DELAY(
3033 MLD_RESPONSE_BURST_INTERVAL);
3034 V_interface_timers_running6 = 1;
3039 * Transmit the next pending message in the output queue.
3041 * VIMAGE: Needs to store/restore vnet pointer on a per-mbuf-chain basis.
3042 * MRT: Nothing needs to be done, as MLD traffic is always local to
3043 * a link and uses a link-scope multicast address.
3046 mld_dispatch_packet(struct mbuf *m)
3048 struct ip6_moptions im6o;
3053 struct ip6_hdr *ip6;
3054 struct mld_hdr *mld;
3060 CTR2(KTR_MLD, "%s: transmit %p", __func__, m);
3063 * Set VNET image pointer from enqueued mbuf chain
3064 * before doing anything else. Whilst we use interface
3065 * indexes to guard against interface detach, they are
3066 * unique to each VIMAGE and must be retrieved.
3068 ifindex = mld_restore_context(m);
3071 * Check if the ifnet still exists. This limits the scope of
3072 * any race in the absence of a global ifp lock for low cost
3073 * (an array lookup).
3075 ifp = ifnet_byindex(ifindex);
3077 CTR3(KTR_MLD, "%s: dropped %p as ifindex %u went away.",
3078 __func__, m, ifindex);
3080 IP6STAT_INC(ip6s_noroute);
3084 im6o.im6o_multicast_hlim = 1;
3085 im6o.im6o_multicast_loop = (V_ip6_mrouter != NULL);
3086 im6o.im6o_multicast_ifp = ifp;
3088 if (m->m_flags & M_MLDV1) {
3091 m0 = mld_v2_encap_report(ifp, m);
3093 CTR2(KTR_MLD, "%s: dropped %p", __func__, m);
3094 IP6STAT_INC(ip6s_odropped);
3099 mld_scrub_context(m0);
3101 m0->m_pkthdr.rcvif = V_loif;
3103 ip6 = mtod(m0, struct ip6_hdr *);
3105 (void)in6_setscope(&ip6->ip6_dst, ifp, NULL); /* XXX LOR */
3108 * XXX XXX Break some KPI rules to prevent an LOR which would
3109 * occur if we called in6_setscope() at transmission.
3110 * See comments at top of file.
3112 MLD_EMBEDSCOPE(&ip6->ip6_dst, ifp->if_index);
3116 * Retrieve the ICMPv6 type before handoff to ip6_output(),
3117 * so we can bump the stats.
3119 md = m_getptr(m0, sizeof(struct ip6_hdr), &off);
3120 mld = (struct mld_hdr *)(mtod(md, uint8_t *) + off);
3121 type = mld->mld_type;
3123 error = ip6_output(m0, &mld_po, NULL, IPV6_UNSPECSRC, &im6o,
3126 CTR3(KTR_MLD, "%s: ip6_output(%p) = %d", __func__, m0, error);
3129 ICMP6STAT_INC(icp6s_outhist[type]);
3131 icmp6_ifstat_inc(oifp, ifs6_out_msg);
3133 case MLD_LISTENER_REPORT:
3134 case MLDV2_LISTENER_REPORT:
3135 icmp6_ifstat_inc(oifp, ifs6_out_mldreport);
3137 case MLD_LISTENER_DONE:
3138 icmp6_ifstat_inc(oifp, ifs6_out_mlddone);
3147 * Encapsulate an MLDv2 report.
3149 * KAME IPv6 requires that hop-by-hop options be passed separately,
3150 * and that the IPv6 header be prepended in a separate mbuf.
3152 * Returns a pointer to the new mbuf chain head, or NULL if the
3153 * allocation failed.
3155 static struct mbuf *
3156 mld_v2_encap_report(struct ifnet *ifp, struct mbuf *m)
3159 struct mldv2_report *mld;
3160 struct ip6_hdr *ip6;
3161 struct in6_ifaddr *ia;
3164 KASSERT(ifp != NULL, ("%s: null ifp", __func__));
3165 KASSERT((m->m_flags & M_PKTHDR),
3166 ("%s: mbuf chain %p is !M_PKTHDR", __func__, m));
3169 * RFC3590: OK to send as :: or tentative during DAD.
3171 ia = in6ifa_ifpforlinklocal(ifp, IN6_IFF_NOTREADY|IN6_IFF_ANYCAST);
3173 CTR1(KTR_MLD, "%s: warning: ia is NULL", __func__);
3175 mh = m_gethdr(M_NOWAIT, MT_DATA);
3178 ifa_free(&ia->ia_ifa);
3182 MH_ALIGN(mh, sizeof(struct ip6_hdr) + sizeof(struct mldv2_report));
3184 mldreclen = m_length(m, NULL);
3185 CTR2(KTR_MLD, "%s: mldreclen is %d", __func__, mldreclen);
3187 mh->m_len = sizeof(struct ip6_hdr) + sizeof(struct mldv2_report);
3188 mh->m_pkthdr.len = sizeof(struct ip6_hdr) +
3189 sizeof(struct mldv2_report) + mldreclen;
3191 ip6 = mtod(mh, struct ip6_hdr *);
3193 ip6->ip6_vfc &= ~IPV6_VERSION_MASK;
3194 ip6->ip6_vfc |= IPV6_VERSION;
3195 ip6->ip6_nxt = IPPROTO_ICMPV6;
3196 ip6->ip6_src = ia ? ia->ia_addr.sin6_addr : in6addr_any;
3198 ifa_free(&ia->ia_ifa);
3199 ip6->ip6_dst = in6addr_linklocal_allv2routers;
3200 /* scope ID will be set in netisr */
3202 mld = (struct mldv2_report *)(ip6 + 1);
3203 mld->mld_type = MLDV2_LISTENER_REPORT;
3206 mld->mld_v2_reserved = 0;
3207 mld->mld_v2_numrecs = htons(m->m_pkthdr.PH_vt.vt_nrecs);
3208 m->m_pkthdr.PH_vt.vt_nrecs = 0;
3211 mld->mld_cksum = in6_cksum(mh, IPPROTO_ICMPV6,
3212 sizeof(struct ip6_hdr), sizeof(struct mldv2_report) + mldreclen);
3218 mld_rec_type_to_str(const int type)
3222 case MLD_CHANGE_TO_EXCLUDE_MODE:
3225 case MLD_CHANGE_TO_INCLUDE_MODE:
3228 case MLD_MODE_IS_EXCLUDE:
3231 case MLD_MODE_IS_INCLUDE:
3234 case MLD_ALLOW_NEW_SOURCES:
3237 case MLD_BLOCK_OLD_SOURCES:
3248 mld_init(void *unused __unused)
3251 CTR1(KTR_MLD, "%s: initializing", __func__);
3254 ip6_initpktopts(&mld_po);
3255 mld_po.ip6po_hlim = 1;
3256 mld_po.ip6po_hbh = &mld_ra.hbh;
3257 mld_po.ip6po_prefer_tempaddr = IP6PO_TEMPADDR_NOTPREFER;
3258 mld_po.ip6po_flags = IP6PO_DONTFRAG;
3260 SYSINIT(mld_init, SI_SUB_PSEUDO, SI_ORDER_MIDDLE, mld_init, NULL);
3263 mld_uninit(void *unused __unused)
3266 CTR1(KTR_MLD, "%s: tearing down", __func__);
3269 SYSUNINIT(mld_uninit, SI_SUB_PSEUDO, SI_ORDER_MIDDLE, mld_uninit, NULL);
3272 vnet_mld_init(const void *unused __unused)
3275 CTR1(KTR_MLD, "%s: initializing", __func__);
3277 LIST_INIT(&V_mli_head);
3279 VNET_SYSINIT(vnet_mld_init, SI_SUB_PSEUDO, SI_ORDER_ANY, vnet_mld_init,
3283 vnet_mld_uninit(const void *unused __unused)
3286 CTR1(KTR_MLD, "%s: tearing down", __func__);
3288 KASSERT(LIST_EMPTY(&V_mli_head),
3289 ("%s: mli list not empty; ifnets not detached?", __func__));
3291 VNET_SYSUNINIT(vnet_mld_uninit, SI_SUB_PSEUDO, SI_ORDER_ANY, vnet_mld_uninit,
3295 mld_modevent(module_t mod, int type, void *unused __unused)
3303 return (EOPNOTSUPP);
3308 static moduledata_t mld_mod = {
3313 DECLARE_MODULE(mld, mld_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);