don't rely on AF_MAX (comes from kernel include): check for nil explicitly,
authorMike Karels <karels@ucbvax.Berkeley.EDU>
Mon, 24 Feb 1986 11:34:27 +0000 (03:34 -0800)
committerMike Karels <karels@ucbvax.Berkeley.EDU>
Mon, 24 Feb 1986 11:34:27 +0000 (03:34 -0800)
use our own idea of table size.  Use syslog to (optionally) log bad packets.
Don't respond to routing inquiries from a router unless we are suppliers
(really want to respondto everything but broadcasts, but haven't got
the "to" address.)

SCCS-vsn: sbin/routed/af.c 5.3
SCCS-vsn: sbin/routed/af.h 5.2
SCCS-vsn: sbin/routed/if.c 5.2
SCCS-vsn: sbin/routed/input.c 5.2
SCCS-vsn: sbin/routed/main.c 5.6
SCCS-vsn: sbin/routed/tables.c 5.2

usr/src/sbin/routed/af.c
usr/src/sbin/routed/af.h
usr/src/sbin/routed/if.c
usr/src/sbin/routed/input.c
usr/src/sbin/routed/main.c
usr/src/sbin/routed/tables.c

index 4fcd73e..a6e0257 100644 (file)
@@ -5,7 +5,7 @@
  */
 
 #ifndef lint
  */
 
 #ifndef lint
-static char sccsid[] = "@(#)af.c       5.2 (Berkeley) %G%";
+static char sccsid[] = "@(#)af.c       5.3 (Berkeley) %G%";
 #endif not lint
 
 #include "defs.h"
 #endif not lint
 
 #include "defs.h"
@@ -13,23 +13,20 @@ static char sccsid[] = "@(#)af.c    5.2 (Berkeley) %G%";
 /*
  * Address family support routines
  */
 /*
  * Address family support routines
  */
-int    null_hash(), null_netmatch(), null_output(),
-       null_portmatch(), null_portcheck(),
-       null_checkhost(), null_ishost(), null_canon();
 int    inet_hash(), inet_netmatch(), inet_output(),
        inet_portmatch(), inet_portcheck(),
        inet_checkhost(), inet_ishost(), inet_canon();
 int    inet_hash(), inet_netmatch(), inet_output(),
        inet_portmatch(), inet_portcheck(),
        inet_checkhost(), inet_ishost(), inet_canon();
-#define NIL \
-       { null_hash,            null_netmatch,          null_output, \
-         null_portmatch,       null_portcheck,         null_checkhost, \
-         null_ishost,          null_canon }
+char   *inet_format();
+#define NIL    { 0 }
 #define        INET \
        { inet_hash,            inet_netmatch,          inet_output, \
          inet_portmatch,       inet_portcheck,         inet_checkhost, \
 #define        INET \
        { inet_hash,            inet_netmatch,          inet_output, \
          inet_portmatch,       inet_portcheck,         inet_checkhost, \
-         inet_ishost,          inet_canon }
+         inet_ishost,          inet_canon,             inet_format }
 
 struct afswitch afswitch[AF_MAX] =
 
 struct afswitch afswitch[AF_MAX] =
-       { NIL, NIL, INET, INET, NIL, NIL, NIL, NIL, NIL, NIL, NIL };
+       { NIL, NIL, INET, };
+
+int af_max = sizeof(afswitch) / sizeof(afswitch[0]);
 
 struct sockaddr_in inet_default = { AF_INET, INADDR_ANY };
 
 
 struct sockaddr_in inet_default = { AF_INET, INADDR_ANY };
 
@@ -132,69 +129,11 @@ inet_canon(sin)
        sin->sin_port = 0;
 }
 
        sin->sin_port = 0;
 }
 
