xref: /dragonfly/usr.sbin/rtsold/rtsold.c (revision 2ee85085)
1 /*	$KAME: rtsold.c,v 1.31 2001/05/22 06:03:06 jinmei Exp $	*/
2 
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.
18  *
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.
30  *
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.6 2005/02/17 14:00:10 joerg Exp $
33  */
34 
35 #include <sys/types.h>
36 #include <sys/time.h>
37 #include <sys/socket.h>
38 
39 #include <net/if.h>
40 #include <net/if_dl.h>
41 
42 #include <netinet/in.h>
43 #include <netinet/icmp6.h>
44 
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"
56 
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;
63 
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 */
68 
69 /* implementation dependent constants */
70 #define PROBE_INTERVAL 60	/* secondes XXX: should be configurable */
71 
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)))
77 
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)))
82 
83 /* a == b */
84 #define TIMEVAL_EQ(a, b) (((a).tv_sec==(b).tv_sec) && ((a).tv_usec==(b).tv_usec))
85 
86 int main(int argc, char *argv[]);
87 
88 /* static variables and functions */
89 static int mobile_node = 0;
90 static int do_dump;
91 /*
92  * XXX: the following two values should be configurable
93  */
94 static const char *dumpfilename = "/var/run/rtsold.dump";
95 static const char *pidfilename = "/var/run/rtsold.pid";
96 
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);
107 
108 static void rtsold_set_dump_file(int);
109 static void usage(char *progname);
110 static char **autoifprobe(void);
111 
112 int
113 main(argc, argv)
114 	int argc;
115 	char *argv[];
116 {
117 	int s, rtsock, maxfd, ch;
118 	int once = 0;
119 	struct timeval *timeout;
120 	struct fd_set fdset;
121 	char *argv0;
122 	const char *opts;
123 
124 	/*
125 	 * Initialization
126 	 */
127 	argv0 = argv[0];
128 
129 	/* get option */
130 	if (argv0 && argv0[strlen(argv0) - 1] != 'd') {
131 		fflag = 1;
132 		once = 1;
133 		opts = "adD";
134 	} else
135 		opts = "adDfm1";
136 
137 	while ((ch = getopt(argc, argv, opts)) != -1) {
138 		switch (ch) {
139 		case 'a':
140 			aflag = 1;
141 			break;
142 		case 'd':
143 			dflag = 1;
144 			break;
145 		case 'D':
146 			dflag = 2;
147 			break;
148 		case 'f':
149 			fflag = 1;
150 			break;
151 		case 'm':
152 			mobile_node = 1;
153 			break;
154 		case '1':
155 			once = 1;
156 			break;
157 		default:
158 			usage(argv0);
159 			/*NOTREACHED*/
160 		}
161 	}
162 	argc -= optind;
163 	argv += optind;
164 
165 	if (aflag) {
166 		int i;
167 
168 		if (argc != 0) {
169 			usage(argv0);
170 			/*NOTREACHED*/
171 		}
172 
173 		argv = autoifprobe();
174 		if (!argv) {
175 			errx(1, "could not autoprobe interface");
176 			/*NOTREACHED*/
177 		}
178 
179 		for (i = 0; argv[i]; i++)
180 			;
181 		argc = i;
182 	}
183 	if (argc == 0) {
184 		usage(argv0);
185 		/*NOTREACHED*/
186 	}
187 
188 	/* set log level */
189 	if (dflag == 0)
190 		log_upto = LOG_NOTICE;
191 	if (!fflag) {
192 		char *ident;
193 		ident = strrchr(argv0, '/');
194 		if (!ident)
195 			ident = argv0;
196 		else
197 			ident++;
198 		openlog(ident, LOG_NDELAY|LOG_PID, LOG_DAEMON);
199 		if (log_upto >= 0)
200 			setlogmask(LOG_UPTO(log_upto));
201 	}
202 
203 #ifndef HAVE_ARC4RANDOM
204 	/* random value initilization */
205 	srandom((u_long)time(NULL));
206 #endif
207 
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");
214 
215 	/* initialization to dump internal status to a file */
216 	if (signal(SIGUSR1, rtsold_set_dump_file) == SIG_ERR) {
217 		errx(1, "failed to set signal for dump status");
218 		/*NOTREACHED*/
219 	}
220 
221 	/*
222 	 * Open a socket for sending RS and receiving RA.
223 	 * This should be done before calling ifinit(), since the function
224 	 * uses the socket.
225 	 */
226 	if ((s = sockopen()) < 0) {
227 		errx(1, "failed to open a socket");
228 		/*NOTREACHED*/
229 	}
230 	maxfd = s;
231 	if ((rtsock = rtsock_open()) < 0) {
232 		errx(1, "failed to open a socket");
233 		/*NOTREACHED*/
234 	}
235 	if (rtsock > maxfd)
236 		maxfd = rtsock;
237 
238 	/* configuration per interface */
239 	if (ifinit()) {
240 		errx(1, "failed to initilizatoin interfaces");
241 		/*NOTREACHED*/
242 	}
243 	while (argc--) {
244 		if (ifconfig(*argv)) {
245 			errx(1, "failed to initialize %s", *argv);
246 			/*NOTREACHED*/
247 		}
248 		argv++;
249 	}
250 
251 	/* setup for probing default routers */
252 	if (probe_init()) {
253 		errx(1, "failed to setup for probing routers");
254 		/*NOTREACHED*/
255 	}
256 
257 	if (!fflag)
258 		daemon(0, 0);		/* act as a daemon */
259 
260 	/* dump the current pid */
261 	if (!once) {
262 		pid_t pid = getpid();
263 		FILE *fp;
264 
265 		if ((fp = fopen(pidfilename, "w")) == NULL)
266 			warnmsg(LOG_ERR, __func__,
267 				"failed to open a log file(%s): %s",
268 				pidfilename, strerror(errno));
269 		else {
270 			fprintf(fp, "%d\n", pid);
271 			fclose(fp);
272 		}
273 	}
274 
275 	FD_ZERO(&fdset);
276 	FD_SET(s, &fdset);
277 	FD_SET(rtsock, &fdset);
278 	while (1) {		/* main loop */
279 		int e;
280 		struct fd_set select_fd = fdset;
281 
282 		if (do_dump) {	/* SIGUSR1 */
283 			do_dump = 0;
284 			rtsold_dump_file(dumpfilename);
285 		}
286 
287 		timeout = rtsol_check_timer();
288 
289 		if (once) {
290 			struct ifinfo *ifi;
291 
292 			/* if we have no timeout, we are done (or failed) */
293 			if (timeout == NULL)
294 				break;
295 
296 			/* if all interfaces have got RA packet, we are done */
297 			for (ifi = iflist; ifi; ifi = ifi->next) {
298 				if (ifi->state != IFS_DOWN && ifi->racnt == 0)
299 					break;
300 			}
301 			if (ifi == NULL)
302 				break;
303 		}
304 		e = select(maxfd + 1, &select_fd, NULL, NULL, timeout);
305 		if (e < 1) {
306 			if (e < 0 && errno != EINTR) {
307 				warnmsg(LOG_ERR, __func__, "select: %s",
308 				       strerror(errno));
309 			}
310 			continue;
311 		}
312 
313 		/* packet reception */
314 		if (FD_ISSET(rtsock, &select_fd))
315 			rtsock_input(rtsock);
316 		if (FD_ISSET(s, &select_fd))
317 			rtsol_input(s);
318 	}
319 	/* NOTREACHED */
320 
321 	return 0;
322 }
323 
324 static int
325 ifconfig(char *ifname)
326 {
327 	struct ifinfo *ifinfo;
328 	struct sockaddr_dl *sdl;
329 	int flags;
330 
331 	if ((sdl = if_nametosdl(ifname)) == NULL) {
332 		warnmsg(LOG_ERR, __func__,
333 		       "failed to get link layer information for %s", ifname);
334 		return(-1);
335 	}
336 	if (find_ifinfo(sdl->sdl_index)) {
337 		warnmsg(LOG_ERR, __func__,
338 			"interface %s was already configured", ifname);
339 		free(sdl);
340 		return(-1);
341 	}
342 
343 	if ((ifinfo = malloc(sizeof(*ifinfo))) == NULL) {
344 		warnmsg(LOG_ERR, __func__, "memory allocation failed");
345 		free(sdl);
346 		return(-1);
347 	}
348 	memset(ifinfo, 0, sizeof(*ifinfo));
349 	ifinfo->sdl = sdl;
350 
351 	strncpy(ifinfo->ifname, ifname, sizeof(ifinfo->ifname));
352 
353 	/* construct a router solicitation message */
354 	if (make_packet(ifinfo))
355 		goto bad;
356 
357 	/*
358 	 * check if the interface is available.
359 	 * also check if SIOCGIFMEDIA ioctl is OK on the interface.
360 	 */
361 	ifinfo->mediareqok = 1;
362 	ifinfo->active = interface_status(ifinfo);
363 	if (!ifinfo->mediareqok) {
364 		/*
365 		 * probe routers periodically even if the link status
366 		 * does not change.
367 		 */
368 		ifinfo->probeinterval = PROBE_INTERVAL;
369 	}
370 
371 	/* activate interface: interface_up returns 0 on success */
372 	flags = interface_up(ifinfo->ifname);
373 	if (flags == 0)
374 		ifinfo->state = IFS_DELAY;
375 	else if (flags == IFS_TENTATIVE)
376 		ifinfo->state = IFS_TENTATIVE;
377 	else
378 		ifinfo->state = IFS_DOWN;
379 
380 	rtsol_timer_update(ifinfo);
381 
382 	/* link into chain */
383 	if (iflist)
384 		ifinfo->next = iflist;
385 	iflist = ifinfo;
386 
387 	return(0);
388 
389   bad:
390 	free(ifinfo->sdl);
391 	free(ifinfo);
392 	return(-1);
393 }
394 
395 #if 0
396 static int
397 ifreconfig(char *ifname)
398 {
399 	struct ifinfo *ifi, *prev;
400 	int rv;
401 
402 	prev = NULL;
403 	for (ifi = iflist; ifi; ifi = ifi->next) {
404 		if (strncmp(ifi->ifname, ifname, sizeof(ifi->ifname)) == 0)
405 			break;
406 		prev = ifi;
407 	}
408 	prev->next = ifi->next;
409 
410 	rv = ifconfig(ifname);
411 
412 	/* reclaim it after ifconfig() in case ifname is pointer inside ifi */
413 	if (ifi->rs_data)
414 		free(ifi->rs_data);
415 	free(ifi->sdl);
416 	free(ifi);
417 
418 	return rv;
419 }
420 #endif
421 
422 struct ifinfo *
423 find_ifinfo(int ifindex)
424 {
425 	struct ifinfo *ifi;
426 
427 	for (ifi = iflist; ifi; ifi = ifi->next)
428 		if (ifi->sdl->sdl_index == ifindex)
429 			return(ifi);
430 
431 	return(NULL);
432 }
433 
434 static int
435 make_packet(struct ifinfo *ifinfo)
436 {
437 	char *buf;
438 	struct nd_router_solicit *rs;
439 	size_t packlen = sizeof(struct nd_router_solicit), lladdroptlen = 0;
440 
441 	if ((lladdroptlen = lladdropt_length(ifinfo->sdl)) == 0) {
442 		warnmsg(LOG_INFO, __func__,
443 			"link-layer address option has null length"
444 		       " on %s. Treat as not included.", ifinfo->ifname);
445 	}
446 	packlen += lladdroptlen;
447 	ifinfo->rs_datalen = packlen;
448 
449 	/* allocate buffer */
450 	if ((buf = malloc(packlen)) == NULL) {
451 		warnmsg(LOG_ERR, __func__,
452 			"memory allocation failed for %s", ifinfo->ifname);
453 		return(-1);
454 	}
455 	ifinfo->rs_data = buf;
456 
457 	/* fill in the message */
458 	rs = (struct nd_router_solicit *)buf;
459 	rs->nd_rs_type = ND_ROUTER_SOLICIT;
460 	rs->nd_rs_code = 0;
461 	rs->nd_rs_cksum = 0;
462 	rs->nd_rs_reserved = 0;
463 	buf += sizeof(*rs);
464 
465 	/* fill in source link-layer address option */
466 	if (lladdroptlen)
467 		lladdropt_fill(ifinfo->sdl, (struct nd_opt_hdr *)buf);
468 
469 	return(0);
470 }
471 
472 static struct timeval *
473 rtsol_check_timer()
474 {
475 	static struct timeval returnval;
476 	struct timeval now, rtsol_timer;
477 	struct ifinfo *ifinfo;
478 	int flags;
479 
480 	gettimeofday(&now, NULL);
481 
482 	rtsol_timer = tm_max;
483 
484 	for (ifinfo = iflist; ifinfo; ifinfo = ifinfo->next) {
485 		if (TIMEVAL_LEQ(ifinfo->expire, now)) {
486 			if (dflag > 1)
487 				warnmsg(LOG_DEBUG, __func__,
488 					"timer expiration on %s, "
489 				       "state = %d", ifinfo->ifname,
490 				       ifinfo->state);
491 
492 			switch (ifinfo->state) {
493 			case IFS_DOWN:
494 			case IFS_TENTATIVE:
495 				/* interface_up returns 0 on success */
496 				flags = interface_up(ifinfo->ifname);
497 				if (flags == 0)
498 					ifinfo->state = IFS_DELAY;
499 				else if (flags == IFS_TENTATIVE)
500 					ifinfo->state = IFS_TENTATIVE;
501 				else
502 					ifinfo->state = IFS_DOWN;
503 				break;
504 			case IFS_IDLE:
505 			{
506 				int oldstatus = ifinfo->active;
507 				int probe = 0;
508 
509 				ifinfo->active =
510 					interface_status(ifinfo);
511 
512 				if (oldstatus != ifinfo->active) {
513 					warnmsg(LOG_DEBUG, __func__,
514 						"%s status is changed"
515 						" from %d to %d",
516 						ifinfo->ifname,
517 						oldstatus, ifinfo->active);
518 					probe = 1;
519 					ifinfo->state = IFS_DELAY;
520 				}
521 				else if (ifinfo->probeinterval &&
522 					 (ifinfo->probetimer -=
523 					  ifinfo->timer.tv_sec) <= 0) {
524 					/* probe timer expired */
525 					ifinfo->probetimer =
526 						ifinfo->probeinterval;
527 					probe = 1;
528 					ifinfo->state = IFS_PROBE;
529 				}
530 
531 				if (probe && mobile_node)
532 					defrouter_probe(ifinfo->sdl->sdl_index);
533 				break;
534 			}
535 			case IFS_DELAY:
536 				ifinfo->state = IFS_PROBE;
537 				sendpacket(ifinfo);
538 				break;
539 			case IFS_PROBE:
540 				if (ifinfo->probes < MAX_RTR_SOLICITATIONS)
541 					sendpacket(ifinfo);
542 				else {
543 					warnmsg(LOG_INFO, __func__,
544 						"No answer "
545 						"after sending %d RSs",
546 						ifinfo->probes);
547 					ifinfo->probes = 0;
548 					ifinfo->state = IFS_IDLE;
549 				}
550 				break;
551 			}
552 			rtsol_timer_update(ifinfo);
553 		}
554 
555 		if (TIMEVAL_LT(ifinfo->expire, rtsol_timer))
556 			rtsol_timer = ifinfo->expire;
557 	}
558 
559 	if (TIMEVAL_EQ(rtsol_timer, tm_max)) {
560 		warnmsg(LOG_DEBUG, __func__, "there is no timer");
561 		return(NULL);
562 	}
563 	else if (TIMEVAL_LT(rtsol_timer, now))
564 		/* this may occur when the interval is too small */
565 		returnval.tv_sec = returnval.tv_usec = 0;
566 	else
567 		TIMEVAL_SUB(&rtsol_timer, &now, &returnval);
568 
569 	if (dflag > 1)
570 		warnmsg(LOG_DEBUG, __func__, "New timer is %ld:%08ld",
571 			(long)returnval.tv_sec, (long)returnval.tv_usec);
572 
573 	return(&returnval);
574 }
575 
576 void
577 rtsol_timer_update(struct ifinfo *ifinfo)
578 {
579 #define MILLION 1000000
580 #define DADRETRY 10		/* XXX: adhoc */
581 	long interval;
582 	struct timeval now;
583 
584 	bzero(&ifinfo->timer, sizeof(ifinfo->timer));
585 
586 	switch (ifinfo->state) {
587 	case IFS_DOWN:
588 	case IFS_TENTATIVE:
589 		if (++ifinfo->dadcount > DADRETRY) {
590 			ifinfo->dadcount = 0;
591 			ifinfo->timer.tv_sec = PROBE_INTERVAL;
592 		}
593 		else
594 			ifinfo->timer.tv_sec = 1;
595 		break;
596 	case IFS_IDLE:
597 		if (mobile_node) {
598 			/* XXX should be configurable */
599 			ifinfo->timer.tv_sec = 3;
600 		}
601 		else
602 			ifinfo->timer = tm_max;	/* stop timer(valid?) */
603 		break;
604 	case IFS_DELAY:
605 #ifndef HAVE_ARC4RANDOM
606 		interval = random() % (MAX_RTR_SOLICITATION_DELAY * MILLION);
607 #else
608 		interval = arc4random() % (MAX_RTR_SOLICITATION_DELAY * MILLION);
609 #endif
610 		ifinfo->timer.tv_sec = interval / MILLION;
611 		ifinfo->timer.tv_usec = interval % MILLION;
612 		break;
613 	case IFS_PROBE:
614 		if (ifinfo->probes < MAX_RTR_SOLICITATIONS)
615 			ifinfo->timer.tv_sec = RTR_SOLICITATION_INTERVAL;
616 		else {
617 			/*
618 			 * After sending MAX_RTR_SOLICITATIONS solicitations,
619 			 * we're just waiting for possible replies; there
620 			 * will be no more solicatation.  Thus, we change
621 			 * the timer value to MAX_RTR_SOLICITATION_DELAY based
622 			 * on RFC 2461, Section 6.3.7.
623 			 */
624 			ifinfo->timer.tv_sec = MAX_RTR_SOLICITATION_DELAY;
625 		}
626 		break;
627 	default:
628 		warnmsg(LOG_ERR, __func__,
629 			"illegal interface state(%d) on %s",
630 			ifinfo->state, ifinfo->ifname);
631 		return;
632 	}
633 
634 	/* reset the timer */
635 	if (TIMEVAL_EQ(ifinfo->timer, tm_max)) {
636 		ifinfo->expire = tm_max;
637 		warnmsg(LOG_DEBUG, __func__,
638 			"stop timer for %s", ifinfo->ifname);
639 	}
640 	else {
641 		gettimeofday(&now, NULL);
642 		TIMEVAL_ADD(&now, &ifinfo->timer, &ifinfo->expire);
643 
644 		if (dflag > 1)
645 			warnmsg(LOG_DEBUG, __func__,
646 				"set timer for %s to %d:%d", ifinfo->ifname,
647 			       (int)ifinfo->timer.tv_sec,
648 			       (int)ifinfo->timer.tv_usec);
649 	}
650 
651 #undef MILLION
652 }
653 
654 /* timer related utility functions */
655 #define MILLION 1000000
656 
657 /* result = a + b */
658 static void
659 TIMEVAL_ADD(struct timeval *a, struct timeval *b, struct timeval *result)
660 {
661 	long l;
662 
663 	if ((l = a->tv_usec + b->tv_usec) < MILLION) {
664 		result->tv_usec = l;
665 		result->tv_sec = a->tv_sec + b->tv_sec;
666 	}
667 	else {
668 		result->tv_usec = l - MILLION;
669 		result->tv_sec = a->tv_sec + b->tv_sec + 1;
670 	}
671 }
672 
673 /*
674  * result = a - b
675  * XXX: this function assumes that a >= b.
676  */
677 void
678 TIMEVAL_SUB(struct timeval *a, struct timeval *b, struct timeval *result)
679 {
680 	long l;
681 
682 	if ((l = a->tv_usec - b->tv_usec) >= 0) {
683 		result->tv_usec = l;
684 		result->tv_sec = a->tv_sec - b->tv_sec;
685 	}
686 	else {
687 		result->tv_usec = MILLION + l;
688 		result->tv_sec = a->tv_sec - b->tv_sec - 1;
689 	}
690 }
691 
692 static void
693 rtsold_set_dump_file(int signo __unused)
694 {
695 	do_dump = 1;
696 }
697 
698 static void
699 usage(char *progname)
700 {
701 	if (progname && progname[strlen(progname) - 1] != 'd') {
702 		fprintf(stderr, "usage: rtsol [-dD] interfaces...\n");
703 		fprintf(stderr, "usage: rtsol [-dD] -a\n");
704 	} else {
705 		fprintf(stderr, "usage: rtsold [-adDfm1] interfaces...\n");
706 		fprintf(stderr, "usage: rtsold [-dDfm1] -a\n");
707 	}
708 	exit(1);
709 }
710 
711 void
712 #if __STDC__
713 warnmsg(int priority, const char *func, const char *msg, ...)
714 #else
715 warnmsg(priority, func, msg, va_alist)
716 	int priority;
717 	const char *func;
718 	const char *msg;
719 	va_dcl
720 #endif
721 {
722 	va_list ap;
723 	char buf[BUFSIZ];
724 
725 	va_start(ap, msg);
726 	if (fflag) {
727 		if (priority <= log_upto) {
728 			vfprintf(stderr, msg, ap);
729 			fprintf(stderr, "\n");
730 		}
731 	} else {
732 		snprintf(buf, sizeof(buf), "<%s> %s", func, msg);
733 		msg = buf;
734 		vsyslog(priority, msg, ap);
735 	}
736 	va_end(ap);
737 }
738 
739 static char **
740 autoifprobe()
741 {
742 #ifndef HAVE_GETIFADDRS
743 	errx(1, "-a is not available with the configuration");
744 #else
745 	static char ifname[IFNAMSIZ + 1];
746 	static char *argv[2];
747 	struct ifaddrs *ifap, *ifa, *target;
748 
749 	if (getifaddrs(&ifap) != 0)
750 		return NULL;
751 
752 	target = NULL;
753 	/* find an ethernet */
754 	for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
755 		if ((ifa->ifa_flags & IFF_UP) == 0)
756 			continue;
757 		if ((ifa->ifa_flags & IFF_POINTOPOINT) != 0)
758 			continue;
759 		if ((ifa->ifa_flags & IFF_LOOPBACK) != 0)
760 			continue;
761 		if ((ifa->ifa_flags & IFF_MULTICAST) == 0)
762 			continue;
763 
764 		if (ifa->ifa_addr->sa_family != AF_INET6)
765 			continue;
766 
767 		if (target && strcmp(target->ifa_name, ifa->ifa_name) == 0)
768 			continue;
769 
770 		if (!target)
771 			target = ifa;
772 		else {
773 			/* if we find multiple candidates, failure. */
774 			if (dflag > 1)
775 				warnx("multiple interfaces found");
776 			target = NULL;
777 			break;
778 		}
779 	}
780 
781 	if (target) {
782 		strncpy(ifname, target->ifa_name, sizeof(ifname) - 1);
783 		ifname[sizeof(ifname) - 1] = '\0';
784 		argv[0] = ifname;
785 		argv[1] = NULL;
786 
787 		if (dflag > 0)
788 			warnx("probing %s", argv[0]);
789 	}
790 	freeifaddrs(ifap);
791 	if (target)
792 		return argv;
793 	else
794 		return (char **)NULL;
795 #endif
796 }
797