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