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

Subversion Repositories openrisc_me

[/] [openrisc/] [trunk/] [rtos/] [ecos-2.0/] [packages/] [net/] [tcpip/] [v2_0/] [src/] [sys/] [net/] [rtsock.c] - Blame information for rev 174

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 27 unneback
//==========================================================================
2
//
3
//      sys/net/rtsock.c
4
//
5
//     
6
//
7
//==========================================================================
8
//####BSDCOPYRIGHTBEGIN####
9
//
10
// -------------------------------------------
11
//
12
// Portions of this software may have been derived from OpenBSD or other sources,
13
// and are covered by the appropriate copyright disclaimers included herein.
14
//
15
// -------------------------------------------
16
//
17
//####BSDCOPYRIGHTEND####
18
//==========================================================================
19
//#####DESCRIPTIONBEGIN####
20
//
21
// Author(s):    gthomas
22
// Contributors: gthomas
23
// Date:         2000-01-10
24
// Purpose:      
25
// Description:  
26
//              
27
//
28
//####DESCRIPTIONEND####
29
//
30
//==========================================================================
31
 
32
 
33
/*      $OpenBSD: rtsock.c,v 1.8 1999/12/08 06:50:18 itojun Exp $       */
34
/*      $NetBSD: rtsock.c,v 1.18 1996/03/29 00:32:10 cgd Exp $  */
35
 
36
/*
37
 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
38
 * All rights reserved.
39
 *
40
 * Redistribution and use in source and binary forms, with or without
41
 * modification, are permitted provided that the following conditions
42
 * are met:
43
 * 1. Redistributions of source code must retain the above copyright
44
 *    notice, this list of conditions and the following disclaimer.
45
 * 2. Redistributions in binary form must reproduce the above copyright
46
 *    notice, this list of conditions and the following disclaimer in the
47
 *    documentation and/or other materials provided with the distribution.
48
 * 3. Neither the name of the project nor the names of its contributors
49
 *    may be used to endorse or promote products derived from this software
50
 *    without specific prior written permission.
51
 *
52
 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
53
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
54
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
55
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
56
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
57
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
58
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
59
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
60
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
61
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
62
 * SUCH DAMAGE.
63
 */
64
 
65
/*
66
 * Copyright (c) 1988, 1991, 1993
67
 *      The Regents of the University of California.  All rights reserved.
68
 *
69
 * Redistribution and use in source and binary forms, with or without
70
 * modification, are permitted provided that the following conditions
71
 * are met:
72
 * 1. Redistributions of source code must retain the above copyright
73
 *    notice, this list of conditions and the following disclaimer.
74
 * 2. Redistributions in binary form must reproduce the above copyright
75
 *    notice, this list of conditions and the following disclaimer in the
76
 *    documentation and/or other materials provided with the distribution.
77
 * 3. All advertising materials mentioning features or use of this software
78
 *    must display the following acknowledgement:
79
 *      This product includes software developed by the University of
80
 *      California, Berkeley and its contributors.
81
 * 4. Neither the name of the University nor the names of its contributors
82
 *    may be used to endorse or promote products derived from this software
83
 *    without specific prior written permission.
84
 *
85
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
86
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
87
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
88
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
89
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
90
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
91
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
92
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
93
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
94
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
95
 * SUCH DAMAGE.
96
 *
97
 *      @(#)rtsock.c    8.6 (Berkeley) 2/11/95
98
 */
99
 
100
#include <sys/param.h>
101
#ifndef __ECOS
102
#include <sys/systm.h>
103
#include <sys/proc.h>
104
#endif
105
#include <sys/mbuf.h>
106
#include <sys/socket.h>
107
#include <sys/socketvar.h>
108
#include <sys/domain.h>
109
#include <sys/protosw.h>
110
 
111
#ifndef __ECOS
112
#include <vm/vm.h>
113
#include <sys/sysctl.h>
114
#endif
115
 
116
#include <net/if.h>
117
#include <net/route.h>
118
#include <net/raw_cb.h>
119
 
120
#include <machine/stdarg.h>
121
 
122
struct  sockaddr route_dst = { 2, PF_ROUTE, };
123
struct  sockaddr route_src = { 2, PF_ROUTE, };
124
struct  sockproto route_proto = { PF_ROUTE, };
125
 
126
struct walkarg {
127
        int     w_op, w_arg, w_given, w_needed, w_tmemsize;
128
        caddr_t w_where, w_tmem;
129
};
130
 
131
static struct mbuf *
132
                rt_msg1 __P((int, struct rt_addrinfo *));
133
static int      rt_msg2 __P((int,
134
                    struct rt_addrinfo *, caddr_t, struct walkarg *));
135
static void     rt_xaddrs __P((caddr_t, caddr_t, struct rt_addrinfo *));
136
static void rt_setif __P((struct rtentry *, struct sockaddr *,
137
        struct sockaddr *, struct sockaddr *));
138
 
139
/* Sleazy use of local variables throughout file, warning!!!! */
140
#define dst     info.rti_info[RTAX_DST]
141
#define gate    info.rti_info[RTAX_GATEWAY]
142
#define netmask info.rti_info[RTAX_NETMASK]
143
#define genmask info.rti_info[RTAX_GENMASK]
144
#define ifpaddr info.rti_info[RTAX_IFP]
145
#define ifaaddr info.rti_info[RTAX_IFA]
146
#define brdaddr info.rti_info[RTAX_BRD]
147
 
