source: rtems/cpukit/libnetworking/net/rtsock.c @ f8397280

4.104.115
Last change on this file since f8397280 was f8397280, checked in by Till Straumann <strauman@…>, on 10/20/09 at 14:13:24

2009-10-20 Till Straumann <strauman@…>

PR1424/networking

  • libnetworking/net/rtsock.c: (merged from BSD) copy information out to user buffer.
  • Property mode set to 100644
File size: 21.4 KB
RevLine 
[39e6e65a]1/*
2 * Copyright (c) 1988, 1991, 1993
3 *      The Regents of the University of California.  All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 * 4. Neither the name of the University nor the names of its contributors
14 *    may be used to endorse or promote products derived from this software
15 *    without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 *
[1af76095]29 *      @(#)rtsock.c    8.7 (Berkeley) 10/12/95
30 * $FreeBSD: src/sys/net/rtsock.c,v 1.122 2005/03/26 21:49:43 sam Exp $
[39e6e65a]31 */
32
[1af76095]33/*
34 * $Id$
35 */
36 
[39e6e65a]37#include <sys/param.h>
[c301570]38#include <rtems/bsd/sys/queue.h>
[39e6e65a]39#include <sys/systm.h>
40#include <sys/kernel.h>
41#include <sys/sysctl.h>
42#include <sys/proc.h>
43#include <sys/mbuf.h>
44#include <sys/socket.h>
45#include <sys/socketvar.h>
46#include <sys/domain.h>
47#include <sys/protosw.h>
48
49#include <net/if.h>
50#include <net/route.h>
51#include <net/raw_cb.h>
52
[300c071c]53static struct   sockaddr route_dst = { 2, PF_ROUTE, { 0 }  };
54static struct   sockaddr route_src = { 2, PF_ROUTE, { 0 } };
55static struct   sockaddr sa_zero   = { sizeof(sa_zero), AF_INET, { 0 } };
56static struct   sockproto route_proto = { PF_ROUTE, 0 };
[39e6e65a]57
58struct walkarg {
59        int     w_tmemsize;
60        int     w_op, w_arg;
61        caddr_t w_tmem;
62        struct sysctl_req *w_req;
63};
64
[1af76095]65static struct mbuf *rt_msg1(int type, struct rt_addrinfo *rtinfo);
66static int      rt_msg2(int type, struct rt_addrinfo *rtinfo,
67        caddr_t cp, struct walkarg *w);
68static int      rt_xaddrs(caddr_t cp, caddr_t cplim,
69        struct rt_addrinfo *rtinfo);
70static int      sysctl_dumpentry(struct radix_node *rn, void *vw);
71static int      sysctl_iflist(int af, struct walkarg *w);
72static int      route_output(struct mbuf *m, struct socket *so);
73static int      route_usrreq(struct socket *,
74        int, struct mbuf *, struct mbuf *, struct mbuf *);
75static void     rt_setmetrics(u_long which, const struct rt_metrics *in,
76        struct rt_metrics *out);
[39e6e65a]77
78/*ARGSUSED*/
79static int
[42e10fa2]80route_usrreq(struct socket *so, int req, struct mbuf *m, struct mbuf *nam,
81    struct mbuf *control)
[39e6e65a]82{
[1af76095]83        int error = 0;
84        struct rawcb *rp = sotorawcb(so);
[39e6e65a]85        int s;
86
87        if (req == PRU_ATTACH) {
88                MALLOC(rp, struct rawcb *, sizeof(*rp), M_PCB, M_WAITOK);
89                so->so_pcb = (caddr_t)rp;
90                if (so->so_pcb)
91                        bzero(so->so_pcb, sizeof(*rp));
92        }
93        if (req == PRU_DETACH && rp) {
94                int af = rp->rcb_proto.sp_protocol;
95                if (af == AF_INET)
96                        route_cb.ip_count--;
97                else if (af == AF_IPX)
98                        route_cb.ipx_count--;
99                else if (af == AF_ISO)
100                        route_cb.iso_count--;
101                route_cb.any_count--;
102        }
103        s = splnet();
104        error = raw_usrreq(so, req, m, nam, control);
105        rp = sotorawcb(so);
106        if (req == PRU_ATTACH && rp) {
107                int af = rp->rcb_proto.sp_protocol;
108                if (error) {
109                        free((caddr_t)rp, M_PCB);
110                        splx(s);
111                        return (error);
112                }
113                if (af == AF_INET)
114                        route_cb.ip_count++;
115                else if (af == AF_IPX)
116                        route_cb.ipx_count++;
117                else if (af == AF_ISO)
118                        route_cb.iso_count++;
119                rp->rcb_faddr = &route_src;
120                route_cb.any_count++;
121                soisconnected(so);
122                so->so_options |= SO_USELOOPBACK;
123        }
124        splx(s);
125        return (error);
126}
127
128/*ARGSUSED*/
129static int
[1af76095]130route_output(struct mbuf *m, struct socket *so)
[39e6e65a]131{
[1af76095]132#define sa_equal(a1, a2) (bcmp((a1), (a2), (a1)->sa_len) == 0)
133        struct rt_msghdr *rtm = NULL;
134        struct rtentry *rt = NULL;
[39e6e65a]135        struct rtentry *saved_nrt = 0;
136        struct radix_node_head *rnh;
137        struct rt_addrinfo info;
138        int len, error = 0;
[1af76095]139        struct ifnet *ifp = NULL;
140        struct ifaddr *ifa = NULL;
[39e6e65a]141
142#define senderr(e) { error = e; goto flush;}
[1af76095]143        if (m == NULL || ((m->m_len < sizeof(long)) &&
144                       (m = m_pullup(m, sizeof(long))) == NULL))
[39e6e65a]145                return (ENOBUFS);
146        if ((m->m_flags & M_PKTHDR) == 0)
147                panic("route_output");
148        len = m->m_pkthdr.len;
149        if (len < sizeof(*rtm) ||
150            len != mtod(m, struct rt_msghdr *)->rtm_msglen) {
[1af76095]151                info.rti_info[RTAX_DST] = NULL;
[39e6e65a]152                senderr(EINVAL);
153        }
154        R_Malloc(rtm, struct rt_msghdr *, len);
[1af76095]155        if (rtm == NULL) {
156                info.rti_info[RTAX_DST] = NULL;
[39e6e65a]157                senderr(ENOBUFS);
158        }
159        m_copydata(m, 0, len, (caddr_t)rtm);
160        if (rtm->rtm_version != RTM_VERSION) {
[1af76095]161                info.rti_info[RTAX_DST] = NULL;
[39e6e65a]162                senderr(EPROTONOSUPPORT);
163        }
164        info.rti_addrs = rtm->rtm_addrs;
165        if (rt_xaddrs((caddr_t)(rtm + 1), len + (caddr_t)rtm, &info)) {
[1af76095]166                info.rti_info[RTAX_DST] = NULL;
[39e6e65a]167                senderr(EINVAL);
168        }
[1af76095]169        if (info.rti_info[RTAX_DST] == NULL ||
170            info.rti_info[RTAX_DST]->sa_family >= AF_MAX ||
171            (info.rti_info[RTAX_GATEWAY] != NULL &&
172             info.rti_info[RTAX_GATEWAY]->sa_family >= AF_MAX))
[39e6e65a]173                senderr(EINVAL);
[1af76095]174        if (info.rti_info[RTAX_GENMASK]) {
[39e6e65a]175                struct radix_node *t;
[1af76095]176                t = rn_addmask((caddr_t) info.rti_info[RTAX_GENMASK], 0, 1);
177                if (t != NULL &&
178                    Bcmp(info.rti_info[RTAX_GENMASK], t->rn_key, *(u_char *)info.rti_info[RTAX_GENMASK]) == 0)
179                        info.rti_info[RTAX_GENMASK] =
180                            (struct sockaddr *)t->rn_key;
[39e6e65a]181                else
182                        senderr(ENOBUFS);
183        }
184        switch (rtm->rtm_type) {
185
186        case RTM_ADD:
[1af76095]187                if (info.rti_info[RTAX_GATEWAY] == NULL)
[39e6e65a]188                        senderr(EINVAL);
[1af76095]189                error = rtrequest(RTM_ADD, info.rti_info[RTAX_DST], info.rti_info[RTAX_GATEWAY], info.rti_info[RTAX_NETMASK],
[39e6e65a]190                                        rtm->rtm_flags, &saved_nrt);
191                if (error == 0 && saved_nrt) {
192                        rt_setmetrics(rtm->rtm_inits,
193                                &rtm->rtm_rmx, &saved_nrt->rt_rmx);
194                        saved_nrt->rt_rmx.rmx_locks &= ~(rtm->rtm_inits);
195                        saved_nrt->rt_rmx.rmx_locks |=
196                                (rtm->rtm_inits & rtm->rtm_rmx.rmx_locks);
197                        saved_nrt->rt_refcnt--;
[1af76095]198                        saved_nrt->rt_genmask = info.rti_info[RTAX_GENMASK];
[39e6e65a]199                }
200                break;
201
202        case RTM_DELETE:
[1af76095]203                error = rtrequest(RTM_DELETE, info.rti_info[RTAX_DST], info.rti_info[RTAX_GATEWAY], info.rti_info[RTAX_NETMASK],
[39e6e65a]204                                rtm->rtm_flags, &saved_nrt);
205                if (error == 0) {
206                        if ((rt = saved_nrt))
207                                rt->rt_refcnt++;
208                        goto report;
209                }
210                break;
211
212        case RTM_GET:
213        case RTM_CHANGE:
214        case RTM_LOCK:
[1af76095]215                rnh = rt_tables[info.rti_info[RTAX_DST]->sa_family];
216                if (rnh == NULL) {
[39e6e65a]217                        senderr(EAFNOSUPPORT);
[ff0f694d]218                } else if ((rt = (struct rtentry *)
[1af76095]219                                rnh->rnh_lookup(info.rti_info[RTAX_DST], info.rti_info[RTAX_NETMASK], rnh)))
[39e6e65a]220                        rt->rt_refcnt++;
221                else
222                        senderr(ESRCH);
223                switch(rtm->rtm_type) {
224
225                case RTM_GET:
226                report:
[1af76095]227                        info.rti_info[RTAX_DST] = rt_key(rt);
228                        info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
229                        info.rti_info[RTAX_NETMASK] = rt_mask(rt);
230                        info.rti_info[RTAX_GENMASK] = rt->rt_genmask;
[39e6e65a]231                        if (rtm->rtm_addrs & (RTA_IFP | RTA_IFA)) {
232                                ifp = rt->rt_ifp;
233                                if (ifp) {
[1af76095]234                                        info.rti_info[RTAX_IFP] = ifp->if_addrlist->ifa_addr;
235                                        info.rti_info[RTAX_IFA] = rt->rt_ifa->ifa_addr;
[39e6e65a]236                                        rtm->rtm_index = ifp->if_index;
237                                } else {
[1af76095]238                                        info.rti_info[RTAX_IFP] = NULL;
239                                        info.rti_info[RTAX_IFA] = NULL;
[39e6e65a]240                            }
241                        }
[1af76095]242                        len = rt_msg2(rtm->rtm_type, &info, NULL, NULL);
[39e6e65a]243                        if (len > rtm->rtm_msglen) {
244                                struct rt_msghdr *new_rtm;
245                                R_Malloc(new_rtm, struct rt_msghdr *, len);
[1af76095]246                                if (new_rtm == NULL) {
[39e6e65a]247                                        senderr(ENOBUFS);
[1af76095]248                                }
[39e6e65a]249                                Bcopy(rtm, new_rtm, rtm->rtm_msglen);
250                                Free(rtm); rtm = new_rtm;
251                        }
[1af76095]252                        (void)rt_msg2(rtm->rtm_type, &info, (caddr_t)rtm, NULL);
[39e6e65a]253                        rtm->rtm_flags = rt->rt_flags;
254                        rtm->rtm_rmx = rt->rt_rmx;
255                        rtm->rtm_addrs = info.rti_addrs;
256                        break;
257
258                case RTM_CHANGE:
[1af76095]259                        if (info.rti_info[RTAX_GATEWAY] && (error = rt_setgate(rt, rt_key(rt), info.rti_info[RTAX_GATEWAY])))
[39e6e65a]260                                senderr(error);
261
262                        /*
263                         * If they tried to change things but didn't specify
264                         * the required gateway, then just use the old one.
265                         * This can happen if the user tries to change the
266                         * flags on the default route without changing the
267                         * default gateway.  Changing flags still doesn't work.
268                         */
[1af76095]269                        if ((rt->rt_flags & RTF_GATEWAY) && !info.rti_info[RTAX_GATEWAY])
270                                info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
[39e6e65a]271
272                        /* new gateway could require new ifaddr, ifp;
273                           flags may also be different; ifp may be specified
274                           by ll sockaddr when protocol address is ambiguous */
[1af76095]275                        if (info.rti_info[RTAX_IFP] && (ifa = ifa_ifwithnet(info.rti_info[RTAX_IFP])) &&
276                            (ifp = ifa->ifa_ifp) && (info.rti_info[RTAX_IFA] || info.rti_info[RTAX_GATEWAY]))
277                                ifa = ifaof_ifpforaddr(info.rti_info[RTAX_IFA] ? info.rti_info[RTAX_IFA] : info.rti_info[RTAX_GATEWAY],
[39e6e65a]278                                                        ifp);
[1af76095]279                        else if ((info.rti_info[RTAX_IFA] && (ifa = ifa_ifwithaddr(info.rti_info[RTAX_IFA]))) ||
280                                 (info.rti_info[RTAX_GATEWAY] && (ifa = ifa_ifwithroute(rt->rt_flags,
281                                                        rt_key(rt), info.rti_info[RTAX_GATEWAY]))))
[39e6e65a]282                                ifp = ifa->ifa_ifp;
283                        if (ifa) {
[1af76095]284                                struct ifaddr *oifa = rt->rt_ifa;
[39e6e65a]285                                if (oifa != ifa) {
286                                    if (oifa && oifa->ifa_rtrequest)
287                                        oifa->ifa_rtrequest(RTM_DELETE,
[1af76095]288                                                                rt, info.rti_info[RTAX_GATEWAY]);
[39e6e65a]289                                    IFAFREE(rt->rt_ifa);
290                                    rt->rt_ifa = ifa;
291                                    ifa->ifa_refcnt++;
292                                    rt->rt_ifp = ifp;
293                                }
294                        }
295                        rt_setmetrics(rtm->rtm_inits, &rtm->rtm_rmx,
296                                        &rt->rt_rmx);
297                        if (rt->rt_ifa && rt->rt_ifa->ifa_rtrequest)
[1af76095]298                               rt->rt_ifa->ifa_rtrequest(RTM_ADD, rt, info.rti_info[RTAX_GATEWAY]);
299                        if (info.rti_info[RTAX_GENMASK])
300                                rt->rt_genmask = info.rti_info[RTAX_GENMASK];
301                        /* FALLTHROUGH */
[39e6e65a]302                case RTM_LOCK:
303                        rt->rt_rmx.rmx_locks &= ~(rtm->rtm_inits);
304                        rt->rt_rmx.rmx_locks |=
305                                (rtm->rtm_inits & rtm->rtm_rmx.rmx_locks);
306                        break;
307                }
308                break;
309
310        default:
311                senderr(EOPNOTSUPP);
312        }
313
314flush:
315        if (rtm) {
316                if (error)
317                        rtm->rtm_errno = error;
318                else
319                        rtm->rtm_flags |= RTF_DONE;
320        }
[1af76095]321        if (rt)         /* XXX can this be true? */
[39e6e65a]322                rtfree(rt);
323    {
[1af76095]324        struct rawcb *rp = NULL;
[39e6e65a]325        /*
326         * Check to see if we don't want our own messages.
327         */
328        if ((so->so_options & SO_USELOOPBACK) == 0) {
329                if (route_cb.any_count <= 1) {
330                        if (rtm)
331                                Free(rtm);
332                        m_freem(m);
333                        return (error);
334                }
335                /* There is another listener, so construct message */
336                rp = sotorawcb(so);
337        }
338        if (rtm) {
339                m_copyback(m, 0, rtm->rtm_msglen, (caddr_t)rtm);
340                Free(rtm);
341        }
342        if (rp)
343                rp->rcb_proto.sp_family = 0; /* Avoid us */
[1af76095]344        if (info.rti_info[RTAX_DST])
345                route_proto.sp_protocol = info.rti_info[RTAX_DST]->sa_family;
[39e6e65a]346        raw_input(m, &route_proto, &route_src, &route_dst);
347        if (rp)
348                rp->rcb_proto.sp_family = PF_ROUTE;
349    }
350        return (error);
[1af76095]351#undef  sa_equal
[39e6e65a]352}
353
354static void
[1af76095]355rt_setmetrics(u_long which, const struct rt_metrics *in,
356      struct rt_metrics *out)
[39e6e65a]357{
358#define metric(f, e) if (which & (f)) out->e = in->e;
359        metric(RTV_RPIPE, rmx_recvpipe);
360        metric(RTV_SPIPE, rmx_sendpipe);
361        metric(RTV_SSTHRESH, rmx_ssthresh);
362        metric(RTV_RTT, rmx_rtt);
363        metric(RTV_RTTVAR, rmx_rttvar);
364        metric(RTV_HOPCOUNT, rmx_hopcount);
365        metric(RTV_MTU, rmx_mtu);
366        metric(RTV_EXPIRE, rmx_expire);
367#undef metric
368}
369
370#define ROUNDUP(a) \
371        ((a) > 0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof(long))
372#define ADVANCE(x, n) (x += ROUNDUP((n)->sa_len))
373
374
375/*
376 * Extract the addresses of the passed sockaddrs.
377 * Do a little sanity checking so as to avoid bad memory references.
378 * This data is derived straight from userland.
379 */
380static int
[1af76095]381rt_xaddrs(caddr_t cp, caddr_t cplim, struct rt_addrinfo *rtinfo)
[39e6e65a]382{
[1af76095]383        struct sockaddr *sa;
384        int i;
[39e6e65a]385
386        bzero(rtinfo->rti_info, sizeof(rtinfo->rti_info));
[1af76095]387        for (i = 0; i < RTAX_MAX && cp < cplim; i++) {
[39e6e65a]388                if ((rtinfo->rti_addrs & (1 << i)) == 0)
389                        continue;
390                sa = (struct sockaddr *)cp;
391                /*
392                 * It won't fit.
393                 */
[1af76095]394                if (cp + sa->sa_len > cplim)
[39e6e65a]395                        return (EINVAL);
396                /*
397                 * there are no more.. quit now
398                 * If there are more bits, they are in error.
399                 * I've seen this. route(1) can evidently generate these.
400                 * This causes kernel to core dump.
401                 * for compatibility, If we see this, point to a safe address.
402                 */
403                if (sa->sa_len == 0) {
404                        rtinfo->rti_info[i] = &sa_zero;
405                        return (0); /* should be EINVAL but for compat */
406                }
407                /* accept it */
408                rtinfo->rti_info[i] = sa;
409                ADVANCE(cp, sa);
410        }
411        return (0);
412}
413
414static struct mbuf *
[1af76095]415rt_msg1(int type, struct rt_addrinfo *rtinfo)
[39e6e65a]416{
[1af76095]417        struct rt_msghdr *rtm;
418        struct mbuf *m;
419        int i;
420        struct sockaddr *sa;
[39e6e65a]421        int len, dlen;
422
423        m = m_gethdr(M_DONTWAIT, MT_DATA);
424        if (m == 0)
425                return (m);
426        switch (type) {
427
428        case RTM_DELADDR:
429        case RTM_NEWADDR:
430                len = sizeof(struct ifa_msghdr);
431                break;
432
433        case RTM_IFINFO:
434                len = sizeof(struct if_msghdr);
435                break;
436
437        default:
438                len = sizeof(struct rt_msghdr);
439        }
440        if (len > MHLEN)
441                panic("rt_msg1");
442        m->m_pkthdr.len = m->m_len = len;
[1af76095]443        m->m_pkthdr.rcvif = NULL;
[39e6e65a]444        rtm = mtod(m, struct rt_msghdr *);
445        bzero((caddr_t)rtm, len);
446        for (i = 0; i < RTAX_MAX; i++) {
447                if ((sa = rtinfo->rti_info[i]) == NULL)
448                        continue;
449                rtinfo->rti_addrs |= (1 << i);
450                dlen = ROUNDUP(sa->sa_len);
451                m_copyback(m, len, dlen, (caddr_t)sa);
452                len += dlen;
453        }
454        if (m->m_pkthdr.len != len) {
455                m_freem(m);
456                return (NULL);
457        }
458        rtm->rtm_msglen = len;
459        rtm->rtm_version = RTM_VERSION;
460        rtm->rtm_type = type;
461        return (m);
462}
463
464static int
[1af76095]465rt_msg2(int type, struct rt_addrinfo *rtinfo, caddr_t cp, struct walkarg *w)
[39e6e65a]466{
[1af76095]467        int i;
[39e6e65a]468        int len, dlen, second_time = 0;
469        caddr_t cp0;
470
471        rtinfo->rti_addrs = 0;
472again:
473        switch (type) {
474
475        case RTM_DELADDR:
476        case RTM_NEWADDR:
477                len = sizeof(struct ifa_msghdr);
478                break;
479
480        case RTM_IFINFO:
481                len = sizeof(struct if_msghdr);
482                break;
483
484        default:
485                len = sizeof(struct rt_msghdr);
486        }
487        cp0 = cp;
488        if (cp0)
489                cp += len;
490        for (i = 0; i < RTAX_MAX; i++) {
[1af76095]491                struct sockaddr *sa;
[39e6e65a]492
[1af76095]493                if ((sa = rtinfo->rti_info[i]) == NULL)
[39e6e65a]494                        continue;
495                rtinfo->rti_addrs |= (1 << i);
496                dlen = ROUNDUP(sa->sa_len);
497                if (cp) {
498                        bcopy((caddr_t)sa, cp, (unsigned)dlen);
499                        cp += dlen;
500                }
501                len += dlen;
502        }
[1af76095]503        if (cp == NULL && w != NULL && !second_time) {
504                struct walkarg *rw = w;
[39e6e65a]505
506                if (rw->w_req) {
507                        if (rw->w_tmemsize < len) {
508                                if (rw->w_tmem)
509                                        free(rw->w_tmem, M_RTABLE);
510                                rw->w_tmem = (caddr_t)
511                                        malloc(len, M_RTABLE, M_NOWAIT);
512                                if (rw->w_tmem)
513                                        rw->w_tmemsize = len;
514                        }
515                        if (rw->w_tmem) {
516                                cp = rw->w_tmem;
517                                second_time = 1;
518                                goto again;
519                        }
520                }
521        }
522        if (cp) {
[1af76095]523                struct rt_msghdr *rtm = (struct rt_msghdr *)cp0;
[39e6e65a]524
525                rtm->rtm_version = RTM_VERSION;
526                rtm->rtm_type = type;
527                rtm->rtm_msglen = len;
528        }
529        return (len);
530}
531
532/*
533 * This routine is called to generate a message from the routing
534 * socket indicating that a redirect has occured, a routing lookup
535 * has failed, or that a protocol has detected timeouts to a particular
536 * destination.
537 */
538void
[1af76095]539rt_missmsg(int type, struct rt_addrinfo *rtinfo, int flags, int error)
[39e6e65a]540{
[1af76095]541        struct rt_msghdr *rtm;
542        struct mbuf *m;
[39e6e65a]543        struct sockaddr *sa = rtinfo->rti_info[RTAX_DST];
544
545        if (route_cb.any_count == 0)
546                return;
547        m = rt_msg1(type, rtinfo);
[1af76095]548        if (m == NULL)
[39e6e65a]549                return;
550        rtm = mtod(m, struct rt_msghdr *);
551        rtm->rtm_flags = RTF_DONE | flags;
552        rtm->rtm_errno = error;
553        rtm->rtm_addrs = rtinfo->rti_addrs;
554        route_proto.sp_protocol = sa ? sa->sa_family : 0;
555        raw_input(m, &route_proto, &route_src, &route_dst);
556}
557
558/*
559 * This routine is called to generate a message from the routing
560 * socket indicating that the status of a network interface has changed.
561 */
562void
[1af76095]563rt_ifmsg(struct ifnet *ifp)
[39e6e65a]564{
[1af76095]565        struct if_msghdr *ifm;
[39e6e65a]566        struct mbuf *m;
567        struct rt_addrinfo info;
568
569        if (route_cb.any_count == 0)
570                return;
571        bzero((caddr_t)&info, sizeof(info));
572        m = rt_msg1(RTM_IFINFO, &info);
[1af76095]573        if (m == NULL)
[39e6e65a]574                return;
575        ifm = mtod(m, struct if_msghdr *);
576        ifm->ifm_index = ifp->if_index;
[1af76095]577        ifm->ifm_flags = ifp->if_flags;
[39e6e65a]578        ifm->ifm_data = ifp->if_data;
579        ifm->ifm_addrs = 0;
580        route_proto.sp_protocol = 0;
581        raw_input(m, &route_proto, &route_src, &route_dst);
582}
583
584/*
585 * This is called to generate messages from the routing socket
586 * indicating a network interface has had addresses associated with it.
587 * if we ever reverse the logic and replace messages TO the routing
588 * socket indicate a request to configure interfaces, then it will
589 * be unnecessary as the routing socket will automatically generate
590 * copies of it.
591 */
592void
[1af76095]593rt_newaddrmsg(int cmd, struct ifaddr *ifa, int error, struct rtentry *rt)
[39e6e65a]594{
595        struct rt_addrinfo info;
[1af76095]596        struct sockaddr *sa = NULL;
[39e6e65a]597        int pass;
[1af76095]598        struct mbuf *m = NULL;
[39e6e65a]599        struct ifnet *ifp = ifa->ifa_ifp;
600
601        if (route_cb.any_count == 0)
602                return;
603        for (pass = 1; pass < 3; pass++) {
604                bzero((caddr_t)&info, sizeof(info));
605                if ((cmd == RTM_ADD && pass == 1) ||
606                    (cmd == RTM_DELETE && pass == 2)) {
[1af76095]607                        struct ifa_msghdr *ifam;
[39e6e65a]608                        int ncmd = cmd == RTM_ADD ? RTM_NEWADDR : RTM_DELADDR;
609
[1af76095]610                        info.rti_info[RTAX_IFA] = sa = ifa->ifa_addr;
611                        info.rti_info[RTAX_IFP] = ifp->if_addrlist->ifa_addr;
612                        info.rti_info[RTAX_NETMASK] = ifa->ifa_netmask;
613                        info.rti_info[RTAX_BRD] = ifa->ifa_dstaddr;
[39e6e65a]614                        if ((m = rt_msg1(ncmd, &info)) == NULL)
615                                continue;
616                        ifam = mtod(m, struct ifa_msghdr *);
617                        ifam->ifam_index = ifp->if_index;
618                        ifam->ifam_metric = ifa->ifa_metric;
619                        ifam->ifam_flags = ifa->ifa_flags;
620                        ifam->ifam_addrs = info.rti_addrs;
621                }
622                if ((cmd == RTM_ADD && pass == 2) ||
623                    (cmd == RTM_DELETE && pass == 1)) {
[1af76095]624                        struct rt_msghdr *rtm;
[39e6e65a]625
[1af76095]626                        if (rt == NULL)
[39e6e65a]627                                continue;
[1af76095]628                        info.rti_info[RTAX_NETMASK] = rt_mask(rt);
629                        info.rti_info[RTAX_DST] = sa = rt_key(rt);
630                        info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
[39e6e65a]631                        if ((m = rt_msg1(cmd, &info)) == NULL)
632                                continue;
633                        rtm = mtod(m, struct rt_msghdr *);
634                        rtm->rtm_index = ifp->if_index;
635                        rtm->rtm_flags |= rt->rt_flags;
636                        rtm->rtm_errno = error;
637                        rtm->rtm_addrs = info.rti_addrs;
638                }
639                route_proto.sp_protocol = sa ? sa->sa_family : 0;
640                raw_input(m, &route_proto, &route_src, &route_dst);
641        }
642}
643
644
645/*
646 * This is used in dumping the kernel table via sysctl().
647 */
648int
[1af76095]649sysctl_dumpentry(struct radix_node *rn, void *vw)
[39e6e65a]650{
[1af76095]651        struct walkarg *w = vw;
652        struct rtentry *rt = (struct rtentry *)rn;
[39e6e65a]653        int error = 0, size;
654        struct rt_addrinfo info;
655
656        if (w->w_op == NET_RT_FLAGS && !(rt->rt_flags & w->w_arg))
657                return 0;
658        bzero((caddr_t)&info, sizeof(info));
[1af76095]659        info.rti_info[RTAX_DST] = rt_key(rt);
660        info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
661        info.rti_info[RTAX_NETMASK] = rt_mask(rt);
662        info.rti_info[RTAX_GENMASK] = rt->rt_genmask;
663        size = rt_msg2(RTM_GET, &info, NULL, w);
[39e6e65a]664        if (w->w_req && w->w_tmem) {
[1af76095]665                struct rt_msghdr *rtm = (struct rt_msghdr *)w->w_tmem;
[39e6e65a]666
667                rtm->rtm_flags = rt->rt_flags;
668                rtm->rtm_use = rt->rt_use;
669                rtm->rtm_rmx = rt->rt_rmx;
670                rtm->rtm_index = rt->rt_ifp->if_index;
671                rtm->rtm_errno = rtm->rtm_pid = rtm->rtm_seq = 0;
672                rtm->rtm_addrs = info.rti_addrs;
[f8397280]673                error = SYSCTL_OUT(w->w_req, (caddr_t)rtm, size);
[39e6e65a]674                return (error);
675        }
676        return (error);
677}
678
679int
[1af76095]680sysctl_iflist(int af, struct walkarg *w)
[39e6e65a]681{
[1af76095]682        struct ifnet *ifp;
683        struct ifaddr *ifa;
[39e6e65a]684        struct  rt_addrinfo info;
685        int     len, error = 0;
686
687        bzero((caddr_t)&info, sizeof(info));
688        for (ifp = ifnet; ifp; ifp = ifp->if_next) {
689                if (w->w_arg && w->w_arg != ifp->if_index)
690                        continue;
691                ifa = ifp->if_addrlist;
[1af76095]692                info.rti_info[RTAX_IFP] = ifa->ifa_addr;
[39e6e65a]693                len = rt_msg2(RTM_IFINFO, &info, (caddr_t)0, w);
[1af76095]694                info.rti_info[RTAX_IFP] = 0;
[39e6e65a]695                if (w->w_req && w->w_tmem) {
[1af76095]696                        struct if_msghdr *ifm;
[39e6e65a]697
698                        ifm = (struct if_msghdr *)w->w_tmem;
699                        ifm->ifm_index = ifp->if_index;
[1af76095]700                        ifm->ifm_flags = ifp->if_flags;
[39e6e65a]701                        ifm->ifm_data = ifp->if_data;
702                        ifm->ifm_addrs = info.rti_addrs;
[36799d4]703                        error = SYSCTL_OUT(w->w_req,(caddr_t)ifm, len);
[39e6e65a]704                        if (error)
705                                return (error);
706                }
707                while ((ifa = ifa->ifa_next) != 0) {
708                        if (af && af != ifa->ifa_addr->sa_family)
709                                continue;
[1af76095]710                        info.rti_info[RTAX_IFA] = ifa->ifa_addr;
711                        info.rti_info[RTAX_NETMASK] = ifa->ifa_netmask;
712                        info.rti_info[RTAX_BRD] = ifa->ifa_dstaddr;
713                        len = rt_msg2(RTM_NEWADDR, &info, NULL, w);
[39e6e65a]714                        if (w->w_req && w->w_tmem) {
[1af76095]715                                struct ifa_msghdr *ifam;
[39e6e65a]716
717                                ifam = (struct ifa_msghdr *)w->w_tmem;
718                                ifam->ifam_index = ifa->ifa_ifp->if_index;
719                                ifam->ifam_flags = ifa->ifa_flags;
720                                ifam->ifam_metric = ifa->ifa_metric;
721                                ifam->ifam_addrs = info.rti_addrs;
[36799d4]722                                error = SYSCTL_OUT(w->w_req, w->w_tmem, len);
[39e6e65a]723                                if (error)
724                                        return (error);
725                        }
726                }
[1af76095]727                info.rti_info[RTAX_IFA] = info.rti_info[RTAX_NETMASK] = info.rti_info[RTAX_BRD] = 0;
[39e6e65a]728        }
729        return (0);
730}
731
732static int
[36799d4]733sysctl_rtsock(SYSCTL_HANDLER_ARGS)
[39e6e65a]734{
735        int     *name = (int *)arg1;
736        u_int   namelen = arg2;
[1af76095]737        struct radix_node_head *rnh;
[39e6e65a]738        int     i, s, error = EINVAL;
739        u_char  af;
740        struct  walkarg w;
741
742        name ++;
743        namelen--;
744        if (req->newptr)
745                return (EPERM);
746        if (namelen != 3)
747                return (EINVAL);
748        af = name[0];
749        Bzero(&w, sizeof(w));
750        w.w_op = name[1];
751        w.w_arg = name[2];
752        w.w_req = req;
753
754        s = splnet();
755        switch (w.w_op) {
756
757        case NET_RT_DUMP:
758        case NET_RT_FLAGS:
759                for (i = 1; i <= AF_MAX; i++)
760                        if ((rnh = rt_tables[i]) && (af == 0 || af == i) &&
761                            (error = rnh->rnh_walktree(rnh,
762                                                        sysctl_dumpentry, &w)))
763                                break;
764                break;
765
766        case NET_RT_IFLIST:
767                error = sysctl_iflist(af, &w);
768        }
769        splx(s);
770        if (w.w_tmem)
771                free(w.w_tmem, M_RTABLE);
772        return (error);
773}
774
775SYSCTL_NODE(_net, PF_ROUTE, routetable, CTLFLAG_RD, sysctl_rtsock,"");
776
777/*
778 * Definitions of protocols supported in the ROUTE domain.
779 */
780
781extern struct domain routedomain;               /* or at least forward */
782
783static struct protosw routesw[] = {
784{ SOCK_RAW,     &routedomain,   0,              PR_ATOMIC|PR_ADDR,
785  0,            route_output,   raw_ctlinput,   0,
786  route_usrreq,
[300c071c]787  raw_init,     NULL,           NULL,           NULL,
788  NULL
[39e6e65a]789}
790};
791
792struct domain routedomain =
793    { PF_ROUTE, "route", route_init, 0, 0,
[300c071c]794      routesw, &routesw[sizeof(routesw)/sizeof(routesw[0])],
795      NULL, NULL, 0, 0 };
[39e6e65a]796
797DOMAIN_SET(route);
Note: See TracBrowser for help on using the repository browser.