1 /* $KAME: rtsold.c,v 1.31 2001/05/22 06:03:06 jinmei Exp $ */
4 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the project nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * $FreeBSD: src/usr.sbin/rtsold/rtsold.c,v 1.1.2.4 2002/04/04 11:07:19 ume Exp $
32 * $DragonFly: src/usr.sbin/rtsold/rtsold.c,v 1.6 2005/02/17 14:00:10 joerg Exp $
35 #include <sys/types.h>
37 #include <sys/socket.h>
40 #include <net/if_dl.h>
42 #include <netinet/in.h>
43 #include <netinet/icmp6.h>
57 struct ifinfo *iflist;
58 struct timeval tm_max = {0x7fffffff, 0x7fffffff};
61 static int log_upto = 999;
64 /* protocol constatns */
65 #define MAX_RTR_SOLICITATION_DELAY 1 /* second */
66 #define RTR_SOLICITATION_INTERVAL 4 /* seconds */
67 #define MAX_RTR_SOLICITATIONS 3 /* times */
69 /* implementation dependent constants */
70 #define PROBE_INTERVAL 60 /* secondes XXX: should be configurable */
74 #define TIMEVAL_LT(a, b) (((a).tv_sec < (b).tv_sec) ||\
75 (((a).tv_sec == (b).tv_sec) && \
76 ((a).tv_usec < (b).tv_usec)))
79 #define TIMEVAL_LEQ(a, b) (((a).tv_sec < (b).tv_sec) ||\
80 (((a).tv_sec == (b).tv_sec) &&\
81 ((a).tv_usec <= (b).tv_usec)))
84 #define TIMEVAL_EQ(a, b) (((a).tv_sec==(b).tv_sec) && ((a).tv_usec==(b).tv_usec))
86 int main(int argc, char *argv[]);
88 /* static variables and functions */
89 static int mobile_node = 0;
92 * XXX: the following two values should be configurable
94 static const char *dumpfilename = "/var/run/rtsold.dump";
95 static const char *pidfilename = "/var/run/rtsold.pid";
97 static int ifconfig(char *ifname);
99 static int ifreconfig(char *ifname);
101 static int make_packet(struct ifinfo *ifinfo);
102 static struct timeval *rtsol_check_timer(void);
103 static void TIMEVAL_ADD(struct timeval *a, struct timeval *b,
104 struct timeval *result);
105 static void TIMEVAL_SUB(struct timeval *a, struct timeval *b,
106 struct timeval *result);
108 static void rtsold_set_dump_file(int);
109 static void usage(char *progname);
110 static char **autoifprobe(void);
117 int s, rtsock, maxfd, ch;
119 struct timeval *timeout;
130 if (argv0 && argv0[strlen(argv0) - 1] != 'd') {
137 while ((ch = getopt(argc, argv, opts)) != -1) {
173 argv = autoifprobe();
175 errx(1, "could not autoprobe interface");
179 for (i = 0; argv[i]; i++)
190 log_upto = LOG_NOTICE;
193 ident = strrchr(argv0, '/');
198 openlog(ident, LOG_NDELAY|LOG_PID, LOG_DAEMON);
200 setlogmask(LOG_UPTO(log_upto));
203 #ifndef HAVE_ARC4RANDOM
204 /* random value initilization */
205 srandom((u_long)time(NULL));
208 /* warn if accept_rtadv is down */
209 if (!getinet6sysctl(IPV6CTL_ACCEPT_RTADV))
210 warnx("kernel is configured not to accept RAs");
211 /* warn if forwarding is up */
212 if (getinet6sysctl(IPV6CTL_FORWARDING))
213 warnx("kernel is configured as a router, not a host");
215 /* initialization to dump internal status to a file */
216 if (signal(SIGUSR1, rtsold_set_dump_file) == SIG_ERR) {
217 errx(1, "failed to set signal for dump status");
222 * Open a socket for sending RS and receiving RA.
223 * This should be done before calling ifinit(), since the function
226 if ((s = sockopen()) < 0) {
227 errx(1, "failed to open a socket");
231 if ((rtsock = rtsock_open()) < 0) {
232 errx(1, "failed to open a socket");
238 /* configuration per interface */
240 errx(1, "failed to initilizatoin interfaces");
244 if (ifconfig(*argv)) {
245 errx(1, "failed to initialize %s", *argv);
251 /* setup for probing default routers */
253 errx(1, "failed to setup for probing routers");
258 daemon(0, 0); /* act as a daemon */
260 /* dump the current pid */
262 pid_t pid = getpid();
265 if ((fp = fopen(pidfilename, "w")) == NULL)
266 warnmsg(LOG_ERR, __func__,
267 "failed to open a log file(%s): %s",
268 pidfilename, strerror(errno));
270 fprintf(fp, "%d\n", pid);
277 FD_SET(rtsock, &fdset);
278 while (1) { /* main loop */
280 struct fd_set select_fd = fdset;
282 if (do_dump) { /* SIGUSR1 */
284 rtsold_dump_file(dumpfilename);
287 timeout = rtsol_check_timer();
292 /* if we have no timeout, we are done (or failed) */
296 /* if all interfaces have got RA packet, we are done */
297 for (ifi = iflist; ifi; ifi = ifi->next) {
298 if (ifi->state != IFS_DOWN && ifi->racnt == 0)
304 e = select(maxfd + 1, &select_fd, NULL, NULL, timeout);
306 if (e < 0 && errno != EINTR) {
307 warnmsg(LOG_ERR, __func__, "select: %s",
313 /* packet reception */
314 if (FD_ISSET(rtsock, &select_fd))
315 rtsock_input(rtsock);
316 if (FD_ISSET(s, &select_fd))
325 ifconfig(char *ifname)
327 struct ifinfo *ifinfo;
328 struct sockaddr_dl *sdl;
331 if ((sdl = if_nametosdl(ifname)) == NULL) {
332 warnmsg(LOG_ERR, __func__,
333 "failed to get link layer information for %s", ifname);
336 if (find_ifinfo(sdl->sdl_index)) {
337 warnmsg(LOG_ERR, __func__,
338 "interface %s was already configured", ifname);
343 if ((ifinfo = malloc(sizeof(*ifinfo))) == NULL) {
344 warnmsg(LOG_ERR, __func__, "memory allocation failed");
348 memset(ifinfo, 0, sizeof(*ifinfo));
351 strncpy(ifinfo->ifname, ifname, sizeof(ifinfo->ifname));
353 /* construct a router solicitation message */
354 if (make_packet(ifinfo))
358 * check if the interface is available.
359 * also check if SIOCGIFMEDIA ioctl is OK on the interface.
361 ifinfo->mediareqok = 1;
362 ifinfo->active = interface_status(ifinfo);
363 if (!ifinfo->mediareqok) {
365 * probe routers periodically even if the link status
368 ifinfo->probeinterval = PROBE_INTERVAL;
371 /* activate interface: interface_up returns 0 on success */
372 flags = interface_up(ifinfo->ifname);
374 ifinfo->state = IFS_DELAY;
375 else if (flags == IFS_TENTATIVE)
376 ifinfo->state = IFS_TENTATIVE;
378 ifinfo->state = IFS_DOWN;
380 rtsol_timer_update(ifinfo);
382 /* link into chain */
384 ifinfo->next = iflist;
397 ifreconfig(char *ifname)
399 struct ifinfo *ifi, *prev;
403 for (ifi = iflist; ifi; ifi = ifi->next) {
404 if (strncmp(ifi->ifname, ifname, sizeof(ifi->ifname)) == 0)
408 prev->next = ifi->next;
410 rv = ifconfig(ifname);
412 /* reclaim it after ifconfig() in case ifname is pointer inside ifi */
423 find_ifinfo(int ifindex)
427 for (ifi = iflist; ifi; ifi = ifi->next)
428 if (ifi->sdl->sdl_index == ifindex)
435 make_packet(struct ifinfo *ifinfo)
438 struct nd_router_solicit *rs;
439 size_t packlen = sizeof(struct nd_router_solicit), lladdroptlen = 0;
441 if ((lladdroptlen = lladdropt_length(ifinfo->sdl)) == 0) {
442 warnmsg(LOG_INFO, __func__,
443 "link-layer address option has null length"
444 " on %s. Treat as not included.", ifinfo->ifname);
446 packlen += lladdroptlen;
447 ifinfo->rs_datalen = packlen;
449 /* allocate buffer */
450 if ((buf = malloc(packlen)) == NULL) {
451 warnmsg(LOG_ERR, __func__,
452 "memory allocation failed for %s", ifinfo->ifname);
455 ifinfo->rs_data = buf;
457 /* fill in the message */
458 rs = (struct nd_router_solicit *)buf;
459 rs->nd_rs_type = ND_ROUTER_SOLICIT;
462 rs->nd_rs_reserved = 0;
465 /* fill in source link-layer address option */
467 lladdropt_fill(ifinfo->sdl, (struct nd_opt_hdr *)buf);
472 static struct timeval *
475 static struct timeval returnval;
476 struct timeval now, rtsol_timer;
477 struct ifinfo *ifinfo;
480 gettimeofday(&now, NULL);
482 rtsol_timer = tm_max;
484 for (ifinfo = iflist; ifinfo; ifinfo = ifinfo->next) {
485 if (TIMEVAL_LEQ(ifinfo->expire, now)) {
487 warnmsg(LOG_DEBUG, __func__,
488 "timer expiration on %s, "
489 "state = %d", ifinfo->ifname,
492 switch (ifinfo->state) {
495 /* interface_up returns 0 on success */
496 flags = interface_up(ifinfo->ifname);
498 ifinfo->state = IFS_DELAY;
499 else if (flags == IFS_TENTATIVE)
500 ifinfo->state = IFS_TENTATIVE;
502 ifinfo->state = IFS_DOWN;
506 int oldstatus = ifinfo->active;
510 interface_status(ifinfo);
512 if (oldstatus != ifinfo->active) {
513 warnmsg(LOG_DEBUG, __func__,
514 "%s status is changed"
517 oldstatus, ifinfo->active);
519 ifinfo->state = IFS_DELAY;
521 else if (ifinfo->probeinterval &&
522 (ifinfo->probetimer -=
523 ifinfo->timer.tv_sec) <= 0) {
524 /* probe timer expired */
526 ifinfo->probeinterval;
528 ifinfo->state = IFS_PROBE;
531 if (probe && mobile_node)
532 defrouter_probe(ifinfo->sdl->sdl_index);
536 ifinfo->state = IFS_PROBE;
540 if (ifinfo->probes < MAX_RTR_SOLICITATIONS)
543 warnmsg(LOG_INFO, __func__,
545 "after sending %d RSs",
548 ifinfo->state = IFS_IDLE;
552 rtsol_timer_update(ifinfo);
555 if (TIMEVAL_LT(ifinfo->expire, rtsol_timer))
556 rtsol_timer = ifinfo->expire;
559 if (TIMEVAL_EQ(rtsol_timer, tm_max)) {
560 warnmsg(LOG_DEBUG, __func__, "there is no timer");
563 else if (TIMEVAL_LT(rtsol_timer, now))
564 /* this may occur when the interval is too small */
565 returnval.tv_sec = returnval.tv_usec = 0;
567 TIMEVAL_SUB(&rtsol_timer, &now, &returnval);
570 warnmsg(LOG_DEBUG, __func__, "New timer is %ld:%08ld",
571 (long)returnval.tv_sec, (long)returnval.tv_usec);
577 rtsol_timer_update(struct ifinfo *ifinfo)
579 #define MILLION 1000000
580 #define DADRETRY 10 /* XXX: adhoc */
584 bzero(&ifinfo->timer, sizeof(ifinfo->timer));
586 switch (ifinfo->state) {
589 if (++ifinfo->dadcount > DADRETRY) {
590 ifinfo->dadcount = 0;
591 ifinfo->timer.tv_sec = PROBE_INTERVAL;
594 ifinfo->timer.tv_sec = 1;
598 /* XXX should be configurable */
599 ifinfo->timer.tv_sec = 3;
602 ifinfo->timer = tm_max; /* stop timer(valid?) */
605 #ifndef HAVE_ARC4RANDOM
606 interval = random() % (MAX_RTR_SOLICITATION_DELAY * MILLION);
608 interval = arc4random() % (MAX_RTR_SOLICITATION_DELAY * MILLION);
610 ifinfo->timer.tv_sec = interval / MILLION;
611 ifinfo->timer.tv_usec = interval % MILLION;
614 if (ifinfo->probes < MAX_RTR_SOLICITATIONS)
615 ifinfo->timer.tv_sec = RTR_SOLICITATION_INTERVAL;
618 * After sending MAX_RTR_SOLICITATIONS solicitations,
619 * we're just waiting for possible replies; there
620 * will be no more solicatation. Thus, we change
621 * the timer value to MAX_RTR_SOLICITATION_DELAY based
622 * on RFC 2461, Section 6.3.7.
624 ifinfo->timer.tv_sec = MAX_RTR_SOLICITATION_DELAY;
628 warnmsg(LOG_ERR, __func__,
629 "illegal interface state(%d) on %s",
630 ifinfo->state, ifinfo->ifname);
634 /* reset the timer */
635 if (TIMEVAL_EQ(ifinfo->timer, tm_max)) {
636 ifinfo->expire = tm_max;
637 warnmsg(LOG_DEBUG, __func__,
638 "stop timer for %s", ifinfo->ifname);
641 gettimeofday(&now, NULL);
642 TIMEVAL_ADD(&now, &ifinfo->timer, &ifinfo->expire);
645 warnmsg(LOG_DEBUG, __func__,
646 "set timer for %s to %d:%d", ifinfo->ifname,
647 (int)ifinfo->timer.tv_sec,
648 (int)ifinfo->timer.tv_usec);
654 /* timer related utility functions */
655 #define MILLION 1000000
659 TIMEVAL_ADD(struct timeval *a, struct timeval *b, struct timeval *result)
663 if ((l = a->tv_usec + b->tv_usec) < MILLION) {
665 result->tv_sec = a->tv_sec + b->tv_sec;
668 result->tv_usec = l - MILLION;
669 result->tv_sec = a->tv_sec + b->tv_sec + 1;
675 * XXX: this function assumes that a >= b.
678 TIMEVAL_SUB(struct timeval *a, struct timeval *b, struct timeval *result)
682 if ((l = a->tv_usec - b->tv_usec) >= 0) {
684 result->tv_sec = a->tv_sec - b->tv_sec;
687 result->tv_usec = MILLION + l;
688 result->tv_sec = a->tv_sec - b->tv_sec - 1;
693 rtsold_set_dump_file(int signo __unused)
699 usage(char *progname)
701 if (progname && progname[strlen(progname) - 1] != 'd') {
702 fprintf(stderr, "usage: rtsol [-dD] interfaces...\n");
703 fprintf(stderr, "usage: rtsol [-dD] -a\n");
705 fprintf(stderr, "usage: rtsold [-adDfm1] interfaces...\n");
706 fprintf(stderr, "usage: rtsold [-dDfm1] -a\n");
713 warnmsg(int priority, const char *func, const char *msg, ...)
715 warnmsg(priority, func, msg, va_alist)
727 if (priority <= log_upto) {
728 vfprintf(stderr, msg, ap);
729 fprintf(stderr, "\n");
732 snprintf(buf, sizeof(buf), "<%s> %s", func, msg);
734 vsyslog(priority, msg, ap);
742 #ifndef HAVE_GETIFADDRS
743 errx(1, "-a is not available with the configuration");
745 static char ifname[IFNAMSIZ + 1];
746 static char *argv[2];
747 struct ifaddrs *ifap, *ifa, *target;
749 if (getifaddrs(&ifap) != 0)
753 /* find an ethernet */
754 for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
755 if ((ifa->ifa_flags & IFF_UP) == 0)
757 if ((ifa->ifa_flags & IFF_POINTOPOINT) != 0)
759 if ((ifa->ifa_flags & IFF_LOOPBACK) != 0)
761 if ((ifa->ifa_flags & IFF_MULTICAST) == 0)
764 if (ifa->ifa_addr->sa_family != AF_INET6)
767 if (target && strcmp(target->ifa_name, ifa->ifa_name) == 0)
773 /* if we find multiple candidates, failure. */
775 warnx("multiple interfaces found");
782 strncpy(ifname, target->ifa_name, sizeof(ifname) - 1);
783 ifname[sizeof(ifname) - 1] = '\0';
788 warnx("probing %s", argv[0]);
794 return (char **)NULL;