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