2 * Copyright (c) 2010-2017 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.
30 * All rights reserved.
32 * Redistribution and use in source and binary forms, with or without
33 * modification, are permitted provided that the following conditions
35 * 1. Redistributions of source code must retain the above copyright
36 * notice, this list of conditions and the following disclaimer.
37 * 2. Redistributions in binary form must reproduce the above copyright
38 * notice, this list of conditions and the following disclaimer in the
39 * documentation and/or other materials provided with the distribution.
40 * 3. The name of the author may not be used to endorse or promote
41 * products derived from this software without specific prior written
44 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
45 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
46 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
47 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
48 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
49 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
50 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
51 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
52 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
53 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
58 * IPv6 multicast socket, group, and socket option processing module.
59 * Normative references: RFC 2292, RFC 3492, RFC 3542, RFC 3678, RFC 3810.
62 #include <sys/cdefs.h>
64 #include <sys/param.h>
65 #include <sys/systm.h>
66 #include <sys/kernel.h>
67 #include <sys/malloc.h>
69 #include <sys/protosw.h>
70 #include <sys/socket.h>
71 #include <sys/socketvar.h>
72 #include <sys/protosw.h>
73 #include <sys/sysctl.h>
75 #include <sys/mcache.h>
77 #include <kern/zalloc.h>
79 #include <pexpert/pexpert.h>
82 #include <net/if_dl.h>
83 #include <net/net_api_stats.h>
84 #include <net/route.h>
86 #include <netinet/in.h>
87 #include <netinet/in_var.h>
88 #include <netinet6/in6_var.h>
89 #include <netinet/ip6.h>
90 #include <netinet/icmp6.h>
91 #include <netinet6/ip6_var.h>
92 #include <netinet/in_pcb.h>
93 #include <netinet/tcp.h>
94 #include <netinet/tcp_seq.h>
95 #include <netinet/tcp_var.h>
96 #include <netinet6/nd6.h>
97 #include <netinet6/mld6_var.h>
98 #include <netinet6/scope6_var.h>
100 static void im6f_commit(struct in6_mfilter
*);
101 static int im6f_get_source(struct in6_mfilter
*imf
,
102 const struct sockaddr_in6
*psin
,
103 struct in6_msource
**);
104 static struct in6_msource
*
105 im6f_graft(struct in6_mfilter
*, const uint8_t,
106 const struct sockaddr_in6
*);
107 static int im6f_prune(struct in6_mfilter
*, const struct sockaddr_in6
*);
108 static void im6f_rollback(struct in6_mfilter
*);
109 static void im6f_reap(struct in6_mfilter
*);
110 static int im6o_grow(struct ip6_moptions
*, size_t);
111 static size_t im6o_match_group(const struct ip6_moptions
*,
112 const struct ifnet
*, const struct sockaddr_in6
*);
113 static struct in6_msource
*
114 im6o_match_source(const struct ip6_moptions
*,
115 const size_t, const struct sockaddr_in6
*);
116 static void im6s_merge(struct ip6_msource
*ims
,
117 const struct in6_msource
*lims
, const int rollback
);
118 static int in6_mc_get(struct ifnet
*, const struct in6_addr
*,
119 struct in6_multi
**);
120 static int in6m_get_source(struct in6_multi
*inm
,
121 const struct in6_addr
*addr
, const int noalloc
,
122 struct ip6_msource
**pims
);
123 static int in6m_is_ifp_detached(const struct in6_multi
*);
124 static int in6m_merge(struct in6_multi
*, /*const*/ struct in6_mfilter
*);
125 static void in6m_reap(struct in6_multi
*);
126 static struct ip6_moptions
*
127 in6p_findmoptions(struct inpcb
*);
128 static int in6p_get_source_filters(struct inpcb
*, struct sockopt
*);
129 static int in6p_lookup_v4addr(struct ipv6_mreq
*, struct ip_mreq
*);
130 static int in6p_join_group(struct inpcb
*, struct sockopt
*);
131 static int in6p_leave_group(struct inpcb
*, struct sockopt
*);
132 static struct ifnet
*
133 in6p_lookup_mcast_ifp(const struct inpcb
*,
134 const struct sockaddr_in6
*);
135 static int in6p_block_unblock_source(struct inpcb
*, struct sockopt
*);
136 static int in6p_set_multicast_if(struct inpcb
*, struct sockopt
*);
137 static int in6p_set_source_filters(struct inpcb
*, struct sockopt
*);
138 static int sysctl_ip6_mcast_filters SYSCTL_HANDLER_ARGS
;
139 static __inline__
int ip6_msource_cmp(const struct ip6_msource
*,
140 const struct ip6_msource
*);
142 SYSCTL_DECL(_net_inet6_ip6
); /* XXX Not in any common header. */
144 SYSCTL_NODE(_net_inet6_ip6
, OID_AUTO
, mcast
, CTLFLAG_RW
| CTLFLAG_LOCKED
, 0, "IPv6 multicast");
146 static unsigned long in6_mcast_maxgrpsrc
= IPV6_MAX_GROUP_SRC_FILTER
;
147 SYSCTL_LONG(_net_inet6_ip6_mcast
, OID_AUTO
, maxgrpsrc
,
148 CTLFLAG_RW
| CTLFLAG_LOCKED
, &in6_mcast_maxgrpsrc
,
149 "Max source filters per group");
151 static unsigned long in6_mcast_maxsocksrc
= IPV6_MAX_SOCK_SRC_FILTER
;
152 SYSCTL_LONG(_net_inet6_ip6_mcast
, OID_AUTO
, maxsocksrc
,
153 CTLFLAG_RW
| CTLFLAG_LOCKED
, &in6_mcast_maxsocksrc
,
154 "Max source filters per socket");
156 int in6_mcast_loop
= IPV6_DEFAULT_MULTICAST_LOOP
;
157 SYSCTL_INT(_net_inet6_ip6_mcast
, OID_AUTO
, loop
, CTLFLAG_RW
| CTLFLAG_LOCKED
,
158 &in6_mcast_loop
, 0, "Loopback multicast datagrams by default");
160 SYSCTL_NODE(_net_inet6_ip6_mcast
, OID_AUTO
, filters
,
161 CTLFLAG_RD
| CTLFLAG_LOCKED
, sysctl_ip6_mcast_filters
,
162 "Per-interface stack-wide source filters");
164 RB_GENERATE_PREV(ip6_msource_tree
, ip6_msource
, im6s_link
, ip6_msource_cmp
);
166 #define IN6M_TRACE_HIST_SIZE 32 /* size of trace history */
169 __private_extern__
unsigned int in6m_trace_hist_size
= IN6M_TRACE_HIST_SIZE
;
171 struct in6_multi_dbg
{
172 struct in6_multi in6m
; /* in6_multi */
173 u_int16_t in6m_refhold_cnt
; /* # of ref */
174 u_int16_t in6m_refrele_cnt
; /* # of rele */
176 * Circular lists of in6m_addref and in6m_remref callers.
178 ctrace_t in6m_refhold
[IN6M_TRACE_HIST_SIZE
];
179 ctrace_t in6m_refrele
[IN6M_TRACE_HIST_SIZE
];
183 TAILQ_ENTRY(in6_multi_dbg
) in6m_trash_link
;
186 /* List of trash in6_multi entries protected by in6m_trash_lock */
187 static TAILQ_HEAD(, in6_multi_dbg
) in6m_trash_head
;
188 static decl_lck_mtx_data(, in6m_trash_lock
);
191 static unsigned int in6m_debug
= 1; /* debugging (enabled) */
193 static unsigned int in6m_debug
; /* debugging (disabled) */
195 static unsigned int in6m_size
; /* size of zone element */
196 static struct zone
*in6m_zone
; /* zone for in6_multi */
198 #define IN6M_ZONE_MAX 64 /* maximum elements in zone */
199 #define IN6M_ZONE_NAME "in6_multi" /* zone name */
201 static unsigned int imm_size
; /* size of zone element */
202 static struct zone
*imm_zone
; /* zone for in6_multi_mship */
204 #define IMM_ZONE_MAX 64 /* maximum elements in zone */
205 #define IMM_ZONE_NAME "in6_multi_mship" /* zone name */
207 #define IP6MS_ZONE_MAX 64 /* maximum elements in zone */
208 #define IP6MS_ZONE_NAME "ip6_msource" /* zone name */
210 static unsigned int ip6ms_size
; /* size of zone element */
211 static struct zone
*ip6ms_zone
; /* zone for ip6_msource */
213 #define IN6MS_ZONE_MAX 64 /* maximum elements in zone */
214 #define IN6MS_ZONE_NAME "in6_msource" /* zone name */
216 static unsigned int in6ms_size
; /* size of zone element */
217 static struct zone
*in6ms_zone
; /* zone for in6_msource */
219 /* Lock group and attribute for in6_multihead_lock lock */
220 static lck_attr_t
*in6_multihead_lock_attr
;
221 static lck_grp_t
*in6_multihead_lock_grp
;
222 static lck_grp_attr_t
*in6_multihead_lock_grp_attr
;
224 static decl_lck_rw_data(, in6_multihead_lock
);
225 struct in6_multihead in6_multihead
;
227 static struct in6_multi
*in6_multi_alloc(int);
228 static void in6_multi_free(struct in6_multi
*);
229 static void in6_multi_attach(struct in6_multi
*);
230 static struct in6_multi_mship
*in6_multi_mship_alloc(int);
231 static void in6_multi_mship_free(struct in6_multi_mship
*);
232 static void in6m_trace(struct in6_multi
*, int);
234 static struct ip6_msource
*ip6ms_alloc(int);
235 static void ip6ms_free(struct ip6_msource
*);
236 static struct in6_msource
*in6ms_alloc(int);
237 static void in6ms_free(struct in6_msource
*);
240 * IPv6 source tree comparison function.
242 * An ordered predicate is necessary; bcmp() is not documented to return
243 * an indication of order, memcmp() is, and is an ISO C99 requirement.
246 ip6_msource_cmp(const struct ip6_msource
*a
, const struct ip6_msource
*b
)
248 return memcmp(&a
->im6s_addr
, &b
->im6s_addr
, sizeof(struct in6_addr
));
252 * Inline function which wraps assertions for a valid ifp.
254 static __inline__
int
255 in6m_is_ifp_detached(const struct in6_multi
*inm
)
257 VERIFY(inm
->in6m_ifma
!= NULL
);
258 VERIFY(inm
->in6m_ifp
== inm
->in6m_ifma
->ifma_ifp
);
260 return !ifnet_is_attached(inm
->in6m_ifp
, 0);
264 * Initialize an in6_mfilter structure to a known state at t0, t1
265 * with an empty source filter list.
267 static __inline__
void
268 im6f_init(struct in6_mfilter
*imf
, const int st0
, const int st1
)
270 memset(imf
, 0, sizeof(struct in6_mfilter
));
271 RB_INIT(&imf
->im6f_sources
);
272 imf
->im6f_st
[0] = st0
;
273 imf
->im6f_st
[1] = st1
;
277 * Resize the ip6_moptions vector to the next power-of-two minus 1.
280 im6o_grow(struct ip6_moptions
*imo
, size_t newmax
)
282 struct in6_multi
**nmships
;
283 struct in6_multi
**omships
;
284 struct in6_mfilter
*nmfilters
;
285 struct in6_mfilter
*omfilters
;
289 IM6O_LOCK_ASSERT_HELD(imo
);
293 omships
= imo
->im6o_membership
;
294 omfilters
= imo
->im6o_mfilters
;
295 oldmax
= imo
->im6o_max_memberships
;
297 newmax
= ((oldmax
+ 1) * 2) - 1;
300 if (newmax
> IPV6_MAX_MEMBERSHIPS
) {
304 if ((nmships
= (struct in6_multi
**)_REALLOC(omships
,
305 sizeof(struct in6_multi
*) * newmax
, M_IP6MOPTS
,
306 M_WAITOK
| M_ZERO
)) == NULL
) {
310 imo
->im6o_membership
= nmships
;
312 if ((nmfilters
= (struct in6_mfilter
*)_REALLOC(omfilters
,
313 sizeof(struct in6_mfilter
) * newmax
, M_IN6MFILTER
,
314 M_WAITOK
| M_ZERO
)) == NULL
) {
318 imo
->im6o_mfilters
= nmfilters
;
320 /* Initialize newly allocated source filter heads. */
321 for (idx
= oldmax
; idx
< newmax
; idx
++) {
322 im6f_init(&nmfilters
[idx
], MCAST_UNDEFINED
, MCAST_EXCLUDE
);
325 imo
->im6o_max_memberships
= newmax
;
331 * Find an IPv6 multicast group entry for this ip6_moptions instance
332 * which matches the specified group, and optionally an interface.
333 * Return its index into the array, or -1 if not found.
336 im6o_match_group(const struct ip6_moptions
*imo
, const struct ifnet
*ifp
,
337 const struct sockaddr_in6
*group
)
339 const struct sockaddr_in6
*gsin6
;
340 struct in6_multi
*pinm
;
344 IM6O_LOCK_ASSERT_HELD(__DECONST(struct ip6_moptions
*, imo
));
348 /* The im6o_membership array may be lazy allocated. */
349 if (imo
->im6o_membership
== NULL
|| imo
->im6o_num_memberships
== 0) {
353 nmships
= imo
->im6o_num_memberships
;
354 for (idx
= 0; idx
< nmships
; idx
++) {
355 pinm
= imo
->im6o_membership
[idx
];
360 if ((ifp
== NULL
|| (pinm
->in6m_ifp
== ifp
)) &&
361 IN6_ARE_ADDR_EQUAL(&pinm
->in6m_addr
,
362 &gsin6
->sin6_addr
)) {
368 if (idx
>= nmships
) {
376 * Find an IPv6 multicast source entry for this imo which matches
377 * the given group index for this socket, and source address.
379 * XXX TODO: The scope ID, if present in src, is stripped before
380 * any comparison. We SHOULD enforce scope/zone checks where the source
381 * filter entry has a link scope.
383 * NOTE: This does not check if the entry is in-mode, merely if
384 * it exists, which may not be the desired behaviour.
386 static struct in6_msource
*
387 im6o_match_source(const struct ip6_moptions
*imo
, const size_t gidx
,
388 const struct sockaddr_in6
*src
)
390 struct ip6_msource find
;
391 struct in6_mfilter
*imf
;
392 struct ip6_msource
*ims
;
393 const struct sockaddr_in6
*psa
;
395 IM6O_LOCK_ASSERT_HELD(__DECONST(struct ip6_moptions
*, imo
));
397 VERIFY(src
->sin6_family
== AF_INET6
);
398 VERIFY(gidx
!= (size_t)-1 && gidx
< imo
->im6o_num_memberships
);
400 /* The im6o_mfilters array may be lazy allocated. */
401 if (imo
->im6o_mfilters
== NULL
) {
404 imf
= &imo
->im6o_mfilters
[gidx
];
407 find
.im6s_addr
= psa
->sin6_addr
;
408 in6_clearscope(&find
.im6s_addr
); /* XXX */
409 ims
= RB_FIND(ip6_msource_tree
, &imf
->im6f_sources
, &find
);
411 return (struct in6_msource
*)ims
;
415 * Perform filtering for multicast datagrams on a socket by group and source.
417 * Returns 0 if a datagram should be allowed through, or various error codes
418 * if the socket was not a member of the group, or the source was muted, etc.
421 im6o_mc_filter(const struct ip6_moptions
*imo
, const struct ifnet
*ifp
,
422 const struct sockaddr_in6
*group
, const struct sockaddr_in6
*src
)
425 struct in6_msource
*ims
;
428 IM6O_LOCK_ASSERT_HELD(__DECONST(struct ip6_moptions
*, imo
));
431 gidx
= im6o_match_group(imo
, ifp
, group
);
432 if (gidx
== (size_t)-1) {
433 return MCAST_NOTGMEMBER
;
437 * Check if the source was included in an (S,G) join.
438 * Allow reception on exclusive memberships by default,
439 * reject reception on inclusive memberships by default.
440 * Exclude source only if an in-mode exclude filter exists.
441 * Include source only if an in-mode include filter exists.
442 * NOTE: We are comparing group state here at MLD t1 (now)
443 * with socket-layer t0 (since last downcall).
445 mode
= imo
->im6o_mfilters
[gidx
].im6f_st
[1];
446 ims
= im6o_match_source(imo
, gidx
, src
);
448 if ((ims
== NULL
&& mode
== MCAST_INCLUDE
) ||
449 (ims
!= NULL
&& ims
->im6sl_st
[0] != mode
)) {
450 return MCAST_NOTSMEMBER
;
457 * Find and return a reference to an in6_multi record for (ifp, group),
458 * and bump its reference count.
459 * If one does not exist, try to allocate it, and update link-layer multicast
460 * filters on ifp to listen for group.
461 * Assumes the IN6_MULTI lock is held across the call.
462 * Return 0 if successful, otherwise return an appropriate error code.
465 in6_mc_get(struct ifnet
*ifp
, const struct in6_addr
*group
,
466 struct in6_multi
**pinm
)
468 struct sockaddr_in6 gsin6
;
469 struct ifmultiaddr
*ifma
;
470 struct in6_multi
*inm
;
475 in6_multihead_lock_shared();
476 IN6_LOOKUP_MULTI(group
, ifp
, inm
);
479 VERIFY(inm
->in6m_reqcnt
>= 1);
481 VERIFY(inm
->in6m_reqcnt
!= 0);
484 in6_multihead_lock_done();
486 * We already joined this group; return the in6m
487 * with a refcount held (via lookup) for caller.
491 in6_multihead_lock_done();
493 memset(&gsin6
, 0, sizeof(gsin6
));
494 gsin6
.sin6_family
= AF_INET6
;
495 gsin6
.sin6_len
= sizeof(struct sockaddr_in6
);
496 gsin6
.sin6_addr
= *group
;
499 * Check if a link-layer group is already associated
500 * with this network-layer group on the given ifnet.
502 error
= if_addmulti(ifp
, (struct sockaddr
*)&gsin6
, &ifma
);
508 * See comments in in6m_remref() for access to ifma_protospec.
510 in6_multihead_lock_exclusive();
512 if ((inm
= ifma
->ifma_protospec
) != NULL
) {
513 VERIFY(ifma
->ifma_addr
!= NULL
);
514 VERIFY(ifma
->ifma_addr
->sa_family
== AF_INET6
);
515 IN6M_ADDREF(inm
); /* for caller */
518 VERIFY(inm
->in6m_ifma
== ifma
);
519 VERIFY(inm
->in6m_ifp
== ifp
);
520 VERIFY(IN6_ARE_ADDR_EQUAL(&inm
->in6m_addr
, group
));
521 if (inm
->in6m_debug
& IFD_ATTACHED
) {
522 VERIFY(inm
->in6m_reqcnt
>= 1);
524 VERIFY(inm
->in6m_reqcnt
!= 0);
527 in6_multihead_lock_done();
530 * We lost the race with another thread doing
531 * in6_mc_get(); since this group has already
532 * been joined; return the inm with a refcount
538 * We lost the race with another thread doing in6_delmulti();
539 * the inm referring to the ifma has been detached, thus we
540 * reattach it back to the in6_multihead list, and return the
541 * inm with a refcount held for the caller.
543 in6_multi_attach(inm
);
544 VERIFY((inm
->in6m_debug
&
545 (IFD_ATTACHED
| IFD_TRASHED
)) == IFD_ATTACHED
);
548 in6_multihead_lock_done();
555 * A new in6_multi record is needed; allocate and initialize it.
556 * We DO NOT perform an MLD join as the in6_ layer may need to
557 * push an initial source list down to MLD to support SSM.
559 * The initial source filter state is INCLUDE, {} as per the RFC.
560 * Pending state-changes per group are subject to a bounds check.
562 inm
= in6_multi_alloc(M_WAITOK
);
564 in6_multihead_lock_done();
569 inm
->in6m_addr
= *group
;
571 inm
->in6m_mli
= MLD_IFINFO(ifp
);
572 VERIFY(inm
->in6m_mli
!= NULL
);
573 MLI_ADDREF(inm
->in6m_mli
);
574 inm
->in6m_ifma
= ifma
; /* keep refcount from if_addmulti() */
575 inm
->in6m_state
= MLD_NOT_MEMBER
;
577 * Pending state-changes per group are subject to a bounds check.
579 inm
->in6m_scq
.ifq_maxlen
= MLD_MAX_STATE_CHANGES
;
580 inm
->in6m_st
[0].iss_fmode
= MCAST_UNDEFINED
;
581 inm
->in6m_st
[1].iss_fmode
= MCAST_UNDEFINED
;
582 RB_INIT(&inm
->in6m_srcs
);
584 in6_multi_attach(inm
);
585 VERIFY((inm
->in6m_debug
&
586 (IFD_ATTACHED
| IFD_TRASHED
)) == IFD_ATTACHED
);
587 IN6M_ADDREF_LOCKED(inm
); /* for caller */
591 VERIFY(ifma
->ifma_protospec
== NULL
);
592 ifma
->ifma_protospec
= inm
;
594 in6_multihead_lock_done();
600 * Clear recorded source entries for a group.
601 * Used by the MLD code. Caller must hold the IN6_MULTI lock.
602 * FIXME: Should reap.
605 in6m_clear_recorded(struct in6_multi
*inm
)
607 struct ip6_msource
*ims
;
609 IN6M_LOCK_ASSERT_HELD(inm
);
611 RB_FOREACH(ims
, ip6_msource_tree
, &inm
->in6m_srcs
) {
614 --inm
->in6m_st
[1].iss_rec
;
617 VERIFY(inm
->in6m_st
[1].iss_rec
== 0);
621 * Record a source as pending for a Source-Group MLDv2 query.
622 * This lives here as it modifies the shared tree.
624 * inm is the group descriptor.
625 * naddr is the address of the source to record in network-byte order.
627 * If the net.inet6.mld.sgalloc sysctl is non-zero, we will
628 * lazy-allocate a source node in response to an SG query.
629 * Otherwise, no allocation is performed. This saves some memory
630 * with the trade-off that the source will not be reported to the
631 * router if joined in the window between the query response and
632 * the group actually being joined on the local host.
634 * VIMAGE: XXX: Currently the mld_sgalloc feature has been removed.
635 * This turns off the allocation of a recorded source entry if
636 * the group has not been joined.
638 * Return 0 if the source didn't exist or was already marked as recorded.
639 * Return 1 if the source was marked as recorded by this function.
640 * Return <0 if any error occured (negated errno code).
643 in6m_record_source(struct in6_multi
*inm
, const struct in6_addr
*addr
)
645 struct ip6_msource find
;
646 struct ip6_msource
*ims
, *nims
;
648 IN6M_LOCK_ASSERT_HELD(inm
);
650 find
.im6s_addr
= *addr
;
651 ims
= RB_FIND(ip6_msource_tree
, &inm
->in6m_srcs
, &find
);
652 if (ims
&& ims
->im6s_stp
) {
656 if (inm
->in6m_nsrc
== in6_mcast_maxgrpsrc
) {
659 nims
= ip6ms_alloc(M_WAITOK
);
663 nims
->im6s_addr
= find
.im6s_addr
;
664 RB_INSERT(ip6_msource_tree
, &inm
->in6m_srcs
, nims
);
670 * Mark the source as recorded and update the recorded
674 ++inm
->in6m_st
[1].iss_rec
;
680 * Return a pointer to an in6_msource owned by an in6_mfilter,
681 * given its source address.
682 * Lazy-allocate if needed. If this is a new entry its filter state is
685 * imf is the filter set being modified.
686 * addr is the source address.
688 * Caller is expected to be holding im6o_lock.
691 im6f_get_source(struct in6_mfilter
*imf
, const struct sockaddr_in6
*psin
,
692 struct in6_msource
**plims
)
694 struct ip6_msource find
;
695 struct ip6_msource
*ims
;
696 struct in6_msource
*lims
;
703 find
.im6s_addr
= psin
->sin6_addr
;
704 ims
= RB_FIND(ip6_msource_tree
, &imf
->im6f_sources
, &find
);
705 lims
= (struct in6_msource
*)ims
;
707 if (imf
->im6f_nsrc
== in6_mcast_maxsocksrc
) {
710 lims
= in6ms_alloc(M_WAITOK
);
714 lims
->im6s_addr
= find
.im6s_addr
;
715 lims
->im6sl_st
[0] = MCAST_UNDEFINED
;
716 RB_INSERT(ip6_msource_tree
, &imf
->im6f_sources
,
717 (struct ip6_msource
*)lims
);
727 * Graft a source entry into an existing socket-layer filter set,
728 * maintaining any required invariants and checking allocations.
730 * The source is marked as being in the new filter mode at t1.
732 * Return the pointer to the new node, otherwise return NULL.
734 * Caller is expected to be holding im6o_lock.
736 static struct in6_msource
*
737 im6f_graft(struct in6_mfilter
*imf
, const uint8_t st1
,
738 const struct sockaddr_in6
*psin
)
740 struct in6_msource
*lims
;
742 lims
= in6ms_alloc(M_WAITOK
);
746 lims
->im6s_addr
= psin
->sin6_addr
;
747 lims
->im6sl_st
[0] = MCAST_UNDEFINED
;
748 lims
->im6sl_st
[1] = st1
;
749 RB_INSERT(ip6_msource_tree
, &imf
->im6f_sources
,
750 (struct ip6_msource
*)lims
);
757 * Prune a source entry from an existing socket-layer filter set,
758 * maintaining any required invariants and checking allocations.
760 * The source is marked as being left at t1, it is not freed.
762 * Return 0 if no error occurred, otherwise return an errno value.
764 * Caller is expected to be holding im6o_lock.
767 im6f_prune(struct in6_mfilter
*imf
, const struct sockaddr_in6
*psin
)
769 struct ip6_msource find
;
770 struct ip6_msource
*ims
;
771 struct in6_msource
*lims
;
773 find
.im6s_addr
= psin
->sin6_addr
;
774 ims
= RB_FIND(ip6_msource_tree
, &imf
->im6f_sources
, &find
);
778 lims
= (struct in6_msource
*)ims
;
779 lims
->im6sl_st
[1] = MCAST_UNDEFINED
;
784 * Revert socket-layer filter set deltas at t1 to t0 state.
786 * Caller is expected to be holding im6o_lock.
789 im6f_rollback(struct in6_mfilter
*imf
)
791 struct ip6_msource
*ims
, *tims
;
792 struct in6_msource
*lims
;
794 RB_FOREACH_SAFE(ims
, ip6_msource_tree
, &imf
->im6f_sources
, tims
) {
795 lims
= (struct in6_msource
*)ims
;
796 if (lims
->im6sl_st
[0] == lims
->im6sl_st
[1]) {
797 /* no change at t1 */
799 } else if (lims
->im6sl_st
[0] != MCAST_UNDEFINED
) {
800 /* revert change to existing source at t1 */
801 lims
->im6sl_st
[1] = lims
->im6sl_st
[0];
803 /* revert source added t1 */
804 MLD_PRINTF(("%s: free in6ms 0x%llx\n", __func__
,
805 (uint64_t)VM_KERNEL_ADDRPERM(lims
)));
806 RB_REMOVE(ip6_msource_tree
, &imf
->im6f_sources
, ims
);
811 imf
->im6f_st
[1] = imf
->im6f_st
[0];
815 * Mark socket-layer filter set as INCLUDE {} at t1.
817 * Caller is expected to be holding im6o_lock.
820 im6f_leave(struct in6_mfilter
*imf
)
822 struct ip6_msource
*ims
;
823 struct in6_msource
*lims
;
825 RB_FOREACH(ims
, ip6_msource_tree
, &imf
->im6f_sources
) {
826 lims
= (struct in6_msource
*)ims
;
827 lims
->im6sl_st
[1] = MCAST_UNDEFINED
;
829 imf
->im6f_st
[1] = MCAST_INCLUDE
;
833 * Mark socket-layer filter set deltas as committed.
835 * Caller is expected to be holding im6o_lock.
838 im6f_commit(struct in6_mfilter
*imf
)
840 struct ip6_msource
*ims
;
841 struct in6_msource
*lims
;
843 RB_FOREACH(ims
, ip6_msource_tree
, &imf
->im6f_sources
) {
844 lims
= (struct in6_msource
*)ims
;
845 lims
->im6sl_st
[0] = lims
->im6sl_st
[1];
847 imf
->im6f_st
[0] = imf
->im6f_st
[1];
851 * Reap unreferenced sources from socket-layer filter set.
853 * Caller is expected to be holding im6o_lock.
856 im6f_reap(struct in6_mfilter
*imf
)
858 struct ip6_msource
*ims
, *tims
;
859 struct in6_msource
*lims
;
861 RB_FOREACH_SAFE(ims
, ip6_msource_tree
, &imf
->im6f_sources
, tims
) {
862 lims
= (struct in6_msource
*)ims
;
863 if ((lims
->im6sl_st
[0] == MCAST_UNDEFINED
) &&
864 (lims
->im6sl_st
[1] == MCAST_UNDEFINED
)) {
865 MLD_PRINTF(("%s: free in6ms 0x%llx\n", __func__
,
866 (uint64_t)VM_KERNEL_ADDRPERM(lims
)));
867 RB_REMOVE(ip6_msource_tree
, &imf
->im6f_sources
, ims
);
875 * Purge socket-layer filter set.
877 * Caller is expected to be holding im6o_lock.
880 im6f_purge(struct in6_mfilter
*imf
)
882 struct ip6_msource
*ims
, *tims
;
883 struct in6_msource
*lims
;
885 RB_FOREACH_SAFE(ims
, ip6_msource_tree
, &imf
->im6f_sources
, tims
) {
886 lims
= (struct in6_msource
*)ims
;
887 MLD_PRINTF(("%s: free in6ms 0x%llx\n", __func__
,
888 (uint64_t)VM_KERNEL_ADDRPERM(lims
)));
889 RB_REMOVE(ip6_msource_tree
, &imf
->im6f_sources
, ims
);
893 imf
->im6f_st
[0] = imf
->im6f_st
[1] = MCAST_UNDEFINED
;
894 VERIFY(RB_EMPTY(&imf
->im6f_sources
));
898 * Look up a source filter entry for a multicast group.
900 * inm is the group descriptor to work with.
901 * addr is the IPv6 address to look up.
902 * noalloc may be non-zero to suppress allocation of sources.
903 * *pims will be set to the address of the retrieved or allocated source.
905 * Return 0 if successful, otherwise return a non-zero error code.
908 in6m_get_source(struct in6_multi
*inm
, const struct in6_addr
*addr
,
909 const int noalloc
, struct ip6_msource
**pims
)
911 struct ip6_msource find
;
912 struct ip6_msource
*ims
, *nims
;
914 IN6M_LOCK_ASSERT_HELD(inm
);
916 find
.im6s_addr
= *addr
;
917 ims
= RB_FIND(ip6_msource_tree
, &inm
->in6m_srcs
, &find
);
918 if (ims
== NULL
&& !noalloc
) {
919 if (inm
->in6m_nsrc
== in6_mcast_maxgrpsrc
) {
922 nims
= ip6ms_alloc(M_WAITOK
);
926 nims
->im6s_addr
= *addr
;
927 RB_INSERT(ip6_msource_tree
, &inm
->in6m_srcs
, nims
);
930 MLD_PRINTF(("%s: allocated %s as 0x%llx\n", __func__
,
931 ip6_sprintf(addr
), (uint64_t)VM_KERNEL_ADDRPERM(ims
)));
939 * Helper function to derive the filter mode on a source entry
940 * from its internal counters. Predicates are:
941 * A source is only excluded if all listeners exclude it.
942 * A source is only included if no listeners exclude it,
943 * and at least one listener includes it.
944 * May be used by ifmcstat(8).
947 im6s_get_mode(const struct in6_multi
*inm
, const struct ip6_msource
*ims
,
950 IN6M_LOCK_ASSERT_HELD(__DECONST(struct in6_multi
*, inm
));
953 if (inm
->in6m_st
[t
].iss_ex
> 0 &&
954 inm
->in6m_st
[t
].iss_ex
== ims
->im6s_st
[t
].ex
) {
955 return MCAST_EXCLUDE
;
956 } else if (ims
->im6s_st
[t
].in
> 0 && ims
->im6s_st
[t
].ex
== 0) {
957 return MCAST_INCLUDE
;
959 return MCAST_UNDEFINED
;
963 * Merge socket-layer source into MLD-layer source.
964 * If rollback is non-zero, perform the inverse of the merge.
967 im6s_merge(struct ip6_msource
*ims
, const struct in6_msource
*lims
,
970 int n
= rollback
? -1 : 1;
972 if (lims
->im6sl_st
[0] == MCAST_EXCLUDE
) {
973 MLD_PRINTF(("%s: t1 ex -= %d on %s\n", __func__
, n
,
974 ip6_sprintf(&lims
->im6s_addr
)));
975 ims
->im6s_st
[1].ex
-= n
;
976 } else if (lims
->im6sl_st
[0] == MCAST_INCLUDE
) {
977 MLD_PRINTF(("%s: t1 in -= %d on %s\n", __func__
, n
,
978 ip6_sprintf(&lims
->im6s_addr
)));
979 ims
->im6s_st
[1].in
-= n
;
982 if (lims
->im6sl_st
[1] == MCAST_EXCLUDE
) {
983 MLD_PRINTF(("%s: t1 ex += %d on %s\n", __func__
, n
,
984 ip6_sprintf(&lims
->im6s_addr
)));
985 ims
->im6s_st
[1].ex
+= n
;
986 } else if (lims
->im6sl_st
[1] == MCAST_INCLUDE
) {
987 MLD_PRINTF(("%s: t1 in += %d on %s\n", __func__
, n
,
988 ip6_sprintf(&lims
->im6s_addr
)));
989 ims
->im6s_st
[1].in
+= n
;
994 * Atomically update the global in6_multi state, when a membership's
995 * filter list is being updated in any way.
997 * imf is the per-inpcb-membership group filter pointer.
998 * A fake imf may be passed for in-kernel consumers.
1000 * XXX This is a candidate for a set-symmetric-difference style loop
1001 * which would eliminate the repeated lookup from root of ims nodes,
1002 * as they share the same key space.
1004 * If any error occurred this function will back out of refcounts
1005 * and return a non-zero value.
1008 in6m_merge(struct in6_multi
*inm
, /*const*/ struct in6_mfilter
*imf
)
1010 struct ip6_msource
*ims
, *nims
= NULL
;
1011 struct in6_msource
*lims
;
1012 int schanged
, error
;
1015 IN6M_LOCK_ASSERT_HELD(inm
);
1022 * Update the source filters first, as this may fail.
1023 * Maintain count of in-mode filters at t0, t1. These are
1024 * used to work out if we transition into ASM mode or not.
1025 * Maintain a count of source filters whose state was
1026 * actually modified by this operation.
1028 RB_FOREACH(ims
, ip6_msource_tree
, &imf
->im6f_sources
) {
1029 lims
= (struct in6_msource
*)ims
;
1030 if (lims
->im6sl_st
[0] == imf
->im6f_st
[0]) {
1033 if (lims
->im6sl_st
[1] == imf
->im6f_st
[1]) {
1036 if (lims
->im6sl_st
[0] == lims
->im6sl_st
[1]) {
1039 error
= in6m_get_source(inm
, &lims
->im6s_addr
, 0, &nims
);
1044 im6s_merge(nims
, lims
, 0);
1047 struct ip6_msource
*bims
;
1049 RB_FOREACH_REVERSE_FROM(ims
, ip6_msource_tree
, nims
) {
1050 lims
= (struct in6_msource
*)ims
;
1051 if (lims
->im6sl_st
[0] == lims
->im6sl_st
[1]) {
1054 (void) in6m_get_source(inm
, &lims
->im6s_addr
, 1, &bims
);
1058 im6s_merge(bims
, lims
, 1);
1063 MLD_PRINTF(("%s: imf filters in-mode: %d at t0, %d at t1\n",
1064 __func__
, nsrc0
, nsrc1
));
1066 /* Handle transition between INCLUDE {n} and INCLUDE {} on socket. */
1067 if (imf
->im6f_st
[0] == imf
->im6f_st
[1] &&
1068 imf
->im6f_st
[1] == MCAST_INCLUDE
) {
1070 MLD_PRINTF(("%s: --in on inm at t1\n", __func__
));
1071 --inm
->in6m_st
[1].iss_in
;
1075 /* Handle filter mode transition on socket. */
1076 if (imf
->im6f_st
[0] != imf
->im6f_st
[1]) {
1077 MLD_PRINTF(("%s: imf transition %d to %d\n",
1078 __func__
, imf
->im6f_st
[0], imf
->im6f_st
[1]));
1080 if (imf
->im6f_st
[0] == MCAST_EXCLUDE
) {
1081 MLD_PRINTF(("%s: --ex on inm at t1\n", __func__
));
1082 --inm
->in6m_st
[1].iss_ex
;
1083 } else if (imf
->im6f_st
[0] == MCAST_INCLUDE
) {
1084 MLD_PRINTF(("%s: --in on inm at t1\n", __func__
));
1085 --inm
->in6m_st
[1].iss_in
;
1088 if (imf
->im6f_st
[1] == MCAST_EXCLUDE
) {
1089 MLD_PRINTF(("%s: ex++ on inm at t1\n", __func__
));
1090 inm
->in6m_st
[1].iss_ex
++;
1091 } else if (imf
->im6f_st
[1] == MCAST_INCLUDE
&& nsrc1
> 0) {
1092 MLD_PRINTF(("%s: in++ on inm at t1\n", __func__
));
1093 inm
->in6m_st
[1].iss_in
++;
1098 * Track inm filter state in terms of listener counts.
1099 * If there are any exclusive listeners, stack-wide
1100 * membership is exclusive.
1101 * Otherwise, if only inclusive listeners, stack-wide is inclusive.
1102 * If no listeners remain, state is undefined at t1,
1103 * and the MLD lifecycle for this group should finish.
1105 if (inm
->in6m_st
[1].iss_ex
> 0) {
1106 MLD_PRINTF(("%s: transition to EX\n", __func__
));
1107 inm
->in6m_st
[1].iss_fmode
= MCAST_EXCLUDE
;
1108 } else if (inm
->in6m_st
[1].iss_in
> 0) {
1109 MLD_PRINTF(("%s: transition to IN\n", __func__
));
1110 inm
->in6m_st
[1].iss_fmode
= MCAST_INCLUDE
;
1112 MLD_PRINTF(("%s: transition to UNDEF\n", __func__
));
1113 inm
->in6m_st
[1].iss_fmode
= MCAST_UNDEFINED
;
1116 /* Decrement ASM listener count on transition out of ASM mode. */
1117 if (imf
->im6f_st
[0] == MCAST_EXCLUDE
&& nsrc0
== 0) {
1118 if ((imf
->im6f_st
[1] != MCAST_EXCLUDE
) ||
1119 (imf
->im6f_st
[1] == MCAST_EXCLUDE
&& nsrc1
> 0)) {
1120 MLD_PRINTF(("%s: --asm on inm at t1\n", __func__
));
1121 --inm
->in6m_st
[1].iss_asm
;
1125 /* Increment ASM listener count on transition to ASM mode. */
1126 if (imf
->im6f_st
[1] == MCAST_EXCLUDE
&& nsrc1
== 0) {
1127 MLD_PRINTF(("%s: asm++ on inm at t1\n", __func__
));
1128 inm
->in6m_st
[1].iss_asm
++;
1131 MLD_PRINTF(("%s: merged imf 0x%llx to inm 0x%llx\n", __func__
,
1132 (uint64_t)VM_KERNEL_ADDRPERM(imf
),
1133 (uint64_t)VM_KERNEL_ADDRPERM(inm
)));
1138 MLD_PRINTF(("%s: sources changed; reaping\n", __func__
));
1145 * Mark an in6_multi's filter set deltas as committed.
1146 * Called by MLD after a state change has been enqueued.
1149 in6m_commit(struct in6_multi
*inm
)
1151 struct ip6_msource
*ims
;
1153 IN6M_LOCK_ASSERT_HELD(inm
);
1155 MLD_PRINTF(("%s: commit inm 0x%llx\n", __func__
,
1156 (uint64_t)VM_KERNEL_ADDRPERM(inm
)));
1157 MLD_PRINTF(("%s: pre commit:\n", __func__
));
1160 RB_FOREACH(ims
, ip6_msource_tree
, &inm
->in6m_srcs
) {
1161 ims
->im6s_st
[0] = ims
->im6s_st
[1];
1163 inm
->in6m_st
[0] = inm
->in6m_st
[1];
1167 * Reap unreferenced nodes from an in6_multi's filter set.
1170 in6m_reap(struct in6_multi
*inm
)
1172 struct ip6_msource
*ims
, *tims
;
1174 IN6M_LOCK_ASSERT_HELD(inm
);
1176 RB_FOREACH_SAFE(ims
, ip6_msource_tree
, &inm
->in6m_srcs
, tims
) {
1177 if (ims
->im6s_st
[0].ex
> 0 || ims
->im6s_st
[0].in
> 0 ||
1178 ims
->im6s_st
[1].ex
> 0 || ims
->im6s_st
[1].in
> 0 ||
1179 ims
->im6s_stp
!= 0) {
1182 MLD_PRINTF(("%s: free ims 0x%llx\n", __func__
,
1183 (uint64_t)VM_KERNEL_ADDRPERM(ims
)));
1184 RB_REMOVE(ip6_msource_tree
, &inm
->in6m_srcs
, ims
);
1191 * Purge all source nodes from an in6_multi's filter set.
1194 in6m_purge(struct in6_multi
*inm
)
1196 struct ip6_msource
*ims
, *tims
;
1198 IN6M_LOCK_ASSERT_HELD(inm
);
1200 RB_FOREACH_SAFE(ims
, ip6_msource_tree
, &inm
->in6m_srcs
, tims
) {
1201 MLD_PRINTF(("%s: free ims 0x%llx\n", __func__
,
1202 (uint64_t)VM_KERNEL_ADDRPERM(ims
)));
1203 RB_REMOVE(ip6_msource_tree
, &inm
->in6m_srcs
, ims
);
1210 * Join a multicast address w/o sources.
1211 * KAME compatibility entry point.
1214 struct in6_multi_mship
*
1215 in6_joingroup(struct ifnet
*ifp
, struct in6_addr
*mcaddr
,
1216 int *errorp
, int delay
)
1218 struct in6_multi_mship
*imm
;
1223 imm
= in6_multi_mship_alloc(M_WAITOK
);
1229 error
= in6_mc_join(ifp
, mcaddr
, NULL
, &imm
->i6mm_maddr
, delay
);
1232 in6_multi_mship_free(imm
);
1240 * Leave a multicast address w/o sources.
1241 * KAME compatibility entry point.
1244 in6_leavegroup(struct in6_multi_mship
*imm
)
1246 if (imm
->i6mm_maddr
!= NULL
) {
1247 in6_mc_leave(imm
->i6mm_maddr
, NULL
);
1248 IN6M_REMREF(imm
->i6mm_maddr
);
1249 imm
->i6mm_maddr
= NULL
;
1251 in6_multi_mship_free(imm
);
1256 * Join a multicast group; real entry point.
1258 * Only preserves atomicity at inm level.
1259 * NOTE: imf argument cannot be const due to sys/tree.h limitations.
1261 * If the MLD downcall fails, the group is not joined, and an error
1265 in6_mc_join(struct ifnet
*ifp
, const struct in6_addr
*mcaddr
,
1266 /*const*/ struct in6_mfilter
*imf
, struct in6_multi
**pinm
,
1269 struct in6_mfilter timf
;
1270 struct in6_multi
*inm
= NULL
;
1272 struct mld_tparams mtp
;
1275 * Sanity: Check scope zone ID was set for ifp, if and
1276 * only if group is scoped to an interface.
1278 VERIFY(IN6_IS_ADDR_MULTICAST(mcaddr
));
1279 if (IN6_IS_ADDR_MC_LINKLOCAL(mcaddr
) ||
1280 IN6_IS_ADDR_MC_INTFACELOCAL(mcaddr
)) {
1281 VERIFY(mcaddr
->s6_addr16
[1] != 0);
1284 MLD_PRINTF(("%s: join %s on 0x%llx(%s))\n", __func__
,
1285 ip6_sprintf(mcaddr
), (uint64_t)VM_KERNEL_ADDRPERM(ifp
),
1288 bzero(&mtp
, sizeof(mtp
));
1292 * If no imf was specified (i.e. kernel consumer),
1293 * fake one up and assume it is an ASM join.
1296 im6f_init(&timf
, MCAST_UNDEFINED
, MCAST_EXCLUDE
);
1300 error
= in6_mc_get(ifp
, mcaddr
, &inm
);
1302 MLD_PRINTF(("%s: in6_mc_get() failure\n", __func__
));
1306 MLD_PRINTF(("%s: merge inm state\n", __func__
));
1309 error
= in6m_merge(inm
, imf
);
1311 MLD_PRINTF(("%s: failed to merge inm state\n", __func__
));
1312 goto out_in6m_release
;
1315 MLD_PRINTF(("%s: doing mld downcall\n", __func__
));
1316 error
= mld_change_state(inm
, &mtp
, delay
);
1318 MLD_PRINTF(("%s: failed to update source\n", __func__
));
1320 goto out_in6m_release
;
1325 MLD_PRINTF(("%s: dropping ref on 0x%llx\n", __func__
,
1326 (uint64_t)VM_KERNEL_ADDRPERM(inm
)));
1331 *pinm
= inm
; /* keep refcount from in6_mc_get() */
1334 /* schedule timer now that we've dropped the lock(s) */
1335 mld_set_timeout(&mtp
);
1341 * Leave a multicast group; real entry point.
1342 * All source filters will be expunged.
1344 * Only preserves atomicity at inm level.
1346 * Holding the write lock for the INP which contains imf
1347 * is highly advisable. We can't assert for it as imf does not
1348 * contain a back-pointer to the owning inp.
1350 * Note: This is not the same as in6m_release(*) as this function also
1351 * makes a state change downcall into MLD.
1354 in6_mc_leave(struct in6_multi
*inm
, /*const*/ struct in6_mfilter
*imf
)
1356 struct in6_mfilter timf
;
1358 struct mld_tparams mtp
;
1360 bzero(&mtp
, sizeof(mtp
));
1363 IN6M_LOCK_ASSERT_NOTHELD(inm
);
1365 in6_multihead_lock_exclusive();
1368 MLD_PRINTF(("%s: leave inm 0x%llx, %s/%s%d, imf 0x%llx\n", __func__
,
1369 (uint64_t)VM_KERNEL_ADDRPERM(inm
), ip6_sprintf(&inm
->in6m_addr
),
1370 (in6m_is_ifp_detached(inm
) ? "null" : inm
->in6m_ifp
->if_name
),
1371 inm
->in6m_ifp
->if_unit
, (uint64_t)VM_KERNEL_ADDRPERM(imf
)));
1374 * If no imf was specified (i.e. kernel consumer),
1375 * fake one up and assume it is an ASM join.
1378 im6f_init(&timf
, MCAST_EXCLUDE
, MCAST_UNDEFINED
);
1383 * Begin state merge transaction at MLD layer.
1385 * As this particular invocation should not cause any memory
1386 * to be allocated, and there is no opportunity to roll back
1387 * the transaction, it MUST NOT fail.
1389 MLD_PRINTF(("%s: merge inm state\n", __func__
));
1391 error
= in6m_merge(inm
, imf
);
1392 KASSERT(error
== 0, ("%s: failed to merge inm state\n", __func__
));
1394 MLD_PRINTF(("%s: doing mld downcall\n", __func__
));
1395 error
= mld_change_state(inm
, &mtp
, 0);
1398 MLD_PRINTF(("%s: failed mld downcall\n", __func__
));
1401 lastref
= in6_multi_detach(inm
);
1402 VERIFY(!lastref
|| (!(inm
->in6m_debug
& IFD_ATTACHED
) &&
1403 inm
->in6m_reqcnt
== 0));
1405 in6_multihead_lock_done();
1408 IN6M_REMREF(inm
); /* for in6_multihead list */
1410 /* schedule timer now that we've dropped the lock(s) */
1411 mld_set_timeout(&mtp
);
1417 * Block or unblock an ASM multicast source on an inpcb.
1418 * This implements the delta-based API described in RFC 3678.
1420 * The delta-based API applies only to exclusive-mode memberships.
1421 * An MLD downcall will be performed.
1423 * Return 0 if successful, otherwise return an appropriate error code.
1426 in6p_block_unblock_source(struct inpcb
*inp
, struct sockopt
*sopt
)
1428 struct group_source_req gsr
;
1429 struct sockaddr_in6
*gsa
, *ssa
;
1431 struct in6_mfilter
*imf
;
1432 struct ip6_moptions
*imo
;
1433 struct in6_msource
*ims
;
1434 struct in6_multi
*inm
;
1438 struct mld_tparams mtp
;
1440 bzero(&mtp
, sizeof(mtp
));
1445 memset(&gsr
, 0, sizeof(struct group_source_req
));
1446 gsa
= (struct sockaddr_in6
*)&gsr
.gsr_group
;
1447 ssa
= (struct sockaddr_in6
*)&gsr
.gsr_source
;
1449 switch (sopt
->sopt_name
) {
1450 case MCAST_BLOCK_SOURCE
:
1451 case MCAST_UNBLOCK_SOURCE
:
1452 error
= sooptcopyin(sopt
, &gsr
,
1453 sizeof(struct group_source_req
),
1454 sizeof(struct group_source_req
));
1459 if (gsa
->sin6_family
!= AF_INET6
||
1460 gsa
->sin6_len
!= sizeof(struct sockaddr_in6
)) {
1464 if (ssa
->sin6_family
!= AF_INET6
||
1465 ssa
->sin6_len
!= sizeof(struct sockaddr_in6
)) {
1469 ifnet_head_lock_shared();
1470 if (gsr
.gsr_interface
== 0 ||
1471 (u_int
)if_index
< gsr
.gsr_interface
) {
1473 return EADDRNOTAVAIL
;
1476 ifp
= ifindex2ifnet
[gsr
.gsr_interface
];
1480 return EADDRNOTAVAIL
;
1483 if (sopt
->sopt_name
== MCAST_BLOCK_SOURCE
) {
1489 MLD_PRINTF(("%s: unknown sopt_name %d\n",
1490 __func__
, sopt
->sopt_name
));
1494 if (!IN6_IS_ADDR_MULTICAST(&gsa
->sin6_addr
)) {
1498 (void) in6_setscope(&gsa
->sin6_addr
, ifp
, NULL
);
1501 * Check if we are actually a member of this group.
1503 imo
= in6p_findmoptions(inp
);
1509 idx
= im6o_match_group(imo
, ifp
, gsa
);
1510 if (idx
== (size_t)-1 || imo
->im6o_mfilters
== NULL
) {
1511 error
= EADDRNOTAVAIL
;
1512 goto out_imo_locked
;
1515 VERIFY(imo
->im6o_mfilters
!= NULL
);
1516 imf
= &imo
->im6o_mfilters
[idx
];
1517 inm
= imo
->im6o_membership
[idx
];
1520 * Attempting to use the delta-based API on an
1521 * non exclusive-mode membership is an error.
1523 fmode
= imf
->im6f_st
[0];
1524 if (fmode
!= MCAST_EXCLUDE
) {
1526 goto out_imo_locked
;
1530 * Deal with error cases up-front:
1531 * Asked to block, but already blocked; or
1532 * Asked to unblock, but nothing to unblock.
1533 * If adding a new block entry, allocate it.
1535 ims
= im6o_match_source(imo
, idx
, ssa
);
1536 if ((ims
!= NULL
&& doblock
) || (ims
== NULL
&& !doblock
)) {
1537 MLD_PRINTF(("%s: source %s %spresent\n", __func__
,
1538 ip6_sprintf(&ssa
->sin6_addr
),
1539 doblock
? "" : "not "));
1540 error
= EADDRNOTAVAIL
;
1541 goto out_imo_locked
;
1545 * Begin state merge transaction at socket layer.
1548 MLD_PRINTF(("%s: %s source\n", __func__
, "block"));
1549 ims
= im6f_graft(imf
, fmode
, ssa
);
1554 MLD_PRINTF(("%s: %s source\n", __func__
, "allow"));
1555 error
= im6f_prune(imf
, ssa
);
1559 MLD_PRINTF(("%s: merge imf state failed\n", __func__
));
1560 goto out_im6f_rollback
;
1564 * Begin state merge transaction at MLD layer.
1567 MLD_PRINTF(("%s: merge inm state\n", __func__
));
1568 error
= in6m_merge(inm
, imf
);
1570 MLD_PRINTF(("%s: failed to merge inm state\n", __func__
));
1572 goto out_im6f_rollback
;
1575 MLD_PRINTF(("%s: doing mld downcall\n", __func__
));
1576 error
= mld_change_state(inm
, &mtp
, 0);
1580 MLD_PRINTF(("%s: failed mld downcall\n", __func__
));
1595 IM6O_REMREF(imo
); /* from in6p_findmoptions() */
1597 /* schedule timer now that we've dropped the lock(s) */
1598 mld_set_timeout(&mtp
);
1604 * Given an inpcb, return its multicast options structure pointer. Accepts
1605 * an unlocked inpcb pointer, but will return it locked. May sleep.
1608 static struct ip6_moptions
*
1609 in6p_findmoptions(struct inpcb
*inp
)
1611 struct ip6_moptions
*imo
;
1612 struct in6_multi
**immp
;
1613 struct in6_mfilter
*imfp
;
1616 if ((imo
= inp
->in6p_moptions
) != NULL
) {
1617 IM6O_ADDREF(imo
); /* for caller */
1621 imo
= ip6_allocmoptions(M_WAITOK
);
1626 immp
= _MALLOC(sizeof(*immp
) * IPV6_MIN_MEMBERSHIPS
, M_IP6MOPTS
,
1633 imfp
= _MALLOC(sizeof(struct in6_mfilter
) * IPV6_MIN_MEMBERSHIPS
,
1634 M_IN6MFILTER
, M_WAITOK
| M_ZERO
);
1636 _FREE(immp
, M_IP6MOPTS
);
1641 imo
->im6o_multicast_ifp
= NULL
;
1642 imo
->im6o_multicast_hlim
= ip6_defmcasthlim
;
1643 imo
->im6o_multicast_loop
= in6_mcast_loop
;
1644 imo
->im6o_num_memberships
= 0;
1645 imo
->im6o_max_memberships
= IPV6_MIN_MEMBERSHIPS
;
1646 imo
->im6o_membership
= immp
;
1648 /* Initialize per-group source filters. */
1649 for (idx
= 0; idx
< IPV6_MIN_MEMBERSHIPS
; idx
++) {
1650 im6f_init(&imfp
[idx
], MCAST_UNDEFINED
, MCAST_EXCLUDE
);
1653 imo
->im6o_mfilters
= imfp
;
1654 inp
->in6p_moptions
= imo
; /* keep reference from ip6_allocmoptions() */
1655 IM6O_ADDREF(imo
); /* for caller */
1661 * Atomically get source filters on a socket for an IPv6 multicast group.
1662 * Called with INP lock held; returns with lock released.
1665 in6p_get_source_filters(struct inpcb
*inp
, struct sockopt
*sopt
)
1667 struct __msfilterreq64 msfr
= {}, msfr64
;
1668 struct __msfilterreq32 msfr32
;
1669 struct sockaddr_in6
*gsa
;
1671 struct ip6_moptions
*imo
;
1672 struct in6_mfilter
*imf
;
1673 struct ip6_msource
*ims
;
1674 struct in6_msource
*lims
;
1675 struct sockaddr_in6
*psin
;
1676 struct sockaddr_storage
*ptss
;
1677 struct sockaddr_storage
*tss
;
1679 size_t idx
, nsrcs
, ncsrcs
;
1680 user_addr_t tmp_ptr
;
1682 imo
= inp
->in6p_moptions
;
1683 VERIFY(imo
!= NULL
);
1685 if (IS_64BIT_PROCESS(current_proc())) {
1686 error
= sooptcopyin(sopt
, &msfr64
,
1687 sizeof(struct __msfilterreq64
),
1688 sizeof(struct __msfilterreq64
));
1692 /* we never use msfr.msfr_srcs; */
1693 memcpy(&msfr
, &msfr64
, sizeof(msfr64
));
1695 error
= sooptcopyin(sopt
, &msfr32
,
1696 sizeof(struct __msfilterreq32
),
1697 sizeof(struct __msfilterreq32
));
1701 /* we never use msfr.msfr_srcs; */
1702 memcpy(&msfr
, &msfr32
, sizeof(msfr32
));
1705 if (msfr
.msfr_group
.ss_family
!= AF_INET6
||
1706 msfr
.msfr_group
.ss_len
!= sizeof(struct sockaddr_in6
)) {
1710 gsa
= (struct sockaddr_in6
*)&msfr
.msfr_group
;
1711 if (!IN6_IS_ADDR_MULTICAST(&gsa
->sin6_addr
)) {
1715 ifnet_head_lock_shared();
1716 if (msfr
.msfr_ifindex
== 0 || (u_int
)if_index
< msfr
.msfr_ifindex
) {
1718 return EADDRNOTAVAIL
;
1720 ifp
= ifindex2ifnet
[msfr
.msfr_ifindex
];
1724 return EADDRNOTAVAIL
;
1727 if ((size_t) msfr
.msfr_nsrcs
>
1728 UINT32_MAX
/ sizeof(struct sockaddr_storage
)) {
1729 msfr
.msfr_nsrcs
= UINT32_MAX
/ sizeof(struct sockaddr_storage
);
1732 if (msfr
.msfr_nsrcs
> in6_mcast_maxsocksrc
) {
1733 msfr
.msfr_nsrcs
= in6_mcast_maxsocksrc
;
1736 (void)in6_setscope(&gsa
->sin6_addr
, ifp
, NULL
);
1740 * Lookup group on the socket.
1742 idx
= im6o_match_group(imo
, ifp
, gsa
);
1743 if (idx
== (size_t)-1 || imo
->im6o_mfilters
== NULL
) {
1745 return EADDRNOTAVAIL
;
1747 imf
= &imo
->im6o_mfilters
[idx
];
1750 * Ignore memberships which are in limbo.
1752 if (imf
->im6f_st
[1] == MCAST_UNDEFINED
) {
1756 msfr
.msfr_fmode
= imf
->im6f_st
[1];
1759 * If the user specified a buffer, copy out the source filter
1760 * entries to userland gracefully.
1761 * We only copy out the number of entries which userland
1762 * has asked for, but we always tell userland how big the
1763 * buffer really needs to be.
1767 if (IS_64BIT_PROCESS(current_proc())) {
1768 tmp_ptr
= msfr64
.msfr_srcs
;
1770 tmp_ptr
= CAST_USER_ADDR_T(msfr32
.msfr_srcs
);
1773 if (tmp_ptr
!= USER_ADDR_NULL
&& msfr
.msfr_nsrcs
> 0) {
1774 tss
= _MALLOC((size_t) msfr
.msfr_nsrcs
* sizeof(*tss
),
1775 M_TEMP
, M_WAITOK
| M_ZERO
);
1783 * Count number of sources in-mode at t0.
1784 * If buffer space exists and remains, copy out source entries.
1786 nsrcs
= msfr
.msfr_nsrcs
;
1789 RB_FOREACH(ims
, ip6_msource_tree
, &imf
->im6f_sources
) {
1790 lims
= (struct in6_msource
*)ims
;
1791 if (lims
->im6sl_st
[0] == MCAST_UNDEFINED
||
1792 lims
->im6sl_st
[0] != imf
->im6f_st
[0]) {
1795 if (tss
!= NULL
&& nsrcs
> 0) {
1796 psin
= (struct sockaddr_in6
*)ptss
;
1797 psin
->sin6_family
= AF_INET6
;
1798 psin
->sin6_len
= sizeof(struct sockaddr_in6
);
1799 psin
->sin6_addr
= lims
->im6s_addr
;
1800 psin
->sin6_port
= 0;
1810 error
= copyout(tss
, tmp_ptr
, ncsrcs
* sizeof(*tss
));
1817 msfr
.msfr_nsrcs
= ncsrcs
;
1818 if (IS_64BIT_PROCESS(current_proc())) {
1819 msfr64
.msfr_ifindex
= msfr
.msfr_ifindex
;
1820 msfr64
.msfr_fmode
= msfr
.msfr_fmode
;
1821 msfr64
.msfr_nsrcs
= msfr
.msfr_nsrcs
;
1822 memcpy(&msfr64
.msfr_group
, &msfr
.msfr_group
,
1823 sizeof(struct sockaddr_storage
));
1824 error
= sooptcopyout(sopt
, &msfr64
,
1825 sizeof(struct __msfilterreq64
));
1827 msfr32
.msfr_ifindex
= msfr
.msfr_ifindex
;
1828 msfr32
.msfr_fmode
= msfr
.msfr_fmode
;
1829 msfr32
.msfr_nsrcs
= msfr
.msfr_nsrcs
;
1830 memcpy(&msfr32
.msfr_group
, &msfr
.msfr_group
,
1831 sizeof(struct sockaddr_storage
));
1832 error
= sooptcopyout(sopt
, &msfr32
,
1833 sizeof(struct __msfilterreq32
));
1840 * Return the IP multicast options in response to user getsockopt().
1843 ip6_getmoptions(struct inpcb
*inp
, struct sockopt
*sopt
)
1845 struct ip6_moptions
*im6o
;
1849 im6o
= inp
->in6p_moptions
;
1851 * If socket is neither of type SOCK_RAW or SOCK_DGRAM,
1852 * or is a divert socket, reject it.
1854 if (SOCK_PROTO(inp
->inp_socket
) == IPPROTO_DIVERT
||
1855 (SOCK_TYPE(inp
->inp_socket
) != SOCK_RAW
&&
1856 SOCK_TYPE(inp
->inp_socket
) != SOCK_DGRAM
)) {
1861 switch (sopt
->sopt_name
) {
1862 case IPV6_MULTICAST_IF
:
1866 if (im6o
== NULL
|| im6o
->im6o_multicast_ifp
== NULL
) {
1869 optval
= im6o
->im6o_multicast_ifp
->if_index
;
1874 error
= sooptcopyout(sopt
, &optval
, sizeof(u_int
));
1877 case IPV6_MULTICAST_HOPS
:
1879 optval
= ip6_defmcasthlim
;
1882 optval
= im6o
->im6o_multicast_hlim
;
1885 error
= sooptcopyout(sopt
, &optval
, sizeof(u_int
));
1888 case IPV6_MULTICAST_LOOP
:
1890 optval
= in6_mcast_loop
; /* XXX VIMAGE */
1893 optval
= im6o
->im6o_multicast_loop
;
1896 error
= sooptcopyout(sopt
, &optval
, sizeof(u_int
));
1901 error
= EADDRNOTAVAIL
;
1903 error
= in6p_get_source_filters(inp
, sopt
);
1908 error
= ENOPROTOOPT
;
1916 * Look up the ifnet to use for a multicast group membership,
1917 * given the address of an IPv6 group.
1919 * This routine exists to support legacy IPv6 multicast applications.
1921 * If inp is non-NULL and is bound to an interface, use this socket's
1922 * inp_boundif for any required routing table lookup.
1924 * If the route lookup fails, return NULL.
1926 * FUTURE: Support multiple forwarding tables for IPv6.
1928 * Returns NULL if no ifp could be found.
1930 static struct ifnet
*
1931 in6p_lookup_mcast_ifp(const struct inpcb
*in6p
,
1932 const struct sockaddr_in6
*gsin6
)
1934 struct route_in6 ro6
;
1936 unsigned int ifscope
= IFSCOPE_NONE
;
1938 VERIFY(in6p
== NULL
|| (in6p
->inp_vflag
& INP_IPV6
));
1939 VERIFY(gsin6
->sin6_family
== AF_INET6
);
1940 if (IN6_IS_ADDR_MULTICAST(&gsin6
->sin6_addr
) == 0) {
1944 if (in6p
!= NULL
&& (in6p
->inp_flags
& INP_BOUND_IF
)) {
1945 ifscope
= in6p
->inp_boundifp
->if_index
;
1949 memset(&ro6
, 0, sizeof(struct route_in6
));
1950 memcpy(&ro6
.ro_dst
, gsin6
, sizeof(struct sockaddr_in6
));
1951 rtalloc_scoped_ign((struct route
*)&ro6
, 0, ifscope
);
1952 if (ro6
.ro_rt
!= NULL
) {
1953 ifp
= ro6
.ro_rt
->rt_ifp
;
1954 VERIFY(ifp
!= NULL
);
1956 ROUTE_RELEASE(&ro6
);
1962 * Since ipv6_mreq contains an ifindex and ip_mreq contains an AF_INET
1963 * address, we need to lookup the AF_INET address when translating an
1964 * ipv6_mreq structure into an ipmreq structure.
1965 * This is used when userland performs multicast setsockopt() on AF_INET6
1966 * sockets with AF_INET multicast addresses (IPv6 v4 mapped addresses).
1969 in6p_lookup_v4addr(struct ipv6_mreq
*mreq
, struct ip_mreq
*v4mreq
)
1973 struct sockaddr_in
*sin
;
1975 ifnet_head_lock_shared();
1976 if (mreq
->ipv6mr_interface
> (unsigned int)if_index
) {
1978 return EADDRNOTAVAIL
;
1980 ifp
= ifindex2ifnet
[mreq
->ipv6mr_interface
];
1984 return EADDRNOTAVAIL
;
1986 ifa
= ifa_ifpgetprimary(ifp
, AF_INET
);
1988 return EADDRNOTAVAIL
;
1990 sin
= (struct sockaddr_in
*)(uintptr_t)(size_t)ifa
->ifa_addr
;
1991 v4mreq
->imr_interface
.s_addr
= sin
->sin_addr
.s_addr
;
1998 * Join an IPv6 multicast group, possibly with a source.
2000 * FIXME: The KAME use of the unspecified address (::)
2001 * to join *all* multicast groups is currently unsupported.
2004 in6p_join_group(struct inpcb
*inp
, struct sockopt
*sopt
)
2006 struct group_source_req gsr
;
2007 struct sockaddr_in6
*gsa
, *ssa
;
2009 struct in6_mfilter
*imf
;
2010 struct ip6_moptions
*imo
;
2011 struct in6_multi
*inm
= NULL
;
2012 struct in6_msource
*lims
= NULL
;
2015 uint32_t scopeid
= 0;
2016 struct mld_tparams mtp
;
2018 bzero(&mtp
, sizeof(mtp
));
2024 memset(&gsr
, 0, sizeof(struct group_source_req
));
2025 gsa
= (struct sockaddr_in6
*)&gsr
.gsr_group
;
2026 ssa
= (struct sockaddr_in6
*)&gsr
.gsr_source
;
2029 * Chew everything into struct group_source_req.
2030 * Overwrite the port field if present, as the sockaddr
2031 * being copied in may be matched with a binary comparison.
2032 * Ignore passed-in scope ID.
2034 switch (sopt
->sopt_name
) {
2035 case IPV6_JOIN_GROUP
: {
2036 struct ipv6_mreq mreq
;
2038 error
= sooptcopyin(sopt
, &mreq
, sizeof(struct ipv6_mreq
),
2039 sizeof(struct ipv6_mreq
));
2043 if (IN6_IS_ADDR_V4MAPPED(&mreq
.ipv6mr_multiaddr
)) {
2044 struct ip_mreq v4mreq
;
2045 struct sockopt v4sopt
;
2047 v4mreq
.imr_multiaddr
.s_addr
=
2048 mreq
.ipv6mr_multiaddr
.s6_addr32
[3];
2049 if (mreq
.ipv6mr_interface
== 0) {
2050 v4mreq
.imr_interface
.s_addr
= INADDR_ANY
;
2052 error
= in6p_lookup_v4addr(&mreq
, &v4mreq
);
2057 v4sopt
.sopt_dir
= SOPT_SET
;
2058 v4sopt
.sopt_level
= sopt
->sopt_level
;
2059 v4sopt
.sopt_name
= IP_ADD_MEMBERSHIP
;
2060 v4sopt
.sopt_val
= CAST_USER_ADDR_T(&v4mreq
);
2061 v4sopt
.sopt_valsize
= sizeof(v4mreq
);
2062 v4sopt
.sopt_p
= kernproc
;
2064 return inp_join_group(inp
, &v4sopt
);
2066 gsa
->sin6_family
= AF_INET6
;
2067 gsa
->sin6_len
= sizeof(struct sockaddr_in6
);
2068 gsa
->sin6_addr
= mreq
.ipv6mr_multiaddr
;
2070 /* Only allow IPv6 multicast addresses */
2071 if (IN6_IS_ADDR_MULTICAST(&gsa
->sin6_addr
) == 0) {
2075 if (mreq
.ipv6mr_interface
== 0) {
2076 ifp
= in6p_lookup_mcast_ifp(inp
, gsa
);
2078 ifnet_head_lock_shared();
2079 if ((u_int
)if_index
< mreq
.ipv6mr_interface
) {
2081 return EADDRNOTAVAIL
;
2083 ifp
= ifindex2ifnet
[mreq
.ipv6mr_interface
];
2086 MLD_PRINTF(("%s: ipv6mr_interface = %d, ifp = 0x%llx\n",
2087 __func__
, mreq
.ipv6mr_interface
,
2088 (uint64_t)VM_KERNEL_ADDRPERM(ifp
)));
2092 case MCAST_JOIN_GROUP
:
2093 case MCAST_JOIN_SOURCE_GROUP
:
2094 if (sopt
->sopt_name
== MCAST_JOIN_GROUP
) {
2095 error
= sooptcopyin(sopt
, &gsr
,
2096 sizeof(struct group_req
),
2097 sizeof(struct group_req
));
2098 } else if (sopt
->sopt_name
== MCAST_JOIN_SOURCE_GROUP
) {
2099 error
= sooptcopyin(sopt
, &gsr
,
2100 sizeof(struct group_source_req
),
2101 sizeof(struct group_source_req
));
2107 if (gsa
->sin6_family
!= AF_INET6
||
2108 gsa
->sin6_len
!= sizeof(struct sockaddr_in6
)) {
2112 if (sopt
->sopt_name
== MCAST_JOIN_SOURCE_GROUP
) {
2113 if (ssa
->sin6_family
!= AF_INET6
||
2114 ssa
->sin6_len
!= sizeof(struct sockaddr_in6
)) {
2117 if (IN6_IS_ADDR_MULTICAST(&ssa
->sin6_addr
)) {
2121 * TODO: Validate embedded scope ID in source
2122 * list entry against passed-in ifp, if and only
2123 * if source list filter entry is iface or node local.
2125 in6_clearscope(&ssa
->sin6_addr
);
2127 ssa
->sin6_scope_id
= 0;
2130 ifnet_head_lock_shared();
2131 if (gsr
.gsr_interface
== 0 ||
2132 (u_int
)if_index
< gsr
.gsr_interface
) {
2134 return EADDRNOTAVAIL
;
2136 ifp
= ifindex2ifnet
[gsr
.gsr_interface
];
2141 MLD_PRINTF(("%s: unknown sopt_name %d\n",
2142 __func__
, sopt
->sopt_name
));
2146 if (!IN6_IS_ADDR_MULTICAST(&gsa
->sin6_addr
)) {
2150 if (ifp
== NULL
|| (ifp
->if_flags
& IFF_MULTICAST
) == 0) {
2151 return EADDRNOTAVAIL
;
2154 INC_ATOMIC_INT64_LIM(net_api_stats
.nas_socket_mcast_join_total
);
2156 * TBD: revisit the criteria for non-OS initiated joins
2158 if (inp
->inp_lport
== htons(5353)) {
2159 INC_ATOMIC_INT64_LIM(net_api_stats
.nas_socket_mcast_join_os_total
);
2163 gsa
->sin6_scope_id
= 0;
2166 * Always set the scope zone ID on memberships created from userland.
2167 * Use the passed-in ifp to do this.
2169 (void)in6_setscope(&gsa
->sin6_addr
, ifp
, &scopeid
);
2171 * Some addresses are not valid without an embedded scopeid.
2172 * This check must be present because otherwise we will later hit
2173 * a VERIFY() in in6_mc_join().
2175 if ((IN6_IS_ADDR_MC_LINKLOCAL(&gsa
->sin6_addr
) ||
2176 IN6_IS_ADDR_MC_INTFACELOCAL(&gsa
->sin6_addr
)) &&
2177 (scopeid
== 0 || gsa
->sin6_addr
.s6_addr16
[1] == 0)) {
2181 imo
= in6p_findmoptions(inp
);
2187 idx
= im6o_match_group(imo
, ifp
, gsa
);
2188 if (idx
== (size_t)-1) {
2191 inm
= imo
->im6o_membership
[idx
];
2192 imf
= &imo
->im6o_mfilters
[idx
];
2193 if (ssa
->sin6_family
!= AF_UNSPEC
) {
2195 * MCAST_JOIN_SOURCE_GROUP on an exclusive membership
2196 * is an error. On an existing inclusive membership,
2197 * it just adds the source to the filter list.
2199 if (imf
->im6f_st
[1] != MCAST_INCLUDE
) {
2201 goto out_imo_locked
;
2204 * Throw out duplicates.
2206 * XXX FIXME: This makes a naive assumption that
2207 * even if entries exist for *ssa in this imf,
2208 * they will be rejected as dupes, even if they
2209 * are not valid in the current mode (in-mode).
2211 * in6_msource is transactioned just as for anything
2212 * else in SSM -- but note naive use of in6m_graft()
2213 * below for allocating new filter entries.
2215 * This is only an issue if someone mixes the
2216 * full-state SSM API with the delta-based API,
2217 * which is discouraged in the relevant RFCs.
2219 lims
= im6o_match_source(imo
, idx
, ssa
);
2220 if (lims
!= NULL
/*&&
2221 * lims->im6sl_st[1] == MCAST_INCLUDE*/) {
2222 error
= EADDRNOTAVAIL
;
2223 goto out_imo_locked
;
2227 * MCAST_JOIN_GROUP on an existing exclusive
2228 * membership is an error; return EADDRINUSE
2229 * to preserve 4.4BSD API idempotence, and
2230 * avoid tedious detour to code below.
2231 * NOTE: This is bending RFC 3678 a bit.
2233 * On an existing inclusive membership, this is also
2234 * an error; if you want to change filter mode,
2235 * you must use the userland API setsourcefilter().
2236 * XXX We don't reject this for imf in UNDEFINED
2237 * state at t1, because allocation of a filter
2238 * is atomic with allocation of a membership.
2241 /* See comments above for EADDRINUSE */
2242 if (imf
->im6f_st
[1] == MCAST_EXCLUDE
) {
2245 goto out_imo_locked
;
2250 * Begin state merge transaction at socket layer.
2254 if (imo
->im6o_num_memberships
== imo
->im6o_max_memberships
) {
2255 error
= im6o_grow(imo
, 0);
2257 goto out_imo_locked
;
2261 * Allocate the new slot upfront so we can deal with
2262 * grafting the new source filter in same code path
2263 * as for join-source on existing membership.
2265 idx
= imo
->im6o_num_memberships
;
2266 imo
->im6o_membership
[idx
] = NULL
;
2267 imo
->im6o_num_memberships
++;
2268 VERIFY(imo
->im6o_mfilters
!= NULL
);
2269 imf
= &imo
->im6o_mfilters
[idx
];
2270 VERIFY(RB_EMPTY(&imf
->im6f_sources
));
2274 * Graft new source into filter list for this inpcb's
2275 * membership of the group. The in6_multi may not have
2276 * been allocated yet if this is a new membership, however,
2277 * the in_mfilter slot will be allocated and must be initialized.
2279 * Note: Grafting of exclusive mode filters doesn't happen
2281 * XXX: Should check for non-NULL lims (node exists but may
2282 * not be in-mode) for interop with full-state API.
2284 if (ssa
->sin6_family
!= AF_UNSPEC
) {
2285 /* Membership starts in IN mode */
2287 MLD_PRINTF(("%s: new join w/source\n", __func__
);
2288 im6f_init(imf
, MCAST_UNDEFINED
, MCAST_INCLUDE
));
2290 MLD_PRINTF(("%s: %s source\n", __func__
, "allow"));
2292 lims
= im6f_graft(imf
, MCAST_INCLUDE
, ssa
);
2294 MLD_PRINTF(("%s: merge imf state failed\n",
2300 /* No address specified; Membership starts in EX mode */
2302 MLD_PRINTF(("%s: new join w/o source", __func__
));
2303 im6f_init(imf
, MCAST_UNDEFINED
, MCAST_EXCLUDE
);
2308 * Begin state merge transaction at MLD layer.
2313 * See inp_join_group() for why we need to unlock
2315 IM6O_ADDREF_LOCKED(imo
);
2317 socket_unlock(inp
->inp_socket
, 0);
2319 VERIFY(inm
== NULL
);
2320 error
= in6_mc_join(ifp
, &gsa
->sin6_addr
, imf
, &inm
, 0);
2321 VERIFY(inm
!= NULL
|| error
!= 0);
2323 socket_lock(inp
->inp_socket
, 0);
2330 imo
->im6o_membership
[idx
] = inm
; /* from in6_mc_join() */
2332 MLD_PRINTF(("%s: merge inm state\n", __func__
));
2334 error
= in6m_merge(inm
, imf
);
2336 MLD_PRINTF(("%s: failed to merge inm state\n",
2339 goto out_im6f_rollback
;
2341 MLD_PRINTF(("%s: doing mld downcall\n", __func__
));
2342 error
= mld_change_state(inm
, &mtp
, 0);
2345 MLD_PRINTF(("%s: failed mld downcall\n",
2347 goto out_im6f_rollback
;
2364 if (error
&& is_new
) {
2365 VERIFY(inm
== NULL
);
2366 imo
->im6o_membership
[idx
] = NULL
;
2367 --imo
->im6o_num_memberships
;
2372 IM6O_REMREF(imo
); /* from in6p_findmoptions() */
2374 /* schedule timer now that we've dropped the lock(s) */
2375 mld_set_timeout(&mtp
);
2381 * Leave an IPv6 multicast group on an inpcb, possibly with a source.
2384 in6p_leave_group(struct inpcb
*inp
, struct sockopt
*sopt
)
2386 struct ipv6_mreq mreq
;
2387 struct group_source_req gsr
;
2388 struct sockaddr_in6
*gsa
, *ssa
;
2390 struct in6_mfilter
*imf
;
2391 struct ip6_moptions
*imo
;
2392 struct in6_msource
*ims
;
2393 struct in6_multi
*inm
= NULL
;
2394 uint32_t ifindex
= 0;
2396 int error
, is_final
;
2397 struct mld_tparams mtp
;
2399 bzero(&mtp
, sizeof(mtp
));
2404 memset(&gsr
, 0, sizeof(struct group_source_req
));
2405 gsa
= (struct sockaddr_in6
*)&gsr
.gsr_group
;
2406 ssa
= (struct sockaddr_in6
*)&gsr
.gsr_source
;
2409 * Chew everything passed in up into a struct group_source_req
2410 * as that is easier to process.
2411 * Note: Any embedded scope ID in the multicast group passed
2412 * in by userland is ignored, the interface index is the recommended
2413 * mechanism to specify an interface; see below.
2415 switch (sopt
->sopt_name
) {
2416 case IPV6_LEAVE_GROUP
: {
2417 error
= sooptcopyin(sopt
, &mreq
, sizeof(struct ipv6_mreq
),
2418 sizeof(struct ipv6_mreq
));
2422 if (IN6_IS_ADDR_V4MAPPED(&mreq
.ipv6mr_multiaddr
)) {
2423 struct ip_mreq v4mreq
;
2424 struct sockopt v4sopt
;
2426 v4mreq
.imr_multiaddr
.s_addr
=
2427 mreq
.ipv6mr_multiaddr
.s6_addr32
[3];
2428 if (mreq
.ipv6mr_interface
== 0) {
2429 v4mreq
.imr_interface
.s_addr
= INADDR_ANY
;
2431 error
= in6p_lookup_v4addr(&mreq
, &v4mreq
);
2436 v4sopt
.sopt_dir
= SOPT_SET
;
2437 v4sopt
.sopt_level
= sopt
->sopt_level
;
2438 v4sopt
.sopt_name
= IP_DROP_MEMBERSHIP
;
2439 v4sopt
.sopt_val
= CAST_USER_ADDR_T(&v4mreq
);
2440 v4sopt
.sopt_valsize
= sizeof(v4mreq
);
2441 v4sopt
.sopt_p
= kernproc
;
2443 return inp_leave_group(inp
, &v4sopt
);
2445 gsa
->sin6_family
= AF_INET6
;
2446 gsa
->sin6_len
= sizeof(struct sockaddr_in6
);
2447 gsa
->sin6_addr
= mreq
.ipv6mr_multiaddr
;
2449 gsa
->sin6_scope_id
= 0;
2450 ifindex
= mreq
.ipv6mr_interface
;
2451 /* Only allow IPv6 multicast addresses */
2452 if (IN6_IS_ADDR_MULTICAST(&gsa
->sin6_addr
) == 0) {
2458 case MCAST_LEAVE_GROUP
:
2459 case MCAST_LEAVE_SOURCE_GROUP
:
2460 if (sopt
->sopt_name
== MCAST_LEAVE_GROUP
) {
2461 error
= sooptcopyin(sopt
, &gsr
,
2462 sizeof(struct group_req
),
2463 sizeof(struct group_req
));
2464 } else if (sopt
->sopt_name
== MCAST_LEAVE_SOURCE_GROUP
) {
2465 error
= sooptcopyin(sopt
, &gsr
,
2466 sizeof(struct group_source_req
),
2467 sizeof(struct group_source_req
));
2473 if (gsa
->sin6_family
!= AF_INET6
||
2474 gsa
->sin6_len
!= sizeof(struct sockaddr_in6
)) {
2477 if (sopt
->sopt_name
== MCAST_LEAVE_SOURCE_GROUP
) {
2478 if (ssa
->sin6_family
!= AF_INET6
||
2479 ssa
->sin6_len
!= sizeof(struct sockaddr_in6
)) {
2482 if (IN6_IS_ADDR_MULTICAST(&ssa
->sin6_addr
)) {
2486 * TODO: Validate embedded scope ID in source
2487 * list entry against passed-in ifp, if and only
2488 * if source list filter entry is iface or node local.
2490 in6_clearscope(&ssa
->sin6_addr
);
2493 gsa
->sin6_scope_id
= 0;
2494 ifindex
= gsr
.gsr_interface
;
2498 MLD_PRINTF(("%s: unknown sopt_name %d\n",
2499 __func__
, sopt
->sopt_name
));
2503 if (!IN6_IS_ADDR_MULTICAST(&gsa
->sin6_addr
)) {
2508 * Validate interface index if provided. If no interface index
2509 * was provided separately, attempt to look the membership up
2510 * from the default scope as a last resort to disambiguate
2511 * the membership we are being asked to leave.
2512 * XXX SCOPE6 lock potentially taken here.
2515 ifnet_head_lock_shared();
2516 if ((u_int
)if_index
< ifindex
) {
2518 return EADDRNOTAVAIL
;
2520 ifp
= ifindex2ifnet
[ifindex
];
2523 return EADDRNOTAVAIL
;
2525 (void) in6_setscope(&gsa
->sin6_addr
, ifp
, NULL
);
2527 error
= sa6_embedscope(gsa
, ip6_use_defzone
);
2529 return EADDRNOTAVAIL
;
2532 * Some badly behaved applications don't pass an ifindex
2533 * or a scope ID, which is an API violation. In this case,
2534 * perform a lookup as per a v6 join.
2536 * XXX For now, stomp on zone ID for the corner case.
2537 * This is not the 'KAME way', but we need to see the ifp
2538 * directly until such time as this implementation is
2539 * refactored, assuming the scope IDs are the way to go.
2541 ifindex
= ntohs(gsa
->sin6_addr
.s6_addr16
[1]);
2543 MLD_PRINTF(("%s: warning: no ifindex, looking up "
2544 "ifp for group %s.\n", __func__
,
2545 ip6_sprintf(&gsa
->sin6_addr
)));
2546 ifp
= in6p_lookup_mcast_ifp(inp
, gsa
);
2548 if (!IF_INDEX_IN_RANGE(ifindex
)) {
2549 return EADDRNOTAVAIL
;
2551 ifnet_head_lock_shared();
2552 ifp
= ifindex2ifnet
[ifindex
];
2556 return EADDRNOTAVAIL
;
2560 VERIFY(ifp
!= NULL
);
2561 MLD_PRINTF(("%s: ifp = 0x%llx\n", __func__
,
2562 (uint64_t)VM_KERNEL_ADDRPERM(ifp
)));
2565 * Find the membership in the membership array.
2567 imo
= in6p_findmoptions(inp
);
2573 idx
= im6o_match_group(imo
, ifp
, gsa
);
2574 if (idx
== (size_t)-1) {
2575 error
= EADDRNOTAVAIL
;
2578 inm
= imo
->im6o_membership
[idx
];
2579 imf
= &imo
->im6o_mfilters
[idx
];
2581 if (ssa
->sin6_family
!= AF_UNSPEC
) {
2586 * Begin state merge transaction at socket layer.
2590 * If we were instructed only to leave a given source, do so.
2591 * MCAST_LEAVE_SOURCE_GROUP is only valid for inclusive memberships.
2596 if (imf
->im6f_st
[0] == MCAST_EXCLUDE
) {
2597 error
= EADDRNOTAVAIL
;
2600 ims
= im6o_match_source(imo
, idx
, ssa
);
2602 MLD_PRINTF(("%s: source %s %spresent\n", __func__
,
2603 ip6_sprintf(&ssa
->sin6_addr
),
2605 error
= EADDRNOTAVAIL
;
2608 MLD_PRINTF(("%s: %s source\n", __func__
, "block"));
2609 error
= im6f_prune(imf
, ssa
);
2611 MLD_PRINTF(("%s: merge imf state failed\n",
2618 * Begin state merge transaction at MLD layer.
2623 * Give up the multicast address record to which
2624 * the membership points. Reference held in im6o
2625 * will be released below.
2627 (void) in6_mc_leave(inm
, imf
);
2629 MLD_PRINTF(("%s: merge inm state\n", __func__
));
2631 error
= in6m_merge(inm
, imf
);
2633 MLD_PRINTF(("%s: failed to merge inm state\n",
2636 goto out_im6f_rollback
;
2639 MLD_PRINTF(("%s: doing mld downcall\n", __func__
));
2640 error
= mld_change_state(inm
, &mtp
, 0);
2642 MLD_PRINTF(("%s: failed mld downcall\n", __func__
));
2657 /* Remove the gap in the membership array. */
2658 VERIFY(inm
== imo
->im6o_membership
[idx
]);
2659 imo
->im6o_membership
[idx
] = NULL
;
2662 * See inp_join_group() for why we need to unlock
2664 IM6O_ADDREF_LOCKED(imo
);
2666 socket_unlock(inp
->inp_socket
, 0);
2670 socket_lock(inp
->inp_socket
, 0);
2674 for (++idx
; idx
< imo
->im6o_num_memberships
; ++idx
) {
2675 imo
->im6o_membership
[idx
- 1] = imo
->im6o_membership
[idx
];
2676 imo
->im6o_mfilters
[idx
- 1] = imo
->im6o_mfilters
[idx
];
2678 imo
->im6o_num_memberships
--;
2683 IM6O_REMREF(imo
); /* from in6p_findmoptions() */
2685 /* schedule timer now that we've dropped the lock(s) */
2686 mld_set_timeout(&mtp
);
2692 * Select the interface for transmitting IPv6 multicast datagrams.
2694 * Either an instance of struct in6_addr or an instance of struct ipv6_mreqn
2695 * may be passed to this socket option. An address of in6addr_any or an
2696 * interface index of 0 is used to remove a previous selection.
2697 * When no interface is selected, one is chosen for every send.
2700 in6p_set_multicast_if(struct inpcb
*inp
, struct sockopt
*sopt
)
2703 struct ip6_moptions
*imo
;
2707 if (sopt
->sopt_valsize
!= sizeof(u_int
)) {
2711 error
= sooptcopyin(sopt
, &ifindex
, sizeof(u_int
), sizeof(u_int
));
2716 ifnet_head_lock_shared();
2717 if ((u_int
)if_index
< ifindex
) {
2722 ifp
= ifindex2ifnet
[ifindex
];
2724 if (ifp
== NULL
|| (ifp
->if_flags
& IFF_MULTICAST
) == 0) {
2725 return EADDRNOTAVAIL
;
2728 imo
= in6p_findmoptions(inp
);
2734 imo
->im6o_multicast_ifp
= ifp
;
2736 IM6O_REMREF(imo
); /* from in6p_findmoptions() */
2742 * Atomically set source filters on a socket for an IPv6 multicast group.
2746 in6p_set_source_filters(struct inpcb
*inp
, struct sockopt
*sopt
)
2748 struct __msfilterreq64 msfr
= {}, msfr64
;
2749 struct __msfilterreq32 msfr32
;
2750 struct sockaddr_in6
*gsa
;
2752 struct in6_mfilter
*imf
;
2753 struct ip6_moptions
*imo
;
2754 struct in6_multi
*inm
;
2757 user_addr_t tmp_ptr
;
2758 struct mld_tparams mtp
;
2760 bzero(&mtp
, sizeof(mtp
));
2762 if (IS_64BIT_PROCESS(current_proc())) {
2763 error
= sooptcopyin(sopt
, &msfr64
,
2764 sizeof(struct __msfilterreq64
),
2765 sizeof(struct __msfilterreq64
));
2769 /* we never use msfr.msfr_srcs; */
2770 memcpy(&msfr
, &msfr64
, sizeof(msfr64
));
2772 error
= sooptcopyin(sopt
, &msfr32
,
2773 sizeof(struct __msfilterreq32
),
2774 sizeof(struct __msfilterreq32
));
2778 /* we never use msfr.msfr_srcs; */
2779 memcpy(&msfr
, &msfr32
, sizeof(msfr32
));
2782 if ((size_t) msfr
.msfr_nsrcs
>
2783 UINT32_MAX
/ sizeof(struct sockaddr_storage
)) {
2784 msfr
.msfr_nsrcs
= UINT32_MAX
/ sizeof(struct sockaddr_storage
);
2787 if (msfr
.msfr_nsrcs
> in6_mcast_maxsocksrc
) {
2791 if (msfr
.msfr_fmode
!= MCAST_EXCLUDE
&&
2792 msfr
.msfr_fmode
!= MCAST_INCLUDE
) {
2796 if (msfr
.msfr_group
.ss_family
!= AF_INET6
||
2797 msfr
.msfr_group
.ss_len
!= sizeof(struct sockaddr_in6
)) {
2801 gsa
= (struct sockaddr_in6
*)&msfr
.msfr_group
;
2802 if (!IN6_IS_ADDR_MULTICAST(&gsa
->sin6_addr
)) {
2806 gsa
->sin6_port
= 0; /* ignore port */
2808 ifnet_head_lock_shared();
2809 if (msfr
.msfr_ifindex
== 0 || (u_int
)if_index
< msfr
.msfr_ifindex
) {
2811 return EADDRNOTAVAIL
;
2813 ifp
= ifindex2ifnet
[msfr
.msfr_ifindex
];
2816 return EADDRNOTAVAIL
;
2819 (void)in6_setscope(&gsa
->sin6_addr
, ifp
, NULL
);
2822 * Take the INP write lock.
2823 * Check if this socket is a member of this group.
2825 imo
= in6p_findmoptions(inp
);
2831 idx
= im6o_match_group(imo
, ifp
, gsa
);
2832 if (idx
== (size_t)-1 || imo
->im6o_mfilters
== NULL
) {
2833 error
= EADDRNOTAVAIL
;
2834 goto out_imo_locked
;
2836 inm
= imo
->im6o_membership
[idx
];
2837 imf
= &imo
->im6o_mfilters
[idx
];
2840 * Begin state merge transaction at socket layer.
2843 imf
->im6f_st
[1] = msfr
.msfr_fmode
;
2846 * Apply any new source filters, if present.
2847 * Make a copy of the user-space source vector so
2848 * that we may copy them with a single copyin. This
2849 * allows us to deal with page faults up-front.
2851 if (msfr
.msfr_nsrcs
> 0) {
2852 struct in6_msource
*lims
;
2853 struct sockaddr_in6
*psin
;
2854 struct sockaddr_storage
*kss
, *pkss
;
2857 if (IS_64BIT_PROCESS(current_proc())) {
2858 tmp_ptr
= msfr64
.msfr_srcs
;
2860 tmp_ptr
= CAST_USER_ADDR_T(msfr32
.msfr_srcs
);
2863 MLD_PRINTF(("%s: loading %lu source list entries\n",
2864 __func__
, (unsigned long)msfr
.msfr_nsrcs
));
2865 kss
= _MALLOC((size_t) msfr
.msfr_nsrcs
* sizeof(*kss
),
2869 goto out_imo_locked
;
2872 error
= copyin(tmp_ptr
, kss
,
2873 (size_t) msfr
.msfr_nsrcs
* sizeof(*kss
));
2876 goto out_imo_locked
;
2880 * Mark all source filters as UNDEFINED at t1.
2881 * Restore new group filter mode, as im6f_leave()
2882 * will set it to INCLUDE.
2885 imf
->im6f_st
[1] = msfr
.msfr_fmode
;
2888 * Update socket layer filters at t1, lazy-allocating
2889 * new entries. This saves a bunch of memory at the
2890 * cost of one RB_FIND() per source entry; duplicate
2891 * entries in the msfr_nsrcs vector are ignored.
2892 * If we encounter an error, rollback transaction.
2894 * XXX This too could be replaced with a set-symmetric
2895 * difference like loop to avoid walking from root
2896 * every time, as the key space is common.
2898 for (i
= 0, pkss
= kss
; i
< msfr
.msfr_nsrcs
; i
++, pkss
++) {
2899 psin
= (struct sockaddr_in6
*)pkss
;
2900 if (psin
->sin6_family
!= AF_INET6
) {
2901 error
= EAFNOSUPPORT
;
2904 if (psin
->sin6_len
!= sizeof(struct sockaddr_in6
)) {
2908 if (IN6_IS_ADDR_MULTICAST(&psin
->sin6_addr
)) {
2913 * TODO: Validate embedded scope ID in source
2914 * list entry against passed-in ifp, if and only
2915 * if source list filter entry is iface or node local.
2917 in6_clearscope(&psin
->sin6_addr
);
2918 error
= im6f_get_source(imf
, psin
, &lims
);
2922 lims
->im6sl_st
[1] = imf
->im6f_st
[1];
2928 goto out_im6f_rollback
;
2932 * Begin state merge transaction at MLD layer.
2935 MLD_PRINTF(("%s: merge inm state\n", __func__
));
2936 error
= in6m_merge(inm
, imf
);
2938 MLD_PRINTF(("%s: failed to merge inm state\n", __func__
));
2940 goto out_im6f_rollback
;
2943 MLD_PRINTF(("%s: doing mld downcall\n", __func__
));
2944 error
= mld_change_state(inm
, &mtp
, 0);
2948 MLD_PRINTF(("%s: failed mld downcall\n", __func__
));
2963 IM6O_REMREF(imo
); /* from in6p_findmoptions() */
2965 /* schedule timer now that we've dropped the lock(s) */
2966 mld_set_timeout(&mtp
);
2972 * Set the IP multicast options in response to user setsockopt().
2974 * Many of the socket options handled in this function duplicate the
2975 * functionality of socket options in the regular unicast API. However,
2976 * it is not possible to merge the duplicate code, because the idempotence
2977 * of the IPv6 multicast part of the BSD Sockets API must be preserved;
2978 * the effects of these options must be treated as separate and distinct.
2982 ip6_setmoptions(struct inpcb
*inp
, struct sockopt
*sopt
)
2984 struct ip6_moptions
*im6o
;
2990 * If socket is neither of type SOCK_RAW or SOCK_DGRAM,
2991 * or is a divert socket, reject it.
2993 if (SOCK_PROTO(inp
->inp_socket
) == IPPROTO_DIVERT
||
2994 (SOCK_TYPE(inp
->inp_socket
) != SOCK_RAW
&&
2995 SOCK_TYPE(inp
->inp_socket
) != SOCK_DGRAM
)) {
2999 switch (sopt
->sopt_name
) {
3000 case IPV6_MULTICAST_IF
:
3001 error
= in6p_set_multicast_if(inp
, sopt
);
3004 case IPV6_MULTICAST_HOPS
: {
3007 if (sopt
->sopt_valsize
!= sizeof(int)) {
3011 error
= sooptcopyin(sopt
, &hlim
, sizeof(hlim
), sizeof(int));
3015 if (hlim
< -1 || hlim
> 255) {
3018 } else if (hlim
== -1) {
3019 hlim
= ip6_defmcasthlim
;
3021 im6o
= in6p_findmoptions(inp
);
3027 im6o
->im6o_multicast_hlim
= hlim
;
3029 IM6O_REMREF(im6o
); /* from in6p_findmoptions() */
3033 case IPV6_MULTICAST_LOOP
: {
3037 * Set the loopback flag for outgoing multicast packets.
3038 * Must be zero or one.
3040 if (sopt
->sopt_valsize
!= sizeof(u_int
)) {
3044 error
= sooptcopyin(sopt
, &loop
, sizeof(u_int
), sizeof(u_int
));
3052 im6o
= in6p_findmoptions(inp
);
3058 im6o
->im6o_multicast_loop
= loop
;
3060 IM6O_REMREF(im6o
); /* from in6p_findmoptions() */
3064 case IPV6_JOIN_GROUP
:
3065 case MCAST_JOIN_GROUP
:
3066 case MCAST_JOIN_SOURCE_GROUP
:
3067 error
= in6p_join_group(inp
, sopt
);
3070 case IPV6_LEAVE_GROUP
:
3071 case MCAST_LEAVE_GROUP
:
3072 case MCAST_LEAVE_SOURCE_GROUP
:
3073 error
= in6p_leave_group(inp
, sopt
);
3076 case MCAST_BLOCK_SOURCE
:
3077 case MCAST_UNBLOCK_SOURCE
:
3078 error
= in6p_block_unblock_source(inp
, sopt
);
3082 error
= in6p_set_source_filters(inp
, sopt
);
3093 * Expose MLD's multicast filter mode and source list(s) to userland,
3094 * keyed by (ifindex, group).
3095 * The filter mode is written out as a uint32_t, followed by
3096 * 0..n of struct in6_addr.
3097 * For use by ifmcstat(8).
3100 sysctl_ip6_mcast_filters SYSCTL_HANDLER_ARGS
3102 #pragma unused(oidp)
3104 struct in6_addr mcaddr
;
3105 struct in6_addr src
;
3107 struct in6_multi
*inm
;
3108 struct in6_multistep step
;
3109 struct ip6_msource
*ims
;
3113 uint32_t fmode
, ifindex
;
3118 if (req
->newptr
!= USER_ADDR_NULL
) {
3122 /* int: ifindex + 4 * 32 bits of IPv6 address */
3128 ifnet_head_lock_shared();
3129 if (ifindex
<= 0 || ifindex
> (u_int
)if_index
) {
3130 MLD_PRINTF(("%s: ifindex %u out of range\n",
3131 __func__
, ifindex
));
3136 memcpy(&mcaddr
, &name
[1], sizeof(struct in6_addr
));
3137 if (!IN6_IS_ADDR_MULTICAST(&mcaddr
)) {
3138 MLD_PRINTF(("%s: group %s is not multicast\n",
3139 __func__
, ip6_sprintf(&mcaddr
)));
3144 ifp
= ifindex2ifnet
[ifindex
];
3147 MLD_PRINTF(("%s: no ifp for ifindex %u\n", __func__
, ifindex
));
3151 * Internal MLD lookups require that scope/zone ID is set.
3153 (void)in6_setscope(&mcaddr
, ifp
, NULL
);
3155 in6_multihead_lock_shared();
3156 IN6_FIRST_MULTI(step
, inm
);
3157 while (inm
!= NULL
) {
3159 if (inm
->in6m_ifp
!= ifp
) {
3163 if (!IN6_ARE_ADDR_EQUAL(&inm
->in6m_addr
, &mcaddr
)) {
3167 fmode
= inm
->in6m_st
[1].iss_fmode
;
3168 retval
= SYSCTL_OUT(req
, &fmode
, sizeof(uint32_t));
3173 RB_FOREACH(ims
, ip6_msource_tree
, &inm
->in6m_srcs
) {
3174 MLD_PRINTF(("%s: visit node 0x%llx\n", __func__
,
3175 (uint64_t)VM_KERNEL_ADDRPERM(ims
)));
3177 * Only copy-out sources which are in-mode.
3179 if (fmode
!= im6s_get_mode(inm
, ims
, 1)) {
3180 MLD_PRINTF(("%s: skip non-in-mode\n",
3182 continue; /* process next source */
3184 src
= ims
->im6s_addr
;
3185 retval
= SYSCTL_OUT(req
, &src
, sizeof(struct in6_addr
));
3187 break; /* process next inm */
3192 IN6_NEXT_MULTI(step
, inm
);
3194 in6_multihead_lock_done();
3200 in6_multi_init(void)
3202 PE_parse_boot_argn("ifa_debug", &in6m_debug
, sizeof(in6m_debug
));
3204 /* Setup lock group and attribute for in6_multihead */
3205 in6_multihead_lock_grp_attr
= lck_grp_attr_alloc_init();
3206 in6_multihead_lock_grp
= lck_grp_alloc_init("in6_multihead",
3207 in6_multihead_lock_grp_attr
);
3208 in6_multihead_lock_attr
= lck_attr_alloc_init();
3209 lck_rw_init(&in6_multihead_lock
, in6_multihead_lock_grp
,
3210 in6_multihead_lock_attr
);
3212 lck_mtx_init(&in6m_trash_lock
, in6_multihead_lock_grp
,
3213 in6_multihead_lock_attr
);
3214 TAILQ_INIT(&in6m_trash_head
);
3216 in6m_size
= (in6m_debug
== 0) ? sizeof(struct in6_multi
) :
3217 sizeof(struct in6_multi_dbg
);
3218 in6m_zone
= zinit(in6m_size
, IN6M_ZONE_MAX
* in6m_size
,
3220 if (in6m_zone
== NULL
) {
3221 panic("%s: failed allocating %s", __func__
, IN6M_ZONE_NAME
);
3224 zone_change(in6m_zone
, Z_EXPAND
, TRUE
);
3226 imm_size
= sizeof(struct in6_multi_mship
);
3227 imm_zone
= zinit(imm_size
, IMM_ZONE_MAX
* imm_size
, 0, IMM_ZONE_NAME
);
3228 if (imm_zone
== NULL
) {
3229 panic("%s: failed allocating %s", __func__
, IMM_ZONE_NAME
);
3232 zone_change(imm_zone
, Z_EXPAND
, TRUE
);
3234 ip6ms_size
= sizeof(struct ip6_msource
);
3235 ip6ms_zone
= zinit(ip6ms_size
, IP6MS_ZONE_MAX
* ip6ms_size
,
3236 0, IP6MS_ZONE_NAME
);
3237 if (ip6ms_zone
== NULL
) {
3238 panic("%s: failed allocating %s", __func__
, IP6MS_ZONE_NAME
);
3241 zone_change(ip6ms_zone
, Z_EXPAND
, TRUE
);
3243 in6ms_size
= sizeof(struct in6_msource
);
3244 in6ms_zone
= zinit(in6ms_size
, IN6MS_ZONE_MAX
* in6ms_size
,
3245 0, IN6MS_ZONE_NAME
);
3246 if (in6ms_zone
== NULL
) {
3247 panic("%s: failed allocating %s", __func__
, IN6MS_ZONE_NAME
);
3250 zone_change(in6ms_zone
, Z_EXPAND
, TRUE
);
3253 static struct in6_multi
*
3254 in6_multi_alloc(int how
)
3256 struct in6_multi
*in6m
;
3258 in6m
= (how
== M_WAITOK
) ? zalloc(in6m_zone
) :
3259 zalloc_noblock(in6m_zone
);
3261 bzero(in6m
, in6m_size
);
3262 lck_mtx_init(&in6m
->in6m_lock
, in6_multihead_lock_grp
,
3263 in6_multihead_lock_attr
);
3264 in6m
->in6m_debug
|= IFD_ALLOC
;
3265 if (in6m_debug
!= 0) {
3266 in6m
->in6m_debug
|= IFD_DEBUG
;
3267 in6m
->in6m_trace
= in6m_trace
;
3274 in6_multi_free(struct in6_multi
*in6m
)
3277 if (in6m
->in6m_debug
& IFD_ATTACHED
) {
3278 panic("%s: attached in6m=%p is being freed", __func__
, in6m
);
3280 } else if (in6m
->in6m_ifma
!= NULL
) {
3281 panic("%s: ifma not NULL for in6m=%p", __func__
, in6m
);
3283 } else if (!(in6m
->in6m_debug
& IFD_ALLOC
)) {
3284 panic("%s: in6m %p cannot be freed", __func__
, in6m
);
3286 } else if (in6m
->in6m_refcount
!= 0) {
3287 panic("%s: non-zero refcount in6m=%p", __func__
, in6m
);
3289 } else if (in6m
->in6m_reqcnt
!= 0) {
3290 panic("%s: non-zero reqcnt in6m=%p", __func__
, in6m
);
3294 /* Free any pending MLDv2 state-change records */
3295 IF_DRAIN(&in6m
->in6m_scq
);
3297 in6m
->in6m_debug
&= ~IFD_ALLOC
;
3298 if ((in6m
->in6m_debug
& (IFD_DEBUG
| IFD_TRASHED
)) ==
3299 (IFD_DEBUG
| IFD_TRASHED
)) {
3300 lck_mtx_lock(&in6m_trash_lock
);
3301 TAILQ_REMOVE(&in6m_trash_head
, (struct in6_multi_dbg
*)in6m
,
3303 lck_mtx_unlock(&in6m_trash_lock
);
3304 in6m
->in6m_debug
&= ~IFD_TRASHED
;
3308 lck_mtx_destroy(&in6m
->in6m_lock
, in6_multihead_lock_grp
);
3309 zfree(in6m_zone
, in6m
);
3313 in6_multi_attach(struct in6_multi
*in6m
)
3315 in6_multihead_lock_assert(LCK_RW_ASSERT_EXCLUSIVE
);
3316 IN6M_LOCK_ASSERT_HELD(in6m
);
3318 if (in6m
->in6m_debug
& IFD_ATTACHED
) {
3319 panic("%s: Attempt to attach an already attached in6m=%p",
3324 in6m
->in6m_reqcnt
++;
3325 VERIFY(in6m
->in6m_reqcnt
== 1);
3326 IN6M_ADDREF_LOCKED(in6m
);
3327 in6m
->in6m_debug
|= IFD_ATTACHED
;
3329 * Reattach case: If debugging is enabled, take it
3330 * out of the trash list and clear IFD_TRASHED.
3332 if ((in6m
->in6m_debug
& (IFD_DEBUG
| IFD_TRASHED
)) ==
3333 (IFD_DEBUG
| IFD_TRASHED
)) {
3334 /* Become a regular mutex, just in case */
3335 IN6M_CONVERT_LOCK(in6m
);
3336 lck_mtx_lock(&in6m_trash_lock
);
3337 TAILQ_REMOVE(&in6m_trash_head
, (struct in6_multi_dbg
*)in6m
,
3339 lck_mtx_unlock(&in6m_trash_lock
);
3340 in6m
->in6m_debug
&= ~IFD_TRASHED
;
3343 LIST_INSERT_HEAD(&in6_multihead
, in6m
, in6m_entry
);
3347 in6_multi_detach(struct in6_multi
*in6m
)
3349 in6_multihead_lock_assert(LCK_RW_ASSERT_EXCLUSIVE
);
3350 IN6M_LOCK_ASSERT_HELD(in6m
);
3352 if (in6m
->in6m_reqcnt
== 0) {
3353 panic("%s: in6m=%p negative reqcnt", __func__
, in6m
);
3357 --in6m
->in6m_reqcnt
;
3358 if (in6m
->in6m_reqcnt
> 0) {
3362 if (!(in6m
->in6m_debug
& IFD_ATTACHED
)) {
3363 panic("%s: Attempt to detach an unattached record in6m=%p",
3366 } else if (in6m
->in6m_debug
& IFD_TRASHED
) {
3367 panic("%s: in6m %p is already in trash list", __func__
, in6m
);
3372 * NOTE: Caller calls IFMA_REMREF
3374 in6m
->in6m_debug
&= ~IFD_ATTACHED
;
3375 LIST_REMOVE(in6m
, in6m_entry
);
3377 if (in6m
->in6m_debug
& IFD_DEBUG
) {
3378 /* Become a regular mutex, just in case */
3379 IN6M_CONVERT_LOCK(in6m
);
3380 lck_mtx_lock(&in6m_trash_lock
);
3381 TAILQ_INSERT_TAIL(&in6m_trash_head
,
3382 (struct in6_multi_dbg
*)in6m
, in6m_trash_link
);
3383 lck_mtx_unlock(&in6m_trash_lock
);
3384 in6m
->in6m_debug
|= IFD_TRASHED
;
3391 in6m_addref(struct in6_multi
*in6m
, int locked
)
3394 IN6M_LOCK_SPIN(in6m
);
3396 IN6M_LOCK_ASSERT_HELD(in6m
);
3399 if (++in6m
->in6m_refcount
== 0) {
3400 panic("%s: in6m=%p wraparound refcnt", __func__
, in6m
);
3402 } else if (in6m
->in6m_trace
!= NULL
) {
3403 (*in6m
->in6m_trace
)(in6m
, TRUE
);
3411 in6m_remref(struct in6_multi
*in6m
, int locked
)
3413 struct ifmultiaddr
*ifma
;
3414 struct mld_ifinfo
*mli
;
3417 IN6M_LOCK_SPIN(in6m
);
3419 IN6M_LOCK_ASSERT_HELD(in6m
);
3422 if (in6m
->in6m_refcount
== 0 || (in6m
->in6m_refcount
== 1 && locked
)) {
3423 panic("%s: in6m=%p negative refcnt", __func__
, in6m
);
3425 } else if (in6m
->in6m_trace
!= NULL
) {
3426 (*in6m
->in6m_trace
)(in6m
, FALSE
);
3429 --in6m
->in6m_refcount
;
3430 if (in6m
->in6m_refcount
> 0) {
3438 * Synchronization with in6_mc_get(). In the event the in6m has been
3439 * detached, the underlying ifma would still be in the if_multiaddrs
3440 * list, and thus can be looked up via if_addmulti(). At that point,
3441 * the only way to find this in6m is via ifma_protospec. To avoid
3442 * race conditions between the last in6m_remref() of that in6m and its
3443 * use via ifma_protospec, in6_multihead lock is used for serialization.
3444 * In order to avoid violating the lock order, we must drop in6m_lock
3445 * before acquiring in6_multihead lock. To prevent the in6m from being
3446 * freed prematurely, we hold an extra reference.
3448 ++in6m
->in6m_refcount
;
3450 in6_multihead_lock_shared();
3451 IN6M_LOCK_SPIN(in6m
);
3452 --in6m
->in6m_refcount
;
3453 if (in6m
->in6m_refcount
> 0) {
3454 /* We've lost the race, so abort since in6m is still in use */
3456 in6_multihead_lock_done();
3457 /* If it was locked, return it as such */
3464 ifma
= in6m
->in6m_ifma
;
3465 in6m
->in6m_ifma
= NULL
;
3466 in6m
->in6m_ifp
= NULL
;
3467 mli
= in6m
->in6m_mli
;
3468 in6m
->in6m_mli
= NULL
;
3470 IFMA_LOCK_SPIN(ifma
);
3471 ifma
->ifma_protospec
= NULL
;
3473 in6_multihead_lock_done();
3475 in6_multi_free(in6m
);
3476 if_delmulti_ifma(ifma
);
3477 /* Release reference held to the underlying ifmultiaddr */
3486 in6m_trace(struct in6_multi
*in6m
, int refhold
)
3488 struct in6_multi_dbg
*in6m_dbg
= (struct in6_multi_dbg
*)in6m
;
3493 if (!(in6m
->in6m_debug
& IFD_DEBUG
)) {
3494 panic("%s: in6m %p has no debug structure", __func__
, in6m
);
3498 cnt
= &in6m_dbg
->in6m_refhold_cnt
;
3499 tr
= in6m_dbg
->in6m_refhold
;
3501 cnt
= &in6m_dbg
->in6m_refrele_cnt
;
3502 tr
= in6m_dbg
->in6m_refrele
;
3505 idx
= atomic_add_16_ov(cnt
, 1) % IN6M_TRACE_HIST_SIZE
;
3506 ctrace_record(&tr
[idx
]);
3509 static struct in6_multi_mship
*
3510 in6_multi_mship_alloc(int how
)
3512 struct in6_multi_mship
*imm
;
3514 imm
= (how
== M_WAITOK
) ? zalloc(imm_zone
) : zalloc_noblock(imm_zone
);
3516 bzero(imm
, imm_size
);
3523 in6_multi_mship_free(struct in6_multi_mship
*imm
)
3525 if (imm
->i6mm_maddr
!= NULL
) {
3526 panic("%s: i6mm_maddr not NULL for imm=%p", __func__
, imm
);
3529 zfree(imm_zone
, imm
);
3533 in6_multihead_lock_exclusive(void)
3535 lck_rw_lock_exclusive(&in6_multihead_lock
);
3539 in6_multihead_lock_shared(void)
3541 lck_rw_lock_shared(&in6_multihead_lock
);
3545 in6_multihead_lock_assert(int what
)
3548 #pragma unused(what)
3550 LCK_RW_ASSERT(&in6_multihead_lock
, what
);
3554 in6_multihead_lock_done(void)
3556 lck_rw_done(&in6_multihead_lock
);
3559 static struct ip6_msource
*
3560 ip6ms_alloc(int how
)
3562 struct ip6_msource
*i6ms
;
3564 i6ms
= (how
== M_WAITOK
) ? zalloc(ip6ms_zone
) :
3565 zalloc_noblock(ip6ms_zone
);
3567 bzero(i6ms
, ip6ms_size
);
3574 ip6ms_free(struct ip6_msource
*i6ms
)
3576 zfree(ip6ms_zone
, i6ms
);
3579 static struct in6_msource
*
3580 in6ms_alloc(int how
)
3582 struct in6_msource
*in6ms
;
3584 in6ms
= (how
== M_WAITOK
) ? zalloc(in6ms_zone
) :
3585 zalloc_noblock(in6ms_zone
);
3586 if (in6ms
!= NULL
) {
3587 bzero(in6ms
, in6ms_size
);
3594 in6ms_free(struct in6_msource
*in6ms
)
3596 zfree(in6ms_zone
, in6ms
);
3601 static const char *in6m_modestrs
[] = { "un\n", "in", "ex" };
3604 in6m_mode_str(const int mode
)
3606 if (mode
>= MCAST_UNDEFINED
&& mode
<= MCAST_EXCLUDE
) {
3607 return in6m_modestrs
[mode
];
3612 static const char *in6m_statestrs
[] = {
3621 "sg-query-pending\n",
3626 in6m_state_str(const int state
)
3628 if (state
>= MLD_NOT_MEMBER
&& state
<= MLD_LEAVING_MEMBER
) {
3629 return in6m_statestrs
[state
];
3635 * Dump an in6_multi structure to the console.
3638 in6m_print(const struct in6_multi
*inm
)
3642 IN6M_LOCK_ASSERT_HELD(__DECONST(struct in6_multi
*, inm
));
3644 if (mld_debug
== 0) {
3648 printf("%s: --- begin in6m 0x%llx ---\n", __func__
,
3649 (uint64_t)VM_KERNEL_ADDRPERM(inm
));
3650 printf("addr %s ifp 0x%llx(%s) ifma 0x%llx\n",
3651 ip6_sprintf(&inm
->in6m_addr
),
3652 (uint64_t)VM_KERNEL_ADDRPERM(inm
->in6m_ifp
),
3653 if_name(inm
->in6m_ifp
),
3654 (uint64_t)VM_KERNEL_ADDRPERM(inm
->in6m_ifma
));
3655 printf("timer %u state %s refcount %u scq.len %u\n",
3657 in6m_state_str(inm
->in6m_state
),
3659 inm
->in6m_scq
.ifq_len
);
3660 printf("mli 0x%llx nsrc %lu sctimer %u scrv %u\n",
3661 (uint64_t)VM_KERNEL_ADDRPERM(inm
->in6m_mli
),
3665 for (t
= 0; t
< 2; t
++) {
3666 printf("t%d: fmode %s asm %u ex %u in %u rec %u\n", t
,
3667 in6m_mode_str(inm
->in6m_st
[t
].iss_fmode
),
3668 inm
->in6m_st
[t
].iss_asm
,
3669 inm
->in6m_st
[t
].iss_ex
,
3670 inm
->in6m_st
[t
].iss_in
,
3671 inm
->in6m_st
[t
].iss_rec
);
3673 printf("%s: --- end in6m 0x%llx ---\n", __func__
,
3674 (uint64_t)VM_KERNEL_ADDRPERM(inm
));
3680 in6m_print(__unused
const struct in6_multi
*inm
)