10Sstevel@tonic-gate /*
2*10547SVladimir.Kotal@Sun.COM * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
30Sstevel@tonic-gate * Use is subject to license terms.
40Sstevel@tonic-gate *
50Sstevel@tonic-gate * Copyright (c) 1983, 1988, 1993
60Sstevel@tonic-gate * The Regents of the University of California. All rights reserved.
70Sstevel@tonic-gate *
80Sstevel@tonic-gate * Redistribution and use in source and binary forms, with or without
90Sstevel@tonic-gate * modification, are permitted provided that the following conditions
100Sstevel@tonic-gate * are met:
110Sstevel@tonic-gate * 1. Redistributions of source code must retain the above copyright
120Sstevel@tonic-gate * notice, this list of conditions and the following disclaimer.
130Sstevel@tonic-gate * 2. Redistributions in binary form must reproduce the above copyright
140Sstevel@tonic-gate * notice, this list of conditions and the following disclaimer in the
150Sstevel@tonic-gate * documentation and/or other materials provided with the distribution.
160Sstevel@tonic-gate * 3. All advertising materials mentioning features or use of this software
170Sstevel@tonic-gate * must display the following acknowledgment:
180Sstevel@tonic-gate * This product includes software developed by the University of
190Sstevel@tonic-gate * California, Berkeley and its contributors.
200Sstevel@tonic-gate * 4. Neither the name of the University nor the names of its contributors
210Sstevel@tonic-gate * may be used to endorse or promote products derived from this software
220Sstevel@tonic-gate * without specific prior written permission.
230Sstevel@tonic-gate *
240Sstevel@tonic-gate * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
250Sstevel@tonic-gate * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
260Sstevel@tonic-gate * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
270Sstevel@tonic-gate * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
280Sstevel@tonic-gate * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
290Sstevel@tonic-gate * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
300Sstevel@tonic-gate * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
310Sstevel@tonic-gate * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
320Sstevel@tonic-gate * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
330Sstevel@tonic-gate * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
340Sstevel@tonic-gate * SUCH DAMAGE.
350Sstevel@tonic-gate *
360Sstevel@tonic-gate * $FreeBSD: src/sbin/routed/main.c,v 1.14 2000/08/11 08:24:38 sheldonh Exp $
370Sstevel@tonic-gate * char copyright[] = "@(#) Copyright (c) 1983, 1988, 1993\n"
380Sstevel@tonic-gate * " The Regents of the University of California. All rights reserved.\n";
390Sstevel@tonic-gate */
400Sstevel@tonic-gate
410Sstevel@tonic-gate #include "defs.h"
420Sstevel@tonic-gate #include "pathnames.h"
430Sstevel@tonic-gate #include <signal.h>
440Sstevel@tonic-gate #include <fcntl.h>
450Sstevel@tonic-gate #include <sys/file.h>
460Sstevel@tonic-gate #include <userdefs.h>
470Sstevel@tonic-gate #include <sys/stat.h>
480Sstevel@tonic-gate
490Sstevel@tonic-gate #define IN_ROUTED_VERSION "2.22"
500Sstevel@tonic-gate
510Sstevel@tonic-gate int stopint;
520Sstevel@tonic-gate boolean_t supplier; /* supply or broadcast updates */
530Sstevel@tonic-gate boolean_t supplier_set;
540Sstevel@tonic-gate /* -S option. _B_TRUE=treat all RIP speakers as default routers. */
550Sstevel@tonic-gate boolean_t save_space = _B_FALSE;
560Sstevel@tonic-gate
570Sstevel@tonic-gate static boolean_t default_gateway; /* _B_TRUE=advertise default */
580Sstevel@tonic-gate static boolean_t background = _B_TRUE;
590Sstevel@tonic-gate boolean_t ridhosts; /* _B_TRUE=reduce host routes */
600Sstevel@tonic-gate boolean_t mhome; /* _B_TRUE=want multi-homed host route */
610Sstevel@tonic-gate boolean_t advertise_mhome; /* _B_TRUE=must continue advertising it */
620Sstevel@tonic-gate boolean_t auth_ok = _B_TRUE; /* _B_TRUE=ignore auth if we don't care */
630Sstevel@tonic-gate boolean_t no_install; /* _B_TRUE=don't install in kernel */
640Sstevel@tonic-gate
650Sstevel@tonic-gate struct timeval epoch; /* when started */
660Sstevel@tonic-gate struct timeval clk;
670Sstevel@tonic-gate static struct timeval prev_clk;
680Sstevel@tonic-gate static int usec_fudge;
690Sstevel@tonic-gate struct timeval now; /* current idea of time */
700Sstevel@tonic-gate /* If a route's rts_time is <= to now_stale, the route is stale. */
710Sstevel@tonic-gate time_t now_stale;
720Sstevel@tonic-gate /* If a route's rts_time is <= to now_expire, the route is expired */
730Sstevel@tonic-gate time_t now_expire;
740Sstevel@tonic-gate /* If a route's rts_time is <= to now_garbage, the route needs to be deleted */
750Sstevel@tonic-gate time_t now_garbage;
760Sstevel@tonic-gate
770Sstevel@tonic-gate static struct timeval next_bcast; /* next general broadcast */
780Sstevel@tonic-gate struct timeval no_flash = { /* inhibit flash update */
790Sstevel@tonic-gate EPOCH+SUPPLY_INTERVAL, 0
800Sstevel@tonic-gate };
810Sstevel@tonic-gate
820Sstevel@tonic-gate /* When now reaches this time, it's time to call sync_kern() */
830Sstevel@tonic-gate static struct timeval sync_kern_timer;
840Sstevel@tonic-gate
850Sstevel@tonic-gate static fd_set fdbits;
860Sstevel@tonic-gate static int sock_max;
870Sstevel@tonic-gate int rip_sock = -1; /* RIP socket */
880Sstevel@tonic-gate boolean_t rip_enabled;
890Sstevel@tonic-gate static boolean_t openlog_done;
900Sstevel@tonic-gate
910Sstevel@tonic-gate /*
920Sstevel@tonic-gate * The interface to which rip_sock is currently pointing for
930Sstevel@tonic-gate * output.
940Sstevel@tonic-gate */
950Sstevel@tonic-gate struct interface *rip_sock_interface;
960Sstevel@tonic-gate
970Sstevel@tonic-gate int rt_sock; /* routing socket */
980Sstevel@tonic-gate
990Sstevel@tonic-gate
1000Sstevel@tonic-gate static int open_rip_sock();
1010Sstevel@tonic-gate static void timevalsub(struct timeval *, struct timeval *, struct timeval *);
1020Sstevel@tonic-gate static void sigalrm(int);
1030Sstevel@tonic-gate static void sigterm(int);
1040Sstevel@tonic-gate
1050Sstevel@tonic-gate int
main(int argc,char * argv[])1060Sstevel@tonic-gate main(int argc, char *argv[])
1070Sstevel@tonic-gate {
1080Sstevel@tonic-gate int n, off;
1090Sstevel@tonic-gate char *p, *q;
1100Sstevel@tonic-gate const char *cp;
1110Sstevel@tonic-gate struct timeval select_timeout, result;
1120Sstevel@tonic-gate fd_set ibits;
1130Sstevel@tonic-gate in_addr_t p_net, p_mask;
1140Sstevel@tonic-gate struct parm parm;
1150Sstevel@tonic-gate char *tracename = NULL;
1160Sstevel@tonic-gate boolean_t vflag = _B_FALSE;
1170Sstevel@tonic-gate boolean_t version = _B_FALSE;
1180Sstevel@tonic-gate int sigerr = 0;
1190Sstevel@tonic-gate FILE *pidfp;
1200Sstevel@tonic-gate mode_t pidmode = (S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH); /* 0644 */
1210Sstevel@tonic-gate
1220Sstevel@tonic-gate (void) setlocale(LC_ALL, "");
1230Sstevel@tonic-gate
1240Sstevel@tonic-gate #if !defined(TEXT_DOMAIN) /* Should be defined by cc -D */
1250Sstevel@tonic-gate #define TEXT_DOMAIN "SYS_TEXT"
1260Sstevel@tonic-gate #endif /* ! TEXT_DOMAIN */
1270Sstevel@tonic-gate
1280Sstevel@tonic-gate (void) textdomain(TEXT_DOMAIN);
1290Sstevel@tonic-gate
1300Sstevel@tonic-gate /*
1310Sstevel@tonic-gate * Some shells are badly broken and send SIGHUP to backgrounded
1320Sstevel@tonic-gate * processes.
1330Sstevel@tonic-gate */
1340Sstevel@tonic-gate if (signal(SIGHUP, SIG_IGN) == SIG_ERR)
1350Sstevel@tonic-gate sigerr = errno;
1360Sstevel@tonic-gate
1370Sstevel@tonic-gate ftrace = stdout;
1380Sstevel@tonic-gate
1390Sstevel@tonic-gate if (gettimeofday(&clk, 0) == -1) {
1400Sstevel@tonic-gate logbad(_B_FALSE, "gettimeofday: %s", rip_strerror(errno));
1410Sstevel@tonic-gate }
1420Sstevel@tonic-gate prev_clk = clk;
1430Sstevel@tonic-gate epoch = clk;
1440Sstevel@tonic-gate epoch.tv_sec -= EPOCH;
1450Sstevel@tonic-gate now.tv_sec = EPOCH;
1460Sstevel@tonic-gate now_stale = EPOCH - STALE_TIME;
1470Sstevel@tonic-gate now_expire = EPOCH - EXPIRE_TIME;
1480Sstevel@tonic-gate now_garbage = EPOCH - GARBAGE_TIME;
1490Sstevel@tonic-gate select_timeout.tv_sec = 0;
1500Sstevel@tonic-gate
1510Sstevel@tonic-gate while ((n = getopt(argc, argv, "sSqdghmpAztVvnT:F:P:")) != -1) {
1520Sstevel@tonic-gate switch (n) {
1530Sstevel@tonic-gate case 'A':
1540Sstevel@tonic-gate /*
1550Sstevel@tonic-gate * Ignore authentication if we do not care.
1560Sstevel@tonic-gate * Crazy as it is, that is what RFC 2453 requires.
1570Sstevel@tonic-gate */
1580Sstevel@tonic-gate auth_ok = _B_FALSE;
1590Sstevel@tonic-gate break;
1600Sstevel@tonic-gate
1610Sstevel@tonic-gate case 't':
1620Sstevel@tonic-gate if (new_tracelevel < 2)
1630Sstevel@tonic-gate new_tracelevel = 2;
1640Sstevel@tonic-gate background = _B_FALSE;
1650Sstevel@tonic-gate break;
1660Sstevel@tonic-gate
1670Sstevel@tonic-gate case 'd': /* put in.routed in foreground */
1680Sstevel@tonic-gate background = _B_FALSE;
1690Sstevel@tonic-gate break;
1700Sstevel@tonic-gate
1710Sstevel@tonic-gate case 'F': /* minimal routes for SLIP */
1720Sstevel@tonic-gate n = FAKE_METRIC;
1730Sstevel@tonic-gate p = strchr(optarg, ',');
1740Sstevel@tonic-gate if (p != NULL) {
1750Sstevel@tonic-gate n = (int)strtoul(p+1, &q, 0);
1760Sstevel@tonic-gate if (*q == '\0' && p+1 != q &&
1770Sstevel@tonic-gate n <= HOPCNT_INFINITY-1 && n >= 1)
1780Sstevel@tonic-gate *p = '\0';
1790Sstevel@tonic-gate }
1800Sstevel@tonic-gate if (!getnet(optarg, &p_net, &p_mask)) {
1810Sstevel@tonic-gate if (p != NULL)
1820Sstevel@tonic-gate *p = ',';
1830Sstevel@tonic-gate msglog(gettext("bad network; \"-F %s\""),
1840Sstevel@tonic-gate optarg);
1850Sstevel@tonic-gate break;
1860Sstevel@tonic-gate }
1870Sstevel@tonic-gate (void) memset(&parm, 0, sizeof (parm));
1880Sstevel@tonic-gate parm.parm_net = p_net;
1890Sstevel@tonic-gate parm.parm_mask = p_mask;
1900Sstevel@tonic-gate parm.parm_d_metric = n;
1910Sstevel@tonic-gate cp = insert_parm(&parm);
1920Sstevel@tonic-gate if (cp != NULL)
1930Sstevel@tonic-gate msglog(gettext("bad -F: %s"), cp);
1940Sstevel@tonic-gate break;
1950Sstevel@tonic-gate
1960Sstevel@tonic-gate case 'g':
1970Sstevel@tonic-gate (void) memset(&parm, 0, sizeof (parm));
1980Sstevel@tonic-gate parm.parm_d_metric = 1;
1990Sstevel@tonic-gate cp = insert_parm(&parm);
2000Sstevel@tonic-gate if (cp != NULL)
2010Sstevel@tonic-gate msglog(gettext("bad -g: %s"), cp);
2020Sstevel@tonic-gate else
2030Sstevel@tonic-gate default_gateway = _B_TRUE;
2040Sstevel@tonic-gate break;
2050Sstevel@tonic-gate
2060Sstevel@tonic-gate case 'h': /* suppress extra host routes */
2070Sstevel@tonic-gate ridhosts = _B_TRUE;
2080Sstevel@tonic-gate break;
2090Sstevel@tonic-gate
2100Sstevel@tonic-gate case 'm': /* advertise host route */
2110Sstevel@tonic-gate mhome = _B_TRUE; /* on multi-homed hosts */
2120Sstevel@tonic-gate break;
2130Sstevel@tonic-gate
2140Sstevel@tonic-gate case 'n': /* No-install mode */
2150Sstevel@tonic-gate no_install = _B_TRUE;
2160Sstevel@tonic-gate break;
2170Sstevel@tonic-gate
2180Sstevel@tonic-gate case 'P':
2190Sstevel@tonic-gate /* handle arbitrary parameters. */
2200Sstevel@tonic-gate q = strdup(optarg);
2210Sstevel@tonic-gate if (q == NULL)
2220Sstevel@tonic-gate logbad(_B_FALSE, "strdup: %s",
2230Sstevel@tonic-gate rip_strerror(errno));
2240Sstevel@tonic-gate cp = parse_parms(q, _B_FALSE);
2250Sstevel@tonic-gate if (cp != NULL)
2260Sstevel@tonic-gate msglog(gettext("%1$s in \"-P %2$s\""), cp,
2270Sstevel@tonic-gate optarg);
2280Sstevel@tonic-gate free(q);
2290Sstevel@tonic-gate break;
2300Sstevel@tonic-gate
2310Sstevel@tonic-gate case 'q':
2320Sstevel@tonic-gate supplier = _B_FALSE;
2330Sstevel@tonic-gate supplier_set = _B_TRUE;
2340Sstevel@tonic-gate break;
2350Sstevel@tonic-gate
2360Sstevel@tonic-gate case 's':
2370Sstevel@tonic-gate supplier = _B_TRUE;
2380Sstevel@tonic-gate supplier_set = _B_TRUE;
2390Sstevel@tonic-gate break;
2400Sstevel@tonic-gate
2410Sstevel@tonic-gate case 'S': /* save-space option */
2420Sstevel@tonic-gate save_space = _B_TRUE;
2430Sstevel@tonic-gate break;
2440Sstevel@tonic-gate
2450Sstevel@tonic-gate case 'T':
2460Sstevel@tonic-gate tracename = optarg;
2470Sstevel@tonic-gate break;
2480Sstevel@tonic-gate
2490Sstevel@tonic-gate case 'V':
2500Sstevel@tonic-gate /* display version */
2510Sstevel@tonic-gate version = _B_TRUE;
2520Sstevel@tonic-gate msglog(gettext("version " IN_ROUTED_VERSION));
2530Sstevel@tonic-gate break;
2540Sstevel@tonic-gate
2550Sstevel@tonic-gate case 'v':
2560Sstevel@tonic-gate /* display route changes to supplied logfile */
2570Sstevel@tonic-gate new_tracelevel = 1;
2580Sstevel@tonic-gate vflag = _B_TRUE;
2590Sstevel@tonic-gate break;
2600Sstevel@tonic-gate
2610Sstevel@tonic-gate case 'z': /* increase debug-level */
2620Sstevel@tonic-gate new_tracelevel++;
2630Sstevel@tonic-gate break;
2640Sstevel@tonic-gate
2650Sstevel@tonic-gate default:
2660Sstevel@tonic-gate goto usage;
2670Sstevel@tonic-gate }
2680Sstevel@tonic-gate }
2690Sstevel@tonic-gate argc -= optind;
2700Sstevel@tonic-gate argv += optind;
2710Sstevel@tonic-gate
2720Sstevel@tonic-gate if (tracename == NULL && argc >= 1) {
2730Sstevel@tonic-gate tracename = *argv++;
2740Sstevel@tonic-gate argc--;
2750Sstevel@tonic-gate }
2760Sstevel@tonic-gate if (tracename != NULL && tracename[0] == '\0')
2770Sstevel@tonic-gate goto usage;
2780Sstevel@tonic-gate if (vflag && tracename == NULL)
2790Sstevel@tonic-gate goto usage;
2800Sstevel@tonic-gate if (argc != 0) {
2810Sstevel@tonic-gate usage:
282*10547SVladimir.Kotal@Sun.COM (void) fprintf(stderr, gettext(
283*10547SVladimir.Kotal@Sun.COM "usage: in.routed [-AdghmnqsStVvz] "
284*10547SVladimir.Kotal@Sun.COM "[-T <tracefile>]\n"));
2850Sstevel@tonic-gate (void) fprintf(stderr,
2860Sstevel@tonic-gate gettext("\t[-F <net>[/<mask>][,<metric>]] [-P <parms>]\n"));
2870Sstevel@tonic-gate logbad(_B_FALSE, gettext("excess arguments"));
2880Sstevel@tonic-gate }
2890Sstevel@tonic-gate if (geteuid() != 0) {
2900Sstevel@tonic-gate /*
2910Sstevel@tonic-gate * Regular users are allowed to run in.routed for the
2920Sstevel@tonic-gate * sole purpose of obtaining the version number. In
2930Sstevel@tonic-gate * that case, exit(EXIT_SUCCESS) without complaining.
2940Sstevel@tonic-gate */
2950Sstevel@tonic-gate if (version)
2960Sstevel@tonic-gate exit(EXIT_SUCCESS);
2970Sstevel@tonic-gate logbad(_B_FALSE, gettext("requires UID 0"));
2980Sstevel@tonic-gate }
2990Sstevel@tonic-gate
3000Sstevel@tonic-gate if (default_gateway) {
3010Sstevel@tonic-gate if (supplier_set && !supplier) {
3020Sstevel@tonic-gate msglog(gettext("-g and -q are incompatible"));
3030Sstevel@tonic-gate } else {
3040Sstevel@tonic-gate supplier = _B_TRUE;
3050Sstevel@tonic-gate supplier_set = _B_TRUE;
3060Sstevel@tonic-gate }
3070Sstevel@tonic-gate }
3080Sstevel@tonic-gate
3090Sstevel@tonic-gate if (signal(SIGALRM, sigalrm) == SIG_ERR)
3100Sstevel@tonic-gate sigerr = errno;
3110Sstevel@tonic-gate /* SIGHUP fatal during debugging */
3120Sstevel@tonic-gate if (!background)
3130Sstevel@tonic-gate if (signal(SIGHUP, sigterm) == SIG_ERR)
3140Sstevel@tonic-gate sigerr = errno;
3150Sstevel@tonic-gate if (signal(SIGTERM, sigterm) == SIG_ERR)
3160Sstevel@tonic-gate sigerr = errno;
3170Sstevel@tonic-gate if (signal(SIGINT, sigterm) == SIG_ERR)
3180Sstevel@tonic-gate sigerr = errno;
3190Sstevel@tonic-gate if (signal(SIGUSR1, sigtrace_more) == SIG_ERR)
3200Sstevel@tonic-gate sigerr = errno;
3210Sstevel@tonic-gate if (signal(SIGUSR2, sigtrace_less) == SIG_ERR)
3220Sstevel@tonic-gate sigerr = errno;
3230Sstevel@tonic-gate if (signal(SIGHUP, sigtrace_dump) == SIG_ERR)
3240Sstevel@tonic-gate sigerr = errno;
3250Sstevel@tonic-gate
3260Sstevel@tonic-gate if (sigerr)
3270Sstevel@tonic-gate msglog("signal: %s", rip_strerror(sigerr));
3280Sstevel@tonic-gate
3290Sstevel@tonic-gate /* get into the background */
330*10547SVladimir.Kotal@Sun.COM if (background && daemon(0, 0) < 0)
3310Sstevel@tonic-gate BADERR(_B_FALSE, "daemon()");
3320Sstevel@tonic-gate
3330Sstevel@tonic-gate /* Store our process id, blow away any existing file if it exists. */
3340Sstevel@tonic-gate if ((pidfp = fopen(PATH_PID, "w")) == NULL) {
3350Sstevel@tonic-gate (void) fprintf(stderr,
3360Sstevel@tonic-gate gettext("in.routed: unable to open " PATH_PID ": %s\n"),
3370Sstevel@tonic-gate strerror(errno));
3380Sstevel@tonic-gate } else {
3390Sstevel@tonic-gate (void) fprintf(pidfp, "%ld\n", getpid());
3400Sstevel@tonic-gate (void) fclose(pidfp);
3410Sstevel@tonic-gate (void) chmod(PATH_PID, pidmode);
3420Sstevel@tonic-gate }
3430Sstevel@tonic-gate
3440Sstevel@tonic-gate srandom((int)(clk.tv_sec ^ clk.tv_usec ^ getpid()));
3450Sstevel@tonic-gate
3460Sstevel@tonic-gate /* allocate the interface tables */
3470Sstevel@tonic-gate iftbl_alloc();
3480Sstevel@tonic-gate
3490Sstevel@tonic-gate /* prepare socket connected to the kernel. */
3500Sstevel@tonic-gate rt_sock = socket(PF_ROUTE, SOCK_RAW, AF_INET);
3510Sstevel@tonic-gate if (rt_sock < 0)
3520Sstevel@tonic-gate BADERR(_B_TRUE, "rt_sock = socket()");
3530Sstevel@tonic-gate if (fcntl(rt_sock, F_SETFL, O_NONBLOCK) == -1)
3540Sstevel@tonic-gate logbad(_B_TRUE, "fcntl(rt_sock) O_NONBLOCK: %s",
3550Sstevel@tonic-gate rip_strerror(errno));
3560Sstevel@tonic-gate off = 0;
3570Sstevel@tonic-gate if (setsockopt(rt_sock, SOL_SOCKET, SO_USELOOPBACK,
3580Sstevel@tonic-gate &off, sizeof (off)) < 0)
3590Sstevel@tonic-gate LOGERR("setsockopt(SO_USELOOPBACK,0)");
3600Sstevel@tonic-gate
3610Sstevel@tonic-gate fix_select();
3620Sstevel@tonic-gate
3630Sstevel@tonic-gate
3640Sstevel@tonic-gate if (tracename != NULL) {
3650Sstevel@tonic-gate (void) strlcpy(inittracename, tracename,
3660Sstevel@tonic-gate sizeof (inittracename));
3670Sstevel@tonic-gate set_tracefile(inittracename, "%s", -1);
3680Sstevel@tonic-gate } else {
3690Sstevel@tonic-gate tracelevel_msg("%s", -1); /* turn on tracing to stdio */
3700Sstevel@tonic-gate }
3710Sstevel@tonic-gate
3720Sstevel@tonic-gate bufinit();
3730Sstevel@tonic-gate
3740Sstevel@tonic-gate /* initialize radix tree */
3750Sstevel@tonic-gate rtinit();
3760Sstevel@tonic-gate
3770Sstevel@tonic-gate /*
3780Sstevel@tonic-gate * Pick a random part of the second for our output to minimize
3790Sstevel@tonic-gate * collisions.
3800Sstevel@tonic-gate *
3810Sstevel@tonic-gate * Start broadcasting after hearing from other routers, and
3820Sstevel@tonic-gate * at a random time so a bunch of systems do not get synchronized
3830Sstevel@tonic-gate * after a power failure.
3840Sstevel@tonic-gate *
3850Sstevel@tonic-gate * Since now is the number of seconds since epoch (this is initially
3860Sstevel@tonic-gate * EPOCH seconds), these times are really relative to now.
3870Sstevel@tonic-gate */
3880Sstevel@tonic-gate intvl_random(&next_bcast, EPOCH+MIN_WAITTIME, EPOCH+SUPPLY_INTERVAL);
3890Sstevel@tonic-gate age_timer.tv_usec = next_bcast.tv_usec;
3900Sstevel@tonic-gate age_timer.tv_sec = EPOCH+MIN_WAITTIME;
3910Sstevel@tonic-gate rdisc_timer = next_bcast;
3920Sstevel@tonic-gate ifscan_timer.tv_usec = next_bcast.tv_usec;
3930Sstevel@tonic-gate
3940Sstevel@tonic-gate /*
3950Sstevel@tonic-gate * Open the global rip socket. From now on, this socket can be
3960Sstevel@tonic-gate * assumed to be open. It will remain open until in.routed
3970Sstevel@tonic-gate * exits.
3980Sstevel@tonic-gate */
3990Sstevel@tonic-gate rip_sock = open_rip_sock();
4000Sstevel@tonic-gate
4010Sstevel@tonic-gate /*
4020Sstevel@tonic-gate * Collect an initial view of the world by checking the interface
4030Sstevel@tonic-gate * configuration and the kludge file.
4040Sstevel@tonic-gate *
4050Sstevel@tonic-gate * gwkludge() could call addroutefordefault(), resulting in a call to
4060Sstevel@tonic-gate * iflookup, and thus ifscan() to find the physical interfaces.
4070Sstevel@tonic-gate * ifscan() will attempt to use the rip_sock in order to join
4080Sstevel@tonic-gate * mcast groups, so gwkludge *must* be called after opening
4090Sstevel@tonic-gate * the rip_sock.
4100Sstevel@tonic-gate */
4110Sstevel@tonic-gate gwkludge();
4120Sstevel@tonic-gate
4130Sstevel@tonic-gate ifscan();
4140Sstevel@tonic-gate
4150Sstevel@tonic-gate /* Ask for routes */
4160Sstevel@tonic-gate rip_query();
4170Sstevel@tonic-gate rdisc_sol();
4180Sstevel@tonic-gate
4190Sstevel@tonic-gate /* Now turn off stdio if not tracing */
4200Sstevel@tonic-gate if (new_tracelevel == 0)
4210Sstevel@tonic-gate trace_close(background);
4220Sstevel@tonic-gate
4230Sstevel@tonic-gate /* Loop until a fatal error occurs, listening and broadcasting. */
4240Sstevel@tonic-gate for (;;) {
4250Sstevel@tonic-gate prev_clk = clk;
4260Sstevel@tonic-gate if (gettimeofday(&clk, 0) == -1) {
4270Sstevel@tonic-gate logbad(_B_FALSE, "gettimeofday: %s",
4280Sstevel@tonic-gate rip_strerror(errno));
4290Sstevel@tonic-gate }
4300Sstevel@tonic-gate if (prev_clk.tv_sec == clk.tv_sec &&
4310Sstevel@tonic-gate prev_clk.tv_usec == clk.tv_usec+usec_fudge) {
4320Sstevel@tonic-gate /*
4330Sstevel@tonic-gate * Much of `in.routed` depends on time always advancing.
4340Sstevel@tonic-gate * On systems that do not guarantee that gettimeofday()
4350Sstevel@tonic-gate * produces unique timestamps even if called within
4360Sstevel@tonic-gate * a single tick, use trickery like that in classic
4370Sstevel@tonic-gate * BSD kernels.
4380Sstevel@tonic-gate */
4390Sstevel@tonic-gate clk.tv_usec += ++usec_fudge;
4400Sstevel@tonic-gate
4410Sstevel@tonic-gate } else {
4420Sstevel@tonic-gate time_t dt;
4430Sstevel@tonic-gate
4440Sstevel@tonic-gate usec_fudge = 0;
4450Sstevel@tonic-gate
4460Sstevel@tonic-gate timevalsub(&result, &clk, &prev_clk);
4470Sstevel@tonic-gate if (result.tv_sec < 0 || result.tv_sec >
4480Sstevel@tonic-gate select_timeout.tv_sec + 5) {
4490Sstevel@tonic-gate /*
4500Sstevel@tonic-gate * Deal with time changes before other
4510Sstevel@tonic-gate * housekeeping to keep everything straight.
4520Sstevel@tonic-gate */
4530Sstevel@tonic-gate dt = result.tv_sec;
4540Sstevel@tonic-gate if (dt > 0)
4550Sstevel@tonic-gate dt -= select_timeout.tv_sec;
4560Sstevel@tonic-gate trace_act("time changed by %d sec", (int)dt);
4570Sstevel@tonic-gate epoch.tv_sec += dt;
4580Sstevel@tonic-gate }
4590Sstevel@tonic-gate }
4600Sstevel@tonic-gate timevalsub(&now, &clk, &epoch);
4610Sstevel@tonic-gate now_stale = now.tv_sec - STALE_TIME;
4620Sstevel@tonic-gate now_expire = now.tv_sec - EXPIRE_TIME;
4630Sstevel@tonic-gate now_garbage = now.tv_sec - GARBAGE_TIME;
4640Sstevel@tonic-gate
4650Sstevel@tonic-gate /* deal with signals that should affect tracing */
4660Sstevel@tonic-gate set_tracelevel();
4670Sstevel@tonic-gate
4680Sstevel@tonic-gate if (stopint != 0) {
4690Sstevel@tonic-gate trace_off("exiting with signal %d", stopint);
4700Sstevel@tonic-gate break;
4710Sstevel@tonic-gate }
4720Sstevel@tonic-gate
4730Sstevel@tonic-gate /* look for new or dead interfaces */
4740Sstevel@tonic-gate timevalsub(&select_timeout, &ifscan_timer, &now);
4750Sstevel@tonic-gate if (select_timeout.tv_sec <= 0) {
4760Sstevel@tonic-gate select_timeout.tv_sec = 0;
4770Sstevel@tonic-gate ifscan();
4780Sstevel@tonic-gate rip_query();
4790Sstevel@tonic-gate continue;
4800Sstevel@tonic-gate }
4810Sstevel@tonic-gate
4820Sstevel@tonic-gate /*
4830Sstevel@tonic-gate * Check the kernel table occassionally for mysteriously
4840Sstevel@tonic-gate * evaporated routes
4850Sstevel@tonic-gate */
4860Sstevel@tonic-gate timevalsub(&result, &sync_kern_timer, &now);
4870Sstevel@tonic-gate if (result.tv_sec <= 0) {
4880Sstevel@tonic-gate sync_kern();
4890Sstevel@tonic-gate sync_kern_timer.tv_sec = (now.tv_sec
4900Sstevel@tonic-gate + CHECK_QUIET_INTERVAL);
4910Sstevel@tonic-gate continue;
4920Sstevel@tonic-gate }
4930Sstevel@tonic-gate if (timercmp(&result, &select_timeout, < /* */))
4940Sstevel@tonic-gate select_timeout = result;
4950Sstevel@tonic-gate
4960Sstevel@tonic-gate /* If it is time, then broadcast our routes. */
4970Sstevel@tonic-gate if (should_supply(NULL) || advertise_mhome) {
4980Sstevel@tonic-gate timevalsub(&result, &next_bcast, &now);
4990Sstevel@tonic-gate if (result.tv_sec <= 0) {
5000Sstevel@tonic-gate /*
5010Sstevel@tonic-gate * Synchronize the aging and broadcast
5020Sstevel@tonic-gate * timers to minimize awakenings
5030Sstevel@tonic-gate */
5040Sstevel@tonic-gate age(0);
5050Sstevel@tonic-gate age_peer_info();
5060Sstevel@tonic-gate
5070Sstevel@tonic-gate rip_bcast(0);
5080Sstevel@tonic-gate
5090Sstevel@tonic-gate /*
5100Sstevel@tonic-gate * It is desirable to send routing updates
5110Sstevel@tonic-gate * regularly. So schedule the next update
5120Sstevel@tonic-gate * 30 seconds after the previous one was
5130Sstevel@tonic-gate * scheduled, instead of 30 seconds after
5140Sstevel@tonic-gate * the previous update was finished.
5150Sstevel@tonic-gate * Even if we just started after discovering
5160Sstevel@tonic-gate * a 2nd interface or were otherwise delayed,
5170Sstevel@tonic-gate * pick a 30-second aniversary of the
5180Sstevel@tonic-gate * original broadcast time.
5190Sstevel@tonic-gate */
5200Sstevel@tonic-gate n = 1 + (0-result.tv_sec)/SUPPLY_INTERVAL;
5210Sstevel@tonic-gate next_bcast.tv_sec += n*SUPPLY_INTERVAL;
5220Sstevel@tonic-gate
5230Sstevel@tonic-gate continue;
5240Sstevel@tonic-gate }
5250Sstevel@tonic-gate
5260Sstevel@tonic-gate if (timercmp(&result, &select_timeout, < /* */))
5270Sstevel@tonic-gate select_timeout = result;
5280Sstevel@tonic-gate }
5290Sstevel@tonic-gate
5300Sstevel@tonic-gate /*
5310Sstevel@tonic-gate * If we need a flash update, either do it now or
5320Sstevel@tonic-gate * set the delay to end when it is time.
5330Sstevel@tonic-gate *
5340Sstevel@tonic-gate * If we are within MIN_WAITTIME seconds of a full update,
5350Sstevel@tonic-gate * do not bother.
5360Sstevel@tonic-gate */
5370Sstevel@tonic-gate if (need_flash && should_supply(NULL) &&
5380Sstevel@tonic-gate no_flash.tv_sec+MIN_WAITTIME < next_bcast.tv_sec) {
5390Sstevel@tonic-gate /* accurate to the millisecond */
5400Sstevel@tonic-gate if (!timercmp(&no_flash, &now, > /* */))
5410Sstevel@tonic-gate rip_bcast(1);
5420Sstevel@tonic-gate timevalsub(&result, &no_flash, &now);
5430Sstevel@tonic-gate if (timercmp(&result, &select_timeout, < /* */))
5440Sstevel@tonic-gate select_timeout = result;
5450Sstevel@tonic-gate }
5460Sstevel@tonic-gate
5470Sstevel@tonic-gate /* trigger the main aging timer. */
5480Sstevel@tonic-gate timevalsub(&result, &age_timer, &now);
5490Sstevel@tonic-gate if (result.tv_sec <= 0) {
5500Sstevel@tonic-gate age(0);
5510Sstevel@tonic-gate continue;
5520Sstevel@tonic-gate }
5530Sstevel@tonic-gate if (timercmp(&result, &select_timeout, < /* */))
5540Sstevel@tonic-gate select_timeout = result;
5550Sstevel@tonic-gate
5560Sstevel@tonic-gate /* update the kernel routing table */
5570Sstevel@tonic-gate timevalsub(&result, &need_kern, &now);
5580Sstevel@tonic-gate if (result.tv_sec <= 0) {
5590Sstevel@tonic-gate age(0);
5600Sstevel@tonic-gate continue;
5610Sstevel@tonic-gate }
5620Sstevel@tonic-gate if (timercmp(&result, &select_timeout, < /* */))
5630Sstevel@tonic-gate select_timeout = result;
5640Sstevel@tonic-gate
5650Sstevel@tonic-gate /*
5660Sstevel@tonic-gate * take care of router discovery. We compare timeval
5670Sstevel@tonic-gate * structures here to have millisecond granularity.
5680Sstevel@tonic-gate */
5690Sstevel@tonic-gate if (!timercmp(&rdisc_timer, &now, > /* */)) {
5700Sstevel@tonic-gate rdisc_age(0);
5710Sstevel@tonic-gate continue;
5720Sstevel@tonic-gate }
5730Sstevel@tonic-gate timevalsub(&result, &rdisc_timer, &now);
5740Sstevel@tonic-gate if (timercmp(&result, &select_timeout, < /* */))
5750Sstevel@tonic-gate select_timeout = result;
5760Sstevel@tonic-gate
5770Sstevel@tonic-gate /*
5780Sstevel@tonic-gate * Well-known bit of select(3c) silliness inherited
5790Sstevel@tonic-gate * from BSD: anything over 100 million seconds is
5800Sstevel@tonic-gate * considered an "error." Reset that to zero.
5810Sstevel@tonic-gate */
5820Sstevel@tonic-gate if (select_timeout.tv_sec > 100000000)
5830Sstevel@tonic-gate select_timeout.tv_sec = 0;
5840Sstevel@tonic-gate
5850Sstevel@tonic-gate /* wait for input or a timer to expire. */
5860Sstevel@tonic-gate trace_flush();
5870Sstevel@tonic-gate ibits = fdbits;
5880Sstevel@tonic-gate n = select(sock_max, &ibits, 0, 0, &select_timeout);
5890Sstevel@tonic-gate if (n <= 0) {
5900Sstevel@tonic-gate if (n < 0 && errno != EINTR && errno != EAGAIN)
5910Sstevel@tonic-gate BADERR(_B_TRUE, "select");
5920Sstevel@tonic-gate continue;
5930Sstevel@tonic-gate }
5940Sstevel@tonic-gate
5950Sstevel@tonic-gate if (FD_ISSET(rt_sock, &ibits)) {
5960Sstevel@tonic-gate read_rt();
5970Sstevel@tonic-gate n--;
5980Sstevel@tonic-gate }
5990Sstevel@tonic-gate if (rdisc_sock >= 0 && FD_ISSET(rdisc_sock, &ibits)) {
6000Sstevel@tonic-gate read_d();
6010Sstevel@tonic-gate n--;
6020Sstevel@tonic-gate }
6033293Sapersson if (rdisc_mib_sock >= 0 && FD_ISSET(rdisc_mib_sock, &ibits)) {
6043293Sapersson process_d_mib_sock();
6053293Sapersson n--;
6063293Sapersson }
6070Sstevel@tonic-gate if (rip_sock >= 0 && FD_ISSET(rip_sock, &ibits)) {
6080Sstevel@tonic-gate if (read_rip() == -1) {
6090Sstevel@tonic-gate rip_enabled = _B_FALSE;
6100Sstevel@tonic-gate trace_off("main rip socket failed");
6110Sstevel@tonic-gate (void) close(rip_sock);
6120Sstevel@tonic-gate rip_sock = -1;
6130Sstevel@tonic-gate fix_select();
6140Sstevel@tonic-gate break;
6150Sstevel@tonic-gate }
6160Sstevel@tonic-gate n--;
6170Sstevel@tonic-gate }
6180Sstevel@tonic-gate }
6190Sstevel@tonic-gate rip_bcast(0);
6200Sstevel@tonic-gate rdisc_adv(_B_FALSE);
6210Sstevel@tonic-gate (void) unlink(PATH_PID);
6220Sstevel@tonic-gate return (stopint | 128);
6230Sstevel@tonic-gate }
6240Sstevel@tonic-gate
6250Sstevel@tonic-gate
6260Sstevel@tonic-gate static void
sigalrm(int sig)6270Sstevel@tonic-gate sigalrm(int sig)
6280Sstevel@tonic-gate {
6290Sstevel@tonic-gate /*
6300Sstevel@tonic-gate * Historically, SIGALRM would cause the daemon to check for
6310Sstevel@tonic-gate * new and broken interfaces.
6320Sstevel@tonic-gate */
6330Sstevel@tonic-gate ifscan_timer.tv_sec = now.tv_sec;
6340Sstevel@tonic-gate trace_act("SIGALRM");
6350Sstevel@tonic-gate if (signal(sig, sigalrm) == SIG_ERR)
6360Sstevel@tonic-gate msglog("signal: %s", rip_strerror(errno));
6370Sstevel@tonic-gate }
6380Sstevel@tonic-gate
6390Sstevel@tonic-gate
6400Sstevel@tonic-gate /* watch for fatal signals */
6410Sstevel@tonic-gate static void
sigterm(int sig)6420Sstevel@tonic-gate sigterm(int sig)
6430Sstevel@tonic-gate {
6440Sstevel@tonic-gate stopint = sig;
6450Sstevel@tonic-gate if (signal(sig, SIG_DFL) == SIG_ERR) /* catch it only once */
6460Sstevel@tonic-gate msglog("signal: %s", rip_strerror(errno));
6470Sstevel@tonic-gate }
6480Sstevel@tonic-gate
6490Sstevel@tonic-gate
6500Sstevel@tonic-gate void
fix_select(void)6510Sstevel@tonic-gate fix_select(void)
6520Sstevel@tonic-gate {
6530Sstevel@tonic-gate (void) FD_ZERO(&fdbits);
6540Sstevel@tonic-gate sock_max = 0;
6550Sstevel@tonic-gate
6560Sstevel@tonic-gate FD_SET(rt_sock, &fdbits);
6570Sstevel@tonic-gate if (sock_max <= rt_sock)
6580Sstevel@tonic-gate sock_max = rt_sock+1;
6590Sstevel@tonic-gate if (rip_sock >= 0) {
6600Sstevel@tonic-gate FD_SET(rip_sock, &fdbits);
6610Sstevel@tonic-gate if (sock_max <= rip_sock)
6620Sstevel@tonic-gate sock_max = rip_sock+1;
6630Sstevel@tonic-gate }
6640Sstevel@tonic-gate if (rdisc_sock >= 0) {
6650Sstevel@tonic-gate FD_SET(rdisc_sock, &fdbits);
6660Sstevel@tonic-gate if (sock_max <= rdisc_sock)
6670Sstevel@tonic-gate sock_max = rdisc_sock+1;
6683293Sapersson FD_SET(rdisc_mib_sock, &fdbits);
6693293Sapersson if (sock_max <= rdisc_mib_sock)
6703293Sapersson sock_max = rdisc_mib_sock+1;
6710Sstevel@tonic-gate }
6720Sstevel@tonic-gate }
6730Sstevel@tonic-gate
6740Sstevel@tonic-gate
6750Sstevel@tonic-gate void
fix_sock(int sock,const char * name)6760Sstevel@tonic-gate fix_sock(int sock,
6770Sstevel@tonic-gate const char *name)
6780Sstevel@tonic-gate {
6790Sstevel@tonic-gate int on;
6800Sstevel@tonic-gate #define MIN_SOCKBUF (4*1024)
6810Sstevel@tonic-gate static int rbuf;
6820Sstevel@tonic-gate
6830Sstevel@tonic-gate if (fcntl(sock, F_SETFL, O_NONBLOCK) == -1)
6840Sstevel@tonic-gate logbad(_B_TRUE, "fcntl(%s) O_NONBLOCK: %s", name,
6850Sstevel@tonic-gate rip_strerror(errno));
6860Sstevel@tonic-gate on = 1;
6870Sstevel@tonic-gate if (setsockopt(sock, SOL_SOCKET, SO_BROADCAST, &on, sizeof (on)) < 0)
6880Sstevel@tonic-gate msglog("setsockopt(%s,SO_BROADCAST): %s",
6890Sstevel@tonic-gate name, rip_strerror(errno));
6900Sstevel@tonic-gate
6910Sstevel@tonic-gate if (rbuf >= MIN_SOCKBUF) {
6920Sstevel@tonic-gate if (setsockopt(sock, SOL_SOCKET, SO_RCVBUF,
6930Sstevel@tonic-gate &rbuf, sizeof (rbuf)) < 0)
6940Sstevel@tonic-gate msglog("setsockopt(%s,SO_RCVBUF=%d): %s",
6950Sstevel@tonic-gate name, rbuf, rip_strerror(errno));
6960Sstevel@tonic-gate } else {
6970Sstevel@tonic-gate for (rbuf = 60*1024; ; rbuf -= 4096) {
6980Sstevel@tonic-gate if (setsockopt(sock, SOL_SOCKET, SO_RCVBUF,
6990Sstevel@tonic-gate &rbuf, sizeof (rbuf)) == 0) {
7000Sstevel@tonic-gate trace_act("RCVBUF=%d", rbuf);
7010Sstevel@tonic-gate break;
7020Sstevel@tonic-gate }
7030Sstevel@tonic-gate if (rbuf < MIN_SOCKBUF) {
7040Sstevel@tonic-gate msglog("setsockopt(%s,SO_RCVBUF = %d): %s",
7050Sstevel@tonic-gate name, rbuf, rip_strerror(errno));
7060Sstevel@tonic-gate break;
7070Sstevel@tonic-gate }
7080Sstevel@tonic-gate }
7090Sstevel@tonic-gate }
7100Sstevel@tonic-gate }
7110Sstevel@tonic-gate
7120Sstevel@tonic-gate
7130Sstevel@tonic-gate /*
7140Sstevel@tonic-gate * Open and return the global rip socket. It is guaranteed to return
7150Sstevel@tonic-gate * a good file descriptor.
7160Sstevel@tonic-gate */
7170Sstevel@tonic-gate static int
open_rip_sock()7180Sstevel@tonic-gate open_rip_sock()
7190Sstevel@tonic-gate {
7200Sstevel@tonic-gate struct sockaddr_in sin;
7210Sstevel@tonic-gate unsigned char ttl;
7220Sstevel@tonic-gate int s;
7230Sstevel@tonic-gate int on = 1;
7240Sstevel@tonic-gate
7250Sstevel@tonic-gate
7260Sstevel@tonic-gate if ((s = socket(PF_INET, SOCK_DGRAM, 0)) < 0)
7270Sstevel@tonic-gate BADERR(_B_TRUE, "rip_sock = socket()");
7280Sstevel@tonic-gate
7290Sstevel@tonic-gate (void) memset(&sin, 0, sizeof (sin));
7300Sstevel@tonic-gate sin.sin_family = AF_INET;
7310Sstevel@tonic-gate sin.sin_port = htons(RIP_PORT);
7320Sstevel@tonic-gate sin.sin_addr.s_addr = INADDR_ANY;
7330Sstevel@tonic-gate if (bind(s, (struct sockaddr *)&sin, sizeof (sin)) < 0) {
7340Sstevel@tonic-gate BADERR(_B_FALSE, "bind(rip_sock)");
7350Sstevel@tonic-gate }
7360Sstevel@tonic-gate fix_sock(s, "rip_sock");
7370Sstevel@tonic-gate
7380Sstevel@tonic-gate ttl = 1;
7390Sstevel@tonic-gate if (setsockopt(s, IPPROTO_IP, IP_MULTICAST_TTL,
7400Sstevel@tonic-gate &ttl, sizeof (ttl)) < 0)
7410Sstevel@tonic-gate DBGERR(_B_TRUE, "rip_sock setsockopt(IP_MULTICAST_TTL)");
7420Sstevel@tonic-gate
7430Sstevel@tonic-gate if (setsockopt(s, IPPROTO_IP, IP_RECVIF, &on, sizeof (on)))
7440Sstevel@tonic-gate BADERR(_B_FALSE, "setsockopt(IP_RECVIF)");
7450Sstevel@tonic-gate
7460Sstevel@tonic-gate return (s);
7470Sstevel@tonic-gate }
7480Sstevel@tonic-gate
7490Sstevel@tonic-gate
7500Sstevel@tonic-gate /*
7510Sstevel@tonic-gate * Disable RIP. Note that we don't close the global rip socket since
7520Sstevel@tonic-gate * it is used even when RIP is disabled to receive and answer certain
7530Sstevel@tonic-gate * queries.
7540Sstevel@tonic-gate */
7550Sstevel@tonic-gate void
rip_off(void)7560Sstevel@tonic-gate rip_off(void)
7570Sstevel@tonic-gate {
7580Sstevel@tonic-gate struct ip_mreq m;
7590Sstevel@tonic-gate struct interface *ifp;
7600Sstevel@tonic-gate char addrstr[INET_ADDRSTRLEN];
7610Sstevel@tonic-gate
7620Sstevel@tonic-gate if (rip_enabled && !mhome) {
7630Sstevel@tonic-gate trace_act("turn off RIP");
7640Sstevel@tonic-gate
7650Sstevel@tonic-gate /*
7660Sstevel@tonic-gate * Unsubscribe from the 224.0.0.9 RIP multicast
7670Sstevel@tonic-gate * group address
7680Sstevel@tonic-gate */
7690Sstevel@tonic-gate for (ifp = ifnet; ifp != NULL; ifp = ifp->int_next) {
7700Sstevel@tonic-gate if ((ifp->int_if_flags & IFF_MULTICAST) &&
7710Sstevel@tonic-gate !IS_IFF_QUIET(ifp->int_if_flags) &&
7720Sstevel@tonic-gate !IS_RIP_IN_OFF(ifp->int_state) &&
7730Sstevel@tonic-gate !(ifp->int_state & IS_DUP)) {
7740Sstevel@tonic-gate m.imr_multiaddr.s_addr =
7750Sstevel@tonic-gate htonl(INADDR_RIP_GROUP);
7760Sstevel@tonic-gate m.imr_interface.s_addr =
7770Sstevel@tonic-gate (ifp->int_if_flags & IFF_POINTOPOINT) ?
7780Sstevel@tonic-gate ifp->int_dstaddr : ifp->int_addr;
7790Sstevel@tonic-gate (void) strlcpy(addrstr,
7800Sstevel@tonic-gate inet_ntoa(m.imr_multiaddr),
7810Sstevel@tonic-gate sizeof (addrstr));
7820Sstevel@tonic-gate if (setsockopt(rip_sock, IPPROTO_IP,
7830Sstevel@tonic-gate IP_DROP_MEMBERSHIP, &m,
7840Sstevel@tonic-gate sizeof (m)) < 0 &&
7850Sstevel@tonic-gate errno != EADDRNOTAVAIL && errno != ENOENT)
7860Sstevel@tonic-gate writelog(LOG_WARNING,
7870Sstevel@tonic-gate "%s: setsockopt(IP_DROP_MEMBERSHIP "
7880Sstevel@tonic-gate "%s, %s): %s", ifp->int_name,
7890Sstevel@tonic-gate addrstr, inet_ntoa(m.imr_interface),
7900Sstevel@tonic-gate rip_strerror(errno));
7910Sstevel@tonic-gate }
7920Sstevel@tonic-gate }
7930Sstevel@tonic-gate rip_enabled = _B_FALSE;
7940Sstevel@tonic-gate
7950Sstevel@tonic-gate age(0);
7960Sstevel@tonic-gate }
7970Sstevel@tonic-gate }
7980Sstevel@tonic-gate
7990Sstevel@tonic-gate
8000Sstevel@tonic-gate /* turn on RIP multicast input via an interface */
8010Sstevel@tonic-gate void
rip_mcast_on(struct interface * ifp)8020Sstevel@tonic-gate rip_mcast_on(struct interface *ifp)
8030Sstevel@tonic-gate {
8040Sstevel@tonic-gate struct ip_mreq m;
8050Sstevel@tonic-gate
8060Sstevel@tonic-gate if (!IS_RIP_IN_OFF(ifp->int_state) &&
8070Sstevel@tonic-gate (ifp->int_if_flags & IFF_MULTICAST) &&
8080Sstevel@tonic-gate !IS_IFF_QUIET(ifp->int_if_flags) &&
8090Sstevel@tonic-gate !(ifp->int_state & IS_DUP)) {
8100Sstevel@tonic-gate m.imr_multiaddr.s_addr = htonl(INADDR_RIP_GROUP);
8110Sstevel@tonic-gate m.imr_interface.s_addr = (ifp->int_if_flags & IFF_POINTOPOINT) ?
8120Sstevel@tonic-gate ifp->int_dstaddr : ifp->int_addr;
8130Sstevel@tonic-gate if ((setsockopt(rip_sock, IPPROTO_IP, IP_ADD_MEMBERSHIP,
8140Sstevel@tonic-gate &m, sizeof (m)) < 0) && !(ifp->int_state & IS_BROKE))
8150Sstevel@tonic-gate writelog(LOG_WARNING,
8160Sstevel@tonic-gate "Could not join 224.0.0.9 on interface %s: %s",
8170Sstevel@tonic-gate ifp->int_name, rip_strerror(errno));
8180Sstevel@tonic-gate }
8190Sstevel@tonic-gate }
8200Sstevel@tonic-gate
8210Sstevel@tonic-gate /* turn off RIP multicast input via an interface */
8220Sstevel@tonic-gate void
rip_mcast_off(struct interface * ifp)8230Sstevel@tonic-gate rip_mcast_off(struct interface *ifp)
8240Sstevel@tonic-gate {
8250Sstevel@tonic-gate struct ip_mreq m;
8260Sstevel@tonic-gate
8270Sstevel@tonic-gate if ((ifp->int_if_flags & IFF_MULTICAST) &&
8280Sstevel@tonic-gate !IS_IFF_QUIET(ifp->int_if_flags) && rip_enabled) {
8290Sstevel@tonic-gate m.imr_multiaddr.s_addr = htonl(INADDR_RIP_GROUP);
8300Sstevel@tonic-gate m.imr_interface.s_addr = (ifp->int_if_flags & IFF_POINTOPOINT) ?
8310Sstevel@tonic-gate ifp->int_dstaddr : ifp->int_addr;
8320Sstevel@tonic-gate if ((setsockopt(rip_sock, IPPROTO_IP, IP_DROP_MEMBERSHIP,
8330Sstevel@tonic-gate &m, sizeof (m)) < 0) && errno != EADDRNOTAVAIL &&
8340Sstevel@tonic-gate errno != ENOENT)
8350Sstevel@tonic-gate writelog(LOG_WARNING,
8360Sstevel@tonic-gate "setsockopt(IP_DROP_MEMBERSHIP RIP) for %s: %s",
8370Sstevel@tonic-gate ifp->int_name, rip_strerror(errno));
8380Sstevel@tonic-gate }
8390Sstevel@tonic-gate }
8400Sstevel@tonic-gate
8410Sstevel@tonic-gate /* enable RIP */
8420Sstevel@tonic-gate void
rip_on(struct interface * ifp)8430Sstevel@tonic-gate rip_on(struct interface *ifp)
8440Sstevel@tonic-gate {
8450Sstevel@tonic-gate /*
8460Sstevel@tonic-gate * If RIP is already enabled, only start receiving
8470Sstevel@tonic-gate * multicasts for this interface.
8480Sstevel@tonic-gate */
8490Sstevel@tonic-gate if (rip_enabled) {
8500Sstevel@tonic-gate if (ifp != NULL)
8510Sstevel@tonic-gate rip_mcast_on(ifp);
8520Sstevel@tonic-gate return;
8530Sstevel@tonic-gate }
8540Sstevel@tonic-gate
8550Sstevel@tonic-gate /*
8560Sstevel@tonic-gate * If RIP is disabled and it makes sense to enable it, then enable
8570Sstevel@tonic-gate * it on all of the interfaces. It makes sense if either router
8580Sstevel@tonic-gate * discovery is off, or if router discovery is on and at most one
8590Sstevel@tonic-gate * interface is doing RIP.
8600Sstevel@tonic-gate */
8610Sstevel@tonic-gate if (rip_interfaces > 0 && (!rdisc_ok || rip_interfaces > 1)) {
8620Sstevel@tonic-gate trace_act("turn on RIP");
8630Sstevel@tonic-gate
8640Sstevel@tonic-gate rip_enabled = _B_TRUE;
8650Sstevel@tonic-gate rip_sock_interface = NULL;
8660Sstevel@tonic-gate
8670Sstevel@tonic-gate /* Do not advertise anything until we have heard something */
8680Sstevel@tonic-gate if (next_bcast.tv_sec < now.tv_sec+MIN_WAITTIME)
8690Sstevel@tonic-gate next_bcast.tv_sec = now.tv_sec+MIN_WAITTIME;
8700Sstevel@tonic-gate
8710Sstevel@tonic-gate for (ifp = ifnet; ifp != NULL; ifp = ifp->int_next) {
8720Sstevel@tonic-gate ifp->int_query_time = NEVER;
8730Sstevel@tonic-gate rip_mcast_on(ifp);
8740Sstevel@tonic-gate }
8750Sstevel@tonic-gate ifscan_timer.tv_sec = now.tv_sec;
8760Sstevel@tonic-gate }
8770Sstevel@tonic-gate
8780Sstevel@tonic-gate fix_select();
8790Sstevel@tonic-gate }
8800Sstevel@tonic-gate
8810Sstevel@tonic-gate
8820Sstevel@tonic-gate /* die if malloc(3) fails */
8830Sstevel@tonic-gate void *
rtmalloc(size_t size,const char * msg)8840Sstevel@tonic-gate rtmalloc(size_t size,
8850Sstevel@tonic-gate const char *msg)
8860Sstevel@tonic-gate {
8870Sstevel@tonic-gate void *p = malloc(size);
8880Sstevel@tonic-gate if (p == NULL)
8890Sstevel@tonic-gate logbad(_B_TRUE, "malloc(%lu) failed in %s: %s", (ulong_t)size,
8900Sstevel@tonic-gate msg, rip_strerror(errno));
8910Sstevel@tonic-gate return (p);
8920Sstevel@tonic-gate }
8930Sstevel@tonic-gate
8940Sstevel@tonic-gate
8950Sstevel@tonic-gate /* get a random instant in an interval */
8960Sstevel@tonic-gate void
intvl_random(struct timeval * tp,ulong_t lo,ulong_t hi)8970Sstevel@tonic-gate intvl_random(struct timeval *tp, /* put value here */
8980Sstevel@tonic-gate ulong_t lo, /* value is after this second */
8990Sstevel@tonic-gate ulong_t hi) /* and before this */
9000Sstevel@tonic-gate {
9010Sstevel@tonic-gate tp->tv_sec = (time_t)(hi == lo ? lo : (lo + random() % ((hi - lo))));
9020Sstevel@tonic-gate tp->tv_usec = random() % 1000000;
9030Sstevel@tonic-gate }
9040Sstevel@tonic-gate
9050Sstevel@tonic-gate
9060Sstevel@tonic-gate void
timevaladd(struct timeval * t1,struct timeval * t2)9070Sstevel@tonic-gate timevaladd(struct timeval *t1,
9080Sstevel@tonic-gate struct timeval *t2)
9090Sstevel@tonic-gate {
9100Sstevel@tonic-gate
9110Sstevel@tonic-gate t1->tv_sec += t2->tv_sec;
9120Sstevel@tonic-gate if ((t1->tv_usec += t2->tv_usec) >= 1000000) {
9130Sstevel@tonic-gate t1->tv_sec++;
9140Sstevel@tonic-gate t1->tv_usec -= 1000000;
9150Sstevel@tonic-gate }
9160Sstevel@tonic-gate }
9170Sstevel@tonic-gate
9180Sstevel@tonic-gate
9190Sstevel@tonic-gate /* t1 = t2 - t3 */
9200Sstevel@tonic-gate static void
timevalsub(struct timeval * t1,struct timeval * t2,struct timeval * t3)9210Sstevel@tonic-gate timevalsub(struct timeval *t1,
9220Sstevel@tonic-gate struct timeval *t2,
9230Sstevel@tonic-gate struct timeval *t3)
9240Sstevel@tonic-gate {
9250Sstevel@tonic-gate t1->tv_sec = t2->tv_sec - t3->tv_sec;
9260Sstevel@tonic-gate if ((t1->tv_usec = t2->tv_usec - t3->tv_usec) < 0) {
9270Sstevel@tonic-gate t1->tv_sec--;
9280Sstevel@tonic-gate t1->tv_usec += 1000000;
9290Sstevel@tonic-gate }
9300Sstevel@tonic-gate }
9310Sstevel@tonic-gate
9320Sstevel@tonic-gate static void
do_openlog(void)9330Sstevel@tonic-gate do_openlog(void)
9340Sstevel@tonic-gate {
9350Sstevel@tonic-gate openlog_done = _B_TRUE;
9360Sstevel@tonic-gate openlog("in.routed", LOG_PID | LOG_ODELAY, LOG_DAEMON);
9370Sstevel@tonic-gate }
9380Sstevel@tonic-gate
9390Sstevel@tonic-gate /* put a LOG_ERR message into the system log */
9400Sstevel@tonic-gate void
msglog(const char * p,...)9410Sstevel@tonic-gate msglog(const char *p, ...)
9420Sstevel@tonic-gate {
9430Sstevel@tonic-gate va_list args;
9440Sstevel@tonic-gate
9450Sstevel@tonic-gate trace_flush();
9460Sstevel@tonic-gate
9470Sstevel@tonic-gate if (!openlog_done)
9480Sstevel@tonic-gate do_openlog();
9490Sstevel@tonic-gate va_start(args, p);
9500Sstevel@tonic-gate vsyslog(LOG_ERR, p, args);
9510Sstevel@tonic-gate
9520Sstevel@tonic-gate if (ftrace != 0) {
9530Sstevel@tonic-gate if (ftrace == stdout)
9540Sstevel@tonic-gate (void) fputs("in.routed: ", ftrace);
9550Sstevel@tonic-gate (void) vfprintf(ftrace, p, args);
9560Sstevel@tonic-gate (void) fputc('\n', ftrace);
9570Sstevel@tonic-gate }
9580Sstevel@tonic-gate }
9590Sstevel@tonic-gate
9600Sstevel@tonic-gate
9610Sstevel@tonic-gate /*
9620Sstevel@tonic-gate * Put a message about a bad system into the system log if
9630Sstevel@tonic-gate * we have not complained about it recently.
9640Sstevel@tonic-gate *
9650Sstevel@tonic-gate * It is desirable to complain about all bad systems, but not too often.
9660Sstevel@tonic-gate * In the worst case, it is not practical to keep track of all bad systems.
9670Sstevel@tonic-gate * For example, there can be many systems with the wrong password.
9680Sstevel@tonic-gate */
9690Sstevel@tonic-gate void
msglim(struct msg_limit * lim,in_addr_t addr,const char * p,...)9700Sstevel@tonic-gate msglim(struct msg_limit *lim, in_addr_t addr, const char *p, ...)
9710Sstevel@tonic-gate {
9720Sstevel@tonic-gate va_list args;
9730Sstevel@tonic-gate int i;
9740Sstevel@tonic-gate struct msg_sub *ms1, *ms;
9750Sstevel@tonic-gate const char *p1;
9760Sstevel@tonic-gate
9770Sstevel@tonic-gate va_start(args, p);
9780Sstevel@tonic-gate
9790Sstevel@tonic-gate /*
9800Sstevel@tonic-gate * look for the oldest slot in the table
9810Sstevel@tonic-gate * or the slot for the bad router.
9820Sstevel@tonic-gate */
9830Sstevel@tonic-gate ms = ms1 = lim->subs;
9840Sstevel@tonic-gate for (i = MSG_SUBJECT_N; ; i--, ms1++) {
9850Sstevel@tonic-gate if (i == 0) {
9860Sstevel@tonic-gate /* Reuse a slot at most once every 10 minutes. */
9870Sstevel@tonic-gate if (lim->reuse > now.tv_sec) {
9880Sstevel@tonic-gate ms = NULL;
9890Sstevel@tonic-gate } else {
9900Sstevel@tonic-gate lim->reuse = now.tv_sec + 10*60;
9910Sstevel@tonic-gate }
9920Sstevel@tonic-gate break;
9930Sstevel@tonic-gate }
9940Sstevel@tonic-gate if (ms->addr == addr) {
9950Sstevel@tonic-gate /*
9960Sstevel@tonic-gate * Repeat a complaint about a given system at
9970Sstevel@tonic-gate * most once an hour.
9980Sstevel@tonic-gate */
9990Sstevel@tonic-gate if (ms->until > now.tv_sec)
10000Sstevel@tonic-gate ms = NULL;
10010Sstevel@tonic-gate break;
10020Sstevel@tonic-gate }
10030Sstevel@tonic-gate if (ms->until < ms1->until)
10040Sstevel@tonic-gate ms = ms1;
10050Sstevel@tonic-gate }
10060Sstevel@tonic-gate if (ms != NULL) {
10070Sstevel@tonic-gate ms->addr = addr;
10080Sstevel@tonic-gate ms->until = now.tv_sec + 60*60; /* 60 minutes */
10090Sstevel@tonic-gate
10100Sstevel@tonic-gate if (!openlog_done)
10110Sstevel@tonic-gate do_openlog();
10120Sstevel@tonic-gate trace_flush();
10130Sstevel@tonic-gate for (p1 = p; *p1 == ' '; p1++)
10140Sstevel@tonic-gate continue;
10150Sstevel@tonic-gate vsyslog(LOG_ERR, p1, args);
10160Sstevel@tonic-gate }
10170Sstevel@tonic-gate
10180Sstevel@tonic-gate /* always display the message if tracing */
10190Sstevel@tonic-gate if (ftrace != 0) {
10200Sstevel@tonic-gate (void) vfprintf(ftrace, p, args);
10210Sstevel@tonic-gate (void) fputc('\n', ftrace);
10220Sstevel@tonic-gate }
10230Sstevel@tonic-gate }
10240Sstevel@tonic-gate
10250Sstevel@tonic-gate
10260Sstevel@tonic-gate void
logbad(boolean_t dump,const char * p,...)10270Sstevel@tonic-gate logbad(boolean_t dump, const char *p, ...)
10280Sstevel@tonic-gate {
10290Sstevel@tonic-gate va_list args;
10300Sstevel@tonic-gate
10310Sstevel@tonic-gate trace_flush();
10320Sstevel@tonic-gate
10330Sstevel@tonic-gate if (!openlog_done)
10340Sstevel@tonic-gate do_openlog();
10350Sstevel@tonic-gate va_start(args, p);
10360Sstevel@tonic-gate vsyslog(LOG_ERR, p, args);
10370Sstevel@tonic-gate
10380Sstevel@tonic-gate (void) fputs(gettext("in.routed: "), stderr);
10390Sstevel@tonic-gate (void) vfprintf(stderr, p, args);
10400Sstevel@tonic-gate (void) fputs(gettext("; giving up\n"), stderr);
10410Sstevel@tonic-gate (void) fflush(stderr);
10420Sstevel@tonic-gate
10430Sstevel@tonic-gate if (dump)
10440Sstevel@tonic-gate abort();
10450Sstevel@tonic-gate exit(EXIT_FAILURE);
10460Sstevel@tonic-gate }
10470Sstevel@tonic-gate
10480Sstevel@tonic-gate /* put a message into the system log */
10490Sstevel@tonic-gate void
writelog(int level,const char * p,...)10500Sstevel@tonic-gate writelog(int level, const char *p, ...)
10510Sstevel@tonic-gate {
10520Sstevel@tonic-gate va_list args;
10530Sstevel@tonic-gate
10540Sstevel@tonic-gate trace_flush();
10550Sstevel@tonic-gate
10560Sstevel@tonic-gate if (!openlog_done)
10570Sstevel@tonic-gate do_openlog();
10580Sstevel@tonic-gate va_start(args, p);
10590Sstevel@tonic-gate vsyslog(level, p, args);
10600Sstevel@tonic-gate
10610Sstevel@tonic-gate if (ftrace != 0) {
10620Sstevel@tonic-gate if (ftrace == stdout)
10630Sstevel@tonic-gate (void) fputs("in.routed: ", ftrace);
10640Sstevel@tonic-gate (void) vfprintf(ftrace, p, args);
10650Sstevel@tonic-gate (void) fputc('\n', ftrace);
10660Sstevel@tonic-gate }
10670Sstevel@tonic-gate }
1068