wnj changes
[unix-history] / usr / src / sbin / routed / routed.c
index 14dfb96..6300d4e 100644 (file)
@@ -1,19 +1,20 @@
 #ifndef lint
 #ifndef lint
-static char sccsid[] = "@(#)routed.c   4.1 %G%";
+static char sccsid[] = "@(#)routed.c   4.14 82/06/09";
 #endif
 
 #endif
 
-#include <sys/param.h>
-#include <sys/protosw.h>
+/*
+ * Routing Table Management Daemon
+ */
+#include <sys/types.h>
 #include <sys/ioctl.h>
 #include <sys/socket.h>
 #include <net/in.h>
 #include <sys/ioctl.h>
 #include <sys/socket.h>
 #include <net/in.h>
-#define        KERNEL
-#include <net/route.h>
 #include <net/if.h>
 #include <errno.h>
 #include <stdio.h>
 #include <nlist.h>
 #include <signal.h>
 #include <net/if.h>
 #include <errno.h>
 #include <stdio.h>
 #include <nlist.h>
 #include <signal.h>
+#include <time.h>
 #include "rip.h"
 #include "router.h"
 
 #include "rip.h"
 #include "router.h"
 
@@ -23,6 +24,7 @@ static char sccsid[] = "@(#)routed.c  4.1 %G%";
 #define        remque(q)       _remque((caddr_t)q)
 #define equal(a1, a2) \
        (bcmp((caddr_t)(a1), (caddr_t)(a2), sizeof (struct sockaddr)) == 0)
 #define        remque(q)       _remque((caddr_t)q)
 #define equal(a1, a2) \
        (bcmp((caddr_t)(a1), (caddr_t)(a2), sizeof (struct sockaddr)) == 0)
