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