summaryrefslogtreecommitdiff
path: root/src/core/dhcp/nm-dhcp-client.c
blob: b10ce41063de4a084e147123f2b2274f33362231 (plain)
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
/* SPDX-License-Identifier: GPL-2.0-or-later */
/*
 * Copyright (C) 2005 - 2010 Red Hat, Inc.
 */

#include "src/core/nm-default-daemon.h"

#include "nm-dhcp-client.h"

#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <linux/rtnetlink.h>
#include <linux/if_ether.h>

#include "libnm-glib-aux/nm-dedup-multi.h"
#include "libnm-glib-aux/nm-random-utils.h"

#include "NetworkManagerUtils.h"
#include "nm-utils.h"
#include "nm-l3cfg.h"
#include "nm-l3-config-data.h"
#include "nm-dhcp-utils.h"
#include "nm-dhcp-options.h"
#include "libnm-platform/nm-platform.h"
#include "nm-hostname-manager.h"
#include "libnm-systemd-shared/nm-sd-utils-shared.h"

#include "nm-dhcp-client-logging.h"

/*****************************************************************************/

/* This is how long we do ACD for each entry and reject new offers for
 * the same address. Note that the maximum ACD timeout is limited to 30 seconds
 * (NM_ACD_TIMEOUT_MAX_MSEC).
 **/
#define ACD_REGLIST_GRACE_PERIOD_MSEC 300000u

G_STATIC_ASSERT(ACD_REGLIST_GRACE_PERIOD_MSEC > (NM_ACD_TIMEOUT_MAX_MSEC + 1000));

#define ACD_REGLIST_MAX_ENTRIES 30

/* To do ACD for an address (new lease), we will register a NML3ConfigData
 * with l3cfg. After ACD completes, we still continue having NML3Cfg
 * watch that address, for ACD_REGLIST_GRACE_PERIOD_MSEC. The reasons are:
 *
 * - the caller is supposed to actually configure the address right after
 *   ACD passed. We would not want to drop the ACD state before the caller
 *   got a chance to do that.
 * - when ACD fails, we decline the address and expect the DHCP client
 *   to present a new lease. We may want to outright reject the address,
 *   if ACD is bad. Thus, we want to keep running ACD for the address a bit
 *   longer, so that future requests for the same address can be rejected.
 *
 * This data structure is used for tracking the registered ACD address.
 */
typedef struct {
    const NML3ConfigData *l3cd;
    gint64                expiry_msec;
    in_addr_t             addr;
} AcdRegListData;

/*****************************************************************************/

enum {
    SIGNAL_NOTIFY,
    LAST_SIGNAL,
};

static guint signals[LAST_SIGNAL] = {0};

NM_GOBJECT_PROPERTIES_DEFINE(NMDhcpClient, PROP_CONFIG, );

typedef struct _NMDhcpClientPrivate {
    NMDhcpClientConfig config;

    /* This is the "next" data. That is, the one what was received last via
     * _nm_dhcp_client_notify(), but which is currently pending on ACD. */
    const NML3ConfigData *l3cd_next;

    /* This is the currently exposed data. It passed ACD (or no ACD was performed),
     * and is set from l3cd_next. */
    const NML3ConfigData *l3cd_curr;

    GSource *previous_lease_timeout_source;
    GSource *no_lease_timeout_source;
    GSource *watch_source;
    GBytes  *effective_client_id;

    union {
        struct {
            struct {
                NML3CfgCommitTypeHandle *l3cfg_commit_handle;
                GSource                 *done_source;

                /* When we do ACD for a l3cd lease, we will keep running ACD for
                 * the grace period ACD_REGLIST_GRACE_PERIOD_MSEC, even if we already
                 * determined the state. There are two reasons for that:
                 *
                 * - after ACD completes we notify the lease to the user, who is supposed
                 *   to configure the address in NML3Cfg. If we were already removing the
                 *   ACD state from NML3Cfg, ACD might need to start over. Instead, when
                 *   the caller tries to configure the address, ACD state is already good.
                 *
                 * - if we decline on ACD offer, we may want to keep running and
                 *   select other offers. Offers for which we just failed ACD (within
                 *   ACD_REGLIST_GRACE_PERIOD_MSEC) are rejected. See _nm_dhcp_client_accept_offer().
                 *   For that, we keep monitoring the ACD state for up to ACD_REGLIST_MAX_ENTRIES
                 *   addresses, to not restart and select the same lease twice in a row.
                 */
                GArray  *reglist;
                GSource *reglist_timeout_source;

                in_addr_t    addr;
                NMOptionBool state;
            } acd;
        } v4;
        struct {
            GSource *lladdr_timeout_source;
            GSource *dad_timeout_source;
        } v6;
    };

    GDBusMethodInvocation *invocation;

    struct {
        gulong id;
        bool   wait_dhcp_commit : 1;
        bool   wait_ipv6_dad : 1;
        bool   wait_ll_address : 1;
    } l3cfg_notify;

    pid_t pid;
    bool  is_stopped : 1;
} NMDhcpClientPrivate;

G_DEFINE_ABSTRACT_TYPE(NMDhcpClient, nm_dhcp_client, G_TYPE_OBJECT)

#define NM_DHCP_CLIENT_GET_PRIVATE(self) _NM_GET_PRIVATE_PTR(self, NMDhcpClient, NM_IS_DHCP_CLIENT)

/*****************************************************************************/

#define L3CD_ACD_TAG(priv) (&(priv)->v4.acd.addr)

static gboolean _dhcp_client_accept(NMDhcpClient *self, const NML3ConfigData *l3cd, GError **error);

static gboolean _dhcp_client_decline(NMDhcpClient         *self,
                                     const NML3ConfigData *l3cd,
                                     const char           *error_message,
                                     GError              **error);

static void
l3_cfg_notify_cb(NML3Cfg *l3cfg, const NML3ConfigNotifyData *notify_data, NMDhcpClient *self);

static void _acd_reglist_timeout_reschedule(NMDhcpClient *self, gint64 now_msec);

static void _acd_reglist_data_remove(NMDhcpClient *self, guint idx, gboolean do_log);

/*****************************************************************************/

/* we use pid=-1 for invalid PIDs. Ensure that pid_t can hold negative values. */
G_STATIC_ASSERT(!(((pid_t) -1) > 0));

/*****************************************************************************/

NM_UTILS_LOOKUP_STR_DEFINE(nm_dhcp_client_event_type_to_string,
                           NMDhcpClientEventType,
                           NM_UTILS_LOOKUP_DEFAULT_NM_ASSERT(NULL),
                           NM_UTILS_LOOKUP_STR_ITEM(NM_DHCP_CLIENT_EVENT_TYPE_BOUND, "bound"),
                           NM_UTILS_LOOKUP_STR_ITEM(NM_DHCP_CLIENT_EVENT_TYPE_EXPIRE, "expire"),
                           NM_UTILS_LOOKUP_STR_ITEM(NM_DHCP_CLIENT_EVENT_TYPE_EXTENDED, "extended"),
                           NM_UTILS_LOOKUP_STR_ITEM(NM_DHCP_CLIENT_EVENT_TYPE_FAIL, "fail"),
                           NM_UTILS_LOOKUP_STR_ITEM(NM_DHCP_CLIENT_EVENT_TYPE_TERMINATED,
                                                    "terminated"),
                           NM_UTILS_LOOKUP_STR_ITEM(NM_DHCP_CLIENT_EVENT_TYPE_TIMEOUT, "timeout"),
                           NM_UTILS_LOOKUP_STR_ITEM(NM_DHCP_CLIENT_EVENT_TYPE_UNSPECIFIED,
                                                    "unspecified"), );

/*****************************************************************************/

int
nm_dhcp_client_get_addr_family(NMDhcpClient *self)
{
    NMDhcpClientPrivate *priv = NM_DHCP_CLIENT_GET_PRIVATE(self);

    return priv->config.addr_family;
}

const char *
nm_dhcp_client_get_iface(NMDhcpClient *self)
{
    NMDhcpClientPrivate *priv = NM_DHCP_CLIENT_GET_PRIVATE(self);

    return priv->config.iface;
}

NMDedupMultiIndex *
nm_dhcp_client_get_multi_idx(NMDhcpClient *self)
{
    NMDhcpClientPrivate *priv = NM_DHCP_CLIENT_GET_PRIVATE(self);

    return nm_l3cfg_get_multi_idx(priv->config.l3cfg);
}

int
nm_dhcp_client_get_ifindex(NMDhcpClient *self)
{
    NMDhcpClientPrivate *priv = NM_DHCP_CLIENT_GET_PRIVATE(self);

    return nm_l3cfg_get_ifindex(priv->config.l3cfg);
}

const NMDhcpClientConfig *
nm_dhcp_client_get_config(NMDhcpClient *self)
{
    NMDhcpClientPrivate *priv = NM_DHCP_CLIENT_GET_PRIVATE(self);

    return &priv->config;
}

GBytes *
nm_dhcp_client_get_effective_client_id(NMDhcpClient *self)
{
    NMDhcpClientPrivate *priv = NM_DHCP_CLIENT_GET_PRIVATE(self);

    return priv->effective_client_id;
}

NML3ConfigData *
nm_dhcp_client_create_l3cd(NMDhcpClient *self)
{
    NMDhcpClientPrivate *priv = NM_DHCP_CLIENT_GET_PRIVATE(self);

    return nm_l3_config_data_new(nm_l3cfg_get_multi_idx(priv->config.l3cfg),
                                 nm_l3cfg_get_ifindex(priv->config.l3cfg),
                                 NM_IP_CONFIG_SOURCE_DHCP);
}