-/*ARGSUSED*/
-null_hash(addr, hp)
-       struct sockaddr *addr;
-       struct afhash *hp;
-{
-
-       hp->afh_nethash = hp->afh_hosthash = 0;
-}
-
-/*ARGSUSED*/
-null_netmatch(a1, a2)
-       struct sockaddr *a1, *a2;
-{
-
-       return (0);
-}
-
-/*ARGSUSED*/
-null_output(s, f, a1, n)
-       int s, f;
-       struct sockaddr *a1;
-       int n;
-{
-
-       ;
-}
-
-/*ARGSUSED*/
-null_portmatch(a1)
-       struct sockaddr *a1;
-{
-
-       return (0);
-}
-
-/*ARGSUSED*/
-null_portcheck(a1)
-       struct sockaddr *a1;
-{
-
-       return (0);
-}
-
-/*ARGSUSED*/
-null_ishost(a1)
-       struct sockaddr *a1;
-{
-
-       return (0);
-}
-
-/*ARGSUSED*/
-null_checkhost(a1)
-       struct sockaddr *a1;
-{
-
-       return (0);
-}
-
-/*ARGSUSED*/
-null_canon(a1)
-       struct sockaddr *a1;
+char *
+inet_format(sin)
+       struct sockaddr_in *sin;
 {
 {
+       char *inet_ntoa();
 
 
-       ;
+       return (inet_ntoa(sin->sin_addr));
 }
 }
index 45e4e5a..ac8ceff 100644 (file)
@@ -3,7 +3,7 @@
  * All rights reserved.  The Berkeley software License Agreement
  * specifies the terms and conditions for redistribution.
  *
  * All rights reserved.  The Berkeley software License Agreement
  * specifies the terms and conditions for redistribution.
  *
- *     @(#)af.h        5.1 (Berkeley) %G%
+ *     @(#)af.h        5.2 (Berkeley) %G%
  */
 
 /*
  */
 
 /*
@@ -22,6 +22,7 @@ struct afswitch {
        int     (*af_checkhost)();      /* tells if address for host or net */
        int     (*af_ishost)();         /* tells if address is valid */
        int     (*af_canon)();          /* canonicalize address for compares */
        int     (*af_checkhost)();      /* tells if address for host or net */
        int     (*af_ishost)();         /* tells if address is valid */
        int     (*af_canon)();          /* canonicalize address for compares */
+       char    *(*af_format)();        /* convert address to string */
 };
 
 /*
 };
 
 /*
@@ -32,4 +33,5 @@ struct afhash {
        u_int   afh_nethash;            /* network based hash */
 };
 
        u_int   afh_nethash;            /* network based hash */
 };
 
-struct afswitch afswitch[AF_MAX];      /* table proper */
+struct afswitch afswitch[];            /* table proper */
+int    af_max;                         /* number of entries in table */
index 307493f..dc2fd5c 100644 (file)
@@ -5,7 +5,7 @@
  */
 
 #ifndef lint
  */
 
 #ifndef lint
-static char sccsid[] = "@(#)if.c       5.1 (Berkeley) %G%";
+static char sccsid[] = "@(#)if.c       5.2 (Berkeley) %G%";
 #endif not lint
 
 /*
 #endif not lint
 
 /*
@@ -70,7 +70,7 @@ if_ifwithnet(addr)
        register int af = addr->sa_family;
        register int (*netmatch)();
 
        register int af = addr->sa_family;
        register int (*netmatch)();
 
-       if (af >= AF_MAX)
+       if (af >= af_max)
                return (0);
        netmatch = afswitch[af].af_netmatch;
        for (ifp = ifnet; ifp; ifp = ifp->int_next) {
                return (0);
        netmatch = afswitch[af].af_netmatch;
        for (ifp = ifnet; ifp; ifp = ifp->int_next) {
@@ -97,7 +97,7 @@ if_iflookup(addr)
        register int af = addr->sa_family;
        register int (*netmatch)();
 
        register int af = addr->sa_family;
        register int (*netmatch)();
 
-       if (af >= AF_MAX)
+       if (af >= af_max)
                return (0);
        maybe = 0;
        netmatch = afswitch[af].af_netmatch;
                return (0);
        maybe = 0;
        netmatch = afswitch[af].af_netmatch;
index 66e4f5a..e60b852 100644 (file)
@@ -5,13 +5,14 @@
  */
 
 #ifndef lint
  */
 
 #ifndef lint
