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
|
|
|
|
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>
|
|
|
|
#include <sys/time.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>
|
|
|
|
|
|
|
|
#include <signal.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <syslog.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#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
|
|
|
#ifdef HAVE_POLL_H
|
|
|
|
#include <poll.h>
|
|
|
|
#endif
|
2003-08-08 16:56:01 +00:00
|
|
|
|
1999-12-28 02:37:14 +00:00
|
|
|
#include "rtsold.h"
|
|
|
|
|
|
|
|
struct ifinfo *iflist;
|
|
|
|
struct timeval tm_max = {0x7fffffff, 0x7fffffff};
|
2003-08-08 16:56:01 +00:00
|
|
|
static int log_upto = 999;
|
|
|
|
static int fflag = 0;
|
2004-01-14 17:42:03 +00:00
|
|
|
static int Fflag = 0; /* force setting sysctl parameters */
|
2003-08-08 16:56:01 +00:00
|
|
|
|
2000-10-06 23:46:52 +00:00
|
|
|
int aflag = 0;
|
|
|
|
int dflag = 0;
|
2003-08-08 16:56:01 +00:00
|
|
|
|
2003-08-08 16:42:37 +00:00
|
|
|
char *otherconf_script;
|
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
|
|
|
|
2007-11-07 10:53:41 +00:00
|
|
|
int main(int, char **);
|
1999-12-28 02:37:14 +00:00
|
|
|
|
|
|
|
/* static variables and functions */
|
|
|
|
static int mobile_node = 0;
|
2004-01-14 17:59:38 +00:00
|
|
|
#ifndef SMALL
|
1999-12-28 02:37:14 +00:00
|
|
|
static int do_dump;
|
2000-07-05 10:14:11 +00:00
|
|
|
static char *dumpfilename = "/var/run/rtsold.dump"; /* XXX: should be configurable */
|
2004-01-14 17:59:38 +00:00
|
|
|
#endif
|
|
|
|
#if 1
|
1999-12-28 02:37:14 +00:00
|
|
|
static char *pidfilename = "/var/run/rtsold.pid"; /* should be configurable */
|
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
|
2007-11-07 10:53:41 +00:00
|
|
|
static int make_packet(struct ifinfo *);
|
|
|
|
static struct timeval *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
|
2007-11-07 10:53:41 +00:00
|
|
|
static void usage(char *);
|
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;
|
1999-12-28 02:37:14 +00:00
|
|
|
struct timeval *timeout;
|
2003-08-08 16:56:01 +00:00
|
|
|
char *argv0, *opts;
|
2003-08-14 18:43:57 +00:00
|
|
|
#ifdef HAVE_POLL_H
|
|
|
|
struct pollfd set[2];
|
|
|
|
#else
|
2003-08-14 15:47:31 +00:00
|
|
|
fd_set *fdsetp, *selectfdp;
|
|
|
|
int fdmasks;
|
|
|
|
int maxfd;
|
2003-08-14 18:43:57 +00:00
|
|
|
#endif
|
2003-08-14 15:47:31 +00:00
|
|
|
int rtsock;
|
1999-12-28 02:37:14 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Initialization
|
|
|
|
*/
|
|
|
|
argv0 = argv[0];
|
|
|
|
|
|
|
|
/* get option */
|
|
|
|
if (argv0 && argv0[strlen(argv0) - 1] != 'd') {
|
|
|
|
fflag = 1;
|
|
|
|
once = 1;
|
2004-01-14 17:42:03 +00:00
|
|
|
opts = "adDFO:";
|
1999-12-28 02:37:14 +00:00
|
|
|
} else
|
2004-01-14 17:42:03 +00:00
|
|
|
opts = "adDfFm1O:";
|
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':
|
|
|
|
dflag = 1;
|
|
|
|
break;
|
|
|
|
case 'D':
|
|
|
|
dflag = 2;
|
|
|
|
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;
|
2000-10-06 23:46:52 +00:00
|
|
|
default:
|
|
|
|
usage(argv0);
|
|
|
|
/*NOTREACHED*/
|
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)) {
|
1999-12-28 02:37:14 +00:00
|
|
|
usage(argv0);
|
2000-10-06 23:46:52 +00:00
|
|
|
/*NOTREACHED*/
|
|
|
|
}
|
1999-12-28 02:37:14 +00:00
|
|
|
|
|
|
|
/* set log level */
|
|
|
|
if (dflag == 0)
|
|
|
|
log_upto = LOG_NOTICE;
|
|
|
|
if (!fflag) {
|
|
|
|
char *ident;
|
2003-08-08 16:56:01 +00:00
|
|
|
|
1999-12-28 02:37:14 +00:00
|
|
|
ident = strrchr(argv0, '/');
|
|
|
|
if (!ident)
|
|
|
|
ident = argv0;
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2000-07-05 10:14:11 +00:00
|
|
|
#ifndef HAVE_ARC4RANDOM
|
2003-08-08 16:56:01 +00:00
|
|
|
/* random value initialization */
|
1999-12-28 02:37:14 +00:00
|
|
|
srandom((u_long)time(NULL));
|
2000-07-05 10:14:11 +00:00
|
|
|
#endif
|
1999-12-28 02:37:14 +00:00
|
|
|
|
2004-01-14 17:42:03 +00:00
|
|
|
if (Fflag) {
|
|
|
|
setinet6sysctl(IPV6CTL_ACCEPT_RTADV, 1);
|
|
|
|
setinet6sysctl(IPV6CTL_FORWARDING, 0);
|
|
|
|
} else {
|
|
|
|
/* warn if accept_rtadv is down */
|
|
|
|
if (!getinet6sysctl(IPV6CTL_ACCEPT_RTADV))
|
|
|
|
warnx("kernel is configured not to accept RAs");
|
|
|
|
/* warn if forwarding is up */
|
|
|
|
if (getinet6sysctl(IPV6CTL_FORWARDING))
|
|
|
|
warnx("kernel is configured as a router, not a host");
|
|
|
|
}
|
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
|
|
|
/*NOTREACHED*/
|
|
|
|
}
|
2003-08-14 18:43:57 +00:00
|
|
|
#ifdef HAVE_POLL_H
|
|
|
|
set[0].fd = s;
|
|
|
|
set[0].events = POLLIN;
|
|
|
|
#else
|
2001-06-11 12:39:29 +00:00
|
|
|
maxfd = s;
|
2003-08-14 18:43:57 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef HAVE_POLL_H
|
|
|
|
set[1].fd = -1;
|
|
|
|
#endif
|
|
|
|
|
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
|
|
|
/*NOTREACHED*/
|
|
|
|
}
|
2003-08-14 18:43:57 +00:00
|
|
|
#ifdef HAVE_POLL_H
|
|
|
|
set[1].fd = rtsock;
|
|
|
|
set[1].events = POLLIN;
|
|
|
|
#else
|
2001-06-11 12:39:29 +00:00
|
|
|
if (rtsock > maxfd)
|
|
|
|
maxfd = rtsock;
|
2003-08-14 18:43:57 +00:00
|
|
|
#endif
|
2000-07-05 10:14:11 +00:00
|
|
|
|
2003-08-14 18:43:57 +00:00
|
|
|
#ifndef HAVE_POLL_H
|
2003-08-14 15:47:31 +00:00
|
|
|
fdmasks = howmany(maxfd + 1, NFDBITS) * sizeof(fd_mask);
|
|
|
|
if ((fdsetp = malloc(fdmasks)) == NULL) {
|
|
|
|
err(1, "malloc");
|
|
|
|
/*NOTREACHED*/
|
|
|
|
}
|
|
|
|
if ((selectfdp = malloc(fdmasks)) == NULL) {
|
|
|
|
err(1, "malloc");
|
|
|
|
/*NOTREACHED*/
|
|
|
|
}
|
2003-08-14 18:43:57 +00:00
|
|
|
#endif
|
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
|
|
|
/*NOTREACHED*/
|
|
|
|
}
|
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
|
|
|
/*NOTREACHED*/
|
|
|
|
}
|
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
|
|
|
|
2004-01-14 17:59:38 +00:00
|
|
|
#if 1
|
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);
|
|
|
|
}
|
|
|
|
}
|
2004-01-14 17:59:38 +00:00
|
|
|
#endif
|
1999-12-28 02:37:14 +00:00
|
|
|
|
2003-08-14 18:43:57 +00:00
|
|
|
#ifndef HAVE_POLL_H
|
2003-08-14 15:47:31 +00:00
|
|
|
memset(fdsetp, 0, fdmasks);
|
|
|
|
FD_SET(s, fdsetp);
|
|
|
|
FD_SET(rtsock, fdsetp);
|
2003-08-14 18:43:57 +00:00
|
|
|
#endif
|
1999-12-28 02:37:14 +00:00
|
|
|
while (1) { /* main loop */
|
|
|
|
int e;
|
2003-08-14 15:47:31 +00:00
|
|
|
|
2003-08-14 18:43:57 +00:00
|
|
|
#ifndef HAVE_POLL_H
|
2003-08-14 15:47:31 +00:00
|
|
|
memcpy(selectfdp, fdsetp, fdmasks);
|
2003-08-14 18:43:57 +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
|
|
|
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 */
|
|
|
|
for (ifi = iflist; ifi; ifi = ifi->next) {
|
|
|
|
if (ifi->state != IFS_DOWN && ifi->racnt == 0)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (ifi == NULL)
|
|
|
|
break;
|
|
|
|
}
|
2003-08-14 18:43:57 +00:00
|
|
|
#ifdef HAVE_POLL_H
|
|
|
|
e = poll(set, 2, timeout ? (timeout->tv_sec * 1000 + timeout->tv_usec / 1000) : INFTIM);
|
|
|
|
#else
|
2003-08-14 15:47:31 +00:00
|
|
|
e = select(maxfd + 1, selectfdp, NULL, NULL, timeout);
|
2003-08-14 18:43:57 +00:00
|
|
|
#endif
|
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
|
|
|
#ifdef HAVE_POLL_H
|
|
|
|
if (set[1].revents & POLLIN)
|
|
|
|
#else
|
2003-08-14 15:47:31 +00:00
|
|
|
if (FD_ISSET(rtsock, selectfdp))
|
2003-08-14 18:43:57 +00:00
|
|
|
#endif
|
2001-06-11 12:39:29 +00:00
|
|
|
rtsock_input(rtsock);
|
2003-08-14 18:43:57 +00:00
|
|
|
#ifdef HAVE_POLL_H
|
|
|
|
if (set[0].revents & POLLIN)
|
|
|
|
#else
|
2003-08-14 15:47:31 +00:00
|
|
|
if (FD_ISSET(s, selectfdp))
|
2003-08-14 18:43:57 +00:00
|
|
|
#endif
|
1999-12-28 02:37:14 +00:00
|
|
|
rtsol_input(s);
|
|
|
|
}
|
|
|
|
/* NOTREACHED */
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2003-08-17 11:11:32 +00:00
|
|
|
int
|
1999-12-28 02:37:14 +00:00
|
|
|
ifconfig(char *ifname)
|
|
|
|
{
|
|
|
|
struct ifinfo *ifinfo;
|
|
|
|
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);
|
1999-12-28 02:37:14 +00:00
|
|
|
return(-1);
|
|
|
|
}
|
|
|
|
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);
|
1999-12-28 02:37:14 +00:00
|
|
|
return(-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((ifinfo = malloc(sizeof(*ifinfo))) == 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);
|
1999-12-28 02:37:14 +00:00
|
|
|
return(-1);
|
|
|
|
}
|
|
|
|
memset(ifinfo, 0, sizeof(*ifinfo));
|
|
|
|
ifinfo->sdl = sdl;
|
|
|
|
|
2003-08-11 15:49:47 +00:00
|
|
|
strlcpy(ifinfo->ifname, ifname, sizeof(ifinfo->ifname));
|
1999-12-28 02:37:14 +00:00
|
|
|
|
|
|
|
/* construct a router solicitation message */
|
|
|
|
if (make_packet(ifinfo))
|
|
|
|
goto bad;
|
|
|
|
|
2003-08-17 11:11:32 +00:00
|
|
|
/* set link ID of this interface. */
|
|
|
|
#ifdef HAVE_SCOPELIB
|
|
|
|
if (inet_zoneid(AF_INET6, 2, ifname, &ifinfo->linkid))
|
|
|
|
goto bad;
|
|
|
|
#else
|
|
|
|
/* XXX: assume interface IDs as link IDs */
|
|
|
|
ifinfo->linkid = ifinfo->sdl->sdl_index;
|
|
|
|
#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.
|
|
|
|
*/
|
|
|
|
ifinfo->mediareqok = 1;
|
|
|
|
ifinfo->active = interface_status(ifinfo);
|
|
|
|
if (!ifinfo->mediareqok) {
|
|
|
|
/*
|
|
|
|
* probe routers periodically even if the link status
|
|
|
|
* does not change.
|
|
|
|
*/
|
|
|
|
ifinfo->probeinterval = PROBE_INTERVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* activate interface: interface_up returns 0 on success */
|
|
|
|
flags = interface_up(ifinfo->ifname);
|
|
|
|
if (flags == 0)
|
|
|
|
ifinfo->state = IFS_DELAY;
|
|
|
|
else if (flags == IFS_TENTATIVE)
|
|
|
|
ifinfo->state = IFS_TENTATIVE;
|
|
|
|
else
|
|
|
|
ifinfo->state = IFS_DOWN;
|
|
|
|
|
|
|
|
rtsol_timer_update(ifinfo);
|
|
|
|
|
|
|
|
/* link into chain */
|
|
|
|
if (iflist)
|
|
|
|
ifinfo->next = iflist;
|
|
|
|
iflist = ifinfo;
|
|
|
|
|
|
|
|
return(0);
|
|
|
|
|
2003-08-08 16:56:01 +00:00
|
|
|
bad:
|
1999-12-28 02:37:14 +00:00
|
|
|
free(ifinfo->sdl);
|
2000-07-05 10:14:11 +00:00
|
|
|
free(ifinfo);
|
1999-12-28 02:37:14 +00:00
|
|
|
return(-1);
|
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
|
|
|
struct ifinfo *ifi, *next;
|
|
|
|
|
|
|
|
for (ifi = iflist; ifi; ifi = next) {
|
|
|
|
next = ifi->next;
|
|
|
|
if (ifi->sdl)
|
|
|
|
free(ifi->sdl);
|
|
|
|
if (ifi->rs_data)
|
|
|
|
free(ifi->rs_data);
|
|
|
|
free(ifi);
|
|
|
|
iflist = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2000-07-05 10:14:11 +00:00
|
|
|
#if 0
|
|
|
|
static int
|
|
|
|
ifreconfig(char *ifname)
|
|
|
|
{
|
|
|
|
struct ifinfo *ifi, *prev;
|
|
|
|
int rv;
|
|
|
|
|
|
|
|
prev = NULL;
|
|
|
|
for (ifi = iflist; ifi; ifi = ifi->next) {
|
|
|
|
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);
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
1999-12-28 02:37:14 +00:00
|
|
|
struct ifinfo *
|
|
|
|
find_ifinfo(int ifindex)
|
|
|
|
{
|
|
|
|
struct ifinfo *ifi;
|
|
|
|
|
|
|
|
for (ifi = iflist; ifi; ifi = ifi->next)
|
|
|
|
if (ifi->sdl->sdl_index == ifindex)
|
|
|
|
return(ifi);
|
|
|
|
return(NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
make_packet(struct ifinfo *ifinfo)
|
|
|
|
{
|
|
|
|
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
|
|
|
|
|
|
|
if ((lladdroptlen = lladdropt_length(ifinfo->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"
|
|
|
|
" on %s. Treat as not included.", ifinfo->ifname);
|
1999-12-28 02:37:14 +00:00
|
|
|
}
|
|
|
|
packlen += lladdroptlen;
|
|
|
|
ifinfo->rs_datalen = packlen;
|
|
|
|
|
|
|
|
/* allocate buffer */
|
|
|
|
if ((buf = malloc(packlen)) == NULL) {
|
2003-08-08 16:38:23 +00:00
|
|
|
warnmsg(LOG_ERR, __func__,
|
2003-08-08 16:56:01 +00:00
|
|
|
"memory allocation failed for %s", ifinfo->ifname);
|
1999-12-28 02:37:14 +00:00
|
|
|
return(-1);
|
|
|
|
}
|
|
|
|
ifinfo->rs_data = buf;
|
|
|
|
|
|
|
|
/* 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)
|
|
|
|
lladdropt_fill(ifinfo->sdl, (struct nd_opt_hdr *)buf);
|
|
|
|
|
|
|
|
return(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct timeval *
|
2004-01-14 17:16:19 +00:00
|
|
|
rtsol_check_timer(void)
|
1999-12-28 02:37:14 +00:00
|
|
|
{
|
|
|
|
static struct timeval returnval;
|
|
|
|
struct timeval now, rtsol_timer;
|
|
|
|
struct ifinfo *ifinfo;
|
|
|
|
int flags;
|
|
|
|
|
|
|
|
gettimeofday(&now, NULL);
|
|
|
|
|
|
|
|
rtsol_timer = tm_max;
|
|
|
|
|
|
|
|
for (ifinfo = iflist; ifinfo; ifinfo = ifinfo->next) {
|
2004-01-14 16:55:08 +00:00
|
|
|
if (timercmp(&ifinfo->expire, &now, <=)) {
|
1999-12-28 02:37:14 +00:00
|
|
|
if (dflag > 1)
|
2003-08-08 16:38:23 +00:00
|
|
|
warnmsg(LOG_DEBUG, __func__,
|
2003-08-08 16:56:01 +00:00
|
|
|
"timer expiration on %s, "
|
|
|
|
"state = %d", ifinfo->ifname,
|
|
|
|
ifinfo->state);
|
1999-12-28 02:37:14 +00:00
|
|
|
|
2000-10-06 23:46:52 +00:00
|
|
|
switch (ifinfo->state) {
|
1999-12-28 02:37:14 +00:00
|
|
|
case IFS_DOWN:
|
|
|
|
case IFS_TENTATIVE:
|
|
|
|
/* interface_up returns 0 on success */
|
|
|
|
flags = interface_up(ifinfo->ifname);
|
|
|
|
if (flags == 0)
|
|
|
|
ifinfo->state = IFS_DELAY;
|
|
|
|
else if (flags == IFS_TENTATIVE)
|
|
|
|
ifinfo->state = IFS_TENTATIVE;
|
|
|
|
else
|
|
|
|
ifinfo->state = IFS_DOWN;
|
|
|
|
break;
|
|
|
|
case IFS_IDLE:
|
|
|
|
{
|
|
|
|
int oldstatus = ifinfo->active;
|
|
|
|
int probe = 0;
|
|
|
|
|
2003-08-08 16:56:01 +00:00
|
|
|
ifinfo->active = interface_status(ifinfo);
|
1999-12-28 02:37:14 +00:00
|
|
|
|
|
|
|
if (oldstatus != ifinfo->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",
|
|
|
|
ifinfo->ifname,
|
|
|
|
oldstatus, ifinfo->active);
|
1999-12-28 02:37:14 +00:00
|
|
|
probe = 1;
|
|
|
|
ifinfo->state = IFS_DELAY;
|
2003-08-08 16:56:01 +00:00
|
|
|
} else if (ifinfo->probeinterval &&
|
|
|
|
(ifinfo->probetimer -=
|
|
|
|
ifinfo->timer.tv_sec) <= 0) {
|
1999-12-28 02:37:14 +00:00
|
|
|
/* probe timer expired */
|
|
|
|
ifinfo->probetimer =
|
2003-08-08 16:56:01 +00:00
|
|
|
ifinfo->probeinterval;
|
1999-12-28 02:37:14 +00:00
|
|
|
probe = 1;
|
|
|
|
ifinfo->state = IFS_PROBE;
|
|
|
|
}
|
|
|
|
|
2003-08-08 16:42:37 +00:00
|
|
|
/*
|
|
|
|
* If we need a probe, clear the previous
|
|
|
|
* status wrt the "other" configuration.
|
|
|
|
*/
|
|
|
|
if (probe)
|
|
|
|
ifinfo->otherconfig = 0;
|
|
|
|
|
1999-12-28 02:37:14 +00:00
|
|
|
if (probe && mobile_node)
|
2003-08-17 11:11:32 +00:00
|
|
|
defrouter_probe(ifinfo);
|
1999-12-28 02:37:14 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case IFS_DELAY:
|
|
|
|
ifinfo->state = IFS_PROBE;
|
|
|
|
sendpacket(ifinfo);
|
|
|
|
break;
|
|
|
|
case IFS_PROBE:
|
|
|
|
if (ifinfo->probes < MAX_RTR_SOLICITATIONS)
|
|
|
|
sendpacket(ifinfo);
|
|
|
|
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",
|
|
|
|
ifinfo->probes);
|
1999-12-28 02:37:14 +00:00
|
|
|
ifinfo->probes = 0;
|
|
|
|
ifinfo->state = IFS_IDLE;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
rtsol_timer_update(ifinfo);
|
|
|
|
}
|
|
|
|
|
2004-01-14 16:55:08 +00:00
|
|
|
if (timercmp(&ifinfo->expire, &rtsol_timer, <))
|
1999-12-28 02:37:14 +00:00
|
|
|
rtsol_timer = ifinfo->expire;
|
|
|
|
}
|
|
|
|
|
2004-01-14 16:55:08 +00:00
|
|
|
if (timercmp(&rtsol_timer, &tm_max, ==)) {
|
2003-08-08 16:38:23 +00:00
|
|
|
warnmsg(LOG_DEBUG, __func__, "there is no timer");
|
1999-12-28 02:37:14 +00:00
|
|
|
return(NULL);
|
2004-01-14 16:55:08 +00:00
|
|
|
} else if (timercmp(&rtsol_timer, &now, <))
|
1999-12-28 02:37:14 +00:00
|
|
|
/* this may occur when the interval is too small */
|
|
|
|
returnval.tv_sec = returnval.tv_usec = 0;
|
|
|
|
else
|
2004-01-14 16:55:08 +00:00
|
|
|
timersub(&rtsol_timer, &now, &returnval);
|
1999-12-28 02:37:14 +00:00
|
|
|
|
|
|
|
if (dflag > 1)
|
2003-08-08 16:38:23 +00:00
|
|
|
warnmsg(LOG_DEBUG, __func__, "New timer is %ld:%08ld",
|
2003-08-08 16:56:01 +00:00
|
|
|
(long)returnval.tv_sec, (long)returnval.tv_usec);
|
1999-12-28 02:37:14 +00:00
|
|
|
|
|
|
|
return(&returnval);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
rtsol_timer_update(struct ifinfo *ifinfo)
|
|
|
|
{
|
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;
|
|
|
|
struct timeval now;
|
|
|
|
|
|
|
|
bzero(&ifinfo->timer, sizeof(ifinfo->timer));
|
|
|
|
|
|
|
|
switch (ifinfo->state) {
|
|
|
|
case IFS_DOWN:
|
|
|
|
case IFS_TENTATIVE:
|
|
|
|
if (++ifinfo->dadcount > DADRETRY) {
|
|
|
|
ifinfo->dadcount = 0;
|
|
|
|
ifinfo->timer.tv_sec = PROBE_INTERVAL;
|
2003-08-08 16:56:01 +00:00
|
|
|
} else
|
1999-12-28 02:37:14 +00:00
|
|
|
ifinfo->timer.tv_sec = 1;
|
|
|
|
break;
|
|
|
|
case IFS_IDLE:
|
|
|
|
if (mobile_node) {
|
2003-08-08 16:56:01 +00:00
|
|
|
/* XXX should be configurable */
|
1999-12-28 02:37:14 +00:00
|
|
|
ifinfo->timer.tv_sec = 3;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
ifinfo->timer = tm_max; /* stop timer(valid?) */
|
|
|
|
break;
|
|
|
|
case IFS_DELAY:
|
2000-07-05 10:14:11 +00:00
|
|
|
#ifndef HAVE_ARC4RANDOM
|
1999-12-28 02:37:14 +00:00
|
|
|
interval = random() % (MAX_RTR_SOLICITATION_DELAY * MILLION);
|
2000-07-05 10:14:11 +00:00
|
|
|
#else
|
2008-07-26 15:46:39 +00:00
|
|
|
interval = arc4random_uniform(MAX_RTR_SOLICITATION_DELAY * MILLION);
|
2000-07-05 10:14:11 +00:00
|
|
|
#endif
|
1999-12-28 02:37:14 +00:00
|
|
|
ifinfo->timer.tv_sec = interval / MILLION;
|
|
|
|
ifinfo->timer.tv_usec = interval % MILLION;
|
|
|
|
break;
|
|
|
|
case IFS_PROBE:
|
2001-06-11 12:39:29 +00:00
|
|
|
if (ifinfo->probes < MAX_RTR_SOLICITATIONS)
|
|
|
|
ifinfo->timer.tv_sec = RTR_SOLICITATION_INTERVAL;
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
ifinfo->timer.tv_sec = MAX_RTR_SOLICITATION_DELAY;
|
|
|
|
}
|
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",
|
|
|
|
ifinfo->state, ifinfo->ifname);
|
1999-12-28 02:37:14 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* reset the timer */
|
2004-01-14 16:55:08 +00:00
|
|
|
if (timercmp(&ifinfo->timer, &tm_max, ==)) {
|
1999-12-28 02:37:14 +00:00
|
|
|
ifinfo->expire = tm_max;
|
2003-08-08 16:38:23 +00:00
|
|
|
warnmsg(LOG_DEBUG, __func__,
|
2003-08-08 16:56:01 +00:00
|
|
|
"stop timer for %s", ifinfo->ifname);
|
|
|
|
} else {
|
1999-12-28 02:37:14 +00:00
|
|
|
gettimeofday(&now, NULL);
|
2004-01-14 16:55:08 +00:00
|
|
|
timeradd(&now, &ifinfo->timer, &ifinfo->expire);
|
1999-12-28 02:37:14 +00:00
|
|
|
|
|
|
|
if (dflag > 1)
|
2003-08-08 16:38:23 +00:00
|
|
|
warnmsg(LOG_DEBUG, __func__,
|
2003-08-08 16:56:01 +00:00
|
|
|
"set timer for %s to %d:%d", ifinfo->ifname,
|
|
|
|
(int)ifinfo->timer.tv_sec,
|
|
|
|
(int)ifinfo->timer.tv_usec);
|
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
|
2004-01-14 17:16:19 +00:00
|
|
|
rtsold_set_dump_file(int sig)
|
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
|
|
|
|
usage(char *progname)
|
|
|
|
{
|
2000-10-06 23:46:52 +00:00
|
|
|
if (progname && progname[strlen(progname) - 1] != 'd') {
|
2004-01-14 17:42:03 +00:00
|
|
|
fprintf(stderr, "usage: rtsol [-dDF] interfaces...\n");
|
|
|
|
fprintf(stderr, "usage: rtsol [-dDF] -a\n");
|
2000-10-06 23:46:52 +00:00
|
|
|
} else {
|
2004-01-14 17:42:03 +00:00
|
|
|
fprintf(stderr, "usage: rtsold [-adDfFm1] interfaces...\n");
|
|
|
|
fprintf(stderr, "usage: rtsold [-dDfFm1] -a\n");
|
2000-10-06 23:46:52 +00:00
|
|
|
}
|
1999-12-28 02:37:14 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
#if __STDC__
|
|
|
|
warnmsg(int priority, const char *func, const char *msg, ...)
|
|
|
|
#else
|
|
|
|
warnmsg(priority, func, msg, va_alist)
|
|
|
|
int priority;
|
|
|
|
const char *func;
|
|
|
|
const char *msg;
|
|
|
|
va_dcl
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
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;
|
|
|
|
int i, found;
|
2000-10-06 23:46:52 +00:00
|
|
|
struct ifaddrs *ifap, *ifa, *target;
|
|
|
|
|
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)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
target = NULL;
|
|
|
|
/* 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;
|
|
|
|
|
2003-08-17 11:11:32 +00:00
|
|
|
/* if we find multiple candidates, just warn. */
|
|
|
|
if (n != 0 && dflag > 1)
|
|
|
|
warnx("multiple interfaces found");
|
|
|
|
|
|
|
|
a = (char **)realloc(argv, (n + 1) * sizeof(char **));
|
|
|
|
if (a == NULL)
|
|
|
|
err(1, "realloc");
|
|
|
|
argv = a;
|
|
|
|
argv[n] = strdup(ifa->ifa_name);
|
|
|
|
if (!argv[n])
|
|
|
|
err(1, "malloc");
|
|
|
|
n++;
|
|
|
|
argv[n] = NULL;
|
2000-10-06 23:46:52 +00:00
|
|
|
}
|
|
|
|
|
2003-08-17 11:11:32 +00:00
|
|
|
if (n) {
|
|
|
|
a = (char **)realloc(argv, (n + 1) * sizeof(char **));
|
|
|
|
if (a == NULL)
|
|
|
|
err(1, "realloc");
|
|
|
|
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++)
|
|
|
|
warnx("probing %s", argv[i]);
|
|
|
|
}
|
2000-10-06 23:46:52 +00:00
|
|
|
}
|
|
|
|
freeifaddrs(ifap);
|
2003-08-17 11:11:32 +00:00
|
|
|
return argv;
|
2000-10-06 23:46:52 +00:00
|
|
|
}
|