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