MCLALLOC must be called at splimp
[unix-history] / usr / src / sys / net / route.c
index 611f26e..396b6d2 100644 (file)
@@ -1,71 +1,90 @@
-/*     route.c 4.19    83/04/05        */
+/*
+ * Copyright (c) 1980, 1986 Regents of the University of California.
+ * All rights reserved.  The Berkeley software License Agreement
+ * specifies the terms and conditions for redistribution.
+ *
+ *     @(#)route.c     7.1 (Berkeley) %G%
+ */
 
 
-#include "../h/param.h"
-#include "../h/systm.h"
-#include "../h/mbuf.h"
-#include "../h/protosw.h"
-#include "../h/socket.h"
-#include "../h/ioctl.h"
-#include "../h/errno.h"
+#include "param.h"
+#include "systm.h"
+#include "mbuf.h"
+#include "protosw.h"
+#include "socket.h"
+#include "dir.h"
+#include "user.h"
+#include "ioctl.h"
+#include "errno.h"
 
 
-#include "../net/if.h"
-#include "../net/af.h"
-#include "../net/route.h"
+#include "if.h"
+#include "af.h"
+#include "route.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 */
+int    rthashsize = RTHASHSIZ; /* for netstat, etc. */
+
 /*
  * 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 struct mbuf *m;
-       register unsigned hash;
-       register int (*match)();
-       struct afhash h;
+       register u_long hash;
        struct sockaddr *dst = &ro->ro_dst;
        struct sockaddr *dst = &ro->ro_dst;
+       int (*match)(), doinghost, s;
+       struct afhash h;
        u_int af = dst->sa_family;
        u_int af = dst->sa_family;
+       struct mbuf **table;
 
 
-       if (ro->ro_rt && ro->ro_rt->rt_ifp)                     /* XXX */
-               return;
+       if (ro->ro_rt && ro->ro_rt->rt_ifp && (ro->ro_rt->rt_flags & RTF_UP))
+               return;                          /* XXX */
        if (af >= AF_MAX)
                return;
        (*afswitch[af].af_hash)(dst, &h);
        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;
        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;
+       s = splnet();
+again:
+       for (m = table[RTHASHMOD(hash)]; 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 (rtmin == 0 || rt->rt_use < rtmin->rt_use)
-                       rtmin = rt;
+               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;
+               }
+               rt->rt_refcnt++;
+               splx(s);
+               if (dst == &wildcard)
+                       rtstat.rts_wildcard++;
+               ro->ro_rt = rt;
+               return;
+       }
+       if (doinghost) {
+               doinghost = 0;
+               hash = h.afh_nethash, table = rtnet;
+               goto again;
        }
        }
-found:
-       ro->ro_rt = rtmin;
-       if (rtmin)
-               rtmin->rt_refcnt++;
+       /*
+        * Check for wildcard gateway, by convention network 0.
+        */
+       if (dst != &wildcard) {
+               dst = &wildcard, hash = 0;
+               goto again;
+       }
+       splx(s);
+       rtstat.rts_unreach++;
 }
 
 rtfree(rt)
 }
 
 rtfree(rt)
@@ -89,53 +108,113 @@ rtfree(rt)
  *
  * N.B.: must be called at splnet or higher
  *
  *
  * 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,
- * round trip time estimates may be recalculated),
- * but we have no back pointers at the moment.
  */
  */
