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