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