File: netconfig.c

package info (click to toggle)
iwd 3.11-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 8,164 kB
  • sloc: ansic: 140,705; sh: 5,514; makefile: 714
file content (2490 lines) | stat: -rw-r--r-- 71,265 bytes parent folder | download | duplicates (7)
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
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
/*
 * Embedded Linux library
 * Copyright (C) 2022  Intel Corporation
 *
 * SPDX-License-Identifier: LGPL-2.1-or-later
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <asm/types.h>
#include <linux/netlink.h>
#include <linux/rtnetlink.h>
#include <sys/socket.h>
#include <net/if.h>
#include <linux/types.h>
#include <linux/if_ether.h>
#include <linux/if_arp.h>
#include <netinet/ip.h>
#include <arpa/inet.h>
#include <netinet/icmp6.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>

#include "private.h"
#include "useful.h"
#include "log.h"
#include "dhcp.h"
#include "dhcp-private.h"
#include "icmp6.h"
#include "icmp6-private.h"
#include "dhcp6.h"
#include "netlink.h"
#include "rtnl.h"
#include "rtnl-private.h"
#include "queue.h"
#include "time.h"
#include "idle.h"
#include "strv.h"
#include "net.h"
#include "net-private.h"
#include "acd.h"
#include "timeout.h"
#include "sysctl.h"
#include "netconfig.h"

struct l_netconfig {
	uint32_t ifindex;
	uint32_t route_priority;

	bool v4_enabled;
	struct l_rtnl_address *v4_static_addr;
	char *v4_gateway_override;
	char **v4_dns_override;
	char **v4_domain_names_override;
	bool acd_enabled;

	bool v6_enabled;
	struct l_rtnl_address *v6_static_addr;
	char *v6_gateway_override;
	char **v6_dns_override;
	char **v6_domain_names_override;
	bool optimistic_dad_enabled;

	bool started;
	struct l_idle *do_static_work;
	bool v4_configured;
	struct l_dhcp_client *dhcp_client;
	bool v6_configured;
	struct l_icmp6_client *icmp6_client;
	struct l_dhcp6_client *dhcp6_client;
	struct l_idle *signal_expired_work;
	unsigned int ifaddr6_dump_cmd_id;
	struct l_queue *icmp_route_data;
	struct l_acd *acd;
	uint32_t orig_disable_ipv6;
	uint32_t orig_optimistic_dad;
	uint8_t mac[ETH_ALEN];
	struct l_timeout *ra_timeout;
	bool have_lla;
	enum {
		NETCONFIG_V6_METHOD_UNSET,
		NETCONFIG_V6_METHOD_DHCP,       /* Managed bit set in RA */
		NETCONFIG_V6_METHOD_SLAAC_DHCP, /* Other bit set in RA */
		NETCONFIG_V6_METHOD_SLAAC,      /* Neither flag set in RA */
	} v6_auto_method;
	struct l_queue *slaac_dnses;
	struct l_queue *slaac_domains;

	/* These objects, if not NULL, are owned by @addresses and @routes */
	struct l_rtnl_address *v4_address;
	struct l_rtnl_route *v4_subnet_route;
	struct l_rtnl_route *v4_default_route;
	struct l_rtnl_address *v6_address;

	struct {
		struct l_queue *current;

		/*
		 * Temporary lists for use by the UPDATED handler to avoid
		 * having to remove all entries on the interface and re-add
		 * them from @current.  Entries in @updated are those that
		 * RTM_NEWADDR/RTM_NEWROUTE will correctly identify as
		 * existing objects and replace (with NLM_F_REPLACE) or
		 * error out (without it) rather than create duplicates,
		 * for example those that only have their lifetime updated.
		 *
		 * Any entries in @added and @updated are owned by @current.
		 * Entries in @removed need to be removed with an
		 * RTM_DELADD/RTM_DELROUTE while those in @expired are only
		 * informative as the kernel will have removed them already.
		 */
		struct l_queue *added;
		struct l_queue *updated;
		struct l_queue *removed;
		struct l_queue *expired;
	} addresses, routes;

	struct {
		l_netconfig_event_cb_t callback;
		void *user_data;
		l_netconfig_destroy_cb_t destroy;
	} handler;
};

struct netconfig_route_data {
	struct l_rtnl_route *route;
	uint64_t last_ra_time;
	uint64_t kernel_expiry;
	uint64_t max_ra_interval;
};

union netconfig_addr {
	struct in_addr v4;
	struct in6_addr v6;
};

static struct l_queue *addr_wait_list;
static unsigned int rtnl_id;

static const unsigned int max_icmp6_routes = 100;
static const unsigned int max_icmp6_dnses = 10;
static const unsigned int max_icmp6_domains = 10;

static void netconfig_update_cleanup(struct l_netconfig *nc)
{
	l_queue_clear(nc->addresses.added, NULL);
	l_queue_clear(nc->addresses.updated, NULL);
	l_queue_clear(nc->addresses.removed,
			(l_queue_destroy_func_t) l_rtnl_address_free);
	l_queue_clear(nc->addresses.expired,
			(l_queue_destroy_func_t) l_rtnl_address_free);
	l_queue_clear(nc->routes.added, NULL);
	l_queue_clear(nc->routes.updated, NULL);
	l_queue_clear(nc->routes.removed,
			(l_queue_destroy_func_t) l_rtnl_route_free);
	l_queue_clear(nc->routes.expired,
			(l_queue_destroy_func_t) l_rtnl_route_free);
}

static void netconfig_emit_event(struct l_netconfig *nc, uint8_t family,
					enum l_netconfig_event event)
{
	if (!nc->handler.callback)
		return;

	nc->handler.callback(nc, family, event, nc->handler.user_data);

	if (L_IN_SET(event, L_NETCONFIG_EVENT_UPDATE,
			L_NETCONFIG_EVENT_CONFIGURE,
			L_NETCONFIG_EVENT_UNCONFIGURE))
		netconfig_update_cleanup(nc);
}

static void netconfig_addr_wait_unregister(struct l_netconfig *nc,
						bool in_notify);

static void netconfig_failed(struct l_netconfig *nc, uint8_t family)
{
	if (family == AF_INET) {
		l_dhcp_client_stop(nc->dhcp_client);
		l_acd_destroy(l_steal_ptr(nc->acd));
	} else {
		netconfig_addr_wait_unregister(nc, false);
		l_dhcp6_client_stop(nc->dhcp6_client);
		l_icmp6_client_stop(nc->icmp6_client);
		l_timeout_remove(l_steal_ptr(nc->ra_timeout));
	}

	netconfig_emit_event(nc, family, L_NETCONFIG_EVENT_FAILED);
}

static struct l_rtnl_route *netconfig_route_new(struct l_netconfig *nc,
						uint8_t family,
						const void *dst,
						uint8_t prefix_len,
						const void *gw,
						uint8_t protocol)
{
	struct l_rtnl_route *rt = l_new(struct l_rtnl_route, 1);

	rt->family = family;
	rt->scope = (family == AF_INET && dst) ?
		RT_SCOPE_LINK : RT_SCOPE_UNIVERSE;
	rt->protocol = protocol;
	rt->lifetime = 0xffffffff;
	rt->priority = nc->route_priority;

	if (dst) {
		memcpy(&rt->dst, dst, family == AF_INET ? 4 : 16);
		rt->dst_prefix_len = prefix_len;
	}

	if (gw)
		memcpy(&rt->gw, gw, family == AF_INET ? 4 : 16);

	return rt;
}

static void netconfig_signal_expired(struct l_idle *idle, void *user_data)
{
	struct l_netconfig *nc = user_data;

	l_idle_remove(l_steal_ptr(nc->signal_expired_work));

	/*
	 * If the idle work was scheduled from within l_netconfig_get_routes
	 * or netconfig_icmp6_event_handler, the user is likely to have
	 * already received an event and had a chance to process the expired
	 * routes list.  In that case there's no need to emit a new event,
	 * and the list will have been emptied in netconfig_update_cleanup()
	 * anyway.
	 */
	if (!l_queue_isempty(nc->routes.expired))
		netconfig_emit_event(nc, AF_INET6, L_NETCONFIG_EVENT_UPDATE);
}

static void netconfig_add_v4_routes(struct l_netconfig *nc, const char *ip,
					uint8_t prefix_len, const char *gateway,
					uint8_t rtm_protocol)
{
	struct in_addr in_addr;

	/* Subnet route */

	if (L_WARN_ON(inet_pton(AF_INET, ip, &in_addr) != 1))
		return;

	in_addr.s_addr &= htonl(0xfffffffflu << (32 - prefix_len));
	nc->v4_subnet_route = netconfig_route_new(nc, AF_INET, &in_addr,
							prefix_len, NULL,
							rtm_protocol);
	l_queue_push_tail(nc->routes.current, nc->v4_subnet_route);
	l_queue_push_tail(nc->routes.added, nc->v4_subnet_route);

	/* Gateway route */

	if (nc->v4_gateway_override) {
		gateway = nc->v4_gateway_override;
		rtm_protocol = RTPROT_STATIC;
	}

	if (!gateway)
		return;

	nc->v4_default_route = l_rtnl_route_new_gateway(gateway);
	l_rtnl_route_set_protocol(nc->v4_default_route, rtm_protocol);
	L_WARN_ON(!l_rtnl_route_set_prefsrc(nc->v4_default_route, ip));
	l_rtnl_route_set_priority(nc->v4_default_route, nc->route_priority);
	l_queue_push_tail(nc->routes.current, nc->v4_default_route);
	l_queue_push_tail(nc->routes.added, nc->v4_default_route);
}

static void netconfig_add_v6_static_routes(struct l_netconfig *nc,
						const char *ip,
						uint8_t prefix_len)
{
	struct in6_addr in6_addr;
	const void *prefix;
	struct l_rtnl_route *v6_subnet_route;
	struct l_rtnl_route *v6_default_route;

	/* Subnet route */

	if (L_WARN_ON(inet_pton(AF_INET6, ip, &in6_addr) != 1))
		return;

	/*
	 * Zero out host address bits, aka. interface ID, to produce
	 * the network address or prefix.
	 */
	prefix = net_prefix_from_ipv6(in6_addr.s6_addr, prefix_len);

	/*
	 * One reason we add a subnet route instead of letting the kernel
	 * do it, by not specifying IFA_F_NOPREFIXROUTE for the address,
	 * is that that would force a 0 metric for the route.
	 */
	v6_subnet_route = netconfig_route_new(nc, AF_INET6, prefix, prefix_len,
						NULL, RTPROT_STATIC);
	l_queue_push_tail(nc->routes.current, v6_subnet_route);
	l_queue_push_tail(nc->routes.added, v6_subnet_route);

	/* Gateway route */

	if (!nc->v6_gateway_override)
		return;

	v6_default_route = l_rtnl_route_new_gateway(nc->v6_gateway_override);
	l_rtnl_route_set_protocol(v6_default_route, RTPROT_STATIC);
	/*
	 * TODO: Optimally we'd set the prefsrc on the route with:
	 * L_WARN_ON(!l_rtnl_route_set_prefsrc(v6_default_route, ip));
	 *
	 * but that means that we can only commit the route to the kernel
	 * with an RTM_NEWROUTE command after the corresponding RTM_NEWADDR
	 * has returned and the kernel has finished DAD for the address and
	 * cleared IFA_F_TENTATIVE.  That will complicate
	 * l_netconfig_apply_rtnl() significantly but may be inevitable.
	 */

	l_queue_push_tail(nc->routes.current, v6_default_route);
	l_queue_push_tail(nc->routes.added, v6_default_route);
}

