1 /* $OpenBSD: rtsold.c,v 1.37 2005/06/09 00:27:12 deraadt Exp $ */
2 /* $KAME: rtsold.c,v 1.75 2004/01/03 00:00:07 itojun Exp $ */
3
4 /*
5 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of the project nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
31 */
32
33 #include <sys/types.h>
34 #include <sys/time.h>
35 #include <sys/socket.h>
36 #include <sys/param.h>
37
38 #include <net/if.h>
39 #include <net/if_dl.h>
40
41 #include <netinet/in.h>
42 #include <netinet/icmp6.h>
43
44 #include <signal.h>
45 #include <unistd.h>
46 #include <syslog.h>
47 #include <string.h>
48 #include <stdlib.h>
49 #include <stdio.h>
50 #include <errno.h>
51 #include <err.h>
52 #include <stdarg.h>
53 #include <ifaddrs.h>
54 #include <util.h>
55 #include <poll.h>
56
57 #include "rtsold.h"
58
59 struct ifinfo *iflist;
60 struct timeval tm_max = {0x7fffffff, 0x7fffffff};
61 static int log_upto = 999;
62 static int fflag = 0;
63
64 int aflag = 0;
65 int dflag = 0;
66
67 /* protocol constants */
68 #define MAX_RTR_SOLICITATION_DELAY 1 /* second */
69 #define RTR_SOLICITATION_INTERVAL 4 /* seconds */
70 #define MAX_RTR_SOLICITATIONS 3 /* times */
71
72 /*
73 * implementation dependent constants in seconds
74 * XXX: should be configurable
75 */
76 #define PROBE_INTERVAL 60
77
78 int main(int, char **);
79
80 /* static variables and functions */
81 static int mobile_node = 0;
82 #ifndef SMALL
83 volatile sig_atomic_t do_dump;
84 static char *dumpfilename = "/var/run/rtsold.dump"; /* XXX: should be configurable */
85 #endif
86
87 #if 0
88 static int ifreconfig(char *);
89 #endif
90 int ifconfig(char *ifname);
91 void iflist_init(void);
92 static int make_packet(struct ifinfo *);
93 static struct timeval *rtsol_check_timer(void);
94 static void TIMEVAL_ADD(struct timeval *, struct timeval *, struct timeval *);
95 static void TIMEVAL_SUB(struct timeval *, struct timeval *, struct timeval *);
96
97 #ifndef SMALL
98 static void rtsold_set_dump_file(int);
99 #endif
100 static void usage(char *);
101
102 int
main(int argc,char * argv[])103 main(int argc, char *argv[])
104 {
105 int s, ch, once = 0;
106 struct timeval *timeout;
107 char *argv0, *opts;
108 struct pollfd set[2];
109 #ifdef USE_RTSOCK
110 int rtsock;
111 #endif
112
113 /*
114 * Initialization
115 */
116 argv0 = argv[0];
117
118 /* get option */
119 if (argv0 && argv0[strlen(argv0) - 1] != 'd') {
120 fflag = 1;
121 once = 1;
122 opts = "adD";
123 } else
124 opts = "adDfm1";
125
126 while ((ch = getopt(argc, argv, opts)) != -1) {
127 switch (ch) {
128 case 'a':
129 aflag = 1;
130 break;
131 case 'd':
132 dflag = 1;
133 break;
134 case 'D':
135 dflag = 2;
136 break;
137 case 'f':
138 fflag = 1;
139 break;
140 case 'm':
141 mobile_node = 1;
142 break;
143 case '1':
144 once = 1;
145 break;
146 default:
147 usage(argv0);
148 /*NOTREACHED*/
149 }
150 }
151 argc -= optind;
152 argv += optind;
153
154 if ((!aflag && argc == 0) || (aflag && argc != 0)) {
155 usage(argv0);
156 /*NOTREACHED*/
157 }
158
159 /* set log level */
160 if (dflag == 0)
161 log_upto = LOG_NOTICE;
162 if (!fflag) {
163 char *ident;
164
165 ident = strrchr(argv0, '/');
166 if (!ident)
167 ident = argv0;
168 else
169 ident++;
170 openlog(ident, LOG_NDELAY|LOG_PID, LOG_DAEMON);
171 if (log_upto >= 0)
172 setlogmask(LOG_UPTO(log_upto));
173 }
174
175 /* warn if accept_rtadv is down */
176 if (!getinet6sysctl(IPV6CTL_ACCEPT_RTADV))
177 warnx("kernel is configured not to accept RAs");
178 /* warn if forwarding is up */
179 if (getinet6sysctl(IPV6CTL_FORWARDING))
180 warnx("kernel is configured as a router, not a host");
181
182 #ifndef SMALL
183 /* initialization to dump internal status to a file */
184 signal(SIGUSR1, rtsold_set_dump_file);
185 #endif
186
187 if (!fflag)
188 daemon(0, 0); /* act as a daemon */
189
190 /*
191 * Open a socket for sending RS and receiving RA.
192 * This should be done before calling ifinit(), since the function
193 * uses the socket.
194 */
195 if ((s = sockopen()) < 0) {
196 warnmsg(LOG_ERR, __func__, "failed to open a socket");
197 exit(1);
198 /*NOTREACHED*/
199 }
200 set[0].fd = s;
201 set[0].events = POLLIN;
202
203 set[1].fd = -1;
204
205 #ifdef USE_RTSOCK
206 if ((rtsock = rtsock_open()) < 0) {
207 warnmsg(LOG_ERR, __func__, "failed to open a socket");
208 exit(1);
209 /*NOTREACHED*/
210 }
211 set[1].fd = rtsock;
212 set[1].events = POLLIN;
213 #endif
214
215 /* configuration per interface */
216 if (ifinit()) {
217 warnmsg(LOG_ERR, __func__,
218 "failed to initialize interfaces");
219 exit(1);
220 /*NOTREACHED*/
221 }
222 if (aflag)
223 argv = autoifprobe();
224 while (argv && *argv) {
225 if (ifconfig(*argv)) {
226 warnmsg(LOG_ERR, __func__,
227 "failed to initialize %s", *argv);
228 exit(1);
229 /*NOTREACHED*/
230 }
231 argv++;
232 }
233
234 /* setup for probing default routers */
235 if (probe_init()) {
236 warnmsg(LOG_ERR, __func__,
237 "failed to setup for probing routers");
238 exit(1);
239 /*NOTREACHED*/
240 }
241
242 while (1) { /* main loop */
243 int e;
244
245
246 #ifndef SMALL
247 if (do_dump) { /* SIGUSR1 */
248 do_dump = 0;
249 rtsold_dump_file(dumpfilename);
250 }
251 #endif
252
253 timeout = rtsol_check_timer();
254
255 if (once) {
256 struct ifinfo *ifi;
257
258 /* if we have no timeout, we are done (or failed) */
259 if (timeout == NULL)
260 break;
261
262 /* if all interfaces have got RA packet, we are done */
263 for (ifi = iflist; ifi; ifi = ifi->next) {
264 if (ifi->state != IFS_DOWN && ifi->racnt == 0)
265 break;
266 }
267 if (ifi == NULL)
268 break;
269 }
270 e = poll(set, 2, timeout ? (timeout->tv_sec * 1000 + timeout->tv_usec / 1000) : INFTIM);
271 if (e < 1) {
272 if (e < 0 && errno != EINTR) {
273 warnmsg(LOG_ERR, __func__, "select: %s",
274 strerror(errno));
275 }
276 continue;
277 }
278
279 /* packet reception */
280 #ifdef USE_RTSOCK
281 if (set[1].revents & POLLIN)
282 rtsock_input(rtsock);
283 #endif
284 if (set[0].revents & POLLIN)
285 rtsol_input(s);
286 }
287 /* NOTREACHED */
288
289 return 0;
290 }
291
292 int
ifconfig(char * ifname)293 ifconfig(char *ifname)
294 {
295 struct ifinfo *ifinfo;
296 struct sockaddr_dl *sdl;
297 int flags;
298
299 if ((sdl = if_nametosdl(ifname)) == NULL) {
300 warnmsg(LOG_ERR, __func__,
301 "failed to get link layer information for %s", ifname);
302 return(-1);
303 }
304 if (find_ifinfo(sdl->sdl_index)) {
305 warnmsg(LOG_ERR, __func__,
306 "interface %s was already configured", ifname);
307 free(sdl);
308 return(-1);
309 }
310
311 if ((ifinfo = malloc(sizeof(*ifinfo))) == NULL) {
312 warnmsg(LOG_ERR, __func__, "memory allocation failed");
313 free(sdl);
314 return(-1);
315 }
316 memset(ifinfo, 0, sizeof(*ifinfo));
317 ifinfo->sdl = sdl;
318
319 strncpy(ifinfo->ifname, ifname, sizeof(ifinfo->ifname));
320
321 /* construct a router solicitation message */
322 if (make_packet(ifinfo))
323 goto bad;
324
325 /* set link ID of this interface. */
326 #ifdef HAVE_SCOPELIB
327 if (inet_zoneid(AF_INET6, 2, ifname, &ifinfo->linkid))
328 goto bad;
329 #else
330 /* XXX: assume interface IDs as link IDs */
331 ifinfo->linkid = ifinfo->sdl->sdl_index;
332 #endif
333
334 /*
335 * check if the interface is available.
336 * also check if SIOCGIFMEDIA ioctl is OK on the interface.
337 */
338 ifinfo->mediareqok = 1;
339 ifinfo->active = interface_status(ifinfo);
340 if (!ifinfo->mediareqok) {
341 /*
342 * probe routers periodically even if the link status
343 * does not change.
344 */
345 ifinfo->probeinterval = PROBE_INTERVAL;
346 }
347
348 /* activate interface: interface_up returns 0 on success */
349 flags = interface_up(ifinfo->ifname);
350 if (flags == 0)
351 ifinfo->state = IFS_DELAY;
352 else if (flags == IFS_TENTATIVE)
353 ifinfo->state = IFS_TENTATIVE;
354 else
355 ifinfo->state = IFS_DOWN;
356
357 rtsol_timer_update(ifinfo);
358
359 /* link into chain */
360 if (iflist)
361 ifinfo->next = iflist;
362 iflist = ifinfo;
363
364 return(0);
365
366 bad:
367 free(ifinfo->sdl);
368 free(ifinfo);
369 return(-1);
370 }
371
372 void
iflist_init(void)373 iflist_init(void)
374 {
375 struct ifinfo *ifi, *next;
376
377 for (ifi = iflist; ifi; ifi = next) {
378 next = ifi->next;
379 if (ifi->sdl)
380 free(ifi->sdl);
381 if (ifi->rs_data)
382 free(ifi->rs_data);
383 free(ifi);
384 iflist = NULL;
385 }
386 }
387
388 #if 0
389 static int
390 ifreconfig(char *ifname)
391 {
392 struct ifinfo *ifi, *prev;
393 int rv;
394
395 prev = NULL;
396 for (ifi = iflist; ifi; ifi = ifi->next) {
397 if (strncmp(ifi->ifname, ifname, sizeof(ifi->ifname)) == 0)
398 break;
399 prev = ifi;
400 }
401 prev->next = ifi->next;
402
403 rv = ifconfig(ifname);
404
405 /* reclaim it after ifconfig() in case ifname is pointer inside ifi */
406 if (ifi->rs_data)
407 free(ifi->rs_data);
408 free(ifi->sdl);
409 free(ifi);
410 return rv;
411 }
412 #endif
413
414 struct ifinfo *
find_ifinfo(int ifindex)415 find_ifinfo(int ifindex)
416 {
417 struct ifinfo *ifi;
418
419 for (ifi = iflist; ifi; ifi = ifi->next)
420 if (ifi->sdl->sdl_index == ifindex)
421 return(ifi);
422 return(NULL);
423 }
424
425 static int
make_packet(struct ifinfo * ifinfo)426 make_packet(struct ifinfo *ifinfo)
427 {
428 size_t packlen = sizeof(struct nd_router_solicit), lladdroptlen = 0;
429 struct nd_router_solicit *rs;
430 char *buf;
431
432 if ((lladdroptlen = lladdropt_length(ifinfo->sdl)) == 0) {
433 warnmsg(LOG_INFO, __func__,
434 "link-layer address option has null length"
435 " on %s. Treat as not included.", ifinfo->ifname);
436 }
437 packlen += lladdroptlen;
438 ifinfo->rs_datalen = packlen;
439
440 /* allocate buffer */
441 if ((buf = malloc(packlen)) == NULL) {
442 warnmsg(LOG_ERR, __func__,
443 "memory allocation failed for %s", ifinfo->ifname);
444 return(-1);
445 }
446 ifinfo->rs_data = buf;
447
448 /* fill in the message */
449 rs = (struct nd_router_solicit *)buf;
450 rs->nd_rs_type = ND_ROUTER_SOLICIT;
451 rs->nd_rs_code = 0;
452 rs->nd_rs_cksum = 0;
453 rs->nd_rs_reserved = 0;
454 buf += sizeof(*rs);
455
456 /* fill in source link-layer address option */
457 if (lladdroptlen)
458 lladdropt_fill(ifinfo->sdl, (struct nd_opt_hdr *)buf);
459
460 return(0);
461 }
462
463 static struct timeval *
rtsol_check_timer(void)464 rtsol_check_timer(void)
465 {
466 static struct timeval returnval;
467 struct timeval now, rtsol_timer;
468 struct ifinfo *ifinfo;
469 int flags;
470
471 gettimeofday(&now, NULL);
472
473 rtsol_timer = tm_max;
474
475 for (ifinfo = iflist; ifinfo; ifinfo = ifinfo->next) {
476 if (timercmp(&ifinfo->expire, &now, <=)) {
477 if (dflag > 1)
478 warnmsg(LOG_DEBUG, __func__,
479 "timer expiration on %s, "
480 "state = %d", ifinfo->ifname,
481 ifinfo->state);
482
483 switch (ifinfo->state) {
484 case IFS_DOWN:
485 case IFS_TENTATIVE:
486 /* interface_up returns 0 on success */
487 flags = interface_up(ifinfo->ifname);
488 if (flags == 0)
489 ifinfo->state = IFS_DELAY;
490 else if (flags == IFS_TENTATIVE)
491 ifinfo->state = IFS_TENTATIVE;
492 else
493 ifinfo->state = IFS_DOWN;
494 break;
495 case IFS_IDLE:
496 {
497 int oldstatus = ifinfo->active;
498 int probe = 0;
499
500 ifinfo->active = interface_status(ifinfo);
501
502 if (oldstatus != ifinfo->active) {
503 warnmsg(LOG_DEBUG, __func__,
504 "%s status is changed"
505 " from %d to %d",
506 ifinfo->ifname,
507 oldstatus, ifinfo->active);
508 probe = 1;
509 ifinfo->state = IFS_DELAY;
510 } else if (ifinfo->probeinterval &&
511 (ifinfo->probetimer -=
512 ifinfo->timer.tv_sec) <= 0) {
513 /* probe timer expired */
514 ifinfo->probetimer =
515 ifinfo->probeinterval;
516 probe = 1;
517 ifinfo->state = IFS_PROBE;
518 }
519
520 if (probe && mobile_node)
521 defrouter_probe(ifinfo);
522 break;
523 }
524 case IFS_DELAY:
525 ifinfo->state = IFS_PROBE;
526 sendpacket(ifinfo);
527 break;
528 case IFS_PROBE:
529 if (ifinfo->probes < MAX_RTR_SOLICITATIONS)
530 sendpacket(ifinfo);
531 else {
532 warnmsg(LOG_INFO, __func__,
533 "No answer after sending %d RSs",
534 ifinfo->probes);
535 ifinfo->probes = 0;
536 ifinfo->state = IFS_IDLE;
537 }
538 break;
539 }
540 rtsol_timer_update(ifinfo);
541 }
542
543 if (timercmp(&ifinfo->expire, &rtsol_timer, <))
544 rtsol_timer = ifinfo->expire;
545 }
546
547 if (timercmp(&rtsol_timer, &tm_max, ==)) {
548 warnmsg(LOG_DEBUG, __func__, "there is no timer");
549 return(NULL);
550 } else if (timercmp(&rtsol_timer, &now, <))
551 /* this may occur when the interval is too small */
552 returnval.tv_sec = returnval.tv_usec = 0;
553 else
554 timersub(&rtsol_timer, &now, &returnval);
555
556 if (dflag > 1)
557 warnmsg(LOG_DEBUG, __func__, "New timer is %ld:%08ld",
558 (long)returnval.tv_sec, (long)returnval.tv_usec);
559
560 return(&returnval);
561 }
562
563 void
rtsol_timer_update(struct ifinfo * ifinfo)564 rtsol_timer_update(struct ifinfo *ifinfo)
565 {
566 #define MILLION 1000000
567 #define DADRETRY 10 /* XXX: adhoc */
568 long interval;
569 struct timeval now;
570
571 bzero(&ifinfo->timer, sizeof(ifinfo->timer));
572
573 switch (ifinfo->state) {
574 case IFS_DOWN:
575 case IFS_TENTATIVE:
576 if (++ifinfo->dadcount > DADRETRY) {
577 ifinfo->dadcount = 0;
578 ifinfo->timer.tv_sec = PROBE_INTERVAL;
579 } else
580 ifinfo->timer.tv_sec = 1;
581 break;
582 case IFS_IDLE:
583 if (mobile_node) {
584 /* XXX should be configurable */
585 ifinfo->timer.tv_sec = 3;
586 } else
587 ifinfo->timer = tm_max; /* stop timer(valid?) */
588 break;
589 case IFS_DELAY:
590 interval = arc4random_uniform(MAX_RTR_SOLICITATION_DELAY * MILLION);
591 ifinfo->timer.tv_sec = interval / MILLION;
592 ifinfo->timer.tv_usec = interval % MILLION;
593 break;
594 case IFS_PROBE:
595 if (ifinfo->probes < MAX_RTR_SOLICITATIONS)
596 ifinfo->timer.tv_sec = RTR_SOLICITATION_INTERVAL;
597 else {
598 /*
599 * After sending MAX_RTR_SOLICITATIONS solicitations,
600 * we're just waiting for possible replies; there
601 * will be no more solicitation. Thus, we change
602 * the timer value to MAX_RTR_SOLICITATION_DELAY based
603 * on RFC 2461, Section 6.3.7.
604 */
605 ifinfo->timer.tv_sec = MAX_RTR_SOLICITATION_DELAY;
606 }
607 break;
608 default:
609 warnmsg(LOG_ERR, __func__,
610 "illegal interface state(%d) on %s",
611 ifinfo->state, ifinfo->ifname);
612 return;
613 }
614
615 /* reset the timer */
616 if (timercmp(&ifinfo->timer, &tm_max, ==)) {
617 ifinfo->expire = tm_max;
618 warnmsg(LOG_DEBUG, __func__,
619 "stop timer for %s", ifinfo->ifname);
620 } else {
621 gettimeofday(&now, NULL);
622 timeradd(&now, &ifinfo->timer, &ifinfo->expire);
623
624 if (dflag > 1)
625 warnmsg(LOG_DEBUG, __func__,
626 "set timer for %s to %d:%d", ifinfo->ifname,
627 (int)ifinfo->timer.tv_sec,
628 (int)ifinfo->timer.tv_usec);
629 }
630
631 #undef MILLION
632 }
633
634 /* timer related utility functions */
635 #define MILLION 1000000
636
637 #ifndef SMALL
638 static void
rtsold_set_dump_file(int sig)639 rtsold_set_dump_file(int sig)
640 {
641 do_dump = 1;
642 }
643 #endif
644
645 static void
usage(char * progname)646 usage(char *progname)
647 {
648 if (progname && progname[strlen(progname) - 1] != 'd') {
649 fprintf(stderr, "usage: rtsol [-dDF] interfaces...\n");
650 fprintf(stderr, "usage: rtsol [-dDF] -a\n");
651 } else {
652 fprintf(stderr, "usage: rtsold [-adDfFm1] interfaces...\n");
653 fprintf(stderr, "usage: rtsold [-dDfFm1] -a\n");
654 }
655 exit(1);
656 }
657
658 void
warnmsg(int priority,const char * func,const char * msg,...)659 warnmsg(int priority, const char *func, const char *msg, ...)
660 {
661 va_list ap;
662 char buf[BUFSIZ];
663
664 va_start(ap, msg);
665 if (fflag) {
666 if (priority <= log_upto) {
667 (void)vfprintf(stderr, msg, ap);
668 (void)fprintf(stderr, "\n");
669 }
670 } else {
671 snprintf(buf, sizeof(buf), "<%s> %s", func, msg);
672 msg = buf;
673 vsyslog(priority, msg, ap);
674 }
675 va_end(ap);
676 }
677
678 /*
679 * return a list of interfaces which is suitable to sending an RS.
680 */
681 char **
autoifprobe(void)682 autoifprobe(void)
683 {
684 static char **argv = NULL;
685 static int n = 0;
686 char **a;
687 int i, found;
688 struct ifaddrs *ifap, *ifa, *target;
689
690 /* initialize */
691 while (n--)
692 free(argv[n]);
693 if (argv) {
694 free(argv);
695 argv = NULL;
696 }
697 n = 0;
698
699 if (getifaddrs(&ifap) != 0)
700 return NULL;
701
702 target = NULL;
703 /* find an ethernet */
704 for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
705 if ((ifa->ifa_flags & IFF_UP) == 0)
706 continue;
707 if ((ifa->ifa_flags & IFF_POINTOPOINT) != 0)
708 continue;
709 if ((ifa->ifa_flags & IFF_LOOPBACK) != 0)
710 continue;
711 if ((ifa->ifa_flags & IFF_MULTICAST) == 0)
712 continue;
713
714 if (ifa->ifa_addr->sa_family != AF_INET6)
715 continue;
716
717 found = 0;
718 for (i = 0; i < n; i++) {
719 if (strcmp(argv[i], ifa->ifa_name) == 0) {
720 found++;
721 break;
722 }
723 }
724 if (found)
725 continue;
726
727 /* if we find multiple candidates, just warn. */
728 if (n != 0 && dflag > 1)
729 warnx("multiple interfaces found");
730
731 a = (char **)realloc(argv, (n + 1) * sizeof(char **));
732 if (a == NULL)
733 err(1, "realloc");
734 argv = a;
735 if ((argv[n] = strdup(ifa->ifa_name)) == NULL)
736 err(1, "strdup");
737 n++;
738 }
739
740 if (n) {
741 a = (char **)realloc(argv, (n + 1) * sizeof(char **));
742 if (a == NULL)
743 err(1, "realloc");
744 argv = a;
745 argv[n] = NULL;
746
747 if (dflag > 0) {
748 for (i = 0; i < n; i++)
749 warnx("probing %s", argv[i]);
750 }
751 }
752 freeifaddrs(ifap);
753 return argv;
754 }
755