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