static bool netconfig_address_exists(struct l_queue *list,
					const struct l_rtnl_address *address)
{
	const struct l_queue_entry *entry;

	for (entry = l_queue_get_entries(list); entry;
			entry = entry->next)
		if ((const struct l_rtnl_address *) entry->data == address)
			return true;

	return false;
}

static bool netconfig_route_exists(struct l_queue *list,
					const struct l_rtnl_route *route)
{
	const struct l_queue_entry *entry;

	for (entry = l_queue_get_entries(list); entry;
			entry = entry->next)
		if ((const struct l_rtnl_route *) entry->data == route)
			return true;

	return false;
}

static void netconfig_add_dhcp_address_routes(struct l_netconfig *nc)
{
	const struct l_dhcp_lease *lease =
		l_dhcp_client_get_lease(nc->dhcp_client);
	_auto_(l_free) char *ip = NULL;
	_auto_(l_free) char *broadcast = NULL;
	_auto_(l_free) char *gateway = NULL;
	uint32_t prefix_len;

	ip = l_dhcp_lease_get_address(lease);
	broadcast = l_dhcp_lease_get_broadcast(lease);

	prefix_len = l_dhcp_lease_get_prefix_length(lease);
	if (!prefix_len)
		prefix_len = 24;

	nc->v4_address = l_rtnl_address_new(ip, prefix_len);
	if (L_WARN_ON(!nc->v4_address))
		return;

	l_rtnl_address_set_noprefixroute(nc->v4_address, true);

	if (broadcast)
		l_rtnl_address_set_broadcast(nc->v4_address, broadcast);

	l_queue_push_tail(nc->addresses.current, nc->v4_address);
	l_queue_push_tail(nc->addresses.added, nc->v4_address);

	gateway = l_dhcp_lease_get_gateway(lease);
	netconfig_add_v4_routes(nc, ip, prefix_len, gateway, RTPROT_DHCP);
}

static void netconfig_set_dhcp_lifetimes(struct l_netconfig *nc, bool updated)
{
	const struct l_dhcp_lease *lease =
		l_dhcp_client_get_lease(nc->dhcp_client);
	uint32_t lifetime = l_dhcp_lease_get_lifetime(lease);
	uint64_t expiry = l_dhcp_lease_get_start_time(lease) +
		lifetime * L_USEC_PER_SEC;

	l_rtnl_address_set_lifetimes(nc->v4_address, lifetime, lifetime);
	l_rtnl_address_set_expiry(nc->v4_address, expiry, expiry);

	if (updated && !netconfig_address_exists(nc->addresses.added,
							nc->v4_address))
		l_queue_push_tail(nc->addresses.updated, nc->v4_address);

	l_rtnl_route_set_lifetime(nc->v4_subnet_route, lifetime);
	l_rtnl_route_set_expiry(nc->v4_subnet_route, expiry);

	if (updated && !netconfig_route_exists(nc->routes.added,
						nc->v4_subnet_route))
		l_queue_push_tail(nc->routes.updated, nc->v4_subnet_route);

	if (!nc->v4_default_route)
		return;

	l_rtnl_route_set_lifetime(nc->v4_default_route, lifetime);
	l_rtnl_route_set_expiry(nc->v4_default_route, expiry);

	if (updated && !netconfig_route_exists(nc->routes.added,
						nc->v4_default_route))
		l_queue_push_tail(nc->routes.updated, nc->v4_default_route);
}

static void netconfig_remove_v4_address_routes(struct l_netconfig *nc,
						bool expired)
{
	struct l_queue *routes =
		expired ? nc->routes.expired : nc->routes.removed;

	l_queue_remove(nc->addresses.current, nc->v4_address);
	l_queue_remove(nc->addresses.updated, nc->v4_address);

	if (!l_queue_remove(nc->addresses.added, nc->v4_address))
		l_queue_push_tail(
			expired ? nc->addresses.expired : nc->addresses.removed,
			nc->v4_address);

	nc->v4_address = NULL;

	l_queue_remove(nc->routes.current, nc->v4_subnet_route);
	l_queue_remove(nc->routes.updated, nc->v4_subnet_route);

	if (!l_queue_remove(nc->routes.added, nc->v4_subnet_route))
		l_queue_push_tail(routes, nc->v4_subnet_route);

	nc->v4_subnet_route = NULL;

	if (nc->v4_default_route) {
		l_queue_remove(nc->routes.current, nc->v4_default_route);
		l_queue_remove(nc->routes.updated, nc->v4_default_route);

		if (!l_queue_remove(nc->routes.added, nc->v4_default_route))
			l_queue_push_tail(routes, nc->v4_default_route);

		nc->v4_default_route = NULL;
	}
}

static void netconfig_set_neighbor_entry_cb(int error,
						uint16_t type, const void *data,
						uint32_t len, void *user_data)
{
	/* Not critical.  TODO: log warning */
}

static int netconfig_dhcp_gateway_to_arp(struct l_netconfig *nc)
{
	const struct l_dhcp_lease *lease =
		l_dhcp_client_get_lease(nc->dhcp_client);
	_auto_(l_free) char *server_id = l_dhcp_lease_get_server_id(lease);
	_auto_(l_free) char *gw = l_dhcp_lease_get_gateway(lease);
	const uint8_t *server_mac = l_dhcp_lease_get_server_mac(lease);
	struct in_addr in_gw;

	if (!gw || strcmp(server_id, gw) || !server_mac)
		return -ENOENT;

	/* Gateway MAC is known, write it into ARP cache to save ARP traffic */
	in_gw.s_addr = l_dhcp_lease_get_gateway_u32(lease);

	if (!l_rtnl_neighbor_set_hwaddr(l_rtnl_get(), nc->ifindex, AF_INET,
					&in_gw, server_mac, ETH_ALEN,
					netconfig_set_neighbor_entry_cb, nc,
					NULL))
		return -EIO;

	return 0;
}

static void netconfig_dhcp_event_handler(struct l_dhcp_client *client,
						enum l_dhcp_client_event event,
						void *user_data)
{
	struct l_netconfig *nc = user_data;

	switch (event) {
	case L_DHCP_CLIENT_EVENT_IP_CHANGED:
		if (L_WARN_ON(!nc->v4_configured))
			break;

		netconfig_remove_v4_address_routes(nc, false);
		netconfig_add_dhcp_address_routes(nc);
		netconfig_set_dhcp_lifetimes(nc, false);
		netconfig_emit_event(nc, AF_INET, L_NETCONFIG_EVENT_UPDATE);
		break;
	case L_DHCP_CLIENT_EVENT_LEASE_OBTAINED:
		if (L_WARN_ON(nc->v4_configured))
			break;

		netconfig_add_dhcp_address_routes(nc);
		netconfig_set_dhcp_lifetimes(nc, false);
		nc->v4_configured = true;
		netconfig_emit_event(nc, AF_INET, L_NETCONFIG_EVENT_CONFIGURE);
		netconfig_dhcp_gateway_to_arp(nc);
		break;
	case L_DHCP_CLIENT_EVENT_LEASE_RENEWED:
		if (L_WARN_ON(!nc->v4_configured))
			break;

		netconfig_set_dhcp_lifetimes(nc, true);
		netconfig_emit_event(nc, AF_INET, L_NETCONFIG_EVENT_UPDATE);
		break;
	case L_DHCP_CLIENT_EVENT_LEASE_EXPIRED:
		if (L_WARN_ON(!nc->v4_configured))
			break;

		netconfig_remove_v4_address_routes(nc, true);
		nc->v4_configured = false;

		if (l_dhcp_client_start(nc->dhcp_client))
			/* TODO: also start a new timeout */
			netconfig_emit_event(nc, AF_INET,
						L_NETCONFIG_EVENT_UNCONFIGURE);
		else
			netconfig_failed(nc, AF_INET);

		break;
	case L_DHCP_CLIENT_EVENT_NO_LEASE:
		L_WARN_ON(nc->v4_configured);

		/*
		 * The requested address is no longer available, try to restart
		 * the client.
		 *
		 * TODO: this may need to be delayed so we don't flood the
		 * network with DISCOVERs and NAKs.  Also add a retry limit or
		 * better yet a configurable timeout.
		 */
		if (!l_dhcp_client_start(nc->dhcp_client))
			netconfig_failed(nc, AF_INET);

		break;
	case L_DHCP_CLIENT_EVENT_MAX_ATTEMPTS_REACHED:
		L_WARN_ON(nc->v4_configured);

		netconfig_failed(nc, AF_INET);

		break;
	}
}

static void netconfig_add_dhcp6_address(struct l_netconfig *nc)
{
	const struct l_dhcp6_lease *lease =
		l_dhcp6_client_get_lease(nc->dhcp6_client);
	_auto_(l_free) char *ip = NULL;
	uint32_t prefix_len;

	if (L_WARN_ON(!lease))
		return;

	ip = l_dhcp6_lease_get_address(lease);
	prefix_len = l_dhcp6_lease_get_prefix_length(lease);
	nc->v6_address = l_rtnl_address_new(ip, prefix_len);

	if (L_WARN_ON(!nc->v6_address))
		return;

	/*
	 * Assume we already have a route from a Router Advertisement
	 * covering the address from DHCPv6 + prefix length from DHCPv6.
	 * We might want to emit a warning of some sort or
	 * L_NETCONFIG_EVENT_FAILED if we don't since this would
	 * basically be fatal for IPv6 connectivity.
	 */
	l_rtnl_address_set_noprefixroute(nc->v6_address, true);

	l_queue_push_tail(nc->addresses.current, nc->v6_address);
	l_queue_push_tail(nc->addresses.added, nc->v6_address);
}

static void netconfig_set_dhcp6_address_lifetimes(struct l_netconfig *nc,
							bool updated)
{
	const struct l_dhcp6_lease *lease =
		l_dhcp6_client_get_lease(nc->dhcp6_client);
	uint32_t p, v;
	uint64_t start_time;

	if (L_WARN_ON(!lease))
		return;

	p = l_dhcp6_lease_get_preferred_lifetime(lease);
	v = l_dhcp6_lease_get_valid_lifetime(lease);
	start_time = l_dhcp6_lease_get_start_time(lease);

	l_rtnl_address_set_lifetimes(nc->v6_address, p, v);
	l_rtnl_address_set_expiry(nc->v6_address,
					start_time + p * L_USEC_PER_SEC,
					start_time + v * L_USEC_PER_SEC);

	if (updated && !netconfig_address_exists(nc->addresses.added,
							nc->v6_address))
		l_queue_push_tail(nc->addresses.updated, nc->v6_address);
}

