OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [rtos/] [ecos-3.0/] [packages/] [net/] [bsd_tcpip/] [current/] [src/] [sys/] [netinet/] [ip_mroute.c] - Blame information for rev 838

Go to most recent revision | Details | Compare with Previous | View Log

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

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.