ip_mroute.c revision 41878
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.50 1998/12/07 21:58:41 archie 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		break;
433
434	case MRT_ASSERT:
435		error = sooptcopyin(sopt, &optval, sizeof optval,
436				    sizeof optval);
437		if (error)
438			break;
439		set_assert(optval);
440		break;
441
442	default:
443		error = EOPNOTSUPP;
444		break;
445	}
446	return (error);
447}
448
449#ifndef MROUTE_LKM
450int (*ip_mrouter_set)(struct socket *, struct sockopt *) = X_ip_mrouter_set;
451#endif
452
453/*
454 * Handle MRT getsockopt commands
455 */
456static int
457X_ip_mrouter_get(so, sopt)
458	struct socket *so;
459	struct sockopt *sopt;
460{
461	int error;
462	static int version = 0x0305; /* !!! why is this here? XXX */
463
464	switch (sopt->sopt_name) {
465	case MRT_VERSION:
466		error = sooptcopyout(sopt, &version, sizeof version);
467		break;
468
469	case MRT_ASSERT:
470		error = sooptcopyout(sopt, &pim_assert, sizeof pim_assert);
471		break;
472	default:
473		error = EOPNOTSUPP;
474		break;
475	}
476	return (error);
477}
478
479#ifndef MROUTE_LKM
480int (*ip_mrouter_get)(struct socket *, struct sockopt *) = X_ip_mrouter_get;
481#endif
482
483/*
484 * Handle ioctl commands to obtain information from the cache
485 */
486static int
487X_mrt_ioctl(cmd, data)
488    int cmd;
489    caddr_t data;
490{
491    int error = 0;
492
493    switch (cmd) {
494	case (SIOCGETVIFCNT):
495	    return (get_vif_cnt((struct sioc_vif_req *)data));
496	    break;
497	case (SIOCGETSGCNT):
498	    return (get_sg_cnt((struct sioc_sg_req *)data));
499	    break;
500	default:
501	    return (EINVAL);
502	    break;
503    }
504    return error;
505}
506
507#ifndef MROUTE_LKM
508int (*mrt_ioctl)(int, caddr_t) = X_mrt_ioctl;
509#endif
510
511/*
512 * returns the packet, byte, rpf-failure count for the source group provided
513 */
514static int
515get_sg_cnt(req)
516    register struct sioc_sg_req *req;
517{
518    register struct mfc *rt;
519    int s;
520
521    s = splnet();
522    MFCFIND(req->src.s_addr, req->grp.s_addr, rt);
523    splx(s);
524    if (rt != NULL) {
525	req->pktcnt = rt->mfc_pkt_cnt;
526	req->bytecnt = rt->mfc_byte_cnt;
527	req->wrong_if = rt->mfc_wrong_if;
528    } else
529	req->pktcnt = req->bytecnt = req->wrong_if = 0xffffffff;
530
531    return 0;
532}
533
534/*
535 * returns the input and output packet and byte counts on the vif provided
536 */
537static int
538get_vif_cnt(req)
539    register struct sioc_vif_req *req;
540{
541    register vifi_t vifi = req->vifi;
542
543    if (vifi >= numvifs) return EINVAL;
544
545    req->icount = viftable[vifi].v_pkt_in;
546    req->ocount = viftable[vifi].v_pkt_out;
547    req->ibytes = viftable[vifi].v_bytes_in;
548    req->obytes = viftable[vifi].v_bytes_out;
549
550    return 0;
551}
552
553/*
554 * Enable multicast routing
555 */
556static int
557ip_mrouter_init(so, version)
558	struct socket *so;
559	int version;
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    int error, s;
695    struct tbf *v_tbf = tbftable + vifcp->vifc_vifi;
696
697    if (vifcp->vifc_vifi >= MAXVIFS)  return EINVAL;
698    if (vifp->v_lcl_addr.s_addr != 0) return EADDRINUSE;
699
700    /* Find the interface with an address in AF_INET family */
701    sin.sin_addr = vifcp->vifc_lcl_addr;
702    ifa = ifa_ifwithaddr((struct sockaddr *)&sin);
703    if (ifa == 0) return EADDRNOTAVAIL;
704    ifp = ifa->ifa_ifp;
705
706    if (vifcp->vifc_flags & VIFF_TUNNEL) {
707	if ((vifcp->vifc_flags & VIFF_SRCRT) == 0) {
708		/*
709		 * An encapsulating tunnel is wanted.  Tell ipip_input() to
710		 * start paying attention to encapsulated packets.
711		 */
712		if (have_encap_tunnel == 0) {
713			have_encap_tunnel = 1;
714			for (s = 0; s < MAXVIFS; ++s) {
715				multicast_decap_if[s].if_name = "mdecap";
716				multicast_decap_if[s].if_unit = s;
717			}
718		}
719		/*
720		 * Set interface to fake encapsulator interface
721		 */
722		ifp = &multicast_decap_if[vifcp->vifc_vifi];
723		/*
724		 * Prepare cached route entry
725		 */
726		bzero(&vifp->v_route, sizeof(vifp->v_route));
727	} else {
728	    log(LOG_ERR, "source routed tunnels not supported\n");
729	    return EOPNOTSUPP;
730	}
731    } else {
732	/* Make sure the interface supports multicast */
733	if ((ifp->if_flags & IFF_MULTICAST) == 0)
734	    return EOPNOTSUPP;
735
736	/* Enable promiscuous reception of all IP multicasts from the if */
737	s = splnet();
738	error = if_allmulti(ifp, 1);
739	splx(s);
740	if (error)
741	    return error;
742    }
743
744    s = splnet();
745    /* define parameters for the tbf structure */
746    vifp->v_tbf = v_tbf;
747    GET_TIME(vifp->v_tbf->tbf_last_pkt_t);
748    vifp->v_tbf->tbf_n_tok = 0;
749    vifp->v_tbf->tbf_q_len = 0;
750    vifp->v_tbf->tbf_max_q_len = MAXQSIZE;
751    vifp->v_tbf->tbf_q = vifp->v_tbf->tbf_t = NULL;
752
753    vifp->v_flags     = vifcp->vifc_flags;
754    vifp->v_threshold = vifcp->vifc_threshold;
755    vifp->v_lcl_addr  = vifcp->vifc_lcl_addr;
756    vifp->v_rmt_addr  = vifcp->vifc_rmt_addr;
757    vifp->v_ifp       = ifp;
758    /* scaling up here allows division by 1024 in critical code */
759    vifp->v_rate_limit= vifcp->vifc_rate_limit * 1024 / 1000;
760    vifp->v_rsvp_on   = 0;
761    vifp->v_rsvpd     = NULL;
762    /* initialize per vif pkt counters */
763    vifp->v_pkt_in    = 0;
764    vifp->v_pkt_out   = 0;
765    vifp->v_bytes_in  = 0;
766    vifp->v_bytes_out = 0;
767    splx(s);
768
769    /* Adjust numvifs up if the vifi is higher than numvifs */
770    if (numvifs <= vifcp->vifc_vifi) numvifs = vifcp->vifc_vifi + 1;
771
772    if (mrtdebug)
773	log(LOG_DEBUG, "add_vif #%d, lcladdr %lx, %s %lx, thresh %x, rate %d\n",
774	    vifcp->vifc_vifi,
775	    (u_long)ntohl(vifcp->vifc_lcl_addr.s_addr),
776	    (vifcp->vifc_flags & VIFF_TUNNEL) ? "rmtaddr" : "mask",
777	    (u_long)ntohl(vifcp->vifc_rmt_addr.s_addr),
778	    vifcp->vifc_threshold,
779	    vifcp->vifc_rate_limit);
780
781    return 0;
782}
783
784/*
785 * Delete a vif from the vif table
786 */
787static int
788del_vif(vifi)
789	vifi_t vifi;
790{
791    register struct vif *vifp = &viftable[vifi];
792    register struct mbuf *m;
793    struct ifnet *ifp;
794    struct ifreq ifr;
795    int s;
796
797    if (vifi >= numvifs) return EINVAL;
798    if (vifp->v_lcl_addr.s_addr == 0) return EADDRNOTAVAIL;
799
800    s = splnet();
801
802    if (!(vifp->v_flags & VIFF_TUNNEL)) {
803	((struct sockaddr_in *)&(ifr.ifr_addr))->sin_family = AF_INET;
804	((struct sockaddr_in *)&(ifr.ifr_addr))->sin_addr.s_addr = INADDR_ANY;
805	ifp = vifp->v_ifp;
806	if_allmulti(ifp, 0);
807    }
808
809    if (vifp == last_encap_vif) {
810	last_encap_vif = 0;
811	last_encap_src = 0;
812    }
813
814    /*
815     * Free packets queued at the interface
816     */
817    while (vifp->v_tbf->tbf_q) {
818	m = vifp->v_tbf->tbf_q;
819	vifp->v_tbf->tbf_q = m->m_act;
820	m_freem(m);
821    }
822
823    bzero((caddr_t)vifp->v_tbf, sizeof(*(vifp->v_tbf)));
824    bzero((caddr_t)vifp, sizeof (*vifp));
825
826    if (mrtdebug)
827      log(LOG_DEBUG, "del_vif %d, numvifs %d\n", vifi, numvifs);
828
829    /* Adjust numvifs down */
830    for (vifi = numvifs; vifi > 0; vifi--)
831	if (viftable[vifi-1].v_lcl_addr.s_addr != 0) break;
832    numvifs = vifi;
833
834    splx(s);
835
836    return 0;
837}
838
839/*
840 * Add an mfc entry
841 */
842static int
843add_mfc(mfccp)
844    struct mfcctl *mfccp;
845{
846    struct mfc *rt;
847    register struct mbuf *mb_rt;
848    u_long hash;
849    struct mbuf *mb_ntry;
850    struct rtdetq *rte;
851    register u_short nstl;
852    int s;
853    int i;
854
855    MFCFIND(mfccp->mfcc_origin.s_addr, mfccp->mfcc_mcastgrp.s_addr, rt);
856
857    /* If an entry already exists, just update the fields */
858    if (rt) {
859	if (mrtdebug & DEBUG_MFC)
860	    log(LOG_DEBUG,"add_mfc update o %lx g %lx p %x\n",
861		(u_long)ntohl(mfccp->mfcc_origin.s_addr),
862		(u_long)ntohl(mfccp->mfcc_mcastgrp.s_addr),
863		mfccp->mfcc_parent);
864
865	s = splnet();
866	rt->mfc_parent = mfccp->mfcc_parent;
867	for (i = 0; i < numvifs; i++)
868	    rt->mfc_ttls[i] = mfccp->mfcc_ttls[i];
869	splx(s);
870	return 0;
871    }
872
873    /*
874     * Find the entry for which the upcall was made and update
875     */
876    s = splnet();
877    hash = MFCHASH(mfccp->mfcc_origin.s_addr, mfccp->mfcc_mcastgrp.s_addr);
878    for (mb_rt = mfctable[hash], nstl = 0; mb_rt; mb_rt = mb_rt->m_next) {
879
880	rt = mtod(mb_rt, struct mfc *);
881	if ((rt->mfc_origin.s_addr == mfccp->mfcc_origin.s_addr) &&
882	    (rt->mfc_mcastgrp.s_addr == mfccp->mfcc_mcastgrp.s_addr) &&
883	    (mb_rt->m_act != NULL)) {
884
885	    if (nstl++)
886		log(LOG_ERR, "add_mfc %s o %lx g %lx p %x dbx %p\n",
887		    "multiple kernel entries",
888		    (u_long)ntohl(mfccp->mfcc_origin.s_addr),
889		    (u_long)ntohl(mfccp->mfcc_mcastgrp.s_addr),
890		    mfccp->mfcc_parent, (void *)mb_rt->m_act);
891
892	    if (mrtdebug & DEBUG_MFC)
893		log(LOG_DEBUG,"add_mfc o %lx g %lx p %x dbg %p\n",
894		    (u_long)ntohl(mfccp->mfcc_origin.s_addr),
895		    (u_long)ntohl(mfccp->mfcc_mcastgrp.s_addr),
896		    mfccp->mfcc_parent, (void *)mb_rt->m_act);
897
898	    rt->mfc_origin     = mfccp->mfcc_origin;
899	    rt->mfc_mcastgrp   = mfccp->mfcc_mcastgrp;
900	    rt->mfc_parent     = mfccp->mfcc_parent;
901	    for (i = 0; i < numvifs; i++)
902		rt->mfc_ttls[i] = mfccp->mfcc_ttls[i];
903	    /* initialize pkt counters per src-grp */
904	    rt->mfc_pkt_cnt    = 0;
905	    rt->mfc_byte_cnt   = 0;
906	    rt->mfc_wrong_if   = 0;
907	    rt->mfc_last_assert.tv_sec = rt->mfc_last_assert.tv_usec = 0;
908
909	    rt->mfc_expire = 0;	/* Don't clean this guy up */
910	    nexpire[hash]--;
911
912	    /* free packets Qed at the end of this entry */
913	    while (mb_rt->m_act) {
914		mb_ntry = mb_rt->m_act;
915		rte = mtod(mb_ntry, struct rtdetq *);
916/* #ifdef RSVP_ISI */
917		ip_mdq(rte->m, rte->ifp, rt, -1);
918/* #endif */
919		mb_rt->m_act = mb_ntry->m_act;
920		m_freem(rte->m);
921#ifdef UPCALL_TIMING
922		collate(&(rte->t));
923#endif /* UPCALL_TIMING */
924		m_free(mb_ntry);
925	    }
926	}
927    }
928
929    /*
930     * It is possible that an entry is being inserted without an upcall
931     */
932    if (nstl == 0) {
933	if (mrtdebug & DEBUG_MFC)
934	    log(LOG_DEBUG,"add_mfc no upcall h %lu o %lx g %lx p %x\n",
935		hash, (u_long)ntohl(mfccp->mfcc_origin.s_addr),
936		(u_long)ntohl(mfccp->mfcc_mcastgrp.s_addr),
937		mfccp->mfcc_parent);
938
939	for (mb_rt = mfctable[hash]; mb_rt; mb_rt = mb_rt->m_next) {
940
941	    rt = mtod(mb_rt, struct mfc *);
942	    if ((rt->mfc_origin.s_addr == mfccp->mfcc_origin.s_addr) &&
943		(rt->mfc_mcastgrp.s_addr == mfccp->mfcc_mcastgrp.s_addr)) {
944
945		rt->mfc_origin     = mfccp->mfcc_origin;
946		rt->mfc_mcastgrp   = mfccp->mfcc_mcastgrp;
947		rt->mfc_parent     = mfccp->mfcc_parent;
948		for (i = 0; i < numvifs; i++)
949		    rt->mfc_ttls[i] = mfccp->mfcc_ttls[i];
950		/* initialize pkt counters per src-grp */
951		rt->mfc_pkt_cnt    = 0;
952		rt->mfc_byte_cnt   = 0;
953		rt->mfc_wrong_if   = 0;
954		rt->mfc_last_assert.tv_sec = rt->mfc_last_assert.tv_usec = 0;
955		if (rt->mfc_expire)
956		    nexpire[hash]--;
957		rt->mfc_expire	   = 0;
958	    }
959	}
960	if (mb_rt == NULL) {
961	    /* no upcall, so make a new entry */
962	    MGET(mb_rt, M_DONTWAIT, MT_MRTABLE);
963	    if (mb_rt == NULL) {
964		splx(s);
965		return ENOBUFS;
966	    }
967
968	    rt = mtod(mb_rt, struct mfc *);
969
970	    /* insert new entry at head of hash chain */
971	    rt->mfc_origin     = mfccp->mfcc_origin;
972	    rt->mfc_mcastgrp   = mfccp->mfcc_mcastgrp;
973	    rt->mfc_parent     = mfccp->mfcc_parent;
974	    for (i = 0; i < numvifs; i++)
975		    rt->mfc_ttls[i] = mfccp->mfcc_ttls[i];
976	    /* initialize pkt counters per src-grp */
977	    rt->mfc_pkt_cnt    = 0;
978	    rt->mfc_byte_cnt   = 0;
979	    rt->mfc_wrong_if   = 0;
980	    rt->mfc_last_assert.tv_sec = rt->mfc_last_assert.tv_usec = 0;
981	    rt->mfc_expire     = 0;
982
983	    /* link into table */
984	    mb_rt->m_next  = mfctable[hash];
985	    mfctable[hash] = mb_rt;
986	    mb_rt->m_act = NULL;
987	}
988    }
989    splx(s);
990    return 0;
991}
992
993#ifdef UPCALL_TIMING
994/*
995 * collect delay statistics on the upcalls
996 */
997static void collate(t)
998register struct timeval *t;
999{
1000    register u_long d;
1001    register struct timeval tp;
1002    register u_long delta;
1003
1004    GET_TIME(tp);
1005
1006    if (TV_LT(*t, tp))
1007    {
1008	TV_DELTA(tp, *t, delta);
1009
1010	d = delta >> 10;
1011	if (d > 50)
1012	    d = 50;
1013
1014	++upcall_data[d];
1015    }
1016}
1017#endif /* UPCALL_TIMING */
1018
1019/*
1020 * Delete an mfc entry
1021 */
1022static int
1023del_mfc(mfccp)
1024    struct mfcctl *mfccp;
1025{
1026    struct in_addr 	origin;
1027    struct in_addr 	mcastgrp;
1028    struct mfc 		*rt;
1029    struct mbuf 	*mb_rt;
1030    struct mbuf 	**nptr;
1031    u_long 		hash;
1032    int s;
1033
1034    origin = mfccp->mfcc_origin;
1035    mcastgrp = mfccp->mfcc_mcastgrp;
1036    hash = MFCHASH(origin.s_addr, mcastgrp.s_addr);
1037
1038    if (mrtdebug & DEBUG_MFC)
1039	log(LOG_DEBUG,"del_mfc orig %lx mcastgrp %lx\n",
1040	    (u_long)ntohl(origin.s_addr), (u_long)ntohl(mcastgrp.s_addr));
1041
1042    s = splnet();
1043
1044    nptr = &mfctable[hash];
1045    while ((mb_rt = *nptr) != NULL) {
1046        rt = mtod(mb_rt, struct mfc *);
1047	if (origin.s_addr == rt->mfc_origin.s_addr &&
1048	    mcastgrp.s_addr == rt->mfc_mcastgrp.s_addr &&
1049	    mb_rt->m_act == NULL)
1050	    break;
1051
1052	nptr = &mb_rt->m_next;
1053    }
1054    if (mb_rt == NULL) {
1055	splx(s);
1056	return EADDRNOTAVAIL;
1057    }
1058
1059    MFREE(mb_rt, *nptr);
1060
1061    splx(s);
1062
1063    return 0;
1064}
1065
1066/*
1067 * Send a message to mrouted on the multicast routing socket
1068 */
1069static int
1070socket_send(s, mm, src)
1071	struct socket *s;
1072	struct mbuf *mm;
1073	struct sockaddr_in *src;
1074{
1075	if (s) {
1076		if (sbappendaddr(&s->so_rcv,
1077				 (struct sockaddr *)src,
1078				 mm, (struct mbuf *)0) != 0) {
1079			sorwakeup(s);
1080			return 0;
1081		}
1082	}
1083	m_freem(mm);
1084	return -1;
1085}
1086
1087/*
1088 * IP multicast forwarding function. This function assumes that the packet
1089 * pointed to by "ip" has arrived on (or is about to be sent to) the interface
1090 * pointed to by "ifp", and the packet is to be relayed to other networks
1091 * that have members of the packet's destination IP multicast group.
1092 *
1093 * The packet is returned unscathed to the caller, unless it is
1094 * erroneous, in which case a non-zero return value tells the caller to
1095 * discard it.
1096 */
1097
1098#define IP_HDR_LEN  20	/* # bytes of fixed IP header (excluding options) */
1099#define TUNNEL_LEN  12  /* # bytes of IP option for tunnel encapsulation  */
1100
1101static int
1102X_ip_mforward(ip, ifp, m, imo)
1103    register struct ip *ip;
1104    struct ifnet *ifp;
1105    struct mbuf *m;
1106    struct ip_moptions *imo;
1107{
1108    register struct mfc *rt;
1109    register u_char *ipoptions;
1110    static struct sockaddr_in 	k_igmpsrc	= { sizeof k_igmpsrc, AF_INET };
1111    static int srctun = 0;
1112    register struct mbuf *mm;
1113    int s;
1114    vifi_t vifi;
1115    struct vif *vifp;
1116
1117    if (mrtdebug & DEBUG_FORWARD)
1118	log(LOG_DEBUG, "ip_mforward: src %lx, dst %lx, ifp %p\n",
1119	    (u_long)ntohl(ip->ip_src.s_addr), (u_long)ntohl(ip->ip_dst.s_addr),
1120	    (void *)ifp);
1121
1122    if (ip->ip_hl < (IP_HDR_LEN + TUNNEL_LEN) >> 2 ||
1123	(ipoptions = (u_char *)(ip + 1))[1] != IPOPT_LSRR ) {
1124	/*
1125	 * Packet arrived via a physical interface or
1126	 * an encapsulated tunnel.
1127	 */
1128    } else {
1129	/*
1130	 * Packet arrived through a source-route tunnel.
1131	 * Source-route tunnels are no longer supported.
1132	 */
1133	if ((srctun++ % 1000) == 0)
1134	    log(LOG_ERR,
1135		"ip_mforward: received source-routed packet from %lx\n",
1136		(u_long)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 %lx g %lx\n",
1202		(u_long)ntohl(ip->ip_src.s_addr),
1203		(u_long)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(mb0, 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 (%lx %lx)\n",
1362			(u_long)ntohl(mfc->mfc_origin.s_addr),
1363			(u_long)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    expire_upcalls_ch = 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 = 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 %p vifi %d vififp %p\n",
1434		(void *)ifp, vifi, (void *)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 its 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    MGETHDR(mb_copy, M_DONTWAIT, MT_HEADER);
1584    if (mb_copy == NULL)
1585	return;
1586    mb_copy->m_data += max_linkhdr;
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    mb_copy->m_data += sizeof(multicast_encap_iphdr);
1620    ip->ip_sum = in_cksum(mb_copy, ip->ip_hl << 2);
1621    mb_copy->m_data -= sizeof(multicast_encap_iphdr);
1622
1623    if (vifp->v_rate_limit <= 0)
1624	tbf_send_packet(vifp, mb_copy);
1625    else
1626	tbf_control(vifp, mb_copy, ip, ip_copy->ip_len);
1627}
1628
1629/*
1630 * De-encapsulate a packet and feed it back through ip input (this
1631 * routine is called whenever IP gets a packet with proto type
1632 * ENCAP_PROTO and a local destination address).
1633 */
1634void
1635#ifdef MROUTE_LKM
1636X_ipip_input(m, iphlen)
1637#else
1638ipip_input(m, iphlen)
1639#endif
1640	register struct mbuf *m;
1641	int iphlen;
1642{
1643    struct ifnet *ifp = m->m_pkthdr.rcvif;
1644    register struct ip *ip = mtod(m, struct ip *);
1645    register int hlen = ip->ip_hl << 2;
1646    register int s;
1647    register struct ifqueue *ifq;
1648    register struct vif *vifp;
1649
1650    if (!have_encap_tunnel) {
1651	    rip_input(m, iphlen);
1652	    return;
1653    }
1654    /*
1655     * dump the packet if it's not to a multicast destination or if
1656     * we don't have an encapsulating tunnel with the source.
1657     * Note:  This code assumes that the remote site IP address
1658     * uniquely identifies the tunnel (i.e., that this site has
1659     * at most one tunnel with the remote site).
1660     */
1661    if (! IN_MULTICAST(ntohl(((struct ip *)((char *)ip + hlen))->ip_dst.s_addr))) {
1662	++mrtstat.mrts_bad_tunnel;
1663	m_freem(m);
1664	return;
1665    }
1666    if (ip->ip_src.s_addr != last_encap_src) {
1667	register struct vif *vife;
1668
1669	vifp = viftable;
1670	vife = vifp + numvifs;
1671	last_encap_src = ip->ip_src.s_addr;
1672	last_encap_vif = 0;
1673	for ( ; vifp < vife; ++vifp)
1674	    if (vifp->v_rmt_addr.s_addr == ip->ip_src.s_addr) {
1675		if ((vifp->v_flags & (VIFF_TUNNEL|VIFF_SRCRT))
1676		    == VIFF_TUNNEL)
1677		    last_encap_vif = vifp;
1678		break;
1679	    }
1680    }
1681    if ((vifp = last_encap_vif) == 0) {
1682	last_encap_src = 0;
1683	mrtstat.mrts_cant_tunnel++; /*XXX*/
1684	m_freem(m);
1685	if (mrtdebug)
1686	  log(LOG_DEBUG, "ip_mforward: no tunnel with %lx\n",
1687		(u_long)ntohl(ip->ip_src.s_addr));
1688	return;
1689    }
1690    ifp = vifp->v_ifp;
1691
1692    if (hlen > IP_HDR_LEN)
1693      ip_stripoptions(m, (struct mbuf *) 0);
1694    m->m_data += IP_HDR_LEN;
1695    m->m_len -= IP_HDR_LEN;
1696    m->m_pkthdr.len -= IP_HDR_LEN;
1697    m->m_pkthdr.rcvif = ifp;
1698
1699    ifq = &ipintrq;
1700    s = splimp();
1701    if (IF_QFULL(ifq)) {
1702	IF_DROP(ifq);
1703	m_freem(m);
1704    } else {
1705	IF_ENQUEUE(ifq, m);
1706	/*
1707	 * normally we would need a "schednetisr(NETISR_IP)"
1708	 * here but we were called by ip_input and it is going
1709	 * to loop back & try to dequeue the packet we just
1710	 * queued as soon as we return so we avoid the
1711	 * unnecessary software interrrupt.
1712	 */
1713    }
1714    splx(s);
1715}
1716
1717/*
1718 * Token bucket filter module
1719 */
1720
1721static void
1722tbf_control(vifp, m, ip, p_len)
1723	register struct vif *vifp;
1724	register struct mbuf *m;
1725	register struct ip *ip;
1726	register u_long p_len;
1727{
1728    register struct tbf *t = vifp->v_tbf;
1729
1730    if (p_len > MAX_BKT_SIZE) {
1731	/* drop if packet is too large */
1732	mrtstat.mrts_pkt2large++;
1733	m_freem(m);
1734	return;
1735    }
1736
1737    tbf_update_tokens(vifp);
1738
1739    /* if there are enough tokens,
1740     * and the queue is empty,
1741     * send this packet out
1742     */
1743
1744    if (t->tbf_q_len == 0) {
1745	/* queue empty, send packet if enough tokens */
1746	if (p_len <= t->tbf_n_tok) {
1747	    t->tbf_n_tok -= p_len;
1748	    tbf_send_packet(vifp, m);
1749	} else {
1750	    /* queue packet and timeout till later */
1751	    tbf_queue(vifp, m);
1752	    timeout(tbf_reprocess_q, (caddr_t)vifp, TBF_REPROCESS);
1753	}
1754    } else if (t->tbf_q_len < t->tbf_max_q_len) {
1755	/* finite queue length, so queue pkts and process queue */
1756	tbf_queue(vifp, m);
1757	tbf_process_q(vifp);
1758    } else {
1759	/* queue length too much, try to dq and queue and process */
1760	if (!tbf_dq_sel(vifp, ip)) {
1761	    mrtstat.mrts_q_overflow++;
1762	    m_freem(m);
1763	    return;
1764	} else {
1765	    tbf_queue(vifp, m);
1766	    tbf_process_q(vifp);
1767	}
1768    }
1769    return;
1770}
1771
1772/*
1773 * adds a packet to the queue at the interface
1774 */
1775static void
1776tbf_queue(vifp, m)
1777	register struct vif *vifp;
1778	register struct mbuf *m;
1779{
1780    register int s = splnet();
1781    register struct tbf *t = vifp->v_tbf;
1782
1783    if (t->tbf_t == NULL) {
1784	/* Queue was empty */
1785	t->tbf_q = m;
1786    } else {
1787	/* Insert at tail */
1788	t->tbf_t->m_act = m;
1789    }
1790
1791    /* Set new tail pointer */
1792    t->tbf_t = m;
1793
1794#ifdef DIAGNOSTIC
1795    /* Make sure we didn't get fed a bogus mbuf */
1796    if (m->m_act)
1797	panic("tbf_queue: m_act");
1798#endif
1799    m->m_act = NULL;
1800
1801    t->tbf_q_len++;
1802
1803    splx(s);
1804}
1805
1806
1807/*
1808 * processes the queue at the interface
1809 */
1810static void
1811tbf_process_q(vifp)
1812    register struct vif *vifp;
1813{
1814    register struct mbuf *m;
1815    register int len;
1816    register int s = splnet();
1817    register struct tbf *t = vifp->v_tbf;
1818
1819    /* loop through the queue at the interface and send as many packets
1820     * as possible
1821     */
1822    while (t->tbf_q_len > 0) {
1823	m = t->tbf_q;
1824
1825	len = mtod(m, struct ip *)->ip_len;
1826
1827	/* determine if the packet can be sent */
1828	if (len <= t->tbf_n_tok) {
1829	    /* if so,
1830	     * reduce no of tokens, dequeue the packet,
1831	     * send the packet.
1832	     */
1833	    t->tbf_n_tok -= len;
1834
1835	    t->tbf_q = m->m_act;
1836	    if (--t->tbf_q_len == 0)
1837		t->tbf_t = NULL;
1838
1839	    m->m_act = NULL;
1840	    tbf_send_packet(vifp, m);
1841
1842	} else break;
1843    }
1844    splx(s);
1845}
1846
1847static void
1848tbf_reprocess_q(xvifp)
1849	void *xvifp;
1850{
1851    register struct vif *vifp = xvifp;
1852    if (ip_mrouter == NULL)
1853	return;
1854
1855    tbf_update_tokens(vifp);
1856
1857    tbf_process_q(vifp);
1858
1859    if (vifp->v_tbf->tbf_q_len)
1860	timeout(tbf_reprocess_q, (caddr_t)vifp, TBF_REPROCESS);
1861}
1862
1863/* function that will selectively discard a member of the queue
1864 * based on the precedence value and the priority
1865 */
1866static int
1867tbf_dq_sel(vifp, ip)
1868    register struct vif *vifp;
1869    register struct ip *ip;
1870{
1871    register int s = splnet();
1872    register u_int p;
1873    register struct mbuf *m, *last;
1874    register struct mbuf **np;
1875    register struct tbf *t = vifp->v_tbf;
1876
1877    p = priority(vifp, ip);
1878
1879    np = &t->tbf_q;
1880    last = NULL;
1881    while ((m = *np) != NULL) {
1882	if (p > priority(vifp, mtod(m, struct ip *))) {
1883	    *np = m->m_act;
1884	    /* If we're removing the last packet, fix the tail pointer */
1885	    if (m == t->tbf_t)
1886		t->tbf_t = last;
1887	    m_freem(m);
1888	    /* it's impossible for the queue to be empty, but
1889	     * we check anyway. */
1890	    if (--t->tbf_q_len == 0)
1891		t->tbf_t = NULL;
1892	    splx(s);
1893	    mrtstat.mrts_drop_sel++;
1894	    return(1);
1895	}
1896	np = &m->m_act;
1897	last = m;
1898    }
1899    splx(s);
1900    return(0);
1901}
1902
1903static void
1904tbf_send_packet(vifp, m)
1905    register struct vif *vifp;
1906    register struct mbuf *m;
1907{
1908    struct ip_moptions imo;
1909    int error;
1910    static struct route ro;
1911    int s = splnet();
1912
1913    if (vifp->v_flags & VIFF_TUNNEL) {
1914	/* If tunnel options */
1915	ip_output(m, (struct mbuf *)0, &vifp->v_route,
1916		  IP_FORWARDING, (struct ip_moptions *)0);
1917    } else {
1918	imo.imo_multicast_ifp  = vifp->v_ifp;
1919	imo.imo_multicast_ttl  = mtod(m, struct ip *)->ip_ttl - 1;
1920	imo.imo_multicast_loop = 1;
1921	imo.imo_multicast_vif  = -1;
1922
1923	/*
1924	 * Re-entrancy should not be a problem here, because
1925	 * the packets that we send out and are looped back at us
1926	 * should get rejected because they appear to come from
1927	 * the loopback interface, thus preventing looping.
1928	 */
1929	error = ip_output(m, (struct mbuf *)0, &ro,
1930			  IP_FORWARDING, &imo);
1931
1932	if (mrtdebug & DEBUG_XMIT)
1933	    log(LOG_DEBUG, "phyint_send on vif %d err %d\n",
1934		vifp - viftable, error);
1935    }
1936    splx(s);
1937}
1938
1939/* determine the current time and then
1940 * the elapsed time (between the last time and time now)
1941 * in milliseconds & update the no. of tokens in the bucket
1942 */
1943static void
1944tbf_update_tokens(vifp)
1945    register struct vif *vifp;
1946{
1947    struct timeval tp;
1948    register u_long tm;
1949    register int s = splnet();
1950    register struct tbf *t = vifp->v_tbf;
1951
1952    GET_TIME(tp);
1953
1954    TV_DELTA(tp, t->tbf_last_pkt_t, tm);
1955
1956    /*
1957     * This formula is actually
1958     * "time in seconds" * "bytes/second".
1959     *
1960     * (tm / 1000000) * (v_rate_limit * 1000 * (1000/1024) / 8)
1961     *
1962     * The (1000/1024) was introduced in add_vif to optimize
1963     * this divide into a shift.
1964     */
1965    t->tbf_n_tok += tm * vifp->v_rate_limit / 1024 / 8;
1966    t->tbf_last_pkt_t = tp;
1967
1968    if (t->tbf_n_tok > MAX_BKT_SIZE)
1969	t->tbf_n_tok = MAX_BKT_SIZE;
1970
1971    splx(s);
1972}
1973
1974static int
1975priority(vifp, ip)
1976    register struct vif *vifp;
1977    register struct ip *ip;
1978{
1979    register int prio;
1980
1981    /* temporary hack; may add general packet classifier some day */
1982
1983    /*
1984     * The UDP port space is divided up into four priority ranges:
1985     * [0, 16384)     : unclassified - lowest priority
1986     * [16384, 32768) : audio - highest priority
1987     * [32768, 49152) : whiteboard - medium priority
1988     * [49152, 65536) : video - low priority
1989     */
1990    if (ip->ip_p == IPPROTO_UDP) {
1991	struct udphdr *udp = (struct udphdr *)(((char *)ip) + (ip->ip_hl << 2));
1992	switch (ntohs(udp->uh_dport) & 0xc000) {
1993	    case 0x4000:
1994		prio = 70;
1995		break;
1996	    case 0x8000:
1997		prio = 60;
1998		break;
1999	    case 0xc000:
2000		prio = 55;
2001		break;
2002	    default:
2003		prio = 50;
2004		break;
2005	}
2006	if (tbfdebug > 1)
2007		log(LOG_DEBUG, "port %x prio%d\n", ntohs(udp->uh_dport), prio);
2008    } else {
2009	    prio = 50;
2010    }
2011    return prio;
2012}
2013
2014/*
2015 * End of token bucket filter modifications
2016 */
2017
2018int
2019ip_rsvp_vif_init(so, sopt)
2020	struct socket *so;
2021	struct sockopt *sopt;
2022{
2023    int error, i, s;
2024
2025    if (rsvpdebug)
2026	printf("ip_rsvp_vif_init: so_type = %d, pr_protocol = %d\n",
2027	       so->so_type, so->so_proto->pr_protocol);
2028
2029    if (so->so_type != SOCK_RAW || so->so_proto->pr_protocol != IPPROTO_RSVP)
2030	return EOPNOTSUPP;
2031
2032    /* Check mbuf. */
2033    error = sooptcopyin(sopt, &i, sizeof i, sizeof i);
2034    if (error)
2035	    return (error);
2036
2037    if (rsvpdebug)
2038	printf("ip_rsvp_vif_init: vif = %d rsvp_on = %d\n", i, rsvp_on);
2039
2040    s = splnet();
2041
2042    /* Check vif. */
2043    if (!legal_vif_num(i)) {
2044	splx(s);
2045	return EADDRNOTAVAIL;
2046    }
2047
2048    /* Check if socket is available. */
2049    if (viftable[i].v_rsvpd != NULL) {
2050	splx(s);
2051	return EADDRINUSE;
2052    }
2053
2054    viftable[i].v_rsvpd = so;
2055    /* This may seem silly, but we need to be sure we don't over-increment
2056     * the RSVP counter, in case something slips up.
2057     */
2058    if (!viftable[i].v_rsvp_on) {
2059	viftable[i].v_rsvp_on = 1;
2060	rsvp_on++;
2061    }
2062
2063    splx(s);
2064    return 0;
2065}
2066
2067int
2068ip_rsvp_vif_done(so, sopt)
2069	struct socket *so;
2070	struct sockopt *sopt;
2071{
2072	int error, i, 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 ||
2079	    so->so_proto->pr_protocol != IPPROTO_RSVP)
2080		return EOPNOTSUPP;
2081
2082	error = sooptcopyin(sopt, &i, sizeof i, sizeof i);
2083	if (error)
2084		return (error);
2085
2086	s = splnet();
2087
2088	/* Check vif. */
2089	if (!legal_vif_num(i)) {
2090		splx(s);
2091		return EADDRNOTAVAIL;
2092	}
2093
2094	if (rsvpdebug)
2095		printf("ip_rsvp_vif_done: v_rsvpd = %p so = %p\n",
2096		       viftable[i].v_rsvpd, so);
2097
2098	viftable[i].v_rsvpd = NULL;
2099	/*
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, iphlen);
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