+#define        min(a,b)        ((a)>(b)?(b):(a))
 
 struct nlist nl[] = {
 #define        N_IFNET         0
 
 struct nlist nl[] = {
 #define        N_IFNET         0
@@ -33,19 +35,28 @@ struct nlist nl[] = {
 struct sockaddr_in myaddr = { AF_INET, IPPORT_ROUTESERVER };
 
 int    s;
 struct sockaddr_in myaddr = { AF_INET, IPPORT_ROUTESERVER };
 
 int    s;
-int    kmem;
-int    supplier;               /* process should supply updates */
-int    initializing;           /* stem off broadcast() calls */
-int    install = 0;            /* if 1 call kernel */
-int    timeval;
+int    snoroute;               /* socket with no routing */
+int    kmem = -1;
+int    supplier = -1;          /* process should supply updates */
+int    install = 1;            /* if 1 call kernel */
+int    timeval = -TIMER_RATE;
 int    timer();
 int    cleanup();
 int    timer();
 int    cleanup();
+
+#define tprintf if (trace) printf
 int    trace = 0;
 int    trace = 0;
+FILE   *ftrace;
 
 
-char   packet[MAXPACKETSIZE];
+char   packet[MAXPACKETSIZE+1];
+struct rip *msg = (struct rip *)packet;
 
 
+struct in_addr if_makeaddr();
+struct ifnet *if_ifwithaddr(), *if_ifwithnet();
 extern char *malloc();
 extern char *malloc();
-extern int errno;
+extern int errno, exit();
+char   **argv0;
+
+int    sndmsg(), supply();
 
 main(argc, argv)
        int argc;
 
 main(argc, argv)
        int argc;
@@ -54,21 +65,28 @@ main(argc, argv)
        int cc;
        struct sockaddr from;
        
        int cc;
        struct sockaddr from;
        
-       {   int t = open("/dev/tty", 2);
-           if (t >= 0) {
-               ioctl(t, TIOCNOTTY, 0);
-               close(t);
-           }
+       argv0 = argv;
+#ifndef DEBUG
+       if (fork())
+               exit(0);
+       for (cc = 0; cc < 10; cc++)
+               (void) close(cc);
+       (void) open("/", 0);
+       (void) dup2(0, 1);
+       (void) dup2(0, 2);
+       { int t = open("/dev/tty", 2);
+         if (t >= 0) {
+               ioctl(t, TIOCNOTTY, (char *)0);
+               (void) close(t);
+         }
        }
        }
+#endif
        if (trace) {
        if (trace) {
-               (void) fclose(stdout);
-               (void) fclose(stderr);
-               (void) fopen("trace", "a");
-               (void) dup(fileno(stdout));
-               setbuf(stdout, NULL);
-
+               ftrace = fopen("/etc/routerlog", "w");
+               dup2(fileno(ftrace), 1);
+               dup2(fileno(ftrace), 2);
        }
        }
-#ifdef vax
+#ifdef vax || pdp11
        myaddr.sin_port = htons(myaddr.sin_port);
 #endif
 again:
        myaddr.sin_port = htons(myaddr.sin_port);
 #endif
 again:
@@ -78,25 +96,44 @@ again:
                sleep(30);
                goto again;
        }
                sleep(30);
                goto again;
        }
-       rtinit();
-       getothers();
-       getinterfaces();
-       request();
-
+again2:
+       snoroute = socket(SOCK_DGRAM, 0, 0, SO_DONTROUTE);
+       if (snoroute < 0) {
+               perror("socket");
+               sleep(30);
+               goto again2;
+       }
        argv++, argc--;
        argv++, argc--;
-       while (argc > 0) {
-               if (strcmp(*argv, "-s") == 0)
-                       supplier++;
-               else if (strcmp(*argv, "-q") == 0)
+       while (argc > 0 && **argv == '-') {
+               if (!strcmp(*argv, "-s") == 0) {
+                       supplier = 1;
+                       argv++, argc--;
+                       continue;
+               }
+               if (!strcmp(*argv, "-q") == 0) {
                        supplier = 0;
                        supplier = 0;
-               argv++, argc--;
+                       argv++, argc--;
+                       continue;
+               }
+               goto usage;
+       }
+       if (argc > 0) {
+usage:
+               fprintf(stderr, "usage: routed [ -s ]\n");
+               exit(1);
        }
        }
+       rtinit();
+       ifinit();
+       if (supplier < 0)
+               supplier = 0;
+       gwkludge();
+       msg->rip_cmd = RIPCMD_REQUEST;
+       msg->rip_nets[0].rip_dst.sa_family = AF_UNSPEC;
+       msg->rip_nets[0].rip_metric = HOPCNT_INFINITY;
+       toall(sendmsg);
        sigset(SIGALRM, timer);
        sigset(SIGALRM, timer);
-       alarm(TIMER_RATE);
+       timer();
 
 
-       /*
-        * Listen for routing packets
-        */
        for (;;) {
                cc = receive(s, &from, packet, sizeof (packet));
                if (cc <= 0) {
        for (;;) {
                cc = receive(s, &from, packet, sizeof (packet));
                if (cc <= 0) {
@@ -110,276 +147,198 @@ again:
        }
 }
 
        }
 }
 
-/*
- * Look in a file for any gateways we should configure
- * outside the directly connected ones.  This is a kludge,
- * but until we can find out about gateways on the "other side"
- * of the ARPANET using GGP, it's a must.
- *
- * We don't really know the distance to the gateway, so we
- * assume it's a neighbor.
- */
-getothers()
-{
-       struct sockaddr_in dst, gate;
-       FILE *fp = fopen("/etc/gateways", "r");
-       struct rt_entry *rt;
-
-       if (fp == NULL)
-               return;
-       bzero((char *)&dst, sizeof (dst));
-       bzero((char *)&gate, sizeof (gate));
-       dst.sin_family = AF_INET;
-       gate.sin_family = AF_INET;
-       while (fscanf(fp, "%x %x", &dst.sin_addr.s_addr, 
-          &gate.sin_addr.s_addr) != EOF) {
-               rtadd((struct sockaddr *)&dst, (struct sockaddr *)&gate, 1);
-               rt = rtlookup((struct sockaddr *)&dst);
-               if (rt)
-                       rt->rt_flags |= RTF_SILENT;
-       }
-       fclose(fp);
-}
-
-struct ifnet *
-if_ifwithaddr(addr)
-       struct sockaddr *addr;
-{
-       register struct ifnet *ifp;
-
-#define        same(a1, a2) \
-       (bcmp((caddr_t)((a1)->sa_data), (caddr_t)((a2)->sa_data), 14) == 0)
-       for (ifp = ifnet; ifp; ifp = ifp->if_next) {
-               if (ifp->if_addr.sa_family != addr->sa_family)
-                       continue;
-               if (same(&ifp->if_addr, addr))
-                       break;
-               if ((ifp->if_flags & IFF_BROADCAST) &&
-                   same(&ifp->if_broadaddr, addr))
-                       break;
-       }
-       return (ifp);
-#undef same
-}
-
-struct ifnet *
-if_ifwithnet(addr)
-       register struct sockaddr *addr;
+rtinit()
 {
 {
-       register struct ifnet *ifp;
-       register int af = addr->sa_family;
-       register int (*netmatch)();
+       register struct rthash *rh;
 
 
-       if (af >= AF_MAX)
-               return (0);
-       netmatch = afswitch[af].af_netmatch;
-       for (ifp = ifnet; ifp; ifp = ifp->if_next) {
-               if (af != ifp->if_addr.sa_family)
-                       continue;
-               if ((*netmatch)(addr, &ifp->if_addr))
-                       break;
-       }
-       return (ifp);
+       for (rh = nethash; rh < &nethash[ROUTEHASHSIZ]; rh++)
+               rh->rt_forw = rh->rt_back = (struct rt_entry *)rh;
+       for (rh = hosthash; rh < &hosthash[ROUTEHASHSIZ]; rh++)
+               rh->rt_forw = rh->rt_back = (struct rt_entry *)rh;
 }
 
 }
 
-struct in_addr
-if_makeaddr(net, host)
-       int net, host;
-{
-       u_long addr;
-
-       if (net < 128)
-               addr = (net << 24) | host;
-       else if (net < 65536)
-               addr = (net << 16) | host;
-       else
-               addr = (net << 8) | host;
-#ifdef vax
-       addr = htonl(addr);
-#endif
-       return (*(struct in_addr *)&addr);
-}
+struct ifnet *ifnet;
 
 
-/*
- * Find the network interfaces attached to this machine.
- * The info is used to::
- *
- * (1) initialize the routing tables, as done by the kernel.
- * (2) ignore incoming packets we send.
- * (3) figure out broadcast capability and addresses.
- * (4) figure out if we're an internetwork gateway.
- *
- * We don't handle anything but Internet addresses.
- */
-getinterfaces()
+ifinit()
 {
 {
-       register struct ifnet **pifp, *ifp;
-       struct sockaddr_in net;
-       struct in_addr logicaladdr;
-       int nets;
+       struct ifnet *ifp, *next;
+       register struct sockaddr *dst;
+       int uniquemultihostinterfaces = 0;
 
        nlist("/vmunix", nl);
        if (nl[N_IFNET].n_value == 0) {
 
        nlist("/vmunix", nl);
        if (nl[N_IFNET].n_value == 0) {
-               printf("ifnet: symbol not in namelist\n");
-               exit(1);
+               printf("ifnet: not in namelist\n");
+               goto bad;
        }
        kmem = open("/dev/kmem", 0);
        if (kmem < 0) {
                perror("/dev/kmem");
        }
        kmem = open("/dev/kmem", 0);
        if (kmem < 0) {
                perror("/dev/kmem");
-               exit(1);
+               goto bad;
+       }
+       if (lseek(kmem, (long)nl[N_IFNET].n_value, 0) == -1 ||
+           read(kmem, (char *)&next, sizeof (next)) != sizeof (next)) {
+               printf("ifnet: error reading kmem\n");
+               goto bad;
        }
        }
-       (void) lseek(kmem, (long)nl[N_IFNET].n_value, 0);
-       (void) read(kmem, (char *)&ifnet, sizeof (ifnet));
-       bzero((char *)&net, sizeof (net));
-       net.sin_family = AF_INET;
-       logicaladdr.s_addr = 0;
-       nets = 0;
-       pifp = &ifnet;
-       initializing = 1;
-       while (*pifp) {
-               struct sockaddr_in *sin;
-
-               (void) lseek(kmem, (long)*pifp, 0);
-               ifp = *pifp = (struct ifnet *)malloc(sizeof (struct ifnet));
+       while (next) {
+               ifp = (struct ifnet *)malloc(sizeof (struct ifnet));
                if (ifp == 0) {
                        printf("routed: out of memory\n");
                        break;
                }
                if (ifp == 0) {
                        printf("routed: out of memory\n");
                        break;
                }
-               if (read(kmem, (char *)ifp, sizeof (*ifp)) != sizeof (*ifp)) {
+               if (lseek(kmem, (long)next, 0) == -1 ||
+                   read(kmem, (char *)ifp, sizeof (*ifp)) != sizeof (*ifp)) {
                        perror("read");
                        perror("read");
-                       break;
+                       goto bad;
                }
                }
+               next = ifp->if_next;
+               if (ifp->if_addr.sa_family != AF_INET)
+                       continue;
                if (ifp->if_net == LOOPBACKNET)
                if (ifp->if_net == LOOPBACKNET)
-                       goto skip;
-               nets++;
-               if ((ifp->if_flags & IFF_UP) == 0)
-                       goto skip;
-
-               /*
-                * Kludge: don't treat logical host pseudo-interface
-                *         as a net route, instead fabricate route
-                *         to get packets back from the gateway.
-                */
-               sin = (struct sockaddr_in *)&ifp->if_addr;
-               if (sin->sin_family == AF_INET && ifp->if_net == 10 &&
-                   sin->sin_addr.s_lh) {
-                       logicaladdr = sin->sin_addr;
-                       goto skip;
-               }
+                       continue;
+               if ((ifp->if_flags & IFF_POINTOPOINT) == 0 ||
+                   if_ifwithaddr(&ifp->if_dstaddr) == 0)
+                       uniquemultihostinterfaces++;
+               ifp->if_next = ifnet;
+               ifnet = ifp;
+               addrouteforif(ifp);
+       }
+       if (uniquemultihostinterfaces > 1 && supplier < 0)
+               supplier = 1;
+       return;
+bad:
+       sleep(60);
+       execv("/etc/routed", argv0);
+       _exit(0177);
+}
+
+addrouteforif(ifp)
+       struct ifnet *ifp;
+{
+       struct sockaddr_in net;
+       struct sockaddr *dst;
 
 
-               /*
-                * Before we can handle point-point links, the interface
-                * structure will have to include an indicator to allow
-                * us to distinguish entries from "network" entries.
-                */
+       if (ifp->if_flags & IFF_POINTOPOINT)
+               dst = &ifp->if_dstaddr;
+       else {
+               bzero((char *)&net, sizeof (net));
+               net.sin_family = AF_INET;
                net.sin_addr = if_makeaddr(ifp->if_net, INADDR_ANY);
                net.sin_addr = if_makeaddr(ifp->if_net, INADDR_ANY);
-               rtadd((struct sockaddr *)&net, (struct sockaddr *)sin, 0);
-       skip:
-               pifp = &ifp->if_next;
-       }
-       if (logicaladdr.s_addr) {
-               struct rt_entry *rt;
-
-               net.sin_addr = logicaladdr;
-               if (ifnet)
-                       rtadd((struct sockaddr *)&net, &ifnet->if_addr, 0);
-               /* yech...yet another logical host kludge */
-               rt = rtlookup((struct sockaddr *)&net);
-               if (rt)
-                       rt->rt_flags |= RTF_SILENT;
-       }
-       (void) close(kmem);
-       initializing = 0;
-       supplier = nets > 1;
+               dst = (struct sockaddr *)&net;
+       }
+       rtadd(dst, &ifp->if_addr, 0, RTS_INTERFACE);
 }
 
 }
 
-/*
- * Send a request message to all directly
- * connected hosts and networks.
- */
-request()
+gwkludge()
 {
 {
-       register struct rt_entry *rt;
-       register struct rt_hash *rh;
-       struct rt_hash *base = hosthash;
-       int doinghost = 1;
+       struct sockaddr_in dst, gate;
+       FILE *fp;
+       struct rt_entry *rt;
+       char flags[BUFSIZ];
 
 
-again:
-       for (rh = base; rh < &base[ROUTEHASHSIZ]; rh++)
-       for (rt = rh->rt_forw; rt != (struct rt_entry *)rh; rt = rt->rt_forw) {
-               if ((rt->rt_flags & RTF_SILENT) || rt->rt_metric > 0)
-                       continue;
-               getall(rt);
-       }
-       if (doinghost) {
-               base = nethash;
-               doinghost = 0;
-               goto again;
+       fp = fopen("/etc/gateways", "r");
+       if (fp == NULL)
+               return;
+       bzero((char *)&dst, sizeof (dst));
+       bzero((char *)&gate, sizeof (gate));
+       dst.sin_family = AF_INET;
+       gate.sin_family = AF_INET;
+       for (;;) {
+               if (fscanf(fp, "dst %x gateway %x\n", &dst.sin_addr.s_addr, 
+                  &gate.sin_addr.s_addr, flags) == EOF)
+                       break;
+               rtadd((struct sockaddr *)&dst, (struct sockaddr *)&gate, 1,
+                   RTS_GLOBAL|(!strcmp(flags, "passive") ? RTS_PASSIVE : 0));
        }
        }
+       fclose(fp);
 }
 
 }
 
-/*
- * Broadcast a new, or modified, routing table entry
- * to all directly connected hosts and networks.
- */
-broadcast(entry)
-       struct rt_entry *entry;
+timer()
 {
 {
-       register struct rt_hash *rh;
+       register struct rthash *rh;
        register struct rt_entry *rt;
        register struct rt_entry *rt;
-       register struct sockaddr *dst;
-       struct rt_hash *base = hosthash;
-       int doinghost = 1;
-       struct rip *msg = (struct rip *)packet;
-
-       if (trace)
-               log("broadcast", entry);
-       msg->rip_cmd = RIPCMD_RESPONSE;
-       msg->rip_nets[0].rip_dst = entry->rt_dst;
-       msg->rip_nets[0].rip_metric = entry->rt_metric + 1;
+       struct rthash *base = hosthash;
+       int doinghost = 1, state;
 
 
+       timeval += TIMER_RATE;
+       tprintf(">>> time %d >>>\n", timeval);
 again:
 again:
-       for (rh = base; rh < &base[ROUTEHASHSIZ]; rh++)
-       for (rt = rh->rt_forw; rt != (struct rt_entry *)rh; rt = rt->rt_forw) {
-               if ((rt->rt_flags & RTF_SILENT) || rt->rt_metric > 0)
-                       continue;
-               if (rt->rt_ifp && (rt->rt_ifp->if_flags & IFF_BROADCAST))
-                       dst = &rt->rt_ifp->if_broadaddr;
-               else
-                       dst = &rt->rt_gateway;
-               (*afswitch[dst->sa_family].af_output)(dst, sizeof (struct rip));
+       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_GLOBAL))
+                               rt->rt_timer += TIMER_RATE;
+                       log("", rt);
+                       if (rt->rt_timer >= EXPIRE_TIME)
+                               rt->rt_metric = HOPCNT_INFINITY;
+                       if ((rt->rt_state & RTS_DELRT) ||
+                           rt->rt_timer >= GARBAGE_TIME) {
+                               if (rt->rt_state&(RTS_INTERFACE|RTS_GLOBAL)) {
+                                       if (rt->rt_timer > 9999)
+                                               rt->rt_timer = 9999;
+                                       continue;
+                               }
+                               rt = rt->rt_back;
+                               rtdelete(rt->rt_forw);
+                               continue;
+                       }
+                       state = rt->rt_state;
+                       if (rt->rt_state & RTS_ADDRT) {
+                               if (ioctl(s, SIOCADDRT,(char *)&rt->rt_rt) < 0)
+                                       perror("SIOCADDRT");
+                               rt->rt_state &= ~RTS_ADDRT;
+                       }
+                       if (rt->rt_state & RTS_CHGRT) {
+                               struct rtentry oldroute;
+
+                               oldroute = rt->rt_rt;
+                               rt->rt_router = rt->rt_newrouter;
+                               if (ioctl(s, SIOCADDRT,(char *)&rt->rt_rt) < 0)
+                                       perror("SIOCADDRT");
+                               if (ioctl(s, SIOCDELRT, (char *)&oldroute) < 0)
+                                       perror("SIOCDELRT");
+                               rt->rt_state &= ~RTS_CHGRT;
+                       }
+                       if (supplier && (state & (RTS_CHGRT|RTS_ADDRT))) {
+                               log("broadcast", rt);
+                               msg->rip_cmd = RIPCMD_RESPONSE;
+                               msg->rip_nets[0].rip_dst = rt->rt_dst;
+                               msg->rip_nets[0].rip_metric =
+                                   min(rt->rt_metric+1, HOPCNT_INFINITY);
+                               sendmsgtoall();
+                       }
+               }
        }
        if (doinghost) {
                doinghost = 0;
                base = nethash;
                goto again;
        }
        }
        if (doinghost) {
                doinghost = 0;
                base = nethash;
                goto again;
        }
