1 /* $NetBSD: main.c,v 1.6 1995/12/10 10:07:05 mycroft Exp $ */ 2 3 /* 4 * The mrouted program is covered by the license in the accompanying file 5 * named "LICENSE". Use of the mrouted program represents acceptance of 6 * the terms and conditions listed in that file. 7 * 8 * The mrouted program is COPYRIGHT 1989 by The Board of Trustees of 9 * Leland Stanford Junior University. 10 */ 11 12 /* 13 * Written by Steve Deering, Stanford University, February 1989. 14 * 15 * (An earlier version of DVMRP was implemented by David Waitzman of 16 * BBN STC by extending Berkeley's routed program. Some of Waitzman's 17 * extensions have been incorporated into mrouted, but none of the 18 * original routed code has been adopted.) 19 */ 20 21 22 #include "defs.h" 23 #ifdef __STDC__ 24 #include <stdarg.h> 25 #else 26 #include <varargs.h> 27 #endif 28 #include <fcntl.h> 29 #include <util.h> 30 31 #ifdef SNMP 32 #include "snmp.h" 33 #endif 34 35 #ifndef lint 36 static char rcsid[] = 37 "@(#) $Id: main.c,v 1.7 2001/12/01 23:27:23 miod Exp $"; 38 #endif 39 40 extern char *configfilename; 41 char versionstring[100]; 42 43 static char dumpfilename[] = _PATH_MROUTED_DUMP; 44 static char cachefilename[] = _PATH_MROUTED_CACHE; 45 static char genidfilename[] = _PATH_MROUTED_GENID; 46 47 int cache_lifetime = DEFAULT_CACHE_LIFETIME; 48 int max_prune_lifetime = DEFAULT_CACHE_LIFETIME * 2; 49 50 int debug = 0; 51 u_char pruning = 1; /* Enable pruning by default */ 52 53 #ifdef SNMP 54 #define NHANDLERS 34 55 #else 56 #define NHANDLERS 2 57 #endif 58 59 static struct ihandler { 60 int fd; /* File descriptor */ 61 ihfunc_t func; /* Function to call with &fd_set */ 62 } ihandlers[NHANDLERS]; 63 static int nhandlers = 0; 64 65 /* 66 * Forward declarations. 67 */ 68 static void fasttimer __P((int)); 69 static void done __P((int)); 70 static void dump __P((int)); 71 static void fdump __P((int)); 72 static void cdump __P((int)); 73 static void restart __P((int)); 74 static void timer __P((void)); 75 static void cleanup __P((void)); 76 static void resetlogging __P((void *)); 77 78 /* To shut up gcc -Wstrict-prototypes */ 79 int main __P((int argc, char **argv)); 80 81 int 82 register_input_handler(fd, func) 83 int fd; 84 ihfunc_t func; 85 { 86 if (nhandlers >= NHANDLERS) 87 return -1; 88 89 ihandlers[nhandlers].fd = fd; 90 ihandlers[nhandlers++].func = func; 91 92 return 0; 93 } 94 95 int 96 main(argc, argv) 97 int argc; 98 char *argv[]; 99 { 100 register int recvlen; 101 int dummy; 102 FILE *fp; 103 struct timeval tv; 104 u_int32_t prev_genid; 105 int vers; 106 fd_set rfds, readers; 107 int nfds, n, i; 108 sigset_t mask, omask; 109 #ifdef SNMP 110 struct timeval timeout, *tvp = &timeout; 111 struct timeval sched, *svp = &sched, now, *nvp = &now; 112 int index, block; 113 #endif 114 115 if (geteuid() != 0) { 116 fprintf(stderr, "must be root\n"); 117 exit(1); 118 } 119 setlinebuf(stderr); 120 121 argv++, argc--; 122 while (argc > 0 && *argv[0] == '-') { 123 if (strcmp(*argv, "-d") == 0) { 124 if (argc > 1 && isdigit(*(argv + 1)[0])) { 125 argv++, argc--; 126 debug = atoi(*argv); 127 } else 128 debug = DEFAULT_DEBUG; 129 } else if (strcmp(*argv, "-c") == 0) { 130 if (argc > 1) { 131 argv++, argc--; 132 configfilename = *argv; 133 } else 134 goto usage; 135 } else if (strcmp(*argv, "-p") == 0) { 136 pruning = 0; 137 #ifdef SNMP 138 } else if (strcmp(*argv, "-P") == 0) { 139 if (argc > 1 && isdigit(*(argv + 1)[0])) { 140 argv++, argc--; 141 dest_port = atoi(*argv); 142 } else 143 dest_port = DEFAULT_PORT; 144 #endif 145 } else 146 goto usage; 147 argv++, argc--; 148 } 149 150 if (argc > 0) { 151 usage: fprintf(stderr, 152 "usage: mrouted [-p] [-c configfile] [-d [debug_level]]\n"); 153 exit(1); 154 } 155 156 if (debug == 0) { 157 /* 158 * Detach from the terminal 159 */ 160 int t; 161 162 if (fork()) exit(0); 163 (void)close(0); 164 (void)close(1); 165 (void)close(2); 166 (void)open("/", 0); 167 (void)dup2(0, 1); 168 (void)dup2(0, 2); 169 #ifdef SYSV 170 (void)setpgrp(); 171 #else 172 #ifdef TIOCNOTTY 173 t = open("/dev/tty", 2); 174 if (t >= 0) { 175 (void)ioctl(t, TIOCNOTTY, (char *)0); 176 (void)close(t); 177 } 178 #else 179 if (setsid() < 0) 180 perror("setsid"); 181 #endif 182 #endif 183 } 184 else 185 fprintf(stderr, "debug level %u\n", debug); 186 187 #ifdef LOG_DAEMON 188 (void)openlog("mrouted", LOG_PID, LOG_DAEMON); 189 (void)setlogmask(LOG_UPTO(LOG_NOTICE)); 190 #else 191 (void)openlog("mrouted", LOG_PID); 192 #endif 193 sprintf(versionstring, "mrouted version %d.%d", 194 PROTOCOL_VERSION, MROUTED_VERSION); 195 196 log(LOG_NOTICE, 0, "%s", versionstring); 197 198 #ifdef SYSV 199 srand48(time(NULL)); 200 #else 201 srandom(gethostid()); 202 #endif 203 204 /* 205 * Get generation id 206 */ 207 gettimeofday(&tv, 0); 208 dvmrp_genid = tv.tv_sec; 209 210 fp = fopen(genidfilename, "r"); 211 if (fp != NULL) { 212 fscanf(fp, "%d", &prev_genid); 213 if (prev_genid == dvmrp_genid) 214 dvmrp_genid++; 215 (void) fclose(fp); 216 } 217 218 fp = fopen(genidfilename, "w"); 219 if (fp != NULL) { 220 fprintf(fp, "%d", dvmrp_genid); 221 (void) fclose(fp); 222 } 223 224 callout_init(); 225 init_igmp(); 226 init_routes(); 227 init_ktable(); 228 k_init_dvmrp(); /* enable DVMRP routing in kernel */ 229 230 #ifndef OLD_KERNEL 231 vers = k_get_version(); 232 /*XXX 233 * This function must change whenever the kernel version changes 234 */ 235 if ((((vers >> 8) & 0xff) != 3) || 236 ((vers & 0xff) != 5)) 237 log(LOG_ERR, 0, "kernel (v%d.%d)/mrouted (v%d.%d) version mismatch", 238 (vers >> 8) & 0xff, vers & 0xff, 239 PROTOCOL_VERSION, MROUTED_VERSION); 240 #endif 241 242 #ifdef SNMP 243 if (i = snmp_init()) 244 return i; 245 246 gettimeofday(nvp, 0); 247 if (nvp->tv_usec < 500000L){ 248 svp->tv_usec = nvp->tv_usec + 500000L; 249 svp->tv_sec = nvp->tv_sec; 250 } else { 251 svp->tv_usec = nvp->tv_usec - 500000L; 252 svp->tv_sec = nvp->tv_sec + 1; 253 } 254 #endif /* SNMP */ 255 256 init_vifs(); 257 258 #ifdef RSRR 259 rsrr_init(); 260 #endif /* RSRR */ 261 262 #if defined(__STDC__) || defined(__GNUC__) 263 /* 264 * Allow cleanup if unexpected exit. Apparently some architectures 265 * have a kernel bug where closing the socket doesn't do an 266 * ip_mrouter_done(), so we attempt to do it on exit. 267 */ 268 atexit(cleanup); 269 #endif 270 271 if (debug) 272 fprintf(stderr, "pruning %s\n", pruning ? "on" : "off"); 273 274 pidfile(NULL); 275 276 (void)signal(SIGALRM, fasttimer); 277 278 (void)signal(SIGHUP, restart); 279 (void)signal(SIGTERM, done); 280 (void)signal(SIGINT, done); 281 (void)signal(SIGUSR1, fdump); 282 (void)signal(SIGUSR2, cdump); 283 if (debug != 0) 284 (void)signal(SIGQUIT, dump); 285 286 FD_ZERO(&readers); 287 FD_SET(igmp_socket, &readers); 288 nfds = igmp_socket + 1; 289 for (i = 0; i < nhandlers; i++) { 290 FD_SET(ihandlers[i].fd, &readers); 291 if (ihandlers[i].fd >= nfds) 292 nfds = ihandlers[i].fd + 1; 293 } 294 295 /* 296 * Install the vifs in the kernel as late as possible in the 297 * initialization sequence. 298 */ 299 init_installvifs(); 300 301 if (debug >= 2) dump(0); 302 303 /* Start up the log rate-limiter */ 304 resetlogging(NULL); 305 306 (void)alarm(1); /* schedule first timer interrupt */ 307 308 /* 309 * Main receive loop. 310 */ 311 dummy = 0; 312 for(;;) { 313 bcopy((char *)&readers, (char *)&rfds, sizeof(rfds)); 314 #ifdef SNMP 315 gettimeofday(nvp, 0); 316 if (nvp->tv_sec > svp->tv_sec 317 || (nvp->tv_sec == svp->tv_sec && nvp->tv_usec > svp->tv_usec)){ 318 alarmTimer(nvp); 319 eventTimer(nvp); 320 if (nvp->tv_usec < 500000L){ 321 svp->tv_usec = nvp->tv_usec + 500000L; 322 svp->tv_sec = nvp->tv_sec; 323 } else { 324 svp->tv_usec = nvp->tv_usec - 500000L; 325 svp->tv_sec = nvp->tv_sec + 1; 326 } 327 } 328 329 tvp = &timeout; 330 tvp->tv_sec = 0; 331 tvp->tv_usec = 500000L; 332 333 block = 0; 334 snmp_select_info(&nfds, &rfds, tvp, &block); 335 if (block == 1) 336 tvp = NULL; /* block without timeout */ 337 if ((n = select(nfds, &rfds, NULL, NULL, tvp)) < 0) 338 #else 339 if ((n = select(nfds, &rfds, NULL, NULL, NULL)) < 0) 340 #endif 341 { 342 if (errno != EINTR) /* SIGALRM is expected */ 343 log(LOG_WARNING, errno, "select failed"); 344 continue; 345 } 346 347 if (FD_ISSET(igmp_socket, &rfds)) { 348 recvlen = recvfrom(igmp_socket, recv_buf, RECV_BUF_SIZE, 349 0, NULL, &dummy); 350 if (recvlen < 0) { 351 if (errno != EINTR) log(LOG_ERR, errno, "recvfrom"); 352 continue; 353 } 354 (void)sigemptyset(&mask); 355 (void)sigaddset(&mask, SIGALRM); 356 if (sigprocmask(SIG_BLOCK, &mask, &omask) < 0) 357 log(LOG_ERR, errno, "sigprocmask"); 358 accept_igmp(recvlen); 359 (void)sigprocmask(SIG_SETMASK, &omask, NULL); 360 } 361 362 for (i = 0; i < nhandlers; i++) { 363 if (FD_ISSET(ihandlers[i].fd, &rfds)) { 364 (*ihandlers[i].func)(ihandlers[i].fd, &rfds); 365 } 366 } 367 368 #ifdef SNMP 369 snmp_read(&rfds); 370 snmp_timeout(); /* poll */ 371 #endif 372 } 373 } 374 375 376 /* 377 * routine invoked every second. Its main goal is to cycle through 378 * the routing table and send partial updates to all neighbors at a 379 * rate that will cause the entire table to be sent in ROUTE_REPORT_INTERVAL 380 * seconds. Also, every TIMER_INTERVAL seconds it calls timer() to 381 * do all the other time-based processing. 382 */ 383 static void 384 fasttimer(i) 385 int i; 386 { 387 static unsigned int tlast; 388 static unsigned int nsent; 389 register unsigned int t = tlast + 1; 390 register int n; 391 392 /* 393 * if we're in the last second, send everything that's left. 394 * otherwise send at least the fraction we should have sent by now. 395 */ 396 if (t >= ROUTE_REPORT_INTERVAL) { 397 register int nleft = nroutes - nsent; 398 while (nleft > 0) { 399 if ((n = report_next_chunk()) <= 0) 400 break; 401 nleft -= n; 402 } 403 tlast = 0; 404 nsent = 0; 405 } else { 406 register unsigned int ncum = nroutes * t / ROUTE_REPORT_INTERVAL; 407 while (nsent < ncum) { 408 if ((n = report_next_chunk()) <= 0) 409 break; 410 nsent += n; 411 } 412 tlast = t; 413 } 414 if ((t % TIMER_INTERVAL) == 0) 415 timer(); 416 417 age_callout_queue();/* Advance the timer for the callout queue 418 for groups */ 419 alarm(1); 420 } 421 422 /* 423 * The 'virtual_time' variable is initialized to a value that will cause the 424 * first invocation of timer() to send a probe or route report to all vifs 425 * and send group membership queries to all subnets for which this router is 426 * querier. This first invocation occurs approximately TIMER_INTERVAL seconds 427 * after the router starts up. Note that probes for neighbors and queries 428 * for group memberships are also sent at start-up time, as part of initial- 429 * ization. This repetition after a short interval is desirable for quickly 430 * building up topology and membership information in the presence of possible 431 * packet loss. 432 * 433 * 'virtual_time' advances at a rate that is only a crude approximation of 434 * real time, because it does not take into account any time spent processing, 435 * and because the timer intervals are sometimes shrunk by a random amount to 436 * avoid unwanted synchronization with other routers. 437 */ 438 439 static u_long virtual_time = 0; 440 441 442 /* 443 * Timer routine. Performs periodic neighbor probing, route reporting, and 444 * group querying duties, and drives various timers in routing entries and 445 * virtual interface data structures. 446 */ 447 static void 448 timer() 449 { 450 age_routes(); /* Advance the timers in the route entries */ 451 age_vifs(); /* Advance the timers for neighbors */ 452 age_table_entry(); /* Advance the timers for the cache entries */ 453 454 if (virtual_time % GROUP_QUERY_INTERVAL == 0) { 455 /* 456 * Time to query the local group memberships on all subnets 457 * for which this router is the elected querier. 458 */ 459 query_groups(); 460 } 461 462 if (virtual_time % NEIGHBOR_PROBE_INTERVAL == 0) { 463 /* 464 * Time to send a probe on all vifs from which no neighbors have 465 * been heard. Also, check if any inoperative interfaces have now 466 * come up. (If they have, they will also be probed as part of 467 * their initialization.) 468 */ 469 probe_for_neighbors(); 470 471 if (vifs_down) 472 check_vif_state(); 473 } 474 475 delay_change_reports = FALSE; 476 if (routes_changed) { 477 /* 478 * Some routes have changed since the last timer interrupt, but 479 * have not been reported yet. Report the changed routes to all 480 * neighbors. 481 */ 482 report_to_all_neighbors(CHANGED_ROUTES); 483 } 484 485 #ifdef SNMP 486 sync_timer(); 487 #endif 488 489 /* 490 * Advance virtual time 491 */ 492 virtual_time += TIMER_INTERVAL; 493 } 494 495 496 /* 497 * On termination, let everyone know we're going away. 498 */ 499 static void 500 done(i) 501 int i; 502 { 503 log(LOG_NOTICE, 0, "%s exiting", versionstring); 504 cleanup(); 505 _exit(1); 506 } 507 508 static void 509 cleanup() 510 { 511 static in_cleanup = 0; 512 513 if (!in_cleanup) { 514 in_cleanup++; 515 #ifdef RSRR 516 rsrr_clean(); 517 #endif /* RSRR */ 518 expire_all_routes(); 519 report_to_all_neighbors(ALL_ROUTES); 520 k_stop_dvmrp(); 521 } 522 } 523 524 525 /* 526 * Dump internal data structures to stderr. 527 */ 528 static void 529 dump(i) 530 int i; 531 { 532 dump_vifs(stderr); 533 dump_routes(stderr); 534 } 535 536 537 /* 538 * Dump internal data structures to a file. 539 */ 540 static void 541 fdump(i) 542 int i; 543 { 544 FILE *fp; 545 546 fp = fopen(dumpfilename, "w"); 547 if (fp != NULL) { 548 dump_vifs(fp); 549 dump_routes(fp); 550 (void) fclose(fp); 551 } 552 } 553 554 555 /* 556 * Dump local cache contents to a file. 557 */ 558 static void 559 cdump(i) 560 int i; 561 { 562 FILE *fp; 563 564 fp = fopen(cachefilename, "w"); 565 if (fp != NULL) { 566 dump_cache(fp); 567 (void) fclose(fp); 568 } 569 } 570 571 572 /* 573 * Restart mrouted 574 */ 575 static void 576 restart(i) 577 int i; 578 { 579 sigset_t mask, omask; 580 581 log(LOG_NOTICE, 0, "%s restart", versionstring); 582 583 /* 584 * reset all the entries 585 */ 586 (void)sigemptyset(&mask); 587 (void)sigaddset(&mask, SIGALRM); 588 if (sigprocmask(SIG_BLOCK, &mask, &omask) < 0) 589 log(LOG_ERR, errno, "sigprocmask"); 590 free_all_prunes(); 591 free_all_routes(); 592 stop_all_vifs(); 593 k_stop_dvmrp(); 594 close(igmp_socket); 595 close(udp_socket); 596 597 /* 598 * start processing again 599 */ 600 dvmrp_genid++; 601 pruning = 1; 602 603 init_igmp(); 604 init_routes(); 605 init_ktable(); 606 init_vifs(); 607 k_init_dvmrp(); /* enable DVMRP routing in kernel */ 608 init_installvifs(); 609 610 (void)sigprocmask(SIG_SETMASK, &omask, NULL); 611 } 612 613 #define LOG_MAX_MSGS 20 /* if > 20/minute then shut up for a while */ 614 #define LOG_SHUT_UP 600 /* shut up for 10 minutes */ 615 static int log_nmsgs = 0; 616 617 static void 618 resetlogging(arg) 619 void *arg; 620 { 621 int nxttime = 60; 622 void *narg = NULL; 623 624 if (arg == NULL && log_nmsgs > LOG_MAX_MSGS) { 625 nxttime = LOG_SHUT_UP; 626 narg = (void *)&log_nmsgs; /* just need some valid void * */ 627 syslog(LOG_WARNING, "logging too fast, shutting up for %d minutes", 628 LOG_SHUT_UP / 60); 629 } else { 630 log_nmsgs = 0; 631 } 632 633 timer_setTimer(nxttime, resetlogging, narg); 634 } 635 636 /* 637 * Log errors and other messages to the system log daemon and to stderr, 638 * according to the severity of the message and the current debug level. 639 * For errors of severity LOG_ERR or worse, terminate the program. 640 */ 641 #ifdef __STDC__ 642 void 643 log(int severity, int syserr, char *format, ...) 644 { 645 va_list ap; 646 static char fmt[211] = "warning - "; 647 char *msg; 648 char tbuf[20]; 649 struct timeval now; 650 struct tm *thyme; 651 time_t t; 652 653 va_start(ap, format); 654 #else 655 /*VARARGS3*/ 656 void 657 log(severity, syserr, format, va_alist) 658 int severity, syserr; 659 char *format; 660 va_dcl 661 { 662 va_list ap; 663 static char fmt[211] = "warning - "; 664 char *msg; 665 char tbuf[20]; 666 struct timeval now; 667 struct tm *thyme; 668 time_t t; 669 670 va_start(ap); 671 #endif 672 vsprintf(&fmt[10], format, ap); 673 va_end(ap); 674 msg = (severity == LOG_WARNING) ? fmt : &fmt[10]; 675 676 switch (debug) { 677 case 0: break; 678 case 1: if (severity > LOG_NOTICE) break; 679 case 2: if (severity > LOG_INFO ) break; 680 default: 681 gettimeofday(&now,NULL); 682 t = now.tv_sec; 683 thyme = localtime(&t); 684 strftime(tbuf, sizeof(tbuf), "%X.%%03d ", thyme); 685 fprintf(stderr, tbuf, now.tv_usec / 1000); 686 fprintf(stderr, "%s", msg); 687 if (syserr == 0) 688 fprintf(stderr, "\n"); 689 else if (syserr < sys_nerr) 690 fprintf(stderr, ": %s\n", sys_errlist[syserr]); 691 else 692 fprintf(stderr, ": errno %d\n", syserr); 693 } 694 695 if (severity <= LOG_NOTICE) { 696 if (log_nmsgs++ < LOG_MAX_MSGS) { 697 if (syserr != 0) { 698 errno = syserr; 699 syslog(severity, "%s: %m", msg); 700 } else 701 syslog(severity, "%s", msg); 702 } 703 704 if (severity <= LOG_ERR) exit(1); 705 } 706 } 707 708 #ifdef DEBUG_MFC 709 void 710 md_log(what, origin, mcastgrp) 711 int what; 712 u_int32_t origin, mcastgrp; 713 { 714 static FILE *f = NULL; 715 struct timeval tv; 716 u_int32_t buf[4]; 717 718 if (!f) { 719 if ((f = fopen("/tmp/mrouted.clog", "w")) == NULL) { 720 log(LOG_ERR, errno, "open /tmp/mrouted.clog"); 721 } 722 } 723 724 gettimeofday(&tv, NULL); 725 buf[0] = tv.tv_sec; 726 buf[1] = what; 727 buf[2] = origin; 728 buf[3] = mcastgrp; 729 730 fwrite(buf, sizeof(u_int32_t), 4, f); 731 } 732 #endif 733