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
34 #include <sys/types.h>
36 #include <sys/socket.h>
39 #include <net/if_dl.h>
41 #include <netinet/in.h>
42 #include <netinet/icmp6.h>
56 struct ifinfo *iflist;
57 struct timeval tm_max = {0x7fffffff, 0x7fffffff};
60 static int log_upto = 999;
63 /* protocol constatns */
64 #define MAX_RTR_SOLICITATION_DELAY 1 /* second */
65 #define RTR_SOLICITATION_INTERVAL 4 /* seconds */
66 #define MAX_RTR_SOLICITATIONS 3 /* times */
68 /* implementation dependent constants */
69 #define PROBE_INTERVAL 60 /* secondes XXX: should be configurable */
73 #define TIMEVAL_LT(a, b) (((a).tv_sec < (b).tv_sec) ||\
74 (((a).tv_sec == (b).tv_sec) && \
75 ((a).tv_usec < (b).tv_usec)))
78 #define TIMEVAL_LEQ(a, b) (((a).tv_sec < (b).tv_sec) ||\
79 (((a).tv_sec == (b).tv_sec) &&\
80 ((a).tv_usec <= (b).tv_usec)))
83 #define TIMEVAL_EQ(a, b) (((a).tv_sec==(b).tv_sec) && ((a).tv_usec==(b).tv_usec))
85 int main __P((int argc, char *argv[]));
87 /* static variables and functions */
88 static int mobile_node = 0;
90 static char *dumpfilename = "/var/run/rtsold.dump"; /* XXX: should be configurable */
91 static char *pidfilename = "/var/run/rtsold.pid"; /* should be configurable */
93 static int ifconfig __P((char *ifname));
95 static int ifreconfig __P((char *ifname));
97 static int make_packet __P((struct ifinfo *ifinfo));
98 static struct timeval *rtsol_check_timer __P((void));
99 static void TIMEVAL_ADD __P((struct timeval *a, struct timeval *b,
100 struct timeval *result));
101 static void TIMEVAL_SUB __P((struct timeval *a, struct timeval *b,
102 struct timeval *result));
104 static void rtsold_set_dump_file __P((void));
105 static void usage __P((char *progname));
106 static char **autoifprobe __P((void));
113 int s, rtsock, maxfd, ch;
115 struct timeval *timeout;
126 if (argv0 && argv0[strlen(argv0) - 1] != 'd') {
133 while ((ch = getopt(argc, argv, opts)) != -1) {
169 argv = autoifprobe();
171 errx(1, "could not autoprobe interface");
175 for (i = 0; argv[i]; i++)
186 log_upto = LOG_NOTICE;
189 ident = strrchr(argv0, '/');
194 openlog(ident, LOG_NDELAY|LOG_PID, LOG_DAEMON);
196 setlogmask(LOG_UPTO(log_upto));
199 #ifndef HAVE_ARC4RANDOM
200 /* random value initilization */
201 srandom((u_long)time(NULL));
204 /* warn if accept_rtadv is down */
205 if (!getinet6sysctl(IPV6CTL_ACCEPT_RTADV))
206 warnx("kernel is configured not to accept RAs");
207 /* warn if forwarding is up */
208 if (getinet6sysctl(IPV6CTL_FORWARDING))
209 warnx("kernel is configured as a router, not a host");
211 /* initialization to dump internal status to a file */
212 if (signal(SIGUSR1, (void *)rtsold_set_dump_file) < 0) {
213 errx(1, "failed to set signal for dump status");
218 * Open a socket for sending RS and receiving RA.
219 * This should be done before calling ifinit(), since the function
222 if ((s = sockopen()) < 0) {
223 errx(1, "failed to open a socket");
227 if ((rtsock = rtsock_open()) < 0) {
228 errx(1, "failed to open a socket");
234 /* configuration per interface */
236 errx(1, "failed to initilizatoin interfaces");
240 if (ifconfig(*argv)) {
241 errx(1, "failed to initialize %s", *argv);
247 /* setup for probing default routers */
249 errx(1, "failed to setup for probing routers");
254 daemon(0, 0); /* act as a daemon */
256 /* dump the current pid */
258 pid_t pid = getpid();
261 if ((fp = fopen(pidfilename, "w")) == NULL)
262 warnmsg(LOG_ERR, __FUNCTION__,
263 "failed to open a log file(%s): %s",
264 pidfilename, strerror(errno));
266 fprintf(fp, "%d\n", pid);
273 FD_SET(rtsock, &fdset);
274 while (1) { /* main loop */
276 struct fd_set select_fd = fdset;
278 if (do_dump) { /* SIGUSR1 */
280 rtsold_dump_file(dumpfilename);
283 timeout = rtsol_check_timer();
288 /* if we have no timeout, we are done (or failed) */
292 /* if all interfaces have got RA packet, we are done */
293 for (ifi = iflist; ifi; ifi = ifi->next) {
294 if (ifi->state != IFS_DOWN && ifi->racnt == 0)
300 e = select(maxfd + 1, &select_fd, NULL, NULL, timeout);
302 if (e < 0 && errno != EINTR) {
303 warnmsg(LOG_ERR, __FUNCTION__, "select: %s",
309 /* packet reception */
310 if (FD_ISSET(rtsock, &select_fd))
311 rtsock_input(rtsock);
312 if (FD_ISSET(s, &select_fd))
321 ifconfig(char *ifname)
323 struct ifinfo *ifinfo;
324 struct sockaddr_dl *sdl;
327 if ((sdl = if_nametosdl(ifname)) == NULL) {
328 warnmsg(LOG_ERR, __FUNCTION__,
329 "failed to get link layer information for %s", ifname);
332 if (find_ifinfo(sdl->sdl_index)) {
333 warnmsg(LOG_ERR, __FUNCTION__,
334 "interface %s was already configured", ifname);
339 if ((ifinfo = malloc(sizeof(*ifinfo))) == NULL) {
340 warnmsg(LOG_ERR, __FUNCTION__, "memory allocation failed");
344 memset(ifinfo, 0, sizeof(*ifinfo));
347 strncpy(ifinfo->ifname, ifname, sizeof(ifinfo->ifname));
349 /* construct a router solicitation message */
350 if (make_packet(ifinfo))
354 * check if the interface is available.
355 * also check if SIOCGIFMEDIA ioctl is OK on the interface.
357 ifinfo->mediareqok = 1;
358 ifinfo->active = interface_status(ifinfo);
359 if (!ifinfo->mediareqok) {
361 * probe routers periodically even if the link status
364 ifinfo->probeinterval = PROBE_INTERVAL;
367 /* activate interface: interface_up returns 0 on success */
368 flags = interface_up(ifinfo->ifname);
370 ifinfo->state = IFS_DELAY;
371 else if (flags == IFS_TENTATIVE)
372 ifinfo->state = IFS_TENTATIVE;
374 ifinfo->state = IFS_DOWN;
376 rtsol_timer_update(ifinfo);
378 /* link into chain */
380 ifinfo->next = iflist;
393 ifreconfig(char *ifname)
395 struct ifinfo *ifi, *prev;
399 for (ifi = iflist; ifi; ifi = ifi->next) {
400 if (strncmp(ifi->ifname, ifname, sizeof(ifi->ifname)) == 0)
404 prev->next = ifi->next;
406 rv = ifconfig(ifname);
408 /* reclaim it after ifconfig() in case ifname is pointer inside ifi */
419 find_ifinfo(int ifindex)
423 for (ifi = iflist; ifi; ifi = ifi->next)
424 if (ifi->sdl->sdl_index == ifindex)
431 make_packet(struct ifinfo *ifinfo)
434 struct nd_router_solicit *rs;
435 size_t packlen = sizeof(struct nd_router_solicit), lladdroptlen = 0;
437 if ((lladdroptlen = lladdropt_length(ifinfo->sdl)) == 0) {
438 warnmsg(LOG_INFO, __FUNCTION__,
439 "link-layer address option has null length"
440 " on %s. Treat as not included.", ifinfo->ifname);
442 packlen += lladdroptlen;
443 ifinfo->rs_datalen = packlen;
445 /* allocate buffer */
446 if ((buf = malloc(packlen)) == NULL) {
447 warnmsg(LOG_ERR, __FUNCTION__,
448 "memory allocation failed for %s", ifinfo->ifname);
451 ifinfo->rs_data = buf;
453 /* fill in the message */
454 rs = (struct nd_router_solicit *)buf;
455 rs->nd_rs_type = ND_ROUTER_SOLICIT;
458 rs->nd_rs_reserved = 0;
461 /* fill in source link-layer address option */
463 lladdropt_fill(ifinfo->sdl, (struct nd_opt_hdr *)buf);
468 static struct timeval *
471 static struct timeval returnval;
472 struct timeval now, rtsol_timer;
473 struct ifinfo *ifinfo;
476 gettimeofday(&now, NULL);
478 rtsol_timer = tm_max;
480 for (ifinfo = iflist; ifinfo; ifinfo = ifinfo->next) {
481 if (TIMEVAL_LEQ(ifinfo->expire, now)) {
483 warnmsg(LOG_DEBUG, __FUNCTION__,
484 "timer expiration on %s, "
485 "state = %d", ifinfo->ifname,
488 switch (ifinfo->state) {
491 /* interface_up returns 0 on success */
492 flags = interface_up(ifinfo->ifname);
494 ifinfo->state = IFS_DELAY;
495 else if (flags == IFS_TENTATIVE)
496 ifinfo->state = IFS_TENTATIVE;
498 ifinfo->state = IFS_DOWN;
502 int oldstatus = ifinfo->active;
506 interface_status(ifinfo);
508 if (oldstatus != ifinfo->active) {
509 warnmsg(LOG_DEBUG, __FUNCTION__,
510 "%s status is changed"
513 oldstatus, ifinfo->active);
515 ifinfo->state = IFS_DELAY;
517 else if (ifinfo->probeinterval &&
518 (ifinfo->probetimer -=
519 ifinfo->timer.tv_sec) <= 0) {
520 /* probe timer expired */
522 ifinfo->probeinterval;
524 ifinfo->state = IFS_PROBE;
527 if (probe && mobile_node)
528 defrouter_probe(ifinfo->sdl->sdl_index);
532 ifinfo->state = IFS_PROBE;
536 if (ifinfo->probes < MAX_RTR_SOLICITATIONS)
539 warnmsg(LOG_INFO, __FUNCTION__,
541 "after sending %d RSs",
544 ifinfo->state = IFS_IDLE;
548 rtsol_timer_update(ifinfo);
551 if (TIMEVAL_LT(ifinfo->expire, rtsol_timer))
552 rtsol_timer = ifinfo->expire;
555 if (TIMEVAL_EQ(rtsol_timer, tm_max)) {
556 warnmsg(LOG_DEBUG, __FUNCTION__, "there is no timer");
559 else if (TIMEVAL_LT(rtsol_timer, now))
560 /* this may occur when the interval is too small */
561 returnval.tv_sec = returnval.tv_usec = 0;
563 TIMEVAL_SUB(&rtsol_timer, &now, &returnval);
566 warnmsg(LOG_DEBUG, __FUNCTION__, "New timer is %ld:%08ld",
567 (long)returnval.tv_sec, (long)returnval.tv_usec);
573 rtsol_timer_update(struct ifinfo *ifinfo)
575 #define MILLION 1000000
576 #define DADRETRY 10 /* XXX: adhoc */
580 bzero(&ifinfo->timer, sizeof(ifinfo->timer));
582 switch (ifinfo->state) {
585 if (++ifinfo->dadcount > DADRETRY) {
586 ifinfo->dadcount = 0;
587 ifinfo->timer.tv_sec = PROBE_INTERVAL;
590 ifinfo->timer.tv_sec = 1;
594 /* XXX should be configurable */
595 ifinfo->timer.tv_sec = 3;
598 ifinfo->timer = tm_max; /* stop timer(valid?) */
601 #ifndef HAVE_ARC4RANDOM
602 interval = random() % (MAX_RTR_SOLICITATION_DELAY * MILLION);
604 interval = arc4random() % (MAX_RTR_SOLICITATION_DELAY * MILLION);
606 ifinfo->timer.tv_sec = interval / MILLION;
607 ifinfo->timer.tv_usec = interval % MILLION;
610 if (ifinfo->probes < MAX_RTR_SOLICITATIONS)
611 ifinfo->timer.tv_sec = RTR_SOLICITATION_INTERVAL;
614 * After sending MAX_RTR_SOLICITATIONS solicitations,
615 * we're just waiting for possible replies; there
616 * will be no more solicatation. Thus, we change
617 * the timer value to MAX_RTR_SOLICITATION_DELAY based
618 * on RFC 2461, Section 6.3.7.
620 ifinfo->timer.tv_sec = MAX_RTR_SOLICITATION_DELAY;
624 warnmsg(LOG_ERR, __FUNCTION__,
625 "illegal interface state(%d) on %s",
626 ifinfo->state, ifinfo->ifname);
630 /* reset the timer */
631 if (TIMEVAL_EQ(ifinfo->timer, tm_max)) {
632 ifinfo->expire = tm_max;
633 warnmsg(LOG_DEBUG, __FUNCTION__,
634 "stop timer for %s", ifinfo->ifname);
637 gettimeofday(&now, NULL);
638 TIMEVAL_ADD(&now, &ifinfo->timer, &ifinfo->expire);
641 warnmsg(LOG_DEBUG, __FUNCTION__,
642 "set timer for %s to %d:%d", ifinfo->ifname,
643 (int)ifinfo->timer.tv_sec,
644 (int)ifinfo->timer.tv_usec);
650 /* timer related utility functions */
651 #define MILLION 1000000
655 TIMEVAL_ADD(struct timeval *a, struct timeval *b, struct timeval *result)
659 if ((l = a->tv_usec + b->tv_usec) < MILLION) {
661 result->tv_sec = a->tv_sec + b->tv_sec;
664 result->tv_usec = l - MILLION;
665 result->tv_sec = a->tv_sec + b->tv_sec + 1;
671 * XXX: this function assumes that a >= b.
674 TIMEVAL_SUB(struct timeval *a, struct timeval *b, struct timeval *result)
678 if ((l = a->tv_usec - b->tv_usec) >= 0) {
680 result->tv_sec = a->tv_sec - b->tv_sec;
683 result->tv_usec = MILLION + l;
684 result->tv_sec = a->tv_sec - b->tv_sec - 1;
689 rtsold_set_dump_file()
695 usage(char *progname)
697 if (progname && progname[strlen(progname) - 1] != 'd') {
698 fprintf(stderr, "usage: rtsol [-dD] interfaces...\n");
699 fprintf(stderr, "usage: rtsol [-dD] -a\n");
701 fprintf(stderr, "usage: rtsold [-adDfm1] interfaces...\n");
702 fprintf(stderr, "usage: rtsold [-dDfm1] -a\n");
709 warnmsg(int priority, const char *func, const char *msg, ...)
711 warnmsg(priority, func, msg, va_alist)
723 if (priority <= log_upto) {
724 (void)vfprintf(stderr, msg, ap);
725 (void)fprintf(stderr, "\n");
728 snprintf(buf, sizeof(buf), "<%s> %s", func, msg);
730 vsyslog(priority, msg, ap);
738 #ifndef HAVE_GETIFADDRS
739 errx(1, "-a is not available with the configuration");
741 static char ifname[IFNAMSIZ + 1];
742 static char *argv[2];
743 struct ifaddrs *ifap, *ifa, *target;
745 if (getifaddrs(&ifap) != 0)
749 /* find an ethernet */
750 for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
751 if ((ifa->ifa_flags & IFF_UP) == 0)
753 if ((ifa->ifa_flags & IFF_POINTOPOINT) != 0)
755 if ((ifa->ifa_flags & IFF_LOOPBACK) != 0)
757 if ((ifa->ifa_flags & IFF_MULTICAST) == 0)
760 if (ifa->ifa_addr->sa_family != AF_INET6)
763 if (target && strcmp(target->ifa_name, ifa->ifa_name) == 0)
769 /* if we find multiple candidates, failure. */
771 warnx("multiple interfaces found");
778 strncpy(ifname, target->ifa_name, sizeof(ifname) - 1);
779 ifname[sizeof(ifname) - 1] = '\0';
784 warnx("probing %s", argv[0]);
790 return (char **)NULL;