1 /* $NetBSD: main.c,v 1.22 2006/05/09 20:18:09 mrg 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.22 2006/05/09 20:18:09 mrg 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 socklen_t 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 for(;;) { 283 #ifdef SYSV 284 sigset_t block, oblock; 285 #endif 286 bcopy((char *)&readers, (char *)&rfds, sizeof(rfds)); 287 #ifdef SNMP 288 gettimeofday(nvp, 0); 289 if (nvp->tv_sec > svp->tv_sec 290 || (nvp->tv_sec == svp->tv_sec && nvp->tv_usec > svp->tv_usec)){ 291 alarmTimer(nvp); 292 eventTimer(nvp); 293 if (nvp->tv_usec < 500000L){ 294 svp->tv_usec = nvp->tv_usec + 500000L; 295 svp->tv_sec = nvp->tv_sec; 296 } else { 297 svp->tv_usec = nvp->tv_usec - 500000L; 298 svp->tv_sec = nvp->tv_sec + 1; 299 } 300 } 301 302 tvp = &timeout; 303 tvp->tv_sec = 0; 304 tvp->tv_usec = 500000L; 305 306 block = 0; 307 snmp_select_info(&nfds, &rfds, tvp, &block); 308 if (block == 1) 309 tvp = NULL; /* block without timeout */ 310 if ((n = select(nfds, &rfds, NULL, NULL, tvp)) < 0) 311 #else 312 if ((n = select(nfds, &rfds, NULL, NULL, NULL)) < 0) 313 #endif 314 { 315 if (errno != EINTR) /* SIGALRM is expected */ 316 logit(LOG_WARNING, errno, "select failed"); 317 continue; 318 } 319 320 if (FD_ISSET(igmp_socket, &rfds)) { 321 recvlen = recvfrom(igmp_socket, recv_buf, RECV_BUF_SIZE, 322 0, NULL, &dummy); 323 if (recvlen < 0) { 324 if (errno != EINTR) logit(LOG_ERR, errno, "recvfrom"); 325 continue; 326 } 327 #ifdef SYSV 328 (void)sigemptyset(&block); 329 (void)sigaddset(&block, SIGALRM); 330 if (sigprocmask(SIG_BLOCK, &block, &oblock) < 0) 331 logit(LOG_ERR, errno, "sigprocmask"); 332 #else 333 omask = sigblock(sigmask(SIGALRM)); 334 #endif 335 accept_igmp(recvlen); 336 #ifdef SYSV 337 (void)sigprocmask(SIG_SETMASK, &oblock, (sigset_t *)NULL); 338 #else 339 (void)sigsetmask(omask); 340 #endif 341 } 342 343 for (i = 0; i < nhandlers; i++) { 344 if (FD_ISSET(ihandlers[i].fd, &rfds)) { 345 (*ihandlers[i].func)(ihandlers[i].fd, &rfds); 346 } 347 } 348 349 #ifdef SNMP 350 snmp_read(&rfds); 351 snmp_timeout(); /* poll */ 352 #endif 353 } 354 } 355 356 357 /* 358 * routine invoked every second. Its main goal is to cycle through 359 * the routing table and send partial updates to all neighbors at a 360 * rate that will cause the entire table to be sent in ROUTE_REPORT_INTERVAL 361 * seconds. Also, every TIMER_INTERVAL seconds it calls timer() to 362 * do all the other time-based processing. 363 */ 364 static void 365 fasttimer(int i) 366 { 367 static unsigned int tlast; 368 static unsigned int nsent; 369 unsigned int t = tlast + 1; 370 int n; 371 372 /* 373 * if we're in the last second, send everything that's left. 374 * otherwise send at least the fraction we should have sent by now. 375 */ 376 if (t >= ROUTE_REPORT_INTERVAL) { 377 int nleft = nroutes - nsent; 378 while (nleft > 0) { 379 if ((n = report_next_chunk()) <= 0) 380 break; 381 nleft -= n; 382 } 383 tlast = 0; 384 nsent = 0; 385 } else { 386 unsigned int ncum = nroutes * t / ROUTE_REPORT_INTERVAL; 387 while (nsent < ncum) { 388 if ((n = report_next_chunk()) <= 0) 389 break; 390 nsent += n; 391 } 392 tlast = t; 393 } 394 if ((t % TIMER_INTERVAL) == 0) 395 timer(); 396 397 age_callout_queue();/* Advance the timer for the callout queue 398 for groups */ 399 alarm(1); 400 } 401 402 /* 403 * The 'virtual_time' variable is initialized to a value that will cause the 404 * first invocation of timer() to send a probe or route report to all vifs 405 * and send group membership queries to all subnets for which this router is 406 * querier. This first invocation occurs approximately TIMER_INTERVAL seconds 407 * after the router starts up. Note that probes for neighbors and queries 408 * for group memberships are also sent at start-up time, as part of initial- 409 * ization. This repetition after a short interval is desirable for quickly 410 * building up topology and membership information in the presence of possible 411 * packet loss. 412 * 413 * 'virtual_time' advances at a rate that is only a crude approximation of 414 * real time, because it does not take into account any time spent processing, 415 * and because the timer intervals are sometimes shrunk by a random amount to 416 * avoid unwanted synchronization with other routers. 417 */ 418 419 static u_long virtual_time = 0; 420 421 422 /* 423 * Timer routine. Performs periodic neighbor probing, route reporting, and 424 * group querying duties, and drives various timers in routing entries and 425 * virtual interface data structures. 426 */ 427 static void 428 timer(void) 429 { 430 age_routes(); /* Advance the timers in the route entries */ 431 age_vifs(); /* Advance the timers for neighbors */ 432 age_table_entry(); /* Advance the timers for the cache entries */ 433 434 if (virtual_time % GROUP_QUERY_INTERVAL == 0) { 435 /* 436 * Time to query the local group memberships on all subnets 437 * for which this router is the elected querier. 438 */ 439 query_groups(); 440 } 441 442 if (virtual_time % NEIGHBOR_PROBE_INTERVAL == 0) { 443 /* 444 * Time to send a probe on all vifs from which no neighbors have 445 * been heard. Also, check if any inoperative interfaces have now 446 * come up. (If they have, they will also be probed as part of 447 * their initialization.) 448 */ 449 probe_for_neighbors(); 450 451 if (vifs_down) 452 check_vif_state(); 453 } 454 455 delay_change_reports = FALSE; 456 if (routes_changed) { 457 /* 458 * Some routes have changed since the last timer interrupt, but 459 * have not been reported yet. Report the changed routes to all 460 * neighbors. 461 */ 462 report_to_all_neighbors(CHANGED_ROUTES); 463 } 464 465 #ifdef SNMP 466 sync_timer(); 467 #endif 468 469 /* 470 * Advance virtual time 471 */ 472 virtual_time += TIMER_INTERVAL; 473 } 474 475 476 /* 477 * On termination, let everyone know we're going away. 478 */ 479 static void 480 done(int i) 481 { 482 logit(LOG_NOTICE, 0, "%s exiting", versionstring); 483 cleanup(); 484 _exit(1); 485 } 486 487 static void 488 cleanup(void) 489 { 490 static int in_cleanup = 0; 491 492 if (!in_cleanup) { 493 in_cleanup++; 494 #ifdef RSRR 495 rsrr_clean(); 496 #endif /* RSRR */ 497 expire_all_routes(); 498 report_to_all_neighbors(ALL_ROUTES); 499 k_stop_dvmrp(); 500 } 501 } 502 503 504 /* 505 * Dump internal data structures to stderr. 506 */ 507 static void 508 dump(int i) 509 { 510 dump_vifs(stderr); 511 dump_routes(stderr); 512 } 513 514 515 /* 516 * Dump internal data structures to a file. 517 */ 518 static void 519 fdump(int i) 520 { 521 FILE *fp; 522 523 fp = fopen(dumpfilename, "w"); 524 if (fp != NULL) { 525 dump_vifs(fp); 526 dump_routes(fp); 527 (void) fclose(fp); 528 } 529 } 530 531 532 /* 533 * Dump local cache contents to a file. 534 */ 535 static void 536 cdump(int i) 537 { 538 FILE *fp; 539 540 fp = fopen(cachefilename, "w"); 541 if (fp != NULL) { 542 dump_cache(fp); 543 (void) fclose(fp); 544 } 545 } 546 547 548 /* 549 * Restart mrouted 550 */ 551 static void 552 restart(int i) 553 { 554 int omask; 555 #ifdef SYSV 556 sigset_t block, oblock; 557 #endif 558 559 logit(LOG_NOTICE, 0, "%s restart", versionstring); 560 561 /* 562 * reset all the entries 563 */ 564 #ifdef SYSV 565 (void)sigemptyset(&block); 566 (void)sigaddset(&block, SIGALRM); 567 if (sigprocmask(SIG_BLOCK, &block, &oblock) < 0) 568 logit(LOG_ERR, errno, "sigprocmask"); 569 #else 570 omask = sigblock(sigmask(SIGALRM)); 571 #endif 572 free_all_prunes(); 573 free_all_routes(); 574 stop_all_vifs(); 575 k_stop_dvmrp(); 576 close(igmp_socket); 577 close(udp_socket); 578 579 /* 580 * start processing again 581 */ 582 dvmrp_genid++; 583 pruning = 1; 584 585 init_igmp(); 586 init_routes(); 587 init_ktable(); 588 init_vifs(); 589 k_init_dvmrp(); /* enable DVMRP routing in kernel */ 590 init_installvifs(); 591 592 #ifdef SYSV 593 (void)sigprocmask(SIG_SETMASK, &oblock, (sigset_t *)NULL); 594 #else 595 (void)sigsetmask(omask); 596 #endif 597 } 598 599 #define LOG_MAX_MSGS 20 /* if > 20/minute then shut up for a while */ 600 #define LOG_SHUT_UP 600 /* shut up for 10 minutes */ 601 static int log_nmsgs = 0; 602 603 static void 604 resetlogging(void *arg) 605 { 606 int nxttime = 60; 607 void *narg = NULL; 608 609 if (arg == NULL && log_nmsgs > LOG_MAX_MSGS) { 610 nxttime = LOG_SHUT_UP; 611 narg = (void *)&log_nmsgs; /* just need some valid void * */ 612 syslog(LOG_WARNING, "logging too fast, shutting up for %d minutes", 613 LOG_SHUT_UP / 60); 614 } else { 615 log_nmsgs = 0; 616 } 617 618 timer_setTimer(nxttime, resetlogging, narg); 619 } 620 621 /* 622 * Log errors and other messages to the system log daemon and to stderr, 623 * according to the severity of the message and the current debug level. 624 * For errors of severity LOG_ERR or worse, terminate the program. 625 */ 626 void 627 logit(int severity, int syserr, const char *format, ...) 628 { 629 va_list ap; 630 static char fmt[211] = "warning - "; 631 char *msg; 632 char tbuf[20]; 633 struct timeval now; 634 struct tm *thyme; 635 time_t t; 636 637 va_start(ap, format); 638 vsnprintf(&fmt[10], sizeof(fmt) - 10, format, ap); 639 va_end(ap); 640 msg = (severity == LOG_WARNING) ? fmt : &fmt[10]; 641 642 switch (debug) { 643 case 0: break; 644 case 1: if (severity > LOG_NOTICE) break; 645 case 2: if (severity > LOG_INFO ) break; 646 default: 647 gettimeofday(&now,NULL); 648 t = now.tv_sec; 649 thyme = localtime(&t); 650 strftime(tbuf, sizeof(tbuf), "%X", thyme); 651 fprintf(stderr, "%s.%03ld %s", tbuf, (long)now.tv_usec / 1000, 652 msg); 653 if (syserr == 0) 654 fprintf(stderr, "\n"); 655 else 656 fprintf(stderr, ": %s\n", strerror(syserr)); 657 } 658 659 if (severity <= LOG_NOTICE) { 660 if (log_nmsgs++ < LOG_MAX_MSGS) { 661 if (syserr != 0) { 662 errno = syserr; 663 syslog(severity, "%s: %m", msg); 664 } else 665 syslog(severity, "%s", msg); 666 } 667 668 if (severity <= LOG_ERR) exit(1); 669 } 670 } 671 672 #ifdef DEBUG_MFC 673 void 674 md_log(int what, u_int32_t origin, u_int32_t mcastgrp) 675 { 676 static FILE *f = NULL; 677 struct timeval tv; 678 u_int32_t buf[4]; 679 680 if (!f) { 681 if ((f = fopen("/tmp/mrouted.clog", "w")) == NULL) { 682 logit(LOG_ERR, errno, "open /tmp/mrouted.clog"); 683 } 684 } 685 686 gettimeofday(&tv, NULL); 687 buf[0] = tv.tv_sec; 688 buf[1] = what; 689 buf[2] = origin; 690 buf[3] = mcastgrp; 691 692 fwrite(buf, sizeof(u_int32_t), 4, f); 693 } 694 #endif 695