]>
Commit | Line | Data |
---|---|---|
1c79356b A |
1 | /* |
2 | * Copyright (c) 2000 Apple Computer, Inc. All rights reserved. | |
3 | * | |
4 | * @APPLE_LICENSE_HEADER_START@ | |
5 | * | |
ff6e181a A |
6 | * This file contains Original Code and/or Modifications of Original Code |
7 | * as defined in and that are subject to the Apple Public Source License | |
8 | * Version 2.0 (the 'License'). You may not use this file except in | |
9 | * compliance with the License. Please obtain a copy of the License at | |
10 | * http://www.opensource.apple.com/apsl/ and read it before using this | |
11 | * file. | |
1c79356b | 12 | * |
ff6e181a A |
13 | * The Original Code and all software distributed under the License are |
14 | * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER | |
1c79356b A |
15 | * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, |
16 | * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, | |
ff6e181a A |
17 | * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. |
18 | * Please see the License for the specific language governing rights and | |
19 | * limitations under the License. | |
1c79356b A |
20 | * |
21 | * @APPLE_LICENSE_HEADER_END@ | |
22 | */ | |
23 | /* | |
24 | * Copyright (c) 1988 Stephen Deering. | |
25 | * Copyright (c) 1992, 1993 | |
26 | * The Regents of the University of California. All rights reserved. | |
27 | * | |
28 | * This code is derived from software contributed to Berkeley by | |
29 | * Stephen Deering of Stanford University. | |
30 | * | |
31 | * Redistribution and use in source and binary forms, with or without | |
32 | * modification, are permitted provided that the following conditions | |
33 | * are met: | |
34 | * 1. Redistributions of source code must retain the above copyright | |
35 | * notice, this list of conditions and the following disclaimer. | |
36 | * 2. Redistributions in binary form must reproduce the above copyright | |
37 | * notice, this list of conditions and the following disclaimer in the | |
38 | * documentation and/or other materials provided with the distribution. | |
39 | * 3. All advertising materials mentioning features or use of this software | |
40 | * must display the following acknowledgement: | |
41 | * This product includes software developed by the University of | |
42 | * California, Berkeley and its contributors. | |
43 | * 4. Neither the name of the University nor the names of its contributors | |
44 | * may be used to endorse or promote products derived from this software | |
45 | * without specific prior written permission. | |
46 | * | |
47 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | |
48 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
49 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
50 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | |
51 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
52 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | |
53 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
54 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | |
55 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | |
56 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |
57 | * SUCH DAMAGE. | |
58 | * | |
59 | * @(#)igmp.c 8.1 (Berkeley) 7/19/93 | |
60 | */ | |
61 | ||
62 | /* | |
63 | * Internet Group Management Protocol (IGMP) routines. | |
64 | * | |
65 | * Written by Steve Deering, Stanford, May 1988. | |
66 | * Modified by Rosen Sharma, Stanford, Aug 1994. | |
67 | * Modified by Bill Fenner, Xerox PARC, Feb 1995. | |
68 | * Modified to fully comply to IGMPv2 by Bill Fenner, Oct 1995. | |
69 | * | |
70 | * MULTICAST Revision: 3.5.1.4 | |
71 | */ | |
72 | ||
73 | #include <sys/param.h> | |
74 | #include <sys/systm.h> | |
75 | #include <sys/malloc.h> | |
76 | #include <sys/mbuf.h> | |
77 | #include <sys/socket.h> | |
78 | #include <sys/protosw.h> | |
79 | #include <sys/kernel.h> | |
80 | #include <sys/sysctl.h> | |
81 | ||
82 | #include <net/if.h> | |
83 | #include <net/route.h> | |
84 | ||
85 | #include <netinet/in.h> | |
86 | #include <netinet/in_var.h> | |
87 | #include <netinet/in_systm.h> | |
88 | #include <netinet/ip.h> | |
89 | #include <netinet/ip_var.h> | |
90 | #include <netinet/igmp.h> | |
91 | #include <netinet/igmp_var.h> | |
92 | ||
9bccf70c | 93 | #ifndef __APPLE__ |
1c79356b | 94 | static MALLOC_DEFINE(M_IGMP, "igmp", "igmp state"); |
9bccf70c | 95 | #endif |
1c79356b A |
96 | |
97 | static struct router_info * | |
91447636 | 98 | find_rti(struct ifnet *ifp); |
1c79356b A |
99 | |
100 | static struct igmpstat igmpstat; | |
101 | ||
102 | SYSCTL_STRUCT(_net_inet_igmp, IGMPCTL_STATS, stats, CTLFLAG_RD, | |
103 | &igmpstat, igmpstat, ""); | |
104 | ||
105 | static int igmp_timers_are_running; | |
106 | static u_long igmp_all_hosts_group; | |
107 | static u_long igmp_all_rtrs_group; | |
108 | static struct mbuf *router_alert; | |
109 | static struct router_info *Head; | |
110 | ||
91447636 | 111 | static void igmp_sendpkt(struct in_multi *, int, unsigned long); |
1c79356b A |
112 | |
113 | void | |
114 | igmp_init() | |
115 | { | |
116 | struct ipoption *ra; | |
117 | ||
118 | /* | |
119 | * To avoid byte-swapping the same value over and over again. | |
120 | */ | |
121 | igmp_all_hosts_group = htonl(INADDR_ALLHOSTS_GROUP); | |
122 | igmp_all_rtrs_group = htonl(INADDR_ALLRTRS_GROUP); | |
123 | ||
124 | igmp_timers_are_running = 0; | |
125 | ||
126 | /* | |
127 | * Construct a Router Alert option to use in outgoing packets | |
128 | */ | |
129 | MGET(router_alert, M_DONTWAIT, MT_DATA); | |
130 | ra = mtod(router_alert, struct ipoption *); | |
131 | ra->ipopt_dst.s_addr = 0; | |
132 | ra->ipopt_list[0] = IPOPT_RA; /* Router Alert Option */ | |
133 | ra->ipopt_list[1] = 0x04; /* 4 bytes long */ | |
134 | ra->ipopt_list[2] = 0x00; | |
135 | ra->ipopt_list[3] = 0x00; | |
136 | router_alert->m_len = sizeof(ra->ipopt_dst) + ra->ipopt_list[1]; | |
137 | ||
138 | Head = (struct router_info *) 0; | |
139 | } | |
140 | ||
141 | static struct router_info * | |
91447636 A |
142 | find_rti( |
143 | struct ifnet *ifp) | |
1c79356b | 144 | { |
91447636 | 145 | struct router_info *rti = Head; |
55e303ae A |
146 | |
147 | ||
1c79356b A |
148 | #if IGMP_DEBUG |
149 | printf("[igmp.c, _find_rti] --> entering \n"); | |
150 | #endif | |
55e303ae A |
151 | while (rti) { |
152 | if (rti->rti_ifp == ifp) { | |
1c79356b A |
153 | #if IGMP_DEBUG |
154 | printf("[igmp.c, _find_rti] --> found old entry \n"); | |
155 | #endif | |
55e303ae A |
156 | return rti; |
157 | } | |
158 | rti = rti->rti_next; | |
159 | } | |
160 | ||
1c79356b | 161 | MALLOC(rti, struct router_info *, sizeof *rti, M_IGMP, M_NOWAIT); |
55e303ae A |
162 | if (rti != NULL) |
163 | { | |
164 | rti->rti_ifp = ifp; | |
165 | rti->rti_type = IGMP_V2_ROUTER; | |
166 | rti->rti_time = 0; | |
167 | rti->rti_next = Head; | |
168 | Head = rti; | |
169 | } | |
1c79356b | 170 | #if IGMP_DEBUG |
55e303ae | 171 | if (rti) printf("[igmp.c, _find_rti] --> created an entry \n"); |
1c79356b | 172 | #endif |
55e303ae | 173 | return rti; |
1c79356b A |
174 | } |
175 | ||
176 | void | |
91447636 A |
177 | igmp_input( |
178 | struct mbuf *m, | |
179 | int iphlen) | |
1c79356b | 180 | { |
91447636 A |
181 | struct igmp *igmp; |
182 | struct ip *ip; | |
183 | int igmplen; | |
184 | struct ifnet *ifp = m->m_pkthdr.rcvif; | |
185 | int minlen; | |
186 | struct in_multi *inm; | |
187 | struct in_ifaddr *ia; | |
1c79356b A |
188 | struct in_multistep step; |
189 | struct router_info *rti; | |
190 | ||
191 | int timer; /** timer value in the igmp query header **/ | |
192 | ||
193 | ++igmpstat.igps_rcv_total; | |
194 | ||
195 | ip = mtod(m, struct ip *); | |
196 | igmplen = ip->ip_len; | |
197 | ||
198 | /* | |
199 | * Validate lengths | |
200 | */ | |
201 | if (igmplen < IGMP_MINLEN) { | |
202 | ++igmpstat.igps_rcv_tooshort; | |
203 | m_freem(m); | |
204 | return; | |
205 | } | |
206 | minlen = iphlen + IGMP_MINLEN; | |
207 | if ((m->m_flags & M_EXT || m->m_len < minlen) && | |
208 | (m = m_pullup(m, minlen)) == 0) { | |
209 | ++igmpstat.igps_rcv_tooshort; | |
210 | return; | |
211 | } | |
212 | ||
213 | /* | |
214 | * Validate checksum | |
215 | */ | |
216 | m->m_data += iphlen; | |
217 | m->m_len -= iphlen; | |
218 | igmp = mtod(m, struct igmp *); | |
219 | if (in_cksum(m, igmplen)) { | |
220 | ++igmpstat.igps_rcv_badsum; | |
221 | m_freem(m); | |
222 | return; | |
223 | } | |
224 | m->m_data -= iphlen; | |
225 | m->m_len += iphlen; | |
226 | ||
227 | ip = mtod(m, struct ip *); | |
228 | timer = igmp->igmp_code * PR_FASTHZ / IGMP_TIMER_SCALE; | |
229 | if (timer == 0) | |
230 | timer = 1; | |
231 | rti = find_rti(ifp); | |
55e303ae A |
232 | if (rti == NULL) { |
233 | m_freem(m); | |
234 | return; | |
235 | } | |
1c79356b A |
236 | |
237 | /* | |
238 | * In the IGMPv2 specification, there are 3 states and a flag. | |
239 | * | |
240 | * In Non-Member state, we simply don't have a membership record. | |
241 | * In Delaying Member state, our timer is running (inm->inm_timer) | |
242 | * In Idle Member state, our timer is not running (inm->inm_timer==0) | |
243 | * | |
244 | * The flag is inm->inm_state, it is set to IGMP_OTHERMEMBER if | |
245 | * we have heard a report from another member, or IGMP_IREPORTEDLAST | |
246 | * if I sent the last report. | |
247 | */ | |
248 | switch (igmp->igmp_type) { | |
249 | ||
250 | case IGMP_MEMBERSHIP_QUERY: | |
251 | ++igmpstat.igps_rcv_queries; | |
252 | ||
253 | if (ifp->if_flags & IFF_LOOPBACK) | |
254 | break; | |
255 | ||
256 | if (igmp->igmp_code == 0) { | |
257 | /* | |
258 | * Old router. Remember that the querier on this | |
259 | * interface is old, and set the timer to the | |
260 | * value in RFC 1112. | |
261 | */ | |
262 | ||
263 | rti->rti_type = IGMP_V1_ROUTER; | |
264 | rti->rti_time = 0; | |
265 | ||
266 | timer = IGMP_MAX_HOST_REPORT_DELAY * PR_FASTHZ; | |
267 | ||
268 | if (ip->ip_dst.s_addr != igmp_all_hosts_group || | |
269 | igmp->igmp_group.s_addr != 0) { | |
270 | ++igmpstat.igps_rcv_badqueries; | |
271 | m_freem(m); | |
272 | return; | |
273 | } | |
274 | } else { | |
275 | /* | |
276 | * New router. Simply do the new validity check. | |
277 | */ | |
278 | ||
279 | if (igmp->igmp_group.s_addr != 0 && | |
280 | !IN_MULTICAST(ntohl(igmp->igmp_group.s_addr))) { | |
281 | ++igmpstat.igps_rcv_badqueries; | |
282 | m_freem(m); | |
283 | return; | |
284 | } | |
285 | } | |
286 | ||
287 | /* | |
288 | * - Start the timers in all of our membership records | |
289 | * that the query applies to for the interface on | |
290 | * which the query arrived excl. those that belong | |
291 | * to the "all-hosts" group (224.0.0.1). | |
292 | * - Restart any timer that is already running but has | |
293 | * a value longer than the requested timeout. | |
294 | * - Use the value specified in the query message as | |
295 | * the maximum timeout. | |
296 | */ | |
91447636 | 297 | lck_mtx_lock(rt_mtx); |
1c79356b A |
298 | IN_FIRST_MULTI(step, inm); |
299 | while (inm != NULL) { | |
300 | if (inm->inm_ifp == ifp && | |
301 | inm->inm_addr.s_addr != igmp_all_hosts_group && | |
302 | (igmp->igmp_group.s_addr == 0 || | |
303 | igmp->igmp_group.s_addr == inm->inm_addr.s_addr)) { | |
304 | if (inm->inm_timer == 0 || | |
305 | inm->inm_timer > timer) { | |
306 | inm->inm_timer = | |
307 | IGMP_RANDOM_DELAY(timer); | |
308 | igmp_timers_are_running = 1; | |
309 | } | |
310 | } | |
311 | IN_NEXT_MULTI(step, inm); | |
312 | } | |
91447636 | 313 | lck_mtx_unlock(rt_mtx); |
1c79356b A |
314 | |
315 | break; | |
316 | ||
317 | case IGMP_V1_MEMBERSHIP_REPORT: | |
318 | case IGMP_V2_MEMBERSHIP_REPORT: | |
319 | /* | |
320 | * For fast leave to work, we have to know that we are the | |
321 | * last person to send a report for this group. Reports | |
322 | * can potentially get looped back if we are a multicast | |
323 | * router, so discard reports sourced by me. | |
324 | */ | |
325 | IFP_TO_IA(ifp, ia); | |
326 | if (ia && ip->ip_src.s_addr == IA_SIN(ia)->sin_addr.s_addr) | |
327 | break; | |
328 | ||
329 | ++igmpstat.igps_rcv_reports; | |
330 | ||
331 | if (ifp->if_flags & IFF_LOOPBACK) | |
332 | break; | |
333 | ||
334 | if (!IN_MULTICAST(ntohl(igmp->igmp_group.s_addr))) { | |
335 | ++igmpstat.igps_rcv_badreports; | |
336 | m_freem(m); | |
337 | return; | |
338 | } | |
339 | ||
340 | /* | |
341 | * KLUDGE: if the IP source address of the report has an | |
342 | * unspecified (i.e., zero) subnet number, as is allowed for | |
343 | * a booting host, replace it with the correct subnet number | |
344 | * so that a process-level multicast routing demon can | |
345 | * determine which subnet it arrived from. This is necessary | |
346 | * to compensate for the lack of any way for a process to | |
347 | * determine the arrival interface of an incoming packet. | |
348 | */ | |
349 | if ((ntohl(ip->ip_src.s_addr) & IN_CLASSA_NET) == 0) | |
350 | if (ia) ip->ip_src.s_addr = htonl(ia->ia_subnet); | |
351 | ||
352 | /* | |
353 | * If we belong to the group being reported, stop | |
354 | * our timer for that group. | |
355 | */ | |
91447636 | 356 | ifnet_lock_shared(ifp); |
1c79356b | 357 | IN_LOOKUP_MULTI(igmp->igmp_group, ifp, inm); |
91447636 | 358 | ifnet_lock_done(ifp); |
1c79356b A |
359 | |
360 | if (inm != NULL) { | |
361 | inm->inm_timer = 0; | |
362 | ++igmpstat.igps_rcv_ourreports; | |
363 | ||
364 | inm->inm_state = IGMP_OTHERMEMBER; | |
365 | } | |
366 | ||
367 | break; | |
368 | } | |
369 | ||
370 | /* | |
371 | * Pass all valid IGMP packets up to any process(es) listening | |
372 | * on a raw IGMP socket. | |
373 | */ | |
374 | rip_input(m, iphlen); | |
375 | } | |
376 | ||
55e303ae | 377 | int |
1c79356b A |
378 | igmp_joingroup(inm) |
379 | struct in_multi *inm; | |
380 | { | |
1c79356b A |
381 | |
382 | if (inm->inm_addr.s_addr == igmp_all_hosts_group | |
383 | || inm->inm_ifp->if_flags & IFF_LOOPBACK) { | |
384 | inm->inm_timer = 0; | |
385 | inm->inm_state = IGMP_OTHERMEMBER; | |
386 | } else { | |
387 | inm->inm_rti = find_rti(inm->inm_ifp); | |
55e303ae | 388 | if (inm->inm_rti == NULL) return ENOMEM; |
1c79356b A |
389 | igmp_sendpkt(inm, inm->inm_rti->rti_type, 0); |
390 | inm->inm_timer = IGMP_RANDOM_DELAY( | |
391 | IGMP_MAX_HOST_REPORT_DELAY*PR_FASTHZ); | |
392 | inm->inm_state = IGMP_IREPORTEDLAST; | |
393 | igmp_timers_are_running = 1; | |
394 | } | |
55e303ae | 395 | return 0; |
1c79356b A |
396 | } |
397 | ||
398 | void | |
399 | igmp_leavegroup(inm) | |
400 | struct in_multi *inm; | |
401 | { | |
402 | if (inm->inm_state == IGMP_IREPORTEDLAST && | |
403 | inm->inm_addr.s_addr != igmp_all_hosts_group && | |
404 | !(inm->inm_ifp->if_flags & IFF_LOOPBACK) && | |
405 | inm->inm_rti->rti_type != IGMP_V1_ROUTER) | |
406 | igmp_sendpkt(inm, IGMP_V2_LEAVE_GROUP, igmp_all_rtrs_group); | |
407 | } | |
408 | ||
409 | void | |
410 | igmp_fasttimo() | |
411 | { | |
91447636 | 412 | struct in_multi *inm; |
1c79356b | 413 | struct in_multistep step; |
1c79356b A |
414 | |
415 | /* | |
416 | * Quick check to see if any work needs to be done, in order | |
417 | * to minimize the overhead of fasttimo processing. | |
418 | */ | |
419 | ||
420 | if (!igmp_timers_are_running) | |
421 | return; | |
422 | ||
1c79356b A |
423 | igmp_timers_are_running = 0; |
424 | IN_FIRST_MULTI(step, inm); | |
425 | while (inm != NULL) { | |
426 | if (inm->inm_timer == 0) { | |
427 | /* do nothing */ | |
428 | } else if (--inm->inm_timer == 0) { | |
429 | igmp_sendpkt(inm, inm->inm_rti->rti_type, 0); | |
430 | inm->inm_state = IGMP_IREPORTEDLAST; | |
431 | } else { | |
432 | igmp_timers_are_running = 1; | |
433 | } | |
434 | IN_NEXT_MULTI(step, inm); | |
435 | } | |
1c79356b A |
436 | } |
437 | ||
438 | void | |
439 | igmp_slowtimo() | |
440 | { | |
91447636 | 441 | struct router_info *rti = Head; |
1c79356b A |
442 | |
443 | #if IGMP_DEBUG | |
444 | printf("[igmp.c,_slowtimo] -- > entering \n"); | |
445 | #endif | |
446 | while (rti) { | |
447 | if (rti->rti_type == IGMP_V1_ROUTER) { | |
448 | rti->rti_time++; | |
449 | if (rti->rti_time >= IGMP_AGE_THRESHOLD) { | |
450 | rti->rti_type = IGMP_V2_ROUTER; | |
451 | } | |
452 | } | |
453 | rti = rti->rti_next; | |
454 | } | |
455 | #if IGMP_DEBUG | |
456 | printf("[igmp.c,_slowtimo] -- > exiting \n"); | |
457 | #endif | |
1c79356b A |
458 | } |
459 | ||
460 | static struct route igmprt; | |
461 | ||
462 | static void | |
463 | igmp_sendpkt(inm, type, addr) | |
464 | struct in_multi *inm; | |
465 | int type; | |
466 | unsigned long addr; | |
467 | { | |
468 | struct mbuf *m; | |
469 | struct igmp *igmp; | |
470 | struct ip *ip; | |
471 | struct ip_moptions imo; | |
472 | ||
473 | MGETHDR(m, M_DONTWAIT, MT_HEADER); | |
474 | if (m == NULL) | |
475 | return; | |
476 | ||
477 | m->m_pkthdr.rcvif = loif; | |
478 | m->m_pkthdr.len = sizeof(struct ip) + IGMP_MINLEN; | |
479 | MH_ALIGN(m, IGMP_MINLEN + sizeof(struct ip)); | |
480 | m->m_data += sizeof(struct ip); | |
481 | m->m_len = IGMP_MINLEN; | |
0b4e3aa0 A |
482 | m->m_pkthdr.csum_flags = 0; |
483 | m->m_pkthdr.csum_data = 0; | |
1c79356b A |
484 | igmp = mtod(m, struct igmp *); |
485 | igmp->igmp_type = type; | |
486 | igmp->igmp_code = 0; | |
487 | igmp->igmp_group = inm->inm_addr; | |
488 | igmp->igmp_cksum = 0; | |
489 | igmp->igmp_cksum = in_cksum(m, IGMP_MINLEN); | |
490 | ||
491 | m->m_data -= sizeof(struct ip); | |
492 | m->m_len += sizeof(struct ip); | |
493 | ip = mtod(m, struct ip *); | |
494 | ip->ip_tos = 0; | |
495 | ip->ip_len = sizeof(struct ip) + IGMP_MINLEN; | |
496 | ip->ip_off = 0; | |
497 | ip->ip_p = IPPROTO_IGMP; | |
498 | ip->ip_src.s_addr = INADDR_ANY; | |
499 | ip->ip_dst.s_addr = addr ? addr : igmp->igmp_group.s_addr; | |
500 | ||
501 | imo.imo_multicast_ifp = inm->inm_ifp; | |
502 | imo.imo_multicast_ttl = 1; | |
503 | imo.imo_multicast_vif = -1; | |
504 | /* | |
505 | * Request loopback of the report if we are acting as a multicast | |
506 | * router, so that the process-level routing demon can hear it. | |
507 | */ | |
508 | imo.imo_multicast_loop = (ip_mrouter != NULL); | |
509 | ||
510 | /* | |
511 | * XXX | |
512 | * Do we have to worry about reentrancy here? Don't think so. | |
513 | */ | |
1c79356b A |
514 | ip_output(m, router_alert, &igmprt, 0, &imo); |
515 | ||
516 | ++igmpstat.igps_snd_reports; | |
517 | } |