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