source: rtems/cpukit/libnetworking/netinet/ip_mroute.c @ 46a1aa9e

4.104.114.84.95
Last change on this file since 46a1aa9e was 78219ad1, checked in by Joel Sherrill <joel.sherrill@…>, on 12/02/04 at 20:24:30

2004-12-02 Joel Sherrill <joel@…>

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