rework interface metrics to invert perspective; other misc. fixes
[unix-history] / usr / src / sbin / routed / tables.c
index f562087..6b2e18c 100644 (file)
@@ -1,6 +1,12 @@
+/*
+ * Copyright (c) 1983 Regents of the University of California.
+ * All rights reserved.  The Berkeley software License Agreement
+ * specifies the terms and conditions for redistribution.
+ */
+
 #ifndef lint
 #ifndef lint
-static char sccsid[] = "@(#)tables.c   4.8 (Berkeley) %G%";
-#endif
+static char sccsid[] = "@(#)tables.c   5.10 (Berkeley) %G%";
+#endif not lint
 
 /*
  * Routing Table Management Daemon
 
 /*
  * Routing Table Management Daemon
@@ -8,6 +14,7 @@ static char sccsid[] = "@(#)tables.c   4.8 (Berkeley) %G%";
 #include "defs.h"
 #include <sys/ioctl.h>
 #include <errno.h>
 #include "defs.h"
 #include <sys/ioctl.h>
 #include <errno.h>
+#include <syslog.h>
 
 #ifndef DEBUG
 #define        DEBUG   0
 
 #ifndef DEBUG
 #define        DEBUG   0
@@ -28,7 +35,7 @@ rtlookup(dst)
        struct afhash h;
        int doinghost = 1;
 
        struct afhash h;
        int doinghost = 1;
 
-       if (dst->sa_family >= AF_MAX)
+       if (dst->sa_family >= af_max)
                return (0);
        (*afswitch[dst->sa_family].af_hash)(dst, &h);
        hash = h.afh_hosthash;
                return (0);
        (*afswitch[dst->sa_family].af_hash)(dst, &h);
        hash = h.afh_hosthash;
@@ -63,7 +70,7 @@ rtfind(dst)
        int af = dst->sa_family;
        int doinghost = 1, (*match)();
 
        int af = dst->sa_family;
        int doinghost = 1, (*match)();
 
-       if (af >= AF_MAX)
+       if (af >= af_max)
                return (0);
        (*afswitch[af].af_hash)(dst, &h);
        hash = h.afh_hosthash;
                return (0);
        (*afswitch[af].af_hash)(dst, &h);
        hash = h.afh_hosthash;
@@ -102,10 +109,17 @@ rtadd(dst, gate, metric, state)
        int af = dst->sa_family, flags;
        u_int hash;
 
        int af = dst->sa_family, flags;
        u_int hash;
 
-       if (af >= AF_MAX)
+       if (af >= af_max)
                return;
        (*afswitch[af].af_hash)(dst, &h);
                return;
        (*afswitch[af].af_hash)(dst, &h);
-       flags = (*afswitch[af].af_ishost)(dst) ? RTF_HOST : 0;
+       flags = (*afswitch[af].af_rtflags)(dst);
+       /*
+        * Subnet flag isn't visible to kernel, move to state.  XXX
+        */
+       if (flags & RTF_SUBNET) {
+               state |= RTS_SUBNET;
+               flags &= ~RTF_SUBNET;
+       }
        if (flags & RTF_HOST) {
                hash = h.afh_hosthash;
                rh = &hosthash[hash & ROUTEHASHMASK];
        if (flags & RTF_HOST) {
                hash = h.afh_hosthash;
                rh = &hosthash[hash & ROUTEHASHMASK];
@@ -119,13 +133,15 @@ rtadd(dst, gate, metric, state)
        rt->rt_hash = hash;
        rt->rt_dst = *dst;
        rt->rt_router = *gate;
        rt->rt_hash = hash;
        rt->rt_dst = *dst;
        rt->rt_router = *gate;
-       rt->rt_metric = metric;
        rt->rt_timer = 0;
        rt->rt_flags = RTF_UP | flags;
        rt->rt_state = state | RTS_CHANGED;
        rt->rt_timer = 0;
        rt->rt_flags = RTF_UP | flags;
        rt->rt_state = state | RTS_CHANGED;
-       rt->rt_ifp = if_ifwithnet(&rt->rt_router);
-       if (metric)
+       rt->rt_ifp = if_ifwithdstaddr(&rt->rt_router);
+       if (rt->rt_ifp == 0)
+               rt->rt_ifp = if_ifwithnet(&rt->rt_router);
+       if ((state & RTS_INTERFACE) == 0)
                rt->rt_flags |= RTF_GATEWAY;
                rt->rt_flags |= RTF_GATEWAY;
+       rt->rt_metric = metric;
        insque(rt, rh);
        TRACE_ACTION(ADD, rt);
        /*
        insque(rt, rh);
        TRACE_ACTION(ADD, rt);
        /*
@@ -133,7 +149,13 @@ rtadd(dst, gate, metric, state)
         * from this host, discard the entry.  This should only
         * occur because of an incorrect entry in /etc/gateways.
         */
         * from this host, discard the entry.  This should only
         * occur because of an incorrect entry in /etc/gateways.
         */
-       if (install && ioctl(s, SIOCADDRT, (char *)&rt->rt_rt) < 0) {
+       if (install && (rt->rt_state & (RTS_INTERNAL | RTS_EXTERNAL)) == 0 &&
+           ioctl(s, SIOCADDRT, (char *)&rt->rt_rt) < 0) {
+               if (errno != EEXIST && gate->sa_family < af_max)
+                       syslog(LOG_ERR,
+                       "adding route to net/host %s through gateway %s: %m\n",
+                          (*afswitch[dst->sa_family].af_format)(dst),
+                          (*afswitch[gate->sa_family].af_format)(gate));
                perror("SIOCADDRT");
                if (errno == ENETUNREACH) {
                        TRACE_ACTION(DELETE, rt);
                perror("SIOCADDRT");
                if (errno == ENETUNREACH) {
                        TRACE_ACTION(DELETE, rt);
@@ -148,45 +170,103 @@ rtchange(rt, gate, metric)
        struct sockaddr *gate;
        short metric;
 {
        struct sockaddr *gate;
        short metric;
 {
-       int doioctl = 0, metricchanged = 0;
+       int add = 0, delete = 0, metricchanged = 0;
        struct rtentry oldroute;
 
        struct rtentry oldroute;
 
-       if (!equal(&rt->rt_router, gate))
-               doioctl++;
+       if ((rt->rt_state & RTS_INTERNAL) == 0) {
+               /*
+                * If changing to different router, we need to add
+                * new route and delete old one if in the kernel.
+                * If the router is the same, we need to delete
+                * the route if has become unreachable, or re-add
+                * it if it had been unreachable.
+                */
+               if (!equal(&rt->rt_router, gate)) {
+                       add++;
+                       if (rt->rt_metric != HOPCNT_INFINITY)
+                               delete++;
+               } else if (metric == HOPCNT_INFINITY)
+                       delete++;
+               else if (rt->rt_metric == HOPCNT_INFINITY)
+                       add++;
+       }
        if (metric != rt->rt_metric)
                metricchanged++;
        if (metric != rt->rt_metric)
                metricchanged++;
-       if (doioctl || metricchanged) {
+       if (delete || metricchanged)
                TRACE_ACTION(CHANGE FROM, rt);
                TRACE_ACTION(CHANGE FROM, rt);
-               if (doioctl) {
-                       oldroute = rt->rt_rt;
-                       rt->rt_router = *gate;
-               }
-               rt->rt_metric = metric;
+       if ((rt->rt_state & RTS_INTERFACE) && delete) {
                rt->rt_state &= ~RTS_INTERFACE;
                rt->rt_state &= ~RTS_INTERFACE;
-               if (metric)
-                       rt->rt_state |= RTF_GATEWAY;
-               rt->rt_state |= RTS_CHANGED;
-               TRACE_ACTION(CHANGE TO, rt);
+               if (add)
+                       rt->rt_flags |= RTF_GATEWAY;
+               if (metric > rt->rt_metric && delete &&
+                   (rt->rt_state & RTS_INTERNAL) == 0)
+                       syslog(LOG_ERR,
+                               "changing route from interface %s (timed out)",
+                               rt->rt_ifp->int_name);
+       }
+       if (delete)
+               oldroute = rt->rt_rt;
+       if (add) {
+               rt->rt_router = *gate;
+               rt->rt_ifp = if_ifwithdstaddr(&rt->rt_router);
+               if (rt->rt_ifp == 0)
+                       rt->rt_ifp = if_ifwithnet(&rt->rt_router);
        }
        }
-       if (doioctl && install) {
+       rt->rt_metric = metric;
+       rt->rt_state |= RTS_CHANGED;
+       if (add || metricchanged)
+               TRACE_ACTION(CHANGE TO, rt);
+       if (add && install)
                if (ioctl(s, SIOCADDRT, (char *)&rt->rt_rt) < 0)
                        perror("SIOCADDRT");
                if (ioctl(s, SIOCADDRT, (char *)&rt->rt_rt) < 0)
                        perror("SIOCADDRT");
+       if (delete && install)
                if (ioctl(s, SIOCDELRT, (char *)&oldroute) < 0)
                        perror("SIOCDELRT");
                if (ioctl(s, SIOCDELRT, (char *)&oldroute) < 0)
                        perror("SIOCDELRT");
-       }
 }
 
 rtdelete(rt)
        struct rt_entry *rt;
 {
 
 }
 
 rtdelete(rt)
        struct rt_entry *rt;
 {
 
+       if ((rt->rt_state & (RTS_INTERFACE|RTS_INTERNAL)) == RTS_INTERFACE)
+               syslog(LOG_ERR, "deleting route to interface %s (timed out)",
+                       rt->rt_ifp->int_name);
        TRACE_ACTION(DELETE, rt);
        TRACE_ACTION(DELETE, rt);
-       if (install && ioctl(s, SIOCDELRT, (char *)&rt->rt_rt))
+       if (install && (rt->rt_state & (RTS_INTERNAL | RTS_EXTERNAL)) == 0 &&
+           ioctl(s, SIOCDELRT, (char *)&rt->rt_rt))
                perror("SIOCDELRT");
        remque(rt);
        free((char *)rt);
 }
 
                perror("SIOCDELRT");
        remque(rt);
        free((char *)rt);
 }
 
+rtdeleteall(s)
+       int s;
+{
+       register struct rthash *rh;
+       register struct rt_entry *rt;
+       struct rthash *base = hosthash;
+       int doinghost = 1;
+
+again:
+       for (rh = base; rh < &base[ROUTEHASHSIZ]; rh++) {
+               rt = rh->rt_forw;
+               for (; rt != (struct rt_entry *)rh; rt = rt->rt_forw) {
+                       if (rt->rt_state & RTS_INTERFACE)
+                               continue;
+                       TRACE_ACTION(DELETE, rt);
+                       if ((rt->rt_state & (RTS_INTERNAL|RTS_EXTERNAL)) == 0 &&
+                           ioctl(s, SIOCDELRT, (char *)&rt->rt_rt))
+                               perror("SIOCDELRT");
+               }
+       }
+       if (doinghost) {
+               doinghost = 0;
+               base = nethash;
+               goto again;
+       }
+       hup(s);
+}
+
 /*
  * If we have an interface to the wide, wide world,
  * add an entry for an Internet default route (wildcard) to the internal
 /*
  * If we have an interface to the wide, wide world,
  * add an entry for an Internet default route (wildcard) to the internal
@@ -196,26 +276,10 @@ rtdelete(rt)
  */
 rtdefault()
 {
  */
 rtdefault()
 {
-       struct afhash h;
-       register struct rt_entry *rt;
-       struct rthash *rh;
        extern struct sockaddr inet_default;
 
        extern struct sockaddr inet_default;
 
-       rt = (struct rt_entry *)malloc(sizeof (*rt));
-       if (rt == 0)
-               return;
-       rt->rt_hash = h.afh_nethash;
-       rt->rt_dst = inet_default;
-       rt->rt_router = rt->rt_dst;
-       (*afswitch[AF_INET].af_hash)(&rt->rt_dst, &h);
-       rh = &nethash[h.afh_nethash & ROUTEHASHMASK];
-       rt->rt_metric = 0;
-       rt->rt_timer = 0;
-       rt->rt_flags = RTF_UP | RTF_GATEWAY;
-       rt->rt_state = RTS_CHANGED | RTS_PASSIVE;
-       rt->rt_ifp = 0;
-       insque(rt, rh);
-       TRACE_ACTION(ADD, rt);
+       rtadd(&inet_default, &inet_default, 0,
+               RTS_CHANGED | RTS_PASSIVE | RTS_INTERNAL);
 }
 
 rtinit()
 }
 
 rtinit()