GHashTable *
nm_dhcp_client_create_options_dict(NMDhcpClient *self, gboolean static_keys)
{
    NMDhcpClientPrivate *priv    = NM_DHCP_CLIENT_GET_PRIVATE(self);
    const int            IS_IPv4 = NM_IS_IPv4(priv->config.addr_family);
    GHashTable          *options;
    GBytes              *effective_client_id;

    options = nm_dhcp_option_create_options_dict(static_keys);

    effective_client_id = nm_dhcp_client_get_effective_client_id(self);
    if (effective_client_id) {
        guint option = IS_IPv4 ? NM_DHCP_OPTION_DHCP4_CLIENT_ID : NM_DHCP_OPTION_DHCP6_CLIENT_ID;
        gs_free char *str = nm_dhcp_utils_duid_to_string(effective_client_id);

        /* Note that for the nm-dhcp-helper based plugins (dhclient), the plugin
         * may send the used client-id/DUID via the environment variables and
         * overwrite them yet again. */

        nm_dhcp_option_take_option(options,
                                   static_keys,
                                   priv->config.addr_family,
                                   option,
                                   g_steal_pointer(&str));
    }

    return options;
}

const NML3ConfigData *
nm_dhcp_client_get_lease(NMDhcpClient *self)
{
    return NM_DHCP_CLIENT_GET_PRIVATE(self)->l3cd_curr;
}

/*****************************************************************************/

gboolean
nm_dhcp_client_set_effective_client_id(NMDhcpClient *self, GBytes *client_id)
{
    NMDhcpClientPrivate   *priv              = NM_DHCP_CLIENT_GET_PRIVATE(self);
    gs_free char          *tmp_str           = NULL;
    gs_unref_bytes GBytes *client_id_to_free = NULL;

    g_return_val_if_fail(NM_IS_DHCP_CLIENT(self), FALSE);
    g_return_val_if_fail(!client_id || g_bytes_get_size(client_id) >= 2, FALSE);

    priv = NM_DHCP_CLIENT_GET_PRIVATE(self);

    if (nm_g_bytes_equal0(priv->effective_client_id, client_id))
        return FALSE;

    client_id_to_free         = g_steal_pointer(&priv->effective_client_id);
    priv->effective_client_id = nm_g_bytes_ref(client_id);

    _LOGT("%s: set effective %s",
          priv->config.addr_family == AF_INET6 ? "duid" : "client-id",
          priv->effective_client_id
              ? (tmp_str = nm_dhcp_utils_duid_to_string(priv->effective_client_id))
              : "default");

    return TRUE;
}

/*****************************************************************************/

static void
_emit_notify_data(NMDhcpClient *self, const NMDhcpClientNotifyData *notify_data)
{
    g_signal_emit(G_OBJECT(self), signals[SIGNAL_NOTIFY], 0, notify_data);
}

#define _emit_notify(self, _notify_type, ...) \
    _emit_notify_data(                        \
        (self),                               \
        &((const NMDhcpClientNotifyData){.notify_type = (_notify_type), __VA_ARGS__}))

/*****************************************************************************/

static void
l3_cfg_notify_check_connected(NMDhcpClient *self)
{
    NMDhcpClientPrivate *priv = NM_DHCP_CLIENT_GET_PRIVATE(self);
    gboolean             do_connect;

    do_connect = priv->l3cfg_notify.wait_dhcp_commit | priv->l3cfg_notify.wait_ll_address
                 | priv->l3cfg_notify.wait_ipv6_dad
                 | (NM_IS_IPv4(priv->config.addr_family) && priv->v4.acd.l3cfg_commit_handle);

    if (!do_connect) {
        nm_clear_g_signal_handler(priv->config.l3cfg, &priv->l3cfg_notify.id);
        return;
    }

    if (priv->l3cfg_notify.id == 0) {
        priv->l3cfg_notify.id = g_signal_connect(priv->config.l3cfg,
                                                 NM_L3CFG_SIGNAL_NOTIFY,
                                                 G_CALLBACK(l3_cfg_notify_cb),
                                                 self);
    }
}

/*****************************************************************************/

pid_t
nm_dhcp_client_get_pid(NMDhcpClient *self)
{
    g_return_val_if_fail(NM_IS_DHCP_CLIENT(self), -1);

    return NM_DHCP_CLIENT_GET_PRIVATE(self)->pid;
}

static void
watch_cleanup(NMDhcpClient *self)
{
    NMDhcpClientPrivate *priv = NM_DHCP_CLIENT_GET_PRIVATE(self);

    nm_clear_g_source_inst(&priv->watch_source);
}

void
nm_dhcp_client_stop_pid(pid_t pid, const char *iface)
{
    char *name = iface ? g_strdup_printf("dhcp-client-%s", iface) : NULL;

    g_return_if_fail(pid > 1);

    nm_utils_kill_child_sync(pid,
                             SIGTERM,
                             LOGD_DHCP,
                             name ?: "dhcp-client",
                             NULL,
                             1000 / 2,
                             1000 / 20);
    g_free(name);
}

static void
stop(NMDhcpClient *self, gboolean release)
{
    NMDhcpClientPrivate *priv;

    g_return_if_fail(NM_IS_DHCP_CLIENT(self));

    priv = NM_DHCP_CLIENT_GET_PRIVATE(self);

    if (priv->pid > 0) {
        /* Clean up the watch handler since we're explicitly killing the daemon */
        watch_cleanup(self);
        nm_dhcp_client_stop_pid(priv->pid, priv->config.iface);
    }
    priv->pid = -1;
}

/*****************************************************************************/

static gboolean
_no_lease_timeout(gpointer user_data)
{
    NMDhcpClient        *self = user_data;
    NMDhcpClientPrivate *priv = NM_DHCP_CLIENT_GET_PRIVATE(self);

    nm_clear_g_source_inst(&priv->no_lease_timeout_source);
    _emit_notify(self, NM_DHCP_CLIENT_NOTIFY_TYPE_NO_LEASE_TIMEOUT);
    return G_SOURCE_CONTINUE;
}

static void
_no_lease_timeout_schedule(NMDhcpClient *self)
{
    NMDhcpClientPrivate *priv = NM_DHCP_CLIENT_GET_PRIVATE(self);

    if (priv->no_lease_timeout_source)
        return;

    if (priv->config.timeout == NM_DHCP_TIMEOUT_INFINITY) {
        _LOGI("activation: beginning transaction (no timeout)");
        priv->no_lease_timeout_source = g_source_ref(nm_g_source_sentinel_get(0));
    } else {
        _LOGI("activation: beginning transaction (timeout in %u seconds)",
              (guint) priv->config.timeout);
        priv->no_lease_timeout_source =
            nm_g_timeout_add_seconds_source(priv->config.timeout, _no_lease_timeout, self);
    }
}

/*****************************************************************************/

static void
_acd_state_reset(NMDhcpClient *self, gboolean forget_addr, gboolean forget_reglist)
{
    NMDhcpClientPrivate *priv = NM_DHCP_CLIENT_GET_PRIVATE(self);

    if (!NM_IS_IPv4(priv->config.addr_family))
        return;

    if (priv->v4.acd.addr != INADDR_ANY) {
        nm_l3cfg_commit_type_clear(priv->config.l3cfg, &priv->v4.acd.l3cfg_commit_handle);
        l3_cfg_notify_check_connected(self);
        nm_clear_g_source_inst(&priv->v4.acd.done_source);
        if (forget_addr) {
            priv->v4.acd.addr  = INADDR_ANY;
            priv->v4.acd.state = NM_OPTION_BOOL_DEFAULT;
        }
    } else
        nm_assert(priv->v4.acd.state == NM_OPTION_BOOL_DEFAULT);

    if (forget_reglist) {
        guint n;

        while ((n = nm_g_array_len(priv->v4.acd.reglist)) > 0)
            _acd_reglist_data_remove(self, n - 1, TRUE);
    }

    nm_assert(!priv->v4.acd.l3cfg_commit_handle);
    nm_assert(!priv->v4.acd.done_source);
    nm_assert(!forget_reglist
              || !nm_l3cfg_remove_config_all(priv->config.l3cfg, L3CD_ACD_TAG(priv)));
}

static gboolean
_acd_complete_on_idle_cb(gpointer user_data)
{
    NMDhcpClient        *self = user_data;
    NMDhcpClientPrivate *priv = NM_DHCP_CLIENT_GET_PRIVATE(self);

    nm_assert(NM_IS_IPv4(priv->config.addr_family));
    nm_assert(priv->v4.acd.addr != INADDR_ANY);
    nm_assert(!priv->v4.acd.l3cfg_commit_handle);
    nm_assert(priv->l3cd_next);

    _acd_state_reset(self, FALSE, FALSE);

    _nm_dhcp_client_notify(self, NM_DHCP_CLIENT_EVENT_TYPE_BOUND, priv->l3cd_next);

    return G_SOURCE_CONTINUE;
}

#define _acd_reglist_data_get(priv, idx) \
    (&nm_g_array_index((priv)->v4.acd.reglist, AcdRegListData, (idx)))

static guint
_acd_reglist_data_find(NMDhcpClientPrivate *priv, in_addr_t addr_needle)
{
    const guint n = nm_g_array_len(priv->v4.acd.reglist);
    guint       i;

    nm_assert(addr_needle != INADDR_ANY);

    for (i = 0; i < n; i++) {
        AcdRegListData *reglist_data = _acd_reglist_data_get(priv, i);

        if (reglist_data->addr == addr_needle)
            return i;
    }
    return G_MAXUINT;
}

static void
_acd_reglist_data_remove(NMDhcpClient *self, guint idx, gboolean do_log)
{
    NMDhcpClientPrivate *priv = NM_DHCP_CLIENT_GET_PRIVATE(self);
    AcdRegListData      *reglist_data;

    nm_assert(idx < nm_g_array_len(priv->v4.acd.reglist));

    reglist_data = _acd_reglist_data_get(priv, idx);

    if (do_log) {
        char sbuf_addr[NM_INET_ADDRSTRLEN];

        _LOGD("acd: drop check for address %s (l3cd " NM_HASH_OBFUSCATE_PTR_FMT ")",
              nm_inet4_ntop(reglist_data->addr, sbuf_addr),
              NM_HASH_OBFUSCATE_PTR(reglist_data->l3cd));
    }

    if (!nm_l3cfg_remove_config(priv->config.l3cfg, L3CD_ACD_TAG(priv), reglist_data->l3cd))
        nm_assert_not_reached();

    nm_clear_l3cd(&reglist_data->l3cd);

    nm_l3cfg_commit_on_idle_schedule(priv->config.l3cfg, NM_L3_CFG_COMMIT_TYPE_UPDATE);

    g_array_remove_index(priv->v4.acd.reglist, idx);

    if (priv->v4.acd.reglist->len == 0) {
        nm_clear_pointer(&priv->v4.acd.reglist, g_array_unref);
        nm_clear_g_source_inst(&priv->v4.acd.reglist_timeout_source);
    }
}

