Home | History | Annotate | Line # | Download | only in routed
main.c revision 1.15
      1  1.15  christos /*	$NetBSD: main.c,v 1.15 1996/09/24 16:24:15 christos 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.1       cgd  *    must display the following acknowledgement:
     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.14   thorpej char copyright[] =
     37   1.5   mycroft "@(#) Copyright (c) 1983, 1988, 1993\n\
     38   1.5   mycroft 	The Regents of the University of California.  All rights reserved.\n";
     39  1.15  christos #if !defined(lint) && !defined(sgi) && !defined(__NetBSD__)
     40   1.7       cgd static char sccsid[] = "@(#)main.c	8.1 (Berkeley) 6/5/93";
     41  1.15  christos #elif defined(__NetBSD__)
     42  1.15  christos static char rcsid[] = "$NetBSD: main.c,v 1.15 1996/09/24 16:24:15 christos Exp $";
     43   1.7       cgd #endif
     44   1.1       cgd 
     45   1.1       cgd #include "defs.h"
     46  1.14   thorpej #include "pathnames.h"
     47  1.14   thorpej #ifdef sgi
     48  1.14   thorpej #include "math.h"
     49  1.14   thorpej #endif
     50  1.14   thorpej #include <signal.h>
     51  1.14   thorpej #include <fcntl.h>
     52   1.1       cgd #include <sys/file.h>
     53   1.1       cgd 
     54  1.14   thorpej pid_t	mypid;
     55  1.14   thorpej 
     56  1.14   thorpej naddr	myaddr;				/* system address */
     57  1.14   thorpej char	myname[MAXHOSTNAMELEN+1];
     58   1.1       cgd 
     59  1.14   thorpej int	supplier;			/* supply or broadcast updates */
     60  1.14   thorpej int	supplier_set;
     61  1.14   thorpej int	ipforwarding = 1;		/* kernel forwarding on */
     62  1.14   thorpej 
     63  1.14   thorpej int	default_gateway;		/* 1=advertise default */
     64  1.14   thorpej int	background = 1;
     65  1.14   thorpej int	ridhosts;			/* 1=reduce host routes */
     66  1.14   thorpej int	mhome;				/* 1=want multi-homed host route */
     67  1.14   thorpej int	advertise_mhome;		/* 1=must continue adverising it */
     68  1.14   thorpej int	auth_ok = 1;			/* 1=ignore auth if we do not care */
     69  1.14   thorpej 
     70  1.14   thorpej struct timeval epoch;			/* when started */
     71  1.14   thorpej struct timeval clk, prev_clk;
     72  1.14   thorpej struct timeval now;			/* current idea of time */
     73  1.14   thorpej time_t	now_stale;
     74  1.15  christos time_t	now_expire;
     75  1.14   thorpej time_t	now_garbage;
     76  1.14   thorpej 
     77  1.14   thorpej struct timeval next_bcast;		/* next general broadcast */
     78  1.14   thorpej struct timeval no_flash = {EPOCH+SUPPLY_INTERVAL};  /* inhibit flash update */
     79  1.14   thorpej 
     80  1.14   thorpej fd_set	fdbits;
     81  1.14   thorpej int	sock_max;
     82  1.14   thorpej int	rip_sock = -1;			/* RIP socket */
     83  1.14   thorpej struct interface *rip_sock_mcast;	/* current multicast interface */
     84  1.14   thorpej int	rt_sock;			/* routing socket */
     85  1.14   thorpej int	rt_sock_seqno;
     86   1.1       cgd 
     87   1.1       cgd 
     88  1.14   thorpej static  int get_rip_sock(naddr, int);
     89  1.14   thorpej static void timevalsub(struct timeval *, struct timeval *, struct timeval *);
     90   1.1       cgd 
     91   1.6       cgd int
     92  1.14   thorpej main(int argc,
     93  1.14   thorpej      char *argv[])
     94  1.14   thorpej {
     95  1.14   thorpej 	int n, mib[4], off;
     96  1.14   thorpej 	size_t len;
     97  1.14   thorpej 	char *p, *q;
     98  1.14   thorpej 	struct timeval wtime, t2;
     99  1.14   thorpej 	time_t dt;
    100   1.1       cgd 	fd_set ibits;
    101  1.14   thorpej 	naddr p_addr, p_mask;
    102  1.14   thorpej 	struct interface *ifp;
    103  1.14   thorpej 	struct parm parm;
    104  1.14   thorpej 	char *tracename = 0;
    105  1.14   thorpej 
    106  1.14   thorpej 
    107   1.1       cgd 	openlog("routed", LOG_PID | LOG_ODELAY, LOG_DAEMON);
    108  1.14   thorpej 	ftrace = stdout;
    109  1.14   thorpej 
    110  1.14   thorpej 	gettimeofday(&clk, 0);
    111  1.14   thorpej 	prev_clk = clk;
    112  1.14   thorpej 	epoch = clk;
    113  1.14   thorpej 	epoch.tv_sec -= EPOCH;
    114  1.14   thorpej 	now.tv_sec = EPOCH;
    115  1.14   thorpej 	now_stale = EPOCH - STALE_TIME;
    116  1.15  christos 	now_expire = EPOCH - EXPIRE_TIME;
    117  1.14   thorpej 	now_garbage = EPOCH - GARBAGE_TIME;
    118  1.14   thorpej 	wtime.tv_sec = 0;
    119  1.14   thorpej 
    120  1.14   thorpej 	(void)gethostname(myname, sizeof(myname)-1);
    121  1.14   thorpej 	(void)gethost(myname, &myaddr);
    122  1.14   thorpej 
    123  1.14   thorpej 	while ((n = getopt(argc, argv, "sqdghmpAtT:F:P:")) != EOF) {
    124  1.14   thorpej 		switch (n) {
    125  1.14   thorpej 		case 's':
    126  1.14   thorpej 			supplier = 1;
    127  1.14   thorpej 			supplier_set = 1;
    128  1.14   thorpej 			break;
    129  1.14   thorpej 
    130  1.14   thorpej 		case 'q':
    131  1.14   thorpej 			supplier = 0;
    132  1.14   thorpej 			supplier_set = 1;
    133  1.14   thorpej 			break;
    134  1.14   thorpej 
    135  1.14   thorpej 		case 'd':
    136  1.14   thorpej 			background = 0;
    137  1.14   thorpej 			break;
    138  1.14   thorpej 
    139  1.14   thorpej 		case 'g':
    140  1.14   thorpej 			bzero(&parm, sizeof(parm));
    141  1.14   thorpej 			parm.parm_d_metric = 1;
    142  1.14   thorpej 			p = check_parms(&parm);
    143  1.14   thorpej 			if (p != 0)
    144  1.14   thorpej 				msglog("bad -g: %s", p);
    145  1.14   thorpej 			else
    146  1.14   thorpej 				default_gateway = 1;
    147  1.14   thorpej 			break;
    148  1.14   thorpej 
    149  1.14   thorpej 		case 'h':		/* suppress extra host routes */
    150  1.14   thorpej 			ridhosts = 1;
    151  1.14   thorpej 			break;
    152  1.14   thorpej 
    153  1.14   thorpej 		case 'm':		/* advertise host route */
    154  1.14   thorpej 			mhome = 1;	/* on multi-homed hosts */
    155  1.14   thorpej 			break;
    156  1.14   thorpej 
    157  1.14   thorpej 		case 'A':
    158  1.14   thorpej 			/* Ignore authentication if we do not care.
    159  1.14   thorpej 			 * Crazy as it is, that is what RFC 1723 requires.
    160  1.14   thorpej 			 */
    161  1.14   thorpej 			auth_ok = 0;
    162  1.14   thorpej 			break;
    163  1.14   thorpej 
    164  1.14   thorpej 		case 't':
    165  1.14   thorpej 			new_tracelevel++;
    166  1.14   thorpej 			break;
    167  1.14   thorpej 
    168  1.14   thorpej 		case 'T':
    169  1.14   thorpej 			tracename = optarg;
    170  1.14   thorpej 			break;
    171  1.14   thorpej 
    172  1.14   thorpej 		case 'F':		/* minimal routes for SLIP */
    173  1.14   thorpej 			n = HOPCNT_INFINITY-2;
    174  1.14   thorpej 			p = strchr(optarg,',');
    175  1.14   thorpej 			if (p && *p != '\0') {
    176  1.14   thorpej 				n = (int)strtoul(p+1, &q, 0);
    177  1.14   thorpej 				if (*q == '\0'
    178  1.14   thorpej 				    && n <= HOPCNT_INFINITY-1
    179  1.14   thorpej 				    && n >= 1)
    180  1.14   thorpej 					*p = '\0';
    181  1.14   thorpej 			}
    182  1.14   thorpej 			if (!getnet(optarg, &p_addr, &p_mask)) {
    183  1.14   thorpej 				msglog("bad network; \"-F %s\"",
    184  1.14   thorpej 				       optarg);
    185  1.14   thorpej 				break;
    186  1.14   thorpej 			}
    187  1.14   thorpej 			bzero(&parm, sizeof(parm));
    188  1.14   thorpej 			parm.parm_addr_h = ntohl(p_addr);
    189  1.14   thorpej 			parm.parm_mask = p_mask;
    190  1.14   thorpej 			parm.parm_d_metric = n;
    191  1.14   thorpej 			p = check_parms(&parm);
    192  1.14   thorpej 			if (p != 0)
    193  1.14   thorpej 				msglog("bad -F: %s", p);
    194  1.14   thorpej 			break;
    195  1.14   thorpej 
    196  1.14   thorpej 		case 'P':
    197  1.14   thorpej 			/* handle arbirary, (usually) per-interface
    198  1.14   thorpej 			 * parameters.
    199  1.14   thorpej 			 */
    200  1.14   thorpej 			p = parse_parms(optarg);
    201  1.14   thorpej 			if (p != 0)
    202  1.14   thorpej 				msglog("bad \"%s\" in \"%s\"",
    203  1.14   thorpej 				       p, optarg);
    204  1.14   thorpej 			break;
    205  1.14   thorpej 
    206  1.14   thorpej 		default:
    207  1.14   thorpej 			goto usage;
    208  1.14   thorpej 		}
    209  1.14   thorpej 	}
    210  1.14   thorpej 	argc -= optind;
    211  1.14   thorpej 	argv += optind;
    212  1.14   thorpej 
    213  1.14   thorpej 	if (tracename == 0 && argc >= 1) {
    214  1.14   thorpej 		tracename = *argv++;
    215  1.14   thorpej 		argc--;
    216  1.14   thorpej 	}
    217  1.14   thorpej 	if (argc != 0) {
    218  1.14   thorpej usage:
    219  1.14   thorpej 		logbad(0, "usage: routed [-sqdghmpAt] [-T /tracefile]"
    220  1.14   thorpej 		       " [-F net[,metric]] [-P parms]");
    221  1.14   thorpej 	}
    222  1.14   thorpej 	if (geteuid() != 0)
    223  1.14   thorpej 		logbad(0, "requires UID 0");
    224  1.14   thorpej 
    225  1.14   thorpej 	mib[0] = CTL_NET;
    226  1.14   thorpej 	mib[1] = PF_INET;
    227  1.14   thorpej 	mib[2] = IPPROTO_IP;
    228  1.14   thorpej 	mib[3] = IPCTL_FORWARDING;
    229  1.14   thorpej 	len = sizeof(ipforwarding);
    230  1.14   thorpej 	if (sysctl(mib, 4, &ipforwarding, &len, 0, 0) < 0)
    231  1.14   thorpej 		LOGERR("sysctl(IPCTL_FORWARDING)");
    232  1.14   thorpej 
    233  1.14   thorpej 	if (!ipforwarding) {
    234  1.14   thorpej 		if (supplier)
    235  1.14   thorpej 			msglog("-s incompatible with ipforwarding=0");
    236  1.14   thorpej 		if (default_gateway) {
    237  1.14   thorpej 			msglog("-g incompatible with ipforwarding=0");
    238  1.14   thorpej 			default_gateway = 0;
    239  1.14   thorpej 		}
    240  1.14   thorpej 		supplier = 0;
    241  1.14   thorpej 		supplier_set = 1;
    242  1.14   thorpej 	}
    243  1.14   thorpej 	if (default_gateway) {
    244  1.14   thorpej 		if (supplier_set && !supplier) {
    245  1.14   thorpej 			msglog("-g and -q incompatible");
    246  1.14   thorpej 		} else {
    247  1.14   thorpej 			supplier = 1;
    248  1.14   thorpej 			supplier_set = 1;
    249  1.14   thorpej 		}
    250  1.14   thorpej 	}
    251  1.14   thorpej 
    252  1.14   thorpej 
    253  1.15  christos 	signal(SIGALRM, sigalrm);
    254  1.15  christos 	if (!background)
    255  1.15  christos 		signal(SIGHUP, sigterm);    /* SIGHUP fatal during debugging */
    256  1.15  christos 	else
    257  1.15  christos 		signal(SIGHUP, SIG_IGN);
    258  1.15  christos 	signal(SIGTERM, sigterm);
    259  1.15  christos 	signal(SIGINT, sigterm);
    260  1.15  christos 	signal(SIGUSR1, sigtrace_on);
    261  1.15  christos 	signal(SIGUSR2, sigtrace_off);
    262  1.15  christos 
    263  1.14   thorpej 	/* get into the background */
    264  1.14   thorpej 	if (background) {
    265  1.14   thorpej #ifdef sgi
    266  1.14   thorpej 		if (0 > _daemonize(_DF_NOCHDIR,
    267  1.14   thorpej 				   new_tracelevel == 0 ? -1 : STDOUT_FILENO,
    268  1.14   thorpej 				   new_tracelevel == 0 ? -1 : STDERR_FILENO,
    269  1.14   thorpej 				   -1))
    270  1.14   thorpej 			BADERR(0, "_daemonize()");
    271   1.1       cgd #else
    272  1.14   thorpej 		if (daemon(1, 1) < 0)
    273  1.14   thorpej 			BADERR(0,"daemon()");
    274   1.1       cgd #endif
    275  1.14   thorpej 	}
    276  1.14   thorpej 
    277  1.14   thorpej 	mypid = getpid();
    278  1.14   thorpej 	srandom((int)(clk.tv_sec ^ clk.tv_usec ^ mypid));
    279  1.14   thorpej 
    280  1.14   thorpej 	/* prepare socket connected to the kernel.
    281  1.14   thorpej 	 */
    282  1.14   thorpej 	rt_sock = socket(AF_ROUTE, SOCK_RAW, 0);
    283  1.14   thorpej 	if (rt_sock < 0)
    284  1.14   thorpej 		BADERR(1,"rt_sock = socket()");
    285  1.14   thorpej 	if (fcntl(rt_sock, F_SETFL, O_NONBLOCK) == -1)
    286  1.14   thorpej 		logbad(1, "fcntl(rt_sock) O_NONBLOCK: %s", strerror(errno));
    287  1.14   thorpej 	off = 0;
    288  1.14   thorpej 	if (setsockopt(rt_sock, SOL_SOCKET,SO_USELOOPBACK,
    289  1.14   thorpej 		       &off,sizeof(off)) < 0)
    290  1.14   thorpej 		LOGERR("setsockopt(SO_USELOOPBACK,0)");
    291  1.14   thorpej 
    292  1.14   thorpej 	fix_select();
    293  1.14   thorpej 
    294  1.14   thorpej 
    295  1.14   thorpej 	if (background && new_tracelevel == 0)
    296  1.14   thorpej 		ftrace = 0;
    297  1.14   thorpej 	if (tracename != 0) {
    298  1.14   thorpej 		trace_on(tracename, 1);
    299  1.14   thorpej 		if (new_tracelevel == 0)	/* use stdout if file is bad */
    300  1.14   thorpej 			new_tracelevel = 1;
    301  1.14   thorpej 	}
    302  1.14   thorpej 	set_tracelevel();
    303  1.14   thorpej 
    304  1.14   thorpej 	/* initialize radix tree */
    305  1.14   thorpej 	rtinit();
    306  1.14   thorpej 
    307  1.14   thorpej 	/* Pick a random part of the second for our output to minimize
    308  1.14   thorpej 	 * collisions.
    309  1.14   thorpej 	 *
    310  1.14   thorpej 	 * Start broadcasting after hearing from other routers, and
    311  1.14   thorpej 	 * at a random time so a bunch of systems do not get synchronized
    312  1.14   thorpej 	 * after a power failure.
    313  1.14   thorpej 	 */
    314  1.14   thorpej 	intvl_random(&next_bcast, EPOCH+MIN_WAITTIME, EPOCH+SUPPLY_INTERVAL);
    315  1.14   thorpej 	age_timer.tv_usec = next_bcast.tv_usec;
    316  1.14   thorpej 	age_timer.tv_sec = EPOCH+MIN_WAITTIME;
    317  1.14   thorpej 	rdisc_timer = next_bcast;
    318  1.14   thorpej 	ifinit_timer.tv_usec = next_bcast.tv_usec;
    319  1.14   thorpej 
    320  1.14   thorpej 	/* Collect an initial view of the world by checking the interface
    321  1.14   thorpej 	 * configuration and the kludge file.
    322  1.14   thorpej 	 */
    323  1.14   thorpej 	gwkludge();
    324  1.14   thorpej 	ifinit();
    325  1.14   thorpej 	flush_kern();
    326  1.14   thorpej 
    327  1.14   thorpej 	/* Ask for routes */
    328  1.14   thorpej 	rip_query();
    329  1.14   thorpej 	if (!supplier)
    330  1.14   thorpej 		rdisc_sol();
    331  1.14   thorpej 
    332  1.14   thorpej 	/* Loop forever, listening and broadcasting.
    333  1.14   thorpej 	 */
    334  1.14   thorpej 	for (;;) {
    335  1.14   thorpej 		prev_clk = clk;
    336  1.14   thorpej 		gettimeofday(&clk, 0);
    337  1.14   thorpej 		timevalsub(&t2, &clk, &prev_clk);
    338  1.14   thorpej 		if (t2.tv_sec < 0
    339  1.14   thorpej 		    || t2.tv_sec > wtime.tv_sec + 5) {
    340  1.14   thorpej 			/* Deal with time changes before other housekeeping to
    341  1.14   thorpej 			 * keep everything straight.
    342  1.14   thorpej 			 */
    343  1.14   thorpej 			dt = t2.tv_sec;
    344  1.14   thorpej 			if (dt > 0)
    345  1.14   thorpej 				dt -= wtime.tv_sec;
    346  1.14   thorpej 			trace_act("time changed by %d sec\n", dt);
    347  1.14   thorpej 			epoch.tv_sec += dt;
    348  1.14   thorpej 		}
    349  1.14   thorpej 		timevalsub(&now, &clk, &epoch);
    350  1.14   thorpej 		now_stale = now.tv_sec - STALE_TIME;
    351  1.15  christos 		now_expire = now.tv_sec - EXPIRE_TIME;
    352  1.14   thorpej 		now_garbage = now.tv_sec - GARBAGE_TIME;
    353  1.14   thorpej 
    354  1.14   thorpej 		/* deal with interrupts that should affect tracing */
    355  1.14   thorpej 		set_tracelevel();
    356  1.14   thorpej 
    357  1.14   thorpej 		if (stopint != 0) {
    358  1.14   thorpej 			if (supplier) {
    359  1.14   thorpej 				rip_bcast(0);
    360  1.14   thorpej 				rdisc_adv();
    361  1.14   thorpej 			}
    362  1.14   thorpej 			trace_off("exiting with signal %d\n", stopint);
    363  1.14   thorpej 			exit(stopint | 128);
    364  1.14   thorpej 		}
    365  1.14   thorpej 
    366  1.14   thorpej 		/* look for new or dead interfaces */
    367  1.14   thorpej 		timevalsub(&wtime, &ifinit_timer, &now);
    368  1.14   thorpej 		if (wtime.tv_sec <= 0) {
    369  1.14   thorpej 			wtime.tv_sec = 0;
    370  1.14   thorpej 			ifinit();
    371  1.14   thorpej 			rip_query();
    372   1.1       cgd 			continue;
    373   1.1       cgd 		}
    374  1.14   thorpej 
    375  1.14   thorpej 		/* If it is time, then broadcast our routes.
    376  1.14   thorpej 		 */
    377  1.14   thorpej 		if (supplier || advertise_mhome) {
    378  1.14   thorpej 			timevalsub(&t2, &next_bcast, &now);
    379  1.14   thorpej 			if (t2.tv_sec <= 0) {
    380  1.14   thorpej 				/* Synchronize the aging and broadcast
    381  1.14   thorpej 				 * timers to minimize awakenings
    382  1.14   thorpej 				 */
    383  1.14   thorpej 				age(0);
    384  1.14   thorpej 
    385  1.14   thorpej 				rip_bcast(0);
    386  1.14   thorpej 
    387  1.14   thorpej 				/* It is desirable to send routing updates
    388  1.14   thorpej 				 * regularly.  So schedule the next update
    389  1.14   thorpej 				 * 30 seconds after the previous one was
    390  1.14   thorpej 				 * secheduled, instead of 30 seconds after
    391  1.14   thorpej 				 * the previous update was finished.
    392  1.14   thorpej 				 * Even if we just started after discovering
    393  1.14   thorpej 				 * a 2nd interface or were otherwise delayed,
    394  1.14   thorpej 				 * pick a 30-second aniversary of the
    395  1.14   thorpej 				 * original broadcast time.
    396  1.14   thorpej 				 */
    397  1.14   thorpej 				n = 1 + (0-t2.tv_sec)/SUPPLY_INTERVAL;
    398  1.14   thorpej 				next_bcast.tv_sec += n*SUPPLY_INTERVAL;
    399  1.14   thorpej 
    400  1.14   thorpej 				continue;
    401  1.14   thorpej 			}
    402  1.14   thorpej 
    403  1.14   thorpej 			if (timercmp(&t2, &wtime, <))
    404  1.14   thorpej 				wtime = t2;
    405  1.14   thorpej 		}
    406  1.14   thorpej 
    407  1.14   thorpej 		/* If we need a flash update, either do it now or
    408  1.14   thorpej 		 * set the delay to end when it is time.
    409  1.14   thorpej 		 *
    410  1.14   thorpej 		 * If we are within MIN_WAITTIME seconds of a full update,
    411  1.14   thorpej 		 * do not bother.
    412  1.14   thorpej 		 */
    413  1.14   thorpej 		if (need_flash
    414  1.14   thorpej 		    && supplier
    415  1.14   thorpej 		    && no_flash.tv_sec+MIN_WAITTIME < next_bcast.tv_sec) {
    416  1.14   thorpej 			/* accurate to the millisecond */
    417  1.14   thorpej 			if (!timercmp(&no_flash, &now, >))
    418  1.14   thorpej 				rip_bcast(1);
    419  1.14   thorpej 			timevalsub(&t2, &no_flash, &now);
    420  1.14   thorpej 			if (timercmp(&t2, &wtime, <))
    421  1.14   thorpej 				wtime = t2;
    422  1.14   thorpej 		}
    423  1.14   thorpej 
    424  1.14   thorpej 		/* trigger the main aging timer.
    425  1.14   thorpej 		 */
    426  1.14   thorpej 		timevalsub(&t2, &age_timer, &now);
    427  1.14   thorpej 		if (t2.tv_sec <= 0) {
    428  1.14   thorpej 			age(0);
    429   1.1       cgd 			continue;
    430   1.1       cgd 		}
    431  1.14   thorpej 		if (timercmp(&t2, &wtime, <))
    432  1.14   thorpej 			wtime = t2;
    433  1.14   thorpej 
    434  1.14   thorpej 		/* update the kernel routing table
    435  1.14   thorpej 		 */
    436  1.14   thorpej 		timevalsub(&t2, &need_kern, &now);
    437  1.14   thorpej 		if (t2.tv_sec <= 0) {
    438  1.14   thorpej 			age(0);
    439   1.1       cgd 			continue;
    440   1.1       cgd 		}
    441  1.14   thorpej 		if (timercmp(&t2, &wtime, <))
    442  1.14   thorpej 			wtime = t2;
    443  1.14   thorpej 
    444  1.14   thorpej 		/* take care of router discovery,
    445  1.14   thorpej 		 * but do it to the millisecond
    446  1.14   thorpej 		 */
    447  1.14   thorpej 		if (!timercmp(&rdisc_timer, &now, >)) {
    448  1.14   thorpej 			rdisc_age(0);
    449   1.1       cgd 			continue;
    450   1.1       cgd 		}
    451  1.14   thorpej 		timevalsub(&t2, &rdisc_timer, &now);
    452  1.14   thorpej 		if (timercmp(&t2, &wtime, <))
    453  1.14   thorpej 			wtime = t2;
    454  1.14   thorpej 
    455  1.14   thorpej 
    456  1.14   thorpej 		/* wait for input or a timer to expire.
    457  1.14   thorpej 		 */
    458  1.14   thorpej 		trace_flush();
    459  1.14   thorpej 		ibits = fdbits;
    460  1.14   thorpej 		n = select(sock_max, &ibits, 0, 0, &wtime);
    461  1.14   thorpej 		if (n <= 0) {
    462  1.14   thorpej 			if (n < 0 && errno != EINTR && errno != EAGAIN)
    463  1.14   thorpej 				BADERR(1,"select");
    464   1.1       cgd 			continue;
    465   1.1       cgd 		}
    466  1.14   thorpej 
    467  1.14   thorpej 		if (FD_ISSET(rt_sock, &ibits)) {
    468  1.14   thorpej 			read_rt();
    469  1.14   thorpej 			n--;
    470  1.14   thorpej 		}
    471  1.14   thorpej 		if (rdisc_sock >= 0 && FD_ISSET(rdisc_sock, &ibits)) {
    472  1.14   thorpej 			read_d();
    473  1.14   thorpej 			n--;
    474  1.14   thorpej 		}
    475  1.14   thorpej 		if (rip_sock >= 0 && FD_ISSET(rip_sock, &ibits)) {
    476  1.14   thorpej 			read_rip(rip_sock, 0);
    477  1.14   thorpej 			n--;
    478  1.14   thorpej 		}
    479  1.14   thorpej 
    480  1.14   thorpej 		for (ifp = ifnet; n > 0 && 0 != ifp; ifp = ifp->int_next) {
    481  1.14   thorpej 			if (ifp->int_rip_sock >= 0
    482  1.14   thorpej 			    && FD_ISSET(ifp->int_rip_sock, &ibits)) {
    483  1.14   thorpej 				read_rip(ifp->int_rip_sock, ifp);
    484  1.14   thorpej 				n--;
    485  1.14   thorpej 			}
    486  1.14   thorpej 		}
    487   1.1       cgd 	}
    488  1.14   thorpej }
    489   1.1       cgd 
    490  1.14   thorpej 
    491  1.14   thorpej /* ARGSUSED */
    492  1.14   thorpej void
    493  1.14   thorpej sigalrm(int sig)
    494  1.14   thorpej {
    495  1.14   thorpej 	/* Historically, SIGALRM would cause the daemon to check for
    496  1.14   thorpej 	 * new and broken interfaces.
    497   1.1       cgd 	 */
    498  1.14   thorpej 	ifinit_timer.tv_sec = now.tv_sec;
    499  1.14   thorpej 	trace_act("SIGALRM\n");
    500  1.14   thorpej }
    501  1.14   thorpej 
    502  1.14   thorpej 
    503  1.14   thorpej /* watch for fatal signals */
    504  1.14   thorpej void
    505  1.14   thorpej sigterm(int sig)
    506  1.14   thorpej {
    507  1.14   thorpej 	stopint = sig;
    508  1.14   thorpej 	(void)signal(sig, SIG_DFL);	/* catch it only once */
    509  1.14   thorpej }
    510  1.14   thorpej 
    511  1.14   thorpej 
    512  1.14   thorpej void
    513  1.14   thorpej fix_select(void)
    514  1.14   thorpej {
    515  1.14   thorpej 	struct interface *ifp;
    516  1.14   thorpej 
    517  1.14   thorpej 
    518  1.14   thorpej 	FD_ZERO(&fdbits);
    519  1.14   thorpej 	sock_max = 0;
    520  1.14   thorpej 
    521  1.14   thorpej 	FD_SET(rt_sock, &fdbits);
    522  1.14   thorpej 	if (sock_max <= rt_sock)
    523  1.14   thorpej 		sock_max = rt_sock+1;
    524  1.14   thorpej 	if (rip_sock >= 0) {
    525  1.14   thorpej 		FD_SET(rip_sock, &fdbits);
    526  1.14   thorpej 		if (sock_max <= rip_sock)
    527  1.14   thorpej 			sock_max = rip_sock+1;
    528  1.14   thorpej 	}
    529  1.14   thorpej 	for (ifp = ifnet; 0 != ifp; ifp = ifp->int_next) {
    530  1.14   thorpej 		if (ifp->int_rip_sock >= 0) {
    531  1.14   thorpej 			FD_SET(ifp->int_rip_sock, &fdbits);
    532  1.14   thorpej 			if (sock_max <= ifp->int_rip_sock)
    533  1.14   thorpej 				sock_max = ifp->int_rip_sock+1;
    534  1.14   thorpej 		}
    535  1.14   thorpej 	}
    536  1.14   thorpej 	if (rdisc_sock >= 0) {
    537  1.14   thorpej 		FD_SET(rdisc_sock, &fdbits);
    538  1.14   thorpej 		if (sock_max <= rdisc_sock)
    539  1.14   thorpej 			sock_max = rdisc_sock+1;
    540  1.14   thorpej 	}
    541  1.14   thorpej }
    542  1.14   thorpej 
    543  1.14   thorpej 
    544  1.14   thorpej void
    545  1.14   thorpej fix_sock(int sock,
    546  1.14   thorpej 	 char *name)
    547  1.14   thorpej {
    548  1.14   thorpej 	int on;
    549  1.14   thorpej #define MIN_SOCKBUF (4*1024)
    550  1.14   thorpej 	static int rbuf;
    551   1.1       cgd 
    552  1.14   thorpej 	if (fcntl(sock, F_SETFL, O_NONBLOCK) == -1)
    553  1.14   thorpej 		logbad(1, "fcntl(%s) O_NONBLOCK: %s",
    554  1.14   thorpej 		       name, strerror(errno));
    555  1.14   thorpej 	on = 1;
    556  1.14   thorpej 	if (setsockopt(sock, SOL_SOCKET,SO_BROADCAST,
    557  1.14   thorpej 		       &on,sizeof(on)) < 0)
    558  1.14   thorpej 		msglog("setsockopt(%s,SO_BROADCAST): %s",
    559  1.14   thorpej 		       name, strerror(errno));
    560  1.14   thorpej 	if (rbuf >= MIN_SOCKBUF) {
    561  1.14   thorpej 		if (setsockopt(sock, SOL_SOCKET, SO_RCVBUF,
    562  1.14   thorpej 			       &rbuf, sizeof(rbuf)) < 0)
    563  1.14   thorpej 			msglog("setsockopt(%s,SO_RCVBUF=%d): %s",
    564  1.14   thorpej 			       name, rbuf, strerror(errno));
    565  1.14   thorpej 	} else {
    566  1.14   thorpej 		for (rbuf = 60*1024; ; rbuf -= 4096) {
    567  1.14   thorpej 			if (setsockopt(sock, SOL_SOCKET, SO_RCVBUF,
    568  1.14   thorpej 				       &rbuf, sizeof(rbuf)) == 0) {
    569  1.14   thorpej 				trace_act("RCVBUF=%d\n", rbuf);
    570  1.14   thorpej 				break;
    571   1.1       cgd 			}
    572  1.14   thorpej 			if (rbuf < MIN_SOCKBUF) {
    573  1.14   thorpej 				msglog("setsockopt(%s,SO_RCVBUF = %d): %s",
    574  1.14   thorpej 				       name, rbuf, strerror(errno));
    575  1.14   thorpej 				break;
    576   1.1       cgd 			}
    577  1.14   thorpej 		}
    578  1.14   thorpej 	}
    579  1.14   thorpej }
    580  1.14   thorpej 
    581  1.14   thorpej 
    582  1.14   thorpej /* get a rip socket
    583  1.14   thorpej  */
    584  1.14   thorpej static int				/* <0 or file descriptor */
    585  1.14   thorpej get_rip_sock(naddr addr,
    586  1.14   thorpej 	     int serious)		/* 1=failure to bind is serious */
    587  1.14   thorpej {
    588  1.14   thorpej 	struct sockaddr_in sin;
    589  1.14   thorpej 	unsigned char ttl;
    590  1.14   thorpej 	int s;
    591  1.14   thorpej 
    592  1.14   thorpej 
    593  1.14   thorpej 	if ((s = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
    594  1.14   thorpej 		BADERR(1,"rip_sock = socket()");
    595  1.14   thorpej 
    596  1.14   thorpej 	bzero(&sin,sizeof(sin));
    597  1.14   thorpej #ifdef _HAVE_SIN_LEN
    598  1.14   thorpej 	sin.sin_len = sizeof(sin);
    599  1.14   thorpej #endif
    600  1.14   thorpej 	sin.sin_family = AF_INET;
    601  1.14   thorpej 	sin.sin_port = htons(RIP_PORT);
    602  1.14   thorpej 	sin.sin_addr.s_addr = addr;
    603  1.14   thorpej 	if (bind(s, (struct sockaddr *)&sin,sizeof(sin)) < 0) {
    604  1.14   thorpej 		if (serious)
    605  1.14   thorpej 			BADERR(errno != EADDRINUSE, "bind(rip_sock)");
    606  1.14   thorpej 		return -1;
    607  1.14   thorpej 	}
    608  1.14   thorpej 	fix_sock(s,"rip_sock");
    609  1.14   thorpej 
    610  1.14   thorpej 	ttl = 1;
    611  1.14   thorpej 	if (setsockopt(s, IPPROTO_IP, IP_MULTICAST_TTL,
    612  1.14   thorpej 		       &ttl, sizeof(ttl)) < 0)
    613  1.14   thorpej 		DBGERR(1,"rip_sock setsockopt(IP_MULTICAST_TTL)");
    614  1.14   thorpej 
    615  1.14   thorpej 	return s;
    616  1.14   thorpej }
    617  1.14   thorpej 
    618  1.14   thorpej 
    619  1.14   thorpej /* turn off main RIP socket */
    620  1.14   thorpej void
    621  1.14   thorpej rip_off(void)
    622  1.14   thorpej {
    623  1.14   thorpej 	struct interface *ifp;
    624  1.14   thorpej 	register naddr addr;
    625  1.14   thorpej 
    626  1.14   thorpej 
    627  1.14   thorpej 	if (rip_sock >= 0 && !mhome) {
    628  1.14   thorpej 		trace_act("turn off RIP\n");
    629  1.14   thorpej 
    630  1.14   thorpej 		(void)close(rip_sock);
    631  1.14   thorpej 		rip_sock = -1;
    632  1.14   thorpej 
    633  1.14   thorpej 		/* get non-broadcast sockets to listen to queries.
    634  1.14   thorpej 		 */
    635  1.14   thorpej 		for (ifp = ifnet; ifp != 0; ifp = ifp->int_next) {
    636  1.14   thorpej 			if (ifp->int_rip_sock < 0
    637  1.14   thorpej 			    && !(ifp->int_state & IS_ALIAS)) {
    638  1.14   thorpej 				addr = ((ifp->int_if_flags & IFF_POINTOPOINT)
    639  1.14   thorpej 					? ifp->int_dstaddr
    640  1.14   thorpej 					: ifp->int_addr);
    641  1.14   thorpej 				ifp->int_rip_sock = get_rip_sock(addr, 0);
    642   1.1       cgd 			}
    643   1.1       cgd 		}
    644  1.14   thorpej 
    645  1.14   thorpej 		fix_select();
    646  1.14   thorpej 
    647  1.14   thorpej 		age(0);
    648  1.14   thorpej 	}
    649  1.14   thorpej }
    650  1.14   thorpej 
    651  1.14   thorpej 
    652  1.14   thorpej /* turn on RIP multicast input via an interface
    653  1.14   thorpej  */
    654  1.14   thorpej static void
    655  1.14   thorpej rip_mcast_on(struct interface *ifp)
    656  1.14   thorpej {
    657  1.14   thorpej 	struct ip_mreq m;
    658  1.14   thorpej 
    659  1.14   thorpej 	if (!IS_RIP_IN_OFF(ifp->int_state)
    660  1.14   thorpej 	    && (ifp->int_if_flags & IFF_MULTICAST)
    661  1.14   thorpej #ifdef MCAST_PPP_BUG
    662  1.14   thorpej 	    && !(ifp->int_if_flags & IFF_POINTOPOINT)
    663   1.1       cgd #endif
    664  1.14   thorpej 	    && !(ifp->int_state & IS_ALIAS)) {
    665  1.14   thorpej 		m.imr_multiaddr.s_addr = htonl(INADDR_RIP_GROUP);
    666  1.14   thorpej 		m.imr_interface.s_addr = ((ifp->int_if_flags & IFF_POINTOPOINT)
    667  1.14   thorpej 					  ? ifp->int_dstaddr
    668  1.14   thorpej 					  : ifp->int_addr);
    669  1.14   thorpej 		if (setsockopt(rip_sock,IPPROTO_IP, IP_ADD_MEMBERSHIP,
    670  1.14   thorpej 			       &m, sizeof(m)) < 0)
    671  1.14   thorpej 			LOGERR("setsockopt(IP_ADD_MEMBERSHIP RIP)");
    672   1.1       cgd 	}
    673   1.1       cgd }
    674   1.1       cgd 
    675  1.14   thorpej 
    676  1.14   thorpej /* Prepare socket used for RIP.
    677  1.14   thorpej  */
    678   1.6       cgd void
    679  1.14   thorpej rip_on(struct interface *ifp)
    680   1.1       cgd {
    681  1.14   thorpej 	/* If the main RIP socket is already alive, only start receiving
    682  1.14   thorpej 	 * multicasts for this interface.
    683  1.14   thorpej 	 */
    684  1.14   thorpej 	if (rip_sock >= 0) {
    685  1.14   thorpej 		if (ifp != 0)
    686  1.14   thorpej 			rip_mcast_on(ifp);
    687  1.14   thorpej 		return;
    688  1.14   thorpej 	}
    689  1.14   thorpej 
    690  1.14   thorpej 	/* If the main RIP socket is off, and it makes sense to turn it on,
    691  1.14   thorpej 	 * turn it on for all of the interfaces.
    692  1.14   thorpej 	 */
    693  1.14   thorpej 	if (rip_interfaces > 0 && !rdisc_ok) {
    694  1.14   thorpej 		trace_act("turn on RIP\n");
    695  1.14   thorpej 
    696  1.14   thorpej 		/* Close all of the query sockets so that we can open
    697  1.14   thorpej 		 * the main socket.  SO_REUSEPORT is not a solution,
    698  1.14   thorpej 		 * since that would let two daemons bind to the broadcast
    699  1.14   thorpej 		 * socket.
    700  1.14   thorpej 		 */
    701  1.14   thorpej 		for (ifp = ifnet; ifp != 0; ifp = ifp->int_next) {
    702  1.14   thorpej 			if (ifp->int_rip_sock >= 0) {
    703  1.14   thorpej 				(void)close(ifp->int_rip_sock);
    704  1.14   thorpej 				ifp->int_rip_sock = -1;
    705  1.14   thorpej 			}
    706  1.14   thorpej 		}
    707  1.14   thorpej 
    708  1.14   thorpej 		rip_sock = get_rip_sock(INADDR_ANY, 1);
    709  1.14   thorpej 		rip_sock_mcast = 0;
    710  1.14   thorpej 
    711  1.14   thorpej 		/* Do not advertise anything until we have heard something
    712  1.14   thorpej 		 */
    713  1.14   thorpej 		if (next_bcast.tv_sec < now.tv_sec+MIN_WAITTIME)
    714  1.14   thorpej 			next_bcast.tv_sec = now.tv_sec+MIN_WAITTIME;
    715   1.1       cgd 
    716  1.14   thorpej 		for (ifp = ifnet; ifp != 0; ifp = ifp->int_next) {
    717  1.14   thorpej 			if (!IS_RIP_IN_OFF(ifp->int_state))
    718  1.14   thorpej 				ifp->int_state &= ~IS_RIP_QUERIED;
    719  1.14   thorpej 			rip_mcast_on(ifp);
    720   1.1       cgd 		}
    721  1.14   thorpej 
    722  1.14   thorpej 		ifinit_timer.tv_sec = now.tv_sec;
    723  1.14   thorpej 
    724  1.14   thorpej 		fix_select();
    725  1.14   thorpej 
    726  1.14   thorpej 	} else if (ifp != 0
    727  1.14   thorpej 		   && ifp->int_rip_sock < 0
    728  1.14   thorpej 		   && !(ifp->int_state & IS_ALIAS)) {
    729  1.14   thorpej 		/* RIP is off, so ensure there are sockets on which
    730  1.14   thorpej 		 * to listen for queries.
    731  1.14   thorpej 		 */
    732  1.14   thorpej 		ifp->int_rip_sock = get_rip_sock(ifp->int_addr, 0);
    733  1.14   thorpej 
    734  1.14   thorpej 		fix_select();
    735   1.1       cgd 	}
    736  1.15  christos }
    737  1.15  christos 
    738  1.15  christos 
    739  1.15  christos /* die if malloc(3) fails
    740  1.15  christos  */
    741  1.15  christos void *
    742  1.15  christos rtmalloc(size_t size,
    743  1.15  christos 	 char *msg)
    744  1.15  christos {
    745  1.15  christos 	void *p = malloc(size);
    746  1.15  christos 	if (p == 0)
    747  1.15  christos 		logbad(1,"malloc() failed in %s", msg);
    748  1.15  christos 	return p;
    749   1.1       cgd }
    750   1.1       cgd 
    751  1.14   thorpej 
    752  1.14   thorpej /* get a random instant in an interval
    753  1.14   thorpej  */
    754  1.14   thorpej void
    755  1.14   thorpej intvl_random(struct timeval *tp,	/* put value here */
    756  1.14   thorpej 	     u_long lo,			/* value is after this second */
    757  1.14   thorpej 	     u_long hi)			/* and before this */
    758  1.14   thorpej {
    759  1.14   thorpej 	tp->tv_sec = (time_t)(hi == lo
    760  1.14   thorpej 			      ? lo
    761  1.14   thorpej 			      : (lo + random() % ((hi - lo))));
    762  1.14   thorpej 	tp->tv_usec = random() % 1000000;
    763  1.14   thorpej }
    764  1.14   thorpej 
    765  1.14   thorpej 
    766  1.14   thorpej void
    767  1.14   thorpej timevaladd(struct timeval *t1,
    768  1.14   thorpej 	   struct timeval *t2)
    769  1.14   thorpej {
    770  1.14   thorpej 
    771  1.14   thorpej 	t1->tv_sec += t2->tv_sec;
    772  1.14   thorpej 	if ((t1->tv_usec += t2->tv_usec) > 1000000) {
    773  1.14   thorpej 		t1->tv_sec++;
    774  1.14   thorpej 		t1->tv_usec -= 1000000;
    775   1.1       cgd 	}
    776  1.14   thorpej }
    777  1.14   thorpej 
    778  1.14   thorpej 
    779  1.14   thorpej /* t1 = t2 - t3
    780  1.14   thorpej  */
    781  1.14   thorpej static void
    782  1.14   thorpej timevalsub(struct timeval *t1,
    783  1.14   thorpej 	   struct timeval *t2,
    784  1.14   thorpej 	   struct timeval *t3)
    785  1.14   thorpej {
    786  1.14   thorpej 	t1->tv_sec = t2->tv_sec - t3->tv_sec;
    787  1.14   thorpej 	if ((t1->tv_usec = t2->tv_usec - t3->tv_usec) < 0) {
    788  1.14   thorpej 		t1->tv_sec--;
    789  1.14   thorpej 		t1->tv_usec += 1000000;
    790   1.1       cgd 	}
    791  1.14   thorpej }
    792  1.14   thorpej 
    793  1.14   thorpej 
    794  1.14   thorpej void
    795  1.14   thorpej msglog(char *p, ...)
    796  1.14   thorpej {
    797  1.14   thorpej 	va_list args;
    798  1.14   thorpej 
    799  1.14   thorpej 	trace_flush();
    800  1.14   thorpej 
    801  1.14   thorpej 	va_start(args, p);
    802  1.14   thorpej 	vsyslog(LOG_ERR, p, args);
    803  1.14   thorpej 
    804  1.14   thorpej 	if (ftrace != 0) {
    805  1.14   thorpej 		if (ftrace == stdout)
    806  1.14   thorpej 			(void)fputs("routed: ", ftrace);
    807  1.14   thorpej 		(void)vfprintf(ftrace, p, args);
    808  1.14   thorpej 		(void)fputc('\n', ftrace);
    809   1.1       cgd 	}
    810  1.14   thorpej }
    811  1.14   thorpej 
    812  1.14   thorpej 
    813  1.14   thorpej void
    814  1.14   thorpej logbad(int dump, char *p, ...)
    815  1.14   thorpej {
    816  1.14   thorpej 	va_list args;
    817  1.14   thorpej 
    818  1.14   thorpej 	trace_flush();
    819  1.14   thorpej 
    820  1.14   thorpej 	va_start(args, p);
    821  1.14   thorpej 	vsyslog(LOG_ERR, p, args);
    822  1.14   thorpej 
    823  1.14   thorpej 	(void)fputs("routed: ", stderr);
    824  1.14   thorpej 	(void)vfprintf(stderr, p, args);
    825  1.14   thorpej 	(void)fputs("; giving up\n",stderr);
    826  1.14   thorpej 	(void)fflush(stderr);
    827  1.14   thorpej 
    828  1.14   thorpej 	if (dump)
    829  1.14   thorpej 		abort();
    830  1.14   thorpej 	exit(1);
    831   1.1       cgd }
    832