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>
36 #include <sys/socket.h>
37 #include <sys/param.h>
40 #include <net/if_dl.h>
41 #include <net/if_var.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>
67 #define RTSOL_DUMPFILE "/var/run/rtsold.dump";
68 #define RTSOL_PIDFILE "/var/run/rtsold.pid";
70 struct timespec tm_max;
71 static int log_upto = 999;
74 int Fflag = 0; /* force setting sysctl parameters */
79 const char *otherconf_script;
80 const char *resolvconf_script = "/sbin/resolvconf";
82 /* protocol constants */
83 #define MAX_RTR_SOLICITATION_DELAY 1 /* second */
84 #define RTR_SOLICITATION_INTERVAL 4 /* seconds */
85 #define MAX_RTR_SOLICITATIONS 3 /* times */
88 * implementation dependent constants in seconds
89 * XXX: should be configurable
91 #define PROBE_INTERVAL 60
93 /* static variables and functions */
94 static int mobile_node = 0;
95 static const char *pidfilename = RTSOL_PIDFILE;
99 static const char *dumpfilename = RTSOL_DUMPFILE;
103 static int ifreconfig(char *);
106 static int make_packet(struct ifinfo *);
107 static struct timespec *rtsol_check_timer(void);
110 static void rtsold_set_dump_file(int);
112 static void usage(void);
115 main(int argc, char **argv)
118 struct timespec *timeout;
121 struct pollfd set[2];
123 fd_set *fdsetp, *selectfdp;
132 opts = "adDfFm1O:p:R:u";
141 while ((ch = getopt(argc, argv, opts)) != -1) {
165 otherconf_script = optarg;
168 pidfilename = optarg;
171 resolvconf_script = optarg;
184 if ((!aflag && argc == 0) || (aflag && argc != 0)) {
189 /* Generate maximum time in timespec. */
190 tm_max.tv_sec = (-1) & ~((time_t)1 << ((sizeof(tm_max.tv_sec) * 8) - 1));
191 tm_max.tv_nsec = (-1) & ~((long)1 << ((sizeof(tm_max.tv_nsec) * 8) - 1));
195 log_upto = LOG_DEBUG;
199 log_upto = LOG_NOTICE;
204 ident = strrchr(argv0, '/');
209 openlog(ident, LOG_NDELAY|LOG_PID, LOG_DAEMON);
211 setlogmask(LOG_UPTO(log_upto));
214 if (otherconf_script && *otherconf_script != '/') {
215 errx(1, "configuration script (%s) must be an absolute path",
218 if (resolvconf_script && *resolvconf_script != '/') {
219 errx(1, "configuration script (%s) must be an absolute path",
222 if (pidfilename && *pidfilename != '/') {
223 errx(1, "pid filename (%s) must be an absolute path",
226 #ifndef HAVE_ARC4RANDOM
227 /* random value initialization */
228 srandom((u_long)time(NULL));
231 #if (__FreeBSD_version < 900000)
233 setinet6sysctl(IPV6CTL_FORWARDING, 0);
235 /* warn if forwarding is up */
236 if (getinet6sysctl(IPV6CTL_FORWARDING))
237 warnx("kernel is configured as a router, not a host");
242 /* initialization to dump internal status to a file */
243 signal(SIGUSR1, rtsold_set_dump_file);
247 daemon(0, 0); /* act as a daemon */
250 * Open a socket for sending RS and receiving RA.
251 * This should be done before calling ifinit(), since the function
254 if ((s = sockopen()) < 0) {
255 warnmsg(LOG_ERR, __func__, "failed to open a socket");
260 set[0].events = POLLIN;
269 if ((rtsock = rtsock_open()) < 0) {
270 warnmsg(LOG_ERR, __func__, "failed to open a socket");
275 set[1].events = POLLIN;
282 fdmasks = howmany(maxfd + 1, NFDBITS) * sizeof(fd_mask);
283 if ((fdsetp = malloc(fdmasks)) == NULL) {
284 warnmsg(LOG_ERR, __func__, "malloc");
287 if ((selectfdp = malloc(fdmasks)) == NULL) {
288 warnmsg(LOG_ERR, __func__, "malloc");
293 /* configuration per interface */
295 warnmsg(LOG_ERR, __func__,
296 "failed to initialize interfaces");
300 argv = autoifprobe();
301 while (argv && *argv) {
302 if (ifconfig(*argv)) {
303 warnmsg(LOG_ERR, __func__,
304 "failed to initialize %s", *argv);
310 /* setup for probing default routers */
312 warnmsg(LOG_ERR, __func__,
313 "failed to setup for probing routers");
318 /* dump the current pid */
320 pid_t pid = getpid();
323 if ((fp = fopen(pidfilename, "w")) == NULL)
324 warnmsg(LOG_ERR, __func__,
325 "failed to open a pid log file(%s): %s",
326 pidfilename, strerror(errno));
328 fprintf(fp, "%d\n", pid);
333 memset(fdsetp, 0, fdmasks);
335 FD_SET(rtsock, fdsetp);
337 while (1) { /* main loop */
341 memcpy(selectfdp, fdsetp, fdmasks);
345 if (do_dump) { /* SIGUSR1 */
347 rtsold_dump_file(dumpfilename);
351 timeout = rtsol_check_timer();
356 /* if we have no timeout, we are done (or failed) */
360 /* if all interfaces have got RA packet, we are done */
361 TAILQ_FOREACH(ifi, &ifinfo_head, ifi_next) {
362 if (ifi->state != IFS_DOWN && ifi->racnt == 0)
369 e = poll(set, 2, timeout ? (timeout->tv_sec * 1000 + timeout->tv_nsec / 1000 / 1000) : INFTIM);
371 e = select(maxfd + 1, selectfdp, NULL, NULL, timeout);
374 if (e < 0 && errno != EINTR) {
375 warnmsg(LOG_ERR, __func__, "select: %s",
381 /* packet reception */
383 if (set[1].revents & POLLIN)
385 if (FD_ISSET(rtsock, selectfdp))
387 rtsock_input(rtsock);
389 if (set[0].revents & POLLIN)
391 if (FD_ISSET(s, selectfdp))
401 ifconfig(char *ifname)
404 struct sockaddr_dl *sdl;
407 if ((sdl = if_nametosdl(ifname)) == NULL) {
408 warnmsg(LOG_ERR, __func__,
409 "failed to get link layer information for %s", ifname);
412 if (find_ifinfo(sdl->sdl_index)) {
413 warnmsg(LOG_ERR, __func__,
414 "interface %s was already configured", ifname);
420 struct in6_ndireq nd;
423 if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
424 warnmsg(LOG_ERR, __func__, "socket() failed.");
427 memset(&nd, 0, sizeof(nd));
428 strlcpy(nd.ifname, ifname, sizeof(nd.ifname));
429 if (ioctl(s, SIOCGIFINFO_IN6, (caddr_t)&nd) < 0) {
430 warnmsg(LOG_ERR, __func__,
431 "cannot get accept_rtadv flag");
435 nd.ndi.flags |= ND6_IFF_ACCEPT_RTADV;
436 if (ioctl(s, SIOCSIFINFO_IN6, (caddr_t)&nd) < 0) {
437 warnmsg(LOG_ERR, __func__,
438 "cannot set accept_rtadv flag");
445 if ((ifi = malloc(sizeof(*ifi))) == NULL) {
446 warnmsg(LOG_ERR, __func__, "memory allocation failed");
450 memset(ifi, 0, sizeof(*ifi));
452 ifi->ifi_rdnss = IFI_DNSOPT_STATE_NOINFO;
453 ifi->ifi_dnssl = IFI_DNSOPT_STATE_NOINFO;
454 TAILQ_INIT(&ifi->ifi_rainfo);
455 strlcpy(ifi->ifname, ifname, sizeof(ifi->ifname));
457 /* construct a router solicitation message */
458 if (make_packet(ifi))
461 /* set link ID of this interface. */
463 if (inet_zoneid(AF_INET6, 2, ifname, &ifi->linkid))
466 /* XXX: assume interface IDs as link IDs */
467 ifi->linkid = ifi->sdl->sdl_index;
471 * check if the interface is available.
472 * also check if SIOCGIFMEDIA ioctl is OK on the interface.
475 ifi->active = interface_status(ifi);
476 if (!ifi->mediareqok) {
478 * probe routers periodically even if the link status
481 ifi->probeinterval = PROBE_INTERVAL;
484 /* activate interface: interface_up returns 0 on success */
485 flags = interface_up(ifi->ifname);
487 ifi->state = IFS_DELAY;
488 else if (flags == IFS_TENTATIVE)
489 ifi->state = IFS_TENTATIVE;
491 ifi->state = IFS_DOWN;
493 rtsol_timer_update(ifi);
495 TAILQ_INSERT_TAIL(&ifinfo_head, ifi, ifi_next);
509 while ((ifi = TAILQ_FIRST(&ifinfo_head)) != NULL) {
510 TAILQ_REMOVE(&ifinfo_head, ifi, ifi_next);
511 if (ifi->sdl != NULL)
513 if (ifi->rs_data != NULL)
521 ifreconfig(char *ifname)
523 struct ifinfo *ifi, *prev;
527 TAILQ_FOREACH(ifi, &ifinfo_head, ifi_next) {
528 if (strncmp(ifi->ifname, ifname, sizeof(ifi->ifname)) == 0)
532 prev->next = ifi->next;
534 rv = ifconfig(ifname);
536 /* reclaim it after ifconfig() in case ifname is pointer inside ifi */
547 find_rainfo(struct ifinfo *ifi, struct sockaddr_in6 *sin6)
551 TAILQ_FOREACH(rai, &ifi->ifi_rainfo, rai_next)
552 if (memcmp(&rai->rai_saddr.sin6_addr, &sin6->sin6_addr,
553 sizeof(rai->rai_saddr.sin6_addr)) == 0)
560 find_ifinfo(int ifindex)
564 TAILQ_FOREACH(ifi, &ifinfo_head, ifi_next) {
565 if (ifi->sdl->sdl_index == ifindex)
572 make_packet(struct ifinfo *ifi)
574 size_t packlen = sizeof(struct nd_router_solicit), lladdroptlen = 0;
575 struct nd_router_solicit *rs;
578 if ((lladdroptlen = lladdropt_length(ifi->sdl)) == 0) {
579 warnmsg(LOG_INFO, __func__,
580 "link-layer address option has null length"
581 " on %s. Treat as not included.", ifi->ifname);
583 packlen += lladdroptlen;
584 ifi->rs_datalen = packlen;
586 /* allocate buffer */
587 if ((buf = malloc(packlen)) == NULL) {
588 warnmsg(LOG_ERR, __func__,
589 "memory allocation failed for %s", ifi->ifname);
594 /* fill in the message */
595 rs = (struct nd_router_solicit *)buf;
596 rs->nd_rs_type = ND_ROUTER_SOLICIT;
599 rs->nd_rs_reserved = 0;
602 /* fill in source link-layer address option */
604 lladdropt_fill(ifi->sdl, (struct nd_opt_hdr *)buf);
609 static struct timespec *
610 rtsol_check_timer(void)
612 static struct timespec returnval;
613 struct timespec now, rtsol_timer;
619 clock_gettime(CLOCK_MONOTONIC_FAST, &now);
621 rtsol_timer = tm_max;
623 TAILQ_FOREACH(ifi, &ifinfo_head, ifi_next) {
624 if (TS_CMP(&ifi->expire, &now, <=)) {
625 warnmsg(LOG_DEBUG, __func__, "timer expiration on %s, "
626 "state = %d", ifi->ifname, ifi->state);
628 while((rai = TAILQ_FIRST(&ifi->ifi_rainfo)) != NULL) {
629 /* Remove all RA options. */
630 TAILQ_REMOVE(&ifi->ifi_rainfo, rai, rai_next);
631 while ((rao = TAILQ_FIRST(&rai->rai_ra_opt)) !=
633 TAILQ_REMOVE(&rai->rai_ra_opt, rao,
635 if (rao->rao_msg != NULL)
641 switch (ifi->state) {
644 /* interface_up returns 0 on success */
645 flags = interface_up(ifi->ifname);
647 ifi->state = IFS_DELAY;
648 else if (flags == IFS_TENTATIVE)
649 ifi->state = IFS_TENTATIVE;
651 ifi->state = IFS_DOWN;
655 int oldstatus = ifi->active;
658 ifi->active = interface_status(ifi);
660 if (oldstatus != ifi->active) {
661 warnmsg(LOG_DEBUG, __func__,
662 "%s status is changed"
665 oldstatus, ifi->active);
667 ifi->state = IFS_DELAY;
668 } else if (ifi->probeinterval &&
670 ifi->timer.tv_sec) <= 0) {
671 /* probe timer expired */
675 ifi->state = IFS_PROBE;
679 * If we need a probe, clear the previous
680 * status wrt the "other" configuration.
683 ifi->otherconfig = 0;
685 if (probe && mobile_node)
686 defrouter_probe(ifi);
690 ifi->state = IFS_PROBE;
694 if (ifi->probes < MAX_RTR_SOLICITATIONS)
697 warnmsg(LOG_INFO, __func__,
698 "No answer after sending %d RSs",
701 ifi->state = IFS_IDLE;
705 rtsol_timer_update(ifi);
707 /* Expiration check for RA options. */
710 TAILQ_FOREACH(rai, &ifi->ifi_rainfo, rai_next) {
711 TAILQ_FOREACH(rao, &rai->rai_ra_opt, rao_next) {
712 warnmsg(LOG_DEBUG, __func__,
713 "RA expiration timer: "
714 "type=%d, msg=%s, expire=%s",
715 rao->rao_type, (char *)rao->rao_msg,
716 sec2str(&rao->rao_expire));
717 if (TS_CMP(&now, &rao->rao_expire,
719 warnmsg(LOG_DEBUG, __func__,
720 "RA expiration timer: "
722 TAILQ_REMOVE(&rai->rai_ra_opt,
724 if (rao->rao_msg != NULL)
734 if (TS_CMP(&ifi->expire, &rtsol_timer, <))
735 rtsol_timer = ifi->expire;
738 if (TS_CMP(&rtsol_timer, &tm_max, ==)) {
739 warnmsg(LOG_DEBUG, __func__, "there is no timer");
741 } else if (TS_CMP(&rtsol_timer, &now, <))
742 /* this may occur when the interval is too small */
743 returnval.tv_sec = returnval.tv_nsec = 0;
745 TS_SUB(&rtsol_timer, &now, &returnval);
747 now.tv_sec += returnval.tv_sec;
748 now.tv_nsec += returnval.tv_nsec;
749 warnmsg(LOG_DEBUG, __func__, "New timer is %s",
756 rtsol_timer_update(struct ifinfo *ifi)
758 #define MILLION 1000000
759 #define DADRETRY 10 /* XXX: adhoc */
763 bzero(&ifi->timer, sizeof(ifi->timer));
765 switch (ifi->state) {
768 if (++ifi->dadcount > DADRETRY) {
770 ifi->timer.tv_sec = PROBE_INTERVAL;
772 ifi->timer.tv_sec = 1;
776 /* XXX should be configurable */
777 ifi->timer.tv_sec = 3;
780 ifi->timer = tm_max; /* stop timer(valid?) */
783 #ifndef HAVE_ARC4RANDOM
784 interval = random() % (MAX_RTR_SOLICITATION_DELAY * MILLION);
786 interval = arc4random_uniform(MAX_RTR_SOLICITATION_DELAY * MILLION);
788 ifi->timer.tv_sec = interval / MILLION;
789 ifi->timer.tv_nsec = (interval % MILLION) * 1000;
792 if (ifi->probes < MAX_RTR_SOLICITATIONS)
793 ifi->timer.tv_sec = RTR_SOLICITATION_INTERVAL;
796 * After sending MAX_RTR_SOLICITATIONS solicitations,
797 * we're just waiting for possible replies; there
798 * will be no more solicitation. Thus, we change
799 * the timer value to MAX_RTR_SOLICITATION_DELAY based
800 * on RFC 2461, Section 6.3.7.
802 ifi->timer.tv_sec = MAX_RTR_SOLICITATION_DELAY;
806 warnmsg(LOG_ERR, __func__,
807 "illegal interface state(%d) on %s",
808 ifi->state, ifi->ifname);
812 /* reset the timer */
813 if (TS_CMP(&ifi->timer, &tm_max, ==)) {
814 ifi->expire = tm_max;
815 warnmsg(LOG_DEBUG, __func__,
816 "stop timer for %s", ifi->ifname);
818 clock_gettime(CLOCK_MONOTONIC_FAST, &now);
819 TS_ADD(&now, &ifi->timer, &ifi->expire);
821 now.tv_sec += ifi->timer.tv_sec;
822 now.tv_nsec += ifi->timer.tv_nsec;
823 warnmsg(LOG_DEBUG, __func__, "set timer for %s to %s",
824 ifi->ifname, sec2str(&now));
830 /* timer related utility functions */
831 #define MILLION 1000000
835 rtsold_set_dump_file(int sig __unused)
845 fprintf(stderr, "usage: rtsold [-adDfFm1] [-O script-name] "
846 "[-P pidfile] [-R script-name] interfaces...\n");
847 fprintf(stderr, "usage: rtsold [-dDfFm1] [-O script-name] "
848 "[-P pidfile] [-R script-name] -a\n");
850 fprintf(stderr, "usage: rtsol [-dDF] [-O script-name] "
851 "[-P pidfile] [-R script-name] interfaces...\n");
852 fprintf(stderr, "usage: rtsol [-dDF] [-O script-name] "
853 "[-P pidfile] [-R script-name] -a\n");
858 warnmsg(int priority, const char *func, const char *msg, ...)
865 if (priority <= log_upto) {
866 (void)vfprintf(stderr, msg, ap);
867 (void)fprintf(stderr, "\n");
870 snprintf(buf, sizeof(buf), "<%s> %s", func, msg);
872 vsyslog(priority, msg, ap);
878 * return a list of interfaces which is suitable to sending an RS.
883 static char **argv = NULL;
887 struct ifaddrs *ifap, *ifa;
888 struct in6_ndireq nd;
899 if (getifaddrs(&ifap) != 0)
902 if (!Fflag && (s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
903 warnmsg(LOG_ERR, __func__, "socket");
907 /* find an ethernet */
908 for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
909 if ((ifa->ifa_flags & IFF_UP) == 0)
911 if ((ifa->ifa_flags & IFF_POINTOPOINT) != 0)
913 if ((ifa->ifa_flags & IFF_LOOPBACK) != 0)
915 if ((ifa->ifa_flags & IFF_MULTICAST) == 0)
918 if (ifa->ifa_addr->sa_family != AF_INET6)
922 for (i = 0; i < n; i++) {
923 if (strcmp(argv[i], ifa->ifa_name) == 0) {
932 * Skip the interfaces which IPv6 and/or accepting RA
936 memset(&nd, 0, sizeof(nd));
937 strlcpy(nd.ifname, ifa->ifa_name, sizeof(nd.ifname));
938 if (ioctl(s, SIOCGIFINFO_IN6, (caddr_t)&nd) < 0) {
939 warnmsg(LOG_ERR, __func__,
940 "ioctl(SIOCGIFINFO_IN6)");
943 if ((nd.ndi.flags & ND6_IFF_IFDISABLED))
945 if (!(nd.ndi.flags & ND6_IFF_ACCEPT_RTADV))
949 /* if we find multiple candidates, just warn. */
950 if (n != 0 && dflag > 1)
951 warnmsg(LOG_WARNING, __func__,
952 "multiple interfaces found");
954 a = (char **)realloc(argv, (n + 1) * sizeof(char **));
956 warnmsg(LOG_ERR, __func__, "realloc");
960 argv[n] = strdup(ifa->ifa_name);
962 warnmsg(LOG_ERR, __func__, "malloc");
969 a = (char **)realloc(argv, (n + 1) * sizeof(char **));
971 warnmsg(LOG_ERR, __func__, "realloc");
978 for (i = 0; i < n; i++)
979 warnmsg(LOG_WARNING, __func__, "probing %s",