2 * Copyright (c) 2010-2012 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
= (struct sockaddr_in
*)(uintptr_t)(size_t)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
= (sockunion_t
*)(uintptr_t)(size_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 inpcb
*from_inp
, struct inpcb
*to_inp
)
454 struct ip_moptions
*from
;
455 struct ip_moptions
*to
;
457 from
= inp_findmoptions(from_inp
);
461 to
= inp_findmoptions(to_inp
);
470 to
->imo_multicast_ifp
= from
->imo_multicast_ifp
;
471 to
->imo_multicast_vif
= from
->imo_multicast_vif
;
472 to
->imo_multicast_ttl
= from
->imo_multicast_ttl
;
473 to
->imo_multicast_loop
= from
->imo_multicast_loop
;
476 * We're cloning, so drop any existing memberships and source
477 * filters on the destination ip_moptions.
479 for (i
= 0; i
< to
->imo_num_memberships
; ++i
) {
480 struct in_mfilter
*imf
;
482 imf
= to
->imo_mfilters
? &to
->imo_mfilters
[i
] : NULL
;
486 (void) in_leavegroup(to
->imo_membership
[i
], imf
);
491 INM_REMREF(to
->imo_membership
[i
]);
492 to
->imo_membership
[i
] = NULL
;
494 to
->imo_num_memberships
= 0;
496 VERIFY(to
->imo_max_memberships
!= 0 && from
->imo_max_memberships
!= 0);
497 if (to
->imo_max_memberships
< from
->imo_max_memberships
) {
499 * Ensure source and destination ip_moptions memberships
500 * and source filters arrays are at least equal in size.
502 err
= imo_grow(to
, from
->imo_max_memberships
);
506 VERIFY(to
->imo_max_memberships
>= from
->imo_max_memberships
);
509 * Source filtering doesn't apply to OpenTransport socket,
510 * so simply hold additional reference count per membership.
512 for (i
= 0; i
< from
->imo_num_memberships
; i
++) {
513 to
->imo_membership
[i
] =
514 in_addmulti(&from
->imo_membership
[i
]->inm_addr
,
515 from
->imo_membership
[i
]->inm_ifp
);
516 if (to
->imo_membership
[i
] == NULL
)
518 to
->imo_num_memberships
++;
520 VERIFY(to
->imo_num_memberships
== from
->imo_num_memberships
);
532 * Find and return a reference to an in_multi record for (ifp, group),
533 * and bump its reference count.
534 * If one does not exist, try to allocate it, and update link-layer multicast
535 * filters on ifp to listen for group.
536 * Return 0 if successful, otherwise return an appropriate error code.
539 in_getmulti(struct ifnet
*ifp
, const struct in_addr
*group
,
540 struct in_multi
**pinm
)
542 struct sockaddr_in gsin
;
543 struct ifmultiaddr
*ifma
;
544 struct in_multi
*inm
;
547 in_multihead_lock_shared();
548 IN_LOOKUP_MULTI(group
, ifp
, inm
);
551 VERIFY(inm
->inm_reqcnt
>= 1);
553 VERIFY(inm
->inm_reqcnt
!= 0);
556 in_multihead_lock_done();
558 * We already joined this group; return the inm
559 * with a refcount held (via lookup) for caller.
563 in_multihead_lock_done();
565 bzero(&gsin
, sizeof(gsin
));
566 gsin
.sin_family
= AF_INET
;
567 gsin
.sin_len
= sizeof(struct sockaddr_in
);
568 gsin
.sin_addr
= *group
;
571 * Check if a link-layer group is already associated
572 * with this network-layer group on the given ifnet.
574 error
= if_addmulti(ifp
, (struct sockaddr
*)&gsin
, &ifma
);
579 * See comments in inm_remref() for access to ifma_protospec.
581 in_multihead_lock_exclusive();
583 if ((inm
= ifma
->ifma_protospec
) != NULL
) {
584 VERIFY(ifma
->ifma_addr
!= NULL
);
585 VERIFY(ifma
->ifma_addr
->sa_family
== AF_INET
);
586 INM_ADDREF(inm
); /* for caller */
589 VERIFY(inm
->inm_ifma
== ifma
);
590 VERIFY(inm
->inm_ifp
== ifp
);
591 VERIFY(in_hosteq(inm
->inm_addr
, *group
));
592 if (inm
->inm_debug
& IFD_ATTACHED
) {
593 VERIFY(inm
->inm_reqcnt
>= 1);
595 VERIFY(inm
->inm_reqcnt
!= 0);
598 in_multihead_lock_done();
601 * We lost the race with another thread doing
602 * in_getmulti(); since this group has already
603 * been joined; return the inm with a refcount
609 * We lost the race with another thread doing in_delmulti();
610 * the inm referring to the ifma has been detached, thus we
611 * reattach it back to the in_multihead list and return the
612 * inm with a refcount held for the caller.
614 in_multi_attach(inm
);
615 VERIFY((inm
->inm_debug
&
616 (IFD_ATTACHED
| IFD_TRASHED
)) == IFD_ATTACHED
);
619 in_multihead_lock_done();
626 * A new in_multi record is needed; allocate and initialize it.
627 * We DO NOT perform an IGMP join as the in_ layer may need to
628 * push an initial source list down to IGMP to support SSM.
630 * The initial source filter state is INCLUDE, {} as per the RFC.
632 inm
= in_multi_alloc(M_WAITOK
);
634 in_multihead_lock_done();
639 inm
->inm_addr
= *group
;
641 inm
->inm_igi
= IGMP_IFINFO(ifp
);
642 VERIFY(inm
->inm_igi
!= NULL
);
643 IGI_ADDREF(inm
->inm_igi
);
644 inm
->inm_ifma
= ifma
; /* keep refcount from if_addmulti() */
645 inm
->inm_state
= IGMP_NOT_MEMBER
;
647 * Pending state-changes per group are subject to a bounds check.
649 inm
->inm_scq
.ifq_maxlen
= IGMP_MAX_STATE_CHANGES
;
650 inm
->inm_st
[0].iss_fmode
= MCAST_UNDEFINED
;
651 inm
->inm_st
[1].iss_fmode
= MCAST_UNDEFINED
;
652 RB_INIT(&inm
->inm_srcs
);
654 in_multi_attach(inm
);
655 VERIFY((inm
->inm_debug
& (IFD_ATTACHED
| IFD_TRASHED
)) == IFD_ATTACHED
);
656 INM_ADDREF_LOCKED(inm
); /* for caller */
660 VERIFY(ifma
->ifma_protospec
== NULL
);
661 ifma
->ifma_protospec
= inm
;
663 in_multihead_lock_done();
669 * Clear recorded source entries for a group.
670 * Used by the IGMP code.
671 * FIXME: Should reap.
674 inm_clear_recorded(struct in_multi
*inm
)
676 struct ip_msource
*ims
;
678 INM_LOCK_ASSERT_HELD(inm
);
680 RB_FOREACH(ims
, ip_msource_tree
, &inm
->inm_srcs
) {
683 --inm
->inm_st
[1].iss_rec
;
686 VERIFY(inm
->inm_st
[1].iss_rec
== 0);
690 * Record a source as pending for a Source-Group IGMPv3 query.
691 * This lives here as it modifies the shared tree.
693 * inm is the group descriptor.
694 * naddr is the address of the source to record in network-byte order.
696 * If the net.inet.igmp.sgalloc sysctl is non-zero, we will
697 * lazy-allocate a source node in response to an SG query.
698 * Otherwise, no allocation is performed. This saves some memory
699 * with the trade-off that the source will not be reported to the
700 * router if joined in the window between the query response and
701 * the group actually being joined on the local host.
703 * Return 0 if the source didn't exist or was already marked as recorded.
704 * Return 1 if the source was marked as recorded by this function.
705 * Return <0 if any error occured (negated errno code).
708 inm_record_source(struct in_multi
*inm
, const in_addr_t naddr
)
710 struct ip_msource find
;
711 struct ip_msource
*ims
, *nims
;
713 INM_LOCK_ASSERT_HELD(inm
);
715 find
.ims_haddr
= ntohl(naddr
);
716 ims
= RB_FIND(ip_msource_tree
, &inm
->inm_srcs
, &find
);
717 if (ims
&& ims
->ims_stp
)
720 if (inm
->inm_nsrc
== in_mcast_maxgrpsrc
)
722 nims
= ipms_alloc(M_WAITOK
);
725 nims
->ims_haddr
= find
.ims_haddr
;
726 RB_INSERT(ip_msource_tree
, &inm
->inm_srcs
, nims
);
732 * Mark the source as recorded and update the recorded
736 ++inm
->inm_st
[1].iss_rec
;
742 * Return a pointer to an in_msource owned by an in_mfilter,
743 * given its source address.
744 * Lazy-allocate if needed. If this is a new entry its filter state is
747 * imf is the filter set being modified.
748 * haddr is the source address in *host* byte-order.
750 * Caller is expected to be holding imo_lock.
753 imf_get_source(struct in_mfilter
*imf
, const struct sockaddr_in
*psin
,
754 struct in_msource
**plims
)
756 struct ip_msource find
;
757 struct ip_msource
*ims
;
758 struct in_msource
*lims
;
765 /* key is host byte order */
766 find
.ims_haddr
= ntohl(psin
->sin_addr
.s_addr
);
767 ims
= RB_FIND(ip_msource_tree
, &imf
->imf_sources
, &find
);
768 lims
= (struct in_msource
*)ims
;
770 if (imf
->imf_nsrc
== in_mcast_maxsocksrc
)
772 lims
= inms_alloc(M_WAITOK
);
775 lims
->ims_haddr
= find
.ims_haddr
;
776 lims
->imsl_st
[0] = MCAST_UNDEFINED
;
777 RB_INSERT(ip_msource_tree
, &imf
->imf_sources
,
778 (struct ip_msource
*)lims
);
788 * Graft a source entry into an existing socket-layer filter set,
789 * maintaining any required invariants and checking allocations.
791 * The source is marked as being in the new filter mode at t1.
793 * Return the pointer to the new node, otherwise return NULL.
795 * Caller is expected to be holding imo_lock.
797 static struct in_msource
*
798 imf_graft(struct in_mfilter
*imf
, const uint8_t st1
,
799 const struct sockaddr_in
*psin
)
801 struct in_msource
*lims
;
803 lims
= inms_alloc(M_WAITOK
);
806 lims
->ims_haddr
= ntohl(psin
->sin_addr
.s_addr
);
807 lims
->imsl_st
[0] = MCAST_UNDEFINED
;
808 lims
->imsl_st
[1] = st1
;
809 RB_INSERT(ip_msource_tree
, &imf
->imf_sources
,
810 (struct ip_msource
*)lims
);
817 * Prune a source entry from an existing socket-layer filter set,
818 * maintaining any required invariants and checking allocations.
820 * The source is marked as being left at t1, it is not freed.
822 * Return 0 if no error occurred, otherwise return an errno value.
824 * Caller is expected to be holding imo_lock.
827 imf_prune(struct in_mfilter
*imf
, const struct sockaddr_in
*psin
)
829 struct ip_msource find
;
830 struct ip_msource
*ims
;
831 struct in_msource
*lims
;
833 /* key is host byte order */
834 find
.ims_haddr
= ntohl(psin
->sin_addr
.s_addr
);
835 ims
= RB_FIND(ip_msource_tree
, &imf
->imf_sources
, &find
);
838 lims
= (struct in_msource
*)ims
;
839 lims
->imsl_st
[1] = MCAST_UNDEFINED
;
844 * Revert socket-layer filter set deltas at t1 to t0 state.
846 * Caller is expected to be holding imo_lock.
849 imf_rollback(struct in_mfilter
*imf
)
851 struct ip_msource
*ims
, *tims
;
852 struct in_msource
*lims
;
854 RB_FOREACH_SAFE(ims
, ip_msource_tree
, &imf
->imf_sources
, tims
) {
855 lims
= (struct in_msource
*)ims
;
856 if (lims
->imsl_st
[0] == lims
->imsl_st
[1]) {
857 /* no change at t1 */
859 } else if (lims
->imsl_st
[0] != MCAST_UNDEFINED
) {
860 /* revert change to existing source at t1 */
861 lims
->imsl_st
[1] = lims
->imsl_st
[0];
863 /* revert source added t1 */
864 IGMP_PRINTF(("%s: free inms %p\n", __func__
, lims
));
865 RB_REMOVE(ip_msource_tree
, &imf
->imf_sources
, ims
);
870 imf
->imf_st
[1] = imf
->imf_st
[0];
874 * Mark socket-layer filter set as INCLUDE {} at t1.
876 * Caller is expected to be holding imo_lock.
879 imf_leave(struct in_mfilter
*imf
)
881 struct ip_msource
*ims
;
882 struct in_msource
*lims
;
884 RB_FOREACH(ims
, ip_msource_tree
, &imf
->imf_sources
) {
885 lims
= (struct in_msource
*)ims
;
886 lims
->imsl_st
[1] = MCAST_UNDEFINED
;
888 imf
->imf_st
[1] = MCAST_INCLUDE
;
892 * Mark socket-layer filter set deltas as committed.
894 * Caller is expected to be holding imo_lock.
897 imf_commit(struct in_mfilter
*imf
)
899 struct ip_msource
*ims
;
900 struct in_msource
*lims
;
902 RB_FOREACH(ims
, ip_msource_tree
, &imf
->imf_sources
) {
903 lims
= (struct in_msource
*)ims
;
904 lims
->imsl_st
[0] = lims
->imsl_st
[1];
906 imf
->imf_st
[0] = imf
->imf_st
[1];
910 * Reap unreferenced sources from socket-layer filter set.
912 * Caller is expected to be holding imo_lock.
915 imf_reap(struct in_mfilter
*imf
)
917 struct ip_msource
*ims
, *tims
;
918 struct in_msource
*lims
;
920 RB_FOREACH_SAFE(ims
, ip_msource_tree
, &imf
->imf_sources
, tims
) {
921 lims
= (struct in_msource
*)ims
;
922 if ((lims
->imsl_st
[0] == MCAST_UNDEFINED
) &&
923 (lims
->imsl_st
[1] == MCAST_UNDEFINED
)) {
924 IGMP_PRINTF(("%s: free inms %p\n", __func__
, lims
));
925 RB_REMOVE(ip_msource_tree
, &imf
->imf_sources
, ims
);
933 * Purge socket-layer filter set.
935 * Caller is expected to be holding imo_lock.
938 imf_purge(struct in_mfilter
*imf
)
940 struct ip_msource
*ims
, *tims
;
941 struct in_msource
*lims
;
943 RB_FOREACH_SAFE(ims
, ip_msource_tree
, &imf
->imf_sources
, tims
) {
944 lims
= (struct in_msource
*)ims
;
945 IGMP_PRINTF(("%s: free inms %p\n", __func__
, lims
));
946 RB_REMOVE(ip_msource_tree
, &imf
->imf_sources
, ims
);
950 imf
->imf_st
[0] = imf
->imf_st
[1] = MCAST_UNDEFINED
;
951 VERIFY(RB_EMPTY(&imf
->imf_sources
));
955 * Look up a source filter entry for a multicast group.
957 * inm is the group descriptor to work with.
958 * haddr is the host-byte-order IPv4 address to look up.
959 * noalloc may be non-zero to suppress allocation of sources.
960 * *pims will be set to the address of the retrieved or allocated source.
962 * Return 0 if successful, otherwise return a non-zero error code.
965 inm_get_source(struct in_multi
*inm
, const in_addr_t haddr
,
966 const int noalloc
, struct ip_msource
**pims
)
968 struct ip_msource find
;
969 struct ip_msource
*ims
, *nims
;
973 INM_LOCK_ASSERT_HELD(inm
);
975 find
.ims_haddr
= haddr
;
976 ims
= RB_FIND(ip_msource_tree
, &inm
->inm_srcs
, &find
);
977 if (ims
== NULL
&& !noalloc
) {
978 if (inm
->inm_nsrc
== in_mcast_maxgrpsrc
)
980 nims
= ipms_alloc(M_WAITOK
);
983 nims
->ims_haddr
= haddr
;
984 RB_INSERT(ip_msource_tree
, &inm
->inm_srcs
, nims
);
988 ia
.s_addr
= htonl(haddr
);
989 IGMP_PRINTF(("%s: allocated %s as %p\n", __func__
,
990 inet_ntoa(ia
), ims
));
999 * Helper function to derive the filter mode on a source entry
1000 * from its internal counters. Predicates are:
1001 * A source is only excluded if all listeners exclude it.
1002 * A source is only included if no listeners exclude it,
1003 * and at least one listener includes it.
1004 * May be used by ifmcstat(8).
1007 ims_get_mode(const struct in_multi
*inm
, const struct ip_msource
*ims
,
1010 INM_LOCK_ASSERT_HELD(INM_CAST_TO_NONCONST(inm
));
1013 if (inm
->inm_st
[t
].iss_ex
> 0 &&
1014 inm
->inm_st
[t
].iss_ex
== ims
->ims_st
[t
].ex
)
1015 return (MCAST_EXCLUDE
);
1016 else if (ims
->ims_st
[t
].in
> 0 && ims
->ims_st
[t
].ex
== 0)
1017 return (MCAST_INCLUDE
);
1018 return (MCAST_UNDEFINED
);
1022 * Merge socket-layer source into IGMP-layer source.
1023 * If rollback is non-zero, perform the inverse of the merge.
1026 ims_merge(struct ip_msource
*ims
, const struct in_msource
*lims
,
1029 int n
= rollback
? -1 : 1;
1033 ia
.s_addr
= htonl(ims
->ims_haddr
);
1036 if (lims
->imsl_st
[0] == MCAST_EXCLUDE
) {
1037 IGMP_PRINTF(("%s: t1 ex -= %d on %s\n",
1038 __func__
, n
, inet_ntoa(ia
)));
1039 ims
->ims_st
[1].ex
-= n
;
1040 } else if (lims
->imsl_st
[0] == MCAST_INCLUDE
) {
1041 IGMP_PRINTF(("%s: t1 in -= %d on %s\n",
1042 __func__
, n
, inet_ntoa(ia
)));
1043 ims
->ims_st
[1].in
-= n
;
1046 if (lims
->imsl_st
[1] == MCAST_EXCLUDE
) {
1047 IGMP_PRINTF(("%s: t1 ex += %d on %s\n",
1048 __func__
, n
, inet_ntoa(ia
)));
1049 ims
->ims_st
[1].ex
+= n
;
1050 } else if (lims
->imsl_st
[1] == MCAST_INCLUDE
) {
1051 IGMP_PRINTF(("%s: t1 in += %d on %s\n",
1052 __func__
, n
, inet_ntoa(ia
)));
1053 ims
->ims_st
[1].in
+= n
;
1058 * Atomically update the global in_multi state, when a membership's
1059 * filter list is being updated in any way.
1061 * imf is the per-inpcb-membership group filter pointer.
1062 * A fake imf may be passed for in-kernel consumers.
1064 * XXX This is a candidate for a set-symmetric-difference style loop
1065 * which would eliminate the repeated lookup from root of ims nodes,
1066 * as they share the same key space.
1068 * If any error occurred this function will back out of refcounts
1069 * and return a non-zero value.
1072 inm_merge(struct in_multi
*inm
, /*const*/ struct in_mfilter
*imf
)
1074 struct ip_msource
*ims
, *nims
;
1075 struct in_msource
*lims
;
1076 int schanged
, error
;
1079 INM_LOCK_ASSERT_HELD(inm
);
1086 * Update the source filters first, as this may fail.
1087 * Maintain count of in-mode filters at t0, t1. These are
1088 * used to work out if we transition into ASM mode or not.
1089 * Maintain a count of source filters whose state was
1090 * actually modified by this operation.
1092 RB_FOREACH(ims
, ip_msource_tree
, &imf
->imf_sources
) {
1093 lims
= (struct in_msource
*)ims
;
1094 if (lims
->imsl_st
[0] == imf
->imf_st
[0]) nsrc0
++;
1095 if (lims
->imsl_st
[1] == imf
->imf_st
[1]) nsrc1
++;
1096 if (lims
->imsl_st
[0] == lims
->imsl_st
[1]) continue;
1097 error
= inm_get_source(inm
, lims
->ims_haddr
, 0, &nims
);
1101 ims_merge(nims
, lims
, 0);
1104 struct ip_msource
*bims
;
1106 RB_FOREACH_REVERSE_FROM(ims
, ip_msource_tree
, nims
) {
1107 lims
= (struct in_msource
*)ims
;
1108 if (lims
->imsl_st
[0] == lims
->imsl_st
[1])
1110 (void) inm_get_source(inm
, lims
->ims_haddr
, 1, &bims
);
1113 ims_merge(bims
, lims
, 1);
1118 IGMP_PRINTF(("%s: imf filters in-mode: %d at t0, %d at t1\n",
1119 __func__
, nsrc0
, nsrc1
));
1121 /* Handle transition between INCLUDE {n} and INCLUDE {} on socket. */
1122 if (imf
->imf_st
[0] == imf
->imf_st
[1] &&
1123 imf
->imf_st
[1] == MCAST_INCLUDE
) {
1125 IGMP_PRINTF(("%s: --in on inm at t1\n", __func__
));
1126 --inm
->inm_st
[1].iss_in
;
1130 /* Handle filter mode transition on socket. */
1131 if (imf
->imf_st
[0] != imf
->imf_st
[1]) {
1132 IGMP_PRINTF(("%s: imf transition %d to %d\n",
1133 __func__
, imf
->imf_st
[0], imf
->imf_st
[1]));
1135 if (imf
->imf_st
[0] == MCAST_EXCLUDE
) {
1136 IGMP_PRINTF(("%s: --ex on inm at t1\n", __func__
));
1137 --inm
->inm_st
[1].iss_ex
;
1138 } else if (imf
->imf_st
[0] == MCAST_INCLUDE
) {
1139 IGMP_PRINTF(("%s: --in on inm at t1\n", __func__
));
1140 --inm
->inm_st
[1].iss_in
;
1143 if (imf
->imf_st
[1] == MCAST_EXCLUDE
) {
1144 IGMP_PRINTF(("%s: ex++ on inm at t1\n", __func__
));
1145 inm
->inm_st
[1].iss_ex
++;
1146 } else if (imf
->imf_st
[1] == MCAST_INCLUDE
&& nsrc1
> 0) {
1147 IGMP_PRINTF(("%s: in++ on inm at t1\n", __func__
));
1148 inm
->inm_st
[1].iss_in
++;
1153 * Track inm filter state in terms of listener counts.
1154 * If there are any exclusive listeners, stack-wide
1155 * membership is exclusive.
1156 * Otherwise, if only inclusive listeners, stack-wide is inclusive.
1157 * If no listeners remain, state is undefined at t1,
1158 * and the IGMP lifecycle for this group should finish.
1160 if (inm
->inm_st
[1].iss_ex
> 0) {
1161 IGMP_PRINTF(("%s: transition to EX\n", __func__
));
1162 inm
->inm_st
[1].iss_fmode
= MCAST_EXCLUDE
;
1163 } else if (inm
->inm_st
[1].iss_in
> 0) {
1164 IGMP_PRINTF(("%s: transition to IN\n", __func__
));
1165 inm
->inm_st
[1].iss_fmode
= MCAST_INCLUDE
;
1167 IGMP_PRINTF(("%s: transition to UNDEF\n", __func__
));
1168 inm
->inm_st
[1].iss_fmode
= MCAST_UNDEFINED
;
1171 /* Decrement ASM listener count on transition out of ASM mode. */
1172 if (imf
->imf_st
[0] == MCAST_EXCLUDE
&& nsrc0
== 0) {
1173 if ((imf
->imf_st
[1] != MCAST_EXCLUDE
) ||
1174 (imf
->imf_st
[1] == MCAST_EXCLUDE
&& nsrc1
> 0)) {
1175 IGMP_PRINTF(("%s: --asm on inm at t1\n", __func__
));
1176 --inm
->inm_st
[1].iss_asm
;
1180 /* Increment ASM listener count on transition to ASM mode. */
1181 if (imf
->imf_st
[1] == MCAST_EXCLUDE
&& nsrc1
== 0) {
1182 IGMP_PRINTF(("%s: asm++ on inm at t1\n", __func__
));
1183 inm
->inm_st
[1].iss_asm
++;
1186 IGMP_PRINTF(("%s: merged imf %p to inm %p\n", __func__
, imf
, inm
));
1191 IGMP_PRINTF(("%s: sources changed; reaping\n", __func__
));
1198 * Mark an in_multi's filter set deltas as committed.
1199 * Called by IGMP after a state change has been enqueued.
1202 inm_commit(struct in_multi
*inm
)
1204 struct ip_msource
*ims
;
1206 INM_LOCK_ASSERT_HELD(inm
);
1208 IGMP_PRINTF(("%s: commit inm %p\n", __func__
, inm
));
1209 IGMP_PRINTF(("%s: pre commit:\n", __func__
));
1212 RB_FOREACH(ims
, ip_msource_tree
, &inm
->inm_srcs
) {
1213 ims
->ims_st
[0] = ims
->ims_st
[1];
1215 inm
->inm_st
[0] = inm
->inm_st
[1];
1219 * Reap unreferenced nodes from an in_multi's filter set.
1222 inm_reap(struct in_multi
*inm
)
1224 struct ip_msource
*ims
, *tims
;
1226 INM_LOCK_ASSERT_HELD(inm
);
1228 RB_FOREACH_SAFE(ims
, ip_msource_tree
, &inm
->inm_srcs
, tims
) {
1229 if (ims
->ims_st
[0].ex
> 0 || ims
->ims_st
[0].in
> 0 ||
1230 ims
->ims_st
[1].ex
> 0 || ims
->ims_st
[1].in
> 0 ||
1233 IGMP_PRINTF(("%s: free ims %p\n", __func__
, ims
));
1234 RB_REMOVE(ip_msource_tree
, &inm
->inm_srcs
, ims
);
1241 * Purge all source nodes from an in_multi's filter set.
1244 inm_purge(struct in_multi
*inm
)
1246 struct ip_msource
*ims
, *tims
;
1248 INM_LOCK_ASSERT_HELD(inm
);
1250 RB_FOREACH_SAFE(ims
, ip_msource_tree
, &inm
->inm_srcs
, tims
) {
1251 IGMP_PRINTF(("%s: free ims %p\n", __func__
, ims
));
1252 RB_REMOVE(ip_msource_tree
, &inm
->inm_srcs
, ims
);
1259 * Join a multicast group; real entry point.
1261 * Only preserves atomicity at inm level.
1262 * NOTE: imf argument cannot be const due to sys/tree.h limitations.
1264 * If the IGMP downcall fails, the group is not joined, and an error
1268 in_joingroup(struct ifnet
*ifp
, const struct in_addr
*gina
,
1269 /*const*/ struct in_mfilter
*imf
, struct in_multi
**pinm
)
1271 struct in_mfilter timf
;
1272 struct in_multi
*inm
= NULL
;
1275 IGMP_PRINTF(("%s: join %s on %p(%s%d))\n", __func__
,
1276 inet_ntoa(*gina
), ifp
, ifp
->if_name
, ifp
->if_unit
));
1281 * If no imf was specified (i.e. kernel consumer),
1282 * fake one up and assume it is an ASM join.
1285 imf_init(&timf
, MCAST_UNDEFINED
, MCAST_EXCLUDE
);
1289 error
= in_getmulti(ifp
, gina
, &inm
);
1291 IGMP_PRINTF(("%s: in_getmulti() failure\n", __func__
));
1295 IGMP_PRINTF(("%s: merge inm state\n", __func__
));
1298 error
= inm_merge(inm
, imf
);
1300 IGMP_PRINTF(("%s: failed to merge inm state\n", __func__
));
1301 goto out_inm_release
;
1304 IGMP_PRINTF(("%s: doing igmp downcall\n", __func__
));
1305 error
= igmp_change_state(inm
);
1307 IGMP_PRINTF(("%s: failed to update source\n", __func__
));
1308 goto out_inm_release
;
1313 IGMP_PRINTF(("%s: dropping ref on %p\n", __func__
, inm
));
1318 *pinm
= inm
; /* keep refcount from in_getmulti() */
1325 * Leave a multicast group; real entry point.
1326 * All source filters will be expunged.
1328 * Only preserves atomicity at inm level.
1330 * Note: This is not the same as inm_release(*) as this function also
1331 * makes a state change downcall into IGMP.
1334 in_leavegroup(struct in_multi
*inm
, /*const*/ struct in_mfilter
*imf
)
1336 struct in_mfilter timf
;
1341 INM_LOCK_ASSERT_NOTHELD(inm
);
1343 in_multihead_lock_exclusive();
1346 IGMP_PRINTF(("%s: leave inm %p, %s/%s%d, imf %p\n", __func__
,
1347 inm
, inet_ntoa(inm
->inm_addr
),
1348 (inm_is_ifp_detached(inm
) ? "null" : inm
->inm_ifp
->if_name
),
1349 inm
->inm_ifp
->if_unit
, imf
));
1352 * If no imf was specified (i.e. kernel consumer),
1353 * fake one up and assume it is an ASM join.
1356 imf_init(&timf
, MCAST_EXCLUDE
, MCAST_UNDEFINED
);
1361 * Begin state merge transaction at IGMP layer.
1363 * As this particular invocation should not cause any memory
1364 * to be allocated, and there is no opportunity to roll back
1365 * the transaction, it MUST NOT fail.
1367 IGMP_PRINTF(("%s: merge inm state\n", __func__
));
1369 error
= inm_merge(inm
, imf
);
1370 KASSERT(error
== 0, ("%s: failed to merge inm state\n", __func__
));
1372 IGMP_PRINTF(("%s: doing igmp downcall\n", __func__
));
1373 error
= igmp_change_state(inm
);
1376 IGMP_PRINTF(("%s: failed igmp downcall\n", __func__
));
1378 lastref
= in_multi_detach(inm
);
1379 VERIFY(!lastref
|| (!(inm
->inm_debug
& IFD_ATTACHED
) &&
1380 inm
->inm_reqcnt
== 0));
1382 in_multihead_lock_done();
1385 INM_REMREF(inm
); /* for in_multihead list */
1391 * Join an IPv4 multicast group in (*,G) exclusive mode.
1392 * The group must be a 224.0.0.0/24 link-scope group.
1393 * This KPI is for legacy kernel consumers only.
1396 in_addmulti(struct in_addr
*ap
, struct ifnet
*ifp
)
1398 struct in_multi
*pinm
= NULL
;
1401 KASSERT(IN_LOCAL_GROUP(ntohl(ap
->s_addr
)),
1402 ("%s: %s not in 224.0.0.0/24\n", __func__
, inet_ntoa(*ap
)));
1404 error
= in_joingroup(ifp
, ap
, NULL
, &pinm
);
1405 VERIFY(pinm
!= NULL
|| error
!= 0);
1411 * Leave an IPv4 multicast group, assumed to be in exclusive (*,G) mode.
1412 * This KPI is for legacy kernel consumers only.
1415 in_delmulti(struct in_multi
*inm
)
1418 (void) in_leavegroup(inm
, NULL
);
1422 * Block or unblock an ASM multicast source on an inpcb.
1423 * This implements the delta-based API described in RFC 3678.
1425 * The delta-based API applies only to exclusive-mode memberships.
1426 * An IGMP downcall will be performed.
1428 * Return 0 if successful, otherwise return an appropriate error code.
1431 inp_block_unblock_source(struct inpcb
*inp
, struct sockopt
*sopt
)
1433 struct group_source_req gsr
;
1434 sockunion_t
*gsa
, *ssa
;
1436 struct in_mfilter
*imf
;
1437 struct ip_moptions
*imo
;
1438 struct in_msource
*ims
;
1439 struct in_multi
*inm
;
1443 unsigned int ifindex
= 0;
1449 memset(&gsr
, 0, sizeof(struct group_source_req
));
1450 gsa
= (sockunion_t
*)&gsr
.gsr_group
;
1451 ssa
= (sockunion_t
*)&gsr
.gsr_source
;
1453 switch (sopt
->sopt_name
) {
1454 case IP_BLOCK_SOURCE
:
1455 case IP_UNBLOCK_SOURCE
: {
1456 struct ip_mreq_source mreqs
;
1458 error
= sooptcopyin(sopt
, &mreqs
,
1459 sizeof(struct ip_mreq_source
),
1460 sizeof(struct ip_mreq_source
));
1464 gsa
->sin
.sin_family
= AF_INET
;
1465 gsa
->sin
.sin_len
= sizeof(struct sockaddr_in
);
1466 gsa
->sin
.sin_addr
= mreqs
.imr_multiaddr
;
1468 ssa
->sin
.sin_family
= AF_INET
;
1469 ssa
->sin
.sin_len
= sizeof(struct sockaddr_in
);
1470 ssa
->sin
.sin_addr
= mreqs
.imr_sourceaddr
;
1472 if (!in_nullhost(mreqs
.imr_interface
))
1473 ifp
= ip_multicast_if(&mreqs
.imr_interface
, &ifindex
);
1475 if (sopt
->sopt_name
== IP_BLOCK_SOURCE
)
1478 IGMP_PRINTF(("%s: imr_interface = %s, ifp = %p\n",
1479 __func__
, inet_ntoa(mreqs
.imr_interface
), ifp
));
1483 case MCAST_BLOCK_SOURCE
:
1484 case MCAST_UNBLOCK_SOURCE
:
1485 error
= sooptcopyin(sopt
, &gsr
,
1486 sizeof(struct group_source_req
),
1487 sizeof(struct group_source_req
));
1491 if (gsa
->sin
.sin_family
!= AF_INET
||
1492 gsa
->sin
.sin_len
!= sizeof(struct sockaddr_in
))
1495 if (ssa
->sin
.sin_family
!= AF_INET
||
1496 ssa
->sin
.sin_len
!= sizeof(struct sockaddr_in
))
1499 ifnet_head_lock_shared();
1500 if (gsr
.gsr_interface
== 0 ||
1501 (u_int
)if_index
< gsr
.gsr_interface
) {
1503 return (EADDRNOTAVAIL
);
1506 ifp
= ifindex2ifnet
[gsr
.gsr_interface
];
1510 return (EADDRNOTAVAIL
);
1512 if (sopt
->sopt_name
== MCAST_BLOCK_SOURCE
)
1517 IGMP_PRINTF(("%s: unknown sopt_name %d\n",
1518 __func__
, sopt
->sopt_name
));
1519 return (EOPNOTSUPP
);
1523 if (!IN_MULTICAST(ntohl(gsa
->sin
.sin_addr
.s_addr
)))
1527 * Check if we are actually a member of this group.
1529 imo
= inp_findmoptions(inp
);
1534 idx
= imo_match_group(imo
, ifp
, &gsa
->sa
);
1535 if (idx
== (size_t)-1 || imo
->imo_mfilters
== NULL
) {
1536 error
= EADDRNOTAVAIL
;
1537 goto out_imo_locked
;
1540 VERIFY(imo
->imo_mfilters
!= NULL
);
1541 imf
= &imo
->imo_mfilters
[idx
];
1542 inm
= imo
->imo_membership
[idx
];
1545 * Attempting to use the delta-based API on an
1546 * non exclusive-mode membership is an error.
1548 fmode
= imf
->imf_st
[0];
1549 if (fmode
!= MCAST_EXCLUDE
) {
1551 goto out_imo_locked
;
1555 * Deal with error cases up-front:
1556 * Asked to block, but already blocked; or
1557 * Asked to unblock, but nothing to unblock.
1558 * If adding a new block entry, allocate it.
1560 ims
= imo_match_source(imo
, idx
, &ssa
->sa
);
1561 if ((ims
!= NULL
&& doblock
) || (ims
== NULL
&& !doblock
)) {
1562 IGMP_PRINTF(("%s: source %s %spresent\n", __func__
,
1563 inet_ntoa(ssa
->sin
.sin_addr
), doblock
? "" : "not "));
1564 error
= EADDRNOTAVAIL
;
1565 goto out_imo_locked
;
1569 * Begin state merge transaction at socket layer.
1572 IGMP_PRINTF(("%s: %s source\n", __func__
, "block"));
1573 ims
= imf_graft(imf
, fmode
, &ssa
->sin
);
1577 IGMP_PRINTF(("%s: %s source\n", __func__
, "allow"));
1578 error
= imf_prune(imf
, &ssa
->sin
);
1582 IGMP_PRINTF(("%s: merge imf state failed\n", __func__
));
1583 goto out_imf_rollback
;
1587 * Begin state merge transaction at IGMP layer.
1590 IGMP_PRINTF(("%s: merge inm state\n", __func__
));
1591 error
= inm_merge(inm
, imf
);
1593 IGMP_PRINTF(("%s: failed to merge inm state\n", __func__
));
1595 goto out_imf_rollback
;
1598 IGMP_PRINTF(("%s: doing igmp downcall\n", __func__
));
1599 error
= igmp_change_state(inm
);
1603 IGMP_PRINTF(("%s: failed igmp downcall\n", __func__
));
1616 IMO_REMREF(imo
); /* from inp_findmoptions() */
1621 * Given an inpcb, return its multicast options structure pointer.
1623 * Caller is responsible for locking the inpcb, and releasing the
1624 * extra reference held on the imo, upon a successful return.
1626 static struct ip_moptions
*
1627 inp_findmoptions(struct inpcb
*inp
)
1629 struct ip_moptions
*imo
;
1630 struct in_multi
**immp
;
1631 struct in_mfilter
*imfp
;
1634 if ((imo
= inp
->inp_moptions
) != NULL
) {
1635 IMO_ADDREF(imo
); /* for caller */
1639 imo
= ip_allocmoptions(M_WAITOK
);
1643 immp
= _MALLOC(sizeof (*immp
) * IP_MIN_MEMBERSHIPS
, M_IPMOPTS
,
1650 imfp
= _MALLOC(sizeof (struct in_mfilter
) * IP_MIN_MEMBERSHIPS
,
1651 M_INMFILTER
, M_WAITOK
| M_ZERO
);
1653 _FREE(immp
, M_IPMOPTS
);
1658 imo
->imo_multicast_ifp
= NULL
;
1659 imo
->imo_multicast_addr
.s_addr
= INADDR_ANY
;
1660 imo
->imo_multicast_vif
= -1;
1661 imo
->imo_multicast_ttl
= IP_DEFAULT_MULTICAST_TTL
;
1662 imo
->imo_multicast_loop
= in_mcast_loop
;
1663 imo
->imo_num_memberships
= 0;
1664 imo
->imo_max_memberships
= IP_MIN_MEMBERSHIPS
;
1665 imo
->imo_membership
= immp
;
1667 /* Initialize per-group source filters. */
1668 for (idx
= 0; idx
< IP_MIN_MEMBERSHIPS
; idx
++)
1669 imf_init(&imfp
[idx
], MCAST_UNDEFINED
, MCAST_EXCLUDE
);
1671 imo
->imo_mfilters
= imfp
;
1672 inp
->inp_moptions
= imo
; /* keep reference from ip_allocmoptions() */
1673 IMO_ADDREF(imo
); /* for caller */
1678 * Atomically get source filters on a socket for an IPv4 multicast group.
1681 inp_get_source_filters(struct inpcb
*inp
, struct sockopt
*sopt
)
1683 struct __msfilterreq64 msfr
, msfr64
;
1684 struct __msfilterreq32 msfr32
;
1687 struct ip_moptions
*imo
;
1688 struct in_mfilter
*imf
;
1689 struct ip_msource
*ims
;
1690 struct in_msource
*lims
;
1691 struct sockaddr_in
*psin
;
1692 struct sockaddr_storage
*ptss
;
1693 struct sockaddr_storage
*tss
;
1695 size_t idx
, nsrcs
, ncsrcs
;
1696 user_addr_t tmp_ptr
;
1698 imo
= inp
->inp_moptions
;
1699 VERIFY(imo
!= NULL
);
1701 if (IS_64BIT_PROCESS(current_proc())) {
1702 error
= sooptcopyin(sopt
, &msfr64
,
1703 sizeof(struct __msfilterreq64
),
1704 sizeof(struct __msfilterreq64
));
1707 /* we never use msfr.msfr_srcs; */
1708 memcpy(&msfr
, &msfr64
, sizeof(msfr
));
1710 error
= sooptcopyin(sopt
, &msfr32
,
1711 sizeof(struct __msfilterreq32
),
1712 sizeof(struct __msfilterreq32
));
1715 /* we never use msfr.msfr_srcs; */
1716 memcpy(&msfr
, &msfr32
, sizeof(msfr
));
1719 ifnet_head_lock_shared();
1720 if (msfr
.msfr_ifindex
== 0 || (u_int
)if_index
< msfr
.msfr_ifindex
) {
1722 return (EADDRNOTAVAIL
);
1725 ifp
= ifindex2ifnet
[msfr
.msfr_ifindex
];
1729 return (EADDRNOTAVAIL
);
1731 if ((size_t) msfr
.msfr_nsrcs
>
1732 SIZE_MAX
/ sizeof(struct sockaddr_storage
))
1733 msfr
.msfr_nsrcs
= SIZE_MAX
/ sizeof(struct sockaddr_storage
);
1735 if (msfr
.msfr_nsrcs
> in_mcast_maxsocksrc
)
1736 msfr
.msfr_nsrcs
= in_mcast_maxsocksrc
;
1740 * Lookup group on the socket.
1742 gsa
= (sockunion_t
*)&msfr
.msfr_group
;
1743 idx
= imo_match_group(imo
, ifp
, &gsa
->sa
);
1744 if (idx
== (size_t)-1 || imo
->imo_mfilters
== NULL
) {
1746 return (EADDRNOTAVAIL
);
1748 imf
= &imo
->imo_mfilters
[idx
];
1751 * Ignore memberships which are in limbo.
1753 if (imf
->imf_st
[1] == MCAST_UNDEFINED
) {
1757 msfr
.msfr_fmode
= imf
->imf_st
[1];
1760 * If the user specified a buffer, copy out the source filter
1761 * entries to userland gracefully.
1762 * We only copy out the number of entries which userland
1763 * has asked for, but we always tell userland how big the
1764 * buffer really needs to be.
1767 if (IS_64BIT_PROCESS(current_proc()))
1768 tmp_ptr
= msfr64
.msfr_srcs
;
1770 tmp_ptr
= CAST_USER_ADDR_T(msfr32
.msfr_srcs
);
1773 if (tmp_ptr
!= USER_ADDR_NULL
&& msfr
.msfr_nsrcs
> 0) {
1774 tss
= _MALLOC((size_t) msfr
.msfr_nsrcs
* sizeof(*tss
),
1775 M_TEMP
, M_WAITOK
| M_ZERO
);
1780 bzero(tss
, (size_t) msfr
.msfr_nsrcs
* sizeof(*tss
));
1784 * Count number of sources in-mode at t0.
1785 * If buffer space exists and remains, copy out source entries.
1787 nsrcs
= msfr
.msfr_nsrcs
;
1790 RB_FOREACH(ims
, ip_msource_tree
, &imf
->imf_sources
) {
1791 lims
= (struct in_msource
*)ims
;
1792 if (lims
->imsl_st
[0] == MCAST_UNDEFINED
||
1793 lims
->imsl_st
[0] != imf
->imf_st
[0])
1795 if (tss
!= NULL
&& nsrcs
> 0) {
1796 psin
= (struct sockaddr_in
*)ptss
;
1797 psin
->sin_family
= AF_INET
;
1798 psin
->sin_len
= sizeof(struct sockaddr_in
);
1799 psin
->sin_addr
.s_addr
= htonl(lims
->ims_haddr
);
1810 error
= copyout(tss
, tmp_ptr
, ncsrcs
* sizeof(*tss
));
1816 msfr
.msfr_nsrcs
= ncsrcs
;
1817 if (IS_64BIT_PROCESS(current_proc())) {
1818 msfr64
.msfr_ifindex
= msfr
.msfr_ifindex
;
1819 msfr64
.msfr_fmode
= msfr
.msfr_fmode
;
1820 msfr64
.msfr_nsrcs
= msfr
.msfr_nsrcs
;
1821 memcpy(&msfr64
.msfr_group
, &msfr
.msfr_group
,
1822 sizeof(struct sockaddr_storage
));
1823 error
= sooptcopyout(sopt
, &msfr64
,
1824 sizeof(struct __msfilterreq64
));
1826 msfr32
.msfr_ifindex
= msfr
.msfr_ifindex
;
1827 msfr32
.msfr_fmode
= msfr
.msfr_fmode
;
1828 msfr32
.msfr_nsrcs
= msfr
.msfr_nsrcs
;
1829 memcpy(&msfr64
.msfr_group
, &msfr
.msfr_group
,
1830 sizeof(struct sockaddr_storage
));
1831 error
= sooptcopyout(sopt
, &msfr32
,
1832 sizeof(struct __msfilterreq32
));
1839 * Return the IP multicast options in response to user getsockopt().
1842 inp_getmoptions(struct inpcb
*inp
, struct sockopt
*sopt
)
1844 struct ip_mreqn mreqn
;
1845 struct ip_moptions
*imo
;
1847 struct in_ifaddr
*ia
;
1849 unsigned int ifindex
;
1852 imo
= inp
->inp_moptions
;
1854 * If socket is neither of type SOCK_RAW or SOCK_DGRAM,
1855 * or is a divert socket, reject it.
1857 if (inp
->inp_socket
->so_proto
->pr_protocol
== IPPROTO_DIVERT
||
1858 (inp
->inp_socket
->so_proto
->pr_type
!= SOCK_RAW
&&
1859 inp
->inp_socket
->so_proto
->pr_type
!= SOCK_DGRAM
)) {
1860 return (EOPNOTSUPP
);
1864 switch (sopt
->sopt_name
) {
1866 case IP_MULTICAST_VIF
:
1869 optval
= imo
->imo_multicast_vif
;
1873 error
= sooptcopyout(sopt
, &optval
, sizeof(int));
1875 #endif /* MROUTING */
1877 case IP_MULTICAST_IF
:
1878 memset(&mreqn
, 0, sizeof(struct ip_mreqn
));
1881 ifp
= imo
->imo_multicast_ifp
;
1882 if (!in_nullhost(imo
->imo_multicast_addr
)) {
1883 mreqn
.imr_address
= imo
->imo_multicast_addr
;
1884 } else if (ifp
!= NULL
) {
1885 mreqn
.imr_ifindex
= ifp
->if_index
;
1888 IFA_LOCK_SPIN(&ia
->ia_ifa
);
1890 IA_SIN(ia
)->sin_addr
;
1891 IFA_UNLOCK(&ia
->ia_ifa
);
1892 IFA_REMREF(&ia
->ia_ifa
);
1897 if (sopt
->sopt_valsize
== sizeof(struct ip_mreqn
)) {
1898 error
= sooptcopyout(sopt
, &mreqn
,
1899 sizeof(struct ip_mreqn
));
1901 error
= sooptcopyout(sopt
, &mreqn
.imr_address
,
1902 sizeof(struct in_addr
));
1906 case IP_MULTICAST_IFINDEX
:
1909 if (imo
== NULL
|| imo
->imo_multicast_ifp
== NULL
) {
1912 ifindex
= imo
->imo_multicast_ifp
->if_index
;
1916 error
= sooptcopyout(sopt
, &ifindex
, sizeof (ifindex
));
1919 case IP_MULTICAST_TTL
:
1921 optval
= coptval
= IP_DEFAULT_MULTICAST_TTL
;
1924 optval
= coptval
= imo
->imo_multicast_ttl
;
1927 if (sopt
->sopt_valsize
== sizeof(u_char
))
1928 error
= sooptcopyout(sopt
, &coptval
, sizeof(u_char
));
1930 error
= sooptcopyout(sopt
, &optval
, sizeof(int));
1933 case IP_MULTICAST_LOOP
:
1935 optval
= coptval
= IP_DEFAULT_MULTICAST_LOOP
;
1938 optval
= coptval
= imo
->imo_multicast_loop
;
1941 if (sopt
->sopt_valsize
== sizeof(u_char
))
1942 error
= sooptcopyout(sopt
, &coptval
, sizeof(u_char
));
1944 error
= sooptcopyout(sopt
, &optval
, sizeof(int));
1949 error
= EADDRNOTAVAIL
;
1951 error
= inp_get_source_filters(inp
, sopt
);
1956 error
= ENOPROTOOPT
;
1964 * Look up the ifnet to use for a multicast group membership,
1965 * given the IPv4 address of an interface, and the IPv4 group address.
1967 * This routine exists to support legacy multicast applications
1968 * which do not understand that multicast memberships are scoped to
1969 * specific physical links in the networking stack, or which need
1970 * to join link-scope groups before IPv4 addresses are configured.
1972 * If inp is non-NULL and is bound to an interface, use this socket's
1973 * inp_boundif for any required routing table lookup.
1975 * If the route lookup fails, attempt to use the first non-loopback
1976 * interface with multicast capability in the system as a
1977 * last resort. The legacy IPv4 ASM API requires that we do
1978 * this in order to allow groups to be joined when the routing
1979 * table has not yet been populated during boot.
1981 * Returns NULL if no ifp could be found.
1984 static struct ifnet
*
1985 inp_lookup_mcast_ifp(const struct inpcb
*inp
,
1986 const struct sockaddr_in
*gsin
, const struct in_addr ina
)
1989 unsigned int ifindex
= 0;
1991 VERIFY(gsin
->sin_family
== AF_INET
);
1992 VERIFY(IN_MULTICAST(ntohl(gsin
->sin_addr
.s_addr
)));
1995 if (!in_nullhost(ina
)) {
1996 struct in_addr new_ina
;
1997 memcpy(&new_ina
, &ina
, sizeof(struct in_addr
));
1998 ifp
= ip_multicast_if(&new_ina
, &ifindex
);
2001 unsigned int ifscope
= IFSCOPE_NONE
;
2003 if (inp
!= NULL
&& (inp
->inp_flags
& INP_BOUND_IF
))
2004 ifscope
= inp
->inp_boundifp
->if_index
;
2006 bzero(&ro
, sizeof (ro
));
2007 memcpy(&ro
.ro_dst
, gsin
, sizeof(struct sockaddr_in
));
2008 rtalloc_scoped_ign(&ro
, 0, ifscope
);
2009 if (ro
.ro_rt
!= NULL
) {
2010 ifp
= ro
.ro_rt
->rt_ifp
;
2011 VERIFY(ifp
!= NULL
);
2014 struct in_ifaddr
*ia
;
2018 lck_rw_lock_shared(in_ifaddr_rwlock
);
2019 TAILQ_FOREACH(ia
, &in_ifaddrhead
, ia_link
) {
2020 IFA_LOCK_SPIN(&ia
->ia_ifa
);
2022 IFA_UNLOCK(&ia
->ia_ifa
);
2023 if (!(mifp
->if_flags
& IFF_LOOPBACK
) &&
2024 (mifp
->if_flags
& IFF_MULTICAST
)) {
2029 lck_rw_done(in_ifaddr_rwlock
);
2037 * Join an IPv4 multicast group, possibly with a source.
2039 * NB: sopt->sopt_val might point to the kernel address space. This means that
2040 * we were called by the IPv6 stack due to the presence of an IPv6 v4 mapped
2041 * address. In this scenario, sopt_p points to kernproc and sooptcopyin() will
2042 * just issue an in-kernel memcpy.
2045 inp_join_group(struct inpcb
*inp
, struct sockopt
*sopt
)
2047 struct group_source_req gsr
;
2048 sockunion_t
*gsa
, *ssa
;
2050 struct in_mfilter
*imf
;
2051 struct ip_moptions
*imo
;
2052 struct in_multi
*inm
= NULL
;
2053 struct in_msource
*lims
;
2062 memset(&gsr
, 0, sizeof(struct group_source_req
));
2063 gsa
= (sockunion_t
*)&gsr
.gsr_group
;
2064 gsa
->ss
.ss_family
= AF_UNSPEC
;
2065 ssa
= (sockunion_t
*)&gsr
.gsr_source
;
2066 ssa
->ss
.ss_family
= AF_UNSPEC
;
2068 switch (sopt
->sopt_name
) {
2069 case IP_ADD_MEMBERSHIP
:
2070 case IP_ADD_SOURCE_MEMBERSHIP
: {
2071 struct ip_mreq_source mreqs
;
2073 if (sopt
->sopt_name
== IP_ADD_MEMBERSHIP
) {
2074 error
= sooptcopyin(sopt
, &mreqs
,
2075 sizeof(struct ip_mreq
),
2076 sizeof(struct ip_mreq
));
2078 * Do argument switcharoo from ip_mreq into
2079 * ip_mreq_source to avoid using two instances.
2081 mreqs
.imr_interface
= mreqs
.imr_sourceaddr
;
2082 mreqs
.imr_sourceaddr
.s_addr
= INADDR_ANY
;
2083 } else if (sopt
->sopt_name
== IP_ADD_SOURCE_MEMBERSHIP
) {
2084 error
= sooptcopyin(sopt
, &mreqs
,
2085 sizeof(struct ip_mreq_source
),
2086 sizeof(struct ip_mreq_source
));
2089 IGMP_PRINTF(("%s: error copyin IP_ADD_MEMBERSHIP/"
2090 "IP_ADD_SOURCE_MEMBERSHIP %d err=%d\n",
2091 __func__
, sopt
->sopt_name
, error
));
2095 gsa
->sin
.sin_family
= AF_INET
;
2096 gsa
->sin
.sin_len
= sizeof(struct sockaddr_in
);
2097 gsa
->sin
.sin_addr
= mreqs
.imr_multiaddr
;
2099 if (sopt
->sopt_name
== IP_ADD_SOURCE_MEMBERSHIP
) {
2100 ssa
->sin
.sin_family
= AF_INET
;
2101 ssa
->sin
.sin_len
= sizeof(struct sockaddr_in
);
2102 ssa
->sin
.sin_addr
= mreqs
.imr_sourceaddr
;
2105 if (!IN_MULTICAST(ntohl(gsa
->sin
.sin_addr
.s_addr
)))
2108 ifp
= inp_lookup_mcast_ifp(inp
, &gsa
->sin
,
2109 mreqs
.imr_interface
);
2110 IGMP_PRINTF(("%s: imr_interface = %s, ifp = %p\n",
2111 __func__
, inet_ntoa(mreqs
.imr_interface
), ifp
));
2115 case MCAST_JOIN_GROUP
:
2116 case MCAST_JOIN_SOURCE_GROUP
:
2117 if (sopt
->sopt_name
== MCAST_JOIN_GROUP
) {
2118 error
= sooptcopyin(sopt
, &gsr
,
2119 sizeof(struct group_req
),
2120 sizeof(struct group_req
));
2121 } else if (sopt
->sopt_name
== MCAST_JOIN_SOURCE_GROUP
) {
2122 error
= sooptcopyin(sopt
, &gsr
,
2123 sizeof(struct group_source_req
),
2124 sizeof(struct group_source_req
));
2129 if (gsa
->sin
.sin_family
!= AF_INET
||
2130 gsa
->sin
.sin_len
!= sizeof(struct sockaddr_in
))
2134 * Overwrite the port field if present, as the sockaddr
2135 * being copied in may be matched with a binary comparison.
2137 gsa
->sin
.sin_port
= 0;
2138 if (sopt
->sopt_name
== MCAST_JOIN_SOURCE_GROUP
) {
2139 if (ssa
->sin
.sin_family
!= AF_INET
||
2140 ssa
->sin
.sin_len
!= sizeof(struct sockaddr_in
))
2142 ssa
->sin
.sin_port
= 0;
2145 if (!IN_MULTICAST(ntohl(gsa
->sin
.sin_addr
.s_addr
)))
2148 ifnet_head_lock_shared();
2149 if (gsr
.gsr_interface
== 0 ||
2150 (u_int
)if_index
< gsr
.gsr_interface
) {
2152 return (EADDRNOTAVAIL
);
2154 ifp
= ifindex2ifnet
[gsr
.gsr_interface
];
2160 IGMP_PRINTF(("%s: unknown sopt_name %d\n",
2161 __func__
, sopt
->sopt_name
));
2162 return (EOPNOTSUPP
);
2166 if (ifp
== NULL
|| (ifp
->if_flags
& IFF_MULTICAST
) == 0)
2167 return (EADDRNOTAVAIL
);
2169 imo
= inp_findmoptions(inp
);
2174 idx
= imo_match_group(imo
, ifp
, &gsa
->sa
);
2175 if (idx
== (size_t)-1) {
2178 inm
= imo
->imo_membership
[idx
];
2179 imf
= &imo
->imo_mfilters
[idx
];
2180 if (ssa
->ss
.ss_family
!= AF_UNSPEC
) {
2182 * MCAST_JOIN_SOURCE_GROUP on an exclusive membership
2183 * is an error. On an existing inclusive membership,
2184 * it just adds the source to the filter list.
2186 if (imf
->imf_st
[1] != MCAST_INCLUDE
) {
2188 goto out_imo_locked
;
2191 * Throw out duplicates.
2193 * XXX FIXME: This makes a naive assumption that
2194 * even if entries exist for *ssa in this imf,
2195 * they will be rejected as dupes, even if they
2196 * are not valid in the current mode (in-mode).
2198 * in_msource is transactioned just as for anything
2199 * else in SSM -- but note naive use of inm_graft()
2200 * below for allocating new filter entries.
2202 * This is only an issue if someone mixes the
2203 * full-state SSM API with the delta-based API,
2204 * which is discouraged in the relevant RFCs.
2206 lims
= imo_match_source(imo
, idx
, &ssa
->sa
);
2207 if (lims
!= NULL
/*&&
2208 lims->imsl_st[1] == MCAST_INCLUDE*/) {
2209 error
= EADDRNOTAVAIL
;
2210 goto out_imo_locked
;
2214 * MCAST_JOIN_GROUP on an existing exclusive
2215 * membership is an error; return EADDRINUSE
2216 * to preserve 4.4BSD API idempotence, and
2217 * avoid tedious detour to code below.
2218 * NOTE: This is bending RFC 3678 a bit.
2220 * On an existing inclusive membership, this is also
2221 * an error; if you want to change filter mode,
2222 * you must use the userland API setsourcefilter().
2223 * XXX We don't reject this for imf in UNDEFINED
2224 * state at t1, because allocation of a filter
2225 * is atomic with allocation of a membership.
2228 /* See comments above for EADDRINUSE */
2229 if (imf
->imf_st
[1] == MCAST_EXCLUDE
)
2231 goto out_imo_locked
;
2236 * Begin state merge transaction at socket layer.
2240 if (imo
->imo_num_memberships
== imo
->imo_max_memberships
) {
2241 error
= imo_grow(imo
, 0);
2243 goto out_imo_locked
;
2246 * Allocate the new slot upfront so we can deal with
2247 * grafting the new source filter in same code path
2248 * as for join-source on existing membership.
2250 idx
= imo
->imo_num_memberships
;
2251 imo
->imo_membership
[idx
] = NULL
;
2252 imo
->imo_num_memberships
++;
2253 VERIFY(imo
->imo_mfilters
!= NULL
);
2254 imf
= &imo
->imo_mfilters
[idx
];
2255 VERIFY(RB_EMPTY(&imf
->imf_sources
));
2259 * Graft new source into filter list for this inpcb's
2260 * membership of the group. The in_multi may not have
2261 * been allocated yet if this is a new membership, however,
2262 * the in_mfilter slot will be allocated and must be initialized.
2264 if (ssa
->ss
.ss_family
!= AF_UNSPEC
) {
2265 /* Membership starts in IN mode */
2267 IGMP_PRINTF(("%s: new join w/source\n", __func__
));
2268 imf_init(imf
, MCAST_UNDEFINED
, MCAST_INCLUDE
);
2270 IGMP_PRINTF(("%s: %s source\n", __func__
, "allow"));
2272 lims
= imf_graft(imf
, MCAST_INCLUDE
, &ssa
->sin
);
2274 IGMP_PRINTF(("%s: merge imf state failed\n",
2280 /* No address specified; Membership starts in EX mode */
2282 IGMP_PRINTF(("%s: new join w/o source\n", __func__
));
2283 imf_init(imf
, MCAST_UNDEFINED
, MCAST_EXCLUDE
);
2288 * Begin state merge transaction at IGMP layer.
2292 VERIFY(inm
== NULL
);
2293 error
= in_joingroup(ifp
, &gsa
->sin
.sin_addr
, imf
, &inm
);
2294 VERIFY(inm
!= NULL
|| error
!= 0);
2297 imo
->imo_membership
[idx
] = inm
; /* from in_joingroup() */
2299 IGMP_PRINTF(("%s: merge inm state\n", __func__
));
2301 error
= inm_merge(inm
, imf
);
2303 IGMP_PRINTF(("%s: failed to merge inm state\n",
2306 goto out_imf_rollback
;
2308 IGMP_PRINTF(("%s: doing igmp downcall\n", __func__
));
2309 error
= igmp_change_state(inm
);
2312 IGMP_PRINTF(("%s: failed igmp downcall\n",
2314 goto out_imf_rollback
;
2330 if (error
&& is_new
) {
2331 VERIFY(inm
== NULL
);
2332 imo
->imo_membership
[idx
] = NULL
;
2333 --imo
->imo_num_memberships
;
2338 IMO_REMREF(imo
); /* from inp_findmoptions() */
2343 * Leave an IPv4 multicast group on an inpcb, possibly with a source.
2345 * NB: sopt->sopt_val might point to the kernel address space. Refer to the
2346 * block comment on top of inp_join_group() for more information.
2349 inp_leave_group(struct inpcb
*inp
, struct sockopt
*sopt
)
2351 struct group_source_req gsr
;
2352 struct ip_mreq_source mreqs
;
2353 sockunion_t
*gsa
, *ssa
;
2355 struct in_mfilter
*imf
;
2356 struct ip_moptions
*imo
;
2357 struct in_msource
*ims
;
2358 struct in_multi
*inm
= NULL
;
2360 int error
, is_final
;
2361 unsigned int ifindex
= 0;
2367 memset(&gsr
, 0, sizeof(struct group_source_req
));
2368 gsa
= (sockunion_t
*)&gsr
.gsr_group
;
2369 gsa
->ss
.ss_family
= AF_UNSPEC
;
2370 ssa
= (sockunion_t
*)&gsr
.gsr_source
;
2371 ssa
->ss
.ss_family
= AF_UNSPEC
;
2373 switch (sopt
->sopt_name
) {
2374 case IP_DROP_MEMBERSHIP
:
2375 case IP_DROP_SOURCE_MEMBERSHIP
:
2376 if (sopt
->sopt_name
== IP_DROP_MEMBERSHIP
) {
2377 error
= sooptcopyin(sopt
, &mreqs
,
2378 sizeof(struct ip_mreq
),
2379 sizeof(struct ip_mreq
));
2381 * Swap interface and sourceaddr arguments,
2382 * as ip_mreq and ip_mreq_source are laid
2385 mreqs
.imr_interface
= mreqs
.imr_sourceaddr
;
2386 mreqs
.imr_sourceaddr
.s_addr
= INADDR_ANY
;
2387 } else if (sopt
->sopt_name
== IP_DROP_SOURCE_MEMBERSHIP
) {
2388 error
= sooptcopyin(sopt
, &mreqs
,
2389 sizeof(struct ip_mreq_source
),
2390 sizeof(struct ip_mreq_source
));
2395 gsa
->sin
.sin_family
= AF_INET
;
2396 gsa
->sin
.sin_len
= sizeof(struct sockaddr_in
);
2397 gsa
->sin
.sin_addr
= mreqs
.imr_multiaddr
;
2399 if (sopt
->sopt_name
== IP_DROP_SOURCE_MEMBERSHIP
) {
2400 ssa
->sin
.sin_family
= AF_INET
;
2401 ssa
->sin
.sin_len
= sizeof(struct sockaddr_in
);
2402 ssa
->sin
.sin_addr
= mreqs
.imr_sourceaddr
;
2405 * Attempt to look up hinted ifp from interface address.
2406 * Fallthrough with null ifp iff lookup fails, to
2407 * preserve 4.4BSD mcast API idempotence.
2408 * XXX NOTE WELL: The RFC 3678 API is preferred because
2409 * using an IPv4 address as a key is racy.
2411 if (!in_nullhost(mreqs
.imr_interface
))
2412 ifp
= ip_multicast_if(&mreqs
.imr_interface
, &ifindex
);
2414 IGMP_PRINTF(("%s: imr_interface = %s, ifp = %p\n",
2415 __func__
, inet_ntoa(mreqs
.imr_interface
), ifp
));
2419 case MCAST_LEAVE_GROUP
:
2420 case MCAST_LEAVE_SOURCE_GROUP
:
2421 if (sopt
->sopt_name
== MCAST_LEAVE_GROUP
) {
2422 error
= sooptcopyin(sopt
, &gsr
,
2423 sizeof(struct group_req
),
2424 sizeof(struct group_req
));
2425 } else if (sopt
->sopt_name
== MCAST_LEAVE_SOURCE_GROUP
) {
2426 error
= sooptcopyin(sopt
, &gsr
,
2427 sizeof(struct group_source_req
),
2428 sizeof(struct group_source_req
));
2433 if (gsa
->sin
.sin_family
!= AF_INET
||
2434 gsa
->sin
.sin_len
!= sizeof(struct sockaddr_in
))
2437 if (sopt
->sopt_name
== MCAST_LEAVE_SOURCE_GROUP
) {
2438 if (ssa
->sin
.sin_family
!= AF_INET
||
2439 ssa
->sin
.sin_len
!= sizeof(struct sockaddr_in
))
2443 ifnet_head_lock_shared();
2444 if (gsr
.gsr_interface
== 0 ||
2445 (u_int
)if_index
< gsr
.gsr_interface
) {
2447 return (EADDRNOTAVAIL
);
2450 ifp
= ifindex2ifnet
[gsr
.gsr_interface
];
2455 IGMP_PRINTF(("%s: unknown sopt_name %d\n",
2456 __func__
, sopt
->sopt_name
));
2457 return (EOPNOTSUPP
);
2461 if (!IN_MULTICAST(ntohl(gsa
->sin
.sin_addr
.s_addr
)))
2465 * Find the membership in the membership array.
2467 imo
= inp_findmoptions(inp
);
2472 idx
= imo_match_group(imo
, ifp
, &gsa
->sa
);
2473 if (idx
== (size_t)-1) {
2474 error
= EADDRNOTAVAIL
;
2477 inm
= imo
->imo_membership
[idx
];
2478 imf
= &imo
->imo_mfilters
[idx
];
2480 if (ssa
->ss
.ss_family
!= AF_UNSPEC
) {
2481 IGMP_PRINTF(("%s: opt=%d is_final=0\n", __func__
,
2487 * Begin state merge transaction at socket layer.
2491 * If we were instructed only to leave a given source, do so.
2492 * MCAST_LEAVE_SOURCE_GROUP is only valid for inclusive memberships.
2497 if (imf
->imf_st
[0] == MCAST_EXCLUDE
) {
2498 error
= EADDRNOTAVAIL
;
2501 ims
= imo_match_source(imo
, idx
, &ssa
->sa
);
2503 IGMP_PRINTF(("%s: source %s %spresent\n", __func__
,
2504 inet_ntoa(ssa
->sin
.sin_addr
), "not "));
2505 error
= EADDRNOTAVAIL
;
2508 IGMP_PRINTF(("%s: %s source\n", __func__
, "block"));
2509 error
= imf_prune(imf
, &ssa
->sin
);
2511 IGMP_PRINTF(("%s: merge imf state failed\n",
2518 * Begin state merge transaction at IGMP layer.
2523 * Give up the multicast address record to which
2524 * the membership points. Reference held in imo
2525 * will be released below.
2527 (void) in_leavegroup(inm
, imf
);
2529 IGMP_PRINTF(("%s: merge inm state\n", __func__
));
2531 error
= inm_merge(inm
, imf
);
2533 IGMP_PRINTF(("%s: failed to merge inm state\n",
2536 goto out_imf_rollback
;
2539 IGMP_PRINTF(("%s: doing igmp downcall\n", __func__
));
2540 error
= igmp_change_state(inm
);
2542 IGMP_PRINTF(("%s: failed igmp downcall\n", __func__
));
2556 /* Remove the gap in the membership and filter array. */
2557 VERIFY(inm
== imo
->imo_membership
[idx
]);
2558 imo
->imo_membership
[idx
] = NULL
;
2560 for (++idx
; idx
< imo
->imo_num_memberships
; ++idx
) {
2561 imo
->imo_membership
[idx
-1] = imo
->imo_membership
[idx
];
2562 imo
->imo_mfilters
[idx
-1] = imo
->imo_mfilters
[idx
];
2564 imo
->imo_num_memberships
--;
2569 IMO_REMREF(imo
); /* from inp_findmoptions() */
2574 * Select the interface for transmitting IPv4 multicast datagrams.
2576 * Either an instance of struct in_addr or an instance of struct ip_mreqn
2577 * may be passed to this socket option. An address of INADDR_ANY or an
2578 * interface index of 0 is used to remove a previous selection.
2579 * When no interface is selected, one is chosen for every send.
2582 inp_set_multicast_if(struct inpcb
*inp
, struct sockopt
*sopt
)
2584 struct in_addr addr
;
2585 struct ip_mreqn mreqn
;
2587 struct ip_moptions
*imo
;
2589 unsigned int ifindex
= 0;
2591 if (sopt
->sopt_valsize
== sizeof(struct ip_mreqn
)) {
2593 * An interface index was specified using the
2594 * Linux-derived ip_mreqn structure.
2596 error
= sooptcopyin(sopt
, &mreqn
, sizeof(struct ip_mreqn
),
2597 sizeof(struct ip_mreqn
));
2601 ifnet_head_lock_shared();
2602 if (mreqn
.imr_ifindex
< 0 || if_index
< mreqn
.imr_ifindex
) {
2607 if (mreqn
.imr_ifindex
== 0) {
2610 ifp
= ifindex2ifnet
[mreqn
.imr_ifindex
];
2613 return (EADDRNOTAVAIL
);
2619 * An interface was specified by IPv4 address.
2620 * This is the traditional BSD usage.
2622 error
= sooptcopyin(sopt
, &addr
, sizeof(struct in_addr
),
2623 sizeof(struct in_addr
));
2626 if (in_nullhost(addr
)) {
2629 ifp
= ip_multicast_if(&addr
, &ifindex
);
2631 IGMP_PRINTF(("%s: can't find ifp for addr=%s\n",
2632 __func__
, inet_ntoa(addr
)));
2633 return (EADDRNOTAVAIL
);
2637 IGMP_PRINTF(("%s: ifp = %p, addr = %s\n", __func__
, ifp
,
2642 /* Reject interfaces which do not support multicast. */
2643 if (ifp
!= NULL
&& (ifp
->if_flags
& IFF_MULTICAST
) == 0)
2644 return (EOPNOTSUPP
);
2646 imo
= inp_findmoptions(inp
);
2651 imo
->imo_multicast_ifp
= ifp
;
2653 imo
->imo_multicast_addr
= addr
;
2655 imo
->imo_multicast_addr
.s_addr
= INADDR_ANY
;
2657 IMO_REMREF(imo
); /* from inp_findmoptions() */
2663 * Atomically set source filters on a socket for an IPv4 multicast group.
2666 inp_set_source_filters(struct inpcb
*inp
, struct sockopt
*sopt
)
2668 struct __msfilterreq64 msfr
, msfr64
;
2669 struct __msfilterreq32 msfr32
;
2672 struct in_mfilter
*imf
;
2673 struct ip_moptions
*imo
;
2674 struct in_multi
*inm
;
2677 user_addr_t tmp_ptr
;
2679 if (IS_64BIT_PROCESS(current_proc())) {
2680 error
= sooptcopyin(sopt
, &msfr64
,
2681 sizeof(struct __msfilterreq64
),
2682 sizeof(struct __msfilterreq64
));
2685 /* we never use msfr.msfr_srcs; */
2686 memcpy(&msfr
, &msfr64
, sizeof(msfr
));
2688 error
= sooptcopyin(sopt
, &msfr32
,
2689 sizeof(struct __msfilterreq32
),
2690 sizeof(struct __msfilterreq32
));
2693 /* we never use msfr.msfr_srcs; */
2694 memcpy(&msfr
, &msfr32
, sizeof(msfr
));
2697 if ((size_t) msfr
.msfr_nsrcs
>
2698 SIZE_MAX
/ sizeof(struct sockaddr_storage
))
2699 msfr
.msfr_nsrcs
= SIZE_MAX
/ sizeof(struct sockaddr_storage
);
2701 if (msfr
.msfr_nsrcs
> in_mcast_maxsocksrc
)
2704 if ((msfr
.msfr_fmode
!= MCAST_EXCLUDE
&&
2705 msfr
.msfr_fmode
!= MCAST_INCLUDE
))
2708 if (msfr
.msfr_group
.ss_family
!= AF_INET
||
2709 msfr
.msfr_group
.ss_len
!= sizeof(struct sockaddr_in
))
2712 gsa
= (sockunion_t
*)&msfr
.msfr_group
;
2713 if (!IN_MULTICAST(ntohl(gsa
->sin
.sin_addr
.s_addr
)))
2716 gsa
->sin
.sin_port
= 0; /* ignore port */
2718 ifnet_head_lock_shared();
2719 if (msfr
.msfr_ifindex
== 0 || (u_int
)if_index
< msfr
.msfr_ifindex
) {
2721 return (EADDRNOTAVAIL
);
2724 ifp
= ifindex2ifnet
[msfr
.msfr_ifindex
];
2727 return (EADDRNOTAVAIL
);
2730 * Check if this socket is a member of this group.
2732 imo
= inp_findmoptions(inp
);
2737 idx
= imo_match_group(imo
, ifp
, &gsa
->sa
);
2738 if (idx
== (size_t)-1 || imo
->imo_mfilters
== NULL
) {
2739 error
= EADDRNOTAVAIL
;
2740 goto out_imo_locked
;
2742 inm
= imo
->imo_membership
[idx
];
2743 imf
= &imo
->imo_mfilters
[idx
];
2746 * Begin state merge transaction at socket layer.
2749 imf
->imf_st
[1] = msfr
.msfr_fmode
;
2752 * Apply any new source filters, if present.
2753 * Make a copy of the user-space source vector so
2754 * that we may copy them with a single copyin. This
2755 * allows us to deal with page faults up-front.
2757 if (msfr
.msfr_nsrcs
> 0) {
2758 struct in_msource
*lims
;
2759 struct sockaddr_in
*psin
;
2760 struct sockaddr_storage
*kss
, *pkss
;
2763 if (IS_64BIT_PROCESS(current_proc()))
2764 tmp_ptr
= msfr64
.msfr_srcs
;
2766 tmp_ptr
= CAST_USER_ADDR_T(msfr32
.msfr_srcs
);
2768 IGMP_PRINTF(("%s: loading %lu source list entries\n",
2769 __func__
, (unsigned long)msfr
.msfr_nsrcs
));
2770 kss
= _MALLOC((size_t) msfr
.msfr_nsrcs
* sizeof(*kss
),
2774 goto out_imo_locked
;
2776 error
= copyin(tmp_ptr
, kss
,
2777 (size_t) msfr
.msfr_nsrcs
* sizeof(*kss
));
2780 goto out_imo_locked
;
2784 * Mark all source filters as UNDEFINED at t1.
2785 * Restore new group filter mode, as imf_leave()
2786 * will set it to INCLUDE.
2789 imf
->imf_st
[1] = msfr
.msfr_fmode
;
2792 * Update socket layer filters at t1, lazy-allocating
2793 * new entries. This saves a bunch of memory at the
2794 * cost of one RB_FIND() per source entry; duplicate
2795 * entries in the msfr_nsrcs vector are ignored.
2796 * If we encounter an error, rollback transaction.
2798 * XXX This too could be replaced with a set-symmetric
2799 * difference like loop to avoid walking from root
2800 * every time, as the key space is common.
2802 for (i
= 0, pkss
= kss
; (u_int
)i
< msfr
.msfr_nsrcs
;
2804 psin
= (struct sockaddr_in
*)pkss
;
2805 if (psin
->sin_family
!= AF_INET
) {
2806 error
= EAFNOSUPPORT
;
2809 if (psin
->sin_len
!= sizeof(struct sockaddr_in
)) {
2813 error
= imf_get_source(imf
, psin
, &lims
);
2816 lims
->imsl_st
[1] = imf
->imf_st
[1];
2822 goto out_imf_rollback
;
2825 * Begin state merge transaction at IGMP layer.
2828 IGMP_PRINTF(("%s: merge inm state\n", __func__
));
2829 error
= inm_merge(inm
, imf
);
2831 IGMP_PRINTF(("%s: failed to merge inm state\n", __func__
));
2833 goto out_imf_rollback
;
2836 IGMP_PRINTF(("%s: doing igmp downcall\n", __func__
));
2837 error
= igmp_change_state(inm
);
2841 IGMP_PRINTF(("%s: failed igmp downcall\n", __func__
));
2854 IMO_REMREF(imo
); /* from inp_findmoptions() */
2860 * Set the IP multicast options in response to user setsockopt().
2862 * Many of the socket options handled in this function duplicate the
2863 * functionality of socket options in the regular unicast API. However,
2864 * it is not possible to merge the duplicate code, because the idempotence
2865 * of the IPv4 multicast part of the BSD Sockets API must be preserved;
2866 * the effects of these options must be treated as separate and distinct.
2868 * FUTURE: The IP_MULTICAST_VIF option may be eliminated if MROUTING
2869 * is refactored to no longer use vifs.
2872 inp_setmoptions(struct inpcb
*inp
, struct sockopt
*sopt
)
2874 struct ip_moptions
*imo
;
2876 unsigned int ifindex
;
2882 * If socket is neither of type SOCK_RAW or SOCK_DGRAM,
2883 * or is a divert socket, reject it.
2885 if (inp
->inp_socket
->so_proto
->pr_protocol
== IPPROTO_DIVERT
||
2886 (inp
->inp_socket
->so_proto
->pr_type
!= SOCK_RAW
&&
2887 inp
->inp_socket
->so_proto
->pr_type
!= SOCK_DGRAM
))
2888 return (EOPNOTSUPP
);
2890 switch (sopt
->sopt_name
) {
2892 case IP_MULTICAST_VIF
: {
2895 * Select a multicast VIF for transmission.
2896 * Only useful if multicast forwarding is active.
2898 if (legal_vif_num
== NULL
) {
2902 error
= sooptcopyin(sopt
, &vifi
, sizeof(int), sizeof(int));
2905 if (!legal_vif_num(vifi
) && (vifi
!= -1)) {
2909 imo
= inp_findmoptions(inp
);
2915 imo
->imo_multicast_vif
= vifi
;
2917 IMO_REMREF(imo
); /* from inp_findmoptions() */
2921 case IP_MULTICAST_IF
:
2922 error
= inp_set_multicast_if(inp
, sopt
);
2925 case IP_MULTICAST_IFINDEX
:
2927 * Select the interface for outgoing multicast packets.
2929 error
= sooptcopyin(sopt
, &ifindex
, sizeof (ifindex
),
2934 imo
= inp_findmoptions(inp
);
2940 * Index 0 is used to remove a previous selection.
2941 * When no interface is selected, a default one is
2942 * chosen every time a multicast packet is sent.
2946 imo
->imo_multicast_ifp
= NULL
;
2948 IMO_REMREF(imo
); /* from inp_findmoptions() */
2952 ifnet_head_lock_shared();
2953 /* Don't need to check is ifindex is < 0 since it's unsigned */
2954 if ((unsigned int)if_index
< ifindex
) {
2956 IMO_REMREF(imo
); /* from inp_findmoptions() */
2957 error
= ENXIO
; /* per IPV6_MULTICAST_IF */
2960 ifp
= ifindex2ifnet
[ifindex
];
2963 /* If it's detached or isn't a multicast interface, bail out */
2964 if (ifp
== NULL
|| !(ifp
->if_flags
& IFF_MULTICAST
)) {
2965 IMO_REMREF(imo
); /* from inp_findmoptions() */
2966 error
= EADDRNOTAVAIL
;
2970 imo
->imo_multicast_ifp
= ifp
;
2972 * Clear out any remnants of past IP_MULTICAST_IF. The addr
2973 * isn't really used anywhere in the kernel; we could have
2974 * iterated thru the addresses of the interface and pick one
2975 * here, but that is redundant since ip_getmoptions() already
2976 * takes care of that for INADDR_ANY.
2978 imo
->imo_multicast_addr
.s_addr
= INADDR_ANY
;
2980 IMO_REMREF(imo
); /* from inp_findmoptions() */
2983 case IP_MULTICAST_TTL
: {
2987 * Set the IP time-to-live for outgoing multicast packets.
2988 * The original multicast API required a char argument,
2989 * which is inconsistent with the rest of the socket API.
2990 * We allow either a char or an int.
2992 if (sopt
->sopt_valsize
== sizeof(u_char
)) {
2993 error
= sooptcopyin(sopt
, &ttl
, sizeof(u_char
),
3000 error
= sooptcopyin(sopt
, &ittl
, sizeof(u_int
),
3010 imo
= inp_findmoptions(inp
);
3016 imo
->imo_multicast_ttl
= ttl
;
3018 IMO_REMREF(imo
); /* from inp_findmoptions() */
3022 case IP_MULTICAST_LOOP
: {
3026 * Set the loopback flag for outgoing multicast packets.
3027 * Must be zero or one. The original multicast API required a
3028 * char argument, which is inconsistent with the rest
3029 * of the socket API. We allow either a char or an int.
3031 if (sopt
->sopt_valsize
== sizeof(u_char
)) {
3032 error
= sooptcopyin(sopt
, &loop
, sizeof(u_char
),
3039 error
= sooptcopyin(sopt
, &iloop
, sizeof(u_int
),
3043 loop
= (u_char
)iloop
;
3045 imo
= inp_findmoptions(inp
);
3051 imo
->imo_multicast_loop
= !!loop
;
3053 IMO_REMREF(imo
); /* from inp_findmoptions() */
3057 case IP_ADD_MEMBERSHIP
:
3058 case IP_ADD_SOURCE_MEMBERSHIP
:
3059 case MCAST_JOIN_GROUP
:
3060 case MCAST_JOIN_SOURCE_GROUP
:
3061 error
= inp_join_group(inp
, sopt
);
3064 case IP_DROP_MEMBERSHIP
:
3065 case IP_DROP_SOURCE_MEMBERSHIP
:
3066 case MCAST_LEAVE_GROUP
:
3067 case MCAST_LEAVE_SOURCE_GROUP
:
3068 error
= inp_leave_group(inp
, sopt
);
3071 case IP_BLOCK_SOURCE
:
3072 case IP_UNBLOCK_SOURCE
:
3073 case MCAST_BLOCK_SOURCE
:
3074 case MCAST_UNBLOCK_SOURCE
:
3075 error
= inp_block_unblock_source(inp
, sopt
);
3079 error
= inp_set_source_filters(inp
, sopt
);
3091 * Expose IGMP's multicast filter mode and source list(s) to userland,
3092 * keyed by (ifindex, group).
3093 * The filter mode is written out as a uint32_t, followed by
3094 * 0..n of struct in_addr.
3095 * For use by ifmcstat(8).
3098 sysctl_ip_mcast_filters SYSCTL_HANDLER_ARGS
3100 #pragma unused(oidp)
3102 struct in_addr src
, group
;
3104 struct in_multi
*inm
;
3105 struct in_multistep step
;
3106 struct ip_msource
*ims
;
3110 uint32_t fmode
, ifindex
;
3113 namelen
= (u_int
)arg2
;
3115 if (req
->newptr
!= USER_ADDR_NULL
)
3122 ifnet_head_lock_shared();
3123 if (ifindex
<= 0 || ifindex
> (u_int
)if_index
) {
3124 IGMP_PRINTF(("%s: ifindex %u out of range\n",
3125 __func__
, ifindex
));
3130 group
.s_addr
= name
[1];
3131 if (!IN_MULTICAST(ntohl(group
.s_addr
))) {
3132 IGMP_PRINTF(("%s: group %s is not multicast\n",
3133 __func__
, inet_ntoa(group
)));
3138 ifp
= ifindex2ifnet
[ifindex
];
3141 IGMP_PRINTF(("%s: no ifp for ifindex %u\n", __func__
, ifindex
));
3145 in_multihead_lock_shared();
3146 IN_FIRST_MULTI(step
, inm
);
3147 while (inm
!= NULL
) {
3149 if (inm
->inm_ifp
!= ifp
)
3152 if (!in_hosteq(inm
->inm_addr
, group
))
3155 fmode
= inm
->inm_st
[1].iss_fmode
;
3156 retval
= SYSCTL_OUT(req
, &fmode
, sizeof(uint32_t));
3161 RB_FOREACH(ims
, ip_msource_tree
, &inm
->inm_srcs
) {
3164 ina
.s_addr
= htonl(ims
->ims_haddr
);
3165 IGMP_PRINTF(("%s: visit node %s\n", __func__
,
3169 * Only copy-out sources which are in-mode.
3171 if (fmode
!= ims_get_mode(inm
, ims
, 1)) {
3172 IGMP_PRINTF(("%s: skip non-in-mode\n",
3174 continue; /* process next source */
3176 src
.s_addr
= htonl(ims
->ims_haddr
);
3177 retval
= SYSCTL_OUT(req
, &src
, sizeof(struct in_addr
));
3179 break; /* process next inm */
3183 IN_NEXT_MULTI(step
, inm
);
3185 in_multihead_lock_done();
3192 * The whole multicast option thing needs to be re-thought.
3193 * Several of these options are equally applicable to non-multicast
3194 * transmission, and one (IP_MULTICAST_TTL) totally duplicates a
3195 * standard option (IP_TTL).
3198 * following RFC1724 section 3.3, 0.0.0.0/8 is interpreted as interface index.
3200 static struct ifnet
*
3201 ip_multicast_if(struct in_addr
*a
, unsigned int *ifindexp
)
3203 unsigned int ifindex
;
3206 if (ifindexp
!= NULL
)
3208 if (ntohl(a
->s_addr
) >> 24 == 0) {
3209 ifindex
= ntohl(a
->s_addr
) & 0xffffff;
3210 ifnet_head_lock_shared();
3211 /* Don't need to check is ifindex is < 0 since it's unsigned */
3212 if ((unsigned int)if_index
< ifindex
) {
3216 ifp
= ifindex2ifnet
[ifindex
];
3218 if (ifp
!= NULL
&& ifindexp
!= NULL
)
3219 *ifindexp
= ifindex
;
3221 INADDR_TO_IFP(*a
, ifp
);
3229 PE_parse_boot_argn("ifa_debug", &inm_debug
, sizeof (inm_debug
));
3231 /* Setup lock group and attribute for in_multihead */
3232 in_multihead_lock_grp_attr
= lck_grp_attr_alloc_init();
3233 in_multihead_lock_grp
= lck_grp_alloc_init("in_multihead",
3234 in_multihead_lock_grp_attr
);
3235 in_multihead_lock_attr
= lck_attr_alloc_init();
3236 lck_rw_init(&in_multihead_lock
, in_multihead_lock_grp
,
3237 in_multihead_lock_attr
);
3239 lck_mtx_init(&inm_trash_lock
, in_multihead_lock_grp
,
3240 in_multihead_lock_attr
);
3241 TAILQ_INIT(&inm_trash_head
);
3243 inm_size
= (inm_debug
== 0) ? sizeof (struct in_multi
) :
3244 sizeof (struct in_multi_dbg
);
3245 inm_zone
= zinit(inm_size
, INM_ZONE_MAX
* inm_size
,
3247 if (inm_zone
== NULL
) {
3248 panic("%s: failed allocating %s", __func__
, INM_ZONE_NAME
);
3251 zone_change(inm_zone
, Z_EXPAND
, TRUE
);
3253 ipms_size
= sizeof (struct ip_msource
);
3254 ipms_zone
= zinit(ipms_size
, IPMS_ZONE_MAX
* ipms_size
,
3256 if (ipms_zone
== NULL
) {
3257 panic("%s: failed allocating %s", __func__
, IPMS_ZONE_NAME
);
3260 zone_change(ipms_zone
, Z_EXPAND
, TRUE
);
3262 inms_size
= sizeof (struct in_msource
);
3263 inms_zone
= zinit(inms_size
, INMS_ZONE_MAX
* inms_size
,
3265 if (inms_zone
== NULL
) {
3266 panic("%s: failed allocating %s", __func__
, INMS_ZONE_NAME
);
3269 zone_change(inms_zone
, Z_EXPAND
, TRUE
);
3272 static struct in_multi
*
3273 in_multi_alloc(int how
)
3275 struct in_multi
*inm
;
3277 inm
= (how
== M_WAITOK
) ? zalloc(inm_zone
) : zalloc_noblock(inm_zone
);
3279 bzero(inm
, inm_size
);
3280 lck_mtx_init(&inm
->inm_lock
, in_multihead_lock_grp
,
3281 in_multihead_lock_attr
);
3282 inm
->inm_debug
|= IFD_ALLOC
;
3283 if (inm_debug
!= 0) {
3284 inm
->inm_debug
|= IFD_DEBUG
;
3285 inm
->inm_trace
= inm_trace
;
3292 in_multi_free(struct in_multi
*inm
)
3295 if (inm
->inm_debug
& IFD_ATTACHED
) {
3296 panic("%s: attached inm=%p is being freed", __func__
, inm
);
3298 } else if (inm
->inm_ifma
!= NULL
) {
3299 panic("%s: ifma not NULL for inm=%p", __func__
, inm
);
3301 } else if (!(inm
->inm_debug
& IFD_ALLOC
)) {
3302 panic("%s: inm %p cannot be freed", __func__
, inm
);
3304 } else if (inm
->inm_refcount
!= 0) {
3305 panic("%s: non-zero refcount inm=%p", __func__
, inm
);
3307 } else if (inm
->inm_reqcnt
!= 0) {
3308 panic("%s: non-zero reqcnt inm=%p", __func__
, inm
);
3312 /* Free any pending IGMPv3 state-change records */
3313 IF_DRAIN(&inm
->inm_scq
);
3315 inm
->inm_debug
&= ~IFD_ALLOC
;
3316 if ((inm
->inm_debug
& (IFD_DEBUG
| IFD_TRASHED
)) ==
3317 (IFD_DEBUG
| IFD_TRASHED
)) {
3318 lck_mtx_lock(&inm_trash_lock
);
3319 TAILQ_REMOVE(&inm_trash_head
, (struct in_multi_dbg
*)inm
,
3321 lck_mtx_unlock(&inm_trash_lock
);
3322 inm
->inm_debug
&= ~IFD_TRASHED
;
3326 lck_mtx_destroy(&inm
->inm_lock
, in_multihead_lock_grp
);
3327 zfree(inm_zone
, inm
);
3331 in_multi_attach(struct in_multi
*inm
)
3333 in_multihead_lock_assert(LCK_RW_ASSERT_EXCLUSIVE
);
3334 INM_LOCK_ASSERT_HELD(inm
);
3336 if (inm
->inm_debug
& IFD_ATTACHED
) {
3337 panic("%s: Attempt to attach an already attached inm=%p",
3340 } else if (inm
->inm_debug
& IFD_TRASHED
) {
3341 panic("%s: Attempt to reattach a detached inm=%p",
3347 VERIFY(inm
->inm_reqcnt
== 1);
3348 INM_ADDREF_LOCKED(inm
);
3349 inm
->inm_debug
|= IFD_ATTACHED
;
3351 * Reattach case: If debugging is enabled, take it
3352 * out of the trash list and clear IFD_TRASHED.
3354 if ((inm
->inm_debug
& (IFD_DEBUG
| IFD_TRASHED
)) ==
3355 (IFD_DEBUG
| IFD_TRASHED
)) {
3356 /* Become a regular mutex, just in case */
3357 INM_CONVERT_LOCK(inm
);
3358 lck_mtx_lock(&inm_trash_lock
);
3359 TAILQ_REMOVE(&inm_trash_head
, (struct in_multi_dbg
*)inm
,
3361 lck_mtx_unlock(&inm_trash_lock
);
3362 inm
->inm_debug
&= ~IFD_TRASHED
;
3365 LIST_INSERT_HEAD(&in_multihead
, inm
, inm_link
);
3369 in_multi_detach(struct in_multi
*inm
)
3371 in_multihead_lock_assert(LCK_RW_ASSERT_EXCLUSIVE
);
3372 INM_LOCK_ASSERT_HELD(inm
);
3374 if (inm
->inm_reqcnt
== 0) {
3375 panic("%s: inm=%p negative reqcnt", __func__
, inm
);
3380 if (inm
->inm_reqcnt
> 0)
3383 if (!(inm
->inm_debug
& IFD_ATTACHED
)) {
3384 panic("%s: Attempt to detach an unattached record inm=%p",
3387 } else if (inm
->inm_debug
& IFD_TRASHED
) {
3388 panic("%s: inm %p is already in trash list", __func__
, inm
);
3393 * NOTE: Caller calls IFMA_REMREF
3395 inm
->inm_debug
&= ~IFD_ATTACHED
;
3396 LIST_REMOVE(inm
, inm_link
);
3398 if (inm
->inm_debug
& IFD_DEBUG
) {
3399 /* Become a regular mutex, just in case */
3400 INM_CONVERT_LOCK(inm
);
3401 lck_mtx_lock(&inm_trash_lock
);
3402 TAILQ_INSERT_TAIL(&inm_trash_head
,
3403 (struct in_multi_dbg
*)inm
, inm_trash_link
);
3404 lck_mtx_unlock(&inm_trash_lock
);
3405 inm
->inm_debug
|= IFD_TRASHED
;
3412 inm_addref(struct in_multi
*inm
, int locked
)
3417 INM_LOCK_ASSERT_HELD(inm
);
3419 if (++inm
->inm_refcount
== 0) {
3420 panic("%s: inm=%p wraparound refcnt", __func__
, inm
);
3422 } else if (inm
->inm_trace
!= NULL
) {
3423 (*inm
->inm_trace
)(inm
, TRUE
);
3430 inm_remref(struct in_multi
*inm
, int locked
)
3432 struct ifmultiaddr
*ifma
;
3433 struct igmp_ifinfo
*igi
;
3438 INM_LOCK_ASSERT_HELD(inm
);
3440 if (inm
->inm_refcount
== 0 || (inm
->inm_refcount
== 1 && locked
)) {
3441 panic("%s: inm=%p negative/missing refcnt", __func__
, inm
);
3443 } else if (inm
->inm_trace
!= NULL
) {
3444 (*inm
->inm_trace
)(inm
, FALSE
);
3447 --inm
->inm_refcount
;
3448 if (inm
->inm_refcount
> 0) {
3455 * Synchronization with in_getmulti(). In the event the inm has been
3456 * detached, the underlying ifma would still be in the if_multiaddrs
3457 * list, and thus can be looked up via if_addmulti(). At that point,
3458 * the only way to find this inm is via ifma_protospec. To avoid
3459 * race conditions between the last inm_remref() of that inm and its
3460 * use via ifma_protospec, in_multihead lock is used for serialization.
3461 * In order to avoid violating the lock order, we must drop inm_lock
3462 * before acquiring in_multihead lock. To prevent the inm from being
3463 * freed prematurely, we hold an extra reference.
3465 ++inm
->inm_refcount
;
3467 in_multihead_lock_shared();
3469 --inm
->inm_refcount
;
3470 if (inm
->inm_refcount
> 0) {
3471 /* We've lost the race, so abort since inm is still in use */
3473 in_multihead_lock_done();
3474 /* If it was locked, return it as such */
3480 ifma
= inm
->inm_ifma
;
3481 inm
->inm_ifma
= NULL
;
3482 inm
->inm_ifp
= NULL
;
3484 inm
->inm_igi
= NULL
;
3486 IFMA_LOCK_SPIN(ifma
);
3487 ifma
->ifma_protospec
= NULL
;
3489 in_multihead_lock_done();
3492 if_delmulti_ifma(ifma
);
3493 /* Release reference held to the underlying ifmultiaddr */
3501 inm_trace(struct in_multi
*inm
, int refhold
)
3503 struct in_multi_dbg
*inm_dbg
= (struct in_multi_dbg
*)inm
;
3508 if (!(inm
->inm_debug
& IFD_DEBUG
)) {
3509 panic("%s: inm %p has no debug structure", __func__
, inm
);
3513 cnt
= &inm_dbg
->inm_refhold_cnt
;
3514 tr
= inm_dbg
->inm_refhold
;
3516 cnt
= &inm_dbg
->inm_refrele_cnt
;
3517 tr
= inm_dbg
->inm_refrele
;
3520 idx
= atomic_add_16_ov(cnt
, 1) % INM_TRACE_HIST_SIZE
;
3521 ctrace_record(&tr
[idx
]);
3525 in_multihead_lock_exclusive(void)
3527 lck_rw_lock_exclusive(&in_multihead_lock
);
3531 in_multihead_lock_shared(void)
3533 lck_rw_lock_shared(&in_multihead_lock
);
3537 in_multihead_lock_assert(int what
)
3539 lck_rw_assert(&in_multihead_lock
, what
);
3543 in_multihead_lock_done(void)
3545 lck_rw_done(&in_multihead_lock
);
3548 static struct ip_msource
*
3551 struct ip_msource
*ims
;
3553 ims
= (how
== M_WAITOK
) ? zalloc(ipms_zone
) : zalloc_noblock(ipms_zone
);
3555 bzero(ims
, ipms_size
);
3561 ipms_free(struct ip_msource
*ims
)
3563 zfree(ipms_zone
, ims
);
3566 static struct in_msource
*
3569 struct in_msource
*inms
;
3571 inms
= (how
== M_WAITOK
) ? zalloc(inms_zone
) :
3572 zalloc_noblock(inms_zone
);
3574 bzero(inms
, inms_size
);
3580 inms_free(struct in_msource
*inms
)
3582 zfree(inms_zone
, inms
);
3587 static const char *inm_modestrs
[] = { "un\n", "in", "ex" };
3590 inm_mode_str(const int mode
)
3592 if (mode
>= MCAST_UNDEFINED
&& mode
<= MCAST_EXCLUDE
)
3593 return (inm_modestrs
[mode
]);
3597 static const char *inm_statestrs
[] = {
3605 "sg-query-pending\n",
3610 inm_state_str(const int state
)
3612 if (state
>= IGMP_NOT_MEMBER
&& state
<= IGMP_LEAVING_MEMBER
)
3613 return (inm_statestrs
[state
]);
3618 * Dump an in_multi structure to the console.
3621 inm_print(const struct in_multi
*inm
)
3625 INM_LOCK_ASSERT_HELD(INM_CAST_TO_NONCONST(inm
));
3627 if (igmp_debug
== 0)
3630 printf("%s: --- begin inm %p ---\n", __func__
, inm
);
3631 printf("addr %s ifp %p(%s%d) ifma %p\n",
3632 inet_ntoa(inm
->inm_addr
),
3634 inm
->inm_ifp
->if_name
,
3635 inm
->inm_ifp
->if_unit
,
3637 printf("timer %u state %s refcount %u scq.len %u\n",
3639 inm_state_str(inm
->inm_state
),
3641 inm
->inm_scq
.ifq_len
);
3642 printf("igi %p nsrc %lu sctimer %u scrv %u\n",
3647 for (t
= 0; t
< 2; t
++) {
3648 printf("t%d: fmode %s asm %u ex %u in %u rec %u\n", t
,
3649 inm_mode_str(inm
->inm_st
[t
].iss_fmode
),
3650 inm
->inm_st
[t
].iss_asm
,
3651 inm
->inm_st
[t
].iss_ex
,
3652 inm
->inm_st
[t
].iss_in
,
3653 inm
->inm_st
[t
].iss_rec
);
3655 printf("%s: --- end inm %p ---\n", __func__
, inm
);
3661 inm_print(__unused
const struct in_multi
*inm
)