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