-static char sccsid[] = "@(#)input.c    5.1 (Berkeley) %G%";
+static char sccsid[] = "@(#)input.c    5.2 (Berkeley) %G%";
 #endif not lint
 
 /*
  * Routing Table Management Daemon
  */
 #include "defs.h"
 #endif not lint
 
 /*
  * Routing Table Management Daemon
  */
 #include "defs.h"
+#include <sys/syslog.h>
 
 /*
  * Process a newly received packet.
 
 /*
  * Process a newly received packet.
@@ -29,9 +30,13 @@ rip_input(from, size)
 
        ifp = 0;
        TRACE_INPUT(ifp, from, size);
 
        ifp = 0;
        TRACE_INPUT(ifp, from, size);
-       if (from->sa_family >= AF_MAX)
+       if (from->sa_family >= af_max ||
+           (afp = &afswitch[from->sa_family])->af_hash == (int (*)())0) {
+               syslog(LOG_INFO,
+        "\"from\" address in unsupported address family (%d), cmd %d\n",
+                   from->sa_family, msg->rip_cmd);
                return;
                return;
-       afp = &afswitch[from->sa_family];
+       }
        switch (msg->rip_cmd) {
 
        case RIPCMD_REQUEST:
        switch (msg->rip_cmd) {
 
        case RIPCMD_REQUEST:
@@ -51,9 +56,13 @@ rip_input(from, size)
                        /* 
                         * A single entry with sa_family == AF_UNSPEC and
                         * metric ``infinity'' means ``all routes''.
                        /* 
                         * A single entry with sa_family == AF_UNSPEC and
                         * metric ``infinity'' means ``all routes''.
+                        * We respond to routers only if we are acting
+                        * as a supplier, or to anyone other than a router
+                        * (eg, query).
                         */
                        if (n->rip_dst.sa_family == AF_UNSPEC &&
                         */
                        if (n->rip_dst.sa_family == AF_UNSPEC &&
-                           n->rip_metric == HOPCNT_INFINITY && size == 0) {
+                           n->rip_metric == HOPCNT_INFINITY && size == 0 &&
+                           (supplier || (*afp->af_portcheck)(from) == 0)) {
                                supply(from, 0, ifp);
                                return;
                        }
                                supply(from, 0, ifp);
                                return;
                        }
@@ -122,11 +131,23 @@ rip_input(from, size)
                        }
                        if ((unsigned) n->rip_metric > HOPCNT_INFINITY)
                                continue;
                        }
                        if ((unsigned) n->rip_metric > HOPCNT_INFINITY)
                                continue;