148
/*ARGSUSED*/
149
int
150
route_usrreq(so, req, m, nam, control)
151
        register struct socket *so;
152
        int req;
153
        struct mbuf *m, *nam, *control;
154
{
155
        register int error = 0;
156
        register struct rawcb *rp = sotorawcb(so);
157
        int s;
158
 
159
        if (req == PRU_ATTACH) {
160
                MALLOC(rp, struct rawcb *, sizeof(*rp), M_PCB, M_WAITOK);
161
                if ((so->so_pcb = rp) != NULL)
162
                        bzero(so->so_pcb, sizeof(*rp));
163
 
164
        }
165
        if (req == PRU_DETACH && rp) {
166
                int af = rp->rcb_proto.sp_protocol;
167
                if (af == AF_INET)
168
                        route_cb.ip_count--;
169
                else if (af == AF_INET6)
170
                        route_cb.ip6_count--;
171
                else if (af == AF_NS)
172
                        route_cb.ns_count--;
173
                else if (af == AF_ISO)
174
                        route_cb.iso_count--;
175
                route_cb.any_count--;
176
        }
177
        s = splsoftnet();
178
        /*
179
         * Don't call raw_usrreq() in the attach case, because
180
         * we want to allow non-privileged processes to listen on
181
         * and send "safe" commands to the routing socket.
182
         */
183
        if (req == PRU_ATTACH) {
184
#ifndef __ECOS
185
                if (curproc == 0)
186
                        error = EACCES;
187
                else
188
#endif // FIXME?
189
                        error = raw_attach(so, (int)(long)nam);
190
        } else
191
                error = raw_usrreq(so, req, m, nam, control);
192
 
193
        rp = sotorawcb(so);
194
        if (req == PRU_ATTACH && rp) {
195
                int af = rp->rcb_proto.sp_protocol;
196
                if (error) {
197
                        free((caddr_t)rp, M_PCB);
198
                        splx(s);
199
                        return (error);
200
                }
201
                if (af == AF_INET)
202
                        route_cb.ip_count++;
203
                else if (af == AF_INET6)
204
                        route_cb.ip6_count++;
205
                else if (af == AF_NS)
206
                        route_cb.ns_count++;
207
                else if (af == AF_ISO)
208
                        route_cb.iso_count++;
209
                rp->rcb_faddr = &route_src;
210
                route_cb.any_count++;
211
                soisconnected(so);
212
                so->so_options |= SO_USELOOPBACK;
213
        }
214
        splx(s);
215
        return (error);
216
}
217
 
218
/*ARGSUSED*/
219
int
220
#if __STDC__
221
route_output(struct mbuf *m, ...)
222
#else
223
route_output(m, va_alist)
224
        struct mbuf *m;
225
        va_dcl