-rtredirect(dst, gateway)
-       struct sockaddr *dst, *gateway;
+rtredirect(dst, gateway, flags, src)
+       struct sockaddr *dst, *gateway, *src;
+       int flags;
 {
        struct route ro;
        register struct rtentry *rt;
 
        /* verify the gateway is directly reachable */
 {
        struct route ro;
        register struct rtentry *rt;
 
        /* verify the gateway is directly reachable */
-       if (if_ifwithnet(gateway) == 0)
+       if (ifa_ifwithnet(gateway) == 0) {
+               rtstat.rts_badredirect++;
                return;
                return;
+       }
        ro.ro_dst = *dst;
        ro.ro_dst = *dst;
-       ro.ro_rt = NULL;
+       ro.ro_rt = 0;
        rtalloc(&ro);
        rt = ro.ro_rt;
        rtalloc(&ro);
        rt = ro.ro_rt;
+#define        equal(a1, a2) \
+       (bcmp((caddr_t)(a1), (caddr_t)(a2), sizeof(struct sockaddr)) == 0)
+       /*
+        * If the redirect isn't from our current router for this dst,
+        * it's either old or wrong.  If it redirects us to ourselves,
+        * we have a routing loop, perhaps as a result of an interface
+        * going down recently.
+        */
+       if ((rt && !equal(src, &rt->rt_gateway)) || ifa_ifwithaddr(gateway)) {
+               rtstat.rts_badredirect++;
+               if (rt)
+                       rtfree(rt);
+               return;
+       }
+       /*
+        * 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, (int)SIOCADDRT,
+                   (flags & RTF_HOST) | RTF_GATEWAY | RTF_DYNAMIC);
+               rtstat.rts_dynamic++;
+               return;
+       }
        /*
         * Don't listen to the redirect if it's
         * for a route to an interface. 
        /*
         * Don't listen to the redirect if it's
         * for a route to an interface. 
-        *
-        * Should probably create a new entry when
-        * the lookup fails.  This will be necessary
-        * when wildcard routes are added.
         */
         */
-       if (rt == NULL || (rt->rt_flags & RTF_GATEWAY) == 0)
-               return;
-       rt->rt_gateway = *gateway;
+       if (rt->rt_flags & RTF_GATEWAY) {
+               if (((rt->rt_flags & RTF_HOST) == 0) && (flags & RTF_HOST)) {
+                       /*
+                        * Changing from route to net => route to host.
+                        * Create new route, rather than smashing route to net.
+                        */
+                       rtinit(dst, gateway, (int)SIOCADDRT,
+                           flags | RTF_DYNAMIC);
+                       rtstat.rts_dynamic++;
+               } else {
+                       /*
+                        * Smash the current notion of the gateway to
+                        * this destination.
+                        */
+                       rt->rt_gateway = *gateway;
+               }
+               rtstat.rts_newgateway++;
+       } else
+               rtstat.rts_badredirect++;
        rtfree(rt);
 }
 
        rtfree(rt);
 }
 
