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: releng/5.1/sys/netinet/in_rmx.c 110656 2003-02-10 22:01:34Z hsu $
   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 
   53 #include <net/if.h>
   54 #include <net/route.h>
   55 #include <netinet/in.h>
   56 #include <netinet/in_var.h>
   57 #include <netinet/ip_var.h>
   58 
   59 extern int      in_inithead(void **head, int off);
   60 
   61 #define RTPRF_OURS              RTF_PROTO3      /* set on routes we manage */
   62 
   63 /*
   64  * Do what we need to do when inserting a route.
   65  */
   66 static struct radix_node *
   67 in_addroute(void *v_arg, void *n_arg, struct radix_node_head *head,
   68             struct radix_node *treenodes)
   69 {
   70         struct rtentry *rt = (struct rtentry *)treenodes;
   71         struct sockaddr_in *sin = (struct sockaddr_in *)rt_key(rt);
   72         struct radix_node *ret;
   73 
   74         /*
   75          * For IP, all unicast non-host routes are automatically cloning.
   76          */
   77         if (IN_MULTICAST(ntohl(sin->sin_addr.s_addr)))
   78                 rt->rt_flags |= RTF_MULTICAST;
   79 
   80         if (!(rt->rt_flags & (RTF_HOST | RTF_CLONING | RTF_MULTICAST)))
   81                 rt->rt_flags |= RTF_PRCLONING;
   82 
   83         /*
   84          * A little bit of help for both IP output and input:
   85          *   For host routes, we make sure that RTF_BROADCAST
   86          *   is set for anything that looks like a broadcast address.
   87          *   This way, we can avoid an expensive call to in_broadcast()
   88          *   in ip_output() most of the time (because the route passed
   89          *   to ip_output() is almost always a host route).
   90          *
   91          *   We also do the same for local addresses, with the thought
   92          *   that this might one day be used to speed up ip_input().
   93          *
   94          * We also mark routes to multicast addresses as such, because
   95          * it's easy to do and might be useful (but this is much more
   96          * dubious since it's so easy to inspect the address).  (This
   97          * is done above.)
   98          */
   99         if (rt->rt_flags & RTF_HOST) {
  100                 if (in_broadcast(sin->sin_addr, rt->rt_ifp)) {
  101                         rt->rt_flags |= RTF_BROADCAST;
  102                 } else if (satosin(rt->rt_ifa->ifa_addr)->sin_addr.s_addr ==
  103                     sin->sin_addr.s_addr) {
  104                         rt->rt_flags |= RTF_LOCAL;
  105                 }
  106         }
  107 
  108         if (!rt->rt_rmx.rmx_mtu && !(rt->rt_rmx.rmx_locks & RTV_MTU) &&
  109             rt->rt_ifp)
  110                 rt->rt_rmx.rmx_mtu = rt->rt_ifp->if_mtu;
  111 
  112         ret = rn_addroute(v_arg, n_arg, head, treenodes);
  113         if (ret == NULL && rt->rt_flags & RTF_HOST) {
  114                 struct rtentry *rt2;
  115                 /*
  116                  * We are trying to add a host route, but can't.
  117                  * Find out if it is because of an
  118                  * ARP entry and delete it if so.
  119                  */
  120                 rt2 = rtalloc1((struct sockaddr *)sin, 0,
  121                                 RTF_CLONING | RTF_PRCLONING);
  122                 if (rt2) {
  123                         if (rt2->rt_flags & RTF_LLINFO &&
  124                             rt2->rt_flags & RTF_HOST &&
  125                             rt2->rt_gateway &&
  126                             rt2->rt_gateway->sa_family == AF_LINK) {
  127                                 rtrequest(RTM_DELETE,
  128                                           (struct sockaddr *)rt_key(rt2),
  129                                           rt2->rt_gateway, rt_mask(rt2),
  130                                           rt2->rt_flags, 0);
  131                                 ret = rn_addroute(v_arg, n_arg, head,
  132                                                   treenodes);
  133                         }
  134                         RTFREE(rt2);
  135                 }
  136         }
  137 
  138         /*
  139          * If the new route created successfully, and we are forwarding,
  140          * and there is a cached route, free it.  Otherwise, we may end
  141          * up using the wrong route.
  142          */
  143         if (ret != NULL && ipforwarding && ipforward_rt.ro_rt) {
  144                 RTFREE(ipforward_rt.ro_rt);
  145                 ipforward_rt.ro_rt = 0;
  146         }
  147 
  148         return ret;
  149 }
  150 
  151 /*
  152  * This code is the inverse of in_clsroute: on first reference, if we
  153  * were managing the route, stop doing so and set the expiration timer
  154  * back off again.
  155  */
  156 static struct radix_node *
  157 in_matroute(void *v_arg, struct radix_node_head *head)
  158 {
  159         struct radix_node *rn = rn_match(v_arg, head);
  160         struct rtentry *rt = (struct rtentry *)rn;
  161 
  162         if (rt && rt->rt_refcnt == 0) {         /* this is first reference */
  163                 if (rt->rt_flags & RTPRF_OURS) {
  164                         rt->rt_flags &= ~RTPRF_OURS;
  165                         rt->rt_rmx.rmx_expire = 0;
  166                 }
  167         }
  168         return rn;
  169 }
  170 
  171 static int rtq_reallyold = 60*60;               /* one hour is "really old" */
  172 SYSCTL_INT(_net_inet_ip, IPCTL_RTEXPIRE, rtexpire, CTLFLAG_RW,
  173     &rtq_reallyold, 0, "Default expiration time on dynamically learned routes");
  174 
  175 static int rtq_minreallyold = 10;  /* never automatically crank down to less */
  176 SYSCTL_INT(_net_inet_ip, IPCTL_RTMINEXPIRE, rtminexpire, CTLFLAG_RW,
  177     &rtq_minreallyold, 0,
  178     "Minimum time to attempt to hold onto dynamically learned routes");
  179 
  180 static int rtq_toomany = 128;           /* 128 cached routes is "too many" */
  181 SYSCTL_INT(_net_inet_ip, IPCTL_RTMAXCACHE, rtmaxcache, CTLFLAG_RW,
  182     &rtq_toomany, 0, "Upper limit on dynamically learned routes");
  183 
  184 /*
  185  * On last reference drop, mark the route as belong to us so that it can be
  186  * timed out.
  187  */
  188 static void
  189 in_clsroute(struct radix_node *rn, struct radix_node_head *head)
  190 {
  191         struct rtentry *rt = (struct rtentry *)rn;
  192 
  193         if (!(rt->rt_flags & RTF_UP))
  194                 return;                 /* prophylactic measures */
  195 
  196         if ((rt->rt_flags & (RTF_LLINFO | RTF_HOST)) != RTF_HOST)
  197                 return;
  198 
  199         if ((rt->rt_flags & (RTF_WASCLONED | RTPRF_OURS)) != RTF_WASCLONED)
  200                 return;
  201 
  202         /*
  203          * If rtq_reallyold is 0, just delete the route without
  204          * waiting for a timeout cycle to kill it.
  205          */
  206         if (rtq_reallyold != 0) {
  207                 rt->rt_flags |= RTPRF_OURS;
  208                 rt->rt_rmx.rmx_expire = time_second + rtq_reallyold;
  209         } else {
  210                 rtrequest(RTM_DELETE,
  211                           (struct sockaddr *)rt_key(rt),
  212                           rt->rt_gateway, rt_mask(rt),
  213                           rt->rt_flags, 0);
  214         }
  215 }
  216 
  217 struct rtqk_arg {
  218         struct radix_node_head *rnh;
  219         int draining;
  220         int killed;
  221         int found;
  222         int updating;
  223         time_t nextstop;
  224 };
  225 
  226 /*
  227  * Get rid of old routes.  When draining, this deletes everything, even when
  228  * the timeout is not expired yet.  When updating, this makes sure that
  229  * nothing has a timeout longer than the current value of rtq_reallyold.
  230  */
  231 static int
  232 in_rtqkill(struct radix_node *rn, void *rock)
  233 {
  234         struct rtqk_arg *ap = rock;
  235         struct rtentry *rt = (struct rtentry *)rn;
  236         int err;
  237 
  238         if (rt->rt_flags & RTPRF_OURS) {
  239                 ap->found++;
  240 
  241                 if (ap->draining || rt->rt_rmx.rmx_expire <= time_second) {
  242                         if (rt->rt_refcnt > 0)
  243                                 panic("rtqkill route really not free");
  244 
  245                         err = rtrequest(RTM_DELETE,
  246                                         (struct sockaddr *)rt_key(rt),
  247                                         rt->rt_gateway, rt_mask(rt),
  248                                         rt->rt_flags, 0);
  249                         if (err) {
  250                                 log(LOG_WARNING, "in_rtqkill: error %d\n", err);
  251                         } else {
  252                                 ap->killed++;
  253                         }
  254                 } else {
  255                         if (ap->updating &&
  256                             (rt->rt_rmx.rmx_expire - time_second >
  257                              rtq_reallyold)) {
  258                                 rt->rt_rmx.rmx_expire =
  259                                     time_second + rtq_reallyold;
  260                         }
  261                         ap->nextstop = lmin(ap->nextstop,
  262                                             rt->rt_rmx.rmx_expire);
  263                 }
  264         }
  265 
  266         return 0;
  267 }
  268 
  269 #define RTQ_TIMEOUT     60*10   /* run no less than once every ten minutes */
  270 static int rtq_timeout = RTQ_TIMEOUT;
  271 
  272 static void
  273 in_rtqtimo(void *rock)
  274 {
  275         struct radix_node_head *rnh = rock;
  276         struct rtqk_arg arg;
  277         struct timeval atv;
  278         static time_t last_adjusted_timeout = 0;
  279         int s;
  280 
  281         arg.found = arg.killed = 0;
  282         arg.rnh = rnh;
  283         arg.nextstop = time_second + rtq_timeout;
  284         arg.draining = arg.updating = 0;
  285         s = splnet();
  286         RADIX_NODE_HEAD_LOCK(rnh);
  287         rnh->rnh_walktree(rnh, in_rtqkill, &arg);
  288         RADIX_NODE_HEAD_UNLOCK(rnh);
  289         splx(s);
  290 
  291         /*
  292          * Attempt to be somewhat dynamic about this:
  293          * If there are ``too many'' routes sitting around taking up space,
  294          * then crank down the timeout, and see if we can't make some more
  295          * go away.  However, we make sure that we will never adjust more
  296          * than once in rtq_timeout seconds, to keep from cranking down too
  297          * hard.
  298          */
  299         if ((arg.found - arg.killed > rtq_toomany) &&
  300             (time_second - last_adjusted_timeout >= rtq_timeout) &&
  301             rtq_reallyold > rtq_minreallyold) {
  302                 rtq_reallyold = 2 * rtq_reallyold / 3;
  303                 if (rtq_reallyold < rtq_minreallyold) {
  304                         rtq_reallyold = rtq_minreallyold;
  305                 }
  306 
  307                 last_adjusted_timeout = time_second;
  308 #ifdef DIAGNOSTIC
  309                 log(LOG_DEBUG, "in_rtqtimo: adjusted rtq_reallyold to %d\n",
  310                     rtq_reallyold);
  311 #endif
  312                 arg.found = arg.killed = 0;
  313                 arg.updating = 1;
  314                 s = splnet();
  315                 RADIX_NODE_HEAD_LOCK(rnh);
  316                 rnh->rnh_walktree(rnh, in_rtqkill, &arg);
  317                 RADIX_NODE_HEAD_UNLOCK(rnh);
  318                 splx(s);
  319         }
  320 
  321         atv.tv_usec = 0;
  322         atv.tv_sec = arg.nextstop - time_second;
  323         timeout(in_rtqtimo, rock, tvtohz(&atv));
  324 }
  325 
  326 void
  327 in_rtqdrain(void)
  328 {
  329         struct radix_node_head *rnh = rt_tables[AF_INET];
  330         struct rtqk_arg arg;
  331         int s;
  332         arg.found = arg.killed = 0;
  333         arg.rnh = rnh;
  334         arg.nextstop = 0;
  335         arg.draining = 1;
  336         arg.updating = 0;
  337         s = splnet();
  338         RADIX_NODE_HEAD_LOCK(rnh);
  339         rnh->rnh_walktree(rnh, in_rtqkill, &arg);
  340         RADIX_NODE_HEAD_UNLOCK(rnh);
  341         splx(s);
  342 }
  343 
  344 /*
  345  * Initialize our routing tree.
  346  */
  347 int
  348 in_inithead(void **head, int off)
  349 {
  350         struct radix_node_head *rnh;
  351 
  352         if (!rn_inithead(head, off))
  353                 return 0;
  354 
  355         if (head != (void **)&rt_tables[AF_INET])       /* BOGUS! */
  356                 return 1;       /* only do this for the real routing table */
  357 
  358         rnh = *head;
  359         rnh->rnh_addaddr = in_addroute;
  360         rnh->rnh_matchaddr = in_matroute;
  361         rnh->rnh_close = in_clsroute;
  362         in_rtqtimo(rnh);        /* kick off timeout first time */
  363         return 1;
  364 }
  365 
  366 /*
  367  * This zaps old routes when the interface goes down or interface
  368  * address is deleted.  In the latter case, it deletes static routes
  369  * that point to this address.  If we don't do this, we may end up
  370  * using the old address in the future.  The ones we always want to
  371  * get rid of are things like ARP entries, since the user might down
  372  * the interface, walk over to a completely different network, and
  373  * plug back in.
  374  */
  375 struct in_ifadown_arg {
  376         struct radix_node_head *rnh;
  377         struct ifaddr *ifa;
  378         int del;
  379 };
  380 
  381 static int
  382 in_ifadownkill(struct radix_node *rn, void *xap)
  383 {
  384         struct in_ifadown_arg *ap = xap;
  385         struct rtentry *rt = (struct rtentry *)rn;
  386         int err;
  387 
  388         if (rt->rt_ifa == ap->ifa &&
  389             (ap->del || !(rt->rt_flags & RTF_STATIC))) {
  390                 /*
  391                  * We need to disable the automatic prune that happens
  392                  * in this case in rtrequest() because it will blow
  393                  * away the pointers that rn_walktree() needs in order
  394                  * continue our descent.  We will end up deleting all
  395                  * the routes that rtrequest() would have in any case,
  396                  * so that behavior is not needed there.
  397                  */
  398                 rt->rt_flags &= ~(RTF_CLONING | RTF_PRCLONING);
  399                 err = rtrequest(RTM_DELETE, (struct sockaddr *)rt_key(rt),
  400                                 rt->rt_gateway, rt_mask(rt), rt->rt_flags, 0);
  401                 if (err) {
  402                         log(LOG_WARNING, "in_ifadownkill: error %d\n", err);
  403                 }
  404         }
  405         return 0;
  406 }
  407 
  408 int
  409 in_ifadown(struct ifaddr *ifa, int delete)
  410 {
  411         struct in_ifadown_arg arg;
  412         struct radix_node_head *rnh;
  413 
  414         if (ifa->ifa_addr->sa_family != AF_INET)
  415                 return 1;
  416 
  417         arg.rnh = rnh = rt_tables[AF_INET];
  418         arg.ifa = ifa;
  419         arg.del = delete;
  420         RADIX_NODE_HEAD_LOCK(rnh);
  421         rnh->rnh_walktree(rnh, in_ifadownkill, &arg);
  422         RADIX_NODE_HEAD_UNLOCK(rnh);
  423         ifa->ifa_flags &= ~IFA_ROUTE;
  424         return 0;
  425 }

Cache object: d65d0f1da871165f4f568061b91da015


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