static void netconfig_remove_dhcp6_address(struct l_netconfig *nc, bool expired)
{
	l_queue_remove(nc->addresses.current, nc->v6_address);
	l_queue_remove(nc->addresses.updated, nc->v6_address);

	if (!l_queue_remove(nc->addresses.added, nc->v6_address))
		l_queue_push_tail(
			expired ? nc->addresses.expired : nc->addresses.removed,
			nc->v6_address);

	nc->v6_address = NULL;
}

static void netconfig_dhcp6_event_handler(struct l_dhcp6_client *client,
						enum l_dhcp6_client_event event,
						void *user_data)
{
	struct l_netconfig *nc = user_data;

	switch (event) {
	case L_DHCP6_CLIENT_EVENT_LEASE_OBTAINED:
		if (L_WARN_ON(nc->v6_configured))
			break;

		if (nc->v6_auto_method == NETCONFIG_V6_METHOD_DHCP) {
			netconfig_add_dhcp6_address(nc);
			netconfig_set_dhcp6_address_lifetimes(nc, false);
		}

		nc->v6_configured = true;
		netconfig_emit_event(nc, AF_INET6, L_NETCONFIG_EVENT_CONFIGURE);
		break;
	case L_DHCP6_CLIENT_EVENT_IP_CHANGED:
		if (L_WARN_ON(!nc->v6_configured ||
				nc->v6_auto_method != NETCONFIG_V6_METHOD_DHCP))
			break;

		netconfig_remove_dhcp6_address(nc, false);
		netconfig_add_dhcp6_address(nc);
		netconfig_set_dhcp6_address_lifetimes(nc, false);
		netconfig_emit_event(nc, AF_INET6, L_NETCONFIG_EVENT_UPDATE);
		break;
	case L_DHCP6_CLIENT_EVENT_LEASE_EXPIRED:
		if (L_WARN_ON(!nc->v6_configured ||
				nc->v6_auto_method != NETCONFIG_V6_METHOD_DHCP))
			break;

		netconfig_remove_dhcp6_address(nc, true);
		nc->v6_configured = false;

		if (l_dhcp6_client_start(nc->dhcp6_client))
			/* TODO: also start a new timeout */
			netconfig_emit_event(nc, AF_INET6,
						L_NETCONFIG_EVENT_UNCONFIGURE);
		else
			netconfig_failed(nc, AF_INET6);

		break;
	case L_DHCP6_CLIENT_EVENT_LEASE_RENEWED:
		if (L_WARN_ON(!nc->v6_configured))
			break;

		if (nc->v6_auto_method == NETCONFIG_V6_METHOD_DHCP)
			netconfig_set_dhcp6_address_lifetimes(nc, true);

		netconfig_emit_event(nc, AF_INET6, L_NETCONFIG_EVENT_UPDATE);
		break;
	case L_DHCP6_CLIENT_EVENT_NO_LEASE:
		if (L_WARN_ON(nc->v6_configured))
			break;

		if (nc->v6_auto_method == NETCONFIG_V6_METHOD_SLAAC_DHCP &&
				!l_queue_isempty(nc->slaac_dnses))
			break;

		/*
		 * The requested address is no longer available, try to restart
		 * the client.
		 *
		 * TODO: this may need to be delayed so we don't flood the
		 * network with SOLICITs and DECLINEs.  Also add a retry limit
		 * or better yet a configurable timeout.
		 */
		if (!l_dhcp6_client_start(nc->dhcp6_client))
			netconfig_failed(nc, AF_INET6);

		break;
	}
}

static bool netconfig_match(const void *a, const void *b)
{
	return a == b;
}

static bool netconfig_match_addr(const void *a, const void *b)
{
	return memcmp(a, b, 16) == 0;
}

static bool netconfig_match_str(const void *a, const void *b)
{
	return strcmp(a, b) == 0;
}

static bool netconfig_check_start_dhcp6(struct l_netconfig *nc)
{
	/* Don't start DHCPv6 until we get an RA with the managed bit set */
	if (nc->ra_timeout || !L_IN_SET(nc->v6_auto_method,
				NETCONFIG_V6_METHOD_DHCP,
				NETCONFIG_V6_METHOD_SLAAC_DHCP))
		return true;

	/* Don't start DHCPv6 while waiting for the link-local address */
	if (!nc->have_lla)
		return true;

	return l_dhcp6_client_start(nc->dhcp6_client);
}

static void netconfig_ra_timeout_cb(struct l_timeout *timeout, void *user_data)
{
	struct l_netconfig *nc = user_data;

	/* No Router Advertisements received, assume no DHCPv6 or SLAAC */
	netconfig_failed(nc, AF_INET6);
}

static void netconfig_add_slaac_address(struct l_netconfig *nc,
					const struct l_icmp6_router *r)
{
	unsigned int i;
	const struct autoconf_prefix_info *longest = &r->ac_prefixes[0];
	uint8_t addr[16];
	char addr_str[INET6_ADDRSTRLEN];
	uint32_t p, v;

	/* Find the autoconfiguration prefix that offers the longest lifetime */
	for (i = 1; i < r->n_ac_prefixes; i++)
		if (r->ac_prefixes[i].preferred_lifetime >
				longest->preferred_lifetime)
			longest = &r->ac_prefixes[i];

	memcpy(addr, longest->prefix, 8);
	/* EUI-64-based Interface Identifier (RFC2464 Section 4) */
	addr[ 8] = nc->mac[0] ^ 0x02;
	addr[ 9] = nc->mac[1];
	addr[10] = nc->mac[2];
	addr[11] = 0xff;
	addr[12] = 0xfe;
	addr[13] = nc->mac[3];
	addr[14] = nc->mac[4];
	addr[15] = nc->mac[5];
	inet_ntop(AF_INET6, addr, addr_str, sizeof(addr_str));
	p = longest->preferred_lifetime;
	v = longest->valid_lifetime;

	nc->v6_address = l_rtnl_address_new(addr_str, 128);
	l_rtnl_address_set_noprefixroute(nc->v6_address, true);

	if (p != 0xffffffff || v != 0xffffffff) {
		l_rtnl_address_set_lifetimes(nc->v6_address,
					p != 0xffffffff ? p : 0,
					v != 0xffffffff ? v : 0);
		l_rtnl_address_set_expiry(nc->v6_address,
					p != 0xffffffff ?
					r->start_time + p * L_USEC_PER_SEC : 0,
					v != 0xffffffff ?
					r->start_time + v * L_USEC_PER_SEC : 0);
	}

	l_queue_push_tail(nc->addresses.current, nc->v6_address);
	l_queue_push_tail(nc->addresses.added, nc->v6_address);

	if (nc->v6_auto_method == NETCONFIG_V6_METHOD_SLAAC ||
			nc->slaac_dnses) {
		nc->v6_configured = true;
		netconfig_emit_event(nc, AF_INET6, L_NETCONFIG_EVENT_CONFIGURE);
	} else
		netconfig_emit_event(nc, AF_INET6, L_NETCONFIG_EVENT_UPDATE);

	/* TODO: set a renew timeout */
}

static void netconfig_set_slaac_address_lifetimes(struct l_netconfig *nc,
						const struct l_icmp6_router *r)
{
	const uint8_t *addr = l_rtnl_address_get_in_addr(nc->v6_address);
	bool updated = false;
	uint64_t p_expiry;
	uint64_t v_expiry;
	uint32_t remaining = 0xffffffff;
	unsigned int i;

	if (L_WARN_ON(!addr))
		return;

	l_rtnl_address_get_expiry(nc->v6_address, &p_expiry, &v_expiry);

	if (v_expiry)
		remaining = (v_expiry - r->start_time) / L_USEC_PER_SEC;

	for (i = 0; i < r->n_ac_prefixes; i++) {
		const struct autoconf_prefix_info *prefix = &r->ac_prefixes[i];
		uint32_t p = prefix->preferred_lifetime;
		uint32_t v = prefix->valid_lifetime;

		if (memcmp(prefix->prefix, addr, 8))
			continue;

		/* RFC4862 Section 5.5.3 e) */
		if (v < 120 * 60 && v < remaining)
			v = 120 * 60; /* 2 hours */

		l_rtnl_address_set_lifetimes(nc->v6_address,
						p != 0xffffffff ? p : 0,
						v != 0xffffffff ? v : 0);
		p_expiry = p != 0xffffffff ? r->start_time + p * L_USEC_PER_SEC : 0;
		v_expiry = v != 0xffffffff ? r->start_time + v * L_USEC_PER_SEC : 0;
		l_rtnl_address_set_expiry(nc->v6_address, p_expiry, v_expiry);
		updated = true;

		/*
		 * TODO: modify the renew timeout.
		 *
		 * Also we probably want to apply a mechanism similar to that
		 * in netconfig_check_route_need_update() to avoid generating
		 * and UPDATED event for every RA that covers this prefix
		 * with constant lifetime values.
		 */
	}

	if (updated && !l_queue_find(nc->addresses.added, netconfig_match,
					nc->v6_address))
		l_queue_push_tail(nc->addresses.updated, nc->v6_address);
}

static bool netconfig_process_slaac_dns_info(struct l_netconfig *nc,
						const struct l_icmp6_router *r)
{
	bool updated = false;
	unsigned int i;
	unsigned int n_dns = l_queue_length(nc->slaac_dnses);
	unsigned int n_domains = l_queue_length(nc->slaac_domains);

	for (i = 0; i < r->n_dns; i++) {
		const struct dns_info *info = &r->dns_list[i];

		/*
		 * For simplicity don't track lifetimes (TODO), add entries
		 * when the lifetime is non-zero, remove them when the
		 * lifetime is zero.  We have no API to add time-limited
		 * entries to the system either.
		 *
		 * RFC8106 Section 5.1: "A value of zero means that the RDNSS
		 * addresses MUST no longer be used."
		 */
		if (info->lifetime) {
			if (n_dns >= max_icmp6_dnses)
				continue;

			if (l_queue_find(nc->slaac_dnses, netconfig_match_addr,
						info->address))
				continue;

			l_queue_push_tail(nc->slaac_dnses,
						l_memdup(info->address, 16));
			n_dns++;
		} else {
			void *addr = l_queue_remove_if(nc->slaac_dnses,
							netconfig_match_addr,
							info->address);

			if (!addr)
				continue;

			l_free(addr);
			n_dns--;
		}

		updated = true;
	}

	for (i = 0; i < r->n_domains; i++) {
		const struct domain_info *info = &r->domains[i];

		/*
		 * RFC8106 Section 5.2: "A value of zero means that the DNSSL
		 * domain names MUST no longer be used."
		 */
		if (info->lifetime) {
			if (n_domains >= max_icmp6_domains)
				continue;

			if (l_queue_find(nc->slaac_domains, netconfig_match_str,
						info->domain))
				continue;

			l_queue_push_tail(nc->slaac_domains,
						l_strdup(info->domain));
			n_domains++;
		} else {
			void *str = l_queue_remove_if(nc->slaac_domains,
							netconfig_match_str,
							info->domain);

			if (!str)
				continue;

			l_free(str);
			n_domains--;
		}

		updated = true;
	}

	return updated;
}

