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