+/*
+ * 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;
        register struct rtentry *entry;
 {
        register struct mbuf *m, **mprev;
  */
 rtrequest(req, entry)
        int req;
        register struct rtentry *entry;
 {
        register struct mbuf *m, **mprev;
+       struct mbuf **mfirst;
        register struct rtentry *rt;
        struct afhash h;
        register struct rtentry *rt;
        struct afhash h;
-       int s, error = 0, hash, (*match)();
+       int s, error = 0, (*match)();
        u_int af;
        u_int af;
-       struct ifnet *ifp;
+       u_long hash;
+       struct ifaddr *ifa;
+       struct ifaddr *ifa_ifwithdstaddr();
 
        af = entry->rt_dst.sa_family;
        if (af >= AF_MAX)
 
        af = entry->rt_dst.sa_family;
        if (af >= AF_MAX)
@@ -143,20 +222,18 @@ rtrequest(req, entry)
        (*afswitch[af].af_hash)(&entry->rt_dst, &h);
        if (entry->rt_flags & RTF_HOST) {
                hash = h.afh_hosthash;
        (*afswitch[af].af_hash)(&entry->rt_dst, &h);
        if (entry->rt_flags & RTF_HOST) {
                hash = h.afh_hosthash;
-               mprev = &rthost[hash % RTHASHSIZ];
+               mprev = &rthost[RTHASHMOD(hash)];
        } else {
                hash = h.afh_nethash;
        } else {
                hash = h.afh_nethash;
-               mprev = &rtnet[hash % RTHASHSIZ];
+               mprev = &rtnet[RTHASHMOD(hash)];
        }
        match = afswitch[af].af_netmatch;
        s = splimp();
        }
        match = afswitch[af].af_netmatch;
        s = splimp();
-       for (; m = *mprev; mprev = &m->m_next) {
+       for (mfirst = mprev; m = *mprev; mprev = &m->m_next) {
                rt = mtod(m, struct rtentry *);
                if (rt->rt_hash != hash)
                        continue;
                if (entry->rt_flags & RTF_HOST) {
                rt = mtod(m, struct rtentry *);
                if (rt->rt_hash != hash)
                        continue;
                if (entry->rt_flags & RTF_HOST) {
-#define        equal(a1, a2) \
-       (bcmp((caddr_t)(a1), (caddr_t)(a2), sizeof (struct sockaddr)) == 0)
                        if (!equal(&rt->rt_dst, &entry->rt_dst))
                                continue;
                } else {
                        if (!equal(&rt->rt_dst, &entry->rt_dst))
                                continue;
                } else {
@@ -188,10 +265,30 @@ rtrequest(req, entry)
                        error = EEXIST;
                        goto bad;
                }
                        error = EEXIST;
                        goto bad;
                }
-               ifp = if_ifwithaddr(&entry->rt_gateway);
-               if (ifp == 0) {
-                       ifp = if_ifwithnet(&entry->rt_gateway);
-                       if (ifp == 0) {
+               if ((entry->rt_flags & RTF_GATEWAY) == 0) {
+                       /*
+                        * If we are adding a route to an interface,
+                        * and the interface is a pt to pt link
+                        * we should search for the destination
+                        * as our clue to the interface.  Otherwise
+                        * we can use the local address.
+                        */
+                       ifa = 0;
+                       if (entry->rt_flags & RTF_HOST) 
+                               ifa = ifa_ifwithdstaddr(&entry->rt_dst);
+                       if (ifa == 0)
+                               ifa = ifa_ifwithaddr(&entry->rt_gateway);
+               } else {
+                       /*
+                        * If we are adding a route to a remote net
+                        * or host, the gateway may still be on the
+                        * other end of a pt to pt link.
+                        */
+                       ifa = ifa_ifwithdstaddr(&entry->rt_gateway);
+               }
+               if (ifa == 0) {
+                       ifa = ifa_ifwithnet(&entry->rt_gateway);
+                       if (ifa == 0) {
                                error = ENETUNREACH;
                                goto bad;
                        }
                                error = ENETUNREACH;
                                goto bad;
                        }
@@ -201,18 +298,19 @@ rtrequest(req, entry)
                        error = ENOBUFS;
                        goto bad;
                }
                        error = ENOBUFS;
                        goto bad;
                }
-               *mprev = m;
+               m->m_next = *mfirst;
+               *mfirst = m;
                m->m_off = MMINOFF;
                m->m_len = sizeof (struct rtentry);
                rt = mtod(m, struct rtentry *);
                rt->rt_hash = hash;
                rt->rt_dst = entry->rt_dst;
                rt->rt_gateway = entry->rt_gateway;
                m->m_off = MMINOFF;
                m->m_len = sizeof (struct rtentry);
                rt = mtod(m, struct rtentry *);
                rt->rt_hash = hash;
                rt->rt_dst = entry->rt_dst;
                rt->rt_gateway = entry->rt_gateway;
-               rt->rt_flags =
-                   RTF_UP | (entry->rt_flags & (RTF_HOST|RTF_GATEWAY));
+               rt->rt_flags = RTF_UP |
+                   (entry->rt_flags & (RTF_HOST|RTF_GATEWAY|RTF_DYNAMIC));
                rt->rt_refcnt = 0;
                rt->rt_use = 0;
                rt->rt_refcnt = 0;
                rt->rt_use = 0;
-               rt->rt_ifp = ifp;
+               rt->rt_ifp = ifa->ifa_ifp;
                break;
        }
 bad:
                break;
        }
 bad:
@@ -224,19 +322,12 @@ bad:
  * Set up a routing table entry, normally
  * for an interface.
  */
  * Set up a routing table entry, normally
  * for an interface.
  */
-rtinit(dst, gateway, flags)
+rtinit(dst, gateway, cmd, flags)
        struct sockaddr *dst, *gateway;
        struct sockaddr *dst, *gateway;
-       int flags;
+       int cmd, flags;
 {
        struct rtentry route;
 {
        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;
        bzero((caddr_t)&route, sizeof (route));
        route.rt_dst = *dst;
        route.rt_gateway = *gateway;