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