1 /* $KAME: rtsold.c,v 1.31 2001/05/22 06:03:06 jinmei 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
31 * $FreeBSD: src/usr.sbin/rtsold/rtsold.c,v 1.1.2.4 2002/04/04 11:07:19 ume Exp $
32 * $DragonFly: src/usr.sbin/rtsold/rtsold.c,v 1.2 2003/06/17 04:30:03 dillon Exp $
35 #include <sys/types.h>
37 #include <sys/socket.h>
40 #include <net/if_dl.h>
42 #include <netinet/in.h>
43 #include <netinet/icmp6.h>
57 struct ifinfo *iflist;
58 struct timeval tm_max = {0x7fffffff, 0x7fffffff};
61 static int log_upto = 999;
64 /* protocol constatns */
65 #define MAX_RTR_SOLICITATION_DELAY 1 /* second */
66 #define RTR_SOLICITATION_INTERVAL 4 /* seconds */
67 #define MAX_RTR_SOLICITATIONS 3 /* times */
69 /* implementation dependent constants */
70 #define PROBE_INTERVAL 60 /* secondes XXX: should be configurable */
74 #define TIMEVAL_LT(a, b) (((a).tv_sec < (b).tv_sec) ||\
75 (((a).tv_sec == (b).tv_sec) && \
76 ((a).tv_usec < (b).tv_usec)))
79 #define TIMEVAL_LEQ(a, b) (((a).tv_sec < (b).tv_sec) ||\
80 (((a).tv_sec == (b).tv_sec) &&\
81 ((a).tv_usec <= (b).tv_usec)))
84 #define TIMEVAL_EQ(a, b) (((a).tv_sec==(b).tv_sec) && ((a).tv_usec==(b).tv_usec))
86 int main __P((int argc, char *argv[]));
88 /* static variables and functions */
89 static int mobile_node = 0;
91 static char *dumpfilename = "/var/run/rtsold.dump"; /* XXX: should be configurable */
92 static char *pidfilename = "/var/run/rtsold.pid"; /* should be configurable */
94 static int ifconfig __P((char *ifname));
96 static int ifreconfig __P((char *ifname));
98 static int make_packet __P((struct ifinfo *ifinfo));
99 static struct timeval *rtsol_check_timer __P((void));
100 static void TIMEVAL_ADD __P((struct timeval *a, struct timeval *b,
101 struct timeval *result));
102 static void TIMEVAL_SUB __P((struct timeval *a, struct timeval *b,
103 struct timeval *result));
105 static void rtsold_set_dump_file __P((void));
106 static void usage __P((char *progname));
107 static char **autoifprobe __P((void));
114 int s, rtsock, maxfd, ch;
116 struct timeval *timeout;
127 if (argv0 && argv0[strlen(argv0) - 1] != 'd') {
134 while ((ch = getopt(argc, argv, opts)) != -1) {
170 argv = autoifprobe();
172 errx(1, "could not autoprobe interface");
176 for (i = 0; argv[i]; i++)
187 log_upto = LOG_NOTICE;
190 ident = strrchr(argv0, '/');
195 openlog(ident, LOG_NDELAY|LOG_PID, LOG_DAEMON);
197 setlogmask(LOG_UPTO(log_upto));
200 #ifndef HAVE_ARC4RANDOM
201 /* random value initilization */
202 srandom((u_long)time(NULL));
205 /* warn if accept_rtadv is down */
206 if (!getinet6sysctl(IPV6CTL_ACCEPT_RTADV))
207 warnx("kernel is configured not to accept RAs");
208 /* warn if forwarding is up */
209 if (getinet6sysctl(IPV6CTL_FORWARDING))
210 warnx("kernel is configured as a router, not a host");
212 /* initialization to dump internal status to a file */
213 if (signal(SIGUSR1, (void *)rtsold_set_dump_file) < 0) {
214 errx(1, "failed to set signal for dump status");
219 * Open a socket for sending RS and receiving RA.
220 * This should be done before calling ifinit(), since the function
223 if ((s = sockopen()) < 0) {
224 errx(1, "failed to open a socket");
228 if ((rtsock = rtsock_open()) < 0) {
229 errx(1, "failed to open a socket");
235 /* configuration per interface */
237 errx(1, "failed to initilizatoin interfaces");
241 if (ifconfig(*argv)) {
242 errx(1, "failed to initialize %s", *argv);
248 /* setup for probing default routers */
250 errx(1, "failed to setup for probing routers");
255 daemon(0, 0); /* act as a daemon */
257 /* dump the current pid */
259 pid_t pid = getpid();
262 if ((fp = fopen(pidfilename, "w")) == NULL)
263 warnmsg(LOG_ERR, __FUNCTION__,
264 "failed to open a log file(%s): %s",
265 pidfilename, strerror(errno));
267 fprintf(fp, "%d\n", pid);
274 FD_SET(rtsock, &fdset);
275 while (1) { /* main loop */
277 struct fd_set select_fd = fdset;
279 if (do_dump) { /* SIGUSR1 */
281 rtsold_dump_file(dumpfilename);
284 timeout = rtsol_check_timer();
289 /* if we have no timeout, we are done (or failed) */
293 /* if all interfaces have got RA packet, we are done */
294 for (ifi = iflist; ifi; ifi = ifi->next) {
295 if (ifi->state != IFS_DOWN && ifi->racnt == 0)
301 e = select(maxfd + 1, &select_fd, NULL, NULL, timeout);
303 if (e < 0 && errno != EINTR) {
304 warnmsg(LOG_ERR, __FUNCTION__, "select: %s",
310 /* packet reception */
311 if (FD_ISSET(rtsock, &select_fd))
312 rtsock_input(rtsock);
313 if (FD_ISSET(s, &select_fd))
322 ifconfig(char *ifname)
324 struct ifinfo *ifinfo;
325 struct sockaddr_dl *sdl;
328 if ((sdl = if_nametosdl(ifname)) == NULL) {
329 warnmsg(LOG_ERR, __FUNCTION__,
330 "failed to get link layer information for %s", ifname);
333 if (find_ifinfo(sdl->sdl_index)) {
334 warnmsg(LOG_ERR, __FUNCTION__,
335 "interface %s was already configured", ifname);
340 if ((ifinfo = malloc(sizeof(*ifinfo))) == NULL) {
341 warnmsg(LOG_ERR, __FUNCTION__, "memory allocation failed");
345 memset(ifinfo, 0, sizeof(*ifinfo));
348 strncpy(ifinfo->ifname, ifname, sizeof(ifinfo->ifname));
350 /* construct a router solicitation message */
351 if (make_packet(ifinfo))
355 * check if the interface is available.
356 * also check if SIOCGIFMEDIA ioctl is OK on the interface.
358 ifinfo->mediareqok = 1;
359 ifinfo->active = interface_status(ifinfo);
360 if (!ifinfo->mediareqok) {
362 * probe routers periodically even if the link status
365 ifinfo->probeinterval = PROBE_INTERVAL;
368 /* activate interface: interface_up returns 0 on success */
369 flags = interface_up(ifinfo->ifname);
371 ifinfo->state = IFS_DELAY;
372 else if (flags == IFS_TENTATIVE)
373 ifinfo->state = IFS_TENTATIVE;
375 ifinfo->state = IFS_DOWN;
377 rtsol_timer_update(ifinfo);
379 /* link into chain */
381 ifinfo->next = iflist;
394 ifreconfig(char *ifname)
396 struct ifinfo *ifi, *prev;
400 for (ifi = iflist; ifi; ifi = ifi->next) {
401 if (strncmp(ifi->ifname, ifname, sizeof(ifi->ifname)) == 0)
405 prev->next = ifi->next;
407 rv = ifconfig(ifname);
409 /* reclaim it after ifconfig() in case ifname is pointer inside ifi */
420 find_ifinfo(int ifindex)
424 for (ifi = iflist; ifi; ifi = ifi->next)
425 if (ifi->sdl->sdl_index == ifindex)
432 make_packet(struct ifinfo *ifinfo)
435 struct nd_router_solicit *rs;
436 size_t packlen = sizeof(struct nd_router_solicit), lladdroptlen = 0;
438 if ((lladdroptlen = lladdropt_length(ifinfo->sdl)) == 0) {
439 warnmsg(LOG_INFO, __FUNCTION__,
440 "link-layer address option has null length"
441 " on %s. Treat as not included.", ifinfo->ifname);
443 packlen += lladdroptlen;
444 ifinfo->rs_datalen = packlen;
446 /* allocate buffer */
447 if ((buf = malloc(packlen)) == NULL) {
448 warnmsg(LOG_ERR, __FUNCTION__,
449 "memory allocation failed for %s", ifinfo->ifname);
452 ifinfo->rs_data = buf;
454 /* fill in the message */
455 rs = (struct nd_router_solicit *)buf;
456 rs->nd_rs_type = ND_ROUTER_SOLICIT;
459 rs->nd_rs_reserved = 0;
462 /* fill in source link-layer address option */
464 lladdropt_fill(ifinfo->sdl, (struct nd_opt_hdr *)buf);
469 static struct timeval *
472 static struct timeval returnval;
473 struct timeval now, rtsol_timer;
474 struct ifinfo *ifinfo;
477 gettimeofday(&now, NULL);
479 rtsol_timer = tm_max;
481 for (ifinfo = iflist; ifinfo; ifinfo = ifinfo->next) {
482 if (TIMEVAL_LEQ(ifinfo->expire, now)) {
484 warnmsg(LOG_DEBUG, __FUNCTION__,
485 "timer expiration on %s, "
486 "state = %d", ifinfo->ifname,
489 switch (ifinfo->state) {
492 /* interface_up returns 0 on success */
493 flags = interface_up(ifinfo->ifname);
495 ifinfo->state = IFS_DELAY;
496 else if (flags == IFS_TENTATIVE)
497 ifinfo->state = IFS_TENTATIVE;
499 ifinfo->state = IFS_DOWN;
503 int oldstatus = ifinfo->active;
507 interface_status(ifinfo);
509 if (oldstatus != ifinfo->active) {
510 warnmsg(LOG_DEBUG, __FUNCTION__,
511 "%s status is changed"
514 oldstatus, ifinfo->active);
516 ifinfo->state = IFS_DELAY;
518 else if (ifinfo->probeinterval &&
519 (ifinfo->probetimer -=
520 ifinfo->timer.tv_sec) <= 0) {
521 /* probe timer expired */
523 ifinfo->probeinterval;
525 ifinfo->state = IFS_PROBE;
528 if (probe && mobile_node)
529 defrouter_probe(ifinfo->sdl->sdl_index);
533 ifinfo->state = IFS_PROBE;
537 if (ifinfo->probes < MAX_RTR_SOLICITATIONS)
540 warnmsg(LOG_INFO, __FUNCTION__,
542 "after sending %d RSs",
545 ifinfo->state = IFS_IDLE;
549 rtsol_timer_update(ifinfo);
552 if (TIMEVAL_LT(ifinfo->expire, rtsol_timer))
553 rtsol_timer = ifinfo->expire;
556 if (TIMEVAL_EQ(rtsol_timer, tm_max)) {
557 warnmsg(LOG_DEBUG, __FUNCTION__, "there is no timer");
560 else if (TIMEVAL_LT(rtsol_timer, now))
561 /* this may occur when the interval is too small */
562 returnval.tv_sec = returnval.tv_usec = 0;
564 TIMEVAL_SUB(&rtsol_timer, &now, &returnval);
567 warnmsg(LOG_DEBUG, __FUNCTION__, "New timer is %ld:%08ld",
568 (long)returnval.tv_sec, (long)returnval.tv_usec);
574 rtsol_timer_update(struct ifinfo *ifinfo)
576 #define MILLION 1000000
577 #define DADRETRY 10 /* XXX: adhoc */
581 bzero(&ifinfo->timer, sizeof(ifinfo->timer));
583 switch (ifinfo->state) {
586 if (++ifinfo->dadcount > DADRETRY) {
587 ifinfo->dadcount = 0;
588 ifinfo->timer.tv_sec = PROBE_INTERVAL;
591 ifinfo->timer.tv_sec = 1;
595 /* XXX should be configurable */
596 ifinfo->timer.tv_sec = 3;
599 ifinfo->timer = tm_max; /* stop timer(valid?) */
602 #ifndef HAVE_ARC4RANDOM
603 interval = random() % (MAX_RTR_SOLICITATION_DELAY * MILLION);
605 interval = arc4random() % (MAX_RTR_SOLICITATION_DELAY * MILLION);
607 ifinfo->timer.tv_sec = interval / MILLION;
608 ifinfo->timer.tv_usec = interval % MILLION;
611 if (ifinfo->probes < MAX_RTR_SOLICITATIONS)
612 ifinfo->timer.tv_sec = RTR_SOLICITATION_INTERVAL;
615 * After sending MAX_RTR_SOLICITATIONS solicitations,
616 * we're just waiting for possible replies; there
617 * will be no more solicatation. Thus, we change
618 * the timer value to MAX_RTR_SOLICITATION_DELAY based
619 * on RFC 2461, Section 6.3.7.
621 ifinfo->timer.tv_sec = MAX_RTR_SOLICITATION_DELAY;
625 warnmsg(LOG_ERR, __FUNCTION__,
626 "illegal interface state(%d) on %s",
627 ifinfo->state, ifinfo->ifname);
631 /* reset the timer */
632 if (TIMEVAL_EQ(ifinfo->timer, tm_max)) {
633 ifinfo->expire = tm_max;
634 warnmsg(LOG_DEBUG, __FUNCTION__,
635 "stop timer for %s", ifinfo->ifname);
638 gettimeofday(&now, NULL);
639 TIMEVAL_ADD(&now, &ifinfo->timer, &ifinfo->expire);
642 warnmsg(LOG_DEBUG, __FUNCTION__,
643 "set timer for %s to %d:%d", ifinfo->ifname,
644 (int)ifinfo->timer.tv_sec,
645 (int)ifinfo->timer.tv_usec);
651 /* timer related utility functions */
652 #define MILLION 1000000
656 TIMEVAL_ADD(struct timeval *a, struct timeval *b, struct timeval *result)
660 if ((l = a->tv_usec + b->tv_usec) < MILLION) {
662 result->tv_sec = a->tv_sec + b->tv_sec;
665 result->tv_usec = l - MILLION;
666 result->tv_sec = a->tv_sec + b->tv_sec + 1;
672 * XXX: this function assumes that a >= b.
675 TIMEVAL_SUB(struct timeval *a, struct timeval *b, struct timeval *result)
679 if ((l = a->tv_usec - b->tv_usec) >= 0) {
681 result->tv_sec = a->tv_sec - b->tv_sec;
684 result->tv_usec = MILLION + l;
685 result->tv_sec = a->tv_sec - b->tv_sec - 1;
690 rtsold_set_dump_file()
696 usage(char *progname)
698 if (progname && progname[strlen(progname) - 1] != 'd') {
699 fprintf(stderr, "usage: rtsol [-dD] interfaces...\n");
700 fprintf(stderr, "usage: rtsol [-dD] -a\n");
702 fprintf(stderr, "usage: rtsold [-adDfm1] interfaces...\n");
703 fprintf(stderr, "usage: rtsold [-dDfm1] -a\n");
710 warnmsg(int priority, const char *func, const char *msg, ...)
712 warnmsg(priority, func, msg, va_alist)
724 if (priority <= log_upto) {
725 (void)vfprintf(stderr, msg, ap);
726 (void)fprintf(stderr, "\n");
729 snprintf(buf, sizeof(buf), "<%s> %s", func, msg);
731 vsyslog(priority, msg, ap);
739 #ifndef HAVE_GETIFADDRS
740 errx(1, "-a is not available with the configuration");
742 static char ifname[IFNAMSIZ + 1];
743 static char *argv[2];
744 struct ifaddrs *ifap, *ifa, *target;
746 if (getifaddrs(&ifap) != 0)
750 /* find an ethernet */
751 for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
752 if ((ifa->ifa_flags & IFF_UP) == 0)
754 if ((ifa->ifa_flags & IFF_POINTOPOINT) != 0)
756 if ((ifa->ifa_flags & IFF_LOOPBACK) != 0)
758 if ((ifa->ifa_flags & IFF_MULTICAST) == 0)
761 if (ifa->ifa_addr->sa_family != AF_INET6)
764 if (target && strcmp(target->ifa_name, ifa->ifa_name) == 0)
770 /* if we find multiple candidates, failure. */
772 warnx("multiple interfaces found");
779 strncpy(ifname, target->ifa_name, sizeof(ifname) - 1);
780 ifname[sizeof(ifname) - 1] = '\0';
785 warnx("probing %s", argv[0]);
791 return (char **)NULL;