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