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