1 /* $KAME: rtsold.c,v 1.67 2003/05/17 18:16:15 itojun Exp $ */
4 * SPDX-License-Identifier: BSD-3-Clause
6 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the project nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 #include <sys/param.h>
37 #include <sys/ioctl.h>
38 #include <sys/socket.h>
41 #include <net/if_dl.h>
43 #include <netinet/in.h>
44 #include <netinet/icmp6.h>
45 #include <netinet/in_var.h>
46 #include <arpa/inet.h>
48 #include <netinet6/nd6.h>
65 #define RTSOL_DUMPFILE "/var/run/rtsold.dump";
66 #define RTSOL_PIDFILE "/var/run/rtsold.pid";
68 struct timespec tm_max;
69 static int log_upto = 999;
72 int Fflag = 0; /* force setting sysctl parameters */
77 const char *otherconf_script;
78 const char *resolvconf_script = "/sbin/resolvconf";
80 /* protocol constants */
81 #define MAX_RTR_SOLICITATION_DELAY 1 /* second */
82 #define RTR_SOLICITATION_INTERVAL 4 /* seconds */
83 #define MAX_RTR_SOLICITATIONS 3 /* times */
86 * implementation dependent constants in seconds
87 * XXX: should be configurable
89 #define PROBE_INTERVAL 60
91 /* static variables and functions */
92 static int mobile_node = 0;
93 static const char *pidfilename = RTSOL_PIDFILE;
97 static const char *dumpfilename = RTSOL_DUMPFILE;
100 static char **autoifprobe(void);
101 static int ifconfig(char *ifname);
102 static int make_packet(struct ifinfo *);
103 static struct timespec *rtsol_check_timer(void);
106 static void rtsold_set_dump_file(int);
108 static void usage(void);
111 main(int argc, char **argv)
114 struct timespec *timeout;
116 struct pollfd set[2];
122 opts = "adDfFm1O:p:R:u";
131 while ((ch = getopt(argc, argv, opts)) != -1) {
155 otherconf_script = optarg;
158 pidfilename = optarg;
161 resolvconf_script = optarg;
174 if ((!aflag && argc == 0) || (aflag && argc != 0)) {
179 /* Generate maximum time in timespec. */
180 tm_max.tv_sec = (-1) & ~((time_t)1 << ((sizeof(tm_max.tv_sec) * 8) - 1));
181 tm_max.tv_nsec = (-1) & ~((long)1 << ((sizeof(tm_max.tv_nsec) * 8) - 1));
185 log_upto = LOG_DEBUG;
189 log_upto = LOG_NOTICE;
194 ident = strrchr(argv0, '/');
199 openlog(ident, LOG_NDELAY|LOG_PID, LOG_DAEMON);
201 setlogmask(LOG_UPTO(log_upto));
204 if (otherconf_script && *otherconf_script != '/') {
205 errx(1, "configuration script (%s) must be an absolute path",
208 if (resolvconf_script && *resolvconf_script != '/') {
209 errx(1, "configuration script (%s) must be an absolute path",
212 if (pidfilename && *pidfilename != '/') {
213 errx(1, "pid filename (%s) must be an absolute path",
218 /* initialization to dump internal status to a file */
219 signal(SIGUSR1, rtsold_set_dump_file);
223 daemon(0, 0); /* act as a daemon */
226 * Open a socket for sending RS and receiving RA.
227 * This should be done before calling ifinit(), since the function
230 if ((s = sockopen()) < 0) {
231 warnmsg(LOG_ERR, __func__, "failed to open a socket");
235 set[0].events = POLLIN;
238 if ((rtsock = rtsock_open()) < 0) {
239 warnmsg(LOG_ERR, __func__, "failed to open a socket");
243 set[1].events = POLLIN;
245 /* configuration per interface */
247 warnmsg(LOG_ERR, __func__,
248 "failed to initialize interfaces");
252 argv = autoifprobe();
253 while (argv && *argv) {
254 if (ifconfig(*argv)) {
255 warnmsg(LOG_ERR, __func__,
256 "failed to initialize %s", *argv);
262 /* setup for probing default routers */
264 warnmsg(LOG_ERR, __func__,
265 "failed to setup for probing routers");
270 /* dump the current pid */
272 pid_t pid = getpid();
275 if ((fp = fopen(pidfilename, "w")) == NULL)
276 warnmsg(LOG_ERR, __func__,
277 "failed to open a pid log file(%s): %s",
278 pidfilename, strerror(errno));
280 fprintf(fp, "%d\n", pid);
284 while (1) { /* main loop */
287 if (do_dump) { /* SIGUSR1 */
289 rtsold_dump_file(dumpfilename);
293 timeout = rtsol_check_timer();
298 /* if we have no timeout, we are done (or failed) */
302 /* if all interfaces have got RA packet, we are done */
303 TAILQ_FOREACH(ifi, &ifinfo_head, ifi_next) {
304 if (ifi->state != IFS_DOWN && ifi->racnt == 0)
310 e = poll(set, 2, timeout ? (timeout->tv_sec * 1000 + timeout->tv_nsec / 1000 / 1000) : INFTIM);
312 if (e < 0 && errno != EINTR) {
313 warnmsg(LOG_ERR, __func__, "select: %s",
319 /* packet reception */
320 if (set[1].revents & POLLIN)
321 rtsock_input(rtsock);
322 if (set[0].revents & POLLIN)
331 ifconfig(char *ifname)
334 struct sockaddr_dl *sdl;
337 if ((sdl = if_nametosdl(ifname)) == NULL) {
338 warnmsg(LOG_ERR, __func__,
339 "failed to get link layer information for %s", ifname);
342 if (find_ifinfo(sdl->sdl_index)) {
343 warnmsg(LOG_ERR, __func__,
344 "interface %s was already configured", ifname);
350 struct in6_ndireq nd;
353 if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
354 warnmsg(LOG_ERR, __func__, "socket() failed.");
357 memset(&nd, 0, sizeof(nd));
358 strlcpy(nd.ifname, ifname, sizeof(nd.ifname));
359 if (ioctl(s, SIOCGIFINFO_IN6, (caddr_t)&nd) < 0) {
360 warnmsg(LOG_ERR, __func__,
361 "cannot get accept_rtadv flag");
365 nd.ndi.flags |= ND6_IFF_ACCEPT_RTADV;
366 if (ioctl(s, SIOCSIFINFO_IN6, (caddr_t)&nd) < 0) {
367 warnmsg(LOG_ERR, __func__,
368 "cannot set accept_rtadv flag");
375 if ((ifi = malloc(sizeof(*ifi))) == NULL) {
376 warnmsg(LOG_ERR, __func__, "memory allocation failed");
380 memset(ifi, 0, sizeof(*ifi));
382 ifi->ifi_rdnss = IFI_DNSOPT_STATE_NOINFO;
383 ifi->ifi_dnssl = IFI_DNSOPT_STATE_NOINFO;
384 TAILQ_INIT(&ifi->ifi_rainfo);
385 strlcpy(ifi->ifname, ifname, sizeof(ifi->ifname));
387 /* construct a router solicitation message */
388 if (make_packet(ifi))
391 /* set link ID of this interface. */
393 if (inet_zoneid(AF_INET6, 2, ifname, &ifi->linkid))
396 /* XXX: assume interface IDs as link IDs */
397 ifi->linkid = ifi->sdl->sdl_index;
401 * check if the interface is available.
402 * also check if SIOCGIFMEDIA ioctl is OK on the interface.
405 ifi->active = interface_status(ifi);
406 if (!ifi->mediareqok) {
408 * probe routers periodically even if the link status
411 ifi->probeinterval = PROBE_INTERVAL;
414 /* activate interface: interface_up returns 0 on success */
415 flags = interface_up(ifi->ifname);
417 ifi->state = IFS_DELAY;
418 else if (flags == IFS_TENTATIVE)
419 ifi->state = IFS_TENTATIVE;
421 ifi->state = IFS_DOWN;
423 rtsol_timer_update(ifi);
425 TAILQ_INSERT_TAIL(&ifinfo_head, ifi, ifi_next);
435 find_rainfo(struct ifinfo *ifi, struct sockaddr_in6 *sin6)
439 TAILQ_FOREACH(rai, &ifi->ifi_rainfo, rai_next)
440 if (memcmp(&rai->rai_saddr.sin6_addr, &sin6->sin6_addr,
441 sizeof(rai->rai_saddr.sin6_addr)) == 0)
448 find_ifinfo(int ifindex)
452 TAILQ_FOREACH(ifi, &ifinfo_head, ifi_next) {
453 if (ifi->sdl->sdl_index == ifindex)
460 make_packet(struct ifinfo *ifi)
462 size_t packlen = sizeof(struct nd_router_solicit), lladdroptlen = 0;
463 struct nd_router_solicit *rs;
466 if ((lladdroptlen = lladdropt_length(ifi->sdl)) == 0) {
467 warnmsg(LOG_INFO, __func__,
468 "link-layer address option has null length"
469 " on %s. Treat as not included.", ifi->ifname);
471 packlen += lladdroptlen;
472 ifi->rs_datalen = packlen;
474 /* allocate buffer */
475 if ((buf = malloc(packlen)) == NULL) {
476 warnmsg(LOG_ERR, __func__,
477 "memory allocation failed for %s", ifi->ifname);
482 /* fill in the message */
483 rs = (struct nd_router_solicit *)buf;
484 rs->nd_rs_type = ND_ROUTER_SOLICIT;
487 rs->nd_rs_reserved = 0;
490 /* fill in source link-layer address option */
492 lladdropt_fill(ifi->sdl, (struct nd_opt_hdr *)buf);
497 static struct timespec *
498 rtsol_check_timer(void)
500 static struct timespec returnval;
501 struct timespec now, rtsol_timer;
504 struct ra_opt *rao, *raotmp;
507 clock_gettime(CLOCK_MONOTONIC_FAST, &now);
509 rtsol_timer = tm_max;
511 TAILQ_FOREACH(ifi, &ifinfo_head, ifi_next) {
512 if (TS_CMP(&ifi->expire, &now, <=)) {
513 warnmsg(LOG_DEBUG, __func__, "timer expiration on %s, "
514 "state = %d", ifi->ifname, ifi->state);
516 while((rai = TAILQ_FIRST(&ifi->ifi_rainfo)) != NULL) {
517 /* Remove all RA options. */
518 TAILQ_REMOVE(&ifi->ifi_rainfo, rai, rai_next);
519 while ((rao = TAILQ_FIRST(&rai->rai_ra_opt)) !=
521 TAILQ_REMOVE(&rai->rai_ra_opt, rao,
523 if (rao->rao_msg != NULL)
529 switch (ifi->state) {
532 /* interface_up returns 0 on success */
533 flags = interface_up(ifi->ifname);
535 ifi->state = IFS_DELAY;
536 else if (flags == IFS_TENTATIVE)
537 ifi->state = IFS_TENTATIVE;
539 ifi->state = IFS_DOWN;
543 int oldstatus = ifi->active;
546 ifi->active = interface_status(ifi);
548 if (oldstatus != ifi->active) {
549 warnmsg(LOG_DEBUG, __func__,
550 "%s status is changed"
553 oldstatus, ifi->active);
555 ifi->state = IFS_DELAY;
556 } else if (ifi->probeinterval &&
558 ifi->timer.tv_sec) <= 0) {
559 /* probe timer expired */
563 ifi->state = IFS_PROBE;
567 * If we need a probe, clear the previous
568 * status wrt the "other" configuration.
571 ifi->otherconfig = 0;
573 if (probe && mobile_node)
574 defrouter_probe(ifi);
578 ifi->state = IFS_PROBE;
582 if (ifi->probes < MAX_RTR_SOLICITATIONS)
585 warnmsg(LOG_INFO, __func__,
586 "No answer after sending %d RSs",
589 ifi->state = IFS_IDLE;
593 rtsol_timer_update(ifi);
595 /* Expiration check for RA options. */
598 TAILQ_FOREACH(rai, &ifi->ifi_rainfo, rai_next) {
599 TAILQ_FOREACH_SAFE(rao, &rai->rai_ra_opt,
601 warnmsg(LOG_DEBUG, __func__,
602 "RA expiration timer: "
603 "type=%d, msg=%s, expire=%s",
604 rao->rao_type, (char *)rao->rao_msg,
605 sec2str(&rao->rao_expire));
606 if (TS_CMP(&now, &rao->rao_expire,
608 warnmsg(LOG_DEBUG, __func__,
609 "RA expiration timer: "
611 TAILQ_REMOVE(&rai->rai_ra_opt,
613 if (rao->rao_msg != NULL)
623 if (TS_CMP(&ifi->expire, &rtsol_timer, <))
624 rtsol_timer = ifi->expire;
627 if (TS_CMP(&rtsol_timer, &tm_max, ==)) {
628 warnmsg(LOG_DEBUG, __func__, "there is no timer");
630 } else if (TS_CMP(&rtsol_timer, &now, <))
631 /* this may occur when the interval is too small */
632 returnval.tv_sec = returnval.tv_nsec = 0;
634 TS_SUB(&rtsol_timer, &now, &returnval);
636 now.tv_sec += returnval.tv_sec;
637 now.tv_nsec += returnval.tv_nsec;
638 warnmsg(LOG_DEBUG, __func__, "New timer is %s",
645 rtsol_timer_update(struct ifinfo *ifi)
647 #define MILLION 1000000
648 #define DADRETRY 10 /* XXX: adhoc */
652 bzero(&ifi->timer, sizeof(ifi->timer));
654 switch (ifi->state) {
657 if (++ifi->dadcount > DADRETRY) {
659 ifi->timer.tv_sec = PROBE_INTERVAL;
661 ifi->timer.tv_sec = 1;
665 /* XXX should be configurable */
666 ifi->timer.tv_sec = 3;
669 ifi->timer = tm_max; /* stop timer(valid?) */
672 interval = arc4random_uniform(MAX_RTR_SOLICITATION_DELAY * MILLION);
673 ifi->timer.tv_sec = interval / MILLION;
674 ifi->timer.tv_nsec = (interval % MILLION) * 1000;
677 if (ifi->probes < MAX_RTR_SOLICITATIONS)
678 ifi->timer.tv_sec = RTR_SOLICITATION_INTERVAL;
681 * After sending MAX_RTR_SOLICITATIONS solicitations,
682 * we're just waiting for possible replies; there
683 * will be no more solicitation. Thus, we change
684 * the timer value to MAX_RTR_SOLICITATION_DELAY based
685 * on RFC 2461, Section 6.3.7.
687 ifi->timer.tv_sec = MAX_RTR_SOLICITATION_DELAY;
691 warnmsg(LOG_ERR, __func__,
692 "illegal interface state(%d) on %s",
693 ifi->state, ifi->ifname);
697 /* reset the timer */
698 if (TS_CMP(&ifi->timer, &tm_max, ==)) {
699 ifi->expire = tm_max;
700 warnmsg(LOG_DEBUG, __func__,
701 "stop timer for %s", ifi->ifname);
703 clock_gettime(CLOCK_MONOTONIC_FAST, &now);
704 TS_ADD(&now, &ifi->timer, &ifi->expire);
706 now.tv_sec += ifi->timer.tv_sec;
707 now.tv_nsec += ifi->timer.tv_nsec;
708 warnmsg(LOG_DEBUG, __func__, "set timer for %s to %s",
709 ifi->ifname, sec2str(&now));
717 rtsold_set_dump_file(int sig __unused)
727 fprintf(stderr, "usage: rtsold [-dDfFm1] [-O script-name] "
728 "[-p pidfile] [-R script-name] interface ...\n");
729 fprintf(stderr, "usage: rtsold [-dDfFm1] [-O script-name] "
730 "[-p pidfile] [-R script-name] -a\n");
732 fprintf(stderr, "usage: rtsol [-dDF] [-O script-name] "
733 "[-p pidfile] [-R script-name] interface ...\n");
734 fprintf(stderr, "usage: rtsol [-dDF] [-O script-name] "
735 "[-p pidfile] [-R script-name] -a\n");
740 warnmsg(int priority, const char *func, const char *msg, ...)
747 if (priority <= log_upto) {
748 (void)vfprintf(stderr, msg, ap);
749 (void)fprintf(stderr, "\n");
752 snprintf(buf, sizeof(buf), "<%s> %s", func, msg);
754 vsyslog(priority, msg, ap);
760 * return a list of interfaces which is suitable to sending an RS.
765 static char **argv = NULL;
769 struct ifaddrs *ifap, *ifa;
770 struct in6_ndireq nd;
781 if (getifaddrs(&ifap) != 0)
784 if (!Fflag && (s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
785 warnmsg(LOG_ERR, __func__, "socket");
789 /* find an ethernet */
790 for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
791 if ((ifa->ifa_flags & IFF_UP) == 0)
793 if ((ifa->ifa_flags & IFF_POINTOPOINT) != 0)
795 if ((ifa->ifa_flags & IFF_LOOPBACK) != 0)
797 if ((ifa->ifa_flags & IFF_MULTICAST) == 0)
800 if (ifa->ifa_addr->sa_family != AF_INET6)
804 for (i = 0; i < n; i++) {
805 if (strcmp(argv[i], ifa->ifa_name) == 0) {
814 * Skip the interfaces which IPv6 and/or accepting RA
818 memset(&nd, 0, sizeof(nd));
819 strlcpy(nd.ifname, ifa->ifa_name, sizeof(nd.ifname));
820 if (ioctl(s, SIOCGIFINFO_IN6, (caddr_t)&nd) < 0) {
821 warnmsg(LOG_ERR, __func__,
822 "ioctl(SIOCGIFINFO_IN6)");
825 if ((nd.ndi.flags & ND6_IFF_IFDISABLED))
827 if (!(nd.ndi.flags & ND6_IFF_ACCEPT_RTADV))
831 /* if we find multiple candidates, just warn. */
832 if (n != 0 && dflag > 1)
833 warnmsg(LOG_WARNING, __func__,
834 "multiple interfaces found");
836 a = realloc(argv, (n + 1) * sizeof(char *));
838 warnmsg(LOG_ERR, __func__, "realloc");
842 argv[n] = strdup(ifa->ifa_name);
844 warnmsg(LOG_ERR, __func__, "malloc");
851 a = realloc(argv, (n + 1) * sizeof(char *));
853 warnmsg(LOG_ERR, __func__, "realloc");
860 for (i = 0; i < n; i++)
861 warnmsg(LOG_WARNING, __func__, "probing %s",