static uint64_t now;

static bool netconfig_check_route_expired(void *data, void *user_data)
{
	struct l_netconfig *nc = user_data;
	struct netconfig_route_data *rd = data;

	if (!rd->kernel_expiry || now < rd->kernel_expiry)
		return false;

	/*
	 * Since we set lifetimes on the routes we submit to the kernel with
	 * RTM_NEWROUTE, we count on them being deleted automatically so no
	 * need to send an RTM_DELROUTE.  We signal the fact that the route
	 * expired to the user by having it on the expired list but there's
	 * nothing that the user needs to do with the routes on that list
	 * like they do with the added, updated and removed lists.
	 *
	 * If for some reason the route is still on the added list, drop it
	 * from there and there's nothing to notify the user of.
	 */
	if (!l_queue_remove(nc->routes.added, rd->route))
		l_queue_push_tail(nc->routes.expired, rd->route);

	l_queue_remove(nc->routes.current, rd->route);
	l_queue_remove(nc->routes.updated, rd->route);
	l_queue_remove(nc->routes.removed, rd->route);
	return true;
}

static void netconfig_expire_routes(struct l_netconfig *nc)
{
	now = l_time_now();

	if (l_queue_foreach_remove(nc->icmp_route_data,
					netconfig_check_route_expired, nc) &&
			!l_queue_isempty(nc->routes.expired) &&
			!nc->signal_expired_work)
		nc->signal_expired_work = l_idle_create(
						netconfig_signal_expired,
						nc, NULL);
}

static struct netconfig_route_data *netconfig_find_icmp6_route(
						struct l_netconfig *nc,
						const uint8_t *gateway,
						const struct route_info *dst)
{
	const struct l_queue_entry *entry;

	for (entry = l_queue_get_entries(nc->icmp_route_data); entry;
			entry = entry->next) {
		struct netconfig_route_data *rd = entry->data;
		const uint8_t *route_gateway;
		const uint8_t *route_dst;
		uint8_t route_prefix_len = 0;

		route_gateway = l_rtnl_route_get_gateway_in_addr(rd->route);
		if ((gateway || route_gateway) &&
				(!gateway || !route_gateway ||
				 memcmp(gateway, route_gateway, 16)))
			continue;

		route_dst = l_rtnl_route_get_dst_in_addr(rd->route,
							&route_prefix_len);
		if ((dst || route_prefix_len) &&
				(!dst || !route_prefix_len ||
				 dst->prefix_len != route_prefix_len ||
				 memcmp(dst->address, route_dst, 16)))
			continue;

		return rd;
	}

	return NULL;
}

static struct netconfig_route_data *netconfig_add_icmp6_route(
						struct l_netconfig *nc,
						const uint8_t *gateway,
						const struct route_info *dst,
						uint8_t preference)
{
	struct netconfig_route_data *rd;
	struct l_rtnl_route *rt;

	if (l_queue_length(nc->icmp_route_data) >= max_icmp6_routes)
		return NULL;	/* TODO: log a warning the first time */

	rt = netconfig_route_new(nc, AF_INET6, dst ? dst->address : NULL,
					dst ? dst->prefix_len : 0, gateway,
					RTPROT_RA);
	if (L_WARN_ON(!rt))
		return NULL;

	l_rtnl_route_set_preference(rt, preference);
	l_queue_push_tail(nc->routes.current, rt);
	l_queue_push_tail(nc->routes.added, rt);

	rd = l_new(struct netconfig_route_data, 1);
	rd->route = rt;
	l_queue_push_tail(nc->icmp_route_data, rd);
	return rd;
}

static bool netconfig_check_route_need_update(
					const struct netconfig_route_data *rd,
					const struct l_icmp6_router *ra,
					uint64_t new_expiry,
					uint64_t old_expiry)
{
	/*
	 * Decide whether the route is close enough to its expiry time that,
	 * based on the expected Router Advertisement frequency, we should
	 * notify the user and have them update the route's lifetime in the
	 * kernel.  This is an optimization to avoid triggering a syscall and
	 * potentially multiple context-switches in case we expect to have
	 * many more opportunities to update the lifetime before we even get
	 * close to the last expiry time we passed to the kernel.  Without
	 * this we might be wasting a lot of cycles over time if the RAs are
	 * frequent.
	 *
	 * Always update if we have no RA interval information or if the
	 * expiry is moved forward.
	 */
	if (!rd->max_ra_interval || new_expiry < rd->kernel_expiry)
		return true;

	return rd->kernel_expiry < ra->start_time + rd->max_ra_interval * 10;
}

static void netconfig_set_icmp6_route_data(struct l_netconfig *nc,
						struct netconfig_route_data *rd,
						const struct l_icmp6_router *ra,
						uint32_t valid_lifetime,
						uint32_t mtu, bool updated)
{
	uint64_t expiry = ra->start_time + valid_lifetime * L_USEC_PER_SEC;
	uint64_t old_expiry = l_rtnl_route_get_expiry(rd->route);
	bool differs = false;

	if (mtu != l_rtnl_route_get_mtu(rd->route)) {
		l_rtnl_route_set_mtu(rd->route, mtu);
		differs = true;
	}

	/*
	 * The route's lifetime is pretty useless on its own but keep it
	 * updated with the value from the last RA.  Routers can send the same
	 * lifetime in every RA, keep decreasing the lifetimes linearly or
	 * implement any other policy, regardless of whether the resulting
	 * expiry time varies or not.
	 */
	l_rtnl_route_set_lifetime(rd->route, valid_lifetime);

	if (rd->last_ra_time) {
		uint64_t interval = ra->start_time - rd->last_ra_time;

		if (interval > rd->max_ra_interval)
			rd->max_ra_interval = interval;
	}

	rd->last_ra_time = ra->start_time;

	/*
	 * valid_lifetime of 0 from a route_info means the route is being
	 * removed so we wouldn't be here.  valid_lifetime of 0xffffffff
	 * means no timeout.  Check if the lifetime is changing between
	 * finite and infinite, or two finite values that result in expiry
	 * time difference of more than a second -- to avoid emitting
	 * updates for changes resulting only from the valid_lifetime one
	 * second resolution and RA transmission jitter.  As RFC4861
	 * Section 6.2.7 puts it: "Due to link propagation delays and
	 * potentially poorly synchronized clocks between the routers such
	 * comparison SHOULD allow some time skew."  The RFC talks about
	 * routers processing one another's RAs but the same logic applies
	 * here.
	 */
	if (valid_lifetime == 0xffffffff)
		expiry = 0;

	if ((expiry || old_expiry) &&
			(!expiry || !old_expiry ||
			 l_time_diff(expiry, old_expiry) > L_USEC_PER_SEC)) {
		l_rtnl_route_set_expiry(rd->route, expiry);

		differs = differs || !expiry || !old_expiry ||
			netconfig_check_route_need_update(rd, ra,
							expiry, old_expiry);
	}

	if (updated && differs && !netconfig_route_exists(nc->routes.added,
								rd->route)) {
		l_queue_push_tail(nc->routes.updated, rd->route);
		rd->kernel_expiry = expiry;
	}
}

static void netconfig_remove_icmp6_route(struct l_netconfig *nc,
						struct netconfig_route_data *rd)
{
	l_queue_remove(nc->icmp_route_data, rd);
	l_queue_remove(nc->routes.current, rd->route);
	l_queue_remove(nc->routes.updated, rd->route);

	if (!l_queue_remove(nc->routes.added, rd->route))
		l_queue_push_tail(nc->routes.removed, rd->route);
}

