This repository has been archived by the owner on Dec 4, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathipv6.c
2311 lines (2045 loc) · 55.2 KB
/
ipv6.c
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
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* dhcpcd - DHCP client daemon
* Copyright (c) 2006-2016 Roy Marples <[email protected]>
* 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.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR 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 AUTHOR 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.
*/
#include <sys/param.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <net/if.h>
#include <net/route.h>
#include <netinet/in.h>
#include <netinet/if_ether.h>
#include "config.h"
#ifdef HAVE_SYS_BITOPS_H
#include <sys/bitops.h>
#else
#include "compat/bitops.h"
#endif
#ifdef BSD
/* Purely for the ND6_IFF_AUTO_LINKLOCAL #define which is solely used
* to generate our CAN_ADD_LLADDR #define. */
# include <netinet6/in6_var.h>
# include <netinet6/nd6.h>
#endif
#include <errno.h>
#include <ifaddrs.h>
#include <inttypes.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#define ELOOP_QUEUE 7
#include "common.h"
#include "if.h"
#include "dhcpcd.h"
#include "dhcp6.h"
#include "eloop.h"
#include "ipv6.h"
#include "ipv6nd.h"
#include "sa.h"
#include "script.h"
#ifdef HAVE_MD5_H
# ifndef DEPGEN
# include <md5.h>
# endif
#else
# include "md5.h"
#endif
#ifdef SHA2_H
# include SHA2_H
#else
# include "sha256.h"
#endif
#ifndef SHA256_DIGEST_LENGTH
# define SHA256_DIGEST_LENGTH 32
#endif
#ifdef IPV6_POLLADDRFLAG
# warning kernel does not report IPv6 address flag changes
# warning polling tentative address flags periodically
#endif
/* Hackery at it's finest. */
#ifndef s6_addr32
# ifdef __sun
# define s6_addr32 _S6_un._S6_u32
# else
# define s6_addr32 __u6_addr.__u6_addr32
# endif
#endif
#if defined(HAVE_IN6_ADDR_GEN_MODE_NONE) || defined(ND6_IFF_AUTO_LINKLOCAL) || \
defined(IFF_NOLINKLOCAL)
/* Only add the LL address if we have a carrier, so DaD works. */
#define CAN_ADD_LLADDR(ifp) \
(!((ifp)->options->options & DHCPCD_LINK) || (ifp)->carrier != LINK_DOWN)
#ifdef __sun
/* Although we can add our own LL address, we cannot drop it
* without unplumbing the if which is a lot of code.
* So just keep it for the time being. */
#define CAN_DROP_LLADDR(ifp) (0)
#else
#define CAN_DROP_LLADDR(ifp) (1)
#endif
#else
/* We have no control over the OS adding the LLADDR, so just let it do it
* as we cannot force our own view on it. */
#define CAN_ADD_LLADDR(ifp) (0)
#define CAN_DROP_LLADDR(ifp) (0)
#endif
#ifdef IPV6_MANAGETEMPADDR
static void ipv6_regentempifid(void *);
static void ipv6_regentempaddr(void *);
#else
#define ipv6_regentempifid(a) {}
#endif
int
ipv6_init(struct dhcpcd_ctx *ctx)
{
if (ctx->sndhdr.msg_iovlen == 1)
return 0;
if (ctx->ra_routers == NULL) {
ctx->ra_routers = malloc(sizeof(*ctx->ra_routers));
if (ctx->ra_routers == NULL)
return -1;
}
TAILQ_INIT(ctx->ra_routers);
ctx->sndhdr.msg_namelen = sizeof(struct sockaddr_in6);
ctx->sndhdr.msg_iov = ctx->sndiov;
ctx->sndhdr.msg_iovlen = 1;
ctx->sndhdr.msg_control = ctx->sndbuf;
ctx->sndhdr.msg_controllen = sizeof(ctx->sndbuf);
ctx->rcvhdr.msg_name = &ctx->from;
ctx->rcvhdr.msg_namelen = sizeof(ctx->from);
ctx->rcvhdr.msg_iov = ctx->iov;
ctx->rcvhdr.msg_iovlen = 1;
ctx->rcvhdr.msg_control = ctx->ctlbuf;
// controllen is set at recieve
//ctx->rcvhdr.msg_controllen = sizeof(ctx->rcvbuf);
ctx->nd_fd = -1;
ctx->dhcp6_fd = -1;
return 0;
}
static ssize_t
ipv6_readsecret(struct dhcpcd_ctx *ctx)
{
FILE *fp;
char line[1024];
unsigned char *p;
size_t len;
uint32_t r;
int x;
if ((ctx->secret_len = read_hwaddr_aton(&ctx->secret, SECRET)) != 0)
return (ssize_t)ctx->secret_len;
if (errno != ENOENT)
logger(ctx, LOG_ERR, "error reading secret: %s: %m", SECRET);
/* Chaining arc4random should be good enough.
* RFC7217 section 5.1 states the key SHOULD be at least 128 bits.
* To attempt and future proof ourselves, we'll generate a key of
* 512 bits (64 bytes). */
if (ctx->secret_len < 64) {
if ((ctx->secret = malloc(64)) == NULL) {
logger(ctx, LOG_ERR, "%s: malloc: %m", __func__);
return -1;
}
ctx->secret_len = 64;
}
p = ctx->secret;
for (len = 0; len < 512 / NBBY; len += sizeof(r)) {
r = arc4random();
memcpy(p, &r, sizeof(r));
p += sizeof(r);
}
/* Ensure that only the dhcpcd user can read the secret.
* Write permission is also denied as chaning it would remove
* it's stability. */
if ((fp = fopen(SECRET, "w")) == NULL ||
chmod(SECRET, S_IRUSR) == -1)
goto eexit;
x = fprintf(fp, "%s\n",
hwaddr_ntoa(ctx->secret, ctx->secret_len, line, sizeof(line)));
if (fclose(fp) == EOF)
x = -1;
fp = NULL;
if (x > 0)
return (ssize_t)ctx->secret_len;
eexit:
logger(ctx, LOG_ERR, "error writing secret: %s: %m", SECRET);
if (fp != NULL)
fclose(fp);
unlink(SECRET);
ctx->secret_len = 0;
return -1;
}
/* http://www.iana.org/assignments/ipv6-interface-ids/ipv6-interface-ids.xhtml
* RFC5453 */
static const struct reslowhigh {
const uint8_t high[8];
const uint8_t low[8];
} reslowhigh[] = {
/* RFC4291 + RFC6543 */
{ { 0x02, 0x00, 0x5e, 0xff, 0xfe, 0x00, 0x00, 0x00 },
{ 0x02, 0x00, 0x5e, 0xff, 0xfe, 0xff, 0xff, 0xff } },
/* RFC2526 */
{ { 0xfd, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x80 },
{ 0xfd, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff } }
};
static int
ipv6_reserved(const struct in6_addr *addr)
{
uint64_t id, low, high;
size_t i;
const struct reslowhigh *r;
id = be64dec(addr->s6_addr + sizeof(id));
if (id == 0) /* RFC4291 */
return 1;
for (i = 0; i < sizeof(reslowhigh) / sizeof(reslowhigh[0]); i++) {
r = &reslowhigh[i];
low = be64dec(r->low);
high = be64dec(r->high);
if (id >= low && id <= high)
return 1;
}
return 0;
}
/* RFC7217 */
static int
ipv6_makestableprivate1(struct in6_addr *addr,
const struct in6_addr *prefix, int prefix_len,
const unsigned char *netiface, size_t netiface_len,
const unsigned char *netid, size_t netid_len,
uint32_t *dad_counter,
const unsigned char *secret, size_t secret_len)
{
unsigned char buf[2048], *p, digest[SHA256_DIGEST_LENGTH];
size_t len, l;
SHA256_CTX ctx;
if (prefix_len < 0 || prefix_len > 120) {
errno = EINVAL;
return -1;
}
l = (size_t)(ROUNDUP8(prefix_len) / NBBY);
len = l + netiface_len + netid_len + sizeof(*dad_counter) + secret_len;
if (len > sizeof(buf)) {
errno = ENOBUFS;
return -1;
}
for (;; (*dad_counter)++) {
/* Combine all parameters into one buffer */
p = buf;
memcpy(p, prefix, l);
p += l;
memcpy(p, netiface, netiface_len);
p += netiface_len;
memcpy(p, netid, netid_len);
p += netid_len;
memcpy(p, dad_counter, sizeof(*dad_counter));
p += sizeof(*dad_counter);
memcpy(p, secret, secret_len);
/* Make an address using the digest of the above.
* RFC7217 Section 5.1 states that we shouldn't use MD5.
* Pity as we use that for HMAC-MD5 which is still deemed OK.
* SHA-256 is recommended */
SHA256_Init(&ctx);
SHA256_Update(&ctx, buf, len);
SHA256_Final(digest, &ctx);
p = addr->s6_addr;
memcpy(p, prefix, l);
/* RFC7217 section 5.2 says we need to start taking the id from
* the least significant bit */
len = sizeof(addr->s6_addr) - l;
memcpy(p + l, digest + (sizeof(digest) - len), len);
/* Ensure that the Interface ID does not match a reserved one,
* if it does then treat it as a DAD failure.
* RFC7217 section 5.2 */
if (prefix_len != 64)
break;
if (!ipv6_reserved(addr))
break;
}
return 0;
}
int
ipv6_makestableprivate(struct in6_addr *addr,
const struct in6_addr *prefix, int prefix_len,
const struct interface *ifp,
int *dad_counter)
{
uint32_t dad;
int r;
if (ifp->ctx->secret_len == 0) {
if (ipv6_readsecret(ifp->ctx) == -1)
return -1;
}
dad = (uint32_t)*dad_counter;
/* For our implementation, we shall set the hardware address
* as the interface identifier */
r = ipv6_makestableprivate1(addr, prefix, prefix_len,
ifp->hwaddr, ifp->hwlen,
ifp->ssid, ifp->ssid_len,
&dad,
ifp->ctx->secret, ifp->ctx->secret_len);
if (r == 0)
*dad_counter = (int)dad;
return r;
}
int
ipv6_makeaddr(struct in6_addr *addr, struct interface *ifp,
const struct in6_addr *prefix, int prefix_len)
{
const struct ipv6_addr *ap;
int dad;
if (prefix_len < 0 || prefix_len > 120) {
errno = EINVAL;
return -1;
}
if (ifp->options->options & DHCPCD_SLAACPRIVATE) {
dad = 0;
if (ipv6_makestableprivate(addr,
prefix, prefix_len, ifp, &dad) == -1)
return -1;
return dad;
}
if (prefix_len > 64) {
errno = EINVAL;
return -1;
}
if ((ap = ipv6_linklocal(ifp)) == NULL) {
/* We delay a few functions until we get a local-link address
* so this should never be hit. */
errno = ENOENT;
return -1;
}
/* Make the address from the first local-link address */
memcpy(addr, prefix, sizeof(*prefix));
addr->s6_addr32[2] = ap->addr.s6_addr32[2];
addr->s6_addr32[3] = ap->addr.s6_addr32[3];
return 0;
}
int
ipv6_makeprefix(struct in6_addr *prefix, const struct in6_addr *addr, int len)
{
int bytes, bits;
if (len < 0 || len > 128) {
errno = EINVAL;
return -1;
}
bytes = len / NBBY;
bits = len % NBBY;
memcpy(&prefix->s6_addr, &addr->s6_addr, (size_t)bytes);
if (bits != 0) {
/* Coverify false positive.
* bytelen cannot be 16 if bitlen is non zero */
/* coverity[overrun-local] */
prefix->s6_addr[bytes] =
(uint8_t)(prefix->s6_addr[bytes] >> (NBBY - bits));
}
memset((char *)prefix->s6_addr + bytes, 0,
sizeof(prefix->s6_addr) - (size_t)bytes);
return 0;
}
int
ipv6_mask(struct in6_addr *mask, int len)
{
static const unsigned char masks[NBBY] =
{ 0x80, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc, 0xfe, 0xff };
int bytes, bits, i;
if (len < 0 || len > 128) {
errno = EINVAL;
return -1;
}
memset(mask, 0, sizeof(*mask));
bytes = len / NBBY;
bits = len % NBBY;
for (i = 0; i < bytes; i++)
mask->s6_addr[i] = 0xff;
if (bits) {
/* Coverify false positive.
* bytelen cannot be 16 if bitlen is non zero */
/* coverity[overrun-local] */
mask->s6_addr[bytes] = masks[bits - 1];
}
return 0;
}
uint8_t
ipv6_prefixlen(const struct in6_addr *mask)
{
int x = 0, y;
const unsigned char *lim, *p;
lim = (const unsigned char *)mask + sizeof(*mask);
for (p = (const unsigned char *)mask; p < lim; x++, p++) {
if (*p != 0xff)
break;
}
y = 0;
if (p < lim) {
for (y = 0; y < NBBY; y++) {
if ((*p & (0x80 >> y)) == 0)
break;
}
}
/*
* when the limit pointer is given, do a stricter check on the
* remaining bits.
*/
if (p < lim) {
if (y != 0 && (*p & (0x00ff >> y)) != 0)
return 0;
for (p = p + 1; p < lim; p++)
if (*p != 0)
return 0;
}
return (uint8_t)(x * NBBY + y);
}
static void
in6_to_h64(uint64_t *vhigh, uint64_t *vlow, const struct in6_addr *addr)
{
*vhigh = be64dec(addr->s6_addr);
*vlow = be64dec(addr->s6_addr + 8);
}
static void
h64_to_in6(struct in6_addr *addr, uint64_t vhigh, uint64_t vlow)
{
be64enc(addr->s6_addr, vhigh);
be64enc(addr->s6_addr + 8, vlow);
}
int
ipv6_userprefix(
const struct in6_addr *prefix, // prefix from router
short prefix_len, // length of prefix received
uint64_t user_number, // "random" number from user
struct in6_addr *result, // resultant prefix
short result_len) // desired prefix length
{
uint64_t vh, vl, user_low, user_high;
if (prefix_len < 1 || prefix_len > 128 ||
result_len < 1 || result_len > 128)
{
errno = EINVAL;
return -1;
}
/* Check that the user_number fits inside result_len less prefix_len */
if (result_len < prefix_len ||
fls64(user_number) > result_len - prefix_len)
{
errno = ERANGE;
return -1;
}
/* If user_number is zero, just copy the prefix into the result. */
if (user_number == 0) {
*result = *prefix;
return 0;
}
/* Shift user_number so it fit's just inside result_len.
* Shifting by 0 or sizeof(user_number) is undefined,
* so we cater for that. */
if (result_len == 128) {
user_high = 0;
user_low = user_number;
} else if (result_len > 64) {
if (prefix_len >= 64)
user_high = 0;
else
user_high = user_number >> (result_len - prefix_len);
user_low = user_number << (128 - result_len);
} else if (result_len == 64) {
user_high = user_number;
user_low = 0;
} else {
user_high = user_number << (64 - result_len);
user_low = 0;
}
/* convert to two 64bit host order values */
in6_to_h64(&vh, &vl, prefix);
vh |= user_high;
vl |= user_low;
/* copy back result */
h64_to_in6(result, vh, vl);
return 0;
}
#ifdef IPV6_POLLADDRFLAG
void
ipv6_checkaddrflags(void *arg)
{
struct ipv6_addr *ia;
int flags;
const char *alias;
ia = arg;
#ifdef ALIAS_ADDR
alias = ia->alias;
#else
alias = NULL;
#endif
if ((flags = if_addrflags6(ia->iface, &ia->addr, alias)) == -1) {
logger(ia->iface->ctx, LOG_ERR,
"%s: if_addrflags6: %m", ia->iface->name);
return;
}
if (!(flags & IN6_IFF_TENTATIVE)) {
/* Simulate the kernel announcing the new address. */
ipv6_handleifa(ia->iface->ctx, RTM_NEWADDR,
ia->iface->ctx->ifaces, ia->iface->name,
&ia->addr, ia->prefix_len, flags);
} else {
/* Still tentative? Check again in a bit. */
struct timespec tv;
ms_to_ts(&tv, RETRANS_TIMER / 2);
eloop_timeout_add_tv(ia->iface->ctx->eloop, &tv,
ipv6_checkaddrflags, ia);
}
}
#endif
static void
ipv6_deleteaddr(struct ipv6_addr *ia)
{
struct ipv6_state *state;
struct ipv6_addr *ap;
logger(ia->iface->ctx, LOG_INFO, "%s: deleting address %s",
ia->iface->name, ia->saddr);
if (if_address6(RTM_DELADDR, ia) == -1 &&
errno != EADDRNOTAVAIL && errno != ESRCH &&
errno != ENXIO && errno != ENODEV)
logger(ia->iface->ctx, LOG_ERR, "if_address6: %m");
/* NOREJECT is set if we delegated exactly the prefix to another
* address.
* This can only be one address, so just clear the flag.
* This should ensure the reject route will be restored. */
if (ia->delegating_prefix != NULL)
ia->delegating_prefix->flags &= ~IPV6_AF_NOREJECT;
state = IPV6_STATE(ia->iface);
TAILQ_FOREACH(ap, &state->addrs, next) {
if (IN6_ARE_ADDR_EQUAL(&ap->addr, &ia->addr)) {
TAILQ_REMOVE(&state->addrs, ap, next);
ipv6_freeaddr(ap);
break;
}
}
}
static int
ipv6_addaddr1(struct ipv6_addr *ap, const struct timespec *now)
{
struct interface *ifp;
struct ipv6_state *state;
struct ipv6_addr *nap;
uint32_t pltime, vltime;
/* Ensure no other interface has this address */
TAILQ_FOREACH(ifp, ap->iface->ctx->ifaces, next) {
if (ifp == ap->iface)
continue;
state = IPV6_STATE(ifp);
if (state == NULL)
continue;
TAILQ_FOREACH(nap, &state->addrs, next) {
if (IN6_ARE_ADDR_EQUAL(&nap->addr, &ap->addr)) {
ipv6_deleteaddr(nap);
break;
}
}
}
if (!(ap->flags & IPV6_AF_DADCOMPLETED) &&
ipv6_iffindaddr(ap->iface, &ap->addr, IN6_IFF_NOTUSEABLE))
ap->flags |= IPV6_AF_DADCOMPLETED;
logger(ap->iface->ctx, ap->flags & IPV6_AF_NEW ? LOG_INFO : LOG_DEBUG,
"%s: adding %saddress %s", ap->iface->name,
#ifdef IPV6_AF_TEMPORARY
ap->flags & IPV6_AF_TEMPORARY ? "temporary " : "",
#else
"",
#endif
ap->saddr);
if (ap->prefix_pltime == ND6_INFINITE_LIFETIME &&
ap->prefix_vltime == ND6_INFINITE_LIFETIME)
logger(ap->iface->ctx, LOG_DEBUG,
"%s: pltime infinity, vltime infinity",
ap->iface->name);
else if (ap->prefix_pltime == ND6_INFINITE_LIFETIME)
logger(ap->iface->ctx, LOG_DEBUG,
"%s: pltime infinity, vltime %"PRIu32" seconds",
ap->iface->name, ap->prefix_vltime);
else if (ap->prefix_vltime == ND6_INFINITE_LIFETIME)
logger(ap->iface->ctx, LOG_DEBUG,
"%s: pltime %"PRIu32"seconds, vltime infinity",
ap->iface->name, ap->prefix_pltime);
else
logger(ap->iface->ctx, LOG_DEBUG,
"%s: pltime %"PRIu32" seconds, vltime %"PRIu32" seconds",
ap->iface->name, ap->prefix_pltime, ap->prefix_vltime);
/* Adjust plftime and vltime based on acquired time */
pltime = ap->prefix_pltime;
vltime = ap->prefix_vltime;
if (timespecisset(&ap->acquired) &&
(ap->prefix_pltime != ND6_INFINITE_LIFETIME ||
ap->prefix_vltime != ND6_INFINITE_LIFETIME))
{
struct timespec n;
if (now == NULL) {
clock_gettime(CLOCK_MONOTONIC, &n);
now = &n;
}
timespecsub(now, &ap->acquired, &n);
if (ap->prefix_pltime != ND6_INFINITE_LIFETIME) {
ap->prefix_pltime -= (uint32_t)n.tv_sec;
/* This can happen when confirming a
* deprecated but still valid lease. */
if (ap->prefix_pltime > pltime)
ap->prefix_pltime = 0;
}
if (ap->prefix_vltime != ND6_INFINITE_LIFETIME)
ap->prefix_vltime -= (uint32_t)n.tv_sec;
#if 0
logger(ap->iface->ctx, LOG_DEBUG,
"%s: acquired %lld.%.9ld, now %lld.%.9ld, diff %lld.%.9ld",
ap->iface->name,
(long long)ap->acquired.tv_sec, ap->acquired.tv_nsec,
(long long)now->tv_sec, now->tv_nsec,
(long long)n.tv_sec, n.tv_nsec);
logger(ap->iface->ctx, LOG_DEBUG,
"%s: adj pltime %"PRIu32" seconds, "
"vltime %"PRIu32" seconds",
ap->iface->name, ap->prefix_pltime, ap->prefix_vltime);
#endif
}
if (if_address6(RTM_NEWADDR, ap) == -1) {
logger(ap->iface->ctx, LOG_ERR, "if_addaddress6: %m");
/* Restore real pltime and vltime */
ap->prefix_pltime = pltime;
ap->prefix_vltime = vltime;
return -1;
}
#ifdef IPV6_MANAGETEMPADDR
/* RFC4941 Section 3.4 */
if (ap->flags & IPV6_AF_TEMPORARY &&
ap->prefix_pltime &&
ap->prefix_vltime &&
ap->iface->options->options & DHCPCD_IPV6RA_OWN &&
ip6_use_tempaddr(ap->iface->name))
eloop_timeout_add_sec(ap->iface->ctx->eloop,
(time_t)ap->prefix_pltime - REGEN_ADVANCE,
ipv6_regentempaddr, ap);
#endif
/* Restore real pltime and vltime */
ap->prefix_pltime = pltime;
ap->prefix_vltime = vltime;
ap->flags &= ~IPV6_AF_NEW;
ap->flags |= IPV6_AF_ADDED;
if (ap->delegating_prefix != NULL)
ap->flags |= IPV6_AF_DELEGATED;
#ifdef IPV6_POLLADDRFLAG
eloop_timeout_delete(ap->iface->ctx->eloop,
ipv6_checkaddrflags, ap);
if (!(ap->flags & IPV6_AF_DADCOMPLETED)) {
struct timespec tv;
ms_to_ts(&tv, RETRANS_TIMER / 2);
eloop_timeout_add_tv(ap->iface->ctx->eloop,
&tv, ipv6_checkaddrflags, ap);
}
#endif
#ifdef __sun
/* Solaris does not announce new addresses which need DaD
* so we need to take a copy and add it to our list.
* Otherwise aliasing gets confused if we add another
* address during DaD. */
state = IPV6_STATE(ap->iface);
TAILQ_FOREACH(nap, &state->addrs, next) {
if (IN6_ARE_ADDR_EQUAL(&nap->addr, &ap->addr))
break;
}
if (nap == NULL) {
if ((nap = malloc(sizeof(*nap))) == NULL) {
syslog(LOG_ERR, "%s: malloc: %m", __func__);
return 0; /* Well, we did add the address */
}
memcpy(nap, ap, sizeof(*nap));
TAILQ_INSERT_TAIL(&state->addrs, nap, next);
}
#endif
return 0;
}
#ifdef ALIAS_ADDR
/* Find the next logical aliase address we can use. */
static int
ipv6_aliasaddr(struct ipv6_addr *ia, struct ipv6_addr **repl)
{
struct ipv6_state *state;
struct ipv6_addr *iap;
unsigned int unit;
char alias[IF_NAMESIZE];
if (ia->alias[0] != '\0')
return 0;
state = IPV6_STATE(ia->iface);
/* First find an existng address.
* This can happen when dhcpcd restarts as ND and DHCPv6
* maintain their own lists of addresses. */
TAILQ_FOREACH(iap, &state->addrs, next) {
if (iap->alias[0] != '\0' &&
IN6_ARE_ADDR_EQUAL(&iap->addr, &ia->addr))
{
strlcpy(ia->alias, iap->alias, sizeof(ia->alias));
return 0;
}
}
unit = 0;
find_unit:
if (unit == 0)
strlcpy(alias, ia->iface->name, sizeof(alias));
else
snprintf(alias, sizeof(alias), "%s:%u", ia->iface->name, unit);
TAILQ_FOREACH(iap, &state->addrs, next) {
if (iap->alias[0] == '\0')
continue;
if (IN6_IS_ADDR_UNSPECIFIED(&iap->addr)) {
/* No address assigned? Lets use it. */
strlcpy(ia->alias, iap->alias, sizeof(ia->alias));
if (repl)
*repl = iap;
return 1;
}
if (strcmp(iap->alias, alias) == 0)
break;
}
if (iap != NULL) {
if (unit == UINT_MAX) {
errno = ERANGE;
return -1;
}
unit++;
goto find_unit;
}
strlcpy(ia->alias, alias, sizeof(ia->alias));
return 0;
}
#endif
int
ipv6_addaddr(struct ipv6_addr *ia, const struct timespec *now)
{
int r;
#ifdef ALIAS_ADDR
int replaced, blank;
struct ipv6_addr *replaced_ia;
blank = (ia->alias[0] == '\0');
if ((replaced = ipv6_aliasaddr(ia, &replaced_ia)) == -1)
return -1;
if (blank)
logger(ia->iface->ctx, LOG_DEBUG, "%s: aliased %s",
ia->alias, ia->saddr);
#endif
if ((r = ipv6_addaddr1(ia, now)) == 0) {
#ifdef ALIAS_ADDR
if (replaced) {
struct ipv6_state *state;
state = IPV6_STATE(ia->iface);
TAILQ_REMOVE(&state->addrs, replaced_ia, next);
ipv6_freeaddr(replaced_ia);
}
#endif
}
return r;
}
int
ipv6_findaddrmatch(const struct ipv6_addr *addr, const struct in6_addr *match,
short flags)
{
if (match == NULL) {
if ((addr->flags &
(IPV6_AF_ADDED | IPV6_AF_DADCOMPLETED)) ==
(IPV6_AF_ADDED | IPV6_AF_DADCOMPLETED))
return 1;
} else if (addr->prefix_vltime &&
IN6_ARE_ADDR_EQUAL(&addr->addr, match) &&
(!flags || addr->flags & flags))
return 1;
return 0;
}
struct ipv6_addr *
ipv6_findaddr(struct dhcpcd_ctx *ctx, const struct in6_addr *addr, short flags)
{
struct ipv6_addr *dap, *nap;
dap = dhcp6_findaddr(ctx, addr, flags);
nap = ipv6nd_findaddr(ctx, addr, flags);
if (!dap && !nap)
return NULL;
if (dap && !nap)
return dap;
if (nap && !dap)
return nap;
if (nap->iface->metric < dap->iface->metric)
return nap;
return dap;
}
ssize_t
ipv6_addaddrs(struct ipv6_addrhead *addrs)
{
struct ipv6_addr *ap, *apn, *apf;
ssize_t i;
struct timespec now;
i = 0;
timespecclear(&now);
TAILQ_FOREACH_SAFE(ap, addrs, next, apn) {
if (ap->prefix_vltime == 0) {
if (ap->flags & IPV6_AF_ADDED) {
ipv6_deleteaddr(ap);
i++;
}
eloop_q_timeout_delete(ap->iface->ctx->eloop,
0, NULL, ap);
if (ap->flags & IPV6_AF_REQUEST) {
ap->flags &= ~IPV6_AF_ADDED;
} else {
TAILQ_REMOVE(addrs, ap, next);
ipv6_freeaddr(ap);
}
} else if (!(ap->flags & IPV6_AF_STALE) &&
!IN6_IS_ADDR_UNSPECIFIED(&ap->addr))
{
apf = ipv6_findaddr(ap->iface->ctx,
&ap->addr, IPV6_AF_ADDED);
if (apf && apf->iface != ap->iface) {
if (apf->iface->metric <= ap->iface->metric) {
logger(apf->iface->ctx, LOG_INFO,
"%s: preferring %s on %s",
ap->iface->name,
ap->saddr,
apf->iface->name);
continue;
}
logger(apf->iface->ctx, LOG_INFO,
"%s: preferring %s on %s",
apf->iface->name,
ap->saddr,
ap->iface->name);
if (if_address6(RTM_DELADDR, apf) == -1 &&
errno != EADDRNOTAVAIL && errno != ENXIO)
logger(apf->iface->ctx, LOG_ERR,
"if_address6: %m");
apf->flags &=
~(IPV6_AF_ADDED | IPV6_AF_DADCOMPLETED);
} else if (apf)
apf->flags &= ~IPV6_AF_ADDED;
if (ap->flags & IPV6_AF_NEW)
i++;
if (!timespecisset(&now))
clock_gettime(CLOCK_MONOTONIC, &now);
ipv6_addaddr(ap, &now);
}
}
return i;
}
void
ipv6_freeaddr(struct ipv6_addr *ap)
{
struct ipv6_addr *ia;
/* Forget the reference */
if (ap->flags & IPV6_AF_DELEGATEDPFX) {
TAILQ_FOREACH(ia, &ap->pd_pfxs, pd_next) {
ia->delegating_prefix = NULL;
}
} else if (ap->delegating_prefix != NULL) {
TAILQ_REMOVE(&ap->delegating_prefix->pd_pfxs, ap, pd_next);
}
eloop_q_timeout_delete(ap->iface->ctx->eloop, 0, NULL, ap);
free(ap);
}
void
ipv6_freedrop_addrs(struct ipv6_addrhead *addrs, int drop,
const struct interface *ifd)
{
struct ipv6_addr *ap, *apn, *apf;
struct timespec now;
timespecclear(&now);
TAILQ_FOREACH_SAFE(ap, addrs, next, apn) {
if (ifd != NULL &&
(ap->delegating_prefix == NULL ||
ap->delegating_prefix->iface != ifd))
continue;
if (drop != 2)
TAILQ_REMOVE(addrs, ap, next);
if (drop && ap->flags & IPV6_AF_ADDED &&
(ap->iface->options->options &
(DHCPCD_EXITING | DHCPCD_PERSISTENT)) !=
(DHCPCD_EXITING | DHCPCD_PERSISTENT))
{
/* Don't drop link-local addresses. */
if (!IN6_IS_ADDR_LINKLOCAL(&ap->addr) ||
CAN_DROP_LLADDR(ap->iface))
{
if (drop == 2)