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