igmp.c revision 1.50 1 /* $NetBSD: igmp.c,v 1.50 2009/09/13 18:45:11 pooka 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.50 2009/09/13 18:45:11 pooka 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/socketvar.h>
51 #include <sys/protosw.h>
52 #include <sys/systm.h>
53 #include <sys/sysctl.h>
54
55 #include <net/if.h>
56 #include <net/route.h>
57 #include <net/net_stats.h>
58
59 #include <netinet/in.h>
60 #include <netinet/in_var.h>
61 #include <netinet/in_systm.h>
62 #include <netinet/ip.h>
63 #include <netinet/ip_var.h>
64 #include <netinet/igmp.h>
65 #include <netinet/igmp_var.h>
66
67 #include <machine/stdarg.h>
68
69 #define IP_MULTICASTOPTS 0
70
71 static struct pool igmp_rti_pool;
72
73 static percpu_t *igmpstat_percpu;
74
75 #define IGMP_STATINC(x) _NET_STATINC(igmpstat_percpu, x)
76
77 int igmp_timers_are_running;
78 static LIST_HEAD(, router_info) rti_head = LIST_HEAD_INITIALIZER(rti_head);
79
80 void igmp_sendpkt(struct in_multi *, int);
81 static int rti_fill(struct in_multi *);
82 static struct router_info *rti_find(struct ifnet *);
83 static void rti_delete(struct ifnet *);
84
85 static int
86 rti_fill(struct in_multi *inm)
87 {
88 struct router_info *rti;
89
90 /* this function is called at splsoftnet() */
91 LIST_FOREACH(rti, &rti_head, rti_link) {
92 if (rti->rti_ifp == inm->inm_ifp) {
93 inm->inm_rti = rti;
94 if (rti->rti_type == IGMP_v1_ROUTER)
95 return (IGMP_v1_HOST_MEMBERSHIP_REPORT);
96 else
97 return (IGMP_v2_HOST_MEMBERSHIP_REPORT);
98 }
99 }
100
101 rti = pool_get(&igmp_rti_pool, PR_NOWAIT);
102 if (rti == NULL)
103 return 0;
104 rti->rti_ifp = inm->inm_ifp;
105 rti->rti_type = IGMP_v2_ROUTER;
106 LIST_INSERT_HEAD(&rti_head, rti, rti_link);
107 inm->inm_rti = rti;
108 return (IGMP_v2_HOST_MEMBERSHIP_REPORT);
109 }
110
111 static struct router_info *
112 rti_find(struct ifnet *ifp)
113 {
114 struct router_info *rti;
115 int s = splsoftnet();
116
117 LIST_FOREACH(rti, &rti_head, rti_link) {
118 if (rti->rti_ifp == ifp)
119 return (rti);
120 }
121
122 rti = pool_get(&igmp_rti_pool, PR_NOWAIT);
123 if (rti == NULL) {
124 splx(s);
125 return NULL;
126 }
127 rti->rti_ifp = ifp;
128 rti->rti_type = IGMP_v2_ROUTER;
129 LIST_INSERT_HEAD(&rti_head, rti, rti_link);
130 splx(s);
131 return (rti);
132 }
133
134 static void
135 rti_delete(struct ifnet *ifp) /* MUST be called at splsoftnet */
136 {
137 struct router_info *rti;
138
139 LIST_FOREACH(rti, &rti_head, rti_link) {
140 if (rti->rti_ifp == ifp) {
141 LIST_REMOVE(rti, rti_link);
142 pool_put(&igmp_rti_pool, rti);
143 return;
144 }
145 }
146 }
147
148 void
149 igmp_init(void)
150 {
151
152 pool_init(&igmp_rti_pool, sizeof(struct router_info), 0, 0, 0,
153 "igmppl", NULL, IPL_SOFTNET);
154 igmpstat_percpu = percpu_alloc(sizeof(uint64_t) * IGMP_NSTATS);
155 }
156
157 void
158 igmp_input(struct mbuf *m, ...)
159 {
160 int proto;
161 int iphlen;
162 struct ifnet *ifp = m->m_pkthdr.rcvif;
163 struct ip *ip = mtod(m, struct ip *);
164 struct igmp *igmp;
165 u_int minlen;
166 struct in_multi *inm;
167 struct in_multistep step;
168 struct router_info *rti;
169 struct in_ifaddr *ia;
170 u_int timer;
171 va_list ap;
172 u_int16_t ip_len;
173
174 va_start(ap, m);
175 iphlen = va_arg(ap, int);
176 proto = va_arg(ap, int);
177 va_end(ap);
178
179 IGMP_STATINC(IGMP_STAT_RCV_TOTAL);
180
181 /*
182 * Validate lengths
183 */
184 minlen = iphlen + IGMP_MINLEN;
185 ip_len = ntohs(ip->ip_len);
186 if (ip_len < minlen) {
187 IGMP_STATINC(IGMP_STAT_RCV_TOOSHORT);
188 m_freem(m);
189 return;
190 }
191 if (((m->m_flags & M_EXT) && (ip->ip_src.s_addr & IN_CLASSA_NET) == 0)
192 || m->m_len < minlen) {
193 if ((m = m_pullup(m, minlen)) == 0) {
194 IGMP_STATINC(IGMP_STAT_RCV_TOOSHORT);
195 return;
196 }
197 ip = mtod(m, struct ip *);
198 }
199
200 /*
201 * Validate checksum
202 */
203 m->m_data += iphlen;
204 m->m_len -= iphlen;
205 igmp = mtod(m, struct igmp *);
206 /* No need to assert alignment here. */
207 if (in_cksum(m, ip_len - iphlen)) {
208 IGMP_STATINC(IGMP_STAT_RCV_BADSUM);
209 m_freem(m);
210 return;
211 }
212 m->m_data -= iphlen;
213 m->m_len += iphlen;
214
215 switch (igmp->igmp_type) {
216
217 case IGMP_HOST_MEMBERSHIP_QUERY:
218 IGMP_STATINC(IGMP_STAT_RCV_QUERIES);
219
220 if (ifp->if_flags & IFF_LOOPBACK)
221 break;
222
223 if (igmp->igmp_code == 0) {
224 rti = rti_find(ifp);
225 if (rti == NULL)
226 break;
227 rti->rti_type = IGMP_v1_ROUTER;
228 rti->rti_age = 0;
229
230 if (ip->ip_dst.s_addr != INADDR_ALLHOSTS_GROUP) {
231 IGMP_STATINC(IGMP_STAT_RCV_BADQUERIES);
232 m_freem(m);
233 return;
234 }
235
236 /*
237 * Start the timers in all of our membership records
238 * for the interface on which the query arrived,
239 * except those that are already running and those
240 * that belong to a "local" group (224.0.0.X).
241 */
242 IN_FIRST_MULTI(step, inm);
243 while (inm != NULL) {
244 if (inm->inm_ifp == ifp &&
245 inm->inm_timer == 0 &&
246 !IN_LOCAL_GROUP(inm->inm_addr.s_addr)) {
247 inm->inm_state = IGMP_DELAYING_MEMBER;
248 inm->inm_timer = IGMP_RANDOM_DELAY(
249 IGMP_MAX_HOST_REPORT_DELAY * PR_FASTHZ);
250 igmp_timers_are_running = 1;
251 }
252 IN_NEXT_MULTI(step, inm);
253 }
254 } else {
255 if (!IN_MULTICAST(ip->ip_dst.s_addr)) {
256 IGMP_STATINC(IGMP_STAT_RCV_BADQUERIES);
257 m_freem(m);
258 return;
259 }
260
261 timer = igmp->igmp_code * PR_FASTHZ / IGMP_TIMER_SCALE;
262 if (timer == 0)
263 timer =1;
264
265 /*
266 * Start the timers in all of our membership records
267 * for the interface on which the query arrived,
268 * except those that are already running and those
269 * that belong to a "local" group (224.0.0.X). For
270 * timers already running, check if they need to be
271 * reset.
272 */
273 IN_FIRST_MULTI(step, inm);
274 while (inm != NULL) {
275 if (inm->inm_ifp == ifp &&
276 !IN_LOCAL_GROUP(inm->inm_addr.s_addr) &&
277 (ip->ip_dst.s_addr == INADDR_ALLHOSTS_GROUP ||
278 in_hosteq(ip->ip_dst, inm->inm_addr))) {
279 switch (inm->inm_state) {
280 case IGMP_DELAYING_MEMBER:
281 if (inm->inm_timer <= timer)
282 break;
283 /* FALLTHROUGH */
284 case IGMP_IDLE_MEMBER:
285 case IGMP_LAZY_MEMBER:
286 case IGMP_AWAKENING_MEMBER:
287 inm->inm_state =
288 IGMP_DELAYING_MEMBER;
289 inm->inm_timer =
290 IGMP_RANDOM_DELAY(timer);
291 igmp_timers_are_running = 1;
292 break;
293 case IGMP_SLEEPING_MEMBER:
294 inm->inm_state =
295 IGMP_AWAKENING_MEMBER;
296 break;
297 }
298 }
299 IN_NEXT_MULTI(step, inm);
300 }
301 }
302
303 break;
304
305 case IGMP_v1_HOST_MEMBERSHIP_REPORT:
306 IGMP_STATINC(IGMP_STAT_RCV_REPORTS);
307
308 if (ifp->if_flags & IFF_LOOPBACK)
309 break;
310
311 if (!IN_MULTICAST(igmp->igmp_group.s_addr) ||
312 !in_hosteq(igmp->igmp_group, ip->ip_dst)) {
313 IGMP_STATINC(IGMP_STAT_RCV_BADREPORTS);
314 m_freem(m);
315 return;
316 }
317
318 /*
319 * KLUDGE: if the IP source address of the report has an
320 * unspecified (i.e., zero) subnet number, as is allowed for
321 * a booting host, replace it with the correct subnet number
322 * so that a process-level multicast routing daemon can
323 * determine which subnet it arrived from. This is necessary
324 * to compensate for the lack of any way for a process to
325 * determine the arrival interface of an incoming packet.
326 */
327 if ((ip->ip_src.s_addr & IN_CLASSA_NET) == 0) {
328 IFP_TO_IA(ifp, ia); /* XXX */
329 if (ia)
330 ip->ip_src.s_addr = ia->ia_subnet;
331 }
332
333 /*
334 * If we belong to the group being reported, stop
335 * our timer for that group.
336 */
337 IN_LOOKUP_MULTI(igmp->igmp_group, ifp, inm);
338 if (inm != NULL) {
339 inm->inm_timer = 0;
340 IGMP_STATINC(IGMP_STAT_RCV_OURREPORTS);
341
342 switch (inm->inm_state) {
343 case IGMP_IDLE_MEMBER:
344 case IGMP_LAZY_MEMBER:
345 case IGMP_AWAKENING_MEMBER:
346 case IGMP_SLEEPING_MEMBER:
347 inm->inm_state = IGMP_SLEEPING_MEMBER;
348 break;
349 case IGMP_DELAYING_MEMBER:
350 if (inm->inm_rti->rti_type == IGMP_v1_ROUTER)
351 inm->inm_state = IGMP_LAZY_MEMBER;
352 else
353 inm->inm_state = IGMP_SLEEPING_MEMBER;
354 break;
355 }
356 }
357
358 break;
359
360 case IGMP_v2_HOST_MEMBERSHIP_REPORT:
361 #ifdef MROUTING
362 /*
363 * Make sure we don't hear our own membership report. Fast
364 * leave requires knowing that we are the only member of a
365 * group.
366 */
367 IFP_TO_IA(ifp, ia); /* XXX */
368 if (ia && in_hosteq(ip->ip_src, ia->ia_addr.sin_addr))
369 break;
370 #endif
371
372 IGMP_STATINC(IGMP_STAT_RCV_REPORTS);
373
374 if (ifp->if_flags & IFF_LOOPBACK)
375 break;
376
377 if (!IN_MULTICAST(igmp->igmp_group.s_addr) ||
378 !in_hosteq(igmp->igmp_group, ip->ip_dst)) {
379 IGMP_STATINC(IGMP_STAT_RCV_BADREPORTS);
380 m_freem(m);
381 return;
382 }
383
384 /*
385 * KLUDGE: if the IP source address of the report has an
386 * unspecified (i.e., zero) subnet number, as is allowed for
387 * a booting host, replace it with the correct subnet number
388 * so that a process-level multicast routing daemon can
389 * determine which subnet it arrived from. This is necessary
390 * to compensate for the lack of any way for a process to
391 * determine the arrival interface of an incoming packet.
392 */
393 if ((ip->ip_src.s_addr & IN_CLASSA_NET) == 0) {
394 #ifndef MROUTING
395 IFP_TO_IA(ifp, ia); /* XXX */
396 #endif
397 if (ia)
398 ip->ip_src.s_addr = ia->ia_subnet;
399 }
400
401 /*
402 * If we belong to the group being reported, stop
403 * our timer for that group.
404 */
405 IN_LOOKUP_MULTI(igmp->igmp_group, ifp, inm);
406 if (inm != NULL) {
407 inm->inm_timer = 0;
408 IGMP_STATINC(IGMP_STAT_RCV_OURREPORTS);
409
410 switch (inm->inm_state) {
411 case IGMP_DELAYING_MEMBER:
412 case IGMP_IDLE_MEMBER:
413 case IGMP_AWAKENING_MEMBER:
414 inm->inm_state = IGMP_LAZY_MEMBER;
415 break;
416 case IGMP_LAZY_MEMBER:
417 case IGMP_SLEEPING_MEMBER:
418 break;
419 }
420 }
421
422 break;
423
424 }
425
426 /*
427 * Pass all valid IGMP packets up to any process(es) listening
428 * on a raw IGMP socket.
429 */
430 rip_input(m, iphlen, proto);
431 return;
432 }
433
434 int
435 igmp_joingroup(struct in_multi *inm)
436 {
437 int report_type;
438 int s = splsoftnet();
439
440 inm->inm_state = IGMP_IDLE_MEMBER;
441
442 if (!IN_LOCAL_GROUP(inm->inm_addr.s_addr) &&
443 (inm->inm_ifp->if_flags & IFF_LOOPBACK) == 0) {
444 report_type = rti_fill(inm);
445 if (report_type == 0) {
446 splx(s);
447 return ENOMEM;
448 }
449 igmp_sendpkt(inm, report_type);
450 inm->inm_state = IGMP_DELAYING_MEMBER;
451 inm->inm_timer = IGMP_RANDOM_DELAY(
452 IGMP_MAX_HOST_REPORT_DELAY * PR_FASTHZ);
453 igmp_timers_are_running = 1;
454 } else
455 inm->inm_timer = 0;
456 splx(s);
457 return 0;
458 }
459
460 void
461 igmp_leavegroup(struct in_multi *inm)
462 {
463
464 switch (inm->inm_state) {
465 case IGMP_DELAYING_MEMBER:
466 case IGMP_IDLE_MEMBER:
467 if (!IN_LOCAL_GROUP(inm->inm_addr.s_addr) &&
468 (inm->inm_ifp->if_flags & IFF_LOOPBACK) == 0)
469 if (inm->inm_rti->rti_type != IGMP_v1_ROUTER)
470 igmp_sendpkt(inm, IGMP_HOST_LEAVE_MESSAGE);
471 break;
472 case IGMP_LAZY_MEMBER:
473 case IGMP_AWAKENING_MEMBER:
474 case IGMP_SLEEPING_MEMBER:
475 break;
476 }
477 }
478
479 void
480 igmp_fasttimo(void)
481 {
482 struct in_multi *inm;
483 struct in_multistep step;
484
485 /*
486 * Quick check to see if any work needs to be done, in order
487 * to minimize the overhead of fasttimo processing.
488 */
489 if (!igmp_timers_are_running)
490 return;
491
492 mutex_enter(softnet_lock);
493 KERNEL_LOCK(1, NULL);
494
495 igmp_timers_are_running = 0;
496 IN_FIRST_MULTI(step, inm);
497 while (inm != NULL) {
498 if (inm->inm_timer == 0) {
499 /* do nothing */
500 } else if (--inm->inm_timer == 0) {
501 if (inm->inm_state == IGMP_DELAYING_MEMBER) {
502 if (inm->inm_rti->rti_type == IGMP_v1_ROUTER)
503 igmp_sendpkt(inm,
504 IGMP_v1_HOST_MEMBERSHIP_REPORT);
505 else
506 igmp_sendpkt(inm,
507 IGMP_v2_HOST_MEMBERSHIP_REPORT);
508 inm->inm_state = IGMP_IDLE_MEMBER;
509 }
510 } else {
511 igmp_timers_are_running = 1;
512 }
513 IN_NEXT_MULTI(step, inm);
514 }
515
516 KERNEL_UNLOCK_ONE(NULL);
517 mutex_exit(softnet_lock);
518 }
519
520 void
521 igmp_slowtimo(void)
522 {
523 struct router_info *rti;
524
525 mutex_enter(softnet_lock);
526 KERNEL_LOCK(1, NULL);
527 LIST_FOREACH(rti, &rti_head, rti_link) {
528 if (rti->rti_type == IGMP_v1_ROUTER &&
529 ++rti->rti_age >= IGMP_AGE_THRESHOLD) {
530 rti->rti_type = IGMP_v2_ROUTER;
531 }
532 }
533 KERNEL_UNLOCK_ONE(NULL);
534 mutex_exit(softnet_lock);
535 }
536
537 void
538 igmp_sendpkt(struct in_multi *inm, int type)
539 {
540 struct mbuf *m;
541 struct igmp *igmp;
542 struct ip *ip;
543 struct ip_moptions imo;
544 #ifdef MROUTING
545 extern struct socket *ip_mrouter;
546 #endif /* MROUTING */
547
548 MGETHDR(m, M_DONTWAIT, MT_HEADER);
549 if (m == NULL)
550 return;
551 /*
552 * Assume max_linkhdr + sizeof(struct ip) + IGMP_MINLEN
553 * is smaller than mbuf size returned by MGETHDR.
554 */
555 m->m_data += max_linkhdr;
556 m->m_len = sizeof(struct ip) + IGMP_MINLEN;
557 m->m_pkthdr.len = sizeof(struct ip) + IGMP_MINLEN;
558
559 ip = mtod(m, struct ip *);
560 ip->ip_tos = 0;
561 ip->ip_len = htons(sizeof(struct ip) + IGMP_MINLEN);
562 ip->ip_off = htons(0);
563 ip->ip_p = IPPROTO_IGMP;
564 ip->ip_src = zeroin_addr;
565 ip->ip_dst = inm->inm_addr;
566
567 m->m_data += sizeof(struct ip);
568 m->m_len -= sizeof(struct ip);
569 igmp = mtod(m, struct igmp *);
570 igmp->igmp_type = type;
571 igmp->igmp_code = 0;
572 igmp->igmp_group = inm->inm_addr;
573 igmp->igmp_cksum = 0;
574 igmp->igmp_cksum = in_cksum(m, IGMP_MINLEN);
575 m->m_data -= sizeof(struct ip);
576 m->m_len += sizeof(struct ip);
577
578 imo.imo_multicast_ifp = inm->inm_ifp;
579 imo.imo_multicast_ttl = 1;
580 #ifdef RSVP_ISI
581 imo.imo_multicast_vif = -1;
582 #endif
583 /*
584 * Request loopback of the report if we are acting as a multicast
585 * router, so that the process-level routing demon can hear it.
586 */
587 #ifdef MROUTING
588 imo.imo_multicast_loop = (ip_mrouter != NULL);
589 #else
590 imo.imo_multicast_loop = 0;
591 #endif /* MROUTING */
592
593 ip_output(m, NULL, NULL, IP_MULTICASTOPTS, &imo, NULL);
594
595 IGMP_STATINC(IGMP_STAT_SND_REPORTS);
596 }
597
598 void
599 igmp_purgeif(struct ifnet *ifp) /* MUST be called at splsoftnet() */
600 {
601 rti_delete(ifp); /* manipulates pools */
602 }
603
604 static int
605 sysctl_net_inet_igmp_stats(SYSCTLFN_ARGS)
606 {
607
608 return (NETSTAT_SYSCTL(igmpstat_percpu, IGMP_NSTATS));
609 }
610
611 SYSCTL_SETUP(sysctl_net_inet_igmp_setup, "sysctl net.inet.igmp subtree setup")
612 {
613
614 sysctl_createv(clog, 0, NULL, NULL,
615 CTLFLAG_PERMANENT,
616 CTLTYPE_NODE, "net", NULL,
617 NULL, 0, NULL, 0,
618 CTL_NET, CTL_EOL);
619 sysctl_createv(clog, 0, NULL, NULL,
620 CTLFLAG_PERMANENT,
621 CTLTYPE_NODE, "inet", NULL,
622 NULL, 0, NULL, 0,
623 CTL_NET, PF_INET, CTL_EOL);
624 sysctl_createv(clog, 0, NULL, NULL,
625 CTLFLAG_PERMANENT,
626 CTLTYPE_NODE, "igmp",
627 SYSCTL_DESCR("Internet Group Management Protocol"),
628 NULL, 0, NULL, 0,
629 CTL_NET, PF_INET, IPPROTO_IGMP, CTL_EOL);
630
631 sysctl_createv(clog, 0, NULL, NULL,
632 CTLFLAG_PERMANENT,
633 CTLTYPE_STRUCT, "stats",
634 SYSCTL_DESCR("IGMP statistics"),
635 sysctl_net_inet_igmp_stats, 0, NULL, 0,
636 CTL_NET, PF_INET, IPPROTO_IGMP, CTL_CREATE, CTL_EOL);
637 }
638