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

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [rtos/] [ecos-3.0/] [packages/] [net/] [tcpip/] [current/] [src/] [sys/] [net/] [rtsock.c] - Blame information for rev 834

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

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

powered by: WebSVN 2.1.0

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