226
#endif
227
{
228
        register struct rt_msghdr *rtm = 0;
229
        register struct rtentry *rt = 0;
230
        struct rtentry *saved_nrt = 0;
231
        struct radix_node_head *rnh;
232
        struct rt_addrinfo info;
233
        int len, error = 0;
234
        struct ifnet *ifp = 0;
235
        struct socket *so;
236
        va_list ap;
237
 
238
        va_start(ap, m);
239
        so = va_arg(ap, struct socket *);
240
        va_end(ap);
241
 
242
        bzero(&info, sizeof(info));
243
#define senderr(e) { error = e; goto flush;}
244
        if (m == 0 || ((m->m_len < sizeof(int32_t)) &&
245
                       (m = m_pullup(m, sizeof(int32_t))) == 0))
246
                return (ENOBUFS);
247
        if ((m->m_flags & M_PKTHDR) == 0)
248
                panic("route_output");
249
        len = m->m_pkthdr.len;
250
        if (len < sizeof(*rtm) ||
251
            len != mtod(m, struct rt_msghdr *)->rtm_msglen) {
252
                dst = 0;
253
                senderr(EINVAL);
254
        }
255
        R_Malloc(rtm, struct rt_msghdr *, len);
256
        if (rtm == 0) {
257
                dst = 0;
258
                senderr(ENOBUFS);
259
        }
260
        m_copydata(m, 0, len, (caddr_t)rtm);
261
        if (rtm->rtm_version != RTM_VERSION) {
262
                dst = 0;
263
                senderr(EPROTONOSUPPORT);
264
        }
265
#ifdef __ECOS
266
        rtm->rtm_pid = 0; // FIXME
267
#else
268
        rtm->rtm_pid = curproc->p_pid;
269
#endif
270
        info.rti_addrs = rtm->rtm_addrs;
271
        rt_xaddrs((caddr_t)(rtm + 1), len + (caddr_t)rtm, &info);
272
        if (dst == 0)
273
                senderr(EINVAL);
274
        if (genmask) {
275
                struct radix_node *t;
276
                t = rn_addmask((caddr_t)genmask, 0, 1);
277
                if (t && Bcmp(genmask, t->rn_key, *(u_char *)genmask) == 0)
278
                        genmask = (struct sockaddr *)(t->rn_key);
279
                else
280
                        senderr(ENOBUFS);
281
        }
282
 
283
        /*
284
         * Verify that the caller has the appropriate privilege; RTM_GET
285
         * is the only operation the non-superuser is allowed.
286
         */
287
#ifndef __ECOS
288
        if (rtm->rtm_type != RTM_GET &&
289
            suser(curproc->p_ucred, &curproc->p_acflag) != 0)
290
                senderr(EACCES);
291
#endif
292
        switch (rtm->rtm_type) {
293
 
294
        case RTM_ADD:
295
                if (gate == 0)
296
                        senderr(EINVAL);
297
                error = rtrequest(RTM_ADD, dst, gate, netmask,
298
                                        rtm->rtm_flags, &saved_nrt);
299
                if (error == 0 && saved_nrt) {
300
                        /*
301
                         * If the route request specified an interface with
302
                         * IFA and/or IFP, we set the requested interface on
303
                         * the route with rt_setif.  It would be much better
304
                         * to do this inside rtrequest, but that would
305
                         * require passing the desired interface, in some
306
                         * form, to rtrequest.  Since rtrequest is called in
307
                         * so many places (roughly 40 in our source), adding
308
                         * a parameter is to much for us to swallow; this is
309
                         * something for the FreeBSD developers to tackle.
310
                         * Instead, we let rtrequest compute whatever
311
                         * interface it wants, then come in behind it and
312
                         * stick in the interface that we really want.  This
313
                         * works reasonably well except when rtrequest can't
314
                         * figure out what interface to use (with
315
                         * ifa_withroute) and returns ENETUNREACH.  Ideally
316
                         * it shouldn't matter if rtrequest can't figure out
317
                         * the interface if we're going to explicitly set it
318
                         * ourselves anyway.  But practically we can't
319
                         * recover here because rtrequest will not do any of
320
                         * the work necessary to add the route if it can't
321
                         * find an interface.  As long as there is a default
322
                         * route that leads to some interface, rtrequest will
323
                         * find an interface, so this problem should be
324
                         * rarely encountered.
325
                         * dwiggins@bbn.com
326
                         */
327
 
328
                        rt_setif(saved_nrt, ifpaddr, ifaaddr, gate);
329
                        rt_setmetrics(rtm->rtm_inits,
330
                                &rtm->rtm_rmx, &saved_nrt->rt_rmx);
331
                        saved_nrt->rt_refcnt--;
332
                        saved_nrt->rt_genmask = genmask;
333
                }
334
                break;
335
 
336
        case RTM_DELETE:
337
                error = rtrequest(RTM_DELETE, dst, gate, netmask,
338
                                rtm->rtm_flags, &saved_nrt);
339
                if (error == 0) {
340
                        (rt = saved_nrt)->rt_refcnt++;
341
                        goto report;
342
                }
343
                break;
344
 
345
        case RTM_GET:
346
        case RTM_CHANGE:
347
        case RTM_LOCK:
348
                if ((rnh = rt_tables[dst->sa_family]) == 0) {
349
                        senderr(EAFNOSUPPORT);
350
                } else if ((rt = (struct rtentry *)
351
                                rnh->rnh_lookup(dst, netmask, rnh)) != NULL)
352
                        rt->rt_refcnt++;
353
                else
354
                        senderr(ESRCH);
355
                switch(rtm->rtm_type) {
356
 
357
                case RTM_GET:
358
                report:
359
                        dst = rt_key(rt);
360
                        gate = rt->rt_gateway;
361
                        netmask = rt_mask(rt);
362
                        genmask = rt->rt_genmask;
363
                        if (rtm->rtm_addrs & (RTA_IFP | RTA_IFA)) {
364
                                if ((ifp = rt->rt_ifp) != NULL) {
365
                                        ifpaddr = ifp->if_addrlist.tqh_first->ifa_addr;
366
                                        ifaaddr = rt->rt_ifa->ifa_addr;
367
                                        if (ifp->if_flags & IFF_POINTOPOINT)
368
                                                brdaddr = rt->rt_ifa->ifa_dstaddr;
369
                                        else
370
                                                brdaddr = 0;
371
                                        rtm->rtm_index = ifp->if_index;
372
                                } else {
373
                                        ifpaddr = 0;
374
                                        ifaaddr = 0;
375
                            }
376
                        }
377
                        len = rt_msg2(rtm->rtm_type, &info, (caddr_t)0,
378
                                (struct walkarg *)0);
379
                        if (len > rtm->rtm_msglen) {
380
                                struct rt_msghdr *new_rtm;
381
                                R_Malloc(new_rtm, struct rt_msghdr *, len);
382
                                if (new_rtm == 0)
383
                                        senderr(ENOBUFS);
384
                                Bcopy(rtm, new_rtm, rtm->rtm_msglen);
385
                                Free(rtm); rtm = new_rtm;
386
                        }
387
                        (void)rt_msg2(rtm->rtm_type, &info, (caddr_t)rtm,
388
                                (struct walkarg *)0);
389
                        rtm->rtm_flags = rt->rt_flags;
390
                        rtm->rtm_rmx = rt->rt_rmx;
391
                        rtm->rtm_addrs = info.rti_addrs;
392
                        break;
393
 
394
                case RTM_CHANGE:
395
                        if (gate && rt_setgate(rt, rt_key(rt), gate))
396
#ifdef __ECOS
397
                                senderr(EMFILE);
398
#else
399
                                senderr(EDQUOT);
400
#endif
401
 
402
#if 1
403
                        rt_setif(rt, ifpaddr, ifaaddr, gate);
404
#else
405
                        /* new gateway could require new ifaddr, ifp;
406
                           flags may also be different; ifp may be specified
407
                           by ll sockaddr when protocol address is ambiguous */
408
                        if (ifpaddr && (ifa = ifa_ifwithnet(ifpaddr)) &&
409
                            (ifp = ifa->ifa_ifp) && (ifaaddr || gate))
410
                                ifa = ifaof_ifpforaddr(ifaaddr ? ifaaddr : gate,
411
                                                        ifp);
412
                        else if ((ifaaddr && (ifa = ifa_ifwithaddr(ifaaddr))) ||
413
                                 (gate && (ifa = ifa_ifwithroute(rt->rt_flags,
414
                                                        rt_key(rt), gate))))
415
                                ifp = ifa->ifa_ifp;
416
                        if (ifa) {
417
                                register struct ifaddr *oifa = rt->rt_ifa;
418
                                if (oifa != ifa) {
419
                                    if (oifa && oifa->ifa_rtrequest)
420
                                        oifa->ifa_rtrequest(RTM_DELETE,
421
                                                                rt, gate);
422
                                    IFAFREE(rt->rt_ifa);
423
                                    rt->rt_ifa = ifa;
424
                                    ifa->ifa_refcnt++;
425
                                    rt->rt_ifp = ifp;
426
                                }
427
                        }
428
#endif
429
                        rt_setmetrics(rtm->rtm_inits, &rtm->rtm_rmx,
430
                                        &rt->rt_rmx);
431
#if 0
432
                        if (rt->rt_ifa && rt->rt_ifa->ifa_rtrequest)
433
                               rt->rt_ifa->ifa_rtrequest(RTM_ADD, rt, gate);
434
#endif
435
                        if (genmask)
436
                                rt->rt_genmask = genmask;
437
                        /*
438
                         * Fall into
439
                         */
440
                case RTM_LOCK:
441
                        rt->rt_rmx.rmx_locks &= ~(rtm->rtm_inits);
442
                        rt->rt_rmx.rmx_locks |=
443
                                (rtm->rtm_inits & rtm->rtm_rmx.rmx_locks);
444
                        break;
445
                }
446
                break;
447
 
448
        default:
449
                senderr(EOPNOTSUPP);
450
        }
451
 
452
flush:
453
        if (rtm) {
454
                if (error)
455
                        rtm->rtm_errno = error;
456
                else
457
                        rtm->rtm_flags |= RTF_DONE;
458
        }
459
        if (rt)
460
                rtfree(rt);
461
    {
462
        register struct rawcb *rp = 0;
463
        /*
464
         * Check to see if we don't want our own messages.
465
         */
466
        if ((so->so_options & SO_USELOOPBACK) == 0) {
467
                if (route_cb.any_count <= 1) {
468
                        if (rtm)
469
                                Free(rtm);
470
                        m_freem(m);
471
                        return (error);
472
                }
473
                /* There is another listener, so construct message */
474
                rp = sotorawcb(so);
475
        }
476
        if (rtm) {
477
                m_copyback(m, 0, rtm->rtm_msglen, (caddr_t)rtm);
478
                Free(rtm);
479
        }
480
        if (rp)
481
                rp->rcb_proto.sp_family = 0; /* Avoid us */
482
        if (dst)
483
                route_proto.sp_protocol = dst->sa_family;
484
        raw_input(m, &route_proto, &route_src, &route_dst);
485
        if (rp)
486
                rp->rcb_proto.sp_family = PF_ROUTE;
487
    }
488
        return (error);
489
}
490
 
