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 #define RTSOL_DUMPFILE "/var/run/rtsold.dump";
67 #define RTSOL_PIDFILE "/var/run/rtsold.pid";
69 struct ifinfo *iflist;
70 struct timeval tm_max = {0x7fffffff, 0x7fffffff};
71 static int log_upto = 999;
74 int Fflag = 0; /* force setting sysctl parameters */
78 const char *otherconf_script;
79 const char *resolvconf_script = "/sbin/resolvconf";
81 /* protocol constants */
82 #define MAX_RTR_SOLICITATION_DELAY 1 /* second */
83 #define RTR_SOLICITATION_INTERVAL 4 /* seconds */
84 #define MAX_RTR_SOLICITATIONS 3 /* times */
87 * implementation dependent constants in seconds
88 * XXX: should be configurable
90 #define PROBE_INTERVAL 60
92 /* static variables and functions */
93 static int mobile_node = 0;
94 static const char *pidfilename = RTSOL_PIDFILE;
98 static const char *dumpfilename = RTSOL_DUMPFILE;
102 static int ifreconfig(char *);
105 static int make_packet(struct ifinfo *);
106 static struct timeval *rtsol_check_timer(void);
109 static void rtsold_set_dump_file(int);
111 static void usage(void);
114 main(int argc, char **argv)
117 struct timeval *timeout;
120 struct pollfd set[2];
122 fd_set *fdsetp, *selectfdp;
130 opts = "adDfFm1O:P:R:";
137 while ((ch = getopt(argc, argv, opts)) != -1) {
161 otherconf_script = optarg;
164 pidfilename = optarg;
167 resolvconf_script = optarg;
177 if ((!aflag && argc == 0) || (aflag && argc != 0)) {
184 log_upto = LOG_NOTICE;
188 ident = strrchr(argv[0], '/');
193 openlog(ident, LOG_NDELAY|LOG_PID, LOG_DAEMON);
195 setlogmask(LOG_UPTO(log_upto));
198 if (otherconf_script && *otherconf_script != '/') {
199 errx(1, "configuration script (%s) must be an absolute path",
202 if (resolvconf_script && *resolvconf_script != '/') {
203 errx(1, "configuration script (%s) must be an absolute path",
206 if (pidfilename && *pidfilename != '/') {
207 errx(1, "pid filename (%s) must be an absolute path",
210 #ifndef HAVE_ARC4RANDOM
211 /* random value initialization */
212 srandom((u_long)time(NULL));
216 setinet6sysctl(IPV6CTL_FORWARDING, 0);
218 /* warn if forwarding is up */
219 if (getinet6sysctl(IPV6CTL_FORWARDING))
220 warnx("kernel is configured as a router, not a host");
224 /* initialization to dump internal status to a file */
225 signal(SIGUSR1, rtsold_set_dump_file);
229 daemon(0, 0); /* act as a daemon */
232 * Open a socket for sending RS and receiving RA.
233 * This should be done before calling ifinit(), since the function
236 if ((s = sockopen()) < 0) {
237 warnmsg(LOG_ERR, __func__, "failed to open a socket");
242 set[0].events = POLLIN;
251 if ((rtsock = rtsock_open()) < 0) {
252 warnmsg(LOG_ERR, __func__, "failed to open a socket");
257 set[1].events = POLLIN;
264 fdmasks = howmany(maxfd + 1, NFDBITS) * sizeof(fd_mask);
265 if ((fdsetp = malloc(fdmasks)) == NULL) {
266 warnmsg(LOG_ERR, __func__, "malloc");
269 if ((selectfdp = malloc(fdmasks)) == NULL) {
270 warnmsg(LOG_ERR, __func__, "malloc");
275 /* configuration per interface */
277 warnmsg(LOG_ERR, __func__,
278 "failed to initialize interfaces");
282 argv = autoifprobe();
283 while (argv && *argv) {
284 if (ifconfig(*argv)) {
285 warnmsg(LOG_ERR, __func__,
286 "failed to initialize %s", *argv);
292 /* setup for probing default routers */
294 warnmsg(LOG_ERR, __func__,
295 "failed to setup for probing routers");
300 /* dump the current pid */
302 pid_t pid = getpid();
305 if ((fp = fopen(pidfilename, "w")) == NULL)
306 warnmsg(LOG_ERR, __func__,
307 "failed to open a pid log file(%s): %s",
308 pidfilename, strerror(errno));
310 fprintf(fp, "%d\n", pid);
315 memset(fdsetp, 0, fdmasks);
317 FD_SET(rtsock, fdsetp);
319 while (1) { /* main loop */
323 memcpy(selectfdp, fdsetp, fdmasks);
327 if (do_dump) { /* SIGUSR1 */
329 rtsold_dump_file(dumpfilename);
333 timeout = rtsol_check_timer();
338 /* if we have no timeout, we are done (or failed) */
342 /* if all interfaces have got RA packet, we are done */
343 TAILQ_FOREACH(ifi, &ifinfo_head, ifi_next) {
344 if (ifi->state != IFS_DOWN && ifi->racnt == 0)
351 e = poll(set, 2, timeout ? (timeout->tv_sec * 1000 + timeout->tv_usec / 1000) : INFTIM);
353 e = select(maxfd + 1, selectfdp, NULL, NULL, timeout);
356 if (e < 0 && errno != EINTR) {
357 warnmsg(LOG_ERR, __func__, "select: %s",
363 /* packet reception */
365 if (set[1].revents & POLLIN)
367 if (FD_ISSET(rtsock, selectfdp))
369 rtsock_input(rtsock);
371 if (set[0].revents & POLLIN)
373 if (FD_ISSET(s, selectfdp))
383 ifconfig(char *ifname)
386 struct sockaddr_dl *sdl;
389 if ((sdl = if_nametosdl(ifname)) == NULL) {
390 warnmsg(LOG_ERR, __func__,
391 "failed to get link layer information for %s", ifname);
394 if (find_ifinfo(sdl->sdl_index)) {
395 warnmsg(LOG_ERR, __func__,
396 "interface %s was already configured", ifname);
401 if ((ifi = malloc(sizeof(*ifi))) == NULL) {
402 warnmsg(LOG_ERR, __func__, "memory allocation failed");
406 memset(ifi, 0, sizeof(*ifi));
409 strlcpy(ifi->ifname, ifname, sizeof(ifi->ifname));
411 /* construct a router solicitation message */
412 if (make_packet(ifi))
415 /* set link ID of this interface. */
417 if (inet_zoneid(AF_INET6, 2, ifname, &ifi->linkid))
420 /* XXX: assume interface IDs as link IDs */
421 ifi->linkid = ifi->sdl->sdl_index;
425 * check if the interface is available.
426 * also check if SIOCGIFMEDIA ioctl is OK on the interface.
429 ifi->active = interface_status(ifi);
430 if (!ifi->mediareqok) {
432 * probe routers periodically even if the link status
435 ifi->probeinterval = PROBE_INTERVAL;
438 /* activate interface: interface_up returns 0 on success */
439 flags = interface_up(ifi->ifname);
441 ifi->state = IFS_DELAY;
442 else if (flags == IFS_TENTATIVE)
443 ifi->state = IFS_TENTATIVE;
445 ifi->state = IFS_DOWN;
447 rtsol_timer_update(ifi);
449 TAILQ_INSERT_TAIL(&ifinfo_head, ifi, ifi_next);
462 struct ifinfo *ifi_tmp;
464 ifi = TAILQ_FIRST(&ifinfo_head);
465 while (ifi != NULL) {
466 if (ifi->sdl != NULL)
468 if (ifi->rs_data != NULL)
470 ifi_tmp = TAILQ_NEXT(ifi, ifi_next);
478 ifreconfig(char *ifname)
480 struct ifinfo *ifi, *prev;
484 TAILQ_FOREACH(ifi, &ifinfo_head, ifi_next) {
485 if (strncmp(ifi->ifname, ifname, sizeof(ifi->ifname)) == 0)
489 prev->next = ifi->next;
491 rv = ifconfig(ifname);
493 /* reclaim it after ifconfig() in case ifname is pointer inside ifi */
504 find_ifinfo(int ifindex)
508 TAILQ_FOREACH(ifi, &ifinfo_head, ifi_next) {
509 if (ifi->sdl->sdl_index == ifindex)
516 make_packet(struct ifinfo *ifi)
518 size_t packlen = sizeof(struct nd_router_solicit), lladdroptlen = 0;
519 struct nd_router_solicit *rs;
522 if ((lladdroptlen = lladdropt_length(ifi->sdl)) == 0) {
523 warnmsg(LOG_INFO, __func__,
524 "link-layer address option has null length"
525 " on %s. Treat as not included.", ifi->ifname);
527 packlen += lladdroptlen;
528 ifi->rs_datalen = packlen;
530 /* allocate buffer */
531 if ((buf = malloc(packlen)) == NULL) {
532 warnmsg(LOG_ERR, __func__,
533 "memory allocation failed for %s", ifi->ifname);
538 /* fill in the message */
539 rs = (struct nd_router_solicit *)buf;
540 rs->nd_rs_type = ND_ROUTER_SOLICIT;
543 rs->nd_rs_reserved = 0;
546 /* fill in source link-layer address option */
548 lladdropt_fill(ifi->sdl, (struct nd_opt_hdr *)buf);
553 static struct timeval *
554 rtsol_check_timer(void)
556 static struct timeval returnval;
557 struct timeval now, rtsol_timer;
561 gettimeofday(&now, NULL);
563 rtsol_timer = tm_max;
565 TAILQ_FOREACH(ifi, &ifinfo_head, ifi_next) {
566 if (timercmp(&ifi->expire, &now, <=)) {
568 warnmsg(LOG_DEBUG, __func__,
569 "timer expiration on %s, "
570 "state = %d", ifi->ifname,
573 switch (ifi->state) {
576 /* interface_up returns 0 on success */
577 flags = interface_up(ifi->ifname);
579 ifi->state = IFS_DELAY;
580 else if (flags == IFS_TENTATIVE)
581 ifi->state = IFS_TENTATIVE;
583 ifi->state = IFS_DOWN;
587 int oldstatus = ifi->active;
590 ifi->active = interface_status(ifi);
592 if (oldstatus != ifi->active) {
593 warnmsg(LOG_DEBUG, __func__,
594 "%s status is changed"
597 oldstatus, ifi->active);
599 ifi->state = IFS_DELAY;
600 } else if (ifi->probeinterval &&
602 ifi->timer.tv_sec) <= 0) {
603 /* probe timer expired */
607 ifi->state = IFS_PROBE;
611 * If we need a probe, clear the previous
612 * status wrt the "other" configuration.
615 ifi->otherconfig = 0;
617 if (probe && mobile_node)
618 defrouter_probe(ifi);
622 ifi->state = IFS_PROBE;
626 if (ifi->probes < MAX_RTR_SOLICITATIONS)
629 warnmsg(LOG_INFO, __func__,
630 "No answer after sending %d RSs",
633 ifi->state = IFS_IDLE;
637 rtsol_timer_update(ifi);
640 if (timercmp(&ifi->expire, &rtsol_timer, <))
641 rtsol_timer = ifi->expire;
644 if (timercmp(&rtsol_timer, &tm_max, ==)) {
645 warnmsg(LOG_DEBUG, __func__, "there is no timer");
647 } else if (timercmp(&rtsol_timer, &now, <))
648 /* this may occur when the interval is too small */
649 returnval.tv_sec = returnval.tv_usec = 0;
651 timersub(&rtsol_timer, &now, &returnval);
654 warnmsg(LOG_DEBUG, __func__, "New timer is %ld:%08ld",
655 (long)returnval.tv_sec, (long)returnval.tv_usec);
661 rtsol_timer_update(struct ifinfo *ifi)
663 #define MILLION 1000000
664 #define DADRETRY 10 /* XXX: adhoc */
668 bzero(&ifi->timer, sizeof(ifi->timer));
670 switch (ifi->state) {
673 if (++ifi->dadcount > DADRETRY) {
675 ifi->timer.tv_sec = PROBE_INTERVAL;
677 ifi->timer.tv_sec = 1;
681 /* XXX should be configurable */
682 ifi->timer.tv_sec = 3;
685 ifi->timer = tm_max; /* stop timer(valid?) */
688 #ifndef HAVE_ARC4RANDOM
689 interval = random() % (MAX_RTR_SOLICITATION_DELAY * MILLION);
691 interval = arc4random_uniform(MAX_RTR_SOLICITATION_DELAY * MILLION);
693 ifi->timer.tv_sec = interval / MILLION;
694 ifi->timer.tv_usec = interval % MILLION;
697 if (ifi->probes < MAX_RTR_SOLICITATIONS)
698 ifi->timer.tv_sec = RTR_SOLICITATION_INTERVAL;
701 * After sending MAX_RTR_SOLICITATIONS solicitations,
702 * we're just waiting for possible replies; there
703 * will be no more solicitation. Thus, we change
704 * the timer value to MAX_RTR_SOLICITATION_DELAY based
705 * on RFC 2461, Section 6.3.7.
707 ifi->timer.tv_sec = MAX_RTR_SOLICITATION_DELAY;
711 warnmsg(LOG_ERR, __func__,
712 "illegal interface state(%d) on %s",
713 ifi->state, ifi->ifname);
717 /* reset the timer */
718 if (timercmp(&ifi->timer, &tm_max, ==)) {
719 ifi->expire = tm_max;
720 warnmsg(LOG_DEBUG, __func__,
721 "stop timer for %s", ifi->ifname);
723 gettimeofday(&now, NULL);
724 timeradd(&now, &ifi->timer, &ifi->expire);
727 warnmsg(LOG_DEBUG, __func__,
728 "set timer for %s to %d:%d", ifi->ifname,
729 (int)ifi->timer.tv_sec,
730 (int)ifi->timer.tv_usec);
736 /* timer related utility functions */
737 #define MILLION 1000000
741 rtsold_set_dump_file(int sig __unused)
751 fprintf(stderr, "usage: rtsold [-adDfFm1] [-O script-name] [-P pidfile] [-R script-name] interfaces...\n");
752 fprintf(stderr, "usage: rtsold [-dDfFm1] [-O script-name] [-P pidfile] [-R script-name] -a\n");
754 fprintf(stderr, "usage: rtsol [-dDF] [-O script-name] [-P pidfile] [-R script-name] interfaces...\n");
755 fprintf(stderr, "usage: rtsol [-dDF] [-O script-name] [-P pidfile] [-R script-name] -a\n");
760 warnmsg(int priority, const char *func, const char *msg, ...)
767 if (priority <= log_upto) {
768 (void)vfprintf(stderr, msg, ap);
769 (void)fprintf(stderr, "\n");
772 snprintf(buf, sizeof(buf), "<%s> %s", func, msg);
774 vsyslog(priority, msg, ap);
780 * return a list of interfaces which is suitable to sending an RS.
785 static char **argv = NULL;
789 struct ifaddrs *ifap, *ifa, *target;
790 struct in6_ndireq nd;
801 if (getifaddrs(&ifap) != 0)
804 if (!Fflag && (s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
805 warnmsg(LOG_ERR, __func__, "socket");
810 /* find an ethernet */
811 for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
812 if ((ifa->ifa_flags & IFF_UP) == 0)
814 if ((ifa->ifa_flags & IFF_POINTOPOINT) != 0)
816 if ((ifa->ifa_flags & IFF_LOOPBACK) != 0)
818 if ((ifa->ifa_flags & IFF_MULTICAST) == 0)
821 if (ifa->ifa_addr->sa_family != AF_INET6)
825 for (i = 0; i < n; i++) {
826 if (strcmp(argv[i], ifa->ifa_name) == 0) {
835 * Skip the interfaces which IPv6 and/or accepting RA
839 memset(&nd, 0, sizeof(nd));
840 strlcpy(nd.ifname, ifa->ifa_name, sizeof(nd.ifname));
841 if (ioctl(s, SIOCGIFINFO_IN6, (caddr_t)&nd) < 0) {
842 warnmsg(LOG_ERR, __func__,
843 "ioctl(SIOCGIFINFO_IN6)");
846 if ((nd.ndi.flags & ND6_IFF_IFDISABLED))
848 if (!(nd.ndi.flags & ND6_IFF_ACCEPT_RTADV))
852 /* if we find multiple candidates, just warn. */
853 if (n != 0 && dflag > 1)
854 warnmsg(LOG_WARNING, __func__,
855 "multiple interfaces found");
857 a = (char **)realloc(argv, (n + 1) * sizeof(char **));
859 warnmsg(LOG_ERR, __func__, "realloc");
863 argv[n] = strdup(ifa->ifa_name);
865 warnmsg(LOG_ERR, __func__, "malloc");
872 a = (char **)realloc(argv, (n + 1) * sizeof(char **));
874 warnmsg(LOG_ERR, __func__, "realloc");
881 for (i = 0; i < n; i++)
882 warnmsg(LOG_WARNING, __func__, "probing %s",