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