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