igmp.c revision 1.40 1 /* $NetBSD: igmp.c,v 1.40 2005/02/02 21:41:55 perry 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.40 2005/02/02 21:41:55 perry 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(struct in_multi *, int);
74 static int rti_fill(struct in_multi *);
75 static struct router_info *rti_find(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 igmp_input(struct mbuf *m, ...)
141 {
142 int proto;
143 int iphlen;
144 struct ifnet *ifp = m->m_pkthdr.rcvif;
145 struct ip *ip = mtod(m, struct ip *);
146 struct igmp *igmp;
147 u_int minlen;
148 struct in_multi *inm;
149 struct in_multistep step;
150 struct router_info *rti;
151 struct in_ifaddr *ia;
152 u_int timer;
153 va_list ap;
154 u_int16_t ip_len;
155
156 va_start(ap, m);
157 iphlen = va_arg(ap, int);
158 proto = va_arg(ap, int);
159 va_end(ap);
160
161 ++igmpstat.igps_rcv_total;
162
163 /*
164 * Validate lengths
165 */
166 minlen = iphlen + IGMP_MINLEN;
167 ip_len = ntohs(ip->ip_len);
168 if (ip_len < minlen) {
169 ++igmpstat.igps_rcv_tooshort;
170 m_freem(m);
171 return;
172 }
173 if (((m->m_flags & M_EXT) && (ip->ip_src.s_addr & IN_CLASSA_NET) == 0)
174 || m->m_len < minlen) {
175 if ((m = m_pullup(m, minlen)) == 0) {
176 ++igmpstat.igps_rcv_tooshort;
177 return;
178 }
179 ip = mtod(m, struct ip *);
180 }
181
182 /*
183 * Validate checksum
184 */
185 m->m_data += iphlen;
186 m->m_len -= iphlen;
187 igmp = mtod(m, struct igmp *);
188 /* No need to assert alignment here. */
189 if (in_cksum(m, ip_len - iphlen)) {
190 ++igmpstat.igps_rcv_badsum;
191 m_freem(m);
192 return;
193 }
194 m->m_data -= iphlen;
195 m->m_len += iphlen;
196
197 switch (igmp->igmp_type) {
198
199 case IGMP_HOST_MEMBERSHIP_QUERY:
200 ++igmpstat.igps_rcv_queries;
201
202 if (ifp->if_flags & IFF_LOOPBACK)
203 break;
204
205 if (igmp->igmp_code == 0) {
206 rti = rti_find(ifp);
207 if (rti == NULL)
208 break;
209 rti->rti_type = IGMP_v1_ROUTER;
210 rti->rti_age = 0;
211
212 if (ip->ip_dst.s_addr != INADDR_ALLHOSTS_GROUP) {
213 ++igmpstat.igps_rcv_badqueries;
214 m_freem(m);
215 return;
216 }
217
218 /*
219 * Start the timers in all of our membership records
220 * for the interface on which the query arrived,
221 * except those that are already running and those
222 * that belong to a "local" group (224.0.0.X).
223 */
224 IN_FIRST_MULTI(step, inm);
225 while (inm != NULL) {
226 if (inm->inm_ifp == ifp &&
227 inm->inm_timer == 0 &&
228 !IN_LOCAL_GROUP(inm->inm_addr.s_addr)) {
229 inm->inm_state = IGMP_DELAYING_MEMBER;
230 inm->inm_timer = IGMP_RANDOM_DELAY(
231 IGMP_MAX_HOST_REPORT_DELAY * PR_FASTHZ);
232 igmp_timers_are_running = 1;
233 }
234 IN_NEXT_MULTI(step, inm);
235 }
236 } else {
237 if (!IN_MULTICAST(ip->ip_dst.s_addr)) {
238 ++igmpstat.igps_rcv_badqueries;
239 m_freem(m);
240 return;
241 }
242
243 timer = igmp->igmp_code * PR_FASTHZ / IGMP_TIMER_SCALE;
244 if (timer == 0)
245 timer =1;
246
247 /*
248 * Start the timers in all of our membership records
249 * for the interface on which the query arrived,
250 * except those that are already running and those
251 * that belong to a "local" group (224.0.0.X). For
252 * timers already running, check if they need to be
253 * reset.
254 */
255 IN_FIRST_MULTI(step, inm);
256 while (inm != NULL) {
257 if (inm->inm_ifp == ifp &&
258 !IN_LOCAL_GROUP(inm->inm_addr.s_addr) &&
259 (ip->ip_dst.s_addr == INADDR_ALLHOSTS_GROUP ||
260 in_hosteq(ip->ip_dst, inm->inm_addr))) {
261 switch (inm->inm_state) {
262 case IGMP_DELAYING_MEMBER:
263 if (inm->inm_timer <= timer)
264 break;
265 /* FALLTHROUGH */
266 case IGMP_IDLE_MEMBER:
267 case IGMP_LAZY_MEMBER:
268 case IGMP_AWAKENING_MEMBER:
269 inm->inm_state =
270 IGMP_DELAYING_MEMBER;
271 inm->inm_timer =
272 IGMP_RANDOM_DELAY(timer);
273 igmp_timers_are_running = 1;
274 break;
275 case IGMP_SLEEPING_MEMBER:
276 inm->inm_state =
277 IGMP_AWAKENING_MEMBER;
278 break;
279 }
280 }
281 IN_NEXT_MULTI(step, inm);
282 }
283 }
284
285 break;
286
287 case IGMP_v1_HOST_MEMBERSHIP_REPORT:
288 ++igmpstat.igps_rcv_reports;
289
290 if (ifp->if_flags & IFF_LOOPBACK)
291 break;
292
293 if (!IN_MULTICAST(igmp->igmp_group.s_addr) ||
294 !in_hosteq(igmp->igmp_group, ip->ip_dst)) {
295 ++igmpstat.igps_rcv_badreports;
296 m_freem(m);
297 return;
298 }
299
300 /*
301 * KLUDGE: if the IP source address of the report has an
302 * unspecified (i.e., zero) subnet number, as is allowed for
303 * a booting host, replace it with the correct subnet number
304 * so that a process-level multicast routing daemon can
305 * determine which subnet it arrived from. This is necessary
306 * to compensate for the lack of any way for a process to
307 * determine the arrival interface of an incoming packet.
308 */
309 if ((ip->ip_src.s_addr & IN_CLASSA_NET) == 0) {
310 IFP_TO_IA(ifp, ia); /* XXX */
311 if (ia)
312 ip->ip_src.s_addr = ia->ia_subnet;
313 }
314
315 /*
316 * If we belong to the group being reported, stop
317 * our timer for that group.
318 */
319 IN_LOOKUP_MULTI(igmp->igmp_group, ifp, inm);
320 if (inm != NULL) {
321 inm->inm_timer = 0;
322 ++igmpstat.igps_rcv_ourreports;
323
324 switch (inm->inm_state) {
325 case IGMP_IDLE_MEMBER:
326 case IGMP_LAZY_MEMBER:
327 case IGMP_AWAKENING_MEMBER:
328 case IGMP_SLEEPING_MEMBER:
329 inm->inm_state = IGMP_SLEEPING_MEMBER;
330 break;
331 case IGMP_DELAYING_MEMBER:
332 if (inm->inm_rti->rti_type == IGMP_v1_ROUTER)
333 inm->inm_state = IGMP_LAZY_MEMBER;
334 else
335 inm->inm_state = IGMP_SLEEPING_MEMBER;
336 break;
337 }
338 }
339
340 break;
341
342 case IGMP_v2_HOST_MEMBERSHIP_REPORT:
343 #ifdef MROUTING
344 /*
345 * Make sure we don't hear our own membership report. Fast
346 * leave requires knowing that we are the only member of a
347 * group.
348 */
349 IFP_TO_IA(ifp, ia); /* XXX */
350 if (ia && in_hosteq(ip->ip_src, ia->ia_addr.sin_addr))
351 break;
352 #endif
353
354 ++igmpstat.igps_rcv_reports;
355
356 if (ifp->if_flags & IFF_LOOPBACK)
357 break;
358
359 if (!IN_MULTICAST(igmp->igmp_group.s_addr) ||
360 !in_hosteq(igmp->igmp_group, ip->ip_dst)) {
361 ++igmpstat.igps_rcv_badreports;
362 m_freem(m);
363 return;
364 }
365
366 /*
367 * KLUDGE: if the IP source address of the report has an
368 * unspecified (i.e., zero) subnet number, as is allowed for
369 * a booting host, replace it with the correct subnet number
370 * so that a process-level multicast routing daemon can
371 * determine which subnet it arrived from. This is necessary
372 * to compensate for the lack of any way for a process to
373 * determine the arrival interface of an incoming packet.
374 */
375 if ((ip->ip_src.s_addr & IN_CLASSA_NET) == 0) {
376 #ifndef MROUTING
377 IFP_TO_IA(ifp, ia); /* XXX */
378 #endif
379 if (ia)
380 ip->ip_src.s_addr = ia->ia_subnet;
381 }
382
383 /*
384 * If we belong to the group being reported, stop
385 * our timer for that group.
386 */
387 IN_LOOKUP_MULTI(igmp->igmp_group, ifp, inm);
388 if (inm != NULL) {
389 inm->inm_timer = 0;
390 ++igmpstat.igps_rcv_ourreports;
391
392 switch (inm->inm_state) {
393 case IGMP_DELAYING_MEMBER:
394 case IGMP_IDLE_MEMBER:
395 case IGMP_AWAKENING_MEMBER:
396 inm->inm_state = IGMP_LAZY_MEMBER;
397 break;
398 case IGMP_LAZY_MEMBER:
399 case IGMP_SLEEPING_MEMBER:
400 break;
401 }
402 }
403
404 break;
405
406 }
407
408 /*
409 * Pass all valid IGMP packets up to any process(es) listening
410 * on a raw IGMP socket.
411 */
412 rip_input(m, iphlen, proto);
413 return;
414 }
415
416 int
417 igmp_joingroup(inm)
418 struct in_multi *inm;
419 {
420 int report_type;
421 int s = splsoftnet();
422
423 inm->inm_state = IGMP_IDLE_MEMBER;
424
425 if (!IN_LOCAL_GROUP(inm->inm_addr.s_addr) &&
426 (inm->inm_ifp->if_flags & IFF_LOOPBACK) == 0) {
427 report_type = rti_fill(inm);
428 if (report_type == 0) {
429 splx(s);
430 return ENOMEM;
431 }
432 igmp_sendpkt(inm, report_type);
433 inm->inm_state = IGMP_DELAYING_MEMBER;
434 inm->inm_timer = IGMP_RANDOM_DELAY(
435 IGMP_MAX_HOST_REPORT_DELAY * PR_FASTHZ);
436 igmp_timers_are_running = 1;
437 } else
438 inm->inm_timer = 0;
439 splx(s);
440 return 0;
441 }
442
443 void
444 igmp_leavegroup(inm)
445 struct in_multi *inm;
446 {
447
448 switch (inm->inm_state) {
449 case IGMP_DELAYING_MEMBER:
450 case IGMP_IDLE_MEMBER:
451 if (!IN_LOCAL_GROUP(inm->inm_addr.s_addr) &&
452 (inm->inm_ifp->if_flags & IFF_LOOPBACK) == 0)
453 if (inm->inm_rti->rti_type != IGMP_v1_ROUTER)
454 igmp_sendpkt(inm, IGMP_HOST_LEAVE_MESSAGE);
455 break;
456 case IGMP_LAZY_MEMBER:
457 case IGMP_AWAKENING_MEMBER:
458 case IGMP_SLEEPING_MEMBER:
459 break;
460 }
461 }
462
463 void
464 igmp_fasttimo()
465 {
466 struct in_multi *inm;
467 struct in_multistep step;
468 int s;
469
470 /*
471 * Quick check to see if any work needs to be done, in order
472 * to minimize the overhead of fasttimo processing.
473 */
474 if (!igmp_timers_are_running)
475 return;
476
477 s = splsoftnet();
478 igmp_timers_are_running = 0;
479 IN_FIRST_MULTI(step, inm);
480 while (inm != NULL) {
481 if (inm->inm_timer == 0) {
482 /* do nothing */
483 } else if (--inm->inm_timer == 0) {
484 if (inm->inm_state == IGMP_DELAYING_MEMBER) {
485 if (inm->inm_rti->rti_type == IGMP_v1_ROUTER)
486 igmp_sendpkt(inm,
487 IGMP_v1_HOST_MEMBERSHIP_REPORT);
488 else
489 igmp_sendpkt(inm,
490 IGMP_v2_HOST_MEMBERSHIP_REPORT);
491 inm->inm_state = IGMP_IDLE_MEMBER;
492 }
493 } else {
494 igmp_timers_are_running = 1;
495 }
496 IN_NEXT_MULTI(step, inm);
497 }
498 splx(s);
499 }
500
501 void
502 igmp_slowtimo()
503 {
504 struct router_info *rti;
505 int s;
506
507 s = splsoftnet();
508 LIST_FOREACH(rti, &rti_head, rti_link) {
509 if (rti->rti_type == IGMP_v1_ROUTER &&
510 ++rti->rti_age >= IGMP_AGE_THRESHOLD) {
511 rti->rti_type = IGMP_v2_ROUTER;
512 }
513 }
514 splx(s);
515 }
516
517 void
518 igmp_sendpkt(inm, type)
519 struct in_multi *inm;
520 int type;
521 {
522 struct mbuf *m;
523 struct igmp *igmp;
524 struct ip *ip;
525 struct ip_moptions imo;
526 #ifdef MROUTING
527 extern struct socket *ip_mrouter;
528 #endif /* MROUTING */
529
530 MGETHDR(m, M_DONTWAIT, MT_HEADER);
531 if (m == NULL)
532 return;
533 /*
534 * Assume max_linkhdr + sizeof(struct ip) + IGMP_MINLEN
535 * is smaller than mbuf size returned by MGETHDR.
536 */
537 m->m_data += max_linkhdr;
538 m->m_len = sizeof(struct ip) + IGMP_MINLEN;
539 m->m_pkthdr.len = sizeof(struct ip) + IGMP_MINLEN;
540
541 ip = mtod(m, struct ip *);
542 ip->ip_tos = 0;
543 ip->ip_len = htons(sizeof(struct ip) + IGMP_MINLEN);
544 ip->ip_off = htons(0);
545 ip->ip_p = IPPROTO_IGMP;
546 ip->ip_src = zeroin_addr;
547 ip->ip_dst = inm->inm_addr;
548
549 m->m_data += sizeof(struct ip);
550 m->m_len -= sizeof(struct ip);
551 igmp = mtod(m, struct igmp *);
552 igmp->igmp_type = type;
553 igmp->igmp_code = 0;
554 igmp->igmp_group = inm->inm_addr;
555 igmp->igmp_cksum = 0;
556 igmp->igmp_cksum = in_cksum(m, IGMP_MINLEN);
557 m->m_data -= sizeof(struct ip);
558 m->m_len += sizeof(struct ip);
559
560 imo.imo_multicast_ifp = inm->inm_ifp;
561 imo.imo_multicast_ttl = 1;
562 #ifdef RSVP_ISI
563 imo.imo_multicast_vif = -1;
564 #endif
565 /*
566 * Request loopback of the report if we are acting as a multicast
567 * router, so that the process-level routing demon can hear it.
568 */
569 #ifdef MROUTING
570 imo.imo_multicast_loop = (ip_mrouter != NULL);
571 #else
572 imo.imo_multicast_loop = 0;
573 #endif /* MROUTING */
574
575 ip_output(m, (struct mbuf *)NULL, (struct route *)NULL,
576 IP_MULTICASTOPTS, &imo, (struct socket *)NULL);
577
578 ++igmpstat.igps_snd_reports;
579 }
580
581 void
582 igmp_purgeif(ifp)
583 struct ifnet *ifp;
584 {
585
586 rti_delete(ifp);
587 }
588