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