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