+       if (supplier && (timeval % SUPPLY_INTERVAL) == 0)
+               toall(supply);
+       tprintf("<<< time %d <<<\n", timeval);
+       alarm(TIMER_RATE);
 }
 
 }
 
-/*
- * Supply all directly connected neighbors with the
- * current state of the routing tables.
- */
-supplyall()
+toall(f)
+       int (*f)();
 {
 {
+       register struct rthash *rh;
        register struct rt_entry *rt;
        register struct rt_entry *rt;
-       register struct rt_hash *rh;
        register struct sockaddr *dst;
        register struct sockaddr *dst;
-       struct rt_hash *base = hosthash;
+       struct rthash *base = hosthash;
        int doinghost = 1;
 
 again:
        for (rh = base; rh < &base[ROUTEHASHSIZ]; rh++)
        for (rt = rh->rt_forw; rt != (struct rt_entry *)rh; rt = rt->rt_forw) {
        int doinghost = 1;
 
 again:
        for (rh = base; rh < &base[ROUTEHASHSIZ]; rh++)
        for (rt = rh->rt_forw; rt != (struct rt_entry *)rh; rt = rt->rt_forw) {
-               if ((rt->rt_flags & RTF_SILENT) || rt->rt_metric > 0)
+               if ((rt->rt_state&RTS_PASSIVE) || rt->rt_metric > 0)
                        continue;
                if (rt->rt_ifp && (rt->rt_ifp->if_flags & IFF_BROADCAST))
                        dst = &rt->rt_ifp->if_broadaddr;
                else
                        continue;
                if (rt->rt_ifp && (rt->rt_ifp->if_flags & IFF_BROADCAST))
                        dst = &rt->rt_ifp->if_broadaddr;
                else
-                       dst = &rt->rt_gateway;
-               if (trace)
-                       log("supply", rt);
-               supply(dst);
+                       dst = &rt->rt_router;
+               (*f)(rt, dst);
        }
        if (doinghost) {
                base = nethash;
        }
        if (doinghost) {
                base = nethash;
@@ -388,62 +347,46 @@ again:
        }
 }
 
        }
 }
 
