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