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>
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>
61 struct ifinfo *iflist;
62 struct timeval tm_max = {0x7fffffff, 0x7fffffff};
63 static int log_upto = 999;
65 static int Fflag = 0; /* force setting sysctl parameters */
70 char *otherconf_script;
72 /* protocol constants */
73 #define MAX_RTR_SOLICITATION_DELAY 1 /* second */
74 #define RTR_SOLICITATION_INTERVAL 4 /* seconds */
75 #define MAX_RTR_SOLICITATIONS 3 /* times */
78 * implementation dependent constants in seconds
79 * XXX: should be configurable
81 #define PROBE_INTERVAL 60
83 int main __P((int, char **));
85 /* static variables and functions */
86 static int mobile_node = 0;
89 static char *dumpfilename = "/var/run/rtsold.dump"; /* XXX: should be configurable */
92 static char *pidfilename = "/var/run/rtsold.pid"; /* should be configurable */
96 static int ifreconfig __P((char *));
98 static int make_packet __P((struct ifinfo *));
99 static struct timeval *rtsol_check_timer __P((void));
102 static void rtsold_set_dump_file __P((int));
104 static void usage __P((char *));
107 main(int argc, char **argv)
110 struct timeval *timeout;
113 struct pollfd set[2];
115 fd_set *fdsetp, *selectfdp;
127 if (argv0 && argv0[strlen(argv0) - 1] != 'd') {
134 while ((ch = getopt(argc, argv, opts)) != -1) {
158 otherconf_script = optarg;
168 if ((!aflag && argc == 0) || (aflag && argc != 0)) {
175 log_upto = LOG_NOTICE;
179 ident = strrchr(argv0, '/');
184 openlog(ident, LOG_NDELAY|LOG_PID, LOG_DAEMON);
186 setlogmask(LOG_UPTO(log_upto));
189 if (otherconf_script && *otherconf_script != '/') {
190 errx(1, "configuration script (%s) must be an absolute path",
194 #ifndef HAVE_ARC4RANDOM
195 /* random value initialization */
196 srandom((u_long)time(NULL));
200 setinet6sysctl(IPV6CTL_ACCEPT_RTADV, 1);
201 setinet6sysctl(IPV6CTL_FORWARDING, 0);
203 /* warn if accept_rtadv is down */
204 if (!getinet6sysctl(IPV6CTL_ACCEPT_RTADV))
205 warnx("kernel is configured not to accept RAs");
206 /* warn if forwarding is up */
207 if (getinet6sysctl(IPV6CTL_FORWARDING))
208 warnx("kernel is configured as a router, not a host");
212 /* initialization to dump internal status to a file */
213 signal(SIGUSR1, rtsold_set_dump_file);
217 daemon(0, 0); /* act as a daemon */
220 * Open a socket for sending RS and receiving RA.
221 * This should be done before calling ifinit(), since the function
224 if ((s = sockopen()) < 0) {
225 warnmsg(LOG_ERR, __func__, "failed to open a socket");
231 set[0].events = POLLIN;
240 if ((rtsock = rtsock_open()) < 0) {
241 warnmsg(LOG_ERR, __func__, "failed to open a socket");
247 set[1].events = POLLIN;
254 fdmasks = howmany(maxfd + 1, NFDBITS) * sizeof(fd_mask);
255 if ((fdsetp = malloc(fdmasks)) == NULL) {
259 if ((selectfdp = malloc(fdmasks)) == NULL) {
265 /* configuration per interface */
267 warnmsg(LOG_ERR, __func__,
268 "failed to initialize interfaces");
273 argv = autoifprobe();
274 while (argv && *argv) {
275 if (ifconfig(*argv)) {
276 warnmsg(LOG_ERR, __func__,
277 "failed to initialize %s", *argv);
284 /* setup for probing default routers */
286 warnmsg(LOG_ERR, __func__,
287 "failed to setup for probing routers");
293 /* dump the current pid */
295 pid_t pid = getpid();
298 if ((fp = fopen(pidfilename, "w")) == NULL)
299 warnmsg(LOG_ERR, __func__,
300 "failed to open a pid log file(%s): %s",
301 pidfilename, strerror(errno));
303 fprintf(fp, "%d\n", pid);
310 memset(fdsetp, 0, fdmasks);
312 FD_SET(rtsock, fdsetp);
314 while (1) { /* main loop */
318 memcpy(selectfdp, fdsetp, fdmasks);
322 if (do_dump) { /* SIGUSR1 */
324 rtsold_dump_file(dumpfilename);
328 timeout = rtsol_check_timer();
333 /* if we have no timeout, we are done (or failed) */
337 /* if all interfaces have got RA packet, we are done */
338 for (ifi = iflist; ifi; ifi = ifi->next) {
339 if (ifi->state != IFS_DOWN && ifi->racnt == 0)
346 e = poll(set, 2, timeout ? (timeout->tv_sec * 1000 + timeout->tv_usec / 1000) : INFTIM);
348 e = select(maxfd + 1, selectfdp, NULL, NULL, timeout);
351 if (e < 0 && errno != EINTR) {
352 warnmsg(LOG_ERR, __func__, "select: %s",
358 /* packet reception */
360 if (set[1].revents & POLLIN)
362 if (FD_ISSET(rtsock, selectfdp))
364 rtsock_input(rtsock);
366 if (set[0].revents & POLLIN)
368 if (FD_ISSET(s, selectfdp))
378 ifconfig(char *ifname)
380 struct ifinfo *ifinfo;
381 struct sockaddr_dl *sdl;
384 if ((sdl = if_nametosdl(ifname)) == NULL) {
385 warnmsg(LOG_ERR, __func__,
386 "failed to get link layer information for %s", ifname);
389 if (find_ifinfo(sdl->sdl_index)) {
390 warnmsg(LOG_ERR, __func__,
391 "interface %s was already configured", ifname);
396 if ((ifinfo = malloc(sizeof(*ifinfo))) == NULL) {
397 warnmsg(LOG_ERR, __func__, "memory allocation failed");
401 memset(ifinfo, 0, sizeof(*ifinfo));
404 strlcpy(ifinfo->ifname, ifname, sizeof(ifinfo->ifname));
406 /* construct a router solicitation message */
407 if (make_packet(ifinfo))
410 /* set link ID of this interface. */
412 if (inet_zoneid(AF_INET6, 2, ifname, &ifinfo->linkid))
415 /* XXX: assume interface IDs as link IDs */
416 ifinfo->linkid = ifinfo->sdl->sdl_index;
420 * check if the interface is available.
421 * also check if SIOCGIFMEDIA ioctl is OK on the interface.
423 ifinfo->mediareqok = 1;
424 ifinfo->active = interface_status(ifinfo);
425 if (!ifinfo->mediareqok) {
427 * probe routers periodically even if the link status
430 ifinfo->probeinterval = PROBE_INTERVAL;
433 /* activate interface: interface_up returns 0 on success */
434 flags = interface_up(ifinfo->ifname);
436 ifinfo->state = IFS_DELAY;
437 else if (flags == IFS_TENTATIVE)
438 ifinfo->state = IFS_TENTATIVE;
440 ifinfo->state = IFS_DOWN;
442 rtsol_timer_update(ifinfo);
444 /* link into chain */
446 ifinfo->next = iflist;
460 struct ifinfo *ifi, *next;
462 for (ifi = iflist; ifi; ifi = next) {
475 ifreconfig(char *ifname)
477 struct ifinfo *ifi, *prev;
481 for (ifi = iflist; ifi; ifi = ifi->next) {
482 if (strncmp(ifi->ifname, ifname, sizeof(ifi->ifname)) == 0)
486 prev->next = ifi->next;
488 rv = ifconfig(ifname);
490 /* reclaim it after ifconfig() in case ifname is pointer inside ifi */
500 find_ifinfo(int ifindex)
504 for (ifi = iflist; ifi; ifi = ifi->next)
505 if (ifi->sdl->sdl_index == ifindex)
511 make_packet(struct ifinfo *ifinfo)
513 size_t packlen = sizeof(struct nd_router_solicit), lladdroptlen = 0;
514 struct nd_router_solicit *rs;
517 if ((lladdroptlen = lladdropt_length(ifinfo->sdl)) == 0) {
518 warnmsg(LOG_INFO, __func__,
519 "link-layer address option has null length"
520 " on %s. Treat as not included.", ifinfo->ifname);
522 packlen += lladdroptlen;
523 ifinfo->rs_datalen = packlen;
525 /* allocate buffer */
526 if ((buf = malloc(packlen)) == NULL) {
527 warnmsg(LOG_ERR, __func__,
528 "memory allocation failed for %s", ifinfo->ifname);
531 ifinfo->rs_data = buf;
533 /* fill in the message */
534 rs = (struct nd_router_solicit *)buf;
535 rs->nd_rs_type = ND_ROUTER_SOLICIT;
538 rs->nd_rs_reserved = 0;
541 /* fill in source link-layer address option */
543 lladdropt_fill(ifinfo->sdl, (struct nd_opt_hdr *)buf);
548 static struct timeval *
549 rtsol_check_timer(void)
551 static struct timeval returnval;
552 struct timeval now, rtsol_timer;
553 struct ifinfo *ifinfo;
556 gettimeofday(&now, NULL);
558 rtsol_timer = tm_max;
560 for (ifinfo = iflist; ifinfo; ifinfo = ifinfo->next) {
561 if (timercmp(&ifinfo->expire, &now, <=)) {
563 warnmsg(LOG_DEBUG, __func__,
564 "timer expiration on %s, "
565 "state = %d", ifinfo->ifname,
568 switch (ifinfo->state) {
571 /* interface_up returns 0 on success */
572 flags = interface_up(ifinfo->ifname);
574 ifinfo->state = IFS_DELAY;
575 else if (flags == IFS_TENTATIVE)
576 ifinfo->state = IFS_TENTATIVE;
578 ifinfo->state = IFS_DOWN;
582 int oldstatus = ifinfo->active;
585 ifinfo->active = interface_status(ifinfo);
587 if (oldstatus != ifinfo->active) {
588 warnmsg(LOG_DEBUG, __func__,
589 "%s status is changed"
592 oldstatus, ifinfo->active);
594 ifinfo->state = IFS_DELAY;
595 } else if (ifinfo->probeinterval &&
596 (ifinfo->probetimer -=
597 ifinfo->timer.tv_sec) <= 0) {
598 /* probe timer expired */
600 ifinfo->probeinterval;
602 ifinfo->state = IFS_PROBE;
606 * If we need a probe, clear the previous
607 * status wrt the "other" configuration.
610 ifinfo->otherconfig = 0;
612 if (probe && mobile_node)
613 defrouter_probe(ifinfo);
617 ifinfo->state = IFS_PROBE;
621 if (ifinfo->probes < MAX_RTR_SOLICITATIONS)
624 warnmsg(LOG_INFO, __func__,
625 "No answer after sending %d RSs",
628 ifinfo->state = IFS_IDLE;
632 rtsol_timer_update(ifinfo);
635 if (timercmp(&ifinfo->expire, &rtsol_timer, <))
636 rtsol_timer = ifinfo->expire;
639 if (timercmp(&rtsol_timer, &tm_max, ==)) {
640 warnmsg(LOG_DEBUG, __func__, "there is no timer");
642 } else if (timercmp(&rtsol_timer, &now, <))
643 /* this may occur when the interval is too small */
644 returnval.tv_sec = returnval.tv_usec = 0;
646 timersub(&rtsol_timer, &now, &returnval);
649 warnmsg(LOG_DEBUG, __func__, "New timer is %ld:%08ld",
650 (long)returnval.tv_sec, (long)returnval.tv_usec);
656 rtsol_timer_update(struct ifinfo *ifinfo)
658 #define MILLION 1000000
659 #define DADRETRY 10 /* XXX: adhoc */
663 bzero(&ifinfo->timer, sizeof(ifinfo->timer));
665 switch (ifinfo->state) {
668 if (++ifinfo->dadcount > DADRETRY) {
669 ifinfo->dadcount = 0;
670 ifinfo->timer.tv_sec = PROBE_INTERVAL;
672 ifinfo->timer.tv_sec = 1;
676 /* XXX should be configurable */
677 ifinfo->timer.tv_sec = 3;
680 ifinfo->timer = tm_max; /* stop timer(valid?) */
683 #ifndef HAVE_ARC4RANDOM
684 interval = random() % (MAX_RTR_SOLICITATION_DELAY * MILLION);
686 interval = arc4random() % (MAX_RTR_SOLICITATION_DELAY * MILLION);
688 ifinfo->timer.tv_sec = interval / MILLION;
689 ifinfo->timer.tv_usec = interval % MILLION;
692 if (ifinfo->probes < MAX_RTR_SOLICITATIONS)
693 ifinfo->timer.tv_sec = RTR_SOLICITATION_INTERVAL;
696 * After sending MAX_RTR_SOLICITATIONS solicitations,
697 * we're just waiting for possible replies; there
698 * will be no more solicitation. Thus, we change
699 * the timer value to MAX_RTR_SOLICITATION_DELAY based
700 * on RFC 2461, Section 6.3.7.
702 ifinfo->timer.tv_sec = MAX_RTR_SOLICITATION_DELAY;
706 warnmsg(LOG_ERR, __func__,
707 "illegal interface state(%d) on %s",
708 ifinfo->state, ifinfo->ifname);
712 /* reset the timer */
713 if (timercmp(&ifinfo->timer, &tm_max, ==)) {
714 ifinfo->expire = tm_max;
715 warnmsg(LOG_DEBUG, __func__,
716 "stop timer for %s", ifinfo->ifname);
718 gettimeofday(&now, NULL);
719 timeradd(&now, &ifinfo->timer, &ifinfo->expire);
722 warnmsg(LOG_DEBUG, __func__,
723 "set timer for %s to %d:%d", ifinfo->ifname,
724 (int)ifinfo->timer.tv_sec,
725 (int)ifinfo->timer.tv_usec);
731 /* timer related utility functions */
732 #define MILLION 1000000
736 rtsold_set_dump_file(int sig)
743 usage(char *progname)
745 if (progname && progname[strlen(progname) - 1] != 'd') {
746 fprintf(stderr, "usage: rtsol [-dDF] interfaces...\n");
747 fprintf(stderr, "usage: rtsol [-dDF] -a\n");
749 fprintf(stderr, "usage: rtsold [-adDfFm1] interfaces...\n");
750 fprintf(stderr, "usage: rtsold [-dDfFm1] -a\n");
757 warnmsg(int priority, const char *func, const char *msg, ...)
759 warnmsg(priority, func, msg, va_alist)
771 if (priority <= log_upto) {
772 (void)vfprintf(stderr, msg, ap);
773 (void)fprintf(stderr, "\n");
776 snprintf(buf, sizeof(buf), "<%s> %s", func, msg);
778 vsyslog(priority, msg, ap);
784 * return a list of interfaces which is suitable to sending an RS.
789 static char **argv = NULL;
793 struct ifaddrs *ifap, *ifa, *target;
804 if (getifaddrs(&ifap) != 0)
808 /* find an ethernet */
809 for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
810 if ((ifa->ifa_flags & IFF_UP) == 0)
812 if ((ifa->ifa_flags & IFF_POINTOPOINT) != 0)
814 if ((ifa->ifa_flags & IFF_LOOPBACK) != 0)
816 if ((ifa->ifa_flags & IFF_MULTICAST) == 0)
819 if (ifa->ifa_addr->sa_family != AF_INET6)
823 for (i = 0; i < n; i++) {
824 if (strcmp(argv[i], ifa->ifa_name) == 0) {
832 /* if we find multiple candidates, just warn. */
833 if (n != 0 && dflag > 1)
834 warnx("multiple interfaces found");
836 a = (char **)realloc(argv, (n + 1) * sizeof(char **));
840 argv[n] = strdup(ifa->ifa_name);
848 a = (char **)realloc(argv, (n + 1) * sizeof(char **));
855 for (i = 0; i < n; i++)
856 warnx("probing %s", argv[i]);