-/*
- * Supply routing information to target "sa".
- */
-supply(sa)
+sendmsg(rt, dst)
+       register struct rt_entry *rt;
+       struct sockaddr *dst;
+{
+
+       (*afswitch[dst->sa_family].af_output)(s, dst, sizeof (struct rip));
+}
+
+supply(rt, sa)
+       register struct rt_entry *rt;
        struct sockaddr *sa;
 {
        struct sockaddr *sa;
 {
-       struct rip *msg = (struct rip *)packet;
        struct netinfo *n = msg->rip_nets;
        struct netinfo *n = msg->rip_nets;
-       register struct rt_hash *rh;
-       register struct rt_entry *rt;
-       struct rt_hash *base = hosthash;
-       int space = MAXPACKETSIZE - sizeof (int), doinghost = 1;
+       register struct rthash *rh;
+       struct rthash *base = hosthash;
+       int doinghost = 1, size;
        int (*output)() = afswitch[sa->sa_family].af_output;
        int (*output)() = afswitch[sa->sa_family].af_output;
+       int sto = (rt->rt_state&RTS_INTERFACE) ? snoroute : s;
 
 
+       log("supply", rt);
        msg->rip_cmd = RIPCMD_RESPONSE;
 again:
        for (rh = base; rh < &base[ROUTEHASHSIZ]; rh++)
        for (rt = rh->rt_forw; rt != (struct rt_entry *)rh; rt = rt->rt_forw) {
        msg->rip_cmd = RIPCMD_RESPONSE;
 again:
        for (rh = base; rh < &base[ROUTEHASHSIZ]; rh++)
        for (rt = rh->rt_forw; rt != (struct rt_entry *)rh; rt = rt->rt_forw) {
-
-               /*
-                * Flush packet out if not enough room for
-                * another routing table entry.
-                */
-               if (space < sizeof (struct netinfo)) {
-                       (*output)(sa, MAXPACKETSIZE - space);
-                       space = MAXPACKETSIZE - sizeof (int);
+               size = (char *)n - packet;
+               if (size > MAXPACKETSIZE - sizeof (struct netinfo)) {
+                       (*output)(sto, sa, size);
                        n = msg->rip_nets;
                }
                n->rip_dst = rt->rt_dst;
                        n = msg->rip_nets;
                }
                n->rip_dst = rt->rt_dst;
-               n->rip_metric = rt->rt_metric + 1;
-               n++, space -= sizeof (struct netinfo);
+               n->rip_metric = min(rt->rt_metric + 1, HOPCNT_INFINITY);
+               n++;
        }
        if (doinghost) {
                doinghost = 0;
                base = nethash;
                goto again;
        }
        }
        if (doinghost) {
                doinghost = 0;
                base = nethash;
                goto again;
        }
-
-       if (space < MAXPACKETSIZE - sizeof (int))
-               (*output)(sa, MAXPACKETSIZE - space);
-}
-
-getall(rt)
-       struct rt_entry *rt;
-{
-       register struct rip *msg = (struct rip *)packet;
-       struct sockaddr *dst;
-
-       msg->rip_cmd = RIPCMD_REQUEST;
-       msg->rip_nets[0].rip_dst.sa_family = AF_UNSPEC;
-       msg->rip_nets[0].rip_metric = HOPCNT_INFINITY;
-       if (rt->rt_ifp && (rt->rt_ifp->if_flags & IFF_BROADCAST))
-               dst = &rt->rt_ifp->if_broadaddr;
-       else
-               dst = &rt->rt_gateway;
-       (*afswitch[dst->sa_family].af_output)(dst, sizeof (struct rip));
+       if (n != msg->rip_nets)
+               (*output)(sto, sa, (char *)n - packet);
 }
 
 /*
 }
 
 /*
@@ -453,29 +396,29 @@ rip_respond(from, size)
        struct sockaddr *from;
        int size;
 {
        struct sockaddr *from;
        int size;
 {
-       register struct rip *msg = (struct rip *)packet;
        struct netinfo *np = msg->rip_nets;
        struct rt_entry *rt;
        int newsize = 0;
        
        struct netinfo *np = msg->rip_nets;
        struct rt_entry *rt;
        int newsize = 0;
        
-       size -= sizeof (int);
+       size -= 4 * sizeof (char);
        while (size > 0) {
                if (size < sizeof (struct netinfo))
                        break;
                size -= sizeof (struct netinfo);
                if (np->rip_dst.sa_family == AF_UNSPEC &&
                    np->rip_metric == HOPCNT_INFINITY && size == 0) {
        while (size > 0) {
                if (size < sizeof (struct netinfo))
                        break;
                size -= sizeof (struct netinfo);
                if (np->rip_dst.sa_family == AF_UNSPEC &&
                    np->rip_metric == HOPCNT_INFINITY && size == 0) {
-                       supply(from);
+                       supply(s, from);
                        return;
                }
                rt = rtlookup(&np->rip_dst);
                        return;
                }
                rt = rtlookup(&np->rip_dst);
-               np->rip_metric = rt == 0 ? HOPCNT_INFINITY : rt->rt_metric + 1;
+               np->rip_metric = rt == 0 ?
+                       HOPCNT_INFINITY : min(rt->rt_metric+1, HOPCNT_INFINITY);
                np++, newsize += sizeof (struct netinfo);
        }
        if (newsize > 0) {
                msg->rip_cmd = RIPCMD_RESPONSE;
                newsize += sizeof (int);
                np++, newsize += sizeof (struct netinfo);
        }
        if (newsize > 0) {
                msg->rip_cmd = RIPCMD_RESPONSE;
                newsize += sizeof (int);
-               (*afswitch[from->sa_family].af_output)(from, newsize);
+               (*afswitch[from->sa_family].af_output)(s, from, newsize);
        }
 }
 
        }
 }
 
@@ -486,107 +429,144 @@ rip_input(from, size)
        struct sockaddr *from;
        int size;
 {
        struct sockaddr *from;
        int size;
 {
-       register struct rip *msg = (struct rip *)packet;
        struct rt_entry *rt;
        struct netinfo *n;
        struct rt_entry *rt;
        struct netinfo *n;
+       struct ifnet *ifp;
+       time_t t;
 
 
-       if (msg->rip_cmd != RIPCMD_RESPONSE &&
-           msg->rip_cmd != RIPCMD_REQUEST)
-               return;
+       switch (msg->rip_cmd) {
 
 
-       /*
-        * The router port is in the lower 1K of the UDP port space,
-        * and so is priviledged.  Hence we can "authenticate" incoming
-        * updates simply by checking the source port.
-        */
-       if (msg->rip_cmd == RIPCMD_RESPONSE &&
-           (*afswitch[from->sa_family].af_portmatch)(from) == 0)
+       default:
                return;
                return;
-       if (msg->rip_cmd == RIPCMD_REQUEST) {
+
+       case RIPCMD_REQUEST:
                rip_respond(from, size);
                return;
                rip_respond(from, size);
                return;
-       }
 
 
-       /*
-        * Process updates.
-        * Extraneous information like Internet ports
-        * must first be purged from the sender's address for
-        * pattern matching below.
-        */
-       (*afswitch[from->sa_family].af_canon)(from);
-       if (trace)
-               printf("input from %x\n", from->sin_addr);
-       /*
-        * If response packet is from ourselves, use it only
-        * to reset timer on entry.  Otherwise, we'd believe
-        * it as gospel (since it comes from the router) and
-        * unknowingly update the metric to show the outgoing
-        * cost (higher than our real cost).  I guess the protocol
-        * spec doesn't address this because Xerox Ethernets
-        * don't hear their own broadcasts?
-        */
-       if (if_ifwithaddr(from)) {
-               rt = rtlookup(from);
-               if (rt)
-                       rt->rt_timer = 0;
-               return;
-       }
-       size -= sizeof (int);
-       n = msg->rip_nets;
-       for (; size > 0; size -= sizeof (struct netinfo), n++) {
-               if (size < sizeof (struct netinfo))
-                       break;
+       case RIPCMD_TRACEON:
+               if ((*afswitch[from->sa_family].af_portcheck)(from) == 0)
+                       return;
                if (trace)
                if (trace)
-                       printf("dst %x hc %d...", n->rip_dst.sin_addr,
-                               n->rip_metric);
-               rt = rtlookup(&n->rip_dst);
-
-               /*
-                * Unknown entry, add it to the tables only if
-                * its interesting.
-                */
-               if (rt == 0) {
-                       if (n->rip_metric < HOPCNT_INFINITY)
-                               rtadd(&n->rip_dst, from, n->rip_metric);
-                       if (trace)
-                               printf("new\n");
-                       continue;
-               }
+                       return;
+               packet[size] = '\0';
+               ftrace = fopen(msg->rip_tracefile, "a");
+               if (ftrace == NULL)
+                       return;
+               (void) dup2(fileno(ftrace), 1);
+               (void) dup2(fileno(ftrace), 2);
+               trace = 1;
+               t = time(0);
+               printf("*** Tracing turned on at %.24s ***\n", ctime(&t));
+               return;
 
 
-               if (trace)
-                       printf("ours: gate %x hc %d timer %d\n",
-                       rt->rt_gateway.sin_addr,
-                       rt->rt_metric, rt->rt_timer);
-               /*
-                * Update the entry if one of the following is true:
-                *
-                * (1) The update came directly from the gateway.
-                * (2) A shorter path is provided.
-                * (3) The entry hasn't been updated in a while
-                *     and a path of equivalent cost is offered.
-                */
-               if (equal(from, &rt->rt_gateway) ||
-                   rt->rt_metric > n->rip_metric ||
-                   (rt->rt_timer > (EXPIRE_TIME/2) &&
-                   rt->rt_metric == n->rip_metric)) {
-                       rtchange(rt, from, n->rip_metric);
-                       rt->rt_timer = 0;
+       case RIPCMD_TRACEOFF:
+               /* verify message came from a priviledged port */
+               if ((*afswitch[from->sa_family].af_portcheck)(from) == 0)
+                       return;
+               if (!trace)
+                       return;
+               t = time(0);
+               printf("*** Tracing turned off at %.24s ***\n", ctime(&t));
+               fflush(stdout), fflush(stderr);
+               if (ftrace)
+                       fclose(ftrace);
+               (void) close(1), (void) close(2);
+               trace = 0;
+               return;
+
+       case RIPCMD_RESPONSE:
+               /* verify message came from a router */
+               if ((*afswitch[from->sa_family].af_portmatch)(from) == 0)
+                       return;
+               (*afswitch[from->sa_family].af_canon)(from);
+               tprintf("input from %x\n",
+                   ((struct sockaddr_in *)from)->sin_addr);
+               /* are we talking to ourselves? */
+               ifp = if_ifwithaddr(from);
+               if (ifp) {
+                       rt = rtfind(from);
+                       if (rt)
+                               rt->rt_timer = 0;
+                       else
+                               addrouteforif(ifp);
+                       return;
                }
                }
+               size -= 4 * sizeof (char);
+               n = msg->rip_nets;
+               for (; size > 0; size -= sizeof (struct netinfo), n++) {
+                       if (size < sizeof (struct netinfo))
+                               break;
+                       if (n->rip_metric >= HOPCNT_INFINITY)
+                               continue;
+                       tprintf("dst %x hc %d...",
+                           ((struct sockaddr_in *)&n->rip_dst)->sin_addr,
+                           n->rip_metric);
+                       rt = rtlookup(&n->rip_dst);
+                       if (rt == 0) {
+                               rtadd(&n->rip_dst, from, n->rip_metric, 0);
+                               tprintf("new\n");
+                               continue;
+                       }
+                       tprintf("ours: gate %x hc %d timer %d\n",
+                         ((struct sockaddr_in *)&rt->rt_router)->sin_addr,
+                         rt->rt_metric, rt->rt_timer);
+                       /*
+                        * update if from gateway, shorter, or getting stale
+                        * and equivalent.
+                        */
+                       if (equal(from, &rt->rt_router) ||
+                           n->rip_metric < rt->rt_metric ||
+                           (rt->rt_timer > (EXPIRE_TIME/2) &&
+                           rt->rt_metric == n->rip_metric)) {
+                               rtchange(rt, from, n->rip_metric);
+                               rt->rt_timer = 0;
+                       }
+               }
+               return;
        }
 }
 
        }
 }
 
