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
|
/* SPDX-License-Identifier: LGPL-2.1-or-later */
/***
Copyright © 2014-2015 Intel Corporation. All rights reserved.
***/
#include <errno.h>
#include "alloc-util.h"
#include "dhcp6-internal.h"
#include "dhcp6-lease-internal.h"
#include "network-common.h"
#include "strv.h"
#include "unaligned.h"
#define IRT_DEFAULT (1 * USEC_PER_DAY)
#define IRT_MINIMUM (600 * USEC_PER_SEC)
static void dhcp6_lease_set_timestamp(sd_dhcp6_lease *lease, const triple_timestamp *timestamp) {
assert(lease);
if (timestamp && triple_timestamp_is_set(timestamp))
lease->timestamp = *timestamp;
else
triple_timestamp_now(&lease->timestamp);
}
int sd_dhcp6_lease_get_timestamp(sd_dhcp6_lease *lease, clockid_t clock, uint64_t *ret) {
assert_return(lease, -EINVAL);
assert_return(TRIPLE_TIMESTAMP_HAS_CLOCK(clock), -EOPNOTSUPP);
assert_return(clock_supported(clock), -EOPNOTSUPP);
assert_return(ret, -EINVAL);
if (!triple_timestamp_is_set(&lease->timestamp))
return -ENODATA;
*ret = triple_timestamp_by_clock(&lease->timestamp, clock);
return 0;
}
static void dhcp6_lease_set_lifetime(sd_dhcp6_lease *lease) {
usec_t t1 = USEC_INFINITY, t2 = USEC_INFINITY, min_valid_lt = USEC_INFINITY;
assert(lease);
assert(lease->ia_na || lease->ia_pd);
if (lease->ia_na) {
t1 = MIN(t1, be32_sec_to_usec(lease->ia_na->header.lifetime_t1, /* max_as_infinity = */ true));
t2 = MIN(t2, be32_sec_to_usec(lease->ia_na->header.lifetime_t2, /* max_as_infinity = */ true));
LIST_FOREACH(addresses, a, lease->ia_na->addresses)
min_valid_lt = MIN(min_valid_lt, be32_sec_to_usec(a->iaaddr.lifetime_valid, /* max_as_infinity = */ true));
}
if (lease->ia_pd) {
t1 = MIN(t1, be32_sec_to_usec(lease->ia_pd->header.lifetime_t1, /* max_as_infinity = */ true));
t2 = MIN(t2, be32_sec_to_usec(lease->ia_pd->header.lifetime_t2, /* max_as_infinity = */ true));
LIST_FOREACH(addresses, a, lease->ia_pd->addresses)
min_valid_lt = MIN(min_valid_lt, be32_sec_to_usec(a->iapdprefix.lifetime_valid, /* max_as_infinity = */ true));
}
if (t2 == 0 || t2 > min_valid_lt) {
/* If T2 is zero or longer than the minimum valid lifetime of the addresses or prefixes,
* then adjust lifetime with it. */
t1 = min_valid_lt / 2;
t2 = min_valid_lt / 10 * 8;
}
lease->lifetime_valid = min_valid_lt;
lease->lifetime_t1 = t1;
lease->lifetime_t2 = t2;
}
#define DEFINE_GET_TIME_FUNCTIONS(name, val) \
int sd_dhcp6_lease_get_##name( \
sd_dhcp6_lease *lease, \
uint64_t *ret) { \
\
assert_return(lease, -EINVAL); \
\
if (!lease->ia_na && !lease->ia_pd) \
return -ENODATA; \
\
if (ret) \
*ret = lease->val; \
return 0; \
} \
\
int sd_dhcp6_lease_get_##name##_timestamp( \
sd_dhcp6_lease *lease, \
clockid_t clock, \
uint64_t *ret) { \
\
usec_t s, t; \
int r; \
\
assert_return(lease, -EINVAL); \
\
r = sd_dhcp6_lease_get_##name(lease, &s); \
if (r < 0) \
return r; \
\
r = sd_dhcp6_lease_get_timestamp(lease, clock, &t); \
if (r < 0) \
return r; \
\
if (ret) \
*ret = time_span_to_stamp(s, t); \
return 0; \
}
DEFINE_GET_TIME_FUNCTIONS(t1, lifetime_t1);
DEFINE_GET_TIME_FUNCTIONS(t2, lifetime_t1);
DEFINE_GET_TIME_FUNCTIONS(valid_lifetime, lifetime_valid);
static void dhcp6_lease_set_server_address(sd_dhcp6_lease *lease, const struct in6_addr *server_address) {
assert(lease);
if (server_address)
lease->server_address = *server_address;
else
lease->server_address = (struct in6_addr) {};
}
int sd_dhcp6_lease_get_server_address(sd_dhcp6_lease *lease, struct in6_addr *ret) {
assert_return(lease, -EINVAL);
assert_return(ret, -EINVAL);
*ret = lease->server_address;
return 0;
}
void dhcp6_ia_clear_addresses(DHCP6IA *ia) {
assert(ia);
LIST_FOREACH(addresses, a, ia->addresses)
free(a);
ia->addresses = NULL;
}
DHCP6IA *dhcp6_ia_free(DHCP6IA *ia) {
if (!ia)
return NULL;
dhcp6_ia_clear_addresses(ia);
return mfree(ia);
}
int dhcp6_lease_set_clientid(sd_dhcp6_lease *lease, const uint8_t *id, size_t len) {
uint8_t *clientid = NULL;
assert(lease);
assert(id || len == 0);
if (len > 0) {
clientid = memdup(id, len);
if (!clientid)
return -ENOMEM;
}
free_and_replace(lease->clientid, clientid);
lease->clientid_len = len;
return 0;
}
int dhcp6_lease_get_clientid(sd_dhcp6_lease *lease, uint8_t **ret_id, size_t *ret_len) {
assert(lease);
if (!lease->clientid)
return -ENODATA;
if (ret_id)
*ret_id = lease->clientid;
if (ret_len)
*ret_len = lease->clientid_len;
return 0;
}
int dhcp6_lease_set_serverid(sd_dhcp6_lease *lease, const uint8_t *id, size_t len) {
uint8_t *serverid = NULL;
assert(lease);
assert(id || len == 0);
if (len > 0) {
serverid = memdup(id, len);
if (!serverid)
return -ENOMEM;
}
free_and_replace(lease->serverid, serverid);
lease->serverid_len = len;
return 0;
}
int dhcp6_lease_get_serverid(sd_dhcp6_lease *lease, uint8_t **ret_id, size_t *ret_len) {
assert(lease);
if (!lease->serverid)
return -ENODATA;
if (ret_id)
*ret_id = lease->serverid;
if (ret_len)
*ret_len = lease->serverid_len;
return 0;
}
int dhcp6_lease_set_preference(sd_dhcp6_lease *lease, uint8_t preference) {
assert(lease);
lease->preference = preference;
return 0;
}
int dhcp6_lease_get_preference(sd_dhcp6_lease *lease, uint8_t *ret) {
assert(lease);
assert(ret);
*ret = lease->preference;
return 0;
}
int dhcp6_lease_set_rapid_commit(sd_dhcp6_lease *lease) {
assert(lease);
lease->rapid_commit = true;
return 0;
}
int dhcp6_lease_get_rapid_commit(sd_dhcp6_lease *lease, bool *ret) {
assert(lease);
assert(ret);
*ret = lease->rapid_commit;
return 0;
}
int sd_dhcp6_lease_get_address(sd_dhcp6_lease *lease, struct in6_addr *ret) {
assert_return(lease, -EINVAL);
if (!lease->addr_iter)
return -ENODATA;
if (ret)
*ret = lease->addr_iter->iaaddr.address;
return 0;
}
int sd_dhcp6_lease_get_address_lifetime(
sd_dhcp6_lease *lease,
usec_t *ret_lifetime_preferred,
usec_t *ret_lifetime_valid) {
const struct iaaddr *a;
assert_return(lease, -EINVAL);
if (!lease->addr_iter)
return -ENODATA;
a = &lease->addr_iter->iaaddr;
if (ret_lifetime_preferred)
*ret_lifetime_preferred = be32_sec_to_usec(a->lifetime_preferred, /* max_as_infinity = */ true);
if (ret_lifetime_valid)
*ret_lifetime_valid = be32_sec_to_usec(a->lifetime_valid, /* max_as_infinity = */ true);
return 0;
}
int sd_dhcp6_lease_address_iterator_reset(sd_dhcp6_lease *lease) {
if (!lease)
return false;
lease->addr_iter = lease->ia_na ? lease->ia_na->addresses : NULL;
return !!lease->addr_iter;
}
int sd_dhcp6_lease_address_iterator_next(sd_dhcp6_lease *lease) {
if (!lease || !lease->addr_iter)
return false;
lease->addr_iter = lease->addr_iter->addresses_next;
return !!lease->addr_iter;
}
int sd_dhcp6_lease_has_address(sd_dhcp6_lease *lease) {
return lease && lease->ia_na;
}
int sd_dhcp6_lease_get_pd_prefix(
sd_dhcp6_lease *lease,
struct in6_addr *ret_prefix,
uint8_t *ret_prefix_len) {
const struct iapdprefix *a;
assert_return(lease, -EINVAL);
if (!lease->prefix_iter)
return -ENODATA;
a = &lease->prefix_iter->iapdprefix;
if (ret_prefix)
*ret_prefix = a->address;
if (ret_prefix_len)
*ret_prefix_len = a->prefixlen;
return 0;
}
int sd_dhcp6_lease_get_pd_lifetime(
sd_dhcp6_lease *lease,
uint64_t *ret_lifetime_preferred,
uint64_t *ret_lifetime_valid) {
const struct iapdprefix *a;
assert_return(lease, -EINVAL);
if (!lease->prefix_iter)
return -ENODATA;
a = &lease->prefix_iter->iapdprefix;
if (ret_lifetime_preferred)
*ret_lifetime_preferred = be32_sec_to_usec(a->lifetime_preferred, /* max_as_infinity = */ true);
if (ret_lifetime_valid)
*ret_lifetime_valid = be32_sec_to_usec(a->lifetime_valid, /* max_as_infinity = */ true);
return 0;
}
int sd_dhcp6_lease_pd_iterator_reset(sd_dhcp6_lease *lease) {
if (!lease)
return false;
lease->prefix_iter = lease->ia_pd ? lease->ia_pd->addresses : NULL;
return !!lease->prefix_iter;
}
int sd_dhcp6_lease_pd_iterator_next(sd_dhcp6_lease *lease) {
if (!lease || !lease->prefix_iter)
return false;
lease->prefix_iter = lease->prefix_iter->addresses_next;
return !!lease->prefix_iter;
}
#define DEFINE_GET_TIMESTAMP2(name) \
int sd_dhcp6_lease_get_##name##_lifetime_timestamp( \
sd_dhcp6_lease *lease, \
clockid_t clock, \
uint64_t *ret_lifetime_preferred, \
uint64_t *ret_lifetime_valid) { \
\
usec_t t, p, v; \
int r; \
\
assert_return(lease, -EINVAL); \
\
r = sd_dhcp6_lease_get_##name##_lifetime( \
lease, \
ret_lifetime_preferred ? &p : NULL, \
ret_lifetime_valid ? &v : NULL); \
if (r < 0) \
return r; \
\
r = sd_dhcp6_lease_get_timestamp(lease, clock, &t); \
if (r < 0) \
return r; \
\
if (ret_lifetime_preferred) \
*ret_lifetime_preferred = time_span_to_stamp(p, t); \
if (ret_lifetime_valid) \
*ret_lifetime_valid = time_span_to_stamp(v, t); \
return 0; \
}
DEFINE_GET_TIMESTAMP2(address);
DEFINE_GET_TIMESTAMP2(pd);
int sd_dhcp6_lease_has_pd_prefix(sd_dhcp6_lease *lease) {
return lease && lease->ia_pd;
}
int dhcp6_lease_add_dns(sd_dhcp6_lease *lease, const uint8_t *optval, size_t optlen) {
assert(lease);
assert(optval || optlen == 0);
if (optlen == 0)
return 0;
return dhcp6_option_parse_addresses(optval, optlen, &lease->dns, &lease->dns_count);
}
int sd_dhcp6_lease_get_dns(sd_dhcp6_lease *lease, const struct in6_addr **ret) {
assert_return(lease, -EINVAL);
if (!lease->dns)
return -ENODATA;
if (ret)
*ret = lease->dns;
return lease->dns_count;
}
int dhcp6_lease_add_domains(sd_dhcp6_lease *lease, const uint8_t *optval, size_t optlen) {
_cleanup_strv_free_ char **domains = NULL;
int r;
assert(lease);
assert(optval || optlen == 0);
if (optlen == 0)
return 0;
r = dhcp6_option_parse_domainname_list(optval, optlen, &domains);
if (r < 0)
return r;
return strv_extend_strv(&lease->domains, domains, true);
}
int sd_dhcp6_lease_get_domains(sd_dhcp6_lease *lease, char ***ret) {
assert_return(lease, -EINVAL);
assert_return(ret, -EINVAL);
if (!lease->domains)
return -ENODATA;
*ret = lease->domains;
return strv_length(lease->domains);
}
int dhcp6_lease_add_ntp(sd_dhcp6_lease *lease, const uint8_t *optval, size_t optlen) {
int r;
assert(lease);
assert(optval || optlen == 0);
for (size_t offset = 0; offset < optlen;) {
const uint8_t *subval;
size_t sublen;
uint16_t subopt;
r = dhcp6_option_parse(optval, optlen, &offset, &subopt, &sublen, &subval);
if (r < 0)
return r;
switch (subopt) {
case DHCP6_NTP_SUBOPTION_SRV_ADDR:
case DHCP6_NTP_SUBOPTION_MC_ADDR:
if (sublen != 16)
return -EINVAL;
r = dhcp6_option_parse_addresses(subval, sublen, &lease->ntp, &lease->ntp_count);
if (r < 0)
return r;
break;
case DHCP6_NTP_SUBOPTION_SRV_FQDN: {
_cleanup_free_ char *server = NULL;
r = dhcp6_option_parse_domainname(subval, sublen, &server);
if (r < 0)
return r;
if (strv_contains(lease->ntp_fqdn, server))
continue;
r = strv_consume(&lease->ntp_fqdn, TAKE_PTR(server));
if (r < 0)
return r;
break;
}}
}
return 0;
}
int dhcp6_lease_add_sntp(sd_dhcp6_lease *lease, const uint8_t *optval, size_t optlen) {
assert(lease);
assert(optval || optlen == 0);
if (optlen == 0)
return 0;
/* SNTP option is defined in RFC4075, and deprecated by RFC5908. */
return dhcp6_option_parse_addresses(optval, optlen, &lease->sntp, &lease->sntp_count);
}
int sd_dhcp6_lease_get_ntp_addrs(sd_dhcp6_lease *lease, const struct in6_addr **ret) {
assert_return(lease, -EINVAL);
if (lease->ntp) {
if (ret)
*ret = lease->ntp;
return lease->ntp_count;
}
if (lease->sntp && !lease->ntp_fqdn) {
/* Fallback to the deprecated SNTP option. */
if (ret)
*ret = lease->sntp;
return lease->sntp_count;
}
return -ENODATA;
}
int sd_dhcp6_lease_get_ntp_fqdn(sd_dhcp6_lease *lease, char ***ret) {
assert_return(lease, -EINVAL);
if (!lease->ntp_fqdn)
return -ENODATA;
if (ret)
*ret = lease->ntp_fqdn;
return strv_length(lease->ntp_fqdn);
}
int dhcp6_lease_set_fqdn(sd_dhcp6_lease *lease, const uint8_t *optval, size_t optlen) {
char *fqdn;
int r;
assert(lease);
assert(optval || optlen == 0);
if (optlen == 0)
return 0;
if (optlen < 2)
return -ENODATA;
/* Ignore the flags field, it doesn't carry any useful
information for clients. */
r = dhcp6_option_parse_domainname(optval + 1, optlen - 1, &fqdn);
if (r < 0)
return r;
return free_and_replace(lease->fqdn, fqdn);
}
int sd_dhcp6_lease_get_fqdn(sd_dhcp6_lease *lease, const char **ret) {
assert_return(lease, -EINVAL);
assert_return(ret, -EINVAL);
if (!lease->fqdn)
return -ENODATA;
*ret = lease->fqdn;
return 0;
}
int dhcp6_lease_set_captive_portal(sd_dhcp6_lease *lease, const uint8_t *optval, size_t optlen) {
_cleanup_free_ char *uri = NULL;
int r;
assert(lease);
assert(optval || optlen == 0);
r = dhcp6_option_parse_string(optval, optlen, &uri);
if (r < 0)
return r;
if (uri && !in_charset(uri, URI_VALID))
return -EINVAL;
return free_and_replace(lease->captive_portal, uri);
}
int sd_dhcp6_lease_get_captive_portal(sd_dhcp6_lease *lease, const char **ret) {
assert_return(lease, -EINVAL);
assert_return(ret, -EINVAL);
if (!lease->captive_portal)
return -ENODATA;
*ret = lease->captive_portal;
return 0;
}
int sd_dhcp6_lease_get_vendor_options(sd_dhcp6_lease *lease, sd_dhcp6_option ***ret) {
int r;
assert_return(lease, -EINVAL);
if (set_isempty(lease->vendor_options))
return -ENODATA;
if (ret) {
if (!lease->sorted_vendor_options) {
r = set_dump_sorted(lease->vendor_options, (void***) &lease->sorted_vendor_options, NULL);
if (r < 0)
return r;
}
*ret = lease->sorted_vendor_options;
}
return set_size(lease->vendor_options);
}
static int dhcp6_lease_insert_vendor_option(
sd_dhcp6_lease *lease,
uint16_t option_code,
const void *data,
size_t len,
uint32_t enterprise_id) {
_cleanup_(sd_dhcp6_option_unrefp) sd_dhcp6_option *option = NULL;
assert(lease);
option = new(sd_dhcp6_option, 1);
if (!option)
return -ENOMEM;
*option = (sd_dhcp6_option) {
.n_ref = 1,
.enterprise_identifier = enterprise_id,
.option = option_code,
.length = len,
};
option->data = memdup_suffix0(data, len);
if (!option->data)
return -ENOMEM;
return set_ensure_consume(&lease->vendor_options, &dhcp6_option_hash_ops, TAKE_PTR(option));
}
static int dhcp6_lease_add_vendor_option(sd_dhcp6_lease *lease, const uint8_t *optval, size_t optlen) {
int r;
uint32_t enterprise_id;
assert(lease);
assert(optval || optlen == 0);
if (optlen < sizeof(be32_t))
return -EBADMSG;
enterprise_id = unaligned_read_be32(optval);
for (size_t offset = 4; offset < optlen;) {
const uint8_t *subval;
size_t sublen;
uint16_t subopt;
r = dhcp6_option_parse(optval, optlen, &offset, &subopt, &sublen, &subval);
if (r < 0)
return r;
r = dhcp6_lease_insert_vendor_option(lease, subopt, subval, sublen, enterprise_id);
if (r < 0)
return r;
}
return 0;
}
static int dhcp6_lease_parse_message(
sd_dhcp6_client *client,
sd_dhcp6_lease *lease,
const DHCP6Message *message,
size_t len) {
usec_t irt = IRT_DEFAULT;
int r;
assert(client);
assert(lease);
assert(message);
assert(len >= sizeof(DHCP6Message));
len -= sizeof(DHCP6Message);
for (size_t offset = 0; offset < len;) {
uint16_t optcode;
size_t optlen;
const uint8_t *optval;
if (len - offset < offsetof(DHCP6Option, data)) {
log_dhcp6_client(client, "Ignoring %zu invalid byte(s) at the end of the packet", len - offset);
break;
}
r = dhcp6_option_parse(message->options, len, &offset, &optcode, &optlen, &optval);
if (r < 0)
return log_dhcp6_client_errno(client, r,
"Failed to parse option header at offset %zu of total length %zu: %m",
offset, len);
switch (optcode) {
case SD_DHCP6_OPTION_CLIENTID:
if (dhcp6_lease_get_clientid(lease, NULL, NULL) >= 0)
return log_dhcp6_client_errno(client, SYNTHETIC_ERRNO(EINVAL), "%s contains multiple client IDs",
dhcp6_message_type_to_string(message->type));
r = dhcp6_lease_set_clientid(lease, optval, optlen);
if (r < 0)
return log_dhcp6_client_errno(client, r, "Failed to set client ID: %m");
break;
case SD_DHCP6_OPTION_SERVERID:
if (dhcp6_lease_get_serverid(lease, NULL, NULL) >= 0)
return log_dhcp6_client_errno(client, SYNTHETIC_ERRNO(EINVAL), "%s contains multiple server IDs",
dhcp6_message_type_to_string(message->type));
r = dhcp6_lease_set_serverid(lease, optval, optlen);
if (r < 0)
return log_dhcp6_client_errno(client, r, "Failed to set server ID: %m");
break;
case SD_DHCP6_OPTION_PREFERENCE:
if (optlen != 1)
return log_dhcp6_client_errno(client, SYNTHETIC_ERRNO(EINVAL), "Received invalid length for preference.");
r = dhcp6_lease_set_preference(lease, optval[0]);
if (r < 0)
return log_dhcp6_client_errno(client, r, "Failed to set preference: %m");
break;
case SD_DHCP6_OPTION_STATUS_CODE: {
_cleanup_free_ char *msg = NULL;
r = dhcp6_option_parse_status(optval, optlen, &msg);
if (r < 0)
return log_dhcp6_client_errno(client, r, "Failed to parse status code: %m");
if (r > 0)
return log_dhcp6_client_errno(client, dhcp6_message_status_to_errno(r),
"Received %s message with non-zero status%s%s",
dhcp6_message_type_to_string(message->type),
isempty(msg) ? "." : ": ", strempty(msg));
break;
}
case SD_DHCP6_OPTION_IA_NA: {
_cleanup_(dhcp6_ia_freep) DHCP6IA *ia = NULL;
if (client->state == DHCP6_STATE_INFORMATION_REQUEST) {
log_dhcp6_client(client, "Ignoring IA NA option in information requesting mode.");
break;
}
r = dhcp6_option_parse_ia(client, client->ia_na.header.id, optcode, optlen, optval, &ia);
if (r == -ENOMEM)
return log_oom_debug();
if (r < 0) {
log_dhcp6_client_errno(client, r, "Failed to parse IA_NA option, ignoring: %m");
continue;
}
if (lease->ia_na) {
log_dhcp6_client(client, "Received duplicate matching IA_NA option, ignoring.");
continue;
}
dhcp6_ia_free(lease->ia_na);
lease->ia_na = TAKE_PTR(ia);
break;
}
case SD_DHCP6_OPTION_IA_PD: {
_cleanup_(dhcp6_ia_freep) DHCP6IA *ia = NULL;
if (client->state == DHCP6_STATE_INFORMATION_REQUEST) {
log_dhcp6_client(client, "Ignoring IA PD option in information requesting mode.");
break;
}
r = dhcp6_option_parse_ia(client, client->ia_pd.header.id, optcode, optlen, optval, &ia);
if (r == -ENOMEM)
return log_oom_debug();
if (r < 0) {
log_dhcp6_client_errno(client, r, "Failed to parse IA_PD option, ignoring: %m");
continue;
}
if (lease->ia_pd) {
log_dhcp6_client(client, "Received duplicate matching IA_PD option, ignoring.");
continue;
}
dhcp6_ia_free(lease->ia_pd);
lease->ia_pd = TAKE_PTR(ia);
break;
}
case SD_DHCP6_OPTION_RAPID_COMMIT:
if (optlen != 0)
log_dhcp6_client(client, "Received rapid commit option with an invalid length (%zu), ignoring.", optlen);
r = dhcp6_lease_set_rapid_commit(lease);
if (r < 0)
return log_dhcp6_client_errno(client, r, "Failed to set rapid commit flag: %m");
break;
case SD_DHCP6_OPTION_DNS_SERVER:
r = dhcp6_lease_add_dns(lease, optval, optlen);
if (r < 0)
log_dhcp6_client_errno(client, r, "Failed to parse DNS server option, ignoring: %m");
break;
case SD_DHCP6_OPTION_DOMAIN:
r = dhcp6_lease_add_domains(lease, optval, optlen);
if (r < 0)
log_dhcp6_client_errno(client, r, "Failed to parse domain list option, ignoring: %m");
break;
case SD_DHCP6_OPTION_NTP_SERVER:
r = dhcp6_lease_add_ntp(lease, optval, optlen);
if (r < 0)
log_dhcp6_client_errno(client, r, "Failed to parse NTP server option, ignoring: %m");
break;
case SD_DHCP6_OPTION_SNTP_SERVER:
r = dhcp6_lease_add_sntp(lease, optval, optlen);
if (r < 0)
log_dhcp6_client_errno(client, r, "Failed to parse SNTP server option, ignoring: %m");
break;
case SD_DHCP6_OPTION_CAPTIVE_PORTAL:
r = dhcp6_lease_set_captive_portal(lease, optval, optlen);
if (r < 0)
log_dhcp6_client_errno(client, r, "Failed to parse captive portal option, ignoring: %m");
break;
case SD_DHCP6_OPTION_CLIENT_FQDN:
r = dhcp6_lease_set_fqdn(lease, optval, optlen);
if (r < 0)
log_dhcp6_client_errno(client, r, "Failed to parse FQDN option, ignoring: %m");
break;
case SD_DHCP6_OPTION_INFORMATION_REFRESH_TIME:
if (optlen != 4)
return log_dhcp6_client_errno(client, SYNTHETIC_ERRNO(EINVAL),
"Received information refresh time option with an invalid length (%zu).", optlen);
irt = unaligned_be32_sec_to_usec(optval, /* max_as_infinity = */ false);
break;
case SD_DHCP6_OPTION_VENDOR_OPTS:
r = dhcp6_lease_add_vendor_option(lease, optval, optlen);
if (r < 0)
log_dhcp6_client_errno(client, r, "Failed to parse vendor option, ignoring: %m");
break;
}
}
uint8_t *clientid;
size_t clientid_len;
if (dhcp6_lease_get_clientid(lease, &clientid, &clientid_len) < 0)
return log_dhcp6_client_errno(client, SYNTHETIC_ERRNO(EINVAL),
"%s message does not contain client ID. Ignoring.",
dhcp6_message_type_to_string(message->type));
if (memcmp_nn(clientid, clientid_len, &client->duid, client->duid_len) != 0)
return log_dhcp6_client_errno(client, SYNTHETIC_ERRNO(EINVAL),
"The client ID in %s message does not match. Ignoring.",
dhcp6_message_type_to_string(message->type));
if (client->state == DHCP6_STATE_INFORMATION_REQUEST) {
client->information_refresh_time_usec = MAX(irt, IRT_MINIMUM);
log_dhcp6_client(client, "New information request will be refused in %s.",
FORMAT_TIMESPAN(client->information_refresh_time_usec, USEC_PER_SEC));
} else {
r = dhcp6_lease_get_serverid(lease, NULL, NULL);
if (r < 0)
return log_dhcp6_client_errno(client, r, "%s has no server id",
dhcp6_message_type_to_string(message->type));
if (!lease->ia_na && !lease->ia_pd)
return log_dhcp6_client_errno(client, SYNTHETIC_ERRNO(EINVAL),
"No IA_PD prefix or IA_NA address received. Ignoring.");
dhcp6_lease_set_lifetime(lease);
}
return 0;
}
static sd_dhcp6_lease *dhcp6_lease_free(sd_dhcp6_lease *lease) {
if (!lease)
return NULL;
set_free(lease->vendor_options);
free(lease->sorted_vendor_options);
free(lease->clientid);
free(lease->serverid);
dhcp6_ia_free(lease->ia_na);
dhcp6_ia_free(lease->ia_pd);
free(lease->dns);
free(lease->fqdn);
free(lease->captive_portal);
strv_free(lease->domains);
free(lease->ntp);
strv_free(lease->ntp_fqdn);
free(lease->sntp);
return mfree(lease);
}
DEFINE_TRIVIAL_REF_UNREF_FUNC(sd_dhcp6_lease, sd_dhcp6_lease, dhcp6_lease_free);
int dhcp6_lease_new(sd_dhcp6_lease **ret) {
sd_dhcp6_lease *lease;
assert(ret);
lease = new(sd_dhcp6_lease, 1);
if (!lease)
return -ENOMEM;
*lease = (sd_dhcp6_lease) {
.n_ref = 1,
};
*ret = lease;
return 0;
}
int dhcp6_lease_new_from_message(
sd_dhcp6_client *client,
const DHCP6Message *message,
size_t len,
const triple_timestamp *timestamp,
const struct in6_addr *server_address,
sd_dhcp6_lease **ret) {
_cleanup_(sd_dhcp6_lease_unrefp) sd_dhcp6_lease *lease = NULL;
int r;
assert(client);
assert(message);
assert(len >= sizeof(DHCP6Message));
assert(ret);
r = dhcp6_lease_new(&lease);
if (r < 0)
return r;
dhcp6_lease_set_timestamp(lease, timestamp);
dhcp6_lease_set_server_address(lease, server_address);
r = dhcp6_lease_parse_message(client, lease, message, len);
if (r < 0)
return r;
*ret = TAKE_PTR(lease);
return 0;
}
|