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