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 * 4. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
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
36 #elif defined(__FreeBSD__)
39 __RCSID("$Revision: 2.27 $");
40 #ident "$Revision: 2.27 $"
43 static struct rt_spare *rts_better(struct rt_entry *);
44 static struct rt_spare rts_empty = {0,0,0,HOPCNT_INFINITY,0,0,0};
45 static void set_need_flash(void);
47 static void masktrim(struct sockaddr_in *ap);
49 static void masktrim(struct sockaddr_in_new *ap);
53 struct radix_node_head *rhead; /* root of the radix tree */
55 int need_flash = 1; /* flash update needed
56 * start =1 to suppress the 1st
59 struct timeval age_timer; /* next check of old routes */
60 struct timeval need_kern = { /* need to update kernel table */
61 EPOCH+MIN_WAITTIME-1, 0
68 /* zap any old routes through this gateway */
72 /* It is desirable to "aggregate" routes, to combine differing routes of
73 * the same metric and next hop into a common route with a smaller netmask
74 * or to suppress redundant routes, routes that add no information to
75 * routes with smaller netmasks.
77 * A route is redundant if and only if any and all routes with smaller
78 * but matching netmasks and nets are the same. Since routes are
79 * kept sorted in the radix tree, redundant routes always come second.
81 * There are two kinds of aggregations. First, two routes of the same bit
82 * mask and differing only in the least significant bit of the network
83 * number can be combined into a single route with a coarser mask.
85 * Second, a route can be suppressed in favor of another route with a more
86 * coarse mask provided no incompatible routes with intermediate masks
87 * are present. The second kind of aggregation involves suppressing routes.
88 * A route must not be suppressed if an incompatible route exists with
89 * an intermediate mask, since the suppressed route would be covered
90 * by the intermediate.
92 * This code relies on the radix tree walk encountering routes
93 * sorted first by address, with the smallest address first.
96 struct ag_info ag_slots[NUM_AG_SLOTS], *ag_avail, *ag_corsest, *ag_finest;
98 /* #define DEBUG_AG */
100 #define CHECK_AG() {int acnt = 0; struct ag_info *cag; \
101 for (cag = ag_avail; cag != 0; cag = cag->ag_fine) \
103 for (cag = ag_corsest; cag != 0; cag = cag->ag_fine) \
105 if (acnt != NUM_AG_SLOTS) { \
106 (void)fflush(stderr); \
115 /* Output the contents of an aggregation table slot.
116 * This function must always be immediately followed with the deletion
117 * of the target slot.
120 ag_out(struct ag_info *ag,
121 void (*out)(struct ag_info *))
123 struct ag_info *ag_cors;
127 /* Forget it if this route should not be output for split-horizon. */
128 if (ag->ag_state & AGS_SPLIT_HZ)
131 /* If we output both the even and odd twins, then the immediate parent,
132 * if it is present, is redundant, unless the parent manages to
133 * aggregate into something coarser.
134 * On successive calls, this code detects the even and odd twins,
135 * and marks the parent.
137 * Note that the order in which the radix tree code emits routes
138 * ensures that the twins are seen before the parent is emitted.
140 ag_cors = ag->ag_cors;
142 && ag_cors->ag_mask == ag->ag_mask<<1
143 && ag_cors->ag_dst_h == (ag->ag_dst_h & ag_cors->ag_mask)) {
144 ag_cors->ag_state |= ((ag_cors->ag_dst_h == ag->ag_dst_h)
149 /* Skip it if this route is itself redundant.
151 * It is ok to change the contents of the slot here, since it is
152 * always deleted next.
154 if (ag->ag_state & AGS_REDUN0) {
155 if (ag->ag_state & AGS_REDUN1)
156 return; /* quit if fully redundant */
157 /* make it finer if it is half-redundant */
158 bit = (-ag->ag_mask) >> 1;
162 } else if (ag->ag_state & AGS_REDUN1) {
163 /* make it finer if it is half-redundant */
164 bit = (-ag->ag_mask) >> 1;
172 ag_del(struct ag_info *ag)
176 if (ag->ag_cors == 0)
177 ag_corsest = ag->ag_fine;
179 ag->ag_cors->ag_fine = ag->ag_fine;
181 if (ag->ag_fine == 0)
182 ag_finest = ag->ag_cors;
184 ag->ag_fine->ag_cors = ag->ag_cors;
186 ag->ag_fine = ag_avail;
193 /* Flush routes waiting for aggregation.
194 * This must not suppress a route unless it is known that among all
195 * routes with coarser masks that match it, the one with the longest
196 * mask is appropriate. This is ensured by scanning the routes
197 * in lexical order, and with the most restrictive mask first
198 * among routes to the same destination.
201 ag_flush(naddr lim_dst_h, /* flush routes to here */
202 naddr lim_mask, /* matching this mask */
203 void (*out)(struct ag_info *))
205 struct ag_info *ag, *ag_cors;
210 ag != 0 && ag->ag_mask >= lim_mask;
212 ag_cors = ag->ag_cors;
214 /* work on only the specified routes */
215 dst_h = ag->ag_dst_h;
216 if ((dst_h & lim_mask) != lim_dst_h)
219 if (!(ag->ag_state & AGS_SUPPRESS))
222 else for ( ; ; ag_cors = ag_cors->ag_cors) {
223 /* Look for a route that can suppress the
226 /* failed, so output it and look for
227 * another route to work on
233 if ((dst_h & ag_cors->ag_mask) == ag_cors->ag_dst_h) {
234 /* We found a route with a coarser mask that
235 * aggregates the current target.
237 * If it has a different next hop, it
238 * cannot replace the target, so output
241 if (ag->ag_gate != ag_cors->ag_gate
242 && !(ag->ag_state & AGS_FINE_GATE)
243 && !(ag_cors->ag_state & AGS_CORS_GATE)) {
248 /* If the coarse route has a good enough
249 * metric, it suppresses the target.
250 * If the suppressed target was redundant,
251 * then mark the suppressor redundant.
253 if (ag_cors->ag_pref <= ag->ag_pref) {
254 if (AG_IS_REDUN(ag->ag_state)
255 && ag_cors->ag_mask==ag->ag_mask<<1) {
256 if (ag_cors->ag_dst_h == dst_h)
257 ag_cors->ag_state |= AGS_REDUN0;
259 ag_cors->ag_state |= AGS_REDUN1;
261 if (ag->ag_tag != ag_cors->ag_tag)
263 if (ag->ag_nhop != ag_cors->ag_nhop)
264 ag_cors->ag_nhop = 0;
270 /* That route has either been output or suppressed */
271 ag_cors = ag->ag_cors;
279 /* Try to aggregate a route with previous routes.
291 void (*out)(struct ag_info *)) /* output using this */
293 struct ag_info *ag, *nag, *ag_cors;
299 /* Punt non-contiguous subnet masks.
301 * (X & -X) contains a single bit if and only if X is a power of 2.
302 * (X + (X & -X)) == 0 if and only if X is a power of 2.
304 if ((mask & -mask) + mask != 0) {
305 struct ag_info nc_ag;
307 nc_ag.ag_dst_h = dst;
308 nc_ag.ag_mask = mask;
309 nc_ag.ag_gate = gate;
310 nc_ag.ag_nhop = nhop;
311 nc_ag.ag_metric = metric;
312 nc_ag.ag_pref = pref;
314 nc_ag.ag_state = state;
315 nc_ag.ag_seqno = new_seqno;
320 /* Search for the right slot in the aggregation table.
325 if (ag->ag_mask >= mask)
328 /* Suppress old routes (i.e. combine with compatible routes
329 * with coarser masks) as we look for the right slot in the
330 * aggregation table for the new route.
331 * A route to an address less than the current destination
332 * will not be affected by the current route or any route
333 * seen hereafter. That means it is safe to suppress it.
334 * This check keeps poor routes (e.g. with large hop counts)
335 * from preventing suppression of finer routes.
338 && ag->ag_dst_h < dst
339 && (ag->ag_state & AGS_SUPPRESS)
340 && ag_cors->ag_pref <= ag->ag_pref
341 && (ag->ag_dst_h & ag_cors->ag_mask) == ag_cors->ag_dst_h
342 && (ag_cors->ag_gate == ag->ag_gate
343 || (ag->ag_state & AGS_FINE_GATE)
344 || (ag_cors->ag_state & AGS_CORS_GATE))) {
345 /* If the suppressed target was redundant,
346 * then mark the suppressor redundant.
348 if (AG_IS_REDUN(ag->ag_state)
349 && ag_cors->ag_mask == ag->ag_mask<<1) {
350 if (ag_cors->ag_dst_h == dst)
351 ag_cors->ag_state |= AGS_REDUN0;
353 ag_cors->ag_state |= AGS_REDUN1;
355 if (ag->ag_tag != ag_cors->ag_tag)
357 if (ag->ag_nhop != ag_cors->ag_nhop)
358 ag_cors->ag_nhop = 0;
364 ag = ag_cors->ag_fine;
367 /* If we find the even/odd twin of the new route, and if the
368 * masks and so forth are equal, we can aggregate them.
369 * We can probably promote one of the pair.
371 * Since the routes are encountered in lexical order,
372 * the new route must be odd. However, the second or later
373 * times around this loop, it could be the even twin promoted
374 * from the even/odd pair of twins of the finer route.
377 && ag->ag_mask == mask
378 && ((ag->ag_dst_h ^ dst) & (mask<<1)) == 0) {
380 /* Here we know the target route and the route in the current
381 * slot have the same netmasks and differ by at most the
382 * last bit. They are either for the same destination, or
383 * for an even/odd pair of destinations.
385 if (ag->ag_dst_h == dst) {
386 /* We have two routes to the same destination.
387 * Routes are encountered in lexical order, so a
388 * route is never promoted until the parent route is
389 * already present. So we know that the new route is
390 * a promoted (or aggregated) pair and the route
391 * already in the slot is the explicit route.
393 * Prefer the best route if their metrics differ,
394 * or the aggregated one if not, following a sort
395 * of longest-match rule.
397 if (pref <= ag->ag_pref) {
401 ag->ag_metric = metric;
403 if (ag->ag_seqno < new_seqno)
404 ag->ag_seqno = new_seqno;
406 ag->ag_state = state;
410 /* Some bits are set if they are set on either route,
411 * except when the route is for an interface.
413 if (!(ag->ag_state & AGS_IF))
414 ag->ag_state |= (state & (AGS_AGGREGATE_EITHER
420 /* If one of the routes can be promoted and the other can
421 * be suppressed, it may be possible to combine them or
422 * worthwhile to promote one.
424 * Any route that can be promoted is always
425 * marked to be eligible to be suppressed.
427 if (!((state & AGS_AGGREGATE)
428 && (ag->ag_state & AGS_SUPPRESS))
429 && !((ag->ag_state & AGS_AGGREGATE)
430 && (state & AGS_SUPPRESS)))
433 /* A pair of even/odd twin routes can be combined
434 * if either is redundant, or if they are via the
435 * same gateway and have the same metric.
437 if (AG_IS_REDUN(ag->ag_state)
438 || AG_IS_REDUN(state)
439 || (ag->ag_gate == gate
440 && ag->ag_pref == pref
441 && (state & ag->ag_state & AGS_AGGREGATE) != 0)) {
443 /* We have both the even and odd pairs.
444 * Since the routes are encountered in order,
445 * the route in the slot must be the even twin.
447 * Combine and promote (aggregate) the pair of routes.
449 if (new_seqno < ag->ag_seqno)
450 new_seqno = ag->ag_seqno;
451 if (!AG_IS_REDUN(state))
452 state &= ~AGS_REDUN1;
453 if (AG_IS_REDUN(ag->ag_state))
456 state &= ~AGS_REDUN0;
457 state |= (ag->ag_state & AGS_AGGREGATE_EITHER);
458 if (ag->ag_tag != tag)
460 if (ag->ag_nhop != nhop)
463 /* Get rid of the even twin that was already
468 } else if (ag->ag_pref >= pref
469 && (ag->ag_state & AGS_AGGREGATE)) {
470 /* If we cannot combine the pair, maybe the route
471 * with the worse metric can be promoted.
473 * Promote the old, even twin, by giving its slot
474 * in the table to the new, odd twin.
490 /* The promoted route is even-redundant only if the
491 * even twin was fully redundant. It is not
492 * odd-redundant because the odd-twin will still be
499 ag->ag_state = state;
503 ag->ag_metric = metric;
510 /* take the newest sequence number */
511 if (new_seqno <= ag->ag_seqno)
512 new_seqno = ag->ag_seqno;
514 ag->ag_seqno = new_seqno;
517 if (!(state & AGS_AGGREGATE))
518 break; /* cannot promote either twin */
520 /* Promote the new, odd twin by shaving its
522 * The promoted route is odd-redundant only if the
523 * odd twin was fully redundant. It is not
524 * even-redundant because the even twin is still in
527 if (!AG_IS_REDUN(state))
528 state &= ~AGS_REDUN1;
529 state &= ~AGS_REDUN0;
530 if (new_seqno < ag->ag_seqno)
531 new_seqno = ag->ag_seqno;
533 ag->ag_seqno = new_seqno;
544 ag_cors = ag->ag_cors;
547 /* When we can no longer promote and combine routes,
548 * flush the old route in the target slot. Also flush
549 * any finer routes that we know will never be aggregated by
552 * In case we moved toward coarser masks,
553 * get back where we belong
556 && ag->ag_mask < mask) {
561 /* Empty the target slot
563 if (ag != 0 && ag->ag_mask == mask) {
564 ag_flush(ag->ag_dst_h, ag->ag_mask, out);
565 ag = (ag_cors == 0) ? ag_corsest : ag_cors->ag_fine;
569 (void)fflush(stderr);
570 if (ag == 0 && ag_cors != ag_finest)
572 if (ag_cors == 0 && ag != ag_corsest)
574 if (ag != 0 && ag->ag_cors != ag_cors)
576 if (ag_cors != 0 && ag_cors->ag_fine != ag)
581 /* Save the new route on the end of the table.
584 ag_avail = nag->ag_fine;
590 nag->ag_metric = metric;
593 nag->ag_state = state;
594 nag->ag_seqno = new_seqno;
601 nag->ag_cors = ag_cors;
605 ag_cors->ag_fine = nag;
612 rtm_type_name(u_char type)
614 static const char *rtm_types[] = {
635 #define NEW_RTM_PAT "RTM type %#x"
636 static char name0[sizeof(NEW_RTM_PAT)+2];
639 if (type > sizeof(rtm_types)/sizeof(rtm_types[0])
641 snprintf(name0, sizeof(name0), NEW_RTM_PAT, type);
644 return rtm_types[type-1];
650 /* Trim a mask in a sockaddr
651 * Produce a length of 0 for an address of 0.
652 * Otherwise produce the index of the first zero byte.
656 masktrim(struct sockaddr_in *ap)
658 masktrim(struct sockaddr_in_new *ap)
663 if (ap->sin_addr.s_addr == 0) {
667 cp = (char *)(&ap->sin_addr.s_addr+1);
670 ap->sin_len = cp - (char*)ap + 1;
674 /* Tell the kernel to add, delete or change a route
677 rtioctl(int action, /* RTM_DELETE, etc */
685 struct rt_msghdr w_rtm;
686 struct sockaddr_in w_dst;
687 struct sockaddr_in w_gate;
689 struct sockaddr_in w_mask;
691 struct sockaddr_in_new w_mask;
695 # define PAT " %-10s %s metric=%d flags=%#x"
696 # define ARGS rtm_type_name(action), rtname(dst,mask,gate), metric, flags
699 memset(&w, 0, sizeof(w));
700 w.w_rtm.rtm_msglen = sizeof(w);
701 w.w_rtm.rtm_version = RTM_VERSION;
702 w.w_rtm.rtm_type = action;
703 w.w_rtm.rtm_flags = flags;
704 w.w_rtm.rtm_seq = ++rt_sock_seqno;
705 w.w_rtm.rtm_addrs = RTA_DST|RTA_GATEWAY;
706 if (metric != 0 || action == RTM_CHANGE) {
707 w.w_rtm.rtm_rmx.rmx_hopcount = metric;
708 w.w_rtm.rtm_inits |= RTV_HOPCOUNT;
710 w.w_dst.sin_family = AF_INET;
711 w.w_dst.sin_addr.s_addr = dst;
712 w.w_gate.sin_family = AF_INET;
713 w.w_gate.sin_addr.s_addr = gate;
715 w.w_dst.sin_len = sizeof(w.w_dst);
716 w.w_gate.sin_len = sizeof(w.w_gate);
718 if (mask == HOST_MASK) {
719 w.w_rtm.rtm_flags |= RTF_HOST;
720 w.w_rtm.rtm_msglen -= sizeof(w.w_mask);
722 w.w_rtm.rtm_addrs |= RTA_NETMASK;
723 w.w_mask.sin_addr.s_addr = htonl(mask);
726 if (w.w_mask.sin_len == 0)
727 w.w_mask.sin_len = sizeof(long);
728 w.w_rtm.rtm_msglen -= (sizeof(w.w_mask) - w.w_mask.sin_len);
733 cc = write(rt_sock, &w, w.w_rtm.rtm_msglen);
736 && (action == RTM_CHANGE || action == RTM_DELETE)) {
737 trace_act("route disappeared before" PAT, ARGS);
738 if (action == RTM_CHANGE) {
744 msglog("write(rt_sock)" PAT ": %s", ARGS, strerror(errno));
746 } else if (cc != w.w_rtm.rtm_msglen) {
747 msglog("write(rt_sock) wrote %ld instead of %d for" PAT,
748 cc, w.w_rtm.rtm_msglen, ARGS);
753 trace_misc("write kernel" PAT, ARGS);
759 #define KHASH_SIZE 71 /* should be prime */
760 #define KHASH(a,m) khash_bins[((a) ^ (m)) % KHASH_SIZE]
761 static struct khash {
762 struct khash *k_next;
769 #define KS_DELETE 0x002 /* need to delete the route */
770 #define KS_ADD 0x004 /* add to the kernel */
771 #define KS_CHANGE 0x008 /* tell kernel to change the route */
772 #define KS_DEL_ADD 0x010 /* delete & add to change the kernel */
773 #define KS_STATIC 0x020 /* Static flag in kernel */
774 #define KS_GATEWAY 0x040 /* G flag in kernel */
775 #define KS_DYNAMIC 0x080 /* result of redirect */
776 #define KS_DELETED 0x100 /* already deleted from kernel */
777 #define KS_CHECK 0x200
779 #define K_KEEP_LIM 30
780 time_t k_redirect_time; /* when redirected route 1st seen */
781 } *khash_bins[KHASH_SIZE];
785 kern_find(naddr dst, naddr mask, struct khash ***ppk)
787 struct khash *k, **pk;
789 for (pk = &KHASH(dst,mask); (k = *pk) != 0; pk = &k->k_next) {
790 if (k->k_dst == dst && k->k_mask == mask)
800 kern_add(naddr dst, naddr mask)
802 struct khash *k, **pk;
804 k = kern_find(dst, mask, &pk);
808 k = (struct khash *)rtmalloc(sizeof(*k), "kern_add");
810 memset(k, 0, sizeof(*k));
814 k->k_keep = now.tv_sec;
821 /* If a kernel route has a non-zero metric, check that it is still in the
822 * daemon table, and not deleted by interfaces coming and going.
825 kern_check_static(struct khash *k,
826 struct interface *ifp)
831 if (k->k_metric == 0)
834 memset(&new, 0, sizeof(new));
836 new.rts_gate = k->k_gate;
837 new.rts_router = (ifp != 0) ? ifp->int_addr : loopaddr;
838 new.rts_metric = k->k_metric;
839 new.rts_time = now.tv_sec;
841 rt = rtget(k->k_dst, k->k_mask);
843 if (!(rt->rt_state & RS_STATIC))
844 rtchange(rt, rt->rt_state | RS_STATIC, &new, 0);
846 rtadd(k->k_dst, k->k_mask, RS_STATIC, &new);
851 /* operate on a kernel entry
854 kern_ioctl(struct khash *k,
855 int action, /* RTM_DELETE, etc */
861 k->k_state &= ~KS_DYNAMIC;
862 if (k->k_state & KS_DELETED)
864 k->k_state |= KS_DELETED;
867 k->k_state &= ~KS_DELETED;
870 if (k->k_state & KS_DELETED) {
872 k->k_state &= ~KS_DELETED;
877 rtioctl(action, k->k_dst, k->k_gate, k->k_mask, k->k_metric, flags);
881 /* add a route the kernel told us
884 rtm_add(struct rt_msghdr *rtm,
885 struct rt_addrinfo *info,
889 struct interface *ifp;
893 if (rtm->rtm_flags & RTF_HOST) {
895 } else if (INFO_MASK(info) != 0) {
896 mask = ntohl(S_ADDR(INFO_MASK(info)));
898 msglog("ignore %s without mask", rtm_type_name(rtm->rtm_type));
902 k = kern_add(S_ADDR(INFO_DST(info)), mask);
903 if (k->k_state & KS_NEW)
904 k->k_keep = now.tv_sec+keep;
905 if (INFO_GATE(info) == 0) {
906 trace_act("note %s without gateway",
907 rtm_type_name(rtm->rtm_type));
908 k->k_metric = HOPCNT_INFINITY;
909 } else if (INFO_GATE(info)->sa_family != AF_INET) {
910 trace_act("note %s with gateway AF=%d",
911 rtm_type_name(rtm->rtm_type),
912 INFO_GATE(info)->sa_family);
913 k->k_metric = HOPCNT_INFINITY;
915 k->k_gate = S_ADDR(INFO_GATE(info));
916 k->k_metric = rtm->rtm_rmx.rmx_hopcount;
919 else if (k->k_metric > HOPCNT_INFINITY-1)
920 k->k_metric = HOPCNT_INFINITY-1;
922 k->k_state &= ~(KS_DELETE | KS_ADD | KS_CHANGE | KS_DEL_ADD
923 | KS_DELETED | KS_GATEWAY | KS_STATIC
924 | KS_NEW | KS_CHECK);
925 if (rtm->rtm_flags & RTF_GATEWAY)
926 k->k_state |= KS_GATEWAY;
927 if (rtm->rtm_flags & RTF_STATIC)
928 k->k_state |= KS_STATIC;
930 if (0 != (rtm->rtm_flags & (RTF_DYNAMIC | RTF_MODIFIED))) {
931 if (INFO_AUTHOR(info) != 0
932 && INFO_AUTHOR(info)->sa_family == AF_INET)
933 ifp = iflookup(S_ADDR(INFO_AUTHOR(info)));
937 && (ifp == 0 || !(ifp->int_state & IS_REDIRECT_OK))) {
938 /* Routers are not supposed to listen to redirects,
939 * so delete it if it came via an unknown interface
940 * or the interface does not have special permission.
942 k->k_state &= ~KS_DYNAMIC;
943 k->k_state |= KS_DELETE;
944 LIM_SEC(need_kern, 0);
945 trace_act("mark for deletion redirected %s --> %s"
947 addrname(k->k_dst, k->k_mask, 0),
948 naddr_ntoa(k->k_gate),
949 ifp ? ifp->int_name : "unknown interface");
951 k->k_state |= KS_DYNAMIC;
952 k->k_redirect_time = now.tv_sec;
953 trace_act("accept redirected %s --> %s via %s",
954 addrname(k->k_dst, k->k_mask, 0),
955 naddr_ntoa(k->k_gate),
956 ifp ? ifp->int_name : "unknown interface");
961 /* If it is not a static route, quit until the next comparison
962 * between the kernel and daemon tables, when it will be deleted.
964 if (!(k->k_state & KS_STATIC)) {
965 k->k_state |= KS_DELETE;
966 LIM_SEC(need_kern, k->k_keep);
970 /* Put static routes with real metrics into the daemon table so
971 * they can be advertised.
973 * Find the interface toward the gateway.
975 ifp = iflookup(k->k_gate);
977 msglog("static route %s --> %s impossibly lacks ifp",
978 addrname(S_ADDR(INFO_DST(info)), mask, 0),
979 naddr_ntoa(k->k_gate));
981 kern_check_static(k, ifp);
985 /* deal with packet loss
988 rtm_lose(struct rt_msghdr *rtm,
989 struct rt_addrinfo *info)
991 if (INFO_GATE(info) == 0
992 || INFO_GATE(info)->sa_family != AF_INET) {
993 trace_act("ignore %s without gateway",
994 rtm_type_name(rtm->rtm_type));
999 rdisc_age(S_ADDR(INFO_GATE(info)));
1000 age(S_ADDR(INFO_GATE(info)));
1004 /* Make the gateway slot of an info structure point to something
1005 * useful. If it is not already useful, but it specifies an interface,
1006 * then fill in the sockaddr_in provided and point it there.
1009 get_info_gate(struct sockaddr **sap,
1010 struct sockaddr_in *rsin)
1012 struct sockaddr_dl *sdl = (struct sockaddr_dl *)*sap;
1013 struct interface *ifp;
1017 if ((sdl)->sdl_family == AF_INET)
1019 if ((sdl)->sdl_family != AF_LINK)
1022 ifp = ifwithindex(sdl->sdl_index, 1);
1026 rsin->sin_addr.s_addr = ifp->int_addr;
1028 rsin->sin_len = sizeof(*rsin);
1030 rsin->sin_family = AF_INET;
1031 *sap = (struct sockaddr*)rsin;
1037 /* Clean the kernel table by copying it to the daemon image.
1038 * Eventually the daemon will delete any extra routes.
1043 static char *sysctl_buf;
1044 static size_t sysctl_buf_size = 0;
1048 struct rt_msghdr *rtm;
1049 struct sockaddr_in gate_sin;
1050 struct rt_addrinfo info;
1055 for (i = 0; i < KHASH_SIZE; i++) {
1056 for (k = khash_bins[i]; k != 0; k = k->k_next) {
1057 k->k_state |= KS_CHECK;
1063 mib[2] = 0; /* protocol */
1064 mib[3] = 0; /* wildcard address family */
1065 mib[4] = NET_RT_DUMP;
1066 mib[5] = 0; /* no flags */
1068 if ((needed = sysctl_buf_size) != 0) {
1069 if (sysctl(mib, 6, sysctl_buf,&needed, 0, 0) >= 0)
1071 if (errno != ENOMEM && errno != EFAULT)
1072 BADERR(1,"flush_kern: sysctl(RT_DUMP)");
1076 if (sysctl(mib, 6, 0, &needed, 0, 0) < 0)
1077 BADERR(1,"flush_kern: sysctl(RT_DUMP) estimate");
1078 /* Kludge around the habit of some systems, such as
1079 * BSD/OS 3.1, to not admit how many routes are in the
1080 * kernel, or at least to be quite wrong.
1082 needed += 50*(sizeof(*rtm)+5*sizeof(struct sockaddr));
1083 sysctl_buf = rtmalloc(sysctl_buf_size = needed,
1084 "flush_kern sysctl(RT_DUMP)");
1087 lim = sysctl_buf + needed;
1088 for (next = sysctl_buf; next < lim; next += rtm->rtm_msglen) {
1089 rtm = (struct rt_msghdr *)next;
1090 if (rtm->rtm_msglen == 0) {
1091 msglog("zero length kernel route at "
1092 " %#lx in buffer %#lx before %#lx",
1093 (u_long)rtm, (u_long)sysctl_buf, (u_long)lim);
1098 (struct sockaddr *)(rtm+1),
1099 (struct sockaddr *)(next + rtm->rtm_msglen),
1102 if (INFO_DST(&info) == 0
1103 || INFO_DST(&info)->sa_family != AF_INET)
1106 /* ignore ARP table entries on systems with a merged route
1109 if (rtm->rtm_flags & RTF_LLINFO)
1112 #if defined(RTF_WASCLONED) && defined(__FreeBSD__)
1113 /* ignore cloned routes
1115 if (rtm->rtm_flags & RTF_WASCLONED)
1119 /* ignore multicast addresses
1121 if (IN_MULTICAST(ntohl(S_ADDR(INFO_DST(&info)))))
1124 if (!get_info_gate(&INFO_GATE(&info), &gate_sin))
1127 /* Note static routes and interface routes, and also
1128 * preload the image of the kernel table so that
1129 * we can later clean it, as well as avoid making
1130 * unneeded changes. Keep the old kernel routes for a
1131 * few seconds to allow a RIP or router-discovery
1132 * response to be heard.
1134 rtm_add(rtm,&info,MIN_WAITTIME);
1137 for (i = 0; i < KHASH_SIZE; i++) {
1138 for (k = khash_bins[i]; k != 0; k = k->k_next) {
1139 if (k->k_state & KS_CHECK) {
1140 msglog("%s --> %s disappeared from kernel",
1141 addrname(k->k_dst, k->k_mask, 0),
1142 naddr_ntoa(k->k_gate));
1143 del_static(k->k_dst, k->k_mask, k->k_gate, 1);
1150 /* Listen to announcements from the kernel
1156 struct interface *ifp;
1157 struct sockaddr_in gate_sin;
1161 struct rt_msghdr rtm;
1162 struct sockaddr addrs[RTAX_MAX];
1164 struct if_msghdr ifm;
1166 char str[100], *strp;
1167 struct rt_addrinfo info;
1171 cc = read(rt_sock, &m, sizeof(m));
1173 if (cc < 0 && errno != EWOULDBLOCK)
1174 LOGERR("read(rt_sock)");
1178 if (m.r.rtm.rtm_version != RTM_VERSION) {
1179 msglog("bogus routing message version %d",
1180 m.r.rtm.rtm_version);
1184 /* Ignore our own results.
1186 if (m.r.rtm.rtm_type <= RTM_CHANGE
1187 && m.r.rtm.rtm_pid == mypid) {
1188 static int complained = 0;
1190 msglog("receiving our own change messages");
1196 if (m.r.rtm.rtm_type == RTM_IFINFO
1197 || m.r.rtm.rtm_type == RTM_NEWADDR
1198 || m.r.rtm.rtm_type == RTM_DELADDR) {
1199 ifp = ifwithindex(m.ifm.ifm_index,
1200 m.r.rtm.rtm_type != RTM_DELADDR);
1202 trace_act("note %s with flags %#x"
1203 " for unknown interface index #%d",
1204 rtm_type_name(m.r.rtm.rtm_type),
1208 trace_act("note %s with flags %#x for %s",
1209 rtm_type_name(m.r.rtm.rtm_type),
1213 /* After being informed of a change to an interface,
1214 * check them all now if the check would otherwise
1215 * be a long time from now, if the interface is
1216 * not known, or if the interface has been turned
1219 if (ifinit_timer.tv_sec-now.tv_sec>=CHECK_BAD_INTERVAL
1221 || ((ifp->int_if_flags ^ m.ifm.ifm_flags)
1223 ifinit_timer.tv_sec = now.tv_sec;
1227 if (m.r.rtm.rtm_type == RTM_OIFINFO)
1228 continue; /* ignore compat message */
1231 strcpy(str, rtm_type_name(m.r.rtm.rtm_type));
1232 strp = &str[strlen(str)];
1233 if (m.r.rtm.rtm_type <= RTM_CHANGE)
1234 strp += sprintf(strp," from pid %d",m.r.rtm.rtm_pid);
1236 rt_xaddrs(&info, m.r.addrs, &m.r.addrs[RTAX_MAX],
1239 if (INFO_DST(&info) == 0) {
1240 trace_act("ignore %s without dst", str);
1244 if (INFO_DST(&info)->sa_family != AF_INET) {
1245 trace_act("ignore %s for AF %d", str,
1246 INFO_DST(&info)->sa_family);
1250 mask = ((INFO_MASK(&info) != 0)
1251 ? ntohl(S_ADDR(INFO_MASK(&info)))
1252 : (m.r.rtm.rtm_flags & RTF_HOST)
1254 : std_mask(S_ADDR(INFO_DST(&info))));
1256 strp += sprintf(strp, ": %s",
1257 addrname(S_ADDR(INFO_DST(&info)), mask, 0));
1259 if (IN_MULTICAST(ntohl(S_ADDR(INFO_DST(&info))))) {
1260 trace_act("ignore multicast %s", str);
1264 if (m.r.rtm.rtm_flags & RTF_LLINFO) {
1265 trace_act("ignore ARP %s", str);
1269 #if defined(RTF_WASCLONED) && defined(__FreeBSD__)
1270 if (m.r.rtm.rtm_flags & RTF_WASCLONED) {
1271 trace_act("ignore cloned %s", str);
1276 if (get_info_gate(&INFO_GATE(&info), &gate_sin)) {
1277 gate = S_ADDR(INFO_GATE(&info));
1278 strp += sprintf(strp, " --> %s", naddr_ntoa(gate));
1283 if (INFO_AUTHOR(&info) != 0)
1284 strp += sprintf(strp, " by authority of %s",
1285 saddr_ntoa(INFO_AUTHOR(&info)));
1287 switch (m.r.rtm.rtm_type) {
1291 if (m.r.rtm.rtm_errno != 0) {
1292 trace_act("ignore %s with \"%s\" error",
1293 str, strerror(m.r.rtm.rtm_errno));
1295 trace_act("%s", str);
1296 rtm_add(&m.r.rtm,&info,0);
1301 if (m.r.rtm.rtm_errno != 0
1302 && m.r.rtm.rtm_errno != ESRCH) {
1303 trace_act("ignore %s with \"%s\" error",
1304 str, strerror(m.r.rtm.rtm_errno));
1306 trace_act("%s", str);
1307 del_static(S_ADDR(INFO_DST(&info)), mask,
1313 trace_act("%s", str);
1314 rtm_lose(&m.r.rtm,&info);
1318 trace_act("ignore %s", str);
1325 /* after aggregating, note routes that belong in the kernel
1328 kern_out(struct ag_info *ag)
1333 /* Do not install bad routes if they are not already present.
1334 * This includes routes that had RS_NET_SYN for interfaces that
1337 if (ag->ag_metric == HOPCNT_INFINITY) {
1338 k = kern_find(htonl(ag->ag_dst_h), ag->ag_mask, 0);
1342 k = kern_add(htonl(ag->ag_dst_h), ag->ag_mask);
1345 if (k->k_state & KS_NEW) {
1346 /* will need to add new entry to the kernel table */
1347 k->k_state = KS_ADD;
1348 if (ag->ag_state & AGS_GATEWAY)
1349 k->k_state |= KS_GATEWAY;
1350 k->k_gate = ag->ag_gate;
1351 k->k_metric = ag->ag_metric;
1355 if (k->k_state & KS_STATIC)
1358 /* modify existing kernel entry if necessary */
1359 if (k->k_gate != ag->ag_gate
1360 || k->k_metric != ag->ag_metric) {
1361 /* Must delete bad interface routes etc. to change them. */
1362 if (k->k_metric == HOPCNT_INFINITY)
1363 k->k_state |= KS_DEL_ADD;
1364 k->k_gate = ag->ag_gate;
1365 k->k_metric = ag->ag_metric;
1366 k->k_state |= KS_CHANGE;
1369 /* If the daemon thinks the route should exist, forget
1370 * about any redirections.
1371 * If the daemon thinks the route should exist, eventually
1372 * override manual intervention by the operator.
1374 if ((k->k_state & (KS_DYNAMIC | KS_DELETED)) != 0) {
1375 k->k_state &= ~KS_DYNAMIC;
1376 k->k_state |= (KS_ADD | KS_DEL_ADD);
1379 if ((k->k_state & KS_GATEWAY)
1380 && !(ag->ag_state & AGS_GATEWAY)) {
1381 k->k_state &= ~KS_GATEWAY;
1382 k->k_state |= (KS_ADD | KS_DEL_ADD);
1383 } else if (!(k->k_state & KS_GATEWAY)
1384 && (ag->ag_state & AGS_GATEWAY)) {
1385 k->k_state |= KS_GATEWAY;
1386 k->k_state |= (KS_ADD | KS_DEL_ADD);
1389 /* Deleting-and-adding is necessary to change aspects of a route.
1390 * Just delete instead of deleting and then adding a bad route.
1391 * Otherwise, we want to keep the route in the kernel.
1393 if (k->k_metric == HOPCNT_INFINITY
1394 && (k->k_state & KS_DEL_ADD))
1395 k->k_state |= KS_DELETE;
1397 k->k_state &= ~KS_DELETE;
1404 walk_kern(struct radix_node *rn,
1405 struct walkarg *argp UNUSED)
1407 #define RT ((struct rt_entry *)rn)
1412 /* Do not install synthetic routes */
1413 if (RT->rt_state & RS_NET_SYN)
1416 if (!(RT->rt_state & RS_IF)) {
1417 /* This is an ordinary route, not for an interface.
1420 /* aggregate, ordinary good routes without regard to
1424 ags |= (AGS_GATEWAY | AGS_SUPPRESS | AGS_AGGREGATE);
1426 /* Do not install host routes directly to hosts, to avoid
1427 * interfering with ARP entries in the kernel table.
1430 && ntohl(RT->rt_dst) == RT->rt_gate)
1434 /* This is an interface route.
1435 * Do not install routes for "external" remote interfaces.
1437 if (RT->rt_ifp != 0 && (RT->rt_ifp->int_state & IS_EXTERNAL))
1440 /* Interfaces should override received routes.
1443 ags |= (AGS_IF | AGS_CORS_GATE);
1445 /* If it is not an interface, or an alias for an interface,
1446 * it must be a "gateway."
1448 * If it is a "remote" interface, it is also a "gateway" to
1449 * the kernel if is not an alias.
1452 || (RT->rt_ifp->int_state & IS_REMOTE))
1453 ags |= (AGS_GATEWAY | AGS_SUPPRESS | AGS_AGGREGATE);
1456 /* If RIP is off and IRDP is on, let the route to the discovered
1457 * route suppress any RIP routes. Eventually the RIP routes
1458 * will time-out and be deleted. This reaches the steady-state
1461 if ((RT->rt_state & RS_RDISC) && rip_sock < 0)
1462 ags |= AGS_CORS_GATE;
1464 metric = RT->rt_metric;
1465 if (metric == HOPCNT_INFINITY) {
1466 /* if the route is dead, so try hard to aggregate. */
1467 pref = HOPCNT_INFINITY;
1468 ags |= (AGS_FINE_GATE | AGS_SUPPRESS);
1469 ags &= ~(AGS_IF | AGS_CORS_GATE);
1472 ag_check(RT->rt_dst, RT->rt_mask, RT->rt_gate, 0,
1473 metric,pref, 0, 0, ags, kern_out);
1479 /* Update the kernel table to match the daemon table.
1485 struct khash *k, **pk;
1488 need_kern = age_timer;
1490 /* Walk daemon table, updating the copy of the kernel table.
1492 (void)rn_walktree(rhead, walk_kern, 0);
1493 ag_flush(0,0,kern_out);
1495 for (i = 0; i < KHASH_SIZE; i++) {
1496 for (pk = &khash_bins[i]; (k = *pk) != 0; ) {
1497 /* Do not touch static routes */
1498 if (k->k_state & KS_STATIC) {
1499 kern_check_static(k,0);
1504 /* check hold on routes deleted by the operator */
1505 if (k->k_keep > now.tv_sec) {
1506 /* ensure we check when the hold is over */
1507 LIM_SEC(need_kern, k->k_keep);
1508 /* mark for the next cycle */
1509 k->k_state |= KS_DELETE;
1514 if ((k->k_state & KS_DELETE)
1515 && !(k->k_state & KS_DYNAMIC)) {
1516 kern_ioctl(k, RTM_DELETE, 0);
1522 if (k->k_state & KS_DEL_ADD)
1523 kern_ioctl(k, RTM_DELETE, 0);
1525 if (k->k_state & KS_ADD) {
1526 kern_ioctl(k, RTM_ADD,
1527 ((0 != (k->k_state & (KS_GATEWAY
1529 ? RTF_GATEWAY : 0));
1530 } else if (k->k_state & KS_CHANGE) {
1531 kern_ioctl(k, RTM_CHANGE,
1532 ((0 != (k->k_state & (KS_GATEWAY
1534 ? RTF_GATEWAY : 0));
1536 k->k_state &= ~(KS_ADD|KS_CHANGE|KS_DEL_ADD);
1538 /* Mark this route to be deleted in the next cycle.
1539 * This deletes routes that disappear from the
1540 * daemon table, since the normal aging code
1541 * will clear the bit for routes that have not
1542 * disappeared from the daemon table.
1544 k->k_state |= KS_DELETE;
1551 /* Delete a static route in the image of the kernel table.
1554 del_static(naddr dst,
1560 struct rt_entry *rt;
1562 /* Just mark it in the table to be deleted next time the kernel
1564 * If it has already been deleted, mark it as such, and set its
1565 * keep-timer so that it will not be deleted again for a while.
1566 * This lets the operator delete a route added by the daemon
1567 * and add a replacement.
1569 k = kern_find(dst, mask, 0);
1570 if (k != 0 && (gate == 0 || k->k_gate == gate)) {
1571 k->k_state &= ~(KS_STATIC | KS_DYNAMIC | KS_CHECK);
1572 k->k_state |= KS_DELETE;
1574 k->k_state |= KS_DELETED;
1575 k->k_keep = now.tv_sec + K_KEEP_LIM;
1579 rt = rtget(dst, mask);
1580 if (rt != 0 && (rt->rt_state & RS_STATIC))
1585 /* Delete all routes generated from ICMP Redirects that use a given gateway,
1586 * as well as old redirected routes.
1589 del_redirects(naddr bad_gate,
1596 for (i = 0; i < KHASH_SIZE; i++) {
1597 for (k = khash_bins[i]; k != 0; k = k->k_next) {
1598 if (!(k->k_state & KS_DYNAMIC)
1599 || (k->k_state & KS_STATIC))
1602 if (k->k_gate != bad_gate
1603 && k->k_redirect_time > old
1607 k->k_state |= KS_DELETE;
1608 k->k_state &= ~KS_DYNAMIC;
1609 need_kern.tv_sec = now.tv_sec;
1610 trace_act("mark redirected %s --> %s for deletion",
1611 addrname(k->k_dst, k->k_mask, 0),
1612 naddr_ntoa(k->k_gate));
1618 /* Start the daemon tables.
1620 extern int max_keylen;
1628 /* Initialize the radix trees */
1629 max_keylen = sizeof(struct sockaddr_in);
1631 rn_inithead((void**)&rhead, 32);
1633 /* mark all of the slots in the table free */
1634 ag_avail = ag_slots;
1635 for (ag = ag_slots, i = 1; i < NUM_AG_SLOTS; i++) {
1642 #ifdef _HAVE_SIN_LEN
1643 static struct sockaddr_in dst_sock = {sizeof(dst_sock), AF_INET, 0, {0}, {0}};
1644 static struct sockaddr_in mask_sock = {sizeof(mask_sock), AF_INET, 0, {0}, {0}};
1646 static struct sockaddr_in_new dst_sock = {_SIN_ADDR_SIZE, AF_INET};
1647 static struct sockaddr_in_new mask_sock = {_SIN_ADDR_SIZE, AF_INET};
1652 set_need_flash(void)
1656 /* Do not send the flash update immediately. Wait a little
1657 * while to hear from other routers.
1659 no_flash.tv_sec = now.tv_sec + MIN_WAITTIME;
1664 /* Get a particular routing table entry
1667 rtget(naddr dst, naddr mask)
1669 struct rt_entry *rt;
1671 dst_sock.sin_addr.s_addr = dst;
1672 mask_sock.sin_addr.s_addr = htonl(mask);
1673 masktrim(&mask_sock);
1674 rt = (struct rt_entry *)rhead->rnh_lookup(&dst_sock,&mask_sock,rhead);
1676 || rt->rt_dst != dst
1677 || rt->rt_mask != mask)
1684 /* Find a route to dst as the kernel would.
1689 dst_sock.sin_addr.s_addr = dst;
1690 return (struct rt_entry *)rhead->rnh_matchaddr(&dst_sock, rhead);
1694 /* add a route to the table
1699 u_int state, /* rt_state for the entry */
1700 struct rt_spare *new)
1702 struct rt_entry *rt;
1705 struct rt_spare *rts;
1707 rt = (struct rt_entry *)rtmalloc(sizeof (*rt), "rtadd");
1708 memset(rt, 0, sizeof(*rt));
1709 for (rts = rt->rt_spares, i = NUM_SPARES; i != 0; i--, rts++)
1710 rts->rts_metric = HOPCNT_INFINITY;
1712 rt->rt_nodes->rn_key = (caddr_t)&rt->rt_dst_sock;
1714 rt->rt_dst_sock.sin_family = AF_INET;
1715 #ifdef _HAVE_SIN_LEN
1716 rt->rt_dst_sock.sin_len = dst_sock.sin_len;
1718 if (mask != HOST_MASK) {
1719 smask = std_mask(dst);
1720 if ((smask & ~mask) == 0 && mask > smask)
1723 mask_sock.sin_addr.s_addr = htonl(mask);
1724 masktrim(&mask_sock);
1726 rt->rt_state = state;
1727 rt->rt_spares[0] = *new;
1728 rt->rt_time = now.tv_sec;
1729 rt->rt_poison_metric = HOPCNT_INFINITY;
1730 rt->rt_seqno = update_seqno;
1732 if (++total_routes == MAX_ROUTES)
1733 msglog("have maximum (%d) routes", total_routes);
1735 trace_add_del("Add", rt);
1737 need_kern.tv_sec = now.tv_sec;
1740 if (0 == rhead->rnh_addaddr(&rt->rt_dst_sock, &mask_sock,
1741 rhead, rt->rt_nodes)) {
1742 msglog("rnh_addaddr() failed for %s mask=%#lx",
1743 naddr_ntoa(dst), (u_long)mask);
1749 /* notice a changed route
1752 rtchange(struct rt_entry *rt,
1753 u_int state, /* new state bits */
1754 struct rt_spare *new,
1757 if (rt->rt_metric != new->rts_metric) {
1758 /* Fix the kernel immediately if it seems the route
1759 * has gone bad, since there may be a working route that
1760 * aggregates this route.
1762 if (new->rts_metric == HOPCNT_INFINITY) {
1763 need_kern.tv_sec = now.tv_sec;
1764 if (new->rts_time >= now.tv_sec - EXPIRE_TIME)
1765 new->rts_time = now.tv_sec - EXPIRE_TIME;
1767 rt->rt_seqno = update_seqno;
1771 if (rt->rt_gate != new->rts_gate) {
1772 need_kern.tv_sec = now.tv_sec;
1773 rt->rt_seqno = update_seqno;
1777 state |= (rt->rt_state & RS_SUBNET);
1779 /* Keep various things from deciding ageless routes are stale.
1781 if (!AGE_RT(state, new->rts_ifp))
1782 new->rts_time = now.tv_sec;
1785 trace_change(rt, state, new,
1786 label ? label : "Chg ");
1788 rt->rt_state = state;
1789 rt->rt_spares[0] = *new;
1793 /* check for a better route among the spares
1795 static struct rt_spare *
1796 rts_better(struct rt_entry *rt)
1798 struct rt_spare *rts, *rts1;
1801 /* find the best alternative among the spares */
1802 rts = rt->rt_spares+1;
1803 for (i = NUM_SPARES, rts1 = rts+1; i > 2; i--, rts1++) {
1804 if (BETTER_LINK(rt,rts1,rts))
1812 /* switch to a backup route
1815 rtswitch(struct rt_entry *rt,
1816 struct rt_spare *rts)
1818 struct rt_spare swap;
1822 /* Do not change permanent routes */
1823 if (0 != (rt->rt_state & (RS_MHOME | RS_STATIC | RS_RDISC
1824 | RS_NET_SYN | RS_IF)))
1827 /* find the best alternative among the spares */
1829 rts = rts_better(rt);
1831 /* Do not bother if it is not worthwhile.
1833 if (!BETTER_LINK(rt, rts, rt->rt_spares))
1836 swap = rt->rt_spares[0];
1837 (void)sprintf(label, "Use #%d", (int)(rts - rt->rt_spares));
1838 rtchange(rt, rt->rt_state & ~(RS_NET_SYN | RS_RDISC), rts, label);
1839 if (swap.rts_metric == HOPCNT_INFINITY) {
1848 rtdelete(struct rt_entry *rt)
1854 trace_add_del("Del", rt);
1856 k = kern_find(rt->rt_dst, rt->rt_mask, 0);
1858 k->k_state |= KS_DELETE;
1859 need_kern.tv_sec = now.tv_sec;
1862 dst_sock.sin_addr.s_addr = rt->rt_dst;
1863 mask_sock.sin_addr.s_addr = htonl(rt->rt_mask);
1864 masktrim(&mask_sock);
1865 if (rt != (struct rt_entry *)rhead->rnh_deladdr(&dst_sock, &mask_sock,
1867 msglog("rnh_deladdr() failed");
1876 rts_delete(struct rt_entry *rt,
1877 struct rt_spare *rts)
1879 trace_upslot(rt, rts, &rts_empty);
1884 /* Get rid of a bad route, and try to switch to a replacement.
1887 rtbad(struct rt_entry *rt)
1889 struct rt_spare new;
1891 /* Poison the route */
1892 new = rt->rt_spares[0];
1893 new.rts_metric = HOPCNT_INFINITY;
1894 rtchange(rt, rt->rt_state & ~(RS_IF | RS_LOCAL | RS_STATIC), &new, 0);
1899 /* Junk a RS_NET_SYN or RS_LOCAL route,
1900 * unless it is needed by another interface.
1903 rtbad_sub(struct rt_entry *rt)
1905 struct interface *ifp, *ifp1;
1906 struct intnet *intnetp;
1913 if (rt->rt_state & RS_LOCAL) {
1914 /* Is this the route through loopback for the interface?
1915 * If so, see if it is used by any other interfaces, such
1916 * as a point-to-point interface with the same local address.
1918 for (ifp = ifnet; ifp != 0; ifp = ifp->int_next) {
1919 /* Retain it if another interface needs it.
1921 if (ifp->int_addr == rt->rt_ifp->int_addr) {
1930 if (!(state & RS_LOCAL)) {
1931 /* Retain RIPv1 logical network route if there is another
1932 * interface that justifies it.
1934 if (rt->rt_state & RS_NET_SYN) {
1935 for (ifp = ifnet; ifp != 0; ifp = ifp->int_next) {
1936 if ((ifp->int_state & IS_NEED_NET_SYN)
1937 && rt->rt_mask == ifp->int_std_mask
1938 && rt->rt_dst == ifp->int_std_addr) {
1939 state |= RS_NET_SYN;
1946 /* or if there is an authority route that needs it. */
1947 for (intnetp = intnets;
1949 intnetp = intnetp->intnet_next) {
1950 if (intnetp->intnet_addr == rt->rt_dst
1951 && intnetp->intnet_mask == rt->rt_mask) {
1952 state |= (RS_NET_SYN | RS_NET_INT);
1958 if (ifp1 != 0 || (state & RS_NET_SYN)) {
1959 struct rt_spare new = rt->rt_spares[0];
1961 rtchange(rt, ((rt->rt_state & ~(RS_NET_SYN|RS_LOCAL)) | state),
1969 /* Called while walking the table looking for sick interfaces
1970 * or after a time change.
1974 walk_bad(struct radix_node *rn,
1975 struct walkarg *argp UNUSED)
1977 #define RT ((struct rt_entry *)rn)
1978 struct rt_spare *rts;
1982 /* fix any spare routes through the interface
1984 rts = RT->rt_spares;
1985 for (i = NUM_SPARES; i != 1; i--) {
1987 if (rts->rts_metric < HOPCNT_INFINITY
1988 && (rts->rts_ifp == 0
1989 || (rts->rts_ifp->int_state & IS_BROKE)))
1990 rts_delete(RT, rts);
1993 /* Deal with the main route
1995 /* finished if it has been handled before or if its interface is ok
1997 if (RT->rt_ifp == 0 || !(RT->rt_ifp->int_state & IS_BROKE))
2000 /* Bad routes for other than interfaces are easy.
2002 if (0 == (RT->rt_state & (RS_IF | RS_NET_SYN | RS_LOCAL))) {
2013 /* Check the age of an individual route.
2017 walk_age(struct radix_node *rn,
2018 struct walkarg *argp UNUSED)
2020 #define RT ((struct rt_entry *)rn)
2021 struct interface *ifp;
2022 struct rt_spare *rts;
2026 /* age all of the spare routes, including the primary route
2029 rts = RT->rt_spares;
2030 for (i = NUM_SPARES; i != 0; i--, rts++) {
2033 if (i == NUM_SPARES) {
2034 if (!AGE_RT(RT->rt_state, ifp)) {
2035 /* Keep various things from deciding ageless
2038 rts->rts_time = now.tv_sec;
2042 /* forget RIP routes after RIP has been turned off.
2050 /* age failing routes
2052 if (age_bad_gate == rts->rts_gate
2053 && rts->rts_time >= now_stale) {
2054 rts->rts_time -= SUPPLY_INTERVAL;
2057 /* trash the spare routes when they go bad */
2058 if (rts->rts_metric < HOPCNT_INFINITY
2059 && now_garbage > rts->rts_time
2061 rts_delete(RT, rts);
2065 /* finished if the active route is still fresh */
2066 if (now_stale <= RT->rt_time)
2069 /* try to switch to an alternative */
2072 /* Delete a dead route after it has been publically mourned. */
2073 if (now_garbage > RT->rt_time) {
2078 /* Start poisoning a bad route before deleting it. */
2079 if (now.tv_sec - RT->rt_time > EXPIRE_TIME) {
2080 struct rt_spare new = RT->rt_spares[0];
2081 new.rts_metric = HOPCNT_INFINITY;
2082 rtchange(RT, RT->rt_state, &new, 0);
2088 /* Watch for dead routes and interfaces.
2093 struct interface *ifp;
2096 /* If not listening to RIP, there is no need to age the routes in
2099 age_timer.tv_sec = (now.tv_sec
2100 + ((rip_sock < 0) ? NEVER : SUPPLY_INTERVAL));
2102 /* Check for dead IS_REMOTE interfaces by timing their
2105 for (ifp = ifnet; ifp; ifp = ifp->int_next) {
2106 if (!(ifp->int_state & IS_REMOTE))
2109 /* ignore unreachable remote interfaces */
2110 if (!check_remote(ifp))
2113 /* Restore remote interface that has become reachable
2115 if (ifp->int_state & IS_BROKE)
2116 if_ok(ifp, "remote ");
2118 if (ifp->int_act_time != NEVER
2119 && now.tv_sec - ifp->int_act_time > EXPIRE_TIME) {
2120 msglog("remote interface %s to %s timed out after"
2123 naddr_ntoa(ifp->int_dstaddr),
2124 (now.tv_sec - ifp->int_act_time)/60,
2125 (now.tv_sec - ifp->int_act_time)%60);
2129 /* If we have not heard from the other router
2132 if (now.tv_sec >= ifp->int_query_time) {
2133 ifp->int_query_time = NEVER;
2139 age_bad_gate = bad_gate;
2140 (void)rn_walktree(rhead, walk_age, 0);
2142 /* delete old redirected routes to keep the kernel table small
2143 * and prevent blackholes
2145 del_redirects(bad_gate, now.tv_sec-STALE_TIME);
2147 /* Update the kernel routing table. */
2150 /* poke reticent remote gateways */