add Berkeley header
[unix-history] / usr / src / sys / net / route.c
index 5734e51..7198fa9 100644 (file)
-/*     route.c 4.1     82/03/27        */
-
-#include "../h/param.h"
-#include "../h/mbuf.h"
-#include "../h/protosw.h"
-#include "../h/socket.h"
-#include "../h/socketvar.h"
-#include "../net/in.h"
-#include "../net/in_systm.h"
-#include "../net/af.h"
-#include "../net/route.h"
-#include <errno.h>
-
 /*
 /*
- * Packet routing routines.
+ * Copyright (c) 1980, 1986 Regents of the University of California.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms are permitted
+ * provided that this notice is preserved and that due credit is given
+ * to the University of California at Berkeley. The name of the University
+ * may not be used to endorse or promote products derived from this
+ * software without specific prior written permission. This software
+ * is provided ``as is'' without express or implied warranty.
+ *
+ *     @(#)route.c     7.3 (Berkeley) %G%
  */
 
  */
 
+#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 "if.h"
+#include "af.h"
+#include "route.h"
+
+int    rttrash;                /* routes not in table but not freed */
+struct sockaddr wildcard;      /* zero valued cookie for wildcard searches */
+int    rthashsize = RTHASHSIZ; /* for netstat, etc. */
+
 /*
 /*
- * With much ado about nothing...
- * route the cars that climb halfway to the stars...
+ * Packet routing routines.
  */
  */
-route(ro)
+rtalloc(ro)
        register struct route *ro;
 {
        register struct route *ro;
 {
-       register struct rtentry *rt, *rtmin;
+       register struct rtentry *rt;
        register struct mbuf *m;
        register struct mbuf *m;
-       struct afhash h;
+       register u_long hash;
        struct sockaddr *dst = &ro->ro_dst;
        struct sockaddr *dst = &ro->ro_dst;
-       int af = dst->sa_family;
+       int (*match)(), doinghost, s;
+       struct afhash h;
+       u_int af = dst->sa_family;
+       struct mbuf **table;
 
 
-COUNT(ROUTE);
-       if (ro->ro_ifp)         /* ??? */
+       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);
                return;
        (*afswitch[af].af_hash)(dst, &h);
-       m = routehash[h.afh_hosthash % RTHASHSIZ];
-       key = h.afh_hostkey;
-       rtmin = 0, doinghost = 1;
+       match = afswitch[af].af_netmatch;
+       hash = h.afh_hosthash, table = rthost, doinghost = 1;
+       s = splnet();
 again:
 again:
-       for (; m; m = m->m_next) {
+       for (m = table[RTHASHMOD(hash)]; m; m = m->m_next) {
                rt = mtod(m, struct rtentry *);
                rt = mtod(m, struct rtentry *);
-#define        equal(a1, a2) \
-       (bcmp((caddr_t)(a1), (caddr_t)(a2), sizeof(struct sockaddr)) == 0)
-               if (rt->rt_key != key)
+               if (rt->rt_hash != hash)
+                       continue;
+               if ((rt->rt_flags & RTF_UP) == 0 ||
+                   (rt->rt_ifp->if_flags & IFF_UP) == 0)
                        continue;
                if (doinghost) {
                        continue;
                if (doinghost) {
-                       if (!equal(&rt->rt_dst, dst))
+                       if (bcmp((caddr_t)&rt->rt_dst, (caddr_t)dst,
+                           sizeof (*dst)))
                                continue;
                } else {
                                continue;
                } else {
-                       if (rt->rt_dst.sa_family != af)
-                               continue;
-                       if ((*afswitch[af].af_netmatch)(&rt->rt_dst, dst) == 0)
+                       if (rt->rt_dst.sa_family != af ||
+                           !(*match)(&rt->rt_dst, dst))
                                continue;
                }
                                continue;
                }
-               if (rtmin == 0 || rt->rt_use < rtmin->rt_use)
-                       rtmin = rt;
-       }
-       if (rtmin) {
-               ro->ro_dst = rt->rt_dst;
-               ro->ro_rt = rt;
                rt->rt_refcnt++;
                rt->rt_refcnt++;
+               splx(s);
+               if (dst == &wildcard)
+                       rtstat.rts_wildcard++;
+               ro->ro_rt = rt;
                return;
        }
        if (doinghost) {
                doinghost = 0;
                return;
        }
        if (doinghost) {
                doinghost = 0;
-               m = routethash[h.afh_nethash % RTHASHSIZ];
-               key = h.afh_netkey;
+               hash = h.afh_nethash, table = rtnet;
+               goto again;
+       }
+       /*
+        * Check for wildcard gateway, by convention network 0.
+        */
+       if (dst != &wildcard) {
+               dst = &wildcard, hash = 0;
                goto again;
        }
                goto again;
        }
-       ro->ro_ifp = 0;
-       ro->ro_rt = 0;
+       splx(s);
+       rtstat.rts_unreach++;
 }
 
 }
 
