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>
47 #include <arpa/inet.h>
49 #include <netinet6/nd6.h>
67 #define RTSOL_DUMPFILE "/var/run/rtsold.dump";
68 #define RTSOL_PIDFILE "/var/run/rtsold.pid";
70 struct ifinfo *iflist;
71 struct timeval tm_max = {0x7fffffff, 0x7fffffff};
72 static int log_upto = 999;
75 int Fflag = 0; /* force setting sysctl parameters */
80 const char *otherconf_script;
81 const char *resolvconf_script = "/sbin/resolvconf";
83 /* protocol constants */
84 #define MAX_RTR_SOLICITATION_DELAY 1 /* second */
85 #define RTR_SOLICITATION_INTERVAL 4 /* seconds */
86 #define MAX_RTR_SOLICITATIONS 3 /* times */
89 * implementation dependent constants in seconds
90 * XXX: should be configurable
92 #define PROBE_INTERVAL 60
94 /* static variables and functions */
95 static int mobile_node = 0;
96 static const char *pidfilename = RTSOL_PIDFILE;
100 static const char *dumpfilename = RTSOL_DUMPFILE;
104 static int ifreconfig(char *);
107 static int make_packet(struct ifinfo *);
108 static struct timeval *rtsol_check_timer(void);
111 static void rtsold_set_dump_file(int);
113 static void usage(void);
116 main(int argc, char **argv)
119 struct timeval *timeout;
122 struct pollfd set[2];
124 fd_set *fdsetp, *selectfdp;
133 opts = "adDfFm1O:p:R:u";
142 while ((ch = getopt(argc, argv, opts)) != -1) {
166 otherconf_script = optarg;
169 pidfilename = optarg;
172 resolvconf_script = optarg;
185 if ((!aflag && argc == 0) || (aflag && argc != 0)) {
192 log_upto = LOG_DEBUG;
196 log_upto = LOG_NOTICE;
201 ident = strrchr(argv0, '/');
206 openlog(ident, LOG_NDELAY|LOG_PID, LOG_DAEMON);
208 setlogmask(LOG_UPTO(log_upto));
211 if (otherconf_script && *otherconf_script != '/') {
212 errx(1, "configuration script (%s) must be an absolute path",
215 if (resolvconf_script && *resolvconf_script != '/') {
216 errx(1, "configuration script (%s) must be an absolute path",
219 if (pidfilename && *pidfilename != '/') {
220 errx(1, "pid filename (%s) must be an absolute path",
223 #ifndef HAVE_ARC4RANDOM
224 /* random value initialization */
225 srandom((u_long)time(NULL));
228 #if (__FreeBSD_version < 900000)
230 setinet6sysctl(IPV6CTL_FORWARDING, 0);
232 /* warn if forwarding is up */
233 if (getinet6sysctl(IPV6CTL_FORWARDING))
234 warnx("kernel is configured as a router, not a host");
239 /* initialization to dump internal status to a file */
240 signal(SIGUSR1, rtsold_set_dump_file);
244 daemon(0, 0); /* act as a daemon */
247 * Open a socket for sending RS and receiving RA.
248 * This should be done before calling ifinit(), since the function
251 if ((s = sockopen()) < 0) {
252 warnmsg(LOG_ERR, __func__, "failed to open a socket");
257 set[0].events = POLLIN;
266 if ((rtsock = rtsock_open()) < 0) {
267 warnmsg(LOG_ERR, __func__, "failed to open a socket");
272 set[1].events = POLLIN;
279 fdmasks = howmany(maxfd + 1, NFDBITS) * sizeof(fd_mask);
280 if ((fdsetp = malloc(fdmasks)) == NULL) {
281 warnmsg(LOG_ERR, __func__, "malloc");
284 if ((selectfdp = malloc(fdmasks)) == NULL) {
285 warnmsg(LOG_ERR, __func__, "malloc");
290 /* configuration per interface */
292 warnmsg(LOG_ERR, __func__,
293 "failed to initialize interfaces");
297 argv = autoifprobe();
298 while (argv && *argv) {
299 if (ifconfig(*argv)) {
300 warnmsg(LOG_ERR, __func__,
301 "failed to initialize %s", *argv);
307 /* setup for probing default routers */
309 warnmsg(LOG_ERR, __func__,
310 "failed to setup for probing routers");
315 /* dump the current pid */
317 pid_t pid = getpid();
320 if ((fp = fopen(pidfilename, "w")) == NULL)
321 warnmsg(LOG_ERR, __func__,
322 "failed to open a pid log file(%s): %s",
323 pidfilename, strerror(errno));
325 fprintf(fp, "%d\n", pid);
330 memset(fdsetp, 0, fdmasks);
332 FD_SET(rtsock, fdsetp);
334 while (1) { /* main loop */
338 memcpy(selectfdp, fdsetp, fdmasks);
342 if (do_dump) { /* SIGUSR1 */
344 rtsold_dump_file(dumpfilename);
348 timeout = rtsol_check_timer();
353 /* if we have no timeout, we are done (or failed) */
357 /* if all interfaces have got RA packet, we are done */
358 TAILQ_FOREACH(ifi, &ifinfo_head, ifi_next) {
359 if (ifi->state != IFS_DOWN && ifi->racnt == 0)
366 e = poll(set, 2, timeout ? (timeout->tv_sec * 1000 + timeout->tv_usec / 1000) : INFTIM);
368 e = select(maxfd + 1, selectfdp, NULL, NULL, timeout);
371 if (e < 0 && errno != EINTR) {
372 warnmsg(LOG_ERR, __func__, "select: %s",
378 /* packet reception */
380 if (set[1].revents & POLLIN)
382 if (FD_ISSET(rtsock, selectfdp))
384 rtsock_input(rtsock);
386 if (set[0].revents & POLLIN)
388 if (FD_ISSET(s, selectfdp))
398 ifconfig(char *ifname)
401 struct sockaddr_dl *sdl;
404 if ((sdl = if_nametosdl(ifname)) == NULL) {
405 warnmsg(LOG_ERR, __func__,
406 "failed to get link layer information for %s", ifname);
409 if (find_ifinfo(sdl->sdl_index)) {
410 warnmsg(LOG_ERR, __func__,
411 "interface %s was already configured", ifname);
417 struct in6_ndireq nd;
420 if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
421 warnmsg(LOG_ERR, __func__, "socket() failed.");
424 memset(&nd, 0, sizeof(nd));
425 strlcpy(nd.ifname, ifname, sizeof(nd.ifname));
426 if (ioctl(s, SIOCGIFINFO_IN6, (caddr_t)&nd) < 0) {
427 warnmsg(LOG_ERR, __func__,
428 "cannot get accept_rtadv flag");
432 nd.ndi.flags |= ND6_IFF_ACCEPT_RTADV;
433 if (ioctl(s, SIOCSIFINFO_IN6, (caddr_t)&nd) < 0) {
434 warnmsg(LOG_ERR, __func__,
435 "cannot set accept_rtadv flag");
442 if ((ifi = malloc(sizeof(*ifi))) == NULL) {
443 warnmsg(LOG_ERR, __func__, "memory allocation failed");
447 memset(ifi, 0, sizeof(*ifi));
449 ifi->ifi_rdnss = IFI_DNSOPT_STATE_NOINFO;
450 ifi->ifi_dnssl = IFI_DNSOPT_STATE_NOINFO;
451 TAILQ_INIT(&ifi->ifi_rainfo);
452 strlcpy(ifi->ifname, ifname, sizeof(ifi->ifname));
454 /* construct a router solicitation message */
455 if (make_packet(ifi))
458 /* set link ID of this interface. */
460 if (inet_zoneid(AF_INET6, 2, ifname, &ifi->linkid))
463 /* XXX: assume interface IDs as link IDs */
464 ifi->linkid = ifi->sdl->sdl_index;
468 * check if the interface is available.
469 * also check if SIOCGIFMEDIA ioctl is OK on the interface.
472 ifi->active = interface_status(ifi);
473 if (!ifi->mediareqok) {
475 * probe routers periodically even if the link status
478 ifi->probeinterval = PROBE_INTERVAL;
481 /* activate interface: interface_up returns 0 on success */
482 flags = interface_up(ifi->ifname);
484 ifi->state = IFS_DELAY;
485 else if (flags == IFS_TENTATIVE)
486 ifi->state = IFS_TENTATIVE;
488 ifi->state = IFS_DOWN;
490 rtsol_timer_update(ifi);
492 TAILQ_INSERT_TAIL(&ifinfo_head, ifi, ifi_next);
506 while ((ifi = TAILQ_FIRST(&ifinfo_head)) != NULL) {
507 TAILQ_REMOVE(&ifinfo_head, ifi, ifi_next);
508 if (ifi->sdl != NULL)
510 if (ifi->rs_data != NULL)
518 ifreconfig(char *ifname)
520 struct ifinfo *ifi, *prev;
524 TAILQ_FOREACH(ifi, &ifinfo_head, ifi_next) {
525 if (strncmp(ifi->ifname, ifname, sizeof(ifi->ifname)) == 0)
529 prev->next = ifi->next;
531 rv = ifconfig(ifname);
533 /* reclaim it after ifconfig() in case ifname is pointer inside ifi */
544 find_rainfo(struct ifinfo *ifi, struct sockaddr_in6 *sin6)
548 TAILQ_FOREACH(rai, &ifi->ifi_rainfo, rai_next)
549 if (memcmp(&rai->rai_saddr.sin6_addr, &sin6->sin6_addr,
550 sizeof(rai->rai_saddr.sin6_addr)) == 0)
557 find_ifinfo(int ifindex)
561 TAILQ_FOREACH(ifi, &ifinfo_head, ifi_next) {
562 if (ifi->sdl->sdl_index == ifindex)
569 make_packet(struct ifinfo *ifi)
571 size_t packlen = sizeof(struct nd_router_solicit), lladdroptlen = 0;
572 struct nd_router_solicit *rs;
575 if ((lladdroptlen = lladdropt_length(ifi->sdl)) == 0) {
576 warnmsg(LOG_INFO, __func__,
577 "link-layer address option has null length"
578 " on %s. Treat as not included.", ifi->ifname);
580 packlen += lladdroptlen;
581 ifi->rs_datalen = packlen;
583 /* allocate buffer */
584 if ((buf = malloc(packlen)) == NULL) {
585 warnmsg(LOG_ERR, __func__,
586 "memory allocation failed for %s", ifi->ifname);
591 /* fill in the message */
592 rs = (struct nd_router_solicit *)buf;
593 rs->nd_rs_type = ND_ROUTER_SOLICIT;
596 rs->nd_rs_reserved = 0;
599 /* fill in source link-layer address option */
601 lladdropt_fill(ifi->sdl, (struct nd_opt_hdr *)buf);
606 static struct timeval *
607 rtsol_check_timer(void)
609 static struct timeval returnval;
610 struct timeval now, rtsol_timer;
616 gettimeofday(&now, NULL);
618 rtsol_timer = tm_max;
620 TAILQ_FOREACH(ifi, &ifinfo_head, ifi_next) {
621 if (timercmp(&ifi->expire, &now, <=)) {
622 warnmsg(LOG_DEBUG, __func__, "timer expiration on %s, "
623 "state = %d", ifi->ifname, ifi->state);
625 while((rai = TAILQ_FIRST(&ifi->ifi_rainfo)) != NULL) {
626 /* Remove all RA options. */
627 TAILQ_REMOVE(&ifi->ifi_rainfo, rai, rai_next);
628 while ((rao = TAILQ_FIRST(&rai->rai_ra_opt)) !=
630 TAILQ_REMOVE(&rai->rai_ra_opt, rao,
632 if (rao->rao_msg != NULL)
638 switch (ifi->state) {
641 /* interface_up returns 0 on success */
642 flags = interface_up(ifi->ifname);
644 ifi->state = IFS_DELAY;
645 else if (flags == IFS_TENTATIVE)
646 ifi->state = IFS_TENTATIVE;
648 ifi->state = IFS_DOWN;
652 int oldstatus = ifi->active;
655 ifi->active = interface_status(ifi);
657 if (oldstatus != ifi->active) {
658 warnmsg(LOG_DEBUG, __func__,
659 "%s status is changed"
662 oldstatus, ifi->active);
664 ifi->state = IFS_DELAY;
665 } else if (ifi->probeinterval &&
667 ifi->timer.tv_sec) <= 0) {
668 /* probe timer expired */
672 ifi->state = IFS_PROBE;
676 * If we need a probe, clear the previous
677 * status wrt the "other" configuration.
680 ifi->otherconfig = 0;
682 if (probe && mobile_node)
683 defrouter_probe(ifi);
687 ifi->state = IFS_PROBE;
691 if (ifi->probes < MAX_RTR_SOLICITATIONS)
694 warnmsg(LOG_INFO, __func__,
695 "No answer after sending %d RSs",
698 ifi->state = IFS_IDLE;
702 rtsol_timer_update(ifi);
704 /* Expiration check for RA options. */
707 TAILQ_FOREACH(rai, &ifi->ifi_rainfo, rai_next) {
708 TAILQ_FOREACH(rao, &rai->rai_ra_opt, rao_next) {
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 (timercmp(&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 (timercmp(&ifi->expire, &rtsol_timer, <))
732 rtsol_timer = ifi->expire;
735 if (timercmp(&rtsol_timer, &tm_max, ==)) {
736 warnmsg(LOG_DEBUG, __func__, "there is no timer");
738 } else if (timercmp(&rtsol_timer, &now, <))
739 /* this may occur when the interval is too small */
740 returnval.tv_sec = returnval.tv_usec = 0;
742 timersub(&rtsol_timer, &now, &returnval);
744 now.tv_sec += returnval.tv_sec;
745 now.tv_usec += returnval.tv_usec;
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 #ifndef HAVE_ARC4RANDOM
781 interval = random() % (MAX_RTR_SOLICITATION_DELAY * MILLION);
783 interval = arc4random_uniform(MAX_RTR_SOLICITATION_DELAY * MILLION);
785 ifi->timer.tv_sec = interval / MILLION;
786 ifi->timer.tv_usec = interval % MILLION;
789 if (ifi->probes < MAX_RTR_SOLICITATIONS)
790 ifi->timer.tv_sec = RTR_SOLICITATION_INTERVAL;
793 * After sending MAX_RTR_SOLICITATIONS solicitations,
794 * we're just waiting for possible replies; there
795 * will be no more solicitation. Thus, we change
796 * the timer value to MAX_RTR_SOLICITATION_DELAY based
797 * on RFC 2461, Section 6.3.7.
799 ifi->timer.tv_sec = MAX_RTR_SOLICITATION_DELAY;
803 warnmsg(LOG_ERR, __func__,
804 "illegal interface state(%d) on %s",
805 ifi->state, ifi->ifname);
809 /* reset the timer */
810 if (timercmp(&ifi->timer, &tm_max, ==)) {
811 ifi->expire = tm_max;
812 warnmsg(LOG_DEBUG, __func__,
813 "stop timer for %s", ifi->ifname);
815 gettimeofday(&now, NULL);
816 timeradd(&now, &ifi->timer, &ifi->expire);
818 now.tv_sec += ifi->timer.tv_sec;
819 now.tv_usec += ifi->timer.tv_usec;
820 warnmsg(LOG_DEBUG, __func__, "set timer for %s to %s",
821 ifi->ifname, sec2str(&now));
827 /* timer related utility functions */
828 #define MILLION 1000000
832 rtsold_set_dump_file(int sig __unused)
842 fprintf(stderr, "usage: rtsold [-adDfFm1] [-O script-name] "
843 "[-P pidfile] [-R script-name] interfaces...\n");
844 fprintf(stderr, "usage: rtsold [-dDfFm1] [-O script-name] "
845 "[-P pidfile] [-R script-name] -a\n");
847 fprintf(stderr, "usage: rtsol [-dDF] [-O script-name] "
848 "[-P pidfile] [-R script-name] interfaces...\n");
849 fprintf(stderr, "usage: rtsol [-dDF] [-O script-name] "
850 "[-P pidfile] [-R script-name] -a\n");
855 warnmsg(int priority, const char *func, const char *msg, ...)
862 if (priority <= log_upto) {
863 (void)vfprintf(stderr, msg, ap);
864 (void)fprintf(stderr, "\n");
867 snprintf(buf, sizeof(buf), "<%s> %s", func, msg);
869 vsyslog(priority, msg, ap);
875 * return a list of interfaces which is suitable to sending an RS.
880 static char **argv = NULL;
884 struct ifaddrs *ifap, *ifa, *target;
885 struct in6_ndireq nd;
896 if (getifaddrs(&ifap) != 0)
899 if (!Fflag && (s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
900 warnmsg(LOG_ERR, __func__, "socket");
905 /* find an ethernet */
906 for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
907 if ((ifa->ifa_flags & IFF_UP) == 0)
909 if ((ifa->ifa_flags & IFF_POINTOPOINT) != 0)
911 if ((ifa->ifa_flags & IFF_LOOPBACK) != 0)
913 if ((ifa->ifa_flags & IFF_MULTICAST) == 0)
916 if (ifa->ifa_addr->sa_family != AF_INET6)
920 for (i = 0; i < n; i++) {
921 if (strcmp(argv[i], ifa->ifa_name) == 0) {
930 * Skip the interfaces which IPv6 and/or accepting RA
934 memset(&nd, 0, sizeof(nd));
935 strlcpy(nd.ifname, ifa->ifa_name, sizeof(nd.ifname));
936 if (ioctl(s, SIOCGIFINFO_IN6, (caddr_t)&nd) < 0) {
937 warnmsg(LOG_ERR, __func__,
938 "ioctl(SIOCGIFINFO_IN6)");
941 if ((nd.ndi.flags & ND6_IFF_IFDISABLED))
943 if (!(nd.ndi.flags & ND6_IFF_ACCEPT_RTADV))
947 /* if we find multiple candidates, just warn. */
948 if (n != 0 && dflag > 1)
949 warnmsg(LOG_WARNING, __func__,
950 "multiple interfaces found");
952 a = (char **)realloc(argv, (n + 1) * sizeof(char **));
954 warnmsg(LOG_ERR, __func__, "realloc");
958 argv[n] = strdup(ifa->ifa_name);
960 warnmsg(LOG_ERR, __func__, "malloc");
967 a = (char **)realloc(argv, (n + 1) * sizeof(char **));
969 warnmsg(LOG_ERR, __func__, "realloc");
976 for (i = 0; i < n; i++)
977 warnmsg(LOG_WARNING, __func__, "probing %s",