Home | History | Annotate | Line # | Download | only in netinet
igmp.c revision 1.6
      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  *	from: @(#)igmp.c	8.1 (Berkeley) 7/19/93
     38  *	$Id: igmp.c,v 1.6 1994/06/04 08:13:23 mycroft Exp $
     39  */
     40 
     41 /* Internet Group Management Protocol (IGMP) routines. */
     42 
     43 
     44 #include <sys/param.h>
     45 #include <sys/mbuf.h>
     46 #include <sys/socket.h>
     47 #include <sys/protosw.h>
     48 
     49 #include <net/if.h>
     50 #include <net/route.h>
     51 
     52 #include <netinet/in.h>
     53 #include <netinet/in_var.h>
     54 #include <netinet/in_systm.h>
     55 #include <netinet/ip.h>
     56 #include <netinet/ip_var.h>
     57 #include <netinet/igmp.h>
     58 #include <netinet/igmp_var.h>
     59 
     60 static int igmp_timers_are_running = 0;
     61 static u_long igmp_all_hosts_group;
     62 
     63 static void igmp_sendreport __P((struct in_multi *));
     64 
     65 void
     66 igmp_init()
     67 {
     68 	/*
     69 	 * To avoid byte-swapping the same value over and over again.
     70 	 */
     71 	igmp_all_hosts_group = htonl(INADDR_ALLHOSTS_GROUP);
     72 }
     73 
     74 void
     75 igmp_input(m, iphlen)
     76 	register struct mbuf *m;
     77 	register int iphlen;
     78 {
     79 	register struct igmp *igmp;
     80 	register struct ip *ip;
     81 	register int igmplen;
     82 	register struct ifnet *ifp = m->m_pkthdr.rcvif;
     83 	register int minlen;
     84 	register struct in_multi *inm;
     85 	register struct in_ifaddr *ia;
     86 	struct in_multistep step;
     87 
     88 	++igmpstat.igps_rcv_total;
     89 
     90 	ip = mtod(m, struct ip *);
     91 	igmplen = ip->ip_len;
     92 
     93 	/*
     94 	 * Validate lengths
     95 	 */
     96 	if (igmplen < IGMP_MINLEN) {
     97 		++igmpstat.igps_rcv_tooshort;
     98 		m_freem(m);
     99 		return;
    100 	}
    101 	minlen = iphlen + IGMP_MINLEN;
    102 	if ((m->m_flags & M_EXT || m->m_len < minlen) &&
    103 	    (m = m_pullup(m, minlen)) == 0) {
    104 		++igmpstat.igps_rcv_tooshort;
    105 		return;
    106 	}
    107 
    108 	/*
    109 	 * Validate checksum
    110 	 */
    111 	m->m_data += iphlen;
    112 	m->m_len -= iphlen;
    113 	igmp = mtod(m, struct igmp *);
    114 	if (in_cksum(m, igmplen)) {
    115 		++igmpstat.igps_rcv_badsum;
    116 		m_freem(m);
    117 		return;
    118 	}
    119 	m->m_data -= iphlen;
    120 	m->m_len += iphlen;
    121 	ip = mtod(m, struct ip *);
    122 
    123 	switch (igmp->igmp_type) {
    124 
    125 	case IGMP_HOST_MEMBERSHIP_QUERY:
    126 		++igmpstat.igps_rcv_queries;
    127 
    128 		if (ifp->if_flags & IFF_LOOPBACK)
    129 			break;
    130 
    131 		if (ip->ip_dst.s_addr != igmp_all_hosts_group) {
    132 			++igmpstat.igps_rcv_badqueries;
    133 			m_freem(m);
    134 			return;
    135 		}
    136 
    137 		/*
    138 		 * Start the timers in all of our membership records for
    139 		 * the interface on which the query arrived, except those
    140 		 * that are already running and those that belong to the
    141 		 * "all-hosts" group.
    142 		 */
    143 		IN_FIRST_MULTI(step, inm);
    144 		while (inm != NULL) {
    145 			if (inm->inm_ifp == ifp && inm->inm_timer == 0 &&
    146 			    inm->inm_addr.s_addr != igmp_all_hosts_group) {
    147 				inm->inm_timer =
    148 				    IGMP_RANDOM_DELAY(inm->inm_addr);
    149 				igmp_timers_are_running = 1;
    150 			}
    151 			IN_NEXT_MULTI(step, inm);
    152 		}
    153 
    154 		break;
    155 
    156 	case IGMP_HOST_MEMBERSHIP_REPORT:
    157 		++igmpstat.igps_rcv_reports;
    158 
    159 		if (ifp->if_flags & IFF_LOOPBACK)
    160 			break;
    161 
    162 		if (!IN_MULTICAST(ntohl(igmp->igmp_group.s_addr)) ||
    163 		    igmp->igmp_group.s_addr != ip->ip_dst.s_addr) {
    164 			++igmpstat.igps_rcv_badreports;
    165 			m_freem(m);
    166 			return;
    167 		}
    168 
    169 		/*
    170 		 * KLUDGE: if the IP source address of the report has an
    171 		 * unspecified (i.e., zero) subnet number, as is allowed for
    172 		 * a booting host, replace it with the correct subnet number
    173 		 * so that a process-level multicast routing demon can
    174 		 * determine which subnet it arrived from.  This is necessary
    175 		 * to compensate for the lack of any way for a process to
    176 		 * determine the arrival interface of an incoming packet.
    177 		 */
    178 		if ((ntohl(ip->ip_src.s_addr) & IN_CLASSA_NET) == 0) {
    179 			IFP_TO_IA(ifp, ia);
    180 			if (ia) ip->ip_src.s_addr = htonl(ia->ia_subnet);
    181 		}
    182 
    183 		/*
    184 		 * If we belong to the group being reported, stop
    185 		 * our timer for that group.
    186 		 */
    187 		IN_LOOKUP_MULTI(igmp->igmp_group, ifp, inm);
    188 		if (inm != NULL) {
    189 			inm->inm_timer = 0;
    190 			++igmpstat.igps_rcv_ourreports;
    191 		}
    192 
    193 		break;
    194 	}
    195 
    196 	/*
    197 	 * Pass all valid IGMP packets up to any process(es) listening
    198 	 * on a raw IGMP socket.
    199 	 */
    200 	rip_input(m);
    201 }
    202 
    203 void
    204 igmp_joingroup(inm)
    205 	struct in_multi *inm;
    206 {
    207 	register int s = splnet();
    208 
    209 	if (inm->inm_addr.s_addr == igmp_all_hosts_group ||
    210 	    (inm->inm_ifp->if_flags & IFF_LOOPBACK))
    211 		inm->inm_timer = 0;
    212 	else {
    213 		igmp_sendreport(inm);
    214 		inm->inm_timer = IGMP_RANDOM_DELAY(inm->inm_addr);
    215 		igmp_timers_are_running = 1;
    216 	}
    217 	splx(s);
    218 }
    219 
    220 void
    221 igmp_leavegroup(inm)
    222 	struct in_multi *inm;
    223 {
    224 	/*
    225 	 * No action required on leaving a group.
    226 	 */
    227 }
    228 
    229 void
    230 igmp_fasttimo()
    231 {
    232 	register struct in_multi *inm;
    233 	register int s;
    234 	struct in_multistep step;
    235 
    236 	/*
    237 	 * Quick check to see if any work needs to be done, in order
    238 	 * to minimize the overhead of fasttimo processing.
    239 	 */
    240 	if (!igmp_timers_are_running)
    241 		return;
    242 
    243 	s = splnet();
    244 	igmp_timers_are_running = 0;
    245 	IN_FIRST_MULTI(step, inm);
    246 	while (inm != NULL) {
    247 		if (inm->inm_timer == 0) {
    248 			/* do nothing */
    249 		} else if (--inm->inm_timer == 0) {
    250 			igmp_sendreport(inm);
    251 		} else {
    252 			igmp_timers_are_running = 1;
    253 		}
    254 		IN_NEXT_MULTI(step, inm);
    255 	}
    256 	splx(s);
    257 }
    258 
    259 static void
    260 igmp_sendreport(inm)
    261 	register struct in_multi *inm;
    262 {
    263 	register struct mbuf *m;
    264 	register struct igmp *igmp;
    265 	register struct ip *ip;
    266 	register struct ip_moptions *imo;
    267 	struct ip_moptions simo;
    268 
    269 	MGETHDR(m, M_DONTWAIT, MT_HEADER);
    270 	if (m == NULL)
    271 		return;
    272 	/*
    273 	 * Assume max_linkhdr + sizeof(struct ip) + IGMP_MINLEN
    274 	 * is smaller than mbuf size returned by MGETHDR.
    275 	 */
    276 	m->m_data += max_linkhdr;
    277 	m->m_len = sizeof(struct ip) + IGMP_MINLEN;
    278 	m->m_pkthdr.len = sizeof(struct ip) + IGMP_MINLEN;
    279 
    280 	ip = mtod(m, struct ip *);
    281 	ip->ip_tos = 0;
    282 	ip->ip_len = sizeof(struct ip) + IGMP_MINLEN;
    283 	ip->ip_off = 0;
    284 	ip->ip_p = IPPROTO_IGMP;
    285 	ip->ip_src.s_addr = INADDR_ANY;
    286 	ip->ip_dst = inm->inm_addr;
    287 
    288 	igmp = (struct igmp *)(ip + 1);
    289 	igmp->igmp_type = IGMP_HOST_MEMBERSHIP_REPORT;
    290 	igmp->igmp_code = 0;
    291 	igmp->igmp_group = inm->inm_addr;
    292 	igmp->igmp_cksum = 0;
    293 	igmp->igmp_cksum = in_cksum(m, IGMP_MINLEN);
    294 
    295 	imo = &simo;
    296 	bzero((caddr_t)imo, sizeof(*imo));
    297 	imo->imo_multicast_ifp = inm->inm_ifp;
    298 	imo->imo_multicast_ttl = 1;
    299 	/*
    300 	 * Request loopback of the report if we are acting as a multicast
    301 	 * router, so that the process-level routing demon can hear it.
    302 	 */
    303 #ifdef MROUTING
    304     {
    305 	extern struct socket *ip_mrouter;
    306 	imo->imo_multicast_loop = (ip_mrouter != NULL);
    307     }
    308 #endif
    309 	ip_output(m, NULL, NULL, 0, imo);
    310 
    311 	++igmpstat.igps_snd_reports;
    312 }
    313