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/netinet/in_rmx.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 1994, 1995 Massachusetts Institute of Technology
    3  *
    4  * Permission to use, copy, modify, and distribute this software and
    5  * its documentation for any purpose and without fee is hereby
    6  * granted, provided that both the above copyright notice and this
    7  * permission notice appear in all copies, that both the above
    8  * copyright notice and this permission notice appear in all
    9  * supporting documentation, and that the name of M.I.T. not be used
   10  * in advertising or publicity pertaining to distribution of the
   11  * software without specific, written prior permission.  M.I.T. makes
   12  * no representations about the suitability of this software for any
   13  * purpose.  It is provided "as is" without express or implied
   14  * warranty.
   15  *
   16  * THIS SOFTWARE IS PROVIDED BY M.I.T. ``AS IS''.  M.I.T. DISCLAIMS
   17  * ALL EXPRESS OR IMPLIED WARRANTIES WITH REGARD TO THIS SOFTWARE,
   18  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
   19  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT
   20  * SHALL M.I.T. BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   21  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   22  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   23  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   24  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   25  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   26  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   27  * SUCH DAMAGE.
   28  *
   29  * $FreeBSD$
   30  */
   31 
   32 /*
   33  * This code does two things necessary for the enhanced TCP metrics to
   34  * function in a useful manner:
   35  *  1) It marks all non-host routes as `cloning', thus ensuring that
   36  *     every actual reference to such a route actually gets turned
   37  *     into a reference to a host route to the specific destination
   38  *     requested.
   39  *  2) When such routes lose all their references, it arranges for them
   40  *     to be deleted in some random collection of circumstances, so that
   41  *     a large quantity of stale routing data is not kept in kernel memory
   42  *     indefinitely.  See in_rtqtimo() below for the exact mechanism.
   43  */
   44 
   45 #include <sys/param.h>
   46 #include <sys/systm.h>
   47 #include <sys/kernel.h>
   48 #include <sys/sysctl.h>
   49 #include <sys/socket.h>
   50 #include <sys/mbuf.h>
   51 #include <sys/syslog.h>
   52 #include <sys/callout.h>
   53 
   54 #include <net/if.h>
   55 #include <net/route.h>
   56 #include <netinet/in.h>
   57 #include <netinet/in_var.h>
   58 #include <netinet/ip_var.h>
   59 
   60 extern int      in_inithead(void **head, int off);
   61 
   62 #define RTPRF_OURS              RTF_PROTO3      /* set on routes we manage */
   63 
   64 /*
   65  * Do what we need to do when inserting a route.
   66  */
   67 static struct radix_node *
   68 in_addroute(void *v_arg, void *n_arg, struct radix_node_head *head,
   69             struct radix_node *treenodes)
   70 {
   71         struct rtentry *rt = (struct rtentry *)treenodes;
   72         struct sockaddr_in *sin = (struct sockaddr_in *)rt_key(rt);
   73         struct radix_node *ret;
   74 
   75         /*
   76          * A little bit of help for both IP output and input:
   77          *   For host routes, we make sure that RTF_BROADCAST
   78          *   is set for anything that looks like a broadcast address.
   79          *   This way, we can avoid an expensive call to in_broadcast()
   80          *   in ip_output() most of the time (because the route passed
   81          *   to ip_output() is almost always a host route).
   82          *
   83          *   We also do the same for local addresses, with the thought
   84          *   that this might one day be used to speed up ip_input().
   85          *
   86          * We also mark routes to multicast addresses as such, because
   87          * it's easy to do and might be useful (but this is much more
   88          * dubious since it's so easy to inspect the address).
   89          */
   90         if (rt->rt_flags & RTF_HOST) {
   91                 if (in_broadcast(sin->sin_addr, rt->rt_ifp)) {
   92                         rt->rt_flags |= RTF_BROADCAST;
   93                 } else if (satosin(rt->rt_ifa->ifa_addr)->sin_addr.s_addr ==
   94                     sin->sin_addr.s_addr) {
   95                         rt->rt_flags |= RTF_LOCAL;
   96                 }
   97         }
   98         if (IN_MULTICAST(ntohl(sin->sin_addr.s_addr)))
   99                 rt->rt_flags |= RTF_MULTICAST;
  100 
  101         if (!rt->rt_rmx.rmx_mtu && rt->rt_ifp)
  102                 rt->rt_rmx.rmx_mtu = rt->rt_ifp->if_mtu;
  103 
  104         ret = rn_addroute(v_arg, n_arg, head, treenodes);
  105         if (ret == NULL && rt->rt_flags & RTF_HOST) {
  106                 struct rtentry *rt2;
  107                 /*
  108                  * We are trying to add a host route, but can't.
  109                  * Find out if it is because of an
  110                  * ARP entry and delete it if so.
  111                  */
  112                 rt2 = rtalloc1((struct sockaddr *)sin, 0, RTF_CLONING);
  113                 if (rt2) {
  114                         if (rt2->rt_flags & RTF_LLINFO &&
  115                             rt2->rt_flags & RTF_HOST &&
  116                             rt2->rt_gateway &&
  117                             rt2->rt_gateway->sa_family == AF_LINK) {
  118                                 rtexpunge(rt2);
  119                                 RTFREE_LOCKED(rt2);
  120                                 ret = rn_addroute(v_arg, n_arg, head,
  121                                                   treenodes);
  122                         } else
  123                                 RTFREE_LOCKED(rt2);
  124                 }
  125         }
  126 
  127         return ret;
  128 }
  129 
  130 /*
  131  * This code is the inverse of in_clsroute: on first reference, if we
  132  * were managing the route, stop doing so and set the expiration timer
  133  * back off again.
  134  */
  135 static struct radix_node *
  136 in_matroute(void *v_arg, struct radix_node_head *head)
  137 {
  138         struct radix_node *rn = rn_match(v_arg, head);
  139         struct rtentry *rt = (struct rtentry *)rn;
  140 
  141         /*XXX locking? */
  142         if (rt && rt->rt_refcnt == 0) {         /* this is first reference */
  143                 if (rt->rt_flags & RTPRF_OURS) {
  144                         rt->rt_flags &= ~RTPRF_OURS;
  145                         rt->rt_rmx.rmx_expire = 0;
  146                 }
  147         }
  148         return rn;
  149 }
  150 
  151 static int rtq_reallyold = 60*60;               /* one hour is "really old" */
  152 SYSCTL_INT(_net_inet_ip, IPCTL_RTEXPIRE, rtexpire, CTLFLAG_RW,
  153     &rtq_reallyold, 0, "Default expiration time on dynamically learned routes");
  154 
  155 static int rtq_minreallyold = 10;  /* never automatically crank down to less */
  156 SYSCTL_INT(_net_inet_ip, IPCTL_RTMINEXPIRE, rtminexpire, CTLFLAG_RW,
  157     &rtq_minreallyold, 0,
  158     "Minimum time to attempt to hold onto dynamically learned routes");
  159 
  160 static int rtq_toomany = 128;           /* 128 cached routes is "too many" */
  161 SYSCTL_INT(_net_inet_ip, IPCTL_RTMAXCACHE, rtmaxcache, CTLFLAG_RW,
  162     &rtq_toomany, 0, "Upper limit on dynamically learned routes");
  163 
  164 /*
  165  * On last reference drop, mark the route as belong to us so that it can be
  166  * timed out.
  167  */
  168 static void
  169 in_clsroute(struct radix_node *rn, struct radix_node_head *head)
  170 {
  171         struct rtentry *rt = (struct rtentry *)rn;
  172 
  173         RT_LOCK_ASSERT(rt);
  174 
  175         if (!(rt->rt_flags & RTF_UP))
  176                 return;                 /* prophylactic measures */
  177 
  178         if ((rt->rt_flags & (RTF_LLINFO | RTF_HOST)) != RTF_HOST)
  179                 return;
  180 
  181         if (rt->rt_flags & RTPRF_OURS)
  182                 return;
  183 
  184         if (!(rt->rt_flags & (RTF_WASCLONED | RTF_DYNAMIC)))
  185                 return;
  186 
  187         /*
  188          * If rtq_reallyold is 0, just delete the route without
  189          * waiting for a timeout cycle to kill it.
  190          */
  191         if (rtq_reallyold != 0) {
  192                 rt->rt_flags |= RTPRF_OURS;
  193                 rt->rt_rmx.rmx_expire = time_second + rtq_reallyold;
  194         } else {
  195                 rtexpunge(rt);
  196         }
  197 }
  198 
  199 struct rtqk_arg {
  200         struct radix_node_head *rnh;
  201         int draining;
  202         int killed;
  203         int found;
  204         int updating;
  205         time_t nextstop;
  206 };
  207 
  208 /*
  209  * Get rid of old routes.  When draining, this deletes everything, even when
  210  * the timeout is not expired yet.  When updating, this makes sure that
  211  * nothing has a timeout longer than the current value of rtq_reallyold.
  212  */
  213 static int
  214 in_rtqkill(struct radix_node *rn, void *rock)
  215 {
  216         struct rtqk_arg *ap = rock;
  217         struct rtentry *rt = (struct rtentry *)rn;
  218         int err;
  219 
  220         if (rt->rt_flags & RTPRF_OURS) {
  221                 ap->found++;
  222 
  223                 if (ap->draining || rt->rt_rmx.rmx_expire <= time_second) {
  224                         if (rt->rt_refcnt > 0)
  225                                 panic("rtqkill route really not free");
  226 
  227                         err = rtrequest(RTM_DELETE,
  228                                         (struct sockaddr *)rt_key(rt),
  229                                         rt->rt_gateway, rt_mask(rt),
  230                                         rt->rt_flags, 0);
  231                         if (err) {
  232                                 log(LOG_WARNING, "in_rtqkill: error %d\n", err);
  233                         } else {
  234                                 ap->killed++;
  235                         }
  236                 } else {
  237                         if (ap->updating &&
  238                             (rt->rt_rmx.rmx_expire - time_second >
  239                              rtq_reallyold)) {
  240                                 rt->rt_rmx.rmx_expire =
  241                                     time_second + rtq_reallyold;
  242                         }
  243                         ap->nextstop = lmin(ap->nextstop,
  244                                             rt->rt_rmx.rmx_expire);
  245                 }
  246         }
  247 
  248         return 0;
  249 }
  250 
  251 #define RTQ_TIMEOUT     60*10   /* run no less than once every ten minutes */
  252 static int rtq_timeout = RTQ_TIMEOUT;
  253 static struct callout rtq_timer;
  254 
  255 static void
  256 in_rtqtimo(void *rock)
  257 {
  258         struct radix_node_head *rnh = rock;
  259         struct rtqk_arg arg;
  260         struct timeval atv;
  261         static time_t last_adjusted_timeout = 0;
  262 
  263         arg.found = arg.killed = 0;
  264         arg.rnh = rnh;
  265         arg.nextstop = time_second + rtq_timeout;
  266         arg.draining = arg.updating = 0;
  267         RADIX_NODE_HEAD_LOCK(rnh);
  268         rnh->rnh_walktree(rnh, in_rtqkill, &arg);
  269         RADIX_NODE_HEAD_UNLOCK(rnh);
  270 
  271         /*
  272          * Attempt to be somewhat dynamic about this:
  273          * If there are ``too many'' routes sitting around taking up space,
  274          * then crank down the timeout, and see if we can't make some more
  275          * go away.  However, we make sure that we will never adjust more
  276          * than once in rtq_timeout seconds, to keep from cranking down too
  277          * hard.
  278          */
  279         if ((arg.found - arg.killed > rtq_toomany) &&
  280             (time_second - last_adjusted_timeout >= rtq_timeout) &&
  281             rtq_reallyold > rtq_minreallyold) {
  282                 rtq_reallyold = 2 * rtq_reallyold / 3;
  283                 if (rtq_reallyold < rtq_minreallyold) {
  284                         rtq_reallyold = rtq_minreallyold;
  285                 }
  286 
  287                 last_adjusted_timeout = time_second;
  288 #ifdef DIAGNOSTIC
  289                 log(LOG_DEBUG, "in_rtqtimo: adjusted rtq_reallyold to %d\n",
  290                     rtq_reallyold);
  291 #endif
  292                 arg.found = arg.killed = 0;
  293                 arg.updating = 1;
  294                 RADIX_NODE_HEAD_LOCK(rnh);
  295                 rnh->rnh_walktree(rnh, in_rtqkill, &arg);
  296                 RADIX_NODE_HEAD_UNLOCK(rnh);
  297         }
  298 
  299         atv.tv_usec = 0;
  300         atv.tv_sec = arg.nextstop - time_second;
  301         callout_reset(&rtq_timer, tvtohz(&atv), in_rtqtimo, rock);
  302 }
  303 
  304 void
  305 in_rtqdrain(void)
  306 {
  307         struct radix_node_head *rnh = rt_tables[AF_INET];
  308         struct rtqk_arg arg;
  309 
  310         arg.found = arg.killed = 0;
  311         arg.rnh = rnh;
  312         arg.nextstop = 0;
  313         arg.draining = 1;
  314         arg.updating = 0;
  315         RADIX_NODE_HEAD_LOCK(rnh);
  316         rnh->rnh_walktree(rnh, in_rtqkill, &arg);
  317         RADIX_NODE_HEAD_UNLOCK(rnh);
  318 }
  319 
  320 /*
  321  * Initialize our routing tree.
  322  */
  323 int
  324 in_inithead(void **head, int off)
  325 {
  326         struct radix_node_head *rnh;
  327 
  328         if (!rn_inithead(head, off))
  329                 return 0;
  330 
  331         if (head != (void **)&rt_tables[AF_INET])       /* BOGUS! */
  332                 return 1;       /* only do this for the real routing table */
  333 
  334         rnh = *head;
  335         rnh->rnh_addaddr = in_addroute;
  336         rnh->rnh_matchaddr = in_matroute;
  337         rnh->rnh_close = in_clsroute;
  338         callout_init(&rtq_timer, CALLOUT_MPSAFE);
  339         in_rtqtimo(rnh);        /* kick off timeout first time */
  340         return 1;
  341 }
  342 
  343 /*
  344  * This zaps old routes when the interface goes down or interface
  345  * address is deleted.  In the latter case, it deletes static routes
  346  * that point to this address.  If we don't do this, we may end up
  347  * using the old address in the future.  The ones we always want to
  348  * get rid of are things like ARP entries, since the user might down
  349  * the interface, walk over to a completely different network, and
  350  * plug back in.
  351  */
  352 struct in_ifadown_arg {
  353         struct radix_node_head *rnh;
  354         struct ifaddr *ifa;
  355         int del;
  356 };
  357 
  358 static int
  359 in_ifadownkill(struct radix_node *rn, void *xap)
  360 {
  361         struct in_ifadown_arg *ap = xap;
  362         struct rtentry *rt = (struct rtentry *)rn;
  363 
  364         RT_LOCK(rt);
  365         if (rt->rt_ifa == ap->ifa &&
  366             (ap->del || !(rt->rt_flags & RTF_STATIC))) {
  367                 /*
  368                  * Aquire a reference so that it can later be freed
  369                  * as the refcount would be 0 here in case of at least
  370                  * ap->del.
  371                  */
  372                 RT_ADDREF(rt);
  373                 /*
  374                  * We need to disable the automatic prune that happens
  375                  * in this case in rtrequest() because it will blow
  376                  * away the pointers that rn_walktree() needs in order
  377                  * continue our descent.  We will end up deleting all
  378                  * the routes that rtrequest() would have in any case,
  379                  * so that behavior is not needed there.
  380                  * Disconnect it from the tree and permit protocols
  381                  * to cleanup.
  382                  */
  383                 rt->rt_flags &= ~RTF_CLONING;
  384                 rtexpunge(rt);
  385                 /*
  386                  * At this point it is an rttrash node, and in case
  387                  * the above is the only reference we must free it.
  388                  * If we do not noone will have a pointer and the
  389                  * rtentry will be leaked forever.
  390                  * In case someone else holds a reference, we are
  391                  * fine as we only decrement the refcount. In that
  392                  * case if the other entity calls RT_REMREF, we
  393                  * will still be leaking but at least we tried.
  394                  */
  395                 RTFREE_LOCKED(rt);
  396                 return (0);
  397         }
  398         RT_UNLOCK(rt);
  399         return 0;
  400 }
  401 
  402 int
  403 in_ifadown(struct ifaddr *ifa, int delete)
  404 {
  405         struct in_ifadown_arg arg;
  406         struct radix_node_head *rnh;
  407 
  408         if (ifa->ifa_addr->sa_family != AF_INET)
  409                 return 1;
  410 
  411         arg.rnh = rnh = rt_tables[AF_INET];
  412         arg.ifa = ifa;
  413         arg.del = delete;
  414         RADIX_NODE_HEAD_LOCK(rnh);
  415         rnh->rnh_walktree(rnh, in_ifadownkill, &arg);
  416         RADIX_NODE_HEAD_UNLOCK(rnh);
  417         ifa->ifa_flags &= ~IFA_ROUTE;           /* XXXlocking? */
  418         return 0;
  419 }

Cache object: 887a9cb75ffe3b328014fc2b6cf05976


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