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