static gboolean
_acd_reglist_timeout_cb(gpointer user_data)
{
    NMDhcpClient        *self = user_data;
    NMDhcpClientPrivate *priv = NM_DHCP_CLIENT_GET_PRIVATE(self);
    gint64               now_msec;

    nm_clear_g_source_inst(&priv->v4.acd.reglist_timeout_source);

    now_msec = nm_utils_get_monotonic_timestamp_msec();

    while (nm_g_array_len(priv->v4.acd.reglist) > 0) {
        AcdRegListData *reglist_data = _acd_reglist_data_get(priv, 0);

        if (reglist_data->expiry_msec > now_msec)
            break;

        _acd_reglist_data_remove(self, 0, TRUE);
    }

    _acd_reglist_timeout_reschedule(self, now_msec);

    return G_SOURCE_CONTINUE;
}

static void
_acd_reglist_timeout_reschedule(NMDhcpClient *self, gint64 now_msec)
{
    NMDhcpClientPrivate *priv = NM_DHCP_CLIENT_GET_PRIVATE(self);
    AcdRegListData      *reglist_data;

    if (nm_g_array_len(priv->v4.acd.reglist) == 0) {
        nm_assert(!priv->v4.acd.reglist_timeout_source);
        return;
    }

    if (priv->v4.acd.reglist_timeout_source) {
        /* already pending. As we only add new elements with a *later*
          * expiry, we don't need to ever cancel a pending timer. Worst
          * case, the timer fires, and there is nothing to do and we
          * reschedule. */
        return;
    }

    now_msec = nm_utils_get_monotonic_timestamp_msec();

    reglist_data = _acd_reglist_data_get(priv, 0);

    nm_assert(reglist_data->expiry_msec > now_msec);

    priv->v4.acd.reglist_timeout_source =
        nm_g_timeout_add_source(reglist_data->expiry_msec - now_msec,
                                _acd_reglist_timeout_cb,
                                self);
}

static void
_acd_check_lease(NMDhcpClient *self, NMOptionBool *out_acd_state)
{
    NMDhcpClientPrivate *priv = NM_DHCP_CLIENT_GET_PRIVATE(self);
    char                 sbuf_addr[NM_INET_ADDRSTRLEN];
    in_addr_t            addr;
    gboolean             addr_changed = FALSE;
    guint                idx;
    gint64               now_msec;

    if (!NM_IS_IPv4(priv->config.addr_family))
        goto handle_no_acd;

    if (!priv->l3cd_next)
        goto handle_no_acd;

    /* an IPv4 lease is always expected to have exactly one address. */
    nm_assert(nm_l3_config_data_get_num_addresses(priv->l3cd_next, AF_INET) == 1);

    if (priv->config.v4.acd_timeout_msec == 0)
        goto handle_no_acd;

    addr = NMP_OBJECT_CAST_IP4_ADDRESS(
               nm_l3_config_data_get_first_obj(priv->l3cd_next, NMP_OBJECT_TYPE_IP4_ADDRESS, NULL))
               ->address;
    nm_assert(addr != INADDR_ANY);

    nm_clear_g_source_inst(&priv->v4.acd.done_source);

    if (priv->v4.acd.state != NM_OPTION_BOOL_DEFAULT && priv->v4.acd.addr == addr) {
        /* the ACD state is already determined. Return right away. */
        nm_assert(!priv->v4.acd.l3cfg_commit_handle);
        *out_acd_state = !!priv->v4.acd.state;
        return;
    }

    if (priv->v4.acd.addr != addr) {
        addr_changed      = TRUE;
        priv->v4.acd.addr = addr;
    }

    _LOGD("acd: %s check for address %s (timeout %u msec, l3cd " NM_HASH_OBFUSCATE_PTR_FMT ")",
          addr_changed ? "add" : "update",
          nm_inet4_ntop(addr, sbuf_addr),
          priv->config.v4.acd_timeout_msec,
          NM_HASH_OBFUSCATE_PTR(priv->l3cd_next));

    priv->v4.acd.state = NM_OPTION_BOOL_DEFAULT;

    if (nm_l3cfg_add_config(priv->config.l3cfg,
                            L3CD_ACD_TAG(priv),
                            FALSE,
                            priv->l3cd_next,
                            NM_L3CFG_CONFIG_PRIORITY_IPV4LL,
                            0,
                            0,
                            NM_PLATFORM_ROUTE_METRIC_DEFAULT_IP4,
                            NM_PLATFORM_ROUTE_METRIC_DEFAULT_IP6,
                            0,
                            0,
                            NM_DNS_PRIORITY_DEFAULT_NORMAL,
                            NM_DNS_PRIORITY_DEFAULT_NORMAL,
                            NM_L3_ACD_DEFEND_TYPE_ONCE,
                            NM_MIN(priv->config.v4.acd_timeout_msec, NM_ACD_TIMEOUT_MAX_MSEC),
                            NM_L3CFG_CONFIG_FLAGS_ONLY_FOR_ACD,
                            NM_L3_CONFIG_MERGE_FLAGS_NONE))
        addr_changed = TRUE;

    if (!priv->v4.acd.reglist)
        priv->v4.acd.reglist = g_array_new(FALSE, FALSE, sizeof(AcdRegListData));

    idx = _acd_reglist_data_find(priv, addr);

    now_msec = nm_utils_get_monotonic_timestamp_msec();

    g_array_append_val(priv->v4.acd.reglist,
                       ((AcdRegListData){
                           .l3cd        = nm_l3_config_data_ref(priv->l3cd_next),
                           .addr        = addr,
                           .expiry_msec = now_msec + ACD_REGLIST_GRACE_PERIOD_MSEC,
                       }));

    if (idx != G_MAXUINT) {
        /* we already tracked this "addr". We don't need to track it twice,
         * forget about this one. This also has the effect, that we will
         * always append the new entry to the list (so the list
         * stays sorted by the increasing timestamp). */
        _acd_reglist_data_remove(self, idx, FALSE);
    }

    if (priv->v4.acd.reglist->len > ACD_REGLIST_MAX_ENTRIES) {
        /* rate limit how many addresses we track for ACD. */
        _acd_reglist_data_remove(self, 0, TRUE);
    }

    _acd_reglist_timeout_reschedule(self, now_msec);

    if (!priv->v4.acd.l3cfg_commit_handle) {
        priv->v4.acd.l3cfg_commit_handle =
            nm_l3cfg_commit_type_register(priv->config.l3cfg,
                                          NM_L3_CFG_COMMIT_TYPE_UPDATE,
                                          NULL,
                                          "dhcp4-acd");
        l3_cfg_notify_check_connected(self);
    }

    if (addr_changed)
        nm_l3cfg_commit_on_idle_schedule(priv->config.l3cfg, NM_L3_CFG_COMMIT_TYPE_AUTO);

    /* ACD is started/pending... */
    nm_assert(priv->v4.acd.addr != INADDR_ANY);
    nm_assert(priv->v4.acd.state == NM_OPTION_BOOL_DEFAULT);
    nm_assert(priv->v4.acd.l3cfg_commit_handle);
    nm_assert(priv->l3cfg_notify.id);
    *out_acd_state = NM_OPTION_BOOL_DEFAULT;
    return;

handle_no_acd:
    /* Indicate that ACD is good (or disabled) by returning TRUE. */
    _acd_state_reset(self, TRUE, FALSE);
    *out_acd_state = NM_OPTION_BOOL_TRUE;
    return;
}

/*****************************************************************************/

gboolean
_nm_dhcp_client_accept_offer(NMDhcpClient *self, gconstpointer p_yiaddr)
{
    NMDhcpClientPrivate   *priv = NM_DHCP_CLIENT_GET_PRIVATE(self);
    char                   sbuf_addr[NM_INET_ADDRSTRLEN];
    NMIPAddr               yiaddr;
    const NML3AcdAddrInfo *acd_info;

    if (!NM_IS_IPv4(priv->config.addr_family))
        return nm_assert_unreachable_val(FALSE);

    if (priv->config.v4.acd_timeout_msec == 0) {
        /* ACD is disabled. Note that we might track the address for other
         * reasons and have information about the ACD state below. But
         * with ACD disabled, we always ignore that information. */
        return TRUE;
    }

    nm_ip_addr_set(priv->config.addr_family, &yiaddr, p_yiaddr);

    /* Note that once we do ACD for a certain address, even after completing
     * it, we keep the l3cd registered in NML3Cfg for ACD_REGLIST_GRACE_PERIOD_MSEC
     * The idea is, that we don't yet turn off ACD for a grace period, so that
     * we can avoid selecting the same lease again.
     *
     * Note that we even check whether we have an ACD state if priv->v4.acd.reglist
     * is empty. Maybe for odd reasons, we track ACD for the address already. */

    acd_info = nm_l3cfg_get_acd_addr_info(priv->config.l3cfg, yiaddr.addr4);

    if (!acd_info)
        return TRUE;

    if (!NM_IN_SET(acd_info->state, NM_L3_ACD_ADDR_STATE_USED, NM_L3_ACD_ADDR_STATE_CONFLICT))
        return TRUE;

    _LOGD("offered lease rejected: address %s failed ACD check",
          nm_inet4_ntop(yiaddr.addr4, sbuf_addr));

    return FALSE;
}

