Home | History | Annotate | Line # | Download | only in netinet
igmp.c revision 1.30
      1 /*	$NetBSD: igmp.c,v 1.30 2002/06/30 22:40:33 thorpej 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.30 2002/06/30 22:40:33 thorpej 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 	/* No need to assert alignment here. */
    184 	if (in_cksum(m, ip->ip_len - iphlen)) {
    185 		++igmpstat.igps_rcv_badsum;
    186 		m_freem(m);
    187 		return;
    188 	}
    189 	m->m_data -= iphlen;
    190 	m->m_len += iphlen;
    191 
    192 	switch (igmp->igmp_type) {
    193 
    194 	case IGMP_HOST_MEMBERSHIP_QUERY:
    195 		++igmpstat.igps_rcv_queries;
    196 
    197 		if (ifp->if_flags & IFF_LOOPBACK)
    198 			break;
    199 
    200 		if (igmp->igmp_code == 0) {
    201 			rti = rti_find(ifp);
    202 			rti->rti_type = IGMP_v1_ROUTER;
    203 			rti->rti_age = 0;
    204 
    205 			if (ip->ip_dst.s_addr != INADDR_ALLHOSTS_GROUP) {
    206 				++igmpstat.igps_rcv_badqueries;
    207 				m_freem(m);
    208 				return;
    209 			}
    210 
    211 			/*
    212 			 * Start the timers in all of our membership records
    213 			 * for the interface on which the query arrived,
    214 			 * except those that are already running and those
    215 			 * that belong to a "local" group (224.0.0.X).
    216 			 */
    217 			IN_FIRST_MULTI(step, inm);
    218 			while (inm != NULL) {
    219 				if (inm->inm_ifp == ifp &&
    220 				    inm->inm_timer == 0 &&
    221 				    !IN_LOCAL_GROUP(inm->inm_addr.s_addr)) {
    222 					inm->inm_state = IGMP_DELAYING_MEMBER;
    223 					inm->inm_timer = IGMP_RANDOM_DELAY(
    224 					    IGMP_MAX_HOST_REPORT_DELAY * PR_FASTHZ);
    225 					igmp_timers_are_running = 1;
    226 				}
    227 				IN_NEXT_MULTI(step, inm);
    228 			}
    229 		} else {
    230 			if (!IN_MULTICAST(ip->ip_dst.s_addr)) {
    231 				++igmpstat.igps_rcv_badqueries;
    232 				m_freem(m);
    233 				return;
    234 			}
    235 
    236 			timer = igmp->igmp_code * PR_FASTHZ / IGMP_TIMER_SCALE;
    237 			if (timer == 0)
    238 				timer =1;
    239 
    240 			/*
    241 			 * Start the timers in all of our membership records
    242 			 * for the interface on which the query arrived,
    243 			 * except those that are already running and those
    244 			 * that belong to a "local" group (224.0.0.X).  For
    245 			 * timers already running, check if they need to be
    246 			 * reset.
    247 			 */
    248 			IN_FIRST_MULTI(step, inm);
    249 			while (inm != NULL) {
    250 				if (inm->inm_ifp == ifp &&
    251 				    !IN_LOCAL_GROUP(inm->inm_addr.s_addr) &&
    252 				    (ip->ip_dst.s_addr == INADDR_ALLHOSTS_GROUP ||
    253 				     in_hosteq(ip->ip_dst, inm->inm_addr))) {
    254 					switch (inm->inm_state) {
    255 					case IGMP_DELAYING_MEMBER:
    256 						if (inm->inm_timer <= timer)
    257 							break;
    258 						/* FALLTHROUGH */
    259 					case IGMP_IDLE_MEMBER:
    260 					case IGMP_LAZY_MEMBER:
    261 					case IGMP_AWAKENING_MEMBER:
    262 						inm->inm_state =
    263 						    IGMP_DELAYING_MEMBER;
    264 						inm->inm_timer =
    265 						    IGMP_RANDOM_DELAY(timer);
    266 						igmp_timers_are_running = 1;
    267 						break;
    268 					case IGMP_SLEEPING_MEMBER:
    269 						inm->inm_state =
    270 						    IGMP_AWAKENING_MEMBER;
    271 						break;
    272 					}
    273 				}
    274 				IN_NEXT_MULTI(step, inm);
    275 			}
    276 		}
    277 
    278 		break;
    279 
    280 	case IGMP_v1_HOST_MEMBERSHIP_REPORT:
    281 		++igmpstat.igps_rcv_reports;
    282 
    283 		if (ifp->if_flags & IFF_LOOPBACK)
    284 			break;
    285 
    286 		if (!IN_MULTICAST(igmp->igmp_group.s_addr) ||
    287 		    !in_hosteq(igmp->igmp_group, ip->ip_dst)) {
    288 			++igmpstat.igps_rcv_badreports;
    289 			m_freem(m);
    290 			return;
    291 		}
    292 
    293 		/*
    294 		 * KLUDGE: if the IP source address of the report has an
    295 		 * unspecified (i.e., zero) subnet number, as is allowed for
    296 		 * a booting host, replace it with the correct subnet number
    297 		 * so that a process-level multicast routing daemon can
    298 		 * determine which subnet it arrived from.  This is necessary
    299 		 * to compensate for the lack of any way for a process to
    300 		 * determine the arrival interface of an incoming packet.
    301 		 */
    302 		if ((ip->ip_src.s_addr & IN_CLASSA_NET) == 0) {
    303 			IFP_TO_IA(ifp, ia);		/* XXX */
    304 			if (ia)
    305 				ip->ip_src.s_addr = ia->ia_subnet;
    306 		}
    307 
    308 		/*
    309 		 * If we belong to the group being reported, stop
    310 		 * our timer for that group.
    311 		 */
    312 		IN_LOOKUP_MULTI(igmp->igmp_group, ifp, inm);
    313 		if (inm != NULL) {
    314 			inm->inm_timer = 0;
    315 			++igmpstat.igps_rcv_ourreports;
    316 
    317 			switch (inm->inm_state) {
    318 			case IGMP_IDLE_MEMBER:
    319 			case IGMP_LAZY_MEMBER:
    320 			case IGMP_AWAKENING_MEMBER:
    321 			case IGMP_SLEEPING_MEMBER:
    322 				inm->inm_state = IGMP_SLEEPING_MEMBER;
    323 				break;
    324 			case IGMP_DELAYING_MEMBER:
    325 				if (inm->inm_rti->rti_type == IGMP_v1_ROUTER)
    326 					inm->inm_state = IGMP_LAZY_MEMBER;
    327 				else
    328 					inm->inm_state = IGMP_SLEEPING_MEMBER;
    329 				break;
    330 			}
    331 		}
    332 
    333 		break;
    334 
    335 	case IGMP_v2_HOST_MEMBERSHIP_REPORT:
    336 #ifdef MROUTING
    337 		/*
    338 		 * Make sure we don't hear our own membership report.  Fast
    339 		 * leave requires knowing that we are the only member of a
    340 		 * group.
    341 		 */
    342 		IFP_TO_IA(ifp, ia);			/* XXX */
    343 		if (ia && in_hosteq(ip->ip_src, ia->ia_addr.sin_addr))
    344 			break;
    345 #endif
    346 
    347 		++igmpstat.igps_rcv_reports;
    348 
    349 		if (ifp->if_flags & IFF_LOOPBACK)
    350 			break;
    351 
    352 		if (!IN_MULTICAST(igmp->igmp_group.s_addr) ||
    353 		    !in_hosteq(igmp->igmp_group, ip->ip_dst)) {
    354 			++igmpstat.igps_rcv_badreports;
    355 			m_freem(m);
    356 			return;
    357 		}
    358 
    359 		/*
    360 		 * KLUDGE: if the IP source address of the report has an
    361 		 * unspecified (i.e., zero) subnet number, as is allowed for
    362 		 * a booting host, replace it with the correct subnet number
    363 		 * so that a process-level multicast routing daemon can
    364 		 * determine which subnet it arrived from.  This is necessary
    365 		 * to compensate for the lack of any way for a process to
    366 		 * determine the arrival interface of an incoming packet.
    367 		 */
    368 		if ((ip->ip_src.s_addr & IN_CLASSA_NET) == 0) {
    369 #ifndef MROUTING
    370 			IFP_TO_IA(ifp, ia);		/* XXX */
    371 #endif
    372 			if (ia)
    373 				ip->ip_src.s_addr = ia->ia_subnet;
    374 		}
    375 
    376 		/*
    377 		 * If we belong to the group being reported, stop
    378 		 * our timer for that group.
    379 		 */
    380 		IN_LOOKUP_MULTI(igmp->igmp_group, ifp, inm);
    381 		if (inm != NULL) {
    382 			inm->inm_timer = 0;
    383 			++igmpstat.igps_rcv_ourreports;
    384 
    385 			switch (inm->inm_state) {
    386 			case IGMP_DELAYING_MEMBER:
    387 			case IGMP_IDLE_MEMBER:
    388 			case IGMP_AWAKENING_MEMBER:
    389 				inm->inm_state = IGMP_LAZY_MEMBER;
    390 				break;
    391 			case IGMP_LAZY_MEMBER:
    392 			case IGMP_SLEEPING_MEMBER:
    393 				break;
    394 			}
    395 		}
    396 
    397 		break;
    398 
    399 	}
    400 
    401 	/*
    402 	 * Pass all valid IGMP packets up to any process(es) listening
    403 	 * on a raw IGMP socket.
    404 	 */
    405 	rip_input(m, iphlen, proto);
    406 	return;
    407 }
    408 
    409 void
    410 igmp_joingroup(inm)
    411 	struct in_multi *inm;
    412 {
    413 	int s = splsoftnet();
    414 
    415 	inm->inm_state = IGMP_IDLE_MEMBER;
    416 
    417 	if (!IN_LOCAL_GROUP(inm->inm_addr.s_addr) &&
    418 	    (inm->inm_ifp->if_flags & IFF_LOOPBACK) == 0) {
    419 		igmp_sendpkt(inm, rti_fill(inm));
    420 		inm->inm_state = IGMP_DELAYING_MEMBER;
    421 		inm->inm_timer = IGMP_RANDOM_DELAY(
    422 		    IGMP_MAX_HOST_REPORT_DELAY * PR_FASTHZ);
    423 		igmp_timers_are_running = 1;
    424 	} else
    425 		inm->inm_timer = 0;
    426 	splx(s);
    427 }
    428 
    429 void
    430 igmp_leavegroup(inm)
    431 	struct in_multi *inm;
    432 {
    433 
    434 	switch (inm->inm_state) {
    435 	case IGMP_DELAYING_MEMBER:
    436 	case IGMP_IDLE_MEMBER:
    437 		if (!IN_LOCAL_GROUP(inm->inm_addr.s_addr) &&
    438 		    (inm->inm_ifp->if_flags & IFF_LOOPBACK) == 0)
    439 			if (inm->inm_rti->rti_type != IGMP_v1_ROUTER)
    440 				igmp_sendpkt(inm, IGMP_HOST_LEAVE_MESSAGE);
    441 		break;
    442 	case IGMP_LAZY_MEMBER:
    443 	case IGMP_AWAKENING_MEMBER:
    444 	case IGMP_SLEEPING_MEMBER:
    445 		break;
    446 	}
    447 }
    448 
    449 void
    450 igmp_fasttimo()
    451 {
    452 	struct in_multi *inm;
    453 	struct in_multistep step;
    454 	int s;
    455 
    456 	/*
    457 	 * Quick check to see if any work needs to be done, in order
    458 	 * to minimize the overhead of fasttimo processing.
    459 	 */
    460 	if (!igmp_timers_are_running)
    461 		return;
    462 
    463 	s = splsoftnet();
    464 	igmp_timers_are_running = 0;
    465 	IN_FIRST_MULTI(step, inm);
    466 	while (inm != NULL) {
    467 		if (inm->inm_timer == 0) {
    468 			/* do nothing */
    469 		} else if (--inm->inm_timer == 0) {
    470 			if (inm->inm_state == IGMP_DELAYING_MEMBER) {
    471 				if (inm->inm_rti->rti_type == IGMP_v1_ROUTER)
    472 					igmp_sendpkt(inm,
    473 					    IGMP_v1_HOST_MEMBERSHIP_REPORT);
    474 				else
    475 					igmp_sendpkt(inm,
    476 					    IGMP_v2_HOST_MEMBERSHIP_REPORT);
    477 				inm->inm_state = IGMP_IDLE_MEMBER;
    478 			}
    479 		} else {
    480 			igmp_timers_are_running = 1;
    481 		}
    482 		IN_NEXT_MULTI(step, inm);
    483 	}
    484 	splx(s);
    485 }
    486 
    487 void
    488 igmp_slowtimo()
    489 {
    490 	struct router_info *rti;
    491 	int s;
    492 
    493 	s = splsoftnet();
    494 	for (rti = rti_head; rti != 0; rti = rti->rti_next) {
    495 		if (rti->rti_type == IGMP_v1_ROUTER &&
    496 		    ++rti->rti_age >= IGMP_AGE_THRESHOLD) {
    497 			rti->rti_type = IGMP_v2_ROUTER;
    498 		}
    499 	}
    500 	splx(s);
    501 }
    502 
    503 void
    504 igmp_sendpkt(inm, type)
    505 	struct in_multi *inm;
    506 	int type;
    507 {
    508 	struct mbuf *m;
    509 	struct igmp *igmp;
    510 	struct ip *ip;
    511 	struct ip_moptions imo;
    512 #ifdef MROUTING
    513 	extern struct socket *ip_mrouter;
    514 #endif /* MROUTING */
    515 
    516 	MGETHDR(m, M_DONTWAIT, MT_HEADER);
    517 	if (m == NULL)
    518 		return;
    519 	/*
    520 	 * Assume max_linkhdr + sizeof(struct ip) + IGMP_MINLEN
    521 	 * is smaller than mbuf size returned by MGETHDR.
    522 	 */
    523 	m->m_data += max_linkhdr;
    524 	m->m_len = sizeof(struct ip) + IGMP_MINLEN;
    525 	m->m_pkthdr.len = sizeof(struct ip) + IGMP_MINLEN;
    526 
    527 	ip = mtod(m, struct ip *);
    528 	ip->ip_tos = 0;
    529 	ip->ip_len = sizeof(struct ip) + IGMP_MINLEN;
    530 	ip->ip_off = 0;
    531 	ip->ip_p = IPPROTO_IGMP;
    532 	ip->ip_src = zeroin_addr;
    533 	ip->ip_dst = inm->inm_addr;
    534 
    535 	m->m_data += sizeof(struct ip);
    536 	m->m_len -= sizeof(struct ip);
    537 	igmp = mtod(m, struct igmp *);
    538 	igmp->igmp_type = type;
    539 	igmp->igmp_code = 0;
    540 	igmp->igmp_group = inm->inm_addr;
    541 	igmp->igmp_cksum = 0;
    542 	igmp->igmp_cksum = in_cksum(m, IGMP_MINLEN);
    543 	m->m_data -= sizeof(struct ip);
    544 	m->m_len += sizeof(struct ip);
    545 
    546 	imo.imo_multicast_ifp = inm->inm_ifp;
    547 	imo.imo_multicast_ttl = 1;
    548 #ifdef RSVP_ISI
    549 	imo.imo_multicast_vif = -1;
    550 #endif
    551 	/*
    552 	 * Request loopback of the report if we are acting as a multicast
    553 	 * router, so that the process-level routing demon can hear it.
    554 	 */
    555 #ifdef MROUTING
    556 	imo.imo_multicast_loop = (ip_mrouter != NULL);
    557 #else
    558 	imo.imo_multicast_loop = 0;
    559 #endif /* MROUTING */
    560 
    561 	ip_output(m, (struct mbuf *)0, (struct route *)0, IP_MULTICASTOPTS,
    562 	    &imo);
    563 
    564 	++igmpstat.igps_snd_reports;
    565 }
    566