491
void
492
rt_setmetrics(which, in, out)
493
        u_long which;
494
        register struct rt_metrics *in, *out;
495
{
496
#define metric(f, e) if (which & (f)) out->e = in->e;
497
        metric(RTV_RPIPE, rmx_recvpipe);
498
        metric(RTV_SPIPE, rmx_sendpipe);
499
        metric(RTV_SSTHRESH, rmx_ssthresh);
500
        metric(RTV_RTT, rmx_rtt);
501
        metric(RTV_RTTVAR, rmx_rttvar);
502
        metric(RTV_HOPCOUNT, rmx_hopcount);
503
        metric(RTV_MTU, rmx_mtu);
504
        metric(RTV_EXPIRE, rmx_expire);
505
#undef metric
506
}
507
 
508
/*
509
 * Set route's interface given ifpaddr, ifaaddr, and gateway.
510
 */
511
static void
512
rt_setif(rt, Ifpaddr, Ifaaddr, Gate)
513
        struct rtentry *rt;
514
        struct sockaddr *Ifpaddr, *Ifaaddr, *Gate;
515
{
516
        struct ifaddr *ifa = 0;
517
        struct ifnet  *ifp = 0;
518
 
519
        /* new gateway could require new ifaddr, ifp;
520
           flags may also be different; ifp may be specified
521
           by ll sockaddr when protocol address is ambiguous */
522
        if (Ifpaddr && (ifa = ifa_ifwithnet(Ifpaddr)) &&
523
            (ifp = ifa->ifa_ifp) && (Ifaaddr || Gate))
524
                ifa = ifaof_ifpforaddr(Ifaaddr ? Ifaaddr : Gate,
525
                                        ifp);
526
        else if (Ifpaddr && (ifp = if_withname(Ifpaddr)) ) {
527
                ifa = Gate ? ifaof_ifpforaddr(Gate, ifp) :
528
                                TAILQ_FIRST(&ifp->if_addrlist);
529
        }
530
        else if ((Ifaaddr && (ifa = ifa_ifwithaddr(Ifaaddr))) ||
531
                 (Gate && (ifa = ifa_ifwithroute(rt->rt_flags,
532
                                        rt_key(rt), Gate))))
533
                ifp = ifa->ifa_ifp;
534
        if (ifa) {
535
                register struct ifaddr *oifa = rt->rt_ifa;
536
                if (oifa != ifa) {
537
                    if (oifa && oifa->ifa_rtrequest)
538
                        oifa->ifa_rtrequest(RTM_DELETE,
539
                                                rt, Gate);
540
                    IFAFREE(rt->rt_ifa);
541
                    rt->rt_ifa = ifa;
542
                    ifa->ifa_refcnt++;
543
                    rt->rt_ifp = ifp;
544
                    rt->rt_rmx.rmx_mtu = ifp->if_mtu;
545
                    if (rt->rt_ifa && rt->rt_ifa->ifa_rtrequest)
546
                        rt->rt_ifa->ifa_rtrequest(RTM_ADD, rt, Gate);
547
                } else
548
                        goto call_ifareq;
549
                return;
550
        }
551
      call_ifareq:
552
        /* XXX: to reset gateway to correct value, at RTM_CHANGE */
553
        if (rt->rt_ifa && rt->rt_ifa->ifa_rtrequest)
554
                rt->rt_ifa->ifa_rtrequest(RTM_ADD, rt, Gate);
555
}
556
 
