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