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