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