Home | History | Annotate | Line # | Download | only in netinet
ip_mroute.c revision 1.55
      1  1.55   thorpej /*	$NetBSD: ip_mroute.c,v 1.55 2001/06/02 16:17:10 thorpej Exp $	*/
      2  1.13       cgd 
      3   1.1   hpeyerl /*
      4  1.15   mycroft  * IP multicast forwarding procedures
      5   1.1   hpeyerl  *
      6   1.1   hpeyerl  * Written by David Waitzman, BBN Labs, August 1988.
      7   1.1   hpeyerl  * Modified by Steve Deering, Stanford, February 1989.
      8  1.15   mycroft  * Modified by Mark J. Steiglitz, Stanford, May, 1991
      9  1.15   mycroft  * Modified by Van Jacobson, LBL, January 1993
     10  1.15   mycroft  * Modified by Ajit Thyagarajan, PARC, August 1993
     11  1.15   mycroft  * Modified by Bill Fenner, PARC, April 1994
     12  1.15   mycroft  * Modified by Charles M. Hannum, NetBSD, May 1995.
     13   1.1   hpeyerl  *
     14  1.15   mycroft  * MROUTING Revision: 1.2
     15   1.1   hpeyerl  */
     16  1.44   thorpej 
     17  1.44   thorpej #include "opt_ipsec.h"
     18   1.1   hpeyerl 
     19   1.1   hpeyerl #include <sys/param.h>
     20  1.15   mycroft #include <sys/systm.h>
     21  1.47   thorpej #include <sys/callout.h>
     22   1.1   hpeyerl #include <sys/mbuf.h>
     23   1.1   hpeyerl #include <sys/socket.h>
     24   1.1   hpeyerl #include <sys/socketvar.h>
     25  1.15   mycroft #include <sys/protosw.h>
     26  1.15   mycroft #include <sys/errno.h>
     27   1.1   hpeyerl #include <sys/time.h>
     28  1.15   mycroft #include <sys/kernel.h>
     29  1.15   mycroft #include <sys/ioctl.h>
     30  1.15   mycroft #include <sys/syslog.h>
     31   1.1   hpeyerl #include <net/if.h>
     32   1.1   hpeyerl #include <net/route.h>
     33   1.1   hpeyerl #include <net/raw_cb.h>
     34   1.1   hpeyerl #include <netinet/in.h>
     35  1.15   mycroft #include <netinet/in_var.h>
     36   1.1   hpeyerl #include <netinet/in_systm.h>
     37   1.1   hpeyerl #include <netinet/ip.h>
     38  1.15   mycroft #include <netinet/ip_var.h>
     39   1.1   hpeyerl #include <netinet/in_pcb.h>
     40  1.15   mycroft #include <netinet/udp.h>
     41   1.1   hpeyerl #include <netinet/igmp.h>
     42   1.1   hpeyerl #include <netinet/igmp_var.h>
     43   1.1   hpeyerl #include <netinet/ip_mroute.h>
     44  1.54    itojun #include <netinet/ip_encap.h>
     45  1.37       hwr 
     46  1.25  christos #include <machine/stdarg.h>
     47  1.25  christos 
     48  1.15   mycroft #define IP_MULTICASTOPTS 0
     49  1.15   mycroft #define	M_PULLUP(m, len) \
     50  1.15   mycroft 	do { \
     51  1.15   mycroft 		if ((m) && ((m)->m_flags & M_EXT || (m)->m_len < (len))) \
     52  1.15   mycroft 			(m) = m_pullup((m), (len)); \
     53  1.15   mycroft 	} while (0)
     54   1.1   hpeyerl 
     55   1.1   hpeyerl /*
     56   1.1   hpeyerl  * Globals.  All but ip_mrouter and ip_mrtproto could be static,
     57   1.1   hpeyerl  * except for netstat or debugging purposes.
     58   1.1   hpeyerl  */
     59  1.30   mycroft struct socket  *ip_mrouter  = 0;
     60  1.15   mycroft int		ip_mrtproto = IGMP_DVMRP;    /* for netstat only */
     61  1.15   mycroft 
     62  1.15   mycroft #define NO_RTE_FOUND 	0x1
     63  1.15   mycroft #define RTE_FOUND	0x2
     64   1.1   hpeyerl 
     65  1.15   mycroft #define	MFCHASH(a, g) \
     66  1.29   mycroft 	((((a).s_addr >> 20) ^ ((a).s_addr >> 10) ^ (a).s_addr ^ \
     67  1.29   mycroft 	  ((g).s_addr >> 20) ^ ((g).s_addr >> 10) ^ (g).s_addr) & mfchash)
     68  1.15   mycroft LIST_HEAD(mfchashhdr, mfc) *mfchashtbl;
     69  1.15   mycroft u_long	mfchash;
     70  1.15   mycroft 
     71  1.15   mycroft u_char		nexpire[MFCTBLSIZ];
     72  1.15   mycroft struct vif	viftable[MAXVIFS];
     73  1.15   mycroft struct mrtstat	mrtstat;
     74  1.15   mycroft u_int		mrtdebug = 0;	  /* debug level 	*/
     75  1.15   mycroft #define		DEBUG_MFC	0x02
     76  1.15   mycroft #define		DEBUG_FORWARD	0x04
     77  1.15   mycroft #define		DEBUG_EXPIRE	0x08
     78  1.15   mycroft #define		DEBUG_XMIT	0x10
     79  1.15   mycroft u_int       	tbfdebug = 0;     /* tbf debug level 	*/
     80  1.15   mycroft #ifdef RSVP_ISI
     81  1.15   mycroft u_int		rsvpdebug = 0;	  /* rsvp debug level   */
     82  1.15   mycroft extern struct socket *ip_rsvpd;
     83  1.15   mycroft extern int rsvp_on;
     84  1.15   mycroft #endif /* RSVP_ISI */
     85  1.15   mycroft 
     86  1.54    itojun /* vif attachment using sys/netinet/ip_encap.c */
     87  1.54    itojun extern struct domain inetdomain;
     88  1.54    itojun static void vif_input __P((struct mbuf *, ...));
     89  1.54    itojun static int vif_encapcheck __P((const struct mbuf *, int, int, void *));
     90  1.54    itojun static struct protosw vif_protosw =
     91  1.54    itojun { SOCK_RAW,	&inetdomain,	IPPROTO_IPV4,	PR_ATOMIC|PR_ADDR,
     92  1.54    itojun   vif_input,	rip_output,	0,		rip_ctloutput,
     93  1.54    itojun   rip_usrreq,
     94  1.54    itojun   0,            0,              0,              0,
     95  1.54    itojun };
     96  1.54    itojun 
     97  1.15   mycroft #define		EXPIRE_TIMEOUT	(hz / 4)	/* 4x / second */
     98  1.15   mycroft #define		UPCALL_EXPIRE	6		/* number of timeouts */
     99  1.15   mycroft 
    100  1.15   mycroft /*
    101  1.15   mycroft  * Define the token bucket filter structures
    102  1.15   mycroft  */
    103  1.15   mycroft 
    104  1.31   mycroft #define		TBF_REPROCESS	(hz / 100)	/* 100x / second */
    105  1.15   mycroft 
    106  1.25  christos static int get_sg_cnt __P((struct sioc_sg_req *));
    107  1.25  christos static int get_vif_cnt __P((struct sioc_vif_req *));
    108  1.25  christos static int ip_mrouter_init __P((struct socket *, struct mbuf *));
    109  1.25  christos static int get_version __P((struct mbuf *));
    110  1.25  christos static int set_assert __P((struct mbuf *));
    111  1.25  christos static int get_assert __P((struct mbuf *));
    112  1.25  christos static int add_vif __P((struct mbuf *));
    113  1.25  christos static int del_vif __P((struct mbuf *));
    114  1.25  christos static void update_mfc __P((struct mfcctl *, struct mfc *));
    115  1.25  christos static void expire_mfc __P((struct mfc *));
    116  1.25  christos static int add_mfc __P((struct mbuf *));
    117  1.25  christos #ifdef UPCALL_TIMING
    118  1.25  christos static void collate __P((struct timeval *));
    119  1.25  christos #endif
    120  1.25  christos static int del_mfc __P((struct mbuf *));
    121  1.25  christos static int socket_send __P((struct socket *, struct mbuf *,
    122  1.25  christos 			    struct sockaddr_in *));
    123  1.25  christos static void expire_upcalls __P((void *));
    124  1.25  christos #ifdef RSVP_ISI
    125  1.25  christos static int ip_mdq __P((struct mbuf *, struct ifnet *, struct mfc *, vifi_t));
    126  1.25  christos #else
    127  1.25  christos static int ip_mdq __P((struct mbuf *, struct ifnet *, struct mfc *));
    128  1.25  christos #endif
    129  1.25  christos static void phyint_send __P((struct ip *, struct vif *, struct mbuf *));
    130  1.25  christos static void encap_send __P((struct ip *, struct vif *, struct mbuf *));
    131  1.25  christos static void tbf_control __P((struct vif *, struct mbuf *, struct ip *,
    132  1.25  christos 			     u_int32_t));
    133  1.31   mycroft static void tbf_queue __P((struct vif *, struct mbuf *));
    134  1.25  christos static void tbf_process_q __P((struct vif *));
    135  1.25  christos static void tbf_reprocess_q __P((void *));
    136  1.25  christos static int tbf_dq_sel __P((struct vif *, struct ip *));
    137  1.25  christos static void tbf_send_packet __P((struct vif *, struct mbuf *));
    138  1.25  christos static void tbf_update_tokens __P((struct vif *));
    139  1.25  christos static int priority __P((struct vif *, struct ip *));
    140   1.1   hpeyerl 
    141   1.1   hpeyerl /*
    142  1.12    brezak  * 'Interfaces' associated with decapsulator (so we can tell
    143  1.12    brezak  * packets that went through it from ones that get reflected
    144  1.12    brezak  * by a broken gateway).  These interfaces are never linked into
    145  1.12    brezak  * the system ifnet list & no routes point to them.  I.e., packets
    146  1.12    brezak  * can't be sent this way.  They only exist as a placeholder for
    147  1.12    brezak  * multicast source verification.
    148  1.12    brezak  */
    149  1.17   mycroft #if 0
    150  1.12    brezak struct ifnet multicast_decap_if[MAXVIFS];
    151  1.17   mycroft #endif
    152  1.12    brezak 
    153  1.17   mycroft #define	ENCAP_TTL	64
    154  1.17   mycroft #define	ENCAP_PROTO	IPPROTO_IPIP	/* 4 */
    155  1.12    brezak 
    156  1.12    brezak /* prototype IP hdr for encapsulated packets */
    157  1.12    brezak struct ip multicast_encap_iphdr = {
    158  1.15   mycroft #if BYTE_ORDER == LITTLE_ENDIAN
    159  1.12    brezak 	sizeof(struct ip) >> 2, IPVERSION,
    160  1.12    brezak #else
    161  1.12    brezak 	IPVERSION, sizeof(struct ip) >> 2,
    162  1.12    brezak #endif
    163  1.12    brezak 	0,				/* tos */
    164  1.12    brezak 	sizeof(struct ip),		/* total length */
    165  1.12    brezak 	0,				/* id */
    166  1.12    brezak 	0,				/* frag offset */
    167  1.15   mycroft 	ENCAP_TTL, ENCAP_PROTO,
    168  1.12    brezak 	0,				/* checksum */
    169  1.12    brezak };
    170  1.12    brezak 
    171  1.12    brezak /*
    172   1.1   hpeyerl  * Private variables.
    173   1.1   hpeyerl  */
    174  1.15   mycroft static vifi_t	   numvifs = 0;
    175  1.15   mycroft static int have_encap_tunnel = 0;
    176  1.12    brezak 
    177  1.47   thorpej static struct callout expire_upcalls_ch;
    178  1.47   thorpej 
    179  1.12    brezak /*
    180  1.38   thorpej  * one-back cache used by mrt_ipip_input to locate a tunnel's vif
    181  1.12    brezak  * given a datagram's src ip address.
    182  1.12    brezak  */
    183  1.29   mycroft static struct in_addr last_encap_src;
    184  1.12    brezak static struct vif *last_encap_vif;
    185  1.12    brezak 
    186  1.12    brezak /*
    187  1.15   mycroft  * whether or not special PIM assert processing is enabled.
    188  1.15   mycroft  */
    189  1.15   mycroft static int pim_assert;
    190  1.15   mycroft /*
    191  1.15   mycroft  * Rate limit for assert notification messages, in usec
    192  1.12    brezak  */
    193  1.15   mycroft #define ASSERT_MSG_TIME		3000000
    194  1.12    brezak 
    195  1.15   mycroft /*
    196  1.15   mycroft  * Find a route for a given origin IP address and Multicast group address
    197  1.15   mycroft  * Type of service parameter to be added in the future!!!
    198  1.15   mycroft  */
    199  1.15   mycroft 
    200  1.15   mycroft #define MFCFIND(o, g, rt) { \
    201  1.48  augustss 	struct mfc *_rt; \
    202  1.30   mycroft 	(rt) = 0; \
    203  1.15   mycroft 	++mrtstat.mrts_mfc_lookups; \
    204  1.15   mycroft 	for (_rt = mfchashtbl[MFCHASH(o, g)].lh_first; \
    205  1.15   mycroft 	     _rt; _rt = _rt->mfc_hash.le_next) { \
    206  1.29   mycroft 		if (in_hosteq(_rt->mfc_origin, (o)) && \
    207  1.29   mycroft 		    in_hosteq(_rt->mfc_mcastgrp, (g)) && \
    208  1.30   mycroft 		    _rt->mfc_stall == 0) { \
    209  1.15   mycroft 			(rt) = _rt; \
    210  1.15   mycroft 			break; \
    211  1.15   mycroft 		} \
    212  1.15   mycroft 	} \
    213  1.30   mycroft 	if ((rt) == 0) \
    214  1.15   mycroft 		++mrtstat.mrts_mfc_misses; \
    215  1.12    brezak }
    216  1.12    brezak 
    217  1.12    brezak /*
    218  1.15   mycroft  * Macros to compute elapsed time efficiently
    219  1.15   mycroft  * Borrowed from Van Jacobson's scheduling code
    220  1.12    brezak  */
    221  1.15   mycroft #define TV_DELTA(a, b, delta) { \
    222  1.48  augustss 	int xxs; \
    223  1.15   mycroft 	delta = (a).tv_usec - (b).tv_usec; \
    224  1.15   mycroft 	xxs = (a).tv_sec - (b).tv_sec; \
    225  1.15   mycroft 	switch (xxs) { \
    226  1.15   mycroft 	case 2: \
    227  1.15   mycroft 		delta += 1000000; \
    228  1.15   mycroft 		/* fall through */ \
    229  1.15   mycroft 	case 1: \
    230  1.15   mycroft 		delta += 1000000; \
    231  1.15   mycroft 		/* fall through */ \
    232  1.15   mycroft 	case 0: \
    233  1.15   mycroft 		break; \
    234  1.15   mycroft 	default: \
    235  1.15   mycroft 		delta += (1000000 * xxs); \
    236  1.15   mycroft 		break; \
    237  1.15   mycroft 	} \
    238  1.15   mycroft }
    239  1.15   mycroft 
    240  1.15   mycroft #ifdef UPCALL_TIMING
    241  1.15   mycroft u_int32_t upcall_data[51];
    242  1.15   mycroft #endif /* UPCALL_TIMING */
    243  1.15   mycroft 
    244  1.12    brezak /*
    245  1.15   mycroft  * Handle MRT setsockopt commands to modify the multicast routing tables.
    246  1.12    brezak  */
    247  1.15   mycroft int
    248  1.28   mycroft ip_mrouter_set(so, optname, m)
    249  1.15   mycroft 	struct socket *so;
    250  1.28   mycroft 	int optname;
    251  1.15   mycroft 	struct mbuf **m;
    252  1.15   mycroft {
    253  1.15   mycroft 	int error;
    254  1.15   mycroft 
    255  1.28   mycroft 	if (optname != MRT_INIT && so != ip_mrouter)
    256  1.28   mycroft 		error = ENOPROTOOPT;
    257  1.15   mycroft 	else
    258  1.28   mycroft 		switch (optname) {
    259  1.15   mycroft 		case MRT_INIT:
    260  1.15   mycroft 			error = ip_mrouter_init(so, *m);
    261  1.15   mycroft 			break;
    262  1.15   mycroft 		case MRT_DONE:
    263  1.15   mycroft 			error = ip_mrouter_done();
    264  1.15   mycroft 			break;
    265  1.15   mycroft 		case MRT_ADD_VIF:
    266  1.15   mycroft 			error = add_vif(*m);
    267  1.15   mycroft 			break;
    268  1.15   mycroft 		case MRT_DEL_VIF:
    269  1.15   mycroft 			error = del_vif(*m);
    270  1.15   mycroft 			break;
    271  1.15   mycroft 		case MRT_ADD_MFC:
    272  1.15   mycroft 			error = add_mfc(*m);
    273  1.15   mycroft 			break;
    274  1.15   mycroft 		case MRT_DEL_MFC:
    275  1.15   mycroft 			error = del_mfc(*m);
    276  1.15   mycroft 			break;
    277  1.15   mycroft 		case MRT_ASSERT:
    278  1.15   mycroft 			error = set_assert(*m);
    279  1.15   mycroft 			break;
    280  1.15   mycroft 		default:
    281  1.28   mycroft 			error = ENOPROTOOPT;
    282  1.15   mycroft 			break;
    283  1.15   mycroft 		}
    284  1.15   mycroft 
    285  1.15   mycroft 	if (*m)
    286  1.15   mycroft 		m_free(*m);
    287  1.15   mycroft 	return (error);
    288  1.12    brezak }
    289  1.12    brezak 
    290  1.15   mycroft /*
    291  1.15   mycroft  * Handle MRT getsockopt commands
    292  1.15   mycroft  */
    293  1.15   mycroft int
    294  1.28   mycroft ip_mrouter_get(so, optname, m)
    295  1.15   mycroft 	struct socket *so;
    296  1.28   mycroft 	int optname;
    297  1.15   mycroft 	struct mbuf **m;
    298  1.12    brezak {
    299  1.15   mycroft 	int error;
    300  1.12    brezak 
    301  1.15   mycroft 	if (so != ip_mrouter)
    302  1.28   mycroft 		error = ENOPROTOOPT;
    303  1.15   mycroft 	else {
    304  1.28   mycroft 		*m = m_get(M_WAIT, MT_SOOPTS);
    305  1.12    brezak 
    306  1.28   mycroft 		switch (optname) {
    307  1.15   mycroft 		case MRT_VERSION:
    308  1.28   mycroft 			error = get_version(*m);
    309  1.15   mycroft 			break;
    310  1.15   mycroft 		case MRT_ASSERT:
    311  1.28   mycroft 			error = get_assert(*m);
    312  1.15   mycroft 			break;
    313  1.15   mycroft 		default:
    314  1.28   mycroft 			error = ENOPROTOOPT;
    315  1.15   mycroft 			break;
    316  1.15   mycroft 		}
    317  1.15   mycroft 
    318  1.15   mycroft 		if (error)
    319  1.28   mycroft 			m_free(*m);
    320  1.12    brezak 	}
    321  1.15   mycroft 
    322  1.15   mycroft 	return (error);
    323  1.12    brezak }
    324  1.12    brezak 
    325   1.1   hpeyerl /*
    326  1.15   mycroft  * Handle ioctl commands to obtain information from the cache
    327   1.1   hpeyerl  */
    328   1.1   hpeyerl int
    329  1.28   mycroft mrt_ioctl(so, cmd, data)
    330  1.28   mycroft 	struct socket *so;
    331  1.15   mycroft 	u_long cmd;
    332  1.15   mycroft 	caddr_t data;
    333   1.1   hpeyerl {
    334  1.15   mycroft 	int error;
    335   1.1   hpeyerl 
    336  1.28   mycroft 	if (so != ip_mrouter)
    337  1.15   mycroft 		error = EINVAL;
    338  1.28   mycroft 	else
    339  1.28   mycroft 		switch (cmd) {
    340  1.28   mycroft 		case SIOCGETVIFCNT:
    341  1.28   mycroft 			error = get_vif_cnt((struct sioc_vif_req *)data);
    342  1.28   mycroft 			break;
    343  1.28   mycroft 		case SIOCGETSGCNT:
    344  1.28   mycroft 			error = get_sg_cnt((struct sioc_sg_req *)data);
    345  1.28   mycroft 			break;
    346  1.28   mycroft 		default:
    347  1.28   mycroft 			error = EINVAL;
    348  1.28   mycroft 			break;
    349  1.28   mycroft 		}
    350   1.1   hpeyerl 
    351  1.15   mycroft 	return (error);
    352  1.15   mycroft }
    353   1.1   hpeyerl 
    354  1.15   mycroft /*
    355  1.15   mycroft  * returns the packet, byte, rpf-failure count for the source group provided
    356  1.15   mycroft  */
    357  1.15   mycroft static int
    358  1.15   mycroft get_sg_cnt(req)
    359  1.48  augustss 	struct sioc_sg_req *req;
    360  1.15   mycroft {
    361  1.48  augustss 	struct mfc *rt;
    362  1.15   mycroft 	int s;
    363   1.1   hpeyerl 
    364  1.24   mycroft 	s = splsoftnet();
    365  1.29   mycroft 	MFCFIND(req->src, req->grp, rt);
    366  1.15   mycroft 	splx(s);
    367  1.30   mycroft 	if (rt != 0) {
    368  1.15   mycroft 		req->pktcnt = rt->mfc_pkt_cnt;
    369  1.15   mycroft 		req->bytecnt = rt->mfc_byte_cnt;
    370  1.15   mycroft 		req->wrong_if = rt->mfc_wrong_if;
    371  1.15   mycroft 	} else
    372  1.15   mycroft 		req->pktcnt = req->bytecnt = req->wrong_if = 0xffffffff;
    373   1.1   hpeyerl 
    374  1.15   mycroft 	return (0);
    375  1.15   mycroft }
    376   1.1   hpeyerl 
    377  1.15   mycroft /*
    378  1.15   mycroft  * returns the input and output packet and byte counts on the vif provided
    379  1.15   mycroft  */
    380  1.15   mycroft static int
    381  1.15   mycroft get_vif_cnt(req)
    382  1.48  augustss 	struct sioc_vif_req *req;
    383  1.15   mycroft {
    384  1.48  augustss 	vifi_t vifi = req->vifi;
    385   1.1   hpeyerl 
    386  1.15   mycroft 	if (vifi >= numvifs)
    387  1.15   mycroft 		return (EINVAL);
    388   1.1   hpeyerl 
    389  1.15   mycroft 	req->icount = viftable[vifi].v_pkt_in;
    390  1.15   mycroft 	req->ocount = viftable[vifi].v_pkt_out;
    391  1.15   mycroft 	req->ibytes = viftable[vifi].v_bytes_in;
    392  1.15   mycroft 	req->obytes = viftable[vifi].v_bytes_out;
    393   1.1   hpeyerl 
    394  1.15   mycroft 	return (0);
    395   1.1   hpeyerl }
    396   1.1   hpeyerl 
    397   1.1   hpeyerl /*
    398   1.1   hpeyerl  * Enable multicast routing
    399   1.1   hpeyerl  */
    400   1.1   hpeyerl static int
    401  1.15   mycroft ip_mrouter_init(so, m)
    402  1.15   mycroft 	struct socket *so;
    403  1.15   mycroft 	struct mbuf *m;
    404   1.1   hpeyerl {
    405  1.15   mycroft 	int *v;
    406  1.15   mycroft 
    407  1.15   mycroft 	if (mrtdebug)
    408  1.15   mycroft 		log(LOG_DEBUG,
    409  1.30   mycroft 		    "ip_mrouter_init: so_type = %d, pr_protocol = %d\n",
    410  1.15   mycroft 		    so->so_type, so->so_proto->pr_protocol);
    411  1.15   mycroft 
    412   1.1   hpeyerl 	if (so->so_type != SOCK_RAW ||
    413   1.1   hpeyerl 	    so->so_proto->pr_protocol != IPPROTO_IGMP)
    414   1.1   hpeyerl 		return (EOPNOTSUPP);
    415   1.1   hpeyerl 
    416  1.15   mycroft 	if (m == 0 || m->m_len < sizeof(int))
    417  1.15   mycroft 		return (EINVAL);
    418  1.15   mycroft 
    419  1.15   mycroft 	v = mtod(m, int *);
    420  1.15   mycroft 	if (*v != 1)
    421  1.15   mycroft 		return (EINVAL);
    422  1.15   mycroft 
    423  1.30   mycroft 	if (ip_mrouter != 0)
    424   1.1   hpeyerl 		return (EADDRINUSE);
    425   1.1   hpeyerl 
    426   1.1   hpeyerl 	ip_mrouter = so;
    427   1.1   hpeyerl 
    428  1.51        ad 	mfchashtbl =
    429  1.51        ad 	    hashinit(MFCTBLSIZ, HASH_LIST, M_MRTABLE, M_WAITOK, &mfchash);
    430  1.15   mycroft 	bzero((caddr_t)nexpire, sizeof(nexpire));
    431  1.15   mycroft 
    432  1.15   mycroft 	pim_assert = 0;
    433  1.15   mycroft 
    434  1.47   thorpej 	callout_init(&expire_upcalls_ch);
    435  1.47   thorpej 	callout_reset(&expire_upcalls_ch, EXPIRE_TIMEOUT,
    436  1.47   thorpej 	    expire_upcalls, NULL);
    437  1.15   mycroft 
    438  1.15   mycroft 	if (mrtdebug)
    439  1.30   mycroft 		log(LOG_DEBUG, "ip_mrouter_init\n");
    440  1.15   mycroft 
    441   1.1   hpeyerl 	return (0);
    442   1.1   hpeyerl }
    443   1.1   hpeyerl 
    444   1.1   hpeyerl /*
    445   1.1   hpeyerl  * Disable multicast routing
    446   1.1   hpeyerl  */
    447   1.1   hpeyerl int
    448   1.1   hpeyerl ip_mrouter_done()
    449   1.1   hpeyerl {
    450  1.15   mycroft 	vifi_t vifi;
    451  1.48  augustss 	struct vif *vifp;
    452  1.15   mycroft 	int i;
    453  1.15   mycroft 	int s;
    454  1.15   mycroft 
    455  1.24   mycroft 	s = splsoftnet();
    456   1.1   hpeyerl 
    457  1.17   mycroft 	/* Clear out all the vifs currently in use. */
    458   1.1   hpeyerl 	for (vifi = 0; vifi < numvifs; vifi++) {
    459  1.15   mycroft 		vifp = &viftable[vifi];
    460  1.29   mycroft 		if (!in_nullhost(vifp->v_lcl_addr))
    461  1.17   mycroft 			reset_vif(vifp);
    462   1.1   hpeyerl 	}
    463  1.17   mycroft 
    464   1.1   hpeyerl 	numvifs = 0;
    465  1.15   mycroft 	pim_assert = 0;
    466  1.15   mycroft 
    467  1.47   thorpej 	callout_stop(&expire_upcalls_ch);
    468  1.15   mycroft 
    469  1.15   mycroft 	/*
    470  1.15   mycroft 	 * Free all multicast forwarding cache entries.
    471  1.15   mycroft 	 */
    472  1.15   mycroft 	for (i = 0; i < MFCTBLSIZ; i++) {
    473  1.48  augustss 		struct mfc *rt, *nrt;
    474   1.1   hpeyerl 
    475  1.15   mycroft 		for (rt = mfchashtbl[i].lh_first; rt; rt = nrt) {
    476  1.15   mycroft 			nrt = rt->mfc_hash.le_next;
    477  1.15   mycroft 
    478  1.15   mycroft 			expire_mfc(rt);
    479  1.15   mycroft 		}
    480  1.15   mycroft 	}
    481  1.40   mycroft 
    482  1.15   mycroft 	free(mfchashtbl, M_MRTABLE);
    483  1.40   mycroft 	mfchashtbl = 0;
    484  1.15   mycroft 
    485  1.17   mycroft 	/* Reset de-encapsulation cache. */
    486  1.15   mycroft 	have_encap_tunnel = 0;
    487  1.15   mycroft 
    488  1.30   mycroft 	ip_mrouter = 0;
    489  1.15   mycroft 
    490  1.15   mycroft 	splx(s);
    491  1.15   mycroft 
    492  1.15   mycroft 	if (mrtdebug)
    493  1.30   mycroft 		log(LOG_DEBUG, "ip_mrouter_done\n");
    494  1.15   mycroft 
    495  1.15   mycroft 	return (0);
    496  1.15   mycroft }
    497  1.15   mycroft 
    498  1.15   mycroft static int
    499  1.15   mycroft get_version(m)
    500  1.15   mycroft 	struct mbuf *m;
    501  1.15   mycroft {
    502  1.15   mycroft 	int *v = mtod(m, int *);
    503  1.15   mycroft 
    504  1.15   mycroft 	*v = 0x0305;	/* XXX !!!! */
    505  1.15   mycroft 	m->m_len = sizeof(int);
    506  1.15   mycroft 	return (0);
    507  1.15   mycroft }
    508  1.15   mycroft 
    509  1.15   mycroft /*
    510  1.15   mycroft  * Set PIM assert processing global
    511  1.15   mycroft  */
    512  1.15   mycroft static int
    513  1.15   mycroft set_assert(m)
    514  1.15   mycroft 	struct mbuf *m;
    515  1.15   mycroft {
    516  1.15   mycroft 	int *i;
    517  1.15   mycroft 
    518  1.15   mycroft 	if (m == 0 || m->m_len < sizeof(int))
    519  1.15   mycroft 		return (EINVAL);
    520   1.1   hpeyerl 
    521  1.15   mycroft 	i = mtod(m, int *);
    522  1.15   mycroft 	pim_assert = !!*i;
    523  1.15   mycroft 	return (0);
    524  1.15   mycroft }
    525  1.15   mycroft 
    526  1.15   mycroft /*
    527  1.15   mycroft  * Get PIM assert processing global
    528  1.15   mycroft  */
    529  1.15   mycroft static int
    530  1.15   mycroft get_assert(m)
    531  1.15   mycroft 	struct mbuf *m;
    532  1.15   mycroft {
    533  1.15   mycroft 	int *i = mtod(m, int *);
    534   1.1   hpeyerl 
    535  1.15   mycroft 	*i = pim_assert;
    536  1.15   mycroft 	m->m_len = sizeof(int);
    537   1.1   hpeyerl 	return (0);
    538   1.1   hpeyerl }
    539   1.1   hpeyerl 
    540  1.15   mycroft static struct sockaddr_in sin = { sizeof(sin), AF_INET };
    541  1.15   mycroft 
    542   1.1   hpeyerl /*
    543   1.1   hpeyerl  * Add a vif to the vif table
    544   1.1   hpeyerl  */
    545   1.1   hpeyerl static int
    546  1.15   mycroft add_vif(m)
    547  1.15   mycroft 	struct mbuf *m;
    548  1.15   mycroft {
    549  1.48  augustss 	struct vifctl *vifcp;
    550  1.48  augustss 	struct vif *vifp;
    551  1.15   mycroft 	struct ifaddr *ifa;
    552  1.15   mycroft 	struct ifnet *ifp;
    553   1.1   hpeyerl 	struct ifreq ifr;
    554  1.15   mycroft 	int error, s;
    555  1.15   mycroft 
    556  1.15   mycroft 	if (m == 0 || m->m_len < sizeof(struct vifctl))
    557  1.15   mycroft 		return (EINVAL);
    558   1.1   hpeyerl 
    559  1.15   mycroft 	vifcp = mtod(m, struct vifctl *);
    560   1.1   hpeyerl 	if (vifcp->vifc_vifi >= MAXVIFS)
    561   1.1   hpeyerl 		return (EINVAL);
    562  1.15   mycroft 
    563  1.15   mycroft 	vifp = &viftable[vifcp->vifc_vifi];
    564  1.29   mycroft 	if (!in_nullhost(vifp->v_lcl_addr))
    565   1.1   hpeyerl 		return (EADDRINUSE);
    566  1.15   mycroft 
    567  1.15   mycroft 	/* Find the interface with an address in AF_INET family. */
    568   1.1   hpeyerl 	sin.sin_addr = vifcp->vifc_lcl_addr;
    569  1.18   mycroft 	ifa = ifa_ifwithaddr(sintosa(&sin));
    570   1.1   hpeyerl 	if (ifa == 0)
    571   1.1   hpeyerl 		return (EADDRNOTAVAIL);
    572  1.15   mycroft 
    573  1.12    brezak 	if (vifcp->vifc_flags & VIFF_TUNNEL) {
    574  1.17   mycroft 		if (vifcp->vifc_flags & VIFF_SRCRT) {
    575  1.30   mycroft 			log(LOG_ERR, "Source routed tunnels not supported\n");
    576  1.15   mycroft 			return (EOPNOTSUPP);
    577  1.12    brezak 		}
    578  1.17   mycroft 
    579  1.54    itojun 		/* attach this vif to decapsulator dispatch table */
    580  1.54    itojun 		vifp->v_encap_cookie = encap_attach_func(AF_INET, IPPROTO_IPV4,
    581  1.54    itojun 		    vif_encapcheck, &vif_protosw, vifp);
    582  1.54    itojun 		if (!vifp->v_encap_cookie)
    583  1.54    itojun 			return (EINVAL);
    584  1.54    itojun 
    585  1.17   mycroft 		/* Create a fake encapsulation interface. */
    586  1.17   mycroft 		ifp = (struct ifnet *)malloc(sizeof(*ifp), M_MRTABLE, M_WAITOK);
    587  1.17   mycroft 		bzero(ifp, sizeof(*ifp));
    588  1.34  christos 		sprintf(ifp->if_xname, "mdecap%d", vifcp->vifc_vifi);
    589  1.17   mycroft 
    590  1.17   mycroft 		/* Prepare cached route entry. */
    591  1.17   mycroft 		bzero(&vifp->v_route, sizeof(vifp->v_route));
    592  1.17   mycroft 
    593  1.54    itojun 		/*
    594  1.54    itojun 		 * Tell mrt_ipip_input() to start looking at encapsulated
    595  1.54    itojun 		 * packets.
    596  1.54    itojun 		 */
    597  1.17   mycroft 		have_encap_tunnel = 1;
    598  1.12    brezak 	} else {
    599  1.17   mycroft 		/* Use the physical interface associated with the address. */
    600  1.17   mycroft 		ifp = ifa->ifa_ifp;
    601  1.17   mycroft 
    602  1.15   mycroft 		/* Make sure the interface supports multicast. */
    603  1.12    brezak 		if ((ifp->if_flags & IFF_MULTICAST) == 0)
    604  1.15   mycroft 			return (EOPNOTSUPP);
    605  1.45   thorpej 
    606  1.15   mycroft 		/* Enable promiscuous reception of all IP multicasts. */
    607  1.19   mycroft 		satosin(&ifr.ifr_addr)->sin_len = sizeof(struct sockaddr_in);
    608  1.15   mycroft 		satosin(&ifr.ifr_addr)->sin_family = AF_INET;
    609  1.29   mycroft 		satosin(&ifr.ifr_addr)->sin_addr = zeroin_addr;
    610   1.1   hpeyerl 		error = (*ifp->if_ioctl)(ifp, SIOCADDMULTI, (caddr_t)&ifr);
    611  1.12    brezak 		if (error)
    612  1.15   mycroft 			return (error);
    613   1.1   hpeyerl 	}
    614  1.45   thorpej 
    615  1.24   mycroft 	s = splsoftnet();
    616  1.31   mycroft 
    617  1.15   mycroft 	/* Define parameters for the tbf structure. */
    618  1.31   mycroft 	vifp->tbf_q = 0;
    619  1.31   mycroft 	vifp->tbf_t = &vifp->tbf_q;
    620  1.31   mycroft 	microtime(&vifp->tbf_last_pkt_t);
    621  1.31   mycroft 	vifp->tbf_n_tok = 0;
    622  1.31   mycroft 	vifp->tbf_q_len = 0;
    623  1.31   mycroft 	vifp->tbf_max_q_len = MAXQSIZE;
    624  1.15   mycroft 
    625   1.1   hpeyerl 	vifp->v_flags = vifcp->vifc_flags;
    626   1.1   hpeyerl 	vifp->v_threshold = vifcp->vifc_threshold;
    627  1.31   mycroft 	/* scaling up here allows division by 1024 in critical code */
    628  1.31   mycroft 	vifp->v_rate_limit = vifcp->vifc_rate_limit * 1024 / 1000;
    629   1.1   hpeyerl 	vifp->v_lcl_addr = vifcp->vifc_lcl_addr;
    630  1.15   mycroft 	vifp->v_rmt_addr = vifcp->vifc_rmt_addr;
    631  1.12    brezak 	vifp->v_ifp = ifp;
    632  1.15   mycroft 	/* Initialize per vif pkt counters. */
    633  1.15   mycroft 	vifp->v_pkt_in = 0;
    634  1.15   mycroft 	vifp->v_pkt_out = 0;
    635  1.15   mycroft 	vifp->v_bytes_in = 0;
    636  1.15   mycroft 	vifp->v_bytes_out = 0;
    637  1.47   thorpej 
    638  1.47   thorpej 	callout_init(&vifp->v_repq_ch);
    639  1.47   thorpej 
    640  1.31   mycroft #ifdef RSVP_ISI
    641  1.31   mycroft 	vifp->v_rsvp_on = 0;
    642  1.31   mycroft 	vifp->v_rsvpd = 0;
    643  1.31   mycroft #endif /* RSVP_ISI */
    644  1.31   mycroft 
    645  1.12    brezak 	splx(s);
    646  1.15   mycroft 
    647  1.15   mycroft 	/* Adjust numvifs up if the vifi is higher than numvifs. */
    648   1.1   hpeyerl 	if (numvifs <= vifcp->vifc_vifi)
    649   1.1   hpeyerl 		numvifs = vifcp->vifc_vifi + 1;
    650  1.15   mycroft 
    651  1.15   mycroft 	if (mrtdebug)
    652  1.30   mycroft 		log(LOG_DEBUG, "add_vif #%d, lcladdr %x, %s %x, thresh %x, rate %d\n",
    653  1.15   mycroft 		    vifcp->vifc_vifi,
    654  1.15   mycroft 		    ntohl(vifcp->vifc_lcl_addr.s_addr),
    655  1.15   mycroft 		    (vifcp->vifc_flags & VIFF_TUNNEL) ? "rmtaddr" : "mask",
    656  1.15   mycroft 		    ntohl(vifcp->vifc_rmt_addr.s_addr),
    657  1.15   mycroft 		    vifcp->vifc_threshold,
    658  1.15   mycroft 		    vifcp->vifc_rate_limit);
    659  1.15   mycroft 
    660   1.1   hpeyerl 	return (0);
    661   1.1   hpeyerl }
    662   1.1   hpeyerl 
    663  1.17   mycroft void
    664  1.17   mycroft reset_vif(vifp)
    665  1.48  augustss 	struct vif *vifp;
    666  1.17   mycroft {
    667  1.48  augustss 	struct mbuf *m, *n;
    668  1.17   mycroft 	struct ifnet *ifp;
    669  1.17   mycroft 	struct ifreq ifr;
    670  1.17   mycroft 
    671  1.47   thorpej 	callout_stop(&vifp->v_repq_ch);
    672  1.47   thorpej 
    673  1.54    itojun 	/* detach this vif from decapsulator dispatch table */
    674  1.54    itojun 	encap_detach(vifp->v_encap_cookie);
    675  1.54    itojun 	vifp->v_encap_cookie = NULL;
    676  1.54    itojun 
    677  1.31   mycroft 	for (m = vifp->tbf_q; m != 0; m = n) {
    678  1.31   mycroft 		n = m->m_nextpkt;
    679  1.31   mycroft 		m_freem(m);
    680  1.31   mycroft 	}
    681  1.31   mycroft 
    682  1.17   mycroft 	if (vifp->v_flags & VIFF_TUNNEL) {
    683  1.17   mycroft 		free(vifp->v_ifp, M_MRTABLE);
    684  1.17   mycroft 		if (vifp == last_encap_vif) {
    685  1.17   mycroft 			last_encap_vif = 0;
    686  1.29   mycroft 			last_encap_src = zeroin_addr;
    687  1.17   mycroft 		}
    688  1.17   mycroft 	} else {
    689  1.19   mycroft 		satosin(&ifr.ifr_addr)->sin_len = sizeof(struct sockaddr_in);
    690  1.17   mycroft 		satosin(&ifr.ifr_addr)->sin_family = AF_INET;
    691  1.29   mycroft 		satosin(&ifr.ifr_addr)->sin_addr = zeroin_addr;
    692  1.17   mycroft 		ifp = vifp->v_ifp;
    693  1.17   mycroft 		(*ifp->if_ioctl)(ifp, SIOCDELMULTI, (caddr_t)&ifr);
    694  1.17   mycroft 	}
    695  1.17   mycroft 	bzero((caddr_t)vifp, sizeof(*vifp));
    696  1.17   mycroft }
    697  1.17   mycroft 
    698   1.1   hpeyerl /*
    699   1.1   hpeyerl  * Delete a vif from the vif table
    700   1.1   hpeyerl  */
    701   1.1   hpeyerl static int
    702  1.15   mycroft del_vif(m)
    703  1.15   mycroft 	struct mbuf *m;
    704   1.1   hpeyerl {
    705  1.15   mycroft 	vifi_t *vifip;
    706  1.48  augustss 	struct vif *vifp;
    707  1.48  augustss 	vifi_t vifi;
    708  1.15   mycroft 	int s;
    709  1.15   mycroft 
    710  1.15   mycroft 	if (m == 0 || m->m_len < sizeof(vifi_t))
    711  1.15   mycroft 		return (EINVAL);
    712   1.1   hpeyerl 
    713  1.15   mycroft 	vifip = mtod(m, vifi_t *);
    714   1.1   hpeyerl 	if (*vifip >= numvifs)
    715   1.1   hpeyerl 		return (EINVAL);
    716  1.15   mycroft 
    717  1.15   mycroft 	vifp = &viftable[*vifip];
    718  1.29   mycroft 	if (in_nullhost(vifp->v_lcl_addr))
    719   1.1   hpeyerl 		return (EADDRNOTAVAIL);
    720  1.15   mycroft 
    721  1.24   mycroft 	s = splsoftnet();
    722  1.15   mycroft 
    723  1.17   mycroft 	reset_vif(vifp);
    724  1.15   mycroft 
    725   1.1   hpeyerl 	/* Adjust numvifs down */
    726  1.15   mycroft 	for (vifi = numvifs; vifi > 0; vifi--)
    727  1.29   mycroft 		if (!in_nullhost(viftable[vifi-1].v_lcl_addr))
    728   1.1   hpeyerl 			break;
    729  1.15   mycroft 	numvifs = vifi;
    730  1.15   mycroft 
    731   1.1   hpeyerl 	splx(s);
    732  1.15   mycroft 
    733  1.15   mycroft 	if (mrtdebug)
    734  1.30   mycroft 		log(LOG_DEBUG, "del_vif %d, numvifs %d\n", *vifip, numvifs);
    735  1.15   mycroft 
    736   1.1   hpeyerl 	return (0);
    737   1.1   hpeyerl }
    738   1.1   hpeyerl 
    739  1.15   mycroft static void
    740  1.15   mycroft update_mfc(mfccp, rt)
    741  1.15   mycroft 	struct mfcctl *mfccp;
    742  1.15   mycroft 	struct mfc *rt;
    743   1.1   hpeyerl {
    744  1.15   mycroft 	vifi_t vifi;
    745   1.1   hpeyerl 
    746  1.15   mycroft 	rt->mfc_parent = mfccp->mfcc_parent;
    747  1.15   mycroft 	for (vifi = 0; vifi < numvifs; vifi++)
    748  1.15   mycroft 		rt->mfc_ttls[vifi] = mfccp->mfcc_ttls[vifi];
    749  1.15   mycroft 	rt->mfc_expire = 0;
    750  1.15   mycroft 	rt->mfc_stall = 0;
    751  1.15   mycroft }
    752   1.1   hpeyerl 
    753  1.15   mycroft static void
    754  1.15   mycroft expire_mfc(rt)
    755  1.15   mycroft 	struct mfc *rt;
    756  1.15   mycroft {
    757  1.15   mycroft 	struct rtdetq *rte, *nrte;
    758   1.1   hpeyerl 
    759  1.30   mycroft 	for (rte = rt->mfc_stall; rte != 0; rte = nrte) {
    760  1.15   mycroft 		nrte = rte->next;
    761  1.15   mycroft 		m_freem(rte->m);
    762  1.15   mycroft 		free(rte, M_MRTABLE);
    763   1.1   hpeyerl 	}
    764   1.1   hpeyerl 
    765  1.15   mycroft 	LIST_REMOVE(rt, mfc_hash);
    766  1.15   mycroft 	free(rt, M_MRTABLE);
    767   1.1   hpeyerl }
    768   1.1   hpeyerl 
    769   1.1   hpeyerl /*
    770  1.15   mycroft  * Add an mfc entry
    771   1.1   hpeyerl  */
    772   1.1   hpeyerl static int
    773  1.15   mycroft add_mfc(m)
    774  1.15   mycroft 	struct mbuf *m;
    775   1.1   hpeyerl {
    776  1.15   mycroft 	struct mfcctl *mfccp;
    777  1.25  christos 	struct mfc *rt;
    778  1.25  christos 	u_int32_t hash = 0;
    779  1.15   mycroft 	struct rtdetq *rte, *nrte;
    780  1.48  augustss 	u_short nstl;
    781  1.15   mycroft 	int s;
    782   1.1   hpeyerl 
    783  1.15   mycroft 	if (m == 0 || m->m_len < sizeof(struct mfcctl))
    784   1.1   hpeyerl 		return (EINVAL);
    785  1.15   mycroft 
    786  1.15   mycroft 	mfccp = mtod(m, struct mfcctl *);
    787   1.1   hpeyerl 
    788  1.24   mycroft 	s = splsoftnet();
    789  1.29   mycroft 	MFCFIND(mfccp->mfcc_origin, mfccp->mfcc_mcastgrp, rt);
    790   1.1   hpeyerl 
    791  1.15   mycroft 	/* If an entry already exists, just update the fields */
    792  1.15   mycroft 	if (rt) {
    793  1.15   mycroft 		if (mrtdebug & DEBUG_MFC)
    794  1.30   mycroft 			log(LOG_DEBUG,"add_mfc update o %x g %x p %x\n",
    795  1.15   mycroft 			    ntohl(mfccp->mfcc_origin.s_addr),
    796  1.15   mycroft 			    ntohl(mfccp->mfcc_mcastgrp.s_addr),
    797  1.15   mycroft 			    mfccp->mfcc_parent);
    798   1.1   hpeyerl 
    799  1.15   mycroft 		if (rt->mfc_expire)
    800  1.15   mycroft 			nexpire[hash]--;
    801   1.1   hpeyerl 
    802  1.15   mycroft 		update_mfc(mfccp, rt);
    803   1.1   hpeyerl 
    804  1.15   mycroft 		splx(s);
    805  1.15   mycroft 		return (0);
    806  1.15   mycroft 	}
    807   1.1   hpeyerl 
    808  1.15   mycroft 	/*
    809  1.15   mycroft 	 * Find the entry for which the upcall was made and update
    810  1.15   mycroft 	 */
    811  1.15   mycroft 	nstl = 0;
    812  1.29   mycroft 	hash = MFCHASH(mfccp->mfcc_origin, mfccp->mfcc_mcastgrp);
    813  1.15   mycroft 	for (rt = mfchashtbl[hash].lh_first; rt; rt = rt->mfc_hash.le_next) {
    814  1.29   mycroft 		if (in_hosteq(rt->mfc_origin, mfccp->mfcc_origin) &&
    815  1.29   mycroft 		    in_hosteq(rt->mfc_mcastgrp, mfccp->mfcc_mcastgrp) &&
    816  1.30   mycroft 		    rt->mfc_stall != 0) {
    817  1.15   mycroft 			if (nstl++)
    818  1.30   mycroft 				log(LOG_ERR, "add_mfc %s o %x g %x p %x dbx %p\n",
    819  1.15   mycroft 				    "multiple kernel entries",
    820  1.15   mycroft 				    ntohl(mfccp->mfcc_origin.s_addr),
    821  1.15   mycroft 				    ntohl(mfccp->mfcc_mcastgrp.s_addr),
    822  1.15   mycroft 				    mfccp->mfcc_parent, rt->mfc_stall);
    823  1.15   mycroft 
    824  1.15   mycroft 			if (mrtdebug & DEBUG_MFC)
    825  1.30   mycroft 				log(LOG_DEBUG,"add_mfc o %x g %x p %x dbg %p\n",
    826  1.15   mycroft 				    ntohl(mfccp->mfcc_origin.s_addr),
    827  1.15   mycroft 				    ntohl(mfccp->mfcc_mcastgrp.s_addr),
    828  1.15   mycroft 				    mfccp->mfcc_parent, rt->mfc_stall);
    829  1.15   mycroft 
    830  1.15   mycroft 			if (rt->mfc_expire)
    831  1.15   mycroft 				nexpire[hash]--;
    832  1.15   mycroft 
    833  1.35   mycroft 			rte = rt->mfc_stall;
    834  1.35   mycroft 			update_mfc(mfccp, rt);
    835  1.35   mycroft 
    836  1.15   mycroft 			/* free packets Qed at the end of this entry */
    837  1.35   mycroft 			for (; rte != 0; rte = nrte) {
    838  1.15   mycroft 				nrte = rte->next;
    839  1.15   mycroft #ifdef RSVP_ISI
    840  1.15   mycroft 				ip_mdq(rte->m, rte->ifp, rt, -1);
    841  1.15   mycroft #else
    842  1.15   mycroft 				ip_mdq(rte->m, rte->ifp, rt);
    843  1.15   mycroft #endif /* RSVP_ISI */
    844  1.15   mycroft 				m_freem(rte->m);
    845  1.15   mycroft #ifdef UPCALL_TIMING
    846  1.15   mycroft 				collate(&rte->t);
    847  1.15   mycroft #endif /* UPCALL_TIMING */
    848  1.15   mycroft 				free(rte, M_MRTABLE);
    849  1.15   mycroft 			}
    850  1.15   mycroft 		}
    851  1.15   mycroft 	}
    852   1.1   hpeyerl 
    853  1.15   mycroft 	if (nstl == 0) {
    854  1.15   mycroft 		/*
    855  1.15   mycroft 		 * No mfc; make a new one
    856  1.15   mycroft 		 */
    857  1.15   mycroft 		if (mrtdebug & DEBUG_MFC)
    858  1.30   mycroft 			log(LOG_DEBUG,"add_mfc no upcall o %x g %x p %x\n",
    859  1.15   mycroft 			    ntohl(mfccp->mfcc_origin.s_addr),
    860  1.15   mycroft 			    ntohl(mfccp->mfcc_mcastgrp.s_addr),
    861  1.15   mycroft 			    mfccp->mfcc_parent);
    862  1.15   mycroft 
    863  1.15   mycroft 		rt = (struct mfc *)malloc(sizeof(*rt), M_MRTABLE, M_NOWAIT);
    864  1.30   mycroft 		if (rt == 0) {
    865   1.1   hpeyerl 			splx(s);
    866  1.15   mycroft 			return (ENOBUFS);
    867   1.1   hpeyerl 		}
    868  1.15   mycroft 
    869  1.15   mycroft 		rt->mfc_origin = mfccp->mfcc_origin;
    870  1.15   mycroft 		rt->mfc_mcastgrp = mfccp->mfcc_mcastgrp;
    871  1.15   mycroft 		/* initialize pkt counters per src-grp */
    872  1.15   mycroft 		rt->mfc_pkt_cnt = 0;
    873  1.15   mycroft 		rt->mfc_byte_cnt = 0;
    874  1.15   mycroft 		rt->mfc_wrong_if = 0;
    875  1.15   mycroft 		timerclear(&rt->mfc_last_assert);
    876  1.15   mycroft 		update_mfc(mfccp, rt);
    877  1.15   mycroft 
    878  1.15   mycroft 		/* insert new entry at head of hash chain */
    879  1.15   mycroft 		LIST_INSERT_HEAD(&mfchashtbl[hash], rt, mfc_hash);
    880  1.15   mycroft 	}
    881  1.15   mycroft 
    882   1.1   hpeyerl 	splx(s);
    883   1.1   hpeyerl 	return (0);
    884   1.1   hpeyerl }
    885   1.1   hpeyerl 
    886  1.15   mycroft #ifdef UPCALL_TIMING
    887  1.15   mycroft /*
    888  1.15   mycroft  * collect delay statistics on the upcalls
    889  1.15   mycroft  */
    890  1.15   mycroft static void collate(t)
    891  1.48  augustss struct timeval *t;
    892  1.15   mycroft {
    893  1.48  augustss     u_int32_t d;
    894  1.48  augustss     struct timeval tp;
    895  1.48  augustss     u_int32_t delta;
    896  1.15   mycroft 
    897  1.15   mycroft     microtime(&tp);
    898  1.15   mycroft 
    899  1.15   mycroft     if (timercmp(t, &tp, <)) {
    900  1.15   mycroft 	TV_DELTA(tp, *t, delta);
    901  1.15   mycroft 
    902  1.15   mycroft 	d = delta >> 10;
    903  1.15   mycroft 	if (d > 50)
    904  1.15   mycroft 	    d = 50;
    905  1.15   mycroft 
    906  1.15   mycroft 	++upcall_data[d];
    907  1.15   mycroft     }
    908  1.15   mycroft }
    909  1.15   mycroft #endif /* UPCALL_TIMING */
    910  1.15   mycroft 
    911   1.1   hpeyerl /*
    912  1.15   mycroft  * Delete an mfc entry
    913   1.1   hpeyerl  */
    914   1.1   hpeyerl static int
    915  1.15   mycroft del_mfc(m)
    916  1.15   mycroft 	struct mbuf *m;
    917   1.1   hpeyerl {
    918  1.15   mycroft 	struct mfcctl *mfccp;
    919  1.15   mycroft 	struct mfc *rt;
    920   1.1   hpeyerl 	int s;
    921   1.1   hpeyerl 
    922  1.15   mycroft 	if (m == 0 || m->m_len < sizeof(struct mfcctl))
    923  1.15   mycroft 		return (EINVAL);
    924  1.15   mycroft 
    925  1.15   mycroft 	mfccp = mtod(m, struct mfcctl *);
    926  1.15   mycroft 
    927  1.15   mycroft 	if (mrtdebug & DEBUG_MFC)
    928  1.30   mycroft 		log(LOG_DEBUG, "del_mfc origin %x mcastgrp %x\n",
    929  1.29   mycroft 		    ntohl(mfccp->mfcc_origin.s_addr),
    930  1.29   mycroft 		    ntohl(mfccp->mfcc_mcastgrp.s_addr));
    931   1.1   hpeyerl 
    932  1.24   mycroft 	s = splsoftnet();
    933   1.1   hpeyerl 
    934  1.29   mycroft 	MFCFIND(mfccp->mfcc_origin, mfccp->mfcc_mcastgrp, rt);
    935  1.30   mycroft 	if (rt == 0) {
    936   1.1   hpeyerl 		splx(s);
    937  1.15   mycroft 		return (EADDRNOTAVAIL);
    938   1.1   hpeyerl 	}
    939   1.1   hpeyerl 
    940  1.15   mycroft 	LIST_REMOVE(rt, mfc_hash);
    941  1.15   mycroft 	free(rt, M_MRTABLE);
    942   1.1   hpeyerl 
    943   1.1   hpeyerl 	splx(s);
    944   1.1   hpeyerl 	return (0);
    945   1.1   hpeyerl }
    946   1.1   hpeyerl 
    947   1.1   hpeyerl static int
    948  1.15   mycroft socket_send(s, mm, src)
    949  1.15   mycroft     struct socket *s;
    950  1.15   mycroft     struct mbuf *mm;
    951  1.15   mycroft     struct sockaddr_in *src;
    952   1.1   hpeyerl {
    953  1.15   mycroft     if (s) {
    954  1.18   mycroft 	if (sbappendaddr(&s->so_rcv, sintosa(src), mm, (struct mbuf *)0) != 0) {
    955  1.15   mycroft 	    sorwakeup(s);
    956  1.15   mycroft 	    return (0);
    957  1.15   mycroft 	}
    958  1.15   mycroft     }
    959  1.15   mycroft     m_freem(mm);
    960  1.15   mycroft     return (-1);
    961   1.1   hpeyerl }
    962   1.1   hpeyerl 
    963   1.1   hpeyerl /*
    964   1.1   hpeyerl  * IP multicast forwarding function. This function assumes that the packet
    965   1.1   hpeyerl  * pointed to by "ip" has arrived on (or is about to be sent to) the interface
    966   1.1   hpeyerl  * pointed to by "ifp", and the packet is to be relayed to other networks
    967   1.1   hpeyerl  * that have members of the packet's destination IP multicast group.
    968   1.1   hpeyerl  *
    969  1.15   mycroft  * The packet is returned unscathed to the caller, unless it is
    970  1.15   mycroft  * erroneous, in which case a non-zero return value tells the caller to
    971   1.1   hpeyerl  * discard it.
    972   1.1   hpeyerl  */
    973   1.1   hpeyerl 
    974  1.15   mycroft #define IP_HDR_LEN  20	/* # bytes of fixed IP header (excluding options) */
    975  1.15   mycroft #define TUNNEL_LEN  12  /* # bytes of IP option for tunnel encapsulation  */
    976   1.1   hpeyerl 
    977   1.1   hpeyerl int
    978  1.15   mycroft #ifdef RSVP_ISI
    979  1.15   mycroft ip_mforward(m, ifp, imo)
    980  1.15   mycroft #else
    981  1.10   mycroft ip_mforward(m, ifp)
    982  1.15   mycroft #endif /* RSVP_ISI */
    983  1.15   mycroft     struct mbuf *m;
    984  1.15   mycroft     struct ifnet *ifp;
    985  1.15   mycroft #ifdef RSVP_ISI
    986  1.15   mycroft     struct ip_moptions *imo;
    987  1.15   mycroft #endif /* RSVP_ISI */
    988   1.1   hpeyerl {
    989  1.48  augustss     struct ip *ip = mtod(m, struct ip *);
    990  1.48  augustss     struct mfc *rt;
    991  1.48  augustss     u_char *ipoptions;
    992  1.15   mycroft     static int srctun = 0;
    993  1.48  augustss     struct mbuf *mm;
    994  1.15   mycroft     int s;
    995  1.15   mycroft #ifdef RSVP_ISI
    996  1.48  augustss     struct vif *vifp;
    997  1.15   mycroft     vifi_t vifi;
    998  1.15   mycroft #endif /* RSVP_ISI */
    999  1.55   thorpej 
   1000  1.55   thorpej     /*
   1001  1.55   thorpej      * Clear any in-bound checksum flags for this packet.
   1002  1.55   thorpej      */
   1003  1.55   thorpej     m->m_pkthdr.csum_flags = 0;
   1004  1.15   mycroft 
   1005  1.15   mycroft     if (mrtdebug & DEBUG_FORWARD)
   1006  1.30   mycroft 	log(LOG_DEBUG, "ip_mforward: src %x, dst %x, ifp %p\n",
   1007  1.15   mycroft 	    ntohl(ip->ip_src.s_addr), ntohl(ip->ip_dst.s_addr), ifp);
   1008   1.1   hpeyerl 
   1009  1.15   mycroft     if (ip->ip_hl < (IP_HDR_LEN + TUNNEL_LEN) >> 2 ||
   1010  1.15   mycroft 	(ipoptions = (u_char *)(ip + 1))[1] != IPOPT_LSRR) {
   1011   1.1   hpeyerl 	/*
   1012  1.15   mycroft 	 * Packet arrived via a physical interface or
   1013  1.15   mycroft 	 * an encapuslated tunnel.
   1014   1.1   hpeyerl 	 */
   1015  1.15   mycroft     } else {
   1016   1.1   hpeyerl 	/*
   1017  1.15   mycroft 	 * Packet arrived through a source-route tunnel.
   1018  1.15   mycroft 	 * Source-route tunnels are no longer supported.
   1019   1.1   hpeyerl 	 */
   1020  1.15   mycroft 	if ((srctun++ % 1000) == 0)
   1021  1.30   mycroft 	    log(LOG_ERR, "ip_mforward: received source-routed packet from %x\n",
   1022  1.15   mycroft 		ntohl(ip->ip_src.s_addr));
   1023  1.15   mycroft 
   1024  1.15   mycroft 	return (1);
   1025  1.15   mycroft     }
   1026  1.15   mycroft 
   1027  1.15   mycroft #ifdef RSVP_ISI
   1028  1.15   mycroft     if (imo && ((vifi = imo->imo_multicast_vif) < numvifs)) {
   1029  1.15   mycroft 	if (ip->ip_ttl < 255)
   1030  1.15   mycroft 	    ip->ip_ttl++;	/* compensate for -1 in *_send routines */
   1031  1.15   mycroft 	if (rsvpdebug && ip->ip_p == IPPROTO_RSVP) {
   1032  1.15   mycroft 	    vifp = viftable + vifi;
   1033  1.34  christos 	    printf("Sending IPPROTO_RSVP from %x to %x on vif %d (%s%s)\n",
   1034  1.15   mycroft 		ntohl(ip->ip_src), ntohl(ip->ip_dst), vifi,
   1035  1.15   mycroft 		(vifp->v_flags & VIFF_TUNNEL) ? "tunnel on " : "",
   1036  1.27   thorpej 		vifp->v_ifp->if_xname);
   1037  1.15   mycroft 	}
   1038  1.31   mycroft 	return (ip_mdq(m, ifp, (struct mfc *)0, vifi));
   1039  1.15   mycroft     }
   1040  1.15   mycroft     if (rsvpdebug && ip->ip_p == IPPROTO_RSVP) {
   1041  1.34  christos 	printf("Warning: IPPROTO_RSVP from %x to %x without vif option\n",
   1042  1.15   mycroft 	    ntohl(ip->ip_src), ntohl(ip->ip_dst));
   1043  1.15   mycroft     }
   1044  1.15   mycroft #endif /* RSVP_ISI */
   1045  1.15   mycroft 
   1046  1.15   mycroft     /*
   1047  1.15   mycroft      * Don't forward a packet with time-to-live of zero or one,
   1048  1.15   mycroft      * or a packet destined to a local-only group.
   1049  1.15   mycroft      */
   1050  1.15   mycroft     if (ip->ip_ttl <= 1 ||
   1051  1.16   mycroft 	IN_LOCAL_GROUP(ip->ip_dst.s_addr))
   1052  1.15   mycroft 	return (0);
   1053  1.15   mycroft 
   1054  1.15   mycroft     /*
   1055  1.15   mycroft      * Determine forwarding vifs from the forwarding cache table
   1056  1.15   mycroft      */
   1057  1.24   mycroft     s = splsoftnet();
   1058  1.29   mycroft     MFCFIND(ip->ip_src, ip->ip_dst, rt);
   1059   1.1   hpeyerl 
   1060  1.15   mycroft     /* Entry exists, so forward if necessary */
   1061  1.30   mycroft     if (rt != 0) {
   1062  1.15   mycroft 	splx(s);
   1063  1.15   mycroft #ifdef RSVP_ISI
   1064  1.15   mycroft 	return (ip_mdq(m, ifp, rt, -1));
   1065  1.15   mycroft #else
   1066  1.15   mycroft 	return (ip_mdq(m, ifp, rt));
   1067  1.15   mycroft #endif /* RSVP_ISI */
   1068  1.15   mycroft     } else {
   1069   1.1   hpeyerl 	/*
   1070  1.15   mycroft 	 * If we don't have a route for packet's origin,
   1071  1.15   mycroft 	 * Make a copy of the packet &
   1072  1.15   mycroft 	 * send message to routing daemon
   1073   1.1   hpeyerl 	 */
   1074  1.15   mycroft 
   1075  1.48  augustss 	struct mbuf *mb0;
   1076  1.48  augustss 	struct rtdetq *rte;
   1077  1.48  augustss 	u_int32_t hash;
   1078  1.31   mycroft 	int hlen = ip->ip_hl << 2;
   1079  1.15   mycroft #ifdef UPCALL_TIMING
   1080  1.15   mycroft 	struct timeval tp;
   1081  1.15   mycroft 
   1082  1.15   mycroft 	microtime(&tp);
   1083  1.15   mycroft #endif /* UPCALL_TIMING */
   1084  1.15   mycroft 
   1085  1.15   mycroft 	mrtstat.mrts_no_route++;
   1086  1.15   mycroft 	if (mrtdebug & (DEBUG_FORWARD | DEBUG_MFC))
   1087  1.30   mycroft 	    log(LOG_DEBUG, "ip_mforward: no rte s %x g %x\n",
   1088  1.15   mycroft 		ntohl(ip->ip_src.s_addr),
   1089  1.15   mycroft 		ntohl(ip->ip_dst.s_addr));
   1090   1.1   hpeyerl 
   1091   1.1   hpeyerl 	/*
   1092  1.15   mycroft 	 * Allocate mbufs early so that we don't do extra work if we are
   1093  1.31   mycroft 	 * just going to fail anyway.  Make sure to pullup the header so
   1094  1.31   mycroft 	 * that other people can't step on it.
   1095   1.1   hpeyerl 	 */
   1096  1.15   mycroft 	rte = (struct rtdetq *)malloc(sizeof(*rte), M_MRTABLE, M_NOWAIT);
   1097  1.30   mycroft 	if (rte == 0) {
   1098  1.15   mycroft 	    splx(s);
   1099  1.15   mycroft 	    return (ENOBUFS);
   1100  1.15   mycroft 	}
   1101  1.15   mycroft 	mb0 = m_copy(m, 0, M_COPYALL);
   1102  1.31   mycroft 	M_PULLUP(mb0, hlen);
   1103  1.30   mycroft 	if (mb0 == 0) {
   1104  1.15   mycroft 	    free(rte, M_MRTABLE);
   1105  1.15   mycroft 	    splx(s);
   1106  1.15   mycroft 	    return (ENOBUFS);
   1107  1.15   mycroft 	}
   1108  1.15   mycroft 
   1109  1.15   mycroft 	/* is there an upcall waiting for this packet? */
   1110  1.29   mycroft 	hash = MFCHASH(ip->ip_src, ip->ip_dst);
   1111  1.15   mycroft 	for (rt = mfchashtbl[hash].lh_first; rt; rt = rt->mfc_hash.le_next) {
   1112  1.29   mycroft 	    if (in_hosteq(ip->ip_src, rt->mfc_origin) &&
   1113  1.29   mycroft 		in_hosteq(ip->ip_dst, rt->mfc_mcastgrp) &&
   1114  1.30   mycroft 		rt->mfc_stall != 0)
   1115  1.15   mycroft 		break;
   1116   1.1   hpeyerl 	}
   1117  1.15   mycroft 
   1118  1.30   mycroft 	if (rt == 0) {
   1119  1.15   mycroft 	    int i;
   1120  1.15   mycroft 	    struct igmpmsg *im;
   1121  1.15   mycroft 
   1122  1.15   mycroft 	    /* no upcall, so make a new entry */
   1123  1.15   mycroft 	    rt = (struct mfc *)malloc(sizeof(*rt), M_MRTABLE, M_NOWAIT);
   1124  1.30   mycroft 	    if (rt == 0) {
   1125  1.15   mycroft 		free(rte, M_MRTABLE);
   1126  1.31   mycroft 		m_freem(mb0);
   1127  1.15   mycroft 		splx(s);
   1128  1.15   mycroft 		return (ENOBUFS);
   1129  1.15   mycroft 	    }
   1130  1.15   mycroft 	    /* Make a copy of the header to send to the user level process */
   1131  1.15   mycroft 	    mm = m_copy(m, 0, hlen);
   1132  1.15   mycroft 	    M_PULLUP(mm, hlen);
   1133  1.30   mycroft 	    if (mm == 0) {
   1134  1.15   mycroft 		free(rte, M_MRTABLE);
   1135  1.31   mycroft 		m_freem(mb0);
   1136  1.15   mycroft 		free(rt, M_MRTABLE);
   1137  1.15   mycroft 		splx(s);
   1138  1.15   mycroft 		return (ENOBUFS);
   1139  1.15   mycroft 	    }
   1140  1.15   mycroft 
   1141  1.15   mycroft 	    /*
   1142  1.15   mycroft 	     * Send message to routing daemon to install
   1143  1.15   mycroft 	     * a route into the kernel table
   1144  1.15   mycroft 	     */
   1145  1.15   mycroft 	    sin.sin_addr = ip->ip_src;
   1146  1.15   mycroft 
   1147  1.15   mycroft 	    im = mtod(mm, struct igmpmsg *);
   1148  1.15   mycroft 	    im->im_msgtype	= IGMPMSG_NOCACHE;
   1149  1.15   mycroft 	    im->im_mbz		= 0;
   1150  1.15   mycroft 
   1151  1.15   mycroft 	    mrtstat.mrts_upcalls++;
   1152  1.15   mycroft 
   1153  1.15   mycroft 	    if (socket_send(ip_mrouter, mm, &sin) < 0) {
   1154  1.30   mycroft 		log(LOG_WARNING, "ip_mforward: ip_mrouter socket queue full\n");
   1155  1.15   mycroft 		++mrtstat.mrts_upq_sockfull;
   1156  1.15   mycroft 		free(rte, M_MRTABLE);
   1157  1.31   mycroft 		m_freem(mb0);
   1158  1.15   mycroft 		free(rt, M_MRTABLE);
   1159  1.15   mycroft 		splx(s);
   1160  1.15   mycroft 		return (ENOBUFS);
   1161  1.15   mycroft 	    }
   1162  1.15   mycroft 
   1163  1.15   mycroft 	    /* insert new entry at head of hash chain */
   1164  1.23   mycroft 	    rt->mfc_origin = ip->ip_src;
   1165  1.23   mycroft 	    rt->mfc_mcastgrp = ip->ip_dst;
   1166  1.23   mycroft 	    rt->mfc_pkt_cnt = 0;
   1167  1.23   mycroft 	    rt->mfc_byte_cnt = 0;
   1168  1.23   mycroft 	    rt->mfc_wrong_if = 0;
   1169  1.23   mycroft 	    rt->mfc_expire = UPCALL_EXPIRE;
   1170  1.15   mycroft 	    nexpire[hash]++;
   1171  1.15   mycroft 	    for (i = 0; i < numvifs; i++)
   1172  1.15   mycroft 		rt->mfc_ttls[i] = 0;
   1173  1.15   mycroft 	    rt->mfc_parent = -1;
   1174  1.15   mycroft 
   1175  1.15   mycroft 	    /* link into table */
   1176  1.15   mycroft 	    LIST_INSERT_HEAD(&mfchashtbl[hash], rt, mfc_hash);
   1177  1.15   mycroft 	    /* Add this entry to the end of the queue */
   1178  1.15   mycroft 	    rt->mfc_stall = rte;
   1179  1.15   mycroft 	} else {
   1180  1.15   mycroft 	    /* determine if q has overflowed */
   1181  1.15   mycroft 	    struct rtdetq **p;
   1182  1.48  augustss 	    int npkts = 0;
   1183  1.15   mycroft 
   1184  1.30   mycroft 	    for (p = &rt->mfc_stall; *p != 0; p = &(*p)->next)
   1185  1.15   mycroft 		if (++npkts > MAX_UPQ) {
   1186  1.15   mycroft 		    mrtstat.mrts_upq_ovflw++;
   1187  1.15   mycroft 		    free(rte, M_MRTABLE);
   1188  1.31   mycroft 		    m_freem(mb0);
   1189  1.15   mycroft 		    splx(s);
   1190  1.15   mycroft 		    return (0);
   1191  1.15   mycroft 	        }
   1192  1.15   mycroft 
   1193  1.15   mycroft 	    /* Add this entry to the end of the queue */
   1194  1.15   mycroft 	    *p = rte;
   1195  1.15   mycroft 	}
   1196  1.15   mycroft 
   1197  1.30   mycroft 	rte->next		= 0;
   1198  1.15   mycroft 	rte->m 			= mb0;
   1199  1.15   mycroft 	rte->ifp 		= ifp;
   1200  1.15   mycroft #ifdef UPCALL_TIMING
   1201  1.15   mycroft 	rte->t			= tp;
   1202  1.15   mycroft #endif /* UPCALL_TIMING */
   1203  1.15   mycroft 
   1204  1.15   mycroft 
   1205  1.15   mycroft 	splx(s);
   1206  1.15   mycroft 
   1207  1.15   mycroft 	return (0);
   1208  1.15   mycroft     }
   1209   1.1   hpeyerl }
   1210   1.1   hpeyerl 
   1211  1.15   mycroft 
   1212  1.25  christos /*ARGSUSED*/
   1213   1.1   hpeyerl static void
   1214  1.25  christos expire_upcalls(v)
   1215  1.25  christos 	void *v;
   1216   1.1   hpeyerl {
   1217  1.15   mycroft 	int i;
   1218  1.15   mycroft 	int s;
   1219  1.15   mycroft 
   1220  1.24   mycroft 	s = splsoftnet();
   1221  1.15   mycroft 
   1222  1.15   mycroft 	for (i = 0; i < MFCTBLSIZ; i++) {
   1223  1.48  augustss 		struct mfc *rt, *nrt;
   1224  1.15   mycroft 
   1225  1.15   mycroft 		if (nexpire[i] == 0)
   1226  1.15   mycroft 			continue;
   1227  1.15   mycroft 
   1228  1.15   mycroft 		for (rt = mfchashtbl[i].lh_first; rt; rt = nrt) {
   1229  1.15   mycroft 			nrt = rt->mfc_hash.le_next;
   1230   1.1   hpeyerl 
   1231  1.15   mycroft 			if (rt->mfc_expire == 0 ||
   1232  1.15   mycroft 			    --rt->mfc_expire > 0)
   1233  1.15   mycroft 				continue;
   1234  1.15   mycroft 			nexpire[i]--;
   1235  1.15   mycroft 
   1236  1.15   mycroft 			++mrtstat.mrts_cache_cleanups;
   1237  1.15   mycroft 			if (mrtdebug & DEBUG_EXPIRE)
   1238  1.15   mycroft 				log(LOG_DEBUG,
   1239  1.30   mycroft 				    "expire_upcalls: expiring (%x %x)\n",
   1240  1.15   mycroft 				    ntohl(rt->mfc_origin.s_addr),
   1241  1.15   mycroft 				    ntohl(rt->mfc_mcastgrp.s_addr));
   1242   1.1   hpeyerl 
   1243  1.15   mycroft 			expire_mfc(rt);
   1244  1.15   mycroft 		}
   1245  1.15   mycroft 	}
   1246   1.1   hpeyerl 
   1247  1.15   mycroft 	splx(s);
   1248  1.47   thorpej 	callout_reset(&expire_upcalls_ch, EXPIRE_TIMEOUT,
   1249  1.47   thorpej 	    expire_upcalls, NULL);
   1250   1.1   hpeyerl }
   1251   1.1   hpeyerl 
   1252  1.15   mycroft /*
   1253  1.15   mycroft  * Packet forwarding routine once entry in the cache is made
   1254  1.15   mycroft  */
   1255  1.15   mycroft static int
   1256  1.15   mycroft #ifdef RSVP_ISI
   1257  1.15   mycroft ip_mdq(m, ifp, rt, xmt_vif)
   1258  1.15   mycroft #else
   1259  1.15   mycroft ip_mdq(m, ifp, rt)
   1260  1.15   mycroft #endif /* RSVP_ISI */
   1261  1.48  augustss     struct mbuf *m;
   1262  1.48  augustss     struct ifnet *ifp;
   1263  1.48  augustss     struct mfc *rt;
   1264  1.15   mycroft #ifdef RSVP_ISI
   1265  1.48  augustss     vifi_t xmt_vif;
   1266  1.15   mycroft #endif /* RSVP_ISI */
   1267   1.1   hpeyerl {
   1268  1.48  augustss     struct ip  *ip = mtod(m, struct ip *);
   1269  1.48  augustss     vifi_t vifi;
   1270  1.48  augustss     struct vif *vifp;
   1271  1.48  augustss     int plen = ntohs(ip->ip_len);
   1272  1.15   mycroft 
   1273  1.15   mycroft /*
   1274  1.15   mycroft  * Macro to send packet on vif.  Since RSVP packets don't get counted on
   1275  1.15   mycroft  * input, they shouldn't get counted on output, so statistics keeping is
   1276  1.15   mycroft  * seperate.
   1277  1.15   mycroft  */
   1278  1.15   mycroft #define MC_SEND(ip,vifp,m) {                             \
   1279  1.15   mycroft                 if ((vifp)->v_flags & VIFF_TUNNEL)	 \
   1280  1.15   mycroft                     encap_send((ip), (vifp), (m));       \
   1281  1.15   mycroft                 else                                     \
   1282  1.15   mycroft                     phyint_send((ip), (vifp), (m));      \
   1283  1.15   mycroft }
   1284   1.1   hpeyerl 
   1285  1.15   mycroft #ifdef RSVP_ISI
   1286  1.15   mycroft     /*
   1287  1.15   mycroft      * If xmt_vif is not -1, send on only the requested vif.
   1288  1.15   mycroft      *
   1289  1.15   mycroft      * (since vifi_t is u_short, -1 becomes MAXUSHORT, which > numvifs.
   1290  1.15   mycroft      */
   1291  1.15   mycroft     if (xmt_vif < numvifs) {
   1292  1.15   mycroft         MC_SEND(ip, viftable + xmt_vif, m);
   1293  1.15   mycroft 	return (1);
   1294  1.15   mycroft     }
   1295  1.15   mycroft #endif /* RSVP_ISI */
   1296  1.15   mycroft 
   1297  1.15   mycroft     /*
   1298  1.15   mycroft      * Don't forward if it didn't arrive from the parent vif for its origin.
   1299  1.15   mycroft      */
   1300  1.15   mycroft     vifi = rt->mfc_parent;
   1301  1.15   mycroft     if ((vifi >= numvifs) || (viftable[vifi].v_ifp != ifp)) {
   1302  1.15   mycroft 	/* came in the wrong interface */
   1303  1.15   mycroft 	if (mrtdebug & DEBUG_FORWARD)
   1304  1.30   mycroft 	    log(LOG_DEBUG, "wrong if: ifp %p vifi %d vififp %p\n",
   1305  1.15   mycroft 		ifp, vifi, viftable[vifi].v_ifp);
   1306  1.15   mycroft 	++mrtstat.mrts_wrong_if;
   1307  1.15   mycroft 	++rt->mfc_wrong_if;
   1308   1.1   hpeyerl 	/*
   1309  1.15   mycroft 	 * If we are doing PIM assert processing, and we are forwarding
   1310  1.15   mycroft 	 * packets on this interface, and it is a broadcast medium
   1311  1.15   mycroft 	 * interface (and not a tunnel), send a message to the routing daemon.
   1312   1.1   hpeyerl 	 */
   1313  1.15   mycroft 	if (pim_assert && rt->mfc_ttls[vifi] &&
   1314  1.15   mycroft 		(ifp->if_flags & IFF_BROADCAST) &&
   1315  1.15   mycroft 		!(viftable[vifi].v_flags & VIFF_TUNNEL)) {
   1316  1.15   mycroft 	    struct mbuf *mm;
   1317  1.15   mycroft 	    struct igmpmsg *im;
   1318  1.15   mycroft 	    int hlen = ip->ip_hl << 2;
   1319  1.15   mycroft 	    struct timeval now;
   1320  1.48  augustss 	    u_int32_t delta;
   1321  1.15   mycroft 
   1322  1.15   mycroft 	    microtime(&now);
   1323  1.15   mycroft 
   1324  1.15   mycroft 	    TV_DELTA(rt->mfc_last_assert, now, delta);
   1325  1.15   mycroft 
   1326  1.15   mycroft 	    if (delta > ASSERT_MSG_TIME) {
   1327  1.15   mycroft 		mm = m_copy(m, 0, hlen);
   1328  1.15   mycroft 		M_PULLUP(mm, hlen);
   1329  1.30   mycroft 		if (mm == 0) {
   1330  1.15   mycroft 		    return (ENOBUFS);
   1331  1.15   mycroft 		}
   1332  1.15   mycroft 
   1333  1.15   mycroft 		rt->mfc_last_assert = now;
   1334  1.15   mycroft 
   1335  1.15   mycroft 		im = mtod(mm, struct igmpmsg *);
   1336  1.15   mycroft 		im->im_msgtype	= IGMPMSG_WRONGVIF;
   1337  1.15   mycroft 		im->im_mbz	= 0;
   1338  1.15   mycroft 		im->im_vif	= vifi;
   1339  1.15   mycroft 
   1340  1.15   mycroft 		sin.sin_addr = im->im_src;
   1341  1.15   mycroft 
   1342  1.31   mycroft 		socket_send(ip_mrouter, mm, &sin);
   1343  1.15   mycroft 	    }
   1344  1.15   mycroft 	}
   1345  1.15   mycroft 	return (0);
   1346  1.15   mycroft     }
   1347  1.15   mycroft 
   1348  1.15   mycroft     /* If I sourced this packet, it counts as output, else it was input. */
   1349  1.29   mycroft     if (in_hosteq(ip->ip_src, viftable[vifi].v_lcl_addr)) {
   1350  1.15   mycroft 	viftable[vifi].v_pkt_out++;
   1351  1.15   mycroft 	viftable[vifi].v_bytes_out += plen;
   1352  1.15   mycroft     } else {
   1353  1.15   mycroft 	viftable[vifi].v_pkt_in++;
   1354  1.15   mycroft 	viftable[vifi].v_bytes_in += plen;
   1355  1.15   mycroft     }
   1356  1.15   mycroft     rt->mfc_pkt_cnt++;
   1357  1.15   mycroft     rt->mfc_byte_cnt += plen;
   1358  1.15   mycroft 
   1359  1.15   mycroft     /*
   1360  1.15   mycroft      * For each vif, decide if a copy of the packet should be forwarded.
   1361  1.15   mycroft      * Forward if:
   1362  1.15   mycroft      *		- the ttl exceeds the vif's threshold
   1363  1.15   mycroft      *		- there are group members downstream on interface
   1364  1.15   mycroft      */
   1365  1.15   mycroft     for (vifp = viftable, vifi = 0; vifi < numvifs; vifp++, vifi++)
   1366  1.15   mycroft 	if ((rt->mfc_ttls[vifi] > 0) &&
   1367  1.15   mycroft 	    (ip->ip_ttl > rt->mfc_ttls[vifi])) {
   1368  1.15   mycroft 	    vifp->v_pkt_out++;
   1369  1.15   mycroft 	    vifp->v_bytes_out += plen;
   1370  1.15   mycroft 	    MC_SEND(ip, vifp, m);
   1371   1.1   hpeyerl 	}
   1372   1.1   hpeyerl 
   1373  1.15   mycroft     return (0);
   1374  1.15   mycroft }
   1375  1.15   mycroft 
   1376  1.15   mycroft #ifdef RSVP_ISI
   1377  1.15   mycroft /*
   1378  1.15   mycroft  * check if a vif number is legal/ok. This is used by ip_output, to export
   1379  1.15   mycroft  * numvifs there,
   1380  1.15   mycroft  */
   1381  1.15   mycroft int
   1382  1.15   mycroft legal_vif_num(vif)
   1383  1.15   mycroft     int vif;
   1384  1.15   mycroft {
   1385  1.15   mycroft     if (vif >= 0 && vif < numvifs)
   1386  1.15   mycroft        return (1);
   1387  1.15   mycroft     else
   1388  1.15   mycroft        return (0);
   1389  1.15   mycroft }
   1390  1.15   mycroft #endif /* RSVP_ISI */
   1391  1.15   mycroft 
   1392  1.15   mycroft static void
   1393  1.15   mycroft phyint_send(ip, vifp, m)
   1394  1.15   mycroft 	struct ip *ip;
   1395  1.15   mycroft 	struct vif *vifp;
   1396  1.15   mycroft 	struct mbuf *m;
   1397  1.15   mycroft {
   1398  1.48  augustss 	struct mbuf *mb_copy;
   1399  1.48  augustss 	int hlen = ip->ip_hl << 2;
   1400  1.15   mycroft 
   1401  1.15   mycroft 	/*
   1402  1.15   mycroft 	 * Make a new reference to the packet; make sure that
   1403  1.15   mycroft 	 * the IP header is actually copied, not just referenced,
   1404  1.15   mycroft 	 * so that ip_output() only scribbles on the copy.
   1405  1.15   mycroft 	 */
   1406  1.12    brezak 	mb_copy = m_copy(m, 0, M_COPYALL);
   1407  1.15   mycroft 	M_PULLUP(mb_copy, hlen);
   1408  1.30   mycroft 	if (mb_copy == 0)
   1409   1.1   hpeyerl 		return;
   1410   1.1   hpeyerl 
   1411  1.15   mycroft 	if (vifp->v_rate_limit <= 0)
   1412  1.15   mycroft 		tbf_send_packet(vifp, mb_copy);
   1413  1.15   mycroft 	else
   1414  1.15   mycroft 		tbf_control(vifp, mb_copy, mtod(mb_copy, struct ip *), ip->ip_len);
   1415  1.12    brezak }
   1416  1.12    brezak 
   1417  1.12    brezak static void
   1418  1.12    brezak encap_send(ip, vifp, m)
   1419  1.48  augustss 	struct ip *ip;
   1420  1.48  augustss 	struct vif *vifp;
   1421  1.48  augustss 	struct mbuf *m;
   1422  1.48  augustss {
   1423  1.48  augustss 	struct mbuf *mb_copy;
   1424  1.48  augustss 	struct ip *ip_copy;
   1425  1.48  augustss 	int i, len = ip->ip_len + sizeof(multicast_encap_iphdr);
   1426  1.12    brezak 
   1427  1.12    brezak 	/*
   1428  1.12    brezak 	 * copy the old packet & pullup it's IP header into the
   1429  1.12    brezak 	 * new mbuf so we can modify it.  Try to fill the new
   1430  1.12    brezak 	 * mbuf since if we don't the ethernet driver will.
   1431  1.12    brezak 	 */
   1432  1.15   mycroft 	MGETHDR(mb_copy, M_DONTWAIT, MT_DATA);
   1433  1.30   mycroft 	if (mb_copy == 0)
   1434  1.12    brezak 		return;
   1435  1.15   mycroft 	mb_copy->m_data += max_linkhdr;
   1436  1.15   mycroft 	mb_copy->m_pkthdr.len = len;
   1437  1.12    brezak 	mb_copy->m_len = sizeof(multicast_encap_iphdr);
   1438  1.15   mycroft 
   1439  1.30   mycroft 	if ((mb_copy->m_next = m_copy(m, 0, M_COPYALL)) == 0) {
   1440  1.12    brezak 		m_freem(mb_copy);
   1441  1.12    brezak 		return;
   1442  1.12    brezak 	}
   1443  1.15   mycroft 	i = MHLEN - max_linkhdr;
   1444  1.12    brezak 	if (i > len)
   1445  1.12    brezak 		i = len;
   1446  1.12    brezak 	mb_copy = m_pullup(mb_copy, i);
   1447  1.30   mycroft 	if (mb_copy == 0)
   1448  1.12    brezak 		return;
   1449  1.15   mycroft 
   1450  1.12    brezak 	/*
   1451  1.12    brezak 	 * fill in the encapsulating IP header.
   1452  1.12    brezak 	 */
   1453  1.12    brezak 	ip_copy = mtod(mb_copy, struct ip *);
   1454  1.12    brezak 	*ip_copy = multicast_encap_iphdr;
   1455  1.12    brezak 	ip_copy->ip_id = htons(ip_id++);
   1456  1.15   mycroft 	ip_copy->ip_len = len;
   1457  1.12    brezak 	ip_copy->ip_src = vifp->v_lcl_addr;
   1458  1.12    brezak 	ip_copy->ip_dst = vifp->v_rmt_addr;
   1459  1.15   mycroft 
   1460  1.12    brezak 	/*
   1461  1.12    brezak 	 * turn the encapsulated IP header back into a valid one.
   1462  1.12    brezak 	 */
   1463  1.12    brezak 	ip = (struct ip *)((caddr_t)ip_copy + sizeof(multicast_encap_iphdr));
   1464  1.12    brezak 	--ip->ip_ttl;
   1465  1.12    brezak 	HTONS(ip->ip_len);
   1466  1.12    brezak 	HTONS(ip->ip_off);
   1467  1.12    brezak 	ip->ip_sum = 0;
   1468  1.12    brezak 	mb_copy->m_data += sizeof(multicast_encap_iphdr);
   1469  1.12    brezak 	ip->ip_sum = in_cksum(mb_copy, ip->ip_hl << 2);
   1470  1.12    brezak 	mb_copy->m_data -= sizeof(multicast_encap_iphdr);
   1471  1.15   mycroft 
   1472  1.15   mycroft 	if (vifp->v_rate_limit <= 0)
   1473  1.15   mycroft 		tbf_send_packet(vifp, mb_copy);
   1474  1.15   mycroft 	else
   1475  1.15   mycroft 		tbf_control(vifp, mb_copy, ip, ip_copy->ip_len);
   1476  1.12    brezak }
   1477  1.12    brezak 
   1478  1.12    brezak /*
   1479  1.54    itojun  * De-encapsulate a packet and feed it back through ip input.
   1480  1.12    brezak  */
   1481  1.54    itojun static void
   1482  1.54    itojun #if __STDC__
   1483  1.54    itojun vif_input(struct mbuf *m, ...)
   1484  1.54    itojun #else
   1485  1.54    itojun vif_input(m, va_alist)
   1486  1.25  christos 	struct mbuf *m;
   1487  1.54    itojun 	va_dcl
   1488  1.54    itojun #endif
   1489  1.25  christos {
   1490  1.54    itojun 	int off, proto;
   1491  1.54    itojun 	va_list ap;
   1492  1.54    itojun 	struct ip *ip;
   1493  1.54    itojun 	struct vif *vifp;
   1494  1.48  augustss 	int s;
   1495  1.48  augustss 	struct ifqueue *ifq;
   1496  1.25  christos 
   1497  1.54    itojun 	va_start(ap, m);
   1498  1.54    itojun 	off = va_arg(ap, int);
   1499  1.54    itojun 	proto = va_arg(ap, int);
   1500  1.54    itojun 	va_end(ap);
   1501  1.22   mycroft 
   1502  1.54    itojun 	vifp = (struct vif *)encap_getarg(m);
   1503  1.54    itojun 	if (!vifp || proto != AF_INET) {
   1504  1.54    itojun 		m_freem(m);
   1505  1.54    itojun 		mrtstat.mrts_bad_tunnel++;
   1506  1.54    itojun 		return;
   1507  1.12    brezak 	}
   1508  1.22   mycroft 
   1509  1.54    itojun 	ip = mtod(m, struct ip *);
   1510  1.22   mycroft 
   1511  1.54    itojun 	m_adj(m, off);
   1512  1.15   mycroft 	m->m_pkthdr.rcvif = vifp->v_ifp;
   1513  1.12    brezak 	ifq = &ipintrq;
   1514  1.53   thorpej 	s = splnet();
   1515  1.12    brezak 	if (IF_QFULL(ifq)) {
   1516  1.12    brezak 		IF_DROP(ifq);
   1517  1.12    brezak 		m_freem(m);
   1518  1.12    brezak 	} else {
   1519  1.12    brezak 		IF_ENQUEUE(ifq, m);
   1520  1.12    brezak 		/*
   1521  1.12    brezak 		 * normally we would need a "schednetisr(NETISR_IP)"
   1522  1.12    brezak 		 * here but we were called by ip_input and it is going
   1523  1.12    brezak 		 * to loop back & try to dequeue the packet we just
   1524  1.12    brezak 		 * queued as soon as we return so we avoid the
   1525  1.12    brezak 		 * unnecessary software interrrupt.
   1526  1.12    brezak 		 */
   1527  1.12    brezak 	}
   1528  1.12    brezak 	splx(s);
   1529  1.54    itojun }
   1530  1.54    itojun 
   1531  1.54    itojun /*
   1532  1.54    itojun  * Check if the packet should be grabbed by us.
   1533  1.54    itojun  */
   1534  1.54    itojun static int
   1535  1.54    itojun vif_encapcheck(m, off, proto, arg)
   1536  1.54    itojun 	const struct mbuf *m;
   1537  1.54    itojun 	int off;
   1538  1.54    itojun 	int proto;
   1539  1.54    itojun 	void *arg;
   1540  1.54    itojun {
   1541  1.54    itojun 	struct vif *vifp;
   1542  1.54    itojun 	struct ip ip;
   1543  1.54    itojun 
   1544  1.54    itojun #ifdef DIAGNOSTIC
   1545  1.54    itojun 	if (!arg || proto != IPPROTO_IPV4)
   1546  1.54    itojun 		panic("unexpected arg in vif_encapcheck");
   1547  1.54    itojun #endif
   1548  1.54    itojun 
   1549  1.54    itojun 	/*
   1550  1.54    itojun 	 * do not grab the packet if it's not to a multicast destination or if
   1551  1.54    itojun 	 * we don't have an encapsulating tunnel with the source.
   1552  1.54    itojun 	 * Note:  This code assumes that the remote site IP address
   1553  1.54    itojun 	 * uniquely identifies the tunnel (i.e., that this site has
   1554  1.54    itojun 	 * at most one tunnel with the remote site).
   1555  1.54    itojun 	 */
   1556  1.54    itojun 
   1557  1.54    itojun 	/* LINTED const cast */
   1558  1.54    itojun 	m_copydata((struct mbuf *)m, off, sizeof(ip), (caddr_t)&ip);
   1559  1.54    itojun 	if (!IN_MULTICAST(ip.ip_dst.s_addr))
   1560  1.54    itojun 		return 0;
   1561  1.54    itojun 
   1562  1.54    itojun 	/* LINTED const cast */
   1563  1.54    itojun 	m_copydata((struct mbuf *)m, 0, sizeof(ip), (caddr_t)&ip);
   1564  1.54    itojun 	if (!in_hosteq(ip.ip_src, last_encap_src)) {
   1565  1.54    itojun 		vifp = (struct vif *)arg;
   1566  1.54    itojun 		if (vifp->v_flags & VIFF_TUNNEL &&
   1567  1.54    itojun 		    in_hosteq(vifp->v_rmt_addr, ip.ip_src))
   1568  1.54    itojun 			;
   1569  1.54    itojun 		else
   1570  1.54    itojun 			return 0;
   1571  1.54    itojun 		last_encap_vif = vifp;
   1572  1.54    itojun 		last_encap_src = ip.ip_src;
   1573  1.54    itojun 	} else
   1574  1.54    itojun 		vifp = last_encap_vif;
   1575  1.54    itojun 
   1576  1.54    itojun 	/* 32bit match, since we have checked ip_src only */
   1577  1.54    itojun 	return 32;
   1578   1.1   hpeyerl }
   1579  1.15   mycroft 
   1580  1.15   mycroft /*
   1581  1.15   mycroft  * Token bucket filter module
   1582  1.15   mycroft  */
   1583  1.15   mycroft static void
   1584  1.31   mycroft tbf_control(vifp, m, ip, len)
   1585  1.48  augustss 	struct vif *vifp;
   1586  1.48  augustss 	struct mbuf *m;
   1587  1.48  augustss 	struct ip *ip;
   1588  1.48  augustss 	u_int32_t len;
   1589  1.15   mycroft {
   1590  1.15   mycroft 
   1591  1.31   mycroft 	if (len > MAX_BKT_SIZE) {
   1592  1.31   mycroft 		/* drop if packet is too large */
   1593  1.31   mycroft 		mrtstat.mrts_pkt2large++;
   1594  1.31   mycroft 		m_freem(m);
   1595  1.31   mycroft 		return;
   1596  1.31   mycroft 	}
   1597  1.31   mycroft 
   1598  1.21   mycroft 	tbf_update_tokens(vifp);
   1599  1.15   mycroft 
   1600  1.21   mycroft 	/*
   1601  1.21   mycroft 	 * If there are enough tokens, and the queue is empty, send this packet
   1602  1.21   mycroft 	 * out immediately.  Otherwise, try to insert it on this vif's queue.
   1603  1.21   mycroft 	 */
   1604  1.31   mycroft 	if (vifp->tbf_q_len == 0) {
   1605  1.31   mycroft 		if (len <= vifp->tbf_n_tok) {
   1606  1.31   mycroft 			vifp->tbf_n_tok -= len;
   1607  1.21   mycroft 			tbf_send_packet(vifp, m);
   1608  1.21   mycroft 		} else {
   1609  1.21   mycroft 			/* queue packet and timeout till later */
   1610  1.31   mycroft 			tbf_queue(vifp, m);
   1611  1.47   thorpej 			callout_reset(&vifp->v_repq_ch, TBF_REPROCESS,
   1612  1.47   thorpej 			    tbf_reprocess_q, vifp);
   1613  1.21   mycroft 		}
   1614  1.15   mycroft 	} else {
   1615  1.31   mycroft 		if (vifp->tbf_q_len >= vifp->tbf_max_q_len &&
   1616  1.21   mycroft 		    !tbf_dq_sel(vifp, ip)) {
   1617  1.21   mycroft 			/* queue length too much, and couldn't make room */
   1618  1.21   mycroft 			mrtstat.mrts_q_overflow++;
   1619  1.21   mycroft 			m_freem(m);
   1620  1.21   mycroft 		} else {
   1621  1.21   mycroft 			/* queue length low enough, or made room */
   1622  1.31   mycroft 			tbf_queue(vifp, m);
   1623  1.21   mycroft 			tbf_process_q(vifp);
   1624  1.21   mycroft 		}
   1625  1.15   mycroft 	}
   1626  1.15   mycroft }
   1627  1.15   mycroft 
   1628  1.15   mycroft /*
   1629  1.15   mycroft  * adds a packet to the queue at the interface
   1630  1.15   mycroft  */
   1631  1.15   mycroft static void
   1632  1.31   mycroft tbf_queue(vifp, m)
   1633  1.48  augustss 	struct vif *vifp;
   1634  1.48  augustss 	struct mbuf *m;
   1635  1.15   mycroft {
   1636  1.48  augustss 	int s = splsoftnet();
   1637  1.15   mycroft 
   1638  1.31   mycroft 	/* insert at tail */
   1639  1.31   mycroft 	*vifp->tbf_t = m;
   1640  1.31   mycroft 	vifp->tbf_t = &m->m_nextpkt;
   1641  1.31   mycroft 	vifp->tbf_q_len++;
   1642  1.15   mycroft 
   1643  1.31   mycroft 	splx(s);
   1644  1.15   mycroft }
   1645  1.15   mycroft 
   1646  1.15   mycroft 
   1647  1.15   mycroft /*
   1648  1.15   mycroft  * processes the queue at the interface
   1649  1.15   mycroft  */
   1650  1.15   mycroft static void
   1651  1.15   mycroft tbf_process_q(vifp)
   1652  1.48  augustss 	struct vif *vifp;
   1653  1.15   mycroft {
   1654  1.48  augustss 	struct mbuf *m;
   1655  1.48  augustss 	int len;
   1656  1.48  augustss 	int s = splsoftnet();
   1657  1.15   mycroft 
   1658  1.31   mycroft 	/*
   1659  1.31   mycroft 	 * Loop through the queue at the interface and send as many packets
   1660  1.31   mycroft 	 * as possible.
   1661  1.31   mycroft 	 */
   1662  1.31   mycroft 	for (m = vifp->tbf_q;
   1663  1.31   mycroft 	    m != 0;
   1664  1.31   mycroft 	    m = vifp->tbf_q) {
   1665  1.31   mycroft 		len = mtod(m, struct ip *)->ip_len;
   1666  1.31   mycroft 
   1667  1.31   mycroft 		/* determine if the packet can be sent */
   1668  1.31   mycroft 		if (len <= vifp->tbf_n_tok) {
   1669  1.31   mycroft 			/* if so,
   1670  1.31   mycroft 			 * reduce no of tokens, dequeue the packet,
   1671  1.31   mycroft 			 * send the packet.
   1672  1.31   mycroft 			 */
   1673  1.31   mycroft 			if ((vifp->tbf_q = m->m_nextpkt) == 0)
   1674  1.31   mycroft 				vifp->tbf_t = &vifp->tbf_q;
   1675  1.31   mycroft 			--vifp->tbf_q_len;
   1676  1.15   mycroft 
   1677  1.31   mycroft 			m->m_nextpkt = 0;
   1678  1.31   mycroft 			vifp->tbf_n_tok -= len;
   1679  1.31   mycroft 			tbf_send_packet(vifp, m);
   1680  1.31   mycroft 		} else
   1681  1.31   mycroft 			break;
   1682  1.31   mycroft 	}
   1683  1.31   mycroft 	splx(s);
   1684  1.15   mycroft }
   1685  1.15   mycroft 
   1686  1.15   mycroft static void
   1687  1.20   mycroft tbf_reprocess_q(arg)
   1688  1.20   mycroft 	void *arg;
   1689  1.15   mycroft {
   1690  1.48  augustss 	struct vif *vifp = arg;
   1691  1.15   mycroft 
   1692  1.30   mycroft 	if (ip_mrouter == 0)
   1693  1.20   mycroft 		return;
   1694  1.15   mycroft 
   1695  1.20   mycroft 	tbf_update_tokens(vifp);
   1696  1.20   mycroft 	tbf_process_q(vifp);
   1697  1.15   mycroft 
   1698  1.31   mycroft 	if (vifp->tbf_q_len != 0)
   1699  1.47   thorpej 		callout_reset(&vifp->v_repq_ch, TBF_REPROCESS,
   1700  1.47   thorpej 		    tbf_reprocess_q, vifp);
   1701  1.15   mycroft }
   1702  1.15   mycroft 
   1703  1.15   mycroft /* function that will selectively discard a member of the queue
   1704  1.31   mycroft  * based on the precedence value and the priority
   1705  1.15   mycroft  */
   1706  1.15   mycroft static int
   1707  1.15   mycroft tbf_dq_sel(vifp, ip)
   1708  1.48  augustss 	struct vif *vifp;
   1709  1.48  augustss 	struct ip *ip;
   1710  1.15   mycroft {
   1711  1.48  augustss 	u_int p;
   1712  1.48  augustss 	struct mbuf **mp, *m;
   1713  1.48  augustss 	int s = splsoftnet();
   1714  1.31   mycroft 
   1715  1.31   mycroft 	p = priority(vifp, ip);
   1716  1.31   mycroft 
   1717  1.31   mycroft 	for (mp = &vifp->tbf_q, m = *mp;
   1718  1.31   mycroft 	    m != 0;
   1719  1.31   mycroft 	    mp = &m->m_nextpkt, m = *mp) {
   1720  1.31   mycroft 		if (p > priority(vifp, mtod(m, struct ip *))) {
   1721  1.31   mycroft 			if ((*mp = m->m_nextpkt) == 0)
   1722  1.31   mycroft 				vifp->tbf_t = mp;
   1723  1.31   mycroft 			--vifp->tbf_q_len;
   1724  1.31   mycroft 
   1725  1.31   mycroft 			m_freem(m);
   1726  1.31   mycroft 			mrtstat.mrts_drop_sel++;
   1727  1.31   mycroft 			splx(s);
   1728  1.31   mycroft 			return (1);
   1729  1.31   mycroft 		}
   1730  1.15   mycroft 	}
   1731  1.31   mycroft 	splx(s);
   1732  1.31   mycroft 	return (0);
   1733  1.15   mycroft }
   1734  1.15   mycroft 
   1735  1.15   mycroft static void
   1736  1.31   mycroft tbf_send_packet(vifp, m)
   1737  1.48  augustss 	struct vif *vifp;
   1738  1.48  augustss 	struct mbuf *m;
   1739  1.15   mycroft {
   1740  1.31   mycroft 	int error;
   1741  1.31   mycroft 	int s = splsoftnet();
   1742  1.31   mycroft 
   1743  1.31   mycroft 	if (vifp->v_flags & VIFF_TUNNEL) {
   1744  1.31   mycroft 		/* If tunnel options */
   1745  1.43    itojun #ifdef IPSEC
   1746  1.46    itojun 		/* Don't lookup socket in forwading case */
   1747  1.52    itojun 		(void)ipsec_setsocket(m, NULL);
   1748  1.43    itojun #endif
   1749  1.31   mycroft 		ip_output(m, (struct mbuf *)0, &vifp->v_route,
   1750  1.31   mycroft 			  IP_FORWARDING, (struct ip_moptions *)0);
   1751  1.31   mycroft 	} else {
   1752  1.31   mycroft 		/* if physical interface option, extract the options and then send */
   1753  1.31   mycroft 		struct ip_moptions imo;
   1754  1.15   mycroft 
   1755  1.31   mycroft 		imo.imo_multicast_ifp = vifp->v_ifp;
   1756  1.31   mycroft 		imo.imo_multicast_ttl = mtod(m, struct ip *)->ip_ttl - 1;
   1757  1.31   mycroft 		imo.imo_multicast_loop = 1;
   1758  1.15   mycroft #ifdef RSVP_ISI
   1759  1.31   mycroft 		imo.imo_multicast_vif = -1;
   1760   1.1   hpeyerl #endif
   1761  1.15   mycroft 
   1762  1.43    itojun #ifdef IPSEC
   1763  1.46    itojun 		/* Don't lookup socket in forwading case */
   1764  1.52    itojun 		(void)ipsec_setsocket(m, NULL);
   1765  1.43    itojun #endif
   1766  1.31   mycroft 		error = ip_output(m, (struct mbuf *)0, (struct route *)0,
   1767  1.31   mycroft 				  IP_FORWARDING|IP_MULTICASTOPTS, &imo);
   1768  1.31   mycroft 
   1769  1.31   mycroft 		if (mrtdebug & DEBUG_XMIT)
   1770  1.42   nathanw 			log(LOG_DEBUG, "phyint_send on vif %ld err %d\n",
   1771  1.42   nathanw 			    (long)(vifp-viftable), error);
   1772  1.31   mycroft 	}
   1773  1.31   mycroft 	splx(s);
   1774  1.15   mycroft }
   1775  1.15   mycroft 
   1776  1.15   mycroft /* determine the current time and then
   1777  1.15   mycroft  * the elapsed time (between the last time and time now)
   1778  1.15   mycroft  * in milliseconds & update the no. of tokens in the bucket
   1779  1.15   mycroft  */
   1780  1.15   mycroft static void
   1781  1.15   mycroft tbf_update_tokens(vifp)
   1782  1.48  augustss 	struct vif *vifp;
   1783  1.15   mycroft {
   1784  1.31   mycroft 	struct timeval tp;
   1785  1.48  augustss 	u_int32_t tm;
   1786  1.48  augustss 	int s = splsoftnet();
   1787  1.15   mycroft 
   1788  1.31   mycroft 	microtime(&tp);
   1789  1.15   mycroft 
   1790  1.31   mycroft 	TV_DELTA(tp, vifp->tbf_last_pkt_t, tm);
   1791  1.15   mycroft 
   1792  1.31   mycroft 	/*
   1793  1.31   mycroft 	 * This formula is actually
   1794  1.31   mycroft 	 * "time in seconds" * "bytes/second".
   1795  1.31   mycroft 	 *
   1796  1.31   mycroft 	 * (tm / 1000000) * (v_rate_limit * 1000 * (1000/1024) / 8)
   1797  1.31   mycroft 	 *
   1798  1.31   mycroft 	 * The (1000/1024) was introduced in add_vif to optimize
   1799  1.31   mycroft 	 * this divide into a shift.
   1800  1.31   mycroft 	 */
   1801  1.31   mycroft 	vifp->tbf_n_tok += tm * vifp->v_rate_limit / 8192;
   1802  1.31   mycroft 	vifp->tbf_last_pkt_t = tp;
   1803  1.15   mycroft 
   1804  1.31   mycroft 	if (vifp->tbf_n_tok > MAX_BKT_SIZE)
   1805  1.31   mycroft 		vifp->tbf_n_tok = MAX_BKT_SIZE;
   1806  1.15   mycroft 
   1807  1.31   mycroft 	splx(s);
   1808  1.15   mycroft }
   1809  1.15   mycroft 
   1810  1.15   mycroft static int
   1811  1.15   mycroft priority(vifp, ip)
   1812  1.48  augustss     struct vif *vifp;
   1813  1.48  augustss     struct ip *ip;
   1814  1.15   mycroft {
   1815  1.48  augustss     int prio;
   1816  1.15   mycroft 
   1817  1.15   mycroft     /* temporary hack; may add general packet classifier some day */
   1818  1.15   mycroft 
   1819  1.15   mycroft     /*
   1820  1.15   mycroft      * The UDP port space is divided up into four priority ranges:
   1821  1.15   mycroft      * [0, 16384)     : unclassified - lowest priority
   1822  1.15   mycroft      * [16384, 32768) : audio - highest priority
   1823  1.15   mycroft      * [32768, 49152) : whiteboard - medium priority
   1824  1.15   mycroft      * [49152, 65536) : video - low priority
   1825  1.15   mycroft      */
   1826  1.15   mycroft     if (ip->ip_p == IPPROTO_UDP) {
   1827  1.15   mycroft 	struct udphdr *udp = (struct udphdr *)(((char *)ip) + (ip->ip_hl << 2));
   1828  1.15   mycroft 
   1829  1.15   mycroft 	switch (ntohs(udp->uh_dport) & 0xc000) {
   1830  1.15   mycroft 	    case 0x4000:
   1831  1.15   mycroft 		prio = 70;
   1832  1.15   mycroft 		break;
   1833  1.15   mycroft 	    case 0x8000:
   1834  1.15   mycroft 		prio = 60;
   1835  1.15   mycroft 		break;
   1836  1.15   mycroft 	    case 0xc000:
   1837  1.15   mycroft 		prio = 55;
   1838  1.15   mycroft 		break;
   1839  1.15   mycroft 	    default:
   1840  1.15   mycroft 		prio = 50;
   1841  1.15   mycroft 		break;
   1842  1.15   mycroft 	}
   1843  1.15   mycroft 
   1844  1.30   mycroft 	if (tbfdebug > 1)
   1845  1.30   mycroft 	    log(LOG_DEBUG, "port %x prio %d\n", ntohs(udp->uh_dport), prio);
   1846  1.15   mycroft     } else
   1847  1.15   mycroft 	prio = 50;
   1848  1.15   mycroft 
   1849  1.15   mycroft 
   1850  1.15   mycroft     return (prio);
   1851  1.15   mycroft }
   1852  1.15   mycroft 
   1853  1.15   mycroft /*
   1854  1.15   mycroft  * End of token bucket filter modifications
   1855  1.15   mycroft  */
   1856  1.15   mycroft 
   1857  1.15   mycroft #ifdef RSVP_ISI
   1858  1.15   mycroft 
   1859  1.15   mycroft int
   1860  1.15   mycroft ip_rsvp_vif_init(so, m)
   1861  1.15   mycroft     struct socket *so;
   1862  1.15   mycroft     struct mbuf *m;
   1863  1.15   mycroft {
   1864  1.15   mycroft     int i;
   1865  1.48  augustss     int s;
   1866  1.15   mycroft 
   1867  1.15   mycroft     if (rsvpdebug)
   1868  1.34  christos 	printf("ip_rsvp_vif_init: so_type = %d, pr_protocol = %d\n",
   1869  1.33  christos 	    so->so_type, so->so_proto->pr_protocol);
   1870  1.15   mycroft 
   1871  1.15   mycroft     if (so->so_type != SOCK_RAW || so->so_proto->pr_protocol != IPPROTO_RSVP)
   1872  1.15   mycroft 	return (EOPNOTSUPP);
   1873  1.15   mycroft 
   1874  1.15   mycroft     /* Check mbuf. */
   1875  1.30   mycroft     if (m == 0 || m->m_len != sizeof(int)) {
   1876  1.15   mycroft 	return (EINVAL);
   1877  1.15   mycroft     }
   1878  1.15   mycroft     i = *(mtod(m, int *));
   1879  1.15   mycroft 
   1880  1.15   mycroft     if (rsvpdebug)
   1881  1.34  christos 	printf("ip_rsvp_vif_init: vif = %d rsvp_on = %d\n",i,rsvp_on);
   1882  1.15   mycroft 
   1883  1.24   mycroft     s = splsoftnet();
   1884  1.15   mycroft 
   1885  1.15   mycroft     /* Check vif. */
   1886  1.15   mycroft     if (!legal_vif_num(i)) {
   1887  1.15   mycroft 	splx(s);
   1888  1.15   mycroft 	return (EADDRNOTAVAIL);
   1889  1.15   mycroft     }
   1890  1.15   mycroft 
   1891  1.15   mycroft     /* Check if socket is available. */
   1892  1.30   mycroft     if (viftable[i].v_rsvpd != 0) {
   1893  1.15   mycroft 	splx(s);
   1894  1.15   mycroft 	return (EADDRINUSE);
   1895  1.15   mycroft     }
   1896  1.15   mycroft 
   1897  1.15   mycroft     viftable[i].v_rsvpd = so;
   1898  1.15   mycroft     /* This may seem silly, but we need to be sure we don't over-increment
   1899  1.15   mycroft      * the RSVP counter, in case something slips up.
   1900  1.15   mycroft      */
   1901  1.15   mycroft     if (!viftable[i].v_rsvp_on) {
   1902  1.15   mycroft 	viftable[i].v_rsvp_on = 1;
   1903  1.15   mycroft 	rsvp_on++;
   1904  1.15   mycroft     }
   1905  1.15   mycroft 
   1906  1.15   mycroft     splx(s);
   1907  1.15   mycroft     return (0);
   1908  1.15   mycroft }
   1909  1.15   mycroft 
   1910  1.15   mycroft int
   1911  1.15   mycroft ip_rsvp_vif_done(so, m)
   1912  1.15   mycroft     struct socket *so;
   1913  1.15   mycroft     struct mbuf *m;
   1914  1.15   mycroft {
   1915  1.15   mycroft     int i;
   1916  1.48  augustss     int s;
   1917  1.15   mycroft 
   1918  1.15   mycroft     if (rsvpdebug)
   1919  1.34  christos 	printf("ip_rsvp_vif_done: so_type = %d, pr_protocol = %d\n",
   1920  1.15   mycroft 	       so->so_type, so->so_proto->pr_protocol);
   1921  1.15   mycroft 
   1922  1.15   mycroft     if (so->so_type != SOCK_RAW || so->so_proto->pr_protocol != IPPROTO_RSVP)
   1923  1.15   mycroft 	return (EOPNOTSUPP);
   1924  1.15   mycroft 
   1925  1.15   mycroft     /* Check mbuf. */
   1926  1.30   mycroft     if (m == 0 || m->m_len != sizeof(int)) {
   1927  1.15   mycroft 	return (EINVAL);
   1928  1.15   mycroft     }
   1929  1.15   mycroft     i = *(mtod(m, int *));
   1930  1.15   mycroft 
   1931  1.24   mycroft     s = splsoftnet();
   1932  1.15   mycroft 
   1933  1.15   mycroft     /* Check vif. */
   1934  1.15   mycroft     if (!legal_vif_num(i)) {
   1935  1.15   mycroft 	splx(s);
   1936  1.15   mycroft         return (EADDRNOTAVAIL);
   1937  1.15   mycroft     }
   1938  1.15   mycroft 
   1939  1.15   mycroft     if (rsvpdebug)
   1940  1.34  christos 	printf("ip_rsvp_vif_done: v_rsvpd = %x so = %x\n",
   1941  1.33  christos 	    viftable[i].v_rsvpd, so);
   1942  1.15   mycroft 
   1943  1.30   mycroft     viftable[i].v_rsvpd = 0;
   1944  1.15   mycroft     /* This may seem silly, but we need to be sure we don't over-decrement
   1945  1.15   mycroft      * the RSVP counter, in case something slips up.
   1946  1.15   mycroft      */
   1947  1.15   mycroft     if (viftable[i].v_rsvp_on) {
   1948  1.15   mycroft 	viftable[i].v_rsvp_on = 0;
   1949  1.15   mycroft 	rsvp_on--;
   1950  1.15   mycroft     }
   1951  1.15   mycroft 
   1952  1.15   mycroft     splx(s);
   1953  1.15   mycroft     return (0);
   1954  1.15   mycroft }
   1955  1.15   mycroft 
   1956  1.25  christos void
   1957  1.15   mycroft ip_rsvp_force_done(so)
   1958  1.15   mycroft     struct socket *so;
   1959  1.15   mycroft {
   1960  1.15   mycroft     int vifi;
   1961  1.48  augustss     int s;
   1962  1.15   mycroft 
   1963  1.15   mycroft     /* Don't bother if it is not the right type of socket. */
   1964  1.15   mycroft     if (so->so_type != SOCK_RAW || so->so_proto->pr_protocol != IPPROTO_RSVP)
   1965  1.15   mycroft 	return;
   1966  1.15   mycroft 
   1967  1.24   mycroft     s = splsoftnet();
   1968  1.15   mycroft 
   1969  1.15   mycroft     /* The socket may be attached to more than one vif...this
   1970  1.15   mycroft      * is perfectly legal.
   1971  1.15   mycroft      */
   1972  1.15   mycroft     for (vifi = 0; vifi < numvifs; vifi++) {
   1973  1.15   mycroft 	if (viftable[vifi].v_rsvpd == so) {
   1974  1.30   mycroft 	    viftable[vifi].v_rsvpd = 0;
   1975  1.15   mycroft 	    /* This may seem silly, but we need to be sure we don't
   1976  1.15   mycroft 	     * over-decrement the RSVP counter, in case something slips up.
   1977  1.15   mycroft 	     */
   1978  1.15   mycroft 	    if (viftable[vifi].v_rsvp_on) {
   1979  1.15   mycroft 		viftable[vifi].v_rsvp_on = 0;
   1980  1.15   mycroft 		rsvp_on--;
   1981  1.15   mycroft 	    }
   1982  1.15   mycroft 	}
   1983  1.15   mycroft     }
   1984  1.15   mycroft 
   1985  1.15   mycroft     splx(s);
   1986  1.15   mycroft     return;
   1987  1.15   mycroft }
   1988  1.15   mycroft 
   1989  1.25  christos void
   1990  1.15   mycroft rsvp_input(m, ifp)
   1991  1.15   mycroft     struct mbuf *m;
   1992  1.15   mycroft     struct ifnet *ifp;
   1993  1.15   mycroft {
   1994  1.15   mycroft     int vifi;
   1995  1.48  augustss     struct ip *ip = mtod(m, struct ip *);
   1996  1.15   mycroft     static struct sockaddr_in rsvp_src = { sizeof(sin), AF_INET };
   1997  1.48  augustss     int s;
   1998  1.15   mycroft 
   1999  1.15   mycroft     if (rsvpdebug)
   2000  1.34  christos 	printf("rsvp_input: rsvp_on %d\n",rsvp_on);
   2001  1.15   mycroft 
   2002  1.15   mycroft     /* Can still get packets with rsvp_on = 0 if there is a local member
   2003  1.15   mycroft      * of the group to which the RSVP packet is addressed.  But in this
   2004  1.15   mycroft      * case we want to throw the packet away.
   2005  1.15   mycroft      */
   2006  1.15   mycroft     if (!rsvp_on) {
   2007  1.15   mycroft 	m_freem(m);
   2008  1.15   mycroft 	return;
   2009  1.15   mycroft     }
   2010  1.15   mycroft 
   2011  1.15   mycroft     /* If the old-style non-vif-associated socket is set, then use
   2012  1.15   mycroft      * it and ignore the new ones.
   2013  1.15   mycroft      */
   2014  1.30   mycroft     if (ip_rsvpd != 0) {
   2015  1.15   mycroft 	if (rsvpdebug)
   2016  1.34  christos 	    printf("rsvp_input: Sending packet up old-style socket\n");
   2017  1.43    itojun 	rip_input(m);	/*XXX*/
   2018  1.15   mycroft 	return;
   2019  1.15   mycroft     }
   2020  1.15   mycroft 
   2021  1.24   mycroft     s = splsoftnet();
   2022  1.15   mycroft 
   2023  1.15   mycroft     if (rsvpdebug)
   2024  1.34  christos 	printf("rsvp_input: check vifs\n");
   2025  1.15   mycroft 
   2026  1.15   mycroft     /* Find which vif the packet arrived on. */
   2027  1.15   mycroft     for (vifi = 0; vifi < numvifs; vifi++) {
   2028  1.15   mycroft 	if (viftable[vifi].v_ifp == ifp)
   2029  1.15   mycroft 	    break;
   2030  1.15   mycroft     }
   2031  1.15   mycroft 
   2032  1.15   mycroft     if (vifi == numvifs) {
   2033  1.15   mycroft 	/* Can't find vif packet arrived on. Drop packet. */
   2034  1.15   mycroft 	if (rsvpdebug)
   2035  1.34  christos 	    printf("rsvp_input: Can't find vif for packet...dropping it.\n");
   2036  1.15   mycroft 	m_freem(m);
   2037  1.15   mycroft 	splx(s);
   2038  1.15   mycroft 	return;
   2039  1.15   mycroft     }
   2040  1.15   mycroft 
   2041  1.15   mycroft     if (rsvpdebug)
   2042  1.34  christos 	printf("rsvp_input: check socket\n");
   2043  1.15   mycroft 
   2044  1.30   mycroft     if (viftable[vifi].v_rsvpd == 0) {
   2045  1.15   mycroft 	/* drop packet, since there is no specific socket for this
   2046  1.15   mycroft 	 * interface */
   2047  1.15   mycroft 	if (rsvpdebug)
   2048  1.34  christos 	    printf("rsvp_input: No socket defined for vif %d\n",vifi);
   2049  1.15   mycroft 	m_freem(m);
   2050  1.15   mycroft 	splx(s);
   2051  1.15   mycroft 	return;
   2052  1.15   mycroft     }
   2053  1.15   mycroft 
   2054  1.15   mycroft     rsvp_src.sin_addr = ip->ip_src;
   2055  1.15   mycroft 
   2056  1.15   mycroft     if (rsvpdebug && m)
   2057  1.34  christos 	printf("rsvp_input: m->m_len = %d, sbspace() = %d\n",
   2058  1.15   mycroft 	       m->m_len,sbspace(&viftable[vifi].v_rsvpd->so_rcv));
   2059  1.15   mycroft 
   2060  1.15   mycroft     if (socket_send(viftable[vifi].v_rsvpd, m, &rsvp_src) < 0)
   2061  1.15   mycroft 	if (rsvpdebug)
   2062  1.34  christos 	    printf("rsvp_input: Failed to append to socket\n");
   2063  1.15   mycroft     else
   2064  1.15   mycroft 	if (rsvpdebug)
   2065  1.34  christos 	    printf("rsvp_input: send packet up\n");
   2066  1.15   mycroft 
   2067  1.15   mycroft     splx(s);
   2068  1.15   mycroft }
   2069  1.15   mycroft #endif /* RSVP_ISI */
   2070