-/*
- * Lookup an entry to the appropriate dstination.
- */
 struct rt_entry *
 rtlookup(dst)
        struct sockaddr *dst;
 {
        register struct rt_entry *rt;
 struct rt_entry *
 rtlookup(dst)
        struct sockaddr *dst;
 {
        register struct rt_entry *rt;
-       register struct rt_hash *rh;
-       register int hash, (*match)();
+       register struct rthash *rh;
+       register int hash;
+       struct afhash h;
+       int doinghost = 1;
+
+       if (dst->sa_family >= AF_MAX)
+               return (0);
+       (*afswitch[dst->sa_family].af_hash)(dst, &h);
+       hash = h.afh_hosthash;
+       rh = &hosthash[hash % ROUTEHASHSIZ];
+again:
+       for (rt = rh->rt_forw; rt != (struct rt_entry *)rh; rt = rt->rt_forw) {
+               if (rt->rt_hash != hash)
+                       continue;
+               if (equal(&rt->rt_dst, dst))
+                       return (rt);
+       }
+       if (doinghost) {
+               doinghost = 0;
+               hash = h.afh_nethash;
+               rh = &nethash[hash % ROUTEHASHSIZ];
+               goto again;
+       }
+       return (0);
+}
+
+struct rt_entry *
+rtfind(dst)
+       struct sockaddr *dst;
+{
+       register struct rt_entry *rt;
+       register struct rthash *rh;
+       register int hash;
        struct afhash h;
        struct afhash h;
-       int af = dst->sa_family, doinghost = 1;
+       int af = dst->sa_family;
+       int doinghost = 1, (*match)();
 
        if (af >= AF_MAX)
                return (0);
 
        if (af >= AF_MAX)
                return (0);
@@ -610,33 +590,20 @@ again:
        if (doinghost) {
                doinghost = 0;
                hash = h.afh_nethash;
        if (doinghost) {
                doinghost = 0;
                hash = h.afh_nethash;
-               match = afswitch[af].af_netmatch;
                rh = &nethash[hash % ROUTEHASHSIZ];
                rh = &nethash[hash % ROUTEHASHSIZ];
+               match = afswitch[af].af_netmatch;
                goto again;
        }
        return (0);
 }
 
                goto again;
        }
        return (0);
 }
 
