igmp.c revision 1.37 1 /* $NetBSD: igmp.c,v 1.37 2004/04/25 16:42:42 simonb Exp $ */
2
3 /*
4 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the project nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 */
31
32 /*
33 * Internet Group Management Protocol (IGMP) routines.
34 *
35 * Written by Steve Deering, Stanford, May 1988.
36 * Modified by Rosen Sharma, Stanford, Aug 1994.
37 * Modified by Bill Fenner, Xerox PARC, Feb 1995.
38 *
39 * MULTICAST Revision: 1.3
40 */
41
42 #include <sys/cdefs.h>
43 __KERNEL_RCSID(0, "$NetBSD: igmp.c,v 1.37 2004/04/25 16:42:42 simonb Exp $");
44
45 #include "opt_mrouting.h"
46
47 #include <sys/param.h>
48 #include <sys/mbuf.h>
49 #include <sys/socket.h>
50 #include <sys/protosw.h>
51 #include <sys/systm.h>
52
53 #include <net/if.h>
54 #include <net/route.h>
55
56 #include <netinet/in.h>
57 #include <netinet/in_var.h>
58 #include <netinet/in_systm.h>
59 #include <netinet/ip.h>
60 #include <netinet/ip_var.h>
61 #include <netinet/igmp.h>
62 #include <netinet/igmp_var.h>
63
64 #include <machine/stdarg.h>
65
66 #define IP_MULTICASTOPTS 0
67
68 POOL_INIT(igmp_rti_pool, sizeof(struct router_info), 0, 0, 0, "igmppl", NULL);
69 struct igmpstat igmpstat;
70 int igmp_timers_are_running;
71 static LIST_HEAD(, router_info) rti_head = LIST_HEAD_INITIALIZER(rti_head);
72
73 void igmp_sendpkt __P((struct in_multi *, int));
74 static int rti_fill __P((struct in_multi *));
75 static struct router_info *rti_find __P((struct ifnet *));
76 static void rti_delete(struct ifnet *);
77
78 static int
79 rti_fill(inm)
80 struct in_multi *inm;
81 {
82 struct router_info *rti;
83
84 LIST_FOREACH(rti, &rti_head, rti_link) {
85 if (rti->rti_ifp == inm->inm_ifp) {
86 inm->inm_rti = rti;
87 if (rti->rti_type == IGMP_v1_ROUTER)
88 return (IGMP_v1_HOST_MEMBERSHIP_REPORT);
89 else
90 return (IGMP_v2_HOST_MEMBERSHIP_REPORT);
91 }
92 }
93
94 rti = pool_get(&igmp_rti_pool, PR_NOWAIT);
95 if (rti == NULL)
96 return 0;
97 rti->rti_ifp = inm->inm_ifp;
98 rti->rti_type = IGMP_v2_ROUTER;
99 LIST_INSERT_HEAD(&rti_head, rti, rti_link);
100 inm->inm_rti = rti;
101 return (IGMP_v2_HOST_MEMBERSHIP_REPORT);
102 }
103
104 static struct router_info *
105 rti_find(ifp)
106 struct ifnet *ifp;
107 {
108 struct router_info *rti;
109
110 LIST_FOREACH(rti, &rti_head, rti_link) {
111 if (rti->rti_ifp == ifp)
112 return (rti);
113 }
114
115 rti = pool_get(&igmp_rti_pool, PR_NOWAIT);
116 if (rti == NULL)
117 return NULL;
118 rti->rti_ifp = ifp;
119 rti->rti_type = IGMP_v2_ROUTER;
120 LIST_INSERT_HEAD(&rti_head, rti, rti_link);
121 return (rti);
122 }
123
124 static void
125 rti_delete(ifp)
126 struct ifnet *ifp;
127 {
128 struct router_info *rti;
129
130 LIST_FOREACH(rti, &rti_head, rti_link) {
131 if (rti->rti_ifp == ifp) {
132 LIST_REMOVE(rti, rti_link);
133 pool_put(&igmp_rti_pool, rti);
134 return;
135 }
136 }
137 }
138
139 void
140 #if __STDC__
141 igmp_input(struct mbuf *m, ...)
142 #else
143 igmp_input(m, va_alist)
144 struct mbuf *m;
145 va_dcl
146 #endif
147 {
148 int proto;
149 int iphlen;
150 struct ifnet *ifp = m->m_pkthdr.rcvif;
151 struct ip *ip = mtod(m, struct ip *);
152 struct igmp *igmp;
153 u_int minlen;
154 struct in_multi *inm;
155 struct in_multistep step;
156 struct router_info *rti;
157 struct in_ifaddr *ia;
158 u_int timer;
159 va_list ap;
160 u_int16_t ip_len;
161
162 va_start(ap, m);
163 iphlen = va_arg(ap, int);
164 proto = va_arg(ap, int);
165 va_end(ap);
166
167 ++igmpstat.igps_rcv_total;
168
169 /*
170 * Validate lengths
171 */
172 minlen = iphlen + IGMP_MINLEN;
173 ip_len = ntohs(ip->ip_len);
174 if (ip_len < minlen) {
175 ++igmpstat.igps_rcv_tooshort;
176 m_freem(m);
177 return;
178 }
179 if (((m->m_flags & M_EXT) && (ip->ip_src.s_addr & IN_CLASSA_NET) == 0)
180 || m->m_len < minlen) {
181 if ((m = m_pullup(m, minlen)) == 0) {
182 ++igmpstat.igps_rcv_tooshort;
183 return;
184 }
185 ip = mtod(m, struct ip *);
186 }
187
188 /*
189 * Validate checksum
190 */
191 m->m_data += iphlen;
192 m->m_len -= iphlen;
193 igmp = mtod(m, struct igmp *);
194 /* No need to assert alignment here. */
195 if (in_cksum(m, ip_len - iphlen)) {
196 ++igmpstat.igps_rcv_badsum;
197 m_freem(m);
198 return;
199 }
200 m->m_data -= iphlen;
201 m->m_len += iphlen;
202
203 switch (igmp->igmp_type) {
204
205 case IGMP_HOST_MEMBERSHIP_QUERY:
206 ++igmpstat.igps_rcv_queries;
207
208 if (ifp->if_flags & IFF_LOOPBACK)
209 break;
210
211 if (igmp->igmp_code == 0) {
212 rti = rti_find(ifp);
213 if (rti == NULL)
214 break;
215 rti->rti_type = IGMP_v1_ROUTER;
216 rti->rti_age = 0;
217
218 if (ip->ip_dst.s_addr != INADDR_ALLHOSTS_GROUP) {
219 ++igmpstat.igps_rcv_badqueries;
220 m_freem(m);
221 return;
222 }
223
224 /*
225 * Start the timers in all of our membership records
226 * for the interface on which the query arrived,
227 * except those that are already running and those
228 * that belong to a "local" group (224.0.0.X).
229 */
230 IN_FIRST_MULTI(step, inm);
231 while (inm != NULL) {
232 if (inm->inm_ifp == ifp &&
233 inm->inm_timer == 0 &&
234 !IN_LOCAL_GROUP(inm->inm_addr.s_addr)) {
235 inm->inm_state = IGMP_DELAYING_MEMBER;
236 inm->inm_timer = IGMP_RANDOM_DELAY(
237 IGMP_MAX_HOST_REPORT_DELAY * PR_FASTHZ);
238 igmp_timers_are_running = 1;
239 }
240 IN_NEXT_MULTI(step, inm);
241 }
242 } else {
243 if (!IN_MULTICAST(ip->ip_dst.s_addr)) {
244 ++igmpstat.igps_rcv_badqueries;
245 m_freem(m);
246 return;
247 }
248
249 timer = igmp->igmp_code * PR_FASTHZ / IGMP_TIMER_SCALE;
250 if (timer == 0)
251 timer =1;
252
253 /*
254 * Start the timers in all of our membership records
255 * for the interface on which the query arrived,
256 * except those that are already running and those
257 * that belong to a "local" group (224.0.0.X). For
258 * timers already running, check if they need to be
259 * reset.
260 */
261 IN_FIRST_MULTI(step, inm);
262 while (inm != NULL) {
263 if (inm->inm_ifp == ifp &&
264 !IN_LOCAL_GROUP(inm->inm_addr.s_addr) &&
265 (ip->ip_dst.s_addr == INADDR_ALLHOSTS_GROUP ||
266 in_hosteq(ip->ip_dst, inm->inm_addr))) {
267 switch (inm->inm_state) {
268 case IGMP_DELAYING_MEMBER:
269 if (inm->inm_timer <= timer)
270 break;
271 /* FALLTHROUGH */
272 case IGMP_IDLE_MEMBER:
273 case IGMP_LAZY_MEMBER:
274 case IGMP_AWAKENING_MEMBER:
275 inm->inm_state =
276 IGMP_DELAYING_MEMBER;
277 inm->inm_timer =
278 IGMP_RANDOM_DELAY(timer);
279 igmp_timers_are_running = 1;
280 break;
281 case IGMP_SLEEPING_MEMBER:
282 inm->inm_state =
283 IGMP_AWAKENING_MEMBER;
284 break;
285 }
286 }
287 IN_NEXT_MULTI(step, inm);
288 }
289 }
290
291 break;
292
293 case IGMP_v1_HOST_MEMBERSHIP_REPORT:
294 ++igmpstat.igps_rcv_reports;
295
296 if (ifp->if_flags & IFF_LOOPBACK)
297 break;
298
299 if (!IN_MULTICAST(igmp->igmp_group.s_addr) ||
300 !in_hosteq(igmp->igmp_group, ip->ip_dst)) {
301 ++igmpstat.igps_rcv_badreports;
302 m_freem(m);
303 return;
304 }
305
306 /*
307 * KLUDGE: if the IP source address of the report has an
308 * unspecified (i.e., zero) subnet number, as is allowed for
309 * a booting host, replace it with the correct subnet number
310 * so that a process-level multicast routing daemon can
311 * determine which subnet it arrived from. This is necessary
312 * to compensate for the lack of any way for a process to
313 * determine the arrival interface of an incoming packet.
314 */
315 if ((ip->ip_src.s_addr & IN_CLASSA_NET) == 0) {
316 IFP_TO_IA(ifp, ia); /* XXX */
317 if (ia)
318 ip->ip_src.s_addr = ia->ia_subnet;
319 }
320
321 /*
322 * If we belong to the group being reported, stop
323 * our timer for that group.
324 */
325 IN_LOOKUP_MULTI(igmp->igmp_group, ifp, inm);
326 if (inm != NULL) {
327 inm->inm_timer = 0;
328 ++igmpstat.igps_rcv_ourreports;
329
330 switch (inm->inm_state) {
331 case IGMP_IDLE_MEMBER:
332 case IGMP_LAZY_MEMBER:
333 case IGMP_AWAKENING_MEMBER:
334 case IGMP_SLEEPING_MEMBER:
335 inm->inm_state = IGMP_SLEEPING_MEMBER;
336 break;
337 case IGMP_DELAYING_MEMBER:
338 if (inm->inm_rti->rti_type == IGMP_v1_ROUTER)
339 inm->inm_state = IGMP_LAZY_MEMBER;
340 else
341 inm->inm_state = IGMP_SLEEPING_MEMBER;
342 break;
343 }
344 }
345
346 break;
347
348 case IGMP_v2_HOST_MEMBERSHIP_REPORT:
349 #ifdef MROUTING
350 /*
351 * Make sure we don't hear our own membership report. Fast
352 * leave requires knowing that we are the only member of a
353 * group.
354 */
355 IFP_TO_IA(ifp, ia); /* XXX */
356 if (ia && in_hosteq(ip->ip_src, ia->ia_addr.sin_addr))
357 break;
358 #endif
359
360 ++igmpstat.igps_rcv_reports;
361
362 if (ifp->if_flags & IFF_LOOPBACK)
363 break;
364
365 if (!IN_MULTICAST(igmp->igmp_group.s_addr) ||
366 !in_hosteq(igmp->igmp_group, ip->ip_dst)) {
367 ++igmpstat.igps_rcv_badreports;
368 m_freem(m);
369 return;
370 }
371
372 /*
373 * KLUDGE: if the IP source address of the report has an
374 * unspecified (i.e., zero) subnet number, as is allowed for
375 * a booting host, replace it with the correct subnet number
376 * so that a process-level multicast routing daemon can
377 * determine which subnet it arrived from. This is necessary
378 * to compensate for the lack of any way for a process to
379 * determine the arrival interface of an incoming packet.
380 */
381 if ((ip->ip_src.s_addr & IN_CLASSA_NET) == 0) {
382 #ifndef MROUTING
383 IFP_TO_IA(ifp, ia); /* XXX */
384 #endif
385 if (ia)
386 ip->ip_src.s_addr = ia->ia_subnet;
387 }
388
389 /*
390 * If we belong to the group being reported, stop
391 * our timer for that group.
392 */
393 IN_LOOKUP_MULTI(igmp->igmp_group, ifp, inm);
394 if (inm != NULL) {
395 inm->inm_timer = 0;
396 ++igmpstat.igps_rcv_ourreports;
397
398 switch (inm->inm_state) {
399 case IGMP_DELAYING_MEMBER:
400 case IGMP_IDLE_MEMBER:
401 case IGMP_AWAKENING_MEMBER:
402 inm->inm_state = IGMP_LAZY_MEMBER;
403 break;
404 case IGMP_LAZY_MEMBER:
405 case IGMP_SLEEPING_MEMBER:
406 break;
407 }
408 }
409
410 break;
411
412 }
413
414 /*
415 * Pass all valid IGMP packets up to any process(es) listening
416 * on a raw IGMP socket.
417 */
418 rip_input(m, iphlen, proto);
419 return;
420 }
421
422 int
423 igmp_joingroup(inm)
424 struct in_multi *inm;
425 {
426 int report_type;
427 int s = splsoftnet();
428
429 inm->inm_state = IGMP_IDLE_MEMBER;
430
431 if (!IN_LOCAL_GROUP(inm->inm_addr.s_addr) &&
432 (inm->inm_ifp->if_flags & IFF_LOOPBACK) == 0) {
433 report_type = rti_fill(inm);
434 if (report_type == 0)
435 return ENOMEM;
436 igmp_sendpkt(inm, report_type);
437 inm->inm_state = IGMP_DELAYING_MEMBER;
438 inm->inm_timer = IGMP_RANDOM_DELAY(
439 IGMP_MAX_HOST_REPORT_DELAY * PR_FASTHZ);
440 igmp_timers_are_running = 1;
441 } else
442 inm->inm_timer = 0;
443 splx(s);
444 return 0;
445 }
446
447 void
448 igmp_leavegroup(inm)
449 struct in_multi *inm;
450 {
451
452 switch (inm->inm_state) {
453 case IGMP_DELAYING_MEMBER:
454 case IGMP_IDLE_MEMBER:
455 if (!IN_LOCAL_GROUP(inm->inm_addr.s_addr) &&
456 (inm->inm_ifp->if_flags & IFF_LOOPBACK) == 0)
457 if (inm->inm_rti->rti_type != IGMP_v1_ROUTER)
458 igmp_sendpkt(inm, IGMP_HOST_LEAVE_MESSAGE);
459 break;
460 case IGMP_LAZY_MEMBER:
461 case IGMP_AWAKENING_MEMBER:
462 case IGMP_SLEEPING_MEMBER:
463 break;
464 }
465 }
466
467 void
468 igmp_fasttimo()
469 {
470 struct in_multi *inm;
471 struct in_multistep step;
472 int s;
473
474 /*
475 * Quick check to see if any work needs to be done, in order
476 * to minimize the overhead of fasttimo processing.
477 */
478 if (!igmp_timers_are_running)
479 return;
480
481 s = splsoftnet();
482 igmp_timers_are_running = 0;
483 IN_FIRST_MULTI(step, inm);
484 while (inm != NULL) {
485 if (inm->inm_timer == 0) {
486 /* do nothing */
487 } else if (--inm->inm_timer == 0) {
488 if (inm->inm_state == IGMP_DELAYING_MEMBER) {
489 if (inm->inm_rti->rti_type == IGMP_v1_ROUTER)
490 igmp_sendpkt(inm,
491 IGMP_v1_HOST_MEMBERSHIP_REPORT);
492 else
493 igmp_sendpkt(inm,
494 IGMP_v2_HOST_MEMBERSHIP_REPORT);
495 inm->inm_state = IGMP_IDLE_MEMBER;
496 }
497 } else {
498 igmp_timers_are_running = 1;
499 }
500 IN_NEXT_MULTI(step, inm);
501 }
502 splx(s);
503 }
504
505 void
506 igmp_slowtimo()
507 {
508 struct router_info *rti;
509 int s;
510
511 s = splsoftnet();
512 LIST_FOREACH(rti, &rti_head, rti_link) {
513 if (rti->rti_type == IGMP_v1_ROUTER &&
514 ++rti->rti_age >= IGMP_AGE_THRESHOLD) {
515 rti->rti_type = IGMP_v2_ROUTER;
516 }
517 }
518 splx(s);
519 }
520
521 void
522 igmp_sendpkt(inm, type)
523 struct in_multi *inm;
524 int type;
525 {
526 struct mbuf *m;
527 struct igmp *igmp;
528 struct ip *ip;
529 struct ip_moptions imo;
530 #ifdef MROUTING
531 extern struct socket *ip_mrouter;
532 #endif /* MROUTING */
533
534 MGETHDR(m, M_DONTWAIT, MT_HEADER);
535 if (m == NULL)
536 return;
537 /*
538 * Assume max_linkhdr + sizeof(struct ip) + IGMP_MINLEN
539 * is smaller than mbuf size returned by MGETHDR.
540 */
541 m->m_data += max_linkhdr;
542 m->m_len = sizeof(struct ip) + IGMP_MINLEN;
543 m->m_pkthdr.len = sizeof(struct ip) + IGMP_MINLEN;
544
545 ip = mtod(m, struct ip *);
546 ip->ip_tos = 0;
547 ip->ip_len = htons(sizeof(struct ip) + IGMP_MINLEN);
548 ip->ip_off = htons(0);
549 ip->ip_p = IPPROTO_IGMP;
550 ip->ip_src = zeroin_addr;
551 ip->ip_dst = inm->inm_addr;
552
553 m->m_data += sizeof(struct ip);
554 m->m_len -= sizeof(struct ip);
555 igmp = mtod(m, struct igmp *);
556 igmp->igmp_type = type;
557 igmp->igmp_code = 0;
558 igmp->igmp_group = inm->inm_addr;
559 igmp->igmp_cksum = 0;
560 igmp->igmp_cksum = in_cksum(m, IGMP_MINLEN);
561 m->m_data -= sizeof(struct ip);
562 m->m_len += sizeof(struct ip);
563
564 imo.imo_multicast_ifp = inm->inm_ifp;
565 imo.imo_multicast_ttl = 1;
566 #ifdef RSVP_ISI
567 imo.imo_multicast_vif = -1;
568 #endif
569 /*
570 * Request loopback of the report if we are acting as a multicast
571 * router, so that the process-level routing demon can hear it.
572 */
573 #ifdef MROUTING
574 imo.imo_multicast_loop = (ip_mrouter != NULL);
575 #else
576 imo.imo_multicast_loop = 0;
577 #endif /* MROUTING */
578
579 ip_output(m, (struct mbuf *)NULL, (struct route *)NULL,
580 IP_MULTICASTOPTS, &imo, (struct socket *)NULL);
581
582 ++igmpstat.igps_snd_reports;
583 }
584
585 void
586 igmp_purgeif(ifp)
587 struct ifnet *ifp;
588 {
589
590 rti_delete(ifp);
591 }
592