2003-08-17 11:11:32 +00:00
|
|
|
/* $KAME: rtsold.c,v 1.67 2003/05/17 18:16:15 itojun Exp $ */
|
2000-10-06 23:46:52 +00:00
|
|
|
|
2017-11-20 19:49:47 +00:00
|
|
|
/*-
|
|
|
|
* SPDX-License-Identifier: BSD-3-Clause
|
|
|
|
*
|
1999-12-28 02:37:14 +00:00
|
|
|
* Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
|
|
|
|
* All rights reserved.
|
2000-07-05 10:14:11 +00:00
|
|
|
*
|
1999-12-28 02:37:14 +00:00
|
|
|
* 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
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* 3. Neither the name of the project nor the names of its contributors
|
|
|
|
* may be used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
2000-07-05 10:14:11 +00:00
|
|
|
*
|
1999-12-28 02:37:14 +00:00
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE PROJECT 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 PROJECT 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.
|
|
|
|
*
|
|
|
|
* $FreeBSD$
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
2010-02-02 18:38:17 +00:00
|
|
|
#include <sys/ioctl.h>
|
2000-07-05 10:14:11 +00:00
|
|
|
#include <sys/socket.h>
|
2003-08-14 15:47:31 +00:00
|
|
|
#include <sys/param.h>
|
1999-12-28 02:37:14 +00:00
|
|
|
|
2000-07-05 10:14:11 +00:00
|
|
|
#include <net/if.h>
|
1999-12-28 02:37:14 +00:00
|
|
|
#include <net/if_dl.h>
|
|
|
|
|
|
|
|
#include <netinet/in.h>
|
|
|
|
#include <netinet/icmp6.h>
|
2010-02-02 18:38:17 +00:00
|
|
|
#include <netinet/in_var.h>
|
2011-06-08 16:03:29 +00:00
|
|
|
#include <arpa/inet.h>
|
2010-02-02 18:38:17 +00:00
|
|
|
|
|
|
|
#include <netinet6/nd6.h>
|
1999-12-28 02:37:14 +00:00
|
|
|
|
|
|
|
#include <signal.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <syslog.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
2013-08-05 20:13:02 +00:00
|
|
|
#include <time.h>
|
1999-12-28 02:37:14 +00:00
|
|
|
#include <errno.h>
|
|
|
|
#include <err.h>
|
|
|
|
#include <stdarg.h>
|
2000-10-06 23:46:52 +00:00
|
|
|
#include <ifaddrs.h>
|
2003-08-14 18:43:57 +00:00
|
|
|
#include <poll.h>
|
2003-08-08 16:56:01 +00:00
|
|
|
|
1999-12-28 02:37:14 +00:00
|
|
|
#include "rtsold.h"
|
|
|
|
|
2011-06-06 03:06:43 +00:00
|
|
|
#define RTSOL_DUMPFILE "/var/run/rtsold.dump";
|
|
|
|
#define RTSOL_PIDFILE "/var/run/rtsold.pid";
|
|
|
|
|
2013-08-05 20:13:02 +00:00
|
|
|
struct timespec tm_max;
|
2003-08-08 16:56:01 +00:00
|
|
|
static int log_upto = 999;
|
|
|
|
static int fflag = 0;
|
|
|
|
|
2009-09-12 22:14:58 +00:00
|
|
|
int Fflag = 0; /* force setting sysctl parameters */
|
2000-10-06 23:46:52 +00:00
|
|
|
int aflag = 0;
|
|
|
|
int dflag = 0;
|
2011-09-12 23:55:23 +00:00
|
|
|
int uflag = 0;
|
2003-08-08 16:56:01 +00:00
|
|
|
|
2011-06-06 03:06:43 +00:00
|
|
|
const char *otherconf_script;
|
|
|
|
const char *resolvconf_script = "/sbin/resolvconf";
|
1999-12-28 02:37:14 +00:00
|
|
|
|
2005-06-08 23:51:42 +00:00
|
|
|
/* protocol constants */
|
2000-07-05 10:14:11 +00:00
|
|
|
#define MAX_RTR_SOLICITATION_DELAY 1 /* second */
|
|
|
|
#define RTR_SOLICITATION_INTERVAL 4 /* seconds */
|
|
|
|
#define MAX_RTR_SOLICITATIONS 3 /* times */
|
1999-12-28 02:37:14 +00:00
|
|
|
|
2003-08-08 16:56:01 +00:00
|
|
|
/*
|
2003-08-14 15:57:52 +00:00
|
|
|
* implementation dependent constants in seconds
|
2003-08-08 16:56:01 +00:00
|
|
|
* XXX: should be configurable
|
|
|
|
*/
|
|
|
|
#define PROBE_INTERVAL 60
|
1999-12-28 02:37:14 +00:00
|
|
|
|
|
|
|
/* static variables and functions */
|
|
|
|
static int mobile_node = 0;
|
2011-06-06 03:06:43 +00:00
|
|
|
static const char *pidfilename = RTSOL_PIDFILE;
|
|
|
|
|
2004-01-14 17:59:38 +00:00
|
|
|
#ifndef SMALL
|
1999-12-28 02:37:14 +00:00
|
|
|
static int do_dump;
|
2011-06-06 03:06:43 +00:00
|
|
|
static const char *dumpfilename = RTSOL_DUMPFILE;
|
2004-01-14 17:59:38 +00:00
|
|
|
#endif
|
1999-12-28 02:37:14 +00:00
|
|
|
|
2000-07-05 10:14:11 +00:00
|
|
|
#if 0
|
2007-11-07 10:53:41 +00:00
|
|
|
static int ifreconfig(char *);
|
2000-07-05 10:14:11 +00:00
|
|
|
#endif
|
2011-06-06 03:06:43 +00:00
|
|
|
|
2007-11-07 10:53:41 +00:00
|
|
|
static int make_packet(struct ifinfo *);
|
2013-08-05 20:13:02 +00:00
|
|
|
static struct timespec *rtsol_check_timer(void);
|
1999-12-28 02:37:14 +00:00
|
|
|
|
2004-01-14 17:59:38 +00:00
|
|
|
#ifndef SMALL
|
2007-11-07 10:53:41 +00:00
|
|
|
static void rtsold_set_dump_file(int);
|
2004-01-14 17:59:38 +00:00
|
|
|
#endif
|
2011-06-06 03:06:43 +00:00
|
|
|
static void usage(void);
|
1999-12-28 02:37:14 +00:00
|
|
|
|
|
|
|
int
|
2004-01-14 17:16:19 +00:00
|
|
|
main(int argc, char **argv)
|
1999-12-28 02:37:14 +00:00
|
|
|
{
|
2003-08-14 15:47:31 +00:00
|
|
|
int s, ch, once = 0;
|
2013-08-05 20:13:02 +00:00
|
|
|
struct timespec *timeout;
|
2010-02-27 10:19:39 +00:00
|
|
|
const char *opts;
|
2003-08-14 18:43:57 +00:00
|
|
|
struct pollfd set[2];
|
2003-08-14 15:47:31 +00:00
|
|
|
int rtsock;
|
2011-06-08 04:06:56 +00:00
|
|
|
char *argv0;
|
1999-12-28 02:37:14 +00:00
|
|
|
|
2011-06-06 03:06:43 +00:00
|
|
|
#ifndef SMALL
|
|
|
|
/* rtsold */
|
2011-09-12 23:55:23 +00:00
|
|
|
opts = "adDfFm1O:p:R:u";
|
2011-06-06 03:06:43 +00:00
|
|
|
#else
|
|
|
|
/* rtsol */
|
2011-09-12 23:55:23 +00:00
|
|
|
opts = "adDFO:R:u";
|
2011-06-06 03:06:43 +00:00
|
|
|
fflag = 1;
|
|
|
|
once = 1;
|
|
|
|
#endif
|
2011-06-08 04:06:56 +00:00
|
|
|
argv0 = argv[0];
|
|
|
|
|
1999-12-28 02:37:14 +00:00
|
|
|
while ((ch = getopt(argc, argv, opts)) != -1) {
|
2000-10-06 23:46:52 +00:00
|
|
|
switch (ch) {
|
|
|
|
case 'a':
|
|
|
|
aflag = 1;
|
|
|
|
break;
|
|
|
|
case 'd':
|
2011-09-12 23:55:23 +00:00
|
|
|
dflag += 1;
|
2000-10-06 23:46:52 +00:00
|
|
|
break;
|
|
|
|
case 'D':
|
2011-09-12 23:55:23 +00:00
|
|
|
dflag += 2;
|
2000-10-06 23:46:52 +00:00
|
|
|
break;
|
|
|
|
case 'f':
|
|
|
|
fflag = 1;
|
|
|
|
break;
|
2004-01-14 17:42:03 +00:00
|
|
|
case 'F':
|
|
|
|
Fflag = 1;
|
|
|
|
break;
|
2000-10-06 23:46:52 +00:00
|
|
|
case 'm':
|
|
|
|
mobile_node = 1;
|
|
|
|
break;
|
|
|
|
case '1':
|
|
|
|
once = 1;
|
|
|
|
break;
|
2003-08-08 16:42:37 +00:00
|
|
|
case 'O':
|
|
|
|
otherconf_script = optarg;
|
|
|
|
break;
|
2011-09-12 23:55:23 +00:00
|
|
|
case 'p':
|
2011-06-06 03:06:43 +00:00
|
|
|
pidfilename = optarg;
|
|
|
|
break;
|
|
|
|
case 'R':
|
|
|
|
resolvconf_script = optarg;
|
|
|
|
break;
|
2011-09-12 23:55:23 +00:00
|
|
|
case 'u':
|
|
|
|
uflag = 1;
|
|
|
|
break;
|
2000-10-06 23:46:52 +00:00
|
|
|
default:
|
2011-06-06 03:06:43 +00:00
|
|
|
usage();
|
|
|
|
exit(1);
|
1999-12-28 02:37:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
argc -= optind;
|
|
|
|
argv += optind;
|
2000-10-06 23:46:52 +00:00
|
|
|
|
2003-08-17 11:11:32 +00:00
|
|
|
if ((!aflag && argc == 0) || (aflag && argc != 0)) {
|
2011-06-06 03:06:43 +00:00
|
|
|
usage();
|
|
|
|
exit(1);
|
2000-10-06 23:46:52 +00:00
|
|
|
}
|
1999-12-28 02:37:14 +00:00
|
|
|
|
2013-08-05 20:13:02 +00:00
|
|
|
/* Generate maximum time in timespec. */
|
2013-08-06 15:49:18 +00:00
|
|
|
tm_max.tv_sec = (-1) & ~((time_t)1 << ((sizeof(tm_max.tv_sec) * 8) - 1));
|
|
|
|
tm_max.tv_nsec = (-1) & ~((long)1 << ((sizeof(tm_max.tv_nsec) * 8) - 1));
|
2013-08-05 20:13:02 +00:00
|
|
|
|
1999-12-28 02:37:14 +00:00
|
|
|
/* set log level */
|
2011-09-12 23:55:23 +00:00
|
|
|
if (dflag > 1)
|
|
|
|
log_upto = LOG_DEBUG;
|
|
|
|
else if (dflag > 0)
|
|
|
|
log_upto = LOG_INFO;
|
|
|
|
else
|
1999-12-28 02:37:14 +00:00
|
|
|
log_upto = LOG_NOTICE;
|
2011-09-12 23:55:23 +00:00
|
|
|
|
1999-12-28 02:37:14 +00:00
|
|
|
if (!fflag) {
|
|
|
|
char *ident;
|
2003-08-08 16:56:01 +00:00
|
|
|
|
2011-06-08 04:06:56 +00:00
|
|
|
ident = strrchr(argv0, '/');
|
1999-12-28 02:37:14 +00:00
|
|
|
if (!ident)
|
2011-06-08 04:06:56 +00:00
|
|
|
ident = argv0;
|
1999-12-28 02:37:14 +00:00
|
|
|
else
|
|
|
|
ident++;
|
|
|
|
openlog(ident, LOG_NDELAY|LOG_PID, LOG_DAEMON);
|
|
|
|
if (log_upto >= 0)
|
|
|
|
setlogmask(LOG_UPTO(log_upto));
|
|
|
|
}
|
|
|
|
|
2003-08-08 16:42:37 +00:00
|
|
|
if (otherconf_script && *otherconf_script != '/') {
|
|
|
|
errx(1, "configuration script (%s) must be an absolute path",
|
|
|
|
otherconf_script);
|
|
|
|
}
|
2011-06-06 03:06:43 +00:00
|
|
|
if (resolvconf_script && *resolvconf_script != '/') {
|
|
|
|
errx(1, "configuration script (%s) must be an absolute path",
|
|
|
|
resolvconf_script);
|
|
|
|
}
|
|
|
|
if (pidfilename && *pidfilename != '/') {
|
|
|
|
errx(1, "pid filename (%s) must be an absolute path",
|
|
|
|
pidfilename);
|
|
|
|
}
|
1999-12-28 02:37:14 +00:00
|
|
|
|
2011-09-12 23:55:23 +00:00
|
|
|
#if (__FreeBSD_version < 900000)
|
2004-01-14 17:42:03 +00:00
|
|
|
if (Fflag) {
|
|
|
|
setinet6sysctl(IPV6CTL_FORWARDING, 0);
|
|
|
|
} else {
|
|
|
|
/* warn if forwarding is up */
|
|
|
|
if (getinet6sysctl(IPV6CTL_FORWARDING))
|
|
|
|
warnx("kernel is configured as a router, not a host");
|
|
|
|
}
|
2011-09-12 23:55:23 +00:00
|
|
|
#endif
|
1999-12-28 02:37:14 +00:00
|
|
|
|
2004-01-14 17:59:38 +00:00
|
|
|
#ifndef SMALL
|
1999-12-28 02:37:14 +00:00
|
|
|
/* initialization to dump internal status to a file */
|
2003-08-14 15:57:52 +00:00
|
|
|
signal(SIGUSR1, rtsold_set_dump_file);
|
2004-01-14 17:59:38 +00:00
|
|
|
#endif
|
1999-12-28 02:37:14 +00:00
|
|
|
|
2003-08-14 18:13:34 +00:00
|
|
|
if (!fflag)
|
|
|
|
daemon(0, 0); /* act as a daemon */
|
|
|
|
|
2000-07-05 10:14:11 +00:00
|
|
|
/*
|
|
|
|
* Open a socket for sending RS and receiving RA.
|
|
|
|
* This should be done before calling ifinit(), since the function
|
|
|
|
* uses the socket.
|
|
|
|
*/
|
2000-10-06 23:46:52 +00:00
|
|
|
if ((s = sockopen()) < 0) {
|
2003-08-14 18:13:34 +00:00
|
|
|
warnmsg(LOG_ERR, __func__, "failed to open a socket");
|
|
|
|
exit(1);
|
2000-10-06 23:46:52 +00:00
|
|
|
}
|
2003-08-14 18:43:57 +00:00
|
|
|
set[0].fd = s;
|
|
|
|
set[0].events = POLLIN;
|
|
|
|
set[1].fd = -1;
|
|
|
|
|
2001-06-11 12:39:29 +00:00
|
|
|
if ((rtsock = rtsock_open()) < 0) {
|
2003-08-14 18:13:34 +00:00
|
|
|
warnmsg(LOG_ERR, __func__, "failed to open a socket");
|
|
|
|
exit(1);
|
2001-06-11 12:39:29 +00:00
|
|
|
}
|
2003-08-14 18:43:57 +00:00
|
|
|
set[1].fd = rtsock;
|
|
|
|
set[1].events = POLLIN;
|
2003-08-14 15:47:31 +00:00
|
|
|
|
1999-12-28 02:37:14 +00:00
|
|
|
/* configuration per interface */
|
2000-10-06 23:46:52 +00:00
|
|
|
if (ifinit()) {
|
2003-08-14 18:13:34 +00:00
|
|
|
warnmsg(LOG_ERR, __func__,
|
2005-06-09 09:28:35 +00:00
|
|
|
"failed to initialize interfaces");
|
2003-08-14 18:13:34 +00:00
|
|
|
exit(1);
|
2000-10-06 23:46:52 +00:00
|
|
|
}
|
2003-08-17 11:11:32 +00:00
|
|
|
if (aflag)
|
|
|
|
argv = autoifprobe();
|
|
|
|
while (argv && *argv) {
|
2000-10-06 23:46:52 +00:00
|
|
|
if (ifconfig(*argv)) {
|
2003-08-14 18:13:34 +00:00
|
|
|
warnmsg(LOG_ERR, __func__,
|
|
|
|
"failed to initialize %s", *argv);
|
|
|
|
exit(1);
|
2000-10-06 23:46:52 +00:00
|
|
|
}
|
1999-12-28 02:37:14 +00:00
|
|
|
argv++;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* setup for probing default routers */
|
2000-10-06 23:46:52 +00:00
|
|
|
if (probe_init()) {
|
2003-08-14 18:13:34 +00:00
|
|
|
warnmsg(LOG_ERR, __func__,
|
|
|
|
"failed to setup for probing routers");
|
|
|
|
exit(1);
|
2000-10-06 23:46:52 +00:00
|
|
|
/*NOTREACHED*/
|
|
|
|
}
|
1999-12-28 02:37:14 +00:00
|
|
|
|
|
|
|
/* dump the current pid */
|
|
|
|
if (!once) {
|
|
|
|
pid_t pid = getpid();
|
|
|
|
FILE *fp;
|
|
|
|
|
|
|
|
if ((fp = fopen(pidfilename, "w")) == NULL)
|
2003-08-08 16:38:23 +00:00
|
|
|
warnmsg(LOG_ERR, __func__,
|
2003-08-08 16:56:01 +00:00
|
|
|
"failed to open a pid log file(%s): %s",
|
|
|
|
pidfilename, strerror(errno));
|
1999-12-28 02:37:14 +00:00
|
|
|
else {
|
|
|
|
fprintf(fp, "%d\n", pid);
|
|
|
|
fclose(fp);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
while (1) { /* main loop */
|
|
|
|
int e;
|
2004-01-14 17:59:38 +00:00
|
|
|
#ifndef SMALL
|
1999-12-28 02:37:14 +00:00
|
|
|
if (do_dump) { /* SIGUSR1 */
|
|
|
|
do_dump = 0;
|
|
|
|
rtsold_dump_file(dumpfilename);
|
|
|
|
}
|
2004-01-14 17:59:38 +00:00
|
|
|
#endif
|
2003-08-08 16:56:01 +00:00
|
|
|
|
1999-12-28 02:37:14 +00:00
|
|
|
timeout = rtsol_check_timer();
|
|
|
|
|
|
|
|
if (once) {
|
|
|
|
struct ifinfo *ifi;
|
|
|
|
|
|
|
|
/* if we have no timeout, we are done (or failed) */
|
|
|
|
if (timeout == NULL)
|
|
|
|
break;
|
|
|
|
|
|
|
|
/* if all interfaces have got RA packet, we are done */
|
2011-06-06 03:06:43 +00:00
|
|
|
TAILQ_FOREACH(ifi, &ifinfo_head, ifi_next) {
|
1999-12-28 02:37:14 +00:00
|
|
|
if (ifi->state != IFS_DOWN && ifi->racnt == 0)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (ifi == NULL)
|
|
|
|
break;
|
|
|
|
}
|
2013-08-05 20:13:02 +00:00
|
|
|
e = poll(set, 2, timeout ? (timeout->tv_sec * 1000 + timeout->tv_nsec / 1000 / 1000) : INFTIM);
|
2001-06-11 12:39:29 +00:00
|
|
|
if (e < 1) {
|
1999-12-28 02:37:14 +00:00
|
|
|
if (e < 0 && errno != EINTR) {
|
2003-08-08 16:38:23 +00:00
|
|
|
warnmsg(LOG_ERR, __func__, "select: %s",
|
2003-08-08 16:56:01 +00:00
|
|
|
strerror(errno));
|
1999-12-28 02:37:14 +00:00
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* packet reception */
|
2003-08-14 18:43:57 +00:00
|
|
|
if (set[1].revents & POLLIN)
|
2001-06-11 12:39:29 +00:00
|
|
|
rtsock_input(rtsock);
|
2003-08-14 18:43:57 +00:00
|
|
|
if (set[0].revents & POLLIN)
|
1999-12-28 02:37:14 +00:00
|
|
|
rtsol_input(s);
|
|
|
|
}
|
|
|
|
/* NOTREACHED */
|
|
|
|
|
2011-06-06 03:06:43 +00:00
|
|
|
return (0);
|
1999-12-28 02:37:14 +00:00
|
|
|
}
|
|
|
|
|
2003-08-17 11:11:32 +00:00
|
|
|
int
|
1999-12-28 02:37:14 +00:00
|
|
|
ifconfig(char *ifname)
|
|
|
|
{
|
2011-06-06 03:06:43 +00:00
|
|
|
struct ifinfo *ifi;
|
1999-12-28 02:37:14 +00:00
|
|
|
struct sockaddr_dl *sdl;
|
|
|
|
int flags;
|
|
|
|
|
|
|
|
if ((sdl = if_nametosdl(ifname)) == NULL) {
|
2003-08-08 16:38:23 +00:00
|
|
|
warnmsg(LOG_ERR, __func__,
|
2003-08-08 16:56:01 +00:00
|
|
|
"failed to get link layer information for %s", ifname);
|
2011-06-06 03:06:43 +00:00
|
|
|
return (-1);
|
1999-12-28 02:37:14 +00:00
|
|
|
}
|
|
|
|
if (find_ifinfo(sdl->sdl_index)) {
|
2003-08-08 16:38:23 +00:00
|
|
|
warnmsg(LOG_ERR, __func__,
|
2003-08-08 16:56:01 +00:00
|
|
|
"interface %s was already configured", ifname);
|
2000-07-05 10:14:11 +00:00
|
|
|
free(sdl);
|
2011-06-06 03:06:43 +00:00
|
|
|
return (-1);
|
1999-12-28 02:37:14 +00:00
|
|
|
}
|
|
|
|
|
2011-09-12 23:55:23 +00:00
|
|
|
if (Fflag) {
|
|
|
|
struct in6_ndireq nd;
|
|
|
|
int s;
|
|
|
|
|
|
|
|
if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
|
|
|
|
warnmsg(LOG_ERR, __func__, "socket() failed.");
|
|
|
|
return (-1);
|
|
|
|
}
|
|
|
|
memset(&nd, 0, sizeof(nd));
|
|
|
|
strlcpy(nd.ifname, ifname, sizeof(nd.ifname));
|
|
|
|
if (ioctl(s, SIOCGIFINFO_IN6, (caddr_t)&nd) < 0) {
|
|
|
|
warnmsg(LOG_ERR, __func__,
|
|
|
|
"cannot get accept_rtadv flag");
|
|
|
|
close(s);
|
|
|
|
return (-1);
|
|
|
|
}
|
|
|
|
nd.ndi.flags |= ND6_IFF_ACCEPT_RTADV;
|
|
|
|
if (ioctl(s, SIOCSIFINFO_IN6, (caddr_t)&nd) < 0) {
|
|
|
|
warnmsg(LOG_ERR, __func__,
|
|
|
|
"cannot set accept_rtadv flag");
|
|
|
|
close(s);
|
|
|
|
return (-1);
|
|
|
|
}
|
|
|
|
close(s);
|
|
|
|
}
|
|
|
|
|
2011-06-06 03:06:43 +00:00
|
|
|
if ((ifi = malloc(sizeof(*ifi))) == NULL) {
|
2003-08-08 16:38:23 +00:00
|
|
|
warnmsg(LOG_ERR, __func__, "memory allocation failed");
|
2000-07-05 10:14:11 +00:00
|
|
|
free(sdl);
|
2011-06-06 03:06:43 +00:00
|
|
|
return (-1);
|
1999-12-28 02:37:14 +00:00
|
|
|
}
|
2011-06-06 03:06:43 +00:00
|
|
|
memset(ifi, 0, sizeof(*ifi));
|
|
|
|
ifi->sdl = sdl;
|
2011-06-08 16:03:29 +00:00
|
|
|
ifi->ifi_rdnss = IFI_DNSOPT_STATE_NOINFO;
|
|
|
|
ifi->ifi_dnssl = IFI_DNSOPT_STATE_NOINFO;
|
|
|
|
TAILQ_INIT(&ifi->ifi_rainfo);
|
2011-06-06 03:06:43 +00:00
|
|
|
strlcpy(ifi->ifname, ifname, sizeof(ifi->ifname));
|
1999-12-28 02:37:14 +00:00
|
|
|
|
|
|
|
/* construct a router solicitation message */
|
2011-06-06 03:06:43 +00:00
|
|
|
if (make_packet(ifi))
|
1999-12-28 02:37:14 +00:00
|
|
|
goto bad;
|
|
|
|
|
2003-08-17 11:11:32 +00:00
|
|
|
/* set link ID of this interface. */
|
|
|
|
#ifdef HAVE_SCOPELIB
|
2011-06-06 03:06:43 +00:00
|
|
|
if (inet_zoneid(AF_INET6, 2, ifname, &ifi->linkid))
|
2003-08-17 11:11:32 +00:00
|
|
|
goto bad;
|
|
|
|
#else
|
|
|
|
/* XXX: assume interface IDs as link IDs */
|
2011-06-06 03:06:43 +00:00
|
|
|
ifi->linkid = ifi->sdl->sdl_index;
|
2003-08-17 11:11:32 +00:00
|
|
|
#endif
|
|
|
|
|
1999-12-28 02:37:14 +00:00
|
|
|
/*
|
|
|
|
* check if the interface is available.
|
|
|
|
* also check if SIOCGIFMEDIA ioctl is OK on the interface.
|
|
|
|
*/
|
2011-06-06 03:06:43 +00:00
|
|
|
ifi->mediareqok = 1;
|
|
|
|
ifi->active = interface_status(ifi);
|
|
|
|
if (!ifi->mediareqok) {
|
1999-12-28 02:37:14 +00:00
|
|
|
/*
|
|
|
|
* probe routers periodically even if the link status
|
|
|
|
* does not change.
|
|
|
|
*/
|
2011-06-06 03:06:43 +00:00
|
|
|
ifi->probeinterval = PROBE_INTERVAL;
|
1999-12-28 02:37:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* activate interface: interface_up returns 0 on success */
|
2011-06-06 03:06:43 +00:00
|
|
|
flags = interface_up(ifi->ifname);
|
1999-12-28 02:37:14 +00:00
|
|
|
if (flags == 0)
|
2011-06-06 03:06:43 +00:00
|
|
|
ifi->state = IFS_DELAY;
|
1999-12-28 02:37:14 +00:00
|
|
|
else if (flags == IFS_TENTATIVE)
|
2011-06-06 03:06:43 +00:00
|
|
|
ifi->state = IFS_TENTATIVE;
|
1999-12-28 02:37:14 +00:00
|
|
|
else
|
2011-06-06 03:06:43 +00:00
|
|
|
ifi->state = IFS_DOWN;
|
1999-12-28 02:37:14 +00:00
|
|
|
|
2011-06-06 03:06:43 +00:00
|
|
|
rtsol_timer_update(ifi);
|
1999-12-28 02:37:14 +00:00
|
|
|
|
2011-06-06 03:06:43 +00:00
|
|
|
TAILQ_INSERT_TAIL(&ifinfo_head, ifi, ifi_next);
|
|
|
|
return (0);
|
1999-12-28 02:37:14 +00:00
|
|
|
|
2003-08-08 16:56:01 +00:00
|
|
|
bad:
|
2011-06-06 03:06:43 +00:00
|
|
|
free(ifi->sdl);
|
|
|
|
free(ifi);
|
|
|
|
return (-1);
|
1999-12-28 02:37:14 +00:00
|
|
|
}
|
|
|
|
|
2003-08-17 11:11:32 +00:00
|
|
|
void
|
2004-01-14 17:16:19 +00:00
|
|
|
iflist_init(void)
|
2003-08-17 11:11:32 +00:00
|
|
|
{
|
2011-06-06 03:06:43 +00:00
|
|
|
struct ifinfo *ifi;
|
2003-08-17 11:11:32 +00:00
|
|
|
|
2011-06-06 03:06:43 +00:00
|
|
|
while ((ifi = TAILQ_FIRST(&ifinfo_head)) != NULL) {
|
|
|
|
TAILQ_REMOVE(&ifinfo_head, ifi, ifi_next);
|
|
|
|
if (ifi->sdl != NULL)
|
2003-08-17 11:11:32 +00:00
|
|
|
free(ifi->sdl);
|
2011-06-06 03:06:43 +00:00
|
|
|
if (ifi->rs_data != NULL)
|
2003-08-17 11:11:32 +00:00
|
|
|
free(ifi->rs_data);
|
|
|
|
free(ifi);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2000-07-05 10:14:11 +00:00
|
|
|
#if 0
|
|
|
|
static int
|
|
|
|
ifreconfig(char *ifname)
|
|
|
|
{
|
|
|
|
struct ifinfo *ifi, *prev;
|
|
|
|
int rv;
|
|
|
|
|
|
|
|
prev = NULL;
|
2011-06-06 03:06:43 +00:00
|
|
|
TAILQ_FOREACH(ifi, &ifinfo_head, ifi_next) {
|
2000-07-05 10:14:11 +00:00
|
|
|
if (strncmp(ifi->ifname, ifname, sizeof(ifi->ifname)) == 0)
|
|
|
|
break;
|
|
|
|
prev = ifi;
|
|
|
|
}
|
|
|
|
prev->next = ifi->next;
|
|
|
|
|
|
|
|
rv = ifconfig(ifname);
|
|
|
|
|
|
|
|
/* reclaim it after ifconfig() in case ifname is pointer inside ifi */
|
|
|
|
if (ifi->rs_data)
|
|
|
|
free(ifi->rs_data);
|
|
|
|
free(ifi->sdl);
|
|
|
|
free(ifi);
|
2011-06-06 03:06:43 +00:00
|
|
|
|
|
|
|
return (rv);
|
2000-07-05 10:14:11 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2011-06-08 16:03:29 +00:00
|
|
|
struct rainfo *
|
|
|
|
find_rainfo(struct ifinfo *ifi, struct sockaddr_in6 *sin6)
|
|
|
|
{
|
|
|
|
struct rainfo *rai;
|
|
|
|
|
|
|
|
TAILQ_FOREACH(rai, &ifi->ifi_rainfo, rai_next)
|
|
|
|
if (memcmp(&rai->rai_saddr.sin6_addr, &sin6->sin6_addr,
|
|
|
|
sizeof(rai->rai_saddr.sin6_addr)) == 0)
|
|
|
|
return (rai);
|
|
|
|
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
|
1999-12-28 02:37:14 +00:00
|
|
|
struct ifinfo *
|
|
|
|
find_ifinfo(int ifindex)
|
|
|
|
{
|
|
|
|
struct ifinfo *ifi;
|
|
|
|
|
2011-06-06 03:06:43 +00:00
|
|
|
TAILQ_FOREACH(ifi, &ifinfo_head, ifi_next) {
|
1999-12-28 02:37:14 +00:00
|
|
|
if (ifi->sdl->sdl_index == ifindex)
|
2011-06-06 03:06:43 +00:00
|
|
|
return (ifi);
|
|
|
|
}
|
|
|
|
return (NULL);
|
1999-12-28 02:37:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2011-06-06 03:06:43 +00:00
|
|
|
make_packet(struct ifinfo *ifi)
|
1999-12-28 02:37:14 +00:00
|
|
|
{
|
|
|
|
size_t packlen = sizeof(struct nd_router_solicit), lladdroptlen = 0;
|
2003-08-08 16:56:01 +00:00
|
|
|
struct nd_router_solicit *rs;
|
|
|
|
char *buf;
|
1999-12-28 02:37:14 +00:00
|
|
|
|
2011-06-06 03:06:43 +00:00
|
|
|
if ((lladdroptlen = lladdropt_length(ifi->sdl)) == 0) {
|
2003-08-08 16:38:23 +00:00
|
|
|
warnmsg(LOG_INFO, __func__,
|
2003-08-08 16:56:01 +00:00
|
|
|
"link-layer address option has null length"
|
2011-06-06 03:06:43 +00:00
|
|
|
" on %s. Treat as not included.", ifi->ifname);
|
1999-12-28 02:37:14 +00:00
|
|
|
}
|
|
|
|
packlen += lladdroptlen;
|
2011-06-06 03:06:43 +00:00
|
|
|
ifi->rs_datalen = packlen;
|
1999-12-28 02:37:14 +00:00
|
|
|
|
|
|
|
/* allocate buffer */
|
|
|
|
if ((buf = malloc(packlen)) == NULL) {
|
2003-08-08 16:38:23 +00:00
|
|
|
warnmsg(LOG_ERR, __func__,
|
2011-06-06 03:06:43 +00:00
|
|
|
"memory allocation failed for %s", ifi->ifname);
|
|
|
|
return (-1);
|
1999-12-28 02:37:14 +00:00
|
|
|
}
|
2011-06-06 03:06:43 +00:00
|
|
|
ifi->rs_data = buf;
|
1999-12-28 02:37:14 +00:00
|
|
|
|
|
|
|
/* fill in the message */
|
|
|
|
rs = (struct nd_router_solicit *)buf;
|
|
|
|
rs->nd_rs_type = ND_ROUTER_SOLICIT;
|
|
|
|
rs->nd_rs_code = 0;
|
|
|
|
rs->nd_rs_cksum = 0;
|
|
|
|
rs->nd_rs_reserved = 0;
|
|
|
|
buf += sizeof(*rs);
|
|
|
|
|
|
|
|
/* fill in source link-layer address option */
|
|
|
|
if (lladdroptlen)
|
2011-06-06 03:06:43 +00:00
|
|
|
lladdropt_fill(ifi->sdl, (struct nd_opt_hdr *)buf);
|
1999-12-28 02:37:14 +00:00
|
|
|
|
2011-06-06 03:06:43 +00:00
|
|
|
return (0);
|
1999-12-28 02:37:14 +00:00
|
|
|
}
|
|
|
|
|
2013-08-05 20:13:02 +00:00
|
|
|
static struct timespec *
|
2004-01-14 17:16:19 +00:00
|
|
|
rtsol_check_timer(void)
|
1999-12-28 02:37:14 +00:00
|
|
|
{
|
2013-08-05 20:13:02 +00:00
|
|
|
static struct timespec returnval;
|
|
|
|
struct timespec now, rtsol_timer;
|
2011-06-06 03:06:43 +00:00
|
|
|
struct ifinfo *ifi;
|
2011-06-08 16:03:29 +00:00
|
|
|
struct rainfo *rai;
|
2016-02-18 01:58:26 +00:00
|
|
|
struct ra_opt *rao, *raotmp;
|
1999-12-28 02:37:14 +00:00
|
|
|
int flags;
|
|
|
|
|
2013-08-05 20:13:02 +00:00
|
|
|
clock_gettime(CLOCK_MONOTONIC_FAST, &now);
|
1999-12-28 02:37:14 +00:00
|
|
|
|
|
|
|
rtsol_timer = tm_max;
|
|
|
|
|
2011-06-06 03:06:43 +00:00
|
|
|
TAILQ_FOREACH(ifi, &ifinfo_head, ifi_next) {
|
2013-08-05 20:13:02 +00:00
|
|
|
if (TS_CMP(&ifi->expire, &now, <=)) {
|
2011-06-08 16:03:29 +00:00
|
|
|
warnmsg(LOG_DEBUG, __func__, "timer expiration on %s, "
|
|
|
|
"state = %d", ifi->ifname, ifi->state);
|
|
|
|
|
|
|
|
while((rai = TAILQ_FIRST(&ifi->ifi_rainfo)) != NULL) {
|
|
|
|
/* Remove all RA options. */
|
|
|
|
TAILQ_REMOVE(&ifi->ifi_rainfo, rai, rai_next);
|
|
|
|
while ((rao = TAILQ_FIRST(&rai->rai_ra_opt)) !=
|
|
|
|
NULL) {
|
|
|
|
TAILQ_REMOVE(&rai->rai_ra_opt, rao,
|
|
|
|
rao_next);
|
|
|
|
if (rao->rao_msg != NULL)
|
|
|
|
free(rao->rao_msg);
|
|
|
|
free(rao);
|
|
|
|
}
|
|
|
|
free(rai);
|
2011-06-06 03:06:43 +00:00
|
|
|
}
|
|
|
|
switch (ifi->state) {
|
1999-12-28 02:37:14 +00:00
|
|
|
case IFS_DOWN:
|
|
|
|
case IFS_TENTATIVE:
|
|
|
|
/* interface_up returns 0 on success */
|
2011-06-06 03:06:43 +00:00
|
|
|
flags = interface_up(ifi->ifname);
|
1999-12-28 02:37:14 +00:00
|
|
|
if (flags == 0)
|
2011-06-06 03:06:43 +00:00
|
|
|
ifi->state = IFS_DELAY;
|
1999-12-28 02:37:14 +00:00
|
|
|
else if (flags == IFS_TENTATIVE)
|
2011-06-06 03:06:43 +00:00
|
|
|
ifi->state = IFS_TENTATIVE;
|
1999-12-28 02:37:14 +00:00
|
|
|
else
|
2011-06-06 03:06:43 +00:00
|
|
|
ifi->state = IFS_DOWN;
|
1999-12-28 02:37:14 +00:00
|
|
|
break;
|
|
|
|
case IFS_IDLE:
|
|
|
|
{
|
2011-06-06 03:06:43 +00:00
|
|
|
int oldstatus = ifi->active;
|
1999-12-28 02:37:14 +00:00
|
|
|
int probe = 0;
|
|
|
|
|
2011-06-06 03:06:43 +00:00
|
|
|
ifi->active = interface_status(ifi);
|
1999-12-28 02:37:14 +00:00
|
|
|
|
2011-06-06 03:06:43 +00:00
|
|
|
if (oldstatus != ifi->active) {
|
2003-08-08 16:38:23 +00:00
|
|
|
warnmsg(LOG_DEBUG, __func__,
|
2003-08-08 16:56:01 +00:00
|
|
|
"%s status is changed"
|
|
|
|
" from %d to %d",
|
2011-06-06 03:06:43 +00:00
|
|
|
ifi->ifname,
|
|
|
|
oldstatus, ifi->active);
|
1999-12-28 02:37:14 +00:00
|
|
|
probe = 1;
|
2011-06-06 03:06:43 +00:00
|
|
|
ifi->state = IFS_DELAY;
|
|
|
|
} else if (ifi->probeinterval &&
|
|
|
|
(ifi->probetimer -=
|
|
|
|
ifi->timer.tv_sec) <= 0) {
|
1999-12-28 02:37:14 +00:00
|
|
|
/* probe timer expired */
|
2011-06-06 03:06:43 +00:00
|
|
|
ifi->probetimer =
|
|
|
|
ifi->probeinterval;
|
1999-12-28 02:37:14 +00:00
|
|
|
probe = 1;
|
2011-06-06 03:06:43 +00:00
|
|
|
ifi->state = IFS_PROBE;
|
1999-12-28 02:37:14 +00:00
|
|
|
}
|
|
|
|
|
2003-08-08 16:42:37 +00:00
|
|
|
/*
|
|
|
|
* If we need a probe, clear the previous
|
|
|
|
* status wrt the "other" configuration.
|
|
|
|
*/
|
|
|
|
if (probe)
|
2011-06-06 03:06:43 +00:00
|
|
|
ifi->otherconfig = 0;
|
2003-08-08 16:42:37 +00:00
|
|
|
|
1999-12-28 02:37:14 +00:00
|
|
|
if (probe && mobile_node)
|
2011-06-06 03:06:43 +00:00
|
|
|
defrouter_probe(ifi);
|
1999-12-28 02:37:14 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case IFS_DELAY:
|
2011-06-06 03:06:43 +00:00
|
|
|
ifi->state = IFS_PROBE;
|
|
|
|
sendpacket(ifi);
|
1999-12-28 02:37:14 +00:00
|
|
|
break;
|
|
|
|
case IFS_PROBE:
|
2011-06-06 03:06:43 +00:00
|
|
|
if (ifi->probes < MAX_RTR_SOLICITATIONS)
|
|
|
|
sendpacket(ifi);
|
1999-12-28 02:37:14 +00:00
|
|
|
else {
|
2003-08-08 16:38:23 +00:00
|
|
|
warnmsg(LOG_INFO, __func__,
|
2003-08-08 16:56:01 +00:00
|
|
|
"No answer after sending %d RSs",
|
2011-06-06 03:06:43 +00:00
|
|
|
ifi->probes);
|
|
|
|
ifi->probes = 0;
|
|
|
|
ifi->state = IFS_IDLE;
|
1999-12-28 02:37:14 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2011-06-06 03:06:43 +00:00
|
|
|
rtsol_timer_update(ifi);
|
|
|
|
} else {
|
|
|
|
/* Expiration check for RA options. */
|
|
|
|
int expire = 0;
|
1999-12-28 02:37:14 +00:00
|
|
|
|
2011-06-08 16:03:29 +00:00
|
|
|
TAILQ_FOREACH(rai, &ifi->ifi_rainfo, rai_next) {
|
2016-02-18 01:58:26 +00:00
|
|
|
TAILQ_FOREACH_SAFE(rao, &rai->rai_ra_opt,
|
|
|
|
rao_next, raotmp) {
|
2011-06-06 03:06:43 +00:00
|
|
|
warnmsg(LOG_DEBUG, __func__,
|
2011-06-08 16:03:29 +00:00
|
|
|
"RA expiration timer: "
|
|
|
|
"type=%d, msg=%s, expire=%s",
|
|
|
|
rao->rao_type, (char *)rao->rao_msg,
|
|
|
|
sec2str(&rao->rao_expire));
|
2013-08-05 20:13:02 +00:00
|
|
|
if (TS_CMP(&now, &rao->rao_expire,
|
2011-06-08 16:03:29 +00:00
|
|
|
>=)) {
|
|
|
|
warnmsg(LOG_DEBUG, __func__,
|
|
|
|
"RA expiration timer: "
|
|
|
|
"expired.");
|
|
|
|
TAILQ_REMOVE(&rai->rai_ra_opt,
|
|
|
|
rao, rao_next);
|
|
|
|
if (rao->rao_msg != NULL)
|
|
|
|
free(rao->rao_msg);
|
|
|
|
free(rao);
|
|
|
|
expire = 1;
|
|
|
|
}
|
2011-06-06 03:06:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (expire)
|
|
|
|
ra_opt_handler(ifi);
|
|
|
|
}
|
2013-08-05 20:13:02 +00:00
|
|
|
if (TS_CMP(&ifi->expire, &rtsol_timer, <))
|
2011-06-06 03:06:43 +00:00
|
|
|
rtsol_timer = ifi->expire;
|
1999-12-28 02:37:14 +00:00
|
|
|
}
|
|
|
|
|
2013-08-05 20:13:02 +00:00
|
|
|
if (TS_CMP(&rtsol_timer, &tm_max, ==)) {
|
2003-08-08 16:38:23 +00:00
|
|
|
warnmsg(LOG_DEBUG, __func__, "there is no timer");
|
2011-06-06 03:06:43 +00:00
|
|
|
return (NULL);
|
2013-08-05 20:13:02 +00:00
|
|
|
} else if (TS_CMP(&rtsol_timer, &now, <))
|
1999-12-28 02:37:14 +00:00
|
|
|
/* this may occur when the interval is too small */
|
2013-08-05 20:13:02 +00:00
|
|
|
returnval.tv_sec = returnval.tv_nsec = 0;
|
1999-12-28 02:37:14 +00:00
|
|
|
else
|
2013-08-05 20:13:02 +00:00
|
|
|
TS_SUB(&rtsol_timer, &now, &returnval);
|
1999-12-28 02:37:14 +00:00
|
|
|
|
2011-06-08 16:03:29 +00:00
|
|
|
now.tv_sec += returnval.tv_sec;
|
2013-08-05 20:13:02 +00:00
|
|
|
now.tv_nsec += returnval.tv_nsec;
|
2011-06-08 16:03:29 +00:00
|
|
|
warnmsg(LOG_DEBUG, __func__, "New timer is %s",
|
|
|
|
sec2str(&now));
|
1999-12-28 02:37:14 +00:00
|
|
|
|
2011-06-06 03:06:43 +00:00
|
|
|
return (&returnval);
|
1999-12-28 02:37:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2011-06-06 03:06:43 +00:00
|
|
|
rtsol_timer_update(struct ifinfo *ifi)
|
1999-12-28 02:37:14 +00:00
|
|
|
{
|
2000-07-05 10:14:11 +00:00
|
|
|
#define MILLION 1000000
|
|
|
|
#define DADRETRY 10 /* XXX: adhoc */
|
1999-12-28 02:37:14 +00:00
|
|
|
long interval;
|
2013-08-05 20:13:02 +00:00
|
|
|
struct timespec now;
|
1999-12-28 02:37:14 +00:00
|
|
|
|
2011-06-06 03:06:43 +00:00
|
|
|
bzero(&ifi->timer, sizeof(ifi->timer));
|
1999-12-28 02:37:14 +00:00
|
|
|
|
2011-06-06 03:06:43 +00:00
|
|
|
switch (ifi->state) {
|
1999-12-28 02:37:14 +00:00
|
|
|
case IFS_DOWN:
|
|
|
|
case IFS_TENTATIVE:
|
2011-06-06 03:06:43 +00:00
|
|
|
if (++ifi->dadcount > DADRETRY) {
|
|
|
|
ifi->dadcount = 0;
|
|
|
|
ifi->timer.tv_sec = PROBE_INTERVAL;
|
2003-08-08 16:56:01 +00:00
|
|
|
} else
|
2011-06-06 03:06:43 +00:00
|
|
|
ifi->timer.tv_sec = 1;
|
1999-12-28 02:37:14 +00:00
|
|
|
break;
|
|
|
|
case IFS_IDLE:
|
|
|
|
if (mobile_node) {
|
2003-08-08 16:56:01 +00:00
|
|
|
/* XXX should be configurable */
|
2011-06-06 03:06:43 +00:00
|
|
|
ifi->timer.tv_sec = 3;
|
1999-12-28 02:37:14 +00:00
|
|
|
}
|
|
|
|
else
|
2011-06-06 03:06:43 +00:00
|
|
|
ifi->timer = tm_max; /* stop timer(valid?) */
|
1999-12-28 02:37:14 +00:00
|
|
|
break;
|
|
|
|
case IFS_DELAY:
|
2008-07-26 15:46:39 +00:00
|
|
|
interval = arc4random_uniform(MAX_RTR_SOLICITATION_DELAY * MILLION);
|
2011-06-06 03:06:43 +00:00
|
|
|
ifi->timer.tv_sec = interval / MILLION;
|
2013-08-05 20:13:02 +00:00
|
|
|
ifi->timer.tv_nsec = (interval % MILLION) * 1000;
|
1999-12-28 02:37:14 +00:00
|
|
|
break;
|
|
|
|
case IFS_PROBE:
|
2011-06-06 03:06:43 +00:00
|
|
|
if (ifi->probes < MAX_RTR_SOLICITATIONS)
|
|
|
|
ifi->timer.tv_sec = RTR_SOLICITATION_INTERVAL;
|
2001-06-11 12:39:29 +00:00
|
|
|
else {
|
|
|
|
/*
|
|
|
|
* After sending MAX_RTR_SOLICITATIONS solicitations,
|
|
|
|
* we're just waiting for possible replies; there
|
2005-06-08 23:51:42 +00:00
|
|
|
* will be no more solicitation. Thus, we change
|
2001-06-11 12:39:29 +00:00
|
|
|
* the timer value to MAX_RTR_SOLICITATION_DELAY based
|
|
|
|
* on RFC 2461, Section 6.3.7.
|
|
|
|
*/
|
2011-06-06 03:06:43 +00:00
|
|
|
ifi->timer.tv_sec = MAX_RTR_SOLICITATION_DELAY;
|
2001-06-11 12:39:29 +00:00
|
|
|
}
|
1999-12-28 02:37:14 +00:00
|
|
|
break;
|
|
|
|
default:
|
2003-08-08 16:38:23 +00:00
|
|
|
warnmsg(LOG_ERR, __func__,
|
2003-08-08 16:56:01 +00:00
|
|
|
"illegal interface state(%d) on %s",
|
2011-06-06 03:06:43 +00:00
|
|
|
ifi->state, ifi->ifname);
|
1999-12-28 02:37:14 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* reset the timer */
|
2013-08-05 20:13:02 +00:00
|
|
|
if (TS_CMP(&ifi->timer, &tm_max, ==)) {
|
2011-06-06 03:06:43 +00:00
|
|
|
ifi->expire = tm_max;
|
2003-08-08 16:38:23 +00:00
|
|
|
warnmsg(LOG_DEBUG, __func__,
|
2011-06-06 03:06:43 +00:00
|
|
|
"stop timer for %s", ifi->ifname);
|
2003-08-08 16:56:01 +00:00
|
|
|
} else {
|
2013-08-05 20:13:02 +00:00
|
|
|
clock_gettime(CLOCK_MONOTONIC_FAST, &now);
|
|
|
|
TS_ADD(&now, &ifi->timer, &ifi->expire);
|
1999-12-28 02:37:14 +00:00
|
|
|
|
2011-06-08 16:03:29 +00:00
|
|
|
now.tv_sec += ifi->timer.tv_sec;
|
2013-08-05 20:13:02 +00:00
|
|
|
now.tv_nsec += ifi->timer.tv_nsec;
|
2011-06-08 16:03:29 +00:00
|
|
|
warnmsg(LOG_DEBUG, __func__, "set timer for %s to %s",
|
|
|
|
ifi->ifname, sec2str(&now));
|
1999-12-28 02:37:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#undef MILLION
|
|
|
|
}
|
|
|
|
|
|
|
|
/* timer related utility functions */
|
2000-07-05 10:14:11 +00:00
|
|
|
#define MILLION 1000000
|
1999-12-28 02:37:14 +00:00
|
|
|
|
2004-01-14 17:59:38 +00:00
|
|
|
#ifndef SMALL
|
1999-12-28 02:37:14 +00:00
|
|
|
static void
|
2010-02-27 10:19:39 +00:00
|
|
|
rtsold_set_dump_file(int sig __unused)
|
1999-12-28 02:37:14 +00:00
|
|
|
{
|
|
|
|
do_dump = 1;
|
|
|
|
}
|
2004-01-14 17:59:38 +00:00
|
|
|
#endif
|
1999-12-28 02:37:14 +00:00
|
|
|
|
|
|
|
static void
|
2011-06-06 03:06:43 +00:00
|
|
|
usage(void)
|
1999-12-28 02:37:14 +00:00
|
|
|
{
|
2011-06-06 03:06:43 +00:00
|
|
|
#ifndef SMALL
|
|
|
|
fprintf(stderr, "usage: rtsold [-dDfFm1] [-O script-name] "
|
2015-10-30 07:36:43 +00:00
|
|
|
"[-p pidfile] [-R script-name] interface ...\n");
|
|
|
|
fprintf(stderr, "usage: rtsold [-dDfFm1] [-O script-name] "
|
|
|
|
"[-p pidfile] [-R script-name] -a\n");
|
2011-06-06 03:06:43 +00:00
|
|
|
#else
|
|
|
|
fprintf(stderr, "usage: rtsol [-dDF] [-O script-name] "
|
2015-10-30 07:36:43 +00:00
|
|
|
"[-p pidfile] [-R script-name] interface ...\n");
|
2011-06-06 03:06:43 +00:00
|
|
|
fprintf(stderr, "usage: rtsol [-dDF] [-O script-name] "
|
2015-10-30 07:36:43 +00:00
|
|
|
"[-p pidfile] [-R script-name] -a\n");
|
2011-06-06 03:06:43 +00:00
|
|
|
#endif
|
1999-12-28 02:37:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
warnmsg(int priority, const char *func, const char *msg, ...)
|
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
char buf[BUFSIZ];
|
|
|
|
|
|
|
|
va_start(ap, msg);
|
|
|
|
if (fflag) {
|
|
|
|
if (priority <= log_upto) {
|
|
|
|
(void)vfprintf(stderr, msg, ap);
|
|
|
|
(void)fprintf(stderr, "\n");
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
snprintf(buf, sizeof(buf), "<%s> %s", func, msg);
|
2000-10-06 23:46:52 +00:00
|
|
|
msg = buf;
|
|
|
|
vsyslog(priority, msg, ap);
|
1999-12-28 02:37:14 +00:00
|
|
|
}
|
|
|
|
va_end(ap);
|
|
|
|
}
|
2000-10-06 23:46:52 +00:00
|
|
|
|
2003-08-17 11:11:32 +00:00
|
|
|
/*
|
|
|
|
* return a list of interfaces which is suitable to sending an RS.
|
|
|
|
*/
|
|
|
|
char **
|
2004-01-14 17:16:19 +00:00
|
|
|
autoifprobe(void)
|
2000-10-06 23:46:52 +00:00
|
|
|
{
|
2003-08-17 11:11:32 +00:00
|
|
|
static char **argv = NULL;
|
|
|
|
static int n = 0;
|
|
|
|
char **a;
|
2010-02-27 10:19:39 +00:00
|
|
|
int s = 0, i, found;
|
2012-01-20 01:38:52 +00:00
|
|
|
struct ifaddrs *ifap, *ifa;
|
2010-02-02 18:38:17 +00:00
|
|
|
struct in6_ndireq nd;
|
2000-10-06 23:46:52 +00:00
|
|
|
|
2003-08-17 11:11:32 +00:00
|
|
|
/* initialize */
|
|
|
|
while (n--)
|
|
|
|
free(argv[n]);
|
|
|
|
if (argv) {
|
|
|
|
free(argv);
|
|
|
|
argv = NULL;
|
|
|
|
}
|
|
|
|
n = 0;
|
|
|
|
|
2000-10-06 23:46:52 +00:00
|
|
|
if (getifaddrs(&ifap) != 0)
|
2011-06-06 03:06:43 +00:00
|
|
|
return (NULL);
|
2000-10-06 23:46:52 +00:00
|
|
|
|
2010-02-02 18:38:17 +00:00
|
|
|
if (!Fflag && (s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
|
2011-06-06 03:06:43 +00:00
|
|
|
warnmsg(LOG_ERR, __func__, "socket");
|
|
|
|
exit(1);
|
2010-02-02 18:38:17 +00:00
|
|
|
}
|
|
|
|
|
2000-10-06 23:46:52 +00:00
|
|
|
/* find an ethernet */
|
|
|
|
for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
|
|
|
|
if ((ifa->ifa_flags & IFF_UP) == 0)
|
|
|
|
continue;
|
|
|
|
if ((ifa->ifa_flags & IFF_POINTOPOINT) != 0)
|
|
|
|
continue;
|
|
|
|
if ((ifa->ifa_flags & IFF_LOOPBACK) != 0)
|
|
|
|
continue;
|
|
|
|
if ((ifa->ifa_flags & IFF_MULTICAST) == 0)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (ifa->ifa_addr->sa_family != AF_INET6)
|
|
|
|
continue;
|
|
|
|
|
2003-08-17 11:11:32 +00:00
|
|
|
found = 0;
|
|
|
|
for (i = 0; i < n; i++) {
|
|
|
|
if (strcmp(argv[i], ifa->ifa_name) == 0) {
|
|
|
|
found++;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (found)
|
2000-10-06 23:46:52 +00:00
|
|
|
continue;
|
|
|
|
|
2010-02-02 18:38:17 +00:00
|
|
|
/*
|
|
|
|
* Skip the interfaces which IPv6 and/or accepting RA
|
|
|
|
* is disabled.
|
|
|
|
*/
|
|
|
|
if (!Fflag) {
|
|
|
|
memset(&nd, 0, sizeof(nd));
|
|
|
|
strlcpy(nd.ifname, ifa->ifa_name, sizeof(nd.ifname));
|
|
|
|
if (ioctl(s, SIOCGIFINFO_IN6, (caddr_t)&nd) < 0) {
|
2011-06-06 03:06:43 +00:00
|
|
|
warnmsg(LOG_ERR, __func__,
|
|
|
|
"ioctl(SIOCGIFINFO_IN6)");
|
|
|
|
exit(1);
|
2010-02-02 18:38:17 +00:00
|
|
|
}
|
|
|
|
if ((nd.ndi.flags & ND6_IFF_IFDISABLED))
|
|
|
|
continue;
|
|
|
|
if (!(nd.ndi.flags & ND6_IFF_ACCEPT_RTADV))
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2003-08-17 11:11:32 +00:00
|
|
|
/* if we find multiple candidates, just warn. */
|
|
|
|
if (n != 0 && dflag > 1)
|
2011-06-06 03:06:43 +00:00
|
|
|
warnmsg(LOG_WARNING, __func__,
|
|
|
|
"multiple interfaces found");
|
2003-08-17 11:11:32 +00:00
|
|
|
|
2015-12-29 11:24:41 +00:00
|
|
|
a = realloc(argv, (n + 1) * sizeof(char *));
|
2011-06-06 03:06:43 +00:00
|
|
|
if (a == NULL) {
|
|
|
|
warnmsg(LOG_ERR, __func__, "realloc");
|
|
|
|
exit(1);
|
|
|
|
}
|
2003-08-17 11:11:32 +00:00
|
|
|
argv = a;
|
|
|
|
argv[n] = strdup(ifa->ifa_name);
|
2011-06-06 03:06:43 +00:00
|
|
|
if (!argv[n]) {
|
|
|
|
warnmsg(LOG_ERR, __func__, "malloc");
|
|
|
|
exit(1);
|
|
|
|
}
|
2003-08-17 11:11:32 +00:00
|
|
|
n++;
|
2000-10-06 23:46:52 +00:00
|
|
|
}
|
|
|
|
|
2003-08-17 11:11:32 +00:00
|
|
|
if (n) {
|
2015-12-29 11:24:41 +00:00
|
|
|
a = realloc(argv, (n + 1) * sizeof(char *));
|
2011-06-06 03:06:43 +00:00
|
|
|
if (a == NULL) {
|
|
|
|
warnmsg(LOG_ERR, __func__, "realloc");
|
|
|
|
exit(1);
|
|
|
|
}
|
2003-08-17 11:11:32 +00:00
|
|
|
argv = a;
|
|
|
|
argv[n] = NULL;
|
2000-10-06 23:46:52 +00:00
|
|
|
|
2003-08-17 11:11:32 +00:00
|
|
|
if (dflag > 0) {
|
|
|
|
for (i = 0; i < n; i++)
|
2011-06-06 03:06:43 +00:00
|
|
|
warnmsg(LOG_WARNING, __func__, "probing %s",
|
|
|
|
argv[i]);
|
2003-08-17 11:11:32 +00:00
|
|
|
}
|
2000-10-06 23:46:52 +00:00
|
|
|
}
|
2010-02-02 18:38:17 +00:00
|
|
|
if (!Fflag)
|
|
|
|
close(s);
|
2000-10-06 23:46:52 +00:00
|
|
|
freeifaddrs(ifap);
|
2011-06-06 03:06:43 +00:00
|
|
|
return (argv);
|
2000-10-06 23:46:52 +00:00
|
|
|
}
|