rework interface metrics to invert perspective; other misc. fixes
[unix-history] / usr / src / sbin / routed / startup.c
index adc1547..61c1426 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[] = "@(#)startup.c  4.9 (Berkeley) %G%";
-#endif
+static char sccsid[] = "@(#)startup.c  5.9 (Berkeley) %G%";
+#endif not lint
 
 /*
  * Routing Table Management Daemon
 
 /*
  * Routing Table Management Daemon
@@ -8,13 +14,13 @@ static char sccsid[] = "@(#)startup.c        4.9 (Berkeley) %G%";
 #include "defs.h"
 #include <sys/ioctl.h>
 #include <net/if.h>
 #include "defs.h"
 #include <sys/ioctl.h>
 #include <net/if.h>
-#include <nlist.h>
 #include <syslog.h>
 
 struct interface *ifnet;
 int    lookforinterfaces = 1;
 #include <syslog.h>
 
 struct interface *ifnet;
 int    lookforinterfaces = 1;
-int    performnlist = 1;
 int    externalinterfaces = 0;         /* # of remote and local interfaces */
 int    externalinterfaces = 0;         /* # of remote and local interfaces */
+int    foundloopback;                  /* valid flag for loopaddr */
+struct sockaddr loopaddr;              /* our address on loopback */
 
 /*
  * Find the network interfaces which have configured themselves.
 
 /*
  * Find the network interfaces which have configured themselves.
@@ -65,6 +71,13 @@ ifinit()
                /* argh, this'll have to change sometime */
                if (ifs.int_addr.sa_family != AF_INET)
                        continue;
                /* argh, this'll have to change sometime */
                if (ifs.int_addr.sa_family != AF_INET)
                        continue;
+               if (ifs.int_flags & IFF_LOOPBACK) {
+                       foundloopback = 1;
+                       loopaddr = ifs.int_addr;
+                       for (ifp = ifnet; ifp; ifp = ifp->int_next)
+                           if (ifp->int_flags & IFF_POINTOPOINT)
+                               add_ptopt_localrt(ifp);
+               }
                 if (ifs.int_flags & IFF_POINTOPOINT) {
                         if (ioctl(s, SIOCGIFDSTADDR, (char *)&ifreq) < 0) {
                                 syslog(LOG_ERR, "ioctl (get dstaddr)");
                 if (ifs.int_flags & IFF_POINTOPOINT) {
                         if (ioctl(s, SIOCGIFDSTADDR, (char *)&ifreq) < 0) {
                                 syslog(LOG_ERR, "ioctl (get dstaddr)");
@@ -79,6 +92,10 @@ ifinit()
                         }
                        ifs.int_broadaddr = ifreq.ifr_broadaddr;
                }
                         }
                        ifs.int_broadaddr = ifreq.ifr_broadaddr;
                }
