main.c revision 65d6e08afd923e8496fff598c19c151fd4d0ce64
/*
* Copyright 2006 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*
* Copyright (c) 1983, 1988, 1993
* The Regents of the University of California. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgment:
* This product includes software developed by the University of
* California, Berkeley and its contributors.
* 4. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* char copyright[] = "@(#) Copyright (c) 1983, 1988, 1993\n"
* " The Regents of the University of California. All rights reserved.\n";
*/
#pragma ident "%Z%%M% %I% %E% SMI"
#include "defs.h"
#include "pathnames.h"
#include <signal.h>
#include <fcntl.h>
#include <userdefs.h>
#define IN_ROUTED_VERSION "2.22"
int stopint;
/* -S option. _B_TRUE=treat all RIP speakers as default routers. */
static int usec_fudge;
/* If a route's rts_time is <= to now_stale, the route is stale. */
/* If a route's rts_time is <= to now_expire, the route is expired */
/* If a route's rts_time is <= to now_garbage, the route needs to be deleted */
};
/* When now reaches this time, it's time to call sync_kern() */
static struct timeval sync_kern_timer;
static int sock_max;
static boolean_t openlog_done;
/*
* The interface to which rip_sock is currently pointing for
* output.
*/
struct interface *rip_sock_interface;
int rt_sock; /* routing socket */
static int open_rip_sock();
static void sigalrm(int);
static void sigterm(int);
static int
{
int retv;
return (-1);
if (retv != 0)
if (setsid() == -1)
return (-1);
return (-1);
if (retv != 0)
return (-1);
if (!noclose) {
(void) close(0);
(void) close(1);
(void) close(2);
}
}
return (0);
}
int
{
int n, off;
char *p, *q;
const char *cp;
int sigerr = 0;
#if !defined(TEXT_DOMAIN) /* Should be defined by cc -D */
#define TEXT_DOMAIN "SYS_TEXT"
#endif /* ! TEXT_DOMAIN */
(void) textdomain(TEXT_DOMAIN);
/*
* Some shells are badly broken and send SIGHUP to backgrounded
* processes.
*/
}
select_timeout.tv_sec = 0;
switch (n) {
case 'A':
/*
* Ignore authentication if we do not care.
* Crazy as it is, that is what RFC 2453 requires.
*/
break;
case 't':
if (new_tracelevel < 2)
new_tracelevel = 2;
break;
case 'd': /* put in.routed in foreground */
break;
case 'F': /* minimal routes for SLIP */
n = FAKE_METRIC;
if (p != NULL) {
n = (int)strtoul(p+1, &q, 0);
if (*q == '\0' && p+1 != q &&
*p = '\0';
}
if (p != NULL)
*p = ',';
optarg);
break;
}
parm.parm_d_metric = n;
break;
case 'g':
else
break;
case 'h': /* suppress extra host routes */
break;
case 'm': /* advertise host route */
break;
case 'n': /* No-install mode */
break;
case 'P':
/* handle arbitrary parameters. */
if (q == NULL)
optarg);
free(q);
break;
case 'q':
break;
case 's':
break;
case 'S': /* save-space option */
break;
case 'T':
break;
case 'V':
/* display version */
break;
case 'v':
/* display route changes to supplied logfile */
new_tracelevel = 1;
break;
case 'z': /* increase debug-level */
break;
default:
goto usage;
}
}
argc--;
}
goto usage;
goto usage;
if (argc != 0) {
gettext("usage: in.routed [-AdghmnqsStVvz] "
"[-T <tracefile>]\n"));
gettext("\t[-F <net>[/<mask>][,<metric>]] [-P <parms>]\n"));
}
if (geteuid() != 0) {
/*
* Regular users are allowed to run in.routed for the
* sole purpose of obtaining the version number. In
* that case, exit(EXIT_SUCCESS) without complaining.
*/
if (version)
}
if (default_gateway) {
if (supplier_set && !supplier) {
} else {
}
}
/* SIGHUP fatal during debugging */
if (!background)
if (sigerr)
/* get into the background */
/* Store our process id, blow away any existing file if it exists. */
} else {
}
/* allocate the interface tables */
iftbl_alloc();
/* prepare socket connected to the kernel. */
if (rt_sock < 0)
off = 0;
LOGERR("setsockopt(SO_USELOOPBACK,0)");
fix_select();
sizeof (inittracename));
} else {
}
bufinit();
/* initialize radix tree */
rtinit();
/*
* Pick a random part of the second for our output to minimize
* collisions.
*
* Start broadcasting after hearing from other routers, and
* at a random time so a bunch of systems do not get synchronized
* after a power failure.
*
* Since now is the number of seconds since epoch (this is initially
* EPOCH seconds), these times are really relative to now.
*/
/*
* Open the global rip socket. From now on, this socket can be
* assumed to be open. It will remain open until in.routed
* exits.
*/
rip_sock = open_rip_sock();
/*
* Collect an initial view of the world by checking the interface
* configuration and the kludge file.
*
* gwkludge() could call addroutefordefault(), resulting in a call to
* iflookup, and thus ifscan() to find the physical interfaces.
* ifscan() will attempt to use the rip_sock in order to join
* mcast groups, so gwkludge *must* be called after opening
* the rip_sock.
*/
gwkludge();
ifscan();
/* Ask for routes */
rip_query();
rdisc_sol();
/* Now turn off stdio if not tracing */
if (new_tracelevel == 0)
/* Loop until a fatal error occurs, listening and broadcasting. */
for (;;) {
}
/*
* Much of `in.routed` depends on time always advancing.
* On systems that do not guarantee that gettimeofday()
* produces unique timestamps even if called within
* a single tick, use trickery like that in classic
* BSD kernels.
*/
} else {
usec_fudge = 0;
/*
* Deal with time changes before other
* housekeeping to keep everything straight.
*/
if (dt > 0)
}
}
/* deal with signals that should affect tracing */
if (stopint != 0) {
break;
}
/* look for new or dead interfaces */
if (select_timeout.tv_sec <= 0) {
select_timeout.tv_sec = 0;
ifscan();
rip_query();
continue;
}
/*
* Check the kernel table occassionally for mysteriously
* evaporated routes
*/
sync_kern();
continue;
}
/* If it is time, then broadcast our routes. */
/*
* Synchronize the aging and broadcast
* timers to minimize awakenings
*/
age(0);
rip_bcast(0);
/*
* It is desirable to send routing updates
* regularly. So schedule the next update
* 30 seconds after the previous one was
* scheduled, instead of 30 seconds after
* the previous update was finished.
* Even if we just started after discovering
* a 2nd interface or were otherwise delayed,
* pick a 30-second aniversary of the
* original broadcast time.
*/
continue;
}
}
/*
* If we need a flash update, either do it now or
* set the delay to end when it is time.
*
* If we are within MIN_WAITTIME seconds of a full update,
* do not bother.
*/
/* accurate to the millisecond */
rip_bcast(1);
}
/* trigger the main aging timer. */
age(0);
continue;
}
/* update the kernel routing table */
age(0);
continue;
}
/*
* take care of router discovery. We compare timeval
* structures here to have millisecond granularity.
*/
rdisc_age(0);
continue;
}
/*
* Well-known bit of select(3c) silliness inherited
* from BSD: anything over 100 million seconds is
* considered an "error." Reset that to zero.
*/
select_timeout.tv_sec = 0;
/* wait for input or a timer to expire. */
trace_flush();
if (n <= 0) {
continue;
}
read_rt();
n--;
}
read_d();
n--;
}
n--;
}
if (read_rip() == -1) {
trace_off("main rip socket failed");
rip_sock = -1;
fix_select();
break;
}
n--;
}
}
rip_bcast(0);
return (stopint | 128);
}
static void
{
/*
* Historically, SIGALRM would cause the daemon to check for
* new and broken interfaces.
*/
trace_act("SIGALRM");
}
/* watch for fatal signals */
static void
{
}
void
fix_select(void)
{
sock_max = 0;
if (rip_sock >= 0) {
}
if (rdisc_sock >= 0) {
if (sock_max <= rdisc_sock)
if (sock_max <= rdisc_mib_sock)
}
}
void
const char *name)
{
int on;
static int rbuf;
on = 1;
msglog("setsockopt(%s,SO_BROADCAST): %s",
if (rbuf >= MIN_SOCKBUF) {
msglog("setsockopt(%s,SO_RCVBUF=%d): %s",
} else {
break;
}
if (rbuf < MIN_SOCKBUF) {
msglog("setsockopt(%s,SO_RCVBUF = %d): %s",
break;
}
}
}
}
/*
* Open and return the global rip socket. It is guaranteed to return
* a good file descriptor.
*/
static int
{
struct sockaddr_in sin;
unsigned char ttl;
int s;
int on = 1;
}
fix_sock(s, "rip_sock");
ttl = 1;
return (s);
}
/*
* Disable RIP. Note that we don't close the global rip socket since
* it is used even when RIP is disabled to receive and answer certain
* queries.
*/
void
rip_off(void)
{
struct ip_mreq m;
char addrstr[INET_ADDRSTRLEN];
if (rip_enabled && !mhome) {
trace_act("turn off RIP");
/*
* Unsubscribe from the 224.0.0.9 RIP multicast
* group address
*/
m.imr_multiaddr.s_addr =
m.imr_interface.s_addr =
sizeof (addrstr));
IP_DROP_MEMBERSHIP, &m,
sizeof (m)) < 0 &&
"%s: setsockopt(IP_DROP_MEMBERSHIP "
}
}
age(0);
}
}
/* turn on RIP multicast input via an interface */
void
{
struct ip_mreq m;
"Could not join 224.0.0.9 on interface %s: %s",
}
}
/* turn off RIP multicast input via an interface */
void
{
struct ip_mreq m;
&m, sizeof (m)) < 0) && errno != EADDRNOTAVAIL &&
"setsockopt(IP_DROP_MEMBERSHIP RIP) for %s: %s",
}
}
/* enable RIP */
void
{
/*
* If RIP is already enabled, only start receiving
* multicasts for this interface.
*/
if (rip_enabled) {
return;
}
/*
* If RIP is disabled and it makes sense to enable it, then enable
* it on all of the interfaces. It makes sense if either router
* discovery is off, or if router discovery is on and at most one
* interface is doing RIP.
*/
trace_act("turn on RIP");
/* Do not advertise anything until we have heard something */
}
}
fix_select();
}
/* die if malloc(3) fails */
void *
const char *msg)
{
if (p == NULL)
return (p);
}
/* get a random instant in an interval */
void
{
}
void
{
}
}
/* t1 = t2 - t3 */
static void
{
}
}
static void
do_openlog(void)
{
}
/* put a LOG_ERR message into the system log */
void
msglog(const char *p, ...)
{
trace_flush();
if (!openlog_done)
do_openlog();
if (ftrace != 0) {
}
}
/*
* Put a message about a bad system into the system log if
* we have not complained about it recently.
*
* It is desirable to complain about all bad systems, but not too often.
* In the worst case, it is not practical to keep track of all bad systems.
* For example, there can be many systems with the wrong password.
*/
void
{
int i;
const char *p1;
/*
* look for the oldest slot in the table
* or the slot for the bad router.
*/
for (i = MSG_SUBJECT_N; ; i--, ms1++) {
if (i == 0) {
/* Reuse a slot at most once every 10 minutes. */
} else {
}
break;
}
/*
* Repeat a complaint about a given system at
* most once an hour.
*/
break;
}
}
if (!openlog_done)
do_openlog();
trace_flush();
continue;
}
/* always display the message if tracing */
if (ftrace != 0) {
}
}
void
{
trace_flush();
if (!openlog_done)
do_openlog();
if (dump)
abort();
}
/* put a message into the system log */
void
{
trace_flush();
if (!openlog_done)
do_openlog();
if (ftrace != 0) {
}
}