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