getpeer
[unix-history] / usr / src / sys / net / route.c
index 076d7e4..2e2f2ee 100644 (file)
@@ -1,70 +1,86 @@
-/*     route.c 4.15    83/02/02        */
+/*     route.c 4.22    83/06/30        */
 
 #include "../h/param.h"
 #include "../h/systm.h"
 #include "../h/mbuf.h"
 #include "../h/protosw.h"
 #include "../h/socket.h"
 
 #include "../h/param.h"
 #include "../h/systm.h"
 #include "../h/mbuf.h"
 #include "../h/protosw.h"
 #include "../h/socket.h"
+#include "../h/dir.h"
+#include "../h/user.h"
 #include "../h/ioctl.h"
 #include "../h/ioctl.h"
+#include "../h/errno.h"
+
 #include "../net/if.h"
 #include "../net/af.h"
 #include "../net/route.h"
 #include "../net/if.h"
 #include "../net/af.h"
 #include "../net/route.h"
-#include <errno.h>
 
 int    rttrash;                /* routes not in table but not freed */
 
 int    rttrash;                /* routes not in table but not freed */
+struct sockaddr wildcard;      /* zero valued cookie for wildcard searches */
+
 /*
  * Packet routing routines.
  */
 rtalloc(ro)
        register struct route *ro;
 {
 /*
  * Packet routing routines.
  */
 rtalloc(ro)
        register struct route *ro;
 {
-       register struct rtentry *rt, *rtmin;
+       register struct rtentry *rt;
        register struct mbuf *m;
        register unsigned hash;
        register struct mbuf *m;
        register unsigned hash;
-       register int (*match)();
-       struct afhash h;
        struct sockaddr *dst = &ro->ro_dst;
        struct sockaddr *dst = &ro->ro_dst;
-       int af = dst->sa_family;
+       int (*match)(), doinghost;
+       struct afhash h;
+       u_int af = dst->sa_family;
+       struct rtentry *rtmin;
+       struct mbuf **table;
 
        if (ro->ro_rt && ro->ro_rt->rt_ifp)                     /* XXX */
                return;
        if (af >= AF_MAX)
                return;
        (*afswitch[af].af_hash)(dst, &h);
 
        if (ro->ro_rt && ro->ro_rt->rt_ifp)                     /* XXX */
                return;
        if (af >= AF_MAX)
                return;
        (*afswitch[af].af_hash)(dst, &h);
-       rtmin = 0, hash = h.afh_hosthash;
-       for (m = rthost[hash % RTHASHSIZ]; m; m = m->m_next) {
-               rt = mtod(m, struct rtentry *);
-               if (rt->rt_hash != hash)
-                       continue;
-               if ((rt->rt_flags & RTF_UP) == 0 ||
-                   (rt->rt_ifp->if_flags & IFF_UP) == 0)
-                       continue;
-               if (bcmp((caddr_t)&rt->rt_dst, (caddr_t)dst, sizeof (*dst)))
-                       continue;
-               if (rtmin == 0 || rt->rt_use < rtmin->rt_use)
-                       rtmin = rt;
-       }
-       if (rtmin) 
-               goto found;
-
-       hash = h.afh_nethash;
+       rtmin = 0;
        match = afswitch[af].af_netmatch;
        match = afswitch[af].af_netmatch;
-       for (m = rtnet[hash % RTHASHSIZ]; m; m = m->m_next) {
+       hash = h.afh_hosthash, table = rthost, doinghost = 1;
+again:
+       for (m = table[hash % RTHASHSIZ]; m; m = m->m_next) {
                rt = mtod(m, struct rtentry *);
                if (rt->rt_hash != hash)
                        continue;
                if ((rt->rt_flags & RTF_UP) == 0 ||
                    (rt->rt_ifp->if_flags & IFF_UP) == 0)
                        continue;
                rt = mtod(m, struct rtentry *);
                if (rt->rt_hash != hash)
                        continue;
                if ((rt->rt_flags & RTF_UP) == 0 ||
                    (rt->rt_ifp->if_flags & IFF_UP) == 0)
                        continue;
-               if (rt->rt_dst.sa_family != af || !(*match)(&rt->rt_dst, dst))
-                       continue;
+               if (doinghost) {
+                       if (bcmp((caddr_t)&rt->rt_dst, (caddr_t)dst,
+                           sizeof (*dst)))
+                               continue;
+               } else {
+                       if (rt->rt_dst.sa_family != af ||
+                           !(*match)(&rt->rt_dst, dst))
+                               continue;
+               }
                if (rtmin == 0 || rt->rt_use < rtmin->rt_use)
                        rtmin = rt;
        }
                if (rtmin == 0 || rt->rt_use < rtmin->rt_use)
                        rtmin = rt;
        }
-found:
+       if (rtmin == 0 && doinghost) {
+               doinghost = 0;
+               hash = h.afh_nethash, table = rtnet;
+               goto again;
+       }
+       /*
+        * Check for wildcard gateway, by convention network 0.
+        */
+       if (rtmin == 0 && dst != &wildcard) {
+               dst = &wildcard, hash = 0;
+               goto again;
+       }
        ro->ro_rt = rtmin;
        ro->ro_rt = rtmin;
-       if (rtmin)
-               rtmin->rt_refcnt++;
+       if (rtmin == 0) {
+               rtstat.rts_unreach++;
+               return;
+       }
+       rtmin->rt_refcnt++;
+       if (dst == &wildcard)
+               rtstat.rts_wildcard++;
 }
 
 rtfree(rt)
 }
 
 rtfree(rt)
