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. 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.
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
29 * $FreeBSD: src/sbin/routed/table.c,v 1.9.2.2 2000/08/14 17:00:04 sheldonh Exp $
34 #if !defined(__NetBSD__)
35 static char sccsid
[] __attribute__((unused
)) = "@(#)tables.c 8.1 (Berkeley) 6/5/93";
36 #elif defined(__NetBSD__)
40 static struct rt_spare
*rts_better(struct rt_entry
*);
41 static struct rt_spare rts_empty
= {0,0,0,HOPCNT_INFINITY
,0,0,0};
42 static void set_need_flash(void);
44 static void masktrim(struct sockaddr_in
*ap
);
46 static void masktrim(struct sockaddr_in_new
*ap
);
50 struct radix_node_head
*rhead
; /* root of the radix tree */
52 int need_flash
= 1; /* flash update needed
53 * start =1 to suppress the 1st
56 struct timeval age_timer
; /* next check of old routes */
57 struct timeval need_kern
= { /* need to update kernel table */
58 EPOCH
+MIN_WAITTIME
-1, 0
65 /* zap any old routes through this gateway */
69 /* It is desirable to "aggregate" routes, to combine differing routes of
70 * the same metric and next hop into a common route with a smaller netmask
71 * or to suppress redundant routes, routes that add no information to
72 * routes with smaller netmasks.
74 * A route is redundant if and only if any and all routes with smaller
75 * but matching netmasks and nets are the same. Since routes are
76 * kept sorted in the radix tree, redundant routes always come second.
78 * There are two kinds of aggregations. First, two routes of the same bit
79 * mask and differing only in the least significant bit of the network
80 * number can be combined into a single route with a coarser mask.
82 * Second, a route can be suppressed in favor of another route with a more
83 * coarse mask provided no incompatible routes with intermediate masks
84 * are present. The second kind of aggregation involves suppressing routes.
85 * A route must not be suppressed if an incompatible route exists with
86 * an intermediate mask, since the suppressed route would be covered
87 * by the intermediate.
89 * This code relies on the radix tree walk encountering routes
90 * sorted first by address, with the smallest address first.
93 struct ag_info ag_slots
[NUM_AG_SLOTS
], *ag_avail
, *ag_corsest
, *ag_finest
;
95 /* #define DEBUG_AG */
97 #define CHECK_AG() {int acnt = 0; struct ag_info *cag; \
98 for (cag = ag_avail; cag != NULL; cag = cag->ag_fine) \
100 for (cag = ag_corsest; cag != NULL; cag = cag->ag_fine) \
102 if (acnt != NUM_AG_SLOTS) { \
112 /* Output the contents of an aggregation table slot.
113 * This function must always be immediately followed with the deletion
114 * of the target slot.
117 ag_out(struct ag_info
*ag
,
118 void (*out
)(struct ag_info
*))
120 struct ag_info
*ag_cors
;
124 /* Forget it if this route should not be output for split-horizon. */
125 if (ag
->ag_state
& AGS_SPLIT_HZ
)
128 /* If we output both the even and odd twins, then the immediate parent,
129 * if it is present, is redundant, unless the parent manages to
130 * aggregate into something coarser.
131 * On successive calls, this code detects the even and odd twins,
132 * and marks the parent.
134 * Note that the order in which the radix tree code emits routes
135 * ensures that the twins are seen before the parent is emitted.
137 ag_cors
= ag
->ag_cors
;
139 && ag_cors
->ag_mask
== ag
->ag_mask
<<1
140 && ag_cors
->ag_dst_h
== (ag
->ag_dst_h
& ag_cors
->ag_mask
)) {
141 ag_cors
->ag_state
|= ((ag_cors
->ag_dst_h
== ag
->ag_dst_h
)
146 /* Skip it if this route is itself redundant.
148 * It is ok to change the contents of the slot here, since it is
149 * always deleted next.
151 if (ag
->ag_state
& AGS_REDUN0
) {
152 if (ag
->ag_state
& AGS_REDUN1
)
153 return; /* quit if fully redundant */
154 /* make it finer if it is half-redundant */
155 bit
= (-ag
->ag_mask
) >> 1;
159 } else if (ag
->ag_state
& AGS_REDUN1
) {
160 /* make it finer if it is half-redundant */
161 bit
= (-ag
->ag_mask
) >> 1;
169 ag_del(struct ag_info
*ag
)
173 if (ag
->ag_cors
== 0)
174 ag_corsest
= ag
->ag_fine
;
176 ag
->ag_cors
->ag_fine
= ag
->ag_fine
;
178 if (ag
->ag_fine
== 0)
179 ag_finest
= ag
->ag_cors
;
181 ag
->ag_fine
->ag_cors
= ag
->ag_cors
;
183 ag
->ag_fine
= ag_avail
;
190 /* Flush routes waiting for aggregation.
191 * This must not suppress a route unless it is known that among all
192 * routes with coarser masks that match it, the one with the longest
193 * mask is appropriate. This is ensured by scanning the routes
194 * in lexical order, and with the most restrictive mask first
195 * among routes to the same destination.
198 ag_flush(naddr lim_dst_h
, /* flush routes to here */
199 naddr lim_mask
, /* matching this mask */
200 void (*out
)(struct ag_info
*))
202 struct ag_info
*ag
, *ag_cors
;
207 ag
!= NULL
&& ag
->ag_mask
>= lim_mask
;
209 ag_cors
= ag
->ag_cors
;
211 /* work on only the specified routes */
212 dst_h
= ag
->ag_dst_h
;
213 if ((dst_h
& lim_mask
) != lim_dst_h
)
216 if (!(ag
->ag_state
& AGS_SUPPRESS
))
219 else for ( ; ; ag_cors
= ag_cors
->ag_cors
) {
220 /* Look for a route that can suppress the
222 if (ag_cors
== NULL
) {
223 /* failed, so output it and look for
224 * another route to work on
230 if ((dst_h
& ag_cors
->ag_mask
) == ag_cors
->ag_dst_h
) {
231 /* We found a route with a coarser mask that
232 * aggregates the current target.
234 * If it has a different next hop, it
235 * cannot replace the target, so output
238 if (ag
->ag_gate
!= ag_cors
->ag_gate
239 && !(ag
->ag_state
& AGS_FINE_GATE
)
240 && !(ag_cors
->ag_state
& AGS_CORS_GATE
)) {
245 /* If the coarse route has a good enough
246 * metric, it suppresses the target.
247 * If the suppressed target was redundant,
248 * then mark the suppressor redundant.
250 if (ag_cors
->ag_pref
<= ag
->ag_pref
) {
251 if (ag_cors
->ag_seqno
> ag
->ag_seqno
)
252 ag_cors
->ag_seqno
= ag
->ag_seqno
;
253 if (AG_IS_REDUN(ag
->ag_state
)
254 && ag_cors
->ag_mask
==ag
->ag_mask
<<1) {
255 if (ag_cors
->ag_dst_h
== dst_h
)
256 ag_cors
->ag_state
|= AGS_REDUN0
;
258 ag_cors
->ag_state
|= AGS_REDUN1
;
260 if (ag
->ag_tag
!= ag_cors
->ag_tag
)
262 if (ag
->ag_nhop
!= ag_cors
->ag_nhop
)
263 ag_cors
->ag_nhop
= 0;
269 /* That route has either been output or suppressed */
270 ag_cors
= ag
->ag_cors
;
278 /* Try to aggregate a route with previous routes.
290 void (*out
)(struct ag_info
*)) /* output using this */
292 struct ag_info
*ag
, *nag
, *ag_cors
;
298 /* Punt non-contiguous subnet masks.
300 * (X & -X) contains a single bit if and only if X is a power of 2.
301 * (X + (X & -X)) == 0 if and only if X is a power of 2.
303 if ((mask
& -mask
) + mask
!= 0) {
304 struct ag_info nc_ag
;
306 nc_ag
.ag_dst_h
= dst
;
307 nc_ag
.ag_mask
= mask
;
308 nc_ag
.ag_gate
= gate
;
309 nc_ag
.ag_nhop
= nhop
;
310 nc_ag
.ag_metric
= metric
;
311 nc_ag
.ag_pref
= pref
;
313 nc_ag
.ag_state
= state
;
314 nc_ag
.ag_seqno
= seqnum
;
319 /* Search for the right slot in the aggregation table.
324 if (ag
->ag_mask
>= mask
)
327 /* Suppress old routes (i.e. combine with compatible routes
328 * with coarser masks) as we look for the right slot in the
329 * aggregation table for the new route.
330 * A route to an address less than the current destination
331 * will not be affected by the current route or any route
332 * seen hereafter. That means it is safe to suppress it.
333 * This check keeps poor routes (e.g. with large hop counts)
334 * from preventing suppression of finer routes.
337 && ag
->ag_dst_h
< dst
338 && (ag
->ag_state
& AGS_SUPPRESS
)
339 && ag_cors
->ag_pref
<= ag
->ag_pref
340 && (ag
->ag_dst_h
& ag_cors
->ag_mask
) == ag_cors
->ag_dst_h
341 && (ag_cors
->ag_gate
== ag
->ag_gate
342 || (ag
->ag_state
& AGS_FINE_GATE
)
343 || (ag_cors
->ag_state
& AGS_CORS_GATE
))) {
344 if (ag_cors
->ag_seqno
> ag
->ag_seqno
)
345 ag_cors
->ag_seqno
= ag
->ag_seqno
;
346 /* If the suppressed target was redundant,
347 * then mark the suppressor redundant.
349 if (AG_IS_REDUN(ag
->ag_state
)
350 && ag_cors
->ag_mask
== ag
->ag_mask
<<1) {
351 if (ag_cors
->ag_dst_h
== dst
)
352 ag_cors
->ag_state
|= AGS_REDUN0
;
354 ag_cors
->ag_state
|= AGS_REDUN1
;
356 if (ag
->ag_tag
!= ag_cors
->ag_tag
)
358 if (ag
->ag_nhop
!= ag_cors
->ag_nhop
)
359 ag_cors
->ag_nhop
= 0;
365 ag
= ag_cors
->ag_fine
;
368 /* If we find the even/odd twin of the new route, and if the
369 * masks and so forth are equal, we can aggregate them.
370 * We can probably promote one of the pair.
372 * Since the routes are encountered in lexical order,
373 * the new route must be odd. However, the second or later
374 * times around this loop, it could be the even twin promoted
375 * from the even/odd pair of twins of the finer route.
378 && ag
->ag_mask
== mask
379 && ((ag
->ag_dst_h
^ dst
) & (mask
<<1)) == 0) {
381 /* Here we know the target route and the route in the current
382 * slot have the same netmasks and differ by at most the
383 * last bit. They are either for the same destination, or
384 * for an even/odd pair of destinations.
386 if (ag
->ag_dst_h
== dst
) {
387 /* We have two routes to the same destination.
388 * Routes are encountered in lexical order, so a
389 * route is never promoted until the parent route is
390 * already present. So we know that the new route is
391 * a promoted (or aggregated) pair and the route
392 * already in the slot is the explicit route.
394 * Prefer the best route if their metrics differ,
395 * or the aggregated one if not, following a sort
396 * of longest-match rule.
398 if (pref
<= ag
->ag_pref
) {
402 ag
->ag_metric
= metric
;
405 ag
->ag_state
= state
;
409 /* The sequence number controls flash updating,
410 * and should be the smaller of the two.
412 if (ag
->ag_seqno
> seqnum
)
413 ag
->ag_seqno
= seqnum
;
415 /* Some bits are set if they are set on either route,
416 * except when the route is for an interface.
418 if (!(ag
->ag_state
& AGS_IF
))
419 ag
->ag_state
|= (state
& (AGS_AGGREGATE_EITHER
425 /* If one of the routes can be promoted and the other can
426 * be suppressed, it may be possible to combine them or
427 * worthwhile to promote one.
429 * Any route that can be promoted is always
430 * marked to be eligible to be suppressed.
432 if (!((state
& AGS_AGGREGATE
)
433 && (ag
->ag_state
& AGS_SUPPRESS
))
434 && !((ag
->ag_state
& AGS_AGGREGATE
)
435 && (state
& AGS_SUPPRESS
)))
438 /* A pair of even/odd twin routes can be combined
439 * if either is redundant, or if they are via the
440 * same gateway and have the same metric.
442 if (AG_IS_REDUN(ag
->ag_state
)
443 || AG_IS_REDUN(state
)
444 || (ag
->ag_gate
== gate
445 && ag
->ag_pref
== pref
446 && (state
& ag
->ag_state
& AGS_AGGREGATE
) != 0)) {
448 /* We have both the even and odd pairs.
449 * Since the routes are encountered in order,
450 * the route in the slot must be the even twin.
452 * Combine and promote (aggregate) the pair of routes.
454 if (seqnum
> ag
->ag_seqno
)
455 seqnum
= ag
->ag_seqno
;
456 if (!AG_IS_REDUN(state
))
457 state
&= ~AGS_REDUN1
;
458 if (AG_IS_REDUN(ag
->ag_state
))
461 state
&= ~AGS_REDUN0
;
462 state
|= (ag
->ag_state
& AGS_AGGREGATE_EITHER
);
463 if (ag
->ag_tag
!= tag
)
465 if (ag
->ag_nhop
!= nhop
)
468 /* Get rid of the even twin that was already
473 } else if (ag
->ag_pref
>= pref
474 && (ag
->ag_state
& AGS_AGGREGATE
)) {
475 /* If we cannot combine the pair, maybe the route
476 * with the worse metric can be promoted.
478 * Promote the old, even twin, by giving its slot
479 * in the table to the new, odd twin.
495 /* The promoted route is even-redundant only if the
496 * even twin was fully redundant. It is not
497 * odd-redundant because the odd-twin will still be
504 ag
->ag_state
= state
;
508 ag
->ag_metric
= metric
;
515 /* take the newest sequence number */
516 if (seqnum
>= ag
->ag_seqno
)
517 seqnum
= ag
->ag_seqno
;
519 ag
->ag_seqno
= seqnum
;
522 if (!(state
& AGS_AGGREGATE
))
523 break; /* cannot promote either twin */
525 /* Promote the new, odd twin by shaving its
527 * The promoted route is odd-redundant only if the
528 * odd twin was fully redundant. It is not
529 * even-redundant because the even twin is still in
532 if (!AG_IS_REDUN(state
))
533 state
&= ~AGS_REDUN1
;
534 state
&= ~AGS_REDUN0
;
535 if (seqnum
> ag
->ag_seqno
)
536 seqnum
= ag
->ag_seqno
;
538 ag
->ag_seqno
= seqnum
;
544 if (ag_cors
== NULL
) {
549 ag_cors
= ag
->ag_cors
;
552 /* When we can no longer promote and combine routes,
553 * flush the old route in the target slot. Also flush
554 * any finer routes that we know will never be aggregated by
557 * In case we moved toward coarser masks,
558 * get back where we belong
561 && ag
->ag_mask
< mask
) {
566 /* Empty the target slot
568 if (ag
!= NULL
&& ag
->ag_mask
== mask
) {
569 ag_flush(ag
->ag_dst_h
, ag
->ag_mask
, out
);
570 ag
= (ag_cors
== NULL
) ? ag_corsest
: ag_cors
->ag_fine
;
575 if (ag
== NULL
&& ag_cors
!= ag_finest
)
577 if (ag_cors
== NULL
&& ag
!= ag_corsest
)
579 if (ag
!= NULL
&& ag
->ag_cors
!= ag_cors
)
581 if (ag_cors
!= NULL
&& ag_cors
->ag_fine
!= ag
)
586 /* Save the new route on the end of the table.
589 ag_avail
= nag
->ag_fine
;
595 nag
->ag_metric
= metric
;
598 nag
->ag_state
= state
;
599 nag
->ag_seqno
= seqnum
;
606 nag
->ag_cors
= ag_cors
;
610 ag_cors
->ag_fine
= nag
;
617 rtm_type_name(u_char type
)
619 static const char *rtm_types
[] = {
637 #define NEW_RTM_PAT "RTM type %#x"
638 static char name0
[sizeof(NEW_RTM_PAT
)+2];
641 if (type
> sizeof(rtm_types
)/sizeof(rtm_types
[0])
643 snprintf(name0
, sizeof(name0
), NEW_RTM_PAT
, type
);
646 return rtm_types
[type
-1];
652 /* Trim a mask in a sockaddr
653 * Produce a length of 0 for an address of 0.
654 * Otherwise produce the index of the first zero byte.
658 masktrim(struct sockaddr_in
*ap
)
660 masktrim(struct sockaddr_in_new
*ap
)
665 if (ap
->sin_addr
.s_addr
== 0) {
669 cp
= (char *)(&ap
->sin_addr
.s_addr
+1);
672 ap
->sin_len
= cp
- (char*)ap
+ 1;
676 /* Tell the kernel to add, delete or change a route
679 rtioctl(int action
, /* RTM_DELETE, etc */
687 struct rt_msghdr w_rtm
;
688 struct sockaddr_in w_dst
;
689 struct sockaddr_in w_gate
;
691 struct sockaddr_in w_mask
;
693 struct sockaddr_in_new w_mask
;
697 # define PAT " %-10s %s metric=%d flags=%#x"
698 # define ARGS rtm_type_name(action), rtname(dst,mask,gate), metric, flags
701 memset(&w
, 0, sizeof(w
));
702 w
.w_rtm
.rtm_msglen
= sizeof(w
);
703 w
.w_rtm
.rtm_version
= RTM_VERSION
;
704 w
.w_rtm
.rtm_type
= action
;
705 w
.w_rtm
.rtm_flags
= flags
;
706 w
.w_rtm
.rtm_seq
= ++rt_sock_seqno
;
707 w
.w_rtm
.rtm_addrs
= RTA_DST
|RTA_GATEWAY
;
708 if (metric
!= 0 || action
== RTM_CHANGE
) {
709 w
.w_rtm
.rtm_rmx
.rmx_hopcount
= metric
;
710 w
.w_rtm
.rtm_inits
|= RTV_HOPCOUNT
;
712 w
.w_dst
.sin_family
= AF_INET
;
713 w
.w_dst
.sin_addr
.s_addr
= dst
;
714 w
.w_gate
.sin_family
= AF_INET
;
715 w
.w_gate
.sin_addr
.s_addr
= gate
;
717 w
.w_dst
.sin_len
= sizeof(w
.w_dst
);
718 w
.w_gate
.sin_len
= sizeof(w
.w_gate
);
720 if (mask
== HOST_MASK
) {
721 w
.w_rtm
.rtm_flags
|= RTF_HOST
;
722 w
.w_rtm
.rtm_msglen
-= sizeof(w
.w_mask
);
724 w
.w_rtm
.rtm_addrs
|= RTA_NETMASK
;
725 w
.w_mask
.sin_addr
.s_addr
= htonl(mask
);
728 if (w
.w_mask
.sin_len
== 0)
729 w
.w_mask
.sin_len
= sizeof(long);
730 w
.w_rtm
.rtm_msglen
-= (sizeof(w
.w_mask
) - w
.w_mask
.sin_len
);
735 cc
= write(rt_sock
, &w
, w
.w_rtm
.rtm_msglen
);
738 && (action
== RTM_CHANGE
|| action
== RTM_DELETE
)) {
739 trace_act("route disappeared before" PAT
, ARGS
);
740 if (action
== RTM_CHANGE
) {
746 msglog("write(rt_sock)" PAT
": %s", ARGS
, strerror(errno
));
748 } else if (cc
!= w
.w_rtm
.rtm_msglen
) {
749 msglog("write(rt_sock) wrote %ld instead of %d for" PAT
,
750 cc
, w
.w_rtm
.rtm_msglen
, ARGS
);
755 trace_misc("write kernel" PAT
, ARGS
);
761 #define KHASH_SIZE 71 /* should be prime */
762 #define KHASH(a,m) khash_bins[((a) ^ (m)) % KHASH_SIZE]
763 static struct khash
{
764 struct khash
*k_next
;
771 #define KS_DELETE 0x002 /* need to delete the route */
772 #define KS_ADD 0x004 /* add to the kernel */
773 #define KS_CHANGE 0x008 /* tell kernel to change the route */
774 #define KS_DEL_ADD 0x010 /* delete & add to change the kernel */
775 #define KS_STATIC 0x020 /* Static flag in kernel */
776 #define KS_GATEWAY 0x040 /* G flag in kernel */
777 #define KS_DYNAMIC 0x080 /* result of redirect */
778 #define KS_DELETED 0x100 /* already deleted from kernel */
779 #define KS_CHECK 0x200
781 #define K_KEEP_LIM 30
782 time_t k_redirect_time
; /* when redirected route 1st seen */
783 } *khash_bins
[KHASH_SIZE
];
787 kern_find(naddr dst
, naddr mask
, struct khash
***ppk
)
789 struct khash
*k
, **pk
;
791 for (pk
= &KHASH(dst
,mask
); (k
= *pk
) != NULL
; pk
= &k
->k_next
) {
792 if (k
->k_dst
== dst
&& k
->k_mask
== mask
)
802 kern_add(naddr dst
, naddr mask
)
804 struct khash
*k
, **pk
;
806 k
= kern_find(dst
, mask
, &pk
);
810 k
= (struct khash
*)rtmalloc(sizeof(*k
), "kern_add");
812 memset(k
, 0, sizeof(*k
));
816 k
->k_keep
= now
.tv_sec
;
823 /* If a kernel route has a non-zero metric, check that it is still in the
824 * daemon table, and not deleted by interfaces coming and going.
827 kern_check_static(struct khash
*k
,
828 struct interface
*ifp
)
833 if (k
->k_metric
== 0)
836 memset(&new, 0, sizeof(new));
838 new.rts_gate
= k
->k_gate
;
839 new.rts_router
= (ifp
!= NULL
) ? ifp
->int_addr
: loopaddr
;
840 new.rts_metric
= k
->k_metric
;
841 new.rts_time
= now
.tv_sec
;
843 rt
= rtget(k
->k_dst
, k
->k_mask
);
845 if (!(rt
->rt_state
& RS_STATIC
))
846 rtchange(rt
, rt
->rt_state
| RS_STATIC
, &new, 0);
848 rtadd(k
->k_dst
, k
->k_mask
, RS_STATIC
, &new);
853 /* operate on a kernel entry
856 kern_ioctl(struct khash
*k
,
857 int action
, /* RTM_DELETE, etc */
863 k
->k_state
&= ~KS_DYNAMIC
;
864 if (k
->k_state
& KS_DELETED
)
866 k
->k_state
|= KS_DELETED
;
869 k
->k_state
&= ~KS_DELETED
;
872 if (k
->k_state
& KS_DELETED
) {
874 k
->k_state
&= ~KS_DELETED
;
879 rtioctl(action
, k
->k_dst
, k
->k_gate
, k
->k_mask
, k
->k_metric
, flags
);
883 /* add a route the kernel told us
886 rtm_add(struct rt_msghdr
*rtm
,
887 struct rt_addrinfo
*info
,
891 struct interface
*ifp
;
895 if (rtm
->rtm_flags
& RTF_HOST
) {
897 } else if (INFO_MASK(info
) != 0) {
898 mask
= ntohl(S_ADDR(INFO_MASK(info
)));
900 msglog("ignore %s without mask", rtm_type_name(rtm
->rtm_type
));
904 k
= kern_add(S_ADDR(INFO_DST(info
)), mask
);
905 if (k
->k_state
& KS_NEW
)
906 k
->k_keep
= now
.tv_sec
+keep
;
907 if (INFO_GATE(info
) == 0) {
908 trace_act("note %s without gateway",
909 rtm_type_name(rtm
->rtm_type
));
910 k
->k_metric
= HOPCNT_INFINITY
;
911 } else if (INFO_GATE(info
)->sa_family
!= AF_INET
) {
912 trace_act("note %s with gateway AF=%d",
913 rtm_type_name(rtm
->rtm_type
),
914 INFO_GATE(info
)->sa_family
);
915 k
->k_metric
= HOPCNT_INFINITY
;
917 k
->k_gate
= S_ADDR(INFO_GATE(info
));
918 k
->k_metric
= rtm
->rtm_rmx
.rmx_hopcount
;
921 else if (k
->k_metric
> HOPCNT_INFINITY
-1)
922 k
->k_metric
= HOPCNT_INFINITY
-1;
924 k
->k_state
&= ~(KS_DELETE
| KS_ADD
| KS_CHANGE
| KS_DEL_ADD
925 | KS_DELETED
| KS_GATEWAY
| KS_STATIC
926 | KS_NEW
| KS_CHECK
);
927 if (rtm
->rtm_flags
& RTF_GATEWAY
)
928 k
->k_state
|= KS_GATEWAY
;
929 if (rtm
->rtm_flags
& RTF_STATIC
)
930 k
->k_state
|= KS_STATIC
;
932 if (0 != (rtm
->rtm_flags
& (RTF_DYNAMIC
| RTF_MODIFIED
))) {
933 if (INFO_AUTHOR(info
) != 0
934 && INFO_AUTHOR(info
)->sa_family
== AF_INET
)
935 ifp
= iflookup(S_ADDR(INFO_AUTHOR(info
)));
939 && (ifp
== NULL
|| !(ifp
->int_state
& IS_REDIRECT_OK
))) {
940 /* Routers are not supposed to listen to redirects,
941 * so delete it if it came via an unknown interface
942 * or the interface does not have special permission.
944 k
->k_state
&= ~KS_DYNAMIC
;
945 k
->k_state
|= KS_DELETE
;
946 LIM_SEC(need_kern
, 0);
947 trace_act("mark for deletion redirected %s --> %s"
949 addrname(k
->k_dst
, k
->k_mask
, 0),
950 naddr_ntoa(k
->k_gate
),
951 ifp
? ifp
->int_name
: "unknown interface");
953 k
->k_state
|= KS_DYNAMIC
;
954 k
->k_redirect_time
= now
.tv_sec
;
955 trace_act("accept redirected %s --> %s via %s",
956 addrname(k
->k_dst
, k
->k_mask
, 0),
957 naddr_ntoa(k
->k_gate
),
958 ifp
? ifp
->int_name
: "unknown interface");
963 /* If it is not a static route, quit until the next comparison
964 * between the kernel and daemon tables, when it will be deleted.
966 if (!(k
->k_state
& KS_STATIC
)) {
967 k
->k_state
|= KS_DELETE
;
968 LIM_SEC(need_kern
, k
->k_keep
);
972 /* Put static routes with real metrics into the daemon table so
973 * they can be advertised.
975 * Find the interface toward the gateway.
977 ifp
= iflookup(k
->k_gate
);
979 msglog("static route %s --> %s impossibly lacks ifp",
980 addrname(S_ADDR(INFO_DST(info
)), mask
, 0),
981 naddr_ntoa(k
->k_gate
));
983 kern_check_static(k
, ifp
);
987 /* deal with packet loss
990 rtm_lose(struct rt_msghdr
*rtm
,
991 struct rt_addrinfo
*info
)
993 if (INFO_GATE(info
) == 0
994 || INFO_GATE(info
)->sa_family
!= AF_INET
) {
995 trace_act("ignore %s without gateway",
996 rtm_type_name(rtm
->rtm_type
));
1001 rdisc_age(S_ADDR(INFO_GATE(info
)));
1002 age(S_ADDR(INFO_GATE(info
)));
1006 /* Make the gateway slot of an info structure point to something
1007 * useful. If it is not already useful, but it specifies an interface,
1008 * then fill in the sockaddr_in provided and point it there.
1011 get_info_gate(struct sockaddr
**sap
,
1012 struct sockaddr_in
*in
)
1014 struct sockaddr_dl
*sdl
= (struct sockaddr_dl
*)*sap
;
1015 struct interface
*ifp
;
1019 if ((sdl
)->sdl_family
== AF_INET
)
1021 if ((sdl
)->sdl_family
!= AF_LINK
)
1024 ifp
= ifwithindex(sdl
->sdl_index
, 1);
1028 in
->sin_addr
.s_addr
= ifp
->int_addr
;
1030 in
->sin_len
= sizeof(*in
);
1032 in
->sin_family
= AF_INET
;
1033 *sap
= (struct sockaddr
*)in
;
1039 /* Clean the kernel table by copying it to the daemon image.
1040 * Eventually the daemon will delete any extra routes.
1045 static char *sysctl_buf
;
1046 static size_t sysctl_buf_size
= 0;
1050 struct rt_msghdr
*rtm
;
1051 struct sockaddr_in gate_sin
;
1052 struct rt_addrinfo info
;
1057 for (i
= 0; i
< KHASH_SIZE
; i
++) {
1058 for (k
= khash_bins
[i
]; k
!= NULL
; k
= k
->k_next
) {
1059 k
->k_state
|= KS_CHECK
;
1065 mib
[2] = 0; /* protocol */
1066 mib
[3] = 0; /* wildcard address family */
1067 mib
[4] = NET_RT_DUMP
;
1068 mib
[5] = 0; /* no flags */
1070 if ((needed
= sysctl_buf_size
) != 0) {
1071 if (sysctl(mib
, 6, sysctl_buf
,&needed
, 0, 0) >= 0)
1073 if (errno
!= ENOMEM
&& errno
!= EFAULT
)
1074 BADERR(1,"flush_kern: sysctl(RT_DUMP)");
1078 if (sysctl(mib
, 6, 0, &needed
, 0, 0) < 0)
1079 BADERR(1,"flush_kern: sysctl(RT_DUMP) estimate");
1080 /* Kludge around the habit of some systems, such as
1081 * BSD/OS 3.1, to not admit how many routes are in the
1082 * kernel, or at least to be quite wrong.
1084 needed
+= 50*(sizeof(*rtm
)+5*sizeof(struct sockaddr
));
1085 sysctl_buf
= rtmalloc(sysctl_buf_size
= needed
,
1086 "flush_kern sysctl(RT_DUMP)");
1089 lim
= sysctl_buf
+ needed
;
1090 for (next
= sysctl_buf
; next
< lim
; next
+= rtm
->rtm_msglen
) {
1091 rtm
= (struct rt_msghdr
*)next
;
1092 if (rtm
->rtm_msglen
== 0) {
1093 msglog("zero length kernel route at "
1094 " %#lx in buffer %#lx before %#lx",
1095 (u_long
)rtm
, (u_long
)sysctl_buf
, (u_long
)lim
);
1100 (struct sockaddr
*)(rtm
+1),
1101 (struct sockaddr
*)(next
+ rtm
->rtm_msglen
),
1104 if (INFO_DST(&info
) == 0
1105 || INFO_DST(&info
)->sa_family
!= AF_INET
)
1108 /* ignore ARP table entries on systems with a merged route
1111 if (rtm
->rtm_flags
& RTF_LLINFO
)
1114 /* ignore multicast addresses
1116 if (IN_MULTICAST(ntohl(S_ADDR(INFO_DST(&info
)))))
1119 if (!get_info_gate(&INFO_GATE(&info
), &gate_sin
))
1122 /* Note static routes and interface routes, and also
1123 * preload the image of the kernel table so that
1124 * we can later clean it, as well as avoid making
1125 * unneeded changes. Keep the old kernel routes for a
1126 * few seconds to allow a RIP or router-discovery
1127 * response to be heard.
1129 rtm_add(rtm
,&info
,MIN_WAITTIME
);
1132 for (i
= 0; i
< KHASH_SIZE
; i
++) {
1133 for (k
= khash_bins
[i
]; k
!= NULL
; k
= k
->k_next
) {
1134 if (k
->k_state
& KS_CHECK
) {
1135 msglog("%s --> %s disappeared from kernel",
1136 addrname(k
->k_dst
, k
->k_mask
, 0),
1137 naddr_ntoa(k
->k_gate
));
1138 del_static(k
->k_dst
, k
->k_mask
, k
->k_gate
, 1);
1145 /* Listen to announcements from the kernel
1151 struct interface
*ifp
;
1152 struct sockaddr_in gate_sin
;
1156 struct rt_msghdr rtm
;
1157 struct sockaddr addrs
[RTAX_MAX
];
1159 struct if_msghdr ifm
;
1161 char str
[100], *strp
;
1162 struct rt_addrinfo info
;
1166 cc
= read(rt_sock
, &m
, sizeof(m
));
1168 if (cc
< 0 && errno
!= EWOULDBLOCK
)
1169 LOGERR("read(rt_sock)");
1173 if (m
.r
.rtm
.rtm_version
!= RTM_VERSION
) {
1174 msglog("bogus routing message version %d",
1175 m
.r
.rtm
.rtm_version
);
1179 /* Ignore our own results.
1181 if (m
.r
.rtm
.rtm_type
<= RTM_CHANGE
1182 && m
.r
.rtm
.rtm_pid
== mypid
) {
1183 static int complained
= 0;
1185 msglog("receiving our own change messages");
1191 if (m
.r
.rtm
.rtm_type
== RTM_IFINFO
1192 || m
.r
.rtm
.rtm_type
== RTM_NEWADDR
1193 || m
.r
.rtm
.rtm_type
== RTM_DELADDR
) {
1194 ifp
= ifwithindex(m
.ifm
.ifm_index
,
1195 m
.r
.rtm
.rtm_type
!= RTM_DELADDR
);
1197 trace_act("note %s with flags %#x"
1198 " for unknown interface index #%d",
1199 rtm_type_name(m
.r
.rtm
.rtm_type
),
1203 trace_act("note %s with flags %#x for %s",
1204 rtm_type_name(m
.r
.rtm
.rtm_type
),
1208 /* After being informed of a change to an interface,
1209 * check them all now if the check would otherwise
1210 * be a long time from now, if the interface is
1211 * not known, or if the interface has been turned
1214 if (ifinit_timer
.tv_sec
-now
.tv_sec
>=CHECK_BAD_INTERVAL
1216 || ((ifp
->int_if_flags
^ m
.ifm
.ifm_flags
)
1218 ifinit_timer
.tv_sec
= now
.tv_sec
;
1222 strcpy(str
, rtm_type_name(m
.r
.rtm
.rtm_type
));
1223 strp
= &str
[strlen(str
)];
1224 if (m
.r
.rtm
.rtm_type
<= RTM_CHANGE
)
1225 strp
+= sprintf(strp
," from pid %d",m
.r
.rtm
.rtm_pid
);
1227 rt_xaddrs(&info
, m
.r
.addrs
, &m
.r
.addrs
[RTAX_MAX
],
1230 if (INFO_DST(&info
) == 0) {
1231 trace_act("ignore %s without dst", str
);
1235 if (INFO_DST(&info
)->sa_family
!= AF_INET
) {
1236 trace_act("ignore %s for AF %d", str
,
1237 INFO_DST(&info
)->sa_family
);
1241 mask
= ((INFO_MASK(&info
) != 0)
1242 ? ntohl(S_ADDR(INFO_MASK(&info
)))
1243 : (m
.r
.rtm
.rtm_flags
& RTF_HOST
)
1245 : std_mask(S_ADDR(INFO_DST(&info
))));
1247 strp
+= sprintf(strp
, ": %s",
1248 addrname(S_ADDR(INFO_DST(&info
)), mask
, 0));
1250 if (IN_MULTICAST(ntohl(S_ADDR(INFO_DST(&info
))))) {
1251 trace_act("ignore multicast %s", str
);
1255 if (m
.r
.rtm
.rtm_flags
& RTF_LLINFO
) {
1256 trace_act("ignore ARP %s", str
);
1260 if (get_info_gate(&INFO_GATE(&info
), &gate_sin
)) {
1261 gate
= S_ADDR(INFO_GATE(&info
));
1262 strp
+= sprintf(strp
, " --> %s", naddr_ntoa(gate
));
1267 if (INFO_AUTHOR(&info
) != 0)
1268 strp
+= sprintf(strp
, " by authority of %s",
1269 saddr_ntoa(INFO_AUTHOR(&info
)));
1271 switch (m
.r
.rtm
.rtm_type
) {
1275 if (m
.r
.rtm
.rtm_errno
!= 0) {
1276 trace_act("ignore %s with \"%s\" error",
1277 str
, strerror(m
.r
.rtm
.rtm_errno
));
1279 trace_act("%s", str
);
1280 rtm_add(&m
.r
.rtm
,&info
,0);
1285 if (m
.r
.rtm
.rtm_errno
!= 0
1286 && m
.r
.rtm
.rtm_errno
!= ESRCH
) {
1287 trace_act("ignore %s with \"%s\" error",
1288 str
, strerror(m
.r
.rtm
.rtm_errno
));
1290 trace_act("%s", str
);
1291 del_static(S_ADDR(INFO_DST(&info
)), mask
,
1297 trace_act("%s", str
);
1298 rtm_lose(&m
.r
.rtm
,&info
);
1302 trace_act("ignore %s", str
);
1309 /* after aggregating, note routes that belong in the kernel
1312 kern_out(struct ag_info
*ag
)
1317 /* Do not install bad routes if they are not already present.
1318 * This includes routes that had RS_NET_SYN for interfaces that
1321 if (ag
->ag_metric
== HOPCNT_INFINITY
) {
1322 k
= kern_find(htonl(ag
->ag_dst_h
), ag
->ag_mask
, 0);
1326 k
= kern_add(htonl(ag
->ag_dst_h
), ag
->ag_mask
);
1329 if (k
->k_state
& KS_NEW
) {
1330 /* will need to add new entry to the kernel table */
1331 k
->k_state
= KS_ADD
;
1332 if (ag
->ag_state
& AGS_GATEWAY
)
1333 k
->k_state
|= KS_GATEWAY
;
1334 k
->k_gate
= ag
->ag_gate
;
1335 k
->k_metric
= ag
->ag_metric
;
1339 if (k
->k_state
& KS_STATIC
)
1342 /* modify existing kernel entry if necessary */
1343 if (k
->k_gate
!= ag
->ag_gate
1344 || k
->k_metric
!= ag
->ag_metric
) {
1345 /* Must delete bad interface routes etc. to change them. */
1346 if (k
->k_metric
== HOPCNT_INFINITY
)
1347 k
->k_state
|= KS_DEL_ADD
;
1348 k
->k_gate
= ag
->ag_gate
;
1349 k
->k_metric
= ag
->ag_metric
;
1350 k
->k_state
|= KS_CHANGE
;
1353 /* If the daemon thinks the route should exist, forget
1354 * about any redirections.
1355 * If the daemon thinks the route should exist, eventually
1356 * override manual intervention by the operator.
1358 if ((k
->k_state
& (KS_DYNAMIC
| KS_DELETED
)) != 0) {
1359 k
->k_state
&= ~KS_DYNAMIC
;
1360 k
->k_state
|= (KS_ADD
| KS_DEL_ADD
);
1363 if ((k
->k_state
& KS_GATEWAY
)
1364 && !(ag
->ag_state
& AGS_GATEWAY
)) {
1365 k
->k_state
&= ~KS_GATEWAY
;
1366 k
->k_state
|= (KS_ADD
| KS_DEL_ADD
);
1367 } else 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
);
1373 /* Deleting-and-adding is necessary to change aspects of a route.
1374 * Just delete instead of deleting and then adding a bad route.
1375 * Otherwise, we want to keep the route in the kernel.
1377 if (k
->k_metric
== HOPCNT_INFINITY
1378 && (k
->k_state
& KS_DEL_ADD
))
1379 k
->k_state
|= KS_DELETE
;
1381 k
->k_state
&= ~KS_DELETE
;
1388 walk_kern(struct radix_node
*rn
,
1389 struct walkarg
*argp UNUSED
)
1391 #define RT ((struct rt_entry *)rn)
1396 /* Do not install synthetic routes */
1397 if (RT
->rt_state
& RS_NET_SYN
)
1400 if (!(RT
->rt_state
& RS_IF
)) {
1401 /* This is an ordinary route, not for an interface.
1404 /* aggregate, ordinary good routes without regard to
1408 ags
|= (AGS_GATEWAY
| AGS_SUPPRESS
| AGS_AGGREGATE
);
1410 /* Do not install host routes directly to hosts, to avoid
1411 * interfering with ARP entries in the kernel table.
1414 && ntohl(RT
->rt_dst
) == RT
->rt_gate
)
1418 /* This is an interface route.
1419 * Do not install routes for "external" remote interfaces.
1421 if (RT
->rt_ifp
!= 0 && (RT
->rt_ifp
->int_state
& IS_EXTERNAL
))
1424 /* Interfaces should override received routes.
1427 ags
|= (AGS_IF
| AGS_CORS_GATE
);
1429 /* If it is not an interface, or an alias for an interface,
1430 * it must be a "gateway."
1432 * If it is a "remote" interface, it is also a "gateway" to
1433 * the kernel if is not a alias.
1436 || (RT
->rt_ifp
->int_state
& IS_REMOTE
))
1437 ags
|= (AGS_GATEWAY
| AGS_SUPPRESS
| AGS_AGGREGATE
);
1440 /* If RIP is off and IRDP is on, let the route to the discovered
1441 * route suppress any RIP routes. Eventually the RIP routes
1442 * will time-out and be deleted. This reaches the steady-state
1445 if ((RT
->rt_state
& RS_RDISC
) && rip_sock
< 0)
1446 ags
|= AGS_CORS_GATE
;
1448 metric
= RT
->rt_metric
;
1449 if (metric
== HOPCNT_INFINITY
) {
1450 /* if the route is dead, so try hard to aggregate. */
1451 pref
= HOPCNT_INFINITY
;
1452 ags
|= (AGS_FINE_GATE
| AGS_SUPPRESS
);
1453 ags
&= ~(AGS_IF
| AGS_CORS_GATE
);
1456 ag_check(RT
->rt_dst
, RT
->rt_mask
, RT
->rt_gate
, 0,
1457 metric
,pref
, 0, 0, ags
, kern_out
);
1463 /* Update the kernel table to match the daemon table.
1469 struct khash
*k
, **pk
;
1472 need_kern
= age_timer
;
1474 /* Walk daemon table, updating the copy of the kernel table.
1476 rn_walktree(rhead
, walk_kern
, 0);
1477 ag_flush(0,0,kern_out
);
1479 for (i
= 0; i
< KHASH_SIZE
; i
++) {
1480 for (pk
= &khash_bins
[i
]; (k
= *pk
) != NULL
; ) {
1481 /* Do not touch static routes */
1482 if (k
->k_state
& KS_STATIC
) {
1483 kern_check_static(k
,0);
1488 /* check hold on routes deleted by the operator */
1489 if (k
->k_keep
> now
.tv_sec
) {
1490 /* ensure we check when the hold is over */
1491 LIM_SEC(need_kern
, k
->k_keep
);
1492 /* mark for the next cycle */
1493 k
->k_state
|= KS_DELETE
;
1498 if ((k
->k_state
& KS_DELETE
)
1499 && !(k
->k_state
& KS_DYNAMIC
)) {
1500 kern_ioctl(k
, RTM_DELETE
, 0);
1506 if (k
->k_state
& KS_DEL_ADD
)
1507 kern_ioctl(k
, RTM_DELETE
, 0);
1509 if (k
->k_state
& KS_ADD
) {
1510 kern_ioctl(k
, RTM_ADD
,
1511 ((0 != (k
->k_state
& (KS_GATEWAY
1513 ? RTF_GATEWAY
: 0));
1514 } else if (k
->k_state
& KS_CHANGE
) {
1515 kern_ioctl(k
, RTM_CHANGE
,
1516 ((0 != (k
->k_state
& (KS_GATEWAY
1518 ? RTF_GATEWAY
: 0));
1520 k
->k_state
&= ~(KS_ADD
|KS_CHANGE
|KS_DEL_ADD
);
1522 /* Mark this route to be deleted in the next cycle.
1523 * This deletes routes that disappear from the
1524 * daemon table, since the normal aging code
1525 * will clear the bit for routes that have not
1526 * disappeared from the daemon table.
1528 k
->k_state
|= KS_DELETE
;
1535 /* Delete a static route in the image of the kernel table.
1538 del_static(naddr dst
,
1544 struct rt_entry
*rt
;
1546 /* Just mark it in the table to be deleted next time the kernel
1548 * If it has already been deleted, mark it as such, and set its
1549 * keep-timer so that it will not be deleted again for a while.
1550 * This lets the operator delete a route added by the daemon
1551 * and add a replacement.
1553 k
= kern_find(dst
, mask
, 0);
1554 if (k
!= NULL
&& (gate
== 0 || k
->k_gate
== gate
)) {
1555 k
->k_state
&= ~(KS_STATIC
| KS_DYNAMIC
| KS_CHECK
);
1556 k
->k_state
|= KS_DELETE
;
1558 k
->k_state
|= KS_DELETED
;
1559 k
->k_keep
= now
.tv_sec
+ K_KEEP_LIM
;
1563 rt
= rtget(dst
, mask
);
1564 if (rt
!= NULL
&& (rt
->rt_state
& RS_STATIC
))
1569 /* Delete all routes generated from ICMP Redirects that use a given gateway,
1570 * as well as old redirected routes.
1573 del_redirects(naddr bad_gate
,
1580 for (i
= 0; i
< KHASH_SIZE
; i
++) {
1581 for (k
= khash_bins
[i
]; k
!= NULL
; k
= k
->k_next
) {
1582 if (!(k
->k_state
& KS_DYNAMIC
)
1583 || (k
->k_state
& KS_STATIC
))
1586 if (k
->k_gate
!= bad_gate
1587 && k
->k_redirect_time
> old
1591 k
->k_state
|= KS_DELETE
;
1592 k
->k_state
&= ~KS_DYNAMIC
;
1593 need_kern
.tv_sec
= now
.tv_sec
;
1594 trace_act("mark redirected %s --> %s for deletion",
1595 addrname(k
->k_dst
, k
->k_mask
, 0),
1596 naddr_ntoa(k
->k_gate
));
1602 /* Start the daemon tables.
1604 extern int max_keylen
;
1612 /* Initialize the radix trees */
1613 max_keylen
= sizeof(struct sockaddr_in
);
1615 rn_inithead(&rhead
, 32);
1617 /* mark all of the slots in the table free */
1618 ag_avail
= ag_slots
;
1619 for (ag
= ag_slots
, i
= 1; i
< NUM_AG_SLOTS
; i
++) {
1626 #ifdef _HAVE_SIN_LEN
1627 static struct sockaddr_in dst_sock
= {sizeof(dst_sock
), AF_INET
, 0, {0}, {0}};
1628 static struct sockaddr_in mask_sock
= {sizeof(mask_sock
), AF_INET
, 0, {0}, {0}};
1630 static struct sockaddr_in_new dst_sock
= {_SIN_ADDR_SIZE
, AF_INET
};
1631 static struct sockaddr_in_new mask_sock
= {_SIN_ADDR_SIZE
, AF_INET
};
1636 set_need_flash(void)
1640 /* Do not send the flash update immediately. Wait a little
1641 * while to hear from other routers.
1643 no_flash
.tv_sec
= now
.tv_sec
+ MIN_WAITTIME
;
1648 /* Get a particular routing table entry
1651 rtget(naddr dst
, naddr mask
)
1653 struct rt_entry
*rt
;
1655 dst_sock
.sin_addr
.s_addr
= dst
;
1656 mask_sock
.sin_addr
.s_addr
= htonl(mask
);
1657 masktrim(&mask_sock
);
1658 rt
= (struct rt_entry
*)rhead
->rnh_lookup(&dst_sock
,&mask_sock
,rhead
);
1660 || rt
->rt_dst
!= dst
1661 || rt
->rt_mask
!= mask
)
1668 /* Find a route to dst as the kernel would.
1673 dst_sock
.sin_addr
.s_addr
= dst
;
1674 return (struct rt_entry
*)rhead
->rnh_matchaddr(&dst_sock
, rhead
);
1678 /* add a route to the table
1683 u_int state
, /* rt_state for the entry */
1684 struct rt_spare
*new)
1686 struct rt_entry
*rt
;
1689 struct rt_spare
*rts
;
1691 rt
= (struct rt_entry
*)rtmalloc(sizeof (*rt
), "rtadd");
1692 memset(rt
, 0, sizeof(*rt
));
1693 for (rts
= rt
->rt_spares
, i
= NUM_SPARES
; i
!= 0; i
--, rts
++)
1694 rts
->rts_metric
= HOPCNT_INFINITY
;
1696 rt
->rt_nodes
->rn_key
= (caddr_t
)&rt
->rt_dst_sock
;
1698 rt
->rt_dst_sock
.sin_family
= AF_INET
;
1699 #ifdef _HAVE_SIN_LEN
1700 rt
->rt_dst_sock
.sin_len
= dst_sock
.sin_len
;
1702 if (mask
!= HOST_MASK
) {
1703 smask
= std_mask(dst
);
1704 if ((smask
& ~mask
) == 0 && mask
> smask
)
1707 mask_sock
.sin_addr
.s_addr
= htonl(mask
);
1708 masktrim(&mask_sock
);
1710 rt
->rt_state
= state
;
1711 rt
->rt_spares
[0] = *new;
1712 rt
->rt_time
= now
.tv_sec
;
1713 rt
->rt_poison_metric
= HOPCNT_INFINITY
;
1714 rt
->rt_seqno
= update_seqno
;
1716 if (++total_routes
== MAX_ROUTES
)
1717 msglog("have maximum (%d) routes", total_routes
);
1719 trace_add_del("Add", rt
);
1721 need_kern
.tv_sec
= now
.tv_sec
;
1724 if (0 == rhead
->rnh_addaddr(&rt
->rt_dst_sock
, &mask_sock
,
1725 rhead
, rt
->rt_nodes
)) {
1726 msglog("rnh_addaddr() failed for %s mask=%#lx",
1727 naddr_ntoa(dst
), (u_long
)mask
);
1733 /* notice a changed route
1736 rtchange(struct rt_entry
*rt
,
1737 u_int state
, /* new state bits */
1738 struct rt_spare
*new,
1741 if (rt
->rt_metric
!= new->rts_metric
) {
1742 /* Fix the kernel immediately if it seems the route
1743 * has gone bad, since there may be a working route that
1744 * aggregates this route.
1746 if (new->rts_metric
== HOPCNT_INFINITY
) {
1747 need_kern
.tv_sec
= now
.tv_sec
;
1748 if (new->rts_time
>= now
.tv_sec
- EXPIRE_TIME
)
1749 new->rts_time
= now
.tv_sec
- EXPIRE_TIME
;
1751 rt
->rt_seqno
= update_seqno
;
1755 if (rt
->rt_gate
!= new->rts_gate
) {
1756 need_kern
.tv_sec
= now
.tv_sec
;
1757 rt
->rt_seqno
= update_seqno
;
1761 state
|= (rt
->rt_state
& RS_SUBNET
);
1763 /* Keep various things from deciding ageless routes are stale.
1765 if (!AGE_RT(state
, new->rts_ifp
))
1766 new->rts_time
= now
.tv_sec
;
1769 trace_change(rt
, state
, new,
1770 label
? label
: "Chg ");
1772 rt
->rt_state
= state
;
1773 rt
->rt_spares
[0] = *new;
1777 /* check for a better route among the spares
1779 static struct rt_spare
*
1780 rts_better(struct rt_entry
*rt
)
1782 struct rt_spare
*rts
, *rts1
;
1785 /* find the best alternative among the spares */
1786 rts
= rt
->rt_spares
+1;
1787 for (i
= NUM_SPARES
, rts1
= rts
+1; i
> 2; i
--, rts1
++) {
1788 if (BETTER_LINK(rt
,rts1
,rts
))
1796 /* switch to a backup route
1799 rtswitch(struct rt_entry
*rt
,
1800 struct rt_spare
*rts
)
1802 struct rt_spare swap
;
1806 /* Do not change permanent routes */
1807 if (0 != (rt
->rt_state
& (RS_MHOME
| RS_STATIC
| RS_RDISC
1808 | RS_NET_SYN
| RS_IF
)))
1811 /* find the best alternative among the spares */
1813 rts
= rts_better(rt
);
1815 /* Do not bother if it is not worthwhile.
1817 if (!BETTER_LINK(rt
, rts
, rt
->rt_spares
))
1820 swap
= rt
->rt_spares
[0];
1821 sprintf(label
, "Use #%d", (int)(rts
- rt
->rt_spares
));
1822 rtchange(rt
, rt
->rt_state
& ~(RS_NET_SYN
| RS_RDISC
), rts
, label
);
1823 if (swap
.rts_metric
== HOPCNT_INFINITY
) {
1832 rtdelete(struct rt_entry
*rt
)
1838 trace_add_del("Del", rt
);
1840 k
= kern_find(rt
->rt_dst
, rt
->rt_mask
, 0);
1842 k
->k_state
|= KS_DELETE
;
1843 need_kern
.tv_sec
= now
.tv_sec
;
1846 dst_sock
.sin_addr
.s_addr
= rt
->rt_dst
;
1847 mask_sock
.sin_addr
.s_addr
= htonl(rt
->rt_mask
);
1848 masktrim(&mask_sock
);
1849 if (rt
!= (struct rt_entry
*)rhead
->rnh_deladdr(&dst_sock
, &mask_sock
,
1851 msglog("rnh_deladdr() failed");
1860 rts_delete(struct rt_entry
*rt
,
1861 struct rt_spare
*rts
)
1863 trace_upslot(rt
, rts
, &rts_empty
);
1868 /* Get rid of a bad route, and try to switch to a replacement.
1871 rtbad(struct rt_entry
*rt
)
1873 struct rt_spare
new;
1875 /* Poison the route */
1876 new = rt
->rt_spares
[0];
1877 new.rts_metric
= HOPCNT_INFINITY
;
1878 rtchange(rt
, rt
->rt_state
& ~(RS_IF
| RS_LOCAL
| RS_STATIC
), &new, 0);
1883 /* Junk a RS_NET_SYN or RS_LOCAL route,
1884 * unless it is needed by another interface.
1887 rtbad_sub(struct rt_entry
*rt
)
1889 struct interface
*ifp
, *ifp1
;
1890 struct intnet
*intnetp
;
1897 if (rt
->rt_state
& RS_LOCAL
) {
1898 /* Is this the route through loopback for the interface?
1899 * If so, see if it is used by any other interfaces, such
1900 * as a point-to-point interface with the same local address.
1902 for (ifp
= ifnet
; ifp
!= NULL
; ifp
= ifp
->int_next
) {
1903 /* Retain it if another interface needs it.
1905 if (ifp
->int_addr
== rt
->rt_ifp
->int_addr
) {
1914 if (!(state
& RS_LOCAL
)) {
1915 /* Retain RIPv1 logical network route if there is another
1916 * interface that justifies it.
1918 if (rt
->rt_state
& RS_NET_SYN
) {
1919 for (ifp
= ifnet
; ifp
!= NULL
; ifp
= ifp
->int_next
) {
1920 if ((ifp
->int_state
& IS_NEED_NET_SYN
)
1921 && rt
->rt_mask
== ifp
->int_std_mask
1922 && rt
->rt_dst
== ifp
->int_std_addr
) {
1923 state
|= RS_NET_SYN
;
1930 /* or if there is an authority route that needs it. */
1931 for (intnetp
= intnets
;
1933 intnetp
= intnetp
->intnet_next
) {
1934 if (intnetp
->intnet_addr
== rt
->rt_dst
1935 && intnetp
->intnet_mask
== rt
->rt_mask
) {
1936 state
|= (RS_NET_SYN
| RS_NET_INT
);
1942 if (ifp1
!= NULL
|| (state
& RS_NET_SYN
)) {
1943 struct rt_spare
new = rt
->rt_spares
[0];
1945 rtchange(rt
, ((rt
->rt_state
& ~(RS_NET_SYN
|RS_LOCAL
)) | state
),
1953 /* Called while walking the table looking for sick interfaces
1954 * or after a time change.
1958 walk_bad(struct radix_node
*rn
,
1959 struct walkarg
*argp UNUSED
)
1961 #define RT ((struct rt_entry *)rn)
1962 struct rt_spare
*rts
;
1966 /* fix any spare routes through the interface
1968 rts
= RT
->rt_spares
;
1969 for (i
= NUM_SPARES
; i
!= 1; i
--) {
1971 if (rts
->rts_metric
< HOPCNT_INFINITY
1972 && (rts
->rts_ifp
== 0
1973 || (rts
->rts_ifp
->int_state
& IS_BROKE
)))
1974 rts_delete(RT
, rts
);
1977 /* Deal with the main route
1979 /* finished if it has been handled before or if its interface is ok
1981 if (RT
->rt_ifp
== 0 || !(RT
->rt_ifp
->int_state
& IS_BROKE
))
1984 /* Bad routes for other than interfaces are easy.
1986 if (0 == (RT
->rt_state
& (RS_IF
| RS_NET_SYN
| RS_LOCAL
))) {
1997 /* Check the age of an individual route.
2001 walk_age(struct radix_node
*rn
,
2002 struct walkarg
*argp UNUSED
)
2004 #define RT ((struct rt_entry *)rn)
2005 struct interface
*ifp
;
2006 struct rt_spare
*rts
;
2010 /* age all of the spare routes, including the primary route
2013 rts
= RT
->rt_spares
;
2014 for (i
= NUM_SPARES
; i
!= 0; i
--, rts
++) {
2017 if (i
== NUM_SPARES
) {
2018 if (!AGE_RT(RT
->rt_state
, ifp
)) {
2019 /* Keep various things from deciding ageless
2022 rts
->rts_time
= now
.tv_sec
;
2026 /* forget RIP routes after RIP has been turned off.
2034 /* age failing routes
2036 if (age_bad_gate
== rts
->rts_gate
2037 && rts
->rts_time
>= now_stale
) {
2038 rts
->rts_time
-= SUPPLY_INTERVAL
;
2041 /* trash the spare routes when they go bad */
2042 if (rts
->rts_metric
< HOPCNT_INFINITY
2043 && now_garbage
> rts
->rts_time
2045 rts_delete(RT
, rts
);
2049 /* finished if the active route is still fresh */
2050 if (now_stale
<= RT
->rt_time
)
2053 /* try to switch to an alternative */
2056 /* Delete a dead route after it has been publically mourned. */
2057 if (now_garbage
> RT
->rt_time
) {
2062 /* Start poisoning a bad route before deleting it. */
2063 if (now
.tv_sec
- RT
->rt_time
> EXPIRE_TIME
) {
2064 struct rt_spare
new = RT
->rt_spares
[0];
2065 new.rts_metric
= HOPCNT_INFINITY
;
2066 rtchange(RT
, RT
->rt_state
, &new, 0);
2072 /* Watch for dead routes and interfaces.
2077 struct interface
*ifp
;
2080 /* If not listening to RIP, there is no need to age the routes in
2083 age_timer
.tv_sec
= (now
.tv_sec
2084 + ((rip_sock
< 0) ? NEVER
: SUPPLY_INTERVAL
));
2086 /* Check for dead IS_REMOTE interfaces by timing their
2089 for (ifp
= ifnet
; ifp
; ifp
= ifp
->int_next
) {
2090 if (!(ifp
->int_state
& IS_REMOTE
))
2093 /* ignore unreachable remote interfaces */
2094 if (!check_remote(ifp
))
2097 /* Restore remote interface that has become reachable
2099 if (ifp
->int_state
& IS_BROKE
)
2100 if_ok(ifp
, "remote ");
2102 if (ifp
->int_act_time
!= NEVER
2103 && now
.tv_sec
- ifp
->int_act_time
> EXPIRE_TIME
) {
2104 msglog("remote interface %s to %s timed out after"
2107 naddr_ntoa(ifp
->int_dstaddr
),
2108 (now
.tv_sec
- ifp
->int_act_time
)/60,
2109 (now
.tv_sec
- ifp
->int_act_time
)%60);
2113 /* If we have not heard from the other router
2116 if (now
.tv_sec
>= ifp
->int_query_time
) {
2117 ifp
->int_query_time
= NEVER
;
2123 age_bad_gate
= bad_gate
;
2124 rn_walktree(rhead
, walk_age
, 0);
2126 /* delete old redirected routes to keep the kernel table small
2127 * and prevent blackholes
2129 del_redirects(bad_gate
, now
.tv_sec
-STALE_TIME
);
2131 /* Update the kernel routing table. */
2134 /* poke reticent remote gateways */