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