BSD 4_4 release
[unix-history] / usr / src / sbin / routed / input.c
index 0b067b7..fb0ee79 100644 (file)
@@ -1,12 +1,39 @@
 /*
 /*
- * Copyright (c) 1983 Regents of the University of California.
- * All rights reserved.  The Berkeley software License Agreement
- * specifies the terms and conditions for redistribution.
+ * Copyright (c) 1983, 1988, 1993
+ *     The Regents of the University of California.  All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ * 3. All advertising materials mentioning features or use of this software
+ *    must display the following acknowledgement:
+ *     This product includes software developed by the University of
+ *     California, Berkeley and its contributors.
+ * 4. Neither the name of the University nor the names of its contributors
+ *    may be used to endorse or promote products derived from this software
+ *    without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
  */
 
 #ifndef lint
  */
 
 #ifndef lint
-static char sccsid[] = "@(#)input.c    5.12 (Berkeley) %G%";
-#endif not lint
+static char sccsid[] = "@(#)input.c    8.1 (Berkeley) 6/5/93";
+#endif /* not lint */
 
 /*
  * Routing Table Management Daemon
 
 /*
  * Routing Table Management Daemon
@@ -17,43 +44,56 @@ static char sccsid[] = "@(#)input.c 5.12 (Berkeley) %G%";
 /*
  * Process a newly received packet.
  */
 /*
  * Process a newly received packet.
  */
-rip_input(from, size)
+rip_input(from, rip, size)
        struct sockaddr *from;
        struct sockaddr *from;
+       register struct rip *rip;
        int size;
 {
        register struct rt_entry *rt;
        register struct netinfo *n;
        register struct interface *ifp;
        struct interface *if_ifwithdstaddr();
        int size;
 {
        register struct rt_entry *rt;
        register struct netinfo *n;
        register struct interface *ifp;
        struct interface *if_ifwithdstaddr();
-       int newsize;
+       int count, changes = 0;
        register struct afswitch *afp;
        register struct afswitch *afp;
-       static struct sockaddr badfrom;
+       static struct sockaddr badfrom, badfrom2;
 
        ifp = 0;
 
        ifp = 0;
-       TRACE_INPUT(ifp, from, size);
+       TRACE_INPUT(ifp, from, (char *)rip, size);
        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",
        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);
+                   from->sa_family, rip->rip_cmd);
                return;
        }
                return;
        }
