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_ifsoftc *
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 mbufq *, int);
110 static void mld_final_leave(struct in6_multi *, struct mld_ifsoftc *);
111 static void mld_fasttimo_vnet(void);
112 static int mld_handle_state_change(struct in6_multi *,
113 struct mld_ifsoftc *);
114 static int mld_initial_join(struct in6_multi *, struct mld_ifsoftc *,
117 static char * mld_rec_type_to_str(const int);
119 static void mld_set_version(struct mld_ifsoftc *, 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_ifsoftc *,
127 static void mld_v1_process_querier_timers(struct mld_ifsoftc *);
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_ifsoftc *);
131 static void mld_v2_dispatch_general_query(struct mld_ifsoftc *);
133 mld_v2_encap_report(struct ifnet *, struct mbuf *);
134 static int mld_v2_enqueue_filter_change(struct mbufq *,
136 static int mld_v2_enqueue_group_record(struct mbufq *,
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 *, struct mldv2_query *, 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_ifsoftc *,
144 struct mbufq *, struct mbufq *,
145 struct in6_multi *, const int);
146 static int mld_v2_process_group_query(struct in6_multi *,
147 struct mld_ifsoftc *mli, int, struct mbuf *,
148 struct mldv2_query *, const int);
149 static int sysctl_mld_gsr(SYSCTL_HANDLER_ARGS);
150 static int sysctl_mld_ifinfo(SYSCTL_HANDLER_ARGS);
153 * Normative references: RFC 2710, RFC 3590, RFC 3810.
156 * * The MLD subsystem lock ends up being system-wide for the moment,
157 * but could be per-VIMAGE later on.
158 * * The permitted lock order is: IN6_MULTI_LOCK, MLD_LOCK, IF_ADDR_LOCK.
159 * Any may be taken independently; if any are held at the same
160 * time, the above lock order must be followed.
161 * * IN6_MULTI_LOCK covers in_multi.
162 * * MLD_LOCK covers per-link state and any global variables in this file.
163 * * IF_ADDR_LOCK covers if_multiaddrs, which is used for a variety of
164 * per-link state iterators.
167 * A special case for IPv6 is the in6_setscope() routine. ip6_output()
168 * will not accept an ifp; it wants an embedded scope ID, unlike
169 * ip_output(), which happily takes the ifp given to it. The embedded
170 * scope ID is only used by MLD to select the outgoing interface.
172 * During interface attach and detach, MLD will take MLD_LOCK *after*
173 * the IF_AFDATA_LOCK.
174 * As in6_setscope() takes IF_AFDATA_LOCK then SCOPE_LOCK, we can't call
175 * it with MLD_LOCK held without triggering an LOR. A netisr with indirect
176 * dispatch could work around this, but we'd rather not do that, as it
177 * can introduce other races.
179 * As such, we exploit the fact that the scope ID is just the interface
180 * index, and embed it in the IPv6 destination address accordingly.
181 * This is potentially NOT VALID for MLDv1 reports, as they
182 * are always sent to the multicast group itself; as MLDv2
183 * reports are always sent to ff02::16, this is not an issue
184 * when MLDv2 is in use.
186 * This does not however eliminate the LOR when ip6_output() itself
187 * calls in6_setscope() internally whilst MLD_LOCK is held. This will
188 * trigger a LOR warning in WITNESS when the ifnet is detached.
190 * The right answer is probably to make IF_AFDATA_LOCK an rwlock, given
191 * how it's used across the network stack. Here we're simply exploiting
192 * the fact that MLD runs at a similar layer in the stack to scope6.c.
195 * * Each in6_multi corresponds to an ifp, and each ifp corresponds
196 * to a vnet in ifp->if_vnet.
198 static struct mtx mld_mtx;
199 static MALLOC_DEFINE(M_MLD, "mld", "mld state");
201 #define MLD_EMBEDSCOPE(pin6, zoneid) \
202 if (IN6_IS_SCOPE_LINKLOCAL(pin6) || \
203 IN6_IS_ADDR_MC_INTFACELOCAL(pin6)) \
204 (pin6)->s6_addr16[1] = htons((zoneid) & 0xFFFF) \
207 * VIMAGE-wide globals.
209 static VNET_DEFINE(struct timeval, mld_gsrdelay) = {10, 0};
210 static VNET_DEFINE(LIST_HEAD(, mld_ifsoftc), mli_head);
211 static VNET_DEFINE(int, interface_timers_running6);
212 static VNET_DEFINE(int, state_change_timers_running6);
213 static VNET_DEFINE(int, current_state_timers_running6);
215 #define V_mld_gsrdelay VNET(mld_gsrdelay)
216 #define V_mli_head VNET(mli_head)
217 #define V_interface_timers_running6 VNET(interface_timers_running6)
218 #define V_state_change_timers_running6 VNET(state_change_timers_running6)
219 #define V_current_state_timers_running6 VNET(current_state_timers_running6)
221 SYSCTL_DECL(_net_inet6); /* Note: Not in any common header. */
223 SYSCTL_NODE(_net_inet6, OID_AUTO, mld, CTLFLAG_RW, 0,
224 "IPv6 Multicast Listener Discovery");
227 * Virtualized sysctls.
229 SYSCTL_PROC(_net_inet6_mld, OID_AUTO, gsrdelay,
230 CTLFLAG_VNET | CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
231 &VNET_NAME(mld_gsrdelay.tv_sec), 0, sysctl_mld_gsr, "I",
232 "Rate limit for MLDv2 Group-and-Source queries in seconds");
235 * Non-virtualized sysctls.
237 static SYSCTL_NODE(_net_inet6_mld, OID_AUTO, ifinfo,
238 CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_mld_ifinfo,
239 "Per-interface MLDv2 state");
241 static int mld_v1enable = 1;
242 SYSCTL_INT(_net_inet6_mld, OID_AUTO, v1enable, CTLFLAG_RWTUN,
243 &mld_v1enable, 0, "Enable fallback to MLDv1");
245 static int mld_use_allow = 1;
246 SYSCTL_INT(_net_inet6_mld, OID_AUTO, use_allow, CTLFLAG_RWTUN,
247 &mld_use_allow, 0, "Use ALLOW/BLOCK for RFC 4604 SSM joins/leaves");
250 * Packed Router Alert option structure declaration.
255 struct ip6_opt_router ra;
259 * Router Alert hop-by-hop option header.
261 static struct mld_raopt mld_ra = {
263 .pad = { .ip6o_type = IP6OPT_PADN, 0 },
265 .ip6or_type = IP6OPT_ROUTER_ALERT,
266 .ip6or_len = IP6OPT_RTALERT_LEN - 2,
267 .ip6or_value[0] = ((IP6OPT_RTALERT_MLD >> 8) & 0xFF),
268 .ip6or_value[1] = (IP6OPT_RTALERT_MLD & 0xFF)
271 static struct ip6_pktopts mld_po;
274 mld_save_context(struct mbuf *m, struct ifnet *ifp)
278 m->m_pkthdr.PH_loc.ptr = ifp->if_vnet;
280 m->m_pkthdr.flowid = ifp->if_index;
284 mld_scrub_context(struct mbuf *m)
287 m->m_pkthdr.PH_loc.ptr = NULL;
288 m->m_pkthdr.flowid = 0;
292 * Restore context from a queued output chain.
293 * Return saved ifindex.
295 * VIMAGE: The assertion is there to make sure that we
296 * actually called CURVNET_SET() with what's in the mbuf chain.
298 static __inline uint32_t
299 mld_restore_context(struct mbuf *m)
302 #if defined(VIMAGE) && defined(INVARIANTS)
303 KASSERT(curvnet == m->m_pkthdr.PH_loc.ptr,
304 ("%s: called when curvnet was not restored: cuvnet %p m ptr %p",
305 __func__, curvnet, m->m_pkthdr.PH_loc.ptr));
307 return (m->m_pkthdr.flowid);
311 * Retrieve or set threshold between group-source queries in seconds.
313 * VIMAGE: Assume curvnet set by caller.
314 * SMPng: NOTE: Serialized by MLD lock.
317 sysctl_mld_gsr(SYSCTL_HANDLER_ARGS)
322 error = sysctl_wire_old_buffer(req, sizeof(int));
328 i = V_mld_gsrdelay.tv_sec;
330 error = sysctl_handle_int(oidp, &i, 0, req);
331 if (error || !req->newptr)
334 if (i < -1 || i >= 60) {
339 CTR2(KTR_MLD, "change mld_gsrdelay from %d to %d",
340 V_mld_gsrdelay.tv_sec, i);
341 V_mld_gsrdelay.tv_sec = i;
349 * Expose struct mld_ifsoftc to userland, keyed by ifindex.
350 * For use by ifmcstat(8).
352 * SMPng: NOTE: Does an unlocked ifindex space read.
353 * VIMAGE: Assume curvnet set by caller. The node handler itself
354 * is not directly virtualized.
357 sysctl_mld_ifinfo(SYSCTL_HANDLER_ARGS)
363 struct mld_ifsoftc *mli;
368 if (req->newptr != NULL)
374 error = sysctl_wire_old_buffer(req, sizeof(struct mld_ifinfo));
381 if (name[0] <= 0 || name[0] > V_if_index) {
388 ifp = ifnet_byindex(name[0]);
392 LIST_FOREACH(mli, &V_mli_head, mli_link) {
393 if (ifp == mli->mli_ifp) {
394 struct mld_ifinfo info;
396 info.mli_version = mli->mli_version;
397 info.mli_v1_timer = mli->mli_v1_timer;
398 info.mli_v2_timer = mli->mli_v2_timer;
399 info.mli_flags = mli->mli_flags;
400 info.mli_rv = mli->mli_rv;
401 info.mli_qi = mli->mli_qi;
402 info.mli_qri = mli->mli_qri;
403 info.mli_uri = mli->mli_uri;
404 error = SYSCTL_OUT(req, &info, sizeof(info));
416 * Dispatch an entire queue of pending packet chains.
417 * VIMAGE: Assumes the vnet pointer has been set.
420 mld_dispatch_queue(struct mbufq *mq, int limit)
424 while ((m = mbufq_dequeue(mq)) != NULL) {
425 CTR3(KTR_MLD, "%s: dispatch %p from %p", __func__, mq, m);
426 mld_dispatch_packet(m);
433 * Filter outgoing MLD report state by group.
435 * Reports are ALWAYS suppressed for ALL-HOSTS (ff02::1)
436 * and node-local addresses. However, kernel and socket consumers
437 * always embed the KAME scope ID in the address provided, so strip it
438 * when performing comparison.
439 * Note: This is not the same as the *multicast* scope.
441 * Return zero if the given group is one for which MLD reports
442 * should be suppressed, or non-zero if reports should be issued.
445 mld_is_addr_reported(const struct in6_addr *addr)
448 KASSERT(IN6_IS_ADDR_MULTICAST(addr), ("%s: not multicast", __func__));
450 if (IPV6_ADDR_MC_SCOPE(addr) == IPV6_ADDR_SCOPE_NODELOCAL)
453 if (IPV6_ADDR_MC_SCOPE(addr) == IPV6_ADDR_SCOPE_LINKLOCAL) {
454 struct in6_addr tmp = *addr;
455 in6_clearscope(&tmp);
456 if (IN6_ARE_ADDR_EQUAL(&tmp, &in6addr_linklocal_allnodes))
464 * Attach MLD when PF_INET6 is attached to an interface.
466 * SMPng: Normally called with IF_AFDATA_LOCK held.
469 mld_domifattach(struct ifnet *ifp)
471 struct mld_ifsoftc *mli;
473 CTR3(KTR_MLD, "%s: called for ifp %p(%s)",
474 __func__, ifp, if_name(ifp));
478 mli = mli_alloc_locked(ifp);
479 if (!(ifp->if_flags & IFF_MULTICAST))
480 mli->mli_flags |= MLIF_SILENT;
482 mli->mli_flags |= MLIF_USEALLOW;
490 * VIMAGE: assume curvnet set by caller.
492 static struct mld_ifsoftc *
493 mli_alloc_locked(/*const*/ struct ifnet *ifp)
495 struct mld_ifsoftc *mli;
499 mli = malloc(sizeof(struct mld_ifsoftc), M_MLD, M_NOWAIT|M_ZERO);
504 mli->mli_version = MLD_VERSION_2;
506 mli->mli_rv = MLD_RV_INIT;
507 mli->mli_qi = MLD_QI_INIT;
508 mli->mli_qri = MLD_QRI_INIT;
509 mli->mli_uri = MLD_URI_INIT;
510 SLIST_INIT(&mli->mli_relinmhead);
511 mbufq_init(&mli->mli_gq, MLD_MAX_RESPONSE_PACKETS);
513 LIST_INSERT_HEAD(&V_mli_head, mli, mli_link);
515 CTR2(KTR_MLD, "allocate mld_ifsoftc for ifp %p(%s)",
525 * NOTE: Some finalization tasks need to run before the protocol domain
526 * is detached, but also before the link layer does its cleanup.
527 * Run before link-layer cleanup; cleanup groups, but do not free MLD state.
529 * SMPng: Caller must hold IN6_MULTI_LOCK().
530 * Must take IF_ADDR_LOCK() to cover if_multiaddrs iterator.
531 * XXX This routine is also bitten by unlocked ifma_protospec access.
534 mld_ifdetach(struct ifnet *ifp)
536 struct mld_ifsoftc *mli;
537 struct ifmultiaddr *ifma;
538 struct in6_multi *inm, *tinm;
540 CTR3(KTR_MLD, "%s: called for ifp %p(%s)", __func__, ifp,
543 IN6_MULTI_LOCK_ASSERT();
546 mli = MLD_IFINFO(ifp);
547 if (mli->mli_version == MLD_VERSION_2) {
549 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
550 if (ifma->ifma_addr->sa_family != AF_INET6 ||
551 ifma->ifma_protospec == NULL)
553 inm = (struct in6_multi *)ifma->ifma_protospec;
554 if (inm->in6m_state == MLD_LEAVING_MEMBER) {
555 SLIST_INSERT_HEAD(&mli->mli_relinmhead,
558 in6m_clear_recorded(inm);
560 IF_ADDR_RUNLOCK(ifp);
561 SLIST_FOREACH_SAFE(inm, &mli->mli_relinmhead, in6m_nrele,
563 SLIST_REMOVE_HEAD(&mli->mli_relinmhead, in6m_nrele);
564 in6m_release_locked(inm);
572 * Hook for domifdetach.
573 * Runs after link-layer cleanup; free MLD state.
575 * SMPng: Normally called with IF_AFDATA_LOCK held.
578 mld_domifdetach(struct ifnet *ifp)
581 CTR3(KTR_MLD, "%s: called for ifp %p(%s)",
582 __func__, ifp, if_name(ifp));
585 mli_delete_locked(ifp);
590 mli_delete_locked(const struct ifnet *ifp)
592 struct mld_ifsoftc *mli, *tmli;
594 CTR3(KTR_MLD, "%s: freeing mld_ifsoftc for ifp %p(%s)",
595 __func__, ifp, if_name(ifp));
599 LIST_FOREACH_SAFE(mli, &V_mli_head, mli_link, tmli) {
600 if (mli->mli_ifp == ifp) {
602 * Free deferred General Query responses.
604 mbufq_drain(&mli->mli_gq);
606 LIST_REMOVE(mli, mli_link);
608 KASSERT(SLIST_EMPTY(&mli->mli_relinmhead),
609 ("%s: there are dangling in_multi references",
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_ifsoftc *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_ifsoftc 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 mld points to a struct mldv2_query which is stored in
801 * Return 0 if successful, otherwise an appropriate error code is returned.
804 mld_v2_input_query(struct ifnet *ifp, const struct ip6_hdr *ip6,
805 struct mbuf *m, struct mldv2_query *mld, const int off, const int icmp6len)
807 struct mld_ifsoftc *mli;
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 maxdelay = ntohs(mld->mld_maxdelay); /* in 1/10ths of a second */
833 if (maxdelay >= 32768) {
834 maxdelay = (MLD_MRC_MANT(maxdelay) | 0x1000) <<
835 (MLD_MRC_EXP(maxdelay) + 3);
837 timer = (maxdelay * PR_FASTHZ) / MLD_TIMER_SCALE;
841 qrv = MLD_QRV(mld->mld_misc);
843 CTR3(KTR_MLD, "%s: clamping qrv %d to %d", __func__,
850 qqi = MLD_QQIC_MANT(mld->mld_qqi) <<
851 (MLD_QQIC_EXP(mld->mld_qqi) + 3);
854 nsrc = ntohs(mld->mld_numsrc);
855 if (nsrc > MLD_MAX_GS_SOURCES)
857 if (icmp6len < sizeof(struct mldv2_query) +
858 (nsrc * sizeof(struct in6_addr)))
862 * Do further input validation upfront to avoid resetting timers
863 * should we need to discard this query.
865 if (IN6_IS_ADDR_UNSPECIFIED(&mld->mld_addr)) {
867 * A general query with a source list has undefined
868 * behaviour; discard it.
872 is_general_query = 1;
875 * Embed scope ID of receiving interface in MLD query for
876 * lookup whilst we don't hold other locks (due to KAME
877 * locking lameness). We own this mbuf chain just now.
879 in6_setscope(&mld->mld_addr, ifp, NULL);
885 mli = MLD_IFINFO(ifp);
886 KASSERT(mli != NULL, ("%s: no mld_ifsoftc for ifp %p", __func__, ifp));
889 * Discard the v2 query if we're in Compatibility Mode.
890 * The RFC is pretty clear that hosts need to stay in MLDv1 mode
891 * until the Old Version Querier Present timer expires.
893 if (mli->mli_version != MLD_VERSION_2)
896 mld_set_version(mli, MLD_VERSION_2);
899 mli->mli_qri = maxdelay;
901 CTR4(KTR_MLD, "%s: qrv %d qi %d maxdelay %d", __func__, qrv, qqi,
904 if (is_general_query) {
906 * MLDv2 General Query.
908 * Schedule a current-state report on this ifp for
909 * all groups, possibly containing source lists.
911 * If there is a pending General Query response
912 * scheduled earlier than the selected delay, do
913 * not schedule any other reports.
914 * Otherwise, reset the interface timer.
916 CTR2(KTR_MLD, "process v2 general query on ifp %p(%s)",
918 if (mli->mli_v2_timer == 0 || mli->mli_v2_timer >= timer) {
919 mli->mli_v2_timer = MLD_RANDOM_DELAY(timer);
920 V_interface_timers_running6 = 1;
924 * MLDv2 Group-specific or Group-and-source-specific Query.
926 * Group-source-specific queries are throttled on
927 * a per-group basis to defeat denial-of-service attempts.
928 * Queries for groups we are not a member of on this
929 * link are simply ignored.
932 inm = in6m_lookup_locked(ifp, &mld->mld_addr);
934 IF_ADDR_RUNLOCK(ifp);
938 if (!ratecheck(&inm->in6m_lastgsrtv,
940 CTR1(KTR_MLD, "%s: GS query throttled.",
942 IF_ADDR_RUNLOCK(ifp);
946 CTR2(KTR_MLD, "process v2 group query on ifp %p(%s)",
949 * If there is a pending General Query response
950 * scheduled sooner than the selected delay, no
951 * further report need be scheduled.
952 * Otherwise, prepare to respond to the
953 * group-specific or group-and-source query.
955 if (mli->mli_v2_timer == 0 || mli->mli_v2_timer >= timer)
956 mld_v2_process_group_query(inm, mli, timer, m, mld, off);
958 /* XXX Clear embedded scope ID as userland won't expect it. */
959 in6_clearscope(&mld->mld_addr);
960 IF_ADDR_RUNLOCK(ifp);
971 * Process a received MLDv2 group-specific or group-and-source-specific
973 * Return <0 if any error occurred. Currently this is ignored.
976 mld_v2_process_group_query(struct in6_multi *inm, struct mld_ifsoftc *mli,
977 int timer, struct mbuf *m0, struct mldv2_query *mld, const int off)
982 IN6_MULTI_LOCK_ASSERT();
987 switch (inm->in6m_state) {
989 case MLD_SILENT_MEMBER:
990 case MLD_SLEEPING_MEMBER:
991 case MLD_LAZY_MEMBER:
992 case MLD_AWAKENING_MEMBER:
993 case MLD_IDLE_MEMBER:
994 case MLD_LEAVING_MEMBER:
997 case MLD_REPORTING_MEMBER:
998 case MLD_G_QUERY_PENDING_MEMBER:
999 case MLD_SG_QUERY_PENDING_MEMBER:
1003 nsrc = ntohs(mld->mld_numsrc);
1005 /* Length should be checked by calling function. */
1006 KASSERT((m0->m_flags & M_PKTHDR) == 0 ||
1007 m0->m_pkthdr.len >= off + sizeof(struct mldv2_query) +
1008 nsrc * sizeof(struct in6_addr),
1009 ("mldv2 packet is too short: (%d bytes < %zd bytes, m=%p)",
1010 m0->m_pkthdr.len, off + sizeof(struct mldv2_query) +
1011 nsrc * sizeof(struct in6_addr), m0));
1015 * Deal with group-specific queries upfront.
1016 * If any group query is already pending, purge any recorded
1017 * source-list state if it exists, and schedule a query response
1018 * for this group-specific query.
1021 if (inm->in6m_state == MLD_G_QUERY_PENDING_MEMBER ||
1022 inm->in6m_state == MLD_SG_QUERY_PENDING_MEMBER) {
1023 in6m_clear_recorded(inm);
1024 timer = min(inm->in6m_timer, timer);
1026 inm->in6m_state = MLD_G_QUERY_PENDING_MEMBER;
1027 inm->in6m_timer = MLD_RANDOM_DELAY(timer);
1028 V_current_state_timers_running6 = 1;
1033 * Deal with the case where a group-and-source-specific query has
1034 * been received but a group-specific query is already pending.
1036 if (inm->in6m_state == MLD_G_QUERY_PENDING_MEMBER) {
1037 timer = min(inm->in6m_timer, timer);
1038 inm->in6m_timer = MLD_RANDOM_DELAY(timer);
1039 V_current_state_timers_running6 = 1;
1044 * Finally, deal with the case where a group-and-source-specific
1045 * query has been received, where a response to a previous g-s-r
1046 * query exists, or none exists.
1047 * In this case, we need to parse the source-list which the Querier
1048 * has provided us with and check if we have any source list filter
1049 * entries at T1 for these sources. If we do not, there is no need
1050 * schedule a report and the query may be dropped.
1051 * If we do, we must record them and schedule a current-state
1052 * report for those sources.
1054 if (inm->in6m_nsrc > 0) {
1055 struct in6_addr srcaddr;
1059 soff = off + sizeof(struct mldv2_query);
1061 for (i = 0; i < nsrc; i++) {
1062 m_copydata(m0, soff, sizeof(struct in6_addr),
1064 retval = in6m_record_source(inm, &srcaddr);
1067 nrecorded += retval;
1068 soff += sizeof(struct in6_addr);
1070 if (nrecorded > 0) {
1072 "%s: schedule response to SG query", __func__);
1073 inm->in6m_state = MLD_SG_QUERY_PENDING_MEMBER;
1074 inm->in6m_timer = MLD_RANDOM_DELAY(timer);
1075 V_current_state_timers_running6 = 1;
1083 * Process a received MLDv1 host membership report.
1084 * Assumes mld points to mld_hdr in pulled up mbuf chain.
1086 * NOTE: Can't be fully const correct as we temporarily embed scope ID in
1087 * mld_addr. This is OK as we own the mbuf chain.
1090 mld_v1_input_report(struct ifnet *ifp, const struct ip6_hdr *ip6,
1091 /*const*/ struct mld_hdr *mld)
1093 struct in6_addr src, dst;
1094 struct in6_ifaddr *ia;
1095 struct in6_multi *inm;
1097 char ip6tbuf[INET6_ADDRSTRLEN];
1100 if (!mld_v1enable) {
1101 CTR3(KTR_MLD, "ignore v1 report %s on ifp %p(%s)",
1102 ip6_sprintf(ip6tbuf, &mld->mld_addr),
1107 if (ifp->if_flags & IFF_LOOPBACK)
1111 * MLDv1 reports must originate from a host's link-local address,
1112 * or the unspecified address (when booting).
1115 in6_clearscope(&src);
1116 if (!IN6_IS_SCOPE_LINKLOCAL(&src) && !IN6_IS_ADDR_UNSPECIFIED(&src)) {
1117 CTR3(KTR_MLD, "ignore v1 query src %s on ifp %p(%s)",
1118 ip6_sprintf(ip6tbuf, &ip6->ip6_src),
1124 * RFC2710 Section 4: MLDv1 reports must pertain to a multicast
1125 * group, and must be directed to the group itself.
1128 in6_clearscope(&dst);
1129 if (!IN6_IS_ADDR_MULTICAST(&mld->mld_addr) ||
1130 !IN6_ARE_ADDR_EQUAL(&mld->mld_addr, &dst)) {
1131 CTR3(KTR_MLD, "ignore v1 query dst %s on ifp %p(%s)",
1132 ip6_sprintf(ip6tbuf, &ip6->ip6_dst),
1138 * Make sure we don't hear our own membership report, as fast
1139 * leave requires knowing that we are the only member of a
1140 * group. Assume we used the link-local address if available,
1141 * otherwise look for ::.
1143 * XXX Note that scope ID comparison is needed for the address
1144 * returned by in6ifa_ifpforlinklocal(), but SHOULD NOT be
1145 * performed for the on-wire address.
1147 ia = in6ifa_ifpforlinklocal(ifp, IN6_IFF_NOTREADY|IN6_IFF_ANYCAST);
1148 if ((ia && IN6_ARE_ADDR_EQUAL(&ip6->ip6_src, IA6_IN6(ia))) ||
1149 (ia == NULL && IN6_IS_ADDR_UNSPECIFIED(&src))) {
1151 ifa_free(&ia->ia_ifa);
1155 ifa_free(&ia->ia_ifa);
1157 CTR3(KTR_MLD, "process v1 report %s on ifp %p(%s)",
1158 ip6_sprintf(ip6tbuf, &mld->mld_addr), ifp, if_name(ifp));
1161 * Embed scope ID of receiving interface in MLD query for lookup
1162 * whilst we don't hold other locks (due to KAME locking lameness).
1164 if (!IN6_IS_ADDR_UNSPECIFIED(&mld->mld_addr))
1165 in6_setscope(&mld->mld_addr, ifp, NULL);
1172 * MLDv1 report suppression.
1173 * If we are a member of this group, and our membership should be
1174 * reported, and our group timer is pending or about to be reset,
1175 * stop our group timer by transitioning to the 'lazy' state.
1177 inm = in6m_lookup_locked(ifp, &mld->mld_addr);
1179 struct mld_ifsoftc *mli;
1181 mli = inm->in6m_mli;
1182 KASSERT(mli != NULL,
1183 ("%s: no mli for ifp %p", __func__, ifp));
1186 * If we are in MLDv2 host mode, do not allow the
1187 * other host's MLDv1 report to suppress our reports.
1189 if (mli->mli_version == MLD_VERSION_2)
1192 inm->in6m_timer = 0;
1194 switch (inm->in6m_state) {
1195 case MLD_NOT_MEMBER:
1196 case MLD_SILENT_MEMBER:
1197 case MLD_SLEEPING_MEMBER:
1199 case MLD_REPORTING_MEMBER:
1200 case MLD_IDLE_MEMBER:
1201 case MLD_AWAKENING_MEMBER:
1203 "report suppressed for %s on ifp %p(%s)",
1204 ip6_sprintf(ip6tbuf, &mld->mld_addr),
1206 case MLD_LAZY_MEMBER:
1207 inm->in6m_state = MLD_LAZY_MEMBER;
1209 case MLD_G_QUERY_PENDING_MEMBER:
1210 case MLD_SG_QUERY_PENDING_MEMBER:
1211 case MLD_LEAVING_MEMBER:
1217 IF_ADDR_RUNLOCK(ifp);
1221 /* XXX Clear embedded scope ID as userland won't expect it. */
1222 in6_clearscope(&mld->mld_addr);
1230 * Assume query messages which fit in a single ICMPv6 message header
1231 * have been pulled up.
1232 * Assume that userland will want to see the message, even if it
1233 * otherwise fails kernel input validation; do not free it.
1234 * Pullup may however free the mbuf chain m if it fails.
1236 * Return IPPROTO_DONE if we freed m. Otherwise, return 0.
1239 mld_input(struct mbuf *m, int off, int icmp6len)
1242 struct ip6_hdr *ip6;
1243 struct mld_hdr *mld;
1246 CTR3(KTR_MLD, "%s: called w/mbuf (%p,%d)", __func__, m, off);
1248 ifp = m->m_pkthdr.rcvif;
1250 ip6 = mtod(m, struct ip6_hdr *);
1252 /* Pullup to appropriate size. */
1253 mld = (struct mld_hdr *)(mtod(m, uint8_t *) + off);
1254 if (mld->mld_type == MLD_LISTENER_QUERY &&
1255 icmp6len >= sizeof(struct mldv2_query)) {
1256 mldlen = sizeof(struct mldv2_query);
1258 mldlen = sizeof(struct mld_hdr);
1260 IP6_EXTHDR_GET(mld, struct mld_hdr *, m, off, mldlen);
1262 ICMP6STAT_INC(icp6s_badlen);
1263 return (IPPROTO_DONE);
1267 * Userland needs to see all of this traffic for implementing
1268 * the endpoint discovery portion of multicast routing.
1270 switch (mld->mld_type) {
1271 case MLD_LISTENER_QUERY:
1272 icmp6_ifstat_inc(ifp, ifs6_in_mldquery);
1273 if (icmp6len == sizeof(struct mld_hdr)) {
1274 if (mld_v1_input_query(ifp, ip6, mld) != 0)
1276 } else if (icmp6len >= sizeof(struct mldv2_query)) {
1277 if (mld_v2_input_query(ifp, ip6, m,
1278 (struct mldv2_query *)mld, off, icmp6len) != 0)
1282 case MLD_LISTENER_REPORT:
1283 icmp6_ifstat_inc(ifp, ifs6_in_mldreport);
1284 if (mld_v1_input_report(ifp, ip6, mld) != 0)
1287 case MLDV2_LISTENER_REPORT:
1288 icmp6_ifstat_inc(ifp, ifs6_in_mldreport);
1290 case MLD_LISTENER_DONE:
1291 icmp6_ifstat_inc(ifp, ifs6_in_mlddone);
1301 * Fast timeout handler (global).
1302 * VIMAGE: Timeout handlers are expected to service all vimages.
1307 VNET_ITERATOR_DECL(vnet_iter);
1309 VNET_LIST_RLOCK_NOSLEEP();
1310 VNET_FOREACH(vnet_iter) {
1311 CURVNET_SET(vnet_iter);
1312 mld_fasttimo_vnet();
1315 VNET_LIST_RUNLOCK_NOSLEEP();
1319 * Fast timeout handler (per-vnet).
1321 * VIMAGE: Assume caller has set up our curvnet.
1324 mld_fasttimo_vnet(void)
1326 struct mbufq scq; /* State-change packets */
1327 struct mbufq qrq; /* Query response packets */
1329 struct mld_ifsoftc *mli;
1330 struct ifmultiaddr *ifma;
1331 struct in6_multi *inm, *tinm;
1337 * Quick check to see if any work needs to be done, in order to
1338 * minimize the overhead of fasttimo processing.
1339 * SMPng: XXX Unlocked reads.
1341 if (!V_current_state_timers_running6 &&
1342 !V_interface_timers_running6 &&
1343 !V_state_change_timers_running6)
1350 * MLDv2 General Query response timer processing.
1352 if (V_interface_timers_running6) {
1353 CTR1(KTR_MLD, "%s: interface timers running", __func__);
1355 V_interface_timers_running6 = 0;
1356 LIST_FOREACH(mli, &V_mli_head, mli_link) {
1357 if (mli->mli_v2_timer == 0) {
1359 } else if (--mli->mli_v2_timer == 0) {
1360 mld_v2_dispatch_general_query(mli);
1362 V_interface_timers_running6 = 1;
1367 if (!V_current_state_timers_running6 &&
1368 !V_state_change_timers_running6)
1371 V_current_state_timers_running6 = 0;
1372 V_state_change_timers_running6 = 0;
1374 CTR1(KTR_MLD, "%s: state change timers running", __func__);
1377 * MLD host report and state-change timer processing.
1378 * Note: Processing a v2 group timer may remove a node.
1380 LIST_FOREACH(mli, &V_mli_head, mli_link) {
1383 if (mli->mli_version == MLD_VERSION_2) {
1384 uri_fasthz = MLD_RANDOM_DELAY(mli->mli_uri *
1386 mbufq_init(&qrq, MLD_MAX_G_GS_PACKETS);
1387 mbufq_init(&scq, MLD_MAX_STATE_CHANGE_PACKETS);
1391 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
1392 if (ifma->ifma_addr->sa_family != AF_INET6 ||
1393 ifma->ifma_protospec == NULL)
1395 inm = (struct in6_multi *)ifma->ifma_protospec;
1396 switch (mli->mli_version) {
1398 mld_v1_process_group_timer(mli, inm);
1401 mld_v2_process_group_timers(mli, &qrq,
1402 &scq, inm, uri_fasthz);
1406 IF_ADDR_RUNLOCK(ifp);
1408 switch (mli->mli_version) {
1411 * Transmit reports for this lifecycle. This
1412 * is done while not holding IF_ADDR_LOCK
1413 * since this can call
1414 * in6ifa_ifpforlinklocal() which locks
1415 * IF_ADDR_LOCK internally as well as
1416 * ip6_output() to transmit a packet.
1418 SLIST_FOREACH_SAFE(inm, &mli->mli_relinmhead,
1420 SLIST_REMOVE_HEAD(&mli->mli_relinmhead,
1422 (void)mld_v1_transmit_report(inm,
1423 MLD_LISTENER_REPORT);
1427 mld_dispatch_queue(&qrq, 0);
1428 mld_dispatch_queue(&scq, 0);
1431 * Free the in_multi reference(s) for
1434 SLIST_FOREACH_SAFE(inm, &mli->mli_relinmhead,
1436 SLIST_REMOVE_HEAD(&mli->mli_relinmhead,
1438 in6m_release_locked(inm);
1450 * Update host report group timer.
1451 * Will update the global pending timer flags.
1454 mld_v1_process_group_timer(struct mld_ifsoftc *mli, struct in6_multi *inm)
1456 int report_timer_expired;
1458 IN6_MULTI_LOCK_ASSERT();
1461 if (inm->in6m_timer == 0) {
1462 report_timer_expired = 0;
1463 } else if (--inm->in6m_timer == 0) {
1464 report_timer_expired = 1;
1466 V_current_state_timers_running6 = 1;
1470 switch (inm->in6m_state) {
1471 case MLD_NOT_MEMBER:
1472 case MLD_SILENT_MEMBER:
1473 case MLD_IDLE_MEMBER:
1474 case MLD_LAZY_MEMBER:
1475 case MLD_SLEEPING_MEMBER:
1476 case MLD_AWAKENING_MEMBER:
1478 case MLD_REPORTING_MEMBER:
1479 if (report_timer_expired) {
1480 inm->in6m_state = MLD_IDLE_MEMBER;
1481 SLIST_INSERT_HEAD(&mli->mli_relinmhead, inm,
1485 case MLD_G_QUERY_PENDING_MEMBER:
1486 case MLD_SG_QUERY_PENDING_MEMBER:
1487 case MLD_LEAVING_MEMBER:
1493 * Update a group's timers for MLDv2.
1494 * Will update the global pending timer flags.
1495 * Note: Unlocked read from mli.
1498 mld_v2_process_group_timers(struct mld_ifsoftc *mli,
1499 struct mbufq *qrq, struct mbufq *scq,
1500 struct in6_multi *inm, const int uri_fasthz)
1502 int query_response_timer_expired;
1503 int state_change_retransmit_timer_expired;
1505 char ip6tbuf[INET6_ADDRSTRLEN];
1508 IN6_MULTI_LOCK_ASSERT();
1511 query_response_timer_expired = 0;
1512 state_change_retransmit_timer_expired = 0;
1515 * During a transition from compatibility mode back to MLDv2,
1516 * a group record in REPORTING state may still have its group
1517 * timer active. This is a no-op in this function; it is easier
1518 * to deal with it here than to complicate the slow-timeout path.
1520 if (inm->in6m_timer == 0) {
1521 query_response_timer_expired = 0;
1522 } else if (--inm->in6m_timer == 0) {
1523 query_response_timer_expired = 1;
1525 V_current_state_timers_running6 = 1;
1528 if (inm->in6m_sctimer == 0) {
1529 state_change_retransmit_timer_expired = 0;
1530 } else if (--inm->in6m_sctimer == 0) {
1531 state_change_retransmit_timer_expired = 1;
1533 V_state_change_timers_running6 = 1;
1536 /* We are in fasttimo, so be quick about it. */
1537 if (!state_change_retransmit_timer_expired &&
1538 !query_response_timer_expired)
1541 switch (inm->in6m_state) {
1542 case MLD_NOT_MEMBER:
1543 case MLD_SILENT_MEMBER:
1544 case MLD_SLEEPING_MEMBER:
1545 case MLD_LAZY_MEMBER:
1546 case MLD_AWAKENING_MEMBER:
1547 case MLD_IDLE_MEMBER:
1549 case MLD_G_QUERY_PENDING_MEMBER:
1550 case MLD_SG_QUERY_PENDING_MEMBER:
1552 * Respond to a previously pending Group-Specific
1553 * or Group-and-Source-Specific query by enqueueing
1554 * the appropriate Current-State report for
1555 * immediate transmission.
1557 if (query_response_timer_expired) {
1560 retval = mld_v2_enqueue_group_record(qrq, inm, 0, 1,
1561 (inm->in6m_state == MLD_SG_QUERY_PENDING_MEMBER),
1563 CTR2(KTR_MLD, "%s: enqueue record = %d",
1565 inm->in6m_state = MLD_REPORTING_MEMBER;
1566 in6m_clear_recorded(inm);
1569 case MLD_REPORTING_MEMBER:
1570 case MLD_LEAVING_MEMBER:
1571 if (state_change_retransmit_timer_expired) {
1573 * State-change retransmission timer fired.
1574 * If there are any further pending retransmissions,
1575 * set the global pending state-change flag, and
1578 if (--inm->in6m_scrv > 0) {
1579 inm->in6m_sctimer = uri_fasthz;
1580 V_state_change_timers_running6 = 1;
1583 * Retransmit the previously computed state-change
1584 * report. If there are no further pending
1585 * retransmissions, the mbuf queue will be consumed.
1586 * Update T0 state to T1 as we have now sent
1589 (void)mld_v2_merge_state_changes(inm, scq);
1592 CTR3(KTR_MLD, "%s: T1 -> T0 for %s/%s", __func__,
1593 ip6_sprintf(ip6tbuf, &inm->in6m_addr),
1594 if_name(inm->in6m_ifp));
1597 * If we are leaving the group for good, make sure
1598 * we release MLD's reference to it.
1599 * This release must be deferred using a SLIST,
1600 * as we are called from a loop which traverses
1601 * the in_ifmultiaddr TAILQ.
1603 if (inm->in6m_state == MLD_LEAVING_MEMBER &&
1604 inm->in6m_scrv == 0) {
1605 inm->in6m_state = MLD_NOT_MEMBER;
1606 SLIST_INSERT_HEAD(&mli->mli_relinmhead,
1615 * Switch to a different version on the given interface,
1616 * as per Section 9.12.
1619 mld_set_version(struct mld_ifsoftc *mli, const int version)
1621 int old_version_timer;
1625 CTR4(KTR_MLD, "%s: switching to v%d on ifp %p(%s)", __func__,
1626 version, mli->mli_ifp, if_name(mli->mli_ifp));
1628 if (version == MLD_VERSION_1) {
1630 * Compute the "Older Version Querier Present" timer as per
1633 old_version_timer = (mli->mli_rv * mli->mli_qi) + mli->mli_qri;
1634 old_version_timer *= PR_SLOWHZ;
1635 mli->mli_v1_timer = old_version_timer;
1638 if (mli->mli_v1_timer > 0 && mli->mli_version != MLD_VERSION_1) {
1639 mli->mli_version = MLD_VERSION_1;
1640 mld_v2_cancel_link_timers(mli);
1645 * Cancel pending MLDv2 timers for the given link and all groups
1646 * joined on it; state-change, general-query, and group-query timers.
1649 mld_v2_cancel_link_timers(struct mld_ifsoftc *mli)
1651 struct ifmultiaddr *ifma;
1653 struct in6_multi *inm, *tinm;
1655 CTR3(KTR_MLD, "%s: cancel v2 timers on ifp %p(%s)", __func__,
1656 mli->mli_ifp, if_name(mli->mli_ifp));
1658 IN6_MULTI_LOCK_ASSERT();
1662 * Fast-track this potentially expensive operation
1663 * by checking all the global 'timer pending' flags.
1665 if (!V_interface_timers_running6 &&
1666 !V_state_change_timers_running6 &&
1667 !V_current_state_timers_running6)
1670 mli->mli_v2_timer = 0;
1675 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
1676 if (ifma->ifma_addr->sa_family != AF_INET6)
1678 inm = (struct in6_multi *)ifma->ifma_protospec;
1679 switch (inm->in6m_state) {
1680 case MLD_NOT_MEMBER:
1681 case MLD_SILENT_MEMBER:
1682 case MLD_IDLE_MEMBER:
1683 case MLD_LAZY_MEMBER:
1684 case MLD_SLEEPING_MEMBER:
1685 case MLD_AWAKENING_MEMBER:
1687 case MLD_LEAVING_MEMBER:
1689 * If we are leaving the group and switching
1690 * version, we need to release the final
1691 * reference held for issuing the INCLUDE {}.
1693 SLIST_INSERT_HEAD(&mli->mli_relinmhead, inm,
1696 case MLD_G_QUERY_PENDING_MEMBER:
1697 case MLD_SG_QUERY_PENDING_MEMBER:
1698 in6m_clear_recorded(inm);
1700 case MLD_REPORTING_MEMBER:
1701 inm->in6m_sctimer = 0;
1702 inm->in6m_timer = 0;
1703 inm->in6m_state = MLD_REPORTING_MEMBER;
1705 * Free any pending MLDv2 state-change records.
1707 mbufq_drain(&inm->in6m_scq);
1711 IF_ADDR_RUNLOCK(ifp);
1712 SLIST_FOREACH_SAFE(inm, &mli->mli_relinmhead, in6m_nrele, tinm) {
1713 SLIST_REMOVE_HEAD(&mli->mli_relinmhead, in6m_nrele);
1714 in6m_release_locked(inm);
1719 * Global slowtimo handler.
1720 * VIMAGE: Timeout handlers are expected to service all vimages.
1725 VNET_ITERATOR_DECL(vnet_iter);
1727 VNET_LIST_RLOCK_NOSLEEP();
1728 VNET_FOREACH(vnet_iter) {
1729 CURVNET_SET(vnet_iter);
1730 mld_slowtimo_vnet();
1733 VNET_LIST_RUNLOCK_NOSLEEP();
1737 * Per-vnet slowtimo handler.
1740 mld_slowtimo_vnet(void)
1742 struct mld_ifsoftc *mli;
1746 LIST_FOREACH(mli, &V_mli_head, mli_link) {
1747 mld_v1_process_querier_timers(mli);
1754 * Update the Older Version Querier Present timers for a link.
1755 * See Section 9.12 of RFC 3810.
1758 mld_v1_process_querier_timers(struct mld_ifsoftc *mli)
1763 if (mli->mli_version != MLD_VERSION_2 && --mli->mli_v1_timer == 0) {
1765 * MLDv1 Querier Present timer expired; revert to MLDv2.
1768 "%s: transition from v%d -> v%d on %p(%s)",
1769 __func__, mli->mli_version, MLD_VERSION_2,
1770 mli->mli_ifp, if_name(mli->mli_ifp));
1771 mli->mli_version = MLD_VERSION_2;
1776 * Transmit an MLDv1 report immediately.
1779 mld_v1_transmit_report(struct in6_multi *in6m, const int type)
1782 struct in6_ifaddr *ia;
1783 struct ip6_hdr *ip6;
1784 struct mbuf *mh, *md;
1785 struct mld_hdr *mld;
1787 IN6_MULTI_LOCK_ASSERT();
1790 ifp = in6m->in6m_ifp;
1791 ia = in6ifa_ifpforlinklocal(ifp, IN6_IFF_NOTREADY|IN6_IFF_ANYCAST);
1792 /* ia may be NULL if link-local address is tentative. */
1794 mh = m_gethdr(M_NOWAIT, MT_DATA);
1797 ifa_free(&ia->ia_ifa);
1800 md = m_get(M_NOWAIT, MT_DATA);
1804 ifa_free(&ia->ia_ifa);
1810 * FUTURE: Consider increasing alignment by ETHER_HDR_LEN, so
1811 * that ether_output() does not need to allocate another mbuf
1812 * for the header in the most common case.
1814 M_ALIGN(mh, sizeof(struct ip6_hdr));
1815 mh->m_pkthdr.len = sizeof(struct ip6_hdr) + sizeof(struct mld_hdr);
1816 mh->m_len = sizeof(struct ip6_hdr);
1818 ip6 = mtod(mh, struct ip6_hdr *);
1820 ip6->ip6_vfc &= ~IPV6_VERSION_MASK;
1821 ip6->ip6_vfc |= IPV6_VERSION;
1822 ip6->ip6_nxt = IPPROTO_ICMPV6;
1823 ip6->ip6_src = ia ? ia->ia_addr.sin6_addr : in6addr_any;
1824 ip6->ip6_dst = in6m->in6m_addr;
1826 md->m_len = sizeof(struct mld_hdr);
1827 mld = mtod(md, struct mld_hdr *);
1828 mld->mld_type = type;
1831 mld->mld_maxdelay = 0;
1832 mld->mld_reserved = 0;
1833 mld->mld_addr = in6m->in6m_addr;
1834 in6_clearscope(&mld->mld_addr);
1835 mld->mld_cksum = in6_cksum(mh, IPPROTO_ICMPV6,
1836 sizeof(struct ip6_hdr), sizeof(struct mld_hdr));
1838 mld_save_context(mh, ifp);
1839 mh->m_flags |= M_MLDV1;
1841 mld_dispatch_packet(mh);
1844 ifa_free(&ia->ia_ifa);
1849 * Process a state change from the upper layer for the given IPv6 group.
1851 * Each socket holds a reference on the in_multi in its own ip_moptions.
1852 * The socket layer will have made the necessary updates to.the group
1853 * state, it is now up to MLD to issue a state change report if there
1854 * has been any change between T0 (when the last state-change was issued)
1857 * We use the MLDv2 state machine at group level. The MLd module
1858 * however makes the decision as to which MLD protocol version to speak.
1859 * A state change *from* INCLUDE {} always means an initial join.
1860 * A state change *to* INCLUDE {} always means a final leave.
1862 * If delay is non-zero, and the state change is an initial multicast
1863 * join, the state change report will be delayed by 'delay' ticks
1864 * in units of PR_FASTHZ if MLDv1 is active on the link; otherwise
1865 * the initial MLDv2 state change report will be delayed by whichever
1866 * is sooner, a pending state-change timer or delay itself.
1868 * VIMAGE: curvnet should have been set by caller, as this routine
1869 * is called from the socket option handlers.
1872 mld_change_state(struct in6_multi *inm, const int delay)
1874 struct mld_ifsoftc *mli;
1878 IN6_MULTI_LOCK_ASSERT();
1883 * Try to detect if the upper layer just asked us to change state
1884 * for an interface which has now gone away.
1886 KASSERT(inm->in6m_ifma != NULL, ("%s: no ifma", __func__));
1887 ifp = inm->in6m_ifma->ifma_ifp;
1890 * Sanity check that netinet6's notion of ifp is the
1893 KASSERT(inm->in6m_ifp == ifp, ("%s: bad ifp", __func__));
1898 mli = MLD_IFINFO(ifp);
1899 KASSERT(mli != NULL, ("%s: no mld_ifsoftc for ifp %p", __func__, ifp));
1902 * If we detect a state transition to or from MCAST_UNDEFINED
1903 * for this group, then we are starting or finishing an MLD
1904 * life cycle for this group.
1906 if (inm->in6m_st[1].iss_fmode != inm->in6m_st[0].iss_fmode) {
1907 CTR3(KTR_MLD, "%s: inm transition %d -> %d", __func__,
1908 inm->in6m_st[0].iss_fmode, inm->in6m_st[1].iss_fmode);
1909 if (inm->in6m_st[0].iss_fmode == MCAST_UNDEFINED) {
1910 CTR1(KTR_MLD, "%s: initial join", __func__);
1911 error = mld_initial_join(inm, mli, delay);
1913 } else if (inm->in6m_st[1].iss_fmode == MCAST_UNDEFINED) {
1914 CTR1(KTR_MLD, "%s: final leave", __func__);
1915 mld_final_leave(inm, mli);
1919 CTR1(KTR_MLD, "%s: filter set change", __func__);
1922 error = mld_handle_state_change(inm, mli);
1930 * Perform the initial join for an MLD group.
1932 * When joining a group:
1933 * If the group should have its MLD traffic suppressed, do nothing.
1934 * MLDv1 starts sending MLDv1 host membership reports.
1935 * MLDv2 will schedule an MLDv2 state-change report containing the
1936 * initial state of the membership.
1938 * If the delay argument is non-zero, then we must delay sending the
1939 * initial state change for delay ticks (in units of PR_FASTHZ).
1942 mld_initial_join(struct in6_multi *inm, struct mld_ifsoftc *mli,
1947 int error, retval, syncstates;
1950 char ip6tbuf[INET6_ADDRSTRLEN];
1953 CTR4(KTR_MLD, "%s: initial join %s on ifp %p(%s)",
1954 __func__, ip6_sprintf(ip6tbuf, &inm->in6m_addr),
1955 inm->in6m_ifp, if_name(inm->in6m_ifp));
1960 ifp = inm->in6m_ifp;
1962 IN6_MULTI_LOCK_ASSERT();
1965 KASSERT(mli && mli->mli_ifp == ifp, ("%s: inconsistent ifp", __func__));
1968 * Groups joined on loopback or marked as 'not reported',
1969 * enter the MLD_SILENT_MEMBER state and
1970 * are never reported in any protocol exchanges.
1971 * All other groups enter the appropriate state machine
1972 * for the version in use on this link.
1973 * A link marked as MLIF_SILENT causes MLD to be completely
1974 * disabled for the link.
1976 if ((ifp->if_flags & IFF_LOOPBACK) ||
1977 (mli->mli_flags & MLIF_SILENT) ||
1978 !mld_is_addr_reported(&inm->in6m_addr)) {
1980 "%s: not kicking state machine for silent group", __func__);
1981 inm->in6m_state = MLD_SILENT_MEMBER;
1982 inm->in6m_timer = 0;
1985 * Deal with overlapping in_multi lifecycle.
1986 * If this group was LEAVING, then make sure
1987 * we drop the reference we picked up to keep the
1988 * group around for the final INCLUDE {} enqueue.
1990 if (mli->mli_version == MLD_VERSION_2 &&
1991 inm->in6m_state == MLD_LEAVING_MEMBER)
1992 in6m_release_locked(inm);
1994 inm->in6m_state = MLD_REPORTING_MEMBER;
1996 switch (mli->mli_version) {
1999 * If a delay was provided, only use it if
2000 * it is greater than the delay normally
2001 * used for an MLDv1 state change report,
2002 * and delay sending the initial MLDv1 report
2003 * by not transitioning to the IDLE state.
2005 odelay = MLD_RANDOM_DELAY(MLD_V1_MAX_RI * PR_FASTHZ);
2007 inm->in6m_timer = max(delay, odelay);
2008 V_current_state_timers_running6 = 1;
2010 inm->in6m_state = MLD_IDLE_MEMBER;
2011 error = mld_v1_transmit_report(inm,
2012 MLD_LISTENER_REPORT);
2014 inm->in6m_timer = odelay;
2015 V_current_state_timers_running6 = 1;
2022 * Defer update of T0 to T1, until the first copy
2023 * of the state change has been transmitted.
2028 * Immediately enqueue a State-Change Report for
2029 * this interface, freeing any previous reports.
2030 * Don't kick the timers if there is nothing to do,
2031 * or if an error occurred.
2033 mq = &inm->in6m_scq;
2035 retval = mld_v2_enqueue_group_record(mq, inm, 1,
2036 0, 0, (mli->mli_flags & MLIF_USEALLOW));
2037 CTR2(KTR_MLD, "%s: enqueue record = %d",
2040 error = retval * -1;
2045 * Schedule transmission of pending state-change
2046 * report up to RV times for this link. The timer
2047 * will fire at the next mld_fasttimo (~200ms),
2048 * giving us an opportunity to merge the reports.
2050 * If a delay was provided to this function, only
2051 * use this delay if sooner than the existing one.
2053 KASSERT(mli->mli_rv > 1,
2054 ("%s: invalid robustness %d", __func__,
2056 inm->in6m_scrv = mli->mli_rv;
2058 if (inm->in6m_sctimer > 1) {
2060 min(inm->in6m_sctimer, delay);
2062 inm->in6m_sctimer = delay;
2064 inm->in6m_sctimer = 1;
2065 V_state_change_timers_running6 = 1;
2073 * Only update the T0 state if state change is atomic,
2074 * i.e. we don't need to wait for a timer to fire before we
2075 * can consider the state change to have been communicated.
2079 CTR3(KTR_MLD, "%s: T1 -> T0 for %s/%s", __func__,
2080 ip6_sprintf(ip6tbuf, &inm->in6m_addr),
2081 if_name(inm->in6m_ifp));
2088 * Issue an intermediate state change during the life-cycle.
2091 mld_handle_state_change(struct in6_multi *inm, struct mld_ifsoftc *mli)
2096 char ip6tbuf[INET6_ADDRSTRLEN];
2099 CTR4(KTR_MLD, "%s: state change for %s on ifp %p(%s)",
2100 __func__, ip6_sprintf(ip6tbuf, &inm->in6m_addr),
2101 inm->in6m_ifp, if_name(inm->in6m_ifp));
2103 ifp = inm->in6m_ifp;
2105 IN6_MULTI_LOCK_ASSERT();
2108 KASSERT(mli && mli->mli_ifp == ifp,
2109 ("%s: inconsistent ifp", __func__));
2111 if ((ifp->if_flags & IFF_LOOPBACK) ||
2112 (mli->mli_flags & MLIF_SILENT) ||
2113 !mld_is_addr_reported(&inm->in6m_addr) ||
2114 (mli->mli_version != MLD_VERSION_2)) {
2115 if (!mld_is_addr_reported(&inm->in6m_addr)) {
2117 "%s: not kicking state machine for silent group", __func__);
2119 CTR1(KTR_MLD, "%s: nothing to do", __func__);
2121 CTR3(KTR_MLD, "%s: T1 -> T0 for %s/%s", __func__,
2122 ip6_sprintf(ip6tbuf, &inm->in6m_addr),
2123 if_name(inm->in6m_ifp));
2127 mbufq_drain(&inm->in6m_scq);
2129 retval = mld_v2_enqueue_group_record(&inm->in6m_scq, inm, 1, 0, 0,
2130 (mli->mli_flags & MLIF_USEALLOW));
2131 CTR2(KTR_MLD, "%s: enqueue record = %d", __func__, retval);
2136 * If record(s) were enqueued, start the state-change
2137 * report timer for this group.
2139 inm->in6m_scrv = mli->mli_rv;
2140 inm->in6m_sctimer = 1;
2141 V_state_change_timers_running6 = 1;
2147 * Perform the final leave for a multicast address.
2149 * When leaving a group:
2150 * MLDv1 sends a DONE message, if and only if we are the reporter.
2151 * MLDv2 enqueues a state-change report containing a transition
2152 * to INCLUDE {} for immediate transmission.
2155 mld_final_leave(struct in6_multi *inm, struct mld_ifsoftc *mli)
2159 char ip6tbuf[INET6_ADDRSTRLEN];
2164 CTR4(KTR_MLD, "%s: final leave %s on ifp %p(%s)",
2165 __func__, ip6_sprintf(ip6tbuf, &inm->in6m_addr),
2166 inm->in6m_ifp, if_name(inm->in6m_ifp));
2168 IN6_MULTI_LOCK_ASSERT();
2171 switch (inm->in6m_state) {
2172 case MLD_NOT_MEMBER:
2173 case MLD_SILENT_MEMBER:
2174 case MLD_LEAVING_MEMBER:
2175 /* Already leaving or left; do nothing. */
2177 "%s: not kicking state machine for silent group", __func__);
2179 case MLD_REPORTING_MEMBER:
2180 case MLD_IDLE_MEMBER:
2181 case MLD_G_QUERY_PENDING_MEMBER:
2182 case MLD_SG_QUERY_PENDING_MEMBER:
2183 if (mli->mli_version == MLD_VERSION_1) {
2185 if (inm->in6m_state == MLD_G_QUERY_PENDING_MEMBER ||
2186 inm->in6m_state == MLD_SG_QUERY_PENDING_MEMBER)
2187 panic("%s: MLDv2 state reached, not MLDv2 mode",
2190 mld_v1_transmit_report(inm, MLD_LISTENER_DONE);
2191 inm->in6m_state = MLD_NOT_MEMBER;
2192 V_current_state_timers_running6 = 1;
2193 } else if (mli->mli_version == MLD_VERSION_2) {
2195 * Stop group timer and all pending reports.
2196 * Immediately enqueue a state-change report
2197 * TO_IN {} to be sent on the next fast timeout,
2198 * giving us an opportunity to merge reports.
2200 mbufq_drain(&inm->in6m_scq);
2201 inm->in6m_timer = 0;
2202 inm->in6m_scrv = mli->mli_rv;
2203 CTR4(KTR_MLD, "%s: Leaving %s/%s with %d "
2204 "pending retransmissions.", __func__,
2205 ip6_sprintf(ip6tbuf, &inm->in6m_addr),
2206 if_name(inm->in6m_ifp), inm->in6m_scrv);
2207 if (inm->in6m_scrv == 0) {
2208 inm->in6m_state = MLD_NOT_MEMBER;
2209 inm->in6m_sctimer = 0;
2213 in6m_acquire_locked(inm);
2215 retval = mld_v2_enqueue_group_record(
2216 &inm->in6m_scq, inm, 1, 0, 0,
2217 (mli->mli_flags & MLIF_USEALLOW));
2218 KASSERT(retval != 0,
2219 ("%s: enqueue record = %d", __func__,
2222 inm->in6m_state = MLD_LEAVING_MEMBER;
2223 inm->in6m_sctimer = 1;
2224 V_state_change_timers_running6 = 1;
2230 case MLD_LAZY_MEMBER:
2231 case MLD_SLEEPING_MEMBER:
2232 case MLD_AWAKENING_MEMBER:
2233 /* Our reports are suppressed; do nothing. */
2239 CTR3(KTR_MLD, "%s: T1 -> T0 for %s/%s", __func__,
2240 ip6_sprintf(ip6tbuf, &inm->in6m_addr),
2241 if_name(inm->in6m_ifp));
2242 inm->in6m_st[1].iss_fmode = MCAST_UNDEFINED;
2243 CTR3(KTR_MLD, "%s: T1 now MCAST_UNDEFINED for %p/%s",
2244 __func__, &inm->in6m_addr, if_name(inm->in6m_ifp));
2249 * Enqueue an MLDv2 group record to the given output queue.
2251 * If is_state_change is zero, a current-state record is appended.
2252 * If is_state_change is non-zero, a state-change report is appended.
2254 * If is_group_query is non-zero, an mbuf packet chain is allocated.
2255 * If is_group_query is zero, and if there is a packet with free space
2256 * at the tail of the queue, it will be appended to providing there
2257 * is enough free space.
2258 * Otherwise a new mbuf packet chain is allocated.
2260 * If is_source_query is non-zero, each source is checked to see if
2261 * it was recorded for a Group-Source query, and will be omitted if
2262 * it is not both in-mode and recorded.
2264 * If use_block_allow is non-zero, state change reports for initial join
2265 * and final leave, on an inclusive mode group with a source list, will be
2266 * rewritten to use the ALLOW_NEW and BLOCK_OLD record types, respectively.
2268 * The function will attempt to allocate leading space in the packet
2269 * for the IPv6+ICMP headers to be prepended without fragmenting the chain.
2271 * If successful the size of all data appended to the queue is returned,
2272 * otherwise an error code less than zero is returned, or zero if
2273 * no record(s) were appended.
2276 mld_v2_enqueue_group_record(struct mbufq *mq, struct in6_multi *inm,
2277 const int is_state_change, const int is_group_query,
2278 const int is_source_query, const int use_block_allow)
2280 struct mldv2_record mr;
2281 struct mldv2_record *pmr;
2283 struct ip6_msource *ims, *nims;
2284 struct mbuf *m0, *m, *md;
2285 int error, is_filter_list_change;
2286 int minrec0len, m0srcs, msrcs, nbytes, off;
2287 int record_has_sources;
2292 char ip6tbuf[INET6_ADDRSTRLEN];
2295 IN6_MULTI_LOCK_ASSERT();
2298 ifp = inm->in6m_ifp;
2299 is_filter_list_change = 0;
2306 record_has_sources = 1;
2308 type = MLD_DO_NOTHING;
2309 mode = inm->in6m_st[1].iss_fmode;
2312 * If we did not transition out of ASM mode during t0->t1,
2313 * and there are no source nodes to process, we can skip
2314 * the generation of source records.
2316 if (inm->in6m_st[0].iss_asm > 0 && inm->in6m_st[1].iss_asm > 0 &&
2317 inm->in6m_nsrc == 0)
2318 record_has_sources = 0;
2320 if (is_state_change) {
2322 * Queue a state change record.
2323 * If the mode did not change, and there are non-ASM
2324 * listeners or source filters present,
2325 * we potentially need to issue two records for the group.
2326 * If there are ASM listeners, and there was no filter
2327 * mode transition of any kind, do nothing.
2329 * If we are transitioning to MCAST_UNDEFINED, we need
2330 * not send any sources. A transition to/from this state is
2331 * considered inclusive with some special treatment.
2333 * If we are rewriting initial joins/leaves to use
2334 * ALLOW/BLOCK, and the group's membership is inclusive,
2335 * we need to send sources in all cases.
2337 if (mode != inm->in6m_st[0].iss_fmode) {
2338 if (mode == MCAST_EXCLUDE) {
2339 CTR1(KTR_MLD, "%s: change to EXCLUDE",
2341 type = MLD_CHANGE_TO_EXCLUDE_MODE;
2343 CTR1(KTR_MLD, "%s: change to INCLUDE",
2345 if (use_block_allow) {
2348 * Here we're interested in state
2349 * edges either direction between
2350 * MCAST_UNDEFINED and MCAST_INCLUDE.
2351 * Perhaps we should just check
2352 * the group state, rather than
2355 if (mode == MCAST_UNDEFINED) {
2356 type = MLD_BLOCK_OLD_SOURCES;
2358 type = MLD_ALLOW_NEW_SOURCES;
2361 type = MLD_CHANGE_TO_INCLUDE_MODE;
2362 if (mode == MCAST_UNDEFINED)
2363 record_has_sources = 0;
2367 if (record_has_sources) {
2368 is_filter_list_change = 1;
2370 type = MLD_DO_NOTHING;
2375 * Queue a current state record.
2377 if (mode == MCAST_EXCLUDE) {
2378 type = MLD_MODE_IS_EXCLUDE;
2379 } else if (mode == MCAST_INCLUDE) {
2380 type = MLD_MODE_IS_INCLUDE;
2381 KASSERT(inm->in6m_st[1].iss_asm == 0,
2382 ("%s: inm %p is INCLUDE but ASM count is %d",
2383 __func__, inm, inm->in6m_st[1].iss_asm));
2388 * Generate the filter list changes using a separate function.
2390 if (is_filter_list_change)
2391 return (mld_v2_enqueue_filter_change(mq, inm));
2393 if (type == MLD_DO_NOTHING) {
2394 CTR3(KTR_MLD, "%s: nothing to do for %s/%s",
2395 __func__, ip6_sprintf(ip6tbuf, &inm->in6m_addr),
2396 if_name(inm->in6m_ifp));
2401 * If any sources are present, we must be able to fit at least
2402 * one in the trailing space of the tail packet's mbuf,
2405 minrec0len = sizeof(struct mldv2_record);
2406 if (record_has_sources)
2407 minrec0len += sizeof(struct in6_addr);
2409 CTR4(KTR_MLD, "%s: queueing %s for %s/%s", __func__,
2410 mld_rec_type_to_str(type),
2411 ip6_sprintf(ip6tbuf, &inm->in6m_addr),
2412 if_name(inm->in6m_ifp));
2415 * Check if we have a packet in the tail of the queue for this
2416 * group into which the first group record for this group will fit.
2417 * Otherwise allocate a new packet.
2418 * Always allocate leading space for IP6+RA+ICMPV6+REPORT.
2419 * Note: Group records for G/GSR query responses MUST be sent
2420 * in their own packet.
2422 m0 = mbufq_last(mq);
2423 if (!is_group_query &&
2425 (m0->m_pkthdr.PH_vt.vt_nrecs + 1 <= MLD_V2_REPORT_MAXRECS) &&
2426 (m0->m_pkthdr.len + minrec0len) <
2427 (ifp->if_mtu - MLD_MTUSPACE)) {
2428 m0srcs = (ifp->if_mtu - m0->m_pkthdr.len -
2429 sizeof(struct mldv2_record)) /
2430 sizeof(struct in6_addr);
2432 CTR1(KTR_MLD, "%s: use existing packet", __func__);
2434 if (mbufq_full(mq)) {
2435 CTR1(KTR_MLD, "%s: outbound queue full", __func__);
2439 m0srcs = (ifp->if_mtu - MLD_MTUSPACE -
2440 sizeof(struct mldv2_record)) / sizeof(struct in6_addr);
2441 if (!is_state_change && !is_group_query)
2442 m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
2444 m = m_gethdr(M_NOWAIT, MT_DATA);
2448 mld_save_context(m, ifp);
2450 CTR1(KTR_MLD, "%s: allocated first packet", __func__);
2454 * Append group record.
2455 * If we have sources, we don't know how many yet.
2460 mr.mr_addr = inm->in6m_addr;
2461 in6_clearscope(&mr.mr_addr);
2462 if (!m_append(m, sizeof(struct mldv2_record), (void *)&mr)) {
2465 CTR1(KTR_MLD, "%s: m_append() failed.", __func__);
2468 nbytes += sizeof(struct mldv2_record);
2471 * Append as many sources as will fit in the first packet.
2472 * If we are appending to a new packet, the chain allocation
2473 * may potentially use clusters; use m_getptr() in this case.
2474 * If we are appending to an existing packet, we need to obtain
2475 * a pointer to the group record after m_append(), in case a new
2476 * mbuf was allocated.
2478 * Only append sources which are in-mode at t1. If we are
2479 * transitioning to MCAST_UNDEFINED state on the group, and
2480 * use_block_allow is zero, do not include source entries.
2481 * Otherwise, we need to include this source in the report.
2483 * Only report recorded sources in our filter set when responding
2484 * to a group-source query.
2486 if (record_has_sources) {
2489 pmr = (struct mldv2_record *)(mtod(md, uint8_t *) +
2490 md->m_len - nbytes);
2492 md = m_getptr(m, 0, &off);
2493 pmr = (struct mldv2_record *)(mtod(md, uint8_t *) +
2497 RB_FOREACH_SAFE(ims, ip6_msource_tree, &inm->in6m_srcs,
2499 CTR2(KTR_MLD, "%s: visit node %s", __func__,
2500 ip6_sprintf(ip6tbuf, &ims->im6s_addr));
2501 now = im6s_get_mode(inm, ims, 1);
2502 CTR2(KTR_MLD, "%s: node is %d", __func__, now);
2503 if ((now != mode) ||
2505 (!use_block_allow && mode == MCAST_UNDEFINED))) {
2506 CTR1(KTR_MLD, "%s: skip node", __func__);
2509 if (is_source_query && ims->im6s_stp == 0) {
2510 CTR1(KTR_MLD, "%s: skip unrecorded node",
2514 CTR1(KTR_MLD, "%s: append node", __func__);
2515 if (!m_append(m, sizeof(struct in6_addr),
2516 (void *)&ims->im6s_addr)) {
2519 CTR1(KTR_MLD, "%s: m_append() failed.",
2523 nbytes += sizeof(struct in6_addr);
2525 if (msrcs == m0srcs)
2528 CTR2(KTR_MLD, "%s: msrcs is %d this packet", __func__,
2530 pmr->mr_numsrc = htons(msrcs);
2531 nbytes += (msrcs * sizeof(struct in6_addr));
2534 if (is_source_query && msrcs == 0) {
2535 CTR1(KTR_MLD, "%s: no recorded sources to report", __func__);
2542 * We are good to go with first packet.
2545 CTR1(KTR_MLD, "%s: enqueueing first packet", __func__);
2546 m->m_pkthdr.PH_vt.vt_nrecs = 1;
2547 mbufq_enqueue(mq, m);
2549 m->m_pkthdr.PH_vt.vt_nrecs++;
2552 * No further work needed if no source list in packet(s).
2554 if (!record_has_sources)
2558 * Whilst sources remain to be announced, we need to allocate
2559 * a new packet and fill out as many sources as will fit.
2560 * Always try for a cluster first.
2562 while (nims != NULL) {
2563 if (mbufq_full(mq)) {
2564 CTR1(KTR_MLD, "%s: outbound queue full", __func__);
2567 m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
2569 m = m_gethdr(M_NOWAIT, MT_DATA);
2572 mld_save_context(m, ifp);
2573 md = m_getptr(m, 0, &off);
2574 pmr = (struct mldv2_record *)(mtod(md, uint8_t *) + off);
2575 CTR1(KTR_MLD, "%s: allocated next packet", __func__);
2577 if (!m_append(m, sizeof(struct mldv2_record), (void *)&mr)) {
2580 CTR1(KTR_MLD, "%s: m_append() failed.", __func__);
2583 m->m_pkthdr.PH_vt.vt_nrecs = 1;
2584 nbytes += sizeof(struct mldv2_record);
2586 m0srcs = (ifp->if_mtu - MLD_MTUSPACE -
2587 sizeof(struct mldv2_record)) / sizeof(struct in6_addr);
2590 RB_FOREACH_FROM(ims, ip6_msource_tree, nims) {
2591 CTR2(KTR_MLD, "%s: visit node %s",
2592 __func__, ip6_sprintf(ip6tbuf, &ims->im6s_addr));
2593 now = im6s_get_mode(inm, ims, 1);
2594 if ((now != mode) ||
2596 (!use_block_allow && mode == MCAST_UNDEFINED))) {
2597 CTR1(KTR_MLD, "%s: skip node", __func__);
2600 if (is_source_query && ims->im6s_stp == 0) {
2601 CTR1(KTR_MLD, "%s: skip unrecorded node",
2605 CTR1(KTR_MLD, "%s: append node", __func__);
2606 if (!m_append(m, sizeof(struct in6_addr),
2607 (void *)&ims->im6s_addr)) {
2610 CTR1(KTR_MLD, "%s: m_append() failed.",
2615 if (msrcs == m0srcs)
2618 pmr->mr_numsrc = htons(msrcs);
2619 nbytes += (msrcs * sizeof(struct in6_addr));
2621 CTR1(KTR_MLD, "%s: enqueueing next packet", __func__);
2622 mbufq_enqueue(mq, m);
2629 * Type used to mark record pass completion.
2630 * We exploit the fact we can cast to this easily from the
2631 * current filter modes on each ip_msource node.
2634 REC_NONE = 0x00, /* MCAST_UNDEFINED */
2635 REC_ALLOW = 0x01, /* MCAST_INCLUDE */
2636 REC_BLOCK = 0x02, /* MCAST_EXCLUDE */
2637 REC_FULL = REC_ALLOW | REC_BLOCK
2641 * Enqueue an MLDv2 filter list change to the given output queue.
2643 * Source list filter state is held in an RB-tree. When the filter list
2644 * for a group is changed without changing its mode, we need to compute
2645 * the deltas between T0 and T1 for each source in the filter set,
2646 * and enqueue the appropriate ALLOW_NEW/BLOCK_OLD records.
2648 * As we may potentially queue two record types, and the entire R-B tree
2649 * needs to be walked at once, we break this out into its own function
2650 * so we can generate a tightly packed queue of packets.
2652 * XXX This could be written to only use one tree walk, although that makes
2653 * serializing into the mbuf chains a bit harder. For now we do two walks
2654 * which makes things easier on us, and it may or may not be harder on
2657 * If successful the size of all data appended to the queue is returned,
2658 * otherwise an error code less than zero is returned, or zero if
2659 * no record(s) were appended.
2662 mld_v2_enqueue_filter_change(struct mbufq *mq, struct in6_multi *inm)
2664 static const int MINRECLEN =
2665 sizeof(struct mldv2_record) + sizeof(struct in6_addr);
2667 struct mldv2_record mr;
2668 struct mldv2_record *pmr;
2669 struct ip6_msource *ims, *nims;
2670 struct mbuf *m, *m0, *md;
2671 int m0srcs, nbytes, npbytes, off, rsrcs, schanged;
2673 uint8_t mode, now, then;
2674 rectype_t crt, drt, nrt;
2676 char ip6tbuf[INET6_ADDRSTRLEN];
2679 IN6_MULTI_LOCK_ASSERT();
2681 if (inm->in6m_nsrc == 0 ||
2682 (inm->in6m_st[0].iss_asm > 0 && inm->in6m_st[1].iss_asm > 0))
2685 ifp = inm->in6m_ifp; /* interface */
2686 mode = inm->in6m_st[1].iss_fmode; /* filter mode at t1 */
2687 crt = REC_NONE; /* current group record type */
2688 drt = REC_NONE; /* mask of completed group record types */
2689 nrt = REC_NONE; /* record type for current node */
2690 m0srcs = 0; /* # source which will fit in current mbuf chain */
2691 npbytes = 0; /* # of bytes appended this packet */
2692 nbytes = 0; /* # of bytes appended to group's state-change queue */
2693 rsrcs = 0; /* # sources encoded in current record */
2694 schanged = 0; /* # nodes encoded in overall filter change */
2695 nallow = 0; /* # of source entries in ALLOW_NEW */
2696 nblock = 0; /* # of source entries in BLOCK_OLD */
2697 nims = NULL; /* next tree node pointer */
2700 * For each possible filter record mode.
2701 * The first kind of source we encounter tells us which
2702 * is the first kind of record we start appending.
2703 * If a node transitioned to UNDEFINED at t1, its mode is treated
2704 * as the inverse of the group's filter mode.
2706 while (drt != REC_FULL) {
2708 m0 = mbufq_last(mq);
2710 (m0->m_pkthdr.PH_vt.vt_nrecs + 1 <=
2711 MLD_V2_REPORT_MAXRECS) &&
2712 (m0->m_pkthdr.len + MINRECLEN) <
2713 (ifp->if_mtu - MLD_MTUSPACE)) {
2715 m0srcs = (ifp->if_mtu - m0->m_pkthdr.len -
2716 sizeof(struct mldv2_record)) /
2717 sizeof(struct in6_addr);
2719 "%s: use previous packet", __func__);
2721 m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
2723 m = m_gethdr(M_NOWAIT, MT_DATA);
2726 "%s: m_get*() failed", __func__);
2729 m->m_pkthdr.PH_vt.vt_nrecs = 0;
2730 mld_save_context(m, ifp);
2731 m0srcs = (ifp->if_mtu - MLD_MTUSPACE -
2732 sizeof(struct mldv2_record)) /
2733 sizeof(struct in6_addr);
2736 "%s: allocated new packet", __func__);
2739 * Append the MLD group record header to the
2740 * current packet's data area.
2741 * Recalculate pointer to free space for next
2742 * group record, in case m_append() allocated
2743 * a new mbuf or cluster.
2745 memset(&mr, 0, sizeof(mr));
2746 mr.mr_addr = inm->in6m_addr;
2747 in6_clearscope(&mr.mr_addr);
2748 if (!m_append(m, sizeof(mr), (void *)&mr)) {
2752 "%s: m_append() failed", __func__);
2755 npbytes += sizeof(struct mldv2_record);
2757 /* new packet; offset in chain */
2758 md = m_getptr(m, npbytes -
2759 sizeof(struct mldv2_record), &off);
2760 pmr = (struct mldv2_record *)(mtod(md,
2763 /* current packet; offset from last append */
2765 pmr = (struct mldv2_record *)(mtod(md,
2766 uint8_t *) + md->m_len -
2767 sizeof(struct mldv2_record));
2770 * Begin walking the tree for this record type
2771 * pass, or continue from where we left off
2772 * previously if we had to allocate a new packet.
2773 * Only report deltas in-mode at t1.
2774 * We need not report included sources as allowed
2775 * if we are in inclusive mode on the group,
2776 * however the converse is not true.
2780 nims = RB_MIN(ip6_msource_tree,
2783 RB_FOREACH_FROM(ims, ip6_msource_tree, nims) {
2784 CTR2(KTR_MLD, "%s: visit node %s", __func__,
2785 ip6_sprintf(ip6tbuf, &ims->im6s_addr));
2786 now = im6s_get_mode(inm, ims, 1);
2787 then = im6s_get_mode(inm, ims, 0);
2788 CTR3(KTR_MLD, "%s: mode: t0 %d, t1 %d",
2789 __func__, then, now);
2792 "%s: skip unchanged", __func__);
2795 if (mode == MCAST_EXCLUDE &&
2796 now == MCAST_INCLUDE) {
2798 "%s: skip IN src on EX group",
2802 nrt = (rectype_t)now;
2803 if (nrt == REC_NONE)
2804 nrt = (rectype_t)(~mode & REC_FULL);
2805 if (schanged++ == 0) {
2807 } else if (crt != nrt)
2809 if (!m_append(m, sizeof(struct in6_addr),
2810 (void *)&ims->im6s_addr)) {
2814 "%s: m_append() failed", __func__);
2817 nallow += !!(crt == REC_ALLOW);
2818 nblock += !!(crt == REC_BLOCK);
2819 if (++rsrcs == m0srcs)
2823 * If we did not append any tree nodes on this
2824 * pass, back out of allocations.
2827 npbytes -= sizeof(struct mldv2_record);
2830 "%s: m_free(m)", __func__);
2834 "%s: m_adj(m, -mr)", __func__);
2835 m_adj(m, -((int)sizeof(
2836 struct mldv2_record)));
2840 npbytes += (rsrcs * sizeof(struct in6_addr));
2841 if (crt == REC_ALLOW)
2842 pmr->mr_type = MLD_ALLOW_NEW_SOURCES;
2843 else if (crt == REC_BLOCK)
2844 pmr->mr_type = MLD_BLOCK_OLD_SOURCES;
2845 pmr->mr_numsrc = htons(rsrcs);
2847 * Count the new group record, and enqueue this
2848 * packet if it wasn't already queued.
2850 m->m_pkthdr.PH_vt.vt_nrecs++;
2852 mbufq_enqueue(mq, m);
2854 } while (nims != NULL);
2856 crt = (~crt & REC_FULL);
2859 CTR3(KTR_MLD, "%s: queued %d ALLOW_NEW, %d BLOCK_OLD", __func__,
2866 mld_v2_merge_state_changes(struct in6_multi *inm, struct mbufq *scq)
2869 struct mbuf *m; /* pending state-change */
2870 struct mbuf *m0; /* copy of pending state-change */
2871 struct mbuf *mt; /* last state-change in packet */
2872 int docopy, domerge;
2879 IN6_MULTI_LOCK_ASSERT();
2883 * If there are further pending retransmissions, make a writable
2884 * copy of each queued state-change message before merging.
2886 if (inm->in6m_scrv > 0)
2889 gq = &inm->in6m_scq;
2891 if (mbufq_first(gq) == NULL) {
2892 CTR2(KTR_MLD, "%s: WARNING: queue for inm %p is empty",
2897 m = mbufq_first(gq);
2900 * Only merge the report into the current packet if
2901 * there is sufficient space to do so; an MLDv2 report
2902 * packet may only contain 65,535 group records.
2903 * Always use a simple mbuf chain concatentation to do this,
2904 * as large state changes for single groups may have
2905 * allocated clusters.
2908 mt = mbufq_last(scq);
2910 recslen = m_length(m, NULL);
2912 if ((mt->m_pkthdr.PH_vt.vt_nrecs +
2913 m->m_pkthdr.PH_vt.vt_nrecs <=
2914 MLD_V2_REPORT_MAXRECS) &&
2915 (mt->m_pkthdr.len + recslen <=
2916 (inm->in6m_ifp->if_mtu - MLD_MTUSPACE)))
2920 if (!domerge && mbufq_full(gq)) {
2922 "%s: outbound queue full, skipping whole packet %p",
2932 CTR2(KTR_MLD, "%s: dequeueing %p", __func__, m);
2933 m0 = mbufq_dequeue(gq);
2936 CTR2(KTR_MLD, "%s: copying %p", __func__, m);
2937 m0 = m_dup(m, M_NOWAIT);
2940 m0->m_nextpkt = NULL;
2945 CTR3(KTR_MLD, "%s: queueing %p to scq %p)",
2947 mbufq_enqueue(scq, m0);
2949 struct mbuf *mtl; /* last mbuf of packet mt */
2951 CTR3(KTR_MLD, "%s: merging %p with ifscq tail %p)",
2955 m0->m_flags &= ~M_PKTHDR;
2956 mt->m_pkthdr.len += recslen;
2957 mt->m_pkthdr.PH_vt.vt_nrecs +=
2958 m0->m_pkthdr.PH_vt.vt_nrecs;
2968 * Respond to a pending MLDv2 General Query.
2971 mld_v2_dispatch_general_query(struct mld_ifsoftc *mli)
2973 struct ifmultiaddr *ifma;
2975 struct in6_multi *inm;
2978 IN6_MULTI_LOCK_ASSERT();
2981 KASSERT(mli->mli_version == MLD_VERSION_2,
2982 ("%s: called when version %d", __func__, mli->mli_version));
2985 * Check that there are some packets queued. If so, send them first.
2986 * For large number of groups the reply to general query can take
2987 * many packets, we should finish sending them before starting of
2988 * queuing the new reply.
2990 if (mbufq_len(&mli->mli_gq) != 0)
2996 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
2997 if (ifma->ifma_addr->sa_family != AF_INET6 ||
2998 ifma->ifma_protospec == NULL)
3001 inm = (struct in6_multi *)ifma->ifma_protospec;
3002 KASSERT(ifp == inm->in6m_ifp,
3003 ("%s: inconsistent ifp", __func__));
3005 switch (inm->in6m_state) {
3006 case MLD_NOT_MEMBER:
3007 case MLD_SILENT_MEMBER:
3009 case MLD_REPORTING_MEMBER:
3010 case MLD_IDLE_MEMBER:
3011 case MLD_LAZY_MEMBER:
3012 case MLD_SLEEPING_MEMBER:
3013 case MLD_AWAKENING_MEMBER:
3014 inm->in6m_state = MLD_REPORTING_MEMBER;
3015 retval = mld_v2_enqueue_group_record(&mli->mli_gq,
3017 CTR2(KTR_MLD, "%s: enqueue record = %d",
3020 case MLD_G_QUERY_PENDING_MEMBER:
3021 case MLD_SG_QUERY_PENDING_MEMBER:
3022 case MLD_LEAVING_MEMBER:
3026 IF_ADDR_RUNLOCK(ifp);
3029 mld_dispatch_queue(&mli->mli_gq, MLD_MAX_RESPONSE_BURST);
3032 * Slew transmission of bursts over 500ms intervals.
3034 if (mbufq_first(&mli->mli_gq) != NULL) {
3035 mli->mli_v2_timer = 1 + MLD_RANDOM_DELAY(
3036 MLD_RESPONSE_BURST_INTERVAL);
3037 V_interface_timers_running6 = 1;
3042 * Transmit the next pending message in the output queue.
3044 * VIMAGE: Needs to store/restore vnet pointer on a per-mbuf-chain basis.
3045 * MRT: Nothing needs to be done, as MLD traffic is always local to
3046 * a link and uses a link-scope multicast address.
3049 mld_dispatch_packet(struct mbuf *m)
3051 struct ip6_moptions im6o;
3056 struct ip6_hdr *ip6;
3057 struct mld_hdr *mld;
3063 CTR2(KTR_MLD, "%s: transmit %p", __func__, m);
3066 * Set VNET image pointer from enqueued mbuf chain
3067 * before doing anything else. Whilst we use interface
3068 * indexes to guard against interface detach, they are
3069 * unique to each VIMAGE and must be retrieved.
3071 ifindex = mld_restore_context(m);
3074 * Check if the ifnet still exists. This limits the scope of
3075 * any race in the absence of a global ifp lock for low cost
3076 * (an array lookup).
3078 ifp = ifnet_byindex(ifindex);
3080 CTR3(KTR_MLD, "%s: dropped %p as ifindex %u went away.",
3081 __func__, m, ifindex);
3083 IP6STAT_INC(ip6s_noroute);
3087 im6o.im6o_multicast_hlim = 1;
3088 im6o.im6o_multicast_loop = (V_ip6_mrouter != NULL);
3089 im6o.im6o_multicast_ifp = ifp;
3091 if (m->m_flags & M_MLDV1) {
3094 m0 = mld_v2_encap_report(ifp, m);
3096 CTR2(KTR_MLD, "%s: dropped %p", __func__, m);
3097 IP6STAT_INC(ip6s_odropped);
3102 mld_scrub_context(m0);
3104 m0->m_pkthdr.rcvif = V_loif;
3106 ip6 = mtod(m0, struct ip6_hdr *);
3108 (void)in6_setscope(&ip6->ip6_dst, ifp, NULL); /* XXX LOR */
3111 * XXX XXX Break some KPI rules to prevent an LOR which would
3112 * occur if we called in6_setscope() at transmission.
3113 * See comments at top of file.
3115 MLD_EMBEDSCOPE(&ip6->ip6_dst, ifp->if_index);
3119 * Retrieve the ICMPv6 type before handoff to ip6_output(),
3120 * so we can bump the stats.
3122 md = m_getptr(m0, sizeof(struct ip6_hdr), &off);
3123 mld = (struct mld_hdr *)(mtod(md, uint8_t *) + off);
3124 type = mld->mld_type;
3126 error = ip6_output(m0, &mld_po, NULL, IPV6_UNSPECSRC, &im6o,
3129 CTR3(KTR_MLD, "%s: ip6_output(%p) = %d", __func__, m0, error);
3132 ICMP6STAT_INC(icp6s_outhist[type]);
3134 icmp6_ifstat_inc(oifp, ifs6_out_msg);
3136 case MLD_LISTENER_REPORT:
3137 case MLDV2_LISTENER_REPORT:
3138 icmp6_ifstat_inc(oifp, ifs6_out_mldreport);
3140 case MLD_LISTENER_DONE:
3141 icmp6_ifstat_inc(oifp, ifs6_out_mlddone);
3150 * Encapsulate an MLDv2 report.
3152 * KAME IPv6 requires that hop-by-hop options be passed separately,
3153 * and that the IPv6 header be prepended in a separate mbuf.
3155 * Returns a pointer to the new mbuf chain head, or NULL if the
3156 * allocation failed.
3158 static struct mbuf *
3159 mld_v2_encap_report(struct ifnet *ifp, struct mbuf *m)
3162 struct mldv2_report *mld;
3163 struct ip6_hdr *ip6;
3164 struct in6_ifaddr *ia;
3167 KASSERT(ifp != NULL, ("%s: null ifp", __func__));
3168 KASSERT((m->m_flags & M_PKTHDR),
3169 ("%s: mbuf chain %p is !M_PKTHDR", __func__, m));
3172 * RFC3590: OK to send as :: or tentative during DAD.
3174 ia = in6ifa_ifpforlinklocal(ifp, IN6_IFF_NOTREADY|IN6_IFF_ANYCAST);
3176 CTR1(KTR_MLD, "%s: warning: ia is NULL", __func__);
3178 mh = m_gethdr(M_NOWAIT, MT_DATA);
3181 ifa_free(&ia->ia_ifa);
3185 M_ALIGN(mh, sizeof(struct ip6_hdr) + sizeof(struct mldv2_report));
3187 mldreclen = m_length(m, NULL);
3188 CTR2(KTR_MLD, "%s: mldreclen is %d", __func__, mldreclen);
3190 mh->m_len = sizeof(struct ip6_hdr) + sizeof(struct mldv2_report);
3191 mh->m_pkthdr.len = sizeof(struct ip6_hdr) +
3192 sizeof(struct mldv2_report) + mldreclen;
3194 ip6 = mtod(mh, struct ip6_hdr *);
3196 ip6->ip6_vfc &= ~IPV6_VERSION_MASK;
3197 ip6->ip6_vfc |= IPV6_VERSION;
3198 ip6->ip6_nxt = IPPROTO_ICMPV6;
3199 ip6->ip6_src = ia ? ia->ia_addr.sin6_addr : in6addr_any;
3201 ifa_free(&ia->ia_ifa);
3202 ip6->ip6_dst = in6addr_linklocal_allv2routers;
3203 /* scope ID will be set in netisr */
3205 mld = (struct mldv2_report *)(ip6 + 1);
3206 mld->mld_type = MLDV2_LISTENER_REPORT;
3209 mld->mld_v2_reserved = 0;
3210 mld->mld_v2_numrecs = htons(m->m_pkthdr.PH_vt.vt_nrecs);
3211 m->m_pkthdr.PH_vt.vt_nrecs = 0;
3214 mld->mld_cksum = in6_cksum(mh, IPPROTO_ICMPV6,
3215 sizeof(struct ip6_hdr), sizeof(struct mldv2_report) + mldreclen);
3221 mld_rec_type_to_str(const int type)
3225 case MLD_CHANGE_TO_EXCLUDE_MODE:
3228 case MLD_CHANGE_TO_INCLUDE_MODE:
3231 case MLD_MODE_IS_EXCLUDE:
3234 case MLD_MODE_IS_INCLUDE:
3237 case MLD_ALLOW_NEW_SOURCES:
3240 case MLD_BLOCK_OLD_SOURCES:
3251 mld_init(void *unused __unused)
3254 CTR1(KTR_MLD, "%s: initializing", __func__);
3257 ip6_initpktopts(&mld_po);
3258 mld_po.ip6po_hlim = 1;
3259 mld_po.ip6po_hbh = &mld_ra.hbh;
3260 mld_po.ip6po_prefer_tempaddr = IP6PO_TEMPADDR_NOTPREFER;
3261 mld_po.ip6po_flags = IP6PO_DONTFRAG;
3263 SYSINIT(mld_init, SI_SUB_PROTO_MC, SI_ORDER_MIDDLE, mld_init, NULL);
3266 mld_uninit(void *unused __unused)
3269 CTR1(KTR_MLD, "%s: tearing down", __func__);
3272 SYSUNINIT(mld_uninit, SI_SUB_PROTO_MC, SI_ORDER_MIDDLE, mld_uninit, NULL);
3275 vnet_mld_init(const void *unused __unused)
3278 CTR1(KTR_MLD, "%s: initializing", __func__);
3280 LIST_INIT(&V_mli_head);
3282 VNET_SYSINIT(vnet_mld_init, SI_SUB_PROTO_MC, SI_ORDER_ANY, vnet_mld_init,
3286 vnet_mld_uninit(const void *unused __unused)
3289 /* This can happen if we shutdown the network stack. */
3290 CTR1(KTR_MLD, "%s: tearing down", __func__);
3292 VNET_SYSUNINIT(vnet_mld_uninit, SI_SUB_PROTO_MC, SI_ORDER_ANY, vnet_mld_uninit,
3296 mld_modevent(module_t mod, int type, void *unused __unused)
3304 return (EOPNOTSUPP);
3309 static moduledata_t mld_mod = {
3314 DECLARE_MODULE(mld, mld_mod, SI_SUB_PROTO_MC, SI_ORDER_ANY);