2 * Copyright (c) 1983, 1988, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgment:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * $FreeBSD: src/sbin/routed/table.c,v 1.9.2.2 2000/08/14 17:00:04 sheldonh Exp $
38 #if !defined(sgi) && !defined(__NetBSD__)
39 static char sccsid
[] __attribute__((unused
)) = "@(#)tables.c 8.1 (Berkeley) 6/5/93";
40 #elif defined(__NetBSD__)
44 static struct rt_spare
*rts_better(struct rt_entry
*);
45 static struct rt_spare rts_empty
= {0,0,0,HOPCNT_INFINITY
,0,0,0};
46 static void set_need_flash(void);
48 static void masktrim(struct sockaddr_in
*ap
);
50 static void masktrim(struct sockaddr_in_new
*ap
);
54 struct radix_node_head
*rhead
; /* root of the radix tree */
56 int need_flash
= 1; /* flash update needed
57 * start =1 to suppress the 1st
60 struct timeval age_timer
; /* next check of old routes */
61 struct timeval need_kern
= { /* need to update kernel table */
62 EPOCH
+MIN_WAITTIME
-1, 0
69 /* zap any old routes through this gateway */
73 /* It is desirable to "aggregate" routes, to combine differing routes of
74 * the same metric and next hop into a common route with a smaller netmask
75 * or to suppress redundant routes, routes that add no information to
76 * routes with smaller netmasks.
78 * A route is redundant if and only if any and all routes with smaller
79 * but matching netmasks and nets are the same. Since routes are
80 * kept sorted in the radix tree, redundant routes always come second.
82 * There are two kinds of aggregations. First, two routes of the same bit
83 * mask and differing only in the least significant bit of the network
84 * number can be combined into a single route with a coarser mask.
86 * Second, a route can be suppressed in favor of another route with a more
87 * coarse mask provided no incompatible routes with intermediate masks
88 * are present. The second kind of aggregation involves suppressing routes.
89 * A route must not be suppressed if an incompatible route exists with
90 * an intermediate mask, since the suppressed route would be covered
91 * by the intermediate.
93 * This code relies on the radix tree walk encountering routes
94 * sorted first by address, with the smallest address first.
97 struct ag_info ag_slots
[NUM_AG_SLOTS
], *ag_avail
, *ag_corsest
, *ag_finest
;
99 /* #define DEBUG_AG */
101 #define CHECK_AG() {int acnt = 0; struct ag_info *cag; \
102 for (cag = ag_avail; cag != NULL; cag = cag->ag_fine) \
104 for (cag = ag_corsest; cag != NULL; cag = cag->ag_fine) \
106 if (acnt != NUM_AG_SLOTS) { \
116 /* Output the contents of an aggregation table slot.
117 * This function must always be immediately followed with the deletion
118 * of the target slot.
121 ag_out(struct ag_info
*ag
,
122 void (*out
)(struct ag_info
*))
124 struct ag_info
*ag_cors
;
128 /* Forget it if this route should not be output for split-horizon. */
129 if (ag
->ag_state
& AGS_SPLIT_HZ
)
132 /* If we output both the even and odd twins, then the immediate parent,
133 * if it is present, is redundant, unless the parent manages to
134 * aggregate into something coarser.
135 * On successive calls, this code detects the even and odd twins,
136 * and marks the parent.
138 * Note that the order in which the radix tree code emits routes
139 * ensures that the twins are seen before the parent is emitted.
141 ag_cors
= ag
->ag_cors
;
143 && ag_cors
->ag_mask
== ag
->ag_mask
<<1
144 && ag_cors
->ag_dst_h
== (ag
->ag_dst_h
& ag_cors
->ag_mask
)) {
145 ag_cors
->ag_state
|= ((ag_cors
->ag_dst_h
== ag
->ag_dst_h
)
150 /* Skip it if this route is itself redundant.
152 * It is ok to change the contents of the slot here, since it is
153 * always deleted next.
155 if (ag
->ag_state
& AGS_REDUN0
) {
156 if (ag
->ag_state
& AGS_REDUN1
)
157 return; /* quit if fully redundant */
158 /* make it finer if it is half-redundant */
159 bit
= (-ag
->ag_mask
) >> 1;
163 } else if (ag
->ag_state
& AGS_REDUN1
) {
164 /* make it finer if it is half-redundant */
165 bit
= (-ag
->ag_mask
) >> 1;
173 ag_del(struct ag_info
*ag
)
177 if (ag
->ag_cors
== 0)
178 ag_corsest
= ag
->ag_fine
;
180 ag
->ag_cors
->ag_fine
= ag
->ag_fine
;
182 if (ag
->ag_fine
== 0)
183 ag_finest
= ag
->ag_cors
;
185 ag
->ag_fine
->ag_cors
= ag
->ag_cors
;
187 ag
->ag_fine
= ag_avail
;
194 /* Flush routes waiting for aggregation.
195 * This must not suppress a route unless it is known that among all
196 * routes with coarser masks that match it, the one with the longest
197 * mask is appropriate. This is ensured by scanning the routes
198 * in lexical order, and with the most restrictive mask first
199 * among routes to the same destination.
202 ag_flush(naddr lim_dst_h
, /* flush routes to here */
203 naddr lim_mask
, /* matching this mask */
204 void (*out
)(struct ag_info
*))
206 struct ag_info
*ag
, *ag_cors
;
211 ag
!= NULL
&& ag
->ag_mask
>= lim_mask
;
213 ag_cors
= ag
->ag_cors
;
215 /* work on only the specified routes */
216 dst_h
= ag
->ag_dst_h
;
217 if ((dst_h
& lim_mask
) != lim_dst_h
)
220 if (!(ag
->ag_state
& AGS_SUPPRESS
))
223 else for ( ; ; ag_cors
= ag_cors
->ag_cors
) {
224 /* Look for a route that can suppress the
226 if (ag_cors
== NULL
) {
227 /* failed, so output it and look for
228 * another route to work on
234 if ((dst_h
& ag_cors
->ag_mask
) == ag_cors
->ag_dst_h
) {
235 /* We found a route with a coarser mask that
236 * aggregates the current target.
238 * If it has a different next hop, it
239 * cannot replace the target, so output
242 if (ag
->ag_gate
!= ag_cors
->ag_gate
243 && !(ag
->ag_state
& AGS_FINE_GATE
)
244 && !(ag_cors
->ag_state
& AGS_CORS_GATE
)) {
249 /* If the coarse route has a good enough
250 * metric, it suppresses the target.
251 * If the suppressed target was redundant,
252 * then mark the suppressor redundant.
254 if (ag_cors
->ag_pref
<= ag
->ag_pref
) {
255 if (ag_cors
->ag_seqno
> ag
->ag_seqno
)
256 ag_cors
->ag_seqno
= ag
->ag_seqno
;
257 if (AG_IS_REDUN(ag
->ag_state
)
258 && ag_cors
->ag_mask
==ag
->ag_mask
<<1) {
259 if (ag_cors
->ag_dst_h
== dst_h
)
260 ag_cors
->ag_state
|= AGS_REDUN0
;
262 ag_cors
->ag_state
|= AGS_REDUN1
;
264 if (ag
->ag_tag
!= ag_cors
->ag_tag
)
266 if (ag
->ag_nhop
!= ag_cors
->ag_nhop
)
267 ag_cors
->ag_nhop
= 0;
273 /* That route has either been output or suppressed */
274 ag_cors
= ag
->ag_cors
;
282 /* Try to aggregate a route with previous routes.
294 void (*out
)(struct ag_info
*)) /* output using this */
296 struct ag_info
*ag
, *nag
, *ag_cors
;
302 /* Punt non-contiguous subnet masks.
304 * (X & -X) contains a single bit if and only if X is a power of 2.
305 * (X + (X & -X)) == 0 if and only if X is a power of 2.
307 if ((mask
& -mask
) + mask
!= 0) {
308 struct ag_info nc_ag
;
310 nc_ag
.ag_dst_h
= dst
;
311 nc_ag
.ag_mask
= mask
;
312 nc_ag
.ag_gate
= gate
;
313 nc_ag
.ag_nhop
= nhop
;
314 nc_ag
.ag_metric
= metric
;
315 nc_ag
.ag_pref
= pref
;
317 nc_ag
.ag_state
= state
;
318 nc_ag
.ag_seqno
= seqnum
;
323 /* Search for the right slot in the aggregation table.
328 if (ag
->ag_mask
>= mask
)
331 /* Suppress old routes (i.e. combine with compatible routes
332 * with coarser masks) as we look for the right slot in the
333 * aggregation table for the new route.
334 * A route to an address less than the current destination
335 * will not be affected by the current route or any route
336 * seen hereafter. That means it is safe to suppress it.
337 * This check keeps poor routes (e.g. with large hop counts)
338 * from preventing suppression of finer routes.
341 && ag
->ag_dst_h
< dst
342 && (ag
->ag_state
& AGS_SUPPRESS
)
343 && ag_cors
->ag_pref
<= ag
->ag_pref
344 && (ag
->ag_dst_h
& ag_cors
->ag_mask
) == ag_cors
->ag_dst_h
345 && (ag_cors
->ag_gate
== ag
->ag_gate
346 || (ag
->ag_state
& AGS_FINE_GATE
)
347 || (ag_cors
->ag_state
& AGS_CORS_GATE
))) {
348 if (ag_cors
->ag_seqno
> ag
->ag_seqno
)
349 ag_cors
->ag_seqno
= ag
->ag_seqno
;
350 /* If the suppressed target was redundant,
351 * then mark the suppressor redundant.
353 if (AG_IS_REDUN(ag
->ag_state
)
354 && ag_cors
->ag_mask
== ag
->ag_mask
<<1) {
355 if (ag_cors
->ag_dst_h
== dst
)
356 ag_cors
->ag_state
|= AGS_REDUN0
;
358 ag_cors
->ag_state
|= AGS_REDUN1
;
360 if (ag
->ag_tag
!= ag_cors
->ag_tag
)
362 if (ag
->ag_nhop
!= ag_cors
->ag_nhop
)
363 ag_cors
->ag_nhop
= 0;
369 ag
= ag_cors
->ag_fine
;
372 /* If we find the even/odd twin of the new route, and if the
373 * masks and so forth are equal, we can aggregate them.
374 * We can probably promote one of the pair.
376 * Since the routes are encountered in lexical order,
377 * the new route must be odd. However, the second or later
378 * times around this loop, it could be the even twin promoted
379 * from the even/odd pair of twins of the finer route.
382 && ag
->ag_mask
== mask
383 && ((ag
->ag_dst_h
^ dst
) & (mask
<<1)) == 0) {
385 /* Here we know the target route and the route in the current
386 * slot have the same netmasks and differ by at most the
387 * last bit. They are either for the same destination, or
388 * for an even/odd pair of destinations.
390 if (ag
->ag_dst_h
== dst
) {
391 /* We have two routes to the same destination.
392 * Routes are encountered in lexical order, so a
393 * route is never promoted until the parent route is
394 * already present. So we know that the new route is
395 * a promoted (or aggregated) pair and the route
396 * already in the slot is the explicit route.
398 * Prefer the best route if their metrics differ,
399 * or the aggregated one if not, following a sort
400 * of longest-match rule.
402 if (pref
<= ag
->ag_pref
) {
406 ag
->ag_metric
= metric
;
409 ag
->ag_state
= state
;
413 /* The sequence number controls flash updating,
414 * and should be the smaller of the two.
416 if (ag
->ag_seqno
> seqnum
)
417 ag
->ag_seqno
= seqnum
;
419 /* Some bits are set if they are set on either route,
420 * except when the route is for an interface.
422 if (!(ag
->ag_state
& AGS_IF
))
423 ag
->ag_state
|= (state
& (AGS_AGGREGATE_EITHER
429 /* If one of the routes can be promoted and the other can
430 * be suppressed, it may be possible to combine them or
431 * worthwhile to promote one.
433 * Any route that can be promoted is always
434 * marked to be eligible to be suppressed.
436 if (!((state
& AGS_AGGREGATE
)
437 && (ag
->ag_state
& AGS_SUPPRESS
))
438 && !((ag
->ag_state
& AGS_AGGREGATE
)
439 && (state
& AGS_SUPPRESS
)))
442 /* A pair of even/odd twin routes can be combined
443 * if either is redundant, or if they are via the
444 * same gateway and have the same metric.
446 if (AG_IS_REDUN(ag
->ag_state
)
447 || AG_IS_REDUN(state
)
448 || (ag
->ag_gate
== gate
449 && ag
->ag_pref
== pref
450 && (state
& ag
->ag_state
& AGS_AGGREGATE
) != 0)) {
452 /* We have both the even and odd pairs.
453 * Since the routes are encountered in order,
454 * the route in the slot must be the even twin.
456 * Combine and promote (aggregate) the pair of routes.
458 if (seqnum
> ag
->ag_seqno
)
459 seqnum
= ag
->ag_seqno
;
460 if (!AG_IS_REDUN(state
))
461 state
&= ~AGS_REDUN1
;
462 if (AG_IS_REDUN(ag
->ag_state
))
465 state
&= ~AGS_REDUN0
;
466 state
|= (ag
->ag_state
& AGS_AGGREGATE_EITHER
);
467 if (ag
->ag_tag
!= tag
)
469 if (ag
->ag_nhop
!= nhop
)
472 /* Get rid of the even twin that was already
477 } else if (ag
->ag_pref
>= pref
478 && (ag
->ag_state
& AGS_AGGREGATE
)) {
479 /* If we cannot combine the pair, maybe the route
480 * with the worse metric can be promoted.
482 * Promote the old, even twin, by giving its slot
483 * in the table to the new, odd twin.
499 /* The promoted route is even-redundant only if the
500 * even twin was fully redundant. It is not
501 * odd-redundant because the odd-twin will still be
508 ag
->ag_state
= state
;
512 ag
->ag_metric
= metric
;
519 /* take the newest sequence number */
520 if (seqnum
>= ag
->ag_seqno
)
521 seqnum
= ag
->ag_seqno
;
523 ag
->ag_seqno
= seqnum
;
526 if (!(state
& AGS_AGGREGATE
))
527 break; /* cannot promote either twin */
529 /* Promote the new, odd twin by shaving its
531 * The promoted route is odd-redundant only if the
532 * odd twin was fully redundant. It is not
533 * even-redundant because the even twin is still in
536 if (!AG_IS_REDUN(state
))
537 state
&= ~AGS_REDUN1
;
538 state
&= ~AGS_REDUN0
;
539 if (seqnum
> ag
->ag_seqno
)
540 seqnum
= ag
->ag_seqno
;
542 ag
->ag_seqno
= seqnum
;
548 if (ag_cors
== NULL
) {
553 ag_cors
= ag
->ag_cors
;
556 /* When we can no longer promote and combine routes,
557 * flush the old route in the target slot. Also flush
558 * any finer routes that we know will never be aggregated by
561 * In case we moved toward coarser masks,
562 * get back where we belong
565 && ag
->ag_mask
< mask
) {
570 /* Empty the target slot
572 if (ag
!= NULL
&& ag
->ag_mask
== mask
) {
573 ag_flush(ag
->ag_dst_h
, ag
->ag_mask
, out
);
574 ag
= (ag_cors
== NULL
) ? ag_corsest
: ag_cors
->ag_fine
;
579 if (ag
== NULL
&& ag_cors
!= ag_finest
)
581 if (ag_cors
== NULL
&& ag
!= ag_corsest
)
583 if (ag
!= NULL
&& ag
->ag_cors
!= ag_cors
)
585 if (ag_cors
!= NULL
&& ag_cors
->ag_fine
!= ag
)
590 /* Save the new route on the end of the table.
593 ag_avail
= nag
->ag_fine
;
599 nag
->ag_metric
= metric
;
602 nag
->ag_state
= state
;
603 nag
->ag_seqno
= seqnum
;
610 nag
->ag_cors
= ag_cors
;
614 ag_cors
->ag_fine
= nag
;
621 rtm_type_name(u_char type
)
623 static const char *rtm_types
[] = {
641 #define NEW_RTM_PAT "RTM type %#x"
642 static char name0
[sizeof(NEW_RTM_PAT
)+2];
645 if (type
> sizeof(rtm_types
)/sizeof(rtm_types
[0])
647 snprintf(name0
, sizeof(name0
), NEW_RTM_PAT
, type
);
650 return rtm_types
[type
-1];
656 /* Trim a mask in a sockaddr
657 * Produce a length of 0 for an address of 0.
658 * Otherwise produce the index of the first zero byte.
662 masktrim(struct sockaddr_in
*ap
)
664 masktrim(struct sockaddr_in_new
*ap
)
669 if (ap
->sin_addr
.s_addr
== 0) {
673 cp
= (char *)(&ap
->sin_addr
.s_addr
+1);
676 ap
->sin_len
= cp
- (char*)ap
+ 1;
680 /* Tell the kernel to add, delete or change a route
683 rtioctl(int action
, /* RTM_DELETE, etc */
691 struct rt_msghdr w_rtm
;
692 struct sockaddr_in w_dst
;
693 struct sockaddr_in w_gate
;
695 struct sockaddr_in w_mask
;
697 struct sockaddr_in_new w_mask
;
701 # define PAT " %-10s %s metric=%d flags=%#x"
702 # define ARGS rtm_type_name(action), rtname(dst,mask,gate), metric, flags
705 memset(&w
, 0, sizeof(w
));
706 w
.w_rtm
.rtm_msglen
= sizeof(w
);
707 w
.w_rtm
.rtm_version
= RTM_VERSION
;
708 w
.w_rtm
.rtm_type
= action
;
709 w
.w_rtm
.rtm_flags
= flags
;
710 w
.w_rtm
.rtm_seq
= ++rt_sock_seqno
;
711 w
.w_rtm
.rtm_addrs
= RTA_DST
|RTA_GATEWAY
;
712 if (metric
!= 0 || action
== RTM_CHANGE
) {
713 w
.w_rtm
.rtm_rmx
.rmx_hopcount
= metric
;
714 w
.w_rtm
.rtm_inits
|= RTV_HOPCOUNT
;
716 w
.w_dst
.sin_family
= AF_INET
;
717 w
.w_dst
.sin_addr
.s_addr
= dst
;
718 w
.w_gate
.sin_family
= AF_INET
;
719 w
.w_gate
.sin_addr
.s_addr
= gate
;
721 w
.w_dst
.sin_len
= sizeof(w
.w_dst
);
722 w
.w_gate
.sin_len
= sizeof(w
.w_gate
);
724 if (mask
== HOST_MASK
) {
725 w
.w_rtm
.rtm_flags
|= RTF_HOST
;
726 w
.w_rtm
.rtm_msglen
-= sizeof(w
.w_mask
);
728 w
.w_rtm
.rtm_addrs
|= RTA_NETMASK
;
729 w
.w_mask
.sin_addr
.s_addr
= htonl(mask
);
732 if (w
.w_mask
.sin_len
== 0)
733 w
.w_mask
.sin_len
= sizeof(long);
734 w
.w_rtm
.rtm_msglen
-= (sizeof(w
.w_mask
) - w
.w_mask
.sin_len
);
739 cc
= write(rt_sock
, &w
, w
.w_rtm
.rtm_msglen
);
742 && (action
== RTM_CHANGE
|| action
== RTM_DELETE
)) {
743 trace_act("route disappeared before" PAT
, ARGS
);
744 if (action
== RTM_CHANGE
) {
750 msglog("write(rt_sock)" PAT
": %s", ARGS
, strerror(errno
));
752 } else if (cc
!= w
.w_rtm
.rtm_msglen
) {
753 msglog("write(rt_sock) wrote %ld instead of %d for" PAT
,
754 cc
, w
.w_rtm
.rtm_msglen
, ARGS
);
759 trace_misc("write kernel" PAT
, ARGS
);
765 #define KHASH_SIZE 71 /* should be prime */
766 #define KHASH(a,m) khash_bins[((a) ^ (m)) % KHASH_SIZE]
767 static struct khash
{
768 struct khash
*k_next
;
775 #define KS_DELETE 0x002 /* need to delete the route */
776 #define KS_ADD 0x004 /* add to the kernel */
777 #define KS_CHANGE 0x008 /* tell kernel to change the route */
778 #define KS_DEL_ADD 0x010 /* delete & add to change the kernel */
779 #define KS_STATIC 0x020 /* Static flag in kernel */
780 #define KS_GATEWAY 0x040 /* G flag in kernel */
781 #define KS_DYNAMIC 0x080 /* result of redirect */
782 #define KS_DELETED 0x100 /* already deleted from kernel */
783 #define KS_CHECK 0x200
785 #define K_KEEP_LIM 30
786 time_t k_redirect_time
; /* when redirected route 1st seen */
787 } *khash_bins
[KHASH_SIZE
];
791 kern_find(naddr dst
, naddr mask
, struct khash
***ppk
)
793 struct khash
*k
, **pk
;
795 for (pk
= &KHASH(dst
,mask
); (k
= *pk
) != NULL
; pk
= &k
->k_next
) {
796 if (k
->k_dst
== dst
&& k
->k_mask
== mask
)
806 kern_add(naddr dst
, naddr mask
)
808 struct khash
*k
, **pk
;
810 k
= kern_find(dst
, mask
, &pk
);
814 k
= (struct khash
*)rtmalloc(sizeof(*k
), "kern_add");
816 memset(k
, 0, sizeof(*k
));
820 k
->k_keep
= now
.tv_sec
;
827 /* If a kernel route has a non-zero metric, check that it is still in the
828 * daemon table, and not deleted by interfaces coming and going.
831 kern_check_static(struct khash
*k
,
832 struct interface
*ifp
)
837 if (k
->k_metric
== 0)
840 memset(&new, 0, sizeof(new));
842 new.rts_gate
= k
->k_gate
;
843 new.rts_router
= (ifp
!= NULL
) ? ifp
->int_addr
: loopaddr
;
844 new.rts_metric
= k
->k_metric
;
845 new.rts_time
= now
.tv_sec
;
847 rt
= rtget(k
->k_dst
, k
->k_mask
);
849 if (!(rt
->rt_state
& RS_STATIC
))
850 rtchange(rt
, rt
->rt_state
| RS_STATIC
, &new, 0);
852 rtadd(k
->k_dst
, k
->k_mask
, RS_STATIC
, &new);
857 /* operate on a kernel entry
860 kern_ioctl(struct khash
*k
,
861 int action
, /* RTM_DELETE, etc */
867 k
->k_state
&= ~KS_DYNAMIC
;
868 if (k
->k_state
& KS_DELETED
)
870 k
->k_state
|= KS_DELETED
;
873 k
->k_state
&= ~KS_DELETED
;
876 if (k
->k_state
& KS_DELETED
) {
878 k
->k_state
&= ~KS_DELETED
;
883 rtioctl(action
, k
->k_dst
, k
->k_gate
, k
->k_mask
, k
->k_metric
, flags
);
887 /* add a route the kernel told us
890 rtm_add(struct rt_msghdr
*rtm
,
891 struct rt_addrinfo
*info
,
895 struct interface
*ifp
;
899 if (rtm
->rtm_flags
& RTF_HOST
) {
901 } else if (INFO_MASK(info
) != 0) {
902 mask
= ntohl(S_ADDR(INFO_MASK(info
)));
904 msglog("ignore %s without mask", rtm_type_name(rtm
->rtm_type
));
908 k
= kern_add(S_ADDR(INFO_DST(info
)), mask
);
909 if (k
->k_state
& KS_NEW
)
910 k
->k_keep
= now
.tv_sec
+keep
;
911 if (INFO_GATE(info
) == 0) {
912 trace_act("note %s without gateway",
913 rtm_type_name(rtm
->rtm_type
));
914 k
->k_metric
= HOPCNT_INFINITY
;
915 } else if (INFO_GATE(info
)->sa_family
!= AF_INET
) {
916 trace_act("note %s with gateway AF=%d",
917 rtm_type_name(rtm
->rtm_type
),
918 INFO_GATE(info
)->sa_family
);
919 k
->k_metric
= HOPCNT_INFINITY
;
921 k
->k_gate
= S_ADDR(INFO_GATE(info
));
922 k
->k_metric
= rtm
->rtm_rmx
.rmx_hopcount
;
925 else if (k
->k_metric
> HOPCNT_INFINITY
-1)
926 k
->k_metric
= HOPCNT_INFINITY
-1;
928 k
->k_state
&= ~(KS_DELETE
| KS_ADD
| KS_CHANGE
| KS_DEL_ADD
929 | KS_DELETED
| KS_GATEWAY
| KS_STATIC
930 | KS_NEW
| KS_CHECK
);
931 if (rtm
->rtm_flags
& RTF_GATEWAY
)
932 k
->k_state
|= KS_GATEWAY
;
933 if (rtm
->rtm_flags
& RTF_STATIC
)
934 k
->k_state
|= KS_STATIC
;
936 if (0 != (rtm
->rtm_flags
& (RTF_DYNAMIC
| RTF_MODIFIED
))) {
937 if (INFO_AUTHOR(info
) != 0
938 && INFO_AUTHOR(info
)->sa_family
== AF_INET
)
939 ifp
= iflookup(S_ADDR(INFO_AUTHOR(info
)));
943 && (ifp
== NULL
|| !(ifp
->int_state
& IS_REDIRECT_OK
))) {
944 /* Routers are not supposed to listen to redirects,
945 * so delete it if it came via an unknown interface
946 * or the interface does not have special permission.
948 k
->k_state
&= ~KS_DYNAMIC
;
949 k
->k_state
|= KS_DELETE
;
950 LIM_SEC(need_kern
, 0);
951 trace_act("mark for deletion redirected %s --> %s"
953 addrname(k
->k_dst
, k
->k_mask
, 0),
954 naddr_ntoa(k
->k_gate
),
955 ifp
? ifp
->int_name
: "unknown interface");
957 k
->k_state
|= KS_DYNAMIC
;
958 k
->k_redirect_time
= now
.tv_sec
;
959 trace_act("accept redirected %s --> %s via %s",
960 addrname(k
->k_dst
, k
->k_mask
, 0),
961 naddr_ntoa(k
->k_gate
),
962 ifp
? ifp
->int_name
: "unknown interface");
967 /* If it is not a static route, quit until the next comparison
968 * between the kernel and daemon tables, when it will be deleted.
970 if (!(k
->k_state
& KS_STATIC
)) {
971 k
->k_state
|= KS_DELETE
;
972 LIM_SEC(need_kern
, k
->k_keep
);
976 /* Put static routes with real metrics into the daemon table so
977 * they can be advertised.
979 * Find the interface toward the gateway.
981 ifp
= iflookup(k
->k_gate
);
983 msglog("static route %s --> %s impossibly lacks ifp",
984 addrname(S_ADDR(INFO_DST(info
)), mask
, 0),
985 naddr_ntoa(k
->k_gate
));
987 kern_check_static(k
, ifp
);
991 /* deal with packet loss
994 rtm_lose(struct rt_msghdr
*rtm
,
995 struct rt_addrinfo
*info
)
997 if (INFO_GATE(info
) == 0
998 || INFO_GATE(info
)->sa_family
!= AF_INET
) {
999 trace_act("ignore %s without gateway",
1000 rtm_type_name(rtm
->rtm_type
));
1005 rdisc_age(S_ADDR(INFO_GATE(info
)));
1006 age(S_ADDR(INFO_GATE(info
)));
1010 /* Make the gateway slot of an info structure point to something
1011 * useful. If it is not already useful, but it specifies an interface,
1012 * then fill in the sockaddr_in provided and point it there.
1015 get_info_gate(struct sockaddr
**sap
,
1016 struct sockaddr_in
*in
)
1018 struct sockaddr_dl
*sdl
= (struct sockaddr_dl
*)*sap
;
1019 struct interface
*ifp
;
1023 if ((sdl
)->sdl_family
== AF_INET
)
1025 if ((sdl
)->sdl_family
!= AF_LINK
)
1028 ifp
= ifwithindex(sdl
->sdl_index
, 1);
1032 in
->sin_addr
.s_addr
= ifp
->int_addr
;
1034 in
->sin_len
= sizeof(*in
);
1036 in
->sin_family
= AF_INET
;
1037 *sap
= (struct sockaddr
*)in
;
1043 /* Clean the kernel table by copying it to the daemon image.
1044 * Eventually the daemon will delete any extra routes.
1049 static char *sysctl_buf
;
1050 static size_t sysctl_buf_size
= 0;
1054 struct rt_msghdr
*rtm
;
1055 struct sockaddr_in gate_sin
;
1056 struct rt_addrinfo info
;
1061 for (i
= 0; i
< KHASH_SIZE
; i
++) {
1062 for (k
= khash_bins
[i
]; k
!= NULL
; k
= k
->k_next
) {
1063 k
->k_state
|= KS_CHECK
;
1069 mib
[2] = 0; /* protocol */
1070 mib
[3] = 0; /* wildcard address family */
1071 mib
[4] = NET_RT_DUMP
;
1072 mib
[5] = 0; /* no flags */
1074 if ((needed
= sysctl_buf_size
) != 0) {
1075 if (sysctl(mib
, 6, sysctl_buf
,&needed
, 0, 0) >= 0)
1077 if (errno
!= ENOMEM
&& errno
!= EFAULT
)
1078 BADERR(1,"flush_kern: sysctl(RT_DUMP)");
1082 if (sysctl(mib
, 6, 0, &needed
, 0, 0) < 0)
1083 BADERR(1,"flush_kern: sysctl(RT_DUMP) estimate");
1084 /* Kludge around the habit of some systems, such as
1085 * BSD/OS 3.1, to not admit how many routes are in the
1086 * kernel, or at least to be quite wrong.
1088 needed
+= 50*(sizeof(*rtm
)+5*sizeof(struct sockaddr
));
1089 sysctl_buf
= rtmalloc(sysctl_buf_size
= needed
,
1090 "flush_kern sysctl(RT_DUMP)");
1093 lim
= sysctl_buf
+ needed
;
1094 for (next
= sysctl_buf
; next
< lim
; next
+= rtm
->rtm_msglen
) {
1095 rtm
= (struct rt_msghdr
*)next
;
1096 if (rtm
->rtm_msglen
== 0) {
1097 msglog("zero length kernel route at "
1098 " %#lx in buffer %#lx before %#lx",
1099 (u_long
)rtm
, (u_long
)sysctl_buf
, (u_long
)lim
);
1104 (struct sockaddr
*)(rtm
+1),
1105 (struct sockaddr
*)(next
+ rtm
->rtm_msglen
),
1108 if (INFO_DST(&info
) == 0
1109 || INFO_DST(&info
)->sa_family
!= AF_INET
)
1112 /* ignore ARP table entries on systems with a merged route
1115 if (rtm
->rtm_flags
& RTF_LLINFO
)
1118 /* ignore multicast addresses
1120 if (IN_MULTICAST(ntohl(S_ADDR(INFO_DST(&info
)))))
1123 if (!get_info_gate(&INFO_GATE(&info
), &gate_sin
))
1126 /* Note static routes and interface routes, and also
1127 * preload the image of the kernel table so that
1128 * we can later clean it, as well as avoid making
1129 * unneeded changes. Keep the old kernel routes for a
1130 * few seconds to allow a RIP or router-discovery
1131 * response to be heard.
1133 rtm_add(rtm
,&info
,MIN_WAITTIME
);
1136 for (i
= 0; i
< KHASH_SIZE
; i
++) {
1137 for (k
= khash_bins
[i
]; k
!= NULL
; k
= k
->k_next
) {
1138 if (k
->k_state
& KS_CHECK
) {
1139 msglog("%s --> %s disappeared from kernel",
1140 addrname(k
->k_dst
, k
->k_mask
, 0),
1141 naddr_ntoa(k
->k_gate
));
1142 del_static(k
->k_dst
, k
->k_mask
, k
->k_gate
, 1);
1149 /* Listen to announcements from the kernel
1155 struct interface
*ifp
;
1156 struct sockaddr_in gate_sin
;
1160 struct rt_msghdr rtm
;
1161 struct sockaddr addrs
[RTAX_MAX
];
1163 struct if_msghdr ifm
;
1165 char str
[100], *strp
;
1166 struct rt_addrinfo info
;
1170 cc
= read(rt_sock
, &m
, sizeof(m
));
1172 if (cc
< 0 && errno
!= EWOULDBLOCK
)
1173 LOGERR("read(rt_sock)");
1177 if (m
.r
.rtm
.rtm_version
!= RTM_VERSION
) {
1178 msglog("bogus routing message version %d",
1179 m
.r
.rtm
.rtm_version
);
1183 /* Ignore our own results.
1185 if (m
.r
.rtm
.rtm_type
<= RTM_CHANGE
1186 && m
.r
.rtm
.rtm_pid
== mypid
) {
1187 static int complained
= 0;
1189 msglog("receiving our own change messages");
1195 if (m
.r
.rtm
.rtm_type
== RTM_IFINFO
1196 || m
.r
.rtm
.rtm_type
== RTM_NEWADDR
1197 || m
.r
.rtm
.rtm_type
== RTM_DELADDR
) {
1198 ifp
= ifwithindex(m
.ifm
.ifm_index
,
1199 m
.r
.rtm
.rtm_type
!= RTM_DELADDR
);
1201 trace_act("note %s with flags %#x"
1202 " for unknown interface index #%d",
1203 rtm_type_name(m
.r
.rtm
.rtm_type
),
1207 trace_act("note %s with flags %#x for %s",
1208 rtm_type_name(m
.r
.rtm
.rtm_type
),
1212 /* After being informed of a change to an interface,
1213 * check them all now if the check would otherwise
1214 * be a long time from now, if the interface is
1215 * not known, or if the interface has been turned
1218 if (ifinit_timer
.tv_sec
-now
.tv_sec
>=CHECK_BAD_INTERVAL
1220 || ((ifp
->int_if_flags
^ m
.ifm
.ifm_flags
)
1222 ifinit_timer
.tv_sec
= now
.tv_sec
;
1226 strcpy(str
, rtm_type_name(m
.r
.rtm
.rtm_type
));
1227 strp
= &str
[strlen(str
)];
1228 if (m
.r
.rtm
.rtm_type
<= RTM_CHANGE
)
1229 strp
+= sprintf(strp
," from pid %d",m
.r
.rtm
.rtm_pid
);
1231 rt_xaddrs(&info
, m
.r
.addrs
, &m
.r
.addrs
[RTAX_MAX
],
1234 if (INFO_DST(&info
) == 0) {
1235 trace_act("ignore %s without dst", str
);
1239 if (INFO_DST(&info
)->sa_family
!= AF_INET
) {
1240 trace_act("ignore %s for AF %d", str
,
1241 INFO_DST(&info
)->sa_family
);
1245 mask
= ((INFO_MASK(&info
) != 0)
1246 ? ntohl(S_ADDR(INFO_MASK(&info
)))
1247 : (m
.r
.rtm
.rtm_flags
& RTF_HOST
)
1249 : std_mask(S_ADDR(INFO_DST(&info
))));
1251 strp
+= sprintf(strp
, ": %s",
1252 addrname(S_ADDR(INFO_DST(&info
)), mask
, 0));
1254 if (IN_MULTICAST(ntohl(S_ADDR(INFO_DST(&info
))))) {
1255 trace_act("ignore multicast %s", str
);
1259 if (m
.r
.rtm
.rtm_flags
& RTF_LLINFO
) {
1260 trace_act("ignore ARP %s", str
);
1264 if (get_info_gate(&INFO_GATE(&info
), &gate_sin
)) {
1265 gate
= S_ADDR(INFO_GATE(&info
));
1266 strp
+= sprintf(strp
, " --> %s", naddr_ntoa(gate
));
1271 if (INFO_AUTHOR(&info
) != 0)
1272 strp
+= sprintf(strp
, " by authority of %s",
1273 saddr_ntoa(INFO_AUTHOR(&info
)));
1275 switch (m
.r
.rtm
.rtm_type
) {
1279 if (m
.r
.rtm
.rtm_errno
!= 0) {
1280 trace_act("ignore %s with \"%s\" error",
1281 str
, strerror(m
.r
.rtm
.rtm_errno
));
1283 trace_act("%s", str
);
1284 rtm_add(&m
.r
.rtm
,&info
,0);
1289 if (m
.r
.rtm
.rtm_errno
!= 0
1290 && m
.r
.rtm
.rtm_errno
!= ESRCH
) {
1291 trace_act("ignore %s with \"%s\" error",
1292 str
, strerror(m
.r
.rtm
.rtm_errno
));
1294 trace_act("%s", str
);
1295 del_static(S_ADDR(INFO_DST(&info
)), mask
,
1301 trace_act("%s", str
);
1302 rtm_lose(&m
.r
.rtm
,&info
);
1306 trace_act("ignore %s", str
);
1313 /* after aggregating, note routes that belong in the kernel
1316 kern_out(struct ag_info
*ag
)
1321 /* Do not install bad routes if they are not already present.
1322 * This includes routes that had RS_NET_SYN for interfaces that
1325 if (ag
->ag_metric
== HOPCNT_INFINITY
) {
1326 k
= kern_find(htonl(ag
->ag_dst_h
), ag
->ag_mask
, 0);
1330 k
= kern_add(htonl(ag
->ag_dst_h
), ag
->ag_mask
);
1333 if (k
->k_state
& KS_NEW
) {
1334 /* will need to add new entry to the kernel table */
1335 k
->k_state
= KS_ADD
;
1336 if (ag
->ag_state
& AGS_GATEWAY
)
1337 k
->k_state
|= KS_GATEWAY
;
1338 k
->k_gate
= ag
->ag_gate
;
1339 k
->k_metric
= ag
->ag_metric
;
1343 if (k
->k_state
& KS_STATIC
)
1346 /* modify existing kernel entry if necessary */
1347 if (k
->k_gate
!= ag
->ag_gate
1348 || k
->k_metric
!= ag
->ag_metric
) {
1349 /* Must delete bad interface routes etc. to change them. */
1350 if (k
->k_metric
== HOPCNT_INFINITY
)
1351 k
->k_state
|= KS_DEL_ADD
;
1352 k
->k_gate
= ag
->ag_gate
;
1353 k
->k_metric
= ag
->ag_metric
;
1354 k
->k_state
|= KS_CHANGE
;
1357 /* If the daemon thinks the route should exist, forget
1358 * about any redirections.
1359 * If the daemon thinks the route should exist, eventually
1360 * override manual intervention by the operator.
1362 if ((k
->k_state
& (KS_DYNAMIC
| KS_DELETED
)) != 0) {
1363 k
->k_state
&= ~KS_DYNAMIC
;
1364 k
->k_state
|= (KS_ADD
| KS_DEL_ADD
);
1367 if ((k
->k_state
& KS_GATEWAY
)
1368 && !(ag
->ag_state
& AGS_GATEWAY
)) {
1369 k
->k_state
&= ~KS_GATEWAY
;
1370 k
->k_state
|= (KS_ADD
| KS_DEL_ADD
);
1371 } else 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
);
1377 /* Deleting-and-adding is necessary to change aspects of a route.
1378 * Just delete instead of deleting and then adding a bad route.
1379 * Otherwise, we want to keep the route in the kernel.
1381 if (k
->k_metric
== HOPCNT_INFINITY
1382 && (k
->k_state
& KS_DEL_ADD
))
1383 k
->k_state
|= KS_DELETE
;
1385 k
->k_state
&= ~KS_DELETE
;
1392 walk_kern(struct radix_node
*rn
,
1393 struct walkarg
*argp UNUSED
)
1395 #define RT ((struct rt_entry *)rn)
1400 /* Do not install synthetic routes */
1401 if (RT
->rt_state
& RS_NET_SYN
)
1404 if (!(RT
->rt_state
& RS_IF
)) {
1405 /* This is an ordinary route, not for an interface.
1408 /* aggregate, ordinary good routes without regard to
1412 ags
|= (AGS_GATEWAY
| AGS_SUPPRESS
| AGS_AGGREGATE
);
1414 /* Do not install host routes directly to hosts, to avoid
1415 * interfering with ARP entries in the kernel table.
1418 && ntohl(RT
->rt_dst
) == RT
->rt_gate
)
1422 /* This is an interface route.
1423 * Do not install routes for "external" remote interfaces.
1425 if (RT
->rt_ifp
!= 0 && (RT
->rt_ifp
->int_state
& IS_EXTERNAL
))
1428 /* Interfaces should override received routes.
1431 ags
|= (AGS_IF
| AGS_CORS_GATE
);
1433 /* If it is not an interface, or an alias for an interface,
1434 * it must be a "gateway."
1436 * If it is a "remote" interface, it is also a "gateway" to
1437 * the kernel if is not a alias.
1440 || (RT
->rt_ifp
->int_state
& IS_REMOTE
))
1441 ags
|= (AGS_GATEWAY
| AGS_SUPPRESS
| AGS_AGGREGATE
);
1444 /* If RIP is off and IRDP is on, let the route to the discovered
1445 * route suppress any RIP routes. Eventually the RIP routes
1446 * will time-out and be deleted. This reaches the steady-state
1449 if ((RT
->rt_state
& RS_RDISC
) && rip_sock
< 0)
1450 ags
|= AGS_CORS_GATE
;
1452 metric
= RT
->rt_metric
;
1453 if (metric
== HOPCNT_INFINITY
) {
1454 /* if the route is dead, so try hard to aggregate. */
1455 pref
= HOPCNT_INFINITY
;
1456 ags
|= (AGS_FINE_GATE
| AGS_SUPPRESS
);
1457 ags
&= ~(AGS_IF
| AGS_CORS_GATE
);
1460 ag_check(RT
->rt_dst
, RT
->rt_mask
, RT
->rt_gate
, 0,
1461 metric
,pref
, 0, 0, ags
, kern_out
);
1467 /* Update the kernel table to match the daemon table.
1473 struct khash
*k
, **pk
;
1476 need_kern
= age_timer
;
1478 /* Walk daemon table, updating the copy of the kernel table.
1480 rn_walktree(rhead
, walk_kern
, 0);
1481 ag_flush(0,0,kern_out
);
1483 for (i
= 0; i
< KHASH_SIZE
; i
++) {
1484 for (pk
= &khash_bins
[i
]; (k
= *pk
) != NULL
; ) {
1485 /* Do not touch static routes */
1486 if (k
->k_state
& KS_STATIC
) {
1487 kern_check_static(k
,0);
1492 /* check hold on routes deleted by the operator */
1493 if (k
->k_keep
> now
.tv_sec
) {
1494 /* ensure we check when the hold is over */
1495 LIM_SEC(need_kern
, k
->k_keep
);
1496 /* mark for the next cycle */
1497 k
->k_state
|= KS_DELETE
;
1502 if ((k
->k_state
& KS_DELETE
)
1503 && !(k
->k_state
& KS_DYNAMIC
)) {
1504 kern_ioctl(k
, RTM_DELETE
, 0);
1510 if (k
->k_state
& KS_DEL_ADD
)
1511 kern_ioctl(k
, RTM_DELETE
, 0);
1513 if (k
->k_state
& KS_ADD
) {
1514 kern_ioctl(k
, RTM_ADD
,
1515 ((0 != (k
->k_state
& (KS_GATEWAY
1517 ? RTF_GATEWAY
: 0));
1518 } else if (k
->k_state
& KS_CHANGE
) {
1519 kern_ioctl(k
, RTM_CHANGE
,
1520 ((0 != (k
->k_state
& (KS_GATEWAY
1522 ? RTF_GATEWAY
: 0));
1524 k
->k_state
&= ~(KS_ADD
|KS_CHANGE
|KS_DEL_ADD
);
1526 /* Mark this route to be deleted in the next cycle.
1527 * This deletes routes that disappear from the
1528 * daemon table, since the normal aging code
1529 * will clear the bit for routes that have not
1530 * disappeared from the daemon table.
1532 k
->k_state
|= KS_DELETE
;
1539 /* Delete a static route in the image of the kernel table.
1542 del_static(naddr dst
,
1548 struct rt_entry
*rt
;
1550 /* Just mark it in the table to be deleted next time the kernel
1552 * If it has already been deleted, mark it as such, and set its
1553 * keep-timer so that it will not be deleted again for a while.
1554 * This lets the operator delete a route added by the daemon
1555 * and add a replacement.
1557 k
= kern_find(dst
, mask
, 0);
1558 if (k
!= NULL
&& (gate
== 0 || k
->k_gate
== gate
)) {
1559 k
->k_state
&= ~(KS_STATIC
| KS_DYNAMIC
| KS_CHECK
);
1560 k
->k_state
|= KS_DELETE
;
1562 k
->k_state
|= KS_DELETED
;
1563 k
->k_keep
= now
.tv_sec
+ K_KEEP_LIM
;
1567 rt
= rtget(dst
, mask
);
1568 if (rt
!= NULL
&& (rt
->rt_state
& RS_STATIC
))
1573 /* Delete all routes generated from ICMP Redirects that use a given gateway,
1574 * as well as old redirected routes.
1577 del_redirects(naddr bad_gate
,
1584 for (i
= 0; i
< KHASH_SIZE
; i
++) {
1585 for (k
= khash_bins
[i
]; k
!= NULL
; k
= k
->k_next
) {
1586 if (!(k
->k_state
& KS_DYNAMIC
)
1587 || (k
->k_state
& KS_STATIC
))
1590 if (k
->k_gate
!= bad_gate
1591 && k
->k_redirect_time
> old
1595 k
->k_state
|= KS_DELETE
;
1596 k
->k_state
&= ~KS_DYNAMIC
;
1597 need_kern
.tv_sec
= now
.tv_sec
;
1598 trace_act("mark redirected %s --> %s for deletion",
1599 addrname(k
->k_dst
, k
->k_mask
, 0),
1600 naddr_ntoa(k
->k_gate
));
1606 /* Start the daemon tables.
1608 extern int max_keylen
;
1616 /* Initialize the radix trees */
1617 max_keylen
= sizeof(struct sockaddr_in
);
1619 rn_inithead(&rhead
, 32);
1621 /* mark all of the slots in the table free */
1622 ag_avail
= ag_slots
;
1623 for (ag
= ag_slots
, i
= 1; i
< NUM_AG_SLOTS
; i
++) {
1630 #ifdef _HAVE_SIN_LEN
1631 static struct sockaddr_in dst_sock
= {sizeof(dst_sock
), AF_INET
, 0, {0}, {0}};
1632 static struct sockaddr_in mask_sock
= {sizeof(mask_sock
), AF_INET
, 0, {0}, {0}};
1634 static struct sockaddr_in_new dst_sock
= {_SIN_ADDR_SIZE
, AF_INET
};
1635 static struct sockaddr_in_new mask_sock
= {_SIN_ADDR_SIZE
, AF_INET
};
1640 set_need_flash(void)
1644 /* Do not send the flash update immediately. Wait a little
1645 * while to hear from other routers.
1647 no_flash
.tv_sec
= now
.tv_sec
+ MIN_WAITTIME
;
1652 /* Get a particular routing table entry
1655 rtget(naddr dst
, naddr mask
)
1657 struct rt_entry
*rt
;
1659 dst_sock
.sin_addr
.s_addr
= dst
;
1660 mask_sock
.sin_addr
.s_addr
= htonl(mask
);
1661 masktrim(&mask_sock
);
1662 rt
= (struct rt_entry
*)rhead
->rnh_lookup(&dst_sock
,&mask_sock
,rhead
);
1664 || rt
->rt_dst
!= dst
1665 || rt
->rt_mask
!= mask
)
1672 /* Find a route to dst as the kernel would.
1677 dst_sock
.sin_addr
.s_addr
= dst
;
1678 return (struct rt_entry
*)rhead
->rnh_matchaddr(&dst_sock
, rhead
);
1682 /* add a route to the table
1687 u_int state
, /* rt_state for the entry */
1688 struct rt_spare
*new)
1690 struct rt_entry
*rt
;
1693 struct rt_spare
*rts
;
1695 rt
= (struct rt_entry
*)rtmalloc(sizeof (*rt
), "rtadd");
1696 memset(rt
, 0, sizeof(*rt
));
1697 for (rts
= rt
->rt_spares
, i
= NUM_SPARES
; i
!= 0; i
--, rts
++)
1698 rts
->rts_metric
= HOPCNT_INFINITY
;
1700 rt
->rt_nodes
->rn_key
= (caddr_t
)&rt
->rt_dst_sock
;
1702 rt
->rt_dst_sock
.sin_family
= AF_INET
;
1703 #ifdef _HAVE_SIN_LEN
1704 rt
->rt_dst_sock
.sin_len
= dst_sock
.sin_len
;
1706 if (mask
!= HOST_MASK
) {
1707 smask
= std_mask(dst
);
1708 if ((smask
& ~mask
) == 0 && mask
> smask
)
1711 mask_sock
.sin_addr
.s_addr
= htonl(mask
);
1712 masktrim(&mask_sock
);
1714 rt
->rt_state
= state
;
1715 rt
->rt_spares
[0] = *new;
1716 rt
->rt_time
= now
.tv_sec
;
1717 rt
->rt_poison_metric
= HOPCNT_INFINITY
;
1718 rt
->rt_seqno
= update_seqno
;
1720 if (++total_routes
== MAX_ROUTES
)
1721 msglog("have maximum (%d) routes", total_routes
);
1723 trace_add_del("Add", rt
);
1725 need_kern
.tv_sec
= now
.tv_sec
;
1728 if (0 == rhead
->rnh_addaddr(&rt
->rt_dst_sock
, &mask_sock
,
1729 rhead
, rt
->rt_nodes
)) {
1730 msglog("rnh_addaddr() failed for %s mask=%#lx",
1731 naddr_ntoa(dst
), (u_long
)mask
);
1737 /* notice a changed route
1740 rtchange(struct rt_entry
*rt
,
1741 u_int state
, /* new state bits */
1742 struct rt_spare
*new,
1745 if (rt
->rt_metric
!= new->rts_metric
) {
1746 /* Fix the kernel immediately if it seems the route
1747 * has gone bad, since there may be a working route that
1748 * aggregates this route.
1750 if (new->rts_metric
== HOPCNT_INFINITY
) {
1751 need_kern
.tv_sec
= now
.tv_sec
;
1752 if (new->rts_time
>= now
.tv_sec
- EXPIRE_TIME
)
1753 new->rts_time
= now
.tv_sec
- EXPIRE_TIME
;
1755 rt
->rt_seqno
= update_seqno
;
1759 if (rt
->rt_gate
!= new->rts_gate
) {
1760 need_kern
.tv_sec
= now
.tv_sec
;
1761 rt
->rt_seqno
= update_seqno
;
1765 state
|= (rt
->rt_state
& RS_SUBNET
);
1767 /* Keep various things from deciding ageless routes are stale.
1769 if (!AGE_RT(state
, new->rts_ifp
))
1770 new->rts_time
= now
.tv_sec
;
1773 trace_change(rt
, state
, new,
1774 label
? label
: "Chg ");
1776 rt
->rt_state
= state
;
1777 rt
->rt_spares
[0] = *new;
1781 /* check for a better route among the spares
1783 static struct rt_spare
*
1784 rts_better(struct rt_entry
*rt
)
1786 struct rt_spare
*rts
, *rts1
;
1789 /* find the best alternative among the spares */
1790 rts
= rt
->rt_spares
+1;
1791 for (i
= NUM_SPARES
, rts1
= rts
+1; i
> 2; i
--, rts1
++) {
1792 if (BETTER_LINK(rt
,rts1
,rts
))
1800 /* switch to a backup route
1803 rtswitch(struct rt_entry
*rt
,
1804 struct rt_spare
*rts
)
1806 struct rt_spare swap
;
1810 /* Do not change permanent routes */
1811 if (0 != (rt
->rt_state
& (RS_MHOME
| RS_STATIC
| RS_RDISC
1812 | RS_NET_SYN
| RS_IF
)))
1815 /* find the best alternative among the spares */
1817 rts
= rts_better(rt
);
1819 /* Do not bother if it is not worthwhile.
1821 if (!BETTER_LINK(rt
, rts
, rt
->rt_spares
))
1824 swap
= rt
->rt_spares
[0];
1825 sprintf(label
, "Use #%d", (int)(rts
- rt
->rt_spares
));
1826 rtchange(rt
, rt
->rt_state
& ~(RS_NET_SYN
| RS_RDISC
), rts
, label
);
1827 if (swap
.rts_metric
== HOPCNT_INFINITY
) {
1836 rtdelete(struct rt_entry
*rt
)
1842 trace_add_del("Del", rt
);
1844 k
= kern_find(rt
->rt_dst
, rt
->rt_mask
, 0);
1846 k
->k_state
|= KS_DELETE
;
1847 need_kern
.tv_sec
= now
.tv_sec
;
1850 dst_sock
.sin_addr
.s_addr
= rt
->rt_dst
;
1851 mask_sock
.sin_addr
.s_addr
= htonl(rt
->rt_mask
);
1852 masktrim(&mask_sock
);
1853 if (rt
!= (struct rt_entry
*)rhead
->rnh_deladdr(&dst_sock
, &mask_sock
,
1855 msglog("rnh_deladdr() failed");
1864 rts_delete(struct rt_entry
*rt
,
1865 struct rt_spare
*rts
)
1867 trace_upslot(rt
, rts
, &rts_empty
);
1872 /* Get rid of a bad route, and try to switch to a replacement.
1875 rtbad(struct rt_entry
*rt
)
1877 struct rt_spare
new;
1879 /* Poison the route */
1880 new = rt
->rt_spares
[0];
1881 new.rts_metric
= HOPCNT_INFINITY
;
1882 rtchange(rt
, rt
->rt_state
& ~(RS_IF
| RS_LOCAL
| RS_STATIC
), &new, 0);
1887 /* Junk a RS_NET_SYN or RS_LOCAL route,
1888 * unless it is needed by another interface.
1891 rtbad_sub(struct rt_entry
*rt
)
1893 struct interface
*ifp
, *ifp1
;
1894 struct intnet
*intnetp
;
1901 if (rt
->rt_state
& RS_LOCAL
) {
1902 /* Is this the route through loopback for the interface?
1903 * If so, see if it is used by any other interfaces, such
1904 * as a point-to-point interface with the same local address.
1906 for (ifp
= ifnet
; ifp
!= NULL
; ifp
= ifp
->int_next
) {
1907 /* Retain it if another interface needs it.
1909 if (ifp
->int_addr
== rt
->rt_ifp
->int_addr
) {
1918 if (!(state
& RS_LOCAL
)) {
1919 /* Retain RIPv1 logical network route if there is another
1920 * interface that justifies it.
1922 if (rt
->rt_state
& RS_NET_SYN
) {
1923 for (ifp
= ifnet
; ifp
!= NULL
; ifp
= ifp
->int_next
) {
1924 if ((ifp
->int_state
& IS_NEED_NET_SYN
)
1925 && rt
->rt_mask
== ifp
->int_std_mask
1926 && rt
->rt_dst
== ifp
->int_std_addr
) {
1927 state
|= RS_NET_SYN
;
1934 /* or if there is an authority route that needs it. */
1935 for (intnetp
= intnets
;
1937 intnetp
= intnetp
->intnet_next
) {
1938 if (intnetp
->intnet_addr
== rt
->rt_dst
1939 && intnetp
->intnet_mask
== rt
->rt_mask
) {
1940 state
|= (RS_NET_SYN
| RS_NET_INT
);
1946 if (ifp1
!= NULL
|| (state
& RS_NET_SYN
)) {
1947 struct rt_spare
new = rt
->rt_spares
[0];
1949 rtchange(rt
, ((rt
->rt_state
& ~(RS_NET_SYN
|RS_LOCAL
)) | state
),
1957 /* Called while walking the table looking for sick interfaces
1958 * or after a time change.
1962 walk_bad(struct radix_node
*rn
,
1963 struct walkarg
*argp UNUSED
)
1965 #define RT ((struct rt_entry *)rn)
1966 struct rt_spare
*rts
;
1970 /* fix any spare routes through the interface
1972 rts
= RT
->rt_spares
;
1973 for (i
= NUM_SPARES
; i
!= 1; i
--) {
1975 if (rts
->rts_metric
< HOPCNT_INFINITY
1976 && (rts
->rts_ifp
== 0
1977 || (rts
->rts_ifp
->int_state
& IS_BROKE
)))
1978 rts_delete(RT
, rts
);
1981 /* Deal with the main route
1983 /* finished if it has been handled before or if its interface is ok
1985 if (RT
->rt_ifp
== 0 || !(RT
->rt_ifp
->int_state
& IS_BROKE
))
1988 /* Bad routes for other than interfaces are easy.
1990 if (0 == (RT
->rt_state
& (RS_IF
| RS_NET_SYN
| RS_LOCAL
))) {
2001 /* Check the age of an individual route.
2005 walk_age(struct radix_node
*rn
,
2006 struct walkarg
*argp UNUSED
)
2008 #define RT ((struct rt_entry *)rn)
2009 struct interface
*ifp
;
2010 struct rt_spare
*rts
;
2014 /* age all of the spare routes, including the primary route
2017 rts
= RT
->rt_spares
;
2018 for (i
= NUM_SPARES
; i
!= 0; i
--, rts
++) {
2021 if (i
== NUM_SPARES
) {
2022 if (!AGE_RT(RT
->rt_state
, ifp
)) {
2023 /* Keep various things from deciding ageless
2026 rts
->rts_time
= now
.tv_sec
;
2030 /* forget RIP routes after RIP has been turned off.
2038 /* age failing routes
2040 if (age_bad_gate
== rts
->rts_gate
2041 && rts
->rts_time
>= now_stale
) {
2042 rts
->rts_time
-= SUPPLY_INTERVAL
;
2045 /* trash the spare routes when they go bad */
2046 if (rts
->rts_metric
< HOPCNT_INFINITY
2047 && now_garbage
> rts
->rts_time
2049 rts_delete(RT
, rts
);
2053 /* finished if the active route is still fresh */
2054 if (now_stale
<= RT
->rt_time
)
2057 /* try to switch to an alternative */
2060 /* Delete a dead route after it has been publically mourned. */
2061 if (now_garbage
> RT
->rt_time
) {
2066 /* Start poisoning a bad route before deleting it. */
2067 if (now
.tv_sec
- RT
->rt_time
> EXPIRE_TIME
) {
2068 struct rt_spare
new = RT
->rt_spares
[0];
2069 new.rts_metric
= HOPCNT_INFINITY
;
2070 rtchange(RT
, RT
->rt_state
, &new, 0);
2076 /* Watch for dead routes and interfaces.
2081 struct interface
*ifp
;
2084 /* If not listening to RIP, there is no need to age the routes in
2087 age_timer
.tv_sec
= (now
.tv_sec
2088 + ((rip_sock
< 0) ? NEVER
: SUPPLY_INTERVAL
));
2090 /* Check for dead IS_REMOTE interfaces by timing their
2093 for (ifp
= ifnet
; ifp
; ifp
= ifp
->int_next
) {
2094 if (!(ifp
->int_state
& IS_REMOTE
))
2097 /* ignore unreachable remote interfaces */
2098 if (!check_remote(ifp
))
2101 /* Restore remote interface that has become reachable
2103 if (ifp
->int_state
& IS_BROKE
)
2104 if_ok(ifp
, "remote ");
2106 if (ifp
->int_act_time
!= NEVER
2107 && now
.tv_sec
- ifp
->int_act_time
> EXPIRE_TIME
) {
2108 msglog("remote interface %s to %s timed out after"
2111 naddr_ntoa(ifp
->int_dstaddr
),
2112 (now
.tv_sec
- ifp
->int_act_time
)/60,
2113 (now
.tv_sec
- ifp
->int_act_time
)%60);
2117 /* If we have not heard from the other router
2120 if (now
.tv_sec
>= ifp
->int_query_time
) {
2121 ifp
->int_query_time
= NEVER
;
2127 age_bad_gate
= bad_gate
;
2128 rn_walktree(rhead
, walk_age
, 0);
2130 /* delete old redirected routes to keep the kernel table small
2131 * and prevent blackholes
2133 del_redirects(bad_gate
, now
.tv_sec
-STALE_TIME
);
2135 /* Update the kernel routing table. */
2138 /* poke reticent remote gateways */