]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - usr.sbin/rtsold/rtsold.c
Remove dead code.
[FreeBSD/FreeBSD.git] / usr.sbin / rtsold / rtsold.c
1 /*      $KAME: rtsold.c,v 1.67 2003/05/17 18:16:15 itojun Exp $ */
2
3 /*-
4  * SPDX-License-Identifier: BSD-3-Clause
5  *
6  * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
7  * All rights reserved.
8  * 
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  * 3. Neither the name of the project nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  * 
21  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  *
33  * $FreeBSD$
34  */
35
36 #include <sys/param.h>
37 #include <sys/ioctl.h>
38 #include <sys/socket.h>
39
40 #include <net/if.h>
41 #include <net/if_dl.h>
42
43 #include <netinet/in.h>
44 #include <netinet/icmp6.h>
45 #include <netinet/in_var.h>
46 #include <arpa/inet.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 <time.h>
57 #include <errno.h>
58 #include <err.h>
59 #include <stdarg.h>
60 #include <ifaddrs.h>
61 #include <poll.h>
62
63 #include "rtsold.h"
64
65 #define RTSOL_DUMPFILE  "/var/run/rtsold.dump";
66 #define RTSOL_PIDFILE   "/var/run/rtsold.pid";
67
68 struct timespec tm_max;
69 static int log_upto = 999;
70 static int fflag = 0;
71
72 int Fflag = 0;  /* force setting sysctl parameters */
73 int aflag = 0;
74 int dflag = 0;
75 int uflag = 0;
76
77 const char *otherconf_script;
78 const char *resolvconf_script = "/sbin/resolvconf";
79
80 /* protocol constants */
81 #define MAX_RTR_SOLICITATION_DELAY      1 /* second */
82 #define RTR_SOLICITATION_INTERVAL       4 /* seconds */
83 #define MAX_RTR_SOLICITATIONS           3 /* times */
84
85 /*
86  * implementation dependent constants in seconds
87  * XXX: should be configurable
88  */
89 #define PROBE_INTERVAL 60
90
91 /* static variables and functions */
92 static int mobile_node = 0;
93 static const char *pidfilename = RTSOL_PIDFILE;
94
95 #ifndef SMALL
96 static int do_dump;
97 static const char *dumpfilename = RTSOL_DUMPFILE;
98 #endif
99
100 static char **autoifprobe(void);
101 static int ifconfig(char *ifname);
102 static int make_packet(struct ifinfo *);
103 static struct timespec *rtsol_check_timer(void);
104
105 #ifndef SMALL
106 static void rtsold_set_dump_file(int);
107 #endif
108 static void usage(void);
109
110 int
111 main(int argc, char **argv)
112 {
113         int s, ch, once = 0;
114         struct timespec *timeout;
115         const char *opts;
116         struct pollfd set[2];
117         int rtsock;
118         char *argv0;
119
120 #ifndef SMALL
121         /* rtsold */
122         opts = "adDfFm1O:p:R:u";
123 #else
124         /* rtsol */
125         opts = "adDFO:R:u";
126         fflag = 1;
127         once = 1;
128 #endif
129         argv0 = argv[0];
130
131         while ((ch = getopt(argc, argv, opts)) != -1) {
132                 switch (ch) {
133                 case 'a':
134                         aflag = 1;
135                         break;
136                 case 'd':
137                         dflag += 1;
138                         break;
139                 case 'D':
140                         dflag += 2;
141                         break;
142                 case 'f':
143                         fflag = 1;
144                         break;
145                 case 'F':
146                         Fflag = 1;
147                         break;
148                 case 'm':
149                         mobile_node = 1;
150                         break;
151                 case '1':
152                         once = 1;
153                         break;
154                 case 'O':
155                         otherconf_script = optarg;
156                         break;
157                 case 'p':
158                         pidfilename = optarg;
159                         break;
160                 case 'R':
161                         resolvconf_script = optarg;
162                         break;
163                 case 'u':
164                         uflag = 1;
165                         break;
166                 default:
167                         usage();
168                         exit(1);
169                 }
170         }
171         argc -= optind;
172         argv += optind;
173
174         if ((!aflag && argc == 0) || (aflag && argc != 0)) {
175                 usage();
176                 exit(1);
177         }
178
179         /* Generate maximum time in timespec. */
180         tm_max.tv_sec = (-1) & ~((time_t)1 << ((sizeof(tm_max.tv_sec) * 8) - 1));
181         tm_max.tv_nsec = (-1) & ~((long)1 << ((sizeof(tm_max.tv_nsec) * 8) - 1));
182
183         /* set log level */
184         if (dflag > 1)
185                 log_upto = LOG_DEBUG;
186         else if (dflag > 0)
187                 log_upto = LOG_INFO;
188         else
189                 log_upto = LOG_NOTICE;
190
191         if (!fflag) {
192                 char *ident;
193
194                 ident = strrchr(argv0, '/');
195                 if (!ident)
196                         ident = argv0;
197                 else
198                         ident++;
199                 openlog(ident, LOG_NDELAY|LOG_PID, LOG_DAEMON);
200                 if (log_upto >= 0)
201                         setlogmask(LOG_UPTO(log_upto));
202         }
203
204         if (otherconf_script && *otherconf_script != '/') {
205                 errx(1, "configuration script (%s) must be an absolute path",
206                     otherconf_script);
207         }
208         if (resolvconf_script && *resolvconf_script != '/') {
209                 errx(1, "configuration script (%s) must be an absolute path",
210                     resolvconf_script);
211         }
212         if (pidfilename && *pidfilename != '/') {
213                 errx(1, "pid filename (%s) must be an absolute path",
214                     pidfilename);
215         }
216
217 #ifndef SMALL
218         /* initialization to dump internal status to a file */
219         signal(SIGUSR1, rtsold_set_dump_file);
220 #endif
221
222         if (!fflag)
223                 daemon(0, 0);           /* act as a daemon */
224
225         /*
226          * Open a socket for sending RS and receiving RA.
227          * This should be done before calling ifinit(), since the function
228          * uses the socket.
229          */
230         if ((s = sockopen()) < 0) {
231                 warnmsg(LOG_ERR, __func__, "failed to open a socket");
232                 exit(1);
233         }
234         set[0].fd = s;
235         set[0].events = POLLIN;
236         set[1].fd = -1;
237
238         if ((rtsock = rtsock_open()) < 0) {
239                 warnmsg(LOG_ERR, __func__, "failed to open a socket");
240                 exit(1);
241         }
242         set[1].fd = rtsock;
243         set[1].events = POLLIN;
244
245         /* configuration per interface */
246         if (ifinit()) {
247                 warnmsg(LOG_ERR, __func__,
248                     "failed to initialize interfaces");
249                 exit(1);
250         }
251         if (aflag)
252                 argv = autoifprobe();
253         while (argv && *argv) {
254                 if (ifconfig(*argv)) {
255                         warnmsg(LOG_ERR, __func__,
256                             "failed to initialize %s", *argv);
257                         exit(1);
258                 }
259                 argv++;
260         }
261
262         /* setup for probing default routers */
263         if (probe_init()) {
264                 warnmsg(LOG_ERR, __func__,
265                     "failed to setup for probing routers");
266                 exit(1);
267                 /*NOTREACHED*/
268         }
269
270         /* dump the current pid */
271         if (!once) {
272                 pid_t pid = getpid();
273                 FILE *fp;
274
275                 if ((fp = fopen(pidfilename, "w")) == NULL)
276                         warnmsg(LOG_ERR, __func__,
277                             "failed to open a pid log file(%s): %s",
278                             pidfilename, strerror(errno));
279                 else {
280                         fprintf(fp, "%d\n", pid);
281                         fclose(fp);
282                 }
283         }
284         while (1) {             /* main loop */
285                 int e;
286 #ifndef SMALL
287                 if (do_dump) {  /* SIGUSR1 */
288                         do_dump = 0;
289                         rtsold_dump_file(dumpfilename);
290                 }
291 #endif
292
293                 timeout = rtsol_check_timer();
294
295                 if (once) {
296                         struct ifinfo *ifi;
297
298                         /* if we have no timeout, we are done (or failed) */
299                         if (timeout == NULL)
300                                 break;
301
302                         /* if all interfaces have got RA packet, we are done */
303                         TAILQ_FOREACH(ifi, &ifinfo_head, ifi_next) {
304                                 if (ifi->state != IFS_DOWN && ifi->racnt == 0)
305                                         break;
306                         }
307                         if (ifi == NULL)
308                                 break;
309                 }
310                 e = poll(set, 2, timeout ? (timeout->tv_sec * 1000 + timeout->tv_nsec / 1000 / 1000) : INFTIM);
311                 if (e < 1) {
312                         if (e < 0 && errno != EINTR) {
313                                 warnmsg(LOG_ERR, __func__, "select: %s",
314                                     strerror(errno));
315                         }
316                         continue;
317                 }
318
319                 /* packet reception */
320                 if (set[1].revents & POLLIN)
321                         rtsock_input(rtsock);
322                 if (set[0].revents & POLLIN)
323                         rtsol_input(s);
324         }
325         /* NOTREACHED */
326
327         return (0);
328 }
329
330 static int
331 ifconfig(char *ifname)
332 {
333         struct ifinfo *ifi;
334         struct sockaddr_dl *sdl;
335         int flags;
336
337         if ((sdl = if_nametosdl(ifname)) == NULL) {
338                 warnmsg(LOG_ERR, __func__,
339                     "failed to get link layer information for %s", ifname);
340                 return (-1);
341         }
342         if (find_ifinfo(sdl->sdl_index)) {
343                 warnmsg(LOG_ERR, __func__,
344                     "interface %s was already configured", ifname);
345                 free(sdl);
346                 return (-1);
347         }
348
349         if (Fflag) {
350                 struct in6_ndireq nd;
351                 int s;
352
353                 if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
354                         warnmsg(LOG_ERR, __func__, "socket() failed.");
355                         return (-1);
356                 }
357                 memset(&nd, 0, sizeof(nd));
358                 strlcpy(nd.ifname, ifname, sizeof(nd.ifname));
359                 if (ioctl(s, SIOCGIFINFO_IN6, (caddr_t)&nd) < 0) {
360                         warnmsg(LOG_ERR, __func__,
361                             "cannot get accept_rtadv flag");
362                         close(s);
363                         return (-1);
364                 }
365                 nd.ndi.flags |= ND6_IFF_ACCEPT_RTADV;
366                 if (ioctl(s, SIOCSIFINFO_IN6, (caddr_t)&nd) < 0) {
367                         warnmsg(LOG_ERR, __func__,
368                             "cannot set accept_rtadv flag");
369                         close(s);
370                         return (-1);
371                 }
372                 close(s);
373         }
374
375         if ((ifi = malloc(sizeof(*ifi))) == NULL) {
376                 warnmsg(LOG_ERR, __func__, "memory allocation failed");
377                 free(sdl);
378                 return (-1);
379         }
380         memset(ifi, 0, sizeof(*ifi));
381         ifi->sdl = sdl;
382         ifi->ifi_rdnss = IFI_DNSOPT_STATE_NOINFO;
383         ifi->ifi_dnssl = IFI_DNSOPT_STATE_NOINFO;
384         TAILQ_INIT(&ifi->ifi_rainfo);
385         strlcpy(ifi->ifname, ifname, sizeof(ifi->ifname));
386
387         /* construct a router solicitation message */
388         if (make_packet(ifi))
389                 goto bad;
390
391         /* set link ID of this interface. */
392 #ifdef HAVE_SCOPELIB
393         if (inet_zoneid(AF_INET6, 2, ifname, &ifi->linkid))
394                 goto bad;
395 #else
396         /* XXX: assume interface IDs as link IDs */
397         ifi->linkid = ifi->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         ifi->mediareqok = 1;
405         ifi->active = interface_status(ifi);
406         if (!ifi->mediareqok) {
407                 /*
408                  * probe routers periodically even if the link status
409                  * does not change.
410                  */
411                 ifi->probeinterval = PROBE_INTERVAL;
412         }
413
414         /* activate interface: interface_up returns 0 on success */
415         flags = interface_up(ifi->ifname);
416         if (flags == 0)
417                 ifi->state = IFS_DELAY;
418         else if (flags == IFS_TENTATIVE)
419                 ifi->state = IFS_TENTATIVE;
420         else
421                 ifi->state = IFS_DOWN;
422
423         rtsol_timer_update(ifi);
424
425         TAILQ_INSERT_TAIL(&ifinfo_head, ifi, ifi_next);
426         return (0);
427
428 bad:
429         free(ifi->sdl);
430         free(ifi);
431         return (-1);
432 }
433
434 struct rainfo *
435 find_rainfo(struct ifinfo *ifi, struct sockaddr_in6 *sin6)
436 {
437         struct rainfo *rai;
438
439         TAILQ_FOREACH(rai, &ifi->ifi_rainfo, rai_next)
440                 if (memcmp(&rai->rai_saddr.sin6_addr, &sin6->sin6_addr,
441                     sizeof(rai->rai_saddr.sin6_addr)) == 0)
442                         return (rai);
443
444         return (NULL);
445 }
446
447 struct ifinfo *
448 find_ifinfo(int ifindex)
449 {
450         struct ifinfo *ifi;
451
452         TAILQ_FOREACH(ifi, &ifinfo_head, ifi_next) {
453                 if (ifi->sdl->sdl_index == ifindex)
454                         return (ifi);
455         }
456         return (NULL);
457 }
458
459 static int
460 make_packet(struct ifinfo *ifi)
461 {
462         size_t packlen = sizeof(struct nd_router_solicit), lladdroptlen = 0;
463         struct nd_router_solicit *rs;
464         char *buf;
465
466         if ((lladdroptlen = lladdropt_length(ifi->sdl)) == 0) {
467                 warnmsg(LOG_INFO, __func__,
468                     "link-layer address option has null length"
469                     " on %s. Treat as not included.", ifi->ifname);
470         }
471         packlen += lladdroptlen;
472         ifi->rs_datalen = packlen;
473
474         /* allocate buffer */
475         if ((buf = malloc(packlen)) == NULL) {
476                 warnmsg(LOG_ERR, __func__,
477                     "memory allocation failed for %s", ifi->ifname);
478                 return (-1);
479         }
480         ifi->rs_data = buf;
481
482         /* fill in the message */
483         rs = (struct nd_router_solicit *)buf;
484         rs->nd_rs_type = ND_ROUTER_SOLICIT;
485         rs->nd_rs_code = 0;
486         rs->nd_rs_cksum = 0;
487         rs->nd_rs_reserved = 0;
488         buf += sizeof(*rs);
489
490         /* fill in source link-layer address option */
491         if (lladdroptlen)
492                 lladdropt_fill(ifi->sdl, (struct nd_opt_hdr *)buf);
493
494         return (0);
495 }
496
497 static struct timespec *
498 rtsol_check_timer(void)
499 {
500         static struct timespec returnval;
501         struct timespec now, rtsol_timer;
502         struct ifinfo *ifi;
503         struct rainfo *rai;
504         struct ra_opt *rao, *raotmp;
505         int flags;
506
507         clock_gettime(CLOCK_MONOTONIC_FAST, &now);
508
509         rtsol_timer = tm_max;
510
511         TAILQ_FOREACH(ifi, &ifinfo_head, ifi_next) {
512                 if (TS_CMP(&ifi->expire, &now, <=)) {
513                         warnmsg(LOG_DEBUG, __func__, "timer expiration on %s, "
514                             "state = %d", ifi->ifname, ifi->state);
515
516                         while((rai = TAILQ_FIRST(&ifi->ifi_rainfo)) != NULL) {
517                                 /* Remove all RA options. */
518                                 TAILQ_REMOVE(&ifi->ifi_rainfo, rai, rai_next);
519                                 while ((rao = TAILQ_FIRST(&rai->rai_ra_opt)) !=
520                                     NULL) {
521                                         TAILQ_REMOVE(&rai->rai_ra_opt, rao,
522                                             rao_next);
523                                         if (rao->rao_msg != NULL)
524                                                 free(rao->rao_msg);
525                                         free(rao);
526                                 }
527                                 free(rai);
528                         }
529                         switch (ifi->state) {
530                         case IFS_DOWN:
531                         case IFS_TENTATIVE:
532                                 /* interface_up returns 0 on success */
533                                 flags = interface_up(ifi->ifname);
534                                 if (flags == 0)
535                                         ifi->state = IFS_DELAY;
536                                 else if (flags == IFS_TENTATIVE)
537                                         ifi->state = IFS_TENTATIVE;
538                                 else
539                                         ifi->state = IFS_DOWN;
540                                 break;
541                         case IFS_IDLE:
542                         {
543                                 int oldstatus = ifi->active;
544                                 int probe = 0;
545
546                                 ifi->active = interface_status(ifi);
547
548                                 if (oldstatus != ifi->active) {
549                                         warnmsg(LOG_DEBUG, __func__,
550                                             "%s status is changed"
551                                             " from %d to %d",
552                                             ifi->ifname,
553                                             oldstatus, ifi->active);
554                                         probe = 1;
555                                         ifi->state = IFS_DELAY;
556                                 } else if (ifi->probeinterval &&
557                                     (ifi->probetimer -=
558                                     ifi->timer.tv_sec) <= 0) {
559                                         /* probe timer expired */
560                                         ifi->probetimer =
561                                             ifi->probeinterval;
562                                         probe = 1;
563                                         ifi->state = IFS_PROBE;
564                                 }
565
566                                 /*
567                                  * If we need a probe, clear the previous
568                                  * status wrt the "other" configuration.
569                                  */
570                                 if (probe)
571                                         ifi->otherconfig = 0;
572
573                                 if (probe && mobile_node)
574                                         defrouter_probe(ifi);
575                                 break;
576                         }
577                         case IFS_DELAY:
578                                 ifi->state = IFS_PROBE;
579                                 sendpacket(ifi);
580                                 break;
581                         case IFS_PROBE:
582                                 if (ifi->probes < MAX_RTR_SOLICITATIONS)
583                                         sendpacket(ifi);
584                                 else {
585                                         warnmsg(LOG_INFO, __func__,
586                                             "No answer after sending %d RSs",
587                                             ifi->probes);
588                                         ifi->probes = 0;
589                                         ifi->state = IFS_IDLE;
590                                 }
591                                 break;
592                         }
593                         rtsol_timer_update(ifi);
594                 } else {
595                         /* Expiration check for RA options. */
596                         int expire = 0;
597
598                         TAILQ_FOREACH(rai, &ifi->ifi_rainfo, rai_next) {
599                                 TAILQ_FOREACH_SAFE(rao, &rai->rai_ra_opt,
600                                     rao_next, raotmp) {
601                                         warnmsg(LOG_DEBUG, __func__,
602                                             "RA expiration timer: "
603                                             "type=%d, msg=%s, expire=%s",
604                                             rao->rao_type, (char *)rao->rao_msg,
605                                                 sec2str(&rao->rao_expire));
606                                         if (TS_CMP(&now, &rao->rao_expire,
607                                             >=)) {
608                                                 warnmsg(LOG_DEBUG, __func__,
609                                                     "RA expiration timer: "
610                                                     "expired.");
611                                                 TAILQ_REMOVE(&rai->rai_ra_opt,
612                                                     rao, rao_next);
613                                                 if (rao->rao_msg != NULL)
614                                                         free(rao->rao_msg);
615                                                 free(rao);
616                                                 expire = 1;
617                                         }
618                                 }
619                         }
620                         if (expire)
621                                 ra_opt_handler(ifi);
622                 }
623                 if (TS_CMP(&ifi->expire, &rtsol_timer, <))
624                         rtsol_timer = ifi->expire;
625         }
626
627         if (TS_CMP(&rtsol_timer, &tm_max, ==)) {
628                 warnmsg(LOG_DEBUG, __func__, "there is no timer");
629                 return (NULL);
630         } else if (TS_CMP(&rtsol_timer, &now, <))
631                 /* this may occur when the interval is too small */
632                 returnval.tv_sec = returnval.tv_nsec = 0;
633         else
634                 TS_SUB(&rtsol_timer, &now, &returnval);
635
636         now.tv_sec += returnval.tv_sec;
637         now.tv_nsec += returnval.tv_nsec;
638         warnmsg(LOG_DEBUG, __func__, "New timer is %s",
639             sec2str(&now));
640
641         return (&returnval);
642 }
643
644 void
645 rtsol_timer_update(struct ifinfo *ifi)
646 {
647 #define MILLION 1000000
648 #define DADRETRY 10             /* XXX: adhoc */
649         long interval;
650         struct timespec now;
651
652         bzero(&ifi->timer, sizeof(ifi->timer));
653
654         switch (ifi->state) {
655         case IFS_DOWN:
656         case IFS_TENTATIVE:
657                 if (++ifi->dadcount > DADRETRY) {
658                         ifi->dadcount = 0;
659                         ifi->timer.tv_sec = PROBE_INTERVAL;
660                 } else
661                         ifi->timer.tv_sec = 1;
662                 break;
663         case IFS_IDLE:
664                 if (mobile_node) {
665                         /* XXX should be configurable */
666                         ifi->timer.tv_sec = 3;
667                 }
668                 else
669                         ifi->timer = tm_max;    /* stop timer(valid?) */
670                 break;
671         case IFS_DELAY:
672                 interval = arc4random_uniform(MAX_RTR_SOLICITATION_DELAY * MILLION);
673                 ifi->timer.tv_sec = interval / MILLION;
674                 ifi->timer.tv_nsec = (interval % MILLION) * 1000;
675                 break;
676         case IFS_PROBE:
677                 if (ifi->probes < MAX_RTR_SOLICITATIONS)
678                         ifi->timer.tv_sec = RTR_SOLICITATION_INTERVAL;
679                 else {
680                         /*
681                          * After sending MAX_RTR_SOLICITATIONS solicitations,
682                          * we're just waiting for possible replies; there
683                          * will be no more solicitation.  Thus, we change
684                          * the timer value to MAX_RTR_SOLICITATION_DELAY based
685                          * on RFC 2461, Section 6.3.7.
686                          */
687                         ifi->timer.tv_sec = MAX_RTR_SOLICITATION_DELAY;
688                 }
689                 break;
690         default:
691                 warnmsg(LOG_ERR, __func__,
692                     "illegal interface state(%d) on %s",
693                     ifi->state, ifi->ifname);
694                 return;
695         }
696
697         /* reset the timer */
698         if (TS_CMP(&ifi->timer, &tm_max, ==)) {
699                 ifi->expire = tm_max;
700                 warnmsg(LOG_DEBUG, __func__,
701                     "stop timer for %s", ifi->ifname);
702         } else {
703                 clock_gettime(CLOCK_MONOTONIC_FAST, &now);
704                 TS_ADD(&now, &ifi->timer, &ifi->expire);
705
706                 now.tv_sec += ifi->timer.tv_sec;
707                 now.tv_nsec += ifi->timer.tv_nsec;
708                 warnmsg(LOG_DEBUG, __func__, "set timer for %s to %s",
709                     ifi->ifname, sec2str(&now));
710         }
711
712 #undef MILLION
713 }
714
715 #ifndef SMALL
716 static void
717 rtsold_set_dump_file(int sig __unused)
718 {
719         do_dump = 1;
720 }
721 #endif
722
723 static void
724 usage(void)
725 {
726 #ifndef SMALL
727         fprintf(stderr, "usage: rtsold [-dDfFm1] [-O script-name] "
728             "[-p pidfile] [-R script-name] interface ...\n");
729         fprintf(stderr, "usage: rtsold [-dDfFm1] [-O script-name] "
730             "[-p pidfile] [-R script-name] -a\n");
731 #else
732         fprintf(stderr, "usage: rtsol [-dDF] [-O script-name] "
733             "[-p pidfile] [-R script-name] interface ...\n");
734         fprintf(stderr, "usage: rtsol [-dDF] [-O script-name] "
735             "[-p pidfile] [-R script-name] -a\n");
736 #endif
737 }
738
739 void
740 warnmsg(int priority, const char *func, const char *msg, ...)
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
759 /*
760  * return a list of interfaces which is suitable to sending an RS.
761  */
762 static char **
763 autoifprobe(void)
764 {
765         static char **argv = NULL;
766         static int n = 0;
767         char **a;
768         int s = 0, i, found;
769         struct ifaddrs *ifap, *ifa;
770         struct in6_ndireq nd;
771
772         /* initialize */
773         while (n--)
774                 free(argv[n]);
775         if (argv) {
776                 free(argv);
777                 argv = NULL;
778         }
779         n = 0;
780
781         if (getifaddrs(&ifap) != 0)
782                 return (NULL);
783
784         if (!Fflag && (s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
785                 warnmsg(LOG_ERR, __func__, "socket");
786                 exit(1);
787         }
788
789         /* find an ethernet */
790         for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
791                 if ((ifa->ifa_flags & IFF_UP) == 0)
792                         continue;
793                 if ((ifa->ifa_flags & IFF_POINTOPOINT) != 0)
794                         continue;
795                 if ((ifa->ifa_flags & IFF_LOOPBACK) != 0)
796                         continue;
797                 if ((ifa->ifa_flags & IFF_MULTICAST) == 0)
798                         continue;
799
800                 if (ifa->ifa_addr->sa_family != AF_INET6)
801                         continue;
802
803                 found = 0;
804                 for (i = 0; i < n; i++) {
805                         if (strcmp(argv[i], ifa->ifa_name) == 0) {
806                                 found++;
807                                 break;
808                         }
809                 }
810                 if (found)
811                         continue;
812
813                 /*
814                  * Skip the interfaces which IPv6 and/or accepting RA
815                  * is disabled.
816                  */
817                 if (!Fflag) {
818                         memset(&nd, 0, sizeof(nd));
819                         strlcpy(nd.ifname, ifa->ifa_name, sizeof(nd.ifname));
820                         if (ioctl(s, SIOCGIFINFO_IN6, (caddr_t)&nd) < 0) {
821                                 warnmsg(LOG_ERR, __func__,
822                                         "ioctl(SIOCGIFINFO_IN6)");
823                                 exit(1);
824                         }
825                         if ((nd.ndi.flags & ND6_IFF_IFDISABLED))
826                                 continue;
827                         if (!(nd.ndi.flags & ND6_IFF_ACCEPT_RTADV))
828                                 continue;
829                 }
830
831                 /* if we find multiple candidates, just warn. */
832                 if (n != 0 && dflag > 1)
833                         warnmsg(LOG_WARNING, __func__,
834                                 "multiple interfaces found");
835
836                 a = realloc(argv, (n + 1) * sizeof(char *));
837                 if (a == NULL) {
838                         warnmsg(LOG_ERR, __func__, "realloc");
839                         exit(1);
840                 }
841                 argv = a;
842                 argv[n] = strdup(ifa->ifa_name);
843                 if (!argv[n]) {
844                         warnmsg(LOG_ERR, __func__, "malloc");
845                         exit(1);
846                 }
847                 n++;
848         }
849
850         if (n) {
851                 a = realloc(argv, (n + 1) * sizeof(char *));
852                 if (a == NULL) {
853                         warnmsg(LOG_ERR, __func__, "realloc");
854                         exit(1);
855                 }
856                 argv = a;
857                 argv[n] = NULL;
858
859                 if (dflag > 0) {
860                         for (i = 0; i < n; i++)
861                                 warnmsg(LOG_WARNING, __func__, "probing %s",
862                                         argv[i]);
863                 }
864         }
865         if (!Fflag)
866                 close(s);
867         freeifaddrs(ifap);
868         return (argv);
869 }