static void netconfig_icmp6_event_handler(struct l_icmp6_client *client,
						enum l_icmp6_client_event event,
						void *event_data,
						void *user_data)
{
	struct l_netconfig *nc = user_data;
	const struct l_icmp6_router *r;
	struct netconfig_route_data *default_rd;
	unsigned int i;
	bool dns_updated = false;

	if (event != L_ICMP6_CLIENT_EVENT_ROUTER_FOUND)
		return;

	r = event_data;

	if (nc->ra_timeout)
		l_timeout_remove(l_steal_ptr(nc->ra_timeout));

	netconfig_expire_routes(nc);

	if (nc->v6_gateway_override)
		goto process_nondefault_routes;

	/* Process the default gateway information */
	default_rd = netconfig_find_icmp6_route(nc, r->address, NULL);

	if (!default_rd && r->lifetime) {
		default_rd = netconfig_add_icmp6_route(nc, r->address, NULL,
								r->pref);
		if (unlikely(!default_rd))
			return;

		/*
		 * r->lifetime is 16-bit only so there's no risk it gets
		 * confused for the special 0xffffffff value in
		 * netconfig_set_icmp6_route_data.
		 */
		netconfig_set_icmp6_route_data(nc, default_rd, r, r->lifetime,
						r->mtu, false);
	} else if (default_rd && r->lifetime)
		netconfig_set_icmp6_route_data(nc, default_rd, r, r->lifetime,
						r->mtu, true);
	else if (default_rd && !r->lifetime)
		netconfig_remove_icmp6_route(nc, default_rd);

process_nondefault_routes:
	/*
	 * Process the onlink and offlink routes, from the Router
	 * Advertisement's Prefix Information options and Route
	 * Information options respectively.
	 */
	for (i = 0; i < r->n_routes; i++) {
		const struct route_info *info = &r->routes[i];
		const uint8_t *gateway = info->onlink ? NULL : r->address;
		struct netconfig_route_data *rd =
			netconfig_find_icmp6_route(nc, gateway, info);

		if (!rd && info->valid_lifetime) {
			rd = netconfig_add_icmp6_route(nc, gateway, info,
							info->preference);
			if (unlikely(!rd))
				continue;

			netconfig_set_icmp6_route_data(nc, rd, r,
						info->valid_lifetime,
						gateway ? r->mtu : 0, false);
		} else if (rd && info->valid_lifetime)
			netconfig_set_icmp6_route_data(nc, rd, r,
						info->valid_lifetime,
						gateway ? r->mtu : 0, true);
		else if (rd && !info->valid_lifetime)
			netconfig_remove_icmp6_route(nc, rd);
	}

	/*
	 * Do this first so that any changes are included in the event
	 * emitted next, be it UPDATE or CONFIGURE.
	 */
	if (r->n_dns || r->n_domains) {
		if (!nc->slaac_dnses && r->n_dns)
			nc->slaac_dnses = l_queue_new();

		if (!nc->slaac_domains && r->n_domains)
			nc->slaac_domains = l_queue_new();

		dns_updated = netconfig_process_slaac_dns_info(nc, r);
	}

	/*
	 * For lack of a better policy, select between DHCPv6 and SLAAC based
	 * on the first RA received.  Prefer DHCPv6.
	 *
	 * Just like we currently only request one address in l_dhcp6_client,
	 * we only set up one address using SLAAC regardless of how many
	 * prefixes are available.  Generate the address in the prefix that
	 * offers the longest preferred_lifetime.
	 */
	if (nc->v6_auto_method == NETCONFIG_V6_METHOD_UNSET &&
			l_icmp6_router_get_managed(r)) {
		nc->v6_auto_method = NETCONFIG_V6_METHOD_DHCP;
		l_dhcp6_client_set_stateless(nc->dhcp6_client, false);

		if (!netconfig_check_start_dhcp6(nc)) {
			netconfig_failed(nc, AF_INET6);
			return;
		}

		goto emit_event;
	}

	/*
	 * Stateful DHCP not available according to this router, check if
	 * any of the prefixes allow SLAAC.
	 */
	if (nc->v6_auto_method == NETCONFIG_V6_METHOD_UNSET &&
			r->n_ac_prefixes) {
		if (l_icmp6_router_get_other(r)) {
			nc->v6_auto_method = NETCONFIG_V6_METHOD_SLAAC_DHCP;
			l_dhcp6_client_set_stateless(nc->dhcp6_client, true);
			netconfig_check_start_dhcp6(nc);
		} else
			nc->v6_auto_method = NETCONFIG_V6_METHOD_SLAAC;

		/*
		 * The DAD for the link-local address may be still running
		 * but again we can generate the global address already and
		 * commit it to start in-kernel DAD for it.
		 *
		 * The global address alone should work for most uses.  On
		 * the other hand since both the link-local address and the
		 * global address are based on the same MAC, there's some
		 * correlation between one failing DAD and the other
		 * failing DAD due to another host using the same address.
		 * As RFC4862 Section 5.4 notes we can't rely on that to
		 * skip DAD for one of the addresses.
		 */

		netconfig_add_slaac_address(nc, r);
		return;
	}

	/* Neither method seems available, fail */
	if (nc->v6_auto_method == NETCONFIG_V6_METHOD_UNSET) {
		netconfig_failed(nc, AF_INET6);
		return;
	}

	/* DHCP already started or waiting for the LL address, nothing to do */
	if (nc->v6_auto_method == NETCONFIG_V6_METHOD_DHCP)
		goto emit_event;

	/*
	 * Otherwise we already have a SLAAC address, just check if any of the
	 * auto-configuration prefixes in this RA covers our existing address
	 * and allows us to extend its lifetime.
	 */
	netconfig_set_slaac_address_lifetimes(nc, r);

emit_event:
	/*
	 * Note: we may be emitting this before L_NETCONFIG_EVENT_CONFIGURE.
	 * We should probably instead save the affected routes in separate
	 * lists and add them to the _CONFIGURE event, suppressing any _UPDATE
	 * events while nc->v6_configured is false.
	 */
	if (!l_queue_isempty(nc->routes.added) ||
			!l_queue_isempty(nc->routes.updated) ||
			!l_queue_isempty(nc->routes.removed) ||
			!l_queue_isempty(nc->routes.expired) ||
			!l_queue_isempty(nc->addresses.updated) ||
			dns_updated)
		netconfig_emit_event(nc, AF_INET6, L_NETCONFIG_EVENT_UPDATE);
}

static int netconfig_proc_write_ipv6_uint_setting(struct l_netconfig *nc,
							const char *setting,
							unsigned int value)
{
	char ifname[IF_NAMESIZE];

	if (unlikely(!if_indextoname(nc->ifindex, ifname)))
		return -errno;

	return l_sysctl_set_u32(value, "/proc/sys/net/ipv6/conf/%s/%s",
					ifname, setting);
}

static int netconfig_proc_read_ipv6_uint_setting(struct l_netconfig *nc,
							const char *setting,
							uint32_t *out_v)
{
	char ifname[IF_NAMESIZE];

	if (unlikely(!if_indextoname(nc->ifindex, ifname)))
		return -errno;

	return l_sysctl_get_u32(out_v, "/proc/sys/net/ipv6/conf/%s/%s",
					ifname, setting);
}

LIB_EXPORT struct l_netconfig *l_netconfig_new(uint32_t ifindex)
{
	struct l_netconfig *nc;

	nc = l_new(struct l_netconfig, 1);
	nc->ifindex = ifindex;

	nc->addresses.current = l_queue_new();
	nc->addresses.added = l_queue_new();
	nc->addresses.updated = l_queue_new();
	nc->addresses.removed = l_queue_new();
	nc->routes.current = l_queue_new();
	nc->routes.added = l_queue_new();
	nc->routes.updated = l_queue_new();
	nc->routes.removed = l_queue_new();
	nc->icmp_route_data = l_queue_new();

	nc->dhcp_client = l_dhcp_client_new(ifindex);
	l_dhcp_client_set_event_handler(nc->dhcp_client,
					netconfig_dhcp_event_handler,
					nc, NULL);

	nc->dhcp6_client = l_dhcp6_client_new(ifindex);
	l_dhcp6_client_set_nora(nc->dhcp6_client, true);
	l_dhcp6_client_set_event_handler(nc->dhcp6_client,
					netconfig_dhcp6_event_handler,
					nc, NULL);

	nc->icmp6_client = l_dhcp6_client_get_icmp6(nc->dhcp6_client);
	l_icmp6_client_add_event_handler(nc->icmp6_client,
					netconfig_icmp6_event_handler,
					nc, NULL);

	/* Disable in-kernel autoconfiguration for the interface */
	netconfig_proc_write_ipv6_uint_setting(nc, "accept_ra", 0);

	l_netconfig_reset_config(nc);
	return nc;
}

LIB_EXPORT void l_netconfig_destroy(struct l_netconfig *netconfig)
{
	if (unlikely(!netconfig))
		return;

	l_netconfig_stop(netconfig);

	l_netconfig_set_static_addr(netconfig, AF_INET, NULL);
	l_netconfig_set_gateway_override(netconfig, AF_INET, NULL);
	l_netconfig_set_dns_override(netconfig, AF_INET, NULL);
	l_netconfig_set_domain_names_override(netconfig, AF_INET, NULL);
	l_netconfig_set_static_addr(netconfig, AF_INET6, NULL);
	l_netconfig_set_gateway_override(netconfig, AF_INET6, NULL);
	l_netconfig_set_dns_override(netconfig, AF_INET6, NULL);
	l_netconfig_set_domain_names_override(netconfig, AF_INET6, NULL);

	l_dhcp_client_destroy(netconfig->dhcp_client);
	l_dhcp6_client_destroy(netconfig->dhcp6_client);
	l_netconfig_set_event_handler(netconfig, NULL, NULL, NULL);
	l_queue_destroy(netconfig->addresses.current, NULL);
	l_queue_destroy(netconfig->addresses.added, NULL);
	l_queue_destroy(netconfig->addresses.updated, NULL);
	l_queue_destroy(netconfig->addresses.removed, NULL);
	l_queue_destroy(netconfig->routes.current, NULL);
	l_queue_destroy(netconfig->routes.added, NULL);
	l_queue_destroy(netconfig->routes.updated, NULL);
	l_queue_destroy(netconfig->routes.removed, NULL);
	l_queue_destroy(netconfig->icmp_route_data, NULL);
	l_queue_destroy(netconfig->slaac_domains, NULL);
	l_queue_destroy(netconfig->slaac_dnses, NULL);
	l_free(netconfig);
}

/*
 * The following l_netconfig_set_* functions configure the l_netconfig's
 * client settings.  The setters can be called independently, without
 * following a specific order.  Most of the setters will not validate the
 * values passed, l_netconfig_start() will fail if settings are incorrect
 * or inconsistent between themselves, e.g. if the static local IP and
 * gateway IP are not in the same subnet.  Alternatively
 * l_netconfig_check_config() can be called at any point to validate the
 * current configuration.  The configuration can only be changed while
 * the l_netconfig state machine is stopped, i.e. before
 * l_netconfig_start() and after l_netconfig_stop().
 *
 * l_netconfig_set_hostname, l_netconfig_set_static_addr,
 * l_netconfig_set_gateway_override, l_netconfig_set_dns_override and
 * l_netconfig_set_domain_names_override can be passed NULL to unset a
 * value that had been set before (revert to auto).  This is why the
 * family parameter is needed even when it could otherwise be derived
 * from the new value that is passed.
 */
LIB_EXPORT bool l_netconfig_set_family_enabled(struct l_netconfig *netconfig,
						uint8_t family, bool enabled)
{
	if (unlikely(!netconfig || netconfig->started))
		return false;

	switch (family) {
	case AF_INET:
		netconfig->v4_enabled = enabled;
		return true;
	case AF_INET6:
		netconfig->v6_enabled = enabled;
		return true;
	}

	return false;
}

LIB_EXPORT bool l_netconfig_set_hostname(struct l_netconfig *netconfig,
						const char *hostname)
{
	if (unlikely(!netconfig || netconfig->started))
		return false;

	return l_dhcp_client_set_hostname(netconfig->dhcp_client, hostname);
}

LIB_EXPORT bool l_netconfig_set_route_priority(struct l_netconfig *netconfig,
						uint32_t priority)
{
	if (unlikely(!netconfig || netconfig->started))
		return false;

	netconfig->route_priority = priority;
	return true;
}

LIB_EXPORT bool l_netconfig_set_static_addr(struct l_netconfig *netconfig,
					uint8_t family,
					const struct l_rtnl_address *addr)
{
	struct l_rtnl_address **ptr;

	if (unlikely(!netconfig || netconfig->started))
		return false;

	if (addr && l_rtnl_address_get_family(addr) != family)
		return false;

	switch (family) {
	case AF_INET:
		ptr = &netconfig->v4_static_addr;
		break;
	case AF_INET6:
		ptr = &netconfig->v6_static_addr;
		break;
	default:
		return false;
	}

	l_rtnl_address_free(*ptr);
	*ptr = NULL;

	if (!addr)
		return true;

	*ptr = l_rtnl_address_clone(addr);
	l_rtnl_address_set_lifetimes(*ptr, 0, 0);
	l_rtnl_address_set_noprefixroute(*ptr, true);
	return true;
}

LIB_EXPORT bool l_netconfig_set_gateway_override(struct l_netconfig *netconfig,
							uint8_t family,
							const char *gateway_str)
{
	char **ptr;

	if (unlikely(!netconfig || netconfig->started))
		return false;

	switch (family) {
	case AF_INET:
		ptr = &netconfig->v4_gateway_override;
		break;
	case AF_INET6:
		ptr = &netconfig->v6_gateway_override;
		break;
	default:
		return false;
	}

	l_free(*ptr);
	*ptr = NULL;

	if (!gateway_str)
		return true;

	*ptr = l_strdup(gateway_str);
	return true;
}

