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>
42 #include <netinet/in.h>
43 #include <netinet/icmp6.h>
44 #include <netinet/in_var.h>
45 #include <arpa/inet.h>
47 #include <netinet6/nd6.h>
66 #define RTSOL_DUMPFILE "/var/run/rtsold.dump";
67 #define RTSOL_PIDFILE "/var/run/rtsold.pid";
69 struct timespec tm_max;
70 static int log_upto = 999;
73 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 timespec *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 timespec *timeout;
120 struct pollfd set[2];
122 fd_set *fdsetp, *selectfdp;
131 opts = "adDfFm1O:p:R:u";
140 while ((ch = getopt(argc, argv, opts)) != -1) {
164 otherconf_script = optarg;
167 pidfilename = optarg;
170 resolvconf_script = optarg;
183 if ((!aflag && argc == 0) || (aflag && argc != 0)) {
188 /* Generate maximum time in timespec. */
189 tm_max.tv_sec = (-1) & ~((time_t)1 << ((sizeof(tm_max.tv_sec) * 8) - 1));
190 tm_max.tv_nsec = (-1) & ~((long)1 << ((sizeof(tm_max.tv_nsec) * 8) - 1));
194 log_upto = LOG_DEBUG;
198 log_upto = LOG_NOTICE;
203 ident = strrchr(argv0, '/');
208 openlog(ident, LOG_NDELAY|LOG_PID, LOG_DAEMON);
210 setlogmask(LOG_UPTO(log_upto));
213 if (otherconf_script && *otherconf_script != '/') {
214 errx(1, "configuration script (%s) must be an absolute path",
217 if (resolvconf_script && *resolvconf_script != '/') {
218 errx(1, "configuration script (%s) must be an absolute path",
221 if (pidfilename && *pidfilename != '/') {
222 errx(1, "pid filename (%s) must be an absolute path",
225 #ifndef HAVE_ARC4RANDOM
226 /* random value initialization */
227 srandom((u_long)time(NULL));
230 #if (__FreeBSD_version < 900000)
232 setinet6sysctl(IPV6CTL_FORWARDING, 0);
234 /* warn if forwarding is up */
235 if (getinet6sysctl(IPV6CTL_FORWARDING))
236 warnx("kernel is configured as a router, not a host");
241 /* initialization to dump internal status to a file */
242 signal(SIGUSR1, rtsold_set_dump_file);
246 daemon(0, 0); /* act as a daemon */
249 * Open a socket for sending RS and receiving RA.
250 * This should be done before calling ifinit(), since the function
253 if ((s = sockopen()) < 0) {
254 warnmsg(LOG_ERR, __func__, "failed to open a socket");
259 set[0].events = POLLIN;
268 if ((rtsock = rtsock_open()) < 0) {
269 warnmsg(LOG_ERR, __func__, "failed to open a socket");
274 set[1].events = POLLIN;
281 fdmasks = howmany(maxfd + 1, NFDBITS) * sizeof(fd_mask);
282 if ((fdsetp = malloc(fdmasks)) == NULL) {
283 warnmsg(LOG_ERR, __func__, "malloc");
286 if ((selectfdp = malloc(fdmasks)) == NULL) {
287 warnmsg(LOG_ERR, __func__, "malloc");
292 /* configuration per interface */
294 warnmsg(LOG_ERR, __func__,
295 "failed to initialize interfaces");
299 argv = autoifprobe();
300 while (argv && *argv) {
301 if (ifconfig(*argv)) {
302 warnmsg(LOG_ERR, __func__,
303 "failed to initialize %s", *argv);
309 /* setup for probing default routers */
311 warnmsg(LOG_ERR, __func__,
312 "failed to setup for probing routers");
317 /* dump the current pid */
319 pid_t pid = getpid();
322 if ((fp = fopen(pidfilename, "w")) == NULL)
323 warnmsg(LOG_ERR, __func__,
324 "failed to open a pid log file(%s): %s",
325 pidfilename, strerror(errno));
327 fprintf(fp, "%d\n", pid);
332 memset(fdsetp, 0, fdmasks);
334 FD_SET(rtsock, fdsetp);
336 while (1) { /* main loop */
340 memcpy(selectfdp, fdsetp, fdmasks);
344 if (do_dump) { /* SIGUSR1 */
346 rtsold_dump_file(dumpfilename);
350 timeout = rtsol_check_timer();
355 /* if we have no timeout, we are done (or failed) */
359 /* if all interfaces have got RA packet, we are done */
360 TAILQ_FOREACH(ifi, &ifinfo_head, ifi_next) {
361 if (ifi->state != IFS_DOWN && ifi->racnt == 0)
368 e = poll(set, 2, timeout ? (timeout->tv_sec * 1000 + timeout->tv_nsec / 1000 / 1000) : INFTIM);
370 e = select(maxfd + 1, selectfdp, NULL, NULL, timeout);
373 if (e < 0 && errno != EINTR) {
374 warnmsg(LOG_ERR, __func__, "select: %s",
380 /* packet reception */
382 if (set[1].revents & POLLIN)
384 if (FD_ISSET(rtsock, selectfdp))
386 rtsock_input(rtsock);
388 if (set[0].revents & POLLIN)
390 if (FD_ISSET(s, selectfdp))
400 ifconfig(char *ifname)
403 struct sockaddr_dl *sdl;
406 if ((sdl = if_nametosdl(ifname)) == NULL) {
407 warnmsg(LOG_ERR, __func__,
408 "failed to get link layer information for %s", ifname);
411 if (find_ifinfo(sdl->sdl_index)) {
412 warnmsg(LOG_ERR, __func__,
413 "interface %s was already configured", ifname);
419 struct in6_ndireq nd;
422 if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
423 warnmsg(LOG_ERR, __func__, "socket() failed.");
426 memset(&nd, 0, sizeof(nd));
427 strlcpy(nd.ifname, ifname, sizeof(nd.ifname));
428 if (ioctl(s, SIOCGIFINFO_IN6, (caddr_t)&nd) < 0) {
429 warnmsg(LOG_ERR, __func__,
430 "cannot get accept_rtadv flag");
434 nd.ndi.flags |= ND6_IFF_ACCEPT_RTADV;
435 if (ioctl(s, SIOCSIFINFO_IN6, (caddr_t)&nd) < 0) {
436 warnmsg(LOG_ERR, __func__,
437 "cannot set accept_rtadv flag");
444 if ((ifi = malloc(sizeof(*ifi))) == NULL) {
445 warnmsg(LOG_ERR, __func__, "memory allocation failed");
449 memset(ifi, 0, sizeof(*ifi));
451 ifi->ifi_rdnss = IFI_DNSOPT_STATE_NOINFO;
452 ifi->ifi_dnssl = IFI_DNSOPT_STATE_NOINFO;
453 TAILQ_INIT(&ifi->ifi_rainfo);
454 strlcpy(ifi->ifname, ifname, sizeof(ifi->ifname));
456 /* construct a router solicitation message */
457 if (make_packet(ifi))
460 /* set link ID of this interface. */
462 if (inet_zoneid(AF_INET6, 2, ifname, &ifi->linkid))
465 /* XXX: assume interface IDs as link IDs */
466 ifi->linkid = ifi->sdl->sdl_index;
470 * check if the interface is available.
471 * also check if SIOCGIFMEDIA ioctl is OK on the interface.
474 ifi->active = interface_status(ifi);
475 if (!ifi->mediareqok) {
477 * probe routers periodically even if the link status
480 ifi->probeinterval = PROBE_INTERVAL;
483 /* activate interface: interface_up returns 0 on success */
484 flags = interface_up(ifi->ifname);
486 ifi->state = IFS_DELAY;
487 else if (flags == IFS_TENTATIVE)
488 ifi->state = IFS_TENTATIVE;
490 ifi->state = IFS_DOWN;
492 rtsol_timer_update(ifi);
494 TAILQ_INSERT_TAIL(&ifinfo_head, ifi, ifi_next);
508 while ((ifi = TAILQ_FIRST(&ifinfo_head)) != NULL) {
509 TAILQ_REMOVE(&ifinfo_head, ifi, ifi_next);
510 if (ifi->sdl != NULL)
512 if (ifi->rs_data != NULL)
520 ifreconfig(char *ifname)
522 struct ifinfo *ifi, *prev;
526 TAILQ_FOREACH(ifi, &ifinfo_head, ifi_next) {
527 if (strncmp(ifi->ifname, ifname, sizeof(ifi->ifname)) == 0)
531 prev->next = ifi->next;
533 rv = ifconfig(ifname);
535 /* reclaim it after ifconfig() in case ifname is pointer inside ifi */
546 find_rainfo(struct ifinfo *ifi, struct sockaddr_in6 *sin6)
550 TAILQ_FOREACH(rai, &ifi->ifi_rainfo, rai_next)
551 if (memcmp(&rai->rai_saddr.sin6_addr, &sin6->sin6_addr,
552 sizeof(rai->rai_saddr.sin6_addr)) == 0)
559 find_ifinfo(int ifindex)
563 TAILQ_FOREACH(ifi, &ifinfo_head, ifi_next) {
564 if (ifi->sdl->sdl_index == ifindex)
571 make_packet(struct ifinfo *ifi)
573 size_t packlen = sizeof(struct nd_router_solicit), lladdroptlen = 0;
574 struct nd_router_solicit *rs;
577 if ((lladdroptlen = lladdropt_length(ifi->sdl)) == 0) {
578 warnmsg(LOG_INFO, __func__,
579 "link-layer address option has null length"
580 " on %s. Treat as not included.", ifi->ifname);
582 packlen += lladdroptlen;
583 ifi->rs_datalen = packlen;
585 /* allocate buffer */
586 if ((buf = malloc(packlen)) == NULL) {
587 warnmsg(LOG_ERR, __func__,
588 "memory allocation failed for %s", ifi->ifname);
593 /* fill in the message */
594 rs = (struct nd_router_solicit *)buf;
595 rs->nd_rs_type = ND_ROUTER_SOLICIT;
598 rs->nd_rs_reserved = 0;
601 /* fill in source link-layer address option */
603 lladdropt_fill(ifi->sdl, (struct nd_opt_hdr *)buf);
608 static struct timespec *
609 rtsol_check_timer(void)
611 static struct timespec returnval;
612 struct timespec now, rtsol_timer;
618 clock_gettime(CLOCK_MONOTONIC_FAST, &now);
620 rtsol_timer = tm_max;
622 TAILQ_FOREACH(ifi, &ifinfo_head, ifi_next) {
623 if (TS_CMP(&ifi->expire, &now, <=)) {
624 warnmsg(LOG_DEBUG, __func__, "timer expiration on %s, "
625 "state = %d", ifi->ifname, ifi->state);
627 while((rai = TAILQ_FIRST(&ifi->ifi_rainfo)) != NULL) {
628 /* Remove all RA options. */
629 TAILQ_REMOVE(&ifi->ifi_rainfo, rai, rai_next);
630 while ((rao = TAILQ_FIRST(&rai->rai_ra_opt)) !=
632 TAILQ_REMOVE(&rai->rai_ra_opt, rao,
634 if (rao->rao_msg != NULL)
640 switch (ifi->state) {
643 /* interface_up returns 0 on success */
644 flags = interface_up(ifi->ifname);
646 ifi->state = IFS_DELAY;
647 else if (flags == IFS_TENTATIVE)
648 ifi->state = IFS_TENTATIVE;
650 ifi->state = IFS_DOWN;
654 int oldstatus = ifi->active;
657 ifi->active = interface_status(ifi);
659 if (oldstatus != ifi->active) {
660 warnmsg(LOG_DEBUG, __func__,
661 "%s status is changed"
664 oldstatus, ifi->active);
666 ifi->state = IFS_DELAY;
667 } else if (ifi->probeinterval &&
669 ifi->timer.tv_sec) <= 0) {
670 /* probe timer expired */
674 ifi->state = IFS_PROBE;
678 * If we need a probe, clear the previous
679 * status wrt the "other" configuration.
682 ifi->otherconfig = 0;
684 if (probe && mobile_node)
685 defrouter_probe(ifi);
689 ifi->state = IFS_PROBE;
693 if (ifi->probes < MAX_RTR_SOLICITATIONS)
696 warnmsg(LOG_INFO, __func__,
697 "No answer after sending %d RSs",
700 ifi->state = IFS_IDLE;
704 rtsol_timer_update(ifi);
706 /* Expiration check for RA options. */
709 TAILQ_FOREACH(rai, &ifi->ifi_rainfo, rai_next) {
710 TAILQ_FOREACH(rao, &rai->rai_ra_opt, rao_next) {
711 warnmsg(LOG_DEBUG, __func__,
712 "RA expiration timer: "
713 "type=%d, msg=%s, expire=%s",
714 rao->rao_type, (char *)rao->rao_msg,
715 sec2str(&rao->rao_expire));
716 if (TS_CMP(&now, &rao->rao_expire,
718 warnmsg(LOG_DEBUG, __func__,
719 "RA expiration timer: "
721 TAILQ_REMOVE(&rai->rai_ra_opt,
723 if (rao->rao_msg != NULL)
733 if (TS_CMP(&ifi->expire, &rtsol_timer, <))
734 rtsol_timer = ifi->expire;
737 if (TS_CMP(&rtsol_timer, &tm_max, ==)) {
738 warnmsg(LOG_DEBUG, __func__, "there is no timer");
740 } else if (TS_CMP(&rtsol_timer, &now, <))
741 /* this may occur when the interval is too small */
742 returnval.tv_sec = returnval.tv_nsec = 0;
744 TS_SUB(&rtsol_timer, &now, &returnval);
746 now.tv_sec += returnval.tv_sec;
747 now.tv_nsec += returnval.tv_nsec;
748 warnmsg(LOG_DEBUG, __func__, "New timer is %s",
755 rtsol_timer_update(struct ifinfo *ifi)
757 #define MILLION 1000000
758 #define DADRETRY 10 /* XXX: adhoc */
762 bzero(&ifi->timer, sizeof(ifi->timer));
764 switch (ifi->state) {
767 if (++ifi->dadcount > DADRETRY) {
769 ifi->timer.tv_sec = PROBE_INTERVAL;
771 ifi->timer.tv_sec = 1;
775 /* XXX should be configurable */
776 ifi->timer.tv_sec = 3;
779 ifi->timer = tm_max; /* stop timer(valid?) */
782 #ifndef HAVE_ARC4RANDOM
783 interval = random() % (MAX_RTR_SOLICITATION_DELAY * MILLION);
785 interval = arc4random_uniform(MAX_RTR_SOLICITATION_DELAY * MILLION);
787 ifi->timer.tv_sec = interval / MILLION;
788 ifi->timer.tv_nsec = (interval % MILLION) * 1000;
791 if (ifi->probes < MAX_RTR_SOLICITATIONS)
792 ifi->timer.tv_sec = RTR_SOLICITATION_INTERVAL;
795 * After sending MAX_RTR_SOLICITATIONS solicitations,
796 * we're just waiting for possible replies; there
797 * will be no more solicitation. Thus, we change
798 * the timer value to MAX_RTR_SOLICITATION_DELAY based
799 * on RFC 2461, Section 6.3.7.
801 ifi->timer.tv_sec = MAX_RTR_SOLICITATION_DELAY;
805 warnmsg(LOG_ERR, __func__,
806 "illegal interface state(%d) on %s",
807 ifi->state, ifi->ifname);
811 /* reset the timer */
812 if (TS_CMP(&ifi->timer, &tm_max, ==)) {
813 ifi->expire = tm_max;
814 warnmsg(LOG_DEBUG, __func__,
815 "stop timer for %s", ifi->ifname);
817 clock_gettime(CLOCK_MONOTONIC_FAST, &now);
818 TS_ADD(&now, &ifi->timer, &ifi->expire);
820 now.tv_sec += ifi->timer.tv_sec;
821 now.tv_nsec += ifi->timer.tv_nsec;
822 warnmsg(LOG_DEBUG, __func__, "set timer for %s to %s",
823 ifi->ifname, sec2str(&now));
829 /* timer related utility functions */
830 #define MILLION 1000000
834 rtsold_set_dump_file(int sig __unused)
844 fprintf(stderr, "usage: rtsold [-adDfFm1] [-O script-name] "
845 "[-P pidfile] [-R script-name] interfaces...\n");
846 fprintf(stderr, "usage: rtsold [-dDfFm1] [-O script-name] "
847 "[-P pidfile] [-R script-name] -a\n");
849 fprintf(stderr, "usage: rtsol [-dDF] [-O script-name] "
850 "[-P pidfile] [-R script-name] interfaces...\n");
851 fprintf(stderr, "usage: rtsol [-dDF] [-O script-name] "
852 "[-P pidfile] [-R script-name] -a\n");
857 warnmsg(int priority, const char *func, const char *msg, ...)
864 if (priority <= log_upto) {
865 (void)vfprintf(stderr, msg, ap);
866 (void)fprintf(stderr, "\n");
869 snprintf(buf, sizeof(buf), "<%s> %s", func, msg);
871 vsyslog(priority, msg, ap);
877 * return a list of interfaces which is suitable to sending an RS.
882 static char **argv = NULL;
886 struct ifaddrs *ifap, *ifa;
887 struct in6_ndireq nd;
898 if (getifaddrs(&ifap) != 0)
901 if (!Fflag && (s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
902 warnmsg(LOG_ERR, __func__, "socket");
906 /* find an ethernet */
907 for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
908 if ((ifa->ifa_flags & IFF_UP) == 0)
910 if ((ifa->ifa_flags & IFF_POINTOPOINT) != 0)
912 if ((ifa->ifa_flags & IFF_LOOPBACK) != 0)
914 if ((ifa->ifa_flags & IFF_MULTICAST) == 0)
917 if (ifa->ifa_addr->sa_family != AF_INET6)
921 for (i = 0; i < n; i++) {
922 if (strcmp(argv[i], ifa->ifa_name) == 0) {
931 * Skip the interfaces which IPv6 and/or accepting RA
935 memset(&nd, 0, sizeof(nd));
936 strlcpy(nd.ifname, ifa->ifa_name, sizeof(nd.ifname));
937 if (ioctl(s, SIOCGIFINFO_IN6, (caddr_t)&nd) < 0) {
938 warnmsg(LOG_ERR, __func__,
939 "ioctl(SIOCGIFINFO_IN6)");
942 if ((nd.ndi.flags & ND6_IFF_IFDISABLED))
944 if (!(nd.ndi.flags & ND6_IFF_ACCEPT_RTADV))
948 /* if we find multiple candidates, just warn. */
949 if (n != 0 && dflag > 1)
950 warnmsg(LOG_WARNING, __func__,
951 "multiple interfaces found");
953 a = (char **)realloc(argv, (n + 1) * sizeof(char **));
955 warnmsg(LOG_ERR, __func__, "realloc");
959 argv[n] = strdup(ifa->ifa_name);
961 warnmsg(LOG_ERR, __func__, "malloc");
968 a = (char **)realloc(argv, (n + 1) * sizeof(char **));
970 warnmsg(LOG_ERR, __func__, "realloc");
977 for (i = 0; i < n; i++)
978 warnmsg(LOG_WARNING, __func__, "probing %s",