2 * Copyright (c) 2010-2011 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/route.h>
85 #include <netinet/in.h>
86 #include <netinet/in_var.h>
87 #include <netinet6/in6_var.h>
88 #include <netinet/ip6.h>
89 #include <netinet/icmp6.h>
90 #include <netinet6/ip6_var.h>
91 #include <netinet/in_pcb.h>
92 #include <netinet/tcp.h>
93 #include <netinet/tcp_seq.h>
94 #include <netinet/tcp_var.h>
95 #include <netinet6/nd6.h>
96 #include <netinet6/mld6_var.h>
97 #include <netinet6/scope6_var.h>
99 #ifndef __SOCKUNION_DECLARED
101 struct sockaddr_storage ss
;
103 struct sockaddr_dl sdl
;
104 struct sockaddr_in6 sin6
;
106 typedef union sockunion sockunion_t
;
107 #define __SOCKUNION_DECLARED
108 #endif /* __SOCKUNION_DECLARED */
110 static void im6f_commit(struct in6_mfilter
*);
111 static int im6f_get_source(struct in6_mfilter
*imf
,
112 const struct sockaddr_in6
*psin
,
113 struct in6_msource
**);
114 static struct in6_msource
*
115 im6f_graft(struct in6_mfilter
*, const uint8_t,
116 const struct sockaddr_in6
*);
117 static int im6f_prune(struct in6_mfilter
*, const struct sockaddr_in6
*);
118 static void im6f_rollback(struct in6_mfilter
*);
119 static void im6f_reap(struct in6_mfilter
*);
120 static int im6o_grow(struct ip6_moptions
*, size_t);
121 static size_t im6o_match_group(const struct ip6_moptions
*,
122 const struct ifnet
*, const struct sockaddr
*);
123 static struct in6_msource
*
124 im6o_match_source(const struct ip6_moptions
*, const size_t,
125 const struct sockaddr
*);
126 static void im6s_merge(struct ip6_msource
*ims
,
127 const struct in6_msource
*lims
, const int rollback
);
128 static int in6_mc_get(struct ifnet
*, const struct in6_addr
*,
129 struct in6_multi
**);
130 static int in6m_get_source(struct in6_multi
*inm
,
131 const struct in6_addr
*addr
, const int noalloc
,
132 struct ip6_msource
**pims
);
133 static int in6m_is_ifp_detached(const struct in6_multi
*);
134 static int in6m_merge(struct in6_multi
*, /*const*/ struct in6_mfilter
*);
135 static void in6m_reap(struct in6_multi
*);
136 static struct ip6_moptions
*
137 in6p_findmoptions(struct inpcb
*);
138 static int in6p_get_source_filters(struct inpcb
*, struct sockopt
*);
139 static int in6p_lookup_v4addr(struct ipv6_mreq
*, struct ip_mreq
*);
140 static int in6p_join_group(struct inpcb
*, struct sockopt
*);
141 static int in6p_leave_group(struct inpcb
*, struct sockopt
*);
142 static struct ifnet
*
143 in6p_lookup_mcast_ifp(const struct inpcb
*,
144 const struct sockaddr_in6
*);
145 static int in6p_block_unblock_source(struct inpcb
*, struct sockopt
*);
146 static int in6p_set_multicast_if(struct inpcb
*, struct sockopt
*);
147 static int in6p_set_source_filters(struct inpcb
*, struct sockopt
*);
148 static int sysctl_ip6_mcast_filters SYSCTL_HANDLER_ARGS
;
149 static __inline__
int ip6_msource_cmp(const struct ip6_msource
*,
150 const struct ip6_msource
*);
152 SYSCTL_DECL(_net_inet6_ip6
); /* XXX Not in any common header. */
154 SYSCTL_NODE(_net_inet6_ip6
, OID_AUTO
, mcast
, CTLFLAG_RW
| CTLFLAG_LOCKED
, 0, "IPv6 multicast");
156 static unsigned long in6_mcast_maxgrpsrc
= IPV6_MAX_GROUP_SRC_FILTER
;
157 SYSCTL_LONG(_net_inet6_ip6_mcast
, OID_AUTO
, maxgrpsrc
,
158 CTLFLAG_RW
| CTLFLAG_LOCKED
, &in6_mcast_maxgrpsrc
,
159 "Max source filters per group");
161 static unsigned long in6_mcast_maxsocksrc
= IPV6_MAX_SOCK_SRC_FILTER
;
162 SYSCTL_LONG(_net_inet6_ip6_mcast
, OID_AUTO
, maxsocksrc
,
163 CTLFLAG_RW
| CTLFLAG_LOCKED
, &in6_mcast_maxsocksrc
,
164 "Max source filters per socket");
166 int in6_mcast_loop
= IPV6_DEFAULT_MULTICAST_LOOP
;
167 SYSCTL_INT(_net_inet6_ip6_mcast
, OID_AUTO
, loop
, CTLFLAG_RW
| CTLFLAG_LOCKED
,
168 &in6_mcast_loop
, 0, "Loopback multicast datagrams by default");
170 SYSCTL_NODE(_net_inet6_ip6_mcast
, OID_AUTO
, filters
,
171 CTLFLAG_RD
| CTLFLAG_LOCKED
, sysctl_ip6_mcast_filters
,
172 "Per-interface stack-wide source filters");
174 RB_GENERATE_PREV(ip6_msource_tree
, ip6_msource
, im6s_link
, ip6_msource_cmp
);
176 #define IN6M_TRACE_HIST_SIZE 32 /* size of trace history */
179 __private_extern__
unsigned int in6m_trace_hist_size
= IN6M_TRACE_HIST_SIZE
;
181 struct in6_multi_dbg
{
182 struct in6_multi in6m
; /* in6_multi */
183 u_int16_t in6m_refhold_cnt
; /* # of ref */
184 u_int16_t in6m_refrele_cnt
; /* # of rele */
186 * Circular lists of in6m_addref and in6m_remref callers.
188 ctrace_t in6m_refhold
[IN6M_TRACE_HIST_SIZE
];
189 ctrace_t in6m_refrele
[IN6M_TRACE_HIST_SIZE
];
193 TAILQ_ENTRY(in6_multi_dbg
) in6m_trash_link
;
196 /* List of trash in6_multi entries protected by in6m_trash_lock */
197 static TAILQ_HEAD(, in6_multi_dbg
) in6m_trash_head
;
198 static decl_lck_mtx_data(, in6m_trash_lock
);
201 static unsigned int in6m_debug
= 1; /* debugging (enabled) */
203 static unsigned int in6m_debug
; /* debugging (disabled) */
205 static unsigned int in6m_size
; /* size of zone element */
206 static struct zone
*in6m_zone
; /* zone for in6_multi */
208 #define IN6M_ZONE_MAX 64 /* maximum elements in zone */
209 #define IN6M_ZONE_NAME "in6_multi" /* zone name */
211 static unsigned int imm_size
; /* size of zone element */
212 static struct zone
*imm_zone
; /* zone for in6_multi_mship */
214 #define IMM_ZONE_MAX 64 /* maximum elements in zone */
215 #define IMM_ZONE_NAME "in6_multi_mship" /* zone name */
217 #define IP6MS_ZONE_MAX 64 /* maximum elements in zone */
218 #define IP6MS_ZONE_NAME "ip6_msource" /* zone name */
220 static unsigned int ip6ms_size
; /* size of zone element */
221 static struct zone
*ip6ms_zone
; /* zone for ip6_msource */
223 #define IN6MS_ZONE_MAX 64 /* maximum elements in zone */
224 #define IN6MS_ZONE_NAME "in6_msource" /* zone name */
226 static unsigned int in6ms_size
; /* size of zone element */
227 static struct zone
*in6ms_zone
; /* zone for in6_msource */
229 /* Lock group and attribute for in6_multihead_lock lock */
230 static lck_attr_t
*in6_multihead_lock_attr
;
231 static lck_grp_t
*in6_multihead_lock_grp
;
232 static lck_grp_attr_t
*in6_multihead_lock_grp_attr
;
234 static decl_lck_rw_data(, in6_multihead_lock
);
235 struct in6_multihead in6_multihead
;
237 static struct in6_multi
*in6_multi_alloc(int);
238 static void in6_multi_free(struct in6_multi
*);
239 static void in6_multi_attach(struct in6_multi
*);
240 static struct in6_multi_mship
*in6_multi_mship_alloc(int);
241 static void in6_multi_mship_free(struct in6_multi_mship
*);
242 static void in6m_trace(struct in6_multi
*, int);
244 static struct ip6_msource
*ip6ms_alloc(int);
245 static void ip6ms_free(struct ip6_msource
*);
246 static struct in6_msource
*in6ms_alloc(int);
247 static void in6ms_free(struct in6_msource
*);
249 #define IM6O_CAST_TO_NONCONST(x) ((struct ip6_moptions *)(void *)(uintptr_t)x)
250 #define IN6M_CAST_TO_NONCONST(x) ((struct in6_multi *)(void *)(uintptr_t)x)
253 * IPv6 source tree comparison function.
255 * An ordered predicate is necessary; bcmp() is not documented to return
256 * an indication of order, memcmp() is, and is an ISO C99 requirement.
259 ip6_msource_cmp(const struct ip6_msource
*a
, const struct ip6_msource
*b
)
261 return (memcmp(&a
->im6s_addr
, &b
->im6s_addr
, sizeof(struct in6_addr
)));
265 * Inline function which wraps assertions for a valid ifp.
267 static __inline__
int
268 in6m_is_ifp_detached(const struct in6_multi
*inm
)
270 VERIFY(inm
->in6m_ifma
!= NULL
);
271 VERIFY(inm
->in6m_ifp
== inm
->in6m_ifma
->ifma_ifp
);
273 return (!ifnet_is_attached(inm
->in6m_ifp
, 0));
277 * Initialize an in6_mfilter structure to a known state at t0, t1
278 * with an empty source filter list.
280 static __inline__
void
281 im6f_init(struct in6_mfilter
*imf
, const int st0
, const int st1
)
283 memset(imf
, 0, sizeof(struct in6_mfilter
));
284 RB_INIT(&imf
->im6f_sources
);
285 imf
->im6f_st
[0] = st0
;
286 imf
->im6f_st
[1] = st1
;
290 * Resize the ip6_moptions vector to the next power-of-two minus 1.
293 im6o_grow(struct ip6_moptions
*imo
, size_t newmax
)
295 struct in6_multi
**nmships
;
296 struct in6_multi
**omships
;
297 struct in6_mfilter
*nmfilters
;
298 struct in6_mfilter
*omfilters
;
302 IM6O_LOCK_ASSERT_HELD(imo
);
306 omships
= imo
->im6o_membership
;
307 omfilters
= imo
->im6o_mfilters
;
308 oldmax
= imo
->im6o_max_memberships
;
310 newmax
= ((oldmax
+ 1) * 2) - 1;
312 if (newmax
> IPV6_MAX_MEMBERSHIPS
)
313 return (ETOOMANYREFS
);
315 if ((nmships
= (struct in6_multi
**)_REALLOC(omships
,
316 sizeof (struct in6_multi
*) * newmax
, M_IP6MOPTS
,
317 M_WAITOK
| M_ZERO
)) == NULL
)
320 imo
->im6o_membership
= nmships
;
322 if ((nmfilters
= (struct in6_mfilter
*)_REALLOC(omfilters
,
323 sizeof (struct in6_mfilter
) * newmax
, M_IN6MFILTER
,
324 M_WAITOK
| M_ZERO
)) == NULL
)
327 imo
->im6o_mfilters
= nmfilters
;
329 /* Initialize newly allocated source filter heads. */
330 for (idx
= oldmax
; idx
< newmax
; idx
++)
331 im6f_init(&nmfilters
[idx
], MCAST_UNDEFINED
, MCAST_EXCLUDE
);
333 imo
->im6o_max_memberships
= newmax
;
339 * Find an IPv6 multicast group entry for this ip6_moptions instance
340 * which matches the specified group, and optionally an interface.
341 * Return its index into the array, or -1 if not found.
344 im6o_match_group(const struct ip6_moptions
*imo
, const struct ifnet
*ifp
,
345 const struct sockaddr
*group
)
347 const struct sockaddr_in6
*gsin6
;
348 struct in6_multi
*pinm
;
352 IM6O_LOCK_ASSERT_HELD(IM6O_CAST_TO_NONCONST(imo
));
354 gsin6
= (const struct sockaddr_in6
*)group
;
356 /* The im6o_membership array may be lazy allocated. */
357 if (imo
->im6o_membership
== NULL
|| imo
->im6o_num_memberships
== 0)
360 nmships
= imo
->im6o_num_memberships
;
361 for (idx
= 0; idx
< nmships
; idx
++) {
362 pinm
= imo
->im6o_membership
[idx
];
366 if ((ifp
== NULL
|| (pinm
->in6m_ifp
== ifp
)) &&
367 IN6_ARE_ADDR_EQUAL(&pinm
->in6m_addr
,
368 &gsin6
->sin6_addr
)) {
381 * Find an IPv6 multicast source entry for this imo which matches
382 * the given group index for this socket, and source address.
384 * XXX TODO: The scope ID, if present in src, is stripped before
385 * any comparison. We SHOULD enforce scope/zone checks where the source
386 * filter entry has a link scope.
388 * NOTE: This does not check if the entry is in-mode, merely if
389 * it exists, which may not be the desired behaviour.
391 static struct in6_msource
*
392 im6o_match_source(const struct ip6_moptions
*imo
, const size_t gidx
,
393 const struct sockaddr
*src
)
395 struct ip6_msource find
;
396 struct in6_mfilter
*imf
;
397 struct ip6_msource
*ims
;
398 const sockunion_t
*psa
;
400 IM6O_LOCK_ASSERT_HELD(IM6O_CAST_TO_NONCONST(imo
));
402 VERIFY(src
->sa_family
== AF_INET6
);
403 VERIFY(gidx
!= (size_t)-1 && gidx
< imo
->im6o_num_memberships
);
405 /* The im6o_mfilters array may be lazy allocated. */
406 if (imo
->im6o_mfilters
== NULL
)
408 imf
= &imo
->im6o_mfilters
[gidx
];
410 psa
= (const sockunion_t
*)src
;
411 find
.im6s_addr
= psa
->sin6
.sin6_addr
;
412 in6_clearscope(&find
.im6s_addr
); /* XXX */
413 ims
= RB_FIND(ip6_msource_tree
, &imf
->im6f_sources
, &find
);
415 return ((struct in6_msource
*)ims
);
419 * Perform filtering for multicast datagrams on a socket by group and source.
421 * Returns 0 if a datagram should be allowed through, or various error codes
422 * if the socket was not a member of the group, or the source was muted, etc.
425 im6o_mc_filter(const struct ip6_moptions
*imo
, const struct ifnet
*ifp
,
426 const struct sockaddr
*group
, const struct sockaddr
*src
)
429 struct in6_msource
*ims
;
432 IM6O_LOCK_ASSERT_HELD(IM6O_CAST_TO_NONCONST(imo
));
435 gidx
= im6o_match_group(imo
, ifp
, group
);
436 if (gidx
== (size_t)-1)
437 return (MCAST_NOTGMEMBER
);
440 * Check if the source was included in an (S,G) join.
441 * Allow reception on exclusive memberships by default,
442 * reject reception on inclusive memberships by default.
443 * Exclude source only if an in-mode exclude filter exists.
444 * Include source only if an in-mode include filter exists.
445 * NOTE: We are comparing group state here at MLD t1 (now)
446 * with socket-layer t0 (since last downcall).
448 mode
= imo
->im6o_mfilters
[gidx
].im6f_st
[1];
449 ims
= im6o_match_source(imo
, gidx
, src
);
451 if ((ims
== NULL
&& mode
== MCAST_INCLUDE
) ||
452 (ims
!= NULL
&& ims
->im6sl_st
[0] != mode
))
453 return (MCAST_NOTSMEMBER
);
459 * Find and return a reference to an in6_multi record for (ifp, group),
460 * and bump its reference count.
461 * If one does not exist, try to allocate it, and update link-layer multicast
462 * filters on ifp to listen for group.
463 * Assumes the IN6_MULTI lock is held across the call.
464 * Return 0 if successful, otherwise return an appropriate error code.
467 in6_mc_get(struct ifnet
*ifp
, const struct in6_addr
*group
,
468 struct in6_multi
**pinm
)
470 struct sockaddr_in6 gsin6
;
471 struct ifmultiaddr
*ifma
;
472 struct in6_multi
*inm
;
477 in6_multihead_lock_shared();
478 IN6_LOOKUP_MULTI(group
, ifp
, inm
);
481 VERIFY(inm
->in6m_reqcnt
>= 1);
483 VERIFY(inm
->in6m_reqcnt
!= 0);
486 in6_multihead_lock_done();
488 * We already joined this group; return the in6m
489 * with a refcount held (via lookup) for caller.
493 in6_multihead_lock_done();
495 memset(&gsin6
, 0, sizeof(gsin6
));
496 gsin6
.sin6_family
= AF_INET6
;
497 gsin6
.sin6_len
= sizeof(struct sockaddr_in6
);
498 gsin6
.sin6_addr
= *group
;
501 * Check if a link-layer group is already associated
502 * with this network-layer group on the given ifnet.
504 error
= if_addmulti(ifp
, (struct sockaddr
*)&gsin6
, &ifma
);
509 * See comments in in6m_remref() for access to ifma_protospec.
511 in6_multihead_lock_exclusive();
513 if ((inm
= ifma
->ifma_protospec
) != NULL
) {
514 VERIFY(ifma
->ifma_addr
!= NULL
);
515 VERIFY(ifma
->ifma_addr
->sa_family
== AF_INET6
);
516 IN6M_ADDREF(inm
); /* for caller */
519 VERIFY(inm
->in6m_ifma
== ifma
);
520 VERIFY(inm
->in6m_ifp
== ifp
);
521 VERIFY(IN6_ARE_ADDR_EQUAL(&inm
->in6m_addr
, group
));
522 if (inm
->in6m_debug
& IFD_ATTACHED
) {
523 VERIFY(inm
->in6m_reqcnt
>= 1);
525 VERIFY(inm
->in6m_reqcnt
!= 0);
528 in6_multihead_lock_done();
531 * We lost the race with another thread doing
532 * in6_mc_get(); since this group has already
533 * been joined; return the inm with a refcount
539 * We lost the race with another thread doing in6_delmulti();
540 * the inm referring to the ifma has been detached, thus we
541 * reattach it back to the in6_multihead list, and return the
542 * inm with a refcount held for the caller.
544 in6_multi_attach(inm
);
545 VERIFY((inm
->in6m_debug
&
546 (IFD_ATTACHED
| IFD_TRASHED
)) == IFD_ATTACHED
);
549 in6_multihead_lock_done();
556 * A new in6_multi record is needed; allocate and initialize it.
557 * We DO NOT perform an MLD join as the in6_ layer may need to
558 * push an initial source list down to MLD to support SSM.
560 * The initial source filter state is INCLUDE, {} as per the RFC.
561 * Pending state-changes per group are subject to a bounds check.
563 inm
= in6_multi_alloc(M_WAITOK
);
565 in6_multihead_lock_done();
570 inm
->in6m_addr
= *group
;
572 inm
->in6m_mli
= MLD_IFINFO(ifp
);
573 VERIFY(inm
->in6m_mli
!= NULL
);
574 MLI_ADDREF(inm
->in6m_mli
);
575 inm
->in6m_ifma
= ifma
; /* keep refcount from if_addmulti() */
576 inm
->in6m_state
= MLD_NOT_MEMBER
;
578 * Pending state-changes per group are subject to a bounds check.
580 inm
->in6m_scq
.ifq_maxlen
= MLD_MAX_STATE_CHANGES
;
581 inm
->in6m_st
[0].iss_fmode
= MCAST_UNDEFINED
;
582 inm
->in6m_st
[1].iss_fmode
= MCAST_UNDEFINED
;
583 RB_INIT(&inm
->in6m_srcs
);
585 in6_multi_attach(inm
);
586 VERIFY((inm
->in6m_debug
&
587 (IFD_ATTACHED
| IFD_TRASHED
)) == IFD_ATTACHED
);
588 IN6M_ADDREF_LOCKED(inm
); /* for caller */
592 VERIFY(ifma
->ifma_protospec
== NULL
);
593 ifma
->ifma_protospec
= inm
;
595 in6_multihead_lock_done();
601 * Clear recorded source entries for a group.
602 * Used by the MLD code. Caller must hold the IN6_MULTI lock.
603 * FIXME: Should reap.
606 in6m_clear_recorded(struct in6_multi
*inm
)
608 struct ip6_msource
*ims
;
610 IN6M_LOCK_ASSERT_HELD(inm
);
612 RB_FOREACH(ims
, ip6_msource_tree
, &inm
->in6m_srcs
) {
615 --inm
->in6m_st
[1].iss_rec
;
618 VERIFY(inm
->in6m_st
[1].iss_rec
== 0);
622 * Record a source as pending for a Source-Group MLDv2 query.
623 * This lives here as it modifies the shared tree.
625 * inm is the group descriptor.
626 * naddr is the address of the source to record in network-byte order.
628 * If the net.inet6.mld.sgalloc sysctl is non-zero, we will
629 * lazy-allocate a source node in response to an SG query.
630 * Otherwise, no allocation is performed. This saves some memory
631 * with the trade-off that the source will not be reported to the
632 * router if joined in the window between the query response and
633 * the group actually being joined on the local host.
635 * VIMAGE: XXX: Currently the mld_sgalloc feature has been removed.
636 * This turns off the allocation of a recorded source entry if
637 * the group has not been joined.
639 * Return 0 if the source didn't exist or was already marked as recorded.
640 * Return 1 if the source was marked as recorded by this function.
641 * Return <0 if any error occured (negated errno code).
644 in6m_record_source(struct in6_multi
*inm
, const struct in6_addr
*addr
)
646 struct ip6_msource find
;
647 struct ip6_msource
*ims
, *nims
;
649 IN6M_LOCK_ASSERT_HELD(inm
);
651 find
.im6s_addr
= *addr
;
652 ims
= RB_FIND(ip6_msource_tree
, &inm
->in6m_srcs
, &find
);
653 if (ims
&& ims
->im6s_stp
)
656 if (inm
->in6m_nsrc
== in6_mcast_maxgrpsrc
)
658 nims
= ip6ms_alloc(M_WAITOK
);
661 nims
->im6s_addr
= find
.im6s_addr
;
662 RB_INSERT(ip6_msource_tree
, &inm
->in6m_srcs
, nims
);
668 * Mark the source as recorded and update the recorded
672 ++inm
->in6m_st
[1].iss_rec
;
678 * Return a pointer to an in6_msource owned by an in6_mfilter,
679 * given its source address.
680 * Lazy-allocate if needed. If this is a new entry its filter state is
683 * imf is the filter set being modified.
684 * addr is the source address.
686 * Caller is expected to be holding im6o_lock.
689 im6f_get_source(struct in6_mfilter
*imf
, const struct sockaddr_in6
*psin
,
690 struct in6_msource
**plims
)
692 struct ip6_msource find
;
693 struct ip6_msource
*ims
;
694 struct in6_msource
*lims
;
701 find
.im6s_addr
= psin
->sin6_addr
;
702 ims
= RB_FIND(ip6_msource_tree
, &imf
->im6f_sources
, &find
);
703 lims
= (struct in6_msource
*)ims
;
705 if (imf
->im6f_nsrc
== in6_mcast_maxsocksrc
)
707 lims
= in6ms_alloc(M_WAITOK
);
710 lims
->im6s_addr
= find
.im6s_addr
;
711 lims
->im6sl_st
[0] = MCAST_UNDEFINED
;
712 RB_INSERT(ip6_msource_tree
, &imf
->im6f_sources
,
713 (struct ip6_msource
*)lims
);
723 * Graft a source entry into an existing socket-layer filter set,
724 * maintaining any required invariants and checking allocations.
726 * The source is marked as being in the new filter mode at t1.
728 * Return the pointer to the new node, otherwise return NULL.
730 * Caller is expected to be holding im6o_lock.
732 static struct in6_msource
*
733 im6f_graft(struct in6_mfilter
*imf
, const uint8_t st1
,
734 const struct sockaddr_in6
*psin
)
736 struct in6_msource
*lims
;
738 lims
= in6ms_alloc(M_WAITOK
);
741 lims
->im6s_addr
= psin
->sin6_addr
;
742 lims
->im6sl_st
[0] = MCAST_UNDEFINED
;
743 lims
->im6sl_st
[1] = st1
;
744 RB_INSERT(ip6_msource_tree
, &imf
->im6f_sources
,
745 (struct ip6_msource
*)lims
);
752 * Prune a source entry from an existing socket-layer filter set,
753 * maintaining any required invariants and checking allocations.
755 * The source is marked as being left at t1, it is not freed.
757 * Return 0 if no error occurred, otherwise return an errno value.
759 * Caller is expected to be holding im6o_lock.
762 im6f_prune(struct in6_mfilter
*imf
, const struct sockaddr_in6
*psin
)
764 struct ip6_msource find
;
765 struct ip6_msource
*ims
;
766 struct in6_msource
*lims
;
768 find
.im6s_addr
= psin
->sin6_addr
;
769 ims
= RB_FIND(ip6_msource_tree
, &imf
->im6f_sources
, &find
);
772 lims
= (struct in6_msource
*)ims
;
773 lims
->im6sl_st
[1] = MCAST_UNDEFINED
;
778 * Revert socket-layer filter set deltas at t1 to t0 state.
780 * Caller is expected to be holding im6o_lock.
783 im6f_rollback(struct in6_mfilter
*imf
)
785 struct ip6_msource
*ims
, *tims
;
786 struct in6_msource
*lims
;
788 RB_FOREACH_SAFE(ims
, ip6_msource_tree
, &imf
->im6f_sources
, tims
) {
789 lims
= (struct in6_msource
*)ims
;
790 if (lims
->im6sl_st
[0] == lims
->im6sl_st
[1]) {
791 /* no change at t1 */
793 } else if (lims
->im6sl_st
[0] != MCAST_UNDEFINED
) {
794 /* revert change to existing source at t1 */
795 lims
->im6sl_st
[1] = lims
->im6sl_st
[0];
797 /* revert source added t1 */
798 MLD_PRINTF(("%s: free in6ms %p\n", __func__
, lims
));
799 RB_REMOVE(ip6_msource_tree
, &imf
->im6f_sources
, ims
);
804 imf
->im6f_st
[1] = imf
->im6f_st
[0];
808 * Mark socket-layer filter set as INCLUDE {} at t1.
810 * Caller is expected to be holding im6o_lock.
813 im6f_leave(struct in6_mfilter
*imf
)
815 struct ip6_msource
*ims
;
816 struct in6_msource
*lims
;
818 RB_FOREACH(ims
, ip6_msource_tree
, &imf
->im6f_sources
) {
819 lims
= (struct in6_msource
*)ims
;
820 lims
->im6sl_st
[1] = MCAST_UNDEFINED
;
822 imf
->im6f_st
[1] = MCAST_INCLUDE
;
826 * Mark socket-layer filter set deltas as committed.
828 * Caller is expected to be holding im6o_lock.
831 im6f_commit(struct in6_mfilter
*imf
)
833 struct ip6_msource
*ims
;
834 struct in6_msource
*lims
;
836 RB_FOREACH(ims
, ip6_msource_tree
, &imf
->im6f_sources
) {
837 lims
= (struct in6_msource
*)ims
;
838 lims
->im6sl_st
[0] = lims
->im6sl_st
[1];
840 imf
->im6f_st
[0] = imf
->im6f_st
[1];
844 * Reap unreferenced sources from socket-layer filter set.
846 * Caller is expected to be holding im6o_lock.
849 im6f_reap(struct in6_mfilter
*imf
)
851 struct ip6_msource
*ims
, *tims
;
852 struct in6_msource
*lims
;
854 RB_FOREACH_SAFE(ims
, ip6_msource_tree
, &imf
->im6f_sources
, tims
) {
855 lims
= (struct in6_msource
*)ims
;
856 if ((lims
->im6sl_st
[0] == MCAST_UNDEFINED
) &&
857 (lims
->im6sl_st
[1] == MCAST_UNDEFINED
)) {
858 MLD_PRINTF(("%s: free in6ms %p\n", __func__
, lims
));
859 RB_REMOVE(ip6_msource_tree
, &imf
->im6f_sources
, ims
);
867 * Purge socket-layer filter set.
869 * Caller is expected to be holding im6o_lock.
872 im6f_purge(struct in6_mfilter
*imf
)
874 struct ip6_msource
*ims
, *tims
;
875 struct in6_msource
*lims
;
877 RB_FOREACH_SAFE(ims
, ip6_msource_tree
, &imf
->im6f_sources
, tims
) {
878 lims
= (struct in6_msource
*)ims
;
879 MLD_PRINTF(("%s: free in6ms %p\n", __func__
, lims
));
880 RB_REMOVE(ip6_msource_tree
, &imf
->im6f_sources
, ims
);
884 imf
->im6f_st
[0] = imf
->im6f_st
[1] = MCAST_UNDEFINED
;
885 VERIFY(RB_EMPTY(&imf
->im6f_sources
));
889 * Look up a source filter entry for a multicast group.
891 * inm is the group descriptor to work with.
892 * addr is the IPv6 address to look up.
893 * noalloc may be non-zero to suppress allocation of sources.
894 * *pims will be set to the address of the retrieved or allocated source.
896 * Return 0 if successful, otherwise return a non-zero error code.
899 in6m_get_source(struct in6_multi
*inm
, const struct in6_addr
*addr
,
900 const int noalloc
, struct ip6_msource
**pims
)
902 struct ip6_msource find
;
903 struct ip6_msource
*ims
, *nims
;
905 IN6M_LOCK_ASSERT_HELD(inm
);
907 find
.im6s_addr
= *addr
;
908 ims
= RB_FIND(ip6_msource_tree
, &inm
->in6m_srcs
, &find
);
909 if (ims
== NULL
&& !noalloc
) {
910 if (inm
->in6m_nsrc
== in6_mcast_maxgrpsrc
)
912 nims
= ip6ms_alloc(M_WAITOK
);
915 nims
->im6s_addr
= *addr
;
916 RB_INSERT(ip6_msource_tree
, &inm
->in6m_srcs
, nims
);
919 MLD_PRINTF(("%s: allocated %s as %p\n", __func__
,
920 ip6_sprintf(addr
), ims
));
928 * Helper function to derive the filter mode on a source entry
929 * from its internal counters. Predicates are:
930 * A source is only excluded if all listeners exclude it.
931 * A source is only included if no listeners exclude it,
932 * and at least one listener includes it.
933 * May be used by ifmcstat(8).
936 im6s_get_mode(const struct in6_multi
*inm
, const struct ip6_msource
*ims
,
939 IN6M_LOCK_ASSERT_HELD(IN6M_CAST_TO_NONCONST(inm
));
942 if (inm
->in6m_st
[t
].iss_ex
> 0 &&
943 inm
->in6m_st
[t
].iss_ex
== ims
->im6s_st
[t
].ex
)
944 return (MCAST_EXCLUDE
);
945 else if (ims
->im6s_st
[t
].in
> 0 && ims
->im6s_st
[t
].ex
== 0)
946 return (MCAST_INCLUDE
);
947 return (MCAST_UNDEFINED
);
951 * Merge socket-layer source into MLD-layer source.
952 * If rollback is non-zero, perform the inverse of the merge.
955 im6s_merge(struct ip6_msource
*ims
, const struct in6_msource
*lims
,
958 int n
= rollback
? -1 : 1;
960 if (lims
->im6sl_st
[0] == 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
[0] == 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
;
970 if (lims
->im6sl_st
[1] == MCAST_EXCLUDE
) {
971 MLD_PRINTF(("%s: t1 ex += %d on %s\n", __func__
, n
,
972 ip6_sprintf(&lims
->im6s_addr
)));
973 ims
->im6s_st
[1].ex
+= n
;
974 } else if (lims
->im6sl_st
[1] == MCAST_INCLUDE
) {
975 MLD_PRINTF(("%s: t1 in += %d on %s\n", __func__
, n
,
976 ip6_sprintf(&lims
->im6s_addr
)));
977 ims
->im6s_st
[1].in
+= n
;
982 * Atomically update the global in6_multi state, when a membership's
983 * filter list is being updated in any way.
985 * imf is the per-inpcb-membership group filter pointer.
986 * A fake imf may be passed for in-kernel consumers.
988 * XXX This is a candidate for a set-symmetric-difference style loop
989 * which would eliminate the repeated lookup from root of ims nodes,
990 * as they share the same key space.
992 * If any error occurred this function will back out of refcounts
993 * and return a non-zero value.
996 in6m_merge(struct in6_multi
*inm
, /*const*/ struct in6_mfilter
*imf
)
998 struct ip6_msource
*ims
, *nims
;
999 struct in6_msource
*lims
;
1000 int schanged
, error
;
1003 IN6M_LOCK_ASSERT_HELD(inm
);
1010 * Update the source filters first, as this may fail.
1011 * Maintain count of in-mode filters at t0, t1. These are
1012 * used to work out if we transition into ASM mode or not.
1013 * Maintain a count of source filters whose state was
1014 * actually modified by this operation.
1016 RB_FOREACH(ims
, ip6_msource_tree
, &imf
->im6f_sources
) {
1017 lims
= (struct in6_msource
*)ims
;
1018 if (lims
->im6sl_st
[0] == imf
->im6f_st
[0]) nsrc0
++;
1019 if (lims
->im6sl_st
[1] == imf
->im6f_st
[1]) nsrc1
++;
1020 if (lims
->im6sl_st
[0] == lims
->im6sl_st
[1]) continue;
1021 error
= in6m_get_source(inm
, &lims
->im6s_addr
, 0, &nims
);
1025 im6s_merge(nims
, lims
, 0);
1028 struct ip6_msource
*bims
;
1030 RB_FOREACH_REVERSE_FROM(ims
, ip6_msource_tree
, nims
) {
1031 lims
= (struct in6_msource
*)ims
;
1032 if (lims
->im6sl_st
[0] == lims
->im6sl_st
[1])
1034 (void) in6m_get_source(inm
, &lims
->im6s_addr
, 1, &bims
);
1037 im6s_merge(bims
, lims
, 1);
1042 MLD_PRINTF(("%s: imf filters in-mode: %d at t0, %d at t1\n",
1043 __func__
, nsrc0
, nsrc1
));
1045 /* Handle transition between INCLUDE {n} and INCLUDE {} on socket. */
1046 if (imf
->im6f_st
[0] == imf
->im6f_st
[1] &&
1047 imf
->im6f_st
[1] == MCAST_INCLUDE
) {
1049 MLD_PRINTF(("%s: --in on inm at t1\n", __func__
));
1050 --inm
->in6m_st
[1].iss_in
;
1054 /* Handle filter mode transition on socket. */
1055 if (imf
->im6f_st
[0] != imf
->im6f_st
[1]) {
1056 MLD_PRINTF(("%s: imf transition %d to %d\n",
1057 __func__
, imf
->im6f_st
[0], imf
->im6f_st
[1]));
1059 if (imf
->im6f_st
[0] == MCAST_EXCLUDE
) {
1060 MLD_PRINTF(("%s: --ex on inm at t1\n", __func__
));
1061 --inm
->in6m_st
[1].iss_ex
;
1062 } else if (imf
->im6f_st
[0] == MCAST_INCLUDE
) {
1063 MLD_PRINTF(("%s: --in on inm at t1\n", __func__
));
1064 --inm
->in6m_st
[1].iss_in
;
1067 if (imf
->im6f_st
[1] == MCAST_EXCLUDE
) {
1068 MLD_PRINTF(("%s: ex++ on inm at t1\n", __func__
));
1069 inm
->in6m_st
[1].iss_ex
++;
1070 } else if (imf
->im6f_st
[1] == MCAST_INCLUDE
&& nsrc1
> 0) {
1071 MLD_PRINTF(("%s: in++ on inm at t1\n", __func__
));
1072 inm
->in6m_st
[1].iss_in
++;
1077 * Track inm filter state in terms of listener counts.
1078 * If there are any exclusive listeners, stack-wide
1079 * membership is exclusive.
1080 * Otherwise, if only inclusive listeners, stack-wide is inclusive.
1081 * If no listeners remain, state is undefined at t1,
1082 * and the MLD lifecycle for this group should finish.
1084 if (inm
->in6m_st
[1].iss_ex
> 0) {
1085 MLD_PRINTF(("%s: transition to EX\n", __func__
));
1086 inm
->in6m_st
[1].iss_fmode
= MCAST_EXCLUDE
;
1087 } else if (inm
->in6m_st
[1].iss_in
> 0) {
1088 MLD_PRINTF(("%s: transition to IN\n", __func__
));
1089 inm
->in6m_st
[1].iss_fmode
= MCAST_INCLUDE
;
1091 MLD_PRINTF(("%s: transition to UNDEF\n", __func__
));
1092 inm
->in6m_st
[1].iss_fmode
= MCAST_UNDEFINED
;
1095 /* Decrement ASM listener count on transition out of ASM mode. */
1096 if (imf
->im6f_st
[0] == MCAST_EXCLUDE
&& nsrc0
== 0) {
1097 if ((imf
->im6f_st
[1] != MCAST_EXCLUDE
) ||
1098 (imf
->im6f_st
[1] == MCAST_EXCLUDE
&& nsrc1
> 0)) {
1099 MLD_PRINTF(("%s: --asm on inm at t1\n", __func__
));
1100 --inm
->in6m_st
[1].iss_asm
;
1104 /* Increment ASM listener count on transition to ASM mode. */
1105 if (imf
->im6f_st
[1] == MCAST_EXCLUDE
&& nsrc1
== 0) {
1106 MLD_PRINTF(("%s: asm++ on inm at t1\n", __func__
));
1107 inm
->in6m_st
[1].iss_asm
++;
1110 MLD_PRINTF(("%s: merged imf %p to inm %p\n", __func__
, imf
, inm
));
1115 MLD_PRINTF(("%s: sources changed; reaping\n", __func__
));
1122 * Mark an in6_multi's filter set deltas as committed.
1123 * Called by MLD after a state change has been enqueued.
1126 in6m_commit(struct in6_multi
*inm
)
1128 struct ip6_msource
*ims
;
1130 IN6M_LOCK_ASSERT_HELD(inm
);
1132 MLD_PRINTF(("%s: commit inm %p\n", __func__
, inm
));
1133 MLD_PRINTF(("%s: pre commit:\n", __func__
));
1136 RB_FOREACH(ims
, ip6_msource_tree
, &inm
->in6m_srcs
) {
1137 ims
->im6s_st
[0] = ims
->im6s_st
[1];
1139 inm
->in6m_st
[0] = inm
->in6m_st
[1];
1143 * Reap unreferenced nodes from an in6_multi's filter set.
1146 in6m_reap(struct in6_multi
*inm
)
1148 struct ip6_msource
*ims
, *tims
;
1150 IN6M_LOCK_ASSERT_HELD(inm
);
1152 RB_FOREACH_SAFE(ims
, ip6_msource_tree
, &inm
->in6m_srcs
, tims
) {
1153 if (ims
->im6s_st
[0].ex
> 0 || ims
->im6s_st
[0].in
> 0 ||
1154 ims
->im6s_st
[1].ex
> 0 || ims
->im6s_st
[1].in
> 0 ||
1157 MLD_PRINTF(("%s: free ims %p\n", __func__
, ims
));
1158 RB_REMOVE(ip6_msource_tree
, &inm
->in6m_srcs
, ims
);
1165 * Purge all source nodes from an in6_multi's filter set.
1168 in6m_purge(struct in6_multi
*inm
)
1170 struct ip6_msource
*ims
, *tims
;
1172 IN6M_LOCK_ASSERT_HELD(inm
);
1174 RB_FOREACH_SAFE(ims
, ip6_msource_tree
, &inm
->in6m_srcs
, tims
) {
1175 MLD_PRINTF(("%s: free ims %p\n", __func__
, ims
));
1176 RB_REMOVE(ip6_msource_tree
, &inm
->in6m_srcs
, ims
);
1183 * Join a multicast address w/o sources.
1184 * KAME compatibility entry point.
1187 struct in6_multi_mship
*
1188 in6_joingroup(struct ifnet
*ifp
, struct in6_addr
*mcaddr
,
1189 int *errorp
, int delay
)
1191 struct in6_multi_mship
*imm
;
1196 imm
= in6_multi_mship_alloc(M_WAITOK
);
1202 delay
= (delay
* PR_SLOWHZ
) / hz
;
1204 error
= in6_mc_join(ifp
, mcaddr
, NULL
, &imm
->i6mm_maddr
, delay
);
1207 in6_multi_mship_free(imm
);
1215 * Leave a multicast address w/o sources.
1216 * KAME compatibility entry point.
1219 in6_leavegroup(struct in6_multi_mship
*imm
)
1221 if (imm
->i6mm_maddr
!= NULL
) {
1222 in6_mc_leave(imm
->i6mm_maddr
, NULL
);
1223 IN6M_REMREF(imm
->i6mm_maddr
);
1224 imm
->i6mm_maddr
= NULL
;
1226 in6_multi_mship_free(imm
);
1231 * Join a multicast group; real entry point.
1233 * Only preserves atomicity at inm level.
1234 * NOTE: imf argument cannot be const due to sys/tree.h limitations.
1236 * If the MLD downcall fails, the group is not joined, and an error
1240 in6_mc_join(struct ifnet
*ifp
, const struct in6_addr
*mcaddr
,
1241 /*const*/ struct in6_mfilter
*imf
, struct in6_multi
**pinm
,
1244 struct in6_mfilter timf
;
1245 struct in6_multi
*inm
= NULL
;
1249 * Sanity: Check scope zone ID was set for ifp, if and
1250 * only if group is scoped to an interface.
1252 VERIFY(IN6_IS_ADDR_MULTICAST(mcaddr
));
1253 if (IN6_IS_ADDR_MC_LINKLOCAL(mcaddr
) ||
1254 IN6_IS_ADDR_MC_INTFACELOCAL(mcaddr
)) {
1255 VERIFY(mcaddr
->s6_addr16
[1] != 0);
1258 MLD_PRINTF(("%s: join %s on %p(%s%d))\n", __func__
,
1259 ip6_sprintf(mcaddr
), ifp
, ifp
->if_name
, ifp
->if_unit
));
1264 * If no imf was specified (i.e. kernel consumer),
1265 * fake one up and assume it is an ASM join.
1268 im6f_init(&timf
, MCAST_UNDEFINED
, MCAST_EXCLUDE
);
1272 error
= in6_mc_get(ifp
, mcaddr
, &inm
);
1274 MLD_PRINTF(("%s: in6_mc_get() failure\n", __func__
));
1278 MLD_PRINTF(("%s: merge inm state\n", __func__
));
1281 error
= in6m_merge(inm
, imf
);
1283 MLD_PRINTF(("%s: failed to merge inm state\n", __func__
));
1284 goto out_in6m_release
;
1287 MLD_PRINTF(("%s: doing mld downcall\n", __func__
));
1288 error
= mld_change_state(inm
, delay
);
1290 MLD_PRINTF(("%s: failed to update source\n", __func__
));
1291 goto out_in6m_release
;
1296 MLD_PRINTF(("%s: dropping ref on %p\n", __func__
, inm
));
1301 *pinm
= inm
; /* keep refcount from in6_mc_get() */
1308 * Leave a multicast group; real entry point.
1309 * All source filters will be expunged.
1311 * Only preserves atomicity at inm level.
1313 * Holding the write lock for the INP which contains imf
1314 * is highly advisable. We can't assert for it as imf does not
1315 * contain a back-pointer to the owning inp.
1317 * Note: This is not the same as in6m_release(*) as this function also
1318 * makes a state change downcall into MLD.
1321 in6_mc_leave(struct in6_multi
*inm
, /*const*/ struct in6_mfilter
*imf
)
1323 struct in6_mfilter timf
;
1328 IN6M_LOCK_ASSERT_NOTHELD(inm
);
1330 in6_multihead_lock_exclusive();
1333 MLD_PRINTF(("%s: leave inm %p, %s/%s%d, imf %p\n", __func__
,
1334 inm
, ip6_sprintf(&inm
->in6m_addr
),
1335 (in6m_is_ifp_detached(inm
) ? "null" : inm
->in6m_ifp
->if_name
),
1336 inm
->in6m_ifp
->if_unit
, imf
));
1339 * If no imf was specified (i.e. kernel consumer),
1340 * fake one up and assume it is an ASM join.
1343 im6f_init(&timf
, MCAST_EXCLUDE
, MCAST_UNDEFINED
);
1348 * Begin state merge transaction at MLD layer.
1350 * As this particular invocation should not cause any memory
1351 * to be allocated, and there is no opportunity to roll back
1352 * the transaction, it MUST NOT fail.
1354 MLD_PRINTF(("%s: merge inm state\n", __func__
));
1356 error
= in6m_merge(inm
, imf
);
1357 KASSERT(error
== 0, ("%s: failed to merge inm state\n", __func__
));
1359 MLD_PRINTF(("%s: doing mld downcall\n", __func__
));
1360 error
= mld_change_state(inm
, 0);
1363 MLD_PRINTF(("%s: failed mld downcall\n", __func__
));
1365 lastref
= in6_multi_detach(inm
);
1366 VERIFY(!lastref
|| (!(inm
->in6m_debug
& IFD_ATTACHED
) &&
1367 inm
->in6m_reqcnt
== 0));
1369 in6_multihead_lock_done();
1372 IN6M_REMREF(inm
); /* for in6_multihead list */
1378 * Block or unblock an ASM multicast source on an inpcb.
1379 * This implements the delta-based API described in RFC 3678.
1381 * The delta-based API applies only to exclusive-mode memberships.
1382 * An MLD downcall will be performed.
1384 * Return 0 if successful, otherwise return an appropriate error code.
1387 in6p_block_unblock_source(struct inpcb
*inp
, struct sockopt
*sopt
)
1389 struct group_source_req gsr
;
1390 sockunion_t
*gsa
, *ssa
;
1392 struct in6_mfilter
*imf
;
1393 struct ip6_moptions
*imo
;
1394 struct in6_msource
*ims
;
1395 struct in6_multi
*inm
;
1404 memset(&gsr
, 0, sizeof(struct group_source_req
));
1405 gsa
= (sockunion_t
*)&gsr
.gsr_group
;
1406 ssa
= (sockunion_t
*)&gsr
.gsr_source
;
1408 switch (sopt
->sopt_name
) {
1409 case MCAST_BLOCK_SOURCE
:
1410 case MCAST_UNBLOCK_SOURCE
:
1411 error
= sooptcopyin(sopt
, &gsr
,
1412 sizeof(struct group_source_req
),
1413 sizeof(struct group_source_req
));
1417 if (gsa
->sin6
.sin6_family
!= AF_INET6
||
1418 gsa
->sin6
.sin6_len
!= sizeof(struct sockaddr_in6
))
1421 if (ssa
->sin6
.sin6_family
!= AF_INET6
||
1422 ssa
->sin6
.sin6_len
!= sizeof(struct sockaddr_in6
))
1425 ifnet_head_lock_shared();
1426 if (gsr
.gsr_interface
== 0 ||
1427 (u_int
)if_index
< gsr
.gsr_interface
) {
1429 return (EADDRNOTAVAIL
);
1432 ifp
= ifindex2ifnet
[gsr
.gsr_interface
];
1436 return (EADDRNOTAVAIL
);
1438 if (sopt
->sopt_name
== MCAST_BLOCK_SOURCE
)
1443 MLD_PRINTF(("%s: unknown sopt_name %d\n",
1444 __func__
, sopt
->sopt_name
));
1445 return (EOPNOTSUPP
);
1449 if (!IN6_IS_ADDR_MULTICAST(&gsa
->sin6
.sin6_addr
))
1452 (void) in6_setscope(&gsa
->sin6
.sin6_addr
, ifp
, NULL
);
1455 * Check if we are actually a member of this group.
1457 imo
= in6p_findmoptions(inp
);
1462 idx
= im6o_match_group(imo
, ifp
, &gsa
->sa
);
1463 if (idx
== (size_t)-1 || imo
->im6o_mfilters
== NULL
) {
1464 error
= EADDRNOTAVAIL
;
1465 goto out_imo_locked
;
1468 VERIFY(imo
->im6o_mfilters
!= NULL
);
1469 imf
= &imo
->im6o_mfilters
[idx
];
1470 inm
= imo
->im6o_membership
[idx
];
1473 * Attempting to use the delta-based API on an
1474 * non exclusive-mode membership is an error.
1476 fmode
= imf
->im6f_st
[0];
1477 if (fmode
!= MCAST_EXCLUDE
) {
1479 goto out_imo_locked
;
1483 * Deal with error cases up-front:
1484 * Asked to block, but already blocked; or
1485 * Asked to unblock, but nothing to unblock.
1486 * If adding a new block entry, allocate it.
1488 ims
= im6o_match_source(imo
, idx
, &ssa
->sa
);
1489 if ((ims
!= NULL
&& doblock
) || (ims
== NULL
&& !doblock
)) {
1490 MLD_PRINTF(("%s: source %s %spresent\n", __func__
,
1491 ip6_sprintf(&ssa
->sin6
.sin6_addr
),
1492 doblock
? "" : "not "));
1493 error
= EADDRNOTAVAIL
;
1494 goto out_imo_locked
;
1498 * Begin state merge transaction at socket layer.
1501 MLD_PRINTF(("%s: %s source\n", __func__
, "block"));
1502 ims
= im6f_graft(imf
, fmode
, &ssa
->sin6
);
1506 MLD_PRINTF(("%s: %s source\n", __func__
, "allow"));
1507 error
= im6f_prune(imf
, &ssa
->sin6
);
1511 MLD_PRINTF(("%s: merge imf state failed\n", __func__
));
1512 goto out_im6f_rollback
;
1516 * Begin state merge transaction at MLD layer.
1519 MLD_PRINTF(("%s: merge inm state\n", __func__
));
1520 error
= in6m_merge(inm
, imf
);
1522 MLD_PRINTF(("%s: failed to merge inm state\n", __func__
));
1524 goto out_im6f_rollback
;
1527 MLD_PRINTF(("%s: doing mld downcall\n", __func__
));
1528 error
= mld_change_state(inm
, 0);
1532 MLD_PRINTF(("%s: failed mld downcall\n", __func__
));
1545 IM6O_REMREF(imo
); /* from in6p_findmoptions() */
1550 * Given an inpcb, return its multicast options structure pointer. Accepts
1551 * an unlocked inpcb pointer, but will return it locked. May sleep.
1554 static struct ip6_moptions
*
1555 in6p_findmoptions(struct inpcb
*inp
)
1557 struct ip6_moptions
*imo
;
1558 struct in6_multi
**immp
;
1559 struct in6_mfilter
*imfp
;
1562 if ((imo
= inp
->in6p_moptions
) != NULL
) {
1563 IM6O_ADDREF(imo
); /* for caller */
1567 imo
= ip6_allocmoptions(M_WAITOK
);
1571 immp
= _MALLOC(sizeof (*immp
) * IPV6_MIN_MEMBERSHIPS
, M_IP6MOPTS
,
1578 imfp
= _MALLOC(sizeof (struct in6_mfilter
) * IPV6_MIN_MEMBERSHIPS
,
1579 M_IN6MFILTER
, M_WAITOK
| M_ZERO
);
1581 _FREE(immp
, M_IP6MOPTS
);
1586 imo
->im6o_multicast_ifp
= NULL
;
1587 imo
->im6o_multicast_hlim
= ip6_defmcasthlim
;
1588 imo
->im6o_multicast_loop
= in6_mcast_loop
;
1589 imo
->im6o_num_memberships
= 0;
1590 imo
->im6o_max_memberships
= IPV6_MIN_MEMBERSHIPS
;
1591 imo
->im6o_membership
= immp
;
1593 /* Initialize per-group source filters. */
1594 for (idx
= 0; idx
< IPV6_MIN_MEMBERSHIPS
; idx
++)
1595 im6f_init(&imfp
[idx
], MCAST_UNDEFINED
, MCAST_EXCLUDE
);
1597 imo
->im6o_mfilters
= imfp
;
1598 inp
->in6p_moptions
= imo
; /* keep reference from ip6_allocmoptions() */
1599 IM6O_ADDREF(imo
); /* for caller */
1605 * Atomically get source filters on a socket for an IPv6 multicast group.
1606 * Called with INP lock held; returns with lock released.
1609 in6p_get_source_filters(struct inpcb
*inp
, struct sockopt
*sopt
)
1611 struct __msfilterreq64 msfr
, msfr64
;
1612 struct __msfilterreq32 msfr32
;
1615 struct ip6_moptions
*imo
;
1616 struct in6_mfilter
*imf
;
1617 struct ip6_msource
*ims
;
1618 struct in6_msource
*lims
;
1619 struct sockaddr_in6
*psin
;
1620 struct sockaddr_storage
*ptss
;
1621 struct sockaddr_storage
*tss
;
1623 size_t idx
, nsrcs
, ncsrcs
;
1624 user_addr_t tmp_ptr
;
1626 imo
= inp
->in6p_moptions
;
1627 VERIFY(imo
!= NULL
);
1629 if (IS_64BIT_PROCESS(current_proc())) {
1630 error
= sooptcopyin(sopt
, &msfr64
,
1631 sizeof(struct __msfilterreq64
),
1632 sizeof(struct __msfilterreq64
));
1635 /* we never use msfr.msfr_srcs; */
1636 memcpy(&msfr
, &msfr64
, sizeof(msfr
));
1638 error
= sooptcopyin(sopt
, &msfr32
,
1639 sizeof(struct __msfilterreq32
),
1640 sizeof(struct __msfilterreq32
));
1643 /* we never use msfr.msfr_srcs; */
1644 memcpy(&msfr
, &msfr32
, sizeof(msfr
));
1647 if (msfr
.msfr_group
.ss_family
!= AF_INET6
||
1648 msfr
.msfr_group
.ss_len
!= sizeof(struct sockaddr_in6
))
1651 gsa
= (sockunion_t
*)&msfr
.msfr_group
;
1652 if (!IN6_IS_ADDR_MULTICAST(&gsa
->sin6
.sin6_addr
))
1655 ifnet_head_lock_shared();
1656 if (msfr
.msfr_ifindex
== 0 || (u_int
)if_index
< msfr
.msfr_ifindex
) {
1658 return (EADDRNOTAVAIL
);
1660 ifp
= ifindex2ifnet
[msfr
.msfr_ifindex
];
1664 return (EADDRNOTAVAIL
);
1666 if (msfr
.msfr_nsrcs
> in6_mcast_maxsocksrc
)
1667 msfr
.msfr_nsrcs
= in6_mcast_maxsocksrc
;
1669 (void)in6_setscope(&gsa
->sin6
.sin6_addr
, ifp
, NULL
);
1673 * Lookup group on the socket.
1675 idx
= im6o_match_group(imo
, ifp
, &gsa
->sa
);
1676 if (idx
== (size_t)-1 || imo
->im6o_mfilters
== NULL
) {
1678 return (EADDRNOTAVAIL
);
1680 imf
= &imo
->im6o_mfilters
[idx
];
1683 * Ignore memberships which are in limbo.
1685 if (imf
->im6f_st
[1] == MCAST_UNDEFINED
) {
1689 msfr
.msfr_fmode
= imf
->im6f_st
[1];
1692 * If the user specified a buffer, copy out the source filter
1693 * entries to userland gracefully.
1694 * We only copy out the number of entries which userland
1695 * has asked for, but we always tell userland how big the
1696 * buffer really needs to be.
1700 if (IS_64BIT_PROCESS(current_proc()))
1701 tmp_ptr
= msfr64
.msfr_srcs
;
1703 tmp_ptr
= CAST_USER_ADDR_T(msfr32
.msfr_srcs
);
1705 if (tmp_ptr
!= USER_ADDR_NULL
&& msfr
.msfr_nsrcs
> 0) {
1706 tss
= _MALLOC(sizeof(struct sockaddr_storage
) * msfr
.msfr_nsrcs
,
1707 M_TEMP
, M_WAITOK
| M_ZERO
);
1715 * Count number of sources in-mode at t0.
1716 * If buffer space exists and remains, copy out source entries.
1718 nsrcs
= msfr
.msfr_nsrcs
;
1721 RB_FOREACH(ims
, ip6_msource_tree
, &imf
->im6f_sources
) {
1722 lims
= (struct in6_msource
*)ims
;
1723 if (lims
->im6sl_st
[0] == MCAST_UNDEFINED
||
1724 lims
->im6sl_st
[0] != imf
->im6f_st
[0])
1726 if (tss
!= NULL
&& nsrcs
> 0) {
1727 psin
= (struct sockaddr_in6
*)ptss
;
1728 psin
->sin6_family
= AF_INET6
;
1729 psin
->sin6_len
= sizeof(struct sockaddr_in6
);
1730 psin
->sin6_addr
= lims
->im6s_addr
;
1731 psin
->sin6_port
= 0;
1741 error
= copyout(tss
, tmp_ptr
,
1742 sizeof(struct sockaddr_storage
) * msfr
.msfr_nsrcs
);
1748 msfr
.msfr_nsrcs
= ncsrcs
;
1749 if (IS_64BIT_PROCESS(current_proc())) {
1750 msfr64
.msfr_ifindex
= msfr
.msfr_ifindex
;
1751 msfr64
.msfr_fmode
= msfr
.msfr_fmode
;
1752 msfr64
.msfr_nsrcs
= msfr
.msfr_nsrcs
;
1753 memcpy(&msfr64
.msfr_group
, &msfr
.msfr_group
,
1754 sizeof(struct sockaddr_storage
));
1755 error
= sooptcopyout(sopt
, &msfr64
,
1756 sizeof(struct __msfilterreq64
));
1758 msfr32
.msfr_ifindex
= msfr
.msfr_ifindex
;
1759 msfr32
.msfr_fmode
= msfr
.msfr_fmode
;
1760 msfr32
.msfr_nsrcs
= msfr
.msfr_nsrcs
;
1761 memcpy(&msfr64
.msfr_group
, &msfr
.msfr_group
,
1762 sizeof(struct sockaddr_storage
));
1763 error
= sooptcopyout(sopt
, &msfr32
,
1764 sizeof(struct __msfilterreq32
));
1771 * Return the IP multicast options in response to user getsockopt().
1774 ip6_getmoptions(struct inpcb
*inp
, struct sockopt
*sopt
)
1776 struct ip6_moptions
*im6o
;
1780 im6o
= inp
->in6p_moptions
;
1782 * If socket is neither of type SOCK_RAW or SOCK_DGRAM,
1783 * or is a divert socket, reject it.
1785 if (inp
->inp_socket
->so_proto
->pr_protocol
== IPPROTO_DIVERT
||
1786 (inp
->inp_socket
->so_proto
->pr_type
!= SOCK_RAW
&&
1787 inp
->inp_socket
->so_proto
->pr_type
!= SOCK_DGRAM
)) {
1788 return (EOPNOTSUPP
);
1792 switch (sopt
->sopt_name
) {
1793 case IPV6_MULTICAST_IF
:
1796 if (im6o
== NULL
|| im6o
->im6o_multicast_ifp
== NULL
) {
1799 optval
= im6o
->im6o_multicast_ifp
->if_index
;
1803 error
= sooptcopyout(sopt
, &optval
, sizeof(u_int
));
1806 case IPV6_MULTICAST_HOPS
:
1808 optval
= ip6_defmcasthlim
;
1811 optval
= im6o
->im6o_multicast_hlim
;
1814 error
= sooptcopyout(sopt
, &optval
, sizeof(u_int
));
1817 case IPV6_MULTICAST_LOOP
:
1819 optval
= in6_mcast_loop
; /* XXX VIMAGE */
1822 optval
= im6o
->im6o_multicast_loop
;
1825 error
= sooptcopyout(sopt
, &optval
, sizeof(u_int
));
1830 error
= EADDRNOTAVAIL
;
1832 error
= in6p_get_source_filters(inp
, sopt
);
1837 error
= ENOPROTOOPT
;
1845 * Look up the ifnet to use for a multicast group membership,
1846 * given the address of an IPv6 group.
1848 * This routine exists to support legacy IPv6 multicast applications.
1850 * If inp is non-NULL and is bound to an interface, use this socket's
1851 * inp_boundif for any required routing table lookup.
1853 * If the route lookup fails, return NULL.
1855 * FUTURE: Support multiple forwarding tables for IPv6.
1857 * Returns NULL if no ifp could be found.
1859 static struct ifnet
*
1860 in6p_lookup_mcast_ifp(const struct inpcb
*in6p
,
1861 const struct sockaddr_in6
*gsin6
)
1863 struct route_in6 ro6
;
1865 unsigned int ifscope
= IFSCOPE_NONE
;
1867 VERIFY(in6p
== NULL
|| (in6p
->inp_vflag
& INP_IPV6
));
1868 VERIFY(gsin6
->sin6_family
== AF_INET6
);
1869 if (IN6_IS_ADDR_MULTICAST(&gsin6
->sin6_addr
) == 0)
1872 if (in6p
!= NULL
&& (in6p
->inp_flags
& INP_BOUND_IF
))
1873 ifscope
= in6p
->inp_boundif
;
1876 memset(&ro6
, 0, sizeof(struct route_in6
));
1877 memcpy(&ro6
.ro_dst
, gsin6
, sizeof(struct sockaddr_in6
));
1878 rtalloc_scoped_ign((struct route
*)&ro6
, 0, ifscope
);
1879 if (ro6
.ro_rt
!= NULL
) {
1880 ifp
= ro6
.ro_rt
->rt_ifp
;
1881 VERIFY(ifp
!= NULL
);
1889 * Since ipv6_mreq contains an ifindex and ip_mreq contains an AF_INET
1890 * address, we need to lookup the AF_INET address when translating an
1891 * ipv6_mreq structure into an ipmreq structure.
1892 * This is used when userland performs multicast setsockopt() on AF_INET6
1893 * sockets with AF_INET multicast addresses (IPv6 v4 mapped addresses).
1896 in6p_lookup_v4addr(struct ipv6_mreq
*mreq
, struct ip_mreq
*v4mreq
)
1900 struct sockaddr_in
*sin
;
1902 ifnet_head_lock_shared();
1903 if (mreq
->ipv6mr_interface
> (unsigned int)if_index
) {
1905 return (EADDRNOTAVAIL
);
1907 ifp
= ifindex2ifnet
[mreq
->ipv6mr_interface
];
1910 return (EADDRNOTAVAIL
);
1911 ifa
= ifa_ifpgetprimary(ifp
, AF_INET
);
1913 return (EADDRNOTAVAIL
);
1914 sin
= (struct sockaddr_in
*)ifa
->ifa_addr
;
1915 v4mreq
->imr_interface
.s_addr
= sin
->sin_addr
.s_addr
;
1922 * Join an IPv6 multicast group, possibly with a source.
1924 * FIXME: The KAME use of the unspecified address (::)
1925 * to join *all* multicast groups is currently unsupported.
1928 in6p_join_group(struct inpcb
*inp
, struct sockopt
*sopt
)
1930 struct group_source_req gsr
;
1931 sockunion_t
*gsa
, *ssa
;
1933 struct in6_mfilter
*imf
;
1934 struct ip6_moptions
*imo
;
1935 struct in6_multi
*inm
= NULL
;
1936 struct in6_msource
*lims
= NULL
;
1939 uint32_t scopeid
= 0;
1946 memset(&gsr
, 0, sizeof(struct group_source_req
));
1947 gsa
= (sockunion_t
*)&gsr
.gsr_group
;
1948 gsa
->ss
.ss_family
= AF_UNSPEC
;
1949 ssa
= (sockunion_t
*)&gsr
.gsr_source
;
1950 ssa
->ss
.ss_family
= AF_UNSPEC
;
1953 * Chew everything into struct group_source_req.
1954 * Overwrite the port field if present, as the sockaddr
1955 * being copied in may be matched with a binary comparison.
1956 * Ignore passed-in scope ID.
1958 switch (sopt
->sopt_name
) {
1959 case IPV6_JOIN_GROUP
: {
1960 struct ipv6_mreq mreq
;
1961 struct sockaddr_in6
*gsin6
;
1963 error
= sooptcopyin(sopt
, &mreq
, sizeof(struct ipv6_mreq
),
1964 sizeof(struct ipv6_mreq
));
1967 if (IN6_IS_ADDR_V4MAPPED(&mreq
.ipv6mr_multiaddr
)) {
1968 struct ip_mreq v4mreq
;
1969 struct sockopt v4sopt
;
1971 v4mreq
.imr_multiaddr
.s_addr
=
1972 mreq
.ipv6mr_multiaddr
.s6_addr32
[3];
1973 if (mreq
.ipv6mr_interface
== 0)
1974 v4mreq
.imr_interface
.s_addr
= INADDR_ANY
;
1976 error
= in6p_lookup_v4addr(&mreq
, &v4mreq
);
1979 v4sopt
.sopt_dir
= SOPT_SET
;
1980 v4sopt
.sopt_level
= sopt
->sopt_level
;
1981 v4sopt
.sopt_name
= IP_ADD_MEMBERSHIP
;
1982 v4sopt
.sopt_val
= CAST_USER_ADDR_T(&v4mreq
);
1983 v4sopt
.sopt_valsize
= sizeof(v4mreq
);
1984 v4sopt
.sopt_p
= kernproc
;
1986 return (inp_join_group(inp
, &v4sopt
));
1988 gsa
->sin6
.sin6_family
= AF_INET6
;
1989 gsa
->sin6
.sin6_len
= sizeof(struct sockaddr_in6
);
1990 gsa
->sin6
.sin6_addr
= mreq
.ipv6mr_multiaddr
;
1994 /* Only allow IPv6 multicast addresses */
1995 if (IN6_IS_ADDR_MULTICAST(&gsin6
->sin6_addr
) == 0) {
1999 if (mreq
.ipv6mr_interface
== 0) {
2000 ifp
= in6p_lookup_mcast_ifp(inp
, gsin6
);
2002 ifnet_head_lock_shared();
2003 if ((u_int
)if_index
< mreq
.ipv6mr_interface
) {
2005 return (EADDRNOTAVAIL
);
2007 ifp
= ifindex2ifnet
[mreq
.ipv6mr_interface
];
2010 MLD_PRINTF(("%s: ipv6mr_interface = %d, ifp = %p\n",
2011 __func__
, mreq
.ipv6mr_interface
, ifp
));
2015 case MCAST_JOIN_GROUP
:
2016 case MCAST_JOIN_SOURCE_GROUP
:
2017 if (sopt
->sopt_name
== MCAST_JOIN_GROUP
) {
2018 error
= sooptcopyin(sopt
, &gsr
,
2019 sizeof(struct group_req
),
2020 sizeof(struct group_req
));
2021 } else if (sopt
->sopt_name
== MCAST_JOIN_SOURCE_GROUP
) {
2022 error
= sooptcopyin(sopt
, &gsr
,
2023 sizeof(struct group_source_req
),
2024 sizeof(struct group_source_req
));
2029 if (gsa
->sin6
.sin6_family
!= AF_INET6
||
2030 gsa
->sin6
.sin6_len
!= sizeof(struct sockaddr_in6
))
2033 if (sopt
->sopt_name
== MCAST_JOIN_SOURCE_GROUP
) {
2034 if (ssa
->sin6
.sin6_family
!= AF_INET6
||
2035 ssa
->sin6
.sin6_len
!= sizeof(struct sockaddr_in6
))
2037 if (IN6_IS_ADDR_MULTICAST(&ssa
->sin6
.sin6_addr
))
2040 * TODO: Validate embedded scope ID in source
2041 * list entry against passed-in ifp, if and only
2042 * if source list filter entry is iface or node local.
2044 in6_clearscope(&ssa
->sin6
.sin6_addr
);
2045 ssa
->sin6
.sin6_port
= 0;
2046 ssa
->sin6
.sin6_scope_id
= 0;
2049 ifnet_head_lock_shared();
2050 if (gsr
.gsr_interface
== 0 ||
2051 (u_int
)if_index
< gsr
.gsr_interface
) {
2053 return (EADDRNOTAVAIL
);
2055 ifp
= ifindex2ifnet
[gsr
.gsr_interface
];
2060 MLD_PRINTF(("%s: unknown sopt_name %d\n",
2061 __func__
, sopt
->sopt_name
));
2062 return (EOPNOTSUPP
);
2066 if (!IN6_IS_ADDR_MULTICAST(&gsa
->sin6
.sin6_addr
))
2069 if (ifp
== NULL
|| (ifp
->if_flags
& IFF_MULTICAST
) == 0)
2070 return (EADDRNOTAVAIL
);
2072 gsa
->sin6
.sin6_port
= 0;
2073 gsa
->sin6
.sin6_scope_id
= 0;
2076 * Always set the scope zone ID on memberships created from userland.
2077 * Use the passed-in ifp to do this.
2079 (void)in6_setscope(&gsa
->sin6
.sin6_addr
, ifp
, &scopeid
);
2081 * Some addresses are not valid without an embedded scopeid.
2082 * This check must be present because otherwise we will later hit
2083 * a VERIFY() in in6_mc_join().
2085 if ((IN6_IS_ADDR_MC_LINKLOCAL(&gsa
->sin6
.sin6_addr
) ||
2086 IN6_IS_ADDR_MC_INTFACELOCAL(&gsa
->sin6
.sin6_addr
)) && scopeid
== 0)
2089 imo
= in6p_findmoptions(inp
);
2094 idx
= im6o_match_group(imo
, ifp
, &gsa
->sa
);
2095 if (idx
== (size_t)-1) {
2098 inm
= imo
->im6o_membership
[idx
];
2099 imf
= &imo
->im6o_mfilters
[idx
];
2100 if (ssa
->ss
.ss_family
!= AF_UNSPEC
) {
2102 * MCAST_JOIN_SOURCE_GROUP on an exclusive membership
2103 * is an error. On an existing inclusive membership,
2104 * it just adds the source to the filter list.
2106 if (imf
->im6f_st
[1] != MCAST_INCLUDE
) {
2108 goto out_imo_locked
;
2111 * Throw out duplicates.
2113 * XXX FIXME: This makes a naive assumption that
2114 * even if entries exist for *ssa in this imf,
2115 * they will be rejected as dupes, even if they
2116 * are not valid in the current mode (in-mode).
2118 * in6_msource is transactioned just as for anything
2119 * else in SSM -- but note naive use of in6m_graft()
2120 * below for allocating new filter entries.
2122 * This is only an issue if someone mixes the
2123 * full-state SSM API with the delta-based API,
2124 * which is discouraged in the relevant RFCs.
2126 lims
= im6o_match_source(imo
, idx
, &ssa
->sa
);
2127 if (lims
!= NULL
/*&&
2128 lims->im6sl_st[1] == MCAST_INCLUDE*/) {
2129 error
= EADDRNOTAVAIL
;
2130 goto out_imo_locked
;
2134 * MCAST_JOIN_GROUP on an existing exclusive
2135 * membership is an error; return EADDRINUSE
2136 * to preserve 4.4BSD API idempotence, and
2137 * avoid tedious detour to code below.
2138 * NOTE: This is bending RFC 3678 a bit.
2140 * On an existing inclusive membership, this is also
2141 * an error; if you want to change filter mode,
2142 * you must use the userland API setsourcefilter().
2143 * XXX We don't reject this for imf in UNDEFINED
2144 * state at t1, because allocation of a filter
2145 * is atomic with allocation of a membership.
2148 /* See comments above for EADDRINUSE */
2149 if (imf
->im6f_st
[1] == MCAST_EXCLUDE
)
2151 goto out_imo_locked
;
2156 * Begin state merge transaction at socket layer.
2160 if (imo
->im6o_num_memberships
== imo
->im6o_max_memberships
) {
2161 error
= im6o_grow(imo
, 0);
2163 goto out_imo_locked
;
2166 * Allocate the new slot upfront so we can deal with
2167 * grafting the new source filter in same code path
2168 * as for join-source on existing membership.
2170 idx
= imo
->im6o_num_memberships
;
2171 imo
->im6o_membership
[idx
] = NULL
;
2172 imo
->im6o_num_memberships
++;
2173 VERIFY(imo
->im6o_mfilters
!= NULL
);
2174 imf
= &imo
->im6o_mfilters
[idx
];
2175 VERIFY(RB_EMPTY(&imf
->im6f_sources
));
2179 * Graft new source into filter list for this inpcb's
2180 * membership of the group. The in6_multi may not have
2181 * been allocated yet if this is a new membership, however,
2182 * the in_mfilter slot will be allocated and must be initialized.
2184 * Note: Grafting of exclusive mode filters doesn't happen
2186 * XXX: Should check for non-NULL lims (node exists but may
2187 * not be in-mode) for interop with full-state API.
2189 if (ssa
->ss
.ss_family
!= AF_UNSPEC
) {
2190 /* Membership starts in IN mode */
2192 MLD_PRINTF(("%s: new join w/source\n", __func__
);
2193 im6f_init(imf
, MCAST_UNDEFINED
, MCAST_INCLUDE
));
2195 MLD_PRINTF(("%s: %s source\n", __func__
, "allow"));
2197 lims
= im6f_graft(imf
, MCAST_INCLUDE
, &ssa
->sin6
);
2199 MLD_PRINTF(("%s: merge imf state failed\n",
2205 /* No address specified; Membership starts in EX mode */
2207 MLD_PRINTF(("%s: new join w/o source", __func__
));
2208 im6f_init(imf
, MCAST_UNDEFINED
, MCAST_EXCLUDE
);
2213 * Begin state merge transaction at MLD layer.
2217 VERIFY(inm
== NULL
);
2218 error
= in6_mc_join(ifp
, &gsa
->sin6
.sin6_addr
, imf
, &inm
, 0);
2219 VERIFY(inm
!= NULL
|| error
!= 0);
2222 imo
->im6o_membership
[idx
] = inm
; /* from in6_mc_join() */
2224 MLD_PRINTF(("%s: merge inm state\n", __func__
));
2226 error
= in6m_merge(inm
, imf
);
2228 MLD_PRINTF(("%s: failed to merge inm state\n",
2231 goto out_im6f_rollback
;
2233 MLD_PRINTF(("%s: doing mld downcall\n", __func__
));
2234 error
= mld_change_state(inm
, 0);
2237 MLD_PRINTF(("%s: failed mld downcall\n",
2239 goto out_im6f_rollback
;
2255 if (error
&& is_new
) {
2256 VERIFY(inm
== NULL
);
2257 imo
->im6o_membership
[idx
] = NULL
;
2258 --imo
->im6o_num_memberships
;
2263 IM6O_REMREF(imo
); /* from in6p_findmoptions() */
2268 * Leave an IPv6 multicast group on an inpcb, possibly with a source.
2271 in6p_leave_group(struct inpcb
*inp
, struct sockopt
*sopt
)
2273 struct ipv6_mreq mreq
;
2274 struct group_source_req gsr
;
2275 sockunion_t
*gsa
, *ssa
;
2277 struct in6_mfilter
*imf
;
2278 struct ip6_moptions
*imo
;
2279 struct in6_msource
*ims
;
2280 struct in6_multi
*inm
= NULL
;
2281 uint32_t ifindex
= 0;
2283 int error
, is_final
;
2289 memset(&gsr
, 0, sizeof(struct group_source_req
));
2290 gsa
= (sockunion_t
*)&gsr
.gsr_group
;
2291 gsa
->ss
.ss_family
= AF_UNSPEC
;
2292 ssa
= (sockunion_t
*)&gsr
.gsr_source
;
2293 ssa
->ss
.ss_family
= AF_UNSPEC
;
2296 * Chew everything passed in up into a struct group_source_req
2297 * as that is easier to process.
2298 * Note: Any embedded scope ID in the multicast group passed
2299 * in by userland is ignored, the interface index is the recommended
2300 * mechanism to specify an interface; see below.
2302 switch (sopt
->sopt_name
) {
2303 case IPV6_LEAVE_GROUP
: {
2304 struct sockaddr_in6
*gsin6
;
2306 error
= sooptcopyin(sopt
, &mreq
, sizeof(struct ipv6_mreq
),
2307 sizeof(struct ipv6_mreq
));
2310 if (IN6_IS_ADDR_V4MAPPED(&mreq
.ipv6mr_multiaddr
)) {
2311 struct ip_mreq v4mreq
;
2312 struct sockopt v4sopt
;
2314 v4mreq
.imr_multiaddr
.s_addr
=
2315 mreq
.ipv6mr_multiaddr
.s6_addr32
[3];
2316 if (mreq
.ipv6mr_interface
== 0)
2317 v4mreq
.imr_interface
.s_addr
= INADDR_ANY
;
2319 error
= in6p_lookup_v4addr(&mreq
, &v4mreq
);
2322 v4sopt
.sopt_dir
= SOPT_SET
;
2323 v4sopt
.sopt_level
= sopt
->sopt_level
;
2324 v4sopt
.sopt_name
= IP_DROP_MEMBERSHIP
;
2325 v4sopt
.sopt_val
= CAST_USER_ADDR_T(&v4mreq
);
2326 v4sopt
.sopt_valsize
= sizeof(v4mreq
);
2327 v4sopt
.sopt_p
= kernproc
;
2329 return (inp_leave_group(inp
, &v4sopt
));
2331 gsa
->sin6
.sin6_family
= AF_INET6
;
2332 gsa
->sin6
.sin6_len
= sizeof(struct sockaddr_in6
);
2333 gsa
->sin6
.sin6_addr
= mreq
.ipv6mr_multiaddr
;
2334 gsa
->sin6
.sin6_port
= 0;
2335 gsa
->sin6
.sin6_scope_id
= 0;
2336 ifindex
= mreq
.ipv6mr_interface
;
2338 /* Only allow IPv6 multicast addresses */
2339 if (IN6_IS_ADDR_MULTICAST(&gsin6
->sin6_addr
) == 0) {
2345 case MCAST_LEAVE_GROUP
:
2346 case MCAST_LEAVE_SOURCE_GROUP
:
2347 if (sopt
->sopt_name
== MCAST_LEAVE_GROUP
) {
2348 error
= sooptcopyin(sopt
, &gsr
,
2349 sizeof(struct group_req
),
2350 sizeof(struct group_req
));
2351 } else if (sopt
->sopt_name
== MCAST_LEAVE_SOURCE_GROUP
) {
2352 error
= sooptcopyin(sopt
, &gsr
,
2353 sizeof(struct group_source_req
),
2354 sizeof(struct group_source_req
));
2359 if (gsa
->sin6
.sin6_family
!= AF_INET6
||
2360 gsa
->sin6
.sin6_len
!= sizeof(struct sockaddr_in6
))
2362 if (sopt
->sopt_name
== MCAST_LEAVE_SOURCE_GROUP
) {
2363 if (ssa
->sin6
.sin6_family
!= AF_INET6
||
2364 ssa
->sin6
.sin6_len
!= sizeof(struct sockaddr_in6
))
2366 if (IN6_IS_ADDR_MULTICAST(&ssa
->sin6
.sin6_addr
))
2369 * TODO: Validate embedded scope ID in source
2370 * list entry against passed-in ifp, if and only
2371 * if source list filter entry is iface or node local.
2373 in6_clearscope(&ssa
->sin6
.sin6_addr
);
2375 gsa
->sin6
.sin6_port
= 0;
2376 gsa
->sin6
.sin6_scope_id
= 0;
2377 ifindex
= gsr
.gsr_interface
;
2381 MLD_PRINTF(("%s: unknown sopt_name %d\n",
2382 __func__
, sopt
->sopt_name
));
2383 return (EOPNOTSUPP
);
2387 if (!IN6_IS_ADDR_MULTICAST(&gsa
->sin6
.sin6_addr
))
2391 * Validate interface index if provided. If no interface index
2392 * was provided separately, attempt to look the membership up
2393 * from the default scope as a last resort to disambiguate
2394 * the membership we are being asked to leave.
2395 * XXX SCOPE6 lock potentially taken here.
2398 ifnet_head_lock_shared();
2399 if ((u_int
)if_index
< ifindex
) {
2401 return (EADDRNOTAVAIL
);
2403 ifp
= ifindex2ifnet
[ifindex
];
2406 return (EADDRNOTAVAIL
);
2407 (void) in6_setscope(&gsa
->sin6
.sin6_addr
, ifp
, NULL
);
2409 error
= sa6_embedscope(&gsa
->sin6
, ip6_use_defzone
);
2411 return (EADDRNOTAVAIL
);
2413 * Some badly behaved applications don't pass an ifindex
2414 * or a scope ID, which is an API violation. In this case,
2415 * perform a lookup as per a v6 join.
2417 * XXX For now, stomp on zone ID for the corner case.
2418 * This is not the 'KAME way', but we need to see the ifp
2419 * directly until such time as this implementation is
2420 * refactored, assuming the scope IDs are the way to go.
2422 ifindex
= ntohs(gsa
->sin6
.sin6_addr
.s6_addr16
[1]);
2424 MLD_PRINTF(("%s: warning: no ifindex, looking up "
2425 "ifp for group %s.\n", __func__
,
2426 ip6_sprintf(&gsa
->sin6
.sin6_addr
)));
2427 ifp
= in6p_lookup_mcast_ifp(inp
, &gsa
->sin6
);
2429 ifnet_head_lock_shared();
2430 ifp
= ifindex2ifnet
[ifindex
];
2434 return (EADDRNOTAVAIL
);
2437 VERIFY(ifp
!= NULL
);
2438 MLD_PRINTF(("%s: ifp = %p\n", __func__
, ifp
));
2441 * Find the membership in the membership array.
2443 imo
= in6p_findmoptions(inp
);
2448 idx
= im6o_match_group(imo
, ifp
, &gsa
->sa
);
2449 if (idx
== (size_t)-1) {
2450 error
= EADDRNOTAVAIL
;
2453 inm
= imo
->im6o_membership
[idx
];
2454 imf
= &imo
->im6o_mfilters
[idx
];
2456 if (ssa
->ss
.ss_family
!= AF_UNSPEC
)
2460 * Begin state merge transaction at socket layer.
2464 * If we were instructed only to leave a given source, do so.
2465 * MCAST_LEAVE_SOURCE_GROUP is only valid for inclusive memberships.
2470 if (imf
->im6f_st
[0] == MCAST_EXCLUDE
) {
2471 error
= EADDRNOTAVAIL
;
2474 ims
= im6o_match_source(imo
, idx
, &ssa
->sa
);
2476 MLD_PRINTF(("%s: source %p %spresent\n", __func__
,
2477 ip6_sprintf(&ssa
->sin6
.sin6_addr
),
2479 error
= EADDRNOTAVAIL
;
2482 MLD_PRINTF(("%s: %s source\n", __func__
, "block"));
2483 error
= im6f_prune(imf
, &ssa
->sin6
);
2485 MLD_PRINTF(("%s: merge imf state failed\n",
2492 * Begin state merge transaction at MLD layer.
2497 * Give up the multicast address record to which
2498 * the membership points. Reference held in im6o
2499 * will be released below.
2501 (void) in6_mc_leave(inm
, imf
);
2503 MLD_PRINTF(("%s: merge inm state\n", __func__
));
2505 error
= in6m_merge(inm
, imf
);
2507 MLD_PRINTF(("%s: failed to merge inm state\n",
2510 goto out_im6f_rollback
;
2513 MLD_PRINTF(("%s: doing mld downcall\n", __func__
));
2514 error
= mld_change_state(inm
, 0);
2516 MLD_PRINTF(("%s: failed mld downcall\n", __func__
));
2530 /* Remove the gap in the membership array. */
2531 VERIFY(inm
== imo
->im6o_membership
[idx
]);
2532 imo
->im6o_membership
[idx
] = NULL
;
2534 for (++idx
; idx
< imo
->im6o_num_memberships
; ++idx
) {
2535 imo
->im6o_membership
[idx
-1] = imo
->im6o_membership
[idx
];
2536 imo
->im6o_mfilters
[idx
-1] = imo
->im6o_mfilters
[idx
];
2538 imo
->im6o_num_memberships
--;
2543 IM6O_REMREF(imo
); /* from in6p_findmoptions() */
2548 * Select the interface for transmitting IPv6 multicast datagrams.
2550 * Either an instance of struct in6_addr or an instance of struct ipv6_mreqn
2551 * may be passed to this socket option. An address of in6addr_any or an
2552 * interface index of 0 is used to remove a previous selection.
2553 * When no interface is selected, one is chosen for every send.
2556 in6p_set_multicast_if(struct inpcb
*inp
, struct sockopt
*sopt
)
2559 struct ip6_moptions
*imo
;
2563 if (sopt
->sopt_valsize
!= sizeof(u_int
))
2566 error
= sooptcopyin(sopt
, &ifindex
, sizeof(u_int
), sizeof(u_int
));
2570 ifnet_head_lock_shared();
2571 if ((u_int
)if_index
< ifindex
) {
2576 ifp
= ifindex2ifnet
[ifindex
];
2578 if (ifp
== NULL
|| (ifp
->if_flags
& IFF_MULTICAST
) == 0)
2579 return (EADDRNOTAVAIL
);
2581 imo
= in6p_findmoptions(inp
);
2586 imo
->im6o_multicast_ifp
= ifp
;
2588 IM6O_REMREF(imo
); /* from in6p_findmoptions() */
2594 * Atomically set source filters on a socket for an IPv6 multicast group.
2598 in6p_set_source_filters(struct inpcb
*inp
, struct sockopt
*sopt
)
2600 struct __msfilterreq64 msfr
, msfr64
;
2601 struct __msfilterreq32 msfr32
;
2604 struct in6_mfilter
*imf
;
2605 struct ip6_moptions
*imo
;
2606 struct in6_multi
*inm
;
2609 user_addr_t tmp_ptr
;
2611 if (IS_64BIT_PROCESS(current_proc())) {
2612 error
= sooptcopyin(sopt
, &msfr64
,
2613 sizeof(struct __msfilterreq64
),
2614 sizeof(struct __msfilterreq64
));
2617 /* we never use msfr.msfr_srcs; */
2618 memcpy(&msfr
, &msfr64
, sizeof(msfr
));
2620 error
= sooptcopyin(sopt
, &msfr32
,
2621 sizeof(struct __msfilterreq32
),
2622 sizeof(struct __msfilterreq32
));
2625 /* we never use msfr.msfr_srcs; */
2626 memcpy(&msfr
, &msfr32
, sizeof(msfr
));
2629 if (msfr
.msfr_nsrcs
> in6_mcast_maxsocksrc
)
2632 if (msfr
.msfr_fmode
!= MCAST_EXCLUDE
&&
2633 msfr
.msfr_fmode
!= MCAST_INCLUDE
)
2636 if (msfr
.msfr_group
.ss_family
!= AF_INET6
||
2637 msfr
.msfr_group
.ss_len
!= sizeof(struct sockaddr_in6
))
2640 gsa
= (sockunion_t
*)&msfr
.msfr_group
;
2641 if (!IN6_IS_ADDR_MULTICAST(&gsa
->sin6
.sin6_addr
))
2644 gsa
->sin6
.sin6_port
= 0; /* ignore port */
2646 ifnet_head_lock_shared();
2647 if (msfr
.msfr_ifindex
== 0 || (u_int
)if_index
< msfr
.msfr_ifindex
) {
2649 return (EADDRNOTAVAIL
);
2651 ifp
= ifindex2ifnet
[msfr
.msfr_ifindex
];
2654 return (EADDRNOTAVAIL
);
2656 (void)in6_setscope(&gsa
->sin6
.sin6_addr
, ifp
, NULL
);
2659 * Take the INP write lock.
2660 * Check if this socket is a member of this group.
2662 imo
= in6p_findmoptions(inp
);
2667 idx
= im6o_match_group(imo
, ifp
, &gsa
->sa
);
2668 if (idx
== (size_t)-1 || imo
->im6o_mfilters
== NULL
) {
2669 error
= EADDRNOTAVAIL
;
2670 goto out_imo_locked
;
2672 inm
= imo
->im6o_membership
[idx
];
2673 imf
= &imo
->im6o_mfilters
[idx
];
2676 * Begin state merge transaction at socket layer.
2679 imf
->im6f_st
[1] = msfr
.msfr_fmode
;
2682 * Apply any new source filters, if present.
2683 * Make a copy of the user-space source vector so
2684 * that we may copy them with a single copyin. This
2685 * allows us to deal with page faults up-front.
2687 if (msfr
.msfr_nsrcs
> 0) {
2688 struct in6_msource
*lims
;
2689 struct sockaddr_in6
*psin
;
2690 struct sockaddr_storage
*kss
, *pkss
;
2693 if (IS_64BIT_PROCESS(current_proc()))
2694 tmp_ptr
= msfr64
.msfr_srcs
;
2696 tmp_ptr
= CAST_USER_ADDR_T(msfr32
.msfr_srcs
);
2698 MLD_PRINTF(("%s: loading %lu source list entries\n",
2699 __func__
, (unsigned long)msfr
.msfr_nsrcs
));
2700 kss
= _MALLOC(sizeof(struct sockaddr_storage
) * msfr
.msfr_nsrcs
,
2704 goto out_imo_locked
;
2707 error
= copyin(tmp_ptr
, kss
,
2708 sizeof(struct sockaddr_storage
) * msfr
.msfr_nsrcs
);
2711 goto out_imo_locked
;
2715 * Mark all source filters as UNDEFINED at t1.
2716 * Restore new group filter mode, as im6f_leave()
2717 * will set it to INCLUDE.
2720 imf
->im6f_st
[1] = msfr
.msfr_fmode
;
2723 * Update socket layer filters at t1, lazy-allocating
2724 * new entries. This saves a bunch of memory at the
2725 * cost of one RB_FIND() per source entry; duplicate
2726 * entries in the msfr_nsrcs vector are ignored.
2727 * If we encounter an error, rollback transaction.
2729 * XXX This too could be replaced with a set-symmetric
2730 * difference like loop to avoid walking from root
2731 * every time, as the key space is common.
2733 for (i
= 0, pkss
= kss
; i
< msfr
.msfr_nsrcs
; i
++, pkss
++) {
2734 psin
= (struct sockaddr_in6
*)pkss
;
2735 if (psin
->sin6_family
!= AF_INET6
) {
2736 error
= EAFNOSUPPORT
;
2739 if (psin
->sin6_len
!= sizeof(struct sockaddr_in6
)) {
2743 if (IN6_IS_ADDR_MULTICAST(&psin
->sin6_addr
)) {
2748 * TODO: Validate embedded scope ID in source
2749 * list entry against passed-in ifp, if and only
2750 * if source list filter entry is iface or node local.
2752 in6_clearscope(&psin
->sin6_addr
);
2753 error
= im6f_get_source(imf
, psin
, &lims
);
2756 lims
->im6sl_st
[1] = imf
->im6f_st
[1];
2762 goto out_im6f_rollback
;
2765 * Begin state merge transaction at MLD layer.
2768 MLD_PRINTF(("%s: merge inm state\n", __func__
));
2769 error
= in6m_merge(inm
, imf
);
2771 MLD_PRINTF(("%s: failed to merge inm state\n", __func__
));
2773 goto out_im6f_rollback
;
2776 MLD_PRINTF(("%s: doing mld downcall\n", __func__
));
2777 error
= mld_change_state(inm
, 0);
2781 MLD_PRINTF(("%s: failed mld downcall\n", __func__
));
2794 IM6O_REMREF(imo
); /* from in6p_findmoptions() */
2800 * Set the IP multicast options in response to user setsockopt().
2802 * Many of the socket options handled in this function duplicate the
2803 * functionality of socket options in the regular unicast API. However,
2804 * it is not possible to merge the duplicate code, because the idempotence
2805 * of the IPv6 multicast part of the BSD Sockets API must be preserved;
2806 * the effects of these options must be treated as separate and distinct.
2810 ip6_setmoptions(struct inpcb
*inp
, struct sockopt
*sopt
)
2812 struct ip6_moptions
*im6o
;
2818 * If socket is neither of type SOCK_RAW or SOCK_DGRAM,
2819 * or is a divert socket, reject it.
2821 if (inp
->inp_socket
->so_proto
->pr_protocol
== IPPROTO_DIVERT
||
2822 (inp
->inp_socket
->so_proto
->pr_type
!= SOCK_RAW
&&
2823 inp
->inp_socket
->so_proto
->pr_type
!= SOCK_DGRAM
))
2824 return (EOPNOTSUPP
);
2826 switch (sopt
->sopt_name
) {
2827 case IPV6_MULTICAST_IF
:
2828 error
= in6p_set_multicast_if(inp
, sopt
);
2831 case IPV6_MULTICAST_HOPS
: {
2834 if (sopt
->sopt_valsize
!= sizeof(int)) {
2838 error
= sooptcopyin(sopt
, &hlim
, sizeof(hlim
), sizeof(int));
2841 if (hlim
< -1 || hlim
> 255) {
2844 } else if (hlim
== -1) {
2845 hlim
= ip6_defmcasthlim
;
2847 im6o
= in6p_findmoptions(inp
);
2853 im6o
->im6o_multicast_hlim
= hlim
;
2855 IM6O_REMREF(im6o
); /* from in6p_findmoptions() */
2859 case IPV6_MULTICAST_LOOP
: {
2863 * Set the loopback flag for outgoing multicast packets.
2864 * Must be zero or one.
2866 if (sopt
->sopt_valsize
!= sizeof(u_int
)) {
2870 error
= sooptcopyin(sopt
, &loop
, sizeof(u_int
), sizeof(u_int
));
2877 im6o
= in6p_findmoptions(inp
);
2883 im6o
->im6o_multicast_loop
= loop
;
2885 IM6O_REMREF(im6o
); /* from in6p_findmoptions() */
2889 case IPV6_JOIN_GROUP
:
2890 case MCAST_JOIN_GROUP
:
2891 case MCAST_JOIN_SOURCE_GROUP
:
2892 error
= in6p_join_group(inp
, sopt
);
2895 case IPV6_LEAVE_GROUP
:
2896 case MCAST_LEAVE_GROUP
:
2897 case MCAST_LEAVE_SOURCE_GROUP
:
2898 error
= in6p_leave_group(inp
, sopt
);
2901 case MCAST_BLOCK_SOURCE
:
2902 case MCAST_UNBLOCK_SOURCE
:
2903 error
= in6p_block_unblock_source(inp
, sopt
);
2907 error
= in6p_set_source_filters(inp
, sopt
);
2918 * Expose MLD's multicast filter mode and source list(s) to userland,
2919 * keyed by (ifindex, group).
2920 * The filter mode is written out as a uint32_t, followed by
2921 * 0..n of struct in6_addr.
2922 * For use by ifmcstat(8).
2925 sysctl_ip6_mcast_filters SYSCTL_HANDLER_ARGS
2927 #pragma unused(oidp)
2929 struct in6_addr mcaddr
;
2930 struct in6_addr src
;
2932 struct in6_multi
*inm
;
2933 struct in6_multistep step
;
2934 struct ip6_msource
*ims
;
2938 uint32_t fmode
, ifindex
;
2943 if (req
->newptr
!= USER_ADDR_NULL
)
2946 /* int: ifindex + 4 * 32 bits of IPv6 address */
2951 ifnet_head_lock_shared();
2952 if (ifindex
<= 0 || ifindex
> (u_int
)if_index
) {
2953 MLD_PRINTF(("%s: ifindex %u out of range\n",
2954 __func__
, ifindex
));
2959 memcpy(&mcaddr
, &name
[1], sizeof(struct in6_addr
));
2960 if (!IN6_IS_ADDR_MULTICAST(&mcaddr
)) {
2961 MLD_PRINTF(("%s: group %s is not multicast\n",
2962 __func__
, ip6_sprintf(&mcaddr
)));
2967 ifp
= ifindex2ifnet
[ifindex
];
2970 MLD_PRINTF(("%s: no ifp for ifindex %u\n", __func__
, ifindex
));
2974 * Internal MLD lookups require that scope/zone ID is set.
2976 (void)in6_setscope(&mcaddr
, ifp
, NULL
);
2978 in6_multihead_lock_shared();
2979 IN6_FIRST_MULTI(step
, inm
);
2980 while (inm
!= NULL
) {
2982 if (inm
->in6m_ifp
!= ifp
)
2985 if (!IN6_ARE_ADDR_EQUAL(&inm
->in6m_addr
, &mcaddr
))
2988 fmode
= inm
->in6m_st
[1].iss_fmode
;
2989 retval
= SYSCTL_OUT(req
, &fmode
, sizeof(uint32_t));
2994 RB_FOREACH(ims
, ip6_msource_tree
, &inm
->in6m_srcs
) {
2995 MLD_PRINTF(("%s: visit node %p\n", __func__
, ims
));
2997 * Only copy-out sources which are in-mode.
2999 if (fmode
!= im6s_get_mode(inm
, ims
, 1)) {
3000 MLD_PRINTF(("%s: skip non-in-mode\n",
3002 continue; /* process next source */
3004 src
= ims
->im6s_addr
;
3005 retval
= SYSCTL_OUT(req
, &src
, sizeof(struct in6_addr
));
3007 break; /* process next inm */
3011 IN6_NEXT_MULTI(step
, inm
);
3013 in6_multihead_lock_done();
3019 in6_multi_init(void)
3021 PE_parse_boot_argn("ifa_debug", &in6m_debug
, sizeof (in6m_debug
));
3023 /* Setup lock group and attribute for in6_multihead */
3024 in6_multihead_lock_grp_attr
= lck_grp_attr_alloc_init();
3025 in6_multihead_lock_grp
= lck_grp_alloc_init("in6_multihead",
3026 in6_multihead_lock_grp_attr
);
3027 in6_multihead_lock_attr
= lck_attr_alloc_init();
3028 lck_rw_init(&in6_multihead_lock
, in6_multihead_lock_grp
,
3029 in6_multihead_lock_attr
);
3031 lck_mtx_init(&in6m_trash_lock
, in6_multihead_lock_grp
,
3032 in6_multihead_lock_attr
);
3033 TAILQ_INIT(&in6m_trash_head
);
3035 in6m_size
= (in6m_debug
== 0) ? sizeof (struct in6_multi
) :
3036 sizeof (struct in6_multi_dbg
);
3037 in6m_zone
= zinit(in6m_size
, IN6M_ZONE_MAX
* in6m_size
,
3039 if (in6m_zone
== NULL
) {
3040 panic("%s: failed allocating %s", __func__
, IN6M_ZONE_NAME
);
3043 zone_change(in6m_zone
, Z_EXPAND
, TRUE
);
3045 imm_size
= sizeof (struct in6_multi_mship
);
3046 imm_zone
= zinit(imm_size
, IMM_ZONE_MAX
* imm_size
, 0, IMM_ZONE_NAME
);
3047 if (imm_zone
== NULL
) {
3048 panic("%s: failed allocating %s", __func__
, IMM_ZONE_NAME
);
3051 zone_change(imm_zone
, Z_EXPAND
, TRUE
);
3053 ip6ms_size
= sizeof (struct ip6_msource
);
3054 ip6ms_zone
= zinit(ip6ms_size
, IP6MS_ZONE_MAX
* ip6ms_size
,
3055 0, IP6MS_ZONE_NAME
);
3056 if (ip6ms_zone
== NULL
) {
3057 panic("%s: failed allocating %s", __func__
, IP6MS_ZONE_NAME
);
3060 zone_change(ip6ms_zone
, Z_EXPAND
, TRUE
);
3062 in6ms_size
= sizeof (struct in6_msource
);
3063 in6ms_zone
= zinit(in6ms_size
, IN6MS_ZONE_MAX
* in6ms_size
,
3064 0, IN6MS_ZONE_NAME
);
3065 if (in6ms_zone
== NULL
) {
3066 panic("%s: failed allocating %s", __func__
, IN6MS_ZONE_NAME
);
3069 zone_change(in6ms_zone
, Z_EXPAND
, TRUE
);
3072 static struct in6_multi
*
3073 in6_multi_alloc(int how
)
3075 struct in6_multi
*in6m
;
3077 in6m
= (how
== M_WAITOK
) ? zalloc(in6m_zone
) :
3078 zalloc_noblock(in6m_zone
);
3080 bzero(in6m
, in6m_size
);
3081 lck_mtx_init(&in6m
->in6m_lock
, in6_multihead_lock_grp
,
3082 in6_multihead_lock_attr
);
3083 in6m
->in6m_debug
|= IFD_ALLOC
;
3084 if (in6m_debug
!= 0) {
3085 in6m
->in6m_debug
|= IFD_DEBUG
;
3086 in6m
->in6m_trace
= in6m_trace
;
3093 in6_multi_free(struct in6_multi
*in6m
)
3096 if (in6m
->in6m_debug
& IFD_ATTACHED
) {
3097 panic("%s: attached in6m=%p is being freed", __func__
, in6m
);
3099 } else if (in6m
->in6m_ifma
!= NULL
) {
3100 panic("%s: ifma not NULL for in6m=%p", __func__
, in6m
);
3102 } else if (!(in6m
->in6m_debug
& IFD_ALLOC
)) {
3103 panic("%s: in6m %p cannot be freed", __func__
, in6m
);
3105 } else if (in6m
->in6m_refcount
!= 0) {
3106 panic("%s: non-zero refcount in6m=%p", __func__
, in6m
);
3108 } else if (in6m
->in6m_reqcnt
!= 0) {
3109 panic("%s: non-zero reqcnt in6m=%p", __func__
, in6m
);
3113 /* Free any pending MLDv2 state-change records */
3114 IF_DRAIN(&in6m
->in6m_scq
);
3116 in6m
->in6m_debug
&= ~IFD_ALLOC
;
3117 if ((in6m
->in6m_debug
& (IFD_DEBUG
| IFD_TRASHED
)) ==
3118 (IFD_DEBUG
| IFD_TRASHED
)) {
3119 lck_mtx_lock(&in6m_trash_lock
);
3120 TAILQ_REMOVE(&in6m_trash_head
, (struct in6_multi_dbg
*)in6m
,
3122 lck_mtx_unlock(&in6m_trash_lock
);
3123 in6m
->in6m_debug
&= ~IFD_TRASHED
;
3127 lck_mtx_destroy(&in6m
->in6m_lock
, in6_multihead_lock_grp
);
3128 zfree(in6m_zone
, in6m
);
3132 in6_multi_attach(struct in6_multi
*in6m
)
3134 in6_multihead_lock_assert(LCK_RW_ASSERT_EXCLUSIVE
);
3135 IN6M_LOCK_ASSERT_HELD(in6m
);
3137 if (in6m
->in6m_debug
& IFD_ATTACHED
) {
3138 panic("%s: Attempt to attach an already attached in6m=%p",
3141 } else if (in6m
->in6m_debug
& IFD_TRASHED
) {
3142 panic("%s: Attempt to reattach a detached in6m=%p",
3147 in6m
->in6m_reqcnt
++;
3148 VERIFY(in6m
->in6m_reqcnt
== 1);
3149 IN6M_ADDREF_LOCKED(in6m
);
3150 in6m
->in6m_debug
|= IFD_ATTACHED
;
3152 * Reattach case: If debugging is enabled, take it
3153 * out of the trash list and clear IFD_TRASHED.
3155 if ((in6m
->in6m_debug
& (IFD_DEBUG
| IFD_TRASHED
)) ==
3156 (IFD_DEBUG
| IFD_TRASHED
)) {
3157 /* Become a regular mutex, just in case */
3158 IN6M_CONVERT_LOCK(in6m
);
3159 lck_mtx_lock(&in6m_trash_lock
);
3160 TAILQ_REMOVE(&in6m_trash_head
, (struct in6_multi_dbg
*)in6m
,
3162 lck_mtx_unlock(&in6m_trash_lock
);
3163 in6m
->in6m_debug
&= ~IFD_TRASHED
;
3166 LIST_INSERT_HEAD(&in6_multihead
, in6m
, in6m_entry
);
3170 in6_multi_detach(struct in6_multi
*in6m
)
3172 in6_multihead_lock_assert(LCK_RW_ASSERT_EXCLUSIVE
);
3173 IN6M_LOCK_ASSERT_HELD(in6m
);
3175 if (in6m
->in6m_reqcnt
== 0) {
3176 panic("%s: in6m=%p negative reqcnt", __func__
, in6m
);
3180 --in6m
->in6m_reqcnt
;
3181 if (in6m
->in6m_reqcnt
> 0)
3184 if (!(in6m
->in6m_debug
& IFD_ATTACHED
)) {
3185 panic("%s: Attempt to detach an unattached record in6m=%p",
3188 } else if (in6m
->in6m_debug
& IFD_TRASHED
) {
3189 panic("%s: in6m %p is already in trash list", __func__
, in6m
);
3194 * NOTE: Caller calls IFMA_REMREF
3196 in6m
->in6m_debug
&= ~IFD_ATTACHED
;
3197 LIST_REMOVE(in6m
, in6m_entry
);
3199 if (in6m
->in6m_debug
& IFD_DEBUG
) {
3200 /* Become a regular mutex, just in case */
3201 IN6M_CONVERT_LOCK(in6m
);
3202 lck_mtx_lock(&in6m_trash_lock
);
3203 TAILQ_INSERT_TAIL(&in6m_trash_head
,
3204 (struct in6_multi_dbg
*)in6m
, in6m_trash_link
);
3205 lck_mtx_unlock(&in6m_trash_lock
);
3206 in6m
->in6m_debug
|= IFD_TRASHED
;
3213 in6m_addref(struct in6_multi
*in6m
, int locked
)
3216 IN6M_LOCK_SPIN(in6m
);
3218 IN6M_LOCK_ASSERT_HELD(in6m
);
3220 if (++in6m
->in6m_refcount
== 0) {
3221 panic("%s: in6m=%p wraparound refcnt", __func__
, in6m
);
3223 } else if (in6m
->in6m_trace
!= NULL
) {
3224 (*in6m
->in6m_trace
)(in6m
, TRUE
);
3231 in6m_remref(struct in6_multi
*in6m
, int locked
)
3233 struct ifmultiaddr
*ifma
;
3234 struct mld_ifinfo
*mli
;
3237 IN6M_LOCK_SPIN(in6m
);
3239 IN6M_LOCK_ASSERT_HELD(in6m
);
3241 if (in6m
->in6m_refcount
== 0 || (in6m
->in6m_refcount
== 1 && locked
)) {
3242 panic("%s: in6m=%p negative refcnt", __func__
, in6m
);
3244 } else if (in6m
->in6m_trace
!= NULL
) {
3245 (*in6m
->in6m_trace
)(in6m
, FALSE
);
3248 --in6m
->in6m_refcount
;
3249 if (in6m
->in6m_refcount
> 0) {
3256 * Synchronization with in6_mc_get(). In the event the in6m has been
3257 * detached, the underlying ifma would still be in the if_multiaddrs
3258 * list, and thus can be looked up via if_addmulti(). At that point,
3259 * the only way to find this in6m is via ifma_protospec. To avoid
3260 * race conditions between the last in6m_remref() of that in6m and its
3261 * use via ifma_protospec, in6_multihead lock is used for serialization.
3262 * In order to avoid violating the lock order, we must drop in6m_lock
3263 * before acquiring in6_multihead lock. To prevent the in6m from being
3264 * freed prematurely, we hold an extra reference.
3266 ++in6m
->in6m_refcount
;
3268 in6_multihead_lock_shared();
3269 IN6M_LOCK_SPIN(in6m
);
3270 --in6m
->in6m_refcount
;
3271 if (in6m
->in6m_refcount
> 0) {
3272 /* We've lost the race, so abort since in6m is still in use */
3274 in6_multihead_lock_done();
3275 /* If it was locked, return it as such */
3281 ifma
= in6m
->in6m_ifma
;
3282 in6m
->in6m_ifma
= NULL
;
3283 in6m
->in6m_ifp
= NULL
;
3284 mli
= in6m
->in6m_mli
;
3285 in6m
->in6m_mli
= NULL
;
3287 IFMA_LOCK_SPIN(ifma
);
3288 ifma
->ifma_protospec
= NULL
;
3290 in6_multihead_lock_done();
3292 in6_multi_free(in6m
);
3293 if_delmulti_ifma(ifma
);
3294 /* Release reference held to the underlying ifmultiaddr */
3302 in6m_trace(struct in6_multi
*in6m
, int refhold
)
3304 struct in6_multi_dbg
*in6m_dbg
= (struct in6_multi_dbg
*)in6m
;
3309 if (!(in6m
->in6m_debug
& IFD_DEBUG
)) {
3310 panic("%s: in6m %p has no debug structure", __func__
, in6m
);
3314 cnt
= &in6m_dbg
->in6m_refhold_cnt
;
3315 tr
= in6m_dbg
->in6m_refhold
;
3317 cnt
= &in6m_dbg
->in6m_refrele_cnt
;
3318 tr
= in6m_dbg
->in6m_refrele
;
3321 idx
= atomic_add_16_ov(cnt
, 1) % IN6M_TRACE_HIST_SIZE
;
3322 ctrace_record(&tr
[idx
]);
3325 static struct in6_multi_mship
*
3326 in6_multi_mship_alloc(int how
)
3328 struct in6_multi_mship
*imm
;
3330 imm
= (how
== M_WAITOK
) ? zalloc(imm_zone
) : zalloc_noblock(imm_zone
);
3332 bzero(imm
, imm_size
);
3338 in6_multi_mship_free(struct in6_multi_mship
*imm
)
3340 if (imm
->i6mm_maddr
!= NULL
) {
3341 panic("%s: i6mm_maddr not NULL for imm=%p", __func__
, imm
);
3344 zfree(imm_zone
, imm
);
3348 in6_multihead_lock_exclusive(void)
3350 lck_rw_lock_exclusive(&in6_multihead_lock
);
3354 in6_multihead_lock_shared(void)
3356 lck_rw_lock_shared(&in6_multihead_lock
);
3360 in6_multihead_lock_assert(int what
)
3362 lck_rw_assert(&in6_multihead_lock
, what
);
3366 in6_multihead_lock_done(void)
3368 lck_rw_done(&in6_multihead_lock
);
3371 static struct ip6_msource
*
3372 ip6ms_alloc(int how
)
3374 struct ip6_msource
*i6ms
;
3376 i6ms
= (how
== M_WAITOK
) ? zalloc(ip6ms_zone
) :
3377 zalloc_noblock(ip6ms_zone
);
3379 bzero(i6ms
, ip6ms_size
);
3385 ip6ms_free(struct ip6_msource
*i6ms
)
3387 zfree(ip6ms_zone
, i6ms
);
3390 static struct in6_msource
*
3391 in6ms_alloc(int how
)
3393 struct in6_msource
*in6ms
;
3395 in6ms
= (how
== M_WAITOK
) ? zalloc(in6ms_zone
) :
3396 zalloc_noblock(in6ms_zone
);
3398 bzero(in6ms
, in6ms_size
);
3404 in6ms_free(struct in6_msource
*in6ms
)
3406 zfree(in6ms_zone
, in6ms
);
3411 static const char *in6m_modestrs
[] = { "un\n", "in", "ex" };
3414 in6m_mode_str(const int mode
)
3416 if (mode
>= MCAST_UNDEFINED
&& mode
<= MCAST_EXCLUDE
)
3417 return (in6m_modestrs
[mode
]);
3421 static const char *in6m_statestrs
[] = {
3429 "sg-query-pending\n",
3434 in6m_state_str(const int state
)
3436 if (state
>= MLD_NOT_MEMBER
&& state
<= MLD_LEAVING_MEMBER
)
3437 return (in6m_statestrs
[state
]);
3442 * Dump an in6_multi structure to the console.
3445 in6m_print(const struct in6_multi
*inm
)
3449 IN6M_LOCK_ASSERT_HELD(IN6M_CAST_TO_NONCONST(inm
));
3454 printf("%s: --- begin in6m %p ---\n", __func__
, inm
);
3455 printf("addr %s ifp %p(%s%d) ifma %p\n",
3456 ip6_sprintf(&inm
->in6m_addr
),
3458 inm
->in6m_ifp
->if_name
,
3459 inm
->in6m_ifp
->if_unit
,
3461 printf("timer %u state %s refcount %u scq.len %u\n",
3463 in6m_state_str(inm
->in6m_state
),
3465 inm
->in6m_scq
.ifq_len
);
3466 printf("mli %p nsrc %lu sctimer %u scrv %u\n",
3471 for (t
= 0; t
< 2; t
++) {
3472 printf("t%d: fmode %s asm %u ex %u in %u rec %u\n", t
,
3473 in6m_mode_str(inm
->in6m_st
[t
].iss_fmode
),
3474 inm
->in6m_st
[t
].iss_asm
,
3475 inm
->in6m_st
[t
].iss_ex
,
3476 inm
->in6m_st
[t
].iss_in
,
3477 inm
->in6m_st
[t
].iss_rec
);
3479 printf("%s: --- end in6m %p ---\n", __func__
, inm
);
3485 in6m_print(__unused
const struct in6_multi
*inm
)