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