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 */
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 timeval *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 timeval *timeout;
121 struct pollfd set[2];
123 fd_set *fdsetp, *selectfdp;
132 opts = "adDfFm1O:P:R:";
141 while ((ch = getopt(argc, argv, opts)) != -1) {
165 otherconf_script = optarg;
168 pidfilename = optarg;
171 resolvconf_script = optarg;
181 if ((!aflag && argc == 0) || (aflag && argc != 0)) {
188 log_upto = LOG_NOTICE;
192 ident = strrchr(argv0, '/');
197 openlog(ident, LOG_NDELAY|LOG_PID, LOG_DAEMON);
199 setlogmask(LOG_UPTO(log_upto));
202 if (otherconf_script && *otherconf_script != '/') {
203 errx(1, "configuration script (%s) must be an absolute path",
206 if (resolvconf_script && *resolvconf_script != '/') {
207 errx(1, "configuration script (%s) must be an absolute path",
210 if (pidfilename && *pidfilename != '/') {
211 errx(1, "pid filename (%s) must be an absolute path",
214 #ifndef HAVE_ARC4RANDOM
215 /* random value initialization */
216 srandom((u_long)time(NULL));
220 setinet6sysctl(IPV6CTL_FORWARDING, 0);
222 /* warn if forwarding is up */
223 if (getinet6sysctl(IPV6CTL_FORWARDING))
224 warnx("kernel is configured as a router, not a host");
228 /* initialization to dump internal status to a file */
229 signal(SIGUSR1, rtsold_set_dump_file);
233 daemon(0, 0); /* act as a daemon */
236 * Open a socket for sending RS and receiving RA.
237 * This should be done before calling ifinit(), since the function
240 if ((s = sockopen()) < 0) {
241 warnmsg(LOG_ERR, __func__, "failed to open a socket");
246 set[0].events = POLLIN;
255 if ((rtsock = rtsock_open()) < 0) {
256 warnmsg(LOG_ERR, __func__, "failed to open a socket");
261 set[1].events = POLLIN;
268 fdmasks = howmany(maxfd + 1, NFDBITS) * sizeof(fd_mask);
269 if ((fdsetp = malloc(fdmasks)) == NULL) {
270 warnmsg(LOG_ERR, __func__, "malloc");
273 if ((selectfdp = malloc(fdmasks)) == NULL) {
274 warnmsg(LOG_ERR, __func__, "malloc");
279 /* configuration per interface */
281 warnmsg(LOG_ERR, __func__,
282 "failed to initialize interfaces");
286 argv = autoifprobe();
287 while (argv && *argv) {
288 if (ifconfig(*argv)) {
289 warnmsg(LOG_ERR, __func__,
290 "failed to initialize %s", *argv);
296 /* setup for probing default routers */
298 warnmsg(LOG_ERR, __func__,
299 "failed to setup for probing routers");
304 /* dump the current pid */
306 pid_t pid = getpid();
309 if ((fp = fopen(pidfilename, "w")) == NULL)
310 warnmsg(LOG_ERR, __func__,
311 "failed to open a pid log file(%s): %s",
312 pidfilename, strerror(errno));
314 fprintf(fp, "%d\n", pid);
319 memset(fdsetp, 0, fdmasks);
321 FD_SET(rtsock, fdsetp);
323 while (1) { /* main loop */
327 memcpy(selectfdp, fdsetp, fdmasks);
331 if (do_dump) { /* SIGUSR1 */
333 rtsold_dump_file(dumpfilename);
337 timeout = rtsol_check_timer();
342 /* if we have no timeout, we are done (or failed) */
346 /* if all interfaces have got RA packet, we are done */
347 TAILQ_FOREACH(ifi, &ifinfo_head, ifi_next) {
348 if (ifi->state != IFS_DOWN && ifi->racnt == 0)
355 e = poll(set, 2, timeout ? (timeout->tv_sec * 1000 + timeout->tv_usec / 1000) : INFTIM);
357 e = select(maxfd + 1, selectfdp, NULL, NULL, timeout);
360 if (e < 0 && errno != EINTR) {
361 warnmsg(LOG_ERR, __func__, "select: %s",
367 /* packet reception */
369 if (set[1].revents & POLLIN)
371 if (FD_ISSET(rtsock, selectfdp))
373 rtsock_input(rtsock);
375 if (set[0].revents & POLLIN)
377 if (FD_ISSET(s, selectfdp))
387 ifconfig(char *ifname)
390 struct sockaddr_dl *sdl;
393 if ((sdl = if_nametosdl(ifname)) == NULL) {
394 warnmsg(LOG_ERR, __func__,
395 "failed to get link layer information for %s", ifname);
398 if (find_ifinfo(sdl->sdl_index)) {
399 warnmsg(LOG_ERR, __func__,
400 "interface %s was already configured", ifname);
405 if ((ifi = malloc(sizeof(*ifi))) == NULL) {
406 warnmsg(LOG_ERR, __func__, "memory allocation failed");
410 memset(ifi, 0, sizeof(*ifi));
412 ifi->ifi_rdnss = IFI_DNSOPT_STATE_NOINFO;
413 ifi->ifi_dnssl = IFI_DNSOPT_STATE_NOINFO;
414 TAILQ_INIT(&ifi->ifi_rainfo);
415 strlcpy(ifi->ifname, ifname, sizeof(ifi->ifname));
417 /* construct a router solicitation message */
418 if (make_packet(ifi))
421 /* set link ID of this interface. */
423 if (inet_zoneid(AF_INET6, 2, ifname, &ifi->linkid))
426 /* XXX: assume interface IDs as link IDs */
427 ifi->linkid = ifi->sdl->sdl_index;
431 * check if the interface is available.
432 * also check if SIOCGIFMEDIA ioctl is OK on the interface.
435 ifi->active = interface_status(ifi);
436 if (!ifi->mediareqok) {
438 * probe routers periodically even if the link status
441 ifi->probeinterval = PROBE_INTERVAL;
444 /* activate interface: interface_up returns 0 on success */
445 flags = interface_up(ifi->ifname);
447 ifi->state = IFS_DELAY;
448 else if (flags == IFS_TENTATIVE)
449 ifi->state = IFS_TENTATIVE;
451 ifi->state = IFS_DOWN;
453 rtsol_timer_update(ifi);
455 TAILQ_INSERT_TAIL(&ifinfo_head, ifi, ifi_next);
469 while ((ifi = TAILQ_FIRST(&ifinfo_head)) != NULL) {
470 TAILQ_REMOVE(&ifinfo_head, ifi, ifi_next);
471 if (ifi->sdl != NULL)
473 if (ifi->rs_data != NULL)
481 ifreconfig(char *ifname)
483 struct ifinfo *ifi, *prev;
487 TAILQ_FOREACH(ifi, &ifinfo_head, ifi_next) {
488 if (strncmp(ifi->ifname, ifname, sizeof(ifi->ifname)) == 0)
492 prev->next = ifi->next;
494 rv = ifconfig(ifname);
496 /* reclaim it after ifconfig() in case ifname is pointer inside ifi */
507 find_rainfo(struct ifinfo *ifi, struct sockaddr_in6 *sin6)
511 TAILQ_FOREACH(rai, &ifi->ifi_rainfo, rai_next)
512 if (memcmp(&rai->rai_saddr.sin6_addr, &sin6->sin6_addr,
513 sizeof(rai->rai_saddr.sin6_addr)) == 0)
520 find_ifinfo(int ifindex)
524 TAILQ_FOREACH(ifi, &ifinfo_head, ifi_next) {
525 if (ifi->sdl->sdl_index == ifindex)
532 make_packet(struct ifinfo *ifi)
534 size_t packlen = sizeof(struct nd_router_solicit), lladdroptlen = 0;
535 struct nd_router_solicit *rs;
538 if ((lladdroptlen = lladdropt_length(ifi->sdl)) == 0) {
539 warnmsg(LOG_INFO, __func__,
540 "link-layer address option has null length"
541 " on %s. Treat as not included.", ifi->ifname);
543 packlen += lladdroptlen;
544 ifi->rs_datalen = packlen;
546 /* allocate buffer */
547 if ((buf = malloc(packlen)) == NULL) {
548 warnmsg(LOG_ERR, __func__,
549 "memory allocation failed for %s", ifi->ifname);
554 /* fill in the message */
555 rs = (struct nd_router_solicit *)buf;
556 rs->nd_rs_type = ND_ROUTER_SOLICIT;
559 rs->nd_rs_reserved = 0;
562 /* fill in source link-layer address option */
564 lladdropt_fill(ifi->sdl, (struct nd_opt_hdr *)buf);
569 static struct timeval *
570 rtsol_check_timer(void)
572 static struct timeval returnval;
573 struct timeval now, rtsol_timer;
579 gettimeofday(&now, NULL);
581 rtsol_timer = tm_max;
583 TAILQ_FOREACH(ifi, &ifinfo_head, ifi_next) {
584 if (timercmp(&ifi->expire, &now, <=)) {
585 warnmsg(LOG_DEBUG, __func__, "timer expiration on %s, "
586 "state = %d", ifi->ifname, ifi->state);
588 while((rai = TAILQ_FIRST(&ifi->ifi_rainfo)) != NULL) {
589 /* Remove all RA options. */
590 TAILQ_REMOVE(&ifi->ifi_rainfo, rai, rai_next);
591 while ((rao = TAILQ_FIRST(&rai->rai_ra_opt)) !=
593 TAILQ_REMOVE(&rai->rai_ra_opt, rao,
595 if (rao->rao_msg != NULL)
601 switch (ifi->state) {
604 /* interface_up returns 0 on success */
605 flags = interface_up(ifi->ifname);
607 ifi->state = IFS_DELAY;
608 else if (flags == IFS_TENTATIVE)
609 ifi->state = IFS_TENTATIVE;
611 ifi->state = IFS_DOWN;
615 int oldstatus = ifi->active;
618 ifi->active = interface_status(ifi);
620 if (oldstatus != ifi->active) {
621 warnmsg(LOG_DEBUG, __func__,
622 "%s status is changed"
625 oldstatus, ifi->active);
627 ifi->state = IFS_DELAY;
628 } else if (ifi->probeinterval &&
630 ifi->timer.tv_sec) <= 0) {
631 /* probe timer expired */
635 ifi->state = IFS_PROBE;
639 * If we need a probe, clear the previous
640 * status wrt the "other" configuration.
643 ifi->otherconfig = 0;
645 if (probe && mobile_node)
646 defrouter_probe(ifi);
650 ifi->state = IFS_PROBE;
654 if (ifi->probes < MAX_RTR_SOLICITATIONS)
657 warnmsg(LOG_INFO, __func__,
658 "No answer after sending %d RSs",
661 ifi->state = IFS_IDLE;
665 rtsol_timer_update(ifi);
667 /* Expiration check for RA options. */
670 TAILQ_FOREACH(rai, &ifi->ifi_rainfo, rai_next) {
671 TAILQ_FOREACH(rao, &rai->rai_ra_opt, rao_next) {
672 warnmsg(LOG_DEBUG, __func__,
673 "RA expiration timer: "
674 "type=%d, msg=%s, expire=%s",
675 rao->rao_type, (char *)rao->rao_msg,
676 sec2str(&rao->rao_expire));
677 if (timercmp(&now, &rao->rao_expire,
679 warnmsg(LOG_DEBUG, __func__,
680 "RA expiration timer: "
682 TAILQ_REMOVE(&rai->rai_ra_opt,
684 if (rao->rao_msg != NULL)
694 if (timercmp(&ifi->expire, &rtsol_timer, <))
695 rtsol_timer = ifi->expire;
698 if (timercmp(&rtsol_timer, &tm_max, ==)) {
699 warnmsg(LOG_DEBUG, __func__, "there is no timer");
701 } else if (timercmp(&rtsol_timer, &now, <))
702 /* this may occur when the interval is too small */
703 returnval.tv_sec = returnval.tv_usec = 0;
705 timersub(&rtsol_timer, &now, &returnval);
707 now.tv_sec += returnval.tv_sec;
708 now.tv_usec += returnval.tv_usec;
709 warnmsg(LOG_DEBUG, __func__, "New timer is %s",
716 rtsol_timer_update(struct ifinfo *ifi)
718 #define MILLION 1000000
719 #define DADRETRY 10 /* XXX: adhoc */
723 bzero(&ifi->timer, sizeof(ifi->timer));
725 switch (ifi->state) {
728 if (++ifi->dadcount > DADRETRY) {
730 ifi->timer.tv_sec = PROBE_INTERVAL;
732 ifi->timer.tv_sec = 1;
736 /* XXX should be configurable */
737 ifi->timer.tv_sec = 3;
740 ifi->timer = tm_max; /* stop timer(valid?) */
743 #ifndef HAVE_ARC4RANDOM
744 interval = random() % (MAX_RTR_SOLICITATION_DELAY * MILLION);
746 interval = arc4random_uniform(MAX_RTR_SOLICITATION_DELAY * MILLION);
748 ifi->timer.tv_sec = interval / MILLION;
749 ifi->timer.tv_usec = interval % MILLION;
752 if (ifi->probes < MAX_RTR_SOLICITATIONS)
753 ifi->timer.tv_sec = RTR_SOLICITATION_INTERVAL;
756 * After sending MAX_RTR_SOLICITATIONS solicitations,
757 * we're just waiting for possible replies; there
758 * will be no more solicitation. Thus, we change
759 * the timer value to MAX_RTR_SOLICITATION_DELAY based
760 * on RFC 2461, Section 6.3.7.
762 ifi->timer.tv_sec = MAX_RTR_SOLICITATION_DELAY;
766 warnmsg(LOG_ERR, __func__,
767 "illegal interface state(%d) on %s",
768 ifi->state, ifi->ifname);
772 /* reset the timer */
773 if (timercmp(&ifi->timer, &tm_max, ==)) {
774 ifi->expire = tm_max;
775 warnmsg(LOG_DEBUG, __func__,
776 "stop timer for %s", ifi->ifname);
778 gettimeofday(&now, NULL);
779 timeradd(&now, &ifi->timer, &ifi->expire);
781 now.tv_sec += ifi->timer.tv_sec;
782 now.tv_usec += ifi->timer.tv_usec;
783 warnmsg(LOG_DEBUG, __func__, "set timer for %s to %s",
784 ifi->ifname, sec2str(&now));
790 /* timer related utility functions */
791 #define MILLION 1000000
795 rtsold_set_dump_file(int sig __unused)
805 fprintf(stderr, "usage: rtsold [-adDfFm1] [-O script-name] "
806 "[-P pidfile] [-R script-name] interfaces...\n");
807 fprintf(stderr, "usage: rtsold [-dDfFm1] [-O script-name] "
808 "[-P pidfile] [-R script-name] -a\n");
810 fprintf(stderr, "usage: rtsol [-dDF] [-O script-name] "
811 "[-P pidfile] [-R script-name] interfaces...\n");
812 fprintf(stderr, "usage: rtsol [-dDF] [-O script-name] "
813 "[-P pidfile] [-R script-name] -a\n");
818 warnmsg(int priority, const char *func, const char *msg, ...)
825 if (priority <= log_upto) {
826 (void)vfprintf(stderr, msg, ap);
827 (void)fprintf(stderr, "\n");
830 snprintf(buf, sizeof(buf), "<%s> %s", func, msg);
832 vsyslog(priority, msg, ap);
838 * return a list of interfaces which is suitable to sending an RS.
843 static char **argv = NULL;
847 struct ifaddrs *ifap, *ifa, *target;
848 struct in6_ndireq nd;
859 if (getifaddrs(&ifap) != 0)
862 if (!Fflag && (s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
863 warnmsg(LOG_ERR, __func__, "socket");
868 /* find an ethernet */
869 for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
870 if ((ifa->ifa_flags & IFF_UP) == 0)
872 if ((ifa->ifa_flags & IFF_POINTOPOINT) != 0)
874 if ((ifa->ifa_flags & IFF_LOOPBACK) != 0)
876 if ((ifa->ifa_flags & IFF_MULTICAST) == 0)
879 if (ifa->ifa_addr->sa_family != AF_INET6)
883 for (i = 0; i < n; i++) {
884 if (strcmp(argv[i], ifa->ifa_name) == 0) {
893 * Skip the interfaces which IPv6 and/or accepting RA
897 memset(&nd, 0, sizeof(nd));
898 strlcpy(nd.ifname, ifa->ifa_name, sizeof(nd.ifname));
899 if (ioctl(s, SIOCGIFINFO_IN6, (caddr_t)&nd) < 0) {
900 warnmsg(LOG_ERR, __func__,
901 "ioctl(SIOCGIFINFO_IN6)");
904 if ((nd.ndi.flags & ND6_IFF_IFDISABLED))
906 if (!(nd.ndi.flags & ND6_IFF_ACCEPT_RTADV))
910 /* if we find multiple candidates, just warn. */
911 if (n != 0 && dflag > 1)
912 warnmsg(LOG_WARNING, __func__,
913 "multiple interfaces found");
915 a = (char **)realloc(argv, (n + 1) * sizeof(char **));
917 warnmsg(LOG_ERR, __func__, "realloc");
921 argv[n] = strdup(ifa->ifa_name);
923 warnmsg(LOG_ERR, __func__, "malloc");
930 a = (char **)realloc(argv, (n + 1) * sizeof(char **));
932 warnmsg(LOG_ERR, __func__, "realloc");
939 for (i = 0; i < n; i++)
940 warnmsg(LOG_WARNING, __func__, "probing %s",