2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 1983, 1988, 1993
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 static struct rt_spare *rts_better(struct rt_entry *);
39 static struct rt_spare rts_empty = {0,0,0,HOPCNT_INFINITY,0,0,0};
40 static void set_need_flash(void);
42 static void masktrim(struct sockaddr_in *ap);
44 static void masktrim(struct sockaddr_in_new *ap);
46 static void rtbad(struct rt_entry *);
49 struct radix_node_head *rhead; /* root of the radix tree */
51 int need_flash = 1; /* flash update needed
52 * start =1 to suppress the 1st
55 struct timeval age_timer; /* next check of old routes */
56 struct timeval need_kern = { /* need to update kernel table */
57 EPOCH+MIN_WAITTIME-1, 0
64 /* zap any old routes through this gateway */
65 static naddr age_bad_gate;
68 /* It is desirable to "aggregate" routes, to combine differing routes of
69 * the same metric and next hop into a common route with a smaller netmask
70 * or to suppress redundant routes, routes that add no information to
71 * routes with smaller netmasks.
73 * A route is redundant if and only if any and all routes with smaller
74 * but matching netmasks and nets are the same. Since routes are
75 * kept sorted in the radix tree, redundant routes always come second.
77 * There are two kinds of aggregations. First, two routes of the same bit
78 * mask and differing only in the least significant bit of the network
79 * number can be combined into a single route with a coarser mask.
81 * Second, a route can be suppressed in favor of another route with a more
82 * coarse mask provided no incompatible routes with intermediate masks
83 * are present. The second kind of aggregation involves suppressing routes.
84 * A route must not be suppressed if an incompatible route exists with
85 * an intermediate mask, since the suppressed route would be covered
86 * by the intermediate.
88 * This code relies on the radix tree walk encountering routes
89 * sorted first by address, with the smallest address first.
92 static struct ag_info ag_slots[NUM_AG_SLOTS], *ag_avail, *ag_corsest, *ag_finest;
94 /* #define DEBUG_AG */
96 #define CHECK_AG() {int acnt = 0; struct ag_info *cag; \
97 for (cag = ag_avail; cag != NULL; cag = cag->ag_fine) \
99 for (cag = ag_corsest; cag != NULL; cag = cag->ag_fine) \
101 if (acnt != NUM_AG_SLOTS) { \
102 (void)fflush(stderr); \
111 /* Output the contents of an aggregation table slot.
112 * This function must always be immediately followed with the deletion
113 * of the target slot.
116 ag_out(struct ag_info *ag,
117 void (*out)(struct ag_info *))
119 struct ag_info *ag_cors;
123 /* Forget it if this route should not be output for split-horizon. */
124 if (ag->ag_state & AGS_SPLIT_HZ)
127 /* If we output both the even and odd twins, then the immediate parent,
128 * if it is present, is redundant, unless the parent manages to
129 * aggregate into something coarser.
130 * On successive calls, this code detects the even and odd twins,
131 * and marks the parent.
133 * Note that the order in which the radix tree code emits routes
134 * ensures that the twins are seen before the parent is emitted.
136 ag_cors = ag->ag_cors;
138 && ag_cors->ag_mask == ag->ag_mask<<1
139 && ag_cors->ag_dst_h == (ag->ag_dst_h & ag_cors->ag_mask)) {
140 ag_cors->ag_state |= ((ag_cors->ag_dst_h == ag->ag_dst_h)
145 /* Skip it if this route is itself redundant.
147 * It is ok to change the contents of the slot here, since it is
148 * always deleted next.
150 if (ag->ag_state & AGS_REDUN0) {
151 if (ag->ag_state & AGS_REDUN1)
152 return; /* quit if fully redundant */
153 /* make it finer if it is half-redundant */
154 bit = (-ag->ag_mask) >> 1;
158 } else if (ag->ag_state & AGS_REDUN1) {
159 /* make it finer if it is half-redundant */
160 bit = (-ag->ag_mask) >> 1;
168 ag_del(struct ag_info *ag)
172 if (ag->ag_cors == NULL)
173 ag_corsest = ag->ag_fine;
175 ag->ag_cors->ag_fine = ag->ag_fine;
177 if (ag->ag_fine == NULL)
178 ag_finest = ag->ag_cors;
180 ag->ag_fine->ag_cors = ag->ag_cors;
182 ag->ag_fine = ag_avail;
189 /* Flush routes waiting for aggregation.
190 * This must not suppress a route unless it is known that among all
191 * routes with coarser masks that match it, the one with the longest
192 * mask is appropriate. This is ensured by scanning the routes
193 * in lexical order, and with the most restrictive mask first
194 * among routes to the same destination.
197 ag_flush(naddr lim_dst_h, /* flush routes to here */
198 naddr lim_mask, /* matching this mask */
199 void (*out)(struct ag_info *))
201 struct ag_info *ag, *ag_cors;
206 ag != NULL && ag->ag_mask >= lim_mask;
208 ag_cors = ag->ag_cors;
210 /* work on only the specified routes */
211 dst_h = ag->ag_dst_h;
212 if ((dst_h & lim_mask) != lim_dst_h)
215 if (!(ag->ag_state & AGS_SUPPRESS))
218 else for ( ; ; ag_cors = ag_cors->ag_cors) {
219 /* Look for a route that can suppress the
221 if (ag_cors == NULL) {
222 /* failed, so output it and look for
223 * another route to work on
229 if ((dst_h & ag_cors->ag_mask) == ag_cors->ag_dst_h) {
230 /* We found a route with a coarser mask that
231 * aggregates the current target.
233 * If it has a different next hop, it
234 * cannot replace the target, so output
237 if (ag->ag_gate != ag_cors->ag_gate
238 && !(ag->ag_state & AGS_FINE_GATE)
239 && !(ag_cors->ag_state & AGS_CORS_GATE)) {
244 /* If the coarse route has a good enough
245 * metric, it suppresses the target.
246 * If the suppressed target was redundant,
247 * then mark the suppressor redundant.
249 if (ag_cors->ag_pref <= ag->ag_pref) {
250 if (AG_IS_REDUN(ag->ag_state)
251 && ag_cors->ag_mask==ag->ag_mask<<1) {
252 if (ag_cors->ag_dst_h == dst_h)
253 ag_cors->ag_state |= AGS_REDUN0;
255 ag_cors->ag_state |= AGS_REDUN1;
257 if (ag->ag_tag != ag_cors->ag_tag)
259 if (ag->ag_nhop != ag_cors->ag_nhop)
260 ag_cors->ag_nhop = 0;
266 /* That route has either been output or suppressed */
267 ag_cors = ag->ag_cors;
275 /* Try to aggregate a route with previous routes.
287 void (*out)(struct ag_info *)) /* output using this */
289 struct ag_info *ag, *nag, *ag_cors;
295 /* Punt non-contiguous subnet masks.
297 * (X & -X) contains a single bit if and only if X is a power of 2.
298 * (X + (X & -X)) == 0 if and only if X is a power of 2.
300 if ((mask & -mask) + mask != 0) {
301 struct ag_info nc_ag;
303 nc_ag.ag_dst_h = dst;
304 nc_ag.ag_mask = mask;
305 nc_ag.ag_gate = gate;
306 nc_ag.ag_nhop = nhop;
307 nc_ag.ag_metric = metric;
308 nc_ag.ag_pref = pref;
310 nc_ag.ag_state = state;
311 nc_ag.ag_seqno = new_seqno;
316 /* Search for the right slot in the aggregation table.
321 if (ag->ag_mask >= mask)
324 /* Suppress old routes (i.e. combine with compatible routes
325 * with coarser masks) as we look for the right slot in the
326 * aggregation table for the new route.
327 * A route to an address less than the current destination
328 * will not be affected by the current route or any route
329 * seen hereafter. That means it is safe to suppress it.
330 * This check keeps poor routes (e.g. with large hop counts)
331 * from preventing suppression of finer routes.
334 && ag->ag_dst_h < dst
335 && (ag->ag_state & AGS_SUPPRESS)
336 && ag_cors->ag_pref <= ag->ag_pref
337 && (ag->ag_dst_h & ag_cors->ag_mask) == ag_cors->ag_dst_h
338 && (ag_cors->ag_gate == ag->ag_gate
339 || (ag->ag_state & AGS_FINE_GATE)
340 || (ag_cors->ag_state & AGS_CORS_GATE))) {
341 /* If the suppressed target was redundant,
342 * then mark the suppressor redundant.
344 if (AG_IS_REDUN(ag->ag_state)
345 && ag_cors->ag_mask == ag->ag_mask<<1) {
346 if (ag_cors->ag_dst_h == dst)
347 ag_cors->ag_state |= AGS_REDUN0;
349 ag_cors->ag_state |= AGS_REDUN1;
351 if (ag->ag_tag != ag_cors->ag_tag)
353 if (ag->ag_nhop != ag_cors->ag_nhop)
354 ag_cors->ag_nhop = 0;
360 ag = ag_cors->ag_fine;
363 /* If we find the even/odd twin of the new route, and if the
364 * masks and so forth are equal, we can aggregate them.
365 * We can probably promote one of the pair.
367 * Since the routes are encountered in lexical order,
368 * the new route must be odd. However, the second or later
369 * times around this loop, it could be the even twin promoted
370 * from the even/odd pair of twins of the finer route.
373 && ag->ag_mask == mask
374 && ((ag->ag_dst_h ^ dst) & (mask<<1)) == 0) {
376 /* Here we know the target route and the route in the current
377 * slot have the same netmasks and differ by at most the
378 * last bit. They are either for the same destination, or
379 * for an even/odd pair of destinations.
381 if (ag->ag_dst_h == dst) {
382 /* We have two routes to the same destination.
383 * Routes are encountered in lexical order, so a
384 * route is never promoted until the parent route is
385 * already present. So we know that the new route is
386 * a promoted (or aggregated) pair and the route
387 * already in the slot is the explicit route.
389 * Prefer the best route if their metrics differ,
390 * or the aggregated one if not, following a sort
391 * of longest-match rule.
393 if (pref <= ag->ag_pref) {
397 ag->ag_metric = metric;
399 if (ag->ag_seqno < new_seqno)
400 ag->ag_seqno = new_seqno;
402 ag->ag_state = state;
406 /* Some bits are set if they are set on either route,
407 * except when the route is for an interface.
409 if (!(ag->ag_state & AGS_IF))
410 ag->ag_state |= (state & (AGS_AGGREGATE_EITHER
416 /* If one of the routes can be promoted and the other can
417 * be suppressed, it may be possible to combine them or
418 * worthwhile to promote one.
420 * Any route that can be promoted is always
421 * marked to be eligible to be suppressed.
423 if (!((state & AGS_AGGREGATE)
424 && (ag->ag_state & AGS_SUPPRESS))
425 && !((ag->ag_state & AGS_AGGREGATE)
426 && (state & AGS_SUPPRESS)))
429 /* A pair of even/odd twin routes can be combined
430 * if either is redundant, or if they are via the
431 * same gateway and have the same metric.
433 if (AG_IS_REDUN(ag->ag_state)
434 || AG_IS_REDUN(state)
435 || (ag->ag_gate == gate
436 && ag->ag_pref == pref
437 && (state & ag->ag_state & AGS_AGGREGATE) != 0)) {
439 /* We have both the even and odd pairs.
440 * Since the routes are encountered in order,
441 * the route in the slot must be the even twin.
443 * Combine and promote (aggregate) the pair of routes.
445 if (new_seqno < ag->ag_seqno)
446 new_seqno = ag->ag_seqno;
447 if (!AG_IS_REDUN(state))
448 state &= ~AGS_REDUN1;
449 if (AG_IS_REDUN(ag->ag_state))
452 state &= ~AGS_REDUN0;
453 state |= (ag->ag_state & AGS_AGGREGATE_EITHER);
454 if (ag->ag_tag != tag)
456 if (ag->ag_nhop != nhop)
459 /* Get rid of the even twin that was already
464 } else if (ag->ag_pref >= pref
465 && (ag->ag_state & AGS_AGGREGATE)) {
466 /* If we cannot combine the pair, maybe the route
467 * with the worse metric can be promoted.
469 * Promote the old, even twin, by giving its slot
470 * in the table to the new, odd twin.
486 /* The promoted route is even-redundant only if the
487 * even twin was fully redundant. It is not
488 * odd-redundant because the odd-twin will still be
495 ag->ag_state = state;
499 ag->ag_metric = metric;
506 /* take the newest sequence number */
507 if (new_seqno <= ag->ag_seqno)
508 new_seqno = ag->ag_seqno;
510 ag->ag_seqno = new_seqno;
513 if (!(state & AGS_AGGREGATE))
514 break; /* cannot promote either twin */
516 /* Promote the new, odd twin by shaving its
518 * The promoted route is odd-redundant only if the
519 * odd twin was fully redundant. It is not
520 * even-redundant because the even twin is still in
523 if (!AG_IS_REDUN(state))
524 state &= ~AGS_REDUN1;
525 state &= ~AGS_REDUN0;
526 if (new_seqno < ag->ag_seqno)
527 new_seqno = ag->ag_seqno;
529 ag->ag_seqno = new_seqno;
535 if (ag_cors == NULL) {
540 ag_cors = ag->ag_cors;
543 /* When we can no longer promote and combine routes,
544 * flush the old route in the target slot. Also flush
545 * any finer routes that we know will never be aggregated by
548 * In case we moved toward coarser masks,
549 * get back where we belong
552 && ag->ag_mask < mask) {
557 /* Empty the target slot
559 if (ag != NULL && ag->ag_mask == mask) {
560 ag_flush(ag->ag_dst_h, ag->ag_mask, out);
561 ag = (ag_cors == NULL) ? ag_corsest : ag_cors->ag_fine;
565 (void)fflush(stderr);
566 if (ag == NULL && ag_cors != ag_finest)
568 if (ag_cors == NULL && ag != ag_corsest)
570 if (ag != NULL && ag->ag_cors != ag_cors)
572 if (ag_cors != NULL && ag_cors->ag_fine != ag)
577 /* Save the new route on the end of the table.
580 ag_avail = nag->ag_fine;
586 nag->ag_metric = metric;
589 nag->ag_state = state;
590 nag->ag_seqno = new_seqno;
597 nag->ag_cors = ag_cors;
601 ag_cors->ag_fine = nag;
606 rtm_type_name(u_char type)
608 static const char * const rtm_types[] = {
629 #define NEW_RTM_PAT "RTM type %#x"
630 static char name0[sizeof(NEW_RTM_PAT)+2];
633 if (type > sizeof(rtm_types)/sizeof(rtm_types[0])
635 snprintf(name0, sizeof(name0), NEW_RTM_PAT, type);
638 return rtm_types[type-1];
644 /* Trim a mask in a sockaddr
645 * Produce a length of 0 for an address of 0.
646 * Otherwise produce the index of the first zero byte.
650 masktrim(struct sockaddr_in *ap)
652 masktrim(struct sockaddr_in_new *ap)
657 if (ap->sin_addr.s_addr == 0) {
661 cp = (char *)(&ap->sin_addr.s_addr+1);
664 ap->sin_len = cp - (char*)ap + 1;
668 /* Tell the kernel to add, delete or change a route
671 rtioctl(int action, /* RTM_DELETE, etc */
679 struct rt_msghdr w_rtm;
680 struct sockaddr_in w_dst;
681 struct sockaddr_in w_gate;
683 struct sockaddr_in w_mask;
685 struct sockaddr_in_new w_mask;
689 # define PAT " %-10s %s metric=%d flags=%#x"
690 # define ARGS rtm_type_name(action), rtname(dst,mask,gate), metric, flags
693 memset(&w, 0, sizeof(w));
694 w.w_rtm.rtm_msglen = sizeof(w);
695 w.w_rtm.rtm_version = RTM_VERSION;
696 w.w_rtm.rtm_type = action;
697 w.w_rtm.rtm_flags = flags;
698 w.w_rtm.rtm_seq = ++rt_sock_seqno;
699 w.w_rtm.rtm_addrs = RTA_DST|RTA_GATEWAY;
700 if (metric != 0 || action == RTM_CHANGE) {
701 w.w_rtm.rtm_rmx.rmx_hopcount = metric;
702 w.w_rtm.rtm_inits |= RTV_HOPCOUNT;
704 w.w_dst.sin_family = AF_INET;
705 w.w_dst.sin_addr.s_addr = dst;
706 w.w_gate.sin_family = AF_INET;
707 w.w_gate.sin_addr.s_addr = gate;
709 w.w_dst.sin_len = sizeof(w.w_dst);
710 w.w_gate.sin_len = sizeof(w.w_gate);
712 if (mask == HOST_MASK) {
713 w.w_rtm.rtm_flags |= RTF_HOST;
714 w.w_rtm.rtm_msglen -= sizeof(w.w_mask);
716 w.w_rtm.rtm_addrs |= RTA_NETMASK;
717 w.w_mask.sin_addr.s_addr = htonl(mask);
720 if (w.w_mask.sin_len == 0)
721 w.w_mask.sin_len = sizeof(long);
722 w.w_rtm.rtm_msglen -= (sizeof(w.w_mask) - w.w_mask.sin_len);
727 cc = write(rt_sock, &w, w.w_rtm.rtm_msglen);
730 && (action == RTM_CHANGE || action == RTM_DELETE)) {
731 trace_act("route disappeared before" PAT, ARGS);
732 if (action == RTM_CHANGE) {
738 msglog("write(rt_sock)" PAT ": %s", ARGS, strerror(errno));
740 } else if (cc != w.w_rtm.rtm_msglen) {
741 msglog("write(rt_sock) wrote %ld instead of %d for" PAT,
742 cc, w.w_rtm.rtm_msglen, ARGS);
747 trace_misc("write kernel" PAT, ARGS);
753 #define KHASH_SIZE 71 /* should be prime */
754 #define KHASH(a,m) khash_bins[((a) ^ (m)) % KHASH_SIZE]
755 static struct khash {
756 struct khash *k_next;
763 #define KS_DELETE 0x002 /* need to delete the route */
764 #define KS_ADD 0x004 /* add to the kernel */
765 #define KS_CHANGE 0x008 /* tell kernel to change the route */
766 #define KS_DEL_ADD 0x010 /* delete & add to change the kernel */
767 #define KS_STATIC 0x020 /* Static flag in kernel */
768 #define KS_GATEWAY 0x040 /* G flag in kernel */
769 #define KS_DYNAMIC 0x080 /* result of redirect */
770 #define KS_DELETED 0x100 /* already deleted from kernel */
771 #define KS_CHECK 0x200
773 #define K_KEEP_LIM 30
774 time_t k_redirect_time; /* when redirected route 1st seen */
775 } *khash_bins[KHASH_SIZE];
779 kern_find(naddr dst, naddr mask, struct khash ***ppk)
781 struct khash *k, **pk;
783 for (pk = &KHASH(dst,mask); (k = *pk) != NULL; pk = &k->k_next) {
784 if (k->k_dst == dst && k->k_mask == mask)
794 kern_add(naddr dst, naddr mask)
796 struct khash *k, **pk;
798 k = kern_find(dst, mask, &pk);
802 k = (struct khash *)rtmalloc(sizeof(*k), "kern_add");
804 memset(k, 0, sizeof(*k));
808 k->k_keep = now.tv_sec;
815 /* If a kernel route has a non-zero metric, check that it is still in the
816 * daemon table, and not deleted by interfaces coming and going.
819 kern_check_static(struct khash *k,
820 struct interface *ifp)
825 if (k->k_metric == 0)
828 memset(&new, 0, sizeof(new));
830 new.rts_gate = k->k_gate;
831 new.rts_router = (ifp != NULL) ? ifp->int_addr : loopaddr;
832 new.rts_metric = k->k_metric;
833 new.rts_time = now.tv_sec;
835 rt = rtget(k->k_dst, k->k_mask);
837 if (!(rt->rt_state & RS_STATIC))
838 rtchange(rt, rt->rt_state | RS_STATIC, &new, 0);
840 rtadd(k->k_dst, k->k_mask, RS_STATIC, &new);
845 /* operate on a kernel entry
848 kern_ioctl(struct khash *k,
849 int action, /* RTM_DELETE, etc */
855 k->k_state &= ~KS_DYNAMIC;
856 if (k->k_state & KS_DELETED)
858 k->k_state |= KS_DELETED;
861 k->k_state &= ~KS_DELETED;
864 if (k->k_state & KS_DELETED) {
866 k->k_state &= ~KS_DELETED;
871 rtioctl(action, k->k_dst, k->k_gate, k->k_mask, k->k_metric, flags);
875 /* add a route the kernel told us
878 rtm_add(struct rt_msghdr *rtm,
879 struct rt_addrinfo *info,
883 struct interface *ifp;
887 if (rtm->rtm_flags & RTF_HOST) {
889 } else if (INFO_MASK(info) != 0) {
890 mask = ntohl(S_ADDR(INFO_MASK(info)));
892 msglog("ignore %s without mask", rtm_type_name(rtm->rtm_type));
896 k = kern_add(S_ADDR(INFO_DST(info)), mask);
897 if (k->k_state & KS_NEW)
898 k->k_keep = now.tv_sec+keep;
899 if (INFO_GATE(info) == 0) {
900 trace_act("note %s without gateway",
901 rtm_type_name(rtm->rtm_type));
902 k->k_metric = HOPCNT_INFINITY;
903 } else if (INFO_GATE(info)->sa_family != AF_INET) {
904 trace_act("note %s with gateway AF=%d",
905 rtm_type_name(rtm->rtm_type),
906 INFO_GATE(info)->sa_family);
907 k->k_metric = HOPCNT_INFINITY;
909 k->k_gate = S_ADDR(INFO_GATE(info));
910 k->k_metric = rtm->rtm_rmx.rmx_hopcount;
913 else if (k->k_metric > HOPCNT_INFINITY-1)
914 k->k_metric = HOPCNT_INFINITY-1;
916 k->k_state &= ~(KS_DELETE | KS_ADD | KS_CHANGE | KS_DEL_ADD
917 | KS_DELETED | KS_GATEWAY | KS_STATIC
918 | KS_NEW | KS_CHECK);
919 if (rtm->rtm_flags & RTF_GATEWAY)
920 k->k_state |= KS_GATEWAY;
921 if (rtm->rtm_flags & RTF_STATIC)
922 k->k_state |= KS_STATIC;
924 if (0 != (rtm->rtm_flags & (RTF_DYNAMIC | RTF_MODIFIED))) {
925 if (INFO_AUTHOR(info) != 0
926 && INFO_AUTHOR(info)->sa_family == AF_INET)
927 ifp = iflookup(S_ADDR(INFO_AUTHOR(info)));
931 && (ifp == NULL || !(ifp->int_state & IS_REDIRECT_OK))) {
932 /* Routers are not supposed to listen to redirects,
933 * so delete it if it came via an unknown interface
934 * or the interface does not have special permission.
936 k->k_state &= ~KS_DYNAMIC;
937 k->k_state |= KS_DELETE;
938 LIM_SEC(need_kern, 0);
939 trace_act("mark for deletion redirected %s --> %s"
941 addrname(k->k_dst, k->k_mask, 0),
942 naddr_ntoa(k->k_gate),
943 ifp ? ifp->int_name : "unknown interface");
945 k->k_state |= KS_DYNAMIC;
946 k->k_redirect_time = now.tv_sec;
947 trace_act("accept redirected %s --> %s via %s",
948 addrname(k->k_dst, k->k_mask, 0),
949 naddr_ntoa(k->k_gate),
950 ifp ? ifp->int_name : "unknown interface");
955 /* If it is not a static route, quit until the next comparison
956 * between the kernel and daemon tables, when it will be deleted.
958 if (!(k->k_state & KS_STATIC)) {
959 k->k_state |= KS_DELETE;
960 LIM_SEC(need_kern, k->k_keep);
964 /* Put static routes with real metrics into the daemon table so
965 * they can be advertised.
967 * Find the interface toward the gateway.
969 ifp = iflookup(k->k_gate);
971 msglog("static route %s --> %s impossibly lacks ifp",
972 addrname(S_ADDR(INFO_DST(info)), mask, 0),
973 naddr_ntoa(k->k_gate));
975 kern_check_static(k, ifp);
979 /* deal with packet loss
982 rtm_lose(struct rt_msghdr *rtm,
983 struct rt_addrinfo *info)
985 if (INFO_GATE(info) == 0
986 || INFO_GATE(info)->sa_family != AF_INET) {
987 trace_act("ignore %s without gateway",
988 rtm_type_name(rtm->rtm_type));
993 rdisc_age(S_ADDR(INFO_GATE(info)));
994 age(S_ADDR(INFO_GATE(info)));
998 /* Make the gateway slot of an info structure point to something
999 * useful. If it is not already useful, but it specifies an interface,
1000 * then fill in the sockaddr_in provided and point it there.
1003 get_info_gate(struct sockaddr **sap,
1004 struct sockaddr_in *rsin)
1006 struct sockaddr_dl *sdl = (struct sockaddr_dl *)*sap;
1007 struct interface *ifp;
1011 if ((sdl)->sdl_family == AF_INET)
1013 if ((sdl)->sdl_family != AF_LINK)
1016 ifp = ifwithindex(sdl->sdl_index, 1);
1020 rsin->sin_addr.s_addr = ifp->int_addr;
1022 rsin->sin_len = sizeof(*rsin);
1024 rsin->sin_family = AF_INET;
1025 *sap = (struct sockaddr*)rsin;
1031 /* Clean the kernel table by copying it to the daemon image.
1032 * Eventually the daemon will delete any extra routes.
1037 static char *sysctl_buf;
1038 static size_t sysctl_buf_size = 0;
1042 struct rt_msghdr *rtm;
1043 struct sockaddr_in gate_sin;
1044 struct rt_addrinfo info;
1049 for (i = 0; i < KHASH_SIZE; i++) {
1050 for (k = khash_bins[i]; k != NULL; k = k->k_next) {
1051 k->k_state |= KS_CHECK;
1057 mib[2] = 0; /* protocol */
1058 mib[3] = 0; /* wildcard address family */
1059 mib[4] = NET_RT_DUMP;
1060 mib[5] = 0; /* no flags */
1062 if ((needed = sysctl_buf_size) != 0) {
1063 if (sysctl(mib, 6, sysctl_buf,&needed, 0, 0) >= 0)
1065 if (errno != ENOMEM && errno != EFAULT)
1066 BADERR(1,"flush_kern: sysctl(RT_DUMP)");
1070 if (sysctl(mib, 6, 0, &needed, 0, 0) < 0)
1071 BADERR(1,"flush_kern: sysctl(RT_DUMP) estimate");
1072 /* Kludge around the habit of some systems, such as
1073 * BSD/OS 3.1, to not admit how many routes are in the
1074 * kernel, or at least to be quite wrong.
1076 needed += 50*(sizeof(*rtm)+5*sizeof(struct sockaddr));
1077 sysctl_buf = rtmalloc(sysctl_buf_size = needed,
1078 "flush_kern sysctl(RT_DUMP)");
1081 lim = sysctl_buf + needed;
1082 for (next = sysctl_buf; next < lim; next += rtm->rtm_msglen) {
1083 rtm = (struct rt_msghdr *)next;
1084 if (rtm->rtm_msglen == 0) {
1085 msglog("zero length kernel route at "
1086 " %#lx in buffer %#lx before %#lx",
1087 (u_long)rtm, (u_long)sysctl_buf, (u_long)lim);
1092 (struct sockaddr *)(rtm+1),
1093 (struct sockaddr *)(next + rtm->rtm_msglen),
1096 if (INFO_DST(&info) == 0
1097 || INFO_DST(&info)->sa_family != AF_INET)
1100 #if defined (RTF_LLINFO)
1101 /* ignore ARP table entries on systems with a merged route
1104 if (rtm->rtm_flags & RTF_LLINFO)
1107 #if defined(RTF_WASCLONED) && defined(__FreeBSD__)
1108 /* ignore cloned routes
1110 if (rtm->rtm_flags & RTF_WASCLONED)
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 if (m.r.rtm.rtm_type == RTM_OIFINFO)
1223 continue; /* ignore compat message */
1226 strlcpy(str, rtm_type_name(m.r.rtm.rtm_type), sizeof(str));
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);
1232 * Only messages that use the struct rt_msghdr format are
1233 * allowed beyond this point.
1235 if (m.r.rtm.rtm_type > RTM_RESOLVE) {
1236 trace_act("ignore %s", str);
1240 rt_xaddrs(&info, m.r.addrs, &m.r.addrs[RTAX_MAX],
1243 if (INFO_DST(&info) == 0) {
1244 trace_act("ignore %s without dst", str);
1248 if (INFO_DST(&info)->sa_family != AF_INET) {
1249 trace_act("ignore %s for AF %d", str,
1250 INFO_DST(&info)->sa_family);
1254 mask = ((INFO_MASK(&info) != 0)
1255 ? ntohl(S_ADDR(INFO_MASK(&info)))
1256 : (m.r.rtm.rtm_flags & RTF_HOST)
1258 : std_mask(S_ADDR(INFO_DST(&info))));
1260 strp += sprintf(strp, ": %s",
1261 addrname(S_ADDR(INFO_DST(&info)), mask, 0));
1263 if (IN_MULTICAST(ntohl(S_ADDR(INFO_DST(&info))))) {
1264 trace_act("ignore multicast %s", str);
1268 #if defined(RTF_LLINFO)
1269 if (m.r.rtm.rtm_flags & RTF_LLINFO) {
1270 trace_act("ignore ARP %s", str);
1275 #if defined(RTF_WASCLONED) && defined(__FreeBSD__)
1276 if (m.r.rtm.rtm_flags & RTF_WASCLONED) {
1277 trace_act("ignore cloned %s", str);
1282 if (get_info_gate(&INFO_GATE(&info), &gate_sin)) {
1283 gate = S_ADDR(INFO_GATE(&info));
1284 strp += sprintf(strp, " --> %s", naddr_ntoa(gate));
1289 if (INFO_AUTHOR(&info) != 0)
1290 strp += sprintf(strp, " by authority of %s",
1291 saddr_ntoa(INFO_AUTHOR(&info)));
1293 switch (m.r.rtm.rtm_type) {
1297 if (m.r.rtm.rtm_errno != 0) {
1298 trace_act("ignore %s with \"%s\" error",
1299 str, strerror(m.r.rtm.rtm_errno));
1301 trace_act("%s", str);
1302 rtm_add(&m.r.rtm,&info,0);
1307 if (m.r.rtm.rtm_errno != 0
1308 && m.r.rtm.rtm_errno != ESRCH) {
1309 trace_act("ignore %s with \"%s\" error",
1310 str, strerror(m.r.rtm.rtm_errno));
1312 trace_act("%s", str);
1313 del_static(S_ADDR(INFO_DST(&info)), mask,
1319 trace_act("%s", str);
1320 rtm_lose(&m.r.rtm,&info);
1324 trace_act("ignore %s", str);
1331 /* after aggregating, note routes that belong in the kernel
1334 kern_out(struct ag_info *ag)
1339 /* Do not install bad routes if they are not already present.
1340 * This includes routes that had RS_NET_SYN for interfaces that
1343 if (ag->ag_metric == HOPCNT_INFINITY) {
1344 k = kern_find(htonl(ag->ag_dst_h), ag->ag_mask, 0);
1348 k = kern_add(htonl(ag->ag_dst_h), ag->ag_mask);
1351 if (k->k_state & KS_NEW) {
1352 /* will need to add new entry to the kernel table */
1353 k->k_state = KS_ADD;
1354 if (ag->ag_state & AGS_GATEWAY)
1355 k->k_state |= KS_GATEWAY;
1356 k->k_gate = ag->ag_gate;
1357 k->k_metric = ag->ag_metric;
1361 if (k->k_state & KS_STATIC)
1364 /* modify existing kernel entry if necessary */
1365 if (k->k_gate != ag->ag_gate
1366 || k->k_metric != ag->ag_metric) {
1367 /* Must delete bad interface routes etc. to change them. */
1368 if (k->k_metric == HOPCNT_INFINITY)
1369 k->k_state |= KS_DEL_ADD;
1370 k->k_gate = ag->ag_gate;
1371 k->k_metric = ag->ag_metric;
1372 k->k_state |= KS_CHANGE;
1375 /* If the daemon thinks the route should exist, forget
1376 * about any redirections.
1377 * If the daemon thinks the route should exist, eventually
1378 * override manual intervention by the operator.
1380 if ((k->k_state & (KS_DYNAMIC | KS_DELETED)) != 0) {
1381 k->k_state &= ~KS_DYNAMIC;
1382 k->k_state |= (KS_ADD | KS_DEL_ADD);
1385 if ((k->k_state & KS_GATEWAY)
1386 && !(ag->ag_state & AGS_GATEWAY)) {
1387 k->k_state &= ~KS_GATEWAY;
1388 k->k_state |= (KS_ADD | KS_DEL_ADD);
1389 } else if (!(k->k_state & KS_GATEWAY)
1390 && (ag->ag_state & AGS_GATEWAY)) {
1391 k->k_state |= KS_GATEWAY;
1392 k->k_state |= (KS_ADD | KS_DEL_ADD);
1395 /* Deleting-and-adding is necessary to change aspects of a route.
1396 * Just delete instead of deleting and then adding a bad route.
1397 * Otherwise, we want to keep the route in the kernel.
1399 if (k->k_metric == HOPCNT_INFINITY
1400 && (k->k_state & KS_DEL_ADD))
1401 k->k_state |= KS_DELETE;
1403 k->k_state &= ~KS_DELETE;
1410 walk_kern(struct radix_node *rn,
1411 struct walkarg *argp UNUSED)
1413 #define RT ((struct rt_entry *)rn)
1418 /* Do not install synthetic routes */
1419 if (RT->rt_state & RS_NET_SYN)
1422 if (!(RT->rt_state & RS_IF)) {
1423 /* This is an ordinary route, not for an interface.
1426 /* aggregate, ordinary good routes without regard to
1430 ags |= (AGS_GATEWAY | AGS_SUPPRESS | AGS_AGGREGATE);
1432 /* Do not install host routes directly to hosts, to avoid
1433 * interfering with ARP entries in the kernel table.
1436 && ntohl(RT->rt_dst) == RT->rt_gate)
1440 /* This is an interface route.
1441 * Do not install routes for "external" remote interfaces.
1443 if (RT->rt_ifp != 0 && (RT->rt_ifp->int_state & IS_EXTERNAL))
1446 /* Interfaces should override received routes.
1449 ags |= (AGS_IF | AGS_CORS_GATE);
1451 /* If it is not an interface, or an alias for an interface,
1452 * it must be a "gateway."
1454 * If it is a "remote" interface, it is also a "gateway" to
1455 * the kernel if is not an alias.
1458 || (RT->rt_ifp->int_state & IS_REMOTE))
1459 ags |= (AGS_GATEWAY | AGS_SUPPRESS | AGS_AGGREGATE);
1462 /* If RIP is off and IRDP is on, let the route to the discovered
1463 * route suppress any RIP routes. Eventually the RIP routes
1464 * will time-out and be deleted. This reaches the steady-state
1467 if ((RT->rt_state & RS_RDISC) && rip_sock < 0)
1468 ags |= AGS_CORS_GATE;
1470 metric = RT->rt_metric;
1471 if (metric == HOPCNT_INFINITY) {
1472 /* if the route is dead, so try hard to aggregate. */
1473 pref = HOPCNT_INFINITY;
1474 ags |= (AGS_FINE_GATE | AGS_SUPPRESS);
1475 ags &= ~(AGS_IF | AGS_CORS_GATE);
1478 ag_check(RT->rt_dst, RT->rt_mask, RT->rt_gate, 0,
1479 metric,pref, 0, 0, ags, kern_out);
1485 /* Update the kernel table to match the daemon table.
1491 struct khash *k, **pk;
1494 need_kern = age_timer;
1496 /* Walk daemon table, updating the copy of the kernel table.
1498 (void)rn_walktree(rhead, walk_kern, 0);
1499 ag_flush(0,0,kern_out);
1501 for (i = 0; i < KHASH_SIZE; i++) {
1502 for (pk = &khash_bins[i]; (k = *pk) != NULL; ) {
1503 /* Do not touch static routes */
1504 if (k->k_state & KS_STATIC) {
1505 kern_check_static(k,0);
1510 /* check hold on routes deleted by the operator */
1511 if (k->k_keep > now.tv_sec) {
1512 /* ensure we check when the hold is over */
1513 LIM_SEC(need_kern, k->k_keep);
1514 /* mark for the next cycle */
1515 k->k_state |= KS_DELETE;
1520 if ((k->k_state & KS_DELETE)
1521 && !(k->k_state & KS_DYNAMIC)) {
1522 kern_ioctl(k, RTM_DELETE, 0);
1528 if (k->k_state & KS_DEL_ADD)
1529 kern_ioctl(k, RTM_DELETE, 0);
1531 if (k->k_state & KS_ADD) {
1532 kern_ioctl(k, RTM_ADD,
1533 ((0 != (k->k_state & (KS_GATEWAY
1535 ? RTF_GATEWAY : 0));
1536 } else if (k->k_state & KS_CHANGE) {
1537 kern_ioctl(k, RTM_CHANGE,
1538 ((0 != (k->k_state & (KS_GATEWAY
1540 ? RTF_GATEWAY : 0));
1542 k->k_state &= ~(KS_ADD|KS_CHANGE|KS_DEL_ADD);
1544 /* Mark this route to be deleted in the next cycle.
1545 * This deletes routes that disappear from the
1546 * daemon table, since the normal aging code
1547 * will clear the bit for routes that have not
1548 * disappeared from the daemon table.
1550 k->k_state |= KS_DELETE;
1557 /* Delete a static route in the image of the kernel table.
1560 del_static(naddr dst,
1566 struct rt_entry *rt;
1568 /* Just mark it in the table to be deleted next time the kernel
1570 * If it has already been deleted, mark it as such, and set its
1571 * keep-timer so that it will not be deleted again for a while.
1572 * This lets the operator delete a route added by the daemon
1573 * and add a replacement.
1575 k = kern_find(dst, mask, 0);
1576 if (k != NULL && (gate == 0 || k->k_gate == gate)) {
1577 k->k_state &= ~(KS_STATIC | KS_DYNAMIC | KS_CHECK);
1578 k->k_state |= KS_DELETE;
1580 k->k_state |= KS_DELETED;
1581 k->k_keep = now.tv_sec + K_KEEP_LIM;
1585 rt = rtget(dst, mask);
1586 if (rt != NULL && (rt->rt_state & RS_STATIC))
1591 /* Delete all routes generated from ICMP Redirects that use a given gateway,
1592 * as well as old redirected routes.
1595 del_redirects(naddr bad_gate,
1602 for (i = 0; i < KHASH_SIZE; i++) {
1603 for (k = khash_bins[i]; k != NULL; k = k->k_next) {
1604 if (!(k->k_state & KS_DYNAMIC)
1605 || (k->k_state & KS_STATIC))
1608 if (k->k_gate != bad_gate
1609 && k->k_redirect_time > old
1613 k->k_state |= KS_DELETE;
1614 k->k_state &= ~KS_DYNAMIC;
1615 need_kern.tv_sec = now.tv_sec;
1616 trace_act("mark redirected %s --> %s for deletion",
1617 addrname(k->k_dst, k->k_mask, 0),
1618 naddr_ntoa(k->k_gate));
1624 /* Start the daemon tables.
1626 extern int max_keylen;
1634 /* Initialize the radix trees */
1635 max_keylen = sizeof(struct sockaddr_in);
1637 rn_inithead(&rhead, 32);
1639 /* mark all of the slots in the table free */
1640 ag_avail = ag_slots;
1641 for (ag = ag_slots, i = 1; i < NUM_AG_SLOTS; i++) {
1648 #ifdef _HAVE_SIN_LEN
1649 static struct sockaddr_in dst_sock = {sizeof(dst_sock), AF_INET, 0, {0}, {0}};
1650 static struct sockaddr_in mask_sock = {sizeof(mask_sock), AF_INET, 0, {0}, {0}};
1652 static struct sockaddr_in_new dst_sock = {_SIN_ADDR_SIZE, AF_INET};
1653 static struct sockaddr_in_new mask_sock = {_SIN_ADDR_SIZE, AF_INET};
1658 set_need_flash(void)
1662 /* Do not send the flash update immediately. Wait a little
1663 * while to hear from other routers.
1665 no_flash.tv_sec = now.tv_sec + MIN_WAITTIME;
1670 /* Get a particular routing table entry
1673 rtget(naddr dst, naddr mask)
1675 struct rt_entry *rt;
1677 dst_sock.sin_addr.s_addr = dst;
1678 mask_sock.sin_addr.s_addr = htonl(mask);
1679 masktrim(&mask_sock);
1680 rt = (struct rt_entry *)rhead->rnh_lookup(&dst_sock,&mask_sock,rhead);
1682 || rt->rt_dst != dst
1683 || rt->rt_mask != mask)
1690 /* Find a route to dst as the kernel would.
1695 dst_sock.sin_addr.s_addr = dst;
1696 return (struct rt_entry *)rhead->rnh_matchaddr(&dst_sock, rhead);
1700 /* add a route to the table
1705 u_int state, /* rt_state for the entry */
1706 struct rt_spare *new)
1708 struct rt_entry *rt;
1711 struct rt_spare *rts;
1713 rt = (struct rt_entry *)rtmalloc(sizeof (*rt), "rtadd");
1714 memset(rt, 0, sizeof(*rt));
1715 for (rts = rt->rt_spares, i = NUM_SPARES; i != 0; i--, rts++)
1716 rts->rts_metric = HOPCNT_INFINITY;
1718 rt->rt_nodes->rn_key = (caddr_t)&rt->rt_dst_sock;
1720 rt->rt_dst_sock.sin_family = AF_INET;
1721 #ifdef _HAVE_SIN_LEN
1722 rt->rt_dst_sock.sin_len = dst_sock.sin_len;
1724 if (mask != HOST_MASK) {
1725 smask = std_mask(dst);
1726 if ((smask & ~mask) == 0 && mask > smask)
1729 mask_sock.sin_addr.s_addr = htonl(mask);
1730 masktrim(&mask_sock);
1732 rt->rt_state = state;
1733 rt->rt_spares[0] = *new;
1734 rt->rt_time = now.tv_sec;
1735 rt->rt_poison_metric = HOPCNT_INFINITY;
1736 rt->rt_seqno = update_seqno;
1738 if (++total_routes == MAX_ROUTES)
1739 msglog("have maximum (%d) routes", total_routes);
1741 trace_add_del("Add", rt);
1743 need_kern.tv_sec = now.tv_sec;
1746 if (0 == rhead->rnh_addaddr(&rt->rt_dst_sock, &mask_sock,
1747 rhead, rt->rt_nodes)) {
1748 msglog("rnh_addaddr() failed for %s mask=%#lx",
1749 naddr_ntoa(dst), (u_long)mask);
1755 /* notice a changed route
1758 rtchange(struct rt_entry *rt,
1759 u_int state, /* new state bits */
1760 struct rt_spare *new,
1763 if (rt->rt_metric != new->rts_metric) {
1764 /* Fix the kernel immediately if it seems the route
1765 * has gone bad, since there may be a working route that
1766 * aggregates this route.
1768 if (new->rts_metric == HOPCNT_INFINITY) {
1769 need_kern.tv_sec = now.tv_sec;
1770 if (new->rts_time >= now.tv_sec - EXPIRE_TIME)
1771 new->rts_time = now.tv_sec - EXPIRE_TIME;
1773 rt->rt_seqno = update_seqno;
1777 if (rt->rt_gate != new->rts_gate) {
1778 need_kern.tv_sec = now.tv_sec;
1779 rt->rt_seqno = update_seqno;
1783 state |= (rt->rt_state & RS_SUBNET);
1785 /* Keep various things from deciding ageless routes are stale.
1787 if (!AGE_RT(state, new->rts_ifp))
1788 new->rts_time = now.tv_sec;
1791 trace_change(rt, state, new,
1792 label ? label : "Chg ");
1794 rt->rt_state = state;
1795 rt->rt_spares[0] = *new;
1799 /* check for a better route among the spares
1801 static struct rt_spare *
1802 rts_better(struct rt_entry *rt)
1804 struct rt_spare *rts, *rts1;
1807 /* find the best alternative among the spares */
1808 rts = rt->rt_spares+1;
1809 for (i = NUM_SPARES, rts1 = rts+1; i > 2; i--, rts1++) {
1810 if (BETTER_LINK(rt,rts1,rts))
1818 /* switch to a backup route
1821 rtswitch(struct rt_entry *rt,
1822 struct rt_spare *rts)
1824 struct rt_spare swap;
1828 /* Do not change permanent routes */
1829 if (0 != (rt->rt_state & (RS_MHOME | RS_STATIC | RS_RDISC
1830 | RS_NET_SYN | RS_IF)))
1833 /* find the best alternative among the spares */
1835 rts = rts_better(rt);
1837 /* Do not bother if it is not worthwhile.
1839 if (!BETTER_LINK(rt, rts, rt->rt_spares))
1842 swap = rt->rt_spares[0];
1843 (void)sprintf(label, "Use #%d", (int)(rts - rt->rt_spares));
1844 rtchange(rt, rt->rt_state & ~(RS_NET_SYN | RS_RDISC), rts, label);
1845 if (swap.rts_metric == HOPCNT_INFINITY) {
1854 rtdelete(struct rt_entry *rt)
1860 trace_add_del("Del", rt);
1862 k = kern_find(rt->rt_dst, rt->rt_mask, 0);
1864 k->k_state |= KS_DELETE;
1865 need_kern.tv_sec = now.tv_sec;
1868 dst_sock.sin_addr.s_addr = rt->rt_dst;
1869 mask_sock.sin_addr.s_addr = htonl(rt->rt_mask);
1870 masktrim(&mask_sock);
1871 if (rt != (struct rt_entry *)rhead->rnh_deladdr(&dst_sock, &mask_sock,
1873 msglog("rnh_deladdr() failed");
1882 rts_delete(struct rt_entry *rt,
1883 struct rt_spare *rts)
1885 trace_upslot(rt, rts, &rts_empty);
1890 /* Get rid of a bad route, and try to switch to a replacement.
1893 rtbad(struct rt_entry *rt)
1895 struct rt_spare new;
1897 /* Poison the route */
1898 new = rt->rt_spares[0];
1899 new.rts_metric = HOPCNT_INFINITY;
1900 rtchange(rt, rt->rt_state & ~(RS_IF | RS_LOCAL | RS_STATIC), &new, 0);
1905 /* Junk a RS_NET_SYN or RS_LOCAL route,
1906 * unless it is needed by another interface.
1909 rtbad_sub(struct rt_entry *rt)
1911 struct interface *ifp, *ifp1;
1912 struct intnet *intnetp;
1919 if (rt->rt_state & RS_LOCAL) {
1920 /* Is this the route through loopback for the interface?
1921 * If so, see if it is used by any other interfaces, such
1922 * as a point-to-point interface with the same local address.
1924 LIST_FOREACH(ifp, &ifnet, int_list) {
1925 /* Retain it if another interface needs it.
1927 if (ifp->int_addr == rt->rt_ifp->int_addr) {
1936 if (!(state & RS_LOCAL)) {
1937 /* Retain RIPv1 logical network route if there is another
1938 * interface that justifies it.
1940 if (rt->rt_state & RS_NET_SYN) {
1941 LIST_FOREACH(ifp, &ifnet, int_list) {
1942 if ((ifp->int_state & IS_NEED_NET_SYN)
1943 && rt->rt_mask == ifp->int_std_mask
1944 && rt->rt_dst == ifp->int_std_addr) {
1945 state |= RS_NET_SYN;
1952 /* or if there is an authority route that needs it. */
1953 for (intnetp = intnets;
1955 intnetp = intnetp->intnet_next) {
1956 if (intnetp->intnet_addr == rt->rt_dst
1957 && intnetp->intnet_mask == rt->rt_mask) {
1958 state |= (RS_NET_SYN | RS_NET_INT);
1964 if (ifp1 != NULL || (state & RS_NET_SYN)) {
1965 struct rt_spare new = rt->rt_spares[0];
1967 rtchange(rt, ((rt->rt_state & ~(RS_NET_SYN|RS_LOCAL)) | state),
1975 /* Called while walking the table looking for sick interfaces
1976 * or after a time change.
1980 walk_bad(struct radix_node *rn,
1981 struct walkarg *argp UNUSED)
1983 #define RT ((struct rt_entry *)rn)
1984 struct rt_spare *rts;
1988 /* fix any spare routes through the interface
1990 rts = RT->rt_spares;
1991 for (i = NUM_SPARES; i != 1; i--) {
1993 if (rts->rts_metric < HOPCNT_INFINITY
1994 && (rts->rts_ifp == NULL
1995 || (rts->rts_ifp->int_state & IS_BROKE)))
1996 rts_delete(RT, rts);
1999 /* Deal with the main route
2001 /* finished if it has been handled before or if its interface is ok
2003 if (RT->rt_ifp == 0 || !(RT->rt_ifp->int_state & IS_BROKE))
2006 /* Bad routes for other than interfaces are easy.
2008 if (0 == (RT->rt_state & (RS_IF | RS_NET_SYN | RS_LOCAL))) {
2019 /* Check the age of an individual route.
2023 walk_age(struct radix_node *rn,
2024 struct walkarg *argp UNUSED)
2026 #define RT ((struct rt_entry *)rn)
2027 struct interface *ifp;
2028 struct rt_spare *rts;
2032 /* age all of the spare routes, including the primary route
2035 rts = RT->rt_spares;
2036 for (i = NUM_SPARES; i != 0; i--, rts++) {
2039 if (i == NUM_SPARES) {
2040 if (!AGE_RT(RT->rt_state, ifp)) {
2041 /* Keep various things from deciding ageless
2044 rts->rts_time = now.tv_sec;
2048 /* forget RIP routes after RIP has been turned off.
2056 /* age failing routes
2058 if (age_bad_gate == rts->rts_gate
2059 && rts->rts_time >= now_stale) {
2060 rts->rts_time -= SUPPLY_INTERVAL;
2063 /* trash the spare routes when they go bad */
2064 if (rts->rts_metric < HOPCNT_INFINITY
2065 && now_garbage > rts->rts_time
2067 rts_delete(RT, rts);
2071 /* finished if the active route is still fresh */
2072 if (now_stale <= RT->rt_time)
2075 /* try to switch to an alternative */
2078 /* Delete a dead route after it has been publicly mourned. */
2079 if (now_garbage > RT->rt_time) {
2084 /* Start poisoning a bad route before deleting it. */
2085 if (now.tv_sec - RT->rt_time > EXPIRE_TIME) {
2086 struct rt_spare new = RT->rt_spares[0];
2087 new.rts_metric = HOPCNT_INFINITY;
2088 rtchange(RT, RT->rt_state, &new, 0);
2094 /* Watch for dead routes and interfaces.
2099 struct interface *ifp;
2102 /* If not listening to RIP, there is no need to age the routes in
2105 age_timer.tv_sec = (now.tv_sec
2106 + ((rip_sock < 0) ? NEVER : SUPPLY_INTERVAL));
2108 /* Check for dead IS_REMOTE interfaces by timing their
2111 LIST_FOREACH(ifp, &ifnet, int_list) {
2112 if (!(ifp->int_state & IS_REMOTE))
2115 /* ignore unreachable remote interfaces */
2116 if (!check_remote(ifp))
2119 /* Restore remote interface that has become reachable
2121 if (ifp->int_state & IS_BROKE)
2122 if_ok(ifp, "remote ");
2124 if (ifp->int_act_time != NEVER
2125 && now.tv_sec - ifp->int_act_time > EXPIRE_TIME) {
2126 msglog("remote interface %s to %s timed out after"
2129 naddr_ntoa(ifp->int_dstaddr),
2130 (long)(now.tv_sec - ifp->int_act_time)/60,
2131 (long)(now.tv_sec - ifp->int_act_time)%60);
2135 /* If we have not heard from the other router
2138 if (now.tv_sec >= ifp->int_query_time) {
2139 ifp->int_query_time = NEVER;
2145 age_bad_gate = bad_gate;
2146 (void)rn_walktree(rhead, walk_age, 0);
2148 /* delete old redirected routes to keep the kernel table small
2149 * and prevent blackholes
2151 del_redirects(bad_gate, now.tv_sec-STALE_TIME);
2153 /* Update the kernel routing table. */
2156 /* poke reticent remote gateways */