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