Home | History | Annotate | Line # | Download | only in netinet
igmp.c revision 1.62
      1  1.62     ozaki /*	$NetBSD: igmp.c,v 1.62 2016/08/01 03:15:30 ozaki-r Exp $	*/
      2  1.21    itojun 
      3  1.21    itojun /*
      4  1.21    itojun  * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
      5  1.21    itojun  * All rights reserved.
      6  1.29    itojun  *
      7  1.21    itojun  * Redistribution and use in source and binary forms, with or without
      8  1.21    itojun  * modification, are permitted provided that the following conditions
      9  1.21    itojun  * are met:
     10  1.21    itojun  * 1. Redistributions of source code must retain the above copyright
     11  1.21    itojun  *    notice, this list of conditions and the following disclaimer.
     12  1.21    itojun  * 2. Redistributions in binary form must reproduce the above copyright
     13  1.21    itojun  *    notice, this list of conditions and the following disclaimer in the
     14  1.21    itojun  *    documentation and/or other materials provided with the distribution.
     15  1.21    itojun  * 3. Neither the name of the project nor the names of its contributors
     16  1.21    itojun  *    may be used to endorse or promote products derived from this software
     17  1.21    itojun  *    without specific prior written permission.
     18  1.29    itojun  *
     19  1.21    itojun  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
     20  1.21    itojun  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     21  1.21    itojun  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     22  1.21    itojun  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
     23  1.21    itojun  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     24  1.21    itojun  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     25  1.21    itojun  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     26  1.21    itojun  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     27  1.21    itojun  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     28  1.21    itojun  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     29  1.21    itojun  * SUCH DAMAGE.
     30  1.21    itojun  */
     31   1.8       cgd 
     32   1.1   hpeyerl /*
     33  1.10   mycroft  * Internet Group Management Protocol (IGMP) routines.
     34   1.1   hpeyerl  *
     35  1.10   mycroft  * Written by Steve Deering, Stanford, May 1988.
     36  1.10   mycroft  * Modified by Rosen Sharma, Stanford, Aug 1994.
     37  1.10   mycroft  * Modified by Bill Fenner, Xerox PARC, Feb 1995.
     38   1.1   hpeyerl  *
     39  1.10   mycroft  * MULTICAST Revision: 1.3
     40   1.1   hpeyerl  */
     41  1.27     lukem 
     42  1.27     lukem #include <sys/cdefs.h>
     43  1.62     ozaki __KERNEL_RCSID(0, "$NetBSD: igmp.c,v 1.62 2016/08/01 03:15:30 ozaki-r Exp $");
     44  1.17    scottr 
     45  1.56     pooka #ifdef _KERNEL_OPT
     46  1.17    scottr #include "opt_mrouting.h"
     47  1.56     pooka #endif
     48   1.1   hpeyerl 
     49   1.1   hpeyerl #include <sys/param.h>
     50   1.1   hpeyerl #include <sys/mbuf.h>
     51   1.1   hpeyerl #include <sys/socket.h>
     52  1.48        ad #include <sys/socketvar.h>
     53   1.1   hpeyerl #include <sys/protosw.h>
     54  1.15  christos #include <sys/systm.h>
     55  1.55     rmind #include <sys/cprng.h>
     56  1.46   thorpej #include <sys/sysctl.h>
     57   1.1   hpeyerl 
     58   1.1   hpeyerl #include <net/if.h>
     59  1.47   thorpej #include <net/net_stats.h>
     60   1.1   hpeyerl 
     61   1.1   hpeyerl #include <netinet/in.h>
     62   1.1   hpeyerl #include <netinet/in_var.h>
     63   1.1   hpeyerl #include <netinet/in_systm.h>
     64   1.1   hpeyerl #include <netinet/ip.h>
     65   1.1   hpeyerl #include <netinet/ip_var.h>
     66   1.1   hpeyerl #include <netinet/igmp.h>
     67   1.1   hpeyerl #include <netinet/igmp_var.h>
     68   1.1   hpeyerl 
     69  1.55     rmind /*
     70  1.55     rmind  * Per-interface router version information.
     71  1.55     rmind  */
     72  1.55     rmind typedef struct router_info {
     73  1.55     rmind 	LIST_ENTRY(router_info) rti_link;
     74  1.55     rmind 	ifnet_t *	rti_ifp;
     75  1.55     rmind 	int		rti_type;	/* type of router on this interface */
     76  1.55     rmind 	int		rti_age;	/* time since last v1 query */
     77  1.55     rmind } router_info_t;
     78  1.10   mycroft 
     79  1.55     rmind /*
     80  1.55     rmind  * The router-info list and the timer flag are protected by in_multilock.
     81  1.55     rmind  *
     82  1.55     rmind  * Lock order:
     83  1.55     rmind  *
     84  1.55     rmind  *	softnet_lock ->
     85  1.55     rmind  *		in_multilock
     86  1.55     rmind  */
     87  1.55     rmind static struct pool	igmp_rti_pool		__cacheline_aligned;
     88  1.55     rmind static LIST_HEAD(, router_info)	rti_head	__cacheline_aligned;
     89  1.55     rmind static int		igmp_timers_on		__cacheline_aligned;
     90  1.55     rmind static percpu_t *	igmpstat_percpu		__read_mostly;
     91  1.46   thorpej 
     92  1.47   thorpej #define	IGMP_STATINC(x)		_NET_STATINC(igmpstat_percpu, x)
     93  1.46   thorpej 
     94  1.55     rmind static void		igmp_sendpkt(struct in_multi *, int);
     95  1.55     rmind static int		rti_fill(struct in_multi *);
     96  1.55     rmind static router_info_t *	rti_find(struct ifnet *);
     97  1.55     rmind static void		rti_delete(struct ifnet *);
     98  1.55     rmind static void		sysctl_net_inet_igmp_setup(struct sysctllog **);
     99  1.51     pooka 
    100  1.55     rmind /*
    101  1.55     rmind  * rti_fill: associate router information with the given multicast group;
    102  1.55     rmind  * if there is no router information for the interface, then create it.
    103  1.55     rmind  */
    104  1.10   mycroft static int
    105  1.41     perry rti_fill(struct in_multi *inm)
    106  1.10   mycroft {
    107  1.55     rmind 	router_info_t *rti;
    108  1.55     rmind 
    109  1.55     rmind 	KASSERT(in_multi_lock_held());
    110  1.10   mycroft 
    111  1.33      matt 	LIST_FOREACH(rti, &rti_head, rti_link) {
    112  1.10   mycroft 		if (rti->rti_ifp == inm->inm_ifp) {
    113  1.10   mycroft 			inm->inm_rti = rti;
    114  1.55     rmind 			return rti->rti_type == IGMP_v1_ROUTER ?
    115  1.55     rmind 			    IGMP_v1_HOST_MEMBERSHIP_REPORT :
    116  1.55     rmind 			    IGMP_v2_HOST_MEMBERSHIP_REPORT;
    117  1.10   mycroft 		}
    118  1.10   mycroft 	}
    119  1.33      matt 	rti = pool_get(&igmp_rti_pool, PR_NOWAIT);
    120  1.55     rmind 	if (rti == NULL) {
    121  1.33      matt 		return 0;
    122  1.55     rmind 	}
    123  1.10   mycroft 	rti->rti_ifp = inm->inm_ifp;
    124  1.10   mycroft 	rti->rti_type = IGMP_v2_ROUTER;
    125  1.33      matt 	LIST_INSERT_HEAD(&rti_head, rti, rti_link);
    126  1.10   mycroft 	inm->inm_rti = rti;
    127  1.55     rmind 	return IGMP_v2_HOST_MEMBERSHIP_REPORT;
    128  1.10   mycroft }
    129  1.10   mycroft 
    130  1.55     rmind /*
    131  1.55     rmind  * rti_find: lookup or create router information for the given interface.
    132  1.55     rmind  */
    133  1.55     rmind static router_info_t *
    134  1.55     rmind rti_find(ifnet_t *ifp)
    135  1.10   mycroft {
    136  1.55     rmind 	router_info_t *rti;
    137  1.55     rmind 
    138  1.55     rmind 	KASSERT(in_multi_lock_held());
    139  1.10   mycroft 
    140  1.33      matt 	LIST_FOREACH(rti, &rti_head, rti_link) {
    141  1.10   mycroft 		if (rti->rti_ifp == ifp)
    142  1.55     rmind 			return rti;
    143  1.10   mycroft 	}
    144  1.33      matt 	rti = pool_get(&igmp_rti_pool, PR_NOWAIT);
    145  1.43       tls 	if (rti == NULL) {
    146  1.33      matt 		return NULL;
    147  1.43       tls 	}
    148  1.10   mycroft 	rti->rti_ifp = ifp;
    149  1.10   mycroft 	rti->rti_type = IGMP_v2_ROUTER;
    150  1.33      matt 	LIST_INSERT_HEAD(&rti_head, rti, rti_link);
    151  1.55     rmind 	return rti;
    152   1.1   hpeyerl }
    153   1.1   hpeyerl 
    154  1.55     rmind /*
    155  1.55     rmind  * rti_delete: remove and free the router information entry for the
    156  1.55     rmind  * given interface.
    157  1.55     rmind  */
    158  1.34    itojun static void
    159  1.55     rmind rti_delete(ifnet_t *ifp)
    160  1.34    itojun {
    161  1.55     rmind 	router_info_t *rti;
    162  1.55     rmind 
    163  1.55     rmind 	KASSERT(in_multi_lock_held());
    164  1.34    itojun 
    165  1.34    itojun 	LIST_FOREACH(rti, &rti_head, rti_link) {
    166  1.34    itojun 		if (rti->rti_ifp == ifp) {
    167  1.34    itojun 			LIST_REMOVE(rti, rti_link);
    168  1.34    itojun 			pool_put(&igmp_rti_pool, rti);
    169  1.55     rmind 			break;
    170  1.34    itojun 		}
    171  1.34    itojun 	}
    172  1.34    itojun }
    173  1.34    itojun 
    174   1.1   hpeyerl void
    175  1.46   thorpej igmp_init(void)
    176  1.46   thorpej {
    177  1.55     rmind 	pool_init(&igmp_rti_pool, sizeof(router_info_t), 0, 0, 0,
    178  1.50     pooka 	    "igmppl", NULL, IPL_SOFTNET);
    179  1.46   thorpej 	igmpstat_percpu = percpu_alloc(sizeof(uint64_t) * IGMP_NSTATS);
    180  1.55     rmind 	sysctl_net_inet_igmp_setup(NULL);
    181  1.55     rmind 	LIST_INIT(&rti_head);
    182  1.46   thorpej }
    183  1.46   thorpej 
    184  1.46   thorpej void
    185  1.15  christos igmp_input(struct mbuf *m, ...)
    186  1.15  christos {
    187  1.58     ozaki 	ifnet_t *ifp;
    188  1.24  augustss 	struct ip *ip = mtod(m, struct ip *);
    189  1.24  augustss 	struct igmp *igmp;
    190  1.55     rmind 	u_int minlen, timer;
    191  1.10   mycroft 	struct in_multi *inm;
    192  1.24  augustss 	struct in_ifaddr *ia;
    193  1.55     rmind 	int proto, ip_len, iphlen;
    194  1.15  christos 	va_list ap;
    195  1.58     ozaki 	struct psref psref;
    196  1.15  christos 
    197  1.15  christos 	va_start(ap, m);
    198  1.15  christos 	iphlen = va_arg(ap, int);
    199  1.21    itojun 	proto = va_arg(ap, int);
    200  1.15  christos 	va_end(ap);
    201   1.1   hpeyerl 
    202  1.46   thorpej 	IGMP_STATINC(IGMP_STAT_RCV_TOTAL);
    203   1.1   hpeyerl 
    204   1.1   hpeyerl 	/*
    205   1.1   hpeyerl 	 * Validate lengths
    206   1.1   hpeyerl 	 */
    207  1.19   mycroft 	minlen = iphlen + IGMP_MINLEN;
    208  1.31    itojun 	ip_len = ntohs(ip->ip_len);
    209  1.31    itojun 	if (ip_len < minlen) {
    210  1.46   thorpej 		IGMP_STATINC(IGMP_STAT_RCV_TOOSHORT);
    211   1.1   hpeyerl 		m_freem(m);
    212   1.1   hpeyerl 		return;
    213   1.1   hpeyerl 	}
    214  1.25      matt 	if (((m->m_flags & M_EXT) && (ip->ip_src.s_addr & IN_CLASSA_NET) == 0)
    215  1.25      matt 	    || m->m_len < minlen) {
    216  1.53  liamjfoy 		if ((m = m_pullup(m, minlen)) == NULL) {
    217  1.46   thorpej 			IGMP_STATINC(IGMP_STAT_RCV_TOOSHORT);
    218  1.25      matt 			return;
    219  1.25      matt 		}
    220  1.25      matt 		ip = mtod(m, struct ip *);
    221   1.1   hpeyerl 	}
    222   1.1   hpeyerl 
    223   1.1   hpeyerl 	/*
    224   1.1   hpeyerl 	 * Validate checksum
    225   1.1   hpeyerl 	 */
    226   1.1   hpeyerl 	m->m_data += iphlen;
    227   1.1   hpeyerl 	m->m_len -= iphlen;
    228   1.1   hpeyerl 	igmp = mtod(m, struct igmp *);
    229  1.30   thorpej 	/* No need to assert alignment here. */
    230  1.31    itojun 	if (in_cksum(m, ip_len - iphlen)) {
    231  1.46   thorpej 		IGMP_STATINC(IGMP_STAT_RCV_BADSUM);
    232   1.1   hpeyerl 		m_freem(m);
    233   1.1   hpeyerl 		return;
    234   1.1   hpeyerl 	}
    235   1.1   hpeyerl 	m->m_data -= iphlen;
    236   1.1   hpeyerl 	m->m_len += iphlen;
    237   1.1   hpeyerl 
    238  1.58     ozaki 	ifp = m_get_rcvif_psref(m, &psref);
    239  1.60     ozaki 	if (__predict_false(ifp == NULL))
    240  1.60     ozaki 		goto drop;
    241  1.60     ozaki 
    242   1.1   hpeyerl 	switch (igmp->igmp_type) {
    243   1.1   hpeyerl 
    244   1.1   hpeyerl 	case IGMP_HOST_MEMBERSHIP_QUERY:
    245  1.46   thorpej 		IGMP_STATINC(IGMP_STAT_RCV_QUERIES);
    246   1.1   hpeyerl 
    247   1.6   mycroft 		if (ifp->if_flags & IFF_LOOPBACK)
    248   1.1   hpeyerl 			break;
    249   1.1   hpeyerl 
    250  1.10   mycroft 		if (igmp->igmp_code == 0) {
    251  1.55     rmind 			struct in_multistep step;
    252  1.55     rmind 			router_info_t *rti;
    253  1.10   mycroft 
    254  1.12   mycroft 			if (ip->ip_dst.s_addr != INADDR_ALLHOSTS_GROUP) {
    255  1.46   thorpej 				IGMP_STATINC(IGMP_STAT_RCV_BADQUERIES);
    256  1.58     ozaki 				goto drop;
    257  1.10   mycroft 			}
    258  1.10   mycroft 
    259  1.55     rmind 			in_multi_lock(RW_WRITER);
    260  1.55     rmind 			rti = rti_find(ifp);
    261  1.55     rmind 			if (rti == NULL) {
    262  1.55     rmind 				in_multi_unlock();
    263  1.55     rmind 				break;
    264  1.55     rmind 			}
    265  1.55     rmind 			rti->rti_type = IGMP_v1_ROUTER;
    266  1.55     rmind 			rti->rti_age = 0;
    267  1.55     rmind 
    268  1.10   mycroft 			/*
    269  1.10   mycroft 			 * Start the timers in all of our membership records
    270  1.10   mycroft 			 * for the interface on which the query arrived,
    271  1.10   mycroft 			 * except those that are already running and those
    272  1.10   mycroft 			 * that belong to a "local" group (224.0.0.X).
    273  1.10   mycroft 			 */
    274  1.55     rmind 
    275  1.55     rmind 			inm = in_first_multi(&step);
    276  1.10   mycroft 			while (inm != NULL) {
    277  1.10   mycroft 				if (inm->inm_ifp == ifp &&
    278  1.10   mycroft 				    inm->inm_timer == 0 &&
    279  1.10   mycroft 				    !IN_LOCAL_GROUP(inm->inm_addr.s_addr)) {
    280  1.10   mycroft 					inm->inm_state = IGMP_DELAYING_MEMBER;
    281  1.10   mycroft 					inm->inm_timer = IGMP_RANDOM_DELAY(
    282  1.10   mycroft 					    IGMP_MAX_HOST_REPORT_DELAY * PR_FASTHZ);
    283  1.55     rmind 					igmp_timers_on = true;
    284  1.10   mycroft 				}
    285  1.55     rmind 				inm = in_next_multi(&step);
    286  1.10   mycroft 			}
    287  1.55     rmind 			in_multi_unlock();
    288  1.10   mycroft 		} else {
    289  1.55     rmind 			struct in_multistep step;
    290  1.55     rmind 
    291  1.12   mycroft 			if (!IN_MULTICAST(ip->ip_dst.s_addr)) {
    292  1.46   thorpej 				IGMP_STATINC(IGMP_STAT_RCV_BADQUERIES);
    293  1.58     ozaki 				goto drop;
    294  1.10   mycroft 			}
    295  1.10   mycroft 
    296  1.10   mycroft 			timer = igmp->igmp_code * PR_FASTHZ / IGMP_TIMER_SCALE;
    297  1.20       hwr 			if (timer == 0)
    298  1.55     rmind 				timer = 1;
    299  1.10   mycroft 
    300  1.10   mycroft 			/*
    301  1.10   mycroft 			 * Start the timers in all of our membership records
    302  1.10   mycroft 			 * for the interface on which the query arrived,
    303  1.10   mycroft 			 * except those that are already running and those
    304  1.10   mycroft 			 * that belong to a "local" group (224.0.0.X).  For
    305  1.10   mycroft 			 * timers already running, check if they need to be
    306  1.10   mycroft 			 * reset.
    307  1.10   mycroft 			 */
    308  1.55     rmind 			in_multi_lock(RW_WRITER);
    309  1.55     rmind 			inm = in_first_multi(&step);
    310  1.10   mycroft 			while (inm != NULL) {
    311  1.10   mycroft 				if (inm->inm_ifp == ifp &&
    312  1.10   mycroft 				    !IN_LOCAL_GROUP(inm->inm_addr.s_addr) &&
    313  1.12   mycroft 				    (ip->ip_dst.s_addr == INADDR_ALLHOSTS_GROUP ||
    314  1.16   mycroft 				     in_hosteq(ip->ip_dst, inm->inm_addr))) {
    315  1.10   mycroft 					switch (inm->inm_state) {
    316  1.10   mycroft 					case IGMP_DELAYING_MEMBER:
    317  1.10   mycroft 						if (inm->inm_timer <= timer)
    318  1.10   mycroft 							break;
    319  1.10   mycroft 						/* FALLTHROUGH */
    320  1.10   mycroft 					case IGMP_IDLE_MEMBER:
    321  1.10   mycroft 					case IGMP_LAZY_MEMBER:
    322  1.10   mycroft 					case IGMP_AWAKENING_MEMBER:
    323  1.10   mycroft 						inm->inm_state =
    324  1.10   mycroft 						    IGMP_DELAYING_MEMBER;
    325  1.10   mycroft 						inm->inm_timer =
    326  1.10   mycroft 						    IGMP_RANDOM_DELAY(timer);
    327  1.55     rmind 						igmp_timers_on = true;
    328  1.10   mycroft 						break;
    329  1.10   mycroft 					case IGMP_SLEEPING_MEMBER:
    330  1.10   mycroft 						inm->inm_state =
    331  1.10   mycroft 						    IGMP_AWAKENING_MEMBER;
    332  1.10   mycroft 						break;
    333  1.10   mycroft 					}
    334  1.10   mycroft 				}
    335  1.55     rmind 				inm = in_next_multi(&step);
    336  1.10   mycroft 			}
    337  1.55     rmind 			in_multi_unlock();
    338  1.10   mycroft 		}
    339  1.10   mycroft 
    340  1.10   mycroft 		break;
    341  1.10   mycroft 
    342  1.10   mycroft 	case IGMP_v1_HOST_MEMBERSHIP_REPORT:
    343  1.46   thorpej 		IGMP_STATINC(IGMP_STAT_RCV_REPORTS);
    344  1.10   mycroft 
    345  1.10   mycroft 		if (ifp->if_flags & IFF_LOOPBACK)
    346  1.10   mycroft 			break;
    347  1.10   mycroft 
    348  1.12   mycroft 		if (!IN_MULTICAST(igmp->igmp_group.s_addr) ||
    349  1.16   mycroft 		    !in_hosteq(igmp->igmp_group, ip->ip_dst)) {
    350  1.46   thorpej 			IGMP_STATINC(IGMP_STAT_RCV_BADREPORTS);
    351  1.58     ozaki 			goto drop;
    352   1.1   hpeyerl 		}
    353   1.1   hpeyerl 
    354   1.1   hpeyerl 		/*
    355  1.10   mycroft 		 * KLUDGE: if the IP source address of the report has an
    356  1.10   mycroft 		 * unspecified (i.e., zero) subnet number, as is allowed for
    357  1.10   mycroft 		 * a booting host, replace it with the correct subnet number
    358  1.10   mycroft 		 * so that a process-level multicast routing daemon can
    359  1.10   mycroft 		 * determine which subnet it arrived from.  This is necessary
    360  1.10   mycroft 		 * to compensate for the lack of any way for a process to
    361  1.10   mycroft 		 * determine the arrival interface of an incoming packet.
    362   1.1   hpeyerl 		 */
    363  1.12   mycroft 		if ((ip->ip_src.s_addr & IN_CLASSA_NET) == 0) {
    364  1.62     ozaki 			int s = pserialize_read_enter();
    365  1.62     ozaki 			ia = in_get_ia_from_ifp(ifp); /* XXX */
    366  1.10   mycroft 			if (ia)
    367  1.12   mycroft 				ip->ip_src.s_addr = ia->ia_subnet;
    368  1.62     ozaki 			pserialize_read_exit(s);
    369  1.10   mycroft 		}
    370  1.10   mycroft 
    371  1.10   mycroft 		/*
    372  1.10   mycroft 		 * If we belong to the group being reported, stop
    373  1.10   mycroft 		 * our timer for that group.
    374  1.10   mycroft 		 */
    375  1.55     rmind 		in_multi_lock(RW_WRITER);
    376  1.55     rmind 		inm = in_lookup_multi(igmp->igmp_group, ifp);
    377  1.10   mycroft 		if (inm != NULL) {
    378  1.10   mycroft 			inm->inm_timer = 0;
    379  1.46   thorpej 			IGMP_STATINC(IGMP_STAT_RCV_OURREPORTS);
    380  1.10   mycroft 
    381  1.10   mycroft 			switch (inm->inm_state) {
    382  1.10   mycroft 			case IGMP_IDLE_MEMBER:
    383  1.10   mycroft 			case IGMP_LAZY_MEMBER:
    384  1.10   mycroft 			case IGMP_AWAKENING_MEMBER:
    385  1.10   mycroft 			case IGMP_SLEEPING_MEMBER:
    386  1.10   mycroft 				inm->inm_state = IGMP_SLEEPING_MEMBER;
    387  1.10   mycroft 				break;
    388  1.10   mycroft 			case IGMP_DELAYING_MEMBER:
    389  1.10   mycroft 				if (inm->inm_rti->rti_type == IGMP_v1_ROUTER)
    390  1.10   mycroft 					inm->inm_state = IGMP_LAZY_MEMBER;
    391  1.10   mycroft 				else
    392  1.10   mycroft 					inm->inm_state = IGMP_SLEEPING_MEMBER;
    393  1.10   mycroft 				break;
    394   1.1   hpeyerl 			}
    395   1.1   hpeyerl 		}
    396  1.55     rmind 		in_multi_unlock();
    397   1.1   hpeyerl 		break;
    398   1.1   hpeyerl 
    399  1.62     ozaki 	case IGMP_v2_HOST_MEMBERSHIP_REPORT: {
    400  1.62     ozaki 		int s = pserialize_read_enter();
    401  1.10   mycroft #ifdef MROUTING
    402  1.10   mycroft 		/*
    403  1.10   mycroft 		 * Make sure we don't hear our own membership report.  Fast
    404  1.10   mycroft 		 * leave requires knowing that we are the only member of a
    405  1.10   mycroft 		 * group.
    406  1.10   mycroft 		 */
    407  1.62     ozaki 		ia = in_get_ia_from_ifp(ifp);	/* XXX */
    408  1.62     ozaki 		if (ia && in_hosteq(ip->ip_src, ia->ia_addr.sin_addr)) {
    409  1.62     ozaki 			pserialize_read_exit(s);
    410  1.10   mycroft 			break;
    411  1.62     ozaki 		}
    412  1.10   mycroft #endif
    413  1.10   mycroft 
    414  1.46   thorpej 		IGMP_STATINC(IGMP_STAT_RCV_REPORTS);
    415   1.1   hpeyerl 
    416  1.62     ozaki 		if (ifp->if_flags & IFF_LOOPBACK) {
    417  1.62     ozaki 			pserialize_read_exit(s);
    418   1.1   hpeyerl 			break;
    419  1.62     ozaki 		}
    420   1.1   hpeyerl 
    421  1.12   mycroft 		if (!IN_MULTICAST(igmp->igmp_group.s_addr) ||
    422  1.16   mycroft 		    !in_hosteq(igmp->igmp_group, ip->ip_dst)) {
    423  1.46   thorpej 			IGMP_STATINC(IGMP_STAT_RCV_BADREPORTS);
    424  1.62     ozaki 			pserialize_read_exit(s);
    425  1.58     ozaki 			goto drop;
    426   1.1   hpeyerl 		}
    427   1.1   hpeyerl 
    428   1.1   hpeyerl 		/*
    429   1.1   hpeyerl 		 * KLUDGE: if the IP source address of the report has an
    430   1.1   hpeyerl 		 * unspecified (i.e., zero) subnet number, as is allowed for
    431   1.1   hpeyerl 		 * a booting host, replace it with the correct subnet number
    432  1.10   mycroft 		 * so that a process-level multicast routing daemon can
    433   1.1   hpeyerl 		 * determine which subnet it arrived from.  This is necessary
    434   1.1   hpeyerl 		 * to compensate for the lack of any way for a process to
    435   1.1   hpeyerl 		 * determine the arrival interface of an incoming packet.
    436   1.1   hpeyerl 		 */
    437  1.12   mycroft 		if ((ip->ip_src.s_addr & IN_CLASSA_NET) == 0) {
    438  1.10   mycroft #ifndef MROUTING
    439  1.62     ozaki 			ia = in_get_ia_from_ifp(ifp); /* XXX */
    440  1.10   mycroft #endif
    441  1.10   mycroft 			if (ia)
    442  1.12   mycroft 				ip->ip_src.s_addr = ia->ia_subnet;
    443   1.1   hpeyerl 		}
    444  1.62     ozaki 		pserialize_read_exit(s);
    445   1.1   hpeyerl 
    446   1.1   hpeyerl 		/*
    447   1.1   hpeyerl 		 * If we belong to the group being reported, stop
    448   1.1   hpeyerl 		 * our timer for that group.
    449   1.1   hpeyerl 		 */
    450  1.55     rmind 		in_multi_lock(RW_WRITER);
    451  1.55     rmind 		inm = in_lookup_multi(igmp->igmp_group, ifp);
    452   1.1   hpeyerl 		if (inm != NULL) {
    453   1.1   hpeyerl 			inm->inm_timer = 0;
    454  1.46   thorpej 			IGMP_STATINC(IGMP_STAT_RCV_OURREPORTS);
    455  1.10   mycroft 
    456  1.10   mycroft 			switch (inm->inm_state) {
    457  1.10   mycroft 			case IGMP_DELAYING_MEMBER:
    458  1.10   mycroft 			case IGMP_IDLE_MEMBER:
    459  1.10   mycroft 			case IGMP_AWAKENING_MEMBER:
    460  1.10   mycroft 				inm->inm_state = IGMP_LAZY_MEMBER;
    461  1.10   mycroft 				break;
    462  1.10   mycroft 			case IGMP_LAZY_MEMBER:
    463  1.10   mycroft 			case IGMP_SLEEPING_MEMBER:
    464  1.10   mycroft 				break;
    465  1.10   mycroft 			}
    466   1.1   hpeyerl 		}
    467  1.55     rmind 		in_multi_unlock();
    468   1.1   hpeyerl 		break;
    469  1.62     ozaki 	    }
    470   1.1   hpeyerl 	}
    471  1.58     ozaki 	m_put_rcvif_psref(ifp, &psref);
    472   1.1   hpeyerl 
    473   1.1   hpeyerl 	/*
    474   1.1   hpeyerl 	 * Pass all valid IGMP packets up to any process(es) listening
    475   1.1   hpeyerl 	 * on a raw IGMP socket.
    476   1.1   hpeyerl 	 */
    477  1.21    itojun 	rip_input(m, iphlen, proto);
    478  1.21    itojun 	return;
    479  1.58     ozaki 
    480  1.58     ozaki drop:
    481  1.58     ozaki 	m_put_rcvif_psref(ifp, &psref);
    482  1.58     ozaki 	m_freem(m);
    483  1.58     ozaki 	return;
    484   1.1   hpeyerl }
    485   1.1   hpeyerl 
    486  1.33      matt int
    487  1.41     perry igmp_joingroup(struct in_multi *inm)
    488   1.1   hpeyerl {
    489  1.55     rmind 	KASSERT(in_multi_lock_held());
    490  1.10   mycroft 	inm->inm_state = IGMP_IDLE_MEMBER;
    491   1.1   hpeyerl 
    492  1.10   mycroft 	if (!IN_LOCAL_GROUP(inm->inm_addr.s_addr) &&
    493  1.10   mycroft 	    (inm->inm_ifp->if_flags & IFF_LOOPBACK) == 0) {
    494  1.55     rmind 		int report_type;
    495  1.55     rmind 
    496  1.33      matt 		report_type = rti_fill(inm);
    497  1.39  christos 		if (report_type == 0) {
    498  1.33      matt 			return ENOMEM;
    499  1.39  christos 		}
    500  1.33      matt 		igmp_sendpkt(inm, report_type);
    501  1.10   mycroft 		inm->inm_state = IGMP_DELAYING_MEMBER;
    502  1.10   mycroft 		inm->inm_timer = IGMP_RANDOM_DELAY(
    503  1.10   mycroft 		    IGMP_MAX_HOST_REPORT_DELAY * PR_FASTHZ);
    504  1.55     rmind 		igmp_timers_on = true;
    505  1.10   mycroft 	} else
    506   1.1   hpeyerl 		inm->inm_timer = 0;
    507  1.55     rmind 
    508  1.33      matt 	return 0;
    509   1.1   hpeyerl }
    510   1.1   hpeyerl 
    511   1.1   hpeyerl void
    512  1.41     perry igmp_leavegroup(struct in_multi *inm)
    513   1.1   hpeyerl {
    514  1.55     rmind 	KASSERT(in_multi_lock_held());
    515  1.10   mycroft 
    516  1.10   mycroft 	switch (inm->inm_state) {
    517  1.10   mycroft 	case IGMP_DELAYING_MEMBER:
    518  1.10   mycroft 	case IGMP_IDLE_MEMBER:
    519  1.10   mycroft 		if (!IN_LOCAL_GROUP(inm->inm_addr.s_addr) &&
    520  1.10   mycroft 		    (inm->inm_ifp->if_flags & IFF_LOOPBACK) == 0)
    521  1.10   mycroft 			if (inm->inm_rti->rti_type != IGMP_v1_ROUTER)
    522  1.10   mycroft 				igmp_sendpkt(inm, IGMP_HOST_LEAVE_MESSAGE);
    523  1.10   mycroft 		break;
    524  1.10   mycroft 	case IGMP_LAZY_MEMBER:
    525  1.10   mycroft 	case IGMP_AWAKENING_MEMBER:
    526  1.10   mycroft 	case IGMP_SLEEPING_MEMBER:
    527  1.10   mycroft 		break;
    528  1.10   mycroft 	}
    529   1.1   hpeyerl }
    530   1.1   hpeyerl 
    531   1.1   hpeyerl void
    532  1.41     perry igmp_fasttimo(void)
    533   1.1   hpeyerl {
    534  1.24  augustss 	struct in_multi *inm;
    535   1.1   hpeyerl 	struct in_multistep step;
    536   1.1   hpeyerl 
    537   1.1   hpeyerl 	/*
    538   1.1   hpeyerl 	 * Quick check to see if any work needs to be done, in order
    539   1.1   hpeyerl 	 * to minimize the overhead of fasttimo processing.
    540   1.1   hpeyerl 	 */
    541  1.55     rmind 	if (!igmp_timers_on) {
    542   1.1   hpeyerl 		return;
    543  1.55     rmind 	}
    544   1.1   hpeyerl 
    545  1.55     rmind 	/* XXX: Needed for ip_output(). */
    546  1.48        ad 	mutex_enter(softnet_lock);
    547  1.48        ad 
    548  1.55     rmind 	in_multi_lock(RW_WRITER);
    549  1.55     rmind 	igmp_timers_on = false;
    550  1.55     rmind 	inm = in_first_multi(&step);
    551   1.1   hpeyerl 	while (inm != NULL) {
    552   1.1   hpeyerl 		if (inm->inm_timer == 0) {
    553   1.1   hpeyerl 			/* do nothing */
    554   1.1   hpeyerl 		} else if (--inm->inm_timer == 0) {
    555  1.10   mycroft 			if (inm->inm_state == IGMP_DELAYING_MEMBER) {
    556  1.10   mycroft 				if (inm->inm_rti->rti_type == IGMP_v1_ROUTER)
    557  1.10   mycroft 					igmp_sendpkt(inm,
    558  1.10   mycroft 					    IGMP_v1_HOST_MEMBERSHIP_REPORT);
    559  1.10   mycroft 				else
    560  1.10   mycroft 					igmp_sendpkt(inm,
    561  1.10   mycroft 					    IGMP_v2_HOST_MEMBERSHIP_REPORT);
    562  1.10   mycroft 				inm->inm_state = IGMP_IDLE_MEMBER;
    563  1.10   mycroft 			}
    564   1.1   hpeyerl 		} else {
    565  1.55     rmind 			igmp_timers_on = true;
    566   1.1   hpeyerl 		}
    567  1.55     rmind 		inm = in_next_multi(&step);
    568   1.1   hpeyerl 	}
    569  1.55     rmind 	in_multi_unlock();
    570  1.48        ad 	mutex_exit(softnet_lock);
    571   1.1   hpeyerl }
    572   1.1   hpeyerl 
    573  1.10   mycroft void
    574  1.41     perry igmp_slowtimo(void)
    575  1.10   mycroft {
    576  1.55     rmind 	router_info_t *rti;
    577  1.10   mycroft 
    578  1.55     rmind 	in_multi_lock(RW_WRITER);
    579  1.33      matt 	LIST_FOREACH(rti, &rti_head, rti_link) {
    580  1.10   mycroft 		if (rti->rti_type == IGMP_v1_ROUTER &&
    581  1.10   mycroft 		    ++rti->rti_age >= IGMP_AGE_THRESHOLD) {
    582  1.10   mycroft 			rti->rti_type = IGMP_v2_ROUTER;
    583  1.10   mycroft 		}
    584  1.10   mycroft 	}
    585  1.55     rmind 	in_multi_unlock();
    586  1.10   mycroft }
    587  1.10   mycroft 
    588  1.55     rmind /*
    589  1.55     rmind  * igmp_sendpkt: construct an IGMP packet, given the multicast structure
    590  1.55     rmind  * and the type, and send the datagram.
    591  1.55     rmind  */
    592  1.55     rmind static void
    593  1.41     perry igmp_sendpkt(struct in_multi *inm, int type)
    594   1.1   hpeyerl {
    595  1.10   mycroft 	struct mbuf *m;
    596  1.10   mycroft 	struct igmp *igmp;
    597  1.10   mycroft 	struct ip *ip;
    598  1.10   mycroft 	struct ip_moptions imo;
    599  1.55     rmind 
    600  1.55     rmind 	KASSERT(in_multi_lock_held());
    601   1.1   hpeyerl 
    602   1.1   hpeyerl 	MGETHDR(m, M_DONTWAIT, MT_HEADER);
    603   1.1   hpeyerl 	if (m == NULL)
    604   1.1   hpeyerl 		return;
    605  1.55     rmind 
    606   1.1   hpeyerl 	/*
    607   1.1   hpeyerl 	 * Assume max_linkhdr + sizeof(struct ip) + IGMP_MINLEN
    608   1.1   hpeyerl 	 * is smaller than mbuf size returned by MGETHDR.
    609   1.1   hpeyerl 	 */
    610   1.1   hpeyerl 	m->m_data += max_linkhdr;
    611   1.1   hpeyerl 	m->m_len = sizeof(struct ip) + IGMP_MINLEN;
    612   1.1   hpeyerl 	m->m_pkthdr.len = sizeof(struct ip) + IGMP_MINLEN;
    613   1.1   hpeyerl 
    614   1.1   hpeyerl 	ip = mtod(m, struct ip *);
    615   1.1   hpeyerl 	ip->ip_tos = 0;
    616  1.31    itojun 	ip->ip_len = htons(sizeof(struct ip) + IGMP_MINLEN);
    617  1.31    itojun 	ip->ip_off = htons(0);
    618   1.1   hpeyerl 	ip->ip_p = IPPROTO_IGMP;
    619  1.16   mycroft 	ip->ip_src = zeroin_addr;
    620   1.1   hpeyerl 	ip->ip_dst = inm->inm_addr;
    621   1.1   hpeyerl 
    622   1.7    brezak 	m->m_data += sizeof(struct ip);
    623   1.7    brezak 	m->m_len -= sizeof(struct ip);
    624   1.7    brezak 	igmp = mtod(m, struct igmp *);
    625  1.10   mycroft 	igmp->igmp_type = type;
    626   1.1   hpeyerl 	igmp->igmp_code = 0;
    627   1.1   hpeyerl 	igmp->igmp_group = inm->inm_addr;
    628   1.1   hpeyerl 	igmp->igmp_cksum = 0;
    629   1.1   hpeyerl 	igmp->igmp_cksum = in_cksum(m, IGMP_MINLEN);
    630   1.7    brezak 	m->m_data -= sizeof(struct ip);
    631   1.7    brezak 	m->m_len += sizeof(struct ip);
    632   1.1   hpeyerl 
    633  1.59     ozaki 	imo.imo_multicast_if_index = if_get_index(inm->inm_ifp);
    634  1.10   mycroft 	imo.imo_multicast_ttl = 1;
    635  1.10   mycroft #ifdef RSVP_ISI
    636  1.10   mycroft 	imo.imo_multicast_vif = -1;
    637  1.10   mycroft #endif
    638   1.1   hpeyerl 	/*
    639   1.1   hpeyerl 	 * Request loopback of the report if we are acting as a multicast
    640   1.1   hpeyerl 	 * router, so that the process-level routing demon can hear it.
    641   1.1   hpeyerl 	 */
    642   1.1   hpeyerl #ifdef MROUTING
    643  1.55     rmind 	extern struct socket *ip_mrouter;
    644  1.10   mycroft 	imo.imo_multicast_loop = (ip_mrouter != NULL);
    645  1.10   mycroft #else
    646  1.10   mycroft 	imo.imo_multicast_loop = 0;
    647  1.55     rmind #endif
    648   1.1   hpeyerl 
    649  1.55     rmind 	/*
    650  1.55     rmind 	 * Note: IP_IGMP_MCAST indicates that in_multilock is held.
    651  1.55     rmind 	 * The caller must still acquire softnet_lock for ip_output().
    652  1.55     rmind 	 */
    653  1.55     rmind 	KASSERT(mutex_owned(softnet_lock));
    654  1.55     rmind 	ip_output(m, NULL, NULL, IP_IGMP_MCAST, &imo, NULL);
    655  1.46   thorpej 	IGMP_STATINC(IGMP_STAT_SND_REPORTS);
    656  1.34    itojun }
    657  1.34    itojun 
    658  1.34    itojun void
    659  1.55     rmind igmp_purgeif(ifnet_t *ifp)
    660  1.34    itojun {
    661  1.55     rmind 	in_multi_lock(RW_WRITER);
    662  1.55     rmind 	rti_delete(ifp);
    663  1.55     rmind 	in_multi_unlock();
    664   1.1   hpeyerl }
    665  1.46   thorpej 
    666  1.46   thorpej static int
    667  1.46   thorpej sysctl_net_inet_igmp_stats(SYSCTLFN_ARGS)
    668  1.46   thorpej {
    669  1.55     rmind 	return NETSTAT_SYSCTL(igmpstat_percpu, IGMP_NSTATS);
    670  1.46   thorpej }
    671  1.46   thorpej 
    672  1.51     pooka static void
    673  1.51     pooka sysctl_net_inet_igmp_setup(struct sysctllog **clog)
    674  1.46   thorpej {
    675  1.46   thorpej 	sysctl_createv(clog, 0, NULL, NULL,
    676  1.46   thorpej 			CTLFLAG_PERMANENT,
    677  1.46   thorpej 			CTLTYPE_NODE, "inet", NULL,
    678  1.46   thorpej 			NULL, 0, NULL, 0,
    679  1.46   thorpej 			CTL_NET, PF_INET, CTL_EOL);
    680  1.46   thorpej 	sysctl_createv(clog, 0, NULL, NULL,
    681  1.46   thorpej 			CTLFLAG_PERMANENT,
    682  1.46   thorpej 			CTLTYPE_NODE, "igmp",
    683  1.46   thorpej 			SYSCTL_DESCR("Internet Group Management Protocol"),
    684  1.46   thorpej 			NULL, 0, NULL, 0,
    685  1.46   thorpej 			CTL_NET, PF_INET, IPPROTO_IGMP, CTL_EOL);
    686  1.46   thorpej 	sysctl_createv(clog, 0, NULL, NULL,
    687  1.46   thorpej 			CTLFLAG_PERMANENT,
    688  1.46   thorpej 			CTLTYPE_STRUCT, "stats",
    689  1.46   thorpej 			SYSCTL_DESCR("IGMP statistics"),
    690  1.46   thorpej 			sysctl_net_inet_igmp_stats, 0, NULL, 0,
    691  1.46   thorpej 			CTL_NET, PF_INET, IPPROTO_IGMP, CTL_CREATE, CTL_EOL);
    692  1.46   thorpej }
    693