]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - usr.sbin/rtsold/rtsold.c
- Add initial support for expiration timer for RNDSS and DNSSL options.
[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  * 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 #define RTSOL_DUMPFILE  "/var/run/rtsold.dump";
67 #define RTSOL_PIDFILE   "/var/run/rtsold.pid";
68
69 struct ifinfo *iflist;
70 struct timeval tm_max = {0x7fffffff, 0x7fffffff};
71 static int log_upto = 999;
72 static int fflag = 0;
73
74 int Fflag = 0;  /* force setting sysctl parameters */
75 int aflag = 0;
76 int dflag = 0;
77
78 const char *otherconf_script;
79 const 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 */
93 static int mobile_node = 0;
94 static const char *pidfilename = RTSOL_PIDFILE;
95
96 #ifndef SMALL
97 static int do_dump;
98 static const char *dumpfilename = RTSOL_DUMPFILE;
99 #endif
100
101 #if 0
102 static int ifreconfig(char *);
103 #endif
104
105 static int make_packet(struct ifinfo *);
106 static struct timeval *rtsol_check_timer(void);
107
108 #ifndef SMALL
109 static void rtsold_set_dump_file(int);
110 #endif
111 static void usage(void);
112
113 int
114 main(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
128 #ifndef SMALL
129         /* rtsold */
130         opts = "adDfFm1O:P:R:";
131 #else
132         /* rtsol */
133         opts = "adDFO:P:R:";
134         fflag = 1;
135         once = 1;
136 #endif
137         while ((ch = getopt(argc, argv, opts)) != -1) {
138                 switch (ch) {
139                 case 'a':
140                         aflag = 1;
141                         break;
142                 case 'd':
143                         dflag = 1;
144                         break;
145                 case 'D':
146                         dflag = 2;
147                         break;
148                 case 'f':
149                         fflag = 1;
150                         break;
151                 case 'F':
152                         Fflag = 1;
153                         break;
154                 case 'm':
155                         mobile_node = 1;
156                         break;
157                 case '1':
158                         once = 1;
159                         break;
160                 case 'O':
161                         otherconf_script = optarg;
162                         break;
163                 case 'P':
164                         pidfilename = optarg;
165                         break;
166                 case 'R':
167                         resolvconf_script = optarg;
168                         break;
169                 default:
170                         usage();
171                         exit(1);
172                 }
173         }
174         argc -= optind;
175         argv += optind;
176
177         if ((!aflag && argc == 0) || (aflag && argc != 0)) {
178                 usage();
179                 exit(1);
180         }
181
182         /* set log level */
183         if (dflag == 0)
184                 log_upto = LOG_NOTICE;
185         if (!fflag) {
186                 char *ident;
187
188                 ident = strrchr(argv[0], '/');
189                 if (!ident)
190                         ident = argv[0];
191                 else
192                         ident++;
193                 openlog(ident, LOG_NDELAY|LOG_PID, LOG_DAEMON);
194                 if (log_upto >= 0)
195                         setlogmask(LOG_UPTO(log_upto));
196         }
197
198         if (otherconf_script && *otherconf_script != '/') {
199                 errx(1, "configuration script (%s) must be an absolute path",
200                     otherconf_script);
201         }
202         if (resolvconf_script && *resolvconf_script != '/') {
203                 errx(1, "configuration script (%s) must be an absolute path",
204                     resolvconf_script);
205         }
206         if (pidfilename && *pidfilename != '/') {
207                 errx(1, "pid filename (%s) must be an absolute path",
208                     pidfilename);
209         }
210 #ifndef HAVE_ARC4RANDOM
211         /* random value initialization */
212         srandom((u_long)time(NULL));
213 #endif
214
215         if (Fflag) {
216                 setinet6sysctl(IPV6CTL_FORWARDING, 0);
217         } else {
218                 /* warn if forwarding is up */
219                 if (getinet6sysctl(IPV6CTL_FORWARDING))
220                         warnx("kernel is configured as a router, not a host");
221         }
222
223 #ifndef SMALL
224         /* initialization to dump internal status to a file */
225         signal(SIGUSR1, rtsold_set_dump_file);
226 #endif
227
228         if (!fflag)
229                 daemon(0, 0);           /* act as a daemon */
230
231         /*
232          * Open a socket for sending RS and receiving RA.
233          * This should be done before calling ifinit(), since the function
234          * uses the socket.
235          */
236         if ((s = sockopen()) < 0) {
237                 warnmsg(LOG_ERR, __func__, "failed to open a socket");
238                 exit(1);
239         }
240 #ifdef HAVE_POLL_H
241         set[0].fd = s;
242         set[0].events = POLLIN;
243 #else
244         maxfd = s;
245 #endif
246
247 #ifdef HAVE_POLL_H
248         set[1].fd = -1;
249 #endif
250
251         if ((rtsock = rtsock_open()) < 0) {
252                 warnmsg(LOG_ERR, __func__, "failed to open a socket");
253                 exit(1);
254         }
255 #ifdef HAVE_POLL_H
256         set[1].fd = rtsock;
257         set[1].events = POLLIN;
258 #else
259         if (rtsock > maxfd)
260                 maxfd = rtsock;
261 #endif
262
263 #ifndef HAVE_POLL_H
264         fdmasks = howmany(maxfd + 1, NFDBITS) * sizeof(fd_mask);
265         if ((fdsetp = malloc(fdmasks)) == NULL) {
266                 warnmsg(LOG_ERR, __func__, "malloc");
267                 exit(1);
268         }
269         if ((selectfdp = malloc(fdmasks)) == NULL) {
270                 warnmsg(LOG_ERR, __func__, "malloc");
271                 exit(1);
272         }
273 #endif
274
275         /* configuration per interface */
276         if (ifinit()) {
277                 warnmsg(LOG_ERR, __func__,
278                     "failed to initialize interfaces");
279                 exit(1);
280         }
281         if (aflag)
282                 argv = autoifprobe();
283         while (argv && *argv) {
284                 if (ifconfig(*argv)) {
285                         warnmsg(LOG_ERR, __func__,
286                             "failed to initialize %s", *argv);
287                         exit(1);
288                 }
289                 argv++;
290         }
291
292         /* setup for probing default routers */
293         if (probe_init()) {
294                 warnmsg(LOG_ERR, __func__,
295                     "failed to setup for probing routers");
296                 exit(1);
297                 /*NOTREACHED*/
298         }
299
300         /* dump the current pid */
301         if (!once) {
302                 pid_t pid = getpid();
303                 FILE *fp;
304
305                 if ((fp = fopen(pidfilename, "w")) == NULL)
306                         warnmsg(LOG_ERR, __func__,
307                             "failed to open a pid log file(%s): %s",
308                             pidfilename, strerror(errno));
309                 else {
310                         fprintf(fp, "%d\n", pid);
311                         fclose(fp);
312                 }
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                         TAILQ_FOREACH(ifi, &ifinfo_head, 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 *ifi;
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 ((ifi = malloc(sizeof(*ifi))) == NULL) {
402                 warnmsg(LOG_ERR, __func__, "memory allocation failed");
403                 free(sdl);
404                 return (-1);
405         }
406         memset(ifi, 0, sizeof(*ifi));
407         ifi->sdl = sdl;
408
409         strlcpy(ifi->ifname, ifname, sizeof(ifi->ifname));
410
411         /* construct a router solicitation message */
412         if (make_packet(ifi))
413                 goto bad;
414
415         /* set link ID of this interface. */
416 #ifdef HAVE_SCOPELIB
417         if (inet_zoneid(AF_INET6, 2, ifname, &ifi->linkid))
418                 goto bad;
419 #else
420         /* XXX: assume interface IDs as link IDs */
421         ifi->linkid = ifi->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         ifi->mediareqok = 1;
429         ifi->active = interface_status(ifi);
430         if (!ifi->mediareqok) {
431                 /*
432                  * probe routers periodically even if the link status
433                  * does not change.
434                  */
435                 ifi->probeinterval = PROBE_INTERVAL;
436         }
437
438         /* activate interface: interface_up returns 0 on success */
439         flags = interface_up(ifi->ifname);
440         if (flags == 0)
441                 ifi->state = IFS_DELAY;
442         else if (flags == IFS_TENTATIVE)
443                 ifi->state = IFS_TENTATIVE;
444         else
445                 ifi->state = IFS_DOWN;
446
447         rtsol_timer_update(ifi);
448
449         TAILQ_INSERT_TAIL(&ifinfo_head, ifi, ifi_next);
450         return (0);
451
452 bad:
453         free(ifi->sdl);
454         free(ifi);
455         return (-1);
456 }
457
458 void
459 iflist_init(void)
460 {
461         struct ifinfo *ifi;
462         struct ifinfo *ifi_tmp;
463
464         ifi = TAILQ_FIRST(&ifinfo_head);
465         while (ifi != NULL) {
466                 if (ifi->sdl != NULL)
467                         free(ifi->sdl);
468                 if (ifi->rs_data != NULL)
469                         free(ifi->rs_data);
470                 ifi_tmp = TAILQ_NEXT(ifi, ifi_next);
471                 free(ifi);
472                 ifi = ifi_tmp;
473         }
474 }
475
476 #if 0
477 static int
478 ifreconfig(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
503 struct ifinfo *
504 find_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
515 static int
516 make_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
553 static struct timeval *
554 rtsol_check_timer(void)
555 {
556         static struct timeval returnval;
557         struct timeval now, rtsol_timer;
558         struct ifinfo *ifi;
559         int flags;
560
561         gettimeofday(&now, NULL);
562
563         rtsol_timer = tm_max;
564
565         TAILQ_FOREACH(ifi, &ifinfo_head, ifi_next) {
566                 if (timercmp(&ifi->expire, &now, <=)) {
567                         if (dflag > 1)
568                                 warnmsg(LOG_DEBUG, __func__,
569                                     "timer expiration on %s, "
570                                     "state = %d", ifi->ifname,
571                                     ifi->state);
572
573                         switch (ifi->state) {
574                         case IFS_DOWN:
575                         case IFS_TENTATIVE:
576                                 /* interface_up returns 0 on success */
577                                 flags = interface_up(ifi->ifname);
578                                 if (flags == 0)
579                                         ifi->state = IFS_DELAY;
580                                 else if (flags == IFS_TENTATIVE)
581                                         ifi->state = IFS_TENTATIVE;
582                                 else
583                                         ifi->state = IFS_DOWN;
584                                 break;
585                         case IFS_IDLE:
586                         {
587                                 int oldstatus = ifi->active;
588                                 int probe = 0;
589
590                                 ifi->active = interface_status(ifi);
591
592                                 if (oldstatus != ifi->active) {
593                                         warnmsg(LOG_DEBUG, __func__,
594                                             "%s status is changed"
595                                             " from %d to %d",
596                                             ifi->ifname,
597                                             oldstatus, ifi->active);
598                                         probe = 1;
599                                         ifi->state = IFS_DELAY;
600                                 } else if (ifi->probeinterval &&
601                                     (ifi->probetimer -=
602                                     ifi->timer.tv_sec) <= 0) {
603                                         /* probe timer expired */
604                                         ifi->probetimer =
605                                             ifi->probeinterval;
606                                         probe = 1;
607                                         ifi->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                                         ifi->otherconfig = 0;
616
617                                 if (probe && mobile_node)
618                                         defrouter_probe(ifi);
619                                 break;
620                         }
621                         case IFS_DELAY:
622                                 ifi->state = IFS_PROBE;
623                                 sendpacket(ifi);
624                                 break;
625                         case IFS_PROBE:
626                                 if (ifi->probes < MAX_RTR_SOLICITATIONS)
627                                         sendpacket(ifi);
628                                 else {
629                                         warnmsg(LOG_INFO, __func__,
630                                             "No answer after sending %d RSs",
631                                             ifi->probes);
632                                         ifi->probes = 0;
633                                         ifi->state = IFS_IDLE;
634                                 }
635                                 break;
636                         }
637                         rtsol_timer_update(ifi);
638                 }
639
640                 if (timercmp(&ifi->expire, &rtsol_timer, <))
641                         rtsol_timer = ifi->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 *ifi)
662 {
663 #define MILLION 1000000
664 #define DADRETRY 10             /* XXX: adhoc */
665         long interval;
666         struct timeval now;
667
668         bzero(&ifi->timer, sizeof(ifi->timer));
669
670         switch (ifi->state) {
671         case IFS_DOWN:
672         case IFS_TENTATIVE:
673                 if (++ifi->dadcount > DADRETRY) {
674                         ifi->dadcount = 0;
675                         ifi->timer.tv_sec = PROBE_INTERVAL;
676                 } else
677                         ifi->timer.tv_sec = 1;
678                 break;
679         case IFS_IDLE:
680                 if (mobile_node) {
681                         /* XXX should be configurable */
682                         ifi->timer.tv_sec = 3;
683                 }
684                 else
685                         ifi->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                 ifi->timer.tv_sec = interval / MILLION;
694                 ifi->timer.tv_usec = interval % MILLION;
695                 break;
696         case IFS_PROBE:
697                 if (ifi->probes < MAX_RTR_SOLICITATIONS)
698                         ifi->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                         ifi->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                     ifi->state, ifi->ifname);
714                 return;
715         }
716
717         /* reset the timer */
718         if (timercmp(&ifi->timer, &tm_max, ==)) {
719                 ifi->expire = tm_max;
720                 warnmsg(LOG_DEBUG, __func__,
721                     "stop timer for %s", ifi->ifname);
722         } else {
723                 gettimeofday(&now, NULL);
724                 timeradd(&now, &ifi->timer, &ifi->expire);
725
726                 if (dflag > 1)
727                         warnmsg(LOG_DEBUG, __func__,
728                             "set timer for %s to %d:%d", ifi->ifname,
729                             (int)ifi->timer.tv_sec,
730                             (int)ifi->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 __unused)
742 {
743         do_dump = 1;
744 }
745 #endif
746
747 static void
748 usage(void)
749 {
750 #ifndef SMALL
751         fprintf(stderr, "usage: rtsold [-adDfFm1] [-O script-name] [-P pidfile] [-R script-name] interfaces...\n");
752         fprintf(stderr, "usage: rtsold [-dDfFm1] [-O script-name] [-P pidfile] [-R script-name] -a\n");
753 #else
754         fprintf(stderr, "usage: rtsol [-dDF] [-O script-name] [-P pidfile] [-R script-name] interfaces...\n");
755         fprintf(stderr, "usage: rtsol [-dDF] [-O script-name] [-P pidfile] [-R script-name] -a\n");
756 #endif
757 }
758
759 void
760 warnmsg(int priority, const char *func, const char *msg, ...)
761 {
762         va_list ap;
763         char buf[BUFSIZ];
764
765         va_start(ap, msg);
766         if (fflag) {
767                 if (priority <= log_upto) {
768                         (void)vfprintf(stderr, msg, ap);
769                         (void)fprintf(stderr, "\n");
770                 }
771         } else {
772                 snprintf(buf, sizeof(buf), "<%s> %s", func, msg);
773                 msg = buf;
774                 vsyslog(priority, msg, ap);
775         }
776         va_end(ap);
777 }
778
779 /*
780  * return a list of interfaces which is suitable to sending an RS.
781  */
782 char **
783 autoifprobe(void)
784 {
785         static char **argv = NULL;
786         static int n = 0;
787         char **a;
788         int s = 0, i, found;
789         struct ifaddrs *ifap, *ifa, *target;
790         struct in6_ndireq nd;
791
792         /* initialize */
793         while (n--)
794                 free(argv[n]);
795         if (argv) {
796                 free(argv);
797                 argv = NULL;
798         }
799         n = 0;
800
801         if (getifaddrs(&ifap) != 0)
802                 return (NULL);
803
804         if (!Fflag && (s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
805                 warnmsg(LOG_ERR, __func__, "socket");
806                 exit(1);
807         }
808
809         target = NULL;
810         /* find an ethernet */
811         for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
812                 if ((ifa->ifa_flags & IFF_UP) == 0)
813                         continue;
814                 if ((ifa->ifa_flags & IFF_POINTOPOINT) != 0)
815                         continue;
816                 if ((ifa->ifa_flags & IFF_LOOPBACK) != 0)
817                         continue;
818                 if ((ifa->ifa_flags & IFF_MULTICAST) == 0)
819                         continue;
820
821                 if (ifa->ifa_addr->sa_family != AF_INET6)
822                         continue;
823
824                 found = 0;
825                 for (i = 0; i < n; i++) {
826                         if (strcmp(argv[i], ifa->ifa_name) == 0) {
827                                 found++;
828                                 break;
829                         }
830                 }
831                 if (found)
832                         continue;
833
834                 /*
835                  * Skip the interfaces which IPv6 and/or accepting RA
836                  * is disabled.
837                  */
838                 if (!Fflag) {
839                         memset(&nd, 0, sizeof(nd));
840                         strlcpy(nd.ifname, ifa->ifa_name, sizeof(nd.ifname));
841                         if (ioctl(s, SIOCGIFINFO_IN6, (caddr_t)&nd) < 0) {
842                                 warnmsg(LOG_ERR, __func__,
843                                         "ioctl(SIOCGIFINFO_IN6)");
844                                 exit(1);
845                         }
846                         if ((nd.ndi.flags & ND6_IFF_IFDISABLED))
847                                 continue;
848                         if (!(nd.ndi.flags & ND6_IFF_ACCEPT_RTADV))
849                                 continue;
850                 }
851
852                 /* if we find multiple candidates, just warn. */
853                 if (n != 0 && dflag > 1)
854                         warnmsg(LOG_WARNING, __func__,
855                                 "multiple interfaces found");
856
857                 a = (char **)realloc(argv, (n + 1) * sizeof(char **));
858                 if (a == NULL) {
859                         warnmsg(LOG_ERR, __func__, "realloc");
860                         exit(1);
861                 }
862                 argv = a;
863                 argv[n] = strdup(ifa->ifa_name);
864                 if (!argv[n]) {
865                         warnmsg(LOG_ERR, __func__, "malloc");
866                         exit(1);
867                 }
868                 n++;
869         }
870
871         if (n) {
872                 a = (char **)realloc(argv, (n + 1) * sizeof(char **));
873                 if (a == NULL) {
874                         warnmsg(LOG_ERR, __func__, "realloc");
875                         exit(1);
876                 }
877                 argv = a;
878                 argv[n] = NULL;
879
880                 if (dflag > 0) {
881                         for (i = 0; i < n; i++)
882                                 warnmsg(LOG_WARNING, __func__, "probing %s",
883                                         argv[i]);
884                 }
885         }
886         if (!Fflag)
887                 close(s);
888         freeifaddrs(ifap);
889         return (argv);
890 }