void
_nm_dhcp_client_notify(NMDhcpClient         *self,
                       NMDhcpClientEventType client_event_type,
                       const NML3ConfigData *l3cd)
{
    NMDhcpClientPrivate                     *priv = NM_DHCP_CLIENT_GET_PRIVATE(self);
    GHashTable                              *options;
    gboolean                                 l3cd_changed;
    NMOptionBool                             acd_state;
    const int                                IS_IPv4     = NM_IS_IPv4(priv->config.addr_family);
    nm_auto_unref_l3cd const NML3ConfigData *l3cd_merged = NULL;
    char                                     sbuf1[NM_HASH_OBFUSCATE_PTR_STR_BUF_SIZE];

    nm_assert(NM_IN_SET(client_event_type,
                        NM_DHCP_CLIENT_EVENT_TYPE_UNSPECIFIED,
                        NM_DHCP_CLIENT_EVENT_TYPE_BOUND,
                        NM_DHCP_CLIENT_EVENT_TYPE_EXTENDED,
                        NM_DHCP_CLIENT_EVENT_TYPE_TIMEOUT,
                        NM_DHCP_CLIENT_EVENT_TYPE_EXPIRE,
                        NM_DHCP_CLIENT_EVENT_TYPE_FAIL,
                        NM_DHCP_CLIENT_EVENT_TYPE_TERMINATED));
    nm_assert((client_event_type >= NM_DHCP_CLIENT_EVENT_TYPE_TIMEOUT)
              == NM_IN_SET(client_event_type,
                           NM_DHCP_CLIENT_EVENT_TYPE_TIMEOUT,
                           NM_DHCP_CLIENT_EVENT_TYPE_EXPIRE,
                           NM_DHCP_CLIENT_EVENT_TYPE_FAIL,
                           NM_DHCP_CLIENT_EVENT_TYPE_TERMINATED));
    nm_assert((!!l3cd)
              == NM_IN_SET(client_event_type,
                           NM_DHCP_CLIENT_EVENT_TYPE_BOUND,
                           NM_DHCP_CLIENT_EVENT_TYPE_EXTENDED));

    nm_assert(!l3cd || NM_IS_L3_CONFIG_DATA(l3cd));
    nm_assert(!l3cd || nm_l3_config_data_get_dhcp_lease(l3cd, priv->config.addr_family));

    _LOGT("notify: event=%s%s%s",
          nm_dhcp_client_event_type_to_string(client_event_type),
          NM_PRINT_FMT_QUOTED2(l3cd, ", l3cd=", NM_HASH_OBFUSCATE_PTR_STR(l3cd, sbuf1), ""));

    nm_l3_config_data_seal(l3cd);

    if (client_event_type >= NM_DHCP_CLIENT_EVENT_TYPE_TIMEOUT)
        watch_cleanup(self);

    if (!IS_IPv4 && l3cd) {
        /* nm_dhcp_utils_merge_new_dhcp6_lease() relies on "life_starts" option
         * for merging, which is only set by dhclient. Internal client never sets that,
         * but it supports multiple IP addresses per lease. */
        if (nm_dhcp_utils_merge_new_dhcp6_lease(priv->l3cd_next, l3cd, &l3cd_merged)) {
            _LOGD("lease merged with existing one");
            l3cd = nm_l3_config_data_seal(l3cd_merged);
        }
    }

    if (l3cd) {
        nm_clear_g_source_inst(&priv->no_lease_timeout_source);
    } else
        _no_lease_timeout_schedule(self);

    l3cd_changed = nm_l3_config_data_reset(&priv->l3cd_next, l3cd);

    _acd_check_lease(self, &acd_state);

    options = priv->l3cd_next ? nm_dhcp_lease_get_options(
                  nm_l3_config_data_get_dhcp_lease(priv->l3cd_next, priv->config.addr_family))
                              : NULL;

    if (_LOGI_ENABLED()) {
        const char *req_str =
            IS_IPv4 ? nm_dhcp_option_request_string(AF_INET, NM_DHCP_OPTION_DHCP4_NM_IP_ADDRESS)
                    : nm_dhcp_option_request_string(AF_INET6, NM_DHCP_OPTION_DHCP6_NM_IP_ADDRESS);
        const char *addr = nm_g_hash_table_lookup(options, req_str);

        _LOGI("state changed %s%s%s%s",
              priv->l3cd_next ? "new lease" : "no lease",
              NM_PRINT_FMT_QUOTED2(addr, ", address=", addr, ""),
              acd_state == NM_OPTION_BOOL_DEFAULT ? ", acd pending"
                                                  : (acd_state ? "" : ", acd conflict"));
    }

    if (_LOGD_ENABLED()) {
        if (l3cd_changed && options) {
            gs_free const char **keys = NULL;
            guint                nkeys;
            guint                i;

            keys = nm_strdict_get_keys(options, TRUE, &nkeys);
            for (i = 0; i < nkeys; i++) {
                _LOGD("option %-20s => '%s'",
                      keys[i],
                      (char *) g_hash_table_lookup(options, keys[i]));
            }
        }
    }

    if (acd_state == NM_OPTION_BOOL_DEFAULT) {
        /* ACD is in progress... */
        return;
    }

    if (!acd_state) {
        gs_free_error GError *error = NULL;

        /* We only decline. We don't actually emit to the caller that
         * something is wrong (like NM_DHCP_CLIENT_NOTIFY_TYPE_IT_LOOKS_BAD).
         * If we would, NMDevice might decide to tear down the device, when
         * we actually should continue trying to get a better lease. There
         * is already "ipv4.dhcp-timeout" which will handle the failure if
         * we don't get a good lease. */
        if (!_dhcp_client_decline(self, priv->l3cd_next, "acd failed", &error))
            _LOGD("decline failed: %s", error->message);
        return;
    }

    if (priv->l3cd_next)
        nm_clear_g_source_inst(&priv->previous_lease_timeout_source);

    nm_l3_config_data_reset(&priv->l3cd_curr, priv->l3cd_next);
    priv->l3cfg_notify.wait_ipv6_dad = FALSE;

    if (client_event_type == NM_DHCP_CLIENT_EVENT_TYPE_BOUND && priv->l3cd_curr
        && nm_l3_config_data_get_num_addresses(priv->l3cd_curr, priv->config.addr_family) > 0)
        priv->l3cfg_notify.wait_dhcp_commit = TRUE;
    else
        priv->l3cfg_notify.wait_dhcp_commit = FALSE;

    if (!priv->l3cfg_notify.wait_dhcp_commit && priv->l3cd_curr) {
        gs_free_error GError *error = NULL;

        _LOGD("accept lease right away");
        if (!_dhcp_client_accept(self, priv->l3cd_curr, &error)) {
            _LOGD("accept failed: %s", error->message);
            /* Unclear why this happened, or what to do about it. Just proceed. */
        }
    }

    l3_cfg_notify_check_connected(self);

    _emit_notify(self,
                 NM_DHCP_CLIENT_NOTIFY_TYPE_LEASE_UPDATE,
                 .lease_update = {
                     .l3cd     = priv->l3cd_curr,
                     .accepted = !priv->l3cfg_notify.wait_dhcp_commit,
                 });
}

static void
daemon_watch_cb(GPid pid, int status, gpointer user_data)
{
    NMDhcpClient        *self = NM_DHCP_CLIENT(user_data);
    NMDhcpClientPrivate *priv = NM_DHCP_CLIENT_GET_PRIVATE(self);
    gs_free char        *desc = NULL;

    g_return_if_fail(priv->watch_source);

    priv->watch_source = NULL;

    _LOGI("client pid %d %s", pid, (desc = nm_utils_get_process_exit_status_desc(status)));

    priv->pid = -1;

    _nm_dhcp_client_notify(self, NM_DHCP_CLIENT_EVENT_TYPE_TERMINATED, NULL);
}

void
nm_dhcp_client_watch_child(NMDhcpClient *self, pid_t pid)
{
    NMDhcpClientPrivate *priv = NM_DHCP_CLIENT_GET_PRIVATE(self);

    g_return_if_fail(priv->pid == -1);
    priv->pid = pid;

    g_return_if_fail(!priv->watch_source);
    priv->watch_source = nm_g_child_watch_add_source(pid, daemon_watch_cb, self);
}

void
nm_dhcp_client_stop_watch_child(NMDhcpClient *self, pid_t pid)
{
    NMDhcpClientPrivate *priv = NM_DHCP_CLIENT_GET_PRIVATE(self);

    g_return_if_fail(priv->pid == pid);
    priv->pid = -1;

    watch_cleanup(self);
}

static gboolean
_accept(NMDhcpClient *self, const NML3ConfigData *l3cd, GError **error)
{
    NMDhcpClientPrivate *priv = NM_DHCP_CLIENT_GET_PRIVATE(self);

    if (!priv->invocation)
        return TRUE;

    g_dbus_method_invocation_return_value(g_steal_pointer(&priv->invocation), NULL);
    return TRUE;
}

static gboolean
_dhcp_client_accept(NMDhcpClient *self, const NML3ConfigData *l3cd, GError **error)
{
    NMDhcpClientClass *klass;

    g_return_val_if_fail(NM_IS_DHCP_CLIENT(self), FALSE);
    nm_assert(l3cd);

    klass = NM_DHCP_CLIENT_GET_CLASS(self);

    g_return_val_if_fail(NM_DHCP_CLIENT_GET_PRIVATE(self)->l3cd_curr, FALSE);

    return klass->accept(self, l3cd, error);
}

static gboolean
decline(NMDhcpClient *self, const NML3ConfigData *l3cd, const char *error_message, GError **error)
{
    NMDhcpClientPrivate *priv = NM_DHCP_CLIENT_GET_PRIVATE(self);

    if (!priv->invocation) {
        nm_utils_error_set(error,
                           NM_UTILS_ERROR_UNKNOWN,
                           "calling decline in unexpected script state");
        return FALSE;
    }
    g_dbus_method_invocation_return_error(g_steal_pointer(&priv->invocation),
                                          NM_DEVICE_ERROR,
                                          NM_DEVICE_ERROR_FAILED,
                                          NM_IS_IPv4(priv->config.addr_family) ? "ACD failed"
                                                                               : "DAD failed");
    return TRUE;
}