-rtinit()
-{
-       register struct rt_hash *rh;
-
-       for (rh = nethash; rh < &nethash[ROUTEHASHSIZ]; rh++)
-               rh->rt_forw = rh->rt_back = (struct rt_entry *)rh;
-       for (rh = hosthash; rh < &hosthash[ROUTEHASHSIZ]; rh++)
-               rh->rt_forw = rh->rt_back = (struct rt_entry *)rh;
-}
-
-/*
- * Add a new entry.
- */
-rtadd(dst, gate, metric)
+rtadd(dst, gate, metric, iflags)
        struct sockaddr *dst, *gate;
        struct sockaddr *dst, *gate;
-       short metric;
+       int metric, iflags;
 {
        struct afhash h;
        register struct rt_entry *rt;
 {
        struct afhash h;
        register struct rt_entry *rt;
-       struct rt_hash *rh;
+       struct rthash *rh;
        int af = dst->sa_family, flags, hash;
 
        if (af >= AF_MAX)
        int af = dst->sa_family, flags, hash;
 
        if (af >= AF_MAX)
@@ -655,30 +622,20 @@ rtadd(dst, gate, metric)
                return;
        rt->rt_hash = hash;
        rt->rt_dst = *dst;
                return;
        rt->rt_hash = hash;
        rt->rt_dst = *dst;
-       rt->rt_gateway = *gate;
+       rt->rt_router = *gate;
        rt->rt_metric = metric;
        rt->rt_timer = 0;
        rt->rt_metric = metric;
        rt->rt_timer = 0;
-       rt->rt_flags = RTF_UP | flags;
-       rt->rt_ifp = if_ifwithnet(&rt->rt_gateway);
-       if (metric == 0)
-               rt->rt_flags |= RTF_DIRECT;
+       rt->rt_flags = RTF_UP | flags | iflags;
+       rt->rt_state = 0;
+       rt->rt_ifp = if_ifwithnet(&rt->rt_router);
+       if (metric)
+               rt->rt_flags |= RTF_GATEWAY;
        insque(rt, rh);
        insque(rt, rh);
-       if (trace)
-               log("add", rt);
-       if (initializing)
-               return;
-       if (supplier)
-               broadcast(rt);
-       if (install) {
-               rt->rt_flags |= RTF_ADDRT;
-               rt->rt_retry = EXPIRE_TIME/TIMER_RATE;
-       }
+       log("add", rt);
+       if (install)
+               rt->rt_state |= RTS_ADDRT;
 }
 
 }
 