557
 
558
#define ROUNDUP(a) \
559
        ((a) > 0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof(long))
560
#define ADVANCE(x, n) (x += ROUNDUP((n)->sa_len))
561
 
562
static void
563
rt_xaddrs(cp, cplim, rtinfo)
564
        register caddr_t cp, cplim;
565
        register struct rt_addrinfo *rtinfo;
566
{
567
        register struct sockaddr *sa;
568
        register int i;
569
 
570
        bzero(rtinfo->rti_info, sizeof(rtinfo->rti_info));
571
        for (i = 0; (i < RTAX_MAX) && (cp < cplim); i++) {
572
                if ((rtinfo->rti_addrs & (1 << i)) == 0)
573
                        continue;
574
                rtinfo->rti_info[i] = sa = (struct sockaddr *)cp;
575
                ADVANCE(cp, sa);
576
        }
577
}
578
 
579
/*
580
 * Copy data from a buffer back into the indicated mbuf chain,
581
 * starting "off" bytes from the beginning, extending the mbuf
582
 * chain if necessary. The mbuf needs to be properly initalized
583
 * including the setting of m_len.
584
 */
585
void
586
m_copyback(m0, off, len, cp)
587
        struct  mbuf *m0;
588
        register int off;
589
        register int len;
590
        caddr_t cp;
591
{
592
        register int mlen;
593
        register struct mbuf *m = m0, *n;
594
        int totlen = 0;
595
 
596
        if (m0 == 0)
597
                return;
598
        while (off > (mlen = m->m_len)) {
599
                off -= mlen;
600
                totlen += mlen;
601
                if (m->m_next == 0) {
602
                        n = m_getclr(M_DONTWAIT, m->m_type);
603
                        if (n == 0)
604
                                goto out;
605
                        n->m_len = min(MLEN, len + off);
606
                        m->m_next = n;
607
                }
608
                m = m->m_next;
609
        }
610
        while (len > 0) {
611
                mlen = min (m->m_len - off, len);
612
                bcopy(cp, off + mtod(m, caddr_t), (unsigned)mlen);
613
                cp += mlen;
614
                len -= mlen;
615
                mlen += off;
616
                off = 0;
617
                totlen += mlen;
618
                if (len == 0)
619
                        break;
620
                if (m->m_next == 0) {
621
                        n = m_get(M_DONTWAIT, m->m_type);
622
                        if (n == 0)
623
                                break;
624
                        n->m_len = min(MLEN, len);
625
                        m->m_next = n;
626
                }
627
                m = m->m_next;
628
        }
629
out:    if (((m = m0)->m_flags & M_PKTHDR) && (m->m_pkthdr.len < totlen))
630
                m->m_pkthdr.len = totlen;
631
}
632
 
633
static struct mbuf *
634
rt_msg1(type, rtinfo)
635
        int type;
636
        register struct rt_addrinfo *rtinfo;
