From: Mike Karels Date: Mon, 24 Feb 1986 11:34:27 +0000 (-0800) Subject: don't rely on AF_MAX (comes from kernel include): check for nil explicitly, X-Git-Tag: BSD-4_3-Snapshot-Development~3186 X-Git-Url: https://git.subgeniuskitty.com/unix-history/.git/commitdiff_plain/17fe297f44883f6518ccb6959e1808c1bb342fdd don't rely on AF_MAX (comes from kernel include): check for nil explicitly, 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 --- diff --git a/usr/src/sbin/routed/af.c b/usr/src/sbin/routed/af.c index 4fcd73eec9..a6e02577f9 100644 --- a/usr/src/sbin/routed/af.c +++ b/usr/src/sbin/routed/af.c @@ -5,7 +5,7 @@ */ #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" @@ -13,23 +13,20 @@ static char sccsid[] = "@(#)af.c 5.2 (Berkeley) %G%"; /* * 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(); -#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, \ - inet_ishost, inet_canon } + inet_ishost, inet_canon, inet_format } 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 }; @@ -132,69 +129,11 @@ inet_canon(sin) 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)); } diff --git a/usr/src/sbin/routed/af.h b/usr/src/sbin/routed/af.h index 45e4e5a853..ac8ceff595 100644 --- a/usr/src/sbin/routed/af.h +++ b/usr/src/sbin/routed/af.h @@ -3,7 +3,7 @@ * 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 */ + char *(*af_format)(); /* convert address to string */ }; /* @@ -32,4 +33,5 @@ struct afhash { 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 */ diff --git a/usr/src/sbin/routed/if.c b/usr/src/sbin/routed/if.c index 307493fa4a..dc2fd5ce4f 100644 --- a/usr/src/sbin/routed/if.c +++ b/usr/src/sbin/routed/if.c @@ -5,7 +5,7 @@ */ #ifndef lint -static char sccsid[] = "@(#)if.c 5.1 (Berkeley) %G%"; +static char sccsid[] = "@(#)if.c 5.2 (Berkeley) %G%"; #endif not lint /* @@ -70,7 +70,7 @@ if_ifwithnet(addr) 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) { @@ -97,7 +97,7 @@ if_iflookup(addr) 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; diff --git a/usr/src/sbin/routed/input.c b/usr/src/sbin/routed/input.c index 66e4f5a0e1..e60b85298e 100644 --- a/usr/src/sbin/routed/input.c +++ b/usr/src/sbin/routed/input.c @@ -5,13 +5,14 @@ */ #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" +#include /* * Process a newly received packet. @@ -29,9 +30,13 @@ rip_input(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; - afp = &afswitch[from->sa_family]; + } 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''. + * 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 && - 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; } @@ -122,11 +131,23 @@ rip_input(from, size) } 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; - 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; + } rt = rtlookup(&n->rip_dst); if (rt == 0) { if (n->rip_metric < HOPCNT_INFINITY) diff --git a/usr/src/sbin/routed/main.c b/usr/src/sbin/routed/main.c index 8e155d8e4c..9c766eb391 100644 --- a/usr/src/sbin/routed/main.c +++ b/usr/src/sbin/routed/main.c @@ -11,7 +11,7 @@ char copyright[] = #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 /* @@ -44,6 +44,7 @@ main(argc, argv) 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"); @@ -68,6 +69,12 @@ main(argc, argv) } 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; } diff --git a/usr/src/sbin/routed/tables.c b/usr/src/sbin/routed/tables.c index 4653b0e410..364f5fbe06 100644 --- a/usr/src/sbin/routed/tables.c +++ b/usr/src/sbin/routed/tables.c @@ -5,7 +5,7 @@ */ #ifndef lint -static char sccsid[] = "@(#)tables.c 5.1 (Berkeley) %G%"; +static char sccsid[] = "@(#)tables.c 5.2 (Berkeley) %G%"; #endif not lint /* @@ -35,7 +35,7 @@ rtlookup(dst) 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; @@ -70,7 +70,7 @@ rtfind(dst) 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; @@ -109,7 +109,7 @@ rtadd(dst, gate, metric, state) 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;