-/*
- * Look to see if a change to an existing entry
- * is warranted; if so, make it.
- */
 rtchange(rt, gate, metric)
        struct rt_entry *rt;
        struct sockaddr *gate;
 rtchange(rt, gate, metric)
        struct rt_entry *rt;
        struct sockaddr *gate;
@@ -686,151 +643,72 @@ rtchange(rt, gate, metric)
 {
        int change = 0;
 
 {
        int change = 0;
 
-       if (!equal(&rt->rt_gateway, gate)) {
-               rt->rt_gateway = *gate;
+       if (!equal(&rt->rt_router, gate)) {
+               rt->rt_newrouter = *gate;
                change++;
        }
                change++;
        }
-
-       /*
-        * If the hop count has changed, adjust
-        * the flags in the routing table entry accordingly.
-        */
        if (metric != rt->rt_metric) {
        if (metric != rt->rt_metric) {
-               if (rt->rt_metric == 0)
-                       rt->rt_flags &= ~RTF_DIRECT;
+               if (metric == 0)
+                       rt->rt_flags |= RTF_GATEWAY;
                rt->rt_metric = metric;
                rt->rt_metric = metric;
-               if (metric >= HOPCNT_INFINITY)
-                       rt->rt_flags &= ~RTF_UP;
-               else
-                       rt->rt_flags |= RTF_UP;
                change++;
        }
                change++;
        }
-
        if (!change)
                return;
        if (!change)
                return;
-       if (supplier)
-               broadcast(rt);
-       if (trace)
-               log("change", rt);
-       if (install) {
-               rt->rt_flags |= RTF_CHGRT;
-               rt->rt_retry = EXPIRE_TIME/TIMER_RATE;
-       }
+       log("change", rt);
+       if (install)
+               rt->rt_state |= RTS_CHGRT;
 }
 
 }
 
-/*
- * Delete a routing table entry.
- */
 rtdelete(rt)
        struct rt_entry *rt;
 {
 rtdelete(rt)
        struct rt_entry *rt;
 {
-       if (trace)
-               log("delete", rt);
-       if (install)
-               if (ioctl(s, SIOCDELRT, (char *)&rt->rt_hash) &&
-                 errno == EBUSY)
-                       rt->rt_flags |= RTF_DELRT;
+
+       log("delete", rt);
+       if (install && ioctl(s, SIOCDELRT, (char *)&rt->rt_rt))
+               perror("SIOCDELRT");
+       /* don't delete interface entries so we can poll them later */
+       if (rt->rt_state & RTS_INTERFACE)
+               return;
        remque(rt);
        free((char *)rt);
 }
 
        remque(rt);
        free((char *)rt);
 }
 
