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