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