Home | History | Annotate | Line # | Download | only in routed
input.c revision 1.9
      1 /*	$NetBSD: input.c,v 1.9 1995/03/18 15:00:31 cgd Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1983, 1988, 1993
      5  *	The Regents of the University of California.  All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. All advertising materials mentioning features or use of this software
     16  *    must display the following acknowledgement:
     17  *	This product includes software developed by the University of
     18  *	California, Berkeley and its contributors.
     19  * 4. Neither the name of the University nor the names of its contributors
     20  *    may be used to endorse or promote products derived from this software
     21  *    without specific prior written permission.
     22  *
     23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     33  * SUCH DAMAGE.
     34  */
     35 
     36 #ifndef lint
     37 #if 0
     38 static char sccsid[] = "@(#)input.c	8.1 (Berkeley) 6/5/93";
     39 #else
     40 static char rcsid[] = "$NetBSD: input.c,v 1.9 1995/03/18 15:00:31 cgd Exp $";
     41 #endif
     42 #endif /* not lint */
     43 
     44 /*
     45  * Routing Table Management Daemon
     46  */
     47 #include "defs.h"
     48 #include <sys/syslog.h>
     49 
     50 /*
     51  * Process a newly received packet.
     52  */
     53 void
     54 rip_input(from, rip, size)
     55 	struct sockaddr *from;
     56 	register struct rip *rip;
     57 	int size;
     58 {
     59 	register struct rt_entry *rt;
     60 	register struct netinfo *n;
     61 	register struct interface *ifp;
     62 	struct interface *if_ifwithdstaddr();
     63 	int count, changes = 0;
     64 	register struct afswitch *afp;
     65 	static struct sockaddr badfrom, badfrom2;
     66 
     67 	ifp = 0;
     68 	TRACE_INPUT(ifp, (struct sockaddr_in *)from, (char *)rip, size);
     69 	if (from->sa_family >= af_max ||
     70 	    (afp = &afswitch[from->sa_family])->af_hash == (int (*)())0) {
     71 		syslog(LOG_INFO,
     72 	 "\"from\" address in unsupported address family (%d), cmd %d\n",
     73 		    from->sa_family, rip->rip_cmd);
     74 		return;
     75 	}
     76 	if (rip->rip_vers == 0) {
     77 		syslog(LOG_ERR,
     78 		    "RIP version 0 packet received from %s! (cmd %d)",
     79 		    (*afswitch[from->sa_family].af_format)(from), rip->rip_cmd);
     80 		return;
     81 	}
     82 	switch (rip->rip_cmd) {
     83 
     84 	case RIPCMD_REQUEST:
     85 		n = rip->rip_nets;
     86 		count = size - ((char *)n - (char *)rip);
     87 		if (count < sizeof (struct netinfo))
     88 			return;
     89 		for (; count > 0; n++) {
     90 			if (count < sizeof (struct netinfo))
     91 				break;
     92 			count -= sizeof (struct netinfo);
     93 
     94 #if BSD < 198810
     95 			if (sizeof(n->rip_dst.sa_family) > 1)	/* XXX */
     96 			    n->rip_dst.sa_family = ntohs(n->rip_dst.sa_family);
     97 #else
     98 #define osa(x) ((struct osockaddr *)(&(x)))
     99 			    n->rip_dst.sa_family =
    100 					ntohs(osa(n->rip_dst)->sa_family);
    101 			    n->rip_dst.sa_len = sizeof(n->rip_dst);
    102 #endif
    103 			n->rip_metric = ntohl(n->rip_metric);
    104 			/*
    105 			 * A single entry with sa_family == AF_UNSPEC and
    106 			 * metric ``infinity'' means ``all routes''.
    107 			 * We respond to routers only if we are acting
    108 			 * as a supplier, or to anyone other than a router
    109 			 * (eg, query).
    110 			 */
    111 			if (n->rip_dst.sa_family == AF_UNSPEC &&
    112 			    n->rip_metric == HOPCNT_INFINITY && count == 0) {
    113 			    	if (supplier || (*afp->af_portmatch)(from) == 0)
    114 					supply(from, 0, 0, 0);
    115 				return;
    116 			}
    117 			if (n->rip_dst.sa_family < af_max &&
    118 			    afswitch[n->rip_dst.sa_family].af_hash)
    119 				rt = rtlookup(&n->rip_dst);
    120 			else
    121 				rt = 0;
    122 #define min(a, b) (a < b ? a : b)
    123 			n->rip_metric = rt == 0 ? HOPCNT_INFINITY :
    124 				min(rt->rt_metric + 1, HOPCNT_INFINITY);
    125 #if BSD < 198810
    126 			if (sizeof(n->rip_dst.sa_family) > 1)	/* XXX */
    127 			    n->rip_dst.sa_family = htons(n->rip_dst.sa_family);
    128 #else
    129 			    osa(n->rip_dst)->sa_family =
    130 						htons(n->rip_dst.sa_family);
    131 #endif
    132 			n->rip_metric = htonl(n->rip_metric);
    133 		}
    134 		rip->rip_cmd = RIPCMD_RESPONSE;
    135 		memcpy(packet, rip, size);
    136 		(*afp->af_output)(s, 0, from, size);
    137 		return;
    138 
    139 	case RIPCMD_TRACEON:
    140 	case RIPCMD_TRACEOFF:
    141 		/* verify message came from a privileged port */
    142 		if ((*afp->af_portcheck)(from) == 0)
    143 			return;
    144 		if ((ifp = if_iflookup(from)) == 0 || (ifp->int_flags &
    145 		    (IFF_BROADCAST | IFF_POINTOPOINT | IFF_REMOTE)) == 0 ||
    146 		    ifp->int_flags & IFF_PASSIVE) {
    147 			syslog(LOG_ERR, "trace command from unknown router, %s",
    148 			    (*afswitch[from->sa_family].af_format)(from));
    149 			return;
    150 		}
    151 		((char *)rip)[size] = '\0';
    152 		if (rip->rip_cmd == RIPCMD_TRACEON)
    153 			traceon(rip->rip_tracefile);
    154 		else
    155 			traceoff();
    156 		return;
    157 
    158 	case RIPCMD_RESPONSE:
    159 		/* verify message came from a router */
    160 		if ((*afp->af_portmatch)(from) == 0)
    161 			return;
    162 		(*afp->af_canon)(from);
    163 		/* are we talking to ourselves? */
    164 		ifp = if_ifwithaddr(from);
    165 		if (ifp) {
    166 			if (ifp->int_flags & IFF_PASSIVE) {
    167 				syslog(LOG_ERR,
    168 				  "bogus input (from passive interface, %s)",
    169 				  (*afswitch[from->sa_family].af_format)(from));
    170 				return;
    171 			}
    172 			rt = rtfind(from);
    173 			if (rt == 0 || ((rt->rt_state & RTS_INTERFACE) == 0) &&
    174 			    rt->rt_metric >= ifp->int_metric)
    175 				addrouteforif(ifp);
    176 			else
    177 				rt->rt_timer = 0;
    178 			return;
    179 		}
    180 		/*
    181 		 * Update timer for interface on which the packet arrived.
    182 		 * If from other end of a point-to-point link that isn't
    183 		 * in the routing tables, (re-)add the route.
    184 		 */
    185 		if ((rt = rtfind(from)) &&
    186 		    (rt->rt_state & (RTS_INTERFACE | RTS_REMOTE)))
    187 			rt->rt_timer = 0;
    188 		else if ((ifp = if_ifwithdstaddr(from)) &&
    189 		    (rt == 0 || rt->rt_metric >= ifp->int_metric))
    190 			addrouteforif(ifp);
    191 		/*
    192 		 * "Authenticate" router from which message originated.
    193 		 * We accept routing packets from routers directly connected
    194 		 * via broadcast or point-to-point networks,
    195 		 * and from those listed in /etc/gateways.
    196 		 */
    197 		if ((ifp = if_iflookup(from)) == 0 || (ifp->int_flags &
    198 		    (IFF_BROADCAST | IFF_POINTOPOINT | IFF_REMOTE)) == 0 ||
    199 		    ifp->int_flags & IFF_PASSIVE) {
    200 			if (memcmp(from, &badfrom, sizeof(badfrom)) != 0) {
    201 				syslog(LOG_ERR,
    202 				  "packet from unknown router, %s",
    203 				  (*afswitch[from->sa_family].af_format)(from));
    204 				badfrom = *from;
    205 			}
    206 			return;
    207 		}
    208 		size -= 4 * sizeof (char);
    209 		n = rip->rip_nets;
    210 		for (; size > 0; size -= sizeof (struct netinfo), n++) {
    211 			if (size < sizeof (struct netinfo))
    212 				break;
    213 #if BSD < 198810
    214 			if (sizeof(n->rip_dst.sa_family) > 1)	/* XXX */
    215 				n->rip_dst.sa_family =
    216 					ntohs(n->rip_dst.sa_family);
    217 #else
    218 			    n->rip_dst.sa_family =
    219 					ntohs(osa(n->rip_dst)->sa_family);
    220 			    n->rip_dst.sa_len = sizeof(n->rip_dst);
    221 #endif
    222 			n->rip_metric = ntohl(n->rip_metric);
    223 			if (n->rip_dst.sa_family >= af_max ||
    224 			    (afp = &afswitch[n->rip_dst.sa_family])->af_hash ==
    225 			    (int (*)())0) {
    226 				syslog(LOG_INFO,
    227 		"route in unsupported address family (%d), from %s (af %d)\n",
    228 				   n->rip_dst.sa_family,
    229 				   (*afswitch[from->sa_family].af_format)(from),
    230 				   from->sa_family);
    231 				continue;
    232 			}
    233 			if (((*afp->af_checkhost)(&n->rip_dst)) == 0) {
    234 				syslog(LOG_DEBUG,
    235 				    "bad host in route from %s (af %d)\n",
    236 				   (*afswitch[from->sa_family].af_format)(from),
    237 				   from->sa_family);
    238 				continue;
    239 			}
    240 			if (n->rip_metric == 0 ||
    241 			    (unsigned) n->rip_metric > HOPCNT_INFINITY) {
    242 				if (memcmp(from, &badfrom2,
    243 				    sizeof(badfrom2)) != 0) {
    244 					syslog(LOG_ERR,
    245 					    "bad metric (%d) from %s\n",
    246 					    n->rip_metric,
    247 				  (*afswitch[from->sa_family].af_format)(from));
    248 					badfrom2 = *from;
    249 				}
    250 				continue;
    251 			}
    252 			/*
    253 			 * Adjust metric according to incoming interface.
    254 			 */
    255 			if ((unsigned) n->rip_metric < HOPCNT_INFINITY)
    256 				n->rip_metric += ifp->int_metric;
    257 			if ((unsigned) n->rip_metric > HOPCNT_INFINITY)
    258 				n->rip_metric = HOPCNT_INFINITY;
    259 			rt = rtlookup(&n->rip_dst);
    260 			if (rt == 0 ||
    261 			    (rt->rt_state & (RTS_INTERNAL|RTS_INTERFACE)) ==
    262 			    (RTS_INTERNAL|RTS_INTERFACE)) {
    263 				/*
    264 				 * If we're hearing a logical network route
    265 				 * back from a peer to which we sent it,
    266 				 * ignore it.
    267 				 */
    268 				if (rt && rt->rt_state & RTS_SUBNET &&
    269 				    (*afp->af_sendroute)(rt, from))
    270 					continue;
    271 				if ((unsigned)n->rip_metric < HOPCNT_INFINITY) {
    272 				    /*
    273 				     * Look for an equivalent route that
    274 				     * includes this one before adding
    275 				     * this route.
    276 				     */
    277 				    rt = rtfind(&n->rip_dst);
    278 				    if (rt && equal(from, &rt->rt_router))
    279 					    continue;
    280 				    rtadd(&n->rip_dst, from, n->rip_metric, 0);
    281 				    changes++;
    282 				}
    283 				continue;
    284 			}
    285 
    286 			/*
    287 			 * Update if from gateway and different,
    288 			 * shorter, or equivalent but old route
    289 			 * is getting stale.
    290 			 */
    291 			if (equal(from, &rt->rt_router)) {
    292 				if (n->rip_metric != rt->rt_metric) {
    293 					rtchange(rt, from, n->rip_metric);
    294 					changes++;
    295 					rt->rt_timer = 0;
    296 					if (rt->rt_metric >= HOPCNT_INFINITY)
    297 						rt->rt_timer =
    298 						    GARBAGE_TIME - EXPIRE_TIME;
    299 				} else if (rt->rt_metric < HOPCNT_INFINITY)
    300 					rt->rt_timer = 0;
    301 			} else if ((unsigned) n->rip_metric < rt->rt_metric ||
    302 			    (rt->rt_metric == n->rip_metric &&
    303 			    rt->rt_timer > (EXPIRE_TIME/2) &&
    304 			    (unsigned) n->rip_metric < HOPCNT_INFINITY)) {
    305 				rtchange(rt, from, n->rip_metric);
    306 				changes++;
    307 				rt->rt_timer = 0;
    308 			}
    309 		}
    310 		break;
    311 	}
    312 
    313 	/*
    314 	 * If changes have occurred, and if we have not sent a broadcast
    315 	 * recently, send a dynamic update.  This update is sent only
    316 	 * on interfaces other than the one on which we received notice
    317 	 * of the change.  If we are within MIN_WAITTIME of a full update,
    318 	 * don't bother sending; if we just sent a dynamic update
    319 	 * and set a timer (nextbcast), delay until that time.
    320 	 * If we just sent a full update, delay the dynamic update.
    321 	 * Set a timer for a randomized value to suppress additional
    322 	 * dynamic updates until it expires; if we delayed sending
    323 	 * the current changes, set needupdate.
    324 	 */
    325 	if (changes && supplier &&
    326 	   now.tv_sec - lastfullupdate.tv_sec < SUPPLY_INTERVAL-MAX_WAITTIME) {
    327 		u_long delay;
    328 		extern long random();
    329 
    330 		if (now.tv_sec - lastbcast.tv_sec >= MIN_WAITTIME &&
    331 		    timercmp(&nextbcast, &now, <)) {
    332 			if (traceactions)
    333 				fprintf(ftrace, "send dynamic update\n");
    334 			toall(supply, RTS_CHANGED, ifp);
    335 			lastbcast = now;
    336 			needupdate = 0;
    337 			nextbcast.tv_sec = 0;
    338 		} else {
    339 			needupdate++;
    340 			if (traceactions)
    341 				fprintf(ftrace, "delay dynamic update\n");
    342 		}
    343 #define RANDOMDELAY()	(MIN_WAITTIME * 1000000 + \
    344 		(u_long)random() % ((MAX_WAITTIME - MIN_WAITTIME) * 1000000))
    345 
    346 		if (nextbcast.tv_sec == 0) {
    347 			delay = RANDOMDELAY();
    348 			if (traceactions)
    349 				fprintf(ftrace,
    350 				    "inhibit dynamic update for %d usec\n",
    351 				    delay);
    352 			nextbcast.tv_sec = delay / 1000000;
    353 			nextbcast.tv_usec = delay % 1000000;
    354 			timevaladd(&nextbcast, &now);
    355 			/*
    356 			 * If the next possibly dynamic update
    357 			 * is within MIN_WAITTIME of the next full update,
    358 			 * force the delay past the full update,
    359 			 * or we might send a dynamic update just before
    360 			 * the full update.
    361 			 */
    362 			if (nextbcast.tv_sec > lastfullupdate.tv_sec +
    363 			    SUPPLY_INTERVAL - MIN_WAITTIME)
    364 				nextbcast.tv_sec = lastfullupdate.tv_sec +
    365 				    SUPPLY_INTERVAL + 1;
    366 		}
    367 	}
    368 }
    369