]>
Commit | Line | Data |
---|---|---|
6d2010ae | 1 | /* |
f427ee49 | 2 | * Copyright (c) 2010-2020 Apple Inc. All rights reserved. |
6d2010ae A |
3 | * |
4 | * @APPLE_OSREFERENCE_LICENSE_HEADER_START@ | |
0a7de745 | 5 | * |
6d2010ae A |
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. | |
0a7de745 | 14 | * |
6d2010ae A |
15 | * Please obtain a copy of the License at |
16 | * http://www.opensource.apple.com/apsl/ and read it before using this file. | |
0a7de745 | 17 | * |
6d2010ae A |
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. | |
0a7de745 | 25 | * |
6d2010ae A |
26 | * @APPLE_OSREFERENCE_LICENSE_HEADER_END@ |
27 | */ | |
28 | /*- | |
29 | * Copyright (c) 2007-2009 Bruce Simpson. | |
30 | * Copyright (c) 2005 Robert N. M. Watson. | |
31 | * All rights reserved. | |
32 | * | |
33 | * Redistribution and use in source and binary forms, with or without | |
34 | * modification, are permitted provided that the following conditions | |
35 | * are met: | |
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 | |
43 | * permission. | |
44 | * | |
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 | |
55 | * SUCH DAMAGE. | |
56 | */ | |
57 | ||
58 | /* | |
59 | * IPv4 multicast socket, group, and socket option processing module. | |
60 | */ | |
61 | ||
62 | #include <sys/cdefs.h> | |
63 | ||
64 | #include <sys/param.h> | |
65 | #include <sys/systm.h> | |
66 | #include <sys/kernel.h> | |
67 | #include <sys/malloc.h> | |
68 | #include <sys/mbuf.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> | |
74 | #include <sys/tree.h> | |
75 | #include <sys/mcache.h> | |
76 | ||
77 | #include <kern/zalloc.h> | |
78 | ||
79 | #include <pexpert/pexpert.h> | |
80 | ||
81 | #include <net/if.h> | |
82 | #include <net/if_dl.h> | |
5ba3f43e | 83 | #include <net/net_api_stats.h> |
6d2010ae A |
84 | #include <net/route.h> |
85 | ||
86 | #include <netinet/in.h> | |
87 | #include <netinet/in_systm.h> | |
88 | #include <netinet/in_pcb.h> | |
89 | #include <netinet/in_var.h> | |
90 | #include <netinet/ip_var.h> | |
91 | #include <netinet/igmp_var.h> | |
92 | ||
6d2010ae A |
93 | /* |
94 | * Functions with non-static linkage defined in this file should be | |
95 | * declared in in_var.h: | |
96 | * imo_multi_filter() | |
97 | * in_addmulti() | |
98 | * in_delmulti() | |
99 | * in_joingroup() | |
100 | * in_leavegroup() | |
101 | * and ip_var.h: | |
102 | * inp_freemoptions() | |
103 | * inp_getmoptions() | |
104 | * inp_setmoptions() | |
105 | * | |
106 | * XXX: Both carp and pf need to use the legacy (*,G) KPIs in_addmulti() | |
107 | * and in_delmulti(). | |
108 | */ | |
0a7de745 A |
109 | static void imf_commit(struct in_mfilter *); |
110 | static int imf_get_source(struct in_mfilter *imf, | |
111 | const struct sockaddr_in *psin, | |
112 | struct in_msource **); | |
6d2010ae | 113 | static struct in_msource * |
0a7de745 A |
114 | imf_graft(struct in_mfilter *, const uint8_t, |
115 | const struct sockaddr_in *); | |
116 | static int imf_prune(struct in_mfilter *, const struct sockaddr_in *); | |
117 | static void imf_rollback(struct in_mfilter *); | |
118 | static void imf_reap(struct in_mfilter *); | |
f427ee49 | 119 | static int imo_grow(struct ip_moptions *, uint16_t); |
0a7de745 A |
120 | static size_t imo_match_group(const struct ip_moptions *, |
121 | const struct ifnet *, const struct sockaddr_in *); | |
6d2010ae | 122 | static struct in_msource * |
0a7de745 A |
123 | imo_match_source(const struct ip_moptions *, const size_t, |
124 | const struct sockaddr_in *); | |
125 | static void ims_merge(struct ip_msource *ims, | |
126 | const struct in_msource *lims, const int rollback); | |
127 | static int in_getmulti(struct ifnet *, const struct in_addr *, | |
128 | struct in_multi **); | |
129 | static int in_joingroup(struct ifnet *, const struct in_addr *, | |
130 | struct in_mfilter *, struct in_multi **); | |
131 | static int inm_get_source(struct in_multi *inm, const in_addr_t haddr, | |
132 | const int noalloc, struct ip_msource **pims); | |
133 | static int inm_is_ifp_detached(const struct in_multi *); | |
134 | static int inm_merge(struct in_multi *, /*const*/ struct in_mfilter *); | |
135 | static void inm_reap(struct in_multi *); | |
6d2010ae | 136 | static struct ip_moptions * |
0a7de745 A |
137 | inp_findmoptions(struct inpcb *); |
138 | static int inp_get_source_filters(struct inpcb *, struct sockopt *); | |
6d2010ae | 139 | static struct ifnet * |
0a7de745 A |
140 | inp_lookup_mcast_ifp(const struct inpcb *, |
141 | const struct sockaddr_in *, const struct in_addr); | |
142 | static int inp_block_unblock_source(struct inpcb *, struct sockopt *); | |
143 | static int inp_set_multicast_if(struct inpcb *, struct sockopt *); | |
144 | static int inp_set_source_filters(struct inpcb *, struct sockopt *); | |
145 | static int sysctl_ip_mcast_filters SYSCTL_HANDLER_ARGS; | |
6d2010ae A |
146 | static struct ifnet * ip_multicast_if(struct in_addr *, unsigned int *); |
147 | static __inline__ int ip_msource_cmp(const struct ip_msource *, | |
148 | const struct ip_msource *); | |
149 | ||
150 | SYSCTL_NODE(_net_inet_ip, OID_AUTO, mcast, CTLFLAG_RW | CTLFLAG_LOCKED, 0, "IPv4 multicast"); | |
151 | ||
152 | static u_long in_mcast_maxgrpsrc = IP_MAX_GROUP_SRC_FILTER; | |
153 | SYSCTL_LONG(_net_inet_ip_mcast, OID_AUTO, maxgrpsrc, | |
154 | CTLFLAG_RW | CTLFLAG_LOCKED, &in_mcast_maxgrpsrc, "Max source filters per group"); | |
155 | ||
f427ee49 A |
156 | static u_int in_mcast_maxsocksrc = IP_MAX_SOCK_SRC_FILTER; |
157 | SYSCTL_UINT(_net_inet_ip_mcast, OID_AUTO, maxsocksrc, | |
158 | CTLFLAG_RW | CTLFLAG_LOCKED, &in_mcast_maxsocksrc, IP_MAX_SOCK_SRC_FILTER, | |
6d2010ae A |
159 | "Max source filters per socket"); |
160 | ||
161 | int in_mcast_loop = IP_DEFAULT_MULTICAST_LOOP; | |
0a7de745 | 162 | SYSCTL_INT(_net_inet_ip_mcast, OID_AUTO, loop, CTLFLAG_RW | CTLFLAG_LOCKED, |
6d2010ae A |
163 | &in_mcast_loop, 0, "Loopback multicast datagrams by default"); |
164 | ||
165 | SYSCTL_NODE(_net_inet_ip_mcast, OID_AUTO, filters, | |
166 | CTLFLAG_RD | CTLFLAG_LOCKED, sysctl_ip_mcast_filters, | |
167 | "Per-interface stack-wide source filters"); | |
168 | ||
169 | RB_GENERATE_PREV(ip_msource_tree, ip_msource, ims_link, ip_msource_cmp); | |
170 | ||
0a7de745 | 171 | #define INM_TRACE_HIST_SIZE 32 /* size of trace history */ |
6d2010ae A |
172 | |
173 | /* For gdb */ | |
174 | __private_extern__ unsigned int inm_trace_hist_size = INM_TRACE_HIST_SIZE; | |
175 | ||
176 | struct in_multi_dbg { | |
0a7de745 A |
177 | struct in_multi inm; /* in_multi */ |
178 | u_int16_t inm_refhold_cnt; /* # of ref */ | |
179 | u_int16_t inm_refrele_cnt; /* # of rele */ | |
6d2010ae A |
180 | /* |
181 | * Circular lists of inm_addref and inm_remref callers. | |
182 | */ | |
0a7de745 A |
183 | ctrace_t inm_refhold[INM_TRACE_HIST_SIZE]; |
184 | ctrace_t inm_refrele[INM_TRACE_HIST_SIZE]; | |
6d2010ae A |
185 | /* |
186 | * Trash list linkage | |
187 | */ | |
188 | TAILQ_ENTRY(in_multi_dbg) inm_trash_link; | |
189 | }; | |
190 | ||
191 | /* List of trash in_multi entries protected by inm_trash_lock */ | |
192 | static TAILQ_HEAD(, in_multi_dbg) inm_trash_head; | |
193 | static decl_lck_mtx_data(, inm_trash_lock); | |
194 | ||
6d2010ae A |
195 | |
196 | #if DEBUG | |
0a7de745 | 197 | static unsigned int inm_debug = 1; /* debugging (enabled) */ |
6d2010ae | 198 | #else |
0a7de745 | 199 | static unsigned int inm_debug; /* debugging (disabled) */ |
6d2010ae | 200 | #endif /* !DEBUG */ |
f427ee49 | 201 | #define INM_ZONE_NAME "in_multi" /* zone name */ |
0a7de745 | 202 | static struct zone *inm_zone; /* zone for in_multi */ |
6d2010ae | 203 | |
f427ee49 A |
204 | static ZONE_DECLARE(ipms_zone, "ip_msource", sizeof(struct ip_msource), |
205 | ZC_ZFREE_CLEARMEM); | |
206 | static ZONE_DECLARE(inms_zone, "in_msource", sizeof(struct in_msource), | |
207 | ZC_ZFREE_CLEARMEM); | |
6d2010ae A |
208 | |
209 | /* Lock group and attribute for in_multihead_lock lock */ | |
0a7de745 A |
210 | static lck_attr_t *in_multihead_lock_attr; |
211 | static lck_grp_t *in_multihead_lock_grp; | |
212 | static lck_grp_attr_t *in_multihead_lock_grp_attr; | |
6d2010ae A |
213 | |
214 | static decl_lck_rw_data(, in_multihead_lock); | |
215 | struct in_multihead in_multihead; | |
216 | ||
f427ee49 | 217 | static struct in_multi *in_multi_alloc(zalloc_flags_t); |
6d2010ae A |
218 | static void in_multi_free(struct in_multi *); |
219 | static void in_multi_attach(struct in_multi *); | |
220 | static void inm_trace(struct in_multi *, int); | |
221 | ||
f427ee49 | 222 | static struct ip_msource *ipms_alloc(zalloc_flags_t); |
6d2010ae | 223 | static void ipms_free(struct ip_msource *); |
f427ee49 | 224 | static struct in_msource *inms_alloc(zalloc_flags_t); |
6d2010ae A |
225 | static void inms_free(struct in_msource *); |
226 | ||
6d2010ae A |
227 | static __inline int |
228 | ip_msource_cmp(const struct ip_msource *a, const struct ip_msource *b) | |
229 | { | |
0a7de745 A |
230 | if (a->ims_haddr < b->ims_haddr) { |
231 | return -1; | |
232 | } | |
233 | if (a->ims_haddr == b->ims_haddr) { | |
234 | return 0; | |
235 | } | |
236 | return 1; | |
6d2010ae A |
237 | } |
238 | ||
239 | /* | |
240 | * Inline function which wraps assertions for a valid ifp. | |
241 | */ | |
242 | static __inline__ int | |
243 | inm_is_ifp_detached(const struct in_multi *inm) | |
244 | { | |
245 | VERIFY(inm->inm_ifma != NULL); | |
246 | VERIFY(inm->inm_ifp == inm->inm_ifma->ifma_ifp); | |
247 | ||
0a7de745 | 248 | return !ifnet_is_attached(inm->inm_ifp, 0); |
6d2010ae A |
249 | } |
250 | ||
251 | /* | |
252 | * Initialize an in_mfilter structure to a known state at t0, t1 | |
253 | * with an empty source filter list. | |
254 | */ | |
255 | static __inline__ void | |
f427ee49 | 256 | imf_init(struct in_mfilter *imf, const uint8_t st0, const uint8_t st1) |
6d2010ae A |
257 | { |
258 | memset(imf, 0, sizeof(struct in_mfilter)); | |
259 | RB_INIT(&imf->imf_sources); | |
260 | imf->imf_st[0] = st0; | |
261 | imf->imf_st[1] = st1; | |
262 | } | |
263 | ||
264 | /* | |
265 | * Resize the ip_moptions vector to the next power-of-two minus 1. | |
266 | */ | |
267 | static int | |
f427ee49 | 268 | imo_grow(struct ip_moptions *imo, uint16_t newmax) |
6d2010ae | 269 | { |
0a7de745 A |
270 | struct in_multi **nmships; |
271 | struct in_multi **omships; | |
272 | struct in_mfilter *nmfilters; | |
273 | struct in_mfilter *omfilters; | |
f427ee49 A |
274 | uint16_t idx; |
275 | uint16_t oldmax; | |
6d2010ae A |
276 | |
277 | IMO_LOCK_ASSERT_HELD(imo); | |
278 | ||
279 | nmships = NULL; | |
280 | nmfilters = NULL; | |
281 | omships = imo->imo_membership; | |
282 | omfilters = imo->imo_mfilters; | |
283 | oldmax = imo->imo_max_memberships; | |
0a7de745 | 284 | if (newmax == 0) { |
6d2010ae | 285 | newmax = ((oldmax + 1) * 2) - 1; |
0a7de745 | 286 | } |
6d2010ae | 287 | |
0a7de745 A |
288 | if (newmax > IP_MAX_MEMBERSHIPS) { |
289 | return ETOOMANYREFS; | |
290 | } | |
6d2010ae A |
291 | |
292 | if ((nmships = (struct in_multi **)_REALLOC(omships, | |
0a7de745 A |
293 | sizeof(struct in_multi *) * newmax, M_IPMOPTS, |
294 | M_WAITOK | M_ZERO)) == NULL) { | |
295 | return ENOMEM; | |
296 | } | |
6d2010ae A |
297 | |
298 | imo->imo_membership = nmships; | |
299 | ||
300 | if ((nmfilters = (struct in_mfilter *)_REALLOC(omfilters, | |
0a7de745 A |
301 | sizeof(struct in_mfilter) * newmax, M_INMFILTER, |
302 | M_WAITOK | M_ZERO)) == NULL) { | |
303 | return ENOMEM; | |
304 | } | |
6d2010ae A |
305 | |
306 | imo->imo_mfilters = nmfilters; | |
307 | ||
308 | /* Initialize newly allocated source filter heads. */ | |
0a7de745 | 309 | for (idx = oldmax; idx < newmax; idx++) { |
6d2010ae | 310 | imf_init(&nmfilters[idx], MCAST_UNDEFINED, MCAST_EXCLUDE); |
0a7de745 | 311 | } |
6d2010ae A |
312 | |
313 | imo->imo_max_memberships = newmax; | |
314 | ||
0a7de745 | 315 | return 0; |
6d2010ae A |
316 | } |
317 | ||
318 | /* | |
319 | * Find an IPv4 multicast group entry for this ip_moptions instance | |
320 | * which matches the specified group, and optionally an interface. | |
321 | * Return its index into the array, or -1 if not found. | |
322 | */ | |
323 | static size_t | |
324 | imo_match_group(const struct ip_moptions *imo, const struct ifnet *ifp, | |
5ba3f43e | 325 | const struct sockaddr_in *group) |
6d2010ae | 326 | { |
0a7de745 A |
327 | struct in_multi *pinm; |
328 | int idx; | |
329 | int nmships; | |
6d2010ae | 330 | |
39236c6e | 331 | IMO_LOCK_ASSERT_HELD(__DECONST(struct ip_moptions *, imo)); |
6d2010ae | 332 | |
6d2010ae A |
333 | |
334 | /* The imo_membership array may be lazy allocated. */ | |
0a7de745 A |
335 | if (imo->imo_membership == NULL || imo->imo_num_memberships == 0) { |
336 | return -1; | |
337 | } | |
6d2010ae A |
338 | |
339 | nmships = imo->imo_num_memberships; | |
340 | for (idx = 0; idx < nmships; idx++) { | |
341 | pinm = imo->imo_membership[idx]; | |
0a7de745 | 342 | if (pinm == NULL) { |
6d2010ae | 343 | continue; |
0a7de745 | 344 | } |
6d2010ae A |
345 | INM_LOCK(pinm); |
346 | if ((ifp == NULL || (pinm->inm_ifp == ifp)) && | |
5ba3f43e | 347 | in_hosteq(pinm->inm_addr, group->sin_addr)) { |
6d2010ae A |
348 | INM_UNLOCK(pinm); |
349 | break; | |
350 | } | |
351 | INM_UNLOCK(pinm); | |
352 | } | |
0a7de745 | 353 | if (idx >= nmships) { |
6d2010ae | 354 | idx = -1; |
0a7de745 | 355 | } |
6d2010ae | 356 | |
0a7de745 | 357 | return idx; |
6d2010ae A |
358 | } |
359 | ||
360 | /* | |
361 | * Find an IPv4 multicast source entry for this imo which matches | |
362 | * the given group index for this socket, and source address. | |
363 | * | |
364 | * NOTE: This does not check if the entry is in-mode, merely if | |
365 | * it exists, which may not be the desired behaviour. | |
366 | */ | |
367 | static struct in_msource * | |
368 | imo_match_source(const struct ip_moptions *imo, const size_t gidx, | |
5ba3f43e | 369 | const struct sockaddr_in *src) |
6d2010ae | 370 | { |
0a7de745 A |
371 | struct ip_msource find; |
372 | struct in_mfilter *imf; | |
373 | struct ip_msource *ims; | |
6d2010ae | 374 | |
39236c6e | 375 | IMO_LOCK_ASSERT_HELD(__DECONST(struct ip_moptions *, imo)); |
6d2010ae | 376 | |
5ba3f43e | 377 | VERIFY(src->sin_family == AF_INET); |
6d2010ae A |
378 | VERIFY(gidx != (size_t)-1 && gidx < imo->imo_num_memberships); |
379 | ||
380 | /* The imo_mfilters array may be lazy allocated. */ | |
0a7de745 A |
381 | if (imo->imo_mfilters == NULL) { |
382 | return NULL; | |
383 | } | |
6d2010ae A |
384 | imf = &imo->imo_mfilters[gidx]; |
385 | ||
386 | /* Source trees are keyed in host byte order. */ | |
5ba3f43e | 387 | find.ims_haddr = ntohl(src->sin_addr.s_addr); |
6d2010ae A |
388 | ims = RB_FIND(ip_msource_tree, &imf->imf_sources, &find); |
389 | ||
0a7de745 | 390 | return (struct in_msource *)ims; |
6d2010ae A |
391 | } |
392 | ||
393 | /* | |
394 | * Perform filtering for multicast datagrams on a socket by group and source. | |
395 | * | |
396 | * Returns 0 if a datagram should be allowed through, or various error codes | |
397 | * if the socket was not a member of the group, or the source was muted, etc. | |
398 | */ | |
399 | int | |
400 | imo_multi_filter(const struct ip_moptions *imo, const struct ifnet *ifp, | |
5ba3f43e | 401 | const struct sockaddr_in *group, const struct sockaddr_in *src) |
6d2010ae A |
402 | { |
403 | size_t gidx; | |
404 | struct in_msource *ims; | |
405 | int mode; | |
406 | ||
39236c6e | 407 | IMO_LOCK_ASSERT_HELD(__DECONST(struct ip_moptions *, imo)); |
6d2010ae A |
408 | VERIFY(ifp != NULL); |
409 | ||
410 | gidx = imo_match_group(imo, ifp, group); | |
0a7de745 A |
411 | if (gidx == (size_t)-1) { |
412 | return MCAST_NOTGMEMBER; | |
413 | } | |
6d2010ae A |
414 | |
415 | /* | |
416 | * Check if the source was included in an (S,G) join. | |
417 | * Allow reception on exclusive memberships by default, | |
418 | * reject reception on inclusive memberships by default. | |
419 | * Exclude source only if an in-mode exclude filter exists. | |
420 | * Include source only if an in-mode include filter exists. | |
421 | * NOTE: We are comparing group state here at IGMP t1 (now) | |
422 | * with socket-layer t0 (since last downcall). | |
423 | */ | |
424 | mode = imo->imo_mfilters[gidx].imf_st[1]; | |
425 | ims = imo_match_source(imo, gidx, src); | |
426 | ||
427 | if ((ims == NULL && mode == MCAST_INCLUDE) || | |
428 | (ims != NULL && ims->imsl_st[0] != mode)) { | |
0a7de745 | 429 | return MCAST_NOTSMEMBER; |
6d2010ae A |
430 | } |
431 | ||
0a7de745 | 432 | return MCAST_PASS; |
6d2010ae A |
433 | } |
434 | ||
435 | int | |
316670eb | 436 | imo_clone(struct inpcb *from_inp, struct inpcb *to_inp) |
6d2010ae A |
437 | { |
438 | int i, err = 0; | |
316670eb A |
439 | struct ip_moptions *from; |
440 | struct ip_moptions *to; | |
441 | ||
442 | from = inp_findmoptions(from_inp); | |
0a7de745 A |
443 | if (from == NULL) { |
444 | return ENOMEM; | |
445 | } | |
316670eb A |
446 | |
447 | to = inp_findmoptions(to_inp); | |
448 | if (to == NULL) { | |
449 | IMO_REMREF(from); | |
0a7de745 | 450 | return ENOMEM; |
316670eb | 451 | } |
6d2010ae A |
452 | |
453 | IMO_LOCK(from); | |
454 | IMO_LOCK(to); | |
455 | ||
0a7de745 A |
456 | to->imo_multicast_ifp = from->imo_multicast_ifp; |
457 | to->imo_multicast_vif = from->imo_multicast_vif; | |
458 | to->imo_multicast_ttl = from->imo_multicast_ttl; | |
459 | to->imo_multicast_loop = from->imo_multicast_loop; | |
6d2010ae A |
460 | |
461 | /* | |
462 | * We're cloning, so drop any existing memberships and source | |
463 | * filters on the destination ip_moptions. | |
464 | */ | |
465 | for (i = 0; i < to->imo_num_memberships; ++i) { | |
466 | struct in_mfilter *imf; | |
467 | ||
468 | imf = to->imo_mfilters ? &to->imo_mfilters[i] : NULL; | |
0a7de745 | 469 | if (imf != NULL) { |
6d2010ae | 470 | imf_leave(imf); |
0a7de745 | 471 | } |
6d2010ae A |
472 | |
473 | (void) in_leavegroup(to->imo_membership[i], imf); | |
474 | ||
0a7de745 | 475 | if (imf != NULL) { |
6d2010ae | 476 | imf_purge(imf); |
0a7de745 | 477 | } |
6d2010ae A |
478 | |
479 | INM_REMREF(to->imo_membership[i]); | |
480 | to->imo_membership[i] = NULL; | |
481 | } | |
482 | to->imo_num_memberships = 0; | |
483 | ||
484 | VERIFY(to->imo_max_memberships != 0 && from->imo_max_memberships != 0); | |
485 | if (to->imo_max_memberships < from->imo_max_memberships) { | |
486 | /* | |
487 | * Ensure source and destination ip_moptions memberships | |
488 | * and source filters arrays are at least equal in size. | |
489 | */ | |
490 | err = imo_grow(to, from->imo_max_memberships); | |
0a7de745 | 491 | if (err != 0) { |
6d2010ae | 492 | goto done; |
0a7de745 | 493 | } |
6d2010ae A |
494 | } |
495 | VERIFY(to->imo_max_memberships >= from->imo_max_memberships); | |
496 | ||
497 | /* | |
498 | * Source filtering doesn't apply to OpenTransport socket, | |
499 | * so simply hold additional reference count per membership. | |
500 | */ | |
316670eb | 501 | for (i = 0; i < from->imo_num_memberships; i++) { |
0a7de745 A |
502 | to->imo_membership[i] = |
503 | in_addmulti(&from->imo_membership[i]->inm_addr, | |
504 | from->imo_membership[i]->inm_ifp); | |
505 | if (to->imo_membership[i] == NULL) { | |
316670eb | 506 | break; |
0a7de745 | 507 | } |
6d2010ae | 508 | to->imo_num_memberships++; |
0a7de745 | 509 | } |
6d2010ae A |
510 | VERIFY(to->imo_num_memberships == from->imo_num_memberships); |
511 | ||
512 | done: | |
513 | IMO_UNLOCK(to); | |
316670eb | 514 | IMO_REMREF(to); |
6d2010ae | 515 | IMO_UNLOCK(from); |
316670eb | 516 | IMO_REMREF(from); |
6d2010ae | 517 | |
0a7de745 | 518 | return err; |
6d2010ae A |
519 | } |
520 | ||
521 | /* | |
522 | * Find and return a reference to an in_multi record for (ifp, group), | |
523 | * and bump its reference count. | |
524 | * If one does not exist, try to allocate it, and update link-layer multicast | |
525 | * filters on ifp to listen for group. | |
526 | * Return 0 if successful, otherwise return an appropriate error code. | |
527 | */ | |
528 | static int | |
529 | in_getmulti(struct ifnet *ifp, const struct in_addr *group, | |
530 | struct in_multi **pinm) | |
531 | { | |
0a7de745 A |
532 | struct sockaddr_in gsin; |
533 | struct ifmultiaddr *ifma; | |
534 | struct in_multi *inm; | |
535 | int error; | |
6d2010ae A |
536 | |
537 | in_multihead_lock_shared(); | |
538 | IN_LOOKUP_MULTI(group, ifp, inm); | |
539 | if (inm != NULL) { | |
540 | INM_LOCK(inm); | |
541 | VERIFY(inm->inm_reqcnt >= 1); | |
542 | inm->inm_reqcnt++; | |
543 | VERIFY(inm->inm_reqcnt != 0); | |
544 | *pinm = inm; | |
545 | INM_UNLOCK(inm); | |
546 | in_multihead_lock_done(); | |
547 | /* | |
548 | * We already joined this group; return the inm | |
549 | * with a refcount held (via lookup) for caller. | |
550 | */ | |
0a7de745 | 551 | return 0; |
6d2010ae A |
552 | } |
553 | in_multihead_lock_done(); | |
554 | ||
555 | bzero(&gsin, sizeof(gsin)); | |
556 | gsin.sin_family = AF_INET; | |
557 | gsin.sin_len = sizeof(struct sockaddr_in); | |
558 | gsin.sin_addr = *group; | |
559 | ||
560 | /* | |
561 | * Check if a link-layer group is already associated | |
562 | * with this network-layer group on the given ifnet. | |
563 | */ | |
564 | error = if_addmulti(ifp, (struct sockaddr *)&gsin, &ifma); | |
0a7de745 A |
565 | if (error != 0) { |
566 | return error; | |
567 | } | |
6d2010ae A |
568 | |
569 | /* | |
570 | * See comments in inm_remref() for access to ifma_protospec. | |
571 | */ | |
572 | in_multihead_lock_exclusive(); | |
573 | IFMA_LOCK(ifma); | |
574 | if ((inm = ifma->ifma_protospec) != NULL) { | |
575 | VERIFY(ifma->ifma_addr != NULL); | |
576 | VERIFY(ifma->ifma_addr->sa_family == AF_INET); | |
0a7de745 | 577 | INM_ADDREF(inm); /* for caller */ |
6d2010ae A |
578 | IFMA_UNLOCK(ifma); |
579 | INM_LOCK(inm); | |
580 | VERIFY(inm->inm_ifma == ifma); | |
581 | VERIFY(inm->inm_ifp == ifp); | |
582 | VERIFY(in_hosteq(inm->inm_addr, *group)); | |
583 | if (inm->inm_debug & IFD_ATTACHED) { | |
584 | VERIFY(inm->inm_reqcnt >= 1); | |
585 | inm->inm_reqcnt++; | |
586 | VERIFY(inm->inm_reqcnt != 0); | |
587 | *pinm = inm; | |
588 | INM_UNLOCK(inm); | |
589 | in_multihead_lock_done(); | |
590 | IFMA_REMREF(ifma); | |
591 | /* | |
592 | * We lost the race with another thread doing | |
593 | * in_getmulti(); since this group has already | |
594 | * been joined; return the inm with a refcount | |
595 | * held for caller. | |
596 | */ | |
0a7de745 | 597 | return 0; |
6d2010ae A |
598 | } |
599 | /* | |
600 | * We lost the race with another thread doing in_delmulti(); | |
601 | * the inm referring to the ifma has been detached, thus we | |
602 | * reattach it back to the in_multihead list and return the | |
603 | * inm with a refcount held for the caller. | |
604 | */ | |
605 | in_multi_attach(inm); | |
606 | VERIFY((inm->inm_debug & | |
607 | (IFD_ATTACHED | IFD_TRASHED)) == IFD_ATTACHED); | |
608 | *pinm = inm; | |
609 | INM_UNLOCK(inm); | |
610 | in_multihead_lock_done(); | |
611 | IFMA_REMREF(ifma); | |
0a7de745 | 612 | return 0; |
6d2010ae A |
613 | } |
614 | IFMA_UNLOCK(ifma); | |
615 | ||
616 | /* | |
617 | * A new in_multi record is needed; allocate and initialize it. | |
618 | * We DO NOT perform an IGMP join as the in_ layer may need to | |
619 | * push an initial source list down to IGMP to support SSM. | |
620 | * | |
621 | * The initial source filter state is INCLUDE, {} as per the RFC. | |
622 | */ | |
f427ee49 A |
623 | inm = in_multi_alloc(Z_WAITOK); |
624 | ||
6d2010ae A |
625 | INM_LOCK(inm); |
626 | inm->inm_addr = *group; | |
627 | inm->inm_ifp = ifp; | |
628 | inm->inm_igi = IGMP_IFINFO(ifp); | |
629 | VERIFY(inm->inm_igi != NULL); | |
630 | IGI_ADDREF(inm->inm_igi); | |
0a7de745 | 631 | inm->inm_ifma = ifma; /* keep refcount from if_addmulti() */ |
6d2010ae A |
632 | inm->inm_state = IGMP_NOT_MEMBER; |
633 | /* | |
634 | * Pending state-changes per group are subject to a bounds check. | |
635 | */ | |
636 | inm->inm_scq.ifq_maxlen = IGMP_MAX_STATE_CHANGES; | |
637 | inm->inm_st[0].iss_fmode = MCAST_UNDEFINED; | |
638 | inm->inm_st[1].iss_fmode = MCAST_UNDEFINED; | |
639 | RB_INIT(&inm->inm_srcs); | |
640 | *pinm = inm; | |
641 | in_multi_attach(inm); | |
642 | VERIFY((inm->inm_debug & (IFD_ATTACHED | IFD_TRASHED)) == IFD_ATTACHED); | |
0a7de745 | 643 | INM_ADDREF_LOCKED(inm); /* for caller */ |
6d2010ae A |
644 | INM_UNLOCK(inm); |
645 | ||
646 | IFMA_LOCK(ifma); | |
647 | VERIFY(ifma->ifma_protospec == NULL); | |
648 | ifma->ifma_protospec = inm; | |
649 | IFMA_UNLOCK(ifma); | |
650 | in_multihead_lock_done(); | |
651 | ||
0a7de745 | 652 | return 0; |
6d2010ae A |
653 | } |
654 | ||
655 | /* | |
656 | * Clear recorded source entries for a group. | |
657 | * Used by the IGMP code. | |
658 | * FIXME: Should reap. | |
659 | */ | |
660 | void | |
661 | inm_clear_recorded(struct in_multi *inm) | |
662 | { | |
0a7de745 | 663 | struct ip_msource *ims; |
6d2010ae A |
664 | |
665 | INM_LOCK_ASSERT_HELD(inm); | |
666 | ||
667 | RB_FOREACH(ims, ip_msource_tree, &inm->inm_srcs) { | |
668 | if (ims->ims_stp) { | |
669 | ims->ims_stp = 0; | |
670 | --inm->inm_st[1].iss_rec; | |
671 | } | |
672 | } | |
673 | VERIFY(inm->inm_st[1].iss_rec == 0); | |
674 | } | |
675 | ||
676 | /* | |
677 | * Record a source as pending for a Source-Group IGMPv3 query. | |
678 | * This lives here as it modifies the shared tree. | |
679 | * | |
680 | * inm is the group descriptor. | |
681 | * naddr is the address of the source to record in network-byte order. | |
682 | * | |
683 | * If the net.inet.igmp.sgalloc sysctl is non-zero, we will | |
684 | * lazy-allocate a source node in response to an SG query. | |
685 | * Otherwise, no allocation is performed. This saves some memory | |
686 | * with the trade-off that the source will not be reported to the | |
687 | * router if joined in the window between the query response and | |
688 | * the group actually being joined on the local host. | |
689 | * | |
690 | * Return 0 if the source didn't exist or was already marked as recorded. | |
691 | * Return 1 if the source was marked as recorded by this function. | |
692 | * Return <0 if any error occured (negated errno code). | |
693 | */ | |
694 | int | |
695 | inm_record_source(struct in_multi *inm, const in_addr_t naddr) | |
696 | { | |
0a7de745 A |
697 | struct ip_msource find; |
698 | struct ip_msource *ims, *nims; | |
6d2010ae A |
699 | |
700 | INM_LOCK_ASSERT_HELD(inm); | |
701 | ||
702 | find.ims_haddr = ntohl(naddr); | |
703 | ims = RB_FIND(ip_msource_tree, &inm->inm_srcs, &find); | |
0a7de745 A |
704 | if (ims && ims->ims_stp) { |
705 | return 0; | |
706 | } | |
6d2010ae | 707 | if (ims == NULL) { |
0a7de745 A |
708 | if (inm->inm_nsrc == in_mcast_maxgrpsrc) { |
709 | return -ENOSPC; | |
710 | } | |
f427ee49 | 711 | nims = ipms_alloc(Z_WAITOK); |
6d2010ae A |
712 | nims->ims_haddr = find.ims_haddr; |
713 | RB_INSERT(ip_msource_tree, &inm->inm_srcs, nims); | |
714 | ++inm->inm_nsrc; | |
715 | ims = nims; | |
716 | } | |
717 | ||
718 | /* | |
719 | * Mark the source as recorded and update the recorded | |
720 | * source count. | |
721 | */ | |
722 | ++ims->ims_stp; | |
723 | ++inm->inm_st[1].iss_rec; | |
724 | ||
0a7de745 | 725 | return 1; |
6d2010ae A |
726 | } |
727 | ||
728 | /* | |
729 | * Return a pointer to an in_msource owned by an in_mfilter, | |
730 | * given its source address. | |
731 | * Lazy-allocate if needed. If this is a new entry its filter state is | |
732 | * undefined at t0. | |
733 | * | |
734 | * imf is the filter set being modified. | |
735 | * haddr is the source address in *host* byte-order. | |
736 | * | |
737 | * Caller is expected to be holding imo_lock. | |
738 | */ | |
739 | static int | |
740 | imf_get_source(struct in_mfilter *imf, const struct sockaddr_in *psin, | |
741 | struct in_msource **plims) | |
742 | { | |
0a7de745 A |
743 | struct ip_msource find; |
744 | struct ip_msource *ims; | |
745 | struct in_msource *lims; | |
746 | int error; | |
6d2010ae A |
747 | |
748 | error = 0; | |
749 | ims = NULL; | |
750 | lims = NULL; | |
751 | ||
752 | /* key is host byte order */ | |
753 | find.ims_haddr = ntohl(psin->sin_addr.s_addr); | |
754 | ims = RB_FIND(ip_msource_tree, &imf->imf_sources, &find); | |
755 | lims = (struct in_msource *)ims; | |
756 | if (lims == NULL) { | |
0a7de745 A |
757 | if (imf->imf_nsrc == in_mcast_maxsocksrc) { |
758 | return ENOSPC; | |
759 | } | |
f427ee49 | 760 | lims = inms_alloc(Z_WAITOK); |
6d2010ae A |
761 | lims->ims_haddr = find.ims_haddr; |
762 | lims->imsl_st[0] = MCAST_UNDEFINED; | |
763 | RB_INSERT(ip_msource_tree, &imf->imf_sources, | |
764 | (struct ip_msource *)lims); | |
765 | ++imf->imf_nsrc; | |
766 | } | |
767 | ||
768 | *plims = lims; | |
769 | ||
0a7de745 | 770 | return error; |
6d2010ae A |
771 | } |
772 | ||
773 | /* | |
774 | * Graft a source entry into an existing socket-layer filter set, | |
775 | * maintaining any required invariants and checking allocations. | |
776 | * | |
777 | * The source is marked as being in the new filter mode at t1. | |
778 | * | |
779 | * Return the pointer to the new node, otherwise return NULL. | |
780 | * | |
781 | * Caller is expected to be holding imo_lock. | |
782 | */ | |
783 | static struct in_msource * | |
784 | imf_graft(struct in_mfilter *imf, const uint8_t st1, | |
785 | const struct sockaddr_in *psin) | |
786 | { | |
0a7de745 | 787 | struct in_msource *lims; |
6d2010ae | 788 | |
f427ee49 | 789 | lims = inms_alloc(Z_WAITOK); |
6d2010ae A |
790 | lims->ims_haddr = ntohl(psin->sin_addr.s_addr); |
791 | lims->imsl_st[0] = MCAST_UNDEFINED; | |
792 | lims->imsl_st[1] = st1; | |
793 | RB_INSERT(ip_msource_tree, &imf->imf_sources, | |
794 | (struct ip_msource *)lims); | |
795 | ++imf->imf_nsrc; | |
796 | ||
0a7de745 | 797 | return lims; |
6d2010ae A |
798 | } |
799 | ||
800 | /* | |
801 | * Prune a source entry from an existing socket-layer filter set, | |
802 | * maintaining any required invariants and checking allocations. | |
803 | * | |
804 | * The source is marked as being left at t1, it is not freed. | |
805 | * | |
806 | * Return 0 if no error occurred, otherwise return an errno value. | |
807 | * | |
808 | * Caller is expected to be holding imo_lock. | |
809 | */ | |
810 | static int | |
811 | imf_prune(struct in_mfilter *imf, const struct sockaddr_in *psin) | |
812 | { | |
0a7de745 A |
813 | struct ip_msource find; |
814 | struct ip_msource *ims; | |
815 | struct in_msource *lims; | |
6d2010ae A |
816 | |
817 | /* key is host byte order */ | |
818 | find.ims_haddr = ntohl(psin->sin_addr.s_addr); | |
819 | ims = RB_FIND(ip_msource_tree, &imf->imf_sources, &find); | |
0a7de745 A |
820 | if (ims == NULL) { |
821 | return ENOENT; | |
822 | } | |
6d2010ae A |
823 | lims = (struct in_msource *)ims; |
824 | lims->imsl_st[1] = MCAST_UNDEFINED; | |
0a7de745 | 825 | return 0; |
6d2010ae A |
826 | } |
827 | ||
828 | /* | |
829 | * Revert socket-layer filter set deltas at t1 to t0 state. | |
830 | * | |
831 | * Caller is expected to be holding imo_lock. | |
832 | */ | |
833 | static void | |
834 | imf_rollback(struct in_mfilter *imf) | |
835 | { | |
0a7de745 A |
836 | struct ip_msource *ims, *tims; |
837 | struct in_msource *lims; | |
6d2010ae A |
838 | |
839 | RB_FOREACH_SAFE(ims, ip_msource_tree, &imf->imf_sources, tims) { | |
840 | lims = (struct in_msource *)ims; | |
841 | if (lims->imsl_st[0] == lims->imsl_st[1]) { | |
842 | /* no change at t1 */ | |
843 | continue; | |
844 | } else if (lims->imsl_st[0] != MCAST_UNDEFINED) { | |
845 | /* revert change to existing source at t1 */ | |
846 | lims->imsl_st[1] = lims->imsl_st[0]; | |
847 | } else { | |
848 | /* revert source added t1 */ | |
39236c6e A |
849 | IGMP_PRINTF(("%s: free inms 0x%llx\n", __func__, |
850 | (uint64_t)VM_KERNEL_ADDRPERM(lims))); | |
6d2010ae A |
851 | RB_REMOVE(ip_msource_tree, &imf->imf_sources, ims); |
852 | inms_free(lims); | |
853 | imf->imf_nsrc--; | |
854 | } | |
855 | } | |
856 | imf->imf_st[1] = imf->imf_st[0]; | |
857 | } | |
858 | ||
859 | /* | |
860 | * Mark socket-layer filter set as INCLUDE {} at t1. | |
861 | * | |
862 | * Caller is expected to be holding imo_lock. | |
863 | */ | |
864 | void | |
865 | imf_leave(struct in_mfilter *imf) | |
866 | { | |
0a7de745 A |
867 | struct ip_msource *ims; |
868 | struct in_msource *lims; | |
6d2010ae A |
869 | |
870 | RB_FOREACH(ims, ip_msource_tree, &imf->imf_sources) { | |
871 | lims = (struct in_msource *)ims; | |
872 | lims->imsl_st[1] = MCAST_UNDEFINED; | |
873 | } | |
874 | imf->imf_st[1] = MCAST_INCLUDE; | |
875 | } | |
876 | ||
877 | /* | |
878 | * Mark socket-layer filter set deltas as committed. | |
879 | * | |
880 | * Caller is expected to be holding imo_lock. | |
881 | */ | |
882 | static void | |
883 | imf_commit(struct in_mfilter *imf) | |
884 | { | |
0a7de745 A |
885 | struct ip_msource *ims; |
886 | struct in_msource *lims; | |
6d2010ae A |
887 | |
888 | RB_FOREACH(ims, ip_msource_tree, &imf->imf_sources) { | |
889 | lims = (struct in_msource *)ims; | |
890 | lims->imsl_st[0] = lims->imsl_st[1]; | |
891 | } | |
892 | imf->imf_st[0] = imf->imf_st[1]; | |
893 | } | |
894 | ||
895 | /* | |
896 | * Reap unreferenced sources from socket-layer filter set. | |
897 | * | |
898 | * Caller is expected to be holding imo_lock. | |
899 | */ | |
900 | static void | |
901 | imf_reap(struct in_mfilter *imf) | |
902 | { | |
0a7de745 A |
903 | struct ip_msource *ims, *tims; |
904 | struct in_msource *lims; | |
6d2010ae A |
905 | |
906 | RB_FOREACH_SAFE(ims, ip_msource_tree, &imf->imf_sources, tims) { | |
907 | lims = (struct in_msource *)ims; | |
908 | if ((lims->imsl_st[0] == MCAST_UNDEFINED) && | |
909 | (lims->imsl_st[1] == MCAST_UNDEFINED)) { | |
39236c6e A |
910 | IGMP_PRINTF(("%s: free inms 0x%llx\n", __func__, |
911 | (uint64_t)VM_KERNEL_ADDRPERM(lims))); | |
6d2010ae A |
912 | RB_REMOVE(ip_msource_tree, &imf->imf_sources, ims); |
913 | inms_free(lims); | |
914 | imf->imf_nsrc--; | |
915 | } | |
916 | } | |
917 | } | |
918 | ||
919 | /* | |
920 | * Purge socket-layer filter set. | |
921 | * | |
922 | * Caller is expected to be holding imo_lock. | |
923 | */ | |
924 | void | |
925 | imf_purge(struct in_mfilter *imf) | |
926 | { | |
0a7de745 A |
927 | struct ip_msource *ims, *tims; |
928 | struct in_msource *lims; | |
6d2010ae A |
929 | |
930 | RB_FOREACH_SAFE(ims, ip_msource_tree, &imf->imf_sources, tims) { | |
931 | lims = (struct in_msource *)ims; | |
39236c6e A |
932 | IGMP_PRINTF(("%s: free inms 0x%llx\n", __func__, |
933 | (uint64_t)VM_KERNEL_ADDRPERM(lims))); | |
6d2010ae A |
934 | RB_REMOVE(ip_msource_tree, &imf->imf_sources, ims); |
935 | inms_free(lims); | |
936 | imf->imf_nsrc--; | |
937 | } | |
938 | imf->imf_st[0] = imf->imf_st[1] = MCAST_UNDEFINED; | |
939 | VERIFY(RB_EMPTY(&imf->imf_sources)); | |
940 | } | |
941 | ||
942 | /* | |
943 | * Look up a source filter entry for a multicast group. | |
944 | * | |
945 | * inm is the group descriptor to work with. | |
946 | * haddr is the host-byte-order IPv4 address to look up. | |
947 | * noalloc may be non-zero to suppress allocation of sources. | |
948 | * *pims will be set to the address of the retrieved or allocated source. | |
949 | * | |
950 | * Return 0 if successful, otherwise return a non-zero error code. | |
951 | */ | |
952 | static int | |
953 | inm_get_source(struct in_multi *inm, const in_addr_t haddr, | |
954 | const int noalloc, struct ip_msource **pims) | |
955 | { | |
0a7de745 A |
956 | struct ip_msource find; |
957 | struct ip_msource *ims, *nims; | |
6d2010ae A |
958 | #ifdef IGMP_DEBUG |
959 | struct in_addr ia; | |
39236c6e | 960 | char buf[MAX_IPv4_STR_LEN]; |
6d2010ae A |
961 | #endif |
962 | INM_LOCK_ASSERT_HELD(inm); | |
963 | ||
964 | find.ims_haddr = haddr; | |
965 | ims = RB_FIND(ip_msource_tree, &inm->inm_srcs, &find); | |
966 | if (ims == NULL && !noalloc) { | |
0a7de745 A |
967 | if (inm->inm_nsrc == in_mcast_maxgrpsrc) { |
968 | return ENOSPC; | |
969 | } | |
f427ee49 | 970 | nims = ipms_alloc(Z_WAITOK); |
6d2010ae A |
971 | nims->ims_haddr = haddr; |
972 | RB_INSERT(ip_msource_tree, &inm->inm_srcs, nims); | |
973 | ++inm->inm_nsrc; | |
974 | ims = nims; | |
975 | #ifdef IGMP_DEBUG | |
976 | ia.s_addr = htonl(haddr); | |
39236c6e A |
977 | inet_ntop(AF_INET, &ia, buf, sizeof(buf)); |
978 | IGMP_PRINTF(("%s: allocated %s as 0x%llx\n", __func__, | |
979 | buf, (uint64_t)VM_KERNEL_ADDRPERM(ims))); | |
6d2010ae A |
980 | #endif |
981 | } | |
982 | ||
983 | *pims = ims; | |
0a7de745 | 984 | return 0; |
6d2010ae A |
985 | } |
986 | ||
987 | /* | |
988 | * Helper function to derive the filter mode on a source entry | |
989 | * from its internal counters. Predicates are: | |
990 | * A source is only excluded if all listeners exclude it. | |
991 | * A source is only included if no listeners exclude it, | |
992 | * and at least one listener includes it. | |
993 | * May be used by ifmcstat(8). | |
994 | */ | |
995 | uint8_t | |
996 | ims_get_mode(const struct in_multi *inm, const struct ip_msource *ims, | |
997 | uint8_t t) | |
998 | { | |
39236c6e | 999 | INM_LOCK_ASSERT_HELD(__DECONST(struct in_multi *, inm)); |
6d2010ae A |
1000 | |
1001 | t = !!t; | |
1002 | if (inm->inm_st[t].iss_ex > 0 && | |
0a7de745 A |
1003 | inm->inm_st[t].iss_ex == ims->ims_st[t].ex) { |
1004 | return MCAST_EXCLUDE; | |
1005 | } else if (ims->ims_st[t].in > 0 && ims->ims_st[t].ex == 0) { | |
1006 | return MCAST_INCLUDE; | |
1007 | } | |
1008 | return MCAST_UNDEFINED; | |
6d2010ae A |
1009 | } |
1010 | ||
1011 | /* | |
1012 | * Merge socket-layer source into IGMP-layer source. | |
1013 | * If rollback is non-zero, perform the inverse of the merge. | |
1014 | */ | |
1015 | static void | |
1016 | ims_merge(struct ip_msource *ims, const struct in_msource *lims, | |
1017 | const int rollback) | |
1018 | { | |
1019 | int n = rollback ? -1 : 1; | |
1020 | #ifdef IGMP_DEBUG | |
1021 | struct in_addr ia; | |
1022 | ||
1023 | ia.s_addr = htonl(ims->ims_haddr); | |
1024 | #endif | |
1025 | ||
1026 | if (lims->imsl_st[0] == MCAST_EXCLUDE) { | |
39236c6e A |
1027 | IGMP_INET_PRINTF(ia, |
1028 | ("%s: t1 ex -= %d on %s\n", | |
1029 | __func__, n, _igmp_inet_buf)); | |
6d2010ae A |
1030 | ims->ims_st[1].ex -= n; |
1031 | } else if (lims->imsl_st[0] == MCAST_INCLUDE) { | |
39236c6e A |
1032 | IGMP_INET_PRINTF(ia, |
1033 | ("%s: t1 in -= %d on %s\n", | |
1034 | __func__, n, _igmp_inet_buf)); | |
6d2010ae A |
1035 | ims->ims_st[1].in -= n; |
1036 | } | |
1037 | ||
1038 | if (lims->imsl_st[1] == MCAST_EXCLUDE) { | |
39236c6e A |
1039 | IGMP_INET_PRINTF(ia, |
1040 | ("%s: t1 ex += %d on %s\n", | |
1041 | __func__, n, _igmp_inet_buf)); | |
6d2010ae A |
1042 | ims->ims_st[1].ex += n; |
1043 | } else if (lims->imsl_st[1] == MCAST_INCLUDE) { | |
39236c6e A |
1044 | IGMP_INET_PRINTF(ia, |
1045 | ("%s: t1 in += %d on %s\n", | |
1046 | __func__, n, _igmp_inet_buf)); | |
6d2010ae A |
1047 | ims->ims_st[1].in += n; |
1048 | } | |
1049 | } | |
1050 | ||
1051 | /* | |
1052 | * Atomically update the global in_multi state, when a membership's | |
1053 | * filter list is being updated in any way. | |
1054 | * | |
1055 | * imf is the per-inpcb-membership group filter pointer. | |
1056 | * A fake imf may be passed for in-kernel consumers. | |
1057 | * | |
1058 | * XXX This is a candidate for a set-symmetric-difference style loop | |
1059 | * which would eliminate the repeated lookup from root of ims nodes, | |
1060 | * as they share the same key space. | |
1061 | * | |
1062 | * If any error occurred this function will back out of refcounts | |
1063 | * and return a non-zero value. | |
1064 | */ | |
1065 | static int | |
1066 | inm_merge(struct in_multi *inm, /*const*/ struct in_mfilter *imf) | |
1067 | { | |
0a7de745 A |
1068 | struct ip_msource *ims, *nims = NULL; |
1069 | struct in_msource *lims; | |
1070 | int schanged, error; | |
1071 | int nsrc0, nsrc1; | |
6d2010ae A |
1072 | |
1073 | INM_LOCK_ASSERT_HELD(inm); | |
1074 | ||
1075 | schanged = 0; | |
1076 | error = 0; | |
1077 | nsrc1 = nsrc0 = 0; | |
1078 | ||
1079 | /* | |
1080 | * Update the source filters first, as this may fail. | |
1081 | * Maintain count of in-mode filters at t0, t1. These are | |
1082 | * used to work out if we transition into ASM mode or not. | |
1083 | * Maintain a count of source filters whose state was | |
1084 | * actually modified by this operation. | |
1085 | */ | |
1086 | RB_FOREACH(ims, ip_msource_tree, &imf->imf_sources) { | |
1087 | lims = (struct in_msource *)ims; | |
0a7de745 A |
1088 | if (lims->imsl_st[0] == imf->imf_st[0]) { |
1089 | nsrc0++; | |
1090 | } | |
1091 | if (lims->imsl_st[1] == imf->imf_st[1]) { | |
1092 | nsrc1++; | |
1093 | } | |
1094 | if (lims->imsl_st[0] == lims->imsl_st[1]) { | |
1095 | continue; | |
1096 | } | |
6d2010ae A |
1097 | error = inm_get_source(inm, lims->ims_haddr, 0, &nims); |
1098 | ++schanged; | |
0a7de745 | 1099 | if (error) { |
6d2010ae | 1100 | break; |
0a7de745 | 1101 | } |
6d2010ae A |
1102 | ims_merge(nims, lims, 0); |
1103 | } | |
1104 | if (error) { | |
1105 | struct ip_msource *bims; | |
1106 | ||
1107 | RB_FOREACH_REVERSE_FROM(ims, ip_msource_tree, nims) { | |
1108 | lims = (struct in_msource *)ims; | |
0a7de745 | 1109 | if (lims->imsl_st[0] == lims->imsl_st[1]) { |
6d2010ae | 1110 | continue; |
0a7de745 | 1111 | } |
6d2010ae | 1112 | (void) inm_get_source(inm, lims->ims_haddr, 1, &bims); |
0a7de745 | 1113 | if (bims == NULL) { |
6d2010ae | 1114 | continue; |
0a7de745 | 1115 | } |
6d2010ae A |
1116 | ims_merge(bims, lims, 1); |
1117 | } | |
1118 | goto out_reap; | |
1119 | } | |
1120 | ||
1121 | IGMP_PRINTF(("%s: imf filters in-mode: %d at t0, %d at t1\n", | |
1122 | __func__, nsrc0, nsrc1)); | |
1123 | ||
1124 | /* Handle transition between INCLUDE {n} and INCLUDE {} on socket. */ | |
1125 | if (imf->imf_st[0] == imf->imf_st[1] && | |
1126 | imf->imf_st[1] == MCAST_INCLUDE) { | |
1127 | if (nsrc1 == 0) { | |
1128 | IGMP_PRINTF(("%s: --in on inm at t1\n", __func__)); | |
1129 | --inm->inm_st[1].iss_in; | |
1130 | } | |
1131 | } | |
1132 | ||
1133 | /* Handle filter mode transition on socket. */ | |
1134 | if (imf->imf_st[0] != imf->imf_st[1]) { | |
1135 | IGMP_PRINTF(("%s: imf transition %d to %d\n", | |
1136 | __func__, imf->imf_st[0], imf->imf_st[1])); | |
1137 | ||
1138 | if (imf->imf_st[0] == MCAST_EXCLUDE) { | |
1139 | IGMP_PRINTF(("%s: --ex on inm at t1\n", __func__)); | |
1140 | --inm->inm_st[1].iss_ex; | |
1141 | } else if (imf->imf_st[0] == MCAST_INCLUDE) { | |
1142 | IGMP_PRINTF(("%s: --in on inm at t1\n", __func__)); | |
1143 | --inm->inm_st[1].iss_in; | |
1144 | } | |
1145 | ||
1146 | if (imf->imf_st[1] == MCAST_EXCLUDE) { | |
1147 | IGMP_PRINTF(("%s: ex++ on inm at t1\n", __func__)); | |
1148 | inm->inm_st[1].iss_ex++; | |
1149 | } else if (imf->imf_st[1] == MCAST_INCLUDE && nsrc1 > 0) { | |
1150 | IGMP_PRINTF(("%s: in++ on inm at t1\n", __func__)); | |
1151 | inm->inm_st[1].iss_in++; | |
1152 | } | |
1153 | } | |
1154 | ||
1155 | /* | |
1156 | * Track inm filter state in terms of listener counts. | |
1157 | * If there are any exclusive listeners, stack-wide | |
1158 | * membership is exclusive. | |
1159 | * Otherwise, if only inclusive listeners, stack-wide is inclusive. | |
1160 | * If no listeners remain, state is undefined at t1, | |
1161 | * and the IGMP lifecycle for this group should finish. | |
1162 | */ | |
1163 | if (inm->inm_st[1].iss_ex > 0) { | |
1164 | IGMP_PRINTF(("%s: transition to EX\n", __func__)); | |
1165 | inm->inm_st[1].iss_fmode = MCAST_EXCLUDE; | |
1166 | } else if (inm->inm_st[1].iss_in > 0) { | |
1167 | IGMP_PRINTF(("%s: transition to IN\n", __func__)); | |
1168 | inm->inm_st[1].iss_fmode = MCAST_INCLUDE; | |
1169 | } else { | |
1170 | IGMP_PRINTF(("%s: transition to UNDEF\n", __func__)); | |
1171 | inm->inm_st[1].iss_fmode = MCAST_UNDEFINED; | |
1172 | } | |
1173 | ||
1174 | /* Decrement ASM listener count on transition out of ASM mode. */ | |
1175 | if (imf->imf_st[0] == MCAST_EXCLUDE && nsrc0 == 0) { | |
1176 | if ((imf->imf_st[1] != MCAST_EXCLUDE) || | |
1177 | (imf->imf_st[1] == MCAST_EXCLUDE && nsrc1 > 0)) { | |
1178 | IGMP_PRINTF(("%s: --asm on inm at t1\n", __func__)); | |
1179 | --inm->inm_st[1].iss_asm; | |
1180 | } | |
1181 | } | |
1182 | ||
1183 | /* Increment ASM listener count on transition to ASM mode. */ | |
1184 | if (imf->imf_st[1] == MCAST_EXCLUDE && nsrc1 == 0) { | |
1185 | IGMP_PRINTF(("%s: asm++ on inm at t1\n", __func__)); | |
1186 | inm->inm_st[1].iss_asm++; | |
1187 | } | |
1188 | ||
39236c6e A |
1189 | IGMP_PRINTF(("%s: merged imf 0x%llx to inm 0x%llx\n", __func__, |
1190 | (uint64_t)VM_KERNEL_ADDRPERM(imf), | |
1191 | (uint64_t)VM_KERNEL_ADDRPERM(inm))); | |
6d2010ae A |
1192 | inm_print(inm); |
1193 | ||
1194 | out_reap: | |
1195 | if (schanged > 0) { | |
1196 | IGMP_PRINTF(("%s: sources changed; reaping\n", __func__)); | |
1197 | inm_reap(inm); | |
1198 | } | |
0a7de745 | 1199 | return error; |
6d2010ae A |
1200 | } |
1201 | ||
1202 | /* | |
1203 | * Mark an in_multi's filter set deltas as committed. | |
1204 | * Called by IGMP after a state change has been enqueued. | |
1205 | */ | |
1206 | void | |
1207 | inm_commit(struct in_multi *inm) | |
1208 | { | |
0a7de745 | 1209 | struct ip_msource *ims; |
6d2010ae A |
1210 | |
1211 | INM_LOCK_ASSERT_HELD(inm); | |
1212 | ||
39236c6e A |
1213 | IGMP_PRINTF(("%s: commit inm 0x%llx\n", __func__, |
1214 | (uint64_t)VM_KERNEL_ADDRPERM(inm))); | |
6d2010ae A |
1215 | IGMP_PRINTF(("%s: pre commit:\n", __func__)); |
1216 | inm_print(inm); | |
1217 | ||
1218 | RB_FOREACH(ims, ip_msource_tree, &inm->inm_srcs) { | |
1219 | ims->ims_st[0] = ims->ims_st[1]; | |
1220 | } | |
1221 | inm->inm_st[0] = inm->inm_st[1]; | |
1222 | } | |
1223 | ||
1224 | /* | |
1225 | * Reap unreferenced nodes from an in_multi's filter set. | |
1226 | */ | |
1227 | static void | |
1228 | inm_reap(struct in_multi *inm) | |
1229 | { | |
0a7de745 | 1230 | struct ip_msource *ims, *tims; |
6d2010ae A |
1231 | |
1232 | INM_LOCK_ASSERT_HELD(inm); | |
1233 | ||
1234 | RB_FOREACH_SAFE(ims, ip_msource_tree, &inm->inm_srcs, tims) { | |
1235 | if (ims->ims_st[0].ex > 0 || ims->ims_st[0].in > 0 || | |
1236 | ims->ims_st[1].ex > 0 || ims->ims_st[1].in > 0 || | |
0a7de745 | 1237 | ims->ims_stp != 0) { |
6d2010ae | 1238 | continue; |
0a7de745 | 1239 | } |
39236c6e A |
1240 | IGMP_PRINTF(("%s: free ims 0x%llx\n", __func__, |
1241 | (uint64_t)VM_KERNEL_ADDRPERM(ims))); | |
6d2010ae A |
1242 | RB_REMOVE(ip_msource_tree, &inm->inm_srcs, ims); |
1243 | ipms_free(ims); | |
1244 | inm->inm_nsrc--; | |
1245 | } | |
1246 | } | |
1247 | ||
1248 | /* | |
1249 | * Purge all source nodes from an in_multi's filter set. | |
1250 | */ | |
1251 | void | |
1252 | inm_purge(struct in_multi *inm) | |
1253 | { | |
0a7de745 | 1254 | struct ip_msource *ims, *tims; |
6d2010ae A |
1255 | |
1256 | INM_LOCK_ASSERT_HELD(inm); | |
1257 | ||
1258 | RB_FOREACH_SAFE(ims, ip_msource_tree, &inm->inm_srcs, tims) { | |
39236c6e A |
1259 | IGMP_PRINTF(("%s: free ims 0x%llx\n", __func__, |
1260 | (uint64_t)VM_KERNEL_ADDRPERM(ims))); | |
6d2010ae A |
1261 | RB_REMOVE(ip_msource_tree, &inm->inm_srcs, ims); |
1262 | ipms_free(ims); | |
1263 | inm->inm_nsrc--; | |
1264 | } | |
1265 | } | |
1266 | ||
1267 | /* | |
1268 | * Join a multicast group; real entry point. | |
1269 | * | |
1270 | * Only preserves atomicity at inm level. | |
1271 | * NOTE: imf argument cannot be const due to sys/tree.h limitations. | |
1272 | * | |
1273 | * If the IGMP downcall fails, the group is not joined, and an error | |
1274 | * code is returned. | |
1275 | */ | |
1276 | static int | |
1277 | in_joingroup(struct ifnet *ifp, const struct in_addr *gina, | |
1278 | /*const*/ struct in_mfilter *imf, struct in_multi **pinm) | |
1279 | { | |
0a7de745 A |
1280 | struct in_mfilter timf; |
1281 | struct in_multi *inm = NULL; | |
1282 | int error = 0; | |
1283 | struct igmp_tparams itp; | |
6d2010ae | 1284 | |
39236c6e A |
1285 | IGMP_INET_PRINTF(*gina, ("%s: join %s on 0x%llx(%s))\n", __func__, |
1286 | _igmp_inet_buf, (uint64_t)VM_KERNEL_ADDRPERM(ifp), if_name(ifp))); | |
6d2010ae | 1287 | |
0a7de745 | 1288 | bzero(&itp, sizeof(itp)); |
6d2010ae A |
1289 | *pinm = NULL; |
1290 | ||
1291 | /* | |
1292 | * If no imf was specified (i.e. kernel consumer), | |
1293 | * fake one up and assume it is an ASM join. | |
1294 | */ | |
1295 | if (imf == NULL) { | |
1296 | imf_init(&timf, MCAST_UNDEFINED, MCAST_EXCLUDE); | |
1297 | imf = &timf; | |
1298 | } | |
1299 | ||
1300 | error = in_getmulti(ifp, gina, &inm); | |
1301 | if (error) { | |
1302 | IGMP_PRINTF(("%s: in_getmulti() failure\n", __func__)); | |
0a7de745 | 1303 | return error; |
6d2010ae A |
1304 | } |
1305 | ||
1306 | IGMP_PRINTF(("%s: merge inm state\n", __func__)); | |
1307 | ||
1308 | INM_LOCK(inm); | |
1309 | error = inm_merge(inm, imf); | |
1310 | if (error) { | |
1311 | IGMP_PRINTF(("%s: failed to merge inm state\n", __func__)); | |
1312 | goto out_inm_release; | |
1313 | } | |
1314 | ||
1315 | IGMP_PRINTF(("%s: doing igmp downcall\n", __func__)); | |
39236c6e | 1316 | error = igmp_change_state(inm, &itp); |
6d2010ae A |
1317 | if (error) { |
1318 | IGMP_PRINTF(("%s: failed to update source\n", __func__)); | |
39236c6e | 1319 | imf_rollback(imf); |
6d2010ae A |
1320 | goto out_inm_release; |
1321 | } | |
1322 | ||
1323 | out_inm_release: | |
1324 | if (error) { | |
39236c6e A |
1325 | IGMP_PRINTF(("%s: dropping ref on 0x%llx\n", __func__, |
1326 | (uint64_t)VM_KERNEL_ADDRPERM(inm))); | |
6d2010ae A |
1327 | INM_UNLOCK(inm); |
1328 | INM_REMREF(inm); | |
1329 | } else { | |
1330 | INM_UNLOCK(inm); | |
0a7de745 | 1331 | *pinm = inm; /* keep refcount from in_getmulti() */ |
6d2010ae A |
1332 | } |
1333 | ||
39236c6e A |
1334 | /* schedule timer now that we've dropped the lock(s) */ |
1335 | igmp_set_timeout(&itp); | |
1336 | ||
0a7de745 | 1337 | return error; |
6d2010ae A |
1338 | } |
1339 | ||
1340 | /* | |
1341 | * Leave a multicast group; real entry point. | |
1342 | * All source filters will be expunged. | |
1343 | * | |
1344 | * Only preserves atomicity at inm level. | |
1345 | * | |
1346 | * Note: This is not the same as inm_release(*) as this function also | |
1347 | * makes a state change downcall into IGMP. | |
1348 | */ | |
1349 | int | |
1350 | in_leavegroup(struct in_multi *inm, /*const*/ struct in_mfilter *imf) | |
1351 | { | |
0a7de745 A |
1352 | struct in_mfilter timf; |
1353 | int error, lastref; | |
1354 | struct igmp_tparams itp; | |
6d2010ae | 1355 | |
0a7de745 | 1356 | bzero(&itp, sizeof(itp)); |
6d2010ae A |
1357 | error = 0; |
1358 | ||
1359 | INM_LOCK_ASSERT_NOTHELD(inm); | |
1360 | ||
0a7de745 A |
1361 | in_multihead_lock_exclusive(); |
1362 | INM_LOCK(inm); | |
6d2010ae | 1363 | |
39236c6e A |
1364 | IGMP_INET_PRINTF(inm->inm_addr, |
1365 | ("%s: leave inm 0x%llx, %s/%s%d, imf 0x%llx\n", __func__, | |
1366 | (uint64_t)VM_KERNEL_ADDRPERM(inm), _igmp_inet_buf, | |
6d2010ae | 1367 | (inm_is_ifp_detached(inm) ? "null" : inm->inm_ifp->if_name), |
39236c6e | 1368 | inm->inm_ifp->if_unit, (uint64_t)VM_KERNEL_ADDRPERM(imf))); |
6d2010ae A |
1369 | |
1370 | /* | |
1371 | * If no imf was specified (i.e. kernel consumer), | |
1372 | * fake one up and assume it is an ASM join. | |
1373 | */ | |
1374 | if (imf == NULL) { | |
1375 | imf_init(&timf, MCAST_EXCLUDE, MCAST_UNDEFINED); | |
1376 | imf = &timf; | |
1377 | } | |
1378 | ||
1379 | /* | |
1380 | * Begin state merge transaction at IGMP layer. | |
1381 | * | |
1382 | * As this particular invocation should not cause any memory | |
1383 | * to be allocated, and there is no opportunity to roll back | |
1384 | * the transaction, it MUST NOT fail. | |
1385 | */ | |
1386 | IGMP_PRINTF(("%s: merge inm state\n", __func__)); | |
1387 | ||
1388 | error = inm_merge(inm, imf); | |
1389 | KASSERT(error == 0, ("%s: failed to merge inm state\n", __func__)); | |
1390 | ||
1391 | IGMP_PRINTF(("%s: doing igmp downcall\n", __func__)); | |
39236c6e | 1392 | error = igmp_change_state(inm, &itp); |
6d2010ae | 1393 | #if IGMP_DEBUG |
0a7de745 | 1394 | if (error) { |
6d2010ae | 1395 | IGMP_PRINTF(("%s: failed igmp downcall\n", __func__)); |
0a7de745 | 1396 | } |
6d2010ae | 1397 | #endif |
0a7de745 A |
1398 | lastref = in_multi_detach(inm); |
1399 | VERIFY(!lastref || (!(inm->inm_debug & IFD_ATTACHED) && | |
1400 | inm->inm_reqcnt == 0)); | |
6d2010ae | 1401 | INM_UNLOCK(inm); |
0a7de745 | 1402 | in_multihead_lock_done(); |
6d2010ae | 1403 | |
0a7de745 A |
1404 | if (lastref) { |
1405 | INM_REMREF(inm); /* for in_multihead list */ | |
1406 | } | |
39236c6e A |
1407 | /* schedule timer now that we've dropped the lock(s) */ |
1408 | igmp_set_timeout(&itp); | |
1409 | ||
0a7de745 | 1410 | return error; |
6d2010ae A |
1411 | } |
1412 | ||
1413 | /* | |
1414 | * Join an IPv4 multicast group in (*,G) exclusive mode. | |
1415 | * The group must be a 224.0.0.0/24 link-scope group. | |
1416 | * This KPI is for legacy kernel consumers only. | |
1417 | */ | |
1418 | struct in_multi * | |
1419 | in_addmulti(struct in_addr *ap, struct ifnet *ifp) | |
1420 | { | |
1421 | struct in_multi *pinm = NULL; | |
1422 | int error; | |
1423 | ||
1424 | KASSERT(IN_LOCAL_GROUP(ntohl(ap->s_addr)), | |
1425 | ("%s: %s not in 224.0.0.0/24\n", __func__, inet_ntoa(*ap))); | |
1426 | ||
1427 | error = in_joingroup(ifp, ap, NULL, &pinm); | |
1428 | VERIFY(pinm != NULL || error != 0); | |
1429 | ||
0a7de745 | 1430 | return pinm; |
6d2010ae A |
1431 | } |
1432 | ||
1433 | /* | |
1434 | * Leave an IPv4 multicast group, assumed to be in exclusive (*,G) mode. | |
1435 | * This KPI is for legacy kernel consumers only. | |
1436 | */ | |
1437 | void | |
1438 | in_delmulti(struct in_multi *inm) | |
1439 | { | |
6d2010ae A |
1440 | (void) in_leavegroup(inm, NULL); |
1441 | } | |
1442 | ||
1443 | /* | |
1444 | * Block or unblock an ASM multicast source on an inpcb. | |
1445 | * This implements the delta-based API described in RFC 3678. | |
1446 | * | |
1447 | * The delta-based API applies only to exclusive-mode memberships. | |
1448 | * An IGMP downcall will be performed. | |
1449 | * | |
1450 | * Return 0 if successful, otherwise return an appropriate error code. | |
1451 | */ | |
1452 | static int | |
1453 | inp_block_unblock_source(struct inpcb *inp, struct sockopt *sopt) | |
1454 | { | |
0a7de745 A |
1455 | struct group_source_req gsr; |
1456 | struct sockaddr_in *gsa, *ssa; | |
1457 | struct ifnet *ifp; | |
1458 | struct in_mfilter *imf; | |
1459 | struct ip_moptions *imo; | |
1460 | struct in_msource *ims; | |
1461 | struct in_multi *inm; | |
1462 | size_t idx; | |
f427ee49 | 1463 | uint8_t fmode; |
0a7de745 A |
1464 | int error, doblock; |
1465 | unsigned int ifindex = 0; | |
1466 | struct igmp_tparams itp; | |
1467 | ||
1468 | bzero(&itp, sizeof(itp)); | |
6d2010ae A |
1469 | ifp = NULL; |
1470 | error = 0; | |
1471 | doblock = 0; | |
1472 | ||
1473 | memset(&gsr, 0, sizeof(struct group_source_req)); | |
5ba3f43e A |
1474 | gsa = (struct sockaddr_in *)&gsr.gsr_group; |
1475 | ssa = (struct sockaddr_in *)&gsr.gsr_source; | |
6d2010ae A |
1476 | |
1477 | switch (sopt->sopt_name) { | |
1478 | case IP_BLOCK_SOURCE: | |
1479 | case IP_UNBLOCK_SOURCE: { | |
0a7de745 | 1480 | struct ip_mreq_source mreqs; |
6d2010ae A |
1481 | |
1482 | error = sooptcopyin(sopt, &mreqs, | |
1483 | sizeof(struct ip_mreq_source), | |
1484 | sizeof(struct ip_mreq_source)); | |
0a7de745 A |
1485 | if (error) { |
1486 | return error; | |
1487 | } | |
6d2010ae | 1488 | |
5ba3f43e A |
1489 | gsa->sin_family = AF_INET; |
1490 | gsa->sin_len = sizeof(struct sockaddr_in); | |
1491 | gsa->sin_addr = mreqs.imr_multiaddr; | |
6d2010ae | 1492 | |
5ba3f43e A |
1493 | ssa->sin_family = AF_INET; |
1494 | ssa->sin_len = sizeof(struct sockaddr_in); | |
1495 | ssa->sin_addr = mreqs.imr_sourceaddr; | |
6d2010ae | 1496 | |
0a7de745 | 1497 | if (!in_nullhost(mreqs.imr_interface)) { |
6d2010ae | 1498 | ifp = ip_multicast_if(&mreqs.imr_interface, &ifindex); |
0a7de745 | 1499 | } |
6d2010ae | 1500 | |
0a7de745 | 1501 | if (sopt->sopt_name == IP_BLOCK_SOURCE) { |
6d2010ae | 1502 | doblock = 1; |
0a7de745 | 1503 | } |
6d2010ae | 1504 | |
39236c6e A |
1505 | IGMP_INET_PRINTF(mreqs.imr_interface, |
1506 | ("%s: imr_interface = %s, ifp = 0x%llx\n", __func__, | |
1507 | _igmp_inet_buf, (uint64_t)VM_KERNEL_ADDRPERM(ifp))); | |
6d2010ae | 1508 | break; |
0a7de745 | 1509 | } |
6d2010ae A |
1510 | |
1511 | case MCAST_BLOCK_SOURCE: | |
1512 | case MCAST_UNBLOCK_SOURCE: | |
1513 | error = sooptcopyin(sopt, &gsr, | |
1514 | sizeof(struct group_source_req), | |
1515 | sizeof(struct group_source_req)); | |
0a7de745 A |
1516 | if (error) { |
1517 | return error; | |
1518 | } | |
6d2010ae | 1519 | |
5ba3f43e | 1520 | if (gsa->sin_family != AF_INET || |
0a7de745 A |
1521 | gsa->sin_len != sizeof(struct sockaddr_in)) { |
1522 | return EINVAL; | |
1523 | } | |
6d2010ae | 1524 | |
5ba3f43e | 1525 | if (ssa->sin_family != AF_INET || |
0a7de745 A |
1526 | ssa->sin_len != sizeof(struct sockaddr_in)) { |
1527 | return EINVAL; | |
1528 | } | |
6d2010ae A |
1529 | |
1530 | ifnet_head_lock_shared(); | |
1531 | if (gsr.gsr_interface == 0 || | |
1532 | (u_int)if_index < gsr.gsr_interface) { | |
1533 | ifnet_head_done(); | |
0a7de745 | 1534 | return EADDRNOTAVAIL; |
6d2010ae A |
1535 | } |
1536 | ||
1537 | ifp = ifindex2ifnet[gsr.gsr_interface]; | |
1538 | ifnet_head_done(); | |
1539 | ||
0a7de745 A |
1540 | if (ifp == NULL) { |
1541 | return EADDRNOTAVAIL; | |
1542 | } | |
6d2010ae | 1543 | |
0a7de745 | 1544 | if (sopt->sopt_name == MCAST_BLOCK_SOURCE) { |
6d2010ae | 1545 | doblock = 1; |
0a7de745 | 1546 | } |
6d2010ae A |
1547 | break; |
1548 | ||
1549 | default: | |
1550 | IGMP_PRINTF(("%s: unknown sopt_name %d\n", | |
1551 | __func__, sopt->sopt_name)); | |
0a7de745 | 1552 | return EOPNOTSUPP; |
6d2010ae A |
1553 | } |
1554 | ||
0a7de745 A |
1555 | if (!IN_MULTICAST(ntohl(gsa->sin_addr.s_addr))) { |
1556 | return EINVAL; | |
1557 | } | |
6d2010ae A |
1558 | |
1559 | /* | |
1560 | * Check if we are actually a member of this group. | |
1561 | */ | |
1562 | imo = inp_findmoptions(inp); | |
0a7de745 A |
1563 | if (imo == NULL) { |
1564 | return ENOMEM; | |
1565 | } | |
6d2010ae A |
1566 | |
1567 | IMO_LOCK(imo); | |
5ba3f43e | 1568 | idx = imo_match_group(imo, ifp, gsa); |
6d2010ae A |
1569 | if (idx == (size_t)-1 || imo->imo_mfilters == NULL) { |
1570 | error = EADDRNOTAVAIL; | |
1571 | goto out_imo_locked; | |
1572 | } | |
1573 | ||
1574 | VERIFY(imo->imo_mfilters != NULL); | |
1575 | imf = &imo->imo_mfilters[idx]; | |
1576 | inm = imo->imo_membership[idx]; | |
1577 | ||
1578 | /* | |
1579 | * Attempting to use the delta-based API on an | |
1580 | * non exclusive-mode membership is an error. | |
1581 | */ | |
1582 | fmode = imf->imf_st[0]; | |
1583 | if (fmode != MCAST_EXCLUDE) { | |
1584 | error = EINVAL; | |
1585 | goto out_imo_locked; | |
1586 | } | |
1587 | ||
1588 | /* | |
1589 | * Deal with error cases up-front: | |
1590 | * Asked to block, but already blocked; or | |
1591 | * Asked to unblock, but nothing to unblock. | |
1592 | * If adding a new block entry, allocate it. | |
1593 | */ | |
5ba3f43e | 1594 | ims = imo_match_source(imo, idx, ssa); |
6d2010ae | 1595 | if ((ims != NULL && doblock) || (ims == NULL && !doblock)) { |
5ba3f43e | 1596 | IGMP_INET_PRINTF(ssa->sin_addr, |
39236c6e A |
1597 | ("%s: source %s %spresent\n", __func__, |
1598 | _igmp_inet_buf, doblock ? "" : "not ")); | |
6d2010ae A |
1599 | error = EADDRNOTAVAIL; |
1600 | goto out_imo_locked; | |
1601 | } | |
1602 | ||
1603 | /* | |
1604 | * Begin state merge transaction at socket layer. | |
1605 | */ | |
1606 | if (doblock) { | |
1607 | IGMP_PRINTF(("%s: %s source\n", __func__, "block")); | |
5ba3f43e | 1608 | ims = imf_graft(imf, fmode, ssa); |
0a7de745 | 1609 | if (ims == NULL) { |
6d2010ae | 1610 | error = ENOMEM; |
0a7de745 | 1611 | } |
6d2010ae A |
1612 | } else { |
1613 | IGMP_PRINTF(("%s: %s source\n", __func__, "allow")); | |
5ba3f43e | 1614 | error = imf_prune(imf, ssa); |
6d2010ae A |
1615 | } |
1616 | ||
1617 | if (error) { | |
1618 | IGMP_PRINTF(("%s: merge imf state failed\n", __func__)); | |
1619 | goto out_imf_rollback; | |
1620 | } | |
1621 | ||
1622 | /* | |
1623 | * Begin state merge transaction at IGMP layer. | |
1624 | */ | |
1625 | INM_LOCK(inm); | |
1626 | IGMP_PRINTF(("%s: merge inm state\n", __func__)); | |
1627 | error = inm_merge(inm, imf); | |
1628 | if (error) { | |
1629 | IGMP_PRINTF(("%s: failed to merge inm state\n", __func__)); | |
1630 | INM_UNLOCK(inm); | |
1631 | goto out_imf_rollback; | |
1632 | } | |
1633 | ||
1634 | IGMP_PRINTF(("%s: doing igmp downcall\n", __func__)); | |
39236c6e | 1635 | error = igmp_change_state(inm, &itp); |
6d2010ae A |
1636 | INM_UNLOCK(inm); |
1637 | #if IGMP_DEBUG | |
0a7de745 | 1638 | if (error) { |
6d2010ae | 1639 | IGMP_PRINTF(("%s: failed igmp downcall\n", __func__)); |
0a7de745 | 1640 | } |
6d2010ae A |
1641 | #endif |
1642 | ||
1643 | out_imf_rollback: | |
0a7de745 | 1644 | if (error) { |
6d2010ae | 1645 | imf_rollback(imf); |
0a7de745 | 1646 | } else { |
6d2010ae | 1647 | imf_commit(imf); |
0a7de745 | 1648 | } |
6d2010ae A |
1649 | |
1650 | imf_reap(imf); | |
1651 | ||
1652 | out_imo_locked: | |
1653 | IMO_UNLOCK(imo); | |
0a7de745 | 1654 | IMO_REMREF(imo); /* from inp_findmoptions() */ |
39236c6e A |
1655 | |
1656 | /* schedule timer now that we've dropped the lock(s) */ | |
1657 | igmp_set_timeout(&itp); | |
1658 | ||
0a7de745 | 1659 | return error; |
6d2010ae A |
1660 | } |
1661 | ||
1662 | /* | |
1663 | * Given an inpcb, return its multicast options structure pointer. | |
1664 | * | |
1665 | * Caller is responsible for locking the inpcb, and releasing the | |
1666 | * extra reference held on the imo, upon a successful return. | |
1667 | */ | |
1668 | static struct ip_moptions * | |
1669 | inp_findmoptions(struct inpcb *inp) | |
1670 | { | |
0a7de745 A |
1671 | struct ip_moptions *imo; |
1672 | struct in_multi **immp; | |
1673 | struct in_mfilter *imfp; | |
1674 | size_t idx; | |
6d2010ae A |
1675 | |
1676 | if ((imo = inp->inp_moptions) != NULL) { | |
0a7de745 A |
1677 | IMO_ADDREF(imo); /* for caller */ |
1678 | return imo; | |
6d2010ae A |
1679 | } |
1680 | ||
f427ee49 | 1681 | imo = ip_allocmoptions(Z_WAITOK); |
0a7de745 A |
1682 | if (imo == NULL) { |
1683 | return NULL; | |
1684 | } | |
6d2010ae | 1685 | |
0a7de745 | 1686 | immp = _MALLOC(sizeof(*immp) * IP_MIN_MEMBERSHIPS, M_IPMOPTS, |
6d2010ae A |
1687 | M_WAITOK | M_ZERO); |
1688 | if (immp == NULL) { | |
1689 | IMO_REMREF(imo); | |
0a7de745 | 1690 | return NULL; |
6d2010ae A |
1691 | } |
1692 | ||
0a7de745 | 1693 | imfp = _MALLOC(sizeof(struct in_mfilter) * IP_MIN_MEMBERSHIPS, |
6d2010ae A |
1694 | M_INMFILTER, M_WAITOK | M_ZERO); |
1695 | if (imfp == NULL) { | |
1696 | _FREE(immp, M_IPMOPTS); | |
1697 | IMO_REMREF(imo); | |
0a7de745 | 1698 | return NULL; |
6d2010ae A |
1699 | } |
1700 | ||
1701 | imo->imo_multicast_ifp = NULL; | |
1702 | imo->imo_multicast_addr.s_addr = INADDR_ANY; | |
1703 | imo->imo_multicast_vif = -1; | |
1704 | imo->imo_multicast_ttl = IP_DEFAULT_MULTICAST_TTL; | |
f427ee49 | 1705 | imo->imo_multicast_loop = !!in_mcast_loop; |
6d2010ae A |
1706 | imo->imo_num_memberships = 0; |
1707 | imo->imo_max_memberships = IP_MIN_MEMBERSHIPS; | |
1708 | imo->imo_membership = immp; | |
1709 | ||
1710 | /* Initialize per-group source filters. */ | |
0a7de745 | 1711 | for (idx = 0; idx < IP_MIN_MEMBERSHIPS; idx++) { |
6d2010ae | 1712 | imf_init(&imfp[idx], MCAST_UNDEFINED, MCAST_EXCLUDE); |
0a7de745 | 1713 | } |
6d2010ae A |
1714 | |
1715 | imo->imo_mfilters = imfp; | |
1716 | inp->inp_moptions = imo; /* keep reference from ip_allocmoptions() */ | |
0a7de745 | 1717 | IMO_ADDREF(imo); /* for caller */ |
6d2010ae | 1718 | |
0a7de745 | 1719 | return imo; |
6d2010ae A |
1720 | } |
1721 | /* | |
1722 | * Atomically get source filters on a socket for an IPv4 multicast group. | |
1723 | */ | |
1724 | static int | |
1725 | inp_get_source_filters(struct inpcb *inp, struct sockopt *sopt) | |
1726 | { | |
0a7de745 A |
1727 | struct __msfilterreq64 msfr = {}, msfr64; |
1728 | struct __msfilterreq32 msfr32; | |
1729 | struct sockaddr_in *gsa; | |
1730 | struct ifnet *ifp; | |
1731 | struct ip_moptions *imo; | |
1732 | struct in_mfilter *imf; | |
1733 | struct ip_msource *ims; | |
1734 | struct in_msource *lims; | |
1735 | struct sockaddr_in *psin; | |
1736 | struct sockaddr_storage *ptss; | |
1737 | struct sockaddr_storage *tss; | |
1738 | int error; | |
f427ee49 A |
1739 | size_t idx; |
1740 | uint32_t nsrcs, ncsrcs; | |
0a7de745 | 1741 | user_addr_t tmp_ptr; |
6d2010ae A |
1742 | |
1743 | imo = inp->inp_moptions; | |
1744 | VERIFY(imo != NULL); | |
1745 | ||
1746 | if (IS_64BIT_PROCESS(current_proc())) { | |
1747 | error = sooptcopyin(sopt, &msfr64, | |
1748 | sizeof(struct __msfilterreq64), | |
1749 | sizeof(struct __msfilterreq64)); | |
0a7de745 A |
1750 | if (error) { |
1751 | return error; | |
1752 | } | |
6d2010ae | 1753 | /* we never use msfr.msfr_srcs; */ |
3e170ce0 | 1754 | memcpy(&msfr, &msfr64, sizeof(msfr64)); |
6d2010ae A |
1755 | } else { |
1756 | error = sooptcopyin(sopt, &msfr32, | |
1757 | sizeof(struct __msfilterreq32), | |
1758 | sizeof(struct __msfilterreq32)); | |
0a7de745 A |
1759 | if (error) { |
1760 | return error; | |
1761 | } | |
6d2010ae | 1762 | /* we never use msfr.msfr_srcs; */ |
3e170ce0 | 1763 | memcpy(&msfr, &msfr32, sizeof(msfr32)); |
6d2010ae A |
1764 | } |
1765 | ||
1766 | ifnet_head_lock_shared(); | |
1767 | if (msfr.msfr_ifindex == 0 || (u_int)if_index < msfr.msfr_ifindex) { | |
1768 | ifnet_head_done(); | |
0a7de745 | 1769 | return EADDRNOTAVAIL; |
6d2010ae A |
1770 | } |
1771 | ||
1772 | ifp = ifindex2ifnet[msfr.msfr_ifindex]; | |
1773 | ifnet_head_done(); | |
1774 | ||
0a7de745 A |
1775 | if (ifp == NULL) { |
1776 | return EADDRNOTAVAIL; | |
1777 | } | |
316670eb A |
1778 | |
1779 | if ((size_t) msfr.msfr_nsrcs > | |
0a7de745 | 1780 | UINT32_MAX / sizeof(struct sockaddr_storage)) { |
39236c6e | 1781 | msfr.msfr_nsrcs = UINT32_MAX / sizeof(struct sockaddr_storage); |
0a7de745 | 1782 | } |
316670eb | 1783 | |
0a7de745 | 1784 | if (msfr.msfr_nsrcs > in_mcast_maxsocksrc) { |
6d2010ae | 1785 | msfr.msfr_nsrcs = in_mcast_maxsocksrc; |
0a7de745 | 1786 | } |
6d2010ae A |
1787 | |
1788 | IMO_LOCK(imo); | |
1789 | /* | |
1790 | * Lookup group on the socket. | |
1791 | */ | |
5ba3f43e A |
1792 | gsa = (struct sockaddr_in *)&msfr.msfr_group; |
1793 | ||
1794 | idx = imo_match_group(imo, ifp, gsa); | |
6d2010ae A |
1795 | if (idx == (size_t)-1 || imo->imo_mfilters == NULL) { |
1796 | IMO_UNLOCK(imo); | |
0a7de745 | 1797 | return EADDRNOTAVAIL; |
6d2010ae A |
1798 | } |
1799 | imf = &imo->imo_mfilters[idx]; | |
1800 | ||
1801 | /* | |
1802 | * Ignore memberships which are in limbo. | |
1803 | */ | |
1804 | if (imf->imf_st[1] == MCAST_UNDEFINED) { | |
1805 | IMO_UNLOCK(imo); | |
0a7de745 | 1806 | return EAGAIN; |
6d2010ae A |
1807 | } |
1808 | msfr.msfr_fmode = imf->imf_st[1]; | |
1809 | ||
1810 | /* | |
1811 | * If the user specified a buffer, copy out the source filter | |
1812 | * entries to userland gracefully. | |
1813 | * We only copy out the number of entries which userland | |
1814 | * has asked for, but we always tell userland how big the | |
1815 | * buffer really needs to be. | |
1816 | */ | |
1817 | ||
0a7de745 | 1818 | if (IS_64BIT_PROCESS(current_proc())) { |
f427ee49 | 1819 | tmp_ptr = CAST_USER_ADDR_T(msfr64.msfr_srcs); |
0a7de745 | 1820 | } else { |
6d2010ae | 1821 | tmp_ptr = CAST_USER_ADDR_T(msfr32.msfr_srcs); |
0a7de745 | 1822 | } |
6d2010ae A |
1823 | |
1824 | tss = NULL; | |
1825 | if (tmp_ptr != USER_ADDR_NULL && msfr.msfr_nsrcs > 0) { | |
316670eb | 1826 | tss = _MALLOC((size_t) msfr.msfr_nsrcs * sizeof(*tss), |
6d2010ae A |
1827 | M_TEMP, M_WAITOK | M_ZERO); |
1828 | if (tss == NULL) { | |
1829 | IMO_UNLOCK(imo); | |
0a7de745 | 1830 | return ENOBUFS; |
6d2010ae A |
1831 | } |
1832 | } | |
1833 | ||
1834 | /* | |
1835 | * Count number of sources in-mode at t0. | |
1836 | * If buffer space exists and remains, copy out source entries. | |
1837 | */ | |
1838 | nsrcs = msfr.msfr_nsrcs; | |
1839 | ncsrcs = 0; | |
1840 | ptss = tss; | |
1841 | RB_FOREACH(ims, ip_msource_tree, &imf->imf_sources) { | |
1842 | lims = (struct in_msource *)ims; | |
1843 | if (lims->imsl_st[0] == MCAST_UNDEFINED || | |
0a7de745 | 1844 | lims->imsl_st[0] != imf->imf_st[0]) { |
6d2010ae | 1845 | continue; |
0a7de745 | 1846 | } |
6d2010ae A |
1847 | if (tss != NULL && nsrcs > 0) { |
1848 | psin = (struct sockaddr_in *)ptss; | |
1849 | psin->sin_family = AF_INET; | |
1850 | psin->sin_len = sizeof(struct sockaddr_in); | |
1851 | psin->sin_addr.s_addr = htonl(lims->ims_haddr); | |
1852 | psin->sin_port = 0; | |
1853 | ++ptss; | |
1854 | --nsrcs; | |
1855 | ++ncsrcs; | |
1856 | } | |
1857 | } | |
1858 | ||
1859 | IMO_UNLOCK(imo); | |
1860 | ||
1861 | if (tss != NULL) { | |
f427ee49 | 1862 | error = copyout(tss, CAST_USER_ADDR_T(tmp_ptr), ncsrcs * sizeof(*tss)); |
6d2010ae | 1863 | FREE(tss, M_TEMP); |
0a7de745 A |
1864 | if (error) { |
1865 | return error; | |
1866 | } | |
6d2010ae A |
1867 | } |
1868 | ||
1869 | msfr.msfr_nsrcs = ncsrcs; | |
1870 | if (IS_64BIT_PROCESS(current_proc())) { | |
1871 | msfr64.msfr_ifindex = msfr.msfr_ifindex; | |
1872 | msfr64.msfr_fmode = msfr.msfr_fmode; | |
1873 | msfr64.msfr_nsrcs = msfr.msfr_nsrcs; | |
1874 | memcpy(&msfr64.msfr_group, &msfr.msfr_group, | |
1875 | sizeof(struct sockaddr_storage)); | |
1876 | error = sooptcopyout(sopt, &msfr64, | |
1877 | sizeof(struct __msfilterreq64)); | |
1878 | } else { | |
1879 | msfr32.msfr_ifindex = msfr.msfr_ifindex; | |
1880 | msfr32.msfr_fmode = msfr.msfr_fmode; | |
1881 | msfr32.msfr_nsrcs = msfr.msfr_nsrcs; | |
3e170ce0 | 1882 | memcpy(&msfr32.msfr_group, &msfr.msfr_group, |
6d2010ae A |
1883 | sizeof(struct sockaddr_storage)); |
1884 | error = sooptcopyout(sopt, &msfr32, | |
1885 | sizeof(struct __msfilterreq32)); | |
1886 | } | |
1887 | ||
0a7de745 | 1888 | return error; |
6d2010ae A |
1889 | } |
1890 | ||
1891 | /* | |
1892 | * Return the IP multicast options in response to user getsockopt(). | |
1893 | */ | |
1894 | int | |
1895 | inp_getmoptions(struct inpcb *inp, struct sockopt *sopt) | |
1896 | { | |
0a7de745 A |
1897 | struct ip_mreqn mreqn; |
1898 | struct ip_moptions *imo; | |
1899 | struct ifnet *ifp; | |
1900 | struct in_ifaddr *ia; | |
1901 | int error, optval; | |
1902 | unsigned int ifindex; | |
1903 | u_char coptval; | |
6d2010ae A |
1904 | |
1905 | imo = inp->inp_moptions; | |
1906 | /* | |
1907 | * If socket is neither of type SOCK_RAW or SOCK_DGRAM, | |
1908 | * or is a divert socket, reject it. | |
1909 | */ | |
39236c6e A |
1910 | if (SOCK_PROTO(inp->inp_socket) == IPPROTO_DIVERT || |
1911 | (SOCK_TYPE(inp->inp_socket) != SOCK_RAW && | |
1912 | SOCK_TYPE(inp->inp_socket) != SOCK_DGRAM)) { | |
0a7de745 | 1913 | return EOPNOTSUPP; |
6d2010ae A |
1914 | } |
1915 | ||
1916 | error = 0; | |
1917 | switch (sopt->sopt_name) { | |
6d2010ae A |
1918 | case IP_MULTICAST_IF: |
1919 | memset(&mreqn, 0, sizeof(struct ip_mreqn)); | |
1920 | if (imo != NULL) { | |
1921 | IMO_LOCK(imo); | |
1922 | ifp = imo->imo_multicast_ifp; | |
1923 | if (!in_nullhost(imo->imo_multicast_addr)) { | |
1924 | mreqn.imr_address = imo->imo_multicast_addr; | |
1925 | } else if (ifp != NULL) { | |
1926 | mreqn.imr_ifindex = ifp->if_index; | |
1927 | IFP_TO_IA(ifp, ia); | |
1928 | if (ia != NULL) { | |
1929 | IFA_LOCK_SPIN(&ia->ia_ifa); | |
1930 | mreqn.imr_address = | |
1931 | IA_SIN(ia)->sin_addr; | |
1932 | IFA_UNLOCK(&ia->ia_ifa); | |
1933 | IFA_REMREF(&ia->ia_ifa); | |
1934 | } | |
1935 | } | |
1936 | IMO_UNLOCK(imo); | |
1937 | } | |
1938 | if (sopt->sopt_valsize == sizeof(struct ip_mreqn)) { | |
1939 | error = sooptcopyout(sopt, &mreqn, | |
1940 | sizeof(struct ip_mreqn)); | |
1941 | } else { | |
1942 | error = sooptcopyout(sopt, &mreqn.imr_address, | |
1943 | sizeof(struct in_addr)); | |
1944 | } | |
1945 | break; | |
1946 | ||
1947 | case IP_MULTICAST_IFINDEX: | |
0a7de745 | 1948 | if (imo != NULL) { |
6d2010ae | 1949 | IMO_LOCK(imo); |
0a7de745 | 1950 | } |
6d2010ae A |
1951 | if (imo == NULL || imo->imo_multicast_ifp == NULL) { |
1952 | ifindex = 0; | |
1953 | } else { | |
1954 | ifindex = imo->imo_multicast_ifp->if_index; | |
1955 | } | |
0a7de745 | 1956 | if (imo != NULL) { |
6d2010ae | 1957 | IMO_UNLOCK(imo); |
0a7de745 A |
1958 | } |
1959 | error = sooptcopyout(sopt, &ifindex, sizeof(ifindex)); | |
6d2010ae A |
1960 | break; |
1961 | ||
1962 | case IP_MULTICAST_TTL: | |
0a7de745 | 1963 | if (imo == NULL) { |
6d2010ae | 1964 | optval = coptval = IP_DEFAULT_MULTICAST_TTL; |
0a7de745 | 1965 | } else { |
6d2010ae A |
1966 | IMO_LOCK(imo); |
1967 | optval = coptval = imo->imo_multicast_ttl; | |
1968 | IMO_UNLOCK(imo); | |
1969 | } | |
0a7de745 | 1970 | if (sopt->sopt_valsize == sizeof(u_char)) { |
6d2010ae | 1971 | error = sooptcopyout(sopt, &coptval, sizeof(u_char)); |
0a7de745 | 1972 | } else { |
6d2010ae | 1973 | error = sooptcopyout(sopt, &optval, sizeof(int)); |
0a7de745 | 1974 | } |
6d2010ae A |
1975 | break; |
1976 | ||
1977 | case IP_MULTICAST_LOOP: | |
0a7de745 | 1978 | if (imo == 0) { |
6d2010ae | 1979 | optval = coptval = IP_DEFAULT_MULTICAST_LOOP; |
0a7de745 | 1980 | } else { |
6d2010ae A |
1981 | IMO_LOCK(imo); |
1982 | optval = coptval = imo->imo_multicast_loop; | |
1983 | IMO_UNLOCK(imo); | |
1984 | } | |
0a7de745 | 1985 | if (sopt->sopt_valsize == sizeof(u_char)) { |
6d2010ae | 1986 | error = sooptcopyout(sopt, &coptval, sizeof(u_char)); |
0a7de745 | 1987 | } else { |
6d2010ae | 1988 | error = sooptcopyout(sopt, &optval, sizeof(int)); |
0a7de745 | 1989 | } |
6d2010ae A |
1990 | break; |
1991 | ||
1992 | case IP_MSFILTER: | |
1993 | if (imo == NULL) { | |
1994 | error = EADDRNOTAVAIL; | |
1995 | } else { | |
1996 | error = inp_get_source_filters(inp, sopt); | |
1997 | } | |
1998 | break; | |
1999 | ||
2000 | default: | |
2001 | error = ENOPROTOOPT; | |
2002 | break; | |
2003 | } | |
2004 | ||
0a7de745 | 2005 | return error; |
6d2010ae A |
2006 | } |
2007 | ||
2008 | /* | |
2009 | * Look up the ifnet to use for a multicast group membership, | |
2010 | * given the IPv4 address of an interface, and the IPv4 group address. | |
2011 | * | |
2012 | * This routine exists to support legacy multicast applications | |
2013 | * which do not understand that multicast memberships are scoped to | |
2014 | * specific physical links in the networking stack, or which need | |
2015 | * to join link-scope groups before IPv4 addresses are configured. | |
2016 | * | |
2017 | * If inp is non-NULL and is bound to an interface, use this socket's | |
2018 | * inp_boundif for any required routing table lookup. | |
2019 | * | |
2020 | * If the route lookup fails, attempt to use the first non-loopback | |
2021 | * interface with multicast capability in the system as a | |
2022 | * last resort. The legacy IPv4 ASM API requires that we do | |
2023 | * this in order to allow groups to be joined when the routing | |
2024 | * table has not yet been populated during boot. | |
2025 | * | |
2026 | * Returns NULL if no ifp could be found. | |
2027 | * | |
2028 | */ | |
2029 | static struct ifnet * | |
2030 | inp_lookup_mcast_ifp(const struct inpcb *inp, | |
2031 | const struct sockaddr_in *gsin, const struct in_addr ina) | |
2032 | { | |
0a7de745 A |
2033 | struct ifnet *ifp; |
2034 | unsigned int ifindex = 0; | |
6d2010ae A |
2035 | |
2036 | VERIFY(gsin->sin_family == AF_INET); | |
2037 | VERIFY(IN_MULTICAST(ntohl(gsin->sin_addr.s_addr))); | |
2038 | ||
2039 | ifp = NULL; | |
2040 | if (!in_nullhost(ina)) { | |
2041 | struct in_addr new_ina; | |
2042 | memcpy(&new_ina, &ina, sizeof(struct in_addr)); | |
2043 | ifp = ip_multicast_if(&new_ina, &ifindex); | |
2044 | } else { | |
2045 | struct route ro; | |
2046 | unsigned int ifscope = IFSCOPE_NONE; | |
2047 | ||
0a7de745 | 2048 | if (inp != NULL && (inp->inp_flags & INP_BOUND_IF)) { |
316670eb | 2049 | ifscope = inp->inp_boundifp->if_index; |
0a7de745 | 2050 | } |
6d2010ae | 2051 | |
0a7de745 | 2052 | bzero(&ro, sizeof(ro)); |
6d2010ae A |
2053 | memcpy(&ro.ro_dst, gsin, sizeof(struct sockaddr_in)); |
2054 | rtalloc_scoped_ign(&ro, 0, ifscope); | |
2055 | if (ro.ro_rt != NULL) { | |
2056 | ifp = ro.ro_rt->rt_ifp; | |
2057 | VERIFY(ifp != NULL); | |
6d2010ae A |
2058 | } else { |
2059 | struct in_ifaddr *ia; | |
2060 | struct ifnet *mifp; | |
2061 | ||
2062 | mifp = NULL; | |
2063 | lck_rw_lock_shared(in_ifaddr_rwlock); | |
2064 | TAILQ_FOREACH(ia, &in_ifaddrhead, ia_link) { | |
2065 | IFA_LOCK_SPIN(&ia->ia_ifa); | |
2066 | mifp = ia->ia_ifp; | |
2067 | IFA_UNLOCK(&ia->ia_ifa); | |
2068 | if (!(mifp->if_flags & IFF_LOOPBACK) && | |
0a7de745 | 2069 | (mifp->if_flags & IFF_MULTICAST)) { |
6d2010ae A |
2070 | ifp = mifp; |
2071 | break; | |
2072 | } | |
2073 | } | |
2074 | lck_rw_done(in_ifaddr_rwlock); | |
2075 | } | |
39236c6e | 2076 | ROUTE_RELEASE(&ro); |
6d2010ae A |
2077 | } |
2078 | ||
0a7de745 | 2079 | return ifp; |
6d2010ae A |
2080 | } |
2081 | ||
2082 | /* | |
2083 | * Join an IPv4 multicast group, possibly with a source. | |
2084 | * | |
2085 | * NB: sopt->sopt_val might point to the kernel address space. This means that | |
2086 | * we were called by the IPv6 stack due to the presence of an IPv6 v4 mapped | |
2087 | * address. In this scenario, sopt_p points to kernproc and sooptcopyin() will | |
2088 | * just issue an in-kernel memcpy. | |
2089 | */ | |
2090 | int | |
2091 | inp_join_group(struct inpcb *inp, struct sockopt *sopt) | |
2092 | { | |
0a7de745 A |
2093 | struct group_source_req gsr; |
2094 | struct sockaddr_in *gsa, *ssa; | |
2095 | struct ifnet *ifp; | |
2096 | struct in_mfilter *imf; | |
2097 | struct ip_moptions *imo; | |
2098 | struct in_multi *inm = NULL; | |
2099 | struct in_msource *lims; | |
2100 | size_t idx; | |
2101 | int error, is_new; | |
2102 | struct igmp_tparams itp; | |
2103 | ||
2104 | bzero(&itp, sizeof(itp)); | |
6d2010ae A |
2105 | ifp = NULL; |
2106 | imf = NULL; | |
2107 | error = 0; | |
2108 | is_new = 0; | |
2109 | ||
2110 | memset(&gsr, 0, sizeof(struct group_source_req)); | |
5ba3f43e A |
2111 | gsa = (struct sockaddr_in *)&gsr.gsr_group; |
2112 | gsa->sin_family = AF_UNSPEC; | |
2113 | ssa = (struct sockaddr_in *)&gsr.gsr_source; | |
2114 | ssa->sin_family = AF_UNSPEC; | |
6d2010ae A |
2115 | |
2116 | switch (sopt->sopt_name) { | |
2117 | case IP_ADD_MEMBERSHIP: | |
2118 | case IP_ADD_SOURCE_MEMBERSHIP: { | |
0a7de745 | 2119 | struct ip_mreq_source mreqs; |
6d2010ae A |
2120 | |
2121 | if (sopt->sopt_name == IP_ADD_MEMBERSHIP) { | |
2122 | error = sooptcopyin(sopt, &mreqs, | |
2123 | sizeof(struct ip_mreq), | |
2124 | sizeof(struct ip_mreq)); | |
2125 | /* | |
2126 | * Do argument switcharoo from ip_mreq into | |
2127 | * ip_mreq_source to avoid using two instances. | |
2128 | */ | |
2129 | mreqs.imr_interface = mreqs.imr_sourceaddr; | |
2130 | mreqs.imr_sourceaddr.s_addr = INADDR_ANY; | |
2131 | } else if (sopt->sopt_name == IP_ADD_SOURCE_MEMBERSHIP) { | |
2132 | error = sooptcopyin(sopt, &mreqs, | |
2133 | sizeof(struct ip_mreq_source), | |
2134 | sizeof(struct ip_mreq_source)); | |
2135 | } | |
2136 | if (error) { | |
2137 | IGMP_PRINTF(("%s: error copyin IP_ADD_MEMBERSHIP/" | |
2138 | "IP_ADD_SOURCE_MEMBERSHIP %d err=%d\n", | |
2139 | __func__, sopt->sopt_name, error)); | |
0a7de745 | 2140 | return error; |
6d2010ae A |
2141 | } |
2142 | ||
5ba3f43e A |
2143 | gsa->sin_family = AF_INET; |
2144 | gsa->sin_len = sizeof(struct sockaddr_in); | |
2145 | gsa->sin_addr = mreqs.imr_multiaddr; | |
6d2010ae A |
2146 | |
2147 | if (sopt->sopt_name == IP_ADD_SOURCE_MEMBERSHIP) { | |
5ba3f43e A |
2148 | ssa->sin_family = AF_INET; |
2149 | ssa->sin_len = sizeof(struct sockaddr_in); | |
2150 | ssa->sin_addr = mreqs.imr_sourceaddr; | |
6d2010ae A |
2151 | } |
2152 | ||
0a7de745 A |
2153 | if (!IN_MULTICAST(ntohl(gsa->sin_addr.s_addr))) { |
2154 | return EINVAL; | |
2155 | } | |
6d2010ae | 2156 | |
5ba3f43e | 2157 | ifp = inp_lookup_mcast_ifp(inp, gsa, mreqs.imr_interface); |
39236c6e A |
2158 | IGMP_INET_PRINTF(mreqs.imr_interface, |
2159 | ("%s: imr_interface = %s, ifp = 0x%llx\n", __func__, | |
2160 | _igmp_inet_buf, (uint64_t)VM_KERNEL_ADDRPERM(ifp))); | |
6d2010ae A |
2161 | break; |
2162 | } | |
2163 | ||
2164 | case MCAST_JOIN_GROUP: | |
2165 | case MCAST_JOIN_SOURCE_GROUP: | |
2166 | if (sopt->sopt_name == MCAST_JOIN_GROUP) { | |
2167 | error = sooptcopyin(sopt, &gsr, | |
2168 | sizeof(struct group_req), | |
2169 | sizeof(struct group_req)); | |
2170 | } else if (sopt->sopt_name == MCAST_JOIN_SOURCE_GROUP) { | |
2171 | error = sooptcopyin(sopt, &gsr, | |
2172 | sizeof(struct group_source_req), | |
2173 | sizeof(struct group_source_req)); | |
2174 | } | |
0a7de745 A |
2175 | if (error) { |
2176 | return error; | |
2177 | } | |
6d2010ae | 2178 | |
5ba3f43e | 2179 | if (gsa->sin_family != AF_INET || |
0a7de745 A |
2180 | gsa->sin_len != sizeof(struct sockaddr_in)) { |
2181 | return EINVAL; | |
2182 | } | |
6d2010ae A |
2183 | |
2184 | /* | |
2185 | * Overwrite the port field if present, as the sockaddr | |
2186 | * being copied in may be matched with a binary comparison. | |
2187 | */ | |
5ba3f43e | 2188 | gsa->sin_port = 0; |
6d2010ae | 2189 | if (sopt->sopt_name == MCAST_JOIN_SOURCE_GROUP) { |
5ba3f43e | 2190 | if (ssa->sin_family != AF_INET || |
0a7de745 A |
2191 | ssa->sin_len != sizeof(struct sockaddr_in)) { |
2192 | return EINVAL; | |
2193 | } | |
5ba3f43e | 2194 | ssa->sin_port = 0; |
6d2010ae A |
2195 | } |
2196 | ||
0a7de745 A |
2197 | if (!IN_MULTICAST(ntohl(gsa->sin_addr.s_addr))) { |
2198 | return EINVAL; | |
2199 | } | |
6d2010ae A |
2200 | |
2201 | ifnet_head_lock_shared(); | |
2202 | if (gsr.gsr_interface == 0 || | |
2203 | (u_int)if_index < gsr.gsr_interface) { | |
2204 | ifnet_head_done(); | |
0a7de745 | 2205 | return EADDRNOTAVAIL; |
6d2010ae A |
2206 | } |
2207 | ifp = ifindex2ifnet[gsr.gsr_interface]; | |
2208 | ifnet_head_done(); | |
2209 | ||
2210 | break; | |
2211 | ||
2212 | default: | |
2213 | IGMP_PRINTF(("%s: unknown sopt_name %d\n", | |
2214 | __func__, sopt->sopt_name)); | |
0a7de745 | 2215 | return EOPNOTSUPP; |
6d2010ae A |
2216 | } |
2217 | ||
0a7de745 A |
2218 | if (ifp == NULL || (ifp->if_flags & IFF_MULTICAST) == 0) { |
2219 | return EADDRNOTAVAIL; | |
2220 | } | |
6d2010ae | 2221 | |
5ba3f43e A |
2222 | INC_ATOMIC_INT64_LIM(net_api_stats.nas_socket_mcast_join_total); |
2223 | /* | |
2224 | * TBD: revisit the criteria for non-OS initiated joins | |
2225 | */ | |
2226 | if (inp->inp_lport == htons(5353)) { | |
2227 | INC_ATOMIC_INT64_LIM(net_api_stats.nas_socket_mcast_join_os_total); | |
2228 | } | |
2229 | ||
6d2010ae | 2230 | imo = inp_findmoptions(inp); |
0a7de745 A |
2231 | if (imo == NULL) { |
2232 | return ENOMEM; | |
2233 | } | |
6d2010ae A |
2234 | |
2235 | IMO_LOCK(imo); | |
5ba3f43e | 2236 | idx = imo_match_group(imo, ifp, gsa); |
6d2010ae A |
2237 | if (idx == (size_t)-1) { |
2238 | is_new = 1; | |
2239 | } else { | |
2240 | inm = imo->imo_membership[idx]; | |
2241 | imf = &imo->imo_mfilters[idx]; | |
5ba3f43e | 2242 | if (ssa->sin_family != AF_UNSPEC) { |
6d2010ae A |
2243 | /* |
2244 | * MCAST_JOIN_SOURCE_GROUP on an exclusive membership | |
2245 | * is an error. On an existing inclusive membership, | |
2246 | * it just adds the source to the filter list. | |
2247 | */ | |
2248 | if (imf->imf_st[1] != MCAST_INCLUDE) { | |
2249 | error = EINVAL; | |
2250 | goto out_imo_locked; | |
2251 | } | |
2252 | /* | |
2253 | * Throw out duplicates. | |
2254 | * | |
2255 | * XXX FIXME: This makes a naive assumption that | |
2256 | * even if entries exist for *ssa in this imf, | |
2257 | * they will be rejected as dupes, even if they | |
2258 | * are not valid in the current mode (in-mode). | |
2259 | * | |
2260 | * in_msource is transactioned just as for anything | |
2261 | * else in SSM -- but note naive use of inm_graft() | |
2262 | * below for allocating new filter entries. | |
2263 | * | |
2264 | * This is only an issue if someone mixes the | |
2265 | * full-state SSM API with the delta-based API, | |
2266 | * which is discouraged in the relevant RFCs. | |
2267 | */ | |
5ba3f43e | 2268 | lims = imo_match_source(imo, idx, ssa); |
6d2010ae | 2269 | if (lims != NULL /*&& |
0a7de745 | 2270 | * lims->imsl_st[1] == MCAST_INCLUDE*/) { |
6d2010ae A |
2271 | error = EADDRNOTAVAIL; |
2272 | goto out_imo_locked; | |
2273 | } | |
2274 | } else { | |
2275 | /* | |
2276 | * MCAST_JOIN_GROUP on an existing exclusive | |
2277 | * membership is an error; return EADDRINUSE | |
2278 | * to preserve 4.4BSD API idempotence, and | |
2279 | * avoid tedious detour to code below. | |
2280 | * NOTE: This is bending RFC 3678 a bit. | |
2281 | * | |
2282 | * On an existing inclusive membership, this is also | |
2283 | * an error; if you want to change filter mode, | |
2284 | * you must use the userland API setsourcefilter(). | |
2285 | * XXX We don't reject this for imf in UNDEFINED | |
2286 | * state at t1, because allocation of a filter | |
2287 | * is atomic with allocation of a membership. | |
2288 | */ | |
2289 | error = EINVAL; | |
2290 | /* See comments above for EADDRINUSE */ | |
0a7de745 | 2291 | if (imf->imf_st[1] == MCAST_EXCLUDE) { |
6d2010ae | 2292 | error = EADDRINUSE; |
0a7de745 | 2293 | } |
6d2010ae A |
2294 | goto out_imo_locked; |
2295 | } | |
2296 | } | |
2297 | ||
2298 | /* | |
2299 | * Begin state merge transaction at socket layer. | |
2300 | */ | |
2301 | ||
2302 | if (is_new) { | |
2303 | if (imo->imo_num_memberships == imo->imo_max_memberships) { | |
2304 | error = imo_grow(imo, 0); | |
0a7de745 | 2305 | if (error) { |
6d2010ae | 2306 | goto out_imo_locked; |
0a7de745 | 2307 | } |
6d2010ae A |
2308 | } |
2309 | /* | |
2310 | * Allocate the new slot upfront so we can deal with | |
2311 | * grafting the new source filter in same code path | |
2312 | * as for join-source on existing membership. | |
2313 | */ | |
2314 | idx = imo->imo_num_memberships; | |
2315 | imo->imo_membership[idx] = NULL; | |
2316 | imo->imo_num_memberships++; | |
2317 | VERIFY(imo->imo_mfilters != NULL); | |
2318 | imf = &imo->imo_mfilters[idx]; | |
2319 | VERIFY(RB_EMPTY(&imf->imf_sources)); | |
2320 | } | |
2321 | ||
2322 | /* | |
2323 | * Graft new source into filter list for this inpcb's | |
2324 | * membership of the group. The in_multi may not have | |
2325 | * been allocated yet if this is a new membership, however, | |
2326 | * the in_mfilter slot will be allocated and must be initialized. | |
2327 | */ | |
5ba3f43e | 2328 | if (ssa->sin_family != AF_UNSPEC) { |
6d2010ae A |
2329 | /* Membership starts in IN mode */ |
2330 | if (is_new) { | |
2331 | IGMP_PRINTF(("%s: new join w/source\n", __func__)); | |
2332 | imf_init(imf, MCAST_UNDEFINED, MCAST_INCLUDE); | |
2333 | } else { | |
2334 | IGMP_PRINTF(("%s: %s source\n", __func__, "allow")); | |
2335 | } | |
5ba3f43e | 2336 | lims = imf_graft(imf, MCAST_INCLUDE, ssa); |
6d2010ae A |
2337 | if (lims == NULL) { |
2338 | IGMP_PRINTF(("%s: merge imf state failed\n", | |
2339 | __func__)); | |
2340 | error = ENOMEM; | |
2341 | goto out_imo_free; | |
2342 | } | |
2343 | } else { | |
2344 | /* No address specified; Membership starts in EX mode */ | |
2345 | if (is_new) { | |
2346 | IGMP_PRINTF(("%s: new join w/o source\n", __func__)); | |
2347 | imf_init(imf, MCAST_UNDEFINED, MCAST_EXCLUDE); | |
2348 | } | |
2349 | } | |
2350 | ||
2351 | /* | |
2352 | * Begin state merge transaction at IGMP layer. | |
2353 | */ | |
6d2010ae | 2354 | if (is_new) { |
39037602 A |
2355 | /* |
2356 | * Unlock socket as we may end up calling ifnet_ioctl() to join (or leave) | |
2357 | * the multicast group and we run the risk of a lock ordering issue | |
2358 | * if the ifnet thread calls into the socket layer to acquire the pcb list | |
2359 | * lock while the input thread delivers multicast packets | |
2360 | */ | |
2361 | IMO_ADDREF_LOCKED(imo); | |
2362 | IMO_UNLOCK(imo); | |
2363 | socket_unlock(inp->inp_socket, 0); | |
2364 | ||
6d2010ae | 2365 | VERIFY(inm == NULL); |
5ba3f43e | 2366 | error = in_joingroup(ifp, &gsa->sin_addr, imf, &inm); |
39037602 A |
2367 | |
2368 | socket_lock(inp->inp_socket, 0); | |
2369 | IMO_REMREF(imo); | |
2370 | IMO_LOCK(imo); | |
2371 | ||
6d2010ae | 2372 | VERIFY(inm != NULL || error != 0); |
0a7de745 | 2373 | if (error) { |
6d2010ae | 2374 | goto out_imo_free; |
0a7de745 A |
2375 | } |
2376 | imo->imo_membership[idx] = inm; /* from in_joingroup() */ | |
6d2010ae A |
2377 | } else { |
2378 | IGMP_PRINTF(("%s: merge inm state\n", __func__)); | |
2379 | INM_LOCK(inm); | |
2380 | error = inm_merge(inm, imf); | |
2381 | if (error) { | |
2382 | IGMP_PRINTF(("%s: failed to merge inm state\n", | |
2383 | __func__)); | |
2384 | INM_UNLOCK(inm); | |
2385 | goto out_imf_rollback; | |
2386 | } | |
2387 | IGMP_PRINTF(("%s: doing igmp downcall\n", __func__)); | |
39236c6e | 2388 | error = igmp_change_state(inm, &itp); |
6d2010ae A |
2389 | INM_UNLOCK(inm); |
2390 | if (error) { | |
2391 | IGMP_PRINTF(("%s: failed igmp downcall\n", | |
2392 | __func__)); | |
2393 | goto out_imf_rollback; | |
2394 | } | |
2395 | } | |
2396 | ||
2397 | out_imf_rollback: | |
2398 | if (error) { | |
2399 | imf_rollback(imf); | |
0a7de745 | 2400 | if (is_new) { |
6d2010ae | 2401 | imf_purge(imf); |
0a7de745 | 2402 | } else { |
6d2010ae | 2403 | imf_reap(imf); |
0a7de745 | 2404 | } |
6d2010ae A |
2405 | } else { |
2406 | imf_commit(imf); | |
2407 | } | |
2408 | ||
2409 | out_imo_free: | |
2410 | if (error && is_new) { | |
2411 | VERIFY(inm == NULL); | |
2412 | imo->imo_membership[idx] = NULL; | |
2413 | --imo->imo_num_memberships; | |
2414 | } | |
2415 | ||
2416 | out_imo_locked: | |
2417 | IMO_UNLOCK(imo); | |
0a7de745 | 2418 | IMO_REMREF(imo); /* from inp_findmoptions() */ |
39236c6e A |
2419 | |
2420 | /* schedule timer now that we've dropped the lock(s) */ | |
2421 | igmp_set_timeout(&itp); | |
2422 | ||
0a7de745 | 2423 | return error; |
6d2010ae A |
2424 | } |
2425 | ||
2426 | /* | |
2427 | * Leave an IPv4 multicast group on an inpcb, possibly with a source. | |
2428 | * | |
2429 | * NB: sopt->sopt_val might point to the kernel address space. Refer to the | |
2430 | * block comment on top of inp_join_group() for more information. | |
2431 | */ | |
2432 | int | |
2433 | inp_leave_group(struct inpcb *inp, struct sockopt *sopt) | |
2434 | { | |
0a7de745 A |
2435 | struct group_source_req gsr; |
2436 | struct ip_mreq_source mreqs; | |
2437 | struct sockaddr_in *gsa, *ssa; | |
2438 | struct ifnet *ifp; | |
2439 | struct in_mfilter *imf; | |
2440 | struct ip_moptions *imo; | |
2441 | struct in_msource *ims; | |
2442 | struct in_multi *inm = NULL; | |
2443 | size_t idx; | |
2444 | int error, is_final; | |
2445 | unsigned int ifindex = 0; | |
2446 | struct igmp_tparams itp; | |
2447 | ||
2448 | bzero(&itp, sizeof(itp)); | |
6d2010ae A |
2449 | ifp = NULL; |
2450 | error = 0; | |
2451 | is_final = 1; | |
2452 | ||
2453 | memset(&gsr, 0, sizeof(struct group_source_req)); | |
5ba3f43e A |
2454 | gsa = (struct sockaddr_in *)&gsr.gsr_group; |
2455 | ssa = (struct sockaddr_in *)&gsr.gsr_source; | |
6d2010ae A |
2456 | |
2457 | switch (sopt->sopt_name) { | |
2458 | case IP_DROP_MEMBERSHIP: | |
2459 | case IP_DROP_SOURCE_MEMBERSHIP: | |
2460 | if (sopt->sopt_name == IP_DROP_MEMBERSHIP) { | |
2461 | error = sooptcopyin(sopt, &mreqs, | |
2462 | sizeof(struct ip_mreq), | |
2463 | sizeof(struct ip_mreq)); | |
2464 | /* | |
2465 | * Swap interface and sourceaddr arguments, | |
2466 | * as ip_mreq and ip_mreq_source are laid | |
2467 | * out differently. | |
2468 | */ | |
2469 | mreqs.imr_interface = mreqs.imr_sourceaddr; | |
2470 | mreqs.imr_sourceaddr.s_addr = INADDR_ANY; | |
2471 | } else if (sopt->sopt_name == IP_DROP_SOURCE_MEMBERSHIP) { | |
2472 | error = sooptcopyin(sopt, &mreqs, | |
2473 | sizeof(struct ip_mreq_source), | |
2474 | sizeof(struct ip_mreq_source)); | |
2475 | } | |
0a7de745 A |
2476 | if (error) { |
2477 | return error; | |
2478 | } | |
6d2010ae | 2479 | |
5ba3f43e A |
2480 | gsa->sin_family = AF_INET; |
2481 | gsa->sin_len = sizeof(struct sockaddr_in); | |
2482 | gsa->sin_addr = mreqs.imr_multiaddr; | |
6d2010ae A |
2483 | |
2484 | if (sopt->sopt_name == IP_DROP_SOURCE_MEMBERSHIP) { | |
5ba3f43e A |
2485 | ssa->sin_family = AF_INET; |
2486 | ssa->sin_len = sizeof(struct sockaddr_in); | |
2487 | ssa->sin_addr = mreqs.imr_sourceaddr; | |
6d2010ae A |
2488 | } |
2489 | /* | |
2490 | * Attempt to look up hinted ifp from interface address. | |
2491 | * Fallthrough with null ifp iff lookup fails, to | |
2492 | * preserve 4.4BSD mcast API idempotence. | |
2493 | * XXX NOTE WELL: The RFC 3678 API is preferred because | |
2494 | * using an IPv4 address as a key is racy. | |
2495 | */ | |
0a7de745 | 2496 | if (!in_nullhost(mreqs.imr_interface)) { |
6d2010ae | 2497 | ifp = ip_multicast_if(&mreqs.imr_interface, &ifindex); |
0a7de745 | 2498 | } |
6d2010ae | 2499 | |
39236c6e A |
2500 | IGMP_INET_PRINTF(mreqs.imr_interface, |
2501 | ("%s: imr_interface = %s, ifp = 0x%llx\n", __func__, | |
2502 | _igmp_inet_buf, (uint64_t)VM_KERNEL_ADDRPERM(ifp))); | |
6d2010ae A |
2503 | |
2504 | break; | |
2505 | ||
2506 | case MCAST_LEAVE_GROUP: | |
2507 | case MCAST_LEAVE_SOURCE_GROUP: | |
2508 | if (sopt->sopt_name == MCAST_LEAVE_GROUP) { | |
2509 | error = sooptcopyin(sopt, &gsr, | |
2510 | sizeof(struct group_req), | |
2511 | sizeof(struct group_req)); | |
2512 | } else if (sopt->sopt_name == MCAST_LEAVE_SOURCE_GROUP) { | |
2513 | error = sooptcopyin(sopt, &gsr, | |
2514 | sizeof(struct group_source_req), | |
2515 | sizeof(struct group_source_req)); | |
2516 | } | |
0a7de745 A |
2517 | if (error) { |
2518 | return error; | |
2519 | } | |
6d2010ae | 2520 | |
5ba3f43e | 2521 | if (gsa->sin_family != AF_INET || |
0a7de745 A |
2522 | gsa->sin_len != sizeof(struct sockaddr_in)) { |
2523 | return EINVAL; | |
2524 | } | |
6d2010ae A |
2525 | |
2526 | if (sopt->sopt_name == MCAST_LEAVE_SOURCE_GROUP) { | |
5ba3f43e | 2527 | if (ssa->sin_family != AF_INET || |
0a7de745 A |
2528 | ssa->sin_len != sizeof(struct sockaddr_in)) { |
2529 | return EINVAL; | |
2530 | } | |
6d2010ae A |
2531 | } |
2532 | ||
2533 | ifnet_head_lock_shared(); | |
2534 | if (gsr.gsr_interface == 0 || | |
2535 | (u_int)if_index < gsr.gsr_interface) { | |
2536 | ifnet_head_done(); | |
0a7de745 | 2537 | return EADDRNOTAVAIL; |
6d2010ae A |
2538 | } |
2539 | ||
2540 | ifp = ifindex2ifnet[gsr.gsr_interface]; | |
2541 | ifnet_head_done(); | |
2542 | break; | |
2543 | ||
2544 | default: | |
2545 | IGMP_PRINTF(("%s: unknown sopt_name %d\n", | |
2546 | __func__, sopt->sopt_name)); | |
0a7de745 | 2547 | return EOPNOTSUPP; |
6d2010ae A |
2548 | } |
2549 | ||
0a7de745 A |
2550 | if (!IN_MULTICAST(ntohl(gsa->sin_addr.s_addr))) { |
2551 | return EINVAL; | |
2552 | } | |
6d2010ae A |
2553 | |
2554 | /* | |
2555 | * Find the membership in the membership array. | |
2556 | */ | |
2557 | imo = inp_findmoptions(inp); | |
0a7de745 A |
2558 | if (imo == NULL) { |
2559 | return ENOMEM; | |
2560 | } | |
6d2010ae A |
2561 | |
2562 | IMO_LOCK(imo); | |
5ba3f43e | 2563 | idx = imo_match_group(imo, ifp, gsa); |
6d2010ae A |
2564 | if (idx == (size_t)-1) { |
2565 | error = EADDRNOTAVAIL; | |
2566 | goto out_locked; | |
2567 | } | |
2568 | inm = imo->imo_membership[idx]; | |
2569 | imf = &imo->imo_mfilters[idx]; | |
2570 | ||
5ba3f43e | 2571 | if (ssa->sin_family != AF_UNSPEC) { |
6d2010ae A |
2572 | IGMP_PRINTF(("%s: opt=%d is_final=0\n", __func__, |
2573 | sopt->sopt_name)); | |
2574 | is_final = 0; | |
2575 | } | |
2576 | ||
2577 | /* | |
2578 | * Begin state merge transaction at socket layer. | |
2579 | */ | |
2580 | ||
2581 | /* | |
2582 | * If we were instructed only to leave a given source, do so. | |
2583 | * MCAST_LEAVE_SOURCE_GROUP is only valid for inclusive memberships. | |
2584 | */ | |
2585 | if (is_final) { | |
2586 | imf_leave(imf); | |
2587 | } else { | |
2588 | if (imf->imf_st[0] == MCAST_EXCLUDE) { | |
2589 | error = EADDRNOTAVAIL; | |
2590 | goto out_locked; | |
2591 | } | |
5ba3f43e | 2592 | ims = imo_match_source(imo, idx, ssa); |
6d2010ae | 2593 | if (ims == NULL) { |
5ba3f43e | 2594 | IGMP_INET_PRINTF(ssa->sin_addr, |
39236c6e A |
2595 | ("%s: source %s %spresent\n", __func__, |
2596 | _igmp_inet_buf, "not ")); | |
6d2010ae A |
2597 | error = EADDRNOTAVAIL; |
2598 | goto out_locked; | |
2599 | } | |
2600 | IGMP_PRINTF(("%s: %s source\n", __func__, "block")); | |
5ba3f43e | 2601 | error = imf_prune(imf, ssa); |
6d2010ae A |
2602 | if (error) { |
2603 | IGMP_PRINTF(("%s: merge imf state failed\n", | |
2604 | __func__)); | |
2605 | goto out_locked; | |
2606 | } | |
2607 | } | |
2608 | ||
2609 | /* | |
2610 | * Begin state merge transaction at IGMP layer. | |
2611 | */ | |
2612 | ||
39037602 | 2613 | |
6d2010ae A |
2614 | if (is_final) { |
2615 | /* | |
2616 | * Give up the multicast address record to which | |
2617 | * the membership points. Reference held in imo | |
2618 | * will be released below. | |
2619 | */ | |
2620 | (void) in_leavegroup(inm, imf); | |
2621 | } else { | |
2622 | IGMP_PRINTF(("%s: merge inm state\n", __func__)); | |
2623 | INM_LOCK(inm); | |
2624 | error = inm_merge(inm, imf); | |
2625 | if (error) { | |
2626 | IGMP_PRINTF(("%s: failed to merge inm state\n", | |
2627 | __func__)); | |
2628 | INM_UNLOCK(inm); | |
2629 | goto out_imf_rollback; | |
2630 | } | |
2631 | ||
2632 | IGMP_PRINTF(("%s: doing igmp downcall\n", __func__)); | |
39236c6e | 2633 | error = igmp_change_state(inm, &itp); |
6d2010ae A |
2634 | if (error) { |
2635 | IGMP_PRINTF(("%s: failed igmp downcall\n", __func__)); | |
2636 | } | |
2637 | INM_UNLOCK(inm); | |
2638 | } | |
2639 | ||
2640 | out_imf_rollback: | |
0a7de745 | 2641 | if (error) { |
6d2010ae | 2642 | imf_rollback(imf); |
0a7de745 | 2643 | } else { |
6d2010ae | 2644 | imf_commit(imf); |
0a7de745 | 2645 | } |
6d2010ae A |
2646 | |
2647 | imf_reap(imf); | |
2648 | ||
2649 | if (is_final) { | |
39037602 | 2650 | /* Remove the gap in the membership array. */ |
6d2010ae A |
2651 | VERIFY(inm == imo->imo_membership[idx]); |
2652 | imo->imo_membership[idx] = NULL; | |
39037602 A |
2653 | |
2654 | /* | |
2655 | * See inp_join_group() for why we need to unlock | |
2656 | */ | |
2657 | IMO_ADDREF_LOCKED(imo); | |
2658 | IMO_UNLOCK(imo); | |
2659 | socket_unlock(inp->inp_socket, 0); | |
2660 | ||
6d2010ae | 2661 | INM_REMREF(inm); |
39037602 A |
2662 | |
2663 | socket_lock(inp->inp_socket, 0); | |
2664 | IMO_REMREF(imo); | |
2665 | IMO_LOCK(imo); | |
2666 | ||
6d2010ae | 2667 | for (++idx; idx < imo->imo_num_memberships; ++idx) { |
0a7de745 A |
2668 | imo->imo_membership[idx - 1] = imo->imo_membership[idx]; |
2669 | imo->imo_mfilters[idx - 1] = imo->imo_mfilters[idx]; | |
6d2010ae A |
2670 | } |
2671 | imo->imo_num_memberships--; | |
2672 | } | |
2673 | ||
2674 | out_locked: | |
2675 | IMO_UNLOCK(imo); | |
0a7de745 | 2676 | IMO_REMREF(imo); /* from inp_findmoptions() */ |
39236c6e A |
2677 | |
2678 | /* schedule timer now that we've dropped the lock(s) */ | |
2679 | igmp_set_timeout(&itp); | |
2680 | ||
0a7de745 | 2681 | return error; |
6d2010ae A |
2682 | } |
2683 | ||
2684 | /* | |
2685 | * Select the interface for transmitting IPv4 multicast datagrams. | |
2686 | * | |
2687 | * Either an instance of struct in_addr or an instance of struct ip_mreqn | |
2688 | * may be passed to this socket option. An address of INADDR_ANY or an | |
2689 | * interface index of 0 is used to remove a previous selection. | |
2690 | * When no interface is selected, one is chosen for every send. | |
2691 | */ | |
2692 | static int | |
2693 | inp_set_multicast_if(struct inpcb *inp, struct sockopt *sopt) | |
2694 | { | |
0a7de745 A |
2695 | struct in_addr addr; |
2696 | struct ip_mreqn mreqn; | |
2697 | struct ifnet *ifp; | |
2698 | struct ip_moptions *imo; | |
2699 | int error = 0; | |
2700 | unsigned int ifindex = 0; | |
6d2010ae | 2701 | |
5ba3f43e | 2702 | bzero(&addr, sizeof(addr)); |
6d2010ae A |
2703 | if (sopt->sopt_valsize == sizeof(struct ip_mreqn)) { |
2704 | /* | |
2705 | * An interface index was specified using the | |
2706 | * Linux-derived ip_mreqn structure. | |
2707 | */ | |
2708 | error = sooptcopyin(sopt, &mreqn, sizeof(struct ip_mreqn), | |
2709 | sizeof(struct ip_mreqn)); | |
0a7de745 A |
2710 | if (error) { |
2711 | return error; | |
2712 | } | |
6d2010ae A |
2713 | |
2714 | ifnet_head_lock_shared(); | |
2715 | if (mreqn.imr_ifindex < 0 || if_index < mreqn.imr_ifindex) { | |
2716 | ifnet_head_done(); | |
0a7de745 | 2717 | return EINVAL; |
6d2010ae A |
2718 | } |
2719 | ||
2720 | if (mreqn.imr_ifindex == 0) { | |
2721 | ifp = NULL; | |
2722 | } else { | |
2723 | ifp = ifindex2ifnet[mreqn.imr_ifindex]; | |
2724 | if (ifp == NULL) { | |
2725 | ifnet_head_done(); | |
0a7de745 | 2726 | return EADDRNOTAVAIL; |
6d2010ae A |
2727 | } |
2728 | } | |
2729 | ifnet_head_done(); | |
2730 | } else { | |
2731 | /* | |
2732 | * An interface was specified by IPv4 address. | |
2733 | * This is the traditional BSD usage. | |
2734 | */ | |
2735 | error = sooptcopyin(sopt, &addr, sizeof(struct in_addr), | |
2736 | sizeof(struct in_addr)); | |
0a7de745 A |
2737 | if (error) { |
2738 | return error; | |
2739 | } | |
6d2010ae A |
2740 | if (in_nullhost(addr)) { |
2741 | ifp = NULL; | |
2742 | } else { | |
2743 | ifp = ip_multicast_if(&addr, &ifindex); | |
2744 | if (ifp == NULL) { | |
39236c6e A |
2745 | IGMP_INET_PRINTF(addr, |
2746 | ("%s: can't find ifp for addr=%s\n", | |
2747 | __func__, _igmp_inet_buf)); | |
0a7de745 | 2748 | return EADDRNOTAVAIL; |
6d2010ae A |
2749 | } |
2750 | } | |
6d2010ae A |
2751 | } |
2752 | ||
2753 | /* Reject interfaces which do not support multicast. */ | |
0a7de745 A |
2754 | if (ifp != NULL && (ifp->if_flags & IFF_MULTICAST) == 0) { |
2755 | return EOPNOTSUPP; | |
2756 | } | |
6d2010ae A |
2757 | |
2758 | imo = inp_findmoptions(inp); | |
0a7de745 A |
2759 | if (imo == NULL) { |
2760 | return ENOMEM; | |
2761 | } | |
6d2010ae A |
2762 | |
2763 | IMO_LOCK(imo); | |
2764 | imo->imo_multicast_ifp = ifp; | |
0a7de745 | 2765 | if (ifindex) { |
6d2010ae | 2766 | imo->imo_multicast_addr = addr; |
0a7de745 | 2767 | } else { |
6d2010ae | 2768 | imo->imo_multicast_addr.s_addr = INADDR_ANY; |
0a7de745 | 2769 | } |
6d2010ae | 2770 | IMO_UNLOCK(imo); |
0a7de745 | 2771 | IMO_REMREF(imo); /* from inp_findmoptions() */ |
6d2010ae | 2772 | |
0a7de745 | 2773 | return 0; |
6d2010ae A |
2774 | } |
2775 | ||
2776 | /* | |
2777 | * Atomically set source filters on a socket for an IPv4 multicast group. | |
2778 | */ | |
2779 | static int | |
2780 | inp_set_source_filters(struct inpcb *inp, struct sockopt *sopt) | |
2781 | { | |
0a7de745 A |
2782 | struct __msfilterreq64 msfr = {}, msfr64; |
2783 | struct __msfilterreq32 msfr32; | |
2784 | struct sockaddr_in *gsa; | |
2785 | struct ifnet *ifp; | |
2786 | struct in_mfilter *imf; | |
2787 | struct ip_moptions *imo; | |
2788 | struct in_multi *inm; | |
2789 | size_t idx; | |
2790 | int error; | |
f427ee49 | 2791 | uint64_t tmp_ptr; |
0a7de745 A |
2792 | struct igmp_tparams itp; |
2793 | ||
2794 | bzero(&itp, sizeof(itp)); | |
6d2010ae A |
2795 | |
2796 | if (IS_64BIT_PROCESS(current_proc())) { | |
2797 | error = sooptcopyin(sopt, &msfr64, | |
2798 | sizeof(struct __msfilterreq64), | |
2799 | sizeof(struct __msfilterreq64)); | |
0a7de745 A |
2800 | if (error) { |
2801 | return error; | |
2802 | } | |
6d2010ae | 2803 | /* we never use msfr.msfr_srcs; */ |
3e170ce0 | 2804 | memcpy(&msfr, &msfr64, sizeof(msfr64)); |
6d2010ae A |
2805 | } else { |
2806 | error = sooptcopyin(sopt, &msfr32, | |
2807 | sizeof(struct __msfilterreq32), | |
2808 | sizeof(struct __msfilterreq32)); | |
0a7de745 A |
2809 | if (error) { |
2810 | return error; | |
2811 | } | |
6d2010ae | 2812 | /* we never use msfr.msfr_srcs; */ |
3e170ce0 | 2813 | memcpy(&msfr, &msfr32, sizeof(msfr32)); |
6d2010ae A |
2814 | } |
2815 | ||
316670eb | 2816 | if ((size_t) msfr.msfr_nsrcs > |
0a7de745 | 2817 | UINT32_MAX / sizeof(struct sockaddr_storage)) { |
39236c6e | 2818 | msfr.msfr_nsrcs = UINT32_MAX / sizeof(struct sockaddr_storage); |
0a7de745 | 2819 | } |
316670eb | 2820 | |
0a7de745 A |
2821 | if (msfr.msfr_nsrcs > in_mcast_maxsocksrc) { |
2822 | return ENOBUFS; | |
2823 | } | |
6d2010ae A |
2824 | |
2825 | if ((msfr.msfr_fmode != MCAST_EXCLUDE && | |
0a7de745 A |
2826 | msfr.msfr_fmode != MCAST_INCLUDE)) { |
2827 | return EINVAL; | |
2828 | } | |
6d2010ae A |
2829 | |
2830 | if (msfr.msfr_group.ss_family != AF_INET || | |
0a7de745 A |
2831 | msfr.msfr_group.ss_len != sizeof(struct sockaddr_in)) { |
2832 | return EINVAL; | |
2833 | } | |
6d2010ae | 2834 | |
5ba3f43e | 2835 | gsa = (struct sockaddr_in *)&msfr.msfr_group; |
0a7de745 A |
2836 | if (!IN_MULTICAST(ntohl(gsa->sin_addr.s_addr))) { |
2837 | return EINVAL; | |
2838 | } | |
6d2010ae | 2839 | |
0a7de745 | 2840 | gsa->sin_port = 0; /* ignore port */ |
6d2010ae A |
2841 | |
2842 | ifnet_head_lock_shared(); | |
2843 | if (msfr.msfr_ifindex == 0 || (u_int)if_index < msfr.msfr_ifindex) { | |
2844 | ifnet_head_done(); | |
0a7de745 | 2845 | return EADDRNOTAVAIL; |
6d2010ae A |
2846 | } |
2847 | ||
2848 | ifp = ifindex2ifnet[msfr.msfr_ifindex]; | |
2849 | ifnet_head_done(); | |
0a7de745 A |
2850 | if (ifp == NULL) { |
2851 | return EADDRNOTAVAIL; | |
2852 | } | |
6d2010ae A |
2853 | |
2854 | /* | |
2855 | * Check if this socket is a member of this group. | |
2856 | */ | |
2857 | imo = inp_findmoptions(inp); | |
0a7de745 A |
2858 | if (imo == NULL) { |
2859 | return ENOMEM; | |
2860 | } | |
6d2010ae A |
2861 | |
2862 | IMO_LOCK(imo); | |
5ba3f43e | 2863 | idx = imo_match_group(imo, ifp, gsa); |
6d2010ae A |
2864 | if (idx == (size_t)-1 || imo->imo_mfilters == NULL) { |
2865 | error = EADDRNOTAVAIL; | |
2866 | goto out_imo_locked; | |
2867 | } | |
2868 | inm = imo->imo_membership[idx]; | |
2869 | imf = &imo->imo_mfilters[idx]; | |
2870 | ||
2871 | /* | |
2872 | * Begin state merge transaction at socket layer. | |
2873 | */ | |
2874 | ||
f427ee49 | 2875 | imf->imf_st[1] = (uint8_t)msfr.msfr_fmode; |
6d2010ae A |
2876 | |
2877 | /* | |
2878 | * Apply any new source filters, if present. | |
2879 | * Make a copy of the user-space source vector so | |
2880 | * that we may copy them with a single copyin. This | |
2881 | * allows us to deal with page faults up-front. | |
2882 | */ | |
2883 | if (msfr.msfr_nsrcs > 0) { | |
0a7de745 A |
2884 | struct in_msource *lims; |
2885 | struct sockaddr_in *psin; | |
2886 | struct sockaddr_storage *kss, *pkss; | |
2887 | int i; | |
6d2010ae | 2888 | |
0a7de745 | 2889 | if (IS_64BIT_PROCESS(current_proc())) { |
6d2010ae | 2890 | tmp_ptr = msfr64.msfr_srcs; |
0a7de745 | 2891 | } else { |
6d2010ae | 2892 | tmp_ptr = CAST_USER_ADDR_T(msfr32.msfr_srcs); |
0a7de745 | 2893 | } |
6d2010ae A |
2894 | |
2895 | IGMP_PRINTF(("%s: loading %lu source list entries\n", | |
2896 | __func__, (unsigned long)msfr.msfr_nsrcs)); | |
316670eb | 2897 | kss = _MALLOC((size_t) msfr.msfr_nsrcs * sizeof(*kss), |
6d2010ae A |
2898 | M_TEMP, M_WAITOK); |
2899 | if (kss == NULL) { | |
2900 | error = ENOMEM; | |
2901 | goto out_imo_locked; | |
2902 | } | |
f427ee49 | 2903 | error = copyin(CAST_USER_ADDR_T(tmp_ptr), kss, |
316670eb | 2904 | (size_t) msfr.msfr_nsrcs * sizeof(*kss)); |
6d2010ae A |
2905 | if (error) { |
2906 | FREE(kss, M_TEMP); | |
2907 | goto out_imo_locked; | |
2908 | } | |
2909 | ||
2910 | /* | |
2911 | * Mark all source filters as UNDEFINED at t1. | |
2912 | * Restore new group filter mode, as imf_leave() | |
2913 | * will set it to INCLUDE. | |
2914 | */ | |
2915 | imf_leave(imf); | |
f427ee49 | 2916 | imf->imf_st[1] = (uint8_t)msfr.msfr_fmode; |
6d2010ae A |
2917 | |
2918 | /* | |
2919 | * Update socket layer filters at t1, lazy-allocating | |
2920 | * new entries. This saves a bunch of memory at the | |
2921 | * cost of one RB_FIND() per source entry; duplicate | |
2922 | * entries in the msfr_nsrcs vector are ignored. | |
2923 | * If we encounter an error, rollback transaction. | |
2924 | * | |
2925 | * XXX This too could be replaced with a set-symmetric | |
2926 | * difference like loop to avoid walking from root | |
2927 | * every time, as the key space is common. | |
2928 | */ | |
2929 | for (i = 0, pkss = kss; (u_int)i < msfr.msfr_nsrcs; | |
2930 | i++, pkss++) { | |
2931 | psin = (struct sockaddr_in *)pkss; | |
2932 | if (psin->sin_family != AF_INET) { | |
2933 | error = EAFNOSUPPORT; | |
2934 | break; | |
2935 | } | |
2936 | if (psin->sin_len != sizeof(struct sockaddr_in)) { | |
2937 | error = EINVAL; | |
2938 | break; | |
2939 | } | |
2940 | error = imf_get_source(imf, psin, &lims); | |
0a7de745 | 2941 | if (error) { |
6d2010ae | 2942 | break; |
0a7de745 | 2943 | } |
6d2010ae A |
2944 | lims->imsl_st[1] = imf->imf_st[1]; |
2945 | } | |
2946 | FREE(kss, M_TEMP); | |
2947 | } | |
2948 | ||
0a7de745 | 2949 | if (error) { |
6d2010ae | 2950 | goto out_imf_rollback; |
0a7de745 | 2951 | } |
6d2010ae A |
2952 | |
2953 | /* | |
2954 | * Begin state merge transaction at IGMP layer. | |
2955 | */ | |
2956 | INM_LOCK(inm); | |
2957 | IGMP_PRINTF(("%s: merge inm state\n", __func__)); | |
2958 | error = inm_merge(inm, imf); | |
2959 | if (error) { | |
2960 | IGMP_PRINTF(("%s: failed to merge inm state\n", __func__)); | |
2961 | INM_UNLOCK(inm); | |
2962 | goto out_imf_rollback; | |
2963 | } | |
2964 | ||
2965 | IGMP_PRINTF(("%s: doing igmp downcall\n", __func__)); | |
39236c6e | 2966 | error = igmp_change_state(inm, &itp); |
6d2010ae A |
2967 | INM_UNLOCK(inm); |
2968 | #ifdef IGMP_DEBUG | |
0a7de745 | 2969 | if (error) { |
6d2010ae | 2970 | IGMP_PRINTF(("%s: failed igmp downcall\n", __func__)); |
0a7de745 | 2971 | } |
6d2010ae A |
2972 | #endif |
2973 | ||
2974 | out_imf_rollback: | |
0a7de745 | 2975 | if (error) { |
6d2010ae | 2976 | imf_rollback(imf); |
0a7de745 | 2977 | } else { |
6d2010ae | 2978 | imf_commit(imf); |
0a7de745 | 2979 | } |
6d2010ae A |
2980 | |
2981 | imf_reap(imf); | |
2982 | ||
2983 | out_imo_locked: | |
2984 | IMO_UNLOCK(imo); | |
0a7de745 | 2985 | IMO_REMREF(imo); /* from inp_findmoptions() */ |
6d2010ae | 2986 | |
39236c6e A |
2987 | /* schedule timer now that we've dropped the lock(s) */ |
2988 | igmp_set_timeout(&itp); | |
2989 | ||
0a7de745 | 2990 | return error; |
6d2010ae A |
2991 | } |
2992 | ||
2993 | /* | |
2994 | * Set the IP multicast options in response to user setsockopt(). | |
2995 | * | |
2996 | * Many of the socket options handled in this function duplicate the | |
2997 | * functionality of socket options in the regular unicast API. However, | |
2998 | * it is not possible to merge the duplicate code, because the idempotence | |
2999 | * of the IPv4 multicast part of the BSD Sockets API must be preserved; | |
3000 | * the effects of these options must be treated as separate and distinct. | |
6d2010ae A |
3001 | */ |
3002 | int | |
3003 | inp_setmoptions(struct inpcb *inp, struct sockopt *sopt) | |
3004 | { | |
0a7de745 A |
3005 | struct ip_moptions *imo; |
3006 | int error; | |
3007 | unsigned int ifindex; | |
3008 | struct ifnet *ifp; | |
6d2010ae A |
3009 | |
3010 | error = 0; | |
3011 | ||
3012 | /* | |
3013 | * If socket is neither of type SOCK_RAW or SOCK_DGRAM, | |
3014 | * or is a divert socket, reject it. | |
3015 | */ | |
39236c6e A |
3016 | if (SOCK_PROTO(inp->inp_socket) == IPPROTO_DIVERT || |
3017 | (SOCK_TYPE(inp->inp_socket) != SOCK_RAW && | |
0a7de745 A |
3018 | SOCK_TYPE(inp->inp_socket) != SOCK_DGRAM)) { |
3019 | return EOPNOTSUPP; | |
3020 | } | |
6d2010ae A |
3021 | |
3022 | switch (sopt->sopt_name) { | |
6d2010ae A |
3023 | case IP_MULTICAST_IF: |
3024 | error = inp_set_multicast_if(inp, sopt); | |
3025 | break; | |
3026 | ||
3027 | case IP_MULTICAST_IFINDEX: | |
3028 | /* | |
3029 | * Select the interface for outgoing multicast packets. | |
3030 | */ | |
0a7de745 A |
3031 | error = sooptcopyin(sopt, &ifindex, sizeof(ifindex), |
3032 | sizeof(ifindex)); | |
3033 | if (error) { | |
6d2010ae | 3034 | break; |
0a7de745 | 3035 | } |
6d2010ae A |
3036 | |
3037 | imo = inp_findmoptions(inp); | |
3038 | if (imo == NULL) { | |
3039 | error = ENOMEM; | |
3040 | break; | |
3041 | } | |
3042 | /* | |
3043 | * Index 0 is used to remove a previous selection. | |
3044 | * When no interface is selected, a default one is | |
3045 | * chosen every time a multicast packet is sent. | |
3046 | */ | |
3047 | if (ifindex == 0) { | |
3048 | IMO_LOCK(imo); | |
3049 | imo->imo_multicast_ifp = NULL; | |
3050 | IMO_UNLOCK(imo); | |
0a7de745 | 3051 | IMO_REMREF(imo); /* from inp_findmoptions() */ |
6d2010ae A |
3052 | break; |
3053 | } | |
3054 | ||
3055 | ifnet_head_lock_shared(); | |
3056 | /* Don't need to check is ifindex is < 0 since it's unsigned */ | |
3057 | if ((unsigned int)if_index < ifindex) { | |
3058 | ifnet_head_done(); | |
0a7de745 A |
3059 | IMO_REMREF(imo); /* from inp_findmoptions() */ |
3060 | error = ENXIO; /* per IPV6_MULTICAST_IF */ | |
6d2010ae A |
3061 | break; |
3062 | } | |
3063 | ifp = ifindex2ifnet[ifindex]; | |
3064 | ifnet_head_done(); | |
3065 | ||
3066 | /* If it's detached or isn't a multicast interface, bail out */ | |
3067 | if (ifp == NULL || !(ifp->if_flags & IFF_MULTICAST)) { | |
0a7de745 | 3068 | IMO_REMREF(imo); /* from inp_findmoptions() */ |
6d2010ae A |
3069 | error = EADDRNOTAVAIL; |
3070 | break; | |
3071 | } | |
3072 | IMO_LOCK(imo); | |
3073 | imo->imo_multicast_ifp = ifp; | |
3074 | /* | |
3075 | * Clear out any remnants of past IP_MULTICAST_IF. The addr | |
3076 | * isn't really used anywhere in the kernel; we could have | |
3077 | * iterated thru the addresses of the interface and pick one | |
3078 | * here, but that is redundant since ip_getmoptions() already | |
3079 | * takes care of that for INADDR_ANY. | |
3080 | */ | |
3081 | imo->imo_multicast_addr.s_addr = INADDR_ANY; | |
3082 | IMO_UNLOCK(imo); | |
0a7de745 | 3083 | IMO_REMREF(imo); /* from inp_findmoptions() */ |
6d2010ae A |
3084 | break; |
3085 | ||
3086 | case IP_MULTICAST_TTL: { | |
3087 | u_char ttl; | |
3088 | ||
3089 | /* | |
3090 | * Set the IP time-to-live for outgoing multicast packets. | |
3091 | * The original multicast API required a char argument, | |
3092 | * which is inconsistent with the rest of the socket API. | |
3093 | * We allow either a char or an int. | |
3094 | */ | |
3095 | if (sopt->sopt_valsize == sizeof(u_char)) { | |
3096 | error = sooptcopyin(sopt, &ttl, sizeof(u_char), | |
3097 | sizeof(u_char)); | |
0a7de745 | 3098 | if (error) { |
6d2010ae | 3099 | break; |
0a7de745 | 3100 | } |
6d2010ae A |
3101 | } else { |
3102 | u_int ittl; | |
3103 | ||
3104 | error = sooptcopyin(sopt, &ittl, sizeof(u_int), | |
3105 | sizeof(u_int)); | |
0a7de745 | 3106 | if (error) { |
6d2010ae | 3107 | break; |
0a7de745 | 3108 | } |
6d2010ae A |
3109 | if (ittl > 255) { |
3110 | error = EINVAL; | |
3111 | break; | |
3112 | } | |
3113 | ttl = (u_char)ittl; | |
3114 | } | |
3115 | imo = inp_findmoptions(inp); | |
3116 | if (imo == NULL) { | |
3117 | error = ENOMEM; | |
3118 | break; | |
3119 | } | |
3120 | IMO_LOCK(imo); | |
3121 | imo->imo_multicast_ttl = ttl; | |
3122 | IMO_UNLOCK(imo); | |
0a7de745 | 3123 | IMO_REMREF(imo); /* from inp_findmoptions() */ |
6d2010ae A |
3124 | break; |
3125 | } | |
3126 | ||
3127 | case IP_MULTICAST_LOOP: { | |
3128 | u_char loop; | |
3129 | ||
3130 | /* | |
3131 | * Set the loopback flag for outgoing multicast packets. | |
3132 | * Must be zero or one. The original multicast API required a | |
3133 | * char argument, which is inconsistent with the rest | |
3134 | * of the socket API. We allow either a char or an int. | |
3135 | */ | |
3136 | if (sopt->sopt_valsize == sizeof(u_char)) { | |
3137 | error = sooptcopyin(sopt, &loop, sizeof(u_char), | |
3138 | sizeof(u_char)); | |
0a7de745 | 3139 | if (error) { |
6d2010ae | 3140 | break; |
0a7de745 | 3141 | } |
6d2010ae A |
3142 | } else { |
3143 | u_int iloop; | |
3144 | ||
3145 | error = sooptcopyin(sopt, &iloop, sizeof(u_int), | |
0a7de745 A |
3146 | sizeof(u_int)); |
3147 | if (error) { | |
6d2010ae | 3148 | break; |
0a7de745 | 3149 | } |
6d2010ae A |
3150 | loop = (u_char)iloop; |
3151 | } | |
3152 | imo = inp_findmoptions(inp); | |
3153 | if (imo == NULL) { | |
3154 | error = ENOMEM; | |
3155 | break; | |
3156 | } | |
3157 | IMO_LOCK(imo); | |
3158 | imo->imo_multicast_loop = !!loop; | |
3159 | IMO_UNLOCK(imo); | |
0a7de745 | 3160 | IMO_REMREF(imo); /* from inp_findmoptions() */ |
6d2010ae A |
3161 | break; |
3162 | } | |
3163 | ||
3164 | case IP_ADD_MEMBERSHIP: | |
3165 | case IP_ADD_SOURCE_MEMBERSHIP: | |
3166 | case MCAST_JOIN_GROUP: | |
3167 | case MCAST_JOIN_SOURCE_GROUP: | |
3168 | error = inp_join_group(inp, sopt); | |
3169 | break; | |
3170 | ||
3171 | case IP_DROP_MEMBERSHIP: | |
3172 | case IP_DROP_SOURCE_MEMBERSHIP: | |
3173 | case MCAST_LEAVE_GROUP: | |
3174 | case MCAST_LEAVE_SOURCE_GROUP: | |
3175 | error = inp_leave_group(inp, sopt); | |
3176 | break; | |
3177 | ||
3178 | case IP_BLOCK_SOURCE: | |
3179 | case IP_UNBLOCK_SOURCE: | |
3180 | case MCAST_BLOCK_SOURCE: | |
3181 | case MCAST_UNBLOCK_SOURCE: | |
3182 | error = inp_block_unblock_source(inp, sopt); | |
3183 | break; | |
3184 | ||
3185 | case IP_MSFILTER: | |
3186 | error = inp_set_source_filters(inp, sopt); | |
3187 | break; | |
3188 | ||
3189 | default: | |
3190 | error = EOPNOTSUPP; | |
3191 | break; | |
3192 | } | |
3193 | ||
0a7de745 | 3194 | return error; |
6d2010ae A |
3195 | } |
3196 | ||
3197 | /* | |
3198 | * Expose IGMP's multicast filter mode and source list(s) to userland, | |
3199 | * keyed by (ifindex, group). | |
3200 | * The filter mode is written out as a uint32_t, followed by | |
3201 | * 0..n of struct in_addr. | |
3202 | * For use by ifmcstat(8). | |
3203 | */ | |
3204 | static int | |
3205 | sysctl_ip_mcast_filters SYSCTL_HANDLER_ARGS | |
3206 | { | |
3207 | #pragma unused(oidp) | |
3208 | ||
0a7de745 A |
3209 | struct in_addr src = {}, group; |
3210 | struct ifnet *ifp; | |
3211 | struct in_multi *inm; | |
3212 | struct in_multistep step; | |
3213 | struct ip_msource *ims; | |
3214 | int *name; | |
3215 | int retval = 0; | |
3216 | u_int namelen; | |
3217 | uint32_t fmode, ifindex; | |
6d2010ae A |
3218 | |
3219 | name = (int *)arg1; | |
3220 | namelen = (u_int)arg2; | |
3221 | ||
0a7de745 A |
3222 | if (req->newptr != USER_ADDR_NULL) { |
3223 | return EPERM; | |
3224 | } | |
6d2010ae | 3225 | |
0a7de745 A |
3226 | if (namelen != 2) { |
3227 | return EINVAL; | |
3228 | } | |
6d2010ae A |
3229 | |
3230 | ifindex = name[0]; | |
3231 | ifnet_head_lock_shared(); | |
3232 | if (ifindex <= 0 || ifindex > (u_int)if_index) { | |
3233 | IGMP_PRINTF(("%s: ifindex %u out of range\n", | |
3234 | __func__, ifindex)); | |
3235 | ifnet_head_done(); | |
0a7de745 | 3236 | return ENOENT; |
6d2010ae A |
3237 | } |
3238 | ||
3239 | group.s_addr = name[1]; | |
3240 | if (!IN_MULTICAST(ntohl(group.s_addr))) { | |
39236c6e A |
3241 | IGMP_INET_PRINTF(group, |
3242 | ("%s: group %s is not multicast\n", | |
3243 | __func__, _igmp_inet_buf)); | |
6d2010ae | 3244 | ifnet_head_done(); |
0a7de745 | 3245 | return EINVAL; |
6d2010ae A |
3246 | } |
3247 | ||
3248 | ifp = ifindex2ifnet[ifindex]; | |
3249 | ifnet_head_done(); | |
3250 | if (ifp == NULL) { | |
3251 | IGMP_PRINTF(("%s: no ifp for ifindex %u\n", __func__, ifindex)); | |
0a7de745 | 3252 | return ENOENT; |
6d2010ae A |
3253 | } |
3254 | ||
3255 | in_multihead_lock_shared(); | |
3256 | IN_FIRST_MULTI(step, inm); | |
3257 | while (inm != NULL) { | |
3258 | INM_LOCK(inm); | |
0a7de745 | 3259 | if (inm->inm_ifp != ifp) { |
6d2010ae | 3260 | goto next; |
0a7de745 | 3261 | } |
6d2010ae | 3262 | |
0a7de745 | 3263 | if (!in_hosteq(inm->inm_addr, group)) { |
6d2010ae | 3264 | goto next; |
0a7de745 | 3265 | } |
6d2010ae A |
3266 | |
3267 | fmode = inm->inm_st[1].iss_fmode; | |
3268 | retval = SYSCTL_OUT(req, &fmode, sizeof(uint32_t)); | |
3269 | if (retval != 0) { | |
3270 | INM_UNLOCK(inm); | |
0a7de745 | 3271 | break; /* abort */ |
6d2010ae A |
3272 | } |
3273 | RB_FOREACH(ims, ip_msource_tree, &inm->inm_srcs) { | |
3274 | #ifdef IGMP_DEBUG | |
3275 | struct in_addr ina; | |
3276 | ina.s_addr = htonl(ims->ims_haddr); | |
39236c6e A |
3277 | IGMP_INET_PRINTF(ina, |
3278 | ("%s: visit node %s\n", __func__, _igmp_inet_buf)); | |
6d2010ae A |
3279 | #endif |
3280 | /* | |
3281 | * Only copy-out sources which are in-mode. | |
3282 | */ | |
3283 | if (fmode != ims_get_mode(inm, ims, 1)) { | |
3284 | IGMP_PRINTF(("%s: skip non-in-mode\n", | |
3285 | __func__)); | |
3286 | continue; /* process next source */ | |
3287 | } | |
3288 | src.s_addr = htonl(ims->ims_haddr); | |
3289 | retval = SYSCTL_OUT(req, &src, sizeof(struct in_addr)); | |
0a7de745 A |
3290 | if (retval != 0) { |
3291 | break; /* process next inm */ | |
3292 | } | |
6d2010ae A |
3293 | } |
3294 | next: | |
3295 | INM_UNLOCK(inm); | |
3296 | IN_NEXT_MULTI(step, inm); | |
3297 | } | |
3298 | in_multihead_lock_done(); | |
3299 | ||
0a7de745 | 3300 | return retval; |
6d2010ae A |
3301 | } |
3302 | ||
3303 | /* | |
3304 | * XXX | |
3305 | * The whole multicast option thing needs to be re-thought. | |
3306 | * Several of these options are equally applicable to non-multicast | |
3307 | * transmission, and one (IP_MULTICAST_TTL) totally duplicates a | |
3308 | * standard option (IP_TTL). | |
3309 | */ | |
3310 | /* | |
3311 | * following RFC1724 section 3.3, 0.0.0.0/8 is interpreted as interface index. | |
3312 | */ | |
3313 | static struct ifnet * | |
3314 | ip_multicast_if(struct in_addr *a, unsigned int *ifindexp) | |
3315 | { | |
3316 | unsigned int ifindex; | |
3317 | struct ifnet *ifp; | |
3318 | ||
0a7de745 | 3319 | if (ifindexp != NULL) { |
6d2010ae | 3320 | *ifindexp = 0; |
0a7de745 | 3321 | } |
6d2010ae A |
3322 | if (ntohl(a->s_addr) >> 24 == 0) { |
3323 | ifindex = ntohl(a->s_addr) & 0xffffff; | |
3324 | ifnet_head_lock_shared(); | |
3325 | /* Don't need to check is ifindex is < 0 since it's unsigned */ | |
3326 | if ((unsigned int)if_index < ifindex) { | |
3327 | ifnet_head_done(); | |
0a7de745 | 3328 | return NULL; |
6d2010ae A |
3329 | } |
3330 | ifp = ifindex2ifnet[ifindex]; | |
3331 | ifnet_head_done(); | |
0a7de745 | 3332 | if (ifp != NULL && ifindexp != NULL) { |
6d2010ae | 3333 | *ifindexp = ifindex; |
0a7de745 | 3334 | } |
6d2010ae A |
3335 | } else { |
3336 | INADDR_TO_IFP(*a, ifp); | |
3337 | } | |
0a7de745 | 3338 | return ifp; |
6d2010ae A |
3339 | } |
3340 | ||
3341 | void | |
3342 | in_multi_init(void) | |
3343 | { | |
0a7de745 | 3344 | PE_parse_boot_argn("ifa_debug", &inm_debug, sizeof(inm_debug)); |
6d2010ae A |
3345 | |
3346 | /* Setup lock group and attribute for in_multihead */ | |
3347 | in_multihead_lock_grp_attr = lck_grp_attr_alloc_init(); | |
3348 | in_multihead_lock_grp = lck_grp_alloc_init("in_multihead", | |
3349 | in_multihead_lock_grp_attr); | |
3350 | in_multihead_lock_attr = lck_attr_alloc_init(); | |
3351 | lck_rw_init(&in_multihead_lock, in_multihead_lock_grp, | |
3352 | in_multihead_lock_attr); | |
3353 | ||
3354 | lck_mtx_init(&inm_trash_lock, in_multihead_lock_grp, | |
3355 | in_multihead_lock_attr); | |
3356 | TAILQ_INIT(&inm_trash_head); | |
3357 | ||
f427ee49 | 3358 | vm_size_t inm_size = (inm_debug == 0) ? sizeof(struct in_multi) : |
0a7de745 | 3359 | sizeof(struct in_multi_dbg); |
f427ee49 | 3360 | inm_zone = zone_create(INM_ZONE_NAME, inm_size, ZC_ZFREE_CLEARMEM); |
6d2010ae A |
3361 | } |
3362 | ||
3363 | static struct in_multi * | |
f427ee49 | 3364 | in_multi_alloc(zalloc_flags_t how) |
6d2010ae A |
3365 | { |
3366 | struct in_multi *inm; | |
3367 | ||
f427ee49 | 3368 | inm = zalloc_flags(inm_zone, how | Z_ZERO); |
6d2010ae | 3369 | if (inm != NULL) { |
6d2010ae A |
3370 | lck_mtx_init(&inm->inm_lock, in_multihead_lock_grp, |
3371 | in_multihead_lock_attr); | |
3372 | inm->inm_debug |= IFD_ALLOC; | |
3373 | if (inm_debug != 0) { | |
3374 | inm->inm_debug |= IFD_DEBUG; | |
3375 | inm->inm_trace = inm_trace; | |
3376 | } | |
3377 | } | |
0a7de745 | 3378 | return inm; |
6d2010ae A |
3379 | } |
3380 | ||
3381 | static void | |
3382 | in_multi_free(struct in_multi *inm) | |
3383 | { | |
3384 | INM_LOCK(inm); | |
3385 | if (inm->inm_debug & IFD_ATTACHED) { | |
3386 | panic("%s: attached inm=%p is being freed", __func__, inm); | |
3387 | /* NOTREACHED */ | |
3388 | } else if (inm->inm_ifma != NULL) { | |
3389 | panic("%s: ifma not NULL for inm=%p", __func__, inm); | |
3390 | /* NOTREACHED */ | |
3391 | } else if (!(inm->inm_debug & IFD_ALLOC)) { | |
3392 | panic("%s: inm %p cannot be freed", __func__, inm); | |
3393 | /* NOTREACHED */ | |
3394 | } else if (inm->inm_refcount != 0) { | |
3395 | panic("%s: non-zero refcount inm=%p", __func__, inm); | |
3396 | /* NOTREACHED */ | |
3397 | } else if (inm->inm_reqcnt != 0) { | |
3398 | panic("%s: non-zero reqcnt inm=%p", __func__, inm); | |
3399 | /* NOTREACHED */ | |
3400 | } | |
3401 | ||
3402 | /* Free any pending IGMPv3 state-change records */ | |
3403 | IF_DRAIN(&inm->inm_scq); | |
3404 | ||
3405 | inm->inm_debug &= ~IFD_ALLOC; | |
3406 | if ((inm->inm_debug & (IFD_DEBUG | IFD_TRASHED)) == | |
3407 | (IFD_DEBUG | IFD_TRASHED)) { | |
3408 | lck_mtx_lock(&inm_trash_lock); | |
3409 | TAILQ_REMOVE(&inm_trash_head, (struct in_multi_dbg *)inm, | |
3410 | inm_trash_link); | |
3411 | lck_mtx_unlock(&inm_trash_lock); | |
3412 | inm->inm_debug &= ~IFD_TRASHED; | |
3413 | } | |
3414 | INM_UNLOCK(inm); | |
3415 | ||
3416 | lck_mtx_destroy(&inm->inm_lock, in_multihead_lock_grp); | |
3417 | zfree(inm_zone, inm); | |
3418 | } | |
3419 | ||
3420 | static void | |
3421 | in_multi_attach(struct in_multi *inm) | |
3422 | { | |
3423 | in_multihead_lock_assert(LCK_RW_ASSERT_EXCLUSIVE); | |
3424 | INM_LOCK_ASSERT_HELD(inm); | |
3425 | ||
3426 | if (inm->inm_debug & IFD_ATTACHED) { | |
3427 | panic("%s: Attempt to attach an already attached inm=%p", | |
3428 | __func__, inm); | |
3429 | /* NOTREACHED */ | |
3430 | } else if (inm->inm_debug & IFD_TRASHED) { | |
3431 | panic("%s: Attempt to reattach a detached inm=%p", | |
3432 | __func__, inm); | |
3433 | /* NOTREACHED */ | |
3434 | } | |
3435 | ||
3436 | inm->inm_reqcnt++; | |
3437 | VERIFY(inm->inm_reqcnt == 1); | |
3438 | INM_ADDREF_LOCKED(inm); | |
3439 | inm->inm_debug |= IFD_ATTACHED; | |
3440 | /* | |
3441 | * Reattach case: If debugging is enabled, take it | |
3442 | * out of the trash list and clear IFD_TRASHED. | |
3443 | */ | |
3444 | if ((inm->inm_debug & (IFD_DEBUG | IFD_TRASHED)) == | |
3445 | (IFD_DEBUG | IFD_TRASHED)) { | |
3446 | /* Become a regular mutex, just in case */ | |
3447 | INM_CONVERT_LOCK(inm); | |
3448 | lck_mtx_lock(&inm_trash_lock); | |
3449 | TAILQ_REMOVE(&inm_trash_head, (struct in_multi_dbg *)inm, | |
3450 | inm_trash_link); | |
3451 | lck_mtx_unlock(&inm_trash_lock); | |
3452 | inm->inm_debug &= ~IFD_TRASHED; | |
3453 | } | |
3454 | ||
3455 | LIST_INSERT_HEAD(&in_multihead, inm, inm_link); | |
3456 | } | |
3457 | ||
3458 | int | |
3459 | in_multi_detach(struct in_multi *inm) | |
3460 | { | |
3461 | in_multihead_lock_assert(LCK_RW_ASSERT_EXCLUSIVE); | |
3462 | INM_LOCK_ASSERT_HELD(inm); | |
3463 | ||
3464 | if (inm->inm_reqcnt == 0) { | |
3465 | panic("%s: inm=%p negative reqcnt", __func__, inm); | |
3466 | /* NOTREACHED */ | |
3467 | } | |
3468 | ||
3469 | --inm->inm_reqcnt; | |
0a7de745 A |
3470 | if (inm->inm_reqcnt > 0) { |
3471 | return 0; | |
3472 | } | |
6d2010ae A |
3473 | |
3474 | if (!(inm->inm_debug & IFD_ATTACHED)) { | |
3475 | panic("%s: Attempt to detach an unattached record inm=%p", | |
3476 | __func__, inm); | |
3477 | /* NOTREACHED */ | |
3478 | } else if (inm->inm_debug & IFD_TRASHED) { | |
3479 | panic("%s: inm %p is already in trash list", __func__, inm); | |
3480 | /* NOTREACHED */ | |
3481 | } | |
3482 | ||
3483 | /* | |
3484 | * NOTE: Caller calls IFMA_REMREF | |
3485 | */ | |
3486 | inm->inm_debug &= ~IFD_ATTACHED; | |
3487 | LIST_REMOVE(inm, inm_link); | |
3488 | ||
3489 | if (inm->inm_debug & IFD_DEBUG) { | |
3490 | /* Become a regular mutex, just in case */ | |
3491 | INM_CONVERT_LOCK(inm); | |
3492 | lck_mtx_lock(&inm_trash_lock); | |
3493 | TAILQ_INSERT_TAIL(&inm_trash_head, | |
3494 | (struct in_multi_dbg *)inm, inm_trash_link); | |
3495 | lck_mtx_unlock(&inm_trash_lock); | |
3496 | inm->inm_debug |= IFD_TRASHED; | |
3497 | } | |
3498 | ||
0a7de745 | 3499 | return 1; |
6d2010ae A |
3500 | } |
3501 | ||
3502 | void | |
3503 | inm_addref(struct in_multi *inm, int locked) | |
3504 | { | |
0a7de745 | 3505 | if (!locked) { |
6d2010ae | 3506 | INM_LOCK_SPIN(inm); |
0a7de745 | 3507 | } else { |
6d2010ae | 3508 | INM_LOCK_ASSERT_HELD(inm); |
0a7de745 | 3509 | } |
6d2010ae A |
3510 | |
3511 | if (++inm->inm_refcount == 0) { | |
3512 | panic("%s: inm=%p wraparound refcnt", __func__, inm); | |
3513 | /* NOTREACHED */ | |
3514 | } else if (inm->inm_trace != NULL) { | |
3515 | (*inm->inm_trace)(inm, TRUE); | |
3516 | } | |
0a7de745 | 3517 | if (!locked) { |
6d2010ae | 3518 | INM_UNLOCK(inm); |
0a7de745 | 3519 | } |
6d2010ae A |
3520 | } |
3521 | ||
3522 | void | |
3523 | inm_remref(struct in_multi *inm, int locked) | |
3524 | { | |
3525 | struct ifmultiaddr *ifma; | |
3526 | struct igmp_ifinfo *igi; | |
3527 | ||
0a7de745 | 3528 | if (!locked) { |
6d2010ae | 3529 | INM_LOCK_SPIN(inm); |
0a7de745 | 3530 | } else { |
6d2010ae | 3531 | INM_LOCK_ASSERT_HELD(inm); |
0a7de745 | 3532 | } |
6d2010ae A |
3533 | |
3534 | if (inm->inm_refcount == 0 || (inm->inm_refcount == 1 && locked)) { | |
3535 | panic("%s: inm=%p negative/missing refcnt", __func__, inm); | |
3536 | /* NOTREACHED */ | |
3537 | } else if (inm->inm_trace != NULL) { | |
3538 | (*inm->inm_trace)(inm, FALSE); | |
3539 | } | |
3540 | ||
3541 | --inm->inm_refcount; | |
3542 | if (inm->inm_refcount > 0) { | |
0a7de745 | 3543 | if (!locked) { |
6d2010ae | 3544 | INM_UNLOCK(inm); |
0a7de745 | 3545 | } |
6d2010ae A |
3546 | return; |
3547 | } | |
3548 | ||
3549 | /* | |
3550 | * Synchronization with in_getmulti(). In the event the inm has been | |
3551 | * detached, the underlying ifma would still be in the if_multiaddrs | |
3552 | * list, and thus can be looked up via if_addmulti(). At that point, | |
3553 | * the only way to find this inm is via ifma_protospec. To avoid | |
3554 | * race conditions between the last inm_remref() of that inm and its | |
3555 | * use via ifma_protospec, in_multihead lock is used for serialization. | |
3556 | * In order to avoid violating the lock order, we must drop inm_lock | |
3557 | * before acquiring in_multihead lock. To prevent the inm from being | |
3558 | * freed prematurely, we hold an extra reference. | |
3559 | */ | |
3560 | ++inm->inm_refcount; | |
3561 | INM_UNLOCK(inm); | |
3562 | in_multihead_lock_shared(); | |
3563 | INM_LOCK_SPIN(inm); | |
3564 | --inm->inm_refcount; | |
3565 | if (inm->inm_refcount > 0) { | |
3566 | /* We've lost the race, so abort since inm is still in use */ | |
3567 | INM_UNLOCK(inm); | |
3568 | in_multihead_lock_done(); | |
3569 | /* If it was locked, return it as such */ | |
0a7de745 | 3570 | if (locked) { |
6d2010ae | 3571 | INM_LOCK(inm); |
0a7de745 | 3572 | } |
6d2010ae A |
3573 | return; |
3574 | } | |
3575 | inm_purge(inm); | |
3576 | ifma = inm->inm_ifma; | |
3577 | inm->inm_ifma = NULL; | |
3578 | inm->inm_ifp = NULL; | |
3579 | igi = inm->inm_igi; | |
3580 | inm->inm_igi = NULL; | |
3581 | INM_UNLOCK(inm); | |
3582 | IFMA_LOCK_SPIN(ifma); | |
3583 | ifma->ifma_protospec = NULL; | |
3584 | IFMA_UNLOCK(ifma); | |
3585 | in_multihead_lock_done(); | |
3586 | ||
3587 | in_multi_free(inm); | |
3588 | if_delmulti_ifma(ifma); | |
3589 | /* Release reference held to the underlying ifmultiaddr */ | |
3590 | IFMA_REMREF(ifma); | |
3591 | ||
0a7de745 | 3592 | if (igi != NULL) { |
6d2010ae | 3593 | IGI_REMREF(igi); |
0a7de745 | 3594 | } |
6d2010ae A |
3595 | } |
3596 | ||
3597 | static void | |
3598 | inm_trace(struct in_multi *inm, int refhold) | |
3599 | { | |
3600 | struct in_multi_dbg *inm_dbg = (struct in_multi_dbg *)inm; | |
3601 | ctrace_t *tr; | |
3602 | u_int32_t idx; | |
3603 | u_int16_t *cnt; | |
3604 | ||
3605 | if (!(inm->inm_debug & IFD_DEBUG)) { | |
3606 | panic("%s: inm %p has no debug structure", __func__, inm); | |
3607 | /* NOTREACHED */ | |
3608 | } | |
3609 | if (refhold) { | |
3610 | cnt = &inm_dbg->inm_refhold_cnt; | |
3611 | tr = inm_dbg->inm_refhold; | |
3612 | } else { | |
3613 | cnt = &inm_dbg->inm_refrele_cnt; | |
3614 | tr = inm_dbg->inm_refrele; | |
3615 | } | |
3616 | ||
3617 | idx = atomic_add_16_ov(cnt, 1) % INM_TRACE_HIST_SIZE; | |
3618 | ctrace_record(&tr[idx]); | |
3619 | } | |
3620 | ||
3621 | void | |
3622 | in_multihead_lock_exclusive(void) | |
3623 | { | |
3624 | lck_rw_lock_exclusive(&in_multihead_lock); | |
3625 | } | |
3626 | ||
3627 | void | |
3628 | in_multihead_lock_shared(void) | |
3629 | { | |
3630 | lck_rw_lock_shared(&in_multihead_lock); | |
3631 | } | |
3632 | ||
3633 | void | |
3634 | in_multihead_lock_assert(int what) | |
3635 | { | |
5ba3f43e A |
3636 | #if !MACH_ASSERT |
3637 | #pragma unused(what) | |
3638 | #endif | |
3639 | LCK_RW_ASSERT(&in_multihead_lock, what); | |
6d2010ae A |
3640 | } |
3641 | ||
3642 | void | |
3643 | in_multihead_lock_done(void) | |
3644 | { | |
3645 | lck_rw_done(&in_multihead_lock); | |
3646 | } | |
3647 | ||
3648 | static struct ip_msource * | |
f427ee49 | 3649 | ipms_alloc(zalloc_flags_t how) |
6d2010ae | 3650 | { |
f427ee49 | 3651 | return zalloc_flags(ipms_zone, how | Z_ZERO); |
6d2010ae A |
3652 | } |
3653 | ||
3654 | static void | |
3655 | ipms_free(struct ip_msource *ims) | |
3656 | { | |
3657 | zfree(ipms_zone, ims); | |
3658 | } | |
3659 | ||
3660 | static struct in_msource * | |
f427ee49 | 3661 | inms_alloc(zalloc_flags_t how) |
6d2010ae | 3662 | { |
f427ee49 | 3663 | return zalloc_flags(inms_zone, how | Z_ZERO); |
6d2010ae A |
3664 | } |
3665 | ||
3666 | static void | |
3667 | inms_free(struct in_msource *inms) | |
3668 | { | |
3669 | zfree(inms_zone, inms); | |
3670 | } | |
3671 | ||
3672 | #ifdef IGMP_DEBUG | |
3673 | ||
3674 | static const char *inm_modestrs[] = { "un\n", "in", "ex" }; | |
3675 | ||
3676 | static const char * | |
3677 | inm_mode_str(const int mode) | |
3678 | { | |
0a7de745 A |
3679 | if (mode >= MCAST_UNDEFINED && mode <= MCAST_EXCLUDE) { |
3680 | return inm_modestrs[mode]; | |
3681 | } | |
3682 | return "??"; | |
6d2010ae A |
3683 | } |
3684 | ||
3685 | static const char *inm_statestrs[] = { | |
3686 | "not-member\n", | |
3687 | "silent\n", | |
39236c6e | 3688 | "reporting\n", |
6d2010ae A |
3689 | "idle\n", |
3690 | "lazy\n", | |
3691 | "sleeping\n", | |
3692 | "awakening\n", | |
3693 | "query-pending\n", | |
3694 | "sg-query-pending\n", | |
3695 | "leaving" | |
3696 | }; | |
3697 | ||
3698 | static const char * | |
3699 | inm_state_str(const int state) | |
3700 | { | |
0a7de745 A |
3701 | if (state >= IGMP_NOT_MEMBER && state <= IGMP_LEAVING_MEMBER) { |
3702 | return inm_statestrs[state]; | |
3703 | } | |
3704 | return "??"; | |
6d2010ae A |
3705 | } |
3706 | ||
3707 | /* | |
3708 | * Dump an in_multi structure to the console. | |
3709 | */ | |
3710 | void | |
3711 | inm_print(const struct in_multi *inm) | |
3712 | { | |
3713 | int t; | |
39236c6e | 3714 | char buf[MAX_IPv4_STR_LEN]; |
6d2010ae | 3715 | |
39236c6e | 3716 | INM_LOCK_ASSERT_HELD(__DECONST(struct in_multi *, inm)); |
6d2010ae | 3717 | |
0a7de745 | 3718 | if (igmp_debug == 0) { |
6d2010ae | 3719 | return; |
0a7de745 | 3720 | } |
6d2010ae | 3721 | |
39236c6e A |
3722 | inet_ntop(AF_INET, &inm->inm_addr, buf, sizeof(buf)); |
3723 | printf("%s: --- begin inm 0x%llx ---\n", __func__, | |
3724 | (uint64_t)VM_KERNEL_ADDRPERM(inm)); | |
3725 | printf("addr %s ifp 0x%llx(%s) ifma 0x%llx\n", | |
3726 | buf, | |
3727 | (uint64_t)VM_KERNEL_ADDRPERM(inm->inm_ifp), | |
3728 | if_name(inm->inm_ifp), | |
3729 | (uint64_t)VM_KERNEL_ADDRPERM(inm->inm_ifma)); | |
6d2010ae A |
3730 | printf("timer %u state %s refcount %u scq.len %u\n", |
3731 | inm->inm_timer, | |
3732 | inm_state_str(inm->inm_state), | |
3733 | inm->inm_refcount, | |
3734 | inm->inm_scq.ifq_len); | |
39236c6e A |
3735 | printf("igi 0x%llx nsrc %lu sctimer %u scrv %u\n", |
3736 | (uint64_t)VM_KERNEL_ADDRPERM(inm->inm_igi), | |
6d2010ae A |
3737 | inm->inm_nsrc, |
3738 | inm->inm_sctimer, | |
3739 | inm->inm_scrv); | |
3740 | for (t = 0; t < 2; t++) { | |
3741 | printf("t%d: fmode %s asm %u ex %u in %u rec %u\n", t, | |
3742 | inm_mode_str(inm->inm_st[t].iss_fmode), | |
3743 | inm->inm_st[t].iss_asm, | |
3744 | inm->inm_st[t].iss_ex, | |
3745 | inm->inm_st[t].iss_in, | |
3746 | inm->inm_st[t].iss_rec); | |
3747 | } | |
39236c6e A |
3748 | printf("%s: --- end inm 0x%llx ---\n", __func__, |
3749 | (uint64_t)VM_KERNEL_ADDRPERM(inm)); | |
6d2010ae A |
3750 | } |
3751 | ||
3752 | #else | |
3753 | ||
3754 | void | |
3755 | inm_print(__unused const struct in_multi *inm) | |
3756 | { | |
6d2010ae A |
3757 | } |
3758 | ||
3759 | #endif |