LIB_EXPORT bool l_netconfig_set_dns_override(struct l_netconfig *netconfig,
						uint8_t family, char **dns_list)
{
	char ***ptr;

	if (unlikely(!netconfig || netconfig->started))
		return false;

	switch (family) {
	case AF_INET:
		ptr = &netconfig->v4_dns_override;
		break;
	case AF_INET6:
		ptr = &netconfig->v6_dns_override;
		break;
	default:
		return false;
	}

	l_strv_free(*ptr);
	*ptr = NULL;

	if (!dns_list)
		return true;

	*ptr = l_strv_copy(dns_list);
	return true;
}

LIB_EXPORT bool l_netconfig_set_domain_names_override(
						struct l_netconfig *netconfig,
						uint8_t family, char **names)
{
	char ***ptr;

	if (unlikely(!netconfig || netconfig->started))
		return false;

	switch (family) {
	case AF_INET:
		ptr = &netconfig->v4_domain_names_override;
		break;
	case AF_INET6:
		ptr = &netconfig->v6_domain_names_override;
		break;
	default:
		return false;
	}

	l_strv_free(*ptr);
	*ptr = NULL;

	if (!names)
		return true;

	*ptr = l_strv_copy(names);
	return true;
}

LIB_EXPORT bool l_netconfig_set_acd_enabled(struct l_netconfig *netconfig,
						bool enabled)
{
	if (unlikely(!netconfig || netconfig->started))
		return false;

	netconfig->acd_enabled = enabled;
	return true;
}

LIB_EXPORT bool l_netconfig_set_optimistic_dad_enabled(
						struct l_netconfig *netconfig,
						bool enabled)
{
	if (unlikely(!netconfig || netconfig->started))
		return false;

	netconfig->optimistic_dad_enabled = enabled;
	return true;
}

static bool netconfig_check_family_config(struct l_netconfig *nc,
						uint8_t family)
{
	struct l_rtnl_address *static_addr = (family == AF_INET) ?
		nc->v4_static_addr : nc->v6_static_addr;
	char *gateway_override = (family == AF_INET) ?
		nc->v4_gateway_override : nc->v6_gateway_override;
	char **dns_override = (family == AF_INET) ?
		nc->v4_dns_override : nc->v6_dns_override;
	unsigned int dns_num = 0;

	if (static_addr && family == AF_INET) {
		uint8_t prefix_len =
			l_rtnl_address_get_prefix_length(static_addr);

		if (prefix_len > 30)
			return false;
	}

	if (gateway_override) {
		union netconfig_addr gateway;

		if (inet_pton(family, gateway_override, &gateway) != 1)
			return false;
	}

	if (dns_override && (dns_num = l_strv_length(dns_override))) {
		unsigned int i;
		_auto_(l_free) union netconfig_addr *dns_list =
			l_new(union netconfig_addr, dns_num);

		for (i = 0; i < dns_num; i++)
			if (inet_pton(family, dns_override[i],
					&dns_list[i]) != 1)
				return false;
	}

	return true;
}

static bool netconfig_check_config(struct l_netconfig *nc)
{
	/* TODO: error reporting through a debug log handler or otherwise */

	return netconfig_check_family_config(nc, AF_INET) &&
		netconfig_check_family_config(nc, AF_INET6);
}

LIB_EXPORT bool l_netconfig_check_config(struct l_netconfig *netconfig)
{
	if (unlikely(!netconfig || netconfig->started))
		return false;

	return netconfig_check_config(netconfig);
}

LIB_EXPORT bool l_netconfig_reset_config(struct l_netconfig *netconfig)
{
	if (unlikely(!netconfig || netconfig->started))
		return false;

	l_netconfig_set_hostname(netconfig, NULL);
	l_netconfig_set_route_priority(netconfig, 0);
	l_netconfig_set_family_enabled(netconfig, AF_INET, true);
	l_netconfig_set_static_addr(netconfig, AF_INET, NULL);
	l_netconfig_set_gateway_override(netconfig, AF_INET, NULL);
	l_netconfig_set_dns_override(netconfig, AF_INET, NULL);
	l_netconfig_set_domain_names_override(netconfig, AF_INET, NULL);
	l_netconfig_set_acd_enabled(netconfig, true);
	l_netconfig_set_family_enabled(netconfig, AF_INET6, false);
	l_netconfig_set_static_addr(netconfig, AF_INET6, NULL);
	l_netconfig_set_gateway_override(netconfig, AF_INET6, NULL);
	l_netconfig_set_dns_override(netconfig, AF_INET6, NULL);
	l_netconfig_set_domain_names_override(netconfig, AF_INET6, NULL);
	return true;
}

static void netconfig_add_v4_static_address_routes(struct l_netconfig *nc)
{
	char ip[INET_ADDRSTRLEN];
	uint32_t prefix_len;

	nc->v4_address = l_rtnl_address_clone(nc->v4_static_addr);
	l_queue_push_tail(nc->addresses.current, nc->v4_address);
	l_queue_push_tail(nc->addresses.added, nc->v4_address);

	l_rtnl_address_get_address(nc->v4_static_addr, ip);
	prefix_len = l_rtnl_address_get_prefix_length(nc->v4_static_addr);
	netconfig_add_v4_routes(nc, ip, prefix_len, NULL, RTPROT_STATIC);
}

/*
 * Just mirror the IPv4 behaviour with static IPv6 configuration.  It would
 * be more logical to let the user choose between static IPv6 address and
 * DHCPv6, and, completely independently, choose between static routes
 * (if a static prefix length and/or gateway address is set) and ICMPv6.
 * Yet a mechanism identical with IPv4 is easier to understand for a typical
 * user so providing a static address just disables all automatic
 * configuration.
 */
static void netconfig_add_v6_static_address_routes(struct l_netconfig *nc)
{
	char ip[INET6_ADDRSTRLEN];
	uint32_t prefix_len;

	nc->v6_address = l_rtnl_address_clone(nc->v6_static_addr);
	l_queue_push_tail(nc->addresses.current, nc->v6_address);
	l_queue_push_tail(nc->addresses.added, nc->v6_address);

	l_rtnl_address_get_address(nc->v6_static_addr, ip);
	prefix_len = l_rtnl_address_get_prefix_length(nc->v6_static_addr);
	netconfig_add_v6_static_routes(nc, ip, prefix_len);
}

static void netconfig_ipv4_acd_event(enum l_acd_event event, void *user_data)
{
	struct l_netconfig *nc = user_data;

	switch (event) {
	case L_ACD_EVENT_AVAILABLE:
		if (L_WARN_ON(nc->v4_configured))
			break;

		netconfig_add_v4_static_address_routes(nc);
		nc->v4_configured = true;
		netconfig_emit_event(nc, AF_INET, L_NETCONFIG_EVENT_CONFIGURE);
		break;
	case L_ACD_EVENT_CONFLICT:
		if (L_WARN_ON(nc->v4_configured))
			break;

		/*
		 * Conflict found, no IP was actually set or routes added so
		 * just emit the event.
		 */
		netconfig_failed(nc, AF_INET);
		break;
	case L_ACD_EVENT_LOST:
		if (L_WARN_ON(!nc->v4_configured))
			break;

		/*
		 * Set IP but lost it some time later.  Reset IPv4 in this
		 * case and emit the FAILED event since we have no way to
		 * recover from here.
		 */
		netconfig_remove_v4_address_routes(nc, false);
		nc->v4_configured = false;
		netconfig_failed(nc, AF_INET);
		break;
	}
}

static void netconfig_do_static_config(struct l_idle *idle, void *user_data)
{
	struct l_netconfig *nc = user_data;

	l_idle_remove(l_steal_ptr(nc->do_static_work));

	if (nc->v4_static_addr && !nc->v4_configured) {
		if (nc->acd_enabled) {
			char ip[INET_ADDRSTRLEN];

			l_rtnl_address_get_address(nc->v4_static_addr, ip);

			nc->acd = l_acd_new(nc->ifindex);
			l_acd_set_event_handler(nc->acd,
						netconfig_ipv4_acd_event, nc,
						NULL);

			if (l_acd_start(nc->acd, ip))
				goto configure_ipv6;

			l_acd_destroy(l_steal_ptr(nc->acd));
			/* Configure right now as a fallback */
		}

		netconfig_add_v4_static_address_routes(nc);
		nc->v4_configured = true;
		netconfig_emit_event(nc, AF_INET, L_NETCONFIG_EVENT_CONFIGURE);
	}

configure_ipv6:
	if (nc->v6_static_addr && !nc->v6_configured) {
		netconfig_add_v6_static_address_routes(nc);
		nc->v6_configured = true;
		netconfig_emit_event(nc, AF_INET6, L_NETCONFIG_EVENT_CONFIGURE);
	}
}

static void netconfig_rtnl_unregister(void *user_data)
{
	struct l_netlink *rtnl = user_data;

	if (!addr_wait_list || !l_queue_isempty(addr_wait_list))
		return;

	l_queue_destroy(l_steal_ptr(addr_wait_list), NULL);
	l_netlink_unregister(rtnl, rtnl_id);
	rtnl_id = 0;
}

static void netconfig_addr_wait_unregister(struct l_netconfig *nc,
						bool in_notify)
{
	struct l_netlink *rtnl = l_rtnl_get();

	if (nc->ifaddr6_dump_cmd_id) {
		unsigned int cmd_id = nc->ifaddr6_dump_cmd_id;

		nc->ifaddr6_dump_cmd_id = 0;
		l_netlink_cancel(rtnl, cmd_id);
	}

	if (!l_queue_remove(addr_wait_list, nc))
		return;

	if (!l_queue_isempty(addr_wait_list))
		return;

	/* We can't do l_netlink_unregister() inside a notification */
	if (in_notify)
		l_idle_oneshot(netconfig_rtnl_unregister, rtnl, NULL);
	else
		netconfig_rtnl_unregister(rtnl);
}

static void netconfig_ifaddr_ipv6_added(struct l_netconfig *nc,
					const struct ifaddrmsg *ifa,
					uint32_t len)
{
	struct in6_addr in6;
	_auto_(l_free) char *ip = NULL;
	bool new_lla;

	if ((ifa->ifa_flags & IFA_F_TENTATIVE) &&
			!(ifa->ifa_flags & IFA_F_OPTIMISTIC))
		return;

	if (!nc->started)
		return;

	l_rtnl_ifaddr6_extract(ifa, len, &ip);
	inet_pton(AF_INET6, ip, &in6);

	if (!IN6_IS_ADDR_LINKLOCAL(&in6))
		return;

	new_lla = !nc->have_lla;
	nc->have_lla = true;

	if (!(ifa->ifa_flags & IFA_F_TENTATIVE))
		netconfig_addr_wait_unregister(nc, true);
	else if (nc->ifaddr6_dump_cmd_id) {
		struct l_netlink *rtnl = l_rtnl_get();
		unsigned int cmd_id = nc->ifaddr6_dump_cmd_id;

		nc->ifaddr6_dump_cmd_id = 0;
		l_netlink_cancel(rtnl, cmd_id);
	}

	l_dhcp6_client_set_link_local_address(nc->dhcp6_client, ip);
	l_icmp6_client_set_link_local_address(nc->icmp6_client, ip,
					!!(ifa->ifa_flags & IFA_F_OPTIMISTIC));

	/*
	 * Only now that we have a link-local address see if we can start
	 * actual DHCPv6 setup.
	 */
	if (new_lla && !netconfig_check_start_dhcp6(nc))
		netconfig_failed(nc, AF_INET6);
}

