Home | History | Annotate | Line # | Download | only in routed
main.c revision 1.39.30.1
      1  1.39.30.1  wrstuden /*	$NetBSD: main.c,v 1.39.30.1 2008/09/18 04:28:29 wrstuden Exp $	*/
      2        1.7       cgd 
      3        1.1       cgd /*
      4        1.5   mycroft  * Copyright (c) 1983, 1988, 1993
      5        1.5   mycroft  *	The Regents of the University of California.  All rights reserved.
      6        1.1       cgd  *
      7        1.1       cgd  * Redistribution and use in source and binary forms, with or without
      8        1.1       cgd  * modification, are permitted provided that the following conditions
      9        1.1       cgd  * are met:
     10        1.1       cgd  * 1. Redistributions of source code must retain the above copyright
     11        1.1       cgd  *    notice, this list of conditions and the following disclaimer.
     12        1.1       cgd  * 2. Redistributions in binary form must reproduce the above copyright
     13        1.1       cgd  *    notice, this list of conditions and the following disclaimer in the
     14        1.1       cgd  *    documentation and/or other materials provided with the distribution.
     15        1.1       cgd  * 3. All advertising materials mentioning features or use of this software
     16       1.20  christos  *    must display the following acknowledgment:
     17        1.1       cgd  *	This product includes software developed by the University of
     18        1.1       cgd  *	California, Berkeley and its contributors.
     19        1.1       cgd  * 4. Neither the name of the University nor the names of its contributors
     20        1.1       cgd  *    may be used to endorse or promote products derived from this software
     21        1.1       cgd  *    without specific prior written permission.
     22        1.1       cgd  *
     23        1.1       cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     24        1.1       cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     25        1.1       cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     26        1.1       cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     27        1.1       cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     28        1.1       cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     29        1.1       cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     30        1.1       cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     31        1.1       cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     32        1.1       cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     33        1.1       cgd  * SUCH DAMAGE.
     34        1.1       cgd  */
     35        1.1       cgd 
     36        1.1       cgd #include "defs.h"
     37       1.14   thorpej #include "pathnames.h"
     38       1.14   thorpej #ifdef sgi
     39       1.14   thorpej #include "math.h"
     40       1.14   thorpej #endif
     41       1.14   thorpej #include <signal.h>
     42       1.14   thorpej #include <fcntl.h>
     43        1.1       cgd #include <sys/file.h>
     44        1.1       cgd 
     45  1.39.30.1  wrstuden __COPYRIGHT("@(#) Copyright (c) 1983, 1988, 1993\
     46  1.39.30.1  wrstuden  The Regents of the University of California.  All rights reserved.");
     47       1.28  christos #ifdef __NetBSD__
     48  1.39.30.1  wrstuden __RCSID("$NetBSD: main.c,v 1.39.30.1 2008/09/18 04:28:29 wrstuden Exp $");
     49       1.28  christos #elif defined(__FreeBSD__)
     50       1.28  christos __RCSID("$FreeBSD$");
     51       1.28  christos #else
     52       1.31  christos __RCSID("Revision: 2.27 ");
     53       1.31  christos #ident "Revision: 2.27 "
     54       1.28  christos #endif
     55       1.28  christos 
     56       1.23   thorpej #if defined(__NetBSD__)
     57       1.23   thorpej #include <util.h>
     58       1.23   thorpej #endif
     59       1.23   thorpej 
     60       1.14   thorpej pid_t	mypid;
     61       1.14   thorpej 
     62       1.14   thorpej naddr	myaddr;				/* system address */
     63       1.14   thorpej char	myname[MAXHOSTNAMELEN+1];
     64        1.1       cgd 
     65       1.18   thorpej int	verbose;
     66       1.18   thorpej 
     67       1.14   thorpej int	supplier;			/* supply or broadcast updates */
     68       1.14   thorpej int	supplier_set;
     69       1.14   thorpej int	ipforwarding = 1;		/* kernel forwarding on */
     70       1.14   thorpej 
     71       1.14   thorpej int	default_gateway;		/* 1=advertise default */
     72       1.14   thorpej int	background = 1;
     73       1.14   thorpej int	ridhosts;			/* 1=reduce host routes */
     74       1.14   thorpej int	mhome;				/* 1=want multi-homed host route */
     75       1.20  christos int	advertise_mhome;		/* 1=must continue advertising it */
     76       1.14   thorpej int	auth_ok = 1;			/* 1=ignore auth if we do not care */
     77       1.14   thorpej 
     78       1.14   thorpej struct timeval epoch;			/* when started */
     79       1.14   thorpej struct timeval clk, prev_clk;
     80       1.24  christos static int usec_fudge;
     81       1.14   thorpej struct timeval now;			/* current idea of time */
     82       1.14   thorpej time_t	now_stale;
     83       1.15  christos time_t	now_expire;
     84       1.14   thorpej time_t	now_garbage;
     85       1.14   thorpej 
     86       1.14   thorpej struct timeval next_bcast;		/* next general broadcast */
     87       1.28  christos struct timeval no_flash = {		/* inhibit flash update */
     88       1.28  christos 	EPOCH+SUPPLY_INTERVAL, 0
     89       1.28  christos };
     90       1.14   thorpej 
     91       1.18   thorpej struct timeval flush_kern_timer;
     92       1.18   thorpej 
     93       1.33    itojun fd_set	*fdbitsp;
     94       1.14   thorpej int	sock_max;
     95       1.14   thorpej int	rip_sock = -1;			/* RIP socket */
     96       1.14   thorpej struct interface *rip_sock_mcast;	/* current multicast interface */
     97       1.14   thorpej int	rt_sock;			/* routing socket */
     98       1.14   thorpej int	rt_sock_seqno;
     99        1.1       cgd 
    100        1.1       cgd 
    101       1.14   thorpej static  int get_rip_sock(naddr, int);
    102       1.14   thorpej static void timevalsub(struct timeval *, struct timeval *, struct timeval *);
    103        1.1       cgd 
    104        1.6       cgd int
    105       1.14   thorpej main(int argc,
    106       1.14   thorpej      char *argv[])
    107       1.14   thorpej {
    108       1.14   thorpej 	int n, mib[4], off;
    109       1.14   thorpej 	size_t len;
    110       1.14   thorpej 	char *p, *q;
    111       1.21  christos 	const char *cp;
    112       1.14   thorpej 	struct timeval wtime, t2;
    113       1.14   thorpej 	time_t dt;
    114       1.35     enami 	fd_set *ibitsp = NULL;
    115       1.16  christos 	naddr p_net, p_mask;
    116       1.14   thorpej 	struct interface *ifp;
    117       1.14   thorpej 	struct parm parm;
    118       1.14   thorpej 	char *tracename = 0;
    119       1.14   thorpej 
    120       1.14   thorpej 
    121       1.16  christos 	/* Some shells are badly broken and send SIGHUP to backgrounded
    122       1.16  christos 	 * processes.
    123       1.16  christos 	 */
    124       1.16  christos 	signal(SIGHUP, SIG_IGN);
    125       1.16  christos 
    126       1.26     lukem 	openlog("routed", LOG_PID, LOG_DAEMON);
    127       1.14   thorpej 	ftrace = stdout;
    128       1.14   thorpej 
    129       1.14   thorpej 	gettimeofday(&clk, 0);
    130       1.14   thorpej 	prev_clk = clk;
    131       1.14   thorpej 	epoch = clk;
    132       1.14   thorpej 	epoch.tv_sec -= EPOCH;
    133       1.14   thorpej 	now.tv_sec = EPOCH;
    134       1.14   thorpej 	now_stale = EPOCH - STALE_TIME;
    135       1.15  christos 	now_expire = EPOCH - EXPIRE_TIME;
    136       1.14   thorpej 	now_garbage = EPOCH - GARBAGE_TIME;
    137       1.14   thorpej 	wtime.tv_sec = 0;
    138       1.14   thorpej 
    139       1.24  christos 	(void)gethostname(myname, sizeof(myname) - 1);
    140       1.14   thorpej 	(void)gethost(myname, &myaddr);
    141       1.14   thorpej 
    142       1.37       wiz 	while ((n = getopt(argc, argv, "sqdghmAtvT:F:P:")) != -1) {
    143       1.14   thorpej 		switch (n) {
    144       1.14   thorpej 		case 's':
    145       1.14   thorpej 			supplier = 1;
    146       1.14   thorpej 			supplier_set = 1;
    147       1.14   thorpej 			break;
    148       1.14   thorpej 
    149       1.14   thorpej 		case 'q':
    150       1.14   thorpej 			supplier = 0;
    151       1.14   thorpej 			supplier_set = 1;
    152       1.14   thorpej 			break;
    153       1.14   thorpej 
    154       1.14   thorpej 		case 'd':
    155       1.14   thorpej 			background = 0;
    156       1.14   thorpej 			break;
    157       1.14   thorpej 
    158       1.14   thorpej 		case 'g':
    159       1.17     lukem 			memset(&parm, 0, sizeof(parm));
    160       1.14   thorpej 			parm.parm_d_metric = 1;
    161       1.21  christos 			cp = check_parms(&parm);
    162       1.21  christos 			if (cp != 0)
    163       1.21  christos 				msglog("bad -g: %s", cp);
    164       1.14   thorpej 			else
    165       1.14   thorpej 				default_gateway = 1;
    166       1.14   thorpej 			break;
    167       1.14   thorpej 
    168       1.14   thorpej 		case 'h':		/* suppress extra host routes */
    169       1.14   thorpej 			ridhosts = 1;
    170       1.14   thorpej 			break;
    171       1.14   thorpej 
    172       1.14   thorpej 		case 'm':		/* advertise host route */
    173       1.14   thorpej 			mhome = 1;	/* on multi-homed hosts */
    174       1.14   thorpej 			break;
    175       1.14   thorpej 
    176       1.14   thorpej 		case 'A':
    177       1.14   thorpej 			/* Ignore authentication if we do not care.
    178       1.14   thorpej 			 * Crazy as it is, that is what RFC 1723 requires.
    179       1.14   thorpej 			 */
    180       1.14   thorpej 			auth_ok = 0;
    181       1.14   thorpej 			break;
    182       1.14   thorpej 
    183       1.14   thorpej 		case 't':
    184       1.14   thorpej 			new_tracelevel++;
    185       1.14   thorpej 			break;
    186       1.14   thorpej 
    187       1.14   thorpej 		case 'T':
    188       1.14   thorpej 			tracename = optarg;
    189       1.14   thorpej 			break;
    190       1.14   thorpej 
    191       1.14   thorpej 		case 'F':		/* minimal routes for SLIP */
    192       1.16  christos 			n = FAKE_METRIC;
    193       1.14   thorpej 			p = strchr(optarg,',');
    194       1.14   thorpej 			if (p && *p != '\0') {
    195       1.14   thorpej 				n = (int)strtoul(p+1, &q, 0);
    196       1.14   thorpej 				if (*q == '\0'
    197       1.14   thorpej 				    && n <= HOPCNT_INFINITY-1
    198       1.14   thorpej 				    && n >= 1)
    199       1.14   thorpej 					*p = '\0';
    200       1.14   thorpej 			}
    201       1.16  christos 			if (!getnet(optarg, &p_net, &p_mask)) {
    202       1.14   thorpej 				msglog("bad network; \"-F %s\"",
    203       1.14   thorpej 				       optarg);
    204       1.14   thorpej 				break;
    205       1.14   thorpej 			}
    206       1.17     lukem 			memset(&parm, 0, sizeof(parm));
    207       1.16  christos 			parm.parm_net = p_net;
    208       1.14   thorpej 			parm.parm_mask = p_mask;
    209       1.14   thorpej 			parm.parm_d_metric = n;
    210       1.21  christos 			cp = check_parms(&parm);
    211       1.21  christos 			if (cp != 0)
    212       1.21  christos 				msglog("bad -F: %s", cp);
    213       1.14   thorpej 			break;
    214       1.14   thorpej 
    215       1.14   thorpej 		case 'P':
    216       1.18   thorpej 			/* handle arbitrary parameters.
    217       1.14   thorpej 			 */
    218       1.18   thorpej 			q = strdup(optarg);
    219       1.21  christos 			cp = parse_parms(q, 0);
    220       1.21  christos 			if (cp != 0)
    221       1.21  christos 				msglog("%s in \"-P %s\"", cp, optarg);
    222       1.18   thorpej 			free(q);
    223       1.18   thorpej 			break;
    224       1.18   thorpej 
    225       1.18   thorpej 		case 'v':
    226       1.18   thorpej 			/* display version */
    227       1.18   thorpej 			verbose++;
    228       1.36  christos 			msglog("version 2.28");
    229       1.14   thorpej 			break;
    230       1.14   thorpej 
    231       1.14   thorpej 		default:
    232       1.14   thorpej 			goto usage;
    233       1.14   thorpej 		}
    234       1.14   thorpej 	}
    235       1.14   thorpej 	argc -= optind;
    236       1.14   thorpej 	argv += optind;
    237       1.14   thorpej 
    238       1.14   thorpej 	if (tracename == 0 && argc >= 1) {
    239       1.14   thorpej 		tracename = *argv++;
    240       1.14   thorpej 		argc--;
    241       1.14   thorpej 	}
    242       1.16  christos 	if (tracename != 0 && tracename[0] == '\0')
    243       1.16  christos 		goto usage;
    244       1.14   thorpej 	if (argc != 0) {
    245       1.14   thorpej usage:
    246       1.38       wiz 		logbad(0, "usage: routed [-sqdghmAtv] [-T tracefile]"
    247       1.39       wiz 		       " [-F net[/mask[,metric]]] [-P parms]");
    248       1.14   thorpej 	}
    249       1.18   thorpej 	if (geteuid() != 0) {
    250       1.18   thorpej 		if (verbose)
    251       1.18   thorpej 			exit(0);
    252       1.14   thorpej 		logbad(0, "requires UID 0");
    253       1.18   thorpej 	}
    254       1.14   thorpej 
    255       1.14   thorpej 	mib[0] = CTL_NET;
    256       1.14   thorpej 	mib[1] = PF_INET;
    257       1.14   thorpej 	mib[2] = IPPROTO_IP;
    258       1.14   thorpej 	mib[3] = IPCTL_FORWARDING;
    259       1.14   thorpej 	len = sizeof(ipforwarding);
    260       1.14   thorpej 	if (sysctl(mib, 4, &ipforwarding, &len, 0, 0) < 0)
    261       1.14   thorpej 		LOGERR("sysctl(IPCTL_FORWARDING)");
    262       1.14   thorpej 
    263       1.14   thorpej 	if (!ipforwarding) {
    264       1.14   thorpej 		if (supplier)
    265       1.14   thorpej 			msglog("-s incompatible with ipforwarding=0");
    266       1.14   thorpej 		if (default_gateway) {
    267       1.14   thorpej 			msglog("-g incompatible with ipforwarding=0");
    268       1.14   thorpej 			default_gateway = 0;
    269       1.14   thorpej 		}
    270       1.14   thorpej 		supplier = 0;
    271       1.14   thorpej 		supplier_set = 1;
    272       1.14   thorpej 	}
    273       1.14   thorpej 	if (default_gateway) {
    274       1.14   thorpej 		if (supplier_set && !supplier) {
    275       1.14   thorpej 			msglog("-g and -q incompatible");
    276       1.14   thorpej 		} else {
    277       1.14   thorpej 			supplier = 1;
    278       1.14   thorpej 			supplier_set = 1;
    279       1.14   thorpej 		}
    280       1.14   thorpej 	}
    281       1.14   thorpej 
    282       1.14   thorpej 
    283       1.15  christos 	signal(SIGALRM, sigalrm);
    284       1.15  christos 	if (!background)
    285       1.15  christos 		signal(SIGHUP, sigterm);    /* SIGHUP fatal during debugging */
    286       1.15  christos 	signal(SIGTERM, sigterm);
    287       1.15  christos 	signal(SIGINT, sigterm);
    288       1.15  christos 	signal(SIGUSR1, sigtrace_on);
    289       1.15  christos 	signal(SIGUSR2, sigtrace_off);
    290       1.15  christos 
    291       1.14   thorpej 	/* get into the background */
    292       1.14   thorpej #ifdef sgi
    293       1.16  christos 	if (0 > _daemonize(background ? 0 : (_DF_NOCHDIR|_DF_NOFORK),
    294       1.18   thorpej 			   STDIN_FILENO, STDOUT_FILENO, STDERR_FILENO))
    295       1.16  christos 		BADERR(0, "_daemonize()");
    296        1.1       cgd #else
    297       1.18   thorpej 	if (background && daemon(0, 1) < 0)
    298       1.16  christos 		BADERR(0,"daemon()");
    299        1.1       cgd #endif
    300       1.14   thorpej 
    301       1.23   thorpej #if defined(__NetBSD__)
    302       1.23   thorpej 	pidfile(NULL);
    303       1.23   thorpej #endif
    304       1.14   thorpej 	mypid = getpid();
    305       1.14   thorpej 
    306       1.14   thorpej 	/* prepare socket connected to the kernel.
    307       1.14   thorpej 	 */
    308       1.21  christos 	rt_sock = socket(AF_ROUTE, SOCK_RAW, 0);
    309       1.14   thorpej 	if (rt_sock < 0)
    310       1.14   thorpej 		BADERR(1,"rt_sock = socket()");
    311       1.14   thorpej 	if (fcntl(rt_sock, F_SETFL, O_NONBLOCK) == -1)
    312       1.14   thorpej 		logbad(1, "fcntl(rt_sock) O_NONBLOCK: %s", strerror(errno));
    313       1.14   thorpej 	off = 0;
    314       1.14   thorpej 	if (setsockopt(rt_sock, SOL_SOCKET,SO_USELOOPBACK,
    315       1.14   thorpej 		       &off,sizeof(off)) < 0)
    316       1.14   thorpej 		LOGERR("setsockopt(SO_USELOOPBACK,0)");
    317       1.14   thorpej 
    318       1.14   thorpej 	fix_select();
    319       1.14   thorpej 
    320       1.14   thorpej 
    321       1.14   thorpej 	if (tracename != 0) {
    322       1.32    itojun 		strlcpy(inittracename, tracename, sizeof(inittracename));
    323       1.16  christos 		set_tracefile(inittracename, "%s", -1);
    324       1.16  christos 	} else {
    325       1.16  christos 		tracelevel_msg("%s", -1);   /* turn on tracing to stdio */
    326       1.14   thorpej 	}
    327       1.16  christos 
    328       1.16  christos 	bufinit();
    329       1.14   thorpej 
    330       1.14   thorpej 	/* initialize radix tree */
    331       1.14   thorpej 	rtinit();
    332       1.14   thorpej 
    333       1.14   thorpej 	/* Pick a random part of the second for our output to minimize
    334       1.14   thorpej 	 * collisions.
    335       1.14   thorpej 	 *
    336       1.14   thorpej 	 * Start broadcasting after hearing from other routers, and
    337       1.14   thorpej 	 * at a random time so a bunch of systems do not get synchronized
    338       1.14   thorpej 	 * after a power failure.
    339       1.14   thorpej 	 */
    340       1.14   thorpej 	intvl_random(&next_bcast, EPOCH+MIN_WAITTIME, EPOCH+SUPPLY_INTERVAL);
    341       1.14   thorpej 	age_timer.tv_usec = next_bcast.tv_usec;
    342       1.14   thorpej 	age_timer.tv_sec = EPOCH+MIN_WAITTIME;
    343       1.14   thorpej 	rdisc_timer = next_bcast;
    344       1.14   thorpej 	ifinit_timer.tv_usec = next_bcast.tv_usec;
    345       1.14   thorpej 
    346       1.14   thorpej 	/* Collect an initial view of the world by checking the interface
    347       1.14   thorpej 	 * configuration and the kludge file.
    348       1.14   thorpej 	 */
    349       1.14   thorpej 	gwkludge();
    350       1.14   thorpej 	ifinit();
    351       1.14   thorpej 
    352       1.14   thorpej 	/* Ask for routes */
    353       1.14   thorpej 	rip_query();
    354       1.16  christos 	rdisc_sol();
    355       1.14   thorpej 
    356       1.18   thorpej 	/* Now turn off stdio if not tracing */
    357       1.18   thorpej 	if (new_tracelevel == 0)
    358       1.18   thorpej 		trace_close(background);
    359       1.18   thorpej 
    360       1.14   thorpej 	/* Loop forever, listening and broadcasting.
    361       1.14   thorpej 	 */
    362       1.14   thorpej 	for (;;) {
    363       1.14   thorpej 		prev_clk = clk;
    364       1.14   thorpej 		gettimeofday(&clk, 0);
    365       1.24  christos 		if (prev_clk.tv_sec == clk.tv_sec
    366       1.24  christos 		    && prev_clk.tv_usec == clk.tv_usec+usec_fudge) {
    367       1.24  christos 			/* Much of `routed` depends on time always advancing.
    368       1.24  christos 			 * On systems that do not guarantee that gettimeofday()
    369       1.24  christos 			 * produces unique timestamps even if called within
    370       1.24  christos 			 * a single tick, use trickery like that in classic
    371       1.24  christos 			 * BSD kernels.
    372       1.14   thorpej 			 */
    373       1.24  christos 			clk.tv_usec += ++usec_fudge;
    374       1.24  christos 
    375       1.24  christos 		} else {
    376       1.24  christos 			usec_fudge = 0;
    377       1.24  christos 
    378       1.24  christos 			timevalsub(&t2, &clk, &prev_clk);
    379       1.24  christos 			if (t2.tv_sec < 0
    380       1.24  christos 			    || t2.tv_sec > wtime.tv_sec + 5) {
    381       1.24  christos 				/* Deal with time changes before other
    382       1.24  christos 				 * housekeeping to keep everything straight.
    383       1.24  christos 				 */
    384       1.24  christos 				dt = t2.tv_sec;
    385       1.24  christos 				if (dt > 0)
    386       1.24  christos 					dt -= wtime.tv_sec;
    387       1.24  christos 				trace_act("time changed by %d sec", (int)dt);
    388       1.24  christos 				epoch.tv_sec += dt;
    389       1.24  christos 			}
    390       1.14   thorpej 		}
    391       1.14   thorpej 		timevalsub(&now, &clk, &epoch);
    392       1.14   thorpej 		now_stale = now.tv_sec - STALE_TIME;
    393       1.15  christos 		now_expire = now.tv_sec - EXPIRE_TIME;
    394       1.14   thorpej 		now_garbage = now.tv_sec - GARBAGE_TIME;
    395       1.14   thorpej 
    396       1.16  christos 		/* deal with signals that should affect tracing */
    397       1.14   thorpej 		set_tracelevel();
    398       1.14   thorpej 
    399       1.14   thorpej 		if (stopint != 0) {
    400       1.16  christos 			rip_bcast(0);
    401       1.16  christos 			rdisc_adv();
    402       1.16  christos 			trace_off("exiting with signal %d", stopint);
    403       1.14   thorpej 			exit(stopint | 128);
    404       1.14   thorpej 		}
    405       1.14   thorpej 
    406       1.14   thorpej 		/* look for new or dead interfaces */
    407       1.14   thorpej 		timevalsub(&wtime, &ifinit_timer, &now);
    408       1.14   thorpej 		if (wtime.tv_sec <= 0) {
    409       1.14   thorpej 			wtime.tv_sec = 0;
    410       1.14   thorpej 			ifinit();
    411       1.14   thorpej 			rip_query();
    412        1.1       cgd 			continue;
    413        1.1       cgd 		}
    414       1.14   thorpej 
    415       1.18   thorpej 		/* Check the kernel table occassionally for mysteriously
    416       1.18   thorpej 		 * evaporated routes
    417       1.18   thorpej 		 */
    418       1.18   thorpej 		timevalsub(&t2, &flush_kern_timer, &now);
    419       1.18   thorpej 		if (t2.tv_sec <= 0) {
    420       1.18   thorpej 			flush_kern();
    421       1.18   thorpej 			flush_kern_timer.tv_sec = (now.tv_sec
    422       1.18   thorpej 						   + CHECK_QUIET_INTERVAL);
    423       1.18   thorpej 			continue;
    424       1.18   thorpej 		}
    425       1.18   thorpej 		if (timercmp(&t2, &wtime, <))
    426       1.18   thorpej 			wtime = t2;
    427       1.18   thorpej 
    428       1.14   thorpej 		/* If it is time, then broadcast our routes.
    429       1.14   thorpej 		 */
    430       1.14   thorpej 		if (supplier || advertise_mhome) {
    431       1.14   thorpej 			timevalsub(&t2, &next_bcast, &now);
    432       1.14   thorpej 			if (t2.tv_sec <= 0) {
    433       1.14   thorpej 				/* Synchronize the aging and broadcast
    434       1.14   thorpej 				 * timers to minimize awakenings
    435       1.14   thorpej 				 */
    436       1.14   thorpej 				age(0);
    437       1.14   thorpej 
    438       1.14   thorpej 				rip_bcast(0);
    439       1.14   thorpej 
    440       1.14   thorpej 				/* It is desirable to send routing updates
    441       1.14   thorpej 				 * regularly.  So schedule the next update
    442       1.14   thorpej 				 * 30 seconds after the previous one was
    443       1.20  christos 				 * scheduled, instead of 30 seconds after
    444       1.14   thorpej 				 * the previous update was finished.
    445       1.14   thorpej 				 * Even if we just started after discovering
    446       1.14   thorpej 				 * a 2nd interface or were otherwise delayed,
    447       1.14   thorpej 				 * pick a 30-second aniversary of the
    448       1.14   thorpej 				 * original broadcast time.
    449       1.14   thorpej 				 */
    450       1.14   thorpej 				n = 1 + (0-t2.tv_sec)/SUPPLY_INTERVAL;
    451       1.14   thorpej 				next_bcast.tv_sec += n*SUPPLY_INTERVAL;
    452       1.14   thorpej 
    453       1.14   thorpej 				continue;
    454       1.14   thorpej 			}
    455       1.14   thorpej 
    456       1.14   thorpej 			if (timercmp(&t2, &wtime, <))
    457       1.14   thorpej 				wtime = t2;
    458       1.14   thorpej 		}
    459       1.14   thorpej 
    460       1.14   thorpej 		/* If we need a flash update, either do it now or
    461       1.14   thorpej 		 * set the delay to end when it is time.
    462       1.14   thorpej 		 *
    463       1.14   thorpej 		 * If we are within MIN_WAITTIME seconds of a full update,
    464       1.14   thorpej 		 * do not bother.
    465       1.14   thorpej 		 */
    466       1.14   thorpej 		if (need_flash
    467       1.14   thorpej 		    && supplier
    468       1.14   thorpej 		    && no_flash.tv_sec+MIN_WAITTIME < next_bcast.tv_sec) {
    469       1.14   thorpej 			/* accurate to the millisecond */
    470       1.14   thorpej 			if (!timercmp(&no_flash, &now, >))
    471       1.14   thorpej 				rip_bcast(1);
    472       1.14   thorpej 			timevalsub(&t2, &no_flash, &now);
    473       1.14   thorpej 			if (timercmp(&t2, &wtime, <))
    474       1.14   thorpej 				wtime = t2;
    475       1.14   thorpej 		}
    476       1.14   thorpej 
    477       1.14   thorpej 		/* trigger the main aging timer.
    478       1.14   thorpej 		 */
    479       1.14   thorpej 		timevalsub(&t2, &age_timer, &now);
    480       1.14   thorpej 		if (t2.tv_sec <= 0) {
    481       1.14   thorpej 			age(0);
    482        1.1       cgd 			continue;
    483        1.1       cgd 		}
    484       1.14   thorpej 		if (timercmp(&t2, &wtime, <))
    485       1.14   thorpej 			wtime = t2;
    486       1.14   thorpej 
    487       1.14   thorpej 		/* update the kernel routing table
    488       1.14   thorpej 		 */
    489       1.14   thorpej 		timevalsub(&t2, &need_kern, &now);
    490       1.14   thorpej 		if (t2.tv_sec <= 0) {
    491       1.14   thorpej 			age(0);
    492        1.1       cgd 			continue;
    493        1.1       cgd 		}
    494       1.14   thorpej 		if (timercmp(&t2, &wtime, <))
    495       1.14   thorpej 			wtime = t2;
    496       1.14   thorpej 
    497       1.14   thorpej 		/* take care of router discovery,
    498       1.18   thorpej 		 * but do it in the correct the millisecond
    499       1.14   thorpej 		 */
    500       1.14   thorpej 		if (!timercmp(&rdisc_timer, &now, >)) {
    501       1.14   thorpej 			rdisc_age(0);
    502        1.1       cgd 			continue;
    503        1.1       cgd 		}
    504       1.14   thorpej 		timevalsub(&t2, &rdisc_timer, &now);
    505       1.14   thorpej 		if (timercmp(&t2, &wtime, <))
    506       1.14   thorpej 			wtime = t2;
    507       1.14   thorpej 
    508       1.14   thorpej 
    509       1.14   thorpej 		/* wait for input or a timer to expire.
    510       1.14   thorpej 		 */
    511       1.14   thorpej 		trace_flush();
    512       1.33    itojun 		if (ibitsp)
    513       1.33    itojun 			free(ibitsp);
    514       1.33    itojun 		ibitsp = (fd_set *)calloc(howmany(sock_max, NFDBITS),
    515       1.33    itojun 		    sizeof(fd_mask));
    516       1.33    itojun 		if (ibitsp == NULL)
    517       1.33    itojun 			BADERR(1, "calloc");
    518       1.33    itojun 		memcpy(ibitsp, fdbitsp, howmany(sock_max, NFDBITS) *
    519       1.33    itojun 		    sizeof(fd_mask));
    520       1.33    itojun 		n = select(sock_max, ibitsp, 0, 0, &wtime);
    521       1.14   thorpej 		if (n <= 0) {
    522       1.14   thorpej 			if (n < 0 && errno != EINTR && errno != EAGAIN)
    523       1.14   thorpej 				BADERR(1,"select");
    524        1.1       cgd 			continue;
    525        1.1       cgd 		}
    526       1.14   thorpej 
    527       1.33    itojun 		if (FD_ISSET(rt_sock, ibitsp)) {
    528       1.14   thorpej 			read_rt();
    529       1.14   thorpej 			n--;
    530       1.14   thorpej 		}
    531       1.33    itojun 		if (rdisc_sock >= 0 && FD_ISSET(rdisc_sock, ibitsp)) {
    532       1.14   thorpej 			read_d();
    533       1.14   thorpej 			n--;
    534       1.14   thorpej 		}
    535       1.33    itojun 		if (rip_sock >= 0 && FD_ISSET(rip_sock, ibitsp)) {
    536       1.14   thorpej 			read_rip(rip_sock, 0);
    537       1.14   thorpej 			n--;
    538       1.14   thorpej 		}
    539       1.14   thorpej 
    540       1.14   thorpej 		for (ifp = ifnet; n > 0 && 0 != ifp; ifp = ifp->int_next) {
    541       1.14   thorpej 			if (ifp->int_rip_sock >= 0
    542       1.33    itojun 			    && FD_ISSET(ifp->int_rip_sock, ibitsp)) {
    543       1.14   thorpej 				read_rip(ifp->int_rip_sock, ifp);
    544       1.14   thorpej 				n--;
    545       1.14   thorpej 			}
    546       1.14   thorpej 		}
    547        1.1       cgd 	}
    548       1.14   thorpej }
    549        1.1       cgd 
    550       1.14   thorpej 
    551       1.14   thorpej /* ARGSUSED */
    552       1.14   thorpej void
    553       1.21  christos sigalrm(int s UNUSED)
    554       1.14   thorpej {
    555       1.14   thorpej 	/* Historically, SIGALRM would cause the daemon to check for
    556       1.14   thorpej 	 * new and broken interfaces.
    557        1.1       cgd 	 */
    558       1.14   thorpej 	ifinit_timer.tv_sec = now.tv_sec;
    559       1.16  christos 	trace_act("SIGALRM");
    560       1.14   thorpej }
    561       1.14   thorpej 
    562       1.14   thorpej 
    563       1.14   thorpej /* watch for fatal signals */
    564       1.14   thorpej void
    565       1.14   thorpej sigterm(int sig)
    566       1.14   thorpej {
    567       1.14   thorpej 	stopint = sig;
    568       1.14   thorpej 	(void)signal(sig, SIG_DFL);	/* catch it only once */
    569       1.14   thorpej }
    570       1.14   thorpej 
    571       1.14   thorpej 
    572       1.14   thorpej void
    573       1.14   thorpej fix_select(void)
    574       1.14   thorpej {
    575       1.14   thorpej 	struct interface *ifp;
    576       1.14   thorpej 
    577       1.14   thorpej 	sock_max = 0;
    578       1.14   thorpej 
    579       1.14   thorpej 	if (sock_max <= rt_sock)
    580       1.33    itojun 		sock_max = rt_sock + 1;
    581       1.33    itojun 	if (rip_sock >= 0)
    582       1.14   thorpej 		if (sock_max <= rip_sock)
    583       1.33    itojun 			sock_max = rip_sock + 1;
    584       1.14   thorpej 	for (ifp = ifnet; 0 != ifp; ifp = ifp->int_next) {
    585       1.33    itojun 		if (ifp->int_rip_sock >= 0)
    586       1.14   thorpej 			if (sock_max <= ifp->int_rip_sock)
    587       1.33    itojun 				sock_max = ifp->int_rip_sock + 1;
    588       1.14   thorpej 	}
    589       1.33    itojun 	if (rdisc_sock >= 0)
    590       1.14   thorpej 		if (sock_max <= rdisc_sock)
    591       1.33    itojun 			sock_max = rdisc_sock + 1;
    592       1.33    itojun 
    593       1.33    itojun 	if (fdbitsp)
    594       1.33    itojun 		free(fdbitsp);
    595       1.33    itojun 	fdbitsp = (fd_set *)calloc(howmany(sock_max, NFDBITS),
    596       1.33    itojun 	    sizeof(fd_mask));
    597       1.33    itojun 	if (fdbitsp == NULL)
    598       1.33    itojun 		BADERR(1, "calloc");
    599       1.33    itojun 
    600       1.33    itojun 	FD_SET(rt_sock, fdbitsp);
    601       1.33    itojun 	if (rip_sock >= 0)
    602       1.33    itojun 		FD_SET(rip_sock, fdbitsp);
    603       1.33    itojun 	for (ifp = ifnet; 0 != ifp; ifp = ifp->int_next) {
    604       1.33    itojun 		if (ifp->int_rip_sock >= 0)
    605       1.33    itojun 			FD_SET(ifp->int_rip_sock, fdbitsp);
    606       1.14   thorpej 	}
    607       1.33    itojun 	if (rdisc_sock >= 0)
    608       1.33    itojun 		FD_SET(rdisc_sock, fdbitsp);
    609       1.14   thorpej }
    610       1.14   thorpej 
    611       1.14   thorpej 
    612       1.14   thorpej void
    613       1.14   thorpej fix_sock(int sock,
    614       1.21  christos 	 const char *name)
    615       1.14   thorpej {
    616       1.14   thorpej 	int on;
    617       1.14   thorpej #define MIN_SOCKBUF (4*1024)
    618       1.14   thorpej 	static int rbuf;
    619        1.1       cgd 
    620       1.14   thorpej 	if (fcntl(sock, F_SETFL, O_NONBLOCK) == -1)
    621       1.14   thorpej 		logbad(1, "fcntl(%s) O_NONBLOCK: %s",
    622       1.14   thorpej 		       name, strerror(errno));
    623       1.14   thorpej 	on = 1;
    624       1.16  christos 	if (setsockopt(sock, SOL_SOCKET,SO_BROADCAST, &on,sizeof(on)) < 0)
    625       1.14   thorpej 		msglog("setsockopt(%s,SO_BROADCAST): %s",
    626       1.14   thorpej 		       name, strerror(errno));
    627       1.16  christos #ifdef USE_PASSIFNAME
    628       1.16  christos 	on = 1;
    629       1.16  christos 	if (setsockopt(sock, SOL_SOCKET, SO_PASSIFNAME, &on,sizeof(on)) < 0)
    630       1.16  christos 		msglog("setsockopt(%s,SO_PASSIFNAME): %s",
    631       1.16  christos 		       name, strerror(errno));
    632       1.16  christos #endif
    633       1.16  christos 
    634       1.14   thorpej 	if (rbuf >= MIN_SOCKBUF) {
    635       1.14   thorpej 		if (setsockopt(sock, SOL_SOCKET, SO_RCVBUF,
    636       1.14   thorpej 			       &rbuf, sizeof(rbuf)) < 0)
    637       1.14   thorpej 			msglog("setsockopt(%s,SO_RCVBUF=%d): %s",
    638       1.14   thorpej 			       name, rbuf, strerror(errno));
    639       1.14   thorpej 	} else {
    640       1.14   thorpej 		for (rbuf = 60*1024; ; rbuf -= 4096) {
    641       1.14   thorpej 			if (setsockopt(sock, SOL_SOCKET, SO_RCVBUF,
    642       1.14   thorpej 				       &rbuf, sizeof(rbuf)) == 0) {
    643       1.16  christos 				trace_act("RCVBUF=%d", rbuf);
    644       1.14   thorpej 				break;
    645        1.1       cgd 			}
    646       1.14   thorpej 			if (rbuf < MIN_SOCKBUF) {
    647       1.14   thorpej 				msglog("setsockopt(%s,SO_RCVBUF = %d): %s",
    648       1.14   thorpej 				       name, rbuf, strerror(errno));
    649       1.14   thorpej 				break;
    650        1.1       cgd 			}
    651       1.14   thorpej 		}
    652       1.14   thorpej 	}
    653       1.14   thorpej }
    654       1.14   thorpej 
    655       1.14   thorpej 
    656       1.14   thorpej /* get a rip socket
    657       1.14   thorpej  */
    658       1.14   thorpej static int				/* <0 or file descriptor */
    659       1.14   thorpej get_rip_sock(naddr addr,
    660       1.14   thorpej 	     int serious)		/* 1=failure to bind is serious */
    661       1.14   thorpej {
    662       1.30     lukem 	struct sockaddr_in rsin;
    663       1.14   thorpej 	unsigned char ttl;
    664       1.14   thorpej 	int s;
    665       1.14   thorpej 
    666       1.14   thorpej 
    667       1.14   thorpej 	if ((s = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
    668       1.14   thorpej 		BADERR(1,"rip_sock = socket()");
    669       1.14   thorpej 
    670       1.30     lukem 	memset(&rsin, 0, sizeof(rsin));
    671       1.14   thorpej #ifdef _HAVE_SIN_LEN
    672       1.30     lukem 	rsin.sin_len = sizeof(rsin);
    673       1.14   thorpej #endif
    674       1.30     lukem 	rsin.sin_family = AF_INET;
    675       1.30     lukem 	rsin.sin_port = htons(RIP_PORT);
    676       1.30     lukem 	rsin.sin_addr.s_addr = addr;
    677       1.30     lukem 	if (bind(s, (struct sockaddr *)&rsin, sizeof(rsin)) < 0) {
    678       1.14   thorpej 		if (serious)
    679       1.14   thorpej 			BADERR(errno != EADDRINUSE, "bind(rip_sock)");
    680       1.14   thorpej 		return -1;
    681       1.14   thorpej 	}
    682       1.14   thorpej 	fix_sock(s,"rip_sock");
    683       1.14   thorpej 
    684       1.14   thorpej 	ttl = 1;
    685       1.14   thorpej 	if (setsockopt(s, IPPROTO_IP, IP_MULTICAST_TTL,
    686       1.14   thorpej 		       &ttl, sizeof(ttl)) < 0)
    687       1.14   thorpej 		DBGERR(1,"rip_sock setsockopt(IP_MULTICAST_TTL)");
    688       1.14   thorpej 
    689       1.14   thorpej 	return s;
    690       1.14   thorpej }
    691       1.14   thorpej 
    692       1.14   thorpej 
    693       1.14   thorpej /* turn off main RIP socket */
    694       1.14   thorpej void
    695       1.14   thorpej rip_off(void)
    696       1.14   thorpej {
    697       1.14   thorpej 	struct interface *ifp;
    698       1.17     lukem 	naddr addr;
    699       1.14   thorpej 
    700       1.14   thorpej 
    701       1.14   thorpej 	if (rip_sock >= 0 && !mhome) {
    702       1.16  christos 		trace_act("turn off RIP");
    703       1.14   thorpej 
    704       1.14   thorpej 		(void)close(rip_sock);
    705       1.14   thorpej 		rip_sock = -1;
    706       1.14   thorpej 
    707       1.14   thorpej 		/* get non-broadcast sockets to listen to queries.
    708       1.14   thorpej 		 */
    709       1.14   thorpej 		for (ifp = ifnet; ifp != 0; ifp = ifp->int_next) {
    710       1.16  christos 			if (ifp->int_state & IS_REMOTE)
    711       1.16  christos 				continue;
    712       1.16  christos 			if (ifp->int_rip_sock < 0) {
    713       1.14   thorpej 				addr = ((ifp->int_if_flags & IFF_POINTOPOINT)
    714       1.14   thorpej 					? ifp->int_dstaddr
    715       1.14   thorpej 					: ifp->int_addr);
    716       1.14   thorpej 				ifp->int_rip_sock = get_rip_sock(addr, 0);
    717        1.1       cgd 			}
    718        1.1       cgd 		}
    719       1.14   thorpej 
    720       1.14   thorpej 		fix_select();
    721       1.14   thorpej 
    722       1.14   thorpej 		age(0);
    723       1.14   thorpej 	}
    724       1.14   thorpej }
    725       1.14   thorpej 
    726       1.14   thorpej 
    727       1.14   thorpej /* turn on RIP multicast input via an interface
    728       1.14   thorpej  */
    729       1.14   thorpej static void
    730       1.14   thorpej rip_mcast_on(struct interface *ifp)
    731       1.14   thorpej {
    732       1.14   thorpej 	struct ip_mreq m;
    733       1.14   thorpej 
    734       1.14   thorpej 	if (!IS_RIP_IN_OFF(ifp->int_state)
    735       1.14   thorpej 	    && (ifp->int_if_flags & IFF_MULTICAST)
    736       1.14   thorpej #ifdef MCAST_PPP_BUG
    737       1.14   thorpej 	    && !(ifp->int_if_flags & IFF_POINTOPOINT)
    738        1.1       cgd #endif
    739       1.14   thorpej 	    && !(ifp->int_state & IS_ALIAS)) {
    740       1.14   thorpej 		m.imr_multiaddr.s_addr = htonl(INADDR_RIP_GROUP);
    741       1.27    itojun #ifdef MCAST_IFINDEX
    742       1.27    itojun 		m.imr_interface.s_addr = htonl(ifp->int_index);
    743       1.27    itojun #else
    744       1.14   thorpej 		m.imr_interface.s_addr = ((ifp->int_if_flags & IFF_POINTOPOINT)
    745       1.14   thorpej 					  ? ifp->int_dstaddr
    746       1.14   thorpej 					  : ifp->int_addr);
    747       1.27    itojun #endif
    748       1.14   thorpej 		if (setsockopt(rip_sock,IPPROTO_IP, IP_ADD_MEMBERSHIP,
    749       1.14   thorpej 			       &m, sizeof(m)) < 0)
    750       1.14   thorpej 			LOGERR("setsockopt(IP_ADD_MEMBERSHIP RIP)");
    751        1.1       cgd 	}
    752        1.1       cgd }
    753        1.1       cgd 
    754       1.14   thorpej 
    755       1.14   thorpej /* Prepare socket used for RIP.
    756       1.14   thorpej  */
    757        1.6       cgd void
    758       1.14   thorpej rip_on(struct interface *ifp)
    759        1.1       cgd {
    760       1.14   thorpej 	/* If the main RIP socket is already alive, only start receiving
    761       1.14   thorpej 	 * multicasts for this interface.
    762       1.14   thorpej 	 */
    763       1.14   thorpej 	if (rip_sock >= 0) {
    764       1.14   thorpej 		if (ifp != 0)
    765       1.14   thorpej 			rip_mcast_on(ifp);
    766       1.14   thorpej 		return;
    767       1.14   thorpej 	}
    768       1.14   thorpej 
    769       1.16  christos 	/* If the main RIP socket is off and it makes sense to turn it on,
    770       1.16  christos 	 * then turn it on for all of the interfaces.
    771       1.18   thorpej 	 * It makes sense if either router discovery is off, or if
    772       1.18   thorpej 	 * router discover is on and at most one interface is doing RIP.
    773       1.14   thorpej 	 */
    774       1.18   thorpej 	if (rip_interfaces > 0 && (!rdisc_ok || rip_interfaces > 1)) {
    775       1.16  christos 		trace_act("turn on RIP");
    776       1.14   thorpej 
    777       1.14   thorpej 		/* Close all of the query sockets so that we can open
    778       1.14   thorpej 		 * the main socket.  SO_REUSEPORT is not a solution,
    779       1.14   thorpej 		 * since that would let two daemons bind to the broadcast
    780       1.14   thorpej 		 * socket.
    781       1.14   thorpej 		 */
    782       1.14   thorpej 		for (ifp = ifnet; ifp != 0; ifp = ifp->int_next) {
    783       1.14   thorpej 			if (ifp->int_rip_sock >= 0) {
    784       1.14   thorpej 				(void)close(ifp->int_rip_sock);
    785       1.14   thorpej 				ifp->int_rip_sock = -1;
    786       1.14   thorpej 			}
    787       1.14   thorpej 		}
    788       1.14   thorpej 
    789       1.14   thorpej 		rip_sock = get_rip_sock(INADDR_ANY, 1);
    790       1.14   thorpej 		rip_sock_mcast = 0;
    791       1.14   thorpej 
    792       1.14   thorpej 		/* Do not advertise anything until we have heard something
    793       1.14   thorpej 		 */
    794       1.14   thorpej 		if (next_bcast.tv_sec < now.tv_sec+MIN_WAITTIME)
    795       1.14   thorpej 			next_bcast.tv_sec = now.tv_sec+MIN_WAITTIME;
    796        1.1       cgd 
    797       1.14   thorpej 		for (ifp = ifnet; ifp != 0; ifp = ifp->int_next) {
    798       1.16  christos 			ifp->int_query_time = NEVER;
    799       1.14   thorpej 			rip_mcast_on(ifp);
    800        1.1       cgd 		}
    801       1.14   thorpej 		ifinit_timer.tv_sec = now.tv_sec;
    802       1.14   thorpej 
    803       1.14   thorpej 	} else if (ifp != 0
    804       1.16  christos 		   && !(ifp->int_state & IS_REMOTE)
    805       1.16  christos 		   && ifp->int_rip_sock < 0) {
    806       1.14   thorpej 		/* RIP is off, so ensure there are sockets on which
    807       1.14   thorpej 		 * to listen for queries.
    808       1.14   thorpej 		 */
    809       1.14   thorpej 		ifp->int_rip_sock = get_rip_sock(ifp->int_addr, 0);
    810       1.16  christos 	}
    811       1.14   thorpej 
    812       1.16  christos 	fix_select();
    813       1.15  christos }
    814       1.15  christos 
    815       1.15  christos 
    816       1.15  christos /* die if malloc(3) fails
    817       1.15  christos  */
    818       1.15  christos void *
    819       1.15  christos rtmalloc(size_t size,
    820       1.21  christos 	 const char *msg)
    821       1.15  christos {
    822       1.15  christos 	void *p = malloc(size);
    823       1.15  christos 	if (p == 0)
    824       1.24  christos 		logbad(1,"malloc(%lu) failed in %s", (u_long)size, msg);
    825       1.15  christos 	return p;
    826        1.1       cgd }
    827        1.1       cgd 
    828       1.14   thorpej 
    829       1.14   thorpej /* get a random instant in an interval
    830       1.14   thorpej  */
    831       1.14   thorpej void
    832       1.14   thorpej intvl_random(struct timeval *tp,	/* put value here */
    833       1.14   thorpej 	     u_long lo,			/* value is after this second */
    834       1.14   thorpej 	     u_long hi)			/* and before this */
    835       1.14   thorpej {
    836       1.14   thorpej 	tp->tv_sec = (time_t)(hi == lo
    837       1.14   thorpej 			      ? lo
    838       1.34    itojun 			      : (lo + arc4random() % ((hi - lo))));
    839       1.34    itojun 	tp->tv_usec = arc4random() % 1000000;
    840       1.14   thorpej }
    841       1.14   thorpej 
    842       1.14   thorpej 
    843       1.14   thorpej void
    844       1.14   thorpej timevaladd(struct timeval *t1,
    845       1.14   thorpej 	   struct timeval *t2)
    846       1.14   thorpej {
    847       1.14   thorpej 
    848       1.14   thorpej 	t1->tv_sec += t2->tv_sec;
    849       1.20  christos 	if ((t1->tv_usec += t2->tv_usec) >= 1000000) {
    850       1.14   thorpej 		t1->tv_sec++;
    851       1.14   thorpej 		t1->tv_usec -= 1000000;
    852        1.1       cgd 	}
    853       1.14   thorpej }
    854       1.14   thorpej 
    855       1.14   thorpej 
    856       1.14   thorpej /* t1 = t2 - t3
    857       1.14   thorpej  */
    858       1.14   thorpej static void
    859       1.14   thorpej timevalsub(struct timeval *t1,
    860       1.14   thorpej 	   struct timeval *t2,
    861       1.14   thorpej 	   struct timeval *t3)
    862       1.14   thorpej {
    863       1.14   thorpej 	t1->tv_sec = t2->tv_sec - t3->tv_sec;
    864       1.14   thorpej 	if ((t1->tv_usec = t2->tv_usec - t3->tv_usec) < 0) {
    865       1.14   thorpej 		t1->tv_sec--;
    866       1.14   thorpej 		t1->tv_usec += 1000000;
    867        1.1       cgd 	}
    868       1.14   thorpej }
    869       1.14   thorpej 
    870       1.14   thorpej 
    871       1.16  christos /* put a message into the system log
    872       1.16  christos  */
    873       1.14   thorpej void
    874       1.21  christos msglog(const char *p, ...)
    875       1.14   thorpej {
    876       1.14   thorpej 	va_list args;
    877       1.14   thorpej 
    878       1.14   thorpej 	trace_flush();
    879       1.14   thorpej 
    880       1.14   thorpej 	va_start(args, p);
    881       1.14   thorpej 	vsyslog(LOG_ERR, p, args);
    882       1.29       wiz 	va_end(args);
    883       1.14   thorpej 
    884       1.14   thorpej 	if (ftrace != 0) {
    885       1.14   thorpej 		if (ftrace == stdout)
    886       1.14   thorpej 			(void)fputs("routed: ", ftrace);
    887       1.29       wiz 		va_start(args, p);
    888       1.16  christos 		(void)vfprintf(ftrace, p, args);
    889       1.29       wiz 		va_end(args);
    890       1.16  christos 		(void)fputc('\n', ftrace);
    891       1.16  christos 	}
    892       1.16  christos }
    893       1.16  christos 
    894       1.16  christos 
    895       1.16  christos /* Put a message about a bad system into the system log if
    896       1.16  christos  * we have not complained about it recently.
    897       1.16  christos  *
    898       1.16  christos  * It is desirable to complain about all bad systems, but not too often.
    899       1.16  christos  * In the worst case, it is not practical to keep track of all bad systems.
    900       1.16  christos  * For example, there can be many systems with the wrong password.
    901       1.16  christos  */
    902       1.16  christos void
    903       1.21  christos msglim(struct msg_limit *lim, naddr addr, const char *p, ...)
    904       1.16  christos {
    905       1.16  christos 	va_list args;
    906       1.16  christos 	int i;
    907       1.16  christos 	struct msg_sub *ms1, *ms;
    908       1.21  christos 	const char *p1;
    909       1.16  christos 
    910       1.16  christos 	/* look for the oldest slot in the table
    911       1.16  christos 	 * or the slot for the bad router.
    912       1.16  christos 	 */
    913       1.16  christos 	ms = ms1 = lim->subs;
    914       1.16  christos 	for (i = MSG_SUBJECT_N; ; i--, ms1++) {
    915       1.16  christos 		if (i == 0) {
    916       1.16  christos 			/* Reuse a slot at most once every 10 minutes.
    917       1.16  christos 			 */
    918       1.16  christos 			if (lim->reuse > now.tv_sec) {
    919       1.16  christos 				ms = 0;
    920       1.16  christos 			} else {
    921       1.16  christos 				ms = ms1;
    922       1.16  christos 				lim->reuse = now.tv_sec + 10*60;
    923       1.16  christos 			}
    924       1.16  christos 			break;
    925       1.16  christos 		}
    926       1.16  christos 		if (ms->addr == addr) {
    927       1.16  christos 			/* Repeat a complaint about a given system at
    928       1.16  christos 			 * most once an hour.
    929       1.16  christos 			 */
    930       1.16  christos 			if (ms->until > now.tv_sec)
    931       1.16  christos 				ms = 0;
    932       1.16  christos 			break;
    933       1.16  christos 		}
    934       1.16  christos 		if (ms->until < ms1->until)
    935       1.16  christos 			ms = ms1;
    936       1.16  christos 	}
    937       1.16  christos 	if (ms != 0) {
    938       1.16  christos 		ms->addr = addr;
    939       1.16  christos 		ms->until = now.tv_sec + 60*60;	/* 60 minutes */
    940       1.16  christos 
    941       1.16  christos 		trace_flush();
    942       1.16  christos 		for (p1 = p; *p1 == ' '; p1++)
    943       1.16  christos 			continue;
    944       1.29       wiz 		va_start(args, p);
    945       1.29       wiz 		vsyslog(LOG_ERR, p1, args);
    946       1.29       wiz 		va_end(args);
    947       1.16  christos 	}
    948       1.16  christos 
    949       1.16  christos 	/* always display the message if tracing */
    950       1.16  christos 	if (ftrace != 0) {
    951       1.29       wiz 		va_start(args, p);
    952       1.14   thorpej 		(void)vfprintf(ftrace, p, args);
    953       1.14   thorpej 		(void)fputc('\n', ftrace);
    954       1.29       wiz 		va_end(args);
    955        1.1       cgd 	}
    956       1.14   thorpej }
    957       1.14   thorpej 
    958       1.14   thorpej 
    959       1.14   thorpej void
    960       1.21  christos logbad(int dump, const char *p, ...)
    961       1.14   thorpej {
    962       1.14   thorpej 	va_list args;
    963       1.14   thorpej 
    964       1.14   thorpej 	trace_flush();
    965       1.14   thorpej 
    966       1.14   thorpej 	va_start(args, p);
    967       1.14   thorpej 	vsyslog(LOG_ERR, p, args);
    968       1.29       wiz 	va_end(args);
    969       1.14   thorpej 
    970       1.14   thorpej 	(void)fputs("routed: ", stderr);
    971       1.29       wiz 	va_start(args, p);
    972       1.14   thorpej 	(void)vfprintf(stderr, p, args);
    973       1.29       wiz 	va_end(args);
    974       1.14   thorpej 	(void)fputs("; giving up\n",stderr);
    975       1.14   thorpej 	(void)fflush(stderr);
    976       1.14   thorpej 
    977       1.14   thorpej 	if (dump)
    978       1.14   thorpej 		abort();
    979       1.14   thorpej 	exit(1);
    980        1.1       cgd }
    981