Deleted wrongly imported bind sources from base.
[dragonfly.git] / usr.sbin / rtsold / rtsold.c
blob4d1c5ec5a6f58281cab52d4fe14867b7f4992954
1 /* $KAME: rtsold.c,v 1.31 2001/05/22 06:03:06 jinmei Exp $ */
3 /*
4 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
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
29 * SUCH DAMAGE.
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.7 2005/12/05 00:56:37 swildner Exp $
35 #include <sys/types.h>
36 #include <sys/time.h>
37 #include <sys/socket.h>
39 #include <net/if.h>
40 #include <net/if_dl.h>
42 #include <netinet/in.h>
43 #include <netinet/icmp6.h>
45 #include <signal.h>
46 #include <unistd.h>
47 #include <syslog.h>
48 #include <string.h>
49 #include <stdlib.h>
50 #include <stdio.h>
51 #include <errno.h>
52 #include <err.h>
53 #include <stdarg.h>
54 #include <ifaddrs.h>
55 #include "rtsold.h"
57 struct ifinfo *iflist;
58 struct timeval tm_max = {0x7fffffff, 0x7fffffff};
59 int aflag = 0;
60 int dflag = 0;
61 static int log_upto = 999;
62 static int fflag = 0;
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 */
72 /* utility macros */
73 /* a < b */
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)))
78 /* a <= b */
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)))
83 /* a == b */
84 #define TIMEVAL_EQ(a, b) (((a).tv_sec==(b).tv_sec) && ((a).tv_usec==(b).tv_usec))
86 int main(int argc, char *argv[]);
88 /* static variables and functions */
89 static int mobile_node = 0;
90 static int do_dump;
92 * XXX: the following two values should be configurable
94 static const char *dumpfilename = "/var/run/rtsold.dump";
95 static const char *pidfilename = "/var/run/rtsold.pid";
97 static int ifconfig(char *ifname);
98 #if 0
99 static int ifreconfig(char *ifname);
100 #endif
101 static int make_packet(struct ifinfo *ifinfo);
102 static struct timeval *rtsol_check_timer(void);
103 static void TIMEVAL_ADD(struct timeval *a, struct timeval *b,
104 struct timeval *result);
105 static void TIMEVAL_SUB(struct timeval *a, struct timeval *b,
106 struct timeval *result);
108 static void rtsold_set_dump_file(int);
109 static void usage(char *progname);
110 static char **autoifprobe(void);
113 main(int argc, char *argv[])
115 int s, rtsock, maxfd, ch;
116 int once = 0;
117 struct timeval *timeout;
118 struct fd_set fdset;
119 char *argv0;
120 const char *opts;
123 * Initialization
125 argv0 = argv[0];
127 /* get option */
128 if (argv0 && argv0[strlen(argv0) - 1] != 'd') {
129 fflag = 1;
130 once = 1;
131 opts = "adD";
132 } else
133 opts = "adDfm1";
135 while ((ch = getopt(argc, argv, opts)) != -1) {
136 switch (ch) {
137 case 'a':
138 aflag = 1;
139 break;
140 case 'd':
141 dflag = 1;
142 break;
143 case 'D':
144 dflag = 2;
145 break;
146 case 'f':
147 fflag = 1;
148 break;
149 case 'm':
150 mobile_node = 1;
151 break;
152 case '1':
153 once = 1;
154 break;
155 default:
156 usage(argv0);
157 /*NOTREACHED*/
160 argc -= optind;
161 argv += optind;
163 if (aflag) {
164 int i;
166 if (argc != 0) {
167 usage(argv0);
168 /*NOTREACHED*/
171 argv = autoifprobe();
172 if (!argv) {
173 errx(1, "could not autoprobe interface");
174 /*NOTREACHED*/
177 for (i = 0; argv[i]; i++)
179 argc = i;
181 if (argc == 0) {
182 usage(argv0);
183 /*NOTREACHED*/
186 /* set log level */
187 if (dflag == 0)
188 log_upto = LOG_NOTICE;
189 if (!fflag) {
190 char *ident;
191 ident = strrchr(argv0, '/');
192 if (!ident)
193 ident = argv0;
194 else
195 ident++;
196 openlog(ident, LOG_NDELAY|LOG_PID, LOG_DAEMON);
197 if (log_upto >= 0)
198 setlogmask(LOG_UPTO(log_upto));
201 #ifndef HAVE_ARC4RANDOM
202 /* random value initilization */
203 srandom((u_long)time(NULL));
204 #endif
206 /* warn if accept_rtadv is down */
207 if (!getinet6sysctl(IPV6CTL_ACCEPT_RTADV))
208 warnx("kernel is configured not to accept RAs");
209 /* warn if forwarding is up */
210 if (getinet6sysctl(IPV6CTL_FORWARDING))
211 warnx("kernel is configured as a router, not a host");
213 /* initialization to dump internal status to a file */
214 if (signal(SIGUSR1, rtsold_set_dump_file) == SIG_ERR) {
215 errx(1, "failed to set signal for dump status");
216 /*NOTREACHED*/
220 * Open a socket for sending RS and receiving RA.
221 * This should be done before calling ifinit(), since the function
222 * uses the socket.
224 if ((s = sockopen()) < 0) {
225 errx(1, "failed to open a socket");
226 /*NOTREACHED*/
228 maxfd = s;
229 if ((rtsock = rtsock_open()) < 0) {
230 errx(1, "failed to open a socket");
231 /*NOTREACHED*/
233 if (rtsock > maxfd)
234 maxfd = rtsock;
236 /* configuration per interface */
237 if (ifinit()) {
238 errx(1, "failed to initilizatoin interfaces");
239 /*NOTREACHED*/
241 while (argc--) {
242 if (ifconfig(*argv)) {
243 errx(1, "failed to initialize %s", *argv);
244 /*NOTREACHED*/
246 argv++;
249 /* setup for probing default routers */
250 if (probe_init()) {
251 errx(1, "failed to setup for probing routers");
252 /*NOTREACHED*/
255 if (!fflag)
256 daemon(0, 0); /* act as a daemon */
258 /* dump the current pid */
259 if (!once) {
260 pid_t pid = getpid();
261 FILE *fp;
263 if ((fp = fopen(pidfilename, "w")) == NULL)
264 warnmsg(LOG_ERR, __func__,
265 "failed to open a log file(%s): %s",
266 pidfilename, strerror(errno));
267 else {
268 fprintf(fp, "%d\n", pid);
269 fclose(fp);
273 FD_ZERO(&fdset);
274 FD_SET(s, &fdset);
275 FD_SET(rtsock, &fdset);
276 while (1) { /* main loop */
277 int e;
278 struct fd_set select_fd = fdset;
280 if (do_dump) { /* SIGUSR1 */
281 do_dump = 0;
282 rtsold_dump_file(dumpfilename);
285 timeout = rtsol_check_timer();
287 if (once) {
288 struct ifinfo *ifi;
290 /* if we have no timeout, we are done (or failed) */
291 if (timeout == NULL)
292 break;
294 /* if all interfaces have got RA packet, we are done */
295 for (ifi = iflist; ifi; ifi = ifi->next) {
296 if (ifi->state != IFS_DOWN && ifi->racnt == 0)
297 break;
299 if (ifi == NULL)
300 break;
302 e = select(maxfd + 1, &select_fd, NULL, NULL, timeout);
303 if (e < 1) {
304 if (e < 0 && errno != EINTR) {
305 warnmsg(LOG_ERR, __func__, "select: %s",
306 strerror(errno));
308 continue;
311 /* packet reception */
312 if (FD_ISSET(rtsock, &select_fd))
313 rtsock_input(rtsock);
314 if (FD_ISSET(s, &select_fd))
315 rtsol_input(s);
317 /* NOTREACHED */
319 return 0;
322 static int
323 ifconfig(char *ifname)
325 struct ifinfo *ifinfo;
326 struct sockaddr_dl *sdl;
327 int flags;
329 if ((sdl = if_nametosdl(ifname)) == NULL) {
330 warnmsg(LOG_ERR, __func__,
331 "failed to get link layer information for %s", ifname);
332 return(-1);
334 if (find_ifinfo(sdl->sdl_index)) {
335 warnmsg(LOG_ERR, __func__,
336 "interface %s was already configured", ifname);
337 free(sdl);
338 return(-1);
341 if ((ifinfo = malloc(sizeof(*ifinfo))) == NULL) {
342 warnmsg(LOG_ERR, __func__, "memory allocation failed");
343 free(sdl);
344 return(-1);
346 memset(ifinfo, 0, sizeof(*ifinfo));
347 ifinfo->sdl = sdl;
349 strncpy(ifinfo->ifname, ifname, sizeof(ifinfo->ifname));
351 /* construct a router solicitation message */
352 if (make_packet(ifinfo))
353 goto bad;
356 * check if the interface is available.
357 * also check if SIOCGIFMEDIA ioctl is OK on the interface.
359 ifinfo->mediareqok = 1;
360 ifinfo->active = interface_status(ifinfo);
361 if (!ifinfo->mediareqok) {
363 * probe routers periodically even if the link status
364 * does not change.
366 ifinfo->probeinterval = PROBE_INTERVAL;
369 /* activate interface: interface_up returns 0 on success */
370 flags = interface_up(ifinfo->ifname);
371 if (flags == 0)
372 ifinfo->state = IFS_DELAY;
373 else if (flags == IFS_TENTATIVE)
374 ifinfo->state = IFS_TENTATIVE;
375 else
376 ifinfo->state = IFS_DOWN;
378 rtsol_timer_update(ifinfo);
380 /* link into chain */
381 if (iflist)
382 ifinfo->next = iflist;
383 iflist = ifinfo;
385 return(0);
387 bad:
388 free(ifinfo->sdl);
389 free(ifinfo);
390 return(-1);
393 #if 0
394 static int
395 ifreconfig(char *ifname)
397 struct ifinfo *ifi, *prev;
398 int rv;
400 prev = NULL;
401 for (ifi = iflist; ifi; ifi = ifi->next) {
402 if (strncmp(ifi->ifname, ifname, sizeof(ifi->ifname)) == 0)
403 break;
404 prev = ifi;
406 prev->next = ifi->next;
408 rv = ifconfig(ifname);
410 /* reclaim it after ifconfig() in case ifname is pointer inside ifi */
411 if (ifi->rs_data)
412 free(ifi->rs_data);
413 free(ifi->sdl);
414 free(ifi);
416 return rv;
418 #endif
420 struct ifinfo *
421 find_ifinfo(int ifindex)
423 struct ifinfo *ifi;
425 for (ifi = iflist; ifi; ifi = ifi->next)
426 if (ifi->sdl->sdl_index == ifindex)
427 return(ifi);
429 return(NULL);
432 static int
433 make_packet(struct ifinfo *ifinfo)
435 char *buf;
436 struct nd_router_solicit *rs;
437 size_t packlen = sizeof(struct nd_router_solicit), lladdroptlen = 0;
439 if ((lladdroptlen = lladdropt_length(ifinfo->sdl)) == 0) {
440 warnmsg(LOG_INFO, __func__,
441 "link-layer address option has null length"
442 " on %s. Treat as not included.", ifinfo->ifname);
444 packlen += lladdroptlen;
445 ifinfo->rs_datalen = packlen;
447 /* allocate buffer */
448 if ((buf = malloc(packlen)) == NULL) {
449 warnmsg(LOG_ERR, __func__,
450 "memory allocation failed for %s", ifinfo->ifname);
451 return(-1);
453 ifinfo->rs_data = buf;
455 /* fill in the message */
456 rs = (struct nd_router_solicit *)buf;
457 rs->nd_rs_type = ND_ROUTER_SOLICIT;
458 rs->nd_rs_code = 0;
459 rs->nd_rs_cksum = 0;
460 rs->nd_rs_reserved = 0;
461 buf += sizeof(*rs);
463 /* fill in source link-layer address option */
464 if (lladdroptlen)
465 lladdropt_fill(ifinfo->sdl, (struct nd_opt_hdr *)buf);
467 return(0);
470 static struct timeval *
471 rtsol_check_timer(void)
473 static struct timeval returnval;
474 struct timeval now, rtsol_timer;
475 struct ifinfo *ifinfo;
476 int flags;
478 gettimeofday(&now, NULL);
480 rtsol_timer = tm_max;
482 for (ifinfo = iflist; ifinfo; ifinfo = ifinfo->next) {
483 if (TIMEVAL_LEQ(ifinfo->expire, now)) {
484 if (dflag > 1)
485 warnmsg(LOG_DEBUG, __func__,
486 "timer expiration on %s, "
487 "state = %d", ifinfo->ifname,
488 ifinfo->state);
490 switch (ifinfo->state) {
491 case IFS_DOWN:
492 case IFS_TENTATIVE:
493 /* interface_up returns 0 on success */
494 flags = interface_up(ifinfo->ifname);
495 if (flags == 0)
496 ifinfo->state = IFS_DELAY;
497 else if (flags == IFS_TENTATIVE)
498 ifinfo->state = IFS_TENTATIVE;
499 else
500 ifinfo->state = IFS_DOWN;
501 break;
502 case IFS_IDLE:
504 int oldstatus = ifinfo->active;
505 int probe = 0;
507 ifinfo->active =
508 interface_status(ifinfo);
510 if (oldstatus != ifinfo->active) {
511 warnmsg(LOG_DEBUG, __func__,
512 "%s status is changed"
513 " from %d to %d",
514 ifinfo->ifname,
515 oldstatus, ifinfo->active);
516 probe = 1;
517 ifinfo->state = IFS_DELAY;
519 else if (ifinfo->probeinterval &&
520 (ifinfo->probetimer -=
521 ifinfo->timer.tv_sec) <= 0) {
522 /* probe timer expired */
523 ifinfo->probetimer =
524 ifinfo->probeinterval;
525 probe = 1;
526 ifinfo->state = IFS_PROBE;
529 if (probe && mobile_node)
530 defrouter_probe(ifinfo->sdl->sdl_index);
531 break;
533 case IFS_DELAY:
534 ifinfo->state = IFS_PROBE;
535 sendpacket(ifinfo);
536 break;
537 case IFS_PROBE:
538 if (ifinfo->probes < MAX_RTR_SOLICITATIONS)
539 sendpacket(ifinfo);
540 else {
541 warnmsg(LOG_INFO, __func__,
542 "No answer "
543 "after sending %d RSs",
544 ifinfo->probes);
545 ifinfo->probes = 0;
546 ifinfo->state = IFS_IDLE;
548 break;
550 rtsol_timer_update(ifinfo);
553 if (TIMEVAL_LT(ifinfo->expire, rtsol_timer))
554 rtsol_timer = ifinfo->expire;
557 if (TIMEVAL_EQ(rtsol_timer, tm_max)) {
558 warnmsg(LOG_DEBUG, __func__, "there is no timer");
559 return(NULL);
561 else if (TIMEVAL_LT(rtsol_timer, now))
562 /* this may occur when the interval is too small */
563 returnval.tv_sec = returnval.tv_usec = 0;
564 else
565 TIMEVAL_SUB(&rtsol_timer, &now, &returnval);
567 if (dflag > 1)
568 warnmsg(LOG_DEBUG, __func__, "New timer is %ld:%08ld",
569 (long)returnval.tv_sec, (long)returnval.tv_usec);
571 return(&returnval);
574 void
575 rtsol_timer_update(struct ifinfo *ifinfo)
577 #define MILLION 1000000
578 #define DADRETRY 10 /* XXX: adhoc */
579 long interval;
580 struct timeval now;
582 bzero(&ifinfo->timer, sizeof(ifinfo->timer));
584 switch (ifinfo->state) {
585 case IFS_DOWN:
586 case IFS_TENTATIVE:
587 if (++ifinfo->dadcount > DADRETRY) {
588 ifinfo->dadcount = 0;
589 ifinfo->timer.tv_sec = PROBE_INTERVAL;
591 else
592 ifinfo->timer.tv_sec = 1;
593 break;
594 case IFS_IDLE:
595 if (mobile_node) {
596 /* XXX should be configurable */
597 ifinfo->timer.tv_sec = 3;
599 else
600 ifinfo->timer = tm_max; /* stop timer(valid?) */
601 break;
602 case IFS_DELAY:
603 #ifndef HAVE_ARC4RANDOM
604 interval = random() % (MAX_RTR_SOLICITATION_DELAY * MILLION);
605 #else
606 interval = arc4random() % (MAX_RTR_SOLICITATION_DELAY * MILLION);
607 #endif
608 ifinfo->timer.tv_sec = interval / MILLION;
609 ifinfo->timer.tv_usec = interval % MILLION;
610 break;
611 case IFS_PROBE:
612 if (ifinfo->probes < MAX_RTR_SOLICITATIONS)
613 ifinfo->timer.tv_sec = RTR_SOLICITATION_INTERVAL;
614 else {
616 * After sending MAX_RTR_SOLICITATIONS solicitations,
617 * we're just waiting for possible replies; there
618 * will be no more solicatation. Thus, we change
619 * the timer value to MAX_RTR_SOLICITATION_DELAY based
620 * on RFC 2461, Section 6.3.7.
622 ifinfo->timer.tv_sec = MAX_RTR_SOLICITATION_DELAY;
624 break;
625 default:
626 warnmsg(LOG_ERR, __func__,
627 "illegal interface state(%d) on %s",
628 ifinfo->state, ifinfo->ifname);
629 return;
632 /* reset the timer */
633 if (TIMEVAL_EQ(ifinfo->timer, tm_max)) {
634 ifinfo->expire = tm_max;
635 warnmsg(LOG_DEBUG, __func__,
636 "stop timer for %s", ifinfo->ifname);
638 else {
639 gettimeofday(&now, NULL);
640 TIMEVAL_ADD(&now, &ifinfo->timer, &ifinfo->expire);
642 if (dflag > 1)
643 warnmsg(LOG_DEBUG, __func__,
644 "set timer for %s to %d:%d", ifinfo->ifname,
645 (int)ifinfo->timer.tv_sec,
646 (int)ifinfo->timer.tv_usec);
649 #undef MILLION
652 /* timer related utility functions */
653 #define MILLION 1000000
655 /* result = a + b */
656 static void
657 TIMEVAL_ADD(struct timeval *a, struct timeval *b, struct timeval *result)
659 long l;
661 if ((l = a->tv_usec + b->tv_usec) < MILLION) {
662 result->tv_usec = l;
663 result->tv_sec = a->tv_sec + b->tv_sec;
665 else {
666 result->tv_usec = l - MILLION;
667 result->tv_sec = a->tv_sec + b->tv_sec + 1;
672 * result = a - b
673 * XXX: this function assumes that a >= b.
675 void
676 TIMEVAL_SUB(struct timeval *a, struct timeval *b, struct timeval *result)
678 long l;
680 if ((l = a->tv_usec - b->tv_usec) >= 0) {
681 result->tv_usec = l;
682 result->tv_sec = a->tv_sec - b->tv_sec;
684 else {
685 result->tv_usec = MILLION + l;
686 result->tv_sec = a->tv_sec - b->tv_sec - 1;
690 static void
691 rtsold_set_dump_file(int signo __unused)
693 do_dump = 1;
696 static void
697 usage(char *progname)
699 if (progname && progname[strlen(progname) - 1] != 'd') {
700 fprintf(stderr, "usage: rtsol [-dD] interfaces...\n");
701 fprintf(stderr, "usage: rtsol [-dD] -a\n");
702 } else {
703 fprintf(stderr, "usage: rtsold [-adDfm1] interfaces...\n");
704 fprintf(stderr, "usage: rtsold [-dDfm1] -a\n");
706 exit(1);
709 void
710 warnmsg(int priority, const char *func, const char *msg, ...)
712 va_list ap;
713 char buf[BUFSIZ];
715 va_start(ap, msg);
716 if (fflag) {
717 if (priority <= log_upto) {
718 vfprintf(stderr, msg, ap);
719 fprintf(stderr, "\n");
721 } else {
722 snprintf(buf, sizeof(buf), "<%s> %s", func, msg);
723 msg = buf;
724 vsyslog(priority, msg, ap);
726 va_end(ap);
729 static char **
730 autoifprobe(void)
732 #ifndef HAVE_GETIFADDRS
733 errx(1, "-a is not available with the configuration");
734 #else
735 static char ifname[IFNAMSIZ + 1];
736 static char *argv[2];
737 struct ifaddrs *ifap, *ifa, *target;
739 if (getifaddrs(&ifap) != 0)
740 return NULL;
742 target = NULL;
743 /* find an ethernet */
744 for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
745 if ((ifa->ifa_flags & IFF_UP) == 0)
746 continue;
747 if ((ifa->ifa_flags & IFF_POINTOPOINT) != 0)
748 continue;
749 if ((ifa->ifa_flags & IFF_LOOPBACK) != 0)
750 continue;
751 if ((ifa->ifa_flags & IFF_MULTICAST) == 0)
752 continue;
754 if (ifa->ifa_addr->sa_family != AF_INET6)
755 continue;
757 if (target && strcmp(target->ifa_name, ifa->ifa_name) == 0)
758 continue;
760 if (!target)
761 target = ifa;
762 else {
763 /* if we find multiple candidates, failure. */
764 if (dflag > 1)
765 warnx("multiple interfaces found");
766 target = NULL;
767 break;
771 if (target) {
772 strncpy(ifname, target->ifa_name, sizeof(ifname) - 1);
773 ifname[sizeof(ifname) - 1] = '\0';
774 argv[0] = ifname;
775 argv[1] = NULL;
777 if (dflag > 0)
778 warnx("probing %s", argv[0]);
780 freeifaddrs(ifap);
781 if (target)
782 return argv;
783 else
784 return NULL;
785 #endif