static void netconfig_ifaddr_ipv6_notify(uint16_t type, const void *data,
						uint32_t len, void *user_data)
{
	const struct ifaddrmsg *ifa = data;
	uint32_t bytes = len - NLMSG_ALIGN(sizeof(struct ifaddrmsg));
	const struct l_queue_entry *entry, *next;

	switch (type) {
	case RTM_NEWADDR:
		/* Iterate safely since elements may be removed */
		for (entry = l_queue_get_entries(addr_wait_list); entry;
				entry = next) {
			struct l_netconfig *nc = entry->data;

			next = entry->next;

			if (ifa->ifa_index == nc->ifindex)
				netconfig_ifaddr_ipv6_added(nc, ifa, bytes);
		}

		break;
	}
}

static void netconfig_ifaddr_ipv6_dump_cb(int error, uint16_t type,
						const void *data, uint32_t len,
						void *user_data)
{
	struct l_netconfig *nc = user_data;

	if (!nc->ifaddr6_dump_cmd_id || !nc->started)
		return;

	if (error) {
		netconfig_failed(nc, AF_INET6);
		return;
	}

	if (type != RTM_NEWADDR)
		return;

	netconfig_ifaddr_ipv6_notify(type, data, len, user_data);
}

static void netconfig_ifaddr_ipv6_dump_done_cb(void *user_data)
{
	struct l_netconfig *nc = user_data;
	int r;

	/*
	 * Handle the case of no link-local address having been found during
	 * the dump.  If nc->ifaddr6_dump_cmd_id is 0, we have found one or
	 * the dump is being cancelled.  Otherwise try disabling the
	 * "disable_ipv6" setting for the interface since it may have been
	 * enabled.  Also write "addr_gen_mode" which triggers regerating
	 * the link-local address on the interface in the kernel if it
	 * was previously removed.
	 */
	if (!nc->ifaddr6_dump_cmd_id || !nc->started)
		return;

	nc->ifaddr6_dump_cmd_id = 0;

	/* "do not generate a link-local address" */
	netconfig_proc_write_ipv6_uint_setting(nc, "addr_gen_mode", 1);
	/* "generate address based on EUI64 (default)" */
	netconfig_proc_write_ipv6_uint_setting(nc, "addr_gen_mode", 0);

	/* "enable IPv6 operation" */
	r = netconfig_proc_read_ipv6_uint_setting(nc, "disable_ipv6",
						&nc->orig_disable_ipv6);
	if (r < 0) /* TODO: Log error? */
		nc->orig_disable_ipv6 = 0;

	if (nc->orig_disable_ipv6)
		netconfig_proc_write_ipv6_uint_setting(nc, "disable_ipv6", 0);
}

LIB_EXPORT bool l_netconfig_start(struct l_netconfig *netconfig)
{
	int r;
	bool optimistic_dad;

	if (unlikely(!netconfig || netconfig->started))
		return false;

	if (!netconfig_check_config(netconfig))
		return false;

	if (!l_net_get_mac_address(netconfig->ifindex, netconfig->mac))
		return false;

	if (!netconfig->v4_enabled)
		goto configure_ipv6;

	if (netconfig->v4_static_addr) {
		/*
		 * We're basically ready to configure the interface
		 * but do this in an idle callback.
		 */
		netconfig->do_static_work = l_idle_create(
						netconfig_do_static_config,
						netconfig, NULL);
		goto configure_ipv6;
	}

	l_dhcp_client_set_address(netconfig->dhcp_client, ARPHRD_ETHER,
					netconfig->mac, ETH_ALEN);

	if (!l_dhcp_client_start(netconfig->dhcp_client))
		return false;

configure_ipv6:
	if (!netconfig->v6_enabled)
		goto done;

	/*
	 * Enable optimistic DAD if the user has requested it *and* it is
	 * recommended by RFC 4429 Section 3.1 for the address generation
	 * method in use:
	 *   * mac-based Interface ID such as EUI-64
	 *   * random
	 *   * well-distributed hash function
	 *   * DHCPv6
	 * i.e. all autoconfiguration methods.  In any other case disable
	 * it.
	 */
	optimistic_dad = netconfig->optimistic_dad_enabled &&
		!netconfig->v6_static_addr;

	r = netconfig_proc_read_ipv6_uint_setting(netconfig, "optimistic_dad",
				&netconfig->orig_optimistic_dad);
	if (r < 0) /* TODO: Log error? */
		netconfig->orig_optimistic_dad = optimistic_dad;

	if (!r && !!netconfig->orig_optimistic_dad != optimistic_dad)
		netconfig_proc_write_ipv6_uint_setting(netconfig,
							"optimistic_dad",
							optimistic_dad ? 1 : 0);

	if (netconfig->v6_static_addr) {
		/*
		 * We're basically ready to configure the interface
		 * but do this in an idle callback.
		 */
		if (!netconfig->do_static_work)
			netconfig->do_static_work = l_idle_create(
						netconfig_do_static_config,
						netconfig, NULL);

		goto done;
	}

	netconfig->v6_auto_method = NETCONFIG_V6_METHOD_UNSET;

	/*
	 * We only care about being on addr_wait_list if we're waiting for
	 * the link-local address for DHCP6.  Add ourself to the list here
	 * before we start the dump, instead of after it ends, to eliminate
	 * the possibility of missing an RTM_NEWADDR between the end of
	 * the dump command and registering for the events.
	 *
	 * We stay on that list until we receive a non-tentative LL address.
	 * Note that we may set .have_lla earlier, specifically when we
	 * receive a tentative LL address that is also optimistic.  We will
	 * however stay on addr_wait_list because we want to notify
	 * l_icmp6_client again when the LL address completes DAD and becomes
	 * non-tentative.
	 */
	if (!addr_wait_list) {
		addr_wait_list = l_queue_new();

		rtnl_id = l_netlink_register(l_rtnl_get(), RTNLGRP_IPV6_IFADDR,
						netconfig_ifaddr_ipv6_notify,
						netconfig, NULL);
		if (!rtnl_id)
			goto unregister;
	}

	netconfig->ifaddr6_dump_cmd_id = l_rtnl_ifaddr6_dump(l_rtnl_get(),
					netconfig_ifaddr_ipv6_dump_cb,
					netconfig,
					netconfig_ifaddr_ipv6_dump_done_cb);
	if (!netconfig->ifaddr6_dump_cmd_id)
		goto unregister;

	l_queue_push_tail(addr_wait_list, netconfig);
	netconfig->have_lla = false;

	l_dhcp6_client_set_address(netconfig->dhcp6_client, ARPHRD_ETHER,
					netconfig->mac, ETH_ALEN);
	l_icmp6_client_set_address(netconfig->icmp6_client, netconfig->mac);

	/*
	 * RFC4862 Section 4: "To speed the autoconfiguration process, a host
	 * may generate its link-local address (and verify its uniqueness) in
	 * parallel with waiting for a Router Advertisement.  Because a router
	 * may delay responding to a Router Solicitation for a few seconds,
	 * the total time needed to complete autoconfiguration can be
	 * significantly longer if the two steps are done serially."
	 *
	 * We don't know whether we have the LL address yet.  The interface
	 * may have been just brought up and DAD may still running or the LL
	 * address may have been deleted and won't be added until
	 * netconfig_ifaddr_ipv6_dump_done_cb() writes the /proc settings.
	 * In any case the Router Solicitation doesn't depend on having the
	 * LL address so send it now.  We won't start DHCPv6 however until we
	 * have both the LL address and the Router Advertisement.
	 */
	if (!l_icmp6_client_start(netconfig->icmp6_client))
		goto unregister;

	netconfig->ra_timeout = l_timeout_create(10, netconfig_ra_timeout_cb,
							netconfig, NULL);

done:
	netconfig->started = true;
	return true;

unregister:
	netconfig_addr_wait_unregister(netconfig, false);

	if (netconfig->v4_enabled) {
		if (netconfig->v4_static_addr)
			l_idle_remove(l_steal_ptr(netconfig->do_static_work));
		else
			l_dhcp_client_stop(netconfig->dhcp_client);
	}

	return false;
}

LIB_EXPORT void l_netconfig_stop(struct l_netconfig *netconfig)
{
	bool optimistic_dad;

	if (unlikely(!netconfig || !netconfig->started))
		return;

	netconfig->started = false;

	if (netconfig->do_static_work)
		l_idle_remove(l_steal_ptr(netconfig->do_static_work));

	if (netconfig->signal_expired_work)
		l_idle_remove(l_steal_ptr(netconfig->signal_expired_work));

	if (netconfig->ra_timeout)
		l_timeout_remove(l_steal_ptr(netconfig->ra_timeout));

	netconfig_addr_wait_unregister(netconfig, false);

	netconfig_update_cleanup(netconfig);
	l_queue_clear(netconfig->addresses.current,
			(l_queue_destroy_func_t) l_rtnl_address_free);
	l_queue_clear(netconfig->routes.current,
			(l_queue_destroy_func_t) l_rtnl_route_free);
	l_queue_clear(netconfig->icmp_route_data, l_free);
	l_queue_clear(netconfig->slaac_dnses, l_free);
	l_queue_clear(netconfig->slaac_domains, l_free);
	netconfig->v4_address = NULL;
	netconfig->v4_subnet_route = NULL;
	netconfig->v4_default_route = NULL;
	netconfig->v6_address = NULL;
	netconfig->v4_configured = false;
	netconfig->v6_configured = false;

	l_dhcp_client_stop(netconfig->dhcp_client);
	l_dhcp6_client_stop(netconfig->dhcp6_client);
	l_icmp6_client_stop(netconfig->icmp6_client);

	l_acd_destroy(l_steal_ptr(netconfig->acd));

	if (netconfig->orig_disable_ipv6) {
		netconfig_proc_write_ipv6_uint_setting(netconfig,
						"disable_ipv6",
						netconfig->orig_disable_ipv6);
		netconfig->orig_disable_ipv6 = 0;
	}

	optimistic_dad = netconfig->optimistic_dad_enabled &&
		!netconfig->v6_static_addr;
	if (!!netconfig->orig_optimistic_dad != optimistic_dad)
		netconfig_proc_write_ipv6_uint_setting(netconfig,
						"optimistic_dad",
						netconfig->orig_optimistic_dad);
}

