2 * Copyright (c) 2000-2020 Apple Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
29 * Copyright (c) 2009 Bruce Simpson.
31 * Redistribution and use in source and binary forms, with or without
32 * modification, are permitted provided that the following conditions
34 * 1. Redistributions of source code must retain the above copyright
35 * notice, this list of conditions and the following disclaimer.
36 * 2. Redistributions in binary form must reproduce the above copyright
37 * notice, this list of conditions and the following disclaimer in the
38 * documentation and/or other materials provided with the distribution.
39 * 3. The name of the author may not be used to endorse or promote
40 * products derived from this software without specific prior written
43 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
44 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
45 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
46 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
47 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
48 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
49 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
50 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
51 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
52 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
57 * Copyright (c) 1988 Stephen Deering.
58 * Copyright (c) 1992, 1993
59 * The Regents of the University of California. All rights reserved.
61 * This code is derived from software contributed to Berkeley by
62 * Stephen Deering of Stanford University.
64 * Redistribution and use in source and binary forms, with or without
65 * modification, are permitted provided that the following conditions
67 * 1. Redistributions of source code must retain the above copyright
68 * notice, this list of conditions and the following disclaimer.
69 * 2. Redistributions in binary form must reproduce the above copyright
70 * notice, this list of conditions and the following disclaimer in the
71 * documentation and/or other materials provided with the distribution.
72 * 3. All advertising materials mentioning features or use of this software
73 * must display the following acknowledgement:
74 * This product includes software developed by the University of
75 * California, Berkeley and its contributors.
76 * 4. Neither the name of the University nor the names of its contributors
77 * may be used to endorse or promote products derived from this software
78 * without specific prior written permission.
80 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
81 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
82 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
83 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
84 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
85 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
86 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
87 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
88 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
89 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
92 * @(#)igmp.c 8.1 (Berkeley) 7/19/93
95 * NOTICE: This file was modified by SPARTA, Inc. in 2005 to introduce
96 * support for mandatory and extensible security protections. This notice
97 * is included in support of clause 2.2 (b) of the Apple Public License,
101 #include <sys/cdefs.h>
103 #include <sys/param.h>
104 #include <sys/systm.h>
105 #include <sys/mbuf.h>
106 #include <sys/socket.h>
107 #include <sys/protosw.h>
108 #include <sys/sysctl.h>
109 #include <sys/kernel.h>
110 #include <sys/malloc.h>
111 #include <sys/mcache.h>
113 #include <dev/random/randomdev.h>
115 #include <kern/zalloc.h>
118 #include <net/route.h>
120 #include <netinet/in.h>
121 #include <netinet/in_var.h>
122 #include <netinet6/in6_var.h>
123 #include <netinet/ip6.h>
124 #include <netinet6/ip6_var.h>
125 #include <netinet6/scope6_var.h>
126 #include <netinet/icmp6.h>
127 #include <netinet6/mld6.h>
128 #include <netinet6/mld6_var.h>
130 /* Lock group and attribute for mld_mtx */
131 static lck_attr_t
*mld_mtx_attr
;
132 static lck_grp_t
*mld_mtx_grp
;
133 static lck_grp_attr_t
*mld_mtx_grp_attr
;
136 * Locking and reference counting:
138 * mld_mtx mainly protects mli_head. In cases where both mld_mtx and
139 * in6_multihead_lock must be held, the former must be acquired first in order
140 * to maintain lock ordering. It is not a requirement that mld_mtx be
141 * acquired first before in6_multihead_lock, but in case both must be acquired
142 * in succession, the correct lock ordering must be followed.
144 * Instead of walking the if_multiaddrs list at the interface and returning
145 * the ifma_protospec value of a matching entry, we search the global list
146 * of in6_multi records and find it that way; this is done with in6_multihead
147 * lock held. Doing so avoids the race condition issues that many other BSDs
148 * suffer from (therefore in our implementation, ifma_protospec will never be
149 * NULL for as long as the in6_multi is valid.)
151 * The above creates a requirement for the in6_multi to stay in in6_multihead
152 * list even after the final MLD leave (in MLDv2 mode) until no longer needs
153 * be retransmitted (this is not required for MLDv1.) In order to handle
154 * this, the request and reference counts of the in6_multi are bumped up when
155 * the state changes to MLD_LEAVING_MEMBER, and later dropped in the timeout
156 * handler. Each in6_multi holds a reference to the underlying mld_ifinfo.
158 * Thus, the permitted lock order is:
160 * mld_mtx, in6_multihead_lock, inm6_lock, mli_lock
162 * Any may be taken independently, but if any are held at the same time,
163 * the above lock order must be followed.
165 static decl_lck_mtx_data(, mld_mtx
);
167 SLIST_HEAD(mld_in6m_relhead
, in6_multi
);
169 static void mli_initvar(struct mld_ifinfo
*, struct ifnet
*, int);
170 static struct mld_ifinfo
*mli_alloc(zalloc_flags_t
);
171 static void mli_free(struct mld_ifinfo
*);
172 static void mli_delete(const struct ifnet
*, struct mld_in6m_relhead
*);
173 static void mld_dispatch_packet(struct mbuf
*);
174 static void mld_final_leave(struct in6_multi
*, struct mld_ifinfo
*,
175 struct mld_tparams
*);
176 static int mld_handle_state_change(struct in6_multi
*, struct mld_ifinfo
*,
177 struct mld_tparams
*);
178 static int mld_initial_join(struct in6_multi
*, struct mld_ifinfo
*,
179 struct mld_tparams
*, const int);
181 static const char * mld_rec_type_to_str(const int);
183 static uint32_t mld_set_version(struct mld_ifinfo
*, const int);
184 static void mld_flush_relq(struct mld_ifinfo
*, struct mld_in6m_relhead
*);
185 static void mld_dispatch_queue_locked(struct mld_ifinfo
*, struct ifqueue
*, int);
186 static int mld_v1_input_query(struct ifnet
*, const struct ip6_hdr
*,
187 /*const*/ struct mld_hdr
*);
188 static int mld_v1_input_report(struct ifnet
*, struct mbuf
*,
189 const struct ip6_hdr
*, /*const*/ struct mld_hdr
*);
190 static void mld_v1_process_group_timer(struct in6_multi
*, const int);
191 static void mld_v1_process_querier_timers(struct mld_ifinfo
*);
192 static int mld_v1_transmit_report(struct in6_multi
*, const uint8_t);
193 static uint32_t mld_v1_update_group(struct in6_multi
*, const int);
194 static void mld_v2_cancel_link_timers(struct mld_ifinfo
*);
195 static uint32_t mld_v2_dispatch_general_query(struct mld_ifinfo
*);
197 mld_v2_encap_report(struct ifnet
*, struct mbuf
*);
198 static int mld_v2_enqueue_filter_change(struct ifqueue
*,
200 static int mld_v2_enqueue_group_record(struct ifqueue
*,
201 struct in6_multi
*, const int, const int, const int,
203 static int mld_v2_input_query(struct ifnet
*, const struct ip6_hdr
*,
204 struct mbuf
*, const int, const int);
205 static int mld_v2_merge_state_changes(struct in6_multi
*,
207 static void mld_v2_process_group_timers(struct mld_ifinfo
*,
208 struct ifqueue
*, struct ifqueue
*,
209 struct in6_multi
*, const int);
210 static int mld_v2_process_group_query(struct in6_multi
*,
211 int, struct mbuf
*, const int);
212 static int sysctl_mld_gsr SYSCTL_HANDLER_ARGS
;
213 static int sysctl_mld_ifinfo SYSCTL_HANDLER_ARGS
;
214 static int sysctl_mld_v2enable SYSCTL_HANDLER_ARGS
;
216 static int mld_timeout_run
; /* MLD timer is scheduled to run */
217 static void mld_timeout(void *);
218 static void mld_sched_timeout(void);
221 * Normative references: RFC 2710, RFC 3590, RFC 3810.
223 static struct timeval mld_gsrdelay
= {.tv_sec
= 10, .tv_usec
= 0};
224 static LIST_HEAD(, mld_ifinfo
) mli_head
;
226 static int querier_present_timers_running6
;
227 static int interface_timers_running6
;
228 static int state_change_timers_running6
;
229 static int current_state_timers_running6
;
231 static unsigned int mld_mli_list_genid
;
233 * Subsystem lock macros.
236 lck_mtx_lock(&mld_mtx)
237 #define MLD_LOCK_ASSERT_HELD() \
238 LCK_MTX_ASSERT(&mld_mtx, LCK_MTX_ASSERT_OWNED)
239 #define MLD_LOCK_ASSERT_NOTHELD() \
240 LCK_MTX_ASSERT(&mld_mtx, LCK_MTX_ASSERT_NOTOWNED)
241 #define MLD_UNLOCK() \
242 lck_mtx_unlock(&mld_mtx)
244 #define MLD_ADD_DETACHED_IN6M(_head, _in6m) { \
245 SLIST_INSERT_HEAD(_head, _in6m, in6m_dtle); \
248 #define MLD_REMOVE_DETACHED_IN6M(_head) { \
249 struct in6_multi *_in6m, *_inm_tmp; \
250 SLIST_FOREACH_SAFE(_in6m, _head, in6m_dtle, _inm_tmp) { \
251 SLIST_REMOVE(_head, _in6m, in6_multi, in6m_dtle); \
252 IN6M_REMREF(_in6m); \
254 VERIFY(SLIST_EMPTY(_head)); \
257 static ZONE_DECLARE(mli_zone
, "mld_ifinfo",
258 sizeof(struct mld_ifinfo
), ZC_ZFREE_CLEARMEM
);
260 SYSCTL_DECL(_net_inet6
); /* Note: Not in any common header. */
262 SYSCTL_NODE(_net_inet6
, OID_AUTO
, mld
, CTLFLAG_RW
| CTLFLAG_LOCKED
, 0,
263 "IPv6 Multicast Listener Discovery");
264 SYSCTL_PROC(_net_inet6_mld
, OID_AUTO
, gsrdelay
,
265 CTLTYPE_INT
| CTLFLAG_RW
| CTLFLAG_LOCKED
,
266 &mld_gsrdelay
.tv_sec
, 0, sysctl_mld_gsr
, "I",
267 "Rate limit for MLDv2 Group-and-Source queries in seconds");
269 SYSCTL_NODE(_net_inet6_mld
, OID_AUTO
, ifinfo
, CTLFLAG_RD
| CTLFLAG_LOCKED
,
270 sysctl_mld_ifinfo
, "Per-interface MLDv2 state");
272 static int mld_v1enable
= 1;
273 SYSCTL_INT(_net_inet6_mld
, OID_AUTO
, v1enable
, CTLFLAG_RW
| CTLFLAG_LOCKED
,
274 &mld_v1enable
, 0, "Enable fallback to MLDv1");
276 static int mld_v2enable
= 1;
277 SYSCTL_PROC(_net_inet6_mld
, OID_AUTO
, v2enable
,
278 CTLTYPE_INT
| CTLFLAG_RW
| CTLFLAG_LOCKED
,
279 &mld_v2enable
, 0, sysctl_mld_v2enable
, "I",
280 "Enable MLDv2 (debug purposes only)");
282 static int mld_use_allow
= 1;
283 SYSCTL_INT(_net_inet6_mld
, OID_AUTO
, use_allow
, CTLFLAG_RW
| CTLFLAG_LOCKED
,
284 &mld_use_allow
, 0, "Use ALLOW/BLOCK for RFC 4604 SSM joins/leaves");
288 SYSCTL_INT(_net_inet6_mld
, OID_AUTO
,
289 debug
, CTLFLAG_RW
| CTLFLAG_LOCKED
, &mld_debug
, 0, "");
292 * Packed Router Alert option structure declaration.
297 struct ip6_opt_router ra
;
301 * Router Alert hop-by-hop option header.
303 static struct mld_raopt mld_ra
= {
304 .hbh
= { .ip6h_nxt
= 0, .ip6h_len
= 0 },
305 .pad
= { .ip6o_type
= IP6OPT_PADN
, .ip6o_len
= 0 },
307 .ip6or_type
= (u_int8_t
)IP6OPT_ROUTER_ALERT
,
308 .ip6or_len
= (u_int8_t
)(IP6OPT_RTALERT_LEN
- 2),
309 .ip6or_value
= {((IP6OPT_RTALERT_MLD
>> 8) & 0xFF),
310 (IP6OPT_RTALERT_MLD
& 0xFF) }
313 static struct ip6_pktopts mld_po
;
315 /* Store MLDv2 record count in the module private scratch space */
316 #define vt_nrecs pkt_mpriv.__mpriv_u.__mpriv32[0].__mpriv32_u.__val16[0]
319 mld_save_context(struct mbuf
*m
, struct ifnet
*ifp
)
321 m
->m_pkthdr
.rcvif
= ifp
;
325 mld_scrub_context(struct mbuf
*m
)
327 m
->m_pkthdr
.rcvif
= NULL
;
331 * Restore context from a queued output chain.
334 static __inline
struct ifnet
*
335 mld_restore_context(struct mbuf
*m
)
337 return m
->m_pkthdr
.rcvif
;
341 * Retrieve or set threshold between group-source queries in seconds.
344 sysctl_mld_gsr SYSCTL_HANDLER_ARGS
346 #pragma unused(arg1, arg2)
352 i
= (int)mld_gsrdelay
.tv_sec
;
354 error
= sysctl_handle_int(oidp
, &i
, 0, req
);
355 if (error
|| !req
->newptr
) {
359 if (i
< -1 || i
>= 60) {
364 mld_gsrdelay
.tv_sec
= i
;
371 * Expose struct mld_ifinfo to userland, keyed by ifindex.
372 * For use by ifmcstat(8).
376 sysctl_mld_ifinfo SYSCTL_HANDLER_ARGS
383 struct mld_ifinfo
*mli
;
384 struct mld_ifinfo_u mli_u
;
389 if (req
->newptr
!= USER_ADDR_NULL
) {
399 if (name
[0] <= 0 || name
[0] > (u_int
)if_index
) {
406 ifnet_head_lock_shared();
407 ifp
= ifindex2ifnet
[name
[0]];
413 bzero(&mli_u
, sizeof(mli_u
));
415 LIST_FOREACH(mli
, &mli_head
, mli_link
) {
417 if (ifp
!= mli
->mli_ifp
) {
422 mli_u
.mli_ifindex
= mli
->mli_ifp
->if_index
;
423 mli_u
.mli_version
= mli
->mli_version
;
424 mli_u
.mli_v1_timer
= mli
->mli_v1_timer
;
425 mli_u
.mli_v2_timer
= mli
->mli_v2_timer
;
426 mli_u
.mli_flags
= mli
->mli_flags
;
427 mli_u
.mli_rv
= mli
->mli_rv
;
428 mli_u
.mli_qi
= mli
->mli_qi
;
429 mli_u
.mli_qri
= mli
->mli_qri
;
430 mli_u
.mli_uri
= mli
->mli_uri
;
433 error
= SYSCTL_OUT(req
, &mli_u
, sizeof(mli_u
));
443 sysctl_mld_v2enable SYSCTL_HANDLER_ARGS
445 #pragma unused(arg1, arg2)
448 struct mld_ifinfo
*mli
;
449 struct mld_tparams mtp
= { .qpt
= 0, .it
= 0, .cst
= 0, .sct
= 0 };
455 error
= sysctl_handle_int(oidp
, &i
, 0, req
);
456 if (error
|| !req
->newptr
) {
460 if (i
< 0 || i
> 1) {
467 * If we enabled v2, the state transition will take care of upgrading
468 * the MLD version back to v2. Otherwise, we have to explicitly
469 * downgrade. Note that this functionality is to be used for debugging.
471 if (mld_v2enable
== 1) {
475 LIST_FOREACH(mli
, &mli_head
, mli_link
) {
477 if (mld_set_version(mli
, MLD_VERSION_1
) > 0) {
486 mld_set_timeout(&mtp
);
492 * Dispatch an entire queue of pending packet chains.
494 * Must not be called with in6m_lock held.
495 * XXX This routine unlocks MLD global lock and also mli locks.
496 * Make sure that the calling routine takes reference on the mli
497 * before calling this routine.
498 * Also if we are traversing mli_head, remember to check for
499 * mli list generation count and restart the loop if generation count
503 mld_dispatch_queue_locked(struct mld_ifinfo
*mli
, struct ifqueue
*ifq
, int limit
)
507 MLD_LOCK_ASSERT_HELD();
510 MLI_LOCK_ASSERT_HELD(mli
);
518 MLD_PRINTF(("%s: dispatch 0x%llx from 0x%llx\n", __func__
,
519 (uint64_t)VM_KERNEL_ADDRPERM(ifq
),
520 (uint64_t)VM_KERNEL_ADDRPERM(m
)));
527 mld_dispatch_packet(m
);
540 MLI_LOCK_ASSERT_HELD(mli
);
545 * Filter outgoing MLD report state by group.
547 * Reports are ALWAYS suppressed for ALL-HOSTS (ff02::1)
548 * and node-local addresses. However, kernel and socket consumers
549 * always embed the KAME scope ID in the address provided, so strip it
550 * when performing comparison.
551 * Note: This is not the same as the *multicast* scope.
553 * Return zero if the given group is one for which MLD reports
554 * should be suppressed, or non-zero if reports should be issued.
556 static __inline__
int
557 mld_is_addr_reported(const struct in6_addr
*addr
)
559 VERIFY(IN6_IS_ADDR_MULTICAST(addr
));
561 if (IPV6_ADDR_MC_SCOPE(addr
) == IPV6_ADDR_SCOPE_NODELOCAL
) {
565 if (IPV6_ADDR_MC_SCOPE(addr
) == IPV6_ADDR_SCOPE_LINKLOCAL
&& !IN6_IS_ADDR_UNICAST_BASED_MULTICAST(addr
)) {
566 struct in6_addr tmp
= *addr
;
567 in6_clearscope(&tmp
);
568 if (IN6_ARE_ADDR_EQUAL(&tmp
, &in6addr_linklocal_allnodes
)) {
577 * Attach MLD when PF_INET6 is attached to an interface.
580 mld_domifattach(struct ifnet
*ifp
, zalloc_flags_t how
)
582 struct mld_ifinfo
*mli
;
584 MLD_PRINTF(("%s: called for ifp 0x%llx(%s)\n", __func__
,
585 (uint64_t)VM_KERNEL_ADDRPERM(ifp
), if_name(ifp
)));
587 mli
= mli_alloc(how
);
595 mli_initvar(mli
, ifp
, 0);
596 mli
->mli_debug
|= IFD_ATTACHED
;
597 MLI_ADDREF_LOCKED(mli
); /* hold a reference for mli_head */
598 MLI_ADDREF_LOCKED(mli
); /* hold a reference for caller */
600 ifnet_lock_shared(ifp
);
601 mld6_initsilent(ifp
, mli
);
602 ifnet_lock_done(ifp
);
604 LIST_INSERT_HEAD(&mli_head
, mli
, mli_link
);
605 mld_mli_list_genid
++;
609 MLD_PRINTF(("%s: allocate mld_ifinfo for ifp 0x%llx(%s)\n",
610 __func__
, (uint64_t)VM_KERNEL_ADDRPERM(ifp
), if_name(ifp
)));
616 * Attach MLD when PF_INET6 is reattached to an interface. Caller is
617 * expected to have an outstanding reference to the mli.
620 mld_domifreattach(struct mld_ifinfo
*mli
)
627 VERIFY(!(mli
->mli_debug
& IFD_ATTACHED
));
630 mli_initvar(mli
, ifp
, 1);
631 mli
->mli_debug
|= IFD_ATTACHED
;
632 MLI_ADDREF_LOCKED(mli
); /* hold a reference for mli_head */
634 ifnet_lock_shared(ifp
);
635 mld6_initsilent(ifp
, mli
);
636 ifnet_lock_done(ifp
);
638 LIST_INSERT_HEAD(&mli_head
, mli
, mli_link
);
639 mld_mli_list_genid
++;
643 MLD_PRINTF(("%s: reattached mld_ifinfo for ifp 0x%llx(%s)\n",
644 __func__
, (uint64_t)VM_KERNEL_ADDRPERM(ifp
), if_name(ifp
)));
648 * Hook for domifdetach.
651 mld_domifdetach(struct ifnet
*ifp
)
653 SLIST_HEAD(, in6_multi
) in6m_dthead
;
655 SLIST_INIT(&in6m_dthead
);
657 MLD_PRINTF(("%s: called for ifp 0x%llx(%s)\n", __func__
,
658 (uint64_t)VM_KERNEL_ADDRPERM(ifp
), if_name(ifp
)));
661 mli_delete(ifp
, (struct mld_in6m_relhead
*)&in6m_dthead
);
664 /* Now that we're dropped all locks, release detached records */
665 MLD_REMOVE_DETACHED_IN6M(&in6m_dthead
);
669 * Called at interface detach time. Note that we only flush all deferred
670 * responses and record releases; all remaining inm records and their source
671 * entries related to this interface are left intact, in order to handle
675 mli_delete(const struct ifnet
*ifp
, struct mld_in6m_relhead
*in6m_dthead
)
677 struct mld_ifinfo
*mli
, *tmli
;
679 MLD_LOCK_ASSERT_HELD();
681 LIST_FOREACH_SAFE(mli
, &mli_head
, mli_link
, tmli
) {
683 if (mli
->mli_ifp
== ifp
) {
685 * Free deferred General Query responses.
687 IF_DRAIN(&mli
->mli_gq
);
688 IF_DRAIN(&mli
->mli_v1q
);
689 mld_flush_relq(mli
, in6m_dthead
);
690 VERIFY(SLIST_EMPTY(&mli
->mli_relinmhead
));
691 mli
->mli_debug
&= ~IFD_ATTACHED
;
694 LIST_REMOVE(mli
, mli_link
);
695 MLI_REMREF(mli
); /* release mli_head reference */
696 mld_mli_list_genid
++;
701 panic("%s: mld_ifinfo not found for ifp %p(%s)\n", __func__
,
705 __private_extern__
void
706 mld6_initsilent(struct ifnet
*ifp
, struct mld_ifinfo
*mli
)
708 ifnet_lock_assert(ifp
, IFNET_LCK_ASSERT_OWNED
);
710 MLI_LOCK_ASSERT_NOTHELD(mli
);
712 if (!(ifp
->if_flags
& IFF_MULTICAST
) &&
713 (ifp
->if_eflags
& (IFEF_IPV6_ND6ALT
| IFEF_LOCALNET_PRIVATE
))) {
714 mli
->mli_flags
|= MLIF_SILENT
;
716 mli
->mli_flags
&= ~MLIF_SILENT
;
722 mli_initvar(struct mld_ifinfo
*mli
, struct ifnet
*ifp
, int reattach
)
724 MLI_LOCK_ASSERT_HELD(mli
);
728 mli
->mli_version
= MLD_VERSION_2
;
730 mli
->mli_version
= MLD_VERSION_1
;
733 mli
->mli_rv
= MLD_RV_INIT
;
734 mli
->mli_qi
= MLD_QI_INIT
;
735 mli
->mli_qri
= MLD_QRI_INIT
;
736 mli
->mli_uri
= MLD_URI_INIT
;
739 mli
->mli_flags
|= MLIF_USEALLOW
;
742 SLIST_INIT(&mli
->mli_relinmhead
);
746 * Responses to general queries are subject to bounds.
748 mli
->mli_gq
.ifq_maxlen
= MLD_MAX_RESPONSE_PACKETS
;
749 mli
->mli_v1q
.ifq_maxlen
= MLD_MAX_RESPONSE_PACKETS
;
752 static struct mld_ifinfo
*
753 mli_alloc(zalloc_flags_t how
)
755 struct mld_ifinfo
*mli
= zalloc_flags(mli_zone
, how
| Z_ZERO
);
757 lck_mtx_init(&mli
->mli_lock
, mld_mtx_grp
, mld_mtx_attr
);
758 mli
->mli_debug
|= IFD_ALLOC
;
764 mli_free(struct mld_ifinfo
*mli
)
767 if (mli
->mli_debug
& IFD_ATTACHED
) {
768 panic("%s: attached mli=%p is being freed", __func__
, mli
);
770 } else if (mli
->mli_ifp
!= NULL
) {
771 panic("%s: ifp not NULL for mli=%p", __func__
, mli
);
773 } else if (!(mli
->mli_debug
& IFD_ALLOC
)) {
774 panic("%s: mli %p cannot be freed", __func__
, mli
);
776 } else if (mli
->mli_refcnt
!= 0) {
777 panic("%s: non-zero refcnt mli=%p", __func__
, mli
);
780 mli
->mli_debug
&= ~IFD_ALLOC
;
783 lck_mtx_destroy(&mli
->mli_lock
, mld_mtx_grp
);
784 zfree(mli_zone
, mli
);
788 mli_addref(struct mld_ifinfo
*mli
, int locked
)
793 MLI_LOCK_ASSERT_HELD(mli
);
796 if (++mli
->mli_refcnt
== 0) {
797 panic("%s: mli=%p wraparound refcnt", __func__
, mli
);
806 mli_remref(struct mld_ifinfo
*mli
)
808 SLIST_HEAD(, in6_multi
) in6m_dthead
;
813 if (mli
->mli_refcnt
== 0) {
814 panic("%s: mli=%p negative refcnt", __func__
, mli
);
819 if (mli
->mli_refcnt
> 0) {
826 IF_DRAIN(&mli
->mli_gq
);
827 IF_DRAIN(&mli
->mli_v1q
);
828 SLIST_INIT(&in6m_dthead
);
829 mld_flush_relq(mli
, (struct mld_in6m_relhead
*)&in6m_dthead
);
830 VERIFY(SLIST_EMPTY(&mli
->mli_relinmhead
));
833 /* Now that we're dropped all locks, release detached records */
834 MLD_REMOVE_DETACHED_IN6M(&in6m_dthead
);
836 MLD_PRINTF(("%s: freeing mld_ifinfo for ifp 0x%llx(%s)\n",
837 __func__
, (uint64_t)VM_KERNEL_ADDRPERM(ifp
), if_name(ifp
)));
843 * Process a received MLDv1 general or address-specific query.
844 * Assumes that the query header has been pulled up to sizeof(mld_hdr).
846 * NOTE: Can't be fully const correct as we temporarily embed scope ID in
847 * mld_addr. This is OK as we own the mbuf chain.
850 mld_v1_input_query(struct ifnet
*ifp
, const struct ip6_hdr
*ip6
,
851 /*const*/ struct mld_hdr
*mld
)
853 struct mld_ifinfo
*mli
;
854 struct in6_multi
*inm
;
855 int err
= 0, is_general_query
;
857 struct mld_tparams mtp
= { .qpt
= 0, .it
= 0, .cst
= 0, .sct
= 0 };
859 MLD_LOCK_ASSERT_NOTHELD();
861 is_general_query
= 0;
864 MLD_PRINTF(("%s: ignore v1 query %s on ifp 0x%llx(%s)\n",
865 __func__
, ip6_sprintf(&mld
->mld_addr
),
866 (uint64_t)VM_KERNEL_ADDRPERM(ifp
), if_name(ifp
)));
871 * RFC3810 Section 6.2: MLD queries must originate from
872 * a router's link-local address.
874 if (!IN6_IS_SCOPE_LINKLOCAL(&ip6
->ip6_src
)) {
875 MLD_PRINTF(("%s: ignore v1 query src %s on ifp 0x%llx(%s)\n",
876 __func__
, ip6_sprintf(&ip6
->ip6_src
),
877 (uint64_t)VM_KERNEL_ADDRPERM(ifp
), if_name(ifp
)));
882 * Do address field validation upfront before we accept
885 if (IN6_IS_ADDR_UNSPECIFIED(&mld
->mld_addr
)) {
887 * MLDv1 General Query.
888 * If this was not sent to the all-nodes group, ignore it.
893 in6_clearscope(&dst
);
894 if (!IN6_ARE_ADDR_EQUAL(&dst
, &in6addr_linklocal_allnodes
)) {
898 is_general_query
= 1;
901 * Embed scope ID of receiving interface in MLD query for
902 * lookup whilst we don't hold other locks.
904 (void)in6_setscope(&mld
->mld_addr
, ifp
, NULL
);
908 * Switch to MLDv1 host compatibility mode.
910 mli
= MLD_IFINFO(ifp
);
914 mtp
.qpt
= mld_set_version(mli
, MLD_VERSION_1
);
917 timer
= ntohs(mld
->mld_maxdelay
) / MLD_TIMER_SCALE
;
922 if (is_general_query
) {
923 struct in6_multistep step
;
925 MLD_PRINTF(("%s: process v1 general query on ifp 0x%llx(%s)\n",
926 __func__
, (uint64_t)VM_KERNEL_ADDRPERM(ifp
), if_name(ifp
)));
928 * For each reporting group joined on this
929 * interface, kick the report timer.
931 in6_multihead_lock_shared();
932 IN6_FIRST_MULTI(step
, inm
);
933 while (inm
!= NULL
) {
935 if (inm
->in6m_ifp
== ifp
) {
936 mtp
.cst
+= mld_v1_update_group(inm
, timer
);
939 IN6_NEXT_MULTI(step
, inm
);
941 in6_multihead_lock_done();
944 * MLDv1 Group-Specific Query.
945 * If this is a group-specific MLDv1 query, we need only
946 * look up the single group to process it.
948 in6_multihead_lock_shared();
949 IN6_LOOKUP_MULTI(&mld
->mld_addr
, ifp
, inm
);
950 in6_multihead_lock_done();
954 MLD_PRINTF(("%s: process v1 query %s on "
955 "ifp 0x%llx(%s)\n", __func__
,
956 ip6_sprintf(&mld
->mld_addr
),
957 (uint64_t)VM_KERNEL_ADDRPERM(ifp
), if_name(ifp
)));
958 mtp
.cst
= mld_v1_update_group(inm
, timer
);
960 IN6M_REMREF(inm
); /* from IN6_LOOKUP_MULTI */
962 /* XXX Clear embedded scope ID as userland won't expect it. */
963 in6_clearscope(&mld
->mld_addr
);
966 mld_set_timeout(&mtp
);
972 * Update the report timer on a group in response to an MLDv1 query.
974 * If we are becoming the reporting member for this group, start the timer.
975 * If we already are the reporting member for this group, and timer is
976 * below the threshold, reset it.
978 * We may be updating the group for the first time since we switched
979 * to MLDv2. If we are, then we must clear any recorded source lists,
980 * and transition to REPORTING state; the group timer is overloaded
981 * for group and group-source query responses.
983 * Unlike MLDv2, the delay per group should be jittered
984 * to avoid bursts of MLDv1 reports.
987 mld_v1_update_group(struct in6_multi
*inm
, const int timer
)
989 IN6M_LOCK_ASSERT_HELD(inm
);
991 MLD_PRINTF(("%s: %s/%s timer=%d\n", __func__
,
992 ip6_sprintf(&inm
->in6m_addr
),
993 if_name(inm
->in6m_ifp
), timer
));
995 switch (inm
->in6m_state
) {
997 case MLD_SILENT_MEMBER
:
999 case MLD_REPORTING_MEMBER
:
1000 if (inm
->in6m_timer
!= 0 &&
1001 inm
->in6m_timer
<= timer
) {
1002 MLD_PRINTF(("%s: REPORTING and timer running, "
1003 "skipping.\n", __func__
));
1007 case MLD_SG_QUERY_PENDING_MEMBER
:
1008 case MLD_G_QUERY_PENDING_MEMBER
:
1009 case MLD_IDLE_MEMBER
:
1010 case MLD_LAZY_MEMBER
:
1011 case MLD_AWAKENING_MEMBER
:
1012 MLD_PRINTF(("%s: ->REPORTING\n", __func__
));
1013 inm
->in6m_state
= MLD_REPORTING_MEMBER
;
1014 inm
->in6m_timer
= MLD_RANDOM_DELAY(timer
);
1016 case MLD_SLEEPING_MEMBER
:
1017 MLD_PRINTF(("%s: ->AWAKENING\n", __func__
));
1018 inm
->in6m_state
= MLD_AWAKENING_MEMBER
;
1020 case MLD_LEAVING_MEMBER
:
1024 return inm
->in6m_timer
;
1028 * Process a received MLDv2 general, group-specific or
1029 * group-and-source-specific query.
1031 * Assumes that the query header has been pulled up to sizeof(mldv2_query).
1033 * Return 0 if successful, otherwise an appropriate error code is returned.
1036 mld_v2_input_query(struct ifnet
*ifp
, const struct ip6_hdr
*ip6
,
1037 struct mbuf
*m
, const int off
, const int icmp6len
)
1039 struct mld_ifinfo
*mli
;
1040 struct mldv2_query
*mld
;
1041 struct in6_multi
*inm
;
1042 uint32_t maxdelay
, nsrc
, qqi
, timer
;
1043 int err
= 0, is_general_query
;
1045 struct mld_tparams mtp
= { .qpt
= 0, .it
= 0, .cst
= 0, .sct
= 0 };
1047 MLD_LOCK_ASSERT_NOTHELD();
1049 is_general_query
= 0;
1051 if (!mld_v2enable
) {
1052 MLD_PRINTF(("%s: ignore v2 query %s on ifp 0x%llx(%s)\n",
1053 __func__
, ip6_sprintf(&ip6
->ip6_src
),
1054 (uint64_t)VM_KERNEL_ADDRPERM(ifp
), if_name(ifp
)));
1059 * RFC3810 Section 6.2: MLD queries must originate from
1060 * a router's link-local address.
1062 if (!IN6_IS_SCOPE_LINKLOCAL(&ip6
->ip6_src
)) {
1063 MLD_PRINTF(("%s: ignore v1 query src %s on ifp 0x%llx(%s)\n",
1064 __func__
, ip6_sprintf(&ip6
->ip6_src
),
1065 (uint64_t)VM_KERNEL_ADDRPERM(ifp
), if_name(ifp
)));
1069 MLD_PRINTF(("%s: input v2 query on ifp 0x%llx(%s)\n", __func__
,
1070 (uint64_t)VM_KERNEL_ADDRPERM(ifp
), if_name(ifp
)));
1072 mld
= (struct mldv2_query
*)(mtod(m
, uint8_t *) + off
);
1074 maxdelay
= ntohs(mld
->mld_maxdelay
); /* in 1/10ths of a second */
1075 if (maxdelay
> SHRT_MAX
) {
1076 maxdelay
= (MLD_MRC_MANT((uint16_t)maxdelay
) | 0x1000) <<
1077 (MLD_MRC_EXP((uint16_t)maxdelay
) + 3);
1079 timer
= maxdelay
/ MLD_TIMER_SCALE
;
1084 qrv
= MLD_QRV(mld
->mld_misc
);
1086 MLD_PRINTF(("%s: clamping qrv %d to %d\n", __func__
,
1093 qqi
= MLD_QQIC_MANT(mld
->mld_qqi
) <<
1094 (MLD_QQIC_EXP(mld
->mld_qqi
) + 3);
1097 nsrc
= ntohs(mld
->mld_numsrc
);
1098 if (nsrc
> MLD_MAX_GS_SOURCES
) {
1102 if (icmp6len
< sizeof(struct mldv2_query
) +
1103 (nsrc
* sizeof(struct in6_addr
))) {
1109 * Do further input validation upfront to avoid resetting timers
1110 * should we need to discard this query.
1112 if (IN6_IS_ADDR_UNSPECIFIED(&mld
->mld_addr
)) {
1114 * A general query with a source list has undefined
1115 * behaviour; discard it.
1121 is_general_query
= 1;
1124 * Embed scope ID of receiving interface in MLD query for
1125 * lookup whilst we don't hold other locks (due to KAME
1126 * locking lameness). We own this mbuf chain just now.
1128 (void)in6_setscope(&mld
->mld_addr
, ifp
, NULL
);
1131 mli
= MLD_IFINFO(ifp
);
1132 VERIFY(mli
!= NULL
);
1136 * Discard the v2 query if we're in Compatibility Mode.
1137 * The RFC is pretty clear that hosts need to stay in MLDv1 mode
1138 * until the Old Version Querier Present timer expires.
1140 if (mli
->mli_version
!= MLD_VERSION_2
) {
1145 mtp
.qpt
= mld_set_version(mli
, MLD_VERSION_2
);
1148 mli
->mli_qri
= MAX(timer
, MLD_QRI_MIN
);
1150 MLD_PRINTF(("%s: qrv %d qi %d qri %d\n", __func__
, mli
->mli_rv
,
1151 mli
->mli_qi
, mli
->mli_qri
));
1153 if (is_general_query
) {
1155 * MLDv2 General Query.
1157 * Schedule a current-state report on this ifp for
1158 * all groups, possibly containing source lists.
1160 * If there is a pending General Query response
1161 * scheduled earlier than the selected delay, do
1162 * not schedule any other reports.
1163 * Otherwise, reset the interface timer.
1165 MLD_PRINTF(("%s: process v2 general query on ifp 0x%llx(%s)\n",
1166 __func__
, (uint64_t)VM_KERNEL_ADDRPERM(ifp
), if_name(ifp
)));
1167 if (mli
->mli_v2_timer
== 0 || mli
->mli_v2_timer
>= timer
) {
1168 mtp
.it
= mli
->mli_v2_timer
= MLD_RANDOM_DELAY(timer
);
1174 * MLDv2 Group-specific or Group-and-source-specific Query.
1176 * Group-source-specific queries are throttled on
1177 * a per-group basis to defeat denial-of-service attempts.
1178 * Queries for groups we are not a member of on this
1179 * link are simply ignored.
1181 in6_multihead_lock_shared();
1182 IN6_LOOKUP_MULTI(&mld
->mld_addr
, ifp
, inm
);
1183 in6_multihead_lock_done();
1190 if (!ratecheck(&inm
->in6m_lastgsrtv
,
1192 MLD_PRINTF(("%s: GS query throttled.\n",
1195 IN6M_REMREF(inm
); /* from IN6_LOOKUP_MULTI */
1199 MLD_PRINTF(("%s: process v2 group query on ifp 0x%llx(%s)\n",
1200 __func__
, (uint64_t)VM_KERNEL_ADDRPERM(ifp
), if_name(ifp
)));
1202 * If there is a pending General Query response
1203 * scheduled sooner than the selected delay, no
1204 * further report need be scheduled.
1205 * Otherwise, prepare to respond to the
1206 * group-specific or group-and-source query.
1209 mtp
.it
= mli
->mli_v2_timer
;
1211 if (mtp
.it
== 0 || mtp
.it
>= timer
) {
1212 (void) mld_v2_process_group_query(inm
, timer
, m
, off
);
1213 mtp
.cst
= inm
->in6m_timer
;
1216 IN6M_REMREF(inm
); /* from IN6_LOOKUP_MULTI */
1217 /* XXX Clear embedded scope ID as userland won't expect it. */
1218 in6_clearscope(&mld
->mld_addr
);
1222 MLD_PRINTF(("%s: v2 general query response scheduled in "
1223 "T+%d seconds on ifp 0x%llx(%s)\n", __func__
, mtp
.it
,
1224 (uint64_t)VM_KERNEL_ADDRPERM(ifp
), if_name(ifp
)));
1226 mld_set_timeout(&mtp
);
1232 * Process a recieved MLDv2 group-specific or group-and-source-specific
1234 * Return <0 if any error occured. Currently this is ignored.
1237 mld_v2_process_group_query(struct in6_multi
*inm
, int timer
, struct mbuf
*m0
,
1240 struct mldv2_query
*mld
;
1244 IN6M_LOCK_ASSERT_HELD(inm
);
1247 mld
= (struct mldv2_query
*)(mtod(m0
, uint8_t *) + off
);
1249 switch (inm
->in6m_state
) {
1250 case MLD_NOT_MEMBER
:
1251 case MLD_SILENT_MEMBER
:
1252 case MLD_SLEEPING_MEMBER
:
1253 case MLD_LAZY_MEMBER
:
1254 case MLD_AWAKENING_MEMBER
:
1255 case MLD_IDLE_MEMBER
:
1256 case MLD_LEAVING_MEMBER
:
1258 case MLD_REPORTING_MEMBER
:
1259 case MLD_G_QUERY_PENDING_MEMBER
:
1260 case MLD_SG_QUERY_PENDING_MEMBER
:
1264 nsrc
= ntohs(mld
->mld_numsrc
);
1267 * Deal with group-specific queries upfront.
1268 * If any group query is already pending, purge any recorded
1269 * source-list state if it exists, and schedule a query response
1270 * for this group-specific query.
1273 if (inm
->in6m_state
== MLD_G_QUERY_PENDING_MEMBER
||
1274 inm
->in6m_state
== MLD_SG_QUERY_PENDING_MEMBER
) {
1275 in6m_clear_recorded(inm
);
1276 timer
= min(inm
->in6m_timer
, timer
);
1278 inm
->in6m_state
= MLD_G_QUERY_PENDING_MEMBER
;
1279 inm
->in6m_timer
= MLD_RANDOM_DELAY(timer
);
1284 * Deal with the case where a group-and-source-specific query has
1285 * been received but a group-specific query is already pending.
1287 if (inm
->in6m_state
== MLD_G_QUERY_PENDING_MEMBER
) {
1288 timer
= min(inm
->in6m_timer
, timer
);
1289 inm
->in6m_timer
= MLD_RANDOM_DELAY(timer
);
1294 * Finally, deal with the case where a group-and-source-specific
1295 * query has been received, where a response to a previous g-s-r
1296 * query exists, or none exists.
1297 * In this case, we need to parse the source-list which the Querier
1298 * has provided us with and check if we have any source list filter
1299 * entries at T1 for these sources. If we do not, there is no need
1300 * schedule a report and the query may be dropped.
1301 * If we do, we must record them and schedule a current-state
1302 * report for those sources.
1304 if (inm
->in6m_nsrc
> 0) {
1311 soff
= off
+ sizeof(struct mldv2_query
);
1313 for (i
= 0; i
< nsrc
; i
++) {
1314 sp
= mtod(m
, uint8_t *) + soff
;
1315 retval
= in6m_record_source(inm
,
1316 (const struct in6_addr
*)(void *)sp
);
1320 nrecorded
+= retval
;
1321 soff
+= sizeof(struct in6_addr
);
1322 if (soff
>= m
->m_len
) {
1323 soff
= soff
- m
->m_len
;
1330 if (nrecorded
> 0) {
1331 MLD_PRINTF(("%s: schedule response to SG query\n",
1333 inm
->in6m_state
= MLD_SG_QUERY_PENDING_MEMBER
;
1334 inm
->in6m_timer
= MLD_RANDOM_DELAY(timer
);
1342 * Process a received MLDv1 host membership report.
1343 * Assumes mld points to mld_hdr in pulled up mbuf chain.
1345 * NOTE: Can't be fully const correct as we temporarily embed scope ID in
1346 * mld_addr. This is OK as we own the mbuf chain.
1349 mld_v1_input_report(struct ifnet
*ifp
, struct mbuf
*m
,
1350 const struct ip6_hdr
*ip6
, /*const*/ struct mld_hdr
*mld
)
1352 struct in6_addr src
, dst
;
1353 struct in6_ifaddr
*ia
;
1354 struct in6_multi
*inm
;
1356 if (!mld_v1enable
) {
1357 MLD_PRINTF(("%s: ignore v1 report %s on ifp 0x%llx(%s)\n",
1358 __func__
, ip6_sprintf(&mld
->mld_addr
),
1359 (uint64_t)VM_KERNEL_ADDRPERM(ifp
), if_name(ifp
)));
1363 if ((ifp
->if_flags
& IFF_LOOPBACK
) ||
1364 (m
->m_pkthdr
.pkt_flags
& PKTF_LOOP
)) {
1369 * MLDv1 reports must originate from a host's link-local address,
1370 * or the unspecified address (when booting).
1373 in6_clearscope(&src
);
1374 if (!IN6_IS_SCOPE_LINKLOCAL(&src
) && !IN6_IS_ADDR_UNSPECIFIED(&src
)) {
1375 MLD_PRINTF(("%s: ignore v1 query src %s on ifp 0x%llx(%s)\n",
1376 __func__
, ip6_sprintf(&ip6
->ip6_src
),
1377 (uint64_t)VM_KERNEL_ADDRPERM(ifp
), if_name(ifp
)));
1382 * RFC2710 Section 4: MLDv1 reports must pertain to a multicast
1383 * group, and must be directed to the group itself.
1386 in6_clearscope(&dst
);
1387 if (!IN6_IS_ADDR_MULTICAST(&mld
->mld_addr
) ||
1388 !IN6_ARE_ADDR_EQUAL(&mld
->mld_addr
, &dst
)) {
1389 MLD_PRINTF(("%s: ignore v1 query dst %s on ifp 0x%llx(%s)\n",
1390 __func__
, ip6_sprintf(&ip6
->ip6_dst
),
1391 (uint64_t)VM_KERNEL_ADDRPERM(ifp
), if_name(ifp
)));
1396 * Make sure we don't hear our own membership report, as fast
1397 * leave requires knowing that we are the only member of a
1398 * group. Assume we used the link-local address if available,
1399 * otherwise look for ::.
1401 * XXX Note that scope ID comparison is needed for the address
1402 * returned by in6ifa_ifpforlinklocal(), but SHOULD NOT be
1403 * performed for the on-wire address.
1405 ia
= in6ifa_ifpforlinklocal(ifp
, IN6_IFF_NOTREADY
| IN6_IFF_ANYCAST
);
1407 IFA_LOCK(&ia
->ia_ifa
);
1408 if ((IN6_ARE_ADDR_EQUAL(&ip6
->ip6_src
, IA6_IN6(ia
)))) {
1409 IFA_UNLOCK(&ia
->ia_ifa
);
1410 IFA_REMREF(&ia
->ia_ifa
);
1413 IFA_UNLOCK(&ia
->ia_ifa
);
1414 IFA_REMREF(&ia
->ia_ifa
);
1415 } else if (IN6_IS_ADDR_UNSPECIFIED(&src
)) {
1419 MLD_PRINTF(("%s: process v1 report %s on ifp 0x%llx(%s)\n",
1420 __func__
, ip6_sprintf(&mld
->mld_addr
),
1421 (uint64_t)VM_KERNEL_ADDRPERM(ifp
), if_name(ifp
)));
1424 * Embed scope ID of receiving interface in MLD query for lookup
1425 * whilst we don't hold other locks (due to KAME locking lameness).
1427 if (!IN6_IS_ADDR_UNSPECIFIED(&mld
->mld_addr
)) {
1428 (void)in6_setscope(&mld
->mld_addr
, ifp
, NULL
);
1432 * MLDv1 report suppression.
1433 * If we are a member of this group, and our membership should be
1434 * reported, and our group timer is pending or about to be reset,
1435 * stop our group timer by transitioning to the 'lazy' state.
1437 in6_multihead_lock_shared();
1438 IN6_LOOKUP_MULTI(&mld
->mld_addr
, ifp
, inm
);
1439 in6_multihead_lock_done();
1442 struct mld_ifinfo
*mli
;
1445 mli
= inm
->in6m_mli
;
1446 VERIFY(mli
!= NULL
);
1450 * If we are in MLDv2 host mode, do not allow the
1451 * other host's MLDv1 report to suppress our reports.
1453 if (mli
->mli_version
== MLD_VERSION_2
) {
1456 IN6M_REMREF(inm
); /* from IN6_LOOKUP_MULTI */
1461 inm
->in6m_timer
= 0;
1463 switch (inm
->in6m_state
) {
1464 case MLD_NOT_MEMBER
:
1465 case MLD_SILENT_MEMBER
:
1466 case MLD_SLEEPING_MEMBER
:
1468 case MLD_REPORTING_MEMBER
:
1469 case MLD_IDLE_MEMBER
:
1470 case MLD_AWAKENING_MEMBER
:
1471 MLD_PRINTF(("%s: report suppressed for %s on "
1472 "ifp 0x%llx(%s)\n", __func__
,
1473 ip6_sprintf(&mld
->mld_addr
),
1474 (uint64_t)VM_KERNEL_ADDRPERM(ifp
), if_name(ifp
)));
1476 case MLD_LAZY_MEMBER
:
1477 inm
->in6m_state
= MLD_LAZY_MEMBER
;
1479 case MLD_G_QUERY_PENDING_MEMBER
:
1480 case MLD_SG_QUERY_PENDING_MEMBER
:
1481 case MLD_LEAVING_MEMBER
:
1485 IN6M_REMREF(inm
); /* from IN6_LOOKUP_MULTI */
1489 /* XXX Clear embedded scope ID as userland won't expect it. */
1490 in6_clearscope(&mld
->mld_addr
);
1498 * Assume query messages which fit in a single ICMPv6 message header
1499 * have been pulled up.
1500 * Assume that userland will want to see the message, even if it
1501 * otherwise fails kernel input validation; do not free it.
1502 * Pullup may however free the mbuf chain m if it fails.
1504 * Return IPPROTO_DONE if we freed m. Otherwise, return 0.
1507 mld_input(struct mbuf
*m
, int off
, int icmp6len
)
1509 struct ifnet
*ifp
= NULL
;
1510 struct ip6_hdr
*ip6
= NULL
;
1511 struct mld_hdr
*mld
= NULL
;
1514 MLD_PRINTF(("%s: called w/mbuf (0x%llx,%d)\n", __func__
,
1515 (uint64_t)VM_KERNEL_ADDRPERM(m
), off
));
1517 ifp
= m
->m_pkthdr
.rcvif
;
1519 /* Pullup to appropriate size. */
1520 mld
= (struct mld_hdr
*)(mtod(m
, uint8_t *) + off
);
1521 if (mld
->mld_type
== MLD_LISTENER_QUERY
&&
1522 icmp6len
>= sizeof(struct mldv2_query
)) {
1523 mldlen
= sizeof(struct mldv2_query
);
1525 mldlen
= sizeof(struct mld_hdr
);
1527 // check if mldv2_query/mld_hdr fits in the first mbuf
1528 IP6_EXTHDR_CHECK(m
, off
, mldlen
, return IPPROTO_DONE
);
1529 IP6_EXTHDR_GET(mld
, struct mld_hdr
*, m
, off
, mldlen
);
1531 icmp6stat
.icp6s_badlen
++;
1532 return IPPROTO_DONE
;
1534 ip6
= mtod(m
, struct ip6_hdr
*);
1537 * Userland needs to see all of this traffic for implementing
1538 * the endpoint discovery portion of multicast routing.
1540 switch (mld
->mld_type
) {
1541 case MLD_LISTENER_QUERY
:
1542 icmp6_ifstat_inc(ifp
, ifs6_in_mldquery
);
1543 if (icmp6len
== sizeof(struct mld_hdr
)) {
1544 if (mld_v1_input_query(ifp
, ip6
, mld
) != 0) {
1547 } else if (icmp6len
>= sizeof(struct mldv2_query
)) {
1548 if (mld_v2_input_query(ifp
, ip6
, m
, off
,
1554 case MLD_LISTENER_REPORT
:
1555 icmp6_ifstat_inc(ifp
, ifs6_in_mldreport
);
1556 if (mld_v1_input_report(ifp
, m
, ip6
, mld
) != 0) {
1560 case MLDV2_LISTENER_REPORT
:
1561 icmp6_ifstat_inc(ifp
, ifs6_in_mldreport
);
1563 case MLD_LISTENER_DONE
:
1564 icmp6_ifstat_inc(ifp
, ifs6_in_mlddone
);
1574 * Schedule MLD timer based on various parameters; caller must ensure that
1575 * lock ordering is maintained as this routine acquires MLD global lock.
1578 mld_set_timeout(struct mld_tparams
*mtp
)
1580 MLD_LOCK_ASSERT_NOTHELD();
1581 VERIFY(mtp
!= NULL
);
1583 if (mtp
->qpt
!= 0 || mtp
->it
!= 0 || mtp
->cst
!= 0 || mtp
->sct
!= 0) {
1585 if (mtp
->qpt
!= 0) {
1586 querier_present_timers_running6
= 1;
1589 interface_timers_running6
= 1;
1591 if (mtp
->cst
!= 0) {
1592 current_state_timers_running6
= 1;
1594 if (mtp
->sct
!= 0) {
1595 state_change_timers_running6
= 1;
1597 mld_sched_timeout();
1603 * MLD6 timer handler (per 1 second).
1606 mld_timeout(void *arg
)
1609 struct ifqueue scq
; /* State-change packets */
1610 struct ifqueue qrq
; /* Query response packets */
1612 struct mld_ifinfo
*mli
;
1613 struct in6_multi
*inm
;
1615 unsigned int genid
= mld_mli_list_genid
;
1617 SLIST_HEAD(, in6_multi
) in6m_dthead
;
1619 SLIST_INIT(&in6m_dthead
);
1622 * Update coarse-grained networking timestamp (in sec.); the idea
1623 * is to piggy-back on the timeout callout to update the counter
1624 * returnable via net_uptime().
1626 net_update_uptime();
1630 MLD_PRINTF(("%s: qpt %d, it %d, cst %d, sct %d\n", __func__
,
1631 querier_present_timers_running6
, interface_timers_running6
,
1632 current_state_timers_running6
, state_change_timers_running6
));
1635 * MLDv1 querier present timer processing.
1637 if (querier_present_timers_running6
) {
1638 querier_present_timers_running6
= 0;
1639 LIST_FOREACH(mli
, &mli_head
, mli_link
) {
1641 mld_v1_process_querier_timers(mli
);
1642 if (mli
->mli_v1_timer
> 0) {
1643 querier_present_timers_running6
= 1;
1650 * MLDv2 General Query response timer processing.
1652 if (interface_timers_running6
) {
1653 MLD_PRINTF(("%s: interface timers running\n", __func__
));
1654 interface_timers_running6
= 0;
1655 mli
= LIST_FIRST(&mli_head
);
1657 while (mli
!= NULL
) {
1658 if (mli
->mli_flags
& MLIF_PROCESSED
) {
1659 mli
= LIST_NEXT(mli
, mli_link
);
1664 if (mli
->mli_version
!= MLD_VERSION_2
) {
1666 mli
= LIST_NEXT(mli
, mli_link
);
1670 * XXX The logic below ends up calling
1671 * mld_dispatch_packet which can unlock mli
1672 * and the global MLD lock.
1673 * Therefore grab a reference on MLI and also
1674 * check for generation count to see if we should
1675 * iterate the list again.
1677 MLI_ADDREF_LOCKED(mli
);
1679 if (mli
->mli_v2_timer
== 0) {
1681 } else if (--mli
->mli_v2_timer
== 0) {
1682 if (mld_v2_dispatch_general_query(mli
) > 0) {
1683 interface_timers_running6
= 1;
1686 interface_timers_running6
= 1;
1688 mli
->mli_flags
|= MLIF_PROCESSED
;
1692 if (genid
!= mld_mli_list_genid
) {
1693 MLD_PRINTF(("%s: MLD information list changed "
1694 "in the middle of iteration! Restart iteration.\n",
1696 mli
= LIST_FIRST(&mli_head
);
1697 genid
= mld_mli_list_genid
;
1699 mli
= LIST_NEXT(mli
, mli_link
);
1703 LIST_FOREACH(mli
, &mli_head
, mli_link
)
1704 mli
->mli_flags
&= ~MLIF_PROCESSED
;
1709 if (!current_state_timers_running6
&&
1710 !state_change_timers_running6
) {
1714 current_state_timers_running6
= 0;
1715 state_change_timers_running6
= 0;
1717 MLD_PRINTF(("%s: state change timers running\n", __func__
));
1719 memset(&qrq
, 0, sizeof(struct ifqueue
));
1720 qrq
.ifq_maxlen
= MLD_MAX_G_GS_PACKETS
;
1722 memset(&scq
, 0, sizeof(struct ifqueue
));
1723 scq
.ifq_maxlen
= MLD_MAX_STATE_CHANGE_PACKETS
;
1726 * MLD host report and state-change timer processing.
1727 * Note: Processing a v2 group timer may remove a node.
1729 mli
= LIST_FIRST(&mli_head
);
1731 while (mli
!= NULL
) {
1732 struct in6_multistep step
;
1734 if (mli
->mli_flags
& MLIF_PROCESSED
) {
1735 mli
= LIST_NEXT(mli
, mli_link
);
1741 uri_sec
= MLD_RANDOM_DELAY(mli
->mli_uri
);
1744 in6_multihead_lock_shared();
1745 IN6_FIRST_MULTI(step
, inm
);
1746 while (inm
!= NULL
) {
1748 if (inm
->in6m_ifp
!= ifp
) {
1753 switch (mli
->mli_version
) {
1755 mld_v1_process_group_timer(inm
,
1759 mld_v2_process_group_timers(mli
, &qrq
,
1760 &scq
, inm
, uri_sec
);
1766 IN6_NEXT_MULTI(step
, inm
);
1768 in6_multihead_lock_done();
1771 * XXX The logic below ends up calling
1772 * mld_dispatch_packet which can unlock mli
1773 * and the global MLD lock.
1774 * Therefore grab a reference on MLI and also
1775 * check for generation count to see if we should
1776 * iterate the list again.
1779 MLI_ADDREF_LOCKED(mli
);
1780 if (mli
->mli_version
== MLD_VERSION_1
) {
1781 mld_dispatch_queue_locked(mli
, &mli
->mli_v1q
, 0);
1782 } else if (mli
->mli_version
== MLD_VERSION_2
) {
1784 mld_dispatch_queue_locked(NULL
, &qrq
, 0);
1785 mld_dispatch_queue_locked(NULL
, &scq
, 0);
1786 VERIFY(qrq
.ifq_len
== 0);
1787 VERIFY(scq
.ifq_len
== 0);
1791 * In case there are still any pending membership reports
1792 * which didn't get drained at version change time.
1794 IF_DRAIN(&mli
->mli_v1q
);
1796 * Release all deferred inm records, and drain any locally
1797 * enqueued packets; do it even if the current MLD version
1798 * for the link is no longer MLDv2, in order to handle the
1799 * version change case.
1801 mld_flush_relq(mli
, (struct mld_in6m_relhead
*)&in6m_dthead
);
1802 VERIFY(SLIST_EMPTY(&mli
->mli_relinmhead
));
1803 mli
->mli_flags
|= MLIF_PROCESSED
;
1810 if (genid
!= mld_mli_list_genid
) {
1811 MLD_PRINTF(("%s: MLD information list changed "
1812 "in the middle of iteration! Restart iteration.\n",
1814 mli
= LIST_FIRST(&mli_head
);
1815 genid
= mld_mli_list_genid
;
1817 mli
= LIST_NEXT(mli
, mli_link
);
1821 LIST_FOREACH(mli
, &mli_head
, mli_link
)
1822 mli
->mli_flags
&= ~MLIF_PROCESSED
;
1825 /* re-arm the timer if there's work to do */
1826 mld_timeout_run
= 0;
1827 mld_sched_timeout();
1830 /* Now that we're dropped all locks, release detached records */
1831 MLD_REMOVE_DETACHED_IN6M(&in6m_dthead
);
1835 mld_sched_timeout(void)
1837 MLD_LOCK_ASSERT_HELD();
1839 if (!mld_timeout_run
&&
1840 (querier_present_timers_running6
|| current_state_timers_running6
||
1841 interface_timers_running6
|| state_change_timers_running6
)) {
1842 mld_timeout_run
= 1;
1843 timeout(mld_timeout
, NULL
, hz
);
1848 * Free the in6_multi reference(s) for this MLD lifecycle.
1850 * Caller must be holding mli_lock.
1853 mld_flush_relq(struct mld_ifinfo
*mli
, struct mld_in6m_relhead
*in6m_dthead
)
1855 struct in6_multi
*inm
;
1858 MLI_LOCK_ASSERT_HELD(mli
);
1859 inm
= SLIST_FIRST(&mli
->mli_relinmhead
);
1863 SLIST_REMOVE_HEAD(&mli
->mli_relinmhead
, in6m_nrele
);
1866 in6_multihead_lock_exclusive();
1868 VERIFY(inm
->in6m_nrelecnt
!= 0);
1869 inm
->in6m_nrelecnt
--;
1870 lastref
= in6_multi_detach(inm
);
1871 VERIFY(!lastref
|| (!(inm
->in6m_debug
& IFD_ATTACHED
) &&
1872 inm
->in6m_reqcnt
== 0));
1874 in6_multihead_lock_done();
1875 /* from mli_relinmhead */
1877 /* from in6_multihead_list */
1880 * Defer releasing our final reference, as we
1881 * are holding the MLD lock at this point, and
1882 * we could end up with locking issues later on
1883 * (while issuing SIOCDELMULTI) when this is the
1884 * final reference count. Let the caller do it
1887 MLD_ADD_DETACHED_IN6M(in6m_dthead
, inm
);
1895 * Update host report group timer.
1896 * Will update the global pending timer flags.
1899 mld_v1_process_group_timer(struct in6_multi
*inm
, const int mld_version
)
1901 #pragma unused(mld_version)
1902 int report_timer_expired
;
1904 MLD_LOCK_ASSERT_HELD();
1905 IN6M_LOCK_ASSERT_HELD(inm
);
1906 MLI_LOCK_ASSERT_HELD(inm
->in6m_mli
);
1908 if (inm
->in6m_timer
== 0) {
1909 report_timer_expired
= 0;
1910 } else if (--inm
->in6m_timer
== 0) {
1911 report_timer_expired
= 1;
1913 current_state_timers_running6
= 1;
1914 /* caller will schedule timer */
1918 switch (inm
->in6m_state
) {
1919 case MLD_NOT_MEMBER
:
1920 case MLD_SILENT_MEMBER
:
1921 case MLD_IDLE_MEMBER
:
1922 case MLD_LAZY_MEMBER
:
1923 case MLD_SLEEPING_MEMBER
:
1924 case MLD_AWAKENING_MEMBER
:
1926 case MLD_REPORTING_MEMBER
:
1927 if (report_timer_expired
) {
1928 inm
->in6m_state
= MLD_IDLE_MEMBER
;
1929 (void) mld_v1_transmit_report(inm
,
1930 MLD_LISTENER_REPORT
);
1931 IN6M_LOCK_ASSERT_HELD(inm
);
1932 MLI_LOCK_ASSERT_HELD(inm
->in6m_mli
);
1935 case MLD_G_QUERY_PENDING_MEMBER
:
1936 case MLD_SG_QUERY_PENDING_MEMBER
:
1937 case MLD_LEAVING_MEMBER
:
1943 * Update a group's timers for MLDv2.
1944 * Will update the global pending timer flags.
1945 * Note: Unlocked read from mli.
1948 mld_v2_process_group_timers(struct mld_ifinfo
*mli
,
1949 struct ifqueue
*qrq
, struct ifqueue
*scq
,
1950 struct in6_multi
*inm
, const int uri_sec
)
1952 int query_response_timer_expired
;
1953 int state_change_retransmit_timer_expired
;
1955 MLD_LOCK_ASSERT_HELD();
1956 IN6M_LOCK_ASSERT_HELD(inm
);
1957 MLI_LOCK_ASSERT_HELD(mli
);
1958 VERIFY(mli
== inm
->in6m_mli
);
1960 query_response_timer_expired
= 0;
1961 state_change_retransmit_timer_expired
= 0;
1964 * During a transition from compatibility mode back to MLDv2,
1965 * a group record in REPORTING state may still have its group
1966 * timer active. This is a no-op in this function; it is easier
1967 * to deal with it here than to complicate the timeout path.
1969 if (inm
->in6m_timer
== 0) {
1970 query_response_timer_expired
= 0;
1971 } else if (--inm
->in6m_timer
== 0) {
1972 query_response_timer_expired
= 1;
1974 current_state_timers_running6
= 1;
1975 /* caller will schedule timer */
1978 if (inm
->in6m_sctimer
== 0) {
1979 state_change_retransmit_timer_expired
= 0;
1980 } else if (--inm
->in6m_sctimer
== 0) {
1981 state_change_retransmit_timer_expired
= 1;
1983 state_change_timers_running6
= 1;
1984 /* caller will schedule timer */
1987 /* We are in timer callback, so be quick about it. */
1988 if (!state_change_retransmit_timer_expired
&&
1989 !query_response_timer_expired
) {
1993 switch (inm
->in6m_state
) {
1994 case MLD_NOT_MEMBER
:
1995 case MLD_SILENT_MEMBER
:
1996 case MLD_SLEEPING_MEMBER
:
1997 case MLD_LAZY_MEMBER
:
1998 case MLD_AWAKENING_MEMBER
:
1999 case MLD_IDLE_MEMBER
:
2001 case MLD_G_QUERY_PENDING_MEMBER
:
2002 case MLD_SG_QUERY_PENDING_MEMBER
:
2004 * Respond to a previously pending Group-Specific
2005 * or Group-and-Source-Specific query by enqueueing
2006 * the appropriate Current-State report for
2007 * immediate transmission.
2009 if (query_response_timer_expired
) {
2012 retval
= mld_v2_enqueue_group_record(qrq
, inm
, 0, 1,
2013 (inm
->in6m_state
== MLD_SG_QUERY_PENDING_MEMBER
),
2015 MLD_PRINTF(("%s: enqueue record = %d\n",
2017 inm
->in6m_state
= MLD_REPORTING_MEMBER
;
2018 in6m_clear_recorded(inm
);
2021 case MLD_REPORTING_MEMBER
:
2022 case MLD_LEAVING_MEMBER
:
2023 if (state_change_retransmit_timer_expired
) {
2025 * State-change retransmission timer fired.
2026 * If there are any further pending retransmissions,
2027 * set the global pending state-change flag, and
2030 if (--inm
->in6m_scrv
> 0) {
2031 inm
->in6m_sctimer
= (uint16_t)uri_sec
;
2032 state_change_timers_running6
= 1;
2033 /* caller will schedule timer */
2036 * Retransmit the previously computed state-change
2037 * report. If there are no further pending
2038 * retransmissions, the mbuf queue will be consumed.
2039 * Update T0 state to T1 as we have now sent
2042 (void) mld_v2_merge_state_changes(inm
, scq
);
2045 MLD_PRINTF(("%s: T1 -> T0 for %s/%s\n", __func__
,
2046 ip6_sprintf(&inm
->in6m_addr
),
2047 if_name(inm
->in6m_ifp
)));
2050 * If we are leaving the group for good, make sure
2051 * we release MLD's reference to it.
2052 * This release must be deferred using a SLIST,
2053 * as we are called from a loop which traverses
2054 * the in_ifmultiaddr TAILQ.
2056 if (inm
->in6m_state
== MLD_LEAVING_MEMBER
&&
2057 inm
->in6m_scrv
== 0) {
2058 inm
->in6m_state
= MLD_NOT_MEMBER
;
2060 * A reference has already been held in
2061 * mld_final_leave() for this inm, so
2062 * no need to hold another one. We also
2063 * bumped up its request count then, so
2064 * that it stays in in6_multihead. Both
2065 * of them will be released when it is
2066 * dequeued later on.
2068 VERIFY(inm
->in6m_nrelecnt
!= 0);
2069 SLIST_INSERT_HEAD(&mli
->mli_relinmhead
,
2078 * Switch to a different version on the given interface,
2079 * as per Section 9.12.
2082 mld_set_version(struct mld_ifinfo
*mli
, const int mld_version
)
2084 int old_version_timer
;
2086 MLI_LOCK_ASSERT_HELD(mli
);
2088 MLD_PRINTF(("%s: switching to v%d on ifp 0x%llx(%s)\n", __func__
,
2089 mld_version
, (uint64_t)VM_KERNEL_ADDRPERM(mli
->mli_ifp
),
2090 if_name(mli
->mli_ifp
)));
2092 if (mld_version
== MLD_VERSION_1
) {
2094 * Compute the "Older Version Querier Present" timer as per
2095 * Section 9.12, in seconds.
2097 old_version_timer
= (mli
->mli_rv
* mli
->mli_qi
) + mli
->mli_qri
;
2098 mli
->mli_v1_timer
= old_version_timer
;
2101 if (mli
->mli_v1_timer
> 0 && mli
->mli_version
!= MLD_VERSION_1
) {
2102 mli
->mli_version
= MLD_VERSION_1
;
2103 mld_v2_cancel_link_timers(mli
);
2106 MLI_LOCK_ASSERT_HELD(mli
);
2108 return mli
->mli_v1_timer
;
2112 * Cancel pending MLDv2 timers for the given link and all groups
2113 * joined on it; state-change, general-query, and group-query timers.
2115 * Only ever called on a transition from v2 to Compatibility mode. Kill
2116 * the timers stone dead (this may be expensive for large N groups), they
2117 * will be restarted if Compatibility Mode deems that they must be due to
2121 mld_v2_cancel_link_timers(struct mld_ifinfo
*mli
)
2124 struct in6_multi
*inm
;
2125 struct in6_multistep step
;
2127 MLI_LOCK_ASSERT_HELD(mli
);
2129 MLD_PRINTF(("%s: cancel v2 timers on ifp 0x%llx(%s)\n", __func__
,
2130 (uint64_t)VM_KERNEL_ADDRPERM(mli
->mli_ifp
), if_name(mli
->mli_ifp
)));
2133 * Stop the v2 General Query Response on this link stone dead.
2134 * If timer is woken up due to interface_timers_running6,
2135 * the flag will be cleared if there are no pending link timers.
2137 mli
->mli_v2_timer
= 0;
2140 * Now clear the current-state and state-change report timers
2141 * for all memberships scoped to this link.
2146 in6_multihead_lock_shared();
2147 IN6_FIRST_MULTI(step
, inm
);
2148 while (inm
!= NULL
) {
2150 if (inm
->in6m_ifp
!= ifp
) {
2154 switch (inm
->in6m_state
) {
2155 case MLD_NOT_MEMBER
:
2156 case MLD_SILENT_MEMBER
:
2157 case MLD_IDLE_MEMBER
:
2158 case MLD_LAZY_MEMBER
:
2159 case MLD_SLEEPING_MEMBER
:
2160 case MLD_AWAKENING_MEMBER
:
2162 * These states are either not relevant in v2 mode,
2163 * or are unreported. Do nothing.
2166 case MLD_LEAVING_MEMBER
:
2168 * If we are leaving the group and switching
2169 * version, we need to release the final
2170 * reference held for issuing the INCLUDE {}.
2171 * During mld_final_leave(), we bumped up both the
2172 * request and reference counts. Since we cannot
2173 * call in6_multi_detach() here, defer this task to
2174 * the timer routine.
2176 VERIFY(inm
->in6m_nrelecnt
!= 0);
2178 SLIST_INSERT_HEAD(&mli
->mli_relinmhead
, inm
,
2182 case MLD_G_QUERY_PENDING_MEMBER
:
2183 case MLD_SG_QUERY_PENDING_MEMBER
:
2184 in6m_clear_recorded(inm
);
2186 case MLD_REPORTING_MEMBER
:
2187 inm
->in6m_state
= MLD_REPORTING_MEMBER
;
2191 * Always clear state-change and group report timers.
2192 * Free any pending MLDv2 state-change records.
2194 inm
->in6m_sctimer
= 0;
2195 inm
->in6m_timer
= 0;
2196 IF_DRAIN(&inm
->in6m_scq
);
2199 IN6_NEXT_MULTI(step
, inm
);
2201 in6_multihead_lock_done();
2207 * Update the Older Version Querier Present timers for a link.
2208 * See Section 9.12 of RFC 3810.
2211 mld_v1_process_querier_timers(struct mld_ifinfo
*mli
)
2213 MLI_LOCK_ASSERT_HELD(mli
);
2215 if (mld_v2enable
&& mli
->mli_version
!= MLD_VERSION_2
&&
2216 --mli
->mli_v1_timer
== 0) {
2218 * MLDv1 Querier Present timer expired; revert to MLDv2.
2220 MLD_PRINTF(("%s: transition from v%d -> v%d on 0x%llx(%s)\n",
2221 __func__
, mli
->mli_version
, MLD_VERSION_2
,
2222 (uint64_t)VM_KERNEL_ADDRPERM(mli
->mli_ifp
),
2223 if_name(mli
->mli_ifp
)));
2224 mli
->mli_version
= MLD_VERSION_2
;
2229 * Transmit an MLDv1 report immediately.
2232 mld_v1_transmit_report(struct in6_multi
*in6m
, const uint8_t type
)
2235 struct in6_ifaddr
*ia
;
2236 struct ip6_hdr
*ip6
;
2237 struct mbuf
*mh
, *md
;
2238 struct mld_hdr
*mld
;
2241 IN6M_LOCK_ASSERT_HELD(in6m
);
2242 MLI_LOCK_ASSERT_HELD(in6m
->in6m_mli
);
2244 ifp
= in6m
->in6m_ifp
;
2245 /* ia may be NULL if link-local address is tentative. */
2246 ia
= in6ifa_ifpforlinklocal(ifp
, IN6_IFF_NOTREADY
| IN6_IFF_ANYCAST
);
2248 MGETHDR(mh
, M_DONTWAIT
, MT_HEADER
);
2251 IFA_REMREF(&ia
->ia_ifa
);
2255 MGET(md
, M_DONTWAIT
, MT_DATA
);
2259 IFA_REMREF(&ia
->ia_ifa
);
2266 * FUTURE: Consider increasing alignment by ETHER_HDR_LEN, so
2267 * that ether_output() does not need to allocate another mbuf
2268 * for the header in the most common case.
2270 MH_ALIGN(mh
, sizeof(struct ip6_hdr
));
2271 mh
->m_pkthdr
.len
= sizeof(struct ip6_hdr
) + sizeof(struct mld_hdr
);
2272 mh
->m_len
= sizeof(struct ip6_hdr
);
2274 ip6
= mtod(mh
, struct ip6_hdr
*);
2276 ip6
->ip6_vfc
&= ~IPV6_VERSION_MASK
;
2277 ip6
->ip6_vfc
|= IPV6_VERSION
;
2278 ip6
->ip6_nxt
= IPPROTO_ICMPV6
;
2280 IFA_LOCK(&ia
->ia_ifa
);
2282 ip6
->ip6_src
= ia
? ia
->ia_addr
.sin6_addr
: in6addr_any
;
2284 IFA_UNLOCK(&ia
->ia_ifa
);
2285 IFA_REMREF(&ia
->ia_ifa
);
2288 ip6
->ip6_dst
= in6m
->in6m_addr
;
2290 md
->m_len
= sizeof(struct mld_hdr
);
2291 mld
= mtod(md
, struct mld_hdr
*);
2292 mld
->mld_type
= type
;
2295 mld
->mld_maxdelay
= 0;
2296 mld
->mld_reserved
= 0;
2297 mld
->mld_addr
= in6m
->in6m_addr
;
2298 in6_clearscope(&mld
->mld_addr
);
2299 mld
->mld_cksum
= in6_cksum(mh
, IPPROTO_ICMPV6
,
2300 sizeof(struct ip6_hdr
), sizeof(struct mld_hdr
));
2302 mld_save_context(mh
, ifp
);
2303 mh
->m_flags
|= M_MLDV1
;
2306 * Due to the fact that at this point we are possibly holding
2307 * in6_multihead_lock in shared or exclusive mode, we can't call
2308 * mld_dispatch_packet() here since that will eventually call
2309 * ip6_output(), which will try to lock in6_multihead_lock and cause
2311 * Instead we defer the work to the mld_timeout() thread, thus
2312 * avoiding unlocking in_multihead_lock here.
2314 if (IF_QFULL(&in6m
->in6m_mli
->mli_v1q
)) {
2315 MLD_PRINTF(("%s: v1 outbound queue full\n", __func__
));
2319 IF_ENQUEUE(&in6m
->in6m_mli
->mli_v1q
, mh
);
2327 * Process a state change from the upper layer for the given IPv6 group.
2329 * Each socket holds a reference on the in6_multi in its own ip_moptions.
2330 * The socket layer will have made the necessary updates to.the group
2331 * state, it is now up to MLD to issue a state change report if there
2332 * has been any change between T0 (when the last state-change was issued)
2335 * We use the MLDv2 state machine at group level. The MLd module
2336 * however makes the decision as to which MLD protocol version to speak.
2337 * A state change *from* INCLUDE {} always means an initial join.
2338 * A state change *to* INCLUDE {} always means a final leave.
2340 * If delay is non-zero, and the state change is an initial multicast
2341 * join, the state change report will be delayed by 'delay' ticks
2342 * in units of seconds if MLDv1 is active on the link; otherwise
2343 * the initial MLDv2 state change report will be delayed by whichever
2344 * is sooner, a pending state-change timer or delay itself.
2347 mld_change_state(struct in6_multi
*inm
, struct mld_tparams
*mtp
,
2350 struct mld_ifinfo
*mli
;
2354 VERIFY(mtp
!= NULL
);
2355 bzero(mtp
, sizeof(*mtp
));
2357 IN6M_LOCK_ASSERT_HELD(inm
);
2358 VERIFY(inm
->in6m_mli
!= NULL
);
2359 MLI_LOCK_ASSERT_NOTHELD(inm
->in6m_mli
);
2362 * Try to detect if the upper layer just asked us to change state
2363 * for an interface which has now gone away.
2365 VERIFY(inm
->in6m_ifma
!= NULL
);
2366 ifp
= inm
->in6m_ifma
->ifma_ifp
;
2368 * Sanity check that netinet6's notion of ifp is the same as net's.
2370 VERIFY(inm
->in6m_ifp
== ifp
);
2372 mli
= MLD_IFINFO(ifp
);
2373 VERIFY(mli
!= NULL
);
2376 * If we detect a state transition to or from MCAST_UNDEFINED
2377 * for this group, then we are starting or finishing an MLD
2378 * life cycle for this group.
2380 if (inm
->in6m_st
[1].iss_fmode
!= inm
->in6m_st
[0].iss_fmode
) {
2381 MLD_PRINTF(("%s: inm transition %d -> %d\n", __func__
,
2382 inm
->in6m_st
[0].iss_fmode
, inm
->in6m_st
[1].iss_fmode
));
2383 if (inm
->in6m_st
[0].iss_fmode
== MCAST_UNDEFINED
) {
2384 MLD_PRINTF(("%s: initial join\n", __func__
));
2385 error
= mld_initial_join(inm
, mli
, mtp
, delay
);
2387 } else if (inm
->in6m_st
[1].iss_fmode
== MCAST_UNDEFINED
) {
2388 MLD_PRINTF(("%s: final leave\n", __func__
));
2389 mld_final_leave(inm
, mli
, mtp
);
2393 MLD_PRINTF(("%s: filter set change\n", __func__
));
2396 error
= mld_handle_state_change(inm
, mli
, mtp
);
2402 * Perform the initial join for an MLD group.
2404 * When joining a group:
2405 * If the group should have its MLD traffic suppressed, do nothing.
2406 * MLDv1 starts sending MLDv1 host membership reports.
2407 * MLDv2 will schedule an MLDv2 state-change report containing the
2408 * initial state of the membership.
2410 * If the delay argument is non-zero, then we must delay sending the
2411 * initial state change for delay ticks (in units of seconds).
2414 mld_initial_join(struct in6_multi
*inm
, struct mld_ifinfo
*mli
,
2415 struct mld_tparams
*mtp
, const int delay
)
2418 struct ifqueue
*ifq
;
2419 int error
, retval
, syncstates
;
2422 IN6M_LOCK_ASSERT_HELD(inm
);
2423 MLI_LOCK_ASSERT_NOTHELD(mli
);
2424 VERIFY(mtp
!= NULL
);
2426 MLD_PRINTF(("%s: initial join %s on ifp 0x%llx(%s)\n",
2427 __func__
, ip6_sprintf(&inm
->in6m_addr
),
2428 (uint64_t)VM_KERNEL_ADDRPERM(inm
->in6m_ifp
),
2429 if_name(inm
->in6m_ifp
)));
2434 ifp
= inm
->in6m_ifp
;
2437 VERIFY(mli
->mli_ifp
== ifp
);
2440 * Avoid MLD if group is :
2441 * 1. Joined on loopback, OR
2442 * 2. On a link that is marked MLIF_SILENT
2443 * 3. rdar://problem/19227650 Is link local scoped and
2444 * on cellular interface
2445 * 4. Is a type that should not be reported (node local
2446 * or all node link local multicast.
2447 * All other groups enter the appropriate state machine
2448 * for the version in use on this link.
2450 if ((ifp
->if_flags
& IFF_LOOPBACK
) ||
2451 (mli
->mli_flags
& MLIF_SILENT
) ||
2452 (IFNET_IS_CELLULAR(ifp
) &&
2453 (IN6_IS_ADDR_MC_LINKLOCAL(&inm
->in6m_addr
) || IN6_IS_ADDR_MC_UNICAST_BASED_LINKLOCAL(&inm
->in6m_addr
))) ||
2454 !mld_is_addr_reported(&inm
->in6m_addr
)) {
2455 MLD_PRINTF(("%s: not kicking state machine for silent group\n",
2457 inm
->in6m_state
= MLD_SILENT_MEMBER
;
2458 inm
->in6m_timer
= 0;
2461 * Deal with overlapping in6_multi lifecycle.
2462 * If this group was LEAVING, then make sure
2463 * we drop the reference we picked up to keep the
2464 * group around for the final INCLUDE {} enqueue.
2465 * Since we cannot call in6_multi_detach() here,
2466 * defer this task to the timer routine.
2468 if (mli
->mli_version
== MLD_VERSION_2
&&
2469 inm
->in6m_state
== MLD_LEAVING_MEMBER
) {
2470 VERIFY(inm
->in6m_nrelecnt
!= 0);
2471 SLIST_INSERT_HEAD(&mli
->mli_relinmhead
, inm
,
2475 inm
->in6m_state
= MLD_REPORTING_MEMBER
;
2477 switch (mli
->mli_version
) {
2480 * If a delay was provided, only use it if
2481 * it is greater than the delay normally
2482 * used for an MLDv1 state change report,
2483 * and delay sending the initial MLDv1 report
2484 * by not transitioning to the IDLE state.
2486 odelay
= MLD_RANDOM_DELAY(MLD_V1_MAX_RI
);
2488 inm
->in6m_timer
= max(delay
, odelay
);
2491 inm
->in6m_state
= MLD_IDLE_MEMBER
;
2492 error
= mld_v1_transmit_report(inm
,
2493 MLD_LISTENER_REPORT
);
2495 IN6M_LOCK_ASSERT_HELD(inm
);
2496 MLI_LOCK_ASSERT_HELD(mli
);
2499 inm
->in6m_timer
= odelay
;
2507 * Defer update of T0 to T1, until the first copy
2508 * of the state change has been transmitted.
2513 * Immediately enqueue a State-Change Report for
2514 * this interface, freeing any previous reports.
2515 * Don't kick the timers if there is nothing to do,
2516 * or if an error occurred.
2518 ifq
= &inm
->in6m_scq
;
2520 retval
= mld_v2_enqueue_group_record(ifq
, inm
, 1,
2521 0, 0, (mli
->mli_flags
& MLIF_USEALLOW
));
2522 mtp
->cst
= (ifq
->ifq_len
> 0);
2523 MLD_PRINTF(("%s: enqueue record = %d\n",
2526 error
= retval
* -1;
2531 * Schedule transmission of pending state-change
2532 * report up to RV times for this link. The timer
2533 * will fire at the next mld_timeout (1 second)),
2534 * giving us an opportunity to merge the reports.
2536 * If a delay was provided to this function, only
2537 * use this delay if sooner than the existing one.
2539 VERIFY(mli
->mli_rv
> 1);
2540 inm
->in6m_scrv
= (uint16_t)mli
->mli_rv
;
2542 if (inm
->in6m_sctimer
> 1) {
2544 MIN(inm
->in6m_sctimer
, (uint16_t)delay
);
2546 inm
->in6m_sctimer
= (uint16_t)delay
;
2549 inm
->in6m_sctimer
= 1;
2559 * Only update the T0 state if state change is atomic,
2560 * i.e. we don't need to wait for a timer to fire before we
2561 * can consider the state change to have been communicated.
2565 MLD_PRINTF(("%s: T1 -> T0 for %s/%s\n", __func__
,
2566 ip6_sprintf(&inm
->in6m_addr
),
2567 if_name(inm
->in6m_ifp
)));
2574 * Issue an intermediate state change during the life-cycle.
2577 mld_handle_state_change(struct in6_multi
*inm
, struct mld_ifinfo
*mli
,
2578 struct mld_tparams
*mtp
)
2583 IN6M_LOCK_ASSERT_HELD(inm
);
2584 MLI_LOCK_ASSERT_NOTHELD(mli
);
2585 VERIFY(mtp
!= NULL
);
2587 MLD_PRINTF(("%s: state change for %s on ifp 0x%llx(%s)\n",
2588 __func__
, ip6_sprintf(&inm
->in6m_addr
),
2589 (uint64_t)VM_KERNEL_ADDRPERM(inm
->in6m_ifp
),
2590 if_name(inm
->in6m_ifp
)));
2592 ifp
= inm
->in6m_ifp
;
2595 VERIFY(mli
->mli_ifp
== ifp
);
2597 if ((ifp
->if_flags
& IFF_LOOPBACK
) ||
2598 (mli
->mli_flags
& MLIF_SILENT
) ||
2599 !mld_is_addr_reported(&inm
->in6m_addr
) ||
2600 (mli
->mli_version
!= MLD_VERSION_2
)) {
2602 if (!mld_is_addr_reported(&inm
->in6m_addr
)) {
2603 MLD_PRINTF(("%s: not kicking state machine for silent "
2604 "group\n", __func__
));
2606 MLD_PRINTF(("%s: nothing to do\n", __func__
));
2608 MLD_PRINTF(("%s: T1 -> T0 for %s/%s\n", __func__
,
2609 ip6_sprintf(&inm
->in6m_addr
),
2610 if_name(inm
->in6m_ifp
)));
2614 IF_DRAIN(&inm
->in6m_scq
);
2616 retval
= mld_v2_enqueue_group_record(&inm
->in6m_scq
, inm
, 1, 0, 0,
2617 (mli
->mli_flags
& MLIF_USEALLOW
));
2618 mtp
->cst
= (inm
->in6m_scq
.ifq_len
> 0);
2619 MLD_PRINTF(("%s: enqueue record = %d\n", __func__
, retval
));
2629 * If record(s) were enqueued, start the state-change
2630 * report timer for this group.
2632 inm
->in6m_scrv
= (uint16_t)mli
->mli_rv
;
2633 inm
->in6m_sctimer
= 1;
2642 * Perform the final leave for a multicast address.
2644 * When leaving a group:
2645 * MLDv1 sends a DONE message, if and only if we are the reporter.
2646 * MLDv2 enqueues a state-change report containing a transition
2647 * to INCLUDE {} for immediate transmission.
2650 mld_final_leave(struct in6_multi
*inm
, struct mld_ifinfo
*mli
,
2651 struct mld_tparams
*mtp
)
2655 IN6M_LOCK_ASSERT_HELD(inm
);
2656 MLI_LOCK_ASSERT_NOTHELD(mli
);
2657 VERIFY(mtp
!= NULL
);
2659 MLD_PRINTF(("%s: final leave %s on ifp 0x%llx(%s)\n",
2660 __func__
, ip6_sprintf(&inm
->in6m_addr
),
2661 (uint64_t)VM_KERNEL_ADDRPERM(inm
->in6m_ifp
),
2662 if_name(inm
->in6m_ifp
)));
2664 switch (inm
->in6m_state
) {
2665 case MLD_NOT_MEMBER
:
2666 case MLD_SILENT_MEMBER
:
2667 case MLD_LEAVING_MEMBER
:
2668 /* Already leaving or left; do nothing. */
2669 MLD_PRINTF(("%s: not kicking state machine for silent group\n",
2672 case MLD_REPORTING_MEMBER
:
2673 case MLD_IDLE_MEMBER
:
2674 case MLD_G_QUERY_PENDING_MEMBER
:
2675 case MLD_SG_QUERY_PENDING_MEMBER
:
2677 if (mli
->mli_version
== MLD_VERSION_1
) {
2678 if (inm
->in6m_state
== MLD_G_QUERY_PENDING_MEMBER
||
2679 inm
->in6m_state
== MLD_SG_QUERY_PENDING_MEMBER
) {
2680 panic("%s: MLDv2 state reached, not MLDv2 "
2681 "mode\n", __func__
);
2684 /* scheduler timer if enqueue is successful */
2685 mtp
->cst
= (mld_v1_transmit_report(inm
,
2686 MLD_LISTENER_DONE
) == 0);
2688 IN6M_LOCK_ASSERT_HELD(inm
);
2689 MLI_LOCK_ASSERT_HELD(mli
);
2691 inm
->in6m_state
= MLD_NOT_MEMBER
;
2692 } else if (mli
->mli_version
== MLD_VERSION_2
) {
2694 * Stop group timer and all pending reports.
2695 * Immediately enqueue a state-change report
2696 * TO_IN {} to be sent on the next timeout,
2697 * giving us an opportunity to merge reports.
2699 IF_DRAIN(&inm
->in6m_scq
);
2700 inm
->in6m_timer
= 0;
2701 inm
->in6m_scrv
= (uint16_t)mli
->mli_rv
;
2702 MLD_PRINTF(("%s: Leaving %s/%s with %d "
2703 "pending retransmissions.\n", __func__
,
2704 ip6_sprintf(&inm
->in6m_addr
),
2705 if_name(inm
->in6m_ifp
),
2707 if (inm
->in6m_scrv
== 0) {
2708 inm
->in6m_state
= MLD_NOT_MEMBER
;
2709 inm
->in6m_sctimer
= 0;
2713 * Stick around in the in6_multihead list;
2714 * the final detach will be issued by
2715 * mld_v2_process_group_timers() when
2716 * the retransmit timer expires.
2718 IN6M_ADDREF_LOCKED(inm
);
2719 VERIFY(inm
->in6m_debug
& IFD_ATTACHED
);
2721 VERIFY(inm
->in6m_reqcnt
>= 1);
2722 inm
->in6m_nrelecnt
++;
2723 VERIFY(inm
->in6m_nrelecnt
!= 0);
2725 retval
= mld_v2_enqueue_group_record(
2726 &inm
->in6m_scq
, inm
, 1, 0, 0,
2727 (mli
->mli_flags
& MLIF_USEALLOW
));
2728 mtp
->cst
= (inm
->in6m_scq
.ifq_len
> 0);
2729 KASSERT(retval
!= 0,
2730 ("%s: enqueue record = %d\n", __func__
,
2733 inm
->in6m_state
= MLD_LEAVING_MEMBER
;
2734 inm
->in6m_sctimer
= 1;
2741 case MLD_LAZY_MEMBER
:
2742 case MLD_SLEEPING_MEMBER
:
2743 case MLD_AWAKENING_MEMBER
:
2744 /* Our reports are suppressed; do nothing. */
2750 MLD_PRINTF(("%s: T1 -> T0 for %s/%s\n", __func__
,
2751 ip6_sprintf(&inm
->in6m_addr
),
2752 if_name(inm
->in6m_ifp
)));
2753 inm
->in6m_st
[1].iss_fmode
= MCAST_UNDEFINED
;
2754 MLD_PRINTF(("%s: T1 now MCAST_UNDEFINED for 0x%llx/%s\n",
2755 __func__
, (uint64_t)VM_KERNEL_ADDRPERM(&inm
->in6m_addr
),
2756 if_name(inm
->in6m_ifp
)));
2761 * Enqueue an MLDv2 group record to the given output queue.
2763 * If is_state_change is zero, a current-state record is appended.
2764 * If is_state_change is non-zero, a state-change report is appended.
2766 * If is_group_query is non-zero, an mbuf packet chain is allocated.
2767 * If is_group_query is zero, and if there is a packet with free space
2768 * at the tail of the queue, it will be appended to providing there
2769 * is enough free space.
2770 * Otherwise a new mbuf packet chain is allocated.
2772 * If is_source_query is non-zero, each source is checked to see if
2773 * it was recorded for a Group-Source query, and will be omitted if
2774 * it is not both in-mode and recorded.
2776 * If use_block_allow is non-zero, state change reports for initial join
2777 * and final leave, on an inclusive mode group with a source list, will be
2778 * rewritten to use the ALLOW_NEW and BLOCK_OLD record types, respectively.
2780 * The function will attempt to allocate leading space in the packet
2781 * for the IPv6+ICMP headers to be prepended without fragmenting the chain.
2783 * If successful the size of all data appended to the queue is returned,
2784 * otherwise an error code less than zero is returned, or zero if
2785 * no record(s) were appended.
2788 mld_v2_enqueue_group_record(struct ifqueue
*ifq
, struct in6_multi
*inm
,
2789 const int is_state_change
, const int is_group_query
,
2790 const int is_source_query
, const int use_block_allow
)
2792 struct mldv2_record mr
;
2793 struct mldv2_record
*pmr
;
2795 struct ip6_msource
*ims
, *nims
;
2796 struct mbuf
*m0
, *m
, *md
;
2797 int error
, is_filter_list_change
;
2798 int minrec0len
, m0srcs
, msrcs
, nbytes
, off
;
2799 int record_has_sources
;
2804 IN6M_LOCK_ASSERT_HELD(inm
);
2805 MLI_LOCK_ASSERT_HELD(inm
->in6m_mli
);
2808 ifp
= inm
->in6m_ifp
;
2809 is_filter_list_change
= 0;
2816 record_has_sources
= 1;
2818 type
= MLD_DO_NOTHING
;
2819 mode
= (uint8_t)inm
->in6m_st
[1].iss_fmode
;
2822 * If we did not transition out of ASM mode during t0->t1,
2823 * and there are no source nodes to process, we can skip
2824 * the generation of source records.
2826 if (inm
->in6m_st
[0].iss_asm
> 0 && inm
->in6m_st
[1].iss_asm
> 0 &&
2827 inm
->in6m_nsrc
== 0) {
2828 record_has_sources
= 0;
2831 if (is_state_change
) {
2833 * Queue a state change record.
2834 * If the mode did not change, and there are non-ASM
2835 * listeners or source filters present,
2836 * we potentially need to issue two records for the group.
2837 * If there are ASM listeners, and there was no filter
2838 * mode transition of any kind, do nothing.
2840 * If we are transitioning to MCAST_UNDEFINED, we need
2841 * not send any sources. A transition to/from this state is
2842 * considered inclusive with some special treatment.
2844 * If we are rewriting initial joins/leaves to use
2845 * ALLOW/BLOCK, and the group's membership is inclusive,
2846 * we need to send sources in all cases.
2848 if (mode
!= inm
->in6m_st
[0].iss_fmode
) {
2849 if (mode
== MCAST_EXCLUDE
) {
2850 MLD_PRINTF(("%s: change to EXCLUDE\n",
2852 type
= MLD_CHANGE_TO_EXCLUDE_MODE
;
2854 MLD_PRINTF(("%s: change to INCLUDE\n",
2856 if (use_block_allow
) {
2859 * Here we're interested in state
2860 * edges either direction between
2861 * MCAST_UNDEFINED and MCAST_INCLUDE.
2862 * Perhaps we should just check
2863 * the group state, rather than
2866 if (mode
== MCAST_UNDEFINED
) {
2867 type
= MLD_BLOCK_OLD_SOURCES
;
2869 type
= MLD_ALLOW_NEW_SOURCES
;
2872 type
= MLD_CHANGE_TO_INCLUDE_MODE
;
2873 if (mode
== MCAST_UNDEFINED
) {
2874 record_has_sources
= 0;
2879 if (record_has_sources
) {
2880 is_filter_list_change
= 1;
2882 type
= MLD_DO_NOTHING
;
2887 * Queue a current state record.
2889 if (mode
== MCAST_EXCLUDE
) {
2890 type
= MLD_MODE_IS_EXCLUDE
;
2891 } else if (mode
== MCAST_INCLUDE
) {
2892 type
= MLD_MODE_IS_INCLUDE
;
2893 VERIFY(inm
->in6m_st
[1].iss_asm
== 0);
2898 * Generate the filter list changes using a separate function.
2900 if (is_filter_list_change
) {
2901 return mld_v2_enqueue_filter_change(ifq
, inm
);
2904 if (type
== MLD_DO_NOTHING
) {
2905 MLD_PRINTF(("%s: nothing to do for %s/%s\n",
2906 __func__
, ip6_sprintf(&inm
->in6m_addr
),
2907 if_name(inm
->in6m_ifp
)));
2912 * If any sources are present, we must be able to fit at least
2913 * one in the trailing space of the tail packet's mbuf,
2916 minrec0len
= sizeof(struct mldv2_record
);
2917 if (record_has_sources
) {
2918 minrec0len
+= sizeof(struct in6_addr
);
2920 MLD_PRINTF(("%s: queueing %s for %s/%s\n", __func__
,
2921 mld_rec_type_to_str(type
),
2922 ip6_sprintf(&inm
->in6m_addr
),
2923 if_name(inm
->in6m_ifp
)));
2926 * Check if we have a packet in the tail of the queue for this
2927 * group into which the first group record for this group will fit.
2928 * Otherwise allocate a new packet.
2929 * Always allocate leading space for IP6+RA+ICMPV6+REPORT.
2930 * Note: Group records for G/GSR query responses MUST be sent
2931 * in their own packet.
2934 if (!is_group_query
&&
2936 (m0
->m_pkthdr
.vt_nrecs
+ 1 <= MLD_V2_REPORT_MAXRECS
) &&
2937 (m0
->m_pkthdr
.len
+ minrec0len
) <
2938 (ifp
->if_mtu
- MLD_MTUSPACE
)) {
2939 m0srcs
= (ifp
->if_mtu
- m0
->m_pkthdr
.len
-
2940 sizeof(struct mldv2_record
)) /
2941 sizeof(struct in6_addr
);
2943 MLD_PRINTF(("%s: use existing packet\n", __func__
));
2945 if (IF_QFULL(ifq
)) {
2946 MLD_PRINTF(("%s: outbound queue full\n", __func__
));
2950 m0srcs
= (ifp
->if_mtu
- MLD_MTUSPACE
-
2951 sizeof(struct mldv2_record
)) / sizeof(struct in6_addr
);
2952 if (!is_state_change
&& !is_group_query
) {
2953 m
= m_getcl(M_DONTWAIT
, MT_DATA
, M_PKTHDR
);
2956 m
= m_gethdr(M_DONTWAIT
, MT_DATA
);
2962 mld_save_context(m
, ifp
);
2964 MLD_PRINTF(("%s: allocated first packet\n", __func__
));
2968 * Append group record.
2969 * If we have sources, we don't know how many yet.
2974 mr
.mr_addr
= inm
->in6m_addr
;
2975 in6_clearscope(&mr
.mr_addr
);
2976 if (!m_append(m
, sizeof(struct mldv2_record
), (void *)&mr
)) {
2980 MLD_PRINTF(("%s: m_append() failed.\n", __func__
));
2983 nbytes
+= sizeof(struct mldv2_record
);
2986 * Append as many sources as will fit in the first packet.
2987 * If we are appending to a new packet, the chain allocation
2988 * may potentially use clusters; use m_getptr() in this case.
2989 * If we are appending to an existing packet, we need to obtain
2990 * a pointer to the group record after m_append(), in case a new
2991 * mbuf was allocated.
2993 * Only append sources which are in-mode at t1. If we are
2994 * transitioning to MCAST_UNDEFINED state on the group, and
2995 * use_block_allow is zero, do not include source entries.
2996 * Otherwise, we need to include this source in the report.
2998 * Only report recorded sources in our filter set when responding
2999 * to a group-source query.
3001 if (record_has_sources
) {
3004 pmr
= (struct mldv2_record
*)(mtod(md
, uint8_t *) +
3005 md
->m_len
- nbytes
);
3007 md
= m_getptr(m
, 0, &off
);
3008 pmr
= (struct mldv2_record
*)(mtod(md
, uint8_t *) +
3012 RB_FOREACH_SAFE(ims
, ip6_msource_tree
, &inm
->in6m_srcs
,
3014 MLD_PRINTF(("%s: visit node %s\n", __func__
,
3015 ip6_sprintf(&ims
->im6s_addr
)));
3016 now
= im6s_get_mode(inm
, ims
, 1);
3017 MLD_PRINTF(("%s: node is %d\n", __func__
, now
));
3018 if ((now
!= mode
) ||
3020 (!use_block_allow
&& mode
== MCAST_UNDEFINED
))) {
3021 MLD_PRINTF(("%s: skip node\n", __func__
));
3024 if (is_source_query
&& ims
->im6s_stp
== 0) {
3025 MLD_PRINTF(("%s: skip unrecorded node\n",
3029 MLD_PRINTF(("%s: append node\n", __func__
));
3030 if (!m_append(m
, sizeof(struct in6_addr
),
3031 (void *)&ims
->im6s_addr
)) {
3035 MLD_PRINTF(("%s: m_append() failed.\n",
3039 nbytes
+= sizeof(struct in6_addr
);
3041 if (msrcs
== m0srcs
) {
3045 MLD_PRINTF(("%s: msrcs is %d this packet\n", __func__
,
3047 pmr
->mr_numsrc
= htons((uint16_t)msrcs
);
3048 nbytes
+= (msrcs
* sizeof(struct in6_addr
));
3051 if (is_source_query
&& msrcs
== 0) {
3052 MLD_PRINTF(("%s: no recorded sources to report\n", __func__
));
3060 * We are good to go with first packet.
3063 MLD_PRINTF(("%s: enqueueing first packet\n", __func__
));
3064 m
->m_pkthdr
.vt_nrecs
= 1;
3067 m
->m_pkthdr
.vt_nrecs
++;
3070 * No further work needed if no source list in packet(s).
3072 if (!record_has_sources
) {
3077 * Whilst sources remain to be announced, we need to allocate
3078 * a new packet and fill out as many sources as will fit.
3079 * Always try for a cluster first.
3081 while (nims
!= NULL
) {
3082 if (IF_QFULL(ifq
)) {
3083 MLD_PRINTF(("%s: outbound queue full\n", __func__
));
3086 m
= m_getcl(M_DONTWAIT
, MT_DATA
, M_PKTHDR
);
3088 m
= m_gethdr(M_DONTWAIT
, MT_DATA
);
3093 mld_save_context(m
, ifp
);
3094 md
= m_getptr(m
, 0, &off
);
3095 pmr
= (struct mldv2_record
*)(mtod(md
, uint8_t *) + off
);
3096 MLD_PRINTF(("%s: allocated next packet\n", __func__
));
3098 if (!m_append(m
, sizeof(struct mldv2_record
), (void *)&mr
)) {
3102 MLD_PRINTF(("%s: m_append() failed.\n", __func__
));
3105 m
->m_pkthdr
.vt_nrecs
= 1;
3106 nbytes
+= sizeof(struct mldv2_record
);
3108 m0srcs
= (ifp
->if_mtu
- MLD_MTUSPACE
-
3109 sizeof(struct mldv2_record
)) / sizeof(struct in6_addr
);
3112 RB_FOREACH_FROM(ims
, ip6_msource_tree
, nims
) {
3113 MLD_PRINTF(("%s: visit node %s\n",
3114 __func__
, ip6_sprintf(&ims
->im6s_addr
)));
3115 now
= im6s_get_mode(inm
, ims
, 1);
3116 if ((now
!= mode
) ||
3118 (!use_block_allow
&& mode
== MCAST_UNDEFINED
))) {
3119 MLD_PRINTF(("%s: skip node\n", __func__
));
3122 if (is_source_query
&& ims
->im6s_stp
== 0) {
3123 MLD_PRINTF(("%s: skip unrecorded node\n",
3127 MLD_PRINTF(("%s: append node\n", __func__
));
3128 if (!m_append(m
, sizeof(struct in6_addr
),
3129 (void *)&ims
->im6s_addr
)) {
3133 MLD_PRINTF(("%s: m_append() failed.\n",
3138 if (msrcs
== m0srcs
) {
3142 pmr
->mr_numsrc
= htons((uint16_t)msrcs
);
3143 nbytes
+= (msrcs
* sizeof(struct in6_addr
));
3145 MLD_PRINTF(("%s: enqueueing next packet\n", __func__
));
3153 * Type used to mark record pass completion.
3154 * We exploit the fact we can cast to this easily from the
3155 * current filter modes on each ip_msource node.
3158 REC_NONE
= 0x00, /* MCAST_UNDEFINED */
3159 REC_ALLOW
= 0x01, /* MCAST_INCLUDE */
3160 REC_BLOCK
= 0x02, /* MCAST_EXCLUDE */
3161 REC_FULL
= REC_ALLOW
| REC_BLOCK
3165 * Enqueue an MLDv2 filter list change to the given output queue.
3167 * Source list filter state is held in an RB-tree. When the filter list
3168 * for a group is changed without changing its mode, we need to compute
3169 * the deltas between T0 and T1 for each source in the filter set,
3170 * and enqueue the appropriate ALLOW_NEW/BLOCK_OLD records.
3172 * As we may potentially queue two record types, and the entire R-B tree
3173 * needs to be walked at once, we break this out into its own function
3174 * so we can generate a tightly packed queue of packets.
3176 * XXX This could be written to only use one tree walk, although that makes
3177 * serializing into the mbuf chains a bit harder. For now we do two walks
3178 * which makes things easier on us, and it may or may not be harder on
3181 * If successful the size of all data appended to the queue is returned,
3182 * otherwise an error code less than zero is returned, or zero if
3183 * no record(s) were appended.
3186 mld_v2_enqueue_filter_change(struct ifqueue
*ifq
, struct in6_multi
*inm
)
3188 static const int MINRECLEN
=
3189 sizeof(struct mldv2_record
) + sizeof(struct in6_addr
);
3191 struct mldv2_record mr
;
3192 struct mldv2_record
*pmr
;
3193 struct ip6_msource
*ims
, *nims
;
3194 struct mbuf
*m
, *m0
, *md
;
3195 int m0srcs
, nbytes
, npbytes
, off
, rsrcs
, schanged
;
3197 uint8_t mode
, now
, then
;
3198 rectype_t crt
, drt
, nrt
;
3200 IN6M_LOCK_ASSERT_HELD(inm
);
3202 if (inm
->in6m_nsrc
== 0 ||
3203 (inm
->in6m_st
[0].iss_asm
> 0 && inm
->in6m_st
[1].iss_asm
> 0)) {
3207 ifp
= inm
->in6m_ifp
; /* interface */
3208 mode
= (uint8_t)inm
->in6m_st
[1].iss_fmode
; /* filter mode at t1 */
3209 crt
= REC_NONE
; /* current group record type */
3210 drt
= REC_NONE
; /* mask of completed group record types */
3211 nrt
= REC_NONE
; /* record type for current node */
3212 m0srcs
= 0; /* # source which will fit in current mbuf chain */
3213 npbytes
= 0; /* # of bytes appended this packet */
3214 nbytes
= 0; /* # of bytes appended to group's state-change queue */
3215 rsrcs
= 0; /* # sources encoded in current record */
3216 schanged
= 0; /* # nodes encoded in overall filter change */
3217 nallow
= 0; /* # of source entries in ALLOW_NEW */
3218 nblock
= 0; /* # of source entries in BLOCK_OLD */
3219 nims
= NULL
; /* next tree node pointer */
3222 * For each possible filter record mode.
3223 * The first kind of source we encounter tells us which
3224 * is the first kind of record we start appending.
3225 * If a node transitioned to UNDEFINED at t1, its mode is treated
3226 * as the inverse of the group's filter mode.
3228 while (drt
!= REC_FULL
) {
3232 (m0
->m_pkthdr
.vt_nrecs
+ 1 <=
3233 MLD_V2_REPORT_MAXRECS
) &&
3234 (m0
->m_pkthdr
.len
+ MINRECLEN
) <
3235 (ifp
->if_mtu
- MLD_MTUSPACE
)) {
3237 m0srcs
= (ifp
->if_mtu
- m0
->m_pkthdr
.len
-
3238 sizeof(struct mldv2_record
)) /
3239 sizeof(struct in6_addr
);
3240 MLD_PRINTF(("%s: use previous packet\n",
3243 m
= m_getcl(M_DONTWAIT
, MT_DATA
, M_PKTHDR
);
3245 m
= m_gethdr(M_DONTWAIT
, MT_DATA
);
3248 MLD_PRINTF(("%s: m_get*() failed\n",
3252 m
->m_pkthdr
.vt_nrecs
= 0;
3253 mld_save_context(m
, ifp
);
3254 m0srcs
= (ifp
->if_mtu
- MLD_MTUSPACE
-
3255 sizeof(struct mldv2_record
)) /
3256 sizeof(struct in6_addr
);
3258 MLD_PRINTF(("%s: allocated new packet\n",
3262 * Append the MLD group record header to the
3263 * current packet's data area.
3264 * Recalculate pointer to free space for next
3265 * group record, in case m_append() allocated
3266 * a new mbuf or cluster.
3268 memset(&mr
, 0, sizeof(mr
));
3269 mr
.mr_addr
= inm
->in6m_addr
;
3270 in6_clearscope(&mr
.mr_addr
);
3271 if (!m_append(m
, sizeof(mr
), (void *)&mr
)) {
3275 MLD_PRINTF(("%s: m_append() failed\n",
3279 npbytes
+= sizeof(struct mldv2_record
);
3281 /* new packet; offset in chain */
3282 md
= m_getptr(m
, npbytes
-
3283 sizeof(struct mldv2_record
), &off
);
3284 pmr
= (struct mldv2_record
*)(mtod(md
,
3287 /* current packet; offset from last append */
3289 pmr
= (struct mldv2_record
*)(mtod(md
,
3290 uint8_t *) + md
->m_len
-
3291 sizeof(struct mldv2_record
));
3294 * Begin walking the tree for this record type
3295 * pass, or continue from where we left off
3296 * previously if we had to allocate a new packet.
3297 * Only report deltas in-mode at t1.
3298 * We need not report included sources as allowed
3299 * if we are in inclusive mode on the group,
3300 * however the converse is not true.
3304 nims
= RB_MIN(ip6_msource_tree
,
3307 RB_FOREACH_FROM(ims
, ip6_msource_tree
, nims
) {
3308 MLD_PRINTF(("%s: visit node %s\n", __func__
,
3309 ip6_sprintf(&ims
->im6s_addr
)));
3310 now
= im6s_get_mode(inm
, ims
, 1);
3311 then
= im6s_get_mode(inm
, ims
, 0);
3312 MLD_PRINTF(("%s: mode: t0 %d, t1 %d\n",
3313 __func__
, then
, now
));
3315 MLD_PRINTF(("%s: skip unchanged\n",
3319 if (mode
== MCAST_EXCLUDE
&&
3320 now
== MCAST_INCLUDE
) {
3321 MLD_PRINTF(("%s: skip IN src on EX "
3322 "group\n", __func__
));
3325 nrt
= (rectype_t
)now
;
3326 if (nrt
== REC_NONE
) {
3327 nrt
= (rectype_t
)(~mode
& REC_FULL
);
3329 if (schanged
++ == 0) {
3331 } else if (crt
!= nrt
) {
3334 if (!m_append(m
, sizeof(struct in6_addr
),
3335 (void *)&ims
->im6s_addr
)) {
3339 MLD_PRINTF(("%s: m_append() failed\n",
3343 nallow
+= !!(crt
== REC_ALLOW
);
3344 nblock
+= !!(crt
== REC_BLOCK
);
3345 if (++rsrcs
== m0srcs
) {
3350 * If we did not append any tree nodes on this
3351 * pass, back out of allocations.
3354 npbytes
-= sizeof(struct mldv2_record
);
3356 MLD_PRINTF(("%s: m_free(m)\n",
3360 MLD_PRINTF(("%s: m_adj(m, -mr)\n",
3362 m_adj(m
, -((int)sizeof(
3363 struct mldv2_record
)));
3367 npbytes
+= (rsrcs
* sizeof(struct in6_addr
));
3368 if (crt
== REC_ALLOW
) {
3369 pmr
->mr_type
= MLD_ALLOW_NEW_SOURCES
;
3370 } else if (crt
== REC_BLOCK
) {
3371 pmr
->mr_type
= MLD_BLOCK_OLD_SOURCES
;
3373 pmr
->mr_numsrc
= htons((uint16_t)rsrcs
);
3375 * Count the new group record, and enqueue this
3376 * packet if it wasn't already queued.
3378 m
->m_pkthdr
.vt_nrecs
++;
3383 } while (nims
!= NULL
);
3385 crt
= (~crt
& REC_FULL
);
3388 MLD_PRINTF(("%s: queued %d ALLOW_NEW, %d BLOCK_OLD\n", __func__
,
3395 mld_v2_merge_state_changes(struct in6_multi
*inm
, struct ifqueue
*ifscq
)
3398 struct mbuf
*m
; /* pending state-change */
3399 struct mbuf
*m0
; /* copy of pending state-change */
3400 struct mbuf
*mt
; /* last state-change in packet */
3402 int docopy
, domerge
;
3405 IN6M_LOCK_ASSERT_HELD(inm
);
3412 * If there are further pending retransmissions, make a writable
3413 * copy of each queued state-change message before merging.
3415 if (inm
->in6m_scrv
> 0) {
3419 gq
= &inm
->in6m_scq
;
3421 if (gq
->ifq_head
== NULL
) {
3422 MLD_PRINTF(("%s: WARNING: queue for inm 0x%llx is empty\n",
3423 __func__
, (uint64_t)VM_KERNEL_ADDRPERM(inm
)));
3428 * Use IF_REMQUEUE() instead of IF_DEQUEUE() below, since the
3429 * packet might not always be at the head of the ifqueue.
3434 * Only merge the report into the current packet if
3435 * there is sufficient space to do so; an MLDv2 report
3436 * packet may only contain 65,535 group records.
3437 * Always use a simple mbuf chain concatentation to do this,
3438 * as large state changes for single groups may have
3439 * allocated clusters.
3442 mt
= ifscq
->ifq_tail
;
3444 recslen
= m_length(m
);
3446 if ((mt
->m_pkthdr
.vt_nrecs
+
3447 m
->m_pkthdr
.vt_nrecs
<=
3448 MLD_V2_REPORT_MAXRECS
) &&
3449 (mt
->m_pkthdr
.len
+ recslen
<=
3450 (inm
->in6m_ifp
->if_mtu
- MLD_MTUSPACE
))) {
3455 if (!domerge
&& IF_QFULL(gq
)) {
3456 MLD_PRINTF(("%s: outbound queue full, skipping whole "
3457 "packet 0x%llx\n", __func__
,
3458 (uint64_t)VM_KERNEL_ADDRPERM(m
)));
3469 MLD_PRINTF(("%s: dequeueing 0x%llx\n", __func__
,
3470 (uint64_t)VM_KERNEL_ADDRPERM(m
)));
3476 MLD_PRINTF(("%s: copying 0x%llx\n", __func__
,
3477 (uint64_t)VM_KERNEL_ADDRPERM(m
)));
3478 m0
= m_dup(m
, M_NOWAIT
);
3482 m0
->m_nextpkt
= NULL
;
3487 MLD_PRINTF(("%s: queueing 0x%llx to ifscq 0x%llx)\n",
3488 __func__
, (uint64_t)VM_KERNEL_ADDRPERM(m0
),
3489 (uint64_t)VM_KERNEL_ADDRPERM(ifscq
)));
3490 IF_ENQUEUE(ifscq
, m0
);
3492 struct mbuf
*mtl
; /* last mbuf of packet mt */
3494 MLD_PRINTF(("%s: merging 0x%llx with ifscq tail "
3495 "0x%llx)\n", __func__
,
3496 (uint64_t)VM_KERNEL_ADDRPERM(m0
),
3497 (uint64_t)VM_KERNEL_ADDRPERM(mt
)));
3500 m0
->m_flags
&= ~M_PKTHDR
;
3501 mt
->m_pkthdr
.len
+= recslen
;
3502 mt
->m_pkthdr
.vt_nrecs
+=
3503 m0
->m_pkthdr
.vt_nrecs
;
3513 * Respond to a pending MLDv2 General Query.
3516 mld_v2_dispatch_general_query(struct mld_ifinfo
*mli
)
3519 struct in6_multi
*inm
;
3520 struct in6_multistep step
;
3523 MLI_LOCK_ASSERT_HELD(mli
);
3525 VERIFY(mli
->mli_version
== MLD_VERSION_2
);
3530 in6_multihead_lock_shared();
3531 IN6_FIRST_MULTI(step
, inm
);
3532 while (inm
!= NULL
) {
3534 if (inm
->in6m_ifp
!= ifp
) {
3538 switch (inm
->in6m_state
) {
3539 case MLD_NOT_MEMBER
:
3540 case MLD_SILENT_MEMBER
:
3542 case MLD_REPORTING_MEMBER
:
3543 case MLD_IDLE_MEMBER
:
3544 case MLD_LAZY_MEMBER
:
3545 case MLD_SLEEPING_MEMBER
:
3546 case MLD_AWAKENING_MEMBER
:
3547 inm
->in6m_state
= MLD_REPORTING_MEMBER
;
3549 retval
= mld_v2_enqueue_group_record(&mli
->mli_gq
,
3552 MLD_PRINTF(("%s: enqueue record = %d\n",
3555 case MLD_G_QUERY_PENDING_MEMBER
:
3556 case MLD_SG_QUERY_PENDING_MEMBER
:
3557 case MLD_LEAVING_MEMBER
:
3562 IN6_NEXT_MULTI(step
, inm
);
3564 in6_multihead_lock_done();
3567 mld_dispatch_queue_locked(mli
, &mli
->mli_gq
, MLD_MAX_RESPONSE_BURST
);
3568 MLI_LOCK_ASSERT_HELD(mli
);
3571 * Slew transmission of bursts over 1 second intervals.
3573 if (mli
->mli_gq
.ifq_head
!= NULL
) {
3574 mli
->mli_v2_timer
= 1 + MLD_RANDOM_DELAY(
3575 MLD_RESPONSE_BURST_INTERVAL
);
3578 return mli
->mli_v2_timer
;
3582 * Transmit the next pending message in the output queue.
3584 * Must not be called with in6m_lockm or mli_lock held.
3587 mld_dispatch_packet(struct mbuf
*m
)
3589 struct ip6_moptions
*im6o
;
3591 struct ifnet
*oifp
= NULL
;
3594 struct ip6_hdr
*ip6
;
3595 struct mld_hdr
*mld
;
3600 MLD_PRINTF(("%s: transmit 0x%llx\n", __func__
,
3601 (uint64_t)VM_KERNEL_ADDRPERM(m
)));
3604 * Check if the ifnet is still attached.
3606 ifp
= mld_restore_context(m
);
3607 if (ifp
== NULL
|| !ifnet_is_attached(ifp
, 0)) {
3608 MLD_PRINTF(("%s: dropped 0x%llx as ifindex %u went away.\n",
3609 __func__
, (uint64_t)VM_KERNEL_ADDRPERM(m
),
3612 ip6stat
.ip6s_noroute
++;
3616 im6o
= ip6_allocmoptions(Z_WAITOK
);
3622 im6o
->im6o_multicast_hlim
= 1;
3623 im6o
->im6o_multicast_loop
= 0;
3624 im6o
->im6o_multicast_ifp
= ifp
;
3626 if (m
->m_flags
& M_MLDV1
) {
3629 m0
= mld_v2_encap_report(ifp
, m
);
3631 MLD_PRINTF(("%s: dropped 0x%llx\n", __func__
,
3632 (uint64_t)VM_KERNEL_ADDRPERM(m
)));
3634 * mld_v2_encap_report() has already freed our mbuf.
3637 ip6stat
.ip6s_odropped
++;
3642 mld_scrub_context(m0
);
3643 m
->m_flags
&= ~(M_PROTOFLAGS
);
3644 m0
->m_pkthdr
.rcvif
= lo_ifp
;
3646 ip6
= mtod(m0
, struct ip6_hdr
*);
3647 (void)in6_setscope(&ip6
->ip6_dst
, ifp
, NULL
);
3650 * Retrieve the ICMPv6 type before handoff to ip6_output(),
3651 * so we can bump the stats.
3653 md
= m_getptr(m0
, sizeof(struct ip6_hdr
), &off
);
3654 mld
= (struct mld_hdr
*)(mtod(md
, uint8_t *) + off
);
3655 type
= mld
->mld_type
;
3657 if (ifp
->if_eflags
& IFEF_TXSTART
) {
3659 * Use control service class if the outgoing
3660 * interface supports transmit-start model.
3662 (void) m_set_service_class(m0
, MBUF_SC_CTL
);
3665 error
= ip6_output(m0
, &mld_po
, NULL
, IPV6_UNSPECSRC
, im6o
,
3671 MLD_PRINTF(("%s: ip6_output(0x%llx) = %d\n", __func__
,
3672 (uint64_t)VM_KERNEL_ADDRPERM(m0
), error
));
3674 ifnet_release(oifp
);
3679 icmp6stat
.icp6s_outhist
[type
]++;
3681 icmp6_ifstat_inc(oifp
, ifs6_out_msg
);
3683 case MLD_LISTENER_REPORT
:
3684 case MLDV2_LISTENER_REPORT
:
3685 icmp6_ifstat_inc(oifp
, ifs6_out_mldreport
);
3687 case MLD_LISTENER_DONE
:
3688 icmp6_ifstat_inc(oifp
, ifs6_out_mlddone
);
3691 ifnet_release(oifp
);
3696 * Encapsulate an MLDv2 report.
3698 * KAME IPv6 requires that hop-by-hop options be passed separately,
3699 * and that the IPv6 header be prepended in a separate mbuf.
3701 * Returns a pointer to the new mbuf chain head, or NULL if the
3702 * allocation failed.
3704 static struct mbuf
*
3705 mld_v2_encap_report(struct ifnet
*ifp
, struct mbuf
*m
)
3708 struct mldv2_report
*mld
;
3709 struct ip6_hdr
*ip6
;
3710 struct in6_ifaddr
*ia
;
3713 VERIFY(m
->m_flags
& M_PKTHDR
);
3716 * RFC3590: OK to send as :: or tentative during DAD.
3718 ia
= in6ifa_ifpforlinklocal(ifp
, IN6_IFF_NOTREADY
| IN6_IFF_ANYCAST
);
3720 MLD_PRINTF(("%s: warning: ia is NULL\n", __func__
));
3723 MGETHDR(mh
, M_DONTWAIT
, MT_HEADER
);
3726 IFA_REMREF(&ia
->ia_ifa
);
3731 MH_ALIGN(mh
, sizeof(struct ip6_hdr
) + sizeof(struct mldv2_report
));
3733 mldreclen
= m_length(m
);
3734 MLD_PRINTF(("%s: mldreclen is %d\n", __func__
, mldreclen
));
3736 mh
->m_len
= sizeof(struct ip6_hdr
) + sizeof(struct mldv2_report
);
3737 mh
->m_pkthdr
.len
= sizeof(struct ip6_hdr
) +
3738 sizeof(struct mldv2_report
) + mldreclen
;
3740 ip6
= mtod(mh
, struct ip6_hdr
*);
3742 ip6
->ip6_vfc
&= ~IPV6_VERSION_MASK
;
3743 ip6
->ip6_vfc
|= IPV6_VERSION
;
3744 ip6
->ip6_nxt
= IPPROTO_ICMPV6
;
3746 IFA_LOCK(&ia
->ia_ifa
);
3748 ip6
->ip6_src
= ia
? ia
->ia_addr
.sin6_addr
: in6addr_any
;
3750 IFA_UNLOCK(&ia
->ia_ifa
);
3751 IFA_REMREF(&ia
->ia_ifa
);
3754 ip6
->ip6_dst
= in6addr_linklocal_allv2routers
;
3755 /* scope ID will be set in netisr */
3757 mld
= (struct mldv2_report
*)(ip6
+ 1);
3758 mld
->mld_type
= MLDV2_LISTENER_REPORT
;
3761 mld
->mld_v2_reserved
= 0;
3762 mld
->mld_v2_numrecs
= htons(m
->m_pkthdr
.vt_nrecs
);
3763 m
->m_pkthdr
.vt_nrecs
= 0;
3764 m
->m_flags
&= ~M_PKTHDR
;
3767 mld
->mld_cksum
= in6_cksum(mh
, IPPROTO_ICMPV6
,
3768 sizeof(struct ip6_hdr
), sizeof(struct mldv2_report
) + mldreclen
);
3774 mld_rec_type_to_str(const int type
)
3777 case MLD_CHANGE_TO_EXCLUDE_MODE
:
3779 case MLD_CHANGE_TO_INCLUDE_MODE
:
3781 case MLD_MODE_IS_EXCLUDE
:
3783 case MLD_MODE_IS_INCLUDE
:
3785 case MLD_ALLOW_NEW_SOURCES
:
3787 case MLD_BLOCK_OLD_SOURCES
:
3799 MLD_PRINTF(("%s: initializing\n", __func__
));
3801 /* Setup lock group and attribute for mld_mtx */
3802 mld_mtx_grp_attr
= lck_grp_attr_alloc_init();
3803 mld_mtx_grp
= lck_grp_alloc_init("mld_mtx\n", mld_mtx_grp_attr
);
3804 mld_mtx_attr
= lck_attr_alloc_init();
3805 lck_mtx_init(&mld_mtx
, mld_mtx_grp
, mld_mtx_attr
);
3807 ip6_initpktopts(&mld_po
);
3808 mld_po
.ip6po_hlim
= 1;
3809 mld_po
.ip6po_hbh
= &mld_ra
.hbh
;
3810 mld_po
.ip6po_prefer_tempaddr
= IP6PO_TEMPADDR_NOTPREFER
;
3811 mld_po
.ip6po_flags
= IP6PO_DONTFRAG
;
3812 LIST_INIT(&mli_head
);