+               if (ioctl(s, SIOCGIFMETRIC, (char *)&ifreq) < 0)
+                       syslog(LOG_ERR, "ioctl (get metric)");
+               else
+                       ifs.int_metric = ifreq.ifr_metric;
                if (ioctl(s, SIOCGIFNETMASK, (char *)&ifreq) < 0) {
                        syslog(LOG_ERR, "ioctl (get netmask)");
                        continue;
                if (ioctl(s, SIOCGIFNETMASK, (char *)&ifreq) < 0) {
                        syslog(LOG_ERR, "ioctl (get netmask)");
                        continue;
@@ -95,9 +112,8 @@ ifinit()
                        ifs.int_netmask = IN_CLASSC_NET;
                ifs.int_net = i & ifs.int_netmask;
                ifs.int_subnet = i & ifs.int_subnetmask;
                        ifs.int_netmask = IN_CLASSC_NET;
                ifs.int_net = i & ifs.int_netmask;
                ifs.int_subnet = i & ifs.int_subnetmask;
-               /* no one cares about software loopback interfaces */
-               if (ifs.int_net == LOOPBACKNET)
-                       continue;
+               if (ifs.int_subnetmask != ifs.int_netmask)
+                       ifs.int_flags |= IFF_SUBNET;
                ifp = (struct interface *)malloc(sizeof (struct interface));
                if (ifp == 0) {
                        printf("routed: out of memory\n");
                ifp = (struct interface *)malloc(sizeof (struct interface));
                if (ifp == 0) {
                        printf("routed: out of memory\n");
@@ -110,16 +126,25 @@ ifinit()
                 * back to ourself.  This is used below to
                 * decide if we should be a routing ``supplier''.
                 */
                 * back to ourself.  This is used below to
                 * decide if we should be a routing ``supplier''.
                 */
-               if ((ifs.int_flags & IFF_POINTOPOINT) == 0 ||
-                   if_ifwithaddr(&ifs.int_dstaddr) == 0)
+               if ((ifs.int_flags & IFF_LOOPBACK) == 0 &&
+                   ((ifs.int_flags & IFF_POINTOPOINT) == 0 ||
+                   if_ifwithaddr(&ifs.int_dstaddr) == 0))
                        externalinterfaces++;
                        externalinterfaces++;
+               /*
+                * If we have a point-to-point link, we want to act
+                * as a supplier even if it's our only interface,
+                * as that's the only way our peer on the other end
+                * can tell that the link is up.
+                */
+               if ((ifs.int_flags & IFF_POINTOPOINT) && supplier < 0)
+                       supplier = 1;
                ifp->int_name = malloc(strlen(ifr->ifr_name) + 1);
                if (ifp->int_name == 0) {
                        fprintf(stderr, "routed: ifinit: out of memory\n");
                ifp->int_name = malloc(strlen(ifr->ifr_name) + 1);
                if (ifp->int_name == 0) {
                        fprintf(stderr, "routed: ifinit: out of memory\n");
-                       goto bad;               /* ??? */
+                       syslog(LOG_ERR, "routed: ifinit: out of memory\n");
+                       return;
                }
                strcpy(ifp->int_name, ifr->ifr_name);
                }
                strcpy(ifp->int_name, ifr->ifr_name);
-               ifp->int_metric = 0;
                ifp->int_next = ifnet;
                ifnet = ifp;
                traceinit(ifp);
                ifp->int_next = ifnet;
                ifnet = ifp;
                traceinit(ifp);
@@ -128,21 +153,21 @@ ifinit()
        if (externalinterfaces > 1 && supplier < 0)
                supplier = 1;
        close(s);
        if (externalinterfaces > 1 && supplier < 0)
                supplier = 1;
        close(s);
-       return;
-bad:
-       sleep(60);
-       close(kmem), close(s);
-       execv("/etc/routed", argv0);
-       _exit(0177);
 }
 
 }
 
+/*
+ * Add route for interface if not currently installed.
+ * Create route to other end if a point-to-point link,
+ * otherwise a route to this (sub)network.
+ * INTERNET SPECIFIC.
+ */
 addrouteforif(ifp)
 addrouteforif(ifp)
-       struct interface *ifp;
+       register struct interface *ifp;
 {
        struct sockaddr_in net;
        struct sockaddr *dst;
        int state, metric;
 {
        struct sockaddr_in net;
        struct sockaddr *dst;
        int state, metric;
-       struct rt_entry *rt;
+       register struct rt_entry *rt;
 
        if (ifp->int_flags & IFF_POINTOPOINT)
                dst = &ifp->int_dstaddr;
 
        if (ifp->int_flags & IFF_POINTOPOINT)
                dst = &ifp->int_dstaddr;
@@ -153,14 +178,78 @@ addrouteforif(ifp)
                dst = (struct sockaddr *)&net;
        }
        rt = rtfind(dst);
                dst = (struct sockaddr *)&net;
        }
        rt = rtfind(dst);
-       if (rt && (rt->rt_state & RTS_INTERFACE))
+       if (rt &&
+           (rt->rt_state & (RTS_INTERFACE | RTS_INTERNAL)) == RTS_INTERFACE)
                return;
        if (rt)
                rtdelete(rt);
                return;
        if (rt)
                rtdelete(rt);
+       /*
+        * If interface on subnetted network,
+        * install route to network as well.
+        * This is meant for external viewers.
+        */
+       if ((ifp->int_flags & (IFF_SUBNET|IFF_POINTOPOINT)) == IFF_SUBNET) {
+               struct in_addr subnet;
+
+               subnet = net.sin_addr;
+               net.sin_addr = inet_makeaddr(ifp->int_net, INADDR_ANY);
+               rt = rtfind(dst);
+               if (rt == 0)
+                       rtadd(dst, &ifp->int_addr, ifp->int_metric,
+                           ((ifp->int_flags & (IFF_INTERFACE|IFF_REMOTE)) |
+                           RTS_PASSIVE | RTS_INTERNAL | RTS_SUBNET));
+               else if ((rt->rt_state & (RTS_INTERNAL|RTS_SUBNET)) == 
+                   (RTS_INTERNAL|RTS_SUBNET) &&
+                   ifp->int_metric < rt->rt_metric)
+                       rtchange(rt, &rt->rt_router, ifp->int_metric);
+               net.sin_addr = subnet;
+       }
        if (ifp->int_transitions++ > 0)
                syslog(LOG_ERR, "re-installing interface %s", ifp->int_name);
        if (ifp->int_transitions++ > 0)
                syslog(LOG_ERR, "re-installing interface %s", ifp->int_name);
-       rtadd(dst, &ifp->int_addr, ifp->int_metric,
-               ifp->int_flags & (IFF_INTERFACE|IFF_PASSIVE|IFF_REMOTE));
+       state = ifp->int_flags &
+           (IFF_INTERFACE | IFF_PASSIVE | IFF_REMOTE | IFF_SUBNET);
+       if (ifp->int_flags & IFF_POINTOPOINT &&
+           (ntohl(((struct sockaddr_in *)&ifp->int_dstaddr)->sin_addr.s_addr) &
+           ifp->int_netmask) != ifp->int_net)
+               state &= ~RTS_SUBNET;
+       if (ifp->int_flags & IFF_LOOPBACK)
+               state |= RTS_EXTERNAL;
+       rtadd(dst, &ifp->int_addr, ifp->int_metric, state);
+       if (ifp->int_flags & IFF_POINTOPOINT && foundloopback)
+               add_ptopt_localrt(ifp);
+}
+
+/*
+ * Add route to local end of point-to-point using loopback.
+ * If a route to this network is being sent to neighbors on other nets,
+ * mark this route as subnet so we don't have to propagate it too.
+ */
+add_ptopt_localrt(ifp)
+       register struct interface *ifp;
+{
+       struct rt_entry *rt;
+       struct sockaddr *dst;
+       struct sockaddr_in net;
+       int state;
+
+       state = RTS_INTERFACE | RTS_PASSIVE;
+
+       /* look for route to logical network */
+       bzero((char *)&net, sizeof (net));
+       net.sin_family = AF_INET;
+       net.sin_addr = inet_makeaddr(ifp->int_net, INADDR_ANY);
+       dst = (struct sockaddr *)&net;
+       rt = rtfind(dst);
+       if (rt && rt->rt_state & RTS_INTERNAL)
+               state |= RTS_SUBNET;
+
+       dst = &ifp->int_addr;
+       if (rt = rtfind(dst)) {
+               if (rt && rt->rt_state & RTS_INTERFACE)
+                       return;
+               rtdelete(rt);
+       }
+       rtadd(dst, &loopaddr, 0, state);
 }
 
 /*
 }
 
 /*
@@ -175,6 +264,8 @@ addrouteforif(ifp)
  * not marked passive are treated as if they were directly
  * connected -- they're added into the interface list so we'll
  * send them routing updates.
  * not marked passive are treated as if they were directly
  * connected -- they're added into the interface list so we'll
  * send them routing updates.
+ *
+ * PASSIVE ENTRIES AREN'T NEEDED OR USED ON GATEWAYS RUNNING EGP.
  */
 gwkludge()
 {
  */
 gwkludge()
 {
@@ -182,7 +273,7 @@ gwkludge()
        FILE *fp;
        char *type, *dname, *gname, *qual, buf[BUFSIZ];
        struct interface *ifp;
        FILE *fp;
        char *type, *dname, *gname, *qual, buf[BUFSIZ];
        struct interface *ifp;
-       int metric;
+       int metric, n;
        struct rt_entry route;
 
        fp = fopen("/etc/gateways", "r");
        struct rt_entry route;
 
        fp = fopen("/etc/gateways", "r");
@@ -195,12 +286,12 @@ gwkludge()
        bzero((char *)&dst, sizeof (dst));
        bzero((char *)&gate, sizeof (gate));
        bzero((char *)&route, sizeof(route));
        bzero((char *)&dst, sizeof (dst));
        bzero((char *)&gate, sizeof (gate));
        bzero((char *)&route, sizeof(route));
-       /* format: {net | host} XX gateway XX metric DD [passive]\n */
+/* format: {net | host} XX gateway XX metric DD [passive | external]\n */
 #define        readentry(fp) \
        fscanf((fp), "%s %s gateway %s metric %d %s\n", \
                type, dname, gname, &metric, qual)
        for (;;) {
 #define        readentry(fp) \
        fscanf((fp), "%s %s gateway %s metric %d %s\n", \
                type, dname, gname, &metric, qual)
        for (;;) {
-               if (readentry(fp) == EOF)
+               if ((n = readentry(fp)) == EOF)
                        break;
                if (!getnetorhostname(type, dname, &dst))
                        continue;
                        break;
                if (!getnetorhostname(type, dname, &dst))
                        continue;
@@ -224,6 +315,17 @@ gwkludge()
                        (void) ioctl(s, SIOCADDRT, (char *)&route.rt_rt);
                        continue;
                }
                        (void) ioctl(s, SIOCADDRT, (char *)&route.rt_rt);
                        continue;
                }
+               if (strcmp(qual, "external") == 0) {
+                       /*
+                        * Entries marked external are handled
+                        * by other means, e.g. EGP,
+                        * and are placed in our tables only
+                        * to prevent overriding them
+                        * with something else.
+                        */
+                       rtadd(&dst, &gate, metric, RTS_EXTERNAL|RTS_PASSIVE);
+                       continue;
+               }
                /* assume no duplicate entries */
                externalinterfaces++;
                ifp = (struct interface *)malloc(sizeof (*ifp));
                /* assume no duplicate entries */
                externalinterfaces++;
                ifp = (struct interface *)malloc(sizeof (*ifp));