637
{
638
        register struct rt_msghdr *rtm;
639
        register struct mbuf *m;
640
        register int i;
641
        register struct sockaddr *sa;
642
        int len, dlen;
643
 
644
        m = m_gethdr(M_DONTWAIT, MT_DATA);
645
        if (m == 0)
646
                return (m);
647
        switch (type) {
648
 
649
        case RTM_DELADDR:
650
        case RTM_NEWADDR:
651
                len = sizeof(struct ifa_msghdr);
652
                break;
653
 
654
        case RTM_IFINFO:
655
                len = sizeof(struct if_msghdr);
656
                break;
657
 
658
        default:
659
                len = sizeof(struct rt_msghdr);
660
        }
661
        if (len > MHLEN)
662
                panic("rt_msg1");
663
        m->m_pkthdr.len = m->m_len = len;
664
        m->m_pkthdr.rcvif = 0;
665
        rtm = mtod(m, struct rt_msghdr *);
666
        bzero((caddr_t)rtm, len);
667
        for (i = 0; i < RTAX_MAX; i++) {
668
                if ((sa = rtinfo->rti_info[i]) == NULL)
669
                        continue;
670
                rtinfo->rti_addrs |= (1 << i);
671
                dlen = ROUNDUP(sa->sa_len);
672
                m_copyback(m, len, dlen, (caddr_t)sa);
673
                len += dlen;
674
        }
675
        if (m->m_pkthdr.len != len) {
676
                m_freem(m);
677
                return (NULL);
678
        }
679
        rtm->rtm_msglen = len;
680
        rtm->rtm_version = RTM_VERSION;
681
        rtm->rtm_type = type;
682
        return (m);
683
}
684
 
685
static int
686
rt_msg2(type, rtinfo, cp, w)
687
        int type;
688
        register struct rt_addrinfo *rtinfo;
689
        caddr_t cp;
690
        struct walkarg *w;
691
{
692
        register int i;
693
        int len, dlen, second_time = 0;
694
        caddr_t cp0;
695
 
696
        rtinfo->rti_addrs = 0;
697
again:
698
        switch (type) {
699
 
700
        case RTM_DELADDR:
701
        case RTM_NEWADDR:
702
                len = sizeof(struct ifa_msghdr);
703
                break;
704
 
705
        case RTM_IFINFO:
706
                len = sizeof(struct if_msghdr);
707
                break;
708
 
709
        default:
710
                len = sizeof(struct rt_msghdr);
711
        }
712
        if ((cp0 = cp) != NULL)
713
                cp += len;
714
        for (i = 0; i < RTAX_MAX; i++) {
715
                register struct sockaddr *sa;
716
 
717
                if ((sa = rtinfo->rti_info[i]) == 0)
718
                        continue;
719
                rtinfo->rti_addrs |= (1 << i);
720
                dlen = ROUNDUP(sa->sa_len);
721
                if (cp) {
722
                        bcopy((caddr_t)sa, cp, (unsigned)dlen);
723
                        cp += dlen;
724
                }
725
                len += dlen;
726
        }
727
        if (cp == 0 && w != NULL && !second_time) {
728
                register struct walkarg *rw = w;
729
 
730
                rw->w_needed += len;
731
                if (rw->w_needed <= 0 && rw->w_where) {
732
                        if (rw->w_tmemsize < len) {
733
                                if (rw->w_tmem)
734
                                        free(rw->w_tmem, M_RTABLE);
735
                                rw->w_tmem = (caddr_t) malloc(len, M_RTABLE,
736
                                                              M_NOWAIT);
737
                                if (rw->w_tmem)
738
                                        rw->w_tmemsize = len;
739
                        }
740
                        if (rw->w_tmem) {
741
                                cp = rw->w_tmem;
742
                                second_time = 1;
743
                                goto again;
744
                        } else
745
                                rw->w_where = 0;
746
                }
747
        }
748
        if (cp) {
749
                register struct rt_msghdr *rtm = (struct rt_msghdr *)cp0;
750
 
751
                rtm->rtm_version = RTM_VERSION;
752
                rtm->rtm_type = type;
753
                rtm->rtm_msglen = len;
754
        }
755
        return (len);
756
}
757
 
758
/*
759
 * This routine is called to generate a message from the routing
760
 * socket indicating that a redirect has occured, a routing lookup
761
 * has failed, or that a protocol has detected timeouts to a particular
762
 * destination.
763
 */
764
void
765
rt_missmsg(type, rtinfo, flags, error)
766
        int type, flags, error;
767
        register struct rt_addrinfo *rtinfo;
768
{
769
        register struct rt_msghdr *rtm;
770
        register struct mbuf *m;
771
        struct sockaddr *sa = rtinfo->rti_info[RTAX_DST];
772
 
773
        if (route_cb.any_count == 0)
774
                return;
775
        m = rt_msg1(type, rtinfo);
776
        if (m == 0)
777
                return;
778
        rtm = mtod(m, struct rt_msghdr *);
779
        rtm->rtm_flags = RTF_DONE | flags;
780
        rtm->rtm_errno = error;
781
        rtm->rtm_addrs = rtinfo->rti_addrs;
782
        route_proto.sp_protocol = sa ? sa->sa_family : 0;
783
        raw_input(m, &route_proto, &route_src, &route_dst);
784
}
785
 
786
/*
787
 * This routine is called to generate a message from the routing
788
 * socket indicating that the status of a network interface has changed.
789
 */
790
void
791
rt_ifmsg(ifp)
792
        register struct ifnet *ifp;
793
{
794
        register struct if_msghdr *ifm;
795
        struct mbuf *m;
796
        struct rt_addrinfo info;
797
 
798
        if (route_cb.any_count == 0)
799
                return;
800
        bzero((caddr_t)&info, sizeof(info));
801
        m = rt_msg1(RTM_IFINFO, &info);
802
        if (m == 0)
803
                return;
804
        ifm = mtod(m, struct if_msghdr *);
805
        ifm->ifm_index = ifp->if_index;
806
        ifm->ifm_flags = ifp->if_flags;
807
        ifm->ifm_data = ifp->if_data;
808
        ifm->ifm_addrs = 0;
809
        route_proto.sp_protocol = 0;
810
        raw_input(m, &route_proto, &route_src, &route_dst);
811
}
812
 