static gboolean
_dhcp_client_decline(NMDhcpClient         *self,
                     const NML3ConfigData *l3cd,
                     const char           *error_message,
                     GError              **error)
{
    NMDhcpClientClass *klass;

    g_return_val_if_fail(NM_IS_DHCP_CLIENT(self), FALSE);
    nm_assert(l3cd);

    klass = NM_DHCP_CLIENT_GET_CLASS(self);

    g_return_val_if_fail(NM_DHCP_CLIENT_GET_PRIVATE(self)->l3cd_next, FALSE);

    return klass->decline(self, l3cd, error_message, error);
}

static gboolean
ipv6_lladdr_timeout(gpointer user_data)
{
    NMDhcpClient        *self = user_data;
    NMDhcpClientPrivate *priv = NM_DHCP_CLIENT_GET_PRIVATE(self);

    nm_clear_g_source_inst(&priv->v6.lladdr_timeout_source);

    _emit_notify(self,
                 NM_DHCP_CLIENT_NOTIFY_TYPE_IT_LOOKS_BAD,
                 .it_looks_bad.reason =
                     "timeout reached while waiting for an IPv6 link-local address");
    return G_SOURCE_CONTINUE;
}

static gboolean
ipv6_dad_timeout(gpointer user_data)
{
    NMDhcpClient        *self = user_data;
    NMDhcpClientPrivate *priv = NM_DHCP_CLIENT_GET_PRIVATE(self);

    nm_clear_g_source_inst(&priv->v6.dad_timeout_source);

    _emit_notify(self,
                 NM_DHCP_CLIENT_NOTIFY_TYPE_IT_LOOKS_BAD,
                 .it_looks_bad.reason = "timeout reached while waiting for IPv6 DAD to complete");
    return G_SOURCE_CONTINUE;
}

static const NMPlatformIP6Address *
ipv6_lladdr_find(NMDhcpClient *self)
{
    NMDhcpClientPrivate *priv = NM_DHCP_CLIENT_GET_PRIVATE(self);
    NML3Cfg             *l3cfg;
    NMPLookup            lookup;
    NMDedupMultiIter     iter;
    const NMPObject     *obj;

    nm_assert(!NM_IS_IPv4(priv->config.addr_family));

    l3cfg = priv->config.l3cfg;
    nmp_lookup_init_object_by_ifindex(&lookup,
                                      NMP_OBJECT_TYPE_IP6_ADDRESS,
                                      nm_l3cfg_get_ifindex(l3cfg));

    nm_platform_iter_obj_for_each (&iter, nm_l3cfg_get_platform(l3cfg), &lookup, &obj) {
        const NMPlatformIP6Address *pladdr = NMP_OBJECT_CAST_IP6_ADDRESS(obj);

        if (!IN6_IS_ADDR_LINKLOCAL(&pladdr->address))
            continue;
        if (NM_FLAGS_HAS(pladdr->n_ifa_flags, IFA_F_TENTATIVE)
            && !NM_FLAGS_HAS(pladdr->n_ifa_flags, IFA_F_OPTIMISTIC))
            continue;
        return pladdr;
    }
    return NULL;
}

static void
ipv6_tentative_addr_check(NMDhcpClient                *self,
                          GPtrArray                  **tentative,
                          GPtrArray                  **dadfailed,
                          const NMPlatformIP6Address **valid)
{
    NMDhcpClientPrivate        *priv = NM_DHCP_CLIENT_GET_PRIVATE(self);
    NMDedupMultiIter            iter;
    const NMPlatformIP6Address *addr;
    NML3Cfg                    *l3cfg    = priv->config.l3cfg;
    NMPlatform                 *platform = nm_l3cfg_get_platform(l3cfg);
    int                         ifindex  = nm_l3cfg_get_ifindex(l3cfg);

    /* For each address in the lease, check whether it's tentative
     * or dad-failed in platform. */
    nm_l3_config_data_iter_ip6_address_for_each (&iter, priv->l3cd_curr, &addr) {
        const NMPlatformIP6Address *pladdr;

        pladdr = nm_platform_ip6_address_get(platform, ifindex, &addr->address);
        if ((pladdr && NM_FLAGS_HAS(pladdr->n_ifa_flags, IFA_F_DADFAILED))
            || (!pladdr && nm_platform_ip6_dadfailed_check(platform, ifindex, &addr->address))) {
            if (dadfailed) {
                if (!*dadfailed)
                    *dadfailed = g_ptr_array_new();
                g_ptr_array_add(*dadfailed, (gpointer) addr);
            }
            continue;
        }

        if (pladdr && NM_FLAGS_HAS(pladdr->n_ifa_flags, IFA_F_TENTATIVE)
            && !NM_FLAGS_HAS(pladdr->n_ifa_flags, IFA_F_OPTIMISTIC)) {
            if (tentative) {
                if (!*tentative)
                    *tentative = g_ptr_array_new();
                g_ptr_array_add(*tentative, (gpointer) addr);
            }
        }

        /* Here the address is non-tentative or it was removed externally by the user.
         * In both cases it has completed DAD.
         */
        NM_SET_OUT(valid, addr);
    }
}

static void
l3_cfg_notify_cb(NML3Cfg *l3cfg, const NML3ConfigNotifyData *notify_data, NMDhcpClient *self)
{
    NMDhcpClientPrivate *priv = NM_DHCP_CLIENT_GET_PRIVATE(self);
    char                 sbuf_addr[NM_INET_ADDRSTRLEN];

    nm_assert(l3cfg == priv->config.l3cfg);

    if (notify_data->notify_type == NM_L3_CONFIG_NOTIFY_TYPE_PLATFORM_CHANGE_ON_IDLE
        && priv->l3cfg_notify.wait_ll_address) {
        const NMPlatformIP6Address *addr;
        gs_free_error GError       *error = NULL;

        addr = ipv6_lladdr_find(self);
        if (addr) {
            _LOGD("got IPv6LL address, starting transaction");
            priv->l3cfg_notify.wait_ll_address = FALSE;
            l3_cfg_notify_check_connected(self);
            nm_clear_g_source_inst(&priv->v6.lladdr_timeout_source);

            _no_lease_timeout_schedule(self);

            if (!NM_DHCP_CLIENT_GET_CLASS(self)->ip6_start(self, &addr->address, &error)) {
                _emit_notify(self,
                             NM_DHCP_CLIENT_NOTIFY_TYPE_IT_LOOKS_BAD,
                             .it_looks_bad.reason = error->message);
            }
        }
    }

    if (notify_data->notify_type == NM_L3_CONFIG_NOTIFY_TYPE_PLATFORM_CHANGE_ON_IDLE
        && priv->l3cfg_notify.wait_ipv6_dad) {
        gs_unref_ptrarray GPtrArray *tentative = NULL;
        gs_unref_ptrarray GPtrArray *dadfailed = NULL;
        const NMPlatformIP6Address  *valid     = NULL;
        char                         str[NM_UTILS_TO_STRING_BUFFER_SIZE];
        guint                        i;
        gs_free_error GError        *error = NULL;

        ipv6_tentative_addr_check(self, &tentative, &dadfailed, &valid);
        if (tentative) {
            for (i = 0; i < tentative->len; i++) {
                _LOGD("still waiting DAD for address: %s",
                      nm_platform_ip6_address_to_string(tentative->pdata[i], str, sizeof(str)));
            }
        } else {
            /* done */

            priv->l3cfg_notify.wait_ipv6_dad = FALSE;
            nm_clear_g_source_inst(&priv->v6.dad_timeout_source);
            l3_cfg_notify_check_connected(self);

            if (dadfailed) {
                for (i = 0; i < dadfailed->len; i++) {
                    _LOGE("DAD failed for address: %s",
                          nm_platform_ip6_address_to_string(dadfailed->pdata[i], str, sizeof(str)));
                }
            }

            if (valid) {
                /* at least one non-duplicate address */
                _LOGD("addresses in the lease completed DAD: accept the lease");

                if (_dhcp_client_accept(self, priv->l3cd_curr, &error)) {
                    _emit_notify(self,
                                 NM_DHCP_CLIENT_NOTIFY_TYPE_LEASE_UPDATE,
                                 .lease_update = {
                                     .l3cd     = priv->l3cd_curr,
                                     .accepted = TRUE,
                                 });
                } else {
                    gs_free char *reason =
                        g_strdup_printf("error accepting lease: %s", error->message);

                    _LOGD("accept failed: %s", error->message);
                    _emit_notify(self,
                                 NM_DHCP_CLIENT_NOTIFY_TYPE_IT_LOOKS_BAD,
                                 .it_looks_bad.reason = reason);
                }
            } else {
                _LOGD("decline the lease");
                if (!_dhcp_client_decline(self, priv->l3cd_curr, "DAD failed", &error))
                    _LOGD("decline failed: %s", error->message);
            }
        }
    }

    if (notify_data->notify_type == NM_L3_CONFIG_NOTIFY_TYPE_POST_COMMIT
        && priv->l3cfg_notify.wait_dhcp_commit) {
        const NML3ConfigData      *committed_l3cd;
        NMDedupMultiIter           ipconf_iter;
        const NMPlatformIPAddress *lease_address;
        gs_free_error GError      *error = NULL;

        /* A new configuration was committed to the interface. If we previously
         * got a lease, check whether we are waiting for the address to be
         * configured. If the address was added, we can proceed accepting the
         * lease and notifying NMDevice. */

        nm_l3_config_data_iter_ip_address_for_each (&ipconf_iter,
                                                    priv->l3cd_curr,
                                                    priv->config.addr_family,
                                                    &lease_address)
            break;
        nm_assert(lease_address);
        committed_l3cd = nm_l3cfg_get_combined_l3cd(l3cfg, TRUE);

        if (priv->config.addr_family == AF_INET) {
            const NMPlatformIP4Address *address4 = (const NMPlatformIP4Address *) lease_address;

            if (!nm_l3_config_data_lookup_address_4(committed_l3cd,
                                                    address4->address,
                                                    address4->plen,
                                                    address4->peer_address))
                goto wait_dhcp_commit_done;
        } else {
            const NMPlatformIP6Address  *address6  = (const NMPlatformIP6Address *) lease_address;
            gs_unref_ptrarray GPtrArray *tentative = NULL;
            char                         str[NM_UTILS_TO_STRING_BUFFER_SIZE];
            guint                        i;

            if (!nm_l3_config_data_lookup_address_6(committed_l3cd, &address6->address))
                goto wait_dhcp_commit_done;

            ipv6_tentative_addr_check(self, &tentative, NULL, NULL);
            if (tentative) {
                priv->l3cfg_notify.wait_ipv6_dad = TRUE;
                priv->v6.dad_timeout_source =
                    nm_g_timeout_add_seconds_source(30, ipv6_dad_timeout, self);
                for (i = 0; i < tentative->len; i++) {
                    _LOGD("wait DAD for address %s",
                          nm_platform_ip6_address_to_string(tentative->pdata[i], str, sizeof(str)));
                }
            } else {
                priv->l3cfg_notify.wait_ipv6_dad = FALSE;
                nm_clear_g_source_inst(&priv->v6.dad_timeout_source);
            }
        }

        priv->l3cfg_notify.wait_dhcp_commit = FALSE;

        l3_cfg_notify_check_connected(self);

        if (priv->config.addr_family == AF_INET || !priv->l3cfg_notify.wait_ipv6_dad) {
            _LOGD("accept lease");

            if (!_dhcp_client_accept(self, priv->l3cd_curr, &error)) {
                gs_free char *reason = g_strdup_printf("error accepting lease: %s", error->message);

                _LOGD("accept failed: %s", error->message);

                _emit_notify(self,
                             NM_DHCP_CLIENT_NOTIFY_TYPE_IT_LOOKS_BAD,
                             .it_looks_bad.reason = reason, );
                goto wait_dhcp_commit_done;
            }

            _emit_notify(self,
                         NM_DHCP_CLIENT_NOTIFY_TYPE_LEASE_UPDATE,
                         .lease_update = {
                             .l3cd     = priv->l3cd_curr,
                             .accepted = TRUE,
                         });
        }
    }
wait_dhcp_commit_done:

    if (notify_data->notify_type == NM_L3_CONFIG_NOTIFY_TYPE_ACD_EVENT
        && priv->v4.acd.l3cfg_commit_handle) {
        nm_assert(priv->v4.acd.addr != INADDR_ANY);
        nm_assert(priv->v4.acd.state == NM_OPTION_BOOL_DEFAULT);
        nm_assert(!priv->v4.acd.done_source);

        if (priv->v4.acd.addr == notify_data->acd_event.info.addr
            && nm_l3_acd_addr_info_find_track_info(&notify_data->acd_event.info,
                                                   L3CD_ACD_TAG(priv),
                                                   NULL,
                                                   NULL)) {
            NMOptionBool acd_state;

            switch (notify_data->acd_event.info.state) {
            default:
                nm_assert_not_reached();
                /* fall-through */
            case NM_L3_ACD_ADDR_STATE_INIT:
            case NM_L3_ACD_ADDR_STATE_PROBING:
                acd_state = NM_OPTION_BOOL_DEFAULT;
                break;
            case NM_L3_ACD_ADDR_STATE_USED:
            case NM_L3_ACD_ADDR_STATE_CONFLICT:
            case NM_L3_ACD_ADDR_STATE_EXTERNAL_REMOVED:
                acd_state = NM_OPTION_BOOL_FALSE;
                break;
            case NM_L3_ACD_ADDR_STATE_READY:
            case NM_L3_ACD_ADDR_STATE_DEFENDING:
                acd_state = NM_OPTION_BOOL_TRUE;
                break;
            }
            if (acd_state != NM_OPTION_BOOL_DEFAULT) {
                _LOGD("acd: acd %s for %s",
                      acd_state ? "ready" : "conflict",
                      nm_inet4_ntop(priv->v4.acd.addr, sbuf_addr));
                nm_l3cfg_commit_type_clear(priv->config.l3cfg, &priv->v4.acd.l3cfg_commit_handle);
                priv->v4.acd.state       = acd_state;
                priv->v4.acd.done_source = nm_g_idle_add_source(_acd_complete_on_idle_cb, self);
            }
        }
    }
}