-/*
- * Timer routine:
- *
- * o handle timers on table entries,
- * o invalidate entries which haven't been updated in a while,
- * o delete entries which are too old,
- * o retry ioctl's which weren't successful the first
- *   time due to the kernel entry being busy
- * o if we're an internetwork router, supply routing updates
- *   periodically
- */
-timer()
-{
-       register struct rt_hash *rh;
-       register struct rt_entry *rt;
-       struct rt_hash *base = hosthash;
-       int doinghost = 1;
-
-       if (trace)
-               printf(">>> time %d >>>\n", timeval);
-again:
-       for (rh = base; rh < &base[ROUTEHASHSIZ]; rh++) {
-               rt = rh->rt_forw;
-               for (; rt != (struct rt_entry *)rh; rt = rt->rt_forw) {
-
-                       /*
-                        * If the host is indicated to be
-                        * "silent" (i.e. it's a logical host,
-                        * or one we got from the initialization
-                        * file), don't time out it's entry.
-                        */
-                       if (rt->rt_flags & RTF_SILENT)
-                               continue;
-                       if (trace)
-                               log("", rt);
-                       rt->rt_timer += TIMER_RATE;
-                       if (rt->rt_timer >= GARBAGE_TIME ||
-                         (rt->rt_flags & RTF_DELRT)) {
-                               rt = rt->rt_forw;
-                               rtdelete(rt->rt_back);
-                               rt = rt->rt_back;
-                               continue;
-                       }
-                       if (rt->rt_timer >= EXPIRE_TIME)
-                               rt->rt_metric = HOPCNT_INFINITY;
-                       if (rt->rt_flags & RTF_CHGRT)
-                               if (!ioctl(s, SIOCCHGRT,(char *)&rt->rt_hash) ||
-                                 --rt->rt_retry == 0)
-                                       rt->rt_flags &= ~RTF_CHGRT;
-                       if (rt->rt_flags & RTF_ADDRT)
-                               if (!ioctl(s, SIOCADDRT,(char *)&rt->rt_hash) ||
-                                 --rt->rt_retry == 0)
-                                       rt->rt_flags &= ~RTF_ADDRT;
-               }
-       }
-       if (doinghost) {
-               doinghost = 0;
-               base = nethash;
-               goto again;
-       }
-       timeval += TIMER_RATE;
-       if (supplier && (timeval % SUPPLY_INTERVAL) == 0)
-               supplyall();
-       if (trace)
-               printf("<<< time %d <<<\n", timeval);
-       alarm(TIMER_RATE);
-}
-
 log(operation, rt)
        char *operation;
        struct rt_entry *rt;
 {
        time_t t = time(0);
        struct sockaddr_in *dst, *gate;
 log(operation, rt)
        char *operation;
        struct rt_entry *rt;
 {
        time_t t = time(0);
        struct sockaddr_in *dst, *gate;
-       static struct flagbits {
+       static struct bits {
                int     t_bits;
                char    *t_name;
                int     t_bits;
                char    *t_name;
-       } bits[] = {
+       } flagbits[] = {
                { RTF_UP,       "UP" },
                { RTF_UP,       "UP" },
-               { RTF_DIRECT,   "DIRECT" },
+               { RTF_GATEWAY,  "GATEWAY" },
                { RTF_HOST,     "HOST" },
                { RTF_HOST,     "HOST" },
-               { RTF_DELRT,    "DELETE" },
-               { RTF_CHGRT,    "CHANGE" },
-               { RTF_SILENT,   "SILENT" },
+               { 0 }
+       }, statebits[] = {
+               { RTS_DELRT,    "DELETE" },
+               { RTS_CHGRT,    "CHANGE" },
+               { RTS_PASSIVE,  "PASSIVE" },
+               { RTS_INTERFACE,"INTERFACE" },
+               { RTS_GLOBAL,   "GLOBAL" },
                { 0 }
        };
                { 0 }
        };
-       register struct flagbits *p;
+       register struct bits *p;
        register int first;
        char *cp;
 
        register int first;
        char *cp;
 
+       if (trace == 0)
+               return;
        printf("%s ", operation);
        dst = (struct sockaddr_in *)&rt->rt_dst;
        printf("%s ", operation);
        dst = (struct sockaddr_in *)&rt->rt_dst;
-       gate = (struct sockaddr_in *)&rt->rt_gateway;
-       printf("dst %x, router %x, metric %d, flags ",
+       gate = (struct sockaddr_in *)&rt->rt_router;
+       printf("dst %x, router %x, metric %d, flags",
                dst->sin_addr, gate->sin_addr, rt->rt_metric);
                dst->sin_addr, gate->sin_addr, rt->rt_metric);
-       cp = "%s";
-       for (first = 1, p = bits; p->t_bits > 0; p++) {
+       cp = " %s";
+       for (first = 1, p = flagbits; p->t_bits > 0; p++) {
                if ((rt->rt_flags & p->t_bits) == 0)
                        continue;
                printf(cp, p->t_name);
                if ((rt->rt_flags & p->t_bits) == 0)
                        continue;
                printf(cp, p->t_name);
@@ -839,5 +717,75 @@ log(operation, rt)
                        first = 0;
                }
        }
                        first = 0;
                }
        }
+       printf(" state");
+       cp = " %s";
+       for (first = 1, p = statebits; p->t_bits > 0; p++) {
+               if ((rt->rt_state & p->t_bits) == 0)
+                       continue;
+               printf(cp, p->t_name);
+               if (first) {
+                       cp = "|%s";
+                       first = 0;
+               }
+       }
        putchar('\n');
 }
        putchar('\n');
 }
+
+struct ifnet *
+if_ifwithaddr(addr)
+       struct sockaddr *addr;
+{
+       register struct ifnet *ifp;
+
+#define        same(a1, a2) \
+       (bcmp((caddr_t)((a1)->sa_data), (caddr_t)((a2)->sa_data), 14) == 0)
+       for (ifp = ifnet; ifp; ifp = ifp->if_next) {
+               if (ifp->if_addr.sa_family != addr->sa_family)
+                       continue;
+               if (same(&ifp->if_addr, addr))
+                       break;
+               if ((ifp->if_flags & IFF_BROADCAST) &&
+                   same(&ifp->if_broadaddr, addr))
+                       break;
+       }
+       return (ifp);
+#undef same
+}
+
+struct ifnet *
+if_ifwithnet(addr)
+       register struct sockaddr *addr;
+{
+       register struct ifnet *ifp;
+       register int af = addr->sa_family;
+       register int (*netmatch)();
+
+       if (af >= AF_MAX)
+               return (0);
+       netmatch = afswitch[af].af_netmatch;
+       for (ifp = ifnet; ifp; ifp = ifp->if_next) {
+               if (af != ifp->if_addr.sa_family)
+                       continue;
+               if ((*netmatch)(addr, &ifp->if_addr))
+                       break;
+       }
+       return (ifp);
+}
+
+struct in_addr
+if_makeaddr(net, host)
+       int net, host;
+{
+       u_long addr;
+
+       if (net < 128)
+               addr = (net << 24) | host;
+       else if (net < 65536)
+               addr = (net << 16) | host;
+       else
+               addr = (net << 8) | host;
+#ifdef vax
+       addr = htonl(addr);
+#endif
+       return (*(struct in_addr *)&addr);
+}