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