2 * Copyright (c) 2010-2020 Apple Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
29 * Copyright (c) 2007-2009 Bruce Simpson.
30 * Copyright (c) 2005 Robert N. M. Watson.
31 * All rights reserved.
33 * Redistribution and use in source and binary forms, with or without
34 * modification, are permitted provided that the following conditions
36 * 1. Redistributions of source code must retain the above copyright
37 * notice, this list of conditions and the following disclaimer.
38 * 2. Redistributions in binary form must reproduce the above copyright
39 * notice, this list of conditions and the following disclaimer in the
40 * documentation and/or other materials provided with the distribution.
41 * 3. The name of the author may not be used to endorse or promote
42 * products derived from this software without specific prior written
45 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
46 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
47 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
48 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
49 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
50 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
51 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
52 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
53 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
54 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
59 * IPv4 multicast socket, group, and socket option processing module.
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_systm.h>
88 #include <netinet/in_pcb.h>
89 #include <netinet/in_var.h>
90 #include <netinet/ip_var.h>
91 #include <netinet/igmp_var.h>
94 * Functions with non-static linkage defined in this file should be
95 * declared in in_var.h:
106 * XXX: Both carp and pf need to use the legacy (*,G) KPIs in_addmulti()
109 static void imf_commit(struct in_mfilter
*);
110 static int imf_get_source(struct in_mfilter
*imf
,
111 const struct sockaddr_in
*psin
,
112 struct in_msource
**);
113 static struct in_msource
*
114 imf_graft(struct in_mfilter
*, const uint8_t,
115 const struct sockaddr_in
*);
116 static int imf_prune(struct in_mfilter
*, const struct sockaddr_in
*);
117 static void imf_rollback(struct in_mfilter
*);
118 static void imf_reap(struct in_mfilter
*);
119 static int imo_grow(struct ip_moptions
*, uint16_t);
120 static size_t imo_match_group(const struct ip_moptions
*,
121 const struct ifnet
*, const struct sockaddr_in
*);
122 static struct in_msource
*
123 imo_match_source(const struct ip_moptions
*, const size_t,
124 const struct sockaddr_in
*);
125 static void ims_merge(struct ip_msource
*ims
,
126 const struct in_msource
*lims
, const int rollback
);
127 static int in_getmulti(struct ifnet
*, const struct in_addr
*,
129 static int in_joingroup(struct ifnet
*, const struct in_addr
*,
130 struct in_mfilter
*, struct in_multi
**);
131 static int inm_get_source(struct in_multi
*inm
, const in_addr_t haddr
,
132 const int noalloc
, struct ip_msource
**pims
);
133 static int inm_is_ifp_detached(const struct in_multi
*);
134 static int inm_merge(struct in_multi
*, /*const*/ struct in_mfilter
*);
135 static void inm_reap(struct in_multi
*);
136 static struct ip_moptions
*
137 inp_findmoptions(struct inpcb
*);
138 static int inp_get_source_filters(struct inpcb
*, struct sockopt
*);
139 static struct ifnet
*
140 inp_lookup_mcast_ifp(const struct inpcb
*,
141 const struct sockaddr_in
*, const struct in_addr
);
142 static int inp_block_unblock_source(struct inpcb
*, struct sockopt
*);
143 static int inp_set_multicast_if(struct inpcb
*, struct sockopt
*);
144 static int inp_set_source_filters(struct inpcb
*, struct sockopt
*);
145 static int sysctl_ip_mcast_filters SYSCTL_HANDLER_ARGS
;
146 static struct ifnet
* ip_multicast_if(struct in_addr
*, unsigned int *);
147 static __inline__
int ip_msource_cmp(const struct ip_msource
*,
148 const struct ip_msource
*);
150 SYSCTL_NODE(_net_inet_ip
, OID_AUTO
, mcast
, CTLFLAG_RW
| CTLFLAG_LOCKED
, 0, "IPv4 multicast");
152 static u_long in_mcast_maxgrpsrc
= IP_MAX_GROUP_SRC_FILTER
;
153 SYSCTL_LONG(_net_inet_ip_mcast
, OID_AUTO
, maxgrpsrc
,
154 CTLFLAG_RW
| CTLFLAG_LOCKED
, &in_mcast_maxgrpsrc
, "Max source filters per group");
156 static u_int in_mcast_maxsocksrc
= IP_MAX_SOCK_SRC_FILTER
;
157 SYSCTL_UINT(_net_inet_ip_mcast
, OID_AUTO
, maxsocksrc
,
158 CTLFLAG_RW
| CTLFLAG_LOCKED
, &in_mcast_maxsocksrc
, IP_MAX_SOCK_SRC_FILTER
,
159 "Max source filters per socket");
161 int in_mcast_loop
= IP_DEFAULT_MULTICAST_LOOP
;
162 SYSCTL_INT(_net_inet_ip_mcast
, OID_AUTO
, loop
, CTLFLAG_RW
| CTLFLAG_LOCKED
,
163 &in_mcast_loop
, 0, "Loopback multicast datagrams by default");
165 SYSCTL_NODE(_net_inet_ip_mcast
, OID_AUTO
, filters
,
166 CTLFLAG_RD
| CTLFLAG_LOCKED
, sysctl_ip_mcast_filters
,
167 "Per-interface stack-wide source filters");
169 RB_GENERATE_PREV(ip_msource_tree
, ip_msource
, ims_link
, ip_msource_cmp
);
171 #define INM_TRACE_HIST_SIZE 32 /* size of trace history */
174 __private_extern__
unsigned int inm_trace_hist_size
= INM_TRACE_HIST_SIZE
;
176 struct in_multi_dbg
{
177 struct in_multi inm
; /* in_multi */
178 u_int16_t inm_refhold_cnt
; /* # of ref */
179 u_int16_t inm_refrele_cnt
; /* # of rele */
181 * Circular lists of inm_addref and inm_remref callers.
183 ctrace_t inm_refhold
[INM_TRACE_HIST_SIZE
];
184 ctrace_t inm_refrele
[INM_TRACE_HIST_SIZE
];
188 TAILQ_ENTRY(in_multi_dbg
) inm_trash_link
;
191 /* List of trash in_multi entries protected by inm_trash_lock */
192 static TAILQ_HEAD(, in_multi_dbg
) inm_trash_head
;
193 static decl_lck_mtx_data(, inm_trash_lock
);
197 static unsigned int inm_debug
= 1; /* debugging (enabled) */
199 static unsigned int inm_debug
; /* debugging (disabled) */
201 #define INM_ZONE_NAME "in_multi" /* zone name */
202 static struct zone
*inm_zone
; /* zone for in_multi */
204 static ZONE_DECLARE(ipms_zone
, "ip_msource", sizeof(struct ip_msource
),
206 static ZONE_DECLARE(inms_zone
, "in_msource", sizeof(struct in_msource
),
209 /* Lock group and attribute for in_multihead_lock lock */
210 static lck_attr_t
*in_multihead_lock_attr
;
211 static lck_grp_t
*in_multihead_lock_grp
;
212 static lck_grp_attr_t
*in_multihead_lock_grp_attr
;
214 static decl_lck_rw_data(, in_multihead_lock
);
215 struct in_multihead in_multihead
;
217 static struct in_multi
*in_multi_alloc(zalloc_flags_t
);
218 static void in_multi_free(struct in_multi
*);
219 static void in_multi_attach(struct in_multi
*);
220 static void inm_trace(struct in_multi
*, int);
222 static struct ip_msource
*ipms_alloc(zalloc_flags_t
);
223 static void ipms_free(struct ip_msource
*);
224 static struct in_msource
*inms_alloc(zalloc_flags_t
);
225 static void inms_free(struct in_msource
*);
228 ip_msource_cmp(const struct ip_msource
*a
, const struct ip_msource
*b
)
230 if (a
->ims_haddr
< b
->ims_haddr
) {
233 if (a
->ims_haddr
== b
->ims_haddr
) {
240 * Inline function which wraps assertions for a valid ifp.
242 static __inline__
int
243 inm_is_ifp_detached(const struct in_multi
*inm
)
245 VERIFY(inm
->inm_ifma
!= NULL
);
246 VERIFY(inm
->inm_ifp
== inm
->inm_ifma
->ifma_ifp
);
248 return !ifnet_is_attached(inm
->inm_ifp
, 0);
252 * Initialize an in_mfilter structure to a known state at t0, t1
253 * with an empty source filter list.
255 static __inline__
void
256 imf_init(struct in_mfilter
*imf
, const uint8_t st0
, const uint8_t st1
)
258 memset(imf
, 0, sizeof(struct in_mfilter
));
259 RB_INIT(&imf
->imf_sources
);
260 imf
->imf_st
[0] = st0
;
261 imf
->imf_st
[1] = st1
;
265 * Resize the ip_moptions vector to the next power-of-two minus 1.
268 imo_grow(struct ip_moptions
*imo
, uint16_t newmax
)
270 struct in_multi
**nmships
;
271 struct in_multi
**omships
;
272 struct in_mfilter
*nmfilters
;
273 struct in_mfilter
*omfilters
;
277 IMO_LOCK_ASSERT_HELD(imo
);
281 omships
= imo
->imo_membership
;
282 omfilters
= imo
->imo_mfilters
;
283 oldmax
= imo
->imo_max_memberships
;
285 newmax
= ((oldmax
+ 1) * 2) - 1;
288 if (newmax
> IP_MAX_MEMBERSHIPS
) {
292 if ((nmships
= (struct in_multi
**)_REALLOC(omships
,
293 sizeof(struct in_multi
*) * newmax
, M_IPMOPTS
,
294 M_WAITOK
| M_ZERO
)) == NULL
) {
298 imo
->imo_membership
= nmships
;
300 if ((nmfilters
= (struct in_mfilter
*)_REALLOC(omfilters
,
301 sizeof(struct in_mfilter
) * newmax
, M_INMFILTER
,
302 M_WAITOK
| M_ZERO
)) == NULL
) {
306 imo
->imo_mfilters
= nmfilters
;
308 /* Initialize newly allocated source filter heads. */
309 for (idx
= oldmax
; idx
< newmax
; idx
++) {
310 imf_init(&nmfilters
[idx
], MCAST_UNDEFINED
, MCAST_EXCLUDE
);
313 imo
->imo_max_memberships
= newmax
;
319 * Find an IPv4 multicast group entry for this ip_moptions instance
320 * which matches the specified group, and optionally an interface.
321 * Return its index into the array, or -1 if not found.
324 imo_match_group(const struct ip_moptions
*imo
, const struct ifnet
*ifp
,
325 const struct sockaddr_in
*group
)
327 struct in_multi
*pinm
;
331 IMO_LOCK_ASSERT_HELD(__DECONST(struct ip_moptions
*, imo
));
334 /* The imo_membership array may be lazy allocated. */
335 if (imo
->imo_membership
== NULL
|| imo
->imo_num_memberships
== 0) {
339 nmships
= imo
->imo_num_memberships
;
340 for (idx
= 0; idx
< nmships
; idx
++) {
341 pinm
= imo
->imo_membership
[idx
];
346 if ((ifp
== NULL
|| (pinm
->inm_ifp
== ifp
)) &&
347 in_hosteq(pinm
->inm_addr
, group
->sin_addr
)) {
353 if (idx
>= nmships
) {
361 * Find an IPv4 multicast source entry for this imo which matches
362 * the given group index for this socket, and source address.
364 * NOTE: This does not check if the entry is in-mode, merely if
365 * it exists, which may not be the desired behaviour.
367 static struct in_msource
*
368 imo_match_source(const struct ip_moptions
*imo
, const size_t gidx
,
369 const struct sockaddr_in
*src
)
371 struct ip_msource find
;
372 struct in_mfilter
*imf
;
373 struct ip_msource
*ims
;
375 IMO_LOCK_ASSERT_HELD(__DECONST(struct ip_moptions
*, imo
));
377 VERIFY(src
->sin_family
== AF_INET
);
378 VERIFY(gidx
!= (size_t)-1 && gidx
< imo
->imo_num_memberships
);
380 /* The imo_mfilters array may be lazy allocated. */
381 if (imo
->imo_mfilters
== NULL
) {
384 imf
= &imo
->imo_mfilters
[gidx
];
386 /* Source trees are keyed in host byte order. */
387 find
.ims_haddr
= ntohl(src
->sin_addr
.s_addr
);
388 ims
= RB_FIND(ip_msource_tree
, &imf
->imf_sources
, &find
);
390 return (struct in_msource
*)ims
;
394 * Perform filtering for multicast datagrams on a socket by group and source.
396 * Returns 0 if a datagram should be allowed through, or various error codes
397 * if the socket was not a member of the group, or the source was muted, etc.
400 imo_multi_filter(const struct ip_moptions
*imo
, const struct ifnet
*ifp
,
401 const struct sockaddr_in
*group
, const struct sockaddr_in
*src
)
404 struct in_msource
*ims
;
407 IMO_LOCK_ASSERT_HELD(__DECONST(struct ip_moptions
*, imo
));
410 gidx
= imo_match_group(imo
, ifp
, group
);
411 if (gidx
== (size_t)-1) {
412 return MCAST_NOTGMEMBER
;
416 * Check if the source was included in an (S,G) join.
417 * Allow reception on exclusive memberships by default,
418 * reject reception on inclusive memberships by default.
419 * Exclude source only if an in-mode exclude filter exists.
420 * Include source only if an in-mode include filter exists.
421 * NOTE: We are comparing group state here at IGMP t1 (now)
422 * with socket-layer t0 (since last downcall).
424 mode
= imo
->imo_mfilters
[gidx
].imf_st
[1];
425 ims
= imo_match_source(imo
, gidx
, src
);
427 if ((ims
== NULL
&& mode
== MCAST_INCLUDE
) ||
428 (ims
!= NULL
&& ims
->imsl_st
[0] != mode
)) {
429 return MCAST_NOTSMEMBER
;
436 imo_clone(struct inpcb
*from_inp
, struct inpcb
*to_inp
)
439 struct ip_moptions
*from
;
440 struct ip_moptions
*to
;
442 from
= inp_findmoptions(from_inp
);
447 to
= inp_findmoptions(to_inp
);
456 to
->imo_multicast_ifp
= from
->imo_multicast_ifp
;
457 to
->imo_multicast_vif
= from
->imo_multicast_vif
;
458 to
->imo_multicast_ttl
= from
->imo_multicast_ttl
;
459 to
->imo_multicast_loop
= from
->imo_multicast_loop
;
462 * We're cloning, so drop any existing memberships and source
463 * filters on the destination ip_moptions.
465 for (i
= 0; i
< to
->imo_num_memberships
; ++i
) {
466 struct in_mfilter
*imf
;
468 imf
= to
->imo_mfilters
? &to
->imo_mfilters
[i
] : NULL
;
473 (void) in_leavegroup(to
->imo_membership
[i
], imf
);
479 INM_REMREF(to
->imo_membership
[i
]);
480 to
->imo_membership
[i
] = NULL
;
482 to
->imo_num_memberships
= 0;
484 VERIFY(to
->imo_max_memberships
!= 0 && from
->imo_max_memberships
!= 0);
485 if (to
->imo_max_memberships
< from
->imo_max_memberships
) {
487 * Ensure source and destination ip_moptions memberships
488 * and source filters arrays are at least equal in size.
490 err
= imo_grow(to
, from
->imo_max_memberships
);
495 VERIFY(to
->imo_max_memberships
>= from
->imo_max_memberships
);
498 * Source filtering doesn't apply to OpenTransport socket,
499 * so simply hold additional reference count per membership.
501 for (i
= 0; i
< from
->imo_num_memberships
; i
++) {
502 to
->imo_membership
[i
] =
503 in_addmulti(&from
->imo_membership
[i
]->inm_addr
,
504 from
->imo_membership
[i
]->inm_ifp
);
505 if (to
->imo_membership
[i
] == NULL
) {
508 to
->imo_num_memberships
++;
510 VERIFY(to
->imo_num_memberships
== from
->imo_num_memberships
);
522 * Find and return a reference to an in_multi record for (ifp, group),
523 * and bump its reference count.
524 * If one does not exist, try to allocate it, and update link-layer multicast
525 * filters on ifp to listen for group.
526 * Return 0 if successful, otherwise return an appropriate error code.
529 in_getmulti(struct ifnet
*ifp
, const struct in_addr
*group
,
530 struct in_multi
**pinm
)
532 struct sockaddr_in gsin
;
533 struct ifmultiaddr
*ifma
;
534 struct in_multi
*inm
;
537 in_multihead_lock_shared();
538 IN_LOOKUP_MULTI(group
, ifp
, inm
);
541 VERIFY(inm
->inm_reqcnt
>= 1);
543 VERIFY(inm
->inm_reqcnt
!= 0);
546 in_multihead_lock_done();
548 * We already joined this group; return the inm
549 * with a refcount held (via lookup) for caller.
553 in_multihead_lock_done();
555 bzero(&gsin
, sizeof(gsin
));
556 gsin
.sin_family
= AF_INET
;
557 gsin
.sin_len
= sizeof(struct sockaddr_in
);
558 gsin
.sin_addr
= *group
;
561 * Check if a link-layer group is already associated
562 * with this network-layer group on the given ifnet.
564 error
= if_addmulti(ifp
, (struct sockaddr
*)&gsin
, &ifma
);
570 * See comments in inm_remref() for access to ifma_protospec.
572 in_multihead_lock_exclusive();
574 if ((inm
= ifma
->ifma_protospec
) != NULL
) {
575 VERIFY(ifma
->ifma_addr
!= NULL
);
576 VERIFY(ifma
->ifma_addr
->sa_family
== AF_INET
);
577 INM_ADDREF(inm
); /* for caller */
580 VERIFY(inm
->inm_ifma
== ifma
);
581 VERIFY(inm
->inm_ifp
== ifp
);
582 VERIFY(in_hosteq(inm
->inm_addr
, *group
));
583 if (inm
->inm_debug
& IFD_ATTACHED
) {
584 VERIFY(inm
->inm_reqcnt
>= 1);
586 VERIFY(inm
->inm_reqcnt
!= 0);
589 in_multihead_lock_done();
592 * We lost the race with another thread doing
593 * in_getmulti(); since this group has already
594 * been joined; return the inm with a refcount
600 * We lost the race with another thread doing in_delmulti();
601 * the inm referring to the ifma has been detached, thus we
602 * reattach it back to the in_multihead list and return the
603 * inm with a refcount held for the caller.
605 in_multi_attach(inm
);
606 VERIFY((inm
->inm_debug
&
607 (IFD_ATTACHED
| IFD_TRASHED
)) == IFD_ATTACHED
);
610 in_multihead_lock_done();
617 * A new in_multi record is needed; allocate and initialize it.
618 * We DO NOT perform an IGMP join as the in_ layer may need to
619 * push an initial source list down to IGMP to support SSM.
621 * The initial source filter state is INCLUDE, {} as per the RFC.
623 inm
= in_multi_alloc(Z_WAITOK
);
626 inm
->inm_addr
= *group
;
628 inm
->inm_igi
= IGMP_IFINFO(ifp
);
629 VERIFY(inm
->inm_igi
!= NULL
);
630 IGI_ADDREF(inm
->inm_igi
);
631 inm
->inm_ifma
= ifma
; /* keep refcount from if_addmulti() */
632 inm
->inm_state
= IGMP_NOT_MEMBER
;
634 * Pending state-changes per group are subject to a bounds check.
636 inm
->inm_scq
.ifq_maxlen
= IGMP_MAX_STATE_CHANGES
;
637 inm
->inm_st
[0].iss_fmode
= MCAST_UNDEFINED
;
638 inm
->inm_st
[1].iss_fmode
= MCAST_UNDEFINED
;
639 RB_INIT(&inm
->inm_srcs
);
641 in_multi_attach(inm
);
642 VERIFY((inm
->inm_debug
& (IFD_ATTACHED
| IFD_TRASHED
)) == IFD_ATTACHED
);
643 INM_ADDREF_LOCKED(inm
); /* for caller */
647 VERIFY(ifma
->ifma_protospec
== NULL
);
648 ifma
->ifma_protospec
= inm
;
650 in_multihead_lock_done();
656 * Clear recorded source entries for a group.
657 * Used by the IGMP code.
658 * FIXME: Should reap.
661 inm_clear_recorded(struct in_multi
*inm
)
663 struct ip_msource
*ims
;
665 INM_LOCK_ASSERT_HELD(inm
);
667 RB_FOREACH(ims
, ip_msource_tree
, &inm
->inm_srcs
) {
670 --inm
->inm_st
[1].iss_rec
;
673 VERIFY(inm
->inm_st
[1].iss_rec
== 0);
677 * Record a source as pending for a Source-Group IGMPv3 query.
678 * This lives here as it modifies the shared tree.
680 * inm is the group descriptor.
681 * naddr is the address of the source to record in network-byte order.
683 * If the net.inet.igmp.sgalloc sysctl is non-zero, we will
684 * lazy-allocate a source node in response to an SG query.
685 * Otherwise, no allocation is performed. This saves some memory
686 * with the trade-off that the source will not be reported to the
687 * router if joined in the window between the query response and
688 * the group actually being joined on the local host.
690 * Return 0 if the source didn't exist or was already marked as recorded.
691 * Return 1 if the source was marked as recorded by this function.
692 * Return <0 if any error occured (negated errno code).
695 inm_record_source(struct in_multi
*inm
, const in_addr_t naddr
)
697 struct ip_msource find
;
698 struct ip_msource
*ims
, *nims
;
700 INM_LOCK_ASSERT_HELD(inm
);
702 find
.ims_haddr
= ntohl(naddr
);
703 ims
= RB_FIND(ip_msource_tree
, &inm
->inm_srcs
, &find
);
704 if (ims
&& ims
->ims_stp
) {
708 if (inm
->inm_nsrc
== in_mcast_maxgrpsrc
) {
711 nims
= ipms_alloc(Z_WAITOK
);
712 nims
->ims_haddr
= find
.ims_haddr
;
713 RB_INSERT(ip_msource_tree
, &inm
->inm_srcs
, nims
);
719 * Mark the source as recorded and update the recorded
723 ++inm
->inm_st
[1].iss_rec
;
729 * Return a pointer to an in_msource owned by an in_mfilter,
730 * given its source address.
731 * Lazy-allocate if needed. If this is a new entry its filter state is
734 * imf is the filter set being modified.
735 * haddr is the source address in *host* byte-order.
737 * Caller is expected to be holding imo_lock.
740 imf_get_source(struct in_mfilter
*imf
, const struct sockaddr_in
*psin
,
741 struct in_msource
**plims
)
743 struct ip_msource find
;
744 struct ip_msource
*ims
;
745 struct in_msource
*lims
;
752 /* key is host byte order */
753 find
.ims_haddr
= ntohl(psin
->sin_addr
.s_addr
);
754 ims
= RB_FIND(ip_msource_tree
, &imf
->imf_sources
, &find
);
755 lims
= (struct in_msource
*)ims
;
757 if (imf
->imf_nsrc
== in_mcast_maxsocksrc
) {
760 lims
= inms_alloc(Z_WAITOK
);
761 lims
->ims_haddr
= find
.ims_haddr
;
762 lims
->imsl_st
[0] = MCAST_UNDEFINED
;
763 RB_INSERT(ip_msource_tree
, &imf
->imf_sources
,
764 (struct ip_msource
*)lims
);
774 * Graft a source entry into an existing socket-layer filter set,
775 * maintaining any required invariants and checking allocations.
777 * The source is marked as being in the new filter mode at t1.
779 * Return the pointer to the new node, otherwise return NULL.
781 * Caller is expected to be holding imo_lock.
783 static struct in_msource
*
784 imf_graft(struct in_mfilter
*imf
, const uint8_t st1
,
785 const struct sockaddr_in
*psin
)
787 struct in_msource
*lims
;
789 lims
= inms_alloc(Z_WAITOK
);
790 lims
->ims_haddr
= ntohl(psin
->sin_addr
.s_addr
);
791 lims
->imsl_st
[0] = MCAST_UNDEFINED
;
792 lims
->imsl_st
[1] = st1
;
793 RB_INSERT(ip_msource_tree
, &imf
->imf_sources
,
794 (struct ip_msource
*)lims
);
801 * Prune a source entry from an existing socket-layer filter set,
802 * maintaining any required invariants and checking allocations.
804 * The source is marked as being left at t1, it is not freed.
806 * Return 0 if no error occurred, otherwise return an errno value.
808 * Caller is expected to be holding imo_lock.
811 imf_prune(struct in_mfilter
*imf
, const struct sockaddr_in
*psin
)
813 struct ip_msource find
;
814 struct ip_msource
*ims
;
815 struct in_msource
*lims
;
817 /* key is host byte order */
818 find
.ims_haddr
= ntohl(psin
->sin_addr
.s_addr
);
819 ims
= RB_FIND(ip_msource_tree
, &imf
->imf_sources
, &find
);
823 lims
= (struct in_msource
*)ims
;
824 lims
->imsl_st
[1] = MCAST_UNDEFINED
;
829 * Revert socket-layer filter set deltas at t1 to t0 state.
831 * Caller is expected to be holding imo_lock.
834 imf_rollback(struct in_mfilter
*imf
)
836 struct ip_msource
*ims
, *tims
;
837 struct in_msource
*lims
;
839 RB_FOREACH_SAFE(ims
, ip_msource_tree
, &imf
->imf_sources
, tims
) {
840 lims
= (struct in_msource
*)ims
;
841 if (lims
->imsl_st
[0] == lims
->imsl_st
[1]) {
842 /* no change at t1 */
844 } else if (lims
->imsl_st
[0] != MCAST_UNDEFINED
) {
845 /* revert change to existing source at t1 */
846 lims
->imsl_st
[1] = lims
->imsl_st
[0];
848 /* revert source added t1 */
849 IGMP_PRINTF(("%s: free inms 0x%llx\n", __func__
,
850 (uint64_t)VM_KERNEL_ADDRPERM(lims
)));
851 RB_REMOVE(ip_msource_tree
, &imf
->imf_sources
, ims
);
856 imf
->imf_st
[1] = imf
->imf_st
[0];
860 * Mark socket-layer filter set as INCLUDE {} at t1.
862 * Caller is expected to be holding imo_lock.
865 imf_leave(struct in_mfilter
*imf
)
867 struct ip_msource
*ims
;
868 struct in_msource
*lims
;
870 RB_FOREACH(ims
, ip_msource_tree
, &imf
->imf_sources
) {
871 lims
= (struct in_msource
*)ims
;
872 lims
->imsl_st
[1] = MCAST_UNDEFINED
;
874 imf
->imf_st
[1] = MCAST_INCLUDE
;
878 * Mark socket-layer filter set deltas as committed.
880 * Caller is expected to be holding imo_lock.
883 imf_commit(struct in_mfilter
*imf
)
885 struct ip_msource
*ims
;
886 struct in_msource
*lims
;
888 RB_FOREACH(ims
, ip_msource_tree
, &imf
->imf_sources
) {
889 lims
= (struct in_msource
*)ims
;
890 lims
->imsl_st
[0] = lims
->imsl_st
[1];
892 imf
->imf_st
[0] = imf
->imf_st
[1];
896 * Reap unreferenced sources from socket-layer filter set.
898 * Caller is expected to be holding imo_lock.
901 imf_reap(struct in_mfilter
*imf
)
903 struct ip_msource
*ims
, *tims
;
904 struct in_msource
*lims
;
906 RB_FOREACH_SAFE(ims
, ip_msource_tree
, &imf
->imf_sources
, tims
) {
907 lims
= (struct in_msource
*)ims
;
908 if ((lims
->imsl_st
[0] == MCAST_UNDEFINED
) &&
909 (lims
->imsl_st
[1] == MCAST_UNDEFINED
)) {
910 IGMP_PRINTF(("%s: free inms 0x%llx\n", __func__
,
911 (uint64_t)VM_KERNEL_ADDRPERM(lims
)));
912 RB_REMOVE(ip_msource_tree
, &imf
->imf_sources
, ims
);
920 * Purge socket-layer filter set.
922 * Caller is expected to be holding imo_lock.
925 imf_purge(struct in_mfilter
*imf
)
927 struct ip_msource
*ims
, *tims
;
928 struct in_msource
*lims
;
930 RB_FOREACH_SAFE(ims
, ip_msource_tree
, &imf
->imf_sources
, tims
) {
931 lims
= (struct in_msource
*)ims
;
932 IGMP_PRINTF(("%s: free inms 0x%llx\n", __func__
,
933 (uint64_t)VM_KERNEL_ADDRPERM(lims
)));
934 RB_REMOVE(ip_msource_tree
, &imf
->imf_sources
, ims
);
938 imf
->imf_st
[0] = imf
->imf_st
[1] = MCAST_UNDEFINED
;
939 VERIFY(RB_EMPTY(&imf
->imf_sources
));
943 * Look up a source filter entry for a multicast group.
945 * inm is the group descriptor to work with.
946 * haddr is the host-byte-order IPv4 address to look up.
947 * noalloc may be non-zero to suppress allocation of sources.
948 * *pims will be set to the address of the retrieved or allocated source.
950 * Return 0 if successful, otherwise return a non-zero error code.
953 inm_get_source(struct in_multi
*inm
, const in_addr_t haddr
,
954 const int noalloc
, struct ip_msource
**pims
)
956 struct ip_msource find
;
957 struct ip_msource
*ims
, *nims
;
960 char buf
[MAX_IPv4_STR_LEN
];
962 INM_LOCK_ASSERT_HELD(inm
);
964 find
.ims_haddr
= haddr
;
965 ims
= RB_FIND(ip_msource_tree
, &inm
->inm_srcs
, &find
);
966 if (ims
== NULL
&& !noalloc
) {
967 if (inm
->inm_nsrc
== in_mcast_maxgrpsrc
) {
970 nims
= ipms_alloc(Z_WAITOK
);
971 nims
->ims_haddr
= haddr
;
972 RB_INSERT(ip_msource_tree
, &inm
->inm_srcs
, nims
);
976 ia
.s_addr
= htonl(haddr
);
977 inet_ntop(AF_INET
, &ia
, buf
, sizeof(buf
));
978 IGMP_PRINTF(("%s: allocated %s as 0x%llx\n", __func__
,
979 buf
, (uint64_t)VM_KERNEL_ADDRPERM(ims
)));
988 * Helper function to derive the filter mode on a source entry
989 * from its internal counters. Predicates are:
990 * A source is only excluded if all listeners exclude it.
991 * A source is only included if no listeners exclude it,
992 * and at least one listener includes it.
993 * May be used by ifmcstat(8).
996 ims_get_mode(const struct in_multi
*inm
, const struct ip_msource
*ims
,
999 INM_LOCK_ASSERT_HELD(__DECONST(struct in_multi
*, inm
));
1002 if (inm
->inm_st
[t
].iss_ex
> 0 &&
1003 inm
->inm_st
[t
].iss_ex
== ims
->ims_st
[t
].ex
) {
1004 return MCAST_EXCLUDE
;
1005 } else if (ims
->ims_st
[t
].in
> 0 && ims
->ims_st
[t
].ex
== 0) {
1006 return MCAST_INCLUDE
;
1008 return MCAST_UNDEFINED
;
1012 * Merge socket-layer source into IGMP-layer source.
1013 * If rollback is non-zero, perform the inverse of the merge.
1016 ims_merge(struct ip_msource
*ims
, const struct in_msource
*lims
,
1019 int n
= rollback
? -1 : 1;
1023 ia
.s_addr
= htonl(ims
->ims_haddr
);
1026 if (lims
->imsl_st
[0] == MCAST_EXCLUDE
) {
1027 IGMP_INET_PRINTF(ia
,
1028 ("%s: t1 ex -= %d on %s\n",
1029 __func__
, n
, _igmp_inet_buf
));
1030 ims
->ims_st
[1].ex
-= n
;
1031 } else if (lims
->imsl_st
[0] == MCAST_INCLUDE
) {
1032 IGMP_INET_PRINTF(ia
,
1033 ("%s: t1 in -= %d on %s\n",
1034 __func__
, n
, _igmp_inet_buf
));
1035 ims
->ims_st
[1].in
-= n
;
1038 if (lims
->imsl_st
[1] == MCAST_EXCLUDE
) {
1039 IGMP_INET_PRINTF(ia
,
1040 ("%s: t1 ex += %d on %s\n",
1041 __func__
, n
, _igmp_inet_buf
));
1042 ims
->ims_st
[1].ex
+= n
;
1043 } else if (lims
->imsl_st
[1] == MCAST_INCLUDE
) {
1044 IGMP_INET_PRINTF(ia
,
1045 ("%s: t1 in += %d on %s\n",
1046 __func__
, n
, _igmp_inet_buf
));
1047 ims
->ims_st
[1].in
+= n
;
1052 * Atomically update the global in_multi state, when a membership's
1053 * filter list is being updated in any way.
1055 * imf is the per-inpcb-membership group filter pointer.
1056 * A fake imf may be passed for in-kernel consumers.
1058 * XXX This is a candidate for a set-symmetric-difference style loop
1059 * which would eliminate the repeated lookup from root of ims nodes,
1060 * as they share the same key space.
1062 * If any error occurred this function will back out of refcounts
1063 * and return a non-zero value.
1066 inm_merge(struct in_multi
*inm
, /*const*/ struct in_mfilter
*imf
)
1068 struct ip_msource
*ims
, *nims
= NULL
;
1069 struct in_msource
*lims
;
1070 int schanged
, error
;
1073 INM_LOCK_ASSERT_HELD(inm
);
1080 * Update the source filters first, as this may fail.
1081 * Maintain count of in-mode filters at t0, t1. These are
1082 * used to work out if we transition into ASM mode or not.
1083 * Maintain a count of source filters whose state was
1084 * actually modified by this operation.
1086 RB_FOREACH(ims
, ip_msource_tree
, &imf
->imf_sources
) {
1087 lims
= (struct in_msource
*)ims
;
1088 if (lims
->imsl_st
[0] == imf
->imf_st
[0]) {
1091 if (lims
->imsl_st
[1] == imf
->imf_st
[1]) {
1094 if (lims
->imsl_st
[0] == lims
->imsl_st
[1]) {
1097 error
= inm_get_source(inm
, lims
->ims_haddr
, 0, &nims
);
1102 ims_merge(nims
, lims
, 0);
1105 struct ip_msource
*bims
;
1107 RB_FOREACH_REVERSE_FROM(ims
, ip_msource_tree
, nims
) {
1108 lims
= (struct in_msource
*)ims
;
1109 if (lims
->imsl_st
[0] == lims
->imsl_st
[1]) {
1112 (void) inm_get_source(inm
, lims
->ims_haddr
, 1, &bims
);
1116 ims_merge(bims
, lims
, 1);
1121 IGMP_PRINTF(("%s: imf filters in-mode: %d at t0, %d at t1\n",
1122 __func__
, nsrc0
, nsrc1
));
1124 /* Handle transition between INCLUDE {n} and INCLUDE {} on socket. */
1125 if (imf
->imf_st
[0] == imf
->imf_st
[1] &&
1126 imf
->imf_st
[1] == MCAST_INCLUDE
) {
1128 IGMP_PRINTF(("%s: --in on inm at t1\n", __func__
));
1129 --inm
->inm_st
[1].iss_in
;
1133 /* Handle filter mode transition on socket. */
1134 if (imf
->imf_st
[0] != imf
->imf_st
[1]) {
1135 IGMP_PRINTF(("%s: imf transition %d to %d\n",
1136 __func__
, imf
->imf_st
[0], imf
->imf_st
[1]));
1138 if (imf
->imf_st
[0] == MCAST_EXCLUDE
) {
1139 IGMP_PRINTF(("%s: --ex on inm at t1\n", __func__
));
1140 --inm
->inm_st
[1].iss_ex
;
1141 } else if (imf
->imf_st
[0] == MCAST_INCLUDE
) {
1142 IGMP_PRINTF(("%s: --in on inm at t1\n", __func__
));
1143 --inm
->inm_st
[1].iss_in
;
1146 if (imf
->imf_st
[1] == MCAST_EXCLUDE
) {
1147 IGMP_PRINTF(("%s: ex++ on inm at t1\n", __func__
));
1148 inm
->inm_st
[1].iss_ex
++;
1149 } else if (imf
->imf_st
[1] == MCAST_INCLUDE
&& nsrc1
> 0) {
1150 IGMP_PRINTF(("%s: in++ on inm at t1\n", __func__
));
1151 inm
->inm_st
[1].iss_in
++;
1156 * Track inm filter state in terms of listener counts.
1157 * If there are any exclusive listeners, stack-wide
1158 * membership is exclusive.
1159 * Otherwise, if only inclusive listeners, stack-wide is inclusive.
1160 * If no listeners remain, state is undefined at t1,
1161 * and the IGMP lifecycle for this group should finish.
1163 if (inm
->inm_st
[1].iss_ex
> 0) {
1164 IGMP_PRINTF(("%s: transition to EX\n", __func__
));
1165 inm
->inm_st
[1].iss_fmode
= MCAST_EXCLUDE
;
1166 } else if (inm
->inm_st
[1].iss_in
> 0) {
1167 IGMP_PRINTF(("%s: transition to IN\n", __func__
));
1168 inm
->inm_st
[1].iss_fmode
= MCAST_INCLUDE
;
1170 IGMP_PRINTF(("%s: transition to UNDEF\n", __func__
));
1171 inm
->inm_st
[1].iss_fmode
= MCAST_UNDEFINED
;
1174 /* Decrement ASM listener count on transition out of ASM mode. */
1175 if (imf
->imf_st
[0] == MCAST_EXCLUDE
&& nsrc0
== 0) {
1176 if ((imf
->imf_st
[1] != MCAST_EXCLUDE
) ||
1177 (imf
->imf_st
[1] == MCAST_EXCLUDE
&& nsrc1
> 0)) {
1178 IGMP_PRINTF(("%s: --asm on inm at t1\n", __func__
));
1179 --inm
->inm_st
[1].iss_asm
;
1183 /* Increment ASM listener count on transition to ASM mode. */
1184 if (imf
->imf_st
[1] == MCAST_EXCLUDE
&& nsrc1
== 0) {
1185 IGMP_PRINTF(("%s: asm++ on inm at t1\n", __func__
));
1186 inm
->inm_st
[1].iss_asm
++;
1189 IGMP_PRINTF(("%s: merged imf 0x%llx to inm 0x%llx\n", __func__
,
1190 (uint64_t)VM_KERNEL_ADDRPERM(imf
),
1191 (uint64_t)VM_KERNEL_ADDRPERM(inm
)));
1196 IGMP_PRINTF(("%s: sources changed; reaping\n", __func__
));
1203 * Mark an in_multi's filter set deltas as committed.
1204 * Called by IGMP after a state change has been enqueued.
1207 inm_commit(struct in_multi
*inm
)
1209 struct ip_msource
*ims
;
1211 INM_LOCK_ASSERT_HELD(inm
);
1213 IGMP_PRINTF(("%s: commit inm 0x%llx\n", __func__
,
1214 (uint64_t)VM_KERNEL_ADDRPERM(inm
)));
1215 IGMP_PRINTF(("%s: pre commit:\n", __func__
));
1218 RB_FOREACH(ims
, ip_msource_tree
, &inm
->inm_srcs
) {
1219 ims
->ims_st
[0] = ims
->ims_st
[1];
1221 inm
->inm_st
[0] = inm
->inm_st
[1];
1225 * Reap unreferenced nodes from an in_multi's filter set.
1228 inm_reap(struct in_multi
*inm
)
1230 struct ip_msource
*ims
, *tims
;
1232 INM_LOCK_ASSERT_HELD(inm
);
1234 RB_FOREACH_SAFE(ims
, ip_msource_tree
, &inm
->inm_srcs
, tims
) {
1235 if (ims
->ims_st
[0].ex
> 0 || ims
->ims_st
[0].in
> 0 ||
1236 ims
->ims_st
[1].ex
> 0 || ims
->ims_st
[1].in
> 0 ||
1237 ims
->ims_stp
!= 0) {
1240 IGMP_PRINTF(("%s: free ims 0x%llx\n", __func__
,
1241 (uint64_t)VM_KERNEL_ADDRPERM(ims
)));
1242 RB_REMOVE(ip_msource_tree
, &inm
->inm_srcs
, ims
);
1249 * Purge all source nodes from an in_multi's filter set.
1252 inm_purge(struct in_multi
*inm
)
1254 struct ip_msource
*ims
, *tims
;
1256 INM_LOCK_ASSERT_HELD(inm
);
1258 RB_FOREACH_SAFE(ims
, ip_msource_tree
, &inm
->inm_srcs
, tims
) {
1259 IGMP_PRINTF(("%s: free ims 0x%llx\n", __func__
,
1260 (uint64_t)VM_KERNEL_ADDRPERM(ims
)));
1261 RB_REMOVE(ip_msource_tree
, &inm
->inm_srcs
, ims
);
1268 * Join a multicast group; real entry point.
1270 * Only preserves atomicity at inm level.
1271 * NOTE: imf argument cannot be const due to sys/tree.h limitations.
1273 * If the IGMP downcall fails, the group is not joined, and an error
1277 in_joingroup(struct ifnet
*ifp
, const struct in_addr
*gina
,
1278 /*const*/ struct in_mfilter
*imf
, struct in_multi
**pinm
)
1280 struct in_mfilter timf
;
1281 struct in_multi
*inm
= NULL
;
1283 struct igmp_tparams itp
;
1285 IGMP_INET_PRINTF(*gina
, ("%s: join %s on 0x%llx(%s))\n", __func__
,
1286 _igmp_inet_buf
, (uint64_t)VM_KERNEL_ADDRPERM(ifp
), if_name(ifp
)));
1288 bzero(&itp
, sizeof(itp
));
1292 * If no imf was specified (i.e. kernel consumer),
1293 * fake one up and assume it is an ASM join.
1296 imf_init(&timf
, MCAST_UNDEFINED
, MCAST_EXCLUDE
);
1300 error
= in_getmulti(ifp
, gina
, &inm
);
1302 IGMP_PRINTF(("%s: in_getmulti() failure\n", __func__
));
1306 IGMP_PRINTF(("%s: merge inm state\n", __func__
));
1309 error
= inm_merge(inm
, imf
);
1311 IGMP_PRINTF(("%s: failed to merge inm state\n", __func__
));
1312 goto out_inm_release
;
1315 IGMP_PRINTF(("%s: doing igmp downcall\n", __func__
));
1316 error
= igmp_change_state(inm
, &itp
);
1318 IGMP_PRINTF(("%s: failed to update source\n", __func__
));
1320 goto out_inm_release
;
1325 IGMP_PRINTF(("%s: dropping ref on 0x%llx\n", __func__
,
1326 (uint64_t)VM_KERNEL_ADDRPERM(inm
)));
1331 *pinm
= inm
; /* keep refcount from in_getmulti() */
1334 /* schedule timer now that we've dropped the lock(s) */
1335 igmp_set_timeout(&itp
);
1341 * Leave a multicast group; real entry point.
1342 * All source filters will be expunged.
1344 * Only preserves atomicity at inm level.
1346 * Note: This is not the same as inm_release(*) as this function also
1347 * makes a state change downcall into IGMP.
1350 in_leavegroup(struct in_multi
*inm
, /*const*/ struct in_mfilter
*imf
)
1352 struct in_mfilter timf
;
1354 struct igmp_tparams itp
;
1356 bzero(&itp
, sizeof(itp
));
1359 INM_LOCK_ASSERT_NOTHELD(inm
);
1361 in_multihead_lock_exclusive();
1364 IGMP_INET_PRINTF(inm
->inm_addr
,
1365 ("%s: leave inm 0x%llx, %s/%s%d, imf 0x%llx\n", __func__
,
1366 (uint64_t)VM_KERNEL_ADDRPERM(inm
), _igmp_inet_buf
,
1367 (inm_is_ifp_detached(inm
) ? "null" : inm
->inm_ifp
->if_name
),
1368 inm
->inm_ifp
->if_unit
, (uint64_t)VM_KERNEL_ADDRPERM(imf
)));
1371 * If no imf was specified (i.e. kernel consumer),
1372 * fake one up and assume it is an ASM join.
1375 imf_init(&timf
, MCAST_EXCLUDE
, MCAST_UNDEFINED
);
1380 * Begin state merge transaction at IGMP layer.
1382 * As this particular invocation should not cause any memory
1383 * to be allocated, and there is no opportunity to roll back
1384 * the transaction, it MUST NOT fail.
1386 IGMP_PRINTF(("%s: merge inm state\n", __func__
));
1388 error
= inm_merge(inm
, imf
);
1389 KASSERT(error
== 0, ("%s: failed to merge inm state\n", __func__
));
1391 IGMP_PRINTF(("%s: doing igmp downcall\n", __func__
));
1392 error
= igmp_change_state(inm
, &itp
);
1395 IGMP_PRINTF(("%s: failed igmp downcall\n", __func__
));
1398 lastref
= in_multi_detach(inm
);
1399 VERIFY(!lastref
|| (!(inm
->inm_debug
& IFD_ATTACHED
) &&
1400 inm
->inm_reqcnt
== 0));
1402 in_multihead_lock_done();
1405 INM_REMREF(inm
); /* for in_multihead list */
1407 /* schedule timer now that we've dropped the lock(s) */
1408 igmp_set_timeout(&itp
);
1414 * Join an IPv4 multicast group in (*,G) exclusive mode.
1415 * The group must be a 224.0.0.0/24 link-scope group.
1416 * This KPI is for legacy kernel consumers only.
1419 in_addmulti(struct in_addr
*ap
, struct ifnet
*ifp
)
1421 struct in_multi
*pinm
= NULL
;
1424 KASSERT(IN_LOCAL_GROUP(ntohl(ap
->s_addr
)),
1425 ("%s: %s not in 224.0.0.0/24\n", __func__
, inet_ntoa(*ap
)));
1427 error
= in_joingroup(ifp
, ap
, NULL
, &pinm
);
1428 VERIFY(pinm
!= NULL
|| error
!= 0);
1434 * Leave an IPv4 multicast group, assumed to be in exclusive (*,G) mode.
1435 * This KPI is for legacy kernel consumers only.
1438 in_delmulti(struct in_multi
*inm
)
1440 (void) in_leavegroup(inm
, NULL
);
1444 * Block or unblock an ASM multicast source on an inpcb.
1445 * This implements the delta-based API described in RFC 3678.
1447 * The delta-based API applies only to exclusive-mode memberships.
1448 * An IGMP downcall will be performed.
1450 * Return 0 if successful, otherwise return an appropriate error code.
1453 inp_block_unblock_source(struct inpcb
*inp
, struct sockopt
*sopt
)
1455 struct group_source_req gsr
;
1456 struct sockaddr_in
*gsa
, *ssa
;
1458 struct in_mfilter
*imf
;
1459 struct ip_moptions
*imo
;
1460 struct in_msource
*ims
;
1461 struct in_multi
*inm
;
1465 unsigned int ifindex
= 0;
1466 struct igmp_tparams itp
;
1468 bzero(&itp
, sizeof(itp
));
1473 memset(&gsr
, 0, sizeof(struct group_source_req
));
1474 gsa
= (struct sockaddr_in
*)&gsr
.gsr_group
;
1475 ssa
= (struct sockaddr_in
*)&gsr
.gsr_source
;
1477 switch (sopt
->sopt_name
) {
1478 case IP_BLOCK_SOURCE
:
1479 case IP_UNBLOCK_SOURCE
: {
1480 struct ip_mreq_source mreqs
;
1482 error
= sooptcopyin(sopt
, &mreqs
,
1483 sizeof(struct ip_mreq_source
),
1484 sizeof(struct ip_mreq_source
));
1489 gsa
->sin_family
= AF_INET
;
1490 gsa
->sin_len
= sizeof(struct sockaddr_in
);
1491 gsa
->sin_addr
= mreqs
.imr_multiaddr
;
1493 ssa
->sin_family
= AF_INET
;
1494 ssa
->sin_len
= sizeof(struct sockaddr_in
);
1495 ssa
->sin_addr
= mreqs
.imr_sourceaddr
;
1497 if (!in_nullhost(mreqs
.imr_interface
)) {
1498 ifp
= ip_multicast_if(&mreqs
.imr_interface
, &ifindex
);
1501 if (sopt
->sopt_name
== IP_BLOCK_SOURCE
) {
1505 IGMP_INET_PRINTF(mreqs
.imr_interface
,
1506 ("%s: imr_interface = %s, ifp = 0x%llx\n", __func__
,
1507 _igmp_inet_buf
, (uint64_t)VM_KERNEL_ADDRPERM(ifp
)));
1511 case MCAST_BLOCK_SOURCE
:
1512 case MCAST_UNBLOCK_SOURCE
:
1513 error
= sooptcopyin(sopt
, &gsr
,
1514 sizeof(struct group_source_req
),
1515 sizeof(struct group_source_req
));
1520 if (gsa
->sin_family
!= AF_INET
||
1521 gsa
->sin_len
!= sizeof(struct sockaddr_in
)) {
1525 if (ssa
->sin_family
!= AF_INET
||
1526 ssa
->sin_len
!= sizeof(struct sockaddr_in
)) {
1530 ifnet_head_lock_shared();
1531 if (gsr
.gsr_interface
== 0 ||
1532 (u_int
)if_index
< gsr
.gsr_interface
) {
1534 return EADDRNOTAVAIL
;
1537 ifp
= ifindex2ifnet
[gsr
.gsr_interface
];
1541 return EADDRNOTAVAIL
;
1544 if (sopt
->sopt_name
== MCAST_BLOCK_SOURCE
) {
1550 IGMP_PRINTF(("%s: unknown sopt_name %d\n",
1551 __func__
, sopt
->sopt_name
));
1555 if (!IN_MULTICAST(ntohl(gsa
->sin_addr
.s_addr
))) {
1560 * Check if we are actually a member of this group.
1562 imo
= inp_findmoptions(inp
);
1568 idx
= imo_match_group(imo
, ifp
, gsa
);
1569 if (idx
== (size_t)-1 || imo
->imo_mfilters
== NULL
) {
1570 error
= EADDRNOTAVAIL
;
1571 goto out_imo_locked
;
1574 VERIFY(imo
->imo_mfilters
!= NULL
);
1575 imf
= &imo
->imo_mfilters
[idx
];
1576 inm
= imo
->imo_membership
[idx
];
1579 * Attempting to use the delta-based API on an
1580 * non exclusive-mode membership is an error.
1582 fmode
= imf
->imf_st
[0];
1583 if (fmode
!= MCAST_EXCLUDE
) {
1585 goto out_imo_locked
;
1589 * Deal with error cases up-front:
1590 * Asked to block, but already blocked; or
1591 * Asked to unblock, but nothing to unblock.
1592 * If adding a new block entry, allocate it.
1594 ims
= imo_match_source(imo
, idx
, ssa
);
1595 if ((ims
!= NULL
&& doblock
) || (ims
== NULL
&& !doblock
)) {
1596 IGMP_INET_PRINTF(ssa
->sin_addr
,
1597 ("%s: source %s %spresent\n", __func__
,
1598 _igmp_inet_buf
, doblock
? "" : "not "));
1599 error
= EADDRNOTAVAIL
;
1600 goto out_imo_locked
;
1604 * Begin state merge transaction at socket layer.
1607 IGMP_PRINTF(("%s: %s source\n", __func__
, "block"));
1608 ims
= imf_graft(imf
, fmode
, ssa
);
1613 IGMP_PRINTF(("%s: %s source\n", __func__
, "allow"));
1614 error
= imf_prune(imf
, ssa
);
1618 IGMP_PRINTF(("%s: merge imf state failed\n", __func__
));
1619 goto out_imf_rollback
;
1623 * Begin state merge transaction at IGMP layer.
1626 IGMP_PRINTF(("%s: merge inm state\n", __func__
));
1627 error
= inm_merge(inm
, imf
);
1629 IGMP_PRINTF(("%s: failed to merge inm state\n", __func__
));
1631 goto out_imf_rollback
;
1634 IGMP_PRINTF(("%s: doing igmp downcall\n", __func__
));
1635 error
= igmp_change_state(inm
, &itp
);
1639 IGMP_PRINTF(("%s: failed igmp downcall\n", __func__
));
1654 IMO_REMREF(imo
); /* from inp_findmoptions() */
1656 /* schedule timer now that we've dropped the lock(s) */
1657 igmp_set_timeout(&itp
);
1663 * Given an inpcb, return its multicast options structure pointer.
1665 * Caller is responsible for locking the inpcb, and releasing the
1666 * extra reference held on the imo, upon a successful return.
1668 static struct ip_moptions
*
1669 inp_findmoptions(struct inpcb
*inp
)
1671 struct ip_moptions
*imo
;
1672 struct in_multi
**immp
;
1673 struct in_mfilter
*imfp
;
1676 if ((imo
= inp
->inp_moptions
) != NULL
) {
1677 IMO_ADDREF(imo
); /* for caller */
1681 imo
= ip_allocmoptions(Z_WAITOK
);
1686 immp
= _MALLOC(sizeof(*immp
) * IP_MIN_MEMBERSHIPS
, M_IPMOPTS
,
1693 imfp
= _MALLOC(sizeof(struct in_mfilter
) * IP_MIN_MEMBERSHIPS
,
1694 M_INMFILTER
, M_WAITOK
| M_ZERO
);
1696 _FREE(immp
, M_IPMOPTS
);
1701 imo
->imo_multicast_ifp
= NULL
;
1702 imo
->imo_multicast_addr
.s_addr
= INADDR_ANY
;
1703 imo
->imo_multicast_vif
= -1;
1704 imo
->imo_multicast_ttl
= IP_DEFAULT_MULTICAST_TTL
;
1705 imo
->imo_multicast_loop
= !!in_mcast_loop
;
1706 imo
->imo_num_memberships
= 0;
1707 imo
->imo_max_memberships
= IP_MIN_MEMBERSHIPS
;
1708 imo
->imo_membership
= immp
;
1710 /* Initialize per-group source filters. */
1711 for (idx
= 0; idx
< IP_MIN_MEMBERSHIPS
; idx
++) {
1712 imf_init(&imfp
[idx
], MCAST_UNDEFINED
, MCAST_EXCLUDE
);
1715 imo
->imo_mfilters
= imfp
;
1716 inp
->inp_moptions
= imo
; /* keep reference from ip_allocmoptions() */
1717 IMO_ADDREF(imo
); /* for caller */
1722 * Atomically get source filters on a socket for an IPv4 multicast group.
1725 inp_get_source_filters(struct inpcb
*inp
, struct sockopt
*sopt
)
1727 struct __msfilterreq64 msfr
= {}, msfr64
;
1728 struct __msfilterreq32 msfr32
;
1729 struct sockaddr_in
*gsa
;
1731 struct ip_moptions
*imo
;
1732 struct in_mfilter
*imf
;
1733 struct ip_msource
*ims
;
1734 struct in_msource
*lims
;
1735 struct sockaddr_in
*psin
;
1736 struct sockaddr_storage
*ptss
;
1737 struct sockaddr_storage
*tss
;
1740 uint32_t nsrcs
, ncsrcs
;
1741 user_addr_t tmp_ptr
;
1743 imo
= inp
->inp_moptions
;
1744 VERIFY(imo
!= NULL
);
1746 if (IS_64BIT_PROCESS(current_proc())) {
1747 error
= sooptcopyin(sopt
, &msfr64
,
1748 sizeof(struct __msfilterreq64
),
1749 sizeof(struct __msfilterreq64
));
1753 /* we never use msfr.msfr_srcs; */
1754 memcpy(&msfr
, &msfr64
, sizeof(msfr64
));
1756 error
= sooptcopyin(sopt
, &msfr32
,
1757 sizeof(struct __msfilterreq32
),
1758 sizeof(struct __msfilterreq32
));
1762 /* we never use msfr.msfr_srcs; */
1763 memcpy(&msfr
, &msfr32
, sizeof(msfr32
));
1766 ifnet_head_lock_shared();
1767 if (msfr
.msfr_ifindex
== 0 || (u_int
)if_index
< msfr
.msfr_ifindex
) {
1769 return EADDRNOTAVAIL
;
1772 ifp
= ifindex2ifnet
[msfr
.msfr_ifindex
];
1776 return EADDRNOTAVAIL
;
1779 if ((size_t) msfr
.msfr_nsrcs
>
1780 UINT32_MAX
/ sizeof(struct sockaddr_storage
)) {
1781 msfr
.msfr_nsrcs
= UINT32_MAX
/ sizeof(struct sockaddr_storage
);
1784 if (msfr
.msfr_nsrcs
> in_mcast_maxsocksrc
) {
1785 msfr
.msfr_nsrcs
= in_mcast_maxsocksrc
;
1790 * Lookup group on the socket.
1792 gsa
= (struct sockaddr_in
*)&msfr
.msfr_group
;
1794 idx
= imo_match_group(imo
, ifp
, gsa
);
1795 if (idx
== (size_t)-1 || imo
->imo_mfilters
== NULL
) {
1797 return EADDRNOTAVAIL
;
1799 imf
= &imo
->imo_mfilters
[idx
];
1802 * Ignore memberships which are in limbo.
1804 if (imf
->imf_st
[1] == MCAST_UNDEFINED
) {
1808 msfr
.msfr_fmode
= imf
->imf_st
[1];
1811 * If the user specified a buffer, copy out the source filter
1812 * entries to userland gracefully.
1813 * We only copy out the number of entries which userland
1814 * has asked for, but we always tell userland how big the
1815 * buffer really needs to be.
1818 if (IS_64BIT_PROCESS(current_proc())) {
1819 tmp_ptr
= CAST_USER_ADDR_T(msfr64
.msfr_srcs
);
1821 tmp_ptr
= CAST_USER_ADDR_T(msfr32
.msfr_srcs
);
1825 if (tmp_ptr
!= USER_ADDR_NULL
&& msfr
.msfr_nsrcs
> 0) {
1826 tss
= _MALLOC((size_t) msfr
.msfr_nsrcs
* sizeof(*tss
),
1827 M_TEMP
, M_WAITOK
| M_ZERO
);
1835 * Count number of sources in-mode at t0.
1836 * If buffer space exists and remains, copy out source entries.
1838 nsrcs
= msfr
.msfr_nsrcs
;
1841 RB_FOREACH(ims
, ip_msource_tree
, &imf
->imf_sources
) {
1842 lims
= (struct in_msource
*)ims
;
1843 if (lims
->imsl_st
[0] == MCAST_UNDEFINED
||
1844 lims
->imsl_st
[0] != imf
->imf_st
[0]) {
1847 if (tss
!= NULL
&& nsrcs
> 0) {
1848 psin
= (struct sockaddr_in
*)ptss
;
1849 psin
->sin_family
= AF_INET
;
1850 psin
->sin_len
= sizeof(struct sockaddr_in
);
1851 psin
->sin_addr
.s_addr
= htonl(lims
->ims_haddr
);
1862 error
= copyout(tss
, CAST_USER_ADDR_T(tmp_ptr
), ncsrcs
* sizeof(*tss
));
1869 msfr
.msfr_nsrcs
= ncsrcs
;
1870 if (IS_64BIT_PROCESS(current_proc())) {
1871 msfr64
.msfr_ifindex
= msfr
.msfr_ifindex
;
1872 msfr64
.msfr_fmode
= msfr
.msfr_fmode
;
1873 msfr64
.msfr_nsrcs
= msfr
.msfr_nsrcs
;
1874 memcpy(&msfr64
.msfr_group
, &msfr
.msfr_group
,
1875 sizeof(struct sockaddr_storage
));
1876 error
= sooptcopyout(sopt
, &msfr64
,
1877 sizeof(struct __msfilterreq64
));
1879 msfr32
.msfr_ifindex
= msfr
.msfr_ifindex
;
1880 msfr32
.msfr_fmode
= msfr
.msfr_fmode
;
1881 msfr32
.msfr_nsrcs
= msfr
.msfr_nsrcs
;
1882 memcpy(&msfr32
.msfr_group
, &msfr
.msfr_group
,
1883 sizeof(struct sockaddr_storage
));
1884 error
= sooptcopyout(sopt
, &msfr32
,
1885 sizeof(struct __msfilterreq32
));
1892 * Return the IP multicast options in response to user getsockopt().
1895 inp_getmoptions(struct inpcb
*inp
, struct sockopt
*sopt
)
1897 struct ip_mreqn mreqn
;
1898 struct ip_moptions
*imo
;
1900 struct in_ifaddr
*ia
;
1902 unsigned int ifindex
;
1905 imo
= inp
->inp_moptions
;
1907 * If socket is neither of type SOCK_RAW or SOCK_DGRAM,
1908 * or is a divert socket, reject it.
1910 if (SOCK_PROTO(inp
->inp_socket
) == IPPROTO_DIVERT
||
1911 (SOCK_TYPE(inp
->inp_socket
) != SOCK_RAW
&&
1912 SOCK_TYPE(inp
->inp_socket
) != SOCK_DGRAM
)) {
1917 switch (sopt
->sopt_name
) {
1918 case IP_MULTICAST_IF
:
1919 memset(&mreqn
, 0, sizeof(struct ip_mreqn
));
1922 ifp
= imo
->imo_multicast_ifp
;
1923 if (!in_nullhost(imo
->imo_multicast_addr
)) {
1924 mreqn
.imr_address
= imo
->imo_multicast_addr
;
1925 } else if (ifp
!= NULL
) {
1926 mreqn
.imr_ifindex
= ifp
->if_index
;
1929 IFA_LOCK_SPIN(&ia
->ia_ifa
);
1931 IA_SIN(ia
)->sin_addr
;
1932 IFA_UNLOCK(&ia
->ia_ifa
);
1933 IFA_REMREF(&ia
->ia_ifa
);
1938 if (sopt
->sopt_valsize
== sizeof(struct ip_mreqn
)) {
1939 error
= sooptcopyout(sopt
, &mreqn
,
1940 sizeof(struct ip_mreqn
));
1942 error
= sooptcopyout(sopt
, &mreqn
.imr_address
,
1943 sizeof(struct in_addr
));
1947 case IP_MULTICAST_IFINDEX
:
1951 if (imo
== NULL
|| imo
->imo_multicast_ifp
== NULL
) {
1954 ifindex
= imo
->imo_multicast_ifp
->if_index
;
1959 error
= sooptcopyout(sopt
, &ifindex
, sizeof(ifindex
));
1962 case IP_MULTICAST_TTL
:
1964 optval
= coptval
= IP_DEFAULT_MULTICAST_TTL
;
1967 optval
= coptval
= imo
->imo_multicast_ttl
;
1970 if (sopt
->sopt_valsize
== sizeof(u_char
)) {
1971 error
= sooptcopyout(sopt
, &coptval
, sizeof(u_char
));
1973 error
= sooptcopyout(sopt
, &optval
, sizeof(int));
1977 case IP_MULTICAST_LOOP
:
1979 optval
= coptval
= IP_DEFAULT_MULTICAST_LOOP
;
1982 optval
= coptval
= imo
->imo_multicast_loop
;
1985 if (sopt
->sopt_valsize
== sizeof(u_char
)) {
1986 error
= sooptcopyout(sopt
, &coptval
, sizeof(u_char
));
1988 error
= sooptcopyout(sopt
, &optval
, sizeof(int));
1994 error
= EADDRNOTAVAIL
;
1996 error
= inp_get_source_filters(inp
, sopt
);
2001 error
= ENOPROTOOPT
;
2009 * Look up the ifnet to use for a multicast group membership,
2010 * given the IPv4 address of an interface, and the IPv4 group address.
2012 * This routine exists to support legacy multicast applications
2013 * which do not understand that multicast memberships are scoped to
2014 * specific physical links in the networking stack, or which need
2015 * to join link-scope groups before IPv4 addresses are configured.
2017 * If inp is non-NULL and is bound to an interface, use this socket's
2018 * inp_boundif for any required routing table lookup.
2020 * If the route lookup fails, attempt to use the first non-loopback
2021 * interface with multicast capability in the system as a
2022 * last resort. The legacy IPv4 ASM API requires that we do
2023 * this in order to allow groups to be joined when the routing
2024 * table has not yet been populated during boot.
2026 * Returns NULL if no ifp could be found.
2029 static struct ifnet
*
2030 inp_lookup_mcast_ifp(const struct inpcb
*inp
,
2031 const struct sockaddr_in
*gsin
, const struct in_addr ina
)
2034 unsigned int ifindex
= 0;
2036 VERIFY(gsin
->sin_family
== AF_INET
);
2037 VERIFY(IN_MULTICAST(ntohl(gsin
->sin_addr
.s_addr
)));
2040 if (!in_nullhost(ina
)) {
2041 struct in_addr new_ina
;
2042 memcpy(&new_ina
, &ina
, sizeof(struct in_addr
));
2043 ifp
= ip_multicast_if(&new_ina
, &ifindex
);
2046 unsigned int ifscope
= IFSCOPE_NONE
;
2048 if (inp
!= NULL
&& (inp
->inp_flags
& INP_BOUND_IF
)) {
2049 ifscope
= inp
->inp_boundifp
->if_index
;
2052 bzero(&ro
, sizeof(ro
));
2053 memcpy(&ro
.ro_dst
, gsin
, sizeof(struct sockaddr_in
));
2054 rtalloc_scoped_ign(&ro
, 0, ifscope
);
2055 if (ro
.ro_rt
!= NULL
) {
2056 ifp
= ro
.ro_rt
->rt_ifp
;
2057 VERIFY(ifp
!= NULL
);
2059 struct in_ifaddr
*ia
;
2063 lck_rw_lock_shared(in_ifaddr_rwlock
);
2064 TAILQ_FOREACH(ia
, &in_ifaddrhead
, ia_link
) {
2065 IFA_LOCK_SPIN(&ia
->ia_ifa
);
2067 IFA_UNLOCK(&ia
->ia_ifa
);
2068 if (!(mifp
->if_flags
& IFF_LOOPBACK
) &&
2069 (mifp
->if_flags
& IFF_MULTICAST
)) {
2074 lck_rw_done(in_ifaddr_rwlock
);
2083 * Join an IPv4 multicast group, possibly with a source.
2085 * NB: sopt->sopt_val might point to the kernel address space. This means that
2086 * we were called by the IPv6 stack due to the presence of an IPv6 v4 mapped
2087 * address. In this scenario, sopt_p points to kernproc and sooptcopyin() will
2088 * just issue an in-kernel memcpy.
2091 inp_join_group(struct inpcb
*inp
, struct sockopt
*sopt
)
2093 struct group_source_req gsr
;
2094 struct sockaddr_in
*gsa
, *ssa
;
2096 struct in_mfilter
*imf
;
2097 struct ip_moptions
*imo
;
2098 struct in_multi
*inm
= NULL
;
2099 struct in_msource
*lims
;
2102 struct igmp_tparams itp
;
2104 bzero(&itp
, sizeof(itp
));
2110 memset(&gsr
, 0, sizeof(struct group_source_req
));
2111 gsa
= (struct sockaddr_in
*)&gsr
.gsr_group
;
2112 gsa
->sin_family
= AF_UNSPEC
;
2113 ssa
= (struct sockaddr_in
*)&gsr
.gsr_source
;
2114 ssa
->sin_family
= AF_UNSPEC
;
2116 switch (sopt
->sopt_name
) {
2117 case IP_ADD_MEMBERSHIP
:
2118 case IP_ADD_SOURCE_MEMBERSHIP
: {
2119 struct ip_mreq_source mreqs
;
2121 if (sopt
->sopt_name
== IP_ADD_MEMBERSHIP
) {
2122 error
= sooptcopyin(sopt
, &mreqs
,
2123 sizeof(struct ip_mreq
),
2124 sizeof(struct ip_mreq
));
2126 * Do argument switcharoo from ip_mreq into
2127 * ip_mreq_source to avoid using two instances.
2129 mreqs
.imr_interface
= mreqs
.imr_sourceaddr
;
2130 mreqs
.imr_sourceaddr
.s_addr
= INADDR_ANY
;
2131 } else if (sopt
->sopt_name
== IP_ADD_SOURCE_MEMBERSHIP
) {
2132 error
= sooptcopyin(sopt
, &mreqs
,
2133 sizeof(struct ip_mreq_source
),
2134 sizeof(struct ip_mreq_source
));
2137 IGMP_PRINTF(("%s: error copyin IP_ADD_MEMBERSHIP/"
2138 "IP_ADD_SOURCE_MEMBERSHIP %d err=%d\n",
2139 __func__
, sopt
->sopt_name
, error
));
2143 gsa
->sin_family
= AF_INET
;
2144 gsa
->sin_len
= sizeof(struct sockaddr_in
);
2145 gsa
->sin_addr
= mreqs
.imr_multiaddr
;
2147 if (sopt
->sopt_name
== IP_ADD_SOURCE_MEMBERSHIP
) {
2148 ssa
->sin_family
= AF_INET
;
2149 ssa
->sin_len
= sizeof(struct sockaddr_in
);
2150 ssa
->sin_addr
= mreqs
.imr_sourceaddr
;
2153 if (!IN_MULTICAST(ntohl(gsa
->sin_addr
.s_addr
))) {
2157 ifp
= inp_lookup_mcast_ifp(inp
, gsa
, mreqs
.imr_interface
);
2158 IGMP_INET_PRINTF(mreqs
.imr_interface
,
2159 ("%s: imr_interface = %s, ifp = 0x%llx\n", __func__
,
2160 _igmp_inet_buf
, (uint64_t)VM_KERNEL_ADDRPERM(ifp
)));
2164 case MCAST_JOIN_GROUP
:
2165 case MCAST_JOIN_SOURCE_GROUP
:
2166 if (sopt
->sopt_name
== MCAST_JOIN_GROUP
) {
2167 error
= sooptcopyin(sopt
, &gsr
,
2168 sizeof(struct group_req
),
2169 sizeof(struct group_req
));
2170 } else if (sopt
->sopt_name
== MCAST_JOIN_SOURCE_GROUP
) {
2171 error
= sooptcopyin(sopt
, &gsr
,
2172 sizeof(struct group_source_req
),
2173 sizeof(struct group_source_req
));
2179 if (gsa
->sin_family
!= AF_INET
||
2180 gsa
->sin_len
!= sizeof(struct sockaddr_in
)) {
2185 * Overwrite the port field if present, as the sockaddr
2186 * being copied in may be matched with a binary comparison.
2189 if (sopt
->sopt_name
== MCAST_JOIN_SOURCE_GROUP
) {
2190 if (ssa
->sin_family
!= AF_INET
||
2191 ssa
->sin_len
!= sizeof(struct sockaddr_in
)) {
2197 if (!IN_MULTICAST(ntohl(gsa
->sin_addr
.s_addr
))) {
2201 ifnet_head_lock_shared();
2202 if (gsr
.gsr_interface
== 0 ||
2203 (u_int
)if_index
< gsr
.gsr_interface
) {
2205 return EADDRNOTAVAIL
;
2207 ifp
= ifindex2ifnet
[gsr
.gsr_interface
];
2213 IGMP_PRINTF(("%s: unknown sopt_name %d\n",
2214 __func__
, sopt
->sopt_name
));
2218 if (ifp
== NULL
|| (ifp
->if_flags
& IFF_MULTICAST
) == 0) {
2219 return EADDRNOTAVAIL
;
2222 INC_ATOMIC_INT64_LIM(net_api_stats
.nas_socket_mcast_join_total
);
2224 * TBD: revisit the criteria for non-OS initiated joins
2226 if (inp
->inp_lport
== htons(5353)) {
2227 INC_ATOMIC_INT64_LIM(net_api_stats
.nas_socket_mcast_join_os_total
);
2230 imo
= inp_findmoptions(inp
);
2236 idx
= imo_match_group(imo
, ifp
, gsa
);
2237 if (idx
== (size_t)-1) {
2240 inm
= imo
->imo_membership
[idx
];
2241 imf
= &imo
->imo_mfilters
[idx
];
2242 if (ssa
->sin_family
!= AF_UNSPEC
) {
2244 * MCAST_JOIN_SOURCE_GROUP on an exclusive membership
2245 * is an error. On an existing inclusive membership,
2246 * it just adds the source to the filter list.
2248 if (imf
->imf_st
[1] != MCAST_INCLUDE
) {
2250 goto out_imo_locked
;
2253 * Throw out duplicates.
2255 * XXX FIXME: This makes a naive assumption that
2256 * even if entries exist for *ssa in this imf,
2257 * they will be rejected as dupes, even if they
2258 * are not valid in the current mode (in-mode).
2260 * in_msource is transactioned just as for anything
2261 * else in SSM -- but note naive use of inm_graft()
2262 * below for allocating new filter entries.
2264 * This is only an issue if someone mixes the
2265 * full-state SSM API with the delta-based API,
2266 * which is discouraged in the relevant RFCs.
2268 lims
= imo_match_source(imo
, idx
, ssa
);
2269 if (lims
!= NULL
/*&&
2270 * lims->imsl_st[1] == MCAST_INCLUDE*/) {
2271 error
= EADDRNOTAVAIL
;
2272 goto out_imo_locked
;
2276 * MCAST_JOIN_GROUP on an existing exclusive
2277 * membership is an error; return EADDRINUSE
2278 * to preserve 4.4BSD API idempotence, and
2279 * avoid tedious detour to code below.
2280 * NOTE: This is bending RFC 3678 a bit.
2282 * On an existing inclusive membership, this is also
2283 * an error; if you want to change filter mode,
2284 * you must use the userland API setsourcefilter().
2285 * XXX We don't reject this for imf in UNDEFINED
2286 * state at t1, because allocation of a filter
2287 * is atomic with allocation of a membership.
2290 /* See comments above for EADDRINUSE */
2291 if (imf
->imf_st
[1] == MCAST_EXCLUDE
) {
2294 goto out_imo_locked
;
2299 * Begin state merge transaction at socket layer.
2303 if (imo
->imo_num_memberships
== imo
->imo_max_memberships
) {
2304 error
= imo_grow(imo
, 0);
2306 goto out_imo_locked
;
2310 * Allocate the new slot upfront so we can deal with
2311 * grafting the new source filter in same code path
2312 * as for join-source on existing membership.
2314 idx
= imo
->imo_num_memberships
;
2315 imo
->imo_membership
[idx
] = NULL
;
2316 imo
->imo_num_memberships
++;
2317 VERIFY(imo
->imo_mfilters
!= NULL
);
2318 imf
= &imo
->imo_mfilters
[idx
];
2319 VERIFY(RB_EMPTY(&imf
->imf_sources
));
2323 * Graft new source into filter list for this inpcb's
2324 * membership of the group. The in_multi may not have
2325 * been allocated yet if this is a new membership, however,
2326 * the in_mfilter slot will be allocated and must be initialized.
2328 if (ssa
->sin_family
!= AF_UNSPEC
) {
2329 /* Membership starts in IN mode */
2331 IGMP_PRINTF(("%s: new join w/source\n", __func__
));
2332 imf_init(imf
, MCAST_UNDEFINED
, MCAST_INCLUDE
);
2334 IGMP_PRINTF(("%s: %s source\n", __func__
, "allow"));
2336 lims
= imf_graft(imf
, MCAST_INCLUDE
, ssa
);
2338 IGMP_PRINTF(("%s: merge imf state failed\n",
2344 /* No address specified; Membership starts in EX mode */
2346 IGMP_PRINTF(("%s: new join w/o source\n", __func__
));
2347 imf_init(imf
, MCAST_UNDEFINED
, MCAST_EXCLUDE
);
2352 * Begin state merge transaction at IGMP layer.
2356 * Unlock socket as we may end up calling ifnet_ioctl() to join (or leave)
2357 * the multicast group and we run the risk of a lock ordering issue
2358 * if the ifnet thread calls into the socket layer to acquire the pcb list
2359 * lock while the input thread delivers multicast packets
2361 IMO_ADDREF_LOCKED(imo
);
2363 socket_unlock(inp
->inp_socket
, 0);
2365 VERIFY(inm
== NULL
);
2366 error
= in_joingroup(ifp
, &gsa
->sin_addr
, imf
, &inm
);
2368 socket_lock(inp
->inp_socket
, 0);
2372 VERIFY(inm
!= NULL
|| error
!= 0);
2376 imo
->imo_membership
[idx
] = inm
; /* from in_joingroup() */
2378 IGMP_PRINTF(("%s: merge inm state\n", __func__
));
2380 error
= inm_merge(inm
, imf
);
2382 IGMP_PRINTF(("%s: failed to merge inm state\n",
2385 goto out_imf_rollback
;
2387 IGMP_PRINTF(("%s: doing igmp downcall\n", __func__
));
2388 error
= igmp_change_state(inm
, &itp
);
2391 IGMP_PRINTF(("%s: failed igmp downcall\n",
2393 goto out_imf_rollback
;
2410 if (error
&& is_new
) {
2411 VERIFY(inm
== NULL
);
2412 imo
->imo_membership
[idx
] = NULL
;
2413 --imo
->imo_num_memberships
;
2418 IMO_REMREF(imo
); /* from inp_findmoptions() */
2420 /* schedule timer now that we've dropped the lock(s) */
2421 igmp_set_timeout(&itp
);
2427 * Leave an IPv4 multicast group on an inpcb, possibly with a source.
2429 * NB: sopt->sopt_val might point to the kernel address space. Refer to the
2430 * block comment on top of inp_join_group() for more information.
2433 inp_leave_group(struct inpcb
*inp
, struct sockopt
*sopt
)
2435 struct group_source_req gsr
;
2436 struct ip_mreq_source mreqs
;
2437 struct sockaddr_in
*gsa
, *ssa
;
2439 struct in_mfilter
*imf
;
2440 struct ip_moptions
*imo
;
2441 struct in_msource
*ims
;
2442 struct in_multi
*inm
= NULL
;
2444 int error
, is_final
;
2445 unsigned int ifindex
= 0;
2446 struct igmp_tparams itp
;
2448 bzero(&itp
, sizeof(itp
));
2453 memset(&gsr
, 0, sizeof(struct group_source_req
));
2454 gsa
= (struct sockaddr_in
*)&gsr
.gsr_group
;
2455 ssa
= (struct sockaddr_in
*)&gsr
.gsr_source
;
2457 switch (sopt
->sopt_name
) {
2458 case IP_DROP_MEMBERSHIP
:
2459 case IP_DROP_SOURCE_MEMBERSHIP
:
2460 if (sopt
->sopt_name
== IP_DROP_MEMBERSHIP
) {
2461 error
= sooptcopyin(sopt
, &mreqs
,
2462 sizeof(struct ip_mreq
),
2463 sizeof(struct ip_mreq
));
2465 * Swap interface and sourceaddr arguments,
2466 * as ip_mreq and ip_mreq_source are laid
2469 mreqs
.imr_interface
= mreqs
.imr_sourceaddr
;
2470 mreqs
.imr_sourceaddr
.s_addr
= INADDR_ANY
;
2471 } else if (sopt
->sopt_name
== IP_DROP_SOURCE_MEMBERSHIP
) {
2472 error
= sooptcopyin(sopt
, &mreqs
,
2473 sizeof(struct ip_mreq_source
),
2474 sizeof(struct ip_mreq_source
));
2480 gsa
->sin_family
= AF_INET
;
2481 gsa
->sin_len
= sizeof(struct sockaddr_in
);
2482 gsa
->sin_addr
= mreqs
.imr_multiaddr
;
2484 if (sopt
->sopt_name
== IP_DROP_SOURCE_MEMBERSHIP
) {
2485 ssa
->sin_family
= AF_INET
;
2486 ssa
->sin_len
= sizeof(struct sockaddr_in
);
2487 ssa
->sin_addr
= mreqs
.imr_sourceaddr
;
2490 * Attempt to look up hinted ifp from interface address.
2491 * Fallthrough with null ifp iff lookup fails, to
2492 * preserve 4.4BSD mcast API idempotence.
2493 * XXX NOTE WELL: The RFC 3678 API is preferred because
2494 * using an IPv4 address as a key is racy.
2496 if (!in_nullhost(mreqs
.imr_interface
)) {
2497 ifp
= ip_multicast_if(&mreqs
.imr_interface
, &ifindex
);
2500 IGMP_INET_PRINTF(mreqs
.imr_interface
,
2501 ("%s: imr_interface = %s, ifp = 0x%llx\n", __func__
,
2502 _igmp_inet_buf
, (uint64_t)VM_KERNEL_ADDRPERM(ifp
)));
2506 case MCAST_LEAVE_GROUP
:
2507 case MCAST_LEAVE_SOURCE_GROUP
:
2508 if (sopt
->sopt_name
== MCAST_LEAVE_GROUP
) {
2509 error
= sooptcopyin(sopt
, &gsr
,
2510 sizeof(struct group_req
),
2511 sizeof(struct group_req
));
2512 } else if (sopt
->sopt_name
== MCAST_LEAVE_SOURCE_GROUP
) {
2513 error
= sooptcopyin(sopt
, &gsr
,
2514 sizeof(struct group_source_req
),
2515 sizeof(struct group_source_req
));
2521 if (gsa
->sin_family
!= AF_INET
||
2522 gsa
->sin_len
!= sizeof(struct sockaddr_in
)) {
2526 if (sopt
->sopt_name
== MCAST_LEAVE_SOURCE_GROUP
) {
2527 if (ssa
->sin_family
!= AF_INET
||
2528 ssa
->sin_len
!= sizeof(struct sockaddr_in
)) {
2533 ifnet_head_lock_shared();
2534 if (gsr
.gsr_interface
== 0 ||
2535 (u_int
)if_index
< gsr
.gsr_interface
) {
2537 return EADDRNOTAVAIL
;
2540 ifp
= ifindex2ifnet
[gsr
.gsr_interface
];
2545 IGMP_PRINTF(("%s: unknown sopt_name %d\n",
2546 __func__
, sopt
->sopt_name
));
2550 if (!IN_MULTICAST(ntohl(gsa
->sin_addr
.s_addr
))) {
2555 * Find the membership in the membership array.
2557 imo
= inp_findmoptions(inp
);
2563 idx
= imo_match_group(imo
, ifp
, gsa
);
2564 if (idx
== (size_t)-1) {
2565 error
= EADDRNOTAVAIL
;
2568 inm
= imo
->imo_membership
[idx
];
2569 imf
= &imo
->imo_mfilters
[idx
];
2571 if (ssa
->sin_family
!= AF_UNSPEC
) {
2572 IGMP_PRINTF(("%s: opt=%d is_final=0\n", __func__
,
2578 * Begin state merge transaction at socket layer.
2582 * If we were instructed only to leave a given source, do so.
2583 * MCAST_LEAVE_SOURCE_GROUP is only valid for inclusive memberships.
2588 if (imf
->imf_st
[0] == MCAST_EXCLUDE
) {
2589 error
= EADDRNOTAVAIL
;
2592 ims
= imo_match_source(imo
, idx
, ssa
);
2594 IGMP_INET_PRINTF(ssa
->sin_addr
,
2595 ("%s: source %s %spresent\n", __func__
,
2596 _igmp_inet_buf
, "not "));
2597 error
= EADDRNOTAVAIL
;
2600 IGMP_PRINTF(("%s: %s source\n", __func__
, "block"));
2601 error
= imf_prune(imf
, ssa
);
2603 IGMP_PRINTF(("%s: merge imf state failed\n",
2610 * Begin state merge transaction at IGMP layer.
2616 * Give up the multicast address record to which
2617 * the membership points. Reference held in imo
2618 * will be released below.
2620 (void) in_leavegroup(inm
, imf
);
2622 IGMP_PRINTF(("%s: merge inm state\n", __func__
));
2624 error
= inm_merge(inm
, imf
);
2626 IGMP_PRINTF(("%s: failed to merge inm state\n",
2629 goto out_imf_rollback
;
2632 IGMP_PRINTF(("%s: doing igmp downcall\n", __func__
));
2633 error
= igmp_change_state(inm
, &itp
);
2635 IGMP_PRINTF(("%s: failed igmp downcall\n", __func__
));
2650 /* Remove the gap in the membership array. */
2651 VERIFY(inm
== imo
->imo_membership
[idx
]);
2652 imo
->imo_membership
[idx
] = NULL
;
2655 * See inp_join_group() for why we need to unlock
2657 IMO_ADDREF_LOCKED(imo
);
2659 socket_unlock(inp
->inp_socket
, 0);
2663 socket_lock(inp
->inp_socket
, 0);
2667 for (++idx
; idx
< imo
->imo_num_memberships
; ++idx
) {
2668 imo
->imo_membership
[idx
- 1] = imo
->imo_membership
[idx
];
2669 imo
->imo_mfilters
[idx
- 1] = imo
->imo_mfilters
[idx
];
2671 imo
->imo_num_memberships
--;
2676 IMO_REMREF(imo
); /* from inp_findmoptions() */
2678 /* schedule timer now that we've dropped the lock(s) */
2679 igmp_set_timeout(&itp
);
2685 * Select the interface for transmitting IPv4 multicast datagrams.
2687 * Either an instance of struct in_addr or an instance of struct ip_mreqn
2688 * may be passed to this socket option. An address of INADDR_ANY or an
2689 * interface index of 0 is used to remove a previous selection.
2690 * When no interface is selected, one is chosen for every send.
2693 inp_set_multicast_if(struct inpcb
*inp
, struct sockopt
*sopt
)
2695 struct in_addr addr
;
2696 struct ip_mreqn mreqn
;
2698 struct ip_moptions
*imo
;
2700 unsigned int ifindex
= 0;
2702 bzero(&addr
, sizeof(addr
));
2703 if (sopt
->sopt_valsize
== sizeof(struct ip_mreqn
)) {
2705 * An interface index was specified using the
2706 * Linux-derived ip_mreqn structure.
2708 error
= sooptcopyin(sopt
, &mreqn
, sizeof(struct ip_mreqn
),
2709 sizeof(struct ip_mreqn
));
2714 ifnet_head_lock_shared();
2715 if (mreqn
.imr_ifindex
< 0 || if_index
< mreqn
.imr_ifindex
) {
2720 if (mreqn
.imr_ifindex
== 0) {
2723 ifp
= ifindex2ifnet
[mreqn
.imr_ifindex
];
2726 return EADDRNOTAVAIL
;
2732 * An interface was specified by IPv4 address.
2733 * This is the traditional BSD usage.
2735 error
= sooptcopyin(sopt
, &addr
, sizeof(struct in_addr
),
2736 sizeof(struct in_addr
));
2740 if (in_nullhost(addr
)) {
2743 ifp
= ip_multicast_if(&addr
, &ifindex
);
2745 IGMP_INET_PRINTF(addr
,
2746 ("%s: can't find ifp for addr=%s\n",
2747 __func__
, _igmp_inet_buf
));
2748 return EADDRNOTAVAIL
;
2753 /* Reject interfaces which do not support multicast. */
2754 if (ifp
!= NULL
&& (ifp
->if_flags
& IFF_MULTICAST
) == 0) {
2758 imo
= inp_findmoptions(inp
);
2764 imo
->imo_multicast_ifp
= ifp
;
2766 imo
->imo_multicast_addr
= addr
;
2768 imo
->imo_multicast_addr
.s_addr
= INADDR_ANY
;
2771 IMO_REMREF(imo
); /* from inp_findmoptions() */
2777 * Atomically set source filters on a socket for an IPv4 multicast group.
2780 inp_set_source_filters(struct inpcb
*inp
, struct sockopt
*sopt
)
2782 struct __msfilterreq64 msfr
= {}, msfr64
;
2783 struct __msfilterreq32 msfr32
;
2784 struct sockaddr_in
*gsa
;
2786 struct in_mfilter
*imf
;
2787 struct ip_moptions
*imo
;
2788 struct in_multi
*inm
;
2792 struct igmp_tparams itp
;
2794 bzero(&itp
, sizeof(itp
));
2796 if (IS_64BIT_PROCESS(current_proc())) {
2797 error
= sooptcopyin(sopt
, &msfr64
,
2798 sizeof(struct __msfilterreq64
),
2799 sizeof(struct __msfilterreq64
));
2803 /* we never use msfr.msfr_srcs; */
2804 memcpy(&msfr
, &msfr64
, sizeof(msfr64
));
2806 error
= sooptcopyin(sopt
, &msfr32
,
2807 sizeof(struct __msfilterreq32
),
2808 sizeof(struct __msfilterreq32
));
2812 /* we never use msfr.msfr_srcs; */
2813 memcpy(&msfr
, &msfr32
, sizeof(msfr32
));
2816 if ((size_t) msfr
.msfr_nsrcs
>
2817 UINT32_MAX
/ sizeof(struct sockaddr_storage
)) {
2818 msfr
.msfr_nsrcs
= UINT32_MAX
/ sizeof(struct sockaddr_storage
);
2821 if (msfr
.msfr_nsrcs
> in_mcast_maxsocksrc
) {
2825 if ((msfr
.msfr_fmode
!= MCAST_EXCLUDE
&&
2826 msfr
.msfr_fmode
!= MCAST_INCLUDE
)) {
2830 if (msfr
.msfr_group
.ss_family
!= AF_INET
||
2831 msfr
.msfr_group
.ss_len
!= sizeof(struct sockaddr_in
)) {
2835 gsa
= (struct sockaddr_in
*)&msfr
.msfr_group
;
2836 if (!IN_MULTICAST(ntohl(gsa
->sin_addr
.s_addr
))) {
2840 gsa
->sin_port
= 0; /* ignore port */
2842 ifnet_head_lock_shared();
2843 if (msfr
.msfr_ifindex
== 0 || (u_int
)if_index
< msfr
.msfr_ifindex
) {
2845 return EADDRNOTAVAIL
;
2848 ifp
= ifindex2ifnet
[msfr
.msfr_ifindex
];
2851 return EADDRNOTAVAIL
;
2855 * Check if this socket is a member of this group.
2857 imo
= inp_findmoptions(inp
);
2863 idx
= imo_match_group(imo
, ifp
, gsa
);
2864 if (idx
== (size_t)-1 || imo
->imo_mfilters
== NULL
) {
2865 error
= EADDRNOTAVAIL
;
2866 goto out_imo_locked
;
2868 inm
= imo
->imo_membership
[idx
];
2869 imf
= &imo
->imo_mfilters
[idx
];
2872 * Begin state merge transaction at socket layer.
2875 imf
->imf_st
[1] = (uint8_t)msfr
.msfr_fmode
;
2878 * Apply any new source filters, if present.
2879 * Make a copy of the user-space source vector so
2880 * that we may copy them with a single copyin. This
2881 * allows us to deal with page faults up-front.
2883 if (msfr
.msfr_nsrcs
> 0) {
2884 struct in_msource
*lims
;
2885 struct sockaddr_in
*psin
;
2886 struct sockaddr_storage
*kss
, *pkss
;
2889 if (IS_64BIT_PROCESS(current_proc())) {
2890 tmp_ptr
= msfr64
.msfr_srcs
;
2892 tmp_ptr
= CAST_USER_ADDR_T(msfr32
.msfr_srcs
);
2895 IGMP_PRINTF(("%s: loading %lu source list entries\n",
2896 __func__
, (unsigned long)msfr
.msfr_nsrcs
));
2897 kss
= _MALLOC((size_t) msfr
.msfr_nsrcs
* sizeof(*kss
),
2901 goto out_imo_locked
;
2903 error
= copyin(CAST_USER_ADDR_T(tmp_ptr
), kss
,
2904 (size_t) msfr
.msfr_nsrcs
* sizeof(*kss
));
2907 goto out_imo_locked
;
2911 * Mark all source filters as UNDEFINED at t1.
2912 * Restore new group filter mode, as imf_leave()
2913 * will set it to INCLUDE.
2916 imf
->imf_st
[1] = (uint8_t)msfr
.msfr_fmode
;
2919 * Update socket layer filters at t1, lazy-allocating
2920 * new entries. This saves a bunch of memory at the
2921 * cost of one RB_FIND() per source entry; duplicate
2922 * entries in the msfr_nsrcs vector are ignored.
2923 * If we encounter an error, rollback transaction.
2925 * XXX This too could be replaced with a set-symmetric
2926 * difference like loop to avoid walking from root
2927 * every time, as the key space is common.
2929 for (i
= 0, pkss
= kss
; (u_int
)i
< msfr
.msfr_nsrcs
;
2931 psin
= (struct sockaddr_in
*)pkss
;
2932 if (psin
->sin_family
!= AF_INET
) {
2933 error
= EAFNOSUPPORT
;
2936 if (psin
->sin_len
!= sizeof(struct sockaddr_in
)) {
2940 error
= imf_get_source(imf
, psin
, &lims
);
2944 lims
->imsl_st
[1] = imf
->imf_st
[1];
2950 goto out_imf_rollback
;
2954 * Begin state merge transaction at IGMP layer.
2957 IGMP_PRINTF(("%s: merge inm state\n", __func__
));
2958 error
= inm_merge(inm
, imf
);
2960 IGMP_PRINTF(("%s: failed to merge inm state\n", __func__
));
2962 goto out_imf_rollback
;
2965 IGMP_PRINTF(("%s: doing igmp downcall\n", __func__
));
2966 error
= igmp_change_state(inm
, &itp
);
2970 IGMP_PRINTF(("%s: failed igmp downcall\n", __func__
));
2985 IMO_REMREF(imo
); /* from inp_findmoptions() */
2987 /* schedule timer now that we've dropped the lock(s) */
2988 igmp_set_timeout(&itp
);
2994 * Set the IP multicast options in response to user setsockopt().
2996 * Many of the socket options handled in this function duplicate the
2997 * functionality of socket options in the regular unicast API. However,
2998 * it is not possible to merge the duplicate code, because the idempotence
2999 * of the IPv4 multicast part of the BSD Sockets API must be preserved;
3000 * the effects of these options must be treated as separate and distinct.
3003 inp_setmoptions(struct inpcb
*inp
, struct sockopt
*sopt
)
3005 struct ip_moptions
*imo
;
3007 unsigned int ifindex
;
3013 * If socket is neither of type SOCK_RAW or SOCK_DGRAM,
3014 * or is a divert socket, reject it.
3016 if (SOCK_PROTO(inp
->inp_socket
) == IPPROTO_DIVERT
||
3017 (SOCK_TYPE(inp
->inp_socket
) != SOCK_RAW
&&
3018 SOCK_TYPE(inp
->inp_socket
) != SOCK_DGRAM
)) {
3022 switch (sopt
->sopt_name
) {
3023 case IP_MULTICAST_IF
:
3024 error
= inp_set_multicast_if(inp
, sopt
);
3027 case IP_MULTICAST_IFINDEX
:
3029 * Select the interface for outgoing multicast packets.
3031 error
= sooptcopyin(sopt
, &ifindex
, sizeof(ifindex
),
3037 imo
= inp_findmoptions(inp
);
3043 * Index 0 is used to remove a previous selection.
3044 * When no interface is selected, a default one is
3045 * chosen every time a multicast packet is sent.
3049 imo
->imo_multicast_ifp
= NULL
;
3051 IMO_REMREF(imo
); /* from inp_findmoptions() */
3055 ifnet_head_lock_shared();
3056 /* Don't need to check is ifindex is < 0 since it's unsigned */
3057 if ((unsigned int)if_index
< ifindex
) {
3059 IMO_REMREF(imo
); /* from inp_findmoptions() */
3060 error
= ENXIO
; /* per IPV6_MULTICAST_IF */
3063 ifp
= ifindex2ifnet
[ifindex
];
3066 /* If it's detached or isn't a multicast interface, bail out */
3067 if (ifp
== NULL
|| !(ifp
->if_flags
& IFF_MULTICAST
)) {
3068 IMO_REMREF(imo
); /* from inp_findmoptions() */
3069 error
= EADDRNOTAVAIL
;
3073 imo
->imo_multicast_ifp
= ifp
;
3075 * Clear out any remnants of past IP_MULTICAST_IF. The addr
3076 * isn't really used anywhere in the kernel; we could have
3077 * iterated thru the addresses of the interface and pick one
3078 * here, but that is redundant since ip_getmoptions() already
3079 * takes care of that for INADDR_ANY.
3081 imo
->imo_multicast_addr
.s_addr
= INADDR_ANY
;
3083 IMO_REMREF(imo
); /* from inp_findmoptions() */
3086 case IP_MULTICAST_TTL
: {
3090 * Set the IP time-to-live for outgoing multicast packets.
3091 * The original multicast API required a char argument,
3092 * which is inconsistent with the rest of the socket API.
3093 * We allow either a char or an int.
3095 if (sopt
->sopt_valsize
== sizeof(u_char
)) {
3096 error
= sooptcopyin(sopt
, &ttl
, sizeof(u_char
),
3104 error
= sooptcopyin(sopt
, &ittl
, sizeof(u_int
),
3115 imo
= inp_findmoptions(inp
);
3121 imo
->imo_multicast_ttl
= ttl
;
3123 IMO_REMREF(imo
); /* from inp_findmoptions() */
3127 case IP_MULTICAST_LOOP
: {
3131 * Set the loopback flag for outgoing multicast packets.
3132 * Must be zero or one. The original multicast API required a
3133 * char argument, which is inconsistent with the rest
3134 * of the socket API. We allow either a char or an int.
3136 if (sopt
->sopt_valsize
== sizeof(u_char
)) {
3137 error
= sooptcopyin(sopt
, &loop
, sizeof(u_char
),
3145 error
= sooptcopyin(sopt
, &iloop
, sizeof(u_int
),
3150 loop
= (u_char
)iloop
;
3152 imo
= inp_findmoptions(inp
);
3158 imo
->imo_multicast_loop
= !!loop
;
3160 IMO_REMREF(imo
); /* from inp_findmoptions() */
3164 case IP_ADD_MEMBERSHIP
:
3165 case IP_ADD_SOURCE_MEMBERSHIP
:
3166 case MCAST_JOIN_GROUP
:
3167 case MCAST_JOIN_SOURCE_GROUP
:
3168 error
= inp_join_group(inp
, sopt
);
3171 case IP_DROP_MEMBERSHIP
:
3172 case IP_DROP_SOURCE_MEMBERSHIP
:
3173 case MCAST_LEAVE_GROUP
:
3174 case MCAST_LEAVE_SOURCE_GROUP
:
3175 error
= inp_leave_group(inp
, sopt
);
3178 case IP_BLOCK_SOURCE
:
3179 case IP_UNBLOCK_SOURCE
:
3180 case MCAST_BLOCK_SOURCE
:
3181 case MCAST_UNBLOCK_SOURCE
:
3182 error
= inp_block_unblock_source(inp
, sopt
);
3186 error
= inp_set_source_filters(inp
, sopt
);
3198 * Expose IGMP's multicast filter mode and source list(s) to userland,
3199 * keyed by (ifindex, group).
3200 * The filter mode is written out as a uint32_t, followed by
3201 * 0..n of struct in_addr.
3202 * For use by ifmcstat(8).
3205 sysctl_ip_mcast_filters SYSCTL_HANDLER_ARGS
3207 #pragma unused(oidp)
3209 struct in_addr src
= {}, group
;
3211 struct in_multi
*inm
;
3212 struct in_multistep step
;
3213 struct ip_msource
*ims
;
3217 uint32_t fmode
, ifindex
;
3220 namelen
= (u_int
)arg2
;
3222 if (req
->newptr
!= USER_ADDR_NULL
) {
3231 ifnet_head_lock_shared();
3232 if (ifindex
<= 0 || ifindex
> (u_int
)if_index
) {
3233 IGMP_PRINTF(("%s: ifindex %u out of range\n",
3234 __func__
, ifindex
));
3239 group
.s_addr
= name
[1];
3240 if (!IN_MULTICAST(ntohl(group
.s_addr
))) {
3241 IGMP_INET_PRINTF(group
,
3242 ("%s: group %s is not multicast\n",
3243 __func__
, _igmp_inet_buf
));
3248 ifp
= ifindex2ifnet
[ifindex
];
3251 IGMP_PRINTF(("%s: no ifp for ifindex %u\n", __func__
, ifindex
));
3255 in_multihead_lock_shared();
3256 IN_FIRST_MULTI(step
, inm
);
3257 while (inm
!= NULL
) {
3259 if (inm
->inm_ifp
!= ifp
) {
3263 if (!in_hosteq(inm
->inm_addr
, group
)) {
3267 fmode
= inm
->inm_st
[1].iss_fmode
;
3268 retval
= SYSCTL_OUT(req
, &fmode
, sizeof(uint32_t));
3273 RB_FOREACH(ims
, ip_msource_tree
, &inm
->inm_srcs
) {
3276 ina
.s_addr
= htonl(ims
->ims_haddr
);
3277 IGMP_INET_PRINTF(ina
,
3278 ("%s: visit node %s\n", __func__
, _igmp_inet_buf
));
3281 * Only copy-out sources which are in-mode.
3283 if (fmode
!= ims_get_mode(inm
, ims
, 1)) {
3284 IGMP_PRINTF(("%s: skip non-in-mode\n",
3286 continue; /* process next source */
3288 src
.s_addr
= htonl(ims
->ims_haddr
);
3289 retval
= SYSCTL_OUT(req
, &src
, sizeof(struct in_addr
));
3291 break; /* process next inm */
3296 IN_NEXT_MULTI(step
, inm
);
3298 in_multihead_lock_done();
3305 * The whole multicast option thing needs to be re-thought.
3306 * Several of these options are equally applicable to non-multicast
3307 * transmission, and one (IP_MULTICAST_TTL) totally duplicates a
3308 * standard option (IP_TTL).
3311 * following RFC1724 section 3.3, 0.0.0.0/8 is interpreted as interface index.
3313 static struct ifnet
*
3314 ip_multicast_if(struct in_addr
*a
, unsigned int *ifindexp
)
3316 unsigned int ifindex
;
3319 if (ifindexp
!= NULL
) {
3322 if (ntohl(a
->s_addr
) >> 24 == 0) {
3323 ifindex
= ntohl(a
->s_addr
) & 0xffffff;
3324 ifnet_head_lock_shared();
3325 /* Don't need to check is ifindex is < 0 since it's unsigned */
3326 if ((unsigned int)if_index
< ifindex
) {
3330 ifp
= ifindex2ifnet
[ifindex
];
3332 if (ifp
!= NULL
&& ifindexp
!= NULL
) {
3333 *ifindexp
= ifindex
;
3336 INADDR_TO_IFP(*a
, ifp
);
3344 PE_parse_boot_argn("ifa_debug", &inm_debug
, sizeof(inm_debug
));
3346 /* Setup lock group and attribute for in_multihead */
3347 in_multihead_lock_grp_attr
= lck_grp_attr_alloc_init();
3348 in_multihead_lock_grp
= lck_grp_alloc_init("in_multihead",
3349 in_multihead_lock_grp_attr
);
3350 in_multihead_lock_attr
= lck_attr_alloc_init();
3351 lck_rw_init(&in_multihead_lock
, in_multihead_lock_grp
,
3352 in_multihead_lock_attr
);
3354 lck_mtx_init(&inm_trash_lock
, in_multihead_lock_grp
,
3355 in_multihead_lock_attr
);
3356 TAILQ_INIT(&inm_trash_head
);
3358 vm_size_t inm_size
= (inm_debug
== 0) ? sizeof(struct in_multi
) :
3359 sizeof(struct in_multi_dbg
);
3360 inm_zone
= zone_create(INM_ZONE_NAME
, inm_size
, ZC_ZFREE_CLEARMEM
);
3363 static struct in_multi
*
3364 in_multi_alloc(zalloc_flags_t how
)
3366 struct in_multi
*inm
;
3368 inm
= zalloc_flags(inm_zone
, how
| Z_ZERO
);
3370 lck_mtx_init(&inm
->inm_lock
, in_multihead_lock_grp
,
3371 in_multihead_lock_attr
);
3372 inm
->inm_debug
|= IFD_ALLOC
;
3373 if (inm_debug
!= 0) {
3374 inm
->inm_debug
|= IFD_DEBUG
;
3375 inm
->inm_trace
= inm_trace
;
3382 in_multi_free(struct in_multi
*inm
)
3385 if (inm
->inm_debug
& IFD_ATTACHED
) {
3386 panic("%s: attached inm=%p is being freed", __func__
, inm
);
3388 } else if (inm
->inm_ifma
!= NULL
) {
3389 panic("%s: ifma not NULL for inm=%p", __func__
, inm
);
3391 } else if (!(inm
->inm_debug
& IFD_ALLOC
)) {
3392 panic("%s: inm %p cannot be freed", __func__
, inm
);
3394 } else if (inm
->inm_refcount
!= 0) {
3395 panic("%s: non-zero refcount inm=%p", __func__
, inm
);
3397 } else if (inm
->inm_reqcnt
!= 0) {
3398 panic("%s: non-zero reqcnt inm=%p", __func__
, inm
);
3402 /* Free any pending IGMPv3 state-change records */
3403 IF_DRAIN(&inm
->inm_scq
);
3405 inm
->inm_debug
&= ~IFD_ALLOC
;
3406 if ((inm
->inm_debug
& (IFD_DEBUG
| IFD_TRASHED
)) ==
3407 (IFD_DEBUG
| IFD_TRASHED
)) {
3408 lck_mtx_lock(&inm_trash_lock
);
3409 TAILQ_REMOVE(&inm_trash_head
, (struct in_multi_dbg
*)inm
,
3411 lck_mtx_unlock(&inm_trash_lock
);
3412 inm
->inm_debug
&= ~IFD_TRASHED
;
3416 lck_mtx_destroy(&inm
->inm_lock
, in_multihead_lock_grp
);
3417 zfree(inm_zone
, inm
);
3421 in_multi_attach(struct in_multi
*inm
)
3423 in_multihead_lock_assert(LCK_RW_ASSERT_EXCLUSIVE
);
3424 INM_LOCK_ASSERT_HELD(inm
);
3426 if (inm
->inm_debug
& IFD_ATTACHED
) {
3427 panic("%s: Attempt to attach an already attached inm=%p",
3430 } else if (inm
->inm_debug
& IFD_TRASHED
) {
3431 panic("%s: Attempt to reattach a detached inm=%p",
3437 VERIFY(inm
->inm_reqcnt
== 1);
3438 INM_ADDREF_LOCKED(inm
);
3439 inm
->inm_debug
|= IFD_ATTACHED
;
3441 * Reattach case: If debugging is enabled, take it
3442 * out of the trash list and clear IFD_TRASHED.
3444 if ((inm
->inm_debug
& (IFD_DEBUG
| IFD_TRASHED
)) ==
3445 (IFD_DEBUG
| IFD_TRASHED
)) {
3446 /* Become a regular mutex, just in case */
3447 INM_CONVERT_LOCK(inm
);
3448 lck_mtx_lock(&inm_trash_lock
);
3449 TAILQ_REMOVE(&inm_trash_head
, (struct in_multi_dbg
*)inm
,
3451 lck_mtx_unlock(&inm_trash_lock
);
3452 inm
->inm_debug
&= ~IFD_TRASHED
;
3455 LIST_INSERT_HEAD(&in_multihead
, inm
, inm_link
);
3459 in_multi_detach(struct in_multi
*inm
)
3461 in_multihead_lock_assert(LCK_RW_ASSERT_EXCLUSIVE
);
3462 INM_LOCK_ASSERT_HELD(inm
);
3464 if (inm
->inm_reqcnt
== 0) {
3465 panic("%s: inm=%p negative reqcnt", __func__
, inm
);
3470 if (inm
->inm_reqcnt
> 0) {
3474 if (!(inm
->inm_debug
& IFD_ATTACHED
)) {
3475 panic("%s: Attempt to detach an unattached record inm=%p",
3478 } else if (inm
->inm_debug
& IFD_TRASHED
) {
3479 panic("%s: inm %p is already in trash list", __func__
, inm
);
3484 * NOTE: Caller calls IFMA_REMREF
3486 inm
->inm_debug
&= ~IFD_ATTACHED
;
3487 LIST_REMOVE(inm
, inm_link
);
3489 if (inm
->inm_debug
& IFD_DEBUG
) {
3490 /* Become a regular mutex, just in case */
3491 INM_CONVERT_LOCK(inm
);
3492 lck_mtx_lock(&inm_trash_lock
);
3493 TAILQ_INSERT_TAIL(&inm_trash_head
,
3494 (struct in_multi_dbg
*)inm
, inm_trash_link
);
3495 lck_mtx_unlock(&inm_trash_lock
);
3496 inm
->inm_debug
|= IFD_TRASHED
;
3503 inm_addref(struct in_multi
*inm
, int locked
)
3508 INM_LOCK_ASSERT_HELD(inm
);
3511 if (++inm
->inm_refcount
== 0) {
3512 panic("%s: inm=%p wraparound refcnt", __func__
, inm
);
3514 } else if (inm
->inm_trace
!= NULL
) {
3515 (*inm
->inm_trace
)(inm
, TRUE
);
3523 inm_remref(struct in_multi
*inm
, int locked
)
3525 struct ifmultiaddr
*ifma
;
3526 struct igmp_ifinfo
*igi
;
3531 INM_LOCK_ASSERT_HELD(inm
);
3534 if (inm
->inm_refcount
== 0 || (inm
->inm_refcount
== 1 && locked
)) {
3535 panic("%s: inm=%p negative/missing refcnt", __func__
, inm
);
3537 } else if (inm
->inm_trace
!= NULL
) {
3538 (*inm
->inm_trace
)(inm
, FALSE
);
3541 --inm
->inm_refcount
;
3542 if (inm
->inm_refcount
> 0) {
3550 * Synchronization with in_getmulti(). In the event the inm has been
3551 * detached, the underlying ifma would still be in the if_multiaddrs
3552 * list, and thus can be looked up via if_addmulti(). At that point,
3553 * the only way to find this inm is via ifma_protospec. To avoid
3554 * race conditions between the last inm_remref() of that inm and its
3555 * use via ifma_protospec, in_multihead lock is used for serialization.
3556 * In order to avoid violating the lock order, we must drop inm_lock
3557 * before acquiring in_multihead lock. To prevent the inm from being
3558 * freed prematurely, we hold an extra reference.
3560 ++inm
->inm_refcount
;
3562 in_multihead_lock_shared();
3564 --inm
->inm_refcount
;
3565 if (inm
->inm_refcount
> 0) {
3566 /* We've lost the race, so abort since inm is still in use */
3568 in_multihead_lock_done();
3569 /* If it was locked, return it as such */
3576 ifma
= inm
->inm_ifma
;
3577 inm
->inm_ifma
= NULL
;
3578 inm
->inm_ifp
= NULL
;
3580 inm
->inm_igi
= NULL
;
3582 IFMA_LOCK_SPIN(ifma
);
3583 ifma
->ifma_protospec
= NULL
;
3585 in_multihead_lock_done();
3588 if_delmulti_ifma(ifma
);
3589 /* Release reference held to the underlying ifmultiaddr */
3598 inm_trace(struct in_multi
*inm
, int refhold
)
3600 struct in_multi_dbg
*inm_dbg
= (struct in_multi_dbg
*)inm
;
3605 if (!(inm
->inm_debug
& IFD_DEBUG
)) {
3606 panic("%s: inm %p has no debug structure", __func__
, inm
);
3610 cnt
= &inm_dbg
->inm_refhold_cnt
;
3611 tr
= inm_dbg
->inm_refhold
;
3613 cnt
= &inm_dbg
->inm_refrele_cnt
;
3614 tr
= inm_dbg
->inm_refrele
;
3617 idx
= atomic_add_16_ov(cnt
, 1) % INM_TRACE_HIST_SIZE
;
3618 ctrace_record(&tr
[idx
]);
3622 in_multihead_lock_exclusive(void)
3624 lck_rw_lock_exclusive(&in_multihead_lock
);
3628 in_multihead_lock_shared(void)
3630 lck_rw_lock_shared(&in_multihead_lock
);
3634 in_multihead_lock_assert(int what
)
3637 #pragma unused(what)
3639 LCK_RW_ASSERT(&in_multihead_lock
, what
);
3643 in_multihead_lock_done(void)
3645 lck_rw_done(&in_multihead_lock
);
3648 static struct ip_msource
*
3649 ipms_alloc(zalloc_flags_t how
)
3651 return zalloc_flags(ipms_zone
, how
| Z_ZERO
);
3655 ipms_free(struct ip_msource
*ims
)
3657 zfree(ipms_zone
, ims
);
3660 static struct in_msource
*
3661 inms_alloc(zalloc_flags_t how
)
3663 return zalloc_flags(inms_zone
, how
| Z_ZERO
);
3667 inms_free(struct in_msource
*inms
)
3669 zfree(inms_zone
, inms
);
3674 static const char *inm_modestrs
[] = { "un\n", "in", "ex" };
3677 inm_mode_str(const int mode
)
3679 if (mode
>= MCAST_UNDEFINED
&& mode
<= MCAST_EXCLUDE
) {
3680 return inm_modestrs
[mode
];
3685 static const char *inm_statestrs
[] = {
3694 "sg-query-pending\n",
3699 inm_state_str(const int state
)
3701 if (state
>= IGMP_NOT_MEMBER
&& state
<= IGMP_LEAVING_MEMBER
) {
3702 return inm_statestrs
[state
];
3708 * Dump an in_multi structure to the console.
3711 inm_print(const struct in_multi
*inm
)
3714 char buf
[MAX_IPv4_STR_LEN
];
3716 INM_LOCK_ASSERT_HELD(__DECONST(struct in_multi
*, inm
));
3718 if (igmp_debug
== 0) {
3722 inet_ntop(AF_INET
, &inm
->inm_addr
, buf
, sizeof(buf
));
3723 printf("%s: --- begin inm 0x%llx ---\n", __func__
,
3724 (uint64_t)VM_KERNEL_ADDRPERM(inm
));
3725 printf("addr %s ifp 0x%llx(%s) ifma 0x%llx\n",
3727 (uint64_t)VM_KERNEL_ADDRPERM(inm
->inm_ifp
),
3728 if_name(inm
->inm_ifp
),
3729 (uint64_t)VM_KERNEL_ADDRPERM(inm
->inm_ifma
));
3730 printf("timer %u state %s refcount %u scq.len %u\n",
3732 inm_state_str(inm
->inm_state
),
3734 inm
->inm_scq
.ifq_len
);
3735 printf("igi 0x%llx nsrc %lu sctimer %u scrv %u\n",
3736 (uint64_t)VM_KERNEL_ADDRPERM(inm
->inm_igi
),
3740 for (t
= 0; t
< 2; t
++) {
3741 printf("t%d: fmode %s asm %u ex %u in %u rec %u\n", t
,
3742 inm_mode_str(inm
->inm_st
[t
].iss_fmode
),
3743 inm
->inm_st
[t
].iss_asm
,
3744 inm
->inm_st
[t
].iss_ex
,
3745 inm
->inm_st
[t
].iss_in
,
3746 inm
->inm_st
[t
].iss_rec
);
3748 printf("%s: --- end inm 0x%llx ---\n", __func__
,
3749 (uint64_t)VM_KERNEL_ADDRPERM(inm
));
3755 inm_print(__unused
const struct in_multi
*inm
)