xref: /freebsd/sys/netinet/igmp.c (revision 8e6b01171e30297084bb0b4457c4183c2746aacc)
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.10 1995/05/16 01:28:29 davidg 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/proc.h>		/* XXX needed for sysctl.h */
57 #include <vm/vm.h>		/* XXX needed for sysctl.h */
58 #include <sys/sysctl.h>
59 
60 #include <net/if.h>
61 #include <net/route.h>
62 
63 #include <netinet/in.h>
64 #include <netinet/in_var.h>
65 #include <netinet/in_systm.h>
66 #include <netinet/ip.h>
67 #include <netinet/ip_var.h>
68 #include <netinet/igmp.h>
69 #include <netinet/igmp_var.h>
70 
71 struct igmpstat igmpstat;
72 
73 static int igmp_timers_are_running;
74 static u_long igmp_all_hosts_group;
75 static u_long igmp_local_group;
76 static u_long igmp_local_group_mask;
77 static struct router_info *Head;
78 
79 static void igmp_sendpkt(struct in_multi *, int);
80 static void igmp_sendleave(struct in_multi *);
81 
82 void
83 igmp_init()
84 {
85 	/*
86 	 * To avoid byte-swapping the same value over and over again.
87 	 */
88 	igmp_all_hosts_group = htonl(INADDR_ALLHOSTS_GROUP);
89 	igmp_local_group = htonl(0xe0000000);		/* 224.0.0.0 */
90 	igmp_local_group_mask = htonl(0xffffff00);	/* ........^ */
91 
92 	igmp_timers_are_running = 0;
93 
94 	Head = (struct router_info *) 0;
95 }
96 
97 int
98 fill_rti(inm)
99 	struct in_multi *inm;
100 {
101 	register struct router_info *rti = Head;
102 
103 #ifdef IGMP_DEBUG
104 	printf("[igmp.c, _fill_rti] --> entering \n");
105 #endif
106 	while (rti) {
107 		if (rti->ifp == inm->inm_ifp) {
108 			inm->inm_rti  = rti;
109 #ifdef IGMP_DEBUG
110 			printf("[igmp.c, _fill_rti] --> found old entry \n");
111 #endif
112 			if (rti->type == IGMP_OLD_ROUTER)
113 				return IGMP_HOST_MEMBERSHIP_REPORT;
114 			else
115 				return IGMP_HOST_NEW_MEMBERSHIP_REPORT;
116 		}
117 		rti = rti->next;
118 	}
119 	MALLOC(rti, struct router_info *, sizeof *rti, M_MRTABLE, M_NOWAIT);
120 	rti->ifp = inm->inm_ifp;
121 	rti->type = IGMP_NEW_ROUTER;
122 	rti->time = IGMP_AGE_THRESHOLD;
123 	rti->next = Head;
124 	Head = rti;
125 	inm->inm_rti = rti;
126 #ifdef IGMP_DEBUG
127 	printf("[igmp.c, _fill_rti] --> created new entry \n");
128 #endif
129 	return IGMP_HOST_NEW_MEMBERSHIP_REPORT;
130 }
131 
132 struct router_info *
133 find_rti(ifp)
134 	struct ifnet *ifp;
135 {
136         register struct router_info *rti = Head;
137 
138 #ifdef IGMP_DEBUG
139 	printf("[igmp.c, _find_rti] --> entering \n");
140 #endif
141         while (rti) {
142                 if (rti->ifp == ifp) {
143 #ifdef IGMP_DEBUG
144 			printf("[igmp.c, _find_rti] --> found old entry \n");
145 #endif
146                         return rti;
147                 }
148                 rti = rti->next;
149         }
150 	MALLOC(rti, struct router_info *, sizeof *rti, M_MRTABLE, M_NOWAIT);
151         rti->ifp = ifp;
152         rti->type = IGMP_NEW_ROUTER;
153         rti->time = IGMP_AGE_THRESHOLD;
154         rti->next = Head;
155         Head = rti;
156 #ifdef IGMP_DEBUG
157 	printf("[igmp.c, _find_rti] --> created an entry \n");
158 #endif
159         return rti;
160 }
161 
162 void
163 igmp_input(m, iphlen)
164 	register struct mbuf *m;
165 	register int iphlen;
166 {
167 	register struct igmp *igmp;
168 	register struct ip *ip;
169 	register int igmplen;
170 	register struct ifnet *ifp = m->m_pkthdr.rcvif;
171 	register int minlen;
172 	register struct in_multi *inm;
173 	register struct in_ifaddr *ia;
174 	struct in_multistep step;
175 	struct router_info *rti;
176 
177 	int timer; /** timer value in the igmp query header **/
178 
179 	++igmpstat.igps_rcv_total;
180 
181 	ip = mtod(m, struct ip *);
182 	igmplen = ip->ip_len;
183 
184 	/*
185 	 * Validate lengths
186 	 */
187 	if (igmplen < IGMP_MINLEN) {
188 		++igmpstat.igps_rcv_tooshort;
189 		m_freem(m);
190 		return;
191 	}
192 	minlen = iphlen + IGMP_MINLEN;
193 	if ((m->m_flags & M_EXT || m->m_len < minlen) &&
194 	    (m = m_pullup(m, minlen)) == 0) {
195 		++igmpstat.igps_rcv_tooshort;
196 		return;
197 	}
198 
199 	/*
200 	 * Validate checksum
201 	 */
202 	m->m_data += iphlen;
203 	m->m_len -= iphlen;
204 	igmp = mtod(m, struct igmp *);
205 	if (in_cksum(m, igmplen)) {
206 		++igmpstat.igps_rcv_badsum;
207 		m_freem(m);
208 		return;
209 	}
210 	m->m_data -= iphlen;
211 	m->m_len += iphlen;
212 
213 	ip = mtod(m, struct ip *);
214 	timer = igmp->igmp_code * PR_FASTHZ / IGMP_TIMER_SCALE;
215 	rti = find_rti(ifp);
216 
217 	switch (igmp->igmp_type) {
218 
219 	case IGMP_HOST_MEMBERSHIP_QUERY:
220 		++igmpstat.igps_rcv_queries;
221 
222 		if (ifp->if_flags & IFF_LOOPBACK)
223 			break;
224 
225 		if (igmp->igmp_code == 0) {
226 
227 			rti->type = IGMP_OLD_ROUTER; rti->time = 0;
228 
229 			/*
230 			** Do exactly as RFC 1112 says
231 			*/
232 
233 			if (ip->ip_dst.s_addr != igmp_all_hosts_group) {
234 				++igmpstat.igps_rcv_badqueries;
235 				m_freem(m);
236 				return;
237 			}
238 
239 			/*
240 			 * Start the timers in all of our membership records for
241 			 * the interface on which the query arrived, except those
242 			 * that are  already running and those that belong to a
243 			 * "local" group (224.0.0.X).
244 			 */
245 			IN_FIRST_MULTI(step, inm);
246 			while (inm != NULL) {
247 				if (inm->inm_ifp == ifp
248 				    && inm->inm_timer == 0
249 				    && ((inm->inm_addr.s_addr
250 					 & igmp_local_group_mask)
251 					!= igmp_local_group)) {
252 
253 					inm->inm_state = IGMP_DELAYING_MEMBER;
254 					inm->inm_timer = IGMP_RANDOM_DELAY(
255 				IGMP_MAX_HOST_REPORT_DELAY * PR_FASTHZ );
256 
257 					igmp_timers_are_running = 1;
258 				}
259 				IN_NEXT_MULTI(step, inm);
260 			}
261 		} else {
262 		    /*
263 		    ** New Router
264 		    */
265 
266 		    if (!(m->m_flags & M_MCAST)) {
267 			++igmpstat.igps_rcv_badqueries;
268 			m_freem(m);
269 			return;
270 		    }
271 
272 		    /*
273 		     * - Start the timers in all of our membership records
274 		     *   that the query applies to for the interface on
275 		     *   which the query arrived excl. those that belong
276 		     *   to a "local" group (224.0.0.X)
277 		     * - For timers already running check if they need to
278 		     *   be reset.
279 		     * - Use the igmp->igmp_code field as the maximum
280 		     *   delay possible
281 		     */
282 		    IN_FIRST_MULTI(step, inm);
283 		    while (inm != NULL) {
284 			if (inm->inm_ifp == ifp &&
285 			    (inm->inm_addr.s_addr & igmp_local_group_mask) !=
286 				igmp_local_group &&
287 			    (ip->ip_dst.s_addr == igmp_all_hosts_group ||
288 			     ip->ip_dst.s_addr == inm->inm_addr.s_addr)) {
289 			    switch(inm->inm_state) {
290 			      case IGMP_IDLE_MEMBER:
291 			      case IGMP_LAZY_MEMBER:
292 			      case IGMP_AWAKENING_MEMBER:
293 				    inm->inm_timer = IGMP_RANDOM_DELAY(timer);
294 				    igmp_timers_are_running = 1;
295 				    inm->inm_state = IGMP_DELAYING_MEMBER;
296 				    break;
297 			      case IGMP_DELAYING_MEMBER:
298 				if (inm->inm_timer > timer) {
299 				    inm->inm_timer = IGMP_RANDOM_DELAY(timer);
300 				    igmp_timers_are_running = 1;
301 				    inm->inm_state = IGMP_DELAYING_MEMBER;
302 				}
303 				break;
304 			      case IGMP_SLEEPING_MEMBER:
305 				inm->inm_state = IGMP_AWAKENING_MEMBER;
306 				break;
307 			    }
308 			}
309 			IN_NEXT_MULTI(step, inm);
310 		}
311 	    }
312 
313 		break;
314 
315 	case IGMP_HOST_MEMBERSHIP_REPORT:
316 		/*
317 		 * an old report
318 		 */
319 		++igmpstat.igps_rcv_reports;
320 
321 		if (ifp->if_flags & IFF_LOOPBACK)
322 			break;
323 
324 		if (!IN_MULTICAST(ntohl(igmp->igmp_group.s_addr)) ||
325 		    igmp->igmp_group.s_addr != ip->ip_dst.s_addr) {
326 			++igmpstat.igps_rcv_badreports;
327 			m_freem(m);
328 			return;
329 		}
330 
331 		/*
332 		 * KLUDGE: if the IP source address of the report has an
333 		 * unspecified (i.e., zero) subnet number, as is allowed for
334 		 * a booting host, replace it with the correct subnet number
335 		 * so that a process-level multicast routing demon can
336 		 * determine which subnet it arrived from.  This is necessary
337 		 * to compensate for the lack of any way for a process to
338 		 * determine the arrival interface of an incoming packet.
339 		 */
340 		if ((ntohl(ip->ip_src.s_addr) & IN_CLASSA_NET) == 0) {
341 			IFP_TO_IA(ifp, ia);
342 			if (ia) ip->ip_src.s_addr = htonl(ia->ia_subnet);
343 		}
344 
345 		/*
346 		 * If we belong to the group being reported, stop
347 		 * our timer for that group.
348 		 */
349 		IN_LOOKUP_MULTI(igmp->igmp_group, ifp, inm);
350 
351 		if (inm != NULL) {
352 		  inm->inm_timer = 0;
353 		  ++igmpstat.igps_rcv_ourreports;
354 
355 		  switch(inm->inm_state){
356 		  case IGMP_IDLE_MEMBER:
357 		  case IGMP_LAZY_MEMBER:
358 		  case IGMP_AWAKENING_MEMBER:
359 		  case IGMP_SLEEPING_MEMBER:
360 		    inm->inm_state = IGMP_SLEEPING_MEMBER;
361 		    break;
362 		  case IGMP_DELAYING_MEMBER:
363 		    if (inm->inm_rti->type == IGMP_OLD_ROUTER)
364 			inm->inm_state = IGMP_LAZY_MEMBER;
365 		    else
366 			inm->inm_state = IGMP_SLEEPING_MEMBER;
367 		    break;
368 		  }
369 		}
370 
371 		break;
372 
373 	      case IGMP_HOST_NEW_MEMBERSHIP_REPORT:
374 		/*
375 		 * a new report
376 		 */
377 
378 		/*
379 		 * We can get confused and think there's someone
380 		 * else out there if we are a multicast router.
381 		 * For fast leave to work, we have to know that
382 		 * we are the only member.
383 		 */
384 		IFP_TO_IA(ifp, ia);
385 		if (ia && ip->ip_src.s_addr == IA_SIN(ia)->sin_addr.s_addr)
386 			break;
387 
388 		++igmpstat.igps_rcv_reports;
389 
390 		if (ifp->if_flags & IFF_LOOPBACK)
391 		  break;
392 
393 		if (!IN_MULTICAST(ntohl(igmp->igmp_group.s_addr)) ||
394 		    igmp->igmp_group.s_addr != ip->ip_dst.s_addr) {
395 		  ++igmpstat.igps_rcv_badreports;
396 		  m_freem(m);
397 		  return;
398 		}
399 
400 		/*
401 		 * KLUDGE: if the IP source address of the report has an
402 		 * unspecified (i.e., zero) subnet number, as is allowed for
403 		 * a booting host, replace it with the correct subnet number
404 		 * so that a process-level multicast routing demon can
405 		 * determine which subnet it arrived from.  This is necessary
406 		 * to compensate for the lack of any way for a process to
407 		 * determine the arrival interface of an incoming packet.
408 		 */
409 		if ((ntohl(ip->ip_src.s_addr) & IN_CLASSA_NET) == 0) {
410 /* #ifndef MROUTING XXX - I don't think the ifdef is necessary */
411 		  IFP_TO_IA(ifp, ia);
412 /* #endif */
413 		  if (ia) ip->ip_src.s_addr = htonl(ia->ia_subnet);
414 		}
415 
416 		/*
417 		 * If we belong to the group being reported, stop
418 		 * our timer for that group.
419 		 */
420 		IN_LOOKUP_MULTI(igmp->igmp_group, ifp, inm);
421 		if (inm != NULL) {
422 		  inm->inm_timer = 0;
423 		  ++igmpstat.igps_rcv_ourreports;
424 
425 		  switch(inm->inm_state){
426 		  case IGMP_DELAYING_MEMBER:
427 		  case IGMP_IDLE_MEMBER:
428 		    inm->inm_state = IGMP_LAZY_MEMBER;
429 		    break;
430 		  case IGMP_AWAKENING_MEMBER:
431 		    inm->inm_state = IGMP_LAZY_MEMBER;
432 		    break;
433 		  case IGMP_LAZY_MEMBER:
434 		  case IGMP_SLEEPING_MEMBER:
435 		    break;
436 		  }
437 		}
438 	}
439 
440 	/*
441 	 * Pass all valid IGMP packets up to any process(es) listening
442 	 * on a raw IGMP socket.
443 	 */
444 	rip_input(m);
445 }
446 
447 void
448 igmp_joingroup(inm)
449 	struct in_multi *inm;
450 {
451 	int s = splnet();
452 
453 	inm->inm_state = IGMP_IDLE_MEMBER;
454 
455 	if ((inm->inm_addr.s_addr & igmp_local_group_mask) == igmp_local_group
456 	    || inm->inm_ifp->if_flags & IFF_LOOPBACK)
457 		inm->inm_timer = 0;
458 	else {
459 		igmp_sendpkt(inm,fill_rti(inm));
460 		inm->inm_timer = IGMP_RANDOM_DELAY(
461 					IGMP_MAX_HOST_REPORT_DELAY*PR_FASTHZ);
462 		inm->inm_state = IGMP_DELAYING_MEMBER;
463 		igmp_timers_are_running = 1;
464 	}
465 	splx(s);
466 }
467 
468 void
469 igmp_leavegroup(inm)
470 	struct in_multi *inm;
471 {
472          switch(inm->inm_state) {
473 	 case IGMP_DELAYING_MEMBER:
474 	 case IGMP_IDLE_MEMBER:
475 	   if (((inm->inm_addr.s_addr & igmp_local_group_mask)
476 		!= igmp_local_group)
477 	       && !(inm->inm_ifp->if_flags & IFF_LOOPBACK))
478 	       if (inm->inm_rti->type != IGMP_OLD_ROUTER)
479 		   igmp_sendleave(inm);
480 	   break;
481 	 case IGMP_LAZY_MEMBER:
482 	 case IGMP_AWAKENING_MEMBER:
483 	 case IGMP_SLEEPING_MEMBER:
484 	   break;
485 	 }
486 }
487 
488 void
489 igmp_fasttimo()
490 {
491 	register struct in_multi *inm;
492 	struct in_multistep step;
493 	int s;
494 
495 	/*
496 	 * Quick check to see if any work needs to be done, in order
497 	 * to minimize the overhead of fasttimo processing.
498 	 */
499 
500 	if (!igmp_timers_are_running)
501 		return;
502 
503 	s = splnet();
504 	igmp_timers_are_running = 0;
505 	IN_FIRST_MULTI(step, inm);
506 	while (inm != NULL) {
507 		if (inm->inm_timer == 0) {
508 			/* do nothing */
509 		} else if (--inm->inm_timer == 0) {
510 		  if (inm->inm_state == IGMP_DELAYING_MEMBER) {
511 		    if (inm->inm_rti->type == IGMP_OLD_ROUTER)
512 			igmp_sendpkt(inm, IGMP_HOST_MEMBERSHIP_REPORT);
513 		    else
514 			igmp_sendpkt(inm, IGMP_HOST_NEW_MEMBERSHIP_REPORT);
515 		    inm->inm_state = IGMP_IDLE_MEMBER;
516 		  }
517 		} else {
518 			igmp_timers_are_running = 1;
519 		}
520 		IN_NEXT_MULTI(step, inm);
521 	}
522 	splx(s);
523 }
524 
525 void
526 igmp_slowtimo()
527 {
528 	int s = splnet();
529 	register struct router_info *rti =  Head;
530 
531 #ifdef IGMP_DEBUG
532 	printf("[igmp.c,_slowtimo] -- > entering \n");
533 #endif
534 	while (rti) {
535 		rti->time ++;
536 		if (rti->time >= IGMP_AGE_THRESHOLD){
537 			rti->type = IGMP_NEW_ROUTER;
538 			rti->time = IGMP_AGE_THRESHOLD;
539 		}
540 		rti = rti->next;
541 	}
542 #ifdef IGMP_DEBUG
543 	printf("[igmp.c,_slowtimo] -- > exiting \n");
544 #endif
545 	splx(s);
546 }
547 
548 static void
549 igmp_sendpkt(inm, type)
550 	struct in_multi *inm;
551 	int type;
552 {
553         struct mbuf *m;
554         struct igmp *igmp;
555         struct ip *ip;
556         struct ip_moptions *imo;
557 
558         MGETHDR(m, M_DONTWAIT, MT_HEADER);
559         if (m == NULL)
560                 return;
561 
562 	MALLOC(imo, struct ip_moptions *, sizeof *imo, M_IPMOPTS, M_DONTWAIT);
563 	if (!imo) {
564 		m_free(m);
565 		return;
566 	}
567 
568 	m->m_pkthdr.rcvif = loif;
569 	m->m_pkthdr.len = sizeof(struct ip) + IGMP_MINLEN;
570 	MH_ALIGN(m, IGMP_MINLEN + sizeof(struct ip));
571 	m->m_data += sizeof(struct ip);
572         m->m_len = IGMP_MINLEN;
573         igmp = mtod(m, struct igmp *);
574         igmp->igmp_type   = type;
575         igmp->igmp_code   = 0;
576         igmp->igmp_group  = inm->inm_addr;
577         igmp->igmp_cksum  = 0;
578         igmp->igmp_cksum  = in_cksum(m, IGMP_MINLEN);
579 
580         m->m_data -= sizeof(struct ip);
581         m->m_len += sizeof(struct ip);
582         ip = mtod(m, struct ip *);
583         ip->ip_tos        = 0;
584         ip->ip_len        = sizeof(struct ip) + IGMP_MINLEN;
585         ip->ip_off        = 0;
586         ip->ip_p          = IPPROTO_IGMP;
587         ip->ip_src.s_addr = INADDR_ANY;
588         ip->ip_dst        = igmp->igmp_group;
589 
590         imo->imo_multicast_ifp  = inm->inm_ifp;
591         imo->imo_multicast_ttl  = 1;
592 	imo->imo_multicast_vif  = -1;
593         /*
594          * Request loopback of the report if we are acting as a multicast
595          * router, so that the process-level routing demon can hear it.
596          */
597         imo->imo_multicast_loop = (ip_mrouter != NULL);
598 
599         ip_output(m, (struct mbuf *)0, (struct route *)0, 0, imo);
600 
601 	FREE(imo, M_IPMOPTS);
602         ++igmpstat.igps_snd_reports;
603 }
604 
605 static void
606 igmp_sendleave(inm)
607 	struct in_multi *inm;
608 {
609 	igmp_sendpkt(inm, IGMP_HOST_LEAVE_MESSAGE);
610 }
611 
612 int
613 igmp_sysctl(int *name, u_int namelen, void *oldp, size_t *oldlenp,
614 	    void *newp, size_t newlen)
615 {
616 	/* All sysctl names at this level are terminal. */
617 	if (namelen != 1)
618 		return ENOTDIR;	/* XXX overloaded */
619 
620 	switch(name[0]) {
621 	case IGMPCTL_STATS:
622 		return sysctl_rdstruct(oldp, oldlenp, newp, &igmpstat,
623 				       sizeof igmpstat);
624 	default:
625 		return ENOPROTOOPT;
626 	}
627 }
628