source: rtems/cpukit/libnetworking/net/route.c @ b25b88e7

4.104.115
Last change on this file since b25b88e7 was b25b88e7, checked in by Ralf Corsepius <ralf.corsepius@…>, on 03/28/10 at 05:50:29

Add HAVE_CONFIG_H support to let files receive configure defines.

  • Property mode set to 100644
File size: 24.8 KB
Line 
1/*
2 * Copyright (c) 1980, 1986, 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 *
29 *      @(#)route.c     8.2 (Berkeley) 11/15/93
30 *      $Id$
31 */
32
33#ifdef HAVE_CONFIG_H
34#include "config.h"
35#endif
36
37#include "opt_mrouting.h"
38
39#include <sys/param.h>
40#include <rtems/bsd/sys/queue.h>
41#include <sys/systm.h>
42#include <sys/kernel.h>
43#include <sys/proc.h>
44#include <sys/mbuf.h>
45#include <sys/socket.h>
46#include <sys/socketvar.h>
47#include <sys/domain.h>
48#include <sys/protosw.h>
49#include <sys/ioctl.h>
50
51#include <net/if.h>
52#include <net/route.h>
53#include <net/raw_cb.h>
54
55#include <netinet/in.h>
56#include <netinet/in_var.h>
57#include <netinet/ip_mroute.h>
58
59#define SA(p) ((struct sockaddr *)(p))
60
61struct route_cb route_cb;
62static struct rtstat rtstat;
63struct radix_node_head *rt_tables[AF_MAX+1];
64
65static int      rttrash;                /* routes not in table but not freed */
66
67static void rt_maskedcopy(struct sockaddr *,
68            struct sockaddr *, struct sockaddr *);
69static void rtable_init(struct radix_node_head **);
70
71/* compare two sockaddr structures */
72#define sa_equal(a1, a2) (bcmp((a1), (a2), (a1)->sa_len) == 0)
73
74static void
75rtable_init(struct radix_node_head **table)
76{
77        struct domain *dom;
78        for (dom = domains; dom; dom = dom->dom_next)
79                if (dom->dom_rtattach)
80                        dom->dom_rtattach((void *)&table[dom->dom_family],
81                            dom->dom_rtoffset);
82}
83
84void
85route_init(void)
86{
87        rn_init();      /* initialize all zeroes, all ones, mask table */
88        rtable_init(rt_tables);
89}
90
91/*
92 * Packet routing routines.
93 */
94void
95rtalloc(struct route *ro)
96{
97        if (ro->ro_rt && ro->ro_rt->rt_ifp && (ro->ro_rt->rt_flags & RTF_UP))
98                return;                          /* XXX */
99        ro->ro_rt = rtalloc1(&ro->ro_dst, 1, 0UL);
100}
101
102void
103rtalloc_ign(struct route *ro, u_long ignore)
104{
105        if (ro->ro_rt && ro->ro_rt->rt_ifp && (ro->ro_rt->rt_flags & RTF_UP))
106                return;                          /* XXX */
107        ro->ro_rt = rtalloc1(&ro->ro_dst, 1, ignore);
108}
109
110/*
111 * Look up the route that matches the address given
112 * Or, at least try.. Create a cloned route if needed.
113 */
114struct rtentry *
115rtalloc1(struct sockaddr *dst, int report, u_long ignflags)
116{
117        struct radix_node_head *rnh = rt_tables[dst->sa_family];
118        struct rtentry *rt;
119        struct radix_node *rn;
120        struct rtentry *newrt;
121        struct rt_addrinfo info;
122        u_long nflags;
123        int  s = splnet();
124        int err = 0, msgtype = RTM_MISS;
125
126        newrt = NULL;
127        /*
128         * Look up the address in the table for that Address Family
129         */
130        if (rnh && (rn = rnh->rnh_matchaddr((caddr_t)dst, rnh)) &&
131            ((rn->rn_flags & RNF_ROOT) == 0)) {
132                /*
133                 * If we find it and it's not the root node, then
134                 * get a refernce on the rtentry associated.
135                 */
136                newrt = rt = (struct rtentry *)rn;
137                nflags = rt->rt_flags & ~ignflags;
138                if (report && (nflags & (RTF_CLONING | RTF_PRCLONING))) {
139                        /*
140                         * We are apparently adding (report = 0 in delete).
141                         * If it requires that it be cloned, do so.
142                         * (This implies it wasn't a HOST route.)
143                         */
144                        err = rtrequest(RTM_RESOLVE, dst, SA(0),
145                                              SA(0), 0, &newrt);
146                        if (err) {
147                                /*
148                                 * If the cloning didn't succeed, maybe
149                                 * what we have will do. Return that.
150                                 */
151                                newrt = rt;
152                                rt->rt_refcnt++;
153                                goto miss;
154                        }
155                        if ((rt = newrt) && (rt->rt_flags & RTF_XRESOLVE)) {
156                                /*
157                                 * If the new route specifies it be
158                                 * externally resolved, then go do that.
159                                 */
160                                msgtype = RTM_RESOLVE;
161                                goto miss;
162                        }
163                } else
164                        rt->rt_refcnt++;
165        } else {
166                /*
167                 * Either we hit the root or couldn't find any match,
168                 * Which basically means
169                 * "caint get there frm here"
170                 */
171                rtstat.rts_unreach++;
172        miss:   if (report) {
173                        /*
174                         * If required, report the failure to the supervising
175                         * Authorities.
176                         * For a delete, this is not an error. (report == 0)
177                         */
178                        bzero(&info, sizeof(info));
179                        info.rti_info[RTAX_DST] = dst;
180                        rt_missmsg(msgtype, &info, 0, err);
181                }
182        }
183        splx(s);
184        return (newrt);
185}
186
187/*
188 * Remove a reference count from an rtentry.
189 * If the count gets low enough, take it out of the routing table
190 */
191void
192rtfree(struct rtentry *rt)
193{
194        register struct radix_node_head *rnh =
195                rt_tables[rt_key(rt)->sa_family];
196        register struct ifaddr *ifa;
197
198        if (rt == 0 || rnh == 0)
199                panic("rtfree");
200        rt->rt_refcnt--;
201        if(rnh->rnh_close && rt->rt_refcnt == 0) {
202                rnh->rnh_close((struct radix_node *)rt, rnh);
203        }
204        if (rt->rt_refcnt <= 0 && (rt->rt_flags & RTF_UP) == 0) {
205                if (rt->rt_nodes->rn_flags & (RNF_ACTIVE | RNF_ROOT))
206                        panic ("rtfree 2");
207                rttrash--;
208                if (rt->rt_refcnt < 0) {
209                        printf("rtfree: %p not freed (neg refs)\n", rt);
210                        return;
211                }
212                ifa = rt->rt_ifa;
213                IFAFREE(ifa);
214                if (rt->rt_parent) {
215                        RTFREE(rt->rt_parent);
216                }
217                Free(rt_key(rt));
218                Free(rt);
219        }
220}
221
222void
223ifafree(struct ifaddr *ifa)
224{
225        if (ifa == NULL)
226                panic("ifafree");
227        if (ifa->ifa_refcnt == 0)
228                free(ifa, M_IFADDR);
229        else
230                ifa->ifa_refcnt--;
231}
232
233/*
234 * Force a routing table entry to the specified
235 * destination to go through the given gateway.
236 * Normally called as a result of a routing redirect
237 * message from the network layer.
238 *
239 * N.B.: must be called at splnet
240 *
241 */
242void
243rtredirect(struct sockaddr *dst, struct sockaddr *gateway,
244    struct sockaddr *netmask, int flags, struct sockaddr *src,
245    struct rtentry **rtp)
246{
247        struct rtentry *rt;
248        int error = 0;
249        short *stat = NULL;
250        struct rt_addrinfo info;
251        struct ifaddr *ifa;
252
253        /* verify the gateway is directly reachable */
254        if ((ifa = ifa_ifwithnet(gateway)) == NULL) {
255                error = ENETUNREACH;
256                goto out;
257        }
258        rt = rtalloc1(dst, 0, 0UL);
259        /*
260         * If the redirect isn't from our current router for this dst,
261         * it's either old or wrong.  If it redirects us to ourselves,
262         * we have a routing loop, perhaps as a result of an interface
263         * going down recently.
264         */
265#define equal(a1, a2) (bcmp((caddr_t)(a1), (caddr_t)(a2), (a1)->sa_len) == 0)
266        if (!(flags & RTF_DONE) && rt &&
267             (!equal(src, rt->rt_gateway) || rt->rt_ifa != ifa))
268                error = EINVAL;
269        else if (ifa_ifwithaddr(gateway))
270                error = EHOSTUNREACH;
271        if (error)
272                goto done;
273        /*
274         * Create a new entry if we just got back a wildcard entry
275         * or the the lookup failed.  This is necessary for hosts
276         * which use routing redirects generated by smart gateways
277         * to dynamically build the routing tables.
278         */
279        if ((rt == 0) || (rt_mask(rt) && rt_mask(rt)->sa_len < 2))
280                goto create;
281        /*
282         * Don't listen to the redirect if it's
283         * for a route to an interface.
284         */
285        if (rt->rt_flags & RTF_GATEWAY) {
286                if (((rt->rt_flags & RTF_HOST) == 0) && (flags & RTF_HOST)) {
287                        /*
288                         * Changing from route to net => route to host.
289                         * Create new route, rather than smashing route to net.
290                         */
291                create:
292                        flags |=  RTF_GATEWAY | RTF_DYNAMIC;
293                        error = rtrequest((int)RTM_ADD, dst, gateway,
294                                    netmask, flags,
295                                    (struct rtentry **)0);
296                        stat = &rtstat.rts_dynamic;
297                } else {
298                        /*
299                         * Smash the current notion of the gateway to
300                         * this destination.  Should check about netmask!!!
301                         */
302                        rt->rt_flags |= RTF_MODIFIED;
303                        flags |= RTF_MODIFIED;
304                        stat = &rtstat.rts_newgateway;
305                        rt_setgate(rt, rt_key(rt), gateway);
306                }
307        } else
308                error = EHOSTUNREACH;
309done:
310        if (rt) {
311                if (rtp && !error)
312                        *rtp = rt;
313                else
314                        rtfree(rt);
315        }
316out:
317        if (error)
318                rtstat.rts_badredirect++;
319        else if (stat != NULL)
320                (*stat)++;
321        bzero(&info, sizeof(info));
322        info.rti_info[RTAX_DST] = dst;
323        info.rti_info[RTAX_GATEWAY] = gateway;
324        info.rti_info[RTAX_NETMASK] = netmask;
325        info.rti_info[RTAX_AUTHOR] = src;
326        rt_missmsg(RTM_REDIRECT, &info, flags, error);
327}
328
329/*
330* Routing table ioctl interface.
331*/
332int
333rtioctl(int req, caddr_t data, struct proc *p)
334{
335#ifdef INET
336        /* Multicast goop, grrr... */
337#ifdef MROUTING
338        return mrt_ioctl(req, data);
339#else
340        return mrt_ioctl(req, data, p);
341#endif
342#else /* INET */
343        return ENXIO;
344#endif /* INET */
345}
346
347struct ifaddr *
348ifa_ifwithroute(int flags, struct sockaddr *dst, struct sockaddr *gateway)
349{
350        register struct ifaddr *ifa;
351        if ((flags & RTF_GATEWAY) == 0) {
352                /*
353                 * If we are adding a route to an interface,
354                 * and the interface is a pt to pt link
355                 * we should search for the destination
356                 * as our clue to the interface.  Otherwise
357                 * we can use the local address.
358                 */
359                ifa = NULL;
360                if (flags & RTF_HOST) {
361                        ifa = ifa_ifwithdstaddr(dst);
362                }
363                if (ifa == NULL)
364                        ifa = ifa_ifwithaddr(gateway);
365        } else {
366                /*
367                 * If we are adding a route to a remote net
368                 * or host, the gateway may still be on the
369                 * other end of a pt to pt link.
370                 */
371                ifa = ifa_ifwithdstaddr(gateway);
372        }
373        if (ifa == 0)
374                ifa = ifa_ifwithnet(gateway);
375        if (ifa == 0) {
376                struct rtentry *rt = rtalloc1(dst, 0, 0UL);
377                if (rt == 0)
378                        return (0);
379                rt->rt_refcnt--;
380                if ((ifa = rt->rt_ifa) == 0)
381                        return (0);
382        }
383        if (ifa->ifa_addr->sa_family != dst->sa_family) {
384                struct ifaddr *oifa = ifa;
385                ifa = ifaof_ifpforaddr(dst, ifa->ifa_ifp);
386                if (ifa == 0)
387                        ifa = oifa;
388        }
389        return (ifa);
390}
391
392#define ROUNDUP(a) (a>0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof(long))
393
394static int rt_fixdelete(struct radix_node *, void *);
395static int rt_fixchange(struct radix_node *, void *);
396
397struct rtfc_arg {
398        struct rtentry *rt0;
399        struct radix_node_head *rnh;
400};
401
402/*
403 * Do appropriate manipulations of a routing tree given
404 * all the bits of info needed
405 */
406int
407rtrequest(int req, struct sockaddr *dst, struct sockaddr *gateway,
408    struct sockaddr *netmask, int flags, struct rtentry **ret_nrt)
409{
410        int s = splnet(); int error = 0;
411        register struct rtentry *rt;
412        register struct radix_node *rn;
413        register struct radix_node_head *rnh;
414        struct ifaddr *ifa;
415        struct sockaddr *ndst;
416#define senderr(x) { error = x ; goto bad; }
417
418        /*
419         * Find the correct routing tree to use for this Address Family
420         */
421        if ((rnh = rt_tables[dst->sa_family]) == 0)
422                senderr(ESRCH);
423        /*
424         * If we are adding a host route then we don't want to put
425         * a netmask in the tree
426         */
427        if (flags & RTF_HOST)
428                netmask = 0;
429        switch (req) {
430        case RTM_DELETE:
431                /*
432                 * Remove the item from the tree and return it.
433                 * Complain if it is not there and do no more processing.
434                 */
435                if ((rn = rnh->rnh_deladdr(dst, netmask, rnh)) == 0)
436                        senderr(ESRCH);
437                if (rn->rn_flags & (RNF_ACTIVE | RNF_ROOT))
438                        panic ("rtrequest delete");
439                rt = (struct rtentry *)rn;
440
441                /*
442                 * Now search what's left of the subtree for any cloned
443                 * routes which might have been formed from this node.
444                 */
445                if ((rt->rt_flags & RTF_PRCLONING) && netmask) {
446                        rnh->rnh_walktree_from(rnh, dst, netmask,
447                                               rt_fixdelete, rt);
448                }
449
450                /*
451                 * Remove any external references we may have.
452                 * This might result in another rtentry being freed if
453                 * we held it's last reference.
454                 */
455                if (rt->rt_gwroute) {
456                        rt = rt->rt_gwroute;
457                        RTFREE(rt);
458                        (rt = (struct rtentry *)rn)->rt_gwroute = 0;
459                }
460
461                /*
462                 * NB: RTF_UP must be set during the search above,
463                 * because we might delete the last ref, causing
464                 * rt to get freed prematurely.
465                 */
466                rt->rt_flags &= ~RTF_UP;
467
468                /*
469                 * If there is llinfo or similar associated with the
470                 * route, give the interface a chance to deal with it..
471                 */
472                if ((ifa = rt->rt_ifa) && ifa->ifa_rtrequest)
473                        ifa->ifa_rtrequest(RTM_DELETE, rt, SA(0));
474                rttrash++;
475                /*
476                 * If the caller wants it, then it can have it, but it's up to it
477                 * to free the rtentry as we won't be doing it.
478                 */
479                if (ret_nrt)
480                        *ret_nrt = rt;
481                else if (rt->rt_refcnt <= 0) {
482                        rt->rt_refcnt++; /* make a 1->0 transition */
483                        rtfree(rt);
484                }
485                break;
486
487        case RTM_RESOLVE:
488                if (ret_nrt == 0 || (rt = *ret_nrt) == 0)
489                        senderr(EINVAL);
490                ifa = rt->rt_ifa;
491                flags = rt->rt_flags &
492                    ~(RTF_CLONING | RTF_PRCLONING | RTF_STATIC);
493                flags |= RTF_WASCLONED;
494                gateway = rt->rt_gateway;
495                if ((netmask = rt->rt_genmask) == 0)
496                        flags |= RTF_HOST;
497                goto makeroute;
498
499        case RTM_ADD:
500                if ((flags & RTF_GATEWAY) && !gateway)
501                        panic("rtrequest: GATEWAY but no gateway");
502
503                if ((ifa = ifa_ifwithroute(flags, dst, gateway)) == 0)
504                        senderr(ENETUNREACH);
505
506        makeroute:
507                R_Malloc(rt, struct rtentry *, sizeof(*rt));
508                if (rt == 0)
509                        senderr(ENOBUFS);
510                Bzero(rt, sizeof(*rt));
511                rt->rt_flags = RTF_UP | flags;
512                if ((error = rt_setgate(rt, dst, gateway))) {
513                        Free(rt);
514                        senderr(error);
515                }
516                ndst = rt_key(rt);
517                if (netmask) {
518                        rt_maskedcopy(dst, ndst, netmask);
519                } else
520                        Bcopy(dst, ndst, dst->sa_len);
521
522                /*
523                 * This moved from below so that rnh->rnh_addaddr() can
524                 * examine the ifa and ifp if it so desires.
525                 */
526                ifa->ifa_refcnt++;
527                rt->rt_ifa = ifa;
528                rt->rt_ifp = ifa->ifa_ifp;
529
530                rn = rnh->rnh_addaddr((caddr_t)ndst, (caddr_t)netmask,
531                                        rnh, rt->rt_nodes);
532                if (rn == 0) {
533                        struct rtentry *rt2;
534                        /*
535                         * Uh-oh, we already have one of these in the tree.
536                         * We do a special hack: if the route that's already
537                         * there was generated by the protocol-cloning
538                         * mechanism, then we just blow it away and retry
539                         * the insertion of the new one.
540                         */
541                        rt2 = rtalloc1(dst, 0, RTF_PRCLONING);
542                        if (rt2 && rt2->rt_parent) {
543                                rtrequest(RTM_DELETE,
544                                          (struct sockaddr *)rt_key(rt2),
545                                          rt2->rt_gateway,
546                                          rt_mask(rt2), rt2->rt_flags, 0);
547                                RTFREE(rt2);
548                                rn = rnh->rnh_addaddr((caddr_t)ndst,
549                                                      (caddr_t)netmask,
550                                                      rnh, rt->rt_nodes);
551                        } else if (rt2) {
552                                RTFREE(rt2);
553                        }
554                }
555
556                if (rn == 0) {
557                        if (rt->rt_gwroute)
558                                rtfree(rt->rt_gwroute);
559                        if (rt->rt_ifa) {
560                                IFAFREE(rt->rt_ifa);
561                        }
562                        Free(rt_key(rt));
563                        Free(rt);
564                        senderr(EEXIST);
565                }
566                rt->rt_parent = 0;
567
568                if (req == RTM_RESOLVE) {
569                        rt->rt_rmx = (*ret_nrt)->rt_rmx; /* copy metrics */
570                        if ((*ret_nrt)->rt_flags & RTF_PRCLONING) {
571                                rt->rt_parent = (*ret_nrt);
572                                (*ret_nrt)->rt_refcnt++;
573                        }
574                }
575                if (ifa->ifa_rtrequest)
576                        ifa->ifa_rtrequest(req, rt, SA(ret_nrt ? *ret_nrt : 0));
577                /*
578                 * We repeat the same procedure from rt_setgate() here because
579                 * it doesn't fire when we call it there because the node
580                 * hasn't been added to the tree yet.
581                 */
582                if (!(rt->rt_flags & RTF_HOST) && rt_mask(rt) != 0) {
583                        struct rtfc_arg arg;
584                        arg.rnh = rnh;
585                        arg.rt0 = rt;
586                        rnh->rnh_walktree_from(rnh, rt_key(rt), rt_mask(rt),
587                                               rt_fixchange, &arg);
588                }
589
590                if (ret_nrt) {
591                        *ret_nrt = rt;
592                        rt->rt_refcnt++;
593                }
594                break;
595        }
596bad:
597        splx(s);
598        return (error);
599}
600
601/*
602 * Called from rtrequest(RTM_DELETE, ...) to fix up the route's ``family''
603 * (i.e., the routes related to it by the operation of cloning).  This
604 * routine is iterated over all potential former-child-routes by way of
605 * rnh->rnh_walktree_from() above, and those that actually are children of
606 * the late parent (passed in as VP here) are themselves deleted.
607 */
608static int
609rt_fixdelete(struct radix_node *rn, void *vp)
610{
611        struct rtentry *rt = (struct rtentry *)rn;
612        struct rtentry *rt0 = vp;
613
614        if (rt->rt_parent == rt0 && !(rt->rt_flags & RTF_PINNED)) {
615                return rtrequest(RTM_DELETE, rt_key(rt),
616                                 (struct sockaddr *)0, rt_mask(rt),
617                                 rt->rt_flags, (struct rtentry **)0);
618        }
619        return 0;
620}
621
622/*
623 * This routine is called from rt_setgate() to do the analogous thing for
624 * adds and changes.  There is the added complication in this case of a
625 * middle insert; i.e., insertion of a new network route between an older
626 * network route and (cloned) host routes.  For this reason, a simple check
627 * of rt->rt_parent is insufficient; each candidate route must be tested
628 * against the (mask, value) of the new route (passed as before in vp)
629 * to see if the new route matches it.  Unfortunately, this has the obnoxious
630 * property of also triggering for insertion /above/ a pre-existing network
631 * route and clones.  Sigh.  This may be fixed some day.
632 *
633 * XXX - it may be possible to do fixdelete() for changes and reserve this
634 * routine just for adds.  I'm not sure why I thought it was necessary to do
635 * changes this way.
636 */
637#ifdef DEBUG
638int rtfcdebug = 0;
639#endif
640
641static int
642rt_fixchange(struct radix_node *rn, void *vp)
643{
644        struct rtentry *rt = (struct rtentry *)rn;
645        struct rtfc_arg *ap = vp;
646        struct rtentry *rt0 = ap->rt0;
647        struct radix_node_head *rnh = ap->rnh;
648        u_char *xk1, *xm1, *xk2;
649        int i, len;
650
651#ifdef DEBUG
652        if (rtfcdebug)
653                printf("rt_fixchange: rt %p, rt0 %p\n", rt, rt0);
654#endif
655
656        if (!rt->rt_parent || (rt->rt_flags & RTF_PINNED)) {
657#ifdef DEBUG
658                if(rtfcdebug) printf("no parent or pinned\n");
659#endif
660                return 0;
661        }
662
663        if (rt->rt_parent == rt0) {
664#ifdef DEBUG
665                if(rtfcdebug) printf("parent match\n");
666#endif
667                return rtrequest(RTM_DELETE, rt_key(rt),
668                                 (struct sockaddr *)0, rt_mask(rt),
669                                 rt->rt_flags, (struct rtentry **)0);
670        }
671
672        /*
673         * There probably is a function somewhere which does this...
674         * if not, there should be.
675         */
676        len = imin(((struct sockaddr *)rt_key(rt0))->sa_len,
677                   ((struct sockaddr *)rt_key(rt))->sa_len);
678
679        xk1 = (u_char *)rt_key(rt0);
680        xm1 = (u_char *)rt_mask(rt0);
681        xk2 = (u_char *)rt_key(rt);
682
683        for (i = rnh->rnh_treetop->rn_offset; i < len; i++) {
684                if ((xk2[i] & xm1[i]) != xk1[i]) {
685#ifdef DEBUG
686                        if(rtfcdebug) printf("no match\n");
687#endif
688                        return 0;
689                }
690        }
691
692        /*
693         * OK, this node is a clone, and matches the node currently being
694         * changed/added under the node's mask.  So, get rid of it.
695         */
696#ifdef DEBUG
697        if(rtfcdebug) printf("deleting\n");
698#endif
699        return rtrequest(RTM_DELETE, rt_key(rt), (struct sockaddr *)0,
700                         rt_mask(rt), rt->rt_flags, (struct rtentry **)0);
701}
702
703int
704rt_setgate(struct rtentry *rt0, struct sockaddr *dst, struct sockaddr *gate)
705{
706        caddr_t new, old;
707        int dlen = ROUNDUP(dst->sa_len), glen = ROUNDUP(gate->sa_len);
708        register struct rtentry *rt = rt0;
709        struct radix_node_head *rnh = rt_tables[dst->sa_family];
710
711        /*
712         * A host route with the destination equal to the gateway
713         * will interfere with keeping LLINFO in the routing
714         * table, so disallow it.
715         */
716        if (((rt0->rt_flags & (RTF_HOST|RTF_GATEWAY|RTF_LLINFO)) ==
717                                        (RTF_HOST|RTF_GATEWAY)) &&
718            (dst->sa_len == gate->sa_len) &&
719            (bcmp(dst, gate, dst->sa_len) == 0)) {
720                /*
721                 * The route might already exist if this is an RTM_CHANGE
722                 * or a routing redirect, so try to delete it.
723                 */
724                if (rt_key(rt0))
725                        rtrequest(RTM_DELETE, (struct sockaddr *)rt_key(rt0),
726                            rt0->rt_gateway, rt_mask(rt0), rt0->rt_flags, 0);
727                return EADDRNOTAVAIL;
728        }
729
730        if (rt->rt_gateway == 0 || glen > ROUNDUP(rt->rt_gateway->sa_len)) {
731                old = (caddr_t)rt_key(rt);
732                R_Malloc(new, caddr_t, dlen + glen);
733                if (new == 0)
734                        return ENOBUFS;
735                rt->rt_nodes->rn_key = new;
736        } else {
737                new = rt->rt_nodes->rn_key;
738                old = 0;
739        }
740        Bcopy(gate, (rt->rt_gateway = (struct sockaddr *)(new + dlen)), glen);
741        if (old) {
742                Bcopy(dst, new, dlen);
743                Free(old);
744        }
745        if (rt->rt_gwroute) {
746                rt = rt->rt_gwroute; RTFREE(rt);
747                rt = rt0; rt->rt_gwroute = 0;
748        }
749        /*
750         * Cloning loop avoidance:
751         * In the presence of protocol-cloning and bad configuration,
752         * it is possible to get stuck in bottomless mutual recursion
753         * (rtrequest rt_setgate rtalloc1).  We avoid this by not allowing
754         * protocol-cloning to operate for gateways (which is probably the
755         * correct choice anyway), and avoid the resulting reference loops
756         * by disallowing any route to run through itself as a gateway.
757         * This is obviuosly mandatory when we get rt->rt_output().
758         */
759        if (rt->rt_flags & RTF_GATEWAY) {
760                rt->rt_gwroute = rtalloc1(gate, 1, RTF_PRCLONING);
761                if (rt->rt_gwroute == rt) {
762                        RTFREE(rt->rt_gwroute);
763                        rt->rt_gwroute = 0;
764                        return EDQUOT; /* failure */
765                }
766        }
767
768        /*
769         * This isn't going to do anything useful for host routes, so
770         * don't bother.  Also make sure we have a reasonable mask
771         * (we don't yet have one during adds).
772         */
773        if (!(rt->rt_flags & RTF_HOST) && rt_mask(rt) != 0) {
774                struct rtfc_arg arg;
775                arg.rnh = rnh;
776                arg.rt0 = rt;
777                rnh->rnh_walktree_from(rnh, rt_key(rt), rt_mask(rt),
778                                       rt_fixchange, &arg);
779        }
780
781        return 0;
782}
783
784static void
785rt_maskedcopy(struct sockaddr *src, struct sockaddr *dst,
786    struct sockaddr *netmask)
787{
788        register u_char *cp1 = (u_char *)src;
789        register u_char *cp2 = (u_char *)dst;
790        register u_char *cp3 = (u_char *)netmask;
791        u_char *cplim = cp2 + *cp3;
792        u_char *cplim2 = cp2 + *cp1;
793
794        *cp2++ = *cp1++; *cp2++ = *cp1++; /* copies sa_len & sa_family */
795        cp3 += 2;
796        if (cplim > cplim2)
797                cplim = cplim2;
798        while (cp2 < cplim)
799                *cp2++ = *cp1++ & *cp3++;
800        if (cp2 < cplim2)
801                bzero(cp2, (unsigned)(cplim2 - cp2));
802}
803
804/*
805 * Set up a routing table entry, normally
806 * for an interface.
807 */
808int
809rtinit(struct ifaddr *ifa, int cmd, int flags)
810{
811        register struct rtentry *rt;
812        register struct sockaddr *dst;
813        register struct sockaddr *deldst;
814        struct mbuf *m = 0;
815        struct rtentry *nrt = 0;
816        int error;
817
818        dst = flags & RTF_HOST ? ifa->ifa_dstaddr : ifa->ifa_addr;
819        /*
820         * If it's a delete, check that if it exists, it's on the correct
821         * interface or we might scrub a route to another ifa which would
822         * be confusing at best and possibly worse.
823         */
824        if (cmd == RTM_DELETE) {
825                /*
826                 * It's a delete, so it should already exist..
827                 * If it's a net, mask off the host bits
828                 * (Assuming we have a mask)
829                 */
830                if ((flags & RTF_HOST) == 0 && ifa->ifa_netmask) {
831                        m = m_get(M_WAIT, MT_SONAME);
832                        deldst = mtod(m, struct sockaddr *);
833                        rt_maskedcopy(dst, deldst, ifa->ifa_netmask);
834                        dst = deldst;
835                }
836                /*
837                 * Get an rtentry that is in the routing tree and
838                 * contains the correct info. (if this fails we can't get there).
839                 * We set "report" to FALSE so that if it doesn't exist,
840                 * it doesn't report an error or clone a route, etc. etc.
841                 */
842                rt = rtalloc1(dst, 0, 0UL);
843                if (rt) {
844                        /*
845                         * Ok so we found the rtentry. it has an extra reference
846                         * for us at this stage. we won't need that so
847                         * lop that off now.
848                         */
849                        rt->rt_refcnt--;
850                        if (rt->rt_ifa != ifa) {
851                                /*
852                                 * If the interface in the rtentry doesn't match
853                                 * the interface we are using, then we don't
854                                 * want to delete it, so return an error.
855                                 * This seems to be the only point of
856                                 * this whole RTM_DELETE clause.
857                                 */
858                                if (m)
859                                        (void) m_free(m);
860                                return (flags & RTF_HOST ? EHOSTUNREACH
861                                                        : ENETUNREACH);
862                        }
863                }
864                /* XXX */
865#if 0
866                else {
867                        /*
868                         * One would think that as we are deleting, and we know
869                         * it doesn't exist, we could just return at this point
870                         * with an "ELSE" clause, but apparently not..
871                         */
872                        return (flags & RTF_HOST ? EHOSTUNREACH
873                                                        : ENETUNREACH);
874                }
875#endif
876        }
877        /*
878         * Do the actual request
879         */
880        error = rtrequest(cmd, dst, ifa->ifa_addr, ifa->ifa_netmask,
881                        flags | ifa->ifa_flags, &nrt);
882        if (m)
883                (void) m_free(m);
884        /*
885         * If we are deleting, and we found an entry, then
886         * it's been removed from the tree.. now throw it away.
887         */
888        if (cmd == RTM_DELETE && error == 0 && (rt = nrt)) {
889                /*
890                 * notify any listenning routing agents of the change
891                 */
892                rt_newaddrmsg(cmd, ifa, error, nrt);
893                if (rt->rt_refcnt <= 0) {
894                        rt->rt_refcnt++; /* need a 1->0 transition to free */
895                        rtfree(rt);
896                }
897        }
898
899        /*
900         * We are adding, and we have a returned routing entry.
901         * We need to sanity check the result.
902         */
903        if (cmd == RTM_ADD && error == 0 && (rt = nrt)) {
904                /*
905                 * We just wanted to add it.. we don't actually need a reference
906                 */
907                rt->rt_refcnt--;
908                /*
909                 * If it came back with an unexpected interface, then it must
910                 * have already existed or something. (XXX)
911                 */
912                if (rt->rt_ifa != ifa) {
913                        printf("rtinit: wrong ifa (%p) was (%p)\n", ifa,
914                                rt->rt_ifa);
915                        /*
916                         * Ask that the route we got back be removed
917                         * from the routing tables as we are trying
918                         * to supersede it.
919                         */
920                        if (rt->rt_ifa->ifa_rtrequest)
921                            rt->rt_ifa->ifa_rtrequest(RTM_DELETE, rt, SA(0));
922                        /*
923                         * Remove the referenve to the it's ifaddr.
924                         */
925                        IFAFREE(rt->rt_ifa);
926                        /*
927                         * And substitute in references to the ifaddr
928                         * we are adding.
929                         */
930                        rt->rt_ifa = ifa;
931                        rt->rt_ifp = ifa->ifa_ifp;
932                        ifa->ifa_refcnt++;
933                        /*
934                         * Now add it to the routing table
935                         * XXX could we have just left it?
936                         * as it might have been in the right place..
937                         */
938                        if (ifa->ifa_rtrequest)
939                            ifa->ifa_rtrequest(RTM_ADD, rt, SA(0));
940                }
941                /*
942                 * notify any listenning routing agents of the change
943                 */
944                rt_newaddrmsg(cmd, ifa, error, nrt);
945        }
946        return (error);
947}
Note: See TracBrowser for help on using the repository browser.