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