]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - usr.sbin/rtsold/rtsold.c
- Implement RDNSS and DNSSL options (RFC 6106, IPv6 Router Advertisement
[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
463         while ((ifi = TAILQ_FIRST(&ifinfo_head)) != NULL) {
464                 TAILQ_REMOVE(&ifinfo_head, ifi, ifi_next);
465                 if (ifi->sdl != NULL)
466                         free(ifi->sdl);
467                 if (ifi->rs_data != NULL)
468                         free(ifi->rs_data);
469                 free(ifi);
470         }
471 }
472
473 #if 0
474 static int
475 ifreconfig(char *ifname)
476 {
477         struct ifinfo *ifi, *prev;
478         int rv;
479
480         prev = NULL;
481         TAILQ_FOREACH(ifi, &ifinfo_head, ifi_next) {
482                 if (strncmp(ifi->ifname, ifname, sizeof(ifi->ifname)) == 0)
483                         break;
484                 prev = ifi;
485         }
486         prev->next = ifi->next;
487
488         rv = ifconfig(ifname);
489
490         /* reclaim it after ifconfig() in case ifname is pointer inside ifi */
491         if (ifi->rs_data)
492                 free(ifi->rs_data);
493         free(ifi->sdl);
494         free(ifi);
495
496         return (rv);
497 }
498 #endif
499
500 struct ifinfo *
501 find_ifinfo(int ifindex)
502 {
503         struct ifinfo *ifi;
504
505         TAILQ_FOREACH(ifi, &ifinfo_head, ifi_next) {
506                 if (ifi->sdl->sdl_index == ifindex)
507                         return (ifi);
508         }
509         return (NULL);
510 }
511
512 static int
513 make_packet(struct ifinfo *ifi)
514 {
515         size_t packlen = sizeof(struct nd_router_solicit), lladdroptlen = 0;
516         struct nd_router_solicit *rs;
517         char *buf;
518
519         if ((lladdroptlen = lladdropt_length(ifi->sdl)) == 0) {
520                 warnmsg(LOG_INFO, __func__,
521                     "link-layer address option has null length"
522                     " on %s. Treat as not included.", ifi->ifname);
523         }
524         packlen += lladdroptlen;
525         ifi->rs_datalen = packlen;
526
527         /* allocate buffer */
528         if ((buf = malloc(packlen)) == NULL) {
529                 warnmsg(LOG_ERR, __func__,
530                     "memory allocation failed for %s", ifi->ifname);
531                 return (-1);
532         }
533         ifi->rs_data = buf;
534
535         /* fill in the message */
536         rs = (struct nd_router_solicit *)buf;
537         rs->nd_rs_type = ND_ROUTER_SOLICIT;
538         rs->nd_rs_code = 0;
539         rs->nd_rs_cksum = 0;
540         rs->nd_rs_reserved = 0;
541         buf += sizeof(*rs);
542
543         /* fill in source link-layer address option */
544         if (lladdroptlen)
545                 lladdropt_fill(ifi->sdl, (struct nd_opt_hdr *)buf);
546
547         return (0);
548 }
549
550 static struct timeval *
551 rtsol_check_timer(void)
552 {
553         static struct timeval returnval;
554         struct timeval now, rtsol_timer;
555         struct ifinfo *ifi;
556         struct ra_opt *rao;
557         int flags;
558
559         gettimeofday(&now, NULL);
560
561         rtsol_timer = tm_max;
562
563         TAILQ_FOREACH(ifi, &ifinfo_head, ifi_next) {
564                 if (timercmp(&ifi->expire, &now, <=)) {
565                         if (dflag > 1)
566                                 warnmsg(LOG_DEBUG, __func__,
567                                     "timer expiration on %s, "
568                                     "state = %d", ifi->ifname,
569                                     ifi->state);
570
571                         /* Remove all RA options. */
572                         while ((rao = TAILQ_FIRST(&ifi->ifi_ra_opt)) != NULL) {
573                                 if (rao->rao_msg != NULL)
574                                         free(rao->rao_msg);
575                                 TAILQ_REMOVE(&ifi->ifi_ra_opt, rao, rao_next);
576                                 free(rao);
577                         }
578                         switch (ifi->state) {
579                         case IFS_DOWN:
580                         case IFS_TENTATIVE:
581                                 /* interface_up returns 0 on success */
582                                 flags = interface_up(ifi->ifname);
583                                 if (flags == 0)
584                                         ifi->state = IFS_DELAY;
585                                 else if (flags == IFS_TENTATIVE)
586                                         ifi->state = IFS_TENTATIVE;
587                                 else
588                                         ifi->state = IFS_DOWN;
589                                 break;
590                         case IFS_IDLE:
591                         {
592                                 int oldstatus = ifi->active;
593                                 int probe = 0;
594
595                                 ifi->active = interface_status(ifi);
596
597                                 if (oldstatus != ifi->active) {
598                                         warnmsg(LOG_DEBUG, __func__,
599                                             "%s status is changed"
600                                             " from %d to %d",
601                                             ifi->ifname,
602                                             oldstatus, ifi->active);
603                                         probe = 1;
604                                         ifi->state = IFS_DELAY;
605                                 } else if (ifi->probeinterval &&
606                                     (ifi->probetimer -=
607                                     ifi->timer.tv_sec) <= 0) {
608                                         /* probe timer expired */
609                                         ifi->probetimer =
610                                             ifi->probeinterval;
611                                         probe = 1;
612                                         ifi->state = IFS_PROBE;
613                                 }
614
615                                 /*
616                                  * If we need a probe, clear the previous
617                                  * status wrt the "other" configuration.
618                                  */
619                                 if (probe)
620                                         ifi->otherconfig = 0;
621
622                                 if (probe && mobile_node)
623                                         defrouter_probe(ifi);
624                                 break;
625                         }
626                         case IFS_DELAY:
627                                 ifi->state = IFS_PROBE;
628                                 sendpacket(ifi);
629                                 break;
630                         case IFS_PROBE:
631                                 if (ifi->probes < MAX_RTR_SOLICITATIONS)
632                                         sendpacket(ifi);
633                                 else {
634                                         warnmsg(LOG_INFO, __func__,
635                                             "No answer after sending %d RSs",
636                                             ifi->probes);
637                                         ifi->probes = 0;
638                                         ifi->state = IFS_IDLE;
639                                 }
640                                 break;
641                         }
642                         rtsol_timer_update(ifi);
643                 } else {
644                         /* Expiration check for RA options. */
645                         struct ra_opt *rao_tmp;
646                         int expire = 0;
647
648                         TAILQ_FOREACH_SAFE(rao, &ifi->ifi_ra_opt, rao_next, rao_tmp) {
649                                 warnmsg(LOG_DEBUG, __func__,
650                                         "RA expiration timer: "
651                                         "type=%d, msg=%s, timer=%ld:%08ld",
652                                         rao->rao_type, (char *)rao->rao_msg,
653                                         (long)rao->rao_expire.tv_sec,
654                                         (long)rao->rao_expire.tv_usec);
655                                 if (timercmp(&now, &rao->rao_expire, >=)) {
656                                         warnmsg(LOG_DEBUG, __func__,
657                                                 "RA expiration timer: expired.");
658                                         TAILQ_REMOVE(&ifi->ifi_ra_opt, rao, rao_next);
659                                         expire = 1;
660                                 }
661                         }
662                         if (expire)
663                                 ra_opt_handler(ifi);
664                 }
665                 if (timercmp(&ifi->expire, &rtsol_timer, <))
666                         rtsol_timer = ifi->expire;
667         }
668
669         if (timercmp(&rtsol_timer, &tm_max, ==)) {
670                 warnmsg(LOG_DEBUG, __func__, "there is no timer");
671                 return (NULL);
672         } else if (timercmp(&rtsol_timer, &now, <))
673                 /* this may occur when the interval is too small */
674                 returnval.tv_sec = returnval.tv_usec = 0;
675         else
676                 timersub(&rtsol_timer, &now, &returnval);
677
678         if (dflag > 1)
679                 warnmsg(LOG_DEBUG, __func__, "New timer is %ld:%08ld",
680                     (long)returnval.tv_sec, (long)returnval.tv_usec);
681
682         return (&returnval);
683 }
684
685 void
686 rtsol_timer_update(struct ifinfo *ifi)
687 {
688 #define MILLION 1000000
689 #define DADRETRY 10             /* XXX: adhoc */
690         long interval;
691         struct timeval now;
692
693         bzero(&ifi->timer, sizeof(ifi->timer));
694
695         switch (ifi->state) {
696         case IFS_DOWN:
697         case IFS_TENTATIVE:
698                 if (++ifi->dadcount > DADRETRY) {
699                         ifi->dadcount = 0;
700                         ifi->timer.tv_sec = PROBE_INTERVAL;
701                 } else
702                         ifi->timer.tv_sec = 1;
703                 break;
704         case IFS_IDLE:
705                 if (mobile_node) {
706                         /* XXX should be configurable */
707                         ifi->timer.tv_sec = 3;
708                 }
709                 else
710                         ifi->timer = tm_max;    /* stop timer(valid?) */
711                 break;
712         case IFS_DELAY:
713 #ifndef HAVE_ARC4RANDOM
714                 interval = random() % (MAX_RTR_SOLICITATION_DELAY * MILLION);
715 #else
716                 interval = arc4random_uniform(MAX_RTR_SOLICITATION_DELAY * MILLION);
717 #endif
718                 ifi->timer.tv_sec = interval / MILLION;
719                 ifi->timer.tv_usec = interval % MILLION;
720                 break;
721         case IFS_PROBE:
722                 if (ifi->probes < MAX_RTR_SOLICITATIONS)
723                         ifi->timer.tv_sec = RTR_SOLICITATION_INTERVAL;
724                 else {
725                         /*
726                          * After sending MAX_RTR_SOLICITATIONS solicitations,
727                          * we're just waiting for possible replies; there
728                          * will be no more solicitation.  Thus, we change
729                          * the timer value to MAX_RTR_SOLICITATION_DELAY based
730                          * on RFC 2461, Section 6.3.7.
731                          */
732                         ifi->timer.tv_sec = MAX_RTR_SOLICITATION_DELAY;
733                 }
734                 break;
735         default:
736                 warnmsg(LOG_ERR, __func__,
737                     "illegal interface state(%d) on %s",
738                     ifi->state, ifi->ifname);
739                 return;
740         }
741
742         /* reset the timer */
743         if (timercmp(&ifi->timer, &tm_max, ==)) {
744                 ifi->expire = tm_max;
745                 warnmsg(LOG_DEBUG, __func__,
746                     "stop timer for %s", ifi->ifname);
747         } else {
748                 gettimeofday(&now, NULL);
749                 timeradd(&now, &ifi->timer, &ifi->expire);
750
751                 if (dflag > 1)
752                         warnmsg(LOG_DEBUG, __func__,
753                             "set timer for %s to %d:%d", ifi->ifname,
754                             (int)ifi->timer.tv_sec,
755                             (int)ifi->timer.tv_usec);
756         }
757
758 #undef MILLION
759 }
760
761 /* timer related utility functions */
762 #define MILLION 1000000
763
764 #ifndef SMALL
765 static void
766 rtsold_set_dump_file(int sig __unused)
767 {
768         do_dump = 1;
769 }
770 #endif
771
772 static void
773 usage(void)
774 {
775 #ifndef SMALL
776         fprintf(stderr, "usage: rtsold [-adDfFm1] [-O script-name] "
777             "[-P pidfile] [-R script-name] interfaces...\n");
778         fprintf(stderr, "usage: rtsold [-dDfFm1] [-O script-name] "
779             "[-P pidfile] [-R script-name] -a\n");
780 #else
781         fprintf(stderr, "usage: rtsol [-dDF] [-O script-name] "
782             "[-P pidfile] [-R script-name] interfaces...\n");
783         fprintf(stderr, "usage: rtsol [-dDF] [-O script-name] "
784             "[-P pidfile] [-R script-name] -a\n");
785 #endif
786 }
787
788 void
789 warnmsg(int priority, const char *func, const char *msg, ...)
790 {
791         va_list ap;
792         char buf[BUFSIZ];
793
794         va_start(ap, msg);
795         if (fflag) {
796                 if (priority <= log_upto) {
797                         (void)vfprintf(stderr, msg, ap);
798                         (void)fprintf(stderr, "\n");
799                 }
800         } else {
801                 snprintf(buf, sizeof(buf), "<%s> %s", func, msg);
802                 msg = buf;
803                 vsyslog(priority, msg, ap);
804         }
805         va_end(ap);
806 }
807
808 /*
809  * return a list of interfaces which is suitable to sending an RS.
810  */
811 char **
812 autoifprobe(void)
813 {
814         static char **argv = NULL;
815         static int n = 0;
816         char **a;
817         int s = 0, i, found;
818         struct ifaddrs *ifap, *ifa, *target;
819         struct in6_ndireq nd;
820
821         /* initialize */
822         while (n--)
823                 free(argv[n]);
824         if (argv) {
825                 free(argv);
826                 argv = NULL;
827         }
828         n = 0;
829
830         if (getifaddrs(&ifap) != 0)
831                 return (NULL);
832
833         if (!Fflag && (s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
834                 warnmsg(LOG_ERR, __func__, "socket");
835                 exit(1);
836         }
837
838         target = NULL;
839         /* find an ethernet */
840         for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
841                 if ((ifa->ifa_flags & IFF_UP) == 0)
842                         continue;
843                 if ((ifa->ifa_flags & IFF_POINTOPOINT) != 0)
844                         continue;
845                 if ((ifa->ifa_flags & IFF_LOOPBACK) != 0)
846                         continue;
847                 if ((ifa->ifa_flags & IFF_MULTICAST) == 0)
848                         continue;
849
850                 if (ifa->ifa_addr->sa_family != AF_INET6)
851                         continue;
852
853                 found = 0;
854                 for (i = 0; i < n; i++) {
855                         if (strcmp(argv[i], ifa->ifa_name) == 0) {
856                                 found++;
857                                 break;
858                         }
859                 }
860                 if (found)
861                         continue;
862
863                 /*
864                  * Skip the interfaces which IPv6 and/or accepting RA
865                  * is disabled.
866                  */
867                 if (!Fflag) {
868                         memset(&nd, 0, sizeof(nd));
869                         strlcpy(nd.ifname, ifa->ifa_name, sizeof(nd.ifname));
870                         if (ioctl(s, SIOCGIFINFO_IN6, (caddr_t)&nd) < 0) {
871                                 warnmsg(LOG_ERR, __func__,
872                                         "ioctl(SIOCGIFINFO_IN6)");
873                                 exit(1);
874                         }
875                         if ((nd.ndi.flags & ND6_IFF_IFDISABLED))
876                                 continue;
877                         if (!(nd.ndi.flags & ND6_IFF_ACCEPT_RTADV))
878                                 continue;
879                 }
880
881                 /* if we find multiple candidates, just warn. */
882                 if (n != 0 && dflag > 1)
883                         warnmsg(LOG_WARNING, __func__,
884                                 "multiple interfaces found");
885
886                 a = (char **)realloc(argv, (n + 1) * sizeof(char **));
887                 if (a == NULL) {
888                         warnmsg(LOG_ERR, __func__, "realloc");
889                         exit(1);
890                 }
891                 argv = a;
892                 argv[n] = strdup(ifa->ifa_name);
893                 if (!argv[n]) {
894                         warnmsg(LOG_ERR, __func__, "malloc");
895                         exit(1);
896                 }
897                 n++;
898         }
899
900         if (n) {
901                 a = (char **)realloc(argv, (n + 1) * sizeof(char **));
902                 if (a == NULL) {
903                         warnmsg(LOG_ERR, __func__, "realloc");
904                         exit(1);
905                 }
906                 argv = a;
907                 argv[n] = NULL;
908
909                 if (dflag > 0) {
910                         for (i = 0; i < n; i++)
911                                 warnmsg(LOG_WARNING, __func__, "probing %s",
912                                         argv[i]);
913                 }
914         }
915         if (!Fflag)
916                 close(s);
917         freeifaddrs(ifap);
918         return (argv);
919 }