xref: /dragonfly/sbin/routed/table.c (revision 0ca59c34)
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/table.c,v 1.9.2.2 2000/08/14 17:00:04 sheldonh Exp $
34  */
35 
36 #include "defs.h"
37 
38 #if !defined(sgi) && !defined(__NetBSD__)
39 static char sccsid[] __attribute__((unused)) = "@(#)tables.c	8.1 (Berkeley) 6/5/93";
40 #elif defined(__NetBSD__)
41 __RCSID("$NetBSD$");
42 #endif
43 
44 static struct rt_spare *rts_better(struct rt_entry *);
45 static struct rt_spare rts_empty = {0,0,0,HOPCNT_INFINITY,0,0,0};
46 static void  set_need_flash(void);
47 #ifdef _HAVE_SIN_LEN
48 static void masktrim(struct sockaddr_in *ap);
49 #else
50 static void masktrim(struct sockaddr_in_new *ap);
51 #endif
52 
53 
54 struct radix_node_head *rhead;		/* root of the radix tree */
55 
56 int	need_flash = 1;			/* flash update needed
57 					 * start =1 to suppress the 1st
58 					 */
59 
60 struct timeval age_timer;		/* next check of old routes */
61 struct timeval need_kern = {		/* need to update kernel table */
62 	EPOCH+MIN_WAITTIME-1, 0
63 };
64 
65 int	stopint;
66 
67 int	total_routes;
68 
69 /* zap any old routes through this gateway */
70 naddr	age_bad_gate;
71 
72 
73 /* It is desirable to "aggregate" routes, to combine differing routes of
74  * the same metric and next hop into a common route with a smaller netmask
75  * or to suppress redundant routes, routes that add no information to
76  * routes with smaller netmasks.
77  *
78  * A route is redundant if and only if any and all routes with smaller
79  * but matching netmasks and nets are the same.  Since routes are
80  * kept sorted in the radix tree, redundant routes always come second.
81  *
82  * There are two kinds of aggregations.  First, two routes of the same bit
83  * mask and differing only in the least significant bit of the network
84  * number can be combined into a single route with a coarser mask.
85  *
86  * Second, a route can be suppressed in favor of another route with a more
87  * coarse mask provided no incompatible routes with intermediate masks
88  * are present.  The second kind of aggregation involves suppressing routes.
89  * A route must not be suppressed if an incompatible route exists with
90  * an intermediate mask, since the suppressed route would be covered
91  * by the intermediate.
92  *
93  * This code relies on the radix tree walk encountering routes
94  * sorted first by address, with the smallest address first.
95  */
96 
97 struct ag_info ag_slots[NUM_AG_SLOTS], *ag_avail, *ag_corsest, *ag_finest;
98 
99 /* #define DEBUG_AG */
100 #ifdef DEBUG_AG
101 #define CHECK_AG() {int acnt = 0; struct ag_info *cag;		\
102 	for (cag = ag_avail; cag != NULL; cag = cag->ag_fine)	\
103 		acnt++;						\
104 	for (cag = ag_corsest; cag != NULL; cag = cag->ag_fine)	\
105 		acnt++;						\
106 	if (acnt != NUM_AG_SLOTS) {				\
107 		fflush(stderr);					\
108 		abort();					\
109 	}							\
110 }
111 #else
112 #define CHECK_AG()
113 #endif
114 
115 
116 /* Output the contents of an aggregation table slot.
117  *	This function must always be immediately followed with the deletion
118  *	of the target slot.
119  */
120 static void
121 ag_out(struct ag_info *ag,
122 	 void (*out)(struct ag_info *))
123 {
124 	struct ag_info *ag_cors;
125 	naddr bit;
126 
127 
128 	/* Forget it if this route should not be output for split-horizon. */
129 	if (ag->ag_state & AGS_SPLIT_HZ)
130 		return;
131 
132 	/* If we output both the even and odd twins, then the immediate parent,
133 	 * if it is present, is redundant, unless the parent manages to
134 	 * aggregate into something coarser.
135 	 * On successive calls, this code detects the even and odd twins,
136 	 * and marks the parent.
137 	 *
138 	 * Note that the order in which the radix tree code emits routes
139 	 * ensures that the twins are seen before the parent is emitted.
140 	 */
141 	ag_cors = ag->ag_cors;
142 	if (ag_cors != NULL
143 	    && ag_cors->ag_mask == ag->ag_mask<<1
144 	    && ag_cors->ag_dst_h == (ag->ag_dst_h & ag_cors->ag_mask)) {
145 		ag_cors->ag_state |= ((ag_cors->ag_dst_h == ag->ag_dst_h)
146 				      ? AGS_REDUN0
147 				      : AGS_REDUN1);
148 	}
149 
150 	/* Skip it if this route is itself redundant.
151 	 *
152 	 * It is ok to change the contents of the slot here, since it is
153 	 * always deleted next.
154 	 */
155 	if (ag->ag_state & AGS_REDUN0) {
156 		if (ag->ag_state & AGS_REDUN1)
157 			return;		/* quit if fully redundant */
158 		/* make it finer if it is half-redundant */
159 		bit = (-ag->ag_mask) >> 1;
160 		ag->ag_dst_h |= bit;
161 		ag->ag_mask |= bit;
162 
163 	} else if (ag->ag_state & AGS_REDUN1) {
164 		/* make it finer if it is half-redundant */
165 		bit = (-ag->ag_mask) >> 1;
166 		ag->ag_mask |= bit;
167 	}
168 	out(ag);
169 }
170 
171 
172 static void
173 ag_del(struct ag_info *ag)
174 {
175 	CHECK_AG();
176 
177 	if (ag->ag_cors == 0)
178 		ag_corsest = ag->ag_fine;
179 	else
180 		ag->ag_cors->ag_fine = ag->ag_fine;
181 
182 	if (ag->ag_fine == 0)
183 		ag_finest = ag->ag_cors;
184 	else
185 		ag->ag_fine->ag_cors = ag->ag_cors;
186 
187 	ag->ag_fine = ag_avail;
188 	ag_avail = ag;
189 
190 	CHECK_AG();
191 }
192 
193 
194 /* Flush routes waiting for aggregation.
195  *	This must not suppress a route unless it is known that among all
196  *	routes with coarser masks that match it, the one with the longest
197  *	mask is appropriate.  This is ensured by scanning the routes
198  *	in lexical order, and with the most restrictive mask first
199  *	among routes to the same destination.
200  */
201 void
202 ag_flush(naddr lim_dst_h,		/* flush routes to here */
203 	 naddr lim_mask,		/* matching this mask */
204 	 void (*out)(struct ag_info *))
205 {
206 	struct ag_info *ag, *ag_cors;
207 	naddr dst_h;
208 
209 
210 	for (ag = ag_finest;
211 	     ag != NULL && ag->ag_mask >= lim_mask;
212 	     ag = ag_cors) {
213 		ag_cors = ag->ag_cors;
214 
215 		/* work on only the specified routes */
216 		dst_h = ag->ag_dst_h;
217 		if ((dst_h & lim_mask) != lim_dst_h)
218 			continue;
219 
220 		if (!(ag->ag_state & AGS_SUPPRESS))
221 			ag_out(ag, out);
222 
223 		else for ( ; ; ag_cors = ag_cors->ag_cors) {
224 			/* Look for a route that can suppress the
225 			 * current route */
226 			if (ag_cors == NULL) {
227 				/* failed, so output it and look for
228 				 * another route to work on
229 				 */
230 				ag_out(ag, out);
231 				break;
232 			}
233 
234 			if ((dst_h & ag_cors->ag_mask) == ag_cors->ag_dst_h) {
235 				/* We found a route with a coarser mask that
236 				 * aggregates the current target.
237 				 *
238 				 * If it has a different next hop, it
239 				 * cannot replace the target, so output
240 				 * the target.
241 				 */
242 				if (ag->ag_gate != ag_cors->ag_gate
243 				    && !(ag->ag_state & AGS_FINE_GATE)
244 				    && !(ag_cors->ag_state & AGS_CORS_GATE)) {
245 					ag_out(ag, out);
246 					break;
247 				}
248 
249 				/* If the coarse route has a good enough
250 				 * metric, it suppresses the target.
251 				 * If the suppressed target was redundant,
252 				 * then mark the suppressor redundant.
253 				 */
254 				if (ag_cors->ag_pref <= ag->ag_pref) {
255 				    if (ag_cors->ag_seqno > ag->ag_seqno)
256 					ag_cors->ag_seqno = ag->ag_seqno;
257 				    if (AG_IS_REDUN(ag->ag_state)
258 					&& ag_cors->ag_mask==ag->ag_mask<<1) {
259 					if (ag_cors->ag_dst_h == dst_h)
260 					    ag_cors->ag_state |= AGS_REDUN0;
261 					else
262 					    ag_cors->ag_state |= AGS_REDUN1;
263 				    }
264 				    if (ag->ag_tag != ag_cors->ag_tag)
265 					    ag_cors->ag_tag = 0;
266 				    if (ag->ag_nhop != ag_cors->ag_nhop)
267 					    ag_cors->ag_nhop = 0;
268 				    break;
269 				}
270 			}
271 		}
272 
273 		/* That route has either been output or suppressed */
274 		ag_cors = ag->ag_cors;
275 		ag_del(ag);
276 	}
277 
278 	CHECK_AG();
279 }
280 
281 
282 /* Try to aggregate a route with previous routes.
283  */
284 void
285 ag_check(naddr	dst,
286 	 naddr	mask,
287 	 naddr	gate,
288 	 naddr	nhop,
289 	 char	metric,
290 	 char	pref,
291 	 u_int	seqnum,
292 	 u_short tag,
293 	 u_short state,
294 	 void (*out)(struct ag_info *))	/* output using this */
295 {
296 	struct ag_info *ag, *nag, *ag_cors;
297 	naddr xaddr;
298 	int x;
299 
300 	dst = ntohl(dst);
301 
302 	/* Punt non-contiguous subnet masks.
303 	 *
304 	 * (X & -X) contains a single bit if and only if X is a power of 2.
305 	 * (X + (X & -X)) == 0 if and only if X is a power of 2.
306 	 */
307 	if ((mask & -mask) + mask != 0) {
308 		struct ag_info nc_ag;
309 
310 		nc_ag.ag_dst_h = dst;
311 		nc_ag.ag_mask = mask;
312 		nc_ag.ag_gate = gate;
313 		nc_ag.ag_nhop = nhop;
314 		nc_ag.ag_metric = metric;
315 		nc_ag.ag_pref = pref;
316 		nc_ag.ag_tag = tag;
317 		nc_ag.ag_state = state;
318 		nc_ag.ag_seqno = seqnum;
319 		out(&nc_ag);
320 		return;
321 	}
322 
323 	/* Search for the right slot in the aggregation table.
324 	 */
325 	ag_cors = NULL;
326 	ag = ag_corsest;
327 	while (ag != NULL) {
328 		if (ag->ag_mask >= mask)
329 			break;
330 
331 		/* Suppress old routes (i.e. combine with compatible routes
332 		 * with coarser masks) as we look for the right slot in the
333 		 * aggregation table for the new route.
334 		 * A route to an address less than the current destination
335 		 * will not be affected by the current route or any route
336 		 * seen hereafter.  That means it is safe to suppress it.
337 		 * This check keeps poor routes (e.g. with large hop counts)
338 		 * from preventing suppression of finer routes.
339 		 */
340 		if (ag_cors != NULL
341 		    && ag->ag_dst_h < dst
342 		    && (ag->ag_state & AGS_SUPPRESS)
343 		    && ag_cors->ag_pref <= ag->ag_pref
344 		    && (ag->ag_dst_h & ag_cors->ag_mask) == ag_cors->ag_dst_h
345 		    && (ag_cors->ag_gate == ag->ag_gate
346 			|| (ag->ag_state & AGS_FINE_GATE)
347 			|| (ag_cors->ag_state & AGS_CORS_GATE))) {
348 			if (ag_cors->ag_seqno > ag->ag_seqno)
349 				ag_cors->ag_seqno = ag->ag_seqno;
350 			/*  If the suppressed target was redundant,
351 			 * then mark the suppressor redundant.
352 			 */
353 			if (AG_IS_REDUN(ag->ag_state)
354 			    && ag_cors->ag_mask == ag->ag_mask<<1) {
355 				if (ag_cors->ag_dst_h == dst)
356 					ag_cors->ag_state |= AGS_REDUN0;
357 				else
358 					ag_cors->ag_state |= AGS_REDUN1;
359 			}
360 			if (ag->ag_tag != ag_cors->ag_tag)
361 				ag_cors->ag_tag = 0;
362 			if (ag->ag_nhop != ag_cors->ag_nhop)
363 				ag_cors->ag_nhop = 0;
364 			ag_del(ag);
365 			CHECK_AG();
366 		} else {
367 			ag_cors = ag;
368 		}
369 		ag = ag_cors->ag_fine;
370 	}
371 
372 	/* If we find the even/odd twin of the new route, and if the
373 	 * masks and so forth are equal, we can aggregate them.
374 	 * We can probably promote one of the pair.
375 	 *
376 	 * Since the routes are encountered in lexical order,
377 	 * the new route must be odd.  However, the second or later
378 	 * times around this loop, it could be the even twin promoted
379 	 * from the even/odd pair of twins of the finer route.
380 	 */
381 	while (ag != NULL
382 	       && ag->ag_mask == mask
383 	       && ((ag->ag_dst_h ^ dst) & (mask<<1)) == 0) {
384 
385 		/* Here we know the target route and the route in the current
386 		 * slot have the same netmasks and differ by at most the
387 		 * last bit.  They are either for the same destination, or
388 		 * for an even/odd pair of destinations.
389 		 */
390 		if (ag->ag_dst_h == dst) {
391 			/* We have two routes to the same destination.
392 			 * Routes are encountered in lexical order, so a
393 			 * route is never promoted until the parent route is
394 			 * already present.  So we know that the new route is
395 			 * a promoted (or aggregated) pair and the route
396 			 * already in the slot is the explicit route.
397 			 *
398 			 * Prefer the best route if their metrics differ,
399 			 * or the aggregated one if not, following a sort
400 			 * of longest-match rule.
401 			 */
402 			if (pref <= ag->ag_pref) {
403 				ag->ag_gate = gate;
404 				ag->ag_nhop = nhop;
405 				ag->ag_tag = tag;
406 				ag->ag_metric = metric;
407 				ag->ag_pref = pref;
408 				x = ag->ag_state;
409 				ag->ag_state = state;
410 				state = x;
411 			}
412 
413 			/* The sequence number controls flash updating,
414 			 * and should be the smaller of the two.
415 			 */
416 			if (ag->ag_seqno > seqnum)
417 				ag->ag_seqno = seqnum;
418 
419 			/* Some bits are set if they are set on either route,
420 			 * except when the route is for an interface.
421 			 */
422 			if (!(ag->ag_state & AGS_IF))
423 				ag->ag_state |= (state & (AGS_AGGREGATE_EITHER
424 							| AGS_REDUN0
425 							| AGS_REDUN1));
426 			return;
427 		}
428 
429 		/* If one of the routes can be promoted and the other can
430 		 * be suppressed, it may be possible to combine them or
431 		 * worthwhile to promote one.
432 		 *
433 		 * Any route that can be promoted is always
434 		 * marked to be eligible to be suppressed.
435 		 */
436 		if (!((state & AGS_AGGREGATE)
437 		      && (ag->ag_state & AGS_SUPPRESS))
438 		    && !((ag->ag_state & AGS_AGGREGATE)
439 			 && (state & AGS_SUPPRESS)))
440 			break;
441 
442 		/* A pair of even/odd twin routes can be combined
443 		 * if either is redundant, or if they are via the
444 		 * same gateway and have the same metric.
445 		 */
446 		if (AG_IS_REDUN(ag->ag_state)
447 		    || AG_IS_REDUN(state)
448 		    || (ag->ag_gate == gate
449 			&& ag->ag_pref == pref
450 			&& (state & ag->ag_state & AGS_AGGREGATE) != 0)) {
451 
452 			/* We have both the even and odd pairs.
453 			 * Since the routes are encountered in order,
454 			 * the route in the slot must be the even twin.
455 			 *
456 			 * Combine and promote (aggregate) the pair of routes.
457 			 */
458 			if (seqnum > ag->ag_seqno)
459 				seqnum = ag->ag_seqno;
460 			if (!AG_IS_REDUN(state))
461 				state &= ~AGS_REDUN1;
462 			if (AG_IS_REDUN(ag->ag_state))
463 				state |= AGS_REDUN0;
464 			else
465 				state &= ~AGS_REDUN0;
466 			state |= (ag->ag_state & AGS_AGGREGATE_EITHER);
467 			if (ag->ag_tag != tag)
468 				tag = 0;
469 			if (ag->ag_nhop != nhop)
470 				nhop = 0;
471 
472 			/* Get rid of the even twin that was already
473 			 * in the slot.
474 			 */
475 			ag_del(ag);
476 
477 		} else if (ag->ag_pref >= pref
478 			   && (ag->ag_state & AGS_AGGREGATE)) {
479 			/* If we cannot combine the pair, maybe the route
480 			 * with the worse metric can be promoted.
481 			 *
482 			 * Promote the old, even twin, by giving its slot
483 			 * in the table to the new, odd twin.
484 			 */
485 			ag->ag_dst_h = dst;
486 
487 			xaddr = ag->ag_gate;
488 			ag->ag_gate = gate;
489 			gate = xaddr;
490 
491 			xaddr = ag->ag_nhop;
492 			ag->ag_nhop = nhop;
493 			nhop = xaddr;
494 
495 			x = ag->ag_tag;
496 			ag->ag_tag = tag;
497 			tag = x;
498 
499 			/* The promoted route is even-redundant only if the
500 			 * even twin was fully redundant.  It is not
501 			 * odd-redundant because the odd-twin will still be
502 			 * in the table.
503 			 */
504 			x = ag->ag_state;
505 			if (!AG_IS_REDUN(x))
506 				x &= ~AGS_REDUN0;
507 			x &= ~AGS_REDUN1;
508 			ag->ag_state = state;
509 			state = x;
510 
511 			x = ag->ag_metric;
512 			ag->ag_metric = metric;
513 			metric = x;
514 
515 			x = ag->ag_pref;
516 			ag->ag_pref = pref;
517 			pref = x;
518 
519 			/* take the newest sequence number */
520 			if (seqnum >= ag->ag_seqno)
521 				seqnum = ag->ag_seqno;
522 			else
523 				ag->ag_seqno = seqnum;
524 
525 		} else {
526 			if (!(state & AGS_AGGREGATE))
527 				break;	/* cannot promote either twin */
528 
529 			/* Promote the new, odd twin by shaving its
530 			 * mask and address.
531 			 * The promoted route is odd-redundant only if the
532 			 * odd twin was fully redundant.  It is not
533 			 * even-redundant because the even twin is still in
534 			 * the table.
535 			 */
536 			if (!AG_IS_REDUN(state))
537 				state &= ~AGS_REDUN1;
538 			state &= ~AGS_REDUN0;
539 			if (seqnum > ag->ag_seqno)
540 				seqnum = ag->ag_seqno;
541 			else
542 				ag->ag_seqno = seqnum;
543 		}
544 
545 		mask <<= 1;
546 		dst &= mask;
547 
548 		if (ag_cors == NULL) {
549 			ag = ag_corsest;
550 			break;
551 		}
552 		ag = ag_cors;
553 		ag_cors = ag->ag_cors;
554 	}
555 
556 	/* When we can no longer promote and combine routes,
557 	 * flush the old route in the target slot.  Also flush
558 	 * any finer routes that we know will never be aggregated by
559 	 * the new route.
560 	 *
561 	 * In case we moved toward coarser masks,
562 	 * get back where we belong
563 	 */
564 	if (ag != NULL
565 	    && ag->ag_mask < mask) {
566 		ag_cors = ag;
567 		ag = ag->ag_fine;
568 	}
569 
570 	/* Empty the target slot
571 	 */
572 	if (ag != NULL && ag->ag_mask == mask) {
573 		ag_flush(ag->ag_dst_h, ag->ag_mask, out);
574 		ag = (ag_cors == NULL) ? ag_corsest : ag_cors->ag_fine;
575 	}
576 
577 #ifdef DEBUG_AG
578 	fflush(stderr);
579 	if (ag == NULL && ag_cors != ag_finest)
580 		abort();
581 	if (ag_cors == NULL && ag != ag_corsest)
582 		abort();
583 	if (ag != NULL && ag->ag_cors != ag_cors)
584 		abort();
585 	if (ag_cors != NULL && ag_cors->ag_fine != ag)
586 		abort();
587 	CHECK_AG();
588 #endif
589 
590 	/* Save the new route on the end of the table.
591 	 */
592 	nag = ag_avail;
593 	ag_avail = nag->ag_fine;
594 
595 	nag->ag_dst_h = dst;
596 	nag->ag_mask = mask;
597 	nag->ag_gate = gate;
598 	nag->ag_nhop = nhop;
599 	nag->ag_metric = metric;
600 	nag->ag_pref = pref;
601 	nag->ag_tag = tag;
602 	nag->ag_state = state;
603 	nag->ag_seqno = seqnum;
604 
605 	nag->ag_fine = ag;
606 	if (ag != NULL)
607 		ag->ag_cors = nag;
608 	else
609 		ag_finest = nag;
610 	nag->ag_cors = ag_cors;
611 	if (ag_cors == NULL)
612 		ag_corsest = nag;
613 	else
614 		ag_cors->ag_fine = nag;
615 	CHECK_AG();
616 }
617 
618 
619 #define	NAME0_LEN 14
620 static const char *
621 rtm_type_name(u_char type)
622 {
623 	static const char *rtm_types[] = {
624 		"RTM_ADD",
625 		"RTM_DELETE",
626 		"RTM_CHANGE",
627 		"RTM_GET",
628 		"RTM_LOSING",
629 		"RTM_REDIRECT",
630 		"RTM_MISS",
631 		"RTM_LOCK",
632 		"RTM_OLDADD",
633 		"RTM_OLDDEL",
634 		"RTM_RESOLVE",
635 		"RTM_NEWADDR",
636 		"RTM_DELADDR",
637 		"RTM_IFINFO",
638 		"RTM_NEWMADDR",
639 		"RTM_DELMADDR"
640 	};
641 #define NEW_RTM_PAT "RTM type %#x"
642 	static char name0[sizeof(NEW_RTM_PAT)+2];
643 
644 
645 	if (type > sizeof(rtm_types)/sizeof(rtm_types[0])
646 	    || type == 0) {
647 		snprintf(name0, sizeof(name0), NEW_RTM_PAT, type);
648 		return name0;
649 	} else {
650 		return rtm_types[type-1];
651 	}
652 #undef NEW_RTM_PAT
653 }
654 
655 
656 /* Trim a mask in a sockaddr
657  *	Produce a length of 0 for an address of 0.
658  *	Otherwise produce the index of the first zero byte.
659  */
660 void
661 #ifdef _HAVE_SIN_LEN
662 masktrim(struct sockaddr_in *ap)
663 #else
664 masktrim(struct sockaddr_in_new *ap)
665 #endif
666 {
667 	char *cp;
668 
669 	if (ap->sin_addr.s_addr == 0) {
670 		ap->sin_len = 0;
671 		return;
672 	}
673 	cp = (char *)(&ap->sin_addr.s_addr+1);
674 	while (*--cp == 0)
675 		continue;
676 	ap->sin_len = cp - (char*)ap + 1;
677 }
678 
679 
680 /* Tell the kernel to add, delete or change a route
681  */
682 static void
683 rtioctl(int action,			/* RTM_DELETE, etc */
684 	naddr dst,
685 	naddr gate,
686 	naddr mask,
687 	int metric,
688 	int flags)
689 {
690 	struct {
691 		struct rt_msghdr w_rtm;
692 		struct sockaddr_in w_dst;
693 		struct sockaddr_in w_gate;
694 #ifdef _HAVE_SA_LEN
695 		struct sockaddr_in w_mask;
696 #else
697 		struct sockaddr_in_new w_mask;
698 #endif
699 	} w;
700 	long cc;
701 #   define PAT " %-10s %s metric=%d flags=%#x"
702 #   define ARGS rtm_type_name(action), rtname(dst,mask,gate), metric, flags
703 
704 again:
705 	memset(&w, 0, sizeof(w));
706 	w.w_rtm.rtm_msglen = sizeof(w);
707 	w.w_rtm.rtm_version = RTM_VERSION;
708 	w.w_rtm.rtm_type = action;
709 	w.w_rtm.rtm_flags = flags;
710 	w.w_rtm.rtm_seq = ++rt_sock_seqno;
711 	w.w_rtm.rtm_addrs = RTA_DST|RTA_GATEWAY;
712 	if (metric != 0 || action == RTM_CHANGE) {
713 		w.w_rtm.rtm_rmx.rmx_hopcount = metric;
714 		w.w_rtm.rtm_inits |= RTV_HOPCOUNT;
715 	}
716 	w.w_dst.sin_family = AF_INET;
717 	w.w_dst.sin_addr.s_addr = dst;
718 	w.w_gate.sin_family = AF_INET;
719 	w.w_gate.sin_addr.s_addr = gate;
720 #ifdef _HAVE_SA_LEN
721 	w.w_dst.sin_len = sizeof(w.w_dst);
722 	w.w_gate.sin_len = sizeof(w.w_gate);
723 #endif
724 	if (mask == HOST_MASK) {
725 		w.w_rtm.rtm_flags |= RTF_HOST;
726 		w.w_rtm.rtm_msglen -= sizeof(w.w_mask);
727 	} else {
728 		w.w_rtm.rtm_addrs |= RTA_NETMASK;
729 		w.w_mask.sin_addr.s_addr = htonl(mask);
730 #ifdef _HAVE_SA_LEN
731 		masktrim(&w.w_mask);
732 		if (w.w_mask.sin_len == 0)
733 			w.w_mask.sin_len = sizeof(long);
734 		w.w_rtm.rtm_msglen -= (sizeof(w.w_mask) - w.w_mask.sin_len);
735 #endif
736 	}
737 
738 #ifndef NO_INSTALL
739 	cc = write(rt_sock, &w, w.w_rtm.rtm_msglen);
740 	if (cc < 0) {
741 		if (errno == ESRCH
742 		    && (action == RTM_CHANGE || action == RTM_DELETE)) {
743 			trace_act("route disappeared before" PAT, ARGS);
744 			if (action == RTM_CHANGE) {
745 				action = RTM_ADD;
746 				goto again;
747 			}
748 			return;
749 		}
750 		msglog("write(rt_sock)" PAT ": %s", ARGS, strerror(errno));
751 		return;
752 	} else if (cc != w.w_rtm.rtm_msglen) {
753 		msglog("write(rt_sock) wrote %ld instead of %d for" PAT,
754 		       cc, w.w_rtm.rtm_msglen, ARGS);
755 		return;
756 	}
757 #endif
758 	if (TRACEKERNEL)
759 		trace_misc("write kernel" PAT, ARGS);
760 #undef PAT
761 #undef ARGS
762 }
763 
764 
765 #define KHASH_SIZE 71			/* should be prime */
766 #define KHASH(a,m) khash_bins[((a) ^ (m)) % KHASH_SIZE]
767 static struct khash {
768 	struct khash *k_next;
769 	naddr	k_dst;
770 	naddr	k_mask;
771 	naddr	k_gate;
772 	short	k_metric;
773 	u_short	k_state;
774 #define	    KS_NEW	0x001
775 #define	    KS_DELETE	0x002		/* need to delete the route */
776 #define	    KS_ADD	0x004		/* add to the kernel */
777 #define	    KS_CHANGE	0x008		/* tell kernel to change the route */
778 #define	    KS_DEL_ADD	0x010		/* delete & add to change the kernel */
779 #define	    KS_STATIC	0x020		/* Static flag in kernel */
780 #define	    KS_GATEWAY	0x040		/* G flag in kernel */
781 #define	    KS_DYNAMIC	0x080		/* result of redirect */
782 #define	    KS_DELETED	0x100		/* already deleted from kernel */
783 #define	    KS_CHECK	0x200
784 	time_t	k_keep;
785 #define	    K_KEEP_LIM	30
786 	time_t	k_redirect_time;	/* when redirected route 1st seen */
787 } *khash_bins[KHASH_SIZE];
788 
789 
790 static struct khash*
791 kern_find(naddr dst, naddr mask, struct khash ***ppk)
792 {
793 	struct khash *k, **pk;
794 
795 	for (pk = &KHASH(dst,mask); (k = *pk) != NULL; pk = &k->k_next) {
796 		if (k->k_dst == dst && k->k_mask == mask)
797 			break;
798 	}
799 	if (ppk != NULL)
800 		*ppk = pk;
801 	return k;
802 }
803 
804 
805 static struct khash*
806 kern_add(naddr dst, naddr mask)
807 {
808 	struct khash *k, **pk;
809 
810 	k = kern_find(dst, mask, &pk);
811 	if (k != NULL)
812 		return k;
813 
814 	k = (struct khash *)rtmalloc(sizeof(*k), "kern_add");
815 
816 	memset(k, 0, sizeof(*k));
817 	k->k_dst = dst;
818 	k->k_mask = mask;
819 	k->k_state = KS_NEW;
820 	k->k_keep = now.tv_sec;
821 	*pk = k;
822 
823 	return k;
824 }
825 
826 
827 /* If a kernel route has a non-zero metric, check that it is still in the
828  *	daemon table, and not deleted by interfaces coming and going.
829  */
830 static void
831 kern_check_static(struct khash *k,
832 		  struct interface *ifp)
833 {
834 	struct rt_entry *rt;
835 	struct rt_spare new;
836 
837 	if (k->k_metric == 0)
838 		return;
839 
840 	memset(&new, 0, sizeof(new));
841 	new.rts_ifp = ifp;
842 	new.rts_gate = k->k_gate;
843 	new.rts_router = (ifp != NULL) ? ifp->int_addr : loopaddr;
844 	new.rts_metric = k->k_metric;
845 	new.rts_time = now.tv_sec;
846 
847 	rt = rtget(k->k_dst, k->k_mask);
848 	if (rt != NULL) {
849 		if (!(rt->rt_state & RS_STATIC))
850 			rtchange(rt, rt->rt_state | RS_STATIC, &new, 0);
851 	} else {
852 		rtadd(k->k_dst, k->k_mask, RS_STATIC, &new);
853 	}
854 }
855 
856 
857 /* operate on a kernel entry
858  */
859 static void
860 kern_ioctl(struct khash *k,
861 	   int action,			/* RTM_DELETE, etc */
862 	   int flags)
863 
864 {
865 	switch (action) {
866 	case RTM_DELETE:
867 		k->k_state &= ~KS_DYNAMIC;
868 		if (k->k_state & KS_DELETED)
869 			return;
870 		k->k_state |= KS_DELETED;
871 		break;
872 	case RTM_ADD:
873 		k->k_state &= ~KS_DELETED;
874 		break;
875 	case RTM_CHANGE:
876 		if (k->k_state & KS_DELETED) {
877 			action = RTM_ADD;
878 			k->k_state &= ~KS_DELETED;
879 		}
880 		break;
881 	}
882 
883 	rtioctl(action, k->k_dst, k->k_gate, k->k_mask, k->k_metric, flags);
884 }
885 
886 
887 /* add a route the kernel told us
888  */
889 static void
890 rtm_add(struct rt_msghdr *rtm,
891 	struct rt_addrinfo *info,
892 	time_t keep)
893 {
894 	struct khash *k;
895 	struct interface *ifp;
896 	naddr mask;
897 
898 
899 	if (rtm->rtm_flags & RTF_HOST) {
900 		mask = HOST_MASK;
901 	} else if (INFO_MASK(info) != 0) {
902 		mask = ntohl(S_ADDR(INFO_MASK(info)));
903 	} else {
904 		msglog("ignore %s without mask", rtm_type_name(rtm->rtm_type));
905 		return;
906 	}
907 
908 	k = kern_add(S_ADDR(INFO_DST(info)), mask);
909 	if (k->k_state & KS_NEW)
910 		k->k_keep = now.tv_sec+keep;
911 	if (INFO_GATE(info) == 0) {
912 		trace_act("note %s without gateway",
913 			  rtm_type_name(rtm->rtm_type));
914 		k->k_metric = HOPCNT_INFINITY;
915 	} else if (INFO_GATE(info)->sa_family != AF_INET) {
916 		trace_act("note %s with gateway AF=%d",
917 			  rtm_type_name(rtm->rtm_type),
918 			  INFO_GATE(info)->sa_family);
919 		k->k_metric = HOPCNT_INFINITY;
920 	} else {
921 		k->k_gate = S_ADDR(INFO_GATE(info));
922 		k->k_metric = rtm->rtm_rmx.rmx_hopcount;
923 		if (k->k_metric < 0)
924 			k->k_metric = 0;
925 		else if (k->k_metric > HOPCNT_INFINITY-1)
926 			k->k_metric = HOPCNT_INFINITY-1;
927 	}
928 	k->k_state &= ~(KS_DELETE | KS_ADD | KS_CHANGE | KS_DEL_ADD
929 			| KS_DELETED | KS_GATEWAY | KS_STATIC
930 			| KS_NEW | KS_CHECK);
931 	if (rtm->rtm_flags & RTF_GATEWAY)
932 		k->k_state |= KS_GATEWAY;
933 	if (rtm->rtm_flags & RTF_STATIC)
934 		k->k_state |= KS_STATIC;
935 
936 	if (0 != (rtm->rtm_flags & (RTF_DYNAMIC | RTF_MODIFIED))) {
937 		if (INFO_AUTHOR(info) != 0
938 		    && INFO_AUTHOR(info)->sa_family == AF_INET)
939 			ifp = iflookup(S_ADDR(INFO_AUTHOR(info)));
940 		else
941 			ifp = NULL;
942 		if (supplier
943 		    && (ifp == NULL || !(ifp->int_state & IS_REDIRECT_OK))) {
944 			/* Routers are not supposed to listen to redirects,
945 			 * so delete it if it came via an unknown interface
946 			 * or the interface does not have special permission.
947 			 */
948 			k->k_state &= ~KS_DYNAMIC;
949 			k->k_state |= KS_DELETE;
950 			LIM_SEC(need_kern, 0);
951 			trace_act("mark for deletion redirected %s --> %s"
952 				  " via %s",
953 				  addrname(k->k_dst, k->k_mask, 0),
954 				  naddr_ntoa(k->k_gate),
955 				  ifp ? ifp->int_name : "unknown interface");
956 		} else {
957 			k->k_state |= KS_DYNAMIC;
958 			k->k_redirect_time = now.tv_sec;
959 			trace_act("accept redirected %s --> %s via %s",
960 				  addrname(k->k_dst, k->k_mask, 0),
961 				  naddr_ntoa(k->k_gate),
962 				  ifp ? ifp->int_name : "unknown interface");
963 		}
964 		return;
965 	}
966 
967 	/* If it is not a static route, quit until the next comparison
968 	 * between the kernel and daemon tables, when it will be deleted.
969 	 */
970 	if (!(k->k_state & KS_STATIC)) {
971 		k->k_state |= KS_DELETE;
972 		LIM_SEC(need_kern, k->k_keep);
973 		return;
974 	}
975 
976 	/* Put static routes with real metrics into the daemon table so
977 	 * they can be advertised.
978 	 *
979 	 * Find the interface toward the gateway.
980 	 */
981 	ifp = iflookup(k->k_gate);
982 	if (ifp == NULL)
983 		msglog("static route %s --> %s impossibly lacks ifp",
984 		       addrname(S_ADDR(INFO_DST(info)), mask, 0),
985 		       naddr_ntoa(k->k_gate));
986 
987 	kern_check_static(k, ifp);
988 }
989 
990 
991 /* deal with packet loss
992  */
993 static void
994 rtm_lose(struct rt_msghdr *rtm,
995 	 struct rt_addrinfo *info)
996 {
997 	if (INFO_GATE(info) == 0
998 	    || INFO_GATE(info)->sa_family != AF_INET) {
999 		trace_act("ignore %s without gateway",
1000 			  rtm_type_name(rtm->rtm_type));
1001 		return;
1002 	}
1003 
1004 	if (rdisc_ok)
1005 		rdisc_age(S_ADDR(INFO_GATE(info)));
1006 	age(S_ADDR(INFO_GATE(info)));
1007 }
1008 
1009 
1010 /* Make the gateway slot of an info structure point to something
1011  * useful.  If it is not already useful, but it specifies an interface,
1012  * then fill in the sockaddr_in provided and point it there.
1013  */
1014 static int
1015 get_info_gate(struct sockaddr **sap,
1016 	      struct sockaddr_in *in)
1017 {
1018 	struct sockaddr_dl *sdl = (struct sockaddr_dl *)*sap;
1019 	struct interface *ifp;
1020 
1021 	if (sdl == NULL)
1022 		return 0;
1023 	if ((sdl)->sdl_family == AF_INET)
1024 		return 1;
1025 	if ((sdl)->sdl_family != AF_LINK)
1026 		return 0;
1027 
1028 	ifp = ifwithindex(sdl->sdl_index, 1);
1029 	if (ifp == NULL)
1030 		return 0;
1031 
1032 	in->sin_addr.s_addr = ifp->int_addr;
1033 #ifdef _HAVE_SA_LEN
1034 	in->sin_len = sizeof(*in);
1035 #endif
1036 	in->sin_family = AF_INET;
1037 	*sap = (struct sockaddr *)in;
1038 
1039 	return 1;
1040 }
1041 
1042 
1043 /* Clean the kernel table by copying it to the daemon image.
1044  * Eventually the daemon will delete any extra routes.
1045  */
1046 void
1047 flush_kern(void)
1048 {
1049 	static char *sysctl_buf;
1050 	static size_t sysctl_buf_size = 0;
1051 	size_t needed;
1052 	int mib[6];
1053 	char *next, *lim;
1054 	struct rt_msghdr *rtm;
1055 	struct sockaddr_in gate_sin;
1056 	struct rt_addrinfo info;
1057 	int i;
1058 	struct khash *k;
1059 
1060 
1061 	for (i = 0; i < KHASH_SIZE; i++) {
1062 		for (k = khash_bins[i]; k != NULL; k = k->k_next) {
1063 			k->k_state |= KS_CHECK;
1064 		}
1065 	}
1066 
1067 	mib[0] = CTL_NET;
1068 	mib[1] = PF_ROUTE;
1069 	mib[2] = 0;		/* protocol */
1070 	mib[3] = 0;		/* wildcard address family */
1071 	mib[4] = NET_RT_DUMP;
1072 	mib[5] = 0;		/* no flags */
1073 	for (;;) {
1074 		if ((needed = sysctl_buf_size) != 0) {
1075 			if (sysctl(mib, 6, sysctl_buf,&needed, 0, 0) >= 0)
1076 				break;
1077 			if (errno != ENOMEM && errno != EFAULT)
1078 				BADERR(1,"flush_kern: sysctl(RT_DUMP)");
1079 			free(sysctl_buf);
1080 			needed = 0;
1081 		}
1082 		if (sysctl(mib, 6, 0, &needed, 0, 0) < 0)
1083 			BADERR(1,"flush_kern: sysctl(RT_DUMP) estimate");
1084 		/* Kludge around the habit of some systems, such as
1085 		 * BSD/OS 3.1, to not admit how many routes are in the
1086 		 * kernel, or at least to be quite wrong.
1087 		 */
1088 		needed += 50*(sizeof(*rtm)+5*sizeof(struct sockaddr));
1089 		sysctl_buf = rtmalloc(sysctl_buf_size = needed,
1090 				      "flush_kern sysctl(RT_DUMP)");
1091 	}
1092 
1093 	lim = sysctl_buf + needed;
1094 	for (next = sysctl_buf; next < lim; next += rtm->rtm_msglen) {
1095 		rtm = (struct rt_msghdr *)next;
1096 		if (rtm->rtm_msglen == 0) {
1097 			msglog("zero length kernel route at "
1098 			       " %#lx in buffer %#lx before %#lx",
1099 			       (u_long)rtm, (u_long)sysctl_buf, (u_long)lim);
1100 			break;
1101 		}
1102 
1103 		rt_xaddrs(&info,
1104 			  (struct sockaddr *)(rtm+1),
1105 			  (struct sockaddr *)(next + rtm->rtm_msglen),
1106 			  rtm->rtm_addrs);
1107 
1108 		if (INFO_DST(&info) == 0
1109 		    || INFO_DST(&info)->sa_family != AF_INET)
1110 			continue;
1111 
1112 		/* ignore ARP table entries on systems with a merged route
1113 		 * and ARP table.
1114 		 */
1115 		if (rtm->rtm_flags & RTF_LLINFO)
1116 			continue;
1117 
1118 		/* ignore multicast addresses
1119 		 */
1120 		if (IN_MULTICAST(ntohl(S_ADDR(INFO_DST(&info)))))
1121 			continue;
1122 
1123 		if (!get_info_gate(&INFO_GATE(&info), &gate_sin))
1124 			continue;
1125 
1126 		/* Note static routes and interface routes, and also
1127 		 * preload the image of the kernel table so that
1128 		 * we can later clean it, as well as avoid making
1129 		 * unneeded changes.  Keep the old kernel routes for a
1130 		 * few seconds to allow a RIP or router-discovery
1131 		 * response to be heard.
1132 		 */
1133 		rtm_add(rtm,&info,MIN_WAITTIME);
1134 	}
1135 
1136 	for (i = 0; i < KHASH_SIZE; i++) {
1137 		for (k = khash_bins[i]; k != NULL; k = k->k_next) {
1138 			if (k->k_state & KS_CHECK) {
1139 				msglog("%s --> %s disappeared from kernel",
1140 				       addrname(k->k_dst, k->k_mask, 0),
1141 				       naddr_ntoa(k->k_gate));
1142 				del_static(k->k_dst, k->k_mask, k->k_gate, 1);
1143 			}
1144 		}
1145 	}
1146 }
1147 
1148 
1149 /* Listen to announcements from the kernel
1150  */
1151 void
1152 read_rt(void)
1153 {
1154 	long cc;
1155 	struct interface *ifp;
1156 	struct sockaddr_in gate_sin;
1157 	naddr mask, gate;
1158 	union {
1159 		struct {
1160 			struct rt_msghdr rtm;
1161 			struct sockaddr addrs[RTAX_MAX];
1162 		} r;
1163 		struct if_msghdr ifm;
1164 	} m;
1165 	char str[100], *strp;
1166 	struct rt_addrinfo info;
1167 
1168 
1169 	for (;;) {
1170 		cc = read(rt_sock, &m, sizeof(m));
1171 		if (cc <= 0) {
1172 			if (cc < 0 && errno != EWOULDBLOCK)
1173 				LOGERR("read(rt_sock)");
1174 			return;
1175 		}
1176 
1177 		if (m.r.rtm.rtm_version != RTM_VERSION) {
1178 			msglog("bogus routing message version %d",
1179 			       m.r.rtm.rtm_version);
1180 			continue;
1181 		}
1182 
1183 		/* Ignore our own results.
1184 		 */
1185 		if (m.r.rtm.rtm_type <= RTM_CHANGE
1186 		    && m.r.rtm.rtm_pid == mypid) {
1187 			static int complained = 0;
1188 			if (!complained) {
1189 				msglog("receiving our own change messages");
1190 				complained = 1;
1191 			}
1192 			continue;
1193 		}
1194 
1195 		if (m.r.rtm.rtm_type == RTM_IFINFO
1196 		    || m.r.rtm.rtm_type == RTM_NEWADDR
1197 		    || m.r.rtm.rtm_type == RTM_DELADDR) {
1198 			ifp = ifwithindex(m.ifm.ifm_index,
1199 					  m.r.rtm.rtm_type != RTM_DELADDR);
1200 			if (ifp == NULL)
1201 				trace_act("note %s with flags %#x"
1202 					  " for unknown interface index #%d",
1203 					  rtm_type_name(m.r.rtm.rtm_type),
1204 					  m.ifm.ifm_flags,
1205 					  m.ifm.ifm_index);
1206 			else
1207 				trace_act("note %s with flags %#x for %s",
1208 					  rtm_type_name(m.r.rtm.rtm_type),
1209 					  m.ifm.ifm_flags,
1210 					  ifp->int_name);
1211 
1212 			/* After being informed of a change to an interface,
1213 			 * check them all now if the check would otherwise
1214 			 * be a long time from now, if the interface is
1215 			 * not known, or if the interface has been turned
1216 			 * off or on.
1217 			 */
1218 			if (ifinit_timer.tv_sec-now.tv_sec>=CHECK_BAD_INTERVAL
1219 			    || ifp == NULL
1220 			    || ((ifp->int_if_flags ^ m.ifm.ifm_flags)
1221 				& IFF_UP) != 0)
1222 				ifinit_timer.tv_sec = now.tv_sec;
1223 			continue;
1224 		}
1225 
1226 		strcpy(str, rtm_type_name(m.r.rtm.rtm_type));
1227 		strp = &str[strlen(str)];
1228 		if (m.r.rtm.rtm_type <= RTM_CHANGE)
1229 			strp += sprintf(strp," from pid %d",m.r.rtm.rtm_pid);
1230 
1231 		rt_xaddrs(&info, m.r.addrs, &m.r.addrs[RTAX_MAX],
1232 			  m.r.rtm.rtm_addrs);
1233 
1234 		if (INFO_DST(&info) == 0) {
1235 			trace_act("ignore %s without dst", str);
1236 			continue;
1237 		}
1238 
1239 		if (INFO_DST(&info)->sa_family != AF_INET) {
1240 			trace_act("ignore %s for AF %d", str,
1241 				  INFO_DST(&info)->sa_family);
1242 			continue;
1243 		}
1244 
1245 		mask = ((INFO_MASK(&info) != 0)
1246 			? ntohl(S_ADDR(INFO_MASK(&info)))
1247 			: (m.r.rtm.rtm_flags & RTF_HOST)
1248 			? HOST_MASK
1249 			: std_mask(S_ADDR(INFO_DST(&info))));
1250 
1251 		strp += sprintf(strp, ": %s",
1252 				addrname(S_ADDR(INFO_DST(&info)), mask, 0));
1253 
1254 		if (IN_MULTICAST(ntohl(S_ADDR(INFO_DST(&info))))) {
1255 			trace_act("ignore multicast %s", str);
1256 			continue;
1257 		}
1258 
1259 		if (m.r.rtm.rtm_flags & RTF_LLINFO) {
1260 			trace_act("ignore ARP %s", str);
1261 			continue;
1262 		}
1263 
1264 		if (get_info_gate(&INFO_GATE(&info), &gate_sin)) {
1265 			gate = S_ADDR(INFO_GATE(&info));
1266 			strp += sprintf(strp, " --> %s", naddr_ntoa(gate));
1267 		} else {
1268 			gate = 0;
1269 		}
1270 
1271 		if (INFO_AUTHOR(&info) != 0)
1272 			strp += sprintf(strp, " by authority of %s",
1273 					saddr_ntoa(INFO_AUTHOR(&info)));
1274 
1275 		switch (m.r.rtm.rtm_type) {
1276 		case RTM_ADD:
1277 		case RTM_CHANGE:
1278 		case RTM_REDIRECT:
1279 			if (m.r.rtm.rtm_errno != 0) {
1280 				trace_act("ignore %s with \"%s\" error",
1281 					  str, strerror(m.r.rtm.rtm_errno));
1282 			} else {
1283 				trace_act("%s", str);
1284 				rtm_add(&m.r.rtm,&info,0);
1285 			}
1286 			break;
1287 
1288 		case RTM_DELETE:
1289 			if (m.r.rtm.rtm_errno != 0
1290 			    && m.r.rtm.rtm_errno != ESRCH) {
1291 				trace_act("ignore %s with \"%s\" error",
1292 					  str, strerror(m.r.rtm.rtm_errno));
1293 			} else {
1294 				trace_act("%s", str);
1295 				del_static(S_ADDR(INFO_DST(&info)), mask,
1296 					   gate, 1);
1297 			}
1298 			break;
1299 
1300 		case RTM_LOSING:
1301 			trace_act("%s", str);
1302 			rtm_lose(&m.r.rtm,&info);
1303 			break;
1304 
1305 		default:
1306 			trace_act("ignore %s", str);
1307 			break;
1308 		}
1309 	}
1310 }
1311 
1312 
1313 /* after aggregating, note routes that belong in the kernel
1314  */
1315 static void
1316 kern_out(struct ag_info *ag)
1317 {
1318 	struct khash *k;
1319 
1320 
1321 	/* Do not install bad routes if they are not already present.
1322 	 * This includes routes that had RS_NET_SYN for interfaces that
1323 	 * recently died.
1324 	 */
1325 	if (ag->ag_metric == HOPCNT_INFINITY) {
1326 		k = kern_find(htonl(ag->ag_dst_h), ag->ag_mask, 0);
1327 		if (k == NULL)
1328 			return;
1329 	} else {
1330 		k = kern_add(htonl(ag->ag_dst_h), ag->ag_mask);
1331 	}
1332 
1333 	if (k->k_state & KS_NEW) {
1334 		/* will need to add new entry to the kernel table */
1335 		k->k_state = KS_ADD;
1336 		if (ag->ag_state & AGS_GATEWAY)
1337 			k->k_state |= KS_GATEWAY;
1338 		k->k_gate = ag->ag_gate;
1339 		k->k_metric = ag->ag_metric;
1340 		return;
1341 	}
1342 
1343 	if (k->k_state & KS_STATIC)
1344 		return;
1345 
1346 	/* modify existing kernel entry if necessary */
1347 	if (k->k_gate != ag->ag_gate
1348 	    || k->k_metric != ag->ag_metric) {
1349 		/* Must delete bad interface routes etc. to change them. */
1350 		if (k->k_metric == HOPCNT_INFINITY)
1351 			k->k_state |= KS_DEL_ADD;
1352 		k->k_gate = ag->ag_gate;
1353 		k->k_metric = ag->ag_metric;
1354 		k->k_state |= KS_CHANGE;
1355 	}
1356 
1357 	/* If the daemon thinks the route should exist, forget
1358 	 * about any redirections.
1359 	 * If the daemon thinks the route should exist, eventually
1360 	 * override manual intervention by the operator.
1361 	 */
1362 	if ((k->k_state & (KS_DYNAMIC | KS_DELETED)) != 0) {
1363 		k->k_state &= ~KS_DYNAMIC;
1364 		k->k_state |= (KS_ADD | KS_DEL_ADD);
1365 	}
1366 
1367 	if ((k->k_state & KS_GATEWAY)
1368 	    && !(ag->ag_state & AGS_GATEWAY)) {
1369 		k->k_state &= ~KS_GATEWAY;
1370 		k->k_state |= (KS_ADD | KS_DEL_ADD);
1371 	} else if (!(k->k_state & KS_GATEWAY)
1372 		   && (ag->ag_state & AGS_GATEWAY)) {
1373 		k->k_state |= KS_GATEWAY;
1374 		k->k_state |= (KS_ADD | KS_DEL_ADD);
1375 	}
1376 
1377 	/* Deleting-and-adding is necessary to change aspects of a route.
1378 	 * Just delete instead of deleting and then adding a bad route.
1379 	 * Otherwise, we want to keep the route in the kernel.
1380 	 */
1381 	if (k->k_metric == HOPCNT_INFINITY
1382 	    && (k->k_state & KS_DEL_ADD))
1383 		k->k_state |= KS_DELETE;
1384 	else
1385 		k->k_state &= ~KS_DELETE;
1386 #undef RT
1387 }
1388 
1389 
1390 /* ARGSUSED */
1391 static int
1392 walk_kern(struct radix_node *rn,
1393 	  struct walkarg *argp UNUSED)
1394 {
1395 #define RT ((struct rt_entry *)rn)
1396 	char metric, pref;
1397 	u_int ags = 0;
1398 
1399 
1400 	/* Do not install synthetic routes */
1401 	if (RT->rt_state & RS_NET_SYN)
1402 		return 0;
1403 
1404 	if (!(RT->rt_state & RS_IF)) {
1405 		/* This is an ordinary route, not for an interface.
1406 		 */
1407 
1408 		/* aggregate, ordinary good routes without regard to
1409 		 * their metric
1410 		 */
1411 		pref = 1;
1412 		ags |= (AGS_GATEWAY | AGS_SUPPRESS | AGS_AGGREGATE);
1413 
1414 		/* Do not install host routes directly to hosts, to avoid
1415 		 * interfering with ARP entries in the kernel table.
1416 		 */
1417 		if (RT_ISHOST(RT)
1418 		    && ntohl(RT->rt_dst) == RT->rt_gate)
1419 			return 0;
1420 
1421 	} else {
1422 		/* This is an interface route.
1423 		 * Do not install routes for "external" remote interfaces.
1424 		 */
1425 		if (RT->rt_ifp != 0 && (RT->rt_ifp->int_state & IS_EXTERNAL))
1426 			return 0;
1427 
1428 		/* Interfaces should override received routes.
1429 		 */
1430 		pref = 0;
1431 		ags |= (AGS_IF | AGS_CORS_GATE);
1432 
1433 		/* If it is not an interface, or an alias for an interface,
1434 		 * it must be a "gateway."
1435 		 *
1436 		 * If it is a "remote" interface, it is also a "gateway" to
1437 		 * the kernel if is not a alias.
1438 		 */
1439 		if (RT->rt_ifp == 0
1440 		    || (RT->rt_ifp->int_state & IS_REMOTE))
1441 			ags |= (AGS_GATEWAY | AGS_SUPPRESS | AGS_AGGREGATE);
1442 	}
1443 
1444 	/* If RIP is off and IRDP is on, let the route to the discovered
1445 	 * route suppress any RIP routes.  Eventually the RIP routes
1446 	 * will time-out and be deleted.  This reaches the steady-state
1447 	 * quicker.
1448 	 */
1449 	if ((RT->rt_state & RS_RDISC) && rip_sock < 0)
1450 		ags |= AGS_CORS_GATE;
1451 
1452 	metric = RT->rt_metric;
1453 	if (metric == HOPCNT_INFINITY) {
1454 		/* if the route is dead, so try hard to aggregate. */
1455 		pref = HOPCNT_INFINITY;
1456 		ags |= (AGS_FINE_GATE | AGS_SUPPRESS);
1457 		ags &= ~(AGS_IF | AGS_CORS_GATE);
1458 	}
1459 
1460 	ag_check(RT->rt_dst, RT->rt_mask, RT->rt_gate, 0,
1461 		 metric,pref, 0, 0, ags, kern_out);
1462 	return 0;
1463 #undef RT
1464 }
1465 
1466 
1467 /* Update the kernel table to match the daemon table.
1468  */
1469 static void
1470 fix_kern(void)
1471 {
1472 	int i;
1473 	struct khash *k, **pk;
1474 
1475 
1476 	need_kern = age_timer;
1477 
1478 	/* Walk daemon table, updating the copy of the kernel table.
1479 	 */
1480 	rn_walktree(rhead, walk_kern, 0);
1481 	ag_flush(0,0,kern_out);
1482 
1483 	for (i = 0; i < KHASH_SIZE; i++) {
1484 		for (pk = &khash_bins[i]; (k = *pk) != NULL; ) {
1485 			/* Do not touch static routes */
1486 			if (k->k_state & KS_STATIC) {
1487 				kern_check_static(k,0);
1488 				pk = &k->k_next;
1489 				continue;
1490 			}
1491 
1492 			/* check hold on routes deleted by the operator */
1493 			if (k->k_keep > now.tv_sec) {
1494 				/* ensure we check when the hold is over */
1495 				LIM_SEC(need_kern, k->k_keep);
1496 				/* mark for the next cycle */
1497 				k->k_state |= KS_DELETE;
1498 				pk = &k->k_next;
1499 				continue;
1500 			}
1501 
1502 			if ((k->k_state & KS_DELETE)
1503 			    && !(k->k_state & KS_DYNAMIC)) {
1504 				kern_ioctl(k, RTM_DELETE, 0);
1505 				*pk = k->k_next;
1506 				free(k);
1507 				continue;
1508 			}
1509 
1510 			if (k->k_state & KS_DEL_ADD)
1511 				kern_ioctl(k, RTM_DELETE, 0);
1512 
1513 			if (k->k_state & KS_ADD) {
1514 				kern_ioctl(k, RTM_ADD,
1515 					   ((0 != (k->k_state & (KS_GATEWAY
1516 							| KS_DYNAMIC)))
1517 					    ? RTF_GATEWAY : 0));
1518 			} else if (k->k_state & KS_CHANGE) {
1519 				kern_ioctl(k,  RTM_CHANGE,
1520 					   ((0 != (k->k_state & (KS_GATEWAY
1521 							| KS_DYNAMIC)))
1522 					    ? RTF_GATEWAY : 0));
1523 			}
1524 			k->k_state &= ~(KS_ADD|KS_CHANGE|KS_DEL_ADD);
1525 
1526 			/* Mark this route to be deleted in the next cycle.
1527 			 * This deletes routes that disappear from the
1528 			 * daemon table, since the normal aging code
1529 			 * will clear the bit for routes that have not
1530 			 * disappeared from the daemon table.
1531 			 */
1532 			k->k_state |= KS_DELETE;
1533 			pk = &k->k_next;
1534 		}
1535 	}
1536 }
1537 
1538 
1539 /* Delete a static route in the image of the kernel table.
1540  */
1541 void
1542 del_static(naddr dst,
1543 	   naddr mask,
1544 	   naddr gate,
1545 	   int gone)
1546 {
1547 	struct khash *k;
1548 	struct rt_entry *rt;
1549 
1550 	/* Just mark it in the table to be deleted next time the kernel
1551 	 * table is updated.
1552 	 * If it has already been deleted, mark it as such, and set its
1553 	 * keep-timer so that it will not be deleted again for a while.
1554 	 * This lets the operator delete a route added by the daemon
1555 	 * and add a replacement.
1556 	 */
1557 	k = kern_find(dst, mask, 0);
1558 	if (k != NULL && (gate == 0 || k->k_gate == gate)) {
1559 		k->k_state &= ~(KS_STATIC | KS_DYNAMIC | KS_CHECK);
1560 		k->k_state |= KS_DELETE;
1561 		if (gone) {
1562 			k->k_state |= KS_DELETED;
1563 			k->k_keep = now.tv_sec + K_KEEP_LIM;
1564 		}
1565 	}
1566 
1567 	rt = rtget(dst, mask);
1568 	if (rt != NULL && (rt->rt_state & RS_STATIC))
1569 		rtbad(rt);
1570 }
1571 
1572 
1573 /* Delete all routes generated from ICMP Redirects that use a given gateway,
1574  * as well as old redirected routes.
1575  */
1576 void
1577 del_redirects(naddr bad_gate,
1578 	      time_t old)
1579 {
1580 	int i;
1581 	struct khash *k;
1582 
1583 
1584 	for (i = 0; i < KHASH_SIZE; i++) {
1585 		for (k = khash_bins[i]; k != NULL; k = k->k_next) {
1586 			if (!(k->k_state & KS_DYNAMIC)
1587 			    || (k->k_state & KS_STATIC))
1588 				continue;
1589 
1590 			if (k->k_gate != bad_gate
1591 			    && k->k_redirect_time > old
1592 			    && !supplier)
1593 				continue;
1594 
1595 			k->k_state |= KS_DELETE;
1596 			k->k_state &= ~KS_DYNAMIC;
1597 			need_kern.tv_sec = now.tv_sec;
1598 			trace_act("mark redirected %s --> %s for deletion",
1599 				  addrname(k->k_dst, k->k_mask, 0),
1600 				  naddr_ntoa(k->k_gate));
1601 		}
1602 	}
1603 }
1604 
1605 
1606 /* Start the daemon tables.
1607  */
1608 extern int max_keylen;
1609 
1610 void
1611 rtinit(void)
1612 {
1613 	int i;
1614 	struct ag_info *ag;
1615 
1616 	/* Initialize the radix trees */
1617 	max_keylen = sizeof(struct sockaddr_in);
1618 	rn_init();
1619 	rn_inithead(&rhead, 32);
1620 
1621 	/* mark all of the slots in the table free */
1622 	ag_avail = ag_slots;
1623 	for (ag = ag_slots, i = 1; i < NUM_AG_SLOTS; i++) {
1624 		ag->ag_fine = ag+1;
1625 		ag++;
1626 	}
1627 }
1628 
1629 
1630 #ifdef _HAVE_SIN_LEN
1631 static struct sockaddr_in dst_sock = {sizeof(dst_sock), AF_INET, 0, {0}, {0}};
1632 static struct sockaddr_in mask_sock = {sizeof(mask_sock), AF_INET, 0, {0}, {0}};
1633 #else
1634 static struct sockaddr_in_new dst_sock = {_SIN_ADDR_SIZE, AF_INET};
1635 static struct sockaddr_in_new mask_sock = {_SIN_ADDR_SIZE, AF_INET};
1636 #endif
1637 
1638 
1639 static void
1640 set_need_flash(void)
1641 {
1642 	if (!need_flash) {
1643 		need_flash = 1;
1644 		/* Do not send the flash update immediately.  Wait a little
1645 		 * while to hear from other routers.
1646 		 */
1647 		no_flash.tv_sec = now.tv_sec + MIN_WAITTIME;
1648 	}
1649 }
1650 
1651 
1652 /* Get a particular routing table entry
1653  */
1654 struct rt_entry *
1655 rtget(naddr dst, naddr mask)
1656 {
1657 	struct rt_entry *rt;
1658 
1659 	dst_sock.sin_addr.s_addr = dst;
1660 	mask_sock.sin_addr.s_addr = htonl(mask);
1661 	masktrim(&mask_sock);
1662 	rt = (struct rt_entry *)rhead->rnh_lookup(&dst_sock,&mask_sock,rhead);
1663 	if (!rt
1664 	    || rt->rt_dst != dst
1665 	    || rt->rt_mask != mask)
1666 		return 0;
1667 
1668 	return rt;
1669 }
1670 
1671 
1672 /* Find a route to dst as the kernel would.
1673  */
1674 struct rt_entry *
1675 rtfind(naddr dst)
1676 {
1677 	dst_sock.sin_addr.s_addr = dst;
1678 	return (struct rt_entry *)rhead->rnh_matchaddr(&dst_sock, rhead);
1679 }
1680 
1681 
1682 /* add a route to the table
1683  */
1684 void
1685 rtadd(naddr	dst,
1686       naddr	mask,
1687       u_int	state,			/* rt_state for the entry */
1688       struct	rt_spare *new)
1689 {
1690 	struct rt_entry *rt;
1691 	naddr smask;
1692 	int i;
1693 	struct rt_spare *rts;
1694 
1695 	rt = (struct rt_entry *)rtmalloc(sizeof (*rt), "rtadd");
1696 	memset(rt, 0, sizeof(*rt));
1697 	for (rts = rt->rt_spares, i = NUM_SPARES; i != 0; i--, rts++)
1698 		rts->rts_metric = HOPCNT_INFINITY;
1699 
1700 	rt->rt_nodes->rn_key = (caddr_t)&rt->rt_dst_sock;
1701 	rt->rt_dst = dst;
1702 	rt->rt_dst_sock.sin_family = AF_INET;
1703 #ifdef _HAVE_SIN_LEN
1704 	rt->rt_dst_sock.sin_len = dst_sock.sin_len;
1705 #endif
1706 	if (mask != HOST_MASK) {
1707 		smask = std_mask(dst);
1708 		if ((smask & ~mask) == 0 && mask > smask)
1709 			state |= RS_SUBNET;
1710 	}
1711 	mask_sock.sin_addr.s_addr = htonl(mask);
1712 	masktrim(&mask_sock);
1713 	rt->rt_mask = mask;
1714 	rt->rt_state = state;
1715 	rt->rt_spares[0] = *new;
1716 	rt->rt_time = now.tv_sec;
1717 	rt->rt_poison_metric = HOPCNT_INFINITY;
1718 	rt->rt_seqno = update_seqno;
1719 
1720 	if (++total_routes == MAX_ROUTES)
1721 		msglog("have maximum (%d) routes", total_routes);
1722 	if (TRACEACTIONS)
1723 		trace_add_del("Add", rt);
1724 
1725 	need_kern.tv_sec = now.tv_sec;
1726 	set_need_flash();
1727 
1728 	if (0 == rhead->rnh_addaddr(&rt->rt_dst_sock, &mask_sock,
1729 				    rhead, rt->rt_nodes)) {
1730 		msglog("rnh_addaddr() failed for %s mask=%#lx",
1731 		       naddr_ntoa(dst), (u_long)mask);
1732 		free(rt);
1733 	}
1734 }
1735 
1736 
1737 /* notice a changed route
1738  */
1739 void
1740 rtchange(struct rt_entry *rt,
1741 	 u_int	state,			/* new state bits */
1742 	 struct rt_spare *new,
1743 	 char	*label)
1744 {
1745 	if (rt->rt_metric != new->rts_metric) {
1746 		/* Fix the kernel immediately if it seems the route
1747 		 * has gone bad, since there may be a working route that
1748 		 * aggregates this route.
1749 		 */
1750 		if (new->rts_metric == HOPCNT_INFINITY) {
1751 			need_kern.tv_sec = now.tv_sec;
1752 			if (new->rts_time >= now.tv_sec - EXPIRE_TIME)
1753 				new->rts_time = now.tv_sec - EXPIRE_TIME;
1754 		}
1755 		rt->rt_seqno = update_seqno;
1756 		set_need_flash();
1757 	}
1758 
1759 	if (rt->rt_gate != new->rts_gate) {
1760 		need_kern.tv_sec = now.tv_sec;
1761 		rt->rt_seqno = update_seqno;
1762 		set_need_flash();
1763 	}
1764 
1765 	state |= (rt->rt_state & RS_SUBNET);
1766 
1767 	/* Keep various things from deciding ageless routes are stale.
1768 	 */
1769 	if (!AGE_RT(state, new->rts_ifp))
1770 		new->rts_time = now.tv_sec;
1771 
1772 	if (TRACEACTIONS)
1773 		trace_change(rt, state, new,
1774 			     label ? label : "Chg   ");
1775 
1776 	rt->rt_state = state;
1777 	rt->rt_spares[0] = *new;
1778 }
1779 
1780 
1781 /* check for a better route among the spares
1782  */
1783 static struct rt_spare *
1784 rts_better(struct rt_entry *rt)
1785 {
1786 	struct rt_spare *rts, *rts1;
1787 	int i;
1788 
1789 	/* find the best alternative among the spares */
1790 	rts = rt->rt_spares+1;
1791 	for (i = NUM_SPARES, rts1 = rts+1; i > 2; i--, rts1++) {
1792 		if (BETTER_LINK(rt,rts1,rts))
1793 			rts = rts1;
1794 	}
1795 
1796 	return rts;
1797 }
1798 
1799 
1800 /* switch to a backup route
1801  */
1802 void
1803 rtswitch(struct rt_entry *rt,
1804 	 struct rt_spare *rts)
1805 {
1806 	struct rt_spare swap;
1807 	char label[10];
1808 
1809 
1810 	/* Do not change permanent routes */
1811 	if (0 != (rt->rt_state & (RS_MHOME | RS_STATIC | RS_RDISC
1812 				  | RS_NET_SYN | RS_IF)))
1813 		return;
1814 
1815 	/* find the best alternative among the spares */
1816 	if (rts == NULL)
1817 		rts = rts_better(rt);
1818 
1819 	/* Do not bother if it is not worthwhile.
1820 	 */
1821 	if (!BETTER_LINK(rt, rts, rt->rt_spares))
1822 		return;
1823 
1824 	swap = rt->rt_spares[0];
1825 	sprintf(label, "Use #%d", (int)(rts - rt->rt_spares));
1826 	rtchange(rt, rt->rt_state & ~(RS_NET_SYN | RS_RDISC), rts, label);
1827 	if (swap.rts_metric == HOPCNT_INFINITY) {
1828 		*rts = rts_empty;
1829 	} else {
1830 		*rts = swap;
1831 	}
1832 }
1833 
1834 
1835 void
1836 rtdelete(struct rt_entry *rt)
1837 {
1838 	struct khash *k;
1839 
1840 
1841 	if (TRACEACTIONS)
1842 		trace_add_del("Del", rt);
1843 
1844 	k = kern_find(rt->rt_dst, rt->rt_mask, 0);
1845 	if (k != NULL) {
1846 		k->k_state |= KS_DELETE;
1847 		need_kern.tv_sec = now.tv_sec;
1848 	}
1849 
1850 	dst_sock.sin_addr.s_addr = rt->rt_dst;
1851 	mask_sock.sin_addr.s_addr = htonl(rt->rt_mask);
1852 	masktrim(&mask_sock);
1853 	if (rt != (struct rt_entry *)rhead->rnh_deladdr(&dst_sock, &mask_sock,
1854 							rhead)) {
1855 		msglog("rnh_deladdr() failed");
1856 	} else {
1857 		free(rt);
1858 		total_routes--;
1859 	}
1860 }
1861 
1862 
1863 void
1864 rts_delete(struct rt_entry *rt,
1865 	   struct rt_spare *rts)
1866 {
1867 	trace_upslot(rt, rts, &rts_empty);
1868 	*rts = rts_empty;
1869 }
1870 
1871 
1872 /* Get rid of a bad route, and try to switch to a replacement.
1873  */
1874 void
1875 rtbad(struct rt_entry *rt)
1876 {
1877 	struct rt_spare new;
1878 
1879 	/* Poison the route */
1880 	new = rt->rt_spares[0];
1881 	new.rts_metric = HOPCNT_INFINITY;
1882 	rtchange(rt, rt->rt_state & ~(RS_IF | RS_LOCAL | RS_STATIC), &new, 0);
1883 	rtswitch(rt, 0);
1884 }
1885 
1886 
1887 /* Junk a RS_NET_SYN or RS_LOCAL route,
1888  *	unless it is needed by another interface.
1889  */
1890 void
1891 rtbad_sub(struct rt_entry *rt)
1892 {
1893 	struct interface *ifp, *ifp1;
1894 	struct intnet *intnetp;
1895 	u_int state;
1896 
1897 
1898 	ifp1 = NULL;
1899 	state = 0;
1900 
1901 	if (rt->rt_state & RS_LOCAL) {
1902 		/* Is this the route through loopback for the interface?
1903 		 * If so, see if it is used by any other interfaces, such
1904 		 * as a point-to-point interface with the same local address.
1905 		 */
1906 		for (ifp = ifnet; ifp != NULL; ifp = ifp->int_next) {
1907 			/* Retain it if another interface needs it.
1908 			 */
1909 			if (ifp->int_addr == rt->rt_ifp->int_addr) {
1910 				state |= RS_LOCAL;
1911 				ifp1 = ifp;
1912 				break;
1913 			}
1914 		}
1915 
1916 	}
1917 
1918 	if (!(state & RS_LOCAL)) {
1919 		/* Retain RIPv1 logical network route if there is another
1920 		 * interface that justifies it.
1921 		 */
1922 		if (rt->rt_state & RS_NET_SYN) {
1923 			for (ifp = ifnet; ifp != NULL; ifp = ifp->int_next) {
1924 				if ((ifp->int_state & IS_NEED_NET_SYN)
1925 				    && rt->rt_mask == ifp->int_std_mask
1926 				    && rt->rt_dst == ifp->int_std_addr) {
1927 					state |= RS_NET_SYN;
1928 					ifp1 = ifp;
1929 					break;
1930 				}
1931 			}
1932 		}
1933 
1934 		/* or if there is an authority route that needs it. */
1935 		for (intnetp = intnets;
1936 		     intnetp != NULL;
1937 		     intnetp = intnetp->intnet_next) {
1938 			if (intnetp->intnet_addr == rt->rt_dst
1939 			    && intnetp->intnet_mask == rt->rt_mask) {
1940 				state |= (RS_NET_SYN | RS_NET_INT);
1941 				break;
1942 			}
1943 		}
1944 	}
1945 
1946 	if (ifp1 != NULL || (state & RS_NET_SYN)) {
1947 		struct rt_spare new = rt->rt_spares[0];
1948 		new.rts_ifp = ifp1;
1949 		rtchange(rt, ((rt->rt_state & ~(RS_NET_SYN|RS_LOCAL)) | state),
1950 			 &new, 0);
1951 	} else {
1952 		rtbad(rt);
1953 	}
1954 }
1955 
1956 
1957 /* Called while walking the table looking for sick interfaces
1958  * or after a time change.
1959  */
1960 /* ARGSUSED */
1961 int
1962 walk_bad(struct radix_node *rn,
1963 	 struct walkarg *argp UNUSED)
1964 {
1965 #define RT ((struct rt_entry *)rn)
1966 	struct rt_spare *rts;
1967 	int i;
1968 
1969 
1970 	/* fix any spare routes through the interface
1971 	 */
1972 	rts = RT->rt_spares;
1973 	for (i = NUM_SPARES; i != 1; i--) {
1974 		rts++;
1975 		if (rts->rts_metric < HOPCNT_INFINITY
1976 		    && (rts->rts_ifp == 0
1977 			|| (rts->rts_ifp->int_state & IS_BROKE)))
1978 			rts_delete(RT, rts);
1979 	}
1980 
1981 	/* Deal with the main route
1982 	 */
1983 	/* finished if it has been handled before or if its interface is ok
1984 	 */
1985 	if (RT->rt_ifp == 0 || !(RT->rt_ifp->int_state & IS_BROKE))
1986 		return 0;
1987 
1988 	/* Bad routes for other than interfaces are easy.
1989 	 */
1990 	if (0 == (RT->rt_state & (RS_IF | RS_NET_SYN | RS_LOCAL))) {
1991 		rtbad(RT);
1992 		return 0;
1993 	}
1994 
1995 	rtbad_sub(RT);
1996 	return 0;
1997 #undef RT
1998 }
1999 
2000 
2001 /* Check the age of an individual route.
2002  */
2003 /* ARGSUSED */
2004 static int
2005 walk_age(struct radix_node *rn,
2006 	   struct walkarg *argp UNUSED)
2007 {
2008 #define RT ((struct rt_entry *)rn)
2009 	struct interface *ifp;
2010 	struct rt_spare *rts;
2011 	int i;
2012 
2013 
2014 	/* age all of the spare routes, including the primary route
2015 	 * currently in use
2016 	 */
2017 	rts = RT->rt_spares;
2018 	for (i = NUM_SPARES; i != 0; i--, rts++) {
2019 
2020 		ifp = rts->rts_ifp;
2021 		if (i == NUM_SPARES) {
2022 			if (!AGE_RT(RT->rt_state, ifp)) {
2023 				/* Keep various things from deciding ageless
2024 				 * routes are stale
2025 				 */
2026 				rts->rts_time = now.tv_sec;
2027 				continue;
2028 			}
2029 
2030 			/* forget RIP routes after RIP has been turned off.
2031 			 */
2032 			if (rip_sock < 0) {
2033 				rtdelete(RT);
2034 				return 0;
2035 			}
2036 		}
2037 
2038 		/* age failing routes
2039 		 */
2040 		if (age_bad_gate == rts->rts_gate
2041 		    && rts->rts_time >= now_stale) {
2042 			rts->rts_time -= SUPPLY_INTERVAL;
2043 		}
2044 
2045 		/* trash the spare routes when they go bad */
2046 		if (rts->rts_metric < HOPCNT_INFINITY
2047 		    && now_garbage > rts->rts_time
2048 		    && i != NUM_SPARES)
2049 			rts_delete(RT, rts);
2050 	}
2051 
2052 
2053 	/* finished if the active route is still fresh */
2054 	if (now_stale <= RT->rt_time)
2055 		return 0;
2056 
2057 	/* try to switch to an alternative */
2058 	rtswitch(RT, 0);
2059 
2060 	/* Delete a dead route after it has been publically mourned. */
2061 	if (now_garbage > RT->rt_time) {
2062 		rtdelete(RT);
2063 		return 0;
2064 	}
2065 
2066 	/* Start poisoning a bad route before deleting it. */
2067 	if (now.tv_sec - RT->rt_time > EXPIRE_TIME) {
2068 		struct rt_spare new = RT->rt_spares[0];
2069 		new.rts_metric = HOPCNT_INFINITY;
2070 		rtchange(RT, RT->rt_state, &new, 0);
2071 	}
2072 	return 0;
2073 }
2074 
2075 
2076 /* Watch for dead routes and interfaces.
2077  */
2078 void
2079 age(naddr bad_gate)
2080 {
2081 	struct interface *ifp;
2082 	int need_query = 0;
2083 
2084 	/* If not listening to RIP, there is no need to age the routes in
2085 	 * the table.
2086 	 */
2087 	age_timer.tv_sec = (now.tv_sec
2088 			    + ((rip_sock < 0) ? NEVER : SUPPLY_INTERVAL));
2089 
2090 	/* Check for dead IS_REMOTE interfaces by timing their
2091 	 * transmissions.
2092 	 */
2093 	for (ifp = ifnet; ifp; ifp = ifp->int_next) {
2094 		if (!(ifp->int_state & IS_REMOTE))
2095 			continue;
2096 
2097 		/* ignore unreachable remote interfaces */
2098 		if (!check_remote(ifp))
2099 			continue;
2100 
2101 		/* Restore remote interface that has become reachable
2102 		 */
2103 		if (ifp->int_state & IS_BROKE)
2104 			if_ok(ifp, "remote ");
2105 
2106 		if (ifp->int_act_time != NEVER
2107 		    && now.tv_sec - ifp->int_act_time > EXPIRE_TIME) {
2108 			msglog("remote interface %s to %s timed out after"
2109 			       " %ld:%ld",
2110 			       ifp->int_name,
2111 			       naddr_ntoa(ifp->int_dstaddr),
2112 			       (now.tv_sec - ifp->int_act_time)/60,
2113 			       (now.tv_sec - ifp->int_act_time)%60);
2114 			if_sick(ifp);
2115 		}
2116 
2117 		/* If we have not heard from the other router
2118 		 * recently, ask it.
2119 		 */
2120 		if (now.tv_sec >= ifp->int_query_time) {
2121 			ifp->int_query_time = NEVER;
2122 			need_query = 1;
2123 		}
2124 	}
2125 
2126 	/* Age routes. */
2127 	age_bad_gate = bad_gate;
2128 	rn_walktree(rhead, walk_age, 0);
2129 
2130 	/* delete old redirected routes to keep the kernel table small
2131 	 * and prevent blackholes
2132 	 */
2133 	del_redirects(bad_gate, now.tv_sec-STALE_TIME);
2134 
2135 	/* Update the kernel routing table. */
2136 	fix_kern();
2137 
2138 	/* poke reticent remote gateways */
2139 	if (need_query)
2140 		rip_query();
2141 }
2142