2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 2009 Bruce Simpson.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. The name of the author may not be used to endorse or promote
15 * products derived from this software without specific prior written
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * $KAME: mld6.c,v 1.27 2001/04/04 05:17:30 itojun Exp $
34 * Copyright (c) 1988 Stephen Deering.
35 * Copyright (c) 1992, 1993
36 * The Regents of the University of California. All rights reserved.
38 * This code is derived from software contributed to Berkeley by
39 * Stephen Deering of Stanford University.
41 * Redistribution and use in source and binary forms, with or without
42 * modification, are permitted provided that the following conditions
44 * 1. Redistributions of source code must retain the above copyright
45 * notice, this list of conditions and the following disclaimer.
46 * 2. Redistributions in binary form must reproduce the above copyright
47 * notice, this list of conditions and the following disclaimer in the
48 * documentation and/or other materials provided with the distribution.
49 * 3. Neither the name of the University nor the names of its contributors
50 * may be used to endorse or promote products derived from this software
51 * without specific prior written permission.
53 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
54 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
55 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
56 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
57 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
58 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
59 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
60 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
61 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
62 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
65 * @(#)igmp.c 8.1 (Berkeley) 7/19/93
68 #include <sys/cdefs.h>
69 __FBSDID("$FreeBSD$");
72 #include "opt_inet6.h"
74 #include <sys/param.h>
75 #include <sys/systm.h>
77 #include <sys/socket.h>
78 #include <sys/protosw.h>
79 #include <sys/sysctl.h>
80 #include <sys/kernel.h>
81 #include <sys/callout.h>
82 #include <sys/malloc.h>
83 #include <sys/module.h>
87 #include <net/if_var.h>
88 #include <net/route.h>
91 #include <netinet/in.h>
92 #include <netinet/in_var.h>
93 #include <netinet6/in6_var.h>
94 #include <netinet/ip6.h>
95 #include <netinet6/ip6_var.h>
96 #include <netinet6/scope6_var.h>
97 #include <netinet/icmp6.h>
98 #include <netinet6/mld6.h>
99 #include <netinet6/mld6_var.h>
101 #include <security/mac/mac_framework.h>
104 #define KTR_MLD KTR_INET6
107 static 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(struct in6_multi_head *inmh);
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 in6_multi_head *,
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 in6_multi_head *,
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 VNET_DEFINE_STATIC(struct timeval, mld_gsrdelay) = {10, 0};
210 VNET_DEFINE_STATIC(LIST_HEAD(, mld_ifsoftc), mli_head);
211 VNET_DEFINE_STATIC(int, interface_timers_running6);
212 VNET_DEFINE_STATIC(int, state_change_timers_running6);
213 VNET_DEFINE_STATIC(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 | CTLFLAG_MPSAFE, 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_v2enable = 1;
246 SYSCTL_INT(_net_inet6_mld, OID_AUTO, v2enable, CTLFLAG_RWTUN,
247 &mld_v2enable, 0, "Enable MLDv2");
249 static int mld_use_allow = 1;
250 SYSCTL_INT(_net_inet6_mld, OID_AUTO, use_allow, CTLFLAG_RWTUN,
251 &mld_use_allow, 0, "Use ALLOW/BLOCK for RFC 4604 SSM joins/leaves");
254 * Packed Router Alert option structure declaration.
259 struct ip6_opt_router ra;
263 * Router Alert hop-by-hop option header.
265 static struct mld_raopt mld_ra = {
267 .pad = { .ip6o_type = IP6OPT_PADN, 0 },
269 .ip6or_type = IP6OPT_ROUTER_ALERT,
270 .ip6or_len = IP6OPT_RTALERT_LEN - 2,
271 .ip6or_value[0] = ((IP6OPT_RTALERT_MLD >> 8) & 0xFF),
272 .ip6or_value[1] = (IP6OPT_RTALERT_MLD & 0xFF)
275 static struct ip6_pktopts mld_po;
278 mld_save_context(struct mbuf *m, struct ifnet *ifp)
282 m->m_pkthdr.PH_loc.ptr = ifp->if_vnet;
284 m->m_pkthdr.rcvif = ifp;
285 m->m_pkthdr.flowid = ifp->if_index;
289 mld_scrub_context(struct mbuf *m)
292 m->m_pkthdr.PH_loc.ptr = NULL;
293 m->m_pkthdr.flowid = 0;
297 * Restore context from a queued output chain.
298 * Return saved ifindex.
300 * VIMAGE: The assertion is there to make sure that we
301 * actually called CURVNET_SET() with what's in the mbuf chain.
303 static __inline uint32_t
304 mld_restore_context(struct mbuf *m)
307 #if defined(VIMAGE) && defined(INVARIANTS)
308 KASSERT(curvnet == m->m_pkthdr.PH_loc.ptr,
309 ("%s: called when curvnet was not restored: cuvnet %p m ptr %p",
310 __func__, curvnet, m->m_pkthdr.PH_loc.ptr));
312 return (m->m_pkthdr.flowid);
316 * Retrieve or set threshold between group-source queries in seconds.
318 * VIMAGE: Assume curvnet set by caller.
319 * SMPng: NOTE: Serialized by MLD lock.
322 sysctl_mld_gsr(SYSCTL_HANDLER_ARGS)
327 error = sysctl_wire_old_buffer(req, sizeof(int));
333 i = V_mld_gsrdelay.tv_sec;
335 error = sysctl_handle_int(oidp, &i, 0, req);
336 if (error || !req->newptr)
339 if (i < -1 || i >= 60) {
344 CTR2(KTR_MLD, "change mld_gsrdelay from %d to %d",
345 V_mld_gsrdelay.tv_sec, i);
346 V_mld_gsrdelay.tv_sec = i;
354 * Expose struct mld_ifsoftc to userland, keyed by ifindex.
355 * For use by ifmcstat(8).
357 * SMPng: NOTE: Does an unlocked ifindex space read.
358 * VIMAGE: Assume curvnet set by caller. The node handler itself
359 * is not directly virtualized.
362 sysctl_mld_ifinfo(SYSCTL_HANDLER_ARGS)
368 struct mld_ifsoftc *mli;
373 if (req->newptr != NULL)
379 error = sysctl_wire_old_buffer(req, sizeof(struct mld_ifinfo));
384 IN6_MULTI_LIST_LOCK();
387 if (name[0] <= 0 || name[0] > V_if_index) {
394 ifp = ifnet_byindex(name[0]);
398 LIST_FOREACH(mli, &V_mli_head, mli_link) {
399 if (ifp == mli->mli_ifp) {
400 struct mld_ifinfo info;
402 info.mli_version = mli->mli_version;
403 info.mli_v1_timer = mli->mli_v1_timer;
404 info.mli_v2_timer = mli->mli_v2_timer;
405 info.mli_flags = mli->mli_flags;
406 info.mli_rv = mli->mli_rv;
407 info.mli_qi = mli->mli_qi;
408 info.mli_qri = mli->mli_qri;
409 info.mli_uri = mli->mli_uri;
410 error = SYSCTL_OUT(req, &info, sizeof(info));
417 IN6_MULTI_LIST_UNLOCK();
423 * Dispatch an entire queue of pending packet chains.
424 * VIMAGE: Assumes the vnet pointer has been set.
427 mld_dispatch_queue(struct mbufq *mq, int limit)
431 while ((m = mbufq_dequeue(mq)) != NULL) {
432 CTR3(KTR_MLD, "%s: dispatch %p from %p", __func__, mq, m);
433 mld_dispatch_packet(m);
440 * Filter outgoing MLD report state by group.
442 * Reports are ALWAYS suppressed for ALL-HOSTS (ff02::1)
443 * and node-local addresses. However, kernel and socket consumers
444 * always embed the KAME scope ID in the address provided, so strip it
445 * when performing comparison.
446 * Note: This is not the same as the *multicast* scope.
448 * Return zero if the given group is one for which MLD reports
449 * should be suppressed, or non-zero if reports should be issued.
452 mld_is_addr_reported(const struct in6_addr *addr)
455 KASSERT(IN6_IS_ADDR_MULTICAST(addr), ("%s: not multicast", __func__));
457 if (IPV6_ADDR_MC_SCOPE(addr) == IPV6_ADDR_SCOPE_NODELOCAL)
460 if (IPV6_ADDR_MC_SCOPE(addr) == IPV6_ADDR_SCOPE_LINKLOCAL) {
461 struct in6_addr tmp = *addr;
462 in6_clearscope(&tmp);
463 if (IN6_ARE_ADDR_EQUAL(&tmp, &in6addr_linklocal_allnodes))
471 * Attach MLD when PF_INET6 is attached to an interface. Assumes that the
472 * current VNET is set by the caller.
475 mld_domifattach(struct ifnet *ifp)
477 struct mld_ifsoftc *mli;
479 CTR3(KTR_MLD, "%s: called for ifp %p(%s)", __func__, ifp, if_name(ifp));
481 mli = malloc(sizeof(struct mld_ifsoftc), M_MLD, M_WAITOK | M_ZERO);
483 mli->mli_version = MLD_VERSION_2;
485 mli->mli_rv = MLD_RV_INIT;
486 mli->mli_qi = MLD_QI_INIT;
487 mli->mli_qri = MLD_QRI_INIT;
488 mli->mli_uri = MLD_URI_INIT;
489 mbufq_init(&mli->mli_gq, MLD_MAX_RESPONSE_PACKETS);
490 if ((ifp->if_flags & IFF_MULTICAST) == 0)
491 mli->mli_flags |= MLIF_SILENT;
493 mli->mli_flags |= MLIF_USEALLOW;
496 LIST_INSERT_HEAD(&V_mli_head, mli, mli_link);
505 * NOTE: Some finalization tasks need to run before the protocol domain
506 * is detached, but also before the link layer does its cleanup.
507 * Run before link-layer cleanup; cleanup groups, but do not free MLD state.
509 * SMPng: Caller must hold IN6_MULTI_LOCK().
510 * Must take IF_ADDR_LOCK() to cover if_multiaddrs iterator.
511 * XXX This routine is also bitten by unlocked ifma_protospec access.
514 mld_ifdetach(struct ifnet *ifp, struct in6_multi_head *inmh)
516 struct epoch_tracker et;
517 struct mld_ifsoftc *mli;
518 struct ifmultiaddr *ifma;
519 struct in6_multi *inm;
521 CTR3(KTR_MLD, "%s: called for ifp %p(%s)", __func__, ifp,
524 IN6_MULTI_LIST_LOCK_ASSERT();
527 mli = MLD_IFINFO(ifp);
530 * Extract list of in6_multi associated with the detaching ifp
531 * which the PF_INET6 layer is about to release.
534 CK_STAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
535 inm = in6m_ifmultiaddr_get_inm(ifma);
538 in6m_disconnect_locked(inmh, inm);
540 if (mli->mli_version == MLD_VERSION_2) {
541 in6m_clear_recorded(inm);
544 * We need to release the final reference held
545 * for issuing the INCLUDE {}.
547 if (inm->in6m_state == MLD_LEAVING_MEMBER) {
548 inm->in6m_state = MLD_NOT_MEMBER;
549 in6m_rele_locked(inmh, inm);
554 IF_ADDR_WUNLOCK(ifp);
559 * Hook for domifdetach.
560 * Runs after link-layer cleanup; free MLD state.
562 * SMPng: Normally called with IF_AFDATA_LOCK held.
565 mld_domifdetach(struct ifnet *ifp)
568 CTR3(KTR_MLD, "%s: called for ifp %p(%s)",
569 __func__, ifp, if_name(ifp));
572 mli_delete_locked(ifp);
577 mli_delete_locked(const struct ifnet *ifp)
579 struct mld_ifsoftc *mli, *tmli;
581 CTR3(KTR_MLD, "%s: freeing mld_ifsoftc for ifp %p(%s)",
582 __func__, ifp, if_name(ifp));
586 LIST_FOREACH_SAFE(mli, &V_mli_head, mli_link, tmli) {
587 if (mli->mli_ifp == ifp) {
589 * Free deferred General Query responses.
591 mbufq_drain(&mli->mli_gq);
593 LIST_REMOVE(mli, mli_link);
602 * Process a received MLDv1 general or address-specific query.
603 * Assumes that the query header has been pulled up to sizeof(mld_hdr).
605 * NOTE: Can't be fully const correct as we temporarily embed scope ID in
606 * mld_addr. This is OK as we own the mbuf chain.
609 mld_v1_input_query(struct ifnet *ifp, const struct ip6_hdr *ip6,
610 /*const*/ struct mld_hdr *mld)
612 struct ifmultiaddr *ifma;
613 struct mld_ifsoftc *mli;
614 struct in6_multi *inm;
615 int is_general_query;
618 char ip6tbuf[INET6_ADDRSTRLEN];
623 is_general_query = 0;
626 CTR3(KTR_MLD, "ignore v1 query %s on ifp %p(%s)",
627 ip6_sprintf(ip6tbuf, &mld->mld_addr),
633 * RFC3810 Section 6.2: MLD queries must originate from
634 * a router's link-local address.
636 if (!IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_src)) {
637 CTR3(KTR_MLD, "ignore v1 query src %s on ifp %p(%s)",
638 ip6_sprintf(ip6tbuf, &ip6->ip6_src),
644 * Do address field validation upfront before we accept
647 if (IN6_IS_ADDR_UNSPECIFIED(&mld->mld_addr)) {
649 * MLDv1 General Query.
650 * If this was not sent to the all-nodes group, ignore it.
655 in6_clearscope(&dst);
656 if (!IN6_ARE_ADDR_EQUAL(&dst, &in6addr_linklocal_allnodes))
658 is_general_query = 1;
661 * Embed scope ID of receiving interface in MLD query for
662 * lookup whilst we don't hold other locks.
664 in6_setscope(&mld->mld_addr, ifp, NULL);
667 IN6_MULTI_LIST_LOCK();
671 * Switch to MLDv1 host compatibility mode.
673 mli = MLD_IFINFO(ifp);
674 KASSERT(mli != NULL, ("%s: no mld_ifsoftc for ifp %p", __func__, ifp));
675 mld_set_version(mli, MLD_VERSION_1);
677 timer = (ntohs(mld->mld_maxdelay) * PR_FASTHZ) / MLD_TIMER_SCALE;
681 if (is_general_query) {
683 * For each reporting group joined on this
684 * interface, kick the report timer.
686 CTR2(KTR_MLD, "process v1 general query on ifp %p(%s)",
688 CK_STAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
689 inm = in6m_ifmultiaddr_get_inm(ifma);
692 mld_v1_update_group(inm, timer);
696 * MLDv1 Group-Specific Query.
697 * If this is a group-specific MLDv1 query, we need only
698 * look up the single group to process it.
700 inm = in6m_lookup_locked(ifp, &mld->mld_addr);
702 CTR3(KTR_MLD, "process v1 query %s on ifp %p(%s)",
703 ip6_sprintf(ip6tbuf, &mld->mld_addr),
705 mld_v1_update_group(inm, timer);
707 /* XXX Clear embedded scope ID as userland won't expect it. */
708 in6_clearscope(&mld->mld_addr);
712 IN6_MULTI_LIST_UNLOCK();
718 * Update the report timer on a group in response to an MLDv1 query.
720 * If we are becoming the reporting member for this group, start the timer.
721 * If we already are the reporting member for this group, and timer is
722 * below the threshold, reset it.
724 * We may be updating the group for the first time since we switched
725 * to MLDv2. If we are, then we must clear any recorded source lists,
726 * and transition to REPORTING state; the group timer is overloaded
727 * for group and group-source query responses.
729 * Unlike MLDv2, the delay per group should be jittered
730 * to avoid bursts of MLDv1 reports.
733 mld_v1_update_group(struct in6_multi *inm, const int timer)
736 char ip6tbuf[INET6_ADDRSTRLEN];
739 CTR4(KTR_MLD, "%s: %s/%s timer=%d", __func__,
740 ip6_sprintf(ip6tbuf, &inm->in6m_addr),
741 if_name(inm->in6m_ifp), timer);
743 IN6_MULTI_LIST_LOCK_ASSERT();
745 switch (inm->in6m_state) {
747 case MLD_SILENT_MEMBER:
749 case MLD_REPORTING_MEMBER:
750 if (inm->in6m_timer != 0 &&
751 inm->in6m_timer <= timer) {
752 CTR1(KTR_MLD, "%s: REPORTING and timer running, "
753 "skipping.", __func__);
757 case MLD_SG_QUERY_PENDING_MEMBER:
758 case MLD_G_QUERY_PENDING_MEMBER:
759 case MLD_IDLE_MEMBER:
760 case MLD_LAZY_MEMBER:
761 case MLD_AWAKENING_MEMBER:
762 CTR1(KTR_MLD, "%s: ->REPORTING", __func__);
763 inm->in6m_state = MLD_REPORTING_MEMBER;
764 inm->in6m_timer = MLD_RANDOM_DELAY(timer);
765 V_current_state_timers_running6 = 1;
767 case MLD_SLEEPING_MEMBER:
768 CTR1(KTR_MLD, "%s: ->AWAKENING", __func__);
769 inm->in6m_state = MLD_AWAKENING_MEMBER;
771 case MLD_LEAVING_MEMBER:
777 * Process a received MLDv2 general, group-specific or
778 * group-and-source-specific query.
780 * Assumes that mld points to a struct mldv2_query which is stored in
783 * Return 0 if successful, otherwise an appropriate error code is returned.
786 mld_v2_input_query(struct ifnet *ifp, const struct ip6_hdr *ip6,
787 struct mbuf *m, struct mldv2_query *mld, const int off, const int icmp6len)
789 struct mld_ifsoftc *mli;
790 struct in6_multi *inm;
791 uint32_t maxdelay, nsrc, qqi;
792 int is_general_query;
796 char ip6tbuf[INET6_ADDRSTRLEN];
802 CTR3(KTR_MLD, "ignore v2 query src %s on ifp %p(%s)",
803 ip6_sprintf(ip6tbuf, &ip6->ip6_src),
809 * RFC3810 Section 6.2: MLD queries must originate from
810 * a router's link-local address.
812 if (!IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_src)) {
813 CTR3(KTR_MLD, "ignore v1 query src %s on ifp %p(%s)",
814 ip6_sprintf(ip6tbuf, &ip6->ip6_src),
819 is_general_query = 0;
821 CTR2(KTR_MLD, "input v2 query on ifp %p(%s)", ifp, if_name(ifp));
823 maxdelay = ntohs(mld->mld_maxdelay); /* in 1/10ths of a second */
824 if (maxdelay >= 32768) {
825 maxdelay = (MLD_MRC_MANT(maxdelay) | 0x1000) <<
826 (MLD_MRC_EXP(maxdelay) + 3);
828 timer = (maxdelay * PR_FASTHZ) / MLD_TIMER_SCALE;
832 qrv = MLD_QRV(mld->mld_misc);
834 CTR3(KTR_MLD, "%s: clamping qrv %d to %d", __func__,
841 qqi = MLD_QQIC_MANT(mld->mld_qqi) <<
842 (MLD_QQIC_EXP(mld->mld_qqi) + 3);
845 nsrc = ntohs(mld->mld_numsrc);
846 if (nsrc > MLD_MAX_GS_SOURCES)
848 if (icmp6len < sizeof(struct mldv2_query) +
849 (nsrc * sizeof(struct in6_addr)))
853 * Do further input validation upfront to avoid resetting timers
854 * should we need to discard this query.
856 if (IN6_IS_ADDR_UNSPECIFIED(&mld->mld_addr)) {
858 * A general query with a source list has undefined
859 * behaviour; discard it.
863 is_general_query = 1;
866 * Embed scope ID of receiving interface in MLD query for
867 * lookup whilst we don't hold other locks (due to KAME
868 * locking lameness). We own this mbuf chain just now.
870 in6_setscope(&mld->mld_addr, ifp, NULL);
873 IN6_MULTI_LIST_LOCK();
876 mli = MLD_IFINFO(ifp);
877 KASSERT(mli != NULL, ("%s: no mld_ifsoftc for ifp %p", __func__, ifp));
880 * Discard the v2 query if we're in Compatibility Mode.
881 * The RFC is pretty clear that hosts need to stay in MLDv1 mode
882 * until the Old Version Querier Present timer expires.
884 if (mli->mli_version != MLD_VERSION_2)
887 mld_set_version(mli, MLD_VERSION_2);
890 mli->mli_qri = maxdelay;
892 CTR4(KTR_MLD, "%s: qrv %d qi %d maxdelay %d", __func__, qrv, qqi,
895 if (is_general_query) {
897 * MLDv2 General Query.
899 * Schedule a current-state report on this ifp for
900 * all groups, possibly containing source lists.
902 * If there is a pending General Query response
903 * scheduled earlier than the selected delay, do
904 * not schedule any other reports.
905 * Otherwise, reset the interface timer.
907 CTR2(KTR_MLD, "process v2 general query on ifp %p(%s)",
909 if (mli->mli_v2_timer == 0 || mli->mli_v2_timer >= timer) {
910 mli->mli_v2_timer = MLD_RANDOM_DELAY(timer);
911 V_interface_timers_running6 = 1;
915 * MLDv2 Group-specific or Group-and-source-specific Query.
917 * Group-source-specific queries are throttled on
918 * a per-group basis to defeat denial-of-service attempts.
919 * Queries for groups we are not a member of on this
920 * link are simply ignored.
922 inm = in6m_lookup_locked(ifp, &mld->mld_addr);
926 if (!ratecheck(&inm->in6m_lastgsrtv,
928 CTR1(KTR_MLD, "%s: GS query throttled.",
933 CTR2(KTR_MLD, "process v2 group query on ifp %p(%s)",
936 * If there is a pending General Query response
937 * scheduled sooner than the selected delay, no
938 * further report need be scheduled.
939 * Otherwise, prepare to respond to the
940 * group-specific or group-and-source query.
942 if (mli->mli_v2_timer == 0 || mli->mli_v2_timer >= timer)
943 mld_v2_process_group_query(inm, mli, timer, m, mld, off);
945 /* XXX Clear embedded scope ID as userland won't expect it. */
946 in6_clearscope(&mld->mld_addr);
951 IN6_MULTI_LIST_UNLOCK();
957 * Process a received MLDv2 group-specific or group-and-source-specific
959 * Return <0 if any error occurred. Currently this is ignored.
962 mld_v2_process_group_query(struct in6_multi *inm, struct mld_ifsoftc *mli,
963 int timer, struct mbuf *m0, struct mldv2_query *mld, const int off)
968 IN6_MULTI_LIST_LOCK_ASSERT();
973 switch (inm->in6m_state) {
975 case MLD_SILENT_MEMBER:
976 case MLD_SLEEPING_MEMBER:
977 case MLD_LAZY_MEMBER:
978 case MLD_AWAKENING_MEMBER:
979 case MLD_IDLE_MEMBER:
980 case MLD_LEAVING_MEMBER:
983 case MLD_REPORTING_MEMBER:
984 case MLD_G_QUERY_PENDING_MEMBER:
985 case MLD_SG_QUERY_PENDING_MEMBER:
989 nsrc = ntohs(mld->mld_numsrc);
991 /* Length should be checked by calling function. */
992 KASSERT((m0->m_flags & M_PKTHDR) == 0 ||
993 m0->m_pkthdr.len >= off + sizeof(struct mldv2_query) +
994 nsrc * sizeof(struct in6_addr),
995 ("mldv2 packet is too short: (%d bytes < %zd bytes, m=%p)",
996 m0->m_pkthdr.len, off + sizeof(struct mldv2_query) +
997 nsrc * sizeof(struct in6_addr), m0));
1000 * Deal with group-specific queries upfront.
1001 * If any group query is already pending, purge any recorded
1002 * source-list state if it exists, and schedule a query response
1003 * for this group-specific query.
1006 if (inm->in6m_state == MLD_G_QUERY_PENDING_MEMBER ||
1007 inm->in6m_state == MLD_SG_QUERY_PENDING_MEMBER) {
1008 in6m_clear_recorded(inm);
1009 timer = min(inm->in6m_timer, timer);
1011 inm->in6m_state = MLD_G_QUERY_PENDING_MEMBER;
1012 inm->in6m_timer = MLD_RANDOM_DELAY(timer);
1013 V_current_state_timers_running6 = 1;
1018 * Deal with the case where a group-and-source-specific query has
1019 * been received but a group-specific query is already pending.
1021 if (inm->in6m_state == MLD_G_QUERY_PENDING_MEMBER) {
1022 timer = min(inm->in6m_timer, timer);
1023 inm->in6m_timer = MLD_RANDOM_DELAY(timer);
1024 V_current_state_timers_running6 = 1;
1029 * Finally, deal with the case where a group-and-source-specific
1030 * query has been received, where a response to a previous g-s-r
1031 * query exists, or none exists.
1032 * In this case, we need to parse the source-list which the Querier
1033 * has provided us with and check if we have any source list filter
1034 * entries at T1 for these sources. If we do not, there is no need
1035 * schedule a report and the query may be dropped.
1036 * If we do, we must record them and schedule a current-state
1037 * report for those sources.
1039 if (inm->in6m_nsrc > 0) {
1040 struct in6_addr srcaddr;
1044 soff = off + sizeof(struct mldv2_query);
1046 for (i = 0; i < nsrc; i++) {
1047 m_copydata(m0, soff, sizeof(struct in6_addr),
1049 retval = in6m_record_source(inm, &srcaddr);
1052 nrecorded += retval;
1053 soff += sizeof(struct in6_addr);
1055 if (nrecorded > 0) {
1057 "%s: schedule response to SG query", __func__);
1058 inm->in6m_state = MLD_SG_QUERY_PENDING_MEMBER;
1059 inm->in6m_timer = MLD_RANDOM_DELAY(timer);
1060 V_current_state_timers_running6 = 1;
1068 * Process a received MLDv1 host membership report.
1069 * Assumes mld points to mld_hdr in pulled up mbuf chain.
1071 * NOTE: Can't be fully const correct as we temporarily embed scope ID in
1072 * mld_addr. This is OK as we own the mbuf chain.
1075 mld_v1_input_report(struct ifnet *ifp, const struct ip6_hdr *ip6,
1076 /*const*/ struct mld_hdr *mld)
1078 struct in6_addr src, dst;
1079 struct in6_ifaddr *ia;
1080 struct in6_multi *inm;
1082 char ip6tbuf[INET6_ADDRSTRLEN];
1087 if (!mld_v1enable) {
1088 CTR3(KTR_MLD, "ignore v1 report %s on ifp %p(%s)",
1089 ip6_sprintf(ip6tbuf, &mld->mld_addr),
1094 if (ifp->if_flags & IFF_LOOPBACK)
1098 * MLDv1 reports must originate from a host's link-local address,
1099 * or the unspecified address (when booting).
1102 in6_clearscope(&src);
1103 if (!IN6_IS_SCOPE_LINKLOCAL(&src) && !IN6_IS_ADDR_UNSPECIFIED(&src)) {
1104 CTR3(KTR_MLD, "ignore v1 query src %s on ifp %p(%s)",
1105 ip6_sprintf(ip6tbuf, &ip6->ip6_src),
1111 * RFC2710 Section 4: MLDv1 reports must pertain to a multicast
1112 * group, and must be directed to the group itself.
1115 in6_clearscope(&dst);
1116 if (!IN6_IS_ADDR_MULTICAST(&mld->mld_addr) ||
1117 !IN6_ARE_ADDR_EQUAL(&mld->mld_addr, &dst)) {
1118 CTR3(KTR_MLD, "ignore v1 query dst %s on ifp %p(%s)",
1119 ip6_sprintf(ip6tbuf, &ip6->ip6_dst),
1125 * Make sure we don't hear our own membership report, as fast
1126 * leave requires knowing that we are the only member of a
1127 * group. Assume we used the link-local address if available,
1128 * otherwise look for ::.
1130 * XXX Note that scope ID comparison is needed for the address
1131 * returned by in6ifa_ifpforlinklocal(), but SHOULD NOT be
1132 * performed for the on-wire address.
1134 ia = in6ifa_ifpforlinklocal(ifp, IN6_IFF_NOTREADY|IN6_IFF_ANYCAST);
1135 if ((ia && IN6_ARE_ADDR_EQUAL(&ip6->ip6_src, IA6_IN6(ia))) ||
1136 (ia == NULL && IN6_IS_ADDR_UNSPECIFIED(&src))) {
1138 ifa_free(&ia->ia_ifa);
1142 ifa_free(&ia->ia_ifa);
1144 CTR3(KTR_MLD, "process v1 report %s on ifp %p(%s)",
1145 ip6_sprintf(ip6tbuf, &mld->mld_addr), ifp, if_name(ifp));
1148 * Embed scope ID of receiving interface in MLD query for lookup
1149 * whilst we don't hold other locks (due to KAME locking lameness).
1151 if (!IN6_IS_ADDR_UNSPECIFIED(&mld->mld_addr))
1152 in6_setscope(&mld->mld_addr, ifp, NULL);
1154 IN6_MULTI_LIST_LOCK();
1158 * MLDv1 report suppression.
1159 * If we are a member of this group, and our membership should be
1160 * reported, and our group timer is pending or about to be reset,
1161 * stop our group timer by transitioning to the 'lazy' state.
1163 inm = in6m_lookup_locked(ifp, &mld->mld_addr);
1165 struct mld_ifsoftc *mli;
1167 mli = inm->in6m_mli;
1168 KASSERT(mli != NULL,
1169 ("%s: no mli for ifp %p", __func__, ifp));
1172 * If we are in MLDv2 host mode, do not allow the
1173 * other host's MLDv1 report to suppress our reports.
1175 if (mli->mli_version == MLD_VERSION_2)
1178 inm->in6m_timer = 0;
1180 switch (inm->in6m_state) {
1181 case MLD_NOT_MEMBER:
1182 case MLD_SILENT_MEMBER:
1183 case MLD_SLEEPING_MEMBER:
1185 case MLD_REPORTING_MEMBER:
1186 case MLD_IDLE_MEMBER:
1187 case MLD_AWAKENING_MEMBER:
1189 "report suppressed for %s on ifp %p(%s)",
1190 ip6_sprintf(ip6tbuf, &mld->mld_addr),
1192 case MLD_LAZY_MEMBER:
1193 inm->in6m_state = MLD_LAZY_MEMBER;
1195 case MLD_G_QUERY_PENDING_MEMBER:
1196 case MLD_SG_QUERY_PENDING_MEMBER:
1197 case MLD_LEAVING_MEMBER:
1204 IN6_MULTI_LIST_UNLOCK();
1206 /* XXX Clear embedded scope ID as userland won't expect it. */
1207 in6_clearscope(&mld->mld_addr);
1215 * Assume query messages which fit in a single ICMPv6 message header
1216 * have been pulled up.
1217 * Assume that userland will want to see the message, even if it
1218 * otherwise fails kernel input validation; do not free it.
1219 * Pullup may however free the mbuf chain m if it fails.
1221 * Return IPPROTO_DONE if we freed m. Otherwise, return 0.
1224 mld_input(struct mbuf **mp, int off, int icmp6len)
1227 struct ip6_hdr *ip6;
1229 struct mld_hdr *mld;
1233 CTR3(KTR_MLD, "%s: called w/mbuf (%p,%d)", __func__, m, off);
1235 ifp = m->m_pkthdr.rcvif;
1237 /* Pullup to appropriate size. */
1238 if (m->m_len < off + sizeof(*mld)) {
1239 m = m_pullup(m, off + sizeof(*mld));
1241 ICMP6STAT_INC(icp6s_badlen);
1242 return (IPPROTO_DONE);
1245 mld = (struct mld_hdr *)(mtod(m, uint8_t *) + off);
1246 if (mld->mld_type == MLD_LISTENER_QUERY &&
1247 icmp6len >= sizeof(struct mldv2_query)) {
1248 mldlen = sizeof(struct mldv2_query);
1250 mldlen = sizeof(struct mld_hdr);
1252 if (m->m_len < off + mldlen) {
1253 m = m_pullup(m, off + mldlen);
1255 ICMP6STAT_INC(icp6s_badlen);
1256 return (IPPROTO_DONE);
1260 ip6 = mtod(m, struct ip6_hdr *);
1261 mld = (struct mld_hdr *)(mtod(m, uint8_t *) + off);
1264 * Userland needs to see all of this traffic for implementing
1265 * the endpoint discovery portion of multicast routing.
1267 switch (mld->mld_type) {
1268 case MLD_LISTENER_QUERY:
1269 icmp6_ifstat_inc(ifp, ifs6_in_mldquery);
1270 if (icmp6len == sizeof(struct mld_hdr)) {
1271 if (mld_v1_input_query(ifp, ip6, mld) != 0)
1273 } else if (icmp6len >= sizeof(struct mldv2_query)) {
1274 if (mld_v2_input_query(ifp, ip6, m,
1275 (struct mldv2_query *)mld, off, icmp6len) != 0)
1279 case MLD_LISTENER_REPORT:
1280 icmp6_ifstat_inc(ifp, ifs6_in_mldreport);
1281 if (mld_v1_input_report(ifp, ip6, mld) != 0)
1284 case MLDV2_LISTENER_REPORT:
1285 icmp6_ifstat_inc(ifp, ifs6_in_mldreport);
1287 case MLD_LISTENER_DONE:
1288 icmp6_ifstat_inc(ifp, ifs6_in_mlddone);
1298 * Fast timeout handler (global).
1299 * VIMAGE: Timeout handlers are expected to service all vimages.
1304 struct in6_multi_head inmh;
1305 VNET_ITERATOR_DECL(vnet_iter);
1309 VNET_LIST_RLOCK_NOSLEEP();
1310 VNET_FOREACH(vnet_iter) {
1311 CURVNET_SET(vnet_iter);
1312 mld_fasttimo_vnet(&inmh);
1315 VNET_LIST_RUNLOCK_NOSLEEP();
1316 in6m_release_list_deferred(&inmh);
1320 * Fast timeout handler (per-vnet).
1322 * VIMAGE: Assume caller has set up our curvnet.
1325 mld_fasttimo_vnet(struct in6_multi_head *inmh)
1327 struct epoch_tracker et;
1328 struct mbufq scq; /* State-change packets */
1329 struct mbufq qrq; /* Query response packets */
1331 struct mld_ifsoftc *mli;
1332 struct ifmultiaddr *ifma;
1333 struct in6_multi *inm;
1339 * Quick check to see if any work needs to be done, in order to
1340 * minimize the overhead of fasttimo processing.
1341 * SMPng: XXX Unlocked reads.
1343 if (!V_current_state_timers_running6 &&
1344 !V_interface_timers_running6 &&
1345 !V_state_change_timers_running6)
1348 IN6_MULTI_LIST_LOCK();
1352 * MLDv2 General Query response timer processing.
1354 if (V_interface_timers_running6) {
1355 CTR1(KTR_MLD, "%s: interface timers running", __func__);
1357 V_interface_timers_running6 = 0;
1358 LIST_FOREACH(mli, &V_mli_head, mli_link) {
1359 if (mli->mli_v2_timer == 0) {
1361 } else if (--mli->mli_v2_timer == 0) {
1362 mld_v2_dispatch_general_query(mli);
1364 V_interface_timers_running6 = 1;
1369 if (!V_current_state_timers_running6 &&
1370 !V_state_change_timers_running6)
1373 V_current_state_timers_running6 = 0;
1374 V_state_change_timers_running6 = 0;
1376 CTR1(KTR_MLD, "%s: state change timers running", __func__);
1379 * MLD host report and state-change timer processing.
1380 * Note: Processing a v2 group timer may remove a node.
1382 LIST_FOREACH(mli, &V_mli_head, mli_link) {
1385 if (mli->mli_version == MLD_VERSION_2) {
1386 uri_fasthz = MLD_RANDOM_DELAY(mli->mli_uri *
1388 mbufq_init(&qrq, MLD_MAX_G_GS_PACKETS);
1389 mbufq_init(&scq, MLD_MAX_STATE_CHANGE_PACKETS);
1392 NET_EPOCH_ENTER(et);
1394 CK_STAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
1395 inm = in6m_ifmultiaddr_get_inm(ifma);
1398 switch (mli->mli_version) {
1400 mld_v1_process_group_timer(inmh, inm);
1403 mld_v2_process_group_timers(inmh, &qrq,
1404 &scq, inm, uri_fasthz);
1408 IF_ADDR_WUNLOCK(ifp);
1410 switch (mli->mli_version) {
1413 * Transmit reports for this lifecycle. This
1414 * is done while not holding IF_ADDR_LOCK
1415 * since this can call
1416 * in6ifa_ifpforlinklocal() which locks
1417 * IF_ADDR_LOCK internally as well as
1418 * ip6_output() to transmit a packet.
1420 while ((inm = SLIST_FIRST(inmh)) != NULL) {
1421 SLIST_REMOVE_HEAD(inmh, in6m_defer);
1422 (void)mld_v1_transmit_report(inm,
1423 MLD_LISTENER_REPORT);
1427 mld_dispatch_queue(&qrq, 0);
1428 mld_dispatch_queue(&scq, 0);
1436 IN6_MULTI_LIST_UNLOCK();
1440 * Update host report group timer.
1441 * Will update the global pending timer flags.
1444 mld_v1_process_group_timer(struct in6_multi_head *inmh, struct in6_multi *inm)
1446 int report_timer_expired;
1448 IN6_MULTI_LIST_LOCK_ASSERT();
1451 if (inm->in6m_timer == 0) {
1452 report_timer_expired = 0;
1453 } else if (--inm->in6m_timer == 0) {
1454 report_timer_expired = 1;
1456 V_current_state_timers_running6 = 1;
1460 switch (inm->in6m_state) {
1461 case MLD_NOT_MEMBER:
1462 case MLD_SILENT_MEMBER:
1463 case MLD_IDLE_MEMBER:
1464 case MLD_LAZY_MEMBER:
1465 case MLD_SLEEPING_MEMBER:
1466 case MLD_AWAKENING_MEMBER:
1468 case MLD_REPORTING_MEMBER:
1469 if (report_timer_expired) {
1470 inm->in6m_state = MLD_IDLE_MEMBER;
1471 SLIST_INSERT_HEAD(inmh, inm, in6m_defer);
1474 case MLD_G_QUERY_PENDING_MEMBER:
1475 case MLD_SG_QUERY_PENDING_MEMBER:
1476 case MLD_LEAVING_MEMBER:
1482 * Update a group's timers for MLDv2.
1483 * Will update the global pending timer flags.
1484 * Note: Unlocked read from mli.
1487 mld_v2_process_group_timers(struct in6_multi_head *inmh,
1488 struct mbufq *qrq, struct mbufq *scq,
1489 struct in6_multi *inm, const int uri_fasthz)
1491 int query_response_timer_expired;
1492 int state_change_retransmit_timer_expired;
1494 char ip6tbuf[INET6_ADDRSTRLEN];
1497 IN6_MULTI_LIST_LOCK_ASSERT();
1500 query_response_timer_expired = 0;
1501 state_change_retransmit_timer_expired = 0;
1504 * During a transition from compatibility mode back to MLDv2,
1505 * a group record in REPORTING state may still have its group
1506 * timer active. This is a no-op in this function; it is easier
1507 * to deal with it here than to complicate the slow-timeout path.
1509 if (inm->in6m_timer == 0) {
1510 query_response_timer_expired = 0;
1511 } else if (--inm->in6m_timer == 0) {
1512 query_response_timer_expired = 1;
1514 V_current_state_timers_running6 = 1;
1517 if (inm->in6m_sctimer == 0) {
1518 state_change_retransmit_timer_expired = 0;
1519 } else if (--inm->in6m_sctimer == 0) {
1520 state_change_retransmit_timer_expired = 1;
1522 V_state_change_timers_running6 = 1;
1525 /* We are in fasttimo, so be quick about it. */
1526 if (!state_change_retransmit_timer_expired &&
1527 !query_response_timer_expired)
1530 switch (inm->in6m_state) {
1531 case MLD_NOT_MEMBER:
1532 case MLD_SILENT_MEMBER:
1533 case MLD_SLEEPING_MEMBER:
1534 case MLD_LAZY_MEMBER:
1535 case MLD_AWAKENING_MEMBER:
1536 case MLD_IDLE_MEMBER:
1538 case MLD_G_QUERY_PENDING_MEMBER:
1539 case MLD_SG_QUERY_PENDING_MEMBER:
1541 * Respond to a previously pending Group-Specific
1542 * or Group-and-Source-Specific query by enqueueing
1543 * the appropriate Current-State report for
1544 * immediate transmission.
1546 if (query_response_timer_expired) {
1549 retval = mld_v2_enqueue_group_record(qrq, inm, 0, 1,
1550 (inm->in6m_state == MLD_SG_QUERY_PENDING_MEMBER),
1552 CTR2(KTR_MLD, "%s: enqueue record = %d",
1554 inm->in6m_state = MLD_REPORTING_MEMBER;
1555 in6m_clear_recorded(inm);
1558 case MLD_REPORTING_MEMBER:
1559 case MLD_LEAVING_MEMBER:
1560 if (state_change_retransmit_timer_expired) {
1562 * State-change retransmission timer fired.
1563 * If there are any further pending retransmissions,
1564 * set the global pending state-change flag, and
1567 if (--inm->in6m_scrv > 0) {
1568 inm->in6m_sctimer = uri_fasthz;
1569 V_state_change_timers_running6 = 1;
1572 * Retransmit the previously computed state-change
1573 * report. If there are no further pending
1574 * retransmissions, the mbuf queue will be consumed.
1575 * Update T0 state to T1 as we have now sent
1578 (void)mld_v2_merge_state_changes(inm, scq);
1581 CTR3(KTR_MLD, "%s: T1 -> T0 for %s/%s", __func__,
1582 ip6_sprintf(ip6tbuf, &inm->in6m_addr),
1583 if_name(inm->in6m_ifp));
1586 * If we are leaving the group for good, make sure
1587 * we release MLD's reference to it.
1588 * This release must be deferred using a SLIST,
1589 * as we are called from a loop which traverses
1590 * the in_ifmultiaddr TAILQ.
1592 if (inm->in6m_state == MLD_LEAVING_MEMBER &&
1593 inm->in6m_scrv == 0) {
1594 inm->in6m_state = MLD_NOT_MEMBER;
1595 in6m_disconnect_locked(inmh, inm);
1596 in6m_rele_locked(inmh, inm);
1604 * Switch to a different version on the given interface,
1605 * as per Section 9.12.
1608 mld_set_version(struct mld_ifsoftc *mli, const int version)
1610 int old_version_timer;
1614 CTR4(KTR_MLD, "%s: switching to v%d on ifp %p(%s)", __func__,
1615 version, mli->mli_ifp, if_name(mli->mli_ifp));
1617 if (version == MLD_VERSION_1) {
1619 * Compute the "Older Version Querier Present" timer as per
1622 old_version_timer = (mli->mli_rv * mli->mli_qi) + mli->mli_qri;
1623 old_version_timer *= PR_SLOWHZ;
1624 mli->mli_v1_timer = old_version_timer;
1627 if (mli->mli_v1_timer > 0 && mli->mli_version != MLD_VERSION_1) {
1628 mli->mli_version = MLD_VERSION_1;
1629 mld_v2_cancel_link_timers(mli);
1634 * Cancel pending MLDv2 timers for the given link and all groups
1635 * joined on it; state-change, general-query, and group-query timers.
1638 mld_v2_cancel_link_timers(struct mld_ifsoftc *mli)
1640 struct epoch_tracker et;
1641 struct in6_multi_head inmh;
1642 struct ifmultiaddr *ifma;
1644 struct in6_multi *inm;
1646 CTR3(KTR_MLD, "%s: cancel v2 timers on ifp %p(%s)", __func__,
1647 mli->mli_ifp, if_name(mli->mli_ifp));
1650 IN6_MULTI_LIST_LOCK_ASSERT();
1654 * Fast-track this potentially expensive operation
1655 * by checking all the global 'timer pending' flags.
1657 if (!V_interface_timers_running6 &&
1658 !V_state_change_timers_running6 &&
1659 !V_current_state_timers_running6)
1662 mli->mli_v2_timer = 0;
1667 NET_EPOCH_ENTER(et);
1668 CK_STAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
1669 inm = in6m_ifmultiaddr_get_inm(ifma);
1672 switch (inm->in6m_state) {
1673 case MLD_NOT_MEMBER:
1674 case MLD_SILENT_MEMBER:
1675 case MLD_IDLE_MEMBER:
1676 case MLD_LAZY_MEMBER:
1677 case MLD_SLEEPING_MEMBER:
1678 case MLD_AWAKENING_MEMBER:
1680 case MLD_LEAVING_MEMBER:
1682 * If we are leaving the group and switching
1683 * version, we need to release the final
1684 * reference held for issuing the INCLUDE {}.
1686 if (inm->in6m_refcount == 1)
1687 in6m_disconnect_locked(&inmh, inm);
1688 in6m_rele_locked(&inmh, inm);
1690 case MLD_G_QUERY_PENDING_MEMBER:
1691 case MLD_SG_QUERY_PENDING_MEMBER:
1692 in6m_clear_recorded(inm);
1694 case MLD_REPORTING_MEMBER:
1695 inm->in6m_sctimer = 0;
1696 inm->in6m_timer = 0;
1697 inm->in6m_state = MLD_REPORTING_MEMBER;
1699 * Free any pending MLDv2 state-change records.
1701 mbufq_drain(&inm->in6m_scq);
1706 IF_ADDR_WUNLOCK(ifp);
1707 in6m_release_list_deferred(&inmh);
1711 * Global slowtimo handler.
1712 * VIMAGE: Timeout handlers are expected to service all vimages.
1717 VNET_ITERATOR_DECL(vnet_iter);
1719 VNET_LIST_RLOCK_NOSLEEP();
1720 VNET_FOREACH(vnet_iter) {
1721 CURVNET_SET(vnet_iter);
1722 mld_slowtimo_vnet();
1725 VNET_LIST_RUNLOCK_NOSLEEP();
1729 * Per-vnet slowtimo handler.
1732 mld_slowtimo_vnet(void)
1734 struct mld_ifsoftc *mli;
1738 LIST_FOREACH(mli, &V_mli_head, mli_link) {
1739 mld_v1_process_querier_timers(mli);
1746 * Update the Older Version Querier Present timers for a link.
1747 * See Section 9.12 of RFC 3810.
1750 mld_v1_process_querier_timers(struct mld_ifsoftc *mli)
1755 if (mli->mli_version != MLD_VERSION_2 && --mli->mli_v1_timer == 0) {
1757 * MLDv1 Querier Present timer expired; revert to MLDv2.
1760 "%s: transition from v%d -> v%d on %p(%s)",
1761 __func__, mli->mli_version, MLD_VERSION_2,
1762 mli->mli_ifp, if_name(mli->mli_ifp));
1763 mli->mli_version = MLD_VERSION_2;
1768 * Transmit an MLDv1 report immediately.
1771 mld_v1_transmit_report(struct in6_multi *in6m, const int type)
1774 struct in6_ifaddr *ia;
1775 struct ip6_hdr *ip6;
1776 struct mbuf *mh, *md;
1777 struct mld_hdr *mld;
1780 IN6_MULTI_LIST_LOCK_ASSERT();
1783 ifp = in6m->in6m_ifp;
1784 /* in process of being freed */
1787 ia = in6ifa_ifpforlinklocal(ifp, IN6_IFF_NOTREADY|IN6_IFF_ANYCAST);
1788 /* ia may be NULL if link-local address is tentative. */
1790 mh = m_gethdr(M_NOWAIT, MT_DATA);
1793 ifa_free(&ia->ia_ifa);
1796 md = m_get(M_NOWAIT, MT_DATA);
1800 ifa_free(&ia->ia_ifa);
1806 * FUTURE: Consider increasing alignment by ETHER_HDR_LEN, so
1807 * that ether_output() does not need to allocate another mbuf
1808 * for the header in the most common case.
1810 M_ALIGN(mh, sizeof(struct ip6_hdr));
1811 mh->m_pkthdr.len = sizeof(struct ip6_hdr) + sizeof(struct mld_hdr);
1812 mh->m_len = sizeof(struct ip6_hdr);
1814 ip6 = mtod(mh, struct ip6_hdr *);
1816 ip6->ip6_vfc &= ~IPV6_VERSION_MASK;
1817 ip6->ip6_vfc |= IPV6_VERSION;
1818 ip6->ip6_nxt = IPPROTO_ICMPV6;
1819 ip6->ip6_src = ia ? ia->ia_addr.sin6_addr : in6addr_any;
1820 ip6->ip6_dst = in6m->in6m_addr;
1822 md->m_len = sizeof(struct mld_hdr);
1823 mld = mtod(md, struct mld_hdr *);
1824 mld->mld_type = type;
1827 mld->mld_maxdelay = 0;
1828 mld->mld_reserved = 0;
1829 mld->mld_addr = in6m->in6m_addr;
1830 in6_clearscope(&mld->mld_addr);
1831 mld->mld_cksum = in6_cksum(mh, IPPROTO_ICMPV6,
1832 sizeof(struct ip6_hdr), sizeof(struct mld_hdr));
1834 mld_save_context(mh, ifp);
1835 mh->m_flags |= M_MLDV1;
1837 mld_dispatch_packet(mh);
1840 ifa_free(&ia->ia_ifa);
1845 * Process a state change from the upper layer for the given IPv6 group.
1847 * Each socket holds a reference on the in_multi in its own ip_moptions.
1848 * The socket layer will have made the necessary updates to.the group
1849 * state, it is now up to MLD to issue a state change report if there
1850 * has been any change between T0 (when the last state-change was issued)
1853 * We use the MLDv2 state machine at group level. The MLd module
1854 * however makes the decision as to which MLD protocol version to speak.
1855 * A state change *from* INCLUDE {} always means an initial join.
1856 * A state change *to* INCLUDE {} always means a final leave.
1858 * If delay is non-zero, and the state change is an initial multicast
1859 * join, the state change report will be delayed by 'delay' ticks
1860 * in units of PR_FASTHZ if MLDv1 is active on the link; otherwise
1861 * the initial MLDv2 state change report will be delayed by whichever
1862 * is sooner, a pending state-change timer or delay itself.
1864 * VIMAGE: curvnet should have been set by caller, as this routine
1865 * is called from the socket option handlers.
1868 mld_change_state(struct in6_multi *inm, const int delay)
1870 struct mld_ifsoftc *mli;
1874 IN6_MULTI_LIST_LOCK_ASSERT();
1879 * Check if the in6_multi has already been disconnected.
1881 if (inm->in6m_ifp == NULL) {
1882 CTR1(KTR_MLD, "%s: inm is disconnected", __func__);
1887 * Try to detect if the upper layer just asked us to change state
1888 * for an interface which has now gone away.
1890 KASSERT(inm->in6m_ifma != NULL, ("%s: no ifma", __func__));
1891 ifp = inm->in6m_ifma->ifma_ifp;
1895 * Sanity check that netinet6's notion of ifp is the
1898 KASSERT(inm->in6m_ifp == ifp, ("%s: bad ifp", __func__));
1901 mli = MLD_IFINFO(ifp);
1902 KASSERT(mli != NULL, ("%s: no mld_ifsoftc for ifp %p", __func__, ifp));
1905 * If we detect a state transition to or from MCAST_UNDEFINED
1906 * for this group, then we are starting or finishing an MLD
1907 * life cycle for this group.
1909 if (inm->in6m_st[1].iss_fmode != inm->in6m_st[0].iss_fmode) {
1910 CTR3(KTR_MLD, "%s: inm transition %d -> %d", __func__,
1911 inm->in6m_st[0].iss_fmode, inm->in6m_st[1].iss_fmode);
1912 if (inm->in6m_st[0].iss_fmode == MCAST_UNDEFINED) {
1913 CTR1(KTR_MLD, "%s: initial join", __func__);
1914 error = mld_initial_join(inm, mli, delay);
1916 } else if (inm->in6m_st[1].iss_fmode == MCAST_UNDEFINED) {
1917 CTR1(KTR_MLD, "%s: final leave", __func__);
1918 mld_final_leave(inm, mli);
1922 CTR1(KTR_MLD, "%s: filter set change", __func__);
1925 error = mld_handle_state_change(inm, mli);
1933 * Perform the initial join for an MLD group.
1935 * When joining a group:
1936 * If the group should have its MLD traffic suppressed, do nothing.
1937 * MLDv1 starts sending MLDv1 host membership reports.
1938 * MLDv2 will schedule an MLDv2 state-change report containing the
1939 * initial state of the membership.
1941 * If the delay argument is non-zero, then we must delay sending the
1942 * initial state change for delay ticks (in units of PR_FASTHZ).
1945 mld_initial_join(struct in6_multi *inm, struct mld_ifsoftc *mli,
1948 struct epoch_tracker et;
1951 int error, retval, syncstates;
1954 char ip6tbuf[INET6_ADDRSTRLEN];
1957 CTR4(KTR_MLD, "%s: initial join %s on ifp %p(%s)",
1958 __func__, ip6_sprintf(ip6tbuf, &inm->in6m_addr),
1959 inm->in6m_ifp, if_name(inm->in6m_ifp));
1964 ifp = inm->in6m_ifp;
1966 IN6_MULTI_LIST_LOCK_ASSERT();
1969 KASSERT(mli && mli->mli_ifp == ifp, ("%s: inconsistent ifp", __func__));
1972 * Groups joined on loopback or marked as 'not reported',
1973 * enter the MLD_SILENT_MEMBER state and
1974 * are never reported in any protocol exchanges.
1975 * All other groups enter the appropriate state machine
1976 * for the version in use on this link.
1977 * A link marked as MLIF_SILENT causes MLD to be completely
1978 * disabled for the link.
1980 if ((ifp->if_flags & IFF_LOOPBACK) ||
1981 (mli->mli_flags & MLIF_SILENT) ||
1982 !mld_is_addr_reported(&inm->in6m_addr)) {
1984 "%s: not kicking state machine for silent group", __func__);
1985 inm->in6m_state = MLD_SILENT_MEMBER;
1986 inm->in6m_timer = 0;
1989 * Deal with overlapping in_multi lifecycle.
1990 * If this group was LEAVING, then make sure
1991 * we drop the reference we picked up to keep the
1992 * group around for the final INCLUDE {} enqueue.
1994 if (mli->mli_version == MLD_VERSION_2 &&
1995 inm->in6m_state == MLD_LEAVING_MEMBER) {
1996 inm->in6m_refcount--;
1997 MPASS(inm->in6m_refcount > 0);
1999 inm->in6m_state = MLD_REPORTING_MEMBER;
2001 switch (mli->mli_version) {
2004 * If a delay was provided, only use it if
2005 * it is greater than the delay normally
2006 * used for an MLDv1 state change report,
2007 * and delay sending the initial MLDv1 report
2008 * by not transitioning to the IDLE state.
2010 odelay = MLD_RANDOM_DELAY(MLD_V1_MAX_RI * PR_FASTHZ);
2012 inm->in6m_timer = max(delay, odelay);
2013 V_current_state_timers_running6 = 1;
2015 inm->in6m_state = MLD_IDLE_MEMBER;
2016 NET_EPOCH_ENTER(et);
2017 error = mld_v1_transmit_report(inm,
2018 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 mq = &inm->in6m_scq;
2042 retval = mld_v2_enqueue_group_record(mq, 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_ifsoftc *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_LIST_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 mbufq_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_ifsoftc *mli)
2164 struct epoch_tracker et;
2167 char ip6tbuf[INET6_ADDRSTRLEN];
2172 CTR4(KTR_MLD, "%s: final leave %s on ifp %p(%s)",
2173 __func__, ip6_sprintf(ip6tbuf, &inm->in6m_addr),
2174 inm->in6m_ifp, if_name(inm->in6m_ifp));
2176 IN6_MULTI_LIST_LOCK_ASSERT();
2179 switch (inm->in6m_state) {
2180 case MLD_NOT_MEMBER:
2181 case MLD_SILENT_MEMBER:
2182 case MLD_LEAVING_MEMBER:
2183 /* Already leaving or left; do nothing. */
2185 "%s: not kicking state machine for silent group", __func__);
2187 case MLD_REPORTING_MEMBER:
2188 case MLD_IDLE_MEMBER:
2189 case MLD_G_QUERY_PENDING_MEMBER:
2190 case MLD_SG_QUERY_PENDING_MEMBER:
2191 if (mli->mli_version == MLD_VERSION_1) {
2193 if (inm->in6m_state == MLD_G_QUERY_PENDING_MEMBER ||
2194 inm->in6m_state == MLD_SG_QUERY_PENDING_MEMBER)
2195 panic("%s: MLDv2 state reached, not MLDv2 mode",
2198 NET_EPOCH_ENTER(et);
2199 mld_v1_transmit_report(inm, MLD_LISTENER_DONE);
2201 inm->in6m_state = MLD_NOT_MEMBER;
2202 V_current_state_timers_running6 = 1;
2203 } else if (mli->mli_version == MLD_VERSION_2) {
2205 * Stop group timer and all pending reports.
2206 * Immediately enqueue a state-change report
2207 * TO_IN {} to be sent on the next fast timeout,
2208 * giving us an opportunity to merge reports.
2210 mbufq_drain(&inm->in6m_scq);
2211 inm->in6m_timer = 0;
2212 inm->in6m_scrv = mli->mli_rv;
2213 CTR4(KTR_MLD, "%s: Leaving %s/%s with %d "
2214 "pending retransmissions.", __func__,
2215 ip6_sprintf(ip6tbuf, &inm->in6m_addr),
2216 if_name(inm->in6m_ifp), inm->in6m_scrv);
2217 if (inm->in6m_scrv == 0) {
2218 inm->in6m_state = MLD_NOT_MEMBER;
2219 inm->in6m_sctimer = 0;
2223 in6m_acquire_locked(inm);
2225 retval = mld_v2_enqueue_group_record(
2226 &inm->in6m_scq, inm, 1, 0, 0,
2227 (mli->mli_flags & MLIF_USEALLOW));
2228 KASSERT(retval != 0,
2229 ("%s: enqueue record = %d", __func__,
2232 inm->in6m_state = MLD_LEAVING_MEMBER;
2233 inm->in6m_sctimer = 1;
2234 V_state_change_timers_running6 = 1;
2240 case MLD_LAZY_MEMBER:
2241 case MLD_SLEEPING_MEMBER:
2242 case MLD_AWAKENING_MEMBER:
2243 /* Our reports are suppressed; do nothing. */
2249 CTR3(KTR_MLD, "%s: T1 -> T0 for %s/%s", __func__,
2250 ip6_sprintf(ip6tbuf, &inm->in6m_addr),
2251 if_name(inm->in6m_ifp));
2252 inm->in6m_st[1].iss_fmode = MCAST_UNDEFINED;
2253 CTR3(KTR_MLD, "%s: T1 now MCAST_UNDEFINED for %p/%s",
2254 __func__, &inm->in6m_addr, if_name(inm->in6m_ifp));
2259 * Enqueue an MLDv2 group record to the given output queue.
2261 * If is_state_change is zero, a current-state record is appended.
2262 * If is_state_change is non-zero, a state-change report is appended.
2264 * If is_group_query is non-zero, an mbuf packet chain is allocated.
2265 * If is_group_query is zero, and if there is a packet with free space
2266 * at the tail of the queue, it will be appended to providing there
2267 * is enough free space.
2268 * Otherwise a new mbuf packet chain is allocated.
2270 * If is_source_query is non-zero, each source is checked to see if
2271 * it was recorded for a Group-Source query, and will be omitted if
2272 * it is not both in-mode and recorded.
2274 * If use_block_allow is non-zero, state change reports for initial join
2275 * and final leave, on an inclusive mode group with a source list, will be
2276 * rewritten to use the ALLOW_NEW and BLOCK_OLD record types, respectively.
2278 * The function will attempt to allocate leading space in the packet
2279 * for the IPv6+ICMP headers to be prepended without fragmenting the chain.
2281 * If successful the size of all data appended to the queue is returned,
2282 * otherwise an error code less than zero is returned, or zero if
2283 * no record(s) were appended.
2286 mld_v2_enqueue_group_record(struct mbufq *mq, struct in6_multi *inm,
2287 const int is_state_change, const int is_group_query,
2288 const int is_source_query, const int use_block_allow)
2290 struct mldv2_record mr;
2291 struct mldv2_record *pmr;
2293 struct ip6_msource *ims, *nims;
2294 struct mbuf *m0, *m, *md;
2295 int is_filter_list_change;
2296 int minrec0len, m0srcs, msrcs, nbytes, off;
2297 int record_has_sources;
2302 char ip6tbuf[INET6_ADDRSTRLEN];
2305 IN6_MULTI_LIST_LOCK_ASSERT();
2307 ifp = inm->in6m_ifp;
2308 is_filter_list_change = 0;
2315 record_has_sources = 1;
2317 type = MLD_DO_NOTHING;
2318 mode = inm->in6m_st[1].iss_fmode;
2321 * If we did not transition out of ASM mode during t0->t1,
2322 * and there are no source nodes to process, we can skip
2323 * the generation of source records.
2325 if (inm->in6m_st[0].iss_asm > 0 && inm->in6m_st[1].iss_asm > 0 &&
2326 inm->in6m_nsrc == 0)
2327 record_has_sources = 0;
2329 if (is_state_change) {
2331 * Queue a state change record.
2332 * If the mode did not change, and there are non-ASM
2333 * listeners or source filters present,
2334 * we potentially need to issue two records for the group.
2335 * If there are ASM listeners, and there was no filter
2336 * mode transition of any kind, do nothing.
2338 * If we are transitioning to MCAST_UNDEFINED, we need
2339 * not send any sources. A transition to/from this state is
2340 * considered inclusive with some special treatment.
2342 * If we are rewriting initial joins/leaves to use
2343 * ALLOW/BLOCK, and the group's membership is inclusive,
2344 * we need to send sources in all cases.
2346 if (mode != inm->in6m_st[0].iss_fmode) {
2347 if (mode == MCAST_EXCLUDE) {
2348 CTR1(KTR_MLD, "%s: change to EXCLUDE",
2350 type = MLD_CHANGE_TO_EXCLUDE_MODE;
2352 CTR1(KTR_MLD, "%s: change to INCLUDE",
2354 if (use_block_allow) {
2357 * Here we're interested in state
2358 * edges either direction between
2359 * MCAST_UNDEFINED and MCAST_INCLUDE.
2360 * Perhaps we should just check
2361 * the group state, rather than
2364 if (mode == MCAST_UNDEFINED) {
2365 type = MLD_BLOCK_OLD_SOURCES;
2367 type = MLD_ALLOW_NEW_SOURCES;
2370 type = MLD_CHANGE_TO_INCLUDE_MODE;
2371 if (mode == MCAST_UNDEFINED)
2372 record_has_sources = 0;
2376 if (record_has_sources) {
2377 is_filter_list_change = 1;
2379 type = MLD_DO_NOTHING;
2384 * Queue a current state record.
2386 if (mode == MCAST_EXCLUDE) {
2387 type = MLD_MODE_IS_EXCLUDE;
2388 } else if (mode == MCAST_INCLUDE) {
2389 type = MLD_MODE_IS_INCLUDE;
2390 KASSERT(inm->in6m_st[1].iss_asm == 0,
2391 ("%s: inm %p is INCLUDE but ASM count is %d",
2392 __func__, inm, inm->in6m_st[1].iss_asm));
2397 * Generate the filter list changes using a separate function.
2399 if (is_filter_list_change)
2400 return (mld_v2_enqueue_filter_change(mq, inm));
2402 if (type == MLD_DO_NOTHING) {
2403 CTR3(KTR_MLD, "%s: nothing to do for %s/%s",
2404 __func__, ip6_sprintf(ip6tbuf, &inm->in6m_addr),
2405 if_name(inm->in6m_ifp));
2410 * If any sources are present, we must be able to fit at least
2411 * one in the trailing space of the tail packet's mbuf,
2414 minrec0len = sizeof(struct mldv2_record);
2415 if (record_has_sources)
2416 minrec0len += sizeof(struct in6_addr);
2418 CTR4(KTR_MLD, "%s: queueing %s for %s/%s", __func__,
2419 mld_rec_type_to_str(type),
2420 ip6_sprintf(ip6tbuf, &inm->in6m_addr),
2421 if_name(inm->in6m_ifp));
2424 * Check if we have a packet in the tail of the queue for this
2425 * group into which the first group record for this group will fit.
2426 * Otherwise allocate a new packet.
2427 * Always allocate leading space for IP6+RA+ICMPV6+REPORT.
2428 * Note: Group records for G/GSR query responses MUST be sent
2429 * in their own packet.
2431 m0 = mbufq_last(mq);
2432 if (!is_group_query &&
2434 (m0->m_pkthdr.PH_vt.vt_nrecs + 1 <= MLD_V2_REPORT_MAXRECS) &&
2435 (m0->m_pkthdr.len + minrec0len) <
2436 (ifp->if_mtu - MLD_MTUSPACE)) {
2437 m0srcs = (ifp->if_mtu - m0->m_pkthdr.len -
2438 sizeof(struct mldv2_record)) /
2439 sizeof(struct in6_addr);
2441 CTR1(KTR_MLD, "%s: use existing packet", __func__);
2443 if (mbufq_full(mq)) {
2444 CTR1(KTR_MLD, "%s: outbound queue full", __func__);
2448 m0srcs = (ifp->if_mtu - MLD_MTUSPACE -
2449 sizeof(struct mldv2_record)) / sizeof(struct in6_addr);
2450 if (!is_state_change && !is_group_query)
2451 m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
2453 m = m_gethdr(M_NOWAIT, MT_DATA);
2457 mld_save_context(m, ifp);
2459 CTR1(KTR_MLD, "%s: allocated first packet", __func__);
2463 * Append group record.
2464 * If we have sources, we don't know how many yet.
2469 mr.mr_addr = inm->in6m_addr;
2470 in6_clearscope(&mr.mr_addr);
2471 if (!m_append(m, sizeof(struct mldv2_record), (void *)&mr)) {
2474 CTR1(KTR_MLD, "%s: m_append() failed.", __func__);
2477 nbytes += sizeof(struct mldv2_record);
2480 * Append as many sources as will fit in the first packet.
2481 * If we are appending to a new packet, the chain allocation
2482 * may potentially use clusters; use m_getptr() in this case.
2483 * If we are appending to an existing packet, we need to obtain
2484 * a pointer to the group record after m_append(), in case a new
2485 * mbuf was allocated.
2487 * Only append sources which are in-mode at t1. If we are
2488 * transitioning to MCAST_UNDEFINED state on the group, and
2489 * use_block_allow is zero, do not include source entries.
2490 * Otherwise, we need to include this source in the report.
2492 * Only report recorded sources in our filter set when responding
2493 * to a group-source query.
2495 if (record_has_sources) {
2498 pmr = (struct mldv2_record *)(mtod(md, uint8_t *) +
2499 md->m_len - nbytes);
2501 md = m_getptr(m, 0, &off);
2502 pmr = (struct mldv2_record *)(mtod(md, uint8_t *) +
2506 RB_FOREACH_SAFE(ims, ip6_msource_tree, &inm->in6m_srcs,
2508 CTR2(KTR_MLD, "%s: visit node %s", __func__,
2509 ip6_sprintf(ip6tbuf, &ims->im6s_addr));
2510 now = im6s_get_mode(inm, ims, 1);
2511 CTR2(KTR_MLD, "%s: node is %d", __func__, now);
2512 if ((now != mode) ||
2514 (!use_block_allow && mode == MCAST_UNDEFINED))) {
2515 CTR1(KTR_MLD, "%s: skip node", __func__);
2518 if (is_source_query && ims->im6s_stp == 0) {
2519 CTR1(KTR_MLD, "%s: skip unrecorded node",
2523 CTR1(KTR_MLD, "%s: append node", __func__);
2524 if (!m_append(m, sizeof(struct in6_addr),
2525 (void *)&ims->im6s_addr)) {
2528 CTR1(KTR_MLD, "%s: m_append() failed.",
2532 nbytes += sizeof(struct in6_addr);
2534 if (msrcs == m0srcs)
2537 CTR2(KTR_MLD, "%s: msrcs is %d this packet", __func__,
2539 pmr->mr_numsrc = htons(msrcs);
2540 nbytes += (msrcs * sizeof(struct in6_addr));
2543 if (is_source_query && msrcs == 0) {
2544 CTR1(KTR_MLD, "%s: no recorded sources to report", __func__);
2551 * We are good to go with first packet.
2554 CTR1(KTR_MLD, "%s: enqueueing first packet", __func__);
2555 m->m_pkthdr.PH_vt.vt_nrecs = 1;
2556 mbufq_enqueue(mq, m);
2558 m->m_pkthdr.PH_vt.vt_nrecs++;
2561 * No further work needed if no source list in packet(s).
2563 if (!record_has_sources)
2567 * Whilst sources remain to be announced, we need to allocate
2568 * a new packet and fill out as many sources as will fit.
2569 * Always try for a cluster first.
2571 while (nims != NULL) {
2572 if (mbufq_full(mq)) {
2573 CTR1(KTR_MLD, "%s: outbound queue full", __func__);
2576 m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
2578 m = m_gethdr(M_NOWAIT, MT_DATA);
2581 mld_save_context(m, ifp);
2582 md = m_getptr(m, 0, &off);
2583 pmr = (struct mldv2_record *)(mtod(md, uint8_t *) + off);
2584 CTR1(KTR_MLD, "%s: allocated next packet", __func__);
2586 if (!m_append(m, sizeof(struct mldv2_record), (void *)&mr)) {
2589 CTR1(KTR_MLD, "%s: m_append() failed.", __func__);
2592 m->m_pkthdr.PH_vt.vt_nrecs = 1;
2593 nbytes += sizeof(struct mldv2_record);
2595 m0srcs = (ifp->if_mtu - MLD_MTUSPACE -
2596 sizeof(struct mldv2_record)) / sizeof(struct in6_addr);
2599 RB_FOREACH_FROM(ims, ip6_msource_tree, nims) {
2600 CTR2(KTR_MLD, "%s: visit node %s",
2601 __func__, ip6_sprintf(ip6tbuf, &ims->im6s_addr));
2602 now = im6s_get_mode(inm, ims, 1);
2603 if ((now != mode) ||
2605 (!use_block_allow && mode == MCAST_UNDEFINED))) {
2606 CTR1(KTR_MLD, "%s: skip node", __func__);
2609 if (is_source_query && ims->im6s_stp == 0) {
2610 CTR1(KTR_MLD, "%s: skip unrecorded node",
2614 CTR1(KTR_MLD, "%s: append node", __func__);
2615 if (!m_append(m, sizeof(struct in6_addr),
2616 (void *)&ims->im6s_addr)) {
2619 CTR1(KTR_MLD, "%s: m_append() failed.",
2624 if (msrcs == m0srcs)
2627 pmr->mr_numsrc = htons(msrcs);
2628 nbytes += (msrcs * sizeof(struct in6_addr));
2630 CTR1(KTR_MLD, "%s: enqueueing next packet", __func__);
2631 mbufq_enqueue(mq, m);
2638 * Type used to mark record pass completion.
2639 * We exploit the fact we can cast to this easily from the
2640 * current filter modes on each ip_msource node.
2643 REC_NONE = 0x00, /* MCAST_UNDEFINED */
2644 REC_ALLOW = 0x01, /* MCAST_INCLUDE */
2645 REC_BLOCK = 0x02, /* MCAST_EXCLUDE */
2646 REC_FULL = REC_ALLOW | REC_BLOCK
2650 * Enqueue an MLDv2 filter list change to the given output queue.
2652 * Source list filter state is held in an RB-tree. When the filter list
2653 * for a group is changed without changing its mode, we need to compute
2654 * the deltas between T0 and T1 for each source in the filter set,
2655 * and enqueue the appropriate ALLOW_NEW/BLOCK_OLD records.
2657 * As we may potentially queue two record types, and the entire R-B tree
2658 * needs to be walked at once, we break this out into its own function
2659 * so we can generate a tightly packed queue of packets.
2661 * XXX This could be written to only use one tree walk, although that makes
2662 * serializing into the mbuf chains a bit harder. For now we do two walks
2663 * which makes things easier on us, and it may or may not be harder on
2666 * If successful the size of all data appended to the queue is returned,
2667 * otherwise an error code less than zero is returned, or zero if
2668 * no record(s) were appended.
2671 mld_v2_enqueue_filter_change(struct mbufq *mq, struct in6_multi *inm)
2673 static const int MINRECLEN =
2674 sizeof(struct mldv2_record) + sizeof(struct in6_addr);
2676 struct mldv2_record mr;
2677 struct mldv2_record *pmr;
2678 struct ip6_msource *ims, *nims;
2679 struct mbuf *m, *m0, *md;
2680 int m0srcs, nbytes, npbytes, off, rsrcs, schanged;
2682 uint8_t mode, now, then;
2683 rectype_t crt, drt, nrt;
2685 char ip6tbuf[INET6_ADDRSTRLEN];
2688 IN6_MULTI_LIST_LOCK_ASSERT();
2690 if (inm->in6m_nsrc == 0 ||
2691 (inm->in6m_st[0].iss_asm > 0 && inm->in6m_st[1].iss_asm > 0))
2694 ifp = inm->in6m_ifp; /* interface */
2695 mode = inm->in6m_st[1].iss_fmode; /* filter mode at t1 */
2696 crt = REC_NONE; /* current group record type */
2697 drt = REC_NONE; /* mask of completed group record types */
2698 nrt = REC_NONE; /* record type for current node */
2699 m0srcs = 0; /* # source which will fit in current mbuf chain */
2700 npbytes = 0; /* # of bytes appended this packet */
2701 nbytes = 0; /* # of bytes appended to group's state-change queue */
2702 rsrcs = 0; /* # sources encoded in current record */
2703 schanged = 0; /* # nodes encoded in overall filter change */
2704 nallow = 0; /* # of source entries in ALLOW_NEW */
2705 nblock = 0; /* # of source entries in BLOCK_OLD */
2706 nims = NULL; /* next tree node pointer */
2709 * For each possible filter record mode.
2710 * The first kind of source we encounter tells us which
2711 * is the first kind of record we start appending.
2712 * If a node transitioned to UNDEFINED at t1, its mode is treated
2713 * as the inverse of the group's filter mode.
2715 while (drt != REC_FULL) {
2717 m0 = mbufq_last(mq);
2719 (m0->m_pkthdr.PH_vt.vt_nrecs + 1 <=
2720 MLD_V2_REPORT_MAXRECS) &&
2721 (m0->m_pkthdr.len + MINRECLEN) <
2722 (ifp->if_mtu - MLD_MTUSPACE)) {
2724 m0srcs = (ifp->if_mtu - m0->m_pkthdr.len -
2725 sizeof(struct mldv2_record)) /
2726 sizeof(struct in6_addr);
2728 "%s: use previous packet", __func__);
2730 m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
2732 m = m_gethdr(M_NOWAIT, MT_DATA);
2735 "%s: m_get*() failed", __func__);
2738 m->m_pkthdr.PH_vt.vt_nrecs = 0;
2739 mld_save_context(m, ifp);
2740 m0srcs = (ifp->if_mtu - MLD_MTUSPACE -
2741 sizeof(struct mldv2_record)) /
2742 sizeof(struct in6_addr);
2745 "%s: allocated new packet", __func__);
2748 * Append the MLD group record header to the
2749 * current packet's data area.
2750 * Recalculate pointer to free space for next
2751 * group record, in case m_append() allocated
2752 * a new mbuf or cluster.
2754 memset(&mr, 0, sizeof(mr));
2755 mr.mr_addr = inm->in6m_addr;
2756 in6_clearscope(&mr.mr_addr);
2757 if (!m_append(m, sizeof(mr), (void *)&mr)) {
2761 "%s: m_append() failed", __func__);
2764 npbytes += sizeof(struct mldv2_record);
2766 /* new packet; offset in chain */
2767 md = m_getptr(m, npbytes -
2768 sizeof(struct mldv2_record), &off);
2769 pmr = (struct mldv2_record *)(mtod(md,
2772 /* current packet; offset from last append */
2774 pmr = (struct mldv2_record *)(mtod(md,
2775 uint8_t *) + md->m_len -
2776 sizeof(struct mldv2_record));
2779 * Begin walking the tree for this record type
2780 * pass, or continue from where we left off
2781 * previously if we had to allocate a new packet.
2782 * Only report deltas in-mode at t1.
2783 * We need not report included sources as allowed
2784 * if we are in inclusive mode on the group,
2785 * however the converse is not true.
2789 nims = RB_MIN(ip6_msource_tree,
2792 RB_FOREACH_FROM(ims, ip6_msource_tree, nims) {
2793 CTR2(KTR_MLD, "%s: visit node %s", __func__,
2794 ip6_sprintf(ip6tbuf, &ims->im6s_addr));
2795 now = im6s_get_mode(inm, ims, 1);
2796 then = im6s_get_mode(inm, ims, 0);
2797 CTR3(KTR_MLD, "%s: mode: t0 %d, t1 %d",
2798 __func__, then, now);
2801 "%s: skip unchanged", __func__);
2804 if (mode == MCAST_EXCLUDE &&
2805 now == MCAST_INCLUDE) {
2807 "%s: skip IN src on EX group",
2811 nrt = (rectype_t)now;
2812 if (nrt == REC_NONE)
2813 nrt = (rectype_t)(~mode & REC_FULL);
2814 if (schanged++ == 0) {
2816 } else if (crt != nrt)
2818 if (!m_append(m, sizeof(struct in6_addr),
2819 (void *)&ims->im6s_addr)) {
2823 "%s: m_append() failed", __func__);
2826 nallow += !!(crt == REC_ALLOW);
2827 nblock += !!(crt == REC_BLOCK);
2828 if (++rsrcs == m0srcs)
2832 * If we did not append any tree nodes on this
2833 * pass, back out of allocations.
2836 npbytes -= sizeof(struct mldv2_record);
2839 "%s: m_free(m)", __func__);
2843 "%s: m_adj(m, -mr)", __func__);
2844 m_adj(m, -((int)sizeof(
2845 struct mldv2_record)));
2849 npbytes += (rsrcs * sizeof(struct in6_addr));
2850 if (crt == REC_ALLOW)
2851 pmr->mr_type = MLD_ALLOW_NEW_SOURCES;
2852 else if (crt == REC_BLOCK)
2853 pmr->mr_type = MLD_BLOCK_OLD_SOURCES;
2854 pmr->mr_numsrc = htons(rsrcs);
2856 * Count the new group record, and enqueue this
2857 * packet if it wasn't already queued.
2859 m->m_pkthdr.PH_vt.vt_nrecs++;
2861 mbufq_enqueue(mq, m);
2863 } while (nims != NULL);
2865 crt = (~crt & REC_FULL);
2868 CTR3(KTR_MLD, "%s: queued %d ALLOW_NEW, %d BLOCK_OLD", __func__,
2875 mld_v2_merge_state_changes(struct in6_multi *inm, struct mbufq *scq)
2878 struct mbuf *m; /* pending state-change */
2879 struct mbuf *m0; /* copy of pending state-change */
2880 struct mbuf *mt; /* last state-change in packet */
2881 int docopy, domerge;
2888 IN6_MULTI_LIST_LOCK_ASSERT();
2892 * If there are further pending retransmissions, make a writable
2893 * copy of each queued state-change message before merging.
2895 if (inm->in6m_scrv > 0)
2898 gq = &inm->in6m_scq;
2900 if (mbufq_first(gq) == NULL) {
2901 CTR2(KTR_MLD, "%s: WARNING: queue for inm %p is empty",
2906 m = mbufq_first(gq);
2909 * Only merge the report into the current packet if
2910 * there is sufficient space to do so; an MLDv2 report
2911 * packet may only contain 65,535 group records.
2912 * Always use a simple mbuf chain concatentation to do this,
2913 * as large state changes for single groups may have
2914 * allocated clusters.
2917 mt = mbufq_last(scq);
2919 recslen = m_length(m, NULL);
2921 if ((mt->m_pkthdr.PH_vt.vt_nrecs +
2922 m->m_pkthdr.PH_vt.vt_nrecs <=
2923 MLD_V2_REPORT_MAXRECS) &&
2924 (mt->m_pkthdr.len + recslen <=
2925 (inm->in6m_ifp->if_mtu - MLD_MTUSPACE)))
2929 if (!domerge && mbufq_full(gq)) {
2931 "%s: outbound queue full, skipping whole packet %p",
2941 CTR2(KTR_MLD, "%s: dequeueing %p", __func__, m);
2942 m0 = mbufq_dequeue(gq);
2945 CTR2(KTR_MLD, "%s: copying %p", __func__, m);
2946 m0 = m_dup(m, M_NOWAIT);
2949 m0->m_nextpkt = NULL;
2954 CTR3(KTR_MLD, "%s: queueing %p to scq %p)",
2956 mbufq_enqueue(scq, m0);
2958 struct mbuf *mtl; /* last mbuf of packet mt */
2960 CTR3(KTR_MLD, "%s: merging %p with ifscq tail %p)",
2964 m0->m_flags &= ~M_PKTHDR;
2965 mt->m_pkthdr.len += recslen;
2966 mt->m_pkthdr.PH_vt.vt_nrecs +=
2967 m0->m_pkthdr.PH_vt.vt_nrecs;
2977 * Respond to a pending MLDv2 General Query.
2980 mld_v2_dispatch_general_query(struct mld_ifsoftc *mli)
2982 struct ifmultiaddr *ifma;
2984 struct in6_multi *inm;
2988 IN6_MULTI_LIST_LOCK_ASSERT();
2991 KASSERT(mli->mli_version == MLD_VERSION_2,
2992 ("%s: called when version %d", __func__, mli->mli_version));
2995 * Check that there are some packets queued. If so, send them first.
2996 * For large number of groups the reply to general query can take
2997 * many packets, we should finish sending them before starting of
2998 * queuing the new reply.
3000 if (mbufq_len(&mli->mli_gq) != 0)
3005 CK_STAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
3006 inm = in6m_ifmultiaddr_get_inm(ifma);
3009 KASSERT(ifp == inm->in6m_ifp,
3010 ("%s: inconsistent ifp", __func__));
3012 switch (inm->in6m_state) {
3013 case MLD_NOT_MEMBER:
3014 case MLD_SILENT_MEMBER:
3016 case MLD_REPORTING_MEMBER:
3017 case MLD_IDLE_MEMBER:
3018 case MLD_LAZY_MEMBER:
3019 case MLD_SLEEPING_MEMBER:
3020 case MLD_AWAKENING_MEMBER:
3021 inm->in6m_state = MLD_REPORTING_MEMBER;
3022 retval = mld_v2_enqueue_group_record(&mli->mli_gq,
3024 CTR2(KTR_MLD, "%s: enqueue record = %d",
3027 case MLD_G_QUERY_PENDING_MEMBER:
3028 case MLD_SG_QUERY_PENDING_MEMBER:
3029 case MLD_LEAVING_MEMBER:
3035 mld_dispatch_queue(&mli->mli_gq, MLD_MAX_RESPONSE_BURST);
3038 * Slew transmission of bursts over 500ms intervals.
3040 if (mbufq_first(&mli->mli_gq) != NULL) {
3041 mli->mli_v2_timer = 1 + MLD_RANDOM_DELAY(
3042 MLD_RESPONSE_BURST_INTERVAL);
3043 V_interface_timers_running6 = 1;
3048 * Transmit the next pending message in the output queue.
3050 * VIMAGE: Needs to store/restore vnet pointer on a per-mbuf-chain basis.
3051 * MRT: Nothing needs to be done, as MLD traffic is always local to
3052 * a link and uses a link-scope multicast address.
3055 mld_dispatch_packet(struct mbuf *m)
3057 struct ip6_moptions im6o;
3062 struct ip6_hdr *ip6;
3063 struct mld_hdr *mld;
3069 CTR2(KTR_MLD, "%s: transmit %p", __func__, m);
3073 * Set VNET image pointer from enqueued mbuf chain
3074 * before doing anything else. Whilst we use interface
3075 * indexes to guard against interface detach, they are
3076 * unique to each VIMAGE and must be retrieved.
3078 ifindex = mld_restore_context(m);
3081 * Check if the ifnet still exists. This limits the scope of
3082 * any race in the absence of a global ifp lock for low cost
3083 * (an array lookup).
3085 ifp = ifnet_byindex(ifindex);
3087 CTR3(KTR_MLD, "%s: dropped %p as ifindex %u went away.",
3088 __func__, m, ifindex);
3090 IP6STAT_INC(ip6s_noroute);
3094 im6o.im6o_multicast_hlim = 1;
3095 im6o.im6o_multicast_loop = (V_ip6_mrouter != NULL);
3096 im6o.im6o_multicast_ifp = ifp;
3098 if (m->m_flags & M_MLDV1) {
3101 m0 = mld_v2_encap_report(ifp, m);
3103 CTR2(KTR_MLD, "%s: dropped %p", __func__, m);
3104 IP6STAT_INC(ip6s_odropped);
3109 mld_scrub_context(m0);
3111 m0->m_pkthdr.rcvif = V_loif;
3113 ip6 = mtod(m0, struct ip6_hdr *);
3115 (void)in6_setscope(&ip6->ip6_dst, ifp, NULL); /* XXX LOR */
3118 * XXX XXX Break some KPI rules to prevent an LOR which would
3119 * occur if we called in6_setscope() at transmission.
3120 * See comments at top of file.
3122 MLD_EMBEDSCOPE(&ip6->ip6_dst, ifp->if_index);
3126 * Retrieve the ICMPv6 type before handoff to ip6_output(),
3127 * so we can bump the stats.
3129 md = m_getptr(m0, sizeof(struct ip6_hdr), &off);
3130 mld = (struct mld_hdr *)(mtod(md, uint8_t *) + off);
3131 type = mld->mld_type;
3134 error = ip6_output(m0, &mld_po, NULL, IPV6_UNSPECSRC, &im6o,
3137 CTR3(KTR_MLD, "%s: ip6_output(%p) = %d", __func__, m0, error);
3140 ICMP6STAT_INC(icp6s_outhist[type]);
3142 icmp6_ifstat_inc(oifp, ifs6_out_msg);
3144 case MLD_LISTENER_REPORT:
3145 case MLDV2_LISTENER_REPORT:
3146 icmp6_ifstat_inc(oifp, ifs6_out_mldreport);
3148 case MLD_LISTENER_DONE:
3149 icmp6_ifstat_inc(oifp, ifs6_out_mlddone);
3158 * Encapsulate an MLDv2 report.
3160 * KAME IPv6 requires that hop-by-hop options be passed separately,
3161 * and that the IPv6 header be prepended in a separate mbuf.
3163 * Returns a pointer to the new mbuf chain head, or NULL if the
3164 * allocation failed.
3166 static struct mbuf *
3167 mld_v2_encap_report(struct ifnet *ifp, struct mbuf *m)
3170 struct mldv2_report *mld;
3171 struct ip6_hdr *ip6;
3172 struct in6_ifaddr *ia;
3175 KASSERT(ifp != NULL, ("%s: null ifp", __func__));
3176 KASSERT((m->m_flags & M_PKTHDR),
3177 ("%s: mbuf chain %p is !M_PKTHDR", __func__, m));
3180 * RFC3590: OK to send as :: or tentative during DAD.
3183 ia = in6ifa_ifpforlinklocal(ifp, IN6_IFF_NOTREADY|IN6_IFF_ANYCAST);
3185 CTR1(KTR_MLD, "%s: warning: ia is NULL", __func__);
3187 mh = m_gethdr(M_NOWAIT, MT_DATA);
3190 ifa_free(&ia->ia_ifa);
3194 M_ALIGN(mh, sizeof(struct ip6_hdr) + sizeof(struct mldv2_report));
3196 mldreclen = m_length(m, NULL);
3197 CTR2(KTR_MLD, "%s: mldreclen is %d", __func__, mldreclen);
3199 mh->m_len = sizeof(struct ip6_hdr) + sizeof(struct mldv2_report);
3200 mh->m_pkthdr.len = sizeof(struct ip6_hdr) +
3201 sizeof(struct mldv2_report) + mldreclen;
3203 ip6 = mtod(mh, struct ip6_hdr *);
3205 ip6->ip6_vfc &= ~IPV6_VERSION_MASK;
3206 ip6->ip6_vfc |= IPV6_VERSION;
3207 ip6->ip6_nxt = IPPROTO_ICMPV6;
3208 ip6->ip6_src = ia ? ia->ia_addr.sin6_addr : in6addr_any;
3210 ifa_free(&ia->ia_ifa);
3211 ip6->ip6_dst = in6addr_linklocal_allv2routers;
3212 /* scope ID will be set in netisr */
3214 mld = (struct mldv2_report *)(ip6 + 1);
3215 mld->mld_type = MLDV2_LISTENER_REPORT;
3218 mld->mld_v2_reserved = 0;
3219 mld->mld_v2_numrecs = htons(m->m_pkthdr.PH_vt.vt_nrecs);
3220 m->m_pkthdr.PH_vt.vt_nrecs = 0;
3223 mld->mld_cksum = in6_cksum(mh, IPPROTO_ICMPV6,
3224 sizeof(struct ip6_hdr), sizeof(struct mldv2_report) + mldreclen);
3230 mld_rec_type_to_str(const int type)
3234 case MLD_CHANGE_TO_EXCLUDE_MODE:
3237 case MLD_CHANGE_TO_INCLUDE_MODE:
3240 case MLD_MODE_IS_EXCLUDE:
3243 case MLD_MODE_IS_INCLUDE:
3246 case MLD_ALLOW_NEW_SOURCES:
3249 case MLD_BLOCK_OLD_SOURCES:
3260 mld_init(void *unused __unused)
3263 CTR1(KTR_MLD, "%s: initializing", __func__);
3266 ip6_initpktopts(&mld_po);
3267 mld_po.ip6po_hlim = 1;
3268 mld_po.ip6po_hbh = &mld_ra.hbh;
3269 mld_po.ip6po_prefer_tempaddr = IP6PO_TEMPADDR_NOTPREFER;
3270 mld_po.ip6po_flags = IP6PO_DONTFRAG;
3272 SYSINIT(mld_init, SI_SUB_PROTO_MC, SI_ORDER_MIDDLE, mld_init, NULL);
3275 mld_uninit(void *unused __unused)
3278 CTR1(KTR_MLD, "%s: tearing down", __func__);
3281 SYSUNINIT(mld_uninit, SI_SUB_PROTO_MC, SI_ORDER_MIDDLE, mld_uninit, NULL);
3284 vnet_mld_init(const void *unused __unused)
3287 CTR1(KTR_MLD, "%s: initializing", __func__);
3289 LIST_INIT(&V_mli_head);
3291 VNET_SYSINIT(vnet_mld_init, SI_SUB_PROTO_MC, SI_ORDER_ANY, vnet_mld_init,
3295 vnet_mld_uninit(const void *unused __unused)
3298 /* This can happen if we shutdown the network stack. */
3299 CTR1(KTR_MLD, "%s: tearing down", __func__);
3301 VNET_SYSUNINIT(vnet_mld_uninit, SI_SUB_PROTO_MC, SI_ORDER_ANY, vnet_mld_uninit,
3305 mld_modevent(module_t mod, int type, void *unused __unused)
3313 return (EOPNOTSUPP);
3318 static moduledata_t mld_mod = {
3323 DECLARE_MODULE(mld, mld_mod, SI_SUB_PROTO_MC, SI_ORDER_ANY);