source: rtems/cpukit/libnetworking/net/route.c @ 4c672b93

4.104.114.84.95
Last change on this file since 4c672b93 was 5821bfd, checked in by Ralf Corsepius <ralf.corsepius@…>, on 12/08/06 at 15:48:34

Cosmetics from FreeBSD.

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