BSD 4_4 release
[unix-history] / usr / src / sbin / routed / input.c
index 396dbbb..fb0ee79 100644 (file)
@@ -1,17 +1,38 @@
 /*
 /*
- * Copyright (c) 1983, 1988 Regents of the University of California.
- * All rights reserved.
+ * Copyright (c) 1983, 1988, 1993
+ *     The Regents of the University of California.  All rights reserved.
  *
  *
- * Redistribution and use in source and binary forms are permitted
- * provided that this notice is preserved and that due credit is given
- * to the University of California at Berkeley. The name of the University
- * may not be used to endorse or promote products derived from this
- * software without specific prior written permission. This software
- * is provided ``as is'' without express or implied warranty.
+ * 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.17 (Berkeley) %G%";
+static char sccsid[] = "@(#)input.c    8.1 (Berkeley) 6/5/93";
 #endif /* not lint */
 
 /*
 #endif /* not lint */
 
 /*
@@ -23,43 +44,56 @@ static char sccsid[] = "@(#)input.c 5.17 (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;
        static struct sockaddr badfrom, badfrom2;
 
        ifp = 0;
        register struct afswitch *afp;
        static struct sockaddr badfrom, badfrom2;
 
        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;
+       }
+       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;
        }
                return;
        }
-       switch (msg->rip_cmd) {
+       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''.
@@ -68,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 &&
@@ -78,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 :
                                min(rt->rt_metric + 1, 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);
+#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:
@@ -106,9 +141,9 @@ rip_input(from, size)
                            (*afswitch[from->sa_family].af_format)(from));
                        return;
                }
                            (*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;
@@ -165,15 +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);
-                       }
+#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) {
@@ -232,6 +272,7 @@ rip_input(from, size)
                                    if (rt && equal(from, &rt->rt_router))
                                            continue;
                                    rtadd(&n->rip_dst, from, n->rip_metric, 0);
                                    if (rt && equal(from, &rt->rt_router))
                                            continue;
                                    rtadd(&n->rip_dst, from, n->rip_metric, 0);
+                                   changes++;
                                }
                                continue;
                        }
                                }
                                continue;
                        }
@@ -244,6 +285,7 @@ rip_input(from, size)
                        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 =
                                        rt->rt_timer = 0;
                                        if (rt->rt_metric >= HOPCNT_INFINITY)
                                                rt->rt_timer =
@@ -255,9 +297,66 @@ rip_input(from, size)
                            rt->rt_timer > (EXPIRE_TIME/2) &&
                            (unsigned) n->rip_metric < HOPCNT_INFINITY)) {
                                rtchange(rt, from, n->rip_metric);
                            rt->rt_timer > (EXPIRE_TIME/2) &&
                            (unsigned) n->rip_metric < HOPCNT_INFINITY)) {
                                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;
+               }
        }
 }
        }
 }