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",
227 #if (__FreeBSD_version < 900000)
229 setinet6sysctl(IPV6CTL_FORWARDING, 0);
231 /* warn if forwarding is up */
232 if (getinet6sysctl(IPV6CTL_FORWARDING))
233 warnx("kernel is configured as a router, not a host");
238 /* initialization to dump internal status to a file */
239 signal(SIGUSR1, rtsold_set_dump_file);
243 daemon(0, 0); /* act as a daemon */
246 * Open a socket for sending RS and receiving RA.
247 * This should be done before calling ifinit(), since the function
250 if ((s = sockopen()) < 0) {
251 warnmsg(LOG_ERR, __func__, "failed to open a socket");
256 set[0].events = POLLIN;
265 if ((rtsock = rtsock_open()) < 0) {
266 warnmsg(LOG_ERR, __func__, "failed to open a socket");
271 set[1].events = POLLIN;
278 fdmasks = howmany(maxfd + 1, NFDBITS) * sizeof(fd_mask);
279 if ((fdsetp = malloc(fdmasks)) == NULL) {
280 warnmsg(LOG_ERR, __func__, "malloc");
283 if ((selectfdp = malloc(fdmasks)) == NULL) {
284 warnmsg(LOG_ERR, __func__, "malloc");
289 /* configuration per interface */
291 warnmsg(LOG_ERR, __func__,
292 "failed to initialize interfaces");
296 argv = autoifprobe();
297 while (argv && *argv) {
298 if (ifconfig(*argv)) {
299 warnmsg(LOG_ERR, __func__,
300 "failed to initialize %s", *argv);
306 /* setup for probing default routers */
308 warnmsg(LOG_ERR, __func__,
309 "failed to setup for probing routers");
314 /* dump the current pid */
316 pid_t pid = getpid();
319 if ((fp = fopen(pidfilename, "w")) == NULL)
320 warnmsg(LOG_ERR, __func__,
321 "failed to open a pid log file(%s): %s",
322 pidfilename, strerror(errno));
324 fprintf(fp, "%d\n", pid);
329 memset(fdsetp, 0, fdmasks);
331 FD_SET(rtsock, fdsetp);
333 while (1) { /* main loop */
337 memcpy(selectfdp, fdsetp, fdmasks);
341 if (do_dump) { /* SIGUSR1 */
343 rtsold_dump_file(dumpfilename);
347 timeout = rtsol_check_timer();
352 /* if we have no timeout, we are done (or failed) */
356 /* if all interfaces have got RA packet, we are done */
357 TAILQ_FOREACH(ifi, &ifinfo_head, ifi_next) {
358 if (ifi->state != IFS_DOWN && ifi->racnt == 0)
365 e = poll(set, 2, timeout ? (timeout->tv_sec * 1000 + timeout->tv_nsec / 1000 / 1000) : INFTIM);
367 e = select(maxfd + 1, selectfdp, NULL, NULL, timeout);
370 if (e < 0 && errno != EINTR) {
371 warnmsg(LOG_ERR, __func__, "select: %s",
377 /* packet reception */
379 if (set[1].revents & POLLIN)
381 if (FD_ISSET(rtsock, selectfdp))
383 rtsock_input(rtsock);
385 if (set[0].revents & POLLIN)
387 if (FD_ISSET(s, selectfdp))
397 ifconfig(char *ifname)
400 struct sockaddr_dl *sdl;
403 if ((sdl = if_nametosdl(ifname)) == NULL) {
404 warnmsg(LOG_ERR, __func__,
405 "failed to get link layer information for %s", ifname);
408 if (find_ifinfo(sdl->sdl_index)) {
409 warnmsg(LOG_ERR, __func__,
410 "interface %s was already configured", ifname);
416 struct in6_ndireq nd;
419 if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
420 warnmsg(LOG_ERR, __func__, "socket() failed.");
423 memset(&nd, 0, sizeof(nd));
424 strlcpy(nd.ifname, ifname, sizeof(nd.ifname));
425 if (ioctl(s, SIOCGIFINFO_IN6, (caddr_t)&nd) < 0) {
426 warnmsg(LOG_ERR, __func__,
427 "cannot get accept_rtadv flag");
431 nd.ndi.flags |= ND6_IFF_ACCEPT_RTADV;
432 if (ioctl(s, SIOCSIFINFO_IN6, (caddr_t)&nd) < 0) {
433 warnmsg(LOG_ERR, __func__,
434 "cannot set accept_rtadv flag");
441 if ((ifi = malloc(sizeof(*ifi))) == NULL) {
442 warnmsg(LOG_ERR, __func__, "memory allocation failed");
446 memset(ifi, 0, sizeof(*ifi));
448 ifi->ifi_rdnss = IFI_DNSOPT_STATE_NOINFO;
449 ifi->ifi_dnssl = IFI_DNSOPT_STATE_NOINFO;
450 TAILQ_INIT(&ifi->ifi_rainfo);
451 strlcpy(ifi->ifname, ifname, sizeof(ifi->ifname));
453 /* construct a router solicitation message */
454 if (make_packet(ifi))
457 /* set link ID of this interface. */
459 if (inet_zoneid(AF_INET6, 2, ifname, &ifi->linkid))
462 /* XXX: assume interface IDs as link IDs */
463 ifi->linkid = ifi->sdl->sdl_index;
467 * check if the interface is available.
468 * also check if SIOCGIFMEDIA ioctl is OK on the interface.
471 ifi->active = interface_status(ifi);
472 if (!ifi->mediareqok) {
474 * probe routers periodically even if the link status
477 ifi->probeinterval = PROBE_INTERVAL;
480 /* activate interface: interface_up returns 0 on success */
481 flags = interface_up(ifi->ifname);
483 ifi->state = IFS_DELAY;
484 else if (flags == IFS_TENTATIVE)
485 ifi->state = IFS_TENTATIVE;
487 ifi->state = IFS_DOWN;
489 rtsol_timer_update(ifi);
491 TAILQ_INSERT_TAIL(&ifinfo_head, ifi, ifi_next);
505 while ((ifi = TAILQ_FIRST(&ifinfo_head)) != NULL) {
506 TAILQ_REMOVE(&ifinfo_head, ifi, ifi_next);
507 if (ifi->sdl != NULL)
509 if (ifi->rs_data != NULL)
517 ifreconfig(char *ifname)
519 struct ifinfo *ifi, *prev;
523 TAILQ_FOREACH(ifi, &ifinfo_head, ifi_next) {
524 if (strncmp(ifi->ifname, ifname, sizeof(ifi->ifname)) == 0)
528 prev->next = ifi->next;
530 rv = ifconfig(ifname);
532 /* reclaim it after ifconfig() in case ifname is pointer inside ifi */
543 find_rainfo(struct ifinfo *ifi, struct sockaddr_in6 *sin6)
547 TAILQ_FOREACH(rai, &ifi->ifi_rainfo, rai_next)
548 if (memcmp(&rai->rai_saddr.sin6_addr, &sin6->sin6_addr,
549 sizeof(rai->rai_saddr.sin6_addr)) == 0)
556 find_ifinfo(int ifindex)
560 TAILQ_FOREACH(ifi, &ifinfo_head, ifi_next) {
561 if (ifi->sdl->sdl_index == ifindex)
568 make_packet(struct ifinfo *ifi)
570 size_t packlen = sizeof(struct nd_router_solicit), lladdroptlen = 0;
571 struct nd_router_solicit *rs;
574 if ((lladdroptlen = lladdropt_length(ifi->sdl)) == 0) {
575 warnmsg(LOG_INFO, __func__,
576 "link-layer address option has null length"
577 " on %s. Treat as not included.", ifi->ifname);
579 packlen += lladdroptlen;
580 ifi->rs_datalen = packlen;
582 /* allocate buffer */
583 if ((buf = malloc(packlen)) == NULL) {
584 warnmsg(LOG_ERR, __func__,
585 "memory allocation failed for %s", ifi->ifname);
590 /* fill in the message */
591 rs = (struct nd_router_solicit *)buf;
592 rs->nd_rs_type = ND_ROUTER_SOLICIT;
595 rs->nd_rs_reserved = 0;
598 /* fill in source link-layer address option */
600 lladdropt_fill(ifi->sdl, (struct nd_opt_hdr *)buf);
605 static struct timespec *
606 rtsol_check_timer(void)
608 static struct timespec returnval;
609 struct timespec now, rtsol_timer;
612 struct ra_opt *rao, *raotmp;
615 clock_gettime(CLOCK_MONOTONIC_FAST, &now);
617 rtsol_timer = tm_max;
619 TAILQ_FOREACH(ifi, &ifinfo_head, ifi_next) {
620 if (TS_CMP(&ifi->expire, &now, <=)) {
621 warnmsg(LOG_DEBUG, __func__, "timer expiration on %s, "
622 "state = %d", ifi->ifname, ifi->state);
624 while((rai = TAILQ_FIRST(&ifi->ifi_rainfo)) != NULL) {
625 /* Remove all RA options. */
626 TAILQ_REMOVE(&ifi->ifi_rainfo, rai, rai_next);
627 while ((rao = TAILQ_FIRST(&rai->rai_ra_opt)) !=
629 TAILQ_REMOVE(&rai->rai_ra_opt, rao,
631 if (rao->rao_msg != NULL)
637 switch (ifi->state) {
640 /* interface_up returns 0 on success */
641 flags = interface_up(ifi->ifname);
643 ifi->state = IFS_DELAY;
644 else if (flags == IFS_TENTATIVE)
645 ifi->state = IFS_TENTATIVE;
647 ifi->state = IFS_DOWN;
651 int oldstatus = ifi->active;
654 ifi->active = interface_status(ifi);
656 if (oldstatus != ifi->active) {
657 warnmsg(LOG_DEBUG, __func__,
658 "%s status is changed"
661 oldstatus, ifi->active);
663 ifi->state = IFS_DELAY;
664 } else if (ifi->probeinterval &&
666 ifi->timer.tv_sec) <= 0) {
667 /* probe timer expired */
671 ifi->state = IFS_PROBE;
675 * If we need a probe, clear the previous
676 * status wrt the "other" configuration.
679 ifi->otherconfig = 0;
681 if (probe && mobile_node)
682 defrouter_probe(ifi);
686 ifi->state = IFS_PROBE;
690 if (ifi->probes < MAX_RTR_SOLICITATIONS)
693 warnmsg(LOG_INFO, __func__,
694 "No answer after sending %d RSs",
697 ifi->state = IFS_IDLE;
701 rtsol_timer_update(ifi);
703 /* Expiration check for RA options. */
706 TAILQ_FOREACH(rai, &ifi->ifi_rainfo, rai_next) {
707 TAILQ_FOREACH_SAFE(rao, &rai->rai_ra_opt,
709 warnmsg(LOG_DEBUG, __func__,
710 "RA expiration timer: "
711 "type=%d, msg=%s, expire=%s",
712 rao->rao_type, (char *)rao->rao_msg,
713 sec2str(&rao->rao_expire));
714 if (TS_CMP(&now, &rao->rao_expire,
716 warnmsg(LOG_DEBUG, __func__,
717 "RA expiration timer: "
719 TAILQ_REMOVE(&rai->rai_ra_opt,
721 if (rao->rao_msg != NULL)
731 if (TS_CMP(&ifi->expire, &rtsol_timer, <))
732 rtsol_timer = ifi->expire;
735 if (TS_CMP(&rtsol_timer, &tm_max, ==)) {
736 warnmsg(LOG_DEBUG, __func__, "there is no timer");
738 } else if (TS_CMP(&rtsol_timer, &now, <))
739 /* this may occur when the interval is too small */
740 returnval.tv_sec = returnval.tv_nsec = 0;
742 TS_SUB(&rtsol_timer, &now, &returnval);
744 now.tv_sec += returnval.tv_sec;
745 now.tv_nsec += returnval.tv_nsec;
746 warnmsg(LOG_DEBUG, __func__, "New timer is %s",
753 rtsol_timer_update(struct ifinfo *ifi)
755 #define MILLION 1000000
756 #define DADRETRY 10 /* XXX: adhoc */
760 bzero(&ifi->timer, sizeof(ifi->timer));
762 switch (ifi->state) {
765 if (++ifi->dadcount > DADRETRY) {
767 ifi->timer.tv_sec = PROBE_INTERVAL;
769 ifi->timer.tv_sec = 1;
773 /* XXX should be configurable */
774 ifi->timer.tv_sec = 3;
777 ifi->timer = tm_max; /* stop timer(valid?) */
780 interval = arc4random_uniform(MAX_RTR_SOLICITATION_DELAY * MILLION);
781 ifi->timer.tv_sec = interval / MILLION;
782 ifi->timer.tv_nsec = (interval % MILLION) * 1000;
785 if (ifi->probes < MAX_RTR_SOLICITATIONS)
786 ifi->timer.tv_sec = RTR_SOLICITATION_INTERVAL;
789 * After sending MAX_RTR_SOLICITATIONS solicitations,
790 * we're just waiting for possible replies; there
791 * will be no more solicitation. Thus, we change
792 * the timer value to MAX_RTR_SOLICITATION_DELAY based
793 * on RFC 2461, Section 6.3.7.
795 ifi->timer.tv_sec = MAX_RTR_SOLICITATION_DELAY;
799 warnmsg(LOG_ERR, __func__,
800 "illegal interface state(%d) on %s",
801 ifi->state, ifi->ifname);
805 /* reset the timer */
806 if (TS_CMP(&ifi->timer, &tm_max, ==)) {
807 ifi->expire = tm_max;
808 warnmsg(LOG_DEBUG, __func__,
809 "stop timer for %s", ifi->ifname);
811 clock_gettime(CLOCK_MONOTONIC_FAST, &now);
812 TS_ADD(&now, &ifi->timer, &ifi->expire);
814 now.tv_sec += ifi->timer.tv_sec;
815 now.tv_nsec += ifi->timer.tv_nsec;
816 warnmsg(LOG_DEBUG, __func__, "set timer for %s to %s",
817 ifi->ifname, sec2str(&now));
823 /* timer related utility functions */
824 #define MILLION 1000000
828 rtsold_set_dump_file(int sig __unused)
838 fprintf(stderr, "usage: rtsold [-dDfFm1] [-O script-name] "
839 "[-p pidfile] [-R script-name] interface ...\n");
840 fprintf(stderr, "usage: rtsold [-dDfFm1] [-O script-name] "
841 "[-p pidfile] [-R script-name] -a\n");
843 fprintf(stderr, "usage: rtsol [-dDF] [-O script-name] "
844 "[-p pidfile] [-R script-name] interface ...\n");
845 fprintf(stderr, "usage: rtsol [-dDF] [-O script-name] "
846 "[-p pidfile] [-R script-name] -a\n");
851 warnmsg(int priority, const char *func, const char *msg, ...)
858 if (priority <= log_upto) {
859 (void)vfprintf(stderr, msg, ap);
860 (void)fprintf(stderr, "\n");
863 snprintf(buf, sizeof(buf), "<%s> %s", func, msg);
865 vsyslog(priority, msg, ap);
871 * return a list of interfaces which is suitable to sending an RS.
876 static char **argv = NULL;
880 struct ifaddrs *ifap, *ifa;
881 struct in6_ndireq nd;
892 if (getifaddrs(&ifap) != 0)
895 if (!Fflag && (s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
896 warnmsg(LOG_ERR, __func__, "socket");
900 /* find an ethernet */
901 for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
902 if ((ifa->ifa_flags & IFF_UP) == 0)
904 if ((ifa->ifa_flags & IFF_POINTOPOINT) != 0)
906 if ((ifa->ifa_flags & IFF_LOOPBACK) != 0)
908 if ((ifa->ifa_flags & IFF_MULTICAST) == 0)
911 if (ifa->ifa_addr->sa_family != AF_INET6)
915 for (i = 0; i < n; i++) {
916 if (strcmp(argv[i], ifa->ifa_name) == 0) {
925 * Skip the interfaces which IPv6 and/or accepting RA
929 memset(&nd, 0, sizeof(nd));
930 strlcpy(nd.ifname, ifa->ifa_name, sizeof(nd.ifname));
931 if (ioctl(s, SIOCGIFINFO_IN6, (caddr_t)&nd) < 0) {
932 warnmsg(LOG_ERR, __func__,
933 "ioctl(SIOCGIFINFO_IN6)");
936 if ((nd.ndi.flags & ND6_IFF_IFDISABLED))
938 if (!(nd.ndi.flags & ND6_IFF_ACCEPT_RTADV))
942 /* if we find multiple candidates, just warn. */
943 if (n != 0 && dflag > 1)
944 warnmsg(LOG_WARNING, __func__,
945 "multiple interfaces found");
947 a = (char **)realloc(argv, (n + 1) * sizeof(char **));
949 warnmsg(LOG_ERR, __func__, "realloc");
953 argv[n] = strdup(ifa->ifa_name);
955 warnmsg(LOG_ERR, __func__, "malloc");
962 a = (char **)realloc(argv, (n + 1) * sizeof(char **));
964 warnmsg(LOG_ERR, __func__, "realloc");
971 for (i = 0; i < n; i++)
972 warnmsg(LOG_WARNING, __func__, "probing %s",