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/11.1/sys/net/route.c 320134 2017-06-20 05:57:28Z ae $
   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_inet6.h"
   39 #include "opt_route.h"
   40 #include "opt_sctp.h"
   41 #include "opt_mrouting.h"
   42 #include "opt_mpath.h"
   43 
   44 #include <sys/param.h>
   45 #include <sys/systm.h>
   46 #include <sys/malloc.h>
   47 #include <sys/mbuf.h>
   48 #include <sys/socket.h>
   49 #include <sys/sysctl.h>
   50 #include <sys/syslog.h>
   51 #include <sys/sysproto.h>
   52 #include <sys/proc.h>
   53 #include <sys/domain.h>
   54 #include <sys/kernel.h>
   55 
   56 #include <net/if.h>
   57 #include <net/if_var.h>
   58 #include <net/if_dl.h>
   59 #include <net/route.h>
   60 #include <net/route_var.h>
   61 #include <net/vnet.h>
   62 #include <net/flowtable.h>
   63 
   64 #ifdef RADIX_MPATH
   65 #include <net/radix_mpath.h>
   66 #endif
   67 
   68 #include <netinet/in.h>
   69 #include <netinet/ip_mroute.h>
   70 
   71 #include <vm/uma.h>
   72 
   73 #define RT_MAXFIBS      UINT16_MAX
   74 
   75 /* Kernel config default option. */
   76 #ifdef ROUTETABLES
   77 #if ROUTETABLES <= 0
   78 #error "ROUTETABLES defined too low"
   79 #endif
   80 #if ROUTETABLES > RT_MAXFIBS
   81 #error "ROUTETABLES defined too big"
   82 #endif
   83 #define RT_NUMFIBS      ROUTETABLES
   84 #endif /* ROUTETABLES */
   85 /* Initialize to default if not otherwise set. */
   86 #ifndef RT_NUMFIBS
   87 #define RT_NUMFIBS      1
   88 #endif
   89 
   90 #if defined(INET) || defined(INET6)
   91 #ifdef SCTP
   92 extern void sctp_addr_change(struct ifaddr *ifa, int cmd);
   93 #endif /* SCTP */
   94 #endif
   95 
   96 
   97 /* This is read-only.. */
   98 u_int rt_numfibs = RT_NUMFIBS;
   99 SYSCTL_UINT(_net, OID_AUTO, fibs, CTLFLAG_RDTUN, &rt_numfibs, 0, "");
  100 
  101 /*
  102  * By default add routes to all fibs for new interfaces.
  103  * Once this is set to 0 then only allocate routes on interface
  104  * changes for the FIB of the caller when adding a new set of addresses
  105  * to an interface.  XXX this is a shotgun aproach to a problem that needs
  106  * a more fine grained solution.. that will come.
  107  * XXX also has the problems getting the FIB from curthread which will not
  108  * always work given the fib can be overridden and prefixes can be added
  109  * from the network stack context.
  110  */
  111 VNET_DEFINE(u_int, rt_add_addr_allfibs) = 1;
  112 SYSCTL_UINT(_net, OID_AUTO, add_addr_allfibs, CTLFLAG_RWTUN | CTLFLAG_VNET,
  113     &VNET_NAME(rt_add_addr_allfibs), 0, "");
  114 
  115 VNET_DEFINE(struct rtstat, rtstat);
  116 #define V_rtstat        VNET(rtstat)
  117 
  118 VNET_DEFINE(struct rib_head *, rt_tables);
  119 #define V_rt_tables     VNET(rt_tables)
  120 
  121 VNET_DEFINE(int, rttrash);              /* routes not in table but not freed */
  122 #define V_rttrash       VNET(rttrash)
  123 
  124 
  125 /*
  126  * Convert a 'struct radix_node *' to a 'struct rtentry *'.
  127  * The operation can be done safely (in this code) because a
  128  * 'struct rtentry' starts with two 'struct radix_node''s, the first
  129  * one representing leaf nodes in the routing tree, which is
  130  * what the code in radix.c passes us as a 'struct radix_node'.
  131  *
  132  * But because there are a lot of assumptions in this conversion,
  133  * do not cast explicitly, but always use the macro below.
  134  */
  135 #define RNTORT(p)       ((struct rtentry *)(p))
  136 
  137 static VNET_DEFINE(uma_zone_t, rtzone);         /* Routing table UMA zone. */
  138 #define V_rtzone        VNET(rtzone)
  139 
  140 static int rtrequest1_fib_change(struct rib_head *, struct rt_addrinfo *,
  141     struct rtentry **, u_int);
  142 static void rt_setmetrics(const struct rt_addrinfo *, struct rtentry *);
  143 static int rt_ifdelroute(const struct rtentry *rt, void *arg);
  144 static struct rtentry *rt_unlinkrte(struct rib_head *rnh,
  145     struct rt_addrinfo *info, int *perror);
  146 static void rt_notifydelete(struct rtentry *rt, struct rt_addrinfo *info);
  147 #ifdef RADIX_MPATH
  148 static struct radix_node *rt_mpath_unlink(struct rib_head *rnh,
  149     struct rt_addrinfo *info, struct rtentry *rto, int *perror);
  150 #endif
  151 static int rt_exportinfo(struct rtentry *rt, struct rt_addrinfo *info,
  152     int flags);
  153 
  154 struct if_mtuinfo
  155 {
  156         struct ifnet    *ifp;
  157         int             mtu;
  158 };
  159 
  160 static int      if_updatemtu_cb(struct radix_node *, void *);
  161 
  162 /*
  163  * handler for net.my_fibnum
  164  */
  165 static int
  166 sysctl_my_fibnum(SYSCTL_HANDLER_ARGS)
  167 {
  168         int fibnum;
  169         int error;
  170  
  171         fibnum = curthread->td_proc->p_fibnum;
  172         error = sysctl_handle_int(oidp, &fibnum, 0, req);
  173         return (error);
  174 }
  175 
  176 SYSCTL_PROC(_net, OID_AUTO, my_fibnum, CTLTYPE_INT|CTLFLAG_RD,
  177             NULL, 0, &sysctl_my_fibnum, "I", "default FIB of caller");
  178 
  179 static __inline struct rib_head **
  180 rt_tables_get_rnh_ptr(int table, int fam)
  181 {
  182         struct rib_head **rnh;
  183 
  184         KASSERT(table >= 0 && table < rt_numfibs, ("%s: table out of bounds.",
  185             __func__));
  186         KASSERT(fam >= 0 && fam < (AF_MAX+1), ("%s: fam out of bounds.",
  187             __func__));
  188 
  189         /* rnh is [fib=0][af=0]. */
  190         rnh = (struct rib_head **)V_rt_tables;
  191         /* Get the offset to the requested table and fam. */
  192         rnh += table * (AF_MAX+1) + fam;
  193 
  194         return (rnh);
  195 }
  196 
  197 struct rib_head *
  198 rt_tables_get_rnh(int table, int fam)
  199 {
  200 
  201         return (*rt_tables_get_rnh_ptr(table, fam));
  202 }
  203 
  204 u_int
  205 rt_tables_get_gen(int table, int fam)
  206 {
  207         struct rib_head *rnh;
  208 
  209         rnh = *rt_tables_get_rnh_ptr(table, fam);
  210         KASSERT(rnh != NULL, ("%s: NULL rib_head pointer table %d fam %d",
  211             __func__, table, fam));
  212         return (rnh->rnh_gen);
  213 }
  214 
  215 
  216 /*
  217  * route initialization must occur before ip6_init2(), which happenas at
  218  * SI_ORDER_MIDDLE.
  219  */
  220 static void
  221 route_init(void)
  222 {
  223 
  224         /* whack the tunable ints into  line. */
  225         if (rt_numfibs > RT_MAXFIBS)
  226                 rt_numfibs = RT_MAXFIBS;
  227         if (rt_numfibs == 0)
  228                 rt_numfibs = 1;
  229 }
  230 SYSINIT(route_init, SI_SUB_PROTO_DOMAIN, SI_ORDER_THIRD, route_init, 0);
  231 
  232 static int
  233 rtentry_zinit(void *mem, int size, int how)
  234 {
  235         struct rtentry *rt = mem;
  236 
  237         rt->rt_pksent = counter_u64_alloc(how);
  238         if (rt->rt_pksent == NULL)
  239                 return (ENOMEM);
  240 
  241         RT_LOCK_INIT(rt);
  242 
  243         return (0);
  244 }
  245 
  246 static void
  247 rtentry_zfini(void *mem, int size)
  248 {
  249         struct rtentry *rt = mem;
  250 
  251         RT_LOCK_DESTROY(rt);
  252         counter_u64_free(rt->rt_pksent);
  253 }
  254 
  255 static int
  256 rtentry_ctor(void *mem, int size, void *arg, int how)
  257 {
  258         struct rtentry *rt = mem;
  259 
  260         bzero(rt, offsetof(struct rtentry, rt_endzero));
  261         counter_u64_zero(rt->rt_pksent);
  262         rt->rt_chain = NULL;
  263 
  264         return (0);
  265 }
  266 
  267 static void
  268 rtentry_dtor(void *mem, int size, void *arg)
  269 {
  270         struct rtentry *rt = mem;
  271 
  272         RT_UNLOCK_COND(rt);
  273 }
  274 
  275 static void
  276 vnet_route_init(const void *unused __unused)
  277 {
  278         struct domain *dom;
  279         struct rib_head **rnh;
  280         int table;
  281         int fam;
  282 
  283         V_rt_tables = malloc(rt_numfibs * (AF_MAX+1) *
  284             sizeof(struct rib_head *), M_RTABLE, M_WAITOK|M_ZERO);
  285 
  286         V_rtzone = uma_zcreate("rtentry", sizeof(struct rtentry),
  287             rtentry_ctor, rtentry_dtor,
  288             rtentry_zinit, rtentry_zfini, UMA_ALIGN_PTR, 0);
  289         for (dom = domains; dom; dom = dom->dom_next) {
  290                 if (dom->dom_rtattach == NULL)
  291                         continue;
  292 
  293                 for  (table = 0; table < rt_numfibs; table++) {
  294                         fam = dom->dom_family;
  295                         if (table != 0 && fam != AF_INET6 && fam != AF_INET)
  296                                 break;
  297 
  298                         rnh = rt_tables_get_rnh_ptr(table, fam);
  299                         if (rnh == NULL)
  300                                 panic("%s: rnh NULL", __func__);
  301                         dom->dom_rtattach((void **)rnh, 0);
  302                 }
  303         }
  304 }
  305 VNET_SYSINIT(vnet_route_init, SI_SUB_PROTO_DOMAIN, SI_ORDER_FOURTH,
  306     vnet_route_init, 0);
  307 
  308 #ifdef VIMAGE
  309 static void
  310 vnet_route_uninit(const void *unused __unused)
  311 {
  312         int table;
  313         int fam;
  314         struct domain *dom;
  315         struct rib_head **rnh;
  316 
  317         for (dom = domains; dom; dom = dom->dom_next) {
  318                 if (dom->dom_rtdetach == NULL)
  319                         continue;
  320 
  321                 for (table = 0; table < rt_numfibs; table++) {
  322                         fam = dom->dom_family;
  323 
  324                         if (table != 0 && fam != AF_INET6 && fam != AF_INET)
  325                                 break;
  326 
  327                         rnh = rt_tables_get_rnh_ptr(table, fam);
  328                         if (rnh == NULL)
  329                                 panic("%s: rnh NULL", __func__);
  330                         dom->dom_rtdetach((void **)rnh, 0);
  331                 }
  332         }
  333 
  334         free(V_rt_tables, M_RTABLE);
  335         uma_zdestroy(V_rtzone);
  336 }
  337 VNET_SYSUNINIT(vnet_route_uninit, SI_SUB_PROTO_DOMAIN, SI_ORDER_FIRST,
  338     vnet_route_uninit, 0);
  339 #endif
  340 
  341 struct rib_head *
  342 rt_table_init(int offset)
  343 {
  344         struct rib_head *rh;
  345 
  346         rh = malloc(sizeof(struct rib_head), M_RTABLE, M_WAITOK | M_ZERO);
  347 
  348         /* TODO: These details should be hidded inside radix.c */
  349         /* Init masks tree */
  350         rn_inithead_internal(&rh->head, rh->rnh_nodes, offset);
  351         rn_inithead_internal(&rh->rmhead.head, rh->rmhead.mask_nodes, 0);
  352         rh->head.rnh_masks = &rh->rmhead;
  353 
  354         /* Init locks */
  355         rw_init(&rh->rib_lock, "rib head lock");
  356 
  357         /* Finally, set base callbacks */
  358         rh->rnh_addaddr = rn_addroute;
  359         rh->rnh_deladdr = rn_delete;
  360         rh->rnh_matchaddr = rn_match;
  361         rh->rnh_lookup = rn_lookup;
  362         rh->rnh_walktree = rn_walktree;
  363         rh->rnh_walktree_from = rn_walktree_from;
  364 
  365         return (rh);
  366 }
  367 
  368 static int
  369 rt_freeentry(struct radix_node *rn, void *arg)
  370 {
  371         struct radix_head * const rnh = arg;
  372         struct radix_node *x;
  373 
  374         x = (struct radix_node *)rn_delete(rn + 2, NULL, rnh);
  375         if (x != NULL)
  376                 R_Free(x);
  377         return (0);
  378 }
  379 
  380 void
  381 rt_table_destroy(struct rib_head *rh)
  382 {
  383 
  384         rn_walktree(&rh->rmhead.head, rt_freeentry, &rh->rmhead.head);
  385 
  386         /* Assume table is already empty */
  387         rw_destroy(&rh->rib_lock);
  388         free(rh, M_RTABLE);
  389 }
  390 
  391 
  392 #ifndef _SYS_SYSPROTO_H_
  393 struct setfib_args {
  394         int     fibnum;
  395 };
  396 #endif
  397 int
  398 sys_setfib(struct thread *td, struct setfib_args *uap)
  399 {
  400         if (uap->fibnum < 0 || uap->fibnum >= rt_numfibs)
  401                 return EINVAL;
  402         td->td_proc->p_fibnum = uap->fibnum;
  403         return (0);
  404 }
  405 
  406 /*
  407  * Packet routing routines.
  408  */
  409 void
  410 rtalloc_ign_fib(struct route *ro, u_long ignore, u_int fibnum)
  411 {
  412         struct rtentry *rt;
  413 
  414         if ((rt = ro->ro_rt) != NULL) {
  415                 if (rt->rt_ifp != NULL && rt->rt_flags & RTF_UP)
  416                         return;
  417                 RTFREE(rt);
  418                 ro->ro_rt = NULL;
  419         }
  420         ro->ro_rt = rtalloc1_fib(&ro->ro_dst, 1, ignore, fibnum);
  421         if (ro->ro_rt)
  422                 RT_UNLOCK(ro->ro_rt);
  423 }
  424 
  425 /*
  426  * Look up the route that matches the address given
  427  * Or, at least try.. Create a cloned route if needed.
  428  *
  429  * The returned route, if any, is locked.
  430  */
  431 struct rtentry *
  432 rtalloc1(struct sockaddr *dst, int report, u_long ignflags)
  433 {
  434 
  435         return (rtalloc1_fib(dst, report, ignflags, RT_DEFAULT_FIB));
  436 }
  437 
  438 struct rtentry *
  439 rtalloc1_fib(struct sockaddr *dst, int report, u_long ignflags,
  440                     u_int fibnum)
  441 {
  442         struct rib_head *rh;
  443         struct radix_node *rn;
  444         struct rtentry *newrt;
  445         struct rt_addrinfo info;
  446         int err = 0, msgtype = RTM_MISS;
  447 
  448         KASSERT((fibnum < rt_numfibs), ("rtalloc1_fib: bad fibnum"));
  449         rh = rt_tables_get_rnh(fibnum, dst->sa_family);
  450         newrt = NULL;
  451         if (rh == NULL)
  452                 goto miss;
  453 
  454         /*
  455          * Look up the address in the table for that Address Family
  456          */
  457         if ((ignflags & RTF_RNH_LOCKED) == 0)
  458                 RIB_RLOCK(rh);
  459 #ifdef INVARIANTS
  460         else
  461                 RIB_LOCK_ASSERT(rh);
  462 #endif
  463         rn = rh->rnh_matchaddr(dst, &rh->head);
  464         if (rn && ((rn->rn_flags & RNF_ROOT) == 0)) {
  465                 newrt = RNTORT(rn);
  466                 RT_LOCK(newrt);
  467                 RT_ADDREF(newrt);
  468                 if ((ignflags & RTF_RNH_LOCKED) == 0)
  469                         RIB_RUNLOCK(rh);
  470                 return (newrt);
  471 
  472         } else if ((ignflags & RTF_RNH_LOCKED) == 0)
  473                 RIB_RUNLOCK(rh);
  474         /*
  475          * Either we hit the root or could not find any match,
  476          * which basically means: "cannot get there from here".
  477          */
  478 miss:
  479         V_rtstat.rts_unreach++;
  480 
  481         if (report) {
  482                 /*
  483                  * If required, report the failure to the supervising
  484                  * Authorities.
  485                  * For a delete, this is not an error. (report == 0)
  486                  */
  487                 bzero(&info, sizeof(info));
  488                 info.rti_info[RTAX_DST] = dst;
  489                 rt_missmsg_fib(msgtype, &info, 0, err, fibnum);
  490         }
  491         return (newrt);
  492 }
  493 
  494 /*
  495  * Remove a reference count from an rtentry.
  496  * If the count gets low enough, take it out of the routing table
  497  */
  498 void
  499 rtfree(struct rtentry *rt)
  500 {
  501         struct rib_head *rnh;
  502 
  503         KASSERT(rt != NULL,("%s: NULL rt", __func__));
  504         rnh = rt_tables_get_rnh(rt->rt_fibnum, rt_key(rt)->sa_family);
  505         KASSERT(rnh != NULL,("%s: NULL rnh", __func__));
  506 
  507         RT_LOCK_ASSERT(rt);
  508 
  509         /*
  510          * The callers should use RTFREE_LOCKED() or RTFREE(), so
  511          * we should come here exactly with the last reference.
  512          */
  513         RT_REMREF(rt);
  514         if (rt->rt_refcnt > 0) {
  515                 log(LOG_DEBUG, "%s: %p has %d refs\n", __func__, rt, rt->rt_refcnt);
  516                 goto done;
  517         }
  518 
  519         /*
  520          * On last reference give the "close method" a chance
  521          * to cleanup private state.  This also permits (for
  522          * IPv4 and IPv6) a chance to decide if the routing table
  523          * entry should be purged immediately or at a later time.
  524          * When an immediate purge is to happen the close routine
  525          * typically calls rtexpunge which clears the RTF_UP flag
  526          * on the entry so that the code below reclaims the storage.
  527          */
  528         if (rt->rt_refcnt == 0 && rnh->rnh_close)
  529                 rnh->rnh_close((struct radix_node *)rt, &rnh->head);
  530 
  531         /*
  532          * If we are no longer "up" (and ref == 0)
  533          * then we can free the resources associated
  534          * with the route.
  535          */
  536         if ((rt->rt_flags & RTF_UP) == 0) {
  537                 if (rt->rt_nodes->rn_flags & (RNF_ACTIVE | RNF_ROOT))
  538                         panic("rtfree 2");
  539                 /*
  540                  * the rtentry must have been removed from the routing table
  541                  * so it is represented in rttrash.. remove that now.
  542                  */
  543                 V_rttrash--;
  544 #ifdef  DIAGNOSTIC
  545                 if (rt->rt_refcnt < 0) {
  546                         printf("rtfree: %p not freed (neg refs)\n", rt);
  547                         goto done;
  548                 }
  549 #endif
  550                 /*
  551                  * release references on items we hold them on..
  552                  * e.g other routes and ifaddrs.
  553                  */
  554                 if (rt->rt_ifa)
  555                         ifa_free(rt->rt_ifa);
  556                 /*
  557                  * The key is separatly alloc'd so free it (see rt_setgate()).
  558                  * This also frees the gateway, as they are always malloc'd
  559                  * together.
  560                  */
  561                 R_Free(rt_key(rt));
  562 
  563                 /*
  564                  * and the rtentry itself of course
  565                  */
  566                 uma_zfree(V_rtzone, rt);
  567                 return;
  568         }
  569 done:
  570         RT_UNLOCK(rt);
  571 }
  572 
  573 
  574 /*
  575  * Force a routing table entry to the specified
  576  * destination to go through the given gateway.
  577  * Normally called as a result of a routing redirect
  578  * message from the network layer.
  579  */
  580 void
  581 rtredirect_fib(struct sockaddr *dst,
  582         struct sockaddr *gateway,
  583         struct sockaddr *netmask,
  584         int flags,
  585         struct sockaddr *src,
  586         u_int fibnum)
  587 {
  588         struct rtentry *rt;
  589         int error = 0;
  590         short *stat = NULL;
  591         struct rt_addrinfo info;
  592         struct ifaddr *ifa;
  593         struct rib_head *rnh;
  594 
  595         ifa = NULL;
  596         rnh = rt_tables_get_rnh(fibnum, dst->sa_family);
  597         if (rnh == NULL) {
  598                 error = EAFNOSUPPORT;
  599                 goto out;
  600         }
  601 
  602         /* verify the gateway is directly reachable */
  603         if ((ifa = ifa_ifwithnet(gateway, 0, fibnum)) == NULL) {
  604                 error = ENETUNREACH;
  605                 goto out;
  606         }
  607         rt = rtalloc1_fib(dst, 0, 0UL, fibnum); /* NB: rt is locked */
  608         /*
  609          * If the redirect isn't from our current router for this dst,
  610          * it's either old or wrong.  If it redirects us to ourselves,
  611          * we have a routing loop, perhaps as a result of an interface
  612          * going down recently.
  613          */
  614         if (!(flags & RTF_DONE) && rt) {
  615                 if (!sa_equal(src, rt->rt_gateway)) {
  616                         error = EINVAL;
  617                         goto done;
  618                 }
  619                 if (rt->rt_ifa != ifa && ifa->ifa_addr->sa_family != AF_LINK) {
  620                         error = EINVAL;
  621                         goto done;
  622                 }
  623         }
  624         if ((flags & RTF_GATEWAY) && ifa_ifwithaddr_check(gateway)) {
  625                 error = EHOSTUNREACH;
  626                 goto done;
  627         }
  628         /*
  629          * Create a new entry if we just got back a wildcard entry
  630          * or the lookup failed.  This is necessary for hosts
  631          * which use routing redirects generated by smart gateways
  632          * to dynamically build the routing tables.
  633          */
  634         if (rt == NULL || (rt_mask(rt) && rt_mask(rt)->sa_len < 2))
  635                 goto create;
  636         /*
  637          * Don't listen to the redirect if it's
  638          * for a route to an interface.
  639          */
  640         if (rt->rt_flags & RTF_GATEWAY) {
  641                 if (((rt->rt_flags & RTF_HOST) == 0) && (flags & RTF_HOST)) {
  642                         /*
  643                          * Changing from route to net => route to host.
  644                          * Create new route, rather than smashing route to net.
  645                          */
  646                 create:
  647                         if (rt != NULL)
  648                                 RTFREE_LOCKED(rt);
  649                 
  650                         flags |= RTF_DYNAMIC;
  651                         bzero((caddr_t)&info, sizeof(info));
  652                         info.rti_info[RTAX_DST] = dst;
  653                         info.rti_info[RTAX_GATEWAY] = gateway;
  654                         info.rti_info[RTAX_NETMASK] = netmask;
  655                         info.rti_ifa = ifa;
  656                         info.rti_flags = flags;
  657                         error = rtrequest1_fib(RTM_ADD, &info, &rt, fibnum);
  658                         if (rt != NULL) {
  659                                 RT_LOCK(rt);
  660                                 flags = rt->rt_flags;
  661                         }
  662                         
  663                         stat = &V_rtstat.rts_dynamic;
  664                 } else {
  665 
  666                         /*
  667                          * Smash the current notion of the gateway to
  668                          * this destination.  Should check about netmask!!!
  669                          */
  670                         if ((flags & RTF_GATEWAY) == 0)
  671                                 rt->rt_flags &= ~RTF_GATEWAY;
  672                         rt->rt_flags |= RTF_MODIFIED;
  673                         flags |= RTF_MODIFIED;
  674                         stat = &V_rtstat.rts_newgateway;
  675                         /*
  676                          * add the key and gateway (in one malloc'd chunk).
  677                          */
  678                         RT_UNLOCK(rt);
  679                         RIB_WLOCK(rnh);
  680                         RT_LOCK(rt);
  681                         rt_setgate(rt, rt_key(rt), gateway);
  682                         RIB_WUNLOCK(rnh);
  683                 }
  684         } else
  685                 error = EHOSTUNREACH;
  686 done:
  687         if (rt)
  688                 RTFREE_LOCKED(rt);
  689 out:
  690         if (error)
  691                 V_rtstat.rts_badredirect++;
  692         else if (stat != NULL)
  693                 (*stat)++;
  694         bzero((caddr_t)&info, sizeof(info));
  695         info.rti_info[RTAX_DST] = dst;
  696         info.rti_info[RTAX_GATEWAY] = gateway;
  697         info.rti_info[RTAX_NETMASK] = netmask;
  698         info.rti_info[RTAX_AUTHOR] = src;
  699         rt_missmsg_fib(RTM_REDIRECT, &info, flags, error, fibnum);
  700         if (ifa != NULL)
  701                 ifa_free(ifa);
  702 }
  703 
  704 /*
  705  * Routing table ioctl interface.
  706  */
  707 int
  708 rtioctl_fib(u_long req, caddr_t data, u_int fibnum)
  709 {
  710 
  711         /*
  712          * If more ioctl commands are added here, make sure the proper
  713          * super-user checks are being performed because it is possible for
  714          * prison-root to make it this far if raw sockets have been enabled
  715          * in jails.
  716          */
  717 #ifdef INET
  718         /* Multicast goop, grrr... */
  719         return mrt_ioctl ? mrt_ioctl(req, data, fibnum) : EOPNOTSUPP;
  720 #else /* INET */
  721         return ENXIO;
  722 #endif /* INET */
  723 }
  724 
  725 struct ifaddr *
  726 ifa_ifwithroute(int flags, const struct sockaddr *dst, struct sockaddr *gateway,
  727                                 u_int fibnum)
  728 {
  729         struct ifaddr *ifa;
  730         int not_found = 0;
  731 
  732         if ((flags & RTF_GATEWAY) == 0) {
  733                 /*
  734                  * If we are adding a route to an interface,
  735                  * and the interface is a pt to pt link
  736                  * we should search for the destination
  737                  * as our clue to the interface.  Otherwise
  738                  * we can use the local address.
  739                  */
  740                 ifa = NULL;
  741                 if (flags & RTF_HOST)
  742                         ifa = ifa_ifwithdstaddr(dst, fibnum);
  743                 if (ifa == NULL)
  744                         ifa = ifa_ifwithaddr(gateway);
  745         } else {
  746                 /*
  747                  * If we are adding a route to a remote net
  748                  * or host, the gateway may still be on the
  749                  * other end of a pt to pt link.
  750                  */
  751                 ifa = ifa_ifwithdstaddr(gateway, fibnum);
  752         }
  753         if (ifa == NULL)
  754                 ifa = ifa_ifwithnet(gateway, 0, fibnum);
  755         if (ifa == NULL) {
  756                 struct rtentry *rt;
  757 
  758                 rt = rtalloc1_fib(gateway, 0, flags, fibnum);
  759                 if (rt == NULL)
  760                         return (NULL);
  761                 /*
  762                  * dismiss a gateway that is reachable only
  763                  * through the default router
  764                  */
  765                 switch (gateway->sa_family) {
  766                 case AF_INET:
  767                         if (satosin(rt_key(rt))->sin_addr.s_addr == INADDR_ANY)
  768                                 not_found = 1;
  769                         break;
  770                 case AF_INET6:
  771                         if (IN6_IS_ADDR_UNSPECIFIED(&satosin6(rt_key(rt))->sin6_addr))
  772                                 not_found = 1;
  773                         break;
  774                 default:
  775                         break;
  776                 }
  777                 if (!not_found && rt->rt_ifa != NULL) {
  778                         ifa = rt->rt_ifa;
  779                         ifa_ref(ifa);
  780                 }
  781                 RT_REMREF(rt);
  782                 RT_UNLOCK(rt);
  783                 if (not_found || ifa == NULL)
  784                         return (NULL);
  785         }
  786         if (ifa->ifa_addr->sa_family != dst->sa_family) {
  787                 struct ifaddr *oifa = ifa;
  788                 ifa = ifaof_ifpforaddr(dst, ifa->ifa_ifp);
  789                 if (ifa == NULL)
  790                         ifa = oifa;
  791                 else
  792                         ifa_free(oifa);
  793         }
  794         return (ifa);
  795 }
  796 
  797 /*
  798  * Do appropriate manipulations of a routing tree given
  799  * all the bits of info needed
  800  */
  801 int
  802 rtrequest_fib(int req,
  803         struct sockaddr *dst,
  804         struct sockaddr *gateway,
  805         struct sockaddr *netmask,
  806         int flags,
  807         struct rtentry **ret_nrt,
  808         u_int fibnum)
  809 {
  810         struct rt_addrinfo info;
  811 
  812         if (dst->sa_len == 0)
  813                 return(EINVAL);
  814 
  815         bzero((caddr_t)&info, sizeof(info));
  816         info.rti_flags = flags;
  817         info.rti_info[RTAX_DST] = dst;
  818         info.rti_info[RTAX_GATEWAY] = gateway;
  819         info.rti_info[RTAX_NETMASK] = netmask;
  820         return rtrequest1_fib(req, &info, ret_nrt, fibnum);
  821 }
  822 
  823 
  824 /*
  825  * Copy most of @rt data into @info.
  826  *
  827  * If @flags contains NHR_COPY, copies dst,netmask and gw to the
  828  * pointers specified by @info structure. Assume such pointers
  829  * are zeroed sockaddr-like structures with sa_len field initialized
  830  * to reflect size of the provided buffer. if no NHR_COPY is specified,
  831  * point dst,netmask and gw @info fields to appropriate @rt values.
  832  *
  833  * if @flags contains NHR_REF, do refcouting on rt_ifp.
  834  *
  835  * Returns 0 on success.
  836  */
  837 int
  838 rt_exportinfo(struct rtentry *rt, struct rt_addrinfo *info, int flags)
  839 {
  840         struct rt_metrics *rmx;
  841         struct sockaddr *src, *dst;
  842         int sa_len;
  843 
  844         if (flags & NHR_COPY) {
  845                 /* Copy destination if dst is non-zero */
  846                 src = rt_key(rt);
  847                 dst = info->rti_info[RTAX_DST];
  848                 sa_len = src->sa_len;
  849                 if (dst != NULL) {
  850                         if (src->sa_len > dst->sa_len)
  851                                 return (ENOMEM);
  852                         memcpy(dst, src, src->sa_len);
  853                         info->rti_addrs |= RTA_DST;
  854                 }
  855 
  856                 /* Copy mask if set && dst is non-zero */
  857                 src = rt_mask(rt);
  858                 dst = info->rti_info[RTAX_NETMASK];
  859                 if (src != NULL && dst != NULL) {
  860 
  861                         /*
  862                          * Radix stores different value in sa_len,
  863                          * assume rt_mask() to have the same length
  864                          * as rt_key()
  865                          */
  866                         if (sa_len > dst->sa_len)
  867                                 return (ENOMEM);
  868                         memcpy(dst, src, src->sa_len);
  869                         info->rti_addrs |= RTA_NETMASK;
  870                 }
  871 
  872                 /* Copy gateway is set && dst is non-zero */
  873                 src = rt->rt_gateway;
  874                 dst = info->rti_info[RTAX_GATEWAY];
  875                 if ((rt->rt_flags & RTF_GATEWAY) && src != NULL && dst != NULL){
  876                         if (src->sa_len > dst->sa_len)
  877                                 return (ENOMEM);
  878                         memcpy(dst, src, src->sa_len);
  879                         info->rti_addrs |= RTA_GATEWAY;
  880                 }
  881         } else {
  882                 info->rti_info[RTAX_DST] = rt_key(rt);
  883                 info->rti_addrs |= RTA_DST;
  884                 if (rt_mask(rt) != NULL) {
  885                         info->rti_info[RTAX_NETMASK] = rt_mask(rt);
  886                         info->rti_addrs |= RTA_NETMASK;
  887                 }
  888                 if (rt->rt_flags & RTF_GATEWAY) {
  889                         info->rti_info[RTAX_GATEWAY] = rt->rt_gateway;
  890                         info->rti_addrs |= RTA_GATEWAY;
  891                 }
  892         }
  893 
  894         rmx = info->rti_rmx;
  895         if (rmx != NULL) {
  896                 info->rti_mflags |= RTV_MTU;
  897                 rmx->rmx_mtu = rt->rt_mtu;
  898         }
  899 
  900         info->rti_flags = rt->rt_flags;
  901         info->rti_ifp = rt->rt_ifp;
  902         info->rti_ifa = rt->rt_ifa;
  903 
  904         if (flags & NHR_REF) {
  905                 /* Do 'traditional' refcouting */
  906                 if_ref(info->rti_ifp);
  907         }
  908 
  909         return (0);
  910 }
  911 
  912 /*
  913  * Lookups up route entry for @dst in RIB database for fib @fibnum.
  914  * Exports entry data to @info using rt_exportinfo().
  915  *
  916  * if @flags contains NHR_REF, refcouting is performed on rt_ifp.
  917  *   All references can be released later by calling rib_free_info()
  918  *
  919  * Returns 0 on success.
  920  * Returns ENOENT for lookup failure, ENOMEM for export failure.
  921  */
  922 int
  923 rib_lookup_info(uint32_t fibnum, const struct sockaddr *dst, uint32_t flags,
  924     uint32_t flowid, struct rt_addrinfo *info)
  925 {
  926         struct rib_head *rh;
  927         struct radix_node *rn;
  928         struct rtentry *rt;
  929         int error;
  930 
  931         KASSERT((fibnum < rt_numfibs), ("rib_lookup_rte: bad fibnum"));
  932         rh = rt_tables_get_rnh(fibnum, dst->sa_family);
  933         if (rh == NULL)
  934                 return (ENOENT);
  935 
  936         RIB_RLOCK(rh);
  937         rn = rh->rnh_matchaddr(__DECONST(void *, dst), &rh->head);
  938         if (rn != NULL && ((rn->rn_flags & RNF_ROOT) == 0)) {
  939                 rt = RNTORT(rn);
  940                 /* Ensure route & ifp is UP */
  941                 if (RT_LINK_IS_UP(rt->rt_ifp)) {
  942                         flags = (flags & NHR_REF) | NHR_COPY;
  943                         error = rt_exportinfo(rt, info, flags);
  944                         RIB_RUNLOCK(rh);
  945 
  946                         return (error);
  947                 }
  948         }
  949         RIB_RUNLOCK(rh);
  950 
  951         return (ENOENT);
  952 }
  953 
  954 /*
  955  * Releases all references acquired by rib_lookup_info() when
  956  * called with NHR_REF flags.
  957  */
  958 void
  959 rib_free_info(struct rt_addrinfo *info)
  960 {
  961 
  962         if_rele(info->rti_ifp);
  963 }
  964 
  965 /*
  966  * Iterates over all existing fibs in system calling
  967  *  @setwa_f function prior to traversing each fib.
  968  *  Calls @wa_f function for each element in current fib.
  969  * If af is not AF_UNSPEC, iterates over fibs in particular
  970  * address family.
  971  */
  972 void
  973 rt_foreach_fib_walk(int af, rt_setwarg_t *setwa_f, rt_walktree_f_t *wa_f,
  974     void *arg)
  975 {
  976         struct rib_head *rnh;
  977         uint32_t fibnum;
  978         int i;
  979 
  980         for (fibnum = 0; fibnum < rt_numfibs; fibnum++) {
  981                 /* Do we want some specific family? */
  982                 if (af != AF_UNSPEC) {
  983                         rnh = rt_tables_get_rnh(fibnum, af);
  984                         if (rnh == NULL)
  985                                 continue;
  986                         if (setwa_f != NULL)
  987                                 setwa_f(rnh, fibnum, af, arg);
  988 
  989                         RIB_WLOCK(rnh);
  990                         rnh->rnh_walktree(&rnh->head, (walktree_f_t *)wa_f,arg);
  991                         RIB_WUNLOCK(rnh);
  992                         continue;
  993                 }
  994 
  995                 for (i = 1; i <= AF_MAX; i++) {
  996                         rnh = rt_tables_get_rnh(fibnum, i);
  997                         if (rnh == NULL)
  998                                 continue;
  999                         if (setwa_f != NULL)
 1000                                 setwa_f(rnh, fibnum, i, arg);
 1001 
 1002                         RIB_WLOCK(rnh);
 1003                         rnh->rnh_walktree(&rnh->head, (walktree_f_t *)wa_f,arg);
 1004                         RIB_WUNLOCK(rnh);
 1005                 }
 1006         }
 1007 }
 1008 
 1009 struct rt_delinfo
 1010 {
 1011         struct rt_addrinfo info;
 1012         struct rib_head *rnh;
 1013         struct rtentry *head;
 1014 };
 1015 
 1016 /*
 1017  * Conditionally unlinks @rn from radix tree based
 1018  * on info data passed in @arg.
 1019  */
 1020 static int
 1021 rt_checkdelroute(struct radix_node *rn, void *arg)
 1022 {
 1023         struct rt_delinfo *di;
 1024         struct rt_addrinfo *info;
 1025         struct rtentry *rt;
 1026         int error;
 1027 
 1028         di = (struct rt_delinfo *)arg;
 1029         rt = (struct rtentry *)rn;
 1030         info = &di->info;
 1031         error = 0;
 1032 
 1033         info->rti_info[RTAX_DST] = rt_key(rt);
 1034         info->rti_info[RTAX_NETMASK] = rt_mask(rt);
 1035         info->rti_info[RTAX_GATEWAY] = rt->rt_gateway;
 1036 
 1037         rt = rt_unlinkrte(di->rnh, info, &error);
 1038         if (rt == NULL) {
 1039                 /* Either not allowed or not matched. Skip entry */
 1040                 return (0);
 1041         }
 1042 
 1043         /* Entry was unlinked. Add to the list and return */
 1044         rt->rt_chain = di->head;
 1045         di->head = rt;
 1046 
 1047         return (0);
 1048 }
 1049 
 1050 /*
 1051  * Iterates over all existing fibs in system.
 1052  * Deletes each element for which @filter_f function returned
 1053  * non-zero value.
 1054  * If @af is not AF_UNSPEC, iterates over fibs in particular
 1055  * address family.
 1056  */
 1057 void
 1058 rt_foreach_fib_walk_del(int af, rt_filter_f_t *filter_f, void *arg)
 1059 {
 1060         struct rib_head *rnh;
 1061         struct rt_delinfo di;
 1062         struct rtentry *rt;
 1063         uint32_t fibnum;
 1064         int i, start, end;
 1065 
 1066         bzero(&di, sizeof(di));
 1067         di.info.rti_filter = filter_f;
 1068         di.info.rti_filterdata = arg;
 1069 
 1070         for (fibnum = 0; fibnum < rt_numfibs; fibnum++) {
 1071                 /* Do we want some specific family? */
 1072                 if (af != AF_UNSPEC) {
 1073                         start = af;
 1074                         end = af;
 1075                 } else {
 1076                         start = 1;
 1077                         end = AF_MAX;
 1078                 }
 1079 
 1080                 for (i = start; i <= end; i++) {
 1081                         rnh = rt_tables_get_rnh(fibnum, i);
 1082                         if (rnh == NULL)
 1083                                 continue;
 1084                         di.rnh = rnh;
 1085 
 1086                         RIB_WLOCK(rnh);
 1087                         rnh->rnh_walktree(&rnh->head, rt_checkdelroute, &di);
 1088                         RIB_WUNLOCK(rnh);
 1089 
 1090                         if (di.head == NULL)
 1091                                 continue;
 1092 
 1093                         /* We might have something to reclaim */
 1094                         while (di.head != NULL) {
 1095                                 rt = di.head;
 1096                                 di.head = rt->rt_chain;
 1097                                 rt->rt_chain = NULL;
 1098 
 1099                                 /* TODO std rt -> rt_addrinfo export */
 1100                                 di.info.rti_info[RTAX_DST] = rt_key(rt);
 1101                                 di.info.rti_info[RTAX_NETMASK] = rt_mask(rt);
 1102 
 1103                                 rt_notifydelete(rt, &di.info);
 1104                                 RTFREE_LOCKED(rt);
 1105                         }
 1106 
 1107                 }
 1108         }
 1109 }
 1110 
 1111 /*
 1112  * Delete Routes for a Network Interface
 1113  *
 1114  * Called for each routing entry via the rnh->rnh_walktree() call above
 1115  * to delete all route entries referencing a detaching network interface.
 1116  *
 1117  * Arguments:
 1118  *      rt      pointer to rtentry
 1119  *      arg     argument passed to rnh->rnh_walktree() - detaching interface
 1120  *
 1121  * Returns:
 1122  *      0       successful
 1123  *      errno   failed - reason indicated
 1124  */
 1125 static int
 1126 rt_ifdelroute(const struct rtentry *rt, void *arg)
 1127 {
 1128         struct ifnet    *ifp = arg;
 1129 
 1130         if (rt->rt_ifp != ifp)
 1131                 return (0);
 1132 
 1133         /*
 1134          * Protect (sorta) against walktree recursion problems
 1135          * with cloned routes
 1136          */
 1137         if ((rt->rt_flags & RTF_UP) == 0)
 1138                 return (0);
 1139 
 1140         return (1);
 1141 }
 1142 
 1143 /*
 1144  * Delete all remaining routes using this interface
 1145  * Unfortuneatly the only way to do this is to slog through
 1146  * the entire routing table looking for routes which point
 1147  * to this interface...oh well...
 1148  */
 1149 void
 1150 rt_flushifroutes_af(struct ifnet *ifp, int af)
 1151 {
 1152         KASSERT((af >= 1 && af <= AF_MAX), ("%s: af %d not >= 1 and <= %d",
 1153             __func__, af, AF_MAX));
 1154 
 1155         rt_foreach_fib_walk_del(af, rt_ifdelroute, ifp);
 1156 }
 1157 
 1158 void
 1159 rt_flushifroutes(struct ifnet *ifp)
 1160 {
 1161 
 1162         rt_foreach_fib_walk_del(AF_UNSPEC, rt_ifdelroute, ifp);
 1163 }
 1164 
 1165 /*
 1166  * Conditionally unlinks rtentry matching data inside @info from @rnh.
 1167  * Returns unlinked, locked and referenced @rtentry on success,
 1168  * Returns NULL and sets @perror to:
 1169  * ESRCH - if prefix was not found,
 1170  * EADDRINUSE - if trying to delete PINNED route without appropriate flag.
 1171  * ENOENT - if supplied filter function returned 0 (not matched).
 1172  */
 1173 static struct rtentry *
 1174 rt_unlinkrte(struct rib_head *rnh, struct rt_addrinfo *info, int *perror)
 1175 {
 1176         struct sockaddr *dst, *netmask;
 1177         struct rtentry *rt;
 1178         struct radix_node *rn;
 1179 
 1180         dst = info->rti_info[RTAX_DST];
 1181         netmask = info->rti_info[RTAX_NETMASK];
 1182 
 1183         rt = (struct rtentry *)rnh->rnh_lookup(dst, netmask, &rnh->head);
 1184         if (rt == NULL) {
 1185                 *perror = ESRCH;
 1186                 return (NULL);
 1187         }
 1188 
 1189         if ((info->rti_flags & RTF_PINNED) == 0) {
 1190                 /* Check if target route can be deleted */
 1191                 if (rt->rt_flags & RTF_PINNED) {
 1192                         *perror = EADDRINUSE;
 1193                         return (NULL);
 1194                 }
 1195         }
 1196 
 1197         if (info->rti_filter != NULL) {
 1198                 if (info->rti_filter(rt, info->rti_filterdata) == 0) {
 1199                         /* Not matched */
 1200                         *perror = ENOENT;
 1201                         return (NULL);
 1202                 }
 1203 
 1204                 /*
 1205                  * Filter function requested rte deletion.
 1206                  * Ease the caller work by filling in remaining info
 1207                  * from that particular entry.
 1208                  */
 1209                 info->rti_info[RTAX_GATEWAY] = rt->rt_gateway;
 1210         }
 1211 
 1212         /*
 1213          * Remove the item from the tree and return it.
 1214          * Complain if it is not there and do no more processing.
 1215          */
 1216         *perror = ESRCH;
 1217 #ifdef RADIX_MPATH
 1218         if (rt_mpath_capable(rnh))
 1219                 rn = rt_mpath_unlink(rnh, info, rt, perror);
 1220         else
 1221 #endif
 1222         rn = rnh->rnh_deladdr(dst, netmask, &rnh->head);
 1223         if (rn == NULL)
 1224                 return (NULL);
 1225 
 1226         if (rn->rn_flags & (RNF_ACTIVE | RNF_ROOT))
 1227                 panic ("rtrequest delete");
 1228 
 1229         rt = RNTORT(rn);
 1230         RT_LOCK(rt);
 1231         RT_ADDREF(rt);
 1232         rt->rt_flags &= ~RTF_UP;
 1233 
 1234         *perror = 0;
 1235 
 1236         return (rt);
 1237 }
 1238 
 1239 static void
 1240 rt_notifydelete(struct rtentry *rt, struct rt_addrinfo *info)
 1241 {
 1242         struct ifaddr *ifa;
 1243 
 1244         /*
 1245          * give the protocol a chance to keep things in sync.
 1246          */
 1247         ifa = rt->rt_ifa;
 1248         if (ifa != NULL && ifa->ifa_rtrequest != NULL)
 1249                 ifa->ifa_rtrequest(RTM_DELETE, rt, info);
 1250 
 1251         /*
 1252          * One more rtentry floating around that is not
 1253          * linked to the routing table. rttrash will be decremented
 1254          * when RTFREE(rt) is eventually called.
 1255          */
 1256         V_rttrash++;
 1257 }
 1258 
 1259 
 1260 /*
 1261  * These (questionable) definitions of apparent local variables apply
 1262  * to the next two functions.  XXXXXX!!!
 1263  */
 1264 #define dst     info->rti_info[RTAX_DST]
 1265 #define gateway info->rti_info[RTAX_GATEWAY]
 1266 #define netmask info->rti_info[RTAX_NETMASK]
 1267 #define ifaaddr info->rti_info[RTAX_IFA]
 1268 #define ifpaddr info->rti_info[RTAX_IFP]
 1269 #define flags   info->rti_flags
 1270 
 1271 /*
 1272  * Look up rt_addrinfo for a specific fib.  Note that if rti_ifa is defined,
 1273  * it will be referenced so the caller must free it.
 1274  */
 1275 int
 1276 rt_getifa_fib(struct rt_addrinfo *info, u_int fibnum)
 1277 {
 1278         struct ifaddr *ifa;
 1279         int error = 0;
 1280 
 1281         /*
 1282          * ifp may be specified by sockaddr_dl
 1283          * when protocol address is ambiguous.
 1284          */
 1285         if (info->rti_ifp == NULL && ifpaddr != NULL &&
 1286             ifpaddr->sa_family == AF_LINK &&
 1287             (ifa = ifa_ifwithnet(ifpaddr, 0, fibnum)) != NULL) {
 1288                 info->rti_ifp = ifa->ifa_ifp;
 1289                 ifa_free(ifa);
 1290         }
 1291         if (info->rti_ifa == NULL && ifaaddr != NULL)
 1292                 info->rti_ifa = ifa_ifwithaddr(ifaaddr);
 1293         if (info->rti_ifa == NULL) {
 1294                 struct sockaddr *sa;
 1295 
 1296                 sa = ifaaddr != NULL ? ifaaddr :
 1297                     (gateway != NULL ? gateway : dst);
 1298                 if (sa != NULL && info->rti_ifp != NULL)
 1299                         info->rti_ifa = ifaof_ifpforaddr(sa, info->rti_ifp);
 1300                 else if (dst != NULL && gateway != NULL)
 1301                         info->rti_ifa = ifa_ifwithroute(flags, dst, gateway,
 1302                                                         fibnum);
 1303                 else if (sa != NULL)
 1304                         info->rti_ifa = ifa_ifwithroute(flags, sa, sa,
 1305                                                         fibnum);
 1306         }
 1307         if ((ifa = info->rti_ifa) != NULL) {
 1308                 if (info->rti_ifp == NULL)
 1309                         info->rti_ifp = ifa->ifa_ifp;
 1310         } else
 1311                 error = ENETUNREACH;
 1312         return (error);
 1313 }
 1314 
 1315 static int
 1316 if_updatemtu_cb(struct radix_node *rn, void *arg)
 1317 {
 1318         struct rtentry *rt;
 1319         struct if_mtuinfo *ifmtu;
 1320 
 1321         rt = (struct rtentry *)rn;
 1322         ifmtu = (struct if_mtuinfo *)arg;
 1323 
 1324         if (rt->rt_ifp != ifmtu->ifp)
 1325                 return (0);
 1326 
 1327         if (rt->rt_mtu >= ifmtu->mtu) {
 1328                 /* We have to decrease mtu regardless of flags */
 1329                 rt->rt_mtu = ifmtu->mtu;
 1330                 return (0);
 1331         }
 1332 
 1333         /*
 1334          * New MTU is bigger. Check if are allowed to alter it
 1335          */
 1336         if ((rt->rt_flags & (RTF_FIXEDMTU | RTF_GATEWAY | RTF_HOST)) != 0) {
 1337 
 1338                 /*
 1339                  * Skip routes with user-supplied MTU and
 1340                  * non-interface routes
 1341                  */
 1342                 return (0);
 1343         }
 1344 
 1345         /* We are safe to update route MTU */
 1346         rt->rt_mtu = ifmtu->mtu;
 1347 
 1348         return (0);
 1349 }
 1350 
 1351 void
 1352 rt_updatemtu(struct ifnet *ifp)
 1353 {
 1354         struct if_mtuinfo ifmtu;
 1355         struct rib_head *rnh;
 1356         int i, j;
 1357 
 1358         ifmtu.ifp = ifp;
 1359 
 1360         /*
 1361          * Try to update rt_mtu for all routes using this interface
 1362          * Unfortunately the only way to do this is to traverse all
 1363          * routing tables in all fibs/domains.
 1364          */
 1365         for (i = 1; i <= AF_MAX; i++) {
 1366                 ifmtu.mtu = if_getmtu_family(ifp, i);
 1367                 for (j = 0; j < rt_numfibs; j++) {
 1368                         rnh = rt_tables_get_rnh(j, i);
 1369                         if (rnh == NULL)
 1370                                 continue;
 1371                         RIB_WLOCK(rnh);
 1372                         rnh->rnh_walktree(&rnh->head, if_updatemtu_cb, &ifmtu);
 1373                         RIB_WUNLOCK(rnh);
 1374                 }
 1375         }
 1376 }
 1377 
 1378 
 1379 #if 0
 1380 int p_sockaddr(char *buf, int buflen, struct sockaddr *s);
 1381 int rt_print(char *buf, int buflen, struct rtentry *rt);
 1382 
 1383 int
 1384 p_sockaddr(char *buf, int buflen, struct sockaddr *s)
 1385 {
 1386         void *paddr = NULL;
 1387 
 1388         switch (s->sa_family) {
 1389         case AF_INET:
 1390                 paddr = &((struct sockaddr_in *)s)->sin_addr;
 1391                 break;
 1392         case AF_INET6:
 1393                 paddr = &((struct sockaddr_in6 *)s)->sin6_addr;
 1394                 break;
 1395         }
 1396 
 1397         if (paddr == NULL)
 1398                 return (0);
 1399 
 1400         if (inet_ntop(s->sa_family, paddr, buf, buflen) == NULL)
 1401                 return (0);
 1402         
 1403         return (strlen(buf));
 1404 }
 1405 
 1406 int
 1407 rt_print(char *buf, int buflen, struct rtentry *rt)
 1408 {
 1409         struct sockaddr *addr, *mask;
 1410         int i = 0;
 1411 
 1412         addr = rt_key(rt);
 1413         mask = rt_mask(rt);
 1414 
 1415         i = p_sockaddr(buf, buflen, addr);
 1416         if (!(rt->rt_flags & RTF_HOST)) {
 1417                 buf[i++] = '/';
 1418                 i += p_sockaddr(buf + i, buflen - i, mask);
 1419         }
 1420 
 1421         if (rt->rt_flags & RTF_GATEWAY) {
 1422                 buf[i++] = '>';
 1423                 i += p_sockaddr(buf + i, buflen - i, rt->rt_gateway);
 1424         }
 1425 
 1426         return (i);
 1427 }
 1428 #endif
 1429 
 1430 #ifdef RADIX_MPATH
 1431 /*
 1432  * Deletes key for single-path routes, unlinks rtentry with
 1433  * gateway specified in @info from multi-path routes.
 1434  *
 1435  * Returnes unlinked entry. In case of failure, returns NULL
 1436  * and sets @perror to ESRCH.
 1437  */
 1438 static struct radix_node *
 1439 rt_mpath_unlink(struct rib_head *rnh, struct rt_addrinfo *info,
 1440     struct rtentry *rto, int *perror)
 1441 {
 1442         /*
 1443          * if we got multipath routes, we require users to specify
 1444          * a matching RTAX_GATEWAY.
 1445          */
 1446         struct rtentry *rt; // *rto = NULL;
 1447         struct radix_node *rn;
 1448         struct sockaddr *gw;
 1449 
 1450         gw = info->rti_info[RTAX_GATEWAY];
 1451         rt = rt_mpath_matchgate(rto, gw);
 1452         if (rt == NULL) {
 1453                 *perror = ESRCH;
 1454                 return (NULL);
 1455         }
 1456 
 1457         /*
 1458          * this is the first entry in the chain
 1459          */
 1460         if (rto == rt) {
 1461                 rn = rn_mpath_next((struct radix_node *)rt);
 1462                 /*
 1463                  * there is another entry, now it's active
 1464                  */
 1465                 if (rn) {
 1466                         rto = RNTORT(rn);
 1467                         RT_LOCK(rto);
 1468                         rto->rt_flags |= RTF_UP;
 1469                         RT_UNLOCK(rto);
 1470                 } else if (rt->rt_flags & RTF_GATEWAY) {
 1471                         /*
 1472                          * For gateway routes, we need to 
 1473                          * make sure that we we are deleting
 1474                          * the correct gateway. 
 1475                          * rt_mpath_matchgate() does not 
 1476                          * check the case when there is only
 1477                          * one route in the chain.  
 1478                          */
 1479                         if (gw &&
 1480                             (rt->rt_gateway->sa_len != gw->sa_len ||
 1481                                 memcmp(rt->rt_gateway, gw, gw->sa_len))) {
 1482                                 *perror = ESRCH;
 1483                                 return (NULL);
 1484                         }
 1485                 }
 1486 
 1487                 /*
 1488                  * use the normal delete code to remove
 1489                  * the first entry
 1490                  */
 1491                 rn = rnh->rnh_deladdr(dst, netmask, &rnh->head);
 1492                 *perror = 0;
 1493                 return (rn);
 1494         }
 1495                 
 1496         /*
 1497          * if the entry is 2nd and on up
 1498          */
 1499         if (rt_mpath_deldup(rto, rt) == 0)
 1500                 panic ("rtrequest1: rt_mpath_deldup");
 1501         *perror = 0;
 1502         rn = (struct radix_node *)rt;
 1503         return (rn);
 1504 }
 1505 #endif
 1506 
 1507 #ifdef FLOWTABLE
 1508 static struct rtentry *
 1509 rt_flowtable_check_route(struct rib_head *rnh, struct rt_addrinfo *info)
 1510 {
 1511 #if defined(INET6) || defined(INET)
 1512         struct radix_node *rn;
 1513 #endif
 1514         struct rtentry *rt0;
 1515 
 1516         rt0 = NULL;
 1517         /* "flow-table" only supports IPv6 and IPv4 at the moment. */
 1518         switch (dst->sa_family) {
 1519 #ifdef INET6
 1520         case AF_INET6:
 1521 #endif
 1522 #ifdef INET
 1523         case AF_INET:
 1524 #endif
 1525 #if defined(INET6) || defined(INET)
 1526                 rn = rnh->rnh_matchaddr(dst, &rnh->head);
 1527                 if (rn && ((rn->rn_flags & RNF_ROOT) == 0)) {
 1528                         struct sockaddr *mask;
 1529                         u_char *m, *n;
 1530                         int len;
 1531 
 1532                         /*
 1533                          * compare mask to see if the new route is
 1534                          * more specific than the existing one
 1535                          */
 1536                         rt0 = RNTORT(rn);
 1537                         RT_LOCK(rt0);
 1538                         RT_ADDREF(rt0);
 1539                         RT_UNLOCK(rt0);
 1540                         /*
 1541                          * A host route is already present, so
 1542                          * leave the flow-table entries as is.
 1543                          */
 1544                         if (rt0->rt_flags & RTF_HOST) {
 1545                                 RTFREE(rt0);
 1546                                 rt0 = NULL;
 1547                         } else if (!(flags & RTF_HOST) && netmask) {
 1548                                 mask = rt_mask(rt0);
 1549                                 len = mask->sa_len;
 1550                                 m = (u_char *)mask;
 1551                                 n = (u_char *)netmask;
 1552                                 while (len-- > 0) {
 1553                                         if (*n != *m)
 1554                                                 break;
 1555                                         n++;
 1556                                         m++;
 1557                                 }
 1558                                 if (len == 0 || (*n < *m)) {
 1559                                         RTFREE(rt0);
 1560                                         rt0 = NULL;
 1561                                 }
 1562                         }
 1563                 }
 1564 #endif/* INET6 || INET */
 1565         }
 1566 
 1567         return (rt0);
 1568 }
 1569 #endif
 1570 
 1571 int
 1572 rtrequest1_fib(int req, struct rt_addrinfo *info, struct rtentry **ret_nrt,
 1573                                 u_int fibnum)
 1574 {
 1575         int error = 0;
 1576         struct rtentry *rt, *rt_old;
 1577 #ifdef FLOWTABLE
 1578         struct rtentry *rt0;
 1579 #endif
 1580         struct radix_node *rn;
 1581         struct rib_head *rnh;
 1582         struct ifaddr *ifa;
 1583         struct sockaddr *ndst;
 1584         struct sockaddr_storage mdst;
 1585 
 1586         KASSERT((fibnum < rt_numfibs), ("rtrequest1_fib: bad fibnum"));
 1587         KASSERT((flags & RTF_RNH_LOCKED) == 0, ("rtrequest1_fib: locked"));
 1588         switch (dst->sa_family) {
 1589         case AF_INET6:
 1590         case AF_INET:
 1591                 /* We support multiple FIBs. */
 1592                 break;
 1593         default:
 1594                 fibnum = RT_DEFAULT_FIB;
 1595                 break;
 1596         }
 1597 
 1598         /*
 1599          * Find the correct routing tree to use for this Address Family
 1600          */
 1601         rnh = rt_tables_get_rnh(fibnum, dst->sa_family);
 1602         if (rnh == NULL)
 1603                 return (EAFNOSUPPORT);
 1604 
 1605         /*
 1606          * If we are adding a host route then we don't want to put
 1607          * a netmask in the tree, nor do we want to clone it.
 1608          */
 1609         if (flags & RTF_HOST)
 1610                 netmask = NULL;
 1611 
 1612         switch (req) {
 1613         case RTM_DELETE:
 1614                 if (netmask) {
 1615                         rt_maskedcopy(dst, (struct sockaddr *)&mdst, netmask);
 1616                         dst = (struct sockaddr *)&mdst;
 1617                 }
 1618 
 1619                 RIB_WLOCK(rnh);
 1620                 rt = rt_unlinkrte(rnh, info, &error);
 1621                 RIB_WUNLOCK(rnh);
 1622                 if (error != 0)
 1623                         return (error);
 1624 
 1625                 rt_notifydelete(rt, info);
 1626 
 1627                 /*
 1628                  * If the caller wants it, then it can have it,
 1629                  * but it's up to it to free the rtentry as we won't be
 1630                  * doing it.
 1631                  */
 1632                 if (ret_nrt) {
 1633                         *ret_nrt = rt;
 1634                         RT_UNLOCK(rt);
 1635                 } else
 1636                         RTFREE_LOCKED(rt);
 1637                 break;
 1638         case RTM_RESOLVE:
 1639                 /*
 1640                  * resolve was only used for route cloning
 1641                  * here for compat
 1642                  */
 1643                 break;
 1644         case RTM_ADD:
 1645                 if ((flags & RTF_GATEWAY) && !gateway)
 1646                         return (EINVAL);
 1647                 if (dst && gateway && (dst->sa_family != gateway->sa_family) && 
 1648                     (gateway->sa_family != AF_UNSPEC) && (gateway->sa_family != AF_LINK))
 1649                         return (EINVAL);
 1650 
 1651                 if (info->rti_ifa == NULL) {
 1652                         error = rt_getifa_fib(info, fibnum);
 1653                         if (error)
 1654                                 return (error);
 1655                 } else
 1656                         ifa_ref(info->rti_ifa);
 1657                 ifa = info->rti_ifa;
 1658                 rt = uma_zalloc(V_rtzone, M_NOWAIT);
 1659                 if (rt == NULL) {
 1660                         ifa_free(ifa);
 1661                         return (ENOBUFS);
 1662                 }
 1663                 rt->rt_flags = RTF_UP | flags;
 1664                 rt->rt_fibnum = fibnum;
 1665                 /*
 1666                  * Add the gateway. Possibly re-malloc-ing the storage for it.
 1667                  */
 1668                 if ((error = rt_setgate(rt, dst, gateway)) != 0) {
 1669                         ifa_free(ifa);
 1670                         uma_zfree(V_rtzone, rt);
 1671                         return (error);
 1672                 }
 1673 
 1674                 /*
 1675                  * point to the (possibly newly malloc'd) dest address.
 1676                  */
 1677                 ndst = (struct sockaddr *)rt_key(rt);
 1678 
 1679                 /*
 1680                  * make sure it contains the value we want (masked if needed).
 1681                  */
 1682                 if (netmask) {
 1683                         rt_maskedcopy(dst, ndst, netmask);
 1684                 } else
 1685                         bcopy(dst, ndst, dst->sa_len);
 1686 
 1687                 /*
 1688                  * We use the ifa reference returned by rt_getifa_fib().
 1689                  * This moved from below so that rnh->rnh_addaddr() can
 1690                  * examine the ifa and  ifa->ifa_ifp if it so desires.
 1691                  */
 1692                 rt->rt_ifa = ifa;
 1693                 rt->rt_ifp = ifa->ifa_ifp;
 1694                 rt->rt_weight = 1;
 1695 
 1696                 rt_setmetrics(info, rt);
 1697 
 1698                 RIB_WLOCK(rnh);
 1699                 RT_LOCK(rt);
 1700 #ifdef RADIX_MPATH
 1701                 /* do not permit exactly the same dst/mask/gw pair */
 1702                 if (rt_mpath_capable(rnh) &&
 1703                         rt_mpath_conflict(rnh, rt, netmask)) {
 1704                         RIB_WUNLOCK(rnh);
 1705 
 1706                         ifa_free(rt->rt_ifa);
 1707                         R_Free(rt_key(rt));
 1708                         uma_zfree(V_rtzone, rt);
 1709                         return (EEXIST);
 1710                 }
 1711 #endif
 1712 
 1713 #ifdef FLOWTABLE
 1714                 rt0 = rt_flowtable_check_route(rnh, info);
 1715 #endif /* FLOWTABLE */
 1716 
 1717                 /* XXX mtu manipulation will be done in rnh_addaddr -- itojun */
 1718                 rn = rnh->rnh_addaddr(ndst, netmask, &rnh->head, rt->rt_nodes);
 1719 
 1720                 rt_old = NULL;
 1721                 if (rn == NULL && (info->rti_flags & RTF_PINNED) != 0) {
 1722 
 1723                         /*
 1724                          * Force removal and re-try addition
 1725                          * TODO: better multipath&pinned support
 1726                          */
 1727                         struct sockaddr *info_dst = info->rti_info[RTAX_DST];
 1728                         info->rti_info[RTAX_DST] = ndst;
 1729                         /* Do not delete existing PINNED(interface) routes */
 1730                         info->rti_flags &= ~RTF_PINNED;
 1731                         rt_old = rt_unlinkrte(rnh, info, &error);
 1732                         info->rti_flags |= RTF_PINNED;
 1733                         info->rti_info[RTAX_DST] = info_dst;
 1734                         if (rt_old != NULL)
 1735                                 rn = rnh->rnh_addaddr(ndst, netmask, &rnh->head,
 1736                                     rt->rt_nodes);
 1737                 }
 1738                 RIB_WUNLOCK(rnh);
 1739 
 1740                 if (rt_old != NULL)
 1741                         RT_UNLOCK(rt_old);
 1742 
 1743                 /*
 1744                  * If it still failed to go into the tree,
 1745                  * then un-make it (this should be a function)
 1746                  */
 1747                 if (rn == NULL) {
 1748                         ifa_free(rt->rt_ifa);
 1749                         R_Free(rt_key(rt));
 1750                         uma_zfree(V_rtzone, rt);
 1751 #ifdef FLOWTABLE
 1752                         if (rt0 != NULL)
 1753                                 RTFREE(rt0);
 1754 #endif
 1755                         return (EEXIST);
 1756                 } 
 1757 #ifdef FLOWTABLE
 1758                 else if (rt0 != NULL) {
 1759                         flowtable_route_flush(dst->sa_family, rt0);
 1760                         RTFREE(rt0);
 1761                 }
 1762 #endif
 1763 
 1764                 if (rt_old != NULL) {
 1765                         rt_notifydelete(rt_old, info);
 1766                         RTFREE(rt_old);
 1767                 }
 1768 
 1769                 /*
 1770                  * If this protocol has something to add to this then
 1771                  * allow it to do that as well.
 1772                  */
 1773                 if (ifa->ifa_rtrequest)
 1774                         ifa->ifa_rtrequest(req, rt, info);
 1775 
 1776                 /*
 1777                  * actually return a resultant rtentry and
 1778                  * give the caller a single reference.
 1779                  */
 1780                 if (ret_nrt) {
 1781                         *ret_nrt = rt;
 1782                         RT_ADDREF(rt);
 1783                 }
 1784                 rnh->rnh_gen++;         /* Routing table updated */
 1785                 RT_UNLOCK(rt);
 1786                 break;
 1787         case RTM_CHANGE:
 1788                 RIB_WLOCK(rnh);
 1789                 error = rtrequest1_fib_change(rnh, info, ret_nrt, fibnum);
 1790                 RIB_WUNLOCK(rnh);
 1791                 break;
 1792         default:
 1793                 error = EOPNOTSUPP;
 1794         }
 1795 
 1796         return (error);
 1797 }
 1798 
 1799 #undef dst
 1800 #undef gateway
 1801 #undef netmask
 1802 #undef ifaaddr
 1803 #undef ifpaddr
 1804 #undef flags
 1805 
 1806 static int
 1807 rtrequest1_fib_change(struct rib_head *rnh, struct rt_addrinfo *info,
 1808     struct rtentry **ret_nrt, u_int fibnum)
 1809 {
 1810         struct rtentry *rt = NULL;
 1811         int error = 0;
 1812         int free_ifa = 0;
 1813         int family, mtu;
 1814         struct if_mtuinfo ifmtu;
 1815 
 1816         rt = (struct rtentry *)rnh->rnh_lookup(info->rti_info[RTAX_DST],
 1817             info->rti_info[RTAX_NETMASK], &rnh->head);
 1818 
 1819         if (rt == NULL)
 1820                 return (ESRCH);
 1821 
 1822 #ifdef RADIX_MPATH
 1823         /*
 1824          * If we got multipath routes,
 1825          * we require users to specify a matching RTAX_GATEWAY.
 1826          */
 1827         if (rt_mpath_capable(rnh)) {
 1828                 rt = rt_mpath_matchgate(rt, info->rti_info[RTAX_GATEWAY]);
 1829                 if (rt == NULL)
 1830                         return (ESRCH);
 1831         }
 1832 #endif
 1833 
 1834         RT_LOCK(rt);
 1835 
 1836         rt_setmetrics(info, rt);
 1837 
 1838         /*
 1839          * New gateway could require new ifaddr, ifp;
 1840          * flags may also be different; ifp may be specified
 1841          * by ll sockaddr when protocol address is ambiguous
 1842          */
 1843         if (((rt->rt_flags & RTF_GATEWAY) &&
 1844             info->rti_info[RTAX_GATEWAY] != NULL) ||
 1845             info->rti_info[RTAX_IFP] != NULL ||
 1846             (info->rti_info[RTAX_IFA] != NULL &&
 1847              !sa_equal(info->rti_info[RTAX_IFA], rt->rt_ifa->ifa_addr))) {
 1848                 /*
 1849                  * XXX: Temporarily set RTF_RNH_LOCKED flag in the rti_flags
 1850                  *      to avoid rlock in the ifa_ifwithroute().
 1851                  */
 1852                 info->rti_flags |= RTF_RNH_LOCKED;
 1853                 error = rt_getifa_fib(info, fibnum);
 1854                 info->rti_flags &= ~RTF_RNH_LOCKED;
 1855                 if (info->rti_ifa != NULL)
 1856                         free_ifa = 1;
 1857 
 1858                 if (error != 0)
 1859                         goto bad;
 1860         }
 1861 
 1862         /* Check if outgoing interface has changed */
 1863         if (info->rti_ifa != NULL && info->rti_ifa != rt->rt_ifa &&
 1864             rt->rt_ifa != NULL && rt->rt_ifa->ifa_rtrequest != NULL) {
 1865                 rt->rt_ifa->ifa_rtrequest(RTM_DELETE, rt, info);
 1866                 ifa_free(rt->rt_ifa);
 1867         }
 1868         /* Update gateway address */
 1869         if (info->rti_info[RTAX_GATEWAY] != NULL) {
 1870                 error = rt_setgate(rt, rt_key(rt), info->rti_info[RTAX_GATEWAY]);
 1871                 if (error != 0)
 1872                         goto bad;
 1873 
 1874                 rt->rt_flags &= ~RTF_GATEWAY;
 1875                 rt->rt_flags |= (RTF_GATEWAY & info->rti_flags);
 1876         }
 1877 
 1878         if (info->rti_ifa != NULL && info->rti_ifa != rt->rt_ifa) {
 1879                 ifa_ref(info->rti_ifa);
 1880                 rt->rt_ifa = info->rti_ifa;
 1881                 rt->rt_ifp = info->rti_ifp;
 1882         }
 1883         /* Allow some flags to be toggled on change. */
 1884         rt->rt_flags &= ~RTF_FMASK;
 1885         rt->rt_flags |= info->rti_flags & RTF_FMASK;
 1886 
 1887         if (rt->rt_ifa && rt->rt_ifa->ifa_rtrequest != NULL)
 1888                rt->rt_ifa->ifa_rtrequest(RTM_ADD, rt, info);
 1889 
 1890         /* Alter route MTU if necessary */
 1891         if (rt->rt_ifp != NULL) {
 1892                 family = info->rti_info[RTAX_DST]->sa_family;
 1893                 mtu = if_getmtu_family(rt->rt_ifp, family);
 1894                 /* Set default MTU */
 1895                 if (rt->rt_mtu == 0)
 1896                         rt->rt_mtu = mtu;
 1897                 if (rt->rt_mtu != mtu) {
 1898                         /* Check if we really need to update */
 1899                         ifmtu.ifp = rt->rt_ifp;
 1900                         ifmtu.mtu = mtu;
 1901                         if_updatemtu_cb(rt->rt_nodes, &ifmtu);
 1902                 }
 1903         }
 1904 
 1905         if (ret_nrt) {
 1906                 *ret_nrt = rt;
 1907                 RT_ADDREF(rt);
 1908         }
 1909 bad:
 1910         RT_UNLOCK(rt);
 1911         if (free_ifa != 0)
 1912                 ifa_free(info->rti_ifa);
 1913         return (error);
 1914 }
 1915 
 1916 static void
 1917 rt_setmetrics(const struct rt_addrinfo *info, struct rtentry *rt)
 1918 {
 1919 
 1920         if (info->rti_mflags & RTV_MTU) {
 1921                 if (info->rti_rmx->rmx_mtu != 0) {
 1922 
 1923                         /*
 1924                          * MTU was explicitly provided by user.
 1925                          * Keep it.
 1926                          */
 1927                         rt->rt_flags |= RTF_FIXEDMTU;
 1928                 } else {
 1929 
 1930                         /*
 1931                          * User explicitly sets MTU to 0.
 1932                          * Assume rollback to default.
 1933                          */
 1934                         rt->rt_flags &= ~RTF_FIXEDMTU;
 1935                 }
 1936                 rt->rt_mtu = info->rti_rmx->rmx_mtu;
 1937         }
 1938         if (info->rti_mflags & RTV_WEIGHT)
 1939                 rt->rt_weight = info->rti_rmx->rmx_weight;
 1940         /* Kernel -> userland timebase conversion. */
 1941         if (info->rti_mflags & RTV_EXPIRE)
 1942                 rt->rt_expire = info->rti_rmx->rmx_expire ?
 1943                     info->rti_rmx->rmx_expire - time_second + time_uptime : 0;
 1944 }
 1945 
 1946 int
 1947 rt_setgate(struct rtentry *rt, struct sockaddr *dst, struct sockaddr *gate)
 1948 {
 1949         /* XXX dst may be overwritten, can we move this to below */
 1950         int dlen = SA_SIZE(dst), glen = SA_SIZE(gate);
 1951 
 1952         /*
 1953          * Prepare to store the gateway in rt->rt_gateway.
 1954          * Both dst and gateway are stored one after the other in the same
 1955          * malloc'd chunk. If we have room, we can reuse the old buffer,
 1956          * rt_gateway already points to the right place.
 1957          * Otherwise, malloc a new block and update the 'dst' address.
 1958          */
 1959         if (rt->rt_gateway == NULL || glen > SA_SIZE(rt->rt_gateway)) {
 1960                 caddr_t new;
 1961 
 1962                 R_Malloc(new, caddr_t, dlen + glen);
 1963                 if (new == NULL)
 1964                         return ENOBUFS;
 1965                 /*
 1966                  * XXX note, we copy from *dst and not *rt_key(rt) because
 1967                  * rt_setgate() can be called to initialize a newly
 1968                  * allocated route entry, in which case rt_key(rt) == NULL
 1969                  * (and also rt->rt_gateway == NULL).
 1970                  * Free()/free() handle a NULL argument just fine.
 1971                  */
 1972                 bcopy(dst, new, dlen);
 1973                 R_Free(rt_key(rt));     /* free old block, if any */
 1974                 rt_key(rt) = (struct sockaddr *)new;
 1975                 rt->rt_gateway = (struct sockaddr *)(new + dlen);
 1976         }
 1977 
 1978         /*
 1979          * Copy the new gateway value into the memory chunk.
 1980          */
 1981         bcopy(gate, rt->rt_gateway, glen);
 1982 
 1983         return (0);
 1984 }
 1985 
 1986 void
 1987 rt_maskedcopy(struct sockaddr *src, struct sockaddr *dst, struct sockaddr *netmask)
 1988 {
 1989         u_char *cp1 = (u_char *)src;
 1990         u_char *cp2 = (u_char *)dst;
 1991         u_char *cp3 = (u_char *)netmask;
 1992         u_char *cplim = cp2 + *cp3;
 1993         u_char *cplim2 = cp2 + *cp1;
 1994 
 1995         *cp2++ = *cp1++; *cp2++ = *cp1++; /* copies sa_len & sa_family */
 1996         cp3 += 2;
 1997         if (cplim > cplim2)
 1998                 cplim = cplim2;
 1999         while (cp2 < cplim)
 2000                 *cp2++ = *cp1++ & *cp3++;
 2001         if (cp2 < cplim2)
 2002                 bzero((caddr_t)cp2, (unsigned)(cplim2 - cp2));
 2003 }
 2004 
 2005 /*
 2006  * Set up a routing table entry, normally
 2007  * for an interface.
 2008  */
 2009 #define _SOCKADDR_TMPSIZE 128 /* Not too big.. kernel stack size is limited */
 2010 static inline  int
 2011 rtinit1(struct ifaddr *ifa, int cmd, int flags, int fibnum)
 2012 {
 2013         struct sockaddr *dst;
 2014         struct sockaddr *netmask;
 2015         struct rtentry *rt = NULL;
 2016         struct rt_addrinfo info;
 2017         int error = 0;
 2018         int startfib, endfib;
 2019         char tempbuf[_SOCKADDR_TMPSIZE];
 2020         int didwork = 0;
 2021         int a_failure = 0;
 2022         static struct sockaddr_dl null_sdl = {sizeof(null_sdl), AF_LINK};
 2023         struct rib_head *rnh;
 2024 
 2025         if (flags & RTF_HOST) {
 2026                 dst = ifa->ifa_dstaddr;
 2027                 netmask = NULL;
 2028         } else {
 2029                 dst = ifa->ifa_addr;
 2030                 netmask = ifa->ifa_netmask;
 2031         }
 2032         if (dst->sa_len == 0)
 2033                 return(EINVAL);
 2034         switch (dst->sa_family) {
 2035         case AF_INET6:
 2036         case AF_INET:
 2037                 /* We support multiple FIBs. */
 2038                 break;
 2039         default:
 2040                 fibnum = RT_DEFAULT_FIB;
 2041                 break;
 2042         }
 2043         if (fibnum == RT_ALL_FIBS) {
 2044                 if (V_rt_add_addr_allfibs == 0 && cmd == (int)RTM_ADD)
 2045                         startfib = endfib = ifa->ifa_ifp->if_fib;
 2046                 else {
 2047                         startfib = 0;
 2048                         endfib = rt_numfibs - 1;
 2049                 }
 2050         } else {
 2051                 KASSERT((fibnum < rt_numfibs), ("rtinit1: bad fibnum"));
 2052                 startfib = fibnum;
 2053                 endfib = fibnum;
 2054         }
 2055 
 2056         /*
 2057          * If it's a delete, check that if it exists,
 2058          * it's on the correct interface or we might scrub
 2059          * a route to another ifa which would
 2060          * be confusing at best and possibly worse.
 2061          */
 2062         if (cmd == RTM_DELETE) {
 2063                 /*
 2064                  * It's a delete, so it should already exist..
 2065                  * If it's a net, mask off the host bits
 2066                  * (Assuming we have a mask)
 2067                  * XXX this is kinda inet specific..
 2068                  */
 2069                 if (netmask != NULL) {
 2070                         rt_maskedcopy(dst, (struct sockaddr *)tempbuf, netmask);
 2071                         dst = (struct sockaddr *)tempbuf;
 2072                 }
 2073         }
 2074         /*
 2075          * Now go through all the requested tables (fibs) and do the
 2076          * requested action. Realistically, this will either be fib 0
 2077          * for protocols that don't do multiple tables or all the
 2078          * tables for those that do.
 2079          */
 2080         for ( fibnum = startfib; fibnum <= endfib; fibnum++) {
 2081                 if (cmd == RTM_DELETE) {
 2082                         struct radix_node *rn;
 2083                         /*
 2084                          * Look up an rtentry that is in the routing tree and
 2085                          * contains the correct info.
 2086                          */
 2087                         rnh = rt_tables_get_rnh(fibnum, dst->sa_family);
 2088                         if (rnh == NULL)
 2089                                 /* this table doesn't exist but others might */
 2090                                 continue;
 2091                         RIB_RLOCK(rnh);
 2092                         rn = rnh->rnh_lookup(dst, netmask, &rnh->head);
 2093 #ifdef RADIX_MPATH
 2094                         if (rt_mpath_capable(rnh)) {
 2095 
 2096                                 if (rn == NULL) 
 2097                                         error = ESRCH;
 2098                                 else {
 2099                                         rt = RNTORT(rn);
 2100                                         /*
 2101                                          * for interface route the
 2102                                          * rt->rt_gateway is sockaddr_intf
 2103                                          * for cloning ARP entries, so
 2104                                          * rt_mpath_matchgate must use the
 2105                                          * interface address
 2106                                          */
 2107                                         rt = rt_mpath_matchgate(rt,
 2108                                             ifa->ifa_addr);
 2109                                         if (rt == NULL) 
 2110                                                 error = ESRCH;
 2111                                 }
 2112                         }
 2113 #endif
 2114                         error = (rn == NULL ||
 2115                             (rn->rn_flags & RNF_ROOT) ||
 2116                             RNTORT(rn)->rt_ifa != ifa);
 2117                         RIB_RUNLOCK(rnh);
 2118                         if (error) {
 2119                                 /* this is only an error if bad on ALL tables */
 2120                                 continue;
 2121                         }
 2122                 }
 2123                 /*
 2124                  * Do the actual request
 2125                  */
 2126                 bzero((caddr_t)&info, sizeof(info));
 2127                 info.rti_ifa = ifa;
 2128                 info.rti_flags = flags |
 2129                     (ifa->ifa_flags & ~IFA_RTSELF) | RTF_PINNED;
 2130                 info.rti_info[RTAX_DST] = dst;
 2131                 /* 
 2132                  * doing this for compatibility reasons
 2133                  */
 2134                 if (cmd == RTM_ADD)
 2135                         info.rti_info[RTAX_GATEWAY] =
 2136                             (struct sockaddr *)&null_sdl;
 2137                 else
 2138                         info.rti_info[RTAX_GATEWAY] = ifa->ifa_addr;
 2139                 info.rti_info[RTAX_NETMASK] = netmask;
 2140                 error = rtrequest1_fib(cmd, &info, &rt, fibnum);
 2141 
 2142                 if (error == 0 && rt != NULL) {
 2143                         /*
 2144                          * notify any listening routing agents of the change
 2145                          */
 2146                         RT_LOCK(rt);
 2147 #ifdef RADIX_MPATH
 2148                         /*
 2149                          * in case address alias finds the first address
 2150                          * e.g. ifconfig bge0 192.0.2.246/24
 2151                          * e.g. ifconfig bge0 192.0.2.247/24
 2152                          * the address set in the route is 192.0.2.246
 2153                          * so we need to replace it with 192.0.2.247
 2154                          */
 2155                         if (memcmp(rt->rt_ifa->ifa_addr,
 2156                             ifa->ifa_addr, ifa->ifa_addr->sa_len)) {
 2157                                 ifa_free(rt->rt_ifa);
 2158                                 ifa_ref(ifa);
 2159                                 rt->rt_ifp = ifa->ifa_ifp;
 2160                                 rt->rt_ifa = ifa;
 2161                         }
 2162 #endif
 2163                         /* 
 2164                          * doing this for compatibility reasons
 2165                          */
 2166                         if (cmd == RTM_ADD) {
 2167                             ((struct sockaddr_dl *)rt->rt_gateway)->sdl_type  =
 2168                                 rt->rt_ifp->if_type;
 2169                             ((struct sockaddr_dl *)rt->rt_gateway)->sdl_index =
 2170                                 rt->rt_ifp->if_index;
 2171                         }
 2172                         RT_ADDREF(rt);
 2173                         RT_UNLOCK(rt);
 2174                         rt_newaddrmsg_fib(cmd, ifa, error, rt, fibnum);
 2175                         RT_LOCK(rt);
 2176                         RT_REMREF(rt);
 2177                         if (cmd == RTM_DELETE) {
 2178                                 /*
 2179                                  * If we are deleting, and we found an entry,
 2180                                  * then it's been removed from the tree..
 2181                                  * now throw it away.
 2182                                  */
 2183                                 RTFREE_LOCKED(rt);
 2184                         } else {
 2185                                 if (cmd == RTM_ADD) {
 2186                                         /*
 2187                                          * We just wanted to add it..
 2188                                          * we don't actually need a reference.
 2189                                          */
 2190                                         RT_REMREF(rt);
 2191                                 }
 2192                                 RT_UNLOCK(rt);
 2193                         }
 2194                         didwork = 1;
 2195                 }
 2196                 if (error)
 2197                         a_failure = error;
 2198         }
 2199         if (cmd == RTM_DELETE) {
 2200                 if (didwork) {
 2201                         error = 0;
 2202                 } else {
 2203                         /* we only give an error if it wasn't in any table */
 2204                         error = ((flags & RTF_HOST) ?
 2205                             EHOSTUNREACH : ENETUNREACH);
 2206                 }
 2207         } else {
 2208                 if (a_failure) {
 2209                         /* return an error if any of them failed */
 2210                         error = a_failure;
 2211                 }
 2212         }
 2213         return (error);
 2214 }
 2215 
 2216 /*
 2217  * Set up a routing table entry, normally
 2218  * for an interface.
 2219  */
 2220 int
 2221 rtinit(struct ifaddr *ifa, int cmd, int flags)
 2222 {
 2223         struct sockaddr *dst;
 2224         int fib = RT_DEFAULT_FIB;
 2225 
 2226         if (flags & RTF_HOST) {
 2227                 dst = ifa->ifa_dstaddr;
 2228         } else {
 2229                 dst = ifa->ifa_addr;
 2230         }
 2231 
 2232         switch (dst->sa_family) {
 2233         case AF_INET6:
 2234         case AF_INET:
 2235                 /* We do support multiple FIBs. */
 2236                 fib = RT_ALL_FIBS;
 2237                 break;
 2238         }
 2239         return (rtinit1(ifa, cmd, flags, fib));
 2240 }
 2241 
 2242 /*
 2243  * Announce interface address arrival/withdraw
 2244  * Returns 0 on success.
 2245  */
 2246 int
 2247 rt_addrmsg(int cmd, struct ifaddr *ifa, int fibnum)
 2248 {
 2249 
 2250         KASSERT(cmd == RTM_ADD || cmd == RTM_DELETE,
 2251             ("unexpected cmd %d", cmd));
 2252         
 2253         KASSERT(fibnum == RT_ALL_FIBS || (fibnum >= 0 && fibnum < rt_numfibs),
 2254             ("%s: fib out of range 0 <=%d<%d", __func__, fibnum, rt_numfibs));
 2255 
 2256 #if defined(INET) || defined(INET6)
 2257 #ifdef SCTP
 2258         /*
 2259          * notify the SCTP stack
 2260          * this will only get called when an address is added/deleted
 2261          * XXX pass the ifaddr struct instead if ifa->ifa_addr...
 2262          */
 2263         sctp_addr_change(ifa, cmd);
 2264 #endif /* SCTP */
 2265 #endif
 2266         return (rtsock_addrmsg(cmd, ifa, fibnum));
 2267 }
 2268 
 2269 /*
 2270  * Announce route addition/removal.
 2271  * Users of this function MUST validate input data BEFORE calling.
 2272  * However we have to be able to handle invalid data:
 2273  * if some userland app sends us "invalid" route message (invalid mask,
 2274  * no dst, wrong address families, etc...) we need to pass it back
 2275  * to app (and any other rtsock consumers) with rtm_errno field set to
 2276  * non-zero value.
 2277  * Returns 0 on success.
 2278  */
 2279 int
 2280 rt_routemsg(int cmd, struct ifnet *ifp, int error, struct rtentry *rt,
 2281     int fibnum)
 2282 {
 2283 
 2284         KASSERT(cmd == RTM_ADD || cmd == RTM_DELETE,
 2285             ("unexpected cmd %d", cmd));
 2286         
 2287         KASSERT(fibnum == RT_ALL_FIBS || (fibnum >= 0 && fibnum < rt_numfibs),
 2288             ("%s: fib out of range 0 <=%d<%d", __func__, fibnum, rt_numfibs));
 2289 
 2290         KASSERT(rt_key(rt) != NULL, (":%s: rt_key must be supplied", __func__));
 2291 
 2292         return (rtsock_routemsg(cmd, ifp, error, rt, fibnum));
 2293 }
 2294 
 2295 void
 2296 rt_newaddrmsg(int cmd, struct ifaddr *ifa, int error, struct rtentry *rt)
 2297 {
 2298 
 2299         rt_newaddrmsg_fib(cmd, ifa, error, rt, RT_ALL_FIBS);
 2300 }
 2301 
 2302 /*
 2303  * This is called to generate messages from the routing socket
 2304  * indicating a network interface has had addresses associated with it.
 2305  */
 2306 void
 2307 rt_newaddrmsg_fib(int cmd, struct ifaddr *ifa, int error, struct rtentry *rt,
 2308     int fibnum)
 2309 {
 2310 
 2311         KASSERT(cmd == RTM_ADD || cmd == RTM_DELETE,
 2312                 ("unexpected cmd %u", cmd));
 2313         KASSERT(fibnum == RT_ALL_FIBS || (fibnum >= 0 && fibnum < rt_numfibs),
 2314             ("%s: fib out of range 0 <=%d<%d", __func__, fibnum, rt_numfibs));
 2315 
 2316         if (cmd == RTM_ADD) {
 2317                 rt_addrmsg(cmd, ifa, fibnum);
 2318                 if (rt != NULL)
 2319                         rt_routemsg(cmd, ifa->ifa_ifp, error, rt, fibnum);
 2320         } else {
 2321                 if (rt != NULL)
 2322                         rt_routemsg(cmd, ifa->ifa_ifp, error, rt, fibnum);
 2323                 rt_addrmsg(cmd, ifa, fibnum);
 2324         }
 2325 }
 2326 

Cache object: 95e2638021a0546a7ab031a3d65b1eee


[ 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.