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