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;
299 if (newmax
> IPV6_MAX_MEMBERSHIPS
)
300 return (ETOOMANYREFS
);
302 if ((nmships
= (struct in6_multi
**)_REALLOC(omships
,
303 sizeof (struct in6_multi
*) * newmax
, M_IP6MOPTS
,
304 M_WAITOK
| M_ZERO
)) == NULL
)
307 imo
->im6o_membership
= nmships
;
309 if ((nmfilters
= (struct in6_mfilter
*)_REALLOC(omfilters
,
310 sizeof (struct in6_mfilter
) * newmax
, M_IN6MFILTER
,
311 M_WAITOK
| M_ZERO
)) == NULL
)
314 imo
->im6o_mfilters
= nmfilters
;
316 /* Initialize newly allocated source filter heads. */
317 for (idx
= oldmax
; idx
< newmax
; idx
++)
318 im6f_init(&nmfilters
[idx
], MCAST_UNDEFINED
, MCAST_EXCLUDE
);
320 imo
->im6o_max_memberships
= newmax
;
326 * Find an IPv6 multicast group entry for this ip6_moptions instance
327 * which matches the specified group, and optionally an interface.
328 * Return its index into the array, or -1 if not found.
331 im6o_match_group(const struct ip6_moptions
*imo
, const struct ifnet
*ifp
,
332 const struct sockaddr_in6
*group
)
334 const struct sockaddr_in6
*gsin6
;
335 struct in6_multi
*pinm
;
339 IM6O_LOCK_ASSERT_HELD(__DECONST(struct ip6_moptions
*, imo
));
343 /* The im6o_membership array may be lazy allocated. */
344 if (imo
->im6o_membership
== NULL
|| imo
->im6o_num_memberships
== 0)
347 nmships
= imo
->im6o_num_memberships
;
348 for (idx
= 0; idx
< nmships
; idx
++) {
349 pinm
= imo
->im6o_membership
[idx
];
353 if ((ifp
== NULL
|| (pinm
->in6m_ifp
== ifp
)) &&
354 IN6_ARE_ADDR_EQUAL(&pinm
->in6m_addr
,
355 &gsin6
->sin6_addr
)) {
368 * Find an IPv6 multicast source entry for this imo which matches
369 * the given group index for this socket, and source address.
371 * XXX TODO: The scope ID, if present in src, is stripped before
372 * any comparison. We SHOULD enforce scope/zone checks where the source
373 * filter entry has a link scope.
375 * NOTE: This does not check if the entry is in-mode, merely if
376 * it exists, which may not be the desired behaviour.
378 static struct in6_msource
*
379 im6o_match_source(const struct ip6_moptions
*imo
, const size_t gidx
,
380 const struct sockaddr_in6
*src
)
382 struct ip6_msource find
;
383 struct in6_mfilter
*imf
;
384 struct ip6_msource
*ims
;
385 const struct sockaddr_in6
*psa
;
387 IM6O_LOCK_ASSERT_HELD(__DECONST(struct ip6_moptions
*, imo
));
389 VERIFY(src
->sin6_family
== AF_INET6
);
390 VERIFY(gidx
!= (size_t)-1 && gidx
< imo
->im6o_num_memberships
);
392 /* The im6o_mfilters array may be lazy allocated. */
393 if (imo
->im6o_mfilters
== NULL
)
395 imf
= &imo
->im6o_mfilters
[gidx
];
398 find
.im6s_addr
= psa
->sin6_addr
;
399 in6_clearscope(&find
.im6s_addr
); /* XXX */
400 ims
= RB_FIND(ip6_msource_tree
, &imf
->im6f_sources
, &find
);
402 return ((struct in6_msource
*)ims
);
406 * Perform filtering for multicast datagrams on a socket by group and source.
408 * Returns 0 if a datagram should be allowed through, or various error codes
409 * if the socket was not a member of the group, or the source was muted, etc.
412 im6o_mc_filter(const struct ip6_moptions
*imo
, const struct ifnet
*ifp
,
413 const struct sockaddr_in6
*group
, const struct sockaddr_in6
*src
)
416 struct in6_msource
*ims
;
419 IM6O_LOCK_ASSERT_HELD(__DECONST(struct ip6_moptions
*, imo
));
422 gidx
= im6o_match_group(imo
, ifp
, group
);
423 if (gidx
== (size_t)-1)
424 return (MCAST_NOTGMEMBER
);
427 * Check if the source was included in an (S,G) join.
428 * Allow reception on exclusive memberships by default,
429 * reject reception on inclusive memberships by default.
430 * Exclude source only if an in-mode exclude filter exists.
431 * Include source only if an in-mode include filter exists.
432 * NOTE: We are comparing group state here at MLD t1 (now)
433 * with socket-layer t0 (since last downcall).
435 mode
= imo
->im6o_mfilters
[gidx
].im6f_st
[1];
436 ims
= im6o_match_source(imo
, gidx
, src
);
438 if ((ims
== NULL
&& mode
== MCAST_INCLUDE
) ||
439 (ims
!= NULL
&& ims
->im6sl_st
[0] != mode
))
440 return (MCAST_NOTSMEMBER
);
446 * Find and return a reference to an in6_multi record for (ifp, group),
447 * and bump its reference count.
448 * If one does not exist, try to allocate it, and update link-layer multicast
449 * filters on ifp to listen for group.
450 * Assumes the IN6_MULTI lock is held across the call.
451 * Return 0 if successful, otherwise return an appropriate error code.
454 in6_mc_get(struct ifnet
*ifp
, const struct in6_addr
*group
,
455 struct in6_multi
**pinm
)
457 struct sockaddr_in6 gsin6
;
458 struct ifmultiaddr
*ifma
;
459 struct in6_multi
*inm
;
464 in6_multihead_lock_shared();
465 IN6_LOOKUP_MULTI(group
, ifp
, inm
);
468 VERIFY(inm
->in6m_reqcnt
>= 1);
470 VERIFY(inm
->in6m_reqcnt
!= 0);
473 in6_multihead_lock_done();
475 * We already joined this group; return the in6m
476 * with a refcount held (via lookup) for caller.
480 in6_multihead_lock_done();
482 memset(&gsin6
, 0, sizeof(gsin6
));
483 gsin6
.sin6_family
= AF_INET6
;
484 gsin6
.sin6_len
= sizeof(struct sockaddr_in6
);
485 gsin6
.sin6_addr
= *group
;
488 * Check if a link-layer group is already associated
489 * with this network-layer group on the given ifnet.
491 error
= if_addmulti(ifp
, (struct sockaddr
*)&gsin6
, &ifma
);
496 * See comments in in6m_remref() for access to ifma_protospec.
498 in6_multihead_lock_exclusive();
500 if ((inm
= ifma
->ifma_protospec
) != NULL
) {
501 VERIFY(ifma
->ifma_addr
!= NULL
);
502 VERIFY(ifma
->ifma_addr
->sa_family
== AF_INET6
);
503 IN6M_ADDREF(inm
); /* for caller */
506 VERIFY(inm
->in6m_ifma
== ifma
);
507 VERIFY(inm
->in6m_ifp
== ifp
);
508 VERIFY(IN6_ARE_ADDR_EQUAL(&inm
->in6m_addr
, group
));
509 if (inm
->in6m_debug
& IFD_ATTACHED
) {
510 VERIFY(inm
->in6m_reqcnt
>= 1);
512 VERIFY(inm
->in6m_reqcnt
!= 0);
515 in6_multihead_lock_done();
518 * We lost the race with another thread doing
519 * in6_mc_get(); since this group has already
520 * been joined; return the inm with a refcount
526 * We lost the race with another thread doing in6_delmulti();
527 * the inm referring to the ifma has been detached, thus we
528 * reattach it back to the in6_multihead list, and return the
529 * inm with a refcount held for the caller.
531 in6_multi_attach(inm
);
532 VERIFY((inm
->in6m_debug
&
533 (IFD_ATTACHED
| IFD_TRASHED
)) == IFD_ATTACHED
);
536 in6_multihead_lock_done();
543 * A new in6_multi record is needed; allocate and initialize it.
544 * We DO NOT perform an MLD join as the in6_ layer may need to
545 * push an initial source list down to MLD to support SSM.
547 * The initial source filter state is INCLUDE, {} as per the RFC.
548 * Pending state-changes per group are subject to a bounds check.
550 inm
= in6_multi_alloc(M_WAITOK
);
552 in6_multihead_lock_done();
557 inm
->in6m_addr
= *group
;
559 inm
->in6m_mli
= MLD_IFINFO(ifp
);
560 VERIFY(inm
->in6m_mli
!= NULL
);
561 MLI_ADDREF(inm
->in6m_mli
);
562 inm
->in6m_ifma
= ifma
; /* keep refcount from if_addmulti() */
563 inm
->in6m_state
= MLD_NOT_MEMBER
;
565 * Pending state-changes per group are subject to a bounds check.
567 inm
->in6m_scq
.ifq_maxlen
= MLD_MAX_STATE_CHANGES
;
568 inm
->in6m_st
[0].iss_fmode
= MCAST_UNDEFINED
;
569 inm
->in6m_st
[1].iss_fmode
= MCAST_UNDEFINED
;
570 RB_INIT(&inm
->in6m_srcs
);
572 in6_multi_attach(inm
);
573 VERIFY((inm
->in6m_debug
&
574 (IFD_ATTACHED
| IFD_TRASHED
)) == IFD_ATTACHED
);
575 IN6M_ADDREF_LOCKED(inm
); /* for caller */
579 VERIFY(ifma
->ifma_protospec
== NULL
);
580 ifma
->ifma_protospec
= inm
;
582 in6_multihead_lock_done();
588 * Clear recorded source entries for a group.
589 * Used by the MLD code. Caller must hold the IN6_MULTI lock.
590 * FIXME: Should reap.
593 in6m_clear_recorded(struct in6_multi
*inm
)
595 struct ip6_msource
*ims
;
597 IN6M_LOCK_ASSERT_HELD(inm
);
599 RB_FOREACH(ims
, ip6_msource_tree
, &inm
->in6m_srcs
) {
602 --inm
->in6m_st
[1].iss_rec
;
605 VERIFY(inm
->in6m_st
[1].iss_rec
== 0);
609 * Record a source as pending for a Source-Group MLDv2 query.
610 * This lives here as it modifies the shared tree.
612 * inm is the group descriptor.
613 * naddr is the address of the source to record in network-byte order.
615 * If the net.inet6.mld.sgalloc sysctl is non-zero, we will
616 * lazy-allocate a source node in response to an SG query.
617 * Otherwise, no allocation is performed. This saves some memory
618 * with the trade-off that the source will not be reported to the
619 * router if joined in the window between the query response and
620 * the group actually being joined on the local host.
622 * VIMAGE: XXX: Currently the mld_sgalloc feature has been removed.
623 * This turns off the allocation of a recorded source entry if
624 * the group has not been joined.
626 * Return 0 if the source didn't exist or was already marked as recorded.
627 * Return 1 if the source was marked as recorded by this function.
628 * Return <0 if any error occured (negated errno code).
631 in6m_record_source(struct in6_multi
*inm
, const struct in6_addr
*addr
)
633 struct ip6_msource find
;
634 struct ip6_msource
*ims
, *nims
;
636 IN6M_LOCK_ASSERT_HELD(inm
);
638 find
.im6s_addr
= *addr
;
639 ims
= RB_FIND(ip6_msource_tree
, &inm
->in6m_srcs
, &find
);
640 if (ims
&& ims
->im6s_stp
)
643 if (inm
->in6m_nsrc
== in6_mcast_maxgrpsrc
)
645 nims
= ip6ms_alloc(M_WAITOK
);
648 nims
->im6s_addr
= find
.im6s_addr
;
649 RB_INSERT(ip6_msource_tree
, &inm
->in6m_srcs
, nims
);
655 * Mark the source as recorded and update the recorded
659 ++inm
->in6m_st
[1].iss_rec
;
665 * Return a pointer to an in6_msource owned by an in6_mfilter,
666 * given its source address.
667 * Lazy-allocate if needed. If this is a new entry its filter state is
670 * imf is the filter set being modified.
671 * addr is the source address.
673 * Caller is expected to be holding im6o_lock.
676 im6f_get_source(struct in6_mfilter
*imf
, const struct sockaddr_in6
*psin
,
677 struct in6_msource
**plims
)
679 struct ip6_msource find
;
680 struct ip6_msource
*ims
;
681 struct in6_msource
*lims
;
688 find
.im6s_addr
= psin
->sin6_addr
;
689 ims
= RB_FIND(ip6_msource_tree
, &imf
->im6f_sources
, &find
);
690 lims
= (struct in6_msource
*)ims
;
692 if (imf
->im6f_nsrc
== in6_mcast_maxsocksrc
)
694 lims
= in6ms_alloc(M_WAITOK
);
697 lims
->im6s_addr
= find
.im6s_addr
;
698 lims
->im6sl_st
[0] = MCAST_UNDEFINED
;
699 RB_INSERT(ip6_msource_tree
, &imf
->im6f_sources
,
700 (struct ip6_msource
*)lims
);
710 * Graft a source entry into an existing socket-layer filter set,
711 * maintaining any required invariants and checking allocations.
713 * The source is marked as being in the new filter mode at t1.
715 * Return the pointer to the new node, otherwise return NULL.
717 * Caller is expected to be holding im6o_lock.
719 static struct in6_msource
*
720 im6f_graft(struct in6_mfilter
*imf
, const uint8_t st1
,
721 const struct sockaddr_in6
*psin
)
723 struct in6_msource
*lims
;
725 lims
= in6ms_alloc(M_WAITOK
);
728 lims
->im6s_addr
= psin
->sin6_addr
;
729 lims
->im6sl_st
[0] = MCAST_UNDEFINED
;
730 lims
->im6sl_st
[1] = st1
;
731 RB_INSERT(ip6_msource_tree
, &imf
->im6f_sources
,
732 (struct ip6_msource
*)lims
);
739 * Prune a source entry from an existing socket-layer filter set,
740 * maintaining any required invariants and checking allocations.
742 * The source is marked as being left at t1, it is not freed.
744 * Return 0 if no error occurred, otherwise return an errno value.
746 * Caller is expected to be holding im6o_lock.
749 im6f_prune(struct in6_mfilter
*imf
, const struct sockaddr_in6
*psin
)
751 struct ip6_msource find
;
752 struct ip6_msource
*ims
;
753 struct in6_msource
*lims
;
755 find
.im6s_addr
= psin
->sin6_addr
;
756 ims
= RB_FIND(ip6_msource_tree
, &imf
->im6f_sources
, &find
);
759 lims
= (struct in6_msource
*)ims
;
760 lims
->im6sl_st
[1] = MCAST_UNDEFINED
;
765 * Revert socket-layer filter set deltas at t1 to t0 state.
767 * Caller is expected to be holding im6o_lock.
770 im6f_rollback(struct in6_mfilter
*imf
)
772 struct ip6_msource
*ims
, *tims
;
773 struct in6_msource
*lims
;
775 RB_FOREACH_SAFE(ims
, ip6_msource_tree
, &imf
->im6f_sources
, tims
) {
776 lims
= (struct in6_msource
*)ims
;
777 if (lims
->im6sl_st
[0] == lims
->im6sl_st
[1]) {
778 /* no change at t1 */
780 } else if (lims
->im6sl_st
[0] != MCAST_UNDEFINED
) {
781 /* revert change to existing source at t1 */
782 lims
->im6sl_st
[1] = lims
->im6sl_st
[0];
784 /* revert source added t1 */
785 MLD_PRINTF(("%s: free in6ms 0x%llx\n", __func__
,
786 (uint64_t)VM_KERNEL_ADDRPERM(lims
)));
787 RB_REMOVE(ip6_msource_tree
, &imf
->im6f_sources
, ims
);
792 imf
->im6f_st
[1] = imf
->im6f_st
[0];
796 * Mark socket-layer filter set as INCLUDE {} at t1.
798 * Caller is expected to be holding im6o_lock.
801 im6f_leave(struct in6_mfilter
*imf
)
803 struct ip6_msource
*ims
;
804 struct in6_msource
*lims
;
806 RB_FOREACH(ims
, ip6_msource_tree
, &imf
->im6f_sources
) {
807 lims
= (struct in6_msource
*)ims
;
808 lims
->im6sl_st
[1] = MCAST_UNDEFINED
;
810 imf
->im6f_st
[1] = MCAST_INCLUDE
;
814 * Mark socket-layer filter set deltas as committed.
816 * Caller is expected to be holding im6o_lock.
819 im6f_commit(struct in6_mfilter
*imf
)
821 struct ip6_msource
*ims
;
822 struct in6_msource
*lims
;
824 RB_FOREACH(ims
, ip6_msource_tree
, &imf
->im6f_sources
) {
825 lims
= (struct in6_msource
*)ims
;
826 lims
->im6sl_st
[0] = lims
->im6sl_st
[1];
828 imf
->im6f_st
[0] = imf
->im6f_st
[1];
832 * Reap unreferenced sources from socket-layer filter set.
834 * Caller is expected to be holding im6o_lock.
837 im6f_reap(struct in6_mfilter
*imf
)
839 struct ip6_msource
*ims
, *tims
;
840 struct in6_msource
*lims
;
842 RB_FOREACH_SAFE(ims
, ip6_msource_tree
, &imf
->im6f_sources
, tims
) {
843 lims
= (struct in6_msource
*)ims
;
844 if ((lims
->im6sl_st
[0] == MCAST_UNDEFINED
) &&
845 (lims
->im6sl_st
[1] == MCAST_UNDEFINED
)) {
846 MLD_PRINTF(("%s: free in6ms 0x%llx\n", __func__
,
847 (uint64_t)VM_KERNEL_ADDRPERM(lims
)));
848 RB_REMOVE(ip6_msource_tree
, &imf
->im6f_sources
, ims
);
856 * Purge socket-layer filter set.
858 * Caller is expected to be holding im6o_lock.
861 im6f_purge(struct in6_mfilter
*imf
)
863 struct ip6_msource
*ims
, *tims
;
864 struct in6_msource
*lims
;
866 RB_FOREACH_SAFE(ims
, ip6_msource_tree
, &imf
->im6f_sources
, tims
) {
867 lims
= (struct in6_msource
*)ims
;
868 MLD_PRINTF(("%s: free in6ms 0x%llx\n", __func__
,
869 (uint64_t)VM_KERNEL_ADDRPERM(lims
)));
870 RB_REMOVE(ip6_msource_tree
, &imf
->im6f_sources
, ims
);
874 imf
->im6f_st
[0] = imf
->im6f_st
[1] = MCAST_UNDEFINED
;
875 VERIFY(RB_EMPTY(&imf
->im6f_sources
));
879 * Look up a source filter entry for a multicast group.
881 * inm is the group descriptor to work with.
882 * addr is the IPv6 address to look up.
883 * noalloc may be non-zero to suppress allocation of sources.
884 * *pims will be set to the address of the retrieved or allocated source.
886 * Return 0 if successful, otherwise return a non-zero error code.
889 in6m_get_source(struct in6_multi
*inm
, const struct in6_addr
*addr
,
890 const int noalloc
, struct ip6_msource
**pims
)
892 struct ip6_msource find
;
893 struct ip6_msource
*ims
, *nims
;
895 IN6M_LOCK_ASSERT_HELD(inm
);
897 find
.im6s_addr
= *addr
;
898 ims
= RB_FIND(ip6_msource_tree
, &inm
->in6m_srcs
, &find
);
899 if (ims
== NULL
&& !noalloc
) {
900 if (inm
->in6m_nsrc
== in6_mcast_maxgrpsrc
)
902 nims
= ip6ms_alloc(M_WAITOK
);
905 nims
->im6s_addr
= *addr
;
906 RB_INSERT(ip6_msource_tree
, &inm
->in6m_srcs
, nims
);
909 MLD_PRINTF(("%s: allocated %s as 0x%llx\n", __func__
,
910 ip6_sprintf(addr
), (uint64_t)VM_KERNEL_ADDRPERM(ims
)));
918 * Helper function to derive the filter mode on a source entry
919 * from its internal counters. Predicates are:
920 * A source is only excluded if all listeners exclude it.
921 * A source is only included if no listeners exclude it,
922 * and at least one listener includes it.
923 * May be used by ifmcstat(8).
926 im6s_get_mode(const struct in6_multi
*inm
, const struct ip6_msource
*ims
,
929 IN6M_LOCK_ASSERT_HELD(__DECONST(struct in6_multi
*, inm
));
932 if (inm
->in6m_st
[t
].iss_ex
> 0 &&
933 inm
->in6m_st
[t
].iss_ex
== ims
->im6s_st
[t
].ex
)
934 return (MCAST_EXCLUDE
);
935 else if (ims
->im6s_st
[t
].in
> 0 && ims
->im6s_st
[t
].ex
== 0)
936 return (MCAST_INCLUDE
);
937 return (MCAST_UNDEFINED
);
941 * Merge socket-layer source into MLD-layer source.
942 * If rollback is non-zero, perform the inverse of the merge.
945 im6s_merge(struct ip6_msource
*ims
, const struct in6_msource
*lims
,
948 int n
= rollback
? -1 : 1;
950 if (lims
->im6sl_st
[0] == MCAST_EXCLUDE
) {
951 MLD_PRINTF(("%s: t1 ex -= %d on %s\n", __func__
, n
,
952 ip6_sprintf(&lims
->im6s_addr
)));
953 ims
->im6s_st
[1].ex
-= n
;
954 } else if (lims
->im6sl_st
[0] == MCAST_INCLUDE
) {
955 MLD_PRINTF(("%s: t1 in -= %d on %s\n", __func__
, n
,
956 ip6_sprintf(&lims
->im6s_addr
)));
957 ims
->im6s_st
[1].in
-= n
;
960 if (lims
->im6sl_st
[1] == MCAST_EXCLUDE
) {
961 MLD_PRINTF(("%s: t1 ex += %d on %s\n", __func__
, n
,
962 ip6_sprintf(&lims
->im6s_addr
)));
963 ims
->im6s_st
[1].ex
+= n
;
964 } else if (lims
->im6sl_st
[1] == MCAST_INCLUDE
) {
965 MLD_PRINTF(("%s: t1 in += %d on %s\n", __func__
, n
,
966 ip6_sprintf(&lims
->im6s_addr
)));
967 ims
->im6s_st
[1].in
+= n
;
972 * Atomically update the global in6_multi state, when a membership's
973 * filter list is being updated in any way.
975 * imf is the per-inpcb-membership group filter pointer.
976 * A fake imf may be passed for in-kernel consumers.
978 * XXX This is a candidate for a set-symmetric-difference style loop
979 * which would eliminate the repeated lookup from root of ims nodes,
980 * as they share the same key space.
982 * If any error occurred this function will back out of refcounts
983 * and return a non-zero value.
986 in6m_merge(struct in6_multi
*inm
, /*const*/ struct in6_mfilter
*imf
)
988 struct ip6_msource
*ims
, *nims
= NULL
;
989 struct in6_msource
*lims
;
993 IN6M_LOCK_ASSERT_HELD(inm
);
1000 * Update the source filters first, as this may fail.
1001 * Maintain count of in-mode filters at t0, t1. These are
1002 * used to work out if we transition into ASM mode or not.
1003 * Maintain a count of source filters whose state was
1004 * actually modified by this operation.
1006 RB_FOREACH(ims
, ip6_msource_tree
, &imf
->im6f_sources
) {
1007 lims
= (struct in6_msource
*)ims
;
1008 if (lims
->im6sl_st
[0] == imf
->im6f_st
[0]) nsrc0
++;
1009 if (lims
->im6sl_st
[1] == imf
->im6f_st
[1]) nsrc1
++;
1010 if (lims
->im6sl_st
[0] == lims
->im6sl_st
[1]) continue;
1011 error
= in6m_get_source(inm
, &lims
->im6s_addr
, 0, &nims
);
1015 im6s_merge(nims
, lims
, 0);
1018 struct ip6_msource
*bims
;
1020 RB_FOREACH_REVERSE_FROM(ims
, ip6_msource_tree
, nims
) {
1021 lims
= (struct in6_msource
*)ims
;
1022 if (lims
->im6sl_st
[0] == lims
->im6sl_st
[1])
1024 (void) in6m_get_source(inm
, &lims
->im6s_addr
, 1, &bims
);
1027 im6s_merge(bims
, lims
, 1);
1032 MLD_PRINTF(("%s: imf filters in-mode: %d at t0, %d at t1\n",
1033 __func__
, nsrc0
, nsrc1
));
1035 /* Handle transition between INCLUDE {n} and INCLUDE {} on socket. */
1036 if (imf
->im6f_st
[0] == imf
->im6f_st
[1] &&
1037 imf
->im6f_st
[1] == MCAST_INCLUDE
) {
1039 MLD_PRINTF(("%s: --in on inm at t1\n", __func__
));
1040 --inm
->in6m_st
[1].iss_in
;
1044 /* Handle filter mode transition on socket. */
1045 if (imf
->im6f_st
[0] != imf
->im6f_st
[1]) {
1046 MLD_PRINTF(("%s: imf transition %d to %d\n",
1047 __func__
, imf
->im6f_st
[0], imf
->im6f_st
[1]));
1049 if (imf
->im6f_st
[0] == MCAST_EXCLUDE
) {
1050 MLD_PRINTF(("%s: --ex on inm at t1\n", __func__
));
1051 --inm
->in6m_st
[1].iss_ex
;
1052 } else if (imf
->im6f_st
[0] == MCAST_INCLUDE
) {
1053 MLD_PRINTF(("%s: --in on inm at t1\n", __func__
));
1054 --inm
->in6m_st
[1].iss_in
;
1057 if (imf
->im6f_st
[1] == MCAST_EXCLUDE
) {
1058 MLD_PRINTF(("%s: ex++ on inm at t1\n", __func__
));
1059 inm
->in6m_st
[1].iss_ex
++;
1060 } else if (imf
->im6f_st
[1] == MCAST_INCLUDE
&& nsrc1
> 0) {
1061 MLD_PRINTF(("%s: in++ on inm at t1\n", __func__
));
1062 inm
->in6m_st
[1].iss_in
++;
1067 * Track inm filter state in terms of listener counts.
1068 * If there are any exclusive listeners, stack-wide
1069 * membership is exclusive.
1070 * Otherwise, if only inclusive listeners, stack-wide is inclusive.
1071 * If no listeners remain, state is undefined at t1,
1072 * and the MLD lifecycle for this group should finish.
1074 if (inm
->in6m_st
[1].iss_ex
> 0) {
1075 MLD_PRINTF(("%s: transition to EX\n", __func__
));
1076 inm
->in6m_st
[1].iss_fmode
= MCAST_EXCLUDE
;
1077 } else if (inm
->in6m_st
[1].iss_in
> 0) {
1078 MLD_PRINTF(("%s: transition to IN\n", __func__
));
1079 inm
->in6m_st
[1].iss_fmode
= MCAST_INCLUDE
;
1081 MLD_PRINTF(("%s: transition to UNDEF\n", __func__
));
1082 inm
->in6m_st
[1].iss_fmode
= MCAST_UNDEFINED
;
1085 /* Decrement ASM listener count on transition out of ASM mode. */
1086 if (imf
->im6f_st
[0] == MCAST_EXCLUDE
&& nsrc0
== 0) {
1087 if ((imf
->im6f_st
[1] != MCAST_EXCLUDE
) ||
1088 (imf
->im6f_st
[1] == MCAST_EXCLUDE
&& nsrc1
> 0)) {
1089 MLD_PRINTF(("%s: --asm on inm at t1\n", __func__
));
1090 --inm
->in6m_st
[1].iss_asm
;
1094 /* Increment ASM listener count on transition to ASM mode. */
1095 if (imf
->im6f_st
[1] == MCAST_EXCLUDE
&& nsrc1
== 0) {
1096 MLD_PRINTF(("%s: asm++ on inm at t1\n", __func__
));
1097 inm
->in6m_st
[1].iss_asm
++;
1100 MLD_PRINTF(("%s: merged imf 0x%llx to inm 0x%llx\n", __func__
,
1101 (uint64_t)VM_KERNEL_ADDRPERM(imf
),
1102 (uint64_t)VM_KERNEL_ADDRPERM(inm
)));
1107 MLD_PRINTF(("%s: sources changed; reaping\n", __func__
));
1114 * Mark an in6_multi's filter set deltas as committed.
1115 * Called by MLD after a state change has been enqueued.
1118 in6m_commit(struct in6_multi
*inm
)
1120 struct ip6_msource
*ims
;
1122 IN6M_LOCK_ASSERT_HELD(inm
);
1124 MLD_PRINTF(("%s: commit inm 0x%llx\n", __func__
,
1125 (uint64_t)VM_KERNEL_ADDRPERM(inm
)));
1126 MLD_PRINTF(("%s: pre commit:\n", __func__
));
1129 RB_FOREACH(ims
, ip6_msource_tree
, &inm
->in6m_srcs
) {
1130 ims
->im6s_st
[0] = ims
->im6s_st
[1];
1132 inm
->in6m_st
[0] = inm
->in6m_st
[1];
1136 * Reap unreferenced nodes from an in6_multi's filter set.
1139 in6m_reap(struct in6_multi
*inm
)
1141 struct ip6_msource
*ims
, *tims
;
1143 IN6M_LOCK_ASSERT_HELD(inm
);
1145 RB_FOREACH_SAFE(ims
, ip6_msource_tree
, &inm
->in6m_srcs
, tims
) {
1146 if (ims
->im6s_st
[0].ex
> 0 || ims
->im6s_st
[0].in
> 0 ||
1147 ims
->im6s_st
[1].ex
> 0 || ims
->im6s_st
[1].in
> 0 ||
1150 MLD_PRINTF(("%s: free ims 0x%llx\n", __func__
,
1151 (uint64_t)VM_KERNEL_ADDRPERM(ims
)));
1152 RB_REMOVE(ip6_msource_tree
, &inm
->in6m_srcs
, ims
);
1159 * Purge all source nodes from an in6_multi's filter set.
1162 in6m_purge(struct in6_multi
*inm
)
1164 struct ip6_msource
*ims
, *tims
;
1166 IN6M_LOCK_ASSERT_HELD(inm
);
1168 RB_FOREACH_SAFE(ims
, ip6_msource_tree
, &inm
->in6m_srcs
, tims
) {
1169 MLD_PRINTF(("%s: free ims 0x%llx\n", __func__
,
1170 (uint64_t)VM_KERNEL_ADDRPERM(ims
)));
1171 RB_REMOVE(ip6_msource_tree
, &inm
->in6m_srcs
, ims
);
1178 * Join a multicast address w/o sources.
1179 * KAME compatibility entry point.
1182 struct in6_multi_mship
*
1183 in6_joingroup(struct ifnet
*ifp
, struct in6_addr
*mcaddr
,
1184 int *errorp
, int delay
)
1186 struct in6_multi_mship
*imm
;
1191 imm
= in6_multi_mship_alloc(M_WAITOK
);
1197 error
= in6_mc_join(ifp
, mcaddr
, NULL
, &imm
->i6mm_maddr
, delay
);
1200 in6_multi_mship_free(imm
);
1208 * Leave a multicast address w/o sources.
1209 * KAME compatibility entry point.
1212 in6_leavegroup(struct in6_multi_mship
*imm
)
1214 if (imm
->i6mm_maddr
!= NULL
) {
1215 in6_mc_leave(imm
->i6mm_maddr
, NULL
);
1216 IN6M_REMREF(imm
->i6mm_maddr
);
1217 imm
->i6mm_maddr
= NULL
;
1219 in6_multi_mship_free(imm
);
1224 * Join a multicast group; real entry point.
1226 * Only preserves atomicity at inm level.
1227 * NOTE: imf argument cannot be const due to sys/tree.h limitations.
1229 * If the MLD downcall fails, the group is not joined, and an error
1233 in6_mc_join(struct ifnet
*ifp
, const struct in6_addr
*mcaddr
,
1234 /*const*/ struct in6_mfilter
*imf
, struct in6_multi
**pinm
,
1237 struct in6_mfilter timf
;
1238 struct in6_multi
*inm
= NULL
;
1240 struct mld_tparams mtp
;
1243 * Sanity: Check scope zone ID was set for ifp, if and
1244 * only if group is scoped to an interface.
1246 VERIFY(IN6_IS_ADDR_MULTICAST(mcaddr
));
1247 if (IN6_IS_ADDR_MC_LINKLOCAL(mcaddr
) ||
1248 IN6_IS_ADDR_MC_INTFACELOCAL(mcaddr
)) {
1249 VERIFY(mcaddr
->s6_addr16
[1] != 0);
1252 MLD_PRINTF(("%s: join %s on 0x%llx(%s))\n", __func__
,
1253 ip6_sprintf(mcaddr
), (uint64_t)VM_KERNEL_ADDRPERM(ifp
),
1256 bzero(&mtp
, sizeof (mtp
));
1260 * If no imf was specified (i.e. kernel consumer),
1261 * fake one up and assume it is an ASM join.
1264 im6f_init(&timf
, MCAST_UNDEFINED
, MCAST_EXCLUDE
);
1268 error
= in6_mc_get(ifp
, mcaddr
, &inm
);
1270 MLD_PRINTF(("%s: in6_mc_get() failure\n", __func__
));
1274 MLD_PRINTF(("%s: merge inm state\n", __func__
));
1277 error
= in6m_merge(inm
, imf
);
1279 MLD_PRINTF(("%s: failed to merge inm state\n", __func__
));
1280 goto out_in6m_release
;
1283 MLD_PRINTF(("%s: doing mld downcall\n", __func__
));
1284 error
= mld_change_state(inm
, &mtp
, delay
);
1286 MLD_PRINTF(("%s: failed to update source\n", __func__
));
1288 goto out_in6m_release
;
1293 MLD_PRINTF(("%s: dropping ref on 0x%llx\n", __func__
,
1294 (uint64_t)VM_KERNEL_ADDRPERM(inm
)));
1299 *pinm
= inm
; /* keep refcount from in6_mc_get() */
1302 /* schedule timer now that we've dropped the lock(s) */
1303 mld_set_timeout(&mtp
);
1309 * Leave a multicast group; real entry point.
1310 * All source filters will be expunged.
1312 * Only preserves atomicity at inm level.
1314 * Holding the write lock for the INP which contains imf
1315 * is highly advisable. We can't assert for it as imf does not
1316 * contain a back-pointer to the owning inp.
1318 * Note: This is not the same as in6m_release(*) as this function also
1319 * makes a state change downcall into MLD.
1322 in6_mc_leave(struct in6_multi
*inm
, /*const*/ struct in6_mfilter
*imf
)
1324 struct in6_mfilter timf
;
1326 struct mld_tparams mtp
;
1328 bzero(&mtp
, sizeof (mtp
));
1331 IN6M_LOCK_ASSERT_NOTHELD(inm
);
1333 in6_multihead_lock_exclusive();
1336 MLD_PRINTF(("%s: leave inm 0x%llx, %s/%s%d, imf 0x%llx\n", __func__
,
1337 (uint64_t)VM_KERNEL_ADDRPERM(inm
), ip6_sprintf(&inm
->in6m_addr
),
1338 (in6m_is_ifp_detached(inm
) ? "null" : inm
->in6m_ifp
->if_name
),
1339 inm
->in6m_ifp
->if_unit
, (uint64_t)VM_KERNEL_ADDRPERM(imf
)));
1342 * If no imf was specified (i.e. kernel consumer),
1343 * fake one up and assume it is an ASM join.
1346 im6f_init(&timf
, MCAST_EXCLUDE
, MCAST_UNDEFINED
);
1351 * Begin state merge transaction at MLD layer.
1353 * As this particular invocation should not cause any memory
1354 * to be allocated, and there is no opportunity to roll back
1355 * the transaction, it MUST NOT fail.
1357 MLD_PRINTF(("%s: merge inm state\n", __func__
));
1359 error
= in6m_merge(inm
, imf
);
1360 KASSERT(error
== 0, ("%s: failed to merge inm state\n", __func__
));
1362 MLD_PRINTF(("%s: doing mld downcall\n", __func__
));
1363 error
= mld_change_state(inm
, &mtp
, 0);
1366 MLD_PRINTF(("%s: failed mld downcall\n", __func__
));
1368 lastref
= in6_multi_detach(inm
);
1369 VERIFY(!lastref
|| (!(inm
->in6m_debug
& IFD_ATTACHED
) &&
1370 inm
->in6m_reqcnt
== 0));
1372 in6_multihead_lock_done();
1375 IN6M_REMREF(inm
); /* for in6_multihead list */
1377 /* schedule timer now that we've dropped the lock(s) */
1378 mld_set_timeout(&mtp
);
1384 * Block or unblock an ASM multicast source on an inpcb.
1385 * This implements the delta-based API described in RFC 3678.
1387 * The delta-based API applies only to exclusive-mode memberships.
1388 * An MLD downcall will be performed.
1390 * Return 0 if successful, otherwise return an appropriate error code.
1393 in6p_block_unblock_source(struct inpcb
*inp
, struct sockopt
*sopt
)
1395 struct group_source_req gsr
;
1396 struct sockaddr_in6
*gsa
, *ssa
;
1398 struct in6_mfilter
*imf
;
1399 struct ip6_moptions
*imo
;
1400 struct in6_msource
*ims
;
1401 struct in6_multi
*inm
;
1405 struct mld_tparams mtp
;
1407 bzero(&mtp
, sizeof (mtp
));
1412 memset(&gsr
, 0, sizeof(struct group_source_req
));
1413 gsa
= (struct sockaddr_in6
*)&gsr
.gsr_group
;
1414 ssa
= (struct sockaddr_in6
*)&gsr
.gsr_source
;
1416 switch (sopt
->sopt_name
) {
1417 case MCAST_BLOCK_SOURCE
:
1418 case MCAST_UNBLOCK_SOURCE
:
1419 error
= sooptcopyin(sopt
, &gsr
,
1420 sizeof(struct group_source_req
),
1421 sizeof(struct group_source_req
));
1425 if (gsa
->sin6_family
!= AF_INET6
||
1426 gsa
->sin6_len
!= sizeof(struct sockaddr_in6
))
1429 if (ssa
->sin6_family
!= AF_INET6
||
1430 ssa
->sin6_len
!= sizeof(struct sockaddr_in6
))
1433 ifnet_head_lock_shared();
1434 if (gsr
.gsr_interface
== 0 ||
1435 (u_int
)if_index
< gsr
.gsr_interface
) {
1437 return (EADDRNOTAVAIL
);
1440 ifp
= ifindex2ifnet
[gsr
.gsr_interface
];
1444 return (EADDRNOTAVAIL
);
1446 if (sopt
->sopt_name
== MCAST_BLOCK_SOURCE
)
1451 MLD_PRINTF(("%s: unknown sopt_name %d\n",
1452 __func__
, sopt
->sopt_name
));
1453 return (EOPNOTSUPP
);
1456 if (!IN6_IS_ADDR_MULTICAST(&gsa
->sin6_addr
))
1459 (void) in6_setscope(&gsa
->sin6_addr
, ifp
, NULL
);
1462 * Check if we are actually a member of this group.
1464 imo
= in6p_findmoptions(inp
);
1469 idx
= im6o_match_group(imo
, ifp
, gsa
);
1470 if (idx
== (size_t)-1 || imo
->im6o_mfilters
== NULL
) {
1471 error
= EADDRNOTAVAIL
;
1472 goto out_imo_locked
;
1475 VERIFY(imo
->im6o_mfilters
!= NULL
);
1476 imf
= &imo
->im6o_mfilters
[idx
];
1477 inm
= imo
->im6o_membership
[idx
];
1480 * Attempting to use the delta-based API on an
1481 * non exclusive-mode membership is an error.
1483 fmode
= imf
->im6f_st
[0];
1484 if (fmode
!= MCAST_EXCLUDE
) {
1486 goto out_imo_locked
;
1490 * Deal with error cases up-front:
1491 * Asked to block, but already blocked; or
1492 * Asked to unblock, but nothing to unblock.
1493 * If adding a new block entry, allocate it.
1495 ims
= im6o_match_source(imo
, idx
, ssa
);
1496 if ((ims
!= NULL
&& doblock
) || (ims
== NULL
&& !doblock
)) {
1497 MLD_PRINTF(("%s: source %s %spresent\n", __func__
,
1498 ip6_sprintf(&ssa
->sin6_addr
),
1499 doblock
? "" : "not "));
1500 error
= EADDRNOTAVAIL
;
1501 goto out_imo_locked
;
1505 * Begin state merge transaction at socket layer.
1508 MLD_PRINTF(("%s: %s source\n", __func__
, "block"));
1509 ims
= im6f_graft(imf
, fmode
, ssa
);
1513 MLD_PRINTF(("%s: %s source\n", __func__
, "allow"));
1514 error
= im6f_prune(imf
, ssa
);
1518 MLD_PRINTF(("%s: merge imf state failed\n", __func__
));
1519 goto out_im6f_rollback
;
1523 * Begin state merge transaction at MLD layer.
1526 MLD_PRINTF(("%s: merge inm state\n", __func__
));
1527 error
= in6m_merge(inm
, imf
);
1529 MLD_PRINTF(("%s: failed to merge inm state\n", __func__
));
1531 goto out_im6f_rollback
;
1534 MLD_PRINTF(("%s: doing mld downcall\n", __func__
));
1535 error
= mld_change_state(inm
, &mtp
, 0);
1539 MLD_PRINTF(("%s: failed mld downcall\n", __func__
));
1552 IM6O_REMREF(imo
); /* from in6p_findmoptions() */
1554 /* schedule timer now that we've dropped the lock(s) */
1555 mld_set_timeout(&mtp
);
1561 * Given an inpcb, return its multicast options structure pointer. Accepts
1562 * an unlocked inpcb pointer, but will return it locked. May sleep.
1565 static struct ip6_moptions
*
1566 in6p_findmoptions(struct inpcb
*inp
)
1568 struct ip6_moptions
*imo
;
1569 struct in6_multi
**immp
;
1570 struct in6_mfilter
*imfp
;
1573 if ((imo
= inp
->in6p_moptions
) != NULL
) {
1574 IM6O_ADDREF(imo
); /* for caller */
1578 imo
= ip6_allocmoptions(M_WAITOK
);
1582 immp
= _MALLOC(sizeof (*immp
) * IPV6_MIN_MEMBERSHIPS
, M_IP6MOPTS
,
1589 imfp
= _MALLOC(sizeof (struct in6_mfilter
) * IPV6_MIN_MEMBERSHIPS
,
1590 M_IN6MFILTER
, M_WAITOK
| M_ZERO
);
1592 _FREE(immp
, M_IP6MOPTS
);
1597 imo
->im6o_multicast_ifp
= NULL
;
1598 imo
->im6o_multicast_hlim
= ip6_defmcasthlim
;
1599 imo
->im6o_multicast_loop
= in6_mcast_loop
;
1600 imo
->im6o_num_memberships
= 0;
1601 imo
->im6o_max_memberships
= IPV6_MIN_MEMBERSHIPS
;
1602 imo
->im6o_membership
= immp
;
1604 /* Initialize per-group source filters. */
1605 for (idx
= 0; idx
< IPV6_MIN_MEMBERSHIPS
; idx
++)
1606 im6f_init(&imfp
[idx
], MCAST_UNDEFINED
, MCAST_EXCLUDE
);
1608 imo
->im6o_mfilters
= imfp
;
1609 inp
->in6p_moptions
= imo
; /* keep reference from ip6_allocmoptions() */
1610 IM6O_ADDREF(imo
); /* for caller */
1616 * Atomically get source filters on a socket for an IPv6 multicast group.
1617 * Called with INP lock held; returns with lock released.
1620 in6p_get_source_filters(struct inpcb
*inp
, struct sockopt
*sopt
)
1622 struct __msfilterreq64 msfr
, msfr64
;
1623 struct __msfilterreq32 msfr32
;
1624 struct sockaddr_in6
*gsa
;
1626 struct ip6_moptions
*imo
;
1627 struct in6_mfilter
*imf
;
1628 struct ip6_msource
*ims
;
1629 struct in6_msource
*lims
;
1630 struct sockaddr_in6
*psin
;
1631 struct sockaddr_storage
*ptss
;
1632 struct sockaddr_storage
*tss
;
1634 size_t idx
, nsrcs
, ncsrcs
;
1635 user_addr_t tmp_ptr
;
1637 imo
= inp
->in6p_moptions
;
1638 VERIFY(imo
!= NULL
);
1640 if (IS_64BIT_PROCESS(current_proc())) {
1641 error
= sooptcopyin(sopt
, &msfr64
,
1642 sizeof(struct __msfilterreq64
),
1643 sizeof(struct __msfilterreq64
));
1646 /* we never use msfr.msfr_srcs; */
1647 memcpy(&msfr
, &msfr64
, sizeof(msfr64
));
1649 error
= sooptcopyin(sopt
, &msfr32
,
1650 sizeof(struct __msfilterreq32
),
1651 sizeof(struct __msfilterreq32
));
1654 /* we never use msfr.msfr_srcs; */
1655 memcpy(&msfr
, &msfr32
, sizeof(msfr32
));
1658 if (msfr
.msfr_group
.ss_family
!= AF_INET6
||
1659 msfr
.msfr_group
.ss_len
!= sizeof(struct sockaddr_in6
))
1662 gsa
= (struct sockaddr_in6
*)&msfr
.msfr_group
;
1663 if (!IN6_IS_ADDR_MULTICAST(&gsa
->sin6_addr
))
1666 ifnet_head_lock_shared();
1667 if (msfr
.msfr_ifindex
== 0 || (u_int
)if_index
< msfr
.msfr_ifindex
) {
1669 return (EADDRNOTAVAIL
);
1671 ifp
= ifindex2ifnet
[msfr
.msfr_ifindex
];
1675 return (EADDRNOTAVAIL
);
1677 if ((size_t) msfr
.msfr_nsrcs
>
1678 UINT32_MAX
/ sizeof(struct sockaddr_storage
))
1679 msfr
.msfr_nsrcs
= UINT32_MAX
/ sizeof(struct sockaddr_storage
);
1681 if (msfr
.msfr_nsrcs
> in6_mcast_maxsocksrc
)
1682 msfr
.msfr_nsrcs
= in6_mcast_maxsocksrc
;
1684 (void)in6_setscope(&gsa
->sin6_addr
, ifp
, NULL
);
1688 * Lookup group on the socket.
1690 idx
= im6o_match_group(imo
, ifp
, gsa
);
1691 if (idx
== (size_t)-1 || imo
->im6o_mfilters
== NULL
) {
1693 return (EADDRNOTAVAIL
);
1695 imf
= &imo
->im6o_mfilters
[idx
];
1698 * Ignore memberships which are in limbo.
1700 if (imf
->im6f_st
[1] == MCAST_UNDEFINED
) {
1704 msfr
.msfr_fmode
= imf
->im6f_st
[1];
1707 * If the user specified a buffer, copy out the source filter
1708 * entries to userland gracefully.
1709 * We only copy out the number of entries which userland
1710 * has asked for, but we always tell userland how big the
1711 * buffer really needs to be.
1715 if (IS_64BIT_PROCESS(current_proc()))
1716 tmp_ptr
= msfr64
.msfr_srcs
;
1718 tmp_ptr
= CAST_USER_ADDR_T(msfr32
.msfr_srcs
);
1720 if (tmp_ptr
!= USER_ADDR_NULL
&& msfr
.msfr_nsrcs
> 0) {
1721 tss
= _MALLOC((size_t) msfr
.msfr_nsrcs
* sizeof(*tss
),
1722 M_TEMP
, M_WAITOK
| M_ZERO
);
1730 * Count number of sources in-mode at t0.
1731 * If buffer space exists and remains, copy out source entries.
1733 nsrcs
= msfr
.msfr_nsrcs
;
1736 RB_FOREACH(ims
, ip6_msource_tree
, &imf
->im6f_sources
) {
1737 lims
= (struct in6_msource
*)ims
;
1738 if (lims
->im6sl_st
[0] == MCAST_UNDEFINED
||
1739 lims
->im6sl_st
[0] != imf
->im6f_st
[0])
1741 if (tss
!= NULL
&& nsrcs
> 0) {
1742 psin
= (struct sockaddr_in6
*)ptss
;
1743 psin
->sin6_family
= AF_INET6
;
1744 psin
->sin6_len
= sizeof(struct sockaddr_in6
);
1745 psin
->sin6_addr
= lims
->im6s_addr
;
1746 psin
->sin6_port
= 0;
1756 error
= copyout(tss
, tmp_ptr
, ncsrcs
* sizeof(*tss
));
1762 msfr
.msfr_nsrcs
= ncsrcs
;
1763 if (IS_64BIT_PROCESS(current_proc())) {
1764 msfr64
.msfr_ifindex
= msfr
.msfr_ifindex
;
1765 msfr64
.msfr_fmode
= msfr
.msfr_fmode
;
1766 msfr64
.msfr_nsrcs
= msfr
.msfr_nsrcs
;
1767 memcpy(&msfr64
.msfr_group
, &msfr
.msfr_group
,
1768 sizeof(struct sockaddr_storage
));
1769 error
= sooptcopyout(sopt
, &msfr64
,
1770 sizeof(struct __msfilterreq64
));
1772 msfr32
.msfr_ifindex
= msfr
.msfr_ifindex
;
1773 msfr32
.msfr_fmode
= msfr
.msfr_fmode
;
1774 msfr32
.msfr_nsrcs
= msfr
.msfr_nsrcs
;
1775 memcpy(&msfr32
.msfr_group
, &msfr
.msfr_group
,
1776 sizeof(struct sockaddr_storage
));
1777 error
= sooptcopyout(sopt
, &msfr32
,
1778 sizeof(struct __msfilterreq32
));
1785 * Return the IP multicast options in response to user getsockopt().
1788 ip6_getmoptions(struct inpcb
*inp
, struct sockopt
*sopt
)
1790 struct ip6_moptions
*im6o
;
1794 im6o
= inp
->in6p_moptions
;
1796 * If socket is neither of type SOCK_RAW or SOCK_DGRAM,
1797 * or is a divert socket, reject it.
1799 if (SOCK_PROTO(inp
->inp_socket
) == IPPROTO_DIVERT
||
1800 (SOCK_TYPE(inp
->inp_socket
) != SOCK_RAW
&&
1801 SOCK_TYPE(inp
->inp_socket
) != SOCK_DGRAM
)) {
1802 return (EOPNOTSUPP
);
1806 switch (sopt
->sopt_name
) {
1807 case IPV6_MULTICAST_IF
:
1810 if (im6o
== NULL
|| im6o
->im6o_multicast_ifp
== NULL
) {
1813 optval
= im6o
->im6o_multicast_ifp
->if_index
;
1817 error
= sooptcopyout(sopt
, &optval
, sizeof(u_int
));
1820 case IPV6_MULTICAST_HOPS
:
1822 optval
= ip6_defmcasthlim
;
1825 optval
= im6o
->im6o_multicast_hlim
;
1828 error
= sooptcopyout(sopt
, &optval
, sizeof(u_int
));
1831 case IPV6_MULTICAST_LOOP
:
1833 optval
= in6_mcast_loop
; /* XXX VIMAGE */
1836 optval
= im6o
->im6o_multicast_loop
;
1839 error
= sooptcopyout(sopt
, &optval
, sizeof(u_int
));
1844 error
= EADDRNOTAVAIL
;
1846 error
= in6p_get_source_filters(inp
, sopt
);
1851 error
= ENOPROTOOPT
;
1859 * Look up the ifnet to use for a multicast group membership,
1860 * given the address of an IPv6 group.
1862 * This routine exists to support legacy IPv6 multicast applications.
1864 * If inp is non-NULL and is bound to an interface, use this socket's
1865 * inp_boundif for any required routing table lookup.
1867 * If the route lookup fails, return NULL.
1869 * FUTURE: Support multiple forwarding tables for IPv6.
1871 * Returns NULL if no ifp could be found.
1873 static struct ifnet
*
1874 in6p_lookup_mcast_ifp(const struct inpcb
*in6p
,
1875 const struct sockaddr_in6
*gsin6
)
1877 struct route_in6 ro6
;
1879 unsigned int ifscope
= IFSCOPE_NONE
;
1881 VERIFY(in6p
== NULL
|| (in6p
->inp_vflag
& INP_IPV6
));
1882 VERIFY(gsin6
->sin6_family
== AF_INET6
);
1883 if (IN6_IS_ADDR_MULTICAST(&gsin6
->sin6_addr
) == 0)
1886 if (in6p
!= NULL
&& (in6p
->inp_flags
& INP_BOUND_IF
))
1887 ifscope
= in6p
->inp_boundifp
->if_index
;
1890 memset(&ro6
, 0, sizeof(struct route_in6
));
1891 memcpy(&ro6
.ro_dst
, gsin6
, sizeof(struct sockaddr_in6
));
1892 rtalloc_scoped_ign((struct route
*)&ro6
, 0, ifscope
);
1893 if (ro6
.ro_rt
!= NULL
) {
1894 ifp
= ro6
.ro_rt
->rt_ifp
;
1895 VERIFY(ifp
!= NULL
);
1897 ROUTE_RELEASE(&ro6
);
1903 * Since ipv6_mreq contains an ifindex and ip_mreq contains an AF_INET
1904 * address, we need to lookup the AF_INET address when translating an
1905 * ipv6_mreq structure into an ipmreq structure.
1906 * This is used when userland performs multicast setsockopt() on AF_INET6
1907 * sockets with AF_INET multicast addresses (IPv6 v4 mapped addresses).
1910 in6p_lookup_v4addr(struct ipv6_mreq
*mreq
, struct ip_mreq
*v4mreq
)
1914 struct sockaddr_in
*sin
;
1916 ifnet_head_lock_shared();
1917 if (mreq
->ipv6mr_interface
> (unsigned int)if_index
) {
1919 return (EADDRNOTAVAIL
);
1921 ifp
= ifindex2ifnet
[mreq
->ipv6mr_interface
];
1924 return (EADDRNOTAVAIL
);
1925 ifa
= ifa_ifpgetprimary(ifp
, AF_INET
);
1927 return (EADDRNOTAVAIL
);
1928 sin
= (struct sockaddr_in
*)(uintptr_t)(size_t)ifa
->ifa_addr
;
1929 v4mreq
->imr_interface
.s_addr
= sin
->sin_addr
.s_addr
;
1936 * Join an IPv6 multicast group, possibly with a source.
1938 * FIXME: The KAME use of the unspecified address (::)
1939 * to join *all* multicast groups is currently unsupported.
1942 in6p_join_group(struct inpcb
*inp
, struct sockopt
*sopt
)
1944 struct group_source_req gsr
;
1945 struct sockaddr_in6
*gsa
, *ssa
;
1947 struct in6_mfilter
*imf
;
1948 struct ip6_moptions
*imo
;
1949 struct in6_multi
*inm
= NULL
;
1950 struct in6_msource
*lims
= NULL
;
1953 uint32_t scopeid
= 0;
1954 struct mld_tparams mtp
;
1956 bzero(&mtp
, sizeof (mtp
));
1962 memset(&gsr
, 0, sizeof(struct group_source_req
));
1963 gsa
= (struct sockaddr_in6
*)&gsr
.gsr_group
;
1964 ssa
= (struct sockaddr_in6
*)&gsr
.gsr_source
;
1967 * Chew everything into struct group_source_req.
1968 * Overwrite the port field if present, as the sockaddr
1969 * being copied in may be matched with a binary comparison.
1970 * Ignore passed-in scope ID.
1972 switch (sopt
->sopt_name
) {
1973 case IPV6_JOIN_GROUP
: {
1974 struct ipv6_mreq mreq
;
1976 error
= sooptcopyin(sopt
, &mreq
, sizeof(struct ipv6_mreq
),
1977 sizeof(struct ipv6_mreq
));
1980 if (IN6_IS_ADDR_V4MAPPED(&mreq
.ipv6mr_multiaddr
)) {
1981 struct ip_mreq v4mreq
;
1982 struct sockopt v4sopt
;
1984 v4mreq
.imr_multiaddr
.s_addr
=
1985 mreq
.ipv6mr_multiaddr
.s6_addr32
[3];
1986 if (mreq
.ipv6mr_interface
== 0)
1987 v4mreq
.imr_interface
.s_addr
= INADDR_ANY
;
1989 error
= in6p_lookup_v4addr(&mreq
, &v4mreq
);
1992 v4sopt
.sopt_dir
= SOPT_SET
;
1993 v4sopt
.sopt_level
= sopt
->sopt_level
;
1994 v4sopt
.sopt_name
= IP_ADD_MEMBERSHIP
;
1995 v4sopt
.sopt_val
= CAST_USER_ADDR_T(&v4mreq
);
1996 v4sopt
.sopt_valsize
= sizeof(v4mreq
);
1997 v4sopt
.sopt_p
= kernproc
;
1999 return (inp_join_group(inp
, &v4sopt
));
2001 gsa
->sin6_family
= AF_INET6
;
2002 gsa
->sin6_len
= sizeof(struct sockaddr_in6
);
2003 gsa
->sin6_addr
= mreq
.ipv6mr_multiaddr
;
2005 /* Only allow IPv6 multicast addresses */
2006 if (IN6_IS_ADDR_MULTICAST(&gsa
->sin6_addr
) == 0) {
2010 if (mreq
.ipv6mr_interface
== 0) {
2011 ifp
= in6p_lookup_mcast_ifp(inp
, gsa
);
2013 ifnet_head_lock_shared();
2014 if ((u_int
)if_index
< mreq
.ipv6mr_interface
) {
2016 return (EADDRNOTAVAIL
);
2018 ifp
= ifindex2ifnet
[mreq
.ipv6mr_interface
];
2021 MLD_PRINTF(("%s: ipv6mr_interface = %d, ifp = 0x%llx\n",
2022 __func__
, mreq
.ipv6mr_interface
,
2023 (uint64_t)VM_KERNEL_ADDRPERM(ifp
)));
2027 case MCAST_JOIN_GROUP
:
2028 case MCAST_JOIN_SOURCE_GROUP
:
2029 if (sopt
->sopt_name
== MCAST_JOIN_GROUP
) {
2030 error
= sooptcopyin(sopt
, &gsr
,
2031 sizeof(struct group_req
),
2032 sizeof(struct group_req
));
2033 } else if (sopt
->sopt_name
== MCAST_JOIN_SOURCE_GROUP
) {
2034 error
= sooptcopyin(sopt
, &gsr
,
2035 sizeof(struct group_source_req
),
2036 sizeof(struct group_source_req
));
2041 if (gsa
->sin6_family
!= AF_INET6
||
2042 gsa
->sin6_len
!= sizeof(struct sockaddr_in6
))
2045 if (sopt
->sopt_name
== MCAST_JOIN_SOURCE_GROUP
) {
2046 if (ssa
->sin6_family
!= AF_INET6
||
2047 ssa
->sin6_len
!= sizeof(struct sockaddr_in6
))
2049 if (IN6_IS_ADDR_MULTICAST(&ssa
->sin6_addr
))
2052 * TODO: Validate embedded scope ID in source
2053 * list entry against passed-in ifp, if and only
2054 * if source list filter entry is iface or node local.
2056 in6_clearscope(&ssa
->sin6_addr
);
2058 ssa
->sin6_scope_id
= 0;
2061 ifnet_head_lock_shared();
2062 if (gsr
.gsr_interface
== 0 ||
2063 (u_int
)if_index
< gsr
.gsr_interface
) {
2065 return (EADDRNOTAVAIL
);
2067 ifp
= ifindex2ifnet
[gsr
.gsr_interface
];
2072 MLD_PRINTF(("%s: unknown sopt_name %d\n",
2073 __func__
, sopt
->sopt_name
));
2074 return (EOPNOTSUPP
);
2077 if (!IN6_IS_ADDR_MULTICAST(&gsa
->sin6_addr
))
2080 if (ifp
== NULL
|| (ifp
->if_flags
& IFF_MULTICAST
) == 0)
2081 return (EADDRNOTAVAIL
);
2083 INC_ATOMIC_INT64_LIM(net_api_stats
.nas_socket_mcast_join_total
);
2085 * TBD: revisit the criteria for non-OS initiated joins
2087 if (inp
->inp_lport
== htons(5353)) {
2088 INC_ATOMIC_INT64_LIM(net_api_stats
.nas_socket_mcast_join_os_total
);
2092 gsa
->sin6_scope_id
= 0;
2095 * Always set the scope zone ID on memberships created from userland.
2096 * Use the passed-in ifp to do this.
2098 (void)in6_setscope(&gsa
->sin6_addr
, ifp
, &scopeid
);
2100 * Some addresses are not valid without an embedded scopeid.
2101 * This check must be present because otherwise we will later hit
2102 * a VERIFY() in in6_mc_join().
2104 if ((IN6_IS_ADDR_MC_LINKLOCAL(&gsa
->sin6_addr
) ||
2105 IN6_IS_ADDR_MC_INTFACELOCAL(&gsa
->sin6_addr
)) &&
2106 (scopeid
== 0 || gsa
->sin6_addr
.s6_addr16
[1] == 0))
2109 imo
= in6p_findmoptions(inp
);
2114 idx
= im6o_match_group(imo
, ifp
, gsa
);
2115 if (idx
== (size_t)-1) {
2118 inm
= imo
->im6o_membership
[idx
];
2119 imf
= &imo
->im6o_mfilters
[idx
];
2120 if (ssa
->sin6_family
!= AF_UNSPEC
) {
2122 * MCAST_JOIN_SOURCE_GROUP on an exclusive membership
2123 * is an error. On an existing inclusive membership,
2124 * it just adds the source to the filter list.
2126 if (imf
->im6f_st
[1] != MCAST_INCLUDE
) {
2128 goto out_imo_locked
;
2131 * Throw out duplicates.
2133 * XXX FIXME: This makes a naive assumption that
2134 * even if entries exist for *ssa in this imf,
2135 * they will be rejected as dupes, even if they
2136 * are not valid in the current mode (in-mode).
2138 * in6_msource is transactioned just as for anything
2139 * else in SSM -- but note naive use of in6m_graft()
2140 * below for allocating new filter entries.
2142 * This is only an issue if someone mixes the
2143 * full-state SSM API with the delta-based API,
2144 * which is discouraged in the relevant RFCs.
2146 lims
= im6o_match_source(imo
, idx
, ssa
);
2147 if (lims
!= NULL
/*&&
2148 lims->im6sl_st[1] == MCAST_INCLUDE*/) {
2149 error
= EADDRNOTAVAIL
;
2150 goto out_imo_locked
;
2154 * MCAST_JOIN_GROUP on an existing exclusive
2155 * membership is an error; return EADDRINUSE
2156 * to preserve 4.4BSD API idempotence, and
2157 * avoid tedious detour to code below.
2158 * NOTE: This is bending RFC 3678 a bit.
2160 * On an existing inclusive membership, this is also
2161 * an error; if you want to change filter mode,
2162 * you must use the userland API setsourcefilter().
2163 * XXX We don't reject this for imf in UNDEFINED
2164 * state at t1, because allocation of a filter
2165 * is atomic with allocation of a membership.
2168 /* See comments above for EADDRINUSE */
2169 if (imf
->im6f_st
[1] == MCAST_EXCLUDE
)
2171 goto out_imo_locked
;
2176 * Begin state merge transaction at socket layer.
2180 if (imo
->im6o_num_memberships
== imo
->im6o_max_memberships
) {
2181 error
= im6o_grow(imo
, 0);
2183 goto out_imo_locked
;
2186 * Allocate the new slot upfront so we can deal with
2187 * grafting the new source filter in same code path
2188 * as for join-source on existing membership.
2190 idx
= imo
->im6o_num_memberships
;
2191 imo
->im6o_membership
[idx
] = NULL
;
2192 imo
->im6o_num_memberships
++;
2193 VERIFY(imo
->im6o_mfilters
!= NULL
);
2194 imf
= &imo
->im6o_mfilters
[idx
];
2195 VERIFY(RB_EMPTY(&imf
->im6f_sources
));
2199 * Graft new source into filter list for this inpcb's
2200 * membership of the group. The in6_multi may not have
2201 * been allocated yet if this is a new membership, however,
2202 * the in_mfilter slot will be allocated and must be initialized.
2204 * Note: Grafting of exclusive mode filters doesn't happen
2206 * XXX: Should check for non-NULL lims (node exists but may
2207 * not be in-mode) for interop with full-state API.
2209 if (ssa
->sin6_family
!= AF_UNSPEC
) {
2210 /* Membership starts in IN mode */
2212 MLD_PRINTF(("%s: new join w/source\n", __func__
);
2213 im6f_init(imf
, MCAST_UNDEFINED
, MCAST_INCLUDE
));
2215 MLD_PRINTF(("%s: %s source\n", __func__
, "allow"));
2217 lims
= im6f_graft(imf
, MCAST_INCLUDE
, ssa
);
2219 MLD_PRINTF(("%s: merge imf state failed\n",
2225 /* No address specified; Membership starts in EX mode */
2227 MLD_PRINTF(("%s: new join w/o source", __func__
));
2228 im6f_init(imf
, MCAST_UNDEFINED
, MCAST_EXCLUDE
);
2233 * Begin state merge transaction at MLD layer.
2238 * See inp_join_group() for why we need to unlock
2240 IM6O_ADDREF_LOCKED(imo
);
2242 socket_unlock(inp
->inp_socket
, 0);
2244 VERIFY(inm
== NULL
);
2245 error
= in6_mc_join(ifp
, &gsa
->sin6_addr
, imf
, &inm
, 0);
2246 VERIFY(inm
!= NULL
|| error
!= 0);
2248 socket_lock(inp
->inp_socket
, 0);
2254 imo
->im6o_membership
[idx
] = inm
; /* from in6_mc_join() */
2256 MLD_PRINTF(("%s: merge inm state\n", __func__
));
2258 error
= in6m_merge(inm
, imf
);
2260 MLD_PRINTF(("%s: failed to merge inm state\n",
2263 goto out_im6f_rollback
;
2265 MLD_PRINTF(("%s: doing mld downcall\n", __func__
));
2266 error
= mld_change_state(inm
, &mtp
, 0);
2269 MLD_PRINTF(("%s: failed mld downcall\n",
2271 goto out_im6f_rollback
;
2287 if (error
&& is_new
) {
2288 VERIFY(inm
== NULL
);
2289 imo
->im6o_membership
[idx
] = NULL
;
2290 --imo
->im6o_num_memberships
;
2295 IM6O_REMREF(imo
); /* from in6p_findmoptions() */
2297 /* schedule timer now that we've dropped the lock(s) */
2298 mld_set_timeout(&mtp
);
2304 * Leave an IPv6 multicast group on an inpcb, possibly with a source.
2307 in6p_leave_group(struct inpcb
*inp
, struct sockopt
*sopt
)
2309 struct ipv6_mreq mreq
;
2310 struct group_source_req gsr
;
2311 struct sockaddr_in6
*gsa
, *ssa
;
2313 struct in6_mfilter
*imf
;
2314 struct ip6_moptions
*imo
;
2315 struct in6_msource
*ims
;
2316 struct in6_multi
*inm
= NULL
;
2317 uint32_t ifindex
= 0;
2319 int error
, is_final
;
2320 struct mld_tparams mtp
;
2322 bzero(&mtp
, sizeof (mtp
));
2327 memset(&gsr
, 0, sizeof(struct group_source_req
));
2328 gsa
= (struct sockaddr_in6
*)&gsr
.gsr_group
;
2329 ssa
= (struct sockaddr_in6
*)&gsr
.gsr_source
;
2332 * Chew everything passed in up into a struct group_source_req
2333 * as that is easier to process.
2334 * Note: Any embedded scope ID in the multicast group passed
2335 * in by userland is ignored, the interface index is the recommended
2336 * mechanism to specify an interface; see below.
2338 switch (sopt
->sopt_name
) {
2339 case IPV6_LEAVE_GROUP
: {
2341 error
= sooptcopyin(sopt
, &mreq
, sizeof(struct ipv6_mreq
),
2342 sizeof(struct ipv6_mreq
));
2345 if (IN6_IS_ADDR_V4MAPPED(&mreq
.ipv6mr_multiaddr
)) {
2346 struct ip_mreq v4mreq
;
2347 struct sockopt v4sopt
;
2349 v4mreq
.imr_multiaddr
.s_addr
=
2350 mreq
.ipv6mr_multiaddr
.s6_addr32
[3];
2351 if (mreq
.ipv6mr_interface
== 0)
2352 v4mreq
.imr_interface
.s_addr
= INADDR_ANY
;
2354 error
= in6p_lookup_v4addr(&mreq
, &v4mreq
);
2357 v4sopt
.sopt_dir
= SOPT_SET
;
2358 v4sopt
.sopt_level
= sopt
->sopt_level
;
2359 v4sopt
.sopt_name
= IP_DROP_MEMBERSHIP
;
2360 v4sopt
.sopt_val
= CAST_USER_ADDR_T(&v4mreq
);
2361 v4sopt
.sopt_valsize
= sizeof(v4mreq
);
2362 v4sopt
.sopt_p
= kernproc
;
2364 return (inp_leave_group(inp
, &v4sopt
));
2366 gsa
->sin6_family
= AF_INET6
;
2367 gsa
->sin6_len
= sizeof(struct sockaddr_in6
);
2368 gsa
->sin6_addr
= mreq
.ipv6mr_multiaddr
;
2370 gsa
->sin6_scope_id
= 0;
2371 ifindex
= mreq
.ipv6mr_interface
;
2372 /* Only allow IPv6 multicast addresses */
2373 if (IN6_IS_ADDR_MULTICAST(&gsa
->sin6_addr
) == 0) {
2379 case MCAST_LEAVE_GROUP
:
2380 case MCAST_LEAVE_SOURCE_GROUP
:
2381 if (sopt
->sopt_name
== MCAST_LEAVE_GROUP
) {
2382 error
= sooptcopyin(sopt
, &gsr
,
2383 sizeof(struct group_req
),
2384 sizeof(struct group_req
));
2385 } else if (sopt
->sopt_name
== MCAST_LEAVE_SOURCE_GROUP
) {
2386 error
= sooptcopyin(sopt
, &gsr
,
2387 sizeof(struct group_source_req
),
2388 sizeof(struct group_source_req
));
2393 if (gsa
->sin6_family
!= AF_INET6
||
2394 gsa
->sin6_len
!= sizeof(struct sockaddr_in6
))
2396 if (sopt
->sopt_name
== MCAST_LEAVE_SOURCE_GROUP
) {
2397 if (ssa
->sin6_family
!= AF_INET6
||
2398 ssa
->sin6_len
!= sizeof(struct sockaddr_in6
))
2400 if (IN6_IS_ADDR_MULTICAST(&ssa
->sin6_addr
))
2403 * TODO: Validate embedded scope ID in source
2404 * list entry against passed-in ifp, if and only
2405 * if source list filter entry is iface or node local.
2407 in6_clearscope(&ssa
->sin6_addr
);
2410 gsa
->sin6_scope_id
= 0;
2411 ifindex
= gsr
.gsr_interface
;
2415 MLD_PRINTF(("%s: unknown sopt_name %d\n",
2416 __func__
, sopt
->sopt_name
));
2417 return (EOPNOTSUPP
);
2420 if (!IN6_IS_ADDR_MULTICAST(&gsa
->sin6_addr
))
2424 * Validate interface index if provided. If no interface index
2425 * was provided separately, attempt to look the membership up
2426 * from the default scope as a last resort to disambiguate
2427 * the membership we are being asked to leave.
2428 * XXX SCOPE6 lock potentially taken here.
2431 ifnet_head_lock_shared();
2432 if ((u_int
)if_index
< ifindex
) {
2434 return (EADDRNOTAVAIL
);
2436 ifp
= ifindex2ifnet
[ifindex
];
2439 return (EADDRNOTAVAIL
);
2440 (void) in6_setscope(&gsa
->sin6_addr
, ifp
, NULL
);
2442 error
= sa6_embedscope(gsa
, ip6_use_defzone
);
2444 return (EADDRNOTAVAIL
);
2446 * Some badly behaved applications don't pass an ifindex
2447 * or a scope ID, which is an API violation. In this case,
2448 * perform a lookup as per a v6 join.
2450 * XXX For now, stomp on zone ID for the corner case.
2451 * This is not the 'KAME way', but we need to see the ifp
2452 * directly until such time as this implementation is
2453 * refactored, assuming the scope IDs are the way to go.
2455 ifindex
= ntohs(gsa
->sin6_addr
.s6_addr16
[1]);
2457 MLD_PRINTF(("%s: warning: no ifindex, looking up "
2458 "ifp for group %s.\n", __func__
,
2459 ip6_sprintf(&gsa
->sin6_addr
)));
2460 ifp
= in6p_lookup_mcast_ifp(inp
, gsa
);
2462 if (!IF_INDEX_IN_RANGE(ifindex
))
2463 return (EADDRNOTAVAIL
);
2464 ifnet_head_lock_shared();
2465 ifp
= ifindex2ifnet
[ifindex
];
2469 return (EADDRNOTAVAIL
);
2472 VERIFY(ifp
!= NULL
);
2473 MLD_PRINTF(("%s: ifp = 0x%llx\n", __func__
,
2474 (uint64_t)VM_KERNEL_ADDRPERM(ifp
)));
2477 * Find the membership in the membership array.
2479 imo
= in6p_findmoptions(inp
);
2484 idx
= im6o_match_group(imo
, ifp
, gsa
);
2485 if (idx
== (size_t)-1) {
2486 error
= EADDRNOTAVAIL
;
2489 inm
= imo
->im6o_membership
[idx
];
2490 imf
= &imo
->im6o_mfilters
[idx
];
2492 if (ssa
->sin6_family
!= AF_UNSPEC
)
2496 * Begin state merge transaction at socket layer.
2500 * If we were instructed only to leave a given source, do so.
2501 * MCAST_LEAVE_SOURCE_GROUP is only valid for inclusive memberships.
2506 if (imf
->im6f_st
[0] == MCAST_EXCLUDE
) {
2507 error
= EADDRNOTAVAIL
;
2510 ims
= im6o_match_source(imo
, idx
, ssa
);
2512 MLD_PRINTF(("%s: source %s %spresent\n", __func__
,
2513 ip6_sprintf(&ssa
->sin6_addr
),
2515 error
= EADDRNOTAVAIL
;
2518 MLD_PRINTF(("%s: %s source\n", __func__
, "block"));
2519 error
= im6f_prune(imf
, ssa
);
2521 MLD_PRINTF(("%s: merge imf state failed\n",
2528 * Begin state merge transaction at MLD layer.
2533 * Give up the multicast address record to which
2534 * the membership points. Reference held in im6o
2535 * will be released below.
2537 (void) in6_mc_leave(inm
, imf
);
2539 MLD_PRINTF(("%s: merge inm state\n", __func__
));
2541 error
= in6m_merge(inm
, imf
);
2543 MLD_PRINTF(("%s: failed to merge inm state\n",
2546 goto out_im6f_rollback
;
2549 MLD_PRINTF(("%s: doing mld downcall\n", __func__
));
2550 error
= mld_change_state(inm
, &mtp
, 0);
2552 MLD_PRINTF(("%s: failed mld downcall\n", __func__
));
2566 /* Remove the gap in the membership array. */
2567 VERIFY(inm
== imo
->im6o_membership
[idx
]);
2568 imo
->im6o_membership
[idx
] = NULL
;
2571 * See inp_join_group() for why we need to unlock
2573 IM6O_ADDREF_LOCKED(imo
);
2575 socket_unlock(inp
->inp_socket
, 0);
2579 socket_lock(inp
->inp_socket
, 0);
2583 for (++idx
; idx
< imo
->im6o_num_memberships
; ++idx
) {
2584 imo
->im6o_membership
[idx
-1] = imo
->im6o_membership
[idx
];
2585 imo
->im6o_mfilters
[idx
-1] = imo
->im6o_mfilters
[idx
];
2587 imo
->im6o_num_memberships
--;
2592 IM6O_REMREF(imo
); /* from in6p_findmoptions() */
2594 /* schedule timer now that we've dropped the lock(s) */
2595 mld_set_timeout(&mtp
);
2601 * Select the interface for transmitting IPv6 multicast datagrams.
2603 * Either an instance of struct in6_addr or an instance of struct ipv6_mreqn
2604 * may be passed to this socket option. An address of in6addr_any or an
2605 * interface index of 0 is used to remove a previous selection.
2606 * When no interface is selected, one is chosen for every send.
2609 in6p_set_multicast_if(struct inpcb
*inp
, struct sockopt
*sopt
)
2612 struct ip6_moptions
*imo
;
2616 if (sopt
->sopt_valsize
!= sizeof(u_int
))
2619 error
= sooptcopyin(sopt
, &ifindex
, sizeof(u_int
), sizeof(u_int
));
2623 ifnet_head_lock_shared();
2624 if ((u_int
)if_index
< ifindex
) {
2629 ifp
= ifindex2ifnet
[ifindex
];
2631 if (ifp
== NULL
|| (ifp
->if_flags
& IFF_MULTICAST
) == 0)
2632 return (EADDRNOTAVAIL
);
2634 imo
= in6p_findmoptions(inp
);
2639 imo
->im6o_multicast_ifp
= ifp
;
2641 IM6O_REMREF(imo
); /* from in6p_findmoptions() */
2647 * Atomically set source filters on a socket for an IPv6 multicast group.
2651 in6p_set_source_filters(struct inpcb
*inp
, struct sockopt
*sopt
)
2653 struct __msfilterreq64 msfr
, msfr64
;
2654 struct __msfilterreq32 msfr32
;
2655 struct sockaddr_in6
*gsa
;
2657 struct in6_mfilter
*imf
;
2658 struct ip6_moptions
*imo
;
2659 struct in6_multi
*inm
;
2662 user_addr_t tmp_ptr
;
2663 struct mld_tparams mtp
;
2665 bzero(&mtp
, sizeof (mtp
));
2667 if (IS_64BIT_PROCESS(current_proc())) {
2668 error
= sooptcopyin(sopt
, &msfr64
,
2669 sizeof(struct __msfilterreq64
),
2670 sizeof(struct __msfilterreq64
));
2673 /* we never use msfr.msfr_srcs; */
2674 memcpy(&msfr
, &msfr64
, sizeof(msfr
));
2676 error
= sooptcopyin(sopt
, &msfr32
,
2677 sizeof(struct __msfilterreq32
),
2678 sizeof(struct __msfilterreq32
));
2681 /* we never use msfr.msfr_srcs; */
2682 memcpy(&msfr
, &msfr32
, sizeof(msfr
));
2685 if ((size_t) msfr
.msfr_nsrcs
>
2686 UINT32_MAX
/ sizeof(struct sockaddr_storage
))
2687 msfr
.msfr_nsrcs
= UINT32_MAX
/ sizeof(struct sockaddr_storage
);
2689 if (msfr
.msfr_nsrcs
> in6_mcast_maxsocksrc
)
2692 if (msfr
.msfr_fmode
!= MCAST_EXCLUDE
&&
2693 msfr
.msfr_fmode
!= MCAST_INCLUDE
)
2696 if (msfr
.msfr_group
.ss_family
!= AF_INET6
||
2697 msfr
.msfr_group
.ss_len
!= sizeof(struct sockaddr_in6
))
2700 gsa
= (struct sockaddr_in6
*)&msfr
.msfr_group
;
2701 if (!IN6_IS_ADDR_MULTICAST(&gsa
->sin6_addr
))
2704 gsa
->sin6_port
= 0; /* ignore port */
2706 ifnet_head_lock_shared();
2707 if (msfr
.msfr_ifindex
== 0 || (u_int
)if_index
< msfr
.msfr_ifindex
) {
2709 return (EADDRNOTAVAIL
);
2711 ifp
= ifindex2ifnet
[msfr
.msfr_ifindex
];
2714 return (EADDRNOTAVAIL
);
2716 (void)in6_setscope(&gsa
->sin6_addr
, ifp
, NULL
);
2719 * Take the INP write lock.
2720 * Check if this socket is a member of this group.
2722 imo
= in6p_findmoptions(inp
);
2727 idx
= im6o_match_group(imo
, ifp
, gsa
);
2728 if (idx
== (size_t)-1 || imo
->im6o_mfilters
== NULL
) {
2729 error
= EADDRNOTAVAIL
;
2730 goto out_imo_locked
;
2732 inm
= imo
->im6o_membership
[idx
];
2733 imf
= &imo
->im6o_mfilters
[idx
];
2736 * Begin state merge transaction at socket layer.
2739 imf
->im6f_st
[1] = msfr
.msfr_fmode
;
2742 * Apply any new source filters, if present.
2743 * Make a copy of the user-space source vector so
2744 * that we may copy them with a single copyin. This
2745 * allows us to deal with page faults up-front.
2747 if (msfr
.msfr_nsrcs
> 0) {
2748 struct in6_msource
*lims
;
2749 struct sockaddr_in6
*psin
;
2750 struct sockaddr_storage
*kss
, *pkss
;
2753 if (IS_64BIT_PROCESS(current_proc()))
2754 tmp_ptr
= msfr64
.msfr_srcs
;
2756 tmp_ptr
= CAST_USER_ADDR_T(msfr32
.msfr_srcs
);
2758 MLD_PRINTF(("%s: loading %lu source list entries\n",
2759 __func__
, (unsigned long)msfr
.msfr_nsrcs
));
2760 kss
= _MALLOC((size_t) msfr
.msfr_nsrcs
* sizeof(*kss
),
2764 goto out_imo_locked
;
2767 error
= copyin(tmp_ptr
, kss
,
2768 (size_t) msfr
.msfr_nsrcs
* sizeof(*kss
));
2771 goto out_imo_locked
;
2775 * Mark all source filters as UNDEFINED at t1.
2776 * Restore new group filter mode, as im6f_leave()
2777 * will set it to INCLUDE.
2780 imf
->im6f_st
[1] = msfr
.msfr_fmode
;
2783 * Update socket layer filters at t1, lazy-allocating
2784 * new entries. This saves a bunch of memory at the
2785 * cost of one RB_FIND() per source entry; duplicate
2786 * entries in the msfr_nsrcs vector are ignored.
2787 * If we encounter an error, rollback transaction.
2789 * XXX This too could be replaced with a set-symmetric
2790 * difference like loop to avoid walking from root
2791 * every time, as the key space is common.
2793 for (i
= 0, pkss
= kss
; i
< msfr
.msfr_nsrcs
; i
++, pkss
++) {
2794 psin
= (struct sockaddr_in6
*)pkss
;
2795 if (psin
->sin6_family
!= AF_INET6
) {
2796 error
= EAFNOSUPPORT
;
2799 if (psin
->sin6_len
!= sizeof(struct sockaddr_in6
)) {
2803 if (IN6_IS_ADDR_MULTICAST(&psin
->sin6_addr
)) {
2808 * TODO: Validate embedded scope ID in source
2809 * list entry against passed-in ifp, if and only
2810 * if source list filter entry is iface or node local.
2812 in6_clearscope(&psin
->sin6_addr
);
2813 error
= im6f_get_source(imf
, psin
, &lims
);
2816 lims
->im6sl_st
[1] = imf
->im6f_st
[1];
2822 goto out_im6f_rollback
;
2825 * Begin state merge transaction at MLD layer.
2828 MLD_PRINTF(("%s: merge inm state\n", __func__
));
2829 error
= in6m_merge(inm
, imf
);
2831 MLD_PRINTF(("%s: failed to merge inm state\n", __func__
));
2833 goto out_im6f_rollback
;
2836 MLD_PRINTF(("%s: doing mld downcall\n", __func__
));
2837 error
= mld_change_state(inm
, &mtp
, 0);
2841 MLD_PRINTF(("%s: failed mld downcall\n", __func__
));
2854 IM6O_REMREF(imo
); /* from in6p_findmoptions() */
2856 /* schedule timer now that we've dropped the lock(s) */
2857 mld_set_timeout(&mtp
);
2863 * Set the IP multicast options in response to user setsockopt().
2865 * Many of the socket options handled in this function duplicate the
2866 * functionality of socket options in the regular unicast API. However,
2867 * it is not possible to merge the duplicate code, because the idempotence
2868 * of the IPv6 multicast part of the BSD Sockets API must be preserved;
2869 * the effects of these options must be treated as separate and distinct.
2873 ip6_setmoptions(struct inpcb
*inp
, struct sockopt
*sopt
)
2875 struct ip6_moptions
*im6o
;
2881 * If socket is neither of type SOCK_RAW or SOCK_DGRAM,
2882 * or is a divert socket, reject it.
2884 if (SOCK_PROTO(inp
->inp_socket
) == IPPROTO_DIVERT
||
2885 (SOCK_TYPE(inp
->inp_socket
) != SOCK_RAW
&&
2886 SOCK_TYPE(inp
->inp_socket
) != SOCK_DGRAM
))
2887 return (EOPNOTSUPP
);
2889 switch (sopt
->sopt_name
) {
2890 case IPV6_MULTICAST_IF
:
2891 error
= in6p_set_multicast_if(inp
, sopt
);
2894 case IPV6_MULTICAST_HOPS
: {
2897 if (sopt
->sopt_valsize
!= sizeof(int)) {
2901 error
= sooptcopyin(sopt
, &hlim
, sizeof(hlim
), sizeof(int));
2904 if (hlim
< -1 || hlim
> 255) {
2907 } else if (hlim
== -1) {
2908 hlim
= ip6_defmcasthlim
;
2910 im6o
= in6p_findmoptions(inp
);
2916 im6o
->im6o_multicast_hlim
= hlim
;
2918 IM6O_REMREF(im6o
); /* from in6p_findmoptions() */
2922 case IPV6_MULTICAST_LOOP
: {
2926 * Set the loopback flag for outgoing multicast packets.
2927 * Must be zero or one.
2929 if (sopt
->sopt_valsize
!= sizeof(u_int
)) {
2933 error
= sooptcopyin(sopt
, &loop
, sizeof(u_int
), sizeof(u_int
));
2940 im6o
= in6p_findmoptions(inp
);
2946 im6o
->im6o_multicast_loop
= loop
;
2948 IM6O_REMREF(im6o
); /* from in6p_findmoptions() */
2952 case IPV6_JOIN_GROUP
:
2953 case MCAST_JOIN_GROUP
:
2954 case MCAST_JOIN_SOURCE_GROUP
:
2955 error
= in6p_join_group(inp
, sopt
);
2958 case IPV6_LEAVE_GROUP
:
2959 case MCAST_LEAVE_GROUP
:
2960 case MCAST_LEAVE_SOURCE_GROUP
:
2961 error
= in6p_leave_group(inp
, sopt
);
2964 case MCAST_BLOCK_SOURCE
:
2965 case MCAST_UNBLOCK_SOURCE
:
2966 error
= in6p_block_unblock_source(inp
, sopt
);
2970 error
= in6p_set_source_filters(inp
, sopt
);
2981 * Expose MLD's multicast filter mode and source list(s) to userland,
2982 * keyed by (ifindex, group).
2983 * The filter mode is written out as a uint32_t, followed by
2984 * 0..n of struct in6_addr.
2985 * For use by ifmcstat(8).
2988 sysctl_ip6_mcast_filters SYSCTL_HANDLER_ARGS
2990 #pragma unused(oidp)
2992 struct in6_addr mcaddr
;
2993 struct in6_addr src
;
2995 struct in6_multi
*inm
;
2996 struct in6_multistep step
;
2997 struct ip6_msource
*ims
;
3001 uint32_t fmode
, ifindex
;
3006 if (req
->newptr
!= USER_ADDR_NULL
)
3009 /* int: ifindex + 4 * 32 bits of IPv6 address */
3014 ifnet_head_lock_shared();
3015 if (ifindex
<= 0 || ifindex
> (u_int
)if_index
) {
3016 MLD_PRINTF(("%s: ifindex %u out of range\n",
3017 __func__
, ifindex
));
3022 memcpy(&mcaddr
, &name
[1], sizeof(struct in6_addr
));
3023 if (!IN6_IS_ADDR_MULTICAST(&mcaddr
)) {
3024 MLD_PRINTF(("%s: group %s is not multicast\n",
3025 __func__
, ip6_sprintf(&mcaddr
)));
3030 ifp
= ifindex2ifnet
[ifindex
];
3033 MLD_PRINTF(("%s: no ifp for ifindex %u\n", __func__
, ifindex
));
3037 * Internal MLD lookups require that scope/zone ID is set.
3039 (void)in6_setscope(&mcaddr
, ifp
, NULL
);
3041 in6_multihead_lock_shared();
3042 IN6_FIRST_MULTI(step
, inm
);
3043 while (inm
!= NULL
) {
3045 if (inm
->in6m_ifp
!= ifp
)
3048 if (!IN6_ARE_ADDR_EQUAL(&inm
->in6m_addr
, &mcaddr
))
3051 fmode
= inm
->in6m_st
[1].iss_fmode
;
3052 retval
= SYSCTL_OUT(req
, &fmode
, sizeof(uint32_t));
3057 RB_FOREACH(ims
, ip6_msource_tree
, &inm
->in6m_srcs
) {
3058 MLD_PRINTF(("%s: visit node 0x%llx\n", __func__
,
3059 (uint64_t)VM_KERNEL_ADDRPERM(ims
)));
3061 * Only copy-out sources which are in-mode.
3063 if (fmode
!= im6s_get_mode(inm
, ims
, 1)) {
3064 MLD_PRINTF(("%s: skip non-in-mode\n",
3066 continue; /* process next source */
3068 src
= ims
->im6s_addr
;
3069 retval
= SYSCTL_OUT(req
, &src
, sizeof(struct in6_addr
));
3071 break; /* process next inm */
3075 IN6_NEXT_MULTI(step
, inm
);
3077 in6_multihead_lock_done();
3083 in6_multi_init(void)
3085 PE_parse_boot_argn("ifa_debug", &in6m_debug
, sizeof (in6m_debug
));
3087 /* Setup lock group and attribute for in6_multihead */
3088 in6_multihead_lock_grp_attr
= lck_grp_attr_alloc_init();
3089 in6_multihead_lock_grp
= lck_grp_alloc_init("in6_multihead",
3090 in6_multihead_lock_grp_attr
);
3091 in6_multihead_lock_attr
= lck_attr_alloc_init();
3092 lck_rw_init(&in6_multihead_lock
, in6_multihead_lock_grp
,
3093 in6_multihead_lock_attr
);
3095 lck_mtx_init(&in6m_trash_lock
, in6_multihead_lock_grp
,
3096 in6_multihead_lock_attr
);
3097 TAILQ_INIT(&in6m_trash_head
);
3099 in6m_size
= (in6m_debug
== 0) ? sizeof (struct in6_multi
) :
3100 sizeof (struct in6_multi_dbg
);
3101 in6m_zone
= zinit(in6m_size
, IN6M_ZONE_MAX
* in6m_size
,
3103 if (in6m_zone
== NULL
) {
3104 panic("%s: failed allocating %s", __func__
, IN6M_ZONE_NAME
);
3107 zone_change(in6m_zone
, Z_EXPAND
, TRUE
);
3109 imm_size
= sizeof (struct in6_multi_mship
);
3110 imm_zone
= zinit(imm_size
, IMM_ZONE_MAX
* imm_size
, 0, IMM_ZONE_NAME
);
3111 if (imm_zone
== NULL
) {
3112 panic("%s: failed allocating %s", __func__
, IMM_ZONE_NAME
);
3115 zone_change(imm_zone
, Z_EXPAND
, TRUE
);
3117 ip6ms_size
= sizeof (struct ip6_msource
);
3118 ip6ms_zone
= zinit(ip6ms_size
, IP6MS_ZONE_MAX
* ip6ms_size
,
3119 0, IP6MS_ZONE_NAME
);
3120 if (ip6ms_zone
== NULL
) {
3121 panic("%s: failed allocating %s", __func__
, IP6MS_ZONE_NAME
);
3124 zone_change(ip6ms_zone
, Z_EXPAND
, TRUE
);
3126 in6ms_size
= sizeof (struct in6_msource
);
3127 in6ms_zone
= zinit(in6ms_size
, IN6MS_ZONE_MAX
* in6ms_size
,
3128 0, IN6MS_ZONE_NAME
);
3129 if (in6ms_zone
== NULL
) {
3130 panic("%s: failed allocating %s", __func__
, IN6MS_ZONE_NAME
);
3133 zone_change(in6ms_zone
, Z_EXPAND
, TRUE
);
3136 static struct in6_multi
*
3137 in6_multi_alloc(int how
)
3139 struct in6_multi
*in6m
;
3141 in6m
= (how
== M_WAITOK
) ? zalloc(in6m_zone
) :
3142 zalloc_noblock(in6m_zone
);
3144 bzero(in6m
, in6m_size
);
3145 lck_mtx_init(&in6m
->in6m_lock
, in6_multihead_lock_grp
,
3146 in6_multihead_lock_attr
);
3147 in6m
->in6m_debug
|= IFD_ALLOC
;
3148 if (in6m_debug
!= 0) {
3149 in6m
->in6m_debug
|= IFD_DEBUG
;
3150 in6m
->in6m_trace
= in6m_trace
;
3157 in6_multi_free(struct in6_multi
*in6m
)
3160 if (in6m
->in6m_debug
& IFD_ATTACHED
) {
3161 panic("%s: attached in6m=%p is being freed", __func__
, in6m
);
3163 } else if (in6m
->in6m_ifma
!= NULL
) {
3164 panic("%s: ifma not NULL for in6m=%p", __func__
, in6m
);
3166 } else if (!(in6m
->in6m_debug
& IFD_ALLOC
)) {
3167 panic("%s: in6m %p cannot be freed", __func__
, in6m
);
3169 } else if (in6m
->in6m_refcount
!= 0) {
3170 panic("%s: non-zero refcount in6m=%p", __func__
, in6m
);
3172 } else if (in6m
->in6m_reqcnt
!= 0) {
3173 panic("%s: non-zero reqcnt in6m=%p", __func__
, in6m
);
3177 /* Free any pending MLDv2 state-change records */
3178 IF_DRAIN(&in6m
->in6m_scq
);
3180 in6m
->in6m_debug
&= ~IFD_ALLOC
;
3181 if ((in6m
->in6m_debug
& (IFD_DEBUG
| IFD_TRASHED
)) ==
3182 (IFD_DEBUG
| IFD_TRASHED
)) {
3183 lck_mtx_lock(&in6m_trash_lock
);
3184 TAILQ_REMOVE(&in6m_trash_head
, (struct in6_multi_dbg
*)in6m
,
3186 lck_mtx_unlock(&in6m_trash_lock
);
3187 in6m
->in6m_debug
&= ~IFD_TRASHED
;
3191 lck_mtx_destroy(&in6m
->in6m_lock
, in6_multihead_lock_grp
);
3192 zfree(in6m_zone
, in6m
);
3196 in6_multi_attach(struct in6_multi
*in6m
)
3198 in6_multihead_lock_assert(LCK_RW_ASSERT_EXCLUSIVE
);
3199 IN6M_LOCK_ASSERT_HELD(in6m
);
3201 if (in6m
->in6m_debug
& IFD_ATTACHED
) {
3202 panic("%s: Attempt to attach an already attached in6m=%p",
3207 in6m
->in6m_reqcnt
++;
3208 VERIFY(in6m
->in6m_reqcnt
== 1);
3209 IN6M_ADDREF_LOCKED(in6m
);
3210 in6m
->in6m_debug
|= IFD_ATTACHED
;
3212 * Reattach case: If debugging is enabled, take it
3213 * out of the trash list and clear IFD_TRASHED.
3215 if ((in6m
->in6m_debug
& (IFD_DEBUG
| IFD_TRASHED
)) ==
3216 (IFD_DEBUG
| IFD_TRASHED
)) {
3217 /* Become a regular mutex, just in case */
3218 IN6M_CONVERT_LOCK(in6m
);
3219 lck_mtx_lock(&in6m_trash_lock
);
3220 TAILQ_REMOVE(&in6m_trash_head
, (struct in6_multi_dbg
*)in6m
,
3222 lck_mtx_unlock(&in6m_trash_lock
);
3223 in6m
->in6m_debug
&= ~IFD_TRASHED
;
3226 LIST_INSERT_HEAD(&in6_multihead
, in6m
, in6m_entry
);
3230 in6_multi_detach(struct in6_multi
*in6m
)
3232 in6_multihead_lock_assert(LCK_RW_ASSERT_EXCLUSIVE
);
3233 IN6M_LOCK_ASSERT_HELD(in6m
);
3235 if (in6m
->in6m_reqcnt
== 0) {
3236 panic("%s: in6m=%p negative reqcnt", __func__
, in6m
);
3240 --in6m
->in6m_reqcnt
;
3241 if (in6m
->in6m_reqcnt
> 0)
3244 if (!(in6m
->in6m_debug
& IFD_ATTACHED
)) {
3245 panic("%s: Attempt to detach an unattached record in6m=%p",
3248 } else if (in6m
->in6m_debug
& IFD_TRASHED
) {
3249 panic("%s: in6m %p is already in trash list", __func__
, in6m
);
3254 * NOTE: Caller calls IFMA_REMREF
3256 in6m
->in6m_debug
&= ~IFD_ATTACHED
;
3257 LIST_REMOVE(in6m
, in6m_entry
);
3259 if (in6m
->in6m_debug
& IFD_DEBUG
) {
3260 /* Become a regular mutex, just in case */
3261 IN6M_CONVERT_LOCK(in6m
);
3262 lck_mtx_lock(&in6m_trash_lock
);
3263 TAILQ_INSERT_TAIL(&in6m_trash_head
,
3264 (struct in6_multi_dbg
*)in6m
, in6m_trash_link
);
3265 lck_mtx_unlock(&in6m_trash_lock
);
3266 in6m
->in6m_debug
|= IFD_TRASHED
;
3273 in6m_addref(struct in6_multi
*in6m
, int locked
)
3276 IN6M_LOCK_SPIN(in6m
);
3278 IN6M_LOCK_ASSERT_HELD(in6m
);
3280 if (++in6m
->in6m_refcount
== 0) {
3281 panic("%s: in6m=%p wraparound refcnt", __func__
, in6m
);
3283 } else if (in6m
->in6m_trace
!= NULL
) {
3284 (*in6m
->in6m_trace
)(in6m
, TRUE
);
3291 in6m_remref(struct in6_multi
*in6m
, int locked
)
3293 struct ifmultiaddr
*ifma
;
3294 struct mld_ifinfo
*mli
;
3297 IN6M_LOCK_SPIN(in6m
);
3299 IN6M_LOCK_ASSERT_HELD(in6m
);
3301 if (in6m
->in6m_refcount
== 0 || (in6m
->in6m_refcount
== 1 && locked
)) {
3302 panic("%s: in6m=%p negative refcnt", __func__
, in6m
);
3304 } else if (in6m
->in6m_trace
!= NULL
) {
3305 (*in6m
->in6m_trace
)(in6m
, FALSE
);
3308 --in6m
->in6m_refcount
;
3309 if (in6m
->in6m_refcount
> 0) {
3316 * Synchronization with in6_mc_get(). In the event the in6m has been
3317 * detached, the underlying ifma would still be in the if_multiaddrs
3318 * list, and thus can be looked up via if_addmulti(). At that point,
3319 * the only way to find this in6m is via ifma_protospec. To avoid
3320 * race conditions between the last in6m_remref() of that in6m and its
3321 * use via ifma_protospec, in6_multihead lock is used for serialization.
3322 * In order to avoid violating the lock order, we must drop in6m_lock
3323 * before acquiring in6_multihead lock. To prevent the in6m from being
3324 * freed prematurely, we hold an extra reference.
3326 ++in6m
->in6m_refcount
;
3328 in6_multihead_lock_shared();
3329 IN6M_LOCK_SPIN(in6m
);
3330 --in6m
->in6m_refcount
;
3331 if (in6m
->in6m_refcount
> 0) {
3332 /* We've lost the race, so abort since in6m is still in use */
3334 in6_multihead_lock_done();
3335 /* If it was locked, return it as such */
3341 ifma
= in6m
->in6m_ifma
;
3342 in6m
->in6m_ifma
= NULL
;
3343 in6m
->in6m_ifp
= NULL
;
3344 mli
= in6m
->in6m_mli
;
3345 in6m
->in6m_mli
= NULL
;
3347 IFMA_LOCK_SPIN(ifma
);
3348 ifma
->ifma_protospec
= NULL
;
3350 in6_multihead_lock_done();
3352 in6_multi_free(in6m
);
3353 if_delmulti_ifma(ifma
);
3354 /* Release reference held to the underlying ifmultiaddr */
3362 in6m_trace(struct in6_multi
*in6m
, int refhold
)
3364 struct in6_multi_dbg
*in6m_dbg
= (struct in6_multi_dbg
*)in6m
;
3369 if (!(in6m
->in6m_debug
& IFD_DEBUG
)) {
3370 panic("%s: in6m %p has no debug structure", __func__
, in6m
);
3374 cnt
= &in6m_dbg
->in6m_refhold_cnt
;
3375 tr
= in6m_dbg
->in6m_refhold
;
3377 cnt
= &in6m_dbg
->in6m_refrele_cnt
;
3378 tr
= in6m_dbg
->in6m_refrele
;
3381 idx
= atomic_add_16_ov(cnt
, 1) % IN6M_TRACE_HIST_SIZE
;
3382 ctrace_record(&tr
[idx
]);
3385 static struct in6_multi_mship
*
3386 in6_multi_mship_alloc(int how
)
3388 struct in6_multi_mship
*imm
;
3390 imm
= (how
== M_WAITOK
) ? zalloc(imm_zone
) : zalloc_noblock(imm_zone
);
3392 bzero(imm
, imm_size
);
3398 in6_multi_mship_free(struct in6_multi_mship
*imm
)
3400 if (imm
->i6mm_maddr
!= NULL
) {
3401 panic("%s: i6mm_maddr not NULL for imm=%p", __func__
, imm
);
3404 zfree(imm_zone
, imm
);
3408 in6_multihead_lock_exclusive(void)
3410 lck_rw_lock_exclusive(&in6_multihead_lock
);
3414 in6_multihead_lock_shared(void)
3416 lck_rw_lock_shared(&in6_multihead_lock
);
3420 in6_multihead_lock_assert(int what
)
3423 #pragma unused(what)
3425 LCK_RW_ASSERT(&in6_multihead_lock
, what
);
3429 in6_multihead_lock_done(void)
3431 lck_rw_done(&in6_multihead_lock
);
3434 static struct ip6_msource
*
3435 ip6ms_alloc(int how
)
3437 struct ip6_msource
*i6ms
;
3439 i6ms
= (how
== M_WAITOK
) ? zalloc(ip6ms_zone
) :
3440 zalloc_noblock(ip6ms_zone
);
3442 bzero(i6ms
, ip6ms_size
);
3448 ip6ms_free(struct ip6_msource
*i6ms
)
3450 zfree(ip6ms_zone
, i6ms
);
3453 static struct in6_msource
*
3454 in6ms_alloc(int how
)
3456 struct in6_msource
*in6ms
;
3458 in6ms
= (how
== M_WAITOK
) ? zalloc(in6ms_zone
) :
3459 zalloc_noblock(in6ms_zone
);
3461 bzero(in6ms
, in6ms_size
);
3467 in6ms_free(struct in6_msource
*in6ms
)
3469 zfree(in6ms_zone
, in6ms
);
3474 static const char *in6m_modestrs
[] = { "un\n", "in", "ex" };
3477 in6m_mode_str(const int mode
)
3479 if (mode
>= MCAST_UNDEFINED
&& mode
<= MCAST_EXCLUDE
)
3480 return (in6m_modestrs
[mode
]);
3484 static const char *in6m_statestrs
[] = {
3493 "sg-query-pending\n",
3498 in6m_state_str(const int state
)
3500 if (state
>= MLD_NOT_MEMBER
&& state
<= MLD_LEAVING_MEMBER
)
3501 return (in6m_statestrs
[state
]);
3506 * Dump an in6_multi structure to the console.
3509 in6m_print(const struct in6_multi
*inm
)
3513 IN6M_LOCK_ASSERT_HELD(__DECONST(struct in6_multi
*, inm
));
3518 printf("%s: --- begin in6m 0x%llx ---\n", __func__
,
3519 (uint64_t)VM_KERNEL_ADDRPERM(inm
));
3520 printf("addr %s ifp 0x%llx(%s) ifma 0x%llx\n",
3521 ip6_sprintf(&inm
->in6m_addr
),
3522 (uint64_t)VM_KERNEL_ADDRPERM(inm
->in6m_ifp
),
3523 if_name(inm
->in6m_ifp
),
3524 (uint64_t)VM_KERNEL_ADDRPERM(inm
->in6m_ifma
));
3525 printf("timer %u state %s refcount %u scq.len %u\n",
3527 in6m_state_str(inm
->in6m_state
),
3529 inm
->in6m_scq
.ifq_len
);
3530 printf("mli 0x%llx nsrc %lu sctimer %u scrv %u\n",
3531 (uint64_t)VM_KERNEL_ADDRPERM(inm
->in6m_mli
),
3535 for (t
= 0; t
< 2; t
++) {
3536 printf("t%d: fmode %s asm %u ex %u in %u rec %u\n", t
,
3537 in6m_mode_str(inm
->in6m_st
[t
].iss_fmode
),
3538 inm
->in6m_st
[t
].iss_asm
,
3539 inm
->in6m_st
[t
].iss_ex
,
3540 inm
->in6m_st
[t
].iss_in
,
3541 inm
->in6m_st
[t
].iss_rec
);
3543 printf("%s: --- end in6m 0x%llx ---\n", __func__
,
3544 (uint64_t)VM_KERNEL_ADDRPERM(inm
));
3550 in6m_print(__unused
const struct in6_multi
*inm
)