1*0Sstevel@tonic-gate /* 2*0Sstevel@tonic-gate * Copyright 2004 Sun Microsystems, Inc. All rights reserved. 3*0Sstevel@tonic-gate * Use is subject to license terms. 4*0Sstevel@tonic-gate * 5*0Sstevel@tonic-gate * Copyright (c) 1983, 1988, 1993 6*0Sstevel@tonic-gate * The Regents of the University of California. All rights reserved. 7*0Sstevel@tonic-gate * 8*0Sstevel@tonic-gate * Redistribution and use in source and binary forms, with or without 9*0Sstevel@tonic-gate * modification, are permitted provided that the following conditions 10*0Sstevel@tonic-gate * are met: 11*0Sstevel@tonic-gate * 1. Redistributions of source code must retain the above copyright 12*0Sstevel@tonic-gate * notice, this list of conditions and the following disclaimer. 13*0Sstevel@tonic-gate * 2. Redistributions in binary form must reproduce the above copyright 14*0Sstevel@tonic-gate * notice, this list of conditions and the following disclaimer in the 15*0Sstevel@tonic-gate * documentation and/or other materials provided with the distribution. 16*0Sstevel@tonic-gate * 3. All advertising materials mentioning features or use of this software 17*0Sstevel@tonic-gate * must display the following acknowledgment: 18*0Sstevel@tonic-gate * This product includes software developed by the University of 19*0Sstevel@tonic-gate * California, Berkeley and its contributors. 20*0Sstevel@tonic-gate * 4. Neither the name of the University nor the names of its contributors 21*0Sstevel@tonic-gate * may be used to endorse or promote products derived from this software 22*0Sstevel@tonic-gate * without specific prior written permission. 23*0Sstevel@tonic-gate * 24*0Sstevel@tonic-gate * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 25*0Sstevel@tonic-gate * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 26*0Sstevel@tonic-gate * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 27*0Sstevel@tonic-gate * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 28*0Sstevel@tonic-gate * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 29*0Sstevel@tonic-gate * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 30*0Sstevel@tonic-gate * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 31*0Sstevel@tonic-gate * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 32*0Sstevel@tonic-gate * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 33*0Sstevel@tonic-gate * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 34*0Sstevel@tonic-gate * SUCH DAMAGE. 35*0Sstevel@tonic-gate * 36*0Sstevel@tonic-gate * $FreeBSD: src/sbin/routed/main.c,v 1.14 2000/08/11 08:24:38 sheldonh Exp $ 37*0Sstevel@tonic-gate * char copyright[] = "@(#) Copyright (c) 1983, 1988, 1993\n" 38*0Sstevel@tonic-gate * " The Regents of the University of California. All rights reserved.\n"; 39*0Sstevel@tonic-gate */ 40*0Sstevel@tonic-gate 41*0Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 42*0Sstevel@tonic-gate 43*0Sstevel@tonic-gate #include "defs.h" 44*0Sstevel@tonic-gate #include "pathnames.h" 45*0Sstevel@tonic-gate #include <signal.h> 46*0Sstevel@tonic-gate #include <fcntl.h> 47*0Sstevel@tonic-gate #include <sys/file.h> 48*0Sstevel@tonic-gate #include <userdefs.h> 49*0Sstevel@tonic-gate #include <sys/stat.h> 50*0Sstevel@tonic-gate 51*0Sstevel@tonic-gate #define IN_ROUTED_VERSION "2.22" 52*0Sstevel@tonic-gate 53*0Sstevel@tonic-gate int stopint; 54*0Sstevel@tonic-gate boolean_t supplier; /* supply or broadcast updates */ 55*0Sstevel@tonic-gate boolean_t supplier_set; 56*0Sstevel@tonic-gate /* -S option. _B_TRUE=treat all RIP speakers as default routers. */ 57*0Sstevel@tonic-gate boolean_t save_space = _B_FALSE; 58*0Sstevel@tonic-gate 59*0Sstevel@tonic-gate static boolean_t default_gateway; /* _B_TRUE=advertise default */ 60*0Sstevel@tonic-gate static boolean_t background = _B_TRUE; 61*0Sstevel@tonic-gate boolean_t ridhosts; /* _B_TRUE=reduce host routes */ 62*0Sstevel@tonic-gate boolean_t mhome; /* _B_TRUE=want multi-homed host route */ 63*0Sstevel@tonic-gate boolean_t advertise_mhome; /* _B_TRUE=must continue advertising it */ 64*0Sstevel@tonic-gate boolean_t auth_ok = _B_TRUE; /* _B_TRUE=ignore auth if we don't care */ 65*0Sstevel@tonic-gate boolean_t no_install; /* _B_TRUE=don't install in kernel */ 66*0Sstevel@tonic-gate 67*0Sstevel@tonic-gate struct timeval epoch; /* when started */ 68*0Sstevel@tonic-gate struct timeval clk; 69*0Sstevel@tonic-gate static struct timeval prev_clk; 70*0Sstevel@tonic-gate static int usec_fudge; 71*0Sstevel@tonic-gate struct timeval now; /* current idea of time */ 72*0Sstevel@tonic-gate /* If a route's rts_time is <= to now_stale, the route is stale. */ 73*0Sstevel@tonic-gate time_t now_stale; 74*0Sstevel@tonic-gate /* If a route's rts_time is <= to now_expire, the route is expired */ 75*0Sstevel@tonic-gate time_t now_expire; 76*0Sstevel@tonic-gate /* If a route's rts_time is <= to now_garbage, the route needs to be deleted */ 77*0Sstevel@tonic-gate time_t now_garbage; 78*0Sstevel@tonic-gate 79*0Sstevel@tonic-gate static struct timeval next_bcast; /* next general broadcast */ 80*0Sstevel@tonic-gate struct timeval no_flash = { /* inhibit flash update */ 81*0Sstevel@tonic-gate EPOCH+SUPPLY_INTERVAL, 0 82*0Sstevel@tonic-gate }; 83*0Sstevel@tonic-gate 84*0Sstevel@tonic-gate /* When now reaches this time, it's time to call sync_kern() */ 85*0Sstevel@tonic-gate static struct timeval sync_kern_timer; 86*0Sstevel@tonic-gate 87*0Sstevel@tonic-gate static fd_set fdbits; 88*0Sstevel@tonic-gate static int sock_max; 89*0Sstevel@tonic-gate int rip_sock = -1; /* RIP socket */ 90*0Sstevel@tonic-gate boolean_t rip_enabled; 91*0Sstevel@tonic-gate static boolean_t openlog_done; 92*0Sstevel@tonic-gate 93*0Sstevel@tonic-gate /* 94*0Sstevel@tonic-gate * The interface to which rip_sock is currently pointing for 95*0Sstevel@tonic-gate * output. 96*0Sstevel@tonic-gate */ 97*0Sstevel@tonic-gate struct interface *rip_sock_interface; 98*0Sstevel@tonic-gate 99*0Sstevel@tonic-gate int rt_sock; /* routing socket */ 100*0Sstevel@tonic-gate 101*0Sstevel@tonic-gate 102*0Sstevel@tonic-gate static int open_rip_sock(); 103*0Sstevel@tonic-gate static void timevalsub(struct timeval *, struct timeval *, struct timeval *); 104*0Sstevel@tonic-gate static void sigalrm(int); 105*0Sstevel@tonic-gate static void sigterm(int); 106*0Sstevel@tonic-gate 107*0Sstevel@tonic-gate static int 108*0Sstevel@tonic-gate daemon(boolean_t nochdir, boolean_t noclose) 109*0Sstevel@tonic-gate { 110*0Sstevel@tonic-gate int retv; 111*0Sstevel@tonic-gate 112*0Sstevel@tonic-gate if ((retv = fork()) == -1) 113*0Sstevel@tonic-gate return (-1); 114*0Sstevel@tonic-gate if (retv != 0) 115*0Sstevel@tonic-gate _exit(EXIT_SUCCESS); 116*0Sstevel@tonic-gate if (setsid() == -1) 117*0Sstevel@tonic-gate return (-1); 118*0Sstevel@tonic-gate if ((retv = fork()) == -1) 119*0Sstevel@tonic-gate return (-1); 120*0Sstevel@tonic-gate if (retv != 0) 121*0Sstevel@tonic-gate _exit(EXIT_SUCCESS); 122*0Sstevel@tonic-gate if (!nochdir && chdir("/") == -1) 123*0Sstevel@tonic-gate return (-1); 124*0Sstevel@tonic-gate if (!noclose) { 125*0Sstevel@tonic-gate (void) close(0); 126*0Sstevel@tonic-gate (void) close(1); 127*0Sstevel@tonic-gate (void) close(2); 128*0Sstevel@tonic-gate if ((retv = open("/dev/null", O_RDWR)) != -1) { 129*0Sstevel@tonic-gate (void) dup2(retv, 1); 130*0Sstevel@tonic-gate (void) dup2(retv, 2); 131*0Sstevel@tonic-gate } 132*0Sstevel@tonic-gate } 133*0Sstevel@tonic-gate return (0); 134*0Sstevel@tonic-gate } 135*0Sstevel@tonic-gate 136*0Sstevel@tonic-gate int 137*0Sstevel@tonic-gate main(int argc, char *argv[]) 138*0Sstevel@tonic-gate { 139*0Sstevel@tonic-gate int n, off; 140*0Sstevel@tonic-gate char *p, *q; 141*0Sstevel@tonic-gate const char *cp; 142*0Sstevel@tonic-gate struct timeval select_timeout, result; 143*0Sstevel@tonic-gate fd_set ibits; 144*0Sstevel@tonic-gate in_addr_t p_net, p_mask; 145*0Sstevel@tonic-gate struct parm parm; 146*0Sstevel@tonic-gate char *tracename = NULL; 147*0Sstevel@tonic-gate boolean_t vflag = _B_FALSE; 148*0Sstevel@tonic-gate boolean_t version = _B_FALSE; 149*0Sstevel@tonic-gate int sigerr = 0; 150*0Sstevel@tonic-gate FILE *pidfp; 151*0Sstevel@tonic-gate mode_t pidmode = (S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH); /* 0644 */ 152*0Sstevel@tonic-gate 153*0Sstevel@tonic-gate (void) setlocale(LC_ALL, ""); 154*0Sstevel@tonic-gate 155*0Sstevel@tonic-gate #if !defined(TEXT_DOMAIN) /* Should be defined by cc -D */ 156*0Sstevel@tonic-gate #define TEXT_DOMAIN "SYS_TEXT" 157*0Sstevel@tonic-gate #endif /* ! TEXT_DOMAIN */ 158*0Sstevel@tonic-gate 159*0Sstevel@tonic-gate (void) textdomain(TEXT_DOMAIN); 160*0Sstevel@tonic-gate 161*0Sstevel@tonic-gate /* 162*0Sstevel@tonic-gate * Some shells are badly broken and send SIGHUP to backgrounded 163*0Sstevel@tonic-gate * processes. 164*0Sstevel@tonic-gate */ 165*0Sstevel@tonic-gate if (signal(SIGHUP, SIG_IGN) == SIG_ERR) 166*0Sstevel@tonic-gate sigerr = errno; 167*0Sstevel@tonic-gate 168*0Sstevel@tonic-gate ftrace = stdout; 169*0Sstevel@tonic-gate 170*0Sstevel@tonic-gate if (gettimeofday(&clk, 0) == -1) { 171*0Sstevel@tonic-gate logbad(_B_FALSE, "gettimeofday: %s", rip_strerror(errno)); 172*0Sstevel@tonic-gate } 173*0Sstevel@tonic-gate prev_clk = clk; 174*0Sstevel@tonic-gate epoch = clk; 175*0Sstevel@tonic-gate epoch.tv_sec -= EPOCH; 176*0Sstevel@tonic-gate now.tv_sec = EPOCH; 177*0Sstevel@tonic-gate now_stale = EPOCH - STALE_TIME; 178*0Sstevel@tonic-gate now_expire = EPOCH - EXPIRE_TIME; 179*0Sstevel@tonic-gate now_garbage = EPOCH - GARBAGE_TIME; 180*0Sstevel@tonic-gate select_timeout.tv_sec = 0; 181*0Sstevel@tonic-gate 182*0Sstevel@tonic-gate while ((n = getopt(argc, argv, "sSqdghmpAztVvnT:F:P:")) != -1) { 183*0Sstevel@tonic-gate switch (n) { 184*0Sstevel@tonic-gate case 'A': 185*0Sstevel@tonic-gate /* 186*0Sstevel@tonic-gate * Ignore authentication if we do not care. 187*0Sstevel@tonic-gate * Crazy as it is, that is what RFC 2453 requires. 188*0Sstevel@tonic-gate */ 189*0Sstevel@tonic-gate auth_ok = _B_FALSE; 190*0Sstevel@tonic-gate break; 191*0Sstevel@tonic-gate 192*0Sstevel@tonic-gate case 't': 193*0Sstevel@tonic-gate if (new_tracelevel < 2) 194*0Sstevel@tonic-gate new_tracelevel = 2; 195*0Sstevel@tonic-gate background = _B_FALSE; 196*0Sstevel@tonic-gate break; 197*0Sstevel@tonic-gate 198*0Sstevel@tonic-gate case 'd': /* put in.routed in foreground */ 199*0Sstevel@tonic-gate background = _B_FALSE; 200*0Sstevel@tonic-gate break; 201*0Sstevel@tonic-gate 202*0Sstevel@tonic-gate case 'F': /* minimal routes for SLIP */ 203*0Sstevel@tonic-gate n = FAKE_METRIC; 204*0Sstevel@tonic-gate p = strchr(optarg, ','); 205*0Sstevel@tonic-gate if (p != NULL) { 206*0Sstevel@tonic-gate n = (int)strtoul(p+1, &q, 0); 207*0Sstevel@tonic-gate if (*q == '\0' && p+1 != q && 208*0Sstevel@tonic-gate n <= HOPCNT_INFINITY-1 && n >= 1) 209*0Sstevel@tonic-gate *p = '\0'; 210*0Sstevel@tonic-gate } 211*0Sstevel@tonic-gate if (!getnet(optarg, &p_net, &p_mask)) { 212*0Sstevel@tonic-gate if (p != NULL) 213*0Sstevel@tonic-gate *p = ','; 214*0Sstevel@tonic-gate msglog(gettext("bad network; \"-F %s\""), 215*0Sstevel@tonic-gate optarg); 216*0Sstevel@tonic-gate break; 217*0Sstevel@tonic-gate } 218*0Sstevel@tonic-gate (void) memset(&parm, 0, sizeof (parm)); 219*0Sstevel@tonic-gate parm.parm_net = p_net; 220*0Sstevel@tonic-gate parm.parm_mask = p_mask; 221*0Sstevel@tonic-gate parm.parm_d_metric = n; 222*0Sstevel@tonic-gate cp = insert_parm(&parm); 223*0Sstevel@tonic-gate if (cp != NULL) 224*0Sstevel@tonic-gate msglog(gettext("bad -F: %s"), cp); 225*0Sstevel@tonic-gate break; 226*0Sstevel@tonic-gate 227*0Sstevel@tonic-gate case 'g': 228*0Sstevel@tonic-gate (void) memset(&parm, 0, sizeof (parm)); 229*0Sstevel@tonic-gate parm.parm_d_metric = 1; 230*0Sstevel@tonic-gate cp = insert_parm(&parm); 231*0Sstevel@tonic-gate if (cp != NULL) 232*0Sstevel@tonic-gate msglog(gettext("bad -g: %s"), cp); 233*0Sstevel@tonic-gate else 234*0Sstevel@tonic-gate default_gateway = _B_TRUE; 235*0Sstevel@tonic-gate break; 236*0Sstevel@tonic-gate 237*0Sstevel@tonic-gate case 'h': /* suppress extra host routes */ 238*0Sstevel@tonic-gate ridhosts = _B_TRUE; 239*0Sstevel@tonic-gate break; 240*0Sstevel@tonic-gate 241*0Sstevel@tonic-gate case 'm': /* advertise host route */ 242*0Sstevel@tonic-gate mhome = _B_TRUE; /* on multi-homed hosts */ 243*0Sstevel@tonic-gate break; 244*0Sstevel@tonic-gate 245*0Sstevel@tonic-gate case 'n': /* No-install mode */ 246*0Sstevel@tonic-gate no_install = _B_TRUE; 247*0Sstevel@tonic-gate break; 248*0Sstevel@tonic-gate 249*0Sstevel@tonic-gate case 'P': 250*0Sstevel@tonic-gate /* handle arbitrary parameters. */ 251*0Sstevel@tonic-gate q = strdup(optarg); 252*0Sstevel@tonic-gate if (q == NULL) 253*0Sstevel@tonic-gate logbad(_B_FALSE, "strdup: %s", 254*0Sstevel@tonic-gate rip_strerror(errno)); 255*0Sstevel@tonic-gate cp = parse_parms(q, _B_FALSE); 256*0Sstevel@tonic-gate if (cp != NULL) 257*0Sstevel@tonic-gate msglog(gettext("%1$s in \"-P %2$s\""), cp, 258*0Sstevel@tonic-gate optarg); 259*0Sstevel@tonic-gate free(q); 260*0Sstevel@tonic-gate break; 261*0Sstevel@tonic-gate 262*0Sstevel@tonic-gate case 'q': 263*0Sstevel@tonic-gate supplier = _B_FALSE; 264*0Sstevel@tonic-gate supplier_set = _B_TRUE; 265*0Sstevel@tonic-gate break; 266*0Sstevel@tonic-gate 267*0Sstevel@tonic-gate case 's': 268*0Sstevel@tonic-gate supplier = _B_TRUE; 269*0Sstevel@tonic-gate supplier_set = _B_TRUE; 270*0Sstevel@tonic-gate break; 271*0Sstevel@tonic-gate 272*0Sstevel@tonic-gate case 'S': /* save-space option */ 273*0Sstevel@tonic-gate save_space = _B_TRUE; 274*0Sstevel@tonic-gate break; 275*0Sstevel@tonic-gate 276*0Sstevel@tonic-gate case 'T': 277*0Sstevel@tonic-gate tracename = optarg; 278*0Sstevel@tonic-gate break; 279*0Sstevel@tonic-gate 280*0Sstevel@tonic-gate case 'V': 281*0Sstevel@tonic-gate /* display version */ 282*0Sstevel@tonic-gate version = _B_TRUE; 283*0Sstevel@tonic-gate msglog(gettext("version " IN_ROUTED_VERSION)); 284*0Sstevel@tonic-gate break; 285*0Sstevel@tonic-gate 286*0Sstevel@tonic-gate case 'v': 287*0Sstevel@tonic-gate /* display route changes to supplied logfile */ 288*0Sstevel@tonic-gate new_tracelevel = 1; 289*0Sstevel@tonic-gate vflag = _B_TRUE; 290*0Sstevel@tonic-gate break; 291*0Sstevel@tonic-gate 292*0Sstevel@tonic-gate case 'z': /* increase debug-level */ 293*0Sstevel@tonic-gate new_tracelevel++; 294*0Sstevel@tonic-gate break; 295*0Sstevel@tonic-gate 296*0Sstevel@tonic-gate default: 297*0Sstevel@tonic-gate goto usage; 298*0Sstevel@tonic-gate } 299*0Sstevel@tonic-gate } 300*0Sstevel@tonic-gate argc -= optind; 301*0Sstevel@tonic-gate argv += optind; 302*0Sstevel@tonic-gate 303*0Sstevel@tonic-gate if (tracename == NULL && argc >= 1) { 304*0Sstevel@tonic-gate tracename = *argv++; 305*0Sstevel@tonic-gate argc--; 306*0Sstevel@tonic-gate } 307*0Sstevel@tonic-gate if (tracename != NULL && tracename[0] == '\0') 308*0Sstevel@tonic-gate goto usage; 309*0Sstevel@tonic-gate if (vflag && tracename == NULL) 310*0Sstevel@tonic-gate goto usage; 311*0Sstevel@tonic-gate if (argc != 0) { 312*0Sstevel@tonic-gate usage: 313*0Sstevel@tonic-gate (void) fprintf(stderr, 314*0Sstevel@tonic-gate gettext("usage: in.routed [-AdghmnqsStVvz] " 315*0Sstevel@tonic-gate "[-T <tracefile>]\n")); 316*0Sstevel@tonic-gate (void) fprintf(stderr, 317*0Sstevel@tonic-gate gettext("\t[-F <net>[/<mask>][,<metric>]] [-P <parms>]\n")); 318*0Sstevel@tonic-gate logbad(_B_FALSE, gettext("excess arguments")); 319*0Sstevel@tonic-gate } 320*0Sstevel@tonic-gate if (geteuid() != 0) { 321*0Sstevel@tonic-gate /* 322*0Sstevel@tonic-gate * Regular users are allowed to run in.routed for the 323*0Sstevel@tonic-gate * sole purpose of obtaining the version number. In 324*0Sstevel@tonic-gate * that case, exit(EXIT_SUCCESS) without complaining. 325*0Sstevel@tonic-gate */ 326*0Sstevel@tonic-gate if (version) 327*0Sstevel@tonic-gate exit(EXIT_SUCCESS); 328*0Sstevel@tonic-gate logbad(_B_FALSE, gettext("requires UID 0")); 329*0Sstevel@tonic-gate } 330*0Sstevel@tonic-gate 331*0Sstevel@tonic-gate if (default_gateway) { 332*0Sstevel@tonic-gate if (supplier_set && !supplier) { 333*0Sstevel@tonic-gate msglog(gettext("-g and -q are incompatible")); 334*0Sstevel@tonic-gate } else { 335*0Sstevel@tonic-gate supplier = _B_TRUE; 336*0Sstevel@tonic-gate supplier_set = _B_TRUE; 337*0Sstevel@tonic-gate } 338*0Sstevel@tonic-gate } 339*0Sstevel@tonic-gate 340*0Sstevel@tonic-gate if (signal(SIGALRM, sigalrm) == SIG_ERR) 341*0Sstevel@tonic-gate sigerr = errno; 342*0Sstevel@tonic-gate /* SIGHUP fatal during debugging */ 343*0Sstevel@tonic-gate if (!background) 344*0Sstevel@tonic-gate if (signal(SIGHUP, sigterm) == SIG_ERR) 345*0Sstevel@tonic-gate sigerr = errno; 346*0Sstevel@tonic-gate if (signal(SIGTERM, sigterm) == SIG_ERR) 347*0Sstevel@tonic-gate sigerr = errno; 348*0Sstevel@tonic-gate if (signal(SIGINT, sigterm) == SIG_ERR) 349*0Sstevel@tonic-gate sigerr = errno; 350*0Sstevel@tonic-gate if (signal(SIGUSR1, sigtrace_more) == SIG_ERR) 351*0Sstevel@tonic-gate sigerr = errno; 352*0Sstevel@tonic-gate if (signal(SIGUSR2, sigtrace_less) == SIG_ERR) 353*0Sstevel@tonic-gate sigerr = errno; 354*0Sstevel@tonic-gate if (signal(SIGHUP, sigtrace_dump) == SIG_ERR) 355*0Sstevel@tonic-gate sigerr = errno; 356*0Sstevel@tonic-gate 357*0Sstevel@tonic-gate if (sigerr) 358*0Sstevel@tonic-gate msglog("signal: %s", rip_strerror(sigerr)); 359*0Sstevel@tonic-gate 360*0Sstevel@tonic-gate /* get into the background */ 361*0Sstevel@tonic-gate if (background && daemon(_B_FALSE, _B_FALSE) < 0) 362*0Sstevel@tonic-gate BADERR(_B_FALSE, "daemon()"); 363*0Sstevel@tonic-gate 364*0Sstevel@tonic-gate /* Store our process id, blow away any existing file if it exists. */ 365*0Sstevel@tonic-gate if ((pidfp = fopen(PATH_PID, "w")) == NULL) { 366*0Sstevel@tonic-gate (void) fprintf(stderr, 367*0Sstevel@tonic-gate gettext("in.routed: unable to open " PATH_PID ": %s\n"), 368*0Sstevel@tonic-gate strerror(errno)); 369*0Sstevel@tonic-gate } else { 370*0Sstevel@tonic-gate (void) fprintf(pidfp, "%ld\n", getpid()); 371*0Sstevel@tonic-gate (void) fclose(pidfp); 372*0Sstevel@tonic-gate (void) chmod(PATH_PID, pidmode); 373*0Sstevel@tonic-gate } 374*0Sstevel@tonic-gate 375*0Sstevel@tonic-gate srandom((int)(clk.tv_sec ^ clk.tv_usec ^ getpid())); 376*0Sstevel@tonic-gate 377*0Sstevel@tonic-gate /* allocate the interface tables */ 378*0Sstevel@tonic-gate iftbl_alloc(); 379*0Sstevel@tonic-gate 380*0Sstevel@tonic-gate /* prepare socket connected to the kernel. */ 381*0Sstevel@tonic-gate rt_sock = socket(PF_ROUTE, SOCK_RAW, AF_INET); 382*0Sstevel@tonic-gate if (rt_sock < 0) 383*0Sstevel@tonic-gate BADERR(_B_TRUE, "rt_sock = socket()"); 384*0Sstevel@tonic-gate if (fcntl(rt_sock, F_SETFL, O_NONBLOCK) == -1) 385*0Sstevel@tonic-gate logbad(_B_TRUE, "fcntl(rt_sock) O_NONBLOCK: %s", 386*0Sstevel@tonic-gate rip_strerror(errno)); 387*0Sstevel@tonic-gate off = 0; 388*0Sstevel@tonic-gate if (setsockopt(rt_sock, SOL_SOCKET, SO_USELOOPBACK, 389*0Sstevel@tonic-gate &off, sizeof (off)) < 0) 390*0Sstevel@tonic-gate LOGERR("setsockopt(SO_USELOOPBACK,0)"); 391*0Sstevel@tonic-gate 392*0Sstevel@tonic-gate fix_select(); 393*0Sstevel@tonic-gate 394*0Sstevel@tonic-gate 395*0Sstevel@tonic-gate if (tracename != NULL) { 396*0Sstevel@tonic-gate (void) strlcpy(inittracename, tracename, 397*0Sstevel@tonic-gate sizeof (inittracename)); 398*0Sstevel@tonic-gate set_tracefile(inittracename, "%s", -1); 399*0Sstevel@tonic-gate } else { 400*0Sstevel@tonic-gate tracelevel_msg("%s", -1); /* turn on tracing to stdio */ 401*0Sstevel@tonic-gate } 402*0Sstevel@tonic-gate 403*0Sstevel@tonic-gate bufinit(); 404*0Sstevel@tonic-gate 405*0Sstevel@tonic-gate /* initialize radix tree */ 406*0Sstevel@tonic-gate rtinit(); 407*0Sstevel@tonic-gate 408*0Sstevel@tonic-gate /* 409*0Sstevel@tonic-gate * Pick a random part of the second for our output to minimize 410*0Sstevel@tonic-gate * collisions. 411*0Sstevel@tonic-gate * 412*0Sstevel@tonic-gate * Start broadcasting after hearing from other routers, and 413*0Sstevel@tonic-gate * at a random time so a bunch of systems do not get synchronized 414*0Sstevel@tonic-gate * after a power failure. 415*0Sstevel@tonic-gate * 416*0Sstevel@tonic-gate * Since now is the number of seconds since epoch (this is initially 417*0Sstevel@tonic-gate * EPOCH seconds), these times are really relative to now. 418*0Sstevel@tonic-gate */ 419*0Sstevel@tonic-gate intvl_random(&next_bcast, EPOCH+MIN_WAITTIME, EPOCH+SUPPLY_INTERVAL); 420*0Sstevel@tonic-gate age_timer.tv_usec = next_bcast.tv_usec; 421*0Sstevel@tonic-gate age_timer.tv_sec = EPOCH+MIN_WAITTIME; 422*0Sstevel@tonic-gate rdisc_timer = next_bcast; 423*0Sstevel@tonic-gate ifscan_timer.tv_usec = next_bcast.tv_usec; 424*0Sstevel@tonic-gate 425*0Sstevel@tonic-gate /* 426*0Sstevel@tonic-gate * Open the global rip socket. From now on, this socket can be 427*0Sstevel@tonic-gate * assumed to be open. It will remain open until in.routed 428*0Sstevel@tonic-gate * exits. 429*0Sstevel@tonic-gate */ 430*0Sstevel@tonic-gate rip_sock = open_rip_sock(); 431*0Sstevel@tonic-gate 432*0Sstevel@tonic-gate /* 433*0Sstevel@tonic-gate * Collect an initial view of the world by checking the interface 434*0Sstevel@tonic-gate * configuration and the kludge file. 435*0Sstevel@tonic-gate * 436*0Sstevel@tonic-gate * gwkludge() could call addroutefordefault(), resulting in a call to 437*0Sstevel@tonic-gate * iflookup, and thus ifscan() to find the physical interfaces. 438*0Sstevel@tonic-gate * ifscan() will attempt to use the rip_sock in order to join 439*0Sstevel@tonic-gate * mcast groups, so gwkludge *must* be called after opening 440*0Sstevel@tonic-gate * the rip_sock. 441*0Sstevel@tonic-gate */ 442*0Sstevel@tonic-gate gwkludge(); 443*0Sstevel@tonic-gate 444*0Sstevel@tonic-gate ifscan(); 445*0Sstevel@tonic-gate 446*0Sstevel@tonic-gate /* Ask for routes */ 447*0Sstevel@tonic-gate rip_query(); 448*0Sstevel@tonic-gate rdisc_sol(); 449*0Sstevel@tonic-gate 450*0Sstevel@tonic-gate /* Now turn off stdio if not tracing */ 451*0Sstevel@tonic-gate if (new_tracelevel == 0) 452*0Sstevel@tonic-gate trace_close(background); 453*0Sstevel@tonic-gate 454*0Sstevel@tonic-gate /* Loop until a fatal error occurs, listening and broadcasting. */ 455*0Sstevel@tonic-gate for (;;) { 456*0Sstevel@tonic-gate prev_clk = clk; 457*0Sstevel@tonic-gate if (gettimeofday(&clk, 0) == -1) { 458*0Sstevel@tonic-gate logbad(_B_FALSE, "gettimeofday: %s", 459*0Sstevel@tonic-gate rip_strerror(errno)); 460*0Sstevel@tonic-gate } 461*0Sstevel@tonic-gate if (prev_clk.tv_sec == clk.tv_sec && 462*0Sstevel@tonic-gate prev_clk.tv_usec == clk.tv_usec+usec_fudge) { 463*0Sstevel@tonic-gate /* 464*0Sstevel@tonic-gate * Much of `in.routed` depends on time always advancing. 465*0Sstevel@tonic-gate * On systems that do not guarantee that gettimeofday() 466*0Sstevel@tonic-gate * produces unique timestamps even if called within 467*0Sstevel@tonic-gate * a single tick, use trickery like that in classic 468*0Sstevel@tonic-gate * BSD kernels. 469*0Sstevel@tonic-gate */ 470*0Sstevel@tonic-gate clk.tv_usec += ++usec_fudge; 471*0Sstevel@tonic-gate 472*0Sstevel@tonic-gate } else { 473*0Sstevel@tonic-gate time_t dt; 474*0Sstevel@tonic-gate 475*0Sstevel@tonic-gate usec_fudge = 0; 476*0Sstevel@tonic-gate 477*0Sstevel@tonic-gate timevalsub(&result, &clk, &prev_clk); 478*0Sstevel@tonic-gate if (result.tv_sec < 0 || result.tv_sec > 479*0Sstevel@tonic-gate select_timeout.tv_sec + 5) { 480*0Sstevel@tonic-gate /* 481*0Sstevel@tonic-gate * Deal with time changes before other 482*0Sstevel@tonic-gate * housekeeping to keep everything straight. 483*0Sstevel@tonic-gate */ 484*0Sstevel@tonic-gate dt = result.tv_sec; 485*0Sstevel@tonic-gate if (dt > 0) 486*0Sstevel@tonic-gate dt -= select_timeout.tv_sec; 487*0Sstevel@tonic-gate trace_act("time changed by %d sec", (int)dt); 488*0Sstevel@tonic-gate epoch.tv_sec += dt; 489*0Sstevel@tonic-gate } 490*0Sstevel@tonic-gate } 491*0Sstevel@tonic-gate timevalsub(&now, &clk, &epoch); 492*0Sstevel@tonic-gate now_stale = now.tv_sec - STALE_TIME; 493*0Sstevel@tonic-gate now_expire = now.tv_sec - EXPIRE_TIME; 494*0Sstevel@tonic-gate now_garbage = now.tv_sec - GARBAGE_TIME; 495*0Sstevel@tonic-gate 496*0Sstevel@tonic-gate /* deal with signals that should affect tracing */ 497*0Sstevel@tonic-gate set_tracelevel(); 498*0Sstevel@tonic-gate 499*0Sstevel@tonic-gate if (stopint != 0) { 500*0Sstevel@tonic-gate trace_off("exiting with signal %d", stopint); 501*0Sstevel@tonic-gate break; 502*0Sstevel@tonic-gate } 503*0Sstevel@tonic-gate 504*0Sstevel@tonic-gate /* look for new or dead interfaces */ 505*0Sstevel@tonic-gate timevalsub(&select_timeout, &ifscan_timer, &now); 506*0Sstevel@tonic-gate if (select_timeout.tv_sec <= 0) { 507*0Sstevel@tonic-gate select_timeout.tv_sec = 0; 508*0Sstevel@tonic-gate ifscan(); 509*0Sstevel@tonic-gate rip_query(); 510*0Sstevel@tonic-gate continue; 511*0Sstevel@tonic-gate } 512*0Sstevel@tonic-gate 513*0Sstevel@tonic-gate /* 514*0Sstevel@tonic-gate * Check the kernel table occassionally for mysteriously 515*0Sstevel@tonic-gate * evaporated routes 516*0Sstevel@tonic-gate */ 517*0Sstevel@tonic-gate timevalsub(&result, &sync_kern_timer, &now); 518*0Sstevel@tonic-gate if (result.tv_sec <= 0) { 519*0Sstevel@tonic-gate sync_kern(); 520*0Sstevel@tonic-gate sync_kern_timer.tv_sec = (now.tv_sec 521*0Sstevel@tonic-gate + CHECK_QUIET_INTERVAL); 522*0Sstevel@tonic-gate continue; 523*0Sstevel@tonic-gate } 524*0Sstevel@tonic-gate if (timercmp(&result, &select_timeout, < /* */)) 525*0Sstevel@tonic-gate select_timeout = result; 526*0Sstevel@tonic-gate 527*0Sstevel@tonic-gate /* If it is time, then broadcast our routes. */ 528*0Sstevel@tonic-gate if (should_supply(NULL) || advertise_mhome) { 529*0Sstevel@tonic-gate timevalsub(&result, &next_bcast, &now); 530*0Sstevel@tonic-gate if (result.tv_sec <= 0) { 531*0Sstevel@tonic-gate /* 532*0Sstevel@tonic-gate * Synchronize the aging and broadcast 533*0Sstevel@tonic-gate * timers to minimize awakenings 534*0Sstevel@tonic-gate */ 535*0Sstevel@tonic-gate age(0); 536*0Sstevel@tonic-gate age_peer_info(); 537*0Sstevel@tonic-gate 538*0Sstevel@tonic-gate rip_bcast(0); 539*0Sstevel@tonic-gate 540*0Sstevel@tonic-gate /* 541*0Sstevel@tonic-gate * It is desirable to send routing updates 542*0Sstevel@tonic-gate * regularly. So schedule the next update 543*0Sstevel@tonic-gate * 30 seconds after the previous one was 544*0Sstevel@tonic-gate * scheduled, instead of 30 seconds after 545*0Sstevel@tonic-gate * the previous update was finished. 546*0Sstevel@tonic-gate * Even if we just started after discovering 547*0Sstevel@tonic-gate * a 2nd interface or were otherwise delayed, 548*0Sstevel@tonic-gate * pick a 30-second aniversary of the 549*0Sstevel@tonic-gate * original broadcast time. 550*0Sstevel@tonic-gate */ 551*0Sstevel@tonic-gate n = 1 + (0-result.tv_sec)/SUPPLY_INTERVAL; 552*0Sstevel@tonic-gate next_bcast.tv_sec += n*SUPPLY_INTERVAL; 553*0Sstevel@tonic-gate 554*0Sstevel@tonic-gate continue; 555*0Sstevel@tonic-gate } 556*0Sstevel@tonic-gate 557*0Sstevel@tonic-gate if (timercmp(&result, &select_timeout, < /* */)) 558*0Sstevel@tonic-gate select_timeout = result; 559*0Sstevel@tonic-gate } 560*0Sstevel@tonic-gate 561*0Sstevel@tonic-gate /* 562*0Sstevel@tonic-gate * If we need a flash update, either do it now or 563*0Sstevel@tonic-gate * set the delay to end when it is time. 564*0Sstevel@tonic-gate * 565*0Sstevel@tonic-gate * If we are within MIN_WAITTIME seconds of a full update, 566*0Sstevel@tonic-gate * do not bother. 567*0Sstevel@tonic-gate */ 568*0Sstevel@tonic-gate if (need_flash && should_supply(NULL) && 569*0Sstevel@tonic-gate no_flash.tv_sec+MIN_WAITTIME < next_bcast.tv_sec) { 570*0Sstevel@tonic-gate /* accurate to the millisecond */ 571*0Sstevel@tonic-gate if (!timercmp(&no_flash, &now, > /* */)) 572*0Sstevel@tonic-gate rip_bcast(1); 573*0Sstevel@tonic-gate timevalsub(&result, &no_flash, &now); 574*0Sstevel@tonic-gate if (timercmp(&result, &select_timeout, < /* */)) 575*0Sstevel@tonic-gate select_timeout = result; 576*0Sstevel@tonic-gate } 577*0Sstevel@tonic-gate 578*0Sstevel@tonic-gate /* trigger the main aging timer. */ 579*0Sstevel@tonic-gate timevalsub(&result, &age_timer, &now); 580*0Sstevel@tonic-gate if (result.tv_sec <= 0) { 581*0Sstevel@tonic-gate age(0); 582*0Sstevel@tonic-gate continue; 583*0Sstevel@tonic-gate } 584*0Sstevel@tonic-gate if (timercmp(&result, &select_timeout, < /* */)) 585*0Sstevel@tonic-gate select_timeout = result; 586*0Sstevel@tonic-gate 587*0Sstevel@tonic-gate /* update the kernel routing table */ 588*0Sstevel@tonic-gate timevalsub(&result, &need_kern, &now); 589*0Sstevel@tonic-gate if (result.tv_sec <= 0) { 590*0Sstevel@tonic-gate age(0); 591*0Sstevel@tonic-gate continue; 592*0Sstevel@tonic-gate } 593*0Sstevel@tonic-gate if (timercmp(&result, &select_timeout, < /* */)) 594*0Sstevel@tonic-gate select_timeout = result; 595*0Sstevel@tonic-gate 596*0Sstevel@tonic-gate /* 597*0Sstevel@tonic-gate * take care of router discovery. We compare timeval 598*0Sstevel@tonic-gate * structures here to have millisecond granularity. 599*0Sstevel@tonic-gate */ 600*0Sstevel@tonic-gate if (!timercmp(&rdisc_timer, &now, > /* */)) { 601*0Sstevel@tonic-gate rdisc_age(0); 602*0Sstevel@tonic-gate continue; 603*0Sstevel@tonic-gate } 604*0Sstevel@tonic-gate timevalsub(&result, &rdisc_timer, &now); 605*0Sstevel@tonic-gate if (timercmp(&result, &select_timeout, < /* */)) 606*0Sstevel@tonic-gate select_timeout = result; 607*0Sstevel@tonic-gate 608*0Sstevel@tonic-gate /* 609*0Sstevel@tonic-gate * Well-known bit of select(3c) silliness inherited 610*0Sstevel@tonic-gate * from BSD: anything over 100 million seconds is 611*0Sstevel@tonic-gate * considered an "error." Reset that to zero. 612*0Sstevel@tonic-gate */ 613*0Sstevel@tonic-gate if (select_timeout.tv_sec > 100000000) 614*0Sstevel@tonic-gate select_timeout.tv_sec = 0; 615*0Sstevel@tonic-gate 616*0Sstevel@tonic-gate /* wait for input or a timer to expire. */ 617*0Sstevel@tonic-gate trace_flush(); 618*0Sstevel@tonic-gate ibits = fdbits; 619*0Sstevel@tonic-gate n = select(sock_max, &ibits, 0, 0, &select_timeout); 620*0Sstevel@tonic-gate if (n <= 0) { 621*0Sstevel@tonic-gate if (n < 0 && errno != EINTR && errno != EAGAIN) 622*0Sstevel@tonic-gate BADERR(_B_TRUE, "select"); 623*0Sstevel@tonic-gate continue; 624*0Sstevel@tonic-gate } 625*0Sstevel@tonic-gate 626*0Sstevel@tonic-gate if (FD_ISSET(rt_sock, &ibits)) { 627*0Sstevel@tonic-gate read_rt(); 628*0Sstevel@tonic-gate n--; 629*0Sstevel@tonic-gate } 630*0Sstevel@tonic-gate if (rdisc_sock >= 0 && FD_ISSET(rdisc_sock, &ibits)) { 631*0Sstevel@tonic-gate read_d(); 632*0Sstevel@tonic-gate n--; 633*0Sstevel@tonic-gate } 634*0Sstevel@tonic-gate if (rip_sock >= 0 && FD_ISSET(rip_sock, &ibits)) { 635*0Sstevel@tonic-gate if (read_rip() == -1) { 636*0Sstevel@tonic-gate rip_enabled = _B_FALSE; 637*0Sstevel@tonic-gate trace_off("main rip socket failed"); 638*0Sstevel@tonic-gate (void) close(rip_sock); 639*0Sstevel@tonic-gate rip_sock = -1; 640*0Sstevel@tonic-gate fix_select(); 641*0Sstevel@tonic-gate break; 642*0Sstevel@tonic-gate } 643*0Sstevel@tonic-gate n--; 644*0Sstevel@tonic-gate } 645*0Sstevel@tonic-gate } 646*0Sstevel@tonic-gate rip_bcast(0); 647*0Sstevel@tonic-gate rdisc_adv(_B_FALSE); 648*0Sstevel@tonic-gate (void) unlink(PATH_PID); 649*0Sstevel@tonic-gate return (stopint | 128); 650*0Sstevel@tonic-gate } 651*0Sstevel@tonic-gate 652*0Sstevel@tonic-gate 653*0Sstevel@tonic-gate static void 654*0Sstevel@tonic-gate sigalrm(int sig) 655*0Sstevel@tonic-gate { 656*0Sstevel@tonic-gate /* 657*0Sstevel@tonic-gate * Historically, SIGALRM would cause the daemon to check for 658*0Sstevel@tonic-gate * new and broken interfaces. 659*0Sstevel@tonic-gate */ 660*0Sstevel@tonic-gate ifscan_timer.tv_sec = now.tv_sec; 661*0Sstevel@tonic-gate trace_act("SIGALRM"); 662*0Sstevel@tonic-gate if (signal(sig, sigalrm) == SIG_ERR) 663*0Sstevel@tonic-gate msglog("signal: %s", rip_strerror(errno)); 664*0Sstevel@tonic-gate } 665*0Sstevel@tonic-gate 666*0Sstevel@tonic-gate 667*0Sstevel@tonic-gate /* watch for fatal signals */ 668*0Sstevel@tonic-gate static void 669*0Sstevel@tonic-gate sigterm(int sig) 670*0Sstevel@tonic-gate { 671*0Sstevel@tonic-gate stopint = sig; 672*0Sstevel@tonic-gate if (signal(sig, SIG_DFL) == SIG_ERR) /* catch it only once */ 673*0Sstevel@tonic-gate msglog("signal: %s", rip_strerror(errno)); 674*0Sstevel@tonic-gate } 675*0Sstevel@tonic-gate 676*0Sstevel@tonic-gate 677*0Sstevel@tonic-gate void 678*0Sstevel@tonic-gate fix_select(void) 679*0Sstevel@tonic-gate { 680*0Sstevel@tonic-gate (void) FD_ZERO(&fdbits); 681*0Sstevel@tonic-gate sock_max = 0; 682*0Sstevel@tonic-gate 683*0Sstevel@tonic-gate FD_SET(rt_sock, &fdbits); 684*0Sstevel@tonic-gate if (sock_max <= rt_sock) 685*0Sstevel@tonic-gate sock_max = rt_sock+1; 686*0Sstevel@tonic-gate if (rip_sock >= 0) { 687*0Sstevel@tonic-gate FD_SET(rip_sock, &fdbits); 688*0Sstevel@tonic-gate if (sock_max <= rip_sock) 689*0Sstevel@tonic-gate sock_max = rip_sock+1; 690*0Sstevel@tonic-gate } 691*0Sstevel@tonic-gate if (rdisc_sock >= 0) { 692*0Sstevel@tonic-gate FD_SET(rdisc_sock, &fdbits); 693*0Sstevel@tonic-gate if (sock_max <= rdisc_sock) 694*0Sstevel@tonic-gate sock_max = rdisc_sock+1; 695*0Sstevel@tonic-gate } 696*0Sstevel@tonic-gate } 697*0Sstevel@tonic-gate 698*0Sstevel@tonic-gate 699*0Sstevel@tonic-gate void 700*0Sstevel@tonic-gate fix_sock(int sock, 701*0Sstevel@tonic-gate const char *name) 702*0Sstevel@tonic-gate { 703*0Sstevel@tonic-gate int on; 704*0Sstevel@tonic-gate #define MIN_SOCKBUF (4*1024) 705*0Sstevel@tonic-gate static int rbuf; 706*0Sstevel@tonic-gate 707*0Sstevel@tonic-gate if (fcntl(sock, F_SETFL, O_NONBLOCK) == -1) 708*0Sstevel@tonic-gate logbad(_B_TRUE, "fcntl(%s) O_NONBLOCK: %s", name, 709*0Sstevel@tonic-gate rip_strerror(errno)); 710*0Sstevel@tonic-gate on = 1; 711*0Sstevel@tonic-gate if (setsockopt(sock, SOL_SOCKET, SO_BROADCAST, &on, sizeof (on)) < 0) 712*0Sstevel@tonic-gate msglog("setsockopt(%s,SO_BROADCAST): %s", 713*0Sstevel@tonic-gate name, rip_strerror(errno)); 714*0Sstevel@tonic-gate 715*0Sstevel@tonic-gate if (rbuf >= MIN_SOCKBUF) { 716*0Sstevel@tonic-gate if (setsockopt(sock, SOL_SOCKET, SO_RCVBUF, 717*0Sstevel@tonic-gate &rbuf, sizeof (rbuf)) < 0) 718*0Sstevel@tonic-gate msglog("setsockopt(%s,SO_RCVBUF=%d): %s", 719*0Sstevel@tonic-gate name, rbuf, rip_strerror(errno)); 720*0Sstevel@tonic-gate } else { 721*0Sstevel@tonic-gate for (rbuf = 60*1024; ; rbuf -= 4096) { 722*0Sstevel@tonic-gate if (setsockopt(sock, SOL_SOCKET, SO_RCVBUF, 723*0Sstevel@tonic-gate &rbuf, sizeof (rbuf)) == 0) { 724*0Sstevel@tonic-gate trace_act("RCVBUF=%d", rbuf); 725*0Sstevel@tonic-gate break; 726*0Sstevel@tonic-gate } 727*0Sstevel@tonic-gate if (rbuf < MIN_SOCKBUF) { 728*0Sstevel@tonic-gate msglog("setsockopt(%s,SO_RCVBUF = %d): %s", 729*0Sstevel@tonic-gate name, rbuf, rip_strerror(errno)); 730*0Sstevel@tonic-gate break; 731*0Sstevel@tonic-gate } 732*0Sstevel@tonic-gate } 733*0Sstevel@tonic-gate } 734*0Sstevel@tonic-gate } 735*0Sstevel@tonic-gate 736*0Sstevel@tonic-gate 737*0Sstevel@tonic-gate /* 738*0Sstevel@tonic-gate * Open and return the global rip socket. It is guaranteed to return 739*0Sstevel@tonic-gate * a good file descriptor. 740*0Sstevel@tonic-gate */ 741*0Sstevel@tonic-gate static int 742*0Sstevel@tonic-gate open_rip_sock() 743*0Sstevel@tonic-gate { 744*0Sstevel@tonic-gate struct sockaddr_in sin; 745*0Sstevel@tonic-gate unsigned char ttl; 746*0Sstevel@tonic-gate int s; 747*0Sstevel@tonic-gate int on = 1; 748*0Sstevel@tonic-gate 749*0Sstevel@tonic-gate 750*0Sstevel@tonic-gate if ((s = socket(PF_INET, SOCK_DGRAM, 0)) < 0) 751*0Sstevel@tonic-gate BADERR(_B_TRUE, "rip_sock = socket()"); 752*0Sstevel@tonic-gate 753*0Sstevel@tonic-gate (void) memset(&sin, 0, sizeof (sin)); 754*0Sstevel@tonic-gate sin.sin_family = AF_INET; 755*0Sstevel@tonic-gate sin.sin_port = htons(RIP_PORT); 756*0Sstevel@tonic-gate sin.sin_addr.s_addr = INADDR_ANY; 757*0Sstevel@tonic-gate if (bind(s, (struct sockaddr *)&sin, sizeof (sin)) < 0) { 758*0Sstevel@tonic-gate BADERR(_B_FALSE, "bind(rip_sock)"); 759*0Sstevel@tonic-gate } 760*0Sstevel@tonic-gate fix_sock(s, "rip_sock"); 761*0Sstevel@tonic-gate 762*0Sstevel@tonic-gate ttl = 1; 763*0Sstevel@tonic-gate if (setsockopt(s, IPPROTO_IP, IP_MULTICAST_TTL, 764*0Sstevel@tonic-gate &ttl, sizeof (ttl)) < 0) 765*0Sstevel@tonic-gate DBGERR(_B_TRUE, "rip_sock setsockopt(IP_MULTICAST_TTL)"); 766*0Sstevel@tonic-gate 767*0Sstevel@tonic-gate if (setsockopt(s, IPPROTO_IP, IP_RECVIF, &on, sizeof (on))) 768*0Sstevel@tonic-gate BADERR(_B_FALSE, "setsockopt(IP_RECVIF)"); 769*0Sstevel@tonic-gate 770*0Sstevel@tonic-gate return (s); 771*0Sstevel@tonic-gate } 772*0Sstevel@tonic-gate 773*0Sstevel@tonic-gate 774*0Sstevel@tonic-gate /* 775*0Sstevel@tonic-gate * Disable RIP. Note that we don't close the global rip socket since 776*0Sstevel@tonic-gate * it is used even when RIP is disabled to receive and answer certain 777*0Sstevel@tonic-gate * queries. 778*0Sstevel@tonic-gate */ 779*0Sstevel@tonic-gate void 780*0Sstevel@tonic-gate rip_off(void) 781*0Sstevel@tonic-gate { 782*0Sstevel@tonic-gate struct ip_mreq m; 783*0Sstevel@tonic-gate struct interface *ifp; 784*0Sstevel@tonic-gate char addrstr[INET_ADDRSTRLEN]; 785*0Sstevel@tonic-gate 786*0Sstevel@tonic-gate if (rip_enabled && !mhome) { 787*0Sstevel@tonic-gate trace_act("turn off RIP"); 788*0Sstevel@tonic-gate 789*0Sstevel@tonic-gate /* 790*0Sstevel@tonic-gate * Unsubscribe from the 224.0.0.9 RIP multicast 791*0Sstevel@tonic-gate * group address 792*0Sstevel@tonic-gate */ 793*0Sstevel@tonic-gate for (ifp = ifnet; ifp != NULL; ifp = ifp->int_next) { 794*0Sstevel@tonic-gate if ((ifp->int_if_flags & IFF_MULTICAST) && 795*0Sstevel@tonic-gate !IS_IFF_QUIET(ifp->int_if_flags) && 796*0Sstevel@tonic-gate !IS_RIP_IN_OFF(ifp->int_state) && 797*0Sstevel@tonic-gate !(ifp->int_state & IS_DUP)) { 798*0Sstevel@tonic-gate m.imr_multiaddr.s_addr = 799*0Sstevel@tonic-gate htonl(INADDR_RIP_GROUP); 800*0Sstevel@tonic-gate m.imr_interface.s_addr = 801*0Sstevel@tonic-gate (ifp->int_if_flags & IFF_POINTOPOINT) ? 802*0Sstevel@tonic-gate ifp->int_dstaddr : ifp->int_addr; 803*0Sstevel@tonic-gate (void) strlcpy(addrstr, 804*0Sstevel@tonic-gate inet_ntoa(m.imr_multiaddr), 805*0Sstevel@tonic-gate sizeof (addrstr)); 806*0Sstevel@tonic-gate if (setsockopt(rip_sock, IPPROTO_IP, 807*0Sstevel@tonic-gate IP_DROP_MEMBERSHIP, &m, 808*0Sstevel@tonic-gate sizeof (m)) < 0 && 809*0Sstevel@tonic-gate errno != EADDRNOTAVAIL && errno != ENOENT) 810*0Sstevel@tonic-gate writelog(LOG_WARNING, 811*0Sstevel@tonic-gate "%s: setsockopt(IP_DROP_MEMBERSHIP " 812*0Sstevel@tonic-gate "%s, %s): %s", ifp->int_name, 813*0Sstevel@tonic-gate addrstr, inet_ntoa(m.imr_interface), 814*0Sstevel@tonic-gate rip_strerror(errno)); 815*0Sstevel@tonic-gate } 816*0Sstevel@tonic-gate } 817*0Sstevel@tonic-gate rip_enabled = _B_FALSE; 818*0Sstevel@tonic-gate 819*0Sstevel@tonic-gate age(0); 820*0Sstevel@tonic-gate } 821*0Sstevel@tonic-gate } 822*0Sstevel@tonic-gate 823*0Sstevel@tonic-gate 824*0Sstevel@tonic-gate /* turn on RIP multicast input via an interface */ 825*0Sstevel@tonic-gate void 826*0Sstevel@tonic-gate rip_mcast_on(struct interface *ifp) 827*0Sstevel@tonic-gate { 828*0Sstevel@tonic-gate struct ip_mreq m; 829*0Sstevel@tonic-gate 830*0Sstevel@tonic-gate if (!IS_RIP_IN_OFF(ifp->int_state) && 831*0Sstevel@tonic-gate (ifp->int_if_flags & IFF_MULTICAST) && 832*0Sstevel@tonic-gate !IS_IFF_QUIET(ifp->int_if_flags) && 833*0Sstevel@tonic-gate !(ifp->int_state & IS_DUP)) { 834*0Sstevel@tonic-gate m.imr_multiaddr.s_addr = htonl(INADDR_RIP_GROUP); 835*0Sstevel@tonic-gate m.imr_interface.s_addr = (ifp->int_if_flags & IFF_POINTOPOINT) ? 836*0Sstevel@tonic-gate ifp->int_dstaddr : ifp->int_addr; 837*0Sstevel@tonic-gate if ((setsockopt(rip_sock, IPPROTO_IP, IP_ADD_MEMBERSHIP, 838*0Sstevel@tonic-gate &m, sizeof (m)) < 0) && !(ifp->int_state & IS_BROKE)) 839*0Sstevel@tonic-gate writelog(LOG_WARNING, 840*0Sstevel@tonic-gate "Could not join 224.0.0.9 on interface %s: %s", 841*0Sstevel@tonic-gate ifp->int_name, rip_strerror(errno)); 842*0Sstevel@tonic-gate } 843*0Sstevel@tonic-gate } 844*0Sstevel@tonic-gate 845*0Sstevel@tonic-gate /* turn off RIP multicast input via an interface */ 846*0Sstevel@tonic-gate void 847*0Sstevel@tonic-gate rip_mcast_off(struct interface *ifp) 848*0Sstevel@tonic-gate { 849*0Sstevel@tonic-gate struct ip_mreq m; 850*0Sstevel@tonic-gate 851*0Sstevel@tonic-gate if ((ifp->int_if_flags & IFF_MULTICAST) && 852*0Sstevel@tonic-gate !IS_IFF_QUIET(ifp->int_if_flags) && rip_enabled) { 853*0Sstevel@tonic-gate m.imr_multiaddr.s_addr = htonl(INADDR_RIP_GROUP); 854*0Sstevel@tonic-gate m.imr_interface.s_addr = (ifp->int_if_flags & IFF_POINTOPOINT) ? 855*0Sstevel@tonic-gate ifp->int_dstaddr : ifp->int_addr; 856*0Sstevel@tonic-gate if ((setsockopt(rip_sock, IPPROTO_IP, IP_DROP_MEMBERSHIP, 857*0Sstevel@tonic-gate &m, sizeof (m)) < 0) && errno != EADDRNOTAVAIL && 858*0Sstevel@tonic-gate errno != ENOENT) 859*0Sstevel@tonic-gate writelog(LOG_WARNING, 860*0Sstevel@tonic-gate "setsockopt(IP_DROP_MEMBERSHIP RIP) for %s: %s", 861*0Sstevel@tonic-gate ifp->int_name, rip_strerror(errno)); 862*0Sstevel@tonic-gate } 863*0Sstevel@tonic-gate } 864*0Sstevel@tonic-gate 865*0Sstevel@tonic-gate /* enable RIP */ 866*0Sstevel@tonic-gate void 867*0Sstevel@tonic-gate rip_on(struct interface *ifp) 868*0Sstevel@tonic-gate { 869*0Sstevel@tonic-gate /* 870*0Sstevel@tonic-gate * If RIP is already enabled, only start receiving 871*0Sstevel@tonic-gate * multicasts for this interface. 872*0Sstevel@tonic-gate */ 873*0Sstevel@tonic-gate if (rip_enabled) { 874*0Sstevel@tonic-gate if (ifp != NULL) 875*0Sstevel@tonic-gate rip_mcast_on(ifp); 876*0Sstevel@tonic-gate return; 877*0Sstevel@tonic-gate } 878*0Sstevel@tonic-gate 879*0Sstevel@tonic-gate /* 880*0Sstevel@tonic-gate * If RIP is disabled and it makes sense to enable it, then enable 881*0Sstevel@tonic-gate * it on all of the interfaces. It makes sense if either router 882*0Sstevel@tonic-gate * discovery is off, or if router discovery is on and at most one 883*0Sstevel@tonic-gate * interface is doing RIP. 884*0Sstevel@tonic-gate */ 885*0Sstevel@tonic-gate if (rip_interfaces > 0 && (!rdisc_ok || rip_interfaces > 1)) { 886*0Sstevel@tonic-gate trace_act("turn on RIP"); 887*0Sstevel@tonic-gate 888*0Sstevel@tonic-gate rip_enabled = _B_TRUE; 889*0Sstevel@tonic-gate rip_sock_interface = NULL; 890*0Sstevel@tonic-gate 891*0Sstevel@tonic-gate /* Do not advertise anything until we have heard something */ 892*0Sstevel@tonic-gate if (next_bcast.tv_sec < now.tv_sec+MIN_WAITTIME) 893*0Sstevel@tonic-gate next_bcast.tv_sec = now.tv_sec+MIN_WAITTIME; 894*0Sstevel@tonic-gate 895*0Sstevel@tonic-gate for (ifp = ifnet; ifp != NULL; ifp = ifp->int_next) { 896*0Sstevel@tonic-gate ifp->int_query_time = NEVER; 897*0Sstevel@tonic-gate rip_mcast_on(ifp); 898*0Sstevel@tonic-gate } 899*0Sstevel@tonic-gate ifscan_timer.tv_sec = now.tv_sec; 900*0Sstevel@tonic-gate } 901*0Sstevel@tonic-gate 902*0Sstevel@tonic-gate fix_select(); 903*0Sstevel@tonic-gate } 904*0Sstevel@tonic-gate 905*0Sstevel@tonic-gate 906*0Sstevel@tonic-gate /* die if malloc(3) fails */ 907*0Sstevel@tonic-gate void * 908*0Sstevel@tonic-gate rtmalloc(size_t size, 909*0Sstevel@tonic-gate const char *msg) 910*0Sstevel@tonic-gate { 911*0Sstevel@tonic-gate void *p = malloc(size); 912*0Sstevel@tonic-gate if (p == NULL) 913*0Sstevel@tonic-gate logbad(_B_TRUE, "malloc(%lu) failed in %s: %s", (ulong_t)size, 914*0Sstevel@tonic-gate msg, rip_strerror(errno)); 915*0Sstevel@tonic-gate return (p); 916*0Sstevel@tonic-gate } 917*0Sstevel@tonic-gate 918*0Sstevel@tonic-gate 919*0Sstevel@tonic-gate /* get a random instant in an interval */ 920*0Sstevel@tonic-gate void 921*0Sstevel@tonic-gate intvl_random(struct timeval *tp, /* put value here */ 922*0Sstevel@tonic-gate ulong_t lo, /* value is after this second */ 923*0Sstevel@tonic-gate ulong_t hi) /* and before this */ 924*0Sstevel@tonic-gate { 925*0Sstevel@tonic-gate tp->tv_sec = (time_t)(hi == lo ? lo : (lo + random() % ((hi - lo)))); 926*0Sstevel@tonic-gate tp->tv_usec = random() % 1000000; 927*0Sstevel@tonic-gate } 928*0Sstevel@tonic-gate 929*0Sstevel@tonic-gate 930*0Sstevel@tonic-gate void 931*0Sstevel@tonic-gate timevaladd(struct timeval *t1, 932*0Sstevel@tonic-gate struct timeval *t2) 933*0Sstevel@tonic-gate { 934*0Sstevel@tonic-gate 935*0Sstevel@tonic-gate t1->tv_sec += t2->tv_sec; 936*0Sstevel@tonic-gate if ((t1->tv_usec += t2->tv_usec) >= 1000000) { 937*0Sstevel@tonic-gate t1->tv_sec++; 938*0Sstevel@tonic-gate t1->tv_usec -= 1000000; 939*0Sstevel@tonic-gate } 940*0Sstevel@tonic-gate } 941*0Sstevel@tonic-gate 942*0Sstevel@tonic-gate 943*0Sstevel@tonic-gate /* t1 = t2 - t3 */ 944*0Sstevel@tonic-gate static void 945*0Sstevel@tonic-gate timevalsub(struct timeval *t1, 946*0Sstevel@tonic-gate struct timeval *t2, 947*0Sstevel@tonic-gate struct timeval *t3) 948*0Sstevel@tonic-gate { 949*0Sstevel@tonic-gate t1->tv_sec = t2->tv_sec - t3->tv_sec; 950*0Sstevel@tonic-gate if ((t1->tv_usec = t2->tv_usec - t3->tv_usec) < 0) { 951*0Sstevel@tonic-gate t1->tv_sec--; 952*0Sstevel@tonic-gate t1->tv_usec += 1000000; 953*0Sstevel@tonic-gate } 954*0Sstevel@tonic-gate } 955*0Sstevel@tonic-gate 956*0Sstevel@tonic-gate static void 957*0Sstevel@tonic-gate do_openlog(void) 958*0Sstevel@tonic-gate { 959*0Sstevel@tonic-gate openlog_done = _B_TRUE; 960*0Sstevel@tonic-gate openlog("in.routed", LOG_PID | LOG_ODELAY, LOG_DAEMON); 961*0Sstevel@tonic-gate } 962*0Sstevel@tonic-gate 963*0Sstevel@tonic-gate /* put a LOG_ERR message into the system log */ 964*0Sstevel@tonic-gate void 965*0Sstevel@tonic-gate msglog(const char *p, ...) 966*0Sstevel@tonic-gate { 967*0Sstevel@tonic-gate va_list args; 968*0Sstevel@tonic-gate 969*0Sstevel@tonic-gate trace_flush(); 970*0Sstevel@tonic-gate 971*0Sstevel@tonic-gate if (!openlog_done) 972*0Sstevel@tonic-gate do_openlog(); 973*0Sstevel@tonic-gate va_start(args, p); 974*0Sstevel@tonic-gate vsyslog(LOG_ERR, p, args); 975*0Sstevel@tonic-gate 976*0Sstevel@tonic-gate if (ftrace != 0) { 977*0Sstevel@tonic-gate if (ftrace == stdout) 978*0Sstevel@tonic-gate (void) fputs("in.routed: ", ftrace); 979*0Sstevel@tonic-gate (void) vfprintf(ftrace, p, args); 980*0Sstevel@tonic-gate (void) fputc('\n', ftrace); 981*0Sstevel@tonic-gate } 982*0Sstevel@tonic-gate } 983*0Sstevel@tonic-gate 984*0Sstevel@tonic-gate 985*0Sstevel@tonic-gate /* 986*0Sstevel@tonic-gate * Put a message about a bad system into the system log if 987*0Sstevel@tonic-gate * we have not complained about it recently. 988*0Sstevel@tonic-gate * 989*0Sstevel@tonic-gate * It is desirable to complain about all bad systems, but not too often. 990*0Sstevel@tonic-gate * In the worst case, it is not practical to keep track of all bad systems. 991*0Sstevel@tonic-gate * For example, there can be many systems with the wrong password. 992*0Sstevel@tonic-gate */ 993*0Sstevel@tonic-gate void 994*0Sstevel@tonic-gate msglim(struct msg_limit *lim, in_addr_t addr, const char *p, ...) 995*0Sstevel@tonic-gate { 996*0Sstevel@tonic-gate va_list args; 997*0Sstevel@tonic-gate int i; 998*0Sstevel@tonic-gate struct msg_sub *ms1, *ms; 999*0Sstevel@tonic-gate const char *p1; 1000*0Sstevel@tonic-gate 1001*0Sstevel@tonic-gate va_start(args, p); 1002*0Sstevel@tonic-gate 1003*0Sstevel@tonic-gate /* 1004*0Sstevel@tonic-gate * look for the oldest slot in the table 1005*0Sstevel@tonic-gate * or the slot for the bad router. 1006*0Sstevel@tonic-gate */ 1007*0Sstevel@tonic-gate ms = ms1 = lim->subs; 1008*0Sstevel@tonic-gate for (i = MSG_SUBJECT_N; ; i--, ms1++) { 1009*0Sstevel@tonic-gate if (i == 0) { 1010*0Sstevel@tonic-gate /* Reuse a slot at most once every 10 minutes. */ 1011*0Sstevel@tonic-gate if (lim->reuse > now.tv_sec) { 1012*0Sstevel@tonic-gate ms = NULL; 1013*0Sstevel@tonic-gate } else { 1014*0Sstevel@tonic-gate lim->reuse = now.tv_sec + 10*60; 1015*0Sstevel@tonic-gate } 1016*0Sstevel@tonic-gate break; 1017*0Sstevel@tonic-gate } 1018*0Sstevel@tonic-gate if (ms->addr == addr) { 1019*0Sstevel@tonic-gate /* 1020*0Sstevel@tonic-gate * Repeat a complaint about a given system at 1021*0Sstevel@tonic-gate * most once an hour. 1022*0Sstevel@tonic-gate */ 1023*0Sstevel@tonic-gate if (ms->until > now.tv_sec) 1024*0Sstevel@tonic-gate ms = NULL; 1025*0Sstevel@tonic-gate break; 1026*0Sstevel@tonic-gate } 1027*0Sstevel@tonic-gate if (ms->until < ms1->until) 1028*0Sstevel@tonic-gate ms = ms1; 1029*0Sstevel@tonic-gate } 1030*0Sstevel@tonic-gate if (ms != NULL) { 1031*0Sstevel@tonic-gate ms->addr = addr; 1032*0Sstevel@tonic-gate ms->until = now.tv_sec + 60*60; /* 60 minutes */ 1033*0Sstevel@tonic-gate 1034*0Sstevel@tonic-gate if (!openlog_done) 1035*0Sstevel@tonic-gate do_openlog(); 1036*0Sstevel@tonic-gate trace_flush(); 1037*0Sstevel@tonic-gate for (p1 = p; *p1 == ' '; p1++) 1038*0Sstevel@tonic-gate continue; 1039*0Sstevel@tonic-gate vsyslog(LOG_ERR, p1, args); 1040*0Sstevel@tonic-gate } 1041*0Sstevel@tonic-gate 1042*0Sstevel@tonic-gate /* always display the message if tracing */ 1043*0Sstevel@tonic-gate if (ftrace != 0) { 1044*0Sstevel@tonic-gate (void) vfprintf(ftrace, p, args); 1045*0Sstevel@tonic-gate (void) fputc('\n', ftrace); 1046*0Sstevel@tonic-gate } 1047*0Sstevel@tonic-gate } 1048*0Sstevel@tonic-gate 1049*0Sstevel@tonic-gate 1050*0Sstevel@tonic-gate void 1051*0Sstevel@tonic-gate logbad(boolean_t dump, const char *p, ...) 1052*0Sstevel@tonic-gate { 1053*0Sstevel@tonic-gate va_list args; 1054*0Sstevel@tonic-gate 1055*0Sstevel@tonic-gate trace_flush(); 1056*0Sstevel@tonic-gate 1057*0Sstevel@tonic-gate if (!openlog_done) 1058*0Sstevel@tonic-gate do_openlog(); 1059*0Sstevel@tonic-gate va_start(args, p); 1060*0Sstevel@tonic-gate vsyslog(LOG_ERR, p, args); 1061*0Sstevel@tonic-gate 1062*0Sstevel@tonic-gate (void) fputs(gettext("in.routed: "), stderr); 1063*0Sstevel@tonic-gate (void) vfprintf(stderr, p, args); 1064*0Sstevel@tonic-gate (void) fputs(gettext("; giving up\n"), stderr); 1065*0Sstevel@tonic-gate (void) fflush(stderr); 1066*0Sstevel@tonic-gate 1067*0Sstevel@tonic-gate if (dump) 1068*0Sstevel@tonic-gate abort(); 1069*0Sstevel@tonic-gate exit(EXIT_FAILURE); 1070*0Sstevel@tonic-gate } 1071*0Sstevel@tonic-gate 1072*0Sstevel@tonic-gate /* put a message into the system log */ 1073*0Sstevel@tonic-gate void 1074*0Sstevel@tonic-gate writelog(int level, const char *p, ...) 1075*0Sstevel@tonic-gate { 1076*0Sstevel@tonic-gate va_list args; 1077*0Sstevel@tonic-gate 1078*0Sstevel@tonic-gate trace_flush(); 1079*0Sstevel@tonic-gate 1080*0Sstevel@tonic-gate if (!openlog_done) 1081*0Sstevel@tonic-gate do_openlog(); 1082*0Sstevel@tonic-gate va_start(args, p); 1083*0Sstevel@tonic-gate vsyslog(level, p, args); 1084*0Sstevel@tonic-gate 1085*0Sstevel@tonic-gate if (ftrace != 0) { 1086*0Sstevel@tonic-gate if (ftrace == stdout) 1087*0Sstevel@tonic-gate (void) fputs("in.routed: ", ftrace); 1088*0Sstevel@tonic-gate (void) vfprintf(ftrace, p, args); 1089*0Sstevel@tonic-gate (void) fputc('\n', ftrace); 1090*0Sstevel@tonic-gate } 1091*0Sstevel@tonic-gate } 1092