table.c revision 1.11 1 /* $NetBSD: table.c,v 1.11 1999/11/19 10:46:35 bouyer Exp $ */
2
3 /*
4 * Copyright (c) 1983, 1988, 1993
5 * The Regents of the University of California. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgment:
17 * This product includes software developed by the University of
18 * California, Berkeley and its contributors.
19 * 4. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
34 */
35
36 #if !defined(lint) && !defined(sgi) && !defined(__NetBSD__)
37 static char sccsid[] __attribute__((unused)) = "@(#)tables.c 8.1 (Berkeley) 6/5/93";
38 #elif defined(__NetBSD__)
39 #include <sys/cdefs.h>
40 __RCSID("$NetBSD: table.c,v 1.11 1999/11/19 10:46:35 bouyer Exp $");
41 #endif
42
43 #include "defs.h"
44
45 static struct rt_spare *rts_better(struct rt_entry *);
46 static struct rt_spare rts_empty = {0,0,0,HOPCNT_INFINITY,0,0,0};
47
48 void set_need_flash(void);
49 #ifdef _HAVE_SIN_LEN
50 void masktrim(struct sockaddr_in *ap);
51 #else
52 void masktrim(struct sockaddr_in_new *ap);
53 #endif
54
55 struct radix_node_head *rhead; /* root of the radix tree */
56
57 int need_flash = 1; /* flash update needed
58 * start =1 to suppress the 1st
59 */
60
61 struct timeval age_timer; /* next check of old routes */
62 struct timeval need_kern = { /* need to update kernel table */
63 EPOCH+MIN_WAITTIME-1
64 };
65
66 int stopint;
67
68 int total_routes;
69
70 /* zap any old routes through this gateway */
71 naddr 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
98 struct 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 */
121 static void
122 ag_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
173 static void
174 ag_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 */
202 void
203 ag_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 */
285 void
286 ag_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 static const char *
621 rtm_type_name(u_char type)
622 {
623 static const char *rtm_types[] = {
624 "RTM_ADD",
625 "RTM_DELETE",
626 "RTM_CHANGE",
627 "RTM_GET",
628 "RTM_LOSING",
629 "RTM_REDIRECT",
630 "RTM_MISS",
631 "RTM_LOCK",
632 "RTM_OLDADD",
633 "RTM_OLDDEL",
634 "RTM_RESOLVE",
635 "RTM_NEWADDR",
636 "RTM_DELADDR",
637 #ifdef RTM_OIFINFO
638 "RTM_OIFINFO",
639 #endif
640 "RTM_IFINFO"
641 };
642 static char name0[10];
643
644
645 if (type > sizeof(rtm_types)/sizeof(rtm_types[0])
646 || type == 0) {
647 sprintf(name0, "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 */
659 void
660 #ifdef _HAVE_SIN_LEN
661 masktrim(struct sockaddr_in *ap)
662 #else
663 masktrim(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 */
681 static void
682 rtioctl(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
703 again:
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]
766 static 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
789 static struct khash*
790 kern_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
804 static struct khash*
805 kern_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 */
829 static void
830 kern_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 */
858 static void
859 kern_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 */
888 static void
889 rtm_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 */
992 static void
993 rtm_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 */
1013 static int
1014 get_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 */
1045 void
1046 flush_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 (long)rtm, (long)sysctl_buf, (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 */
1150 void
1151 read_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 #ifdef RTM_OIFINFO
1225 if (m.r.rtm.rtm_type == RTM_OIFINFO) {
1226 continue; /* ignore compat message */
1227 }
1228 #endif
1229
1230 strcpy(str, rtm_type_name(m.r.rtm.rtm_type));
1231 strp = &str[strlen(str)];
1232 if (m.r.rtm.rtm_type <= RTM_CHANGE)
1233 strp += sprintf(strp," from pid %d",m.r.rtm.rtm_pid);
1234
1235 rt_xaddrs(&info, m.r.addrs, &m.r.addrs[RTAX_MAX],
1236 m.r.rtm.rtm_addrs);
1237
1238 if (INFO_DST(&info) == 0) {
1239 trace_act("ignore %s without dst", str);
1240 continue;
1241 }
1242
1243 if (INFO_DST(&info)->sa_family != AF_INET) {
1244 trace_act("ignore %s for AF %d", str,
1245 INFO_DST(&info)->sa_family);
1246 continue;
1247 }
1248
1249 mask = ((INFO_MASK(&info) != 0)
1250 ? ntohl(S_ADDR(INFO_MASK(&info)))
1251 : (m.r.rtm.rtm_flags & RTF_HOST)
1252 ? HOST_MASK
1253 : std_mask(S_ADDR(INFO_DST(&info))));
1254
1255 strp += sprintf(strp, ": %s",
1256 addrname(S_ADDR(INFO_DST(&info)), mask, 0));
1257
1258 if (IN_MULTICAST(ntohl(S_ADDR(INFO_DST(&info))))) {
1259 trace_act("ignore multicast %s", str);
1260 continue;
1261 }
1262
1263 if (m.r.rtm.rtm_flags & RTF_LLINFO) {
1264 trace_act("ignore ARP %s", str);
1265 continue;
1266 }
1267
1268 if (get_info_gate(&INFO_GATE(&info), &gate_sin)) {
1269 gate = S_ADDR(INFO_GATE(&info));
1270 strp += sprintf(strp, " --> %s", naddr_ntoa(gate));
1271 } else {
1272 gate = 0;
1273 }
1274
1275 if (INFO_AUTHOR(&info) != 0)
1276 strp += sprintf(strp, " by authority of %s",
1277 saddr_ntoa(INFO_AUTHOR(&info)));
1278
1279 switch (m.r.rtm.rtm_type) {
1280 case RTM_ADD:
1281 case RTM_CHANGE:
1282 case RTM_REDIRECT:
1283 if (m.r.rtm.rtm_errno != 0) {
1284 trace_act("ignore %s with \"%s\" error",
1285 str, strerror(m.r.rtm.rtm_errno));
1286 } else {
1287 trace_act("%s", str);
1288 rtm_add(&m.r.rtm,&info,0);
1289 }
1290 break;
1291
1292 case RTM_DELETE:
1293 if (m.r.rtm.rtm_errno != 0
1294 && m.r.rtm.rtm_errno != ESRCH) {
1295 trace_act("ignore %s with \"%s\" error",
1296 str, strerror(m.r.rtm.rtm_errno));
1297 } else {
1298 trace_act("%s", str);
1299 del_static(S_ADDR(INFO_DST(&info)), mask,
1300 gate, 1);
1301 }
1302 break;
1303
1304 case RTM_LOSING:
1305 trace_act("%s", str);
1306 rtm_lose(&m.r.rtm,&info);
1307 break;
1308
1309 default:
1310 trace_act("ignore %s", str);
1311 break;
1312 }
1313 }
1314 }
1315
1316
1317 /* after aggregating, note routes that belong in the kernel
1318 */
1319 static void
1320 kern_out(struct ag_info *ag)
1321 {
1322 struct khash *k;
1323
1324
1325 /* Do not install bad routes if they are not already present.
1326 * This includes routes that had RS_NET_SYN for interfaces that
1327 * recently died.
1328 */
1329 if (ag->ag_metric == HOPCNT_INFINITY) {
1330 k = kern_find(htonl(ag->ag_dst_h), ag->ag_mask, 0);
1331 if (k == 0)
1332 return;
1333 } else {
1334 k = kern_add(htonl(ag->ag_dst_h), ag->ag_mask);
1335 }
1336
1337 if (k->k_state & KS_NEW) {
1338 /* will need to add new entry to the kernel table */
1339 k->k_state = KS_ADD;
1340 if (ag->ag_state & AGS_GATEWAY)
1341 k->k_state |= KS_GATEWAY;
1342 k->k_gate = ag->ag_gate;
1343 k->k_metric = ag->ag_metric;
1344 return;
1345 }
1346
1347 if (k->k_state & KS_STATIC)
1348 return;
1349
1350 /* modify existing kernel entry if necessary */
1351 if (k->k_gate != ag->ag_gate
1352 || k->k_metric != ag->ag_metric) {
1353 /* Must delete bad interface routes etc. to change them. */
1354 if (k->k_metric == HOPCNT_INFINITY)
1355 k->k_state |= KS_DEL_ADD;
1356 k->k_gate = ag->ag_gate;
1357 k->k_metric = ag->ag_metric;
1358 k->k_state |= KS_CHANGE;
1359 }
1360
1361 /* If the daemon thinks the route should exist, forget
1362 * about any redirections.
1363 * If the daemon thinks the route should exist, eventually
1364 * override manual intervention by the operator.
1365 */
1366 if ((k->k_state & (KS_DYNAMIC | KS_DELETED)) != 0) {
1367 k->k_state &= ~KS_DYNAMIC;
1368 k->k_state |= (KS_ADD | KS_DEL_ADD);
1369 }
1370
1371 if ((k->k_state & KS_GATEWAY)
1372 && !(ag->ag_state & AGS_GATEWAY)) {
1373 k->k_state &= ~KS_GATEWAY;
1374 k->k_state |= (KS_ADD | KS_DEL_ADD);
1375 } else if (!(k->k_state & KS_GATEWAY)
1376 && (ag->ag_state & AGS_GATEWAY)) {
1377 k->k_state |= KS_GATEWAY;
1378 k->k_state |= (KS_ADD | KS_DEL_ADD);
1379 }
1380
1381 /* Deleting-and-adding is necessary to change aspects of a route.
1382 * Just delete instead of deleting and then adding a bad route.
1383 * Otherwise, we want to keep the route in the kernel.
1384 */
1385 if (k->k_metric == HOPCNT_INFINITY
1386 && (k->k_state & KS_DEL_ADD))
1387 k->k_state |= KS_DELETE;
1388 else
1389 k->k_state &= ~KS_DELETE;
1390 #undef RT
1391 }
1392
1393
1394 /* ARGSUSED */
1395 static int
1396 walk_kern(struct radix_node *rn,
1397 struct walkarg *argp UNUSED)
1398 {
1399 #define RT ((struct rt_entry *)rn)
1400 char metric, pref;
1401 u_int ags = 0;
1402
1403
1404 /* Do not install synthetic routes */
1405 if (RT->rt_state & RS_NET_SYN)
1406 return 0;
1407
1408 if (!(RT->rt_state & RS_IF)) {
1409 /* This is an ordinary route, not for an interface.
1410 */
1411
1412 /* aggregate, ordinary good routes without regard to
1413 * their metric
1414 */
1415 pref = 1;
1416 ags |= (AGS_GATEWAY | AGS_SUPPRESS | AGS_AGGREGATE);
1417
1418 /* Do not install host routes directly to hosts, to avoid
1419 * interfering with ARP entries in the kernel table.
1420 */
1421 if (RT_ISHOST(RT)
1422 && ntohl(RT->rt_dst) == RT->rt_gate)
1423 return 0;
1424
1425 } else {
1426 /* This is an interface route.
1427 * Do not install routes for "external" remote interfaces.
1428 */
1429 if (RT->rt_ifp != 0 && (RT->rt_ifp->int_state & IS_EXTERNAL))
1430 return 0;
1431
1432 /* Interfaces should override received routes.
1433 */
1434 pref = 0;
1435 ags |= (AGS_IF | AGS_CORS_GATE);
1436
1437 /* If it is not an interface, or an alias for an interface,
1438 * it must be a "gateway."
1439 *
1440 * If it is a "remote" interface, it is also a "gateway" to
1441 * the kernel if is not a alias.
1442 */
1443 if (RT->rt_ifp == 0
1444 || (RT->rt_ifp->int_state & IS_REMOTE))
1445 ags |= (AGS_GATEWAY | AGS_SUPPRESS | AGS_AGGREGATE);
1446 }
1447
1448 /* If RIP is off and IRDP is on, let the route to the discovered
1449 * route suppress any RIP routes. Eventually the RIP routes
1450 * will time-out and be deleted. This reaches the steady-state
1451 * quicker.
1452 */
1453 if ((RT->rt_state & RS_RDISC) && rip_sock < 0)
1454 ags |= AGS_CORS_GATE;
1455
1456 metric = RT->rt_metric;
1457 if (metric == HOPCNT_INFINITY) {
1458 /* if the route is dead, so try hard to aggregate. */
1459 pref = HOPCNT_INFINITY;
1460 ags |= (AGS_FINE_GATE | AGS_SUPPRESS);
1461 ags &= ~(AGS_IF | AGS_CORS_GATE);
1462 }
1463
1464 ag_check(RT->rt_dst, RT->rt_mask, RT->rt_gate, 0,
1465 metric,pref, 0, 0, ags, kern_out);
1466 return 0;
1467 #undef RT
1468 }
1469
1470
1471 /* Update the kernel table to match the daemon table.
1472 */
1473 static void
1474 fix_kern(void)
1475 {
1476 int i;
1477 struct khash *k, **pk;
1478
1479
1480 need_kern = age_timer;
1481
1482 /* Walk daemon table, updating the copy of the kernel table.
1483 */
1484 (void)rn_walktree(rhead, walk_kern, 0);
1485 ag_flush(0,0,kern_out);
1486
1487 for (i = 0; i < KHASH_SIZE; i++) {
1488 for (pk = &khash_bins[i]; (k = *pk) != 0; ) {
1489 /* Do not touch static routes */
1490 if (k->k_state & KS_STATIC) {
1491 kern_check_static(k,0);
1492 pk = &k->k_next;
1493 continue;
1494 }
1495
1496 /* check hold on routes deleted by the operator */
1497 if (k->k_keep > now.tv_sec) {
1498 /* ensure we check when the hold is over */
1499 LIM_SEC(need_kern, k->k_keep);
1500 /* mark for the next cycle */
1501 k->k_state |= KS_DELETE;
1502 pk = &k->k_next;
1503 continue;
1504 }
1505
1506 if ((k->k_state & KS_DELETE)
1507 && !(k->k_state & KS_DYNAMIC)) {
1508 kern_ioctl(k, RTM_DELETE, 0);
1509 *pk = k->k_next;
1510 free(k);
1511 continue;
1512 }
1513
1514 if (k->k_state & KS_DEL_ADD)
1515 kern_ioctl(k, RTM_DELETE, 0);
1516
1517 if (k->k_state & KS_ADD) {
1518 kern_ioctl(k, RTM_ADD,
1519 ((0 != (k->k_state & (KS_GATEWAY
1520 | KS_DYNAMIC)))
1521 ? RTF_GATEWAY : 0));
1522 } else if (k->k_state & KS_CHANGE) {
1523 kern_ioctl(k, RTM_CHANGE,
1524 ((0 != (k->k_state & (KS_GATEWAY
1525 | KS_DYNAMIC)))
1526 ? RTF_GATEWAY : 0));
1527 }
1528 k->k_state &= ~(KS_ADD|KS_CHANGE|KS_DEL_ADD);
1529
1530 /* Mark this route to be deleted in the next cycle.
1531 * This deletes routes that disappear from the
1532 * daemon table, since the normal aging code
1533 * will clear the bit for routes that have not
1534 * disappeared from the daemon table.
1535 */
1536 k->k_state |= KS_DELETE;
1537 pk = &k->k_next;
1538 }
1539 }
1540 }
1541
1542
1543 /* Delete a static route in the image of the kernel table.
1544 */
1545 void
1546 del_static(naddr dst,
1547 naddr mask,
1548 naddr gate,
1549 int gone)
1550 {
1551 struct khash *k;
1552 struct rt_entry *rt;
1553
1554 /* Just mark it in the table to be deleted next time the kernel
1555 * table is updated.
1556 * If it has already been deleted, mark it as such, and set its
1557 * keep-timer so that it will not be deleted again for a while.
1558 * This lets the operator delete a route added by the daemon
1559 * and add a replacement.
1560 */
1561 k = kern_find(dst, mask, 0);
1562 if (k != 0 && (gate == 0 || k->k_gate == gate)) {
1563 k->k_state &= ~(KS_STATIC | KS_DYNAMIC | KS_CHECK);
1564 k->k_state |= KS_DELETE;
1565 if (gone) {
1566 k->k_state |= KS_DELETED;
1567 k->k_keep = now.tv_sec + K_KEEP_LIM;
1568 }
1569 }
1570
1571 rt = rtget(dst, mask);
1572 if (rt != 0 && (rt->rt_state & RS_STATIC))
1573 rtbad(rt);
1574 }
1575
1576
1577 /* Delete all routes generated from ICMP Redirects that use a given gateway,
1578 * as well as old redirected routes.
1579 */
1580 void
1581 del_redirects(naddr bad_gate,
1582 time_t old)
1583 {
1584 int i;
1585 struct khash *k;
1586
1587
1588 for (i = 0; i < KHASH_SIZE; i++) {
1589 for (k = khash_bins[i]; k != 0; k = k->k_next) {
1590 if (!(k->k_state & KS_DYNAMIC)
1591 || (k->k_state & KS_STATIC))
1592 continue;
1593
1594 if (k->k_gate != bad_gate
1595 && k->k_redirect_time > old
1596 && !supplier)
1597 continue;
1598
1599 k->k_state |= KS_DELETE;
1600 k->k_state &= ~KS_DYNAMIC;
1601 need_kern.tv_sec = now.tv_sec;
1602 trace_act("mark redirected %s --> %s for deletion",
1603 addrname(k->k_dst, k->k_mask, 0),
1604 naddr_ntoa(k->k_gate));
1605 }
1606 }
1607 }
1608
1609
1610 /* Start the daemon tables.
1611 */
1612 extern int max_keylen;
1613
1614 void
1615 rtinit(void)
1616 {
1617 int i;
1618 struct ag_info *ag;
1619
1620 /* Initialize the radix trees */
1621 max_keylen = sizeof(struct sockaddr_in);
1622 rn_init();
1623 rn_inithead((void**)&rhead, 32);
1624
1625 /* mark all of the slots in the table free */
1626 ag_avail = ag_slots;
1627 for (ag = ag_slots, i = 1; i < NUM_AG_SLOTS; i++) {
1628 ag->ag_fine = ag+1;
1629 ag++;
1630 }
1631 }
1632
1633
1634 #ifdef _HAVE_SIN_LEN
1635 static struct sockaddr_in dst_sock = {sizeof(dst_sock), AF_INET};
1636 static struct sockaddr_in mask_sock = {sizeof(mask_sock), AF_INET};
1637 #else
1638 static struct sockaddr_in_new dst_sock = {_SIN_ADDR_SIZE, AF_INET};
1639 static struct sockaddr_in_new mask_sock = {_SIN_ADDR_SIZE, AF_INET};
1640 #endif
1641
1642
1643 void
1644 set_need_flash(void)
1645 {
1646 if (!need_flash) {
1647 need_flash = 1;
1648 /* Do not send the flash update immediately. Wait a little
1649 * while to hear from other routers.
1650 */
1651 no_flash.tv_sec = now.tv_sec + MIN_WAITTIME;
1652 }
1653 }
1654
1655
1656 /* Get a particular routing table entry
1657 */
1658 struct rt_entry *
1659 rtget(naddr dst, naddr mask)
1660 {
1661 struct rt_entry *rt;
1662
1663 dst_sock.sin_addr.s_addr = dst;
1664 mask_sock.sin_addr.s_addr = mask;
1665 masktrim(&mask_sock);
1666 rt = (struct rt_entry *)rhead->rnh_lookup(&dst_sock,&mask_sock,rhead);
1667 if (!rt
1668 || rt->rt_dst != dst
1669 || rt->rt_mask != mask)
1670 return 0;
1671
1672 return rt;
1673 }
1674
1675
1676 /* Find a route to dst as the kernel would.
1677 */
1678 struct rt_entry *
1679 rtfind(naddr dst)
1680 {
1681 dst_sock.sin_addr.s_addr = dst;
1682 return (struct rt_entry *)rhead->rnh_matchaddr(&dst_sock, rhead);
1683 }
1684
1685
1686 /* add a route to the table
1687 */
1688 void
1689 rtadd(naddr dst,
1690 naddr mask,
1691 u_int state, /* rt_state for the entry */
1692 struct rt_spare *new)
1693 {
1694 struct rt_entry *rt;
1695 naddr smask;
1696 int i;
1697 struct rt_spare *rts;
1698
1699 rt = (struct rt_entry *)rtmalloc(sizeof (*rt), "rtadd");
1700 memset(rt, 0, sizeof(*rt));
1701 for (rts = rt->rt_spares, i = NUM_SPARES; i != 0; i--, rts++)
1702 rts->rts_metric = HOPCNT_INFINITY;
1703
1704 rt->rt_nodes->rn_key = (caddr_t)&rt->rt_dst_sock;
1705 rt->rt_dst = dst;
1706 rt->rt_dst_sock.sin_family = AF_INET;
1707 #ifdef _HAVE_SIN_LEN
1708 rt->rt_dst_sock.sin_len = dst_sock.sin_len;
1709 #endif
1710 if (mask != HOST_MASK) {
1711 smask = std_mask(dst);
1712 if ((smask & ~mask) == 0 && mask > smask)
1713 state |= RS_SUBNET;
1714 }
1715 mask_sock.sin_addr.s_addr = mask;
1716 masktrim(&mask_sock);
1717 rt->rt_mask = mask;
1718 rt->rt_state = state;
1719 rt->rt_spares[0] = *new;
1720 rt->rt_time = now.tv_sec;
1721 rt->rt_poison_metric = HOPCNT_INFINITY;
1722 rt->rt_seqno = update_seqno;
1723
1724 if (++total_routes == MAX_ROUTES)
1725 msglog("have maximum (%d) routes", total_routes);
1726 if (TRACEACTIONS)
1727 trace_add_del("Add", rt);
1728
1729 need_kern.tv_sec = now.tv_sec;
1730 set_need_flash();
1731
1732 if (0 == rhead->rnh_addaddr(&rt->rt_dst_sock, &mask_sock,
1733 rhead, rt->rt_nodes)) {
1734 msglog("rnh_addaddr() failed for %s mask=%#lx",
1735 naddr_ntoa(dst), (u_long)mask);
1736 }
1737 }
1738
1739
1740 /* notice a changed route
1741 */
1742 void
1743 rtchange(struct rt_entry *rt,
1744 u_int state, /* new state bits */
1745 struct rt_spare *new,
1746 char *label)
1747 {
1748 if (rt->rt_metric != new->rts_metric) {
1749 /* Fix the kernel immediately if it seems the route
1750 * has gone bad, since there may be a working route that
1751 * aggregates this route.
1752 */
1753 if (new->rts_metric == HOPCNT_INFINITY) {
1754 need_kern.tv_sec = now.tv_sec;
1755 if (new->rts_time >= now.tv_sec - EXPIRE_TIME)
1756 new->rts_time = now.tv_sec - EXPIRE_TIME;
1757 }
1758 rt->rt_seqno = update_seqno;
1759 set_need_flash();
1760 }
1761
1762 if (rt->rt_gate != new->rts_gate) {
1763 need_kern.tv_sec = now.tv_sec;
1764 rt->rt_seqno = update_seqno;
1765 set_need_flash();
1766 }
1767
1768 state |= (rt->rt_state & RS_SUBNET);
1769
1770 /* Keep various things from deciding ageless routes are stale.
1771 */
1772 if (!AGE_RT(state, new->rts_ifp))
1773 new->rts_time = now.tv_sec;
1774
1775 if (TRACEACTIONS)
1776 trace_change(rt, state, new,
1777 label ? label : "Chg ");
1778
1779 rt->rt_state = state;
1780 rt->rt_spares[0] = *new;
1781 }
1782
1783
1784 /* check for a better route among the spares
1785 */
1786 static struct rt_spare *
1787 rts_better(struct rt_entry *rt)
1788 {
1789 struct rt_spare *rts, *rts1;
1790 int i;
1791
1792 /* find the best alternative among the spares */
1793 rts = rt->rt_spares+1;
1794 for (i = NUM_SPARES, rts1 = rts+1; i > 2; i--, rts1++) {
1795 if (BETTER_LINK(rt,rts1,rts))
1796 rts = rts1;
1797 }
1798
1799 return rts;
1800 }
1801
1802
1803 /* switch to a backup route
1804 */
1805 void
1806 rtswitch(struct rt_entry *rt,
1807 struct rt_spare *rts)
1808 {
1809 struct rt_spare swap;
1810 char label[10];
1811
1812
1813 /* Do not change permanent routes */
1814 if (0 != (rt->rt_state & (RS_MHOME | RS_STATIC | RS_RDISC
1815 | RS_NET_SYN | RS_IF)))
1816 return;
1817
1818 /* find the best alternative among the spares */
1819 if (rts == 0)
1820 rts = rts_better(rt);
1821
1822 /* Do not bother if it is not worthwhile.
1823 */
1824 if (!BETTER_LINK(rt, rts, rt->rt_spares))
1825 return;
1826
1827 swap = rt->rt_spares[0];
1828 (void)sprintf(label, "Use #%d", (int)(rts - rt->rt_spares));
1829 rtchange(rt, rt->rt_state & ~(RS_NET_SYN | RS_RDISC), rts, label);
1830 if (swap.rts_metric == HOPCNT_INFINITY) {
1831 *rts = rts_empty;
1832 } else {
1833 *rts = swap;
1834 }
1835 }
1836
1837
1838 void
1839 rtdelete(struct rt_entry *rt)
1840 {
1841 struct khash *k;
1842
1843
1844 if (TRACEACTIONS)
1845 trace_add_del("Del", rt);
1846
1847 k = kern_find(rt->rt_dst, rt->rt_mask, 0);
1848 if (k != 0) {
1849 k->k_state |= KS_DELETE;
1850 need_kern.tv_sec = now.tv_sec;
1851 }
1852
1853 dst_sock.sin_addr.s_addr = rt->rt_dst;
1854 mask_sock.sin_addr.s_addr = rt->rt_mask;
1855 masktrim(&mask_sock);
1856 if (rt != (struct rt_entry *)rhead->rnh_deladdr(&dst_sock, &mask_sock,
1857 rhead)) {
1858 msglog("rnh_deladdr() failed");
1859 } else {
1860 free(rt);
1861 total_routes--;
1862 }
1863 }
1864
1865
1866 void
1867 rts_delete(struct rt_entry *rt,
1868 struct rt_spare *rts)
1869 {
1870 trace_upslot(rt, rts, &rts_empty);
1871 *rts = rts_empty;
1872 }
1873
1874
1875 /* Get rid of a bad route, and try to switch to a replacement.
1876 */
1877 void
1878 rtbad(struct rt_entry *rt)
1879 {
1880 struct rt_spare new;
1881
1882 /* Poison the route */
1883 new = rt->rt_spares[0];
1884 new.rts_metric = HOPCNT_INFINITY;
1885 rtchange(rt, rt->rt_state & ~(RS_IF | RS_LOCAL | RS_STATIC), &new, 0);
1886 rtswitch(rt, 0);
1887 }
1888
1889
1890 /* Junk a RS_NET_SYN or RS_LOCAL route,
1891 * unless it is needed by another interface.
1892 */
1893 void
1894 rtbad_sub(struct rt_entry *rt)
1895 {
1896 struct interface *ifp, *ifp1;
1897 struct intnet *intnetp;
1898 u_int state;
1899
1900
1901 ifp1 = 0;
1902 state = 0;
1903
1904 if (rt->rt_state & RS_LOCAL) {
1905 /* Is this the route through loopback for the interface?
1906 * If so, see if it is used by any other interfaces, such
1907 * as a point-to-point interface with the same local address.
1908 */
1909 for (ifp = ifnet; ifp != 0; ifp = ifp->int_next) {
1910 /* Retain it if another interface needs it.
1911 */
1912 if (ifp->int_addr == rt->rt_ifp->int_addr) {
1913 state |= RS_LOCAL;
1914 ifp1 = ifp;
1915 break;
1916 }
1917 }
1918
1919 }
1920
1921 if (!(state & RS_LOCAL)) {
1922 /* Retain RIPv1 logical network route if there is another
1923 * interface that justifies it.
1924 */
1925 if (rt->rt_state & RS_NET_SYN) {
1926 for (ifp = ifnet; ifp != 0; ifp = ifp->int_next) {
1927 if ((ifp->int_state & IS_NEED_NET_SYN)
1928 && rt->rt_mask == ifp->int_std_mask
1929 && rt->rt_dst == ifp->int_std_addr) {
1930 state |= RS_NET_SYN;
1931 ifp1 = ifp;
1932 break;
1933 }
1934 }
1935 }
1936
1937 /* or if there is an authority route that needs it. */
1938 for (intnetp = intnets;
1939 intnetp != 0;
1940 intnetp = intnetp->intnet_next) {
1941 if (intnetp->intnet_addr == rt->rt_dst
1942 && intnetp->intnet_mask == rt->rt_mask) {
1943 state |= (RS_NET_SYN | RS_NET_INT);
1944 break;
1945 }
1946 }
1947 }
1948
1949 if (ifp1 != 0 || (state & RS_NET_SYN)) {
1950 struct rt_spare new = rt->rt_spares[0];
1951 new.rts_ifp = ifp1;
1952 rtchange(rt, ((rt->rt_state & ~(RS_NET_SYN|RS_LOCAL)) | state),
1953 &new, 0);
1954 } else {
1955 rtbad(rt);
1956 }
1957 }
1958
1959
1960 /* Called while walking the table looking for sick interfaces
1961 * or after a time change.
1962 */
1963 /* ARGSUSED */
1964 int
1965 walk_bad(struct radix_node *rn,
1966 struct walkarg *argp UNUSED)
1967 {
1968 #define RT ((struct rt_entry *)rn)
1969 struct rt_spare *rts;
1970 int i;
1971
1972
1973 /* fix any spare routes through the interface
1974 */
1975 rts = RT->rt_spares;
1976 for (i = NUM_SPARES; i != 1; i--) {
1977 rts++;
1978 if (rts->rts_metric < HOPCNT_INFINITY
1979 && (rts->rts_ifp == 0
1980 || (rts->rts_ifp->int_state & IS_BROKE)))
1981 rts_delete(RT, rts);
1982 }
1983
1984 /* Deal with the main route
1985 */
1986 /* finished if it has been handled before or if its interface is ok
1987 */
1988 if (RT->rt_ifp == 0 || !(RT->rt_ifp->int_state & IS_BROKE))
1989 return 0;
1990
1991 /* Bad routes for other than interfaces are easy.
1992 */
1993 if (0 == (RT->rt_state & (RS_IF | RS_NET_SYN | RS_LOCAL))) {
1994 rtbad(RT);
1995 return 0;
1996 }
1997
1998 rtbad_sub(RT);
1999 return 0;
2000 #undef RT
2001 }
2002
2003
2004 /* Check the age of an individual route.
2005 */
2006 /* ARGSUSED */
2007 static int
2008 walk_age(struct radix_node *rn,
2009 struct walkarg *argp UNUSED)
2010 {
2011 #define RT ((struct rt_entry *)rn)
2012 struct interface *ifp;
2013 struct rt_spare *rts;
2014 int i;
2015
2016
2017 /* age all of the spare routes, including the primary route
2018 * currently in use
2019 */
2020 rts = RT->rt_spares;
2021 for (i = NUM_SPARES; i != 0; i--, rts++) {
2022
2023 ifp = rts->rts_ifp;
2024 if (i == NUM_SPARES) {
2025 if (!AGE_RT(RT->rt_state, ifp)) {
2026 /* Keep various things from deciding ageless
2027 * routes are stale
2028 */
2029 rts->rts_time = now.tv_sec;
2030 continue;
2031 }
2032
2033 /* forget RIP routes after RIP has been turned off.
2034 */
2035 if (rip_sock < 0) {
2036 rtdelete(RT);
2037 return 0;
2038 }
2039 }
2040
2041 /* age failing routes
2042 */
2043 if (age_bad_gate == rts->rts_gate
2044 && rts->rts_time >= now_stale) {
2045 rts->rts_time -= SUPPLY_INTERVAL;
2046 }
2047
2048 /* trash the spare routes when they go bad */
2049 if (rts->rts_metric < HOPCNT_INFINITY
2050 && now_garbage > rts->rts_time
2051 && i != NUM_SPARES)
2052 rts_delete(RT, rts);
2053 }
2054
2055
2056 /* finished if the active route is still fresh */
2057 if (now_stale <= RT->rt_time)
2058 return 0;
2059
2060 /* try to switch to an alternative */
2061 rtswitch(RT, 0);
2062
2063 /* Delete a dead route after it has been publically mourned. */
2064 if (now_garbage > RT->rt_time) {
2065 rtdelete(RT);
2066 return 0;
2067 }
2068
2069 /* Start poisoning a bad route before deleting it. */
2070 if (now.tv_sec - RT->rt_time > EXPIRE_TIME) {
2071 struct rt_spare new = RT->rt_spares[0];
2072 new.rts_metric = HOPCNT_INFINITY;
2073 rtchange(RT, RT->rt_state, &new, 0);
2074 }
2075 return 0;
2076 }
2077
2078
2079 /* Watch for dead routes and interfaces.
2080 */
2081 void
2082 age(naddr bad_gate)
2083 {
2084 struct interface *ifp;
2085 int need_query = 0;
2086
2087 /* If not listening to RIP, there is no need to age the routes in
2088 * the table.
2089 */
2090 age_timer.tv_sec = (now.tv_sec
2091 + ((rip_sock < 0) ? NEVER : SUPPLY_INTERVAL));
2092
2093 /* Check for dead IS_REMOTE interfaces by timing their
2094 * transmissions.
2095 */
2096 for (ifp = ifnet; ifp; ifp = ifp->int_next) {
2097 if (!(ifp->int_state & IS_REMOTE))
2098 continue;
2099
2100 /* ignore unreachable remote interfaces */
2101 if (!check_remote(ifp))
2102 continue;
2103
2104 /* Restore remote interface that has become reachable
2105 */
2106 if (ifp->int_state & IS_BROKE)
2107 if_ok(ifp, "remote ");
2108
2109 if (ifp->int_act_time != NEVER
2110 && now.tv_sec - ifp->int_act_time > EXPIRE_TIME) {
2111 msglog("remote interface %s to %s timed out after"
2112 " %ld:%ld",
2113 ifp->int_name,
2114 naddr_ntoa(ifp->int_dstaddr),
2115 (now.tv_sec - ifp->int_act_time)/60,
2116 (now.tv_sec - ifp->int_act_time)%60);
2117 if_sick(ifp);
2118 }
2119
2120 /* If we have not heard from the other router
2121 * recently, ask it.
2122 */
2123 if (now.tv_sec >= ifp->int_query_time) {
2124 ifp->int_query_time = NEVER;
2125 need_query = 1;
2126 }
2127 }
2128
2129 /* Age routes. */
2130 age_bad_gate = bad_gate;
2131 (void)rn_walktree(rhead, walk_age, 0);
2132
2133 /* delete old redirected routes to keep the kernel table small
2134 * and prevent blackholes
2135 */
2136 del_redirects(bad_gate, now.tv_sec-STALE_TIME);
2137
2138 /* Update the kernel routing table. */
2139 fix_kern();
2140
2141 /* poke reticent remote gateways */
2142 if (need_query)
2143 rip_query();
2144 }
2145