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>
64 #define RTSOL_DUMPFILE "/var/run/rtsold.dump";
65 #define RTSOL_PIDFILE "/var/run/rtsold.pid";
67 struct timespec tm_max;
68 static int log_upto = 999;
71 int Fflag = 0; /* force setting sysctl parameters */
76 const char *otherconf_script;
77 const char *resolvconf_script = "/sbin/resolvconf";
79 /* protocol constants */
80 #define MAX_RTR_SOLICITATION_DELAY 1 /* second */
81 #define RTR_SOLICITATION_INTERVAL 4 /* seconds */
82 #define MAX_RTR_SOLICITATIONS 3 /* times */
85 * implementation dependent constants in seconds
86 * XXX: should be configurable
88 #define PROBE_INTERVAL 60
90 /* static variables and functions */
91 static int mobile_node = 0;
92 static const char *pidfilename = RTSOL_PIDFILE;
96 static const char *dumpfilename = RTSOL_DUMPFILE;
100 static int ifreconfig(char *);
103 static int make_packet(struct ifinfo *);
104 static struct timespec *rtsol_check_timer(void);
107 static void rtsold_set_dump_file(int);
109 static void usage(void);
112 main(int argc, char **argv)
115 struct timespec *timeout;
117 struct pollfd set[2];
123 opts = "adDfFm1O:p:R:u";
132 while ((ch = getopt(argc, argv, opts)) != -1) {
156 otherconf_script = optarg;
159 pidfilename = optarg;
162 resolvconf_script = optarg;
175 if ((!aflag && argc == 0) || (aflag && argc != 0)) {
180 /* Generate maximum time in timespec. */
181 tm_max.tv_sec = (-1) & ~((time_t)1 << ((sizeof(tm_max.tv_sec) * 8) - 1));
182 tm_max.tv_nsec = (-1) & ~((long)1 << ((sizeof(tm_max.tv_nsec) * 8) - 1));
186 log_upto = LOG_DEBUG;
190 log_upto = LOG_NOTICE;
195 ident = strrchr(argv0, '/');
200 openlog(ident, LOG_NDELAY|LOG_PID, LOG_DAEMON);
202 setlogmask(LOG_UPTO(log_upto));
205 if (otherconf_script && *otherconf_script != '/') {
206 errx(1, "configuration script (%s) must be an absolute path",
209 if (resolvconf_script && *resolvconf_script != '/') {
210 errx(1, "configuration script (%s) must be an absolute path",
213 if (pidfilename && *pidfilename != '/') {
214 errx(1, "pid filename (%s) must be an absolute path",
218 #if (__FreeBSD_version < 900000)
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");
229 /* initialization to dump internal status to a file */
230 signal(SIGUSR1, rtsold_set_dump_file);
234 daemon(0, 0); /* act as a daemon */
237 * Open a socket for sending RS and receiving RA.
238 * This should be done before calling ifinit(), since the function
241 if ((s = sockopen()) < 0) {
242 warnmsg(LOG_ERR, __func__, "failed to open a socket");
246 set[0].events = POLLIN;
249 if ((rtsock = rtsock_open()) < 0) {
250 warnmsg(LOG_ERR, __func__, "failed to open a socket");
254 set[1].events = POLLIN;
256 /* configuration per interface */
258 warnmsg(LOG_ERR, __func__,
259 "failed to initialize interfaces");
263 argv = autoifprobe();
264 while (argv && *argv) {
265 if (ifconfig(*argv)) {
266 warnmsg(LOG_ERR, __func__,
267 "failed to initialize %s", *argv);
273 /* setup for probing default routers */
275 warnmsg(LOG_ERR, __func__,
276 "failed to setup for probing routers");
281 /* dump the current pid */
283 pid_t pid = getpid();
286 if ((fp = fopen(pidfilename, "w")) == NULL)
287 warnmsg(LOG_ERR, __func__,
288 "failed to open a pid log file(%s): %s",
289 pidfilename, strerror(errno));
291 fprintf(fp, "%d\n", pid);
295 while (1) { /* main loop */
298 if (do_dump) { /* SIGUSR1 */
300 rtsold_dump_file(dumpfilename);
304 timeout = rtsol_check_timer();
309 /* if we have no timeout, we are done (or failed) */
313 /* if all interfaces have got RA packet, we are done */
314 TAILQ_FOREACH(ifi, &ifinfo_head, ifi_next) {
315 if (ifi->state != IFS_DOWN && ifi->racnt == 0)
321 e = poll(set, 2, timeout ? (timeout->tv_sec * 1000 + timeout->tv_nsec / 1000 / 1000) : INFTIM);
323 if (e < 0 && errno != EINTR) {
324 warnmsg(LOG_ERR, __func__, "select: %s",
330 /* packet reception */
331 if (set[1].revents & POLLIN)
332 rtsock_input(rtsock);
333 if (set[0].revents & POLLIN)
342 ifconfig(char *ifname)
345 struct sockaddr_dl *sdl;
348 if ((sdl = if_nametosdl(ifname)) == NULL) {
349 warnmsg(LOG_ERR, __func__,
350 "failed to get link layer information for %s", ifname);
353 if (find_ifinfo(sdl->sdl_index)) {
354 warnmsg(LOG_ERR, __func__,
355 "interface %s was already configured", ifname);
361 struct in6_ndireq nd;
364 if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
365 warnmsg(LOG_ERR, __func__, "socket() failed.");
368 memset(&nd, 0, sizeof(nd));
369 strlcpy(nd.ifname, ifname, sizeof(nd.ifname));
370 if (ioctl(s, SIOCGIFINFO_IN6, (caddr_t)&nd) < 0) {
371 warnmsg(LOG_ERR, __func__,
372 "cannot get accept_rtadv flag");
376 nd.ndi.flags |= ND6_IFF_ACCEPT_RTADV;
377 if (ioctl(s, SIOCSIFINFO_IN6, (caddr_t)&nd) < 0) {
378 warnmsg(LOG_ERR, __func__,
379 "cannot set accept_rtadv flag");
386 if ((ifi = malloc(sizeof(*ifi))) == NULL) {
387 warnmsg(LOG_ERR, __func__, "memory allocation failed");
391 memset(ifi, 0, sizeof(*ifi));
393 ifi->ifi_rdnss = IFI_DNSOPT_STATE_NOINFO;
394 ifi->ifi_dnssl = IFI_DNSOPT_STATE_NOINFO;
395 TAILQ_INIT(&ifi->ifi_rainfo);
396 strlcpy(ifi->ifname, ifname, sizeof(ifi->ifname));
398 /* construct a router solicitation message */
399 if (make_packet(ifi))
402 /* set link ID of this interface. */
404 if (inet_zoneid(AF_INET6, 2, ifname, &ifi->linkid))
407 /* XXX: assume interface IDs as link IDs */
408 ifi->linkid = ifi->sdl->sdl_index;
412 * check if the interface is available.
413 * also check if SIOCGIFMEDIA ioctl is OK on the interface.
416 ifi->active = interface_status(ifi);
417 if (!ifi->mediareqok) {
419 * probe routers periodically even if the link status
422 ifi->probeinterval = PROBE_INTERVAL;
425 /* activate interface: interface_up returns 0 on success */
426 flags = interface_up(ifi->ifname);
428 ifi->state = IFS_DELAY;
429 else if (flags == IFS_TENTATIVE)
430 ifi->state = IFS_TENTATIVE;
432 ifi->state = IFS_DOWN;
434 rtsol_timer_update(ifi);
436 TAILQ_INSERT_TAIL(&ifinfo_head, ifi, ifi_next);
450 while ((ifi = TAILQ_FIRST(&ifinfo_head)) != NULL) {
451 TAILQ_REMOVE(&ifinfo_head, ifi, ifi_next);
452 if (ifi->sdl != NULL)
454 if (ifi->rs_data != NULL)
462 ifreconfig(char *ifname)
464 struct ifinfo *ifi, *prev;
468 TAILQ_FOREACH(ifi, &ifinfo_head, ifi_next) {
469 if (strncmp(ifi->ifname, ifname, sizeof(ifi->ifname)) == 0)
473 prev->next = ifi->next;
475 rv = ifconfig(ifname);
477 /* reclaim it after ifconfig() in case ifname is pointer inside ifi */
488 find_rainfo(struct ifinfo *ifi, struct sockaddr_in6 *sin6)
492 TAILQ_FOREACH(rai, &ifi->ifi_rainfo, rai_next)
493 if (memcmp(&rai->rai_saddr.sin6_addr, &sin6->sin6_addr,
494 sizeof(rai->rai_saddr.sin6_addr)) == 0)
501 find_ifinfo(int ifindex)
505 TAILQ_FOREACH(ifi, &ifinfo_head, ifi_next) {
506 if (ifi->sdl->sdl_index == ifindex)
513 make_packet(struct ifinfo *ifi)
515 size_t packlen = sizeof(struct nd_router_solicit), lladdroptlen = 0;
516 struct nd_router_solicit *rs;
519 if ((lladdroptlen = lladdropt_length(ifi->sdl)) == 0) {
520 warnmsg(LOG_INFO, __func__,
521 "link-layer address option has null length"
522 " on %s. Treat as not included.", ifi->ifname);
524 packlen += lladdroptlen;
525 ifi->rs_datalen = packlen;
527 /* allocate buffer */
528 if ((buf = malloc(packlen)) == NULL) {
529 warnmsg(LOG_ERR, __func__,
530 "memory allocation failed for %s", ifi->ifname);
535 /* fill in the message */
536 rs = (struct nd_router_solicit *)buf;
537 rs->nd_rs_type = ND_ROUTER_SOLICIT;
540 rs->nd_rs_reserved = 0;
543 /* fill in source link-layer address option */
545 lladdropt_fill(ifi->sdl, (struct nd_opt_hdr *)buf);
550 static struct timespec *
551 rtsol_check_timer(void)
553 static struct timespec returnval;
554 struct timespec now, rtsol_timer;
557 struct ra_opt *rao, *raotmp;
560 clock_gettime(CLOCK_MONOTONIC_FAST, &now);
562 rtsol_timer = tm_max;
564 TAILQ_FOREACH(ifi, &ifinfo_head, ifi_next) {
565 if (TS_CMP(&ifi->expire, &now, <=)) {
566 warnmsg(LOG_DEBUG, __func__, "timer expiration on %s, "
567 "state = %d", ifi->ifname, ifi->state);
569 while((rai = TAILQ_FIRST(&ifi->ifi_rainfo)) != NULL) {
570 /* Remove all RA options. */
571 TAILQ_REMOVE(&ifi->ifi_rainfo, rai, rai_next);
572 while ((rao = TAILQ_FIRST(&rai->rai_ra_opt)) !=
574 TAILQ_REMOVE(&rai->rai_ra_opt, rao,
576 if (rao->rao_msg != NULL)
582 switch (ifi->state) {
585 /* interface_up returns 0 on success */
586 flags = interface_up(ifi->ifname);
588 ifi->state = IFS_DELAY;
589 else if (flags == IFS_TENTATIVE)
590 ifi->state = IFS_TENTATIVE;
592 ifi->state = IFS_DOWN;
596 int oldstatus = ifi->active;
599 ifi->active = interface_status(ifi);
601 if (oldstatus != ifi->active) {
602 warnmsg(LOG_DEBUG, __func__,
603 "%s status is changed"
606 oldstatus, ifi->active);
608 ifi->state = IFS_DELAY;
609 } else if (ifi->probeinterval &&
611 ifi->timer.tv_sec) <= 0) {
612 /* probe timer expired */
616 ifi->state = IFS_PROBE;
620 * If we need a probe, clear the previous
621 * status wrt the "other" configuration.
624 ifi->otherconfig = 0;
626 if (probe && mobile_node)
627 defrouter_probe(ifi);
631 ifi->state = IFS_PROBE;
635 if (ifi->probes < MAX_RTR_SOLICITATIONS)
638 warnmsg(LOG_INFO, __func__,
639 "No answer after sending %d RSs",
642 ifi->state = IFS_IDLE;
646 rtsol_timer_update(ifi);
648 /* Expiration check for RA options. */
651 TAILQ_FOREACH(rai, &ifi->ifi_rainfo, rai_next) {
652 TAILQ_FOREACH_SAFE(rao, &rai->rai_ra_opt,
654 warnmsg(LOG_DEBUG, __func__,
655 "RA expiration timer: "
656 "type=%d, msg=%s, expire=%s",
657 rao->rao_type, (char *)rao->rao_msg,
658 sec2str(&rao->rao_expire));
659 if (TS_CMP(&now, &rao->rao_expire,
661 warnmsg(LOG_DEBUG, __func__,
662 "RA expiration timer: "
664 TAILQ_REMOVE(&rai->rai_ra_opt,
666 if (rao->rao_msg != NULL)
676 if (TS_CMP(&ifi->expire, &rtsol_timer, <))
677 rtsol_timer = ifi->expire;
680 if (TS_CMP(&rtsol_timer, &tm_max, ==)) {
681 warnmsg(LOG_DEBUG, __func__, "there is no timer");
683 } else if (TS_CMP(&rtsol_timer, &now, <))
684 /* this may occur when the interval is too small */
685 returnval.tv_sec = returnval.tv_nsec = 0;
687 TS_SUB(&rtsol_timer, &now, &returnval);
689 now.tv_sec += returnval.tv_sec;
690 now.tv_nsec += returnval.tv_nsec;
691 warnmsg(LOG_DEBUG, __func__, "New timer is %s",
698 rtsol_timer_update(struct ifinfo *ifi)
700 #define MILLION 1000000
701 #define DADRETRY 10 /* XXX: adhoc */
705 bzero(&ifi->timer, sizeof(ifi->timer));
707 switch (ifi->state) {
710 if (++ifi->dadcount > DADRETRY) {
712 ifi->timer.tv_sec = PROBE_INTERVAL;
714 ifi->timer.tv_sec = 1;
718 /* XXX should be configurable */
719 ifi->timer.tv_sec = 3;
722 ifi->timer = tm_max; /* stop timer(valid?) */
725 interval = arc4random_uniform(MAX_RTR_SOLICITATION_DELAY * MILLION);
726 ifi->timer.tv_sec = interval / MILLION;
727 ifi->timer.tv_nsec = (interval % MILLION) * 1000;
730 if (ifi->probes < MAX_RTR_SOLICITATIONS)
731 ifi->timer.tv_sec = RTR_SOLICITATION_INTERVAL;
734 * After sending MAX_RTR_SOLICITATIONS solicitations,
735 * we're just waiting for possible replies; there
736 * will be no more solicitation. Thus, we change
737 * the timer value to MAX_RTR_SOLICITATION_DELAY based
738 * on RFC 2461, Section 6.3.7.
740 ifi->timer.tv_sec = MAX_RTR_SOLICITATION_DELAY;
744 warnmsg(LOG_ERR, __func__,
745 "illegal interface state(%d) on %s",
746 ifi->state, ifi->ifname);
750 /* reset the timer */
751 if (TS_CMP(&ifi->timer, &tm_max, ==)) {
752 ifi->expire = tm_max;
753 warnmsg(LOG_DEBUG, __func__,
754 "stop timer for %s", ifi->ifname);
756 clock_gettime(CLOCK_MONOTONIC_FAST, &now);
757 TS_ADD(&now, &ifi->timer, &ifi->expire);
759 now.tv_sec += ifi->timer.tv_sec;
760 now.tv_nsec += ifi->timer.tv_nsec;
761 warnmsg(LOG_DEBUG, __func__, "set timer for %s to %s",
762 ifi->ifname, sec2str(&now));
768 /* timer related utility functions */
769 #define MILLION 1000000
773 rtsold_set_dump_file(int sig __unused)
783 fprintf(stderr, "usage: rtsold [-dDfFm1] [-O script-name] "
784 "[-p pidfile] [-R script-name] interface ...\n");
785 fprintf(stderr, "usage: rtsold [-dDfFm1] [-O script-name] "
786 "[-p pidfile] [-R script-name] -a\n");
788 fprintf(stderr, "usage: rtsol [-dDF] [-O script-name] "
789 "[-p pidfile] [-R script-name] interface ...\n");
790 fprintf(stderr, "usage: rtsol [-dDF] [-O script-name] "
791 "[-p pidfile] [-R script-name] -a\n");
796 warnmsg(int priority, const char *func, const char *msg, ...)
803 if (priority <= log_upto) {
804 (void)vfprintf(stderr, msg, ap);
805 (void)fprintf(stderr, "\n");
808 snprintf(buf, sizeof(buf), "<%s> %s", func, msg);
810 vsyslog(priority, msg, ap);
816 * return a list of interfaces which is suitable to sending an RS.
821 static char **argv = NULL;
825 struct ifaddrs *ifap, *ifa;
826 struct in6_ndireq nd;
837 if (getifaddrs(&ifap) != 0)
840 if (!Fflag && (s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
841 warnmsg(LOG_ERR, __func__, "socket");
845 /* find an ethernet */
846 for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
847 if ((ifa->ifa_flags & IFF_UP) == 0)
849 if ((ifa->ifa_flags & IFF_POINTOPOINT) != 0)
851 if ((ifa->ifa_flags & IFF_LOOPBACK) != 0)
853 if ((ifa->ifa_flags & IFF_MULTICAST) == 0)
856 if (ifa->ifa_addr->sa_family != AF_INET6)
860 for (i = 0; i < n; i++) {
861 if (strcmp(argv[i], ifa->ifa_name) == 0) {
870 * Skip the interfaces which IPv6 and/or accepting RA
874 memset(&nd, 0, sizeof(nd));
875 strlcpy(nd.ifname, ifa->ifa_name, sizeof(nd.ifname));
876 if (ioctl(s, SIOCGIFINFO_IN6, (caddr_t)&nd) < 0) {
877 warnmsg(LOG_ERR, __func__,
878 "ioctl(SIOCGIFINFO_IN6)");
881 if ((nd.ndi.flags & ND6_IFF_IFDISABLED))
883 if (!(nd.ndi.flags & ND6_IFF_ACCEPT_RTADV))
887 /* if we find multiple candidates, just warn. */
888 if (n != 0 && dflag > 1)
889 warnmsg(LOG_WARNING, __func__,
890 "multiple interfaces found");
892 a = realloc(argv, (n + 1) * sizeof(char *));
894 warnmsg(LOG_ERR, __func__, "realloc");
898 argv[n] = strdup(ifa->ifa_name);
900 warnmsg(LOG_ERR, __func__, "malloc");
907 a = realloc(argv, (n + 1) * sizeof(char *));
909 warnmsg(LOG_ERR, __func__, "realloc");
916 for (i = 0; i < n; i++)
917 warnmsg(LOG_WARNING, __func__, "probing %s",