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