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