-struct rtentry *
-reroute(sa)
-       register struct sockaddr *sa;
-{
+rtfree(rt)
        register struct rtentry *rt;
        register struct rtentry *rt;
-       register struct mbuf *m;
-       struct afhash h;
+{
 
 
-COUNT(REROUTE);
-       (*afswitch[sa->sa_family].af_hash)(sa, &h);
-       m = routehash[h.afh_hosthash];
-       key = h.afh_hostkey;
-       for (; m; m = m->m_next) {
-               rt = mtod(m, struct rtentry *);
-               if (rt->rt_key != key)
-                       continue;
-               if (equal(&rt->rt_gateway, sa))
-                       return (rt);
+       if (rt == 0)
+               panic("rtfree");
+       rt->rt_refcnt--;
+       if (rt->rt_refcnt == 0 && (rt->rt_flags&RTF_UP) == 0) {
+               rttrash--;
+               (void) m_free(dtom(rt));
        }
        }
-       return (0);
 }
 
 /*
 }
 
 /*
- * Routing control calls allow a routing daemon
- * to consistenly access the routing data base for updates.
+ * 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
+ *
  */
  */
-rtcontrol(req, addr)
-       caddr_t addr;
+rtredirect(dst, gateway, flags, src)
+       struct sockaddr *dst, *gateway, *src;
+       int flags;
 {
 {
-       register struct rtreq rq;
-       int x = splimp(), err = 0;
-
-COUNT(RTCONTROL);
-       if (suser())
-               goto bad;
-       if (copyin(addr, (caddr_t)&rq, sizeof(struct rtreq))) {
-               u.u_error = EFAULT;
-               goto bad;
+       struct route ro;
+       register struct rtentry *rt;
+
+       /* verify the gateway is directly reachable */
+       if (ifa_ifwithnet(gateway) == 0) {
+               rtstat.rts_badredirect++;
+               return;
        }
        }
-       err = rtrequest(req, &rq);
-bad:
-       splx(x);
-       return (err);
+       ro.ro_dst = *dst;
+       ro.ro_rt = 0;
+       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. 
+        */
+       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;
+                       rt->rt_flags |= RTF_MODIFIED;
+                       rtstat.rts_newgateway++;
+               }
+       } else
+               rtstat.rts_badredirect++;
+       rtfree(rt);
 }
 
 /*
 }
 
 /*
- * Carry out a user request to modify the data base.
+ * Routing table ioctl interface.
  */
  */
-rtrequest(req, new)
+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
+ * 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;
        int req;