static gboolean
_previous_lease_timeout_cb(gpointer user_data)
{
    NMDhcpClient        *self = user_data;
    NMDhcpClientPrivate *priv = NM_DHCP_CLIENT_GET_PRIVATE(self);

    nm_clear_g_source_inst(&priv->previous_lease_timeout_source);

    _nm_dhcp_client_notify(self, NM_DHCP_CLIENT_EVENT_TYPE_TIMEOUT, NULL);

    return G_SOURCE_CONTINUE;
}

gboolean
nm_dhcp_client_start(NMDhcpClient *self, GError **error)
{
    NMDhcpClientPrivate        *priv;
    const NMPlatformIP6Address *addr = NULL;
    int                         IS_IPv4;

    g_return_val_if_fail(NM_IS_DHCP_CLIENT(self), FALSE);

    priv = NM_DHCP_CLIENT_GET_PRIVATE(self);

    g_return_val_if_fail(priv->pid == -1, FALSE);
    g_return_val_if_fail(priv->config.uuid, FALSE);
    nm_assert(!priv->effective_client_id);

    IS_IPv4 = NM_IS_IPv4(priv->config.addr_family);

    if (!IS_IPv4) {
        addr = ipv6_lladdr_find(self);
        if (!addr) {
            _LOGD("waiting for IPv6LL address");
            priv->l3cfg_notify.wait_ll_address = TRUE;
            l3_cfg_notify_check_connected(self);
            priv->v6.lladdr_timeout_source =
                nm_g_timeout_add_seconds_source(10, ipv6_lladdr_timeout, self);
            return TRUE;
        }
    }

    _no_lease_timeout_schedule(self);

    if (priv->config.previous_lease) {
        /* We got passed a previous lease (during a reapply). For a few seconds, we
         * will pretend that this is current lease. */
        priv->l3cd_curr = g_steal_pointer(&priv->config.previous_lease);

        /* Schedule a timeout for when we give up using this lease. Note
         * that then we will emit a NM_DHCP_CLIENT_NOTIFY_TYPE_LEASE_UPDATE event
         * and the lease is gone. Note that NMDevice ignores that and will
         * keep using the lease.
         *
         * At the same time, we have _no_lease_timeout_schedule() ticking, when
         * that expires, we will emit a NM_DHCP_CLIENT_NOTIFY_TYPE_NO_LEASE_TIMEOUT
         * signal, which causes NMDevice to clear the lease. */
        priv->previous_lease_timeout_source =
            nm_g_timeout_add_seconds_source(15, _previous_lease_timeout_cb, self);
    }

    if (IS_IPv4)
        return NM_DHCP_CLIENT_GET_CLASS(self)->ip4_start(self, error);

    return NM_DHCP_CLIENT_GET_CLASS(self)->ip6_start(self, &addr->address, error);
}

/*****************************************************************************/

void
nm_dhcp_client_stop_existing(const char *pid_file, const char *binary_name)
{
    guint64       start_time;
    pid_t         pid, ppid;
    const char   *exe;
    char          proc_path[NM_STRLEN("/proc/%lu/cmdline") + 100];
    gs_free char *pid_contents = NULL, *proc_contents = NULL;

    /* Check for an existing instance and stop it */
    if (!g_file_get_contents(pid_file, &pid_contents, NULL, NULL))
        return;

    pid = _nm_utils_ascii_str_to_int64(pid_contents, 10, 1, G_MAXINT64, 0);
    if (pid <= 0)
        goto out;

    start_time = nm_utils_get_start_time_for_pid(pid, NULL, &ppid);
    if (start_time == 0)
        goto out;

    nm_sprintf_buf(proc_path, "/proc/%lu/cmdline", (unsigned long) pid);
    if (!g_file_get_contents(proc_path, &proc_contents, NULL, NULL))
        goto out;

    exe = strrchr(proc_contents, '/');
    if (exe)
        exe++;
    else
        exe = proc_contents;
    if (!nm_streq0(exe, binary_name))
        goto out;

    if (ppid == getpid()) {
        /* the process is our own child. */
        nm_utils_kill_child_sync(pid, SIGTERM, LOGD_DHCP, "dhcp-client", NULL, 1000 / 2, 1000 / 20);
    } else {
        nm_utils_kill_process_sync(pid,
                                   start_time,
                                   SIGTERM,
                                   LOGD_DHCP,
                                   "dhcp-client",
                                   1000 / 2,
                                   1000 / 20,
                                   2000);
    }

out:
    if (remove(pid_file) == -1) {
        int errsv = errno;

        nm_log_dbg(LOGD_DHCP,
                   "dhcp: could not remove pid file \"%s\": %s (%d)",
                   pid_file,
                   nm_strerror_native(errsv),
                   errsv);
    }
}

void
nm_dhcp_client_stop(NMDhcpClient *self, gboolean release)
{
    NMDhcpClientPrivate *priv;
    pid_t                old_pid = 0;

    g_return_if_fail(NM_IS_DHCP_CLIENT(self));

    priv = NM_DHCP_CLIENT_GET_PRIVATE(self);

    if (priv->is_stopped)
        return;

    nm_clear_g_source_inst(&priv->previous_lease_timeout_source);

    priv->is_stopped = TRUE;

    if (priv->invocation) {
        g_dbus_method_invocation_return_error(g_steal_pointer(&priv->invocation),
                                              NM_DEVICE_ERROR,
                                              NM_DEVICE_ERROR_FAILED,
                                              "dhcp stopping");
    }

    _acd_state_reset(self, TRUE, TRUE);

    priv->l3cfg_notify.wait_dhcp_commit = FALSE;
    priv->l3cfg_notify.wait_ll_address  = FALSE;
    priv->l3cfg_notify.wait_ipv6_dad    = FALSE;
    l3_cfg_notify_check_connected(self);

    /* Kill the DHCP client */
    old_pid = priv->pid;
    NM_DHCP_CLIENT_GET_CLASS(self)->stop(self, release);
    if (old_pid > 0)
        _LOGI("canceled DHCP transaction, DHCP client pid %d", old_pid);
    else
        _LOGI("canceled DHCP transaction");
    nm_assert(priv->pid == -1);

    nm_clear_l3cd(&priv->l3cd_next);
    nm_clear_l3cd(&priv->l3cd_curr);

    _nm_dhcp_client_notify(self, NM_DHCP_CLIENT_EVENT_TYPE_TERMINATED, NULL);
}

