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