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