The Design and Implementation of the FreeBSD Operating System, Second Edition
Now available: The Design and Implementation of the FreeBSD Operating System (Second Edition)


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/net/route.c

Version: -  FREEBSD  -  FREEBSD-13-STABLE  -  FREEBSD-13-0  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  l41  -  OPENBSD  -  linux-2.6  -  MK84  -  PLAN9  -  xnu-8792 
SearchContext: -  none  -  3  -  10 

    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.3.1.1 (Berkeley) 2/23/95
   30  * $FreeBSD: releng/8.0/sys/net/route.c 197869 2009-10-08 20:58:09Z bz $
   31  */
   32 /************************************************************************
   33  * Note: In this file a 'fib' is a "forwarding information base"        *
   34  * Which is the new name for an in kernel routing (next hop) table.     *
   35  ***********************************************************************/
   36 
   37 #include "opt_inet.h"
   38 #include "opt_route.h"
   39 #include "opt_mrouting.h"
   40 #include "opt_mpath.h"
   41 
   42 #include <sys/param.h>
   43 #include <sys/systm.h>
   44 #include <sys/syslog.h>
   45 #include <sys/malloc.h>
   46 #include <sys/mbuf.h>
   47 #include <sys/socket.h>
   48 #include <sys/sysctl.h>
   49 #include <sys/syslog.h>
   50 #include <sys/sysproto.h>
   51 #include <sys/proc.h>
   52 #include <sys/domain.h>
   53 #include <sys/kernel.h>
   54 
   55 #include <net/if.h>
   56 #include <net/if_dl.h>
   57 #include <net/route.h>
   58 #include <net/vnet.h>
   59 #include <net/flowtable.h>
   60 
   61 #ifdef RADIX_MPATH
   62 #include <net/radix_mpath.h>
   63 #endif
   64 
   65 #include <netinet/in.h>
   66 #include <netinet/ip_mroute.h>
   67 
   68 #include <vm/uma.h>
   69 
   70 u_int rt_numfibs = RT_NUMFIBS;
   71 SYSCTL_INT(_net, OID_AUTO, fibs, CTLFLAG_RD, &rt_numfibs, 0, "");
   72 /*
   73  * Allow the boot code to allow LESS than RT_MAXFIBS to be used.
   74  * We can't do more because storage is statically allocated for now.
   75  * (for compatibility reasons.. this will change).
   76  */
   77 TUNABLE_INT("net.fibs", &rt_numfibs);
   78 
   79 /*
   80  * By default add routes to all fibs for new interfaces.
   81  * Once this is set to 0 then only allocate routes on interface
   82  * changes for the FIB of the caller when adding a new set of addresses
   83  * to an interface.  XXX this is a shotgun aproach to a problem that needs
   84  * a more fine grained solution.. that will come.
   85  */
   86 u_int rt_add_addr_allfibs = 1;
   87 SYSCTL_INT(_net, OID_AUTO, add_addr_allfibs, CTLFLAG_RW,
   88     &rt_add_addr_allfibs, 0, "");
   89 TUNABLE_INT("net.add_addr_allfibs", &rt_add_addr_allfibs);
   90 
   91 VNET_DEFINE(struct radix_node_head *, rt_tables);
   92 static VNET_DEFINE(uma_zone_t, rtzone);         /* Routing table UMA zone. */
   93 VNET_DEFINE(int, rttrash);              /* routes not in table but not freed */
   94 VNET_DEFINE(struct rtstat, rtstat);
   95 
   96 #define V_rt_tables     VNET(rt_tables)
   97 #define V_rtzone        VNET(rtzone)
   98 #define V_rttrash       VNET(rttrash)
   99 #define V_rtstat        VNET(rtstat)
  100 
  101 static void rt_maskedcopy(struct sockaddr *,
  102             struct sockaddr *, struct sockaddr *);
  103 
  104 /* compare two sockaddr structures */
  105 #define sa_equal(a1, a2) (bcmp((a1), (a2), (a1)->sa_len) == 0)
  106 
  107 /*
  108  * Convert a 'struct radix_node *' to a 'struct rtentry *'.
  109  * The operation can be done safely (in this code) because a
  110  * 'struct rtentry' starts with two 'struct radix_node''s, the first
  111  * one representing leaf nodes in the routing tree, which is
  112  * what the code in radix.c passes us as a 'struct radix_node'.
  113  *
  114  * But because there are a lot of assumptions in this conversion,
  115  * do not cast explicitly, but always use the macro below.
  116  */
  117 #define RNTORT(p)       ((struct rtentry *)(p))
  118 
  119 #if 0
  120 /* default fib for tunnels to use */
  121 u_int tunnel_fib = 0;
  122 SYSCTL_INT(_net, OID_AUTO, tunnelfib, CTLFLAG_RD, &tunnel_fib, 0, "");
  123 #endif
  124 
  125 /*
  126  * handler for net.my_fibnum
  127  */
  128 static int
  129 sysctl_my_fibnum(SYSCTL_HANDLER_ARGS)
  130 {
  131         int fibnum;
  132         int error;
  133  
  134         fibnum = curthread->td_proc->p_fibnum;
  135         error = sysctl_handle_int(oidp, &fibnum, 0, req);
  136         return (error);
  137 }
  138 
  139 SYSCTL_PROC(_net, OID_AUTO, my_fibnum, CTLTYPE_INT|CTLFLAG_RD,
  140             NULL, 0, &sysctl_my_fibnum, "I", "default FIB of caller");
  141 
  142 static __inline struct radix_node_head **
  143 rt_tables_get_rnh_ptr(int table, int fam)
  144 {
  145         struct radix_node_head **rnh;
  146 
  147         KASSERT(table >= 0 && table < rt_numfibs, ("%s: table out of bounds.",
  148             __func__));
  149         KASSERT(fam >= 0 && fam < (AF_MAX+1), ("%s: fam out of bounds.",
  150             __func__));
  151 
  152         /* rnh is [fib=0][af=0]. */
  153         rnh = (struct radix_node_head **)V_rt_tables;
  154         /* Get the offset to the requested table and fam. */
  155         rnh += table * (AF_MAX+1) + fam;
  156 
  157         return (rnh);
  158 }
  159 
  160 struct radix_node_head *
  161 rt_tables_get_rnh(int table, int fam)
  162 {
  163 
  164         return (*rt_tables_get_rnh_ptr(table, fam));
  165 }
  166 
  167 /*
  168  * route initialization must occur before ip6_init2(), which happenas at
  169  * SI_ORDER_MIDDLE.
  170  */
  171 static void
  172 route_init(void)
  173 {
  174 
  175         /* whack the tunable ints into  line. */
  176         if (rt_numfibs > RT_MAXFIBS)
  177                 rt_numfibs = RT_MAXFIBS;
  178         if (rt_numfibs == 0)
  179                 rt_numfibs = 1;
  180         rn_init();      /* initialize all zeroes, all ones, mask table */
  181 }
  182 SYSINIT(route_init, SI_SUB_PROTO_DOMAIN, SI_ORDER_THIRD, route_init, 0);
  183 
  184 static void
  185 vnet_route_init(const void *unused __unused)
  186 {
  187         struct domain *dom;
  188         struct radix_node_head **rnh;
  189         int table;
  190         int fam;
  191 
  192         V_rt_tables = malloc(rt_numfibs * (AF_MAX+1) *
  193             sizeof(struct radix_node_head *), M_RTABLE, M_WAITOK|M_ZERO);
  194 
  195         V_rtzone = uma_zcreate("rtentry", sizeof(struct rtentry), NULL, NULL,
  196             NULL, NULL, UMA_ALIGN_PTR, 0);
  197         for (dom = domains; dom; dom = dom->dom_next) {
  198                 if (dom->dom_rtattach)  {
  199                         for  (table = 0; table < rt_numfibs; table++) {
  200                                 if ( (fam = dom->dom_family) == AF_INET ||
  201                                     table == 0) {
  202                                         /* for now only AF_INET has > 1 table */
  203                                         /* XXX MRT 
  204                                          * rtattach will be also called
  205                                          * from vfs_export.c but the
  206                                          * offset will be 0
  207                                          * (only for AF_INET and AF_INET6
  208                                          * which don't need it anyhow)
  209                                          */
  210                                         rnh = rt_tables_get_rnh_ptr(table, fam);
  211                                         if (rnh == NULL)
  212                                                 panic("%s: rnh NULL", __func__);
  213                                         dom->dom_rtattach((void **)rnh,
  214                                             dom->dom_rtoffset);
  215                                 } else {
  216                                         break;
  217                                 }
  218                         }
  219                 }
  220         }
  221 }
  222 VNET_SYSINIT(vnet_route_init, SI_SUB_PROTO_DOMAIN, SI_ORDER_FOURTH,
  223     vnet_route_init, 0);
  224 
  225 #ifdef VIMAGE
  226 static void
  227 vnet_route_uninit(const void *unused __unused)
  228 {
  229         int table;
  230         int fam;
  231         struct domain *dom;
  232         struct radix_node_head **rnh;
  233 
  234         for (dom = domains; dom; dom = dom->dom_next) {
  235                 if (dom->dom_rtdetach) {
  236                         for (table = 0; table < rt_numfibs; table++) {
  237                                 if ( (fam = dom->dom_family) == AF_INET ||
  238                                     table == 0) {
  239                                         /* For now only AF_INET has > 1 tbl. */
  240                                         rnh = rt_tables_get_rnh_ptr(table, fam);
  241                                         if (rnh == NULL)
  242                                                 panic("%s: rnh NULL", __func__);
  243                                         dom->dom_rtdetach((void **)rnh,
  244                                             dom->dom_rtoffset);
  245                                 } else {
  246                                         break;
  247                                 }
  248                         }
  249                 }
  250         }
  251 }
  252 VNET_SYSUNINIT(vnet_route_uninit, SI_SUB_PROTO_DOMAIN, SI_ORDER_THIRD,
  253     vnet_route_uninit, 0);
  254 #endif
  255 
  256 #ifndef _SYS_SYSPROTO_H_
  257 struct setfib_args {
  258         int     fibnum;
  259 };
  260 #endif
  261 int
  262 setfib(struct thread *td, struct setfib_args *uap)
  263 {
  264         if (uap->fibnum < 0 || uap->fibnum >= rt_numfibs)
  265                 return EINVAL;
  266         td->td_proc->p_fibnum = uap->fibnum;
  267         return (0);
  268 }
  269 
  270 /*
  271  * Packet routing routines.
  272  */
  273 void
  274 rtalloc(struct route *ro)
  275 {
  276         rtalloc_ign_fib(ro, 0UL, 0);
  277 }
  278 
  279 void
  280 rtalloc_fib(struct route *ro, u_int fibnum)
  281 {
  282         rtalloc_ign_fib(ro, 0UL, fibnum);
  283 }
  284 
  285 void
  286 rtalloc_ign(struct route *ro, u_long ignore)
  287 {
  288         struct rtentry *rt;
  289 
  290         if ((rt = ro->ro_rt) != NULL) {
  291                 if (rt->rt_ifp != NULL && rt->rt_flags & RTF_UP)
  292                         return;
  293                 RTFREE(rt);
  294                 ro->ro_rt = NULL;
  295         }
  296         ro->ro_rt = rtalloc1_fib(&ro->ro_dst, 1, ignore, 0);
  297         if (ro->ro_rt)
  298                 RT_UNLOCK(ro->ro_rt);
  299 }
  300 
  301 void
  302 rtalloc_ign_fib(struct route *ro, u_long ignore, u_int fibnum)
  303 {
  304         struct rtentry *rt;
  305 
  306         if ((rt = ro->ro_rt) != NULL) {
  307                 if (rt->rt_ifp != NULL && rt->rt_flags & RTF_UP)
  308                         return;
  309                 RTFREE(rt);
  310                 ro->ro_rt = NULL;
  311         }
  312         ro->ro_rt = rtalloc1_fib(&ro->ro_dst, 1, ignore, fibnum);
  313         if (ro->ro_rt)
  314                 RT_UNLOCK(ro->ro_rt);
  315 }
  316 
  317 /*
  318  * Look up the route that matches the address given
  319  * Or, at least try.. Create a cloned route if needed.
  320  *
  321  * The returned route, if any, is locked.
  322  */
  323 struct rtentry *
  324 rtalloc1(struct sockaddr *dst, int report, u_long ignflags)
  325 {
  326         return (rtalloc1_fib(dst, report, ignflags, 0));
  327 }
  328 
  329 struct rtentry *
  330 rtalloc1_fib(struct sockaddr *dst, int report, u_long ignflags,
  331                     u_int fibnum)
  332 {
  333         struct radix_node_head *rnh;
  334         struct rtentry *rt;
  335         struct radix_node *rn;
  336         struct rtentry *newrt;
  337         struct rt_addrinfo info;
  338         int err = 0, msgtype = RTM_MISS;
  339         int needlock;
  340 
  341         KASSERT((fibnum < rt_numfibs), ("rtalloc1_fib: bad fibnum"));
  342         if (dst->sa_family != AF_INET)  /* Only INET supports > 1 fib now */
  343                 fibnum = 0;
  344         rnh = rt_tables_get_rnh(fibnum, dst->sa_family);
  345         newrt = NULL;
  346         /*
  347          * Look up the address in the table for that Address Family
  348          */
  349         if (rnh == NULL) {
  350                 V_rtstat.rts_unreach++;
  351                 goto miss;
  352         }
  353         needlock = !(ignflags & RTF_RNH_LOCKED);
  354         if (needlock)
  355                 RADIX_NODE_HEAD_RLOCK(rnh);
  356 #ifdef INVARIANTS       
  357         else
  358                 RADIX_NODE_HEAD_LOCK_ASSERT(rnh);
  359 #endif
  360         rn = rnh->rnh_matchaddr(dst, rnh);
  361         if (rn && ((rn->rn_flags & RNF_ROOT) == 0)) {
  362                 newrt = rt = RNTORT(rn);
  363                 RT_LOCK(newrt);
  364                 RT_ADDREF(newrt);
  365                 if (needlock)
  366                         RADIX_NODE_HEAD_RUNLOCK(rnh);
  367                 goto done;
  368 
  369         } else if (needlock)
  370                 RADIX_NODE_HEAD_RUNLOCK(rnh);
  371         
  372         /*
  373          * Either we hit the root or couldn't find any match,
  374          * Which basically means
  375          * "caint get there frm here"
  376          */
  377         V_rtstat.rts_unreach++;
  378 miss:
  379         if (report) {
  380                 /*
  381                  * If required, report the failure to the supervising
  382                  * Authorities.
  383                  * For a delete, this is not an error. (report == 0)
  384                  */
  385                 bzero(&info, sizeof(info));
  386                 info.rti_info[RTAX_DST] = dst;
  387                 rt_missmsg(msgtype, &info, 0, err);
  388         }       
  389 done:
  390         if (newrt)
  391                 RT_LOCK_ASSERT(newrt);
  392         return (newrt);
  393 }
  394 
  395 /*
  396  * Remove a reference count from an rtentry.
  397  * If the count gets low enough, take it out of the routing table
  398  */
  399 void
  400 rtfree(struct rtentry *rt)
  401 {
  402         struct radix_node_head *rnh;
  403 
  404         KASSERT(rt != NULL,("%s: NULL rt", __func__));
  405         rnh = rt_tables_get_rnh(rt->rt_fibnum, rt_key(rt)->sa_family);
  406         KASSERT(rnh != NULL,("%s: NULL rnh", __func__));
  407 
  408         RT_LOCK_ASSERT(rt);
  409 
  410         /*
  411          * The callers should use RTFREE_LOCKED() or RTFREE(), so
  412          * we should come here exactly with the last reference.
  413          */
  414         RT_REMREF(rt);
  415         if (rt->rt_refcnt > 0) {
  416                 log(LOG_DEBUG, "%s: %p has %d refs\n", __func__, rt, rt->rt_refcnt);
  417                 goto done;
  418         }
  419 
  420         /*
  421          * On last reference give the "close method" a chance
  422          * to cleanup private state.  This also permits (for
  423          * IPv4 and IPv6) a chance to decide if the routing table
  424          * entry should be purged immediately or at a later time.
  425          * When an immediate purge is to happen the close routine
  426          * typically calls rtexpunge which clears the RTF_UP flag
  427          * on the entry so that the code below reclaims the storage.
  428          */
  429         if (rt->rt_refcnt == 0 && rnh->rnh_close)
  430                 rnh->rnh_close((struct radix_node *)rt, rnh);
  431 
  432         /*
  433          * If we are no longer "up" (and ref == 0)
  434          * then we can free the resources associated
  435          * with the route.
  436          */
  437         if ((rt->rt_flags & RTF_UP) == 0) {
  438                 if (rt->rt_nodes->rn_flags & (RNF_ACTIVE | RNF_ROOT))
  439                         panic("rtfree 2");
  440                 /*
  441                  * the rtentry must have been removed from the routing table
  442                  * so it is represented in rttrash.. remove that now.
  443                  */
  444                 V_rttrash--;
  445 #ifdef  DIAGNOSTIC
  446                 if (rt->rt_refcnt < 0) {
  447                         printf("rtfree: %p not freed (neg refs)\n", rt);
  448                         goto done;
  449                 }
  450 #endif
  451                 /*
  452                  * release references on items we hold them on..
  453                  * e.g other routes and ifaddrs.
  454                  */
  455                 if (rt->rt_ifa)
  456                         ifa_free(rt->rt_ifa);
  457                 /*
  458                  * The key is separatly alloc'd so free it (see rt_setgate()).
  459                  * This also frees the gateway, as they are always malloc'd
  460                  * together.
  461                  */
  462                 Free(rt_key(rt));
  463 
  464                 /*
  465                  * and the rtentry itself of course
  466                  */
  467                 RT_LOCK_DESTROY(rt);
  468                 uma_zfree(V_rtzone, rt);
  469                 return;
  470         }
  471 done:
  472         RT_UNLOCK(rt);
  473 }
  474 
  475 
  476 /*
  477  * Force a routing table entry to the specified
  478  * destination to go through the given gateway.
  479  * Normally called as a result of a routing redirect
  480  * message from the network layer.
  481  */
  482 void
  483 rtredirect(struct sockaddr *dst,
  484         struct sockaddr *gateway,
  485         struct sockaddr *netmask,
  486         int flags,
  487         struct sockaddr *src)
  488 {
  489         rtredirect_fib(dst, gateway, netmask, flags, src, 0);
  490 }
  491 
  492 void
  493 rtredirect_fib(struct sockaddr *dst,
  494         struct sockaddr *gateway,
  495         struct sockaddr *netmask,
  496         int flags,
  497         struct sockaddr *src,
  498         u_int fibnum)
  499 {
  500         struct rtentry *rt, *rt0 = NULL;
  501         int error = 0;
  502         short *stat = NULL;
  503         struct rt_addrinfo info;
  504         struct ifaddr *ifa;
  505         struct radix_node_head *rnh;
  506 
  507         ifa = NULL;
  508         rnh = rt_tables_get_rnh(fibnum, dst->sa_family);
  509         if (rnh == NULL) {
  510                 error = EAFNOSUPPORT;
  511                 goto out;
  512         }
  513 
  514         /* verify the gateway is directly reachable */
  515         if ((ifa = ifa_ifwithnet(gateway)) == NULL) {
  516                 error = ENETUNREACH;
  517                 goto out;
  518         }
  519         rt = rtalloc1_fib(dst, 0, 0UL, fibnum); /* NB: rt is locked */
  520         /*
  521          * If the redirect isn't from our current router for this dst,
  522          * it's either old or wrong.  If it redirects us to ourselves,
  523          * we have a routing loop, perhaps as a result of an interface
  524          * going down recently.
  525          */
  526         if (!(flags & RTF_DONE) && rt &&
  527              (!sa_equal(src, rt->rt_gateway) || rt->rt_ifa != ifa))
  528                 error = EINVAL;
  529         else if (ifa_ifwithaddr_check(gateway))
  530                 error = EHOSTUNREACH;
  531         if (error)
  532                 goto done;
  533         /*
  534          * Create a new entry if we just got back a wildcard entry
  535          * or the the lookup failed.  This is necessary for hosts
  536          * which use routing redirects generated by smart gateways
  537          * to dynamically build the routing tables.
  538          */
  539         if (rt == NULL || (rt_mask(rt) && rt_mask(rt)->sa_len < 2))
  540                 goto create;
  541         /*
  542          * Don't listen to the redirect if it's
  543          * for a route to an interface.
  544          */
  545         if (rt->rt_flags & RTF_GATEWAY) {
  546                 if (((rt->rt_flags & RTF_HOST) == 0) && (flags & RTF_HOST)) {
  547                         /*
  548                          * Changing from route to net => route to host.
  549                          * Create new route, rather than smashing route to net.
  550                          */
  551                 create:
  552                         rt0 = rt;
  553                         rt = NULL;
  554                 
  555                         flags |=  RTF_GATEWAY | RTF_DYNAMIC;
  556                         bzero((caddr_t)&info, sizeof(info));
  557                         info.rti_info[RTAX_DST] = dst;
  558                         info.rti_info[RTAX_GATEWAY] = gateway;
  559                         info.rti_info[RTAX_NETMASK] = netmask;
  560                         info.rti_ifa = ifa;
  561                         info.rti_flags = flags;
  562                         if (rt0 != NULL)
  563                                 RT_UNLOCK(rt0); /* drop lock to avoid LOR with RNH */
  564                         error = rtrequest1_fib(RTM_ADD, &info, &rt, fibnum);
  565                         if (rt != NULL) {
  566                                 RT_LOCK(rt);
  567                                 if (rt0 != NULL)
  568                                         EVENTHANDLER_INVOKE(route_redirect_event, rt0, rt, dst);
  569                                 flags = rt->rt_flags;
  570                         }
  571                         if (rt0 != NULL)
  572                                 RTFREE(rt0);
  573                         
  574                         stat = &V_rtstat.rts_dynamic;
  575                 } else {
  576                         struct rtentry *gwrt;
  577 
  578                         /*
  579                          * Smash the current notion of the gateway to
  580                          * this destination.  Should check about netmask!!!
  581                          */
  582                         rt->rt_flags |= RTF_MODIFIED;
  583                         flags |= RTF_MODIFIED;
  584                         stat = &V_rtstat.rts_newgateway;
  585                         /*
  586                          * add the key and gateway (in one malloc'd chunk).
  587                          */
  588                         RT_UNLOCK(rt);
  589                         RADIX_NODE_HEAD_LOCK(rnh);
  590                         RT_LOCK(rt);
  591                         rt_setgate(rt, rt_key(rt), gateway);
  592                         gwrt = rtalloc1(gateway, 1, RTF_RNH_LOCKED);
  593                         RADIX_NODE_HEAD_UNLOCK(rnh);
  594                         EVENTHANDLER_INVOKE(route_redirect_event, rt, gwrt, dst);
  595                         RTFREE_LOCKED(gwrt);
  596                 }
  597         } else
  598                 error = EHOSTUNREACH;
  599 done:
  600         if (rt)
  601                 RTFREE_LOCKED(rt);
  602 out:
  603         if (error)
  604                 V_rtstat.rts_badredirect++;
  605         else if (stat != NULL)
  606                 (*stat)++;
  607         bzero((caddr_t)&info, sizeof(info));
  608         info.rti_info[RTAX_DST] = dst;
  609         info.rti_info[RTAX_GATEWAY] = gateway;
  610         info.rti_info[RTAX_NETMASK] = netmask;
  611         info.rti_info[RTAX_AUTHOR] = src;
  612         rt_missmsg(RTM_REDIRECT, &info, flags, error);
  613         if (ifa != NULL)
  614                 ifa_free(ifa);
  615 }
  616 
  617 int
  618 rtioctl(u_long req, caddr_t data)
  619 {
  620         return (rtioctl_fib(req, data, 0));
  621 }
  622 
  623 /*
  624  * Routing table ioctl interface.
  625  */
  626 int
  627 rtioctl_fib(u_long req, caddr_t data, u_int fibnum)
  628 {
  629 
  630         /*
  631          * If more ioctl commands are added here, make sure the proper
  632          * super-user checks are being performed because it is possible for
  633          * prison-root to make it this far if raw sockets have been enabled
  634          * in jails.
  635          */
  636 #ifdef INET
  637         /* Multicast goop, grrr... */
  638         return mrt_ioctl ? mrt_ioctl(req, data, fibnum) : EOPNOTSUPP;
  639 #else /* INET */
  640         return ENXIO;
  641 #endif /* INET */
  642 }
  643 
  644 /*
  645  * For both ifa_ifwithroute() routines, 'ifa' is returned referenced.
  646  */
  647 struct ifaddr *
  648 ifa_ifwithroute(int flags, struct sockaddr *dst, struct sockaddr *gateway)
  649 {
  650         return (ifa_ifwithroute_fib(flags, dst, gateway, 0));
  651 }
  652 
  653 struct ifaddr *
  654 ifa_ifwithroute_fib(int flags, struct sockaddr *dst, struct sockaddr *gateway,
  655                                 u_int fibnum)
  656 {
  657         register struct ifaddr *ifa;
  658         int not_found = 0;
  659 
  660         if ((flags & RTF_GATEWAY) == 0) {
  661                 /*
  662                  * If we are adding a route to an interface,
  663                  * and the interface is a pt to pt link
  664                  * we should search for the destination
  665                  * as our clue to the interface.  Otherwise
  666                  * we can use the local address.
  667                  */
  668                 ifa = NULL;
  669                 if (flags & RTF_HOST)
  670                         ifa = ifa_ifwithdstaddr(dst);
  671                 if (ifa == NULL)
  672                         ifa = ifa_ifwithaddr(gateway);
  673         } else {
  674                 /*
  675                  * If we are adding a route to a remote net
  676                  * or host, the gateway may still be on the
  677                  * other end of a pt to pt link.
  678                  */
  679                 ifa = ifa_ifwithdstaddr(gateway);
  680         }
  681         if (ifa == NULL)
  682                 ifa = ifa_ifwithnet(gateway);
  683         if (ifa == NULL) {
  684                 struct rtentry *rt = rtalloc1_fib(gateway, 0, RTF_RNH_LOCKED, fibnum);
  685                 if (rt == NULL)
  686                         return (NULL);
  687                 /*
  688                  * dismiss a gateway that is reachable only
  689                  * through the default router
  690                  */
  691                 switch (gateway->sa_family) {
  692                 case AF_INET:
  693                         if (satosin(rt_key(rt))->sin_addr.s_addr == INADDR_ANY)
  694                                 not_found = 1;
  695                         break;
  696                 case AF_INET6:
  697                         if (IN6_IS_ADDR_UNSPECIFIED(&satosin6(rt_key(rt))->sin6_addr))
  698                                 not_found = 1;
  699                         break;
  700                 default:
  701                         break;
  702                 }
  703                 if (!not_found && rt->rt_ifa != NULL) {
  704                         ifa = rt->rt_ifa;
  705                         ifa_ref(ifa);
  706                 }
  707                 RT_REMREF(rt);
  708                 RT_UNLOCK(rt);
  709                 if (not_found || ifa == NULL)
  710                         return (NULL);
  711         }
  712         if (ifa->ifa_addr->sa_family != dst->sa_family) {
  713                 struct ifaddr *oifa = ifa;
  714                 ifa = ifaof_ifpforaddr(dst, ifa->ifa_ifp);
  715                 if (ifa == NULL)
  716                         ifa = oifa;
  717                 else
  718                         ifa_free(oifa);
  719         }
  720         return (ifa);
  721 }
  722 
  723 /*
  724  * Do appropriate manipulations of a routing tree given
  725  * all the bits of info needed
  726  */
  727 int
  728 rtrequest(int req,
  729         struct sockaddr *dst,
  730         struct sockaddr *gateway,
  731         struct sockaddr *netmask,
  732         int flags,
  733         struct rtentry **ret_nrt)
  734 {
  735         return (rtrequest_fib(req, dst, gateway, netmask, flags, ret_nrt, 0));
  736 }
  737 
  738 int
  739 rtrequest_fib(int req,
  740         struct sockaddr *dst,
  741         struct sockaddr *gateway,
  742         struct sockaddr *netmask,
  743         int flags,
  744         struct rtentry **ret_nrt,
  745         u_int fibnum)
  746 {
  747         struct rt_addrinfo info;
  748 
  749         if (dst->sa_len == 0)
  750                 return(EINVAL);
  751 
  752         bzero((caddr_t)&info, sizeof(info));
  753         info.rti_flags = flags;
  754         info.rti_info[RTAX_DST] = dst;
  755         info.rti_info[RTAX_GATEWAY] = gateway;
  756         info.rti_info[RTAX_NETMASK] = netmask;
  757         return rtrequest1_fib(req, &info, ret_nrt, fibnum);
  758 }
  759 
  760 /*
  761  * These (questionable) definitions of apparent local variables apply
  762  * to the next two functions.  XXXXXX!!!
  763  */
  764 #define dst     info->rti_info[RTAX_DST]
  765 #define gateway info->rti_info[RTAX_GATEWAY]
  766 #define netmask info->rti_info[RTAX_NETMASK]
  767 #define ifaaddr info->rti_info[RTAX_IFA]
  768 #define ifpaddr info->rti_info[RTAX_IFP]
  769 #define flags   info->rti_flags
  770 
  771 int
  772 rt_getifa(struct rt_addrinfo *info)
  773 {
  774         return (rt_getifa_fib(info, 0));
  775 }
  776 
  777 /*
  778  * Look up rt_addrinfo for a specific fib.  Note that if rti_ifa is defined,
  779  * it will be referenced so the caller must free it.
  780  */
  781 int
  782 rt_getifa_fib(struct rt_addrinfo *info, u_int fibnum)
  783 {
  784         struct ifaddr *ifa;
  785         int error = 0;
  786 
  787         /*
  788          * ifp may be specified by sockaddr_dl
  789          * when protocol address is ambiguous.
  790          */
  791         if (info->rti_ifp == NULL && ifpaddr != NULL &&
  792             ifpaddr->sa_family == AF_LINK &&
  793             (ifa = ifa_ifwithnet(ifpaddr)) != NULL) {
  794                 info->rti_ifp = ifa->ifa_ifp;
  795                 ifa_free(ifa);
  796         }
  797         if (info->rti_ifa == NULL && ifaaddr != NULL)
  798                 info->rti_ifa = ifa_ifwithaddr(ifaaddr);
  799         if (info->rti_ifa == NULL) {
  800                 struct sockaddr *sa;
  801 
  802                 sa = ifaaddr != NULL ? ifaaddr :
  803                     (gateway != NULL ? gateway : dst);
  804                 if (sa != NULL && info->rti_ifp != NULL)
  805                         info->rti_ifa = ifaof_ifpforaddr(sa, info->rti_ifp);
  806                 else if (dst != NULL && gateway != NULL)
  807                         info->rti_ifa = ifa_ifwithroute_fib(flags, dst, gateway,
  808                                                         fibnum);
  809                 else if (sa != NULL)
  810                         info->rti_ifa = ifa_ifwithroute_fib(flags, sa, sa,
  811                                                         fibnum);
  812         }
  813         if ((ifa = info->rti_ifa) != NULL) {
  814                 if (info->rti_ifp == NULL)
  815                         info->rti_ifp = ifa->ifa_ifp;
  816         } else
  817                 error = ENETUNREACH;
  818         return (error);
  819 }
  820 
  821 /*
  822  * Expunges references to a route that's about to be reclaimed.
  823  * The route must be locked.
  824  */
  825 int
  826 rtexpunge(struct rtentry *rt)
  827 {
  828         struct radix_node *rn;
  829         struct radix_node_head *rnh;
  830         struct ifaddr *ifa;
  831         int error = 0;
  832 
  833         /*
  834          * Find the correct routing tree to use for this Address Family
  835          */
  836         rnh = rt_tables_get_rnh(rt->rt_fibnum, rt_key(rt)->sa_family);
  837         RT_LOCK_ASSERT(rt);
  838         if (rnh == NULL)
  839                 return (EAFNOSUPPORT);
  840         RADIX_NODE_HEAD_LOCK_ASSERT(rnh);
  841 #if 0
  842         /*
  843          * We cannot assume anything about the reference count
  844          * because protocols call us in many situations; often
  845          * before unwinding references to the table entry.
  846          */
  847         KASSERT(rt->rt_refcnt <= 1, ("bogus refcnt %ld", rt->rt_refcnt));
  848 #endif
  849         /*
  850          * Remove the item from the tree; it should be there,
  851          * but when callers invoke us blindly it may not (sigh).
  852          */
  853         rn = rnh->rnh_deladdr(rt_key(rt), rt_mask(rt), rnh);
  854         if (rn == NULL) {
  855                 error = ESRCH;
  856                 goto bad;
  857         }
  858         KASSERT((rn->rn_flags & (RNF_ACTIVE | RNF_ROOT)) == 0,
  859                 ("unexpected flags 0x%x", rn->rn_flags));
  860         KASSERT(rt == RNTORT(rn),
  861                 ("lookup mismatch, rt %p rn %p", rt, rn));
  862 
  863         rt->rt_flags &= ~RTF_UP;
  864 
  865         /*
  866          * Give the protocol a chance to keep things in sync.
  867          */
  868         if ((ifa = rt->rt_ifa) && ifa->ifa_rtrequest) {
  869                 struct rt_addrinfo info;
  870 
  871                 bzero((caddr_t)&info, sizeof(info));
  872                 info.rti_flags = rt->rt_flags;
  873                 info.rti_info[RTAX_DST] = rt_key(rt);
  874                 info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
  875                 info.rti_info[RTAX_NETMASK] = rt_mask(rt);
  876                 ifa->ifa_rtrequest(RTM_DELETE, rt, &info);
  877         }
  878 
  879         /*
  880          * one more rtentry floating around that is not
  881          * linked to the routing table.
  882          */
  883         V_rttrash++;
  884 bad:
  885         return (error);
  886 }
  887 
  888 #ifdef RADIX_MPATH
  889 static int
  890 rn_mpath_update(int req, struct rt_addrinfo *info,
  891     struct radix_node_head *rnh, struct rtentry **ret_nrt)
  892 {
  893         /*
  894          * if we got multipath routes, we require users to specify
  895          * a matching RTAX_GATEWAY.
  896          */
  897         struct rtentry *rt, *rto = NULL;
  898         register struct radix_node *rn;
  899         int error = 0;
  900 
  901         rn = rnh->rnh_matchaddr(dst, rnh);
  902         if (rn == NULL)
  903                 return (ESRCH);
  904         rto = rt = RNTORT(rn);
  905         rt = rt_mpath_matchgate(rt, gateway);
  906         if (rt == NULL)
  907                 return (ESRCH);
  908         /*
  909          * this is the first entry in the chain
  910          */
  911         if (rto == rt) {
  912                 rn = rn_mpath_next((struct radix_node *)rt);
  913                 /*
  914                  * there is another entry, now it's active
  915                  */
  916                 if (rn) {
  917                         rto = RNTORT(rn);
  918                         RT_LOCK(rto);
  919                         rto->rt_flags |= RTF_UP;
  920                         RT_UNLOCK(rto);
  921                 } else if (rt->rt_flags & RTF_GATEWAY) {
  922                         /*
  923                          * For gateway routes, we need to 
  924                          * make sure that we we are deleting
  925                          * the correct gateway. 
  926                          * rt_mpath_matchgate() does not 
  927                          * check the case when there is only
  928                          * one route in the chain.  
  929                          */
  930                         if (gateway &&
  931                             (rt->rt_gateway->sa_len != gateway->sa_len ||
  932                                 memcmp(rt->rt_gateway, gateway, gateway->sa_len)))
  933                                 error = ESRCH;
  934                         else {
  935                                 /*
  936                                  * remove from tree before returning it
  937                                  * to the caller
  938                                  */
  939                                 rn = rnh->rnh_deladdr(dst, netmask, rnh);
  940                                 KASSERT(rt == RNTORT(rn), ("radix node disappeared"));
  941                                 goto gwdelete;
  942                         }
  943                         
  944                 }
  945                 /*
  946                  * use the normal delete code to remove
  947                  * the first entry
  948                  */
  949                 if (req != RTM_DELETE) 
  950                         goto nondelete;
  951 
  952                 error = ENOENT;
  953                 goto done;
  954         }
  955                 
  956         /*
  957          * if the entry is 2nd and on up
  958          */
  959         if ((req == RTM_DELETE) && !rt_mpath_deldup(rto, rt))
  960                 panic ("rtrequest1: rt_mpath_deldup");
  961 gwdelete:
  962         RT_LOCK(rt);
  963         RT_ADDREF(rt);
  964         if (req == RTM_DELETE) {
  965                 rt->rt_flags &= ~RTF_UP;
  966                 /*
  967                  * One more rtentry floating around that is not
  968                  * linked to the routing table. rttrash will be decremented
  969                  * when RTFREE(rt) is eventually called.
  970                  */
  971                 V_rttrash++;
  972         }
  973         
  974 nondelete:
  975         if (req != RTM_DELETE)
  976                 panic("unrecognized request %d", req);
  977         
  978 
  979         /*
  980          * If the caller wants it, then it can have it,
  981          * but it's up to it to free the rtentry as we won't be
  982          * doing it.
  983          */
  984         if (ret_nrt) {
  985                 *ret_nrt = rt;
  986                 RT_UNLOCK(rt);
  987         } else
  988                 RTFREE_LOCKED(rt);
  989 done:
  990         return (error);
  991 }
  992 #endif
  993 
  994 int
  995 rtrequest1_fib(int req, struct rt_addrinfo *info, struct rtentry **ret_nrt,
  996                                 u_int fibnum)
  997 {
  998         int error = 0, needlock = 0;
  999         register struct rtentry *rt;
 1000 #ifdef FLOWTABLE
 1001         register struct rtentry *rt0;
 1002 #endif
 1003         register struct radix_node *rn;
 1004         register struct radix_node_head *rnh;
 1005         struct ifaddr *ifa;
 1006         struct sockaddr *ndst;
 1007 #define senderr(x) { error = x ; goto bad; }
 1008 
 1009         KASSERT((fibnum < rt_numfibs), ("rtrequest1_fib: bad fibnum"));
 1010         if (dst->sa_family != AF_INET)  /* Only INET supports > 1 fib now */
 1011                 fibnum = 0;
 1012         /*
 1013          * Find the correct routing tree to use for this Address Family
 1014          */
 1015         rnh = rt_tables_get_rnh(fibnum, dst->sa_family);
 1016         if (rnh == NULL)
 1017                 return (EAFNOSUPPORT);
 1018         needlock = ((flags & RTF_RNH_LOCKED) == 0);
 1019         flags &= ~RTF_RNH_LOCKED;
 1020         if (needlock)
 1021                 RADIX_NODE_HEAD_LOCK(rnh);
 1022         else
 1023                 RADIX_NODE_HEAD_LOCK_ASSERT(rnh);
 1024         /*
 1025          * If we are adding a host route then we don't want to put
 1026          * a netmask in the tree, nor do we want to clone it.
 1027          */
 1028         if (flags & RTF_HOST)
 1029                 netmask = NULL;
 1030 
 1031         switch (req) {
 1032         case RTM_DELETE:
 1033 #ifdef RADIX_MPATH
 1034                 if (rn_mpath_capable(rnh)) {
 1035                         error = rn_mpath_update(req, info, rnh, ret_nrt);
 1036                         /*
 1037                          * "bad" holds true for the success case
 1038                          * as well
 1039                          */
 1040                         if (error != ENOENT)
 1041                                 goto bad;
 1042                 }
 1043 #endif
 1044                 /*
 1045                  * Remove the item from the tree and return it.
 1046                  * Complain if it is not there and do no more processing.
 1047                  */
 1048                 rn = rnh->rnh_deladdr(dst, netmask, rnh);
 1049                 if (rn == NULL)
 1050                         senderr(ESRCH);
 1051                 if (rn->rn_flags & (RNF_ACTIVE | RNF_ROOT))
 1052                         panic ("rtrequest delete");
 1053                 rt = RNTORT(rn);
 1054                 RT_LOCK(rt);
 1055                 RT_ADDREF(rt);
 1056                 rt->rt_flags &= ~RTF_UP;
 1057 
 1058                 /*
 1059                  * give the protocol a chance to keep things in sync.
 1060                  */
 1061                 if ((ifa = rt->rt_ifa) && ifa->ifa_rtrequest)
 1062                         ifa->ifa_rtrequest(RTM_DELETE, rt, info);
 1063 
 1064                 /*
 1065                  * One more rtentry floating around that is not
 1066                  * linked to the routing table. rttrash will be decremented
 1067                  * when RTFREE(rt) is eventually called.
 1068                  */
 1069                 V_rttrash++;
 1070 
 1071                 /*
 1072                  * If the caller wants it, then it can have it,
 1073                  * but it's up to it to free the rtentry as we won't be
 1074                  * doing it.
 1075                  */
 1076                 if (ret_nrt) {
 1077                         *ret_nrt = rt;
 1078                         RT_UNLOCK(rt);
 1079                 } else
 1080                         RTFREE_LOCKED(rt);
 1081                 break;
 1082         case RTM_RESOLVE:
 1083                 /*
 1084                  * resolve was only used for route cloning
 1085                  * here for compat
 1086                  */
 1087                 break;
 1088         case RTM_ADD:
 1089                 if ((flags & RTF_GATEWAY) && !gateway)
 1090                         senderr(EINVAL);
 1091                 if (dst && gateway && (dst->sa_family != gateway->sa_family) && 
 1092                     (gateway->sa_family != AF_UNSPEC) && (gateway->sa_family != AF_LINK))
 1093                         senderr(EINVAL);
 1094 
 1095                 if (info->rti_ifa == NULL) {
 1096                         error = rt_getifa_fib(info, fibnum);
 1097                         if (error)
 1098                                 senderr(error);
 1099                 } else
 1100                         ifa_ref(info->rti_ifa);
 1101                 ifa = info->rti_ifa;
 1102                 rt = uma_zalloc(V_rtzone, M_NOWAIT | M_ZERO);
 1103                 if (rt == NULL) {
 1104                         if (ifa != NULL)
 1105                                 ifa_free(ifa);
 1106                         senderr(ENOBUFS);
 1107                 }
 1108                 RT_LOCK_INIT(rt);
 1109                 rt->rt_flags = RTF_UP | flags;
 1110                 rt->rt_fibnum = fibnum;
 1111                 /*
 1112                  * Add the gateway. Possibly re-malloc-ing the storage for it
 1113                  * 
 1114                  */
 1115                 RT_LOCK(rt);
 1116                 if ((error = rt_setgate(rt, dst, gateway)) != 0) {
 1117                         RT_LOCK_DESTROY(rt);
 1118                         if (ifa != NULL)
 1119                                 ifa_free(ifa);
 1120                         uma_zfree(V_rtzone, rt);
 1121                         senderr(error);
 1122                 }
 1123 
 1124                 /*
 1125                  * point to the (possibly newly malloc'd) dest address.
 1126                  */
 1127                 ndst = (struct sockaddr *)rt_key(rt);
 1128 
 1129                 /*
 1130                  * make sure it contains the value we want (masked if needed).
 1131                  */
 1132                 if (netmask) {
 1133                         rt_maskedcopy(dst, ndst, netmask);
 1134                 } else
 1135                         bcopy(dst, ndst, dst->sa_len);
 1136 
 1137                 /*
 1138                  * We use the ifa reference returned by rt_getifa_fib().
 1139                  * This moved from below so that rnh->rnh_addaddr() can
 1140                  * examine the ifa and  ifa->ifa_ifp if it so desires.
 1141                  */
 1142                 rt->rt_ifa = ifa;
 1143                 rt->rt_ifp = ifa->ifa_ifp;
 1144                 rt->rt_rmx.rmx_weight = 1;
 1145 
 1146 #ifdef RADIX_MPATH
 1147                 /* do not permit exactly the same dst/mask/gw pair */
 1148                 if (rn_mpath_capable(rnh) &&
 1149                         rt_mpath_conflict(rnh, rt, netmask)) {
 1150                         if (rt->rt_ifa) {
 1151                                 ifa_free(rt->rt_ifa);
 1152                         }
 1153                         Free(rt_key(rt));
 1154                         RT_LOCK_DESTROY(rt);
 1155                         uma_zfree(V_rtzone, rt);
 1156                         senderr(EEXIST);
 1157                 }
 1158 #endif
 1159 
 1160 #ifdef FLOWTABLE
 1161                 rt0 = NULL;
 1162                 /* XXX
 1163                  * "flow-table" only support IPv4 at the moment.
 1164                  */
 1165 #ifdef INET
 1166                 if (dst->sa_family == AF_INET) {
 1167                         rn = rnh->rnh_matchaddr(dst, rnh);
 1168                         if (rn && ((rn->rn_flags & RNF_ROOT) == 0)) {
 1169                                 struct sockaddr *mask;
 1170                                 u_char *m, *n;
 1171                                 int len;
 1172                                 
 1173                                 /*
 1174                                  * compare mask to see if the new route is
 1175                                  * more specific than the existing one
 1176                                  */
 1177                                 rt0 = RNTORT(rn);
 1178                                 RT_LOCK(rt0);
 1179                                 RT_ADDREF(rt0);
 1180                                 RT_UNLOCK(rt0);
 1181                                 /*
 1182                                  * A host route is already present, so 
 1183                                  * leave the flow-table entries as is.
 1184                                  */
 1185                                 if (rt0->rt_flags & RTF_HOST) {
 1186                                         RTFREE(rt0);
 1187                                         rt0 = NULL;
 1188                                 } else if (!(flags & RTF_HOST) && netmask) {
 1189                                         mask = rt_mask(rt0);
 1190                                         len = mask->sa_len;
 1191                                         m = (u_char *)mask;
 1192                                         n = (u_char *)netmask;
 1193                                         while (len-- > 0) {
 1194                                                 if (*n != *m)
 1195                                                         break;
 1196                                                 n++;
 1197                                                 m++;
 1198                                         }
 1199                                         if (len == 0 || (*n < *m)) {
 1200                                                 RTFREE(rt0);
 1201                                                 rt0 = NULL;
 1202                                         }
 1203                                 }
 1204                         }
 1205                 }
 1206 #endif
 1207 #endif
 1208 
 1209                 /* XXX mtu manipulation will be done in rnh_addaddr -- itojun */
 1210                 rn = rnh->rnh_addaddr(ndst, netmask, rnh, rt->rt_nodes);
 1211                 /*
 1212                  * If it still failed to go into the tree,
 1213                  * then un-make it (this should be a function)
 1214                  */
 1215                 if (rn == NULL) {
 1216                         if (rt->rt_ifa)
 1217                                 ifa_free(rt->rt_ifa);
 1218                         Free(rt_key(rt));
 1219                         RT_LOCK_DESTROY(rt);
 1220                         uma_zfree(V_rtzone, rt);
 1221 #ifdef FLOWTABLE
 1222                         if (rt0 != NULL)
 1223                                 RTFREE(rt0);
 1224 #endif
 1225                         senderr(EEXIST);
 1226                 } 
 1227 #ifdef FLOWTABLE
 1228                 else if (rt0 != NULL) {
 1229 #ifdef INET
 1230                         flowtable_route_flush(V_ip_ft, rt0);
 1231 #endif
 1232                         RTFREE(rt0);
 1233                 }
 1234 #endif
 1235 
 1236                 /*
 1237                  * If this protocol has something to add to this then
 1238                  * allow it to do that as well.
 1239                  */
 1240                 if (ifa->ifa_rtrequest)
 1241                         ifa->ifa_rtrequest(req, rt, info);
 1242 
 1243                 /*
 1244                  * actually return a resultant rtentry and
 1245                  * give the caller a single reference.
 1246                  */
 1247                 if (ret_nrt) {
 1248                         *ret_nrt = rt;
 1249                         RT_ADDREF(rt);
 1250                 }
 1251                 RT_UNLOCK(rt);
 1252                 break;
 1253         default:
 1254                 error = EOPNOTSUPP;
 1255         }
 1256 bad:
 1257         if (needlock)
 1258                 RADIX_NODE_HEAD_UNLOCK(rnh);
 1259         return (error);
 1260 #undef senderr
 1261 }
 1262 
 1263 #undef dst
 1264 #undef gateway
 1265 #undef netmask
 1266 #undef ifaaddr
 1267 #undef ifpaddr
 1268 #undef flags
 1269 
 1270 int
 1271 rt_setgate(struct rtentry *rt, struct sockaddr *dst, struct sockaddr *gate)
 1272 {
 1273         /* XXX dst may be overwritten, can we move this to below */
 1274         int dlen = SA_SIZE(dst), glen = SA_SIZE(gate);
 1275 #ifdef INVARIANTS
 1276         struct radix_node_head *rnh;
 1277 
 1278         rnh = rt_tables_get_rnh(rt->rt_fibnum, dst->sa_family);
 1279 #endif
 1280 
 1281         RT_LOCK_ASSERT(rt);
 1282         RADIX_NODE_HEAD_LOCK_ASSERT(rnh);
 1283         
 1284         /*
 1285          * Prepare to store the gateway in rt->rt_gateway.
 1286          * Both dst and gateway are stored one after the other in the same
 1287          * malloc'd chunk. If we have room, we can reuse the old buffer,
 1288          * rt_gateway already points to the right place.
 1289          * Otherwise, malloc a new block and update the 'dst' address.
 1290          */
 1291         if (rt->rt_gateway == NULL || glen > SA_SIZE(rt->rt_gateway)) {
 1292                 caddr_t new;
 1293 
 1294                 R_Malloc(new, caddr_t, dlen + glen);
 1295                 if (new == NULL)
 1296                         return ENOBUFS;
 1297                 /*
 1298                  * XXX note, we copy from *dst and not *rt_key(rt) because
 1299                  * rt_setgate() can be called to initialize a newly
 1300                  * allocated route entry, in which case rt_key(rt) == NULL
 1301                  * (and also rt->rt_gateway == NULL).
 1302                  * Free()/free() handle a NULL argument just fine.
 1303                  */
 1304                 bcopy(dst, new, dlen);
 1305                 Free(rt_key(rt));       /* free old block, if any */
 1306                 rt_key(rt) = (struct sockaddr *)new;
 1307                 rt->rt_gateway = (struct sockaddr *)(new + dlen);
 1308         }
 1309 
 1310         /*
 1311          * Copy the new gateway value into the memory chunk.
 1312          */
 1313         bcopy(gate, rt->rt_gateway, glen);
 1314 
 1315         return (0);
 1316 }
 1317 
 1318 static void
 1319 rt_maskedcopy(struct sockaddr *src, struct sockaddr *dst, struct sockaddr *netmask)
 1320 {
 1321         register u_char *cp1 = (u_char *)src;
 1322         register u_char *cp2 = (u_char *)dst;
 1323         register u_char *cp3 = (u_char *)netmask;
 1324         u_char *cplim = cp2 + *cp3;
 1325         u_char *cplim2 = cp2 + *cp1;
 1326 
 1327         *cp2++ = *cp1++; *cp2++ = *cp1++; /* copies sa_len & sa_family */
 1328         cp3 += 2;
 1329         if (cplim > cplim2)
 1330                 cplim = cplim2;
 1331         while (cp2 < cplim)
 1332                 *cp2++ = *cp1++ & *cp3++;
 1333         if (cp2 < cplim2)
 1334                 bzero((caddr_t)cp2, (unsigned)(cplim2 - cp2));
 1335 }
 1336 
 1337 /*
 1338  * Set up a routing table entry, normally
 1339  * for an interface.
 1340  */
 1341 #define _SOCKADDR_TMPSIZE 128 /* Not too big.. kernel stack size is limited */
 1342 static inline  int
 1343 rtinit1(struct ifaddr *ifa, int cmd, int flags, int fibnum)
 1344 {
 1345         struct sockaddr *dst;
 1346         struct sockaddr *netmask;
 1347         struct rtentry *rt = NULL;
 1348         struct rt_addrinfo info;
 1349         int error = 0;
 1350         int startfib, endfib;
 1351         char tempbuf[_SOCKADDR_TMPSIZE];
 1352         int didwork = 0;
 1353         int a_failure = 0;
 1354         static struct sockaddr_dl null_sdl = {sizeof(null_sdl), AF_LINK};
 1355 
 1356         if (flags & RTF_HOST) {
 1357                 dst = ifa->ifa_dstaddr;
 1358                 netmask = NULL;
 1359         } else {
 1360                 dst = ifa->ifa_addr;
 1361                 netmask = ifa->ifa_netmask;
 1362         }
 1363         if ( dst->sa_family != AF_INET)
 1364                 fibnum = 0;
 1365         if (fibnum == -1) {
 1366                 if (rt_add_addr_allfibs == 0 && cmd == (int)RTM_ADD) {
 1367                         startfib = endfib = curthread->td_proc->p_fibnum;
 1368                 } else {
 1369                         startfib = 0;
 1370                         endfib = rt_numfibs - 1;
 1371                 }
 1372         } else {
 1373                 KASSERT((fibnum < rt_numfibs), ("rtinit1: bad fibnum"));
 1374                 startfib = fibnum;
 1375                 endfib = fibnum;
 1376         }
 1377         if (dst->sa_len == 0)
 1378                 return(EINVAL);
 1379 
 1380         /*
 1381          * If it's a delete, check that if it exists,
 1382          * it's on the correct interface or we might scrub
 1383          * a route to another ifa which would
 1384          * be confusing at best and possibly worse.
 1385          */
 1386         if (cmd == RTM_DELETE) {
 1387                 /*
 1388                  * It's a delete, so it should already exist..
 1389                  * If it's a net, mask off the host bits
 1390                  * (Assuming we have a mask)
 1391                  * XXX this is kinda inet specific..
 1392                  */
 1393                 if (netmask != NULL) {
 1394                         rt_maskedcopy(dst, (struct sockaddr *)tempbuf, netmask);
 1395                         dst = (struct sockaddr *)tempbuf;
 1396                 }
 1397         }
 1398         /*
 1399          * Now go through all the requested tables (fibs) and do the
 1400          * requested action. Realistically, this will either be fib 0
 1401          * for protocols that don't do multiple tables or all the
 1402          * tables for those that do. XXX For this version only AF_INET.
 1403          * When that changes code should be refactored to protocol
 1404          * independent parts and protocol dependent parts.
 1405          */
 1406         for ( fibnum = startfib; fibnum <= endfib; fibnum++) {
 1407                 if (cmd == RTM_DELETE) {
 1408                         struct radix_node_head *rnh;
 1409                         struct radix_node *rn;
 1410                         /*
 1411                          * Look up an rtentry that is in the routing tree and
 1412                          * contains the correct info.
 1413                          */
 1414                         rnh = rt_tables_get_rnh(fibnum, dst->sa_family);
 1415                         if (rnh == NULL)
 1416                                 /* this table doesn't exist but others might */
 1417                                 continue;
 1418                         RADIX_NODE_HEAD_LOCK(rnh);
 1419 #ifdef RADIX_MPATH
 1420                         if (rn_mpath_capable(rnh)) {
 1421 
 1422                                 rn = rnh->rnh_matchaddr(dst, rnh);
 1423                                 if (rn == NULL) 
 1424                                         error = ESRCH;
 1425                                 else {
 1426                                         rt = RNTORT(rn);
 1427                                         /*
 1428                                          * for interface route the
 1429                                          * rt->rt_gateway is sockaddr_intf
 1430                                          * for cloning ARP entries, so
 1431                                          * rt_mpath_matchgate must use the
 1432                                          * interface address
 1433                                          */
 1434                                         rt = rt_mpath_matchgate(rt,
 1435                                             ifa->ifa_addr);
 1436                                         if (!rt) 
 1437                                                 error = ESRCH;
 1438                                 }
 1439                         }
 1440                         else
 1441 #endif
 1442                         rn = rnh->rnh_lookup(dst, netmask, rnh);
 1443                         error = (rn == NULL ||
 1444                             (rn->rn_flags & RNF_ROOT) ||
 1445                             RNTORT(rn)->rt_ifa != ifa ||
 1446                             !sa_equal((struct sockaddr *)rn->rn_key, dst));
 1447                         RADIX_NODE_HEAD_UNLOCK(rnh);
 1448                         if (error) {
 1449                                 /* this is only an error if bad on ALL tables */
 1450                                 continue;
 1451                         }
 1452                 }
 1453                 /*
 1454                  * Do the actual request
 1455                  */
 1456                 bzero((caddr_t)&info, sizeof(info));
 1457                 info.rti_ifa = ifa;
 1458                 info.rti_flags = flags | ifa->ifa_flags;
 1459                 info.rti_info[RTAX_DST] = dst;
 1460                 /* 
 1461                  * doing this for compatibility reasons
 1462                  */
 1463                 if (cmd == RTM_ADD)
 1464                         info.rti_info[RTAX_GATEWAY] =
 1465                             (struct sockaddr *)&null_sdl;
 1466                 else
 1467                         info.rti_info[RTAX_GATEWAY] = ifa->ifa_addr;
 1468                 info.rti_info[RTAX_NETMASK] = netmask;
 1469                 error = rtrequest1_fib(cmd, &info, &rt, fibnum);
 1470                 if (error == 0 && rt != NULL) {
 1471                         /*
 1472                          * notify any listening routing agents of the change
 1473                          */
 1474                         RT_LOCK(rt);
 1475 #ifdef RADIX_MPATH
 1476                         /*
 1477                          * in case address alias finds the first address
 1478                          * e.g. ifconfig bge0 192.103.54.246/24
 1479                          * e.g. ifconfig bge0 192.103.54.247/24
 1480                          * the address set in the route is 192.103.54.246
 1481                          * so we need to replace it with 192.103.54.247
 1482                          */
 1483                         if (memcmp(rt->rt_ifa->ifa_addr,
 1484                             ifa->ifa_addr, ifa->ifa_addr->sa_len)) {
 1485                                 ifa_free(rt->rt_ifa);
 1486                                 ifa_ref(ifa);
 1487                                 rt->rt_ifp = ifa->ifa_ifp;
 1488                                 rt->rt_ifa = ifa;
 1489                         }
 1490 #endif
 1491                         /* 
 1492                          * doing this for compatibility reasons
 1493                          */
 1494                         if (cmd == RTM_ADD) {
 1495                             ((struct sockaddr_dl *)rt->rt_gateway)->sdl_type  =
 1496                                 rt->rt_ifp->if_type;
 1497                             ((struct sockaddr_dl *)rt->rt_gateway)->sdl_index =
 1498                                 rt->rt_ifp->if_index;
 1499                         }
 1500                         rt_newaddrmsg(cmd, ifa, error, rt);
 1501                         if (cmd == RTM_DELETE) {
 1502                                 /*
 1503                                  * If we are deleting, and we found an entry,
 1504                                  * then it's been removed from the tree..
 1505                                  * now throw it away.
 1506                                  */
 1507                                 RTFREE_LOCKED(rt);
 1508                         } else {
 1509                                 if (cmd == RTM_ADD) {
 1510                                         /*
 1511                                          * We just wanted to add it..
 1512                                          * we don't actually need a reference.
 1513                                          */
 1514                                         RT_REMREF(rt);
 1515                                 }
 1516                                 RT_UNLOCK(rt);
 1517                         }
 1518                         didwork = 1;
 1519                 }
 1520                 if (error)
 1521                         a_failure = error;
 1522         }
 1523         if (cmd == RTM_DELETE) {
 1524                 if (didwork) {
 1525                         error = 0;
 1526                 } else {
 1527                         /* we only give an error if it wasn't in any table */
 1528                         error = ((flags & RTF_HOST) ?
 1529                             EHOSTUNREACH : ENETUNREACH);
 1530                 }
 1531         } else {
 1532                 if (a_failure) {
 1533                         /* return an error if any of them failed */
 1534                         error = a_failure;
 1535                 }
 1536         }
 1537         return (error);
 1538 }
 1539 
 1540 /* special one for inet internal use. may not use. */
 1541 int
 1542 rtinit_fib(struct ifaddr *ifa, int cmd, int flags)
 1543 {
 1544         return (rtinit1(ifa, cmd, flags, -1));
 1545 }
 1546 
 1547 /*
 1548  * Set up a routing table entry, normally
 1549  * for an interface.
 1550  */
 1551 int
 1552 rtinit(struct ifaddr *ifa, int cmd, int flags)
 1553 {
 1554         struct sockaddr *dst;
 1555         int fib = 0;
 1556 
 1557         if (flags & RTF_HOST) {
 1558                 dst = ifa->ifa_dstaddr;
 1559         } else {
 1560                 dst = ifa->ifa_addr;
 1561         }
 1562 
 1563         if (dst->sa_family == AF_INET)
 1564                 fib = -1;
 1565         return (rtinit1(ifa, cmd, flags, fib));
 1566 }

Cache object: f75de9d4963b21200d3d135907ffe2e1


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]


This page is part of the FreeBSD/Linux Linux Kernel Cross-Reference, and was automatically generated using a modified version of the LXR engine.