/*
 * Undo any configuration already applied to the interface by previous
 * calls to the event handler, by synchronously emitting
 * L_NETCONFIG_EVENT_UNCONFIGURE events.  This can be called before
 * l_netconfig_stop() which won't emit any events.  It mainly makes
 * sense if the interface isn't being removed or brought DOWN, which
 * would otherwise implicitly remove routes and addresses.
 */
LIB_EXPORT void l_netconfig_unconfigure(struct l_netconfig *netconfig)
{
	const struct l_queue_entry *entry;

	if (netconfig->v4_configured) {
		netconfig_remove_v4_address_routes(netconfig, false);
		netconfig->v4_configured = false;

		netconfig_emit_event(netconfig, AF_INET,
					L_NETCONFIG_EVENT_UNCONFIGURE);
	}

	if (netconfig->v6_address) {
		netconfig_remove_dhcp6_address(netconfig, false);
		netconfig->v6_configured = false;
	}

	/* Bulk remove any other routes or addresses */
	for (entry = l_queue_get_entries(netconfig->addresses.current); entry;
			entry = entry->next)
		l_queue_push_tail(netconfig->addresses.removed, entry->data);

	l_queue_clear(netconfig->addresses.added, NULL);
	l_queue_clear(netconfig->addresses.updated, NULL);
	l_queue_clear(netconfig->addresses.current, NULL);

	for (entry = l_queue_get_entries(netconfig->routes.current); entry;
			entry = entry->next)
		l_queue_push_tail(netconfig->routes.removed, entry->data);

	l_queue_clear(netconfig->routes.added, NULL);
	l_queue_clear(netconfig->routes.updated, NULL);
	l_queue_clear(netconfig->routes.current, NULL);
	l_queue_clear(netconfig->icmp_route_data, l_free);

	if (!l_queue_isempty(netconfig->addresses.removed) ||
			!l_queue_isempty(netconfig->routes.removed))
		netconfig_emit_event(netconfig, AF_INET6,
					L_NETCONFIG_EVENT_UNCONFIGURE);
}

LIB_EXPORT struct l_dhcp_client *l_netconfig_get_dhcp_client(
						struct l_netconfig *netconfig)
{
	if (unlikely(!netconfig))
		return NULL;

	return netconfig->dhcp_client;
}

LIB_EXPORT struct l_dhcp6_client *l_netconfig_get_dhcp6_client(
						struct l_netconfig *netconfig)
{
	if (unlikely(!netconfig))
		return NULL;

	return netconfig->dhcp6_client;
}

LIB_EXPORT struct l_icmp6_client *l_netconfig_get_icmp6_client(
						struct l_netconfig *netconfig)
{
	if (unlikely(!netconfig))
		return NULL;

	return netconfig->icmp6_client;
}

LIB_EXPORT void l_netconfig_set_event_handler(struct l_netconfig *netconfig,
					l_netconfig_event_cb_t handler,
					void *user_data,
					l_netconfig_destroy_cb_t destroy)
{
	if (unlikely(!netconfig))
		return;

	if (netconfig->handler.destroy)
		netconfig->handler.destroy(netconfig->handler.user_data);

	netconfig->handler.callback = handler;
	netconfig->handler.user_data = user_data;
	netconfig->handler.destroy = destroy;
}

LIB_EXPORT void l_netconfig_apply_rtnl(struct l_netconfig *netconfig)
{
	const struct l_queue_entry *entry;

	for (entry = l_queue_get_entries(netconfig->addresses.removed); entry;
			entry = entry->next)
		l_rtnl_ifaddr_delete(l_rtnl_get(), netconfig->ifindex,
					entry->data, NULL, NULL, NULL);

	for (entry = l_queue_get_entries(netconfig->addresses.added); entry;
			entry = entry->next)
		l_rtnl_ifaddr_add(l_rtnl_get(), netconfig->ifindex,
					entry->data, NULL, NULL, NULL);

	/* We can use l_rtnl_ifaddr_add here since that uses NLM_F_REPLACE */
	for (entry = l_queue_get_entries(netconfig->addresses.updated); entry;
			entry = entry->next)
		l_rtnl_ifaddr_add(l_rtnl_get(), netconfig->ifindex,
					entry->data, NULL, NULL, NULL);

	for (entry = l_queue_get_entries(netconfig->routes.removed); entry;
			entry = entry->next)
		l_rtnl_route_delete(l_rtnl_get(), netconfig->ifindex,
					entry->data, NULL, NULL, NULL);

	for (entry = l_queue_get_entries(netconfig->routes.added); entry;
			entry = entry->next)
		l_rtnl_route_add(l_rtnl_get(), netconfig->ifindex,
					entry->data, NULL, NULL, NULL);

	/* We can use l_rtnl_route_add here since that uses NLM_F_REPLACE */
	for (entry = l_queue_get_entries(netconfig->routes.updated); entry;
			entry = entry->next)
		l_rtnl_route_add(l_rtnl_get(), netconfig->ifindex,
					entry->data, NULL, NULL, NULL);
}

LIB_EXPORT const struct l_queue_entry *l_netconfig_get_addresses(
					struct l_netconfig *netconfig,
					const struct l_queue_entry **out_added,
					const struct l_queue_entry **out_updated,
					const struct l_queue_entry **out_removed,
					const struct l_queue_entry **out_expired)
{
	if (out_added)
		*out_added = l_queue_get_entries(netconfig->addresses.added);

	if (out_updated)
		*out_updated = l_queue_get_entries(netconfig->addresses.updated);

	if (out_removed)
		*out_removed = l_queue_get_entries(netconfig->addresses.removed);

	if (out_expired)
		*out_expired = l_queue_get_entries(netconfig->addresses.expired);

	return l_queue_get_entries(netconfig->addresses.current);
}

LIB_EXPORT const struct l_queue_entry *l_netconfig_get_routes(
					struct l_netconfig *netconfig,
					const struct l_queue_entry **out_added,
					const struct l_queue_entry **out_updated,
					const struct l_queue_entry **out_removed,
					const struct l_queue_entry **out_expired)
{
	netconfig_expire_routes(netconfig);

	if (out_added)
		*out_added = l_queue_get_entries(netconfig->routes.added);

	if (out_updated)
		*out_updated = l_queue_get_entries(netconfig->routes.updated);

	if (out_removed)
		*out_removed = l_queue_get_entries(netconfig->routes.removed);

	if (out_expired)
		*out_expired = l_queue_get_entries(netconfig->routes.expired);

	return l_queue_get_entries(netconfig->routes.current);
}

static void netconfig_strv_cat(char ***dest, char **src, bool free)
{
	unsigned int dest_len;
	unsigned int src_len;

	if (!src)
		return;

	if (!free)
		src = l_strv_copy(src);

	if (!*dest) {
		*dest = src;
		return;
	}

	dest_len = l_strv_length(*dest);
	src_len = l_strv_length(src);
	*dest = l_realloc(*dest, sizeof(char *) * (dest_len + src_len + 1));
	memcpy(*dest + dest_len, src, sizeof(char *) * (src_len + 1));
	l_free(src);
}

/* Returns a new strv array to be freed by the caller */
LIB_EXPORT char **l_netconfig_get_dns_list(struct l_netconfig *netconfig)
{
	char **ret = NULL;
	const struct l_dhcp_lease *v4_lease;
	const struct l_dhcp6_lease *v6_lease;

	if (!netconfig->v4_configured)
		goto append_v6;

	if (netconfig->v4_dns_override)
		netconfig_strv_cat(&ret, netconfig->v4_dns_override, false);
	else if ((v4_lease =
			l_dhcp_client_get_lease(netconfig->dhcp_client)))
		netconfig_strv_cat(&ret, l_dhcp_lease_get_dns(v4_lease), true);

append_v6:
	if (!netconfig->v6_configured)
		goto done;

	if (netconfig->v6_dns_override) {
		netconfig_strv_cat(&ret, netconfig->v6_dns_override, false);
		goto done;
	}

	if (L_IN_SET(netconfig->v6_auto_method, NETCONFIG_V6_METHOD_DHCP,
				NETCONFIG_V6_METHOD_SLAAC_DHCP) &&
			(v6_lease = l_dhcp6_client_get_lease(
						netconfig->dhcp6_client)))
		netconfig_strv_cat(&ret, l_dhcp6_lease_get_dns(v6_lease), true);

	if (!l_queue_isempty(netconfig->slaac_dnses)) {
		unsigned int dest_len = l_strv_length(ret);
		unsigned int src_len = l_queue_length(netconfig->slaac_dnses);
		char **i;
		const struct l_queue_entry *entry;

		ret = l_realloc(ret, sizeof(char *) * (dest_len + src_len + 1));
		i = ret + dest_len;

		for (entry = l_queue_get_entries(netconfig->slaac_dnses);
				entry; entry = entry->next) {
			char addr_str[INET6_ADDRSTRLEN];

			if (inet_ntop(AF_INET6, entry->data, addr_str,
					sizeof(addr_str)))
				*i++ = l_strdup(addr_str);
		}

		*i = NULL;
	}

done:
	return ret;
}

/* Returns a new strv array to be freed by the caller */
LIB_EXPORT char **l_netconfig_get_domain_names(struct l_netconfig *netconfig)
{
	char **ret = NULL;
	const struct l_dhcp_lease *v4_lease;
	const struct l_dhcp6_lease *v6_lease;
	char *dn;

	if (!netconfig->v4_configured)
		goto append_v6;

	if (netconfig->v4_domain_names_override)
		netconfig_strv_cat(&ret, netconfig->v4_domain_names_override,
					false);
	else if ((v4_lease =
			l_dhcp_client_get_lease(netconfig->dhcp_client)) &&
			(dn = l_dhcp_lease_get_domain_name(v4_lease))) {
		ret = l_new(char *, 2);
		ret[0] = dn;
	}

append_v6:
	if (!netconfig->v6_configured)
		goto done;

	if (netconfig->v6_domain_names_override) {
		netconfig_strv_cat(&ret, netconfig->v6_domain_names_override,
					false);
		goto done;
	}

	if (L_IN_SET(netconfig->v6_auto_method, NETCONFIG_V6_METHOD_DHCP,
				NETCONFIG_V6_METHOD_SLAAC_DHCP) &&
			(v6_lease = l_dhcp6_client_get_lease(
						netconfig->dhcp6_client)))
		netconfig_strv_cat(&ret, l_dhcp6_lease_get_domains(v6_lease),
					true);

	if (!l_queue_isempty(netconfig->slaac_domains)) {
		unsigned int dest_len = l_strv_length(ret);
		unsigned int src_len = l_queue_length(netconfig->slaac_domains);
		char **i;
		const struct l_queue_entry *entry;

		ret = l_realloc(ret, sizeof(char *) * (dest_len + src_len + 1));
		i = ret + dest_len;

		for (entry = l_queue_get_entries(netconfig->slaac_domains);
				entry; entry = entry->next)
			*i++ = l_strdup(entry->data);

		*i = NULL;
	}

done:
	return ret;
}