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