source: rtems/cpukit/libnetworking/netinet/ip_mroute.c @ 0e16fa45

5
Last change on this file since 0e16fa45 was cb68253, checked in by Sebastian Huber <sebastian.huber@…>, on 09/07/18 at 04:19:02

network: Use kernel/user space header files

Add and use <machine/rtems-bsd-kernel-space.h> and
<machine/rtems-bsd-user-space.h> similar to the libbsd to avoid command
line defines and defines scattered throught the code base.

Simplify cpukit/libnetworking/Makefile.am.

Update #3375.

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