lint cleanup (fix sequence number problem)
[unix-history] / usr / src / sbin / routed / tables.c
index db76f3f..8a5cd88 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.7 (Berkeley) %G%";
-#endif
+static char sccsid[] = "@(#)tables.c   5.4 (Berkeley) %G%";
+#endif not lint
 
 /*
  * Routing Table Management Daemon
 
 /*
  * Routing Table Management Daemon
@@ -8,6 +14,7 @@ static char sccsid[] = "@(#)tables.c   4.7 (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,11 +35,11 @@ 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;
-       rh = &hosthash[hash % ROUTEHASHSIZ];
+       rh = &hosthash[hash & ROUTEHASHMASK];
 again:
        for (rt = rh->rt_forw; rt != (struct rt_entry *)rh; rt = rt->rt_forw) {
                if (rt->rt_hash != hash)
 again:
        for (rt = rh->rt_forw; rt != (struct rt_entry *)rh; rt = rt->rt_forw) {
                if (rt->rt_hash != hash)
@@ -43,7 +50,7 @@ again:
        if (doinghost) {
                doinghost = 0;
                hash = h.afh_nethash;
        if (doinghost) {
                doinghost = 0;
                hash = h.afh_nethash;
-               rh = &nethash[hash % ROUTEHASHSIZ];
+               rh = &nethash[hash & ROUTEHASHMASK];
                goto again;
        }
        return (0);
                goto again;
        }
        return (0);
@@ -63,11 +70,11 @@ 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;
-       rh = &hosthash[hash % ROUTEHASHSIZ];
+       rh = &hosthash[hash & ROUTEHASHMASK];
 
 again:
        for (rt = rh->rt_forw; rt != (struct rt_entry *)rh; rt = rt->rt_forw) {
 
 again:
        for (rt = rh->rt_forw; rt != (struct rt_entry *)rh; rt = rt->rt_forw) {
@@ -85,7 +92,7 @@ again:
        if (doinghost) {
                doinghost = 0;
                hash = h.afh_nethash;
        if (doinghost) {
                doinghost = 0;
                hash = h.afh_nethash;
-               rh = &nethash[hash % ROUTEHASHSIZ];
+               rh = &nethash[hash & ROUTEHASHMASK];
                match = afswitch[af].af_netmatch;
                goto again;
        }
                match = afswitch[af].af_netmatch;
                goto again;
        }
@@ -102,16 +109,23 @@ 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;
        if (flags & RTF_HOST) {
                hash = h.afh_hosthash;
-               rh = &hosthash[hash % ROUTEHASHSIZ];
+               rh = &hosthash[hash & ROUTEHASHMASK];
        } else {
                hash = h.afh_nethash;
        } else {
                hash = h.afh_nethash;
-               rh = &nethash[hash % ROUTEHASHSIZ];
+               rh = &nethash[hash & ROUTEHASHMASK];
        }
        rt = (struct rt_entry *)malloc(sizeof (*rt));
        if (rt == 0)
        }
        rt = (struct rt_entry *)malloc(sizeof (*rt));
        if (rt == 0)
@@ -123,7 +137,9 @@ rtadd(dst, gate, metric, state)
        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);
+       rt->rt_ifp = if_ifwithdstaddr(&rt->rt_router);
+       if (rt->rt_ifp == 0)
+               rt->rt_ifp = if_ifwithnet(&rt->rt_router);
        if (metric)
                rt->rt_flags |= RTF_GATEWAY;
        insque(rt, rh);
        if (metric)
                rt->rt_flags |= RTF_GATEWAY;
        insque(rt, rh);
@@ -133,7 +149,8 @@ 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) {
                perror("SIOCADDRT");
                if (errno == ENETUNREACH) {
                        TRACE_ACTION(DELETE, rt);
                perror("SIOCADDRT");
                if (errno == ENETUNREACH) {
                        TRACE_ACTION(DELETE, rt);
@@ -151,7 +168,7 @@ rtchange(rt, gate, metric)
        int doioctl = 0, metricchanged = 0;
        struct rtentry oldroute;
 
        int doioctl = 0, metricchanged = 0;
        struct rtentry oldroute;
 
-       if (!equal(&rt->rt_router, gate))
+       if (!equal(&rt->rt_router, gate) && (rt->rt_state & RTS_INTERNAL) == 0)
                doioctl++;
        if (metric != rt->rt_metric)
                metricchanged++;
                doioctl++;
        if (metric != rt->rt_metric)
                metricchanged++;
@@ -160,11 +177,21 @@ rtchange(rt, gate, metric)
                if (doioctl) {
                        oldroute = rt->rt_rt;
                        rt->rt_router = *gate;
                if (doioctl) {
                        oldroute = rt->rt_rt;
                        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);
                }
                rt->rt_metric = metric;
                }
                rt->rt_metric = metric;
-               rt->rt_state &= ~RTS_INTERFACE;
+               if ((rt->rt_state & RTS_INTERFACE) && metric) {
+                       rt->rt_state &= ~RTS_INTERFACE;
+                       syslog(LOG_ERR,
+                               "changing route from interface %s (timed out)",
+                               rt->rt_ifp->int_name);
+               }
                if (metric)
                if (metric)
-                       rt->rt_state |= RTF_GATEWAY;
+                       rt->rt_flags |= RTF_GATEWAY;
+               else
+                       rt->rt_flags &= ~RTF_GATEWAY;
                rt->rt_state |= RTS_CHANGED;
                TRACE_ACTION(CHANGE TO, rt);
        }
                rt->rt_state |= RTS_CHANGED;
                TRACE_ACTION(CHANGE TO, rt);
        }
@@ -180,8 +207,12 @@ rtdelete(rt)
        struct rt_entry *rt;
 {
 
        struct rt_entry *rt;
 {
 
+       if (rt->rt_state & 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);
@@ -196,26 +227,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 % ROUTEHASHSIZ];
-       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()