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