source: rtems/cpukit/libnetworking/netinet/ip_mroute.c @ 787f51f

5
Last change on this file since 787f51f was 787f51f, checked in by Sebastian Huber <sebastian.huber@…>, on 06/06/17 at 09:08:16

Do not include <sys/ioctl.h> in kernel-space

Update #2833.

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