1 /* $KAME: rtsold.c,v 1.67 2003/05/17 18:16:15 itojun 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>
35 #include <sys/ioctl.h>
37 #include <sys/socket.h>
38 #include <sys/param.h>
41 #include <net/if_dl.h>
42 #include <net/if_var.h>
44 #include <netinet/in.h>
45 #include <netinet/icmp6.h>
46 #include <netinet/in_var.h>
48 #include <netinet6/nd6.h>
66 struct ifinfo *iflist;
67 struct timeval tm_max = {0x7fffffff, 0x7fffffff};
68 static int log_upto = 999;
71 int Fflag = 0; /* force setting sysctl parameters */
75 char *otherconf_script;
77 /* protocol constants */
78 #define MAX_RTR_SOLICITATION_DELAY 1 /* second */
79 #define RTR_SOLICITATION_INTERVAL 4 /* seconds */
80 #define MAX_RTR_SOLICITATIONS 3 /* times */
83 * implementation dependent constants in seconds
84 * XXX: should be configurable
86 #define PROBE_INTERVAL 60
88 int main(int, char **);
90 /* static variables and functions */
91 static int mobile_node = 0;
94 static const char *dumpfilename = "/var/run/rtsold.dump"; /* XXX: should be configurable */
97 static const char *pidfilename = "/var/run/rtsold.pid"; /* should be configurable */
101 static int ifreconfig(char *);
103 static int make_packet(struct ifinfo *);
104 static struct timeval *rtsol_check_timer(void);
107 static void rtsold_set_dump_file(int);
109 static void usage(char *);
112 main(int argc, char **argv)
115 struct timeval *timeout;
119 struct pollfd set[2];
121 fd_set *fdsetp, *selectfdp;
133 if (argv0 && argv0[strlen(argv0) - 1] != 'd') {
140 while ((ch = getopt(argc, argv, opts)) != -1) {
164 otherconf_script = optarg;
174 if ((!aflag && argc == 0) || (aflag && argc != 0)) {
181 log_upto = LOG_NOTICE;
185 ident = strrchr(argv0, '/');
190 openlog(ident, LOG_NDELAY|LOG_PID, LOG_DAEMON);
192 setlogmask(LOG_UPTO(log_upto));
195 if (otherconf_script && *otherconf_script != '/') {
196 errx(1, "configuration script (%s) must be an absolute path",
200 #ifndef HAVE_ARC4RANDOM
201 /* random value initialization */
202 srandom((u_long)time(NULL));
206 setinet6sysctl(IPV6CTL_FORWARDING, 0);
208 /* warn if forwarding is up */
209 if (getinet6sysctl(IPV6CTL_FORWARDING))
210 warnx("kernel is configured as a router, not a host");
214 /* initialization to dump internal status to a file */
215 signal(SIGUSR1, rtsold_set_dump_file);
219 daemon(0, 0); /* act as a daemon */
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 warnmsg(LOG_ERR, __func__, "failed to open a socket");
233 set[0].events = POLLIN;
242 if ((rtsock = rtsock_open()) < 0) {
243 warnmsg(LOG_ERR, __func__, "failed to open a socket");
249 set[1].events = POLLIN;
256 fdmasks = howmany(maxfd + 1, NFDBITS) * sizeof(fd_mask);
257 if ((fdsetp = malloc(fdmasks)) == NULL) {
261 if ((selectfdp = malloc(fdmasks)) == NULL) {
267 /* configuration per interface */
269 warnmsg(LOG_ERR, __func__,
270 "failed to initialize interfaces");
275 argv = autoifprobe();
276 while (argv && *argv) {
277 if (ifconfig(*argv)) {
278 warnmsg(LOG_ERR, __func__,
279 "failed to initialize %s", *argv);
286 /* setup for probing default routers */
288 warnmsg(LOG_ERR, __func__,
289 "failed to setup for probing routers");
295 /* dump the current pid */
297 pid_t pid = getpid();
300 if ((fp = fopen(pidfilename, "w")) == NULL)
301 warnmsg(LOG_ERR, __func__,
302 "failed to open a pid log file(%s): %s",
303 pidfilename, strerror(errno));
305 fprintf(fp, "%d\n", pid);
312 memset(fdsetp, 0, fdmasks);
314 FD_SET(rtsock, fdsetp);
316 while (1) { /* main loop */
320 memcpy(selectfdp, fdsetp, fdmasks);
324 if (do_dump) { /* SIGUSR1 */
326 rtsold_dump_file(dumpfilename);
330 timeout = rtsol_check_timer();
335 /* if we have no timeout, we are done (or failed) */
339 /* if all interfaces have got RA packet, we are done */
340 for (ifi = iflist; ifi; ifi = ifi->next) {
341 if (ifi->state != IFS_DOWN && ifi->racnt == 0)
348 e = poll(set, 2, timeout ? (timeout->tv_sec * 1000 + timeout->tv_usec / 1000) : INFTIM);
350 e = select(maxfd + 1, selectfdp, NULL, NULL, timeout);
353 if (e < 0 && errno != EINTR) {
354 warnmsg(LOG_ERR, __func__, "select: %s",
360 /* packet reception */
362 if (set[1].revents & POLLIN)
364 if (FD_ISSET(rtsock, selectfdp))
366 rtsock_input(rtsock);
368 if (set[0].revents & POLLIN)
370 if (FD_ISSET(s, selectfdp))
380 ifconfig(char *ifname)
382 struct ifinfo *ifinfo;
383 struct sockaddr_dl *sdl;
386 if ((sdl = if_nametosdl(ifname)) == NULL) {
387 warnmsg(LOG_ERR, __func__,
388 "failed to get link layer information for %s", ifname);
391 if (find_ifinfo(sdl->sdl_index)) {
392 warnmsg(LOG_ERR, __func__,
393 "interface %s was already configured", ifname);
398 if ((ifinfo = malloc(sizeof(*ifinfo))) == NULL) {
399 warnmsg(LOG_ERR, __func__, "memory allocation failed");
403 memset(ifinfo, 0, sizeof(*ifinfo));
406 strlcpy(ifinfo->ifname, ifname, sizeof(ifinfo->ifname));
408 /* construct a router solicitation message */
409 if (make_packet(ifinfo))
412 /* set link ID of this interface. */
414 if (inet_zoneid(AF_INET6, 2, ifname, &ifinfo->linkid))
417 /* XXX: assume interface IDs as link IDs */
418 ifinfo->linkid = ifinfo->sdl->sdl_index;
422 * check if the interface is available.
423 * also check if SIOCGIFMEDIA ioctl is OK on the interface.
425 ifinfo->mediareqok = 1;
426 ifinfo->active = interface_status(ifinfo);
427 if (!ifinfo->mediareqok) {
429 * probe routers periodically even if the link status
432 ifinfo->probeinterval = PROBE_INTERVAL;
435 /* activate interface: interface_up returns 0 on success */
436 flags = interface_up(ifinfo->ifname);
438 ifinfo->state = IFS_DELAY;
439 else if (flags == IFS_TENTATIVE)
440 ifinfo->state = IFS_TENTATIVE;
442 ifinfo->state = IFS_DOWN;
444 rtsol_timer_update(ifinfo);
446 /* link into chain */
448 ifinfo->next = iflist;
462 struct ifinfo *ifi, *next;
464 for (ifi = iflist; ifi; ifi = next) {
477 ifreconfig(char *ifname)
479 struct ifinfo *ifi, *prev;
483 for (ifi = iflist; ifi; ifi = ifi->next) {
484 if (strncmp(ifi->ifname, ifname, sizeof(ifi->ifname)) == 0)
488 prev->next = ifi->next;
490 rv = ifconfig(ifname);
492 /* reclaim it after ifconfig() in case ifname is pointer inside ifi */
502 find_ifinfo(int ifindex)
506 for (ifi = iflist; ifi; ifi = ifi->next)
507 if (ifi->sdl->sdl_index == ifindex)
513 make_packet(struct ifinfo *ifinfo)
515 size_t packlen = sizeof(struct nd_router_solicit), lladdroptlen = 0;
516 struct nd_router_solicit *rs;
519 if ((lladdroptlen = lladdropt_length(ifinfo->sdl)) == 0) {
520 warnmsg(LOG_INFO, __func__,
521 "link-layer address option has null length"
522 " on %s. Treat as not included.", ifinfo->ifname);
524 packlen += lladdroptlen;
525 ifinfo->rs_datalen = packlen;
527 /* allocate buffer */
528 if ((buf = malloc(packlen)) == NULL) {
529 warnmsg(LOG_ERR, __func__,
530 "memory allocation failed for %s", ifinfo->ifname);
533 ifinfo->rs_data = buf;
535 /* fill in the message */
536 rs = (struct nd_router_solicit *)buf;
537 rs->nd_rs_type = ND_ROUTER_SOLICIT;
540 rs->nd_rs_reserved = 0;
543 /* fill in source link-layer address option */
545 lladdropt_fill(ifinfo->sdl, (struct nd_opt_hdr *)buf);
550 static struct timeval *
551 rtsol_check_timer(void)
553 static struct timeval returnval;
554 struct timeval now, rtsol_timer;
555 struct ifinfo *ifinfo;
558 gettimeofday(&now, NULL);
560 rtsol_timer = tm_max;
562 for (ifinfo = iflist; ifinfo; ifinfo = ifinfo->next) {
563 if (timercmp(&ifinfo->expire, &now, <=)) {
565 warnmsg(LOG_DEBUG, __func__,
566 "timer expiration on %s, "
567 "state = %d", ifinfo->ifname,
570 switch (ifinfo->state) {
573 /* interface_up returns 0 on success */
574 flags = interface_up(ifinfo->ifname);
576 ifinfo->state = IFS_DELAY;
577 else if (flags == IFS_TENTATIVE)
578 ifinfo->state = IFS_TENTATIVE;
580 ifinfo->state = IFS_DOWN;
584 int oldstatus = ifinfo->active;
587 ifinfo->active = interface_status(ifinfo);
589 if (oldstatus != ifinfo->active) {
590 warnmsg(LOG_DEBUG, __func__,
591 "%s status is changed"
594 oldstatus, ifinfo->active);
596 ifinfo->state = IFS_DELAY;
597 } else if (ifinfo->probeinterval &&
598 (ifinfo->probetimer -=
599 ifinfo->timer.tv_sec) <= 0) {
600 /* probe timer expired */
602 ifinfo->probeinterval;
604 ifinfo->state = IFS_PROBE;
608 * If we need a probe, clear the previous
609 * status wrt the "other" configuration.
612 ifinfo->otherconfig = 0;
614 if (probe && mobile_node)
615 defrouter_probe(ifinfo);
619 ifinfo->state = IFS_PROBE;
623 if (ifinfo->probes < MAX_RTR_SOLICITATIONS)
626 warnmsg(LOG_INFO, __func__,
627 "No answer after sending %d RSs",
630 ifinfo->state = IFS_IDLE;
634 rtsol_timer_update(ifinfo);
637 if (timercmp(&ifinfo->expire, &rtsol_timer, <))
638 rtsol_timer = ifinfo->expire;
641 if (timercmp(&rtsol_timer, &tm_max, ==)) {
642 warnmsg(LOG_DEBUG, __func__, "there is no timer");
644 } else if (timercmp(&rtsol_timer, &now, <))
645 /* this may occur when the interval is too small */
646 returnval.tv_sec = returnval.tv_usec = 0;
648 timersub(&rtsol_timer, &now, &returnval);
651 warnmsg(LOG_DEBUG, __func__, "New timer is %ld:%08ld",
652 (long)returnval.tv_sec, (long)returnval.tv_usec);
658 rtsol_timer_update(struct ifinfo *ifinfo)
660 #define MILLION 1000000
661 #define DADRETRY 10 /* XXX: adhoc */
665 bzero(&ifinfo->timer, sizeof(ifinfo->timer));
667 switch (ifinfo->state) {
670 if (++ifinfo->dadcount > DADRETRY) {
671 ifinfo->dadcount = 0;
672 ifinfo->timer.tv_sec = PROBE_INTERVAL;
674 ifinfo->timer.tv_sec = 1;
678 /* XXX should be configurable */
679 ifinfo->timer.tv_sec = 3;
682 ifinfo->timer = tm_max; /* stop timer(valid?) */
685 #ifndef HAVE_ARC4RANDOM
686 interval = random() % (MAX_RTR_SOLICITATION_DELAY * MILLION);
688 interval = arc4random_uniform(MAX_RTR_SOLICITATION_DELAY * MILLION);
690 ifinfo->timer.tv_sec = interval / MILLION;
691 ifinfo->timer.tv_usec = interval % MILLION;
694 if (ifinfo->probes < MAX_RTR_SOLICITATIONS)
695 ifinfo->timer.tv_sec = RTR_SOLICITATION_INTERVAL;
698 * After sending MAX_RTR_SOLICITATIONS solicitations,
699 * we're just waiting for possible replies; there
700 * will be no more solicitation. Thus, we change
701 * the timer value to MAX_RTR_SOLICITATION_DELAY based
702 * on RFC 2461, Section 6.3.7.
704 ifinfo->timer.tv_sec = MAX_RTR_SOLICITATION_DELAY;
708 warnmsg(LOG_ERR, __func__,
709 "illegal interface state(%d) on %s",
710 ifinfo->state, ifinfo->ifname);
714 /* reset the timer */
715 if (timercmp(&ifinfo->timer, &tm_max, ==)) {
716 ifinfo->expire = tm_max;
717 warnmsg(LOG_DEBUG, __func__,
718 "stop timer for %s", ifinfo->ifname);
720 gettimeofday(&now, NULL);
721 timeradd(&now, &ifinfo->timer, &ifinfo->expire);
724 warnmsg(LOG_DEBUG, __func__,
725 "set timer for %s to %d:%d", ifinfo->ifname,
726 (int)ifinfo->timer.tv_sec,
727 (int)ifinfo->timer.tv_usec);
733 /* timer related utility functions */
734 #define MILLION 1000000
738 rtsold_set_dump_file(int sig __unused)
745 usage(char *progname)
747 if (progname && progname[strlen(progname) - 1] != 'd') {
748 fprintf(stderr, "usage: rtsol [-dDF] interfaces...\n");
749 fprintf(stderr, "usage: rtsol [-dDF] -a\n");
751 fprintf(stderr, "usage: rtsold [-adDfFm1] interfaces...\n");
752 fprintf(stderr, "usage: rtsold [-dDfFm1] -a\n");
759 warnmsg(int priority, const char *func, const char *msg, ...)
761 warnmsg(priority, func, msg, va_alist)
773 if (priority <= log_upto) {
774 (void)vfprintf(stderr, msg, ap);
775 (void)fprintf(stderr, "\n");
778 snprintf(buf, sizeof(buf), "<%s> %s", func, msg);
780 vsyslog(priority, msg, ap);
786 * return a list of interfaces which is suitable to sending an RS.
791 static char **argv = NULL;
795 struct ifaddrs *ifap, *ifa, *target;
796 struct in6_ndireq nd;
807 if (getifaddrs(&ifap) != 0)
810 if (!Fflag && (s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
816 /* find an ethernet */
817 for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
818 if ((ifa->ifa_flags & IFF_UP) == 0)
820 if ((ifa->ifa_flags & IFF_POINTOPOINT) != 0)
822 if ((ifa->ifa_flags & IFF_LOOPBACK) != 0)
824 if ((ifa->ifa_flags & IFF_MULTICAST) == 0)
827 if (ifa->ifa_addr->sa_family != AF_INET6)
831 for (i = 0; i < n; i++) {
832 if (strcmp(argv[i], ifa->ifa_name) == 0) {
841 * Skip the interfaces which IPv6 and/or accepting RA
845 memset(&nd, 0, sizeof(nd));
846 strlcpy(nd.ifname, ifa->ifa_name, sizeof(nd.ifname));
847 if (ioctl(s, SIOCGIFINFO_IN6, (caddr_t)&nd) < 0) {
848 err(1, "ioctl(SIOCGIFINFO_IN6)");
851 if ((nd.ndi.flags & ND6_IFF_IFDISABLED))
853 if (!(nd.ndi.flags & ND6_IFF_ACCEPT_RTADV))
857 /* if we find multiple candidates, just warn. */
858 if (n != 0 && dflag > 1)
859 warnx("multiple interfaces found");
861 a = (char **)realloc(argv, (n + 1) * sizeof(char **));
865 argv[n] = strdup(ifa->ifa_name);
872 a = (char **)realloc(argv, (n + 1) * sizeof(char **));
879 for (i = 0; i < n; i++)
880 warnx("probing %s", argv[i]);