/*****************************************************************************/

static char *
bytearray_variant_to_string(GVariant *value)
{
    const guint8 *array;
    char         *str;
    gsize         length;
    gsize         i;

    nm_assert(value);

    array = g_variant_get_fixed_array(value, &length, 1);

    /* Since the DHCP options come originally came as environment variables, they
     * have not guaranteed encoding. Let's only accept ASCII here.
     */
    str = g_malloc(length + 1);
    for (i = 0; i < length; i++) {
        guint8 c = array[i];

        if (c == '\0')
            str[i] = ' ';
        else if (c > 127)
            str[i] = '?';
        else
            str[i] = (char) c;
    }
    str[i] = '\0';

    return str;
}

static int
label_is_unknown_xyz(const char *label)
{
    if (!NM_STR_HAS_PREFIX(label, "unknown_"))
        return -EINVAL;

    label += NM_STRLEN("unknown_");
    if (label[0] != '2' || !g_ascii_isdigit(label[1]) || !g_ascii_isdigit(label[2])
        || label[3] != '\0')
        return -EINVAL;

    return _nm_utils_ascii_str_to_int64(label, 10, 224, 254, -EINVAL);
}

#define OLD_TAG "old_"
#define NEW_TAG "new_"

static void
maybe_add_option(NMDhcpClient *self, GHashTable *hash, const char *key, GVariant *value)
{
    const int IS_IPv4 = NM_IS_IPv4(NM_DHCP_CLIENT_GET_PRIVATE(self)->config.addr_family);
    char     *str_value;
    int       priv_opt_num;

    if (!g_variant_is_of_type(value, G_VARIANT_TYPE_BYTESTRING))
        return;

    if (NM_STR_HAS_PREFIX(key, OLD_TAG))
        return;

    /* Filter out stuff that's not actually new DHCP options */
    if (NM_IN_STRSET(key, "interface", "pid", "reason", "dhcp_message_type"))
        return;

    if (NM_STR_HAS_PREFIX(key, NEW_TAG))
        key += NM_STRLEN(NEW_TAG);
    if (NM_STR_HAS_PREFIX(key, "private_") || !key[0])
        return;

    str_value = bytearray_variant_to_string(value);
    if (!str_value)
        return;

    if ((IS_IPv4 && nm_streq(key, "dhcp_client_identifier"))
        || (!IS_IPv4 && nm_streq(key, "dhcp6_client_id"))) {
        gs_free char          *str   = g_steal_pointer(&str_value);
        gs_unref_bytes GBytes *bytes = NULL;

        /* Validate and normalize the client-id/DUID. */

        bytes = nm_utils_hexstr2bin(str);
        if (!bytes || g_bytes_get_size(bytes) < 2) {
            /* Seems invalid. Ignore */
            return;
        }

        if (!nm_dhcp_client_set_effective_client_id(self, bytes)) {
            /* the client-id is identical and we already set it. Nothing to do. */
            return;
        }

        /* The effective-client-id was (re)set. Update "hash" with the new value... */
        str_value = nm_dhcp_utils_duid_to_string(bytes);
    }

    if (!IS_IPv4 && nm_streq(key, "iaid")) {
        gs_free char *str = g_steal_pointer(&str_value);
        guint32       iaid;

        /* Validate and normalize the iaid. */

        if (!nm_dhcp_iaid_from_hexstr(str, &iaid)) {
            /* Seems invalid. Ignore */
            return;
        }

        str_value = nm_dhcp_iaid_to_hexstr(iaid, g_malloc(NM_DHCP_IAID_TO_HEXSTR_BUF_LEN));
    }

    g_hash_table_insert(hash, g_strdup(key), str_value);

    /* dhclient has no special labels for private dhcp options: it uses "unknown_xyz"
     * labels for that. We need to identify those to alias them to our "private_xyz"
     * format unused in the internal dchp plugins.
     */
    if ((priv_opt_num = label_is_unknown_xyz(key)) > 0) {
        gs_free guint8 *check_val = NULL;
        char           *hex_str   = NULL;
        gsize           len;

        /* dhclient passes values from dhcp private options in its own "string" format:
         * if the raw values are printable as ascii strings, it will pass the string
         * representation; if the values are not printable as an ascii string, it will
         * pass a string displaying the hex values (hex string). Try to enforce passing
         * always an hex string, converting string representation if needed.
         */
        check_val = nm_utils_hexstr2bin_alloc(str_value, FALSE, TRUE, ":", 0, &len);
        hex_str   = nm_utils_bin2hexstr_full(check_val ?: (guint8 *) str_value,
                                           check_val ? len : strlen(str_value),
                                           ':',
                                           FALSE,
                                           NULL);
        g_hash_table_insert(hash, g_strdup_printf("private_%d", priv_opt_num), hex_str);
    }
}

void
nm_dhcp_client_emit_ipv6_prefix_delegated(NMDhcpClient *self, const NMPlatformIP6Address *prefix)
{
    _emit_notify(self,
                 NM_DHCP_CLIENT_NOTIFY_TYPE_PREFIX_DELEGATED,
                 .prefix_delegated = {
                     .prefix = prefix,
                 });
}

gboolean
nm_dhcp_client_handle_event(gpointer               unused,
                            const char            *iface,
                            int                    pid,
                            GVariant              *options,
                            const char            *reason,
                            GDBusMethodInvocation *invocation,
                            NMDhcpClient          *self)
{
    NMDhcpClientPrivate                    *priv;
    nm_auto_unref_l3cd_init NML3ConfigData *l3cd = NULL;
    NMDhcpClientEventType                   client_event_type;
    NMPlatformIP6Address                    prefix = {
        0,
    };

    g_return_val_if_fail(NM_IS_DHCP_CLIENT(self), FALSE);
    g_return_val_if_fail(iface != NULL, FALSE);
    g_return_val_if_fail(pid > 0, FALSE);
    g_return_val_if_fail(g_variant_is_of_type(options, G_VARIANT_TYPE_VARDICT), FALSE);
    g_return_val_if_fail(reason != NULL, FALSE);
    g_return_val_if_fail(G_IS_DBUS_METHOD_INVOCATION(invocation), FALSE);

    priv = NM_DHCP_CLIENT_GET_PRIVATE(self);

    g_return_val_if_fail(!priv->is_stopped, FALSE);

    if (!nm_streq0(priv->config.iface, iface))
        return FALSE;
    if (priv->pid != pid)
        return FALSE;

    _LOGD("DHCP event (reason: '%s')", reason);

    if (NM_IN_STRSET_ASCII_CASE(reason, "preinit"))
        goto out_handled;

    if (NM_IN_STRSET_ASCII_CASE(reason, "bound", "bound6", "static"))
        client_event_type = NM_DHCP_CLIENT_EVENT_TYPE_BOUND;
    else if (NM_IN_STRSET_ASCII_CASE(reason, "renew", "renew6", "reboot", "rebind", "rebind6"))
        client_event_type = NM_DHCP_CLIENT_EVENT_TYPE_EXTENDED;
    else if (NM_IN_STRSET_ASCII_CASE(reason, "timeout"))
        client_event_type = NM_DHCP_CLIENT_EVENT_TYPE_TIMEOUT;
    else if (NM_IN_STRSET_ASCII_CASE(reason, "nak", "expire", "expire6"))
        client_event_type = NM_DHCP_CLIENT_EVENT_TYPE_EXPIRE;
    else if (NM_IN_STRSET_ASCII_CASE(reason, "end", "stop", "stopped"))
        client_event_type = NM_DHCP_CLIENT_EVENT_TYPE_TERMINATED;
    else if (NM_IN_STRSET_ASCII_CASE(reason, "fail", "abend"))
        client_event_type = NM_DHCP_CLIENT_EVENT_TYPE_FAIL;
    else
        client_event_type = NM_DHCP_CLIENT_EVENT_TYPE_UNSPECIFIED;

    if (NM_IN_SET(client_event_type,
                  NM_DHCP_CLIENT_EVENT_TYPE_BOUND,
                  NM_DHCP_CLIENT_EVENT_TYPE_EXTENDED)) {
        gs_unref_hashtable GHashTable *str_options = NULL;
        GVariantIter                   iter;
        const char                    *name;
        GVariant                      *value;

        /* Copy options */
        str_options = nm_dhcp_client_create_options_dict(self, FALSE);
        g_variant_iter_init(&iter, options);
        while (g_variant_iter_next(&iter, "{&sv}", &name, &value)) {
            maybe_add_option(self, str_options, name, value);
            g_variant_unref(value);
        }

        /* Create the IP config */
        if (g_hash_table_size(str_options) > 0) {
            if (priv->config.addr_family == AF_INET) {
                l3cd = nm_dhcp_utils_ip4_config_from_options(
                    nm_l3cfg_get_multi_idx(priv->config.l3cfg),
                    nm_l3cfg_get_ifindex(priv->config.l3cfg),
                    priv->config.iface,
                    str_options);
            } else {
                prefix = nm_dhcp_utils_ip6_prefix_from_options(str_options);
                l3cd   = nm_dhcp_utils_ip6_config_from_options(
                    nm_l3cfg_get_multi_idx(priv->config.l3cfg),
                    nm_l3cfg_get_ifindex(priv->config.l3cfg),
                    priv->config.iface,
                    str_options,
                    priv->config.v6.info_only);
            }
        }

        if (l3cd) {
            nm_l3_config_data_set_dhcp_lease_from_options(l3cd,
                                                          priv->config.addr_family,
                                                          g_steal_pointer(&str_options));
        }
    }

    if (!IN6_IS_ADDR_UNSPECIFIED(&prefix.address)) {
        /* If we got an IPv6 prefix to delegate, we don't change the state
         * of the DHCP client instance. Instead, we just signal the prefix
         * to the device. */
        nm_dhcp_client_emit_ipv6_prefix_delegated(self, &prefix);
        goto out_handled;
    }

    if (NM_IN_SET(client_event_type,
                  NM_DHCP_CLIENT_EVENT_TYPE_BOUND,
                  NM_DHCP_CLIENT_EVENT_TYPE_EXTENDED)
        && !l3cd) {
        /* Fail if no valid IP config was received */
        _LOGW("client bound but IP config not received");
        client_event_type = NM_DHCP_CLIENT_EVENT_TYPE_FAIL;
    }

    if (priv->invocation)
        g_dbus_method_invocation_return_value(g_steal_pointer(&priv->invocation), NULL);

    if (NM_IN_SET(client_event_type,
                  NM_DHCP_CLIENT_EVENT_TYPE_BOUND,
                  NM_DHCP_CLIENT_EVENT_TYPE_EXTENDED))
        priv->invocation = g_steal_pointer(&invocation);

    _nm_dhcp_client_notify(self, client_event_type, l3cd);

out_handled:
    if (invocation)
        g_dbus_method_invocation_return_value(invocation, NULL);
    return TRUE;
}

