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