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 pathdhcp6.c
3790 lines (3447 loc) · 89.1 KB
/
dhcp6.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.
*/
/* TODO: We should decline dupliate addresses detected */
#include <sys/stat.h>
#include <sys/utsname.h>
#include <netinet/in.h>
#include <assert.h>
#include <ctype.h>
#include <errno.h>
#include <fcntl.h>
#include <inttypes.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#define ELOOP_QUEUE 4
#include "config.h"
#include "common.h"
#include "dhcp.h"
#include "dhcp6.h"
#include "duid.h"
#include "eloop.h"
#include "if.h"
#include "if-options.h"
#include "ipv6nd.h"
#include "script.h"
#ifdef HAVE_SYS_BITOPS_H
#include <sys/bitops.h>
#else
#include "compat/bitops.h"
#endif
/* DHCPCD Project has been assigned an IANA PEN of 40712 */
#define DHCPCD_IANA_PEN 40712
/* Unsure if I want this */
//#define VENDOR_SPLIT
/* Support older systems with different defines */
#if !defined(IPV6_RECVPKTINFO) && defined(IPV6_PKTINFO)
#define IPV6_RECVPKTINFO IPV6_PKTINFO
#endif
#ifdef DHCP6
/* Assert the correct structure size for on wire */
struct dhcp6_message {
uint8_t type;
uint8_t xid[3];
/* followed by options */
};
__CTASSERT(sizeof(struct dhcp6_message) == 4);
struct dhcp6_option {
uint16_t code;
uint16_t len;
/* followed by data */
};
__CTASSERT(sizeof(struct dhcp6_option) == 4);
struct dhcp6_ia_na {
uint8_t iaid[4];
uint32_t t1;
uint32_t t2;
};
__CTASSERT(sizeof(struct dhcp6_ia_na) == 12);
struct dhcp6_ia_addr {
struct in6_addr addr;
uint32_t pltime;
uint32_t vltime;
};
__CTASSERT(sizeof(struct dhcp6_ia_addr) == 16 + 8);
/* XXX FIXME: This is the only packed structure and it does not align.
* Maybe manually decode it? */
struct dhcp6_pd_addr {
uint32_t pltime;
uint32_t vltime;
uint8_t prefix_len;
struct in6_addr prefix;
} __packed;
__CTASSERT(sizeof(struct dhcp6_pd_addr) == 8 + 1 + 16);
struct dhcp6_op {
uint16_t type;
const char *name;
};
static const struct dhcp6_op dhcp6_ops[] = {
{ DHCP6_SOLICIT, "SOLICIT6" },
{ DHCP6_ADVERTISE, "ADVERTISE6" },
{ DHCP6_REQUEST, "REQUEST6" },
{ DHCP6_REPLY, "REPLY6" },
{ DHCP6_RENEW, "RENEW6" },
{ DHCP6_REBIND, "REBIND6" },
{ DHCP6_CONFIRM, "CONFIRM6" },
{ DHCP6_INFORMATION_REQ, "INFORM6" },
{ DHCP6_RELEASE, "RELEASE6" },
{ DHCP6_RECONFIGURE, "RECONFIGURE6" },
{ 0, NULL }
};
struct dhcp_compat {
uint8_t dhcp_opt;
uint16_t dhcp6_opt;
};
const struct dhcp_compat dhcp_compats[] = {
{ DHO_DNSSERVER, D6_OPTION_DNS_SERVERS },
{ DHO_HOSTNAME, D6_OPTION_FQDN },
{ DHO_DNSDOMAIN, D6_OPTION_FQDN },
{ DHO_NISSERVER, D6_OPTION_NIS_SERVERS },
{ DHO_NTPSERVER, D6_OPTION_SNTP_SERVERS },
{ DHO_RAPIDCOMMIT, D6_OPTION_RAPID_COMMIT },
{ DHO_FQDN, D6_OPTION_FQDN },
{ DHO_VIVCO, D6_OPTION_VENDOR_CLASS },
{ DHO_VIVSO, D6_OPTION_VENDOR_OPTS },
{ DHO_DNSSEARCH, D6_OPTION_DOMAIN_LIST },
{ 0, 0 }
};
static const char * const dhcp6_statuses[] = {
"Success",
"Unspecified Failure",
"No Addresses Available",
"No Binding",
"Not On Link",
"Use Multicast",
"No Prefix Available"
};
void
dhcp6_printoptions(const struct dhcpcd_ctx *ctx,
const struct dhcp_opt *opts, size_t opts_len)
{
size_t i, j;
const struct dhcp_opt *opt, *opt2;
int cols;
for (i = 0, opt = ctx->dhcp6_opts;
i < ctx->dhcp6_opts_len; i++, opt++)
{
for (j = 0, opt2 = opts; j < opts_len; j++, opt2++)
if (opt2->option == opt->option)
break;
if (j == opts_len) {
cols = printf("%05d %s", opt->option, opt->var);
dhcp_print_option_encoding(opt, cols);
}
}
for (i = 0, opt = opts; i < opts_len; i++, opt++) {
cols = printf("%05d %s", opt->option, opt->var);
dhcp_print_option_encoding(opt, cols);
}
}
static size_t
dhcp6_makevendor(void *data, const struct interface *ifp)
{
const struct if_options *ifo;
size_t len, i;
uint8_t *p;
ssize_t vlen;
const struct vivco *vivco;
char vendor[VENDORCLASSID_MAX_LEN];
struct dhcp6_option o;
ifo = ifp->options;
len = sizeof(uint32_t); /* IANA PEN */
if (ifo->vivco_en) {
for (i = 0, vivco = ifo->vivco;
i < ifo->vivco_len;
i++, vivco++)
len += sizeof(uint16_t) + vivco->len;
vlen = 0; /* silence bogus gcc warning */
} else {
vlen = dhcp_vendor(vendor, sizeof(vendor));
if (vlen == -1)
vlen = 0;
else
len += sizeof(uint16_t) + (size_t)vlen;
}
if (len > UINT16_MAX) {
logger(ifp->ctx, LOG_ERR,
"%s: DHCPv6 Vendor Class too big", ifp->name);
return 0;
}
if (data != NULL) {
uint32_t pen;
uint16_t hvlen;
p = data;
o.code = htons(D6_OPTION_VENDOR_CLASS);
o.len = htons((uint16_t)len);
memcpy(p, &o, sizeof(o));
p += sizeof(o);
pen = htonl(ifo->vivco_en ? ifo->vivco_en : DHCPCD_IANA_PEN);
memcpy(p, &pen, sizeof(pen));
p += sizeof(pen);
if (ifo->vivco_en) {
for (i = 0, vivco = ifo->vivco;
i < ifo->vivco_len;
i++, vivco++)
{
hvlen = htons((uint16_t)vivco->len);
memcpy(p, &hvlen, sizeof(hvlen));
p += sizeof(len);
memcpy(p, vivco->data, vivco->len);
p += vivco->len;
}
} else if (vlen) {
hvlen = htons((uint16_t)vlen);
memcpy(p, &hvlen, sizeof(hvlen));
p += sizeof(hvlen);
memcpy(p, vendor, (size_t)vlen);
}
}
return sizeof(o) + len;
}
static void *
dhcp6_findoption(void *data, size_t data_len, uint16_t code, uint16_t *len)
{
uint8_t *d;
struct dhcp6_option o;
code = htons(code);
for (d = data; data_len != 0; d += o.len, data_len -= o.len) {
if (data_len < sizeof(o)) {
errno = EINVAL;
return NULL;
}
memcpy(&o, d, sizeof(o));
d += sizeof(o);
data_len -= sizeof(o);
o.len = htons(o.len);
if (data_len < o.len) {
errno = EINVAL;
return NULL;
}
if (o.code == code) {
if (len != NULL)
*len = o.len;
return d;
}
}
errno = ENOENT;
return NULL;
}
static void *
dhcp6_findmoption(void *data, size_t data_len, uint16_t code,
uint16_t *len)
{
uint8_t *d;
if (data_len < sizeof(struct dhcp6_message)) {
errno = EINVAL;
return false;
}
d = data;
d += sizeof(struct dhcp6_message);
data_len -= sizeof(struct dhcp6_message);
return dhcp6_findoption(d, data_len, code, len);
}
static const uint8_t *
dhcp6_getoption(struct dhcpcd_ctx *ctx,
size_t *os, unsigned int *code, size_t *len,
const uint8_t *od, size_t ol, struct dhcp_opt **oopt)
{
struct dhcp6_option o;
size_t i;
struct dhcp_opt *opt;
if (od != NULL) {
*os = sizeof(o);
if (ol < *os) {
errno = EINVAL;
return NULL;
}
memcpy(&o, od, sizeof(o));
*len = ntohs(o.len);
if (*len > ol - *os) {
errno = ERANGE;
return NULL;
}
*code = ntohs(o.code);
}
*oopt = NULL;
for (i = 0, opt = ctx->dhcp6_opts;
i < ctx->dhcp6_opts_len; i++, opt++)
{
if (opt->option == *code) {
*oopt = opt;
break;
}
}
if (od != NULL)
return od + sizeof(o);
return NULL;
}
static bool
dhcp6_updateelapsed(struct interface *ifp, struct dhcp6_message *m, size_t len)
{
uint8_t *opt;
uint16_t opt_len;
struct dhcp6_state *state;
struct timespec tv;
time_t hsec;
uint16_t sec;
opt = dhcp6_findmoption(m, len, D6_OPTION_ELAPSED, &opt_len);
if (opt == NULL)
return false;
if (opt_len != sizeof(sec)) {
errno = EINVAL;
return false;
}
state = D6_STATE(ifp);
clock_gettime(CLOCK_MONOTONIC, &tv);
if (state->RTC == 0) {
/* An RTC of zero means we're the first message
* out of the door, so the elapsed time is zero. */
state->started = tv;
hsec = 0;
} else {
timespecsub(&tv, &state->started, &tv);
/* Elapsed time is measured in centiseconds.
* We need to be sure it will not potentially overflow. */
if (tv.tv_sec >= (UINT16_MAX / CSEC_PER_SEC) + 1)
hsec = UINT16_MAX;
else {
hsec = (tv.tv_sec * CSEC_PER_SEC) +
(tv.tv_nsec / NSEC_PER_CSEC);
if (hsec > UINT16_MAX)
hsec = UINT16_MAX;
}
}
sec = htons((uint16_t)hsec);
memcpy(opt, &sec, sizeof(sec));
return true;
}
static void
dhcp6_newxid(const struct interface *ifp, struct dhcp6_message *m)
{
uint32_t xid;
if (ifp->options->options & DHCPCD_XID_HWADDR &&
ifp->hwlen >= sizeof(xid))
/* The lower bits are probably more unique on the network */
memcpy(&xid, (ifp->hwaddr + ifp->hwlen) - sizeof(xid),
sizeof(xid));
else
xid = arc4random();
m->xid[0] = (xid >> 16) & 0xff;
m->xid[1] = (xid >> 8) & 0xff;
m->xid[2] = xid & 0xff;
}
#ifndef SMALL
static const struct if_sla *
dhcp6_findselfsla(struct interface *ifp, const uint8_t *iaid)
{
size_t i, j;
for (i = 0; i < ifp->options->ia_len; i++) {
if (iaid == NULL ||
memcmp(&ifp->options->ia[i].iaid, iaid,
sizeof(ifp->options->ia[i].iaid)) == 0)
{
for (j = 0; j < ifp->options->ia[i].sla_len; j++) {
if (strcmp(ifp->options->ia[i].sla[j].ifname,
ifp->name) == 0)
return &ifp->options->ia[i].sla[j];
}
}
}
return NULL;
}
static int
dhcp6_delegateaddr(struct in6_addr *addr, struct interface *ifp,
const struct ipv6_addr *prefix, const struct if_sla *sla, struct if_ia *ia)
{
struct dhcp6_state *state;
struct if_sla asla;
char sabuf[INET6_ADDRSTRLEN];
const char *sa;
state = D6_STATE(ifp);
if (state == NULL) {
ifp->if_data[IF_DATA_DHCP6] = calloc(1, sizeof(*state));
state = D6_STATE(ifp);
if (state == NULL) {
logger(ifp->ctx, LOG_ERR, "%s: %m", __func__);
return -1;
}
TAILQ_INIT(&state->addrs);
state->state = DH6S_DELEGATED;
state->reason = "DELEGATED6";
}
if (sla == NULL || sla->sla_set == 0) {
/* No SLA set, so make an assumption of
* desired SLA and prefix length. */
asla.sla = ifp->index;
asla.prefix_len = 0;
asla.sla_set = 0;
sla = &asla;
} else if (sla->sla == 0 && sla->prefix_len == 0) {
/* An SLA of 0 was set with no prefix length specified.
* This means we delegate the whole prefix. */
asla.sla = sla->sla;
asla.prefix_len = prefix->prefix_len;
asla.sla_set = 0;
sla = &asla;
} else if (sla->prefix_len == 0) {
/* An SLA was given, but prefix length was not.
* We need to work out a suitable prefix length for
* potentially more than one interface. */
asla.sla = sla->sla;
asla.prefix_len = 0;
asla.sla_set = 0;
sla = &asla;
}
if (sla->prefix_len == 0) {
uint32_t sla_max;
int bits;
if (ia->sla_max == 0) {
const struct interface *ifi;
sla_max = 0;
TAILQ_FOREACH(ifi, ifp->ctx->ifaces, next) {
if (ifi->index > sla_max)
sla_max = ifi->index;
}
} else
sla_max = ia->sla_max;
bits = fls32(sla_max);
if (prefix->prefix_len + bits > (int)UINT8_MAX)
asla.prefix_len = UINT8_MAX;
else {
asla.prefix_len = (uint8_t)(prefix->prefix_len + bits);
/* Make a 64 prefix by default, as this makes SLAAC
* possible.
* Otherwise round up to the nearest 4 bits. */
if (asla.prefix_len <= 64)
asla.prefix_len = 64;
else
asla.prefix_len =
(uint8_t)ROUNDUP4(asla.prefix_len);
}
#define BIT(n) (1UL << (n))
#define BIT_MASK(len) (BIT(len) - 1)
if (ia->sla_max == 0)
/* Work out the real sla_max from our bits used */
ia->sla_max = (uint32_t)BIT_MASK(asla.prefix_len -
prefix->prefix_len);
}
if (ipv6_userprefix(&prefix->prefix, prefix->prefix_len,
sla->sla, addr, sla->prefix_len) == -1)
{
sa = inet_ntop(AF_INET6, &prefix->prefix,
sabuf, sizeof(sabuf));
logger(ifp->ctx, LOG_ERR,
"%s: invalid prefix %s/%d + %d/%d: %m",
ifp->name, sa, prefix->prefix_len,
sla->sla, sla->prefix_len);
return -1;
}
if (prefix->prefix_exclude_len &&
IN6_ARE_ADDR_EQUAL(addr, &prefix->prefix_exclude))
{
sa = inet_ntop(AF_INET6, &prefix->prefix_exclude,
sabuf, sizeof(sabuf));
logger(ifp->ctx, LOG_ERR,
"%s: cannot delegate excluded prefix %s/%d",
ifp->name, sa, prefix->prefix_exclude_len);
return -1;
}
return sla->prefix_len;
}
#endif
static int
dhcp6_makemessage(struct interface *ifp)
{
struct dhcp6_state *state;
struct dhcp6_message *m;
struct dhcp6_option o;
uint8_t *p, *si, *unicast, IA;
size_t n, l, len, ml, hl;
uint8_t type;
uint16_t si_len, uni_len, n_options;
uint8_t *o_lenp;
struct if_options *ifo;
const struct dhcp_opt *opt, *opt2;
uint32_t u32;
const struct ipv6_addr *ap;
char hbuf[HOSTNAME_MAX_LEN + 1];
const char *hostname;
int fqdn;
struct dhcp6_ia_na ia_na;
uint16_t ia_na_len;
#ifdef AUTH
uint16_t auth_len;
#endif
state = D6_STATE(ifp);
if (state->send) {
free(state->send);
state->send = NULL;
}
ifo = ifp->options;
fqdn = ifo->fqdn;
if (fqdn == FQDN_DISABLE && ifo->options & DHCPCD_HOSTNAME) {
/* We're sending the DHCPv4 hostname option, so send FQDN as
* DHCPv6 has no FQDN option and DHCPv4 must not send
* hostname and FQDN according to RFC4702 */
fqdn = FQDN_BOTH;
}
if (fqdn != FQDN_DISABLE)
hostname = dhcp_get_hostname(hbuf, sizeof(hbuf), ifo);
else
hostname = NULL; /* appearse gcc */
/* Work out option size first */
n_options = 0;
len = 0;
si = NULL;
hl = 0; /* Appease gcc */
if (state->state != DH6S_RELEASE) {
for (l = 0, opt = ifp->ctx->dhcp6_opts;
l < ifp->ctx->dhcp6_opts_len;
l++, opt++)
{
for (n = 0, opt2 = ifo->dhcp6_override;
n < ifo->dhcp6_override_len;
n++, opt2++)
{
if (opt->option == opt2->option)
break;
}
if (n < ifo->dhcp6_override_len)
continue;
if (!(opt->type & OT_NOREQ) &&
(opt->type & OT_REQUEST ||
has_option_mask(ifo->requestmask6, opt->option)))
{
n_options++;
len += sizeof(o.len);
}
}
#ifndef SMALL
for (l = 0, opt = ifo->dhcp6_override;
l < ifo->dhcp6_override_len;
l++, opt++)
{
if (!(opt->type & OT_NOREQ) &&
(opt->type & OT_REQUEST ||
has_option_mask(ifo->requestmask6, opt->option)))
{
n_options++;
len += sizeof(o.len);
}
}
if (dhcp6_findselfsla(ifp, NULL)) {
n_options++;
len += sizeof(o.len);
}
#endif
if (len)
len += sizeof(o);
if (fqdn != FQDN_DISABLE) {
hl = encode_rfc1035(hostname, NULL);
len += sizeof(o) + 1 + hl;
}
if ((ifo->auth.options & DHCPCD_AUTH_SENDREQUIRE) !=
DHCPCD_AUTH_SENDREQUIRE)
len += sizeof(o); /* Reconfigure Accept */
}
len += sizeof(*state->send);
len += sizeof(o) + ifp->ctx->duid_len;
len += sizeof(o) + sizeof(uint16_t); /* elapsed */
if (!has_option_mask(ifo->nomask6, D6_OPTION_VENDOR_CLASS))
len += dhcp6_makevendor(NULL, ifp);
/* IA */
m = NULL;
ml = 0;
switch(state->state) {
case DH6S_REQUEST:
m = state->recv;
ml = state->recv_len;
/* FALLTHROUGH */
case DH6S_RELEASE:
/* FALLTHROUGH */
case DH6S_RENEW:
if (m == NULL) {
m = state->new;
ml = state->new_len;
}
si = dhcp6_findmoption(m, ml, D6_OPTION_SERVERID, &si_len);
if (si == NULL)
return -1;
len += sizeof(o) + si_len;
/* FALLTHROUGH */
case DH6S_REBIND:
/* FALLTHROUGH */
case DH6S_CONFIRM:
/* FALLTHROUGH */
case DH6S_DISCOVER:
if (m == NULL) {
m = state->new;
ml = state->new_len;
}
TAILQ_FOREACH(ap, &state->addrs, next) {
if (ap->prefix_vltime == 0 &&
!(ap->flags & IPV6_AF_REQUEST))
continue;
if (ap->ia_type == D6_OPTION_IA_PD) {
#ifndef SMALL
len += sizeof(o) + sizeof(struct dhcp6_pd_addr);
if (ap->prefix_exclude_len)
len += sizeof(o) + 1 +
(uint8_t)((ap->prefix_exclude_len -
ap->prefix_len - 1) / NBBY) + 1;
#endif
} else
len += sizeof(o) + sizeof(struct dhcp6_ia_addr);
}
/* FALLTHROUGH */
case DH6S_INIT:
len += ifo->ia_len * (sizeof(o) + (sizeof(u32) * 3));
IA = 1;
break;
default:
IA = 0;
}
if (state->state == DH6S_DISCOVER &&
!(ifp->ctx->options & DHCPCD_TEST) &&
has_option_mask(ifo->requestmask6, D6_OPTION_RAPID_COMMIT))
len += sizeof(o);
if (m == NULL) {
m = state->new;
ml = state->new_len;
}
unicast = NULL;
/* Depending on state, get the unicast address */
switch(state->state) {
case DH6S_INIT: /* FALLTHROUGH */
case DH6S_DISCOVER:
type = DHCP6_SOLICIT;
break;
case DH6S_REQUEST:
type = DHCP6_REQUEST;
unicast = dhcp6_findmoption(m, ml, D6_OPTION_UNICAST, &uni_len);
break;
case DH6S_CONFIRM:
type = DHCP6_CONFIRM;
break;
case DH6S_REBIND:
type = DHCP6_REBIND;
break;
case DH6S_RENEW:
type = DHCP6_RENEW;
unicast = dhcp6_findmoption(m, ml, D6_OPTION_UNICAST, &uni_len);
break;
case DH6S_INFORM:
type = DHCP6_INFORMATION_REQ;
break;
case DH6S_RELEASE:
type = DHCP6_RELEASE;
unicast = dhcp6_findmoption(m, ml, D6_OPTION_UNICAST, &uni_len);
break;
default:
errno = EINVAL;
return -1;
}
#ifdef AUTH
auth_len = 0;
if (ifo->auth.options & DHCPCD_AUTH_SEND) {
ssize_t alen = dhcp_auth_encode(&ifo->auth,
state->auth.token, NULL, 0, 6, type, NULL, 0);
if (alen != -1 && alen > UINT16_MAX) {
errno = ERANGE;
alen = -1;
}
if (alen == -1)
logger(ifp->ctx, LOG_ERR,
"%s: dhcp_auth_encode: %m", ifp->name);
else if (alen != 0) {
auth_len = (uint16_t)alen;
len += sizeof(o) + auth_len;
}
}
#endif
state->send = malloc(len);
if (state->send == NULL)
return -1;
state->send_len = len;
state->send->type = type;
/* If we found a unicast option, copy it to our state for sending */
if (unicast && uni_len == sizeof(state->unicast))
memcpy(&state->unicast, unicast, sizeof(state->unicast));
else
state->unicast = in6addr_any;
dhcp6_newxid(ifp, state->send);
#define COPYIN1(_code, _len) { \
o.code = htons((_code)); \
o.len = htons((_len)); \
memcpy(p, &o, sizeof(o)); \
p += sizeof(o); \
}
#define COPYIN(_code, _data, _len) { \
COPYIN1((_code), (_len)); \
if ((_len) != 0) { \
memcpy(p, (_data), (_len)); \
p += (_len); \
} \
}
#define NEXTLEN (p + offsetof(struct dhcp6_option, len))
p = (uint8_t *)state->send + sizeof(*state->send);
COPYIN(D6_OPTION_CLIENTID, ifp->ctx->duid, ifp->ctx->duid_len);
if (si != NULL)
COPYIN(D6_OPTION_SERVERID, si, si_len);
si_len = 0;
COPYIN(D6_OPTION_ELAPSED, &si_len, sizeof(si_len));
if (!has_option_mask(ifo->nomask6, D6_OPTION_VENDOR_CLASS))
p += dhcp6_makevendor(p, ifp);
if (state->state == DH6S_DISCOVER &&
!(ifp->ctx->options & DHCPCD_TEST) &&
has_option_mask(ifo->requestmask6, D6_OPTION_RAPID_COMMIT))
COPYIN1(D6_OPTION_RAPID_COMMIT, 0);
for (l = 0; IA && l < ifo->ia_len; l++) {
o_lenp = NEXTLEN;
ia_na_len = sizeof(ia_na);
memcpy(ia_na.iaid, ifo->ia[l].iaid, sizeof(ia_na.iaid));
ia_na.t1 = 0;
ia_na.t2 = 0;
COPYIN(ifo->ia[l].ia_type, &ia_na, sizeof(ia_na));
TAILQ_FOREACH(ap, &state->addrs, next) {
if (ap->prefix_vltime == 0 &&
!(ap->flags & IPV6_AF_REQUEST))
continue;
if (memcmp(ifo->ia[l].iaid, ap->iaid, sizeof(u32)))
continue;
if (ap->ia_type == D6_OPTION_IA_PD) {
#ifndef SMALL
struct dhcp6_pd_addr pdp;
pdp.pltime = htonl(ap->prefix_pltime);
pdp.vltime = htonl(ap->prefix_vltime);
pdp.prefix_len = ap->prefix_len;
/* pdp.prefix is not aligned, so copy it in. */
memcpy(&pdp.prefix, &ap->prefix, sizeof(pdp.prefix));
COPYIN(D6_OPTION_IAPREFIX, &pdp, sizeof(pdp));
ia_na_len = (uint16_t)
(ia_na_len + sizeof(o) + sizeof(pdp));
/* RFC6603 Section 4.2 */
if (ap->prefix_exclude_len) {
uint8_t exb[16], *ep, u8;
const uint8_t *pp;
n = (size_t)((ap->prefix_exclude_len -
ap->prefix_len - 1) / NBBY) + 1;
ep = exb;
*ep++ = (uint8_t)ap->prefix_exclude_len;
pp = ap->prefix_exclude.s6_addr;
pp += (size_t)
((ap->prefix_len - 1) / NBBY) +
(n - 1);
u8 = ap->prefix_len % NBBY;
if (u8)
n--;
while (n-- > 0)
*ep++ = *pp--;
if (u8)
*ep = (uint8_t)(*pp << u8);
n++;
COPYIN(D6_OPTION_PD_EXCLUDE, exb, n);
ia_na_len = (uint16_t)
(ia_na_len + sizeof(o) + n);
}
#endif
} else {
struct dhcp6_ia_addr ia;
ia.addr = ap->addr;
ia.pltime = htonl(ap->prefix_pltime);
ia.vltime = htonl(ap->prefix_vltime);
COPYIN(D6_OPTION_IA_ADDR, &ia, sizeof(ia));
ia_na_len = (uint16_t)
(ia_na_len + sizeof(o) + sizeof(ia));
}
}
/* Update the total option lenth. */
ia_na_len = htons(ia_na_len);
memcpy(o_lenp, &ia_na_len, sizeof(ia_na_len));
}
if (state->send->type != DHCP6_RELEASE) {
if (fqdn != FQDN_DISABLE) {
o_lenp = NEXTLEN;
COPYIN1(D6_OPTION_FQDN, 0);
if (hl == 0)
*p = D6_FQDN_NONE;
else {
switch (fqdn) {
case FQDN_BOTH:
*p = D6_FQDN_BOTH;
break;
case FQDN_PTR:
*p = D6_FQDN_PTR;
break;
default:
*p = D6_FQDN_NONE;
break;
}
}
p++;
encode_rfc1035(hostname, p);
p += hl;
o.len = htons((uint16_t)(hl + 1));
memcpy(o_lenp, &o.len, sizeof(o.len));
}
if ((ifo->auth.options & DHCPCD_AUTH_SENDREQUIRE) !=
DHCPCD_AUTH_SENDREQUIRE)
COPYIN1(D6_OPTION_RECONF_ACCEPT, 0);
if (n_options) {
o_lenp = NEXTLEN;
o.len = 0;
COPYIN1(D6_OPTION_ORO, 0);
for (l = 0, opt = ifp->ctx->dhcp6_opts;
l < ifp->ctx->dhcp6_opts_len;
l++, opt++)
{
#ifndef SMALL
for (n = 0, opt2 = ifo->dhcp6_override;
n < ifo->dhcp6_override_len;
n++, opt2++)
{
if (opt->option == opt2->option)
break;
}
if (n < ifo->dhcp6_override_len)
continue;
#endif
if (!(opt->type & OT_NOREQ) &&
(opt->type & OT_REQUEST ||
has_option_mask(ifo->requestmask6,
opt->option)))
{
o.code = htons((uint16_t)opt->option);
memcpy(p, &o.code, sizeof(o.code));
p += sizeof(o.code);
o.len = (uint16_t)
(o.len + sizeof(o.code));
}
}
#ifndef SMALL
for (l = 0, opt = ifo->dhcp6_override;
l < ifo->dhcp6_override_len;
l++, opt++)
{
if (!(opt->type & OT_NOREQ) &&
(opt->type & OT_REQUEST ||
has_option_mask(ifo->requestmask6,
opt->option)))
{
o.code = htons((uint16_t)opt->option);
memcpy(p, &o.code, sizeof(o.code));
p += sizeof(o.code);
o.len = (uint16_t)
(o.len + sizeof(o.code));
}
}
if (dhcp6_findselfsla(ifp, NULL)) {
o.code = htons(D6_OPTION_PD_EXCLUDE);
memcpy(p, &o.code, sizeof(o.code));
p += sizeof(o.code);
o.len = (uint16_t)(o.len + sizeof(o.code));
}
#endif
o.len = htons(o.len);
memcpy(o_lenp, &o.len, sizeof(o.len));
}
}
#ifdef AUTH
/* This has to be the last option */
if (ifo->auth.options & DHCPCD_AUTH_SEND && auth_len != 0) {
COPYIN1(D6_OPTION_AUTH, auth_len);
/* data will be filled at send message time */
}
#endif
return 0;
}
static const char *
dhcp6_get_op(uint16_t type)
{
const struct dhcp6_op *d;
for (d = dhcp6_ops; d->name; d++)
if (d->type == type)
return d->name;
return NULL;
}
static void
dhcp6_freedrop_addrs(struct interface *ifp, int drop,
const struct interface *ifd)
{
struct dhcp6_state *state;
state = D6_STATE(ifp);
if (state) {
ipv6_freedrop_addrs(&state->addrs, drop, ifd);
if (drop)
rt_build(ifp->ctx, AF_INET6);