813
/*
814
 * This is called to generate messages from the routing socket
815
 * indicating a network interface has had addresses associated with it.
816
 * if we ever reverse the logic and replace messages TO the routing
817
 * socket indicate a request to configure interfaces, then it will
818
 * be unnecessary as the routing socket will automatically generate
819
 * copies of it.
820
 */
821
void
822
rt_newaddrmsg(cmd, ifa, error, rt)
823
        int cmd, error;
824
        register struct ifaddr *ifa;
825
        register struct rtentry *rt;
826
{
827
        struct rt_addrinfo info;
828
        struct sockaddr *sa = NULL;
829
        int pass;
830
        struct mbuf *m = NULL;
831
        struct ifnet *ifp = ifa->ifa_ifp;
832
 
833
        if (route_cb.any_count == 0)
834
                return;
835
        for (pass = 1; pass < 3; pass++) {
836
                bzero((caddr_t)&info, sizeof(info));
837
                if ((cmd == RTM_ADD && pass == 1) ||
838
                    (cmd == RTM_DELETE && pass == 2)) {
839
                        register struct ifa_msghdr *ifam;
840
                        int ncmd = cmd == RTM_ADD ? RTM_NEWADDR : RTM_DELADDR;
841
 
842
                        ifaaddr = sa = ifa->ifa_addr;
843
                        ifpaddr = ifp->if_addrlist.tqh_first->ifa_addr;
844
                        netmask = ifa->ifa_netmask;
845
                        brdaddr = ifa->ifa_dstaddr;
846
                        if ((m = rt_msg1(ncmd, &info)) == NULL)
847
                                continue;
848
                        ifam = mtod(m, struct ifa_msghdr *);
849
                        ifam->ifam_index = ifp->if_index;
850
                        ifam->ifam_metric = ifa->ifa_metric;
851
                        ifam->ifam_flags = ifa->ifa_flags;
852
                        ifam->ifam_addrs = info.rti_addrs;
853
                }
854
                if ((cmd == RTM_ADD && pass == 2) ||
855
                    (cmd == RTM_DELETE && pass == 1)) {
856
                        register struct rt_msghdr *rtm;
857
 
858
                        if (rt == 0)
859
                                continue;
860
                        netmask = rt_mask(rt);
861
                        dst = sa = rt_key(rt);
862
                        gate = rt->rt_gateway;
863
                        if ((m = rt_msg1(cmd, &info)) == NULL)
864
                                continue;
865
                        rtm = mtod(m, struct rt_msghdr *);
866
                        rtm->rtm_index = ifp->if_index;
867
                        rtm->rtm_flags |= rt->rt_flags;
868
                        rtm->rtm_errno = error;
869
                        rtm->rtm_addrs = info.rti_addrs;
870
                }
871
                route_proto.sp_protocol = sa ? sa->sa_family : 0;
872
                raw_input(m, &route_proto, &route_src, &route_dst);
873
        }
874
}
875
 
876
#ifndef __ECOS
877
/*
878
 * This is used in dumping the kernel table via sysctl().
879
 */
880
int
881
sysctl_dumpentry(rn, v)
882
        struct radix_node *rn;
883
        register void *v;
884
{
885
        register struct walkarg *w = v;
886
        register struct rtentry *rt = (struct rtentry *)rn;
887
        int error = 0, size;
888
        struct rt_addrinfo info;
889
 
890
        if (w->w_op == NET_RT_FLAGS && !(rt->rt_flags & w->w_arg))
891
                return 0;
892
        bzero((caddr_t)&info, sizeof(info));
893
        dst = rt_key(rt);
894
        gate = rt->rt_gateway;
895
        netmask = rt_mask(rt);
896
        genmask = rt->rt_genmask;
897
        if (rt->rt_ifp) {
898
                ifpaddr = rt->rt_ifp->if_addrlist.tqh_first->ifa_addr;
899
                ifaaddr = rt->rt_ifa->ifa_addr;
900
                if (rt->rt_ifp->if_flags & IFF_POINTOPOINT)
901
                        brdaddr = rt->rt_ifa->ifa_dstaddr;
902
        }
903
        size = rt_msg2(RTM_GET, &info, 0, w);
904
        if (w->w_where && w->w_tmem) {
905
                register struct rt_msghdr *rtm = (struct rt_msghdr *)w->w_tmem;
906
 
907
                rtm->rtm_flags = rt->rt_flags;
908
                rtm->rtm_use = rt->rt_use;
909
                rtm->rtm_rmx = rt->rt_rmx;
910
                rtm->rtm_index = rt->rt_ifp->if_index;
911
                rtm->rtm_errno = rtm->rtm_pid = rtm->rtm_seq = 0;
912
                rtm->rtm_addrs = info.rti_addrs;
913
                if ((error = copyout((caddr_t)rtm, w->w_where, size)) != 0)
914
                        w->w_where = NULL;
915
                else
916
                        w->w_where += size;
917
        }
918
        return (error);
919
}
920
 
921
int
922
sysctl_iflist(af, w)
923
        int     af;
