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
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
|
/* SPDX-License-Identifier: LGPL-2.1-or-later */
#include <netinet/in.h>
#include <netinet/ip.h>
#include <linux/if.h>
#include <linux/if_arp.h>
#include "alloc-util.h"
#include "device-private.h"
#include "dhcp-client-internal.h"
#include "hostname-setup.h"
#include "hostname-util.h"
#include "parse-util.h"
#include "network-internal.h"
#include "networkd-address.h"
#include "networkd-dhcp-prefix-delegation.h"
#include "networkd-dhcp4-bus.h"
#include "networkd-dhcp4.h"
#include "networkd-ipv4acd.h"
#include "networkd-link.h"
#include "networkd-manager.h"
#include "networkd-network.h"
#include "networkd-nexthop.h"
#include "networkd-ntp.h"
#include "networkd-queue.h"
#include "networkd-route.h"
#include "networkd-setlink.h"
#include "networkd-state-file.h"
#include "string-table.h"
#include "strv.h"
#include "sysctl-util.h"
void network_adjust_dhcp4(Network *network) {
assert(network);
if (!FLAGS_SET(network->dhcp, ADDRESS_FAMILY_IPV4))
return;
if (network->dhcp_use_gateway < 0)
network->dhcp_use_gateway = network->dhcp_use_routes;
/* RFC7844 section 3.: MAY contain the Client Identifier option
* Section 3.5: clients MUST use client identifiers based solely on the link-layer address
* NOTE: Using MAC, as it does not reveal extra information, and some servers might not answer
* if this option is not sent */
if (network->dhcp_anonymize &&
network->dhcp_client_identifier >= 0 &&
network->dhcp_client_identifier != DHCP_CLIENT_ID_MAC) {
log_warning("%s: ClientIdentifier= is set, although Anonymize=yes. Using ClientIdentifier=mac.",
network->filename);
network->dhcp_client_identifier = DHCP_CLIENT_ID_MAC;
}
if (network->dhcp_client_identifier < 0)
network->dhcp_client_identifier = network->dhcp_anonymize ? DHCP_CLIENT_ID_MAC : DHCP_CLIENT_ID_DUID;
/* By default, RapidCommit= is enabled when Anonymize=no and neither AllowList= nor DenyList= is specified. */
if (network->dhcp_use_rapid_commit < 0)
network->dhcp_use_rapid_commit =
!network->dhcp_anonymize &&
set_isempty(network->dhcp_allow_listed_ip) &&
set_isempty(network->dhcp_deny_listed_ip);
}
static int dhcp4_prefix_covers(
Link *link,
const struct in_addr *in_prefix,
uint8_t in_prefixlen) {
struct in_addr prefix;
uint8_t prefixlen;
int r;
assert(link);
assert(link->dhcp_lease);
assert(in_prefix);
/* Return true if the input address or address range is in the assigned network.
* E.g. if the DHCP server provides 192.168.0.100/24, then this returns true for the address or
* address range in 192.168.0.0/24, and returns false otherwise. */
r = sd_dhcp_lease_get_prefix(link->dhcp_lease, &prefix, &prefixlen);
if (r < 0)
return r;
return in4_addr_prefix_covers_full(&prefix, prefixlen, in_prefix, in_prefixlen);
}
static int dhcp4_get_router(Link *link, struct in_addr *ret) {
const struct in_addr *routers;
int r;
assert(link);
assert(link->dhcp_lease);
assert(ret);
r = sd_dhcp_lease_get_router(link->dhcp_lease, &routers);
if (r < 0)
return r;
/* The router option may provide multiple routers, We only use the first non-null address. */
FOREACH_ARRAY(router, routers, r) {
if (in4_addr_is_null(router))
continue;
*ret = *router;
return 0;
}
return -ENODATA;
}
static int dhcp4_get_classless_static_or_static_routes(Link *link, sd_dhcp_route ***ret_routes, size_t *ret_num) {
_cleanup_free_ sd_dhcp_route **routes = NULL;
int r;
assert(link);
assert(link->dhcp_lease);
/* If the DHCP server returns both a Classless Static Routes option and a Static Routes option,
* the DHCP client MUST ignore the Static Routes option. */
r = sd_dhcp_lease_get_classless_routes(link->dhcp_lease, &routes);
if (r >= 0) {
assert(r > 0);
if (ret_routes)
*ret_routes = TAKE_PTR(routes);
if (ret_num)
*ret_num = r;
return 1; /* classless */
} else if (r != -ENODATA)
return r;
r = sd_dhcp_lease_get_static_routes(link->dhcp_lease, &routes);
if (r < 0)
return r;
assert(r > 0);
if (ret_routes)
*ret_routes = TAKE_PTR(routes);
if (ret_num)
*ret_num = r;
return 0; /* static */
}
static int dhcp4_find_gateway_for_destination(
Link *link,
const struct in_addr *destination,
uint8_t prefixlength,
struct in_addr *ret) {
_cleanup_free_ sd_dhcp_route **routes = NULL;
size_t n_routes = 0;
bool is_classless;
uint8_t max_prefixlen = UINT8_MAX;
struct in_addr gw;
int r;
assert(link);
assert(link->dhcp_lease);
assert(destination);
assert(ret);
/* This tries to find the most suitable gateway for an address or address range.
* E.g. if the server provides the default gateway 192.168.0.1 and a classless static route for
* 8.0.0.0/8 with gateway 192.168.0.2, then this returns 192.168.0.2 for 8.8.8.8/32, and 192.168.0.1
* for 9.9.9.9/32. If the input address or address range is in the assigned network, then the null
* address will be returned. */
/* First, check with the assigned prefix, and if the destination is in the prefix, set the null
* address for the gateway, and return it unless more suitable static route is found. */
r = dhcp4_prefix_covers(link, destination, prefixlength);
if (r < 0)
return r;
if (r > 0) {
r = sd_dhcp_lease_get_prefix(link->dhcp_lease, NULL, &max_prefixlen);
if (r < 0)
return r;
gw = (struct in_addr) {};
}
r = dhcp4_get_classless_static_or_static_routes(link, &routes, &n_routes);
if (r < 0 && r != -ENODATA)
return r;
is_classless = r > 0;
/* First, find most suitable gateway. */
FOREACH_ARRAY(e, routes, n_routes) {
struct in_addr dst;
uint8_t len;
r = sd_dhcp_route_get_destination(*e, &dst);
if (r < 0)
return r;
r = sd_dhcp_route_get_destination_prefix_length(*e, &len);
if (r < 0)
return r;
r = in4_addr_prefix_covers_full(&dst, len, destination, prefixlength);
if (r < 0)
return r;
if (r == 0)
continue;
if (max_prefixlen != UINT8_MAX && max_prefixlen > len)
continue;
r = sd_dhcp_route_get_gateway(*e, &gw);
if (r < 0)
return r;
max_prefixlen = len;
}
/* The destination is reachable. Note, the gateway address returned here may be NULL. */
if (max_prefixlen != UINT8_MAX) {
*ret = gw;
return 0;
}
/* According to RFC 3442: If the DHCP server returns both a Classless Static Routes option and
* a Router option, the DHCP client MUST ignore the Router option. */
if (!is_classless) {
/* No matching static route is found, and the destination is not in the acquired network,
* falling back to the Router option. */
r = dhcp4_get_router(link, ret);
if (r >= 0)
return 0;
if (r != -ENODATA)
return r;
}
return -EHOSTUNREACH; /* Cannot reach the destination. */
}
static int dhcp4_remove_address_and_routes(Link *link, bool only_marked) {
Address *address;
Route *route;
int ret = 0;
assert(link);
assert(link->manager);
SET_FOREACH(route, link->manager->routes) {
if (route->source != NETWORK_CONFIG_SOURCE_DHCP4)
continue;
if (route->nexthop.ifindex != 0 && route->nexthop.ifindex != link->ifindex)
continue;
if (only_marked && !route_is_marked(route))
continue;
RET_GATHER(ret, route_remove_and_cancel(route, link->manager));
}
SET_FOREACH(address, link->addresses) {
if (address->source != NETWORK_CONFIG_SOURCE_DHCP4)
continue;
if (only_marked && !address_is_marked(address))
continue;
RET_GATHER(ret, address_remove_and_cancel(address, link));
}
return ret;
}
static int dhcp4_address_get(Link *link, Address **ret) {
Address *address;
assert(link);
SET_FOREACH(address, link->addresses) {
if (address->source != NETWORK_CONFIG_SOURCE_DHCP4)
continue;
if (address_is_marked(address))
continue;
if (ret)
*ret = address;
return 0;
}
return -ENOENT;
}
static int dhcp4_address_ready_callback(Address *address) {
assert(address);
assert(address->link);
/* Do not call this again. */
address->callback = NULL;
return dhcp4_check_ready(address->link);
}
int dhcp4_check_ready(Link *link) {
Address *address;
int r;
assert(link);
if (link->dhcp4_messages > 0) {
log_link_debug(link, "%s(): DHCPv4 address and routes are not set.", __func__);
return 0;
}
if (dhcp4_address_get(link, &address) < 0) {
log_link_debug(link, "%s(): DHCPv4 address is not set.", __func__);
return 0;
}
if (!address_is_ready(address)) {
log_link_debug(link, "%s(): DHCPv4 address is not ready.", __func__);
address->callback = dhcp4_address_ready_callback;
return 0;
}
link->dhcp4_configured = true;
log_link_debug(link, "DHCPv4 address and routes set.");
/* New address and routes are configured now. Let's release old lease. */
r = dhcp4_remove_address_and_routes(link, /* only_marked = */ true);
if (r < 0)
return r;
r = sd_ipv4ll_stop(link->ipv4ll);
if (r < 0)
return log_link_warning_errno(link, r, "Failed to drop IPv4 link-local address: %m");
link_check_ready(link);
return 0;
}
static int dhcp4_route_handler(sd_netlink *rtnl, sd_netlink_message *m, Request *req, Link *link, Route *route) {
int r;
assert(m);
assert(req);
assert(link);
r = route_configure_handler_internal(rtnl, m, req, "Could not set DHCPv4 route");
if (r <= 0)
return r;
r = dhcp4_check_ready(link);
if (r < 0)
link_enter_failed(link);
return 1;
}
static int dhcp4_request_route(Route *route, Link *link) {
struct in_addr server;
Route *existing;
int r;
assert(route);
assert(link);
assert(link->manager);
assert(link->network);
assert(link->dhcp_lease);
r = sd_dhcp_lease_get_server_identifier(link->dhcp_lease, &server);
if (r < 0)
return log_link_debug_errno(link, r, "Failed to get DHCP server IP address: %m");
route->source = NETWORK_CONFIG_SOURCE_DHCP4;
route->provider.in = server;
route->family = AF_INET;
if (!route->protocol_set)
route->protocol = RTPROT_DHCP;
if (!route->priority_set)
route->priority = link->network->dhcp_route_metric;
if (!route->table_set)
route->table = link_get_dhcp4_route_table(link);
r = route_metric_set(&route->metric, RTAX_MTU, link->network->dhcp_route_mtu);
if (r < 0)
return r;
r = route_metric_set(&route->metric, RTAX_INITCWND, link->network->dhcp_initial_congestion_window);
if (r < 0)
return r;
r = route_metric_set(&route->metric, RTAX_INITRWND, link->network->dhcp_advertised_receive_window);
if (r < 0)
return r;
r = route_metric_set(&route->metric, RTAX_QUICKACK, link->network->dhcp_quickack);
if (r < 0)
return r;
r = route_adjust_nexthops(route, link);
if (r < 0)
return r;
if (route_get(link->manager, route, &existing) < 0) /* This is a new route. */
link->dhcp4_configured = false;
else
route_unmark(existing);
return link_request_route(link, route, &link->dhcp4_messages, dhcp4_route_handler);
}
static bool link_prefixroute(Link *link) {
return !link->network->dhcp_route_table_set ||
link->network->dhcp_route_table == RT_TABLE_MAIN;
}
static int dhcp4_request_prefix_route(Link *link) {
_cleanup_(route_unrefp) Route *route = NULL;
int r;
assert(link);
assert(link->dhcp_lease);
if (link_prefixroute(link))
/* When true, the route will be created by kernel. See dhcp4_update_address(). */
return 0;
r = route_new(&route);
if (r < 0)
return r;
route->scope = RT_SCOPE_LINK;
r = sd_dhcp_lease_get_prefix(link->dhcp_lease, &route->dst.in, &route->dst_prefixlen);
if (r < 0)
return r;
r = sd_dhcp_lease_get_address(link->dhcp_lease, &route->prefsrc.in);
if (r < 0)
return r;
return dhcp4_request_route(route, link);
}
static int dhcp4_request_route_to_gateway(Link *link, const struct in_addr *gw) {
_cleanup_(route_unrefp) Route *route = NULL;
struct in_addr address;
int r;
assert(link);
assert(link->dhcp_lease);
assert(gw);
r = sd_dhcp_lease_get_address(link->dhcp_lease, &address);
if (r < 0)
return r;
r = route_new(&route);
if (r < 0)
return r;
route->dst.in = *gw;
route->dst_prefixlen = 32;
route->prefsrc.in = address;
route->scope = RT_SCOPE_LINK;
return dhcp4_request_route(route, link);
}
static int dhcp4_request_route_auto(
Route *route,
Link *link,
const struct in_addr *gw) {
struct in_addr address;
int r;
assert(route);
assert(link);
assert(link->dhcp_lease);
assert(gw);
r = sd_dhcp_lease_get_address(link->dhcp_lease, &address);
if (r < 0)
return r;
if (in4_addr_is_localhost(&route->dst.in)) {
if (in4_addr_is_set(gw))
log_link_debug(link, "DHCP: requested route destination "IPV4_ADDRESS_FMT_STR"/%u is localhost, "
"ignoring gateway address "IPV4_ADDRESS_FMT_STR,
IPV4_ADDRESS_FMT_VAL(route->dst.in), route->dst_prefixlen, IPV4_ADDRESS_FMT_VAL(*gw));
route->scope = RT_SCOPE_HOST;
route->nexthop.family = AF_UNSPEC;
route->nexthop.gw = IN_ADDR_NULL;
route->prefsrc = IN_ADDR_NULL;
} else if (in4_addr_equal(&route->dst.in, &address)) {
if (in4_addr_is_set(gw))
log_link_debug(link, "DHCP: requested route destination "IPV4_ADDRESS_FMT_STR"/%u is equivalent to the acquired address, "
"ignoring gateway address "IPV4_ADDRESS_FMT_STR,
IPV4_ADDRESS_FMT_VAL(route->dst.in), route->dst_prefixlen, IPV4_ADDRESS_FMT_VAL(*gw));
route->scope = RT_SCOPE_HOST;
route->nexthop.family = AF_UNSPEC;
route->nexthop.gw = IN_ADDR_NULL;
route->prefsrc.in = address;
} else if (in4_addr_is_null(gw)) {
r = dhcp4_prefix_covers(link, &route->dst.in, route->dst_prefixlen);
if (r < 0)
return r;
if (r == 0 && DEBUG_LOGGING) {
struct in_addr prefix;
uint8_t prefixlen;
r = sd_dhcp_lease_get_prefix(link->dhcp_lease, &prefix, &prefixlen);
if (r < 0)
return r;
log_link_debug(link, "DHCP: requested route destination "IPV4_ADDRESS_FMT_STR"/%u is not in the assigned network "
IPV4_ADDRESS_FMT_STR"/%u, but no gateway is specified, using 'link' scope.",
IPV4_ADDRESS_FMT_VAL(route->dst.in), route->dst_prefixlen,
IPV4_ADDRESS_FMT_VAL(prefix), prefixlen);
}
route->scope = RT_SCOPE_LINK;
route->nexthop.family = AF_UNSPEC;
route->nexthop.gw = IN_ADDR_NULL;
route->prefsrc.in = address;
} else {
r = dhcp4_request_route_to_gateway(link, gw);
if (r < 0)
return r;
route->scope = RT_SCOPE_UNIVERSE;
route->nexthop.family = AF_INET;
route->nexthop.gw.in = *gw;
route->prefsrc.in = address;
}
return dhcp4_request_route(route, link);
}
static int dhcp4_request_classless_static_or_static_routes(Link *link) {
_cleanup_free_ sd_dhcp_route **routes = NULL;
size_t n_routes;
int r;
assert(link);
assert(link->dhcp_lease);
if (!link->network->dhcp_use_routes)
return 0;
r = dhcp4_get_classless_static_or_static_routes(link, &routes, &n_routes);
if (r == -ENODATA)
return 0;
if (r < 0)
return r;
FOREACH_ARRAY(e, routes, n_routes) {
_cleanup_(route_unrefp) Route *route = NULL;
struct in_addr gw;
r = route_new(&route);
if (r < 0)
return r;
r = sd_dhcp_route_get_gateway(*e, &gw);
if (r < 0)
return r;
r = sd_dhcp_route_get_destination(*e, &route->dst.in);
if (r < 0)
return r;
r = sd_dhcp_route_get_destination_prefix_length(*e, &route->dst_prefixlen);
if (r < 0)
return r;
r = dhcp4_request_route_auto(route, link, &gw);
if (r < 0)
return r;
}
return 0;
}
static int dhcp4_request_default_gateway(Link *link) {
_cleanup_(route_unrefp) Route *route = NULL;
struct in_addr address, router;
int r;
assert(link);
assert(link->dhcp_lease);
if (!link->network->dhcp_use_gateway)
return 0;
/* According to RFC 3442: If the DHCP server returns both a Classless Static Routes option and
* a Router option, the DHCP client MUST ignore the Router option. */
if (link->network->dhcp_use_routes &&
dhcp4_get_classless_static_or_static_routes(link, NULL, NULL) > 0)
return 0;
r = sd_dhcp_lease_get_address(link->dhcp_lease, &address);
if (r < 0)
return r;
r = dhcp4_get_router(link, &router);
if (r == -ENODATA) {
log_link_debug(link, "DHCP: No valid router address received from DHCP server.");
return 0;
}
if (r < 0)
return r;
/* The dhcp netmask may mask out the gateway. First, add an explicit route for the gateway host
* so that we can route no matter the netmask or existing kernel route tables. */
r = dhcp4_request_route_to_gateway(link, &router);
if (r < 0)
return r;
r = route_new(&route);
if (r < 0)
return r;
/* Next, add a default gateway. */
route->nexthop.family = AF_INET;
route->nexthop.gw.in = router;
route->prefsrc.in = address;
return dhcp4_request_route(route, link);
}
static int dhcp4_request_semi_static_routes(Link *link) {
Route *rt;
int r;
assert(link);
assert(link->dhcp_lease);
assert(link->network);
HASHMAP_FOREACH(rt, link->network->routes_by_section) {
_cleanup_(route_unrefp) Route *route = NULL;
struct in_addr gw;
if (!rt->gateway_from_dhcp_or_ra)
continue;
if (rt->nexthop.family != AF_INET)
continue;
assert(rt->family == AF_INET);
r = dhcp4_find_gateway_for_destination(link, &rt->dst.in, rt->dst_prefixlen, &gw);
if (r == -EHOSTUNREACH) {
log_link_debug_errno(link, r, "DHCP: Cannot find suitable gateway for destination %s of semi-static route, ignoring: %m",
IN4_ADDR_PREFIX_TO_STRING(&rt->dst.in, rt->dst_prefixlen));
continue;
}
if (r < 0)
return r;
if (in4_addr_is_null(&gw)) {
log_link_debug(link, "DHCP: Destination %s of semi-static route is in the acquired network, skipping configuration.",
IN4_ADDR_PREFIX_TO_STRING(&rt->dst.in, rt->dst_prefixlen));
continue;
}
r = dhcp4_request_route_to_gateway(link, &gw);
if (r < 0)
return r;
r = route_dup(rt, NULL, &route);
if (r < 0)
return r;
route->nexthop.gw.in = gw;
r = dhcp4_request_route(route, link);
if (r < 0)
return r;
}
return 0;
}
static int dhcp4_request_routes_to_servers(
Link *link,
const struct in_addr *servers,
size_t n_servers) {
int r;
assert(link);
assert(link->dhcp_lease);
assert(link->network);
assert(servers || n_servers == 0);
FOREACH_ARRAY(dst, servers, n_servers) {
_cleanup_(route_unrefp) Route *route = NULL;
struct in_addr gw;
if (in4_addr_is_null(dst))
continue;
r = dhcp4_find_gateway_for_destination(link, dst, 32, &gw);
if (r == -EHOSTUNREACH) {
log_link_debug_errno(link, r, "DHCP: Cannot find suitable gateway for destination %s, ignoring: %m",
IN4_ADDR_PREFIX_TO_STRING(dst, 32));
continue;
}
if (r < 0)
return r;
r = route_new(&route);
if (r < 0)
return r;
route->dst.in = *dst;
route->dst_prefixlen = 32;
r = dhcp4_request_route_auto(route, link, &gw);
if (r < 0)
return r;
}
return 0;
}
static int dhcp4_request_routes_to_dns(Link *link) {
const struct in_addr *dns;
int r;
assert(link);
assert(link->dhcp_lease);
assert(link->network);
if (!link_get_use_dns(link, NETWORK_CONFIG_SOURCE_DHCP4) ||
!link->network->dhcp_routes_to_dns)
return 0;
r = sd_dhcp_lease_get_dns(link->dhcp_lease, &dns);
if (IN_SET(r, 0, -ENODATA))
return 0;
if (r < 0)
return r;
return dhcp4_request_routes_to_servers(link, dns, r);
}
static int dhcp4_request_routes_to_ntp(Link *link) {
const struct in_addr *ntp;
int r;
assert(link);
assert(link->dhcp_lease);
assert(link->network);
if (!link_get_use_ntp(link, NETWORK_CONFIG_SOURCE_DHCP4) ||
!link->network->dhcp_routes_to_ntp)
return 0;
r = sd_dhcp_lease_get_ntp(link->dhcp_lease, &ntp);
if (IN_SET(r, 0, -ENODATA))
return 0;
if (r < 0)
return r;
return dhcp4_request_routes_to_servers(link, ntp, r);
}
static int dhcp4_request_routes(Link *link) {
int r;
assert(link);
assert(link->dhcp_lease);
r = dhcp4_request_prefix_route(link);
if (r < 0)
return log_link_error_errno(link, r, "DHCP error: Could not request prefix route: %m");
r = dhcp4_request_default_gateway(link);
if (r < 0)
return log_link_error_errno(link, r, "DHCP error: Could not request default gateway: %m");
r = dhcp4_request_classless_static_or_static_routes(link);
if (r < 0)
return log_link_error_errno(link, r, "DHCP error: Could not request static routes: %m");
r = dhcp4_request_semi_static_routes(link);
if (r < 0)
return log_link_error_errno(link, r, "DHCP error: Could not request routes with Gateway=_dhcp4 setting: %m");
r = dhcp4_request_routes_to_dns(link);
if (r < 0)
return log_link_error_errno(link, r, "DHCP error: Could not request routes to DNS servers: %m");
r = dhcp4_request_routes_to_ntp(link);
if (r < 0)
return log_link_error_errno(link, r, "DHCP error: Could not request routes to NTP servers: %m");
return 0;
}
static int dhcp_reset_mtu(Link *link) {
int r;
assert(link);
if (!link->network->dhcp_use_mtu)
return 0;
r = link_request_to_set_mtu(link, link->original_mtu);
if (r < 0)
return log_link_error_errno(link, r, "DHCP error: Could not queue request to reset MTU: %m");
return 0;
}
static int dhcp_reset_hostname(Link *link) {
const char *hostname;
int r;
assert(link);
if (!link->network->dhcp_use_hostname)
return 0;
hostname = link->network->dhcp_hostname;
if (!hostname)
(void) sd_dhcp_lease_get_hostname(link->dhcp_lease, &hostname);
if (!hostname)
return 0;
/* If a hostname was set due to the lease, then unset it now. */
r = manager_set_hostname(link->manager, NULL);
if (r < 0)
return log_link_error_errno(link, r, "DHCP error: Failed to reset transient hostname: %m");
return 0;
}
int dhcp4_lease_lost(Link *link) {
int r = 0;
assert(link);
assert(link->dhcp_lease);
assert(link->network);
log_link_info(link, "DHCP lease lost");
link->dhcp4_configured = false;
if (link->network->dhcp_use_6rd &&
sd_dhcp_lease_has_6rd(link->dhcp_lease))
dhcp4_pd_prefix_lost(link);
RET_GATHER(r, dhcp4_remove_address_and_routes(link, /* only_marked = */ false));
RET_GATHER(r, dhcp_reset_mtu(link));
RET_GATHER(r, dhcp_reset_hostname(link));
link->dhcp_lease = sd_dhcp_lease_unref(link->dhcp_lease);
link_dirty(link);
/* If one of the above failed. Do not request nexthops and routes. */
if (r < 0)
return r;
r = link_request_static_nexthops(link, true);
if (r < 0)
return r;
return link_request_static_routes(link, true);
}
static int dhcp4_address_handler(sd_netlink *rtnl, sd_netlink_message *m, Request *req, Link *link, Address *address) {
int r;
assert(link);
r = address_configure_handler_internal(rtnl, m, link, "Could not set DHCPv4 address");
if (r <= 0)
return r;
r = dhcp4_check_ready(link);
if (r < 0)
link_enter_failed(link);
return 1;
}
static int dhcp4_request_address(Link *link, bool announce) {
_cleanup_(address_unrefp) Address *addr = NULL;
struct in_addr address, server;
uint8_t prefixlen;
Address *existing;
usec_t lifetime_usec;
int r;
assert(link);
assert(link->manager);
assert(link->network);
assert(link->dhcp_lease);
r = sd_dhcp_lease_get_address(link->dhcp_lease, &address);
if (r < 0)
return log_link_warning_errno(link, r, "DHCP error: no address: %m");
r = sd_dhcp_lease_get_prefix(link->dhcp_lease, NULL, &prefixlen);
if (r < 0)
return log_link_warning_errno(link, r, "DHCP error: no netmask: %m");
r = sd_dhcp_lease_get_server_identifier(link->dhcp_lease, &server);
if (r < 0)
return log_link_debug_errno(link, r, "DHCP error: failed to get DHCP server IP address: %m");
if (!FLAGS_SET(link->network->keep_configuration, KEEP_CONFIGURATION_DHCP)) {
r = sd_dhcp_lease_get_lifetime_timestamp(link->dhcp_lease, CLOCK_BOOTTIME, &lifetime_usec);
if (r < 0)
return log_link_warning_errno(link, r, "DHCP error: failed to get lifetime: %m");
} else
lifetime_usec = USEC_INFINITY;
if (announce) {
const struct in_addr *router;
r = sd_dhcp_lease_get_router(link->dhcp_lease, &router);
if (r < 0 && r != -ENODATA)
return log_link_error_errno(link, r, "DHCP error: Could not get gateway: %m");
if (r > 0 && in4_addr_is_set(&router[0]))
log_struct(LOG_INFO,
LOG_LINK_INTERFACE(link),
LOG_LINK_MESSAGE(link, "DHCPv4 address "IPV4_ADDRESS_FMT_STR"/%u, gateway "IPV4_ADDRESS_FMT_STR" acquired from "IPV4_ADDRESS_FMT_STR,
IPV4_ADDRESS_FMT_VAL(address),
prefixlen,
IPV4_ADDRESS_FMT_VAL(router[0]),
IPV4_ADDRESS_FMT_VAL(server)),
"ADDRESS="IPV4_ADDRESS_FMT_STR, IPV4_ADDRESS_FMT_VAL(address),
"PREFIXLEN=%u", prefixlen,
"GATEWAY="IPV4_ADDRESS_FMT_STR, IPV4_ADDRESS_FMT_VAL(router[0]));
else
log_struct(LOG_INFO,
LOG_LINK_INTERFACE(link),
LOG_LINK_MESSAGE(link, "DHCPv4 address "IPV4_ADDRESS_FMT_STR"/%u acquired from "IPV4_ADDRESS_FMT_STR,
IPV4_ADDRESS_FMT_VAL(address),
prefixlen,
IPV4_ADDRESS_FMT_VAL(server)),
"ADDRESS="IPV4_ADDRESS_FMT_STR, IPV4_ADDRESS_FMT_VAL(address),
"PREFIXLEN=%u", prefixlen);
}
r = address_new(&addr);
if (r < 0)
return log_oom();
addr->source = NETWORK_CONFIG_SOURCE_DHCP4;
addr->provider.in = server;
addr->family = AF_INET;
addr->in_addr.in.s_addr = address.s_addr;
addr->lifetime_preferred_usec = lifetime_usec;
addr->lifetime_valid_usec = lifetime_usec;
addr->prefixlen = prefixlen;
r = sd_dhcp_lease_get_broadcast(link->dhcp_lease, &addr->broadcast);
if (r < 0 && r != -ENODATA)
return log_link_warning_errno(link, r, "DHCP: failed to get broadcast address: %m");
SET_FLAG(addr->flags, IFA_F_NOPREFIXROUTE, !link_prefixroute(link));
addr->route_metric = link->network->dhcp_route_metric;
addr->duplicate_address_detection = link->network->dhcp_send_decline ? ADDRESS_FAMILY_IPV4 : ADDRESS_FAMILY_NO;
r = free_and_strdup_warn(&addr->label, link->network->dhcp_label);
if (r < 0)
return r;
r = free_and_strdup_warn(&addr->netlabel, link->network->dhcp_netlabel);
if (r < 0)
return r;
if (address_get(link, addr, &existing) < 0) /* The address is new. */
link->dhcp4_configured = false;
else
address_unmark(existing);
r = link_request_address(link, addr, &link->dhcp4_messages,
dhcp4_address_handler, NULL);
if (r < 0)
return log_link_error_errno(link, r, "Failed to request DHCPv4 address: %m");
return 0;
}
static int dhcp4_request_address_and_routes(Link *link, bool announce) {
int r;
assert(link);
link_mark_addresses(link, NETWORK_CONFIG_SOURCE_DHCP4);
manager_mark_routes(link->manager, link, NETWORK_CONFIG_SOURCE_DHCP4);
r = dhcp4_request_address(link, announce);
if (r < 0)
return r;
r = dhcp4_request_routes(link);
if (r < 0)
return r;
if (!link->dhcp4_configured) {
link_set_state(link, LINK_STATE_CONFIGURING);
link_check_ready(link);
}
return 0;
}
static int dhcp_lease_renew(sd_dhcp_client *client, Link *link) {
_cleanup_(sd_dhcp_lease_unrefp) sd_dhcp_lease *old_lease = NULL;
sd_dhcp_lease *lease;
int r;
assert(link);
assert(link->network);
assert(client);
r = sd_dhcp_client_get_lease(client, &lease);
if (r < 0)
return log_link_warning_errno(link, r, "DHCP error: no lease: %m");
old_lease = TAKE_PTR(link->dhcp_lease);
link->dhcp_lease = sd_dhcp_lease_ref(lease);
link_dirty(link);
if (link->network->dhcp_use_6rd) {
if (sd_dhcp_lease_has_6rd(link->dhcp_lease)) {
r = dhcp4_pd_prefix_acquired(link);
if (r < 0)
return log_link_warning_errno(link, r, "Failed to process 6rd option: %m");
} else if (sd_dhcp_lease_has_6rd(old_lease))
dhcp4_pd_prefix_lost(link);
}
return dhcp4_request_address_and_routes(link, false);
}
static int dhcp_lease_acquired(sd_dhcp_client *client, Link *link) {
sd_dhcp_lease *lease;
int r;
assert(client);
assert(link);
r = sd_dhcp_client_get_lease(client, &lease);
if (r < 0)
return log_link_error_errno(link, r, "DHCP error: No lease: %m");
sd_dhcp_lease_unref(link->dhcp_lease);
link->dhcp_lease = sd_dhcp_lease_ref(lease);
link_dirty(link);
if (link->network->dhcp_use_mtu) {
uint16_t mtu;
r = sd_dhcp_lease_get_mtu(lease, &mtu);
if (r >= 0) {
r = link_request_to_set_mtu(link, mtu);
if (r < 0)
log_link_error_errno(link, r, "Failed to set MTU to %" PRIu16 ": %m", mtu);
}
}
if (link->network->dhcp_use_hostname) {
const char *dhcpname = NULL;
_cleanup_free_ char *hostname = NULL;
if (link->network->dhcp_hostname)
dhcpname = link->network->dhcp_hostname;
else
(void) sd_dhcp_lease_get_hostname(lease, &dhcpname);
if (dhcpname) {
r = shorten_overlong(dhcpname, &hostname);
if (r < 0)
log_link_warning_errno(link, r, "Unable to shorten overlong DHCP hostname '%s', ignoring: %m", dhcpname);
if (r == 1)
log_link_notice(link, "Overlong DHCP hostname received, shortened from '%s' to '%s'", dhcpname, hostname);
}
if (hostname) {
r = manager_set_hostname(link->manager, hostname);
if (r < 0)
log_link_error_errno(link, r, "Failed to set transient hostname to '%s': %m", hostname);
}
}
if (link->network->dhcp_use_timezone) {
const char *tz = NULL;
(void) sd_dhcp_lease_get_timezone(link->dhcp_lease, &tz);
if (tz) {
r = manager_set_timezone(link->manager, tz);
if (r < 0)
log_link_error_errno(link, r, "Failed to set timezone to '%s': %m", tz);
}
}
if (link->network->dhcp_use_6rd &&
sd_dhcp_lease_has_6rd(link->dhcp_lease)) {
r = dhcp4_pd_prefix_acquired(link);
if (r < 0)
return log_link_warning_errno(link, r, "Failed to process 6rd option: %m");
}
return dhcp4_request_address_and_routes(link, true);
}
static int dhcp_lease_ip_change(sd_dhcp_client *client, Link *link) {
int r;
r = dhcp_lease_acquired(client, link);
if (r < 0)
(void) dhcp4_lease_lost(link);
return r;
}
static int dhcp_server_is_filtered(Link *link, sd_dhcp_client *client) {
sd_dhcp_lease *lease;
struct in_addr addr;
int r;
assert(link);
assert(link->network);
assert(client);
r = sd_dhcp_client_get_lease(client, &lease);
if (r < 0)
return log_link_error_errno(link, r, "Failed to get DHCP lease: %m");
r = sd_dhcp_lease_get_server_identifier(lease, &addr);
if (r < 0)
return log_link_debug_errno(link, r, "Failed to get DHCP server IP address: %m");
if (in4_address_is_filtered(&addr, link->network->dhcp_allow_listed_ip, link->network->dhcp_deny_listed_ip)) {
if (link->network->dhcp_allow_listed_ip)
log_link_debug(link, "DHCPv4 server IP address "IPV4_ADDRESS_FMT_STR" not found in allow-list, ignoring offer.",
IPV4_ADDRESS_FMT_VAL(addr));
else
log_link_debug(link, "DHCPv4 server IP address "IPV4_ADDRESS_FMT_STR" found in deny-list, ignoring offer.",
IPV4_ADDRESS_FMT_VAL(addr));
return true;
}
return false;
}
static int dhcp4_handler(sd_dhcp_client *client, int event, void *userdata) {
Link *link = ASSERT_PTR(userdata);
int r;
assert(link->network);
assert(link->manager);
if (IN_SET(link->state, LINK_STATE_FAILED, LINK_STATE_LINGER))
return 0;
switch (event) {
case SD_DHCP_CLIENT_EVENT_STOP:
if (link->ipv4ll) {
log_link_debug(link, "DHCP client is stopped. Acquiring IPv4 link-local address");
if (in4_addr_is_set(&link->network->ipv4ll_start_address)) {
r = sd_ipv4ll_set_address(link->ipv4ll, &link->network->ipv4ll_start_address);
if (r < 0)
return log_link_warning_errno(link, r, "Could not set IPv4 link-local start address: %m");
}
r = sd_ipv4ll_start(link->ipv4ll);
if (r < 0 && r != -ESTALE) /* On exit, we cannot and should not start sd-ipv4ll. */
return log_link_warning_errno(link, r, "Could not acquire IPv4 link-local address: %m");
}
if (FLAGS_SET(link->network->keep_configuration, KEEP_CONFIGURATION_DHCP)) {
log_link_notice(link, "DHCPv4 connection considered critical, ignoring request to reconfigure it.");
return 0;
}
if (link->dhcp_lease) {
if (link->network->dhcp_send_release) {
r = sd_dhcp_client_send_release(client);
if (r < 0)
log_link_full_errno(link,
ERRNO_IS_DISCONNECT(r) ? LOG_DEBUG : LOG_WARNING,
r, "Failed to send DHCP RELEASE, ignoring: %m");
}
r = dhcp4_lease_lost(link);
if (r < 0) {
link_enter_failed(link);
return r;
}
}
break;
case SD_DHCP_CLIENT_EVENT_EXPIRED:
if (FLAGS_SET(link->network->keep_configuration, KEEP_CONFIGURATION_DHCP)) {
log_link_notice(link, "DHCPv4 connection considered critical, ignoring request to reconfigure it.");
return 0;
}
if (link->dhcp_lease) {
r = dhcp4_lease_lost(link);
if (r < 0) {
link_enter_failed(link);
return r;
}
}
break;
case SD_DHCP_CLIENT_EVENT_IP_CHANGE:
if (FLAGS_SET(link->network->keep_configuration, KEEP_CONFIGURATION_DHCP)) {
log_link_notice(link, "DHCPv4 connection considered critical, ignoring request to reconfigure it.");
return 0;
}
r = dhcp_lease_ip_change(client, link);
if (r < 0) {
link_enter_failed(link);
return r;
}
break;
case SD_DHCP_CLIENT_EVENT_RENEW:
r = dhcp_lease_renew(client, link);
if (r < 0) {
link_enter_failed(link);
return r;
}
break;
case SD_DHCP_CLIENT_EVENT_IP_ACQUIRE:
r = dhcp_lease_acquired(client, link);
if (r < 0) {
link_enter_failed(link);
return r;
}
break;
case SD_DHCP_CLIENT_EVENT_SELECTING:
r = dhcp_server_is_filtered(link, client);
if (r < 0) {
link_enter_failed(link);
return r;
}
if (r > 0)
return -ENOMSG;
break;
case SD_DHCP_CLIENT_EVENT_TRANSIENT_FAILURE:
if (link->ipv4ll && !sd_ipv4ll_is_running(link->ipv4ll)) {
log_link_debug(link, "Problems acquiring DHCP lease, acquiring IPv4 link-local address");
if (in4_addr_is_set(&link->network->ipv4ll_start_address)) {
r = sd_ipv4ll_set_address(link->ipv4ll, &link->network->ipv4ll_start_address);
if (r < 0)
return log_link_warning_errno(link, r, "Could not set IPv4 link-local start address: %m");
}
r = sd_ipv4ll_start(link->ipv4ll);
if (r < 0)
return log_link_warning_errno(link, r, "Could not acquire IPv4 link-local address: %m");
}
break;
default:
if (event < 0)
log_link_warning_errno(link, event, "DHCP error: Client failed: %m");
else
log_link_warning(link, "DHCP unknown event: %i", event);
break;
}
return 0;
}
static int dhcp4_set_hostname(Link *link) {
_cleanup_free_ char *hostname = NULL;
const char *hn;
int r;
assert(link);
if (!link->network->dhcp_send_hostname)
hn = NULL;
else if (link->network->dhcp_hostname)
hn = link->network->dhcp_hostname;
else {
r = gethostname_strict(&hostname);
if (r < 0 && r != -ENXIO) /* ENXIO: no hostname set or hostname is "localhost" */
return log_link_debug_errno(link, r, "DHCPv4 CLIENT: Failed to get hostname: %m");
hn = hostname;
}
r = sd_dhcp_client_set_hostname(link->dhcp_client, hn);
if (r == -EINVAL && hostname)
/* Ignore error when the machine's hostname is not suitable to send in DHCP packet. */
log_link_debug_errno(link, r, "DHCPv4 CLIENT: Failed to set hostname from kernel hostname, ignoring: %m");
else if (r < 0)
return log_link_debug_errno(link, r, "DHCPv4 CLIENT: Failed to set hostname: %m");
return 0;
}
static int dhcp4_set_client_identifier(Link *link) {
int r;
assert(link);
assert(link->network);
assert(link->dhcp_client);
switch (link->network->dhcp_client_identifier) {
case DHCP_CLIENT_ID_DUID: {
/* If configured, apply user specified DUID and IAID */
const DUID *duid = link_get_dhcp4_duid(link);
if (duid->raw_data_len == 0)
switch (duid->type) {
case DUID_TYPE_LLT:
r = sd_dhcp_client_set_iaid_duid_llt(link->dhcp_client,
link->network->dhcp_iaid_set,
link->network->dhcp_iaid,
duid->llt_time);
break;
case DUID_TYPE_LL:
r = sd_dhcp_client_set_iaid_duid_ll(link->dhcp_client,
link->network->dhcp_iaid_set,
link->network->dhcp_iaid);
break;
case DUID_TYPE_EN:
r = sd_dhcp_client_set_iaid_duid_en(link->dhcp_client,
link->network->dhcp_iaid_set,
link->network->dhcp_iaid);
break;
case DUID_TYPE_UUID:
r = sd_dhcp_client_set_iaid_duid_uuid(link->dhcp_client,
link->network->dhcp_iaid_set,
link->network->dhcp_iaid);
break;
default:
r = sd_dhcp_client_set_iaid_duid_raw(link->dhcp_client,
link->network->dhcp_iaid_set,
link->network->dhcp_iaid,
duid->type, NULL, 0);
}
else
r = sd_dhcp_client_set_iaid_duid_raw(link->dhcp_client,
link->network->dhcp_iaid_set,
link->network->dhcp_iaid,
duid->type, duid->raw_data, duid->raw_data_len);
if (r < 0)
return r;
break;
}
case DHCP_CLIENT_ID_MAC: {
const uint8_t *hw_addr = link->hw_addr.bytes;
size_t hw_addr_len = link->hw_addr.length;
if (link->iftype == ARPHRD_INFINIBAND && hw_addr_len == INFINIBAND_ALEN) {
/* set_client_id expects only last 8 bytes of an IB address */
hw_addr += INFINIBAND_ALEN - 8;
hw_addr_len -= INFINIBAND_ALEN - 8;
}
r = sd_dhcp_client_set_client_id(link->dhcp_client,
link->iftype,
hw_addr,
hw_addr_len);
if (r < 0)
return log_link_debug_errno(link, r, "DHCPv4 CLIENT: Failed to set client ID: %m");
break;
}
default:
assert_not_reached();
}
return 0;
}
static int dhcp4_find_dynamic_address(Link *link, struct in_addr *ret) {
Address *a;
assert(link);
assert(link->network);
assert(ret);
if (!FLAGS_SET(link->network->keep_configuration, KEEP_CONFIGURATION_DHCP))
return false;
SET_FOREACH(a, link->addresses) {
if (a->source != NETWORK_CONFIG_SOURCE_FOREIGN)
continue;
if (a->family != AF_INET)
continue;
if (link_address_is_dynamic(link, a))
break;
}
if (!a)
return false;
*ret = a->in_addr.in;
return true;
}
static int dhcp4_set_request_address(Link *link) {
struct in_addr a;
assert(link);
assert(link->network);
assert(link->dhcp_client);
a = link->network->dhcp_request_address;
if (in4_addr_is_null(&a))
(void) dhcp4_find_dynamic_address(link, &a);
if (in4_addr_is_null(&a))
return 0;
log_link_debug(link, "DHCPv4 CLIENT: requesting %s.", IN4_ADDR_TO_STRING(&a));
return sd_dhcp_client_set_request_address(link->dhcp_client, &a);
}
static bool link_needs_dhcp_broadcast(Link *link) {
int r;
assert(link);
assert(link->network);
/* Return the setting in DHCP[4].RequestBroadcast if specified. Otherwise return the device property
* ID_NET_DHCP_BROADCAST setting, which may be set for interfaces requiring that the DHCPOFFER
* message is being broadcast because they can't handle unicast messages while not fully configured.
* If neither is set or a failure occurs, return false, which is the default for this flag. */
r = link->network->dhcp_broadcast;
if (r >= 0)
return r;
if (!link->dev)
return false;
r = device_get_property_bool(link->dev, "ID_NET_DHCP_BROADCAST");
if (r < 0) {
if (r != -ENOENT)
log_link_warning_errno(link, r, "DHCPv4 CLIENT: Failed to get or parse ID_NET_DHCP_BROADCAST, ignoring: %m");
return false;
}
log_link_debug(link, "DHCPv4 CLIENT: Detected ID_NET_DHCP_BROADCAST='%d'.", r);
return r;
}
static bool link_dhcp4_ipv6_only_mode(Link *link) {
assert(link);
assert(link->network);
/* If it is explicitly specified, then honor the setting. */
if (link->network->dhcp_ipv6_only_mode >= 0)
return link->network->dhcp_ipv6_only_mode;
/* Defaults to false, until we support 464XLAT. See issue #30891. */
return false;
}
static int dhcp4_configure(Link *link) {
sd_dhcp_option *send_option;
void *request_options;
int r;
assert(link);
assert(link->network);
if (link->dhcp_client)
return log_link_debug_errno(link, SYNTHETIC_ERRNO(EBUSY), "DHCPv4 client is already configured.");
r = sd_dhcp_client_new(&link->dhcp_client, link->network->dhcp_anonymize);
if (r < 0)
return log_link_debug_errno(link, r, "DHCPv4 CLIENT: Failed to allocate DHCPv4 client: %m");
r = sd_dhcp_client_attach_event(link->dhcp_client, link->manager->event, 0);
if (r < 0)
return log_link_debug_errno(link, r, "DHCPv4 CLIENT: Failed to attach event to DHCPv4 client: %m");
r = sd_dhcp_client_attach_device(link->dhcp_client, link->dev);
if (r < 0)
return log_link_debug_errno(link, r, "DHCPv4 CLIENT: Failed to attach device: %m");
r = sd_dhcp_client_set_rapid_commit(link->dhcp_client, link->network->dhcp_use_rapid_commit);
if (r < 0)
return log_link_debug_errno(link, r, "DHCPv4 CLIENT: Failed to set rapid commit: %m");
r = sd_dhcp_client_set_mac(link->dhcp_client,
link->hw_addr.bytes,
link->bcast_addr.length > 0 ? link->bcast_addr.bytes : NULL,
link->hw_addr.length, link->iftype);
if (r < 0)
return log_link_debug_errno(link, r, "DHCPv4 CLIENT: Failed to set MAC address: %m");
r = sd_dhcp_client_set_ifindex(link->dhcp_client, link->ifindex);
if (r < 0)
return log_link_debug_errno(link, r, "DHCPv4 CLIENT: Failed to set ifindex: %m");
r = sd_dhcp_client_set_callback(link->dhcp_client, dhcp4_handler, link);
if (r < 0)
return log_link_debug_errno(link, r, "DHCPv4 CLIENT: Failed to set callback: %m");
r = sd_dhcp_client_set_request_broadcast(link->dhcp_client, link_needs_dhcp_broadcast(link));
if (r < 0)
return log_link_debug_errno(link, r, "DHCPv4 CLIENT: Failed to set request flag for broadcast: %m");
r = dhcp_client_set_state_callback(link->dhcp_client, dhcp_client_callback_bus, link);
if (r < 0)
return log_link_debug_errno(link, r, "DHCPv4 CLIENT: Failed to set state change callback: %m");
if (link->mtu > 0) {
r = sd_dhcp_client_set_mtu(link->dhcp_client, link->mtu);
if (r < 0)
return log_link_debug_errno(link, r, "DHCPv4 CLIENT: Failed to set MTU: %m");
}
if (!link->network->dhcp_anonymize) {
r = dhcp4_set_request_address(link);
if (r < 0)
return log_link_debug_errno(link, r, "DHCPv4 CLIENT: Failed to set initial DHCPv4 address: %m");
if (link->network->dhcp_use_mtu) {
r = sd_dhcp_client_set_request_option(link->dhcp_client, SD_DHCP_OPTION_MTU_INTERFACE);
if (r < 0)
return log_link_debug_errno(link, r, "DHCPv4 CLIENT: Failed to set request flag for MTU: %m");
}
if (link->network->dhcp_use_routes) {
r = sd_dhcp_client_set_request_option(link->dhcp_client, SD_DHCP_OPTION_STATIC_ROUTE);
if (r < 0)
return log_link_debug_errno(link, r, "DHCPv4 CLIENT: Failed to set request flag for static route: %m");
r = sd_dhcp_client_set_request_option(link->dhcp_client, SD_DHCP_OPTION_CLASSLESS_STATIC_ROUTE);
if (r < 0)
return log_link_debug_errno(link, r, "DHCPv4 CLIENT: Failed to set request flag for classless static route: %m");
}
if (link_get_use_domains(link, NETWORK_CONFIG_SOURCE_DHCP4) > 0) {
r = sd_dhcp_client_set_request_option(link->dhcp_client, SD_DHCP_OPTION_DOMAIN_SEARCH);
if (r < 0)
return log_link_debug_errno(link, r, "DHCPv4 CLIENT: Failed to set request flag for domain search list: %m");
}
if (link_get_use_ntp(link, NETWORK_CONFIG_SOURCE_DHCP4)) {
r = sd_dhcp_client_set_request_option(link->dhcp_client, SD_DHCP_OPTION_NTP_SERVER);
if (r < 0)
return log_link_debug_errno(link, r, "DHCPv4 CLIENT: Failed to set request flag for NTP server: %m");
}
if (link->network->dhcp_use_sip) {
r = sd_dhcp_client_set_request_option(link->dhcp_client, SD_DHCP_OPTION_SIP_SERVER);
if (r < 0)
return log_link_debug_errno(link, r, "DHCPv4 CLIENT: Failed to set request flag for SIP server: %m");
}
if (link->network->dhcp_use_captive_portal) {
r = sd_dhcp_client_set_request_option(link->dhcp_client, SD_DHCP_OPTION_DHCP_CAPTIVE_PORTAL);
if (r < 0)
return log_link_debug_errno(link, r, "DHCPv4 CLIENT: Failed to set request flag for captive portal: %m");
}
if (link->network->dhcp_use_timezone) {
r = sd_dhcp_client_set_request_option(link->dhcp_client, SD_DHCP_OPTION_TZDB_TIMEZONE);
if (r < 0)
return log_link_debug_errno(link, r, "DHCPv4 CLIENT: Failed to set request flag for timezone: %m");
}
if (link->network->dhcp_use_6rd) {
r = sd_dhcp_client_set_request_option(link->dhcp_client, SD_DHCP_OPTION_6RD);
if (r < 0)
return log_link_debug_errno(link, r, "DHCPv4 CLIENT: Failed to set request flag for 6rd: %m");
}
if (link_dhcp4_ipv6_only_mode(link)) {
r = sd_dhcp_client_set_request_option(link->dhcp_client, SD_DHCP_OPTION_IPV6_ONLY_PREFERRED);
if (r < 0)
return log_link_debug_errno(link, r, "DHCPv4 CLIENT: Failed to set request flag for IPv6-only preferred option: %m");
}
SET_FOREACH(request_options, link->network->dhcp_request_options) {
uint32_t option = PTR_TO_UINT32(request_options);
r = sd_dhcp_client_set_request_option(link->dhcp_client, option);
if (r < 0)
return log_link_debug_errno(link, r, "DHCPv4 CLIENT: Failed to set request flag for '%u': %m", option);
}
ORDERED_HASHMAP_FOREACH(send_option, link->network->dhcp_client_send_options) {
r = sd_dhcp_client_add_option(link->dhcp_client, send_option);
if (r == -EEXIST)
continue;
if (r < 0)
return log_link_debug_errno(link, r, "DHCPv4 CLIENT: Failed to set send option: %m");
}
ORDERED_HASHMAP_FOREACH(send_option, link->network->dhcp_client_send_vendor_options) {
r = sd_dhcp_client_add_vendor_option(link->dhcp_client, send_option);
if (r == -EEXIST)
continue;
if (r < 0)
return log_link_debug_errno(link, r, "DHCPv4 CLIENT: Failed to set send option: %m");
}
r = dhcp4_set_hostname(link);
if (r < 0)
return r;
if (link->network->dhcp_vendor_class_identifier) {
r = sd_dhcp_client_set_vendor_class_identifier(link->dhcp_client,
link->network->dhcp_vendor_class_identifier);
if (r < 0)
return log_link_debug_errno(link, r, "DHCPv4 CLIENT: Failed to set vendor class identifier: %m");
}
if (link->network->dhcp_mudurl) {
r = sd_dhcp_client_set_mud_url(link->dhcp_client, link->network->dhcp_mudurl);
if (r < 0)
return log_link_debug_errno(link, r, "DHCPv4 CLIENT: Failed to set MUD URL: %m");
}
if (link->network->dhcp_user_class) {
r = sd_dhcp_client_set_user_class(link->dhcp_client, link->network->dhcp_user_class);
if (r < 0)
return log_link_debug_errno(link, r, "DHCPv4 CLIENT: Failed to set user class: %m");
}
}
if (link->network->dhcp_client_port > 0) {
r = sd_dhcp_client_set_client_port(link->dhcp_client, link->network->dhcp_client_port);
if (r < 0)
return log_link_debug_errno(link, r, "DHCPv4 CLIENT: Failed to set listen port: %m");
}
if (link->network->dhcp_port > 0) {
r = sd_dhcp_client_set_port(link->dhcp_client, link->network->dhcp_port);
if (r < 0)
return log_link_debug_errno(link, r, "DHCPv4 CLIENT: Failed to set server port: %m");
}
if (link->network->dhcp_max_attempts > 0) {
r = sd_dhcp_client_set_max_attempts(link->dhcp_client, link->network->dhcp_max_attempts);
if (r < 0)
return log_link_debug_errno(link, r, "DHCPv4 CLIENT: Failed to set max attempts: %m");
}
if (link->network->dhcp_ip_service_type >= 0) {
r = sd_dhcp_client_set_service_type(link->dhcp_client, link->network->dhcp_ip_service_type);
if (r < 0)
return log_link_debug_errno(link, r, "DHCPv4 CLIENT: Failed to set IP service type: %m");
}
if (link->network->dhcp_socket_priority_set) {
r = sd_dhcp_client_set_socket_priority(link->dhcp_client, link->network->dhcp_socket_priority);
if (r < 0)
return log_link_debug_errno(link, r, "DHCPv4 CLIENT: Failed to set socket priority: %m");
}
if (link->network->dhcp_fallback_lease_lifetime_usec > 0) {
r = sd_dhcp_client_set_fallback_lease_lifetime(link->dhcp_client, link->network->dhcp_fallback_lease_lifetime_usec);
if (r < 0)
return log_link_debug_errno(link, r, "DHCPv4 CLIENT: Failed set to lease lifetime: %m");
}
return dhcp4_set_client_identifier(link);
}
int dhcp4_update_mac(Link *link) {
bool restart;
int r;
assert(link);
if (!link->dhcp_client)
return 0;
restart = sd_dhcp_client_is_running(link->dhcp_client);
r = sd_dhcp_client_stop(link->dhcp_client);
if (r < 0)
return r;
r = sd_dhcp_client_set_mac(link->dhcp_client,
link->hw_addr.bytes,
link->bcast_addr.length > 0 ? link->bcast_addr.bytes : NULL,
link->hw_addr.length, link->iftype);
if (r < 0)
return r;
r = dhcp4_set_client_identifier(link);
if (r < 0)
return r;
if (restart) {
r = dhcp4_start(link);
if (r < 0)
return r;
}
return 0;
}
int dhcp4_update_ipv6_connectivity(Link *link) {
assert(link);
if (!link->network)
return 0;
if (!link->network->dhcp_ipv6_only_mode)
return 0;
if (!link->dhcp_client)
return 0;
/* If the client is running, set the current connectivity. */
if (sd_dhcp_client_is_running(link->dhcp_client))
return sd_dhcp_client_set_ipv6_connectivity(link->dhcp_client, link_has_ipv6_connectivity(link));
/* If the client has been already stopped or not started yet, let's check the current connectivity
* and start the client if necessary. */
if (link_has_ipv6_connectivity(link))
return 0;
return dhcp4_start_full(link, /* set_ipv6_connectivity = */ false);
}
int dhcp4_start_full(Link *link, bool set_ipv6_connectivity) {
int r;
assert(link);
assert(link->network);
if (!link->dhcp_client)
return 0;
if (!link_has_carrier(link))
return 0;
if (sd_dhcp_client_is_running(link->dhcp_client) > 0)
return 0;
r = sd_dhcp_client_start(link->dhcp_client);
if (r < 0)
return r;
if (set_ipv6_connectivity) {
r = dhcp4_update_ipv6_connectivity(link);
if (r < 0)
return r;
}
return 1;
}
int dhcp4_renew(Link *link) {
assert(link);
if (!link->dhcp_client)
return 0;
/* The DHCPv4 client may have been stopped by the IPv6 only mode. Let's unconditionally restart the
* client if it is not running. */
if (!sd_dhcp_client_is_running(link->dhcp_client))
return dhcp4_start(link);
/* The client may be waiting for IPv6 connectivity. Let's restart the client in that case. */
if (dhcp_client_get_state(link->dhcp_client) != DHCP_STATE_BOUND)
return sd_dhcp_client_interrupt_ipv6_only_mode(link->dhcp_client);
/* Otherwise, send a RENEW command. */
return sd_dhcp_client_send_renew(link->dhcp_client);
}
static int dhcp4_configure_duid(Link *link) {
assert(link);
assert(link->network);
if (link->network->dhcp_client_identifier != DHCP_CLIENT_ID_DUID)
return 1;
return dhcp_configure_duid(link, link_get_dhcp4_duid(link));
}
static int dhcp4_process_request(Request *req, Link *link, void *userdata) {
int r;
assert(link);
if (!link_is_ready_to_configure(link, /* allow_unmanaged = */ false))
return 0;
r = dhcp4_configure_duid(link);
if (r <= 0)
return r;
r = dhcp4_configure(link);
if (r < 0)
return log_link_warning_errno(link, r, "Failed to configure DHCPv4 client: %m");
r = dhcp4_start(link);
if (r < 0)
return log_link_warning_errno(link, r, "Failed to start DHCPv4 client: %m");
log_link_debug(link, "DHCPv4 client is configured%s.",
r > 0 ? ", acquiring DHCPv4 lease" : "");
return 1;
}
int link_request_dhcp4_client(Link *link) {
int r;
assert(link);
if (!link_dhcp4_enabled(link))
return 0;
if (link->dhcp_client)
return 0;
r = link_queue_request(link, REQUEST_TYPE_DHCP4_CLIENT, dhcp4_process_request, NULL);
if (r < 0)
return log_link_warning_errno(link, r, "Failed to request configuring of the DHCPv4 client: %m");
log_link_debug(link, "Requested configuring of the DHCPv4 client.");
return 0;
}
int config_parse_dhcp_max_attempts(
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(data);
uint64_t a;
int r;
assert(lvalue);
assert(rvalue);
if (isempty(rvalue)) {
network->dhcp_max_attempts = 0;
return 0;
}
if (streq(rvalue, "infinity")) {
network->dhcp_max_attempts = UINT64_MAX;
return 0;
}
r = safe_atou64(rvalue, &a);
if (r < 0) {
log_syntax(unit, LOG_WARNING, filename, line, r,
"Failed to parse DHCP maximum attempts, ignoring: %s", rvalue);
return 0;
}
if (a == 0) {
log_syntax(unit, LOG_WARNING, filename, line, 0,
"%s= must be positive integer or 'infinity', ignoring: %s", lvalue, rvalue);
return 0;
}
network->dhcp_max_attempts = a;
return 0;
}
int config_parse_dhcp_ip_service_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) {
int *tos = ASSERT_PTR(data);
assert(filename);
assert(lvalue);
assert(rvalue);
if (isempty(rvalue))
*tos = -1; /* use sd_dhcp_client's default (currently, CS6). */
else if (streq(rvalue, "none"))
*tos = 0;
else if (streq(rvalue, "CS4"))
*tos = IPTOS_CLASS_CS4;
else if (streq(rvalue, "CS6"))
*tos = IPTOS_CLASS_CS6;
else
log_syntax(unit, LOG_WARNING, filename, line, 0,
"Failed to parse %s=, ignoring assignment: %s", lvalue, rvalue);
return 0;
}
int config_parse_dhcp_socket_priority(
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(data);
int a, r;
assert(lvalue);
assert(rvalue);
if (isempty(rvalue)) {
network->dhcp_socket_priority_set = false;
return 0;
}
r = safe_atoi(rvalue, &a);
if (r < 0) {
log_syntax(unit, LOG_WARNING, filename, line, r,
"Failed to parse socket priority, ignoring: %s", rvalue);
return 0;
}
network->dhcp_socket_priority_set = true;
network->dhcp_socket_priority = a;
return 0;
}
int config_parse_dhcp_fallback_lease_lifetime(
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;
assert(filename);
assert(section);
assert(lvalue);
assert(rvalue);
assert(data);
if (isempty(rvalue)) {
network->dhcp_fallback_lease_lifetime_usec = 0;
return 0;
}
/* We accept only "forever" or "infinity". */
if (!STR_IN_SET(rvalue, "forever", "infinity")) {
log_syntax(unit, LOG_WARNING, filename, line, 0,
"Invalid LeaseLifetime= value, ignoring: %s", rvalue);
return 0;
}
network->dhcp_fallback_lease_lifetime_usec = USEC_INFINITY;
return 0;
}
int config_parse_dhcp_label(
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 **label = ASSERT_PTR(data);
assert(filename);
assert(lvalue);
assert(rvalue);
if (isempty(rvalue)) {
*label = mfree(*label);
return 0;
}
if (!address_label_valid(rvalue)) {
log_syntax(unit, LOG_WARNING, filename, line, 0,
"Address label is too long or invalid, ignoring assignment: %s", rvalue);
return 0;
}
return free_and_strdup_warn(label, rvalue);
}
static const char* const dhcp_client_identifier_table[_DHCP_CLIENT_ID_MAX] = {
[DHCP_CLIENT_ID_MAC] = "mac",
[DHCP_CLIENT_ID_DUID] = "duid",
};
DEFINE_PRIVATE_STRING_TABLE_LOOKUP_FROM_STRING(dhcp_client_identifier, DHCPClientIdentifier);
DEFINE_CONFIG_PARSE_ENUM(config_parse_dhcp_client_identifier, dhcp_client_identifier, DHCPClientIdentifier);
|