]> git.saurik.com Git - apple/xnu.git/blob - bsd/netinet/igmp.c
022ca8c77d7a95136bc852d0edca34b27aee063d
[apple/xnu.git] / bsd / netinet / igmp.c
1 /*
2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
7 *
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
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
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.
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
95 #ifndef __APPLE__
96 static MALLOC_DEFINE(M_IGMP, "igmp", "igmp state");
97 #endif
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
150 #if IGMP_DEBUG
151 printf("[igmp.c, _find_rti] --> entering \n");
152 #endif
153 while (rti) {
154 if (rti->rti_ifp == ifp) {
155 #if IGMP_DEBUG
156 printf("[igmp.c, _find_rti] --> found old entry \n");
157 #endif
158 return rti;
159 }
160 rti = rti->rti_next;
161 }
162
163 MALLOC(rti, struct router_info *, sizeof *rti, M_IGMP, M_NOWAIT);
164 if (rti != NULL)
165 {
166 rti->rti_ifp = ifp;
167 rti->rti_type = IGMP_V2_ROUTER;
168 rti->rti_time = 0;
169 rti->rti_next = Head;
170 Head = rti;
171 }
172 #if IGMP_DEBUG
173 if (rti) printf("[igmp.c, _find_rti] --> created an entry \n");
174 #endif
175 return rti;
176 }
177
178 void
179 igmp_input(m, iphlen)
180 register struct mbuf *m;
181 register int iphlen;
182 {
183 register struct igmp *igmp;
184 register struct ip *ip;
185 register int igmplen;
186 register struct ifnet *ifp = m->m_pkthdr.rcvif;
187 register int minlen;
188 register struct in_multi *inm;
189 register struct in_ifaddr *ia;
190 struct in_multistep step;
191 struct router_info *rti;
192
193 int timer; /** timer value in the igmp query header **/
194
195 ++igmpstat.igps_rcv_total;
196
197 ip = mtod(m, struct ip *);
198 igmplen = ip->ip_len;
199
200 /*
201 * Validate lengths
202 */
203 if (igmplen < IGMP_MINLEN) {
204 ++igmpstat.igps_rcv_tooshort;
205 m_freem(m);
206 return;
207 }
208 minlen = iphlen + IGMP_MINLEN;
209 if ((m->m_flags & M_EXT || m->m_len < minlen) &&
210 (m = m_pullup(m, minlen)) == 0) {
211 ++igmpstat.igps_rcv_tooshort;
212 return;
213 }
214
215 /*
216 * Validate checksum
217 */
218 m->m_data += iphlen;
219 m->m_len -= iphlen;
220 igmp = mtod(m, struct igmp *);
221 if (in_cksum(m, igmplen)) {
222 ++igmpstat.igps_rcv_badsum;
223 m_freem(m);
224 return;
225 }
226 m->m_data -= iphlen;
227 m->m_len += iphlen;
228
229 ip = mtod(m, struct ip *);
230 timer = igmp->igmp_code * PR_FASTHZ / IGMP_TIMER_SCALE;
231 if (timer == 0)
232 timer = 1;
233 rti = find_rti(ifp);
234 if (rti == NULL) {
235 m_freem(m);
236 return;
237 }
238
239 /*
240 * In the IGMPv2 specification, there are 3 states and a flag.
241 *
242 * In Non-Member state, we simply don't have a membership record.
243 * In Delaying Member state, our timer is running (inm->inm_timer)
244 * In Idle Member state, our timer is not running (inm->inm_timer==0)
245 *
246 * The flag is inm->inm_state, it is set to IGMP_OTHERMEMBER if
247 * we have heard a report from another member, or IGMP_IREPORTEDLAST
248 * if I sent the last report.
249 */
250 switch (igmp->igmp_type) {
251
252 case IGMP_MEMBERSHIP_QUERY:
253 ++igmpstat.igps_rcv_queries;
254
255 if (ifp->if_flags & IFF_LOOPBACK)
256 break;
257
258 if (igmp->igmp_code == 0) {
259 /*
260 * Old router. Remember that the querier on this
261 * interface is old, and set the timer to the
262 * value in RFC 1112.
263 */
264
265 rti->rti_type = IGMP_V1_ROUTER;
266 rti->rti_time = 0;
267
268 timer = IGMP_MAX_HOST_REPORT_DELAY * PR_FASTHZ;
269
270 if (ip->ip_dst.s_addr != igmp_all_hosts_group ||
271 igmp->igmp_group.s_addr != 0) {
272 ++igmpstat.igps_rcv_badqueries;
273 m_freem(m);
274 return;
275 }
276 } else {
277 /*
278 * New router. Simply do the new validity check.
279 */
280
281 if (igmp->igmp_group.s_addr != 0 &&
282 !IN_MULTICAST(ntohl(igmp->igmp_group.s_addr))) {
283 ++igmpstat.igps_rcv_badqueries;
284 m_freem(m);
285 return;
286 }
287 }
288
289 /*
290 * - Start the timers in all of our membership records
291 * that the query applies to for the interface on
292 * which the query arrived excl. those that belong
293 * to the "all-hosts" group (224.0.0.1).
294 * - Restart any timer that is already running but has
295 * a value longer than the requested timeout.
296 * - Use the value specified in the query message as
297 * the maximum timeout.
298 */
299 IN_FIRST_MULTI(step, inm);
300 while (inm != NULL) {
301 if (inm->inm_ifp == ifp &&
302 inm->inm_addr.s_addr != igmp_all_hosts_group &&
303 (igmp->igmp_group.s_addr == 0 ||
304 igmp->igmp_group.s_addr == inm->inm_addr.s_addr)) {
305 if (inm->inm_timer == 0 ||
306 inm->inm_timer > timer) {
307 inm->inm_timer =
308 IGMP_RANDOM_DELAY(timer);
309 igmp_timers_are_running = 1;
310 }
311 }
312 IN_NEXT_MULTI(step, inm);
313 }
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 */
356 IN_LOOKUP_MULTI(igmp->igmp_group, ifp, inm);
357
358 if (inm != NULL) {
359 inm->inm_timer = 0;
360 ++igmpstat.igps_rcv_ourreports;
361
362 inm->inm_state = IGMP_OTHERMEMBER;
363 }
364
365 break;
366 }
367
368 /*
369 * Pass all valid IGMP packets up to any process(es) listening
370 * on a raw IGMP socket.
371 */
372 rip_input(m, iphlen);
373 }
374
375 int
376 igmp_joingroup(inm)
377 struct in_multi *inm;
378 {
379 int s = splnet();
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);
387 if (inm->inm_rti == NULL) return ENOMEM;
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 }
394 return 0;
395 splx(s);
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 {
412 register struct in_multi *inm;
413 struct in_multistep step;
414 int s;
415
416 /*
417 * Quick check to see if any work needs to be done, in order
418 * to minimize the overhead of fasttimo processing.
419 */
420
421 if (!igmp_timers_are_running)
422 return;
423
424 s = splnet();
425 igmp_timers_are_running = 0;
426 IN_FIRST_MULTI(step, inm);
427 while (inm != NULL) {
428 if (inm->inm_timer == 0) {
429 /* do nothing */
430 } else if (--inm->inm_timer == 0) {
431 igmp_sendpkt(inm, inm->inm_rti->rti_type, 0);
432 inm->inm_state = IGMP_IREPORTEDLAST;
433 } else {
434 igmp_timers_are_running = 1;
435 }
436 IN_NEXT_MULTI(step, inm);
437 }
438 splx(s);
439 }
440
441 void
442 igmp_slowtimo()
443 {
444 int s = splnet();
445 register struct router_info *rti = Head;
446
447 #if IGMP_DEBUG
448 printf("[igmp.c,_slowtimo] -- > entering \n");
449 #endif
450 while (rti) {
451 if (rti->rti_type == IGMP_V1_ROUTER) {
452 rti->rti_time++;
453 if (rti->rti_time >= IGMP_AGE_THRESHOLD) {
454 rti->rti_type = IGMP_V2_ROUTER;
455 }
456 }
457 rti = rti->rti_next;
458 }
459 #if IGMP_DEBUG
460 printf("[igmp.c,_slowtimo] -- > exiting \n");
461 #endif
462 splx(s);
463 }
464
465 static struct route igmprt;
466
467 static void
468 igmp_sendpkt(inm, type, addr)
469 struct in_multi *inm;
470 int type;
471 unsigned long addr;
472 {
473 struct mbuf *m;
474 struct igmp *igmp;
475 struct ip *ip;
476 struct ip_moptions imo;
477
478 MGETHDR(m, M_DONTWAIT, MT_HEADER);
479 if (m == NULL)
480 return;
481
482 m->m_pkthdr.rcvif = loif;
483 m->m_pkthdr.len = sizeof(struct ip) + IGMP_MINLEN;
484 MH_ALIGN(m, IGMP_MINLEN + sizeof(struct ip));
485 m->m_data += sizeof(struct ip);
486 m->m_len = IGMP_MINLEN;
487 m->m_pkthdr.csum_flags = 0;
488 m->m_pkthdr.csum_data = 0;
489 igmp = mtod(m, struct igmp *);
490 igmp->igmp_type = type;
491 igmp->igmp_code = 0;
492 igmp->igmp_group = inm->inm_addr;
493 igmp->igmp_cksum = 0;
494 igmp->igmp_cksum = in_cksum(m, IGMP_MINLEN);
495
496 m->m_data -= sizeof(struct ip);
497 m->m_len += sizeof(struct ip);
498 ip = mtod(m, struct ip *);
499 ip->ip_tos = 0;
500 ip->ip_len = sizeof(struct ip) + IGMP_MINLEN;
501 ip->ip_off = 0;
502 ip->ip_p = IPPROTO_IGMP;
503 ip->ip_src.s_addr = INADDR_ANY;
504 ip->ip_dst.s_addr = addr ? addr : igmp->igmp_group.s_addr;
505
506 imo.imo_multicast_ifp = inm->inm_ifp;
507 imo.imo_multicast_ttl = 1;
508 imo.imo_multicast_vif = -1;
509 /*
510 * Request loopback of the report if we are acting as a multicast
511 * router, so that the process-level routing demon can hear it.
512 */
513 imo.imo_multicast_loop = (ip_mrouter != NULL);
514
515 /*
516 * XXX
517 * Do we have to worry about reentrancy here? Don't think so.
518 */
519 ip_output(m, router_alert, &igmprt, 0, &imo);
520
521 ++igmpstat.igps_snd_reports;
522 }