-       register struct rtentry *new;
+       register struct rtentry *entry;
 {
 {
-       register struct rtentry *rt;
        register struct mbuf *m, **mprev;
        register struct mbuf *m, **mprev;
-       struct sockaddr *sa = &new->rt_dst;
+       struct mbuf **mfirst;
+       register struct rtentry *rt;
        struct afhash h;
        struct afhash h;
-       int af = sa->sa_family;
+       int s, error = 0, (*match)();
+       u_int af;
+       u_long hash;
+       struct ifaddr *ifa;
+       struct ifaddr *ifa_ifwithdstaddr();
 
 
-       (*afswitch[af].af_hash)(sa, &h);
-       mprev = &routehash[h.afh_hosthash % RTHASHSIZ];
-       key = h.afh_hostkey;
-       doinghost = 1;
-again:
-       for (; m = *mprev; mprev = &m->m_next) {
+       af = entry->rt_dst.sa_family;
+       if (af >= AF_MAX)
+               return (EAFNOSUPPORT);
+       (*afswitch[af].af_hash)(&entry->rt_dst, &h);
+       if (entry->rt_flags & RTF_HOST) {
+               hash = h.afh_hosthash;
+               mprev = &rthost[RTHASHMOD(hash)];
+       } else {
+               hash = h.afh_nethash;
+               mprev = &rtnet[RTHASHMOD(hash)];
+       }
+       match = afswitch[af].af_netmatch;
+       s = splimp();
+       for (mfirst = mprev; m = *mprev; mprev = &m->m_next) {
                rt = mtod(m, struct rtentry *);
                rt = mtod(m, struct rtentry *);
-               if (rt->rt_key != key)
+               if (rt->rt_hash != hash)
                        continue;
                        continue;
-               if (doinghost) {
-                       if (!equal(&rt->rt_dst, dst))
+               if (entry->rt_flags & RTF_HOST) {
+                       if (!equal(&rt->rt_dst, &entry->rt_dst))
                                continue;
                } else {
                                continue;
                } else {
-                       if (rt->rt_dst.sa_family != af)
-                               continue;
-                       if ((*afswitch[af].af_netmatch)(&rt->rt_dst, sa) == 0)
+                       if (rt->rt_dst.sa_family != entry->rt_dst.sa_family ||
+                           (*match)(&rt->rt_dst, &entry->rt_dst) == 0)
                                continue;
                }
                                continue;
                }
-               break;
-       }
-       if (m == 0 && doinghost) {
-               doinghost = 0;
-               mprev = &routehash[h.afh_nethash % RTHASHSIZ];
-               key = h.afh_netkey;
-               goto again;
+               if (equal(&rt->rt_gateway, &entry->rt_gateway))
+                       break;
        }
        }
-
-       if (m == 0 && req != SIOCADDRT)
-               return (ESEARCH);
        switch (req) {
 
        case SIOCDELRT:
        switch (req) {
 
        case SIOCDELRT:
-               rt->rt_flags &= ~RTF_UP;
-               if (rt->rt_refcnt > 0)  /* should we notify protocols? */
-                       break;
-               *mprev = m_free(m);
-               break;
-
-       case SIOCCHGRT:
-               rt->rt_flags = new->rt_flags;
-               if (rt->rt_refcnt > 0)
-                       return (EINUSE);
-               if (!equal(&rt->rt_gateway, &new->rt_gateway))
-                       goto newneighbor;
+               if (m == 0) {
+                       error = ESRCH;
+                       goto bad;
+               }
+               *mprev = m->m_next;
+               if (rt->rt_refcnt > 0) {
+                       rt->rt_flags &= ~RTF_UP;
+                       rttrash++;
+                       m->m_next = 0;
+               } else
+                       (void) m_free(m);
                break;
 
        case SIOCADDRT:
                break;
 
        case SIOCADDRT:
-               m = m_getclr(M_DONTWAIT);
-               if (m == 0)
-                       return (ENOBUFS);
+               if (m) {
+                       error = EEXIST;
+                       goto bad;
+               }
+               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;
+                       }
+               }
+               m = m_get(M_DONTWAIT, MT_RTABLE);
+               if (m == 0) {
+                       error = ENOBUFS;
+                       goto bad;
+               }
+               m->m_next = *mfirst;
+               *mfirst = m;
                m->m_off = MMINOFF;
                m->m_off = MMINOFF;
-               *mprev = m;
+               m->m_len = sizeof (struct rtentry);
                rt = mtod(m, struct rtentry *);
                rt = mtod(m, struct rtentry *);
-               *rt = *new;
-               rt->rt_key = h.afh_nethash | h.afh_hosthash;
-newneighbor:
-               rt->rt_ifp = if_ifonnetof(&new->rt_gateway);
-               if (rt->rt_ifp == 0)
-                       rt->rt_flags &= ~RTF_UP;
+               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|RTF_DYNAMIC));
                rt->rt_refcnt = 0;
                rt->rt_refcnt = 0;
+               rt->rt_use = 0;
+               rt->rt_ifp = ifa->ifa_ifp;
                break;
        }
                break;
        }
-       return (0);
+bad:
+       splx(s);
+       return (error);
+}
+
+/*
+ * Set up a routing table entry, normally
+ * for an interface.
+ */
+rtinit(dst, gateway, cmd, flags)
+       struct sockaddr *dst, *gateway;
+       int cmd, flags;
+{
+       struct rtentry route;
+
+       bzero((caddr_t)&route, sizeof (route));
+       route.rt_dst = *dst;
+       route.rt_gateway = *gateway;
+       route.rt_flags = flags;
+       (void) rtrequest(cmd, &route);
 }
 }