-       switch (msg->rip_cmd) {
+       if (rip->rip_vers == 0) {
+               syslog(LOG_ERR,
+                   "RIP version 0 packet received from %s! (cmd %d)",
+                   (*afswitch[from->sa_family].af_format)(from), rip->rip_cmd);
+               return;
+       }
+       switch (rip->rip_cmd) {
 
        case RIPCMD_REQUEST:
 
        case RIPCMD_REQUEST:
-               newsize = 0;
-               size -= 4 * sizeof (char);
-               n = msg->rip_nets;
-               while (size > 0) {
-                       if (size < sizeof (struct netinfo))
+               n = rip->rip_nets;
+               count = size - ((char *)n - (char *)rip);
+               if (count < sizeof (struct netinfo))
+                       return;
+               for (; count > 0; n++) {
+                       if (count < sizeof (struct netinfo))
                                break;
                                break;
-                       size -= sizeof (struct netinfo);
+                       count -= sizeof (struct netinfo);
 
 
-                       if (msg->rip_vers > 0) {
-                               n->rip_dst.sa_family =
-                                       ntohs(n->rip_dst.sa_family);
-                               n->rip_metric = ntohl(n->rip_metric);
-                       }
+#if BSD < 198810
+                       if (sizeof(n->rip_dst.sa_family) > 1)   /* XXX */
+                           n->rip_dst.sa_family = ntohs(n->rip_dst.sa_family);
+#else
+#define osa(x) ((struct osockaddr *)(&(x)))
+                           n->rip_dst.sa_family =
+                                       ntohs(osa(n->rip_dst)->sa_family);
+                           n->rip_dst.sa_len = sizeof(n->rip_dst);
+#endif
+                       n->rip_metric = ntohl(n->rip_metric);
                        /* 
                         * 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''.
@@ -62,9 +102,9 @@ rip_input(from, size)
                         * (eg, query).
                         */
                        if (n->rip_dst.sa_family == AF_UNSPEC &&
                         * (eg, query).
                         */
                        if (n->rip_dst.sa_family == AF_UNSPEC &&
-                           n->rip_metric == HOPCNT_INFINITY && size == 0) {
+                           n->rip_metric == HOPCNT_INFINITY && count == 0) {
                                if (supplier || (*afp->af_portmatch)(from) == 0)
                                if (supplier || (*afp->af_portmatch)(from) == 0)
-                                       supply(from, 0, 0);
+                                       supply(from, 0, 0, 0);
                                return;
                        }
                        if (n->rip_dst.sa_family < af_max &&
                                return;
                        }
                        if (n->rip_dst.sa_family < af_max &&
@@ -72,20 +112,21 @@ rip_input(from, size)
                                rt = rtlookup(&n->rip_dst);
                        else
                                rt = 0;
                                rt = rtlookup(&n->rip_dst);
                        else
                                rt = 0;
+#define min(a, b) (a < b ? a : b)
                        n->rip_metric = rt == 0 ? HOPCNT_INFINITY :
                        n->rip_metric = rt == 0 ? HOPCNT_INFINITY :
-                               min(rt->rt_metric+1, HOPCNT_INFINITY);
-                       if (msg->rip_vers > 0) {
-                               n->rip_dst.sa_family =
-                                       htons(n->rip_dst.sa_family);
-                               n->rip_metric = htonl(n->rip_metric);
-                       }
-                       n++, newsize += sizeof (struct netinfo);
-               }
-               if (newsize > 0) {
-                       msg->rip_cmd = RIPCMD_RESPONSE;
-                       newsize += sizeof (int);
-                       (*afp->af_output)(s, 0, from, newsize);
+                               min(rt->rt_metric + 1, HOPCNT_INFINITY);
+#if BSD < 198810
+                       if (sizeof(n->rip_dst.sa_family) > 1)   /* XXX */
+                           n->rip_dst.sa_family = htons(n->rip_dst.sa_family);
+#else
+                           osa(n->rip_dst)->sa_family =
+                                               htons(n->rip_dst.sa_family);
+#endif
+                       n->rip_metric = htonl(n->rip_metric);
                }
                }
+               rip->rip_cmd = RIPCMD_RESPONSE;
+               bcopy((char *)rip, packet, size);
+               (*afp->af_output)(s, 0, from, size);
                return;
 
        case RIPCMD_TRACEON:
                return;
 
        case RIPCMD_TRACEON:
@@ -94,14 +135,15 @@ rip_input(from, size)
                if ((*afp->af_portcheck)(from) == 0)
                        return;
                if ((ifp = if_iflookup(from)) == 0 || (ifp->int_flags &
                if ((*afp->af_portcheck)(from) == 0)
                        return;
                if ((ifp = if_iflookup(from)) == 0 || (ifp->int_flags &
-                   (IFF_BROADCAST | IFF_POINTOPOINT | IFF_REMOTE)) == 0) {
+                   (IFF_BROADCAST | IFF_POINTOPOINT | IFF_REMOTE)) == 0 ||
+                   ifp->int_flags & IFF_PASSIVE) {
                        syslog(LOG_ERR, "trace command from unknown router, %s",
                            (*afswitch[from->sa_family].af_format)(from));
                        return;
                }
                        syslog(LOG_ERR, "trace command from unknown router, %s",
                            (*afswitch[from->sa_family].af_format)(from));
                        return;
                }
-               packet[size] = '\0';
-               if (msg->rip_cmd == RIPCMD_TRACEON)
-                       traceon(msg->rip_tracefile);
+               ((char *)rip)[size] = '\0';
+               if (rip->rip_cmd == RIPCMD_TRACEON)
+                       traceon(rip->rip_tracefile);
                else
                        traceoff();
                return;
                else
                        traceoff();
                return;
@@ -114,6 +156,12 @@ rip_input(from, size)
                /* are we talking to ourselves? */
                ifp = if_ifwithaddr(from);
                if (ifp) {
                /* are we talking to ourselves? */
                ifp = if_ifwithaddr(from);
                if (ifp) {
+                       if (ifp->int_flags & IFF_PASSIVE) {
+                               syslog(LOG_ERR,
+                                 "bogus input (from passive interface, %s)",
+                                 (*afswitch[from->sa_family].af_format)(from));
+                               return;
+                       }
                        rt = rtfind(from);
                        if (rt == 0 || ((rt->rt_state & RTS_INTERFACE) == 0) &&
                            rt->rt_metric >= ifp->int_metric) 
                        rt = rtfind(from);
                        if (rt == 0 || ((rt->rt_state & RTS_INTERFACE) == 0) &&
                            rt->rt_metric >= ifp->int_metric) 
@@ -140,7 +188,8 @@ rip_input(from, size)
                 * and from those listed in /etc/gateways.
                 */
                if ((ifp = if_iflookup(from)) == 0 || (ifp->int_flags &
                 * and from those listed in /etc/gateways.
                 */
                if ((ifp = if_iflookup(from)) == 0 || (ifp->int_flags &
-                   (IFF_BROADCAST | IFF_POINTOPOINT | IFF_REMOTE)) == 0) {
+                   (IFF_BROADCAST | IFF_POINTOPOINT | IFF_REMOTE)) == 0 ||
+                   ifp->int_flags & IFF_PASSIVE) {
                        if (bcmp((char *)from, (char *)&badfrom,
                            sizeof(badfrom)) != 0) {
                                syslog(LOG_ERR,
                        if (bcmp((char *)from, (char *)&badfrom,
                            sizeof(badfrom)) != 0) {
                                syslog(LOG_ERR,
@@ -151,17 +200,20 @@ rip_input(from, size)
                        return;
                }
                size -= 4 * sizeof (char);
                        return;
                }
                size -= 4 * sizeof (char);
-               n = msg->rip_nets;
+               n = rip->rip_nets;
                for (; size > 0; size -= sizeof (struct netinfo), n++) {
                        if (size < sizeof (struct netinfo))
                                break;
                for (; size > 0; size -= sizeof (struct netinfo), n++) {
                        if (size < sizeof (struct netinfo))
                                break;
-                       if (msg->rip_vers > 0) {
+#if BSD < 198810
+                       if (sizeof(n->rip_dst.sa_family) > 1)   /* XXX */
                                n->rip_dst.sa_family =
                                        ntohs(n->rip_dst.sa_family);
                                n->rip_dst.sa_family =
                                        ntohs(n->rip_dst.sa_family);
-                               n->rip_metric = ntohl(n->rip_metric);
-                       }
-                       if ((unsigned) n->rip_metric > HOPCNT_INFINITY)
-                               continue;
+#else
+                           n->rip_dst.sa_family =
+                                       ntohs(osa(n->rip_dst)->sa_family);
+                           n->rip_dst.sa_len = sizeof(n->rip_dst);
+#endif
+                       n->rip_metric = ntohl(n->rip_metric);
                        if (n->rip_dst.sa_family >= af_max ||
                            (afp = &afswitch[n->rip_dst.sa_family])->af_hash ==
                            (int (*)())0) {
                        if (n->rip_dst.sa_family >= af_max ||
                            (afp = &afswitch[n->rip_dst.sa_family])->af_hash ==
                            (int (*)())0) {
@@ -179,11 +231,25 @@ rip_input(from, size)
                                   from->sa_family);
                                continue;
                        }
                                   from->sa_family);
                                continue;
                        }
+                       if (n->rip_metric == 0 ||
+                           (unsigned) n->rip_metric > HOPCNT_INFINITY) {
+                               if (bcmp((char *)from, (char *)&badfrom2,
+                                   sizeof(badfrom2)) != 0) {
+                                       syslog(LOG_ERR,
+                                           "bad metric (%d) from %s\n",
+                                           n->rip_metric,
+                                 (*afswitch[from->sa_family].af_format)(from));
+                                       badfrom2 = *from;
+                               }
+                               continue;
+                       }
                        /*
                         * Adjust metric according to incoming interface.
                         */
                        /*
                         * Adjust metric according to incoming interface.
                         */
-                       if ((unsigned)n->rip_metric < HOPCNT_INFINITY)
+                       if ((unsigned) n->rip_metric < HOPCNT_INFINITY)
                                n->rip_metric += ifp->int_metric;
                                n->rip_metric += ifp->int_metric;
+                       if ((unsigned) n->rip_metric > HOPCNT_INFINITY)
+                               n->rip_metric = HOPCNT_INFINITY;
                        rt = rtlookup(&n->rip_dst);
                        if (rt == 0 ||
                            (rt->rt_state & (RTS_INTERNAL|RTS_INTERFACE)) ==
                        rt = rtlookup(&n->rip_dst);
                        if (rt == 0 ||
                            (rt->rt_state & (RTS_INTERNAL|RTS_INTERFACE)) ==
@@ -196,38 +262,101 @@ rip_input(from, size)
                                if (rt && rt->rt_state & RTS_SUBNET &&
                                    (*afp->af_sendroute)(rt, from))
                                        continue;
                                if (rt && rt->rt_state & RTS_SUBNET &&
                                    (*afp->af_sendroute)(rt, from))
                                        continue;
-                               /*
-                                * Look for an equivalent route that includes
-                                * this one before adding this route.
-                                */
-                               rt = rtfind(&n->rip_dst);
-                               if (rt && equal(from, &rt->rt_router))
-                                       continue;
-                               if (n->rip_metric < HOPCNT_INFINITY)
+                               if ((unsigned)n->rip_metric < HOPCNT_INFINITY) {
+                                   /*
+                                    * Look for an equivalent route that
+                                    * includes this one before adding
+                                    * this route.
+                                    */
+                                   rt = rtfind(&n->rip_dst);
+                                   if (rt && equal(from, &rt->rt_router))
+                                           continue;
                                    rtadd(&n->rip_dst, from, n->rip_metric, 0);
                                    rtadd(&n->rip_dst, from, n->rip_metric, 0);
+                                   changes++;
+                               }
                                continue;
                        }
 
                        /*
                         * Update if from gateway and different,
                                continue;
                        }
 
                        /*
                         * Update if from gateway and different,
-                        * shorter, or getting stale and equivalent.
+                        * shorter, or equivalent but old route
+                        * is getting stale.
                         */
                        if (equal(from, &rt->rt_router)) {
                                if (n->rip_metric != rt->rt_metric) {
                                        rtchange(rt, from, n->rip_metric);
                         */
                        if (equal(from, &rt->rt_router)) {
                                if (n->rip_metric != rt->rt_metric) {
                                        rtchange(rt, from, n->rip_metric);
+                                       changes++;
+                                       rt->rt_timer = 0;
                                        if (rt->rt_metric >= HOPCNT_INFINITY)
                                                rt->rt_timer =
                                                    GARBAGE_TIME - EXPIRE_TIME;
                                } else if (rt->rt_metric < HOPCNT_INFINITY)
                                        rt->rt_timer = 0;
                                        if (rt->rt_metric >= HOPCNT_INFINITY)
                                                rt->rt_timer =
                                                    GARBAGE_TIME - EXPIRE_TIME;
                                } else if (rt->rt_metric < HOPCNT_INFINITY)
                                        rt->rt_timer = 0;
-                       } else if ((unsigned) (n->rip_metric) < rt->rt_metric ||
-                           (rt->rt_timer > (EXPIRE_TIME/2) &&
-                           rt->rt_metric == n->rip_metric &&
-                           n->rip_metric < HOPCNT_INFINITY)) {
+                       } else if ((unsigned) n->rip_metric < rt->rt_metric ||
+                           (rt->rt_metric == n->rip_metric &&
+                           rt->rt_timer > (EXPIRE_TIME/2) &&
+                           (unsigned) n->rip_metric < HOPCNT_INFINITY)) {
                                rtchange(rt, from, n->rip_metric);
                                rtchange(rt, from, n->rip_metric);
+                               changes++;
                                rt->rt_timer = 0;
                        }
                }
                                rt->rt_timer = 0;
                        }
                }
-               return;
+               break;
+       }
+
+       /*
+        * If changes have occurred, and if we have not sent a broadcast
+        * recently, send a dynamic update.  This update is sent only
+        * on interfaces other than the one on which we received notice
+        * of the change.  If we are within MIN_WAITTIME of a full update,
+        * don't bother sending; if we just sent a dynamic update
+        * and set a timer (nextbcast), delay until that time.
+        * If we just sent a full update, delay the dynamic update.
+        * Set a timer for a randomized value to suppress additional
+        * dynamic updates until it expires; if we delayed sending
+        * the current changes, set needupdate.
+        */
+       if (changes && supplier &&
+          now.tv_sec - lastfullupdate.tv_sec < SUPPLY_INTERVAL-MAX_WAITTIME) {
+               u_long delay;
+               extern long random();
+
+               if (now.tv_sec - lastbcast.tv_sec >= MIN_WAITTIME &&
+                   timercmp(&nextbcast, &now, <)) {
+                       if (traceactions)
+                               fprintf(ftrace, "send dynamic update\n");
+                       toall(supply, RTS_CHANGED, ifp);
+                       lastbcast = now;
+                       needupdate = 0;
+                       nextbcast.tv_sec = 0;
+               } else {
+                       needupdate++;
+                       if (traceactions)
+                               fprintf(ftrace, "delay dynamic update\n");
+               }
+#define RANDOMDELAY()  (MIN_WAITTIME * 1000000 + \
+               (u_long)random() % ((MAX_WAITTIME - MIN_WAITTIME) * 1000000))
+
+               if (nextbcast.tv_sec == 0) {
+                       delay = RANDOMDELAY();
+                       if (traceactions)
+                               fprintf(ftrace,
+                                   "inhibit dynamic update for %d usec\n",
+                                   delay);
+                       nextbcast.tv_sec = delay / 1000000;
+                       nextbcast.tv_usec = delay % 1000000;
+                       timevaladd(&nextbcast, &now);
+                       /*
+                        * If the next possibly dynamic update
+                        * is within MIN_WAITTIME of the next full update,
+                        * force the delay past the full update,
+                        * or we might send a dynamic update just before
+                        * the full update.
+                        */
+                       if (nextbcast.tv_sec > lastfullupdate.tv_sec +
+                           SUPPLY_INTERVAL - MIN_WAITTIME)
+                               nextbcast.tv_sec = lastfullupdate.tv_sec +
+                                   SUPPLY_INTERVAL + 1;
+               }
        }
 }
        }
 }