@@ -80,10 +96,88 @@ rtfree(rt)
        }
 }
 
        }
 }
 
+/*
+ * Force a routing table entry to the specified
+ * destination to go through the given gateway.
+ * Normally called as a result of a routing redirect
+ * message from the network layer.
+ *
+ * N.B.: must be called at splnet or higher
+ *
+ * Should notify all parties with a reference to
+ * the route that it's changed (so, for instance,
+ * current round trip time estimates could be flushed),
+ * but we have no back pointers at the moment.
+ */
+rtredirect(dst, gateway)
+       struct sockaddr *dst, *gateway;
+{
+       struct route ro;
+       register struct rtentry *rt;
+
+       /* verify the gateway is directly reachable */
+       if (if_ifwithnet(gateway) == 0) {
+               rtstat.rts_badredirect++;
+               return;
+       }
+       ro.ro_dst = *dst;
+       ro.ro_rt = 0;
+       rtalloc(&ro);
+       rt = ro.ro_rt;
+       /*
+        * Create a new entry if we just got back a wildcard entry
+        * or the the lookup failed.  This is necessary for hosts
+        * which use routing redirects generated by smart gateways
+        * to dynamically build the routing tables.
+        */
+       if (rt &&
+           (*afswitch[dst->sa_family].af_netmatch)(&wildcard, &rt->rt_dst)) {
+               rtfree(rt);
+               rt = 0;
+       }
+       if (rt == 0) {
+               rtinit(dst, gateway, RTF_GATEWAY);
+               rtstat.rts_dynamic++;
+               return;
+       }
+       /*
+        * Don't listen to the redirect if it's
+        * for a route to an interface. 
+        */
+       if (rt->rt_flags & RTF_GATEWAY) {
+               /*
+                * Smash the current notion of the gateway to
+                * this destination.  This is probably not right,
+                * as it's conceivable a flurry of redirects could
+                * cause the gateway value to fluctuate wildly during
+                * dynamic routing reconfiguration.
+                */
+               rt->rt_gateway = *gateway;
+               rtfree(rt);
+               rtstat.rts_newgateway++;
+               return;
+       }
+}
+
+/*
+ * Routing table ioctl interface.
+ */
+rtioctl(cmd, data)
+       int cmd;
+       caddr_t data;
+{
+
+       if (cmd != SIOCADDRT && cmd != SIOCDELRT)
+               return (EINVAL);
+       if (!suser())
+               return (u.u_error);
+       return (rtrequest(cmd, (struct rtentry *)data));
+}
+
 /*
  * Carry out a request to change the routing table.  Called by
 /*
  * Carry out a request to change the routing table.  Called by
- * interfaces at boot time to make their ``local routes'' known
- * and for ioctl's.
+ * interfaces at boot time to make their ``local routes'' known,
+ * for ioctl's, and as the result of routing redirects.
  */
 rtrequest(req, entry)
        int req;
  */
 rtrequest(req, entry)
        int req;
@@ -92,7 +186,8 @@ rtrequest(req, entry)
        register struct mbuf *m, **mprev;
        register struct rtentry *rt;
        struct afhash h;
        register struct mbuf *m, **mprev;
        register struct rtentry *rt;
        struct afhash h;
-       int af, s, error = 0, hash, (*match)();
+       int s, error = 0, hash, (*match)();
+       u_int af;
        struct ifnet *ifp;
 
        af = entry->rt_dst.sa_family;
        struct ifnet *ifp;
 
        af = entry->rt_dst.sa_family;
@@ -187,10 +282,17 @@ rtinit(dst, gateway, flags)
        int flags;
 {
        struct rtentry route;
        int flags;
 {
        struct rtentry route;
+       int cmd;
 
 
+       if (flags == -1) {
+               cmd = (int)SIOCDELRT;
+               flags = 0;
+       } else {
+               cmd = (int)SIOCADDRT;
+       }
        bzero((caddr_t)&route, sizeof (route));
        route.rt_dst = *dst;
        route.rt_gateway = *gateway;
        route.rt_flags = flags;
        bzero((caddr_t)&route, sizeof (route));
        route.rt_dst = *dst;
        route.rt_gateway = *gateway;
        route.rt_flags = flags;
-       (void) rtrequest((int)SIOCADDRT, &route);
+       (void) rtrequest(cmd, &route);
 }
 }