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) 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/route.h>
85 #include <netinet/in.h>
86 #include <netinet/in_systm.h>
87 #include <netinet/in_pcb.h>
88 #include <netinet/in_var.h>
89 #include <netinet/ip_var.h>
90 #include <netinet/igmp_var.h>
92 #ifndef __SOCKUNION_DECLARED
94 struct sockaddr_storage ss
;
96 struct sockaddr_dl sdl
;
97 struct sockaddr_in sin
;
99 typedef union sockunion sockunion_t
;
100 #define __SOCKUNION_DECLARED
101 #endif /* __SOCKUNION_DECLARED */
104 * Functions with non-static linkage defined in this file should be
105 * declared in in_var.h:
116 * XXX: Both carp and pf need to use the legacy (*,G) KPIs in_addmulti()
119 static void imf_commit(struct in_mfilter
*);
120 static int imf_get_source(struct in_mfilter
*imf
,
121 const struct sockaddr_in
*psin
,
122 struct in_msource
**);
123 static struct in_msource
*
124 imf_graft(struct in_mfilter
*, const uint8_t,
125 const struct sockaddr_in
*);
126 static int imf_prune(struct in_mfilter
*, const struct sockaddr_in
*);
127 static void imf_rollback(struct in_mfilter
*);
128 static void imf_reap(struct in_mfilter
*);
129 static int imo_grow(struct ip_moptions
*, size_t);
130 static size_t imo_match_group(const struct ip_moptions
*,
131 const struct ifnet
*, const struct sockaddr
*);
132 static struct in_msource
*
133 imo_match_source(const struct ip_moptions
*, const size_t,
134 const struct sockaddr
*);
135 static void ims_merge(struct ip_msource
*ims
,
136 const struct in_msource
*lims
, const int rollback
);
137 static int in_getmulti(struct ifnet
*, const struct in_addr
*,
139 static int in_joingroup(struct ifnet
*, const struct in_addr
*,
140 struct in_mfilter
*, struct in_multi
**);
141 static int inm_get_source(struct in_multi
*inm
, const in_addr_t haddr
,
142 const int noalloc
, struct ip_msource
**pims
);
143 static int inm_is_ifp_detached(const struct in_multi
*);
144 static int inm_merge(struct in_multi
*, /*const*/ struct in_mfilter
*);
145 static void inm_reap(struct in_multi
*);
146 static struct ip_moptions
*
147 inp_findmoptions(struct inpcb
*);
148 static int inp_get_source_filters(struct inpcb
*, struct sockopt
*);
149 static struct ifnet
*
150 inp_lookup_mcast_ifp(const struct inpcb
*,
151 const struct sockaddr_in
*, const struct in_addr
);
152 static int inp_block_unblock_source(struct inpcb
*, struct sockopt
*);
153 static int inp_set_multicast_if(struct inpcb
*, struct sockopt
*);
154 static int inp_set_source_filters(struct inpcb
*, struct sockopt
*);
155 static int sysctl_ip_mcast_filters SYSCTL_HANDLER_ARGS
;
156 static struct ifnet
* ip_multicast_if(struct in_addr
*, unsigned int *);
157 static __inline__
int ip_msource_cmp(const struct ip_msource
*,
158 const struct ip_msource
*);
160 SYSCTL_NODE(_net_inet_ip
, OID_AUTO
, mcast
, CTLFLAG_RW
| CTLFLAG_LOCKED
, 0, "IPv4 multicast");
162 static u_long in_mcast_maxgrpsrc
= IP_MAX_GROUP_SRC_FILTER
;
163 SYSCTL_LONG(_net_inet_ip_mcast
, OID_AUTO
, maxgrpsrc
,
164 CTLFLAG_RW
| CTLFLAG_LOCKED
, &in_mcast_maxgrpsrc
, "Max source filters per group");
166 static u_long in_mcast_maxsocksrc
= IP_MAX_SOCK_SRC_FILTER
;
167 SYSCTL_LONG(_net_inet_ip_mcast
, OID_AUTO
, maxsocksrc
,
168 CTLFLAG_RW
| CTLFLAG_LOCKED
, &in_mcast_maxsocksrc
,
169 "Max source filters per socket");
171 int in_mcast_loop
= IP_DEFAULT_MULTICAST_LOOP
;
172 SYSCTL_INT(_net_inet_ip_mcast
, OID_AUTO
, loop
, CTLFLAG_RW
| CTLFLAG_LOCKED
,
173 &in_mcast_loop
, 0, "Loopback multicast datagrams by default");
175 SYSCTL_NODE(_net_inet_ip_mcast
, OID_AUTO
, filters
,
176 CTLFLAG_RD
| CTLFLAG_LOCKED
, sysctl_ip_mcast_filters
,
177 "Per-interface stack-wide source filters");
179 RB_GENERATE_PREV(ip_msource_tree
, ip_msource
, ims_link
, ip_msource_cmp
);
181 #define INM_TRACE_HIST_SIZE 32 /* size of trace history */
184 __private_extern__
unsigned int inm_trace_hist_size
= INM_TRACE_HIST_SIZE
;
186 struct in_multi_dbg
{
187 struct in_multi inm
; /* in_multi */
188 u_int16_t inm_refhold_cnt
; /* # of ref */
189 u_int16_t inm_refrele_cnt
; /* # of rele */
191 * Circular lists of inm_addref and inm_remref callers.
193 ctrace_t inm_refhold
[INM_TRACE_HIST_SIZE
];
194 ctrace_t inm_refrele
[INM_TRACE_HIST_SIZE
];
198 TAILQ_ENTRY(in_multi_dbg
) inm_trash_link
;
201 /* List of trash in_multi entries protected by inm_trash_lock */
202 static TAILQ_HEAD(, in_multi_dbg
) inm_trash_head
;
203 static decl_lck_mtx_data(, inm_trash_lock
);
205 #define INM_ZONE_MAX 64 /* maximum elements in zone */
206 #define INM_ZONE_NAME "in_multi" /* zone name */
209 static unsigned int inm_debug
= 1; /* debugging (enabled) */
211 static unsigned int inm_debug
; /* debugging (disabled) */
213 static unsigned int inm_size
; /* size of zone element */
214 static struct zone
*inm_zone
; /* zone for in_multi */
216 #define IPMS_ZONE_MAX 64 /* maximum elements in zone */
217 #define IPMS_ZONE_NAME "ip_msource" /* zone name */
219 static unsigned int ipms_size
; /* size of zone element */
220 static struct zone
*ipms_zone
; /* zone for ip_msource */
222 #define INMS_ZONE_MAX 64 /* maximum elements in zone */
223 #define INMS_ZONE_NAME "in_msource" /* zone name */
225 static unsigned int inms_size
; /* size of zone element */
226 static struct zone
*inms_zone
; /* zone for in_msource */
228 /* Lock group and attribute for in_multihead_lock lock */
229 static lck_attr_t
*in_multihead_lock_attr
;
230 static lck_grp_t
*in_multihead_lock_grp
;
231 static lck_grp_attr_t
*in_multihead_lock_grp_attr
;
233 static decl_lck_rw_data(, in_multihead_lock
);
234 struct in_multihead in_multihead
;
236 static struct in_multi
*in_multi_alloc(int);
237 static void in_multi_free(struct in_multi
*);
238 static void in_multi_attach(struct in_multi
*);
239 static void inm_trace(struct in_multi
*, int);
241 static struct ip_msource
*ipms_alloc(int);
242 static void ipms_free(struct ip_msource
*);
243 static struct in_msource
*inms_alloc(int);
244 static void inms_free(struct in_msource
*);
246 #define IMO_CAST_TO_NONCONST(x) ((struct ip_moptions *)(void *)(uintptr_t)x)
247 #define INM_CAST_TO_NONCONST(x) ((struct in_multi *)(void *)(uintptr_t)x)
250 ip_msource_cmp(const struct ip_msource
*a
, const struct ip_msource
*b
)
253 if (a
->ims_haddr
< b
->ims_haddr
)
255 if (a
->ims_haddr
== b
->ims_haddr
)
261 * Inline function which wraps assertions for a valid ifp.
263 static __inline__
int
264 inm_is_ifp_detached(const struct in_multi
*inm
)
266 VERIFY(inm
->inm_ifma
!= NULL
);
267 VERIFY(inm
->inm_ifp
== inm
->inm_ifma
->ifma_ifp
);
269 return (!ifnet_is_attached(inm
->inm_ifp
, 0));
273 * Initialize an in_mfilter structure to a known state at t0, t1
274 * with an empty source filter list.
276 static __inline__
void
277 imf_init(struct in_mfilter
*imf
, const int st0
, const int st1
)
279 memset(imf
, 0, sizeof(struct in_mfilter
));
280 RB_INIT(&imf
->imf_sources
);
281 imf
->imf_st
[0] = st0
;
282 imf
->imf_st
[1] = st1
;
286 * Resize the ip_moptions vector to the next power-of-two minus 1.
289 imo_grow(struct ip_moptions
*imo
, size_t newmax
)
291 struct in_multi
**nmships
;
292 struct in_multi
**omships
;
293 struct in_mfilter
*nmfilters
;
294 struct in_mfilter
*omfilters
;
298 IMO_LOCK_ASSERT_HELD(imo
);
302 omships
= imo
->imo_membership
;
303 omfilters
= imo
->imo_mfilters
;
304 oldmax
= imo
->imo_max_memberships
;
306 newmax
= ((oldmax
+ 1) * 2) - 1;
308 if (newmax
> IP_MAX_MEMBERSHIPS
)
309 return (ETOOMANYREFS
);
311 if ((nmships
= (struct in_multi
**)_REALLOC(omships
,
312 sizeof (struct in_multi
*) * newmax
, M_IPMOPTS
,
313 M_WAITOK
| M_ZERO
)) == NULL
)
316 imo
->imo_membership
= nmships
;
318 if ((nmfilters
= (struct in_mfilter
*)_REALLOC(omfilters
,
319 sizeof (struct in_mfilter
) * newmax
, M_INMFILTER
,
320 M_WAITOK
| M_ZERO
)) == NULL
)
323 imo
->imo_mfilters
= nmfilters
;
325 /* Initialize newly allocated source filter heads. */
326 for (idx
= oldmax
; idx
< newmax
; idx
++)
327 imf_init(&nmfilters
[idx
], MCAST_UNDEFINED
, MCAST_EXCLUDE
);
329 imo
->imo_max_memberships
= newmax
;
335 * Find an IPv4 multicast group entry for this ip_moptions instance
336 * which matches the specified group, and optionally an interface.
337 * Return its index into the array, or -1 if not found.
340 imo_match_group(const struct ip_moptions
*imo
, const struct ifnet
*ifp
,
341 const struct sockaddr
*group
)
343 const struct sockaddr_in
*gsin
;
344 struct in_multi
*pinm
;
348 IMO_LOCK_ASSERT_HELD(IMO_CAST_TO_NONCONST(imo
));
350 gsin
= (const struct sockaddr_in
*)group
;
352 /* The imo_membership array may be lazy allocated. */
353 if (imo
->imo_membership
== NULL
|| imo
->imo_num_memberships
== 0)
356 nmships
= imo
->imo_num_memberships
;
357 for (idx
= 0; idx
< nmships
; idx
++) {
358 pinm
= imo
->imo_membership
[idx
];
362 if ((ifp
== NULL
|| (pinm
->inm_ifp
== ifp
)) &&
363 in_hosteq(pinm
->inm_addr
, gsin
->sin_addr
)) {
376 * Find an IPv4 multicast source entry for this imo which matches
377 * the given group index for this socket, and source address.
379 * NOTE: This does not check if the entry is in-mode, merely if
380 * it exists, which may not be the desired behaviour.
382 static struct in_msource
*
383 imo_match_source(const struct ip_moptions
*imo
, const size_t gidx
,
384 const struct sockaddr
*src
)
386 struct ip_msource find
;
387 struct in_mfilter
*imf
;
388 struct ip_msource
*ims
;
389 const sockunion_t
*psa
;
391 IMO_LOCK_ASSERT_HELD(IMO_CAST_TO_NONCONST(imo
));
393 VERIFY(src
->sa_family
== AF_INET
);
394 VERIFY(gidx
!= (size_t)-1 && gidx
< imo
->imo_num_memberships
);
396 /* The imo_mfilters array may be lazy allocated. */
397 if (imo
->imo_mfilters
== NULL
)
399 imf
= &imo
->imo_mfilters
[gidx
];
401 /* Source trees are keyed in host byte order. */
402 psa
= (const sockunion_t
*)src
;
403 find
.ims_haddr
= ntohl(psa
->sin
.sin_addr
.s_addr
);
404 ims
= RB_FIND(ip_msource_tree
, &imf
->imf_sources
, &find
);
406 return ((struct in_msource
*)ims
);
410 * Perform filtering for multicast datagrams on a socket by group and source.
412 * Returns 0 if a datagram should be allowed through, or various error codes
413 * if the socket was not a member of the group, or the source was muted, etc.
416 imo_multi_filter(const struct ip_moptions
*imo
, const struct ifnet
*ifp
,
417 const struct sockaddr
*group
, const struct sockaddr
*src
)
420 struct in_msource
*ims
;
423 IMO_LOCK_ASSERT_HELD(IMO_CAST_TO_NONCONST(imo
));
426 gidx
= imo_match_group(imo
, ifp
, group
);
427 if (gidx
== (size_t)-1)
428 return (MCAST_NOTGMEMBER
);
431 * Check if the source was included in an (S,G) join.
432 * Allow reception on exclusive memberships by default,
433 * reject reception on inclusive memberships by default.
434 * Exclude source only if an in-mode exclude filter exists.
435 * Include source only if an in-mode include filter exists.
436 * NOTE: We are comparing group state here at IGMP t1 (now)
437 * with socket-layer t0 (since last downcall).
439 mode
= imo
->imo_mfilters
[gidx
].imf_st
[1];
440 ims
= imo_match_source(imo
, gidx
, src
);
442 if ((ims
== NULL
&& mode
== MCAST_INCLUDE
) ||
443 (ims
!= NULL
&& ims
->imsl_st
[0] != mode
)) {
444 return (MCAST_NOTSMEMBER
);
451 imo_clone(struct ip_moptions
*from
, struct ip_moptions
*to
)
458 to
->imo_multicast_ifp
= from
->imo_multicast_ifp
;
459 to
->imo_multicast_vif
= from
->imo_multicast_vif
;
460 to
->imo_multicast_ttl
= from
->imo_multicast_ttl
;
461 to
->imo_multicast_loop
= from
->imo_multicast_loop
;
464 * We're cloning, so drop any existing memberships and source
465 * filters on the destination ip_moptions.
467 for (i
= 0; i
< to
->imo_num_memberships
; ++i
) {
468 struct in_mfilter
*imf
;
470 imf
= to
->imo_mfilters
? &to
->imo_mfilters
[i
] : NULL
;
474 (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
);
494 VERIFY(to
->imo_max_memberships
>= from
->imo_max_memberships
);
497 * Source filtering doesn't apply to OpenTransport socket,
498 * so simply hold additional reference count per membership.
500 for (i
= 0; i
< from
->imo_num_memberships
; i
++) {
501 to
->imo_membership
[i
] = from
->imo_membership
[i
];
502 INM_ADDREF(from
->imo_membership
[i
]);
503 to
->imo_num_memberships
++;
505 VERIFY(to
->imo_num_memberships
== from
->imo_num_memberships
);
515 * Find and return a reference to an in_multi record for (ifp, group),
516 * and bump its reference count.
517 * If one does not exist, try to allocate it, and update link-layer multicast
518 * filters on ifp to listen for group.
519 * Return 0 if successful, otherwise return an appropriate error code.
522 in_getmulti(struct ifnet
*ifp
, const struct in_addr
*group
,
523 struct in_multi
**pinm
)
525 struct sockaddr_in gsin
;
526 struct ifmultiaddr
*ifma
;
527 struct in_multi
*inm
;
530 in_multihead_lock_shared();
531 IN_LOOKUP_MULTI(group
, ifp
, inm
);
534 VERIFY(inm
->inm_reqcnt
>= 1);
536 VERIFY(inm
->inm_reqcnt
!= 0);
539 in_multihead_lock_done();
541 * We already joined this group; return the inm
542 * with a refcount held (via lookup) for caller.
546 in_multihead_lock_done();
548 bzero(&gsin
, sizeof(gsin
));
549 gsin
.sin_family
= AF_INET
;
550 gsin
.sin_len
= sizeof(struct sockaddr_in
);
551 gsin
.sin_addr
= *group
;
554 * Check if a link-layer group is already associated
555 * with this network-layer group on the given ifnet.
557 error
= if_addmulti(ifp
, (struct sockaddr
*)&gsin
, &ifma
);
562 * See comments in inm_remref() for access to ifma_protospec.
564 in_multihead_lock_exclusive();
566 if ((inm
= ifma
->ifma_protospec
) != NULL
) {
567 VERIFY(ifma
->ifma_addr
!= NULL
);
568 VERIFY(ifma
->ifma_addr
->sa_family
== AF_INET
);
569 INM_ADDREF(inm
); /* for caller */
572 VERIFY(inm
->inm_ifma
== ifma
);
573 VERIFY(inm
->inm_ifp
== ifp
);
574 VERIFY(in_hosteq(inm
->inm_addr
, *group
));
575 if (inm
->inm_debug
& IFD_ATTACHED
) {
576 VERIFY(inm
->inm_reqcnt
>= 1);
578 VERIFY(inm
->inm_reqcnt
!= 0);
581 in_multihead_lock_done();
584 * We lost the race with another thread doing
585 * in_getmulti(); since this group has already
586 * been joined; return the inm with a refcount
592 * We lost the race with another thread doing in_delmulti();
593 * the inm referring to the ifma has been detached, thus we
594 * reattach it back to the in_multihead list and return the
595 * inm with a refcount held for the caller.
597 in_multi_attach(inm
);
598 VERIFY((inm
->inm_debug
&
599 (IFD_ATTACHED
| IFD_TRASHED
)) == IFD_ATTACHED
);
602 in_multihead_lock_done();
609 * A new in_multi record is needed; allocate and initialize it.
610 * We DO NOT perform an IGMP join as the in_ layer may need to
611 * push an initial source list down to IGMP to support SSM.
613 * The initial source filter state is INCLUDE, {} as per the RFC.
615 inm
= in_multi_alloc(M_WAITOK
);
617 in_multihead_lock_done();
622 inm
->inm_addr
= *group
;
624 inm
->inm_igi
= IGMP_IFINFO(ifp
);
625 VERIFY(inm
->inm_igi
!= NULL
);
626 IGI_ADDREF(inm
->inm_igi
);
627 inm
->inm_ifma
= ifma
; /* keep refcount from if_addmulti() */
628 inm
->inm_state
= IGMP_NOT_MEMBER
;
630 * Pending state-changes per group are subject to a bounds check.
632 inm
->inm_scq
.ifq_maxlen
= IGMP_MAX_STATE_CHANGES
;
633 inm
->inm_st
[0].iss_fmode
= MCAST_UNDEFINED
;
634 inm
->inm_st
[1].iss_fmode
= MCAST_UNDEFINED
;
635 RB_INIT(&inm
->inm_srcs
);
637 in_multi_attach(inm
);
638 VERIFY((inm
->inm_debug
& (IFD_ATTACHED
| IFD_TRASHED
)) == IFD_ATTACHED
);
639 INM_ADDREF_LOCKED(inm
); /* for caller */
643 VERIFY(ifma
->ifma_protospec
== NULL
);
644 ifma
->ifma_protospec
= inm
;
646 in_multihead_lock_done();
652 * Clear recorded source entries for a group.
653 * Used by the IGMP code.
654 * FIXME: Should reap.
657 inm_clear_recorded(struct in_multi
*inm
)
659 struct ip_msource
*ims
;
661 INM_LOCK_ASSERT_HELD(inm
);
663 RB_FOREACH(ims
, ip_msource_tree
, &inm
->inm_srcs
) {
666 --inm
->inm_st
[1].iss_rec
;
669 VERIFY(inm
->inm_st
[1].iss_rec
== 0);
673 * Record a source as pending for a Source-Group IGMPv3 query.
674 * This lives here as it modifies the shared tree.
676 * inm is the group descriptor.
677 * naddr is the address of the source to record in network-byte order.
679 * If the net.inet.igmp.sgalloc sysctl is non-zero, we will
680 * lazy-allocate a source node in response to an SG query.
681 * Otherwise, no allocation is performed. This saves some memory
682 * with the trade-off that the source will not be reported to the
683 * router if joined in the window between the query response and
684 * the group actually being joined on the local host.
686 * Return 0 if the source didn't exist or was already marked as recorded.
687 * Return 1 if the source was marked as recorded by this function.
688 * Return <0 if any error occured (negated errno code).
691 inm_record_source(struct in_multi
*inm
, const in_addr_t naddr
)
693 struct ip_msource find
;
694 struct ip_msource
*ims
, *nims
;
696 INM_LOCK_ASSERT_HELD(inm
);
698 find
.ims_haddr
= ntohl(naddr
);
699 ims
= RB_FIND(ip_msource_tree
, &inm
->inm_srcs
, &find
);
700 if (ims
&& ims
->ims_stp
)
703 if (inm
->inm_nsrc
== in_mcast_maxgrpsrc
)
705 nims
= ipms_alloc(M_WAITOK
);
708 nims
->ims_haddr
= find
.ims_haddr
;
709 RB_INSERT(ip_msource_tree
, &inm
->inm_srcs
, nims
);
715 * Mark the source as recorded and update the recorded
719 ++inm
->inm_st
[1].iss_rec
;
725 * Return a pointer to an in_msource owned by an in_mfilter,
726 * given its source address.
727 * Lazy-allocate if needed. If this is a new entry its filter state is
730 * imf is the filter set being modified.
731 * haddr is the source address in *host* byte-order.
733 * Caller is expected to be holding imo_lock.
736 imf_get_source(struct in_mfilter
*imf
, const struct sockaddr_in
*psin
,
737 struct in_msource
**plims
)
739 struct ip_msource find
;
740 struct ip_msource
*ims
;
741 struct in_msource
*lims
;
748 /* key is host byte order */
749 find
.ims_haddr
= ntohl(psin
->sin_addr
.s_addr
);
750 ims
= RB_FIND(ip_msource_tree
, &imf
->imf_sources
, &find
);
751 lims
= (struct in_msource
*)ims
;
753 if (imf
->imf_nsrc
== in_mcast_maxsocksrc
)
755 lims
= inms_alloc(M_WAITOK
);
758 lims
->ims_haddr
= find
.ims_haddr
;
759 lims
->imsl_st
[0] = MCAST_UNDEFINED
;
760 RB_INSERT(ip_msource_tree
, &imf
->imf_sources
,
761 (struct ip_msource
*)lims
);
771 * Graft a source entry into an existing socket-layer filter set,
772 * maintaining any required invariants and checking allocations.
774 * The source is marked as being in the new filter mode at t1.
776 * Return the pointer to the new node, otherwise return NULL.
778 * Caller is expected to be holding imo_lock.
780 static struct in_msource
*
781 imf_graft(struct in_mfilter
*imf
, const uint8_t st1
,
782 const struct sockaddr_in
*psin
)
784 struct in_msource
*lims
;
786 lims
= inms_alloc(M_WAITOK
);
789 lims
->ims_haddr
= ntohl(psin
->sin_addr
.s_addr
);
790 lims
->imsl_st
[0] = MCAST_UNDEFINED
;
791 lims
->imsl_st
[1] = st1
;
792 RB_INSERT(ip_msource_tree
, &imf
->imf_sources
,
793 (struct ip_msource
*)lims
);
800 * Prune a source entry from an existing socket-layer filter set,
801 * maintaining any required invariants and checking allocations.
803 * The source is marked as being left at t1, it is not freed.
805 * Return 0 if no error occurred, otherwise return an errno value.
807 * Caller is expected to be holding imo_lock.
810 imf_prune(struct in_mfilter
*imf
, const struct sockaddr_in
*psin
)
812 struct ip_msource find
;
813 struct ip_msource
*ims
;
814 struct in_msource
*lims
;
816 /* key is host byte order */
817 find
.ims_haddr
= ntohl(psin
->sin_addr
.s_addr
);
818 ims
= RB_FIND(ip_msource_tree
, &imf
->imf_sources
, &find
);
821 lims
= (struct in_msource
*)ims
;
822 lims
->imsl_st
[1] = MCAST_UNDEFINED
;
827 * Revert socket-layer filter set deltas at t1 to t0 state.
829 * Caller is expected to be holding imo_lock.
832 imf_rollback(struct in_mfilter
*imf
)
834 struct ip_msource
*ims
, *tims
;
835 struct in_msource
*lims
;
837 RB_FOREACH_SAFE(ims
, ip_msource_tree
, &imf
->imf_sources
, tims
) {
838 lims
= (struct in_msource
*)ims
;
839 if (lims
->imsl_st
[0] == lims
->imsl_st
[1]) {
840 /* no change at t1 */
842 } else if (lims
->imsl_st
[0] != MCAST_UNDEFINED
) {
843 /* revert change to existing source at t1 */
844 lims
->imsl_st
[1] = lims
->imsl_st
[0];
846 /* revert source added t1 */
847 IGMP_PRINTF(("%s: free inms %p\n", __func__
, lims
));
848 RB_REMOVE(ip_msource_tree
, &imf
->imf_sources
, ims
);
853 imf
->imf_st
[1] = imf
->imf_st
[0];
857 * Mark socket-layer filter set as INCLUDE {} at t1.
859 * Caller is expected to be holding imo_lock.
862 imf_leave(struct in_mfilter
*imf
)
864 struct ip_msource
*ims
;
865 struct in_msource
*lims
;
867 RB_FOREACH(ims
, ip_msource_tree
, &imf
->imf_sources
) {
868 lims
= (struct in_msource
*)ims
;
869 lims
->imsl_st
[1] = MCAST_UNDEFINED
;
871 imf
->imf_st
[1] = MCAST_INCLUDE
;
875 * Mark socket-layer filter set deltas as committed.
877 * Caller is expected to be holding imo_lock.
880 imf_commit(struct in_mfilter
*imf
)
882 struct ip_msource
*ims
;
883 struct in_msource
*lims
;
885 RB_FOREACH(ims
, ip_msource_tree
, &imf
->imf_sources
) {
886 lims
= (struct in_msource
*)ims
;
887 lims
->imsl_st
[0] = lims
->imsl_st
[1];
889 imf
->imf_st
[0] = imf
->imf_st
[1];
893 * Reap unreferenced sources from socket-layer filter set.
895 * Caller is expected to be holding imo_lock.
898 imf_reap(struct in_mfilter
*imf
)
900 struct ip_msource
*ims
, *tims
;
901 struct in_msource
*lims
;
903 RB_FOREACH_SAFE(ims
, ip_msource_tree
, &imf
->imf_sources
, tims
) {
904 lims
= (struct in_msource
*)ims
;
905 if ((lims
->imsl_st
[0] == MCAST_UNDEFINED
) &&
906 (lims
->imsl_st
[1] == MCAST_UNDEFINED
)) {
907 IGMP_PRINTF(("%s: free inms %p\n", __func__
, lims
));
908 RB_REMOVE(ip_msource_tree
, &imf
->imf_sources
, ims
);
916 * Purge socket-layer filter set.
918 * Caller is expected to be holding imo_lock.
921 imf_purge(struct in_mfilter
*imf
)
923 struct ip_msource
*ims
, *tims
;
924 struct in_msource
*lims
;
926 RB_FOREACH_SAFE(ims
, ip_msource_tree
, &imf
->imf_sources
, tims
) {
927 lims
= (struct in_msource
*)ims
;
928 IGMP_PRINTF(("%s: free inms %p\n", __func__
, lims
));
929 RB_REMOVE(ip_msource_tree
, &imf
->imf_sources
, ims
);
933 imf
->imf_st
[0] = imf
->imf_st
[1] = MCAST_UNDEFINED
;
934 VERIFY(RB_EMPTY(&imf
->imf_sources
));
938 * Look up a source filter entry for a multicast group.
940 * inm is the group descriptor to work with.
941 * haddr is the host-byte-order IPv4 address to look up.
942 * noalloc may be non-zero to suppress allocation of sources.
943 * *pims will be set to the address of the retrieved or allocated source.
945 * Return 0 if successful, otherwise return a non-zero error code.
948 inm_get_source(struct in_multi
*inm
, const in_addr_t haddr
,
949 const int noalloc
, struct ip_msource
**pims
)
951 struct ip_msource find
;
952 struct ip_msource
*ims
, *nims
;
956 INM_LOCK_ASSERT_HELD(inm
);
958 find
.ims_haddr
= haddr
;
959 ims
= RB_FIND(ip_msource_tree
, &inm
->inm_srcs
, &find
);
960 if (ims
== NULL
&& !noalloc
) {
961 if (inm
->inm_nsrc
== in_mcast_maxgrpsrc
)
963 nims
= ipms_alloc(M_WAITOK
);
966 nims
->ims_haddr
= haddr
;
967 RB_INSERT(ip_msource_tree
, &inm
->inm_srcs
, nims
);
971 ia
.s_addr
= htonl(haddr
);
972 IGMP_PRINTF(("%s: allocated %s as %p\n", __func__
,
973 inet_ntoa(ia
), ims
));
982 * Helper function to derive the filter mode on a source entry
983 * from its internal counters. Predicates are:
984 * A source is only excluded if all listeners exclude it.
985 * A source is only included if no listeners exclude it,
986 * and at least one listener includes it.
987 * May be used by ifmcstat(8).
990 ims_get_mode(const struct in_multi
*inm
, const struct ip_msource
*ims
,
993 INM_LOCK_ASSERT_HELD(INM_CAST_TO_NONCONST(inm
));
996 if (inm
->inm_st
[t
].iss_ex
> 0 &&
997 inm
->inm_st
[t
].iss_ex
== ims
->ims_st
[t
].ex
)
998 return (MCAST_EXCLUDE
);
999 else if (ims
->ims_st
[t
].in
> 0 && ims
->ims_st
[t
].ex
== 0)
1000 return (MCAST_INCLUDE
);
1001 return (MCAST_UNDEFINED
);
1005 * Merge socket-layer source into IGMP-layer source.
1006 * If rollback is non-zero, perform the inverse of the merge.
1009 ims_merge(struct ip_msource
*ims
, const struct in_msource
*lims
,
1012 int n
= rollback
? -1 : 1;
1016 ia
.s_addr
= htonl(ims
->ims_haddr
);
1019 if (lims
->imsl_st
[0] == MCAST_EXCLUDE
) {
1020 IGMP_PRINTF(("%s: t1 ex -= %d on %s\n",
1021 __func__
, n
, inet_ntoa(ia
)));
1022 ims
->ims_st
[1].ex
-= n
;
1023 } else if (lims
->imsl_st
[0] == MCAST_INCLUDE
) {
1024 IGMP_PRINTF(("%s: t1 in -= %d on %s\n",
1025 __func__
, n
, inet_ntoa(ia
)));
1026 ims
->ims_st
[1].in
-= n
;
1029 if (lims
->imsl_st
[1] == MCAST_EXCLUDE
) {
1030 IGMP_PRINTF(("%s: t1 ex += %d on %s\n",
1031 __func__
, n
, inet_ntoa(ia
)));
1032 ims
->ims_st
[1].ex
+= n
;
1033 } else if (lims
->imsl_st
[1] == MCAST_INCLUDE
) {
1034 IGMP_PRINTF(("%s: t1 in += %d on %s\n",
1035 __func__
, n
, inet_ntoa(ia
)));
1036 ims
->ims_st
[1].in
+= n
;
1041 * Atomically update the global in_multi state, when a membership's
1042 * filter list is being updated in any way.
1044 * imf is the per-inpcb-membership group filter pointer.
1045 * A fake imf may be passed for in-kernel consumers.
1047 * XXX This is a candidate for a set-symmetric-difference style loop
1048 * which would eliminate the repeated lookup from root of ims nodes,
1049 * as they share the same key space.
1051 * If any error occurred this function will back out of refcounts
1052 * and return a non-zero value.
1055 inm_merge(struct in_multi
*inm
, /*const*/ struct in_mfilter
*imf
)
1057 struct ip_msource
*ims
, *nims
;
1058 struct in_msource
*lims
;
1059 int schanged
, error
;
1062 INM_LOCK_ASSERT_HELD(inm
);
1069 * Update the source filters first, as this may fail.
1070 * Maintain count of in-mode filters at t0, t1. These are
1071 * used to work out if we transition into ASM mode or not.
1072 * Maintain a count of source filters whose state was
1073 * actually modified by this operation.
1075 RB_FOREACH(ims
, ip_msource_tree
, &imf
->imf_sources
) {
1076 lims
= (struct in_msource
*)ims
;
1077 if (lims
->imsl_st
[0] == imf
->imf_st
[0]) nsrc0
++;
1078 if (lims
->imsl_st
[1] == imf
->imf_st
[1]) nsrc1
++;
1079 if (lims
->imsl_st
[0] == lims
->imsl_st
[1]) continue;
1080 error
= inm_get_source(inm
, lims
->ims_haddr
, 0, &nims
);
1084 ims_merge(nims
, lims
, 0);
1087 struct ip_msource
*bims
;
1089 RB_FOREACH_REVERSE_FROM(ims
, ip_msource_tree
, nims
) {
1090 lims
= (struct in_msource
*)ims
;
1091 if (lims
->imsl_st
[0] == lims
->imsl_st
[1])
1093 (void) inm_get_source(inm
, lims
->ims_haddr
, 1, &bims
);
1096 ims_merge(bims
, lims
, 1);
1101 IGMP_PRINTF(("%s: imf filters in-mode: %d at t0, %d at t1\n",
1102 __func__
, nsrc0
, nsrc1
));
1104 /* Handle transition between INCLUDE {n} and INCLUDE {} on socket. */
1105 if (imf
->imf_st
[0] == imf
->imf_st
[1] &&
1106 imf
->imf_st
[1] == MCAST_INCLUDE
) {
1108 IGMP_PRINTF(("%s: --in on inm at t1\n", __func__
));
1109 --inm
->inm_st
[1].iss_in
;
1113 /* Handle filter mode transition on socket. */
1114 if (imf
->imf_st
[0] != imf
->imf_st
[1]) {
1115 IGMP_PRINTF(("%s: imf transition %d to %d\n",
1116 __func__
, imf
->imf_st
[0], imf
->imf_st
[1]));
1118 if (imf
->imf_st
[0] == MCAST_EXCLUDE
) {
1119 IGMP_PRINTF(("%s: --ex on inm at t1\n", __func__
));
1120 --inm
->inm_st
[1].iss_ex
;
1121 } else if (imf
->imf_st
[0] == MCAST_INCLUDE
) {
1122 IGMP_PRINTF(("%s: --in on inm at t1\n", __func__
));
1123 --inm
->inm_st
[1].iss_in
;
1126 if (imf
->imf_st
[1] == MCAST_EXCLUDE
) {
1127 IGMP_PRINTF(("%s: ex++ on inm at t1\n", __func__
));
1128 inm
->inm_st
[1].iss_ex
++;
1129 } else if (imf
->imf_st
[1] == MCAST_INCLUDE
&& nsrc1
> 0) {
1130 IGMP_PRINTF(("%s: in++ on inm at t1\n", __func__
));
1131 inm
->inm_st
[1].iss_in
++;
1136 * Track inm filter state in terms of listener counts.
1137 * If there are any exclusive listeners, stack-wide
1138 * membership is exclusive.
1139 * Otherwise, if only inclusive listeners, stack-wide is inclusive.
1140 * If no listeners remain, state is undefined at t1,
1141 * and the IGMP lifecycle for this group should finish.
1143 if (inm
->inm_st
[1].iss_ex
> 0) {
1144 IGMP_PRINTF(("%s: transition to EX\n", __func__
));
1145 inm
->inm_st
[1].iss_fmode
= MCAST_EXCLUDE
;
1146 } else if (inm
->inm_st
[1].iss_in
> 0) {
1147 IGMP_PRINTF(("%s: transition to IN\n", __func__
));
1148 inm
->inm_st
[1].iss_fmode
= MCAST_INCLUDE
;
1150 IGMP_PRINTF(("%s: transition to UNDEF\n", __func__
));
1151 inm
->inm_st
[1].iss_fmode
= MCAST_UNDEFINED
;
1154 /* Decrement ASM listener count on transition out of ASM mode. */
1155 if (imf
->imf_st
[0] == MCAST_EXCLUDE
&& nsrc0
== 0) {
1156 if ((imf
->imf_st
[1] != MCAST_EXCLUDE
) ||
1157 (imf
->imf_st
[1] == MCAST_EXCLUDE
&& nsrc1
> 0)) {
1158 IGMP_PRINTF(("%s: --asm on inm at t1\n", __func__
));
1159 --inm
->inm_st
[1].iss_asm
;
1163 /* Increment ASM listener count on transition to ASM mode. */
1164 if (imf
->imf_st
[1] == MCAST_EXCLUDE
&& nsrc1
== 0) {
1165 IGMP_PRINTF(("%s: asm++ on inm at t1\n", __func__
));
1166 inm
->inm_st
[1].iss_asm
++;
1169 IGMP_PRINTF(("%s: merged imf %p to inm %p\n", __func__
, imf
, inm
));
1174 IGMP_PRINTF(("%s: sources changed; reaping\n", __func__
));
1181 * Mark an in_multi's filter set deltas as committed.
1182 * Called by IGMP after a state change has been enqueued.
1185 inm_commit(struct in_multi
*inm
)
1187 struct ip_msource
*ims
;
1189 INM_LOCK_ASSERT_HELD(inm
);
1191 IGMP_PRINTF(("%s: commit inm %p\n", __func__
, inm
));
1192 IGMP_PRINTF(("%s: pre commit:\n", __func__
));
1195 RB_FOREACH(ims
, ip_msource_tree
, &inm
->inm_srcs
) {
1196 ims
->ims_st
[0] = ims
->ims_st
[1];
1198 inm
->inm_st
[0] = inm
->inm_st
[1];
1202 * Reap unreferenced nodes from an in_multi's filter set.
1205 inm_reap(struct in_multi
*inm
)
1207 struct ip_msource
*ims
, *tims
;
1209 INM_LOCK_ASSERT_HELD(inm
);
1211 RB_FOREACH_SAFE(ims
, ip_msource_tree
, &inm
->inm_srcs
, tims
) {
1212 if (ims
->ims_st
[0].ex
> 0 || ims
->ims_st
[0].in
> 0 ||
1213 ims
->ims_st
[1].ex
> 0 || ims
->ims_st
[1].in
> 0 ||
1216 IGMP_PRINTF(("%s: free ims %p\n", __func__
, ims
));
1217 RB_REMOVE(ip_msource_tree
, &inm
->inm_srcs
, ims
);
1224 * Purge all source nodes from an in_multi's filter set.
1227 inm_purge(struct in_multi
*inm
)
1229 struct ip_msource
*ims
, *tims
;
1231 INM_LOCK_ASSERT_HELD(inm
);
1233 RB_FOREACH_SAFE(ims
, ip_msource_tree
, &inm
->inm_srcs
, tims
) {
1234 IGMP_PRINTF(("%s: free ims %p\n", __func__
, ims
));
1235 RB_REMOVE(ip_msource_tree
, &inm
->inm_srcs
, ims
);
1242 * Join a multicast group; real entry point.
1244 * Only preserves atomicity at inm level.
1245 * NOTE: imf argument cannot be const due to sys/tree.h limitations.
1247 * If the IGMP downcall fails, the group is not joined, and an error
1251 in_joingroup(struct ifnet
*ifp
, const struct in_addr
*gina
,
1252 /*const*/ struct in_mfilter
*imf
, struct in_multi
**pinm
)
1254 struct in_mfilter timf
;
1255 struct in_multi
*inm
= NULL
;
1258 IGMP_PRINTF(("%s: join %s on %p(%s%d))\n", __func__
,
1259 inet_ntoa(*gina
), 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 imf_init(&timf
, MCAST_UNDEFINED
, MCAST_EXCLUDE
);
1272 error
= in_getmulti(ifp
, gina
, &inm
);
1274 IGMP_PRINTF(("%s: in_getmulti() failure\n", __func__
));
1278 IGMP_PRINTF(("%s: merge inm state\n", __func__
));
1281 error
= inm_merge(inm
, imf
);
1283 IGMP_PRINTF(("%s: failed to merge inm state\n", __func__
));
1284 goto out_inm_release
;
1287 IGMP_PRINTF(("%s: doing igmp downcall\n", __func__
));
1288 error
= igmp_change_state(inm
);
1290 IGMP_PRINTF(("%s: failed to update source\n", __func__
));
1291 goto out_inm_release
;
1296 IGMP_PRINTF(("%s: dropping ref on %p\n", __func__
, inm
));
1301 *pinm
= inm
; /* keep refcount from in_getmulti() */
1308 * Leave a multicast group; real entry point.
1309 * All source filters will be expunged.
1311 * Only preserves atomicity at inm level.
1313 * Note: This is not the same as inm_release(*) as this function also
1314 * makes a state change downcall into IGMP.
1317 in_leavegroup(struct in_multi
*inm
, /*const*/ struct in_mfilter
*imf
)
1319 struct in_mfilter timf
;
1324 INM_LOCK_ASSERT_NOTHELD(inm
);
1326 in_multihead_lock_exclusive();
1329 IGMP_PRINTF(("%s: leave inm %p, %s/%s%d, imf %p\n", __func__
,
1330 inm
, inet_ntoa(inm
->inm_addr
),
1331 (inm_is_ifp_detached(inm
) ? "null" : inm
->inm_ifp
->if_name
),
1332 inm
->inm_ifp
->if_unit
, imf
));
1335 * If no imf was specified (i.e. kernel consumer),
1336 * fake one up and assume it is an ASM join.
1339 imf_init(&timf
, MCAST_EXCLUDE
, MCAST_UNDEFINED
);
1344 * Begin state merge transaction at IGMP layer.
1346 * As this particular invocation should not cause any memory
1347 * to be allocated, and there is no opportunity to roll back
1348 * the transaction, it MUST NOT fail.
1350 IGMP_PRINTF(("%s: merge inm state\n", __func__
));
1352 error
= inm_merge(inm
, imf
);
1353 KASSERT(error
== 0, ("%s: failed to merge inm state\n", __func__
));
1355 IGMP_PRINTF(("%s: doing igmp downcall\n", __func__
));
1356 error
= igmp_change_state(inm
);
1359 IGMP_PRINTF(("%s: failed igmp downcall\n", __func__
));
1361 lastref
= in_multi_detach(inm
);
1362 VERIFY(!lastref
|| (!(inm
->inm_debug
& IFD_ATTACHED
) &&
1363 inm
->inm_reqcnt
== 0));
1365 in_multihead_lock_done();
1368 INM_REMREF(inm
); /* for in_multihead list */
1374 * Join an IPv4 multicast group in (*,G) exclusive mode.
1375 * The group must be a 224.0.0.0/24 link-scope group.
1376 * This KPI is for legacy kernel consumers only.
1379 in_addmulti(struct in_addr
*ap
, struct ifnet
*ifp
)
1381 struct in_multi
*pinm
= NULL
;
1384 KASSERT(IN_LOCAL_GROUP(ntohl(ap
->s_addr
)),
1385 ("%s: %s not in 224.0.0.0/24\n", __func__
, inet_ntoa(*ap
)));
1387 error
= in_joingroup(ifp
, ap
, NULL
, &pinm
);
1388 VERIFY(pinm
!= NULL
|| error
!= 0);
1394 * Leave an IPv4 multicast group, assumed to be in exclusive (*,G) mode.
1395 * This KPI is for legacy kernel consumers only.
1398 in_delmulti(struct in_multi
*inm
)
1401 (void) in_leavegroup(inm
, NULL
);
1405 * Block or unblock an ASM multicast source on an inpcb.
1406 * This implements the delta-based API described in RFC 3678.
1408 * The delta-based API applies only to exclusive-mode memberships.
1409 * An IGMP downcall will be performed.
1411 * Return 0 if successful, otherwise return an appropriate error code.
1414 inp_block_unblock_source(struct inpcb
*inp
, struct sockopt
*sopt
)
1416 struct group_source_req gsr
;
1417 sockunion_t
*gsa
, *ssa
;
1419 struct in_mfilter
*imf
;
1420 struct ip_moptions
*imo
;
1421 struct in_msource
*ims
;
1422 struct in_multi
*inm
;
1426 unsigned int ifindex
= 0;
1432 memset(&gsr
, 0, sizeof(struct group_source_req
));
1433 gsa
= (sockunion_t
*)&gsr
.gsr_group
;
1434 ssa
= (sockunion_t
*)&gsr
.gsr_source
;
1436 switch (sopt
->sopt_name
) {
1437 case IP_BLOCK_SOURCE
:
1438 case IP_UNBLOCK_SOURCE
: {
1439 struct ip_mreq_source mreqs
;
1441 error
= sooptcopyin(sopt
, &mreqs
,
1442 sizeof(struct ip_mreq_source
),
1443 sizeof(struct ip_mreq_source
));
1447 gsa
->sin
.sin_family
= AF_INET
;
1448 gsa
->sin
.sin_len
= sizeof(struct sockaddr_in
);
1449 gsa
->sin
.sin_addr
= mreqs
.imr_multiaddr
;
1451 ssa
->sin
.sin_family
= AF_INET
;
1452 ssa
->sin
.sin_len
= sizeof(struct sockaddr_in
);
1453 ssa
->sin
.sin_addr
= mreqs
.imr_sourceaddr
;
1455 if (!in_nullhost(mreqs
.imr_interface
))
1456 ifp
= ip_multicast_if(&mreqs
.imr_interface
, &ifindex
);
1458 if (sopt
->sopt_name
== IP_BLOCK_SOURCE
)
1461 IGMP_PRINTF(("%s: imr_interface = %s, ifp = %p\n",
1462 __func__
, inet_ntoa(mreqs
.imr_interface
), ifp
));
1466 case MCAST_BLOCK_SOURCE
:
1467 case MCAST_UNBLOCK_SOURCE
:
1468 error
= sooptcopyin(sopt
, &gsr
,
1469 sizeof(struct group_source_req
),
1470 sizeof(struct group_source_req
));
1474 if (gsa
->sin
.sin_family
!= AF_INET
||
1475 gsa
->sin
.sin_len
!= sizeof(struct sockaddr_in
))
1478 if (ssa
->sin
.sin_family
!= AF_INET
||
1479 ssa
->sin
.sin_len
!= sizeof(struct sockaddr_in
))
1482 ifnet_head_lock_shared();
1483 if (gsr
.gsr_interface
== 0 ||
1484 (u_int
)if_index
< gsr
.gsr_interface
) {
1486 return (EADDRNOTAVAIL
);
1489 ifp
= ifindex2ifnet
[gsr
.gsr_interface
];
1493 return (EADDRNOTAVAIL
);
1495 if (sopt
->sopt_name
== MCAST_BLOCK_SOURCE
)
1500 IGMP_PRINTF(("%s: unknown sopt_name %d\n",
1501 __func__
, sopt
->sopt_name
));
1502 return (EOPNOTSUPP
);
1506 if (!IN_MULTICAST(ntohl(gsa
->sin
.sin_addr
.s_addr
)))
1510 * Check if we are actually a member of this group.
1512 imo
= inp_findmoptions(inp
);
1517 idx
= imo_match_group(imo
, ifp
, &gsa
->sa
);
1518 if (idx
== (size_t)-1 || imo
->imo_mfilters
== NULL
) {
1519 error
= EADDRNOTAVAIL
;
1520 goto out_imo_locked
;
1523 VERIFY(imo
->imo_mfilters
!= NULL
);
1524 imf
= &imo
->imo_mfilters
[idx
];
1525 inm
= imo
->imo_membership
[idx
];
1528 * Attempting to use the delta-based API on an
1529 * non exclusive-mode membership is an error.
1531 fmode
= imf
->imf_st
[0];
1532 if (fmode
!= MCAST_EXCLUDE
) {
1534 goto out_imo_locked
;
1538 * Deal with error cases up-front:
1539 * Asked to block, but already blocked; or
1540 * Asked to unblock, but nothing to unblock.
1541 * If adding a new block entry, allocate it.
1543 ims
= imo_match_source(imo
, idx
, &ssa
->sa
);
1544 if ((ims
!= NULL
&& doblock
) || (ims
== NULL
&& !doblock
)) {
1545 IGMP_PRINTF(("%s: source %s %spresent\n", __func__
,
1546 inet_ntoa(ssa
->sin
.sin_addr
), doblock
? "" : "not "));
1547 error
= EADDRNOTAVAIL
;
1548 goto out_imo_locked
;
1552 * Begin state merge transaction at socket layer.
1555 IGMP_PRINTF(("%s: %s source\n", __func__
, "block"));
1556 ims
= imf_graft(imf
, fmode
, &ssa
->sin
);
1560 IGMP_PRINTF(("%s: %s source\n", __func__
, "allow"));
1561 error
= imf_prune(imf
, &ssa
->sin
);
1565 IGMP_PRINTF(("%s: merge imf state failed\n", __func__
));
1566 goto out_imf_rollback
;
1570 * Begin state merge transaction at IGMP layer.
1573 IGMP_PRINTF(("%s: merge inm state\n", __func__
));
1574 error
= inm_merge(inm
, imf
);
1576 IGMP_PRINTF(("%s: failed to merge inm state\n", __func__
));
1578 goto out_imf_rollback
;
1581 IGMP_PRINTF(("%s: doing igmp downcall\n", __func__
));
1582 error
= igmp_change_state(inm
);
1586 IGMP_PRINTF(("%s: failed igmp downcall\n", __func__
));
1599 IMO_REMREF(imo
); /* from inp_findmoptions() */
1604 * Given an inpcb, return its multicast options structure pointer.
1606 * Caller is responsible for locking the inpcb, and releasing the
1607 * extra reference held on the imo, upon a successful return.
1609 static struct ip_moptions
*
1610 inp_findmoptions(struct inpcb
*inp
)
1612 struct ip_moptions
*imo
;
1613 struct in_multi
**immp
;
1614 struct in_mfilter
*imfp
;
1617 if ((imo
= inp
->inp_moptions
) != NULL
) {
1618 IMO_ADDREF(imo
); /* for caller */
1622 imo
= ip_allocmoptions(M_WAITOK
);
1626 immp
= _MALLOC(sizeof (*immp
) * IP_MIN_MEMBERSHIPS
, M_IPMOPTS
,
1633 imfp
= _MALLOC(sizeof (struct in_mfilter
) * IP_MIN_MEMBERSHIPS
,
1634 M_INMFILTER
, M_WAITOK
| M_ZERO
);
1636 _FREE(immp
, M_IPMOPTS
);
1641 imo
->imo_multicast_ifp
= NULL
;
1642 imo
->imo_multicast_addr
.s_addr
= INADDR_ANY
;
1643 imo
->imo_multicast_vif
= -1;
1644 imo
->imo_multicast_ttl
= IP_DEFAULT_MULTICAST_TTL
;
1645 imo
->imo_multicast_loop
= in_mcast_loop
;
1646 imo
->imo_num_memberships
= 0;
1647 imo
->imo_max_memberships
= IP_MIN_MEMBERSHIPS
;
1648 imo
->imo_membership
= immp
;
1650 /* Initialize per-group source filters. */
1651 for (idx
= 0; idx
< IP_MIN_MEMBERSHIPS
; idx
++)
1652 imf_init(&imfp
[idx
], MCAST_UNDEFINED
, MCAST_EXCLUDE
);
1654 imo
->imo_mfilters
= imfp
;
1655 inp
->inp_moptions
= imo
; /* keep reference from ip_allocmoptions() */
1656 IMO_ADDREF(imo
); /* for caller */
1661 * Atomically get source filters on a socket for an IPv4 multicast group.
1664 inp_get_source_filters(struct inpcb
*inp
, struct sockopt
*sopt
)
1666 struct __msfilterreq64 msfr
, msfr64
;
1667 struct __msfilterreq32 msfr32
;
1670 struct ip_moptions
*imo
;
1671 struct in_mfilter
*imf
;
1672 struct ip_msource
*ims
;
1673 struct in_msource
*lims
;
1674 struct sockaddr_in
*psin
;
1675 struct sockaddr_storage
*ptss
;
1676 struct sockaddr_storage
*tss
;
1678 size_t idx
, nsrcs
, ncsrcs
;
1679 user_addr_t tmp_ptr
;
1681 imo
= inp
->inp_moptions
;
1682 VERIFY(imo
!= NULL
);
1684 if (IS_64BIT_PROCESS(current_proc())) {
1685 error
= sooptcopyin(sopt
, &msfr64
,
1686 sizeof(struct __msfilterreq64
),
1687 sizeof(struct __msfilterreq64
));
1690 /* we never use msfr.msfr_srcs; */
1691 memcpy(&msfr
, &msfr64
, sizeof(msfr
));
1693 error
= sooptcopyin(sopt
, &msfr32
,
1694 sizeof(struct __msfilterreq32
),
1695 sizeof(struct __msfilterreq32
));
1698 /* we never use msfr.msfr_srcs; */
1699 memcpy(&msfr
, &msfr32
, sizeof(msfr
));
1702 ifnet_head_lock_shared();
1703 if (msfr
.msfr_ifindex
== 0 || (u_int
)if_index
< msfr
.msfr_ifindex
) {
1705 return (EADDRNOTAVAIL
);
1708 ifp
= ifindex2ifnet
[msfr
.msfr_ifindex
];
1712 return (EADDRNOTAVAIL
);
1714 if (msfr
.msfr_nsrcs
> in_mcast_maxsocksrc
)
1715 msfr
.msfr_nsrcs
= in_mcast_maxsocksrc
;
1719 * Lookup group on the socket.
1721 gsa
= (sockunion_t
*)&msfr
.msfr_group
;
1722 idx
= imo_match_group(imo
, ifp
, &gsa
->sa
);
1723 if (idx
== (size_t)-1 || imo
->imo_mfilters
== NULL
) {
1725 return (EADDRNOTAVAIL
);
1727 imf
= &imo
->imo_mfilters
[idx
];
1730 * Ignore memberships which are in limbo.
1732 if (imf
->imf_st
[1] == MCAST_UNDEFINED
) {
1736 msfr
.msfr_fmode
= imf
->imf_st
[1];
1739 * If the user specified a buffer, copy out the source filter
1740 * entries to userland gracefully.
1741 * We only copy out the number of entries which userland
1742 * has asked for, but we always tell userland how big the
1743 * buffer really needs to be.
1746 if (IS_64BIT_PROCESS(current_proc()))
1747 tmp_ptr
= msfr64
.msfr_srcs
;
1749 tmp_ptr
= CAST_USER_ADDR_T(msfr32
.msfr_srcs
);
1752 if (tmp_ptr
!= USER_ADDR_NULL
&& msfr
.msfr_nsrcs
> 0) {
1753 tss
= _MALLOC(sizeof(struct sockaddr_storage
) * msfr
.msfr_nsrcs
,
1754 M_TEMP
, M_WAITOK
| M_ZERO
);
1762 * Count number of sources in-mode at t0.
1763 * If buffer space exists and remains, copy out source entries.
1765 nsrcs
= msfr
.msfr_nsrcs
;
1768 RB_FOREACH(ims
, ip_msource_tree
, &imf
->imf_sources
) {
1769 lims
= (struct in_msource
*)ims
;
1770 if (lims
->imsl_st
[0] == MCAST_UNDEFINED
||
1771 lims
->imsl_st
[0] != imf
->imf_st
[0])
1773 if (tss
!= NULL
&& nsrcs
> 0) {
1774 psin
= (struct sockaddr_in
*)ptss
;
1775 psin
->sin_family
= AF_INET
;
1776 psin
->sin_len
= sizeof(struct sockaddr_in
);
1777 psin
->sin_addr
.s_addr
= htonl(lims
->ims_haddr
);
1788 error
= copyout(tss
, tmp_ptr
,
1789 sizeof(struct sockaddr_storage
) * ncsrcs
);
1795 msfr
.msfr_nsrcs
= ncsrcs
;
1796 if (IS_64BIT_PROCESS(current_proc())) {
1797 msfr64
.msfr_ifindex
= msfr
.msfr_ifindex
;
1798 msfr64
.msfr_fmode
= msfr
.msfr_fmode
;
1799 msfr64
.msfr_nsrcs
= msfr
.msfr_nsrcs
;
1800 memcpy(&msfr64
.msfr_group
, &msfr
.msfr_group
,
1801 sizeof(struct sockaddr_storage
));
1802 error
= sooptcopyout(sopt
, &msfr64
,
1803 sizeof(struct __msfilterreq64
));
1805 msfr32
.msfr_ifindex
= msfr
.msfr_ifindex
;
1806 msfr32
.msfr_fmode
= msfr
.msfr_fmode
;
1807 msfr32
.msfr_nsrcs
= msfr
.msfr_nsrcs
;
1808 memcpy(&msfr64
.msfr_group
, &msfr
.msfr_group
,
1809 sizeof(struct sockaddr_storage
));
1810 error
= sooptcopyout(sopt
, &msfr32
,
1811 sizeof(struct __msfilterreq32
));
1818 * Return the IP multicast options in response to user getsockopt().
1821 inp_getmoptions(struct inpcb
*inp
, struct sockopt
*sopt
)
1823 struct ip_mreqn mreqn
;
1824 struct ip_moptions
*imo
;
1826 struct in_ifaddr
*ia
;
1828 unsigned int ifindex
;
1831 imo
= inp
->inp_moptions
;
1833 * If socket is neither of type SOCK_RAW or SOCK_DGRAM,
1834 * or is a divert socket, reject it.
1836 if (inp
->inp_socket
->so_proto
->pr_protocol
== IPPROTO_DIVERT
||
1837 (inp
->inp_socket
->so_proto
->pr_type
!= SOCK_RAW
&&
1838 inp
->inp_socket
->so_proto
->pr_type
!= SOCK_DGRAM
)) {
1839 return (EOPNOTSUPP
);
1843 switch (sopt
->sopt_name
) {
1845 case IP_MULTICAST_VIF
:
1848 optval
= imo
->imo_multicast_vif
;
1852 error
= sooptcopyout(sopt
, &optval
, sizeof(int));
1854 #endif /* MROUTING */
1856 case IP_MULTICAST_IF
:
1857 memset(&mreqn
, 0, sizeof(struct ip_mreqn
));
1860 ifp
= imo
->imo_multicast_ifp
;
1861 if (!in_nullhost(imo
->imo_multicast_addr
)) {
1862 mreqn
.imr_address
= imo
->imo_multicast_addr
;
1863 } else if (ifp
!= NULL
) {
1864 mreqn
.imr_ifindex
= ifp
->if_index
;
1867 IFA_LOCK_SPIN(&ia
->ia_ifa
);
1869 IA_SIN(ia
)->sin_addr
;
1870 IFA_UNLOCK(&ia
->ia_ifa
);
1871 IFA_REMREF(&ia
->ia_ifa
);
1876 if (sopt
->sopt_valsize
== sizeof(struct ip_mreqn
)) {
1877 error
= sooptcopyout(sopt
, &mreqn
,
1878 sizeof(struct ip_mreqn
));
1880 error
= sooptcopyout(sopt
, &mreqn
.imr_address
,
1881 sizeof(struct in_addr
));
1885 case IP_MULTICAST_IFINDEX
:
1888 if (imo
== NULL
|| imo
->imo_multicast_ifp
== NULL
) {
1891 ifindex
= imo
->imo_multicast_ifp
->if_index
;
1895 error
= sooptcopyout(sopt
, &ifindex
, sizeof (ifindex
));
1898 case IP_MULTICAST_TTL
:
1900 optval
= coptval
= IP_DEFAULT_MULTICAST_TTL
;
1903 optval
= coptval
= imo
->imo_multicast_ttl
;
1906 if (sopt
->sopt_valsize
== sizeof(u_char
))
1907 error
= sooptcopyout(sopt
, &coptval
, sizeof(u_char
));
1909 error
= sooptcopyout(sopt
, &optval
, sizeof(int));
1912 case IP_MULTICAST_LOOP
:
1914 optval
= coptval
= IP_DEFAULT_MULTICAST_LOOP
;
1917 optval
= coptval
= imo
->imo_multicast_loop
;
1920 if (sopt
->sopt_valsize
== sizeof(u_char
))
1921 error
= sooptcopyout(sopt
, &coptval
, sizeof(u_char
));
1923 error
= sooptcopyout(sopt
, &optval
, sizeof(int));
1928 error
= EADDRNOTAVAIL
;
1930 error
= inp_get_source_filters(inp
, sopt
);
1935 error
= ENOPROTOOPT
;
1943 * Look up the ifnet to use for a multicast group membership,
1944 * given the IPv4 address of an interface, and the IPv4 group address.
1946 * This routine exists to support legacy multicast applications
1947 * which do not understand that multicast memberships are scoped to
1948 * specific physical links in the networking stack, or which need
1949 * to join link-scope groups before IPv4 addresses are configured.
1951 * If inp is non-NULL and is bound to an interface, use this socket's
1952 * inp_boundif for any required routing table lookup.
1954 * If the route lookup fails, attempt to use the first non-loopback
1955 * interface with multicast capability in the system as a
1956 * last resort. The legacy IPv4 ASM API requires that we do
1957 * this in order to allow groups to be joined when the routing
1958 * table has not yet been populated during boot.
1960 * Returns NULL if no ifp could be found.
1963 static struct ifnet
*
1964 inp_lookup_mcast_ifp(const struct inpcb
*inp
,
1965 const struct sockaddr_in
*gsin
, const struct in_addr ina
)
1968 unsigned int ifindex
= 0;
1970 VERIFY(gsin
->sin_family
== AF_INET
);
1971 VERIFY(IN_MULTICAST(ntohl(gsin
->sin_addr
.s_addr
)));
1974 if (!in_nullhost(ina
)) {
1975 struct in_addr new_ina
;
1976 memcpy(&new_ina
, &ina
, sizeof(struct in_addr
));
1977 ifp
= ip_multicast_if(&new_ina
, &ifindex
);
1980 unsigned int ifscope
= IFSCOPE_NONE
;
1982 if (inp
!= NULL
&& (inp
->inp_flags
& INP_BOUND_IF
))
1983 ifscope
= inp
->inp_boundif
;
1985 bzero(&ro
, sizeof (ro
));
1986 memcpy(&ro
.ro_dst
, gsin
, sizeof(struct sockaddr_in
));
1987 rtalloc_scoped_ign(&ro
, 0, ifscope
);
1988 if (ro
.ro_rt
!= NULL
) {
1989 ifp
= ro
.ro_rt
->rt_ifp
;
1990 VERIFY(ifp
!= NULL
);
1993 struct in_ifaddr
*ia
;
1997 lck_rw_lock_shared(in_ifaddr_rwlock
);
1998 TAILQ_FOREACH(ia
, &in_ifaddrhead
, ia_link
) {
1999 IFA_LOCK_SPIN(&ia
->ia_ifa
);
2001 IFA_UNLOCK(&ia
->ia_ifa
);
2002 if (!(mifp
->if_flags
& IFF_LOOPBACK
) &&
2003 (mifp
->if_flags
& IFF_MULTICAST
)) {
2008 lck_rw_done(in_ifaddr_rwlock
);
2016 * Join an IPv4 multicast group, possibly with a source.
2018 * NB: sopt->sopt_val might point to the kernel address space. This means that
2019 * we were called by the IPv6 stack due to the presence of an IPv6 v4 mapped
2020 * address. In this scenario, sopt_p points to kernproc and sooptcopyin() will
2021 * just issue an in-kernel memcpy.
2024 inp_join_group(struct inpcb
*inp
, struct sockopt
*sopt
)
2026 struct group_source_req gsr
;
2027 sockunion_t
*gsa
, *ssa
;
2029 struct in_mfilter
*imf
;
2030 struct ip_moptions
*imo
;
2031 struct in_multi
*inm
= NULL
;
2032 struct in_msource
*lims
;
2041 memset(&gsr
, 0, sizeof(struct group_source_req
));
2042 gsa
= (sockunion_t
*)&gsr
.gsr_group
;
2043 gsa
->ss
.ss_family
= AF_UNSPEC
;
2044 ssa
= (sockunion_t
*)&gsr
.gsr_source
;
2045 ssa
->ss
.ss_family
= AF_UNSPEC
;
2047 switch (sopt
->sopt_name
) {
2048 case IP_ADD_MEMBERSHIP
:
2049 case IP_ADD_SOURCE_MEMBERSHIP
: {
2050 struct ip_mreq_source mreqs
;
2052 if (sopt
->sopt_name
== IP_ADD_MEMBERSHIP
) {
2053 error
= sooptcopyin(sopt
, &mreqs
,
2054 sizeof(struct ip_mreq
),
2055 sizeof(struct ip_mreq
));
2057 * Do argument switcharoo from ip_mreq into
2058 * ip_mreq_source to avoid using two instances.
2060 mreqs
.imr_interface
= mreqs
.imr_sourceaddr
;
2061 mreqs
.imr_sourceaddr
.s_addr
= INADDR_ANY
;
2062 } else if (sopt
->sopt_name
== IP_ADD_SOURCE_MEMBERSHIP
) {
2063 error
= sooptcopyin(sopt
, &mreqs
,
2064 sizeof(struct ip_mreq_source
),
2065 sizeof(struct ip_mreq_source
));
2068 IGMP_PRINTF(("%s: error copyin IP_ADD_MEMBERSHIP/"
2069 "IP_ADD_SOURCE_MEMBERSHIP %d err=%d\n",
2070 __func__
, sopt
->sopt_name
, error
));
2074 gsa
->sin
.sin_family
= AF_INET
;
2075 gsa
->sin
.sin_len
= sizeof(struct sockaddr_in
);
2076 gsa
->sin
.sin_addr
= mreqs
.imr_multiaddr
;
2078 if (sopt
->sopt_name
== IP_ADD_SOURCE_MEMBERSHIP
) {
2079 ssa
->sin
.sin_family
= AF_INET
;
2080 ssa
->sin
.sin_len
= sizeof(struct sockaddr_in
);
2081 ssa
->sin
.sin_addr
= mreqs
.imr_sourceaddr
;
2084 if (!IN_MULTICAST(ntohl(gsa
->sin
.sin_addr
.s_addr
)))
2087 ifp
= inp_lookup_mcast_ifp(inp
, &gsa
->sin
,
2088 mreqs
.imr_interface
);
2089 IGMP_PRINTF(("%s: imr_interface = %s, ifp = %p\n",
2090 __func__
, inet_ntoa(mreqs
.imr_interface
), ifp
));
2094 case MCAST_JOIN_GROUP
:
2095 case MCAST_JOIN_SOURCE_GROUP
:
2096 if (sopt
->sopt_name
== MCAST_JOIN_GROUP
) {
2097 error
= sooptcopyin(sopt
, &gsr
,
2098 sizeof(struct group_req
),
2099 sizeof(struct group_req
));
2100 } else if (sopt
->sopt_name
== MCAST_JOIN_SOURCE_GROUP
) {
2101 error
= sooptcopyin(sopt
, &gsr
,
2102 sizeof(struct group_source_req
),
2103 sizeof(struct group_source_req
));
2108 if (gsa
->sin
.sin_family
!= AF_INET
||
2109 gsa
->sin
.sin_len
!= sizeof(struct sockaddr_in
))
2113 * Overwrite the port field if present, as the sockaddr
2114 * being copied in may be matched with a binary comparison.
2116 gsa
->sin
.sin_port
= 0;
2117 if (sopt
->sopt_name
== MCAST_JOIN_SOURCE_GROUP
) {
2118 if (ssa
->sin
.sin_family
!= AF_INET
||
2119 ssa
->sin
.sin_len
!= sizeof(struct sockaddr_in
))
2121 ssa
->sin
.sin_port
= 0;
2124 if (!IN_MULTICAST(ntohl(gsa
->sin
.sin_addr
.s_addr
)))
2127 ifnet_head_lock_shared();
2128 if (gsr
.gsr_interface
== 0 ||
2129 (u_int
)if_index
< gsr
.gsr_interface
) {
2131 return (EADDRNOTAVAIL
);
2133 ifp
= ifindex2ifnet
[gsr
.gsr_interface
];
2139 IGMP_PRINTF(("%s: unknown sopt_name %d\n",
2140 __func__
, sopt
->sopt_name
));
2141 return (EOPNOTSUPP
);
2145 if (ifp
== NULL
|| (ifp
->if_flags
& IFF_MULTICAST
) == 0)
2146 return (EADDRNOTAVAIL
);
2148 imo
= inp_findmoptions(inp
);
2153 idx
= imo_match_group(imo
, ifp
, &gsa
->sa
);
2154 if (idx
== (size_t)-1) {
2157 inm
= imo
->imo_membership
[idx
];
2158 imf
= &imo
->imo_mfilters
[idx
];
2159 if (ssa
->ss
.ss_family
!= AF_UNSPEC
) {
2161 * MCAST_JOIN_SOURCE_GROUP on an exclusive membership
2162 * is an error. On an existing inclusive membership,
2163 * it just adds the source to the filter list.
2165 if (imf
->imf_st
[1] != MCAST_INCLUDE
) {
2167 goto out_imo_locked
;
2170 * Throw out duplicates.
2172 * XXX FIXME: This makes a naive assumption that
2173 * even if entries exist for *ssa in this imf,
2174 * they will be rejected as dupes, even if they
2175 * are not valid in the current mode (in-mode).
2177 * in_msource is transactioned just as for anything
2178 * else in SSM -- but note naive use of inm_graft()
2179 * below for allocating new filter entries.
2181 * This is only an issue if someone mixes the
2182 * full-state SSM API with the delta-based API,
2183 * which is discouraged in the relevant RFCs.
2185 lims
= imo_match_source(imo
, idx
, &ssa
->sa
);
2186 if (lims
!= NULL
/*&&
2187 lims->imsl_st[1] == MCAST_INCLUDE*/) {
2188 error
= EADDRNOTAVAIL
;
2189 goto out_imo_locked
;
2193 * MCAST_JOIN_GROUP on an existing exclusive
2194 * membership is an error; return EADDRINUSE
2195 * to preserve 4.4BSD API idempotence, and
2196 * avoid tedious detour to code below.
2197 * NOTE: This is bending RFC 3678 a bit.
2199 * On an existing inclusive membership, this is also
2200 * an error; if you want to change filter mode,
2201 * you must use the userland API setsourcefilter().
2202 * XXX We don't reject this for imf in UNDEFINED
2203 * state at t1, because allocation of a filter
2204 * is atomic with allocation of a membership.
2207 /* See comments above for EADDRINUSE */
2208 if (imf
->imf_st
[1] == MCAST_EXCLUDE
)
2210 goto out_imo_locked
;
2215 * Begin state merge transaction at socket layer.
2219 if (imo
->imo_num_memberships
== imo
->imo_max_memberships
) {
2220 error
= imo_grow(imo
, 0);
2222 goto out_imo_locked
;
2225 * Allocate the new slot upfront so we can deal with
2226 * grafting the new source filter in same code path
2227 * as for join-source on existing membership.
2229 idx
= imo
->imo_num_memberships
;
2230 imo
->imo_membership
[idx
] = NULL
;
2231 imo
->imo_num_memberships
++;
2232 VERIFY(imo
->imo_mfilters
!= NULL
);
2233 imf
= &imo
->imo_mfilters
[idx
];
2234 VERIFY(RB_EMPTY(&imf
->imf_sources
));
2238 * Graft new source into filter list for this inpcb's
2239 * membership of the group. The in_multi may not have
2240 * been allocated yet if this is a new membership, however,
2241 * the in_mfilter slot will be allocated and must be initialized.
2243 if (ssa
->ss
.ss_family
!= AF_UNSPEC
) {
2244 /* Membership starts in IN mode */
2246 IGMP_PRINTF(("%s: new join w/source\n", __func__
));
2247 imf_init(imf
, MCAST_UNDEFINED
, MCAST_INCLUDE
);
2249 IGMP_PRINTF(("%s: %s source\n", __func__
, "allow"));
2251 lims
= imf_graft(imf
, MCAST_INCLUDE
, &ssa
->sin
);
2253 IGMP_PRINTF(("%s: merge imf state failed\n",
2259 /* No address specified; Membership starts in EX mode */
2261 IGMP_PRINTF(("%s: new join w/o source\n", __func__
));
2262 imf_init(imf
, MCAST_UNDEFINED
, MCAST_EXCLUDE
);
2267 * Begin state merge transaction at IGMP layer.
2271 VERIFY(inm
== NULL
);
2272 error
= in_joingroup(ifp
, &gsa
->sin
.sin_addr
, imf
, &inm
);
2273 VERIFY(inm
!= NULL
|| error
!= 0);
2276 imo
->imo_membership
[idx
] = inm
; /* from in_joingroup() */
2278 IGMP_PRINTF(("%s: merge inm state\n", __func__
));
2280 error
= inm_merge(inm
, imf
);
2282 IGMP_PRINTF(("%s: failed to merge inm state\n",
2285 goto out_imf_rollback
;
2287 IGMP_PRINTF(("%s: doing igmp downcall\n", __func__
));
2288 error
= igmp_change_state(inm
);
2291 IGMP_PRINTF(("%s: failed igmp downcall\n",
2293 goto out_imf_rollback
;
2309 if (error
&& is_new
) {
2310 VERIFY(inm
== NULL
);
2311 imo
->imo_membership
[idx
] = NULL
;
2312 --imo
->imo_num_memberships
;
2317 IMO_REMREF(imo
); /* from inp_findmoptions() */
2322 * Leave an IPv4 multicast group on an inpcb, possibly with a source.
2324 * NB: sopt->sopt_val might point to the kernel address space. Refer to the
2325 * block comment on top of inp_join_group() for more information.
2328 inp_leave_group(struct inpcb
*inp
, struct sockopt
*sopt
)
2330 struct group_source_req gsr
;
2331 struct ip_mreq_source mreqs
;
2332 sockunion_t
*gsa
, *ssa
;
2334 struct in_mfilter
*imf
;
2335 struct ip_moptions
*imo
;
2336 struct in_msource
*ims
;
2337 struct in_multi
*inm
= NULL
;
2339 int error
, is_final
;
2340 unsigned int ifindex
= 0;
2346 memset(&gsr
, 0, sizeof(struct group_source_req
));
2347 gsa
= (sockunion_t
*)&gsr
.gsr_group
;
2348 gsa
->ss
.ss_family
= AF_UNSPEC
;
2349 ssa
= (sockunion_t
*)&gsr
.gsr_source
;
2350 ssa
->ss
.ss_family
= AF_UNSPEC
;
2352 switch (sopt
->sopt_name
) {
2353 case IP_DROP_MEMBERSHIP
:
2354 case IP_DROP_SOURCE_MEMBERSHIP
:
2355 if (sopt
->sopt_name
== IP_DROP_MEMBERSHIP
) {
2356 error
= sooptcopyin(sopt
, &mreqs
,
2357 sizeof(struct ip_mreq
),
2358 sizeof(struct ip_mreq
));
2360 * Swap interface and sourceaddr arguments,
2361 * as ip_mreq and ip_mreq_source are laid
2364 mreqs
.imr_interface
= mreqs
.imr_sourceaddr
;
2365 mreqs
.imr_sourceaddr
.s_addr
= INADDR_ANY
;
2366 } else if (sopt
->sopt_name
== IP_DROP_SOURCE_MEMBERSHIP
) {
2367 error
= sooptcopyin(sopt
, &mreqs
,
2368 sizeof(struct ip_mreq_source
),
2369 sizeof(struct ip_mreq_source
));
2374 gsa
->sin
.sin_family
= AF_INET
;
2375 gsa
->sin
.sin_len
= sizeof(struct sockaddr_in
);
2376 gsa
->sin
.sin_addr
= mreqs
.imr_multiaddr
;
2378 if (sopt
->sopt_name
== IP_DROP_SOURCE_MEMBERSHIP
) {
2379 ssa
->sin
.sin_family
= AF_INET
;
2380 ssa
->sin
.sin_len
= sizeof(struct sockaddr_in
);
2381 ssa
->sin
.sin_addr
= mreqs
.imr_sourceaddr
;
2384 * Attempt to look up hinted ifp from interface address.
2385 * Fallthrough with null ifp iff lookup fails, to
2386 * preserve 4.4BSD mcast API idempotence.
2387 * XXX NOTE WELL: The RFC 3678 API is preferred because
2388 * using an IPv4 address as a key is racy.
2390 if (!in_nullhost(mreqs
.imr_interface
))
2391 ifp
= ip_multicast_if(&mreqs
.imr_interface
, &ifindex
);
2393 IGMP_PRINTF(("%s: imr_interface = %s, ifp = %p\n",
2394 __func__
, inet_ntoa(mreqs
.imr_interface
), ifp
));
2398 case MCAST_LEAVE_GROUP
:
2399 case MCAST_LEAVE_SOURCE_GROUP
:
2400 if (sopt
->sopt_name
== MCAST_LEAVE_GROUP
) {
2401 error
= sooptcopyin(sopt
, &gsr
,
2402 sizeof(struct group_req
),
2403 sizeof(struct group_req
));
2404 } else if (sopt
->sopt_name
== MCAST_LEAVE_SOURCE_GROUP
) {
2405 error
= sooptcopyin(sopt
, &gsr
,
2406 sizeof(struct group_source_req
),
2407 sizeof(struct group_source_req
));
2412 if (gsa
->sin
.sin_family
!= AF_INET
||
2413 gsa
->sin
.sin_len
!= sizeof(struct sockaddr_in
))
2416 if (sopt
->sopt_name
== MCAST_LEAVE_SOURCE_GROUP
) {
2417 if (ssa
->sin
.sin_family
!= AF_INET
||
2418 ssa
->sin
.sin_len
!= sizeof(struct sockaddr_in
))
2422 ifnet_head_lock_shared();
2423 if (gsr
.gsr_interface
== 0 ||
2424 (u_int
)if_index
< gsr
.gsr_interface
) {
2426 return (EADDRNOTAVAIL
);
2429 ifp
= ifindex2ifnet
[gsr
.gsr_interface
];
2434 IGMP_PRINTF(("%s: unknown sopt_name %d\n",
2435 __func__
, sopt
->sopt_name
));
2436 return (EOPNOTSUPP
);
2440 if (!IN_MULTICAST(ntohl(gsa
->sin
.sin_addr
.s_addr
)))
2444 * Find the membership in the membership array.
2446 imo
= inp_findmoptions(inp
);
2451 idx
= imo_match_group(imo
, ifp
, &gsa
->sa
);
2452 if (idx
== (size_t)-1) {
2453 error
= EADDRNOTAVAIL
;
2456 inm
= imo
->imo_membership
[idx
];
2457 imf
= &imo
->imo_mfilters
[idx
];
2459 if (ssa
->ss
.ss_family
!= AF_UNSPEC
) {
2460 IGMP_PRINTF(("%s: opt=%d is_final=0\n", __func__
,
2466 * Begin state merge transaction at socket layer.
2470 * If we were instructed only to leave a given source, do so.
2471 * MCAST_LEAVE_SOURCE_GROUP is only valid for inclusive memberships.
2476 if (imf
->imf_st
[0] == MCAST_EXCLUDE
) {
2477 error
= EADDRNOTAVAIL
;
2480 ims
= imo_match_source(imo
, idx
, &ssa
->sa
);
2482 IGMP_PRINTF(("%s: source %s %spresent\n", __func__
,
2483 inet_ntoa(ssa
->sin
.sin_addr
), "not "));
2484 error
= EADDRNOTAVAIL
;
2487 IGMP_PRINTF(("%s: %s source\n", __func__
, "block"));
2488 error
= imf_prune(imf
, &ssa
->sin
);
2490 IGMP_PRINTF(("%s: merge imf state failed\n",
2497 * Begin state merge transaction at IGMP layer.
2502 * Give up the multicast address record to which
2503 * the membership points. Reference held in imo
2504 * will be released below.
2506 (void) in_leavegroup(inm
, imf
);
2508 IGMP_PRINTF(("%s: merge inm state\n", __func__
));
2510 error
= inm_merge(inm
, imf
);
2512 IGMP_PRINTF(("%s: failed to merge inm state\n",
2515 goto out_imf_rollback
;
2518 IGMP_PRINTF(("%s: doing igmp downcall\n", __func__
));
2519 error
= igmp_change_state(inm
);
2521 IGMP_PRINTF(("%s: failed igmp downcall\n", __func__
));
2535 /* Remove the gap in the membership and filter array. */
2536 VERIFY(inm
== imo
->imo_membership
[idx
]);
2537 imo
->imo_membership
[idx
] = NULL
;
2539 for (++idx
; idx
< imo
->imo_num_memberships
; ++idx
) {
2540 imo
->imo_membership
[idx
-1] = imo
->imo_membership
[idx
];
2541 imo
->imo_mfilters
[idx
-1] = imo
->imo_mfilters
[idx
];
2543 imo
->imo_num_memberships
--;
2548 IMO_REMREF(imo
); /* from inp_findmoptions() */
2553 * Select the interface for transmitting IPv4 multicast datagrams.
2555 * Either an instance of struct in_addr or an instance of struct ip_mreqn
2556 * may be passed to this socket option. An address of INADDR_ANY or an
2557 * interface index of 0 is used to remove a previous selection.
2558 * When no interface is selected, one is chosen for every send.
2561 inp_set_multicast_if(struct inpcb
*inp
, struct sockopt
*sopt
)
2563 struct in_addr addr
;
2564 struct ip_mreqn mreqn
;
2566 struct ip_moptions
*imo
;
2568 unsigned int ifindex
= 0;
2570 if (sopt
->sopt_valsize
== sizeof(struct ip_mreqn
)) {
2572 * An interface index was specified using the
2573 * Linux-derived ip_mreqn structure.
2575 error
= sooptcopyin(sopt
, &mreqn
, sizeof(struct ip_mreqn
),
2576 sizeof(struct ip_mreqn
));
2580 ifnet_head_lock_shared();
2581 if (mreqn
.imr_ifindex
< 0 || if_index
< mreqn
.imr_ifindex
) {
2586 if (mreqn
.imr_ifindex
== 0) {
2589 ifp
= ifindex2ifnet
[mreqn
.imr_ifindex
];
2592 return (EADDRNOTAVAIL
);
2598 * An interface was specified by IPv4 address.
2599 * This is the traditional BSD usage.
2601 error
= sooptcopyin(sopt
, &addr
, sizeof(struct in_addr
),
2602 sizeof(struct in_addr
));
2605 if (in_nullhost(addr
)) {
2608 ifp
= ip_multicast_if(&addr
, &ifindex
);
2610 IGMP_PRINTF(("%s: can't find ifp for addr=%s\n",
2611 __func__
, inet_ntoa(addr
)));
2612 return (EADDRNOTAVAIL
);
2616 IGMP_PRINTF(("%s: ifp = %p, addr = %s\n", __func__
, ifp
,
2621 /* Reject interfaces which do not support multicast. */
2622 if (ifp
!= NULL
&& (ifp
->if_flags
& IFF_MULTICAST
) == 0)
2623 return (EOPNOTSUPP
);
2625 imo
= inp_findmoptions(inp
);
2630 imo
->imo_multicast_ifp
= ifp
;
2632 imo
->imo_multicast_addr
= addr
;
2634 imo
->imo_multicast_addr
.s_addr
= INADDR_ANY
;
2636 IMO_REMREF(imo
); /* from inp_findmoptions() */
2642 * Atomically set source filters on a socket for an IPv4 multicast group.
2645 inp_set_source_filters(struct inpcb
*inp
, struct sockopt
*sopt
)
2647 struct __msfilterreq64 msfr
, msfr64
;
2648 struct __msfilterreq32 msfr32
;
2651 struct in_mfilter
*imf
;
2652 struct ip_moptions
*imo
;
2653 struct in_multi
*inm
;
2656 user_addr_t tmp_ptr
;
2658 if (IS_64BIT_PROCESS(current_proc())) {
2659 error
= sooptcopyin(sopt
, &msfr64
,
2660 sizeof(struct __msfilterreq64
),
2661 sizeof(struct __msfilterreq64
));
2664 /* we never use msfr.msfr_srcs; */
2665 memcpy(&msfr
, &msfr64
, sizeof(msfr
));
2667 error
= sooptcopyin(sopt
, &msfr32
,
2668 sizeof(struct __msfilterreq32
),
2669 sizeof(struct __msfilterreq32
));
2672 /* we never use msfr.msfr_srcs; */
2673 memcpy(&msfr
, &msfr32
, sizeof(msfr
));
2676 if (msfr
.msfr_nsrcs
> in_mcast_maxsocksrc
)
2679 if ((msfr
.msfr_fmode
!= MCAST_EXCLUDE
&&
2680 msfr
.msfr_fmode
!= MCAST_INCLUDE
))
2683 if (msfr
.msfr_group
.ss_family
!= AF_INET
||
2684 msfr
.msfr_group
.ss_len
!= sizeof(struct sockaddr_in
))
2687 gsa
= (sockunion_t
*)&msfr
.msfr_group
;
2688 if (!IN_MULTICAST(ntohl(gsa
->sin
.sin_addr
.s_addr
)))
2691 gsa
->sin
.sin_port
= 0; /* ignore port */
2693 ifnet_head_lock_shared();
2694 if (msfr
.msfr_ifindex
== 0 || (u_int
)if_index
< msfr
.msfr_ifindex
) {
2696 return (EADDRNOTAVAIL
);
2699 ifp
= ifindex2ifnet
[msfr
.msfr_ifindex
];
2702 return (EADDRNOTAVAIL
);
2705 * Check if this socket is a member of this group.
2707 imo
= inp_findmoptions(inp
);
2712 idx
= imo_match_group(imo
, ifp
, &gsa
->sa
);
2713 if (idx
== (size_t)-1 || imo
->imo_mfilters
== NULL
) {
2714 error
= EADDRNOTAVAIL
;
2715 goto out_imo_locked
;
2717 inm
= imo
->imo_membership
[idx
];
2718 imf
= &imo
->imo_mfilters
[idx
];
2721 * Begin state merge transaction at socket layer.
2724 imf
->imf_st
[1] = msfr
.msfr_fmode
;
2727 * Apply any new source filters, if present.
2728 * Make a copy of the user-space source vector so
2729 * that we may copy them with a single copyin. This
2730 * allows us to deal with page faults up-front.
2732 if (msfr
.msfr_nsrcs
> 0) {
2733 struct in_msource
*lims
;
2734 struct sockaddr_in
*psin
;
2735 struct sockaddr_storage
*kss
, *pkss
;
2738 if (IS_64BIT_PROCESS(current_proc()))
2739 tmp_ptr
= msfr64
.msfr_srcs
;
2741 tmp_ptr
= CAST_USER_ADDR_T(msfr32
.msfr_srcs
);
2743 IGMP_PRINTF(("%s: loading %lu source list entries\n",
2744 __func__
, (unsigned long)msfr
.msfr_nsrcs
));
2745 kss
= _MALLOC(sizeof(struct sockaddr_storage
) * msfr
.msfr_nsrcs
,
2749 goto out_imo_locked
;
2751 error
= copyin(tmp_ptr
, kss
,
2752 sizeof(struct sockaddr_storage
) * msfr
.msfr_nsrcs
);
2755 goto out_imo_locked
;
2759 * Mark all source filters as UNDEFINED at t1.
2760 * Restore new group filter mode, as imf_leave()
2761 * will set it to INCLUDE.
2764 imf
->imf_st
[1] = msfr
.msfr_fmode
;
2767 * Update socket layer filters at t1, lazy-allocating
2768 * new entries. This saves a bunch of memory at the
2769 * cost of one RB_FIND() per source entry; duplicate
2770 * entries in the msfr_nsrcs vector are ignored.
2771 * If we encounter an error, rollback transaction.
2773 * XXX This too could be replaced with a set-symmetric
2774 * difference like loop to avoid walking from root
2775 * every time, as the key space is common.
2777 for (i
= 0, pkss
= kss
; (u_int
)i
< msfr
.msfr_nsrcs
;
2779 psin
= (struct sockaddr_in
*)pkss
;
2780 if (psin
->sin_family
!= AF_INET
) {
2781 error
= EAFNOSUPPORT
;
2784 if (psin
->sin_len
!= sizeof(struct sockaddr_in
)) {
2788 error
= imf_get_source(imf
, psin
, &lims
);
2791 lims
->imsl_st
[1] = imf
->imf_st
[1];
2797 goto out_imf_rollback
;
2800 * Begin state merge transaction at IGMP layer.
2803 IGMP_PRINTF(("%s: merge inm state\n", __func__
));
2804 error
= inm_merge(inm
, imf
);
2806 IGMP_PRINTF(("%s: failed to merge inm state\n", __func__
));
2808 goto out_imf_rollback
;
2811 IGMP_PRINTF(("%s: doing igmp downcall\n", __func__
));
2812 error
= igmp_change_state(inm
);
2816 IGMP_PRINTF(("%s: failed igmp downcall\n", __func__
));
2829 IMO_REMREF(imo
); /* from inp_findmoptions() */
2835 * Set the IP multicast options in response to user setsockopt().
2837 * Many of the socket options handled in this function duplicate the
2838 * functionality of socket options in the regular unicast API. However,
2839 * it is not possible to merge the duplicate code, because the idempotence
2840 * of the IPv4 multicast part of the BSD Sockets API must be preserved;
2841 * the effects of these options must be treated as separate and distinct.
2843 * FUTURE: The IP_MULTICAST_VIF option may be eliminated if MROUTING
2844 * is refactored to no longer use vifs.
2847 inp_setmoptions(struct inpcb
*inp
, struct sockopt
*sopt
)
2849 struct ip_moptions
*imo
;
2851 unsigned int ifindex
;
2857 * If socket is neither of type SOCK_RAW or SOCK_DGRAM,
2858 * or is a divert socket, reject it.
2860 if (inp
->inp_socket
->so_proto
->pr_protocol
== IPPROTO_DIVERT
||
2861 (inp
->inp_socket
->so_proto
->pr_type
!= SOCK_RAW
&&
2862 inp
->inp_socket
->so_proto
->pr_type
!= SOCK_DGRAM
))
2863 return (EOPNOTSUPP
);
2865 switch (sopt
->sopt_name
) {
2867 case IP_MULTICAST_VIF
: {
2870 * Select a multicast VIF for transmission.
2871 * Only useful if multicast forwarding is active.
2873 if (legal_vif_num
== NULL
) {
2877 error
= sooptcopyin(sopt
, &vifi
, sizeof(int), sizeof(int));
2880 if (!legal_vif_num(vifi
) && (vifi
!= -1)) {
2884 imo
= inp_findmoptions(inp
);
2890 imo
->imo_multicast_vif
= vifi
;
2892 IMO_REMREF(imo
); /* from inp_findmoptions() */
2896 case IP_MULTICAST_IF
:
2897 error
= inp_set_multicast_if(inp
, sopt
);
2900 case IP_MULTICAST_IFINDEX
:
2902 * Select the interface for outgoing multicast packets.
2904 error
= sooptcopyin(sopt
, &ifindex
, sizeof (ifindex
),
2909 imo
= inp_findmoptions(inp
);
2915 * Index 0 is used to remove a previous selection.
2916 * When no interface is selected, a default one is
2917 * chosen every time a multicast packet is sent.
2921 imo
->imo_multicast_ifp
= NULL
;
2923 IMO_REMREF(imo
); /* from inp_findmoptions() */
2927 ifnet_head_lock_shared();
2928 /* Don't need to check is ifindex is < 0 since it's unsigned */
2929 if ((unsigned int)if_index
< ifindex
) {
2931 IMO_REMREF(imo
); /* from inp_findmoptions() */
2932 error
= ENXIO
; /* per IPV6_MULTICAST_IF */
2935 ifp
= ifindex2ifnet
[ifindex
];
2938 /* If it's detached or isn't a multicast interface, bail out */
2939 if (ifp
== NULL
|| !(ifp
->if_flags
& IFF_MULTICAST
)) {
2940 IMO_REMREF(imo
); /* from inp_findmoptions() */
2941 error
= EADDRNOTAVAIL
;
2945 imo
->imo_multicast_ifp
= ifp
;
2947 * Clear out any remnants of past IP_MULTICAST_IF. The addr
2948 * isn't really used anywhere in the kernel; we could have
2949 * iterated thru the addresses of the interface and pick one
2950 * here, but that is redundant since ip_getmoptions() already
2951 * takes care of that for INADDR_ANY.
2953 imo
->imo_multicast_addr
.s_addr
= INADDR_ANY
;
2955 IMO_REMREF(imo
); /* from inp_findmoptions() */
2958 case IP_MULTICAST_TTL
: {
2962 * Set the IP time-to-live for outgoing multicast packets.
2963 * The original multicast API required a char argument,
2964 * which is inconsistent with the rest of the socket API.
2965 * We allow either a char or an int.
2967 if (sopt
->sopt_valsize
== sizeof(u_char
)) {
2968 error
= sooptcopyin(sopt
, &ttl
, sizeof(u_char
),
2975 error
= sooptcopyin(sopt
, &ittl
, sizeof(u_int
),
2985 imo
= inp_findmoptions(inp
);
2991 imo
->imo_multicast_ttl
= ttl
;
2993 IMO_REMREF(imo
); /* from inp_findmoptions() */
2997 case IP_MULTICAST_LOOP
: {
3001 * Set the loopback flag for outgoing multicast packets.
3002 * Must be zero or one. The original multicast API required a
3003 * char argument, which is inconsistent with the rest
3004 * of the socket API. We allow either a char or an int.
3006 if (sopt
->sopt_valsize
== sizeof(u_char
)) {
3007 error
= sooptcopyin(sopt
, &loop
, sizeof(u_char
),
3014 error
= sooptcopyin(sopt
, &iloop
, sizeof(u_int
),
3018 loop
= (u_char
)iloop
;
3020 imo
= inp_findmoptions(inp
);
3026 imo
->imo_multicast_loop
= !!loop
;
3028 IMO_REMREF(imo
); /* from inp_findmoptions() */
3032 case IP_ADD_MEMBERSHIP
:
3033 case IP_ADD_SOURCE_MEMBERSHIP
:
3034 case MCAST_JOIN_GROUP
:
3035 case MCAST_JOIN_SOURCE_GROUP
:
3036 error
= inp_join_group(inp
, sopt
);
3039 case IP_DROP_MEMBERSHIP
:
3040 case IP_DROP_SOURCE_MEMBERSHIP
:
3041 case MCAST_LEAVE_GROUP
:
3042 case MCAST_LEAVE_SOURCE_GROUP
:
3043 error
= inp_leave_group(inp
, sopt
);
3046 case IP_BLOCK_SOURCE
:
3047 case IP_UNBLOCK_SOURCE
:
3048 case MCAST_BLOCK_SOURCE
:
3049 case MCAST_UNBLOCK_SOURCE
:
3050 error
= inp_block_unblock_source(inp
, sopt
);
3054 error
= inp_set_source_filters(inp
, sopt
);
3066 * Expose IGMP's multicast filter mode and source list(s) to userland,
3067 * keyed by (ifindex, group).
3068 * The filter mode is written out as a uint32_t, followed by
3069 * 0..n of struct in_addr.
3070 * For use by ifmcstat(8).
3073 sysctl_ip_mcast_filters SYSCTL_HANDLER_ARGS
3075 #pragma unused(oidp)
3077 struct in_addr src
, group
;
3079 struct in_multi
*inm
;
3080 struct in_multistep step
;
3081 struct ip_msource
*ims
;
3085 uint32_t fmode
, ifindex
;
3088 namelen
= (u_int
)arg2
;
3090 if (req
->newptr
!= USER_ADDR_NULL
)
3097 ifnet_head_lock_shared();
3098 if (ifindex
<= 0 || ifindex
> (u_int
)if_index
) {
3099 IGMP_PRINTF(("%s: ifindex %u out of range\n",
3100 __func__
, ifindex
));
3105 group
.s_addr
= name
[1];
3106 if (!IN_MULTICAST(ntohl(group
.s_addr
))) {
3107 IGMP_PRINTF(("%s: group %s is not multicast\n",
3108 __func__
, inet_ntoa(group
)));
3113 ifp
= ifindex2ifnet
[ifindex
];
3116 IGMP_PRINTF(("%s: no ifp for ifindex %u\n", __func__
, ifindex
));
3120 in_multihead_lock_shared();
3121 IN_FIRST_MULTI(step
, inm
);
3122 while (inm
!= NULL
) {
3124 if (inm
->inm_ifp
!= ifp
)
3127 if (!in_hosteq(inm
->inm_addr
, group
))
3130 fmode
= inm
->inm_st
[1].iss_fmode
;
3131 retval
= SYSCTL_OUT(req
, &fmode
, sizeof(uint32_t));
3136 RB_FOREACH(ims
, ip_msource_tree
, &inm
->inm_srcs
) {
3139 ina
.s_addr
= htonl(ims
->ims_haddr
);
3140 IGMP_PRINTF(("%s: visit node %s\n", __func__
,
3144 * Only copy-out sources which are in-mode.
3146 if (fmode
!= ims_get_mode(inm
, ims
, 1)) {
3147 IGMP_PRINTF(("%s: skip non-in-mode\n",
3149 continue; /* process next source */
3151 src
.s_addr
= htonl(ims
->ims_haddr
);
3152 retval
= SYSCTL_OUT(req
, &src
, sizeof(struct in_addr
));
3154 break; /* process next inm */
3158 IN_NEXT_MULTI(step
, inm
);
3160 in_multihead_lock_done();
3167 * The whole multicast option thing needs to be re-thought.
3168 * Several of these options are equally applicable to non-multicast
3169 * transmission, and one (IP_MULTICAST_TTL) totally duplicates a
3170 * standard option (IP_TTL).
3173 * following RFC1724 section 3.3, 0.0.0.0/8 is interpreted as interface index.
3175 static struct ifnet
*
3176 ip_multicast_if(struct in_addr
*a
, unsigned int *ifindexp
)
3178 unsigned int ifindex
;
3181 if (ifindexp
!= NULL
)
3183 if (ntohl(a
->s_addr
) >> 24 == 0) {
3184 ifindex
= ntohl(a
->s_addr
) & 0xffffff;
3185 ifnet_head_lock_shared();
3186 /* Don't need to check is ifindex is < 0 since it's unsigned */
3187 if ((unsigned int)if_index
< ifindex
) {
3191 ifp
= ifindex2ifnet
[ifindex
];
3193 if (ifp
!= NULL
&& ifindexp
!= NULL
)
3194 *ifindexp
= ifindex
;
3196 INADDR_TO_IFP(*a
, ifp
);
3204 PE_parse_boot_argn("ifa_debug", &inm_debug
, sizeof (inm_debug
));
3206 /* Setup lock group and attribute for in_multihead */
3207 in_multihead_lock_grp_attr
= lck_grp_attr_alloc_init();
3208 in_multihead_lock_grp
= lck_grp_alloc_init("in_multihead",
3209 in_multihead_lock_grp_attr
);
3210 in_multihead_lock_attr
= lck_attr_alloc_init();
3211 lck_rw_init(&in_multihead_lock
, in_multihead_lock_grp
,
3212 in_multihead_lock_attr
);
3214 lck_mtx_init(&inm_trash_lock
, in_multihead_lock_grp
,
3215 in_multihead_lock_attr
);
3216 TAILQ_INIT(&inm_trash_head
);
3218 inm_size
= (inm_debug
== 0) ? sizeof (struct in_multi
) :
3219 sizeof (struct in_multi_dbg
);
3220 inm_zone
= zinit(inm_size
, INM_ZONE_MAX
* inm_size
,
3222 if (inm_zone
== NULL
) {
3223 panic("%s: failed allocating %s", __func__
, INM_ZONE_NAME
);
3226 zone_change(inm_zone
, Z_EXPAND
, TRUE
);
3228 ipms_size
= sizeof (struct ip_msource
);
3229 ipms_zone
= zinit(ipms_size
, IPMS_ZONE_MAX
* ipms_size
,
3231 if (ipms_zone
== NULL
) {
3232 panic("%s: failed allocating %s", __func__
, IPMS_ZONE_NAME
);
3235 zone_change(ipms_zone
, Z_EXPAND
, TRUE
);
3237 inms_size
= sizeof (struct in_msource
);
3238 inms_zone
= zinit(inms_size
, INMS_ZONE_MAX
* inms_size
,
3240 if (inms_zone
== NULL
) {
3241 panic("%s: failed allocating %s", __func__
, INMS_ZONE_NAME
);
3244 zone_change(inms_zone
, Z_EXPAND
, TRUE
);
3247 static struct in_multi
*
3248 in_multi_alloc(int how
)
3250 struct in_multi
*inm
;
3252 inm
= (how
== M_WAITOK
) ? zalloc(inm_zone
) : zalloc_noblock(inm_zone
);
3254 bzero(inm
, inm_size
);
3255 lck_mtx_init(&inm
->inm_lock
, in_multihead_lock_grp
,
3256 in_multihead_lock_attr
);
3257 inm
->inm_debug
|= IFD_ALLOC
;
3258 if (inm_debug
!= 0) {
3259 inm
->inm_debug
|= IFD_DEBUG
;
3260 inm
->inm_trace
= inm_trace
;
3267 in_multi_free(struct in_multi
*inm
)
3270 if (inm
->inm_debug
& IFD_ATTACHED
) {
3271 panic("%s: attached inm=%p is being freed", __func__
, inm
);
3273 } else if (inm
->inm_ifma
!= NULL
) {
3274 panic("%s: ifma not NULL for inm=%p", __func__
, inm
);
3276 } else if (!(inm
->inm_debug
& IFD_ALLOC
)) {
3277 panic("%s: inm %p cannot be freed", __func__
, inm
);
3279 } else if (inm
->inm_refcount
!= 0) {
3280 panic("%s: non-zero refcount inm=%p", __func__
, inm
);
3282 } else if (inm
->inm_reqcnt
!= 0) {
3283 panic("%s: non-zero reqcnt inm=%p", __func__
, inm
);
3287 /* Free any pending IGMPv3 state-change records */
3288 IF_DRAIN(&inm
->inm_scq
);
3290 inm
->inm_debug
&= ~IFD_ALLOC
;
3291 if ((inm
->inm_debug
& (IFD_DEBUG
| IFD_TRASHED
)) ==
3292 (IFD_DEBUG
| IFD_TRASHED
)) {
3293 lck_mtx_lock(&inm_trash_lock
);
3294 TAILQ_REMOVE(&inm_trash_head
, (struct in_multi_dbg
*)inm
,
3296 lck_mtx_unlock(&inm_trash_lock
);
3297 inm
->inm_debug
&= ~IFD_TRASHED
;
3301 lck_mtx_destroy(&inm
->inm_lock
, in_multihead_lock_grp
);
3302 zfree(inm_zone
, inm
);
3306 in_multi_attach(struct in_multi
*inm
)
3308 in_multihead_lock_assert(LCK_RW_ASSERT_EXCLUSIVE
);
3309 INM_LOCK_ASSERT_HELD(inm
);
3311 if (inm
->inm_debug
& IFD_ATTACHED
) {
3312 panic("%s: Attempt to attach an already attached inm=%p",
3315 } else if (inm
->inm_debug
& IFD_TRASHED
) {
3316 panic("%s: Attempt to reattach a detached inm=%p",
3322 VERIFY(inm
->inm_reqcnt
== 1);
3323 INM_ADDREF_LOCKED(inm
);
3324 inm
->inm_debug
|= IFD_ATTACHED
;
3326 * Reattach case: If debugging is enabled, take it
3327 * out of the trash list and clear IFD_TRASHED.
3329 if ((inm
->inm_debug
& (IFD_DEBUG
| IFD_TRASHED
)) ==
3330 (IFD_DEBUG
| IFD_TRASHED
)) {
3331 /* Become a regular mutex, just in case */
3332 INM_CONVERT_LOCK(inm
);
3333 lck_mtx_lock(&inm_trash_lock
);
3334 TAILQ_REMOVE(&inm_trash_head
, (struct in_multi_dbg
*)inm
,
3336 lck_mtx_unlock(&inm_trash_lock
);
3337 inm
->inm_debug
&= ~IFD_TRASHED
;
3340 LIST_INSERT_HEAD(&in_multihead
, inm
, inm_link
);
3344 in_multi_detach(struct in_multi
*inm
)
3346 in_multihead_lock_assert(LCK_RW_ASSERT_EXCLUSIVE
);
3347 INM_LOCK_ASSERT_HELD(inm
);
3349 if (inm
->inm_reqcnt
== 0) {
3350 panic("%s: inm=%p negative reqcnt", __func__
, inm
);
3355 if (inm
->inm_reqcnt
> 0)
3358 if (!(inm
->inm_debug
& IFD_ATTACHED
)) {
3359 panic("%s: Attempt to detach an unattached record inm=%p",
3362 } else if (inm
->inm_debug
& IFD_TRASHED
) {
3363 panic("%s: inm %p is already in trash list", __func__
, inm
);
3368 * NOTE: Caller calls IFMA_REMREF
3370 inm
->inm_debug
&= ~IFD_ATTACHED
;
3371 LIST_REMOVE(inm
, inm_link
);
3373 if (inm
->inm_debug
& IFD_DEBUG
) {
3374 /* Become a regular mutex, just in case */
3375 INM_CONVERT_LOCK(inm
);
3376 lck_mtx_lock(&inm_trash_lock
);
3377 TAILQ_INSERT_TAIL(&inm_trash_head
,
3378 (struct in_multi_dbg
*)inm
, inm_trash_link
);
3379 lck_mtx_unlock(&inm_trash_lock
);
3380 inm
->inm_debug
|= IFD_TRASHED
;
3387 inm_addref(struct in_multi
*inm
, int locked
)
3392 INM_LOCK_ASSERT_HELD(inm
);
3394 if (++inm
->inm_refcount
== 0) {
3395 panic("%s: inm=%p wraparound refcnt", __func__
, inm
);
3397 } else if (inm
->inm_trace
!= NULL
) {
3398 (*inm
->inm_trace
)(inm
, TRUE
);
3405 inm_remref(struct in_multi
*inm
, int locked
)
3407 struct ifmultiaddr
*ifma
;
3408 struct igmp_ifinfo
*igi
;
3413 INM_LOCK_ASSERT_HELD(inm
);
3415 if (inm
->inm_refcount
== 0 || (inm
->inm_refcount
== 1 && locked
)) {
3416 panic("%s: inm=%p negative/missing refcnt", __func__
, inm
);
3418 } else if (inm
->inm_trace
!= NULL
) {
3419 (*inm
->inm_trace
)(inm
, FALSE
);
3422 --inm
->inm_refcount
;
3423 if (inm
->inm_refcount
> 0) {
3430 * Synchronization with in_getmulti(). In the event the inm has been
3431 * detached, the underlying ifma would still be in the if_multiaddrs
3432 * list, and thus can be looked up via if_addmulti(). At that point,
3433 * the only way to find this inm is via ifma_protospec. To avoid
3434 * race conditions between the last inm_remref() of that inm and its
3435 * use via ifma_protospec, in_multihead lock is used for serialization.
3436 * In order to avoid violating the lock order, we must drop inm_lock
3437 * before acquiring in_multihead lock. To prevent the inm from being
3438 * freed prematurely, we hold an extra reference.
3440 ++inm
->inm_refcount
;
3442 in_multihead_lock_shared();
3444 --inm
->inm_refcount
;
3445 if (inm
->inm_refcount
> 0) {
3446 /* We've lost the race, so abort since inm is still in use */
3448 in_multihead_lock_done();
3449 /* If it was locked, return it as such */
3455 ifma
= inm
->inm_ifma
;
3456 inm
->inm_ifma
= NULL
;
3457 inm
->inm_ifp
= NULL
;
3459 inm
->inm_igi
= NULL
;
3461 IFMA_LOCK_SPIN(ifma
);
3462 ifma
->ifma_protospec
= NULL
;
3464 in_multihead_lock_done();
3467 if_delmulti_ifma(ifma
);
3468 /* Release reference held to the underlying ifmultiaddr */
3476 inm_trace(struct in_multi
*inm
, int refhold
)
3478 struct in_multi_dbg
*inm_dbg
= (struct in_multi_dbg
*)inm
;
3483 if (!(inm
->inm_debug
& IFD_DEBUG
)) {
3484 panic("%s: inm %p has no debug structure", __func__
, inm
);
3488 cnt
= &inm_dbg
->inm_refhold_cnt
;
3489 tr
= inm_dbg
->inm_refhold
;
3491 cnt
= &inm_dbg
->inm_refrele_cnt
;
3492 tr
= inm_dbg
->inm_refrele
;
3495 idx
= atomic_add_16_ov(cnt
, 1) % INM_TRACE_HIST_SIZE
;
3496 ctrace_record(&tr
[idx
]);
3500 in_multihead_lock_exclusive(void)
3502 lck_rw_lock_exclusive(&in_multihead_lock
);
3506 in_multihead_lock_shared(void)
3508 lck_rw_lock_shared(&in_multihead_lock
);
3512 in_multihead_lock_assert(int what
)
3514 lck_rw_assert(&in_multihead_lock
, what
);
3518 in_multihead_lock_done(void)
3520 lck_rw_done(&in_multihead_lock
);
3523 static struct ip_msource
*
3526 struct ip_msource
*ims
;
3528 ims
= (how
== M_WAITOK
) ? zalloc(ipms_zone
) : zalloc_noblock(ipms_zone
);
3530 bzero(ims
, ipms_size
);
3536 ipms_free(struct ip_msource
*ims
)
3538 zfree(ipms_zone
, ims
);
3541 static struct in_msource
*
3544 struct in_msource
*inms
;
3546 inms
= (how
== M_WAITOK
) ? zalloc(inms_zone
) :
3547 zalloc_noblock(inms_zone
);
3549 bzero(inms
, inms_size
);
3555 inms_free(struct in_msource
*inms
)
3557 zfree(inms_zone
, inms
);
3562 static const char *inm_modestrs
[] = { "un\n", "in", "ex" };
3565 inm_mode_str(const int mode
)
3567 if (mode
>= MCAST_UNDEFINED
&& mode
<= MCAST_EXCLUDE
)
3568 return (inm_modestrs
[mode
]);
3572 static const char *inm_statestrs
[] = {
3580 "sg-query-pending\n",
3585 inm_state_str(const int state
)
3587 if (state
>= IGMP_NOT_MEMBER
&& state
<= IGMP_LEAVING_MEMBER
)
3588 return (inm_statestrs
[state
]);
3593 * Dump an in_multi structure to the console.
3596 inm_print(const struct in_multi
*inm
)
3600 INM_LOCK_ASSERT_HELD(INM_CAST_TO_NONCONST(inm
));
3602 if (igmp_debug
== 0)
3605 printf("%s: --- begin inm %p ---\n", __func__
, inm
);
3606 printf("addr %s ifp %p(%s%d) ifma %p\n",
3607 inet_ntoa(inm
->inm_addr
),
3609 inm
->inm_ifp
->if_name
,
3610 inm
->inm_ifp
->if_unit
,
3612 printf("timer %u state %s refcount %u scq.len %u\n",
3614 inm_state_str(inm
->inm_state
),
3616 inm
->inm_scq
.ifq_len
);
3617 printf("igi %p nsrc %lu sctimer %u scrv %u\n",
3622 for (t
= 0; t
< 2; t
++) {
3623 printf("t%d: fmode %s asm %u ex %u in %u rec %u\n", t
,
3624 inm_mode_str(inm
->inm_st
[t
].iss_fmode
),
3625 inm
->inm_st
[t
].iss_asm
,
3626 inm
->inm_st
[t
].iss_ex
,
3627 inm
->inm_st
[t
].iss_in
,
3628 inm
->inm_st
[t
].iss_rec
);
3630 printf("%s: --- end inm %p ---\n", __func__
, inm
);
3636 inm_print(__unused
const struct in_multi
*inm
)