Home | History | Annotate | Line # | Download | only in routed
input.c revision 1.23
      1 /*	$NetBSD: input.c,v 1.23 1998/10/24 18:30:23 christos 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 #if !defined(lint) && !defined(sgi) && !defined(__NetBSD__)
     37 static char sccsid[] = "@(#)input.c	8.1 (Berkeley) 6/5/93";
     38 #elif defined(__NetBSD__)
     39 #include <sys/cdefs.h>
     40 __RCSID("$NetBSD: input.c,v 1.23 1998/10/24 18:30:23 christos Exp $");
     41 #endif
     42 
     43 #include "defs.h"
     44 
     45 static void input(struct sockaddr_in *, struct interface *, struct interface *,
     46 		  struct rip *, int);
     47 static void input_route(naddr, naddr, struct rt_spare *, struct netinfo *);
     48 static int ck_passwd(struct interface *, struct rip *, void *,
     49 		     naddr, struct msg_limit *);
     50 
     51 
     52 /* process RIP input
     53  */
     54 void
     55 read_rip(int sock,
     56 	 struct interface *sifp)
     57 {
     58 	struct sockaddr_in from;
     59 	struct interface *aifp;
     60 	int fromlen, cc;
     61 #ifdef USE_PASSIFNAME
     62 	static struct msg_limit  bad_name;
     63 	struct {
     64 		char	ifname[IFNAMSIZ];
     65 		union pkt_buf pbuf;
     66 	} inbuf;
     67 #else
     68 	struct {
     69 		union pkt_buf pbuf;
     70 	} inbuf;
     71 #endif
     72 
     73 
     74 	for (;;) {
     75 		fromlen = sizeof(from);
     76 		cc = recvfrom(sock, &inbuf, sizeof(inbuf), 0,
     77 			      (struct sockaddr*)&from, &fromlen);
     78 		if (cc <= 0) {
     79 			if (cc < 0 && errno != EWOULDBLOCK)
     80 				LOGERR("recvfrom(rip)");
     81 			break;
     82 		}
     83 		if (fromlen != sizeof(struct sockaddr_in))
     84 			logbad(1,"impossible recvfrom(rip) fromlen=%d",
     85 			       fromlen);
     86 
     87 		/* aifp is the "authenticated" interface via which the packet
     88 		 *	arrived.  In fact, it is only the interface on which
     89 		 *	the packet should have arrived based on is source
     90 		 *	address.
     91 		 * sifp is interface associated with the socket through which
     92 		 *	the packet was received.
     93 		 */
     94 #ifdef USE_PASSIFNAME
     95 		if ((cc -= sizeof(inbuf.ifname)) < 0)
     96 			logbad(0,"missing USE_PASSIFNAME; only %d bytes",
     97 			       cc+sizeof(inbuf.ifname));
     98 
     99 		/* check the remote interfaces first */
    100 		for (aifp = remote_if; aifp; aifp = aifp->int_rlink) {
    101 			if (aifp->int_addr == from.sin_addr.s_addr)
    102 				break;
    103 		}
    104 		if (aifp == 0) {
    105 			aifp = ifwithname(inbuf.ifname, 0);
    106 			if (aifp == 0) {
    107 				msglim(&bad_name, from.sin_addr.s_addr,
    108 				       "impossible interface name %.*s",
    109 				       IFNAMSIZ, inbuf.ifname);
    110 			} else if (((aifp->int_if_flags & IFF_POINTOPOINT)
    111 				    && aifp->int_dstaddr!=from.sin_addr.s_addr)
    112 				   || (!(aifp->int_if_flags & IFF_POINTOPOINT)
    113 				       && !on_net(from.sin_addr.s_addr,
    114 						  aifp->int_net,
    115 						  aifp->int_mask))) {
    116 				/* If it came via the wrong interface, do not
    117 				 * trust it.
    118 				 */
    119 				aifp = 0;
    120 			}
    121 		}
    122 #else
    123 		aifp = iflookup(from.sin_addr.s_addr);
    124 #endif
    125 		if (sifp == 0)
    126 			sifp = aifp;
    127 
    128 		input(&from, sifp, aifp, &inbuf.pbuf.rip, cc);
    129 	}
    130 }
    131 
    132 
    133 /* Process a RIP packet
    134  */
    135 static void
    136 input(struct sockaddr_in *from,		/* received from this IP address */
    137       struct interface *sifp,		/* interface of incoming socket */
    138       struct interface *aifp,		/* "authenticated" interface */
    139       struct rip *rip,
    140       int cc)
    141 {
    142 #	define FROM_NADDR from->sin_addr.s_addr
    143 	static struct msg_limit use_auth, bad_len, bad_mask;
    144 	static struct msg_limit unk_router, bad_router, bad_nhop;
    145 
    146 	struct rt_entry *rt;
    147 	struct rt_spare new;
    148 	struct netinfo *n, *lim;
    149 	struct interface *ifp1;
    150 	naddr gate, mask, v1_mask, dst, ddst_h = 0;
    151 	struct auth *ap;
    152 	struct tgate *tg = 0;
    153 	struct tgate_net *tn;
    154 	int i, j;
    155 
    156 	/* Notice when we hear from a remote gateway
    157 	 */
    158 	if (aifp != 0
    159 	    && (aifp->int_state & IS_REMOTE))
    160 		aifp->int_act_time = now.tv_sec;
    161 
    162 	trace_rip("Recv", "from", from, sifp, rip, cc);
    163 
    164 	if (rip->rip_vers == 0) {
    165 		msglim(&bad_router, FROM_NADDR,
    166 		       "RIP version 0, cmd %d, packet received from %s",
    167 		       rip->rip_cmd, naddr_ntoa(FROM_NADDR));
    168 		return;
    169 	} else if (rip->rip_vers > RIPv2) {
    170 		rip->rip_vers = RIPv2;
    171 	}
    172 	if (cc > OVER_MAXPACKETSIZE) {
    173 		msglim(&bad_router, FROM_NADDR,
    174 		       "packet at least %d bytes too long received from %s",
    175 		       cc-MAXPACKETSIZE, naddr_ntoa(FROM_NADDR));
    176 		return;
    177 	}
    178 
    179 	n = rip->rip_nets;
    180 	lim = (struct netinfo *)((char*)rip + cc);
    181 
    182 	/* Notice authentication.
    183 	 * As required by section 4.2 in RFC 1723, discard authenticated
    184 	 * RIPv2 messages, but only if configured for that silliness.
    185 	 *
    186 	 * RIPv2 authentication is lame.  Why authenticate queries?
    187 	 * Why should a RIPv2 implementation with authentication disabled
    188 	 * not be able to listen to RIPv2 packets with authenication, while
    189 	 * RIPv1 systems will listen?  Crazy!
    190 	 */
    191 	if (!auth_ok
    192 	    && rip->rip_vers == RIPv2
    193 	    && n < lim && n->n_family == RIP_AF_AUTH) {
    194 		msglim(&use_auth, FROM_NADDR,
    195 		       "RIPv2 message with authentication from %s discarded",
    196 		       naddr_ntoa(FROM_NADDR));
    197 		return;
    198 	}
    199 
    200 	switch (rip->rip_cmd) {
    201 	case RIPCMD_REQUEST:
    202 		/* For mere requests, be a little sloppy about the source
    203 		 */
    204 		if (aifp == 0)
    205 			aifp = sifp;
    206 
    207 		/* Are we talking to ourself or a remote gateway?
    208 		 */
    209 		ifp1 = ifwithaddr(FROM_NADDR, 0, 1);
    210 		if (ifp1) {
    211 			if (ifp1->int_state & IS_REMOTE) {
    212 				/* remote gateway */
    213 				aifp = ifp1;
    214 				if (check_remote(aifp)) {
    215 					aifp->int_act_time = now.tv_sec;
    216 					(void)if_ok(aifp, "remote ");
    217 				}
    218 			} else if (from->sin_port == htons(RIP_PORT)) {
    219 				trace_pkt("    discard our own RIP request");
    220 				return;
    221 			}
    222 		}
    223 
    224 		/* did the request come from a router?
    225 		 */
    226 		if (from->sin_port == htons(RIP_PORT)) {
    227 			/* yes, ignore the request if RIP is off so that
    228 			 * the router does not depend on us.
    229 			 */
    230 			if (rip_sock < 0
    231 			    || (aifp != 0
    232 				&& IS_RIP_OUT_OFF(aifp->int_state))) {
    233 				trace_pkt("    discard request while RIP off");
    234 				return;
    235 			}
    236 		}
    237 
    238 		/* According to RFC 1723, we should ignore unathenticated
    239 		 * queries.  That is too silly to bother with.  Sheesh!
    240 		 * Are forwarding tables supposed to be secret, when
    241 		 * a bad guy can infer them with test traffic?  When RIP
    242 		 * is still the most common router-discovery protocol
    243 		 * and so hosts need to send queries that will be answered?
    244 		 * What about `rtquery`?
    245 		 * Maybe on firewalls you'd care, but not enough to
    246 		 * give up the diagnostic facilities of remote probing.
    247 		 */
    248 
    249 		if (n >= lim) {
    250 			msglim(&bad_len, FROM_NADDR, "empty request from %s",
    251 			       naddr_ntoa(FROM_NADDR));
    252 			return;
    253 		}
    254 		if (cc%sizeof(*n) != sizeof(struct rip)%sizeof(*n)) {
    255 			msglim(&bad_len, FROM_NADDR,
    256 			       "request of bad length (%d) from %s",
    257 			       cc, naddr_ntoa(FROM_NADDR));
    258 		}
    259 
    260 		if (rip->rip_vers == RIPv2
    261 		    && (aifp == 0 || (aifp->int_state & IS_NO_RIPV1_OUT))) {
    262 			v12buf.buf->rip_vers = RIPv2;
    263 			/* If we have a secret but it is a cleartext secret,
    264 			 * do not disclose our secret unless the other guy
    265 			 * already knows it.
    266 			 */
    267 			ap = find_auth(aifp);
    268 			if (ap != 0 && ap->type == RIP_AUTH_PW
    269 			    && n->n_family == RIP_AF_AUTH
    270 			    && !ck_passwd(aifp,rip,lim,FROM_NADDR,&use_auth))
    271 				ap = 0;
    272 		} else {
    273 			v12buf.buf->rip_vers = RIPv1;
    274 			ap = 0;
    275 		}
    276 		clr_ws_buf(&v12buf, ap);
    277 
    278 		do {
    279 			NTOHL(n->n_metric);
    280 
    281 			/* A single entry with family RIP_AF_UNSPEC and
    282 			 * metric HOPCNT_INFINITY means "all routes".
    283 			 * We respond to routers only if we are acting
    284 			 * as a supplier, or to anyone other than a router
    285 			 * (i.e. a query).
    286 			 */
    287 			if (n->n_family == RIP_AF_UNSPEC
    288 			    && n->n_metric == HOPCNT_INFINITY) {
    289 				/* Answer a query from a utility program
    290 				 * with all we know.
    291 				 */
    292 				if (from->sin_port != htons(RIP_PORT)) {
    293 					supply(from, aifp, OUT_QUERY, 0,
    294 					       rip->rip_vers, ap != 0);
    295 					return;
    296 				}
    297 
    298 				/* A router trying to prime its tables.
    299 				 * Filter the answer in the about same way
    300 				 * broadcasts are filtered.
    301 				 *
    302 				 * Only answer a router if we are a supplier
    303 				 * to keep an unwary host that is just starting
    304 				 * from picking us as a router.
    305 				 */
    306 				if (aifp == 0) {
    307 					trace_pkt("ignore distant router");
    308 					return;
    309 				}
    310 				if (!supplier
    311 				    || IS_RIP_OFF(aifp->int_state)) {
    312 					trace_pkt("ignore; not supplying");
    313 					return;
    314 				}
    315 
    316 				/* Do not answer a RIPv1 router if
    317 				 * we are sending RIPv2.  But do offer
    318 				 * poor man's router discovery.
    319 				 */
    320 				if ((aifp->int_state & IS_NO_RIPV1_OUT)
    321 				    && rip->rip_vers == RIPv1) {
    322 					if (!(aifp->int_state & IS_PM_RDISC)) {
    323 					    trace_pkt("ignore; sending RIPv2");
    324 					    return;
    325 					}
    326 
    327 					v12buf.n->n_family = RIP_AF_INET;
    328 					v12buf.n->n_dst = RIP_DEFAULT;
    329 					i = aifp->int_d_metric;
    330 					if (0 != (rt = rtget(RIP_DEFAULT, 0)))
    331 					    i = MIN(i, (rt->rt_metric
    332 							+aifp->int_metric+1));
    333 					v12buf.n->n_metric = htonl(i);
    334 					v12buf.n++;
    335 					break;
    336 				}
    337 
    338 				/* Respond with RIPv1 instead of RIPv2 if
    339 				 * that is what we are broadcasting on the
    340 				 * interface to keep the remote router from
    341 				 * getting the wrong initial idea of the
    342 				 * routes we send.
    343 				 */
    344 				supply(from, aifp, OUT_UNICAST, 0,
    345 				       (aifp->int_state & IS_NO_RIPV1_OUT)
    346 				       ? RIPv2 : RIPv1,
    347 				       ap != 0);
    348 				return;
    349 			}
    350 
    351 			/* Ignore authentication */
    352 			if (n->n_family == RIP_AF_AUTH)
    353 				continue;
    354 
    355 			if (n->n_family != RIP_AF_INET) {
    356 				msglim(&bad_router, FROM_NADDR,
    357 				       "request from %s for unsupported"
    358 				       " (af %d) %s",
    359 				       naddr_ntoa(FROM_NADDR),
    360 				       ntohs(n->n_family),
    361 				       naddr_ntoa(n->n_dst));
    362 				return;
    363 			}
    364 
    365 			/* We are being asked about a specific destination.
    366 			 */
    367 			dst = n->n_dst;
    368 			if (!check_dst(dst)) {
    369 				msglim(&bad_router, FROM_NADDR,
    370 				       "bad queried destination %s from %s",
    371 				       naddr_ntoa(dst),
    372 				       naddr_ntoa(FROM_NADDR));
    373 				return;
    374 			}
    375 
    376 			/* decide what mask was intended */
    377 			if (rip->rip_vers == RIPv1
    378 			    || 0 == (mask = ntohl(n->n_mask))
    379 			    || 0 != (ntohl(dst) & ~mask))
    380 				mask = ripv1_mask_host(dst, aifp);
    381 
    382 			/* try to find the answer */
    383 			rt = rtget(dst, mask);
    384 			if (!rt && dst != RIP_DEFAULT)
    385 				rt = rtfind(n->n_dst);
    386 
    387 			if (v12buf.buf->rip_vers != RIPv1)
    388 				v12buf.n->n_mask = mask;
    389 			if (rt == 0) {
    390 				/* we do not have the answer */
    391 				v12buf.n->n_metric = HOPCNT_INFINITY;
    392 			} else {
    393 				/* we have the answer, so compute the
    394 				 * right metric and next hop.
    395 				 */
    396 				v12buf.n->n_family = RIP_AF_INET;
    397 				v12buf.n->n_dst = dst;
    398 				v12buf.n->n_metric = (rt->rt_metric+1
    399 						      + ((aifp!=0)
    400 							  ? aifp->int_metric
    401 							  : 1));
    402 				if (v12buf.n->n_metric > HOPCNT_INFINITY)
    403 					v12buf.n->n_metric = HOPCNT_INFINITY;
    404 				if (v12buf.buf->rip_vers != RIPv1) {
    405 					v12buf.n->n_tag = rt->rt_tag;
    406 					v12buf.n->n_mask = mask;
    407 					if (aifp != 0
    408 					    && on_net(rt->rt_gate,
    409 						      aifp->int_net,
    410 						      aifp->int_mask)
    411 					    && rt->rt_gate != aifp->int_addr)
    412 					    v12buf.n->n_nhop = rt->rt_gate;
    413 				}
    414 			}
    415 			HTONL(v12buf.n->n_metric);
    416 
    417 			/* Stop paying attention if we fill the output buffer.
    418 			 */
    419 			if (++v12buf.n >= v12buf.lim)
    420 				break;
    421 		} while (++n < lim);
    422 
    423 		/* Send the answer about specific routes.
    424 		 */
    425 		if (ap != 0 && ap->type == RIP_AUTH_MD5)
    426 			end_md5_auth(&v12buf, ap);
    427 
    428 		if (from->sin_port != htons(RIP_PORT)) {
    429 			/* query */
    430 			(void)output(OUT_QUERY, from, aifp,
    431 				     v12buf.buf,
    432 				     ((char *)v12buf.n - (char*)v12buf.buf));
    433 		} else if (supplier) {
    434 			(void)output(OUT_UNICAST, from, aifp,
    435 				     v12buf.buf,
    436 				     ((char *)v12buf.n - (char*)v12buf.buf));
    437 		} else {
    438 			/* Only answer a router if we are a supplier
    439 			 * to keep an unwary host that is just starting
    440 			 * from picking us an a router.
    441 			 */
    442 			;
    443 		}
    444 		return;
    445 
    446 	case RIPCMD_TRACEON:
    447 	case RIPCMD_TRACEOFF:
    448 		/* verify message came from a privileged port */
    449 		if (ntohs(from->sin_port) > IPPORT_RESERVED) {
    450 			msglog("trace command from untrusted port on %s",
    451 			       naddr_ntoa(FROM_NADDR));
    452 			return;
    453 		}
    454 		if (aifp == 0) {
    455 			msglog("trace command from unknown router %s",
    456 			       naddr_ntoa(FROM_NADDR));
    457 			return;
    458 		}
    459 		if (rip->rip_cmd == RIPCMD_TRACEON) {
    460 			rip->rip_tracefile[cc-4] = '\0';
    461 #if 0
    462 			set_tracefile((char*)rip->rip_tracefile,
    463 				      "trace command: %s\n", 0);
    464 #else
    465 			msglog("RIP_TRACEON for `%s' from %s ignored",
    466 			    (char *) rip->rip_tracefile,
    467 			    naddr_ntoa(FROM_NADDR));
    468 #endif
    469 		} else {
    470 #if 0
    471 			trace_off("tracing turned off by %s",
    472 				  naddr_ntoa(FROM_NADDR));
    473 #else
    474 			msglog("RIP_TRACEOFF from %s ignored",
    475 			    naddr_ntoa(FROM_NADDR));
    476 #endif
    477 		}
    478 		return;
    479 
    480 	case RIPCMD_RESPONSE:
    481 		if (cc%sizeof(*n) != sizeof(struct rip)%sizeof(*n)) {
    482 			msglim(&bad_len, FROM_NADDR,
    483 			       "response of bad length (%d) from %s",
    484 			       cc, naddr_ntoa(FROM_NADDR));
    485 		}
    486 
    487 		/* verify message came from a router */
    488 		if (from->sin_port != ntohs(RIP_PORT)) {
    489 			msglim(&bad_router, FROM_NADDR,
    490 			       "    discard RIP response from unknown port"
    491 			       " %d", from->sin_port);
    492 			return;
    493 		}
    494 
    495 		if (rip_sock < 0) {
    496 			trace_pkt("    discard response while RIP off");
    497 			return;
    498 		}
    499 
    500 		/* Are we talking to ourself or a remote gateway?
    501 		 */
    502 		ifp1 = ifwithaddr(FROM_NADDR, 0, 1);
    503 		if (ifp1) {
    504 			if (ifp1->int_state & IS_REMOTE) {
    505 				/* remote gateway */
    506 				aifp = ifp1;
    507 				if (check_remote(aifp)) {
    508 					aifp->int_act_time = now.tv_sec;
    509 					(void)if_ok(aifp, "remote ");
    510 				}
    511 			} else {
    512 				trace_pkt("    discard our own RIP response");
    513 				return;
    514 			}
    515 		}
    516 
    517 		/* Accept routing packets from routers directly connected
    518 		 * via broadcast or point-to-point networks, and from
    519 		 * those listed in /etc/gateways.
    520 		 */
    521 		if (aifp == 0) {
    522 			msglim(&unk_router, FROM_NADDR,
    523 			       "   discard response from %s"
    524 			       " via unexpected interface",
    525 			       naddr_ntoa(FROM_NADDR));
    526 			return;
    527 		}
    528 		if (IS_RIP_IN_OFF(aifp->int_state)) {
    529 			trace_pkt("    discard RIPv%d response"
    530 				  " via disabled interface %s",
    531 				  rip->rip_vers, aifp->int_name);
    532 			return;
    533 		}
    534 
    535 		if (n >= lim) {
    536 			msglim(&bad_len, FROM_NADDR, "empty response from %s",
    537 			       naddr_ntoa(FROM_NADDR));
    538 			return;
    539 		}
    540 
    541 		if (((aifp->int_state & IS_NO_RIPV1_IN)
    542 		     && rip->rip_vers == RIPv1)
    543 		    || ((aifp->int_state & IS_NO_RIPV2_IN)
    544 			&& rip->rip_vers != RIPv1)) {
    545 			trace_pkt("    discard RIPv%d response",
    546 				  rip->rip_vers);
    547 			return;
    548 		}
    549 
    550 		/* Ignore routes via dead interface.
    551 		 */
    552 		if (aifp->int_state & IS_BROKE) {
    553 			trace_pkt("discard response via broken interface %s",
    554 				  aifp->int_name);
    555 			return;
    556 		}
    557 
    558 		/* If the interface cares, ignore bad routers.
    559 		 * Trace but do not log this problem, because where it
    560 		 * happens, it happens frequently.
    561 		 */
    562 		if (aifp->int_state & IS_DISTRUST) {
    563 			tg = tgates;
    564 			while (tg->tgate_addr != FROM_NADDR) {
    565 				tg = tg->tgate_next;
    566 				if (tg == 0) {
    567 					trace_pkt("    discard RIP response"
    568 						  " from untrusted router %s",
    569 						  naddr_ntoa(FROM_NADDR));
    570 					return;
    571 				}
    572 			}
    573 		}
    574 
    575 		/* Authenticate the packet if we have a secret.
    576 		 * If we do not have any secrets, ignore the error in
    577 		 * RFC 1723 and accept it regardless.
    578 		 */
    579 		if (aifp->int_auth[0].type != RIP_AUTH_NONE
    580 		    && rip->rip_vers != RIPv1
    581 		    && !ck_passwd(aifp,rip,lim,FROM_NADDR,&use_auth))
    582 			return;
    583 
    584 		do {
    585 			if (n->n_family == RIP_AF_AUTH)
    586 				continue;
    587 
    588 			NTOHL(n->n_metric);
    589 			dst = n->n_dst;
    590 			if (n->n_family != RIP_AF_INET
    591 			    && (n->n_family != RIP_AF_UNSPEC
    592 				|| dst != RIP_DEFAULT)) {
    593 				msglim(&bad_router, FROM_NADDR,
    594 				       "route from %s to unsupported"
    595 				       " address family=%d destination=%s",
    596 				       naddr_ntoa(FROM_NADDR),
    597 				       n->n_family,
    598 				       naddr_ntoa(dst));
    599 				continue;
    600 			}
    601 			if (!check_dst(dst)) {
    602 				msglim(&bad_router, FROM_NADDR,
    603 				       "bad destination %s from %s",
    604 				       naddr_ntoa(dst),
    605 				       naddr_ntoa(FROM_NADDR));
    606 				return;
    607 			}
    608 			if (n->n_metric == 0
    609 			    || n->n_metric > HOPCNT_INFINITY) {
    610 				msglim(&bad_router, FROM_NADDR,
    611 				       "bad metric %d from %s"
    612 				       " for destination %s",
    613 				       n->n_metric,
    614 				       naddr_ntoa(FROM_NADDR),
    615 				       naddr_ntoa(dst));
    616 				return;
    617 			}
    618 
    619 			/* Notice the next-hop.
    620 			 */
    621 			gate = FROM_NADDR;
    622 			if (n->n_nhop != 0) {
    623 				if (rip->rip_vers == RIPv2) {
    624 					n->n_nhop = 0;
    625 				} else {
    626 				    /* Use it only if it is valid. */
    627 				    if (on_net(n->n_nhop,
    628 					       aifp->int_net, aifp->int_mask)
    629 					&& check_dst(n->n_nhop)) {
    630 					    gate = n->n_nhop;
    631 				    } else {
    632 					    msglim(&bad_nhop, FROM_NADDR,
    633 						   "router %s to %s"
    634 						   " has bad next hop %s",
    635 						   naddr_ntoa(FROM_NADDR),
    636 						   naddr_ntoa(dst),
    637 						   naddr_ntoa(n->n_nhop));
    638 					    n->n_nhop = 0;
    639 				    }
    640 				}
    641 			}
    642 
    643 			if (rip->rip_vers == RIPv1
    644 			    || 0 == (mask = ntohl(n->n_mask))) {
    645 				mask = ripv1_mask_host(dst,aifp);
    646 			} else if ((ntohl(dst) & ~mask) != 0) {
    647 				msglim(&bad_mask, FROM_NADDR,
    648 				       "router %s sent bad netmask"
    649 				       " %#x with %s",
    650 				       naddr_ntoa(FROM_NADDR),
    651 				       mask,
    652 				       naddr_ntoa(dst));
    653 				continue;
    654 			}
    655 			if (rip->rip_vers == RIPv1)
    656 				n->n_tag = 0;
    657 
    658 			/* Adjust metric according to incoming interface..
    659 			 */
    660 			n->n_metric += aifp->int_metric;
    661 			if (n->n_metric > HOPCNT_INFINITY)
    662 				n->n_metric = HOPCNT_INFINITY;
    663 
    664 			/* Should we trust this route from this router? */
    665 			if (tg && (tn = tg->tgate_nets)->mask != 0) {
    666 				for (i = 0; i < MAX_TGATE_NETS; i++, tn++) {
    667 					if (on_net(dst, tn->net, tn->mask)
    668 					    && tn->mask <= mask)
    669 					    break;
    670 				}
    671 				if (i >= MAX_TGATE_NETS || tn->mask == 0) {
    672 					trace_pkt("   ignored unauthorized %s",
    673 						  addrname(dst,mask,0));
    674 					continue;
    675 				}
    676 			}
    677 
    678 			/* Recognize and ignore a default route we faked
    679 			 * which is being sent back to us by a machine with
    680 			 * broken split-horizon.
    681 			 * Be a little more paranoid than that, and reject
    682 			 * default routes with the same metric we advertised.
    683 			 */
    684 			if (aifp->int_d_metric != 0
    685 			    && dst == RIP_DEFAULT
    686 			    && n->n_metric >= aifp->int_d_metric)
    687 				continue;
    688 
    689 			/* We can receive aggregated RIPv2 routes that must
    690 			 * be broken down before they are transmitted by
    691 			 * RIPv1 via an interface on a subnet.
    692 			 * We might also receive the same routes aggregated
    693 			 * via other RIPv2 interfaces.
    694 			 * This could cause duplicate routes to be sent on
    695 			 * the RIPv1 interfaces.  "Longest matching variable
    696 			 * length netmasks" lets RIPv2 listeners understand,
    697 			 * but breaking down the aggregated routes for RIPv1
    698 			 * listeners can produce duplicate routes.
    699 			 *
    700 			 * Breaking down aggregated routes here bloats
    701 			 * the daemon table, but does not hurt the kernel
    702 			 * table, since routes are always aggregated for
    703 			 * the kernel.
    704 			 *
    705 			 * Notice that this does not break down network
    706 			 * routes corresponding to subnets.  This is part
    707 			 * of the defense against RS_NET_SYN.
    708 			 */
    709 			if (have_ripv1_out
    710 			    && (((rt = rtget(dst,mask)) == 0
    711 				 || !(rt->rt_state & RS_NET_SYN)))
    712 			    && (v1_mask = ripv1_mask_net(dst,0)) > mask) {
    713 				ddst_h = v1_mask & -v1_mask;
    714 				i = (v1_mask & ~mask)/ddst_h;
    715 				if (i >= 511) {
    716 					/* Punt if we would have to generate
    717 					 * an unreasonable number of routes.
    718 					 */
    719 					if (TRACECONTENTS)
    720 					    trace_misc("accept %s-->%s as 1"
    721 						       " instead of %d routes",
    722 						       addrname(dst,mask,0),
    723 						       naddr_ntoa(FROM_NADDR),
    724 						       i+1);
    725 					i = 0;
    726 				} else {
    727 					mask = v1_mask;
    728 				}
    729 			} else {
    730 				i = 0;
    731 			}
    732 
    733 			new.rts_gate = gate;
    734 			new.rts_router = FROM_NADDR;
    735 			new.rts_metric = n->n_metric;
    736 			new.rts_tag = n->n_tag;
    737 			new.rts_time = now.tv_sec;
    738 			new.rts_ifp = aifp;
    739 			new.rts_de_ag = i;
    740 			j = 0;
    741 			for (;;) {
    742 				input_route(dst, mask, &new, n);
    743 				if (++j > i)
    744 					break;
    745 				dst = htonl(ntohl(dst) + ddst_h);
    746 			}
    747 		} while (++n < lim);
    748 		break;
    749 	}
    750 #undef FROM_NADDR
    751 }
    752 
    753 
    754 /* Process a single input route.
    755  */
    756 static void
    757 input_route(naddr dst,			/* network order */
    758 	    naddr mask,
    759 	    struct rt_spare *new,
    760 	    struct netinfo *n)
    761 {
    762 	int i;
    763 	struct rt_entry *rt;
    764 	struct rt_spare *rts, *rts0;
    765 	struct interface *ifp1;
    766 
    767 
    768 	/* See if the other guy is telling us to send our packets to him.
    769 	 * Sometimes network routes arrive over a point-to-point link for
    770 	 * the network containing the address(es) of the link.
    771 	 *
    772 	 * If our interface is broken, switch to using the other guy.
    773 	 */
    774 	ifp1 = ifwithaddr(dst, 1, 1);
    775 	if (ifp1 != 0
    776 	    && (!(ifp1->int_state & IS_BROKE)
    777 		|| (ifp1->int_state & IS_PASSIVE)))
    778 		return;
    779 
    780 	/* Look for the route in our table.
    781 	 */
    782 	rt = rtget(dst, mask);
    783 
    784 	/* Consider adding the route if we do not already have it.
    785 	 */
    786 	if (rt == 0) {
    787 		/* Ignore unknown routes being poisoned.
    788 		 */
    789 		if (new->rts_metric == HOPCNT_INFINITY)
    790 			return;
    791 
    792 		/* Ignore the route if it points to us */
    793 		if (n->n_nhop != 0
    794 		    && 0 != ifwithaddr(n->n_nhop, 1, 0))
    795 			return;
    796 
    797 		/* If something has not gone crazy and tried to fill
    798 		 * our memory, accept the new route.
    799 		 */
    800 		if (total_routes < MAX_ROUTES)
    801 			rtadd(dst, mask, 0, new);
    802 		return;
    803 	}
    804 
    805 	/* We already know about the route.  Consider this update.
    806 	 *
    807 	 * If (rt->rt_state & RS_NET_SYN), then this route
    808 	 * is the same as a network route we have inferred
    809 	 * for subnets we know, in order to tell RIPv1 routers
    810 	 * about the subnets.
    811 	 *
    812 	 * It is impossible to tell if the route is coming
    813 	 * from a distant RIPv2 router with the standard
    814 	 * netmask because that router knows about the entire
    815 	 * network, or if it is a round-about echo of a
    816 	 * synthetic, RIPv1 network route of our own.
    817 	 * The worst is that both kinds of routes might be
    818 	 * received, and the bad one might have the smaller
    819 	 * metric.  Partly solve this problem by never
    820 	 * aggregating into such a route.  Also keep it
    821 	 * around as long as the interface exists.
    822 	 */
    823 
    824 	rts0 = rt->rt_spares;
    825 	for (rts = rts0, i = NUM_SPARES; i != 0; i--, rts++) {
    826 		if (rts->rts_router == new->rts_router)
    827 			break;
    828 		/* Note the worst slot to reuse,
    829 		 * other than the current slot.
    830 		 */
    831 		if (rts0 == rt->rt_spares
    832 		    || BETTER_LINK(rt, rts0, rts))
    833 			rts0 = rts;
    834 	}
    835 	if (i != 0) {
    836 		/* Found a route from the router already in the table.
    837 		 */
    838 
    839 		/* If the new route is a route broken down from an
    840 		 * aggregated route, and if the previous route is either
    841 		 * not a broken down route or was broken down from a finer
    842 		 * netmask, and if the previous route is current,
    843 		 * then forget this one.
    844 		 */
    845 		if (new->rts_de_ag > rts->rts_de_ag
    846 		    && now_stale <= rts->rts_time)
    847 			return;
    848 
    849 		/* Keep poisoned routes around only long enough to pass
    850 		 * the poison on.  Use a new timestamp for good routes.
    851 		 */
    852 		if (rts->rts_metric == HOPCNT_INFINITY
    853 		    && new->rts_metric == HOPCNT_INFINITY)
    854 			new->rts_time = rts->rts_time;
    855 
    856 		/* If this is an update for the router we currently prefer,
    857 		 * then note it.
    858 		 */
    859 		if (i == NUM_SPARES) {
    860 			rtchange(rt, rt->rt_state, new, 0);
    861 			/* If the route got worse, check for something better.
    862 			 */
    863 			if (new->rts_metric > rts->rts_metric)
    864 				rtswitch(rt, 0);
    865 			return;
    866 		}
    867 
    868 		/* This is an update for a spare route.
    869 		 * Finished if the route is unchanged.
    870 		 */
    871 		if (rts->rts_gate == new->rts_gate
    872 		    && rts->rts_metric == new->rts_metric
    873 		    && rts->rts_tag == new->rts_tag) {
    874 			trace_upslot(rt, rts, new);
    875 			*rts = *new;
    876 			return;
    877 		}
    878 		/* Forget it if it has gone bad.
    879 		 */
    880 		if (new->rts_metric == HOPCNT_INFINITY) {
    881 			rts_delete(rt, rts);
    882 			return;
    883 		}
    884 
    885 	} else {
    886 		/* The update is for a route we know about,
    887 		 * but not from a familiar router.
    888 		 *
    889 		 * Ignore the route if it points to us.
    890 		 */
    891 		if (n->n_nhop != 0
    892 		    && 0 != ifwithaddr(n->n_nhop, 1, 0))
    893 			return;
    894 
    895 		/* the loop above set rts0=worst spare */
    896 		rts = rts0;
    897 
    898 		/* Save the route as a spare only if it has
    899 		 * a better metric than our worst spare.
    900 		 * This also ignores poisoned routes (those
    901 		 * received with metric HOPCNT_INFINITY).
    902 		 */
    903 		if (new->rts_metric >= rts->rts_metric)
    904 			return;
    905 	}
    906 
    907 	trace_upslot(rt, rts, new);
    908 	*rts = *new;
    909 
    910 	/* try to switch to a better route */
    911 	rtswitch(rt, rts);
    912 }
    913 
    914 
    915 static int				/* 0 if bad */
    916 ck_passwd(struct interface *aifp,
    917 	  struct rip *rip,
    918 	  void *lim,
    919 	  naddr from,
    920 	  struct msg_limit *use_authp)
    921 {
    922 #	define NA (rip->rip_auths)
    923 	struct netauth *na2;
    924 	struct auth *ap;
    925 	MD5_CTX md5_ctx;
    926 	u_char hash[RIP_AUTH_PW_LEN];
    927 	int i;
    928 
    929 
    930 	if ((void *)NA >= lim || NA->a_family != RIP_AF_AUTH) {
    931 		msglim(use_authp, from, "missing password from %s",
    932 		       naddr_ntoa(from));
    933 		return 0;
    934 	}
    935 
    936 	/* accept any current (+/- 24 hours) password
    937 	 */
    938 	for (ap = aifp->int_auth, i = 0; i < MAX_AUTH_KEYS; i++, ap++) {
    939 		if (ap->type != NA->a_type
    940 		    || (u_long)ap->start > (u_long)clk.tv_sec+DAY
    941 		    || (u_long)ap->end+DAY < (u_long)clk.tv_sec)
    942 			continue;
    943 
    944 		if (NA->a_type == RIP_AUTH_PW) {
    945 			if (!memcmp(ap->key, NA->au.au_pw, RIP_AUTH_PW_LEN))
    946 				return 1;
    947 
    948 		} else {
    949 			/* accept MD5 secret with the right key ID
    950 			 */
    951 			if (NA->au.a_md5.md5_keyid != ap->keyid)
    952 				continue;
    953 
    954 			na2 = (struct netauth *)((char *)(NA+1)
    955 						 + NA->au.a_md5.md5_pkt_len);
    956 			if (NA->au.a_md5.md5_pkt_len % sizeof(*NA) != 0
    957 			    || lim < (void *)(na2+1)) {
    958 				msglim(use_authp, from,
    959 				       "bad MD5 RIP-II pkt length %d from %s",
    960 				       NA->au.a_md5.md5_pkt_len,
    961 				       naddr_ntoa(from));
    962 				return 0;
    963 			}
    964 			MD5Init(&md5_ctx);
    965 			MD5Update(&md5_ctx, (u_char *)NA,
    966 				  (char *)na2->au.au_pw - (char *)NA);
    967 			MD5Update(&md5_ctx,
    968 				  (u_char *)ap->key, sizeof(ap->key));
    969 			MD5Final(hash, &md5_ctx);
    970 			if (na2->a_family != RIP_AF_AUTH
    971 			    || na2->a_type != 1
    972 			    || NA->au.a_md5.md5_auth_len != RIP_AUTH_PW_LEN
    973 			    || memcmp(hash, na2->au.au_pw, sizeof(hash)))
    974 				return 0;
    975 			return 1;
    976 		}
    977 	}
    978 
    979 	msglim(use_authp, from, "bad password from %s",
    980 	       naddr_ntoa(from));
    981 	return 0;
    982 #undef NA
    983 }
    984