-                       if (n->rip_dst.sa_family >= AF_MAX)
+                       if (n->rip_dst.sa_family >= af_max ||
+                           (afp = &afswitch[n->rip_dst.sa_family])->af_hash ==
+                           (int (*)())0) {
+                               syslog(LOG_INFO,
+               "route in unsupported address family (%d), from %s (af %d)\n",
+                                  n->rip_dst.sa_family,
+                                  (*afswitch[from->sa_family].af_format)(from),
+                                  from->sa_family);
                                continue;
                                continue;
-                       afp = &afswitch[n->rip_dst.sa_family];
-                       if (((*afp->af_checkhost)(&n->rip_dst)) == 0)
+                       }
+                       if (((*afp->af_checkhost)(&n->rip_dst)) == 0) {
+                               syslog(LOG_DEBUG,
+                                   "bad host in route from %s (af %d)\n",
+                                  (*afswitch[from->sa_family].af_format)(from),
+                                  from->sa_family);
                                continue;
                                continue;
+                       }
                        rt = rtlookup(&n->rip_dst);
                        if (rt == 0) {
                                if (n->rip_metric < HOPCNT_INFINITY)
                        rt = rtlookup(&n->rip_dst);
                        if (rt == 0) {
                                if (n->rip_metric < HOPCNT_INFINITY)
index 8e155d8..9c766eb 100644 (file)
@@ -11,7 +11,7 @@ char copyright[] =
 #endif not lint
 
 #ifndef lint
 #endif not lint
 
 #ifndef lint
-static char sccsid[] = "@(#)main.c     5.5 (Berkeley) %G%";
+static char sccsid[] = "@(#)main.c     5.6 (Berkeley) %G%";
 #endif not lint
 
 /*
 #endif not lint
 
 /*
@@ -44,6 +44,7 @@ main(argc, argv)
        
        argv0 = argv;
        openlog("routed", LOG_PID | LOG_ODELAY, LOG_DAEMON);
        
        argv0 = argv;
        openlog("routed", LOG_PID | LOG_ODELAY, LOG_DAEMON);
+       setlogmask(LOG_UPTO(LOG_WARNING));
        sp = getservbyname("router", "udp");
        if (sp == NULL) {
                fprintf(stderr, "routed: router/udp: unknown service\n");
        sp = getservbyname("router", "udp");
        if (sp == NULL) {
                fprintf(stderr, "routed: router/udp: unknown service\n");
@@ -68,6 +69,12 @@ main(argc, argv)
                }
                if (strcmp(*argv, "-t") == 0) {
                        tracepackets++;
                }
                if (strcmp(*argv, "-t") == 0) {
                        tracepackets++;
+                       setlogmask(LOG_UPTO(LOG_DEBUG));
+                       argv++, argc--;
+                       continue;
+               }
+               if (strcmp(*argv, "-d") == 0) {
+                       setlogmask(LOG_UPTO(LOG_DEBUG));
                        argv++, argc--;
                        continue;
                }
                        argv++, argc--;
                        continue;
                }
index 4653b0e..364f5fb 100644 (file)
@@ -5,7 +5,7 @@
  */
 
 #ifndef lint
  */
 
 #ifndef lint
-static char sccsid[] = "@(#)tables.c   5.1 (Berkeley) %G%";
+static char sccsid[] = "@(#)tables.c   5.2 (Berkeley) %G%";
 #endif not lint
 
 /*
 #endif not lint
 
 /*
@@ -35,7 +35,7 @@ rtlookup(dst)
        struct afhash h;
        int doinghost = 1;
 
        struct afhash h;
        int doinghost = 1;
 
-       if (dst->sa_family >= AF_MAX)
+       if (dst->sa_family >= af_max)
                return (0);
        (*afswitch[dst->sa_family].af_hash)(dst, &h);
        hash = h.afh_hosthash;
                return (0);
        (*afswitch[dst->sa_family].af_hash)(dst, &h);
        hash = h.afh_hosthash;
@@ -70,7 +70,7 @@ rtfind(dst)
        int af = dst->sa_family;
        int doinghost = 1, (*match)();
 
        int af = dst->sa_family;
        int doinghost = 1, (*match)();
 
-       if (af >= AF_MAX)
+       if (af >= af_max)
                return (0);
        (*afswitch[af].af_hash)(dst, &h);
        hash = h.afh_hosthash;
                return (0);
        (*afswitch[af].af_hash)(dst, &h);
        hash = h.afh_hosthash;
@@ -109,7 +109,7 @@ rtadd(dst, gate, metric, state)
        int af = dst->sa_family, flags;
        u_int hash;
 
        int af = dst->sa_family, flags;
        u_int hash;
 
-       if (af >= AF_MAX)
+       if (af >= af_max)
                return;
        (*afswitch[af].af_hash)(dst, &h);
        flags = (*afswitch[af].af_ishost)(dst) ? RTF_HOST : 0;
                return;
        (*afswitch[af].af_hash)(dst, &h);
        flags = (*afswitch[af].af_ishost)(dst) ? RTF_HOST : 0;