gboolean
nm_dhcp_client_server_id_is_rejected(NMDhcpClient *self, gconstpointer addr)
{
    NMDhcpClientPrivate *priv  = NM_DHCP_CLIENT_GET_PRIVATE(self);
    in_addr_t            addr4 = *(in_addr_t *) addr;
    guint                i;

    /* IPv6 not implemented yet */
    nm_assert(priv->config.addr_family == AF_INET);

    if (priv->config.reject_servers) {
        for (i = 0; priv->config.reject_servers[i]; i++) {
            in_addr_t r_addr;
            in_addr_t mask;
            int       r_prefix;

            if (!nm_inet_parse_with_prefix_bin(AF_INET,
                                               priv->config.reject_servers[i],
                                               NULL,
                                               &r_addr,
                                               &r_prefix))
                nm_assert_not_reached();

            mask = nm_ip4_addr_netmask_from_prefix(r_prefix < 0 ? 32 : r_prefix);
            if ((addr4 & mask) == (r_addr & mask))
                return TRUE;
        }
    }

    return FALSE;
}

/*****************************************************************************/

static void
config_init(NMDhcpClientConfig *config, const NMDhcpClientConfig *src)
{
    nm_assert(config);
    nm_assert(src);
    nm_assert(config != src);
    nm_assert_addr_family(src->addr_family);

    *config = *src;

    /* We must not return before un-aliasing all pointers in @config! */

    g_object_ref(config->l3cfg);

    nm_l3_config_data_ref_and_seal(config->previous_lease);

    nm_g_bytes_ref(config->hwaddr);
    nm_g_bytes_ref(config->bcast_hwaddr);
    nm_g_bytes_ref(config->vendor_class_identifier);
    nm_g_bytes_ref(config->client_id);

    config->iface           = g_strdup(config->iface);
    config->uuid            = g_strdup(config->uuid);
    config->anycast_address = g_strdup(config->anycast_address);
    config->hostname        = g_strdup(config->hostname);
    config->mud_url         = g_strdup(config->mud_url);

    config->reject_servers = nm_strv_dup_packed(config->reject_servers, -1);

    if (NM_IS_IPv4(config->addr_family))
        config->v4.last_address = g_strdup(config->v4.last_address);
    else {
        config->hwaddr       = NULL;
        config->bcast_hwaddr = NULL;
        config->use_fqdn     = TRUE;
    }

    if (!config->hostname && config->send_hostname) {
        const char   *hostname;
        gs_free char *hostname_tmp = NULL;

        hostname = nm_hostname_manager_get_static_hostname(nm_hostname_manager_get());

        if (nm_utils_is_specific_hostname(hostname)) {
            if (config->addr_family == AF_INET) {
                char *dot;

                hostname_tmp = g_strdup(hostname);
                dot          = strchr(hostname_tmp, '.');
                if (dot)
                    *dot = '\0';
            }
            config->hostname = hostname_tmp ? g_steal_pointer(&hostname_tmp) : g_strdup(hostname);
        }
    }

    if (config->hostname) {
        if (!config->send_hostname) {
            nm_clear_g_free((gpointer *) &config->hostname);
        } else if ((config->use_fqdn && !nm_sd_dns_name_is_valid(config->hostname))
                   || (!config->use_fqdn && !nm_hostname_is_valid(config->hostname, FALSE))) {
            nm_log_warn(LOGD_DHCP,
                        "dhcp%c: %s '%s' is invalid, will be ignored",
                        nm_utils_addr_family_to_char(config->addr_family),
                        config->use_fqdn ? "FQDN" : "hostname",
                        config->hostname);
            nm_clear_g_free((gpointer *) &config->hostname);
        }
    }
}

static void
config_clear(NMDhcpClientConfig *config)
{
    g_object_unref(config->l3cfg);

    nm_clear_l3cd(&config->previous_lease);

    nm_clear_pointer(&config->hwaddr, g_bytes_unref);
    nm_clear_pointer(&config->bcast_hwaddr, g_bytes_unref);
    nm_clear_pointer(&config->vendor_class_identifier, g_bytes_unref);
    nm_clear_pointer(&config->client_id, g_bytes_unref);

    nm_clear_g_free((gpointer *) &config->iface);
    nm_clear_g_free((gpointer *) &config->uuid);
    nm_clear_g_free((gpointer *) &config->anycast_address);
    nm_clear_g_free((gpointer *) &config->hostname);
    nm_clear_g_free((gpointer *) &config->mud_url);
    nm_clear_g_free((gpointer *) &config->reject_servers);

    if (config->addr_family == AF_INET) {
        nm_clear_g_free((gpointer *) &config->v4.last_address);
    }
}

/*****************************************************************************/

static void
set_property(GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec)
{
    NMDhcpClientPrivate *priv = NM_DHCP_CLIENT_GET_PRIVATE(object);

    switch (prop_id) {
    case PROP_CONFIG:
        /* construct-only */
        config_init(&priv->config, g_value_get_pointer(value));

        /* I know, this is technically not necessary. It just feels nicer to
         * explicitly initialize the respective union member. */
        if (NM_IS_IPv4(priv->config.addr_family)) {
            priv->v4 = (typeof(priv->v4)){
                .acd =
                    {
                        .addr                = INADDR_ANY,
                        .state               = NM_OPTION_BOOL_DEFAULT,
                        .l3cfg_commit_handle = NULL,
                        .done_source         = NULL,
                    },
            };
        } else {
            priv->v6 = (typeof(priv->v6)){
                .lladdr_timeout_source = NULL,
            };
        }
        break;
    default:
        G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
        break;
    }
}

/*****************************************************************************/

static void
nm_dhcp_client_init(NMDhcpClient *self)
{
    NMDhcpClientPrivate *priv;

    priv        = G_TYPE_INSTANCE_GET_PRIVATE(self, NM_TYPE_DHCP_CLIENT, NMDhcpClientPrivate);
    self->_priv = priv;

    priv->pid = -1;
}

static void
dispose(GObject *object)
{
    NMDhcpClient        *self = NM_DHCP_CLIENT(object);
    NMDhcpClientPrivate *priv = NM_DHCP_CLIENT_GET_PRIVATE(self);

    nm_dhcp_client_stop(self, FALSE);

    watch_cleanup(self);

    nm_clear_g_source_inst(&priv->previous_lease_timeout_source);
    nm_clear_g_source_inst(&priv->no_lease_timeout_source);

    if (!NM_IS_IPv4(priv->config.addr_family)) {
        nm_clear_g_source_inst(&priv->v6.lladdr_timeout_source);
        nm_clear_g_source_inst(&priv->v6.dad_timeout_source);
    }

    nm_clear_pointer(&priv->effective_client_id, g_bytes_unref);

    nm_assert(!priv->watch_source);
    nm_assert(!priv->l3cd_next);
    nm_assert(!priv->l3cd_curr);
    nm_assert(priv->l3cfg_notify.id == 0);

    G_OBJECT_CLASS(nm_dhcp_client_parent_class)->dispose(object);
}

static void
finalize(GObject *object)
{
    NMDhcpClient        *self = NM_DHCP_CLIENT(object);
    NMDhcpClientPrivate *priv = NM_DHCP_CLIENT_GET_PRIVATE(self);

    config_clear(&priv->config);

    G_OBJECT_CLASS(nm_dhcp_client_parent_class)->finalize(object);
}

static void
nm_dhcp_client_class_init(NMDhcpClientClass *client_class)
{
    GObjectClass *object_class = G_OBJECT_CLASS(client_class);

    g_type_class_add_private(client_class, sizeof(NMDhcpClientPrivate));

    object_class->dispose      = dispose;
    object_class->finalize     = finalize;
    object_class->set_property = set_property;
    client_class->accept       = _accept;
    client_class->decline      = decline;

    client_class->stop = stop;

    obj_properties[PROP_CONFIG] =
        g_param_spec_pointer(NM_DHCP_CLIENT_CONFIG,
                             "",
                             "",
                             G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS);

    g_object_class_install_properties(object_class, _PROPERTY_ENUMS_LAST, obj_properties);

    signals[SIGNAL_NOTIFY] =
        g_signal_new(NM_DHCP_CLIENT_NOTIFY,
                     G_OBJECT_CLASS_TYPE(object_class),
                     G_SIGNAL_RUN_FIRST,
                     0,
                     NULL,
                     NULL,
                     g_cclosure_marshal_VOID__POINTER,
                     G_TYPE_NONE,
                     1,
                     G_TYPE_POINTER /* const NMDhcpClientNotifyData *notify_data */);
}