924
        register struct walkarg *w;
925
{
926
        register struct ifnet *ifp;
927
        register struct ifaddr *ifa;
928
        struct  rt_addrinfo info;
929
        int     len, error = 0;
930
 
931
        bzero((caddr_t)&info, sizeof(info));
932
        for (ifp = ifnet.tqh_first; ifp != 0; ifp = ifp->if_list.tqe_next) {
933
                if (w->w_arg && w->w_arg != ifp->if_index)
934
                        continue;
935
                ifa = ifp->if_addrlist.tqh_first;
936
                ifpaddr = ifa->ifa_addr;
937
                len = rt_msg2(RTM_IFINFO, &info, (caddr_t)0, w);
938
                ifpaddr = 0;
939
                if (w->w_where && w->w_tmem) {
940
                        register struct if_msghdr *ifm;
941
 
942
                        ifm = (struct if_msghdr *)w->w_tmem;
943
                        ifm->ifm_index = ifp->if_index;
944
                        ifm->ifm_flags = ifp->if_flags;
945
                        ifm->ifm_data = ifp->if_data;
946
                        ifm->ifm_addrs = info.rti_addrs;
947
                        error = copyout((caddr_t)ifm, w->w_where, len);
948
                        if (error)
949
                                return (error);
950
                        w->w_where += len;
951
                }
952
                while ((ifa = ifa->ifa_list.tqe_next) != NULL) {
953
                        if (af && af != ifa->ifa_addr->sa_family)
954
                                continue;
955
                        ifaaddr = ifa->ifa_addr;
956
                        netmask = ifa->ifa_netmask;
957
                        brdaddr = ifa->ifa_dstaddr;
958
                        len = rt_msg2(RTM_NEWADDR, &info, 0, w);
959
                        if (w->w_where && w->w_tmem) {
960
                                register struct ifa_msghdr *ifam;
961
 
962
                                ifam = (struct ifa_msghdr *)w->w_tmem;
963
                                ifam->ifam_index = ifa->ifa_ifp->if_index;
964
                                ifam->ifam_flags = ifa->ifa_flags;
965
                                ifam->ifam_metric = ifa->ifa_metric;
966
                                ifam->ifam_addrs = info.rti_addrs;
967
                                error = copyout(w->w_tmem, w->w_where, len);
968
                                if (error)
969
                                        return (error);
970
                                w->w_where += len;
971
                        }
972
                }
973
                ifaaddr = netmask = brdaddr = 0;
974
        }
975
        return (0);
976
}
977
 
978
int
979
sysctl_rtable(name, namelen, where, given, new, newlen)
980
        int     *name;
981
        u_int   namelen;
982
        void    *where;
983
        size_t  *given;
984
        void    *new;
985
        size_t  newlen;
986
{
987
        register struct radix_node_head *rnh;
988
        int     i, s, error = EINVAL;
989
        u_char  af;
990
        struct  walkarg w;
991
 
992
        if (new)
993
                return (EPERM);
994
        if (namelen != 3)
995
                return (EINVAL);
996
        af = name[0];
997
        Bzero(&w, sizeof(w));
998
        w.w_where = where;
999
        w.w_given = *given;
1000
        w.w_needed = 0 - w.w_given;
1001
        w.w_op = name[1];
1002
        w.w_arg = name[2];
1003
 
1004
        s = splsoftnet();
1005
        switch (w.w_op) {
1006
 
1007
        case NET_RT_DUMP:
1008
        case NET_RT_FLAGS:
1009
                for (i = 1; i <= AF_MAX; i++)
1010
                        if ((rnh = rt_tables[i]) && (af == 0 || af == i) &&
1011
                            (error = (*rnh->rnh_walktree)(rnh,
1012
                                                          sysctl_dumpentry,
1013
                                                          &w)))
1014
                                break;
1015
                break;
1016
 
1017
        case NET_RT_IFLIST:
1018
                error = sysctl_iflist(af, &w);
1019
        }
1020
        splx(s);
1021
        if (w.w_tmem)
1022
                free(w.w_tmem, M_RTABLE);
1023
        w.w_needed += w.w_given;
1024
        if (where) {
1025
                *given = w.w_where - (caddr_t) where;
1026
                if (*given < w.w_needed)
1027
                        return (ENOMEM);
1028
        } else {
1029
                *given = (11 * w.w_needed) / 10;
1030
        }
1031
        return (error);
1032
}
1033
#endif
1034
 
1035
/*
1036
 * Definitions of protocols supported in the ROUTE domain.
1037
 */
1038
 
1039
extern  struct domain routedomain;              /* or at least forward */
1040
 
1041
struct protosw routesw[] = {
1042
{ SOCK_RAW,     &routedomain,   0,               PR_ATOMIC|PR_ADDR,
1043
  raw_input,    route_output,   raw_ctlinput,   0,
1044
  route_usrreq,
1045
  raw_init,     0,               0,               0,
1046
#ifdef __ECOS
1047
      0,
1048
#else
1049
  sysctl_rtable,
1050
#endif
1051
}
1052
};
1053
 
1054
struct domain routedomain =
1055
    { PF_ROUTE, "route", route_init, 0, 0,
1056
      routesw, &routesw[sizeof(routesw)/sizeof(routesw[0])] };

powered by: WebSVN 2.1.0

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