]>
Commit | Line | Data |
---|---|---|
1 | /* | |
2 | * Copyright (c) 2000-2010 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) 1988 Stephen Deering. | |
31 | * Copyright (c) 1992, 1993 | |
32 | * The Regents of the University of California. All rights reserved. | |
33 | * | |
34 | * This code is derived from software contributed to Berkeley by | |
35 | * Stephen Deering of Stanford University. | |
36 | * | |
37 | * Redistribution and use in source and binary forms, with or without | |
38 | * modification, are permitted provided that the following conditions | |
39 | * are met: | |
40 | * 1. Redistributions of source code must retain the above copyright | |
41 | * notice, this list of conditions and the following disclaimer. | |
42 | * 2. Redistributions in binary form must reproduce the above copyright | |
43 | * notice, this list of conditions and the following disclaimer in the | |
44 | * documentation and/or other materials provided with the distribution. | |
45 | * 3. All advertising materials mentioning features or use of this software | |
46 | * must display the following acknowledgement: | |
47 | * This product includes software developed by the University of | |
48 | * California, Berkeley and its contributors. | |
49 | * 4. Neither the name of the University nor the names of its contributors | |
50 | * may be used to endorse or promote products derived from this software | |
51 | * without specific prior written permission. | |
52 | * | |
53 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | |
54 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
55 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
56 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | |
57 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
58 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | |
59 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
60 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | |
61 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | |
62 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |
63 | * SUCH DAMAGE. | |
64 | * | |
65 | * @(#)igmp.c 8.1 (Berkeley) 7/19/93 | |
66 | */ | |
67 | /* | |
68 | * NOTICE: This file was modified by SPARTA, Inc. in 2005 to introduce | |
69 | * support for mandatory and extensible security protections. This notice | |
70 | * is included in support of clause 2.2 (b) of the Apple Public License, | |
71 | * Version 2.0. | |
72 | */ | |
73 | ||
74 | /* | |
75 | * Internet Group Management Protocol (IGMP) routines. | |
76 | * [RFC1112, RFC2236, RFC3376] | |
77 | * | |
78 | * Written by Steve Deering, Stanford, May 1988. | |
79 | * Modified by Rosen Sharma, Stanford, Aug 1994. | |
80 | * Modified by Bill Fenner, Xerox PARC, Feb 1995. | |
81 | * Modified to fully comply to IGMPv2 by Bill Fenner, Oct 1995. | |
82 | * Significantly rewritten for IGMPv3, VIMAGE, and SMP by Bruce Simpson. | |
83 | * | |
84 | * MULTICAST Revision: 3.5.1.4 | |
85 | */ | |
86 | ||
87 | #include <sys/cdefs.h> | |
88 | ||
89 | #include <sys/param.h> | |
90 | #include <sys/systm.h> | |
91 | #include <sys/malloc.h> | |
92 | #include <sys/mbuf.h> | |
93 | #include <sys/socket.h> | |
94 | #include <sys/protosw.h> | |
95 | #include <sys/kernel.h> | |
96 | #include <sys/sysctl.h> | |
97 | #include <sys/mcache.h> | |
98 | ||
99 | #include <libkern/libkern.h> | |
100 | #include <kern/zalloc.h> | |
101 | ||
102 | #include <net/if.h> | |
103 | #include <net/route.h> | |
104 | ||
105 | #include <netinet/in.h> | |
106 | #include <netinet/in_var.h> | |
107 | #include <netinet/in_systm.h> | |
108 | #include <netinet/ip.h> | |
109 | #include <netinet/ip_var.h> | |
110 | #include <netinet/igmp.h> | |
111 | #include <netinet/igmp_var.h> | |
112 | #include <netinet/kpi_ipfilter_var.h> | |
113 | ||
114 | #ifdef IGMP_DEBUG | |
115 | __inline__ char * | |
116 | inet_ntoa(struct in_addr ina) | |
117 | { | |
118 | static char buf[4*sizeof "123"]; | |
119 | unsigned char *ucp = (unsigned char *)&ina; | |
120 | ||
121 | snprintf(buf, sizeof(buf), "%d.%d.%d.%d", | |
122 | ucp[0] & 0xff, | |
123 | ucp[1] & 0xff, | |
124 | ucp[2] & 0xff, | |
125 | ucp[3] & 0xff); | |
126 | return buf; | |
127 | } | |
128 | #endif | |
129 | ||
130 | static void igi_initvar(struct igmp_ifinfo *, struct ifnet *, int); | |
131 | static struct igmp_ifinfo *igi_alloc(int); | |
132 | static void igi_free(struct igmp_ifinfo *); | |
133 | static void igi_delete(const struct ifnet *); | |
134 | static void igmp_dispatch_queue(struct igmp_ifinfo *, struct ifqueue *, | |
135 | int, const int, struct ifnet *); | |
136 | static void igmp_final_leave(struct in_multi *, struct igmp_ifinfo *); | |
137 | static int igmp_handle_state_change(struct in_multi *, | |
138 | struct igmp_ifinfo *); | |
139 | static int igmp_initial_join(struct in_multi *, struct igmp_ifinfo *); | |
140 | static int igmp_input_v1_query(struct ifnet *, const struct ip *, | |
141 | const struct igmp *); | |
142 | static int igmp_input_v2_query(struct ifnet *, const struct ip *, | |
143 | const struct igmp *); | |
144 | static int igmp_input_v3_query(struct ifnet *, const struct ip *, | |
145 | /*const*/ struct igmpv3 *); | |
146 | static int igmp_input_v3_group_query(struct in_multi *, | |
147 | int, /*const*/ struct igmpv3 *); | |
148 | static int igmp_input_v1_report(struct ifnet *, /*const*/ struct ip *, | |
149 | /*const*/ struct igmp *); | |
150 | static int igmp_input_v2_report(struct ifnet *, /*const*/ struct ip *, | |
151 | /*const*/ struct igmp *); | |
152 | void igmp_sendpkt(struct mbuf *, struct ifnet *); | |
153 | static __inline__ int igmp_isgroupreported(const struct in_addr); | |
154 | static struct mbuf * | |
155 | igmp_ra_alloc(void); | |
156 | #ifdef IGMP_DEBUG | |
157 | static const char * igmp_rec_type_to_str(const int); | |
158 | #endif | |
159 | static void igmp_set_version(struct igmp_ifinfo *, const int); | |
160 | static void igmp_flush_relq(struct igmp_ifinfo *); | |
161 | static int igmp_v1v2_queue_report(struct in_multi *, const int); | |
162 | static void igmp_v1v2_process_group_timer(struct in_multi *, const int); | |
163 | static void igmp_v1v2_process_querier_timers(struct igmp_ifinfo *); | |
164 | static void igmp_v2_update_group(struct in_multi *, const int); | |
165 | static void igmp_v3_cancel_link_timers(struct igmp_ifinfo *); | |
166 | static void igmp_v3_dispatch_general_query(struct igmp_ifinfo *); | |
167 | static struct mbuf * | |
168 | igmp_v3_encap_report(struct ifnet *, struct mbuf *); | |
169 | static int igmp_v3_enqueue_group_record(struct ifqueue *, | |
170 | struct in_multi *, const int, const int, const int); | |
171 | static int igmp_v3_enqueue_filter_change(struct ifqueue *, | |
172 | struct in_multi *); | |
173 | static void igmp_v3_process_group_timers(struct igmp_ifinfo *, | |
174 | struct ifqueue *, struct ifqueue *, struct in_multi *, | |
175 | const int); | |
176 | static int igmp_v3_merge_state_changes(struct in_multi *, | |
177 | struct ifqueue *); | |
178 | static void igmp_v3_suppress_group_record(struct in_multi *); | |
179 | static int sysctl_igmp_ifinfo SYSCTL_HANDLER_ARGS; | |
180 | static int sysctl_igmp_gsr SYSCTL_HANDLER_ARGS; | |
181 | static int sysctl_igmp_default_version SYSCTL_HANDLER_ARGS; | |
182 | ||
183 | struct mbuf *m_raopt; /* Router Alert option */ | |
184 | ||
185 | static int interface_timers_running; /* IGMPv3 general | |
186 | * query response */ | |
187 | static int state_change_timers_running; /* IGMPv3 state-change | |
188 | * retransmit */ | |
189 | static int current_state_timers_running; /* IGMPv1/v2 host | |
190 | * report; IGMPv3 g/sg | |
191 | * query response */ | |
192 | ||
193 | static LIST_HEAD(, igmp_ifinfo) igi_head; | |
194 | static struct igmpstat_v3 igmpstat_v3 = { | |
195 | .igps_version = IGPS_VERSION_3, | |
196 | .igps_len = sizeof(struct igmpstat_v3), | |
197 | }; | |
198 | static struct igmpstat igmpstat; /* old IGMPv2 stats structure */ | |
199 | static struct timeval igmp_gsrdelay = {10, 0}; | |
200 | ||
201 | static int igmp_recvifkludge = 1; | |
202 | static int igmp_sendra = 1; | |
203 | static int igmp_sendlocal = 1; | |
204 | static int igmp_v1enable = 1; | |
205 | static int igmp_v2enable = 1; | |
206 | static int igmp_legacysupp = 0; | |
207 | static int igmp_default_version = IGMP_VERSION_3; | |
208 | ||
209 | SYSCTL_STRUCT(_net_inet_igmp, IGMPCTL_STATS, stats, CTLFLAG_RD | CTLFLAG_LOCKED, | |
210 | &igmpstat, igmpstat, ""); | |
211 | SYSCTL_STRUCT(_net_inet_igmp, OID_AUTO, v3stats, | |
212 | CTLFLAG_RD | CTLFLAG_LOCKED, &igmpstat_v3, igmpstat_v3, ""); | |
213 | SYSCTL_INT(_net_inet_igmp, OID_AUTO, recvifkludge, CTLFLAG_RW | CTLFLAG_LOCKED, | |
214 | &igmp_recvifkludge, 0, | |
215 | "Rewrite IGMPv1/v2 reports from 0.0.0.0 to contain subnet address"); | |
216 | SYSCTL_INT(_net_inet_igmp, OID_AUTO, sendra, CTLFLAG_RW | CTLFLAG_LOCKED, | |
217 | &igmp_sendra, 0, | |
218 | "Send IP Router Alert option in IGMPv2/v3 messages"); | |
219 | SYSCTL_INT(_net_inet_igmp, OID_AUTO, sendlocal, CTLFLAG_RW | CTLFLAG_LOCKED, | |
220 | &igmp_sendlocal, 0, | |
221 | "Send IGMP membership reports for 224.0.0.0/24 groups"); | |
222 | SYSCTL_INT(_net_inet_igmp, OID_AUTO, v1enable, CTLFLAG_RW | CTLFLAG_LOCKED, | |
223 | &igmp_v1enable, 0, | |
224 | "Enable backwards compatibility with IGMPv1"); | |
225 | SYSCTL_INT(_net_inet_igmp, OID_AUTO, v2enable, CTLFLAG_RW | CTLFLAG_LOCKED, | |
226 | &igmp_v2enable, 0, | |
227 | "Enable backwards compatibility with IGMPv2"); | |
228 | SYSCTL_INT(_net_inet_igmp, OID_AUTO, legacysupp, CTLFLAG_RW | CTLFLAG_LOCKED, | |
229 | &igmp_legacysupp, 0, | |
230 | "Allow v1/v2 reports to suppress v3 group responses"); | |
231 | SYSCTL_PROC(_net_inet_igmp, OID_AUTO, default_version, | |
232 | CTLTYPE_INT | CTLFLAG_RW, | |
233 | &igmp_default_version, 0, sysctl_igmp_default_version, "I", | |
234 | "Default version of IGMP to run on each interface"); | |
235 | SYSCTL_PROC(_net_inet_igmp, OID_AUTO, gsrdelay, | |
236 | CTLTYPE_INT | CTLFLAG_RW, | |
237 | &igmp_gsrdelay.tv_sec, 0, sysctl_igmp_gsr, "I", | |
238 | "Rate limit for IGMPv3 Group-and-Source queries in seconds"); | |
239 | #ifdef IGMP_DEBUG | |
240 | int igmp_debug = 0; | |
241 | SYSCTL_INT(_net_inet_igmp, OID_AUTO, | |
242 | debug, CTLFLAG_RW | CTLFLAG_LOCKED, &igmp_debug, 0, ""); | |
243 | #endif | |
244 | ||
245 | SYSCTL_NODE(_net_inet_igmp, OID_AUTO, ifinfo, CTLFLAG_RD | CTLFLAG_LOCKED, | |
246 | sysctl_igmp_ifinfo, "Per-interface IGMPv3 state"); | |
247 | ||
248 | /* Lock group and attribute for igmp_mtx */ | |
249 | static lck_attr_t *igmp_mtx_attr; | |
250 | static lck_grp_t *igmp_mtx_grp; | |
251 | static lck_grp_attr_t *igmp_mtx_grp_attr; | |
252 | ||
253 | /* | |
254 | * Locking and reference counting: | |
255 | * | |
256 | * igmp_mtx mainly protects igi_head. In cases where both igmp_mtx and | |
257 | * in_multihead_lock must be held, the former must be acquired first in order | |
258 | * to maintain lock ordering. It is not a requirement that igmp_mtx be | |
259 | * acquired first before in_multihead_lock, but in case both must be acquired | |
260 | * in succession, the correct lock ordering must be followed. | |
261 | * | |
262 | * Instead of walking the if_multiaddrs list at the interface and returning | |
263 | * the ifma_protospec value of a matching entry, we search the global list | |
264 | * of in_multi records and find it that way; this is done with in_multihead | |
265 | * lock held. Doing so avoids the race condition issues that many other BSDs | |
266 | * suffer from (therefore in our implementation, ifma_protospec will never be | |
267 | * NULL for as long as the in_multi is valid.) | |
268 | * | |
269 | * The above creates a requirement for the in_multi to stay in in_multihead | |
270 | * list even after the final IGMP leave (in IGMPv3 mode) until no longer needs | |
271 | * be retransmitted (this is not required for IGMPv1/v2.) In order to handle | |
272 | * this, the request and reference counts of the in_multi are bumped up when | |
273 | * the state changes to IGMP_LEAVING_MEMBER, and later dropped in the timeout | |
274 | * handler. Each in_multi holds a reference to the underlying igmp_ifinfo. | |
275 | * | |
276 | * Thus, the permitted lock oder is: | |
277 | * | |
278 | * igmp_mtx, in_multihead_lock, inm_lock, igi_lock | |
279 | * | |
280 | * Any may be taken independently, but if any are held at the same time, | |
281 | * the above lock order must be followed. | |
282 | */ | |
283 | static decl_lck_mtx_data(, igmp_mtx); | |
284 | static int igmp_timers_are_running; | |
285 | ||
286 | #define IGI_ZONE_MAX 64 /* maximum elements in zone */ | |
287 | #define IGI_ZONE_NAME "igmp_ifinfo" /* zone name */ | |
288 | ||
289 | static unsigned int igi_size; /* size of zone element */ | |
290 | static struct zone *igi_zone; /* zone for igmp_ifinfo */ | |
291 | ||
292 | #ifdef IGMP_DEBUG | |
293 | static __inline char * | |
294 | inet_ntoa_haddr(in_addr_t haddr) | |
295 | { | |
296 | struct in_addr ia; | |
297 | ||
298 | ia.s_addr = htonl(haddr); | |
299 | return (inet_ntoa(ia)); | |
300 | } | |
301 | #endif | |
302 | /* | |
303 | * Retrieve or set default IGMP version. | |
304 | */ | |
305 | static int | |
306 | sysctl_igmp_default_version SYSCTL_HANDLER_ARGS | |
307 | { | |
308 | #pragma unused(oidp, arg2) | |
309 | int error; | |
310 | int new; | |
311 | ||
312 | lck_mtx_lock(&igmp_mtx); | |
313 | ||
314 | error = SYSCTL_OUT(req, arg1, sizeof(int)); | |
315 | if (error || !req->newptr) | |
316 | goto out_locked; | |
317 | ||
318 | new = igmp_default_version; | |
319 | ||
320 | error = SYSCTL_IN(req, &new, sizeof(int)); | |
321 | if (error) | |
322 | goto out_locked; | |
323 | ||
324 | if (new < IGMP_VERSION_1 || new > IGMP_VERSION_3) { | |
325 | error = EINVAL; | |
326 | goto out_locked; | |
327 | } | |
328 | ||
329 | IGMP_PRINTF(("change igmp_default_version from %d to %d\n", | |
330 | igmp_default_version, new)); | |
331 | ||
332 | igmp_default_version = new; | |
333 | ||
334 | out_locked: | |
335 | lck_mtx_unlock(&igmp_mtx); | |
336 | return (error); | |
337 | } | |
338 | ||
339 | /* | |
340 | * Retrieve or set threshold between group-source queries in seconds. | |
341 | * | |
342 | */ | |
343 | static int | |
344 | sysctl_igmp_gsr SYSCTL_HANDLER_ARGS | |
345 | { | |
346 | #pragma unused(arg1, arg2) | |
347 | int error; | |
348 | int i; | |
349 | ||
350 | lck_mtx_lock(&igmp_mtx); | |
351 | ||
352 | i = igmp_gsrdelay.tv_sec; | |
353 | ||
354 | error = sysctl_handle_int(oidp, &i, 0, req); | |
355 | if (error || !req->newptr) | |
356 | goto out_locked; | |
357 | ||
358 | if (i < -1 || i >= 60) { | |
359 | error = EINVAL; | |
360 | goto out_locked; | |
361 | } | |
362 | ||
363 | igmp_gsrdelay.tv_sec = i; | |
364 | ||
365 | out_locked: | |
366 | lck_mtx_unlock(&igmp_mtx); | |
367 | return (error); | |
368 | } | |
369 | ||
370 | /* | |
371 | * Expose struct igmp_ifinfo to userland, keyed by ifindex. | |
372 | * For use by ifmcstat(8). | |
373 | * | |
374 | */ | |
375 | static int | |
376 | sysctl_igmp_ifinfo SYSCTL_HANDLER_ARGS | |
377 | { | |
378 | #pragma unused(oidp) | |
379 | int *name; | |
380 | int error; | |
381 | u_int namelen; | |
382 | struct ifnet *ifp; | |
383 | struct igmp_ifinfo *igi; | |
384 | struct igmp_ifinfo_u igi_u; | |
385 | ||
386 | name = (int *)arg1; | |
387 | namelen = arg2; | |
388 | ||
389 | if (req->newptr != USER_ADDR_NULL) | |
390 | return (EPERM); | |
391 | ||
392 | if (namelen != 1) | |
393 | return (EINVAL); | |
394 | ||
395 | lck_mtx_lock(&igmp_mtx); | |
396 | ||
397 | if (name[0] <= 0 || name[0] > (u_int)if_index) { | |
398 | error = ENOENT; | |
399 | goto out_locked; | |
400 | } | |
401 | ||
402 | error = ENOENT; | |
403 | ||
404 | ifnet_head_lock_shared(); | |
405 | ifp = ifindex2ifnet[name[0]]; | |
406 | ifnet_head_done(); | |
407 | if (ifp == NULL) | |
408 | goto out_locked; | |
409 | ||
410 | bzero(&igi_u, sizeof (igi_u)); | |
411 | ||
412 | LIST_FOREACH(igi, &igi_head, igi_link) { | |
413 | IGI_LOCK(igi); | |
414 | if (ifp != igi->igi_ifp) { | |
415 | IGI_UNLOCK(igi); | |
416 | continue; | |
417 | } | |
418 | igi_u.igi_ifindex = igi->igi_ifp->if_index; | |
419 | igi_u.igi_version = igi->igi_version; | |
420 | igi_u.igi_v1_timer = igi->igi_v1_timer; | |
421 | igi_u.igi_v2_timer = igi->igi_v2_timer; | |
422 | igi_u.igi_v3_timer = igi->igi_v3_timer; | |
423 | igi_u.igi_flags = igi->igi_flags; | |
424 | igi_u.igi_rv = igi->igi_rv; | |
425 | igi_u.igi_qi = igi->igi_qi; | |
426 | igi_u.igi_qri = igi->igi_qri; | |
427 | igi_u.igi_uri = igi->igi_uri; | |
428 | IGI_UNLOCK(igi); | |
429 | ||
430 | error = SYSCTL_OUT(req, &igi_u, sizeof (igi_u)); | |
431 | break; | |
432 | } | |
433 | ||
434 | out_locked: | |
435 | lck_mtx_unlock(&igmp_mtx); | |
436 | return (error); | |
437 | } | |
438 | ||
439 | /* | |
440 | * Dispatch an entire queue of pending packet chains | |
441 | * | |
442 | * Must not be called with inm_lock held. | |
443 | */ | |
444 | static void | |
445 | igmp_dispatch_queue(struct igmp_ifinfo *igi, struct ifqueue *ifq, int limit, | |
446 | const int loop, struct ifnet *ifp) | |
447 | { | |
448 | struct mbuf *m; | |
449 | struct ip *ip; | |
450 | ||
451 | if (igi != NULL) | |
452 | IGI_LOCK_ASSERT_HELD(igi); | |
453 | ||
454 | for (;;) { | |
455 | IF_DEQUEUE(ifq, m); | |
456 | if (m == NULL) | |
457 | break; | |
458 | IGMP_PRINTF(("%s: dispatch %p from %p\n", __func__, ifq, m)); | |
459 | ip = mtod(m, struct ip *); | |
460 | if (loop) | |
461 | m->m_flags |= M_IGMP_LOOP; | |
462 | if (igi != NULL) | |
463 | IGI_UNLOCK(igi); | |
464 | igmp_sendpkt(m, ifp); | |
465 | if (igi != NULL) | |
466 | IGI_LOCK(igi); | |
467 | if (--limit == 0) | |
468 | break; | |
469 | } | |
470 | ||
471 | if (igi != NULL) | |
472 | IGI_LOCK_ASSERT_HELD(igi); | |
473 | } | |
474 | ||
475 | /* | |
476 | * Filter outgoing IGMP report state by group. | |
477 | * | |
478 | * Reports are ALWAYS suppressed for ALL-HOSTS (224.0.0.1). | |
479 | * If the net.inet.igmp.sendlocal sysctl is 0, then IGMP reports are | |
480 | * disabled for all groups in the 224.0.0.0/24 link-local scope. However, | |
481 | * this may break certain IGMP snooping switches which rely on the old | |
482 | * report behaviour. | |
483 | * | |
484 | * Return zero if the given group is one for which IGMP reports | |
485 | * should be suppressed, or non-zero if reports should be issued. | |
486 | */ | |
487 | ||
488 | static __inline__ | |
489 | int igmp_isgroupreported(const struct in_addr addr) | |
490 | { | |
491 | ||
492 | if (in_allhosts(addr) || | |
493 | ((!igmp_sendlocal && IN_LOCAL_GROUP(ntohl(addr.s_addr))))) | |
494 | return (0); | |
495 | ||
496 | return (1); | |
497 | } | |
498 | ||
499 | /* | |
500 | * Construct a Router Alert option to use in outgoing packets. | |
501 | */ | |
502 | static struct mbuf * | |
503 | igmp_ra_alloc(void) | |
504 | { | |
505 | struct mbuf *m; | |
506 | struct ipoption *p; | |
507 | ||
508 | MGET(m, M_WAITOK, MT_DATA); | |
509 | p = mtod(m, struct ipoption *); | |
510 | p->ipopt_dst.s_addr = INADDR_ANY; | |
511 | p->ipopt_list[0] = IPOPT_RA; /* Router Alert Option */ | |
512 | p->ipopt_list[1] = 0x04; /* 4 bytes long */ | |
513 | p->ipopt_list[2] = IPOPT_EOL; /* End of IP option list */ | |
514 | p->ipopt_list[3] = 0x00; /* pad byte */ | |
515 | m->m_len = sizeof(p->ipopt_dst) + p->ipopt_list[1]; | |
516 | ||
517 | return (m); | |
518 | } | |
519 | ||
520 | /* | |
521 | * Attach IGMP when PF_INET is attached to an interface. | |
522 | */ | |
523 | struct igmp_ifinfo * | |
524 | igmp_domifattach(struct ifnet *ifp, int how) | |
525 | { | |
526 | struct igmp_ifinfo *igi; | |
527 | ||
528 | IGMP_PRINTF(("%s: called for ifp %p(%s)\n", | |
529 | __func__, ifp, ifp->if_name)); | |
530 | ||
531 | igi = igi_alloc(how); | |
532 | if (igi == NULL) | |
533 | return (NULL); | |
534 | ||
535 | lck_mtx_lock(&igmp_mtx); | |
536 | ||
537 | IGI_LOCK(igi); | |
538 | igi_initvar(igi, ifp, 0); | |
539 | igi->igi_debug |= IFD_ATTACHED; | |
540 | IGI_ADDREF_LOCKED(igi); /* hold a reference for igi_head */ | |
541 | IGI_ADDREF_LOCKED(igi); /* hold a reference for caller */ | |
542 | IGI_UNLOCK(igi); | |
543 | ||
544 | LIST_INSERT_HEAD(&igi_head, igi, igi_link); | |
545 | ||
546 | lck_mtx_unlock(&igmp_mtx); | |
547 | ||
548 | IGMP_PRINTF(("allocate igmp_ifinfo for ifp %p(%s)\n", | |
549 | ifp, ifp->if_name)); | |
550 | ||
551 | return (igi); | |
552 | } | |
553 | ||
554 | /* | |
555 | * Attach IGMP when PF_INET is reattached to an interface. Caller is | |
556 | * expected to have an outstanding reference to the igi. | |
557 | */ | |
558 | void | |
559 | igmp_domifreattach(struct igmp_ifinfo *igi) | |
560 | { | |
561 | struct ifnet *ifp; | |
562 | ||
563 | lck_mtx_lock(&igmp_mtx); | |
564 | ||
565 | IGI_LOCK(igi); | |
566 | VERIFY(!(igi->igi_debug & IFD_ATTACHED)); | |
567 | ifp = igi->igi_ifp; | |
568 | VERIFY(ifp != NULL); | |
569 | igi_initvar(igi, ifp, 1); | |
570 | igi->igi_debug |= IFD_ATTACHED; | |
571 | IGI_ADDREF_LOCKED(igi); /* hold a reference for igi_head */ | |
572 | IGI_UNLOCK(igi); | |
573 | ||
574 | LIST_INSERT_HEAD(&igi_head, igi, igi_link); | |
575 | ||
576 | lck_mtx_unlock(&igmp_mtx); | |
577 | ||
578 | IGMP_PRINTF(("reattached igmp_ifinfo for ifp %p(%s)\n", | |
579 | ifp, ifp->if_name)); | |
580 | } | |
581 | ||
582 | /* | |
583 | * Hook for domifdetach. | |
584 | */ | |
585 | void | |
586 | igmp_domifdetach(struct ifnet *ifp) | |
587 | { | |
588 | IGMP_PRINTF(("%s: called for ifp %p(%s%d)\n", | |
589 | __func__, ifp, ifp->if_name, ifp->if_unit)); | |
590 | ||
591 | lck_mtx_lock(&igmp_mtx); | |
592 | igi_delete(ifp); | |
593 | lck_mtx_unlock(&igmp_mtx); | |
594 | } | |
595 | ||
596 | /* | |
597 | * Called at interface detach time. Note that we only flush all deferred | |
598 | * responses and record releases; all remaining inm records and their source | |
599 | * entries related to this interface are left intact, in order to handle | |
600 | * the reattach case. | |
601 | */ | |
602 | static void | |
603 | igi_delete(const struct ifnet *ifp) | |
604 | { | |
605 | struct igmp_ifinfo *igi, *tigi; | |
606 | ||
607 | lck_mtx_assert(&igmp_mtx, LCK_MTX_ASSERT_OWNED); | |
608 | ||
609 | LIST_FOREACH_SAFE(igi, &igi_head, igi_link, tigi) { | |
610 | IGI_LOCK(igi); | |
611 | if (igi->igi_ifp == ifp) { | |
612 | /* | |
613 | * Free deferred General Query responses. | |
614 | */ | |
615 | IF_DRAIN(&igi->igi_gq); | |
616 | IF_DRAIN(&igi->igi_v2q); | |
617 | igmp_flush_relq(igi); | |
618 | VERIFY(SLIST_EMPTY(&igi->igi_relinmhead)); | |
619 | igi->igi_debug &= ~IFD_ATTACHED; | |
620 | IGI_UNLOCK(igi); | |
621 | ||
622 | LIST_REMOVE(igi, igi_link); | |
623 | IGI_REMREF(igi); /* release igi_head reference */ | |
624 | return; | |
625 | } | |
626 | IGI_UNLOCK(igi); | |
627 | } | |
628 | panic("%s: igmp_ifinfo not found for ifp %p\n", __func__, ifp); | |
629 | } | |
630 | ||
631 | static void | |
632 | igi_initvar(struct igmp_ifinfo *igi, struct ifnet *ifp, int reattach) | |
633 | { | |
634 | IGI_LOCK_ASSERT_HELD(igi); | |
635 | ||
636 | igi->igi_ifp = ifp; | |
637 | igi->igi_version = igmp_default_version; | |
638 | igi->igi_flags = 0; | |
639 | igi->igi_rv = IGMP_RV_INIT; | |
640 | igi->igi_qi = IGMP_QI_INIT; | |
641 | igi->igi_qri = IGMP_QRI_INIT; | |
642 | igi->igi_uri = IGMP_URI_INIT; | |
643 | ||
644 | /* ifnet is not yet attached; no need to hold ifnet lock */ | |
645 | if (!(ifp->if_flags & IFF_MULTICAST)) | |
646 | igi->igi_flags |= IGIF_SILENT; | |
647 | ||
648 | if (!reattach) | |
649 | SLIST_INIT(&igi->igi_relinmhead); | |
650 | ||
651 | /* | |
652 | * Responses to general queries are subject to bounds. | |
653 | */ | |
654 | igi->igi_gq.ifq_maxlen = IGMP_MAX_RESPONSE_PACKETS; | |
655 | igi->igi_v2q.ifq_maxlen = IGMP_MAX_RESPONSE_PACKETS; | |
656 | } | |
657 | ||
658 | static struct igmp_ifinfo * | |
659 | igi_alloc(int how) | |
660 | { | |
661 | struct igmp_ifinfo *igi; | |
662 | ||
663 | igi = (how == M_WAITOK) ? zalloc(igi_zone) : zalloc_noblock(igi_zone); | |
664 | if (igi != NULL) { | |
665 | bzero(igi, igi_size); | |
666 | lck_mtx_init(&igi->igi_lock, igmp_mtx_grp, igmp_mtx_attr); | |
667 | igi->igi_debug |= IFD_ALLOC; | |
668 | } | |
669 | return (igi); | |
670 | } | |
671 | ||
672 | static void | |
673 | igi_free(struct igmp_ifinfo *igi) | |
674 | { | |
675 | IGI_LOCK(igi); | |
676 | if (igi->igi_debug & IFD_ATTACHED) { | |
677 | panic("%s: attached igi=%p is being freed", __func__, igi); | |
678 | /* NOTREACHED */ | |
679 | } else if (igi->igi_ifp != NULL) { | |
680 | panic("%s: ifp not NULL for igi=%p", __func__, igi); | |
681 | /* NOTREACHED */ | |
682 | } else if (!(igi->igi_debug & IFD_ALLOC)) { | |
683 | panic("%s: igi %p cannot be freed", __func__, igi); | |
684 | /* NOTREACHED */ | |
685 | } else if (igi->igi_refcnt != 0) { | |
686 | panic("%s: non-zero refcnt igi=%p", __func__, igi); | |
687 | /* NOTREACHED */ | |
688 | } | |
689 | igi->igi_debug &= ~IFD_ALLOC; | |
690 | IGI_UNLOCK(igi); | |
691 | ||
692 | lck_mtx_destroy(&igi->igi_lock, igmp_mtx_grp); | |
693 | zfree(igi_zone, igi); | |
694 | } | |
695 | ||
696 | void | |
697 | igi_addref(struct igmp_ifinfo *igi, int locked) | |
698 | { | |
699 | if (!locked) | |
700 | IGI_LOCK_SPIN(igi); | |
701 | else | |
702 | IGI_LOCK_ASSERT_HELD(igi); | |
703 | ||
704 | if (++igi->igi_refcnt == 0) { | |
705 | panic("%s: igi=%p wraparound refcnt", __func__, igi); | |
706 | /* NOTREACHED */ | |
707 | } | |
708 | if (!locked) | |
709 | IGI_UNLOCK(igi); | |
710 | } | |
711 | ||
712 | void | |
713 | igi_remref(struct igmp_ifinfo *igi) | |
714 | { | |
715 | struct ifnet *ifp; | |
716 | ||
717 | IGI_LOCK_SPIN(igi); | |
718 | ||
719 | if (igi->igi_refcnt == 0) { | |
720 | panic("%s: igi=%p negative refcnt", __func__, igi); | |
721 | /* NOTREACHED */ | |
722 | } | |
723 | ||
724 | --igi->igi_refcnt; | |
725 | if (igi->igi_refcnt > 0) { | |
726 | IGI_UNLOCK(igi); | |
727 | return; | |
728 | } | |
729 | ||
730 | ifp = igi->igi_ifp; | |
731 | igi->igi_ifp = NULL; | |
732 | IF_DRAIN(&igi->igi_gq); | |
733 | IF_DRAIN(&igi->igi_v2q); | |
734 | igmp_flush_relq(igi); | |
735 | VERIFY(SLIST_EMPTY(&igi->igi_relinmhead)); | |
736 | IGI_UNLOCK(igi); | |
737 | ||
738 | IGMP_PRINTF(("%s: freeing igmp_ifinfo for ifp %p(%s%d)\n", | |
739 | __func__, ifp, ifp->if_name, ifp->if_unit)); | |
740 | ||
741 | igi_free(igi); | |
742 | } | |
743 | ||
744 | /* | |
745 | * Process a received IGMPv1 query. | |
746 | * Return non-zero if the message should be dropped. | |
747 | */ | |
748 | static int | |
749 | igmp_input_v1_query(struct ifnet *ifp, const struct ip *ip, | |
750 | const struct igmp *igmp) | |
751 | { | |
752 | struct igmp_ifinfo *igi; | |
753 | struct in_multi *inm; | |
754 | struct in_multistep step; | |
755 | ||
756 | /* | |
757 | * IGMPv1 Host Membership Queries SHOULD always be addressed to | |
758 | * 224.0.0.1. They are always treated as General Queries. | |
759 | * igmp_group is always ignored. Do not drop it as a userland | |
760 | * daemon may wish to see it. | |
761 | */ | |
762 | if (!in_allhosts(ip->ip_dst) || !in_nullhost(igmp->igmp_group)) { | |
763 | IGMPSTAT_INC(igps_rcv_badqueries); | |
764 | OIGMPSTAT_INC(igps_rcv_badqueries); | |
765 | return (0); | |
766 | } | |
767 | IGMPSTAT_INC(igps_rcv_gen_queries); | |
768 | ||
769 | igi = IGMP_IFINFO(ifp); | |
770 | VERIFY(igi != NULL); | |
771 | ||
772 | IGI_LOCK(igi); | |
773 | if (igi->igi_flags & IGIF_LOOPBACK) { | |
774 | IGMP_PRINTF(("ignore v1 query on IGIF_LOOPBACK ifp %p(%s%d)\n", | |
775 | ifp, ifp->if_name, ifp->if_unit)); | |
776 | IGI_UNLOCK(igi); | |
777 | return (0); | |
778 | } | |
779 | /* | |
780 | * Switch to IGMPv1 host compatibility mode. | |
781 | */ | |
782 | igmp_set_version(igi, IGMP_VERSION_1); | |
783 | IGI_UNLOCK(igi); | |
784 | ||
785 | IGMP_PRINTF(("process v1 query on ifp %p(%s%d)\n", ifp, ifp->if_name, | |
786 | ifp->if_unit)); | |
787 | ||
788 | /* | |
789 | * Start the timers in all of our group records | |
790 | * for the interface on which the query arrived, | |
791 | * except those which are already running. | |
792 | */ | |
793 | in_multihead_lock_shared(); | |
794 | IN_FIRST_MULTI(step, inm); | |
795 | while (inm != NULL) { | |
796 | INM_LOCK(inm); | |
797 | if (inm->inm_ifp != ifp) | |
798 | goto next; | |
799 | if (inm->inm_timer != 0) | |
800 | goto next; | |
801 | ||
802 | switch (inm->inm_state) { | |
803 | case IGMP_NOT_MEMBER: | |
804 | case IGMP_SILENT_MEMBER: | |
805 | break; | |
806 | case IGMP_G_QUERY_PENDING_MEMBER: | |
807 | case IGMP_SG_QUERY_PENDING_MEMBER: | |
808 | case IGMP_REPORTING_MEMBER: | |
809 | case IGMP_IDLE_MEMBER: | |
810 | case IGMP_LAZY_MEMBER: | |
811 | case IGMP_SLEEPING_MEMBER: | |
812 | case IGMP_AWAKENING_MEMBER: | |
813 | inm->inm_state = IGMP_REPORTING_MEMBER; | |
814 | inm->inm_timer = IGMP_RANDOM_DELAY( | |
815 | IGMP_V1V2_MAX_RI * PR_SLOWHZ); | |
816 | current_state_timers_running = 1; | |
817 | break; | |
818 | case IGMP_LEAVING_MEMBER: | |
819 | break; | |
820 | } | |
821 | next: | |
822 | INM_UNLOCK(inm); | |
823 | IN_NEXT_MULTI(step, inm); | |
824 | } | |
825 | in_multihead_lock_done(); | |
826 | ||
827 | return (0); | |
828 | } | |
829 | ||
830 | /* | |
831 | * Process a received IGMPv2 general or group-specific query. | |
832 | */ | |
833 | static int | |
834 | igmp_input_v2_query(struct ifnet *ifp, const struct ip *ip, | |
835 | const struct igmp *igmp) | |
836 | { | |
837 | struct igmp_ifinfo *igi; | |
838 | struct in_multi *inm; | |
839 | int is_general_query; | |
840 | uint16_t timer; | |
841 | ||
842 | is_general_query = 0; | |
843 | ||
844 | /* | |
845 | * Validate address fields upfront. | |
846 | */ | |
847 | if (in_nullhost(igmp->igmp_group)) { | |
848 | /* | |
849 | * IGMPv2 General Query. | |
850 | * If this was not sent to the all-hosts group, ignore it. | |
851 | */ | |
852 | if (!in_allhosts(ip->ip_dst)) | |
853 | return (0); | |
854 | IGMPSTAT_INC(igps_rcv_gen_queries); | |
855 | is_general_query = 1; | |
856 | } else { | |
857 | /* IGMPv2 Group-Specific Query. */ | |
858 | IGMPSTAT_INC(igps_rcv_group_queries); | |
859 | } | |
860 | ||
861 | igi = IGMP_IFINFO(ifp); | |
862 | VERIFY(igi != NULL); | |
863 | ||
864 | IGI_LOCK(igi); | |
865 | if (igi->igi_flags & IGIF_LOOPBACK) { | |
866 | IGMP_PRINTF(("ignore v2 query on IGIF_LOOPBACK ifp %p(%s%d)\n", | |
867 | ifp, ifp->if_name, ifp->if_unit)); | |
868 | IGI_UNLOCK(igi); | |
869 | return(0); | |
870 | } | |
871 | /* | |
872 | * Ignore v2 query if in v1 Compatibility Mode. | |
873 | */ | |
874 | if (igi->igi_version == IGMP_VERSION_1) { | |
875 | IGI_UNLOCK(igi); | |
876 | return (0); | |
877 | } | |
878 | igmp_set_version(igi, IGMP_VERSION_2); | |
879 | IGI_UNLOCK(igi); | |
880 | ||
881 | timer = igmp->igmp_code * PR_SLOWHZ / IGMP_TIMER_SCALE; | |
882 | if (timer == 0) | |
883 | timer = 1; | |
884 | ||
885 | if (is_general_query) { | |
886 | struct in_multistep step; | |
887 | ||
888 | IGMP_PRINTF(("process v2 general query on ifp %p(%s%d)\n", | |
889 | ifp, ifp->if_name, ifp->if_unit)); | |
890 | /* | |
891 | * For each reporting group joined on this | |
892 | * interface, kick the report timer. | |
893 | */ | |
894 | in_multihead_lock_shared(); | |
895 | IN_FIRST_MULTI(step, inm); | |
896 | while (inm != NULL) { | |
897 | INM_LOCK(inm); | |
898 | if (inm->inm_ifp == ifp) | |
899 | igmp_v2_update_group(inm, timer); | |
900 | INM_UNLOCK(inm); | |
901 | IN_NEXT_MULTI(step, inm); | |
902 | } | |
903 | in_multihead_lock_done(); | |
904 | } else { | |
905 | /* | |
906 | * Group-specific IGMPv2 query, we need only | |
907 | * look up the single group to process it. | |
908 | */ | |
909 | in_multihead_lock_shared(); | |
910 | IN_LOOKUP_MULTI(&igmp->igmp_group, ifp, inm); | |
911 | in_multihead_lock_done(); | |
912 | if (inm != NULL) { | |
913 | INM_LOCK(inm); | |
914 | IGMP_PRINTF(("process v2 query %s on ifp %p(%s%d)\n", | |
915 | inet_ntoa(igmp->igmp_group), ifp, ifp->if_name, | |
916 | ifp->if_unit)); | |
917 | igmp_v2_update_group(inm, timer); | |
918 | INM_UNLOCK(inm); | |
919 | INM_REMREF(inm); /* from IN_LOOKUP_MULTI */ | |
920 | } | |
921 | } | |
922 | ||
923 | return (0); | |
924 | } | |
925 | ||
926 | /* | |
927 | * Update the report timer on a group in response to an IGMPv2 query. | |
928 | * | |
929 | * If we are becoming the reporting member for this group, start the timer. | |
930 | * If we already are the reporting member for this group, and timer is | |
931 | * below the threshold, reset it. | |
932 | * | |
933 | * We may be updating the group for the first time since we switched | |
934 | * to IGMPv3. If we are, then we must clear any recorded source lists, | |
935 | * and transition to REPORTING state; the group timer is overloaded | |
936 | * for group and group-source query responses. | |
937 | * | |
938 | * Unlike IGMPv3, the delay per group should be jittered | |
939 | * to avoid bursts of IGMPv2 reports. | |
940 | */ | |
941 | static void | |
942 | igmp_v2_update_group(struct in_multi *inm, const int timer) | |
943 | { | |
944 | ||
945 | IGMP_PRINTF(("%s: %s/%s%d timer=%d\n", __func__, | |
946 | inet_ntoa(inm->inm_addr), inm->inm_ifp->if_name, | |
947 | inm->inm_ifp->if_unit, timer)); | |
948 | ||
949 | INM_LOCK_ASSERT_HELD(inm); | |
950 | ||
951 | switch (inm->inm_state) { | |
952 | case IGMP_NOT_MEMBER: | |
953 | case IGMP_SILENT_MEMBER: | |
954 | break; | |
955 | case IGMP_REPORTING_MEMBER: | |
956 | if (inm->inm_timer != 0 && | |
957 | inm->inm_timer <= timer) { | |
958 | IGMP_PRINTF(("%s: REPORTING and timer running, " | |
959 | "skipping.\n", __func__)); | |
960 | break; | |
961 | } | |
962 | /* FALLTHROUGH */ | |
963 | case IGMP_SG_QUERY_PENDING_MEMBER: | |
964 | case IGMP_G_QUERY_PENDING_MEMBER: | |
965 | case IGMP_IDLE_MEMBER: | |
966 | case IGMP_LAZY_MEMBER: | |
967 | case IGMP_AWAKENING_MEMBER: | |
968 | IGMP_PRINTF(("%s: ->REPORTING\n", __func__)); | |
969 | inm->inm_state = IGMP_REPORTING_MEMBER; | |
970 | inm->inm_timer = IGMP_RANDOM_DELAY(timer); | |
971 | current_state_timers_running = 1; | |
972 | break; | |
973 | case IGMP_SLEEPING_MEMBER: | |
974 | IGMP_PRINTF(("%s: ->AWAKENING\n", __func__)); | |
975 | inm->inm_state = IGMP_AWAKENING_MEMBER; | |
976 | break; | |
977 | case IGMP_LEAVING_MEMBER: | |
978 | break; | |
979 | } | |
980 | } | |
981 | ||
982 | /* | |
983 | * Process a received IGMPv3 general, group-specific or | |
984 | * group-and-source-specific query. | |
985 | * Assumes m has already been pulled up to the full IGMP message length. | |
986 | * Return 0 if successful, otherwise an appropriate error code is returned. | |
987 | */ | |
988 | static int | |
989 | igmp_input_v3_query(struct ifnet *ifp, const struct ip *ip, | |
990 | /*const*/ struct igmpv3 *igmpv3) | |
991 | { | |
992 | struct igmp_ifinfo *igi; | |
993 | struct in_multi *inm; | |
994 | int is_general_query; | |
995 | uint32_t maxresp, nsrc, qqi; | |
996 | uint16_t timer; | |
997 | uint8_t qrv; | |
998 | ||
999 | is_general_query = 0; | |
1000 | ||
1001 | IGMP_PRINTF(("process v3 query on ifp %p(%s%d)\n", ifp, ifp->if_name, | |
1002 | ifp->if_unit)); | |
1003 | ||
1004 | maxresp = igmpv3->igmp_code; /* in 1/10ths of a second */ | |
1005 | if (maxresp >= 128) { | |
1006 | maxresp = IGMP_MANT(igmpv3->igmp_code) << | |
1007 | (IGMP_EXP(igmpv3->igmp_code) + 3); | |
1008 | } | |
1009 | ||
1010 | /* | |
1011 | * Robustness must never be less than 2 for on-wire IGMPv3. | |
1012 | * FUTURE: Check if ifp has IGIF_LOOPBACK set, as we will make | |
1013 | * an exception for interfaces whose IGMPv3 state changes | |
1014 | * are redirected to loopback (e.g. MANET). | |
1015 | */ | |
1016 | qrv = IGMP_QRV(igmpv3->igmp_misc); | |
1017 | if (qrv < 2) { | |
1018 | IGMP_PRINTF(("%s: clamping qrv %d to %d\n", __func__, | |
1019 | qrv, IGMP_RV_INIT)); | |
1020 | qrv = IGMP_RV_INIT; | |
1021 | } | |
1022 | ||
1023 | qqi = igmpv3->igmp_qqi; | |
1024 | if (qqi >= 128) { | |
1025 | qqi = IGMP_MANT(igmpv3->igmp_qqi) << | |
1026 | (IGMP_EXP(igmpv3->igmp_qqi) + 3); | |
1027 | } | |
1028 | ||
1029 | timer = maxresp * PR_SLOWHZ / IGMP_TIMER_SCALE; | |
1030 | if (timer == 0) | |
1031 | timer = 1; | |
1032 | ||
1033 | nsrc = ntohs(igmpv3->igmp_numsrc); | |
1034 | ||
1035 | /* | |
1036 | * Validate address fields and versions upfront before | |
1037 | * accepting v3 query. | |
1038 | */ | |
1039 | if (in_nullhost(igmpv3->igmp_group)) { | |
1040 | /* | |
1041 | * IGMPv3 General Query. | |
1042 | * | |
1043 | * General Queries SHOULD be directed to 224.0.0.1. | |
1044 | * A general query with a source list has undefined | |
1045 | * behaviour; discard it. | |
1046 | */ | |
1047 | IGMPSTAT_INC(igps_rcv_gen_queries); | |
1048 | if (!in_allhosts(ip->ip_dst) || nsrc > 0) { | |
1049 | IGMPSTAT_INC(igps_rcv_badqueries); | |
1050 | OIGMPSTAT_INC(igps_rcv_badqueries); | |
1051 | return (0); | |
1052 | } | |
1053 | is_general_query = 1; | |
1054 | } else { | |
1055 | /* Group or group-source specific query. */ | |
1056 | if (nsrc == 0) | |
1057 | IGMPSTAT_INC(igps_rcv_group_queries); | |
1058 | else | |
1059 | IGMPSTAT_INC(igps_rcv_gsr_queries); | |
1060 | } | |
1061 | ||
1062 | igi = IGMP_IFINFO(ifp); | |
1063 | VERIFY(igi != NULL); | |
1064 | ||
1065 | IGI_LOCK(igi); | |
1066 | if (igi->igi_flags & IGIF_LOOPBACK) { | |
1067 | IGMP_PRINTF(("ignore v3 query on IGIF_LOOPBACK ifp %p(%s%d)\n", | |
1068 | ifp, ifp->if_name, ifp->if_unit)); | |
1069 | IGI_UNLOCK(igi); | |
1070 | return (0); | |
1071 | } | |
1072 | ||
1073 | /* | |
1074 | * Discard the v3 query if we're in Compatibility Mode. | |
1075 | * The RFC is not obviously worded that hosts need to stay in | |
1076 | * compatibility mode until the Old Version Querier Present | |
1077 | * timer expires. | |
1078 | */ | |
1079 | if (igi->igi_version != IGMP_VERSION_3) { | |
1080 | IGMP_PRINTF(("ignore v3 query in v%d mode on ifp %p(%s%d)\n", | |
1081 | igi->igi_version, ifp, ifp->if_name, ifp->if_unit)); | |
1082 | IGI_UNLOCK(igi); | |
1083 | return (0); | |
1084 | } | |
1085 | ||
1086 | igmp_set_version(igi, IGMP_VERSION_3); | |
1087 | igi->igi_rv = qrv; | |
1088 | igi->igi_qi = qqi; | |
1089 | igi->igi_qri = maxresp; | |
1090 | ||
1091 | ||
1092 | IGMP_PRINTF(("%s: qrv %d qi %d qri %d\n", __func__, qrv, qqi, | |
1093 | maxresp)); | |
1094 | ||
1095 | if (is_general_query) { | |
1096 | /* | |
1097 | * Schedule a current-state report on this ifp for | |
1098 | * all groups, possibly containing source lists. | |
1099 | * If there is a pending General Query response | |
1100 | * scheduled earlier than the selected delay, do | |
1101 | * not schedule any other reports. | |
1102 | * Otherwise, reset the interface timer. | |
1103 | */ | |
1104 | IGMP_PRINTF(("process v3 general query on ifp %p(%s%d)\n", | |
1105 | ifp, ifp->if_name, ifp->if_unit)); | |
1106 | if (igi->igi_v3_timer == 0 || igi->igi_v3_timer >= timer) { | |
1107 | igi->igi_v3_timer = IGMP_RANDOM_DELAY(timer); | |
1108 | interface_timers_running = 1; | |
1109 | } | |
1110 | IGI_UNLOCK(igi); | |
1111 | } else { | |
1112 | IGI_UNLOCK(igi); | |
1113 | /* | |
1114 | * Group-source-specific queries are throttled on | |
1115 | * a per-group basis to defeat denial-of-service attempts. | |
1116 | * Queries for groups we are not a member of on this | |
1117 | * link are simply ignored. | |
1118 | */ | |
1119 | in_multihead_lock_shared(); | |
1120 | IN_LOOKUP_MULTI(&igmpv3->igmp_group, ifp, inm); | |
1121 | in_multihead_lock_done(); | |
1122 | if (inm == NULL) | |
1123 | return (0); | |
1124 | ||
1125 | INM_LOCK(inm); | |
1126 | #ifndef __APPLE__ | |
1127 | /* TODO: need ratecheck equivalent */ | |
1128 | if (nsrc > 0) { | |
1129 | if (!ratecheck(&inm->inm_lastgsrtv, | |
1130 | &igmp_gsrdelay)) { | |
1131 | IGMP_PRINTF(("%s: GS query throttled.\n", | |
1132 | __func__)); | |
1133 | IGMPSTAT_INC(igps_drop_gsr_queries); | |
1134 | INM_UNLOCK(inm); | |
1135 | INM_REMREF(inm); /* from IN_LOOKUP_MULTI */ | |
1136 | return (0); | |
1137 | } | |
1138 | } | |
1139 | #endif | |
1140 | IGMP_PRINTF(("process v3 %s query on ifp %p(%s%d)\n", | |
1141 | inet_ntoa(igmpv3->igmp_group), ifp, ifp->if_name, | |
1142 | ifp->if_unit)); | |
1143 | /* | |
1144 | * If there is a pending General Query response | |
1145 | * scheduled sooner than the selected delay, no | |
1146 | * further report need be scheduled. | |
1147 | * Otherwise, prepare to respond to the | |
1148 | * group-specific or group-and-source query. | |
1149 | */ | |
1150 | IGI_LOCK(igi); | |
1151 | if (igi->igi_v3_timer == 0 || igi->igi_v3_timer >= timer) { | |
1152 | IGI_UNLOCK(igi); | |
1153 | igmp_input_v3_group_query(inm, timer, igmpv3); | |
1154 | } else { | |
1155 | IGI_UNLOCK(igi); | |
1156 | } | |
1157 | INM_UNLOCK(inm); | |
1158 | INM_REMREF(inm); /* from IN_LOOKUP_MULTI */ | |
1159 | } | |
1160 | ||
1161 | return (0); | |
1162 | } | |
1163 | ||
1164 | /* | |
1165 | * Process a recieved IGMPv3 group-specific or group-and-source-specific | |
1166 | * query. | |
1167 | * Return <0 if any error occured. Currently this is ignored. | |
1168 | */ | |
1169 | static int | |
1170 | igmp_input_v3_group_query(struct in_multi *inm, | |
1171 | int timer, /*const*/ struct igmpv3 *igmpv3) | |
1172 | { | |
1173 | int retval; | |
1174 | uint16_t nsrc; | |
1175 | ||
1176 | INM_LOCK_ASSERT_HELD(inm); | |
1177 | ||
1178 | retval = 0; | |
1179 | ||
1180 | switch (inm->inm_state) { | |
1181 | case IGMP_NOT_MEMBER: | |
1182 | case IGMP_SILENT_MEMBER: | |
1183 | case IGMP_SLEEPING_MEMBER: | |
1184 | case IGMP_LAZY_MEMBER: | |
1185 | case IGMP_AWAKENING_MEMBER: | |
1186 | case IGMP_IDLE_MEMBER: | |
1187 | case IGMP_LEAVING_MEMBER: | |
1188 | return (retval); | |
1189 | case IGMP_REPORTING_MEMBER: | |
1190 | case IGMP_G_QUERY_PENDING_MEMBER: | |
1191 | case IGMP_SG_QUERY_PENDING_MEMBER: | |
1192 | break; | |
1193 | } | |
1194 | ||
1195 | nsrc = ntohs(igmpv3->igmp_numsrc); | |
1196 | ||
1197 | /* | |
1198 | * Deal with group-specific queries upfront. | |
1199 | * If any group query is already pending, purge any recorded | |
1200 | * source-list state if it exists, and schedule a query response | |
1201 | * for this group-specific query. | |
1202 | */ | |
1203 | if (nsrc == 0) { | |
1204 | if (inm->inm_state == IGMP_G_QUERY_PENDING_MEMBER || | |
1205 | inm->inm_state == IGMP_SG_QUERY_PENDING_MEMBER) { | |
1206 | inm_clear_recorded(inm); | |
1207 | timer = min(inm->inm_timer, timer); | |
1208 | } | |
1209 | inm->inm_state = IGMP_G_QUERY_PENDING_MEMBER; | |
1210 | inm->inm_timer = IGMP_RANDOM_DELAY(timer); | |
1211 | current_state_timers_running = 1; | |
1212 | return (retval); | |
1213 | } | |
1214 | ||
1215 | /* | |
1216 | * Deal with the case where a group-and-source-specific query has | |
1217 | * been received but a group-specific query is already pending. | |
1218 | */ | |
1219 | if (inm->inm_state == IGMP_G_QUERY_PENDING_MEMBER) { | |
1220 | timer = min(inm->inm_timer, timer); | |
1221 | inm->inm_timer = IGMP_RANDOM_DELAY(timer); | |
1222 | current_state_timers_running = 1; | |
1223 | return (retval); | |
1224 | } | |
1225 | ||
1226 | /* | |
1227 | * Finally, deal with the case where a group-and-source-specific | |
1228 | * query has been received, where a response to a previous g-s-r | |
1229 | * query exists, or none exists. | |
1230 | * In this case, we need to parse the source-list which the Querier | |
1231 | * has provided us with and check if we have any source list filter | |
1232 | * entries at T1 for these sources. If we do not, there is no need | |
1233 | * schedule a report and the query may be dropped. | |
1234 | * If we do, we must record them and schedule a current-state | |
1235 | * report for those sources. | |
1236 | * FIXME: Handling source lists larger than 1 mbuf requires that | |
1237 | * we pass the mbuf chain pointer down to this function, and use | |
1238 | * m_getptr() to walk the chain. | |
1239 | */ | |
1240 | if (inm->inm_nsrc > 0) { | |
1241 | const struct in_addr *ap; | |
1242 | int i, nrecorded; | |
1243 | ||
1244 | ap = (const struct in_addr *)(igmpv3 + 1); | |
1245 | nrecorded = 0; | |
1246 | for (i = 0; i < nsrc; i++, ap++) { | |
1247 | retval = inm_record_source(inm, ap->s_addr); | |
1248 | if (retval < 0) | |
1249 | break; | |
1250 | nrecorded += retval; | |
1251 | } | |
1252 | if (nrecorded > 0) { | |
1253 | IGMP_PRINTF(("%s: schedule response to SG query\n", | |
1254 | __func__)); | |
1255 | inm->inm_state = IGMP_SG_QUERY_PENDING_MEMBER; | |
1256 | inm->inm_timer = IGMP_RANDOM_DELAY(timer); | |
1257 | current_state_timers_running = 1; | |
1258 | } | |
1259 | } | |
1260 | ||
1261 | return (retval); | |
1262 | } | |
1263 | ||
1264 | /* | |
1265 | * Process a received IGMPv1 host membership report. | |
1266 | * | |
1267 | * NOTE: 0.0.0.0 workaround breaks const correctness. | |
1268 | */ | |
1269 | static int | |
1270 | igmp_input_v1_report(struct ifnet *ifp, /*const*/ struct ip *ip, | |
1271 | /*const*/ struct igmp *igmp) | |
1272 | { | |
1273 | struct in_ifaddr *ia; | |
1274 | struct in_multi *inm; | |
1275 | ||
1276 | IGMPSTAT_INC(igps_rcv_reports); | |
1277 | OIGMPSTAT_INC(igps_rcv_reports); | |
1278 | ||
1279 | if (ifp->if_flags & IFF_LOOPBACK) | |
1280 | return (0); | |
1281 | ||
1282 | if (!IN_MULTICAST(ntohl(igmp->igmp_group.s_addr) || | |
1283 | !in_hosteq(igmp->igmp_group, ip->ip_dst))) { | |
1284 | IGMPSTAT_INC(igps_rcv_badreports); | |
1285 | OIGMPSTAT_INC(igps_rcv_badreports); | |
1286 | return (EINVAL); | |
1287 | } | |
1288 | ||
1289 | /* | |
1290 | * RFC 3376, Section 4.2.13, 9.2, 9.3: | |
1291 | * Booting clients may use the source address 0.0.0.0. Some | |
1292 | * IGMP daemons may not know how to use IP_RECVIF to determine | |
1293 | * the interface upon which this message was received. | |
1294 | * Replace 0.0.0.0 with the subnet address if told to do so. | |
1295 | */ | |
1296 | if (igmp_recvifkludge && in_nullhost(ip->ip_src)) { | |
1297 | IFP_TO_IA(ifp, ia); | |
1298 | if (ia != NULL) { | |
1299 | IFA_LOCK(&ia->ia_ifa); | |
1300 | ip->ip_src.s_addr = htonl(ia->ia_subnet); | |
1301 | IFA_UNLOCK(&ia->ia_ifa); | |
1302 | IFA_REMREF(&ia->ia_ifa); | |
1303 | } | |
1304 | } | |
1305 | ||
1306 | IGMP_PRINTF(("process v1 report %s on ifp %p(%s%d)\n", | |
1307 | inet_ntoa(igmp->igmp_group), ifp, ifp->if_name, ifp->if_unit)); | |
1308 | ||
1309 | /* | |
1310 | * IGMPv1 report suppression. | |
1311 | * If we are a member of this group, and our membership should be | |
1312 | * reported, stop our group timer and transition to the 'lazy' state. | |
1313 | */ | |
1314 | in_multihead_lock_shared(); | |
1315 | IN_LOOKUP_MULTI(&igmp->igmp_group, ifp, inm); | |
1316 | in_multihead_lock_done(); | |
1317 | if (inm != NULL) { | |
1318 | struct igmp_ifinfo *igi; | |
1319 | ||
1320 | INM_LOCK(inm); | |
1321 | ||
1322 | igi = inm->inm_igi; | |
1323 | VERIFY(igi != NULL); | |
1324 | ||
1325 | IGMPSTAT_INC(igps_rcv_ourreports); | |
1326 | OIGMPSTAT_INC(igps_rcv_ourreports); | |
1327 | ||
1328 | /* | |
1329 | * If we are in IGMPv3 host mode, do not allow the | |
1330 | * other host's IGMPv1 report to suppress our reports | |
1331 | * unless explicitly configured to do so. | |
1332 | */ | |
1333 | IGI_LOCK(igi); | |
1334 | if (igi->igi_version == IGMP_VERSION_3) { | |
1335 | if (igmp_legacysupp) | |
1336 | igmp_v3_suppress_group_record(inm); | |
1337 | IGI_UNLOCK(igi); | |
1338 | INM_UNLOCK(inm); | |
1339 | INM_REMREF(inm); /* from IN_LOOKUP_MULTI */ | |
1340 | return (0); | |
1341 | } | |
1342 | ||
1343 | INM_LOCK_ASSERT_HELD(inm); | |
1344 | inm->inm_timer = 0; | |
1345 | ||
1346 | switch (inm->inm_state) { | |
1347 | case IGMP_NOT_MEMBER: | |
1348 | case IGMP_SILENT_MEMBER: | |
1349 | break; | |
1350 | case IGMP_IDLE_MEMBER: | |
1351 | case IGMP_LAZY_MEMBER: | |
1352 | case IGMP_AWAKENING_MEMBER: | |
1353 | IGMP_PRINTF(("report suppressed for %s on ifp %p(%s%d)\n", | |
1354 | inet_ntoa(igmp->igmp_group), ifp, ifp->if_name, | |
1355 | ifp->if_unit)); | |
1356 | case IGMP_SLEEPING_MEMBER: | |
1357 | inm->inm_state = IGMP_SLEEPING_MEMBER; | |
1358 | break; | |
1359 | case IGMP_REPORTING_MEMBER: | |
1360 | IGMP_PRINTF(("report suppressed for %s on ifp %p(%s%d)\n", | |
1361 | inet_ntoa(igmp->igmp_group), ifp, ifp->if_name, | |
1362 | ifp->if_unit)); | |
1363 | if (igi->igi_version == IGMP_VERSION_1) | |
1364 | inm->inm_state = IGMP_LAZY_MEMBER; | |
1365 | else if (igi->igi_version == IGMP_VERSION_2) | |
1366 | inm->inm_state = IGMP_SLEEPING_MEMBER; | |
1367 | break; | |
1368 | case IGMP_G_QUERY_PENDING_MEMBER: | |
1369 | case IGMP_SG_QUERY_PENDING_MEMBER: | |
1370 | case IGMP_LEAVING_MEMBER: | |
1371 | break; | |
1372 | } | |
1373 | IGI_UNLOCK(igi); | |
1374 | INM_UNLOCK(inm); | |
1375 | INM_REMREF(inm); /* from IN_LOOKUP_MULTI */ | |
1376 | } | |
1377 | ||
1378 | return (0); | |
1379 | } | |
1380 | ||
1381 | /* | |
1382 | * Process a received IGMPv2 host membership report. | |
1383 | * | |
1384 | * NOTE: 0.0.0.0 workaround breaks const correctness. | |
1385 | */ | |
1386 | static int | |
1387 | igmp_input_v2_report(struct ifnet *ifp, /*const*/ struct ip *ip, | |
1388 | /*const*/ struct igmp *igmp) | |
1389 | { | |
1390 | struct in_ifaddr *ia; | |
1391 | struct in_multi *inm; | |
1392 | ||
1393 | /* | |
1394 | * Make sure we don't hear our own membership report. Fast | |
1395 | * leave requires knowing that we are the only member of a | |
1396 | * group. | |
1397 | */ | |
1398 | IFP_TO_IA(ifp, ia); | |
1399 | if (ia != NULL) { | |
1400 | IFA_LOCK(&ia->ia_ifa); | |
1401 | if (in_hosteq(ip->ip_src, IA_SIN(ia)->sin_addr)) { | |
1402 | IFA_UNLOCK(&ia->ia_ifa); | |
1403 | IFA_REMREF(&ia->ia_ifa); | |
1404 | return (0); | |
1405 | } | |
1406 | IFA_UNLOCK(&ia->ia_ifa); | |
1407 | } | |
1408 | ||
1409 | IGMPSTAT_INC(igps_rcv_reports); | |
1410 | OIGMPSTAT_INC(igps_rcv_reports); | |
1411 | ||
1412 | if (ifp->if_flags & IFF_LOOPBACK) { | |
1413 | if (ia != NULL) | |
1414 | IFA_REMREF(&ia->ia_ifa); | |
1415 | return (0); | |
1416 | } | |
1417 | ||
1418 | if (!IN_MULTICAST(ntohl(igmp->igmp_group.s_addr)) || | |
1419 | !in_hosteq(igmp->igmp_group, ip->ip_dst)) { | |
1420 | if (ia != NULL) | |
1421 | IFA_REMREF(&ia->ia_ifa); | |
1422 | IGMPSTAT_INC(igps_rcv_badreports); | |
1423 | OIGMPSTAT_INC(igps_rcv_badreports); | |
1424 | return (EINVAL); | |
1425 | } | |
1426 | ||
1427 | /* | |
1428 | * RFC 3376, Section 4.2.13, 9.2, 9.3: | |
1429 | * Booting clients may use the source address 0.0.0.0. Some | |
1430 | * IGMP daemons may not know how to use IP_RECVIF to determine | |
1431 | * the interface upon which this message was received. | |
1432 | * Replace 0.0.0.0 with the subnet address if told to do so. | |
1433 | */ | |
1434 | if (igmp_recvifkludge && in_nullhost(ip->ip_src)) { | |
1435 | if (ia != NULL) { | |
1436 | IFA_LOCK(&ia->ia_ifa); | |
1437 | ip->ip_src.s_addr = htonl(ia->ia_subnet); | |
1438 | IFA_UNLOCK(&ia->ia_ifa); | |
1439 | } | |
1440 | } | |
1441 | if (ia != NULL) | |
1442 | IFA_REMREF(&ia->ia_ifa); | |
1443 | ||
1444 | IGMP_PRINTF(("process v2 report %s on ifp %p(%s%d)\n", | |
1445 | inet_ntoa(igmp->igmp_group), ifp, ifp->if_name, ifp->if_unit)); | |
1446 | ||
1447 | /* | |
1448 | * IGMPv2 report suppression. | |
1449 | * If we are a member of this group, and our membership should be | |
1450 | * reported, and our group timer is pending or about to be reset, | |
1451 | * stop our group timer by transitioning to the 'lazy' state. | |
1452 | */ | |
1453 | in_multihead_lock_shared(); | |
1454 | IN_LOOKUP_MULTI(&igmp->igmp_group, ifp, inm); | |
1455 | in_multihead_lock_done(); | |
1456 | if (inm != NULL) { | |
1457 | struct igmp_ifinfo *igi; | |
1458 | ||
1459 | INM_LOCK(inm); | |
1460 | igi = inm->inm_igi; | |
1461 | VERIFY(igi != NULL); | |
1462 | ||
1463 | IGMPSTAT_INC(igps_rcv_ourreports); | |
1464 | OIGMPSTAT_INC(igps_rcv_ourreports); | |
1465 | ||
1466 | /* | |
1467 | * If we are in IGMPv3 host mode, do not allow the | |
1468 | * other host's IGMPv1 report to suppress our reports | |
1469 | * unless explicitly configured to do so. | |
1470 | */ | |
1471 | IGI_LOCK(igi); | |
1472 | if (igi->igi_version == IGMP_VERSION_3) { | |
1473 | if (igmp_legacysupp) | |
1474 | igmp_v3_suppress_group_record(inm); | |
1475 | IGI_UNLOCK(igi); | |
1476 | INM_UNLOCK(inm); | |
1477 | INM_REMREF(inm); | |
1478 | return (0); | |
1479 | } | |
1480 | ||
1481 | inm->inm_timer = 0; | |
1482 | ||
1483 | switch (inm->inm_state) { | |
1484 | case IGMP_NOT_MEMBER: | |
1485 | case IGMP_SILENT_MEMBER: | |
1486 | case IGMP_SLEEPING_MEMBER: | |
1487 | break; | |
1488 | case IGMP_REPORTING_MEMBER: | |
1489 | case IGMP_IDLE_MEMBER: | |
1490 | case IGMP_AWAKENING_MEMBER: | |
1491 | IGMP_PRINTF(("report suppressed for %s on ifp %p(%s%d)\n", | |
1492 | inet_ntoa(igmp->igmp_group), ifp, ifp->if_name, | |
1493 | ifp->if_unit)); | |
1494 | case IGMP_LAZY_MEMBER: | |
1495 | inm->inm_state = IGMP_LAZY_MEMBER; | |
1496 | break; | |
1497 | case IGMP_G_QUERY_PENDING_MEMBER: | |
1498 | case IGMP_SG_QUERY_PENDING_MEMBER: | |
1499 | case IGMP_LEAVING_MEMBER: | |
1500 | break; | |
1501 | } | |
1502 | IGI_UNLOCK(igi); | |
1503 | INM_UNLOCK(inm); | |
1504 | INM_REMREF(inm); | |
1505 | } | |
1506 | ||
1507 | return (0); | |
1508 | } | |
1509 | ||
1510 | void | |
1511 | igmp_input(struct mbuf *m, int off) | |
1512 | { | |
1513 | int iphlen; | |
1514 | struct ifnet *ifp; | |
1515 | struct igmp *igmp; | |
1516 | struct ip *ip; | |
1517 | int igmplen; | |
1518 | int minlen; | |
1519 | int queryver; | |
1520 | ||
1521 | IGMP_PRINTF(("%s: called w/mbuf (%p,%d)\n", __func__, m, off)); | |
1522 | ||
1523 | ifp = m->m_pkthdr.rcvif; | |
1524 | ||
1525 | IGMPSTAT_INC(igps_rcv_total); | |
1526 | OIGMPSTAT_INC(igps_rcv_total); | |
1527 | ||
1528 | ip = mtod(m, struct ip *); | |
1529 | iphlen = off; | |
1530 | ||
1531 | /* By now, ip_len no longer contains the length of IP header */ | |
1532 | igmplen = ip->ip_len; | |
1533 | ||
1534 | /* | |
1535 | * Validate lengths. | |
1536 | */ | |
1537 | if (igmplen < IGMP_MINLEN) { | |
1538 | IGMPSTAT_INC(igps_rcv_tooshort); | |
1539 | OIGMPSTAT_INC(igps_rcv_tooshort); | |
1540 | m_freem(m); | |
1541 | return; | |
1542 | } | |
1543 | ||
1544 | /* | |
1545 | * Always pullup to the minimum size for v1/v2 or v3 | |
1546 | * to amortize calls to m_pulldown(). | |
1547 | */ | |
1548 | if (igmplen >= IGMP_V3_QUERY_MINLEN) | |
1549 | minlen = IGMP_V3_QUERY_MINLEN; | |
1550 | else | |
1551 | minlen = IGMP_MINLEN; | |
1552 | ||
1553 | M_STRUCT_GET(igmp, struct igmp *, m, off, minlen); | |
1554 | if (igmp == NULL) { | |
1555 | IGMPSTAT_INC(igps_rcv_tooshort); | |
1556 | OIGMPSTAT_INC(igps_rcv_tooshort); | |
1557 | return; | |
1558 | } | |
1559 | ||
1560 | /* | |
1561 | * Validate checksum. | |
1562 | */ | |
1563 | m->m_data += iphlen; | |
1564 | m->m_len -= iphlen; | |
1565 | if (in_cksum(m, igmplen)) { | |
1566 | IGMPSTAT_INC(igps_rcv_badsum); | |
1567 | OIGMPSTAT_INC(igps_rcv_badsum); | |
1568 | m_freem(m); | |
1569 | return; | |
1570 | } | |
1571 | m->m_data -= iphlen; | |
1572 | m->m_len += iphlen; | |
1573 | ||
1574 | /* | |
1575 | * IGMP control traffic is link-scope, and must have a TTL of 1. | |
1576 | * DVMRP traffic (e.g. mrinfo, mtrace) is an exception; | |
1577 | * probe packets may come from beyond the LAN. | |
1578 | */ | |
1579 | if (igmp->igmp_type != IGMP_DVMRP && ip->ip_ttl != 1) { | |
1580 | IGMPSTAT_INC(igps_rcv_badttl); | |
1581 | m_freem(m); | |
1582 | return; | |
1583 | } | |
1584 | ||
1585 | switch (igmp->igmp_type) { | |
1586 | case IGMP_HOST_MEMBERSHIP_QUERY: | |
1587 | if (igmplen == IGMP_MINLEN) { | |
1588 | if (igmp->igmp_code == 0) | |
1589 | queryver = IGMP_VERSION_1; | |
1590 | else | |
1591 | queryver = IGMP_VERSION_2; | |
1592 | } else if (igmplen >= IGMP_V3_QUERY_MINLEN) { | |
1593 | queryver = IGMP_VERSION_3; | |
1594 | } else { | |
1595 | IGMPSTAT_INC(igps_rcv_tooshort); | |
1596 | OIGMPSTAT_INC(igps_rcv_tooshort); | |
1597 | m_freem(m); | |
1598 | return; | |
1599 | } | |
1600 | ||
1601 | OIGMPSTAT_INC(igps_rcv_queries); | |
1602 | ||
1603 | switch (queryver) { | |
1604 | case IGMP_VERSION_1: | |
1605 | IGMPSTAT_INC(igps_rcv_v1v2_queries); | |
1606 | if (!igmp_v1enable) | |
1607 | break; | |
1608 | if (igmp_input_v1_query(ifp, ip, igmp) != 0) { | |
1609 | m_freem(m); | |
1610 | return; | |
1611 | } | |
1612 | break; | |
1613 | ||
1614 | case IGMP_VERSION_2: | |
1615 | IGMPSTAT_INC(igps_rcv_v1v2_queries); | |
1616 | if (!igmp_v2enable) | |
1617 | break; | |
1618 | if (igmp_input_v2_query(ifp, ip, igmp) != 0) { | |
1619 | m_freem(m); | |
1620 | return; | |
1621 | } | |
1622 | break; | |
1623 | ||
1624 | case IGMP_VERSION_3: { | |
1625 | struct igmpv3 *igmpv3; | |
1626 | uint16_t igmpv3len; | |
1627 | uint16_t srclen; | |
1628 | int nsrc; | |
1629 | ||
1630 | IGMPSTAT_INC(igps_rcv_v3_queries); | |
1631 | igmpv3 = (struct igmpv3 *)igmp; | |
1632 | /* | |
1633 | * Validate length based on source count. | |
1634 | */ | |
1635 | nsrc = ntohs(igmpv3->igmp_numsrc); | |
1636 | srclen = sizeof(struct in_addr) * nsrc; | |
1637 | if (igmplen < (IGMP_V3_QUERY_MINLEN + srclen)) { | |
1638 | IGMPSTAT_INC(igps_rcv_tooshort); | |
1639 | OIGMPSTAT_INC(igps_rcv_tooshort); | |
1640 | m_freem(m); | |
1641 | return; | |
1642 | } | |
1643 | igmpv3len = IGMP_V3_QUERY_MINLEN + srclen; | |
1644 | M_STRUCT_GET(igmpv3, struct igmpv3 *, m, | |
1645 | off, igmpv3len); | |
1646 | if (igmpv3 == NULL) { | |
1647 | IGMPSTAT_INC(igps_rcv_tooshort); | |
1648 | OIGMPSTAT_INC(igps_rcv_tooshort); | |
1649 | return; | |
1650 | } | |
1651 | if (igmp_input_v3_query(ifp, ip, igmpv3) != 0) { | |
1652 | m_freem(m); | |
1653 | return; | |
1654 | } | |
1655 | } | |
1656 | break; | |
1657 | } | |
1658 | break; | |
1659 | ||
1660 | case IGMP_v1_HOST_MEMBERSHIP_REPORT: | |
1661 | if (!igmp_v1enable) | |
1662 | break; | |
1663 | if (igmp_input_v1_report(ifp, ip, igmp) != 0) { | |
1664 | m_freem(m); | |
1665 | return; | |
1666 | } | |
1667 | break; | |
1668 | ||
1669 | case IGMP_v2_HOST_MEMBERSHIP_REPORT: | |
1670 | if (!igmp_v2enable) | |
1671 | break; | |
1672 | #ifndef __APPLE__ | |
1673 | if (!ip_checkrouteralert(m)) | |
1674 | IGMPSTAT_INC(igps_rcv_nora); | |
1675 | #endif | |
1676 | if (igmp_input_v2_report(ifp, ip, igmp) != 0) { | |
1677 | m_freem(m); | |
1678 | return; | |
1679 | } | |
1680 | break; | |
1681 | ||
1682 | case IGMP_v3_HOST_MEMBERSHIP_REPORT: | |
1683 | /* | |
1684 | * Hosts do not need to process IGMPv3 membership reports, | |
1685 | * as report suppression is no longer required. | |
1686 | */ | |
1687 | #ifndef __APPLE__ | |
1688 | if (!ip_checkrouteralert(m)) | |
1689 | IGMPSTAT_INC(igps_rcv_nora); | |
1690 | #endif | |
1691 | break; | |
1692 | ||
1693 | default: | |
1694 | break; | |
1695 | } | |
1696 | ||
1697 | lck_mtx_assert(&igmp_mtx, LCK_MTX_ASSERT_NOTOWNED); | |
1698 | /* | |
1699 | * Pass all valid IGMP packets up to any process(es) listening on a | |
1700 | * raw IGMP socket. | |
1701 | */ | |
1702 | rip_input(m, off); | |
1703 | } | |
1704 | ||
1705 | ||
1706 | /* | |
1707 | * IGMP slowtimo handler. | |
1708 | * Combiles both the slow and fast timer into one. We loose some responsivness but | |
1709 | * allows the system to avoid having a pr_fasttimo, thus allowing for power savings. | |
1710 | * | |
1711 | */ | |
1712 | void | |
1713 | igmp_slowtimo(void) | |
1714 | { | |
1715 | struct ifqueue scq; /* State-change packets */ | |
1716 | struct ifqueue qrq; /* Query response packets */ | |
1717 | struct ifnet *ifp; | |
1718 | struct igmp_ifinfo *igi; | |
1719 | struct in_multi *inm; | |
1720 | int loop = 0, uri_fasthz = 0; | |
1721 | ||
1722 | lck_mtx_lock(&igmp_mtx); | |
1723 | ||
1724 | LIST_FOREACH(igi, &igi_head, igi_link) { | |
1725 | IGI_LOCK(igi); | |
1726 | igmp_v1v2_process_querier_timers(igi); | |
1727 | IGI_UNLOCK(igi); | |
1728 | } | |
1729 | ||
1730 | /* | |
1731 | * NOTE: previously handled by fasttimo | |
1732 | * | |
1733 | * Quick check to see if any work needs to be done, in order to | |
1734 | * minimize the overhead of fasttimo processing. | |
1735 | */ | |
1736 | if (!current_state_timers_running && | |
1737 | !interface_timers_running && | |
1738 | !state_change_timers_running) { | |
1739 | lck_mtx_unlock(&igmp_mtx); | |
1740 | return; | |
1741 | } | |
1742 | ||
1743 | /* | |
1744 | * IGMPv3 General Query response timer processing. | |
1745 | */ | |
1746 | if (interface_timers_running) { | |
1747 | interface_timers_running = 0; | |
1748 | LIST_FOREACH(igi, &igi_head, igi_link) { | |
1749 | IGI_LOCK(igi); | |
1750 | if (igi->igi_v3_timer == 0) { | |
1751 | /* Do nothing. */ | |
1752 | } else if (--igi->igi_v3_timer == 0) { | |
1753 | igmp_v3_dispatch_general_query(igi); | |
1754 | } else { | |
1755 | interface_timers_running = 1; | |
1756 | } | |
1757 | IGI_UNLOCK(igi); | |
1758 | } | |
1759 | } | |
1760 | ||
1761 | if (!current_state_timers_running && | |
1762 | !state_change_timers_running) | |
1763 | goto out_locked; | |
1764 | ||
1765 | current_state_timers_running = 0; | |
1766 | state_change_timers_running = 0; | |
1767 | ||
1768 | memset(&qrq, 0, sizeof(struct ifqueue)); | |
1769 | qrq.ifq_maxlen = IGMP_MAX_G_GS_PACKETS; | |
1770 | ||
1771 | memset(&scq, 0, sizeof(struct ifqueue)); | |
1772 | scq.ifq_maxlen = IGMP_MAX_STATE_CHANGE_PACKETS; | |
1773 | ||
1774 | /* | |
1775 | * IGMPv1/v2/v3 host report and state-change timer processing. | |
1776 | * Note: Processing a v3 group timer may remove a node. | |
1777 | */ | |
1778 | LIST_FOREACH(igi, &igi_head, igi_link) { | |
1779 | struct in_multistep step; | |
1780 | ||
1781 | IGI_LOCK(igi); | |
1782 | ifp = igi->igi_ifp; | |
1783 | loop = (igi->igi_flags & IGIF_LOOPBACK) ? 1 : 0; | |
1784 | uri_fasthz = IGMP_RANDOM_DELAY(igi->igi_uri * PR_SLOWHZ); | |
1785 | IGI_UNLOCK(igi); | |
1786 | ||
1787 | in_multihead_lock_shared(); | |
1788 | IN_FIRST_MULTI(step, inm); | |
1789 | while (inm != NULL) { | |
1790 | INM_LOCK(inm); | |
1791 | if (inm->inm_ifp != ifp) | |
1792 | goto next; | |
1793 | ||
1794 | IGI_LOCK(igi); | |
1795 | switch (igi->igi_version) { | |
1796 | case IGMP_VERSION_1: | |
1797 | case IGMP_VERSION_2: | |
1798 | igmp_v1v2_process_group_timer(inm, | |
1799 | igi->igi_version); | |
1800 | break; | |
1801 | case IGMP_VERSION_3: | |
1802 | igmp_v3_process_group_timers(igi, &qrq, | |
1803 | &scq, inm, uri_fasthz); | |
1804 | break; | |
1805 | } | |
1806 | IGI_UNLOCK(igi); | |
1807 | next: | |
1808 | INM_UNLOCK(inm); | |
1809 | IN_NEXT_MULTI(step, inm); | |
1810 | } | |
1811 | in_multihead_lock_done(); | |
1812 | ||
1813 | IGI_LOCK(igi); | |
1814 | if (igi->igi_version == IGMP_VERSION_1 || | |
1815 | igi->igi_version == IGMP_VERSION_2) { | |
1816 | igmp_dispatch_queue(igi, &igi->igi_v2q, 0, loop, ifp); | |
1817 | } else if (igi->igi_version == IGMP_VERSION_3) { | |
1818 | IGI_UNLOCK(igi); | |
1819 | igmp_dispatch_queue(NULL, &qrq, 0, loop, ifp); | |
1820 | igmp_dispatch_queue(NULL, &scq, 0, loop, ifp); | |
1821 | VERIFY(qrq.ifq_len == 0); | |
1822 | VERIFY(scq.ifq_len == 0); | |
1823 | IGI_LOCK(igi); | |
1824 | } | |
1825 | /* | |
1826 | * In case there are still any pending membership reports | |
1827 | * which didn't get drained at version change time. | |
1828 | */ | |
1829 | IF_DRAIN(&igi->igi_v2q); | |
1830 | /* | |
1831 | * Release all deferred inm records, and drain any locally | |
1832 | * enqueued packets; do it even if the current IGMP version | |
1833 | * for the link is no longer IGMPv3, in order to handle the | |
1834 | * version change case. | |
1835 | */ | |
1836 | igmp_flush_relq(igi); | |
1837 | VERIFY(SLIST_EMPTY(&igi->igi_relinmhead)); | |
1838 | IGI_UNLOCK(igi); | |
1839 | ||
1840 | IF_DRAIN(&qrq); | |
1841 | IF_DRAIN(&scq); | |
1842 | } | |
1843 | ||
1844 | out_locked: | |
1845 | lck_mtx_unlock(&igmp_mtx); | |
1846 | } | |
1847 | ||
1848 | /* | |
1849 | * Free the in_multi reference(s) for this IGMP lifecycle. | |
1850 | * | |
1851 | * Caller must be holding igi_lock. | |
1852 | */ | |
1853 | static void | |
1854 | igmp_flush_relq(struct igmp_ifinfo *igi) | |
1855 | { | |
1856 | struct in_multi *inm; | |
1857 | ||
1858 | again: | |
1859 | IGI_LOCK_ASSERT_HELD(igi); | |
1860 | inm = SLIST_FIRST(&igi->igi_relinmhead); | |
1861 | if (inm != NULL) { | |
1862 | int lastref; | |
1863 | ||
1864 | SLIST_REMOVE_HEAD(&igi->igi_relinmhead, inm_nrele); | |
1865 | IGI_UNLOCK(igi); | |
1866 | ||
1867 | in_multihead_lock_exclusive(); | |
1868 | INM_LOCK(inm); | |
1869 | VERIFY(inm->inm_nrelecnt != 0); | |
1870 | inm->inm_nrelecnt--; | |
1871 | lastref = in_multi_detach(inm); | |
1872 | VERIFY(!lastref || (!(inm->inm_debug & IFD_ATTACHED) && | |
1873 | inm->inm_reqcnt == 0)); | |
1874 | INM_UNLOCK(inm); | |
1875 | in_multihead_lock_done(); | |
1876 | /* from igi_relinmhead */ | |
1877 | INM_REMREF(inm); | |
1878 | /* from in_multihead list */ | |
1879 | if (lastref) | |
1880 | INM_REMREF(inm); | |
1881 | ||
1882 | IGI_LOCK(igi); | |
1883 | goto again; | |
1884 | } | |
1885 | } | |
1886 | ||
1887 | /* | |
1888 | * Update host report group timer for IGMPv1/v2. | |
1889 | * Will update the global pending timer flags. | |
1890 | */ | |
1891 | static void | |
1892 | igmp_v1v2_process_group_timer(struct in_multi *inm, const int igmp_version) | |
1893 | { | |
1894 | int report_timer_expired; | |
1895 | ||
1896 | INM_LOCK_ASSERT_HELD(inm); | |
1897 | IGI_LOCK_ASSERT_HELD(inm->inm_igi); | |
1898 | ||
1899 | if (inm->inm_timer == 0) { | |
1900 | report_timer_expired = 0; | |
1901 | } else if (--inm->inm_timer == 0) { | |
1902 | report_timer_expired = 1; | |
1903 | } else { | |
1904 | current_state_timers_running = 1; | |
1905 | return; | |
1906 | } | |
1907 | ||
1908 | switch (inm->inm_state) { | |
1909 | case IGMP_NOT_MEMBER: | |
1910 | case IGMP_SILENT_MEMBER: | |
1911 | case IGMP_IDLE_MEMBER: | |
1912 | case IGMP_LAZY_MEMBER: | |
1913 | case IGMP_SLEEPING_MEMBER: | |
1914 | case IGMP_AWAKENING_MEMBER: | |
1915 | break; | |
1916 | case IGMP_REPORTING_MEMBER: | |
1917 | if (report_timer_expired) { | |
1918 | inm->inm_state = IGMP_IDLE_MEMBER; | |
1919 | (void) igmp_v1v2_queue_report(inm, | |
1920 | (igmp_version == IGMP_VERSION_2) ? | |
1921 | IGMP_v2_HOST_MEMBERSHIP_REPORT : | |
1922 | IGMP_v1_HOST_MEMBERSHIP_REPORT); | |
1923 | INM_LOCK_ASSERT_HELD(inm); | |
1924 | IGI_LOCK_ASSERT_HELD(inm->inm_igi); | |
1925 | } | |
1926 | break; | |
1927 | case IGMP_G_QUERY_PENDING_MEMBER: | |
1928 | case IGMP_SG_QUERY_PENDING_MEMBER: | |
1929 | case IGMP_LEAVING_MEMBER: | |
1930 | break; | |
1931 | } | |
1932 | } | |
1933 | ||
1934 | /* | |
1935 | * Update a group's timers for IGMPv3. | |
1936 | * Will update the global pending timer flags. | |
1937 | * Note: Unlocked read from igi. | |
1938 | */ | |
1939 | static void | |
1940 | igmp_v3_process_group_timers(struct igmp_ifinfo *igi, | |
1941 | struct ifqueue *qrq, struct ifqueue *scq, | |
1942 | struct in_multi *inm, const int uri_fasthz) | |
1943 | { | |
1944 | int query_response_timer_expired; | |
1945 | int state_change_retransmit_timer_expired; | |
1946 | ||
1947 | INM_LOCK_ASSERT_HELD(inm); | |
1948 | IGI_LOCK_ASSERT_HELD(igi); | |
1949 | VERIFY(igi == inm->inm_igi); | |
1950 | ||
1951 | query_response_timer_expired = 0; | |
1952 | state_change_retransmit_timer_expired = 0; | |
1953 | ||
1954 | /* | |
1955 | * During a transition from v1/v2 compatibility mode back to v3, | |
1956 | * a group record in REPORTING state may still have its group | |
1957 | * timer active. This is a no-op in this function; it is easier | |
1958 | * to deal with it here than to complicate the slow-timeout path. | |
1959 | */ | |
1960 | if (inm->inm_timer == 0) { | |
1961 | query_response_timer_expired = 0; | |
1962 | } else if (--inm->inm_timer == 0) { | |
1963 | query_response_timer_expired = 1; | |
1964 | } else { | |
1965 | current_state_timers_running = 1; | |
1966 | } | |
1967 | ||
1968 | if (inm->inm_sctimer == 0) { | |
1969 | state_change_retransmit_timer_expired = 0; | |
1970 | } else if (--inm->inm_sctimer == 0) { | |
1971 | state_change_retransmit_timer_expired = 1; | |
1972 | } else { | |
1973 | state_change_timers_running = 1; | |
1974 | } | |
1975 | ||
1976 | /* We are in fasttimo, so be quick about it. */ | |
1977 | if (!state_change_retransmit_timer_expired && | |
1978 | !query_response_timer_expired) | |
1979 | return; | |
1980 | ||
1981 | switch (inm->inm_state) { | |
1982 | case IGMP_NOT_MEMBER: | |
1983 | case IGMP_SILENT_MEMBER: | |
1984 | case IGMP_SLEEPING_MEMBER: | |
1985 | case IGMP_LAZY_MEMBER: | |
1986 | case IGMP_AWAKENING_MEMBER: | |
1987 | case IGMP_IDLE_MEMBER: | |
1988 | break; | |
1989 | case IGMP_G_QUERY_PENDING_MEMBER: | |
1990 | case IGMP_SG_QUERY_PENDING_MEMBER: | |
1991 | /* | |
1992 | * Respond to a previously pending Group-Specific | |
1993 | * or Group-and-Source-Specific query by enqueueing | |
1994 | * the appropriate Current-State report for | |
1995 | * immediate transmission. | |
1996 | */ | |
1997 | if (query_response_timer_expired) { | |
1998 | int retval; | |
1999 | ||
2000 | retval = igmp_v3_enqueue_group_record(qrq, inm, 0, 1, | |
2001 | (inm->inm_state == IGMP_SG_QUERY_PENDING_MEMBER)); | |
2002 | IGMP_PRINTF(("%s: enqueue record = %d\n", | |
2003 | __func__, retval)); | |
2004 | inm->inm_state = IGMP_REPORTING_MEMBER; | |
2005 | /* XXX Clear recorded sources for next time. */ | |
2006 | inm_clear_recorded(inm); | |
2007 | } | |
2008 | /* FALLTHROUGH */ | |
2009 | case IGMP_REPORTING_MEMBER: | |
2010 | case IGMP_LEAVING_MEMBER: | |
2011 | if (state_change_retransmit_timer_expired) { | |
2012 | /* | |
2013 | * State-change retransmission timer fired. | |
2014 | * If there are any further pending retransmissions, | |
2015 | * set the global pending state-change flag, and | |
2016 | * reset the timer. | |
2017 | */ | |
2018 | if (--inm->inm_scrv > 0) { | |
2019 | inm->inm_sctimer = uri_fasthz; | |
2020 | state_change_timers_running = 1; | |
2021 | } | |
2022 | /* | |
2023 | * Retransmit the previously computed state-change | |
2024 | * report. If there are no further pending | |
2025 | * retransmissions, the mbuf queue will be consumed. | |
2026 | * Update T0 state to T1 as we have now sent | |
2027 | * a state-change. | |
2028 | */ | |
2029 | (void) igmp_v3_merge_state_changes(inm, scq); | |
2030 | ||
2031 | inm_commit(inm); | |
2032 | IGMP_PRINTF(("%s: T1 -> T0 for %s/%s%d\n", __func__, | |
2033 | inet_ntoa(inm->inm_addr), inm->inm_ifp->if_name, | |
2034 | inm->inm_ifp->if_unit)); | |
2035 | ||
2036 | /* | |
2037 | * If we are leaving the group for good, make sure | |
2038 | * we release IGMP's reference to it. | |
2039 | * This release must be deferred using a SLIST, | |
2040 | * as we are called from a loop which traverses | |
2041 | * the in_multihead list. | |
2042 | */ | |
2043 | if (inm->inm_state == IGMP_LEAVING_MEMBER && | |
2044 | inm->inm_scrv == 0) { | |
2045 | inm->inm_state = IGMP_NOT_MEMBER; | |
2046 | /* | |
2047 | * A reference has already been held in | |
2048 | * igmp_final_leave() for this inm, so | |
2049 | * no need to hold another one. We also | |
2050 | * bumped up its request count then, so | |
2051 | * that it stays in in_multihead. Both | |
2052 | * of them will be released when it is | |
2053 | * dequeued later on. | |
2054 | */ | |
2055 | VERIFY(inm->inm_nrelecnt != 0); | |
2056 | SLIST_INSERT_HEAD(&igi->igi_relinmhead, | |
2057 | inm, inm_nrele); | |
2058 | } | |
2059 | } | |
2060 | break; | |
2061 | } | |
2062 | } | |
2063 | ||
2064 | /* | |
2065 | * Suppress a group's pending response to a group or source/group query. | |
2066 | * | |
2067 | * Do NOT suppress state changes. This leads to IGMPv3 inconsistency. | |
2068 | * Do NOT update ST1/ST0 as this operation merely suppresses | |
2069 | * the currently pending group record. | |
2070 | * Do NOT suppress the response to a general query. It is possible but | |
2071 | * it would require adding another state or flag. | |
2072 | */ | |
2073 | static void | |
2074 | igmp_v3_suppress_group_record(struct in_multi *inm) | |
2075 | { | |
2076 | ||
2077 | INM_LOCK_ASSERT_HELD(inm); | |
2078 | IGI_LOCK_ASSERT_HELD(inm->inm_igi); | |
2079 | ||
2080 | VERIFY(inm->inm_igi->igi_version == IGMP_VERSION_3); | |
2081 | ||
2082 | if (inm->inm_state != IGMP_G_QUERY_PENDING_MEMBER || | |
2083 | inm->inm_state != IGMP_SG_QUERY_PENDING_MEMBER) | |
2084 | return; | |
2085 | ||
2086 | if (inm->inm_state == IGMP_SG_QUERY_PENDING_MEMBER) | |
2087 | inm_clear_recorded(inm); | |
2088 | ||
2089 | inm->inm_timer = 0; | |
2090 | inm->inm_state = IGMP_REPORTING_MEMBER; | |
2091 | } | |
2092 | ||
2093 | /* | |
2094 | * Switch to a different IGMP version on the given interface, | |
2095 | * as per Section 7.2.1. | |
2096 | */ | |
2097 | static void | |
2098 | igmp_set_version(struct igmp_ifinfo *igi, const int igmp_version) | |
2099 | { | |
2100 | int old_version_timer; | |
2101 | ||
2102 | IGI_LOCK_ASSERT_HELD(igi); | |
2103 | ||
2104 | IGMP_PRINTF(("%s: switching to v%d on ifp %p(%s%d)\n", __func__, | |
2105 | igmp_version, igi->igi_ifp, igi->igi_ifp->if_name, | |
2106 | igi->igi_ifp->if_unit)); | |
2107 | ||
2108 | if (igmp_version == IGMP_VERSION_1 || igmp_version == IGMP_VERSION_2) { | |
2109 | /* | |
2110 | * Compute the "Older Version Querier Present" timer as per | |
2111 | * Section 8.12. | |
2112 | */ | |
2113 | old_version_timer = igi->igi_rv * igi->igi_qi + igi->igi_qri; | |
2114 | old_version_timer *= PR_SLOWHZ; | |
2115 | ||
2116 | if (igmp_version == IGMP_VERSION_1) { | |
2117 | igi->igi_v1_timer = old_version_timer; | |
2118 | igi->igi_v2_timer = 0; | |
2119 | } else if (igmp_version == IGMP_VERSION_2) { | |
2120 | igi->igi_v1_timer = 0; | |
2121 | igi->igi_v2_timer = old_version_timer; | |
2122 | } | |
2123 | } | |
2124 | ||
2125 | if (igi->igi_v1_timer == 0 && igi->igi_v2_timer > 0) { | |
2126 | if (igi->igi_version != IGMP_VERSION_2) { | |
2127 | igi->igi_version = IGMP_VERSION_2; | |
2128 | igmp_v3_cancel_link_timers(igi); | |
2129 | } | |
2130 | } else if (igi->igi_v1_timer > 0) { | |
2131 | if (igi->igi_version != IGMP_VERSION_1) { | |
2132 | igi->igi_version = IGMP_VERSION_1; | |
2133 | igmp_v3_cancel_link_timers(igi); | |
2134 | } | |
2135 | } | |
2136 | ||
2137 | IGI_LOCK_ASSERT_HELD(igi); | |
2138 | } | |
2139 | ||
2140 | /* | |
2141 | * Cancel pending IGMPv3 timers for the given link and all groups | |
2142 | * joined on it; state-change, general-query, and group-query timers. | |
2143 | * | |
2144 | * Only ever called on a transition from v3 to Compatibility mode. Kill | |
2145 | * the timers stone dead (this may be expensive for large N groups), they | |
2146 | * will be restarted if Compatibility Mode deems that they must be due to | |
2147 | * query processing. | |
2148 | */ | |
2149 | static void | |
2150 | igmp_v3_cancel_link_timers(struct igmp_ifinfo *igi) | |
2151 | { | |
2152 | struct ifnet *ifp; | |
2153 | struct in_multi *inm; | |
2154 | struct in_multistep step; | |
2155 | ||
2156 | IGI_LOCK_ASSERT_HELD(igi); | |
2157 | ||
2158 | IGMP_PRINTF(("%s: cancel v3 timers on ifp %p(%s%d)\n", __func__, | |
2159 | igi->igi_ifp, igi->igi_ifp->if_name, igi->igi_ifp->if_unit)); | |
2160 | ||
2161 | /* | |
2162 | * Stop the v3 General Query Response on this link stone dead. | |
2163 | * If fasttimo is woken up due to interface_timers_running, | |
2164 | * the flag will be cleared if there are no pending link timers. | |
2165 | */ | |
2166 | igi->igi_v3_timer = 0; | |
2167 | ||
2168 | /* | |
2169 | * Now clear the current-state and state-change report timers | |
2170 | * for all memberships scoped to this link. | |
2171 | */ | |
2172 | ifp = igi->igi_ifp; | |
2173 | IGI_UNLOCK(igi); | |
2174 | ||
2175 | in_multihead_lock_shared(); | |
2176 | IN_FIRST_MULTI(step, inm); | |
2177 | while (inm != NULL) { | |
2178 | INM_LOCK(inm); | |
2179 | if (inm->inm_ifp != ifp) | |
2180 | goto next; | |
2181 | ||
2182 | switch (inm->inm_state) { | |
2183 | case IGMP_NOT_MEMBER: | |
2184 | case IGMP_SILENT_MEMBER: | |
2185 | case IGMP_IDLE_MEMBER: | |
2186 | case IGMP_LAZY_MEMBER: | |
2187 | case IGMP_SLEEPING_MEMBER: | |
2188 | case IGMP_AWAKENING_MEMBER: | |
2189 | /* | |
2190 | * These states are either not relevant in v3 mode, | |
2191 | * or are unreported. Do nothing. | |
2192 | */ | |
2193 | break; | |
2194 | case IGMP_LEAVING_MEMBER: | |
2195 | /* | |
2196 | * If we are leaving the group and switching to | |
2197 | * compatibility mode, we need to release the final | |
2198 | * reference held for issuing the INCLUDE {}, and | |
2199 | * transition to REPORTING to ensure the host leave | |
2200 | * message is sent upstream to the old querier -- | |
2201 | * transition to NOT would lose the leave and race. | |
2202 | * During igmp_final_leave(), we bumped up both the | |
2203 | * request and reference counts. Since we cannot | |
2204 | * call in_multi_detach() here, defer this task to | |
2205 | * the timer routine. | |
2206 | */ | |
2207 | VERIFY(inm->inm_nrelecnt != 0); | |
2208 | IGI_LOCK(igi); | |
2209 | SLIST_INSERT_HEAD(&igi->igi_relinmhead, inm, inm_nrele); | |
2210 | IGI_UNLOCK(igi); | |
2211 | /* FALLTHROUGH */ | |
2212 | case IGMP_G_QUERY_PENDING_MEMBER: | |
2213 | case IGMP_SG_QUERY_PENDING_MEMBER: | |
2214 | inm_clear_recorded(inm); | |
2215 | /* FALLTHROUGH */ | |
2216 | case IGMP_REPORTING_MEMBER: | |
2217 | inm->inm_state = IGMP_REPORTING_MEMBER; | |
2218 | break; | |
2219 | } | |
2220 | /* | |
2221 | * Always clear state-change and group report timers. | |
2222 | * Free any pending IGMPv3 state-change records. | |
2223 | */ | |
2224 | inm->inm_sctimer = 0; | |
2225 | inm->inm_timer = 0; | |
2226 | IF_DRAIN(&inm->inm_scq); | |
2227 | next: | |
2228 | INM_UNLOCK(inm); | |
2229 | IN_NEXT_MULTI(step, inm); | |
2230 | } | |
2231 | in_multihead_lock_done(); | |
2232 | ||
2233 | IGI_LOCK(igi); | |
2234 | } | |
2235 | ||
2236 | /* | |
2237 | * Update the Older Version Querier Present timers for a link. | |
2238 | * See Section 7.2.1 of RFC 3376. | |
2239 | */ | |
2240 | static void | |
2241 | igmp_v1v2_process_querier_timers(struct igmp_ifinfo *igi) | |
2242 | { | |
2243 | IGI_LOCK_ASSERT_HELD(igi); | |
2244 | ||
2245 | if (igi->igi_v1_timer == 0 && igi->igi_v2_timer == 0) { | |
2246 | /* | |
2247 | * IGMPv1 and IGMPv2 Querier Present timers expired. | |
2248 | * | |
2249 | * Revert to IGMPv3. | |
2250 | */ | |
2251 | if (igi->igi_version != IGMP_VERSION_3) { | |
2252 | IGMP_PRINTF(("%s: transition from v%d -> v%d on %p(%s%d)\n", | |
2253 | __func__, igi->igi_version, IGMP_VERSION_3, | |
2254 | igi->igi_ifp, igi->igi_ifp->if_name, | |
2255 | igi->igi_ifp->if_unit)); | |
2256 | igi->igi_version = IGMP_VERSION_3; | |
2257 | IF_DRAIN(&igi->igi_v2q); | |
2258 | } | |
2259 | } else if (igi->igi_v1_timer == 0 && igi->igi_v2_timer > 0) { | |
2260 | /* | |
2261 | * IGMPv1 Querier Present timer expired, | |
2262 | * IGMPv2 Querier Present timer running. | |
2263 | * If IGMPv2 was disabled since last timeout, | |
2264 | * revert to IGMPv3. | |
2265 | * If IGMPv2 is enabled, revert to IGMPv2. | |
2266 | */ | |
2267 | if (!igmp_v2enable) { | |
2268 | IGMP_PRINTF(("%s: transition from v%d -> v%d on %p(%s%d)\n", | |
2269 | __func__, igi->igi_version, IGMP_VERSION_3, | |
2270 | igi->igi_ifp, igi->igi_ifp->if_name, | |
2271 | igi->igi_ifp->if_unit)); | |
2272 | igi->igi_v2_timer = 0; | |
2273 | igi->igi_version = IGMP_VERSION_3; | |
2274 | IF_DRAIN(&igi->igi_v2q); | |
2275 | } else { | |
2276 | --igi->igi_v2_timer; | |
2277 | if (igi->igi_version != IGMP_VERSION_2) { | |
2278 | IGMP_PRINTF(("%s: transition from v%d -> v%d on %p(%s%d)\n", | |
2279 | __func__, igi->igi_version, IGMP_VERSION_2, | |
2280 | igi->igi_ifp, igi->igi_ifp->if_name, | |
2281 | igi->igi_ifp->if_unit)); | |
2282 | igi->igi_version = IGMP_VERSION_2; | |
2283 | IF_DRAIN(&igi->igi_gq); | |
2284 | } | |
2285 | } | |
2286 | } else if (igi->igi_v1_timer > 0) { | |
2287 | /* | |
2288 | * IGMPv1 Querier Present timer running. | |
2289 | * Stop IGMPv2 timer if running. | |
2290 | * | |
2291 | * If IGMPv1 was disabled since last timeout, | |
2292 | * revert to IGMPv3. | |
2293 | * If IGMPv1 is enabled, reset IGMPv2 timer if running. | |
2294 | */ | |
2295 | if (!igmp_v1enable) { | |
2296 | IGMP_PRINTF(("%s: transition from v%d -> v%d on %p(%s%d)\n", | |
2297 | __func__, igi->igi_version, IGMP_VERSION_3, | |
2298 | igi->igi_ifp, igi->igi_ifp->if_name, | |
2299 | igi->igi_ifp->if_unit)); | |
2300 | igi->igi_v1_timer = 0; | |
2301 | igi->igi_version = IGMP_VERSION_3; | |
2302 | IF_DRAIN(&igi->igi_v2q); | |
2303 | } else { | |
2304 | --igi->igi_v1_timer; | |
2305 | } | |
2306 | if (igi->igi_v2_timer > 0) { | |
2307 | IGMP_PRINTF(("%s: cancel v2 timer on %p(%s%d)\n", | |
2308 | __func__, igi->igi_ifp, igi->igi_ifp->if_name, | |
2309 | igi->igi_ifp->if_unit)); | |
2310 | igi->igi_v2_timer = 0; | |
2311 | } | |
2312 | } | |
2313 | } | |
2314 | ||
2315 | /* | |
2316 | * Dispatch an IGMPv1/v2 host report or leave message. | |
2317 | * These are always small enough to fit inside a single mbuf. | |
2318 | */ | |
2319 | static int | |
2320 | igmp_v1v2_queue_report(struct in_multi *inm, const int type) | |
2321 | { | |
2322 | struct ifnet *ifp; | |
2323 | struct igmp *igmp; | |
2324 | struct ip *ip; | |
2325 | struct mbuf *m; | |
2326 | int error = 0; | |
2327 | ||
2328 | INM_LOCK_ASSERT_HELD(inm); | |
2329 | IGI_LOCK_ASSERT_HELD(inm->inm_igi); | |
2330 | ||
2331 | ifp = inm->inm_ifp; | |
2332 | ||
2333 | MGETHDR(m, M_DONTWAIT, MT_DATA); | |
2334 | if (m == NULL) | |
2335 | return (ENOMEM); | |
2336 | MH_ALIGN(m, sizeof(struct ip) + sizeof(struct igmp)); | |
2337 | ||
2338 | m->m_pkthdr.len = sizeof(struct ip) + sizeof(struct igmp); | |
2339 | ||
2340 | m->m_data += sizeof(struct ip); | |
2341 | m->m_len = sizeof(struct igmp); | |
2342 | ||
2343 | igmp = mtod(m, struct igmp *); | |
2344 | igmp->igmp_type = type; | |
2345 | igmp->igmp_code = 0; | |
2346 | igmp->igmp_group = inm->inm_addr; | |
2347 | igmp->igmp_cksum = 0; | |
2348 | igmp->igmp_cksum = in_cksum(m, sizeof(struct igmp)); | |
2349 | ||
2350 | m->m_data -= sizeof(struct ip); | |
2351 | m->m_len += sizeof(struct ip); | |
2352 | ||
2353 | ip = mtod(m, struct ip *); | |
2354 | ip->ip_tos = 0; | |
2355 | ip->ip_len = sizeof(struct ip) + sizeof(struct igmp); | |
2356 | ip->ip_off = 0; | |
2357 | ip->ip_p = IPPROTO_IGMP; | |
2358 | ip->ip_src.s_addr = INADDR_ANY; | |
2359 | ||
2360 | if (type == IGMP_HOST_LEAVE_MESSAGE) | |
2361 | ip->ip_dst.s_addr = htonl(INADDR_ALLRTRS_GROUP); | |
2362 | else | |
2363 | ip->ip_dst = inm->inm_addr; | |
2364 | ||
2365 | m->m_flags |= M_IGMPV2; | |
2366 | if (inm->inm_igi->igi_flags & IGIF_LOOPBACK) | |
2367 | m->m_flags |= M_IGMP_LOOP; | |
2368 | ||
2369 | /* | |
2370 | * Due to the fact that at this point we are possibly holding | |
2371 | * in_multihead_lock in shared or exclusive mode, we can't call | |
2372 | * igmp_sendpkt() here since that will eventually call ip_output(), | |
2373 | * which will try to lock in_multihead_lock and cause a deadlock. | |
2374 | * Instead we defer the work to the igmp_slowtimo() thread, thus | |
2375 | * avoiding unlocking in_multihead_lock here. | |
2376 | */ | |
2377 | if (IF_QFULL(&inm->inm_igi->igi_v2q)) { | |
2378 | IGMP_PRINTF(("%s: v1/v2 outbound queue full\n", __func__)); | |
2379 | error = ENOMEM; | |
2380 | m_freem(m); | |
2381 | } else | |
2382 | IF_ENQUEUE(&inm->inm_igi->igi_v2q, m); | |
2383 | ||
2384 | return (error); | |
2385 | } | |
2386 | ||
2387 | /* | |
2388 | * Process a state change from the upper layer for the given IPv4 group. | |
2389 | * | |
2390 | * Each socket holds a reference on the in_multi in its own ip_moptions. | |
2391 | * The socket layer will have made the necessary updates to the group | |
2392 | * state, it is now up to IGMP to issue a state change report if there | |
2393 | * has been any change between T0 (when the last state-change was issued) | |
2394 | * and T1 (now). | |
2395 | * | |
2396 | * We use the IGMPv3 state machine at group level. The IGMP module | |
2397 | * however makes the decision as to which IGMP protocol version to speak. | |
2398 | * A state change *from* INCLUDE {} always means an initial join. | |
2399 | * A state change *to* INCLUDE {} always means a final leave. | |
2400 | * | |
2401 | * FUTURE: If IGIF_V3LITE is enabled for this interface, then we can | |
2402 | * save ourselves a bunch of work; any exclusive mode groups need not | |
2403 | * compute source filter lists. | |
2404 | */ | |
2405 | int | |
2406 | igmp_change_state(struct in_multi *inm) | |
2407 | { | |
2408 | struct igmp_ifinfo *igi; | |
2409 | struct ifnet *ifp; | |
2410 | int error = 0; | |
2411 | ||
2412 | INM_LOCK_ASSERT_HELD(inm); | |
2413 | VERIFY(inm->inm_igi != NULL); | |
2414 | IGI_LOCK_ASSERT_NOTHELD(inm->inm_igi); | |
2415 | ||
2416 | /* | |
2417 | * Try to detect if the upper layer just asked us to change state | |
2418 | * for an interface which has now gone away. | |
2419 | */ | |
2420 | VERIFY(inm->inm_ifma != NULL); | |
2421 | ifp = inm->inm_ifma->ifma_ifp; | |
2422 | /* | |
2423 | * Sanity check that netinet's notion of ifp is the same as net's. | |
2424 | */ | |
2425 | VERIFY(inm->inm_ifp == ifp); | |
2426 | ||
2427 | igi = IGMP_IFINFO(ifp); | |
2428 | VERIFY(igi != NULL); | |
2429 | ||
2430 | /* | |
2431 | * If we detect a state transition to or from MCAST_UNDEFINED | |
2432 | * for this group, then we are starting or finishing an IGMP | |
2433 | * life cycle for this group. | |
2434 | */ | |
2435 | if (inm->inm_st[1].iss_fmode != inm->inm_st[0].iss_fmode) { | |
2436 | IGMP_PRINTF(("%s: inm transition %d -> %d\n", __func__, | |
2437 | inm->inm_st[0].iss_fmode, inm->inm_st[1].iss_fmode)); | |
2438 | if (inm->inm_st[0].iss_fmode == MCAST_UNDEFINED) { | |
2439 | IGMP_PRINTF(("%s: initial join\n", __func__)); | |
2440 | error = igmp_initial_join(inm, igi); | |
2441 | goto out; | |
2442 | } else if (inm->inm_st[1].iss_fmode == MCAST_UNDEFINED) { | |
2443 | IGMP_PRINTF(("%s: final leave\n", __func__)); | |
2444 | igmp_final_leave(inm, igi); | |
2445 | goto out; | |
2446 | } | |
2447 | } else { | |
2448 | IGMP_PRINTF(("%s: filter set change\n", __func__)); | |
2449 | } | |
2450 | ||
2451 | error = igmp_handle_state_change(inm, igi); | |
2452 | out: | |
2453 | return (error); | |
2454 | } | |
2455 | ||
2456 | /* | |
2457 | * Perform the initial join for an IGMP group. | |
2458 | * | |
2459 | * When joining a group: | |
2460 | * If the group should have its IGMP traffic suppressed, do nothing. | |
2461 | * IGMPv1 starts sending IGMPv1 host membership reports. | |
2462 | * IGMPv2 starts sending IGMPv2 host membership reports. | |
2463 | * IGMPv3 will schedule an IGMPv3 state-change report containing the | |
2464 | * initial state of the membership. | |
2465 | */ | |
2466 | static int | |
2467 | igmp_initial_join(struct in_multi *inm, struct igmp_ifinfo *igi) | |
2468 | { | |
2469 | struct ifnet *ifp; | |
2470 | struct ifqueue *ifq; | |
2471 | int error, retval, syncstates; | |
2472 | ||
2473 | INM_LOCK_ASSERT_HELD(inm); | |
2474 | IGI_LOCK_ASSERT_NOTHELD(igi); | |
2475 | ||
2476 | IGMP_PRINTF(("%s: initial join %s on ifp %p(%s%d)\n", | |
2477 | __func__, inet_ntoa(inm->inm_addr), inm->inm_ifp, | |
2478 | inm->inm_ifp->if_name, inm->inm_ifp->if_unit)); | |
2479 | ||
2480 | error = 0; | |
2481 | syncstates = 1; | |
2482 | ||
2483 | ifp = inm->inm_ifp; | |
2484 | ||
2485 | IGI_LOCK(igi); | |
2486 | VERIFY(igi->igi_ifp == ifp); | |
2487 | ||
2488 | /* | |
2489 | * Groups joined on loopback or marked as 'not reported', | |
2490 | * e.g. 224.0.0.1, enter the IGMP_SILENT_MEMBER state and | |
2491 | * are never reported in any IGMP protocol exchanges. | |
2492 | * All other groups enter the appropriate IGMP state machine | |
2493 | * for the version in use on this link. | |
2494 | * A link marked as IGIF_SILENT causes IGMP to be completely | |
2495 | * disabled for the link. | |
2496 | */ | |
2497 | if ((ifp->if_flags & IFF_LOOPBACK) || | |
2498 | (igi->igi_flags & IGIF_SILENT) || | |
2499 | !igmp_isgroupreported(inm->inm_addr)) { | |
2500 | IGMP_PRINTF(("%s: not kicking state machine for silent group\n", | |
2501 | __func__)); | |
2502 | inm->inm_state = IGMP_SILENT_MEMBER; | |
2503 | inm->inm_timer = 0; | |
2504 | } else { | |
2505 | /* | |
2506 | * Deal with overlapping in_multi lifecycle. | |
2507 | * If this group was LEAVING, then make sure | |
2508 | * we drop the reference we picked up to keep the | |
2509 | * group around for the final INCLUDE {} enqueue. | |
2510 | * Since we cannot call in_multi_detach() here, | |
2511 | * defer this task to the timer routine. | |
2512 | */ | |
2513 | if (igi->igi_version == IGMP_VERSION_3 && | |
2514 | inm->inm_state == IGMP_LEAVING_MEMBER) { | |
2515 | VERIFY(inm->inm_nrelecnt != 0); | |
2516 | SLIST_INSERT_HEAD(&igi->igi_relinmhead, inm, inm_nrele); | |
2517 | } | |
2518 | ||
2519 | inm->inm_state = IGMP_REPORTING_MEMBER; | |
2520 | ||
2521 | switch (igi->igi_version) { | |
2522 | case IGMP_VERSION_1: | |
2523 | case IGMP_VERSION_2: | |
2524 | inm->inm_state = IGMP_IDLE_MEMBER; | |
2525 | error = igmp_v1v2_queue_report(inm, | |
2526 | (igi->igi_version == IGMP_VERSION_2) ? | |
2527 | IGMP_v2_HOST_MEMBERSHIP_REPORT : | |
2528 | IGMP_v1_HOST_MEMBERSHIP_REPORT); | |
2529 | ||
2530 | INM_LOCK_ASSERT_HELD(inm); | |
2531 | IGI_LOCK_ASSERT_HELD(igi); | |
2532 | ||
2533 | if (error == 0) { | |
2534 | inm->inm_timer = IGMP_RANDOM_DELAY( | |
2535 | IGMP_V1V2_MAX_RI * PR_SLOWHZ); | |
2536 | current_state_timers_running = 1; | |
2537 | } | |
2538 | break; | |
2539 | ||
2540 | case IGMP_VERSION_3: | |
2541 | /* | |
2542 | * Defer update of T0 to T1, until the first copy | |
2543 | * of the state change has been transmitted. | |
2544 | */ | |
2545 | syncstates = 0; | |
2546 | ||
2547 | /* | |
2548 | * Immediately enqueue a State-Change Report for | |
2549 | * this interface, freeing any previous reports. | |
2550 | * Don't kick the timers if there is nothing to do, | |
2551 | * or if an error occurred. | |
2552 | */ | |
2553 | ifq = &inm->inm_scq; | |
2554 | IF_DRAIN(ifq); | |
2555 | retval = igmp_v3_enqueue_group_record(ifq, inm, 1, | |
2556 | 0, 0); | |
2557 | IGMP_PRINTF(("%s: enqueue record = %d\n", | |
2558 | __func__, retval)); | |
2559 | if (retval <= 0) { | |
2560 | error = retval * -1; | |
2561 | break; | |
2562 | } | |
2563 | ||
2564 | /* | |
2565 | * Schedule transmission of pending state-change | |
2566 | * report up to RV times for this link. The timer | |
2567 | * will fire at the next igmp_fasttimo (~200ms), | |
2568 | * giving us an opportunity to merge the reports. | |
2569 | */ | |
2570 | if (igi->igi_flags & IGIF_LOOPBACK) { | |
2571 | inm->inm_scrv = 1; | |
2572 | } else { | |
2573 | VERIFY(igi->igi_rv > 1); | |
2574 | inm->inm_scrv = igi->igi_rv; | |
2575 | } | |
2576 | inm->inm_sctimer = 1; | |
2577 | state_change_timers_running = 1; | |
2578 | ||
2579 | error = 0; | |
2580 | break; | |
2581 | } | |
2582 | } | |
2583 | IGI_UNLOCK(igi); | |
2584 | ||
2585 | /* | |
2586 | * Only update the T0 state if state change is atomic, | |
2587 | * i.e. we don't need to wait for a timer to fire before we | |
2588 | * can consider the state change to have been communicated. | |
2589 | */ | |
2590 | if (syncstates) { | |
2591 | inm_commit(inm); | |
2592 | IGMP_PRINTF(("%s: T1 -> T0 for %s/%s%d\n", __func__, | |
2593 | inet_ntoa(inm->inm_addr), inm->inm_ifp->if_name, | |
2594 | inm->inm_ifp->if_unit)); | |
2595 | } | |
2596 | ||
2597 | return (error); | |
2598 | } | |
2599 | ||
2600 | /* | |
2601 | * Issue an intermediate state change during the IGMP life-cycle. | |
2602 | */ | |
2603 | static int | |
2604 | igmp_handle_state_change(struct in_multi *inm, struct igmp_ifinfo *igi) | |
2605 | { | |
2606 | struct ifnet *ifp; | |
2607 | int retval; | |
2608 | ||
2609 | INM_LOCK_ASSERT_HELD(inm); | |
2610 | IGI_LOCK_ASSERT_NOTHELD(igi); | |
2611 | ||
2612 | IGMP_PRINTF(("%s: state change for %s on ifp %p(%s%d)\n", | |
2613 | __func__, inet_ntoa(inm->inm_addr), inm->inm_ifp, | |
2614 | inm->inm_ifp->if_name, inm->inm_ifp->if_unit)); | |
2615 | ||
2616 | ifp = inm->inm_ifp; | |
2617 | ||
2618 | IGI_LOCK(igi); | |
2619 | VERIFY(igi->igi_ifp == ifp); | |
2620 | ||
2621 | if ((ifp->if_flags & IFF_LOOPBACK) || | |
2622 | (igi->igi_flags & IGIF_SILENT) || | |
2623 | !igmp_isgroupreported(inm->inm_addr) || | |
2624 | (igi->igi_version != IGMP_VERSION_3)) { | |
2625 | IGI_UNLOCK(igi); | |
2626 | if (!igmp_isgroupreported(inm->inm_addr)) { | |
2627 | IGMP_PRINTF(("%s: not kicking state " | |
2628 | "machine for silent group\n", __func__)); | |
2629 | } | |
2630 | IGMP_PRINTF(("%s: nothing to do\n", __func__)); | |
2631 | inm_commit(inm); | |
2632 | IGMP_PRINTF(("%s: T1 -> T0 for %s/%s\n", __func__, | |
2633 | inet_ntoa(inm->inm_addr), inm->inm_ifp->if_name)); | |
2634 | return (0); | |
2635 | } | |
2636 | ||
2637 | IF_DRAIN(&inm->inm_scq); | |
2638 | ||
2639 | retval = igmp_v3_enqueue_group_record(&inm->inm_scq, inm, 1, 0, 0); | |
2640 | IGMP_PRINTF(("%s: enqueue record = %d\n", __func__, retval)); | |
2641 | if (retval <= 0) { | |
2642 | IGI_UNLOCK(igi); | |
2643 | return (-retval); | |
2644 | } | |
2645 | /* | |
2646 | * If record(s) were enqueued, start the state-change | |
2647 | * report timer for this group. | |
2648 | */ | |
2649 | inm->inm_scrv = ((igi->igi_flags & IGIF_LOOPBACK) ? 1 : igi->igi_rv); | |
2650 | inm->inm_sctimer = 1; | |
2651 | state_change_timers_running = 1; | |
2652 | IGI_UNLOCK(igi); | |
2653 | ||
2654 | return (0); | |
2655 | } | |
2656 | ||
2657 | /* | |
2658 | * Perform the final leave for an IGMP group. | |
2659 | * | |
2660 | * When leaving a group: | |
2661 | * IGMPv1 does nothing. | |
2662 | * IGMPv2 sends a host leave message, if and only if we are the reporter. | |
2663 | * IGMPv3 enqueues a state-change report containing a transition | |
2664 | * to INCLUDE {} for immediate transmission. | |
2665 | */ | |
2666 | static void | |
2667 | igmp_final_leave(struct in_multi *inm, struct igmp_ifinfo *igi) | |
2668 | { | |
2669 | int syncstates = 1; | |
2670 | ||
2671 | INM_LOCK_ASSERT_HELD(inm); | |
2672 | IGI_LOCK_ASSERT_NOTHELD(igi); | |
2673 | ||
2674 | IGMP_PRINTF(("%s: final leave %s on ifp %p(%s%d)\n", | |
2675 | __func__, inet_ntoa(inm->inm_addr), inm->inm_ifp, | |
2676 | inm->inm_ifp->if_name, inm->inm_ifp->if_unit)); | |
2677 | ||
2678 | switch (inm->inm_state) { | |
2679 | case IGMP_NOT_MEMBER: | |
2680 | case IGMP_SILENT_MEMBER: | |
2681 | case IGMP_LEAVING_MEMBER: | |
2682 | /* Already leaving or left; do nothing. */ | |
2683 | IGMP_PRINTF(("%s: not kicking state machine for silent group\n", | |
2684 | __func__)); | |
2685 | break; | |
2686 | case IGMP_REPORTING_MEMBER: | |
2687 | case IGMP_IDLE_MEMBER: | |
2688 | case IGMP_G_QUERY_PENDING_MEMBER: | |
2689 | case IGMP_SG_QUERY_PENDING_MEMBER: | |
2690 | IGI_LOCK(igi); | |
2691 | if (igi->igi_version == IGMP_VERSION_2) { | |
2692 | if (inm->inm_state == IGMP_G_QUERY_PENDING_MEMBER || | |
2693 | inm->inm_state == IGMP_SG_QUERY_PENDING_MEMBER) { | |
2694 | panic("%s: IGMPv3 state reached, not IGMPv3 " | |
2695 | "mode\n", __func__); | |
2696 | /* NOTREACHED */ | |
2697 | } | |
2698 | igmp_v1v2_queue_report(inm, IGMP_HOST_LEAVE_MESSAGE); | |
2699 | ||
2700 | INM_LOCK_ASSERT_HELD(inm); | |
2701 | IGI_LOCK_ASSERT_HELD(igi); | |
2702 | ||
2703 | inm->inm_state = IGMP_NOT_MEMBER; | |
2704 | } else if (igi->igi_version == IGMP_VERSION_3) { | |
2705 | /* | |
2706 | * Stop group timer and all pending reports. | |
2707 | * Immediately enqueue a state-change report | |
2708 | * TO_IN {} to be sent on the next fast timeout, | |
2709 | * giving us an opportunity to merge reports. | |
2710 | */ | |
2711 | IF_DRAIN(&inm->inm_scq); | |
2712 | inm->inm_timer = 0; | |
2713 | if (igi->igi_flags & IGIF_LOOPBACK) { | |
2714 | inm->inm_scrv = 1; | |
2715 | } else { | |
2716 | inm->inm_scrv = igi->igi_rv; | |
2717 | } | |
2718 | IGMP_PRINTF(("%s: Leaving %s/%s%d with %d " | |
2719 | "pending retransmissions.\n", __func__, | |
2720 | inet_ntoa(inm->inm_addr), | |
2721 | inm->inm_ifp->if_name, inm->inm_ifp->if_unit, | |
2722 | inm->inm_scrv)); | |
2723 | if (inm->inm_scrv == 0) { | |
2724 | inm->inm_state = IGMP_NOT_MEMBER; | |
2725 | inm->inm_sctimer = 0; | |
2726 | } else { | |
2727 | int retval; | |
2728 | /* | |
2729 | * Stick around in the in_multihead list; | |
2730 | * the final detach will be issued by | |
2731 | * igmp_v3_process_group_timers() when | |
2732 | * the retransmit timer expires. | |
2733 | */ | |
2734 | INM_ADDREF_LOCKED(inm); | |
2735 | VERIFY(inm->inm_debug & IFD_ATTACHED); | |
2736 | inm->inm_reqcnt++; | |
2737 | VERIFY(inm->inm_reqcnt >= 1); | |
2738 | inm->inm_nrelecnt++; | |
2739 | VERIFY(inm->inm_nrelecnt != 0); | |
2740 | ||
2741 | retval = igmp_v3_enqueue_group_record( | |
2742 | &inm->inm_scq, inm, 1, 0, 0); | |
2743 | KASSERT(retval != 0, | |
2744 | ("%s: enqueue record = %d\n", __func__, | |
2745 | retval)); | |
2746 | ||
2747 | inm->inm_state = IGMP_LEAVING_MEMBER; | |
2748 | inm->inm_sctimer = 1; | |
2749 | state_change_timers_running = 1; | |
2750 | syncstates = 0; | |
2751 | } | |
2752 | } | |
2753 | IGI_UNLOCK(igi); | |
2754 | break; | |
2755 | case IGMP_LAZY_MEMBER: | |
2756 | case IGMP_SLEEPING_MEMBER: | |
2757 | case IGMP_AWAKENING_MEMBER: | |
2758 | /* Our reports are suppressed; do nothing. */ | |
2759 | break; | |
2760 | } | |
2761 | ||
2762 | if (syncstates) { | |
2763 | inm_commit(inm); | |
2764 | IGMP_PRINTF(("%s: T1 -> T0 for %s/%s%d\n", __func__, | |
2765 | inet_ntoa(inm->inm_addr), inm->inm_ifp->if_name, | |
2766 | inm->inm_ifp->if_unit)); | |
2767 | inm->inm_st[1].iss_fmode = MCAST_UNDEFINED; | |
2768 | IGMP_PRINTF(("%s: T1 now MCAST_UNDEFINED for %s/%s%d\n", | |
2769 | __func__, inet_ntoa(inm->inm_addr), inm->inm_ifp->if_name, | |
2770 | inm->inm_ifp->if_unit)); | |
2771 | } | |
2772 | } | |
2773 | ||
2774 | /* | |
2775 | * Enqueue an IGMPv3 group record to the given output queue. | |
2776 | * | |
2777 | * XXX This function could do with having the allocation code | |
2778 | * split out, and the multiple-tree-walks coalesced into a single | |
2779 | * routine as has been done in igmp_v3_enqueue_filter_change(). | |
2780 | * | |
2781 | * If is_state_change is zero, a current-state record is appended. | |
2782 | * If is_state_change is non-zero, a state-change report is appended. | |
2783 | * | |
2784 | * If is_group_query is non-zero, an mbuf packet chain is allocated. | |
2785 | * If is_group_query is zero, and if there is a packet with free space | |
2786 | * at the tail of the queue, it will be appended to providing there | |
2787 | * is enough free space. | |
2788 | * Otherwise a new mbuf packet chain is allocated. | |
2789 | * | |
2790 | * If is_source_query is non-zero, each source is checked to see if | |
2791 | * it was recorded for a Group-Source query, and will be omitted if | |
2792 | * it is not both in-mode and recorded. | |
2793 | * | |
2794 | * The function will attempt to allocate leading space in the packet | |
2795 | * for the IP/IGMP header to be prepended without fragmenting the chain. | |
2796 | * | |
2797 | * If successful the size of all data appended to the queue is returned, | |
2798 | * otherwise an error code less than zero is returned, or zero if | |
2799 | * no record(s) were appended. | |
2800 | */ | |
2801 | static int | |
2802 | igmp_v3_enqueue_group_record(struct ifqueue *ifq, struct in_multi *inm, | |
2803 | const int is_state_change, const int is_group_query, | |
2804 | const int is_source_query) | |
2805 | { | |
2806 | struct igmp_grouprec ig; | |
2807 | struct igmp_grouprec *pig; | |
2808 | struct ifnet *ifp; | |
2809 | struct ip_msource *ims, *nims; | |
2810 | struct mbuf *m0, *m, *md; | |
2811 | int error, is_filter_list_change; | |
2812 | int minrec0len, m0srcs, msrcs, nbytes, off; | |
2813 | int record_has_sources; | |
2814 | int now; | |
2815 | int type; | |
2816 | in_addr_t naddr; | |
2817 | uint8_t mode; | |
2818 | ||
2819 | INM_LOCK_ASSERT_HELD(inm); | |
2820 | IGI_LOCK_ASSERT_HELD(inm->inm_igi); | |
2821 | ||
2822 | error = 0; | |
2823 | ifp = inm->inm_ifp; | |
2824 | is_filter_list_change = 0; | |
2825 | m = NULL; | |
2826 | m0 = NULL; | |
2827 | m0srcs = 0; | |
2828 | msrcs = 0; | |
2829 | nbytes = 0; | |
2830 | nims = NULL; | |
2831 | record_has_sources = 1; | |
2832 | pig = NULL; | |
2833 | type = IGMP_DO_NOTHING; | |
2834 | mode = inm->inm_st[1].iss_fmode; | |
2835 | ||
2836 | /* | |
2837 | * If we did not transition out of ASM mode during t0->t1, | |
2838 | * and there are no source nodes to process, we can skip | |
2839 | * the generation of source records. | |
2840 | */ | |
2841 | if (inm->inm_st[0].iss_asm > 0 && inm->inm_st[1].iss_asm > 0 && | |
2842 | inm->inm_nsrc == 0) | |
2843 | record_has_sources = 0; | |
2844 | ||
2845 | if (is_state_change) { | |
2846 | /* | |
2847 | * Queue a state change record. | |
2848 | * If the mode did not change, and there are non-ASM | |
2849 | * listeners or source filters present, | |
2850 | * we potentially need to issue two records for the group. | |
2851 | * If we are transitioning to MCAST_UNDEFINED, we need | |
2852 | * not send any sources. | |
2853 | * If there are ASM listeners, and there was no filter | |
2854 | * mode transition of any kind, do nothing. | |
2855 | */ | |
2856 | if (mode != inm->inm_st[0].iss_fmode) { | |
2857 | if (mode == MCAST_EXCLUDE) { | |
2858 | IGMP_PRINTF(("%s: change to EXCLUDE\n", | |
2859 | __func__)); | |
2860 | type = IGMP_CHANGE_TO_EXCLUDE_MODE; | |
2861 | } else { | |
2862 | IGMP_PRINTF(("%s: change to INCLUDE\n", | |
2863 | __func__)); | |
2864 | type = IGMP_CHANGE_TO_INCLUDE_MODE; | |
2865 | if (mode == MCAST_UNDEFINED) | |
2866 | record_has_sources = 0; | |
2867 | } | |
2868 | } else { | |
2869 | if (record_has_sources) { | |
2870 | is_filter_list_change = 1; | |
2871 | } else { | |
2872 | type = IGMP_DO_NOTHING; | |
2873 | } | |
2874 | } | |
2875 | } else { | |
2876 | /* | |
2877 | * Queue a current state record. | |
2878 | */ | |
2879 | if (mode == MCAST_EXCLUDE) { | |
2880 | type = IGMP_MODE_IS_EXCLUDE; | |
2881 | } else if (mode == MCAST_INCLUDE) { | |
2882 | type = IGMP_MODE_IS_INCLUDE; | |
2883 | VERIFY(inm->inm_st[1].iss_asm == 0); | |
2884 | } | |
2885 | } | |
2886 | ||
2887 | /* | |
2888 | * Generate the filter list changes using a separate function. | |
2889 | */ | |
2890 | if (is_filter_list_change) | |
2891 | return (igmp_v3_enqueue_filter_change(ifq, inm)); | |
2892 | ||
2893 | if (type == IGMP_DO_NOTHING) { | |
2894 | IGMP_PRINTF(("%s: nothing to do for %s/%s%d\n", | |
2895 | __func__, inet_ntoa(inm->inm_addr), | |
2896 | inm->inm_ifp->if_name, inm->inm_ifp->if_unit)); | |
2897 | return (0); | |
2898 | } | |
2899 | ||
2900 | /* | |
2901 | * If any sources are present, we must be able to fit at least | |
2902 | * one in the trailing space of the tail packet's mbuf, | |
2903 | * ideally more. | |
2904 | */ | |
2905 | minrec0len = sizeof(struct igmp_grouprec); | |
2906 | if (record_has_sources) | |
2907 | minrec0len += sizeof(in_addr_t); | |
2908 | ||
2909 | IGMP_PRINTF(("%s: queueing %s for %s/%s%d\n", __func__, | |
2910 | igmp_rec_type_to_str(type), inet_ntoa(inm->inm_addr), | |
2911 | inm->inm_ifp->if_name, inm->inm_ifp->if_unit)); | |
2912 | ||
2913 | /* | |
2914 | * Check if we have a packet in the tail of the queue for this | |
2915 | * group into which the first group record for this group will fit. | |
2916 | * Otherwise allocate a new packet. | |
2917 | * Always allocate leading space for IP+RA_OPT+IGMP+REPORT. | |
2918 | * Note: Group records for G/GSR query responses MUST be sent | |
2919 | * in their own packet. | |
2920 | */ | |
2921 | m0 = ifq->ifq_tail; | |
2922 | if (!is_group_query && | |
2923 | m0 != NULL && | |
2924 | (m0->m_pkthdr.vt_nrecs + 1 <= IGMP_V3_REPORT_MAXRECS) && | |
2925 | (m0->m_pkthdr.len + minrec0len) < | |
2926 | (ifp->if_mtu - IGMP_LEADINGSPACE)) { | |
2927 | m0srcs = (ifp->if_mtu - m0->m_pkthdr.len - | |
2928 | sizeof(struct igmp_grouprec)) / sizeof(in_addr_t); | |
2929 | m = m0; | |
2930 | IGMP_PRINTF(("%s: use existing packet\n", __func__)); | |
2931 | } else { | |
2932 | if (IF_QFULL(ifq)) { | |
2933 | IGMP_PRINTF(("%s: outbound queue full\n", __func__)); | |
2934 | return (-ENOMEM); | |
2935 | } | |
2936 | m = NULL; | |
2937 | m0srcs = (ifp->if_mtu - IGMP_LEADINGSPACE - | |
2938 | sizeof(struct igmp_grouprec)) / sizeof(in_addr_t); | |
2939 | if (!is_state_change && !is_group_query) { | |
2940 | m = m_getcl(M_DONTWAIT, MT_DATA, M_PKTHDR); | |
2941 | if (m) | |
2942 | m->m_data += IGMP_LEADINGSPACE; | |
2943 | } | |
2944 | if (m == NULL) { | |
2945 | m = m_gethdr(M_DONTWAIT, MT_DATA); | |
2946 | if (m) | |
2947 | MH_ALIGN(m, IGMP_LEADINGSPACE); | |
2948 | } | |
2949 | if (m == NULL) | |
2950 | return (-ENOMEM); | |
2951 | ||
2952 | IGMP_PRINTF(("%s: allocated first packet\n", __func__)); | |
2953 | } | |
2954 | ||
2955 | /* | |
2956 | * Append group record. | |
2957 | * If we have sources, we don't know how many yet. | |
2958 | */ | |
2959 | ig.ig_type = type; | |
2960 | ig.ig_datalen = 0; | |
2961 | ig.ig_numsrc = 0; | |
2962 | ig.ig_group = inm->inm_addr; | |
2963 | if (!m_append(m, sizeof(struct igmp_grouprec), (void *)&ig)) { | |
2964 | if (m != m0) | |
2965 | m_freem(m); | |
2966 | IGMP_PRINTF(("%s: m_append() failed.\n", __func__)); | |
2967 | return (-ENOMEM); | |
2968 | } | |
2969 | nbytes += sizeof(struct igmp_grouprec); | |
2970 | ||
2971 | /* | |
2972 | * Append as many sources as will fit in the first packet. | |
2973 | * If we are appending to a new packet, the chain allocation | |
2974 | * may potentially use clusters; use m_getptr() in this case. | |
2975 | * If we are appending to an existing packet, we need to obtain | |
2976 | * a pointer to the group record after m_append(), in case a new | |
2977 | * mbuf was allocated. | |
2978 | * Only append sources which are in-mode at t1. If we are | |
2979 | * transitioning to MCAST_UNDEFINED state on the group, do not | |
2980 | * include source entries. | |
2981 | * Only report recorded sources in our filter set when responding | |
2982 | * to a group-source query. | |
2983 | */ | |
2984 | if (record_has_sources) { | |
2985 | if (m == m0) { | |
2986 | md = m_last(m); | |
2987 | pig = (struct igmp_grouprec *)(mtod(md, uint8_t *) + | |
2988 | md->m_len - nbytes); | |
2989 | } else { | |
2990 | md = m_getptr(m, 0, &off); | |
2991 | pig = (struct igmp_grouprec *)(mtod(md, uint8_t *) + | |
2992 | off); | |
2993 | } | |
2994 | msrcs = 0; | |
2995 | RB_FOREACH_SAFE(ims, ip_msource_tree, &inm->inm_srcs, nims) { | |
2996 | IGMP_PRINTF(("%s: visit node %s\n", __func__, | |
2997 | inet_ntoa_haddr(ims->ims_haddr))); | |
2998 | now = ims_get_mode(inm, ims, 1); | |
2999 | IGMP_PRINTF(("%s: node is %d\n", __func__, now)); | |
3000 | if ((now != mode) || | |
3001 | (now == mode && mode == MCAST_UNDEFINED)) { | |
3002 | IGMP_PRINTF(("%s: skip node\n", __func__)); | |
3003 | continue; | |
3004 | } | |
3005 | if (is_source_query && ims->ims_stp == 0) { | |
3006 | IGMP_PRINTF(("%s: skip unrecorded node\n", | |
3007 | __func__)); | |
3008 | continue; | |
3009 | } | |
3010 | IGMP_PRINTF(("%s: append node\n", __func__)); | |
3011 | naddr = htonl(ims->ims_haddr); | |
3012 | if (!m_append(m, sizeof(in_addr_t), (void *)&naddr)) { | |
3013 | if (m != m0) | |
3014 | m_freem(m); | |
3015 | IGMP_PRINTF(("%s: m_append() failed.\n", | |
3016 | __func__)); | |
3017 | return (-ENOMEM); | |
3018 | } | |
3019 | nbytes += sizeof(in_addr_t); | |
3020 | ++msrcs; | |
3021 | if (msrcs == m0srcs) | |
3022 | break; | |
3023 | } | |
3024 | IGMP_PRINTF(("%s: msrcs is %d this packet\n", __func__, | |
3025 | msrcs)); | |
3026 | pig->ig_numsrc = htons(msrcs); | |
3027 | nbytes += (msrcs * sizeof(in_addr_t)); | |
3028 | } | |
3029 | ||
3030 | if (is_source_query && msrcs == 0) { | |
3031 | IGMP_PRINTF(("%s: no recorded sources to report\n", __func__)); | |
3032 | if (m != m0) | |
3033 | m_freem(m); | |
3034 | return (0); | |
3035 | } | |
3036 | ||
3037 | /* | |
3038 | * We are good to go with first packet. | |
3039 | */ | |
3040 | if (m != m0) { | |
3041 | IGMP_PRINTF(("%s: enqueueing first packet\n", __func__)); | |
3042 | m->m_pkthdr.vt_nrecs = 1; | |
3043 | m->m_pkthdr.rcvif = ifp; | |
3044 | IF_ENQUEUE(ifq, m); | |
3045 | } else { | |
3046 | m->m_pkthdr.vt_nrecs++; | |
3047 | } | |
3048 | /* | |
3049 | * No further work needed if no source list in packet(s). | |
3050 | */ | |
3051 | if (!record_has_sources) | |
3052 | return (nbytes); | |
3053 | ||
3054 | /* | |
3055 | * Whilst sources remain to be announced, we need to allocate | |
3056 | * a new packet and fill out as many sources as will fit. | |
3057 | * Always try for a cluster first. | |
3058 | */ | |
3059 | while (nims != NULL) { | |
3060 | if (IF_QFULL(ifq)) { | |
3061 | IGMP_PRINTF(("%s: outbound queue full\n", __func__)); | |
3062 | return (-ENOMEM); | |
3063 | } | |
3064 | m = m_getcl(M_DONTWAIT, MT_DATA, M_PKTHDR); | |
3065 | if (m) | |
3066 | m->m_data += IGMP_LEADINGSPACE; | |
3067 | if (m == NULL) { | |
3068 | m = m_gethdr(M_DONTWAIT, MT_DATA); | |
3069 | if (m) | |
3070 | MH_ALIGN(m, IGMP_LEADINGSPACE); | |
3071 | } | |
3072 | if (m == NULL) | |
3073 | return (-ENOMEM); | |
3074 | md = m_getptr(m, 0, &off); | |
3075 | pig = (struct igmp_grouprec *)(mtod(md, uint8_t *) + off); | |
3076 | IGMP_PRINTF(("%s: allocated next packet\n", __func__)); | |
3077 | ||
3078 | if (!m_append(m, sizeof(struct igmp_grouprec), (void *)&ig)) { | |
3079 | if (m != m0) | |
3080 | m_freem(m); | |
3081 | IGMP_PRINTF(("%s: m_append() failed.\n", __func__)); | |
3082 | return (-ENOMEM); | |
3083 | } | |
3084 | m->m_pkthdr.vt_nrecs = 1; | |
3085 | nbytes += sizeof(struct igmp_grouprec); | |
3086 | ||
3087 | m0srcs = (ifp->if_mtu - IGMP_LEADINGSPACE - | |
3088 | sizeof(struct igmp_grouprec)) / sizeof(in_addr_t); | |
3089 | ||
3090 | msrcs = 0; | |
3091 | RB_FOREACH_FROM(ims, ip_msource_tree, nims) { | |
3092 | IGMP_PRINTF(("%s: visit node %s\n", __func__, | |
3093 | inet_ntoa_haddr(ims->ims_haddr))); | |
3094 | now = ims_get_mode(inm, ims, 1); | |
3095 | if ((now != mode) || | |
3096 | (now == mode && mode == MCAST_UNDEFINED)) { | |
3097 | IGMP_PRINTF(("%s: skip node\n", __func__)); | |
3098 | continue; | |
3099 | } | |
3100 | if (is_source_query && ims->ims_stp == 0) { | |
3101 | IGMP_PRINTF(("%s: skip unrecorded node\n", | |
3102 | __func__)); | |
3103 | continue; | |
3104 | } | |
3105 | IGMP_PRINTF(("%s: append node\n", __func__)); | |
3106 | naddr = htonl(ims->ims_haddr); | |
3107 | if (!m_append(m, sizeof(in_addr_t), (void *)&naddr)) { | |
3108 | if (m != m0) | |
3109 | m_freem(m); | |
3110 | IGMP_PRINTF(("%s: m_append() failed.\n", | |
3111 | __func__)); | |
3112 | return (-ENOMEM); | |
3113 | } | |
3114 | ++msrcs; | |
3115 | if (msrcs == m0srcs) | |
3116 | break; | |
3117 | } | |
3118 | pig->ig_numsrc = htons(msrcs); | |
3119 | nbytes += (msrcs * sizeof(in_addr_t)); | |
3120 | ||
3121 | IGMP_PRINTF(("%s: enqueueing next packet\n", __func__)); | |
3122 | m->m_pkthdr.rcvif = ifp; | |
3123 | IF_ENQUEUE(ifq, m); | |
3124 | } | |
3125 | ||
3126 | return (nbytes); | |
3127 | } | |
3128 | ||
3129 | /* | |
3130 | * Type used to mark record pass completion. | |
3131 | * We exploit the fact we can cast to this easily from the | |
3132 | * current filter modes on each ip_msource node. | |
3133 | */ | |
3134 | typedef enum { | |
3135 | REC_NONE = 0x00, /* MCAST_UNDEFINED */ | |
3136 | REC_ALLOW = 0x01, /* MCAST_INCLUDE */ | |
3137 | REC_BLOCK = 0x02, /* MCAST_EXCLUDE */ | |
3138 | REC_FULL = REC_ALLOW | REC_BLOCK | |
3139 | } rectype_t; | |
3140 | ||
3141 | /* | |
3142 | * Enqueue an IGMPv3 filter list change to the given output queue. | |
3143 | * | |
3144 | * Source list filter state is held in an RB-tree. When the filter list | |
3145 | * for a group is changed without changing its mode, we need to compute | |
3146 | * the deltas between T0 and T1 for each source in the filter set, | |
3147 | * and enqueue the appropriate ALLOW_NEW/BLOCK_OLD records. | |
3148 | * | |
3149 | * As we may potentially queue two record types, and the entire R-B tree | |
3150 | * needs to be walked at once, we break this out into its own function | |
3151 | * so we can generate a tightly packed queue of packets. | |
3152 | * | |
3153 | * XXX This could be written to only use one tree walk, although that makes | |
3154 | * serializing into the mbuf chains a bit harder. For now we do two walks | |
3155 | * which makes things easier on us, and it may or may not be harder on | |
3156 | * the L2 cache. | |
3157 | * | |
3158 | * If successful the size of all data appended to the queue is returned, | |
3159 | * otherwise an error code less than zero is returned, or zero if | |
3160 | * no record(s) were appended. | |
3161 | */ | |
3162 | static int | |
3163 | igmp_v3_enqueue_filter_change(struct ifqueue *ifq, struct in_multi *inm) | |
3164 | { | |
3165 | static const int MINRECLEN = | |
3166 | sizeof(struct igmp_grouprec) + sizeof(in_addr_t); | |
3167 | struct ifnet *ifp; | |
3168 | struct igmp_grouprec ig; | |
3169 | struct igmp_grouprec *pig; | |
3170 | struct ip_msource *ims, *nims; | |
3171 | struct mbuf *m, *m0, *md; | |
3172 | in_addr_t naddr; | |
3173 | int m0srcs, nbytes, npbytes, off, rsrcs, schanged; | |
3174 | int nallow, nblock; | |
3175 | uint8_t mode, now, then; | |
3176 | rectype_t crt, drt, nrt; | |
3177 | ||
3178 | INM_LOCK_ASSERT_HELD(inm); | |
3179 | ||
3180 | if (inm->inm_nsrc == 0 || | |
3181 | (inm->inm_st[0].iss_asm > 0 && inm->inm_st[1].iss_asm > 0)) | |
3182 | return (0); | |
3183 | ||
3184 | ifp = inm->inm_ifp; /* interface */ | |
3185 | mode = inm->inm_st[1].iss_fmode; /* filter mode at t1 */ | |
3186 | crt = REC_NONE; /* current group record type */ | |
3187 | drt = REC_NONE; /* mask of completed group record types */ | |
3188 | nrt = REC_NONE; /* record type for current node */ | |
3189 | m0srcs = 0; /* # source which will fit in current mbuf chain */ | |
3190 | nbytes = 0; /* # of bytes appended to group's state-change queue */ | |
3191 | npbytes = 0; /* # of bytes appended this packet */ | |
3192 | rsrcs = 0; /* # sources encoded in current record */ | |
3193 | schanged = 0; /* # nodes encoded in overall filter change */ | |
3194 | nallow = 0; /* # of source entries in ALLOW_NEW */ | |
3195 | nblock = 0; /* # of source entries in BLOCK_OLD */ | |
3196 | nims = NULL; /* next tree node pointer */ | |
3197 | ||
3198 | /* | |
3199 | * For each possible filter record mode. | |
3200 | * The first kind of source we encounter tells us which | |
3201 | * is the first kind of record we start appending. | |
3202 | * If a node transitioned to UNDEFINED at t1, its mode is treated | |
3203 | * as the inverse of the group's filter mode. | |
3204 | */ | |
3205 | while (drt != REC_FULL) { | |
3206 | do { | |
3207 | m0 = ifq->ifq_tail; | |
3208 | if (m0 != NULL && | |
3209 | (m0->m_pkthdr.vt_nrecs + 1 <= | |
3210 | IGMP_V3_REPORT_MAXRECS) && | |
3211 | (m0->m_pkthdr.len + MINRECLEN) < | |
3212 | (ifp->if_mtu - IGMP_LEADINGSPACE)) { | |
3213 | m = m0; | |
3214 | m0srcs = (ifp->if_mtu - m0->m_pkthdr.len - | |
3215 | sizeof(struct igmp_grouprec)) / | |
3216 | sizeof(in_addr_t); | |
3217 | IGMP_PRINTF(("%s: use previous packet\n", | |
3218 | __func__)); | |
3219 | } else { | |
3220 | m = m_getcl(M_DONTWAIT, MT_DATA, M_PKTHDR); | |
3221 | if (m) | |
3222 | m->m_data += IGMP_LEADINGSPACE; | |
3223 | if (m == NULL) { | |
3224 | m = m_gethdr(M_DONTWAIT, MT_DATA); | |
3225 | if (m) | |
3226 | MH_ALIGN(m, IGMP_LEADINGSPACE); | |
3227 | } | |
3228 | if (m == NULL) { | |
3229 | IGMP_PRINTF(("%s: m_get*() failed\n", | |
3230 | __func__)); | |
3231 | return (-ENOMEM); | |
3232 | } | |
3233 | m->m_pkthdr.vt_nrecs = 0; | |
3234 | m0srcs = (ifp->if_mtu - IGMP_LEADINGSPACE - | |
3235 | sizeof(struct igmp_grouprec)) / | |
3236 | sizeof(in_addr_t); | |
3237 | npbytes = 0; | |
3238 | IGMP_PRINTF(("%s: allocated new packet\n", | |
3239 | __func__)); | |
3240 | } | |
3241 | /* | |
3242 | * Append the IGMP group record header to the | |
3243 | * current packet's data area. | |
3244 | * Recalculate pointer to free space for next | |
3245 | * group record, in case m_append() allocated | |
3246 | * a new mbuf or cluster. | |
3247 | */ | |
3248 | memset(&ig, 0, sizeof(ig)); | |
3249 | ig.ig_group = inm->inm_addr; | |
3250 | if (!m_append(m, sizeof(ig), (void *)&ig)) { | |
3251 | if (m != m0) | |
3252 | m_freem(m); | |
3253 | IGMP_PRINTF(("%s: m_append() failed\n", | |
3254 | __func__)); | |
3255 | return (-ENOMEM); | |
3256 | } | |
3257 | npbytes += sizeof(struct igmp_grouprec); | |
3258 | if (m != m0) { | |
3259 | /* new packet; offset in c hain */ | |
3260 | md = m_getptr(m, npbytes - | |
3261 | sizeof(struct igmp_grouprec), &off); | |
3262 | pig = (struct igmp_grouprec *)(mtod(md, | |
3263 | uint8_t *) + off); | |
3264 | } else { | |
3265 | /* current packet; offset from last append */ | |
3266 | md = m_last(m); | |
3267 | pig = (struct igmp_grouprec *)(mtod(md, | |
3268 | uint8_t *) + md->m_len - | |
3269 | sizeof(struct igmp_grouprec)); | |
3270 | } | |
3271 | /* | |
3272 | * Begin walking the tree for this record type | |
3273 | * pass, or continue from where we left off | |
3274 | * previously if we had to allocate a new packet. | |
3275 | * Only report deltas in-mode at t1. | |
3276 | * We need not report included sources as allowed | |
3277 | * if we are in inclusive mode on the group, | |
3278 | * however the converse is not true. | |
3279 | */ | |
3280 | rsrcs = 0; | |
3281 | if (nims == NULL) | |
3282 | nims = RB_MIN(ip_msource_tree, &inm->inm_srcs); | |
3283 | RB_FOREACH_FROM(ims, ip_msource_tree, nims) { | |
3284 | IGMP_PRINTF(("%s: visit node %s\n", | |
3285 | __func__, inet_ntoa_haddr(ims->ims_haddr))); | |
3286 | now = ims_get_mode(inm, ims, 1); | |
3287 | then = ims_get_mode(inm, ims, 0); | |
3288 | IGMP_PRINTF(("%s: mode: t0 %d, t1 %d\n", | |
3289 | __func__, then, now)); | |
3290 | if (now == then) { | |
3291 | IGMP_PRINTF(("%s: skip unchanged\n", | |
3292 | __func__)); | |
3293 | continue; | |
3294 | } | |
3295 | if (mode == MCAST_EXCLUDE && | |
3296 | now == MCAST_INCLUDE) { | |
3297 | IGMP_PRINTF(("%s: skip IN src on EX " | |
3298 | "group\n", __func__)); | |
3299 | continue; | |
3300 | } | |
3301 | nrt = (rectype_t)now; | |
3302 | if (nrt == REC_NONE) | |
3303 | nrt = (rectype_t)(~mode & REC_FULL); | |
3304 | if (schanged++ == 0) { | |
3305 | crt = nrt; | |
3306 | } else if (crt != nrt) | |
3307 | continue; | |
3308 | naddr = htonl(ims->ims_haddr); | |
3309 | if (!m_append(m, sizeof(in_addr_t), | |
3310 | (void *)&naddr)) { | |
3311 | if (m != m0) | |
3312 | m_freem(m); | |
3313 | IGMP_PRINTF(("%s: m_append() failed\n", | |
3314 | __func__)); | |
3315 | return (-ENOMEM); | |
3316 | } | |
3317 | nallow += !!(crt == REC_ALLOW); | |
3318 | nblock += !!(crt == REC_BLOCK); | |
3319 | if (++rsrcs == m0srcs) | |
3320 | break; | |
3321 | } | |
3322 | /* | |
3323 | * If we did not append any tree nodes on this | |
3324 | * pass, back out of allocations. | |
3325 | */ | |
3326 | if (rsrcs == 0) { | |
3327 | npbytes -= sizeof(struct igmp_grouprec); | |
3328 | if (m != m0) { | |
3329 | IGMP_PRINTF(("%s: m_free(m)\n", | |
3330 | __func__)); | |
3331 | m_freem(m); | |
3332 | } else { | |
3333 | IGMP_PRINTF(("%s: m_adj(m, -ig)\n", | |
3334 | __func__)); | |
3335 | m_adj(m, -((int)sizeof( | |
3336 | struct igmp_grouprec))); | |
3337 | } | |
3338 | continue; | |
3339 | } | |
3340 | npbytes += (rsrcs * sizeof(in_addr_t)); | |
3341 | if (crt == REC_ALLOW) | |
3342 | pig->ig_type = IGMP_ALLOW_NEW_SOURCES; | |
3343 | else if (crt == REC_BLOCK) | |
3344 | pig->ig_type = IGMP_BLOCK_OLD_SOURCES; | |
3345 | pig->ig_numsrc = htons(rsrcs); | |
3346 | /* | |
3347 | * Count the new group record, and enqueue this | |
3348 | * packet if it wasn't already queued. | |
3349 | */ | |
3350 | m->m_pkthdr.vt_nrecs++; | |
3351 | m->m_pkthdr.rcvif = ifp; | |
3352 | if (m != m0) | |
3353 | IF_ENQUEUE(ifq, m); | |
3354 | nbytes += npbytes; | |
3355 | } while (nims != NULL); | |
3356 | drt |= crt; | |
3357 | crt = (~crt & REC_FULL); | |
3358 | } | |
3359 | ||
3360 | IGMP_PRINTF(("%s: queued %d ALLOW_NEW, %d BLOCK_OLD\n", __func__, | |
3361 | nallow, nblock)); | |
3362 | ||
3363 | return (nbytes); | |
3364 | } | |
3365 | ||
3366 | static int | |
3367 | igmp_v3_merge_state_changes(struct in_multi *inm, struct ifqueue *ifscq) | |
3368 | { | |
3369 | struct ifqueue *gq; | |
3370 | struct mbuf *m; /* pending state-change */ | |
3371 | struct mbuf *m0; /* copy of pending state-change */ | |
3372 | struct mbuf *mt; /* last state-change in packet */ | |
3373 | struct mbuf *n; | |
3374 | int docopy, domerge; | |
3375 | u_int recslen; | |
3376 | ||
3377 | INM_LOCK_ASSERT_HELD(inm); | |
3378 | ||
3379 | docopy = 0; | |
3380 | domerge = 0; | |
3381 | recslen = 0; | |
3382 | ||
3383 | /* | |
3384 | * If there are further pending retransmissions, make a writable | |
3385 | * copy of each queued state-change message before merging. | |
3386 | */ | |
3387 | if (inm->inm_scrv > 0) | |
3388 | docopy = 1; | |
3389 | ||
3390 | gq = &inm->inm_scq; | |
3391 | #ifdef IGMP_DEBUG | |
3392 | if (gq->ifq_head == NULL) { | |
3393 | IGMP_PRINTF(("%s: WARNING: queue for inm %p is empty\n", | |
3394 | __func__, inm)); | |
3395 | } | |
3396 | #endif | |
3397 | ||
3398 | /* | |
3399 | * Use IF_REMQUEUE() instead of IF_DEQUEUE() below, since the | |
3400 | * packet might not always be at the head of the ifqueue. | |
3401 | */ | |
3402 | m = gq->ifq_head; | |
3403 | while (m != NULL) { | |
3404 | /* | |
3405 | * Only merge the report into the current packet if | |
3406 | * there is sufficient space to do so; an IGMPv3 report | |
3407 | * packet may only contain 65,535 group records. | |
3408 | * Always use a simple mbuf chain concatentation to do this, | |
3409 | * as large state changes for single groups may have | |
3410 | * allocated clusters. | |
3411 | */ | |
3412 | domerge = 0; | |
3413 | mt = ifscq->ifq_tail; | |
3414 | if (mt != NULL) { | |
3415 | recslen = m_length(m); | |
3416 | ||
3417 | if ((mt->m_pkthdr.vt_nrecs + | |
3418 | m->m_pkthdr.vt_nrecs <= | |
3419 | IGMP_V3_REPORT_MAXRECS) && | |
3420 | (mt->m_pkthdr.len + recslen <= | |
3421 | (inm->inm_ifp->if_mtu - IGMP_LEADINGSPACE))) | |
3422 | domerge = 1; | |
3423 | } | |
3424 | ||
3425 | if (!domerge && IF_QFULL(gq)) { | |
3426 | IGMP_PRINTF(("%s: outbound queue full, skipping whole " | |
3427 | "packet %p\n", __func__, m)); | |
3428 | n = m->m_nextpkt; | |
3429 | if (!docopy) { | |
3430 | IF_REMQUEUE(gq, m); | |
3431 | m_freem(m); | |
3432 | } | |
3433 | m = n; | |
3434 | continue; | |
3435 | } | |
3436 | ||
3437 | if (!docopy) { | |
3438 | IGMP_PRINTF(("%s: dequeueing %p\n", __func__, m)); | |
3439 | n = m->m_nextpkt; | |
3440 | IF_REMQUEUE(gq, m); | |
3441 | m0 = m; | |
3442 | m = n; | |
3443 | } else { | |
3444 | IGMP_PRINTF(("%s: copying %p\n", __func__, m)); | |
3445 | m0 = m_dup(m, M_NOWAIT); | |
3446 | if (m0 == NULL) | |
3447 | return (ENOMEM); | |
3448 | m0->m_nextpkt = NULL; | |
3449 | m = m->m_nextpkt; | |
3450 | } | |
3451 | ||
3452 | if (!domerge) { | |
3453 | IGMP_PRINTF(("%s: queueing %p to ifscq %p)\n", | |
3454 | __func__, m0, ifscq)); | |
3455 | m0->m_pkthdr.rcvif = inm->inm_ifp; | |
3456 | IF_ENQUEUE(ifscq, m0); | |
3457 | } else { | |
3458 | struct mbuf *mtl; /* last mbuf of packet mt */ | |
3459 | ||
3460 | IGMP_PRINTF(("%s: merging %p with ifscq tail %p)\n", | |
3461 | __func__, m0, mt)); | |
3462 | ||
3463 | mtl = m_last(mt); | |
3464 | m0->m_flags &= ~M_PKTHDR; | |
3465 | mt->m_pkthdr.len += recslen; | |
3466 | mt->m_pkthdr.vt_nrecs += | |
3467 | m0->m_pkthdr.vt_nrecs; | |
3468 | ||
3469 | mtl->m_next = m0; | |
3470 | } | |
3471 | } | |
3472 | ||
3473 | return (0); | |
3474 | } | |
3475 | ||
3476 | /* | |
3477 | * Respond to a pending IGMPv3 General Query. | |
3478 | */ | |
3479 | static void | |
3480 | igmp_v3_dispatch_general_query(struct igmp_ifinfo *igi) | |
3481 | { | |
3482 | struct ifnet *ifp; | |
3483 | struct in_multi *inm; | |
3484 | struct in_multistep step; | |
3485 | int retval, loop; | |
3486 | ||
3487 | IGI_LOCK_ASSERT_HELD(igi); | |
3488 | ||
3489 | VERIFY(igi->igi_version == IGMP_VERSION_3); | |
3490 | ||
3491 | ifp = igi->igi_ifp; | |
3492 | IGI_UNLOCK(igi); | |
3493 | ||
3494 | in_multihead_lock_shared(); | |
3495 | IN_FIRST_MULTI(step, inm); | |
3496 | while (inm != NULL) { | |
3497 | INM_LOCK(inm); | |
3498 | if (inm->inm_ifp != ifp) | |
3499 | goto next; | |
3500 | ||
3501 | switch (inm->inm_state) { | |
3502 | case IGMP_NOT_MEMBER: | |
3503 | case IGMP_SILENT_MEMBER: | |
3504 | break; | |
3505 | case IGMP_REPORTING_MEMBER: | |
3506 | case IGMP_IDLE_MEMBER: | |
3507 | case IGMP_LAZY_MEMBER: | |
3508 | case IGMP_SLEEPING_MEMBER: | |
3509 | case IGMP_AWAKENING_MEMBER: | |
3510 | inm->inm_state = IGMP_REPORTING_MEMBER; | |
3511 | IGI_LOCK(igi); | |
3512 | retval = igmp_v3_enqueue_group_record(&igi->igi_gq, | |
3513 | inm, 0, 0, 0); | |
3514 | IGI_UNLOCK(igi); | |
3515 | IGMP_PRINTF(("%s: enqueue record = %d\n", | |
3516 | __func__, retval)); | |
3517 | break; | |
3518 | case IGMP_G_QUERY_PENDING_MEMBER: | |
3519 | case IGMP_SG_QUERY_PENDING_MEMBER: | |
3520 | case IGMP_LEAVING_MEMBER: | |
3521 | break; | |
3522 | } | |
3523 | next: | |
3524 | INM_UNLOCK(inm); | |
3525 | IN_NEXT_MULTI(step, inm); | |
3526 | } | |
3527 | in_multihead_lock_done(); | |
3528 | ||
3529 | IGI_LOCK(igi); | |
3530 | loop = (igi->igi_flags & IGIF_LOOPBACK) ? 1 : 0; | |
3531 | igmp_dispatch_queue(igi, &igi->igi_gq, IGMP_MAX_RESPONSE_BURST, | |
3532 | loop, ifp); | |
3533 | IGI_LOCK_ASSERT_HELD(igi); | |
3534 | /* | |
3535 | * Slew transmission of bursts over 500ms intervals. | |
3536 | */ | |
3537 | if (igi->igi_gq.ifq_head != NULL) { | |
3538 | igi->igi_v3_timer = 1 + IGMP_RANDOM_DELAY( | |
3539 | IGMP_RESPONSE_BURST_INTERVAL); | |
3540 | interface_timers_running = 1; | |
3541 | } | |
3542 | } | |
3543 | ||
3544 | /* | |
3545 | * Transmit the next pending IGMP message in the output queue. | |
3546 | * | |
3547 | * Must not be called with inm_lock or igi_lock held. | |
3548 | */ | |
3549 | void | |
3550 | igmp_sendpkt(struct mbuf *m, struct ifnet *ifp) | |
3551 | { | |
3552 | struct ip_moptions *imo; | |
3553 | struct mbuf *ipopts, *m0; | |
3554 | int error; | |
3555 | struct route ro; | |
3556 | ||
3557 | IGMP_PRINTF(("%s: transmit %p\n", __func__, m)); | |
3558 | ||
3559 | /* | |
3560 | * Check if the ifnet is still attached. | |
3561 | */ | |
3562 | if (ifp == NULL || !ifnet_is_attached(ifp, 0)) { | |
3563 | IGMP_PRINTF(("%s: dropped %p as ifp u went away.\n", | |
3564 | __func__, m)); | |
3565 | m_freem(m); | |
3566 | OSAddAtomic(1, &ipstat.ips_noroute); | |
3567 | return; | |
3568 | } | |
3569 | ||
3570 | ipopts = igmp_sendra ? m_raopt : NULL; | |
3571 | ||
3572 | imo = ip_allocmoptions(M_WAITOK); | |
3573 | if (imo == NULL) { | |
3574 | m_freem(m); | |
3575 | return; | |
3576 | } | |
3577 | ||
3578 | imo->imo_multicast_ttl = 1; | |
3579 | imo->imo_multicast_vif = -1; | |
3580 | #if MROUTING | |
3581 | imo->imo_multicast_loop = (ip_mrouter != NULL); | |
3582 | #else | |
3583 | imo->imo_multicast_loop = 0; | |
3584 | #endif | |
3585 | ||
3586 | /* | |
3587 | * If the user requested that IGMP traffic be explicitly | |
3588 | * redirected to the loopback interface (e.g. they are running a | |
3589 | * MANET interface and the routing protocol needs to see the | |
3590 | * updates), handle this now. | |
3591 | */ | |
3592 | if (m->m_flags & M_IGMP_LOOP) | |
3593 | imo->imo_multicast_ifp = lo_ifp; | |
3594 | else | |
3595 | imo->imo_multicast_ifp = ifp; | |
3596 | ||
3597 | if (m->m_flags & M_IGMPV2) { | |
3598 | m0 = m; | |
3599 | } else { | |
3600 | m0 = igmp_v3_encap_report(ifp, m); | |
3601 | if (m0 == NULL) { | |
3602 | /* | |
3603 | * If igmp_v3_encap_report() failed, then M_PREPEND() | |
3604 | * already freed the original mbuf chain. | |
3605 | * This means that we don't have to m_freem(m) here. | |
3606 | */ | |
3607 | IGMP_PRINTF(("%s: dropped %p\n", __func__, m)); | |
3608 | IMO_REMREF(imo); | |
3609 | atomic_add_32(&ipstat.ips_odropped, 1); | |
3610 | return; | |
3611 | } | |
3612 | } | |
3613 | ||
3614 | m->m_flags &= ~(M_PROTOFLAGS | M_IGMP_LOOP); | |
3615 | m0->m_pkthdr.rcvif = lo_ifp; | |
3616 | #ifdef MAC | |
3617 | mac_netinet_igmp_send(ifp, m0); | |
3618 | #endif | |
3619 | bzero(&ro, sizeof (ro)); | |
3620 | error = ip_output(m0, ipopts, &ro, 0, imo, NULL); | |
3621 | if (ro.ro_rt != NULL) { | |
3622 | rtfree(ro.ro_rt); | |
3623 | ro.ro_rt = NULL; | |
3624 | } | |
3625 | ||
3626 | IMO_REMREF(imo); | |
3627 | ||
3628 | if (error) { | |
3629 | IGMP_PRINTF(("%s: ip_output(%p) = %d\n", __func__, m0, error)); | |
3630 | return; | |
3631 | } | |
3632 | ||
3633 | IGMPSTAT_INC(igps_snd_reports); | |
3634 | OIGMPSTAT_INC(igps_snd_reports); | |
3635 | } | |
3636 | /* | |
3637 | * Encapsulate an IGMPv3 report. | |
3638 | * | |
3639 | * The internal mbuf flag M_IGMPV3_HDR is used to indicate that the mbuf | |
3640 | * chain has already had its IP/IGMPv3 header prepended. In this case | |
3641 | * the function will not attempt to prepend; the lengths and checksums | |
3642 | * will however be re-computed. | |
3643 | * | |
3644 | * Returns a pointer to the new mbuf chain head, or NULL if the | |
3645 | * allocation failed. | |
3646 | */ | |
3647 | static struct mbuf * | |
3648 | igmp_v3_encap_report(struct ifnet *ifp, struct mbuf *m) | |
3649 | { | |
3650 | struct igmp_report *igmp; | |
3651 | struct ip *ip; | |
3652 | int hdrlen, igmpreclen; | |
3653 | ||
3654 | VERIFY((m->m_flags & M_PKTHDR)); | |
3655 | ||
3656 | igmpreclen = m_length(m); | |
3657 | hdrlen = sizeof(struct ip) + sizeof(struct igmp_report); | |
3658 | ||
3659 | if (m->m_flags & M_IGMPV3_HDR) { | |
3660 | igmpreclen -= hdrlen; | |
3661 | } else { | |
3662 | M_PREPEND(m, hdrlen, M_DONTWAIT); | |
3663 | if (m == NULL) | |
3664 | return (NULL); | |
3665 | m->m_flags |= M_IGMPV3_HDR; | |
3666 | } | |
3667 | ||
3668 | IGMP_PRINTF(("%s: igmpreclen is %d\n", __func__, igmpreclen)); | |
3669 | ||
3670 | m->m_data += sizeof(struct ip); | |
3671 | m->m_len -= sizeof(struct ip); | |
3672 | ||
3673 | igmp = mtod(m, struct igmp_report *); | |
3674 | igmp->ir_type = IGMP_v3_HOST_MEMBERSHIP_REPORT; | |
3675 | igmp->ir_rsv1 = 0; | |
3676 | igmp->ir_rsv2 = 0; | |
3677 | igmp->ir_numgrps = htons(m->m_pkthdr.vt_nrecs); | |
3678 | igmp->ir_cksum = 0; | |
3679 | igmp->ir_cksum = in_cksum(m, sizeof(struct igmp_report) + igmpreclen); | |
3680 | m->m_pkthdr.vt_nrecs = 0; | |
3681 | ||
3682 | m->m_data -= sizeof(struct ip); | |
3683 | m->m_len += sizeof(struct ip); | |
3684 | ||
3685 | ip = mtod(m, struct ip *); | |
3686 | ip->ip_tos = IPTOS_PREC_INTERNETCONTROL; | |
3687 | ip->ip_len = hdrlen + igmpreclen; | |
3688 | ip->ip_off = IP_DF; | |
3689 | ip->ip_p = IPPROTO_IGMP; | |
3690 | ip->ip_sum = 0; | |
3691 | ||
3692 | ip->ip_src.s_addr = INADDR_ANY; | |
3693 | ||
3694 | if (m->m_flags & M_IGMP_LOOP) { | |
3695 | struct in_ifaddr *ia; | |
3696 | ||
3697 | IFP_TO_IA(ifp, ia); | |
3698 | if (ia != NULL) { | |
3699 | IFA_LOCK(&ia->ia_ifa); | |
3700 | ip->ip_src = ia->ia_addr.sin_addr; | |
3701 | IFA_UNLOCK(&ia->ia_ifa); | |
3702 | IFA_REMREF(&ia->ia_ifa); | |
3703 | } | |
3704 | } | |
3705 | ||
3706 | ip->ip_dst.s_addr = htonl(INADDR_ALLRPTS_GROUP); | |
3707 | ||
3708 | return (m); | |
3709 | } | |
3710 | ||
3711 | #ifdef IGMP_DEBUG | |
3712 | static const char * | |
3713 | igmp_rec_type_to_str(const int type) | |
3714 | { | |
3715 | switch (type) { | |
3716 | case IGMP_CHANGE_TO_EXCLUDE_MODE: | |
3717 | return "TO_EX"; | |
3718 | break; | |
3719 | case IGMP_CHANGE_TO_INCLUDE_MODE: | |
3720 | return "TO_IN"; | |
3721 | break; | |
3722 | case IGMP_MODE_IS_EXCLUDE: | |
3723 | return "MODE_EX"; | |
3724 | break; | |
3725 | case IGMP_MODE_IS_INCLUDE: | |
3726 | return "MODE_IN"; | |
3727 | break; | |
3728 | case IGMP_ALLOW_NEW_SOURCES: | |
3729 | return "ALLOW_NEW"; | |
3730 | break; | |
3731 | case IGMP_BLOCK_OLD_SOURCES: | |
3732 | return "BLOCK_OLD"; | |
3733 | break; | |
3734 | default: | |
3735 | break; | |
3736 | } | |
3737 | return "unknown"; | |
3738 | } | |
3739 | #endif | |
3740 | ||
3741 | void | |
3742 | igmp_init(void) | |
3743 | { | |
3744 | ||
3745 | IGMP_PRINTF(("%s: initializing\n", __func__)); | |
3746 | ||
3747 | igmp_timers_are_running = 0; | |
3748 | ||
3749 | /* Setup lock group and attribute for igmp_mtx */ | |
3750 | igmp_mtx_grp_attr = lck_grp_attr_alloc_init(); | |
3751 | igmp_mtx_grp = lck_grp_alloc_init("igmp_mtx", igmp_mtx_grp_attr); | |
3752 | igmp_mtx_attr = lck_attr_alloc_init(); | |
3753 | lck_mtx_init(&igmp_mtx, igmp_mtx_grp, igmp_mtx_attr); | |
3754 | ||
3755 | LIST_INIT(&igi_head); | |
3756 | m_raopt = igmp_ra_alloc(); | |
3757 | ||
3758 | igi_size = sizeof (struct igmp_ifinfo); | |
3759 | igi_zone = zinit(igi_size, IGI_ZONE_MAX * igi_size, | |
3760 | 0, IGI_ZONE_NAME); | |
3761 | if (igi_zone == NULL) { | |
3762 | panic("%s: failed allocating %s", __func__, IGI_ZONE_NAME); | |
3763 | /* NOTREACHED */ | |
3764 | } | |
3765 | zone_change(igi_zone, Z_EXPAND, TRUE); | |
3766 | zone_change(igi_zone, Z_CALLERACCT, FALSE); | |
3767 | } |