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