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