1 /* 2 * Copyright (c) 1983, 1988, 1993 3 * The Regents of the University of California. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 3. All advertising materials mentioning features or use of this software 14 * must display the following acknowledgment: 15 * This product includes software developed by the University of 16 * California, Berkeley and its contributors. 17 * 4. Neither the name of the University nor the names of its contributors 18 * may be used to endorse or promote products derived from this software 19 * without specific prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31 * SUCH DAMAGE. 32 * 33 * $Id$ 34 */ 35 36 #include "defs.h" 37 #include "pathnames.h" 38 #ifdef sgi 39 #include "math.h" 40 #endif 41 #include <signal.h> 42 #include <fcntl.h> 43 #include <sys/file.h> 44 45 #if !defined(sgi) && !defined(__NetBSD__) 46 char copyright[] = 47 "@(#) Copyright (c) 1983, 1988, 1993\n\ 48 The Regents of the University of California. All rights reserved.\n"; 49 static char sccsid[] __attribute__((unused)) = "@(#)main.c 8.1 (Berkeley) 6/5/93"; 50 #elif defined(__NetBSD__) 51 __RCSID("$NetBSD$"); 52 __COPYRIGHT("@(#) Copyright (c) 1983, 1988, 1993\n\ 53 The Regents of the University of California. All rights reserved.\n"); 54 #endif 55 #ident "$Revision: 2.17 $" 56 57 58 pid_t mypid; 59 60 naddr myaddr; /* system address */ 61 char myname[MAXHOSTNAMELEN+1]; 62 63 int verbose; 64 65 int supplier; /* supply or broadcast updates */ 66 int supplier_set; 67 int ipforwarding = 1; /* kernel forwarding on */ 68 69 int default_gateway; /* 1=advertise default */ 70 int background = 1; 71 int ridhosts; /* 1=reduce host routes */ 72 int mhome; /* 1=want multi-homed host route */ 73 int advertise_mhome; /* 1=must continue advertising it */ 74 int auth_ok = 1; /* 1=ignore auth if we do not care */ 75 76 struct timeval epoch; /* when started */ 77 struct timeval clk, prev_clk; 78 static int usec_fudge; 79 struct timeval now; /* current idea of time */ 80 time_t now_stale; 81 time_t now_expire; 82 time_t now_garbage; 83 84 struct timeval next_bcast; /* next general broadcast */ 85 struct timeval no_flash = {EPOCH+SUPPLY_INTERVAL}; /* inhibit flash update */ 86 87 struct timeval flush_kern_timer; 88 89 fd_set fdbits; 90 int sock_max; 91 int rip_sock = -1; /* RIP socket */ 92 struct interface *rip_sock_mcast; /* current multicast interface */ 93 int rt_sock; /* routing socket */ 94 int rt_sock_seqno; 95 96 97 static int get_rip_sock(naddr, int); 98 static void timevalsub(struct timeval *, struct timeval *, struct timeval *); 99 100 int 101 main(int argc, 102 char *argv[]) 103 { 104 int n, mib[4], off; 105 size_t len; 106 char *p, *q; 107 const char *cp; 108 struct timeval wtime, t2; 109 time_t dt; 110 fd_set ibits; 111 naddr p_net, p_mask; 112 struct interface *ifp; 113 struct parm parm; 114 char *tracename = 0; 115 116 117 /* Some shells are badly broken and send SIGHUP to backgrounded 118 * processes. 119 */ 120 signal(SIGHUP, SIG_IGN); 121 122 openlog("routed", LOG_PID | LOG_ODELAY, LOG_DAEMON); 123 ftrace = stdout; 124 125 gettimeofday(&clk, 0); 126 prev_clk = clk; 127 epoch = clk; 128 epoch.tv_sec -= EPOCH; 129 now.tv_sec = EPOCH; 130 now_stale = EPOCH - STALE_TIME; 131 now_expire = EPOCH - EXPIRE_TIME; 132 now_garbage = EPOCH - GARBAGE_TIME; 133 wtime.tv_sec = 0; 134 135 (void)gethostname(myname, sizeof(myname)-1); 136 (void)gethost(myname, &myaddr); 137 138 while ((n = getopt(argc, argv, "sqdghmpAtvT:F:P:")) != -1) { 139 switch (n) { 140 case 's': 141 supplier = 1; 142 supplier_set = 1; 143 break; 144 145 case 'q': 146 supplier = 0; 147 supplier_set = 1; 148 break; 149 150 case 'd': 151 background = 0; 152 break; 153 154 case 'g': 155 memset(&parm, 0, sizeof(parm)); 156 parm.parm_d_metric = 1; 157 cp = check_parms(&parm); 158 if (cp != 0) 159 msglog("bad -g: %s", cp); 160 else 161 default_gateway = 1; 162 break; 163 164 case 'h': /* suppress extra host routes */ 165 ridhosts = 1; 166 break; 167 168 case 'm': /* advertise host route */ 169 mhome = 1; /* on multi-homed hosts */ 170 break; 171 172 case 'A': 173 /* Ignore authentication if we do not care. 174 * Crazy as it is, that is what RFC 1723 requires. 175 */ 176 auth_ok = 0; 177 break; 178 179 case 't': 180 new_tracelevel++; 181 break; 182 183 case 'T': 184 tracename = optarg; 185 break; 186 187 case 'F': /* minimal routes for SLIP */ 188 n = FAKE_METRIC; 189 p = strchr(optarg,','); 190 if (p && *p != '\0') { 191 n = (int)strtoul(p+1, &q, 0); 192 if (*q == '\0' 193 && n <= HOPCNT_INFINITY-1 194 && n >= 1) 195 *p = '\0'; 196 } 197 if (!getnet(optarg, &p_net, &p_mask)) { 198 msglog("bad network; \"-F %s\"", 199 optarg); 200 break; 201 } 202 memset(&parm, 0, sizeof(parm)); 203 parm.parm_net = p_net; 204 parm.parm_mask = p_mask; 205 parm.parm_d_metric = n; 206 cp = check_parms(&parm); 207 if (cp != 0) 208 msglog("bad -F: %s", cp); 209 break; 210 211 case 'P': 212 /* handle arbitrary parameters. 213 */ 214 q = strdup(optarg); 215 cp = parse_parms(q, 0); 216 if (cp != 0) 217 msglog("%s in \"-P %s\"", cp, optarg); 218 free(q); 219 break; 220 221 case 'v': 222 /* display version */ 223 verbose++; 224 msglog("version 2.17"); 225 break; 226 227 default: 228 goto usage; 229 } 230 } 231 argc -= optind; 232 argv += optind; 233 234 if (tracename == 0 && argc >= 1) { 235 tracename = *argv++; 236 argc--; 237 } 238 if (tracename != 0 && tracename[0] == '\0') 239 goto usage; 240 if (argc != 0) { 241 usage: 242 logbad(0, "usage: routed [-sqdghmpAtv] [-T tracefile]" 243 " [-F net[,metric]] [-P parms]"); 244 } 245 if (geteuid() != 0) { 246 if (verbose) 247 exit(0); 248 logbad(0, "requires UID 0"); 249 } 250 251 mib[0] = CTL_NET; 252 mib[1] = PF_INET; 253 mib[2] = IPPROTO_IP; 254 mib[3] = IPCTL_FORWARDING; 255 len = sizeof(ipforwarding); 256 if (sysctl(mib, 4, &ipforwarding, &len, 0, 0) < 0) 257 LOGERR("sysctl(IPCTL_FORWARDING)"); 258 259 if (!ipforwarding) { 260 if (supplier) 261 msglog("-s incompatible with ipforwarding=0"); 262 if (default_gateway) { 263 msglog("-g incompatible with ipforwarding=0"); 264 default_gateway = 0; 265 } 266 supplier = 0; 267 supplier_set = 1; 268 } 269 if (default_gateway) { 270 if (supplier_set && !supplier) { 271 msglog("-g and -q incompatible"); 272 } else { 273 supplier = 1; 274 supplier_set = 1; 275 } 276 } 277 278 279 signal(SIGALRM, sigalrm); 280 if (!background) 281 signal(SIGHUP, sigterm); /* SIGHUP fatal during debugging */ 282 signal(SIGTERM, sigterm); 283 signal(SIGINT, sigterm); 284 signal(SIGUSR1, sigtrace_on); 285 signal(SIGUSR2, sigtrace_off); 286 287 /* get into the background */ 288 #ifdef sgi 289 if (0 > _daemonize(background ? 0 : (_DF_NOCHDIR|_DF_NOFORK), 290 STDIN_FILENO, STDOUT_FILENO, STDERR_FILENO)) 291 BADERR(0, "_daemonize()"); 292 #else 293 if (background && daemon(0, 1) < 0) 294 BADERR(0,"daemon()"); 295 #endif 296 297 mypid = getpid(); 298 srandom((int)(clk.tv_sec ^ clk.tv_usec ^ mypid)); 299 300 /* prepare socket connected to the kernel. 301 */ 302 rt_sock = socket(AF_ROUTE, SOCK_RAW, 0); 303 if (rt_sock < 0) 304 BADERR(1,"rt_sock = socket()"); 305 if (fcntl(rt_sock, F_SETFL, O_NONBLOCK) == -1) 306 logbad(1, "fcntl(rt_sock) O_NONBLOCK: %s", strerror(errno)); 307 off = 0; 308 if (setsockopt(rt_sock, SOL_SOCKET,SO_USELOOPBACK, 309 &off,sizeof(off)) < 0) 310 LOGERR("setsockopt(SO_USELOOPBACK,0)"); 311 312 fix_select(); 313 314 315 if (tracename != 0) { 316 strncpy(inittracename, tracename, sizeof(inittracename)-1); 317 set_tracefile(inittracename, "%s", -1); 318 } else { 319 tracelevel_msg("%s", -1); /* turn on tracing to stdio */ 320 } 321 322 bufinit(); 323 324 /* initialize radix tree */ 325 rtinit(); 326 327 /* Pick a random part of the second for our output to minimize 328 * collisions. 329 * 330 * Start broadcasting after hearing from other routers, and 331 * at a random time so a bunch of systems do not get synchronized 332 * after a power failure. 333 */ 334 intvl_random(&next_bcast, EPOCH+MIN_WAITTIME, EPOCH+SUPPLY_INTERVAL); 335 age_timer.tv_usec = next_bcast.tv_usec; 336 age_timer.tv_sec = EPOCH+MIN_WAITTIME; 337 rdisc_timer = next_bcast; 338 ifinit_timer.tv_usec = next_bcast.tv_usec; 339 340 /* Collect an initial view of the world by checking the interface 341 * configuration and the kludge file. 342 */ 343 gwkludge(); 344 ifinit(); 345 346 /* Ask for routes */ 347 rip_query(); 348 rdisc_sol(); 349 350 /* Now turn off stdio if not tracing */ 351 if (new_tracelevel == 0) 352 trace_close(background); 353 354 /* Loop forever, listening and broadcasting. 355 */ 356 for (;;) { 357 prev_clk = clk; 358 gettimeofday(&clk, 0); 359 if (prev_clk.tv_sec == clk.tv_sec 360 && prev_clk.tv_usec == clk.tv_usec+usec_fudge) { 361 /* Much of `routed` depends on time always advancing. 362 * On systems that do not guarantee that gettimeofday() 363 * produces unique timestamps even if called within 364 * a single tick, use trickery like that in classic 365 * BSD kernels. 366 */ 367 clk.tv_usec += ++usec_fudge; 368 369 } else { 370 usec_fudge = 0; 371 372 timevalsub(&t2, &clk, &prev_clk); 373 if (t2.tv_sec < 0 374 || t2.tv_sec > wtime.tv_sec + 5) { 375 /* Deal with time changes before other 376 * housekeeping to keep everything straight. 377 */ 378 dt = t2.tv_sec; 379 if (dt > 0) 380 dt -= wtime.tv_sec; 381 trace_act("time changed by %d sec", (int)dt); 382 epoch.tv_sec += dt; 383 } 384 } 385 timevalsub(&now, &clk, &epoch); 386 now_stale = now.tv_sec - STALE_TIME; 387 now_expire = now.tv_sec - EXPIRE_TIME; 388 now_garbage = now.tv_sec - GARBAGE_TIME; 389 390 /* deal with signals that should affect tracing */ 391 set_tracelevel(); 392 393 if (stopint != 0) { 394 rip_bcast(0); 395 rdisc_adv(); 396 trace_off("exiting with signal %d", stopint); 397 exit(stopint | 128); 398 } 399 400 /* look for new or dead interfaces */ 401 timevalsub(&wtime, &ifinit_timer, &now); 402 if (wtime.tv_sec <= 0) { 403 wtime.tv_sec = 0; 404 ifinit(); 405 rip_query(); 406 continue; 407 } 408 409 /* Check the kernel table occassionally for mysteriously 410 * evaporated routes 411 */ 412 timevalsub(&t2, &flush_kern_timer, &now); 413 if (t2.tv_sec <= 0) { 414 flush_kern(); 415 flush_kern_timer.tv_sec = (now.tv_sec 416 + CHECK_QUIET_INTERVAL); 417 continue; 418 } 419 if (timercmp(&t2, &wtime, <)) 420 wtime = t2; 421 422 /* If it is time, then broadcast our routes. 423 */ 424 if (supplier || advertise_mhome) { 425 timevalsub(&t2, &next_bcast, &now); 426 if (t2.tv_sec <= 0) { 427 /* Synchronize the aging and broadcast 428 * timers to minimize awakenings 429 */ 430 age(0); 431 432 rip_bcast(0); 433 434 /* It is desirable to send routing updates 435 * regularly. So schedule the next update 436 * 30 seconds after the previous one was 437 * scheduled, instead of 30 seconds after 438 * the previous update was finished. 439 * Even if we just started after discovering 440 * a 2nd interface or were otherwise delayed, 441 * pick a 30-second aniversary of the 442 * original broadcast time. 443 */ 444 n = 1 + (0-t2.tv_sec)/SUPPLY_INTERVAL; 445 next_bcast.tv_sec += n*SUPPLY_INTERVAL; 446 447 continue; 448 } 449 450 if (timercmp(&t2, &wtime, <)) 451 wtime = t2; 452 } 453 454 /* If we need a flash update, either do it now or 455 * set the delay to end when it is time. 456 * 457 * If we are within MIN_WAITTIME seconds of a full update, 458 * do not bother. 459 */ 460 if (need_flash 461 && supplier 462 && no_flash.tv_sec+MIN_WAITTIME < next_bcast.tv_sec) { 463 /* accurate to the millisecond */ 464 if (!timercmp(&no_flash, &now, >)) 465 rip_bcast(1); 466 timevalsub(&t2, &no_flash, &now); 467 if (timercmp(&t2, &wtime, <)) 468 wtime = t2; 469 } 470 471 /* trigger the main aging timer. 472 */ 473 timevalsub(&t2, &age_timer, &now); 474 if (t2.tv_sec <= 0) { 475 age(0); 476 continue; 477 } 478 if (timercmp(&t2, &wtime, <)) 479 wtime = t2; 480 481 /* update the kernel routing table 482 */ 483 timevalsub(&t2, &need_kern, &now); 484 if (t2.tv_sec <= 0) { 485 age(0); 486 continue; 487 } 488 if (timercmp(&t2, &wtime, <)) 489 wtime = t2; 490 491 /* take care of router discovery, 492 * but do it in the correct the millisecond 493 */ 494 if (!timercmp(&rdisc_timer, &now, >)) { 495 rdisc_age(0); 496 continue; 497 } 498 timevalsub(&t2, &rdisc_timer, &now); 499 if (timercmp(&t2, &wtime, <)) 500 wtime = t2; 501 502 503 /* wait for input or a timer to expire. 504 */ 505 trace_flush(); 506 ibits = fdbits; 507 n = select(sock_max, &ibits, 0, 0, &wtime); 508 if (n <= 0) { 509 if (n < 0 && errno != EINTR && errno != EAGAIN) 510 BADERR(1,"select"); 511 continue; 512 } 513 514 if (FD_ISSET(rt_sock, &ibits)) { 515 read_rt(); 516 n--; 517 } 518 if (rdisc_sock >= 0 && FD_ISSET(rdisc_sock, &ibits)) { 519 read_d(); 520 n--; 521 } 522 if (rip_sock >= 0 && FD_ISSET(rip_sock, &ibits)) { 523 read_rip(rip_sock, 0); 524 n--; 525 } 526 527 for (ifp = ifnet; n > 0 && 0 != ifp; ifp = ifp->int_next) { 528 if (ifp->int_rip_sock >= 0 529 && FD_ISSET(ifp->int_rip_sock, &ibits)) { 530 read_rip(ifp->int_rip_sock, ifp); 531 n--; 532 } 533 } 534 } 535 } 536 537 538 /* ARGSUSED */ 539 void 540 sigalrm(int s UNUSED) 541 { 542 /* Historically, SIGALRM would cause the daemon to check for 543 * new and broken interfaces. 544 */ 545 ifinit_timer.tv_sec = now.tv_sec; 546 trace_act("SIGALRM"); 547 } 548 549 550 /* watch for fatal signals */ 551 void 552 sigterm(int sig) 553 { 554 stopint = sig; 555 (void)signal(sig, SIG_DFL); /* catch it only once */ 556 } 557 558 559 void 560 fix_select(void) 561 { 562 struct interface *ifp; 563 564 565 FD_ZERO(&fdbits); 566 sock_max = 0; 567 568 FD_SET(rt_sock, &fdbits); 569 if (sock_max <= rt_sock) 570 sock_max = rt_sock+1; 571 if (rip_sock >= 0) { 572 FD_SET(rip_sock, &fdbits); 573 if (sock_max <= rip_sock) 574 sock_max = rip_sock+1; 575 } 576 for (ifp = ifnet; 0 != ifp; ifp = ifp->int_next) { 577 if (ifp->int_rip_sock >= 0) { 578 FD_SET(ifp->int_rip_sock, &fdbits); 579 if (sock_max <= ifp->int_rip_sock) 580 sock_max = ifp->int_rip_sock+1; 581 } 582 } 583 if (rdisc_sock >= 0) { 584 FD_SET(rdisc_sock, &fdbits); 585 if (sock_max <= rdisc_sock) 586 sock_max = rdisc_sock+1; 587 } 588 } 589 590 591 void 592 fix_sock(int sock, 593 const char *name) 594 { 595 int on; 596 #define MIN_SOCKBUF (4*1024) 597 static int rbuf; 598 599 if (fcntl(sock, F_SETFL, O_NONBLOCK) == -1) 600 logbad(1, "fcntl(%s) O_NONBLOCK: %s", 601 name, strerror(errno)); 602 on = 1; 603 if (setsockopt(sock, SOL_SOCKET,SO_BROADCAST, &on,sizeof(on)) < 0) 604 msglog("setsockopt(%s,SO_BROADCAST): %s", 605 name, strerror(errno)); 606 #ifdef USE_PASSIFNAME 607 on = 1; 608 if (setsockopt(sock, SOL_SOCKET, SO_PASSIFNAME, &on,sizeof(on)) < 0) 609 msglog("setsockopt(%s,SO_PASSIFNAME): %s", 610 name, strerror(errno)); 611 #endif 612 613 if (rbuf >= MIN_SOCKBUF) { 614 if (setsockopt(sock, SOL_SOCKET, SO_RCVBUF, 615 &rbuf, sizeof(rbuf)) < 0) 616 msglog("setsockopt(%s,SO_RCVBUF=%d): %s", 617 name, rbuf, strerror(errno)); 618 } else { 619 for (rbuf = 60*1024; ; rbuf -= 4096) { 620 if (setsockopt(sock, SOL_SOCKET, SO_RCVBUF, 621 &rbuf, sizeof(rbuf)) == 0) { 622 trace_act("RCVBUF=%d", rbuf); 623 break; 624 } 625 if (rbuf < MIN_SOCKBUF) { 626 msglog("setsockopt(%s,SO_RCVBUF = %d): %s", 627 name, rbuf, strerror(errno)); 628 break; 629 } 630 } 631 } 632 } 633 634 635 /* get a rip socket 636 */ 637 static int /* <0 or file descriptor */ 638 get_rip_sock(naddr addr, 639 int serious) /* 1=failure to bind is serious */ 640 { 641 struct sockaddr_in sin; 642 unsigned char ttl; 643 int s; 644 645 646 if ((s = socket(AF_INET, SOCK_DGRAM, 0)) < 0) 647 BADERR(1,"rip_sock = socket()"); 648 649 memset(&sin, 0, sizeof(sin)); 650 #ifdef _HAVE_SIN_LEN 651 sin.sin_len = sizeof(sin); 652 #endif 653 sin.sin_family = AF_INET; 654 sin.sin_port = htons(RIP_PORT); 655 sin.sin_addr.s_addr = addr; 656 if (bind(s, (struct sockaddr *)&sin, sizeof(sin)) < 0) { 657 if (serious) 658 BADERR(errno != EADDRINUSE, "bind(rip_sock)"); 659 return -1; 660 } 661 fix_sock(s,"rip_sock"); 662 663 ttl = 1; 664 if (setsockopt(s, IPPROTO_IP, IP_MULTICAST_TTL, 665 &ttl, sizeof(ttl)) < 0) 666 DBGERR(1,"rip_sock setsockopt(IP_MULTICAST_TTL)"); 667 668 return s; 669 } 670 671 672 /* turn off main RIP socket */ 673 void 674 rip_off(void) 675 { 676 struct interface *ifp; 677 naddr addr; 678 679 680 if (rip_sock >= 0 && !mhome) { 681 trace_act("turn off RIP"); 682 683 (void)close(rip_sock); 684 rip_sock = -1; 685 686 /* get non-broadcast sockets to listen to queries. 687 */ 688 for (ifp = ifnet; ifp != 0; ifp = ifp->int_next) { 689 if (ifp->int_state & IS_REMOTE) 690 continue; 691 if (ifp->int_rip_sock < 0) { 692 addr = ((ifp->int_if_flags & IFF_POINTOPOINT) 693 ? ifp->int_dstaddr 694 : ifp->int_addr); 695 ifp->int_rip_sock = get_rip_sock(addr, 0); 696 } 697 } 698 699 fix_select(); 700 701 age(0); 702 } 703 } 704 705 706 /* turn on RIP multicast input via an interface 707 */ 708 static void 709 rip_mcast_on(struct interface *ifp) 710 { 711 struct ip_mreq m; 712 713 if (!IS_RIP_IN_OFF(ifp->int_state) 714 && (ifp->int_if_flags & IFF_MULTICAST) 715 #ifdef MCAST_PPP_BUG 716 && !(ifp->int_if_flags & IFF_POINTOPOINT) 717 #endif 718 && !(ifp->int_state & IS_ALIAS)) { 719 m.imr_multiaddr.s_addr = htonl(INADDR_RIP_GROUP); 720 m.imr_interface.s_addr = ((ifp->int_if_flags & IFF_POINTOPOINT) 721 ? ifp->int_dstaddr 722 : ifp->int_addr); 723 if (setsockopt(rip_sock,IPPROTO_IP, IP_ADD_MEMBERSHIP, 724 &m, sizeof(m)) < 0) 725 LOGERR("setsockopt(IP_ADD_MEMBERSHIP RIP)"); 726 } 727 } 728 729 730 /* Prepare socket used for RIP. 731 */ 732 void 733 rip_on(struct interface *ifp) 734 { 735 /* If the main RIP socket is already alive, only start receiving 736 * multicasts for this interface. 737 */ 738 if (rip_sock >= 0) { 739 if (ifp != 0) 740 rip_mcast_on(ifp); 741 return; 742 } 743 744 /* If the main RIP socket is off and it makes sense to turn it on, 745 * then turn it on for all of the interfaces. 746 * It makes sense if either router discovery is off, or if 747 * router discover is on and at most one interface is doing RIP. 748 */ 749 if (rip_interfaces > 0 && (!rdisc_ok || rip_interfaces > 1)) { 750 trace_act("turn on RIP"); 751 752 /* Close all of the query sockets so that we can open 753 * the main socket. SO_REUSEPORT is not a solution, 754 * since that would let two daemons bind to the broadcast 755 * socket. 756 */ 757 for (ifp = ifnet; ifp != 0; ifp = ifp->int_next) { 758 if (ifp->int_rip_sock >= 0) { 759 (void)close(ifp->int_rip_sock); 760 ifp->int_rip_sock = -1; 761 } 762 } 763 764 rip_sock = get_rip_sock(INADDR_ANY, 1); 765 rip_sock_mcast = 0; 766 767 /* Do not advertise anything until we have heard something 768 */ 769 if (next_bcast.tv_sec < now.tv_sec+MIN_WAITTIME) 770 next_bcast.tv_sec = now.tv_sec+MIN_WAITTIME; 771 772 for (ifp = ifnet; ifp != 0; ifp = ifp->int_next) { 773 ifp->int_query_time = NEVER; 774 rip_mcast_on(ifp); 775 } 776 ifinit_timer.tv_sec = now.tv_sec; 777 778 } else if (ifp != 0 779 && !(ifp->int_state & IS_REMOTE) 780 && ifp->int_rip_sock < 0) { 781 /* RIP is off, so ensure there are sockets on which 782 * to listen for queries. 783 */ 784 ifp->int_rip_sock = get_rip_sock(ifp->int_addr, 0); 785 } 786 787 fix_select(); 788 } 789 790 791 /* die if malloc(3) fails 792 */ 793 void * 794 rtmalloc(size_t size, 795 const char *msg) 796 { 797 void *p = malloc(size); 798 if (p == 0) 799 logbad(1,"malloc(%lu) failed in %s", (u_long)size, msg); 800 return p; 801 } 802 803 804 /* get a random instant in an interval 805 */ 806 void 807 intvl_random(struct timeval *tp, /* put value here */ 808 u_long lo, /* value is after this second */ 809 u_long hi) /* and before this */ 810 { 811 tp->tv_sec = (time_t)(hi == lo 812 ? lo 813 : (lo + random() % ((hi - lo)))); 814 tp->tv_usec = random() % 1000000; 815 } 816 817 818 void 819 timevaladd(struct timeval *t1, 820 struct timeval *t2) 821 { 822 823 t1->tv_sec += t2->tv_sec; 824 if ((t1->tv_usec += t2->tv_usec) >= 1000000) { 825 t1->tv_sec++; 826 t1->tv_usec -= 1000000; 827 } 828 } 829 830 831 /* t1 = t2 - t3 832 */ 833 static void 834 timevalsub(struct timeval *t1, 835 struct timeval *t2, 836 struct timeval *t3) 837 { 838 t1->tv_sec = t2->tv_sec - t3->tv_sec; 839 if ((t1->tv_usec = t2->tv_usec - t3->tv_usec) < 0) { 840 t1->tv_sec--; 841 t1->tv_usec += 1000000; 842 } 843 } 844 845 846 /* put a message into the system log 847 */ 848 void 849 msglog(const char *p, ...) 850 { 851 va_list args; 852 853 trace_flush(); 854 855 va_start(args, p); 856 vsyslog(LOG_ERR, p, args); 857 858 if (ftrace != 0) { 859 if (ftrace == stdout) 860 (void)fputs("routed: ", ftrace); 861 (void)vfprintf(ftrace, p, args); 862 (void)fputc('\n', ftrace); 863 } 864 } 865 866 867 /* Put a message about a bad system into the system log if 868 * we have not complained about it recently. 869 * 870 * It is desirable to complain about all bad systems, but not too often. 871 * In the worst case, it is not practical to keep track of all bad systems. 872 * For example, there can be many systems with the wrong password. 873 */ 874 void 875 msglim(struct msg_limit *lim, naddr addr, const char *p, ...) 876 { 877 va_list args; 878 int i; 879 struct msg_sub *ms1, *ms; 880 const char *p1; 881 882 va_start(args, p); 883 884 /* look for the oldest slot in the table 885 * or the slot for the bad router. 886 */ 887 ms = ms1 = lim->subs; 888 for (i = MSG_SUBJECT_N; ; i--, ms1++) { 889 if (i == 0) { 890 /* Reuse a slot at most once every 10 minutes. 891 */ 892 if (lim->reuse > now.tv_sec) { 893 ms = 0; 894 } else { 895 ms = ms1; 896 lim->reuse = now.tv_sec + 10*60; 897 } 898 break; 899 } 900 if (ms->addr == addr) { 901 /* Repeat a complaint about a given system at 902 * most once an hour. 903 */ 904 if (ms->until > now.tv_sec) 905 ms = 0; 906 break; 907 } 908 if (ms->until < ms1->until) 909 ms = ms1; 910 } 911 if (ms != 0) { 912 ms->addr = addr; 913 ms->until = now.tv_sec + 60*60; /* 60 minutes */ 914 915 trace_flush(); 916 for (p1 = p; *p1 == ' '; p1++) 917 continue; 918 vsyslog(LOG_ERR, p1, args); 919 } 920 921 /* always display the message if tracing */ 922 if (ftrace != 0) { 923 (void)vfprintf(ftrace, p, args); 924 (void)fputc('\n', ftrace); 925 } 926 } 927 928 929 void 930 logbad(int dump, const char *p, ...) 931 { 932 va_list args; 933 934 trace_flush(); 935 936 va_start(args, p); 937 vsyslog(LOG_ERR, p, args); 938 939 (void)fputs("routed: ", stderr); 940 (void)vfprintf(stderr, p, args); 941 (void)fputs("; giving up\n",stderr); 942 (void)fflush(stderr); 943 944 if (dump) 945 abort(); 946 exit(1); 947 } 948