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