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>
48 #include <netinet6/nd6.h>
66 struct ifinfo *iflist;
67 struct timeval tm_max = {0x7fffffff, 0x7fffffff};
68 static int log_upto = 999;
70 static int Fflag = 0; /* force setting sysctl parameters */
75 char *otherconf_script;
77 /* protocol constants */
78 #define MAX_RTR_SOLICITATION_DELAY 1 /* second */
79 #define RTR_SOLICITATION_INTERVAL 4 /* seconds */
80 #define MAX_RTR_SOLICITATIONS 3 /* times */
83 * implementation dependent constants in seconds
84 * XXX: should be configurable
86 #define PROBE_INTERVAL 60
88 int main(int, char **);
90 /* static variables and functions */
91 static int mobile_node = 0;
94 static char *dumpfilename = "/var/run/rtsold.dump"; /* XXX: should be configurable */
97 static char *pidfilename = "/var/run/rtsold.pid"; /* should be configurable */
101 static int ifreconfig(char *);
103 static int make_packet(struct ifinfo *);
104 static struct timeval *rtsol_check_timer(void);
107 static void rtsold_set_dump_file(int);
109 static void usage(char *);
112 main(int argc, char **argv)
115 struct timeval *timeout;
118 struct pollfd set[2];
120 fd_set *fdsetp, *selectfdp;
132 if (argv0 && argv0[strlen(argv0) - 1] != 'd') {
139 while ((ch = getopt(argc, argv, opts)) != -1) {
163 otherconf_script = optarg;
173 if ((!aflag && argc == 0) || (aflag && argc != 0)) {
180 log_upto = LOG_NOTICE;
184 ident = strrchr(argv0, '/');
189 openlog(ident, LOG_NDELAY|LOG_PID, LOG_DAEMON);
191 setlogmask(LOG_UPTO(log_upto));
194 if (otherconf_script && *otherconf_script != '/') {
195 errx(1, "configuration script (%s) must be an absolute path",
199 #ifndef HAVE_ARC4RANDOM
200 /* random value initialization */
201 srandom((u_long)time(NULL));
205 setinet6sysctl(IPV6CTL_ACCEPT_RTADV, 1);
206 setinet6sysctl(IPV6CTL_FORWARDING, 0);
208 /* warn if accept_rtadv is down */
209 if (!getinet6sysctl(IPV6CTL_ACCEPT_RTADV))
210 warnx("kernel is configured not to accept RAs");
211 /* warn if forwarding is up */
212 if (getinet6sysctl(IPV6CTL_FORWARDING))
213 warnx("kernel is configured as a router, not a host");
217 /* initialization to dump internal status to a file */
218 signal(SIGUSR1, rtsold_set_dump_file);
222 daemon(0, 0); /* act as a daemon */
225 * Open a socket for sending RS and receiving RA.
226 * This should be done before calling ifinit(), since the function
229 if ((s = sockopen()) < 0) {
230 warnmsg(LOG_ERR, __func__, "failed to open a socket");
236 set[0].events = POLLIN;
245 if ((rtsock = rtsock_open()) < 0) {
246 warnmsg(LOG_ERR, __func__, "failed to open a socket");
252 set[1].events = POLLIN;
259 fdmasks = howmany(maxfd + 1, NFDBITS) * sizeof(fd_mask);
260 if ((fdsetp = malloc(fdmasks)) == NULL) {
264 if ((selectfdp = malloc(fdmasks)) == NULL) {
270 /* configuration per interface */
272 warnmsg(LOG_ERR, __func__,
273 "failed to initialize interfaces");
278 argv = autoifprobe();
279 while (argv && *argv) {
280 if (ifconfig(*argv)) {
281 warnmsg(LOG_ERR, __func__,
282 "failed to initialize %s", *argv);
289 /* setup for probing default routers */
291 warnmsg(LOG_ERR, __func__,
292 "failed to setup for probing routers");
298 /* dump the current pid */
300 pid_t pid = getpid();
303 if ((fp = fopen(pidfilename, "w")) == NULL)
304 warnmsg(LOG_ERR, __func__,
305 "failed to open a pid log file(%s): %s",
306 pidfilename, strerror(errno));
308 fprintf(fp, "%d\n", pid);
315 memset(fdsetp, 0, fdmasks);
317 FD_SET(rtsock, fdsetp);
319 while (1) { /* main loop */
323 memcpy(selectfdp, fdsetp, fdmasks);
327 if (do_dump) { /* SIGUSR1 */
329 rtsold_dump_file(dumpfilename);
333 timeout = rtsol_check_timer();
338 /* if we have no timeout, we are done (or failed) */
342 /* if all interfaces have got RA packet, we are done */
343 for (ifi = iflist; ifi; ifi = ifi->next) {
344 if (ifi->state != IFS_DOWN && ifi->racnt == 0)
351 e = poll(set, 2, timeout ? (timeout->tv_sec * 1000 + timeout->tv_usec / 1000) : INFTIM);
353 e = select(maxfd + 1, selectfdp, NULL, NULL, timeout);
356 if (e < 0 && errno != EINTR) {
357 warnmsg(LOG_ERR, __func__, "select: %s",
363 /* packet reception */
365 if (set[1].revents & POLLIN)
367 if (FD_ISSET(rtsock, selectfdp))
369 rtsock_input(rtsock);
371 if (set[0].revents & POLLIN)
373 if (FD_ISSET(s, selectfdp))
383 ifconfig(char *ifname)
385 struct ifinfo *ifinfo;
386 struct sockaddr_dl *sdl;
389 if ((sdl = if_nametosdl(ifname)) == NULL) {
390 warnmsg(LOG_ERR, __func__,
391 "failed to get link layer information for %s", ifname);
394 if (find_ifinfo(sdl->sdl_index)) {
395 warnmsg(LOG_ERR, __func__,
396 "interface %s was already configured", ifname);
401 if ((ifinfo = malloc(sizeof(*ifinfo))) == NULL) {
402 warnmsg(LOG_ERR, __func__, "memory allocation failed");
406 memset(ifinfo, 0, sizeof(*ifinfo));
409 strlcpy(ifinfo->ifname, ifname, sizeof(ifinfo->ifname));
411 /* construct a router solicitation message */
412 if (make_packet(ifinfo))
415 /* set link ID of this interface. */
417 if (inet_zoneid(AF_INET6, 2, ifname, &ifinfo->linkid))
420 /* XXX: assume interface IDs as link IDs */
421 ifinfo->linkid = ifinfo->sdl->sdl_index;
425 * check if the interface is available.
426 * also check if SIOCGIFMEDIA ioctl is OK on the interface.
428 ifinfo->mediareqok = 1;
429 ifinfo->active = interface_status(ifinfo);
430 if (!ifinfo->mediareqok) {
432 * probe routers periodically even if the link status
435 ifinfo->probeinterval = PROBE_INTERVAL;
438 /* activate interface: interface_up returns 0 on success */
439 flags = interface_up(ifinfo->ifname);
441 ifinfo->state = IFS_DELAY;
442 else if (flags == IFS_TENTATIVE)
443 ifinfo->state = IFS_TENTATIVE;
445 ifinfo->state = IFS_DOWN;
447 rtsol_timer_update(ifinfo);
449 /* link into chain */
451 ifinfo->next = iflist;
465 struct ifinfo *ifi, *next;
467 for (ifi = iflist; ifi; ifi = next) {
480 ifreconfig(char *ifname)
482 struct ifinfo *ifi, *prev;
486 for (ifi = iflist; ifi; ifi = ifi->next) {
487 if (strncmp(ifi->ifname, ifname, sizeof(ifi->ifname)) == 0)
491 prev->next = ifi->next;
493 rv = ifconfig(ifname);
495 /* reclaim it after ifconfig() in case ifname is pointer inside ifi */
505 find_ifinfo(int ifindex)
509 for (ifi = iflist; ifi; ifi = ifi->next)
510 if (ifi->sdl->sdl_index == ifindex)
516 make_packet(struct ifinfo *ifinfo)
518 size_t packlen = sizeof(struct nd_router_solicit), lladdroptlen = 0;
519 struct nd_router_solicit *rs;
522 if ((lladdroptlen = lladdropt_length(ifinfo->sdl)) == 0) {
523 warnmsg(LOG_INFO, __func__,
524 "link-layer address option has null length"
525 " on %s. Treat as not included.", ifinfo->ifname);
527 packlen += lladdroptlen;
528 ifinfo->rs_datalen = packlen;
530 /* allocate buffer */
531 if ((buf = malloc(packlen)) == NULL) {
532 warnmsg(LOG_ERR, __func__,
533 "memory allocation failed for %s", ifinfo->ifname);
536 ifinfo->rs_data = buf;
538 /* fill in the message */
539 rs = (struct nd_router_solicit *)buf;
540 rs->nd_rs_type = ND_ROUTER_SOLICIT;
543 rs->nd_rs_reserved = 0;
546 /* fill in source link-layer address option */
548 lladdropt_fill(ifinfo->sdl, (struct nd_opt_hdr *)buf);
553 static struct timeval *
554 rtsol_check_timer(void)
556 static struct timeval returnval;
557 struct timeval now, rtsol_timer;
558 struct ifinfo *ifinfo;
561 gettimeofday(&now, NULL);
563 rtsol_timer = tm_max;
565 for (ifinfo = iflist; ifinfo; ifinfo = ifinfo->next) {
566 if (timercmp(&ifinfo->expire, &now, <=)) {
568 warnmsg(LOG_DEBUG, __func__,
569 "timer expiration on %s, "
570 "state = %d", ifinfo->ifname,
573 switch (ifinfo->state) {
576 /* interface_up returns 0 on success */
577 flags = interface_up(ifinfo->ifname);
579 ifinfo->state = IFS_DELAY;
580 else if (flags == IFS_TENTATIVE)
581 ifinfo->state = IFS_TENTATIVE;
583 ifinfo->state = IFS_DOWN;
587 int oldstatus = ifinfo->active;
590 ifinfo->active = interface_status(ifinfo);
592 if (oldstatus != ifinfo->active) {
593 warnmsg(LOG_DEBUG, __func__,
594 "%s status is changed"
597 oldstatus, ifinfo->active);
599 ifinfo->state = IFS_DELAY;
600 } else if (ifinfo->probeinterval &&
601 (ifinfo->probetimer -=
602 ifinfo->timer.tv_sec) <= 0) {
603 /* probe timer expired */
605 ifinfo->probeinterval;
607 ifinfo->state = IFS_PROBE;
611 * If we need a probe, clear the previous
612 * status wrt the "other" configuration.
615 ifinfo->otherconfig = 0;
617 if (probe && mobile_node)
618 defrouter_probe(ifinfo);
622 ifinfo->state = IFS_PROBE;
626 if (ifinfo->probes < MAX_RTR_SOLICITATIONS)
629 warnmsg(LOG_INFO, __func__,
630 "No answer after sending %d RSs",
633 ifinfo->state = IFS_IDLE;
637 rtsol_timer_update(ifinfo);
640 if (timercmp(&ifinfo->expire, &rtsol_timer, <))
641 rtsol_timer = ifinfo->expire;
644 if (timercmp(&rtsol_timer, &tm_max, ==)) {
645 warnmsg(LOG_DEBUG, __func__, "there is no timer");
647 } else if (timercmp(&rtsol_timer, &now, <))
648 /* this may occur when the interval is too small */
649 returnval.tv_sec = returnval.tv_usec = 0;
651 timersub(&rtsol_timer, &now, &returnval);
654 warnmsg(LOG_DEBUG, __func__, "New timer is %ld:%08ld",
655 (long)returnval.tv_sec, (long)returnval.tv_usec);
661 rtsol_timer_update(struct ifinfo *ifinfo)
663 #define MILLION 1000000
664 #define DADRETRY 10 /* XXX: adhoc */
668 bzero(&ifinfo->timer, sizeof(ifinfo->timer));
670 switch (ifinfo->state) {
673 if (++ifinfo->dadcount > DADRETRY) {
674 ifinfo->dadcount = 0;
675 ifinfo->timer.tv_sec = PROBE_INTERVAL;
677 ifinfo->timer.tv_sec = 1;
681 /* XXX should be configurable */
682 ifinfo->timer.tv_sec = 3;
685 ifinfo->timer = tm_max; /* stop timer(valid?) */
688 #ifndef HAVE_ARC4RANDOM
689 interval = random() % (MAX_RTR_SOLICITATION_DELAY * MILLION);
691 interval = arc4random_uniform(MAX_RTR_SOLICITATION_DELAY * MILLION);
693 ifinfo->timer.tv_sec = interval / MILLION;
694 ifinfo->timer.tv_usec = interval % MILLION;
697 if (ifinfo->probes < MAX_RTR_SOLICITATIONS)
698 ifinfo->timer.tv_sec = RTR_SOLICITATION_INTERVAL;
701 * After sending MAX_RTR_SOLICITATIONS solicitations,
702 * we're just waiting for possible replies; there
703 * will be no more solicitation. Thus, we change
704 * the timer value to MAX_RTR_SOLICITATION_DELAY based
705 * on RFC 2461, Section 6.3.7.
707 ifinfo->timer.tv_sec = MAX_RTR_SOLICITATION_DELAY;
711 warnmsg(LOG_ERR, __func__,
712 "illegal interface state(%d) on %s",
713 ifinfo->state, ifinfo->ifname);
717 /* reset the timer */
718 if (timercmp(&ifinfo->timer, &tm_max, ==)) {
719 ifinfo->expire = tm_max;
720 warnmsg(LOG_DEBUG, __func__,
721 "stop timer for %s", ifinfo->ifname);
723 gettimeofday(&now, NULL);
724 timeradd(&now, &ifinfo->timer, &ifinfo->expire);
727 warnmsg(LOG_DEBUG, __func__,
728 "set timer for %s to %d:%d", ifinfo->ifname,
729 (int)ifinfo->timer.tv_sec,
730 (int)ifinfo->timer.tv_usec);
736 /* timer related utility functions */
737 #define MILLION 1000000
741 rtsold_set_dump_file(int sig)
748 usage(char *progname)
750 if (progname && progname[strlen(progname) - 1] != 'd') {
751 fprintf(stderr, "usage: rtsol [-dDF] interfaces...\n");
752 fprintf(stderr, "usage: rtsol [-dDF] -a\n");
754 fprintf(stderr, "usage: rtsold [-adDfFm1] interfaces...\n");
755 fprintf(stderr, "usage: rtsold [-dDfFm1] -a\n");
762 warnmsg(int priority, const char *func, const char *msg, ...)
764 warnmsg(priority, func, msg, va_alist)
776 if (priority <= log_upto) {
777 (void)vfprintf(stderr, msg, ap);
778 (void)fprintf(stderr, "\n");
781 snprintf(buf, sizeof(buf), "<%s> %s", func, msg);
783 vsyslog(priority, msg, ap);
789 * return a list of interfaces which is suitable to sending an RS.
794 static char **argv = NULL;
798 struct ifaddrs *ifap, *ifa, *target;
799 struct in6_ndireq nd;
810 if (getifaddrs(&ifap) != 0)
813 if (!Fflag && (s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
819 /* find an ethernet */
820 for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
821 if ((ifa->ifa_flags & IFF_UP) == 0)
823 if ((ifa->ifa_flags & IFF_POINTOPOINT) != 0)
825 if ((ifa->ifa_flags & IFF_LOOPBACK) != 0)
827 if ((ifa->ifa_flags & IFF_MULTICAST) == 0)
830 if (ifa->ifa_addr->sa_family != AF_INET6)
834 for (i = 0; i < n; i++) {
835 if (strcmp(argv[i], ifa->ifa_name) == 0) {
844 * Skip the interfaces which IPv6 and/or accepting RA
848 memset(&nd, 0, sizeof(nd));
849 strlcpy(nd.ifname, ifa->ifa_name, sizeof(nd.ifname));
850 if (ioctl(s, SIOCGIFINFO_IN6, (caddr_t)&nd) < 0) {
851 err(1, "ioctl(SIOCGIFINFO_IN6)");
854 if ((nd.ndi.flags & ND6_IFF_IFDISABLED))
856 if (!(nd.ndi.flags & ND6_IFF_ACCEPT_RTADV))
860 /* if we find multiple candidates, just warn. */
861 if (n != 0 && dflag > 1)
862 warnx("multiple interfaces found");
864 a = (char **)realloc(argv, (n + 1) * sizeof(char **));
868 argv[n] = strdup(ifa->ifa_name);
875 a = (char **)realloc(argv, (n + 1) * sizeof(char **));
882 for (i = 0; i < n; i++)
883 warnx("probing %s", argv[i]);