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