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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
|
/* SPDX-License-Identifier: LGPL-2.1-or-later */
#include <netinet/in.h>
#include <linux/if_arp.h>
#include "bus-error.h"
#include "bus-locator.h"
#include "dhcp-option.h"
#include "dhcp6-internal.h"
#include "escape.h"
#include "hexdecoct.h"
#include "in-addr-prefix-util.h"
#include "networkd-dhcp-common.h"
#include "networkd-link.h"
#include "networkd-manager.h"
#include "networkd-network.h"
#include "networkd-route-util.h"
#include "parse-util.h"
#include "socket-util.h"
#include "string-table.h"
#include "strv.h"
#include "vrf.h"
static uint32_t link_get_vrf_table(Link *link) {
assert(link);
assert(link->network);
return link->network->vrf ? VRF(link->network->vrf)->table : RT_TABLE_MAIN;
}
uint32_t link_get_dhcp4_route_table(Link *link) {
assert(link);
assert(link->network);
/* When the interface is part of an VRF use the VRFs routing table, unless
* another table is explicitly specified. */
if (link->network->dhcp_route_table_set)
return link->network->dhcp_route_table;
return link_get_vrf_table(link);
}
uint32_t link_get_ndisc_route_table(Link *link) {
assert(link);
assert(link->network);
if (link->network->ndisc_route_table_set)
return link->network->ndisc_route_table;
return link_get_vrf_table(link);
}
bool link_dhcp_enabled(Link *link, int family) {
assert(link);
assert(IN_SET(family, AF_INET, AF_INET6));
/* Currently, sd-dhcp-client supports only ethernet and infiniband. */
if (family == AF_INET && !IN_SET(link->iftype, ARPHRD_ETHER, ARPHRD_INFINIBAND))
return false;
if (family == AF_INET6 && !socket_ipv6_is_supported())
return false;
if (link->flags & IFF_LOOPBACK)
return false;
if (link->iftype == ARPHRD_CAN)
return false;
if (!link->network)
return false;
return link->network->dhcp & (family == AF_INET ? ADDRESS_FAMILY_IPV4 : ADDRESS_FAMILY_IPV6);
}
void network_adjust_dhcp(Network *network) {
assert(network);
assert(network->dhcp >= 0);
if (network->dhcp == ADDRESS_FAMILY_NO)
return;
/* Bonding slave does not support addressing. */
if (network->bond) {
log_warning("%s: Cannot enable DHCP= when Bond= is specified, disabling DHCP=.",
network->filename);
network->dhcp = ADDRESS_FAMILY_NO;
return;
}
if (!FLAGS_SET(network->link_local, ADDRESS_FAMILY_IPV6) &&
FLAGS_SET(network->dhcp, ADDRESS_FAMILY_IPV6)) {
log_warning("%s: DHCPv6 client is enabled but IPv6 link-local addressing is disabled. "
"Disabling DHCPv6 client.", network->filename);
SET_FLAG(network->dhcp, ADDRESS_FAMILY_IPV6, false);
}
network_adjust_dhcp4(network);
}
static bool duid_needs_product_uuid(const DUID *duid) {
assert(duid);
return duid->type == DUID_TYPE_UUID && duid->raw_data_len == 0;
}
static const struct DUID fallback_duid = { .type = DUID_TYPE_EN };
const DUID *link_get_duid(Link *link, int family) {
const DUID *duid;
assert(link);
assert(IN_SET(family, AF_INET, AF_INET6));
if (link->network) {
duid = family == AF_INET ? &link->network->dhcp_duid : &link->network->dhcp6_duid;
if (duid->type != _DUID_TYPE_INVALID) {
if (duid_needs_product_uuid(duid))
return &link->manager->duid_product_uuid;
else
return duid;
}
}
duid = family == AF_INET ? &link->manager->dhcp_duid : &link->manager->dhcp6_duid;
if (link->hw_addr.length == 0 && IN_SET(duid->type, DUID_TYPE_LLT, DUID_TYPE_LL))
/* Fallback to DUID that works without MAC address.
* This is useful for tunnel devices without MAC address. */
return &fallback_duid;
return duid;
}
static int get_product_uuid_handler(sd_bus_message *m, void *userdata, sd_bus_error *ret_error) {
Manager *manager = ASSERT_PTR(userdata);
const sd_bus_error *e;
const void *a;
size_t sz;
int r;
assert(m);
/* To avoid calling GetProductUUID() bus method so frequently, set the flag below
* even if the method fails. */
manager->has_product_uuid = true;
e = sd_bus_message_get_error(m);
if (e) {
r = sd_bus_error_get_errno(e);
log_warning_errno(r, "Could not get product UUID. Falling back to use machine-app-specific ID as DUID-UUID: %s",
bus_error_message(e, r));
return 0;
}
r = sd_bus_message_read_array(m, 'y', &a, &sz);
if (r < 0) {
log_warning_errno(r, "Failed to get product UUID. Falling back to use machine-app-specific ID as DUID-UUID: %m");
return 0;
}
if (sz != sizeof(sd_id128_t)) {
log_warning("Invalid product UUID. Falling back to use machine-app-specific ID as DUID-UUID.");
return 0;
}
log_debug("Successfully obtained product UUID");
memcpy(&manager->duid_product_uuid.raw_data, a, sz);
manager->duid_product_uuid.raw_data_len = sz;
return 0;
}
int manager_request_product_uuid(Manager *m) {
static bool bus_method_is_called = false;
int r;
assert(m);
if (bus_method_is_called)
return 0;
if (sd_bus_is_ready(m->bus) <= 0 && !m->product_uuid_requested) {
log_debug("Not connected to system bus, requesting product UUID later.");
m->product_uuid_requested = true;
return 0;
}
m->product_uuid_requested = false;
r = bus_call_method_async(
m->bus,
NULL,
bus_hostname,
"GetProductUUID",
get_product_uuid_handler,
m,
"b",
false);
if (r < 0)
return log_warning_errno(r, "Failed to get product UUID: %m");
log_debug("Requesting product UUID.");
bus_method_is_called = true;
return 0;
}
int dhcp_configure_duid(Link *link, const DUID *duid) {
Manager *m;
int r;
assert(link);
assert(link->manager);
assert(duid);
m = link->manager;
if (!duid_needs_product_uuid(duid))
return 1;
if (m->has_product_uuid)
return 1;
r = manager_request_product_uuid(m);
if (r < 0) {
log_link_warning_errno(link, r,
"Failed to get product UUID. Falling back to use machine-app-specific ID as DUID-UUID: %m");
m->has_product_uuid = true; /* Do not request UUID again on failure. */
return 1;
}
return 0;
}
bool address_is_filtered(int family, const union in_addr_union *address, uint8_t prefixlen, Set *allow_list, Set *deny_list) {
struct in_addr_prefix *p;
assert(IN_SET(family, AF_INET, AF_INET6));
assert(address);
if (allow_list) {
SET_FOREACH(p, allow_list)
if (p->family == family &&
p->prefixlen <= prefixlen &&
in_addr_prefix_covers(family, &p->address, p->prefixlen, address) > 0)
return false;
return true;
}
SET_FOREACH(p, deny_list)
if (p->family == family &&
in_addr_prefix_intersect(family, &p->address, p->prefixlen, address, prefixlen) > 0)
return true;
return false;
}
int link_get_captive_portal(Link *link, const char **ret) {
const char *dhcp4_cp = NULL, *dhcp6_cp = NULL, *ndisc_cp = NULL;
int r;
assert(link);
if (!link->network) {
*ret = NULL;
return 0;
}
if (link->network->dhcp_use_captive_portal && link->dhcp_lease) {
r = sd_dhcp_lease_get_captive_portal(link->dhcp_lease, &dhcp4_cp);
if (r < 0 && r != -ENODATA)
return r;
}
if (link->network->dhcp6_use_captive_portal && link->dhcp6_lease) {
r = sd_dhcp6_lease_get_captive_portal(link->dhcp6_lease, &dhcp6_cp);
if (r < 0 && r != -ENODATA)
return r;
}
if (link->network->ndisc_use_captive_portal) {
NDiscCaptivePortal *cp;
usec_t usec = 0;
/* Use the captive portal with the longest lifetime. */
SET_FOREACH(cp, link->ndisc_captive_portals) {
if (cp->lifetime_usec < usec)
continue;
ndisc_cp = cp->captive_portal;
usec = cp->lifetime_usec;
}
if (set_size(link->ndisc_captive_portals) > 1)
log_link_debug(link, "Multiple captive portals obtained by IPv6RA, using \"%s\" and ignoring others.",
ndisc_cp);
}
if (dhcp4_cp) {
if (dhcp6_cp && !streq(dhcp4_cp, dhcp6_cp))
log_link_debug(link, "DHCPv6 captive portal (%s) does not match DHCPv4 (%s), ignoring DHCPv6 captive portal.",
dhcp6_cp, dhcp4_cp);
if (ndisc_cp && !streq(dhcp4_cp, ndisc_cp))
log_link_debug(link, "IPv6RA captive portal (%s) does not match DHCPv4 (%s), ignoring IPv6RA captive portal.",
ndisc_cp, dhcp4_cp);
*ret = dhcp4_cp;
return 1;
}
if (dhcp6_cp) {
if (ndisc_cp && !streq(dhcp6_cp, ndisc_cp))
log_link_debug(link, "IPv6RA captive portal (%s) does not match DHCPv6 (%s), ignoring IPv6RA captive portal.",
ndisc_cp, dhcp6_cp);
*ret = dhcp6_cp;
return 1;
}
*ret = ndisc_cp;
return !!ndisc_cp;
}
int config_parse_dhcp(
const char* unit,
const char *filename,
unsigned line,
const char *section,
unsigned section_line,
const char *lvalue,
int ltype,
const char *rvalue,
void *data,
void *userdata) {
AddressFamily *dhcp = data, s;
assert(filename);
assert(lvalue);
assert(rvalue);
assert(data);
/* Note that this is mostly like
* config_parse_address_family(), except that it
* understands some old names for the enum values */
s = address_family_from_string(rvalue);
if (s < 0) {
/* Previously, we had a slightly different enum here,
* support its values for compatibility. */
s = dhcp_deprecated_address_family_from_string(rvalue);
if (s < 0) {
log_syntax(unit, LOG_WARNING, filename, line, s,
"Failed to parse DHCP option, ignoring: %s", rvalue);
return 0;
}
log_syntax(unit, LOG_WARNING, filename, line, 0,
"DHCP=%s is deprecated, please use DHCP=%s instead.",
rvalue, address_family_to_string(s));
}
*dhcp = s;
return 0;
}
int config_parse_dhcp_route_metric(
const char* unit,
const char *filename,
unsigned line,
const char *section,
unsigned section_line,
const char *lvalue,
int ltype,
const char *rvalue,
void *data,
void *userdata) {
Network *network = userdata;
uint32_t metric;
int r;
assert(filename);
assert(lvalue);
assert(IN_SET(ltype, AF_UNSPEC, AF_INET));
assert(rvalue);
assert(data);
r = safe_atou32(rvalue, &metric);
if (r < 0) {
log_syntax(unit, LOG_WARNING, filename, line, r,
"Failed to parse RouteMetric=%s, ignoring assignment: %m", rvalue);
return 0;
}
switch (ltype) {
case AF_INET:
network->dhcp_route_metric = metric;
network->dhcp_route_metric_set = true;
break;
case AF_UNSPEC:
/* For backward compatibility. */
if (!network->dhcp_route_metric_set)
network->dhcp_route_metric = metric;
if (!network->ndisc_route_metric_set) {
network->ndisc_route_metric_high = metric;
network->ndisc_route_metric_medium = metric;
network->ndisc_route_metric_low = metric;
}
break;
default:
assert_not_reached();
}
return 0;
}
int config_parse_ndisc_route_metric(
const char *unit,
const char *filename,
unsigned line,
const char *section,
unsigned section_line,
const char *lvalue,
int ltype,
const char *rvalue,
void *data,
void *userdata) {
Network *network = ASSERT_PTR(userdata);
uint32_t metric_high, metric_medium, metric_low;
int r, s, t;
assert(filename);
assert(rvalue);
if (safe_atou32(rvalue, &metric_low) >= 0)
metric_high = metric_medium = metric_low;
else {
_cleanup_free_ char *high = NULL, *medium = NULL, *low = NULL;
const char *p = rvalue;
r = extract_many_words(&p, ":", EXTRACT_DONT_COALESCE_SEPARATORS, &high, &medium, &low);
if (r == -ENOMEM)
return log_oom();
if (r != 3 || !isempty(p)) {
log_syntax(unit, LOG_WARNING, filename, line, r < 0 ? r : 0,
"Failed to parse RouteTable=%s, ignoring assignment: %m", rvalue);
return 0;
}
r = safe_atou32(high, &metric_high);
s = safe_atou32(medium, &metric_medium);
t = safe_atou32(low, &metric_low);
if (r < 0 || s < 0 || t < 0) {
log_syntax(unit, LOG_WARNING, filename, line, r < 0 ? r : s < 0 ? s : t,
"Failed to parse RouteTable=%s, ignoring assignment: %m", rvalue);
return 0;
}
if (metric_high >= metric_medium || metric_medium >= metric_low) {
log_syntax(unit, LOG_WARNING, filename, line, 0,
"Invalid RouteTable=%s, ignoring assignment: %m", rvalue);
return 0;
}
}
network->ndisc_route_metric_high = metric_high;
network->ndisc_route_metric_medium = metric_medium;
network->ndisc_route_metric_low = metric_low;
network->ndisc_route_metric_set = true;
return 0;
}
int config_parse_dhcp_send_hostname(
const char* unit,
const char *filename,
unsigned line,
const char *section,
unsigned section_line,
const char *lvalue,
int ltype,
const char *rvalue,
void *data,
void *userdata) {
Network *network = userdata;
int r;
assert(filename);
assert(lvalue);
assert(IN_SET(ltype, AF_UNSPEC, AF_INET, AF_INET6));
assert(rvalue);
assert(data);
r = parse_boolean(rvalue);
if (r < 0) {
log_syntax(unit, LOG_WARNING, filename, line, r,
"Failed to parse SendHostname=%s, ignoring assignment: %m", rvalue);
return 0;
}
switch (ltype) {
case AF_INET:
network->dhcp_send_hostname = r;
network->dhcp_send_hostname_set = true;
break;
case AF_INET6:
network->dhcp6_send_hostname = r;
network->dhcp6_send_hostname_set = true;
break;
case AF_UNSPEC:
/* For backward compatibility. */
if (!network->dhcp_send_hostname_set)
network->dhcp_send_hostname = r;
if (!network->dhcp6_send_hostname_set)
network->dhcp6_send_hostname = r;
break;
default:
assert_not_reached();
}
return 0;
}
int config_parse_dhcp_or_ra_route_table(
const char *unit,
const char *filename,
unsigned line,
const char *section,
unsigned section_line,
const char *lvalue,
int ltype,
const char *rvalue,
void *data,
void *userdata) {
Network *network = ASSERT_PTR(userdata);
uint32_t rt;
int r;
assert(filename);
assert(lvalue);
assert(IN_SET(ltype, AF_INET, AF_INET6));
assert(rvalue);
r = manager_get_route_table_from_string(network->manager, rvalue, &rt);
if (r < 0) {
log_syntax(unit, LOG_WARNING, filename, line, r,
"Failed to parse RouteTable=%s, ignoring assignment: %m", rvalue);
return 0;
}
switch (ltype) {
case AF_INET:
network->dhcp_route_table = rt;
network->dhcp_route_table_set = true;
break;
case AF_INET6:
network->ndisc_route_table = rt;
network->ndisc_route_table_set = true;
break;
default:
assert_not_reached();
}
return 0;
}
int config_parse_iaid(
const char *unit,
const char *filename,
unsigned line,
const char *section,
unsigned section_line,
const char *lvalue,
int ltype,
const char *rvalue,
void *data,
void *userdata) {
Network *network = ASSERT_PTR(userdata);
uint32_t iaid;
int r;
assert(filename);
assert(lvalue);
assert(rvalue);
assert(IN_SET(ltype, AF_INET, AF_INET6));
r = safe_atou32(rvalue, &iaid);
if (r < 0) {
log_syntax(unit, LOG_WARNING, filename, line, r,
"Unable to read IAID, ignoring assignment: %s", rvalue);
return 0;
}
if (ltype == AF_INET) {
network->dhcp_iaid = iaid;
network->dhcp_iaid_set = true;
if (!network->dhcp6_iaid_set_explicitly) {
/* Backward compatibility. Previously, IAID is shared by DHCPv4 and DHCPv6.
* If DHCPv6 IAID is not specified explicitly, then use DHCPv4 IAID for DHCPv6. */
network->dhcp6_iaid = iaid;
network->dhcp6_iaid_set = true;
}
} else {
assert(ltype == AF_INET6);
network->dhcp6_iaid = iaid;
network->dhcp6_iaid_set = true;
network->dhcp6_iaid_set_explicitly = true;
}
return 0;
}
int config_parse_dhcp_user_or_vendor_class(
const char *unit,
const char *filename,
unsigned line,
const char *section,
unsigned section_line,
const char *lvalue,
int ltype,
const char *rvalue,
void *data,
void *userdata) {
char ***l = ASSERT_PTR(data);
int r;
assert(lvalue);
assert(rvalue);
assert(IN_SET(ltype, AF_INET, AF_INET6));
if (isempty(rvalue)) {
*l = strv_free(*l);
return 0;
}
for (const char *p = rvalue;;) {
_cleanup_free_ char *w = NULL;
size_t len;
r = extract_first_word(&p, &w, NULL, EXTRACT_CUNESCAPE|EXTRACT_UNQUOTE);
if (r == -ENOMEM)
return log_oom();
if (r < 0) {
log_syntax(unit, LOG_WARNING, filename, line, r,
"Failed to split user classes option, ignoring: %s", rvalue);
return 0;
}
if (r == 0)
return 0;
len = strlen(w);
if (ltype == AF_INET) {
if (len > UINT8_MAX || len == 0) {
log_syntax(unit, LOG_WARNING, filename, line, 0,
"%s length is not in the range 1…255, ignoring.", w);
continue;
}
} else {
if (len > UINT16_MAX || len == 0) {
log_syntax(unit, LOG_WARNING, filename, line, 0,
"%s length is not in the range 1…65535, ignoring.", w);
continue;
}
}
r = strv_consume(l, TAKE_PTR(w));
if (r < 0)
return log_oom();
}
}
int config_parse_dhcp_send_option(
const char *unit,
const char *filename,
unsigned line,
const char *section,
unsigned section_line,
const char *lvalue,
int ltype,
const char *rvalue,
void *data,
void *userdata) {
_cleanup_(sd_dhcp_option_unrefp) sd_dhcp_option *opt4 = NULL;
_cleanup_(sd_dhcp6_option_unrefp) sd_dhcp6_option *opt6 = NULL;
_unused_ _cleanup_(sd_dhcp_option_unrefp) sd_dhcp_option *old4 = NULL;
_unused_ _cleanup_(sd_dhcp6_option_unrefp) sd_dhcp6_option *old6 = NULL;
uint32_t uint32_data, enterprise_identifier = 0;
_cleanup_free_ char *word = NULL, *q = NULL;
OrderedHashmap **options = ASSERT_PTR(data);
uint16_t u16, uint16_data;
union in_addr_union addr;
DHCPOptionDataType type;
uint8_t u8, uint8_data;
const void *udata;
const char *p;
ssize_t sz;
int r;
assert(filename);
assert(lvalue);
assert(rvalue);
if (isempty(rvalue)) {
*options = ordered_hashmap_free(*options);
return 0;
}
p = rvalue;
if (ltype == AF_INET6 && streq(lvalue, "SendVendorOption")) {
r = extract_first_word(&p, &word, ":", 0);
if (r == -ENOMEM)
return log_oom();
if (r <= 0 || isempty(p)) {
log_syntax(unit, LOG_WARNING, filename, line, r,
"Invalid DHCP option, ignoring assignment: %s", rvalue);
return 0;
}
r = safe_atou32(word, &enterprise_identifier);
if (r < 0) {
log_syntax(unit, LOG_WARNING, filename, line, r,
"Failed to parse DHCPv6 enterprise identifier data, ignoring assignment: %s", p);
return 0;
}
word = mfree(word);
}
r = extract_first_word(&p, &word, ":", 0);
if (r == -ENOMEM)
return log_oom();
if (r <= 0 || isempty(p)) {
log_syntax(unit, LOG_WARNING, filename, line, r,
"Invalid DHCP option, ignoring assignment: %s", rvalue);
return 0;
}
if (ltype == AF_INET6) {
r = safe_atou16(word, &u16);
if (r < 0) {
log_syntax(unit, LOG_WARNING, filename, line, r,
"Invalid DHCP option, ignoring assignment: %s", rvalue);
return 0;
}
if (u16 < 1 || u16 >= UINT16_MAX) {
log_syntax(unit, LOG_WARNING, filename, line, 0,
"Invalid DHCP option, valid range is 1-65535, ignoring assignment: %s", rvalue);
return 0;
}
} else {
r = safe_atou8(word, &u8);
if (r < 0) {
log_syntax(unit, LOG_WARNING, filename, line, r,
"Invalid DHCP option, ignoring assignment: %s", rvalue);
return 0;
}
if (u8 < 1 || u8 >= UINT8_MAX) {
log_syntax(unit, LOG_WARNING, filename, line, 0,
"Invalid DHCP option, valid range is 1-254, ignoring assignment: %s", rvalue);
return 0;
}
}
word = mfree(word);
r = extract_first_word(&p, &word, ":", 0);
if (r == -ENOMEM)
return log_oom();
if (r <= 0 || isempty(p)) {
log_syntax(unit, LOG_WARNING, filename, line, r,
"Invalid DHCP option, ignoring assignment: %s", rvalue);
return 0;
}
type = dhcp_option_data_type_from_string(word);
if (type < 0) {
log_syntax(unit, LOG_WARNING, filename, line, type,
"Invalid DHCP option data type, ignoring assignment: %s", p);
return 0;
}
switch (type) {
case DHCP_OPTION_DATA_UINT8:{
r = safe_atou8(p, &uint8_data);
if (r < 0) {
log_syntax(unit, LOG_WARNING, filename, line, r,
"Failed to parse DHCP uint8 data, ignoring assignment: %s", p);
return 0;
}
udata = &uint8_data;
sz = sizeof(uint8_t);
break;
}
case DHCP_OPTION_DATA_UINT16:{
uint16_t k;
r = safe_atou16(p, &k);
if (r < 0) {
log_syntax(unit, LOG_WARNING, filename, line, r,
"Failed to parse DHCP uint16 data, ignoring assignment: %s", p);
return 0;
}
uint16_data = htobe16(k);
udata = &uint16_data;
sz = sizeof(uint16_t);
break;
}
case DHCP_OPTION_DATA_UINT32: {
uint32_t k;
r = safe_atou32(p, &k);
if (r < 0) {
log_syntax(unit, LOG_WARNING, filename, line, r,
"Failed to parse DHCP uint32 data, ignoring assignment: %s", p);
return 0;
}
uint32_data = htobe32(k);
udata = &uint32_data;
sz = sizeof(uint32_t);
break;
}
case DHCP_OPTION_DATA_IPV4ADDRESS: {
r = in_addr_from_string(AF_INET, p, &addr);
if (r < 0) {
log_syntax(unit, LOG_WARNING, filename, line, r,
"Failed to parse DHCP ipv4address data, ignoring assignment: %s", p);
return 0;
}
udata = &addr.in;
sz = sizeof(addr.in.s_addr);
break;
}
case DHCP_OPTION_DATA_IPV6ADDRESS: {
r = in_addr_from_string(AF_INET6, p, &addr);
if (r < 0) {
log_syntax(unit, LOG_WARNING, filename, line, r,
"Failed to parse DHCP ipv6address data, ignoring assignment: %s", p);
return 0;
}
udata = &addr.in6;
sz = sizeof(addr.in6.s6_addr);
break;
}
case DHCP_OPTION_DATA_STRING:
sz = cunescape(p, UNESCAPE_ACCEPT_NUL, &q);
if (sz < 0) {
log_syntax(unit, LOG_WARNING, filename, line, sz,
"Failed to decode DHCP option data, ignoring assignment: %s", p);
return 0;
}
udata = q;
break;
default:
return -EINVAL;
}
if (ltype == AF_INET6) {
r = sd_dhcp6_option_new(u16, udata, sz, enterprise_identifier, &opt6);
if (r < 0) {
log_syntax(unit, LOG_WARNING, filename, line, r,
"Failed to store DHCP option '%s', ignoring assignment: %m", rvalue);
return 0;
}
r = ordered_hashmap_ensure_allocated(options, &dhcp6_option_hash_ops);
if (r < 0)
return log_oom();
/* Overwrite existing option */
old6 = ordered_hashmap_get(*options, UINT_TO_PTR(u16));
r = ordered_hashmap_replace(*options, UINT_TO_PTR(u16), opt6);
if (r < 0) {
log_syntax(unit, LOG_WARNING, filename, line, r,
"Failed to store DHCP option '%s', ignoring assignment: %m", rvalue);
return 0;
}
TAKE_PTR(opt6);
} else {
r = sd_dhcp_option_new(u8, udata, sz, &opt4);
if (r < 0) {
log_syntax(unit, LOG_WARNING, filename, line, r,
"Failed to store DHCP option '%s', ignoring assignment: %m", rvalue);
return 0;
}
r = ordered_hashmap_ensure_allocated(options, &dhcp_option_hash_ops);
if (r < 0)
return log_oom();
/* Overwrite existing option */
old4 = ordered_hashmap_get(*options, UINT_TO_PTR(u8));
r = ordered_hashmap_replace(*options, UINT_TO_PTR(u8), opt4);
if (r < 0) {
log_syntax(unit, LOG_WARNING, filename, line, r,
"Failed to store DHCP option '%s', ignoring assignment: %m", rvalue);
return 0;
}
TAKE_PTR(opt4);
}
return 0;
}
int config_parse_dhcp_request_options(
const char *unit,
const char *filename,
unsigned line,
const char *section,
unsigned section_line,
const char *lvalue,
int ltype,
const char *rvalue,
void *data,
void *userdata) {
Network *network = userdata;
int r;
assert(filename);
assert(lvalue);
assert(rvalue);
assert(data);
if (isempty(rvalue)) {
if (ltype == AF_INET)
network->dhcp_request_options = set_free(network->dhcp_request_options);
else
network->dhcp6_request_options = set_free(network->dhcp6_request_options);
return 0;
}
for (const char *p = rvalue;;) {
_cleanup_free_ char *n = NULL;
uint32_t i;
r = extract_first_word(&p, &n, NULL, 0);
if (r == -ENOMEM)
return log_oom();
if (r < 0) {
log_syntax(unit, LOG_WARNING, filename, line, r,
"Failed to parse DHCP request option, ignoring assignment: %s",
rvalue);
return 0;
}
if (r == 0)
return 0;
r = safe_atou32(n, &i);
if (r < 0) {
log_syntax(unit, LOG_WARNING, filename, line, r,
"DHCP request option is invalid, ignoring assignment: %s", n);
continue;
}
if (i < 1 || i >= UINT8_MAX) {
log_syntax(unit, LOG_WARNING, filename, line, 0,
"DHCP request option is invalid, valid range is 1-254, ignoring assignment: %s", n);
continue;
}
r = set_ensure_put(ltype == AF_INET ? &network->dhcp_request_options : &network->dhcp6_request_options,
NULL, UINT32_TO_PTR(i));
if (r < 0)
log_syntax(unit, LOG_WARNING, filename, line, r,
"Failed to store DHCP request option '%s', ignoring assignment: %m", n);
}
}
static const char * const dhcp_option_data_type_table[_DHCP_OPTION_DATA_MAX] = {
[DHCP_OPTION_DATA_UINT8] = "uint8",
[DHCP_OPTION_DATA_UINT16] = "uint16",
[DHCP_OPTION_DATA_UINT32] = "uint32",
[DHCP_OPTION_DATA_STRING] = "string",
[DHCP_OPTION_DATA_IPV4ADDRESS] = "ipv4address",
[DHCP_OPTION_DATA_IPV6ADDRESS] = "ipv6address",
};
DEFINE_STRING_TABLE_LOOKUP(dhcp_option_data_type, DHCPOptionDataType);
static const char* const duid_type_table[_DUID_TYPE_MAX] = {
[DUID_TYPE_LLT] = "link-layer-time",
[DUID_TYPE_EN] = "vendor",
[DUID_TYPE_LL] = "link-layer",
[DUID_TYPE_UUID] = "uuid",
};
DEFINE_PRIVATE_STRING_TABLE_LOOKUP_FROM_STRING(duid_type, DUIDType);
int config_parse_duid_type(
const char *unit,
const char *filename,
unsigned line,
const char *section,
unsigned section_line,
const char *lvalue,
int ltype,
const char *rvalue,
void *data,
void *userdata) {
_cleanup_free_ char *type_string = NULL;
const char *p = ASSERT_PTR(rvalue);
bool force = ltype;
DUID *duid = ASSERT_PTR(data);
DUIDType type;
int r;
assert(filename);
assert(lvalue);
if (!force && duid->set)
return 0;
r = extract_first_word(&p, &type_string, ":", 0);
if (r == -ENOMEM)
return log_oom();
if (r < 0) {
log_syntax(unit, LOG_WARNING, filename, line, r,
"Invalid syntax, ignoring: %s", rvalue);
return 0;
}
if (r == 0) {
log_syntax(unit, LOG_WARNING, filename, line, 0,
"Failed to extract DUID type from '%s', ignoring.", rvalue);
return 0;
}
type = duid_type_from_string(type_string);
if (type < 0) {
uint16_t t;
r = safe_atou16(type_string, &t);
if (r < 0) {
log_syntax(unit, LOG_WARNING, filename, line, r,
"Failed to parse DUID type '%s', ignoring.", type_string);
return 0;
}
type = t;
assert(type == t); /* Check if type can store uint16_t. */
}
if (!isempty(p)) {
usec_t u;
if (type != DUID_TYPE_LLT) {
log_syntax(unit, LOG_WARNING, filename, line, r,
"Invalid syntax, ignoring: %s", rvalue);
return 0;
}
r = parse_timestamp(p, &u);
if (r < 0) {
log_syntax(unit, LOG_WARNING, filename, line, r,
"Failed to parse timestamp, ignoring: %s", p);
return 0;
}
duid->llt_time = u;
}
duid->type = type;
duid->set = force;
return 0;
}
int config_parse_manager_duid_type(
const char *unit,
const char *filename,
unsigned line,
const char *section,
unsigned section_line,
const char *lvalue,
int ltype,
const char *rvalue,
void *data,
void *userdata) {
Manager *manager = ASSERT_PTR(userdata);
int r;
/* For backward compatibility. Setting both DHCPv4 and DHCPv6 DUID if they are not specified explicitly. */
r = config_parse_duid_type(unit, filename, line, section, section_line, lvalue, false, rvalue, &manager->dhcp_duid, manager);
if (r < 0)
return r;
return config_parse_duid_type(unit, filename, line, section, section_line, lvalue, false, rvalue, &manager->dhcp6_duid, manager);
}
int config_parse_network_duid_type(
const char *unit,
const char *filename,
unsigned line,
const char *section,
unsigned section_line,
const char *lvalue,
int ltype,
const char *rvalue,
void *data,
void *userdata) {
Network *network = ASSERT_PTR(userdata);
int r;
r = config_parse_duid_type(unit, filename, line, section, section_line, lvalue, true, rvalue, &network->dhcp_duid, network);
if (r < 0)
return r;
/* For backward compatibility, also set DHCPv6 DUID if not specified explicitly. */
return config_parse_duid_type(unit, filename, line, section, section_line, lvalue, false, rvalue, &network->dhcp6_duid, network);
}
int config_parse_duid_rawdata(
const char *unit,
const char *filename,
unsigned line,
const char *section,
unsigned section_line,
const char *lvalue,
int ltype,
const char *rvalue,
void *data,
void *userdata) {
uint8_t raw_data[MAX_DUID_DATA_LEN];
unsigned count = 0;
bool force = ltype;
DUID *duid = ASSERT_PTR(data);
assert(filename);
assert(lvalue);
assert(rvalue);
if (!force && duid->set)
return 0;
/* RawData contains DUID in format "NN:NN:NN..." */
for (const char *p = rvalue;;) {
int n1, n2, len, r;
uint32_t byte;
_cleanup_free_ char *cbyte = NULL;
r = extract_first_word(&p, &cbyte, ":", 0);
if (r == -ENOMEM)
return log_oom();
if (r < 0) {
log_syntax(unit, LOG_WARNING, filename, line, r, "Failed to read DUID, ignoring assignment: %s.", rvalue);
return 0;
}
if (r == 0)
break;
if (count >= MAX_DUID_DATA_LEN) {
log_syntax(unit, LOG_WARNING, filename, line, 0, "Max DUID length exceeded, ignoring assignment: %s.", rvalue);
return 0;
}
len = strlen(cbyte);
if (!IN_SET(len, 1, 2)) {
log_syntax(unit, LOG_WARNING, filename, line, 0, "Invalid length - DUID byte: %s, ignoring assignment: %s.", cbyte, rvalue);
return 0;
}
n1 = unhexchar(cbyte[0]);
if (len == 2)
n2 = unhexchar(cbyte[1]);
else
n2 = 0;
if (n1 < 0 || n2 < 0) {
log_syntax(unit, LOG_WARNING, filename, line, 0, "Invalid DUID byte: %s. Ignoring assignment: %s.", cbyte, rvalue);
return 0;
}
byte = ((uint8_t) n1 << (4 * (len-1))) | (uint8_t) n2;
raw_data[count++] = byte;
}
assert_cc(sizeof(raw_data) == sizeof(duid->raw_data));
memcpy(duid->raw_data, raw_data, count);
duid->raw_data_len = count;
duid->set = force;
return 0;
}
int config_parse_manager_duid_rawdata(
const char *unit,
const char *filename,
unsigned line,
const char *section,
unsigned section_line,
const char *lvalue,
int ltype,
const char *rvalue,
void *data,
void *userdata) {
Manager *manager = ASSERT_PTR(userdata);
int r;
/* For backward compatibility. Setting both DHCPv4 and DHCPv6 DUID if they are not specified explicitly. */
r = config_parse_duid_rawdata(unit, filename, line, section, section_line, lvalue, false, rvalue, &manager->dhcp_duid, manager);
if (r < 0)
return r;
return config_parse_duid_rawdata(unit, filename, line, section, section_line, lvalue, false, rvalue, &manager->dhcp6_duid, manager);
}
int config_parse_network_duid_rawdata(
const char *unit,
const char *filename,
unsigned line,
const char *section,
unsigned section_line,
const char *lvalue,
int ltype,
const char *rvalue,
void *data,
void *userdata) {
Network *network = ASSERT_PTR(userdata);
int r;
r = config_parse_duid_rawdata(unit, filename, line, section, section_line, lvalue, true, rvalue, &network->dhcp_duid, network);
if (r < 0)
return r;
/* For backward compatibility, also set DHCPv6 DUID if not specified explicitly. */
return config_parse_duid_rawdata(unit, filename, line, section, section_line, lvalue, false, rvalue, &network->dhcp6_duid, network);
}
int config_parse_uplink(
const char *unit,
const char *filename,
unsigned line,
const char *section,
unsigned section_line,
const char *lvalue,
int ltype,
const char *rvalue,
void *data,
void *userdata) {
Network *network = ASSERT_PTR(userdata);
bool accept_none = true;
int *index, r;
char **name;
assert(filename);
assert(section);
assert(lvalue);
assert(rvalue);
if (streq(section, "DHCPServer")) {
index = &network->dhcp_server_uplink_index;
name = &network->dhcp_server_uplink_name;
} else if (streq(section, "IPv6SendRA")) {
index = &network->router_uplink_index;
name = &network->router_uplink_name;
} else if (STR_IN_SET(section, "DHCPv6PrefixDelegation", "DHCPPrefixDelegation")) {
index = &network->dhcp_pd_uplink_index;
name = &network->dhcp_pd_uplink_name;
accept_none = false;
} else
assert_not_reached();
if (isempty(rvalue) || streq(rvalue, ":auto")) {
*index = UPLINK_INDEX_AUTO;
*name = mfree(*name);
return 0;
}
if (accept_none && streq(rvalue, ":none")) {
*index = UPLINK_INDEX_NONE;
*name = mfree(*name);
return 0;
}
if (!accept_none && streq(rvalue, ":self")) {
*index = UPLINK_INDEX_SELF;
*name = mfree(*name);
return 0;
}
r = parse_ifindex(rvalue);
if (r > 0) {
*index = r;
*name = mfree(*name);
return 0;
}
if (!ifname_valid_full(rvalue, IFNAME_VALID_ALTERNATIVE)) {
log_syntax(unit, LOG_WARNING, filename, line, 0,
"Invalid interface name in %s=, ignoring assignment: %s", lvalue, rvalue);
return 0;
}
/* The interface name will be resolved later. */
r = free_and_strdup_warn(name, rvalue);
if (r < 0)
return r;
/* Note, if uplink_name is set, then uplink_index will be ignored. So, the below does not mean
* an uplink interface will be selected automatically. */
*index = UPLINK_INDEX_AUTO;
return 0;
}
|