Home | History | Annotate | Line # | Download | only in netinet
igmp.c revision 1.8
      1  1.8      cgd /*	$NetBSD: igmp.c,v 1.8 1994/06/29 06:37:56 cgd Exp $	*/
      2  1.8      cgd 
      3  1.1  hpeyerl /*
      4  1.1  hpeyerl  * Copyright (c) 1988 Stephen Deering.
      5  1.5  mycroft  * Copyright (c) 1992, 1993
      6  1.5  mycroft  *	The Regents of the University of California.  All rights reserved.
      7  1.1  hpeyerl  *
      8  1.1  hpeyerl  * This code is derived from software contributed to Berkeley by
      9  1.1  hpeyerl  * Stephen Deering of Stanford University.
     10  1.1  hpeyerl  *
     11  1.1  hpeyerl  * Redistribution and use in source and binary forms, with or without
     12  1.1  hpeyerl  * modification, are permitted provided that the following conditions
     13  1.1  hpeyerl  * are met:
     14  1.1  hpeyerl  * 1. Redistributions of source code must retain the above copyright
     15  1.1  hpeyerl  *    notice, this list of conditions and the following disclaimer.
     16  1.1  hpeyerl  * 2. Redistributions in binary form must reproduce the above copyright
     17  1.1  hpeyerl  *    notice, this list of conditions and the following disclaimer in the
     18  1.1  hpeyerl  *    documentation and/or other materials provided with the distribution.
     19  1.1  hpeyerl  * 3. All advertising materials mentioning features or use of this software
     20  1.1  hpeyerl  *    must display the following acknowledgement:
     21  1.1  hpeyerl  *	This product includes software developed by the University of
     22  1.1  hpeyerl  *	California, Berkeley and its contributors.
     23  1.1  hpeyerl  * 4. Neither the name of the University nor the names of its contributors
     24  1.1  hpeyerl  *    may be used to endorse or promote products derived from this software
     25  1.1  hpeyerl  *    without specific prior written permission.
     26  1.1  hpeyerl  *
     27  1.1  hpeyerl  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     28  1.1  hpeyerl  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     29  1.1  hpeyerl  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     30  1.1  hpeyerl  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     31  1.1  hpeyerl  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     32  1.1  hpeyerl  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     33  1.1  hpeyerl  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     34  1.1  hpeyerl  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     35  1.1  hpeyerl  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     36  1.1  hpeyerl  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     37  1.1  hpeyerl  * SUCH DAMAGE.
     38  1.1  hpeyerl  *
     39  1.8      cgd  *	@(#)igmp.c	8.1 (Berkeley) 7/19/93
     40  1.1  hpeyerl  */
     41  1.1  hpeyerl 
     42  1.1  hpeyerl /* Internet Group Management Protocol (IGMP) routines. */
     43  1.1  hpeyerl 
     44  1.1  hpeyerl 
     45  1.1  hpeyerl #include <sys/param.h>
     46  1.1  hpeyerl #include <sys/mbuf.h>
     47  1.1  hpeyerl #include <sys/socket.h>
     48  1.1  hpeyerl #include <sys/protosw.h>
     49  1.1  hpeyerl 
     50  1.1  hpeyerl #include <net/if.h>
     51  1.1  hpeyerl #include <net/route.h>
     52  1.1  hpeyerl 
     53  1.1  hpeyerl #include <netinet/in.h>
     54  1.1  hpeyerl #include <netinet/in_var.h>
     55  1.1  hpeyerl #include <netinet/in_systm.h>
     56  1.1  hpeyerl #include <netinet/ip.h>
     57  1.1  hpeyerl #include <netinet/ip_var.h>
     58  1.1  hpeyerl #include <netinet/igmp.h>
     59  1.1  hpeyerl #include <netinet/igmp_var.h>
     60  1.1  hpeyerl 
     61  1.1  hpeyerl static int igmp_timers_are_running = 0;
     62  1.1  hpeyerl static u_long igmp_all_hosts_group;
     63  1.1  hpeyerl 
     64  1.1  hpeyerl static void igmp_sendreport __P((struct in_multi *));
     65  1.1  hpeyerl 
     66  1.1  hpeyerl void
     67  1.1  hpeyerl igmp_init()
     68  1.1  hpeyerl {
     69  1.1  hpeyerl 	/*
     70  1.1  hpeyerl 	 * To avoid byte-swapping the same value over and over again.
     71  1.1  hpeyerl 	 */
     72  1.1  hpeyerl 	igmp_all_hosts_group = htonl(INADDR_ALLHOSTS_GROUP);
     73  1.1  hpeyerl }
     74  1.1  hpeyerl 
     75  1.1  hpeyerl void
     76  1.1  hpeyerl igmp_input(m, iphlen)
     77  1.1  hpeyerl 	register struct mbuf *m;
     78  1.1  hpeyerl 	register int iphlen;
     79  1.1  hpeyerl {
     80  1.1  hpeyerl 	register struct igmp *igmp;
     81  1.1  hpeyerl 	register struct ip *ip;
     82  1.2  mycroft 	register int igmplen;
     83  1.1  hpeyerl 	register struct ifnet *ifp = m->m_pkthdr.rcvif;
     84  1.1  hpeyerl 	register int minlen;
     85  1.1  hpeyerl 	register struct in_multi *inm;
     86  1.1  hpeyerl 	register struct in_ifaddr *ia;
     87  1.1  hpeyerl 	struct in_multistep step;
     88  1.1  hpeyerl 
     89  1.1  hpeyerl 	++igmpstat.igps_rcv_total;
     90  1.1  hpeyerl 
     91  1.1  hpeyerl 	ip = mtod(m, struct ip *);
     92  1.1  hpeyerl 	igmplen = ip->ip_len;
     93  1.1  hpeyerl 
     94  1.1  hpeyerl 	/*
     95  1.1  hpeyerl 	 * Validate lengths
     96  1.1  hpeyerl 	 */
     97  1.1  hpeyerl 	if (igmplen < IGMP_MINLEN) {
     98  1.1  hpeyerl 		++igmpstat.igps_rcv_tooshort;
     99  1.1  hpeyerl 		m_freem(m);
    100  1.1  hpeyerl 		return;
    101  1.1  hpeyerl 	}
    102  1.1  hpeyerl 	minlen = iphlen + IGMP_MINLEN;
    103  1.1  hpeyerl 	if ((m->m_flags & M_EXT || m->m_len < minlen) &&
    104  1.1  hpeyerl 	    (m = m_pullup(m, minlen)) == 0) {
    105  1.1  hpeyerl 		++igmpstat.igps_rcv_tooshort;
    106  1.1  hpeyerl 		return;
    107  1.1  hpeyerl 	}
    108  1.1  hpeyerl 
    109  1.1  hpeyerl 	/*
    110  1.1  hpeyerl 	 * Validate checksum
    111  1.1  hpeyerl 	 */
    112  1.1  hpeyerl 	m->m_data += iphlen;
    113  1.1  hpeyerl 	m->m_len -= iphlen;
    114  1.1  hpeyerl 	igmp = mtod(m, struct igmp *);
    115  1.1  hpeyerl 	if (in_cksum(m, igmplen)) {
    116  1.1  hpeyerl 		++igmpstat.igps_rcv_badsum;
    117  1.1  hpeyerl 		m_freem(m);
    118  1.1  hpeyerl 		return;
    119  1.1  hpeyerl 	}
    120  1.1  hpeyerl 	m->m_data -= iphlen;
    121  1.1  hpeyerl 	m->m_len += iphlen;
    122  1.1  hpeyerl 	ip = mtod(m, struct ip *);
    123  1.1  hpeyerl 
    124  1.1  hpeyerl 	switch (igmp->igmp_type) {
    125  1.1  hpeyerl 
    126  1.1  hpeyerl 	case IGMP_HOST_MEMBERSHIP_QUERY:
    127  1.1  hpeyerl 		++igmpstat.igps_rcv_queries;
    128  1.1  hpeyerl 
    129  1.6  mycroft 		if (ifp->if_flags & IFF_LOOPBACK)
    130  1.1  hpeyerl 			break;
    131  1.1  hpeyerl 
    132  1.1  hpeyerl 		if (ip->ip_dst.s_addr != igmp_all_hosts_group) {
    133  1.1  hpeyerl 			++igmpstat.igps_rcv_badqueries;
    134  1.1  hpeyerl 			m_freem(m);
    135  1.1  hpeyerl 			return;
    136  1.1  hpeyerl 		}
    137  1.1  hpeyerl 
    138  1.1  hpeyerl 		/*
    139  1.1  hpeyerl 		 * Start the timers in all of our membership records for
    140  1.1  hpeyerl 		 * the interface on which the query arrived, except those
    141  1.1  hpeyerl 		 * that are already running and those that belong to the
    142  1.1  hpeyerl 		 * "all-hosts" group.
    143  1.1  hpeyerl 		 */
    144  1.1  hpeyerl 		IN_FIRST_MULTI(step, inm);
    145  1.1  hpeyerl 		while (inm != NULL) {
    146  1.1  hpeyerl 			if (inm->inm_ifp == ifp && inm->inm_timer == 0 &&
    147  1.1  hpeyerl 			    inm->inm_addr.s_addr != igmp_all_hosts_group) {
    148  1.1  hpeyerl 				inm->inm_timer =
    149  1.1  hpeyerl 				    IGMP_RANDOM_DELAY(inm->inm_addr);
    150  1.1  hpeyerl 				igmp_timers_are_running = 1;
    151  1.1  hpeyerl 			}
    152  1.1  hpeyerl 			IN_NEXT_MULTI(step, inm);
    153  1.1  hpeyerl 		}
    154  1.1  hpeyerl 
    155  1.1  hpeyerl 		break;
    156  1.1  hpeyerl 
    157  1.1  hpeyerl 	case IGMP_HOST_MEMBERSHIP_REPORT:
    158  1.1  hpeyerl 		++igmpstat.igps_rcv_reports;
    159  1.1  hpeyerl 
    160  1.6  mycroft 		if (ifp->if_flags & IFF_LOOPBACK)
    161  1.1  hpeyerl 			break;
    162  1.1  hpeyerl 
    163  1.1  hpeyerl 		if (!IN_MULTICAST(ntohl(igmp->igmp_group.s_addr)) ||
    164  1.1  hpeyerl 		    igmp->igmp_group.s_addr != ip->ip_dst.s_addr) {
    165  1.1  hpeyerl 			++igmpstat.igps_rcv_badreports;
    166  1.1  hpeyerl 			m_freem(m);
    167  1.1  hpeyerl 			return;
    168  1.1  hpeyerl 		}
    169  1.1  hpeyerl 
    170  1.1  hpeyerl 		/*
    171  1.1  hpeyerl 		 * KLUDGE: if the IP source address of the report has an
    172  1.1  hpeyerl 		 * unspecified (i.e., zero) subnet number, as is allowed for
    173  1.1  hpeyerl 		 * a booting host, replace it with the correct subnet number
    174  1.1  hpeyerl 		 * so that a process-level multicast routing demon can
    175  1.1  hpeyerl 		 * determine which subnet it arrived from.  This is necessary
    176  1.1  hpeyerl 		 * to compensate for the lack of any way for a process to
    177  1.1  hpeyerl 		 * determine the arrival interface of an incoming packet.
    178  1.1  hpeyerl 		 */
    179  1.1  hpeyerl 		if ((ntohl(ip->ip_src.s_addr) & IN_CLASSA_NET) == 0) {
    180  1.1  hpeyerl 			IFP_TO_IA(ifp, ia);
    181  1.1  hpeyerl 			if (ia) ip->ip_src.s_addr = htonl(ia->ia_subnet);
    182  1.1  hpeyerl 		}
    183  1.1  hpeyerl 
    184  1.1  hpeyerl 		/*
    185  1.1  hpeyerl 		 * If we belong to the group being reported, stop
    186  1.1  hpeyerl 		 * our timer for that group.
    187  1.1  hpeyerl 		 */
    188  1.1  hpeyerl 		IN_LOOKUP_MULTI(igmp->igmp_group, ifp, inm);
    189  1.1  hpeyerl 		if (inm != NULL) {
    190  1.1  hpeyerl 			inm->inm_timer = 0;
    191  1.1  hpeyerl 			++igmpstat.igps_rcv_ourreports;
    192  1.1  hpeyerl 		}
    193  1.1  hpeyerl 
    194  1.1  hpeyerl 		break;
    195  1.1  hpeyerl 	}
    196  1.1  hpeyerl 
    197  1.1  hpeyerl 	/*
    198  1.1  hpeyerl 	 * Pass all valid IGMP packets up to any process(es) listening
    199  1.1  hpeyerl 	 * on a raw IGMP socket.
    200  1.1  hpeyerl 	 */
    201  1.1  hpeyerl 	rip_input(m);
    202  1.1  hpeyerl }
    203  1.1  hpeyerl 
    204  1.1  hpeyerl void
    205  1.1  hpeyerl igmp_joingroup(inm)
    206  1.1  hpeyerl 	struct in_multi *inm;
    207  1.1  hpeyerl {
    208  1.1  hpeyerl 	register int s = splnet();
    209  1.1  hpeyerl 
    210  1.1  hpeyerl 	if (inm->inm_addr.s_addr == igmp_all_hosts_group ||
    211  1.6  mycroft 	    (inm->inm_ifp->if_flags & IFF_LOOPBACK))
    212  1.1  hpeyerl 		inm->inm_timer = 0;
    213  1.1  hpeyerl 	else {
    214  1.1  hpeyerl 		igmp_sendreport(inm);
    215  1.1  hpeyerl 		inm->inm_timer = IGMP_RANDOM_DELAY(inm->inm_addr);
    216  1.1  hpeyerl 		igmp_timers_are_running = 1;
    217  1.1  hpeyerl 	}
    218  1.1  hpeyerl 	splx(s);
    219  1.1  hpeyerl }
    220  1.1  hpeyerl 
    221  1.1  hpeyerl void
    222  1.1  hpeyerl igmp_leavegroup(inm)
    223  1.1  hpeyerl 	struct in_multi *inm;
    224  1.1  hpeyerl {
    225  1.1  hpeyerl 	/*
    226  1.1  hpeyerl 	 * No action required on leaving a group.
    227  1.1  hpeyerl 	 */
    228  1.1  hpeyerl }
    229  1.1  hpeyerl 
    230  1.1  hpeyerl void
    231  1.1  hpeyerl igmp_fasttimo()
    232  1.1  hpeyerl {
    233  1.1  hpeyerl 	register struct in_multi *inm;
    234  1.1  hpeyerl 	register int s;
    235  1.1  hpeyerl 	struct in_multistep step;
    236  1.1  hpeyerl 
    237  1.1  hpeyerl 	/*
    238  1.1  hpeyerl 	 * Quick check to see if any work needs to be done, in order
    239  1.1  hpeyerl 	 * to minimize the overhead of fasttimo processing.
    240  1.1  hpeyerl 	 */
    241  1.1  hpeyerl 	if (!igmp_timers_are_running)
    242  1.1  hpeyerl 		return;
    243  1.1  hpeyerl 
    244  1.1  hpeyerl 	s = splnet();
    245  1.1  hpeyerl 	igmp_timers_are_running = 0;
    246  1.1  hpeyerl 	IN_FIRST_MULTI(step, inm);
    247  1.1  hpeyerl 	while (inm != NULL) {
    248  1.1  hpeyerl 		if (inm->inm_timer == 0) {
    249  1.1  hpeyerl 			/* do nothing */
    250  1.1  hpeyerl 		} else if (--inm->inm_timer == 0) {
    251  1.1  hpeyerl 			igmp_sendreport(inm);
    252  1.1  hpeyerl 		} else {
    253  1.1  hpeyerl 			igmp_timers_are_running = 1;
    254  1.1  hpeyerl 		}
    255  1.1  hpeyerl 		IN_NEXT_MULTI(step, inm);
    256  1.1  hpeyerl 	}
    257  1.1  hpeyerl 	splx(s);
    258  1.1  hpeyerl }
    259  1.1  hpeyerl 
    260  1.1  hpeyerl static void
    261  1.1  hpeyerl igmp_sendreport(inm)
    262  1.1  hpeyerl 	register struct in_multi *inm;
    263  1.1  hpeyerl {
    264  1.1  hpeyerl 	register struct mbuf *m;
    265  1.1  hpeyerl 	register struct igmp *igmp;
    266  1.1  hpeyerl 	register struct ip *ip;
    267  1.1  hpeyerl 	register struct ip_moptions *imo;
    268  1.1  hpeyerl 	struct ip_moptions simo;
    269  1.1  hpeyerl 
    270  1.1  hpeyerl 	MGETHDR(m, M_DONTWAIT, MT_HEADER);
    271  1.1  hpeyerl 	if (m == NULL)
    272  1.1  hpeyerl 		return;
    273  1.1  hpeyerl 	/*
    274  1.1  hpeyerl 	 * Assume max_linkhdr + sizeof(struct ip) + IGMP_MINLEN
    275  1.1  hpeyerl 	 * is smaller than mbuf size returned by MGETHDR.
    276  1.1  hpeyerl 	 */
    277  1.1  hpeyerl 	m->m_data += max_linkhdr;
    278  1.1  hpeyerl 	m->m_len = sizeof(struct ip) + IGMP_MINLEN;
    279  1.1  hpeyerl 	m->m_pkthdr.len = sizeof(struct ip) + IGMP_MINLEN;
    280  1.1  hpeyerl 
    281  1.1  hpeyerl 	ip = mtod(m, struct ip *);
    282  1.1  hpeyerl 	ip->ip_tos = 0;
    283  1.1  hpeyerl 	ip->ip_len = sizeof(struct ip) + IGMP_MINLEN;
    284  1.1  hpeyerl 	ip->ip_off = 0;
    285  1.1  hpeyerl 	ip->ip_p = IPPROTO_IGMP;
    286  1.1  hpeyerl 	ip->ip_src.s_addr = INADDR_ANY;
    287  1.1  hpeyerl 	ip->ip_dst = inm->inm_addr;
    288  1.1  hpeyerl 
    289  1.7   brezak 	m->m_data += sizeof(struct ip);
    290  1.7   brezak 	m->m_len -= sizeof(struct ip);
    291  1.7   brezak 	igmp = mtod(m, struct igmp *);
    292  1.1  hpeyerl 	igmp->igmp_type = IGMP_HOST_MEMBERSHIP_REPORT;
    293  1.1  hpeyerl 	igmp->igmp_code = 0;
    294  1.1  hpeyerl 	igmp->igmp_group = inm->inm_addr;
    295  1.1  hpeyerl 	igmp->igmp_cksum = 0;
    296  1.1  hpeyerl 	igmp->igmp_cksum = in_cksum(m, IGMP_MINLEN);
    297  1.7   brezak 	m->m_data -= sizeof(struct ip);
    298  1.7   brezak 	m->m_len += sizeof(struct ip);
    299  1.1  hpeyerl 
    300  1.1  hpeyerl 	imo = &simo;
    301  1.1  hpeyerl 	bzero((caddr_t)imo, sizeof(*imo));
    302  1.1  hpeyerl 	imo->imo_multicast_ifp = inm->inm_ifp;
    303  1.1  hpeyerl 	imo->imo_multicast_ttl = 1;
    304  1.1  hpeyerl 	/*
    305  1.1  hpeyerl 	 * Request loopback of the report if we are acting as a multicast
    306  1.1  hpeyerl 	 * router, so that the process-level routing demon can hear it.
    307  1.1  hpeyerl 	 */
    308  1.1  hpeyerl #ifdef MROUTING
    309  1.5  mycroft     {
    310  1.5  mycroft 	extern struct socket *ip_mrouter;
    311  1.1  hpeyerl 	imo->imo_multicast_loop = (ip_mrouter != NULL);
    312  1.5  mycroft     }
    313  1.1  hpeyerl #endif
    314  1.3  mycroft 	ip_output(m, NULL, NULL, 0, imo);
    315  1.1  hpeyerl 
    316  1.1  hpeyerl 	++igmpstat.igps_snd_reports;
    317  1.1  hpeyerl }
    318