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

Cache object: 20be01ac3703dd04be6856a057ee612b


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