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