#include "defs.h"
#include "pathnames.h"
#include <signal.h>
#include <fcntl.h>
#include <sys/file.h>
#include <userdefs.h>
#include <sys/stat.h>
#define IN_ROUTED_VERSION "2.22"
int stopint;
boolean_t supplier;
boolean_t supplier_set;
boolean_t save_space = _B_FALSE;
static boolean_t default_gateway;
static boolean_t background = _B_TRUE;
boolean_t ridhosts;
boolean_t mhome;
boolean_t advertise_mhome;
boolean_t auth_ok = _B_TRUE;
boolean_t no_install;
struct timeval epoch;
struct timeval clk;
static struct timeval prev_clk;
static int usec_fudge;
struct timeval now;
time_t now_stale;
time_t now_expire;
time_t now_garbage;
static struct timeval next_bcast;
struct timeval no_flash = {
EPOCH+SUPPLY_INTERVAL, 0
};
static struct timeval sync_kern_timer;
static fd_set fdbits;
static int sock_max;
int rip_sock = -1;
boolean_t rip_enabled;
static boolean_t openlog_done;
struct interface *rip_sock_interface;
int rt_sock;
static int open_rip_sock();
static void timevalsub(struct timeval *, struct timeval *, struct timeval *);
static void sigalrm(int);
static void sigterm(int);
int
main(int argc, char *argv[])
{
int n, off;
char *p, *q;
const char *cp;
struct timeval select_timeout, result;
fd_set ibits;
in_addr_t p_net, p_mask;
struct parm parm;
char *tracename = NULL;
boolean_t vflag = _B_FALSE;
boolean_t version = _B_FALSE;
int sigerr = 0;
FILE *pidfp;
mode_t pidmode = (S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
(void) setlocale(LC_ALL, "");
#if !defined(TEXT_DOMAIN)
#define TEXT_DOMAIN "SYS_TEXT"
#endif
(void) textdomain(TEXT_DOMAIN);
if (signal(SIGHUP, SIG_IGN) == SIG_ERR)
sigerr = errno;
ftrace = stdout;
if (gettimeofday(&clk, 0) == -1) {
logbad(_B_FALSE, "gettimeofday: %s", rip_strerror(errno));
}
prev_clk = clk;
epoch = clk;
epoch.tv_sec -= EPOCH;
now.tv_sec = EPOCH;
now_stale = EPOCH - STALE_TIME;
now_expire = EPOCH - EXPIRE_TIME;
now_garbage = EPOCH - GARBAGE_TIME;
select_timeout.tv_sec = 0;
while ((n = getopt(argc, argv, "sSqdghmpAztVvnT:F:P:")) != -1) {
switch (n) {
case 'A':
auth_ok = _B_FALSE;
break;
case 't':
if (new_tracelevel < 2)
new_tracelevel = 2;
background = _B_FALSE;
break;
case 'd':
background = _B_FALSE;
break;
case 'F':
n = FAKE_METRIC;
p = strchr(optarg, ',');
if (p != NULL) {
n = (int)strtoul(p+1, &q, 0);
if (*q == '\0' && p+1 != q &&
n <= HOPCNT_INFINITY-1 && n >= 1)
*p = '\0';
}
if (!getnet(optarg, &p_net, &p_mask)) {
if (p != NULL)
*p = ',';
msglog(gettext("bad network; \"-F %s\""),
optarg);
break;
}
(void) memset(&parm, 0, sizeof (parm));
parm.parm_net = p_net;
parm.parm_mask = p_mask;
parm.parm_d_metric = n;
cp = insert_parm(&parm);
if (cp != NULL)
msglog(gettext("bad -F: %s"), cp);
break;
case 'g':
(void) memset(&parm, 0, sizeof (parm));
parm.parm_d_metric = 1;
cp = insert_parm(&parm);
if (cp != NULL)
msglog(gettext("bad -g: %s"), cp);
else
default_gateway = _B_TRUE;
break;
case 'h':
ridhosts = _B_TRUE;
break;
case 'm':
mhome = _B_TRUE;
break;
case 'n':
no_install = _B_TRUE;
break;
case 'P':
q = strdup(optarg);
if (q == NULL)
logbad(_B_FALSE, "strdup: %s",
rip_strerror(errno));
cp = parse_parms(q, _B_FALSE);
if (cp != NULL)
msglog(gettext("%1$s in \"-P %2$s\""), cp,
optarg);
free(q);
break;
case 'q':
supplier = _B_FALSE;
supplier_set = _B_TRUE;
break;
case 's':
supplier = _B_TRUE;
supplier_set = _B_TRUE;
break;
case 'S':
save_space = _B_TRUE;
break;
case 'T':
tracename = optarg;
break;
case 'V':
version = _B_TRUE;
msglog(gettext("version " IN_ROUTED_VERSION));
break;
case 'v':
new_tracelevel = 1;
vflag = _B_TRUE;
break;
case 'z':
new_tracelevel++;
break;
default:
goto usage;
}
}
argc -= optind;
argv += optind;
if (tracename == NULL && argc >= 1) {
tracename = *argv++;
argc--;
}
if (tracename != NULL && tracename[0] == '\0')
goto usage;
if (vflag && tracename == NULL)
goto usage;
if (argc != 0) {
usage:
(void) fprintf(stderr, gettext(
"usage: in.routed [-AdghmnqsStVvz] "
"[-T <tracefile>]\n"));
(void) fprintf(stderr,
gettext("\t[-F <net>[/<mask>][,<metric>]] [-P <parms>]\n"));
logbad(_B_FALSE, gettext("excess arguments"));
}
if (geteuid() != 0) {
if (version)
exit(EXIT_SUCCESS);
logbad(_B_FALSE, gettext("requires UID 0"));
}
if (default_gateway) {
if (supplier_set && !supplier) {
msglog(gettext("-g and -q are incompatible"));
} else {
supplier = _B_TRUE;
supplier_set = _B_TRUE;
}
}
if (signal(SIGALRM, sigalrm) == SIG_ERR)
sigerr = errno;
if (!background)
if (signal(SIGHUP, sigterm) == SIG_ERR)
sigerr = errno;
if (signal(SIGTERM, sigterm) == SIG_ERR)
sigerr = errno;
if (signal(SIGINT, sigterm) == SIG_ERR)
sigerr = errno;
if (signal(SIGUSR1, sigtrace_more) == SIG_ERR)
sigerr = errno;
if (signal(SIGUSR2, sigtrace_less) == SIG_ERR)
sigerr = errno;
if (signal(SIGHUP, sigtrace_dump) == SIG_ERR)
sigerr = errno;
if (sigerr)
msglog("signal: %s", rip_strerror(sigerr));
if (background && daemon(0, 0) < 0)
BADERR(_B_FALSE, "daemon()");
if ((pidfp = fopen(PATH_PID, "w")) == NULL) {
(void) fprintf(stderr,
gettext("in.routed: unable to open " PATH_PID ": %s\n"),
strerror(errno));
} else {
(void) fprintf(pidfp, "%ld\n", getpid());
(void) fclose(pidfp);
(void) chmod(PATH_PID, pidmode);
}
srandom((int)(clk.tv_sec ^ clk.tv_usec ^ getpid()));
iftbl_alloc();
rt_sock = socket(PF_ROUTE, SOCK_RAW, AF_INET);
if (rt_sock < 0)
BADERR(_B_TRUE, "rt_sock = socket()");
if (fcntl(rt_sock, F_SETFL, O_NONBLOCK) == -1)
logbad(_B_TRUE, "fcntl(rt_sock) O_NONBLOCK: %s",
rip_strerror(errno));
off = 0;
if (setsockopt(rt_sock, SOL_SOCKET, SO_USELOOPBACK,
&off, sizeof (off)) < 0)
LOGERR("setsockopt(SO_USELOOPBACK,0)");
fix_select();
if (tracename != NULL) {
(void) strlcpy(inittracename, tracename,
sizeof (inittracename));
set_tracefile(inittracename, "%s", -1);
} else {
tracelevel_msg("%s", -1);
}
bufinit();
rtinit();
intvl_random(&next_bcast, EPOCH+MIN_WAITTIME, EPOCH+SUPPLY_INTERVAL);
age_timer.tv_usec = next_bcast.tv_usec;
age_timer.tv_sec = EPOCH+MIN_WAITTIME;
rdisc_timer = next_bcast;
ifscan_timer.tv_usec = next_bcast.tv_usec;
rip_sock = open_rip_sock();
gwkludge();
ifscan();
rip_query();
rdisc_sol();
if (new_tracelevel == 0)
trace_close(background);
for (;;) {
prev_clk = clk;
if (gettimeofday(&clk, 0) == -1) {
logbad(_B_FALSE, "gettimeofday: %s",
rip_strerror(errno));
}
if (prev_clk.tv_sec == clk.tv_sec &&
prev_clk.tv_usec == clk.tv_usec+usec_fudge) {
clk.tv_usec += ++usec_fudge;
} else {
time_t dt;
usec_fudge = 0;
timevalsub(&result, &clk, &prev_clk);
if (result.tv_sec < 0 || result.tv_sec >
select_timeout.tv_sec + 5) {
dt = result.tv_sec;
if (dt > 0)
dt -= select_timeout.tv_sec;
trace_act("time changed by %d sec", (int)dt);
epoch.tv_sec += dt;
}
}
timevalsub(&now, &clk, &epoch);
now_stale = now.tv_sec - STALE_TIME;
now_expire = now.tv_sec - EXPIRE_TIME;
now_garbage = now.tv_sec - GARBAGE_TIME;
set_tracelevel();
if (stopint != 0) {
trace_off("exiting with signal %d", stopint);
break;
}
timevalsub(&select_timeout, &ifscan_timer, &now);
if (select_timeout.tv_sec <= 0) {
select_timeout.tv_sec = 0;
ifscan();
rip_query();
continue;
}
timevalsub(&result, &sync_kern_timer, &now);
if (result.tv_sec <= 0) {
sync_kern();
sync_kern_timer.tv_sec = (now.tv_sec
+ CHECK_QUIET_INTERVAL);
continue;
}
if (timercmp(&result, &select_timeout, < ))
select_timeout = result;
if (should_supply(NULL) || advertise_mhome) {
timevalsub(&result, &next_bcast, &now);
if (result.tv_sec <= 0) {
age(0);
age_peer_info();
rip_bcast(0);
n = 1 + (0-result.tv_sec)/SUPPLY_INTERVAL;
next_bcast.tv_sec += n*SUPPLY_INTERVAL;
continue;
}
if (timercmp(&result, &select_timeout, < ))
select_timeout = result;
}
if (need_flash && should_supply(NULL) &&
no_flash.tv_sec+MIN_WAITTIME < next_bcast.tv_sec) {
if (!timercmp(&no_flash, &now, > ))
rip_bcast(1);
timevalsub(&result, &no_flash, &now);
if (timercmp(&result, &select_timeout, < ))
select_timeout = result;
}
timevalsub(&result, &age_timer, &now);
if (result.tv_sec <= 0) {
age(0);
continue;
}
if (timercmp(&result, &select_timeout, < ))
select_timeout = result;
timevalsub(&result, &need_kern, &now);
if (result.tv_sec <= 0) {
age(0);
continue;
}
if (timercmp(&result, &select_timeout, < ))
select_timeout = result;
if (!timercmp(&rdisc_timer, &now, > )) {
rdisc_age(0);
continue;
}
timevalsub(&result, &rdisc_timer, &now);
if (timercmp(&result, &select_timeout, < ))
select_timeout = result;
if (select_timeout.tv_sec > 100000000)
select_timeout.tv_sec = 0;
trace_flush();
ibits = fdbits;
n = select(sock_max, &ibits, 0, 0, &select_timeout);
if (n <= 0) {
if (n < 0 && errno != EINTR && errno != EAGAIN)
BADERR(_B_TRUE, "select");
continue;
}
if (FD_ISSET(rt_sock, &ibits)) {
read_rt();
n--;
}
if (rdisc_sock >= 0 && FD_ISSET(rdisc_sock, &ibits)) {
read_d();
n--;
}
if (rdisc_mib_sock >= 0 && FD_ISSET(rdisc_mib_sock, &ibits)) {
process_d_mib_sock();
n--;
}
if (rip_sock >= 0 && FD_ISSET(rip_sock, &ibits)) {
if (read_rip() == -1) {
rip_enabled = _B_FALSE;
trace_off("main rip socket failed");
(void) close(rip_sock);
rip_sock = -1;
fix_select();
break;
}
n--;
}
}
rip_bcast(0);
rdisc_adv(_B_FALSE);
(void) unlink(PATH_PID);
return (stopint | 128);
}
static void
sigalrm(int sig)
{
ifscan_timer.tv_sec = now.tv_sec;
trace_act("SIGALRM");
if (signal(sig, sigalrm) == SIG_ERR)
msglog("signal: %s", rip_strerror(errno));
}
static void
sigterm(int sig)
{
stopint = sig;
if (signal(sig, SIG_DFL) == SIG_ERR)
msglog("signal: %s", rip_strerror(errno));
}
void
fix_select(void)
{
(void) FD_ZERO(&fdbits);
sock_max = 0;
FD_SET(rt_sock, &fdbits);
if (sock_max <= rt_sock)
sock_max = rt_sock+1;
if (rip_sock >= 0) {
FD_SET(rip_sock, &fdbits);
if (sock_max <= rip_sock)
sock_max = rip_sock+1;
}
if (rdisc_sock >= 0) {
FD_SET(rdisc_sock, &fdbits);
if (sock_max <= rdisc_sock)
sock_max = rdisc_sock+1;
FD_SET(rdisc_mib_sock, &fdbits);
if (sock_max <= rdisc_mib_sock)
sock_max = rdisc_mib_sock+1;
}
}
void
fix_sock(int sock,
const char *name)
{
int on;
#define MIN_SOCKBUF (4*1024)
static int rbuf;
if (fcntl(sock, F_SETFL, O_NONBLOCK) == -1)
logbad(_B_TRUE, "fcntl(%s) O_NONBLOCK: %s", name,
rip_strerror(errno));
on = 1;
if (setsockopt(sock, SOL_SOCKET, SO_BROADCAST, &on, sizeof (on)) < 0)
msglog("setsockopt(%s,SO_BROADCAST): %s",
name, rip_strerror(errno));
if (rbuf >= MIN_SOCKBUF) {
if (setsockopt(sock, SOL_SOCKET, SO_RCVBUF,
&rbuf, sizeof (rbuf)) < 0)
msglog("setsockopt(%s,SO_RCVBUF=%d): %s",
name, rbuf, rip_strerror(errno));
} else {
for (rbuf = 60*1024; ; rbuf -= 4096) {
if (setsockopt(sock, SOL_SOCKET, SO_RCVBUF,
&rbuf, sizeof (rbuf)) == 0) {
trace_act("RCVBUF=%d", rbuf);
break;
}
if (rbuf < MIN_SOCKBUF) {
msglog("setsockopt(%s,SO_RCVBUF = %d): %s",
name, rbuf, rip_strerror(errno));
break;
}
}
}
}
static int
open_rip_sock()
{
struct sockaddr_in sin;
unsigned char ttl;
int s;
int on = 1;
if ((s = socket(PF_INET, SOCK_DGRAM, 0)) < 0)
BADERR(_B_TRUE, "rip_sock = socket()");
(void) memset(&sin, 0, sizeof (sin));
sin.sin_family = AF_INET;
sin.sin_port = htons(RIP_PORT);
sin.sin_addr.s_addr = INADDR_ANY;
if (bind(s, (struct sockaddr *)&sin, sizeof (sin)) < 0) {
BADERR(_B_FALSE, "bind(rip_sock)");
}
fix_sock(s, "rip_sock");
ttl = 1;
if (setsockopt(s, IPPROTO_IP, IP_MULTICAST_TTL,
&ttl, sizeof (ttl)) < 0)
DBGERR(_B_TRUE, "rip_sock setsockopt(IP_MULTICAST_TTL)");
if (setsockopt(s, IPPROTO_IP, IP_RECVIF, &on, sizeof (on)))
BADERR(_B_FALSE, "setsockopt(IP_RECVIF)");
return (s);
}
void
rip_off(void)
{
struct ip_mreq m;
struct interface *ifp;
char addrstr[INET_ADDRSTRLEN];
if (rip_enabled && !mhome) {
trace_act("turn off RIP");
for (ifp = ifnet; ifp != NULL; ifp = ifp->int_next) {
if ((ifp->int_if_flags & IFF_MULTICAST) &&
!IS_IFF_QUIET(ifp->int_if_flags) &&
!IS_RIP_IN_OFF(ifp->int_state) &&
!(ifp->int_state & IS_DUP)) {
m.imr_multiaddr.s_addr =
htonl(INADDR_RIP_GROUP);
m.imr_interface.s_addr =
(ifp->int_if_flags & IFF_POINTOPOINT) ?
ifp->int_dstaddr : ifp->int_addr;
(void) strlcpy(addrstr,
inet_ntoa(m.imr_multiaddr),
sizeof (addrstr));
if (setsockopt(rip_sock, IPPROTO_IP,
IP_DROP_MEMBERSHIP, &m,
sizeof (m)) < 0 &&
errno != EADDRNOTAVAIL && errno != ENOENT)
writelog(LOG_WARNING,
"%s: setsockopt(IP_DROP_MEMBERSHIP "
"%s, %s): %s", ifp->int_name,
addrstr, inet_ntoa(m.imr_interface),
rip_strerror(errno));
}
}
rip_enabled = _B_FALSE;
age(0);
}
}
void
rip_mcast_on(struct interface *ifp)
{
struct ip_mreq m;
if (!IS_RIP_IN_OFF(ifp->int_state) &&
(ifp->int_if_flags & IFF_MULTICAST) &&
!IS_IFF_QUIET(ifp->int_if_flags) &&
!(ifp->int_state & IS_DUP)) {
m.imr_multiaddr.s_addr = htonl(INADDR_RIP_GROUP);
m.imr_interface.s_addr = (ifp->int_if_flags & IFF_POINTOPOINT) ?
ifp->int_dstaddr : ifp->int_addr;
if ((setsockopt(rip_sock, IPPROTO_IP, IP_ADD_MEMBERSHIP,
&m, sizeof (m)) < 0) && !(ifp->int_state & IS_BROKE))
writelog(LOG_WARNING,
"Could not join 224.0.0.9 on interface %s: %s",
ifp->int_name, rip_strerror(errno));
}
}
void
rip_mcast_off(struct interface *ifp)
{
struct ip_mreq m;
if ((ifp->int_if_flags & IFF_MULTICAST) &&
!IS_IFF_QUIET(ifp->int_if_flags) && rip_enabled) {
m.imr_multiaddr.s_addr = htonl(INADDR_RIP_GROUP);
m.imr_interface.s_addr = (ifp->int_if_flags & IFF_POINTOPOINT) ?
ifp->int_dstaddr : ifp->int_addr;
if ((setsockopt(rip_sock, IPPROTO_IP, IP_DROP_MEMBERSHIP,
&m, sizeof (m)) < 0) && errno != EADDRNOTAVAIL &&
errno != ENOENT)
writelog(LOG_WARNING,
"setsockopt(IP_DROP_MEMBERSHIP RIP) for %s: %s",
ifp->int_name, rip_strerror(errno));
}
}
void
rip_on(struct interface *ifp)
{
if (rip_enabled) {
if (ifp != NULL)
rip_mcast_on(ifp);
return;
}
if (rip_interfaces > 0 && (!rdisc_ok || rip_interfaces > 1)) {
trace_act("turn on RIP");
rip_enabled = _B_TRUE;
rip_sock_interface = NULL;
if (next_bcast.tv_sec < now.tv_sec+MIN_WAITTIME)
next_bcast.tv_sec = now.tv_sec+MIN_WAITTIME;
for (ifp = ifnet; ifp != NULL; ifp = ifp->int_next) {
ifp->int_query_time = NEVER;
rip_mcast_on(ifp);
}
ifscan_timer.tv_sec = now.tv_sec;
}
fix_select();
}
void *
rtmalloc(size_t size,
const char *msg)
{
void *p = malloc(size);
if (p == NULL)
logbad(_B_TRUE, "malloc(%lu) failed in %s: %s", (ulong_t)size,
msg, rip_strerror(errno));
return (p);
}
void
intvl_random(struct timeval *tp,
ulong_t lo,
ulong_t hi)
{
tp->tv_sec = (time_t)(hi == lo ? lo : (lo + random() % ((hi - lo))));
tp->tv_usec = random() % 1000000;
}
void
timevaladd(struct timeval *t1,
struct timeval *t2)
{
t1->tv_sec += t2->tv_sec;
if ((t1->tv_usec += t2->tv_usec) >= 1000000) {
t1->tv_sec++;
t1->tv_usec -= 1000000;
}
}
static void
timevalsub(struct timeval *t1,
struct timeval *t2,
struct timeval *t3)
{
t1->tv_sec = t2->tv_sec - t3->tv_sec;
if ((t1->tv_usec = t2->tv_usec - t3->tv_usec) < 0) {
t1->tv_sec--;
t1->tv_usec += 1000000;
}
}
static void
do_openlog(void)
{
openlog_done = _B_TRUE;
openlog("in.routed", LOG_PID | LOG_ODELAY, LOG_DAEMON);
}
void
msglog(const char *p, ...)
{
va_list args;
trace_flush();
if (!openlog_done)
do_openlog();
va_start(args, p);
vsyslog(LOG_ERR, p, args);
if (ftrace != 0) {
if (ftrace == stdout)
(void) fputs("in.routed: ", ftrace);
(void) vfprintf(ftrace, p, args);
(void) fputc('\n', ftrace);
}
}
void
msglim(struct msg_limit *lim, in_addr_t addr, const char *p, ...)
{
va_list args;
int i;
struct msg_sub *ms1, *ms;
const char *p1;
va_start(args, p);
ms = ms1 = lim->subs;
for (i = MSG_SUBJECT_N; ; i--, ms1++) {
if (i == 0) {
if (lim->reuse > now.tv_sec) {
ms = NULL;
} else {
lim->reuse = now.tv_sec + 10*60;
}
break;
}
if (ms->addr == addr) {
if (ms->until > now.tv_sec)
ms = NULL;
break;
}
if (ms->until < ms1->until)
ms = ms1;
}
if (ms != NULL) {
ms->addr = addr;
ms->until = now.tv_sec + 60*60;
if (!openlog_done)
do_openlog();
trace_flush();
for (p1 = p; *p1 == ' '; p1++)
continue;
vsyslog(LOG_ERR, p1, args);
}
if (ftrace != 0) {
(void) vfprintf(ftrace, p, args);
(void) fputc('\n', ftrace);
}
}
void
logbad(boolean_t dump, const char *p, ...)
{
va_list args;
trace_flush();
if (!openlog_done)
do_openlog();
va_start(args, p);
vsyslog(LOG_ERR, p, args);
(void) fputs(gettext("in.routed: "), stderr);
(void) vfprintf(stderr, p, args);
(void) fputs(gettext("; giving up\n"), stderr);
(void) fflush(stderr);
if (dump)
abort();
exit(EXIT_FAILURE);
}
void
writelog(int level, const char *p, ...)
{
va_list args;
trace_flush();
if (!openlog_done)
do_openlog();
va_start(args, p);
vsyslog(level, p, args);
if (ftrace != 0) {
if (ftrace == stdout)
(void) fputs("in.routed: ", ftrace);
(void) vfprintf(ftrace, p, args);
(void) fputc('\n', ftrace);
}
}