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