summaryrefslogtreecommitdiff
path: root/src/platform/nm-linux-platform.c
blob: 9f3da20068c1b006d4b2bf9c836ffb49f5a0432c (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
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
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */
/* nm-linux-platform.c - Linux kernel & udev network configuration layer
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2, or (at your option)
 * any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Copyright (C) 2012-2013 Red Hat, Inc.
 */
#include <config.h>

#include <errno.h>
#include <unistd.h>
#include <sys/socket.h>
#include <fcntl.h>
#include <netinet/icmp6.h>
#include <netinet/in.h>
#include <linux/ip.h>
#include <linux/if_arp.h>
#include <linux/if_link.h>
#include <linux/if_tun.h>
#include <linux/if_tunnel.h>
#include <sys/ioctl.h>
#include <linux/sockios.h>
#include <linux/ethtool.h>
#include <linux/mii.h>
#include <netlink/netlink.h>
#include <netlink/object.h>
#include <netlink/cache.h>
#include <netlink/route/link.h>
#include <netlink/route/link/vlan.h>
#include <netlink/route/addr.h>
#include <netlink/route/route.h>
#include <gudev/gudev.h>

#include "NetworkManagerUtils.h"
#include "nm-linux-platform.h"
#include "NetworkManagerUtils.h"
#include "nm-utils.h"
#include "nm-logging.h"
#include "wifi/wifi-utils.h"

/* This is only included for the translation of VLAN flags */
#include "nm-setting-vlan.h"

#define debug(...) nm_log_dbg (LOGD_PLATFORM, __VA_ARGS__)
#define warning(...) nm_log_warn (LOGD_PLATFORM, __VA_ARGS__)
#define error(...) nm_log_err (LOGD_PLATFORM, __VA_ARGS__)

typedef struct {
	struct nl_sock *nlh;
	struct nl_sock *nlh_event;
	struct nl_cache *link_cache;
	struct nl_cache *address_cache;
	struct nl_cache *route_cache;
	GIOChannel *event_channel;
	guint event_id;

	GUdevClient *udev_client;
	GHashTable *udev_devices;

	int support_kernel_extended_ifa_flags;
} NMLinuxPlatformPrivate;

#define NM_LINUX_PLATFORM_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), NM_TYPE_LINUX_PLATFORM, NMLinuxPlatformPrivate))

G_DEFINE_TYPE (NMLinuxPlatform, nm_linux_platform, NM_TYPE_PLATFORM)

void
nm_linux_platform_setup (void)
{
	nm_platform_setup (NM_TYPE_LINUX_PLATFORM);
}

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

/* libnl library workarounds and additions */

/* Automatic deallocation of local variables */
#define auto_nl_cache __attribute__((cleanup(put_nl_cache)))
static void
put_nl_cache (void *ptr)
{
	struct nl_cache **cache = ptr;

	if (cache && *cache) {
		nl_cache_free (*cache);
		*cache = NULL;
	}
}

#define auto_nl_object __attribute__((cleanup(put_nl_object)))
static void
put_nl_object (void *ptr)
{
	struct nl_object **object = ptr;

	if (object && *object) {
		nl_object_put (*object);
		*object = NULL;
	}
}

#define auto_nl_addr __attribute__((cleanup(put_nl_addr)))
static void
put_nl_addr (void *ptr)
{
	struct nl_addr **object = ptr;

	if (object && *object) {
		nl_addr_put (*object);
		*object = NULL;
	}
}

/* libnl doesn't use const where due */
#define nl_addr_build(family, addr, addrlen) nl_addr_build (family, (gpointer) addr, addrlen)

/* rtnl_addr_set_prefixlen fails to update the nl_addr prefixlen */
static void
nm_rtnl_addr_set_prefixlen (struct rtnl_addr *rtnladdr, int plen)
{
	struct nl_addr *nladdr;

	rtnl_addr_set_prefixlen (rtnladdr, plen);

	nladdr = rtnl_addr_get_local (rtnladdr);
	if (nladdr)
		nl_addr_set_prefixlen (nladdr, plen);
}
#define rtnl_addr_set_prefixlen nm_rtnl_addr_set_prefixlen

typedef enum {
	LINK,
	IP4_ADDRESS,
	IP6_ADDRESS,
	IP4_ROUTE,
	IP6_ROUTE,
	N_TYPES
} ObjectType;

typedef enum {
	ADDED,
	CHANGED,
	REMOVED,
	N_STATUSES
} ObjectStatus;

static ObjectType
object_type_from_nl_object (const struct nl_object *object)
{
	const char *type_str = nl_object_get_type (object);

	g_assert (object);

	if (!strcmp (type_str, "route/link"))
		return LINK;
	else if (!strcmp (type_str, "route/addr")) {
		switch (rtnl_addr_get_family ((struct rtnl_addr *) object)) {
		case AF_INET:
			return IP4_ADDRESS;
		case AF_INET6:
			return IP6_ADDRESS;
		default:
			g_assert_not_reached ();
		}
	} else if (!strcmp (type_str, "route/route")) {
		switch (rtnl_route_get_family ((struct rtnl_route *) object)) {
		case AF_INET:
			return IP4_ROUTE;
		case AF_INET6:
			return IP6_ROUTE;
		default:
			g_assert_not_reached ();
		}
	} else
		g_assert_not_reached ();
}

/* libnl inclues LINK_ATTR_FAMILY in oo_id_attrs of link_obj_ops and thus
 * refuses to search for items that lack this attribute. I believe this is a
 * bug or a bad design at the least. Address family is not an identifying
 * attribute of a network interface and IMO is not an attribute of a network
 * interface at all.
 */
static struct nl_object *
nm_nl_cache_search (struct nl_cache *cache, struct nl_object *needle)
{
	if (object_type_from_nl_object (needle) == LINK)
		rtnl_link_set_family ((struct rtnl_link *) needle, AF_UNSPEC);

	return nl_cache_search (cache, needle);
}
#define nl_cache_search nm_nl_cache_search

/* Ask the kernel for an object identical (as in nl_cache_identical) to the
 * needle argument. This is a kernel counterpart for nl_cache_search.
 *
 * libnl 3.2 doesn't seem to provide such functionality.
 */
static struct nl_object *
get_kernel_object (struct nl_sock *sock, struct nl_object *needle)
{

	switch (object_type_from_nl_object (needle)) {
	case LINK:
		{
			struct nl_object *kernel_object;
			int ifindex = rtnl_link_get_ifindex ((struct rtnl_link *) needle);
			const char *name = rtnl_link_get_name ((struct rtnl_link *) needle);
			int nle;

			nle = rtnl_link_get_kernel (sock, ifindex, name, (struct rtnl_link **) &kernel_object);
			switch (nle) {
			case -NLE_SUCCESS:
				return kernel_object;
			case -NLE_NODEV:
				return NULL;
			default:
				error ("Netlink error: %s", nl_geterror (nle));
				return NULL;
			}
		}
	default:
		/* Fallback to a one-time cache allocation. */
		{
			struct nl_cache *cache;
			struct nl_object *object;
			int nle;

			nle = nl_cache_alloc_and_fill (
					nl_cache_ops_lookup (nl_object_get_type (needle)),
					sock, &cache);
			g_return_val_if_fail (!nle, NULL);
			object = nl_cache_search (cache, needle);

			nl_cache_free (cache);
			return object;
		}
	}
}

/* libnl 3.2 doesn't seem to provide such a generic way to add libnl-route objects. */
static int
add_kernel_object (struct nl_sock *sock, struct nl_object *object)
{
	switch (object_type_from_nl_object (object)) {
	case LINK:
		return rtnl_link_add (sock, (struct rtnl_link *) object, NLM_F_CREATE);
	case IP4_ADDRESS:
	case IP6_ADDRESS:
		return rtnl_addr_add (sock, (struct rtnl_addr *) object, NLM_F_CREATE | NLM_F_REPLACE);
	case IP4_ROUTE:
	case IP6_ROUTE:
		return rtnl_route_add (sock, (struct rtnl_route *) object, NLM_F_CREATE | NLM_F_REPLACE);
	default:
		g_assert_not_reached ();
	}
}

/* libnl 3.2 doesn't seem to provide such a generic way to delete libnl-route objects. */
static int
delete_kernel_object (struct nl_sock *sock, struct nl_object *object)
{
	switch (object_type_from_nl_object (object)) {
	case LINK:
		return rtnl_link_delete (sock, (struct rtnl_link *) object);
	case IP4_ADDRESS:
	case IP6_ADDRESS:
		return rtnl_addr_delete (sock, (struct rtnl_addr *) object, 0);
	case IP4_ROUTE:
	case IP6_ROUTE:
		return rtnl_route_delete (sock, (struct rtnl_route *) object, 0);
	default:
		g_assert_not_reached ();
	}
}

/* nm_rtnl_link_parse_info_data(): Re-fetches a link from the kernel
 * and parses its IFLA_INFO_DATA using a caller-provided parser.
 *
 * Code is stolen from rtnl_link_get_kernel(), nl_pickup(), and link_msg_parser().
 */

typedef int (*NMNLInfoDataParser) (struct nlattr *info_data, gpointer parser_data);

typedef struct {
	NMNLInfoDataParser parser;
	gpointer parser_data;
} NMNLInfoDataClosure;

static struct nla_policy info_data_link_policy[IFLA_MAX + 1] = {
	[IFLA_LINKINFO]	= { .type = NLA_NESTED },
};

static struct nla_policy info_data_link_info_policy[IFLA_INFO_MAX + 1] = {
	[IFLA_INFO_DATA]	= { .type = NLA_NESTED },
};

static int
info_data_parser (struct nl_msg *msg, void *arg)
{
	NMNLInfoDataClosure *closure = arg;
	struct nlmsghdr *n = nlmsg_hdr (msg);
	struct nlattr *tb[IFLA_MAX + 1];
	struct nlattr *li[IFLA_INFO_MAX + 1];
	int err;

	if (!nlmsg_valid_hdr (n, sizeof (struct ifinfomsg)))
		return -NLE_MSG_TOOSHORT;

	err = nlmsg_parse (n, sizeof (struct ifinfomsg), tb, IFLA_MAX, info_data_link_policy);
	if (err < 0)
		return err;

	if (!tb[IFLA_LINKINFO])
		return -NLE_MISSING_ATTR;

	err = nla_parse_nested (li, IFLA_INFO_MAX, tb[IFLA_LINKINFO], info_data_link_info_policy);
	if (err < 0)
		return err;

	if (!li[IFLA_INFO_DATA])
		return -NLE_MISSING_ATTR;

	return closure->parser (li[IFLA_INFO_DATA], closure->parser_data);
}

static int
nm_rtnl_link_parse_info_data (struct nl_sock *sk, int ifindex,
                              NMNLInfoDataParser parser, gpointer parser_data)
{
	NMNLInfoDataClosure data = { .parser = parser, .parser_data = parser_data };
	struct nl_msg *msg = NULL;
	struct nl_cb *cb;
	int err;

	err = rtnl_link_build_get_request (ifindex, NULL, &msg);
	if (err < 0)
		return err;

	err = nl_send_auto (sk, msg);
	nlmsg_free (msg);
	if (err < 0)
		return err;

	cb = nl_cb_clone (nl_socket_get_cb (sk));
	if (cb == NULL)
		return -NLE_NOMEM;
	nl_cb_set (cb, NL_CB_VALID, NL_CB_CUSTOM, info_data_parser, &data);

	err = nl_recvmsgs (sk, cb);
	nl_cb_put (cb);
	if (err < 0)
		return err;

	nl_wait_for_ack (sk);
	return 0;
}

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

static gboolean
ethtool_get (const char *name, gpointer edata)
{
	struct ifreq ifr;
	int fd;

	memset (&ifr, 0, sizeof (ifr));
	strncpy (ifr.ifr_name, name, IFNAMSIZ);
	ifr.ifr_data = edata;

	fd = socket (PF_INET, SOCK_DGRAM, 0);
	if (fd < 0) {
		error ("ethtool: Could not open socket.");
		return FALSE;
	}

	if (ioctl (fd, SIOCETHTOOL, &ifr) < 0) {
		debug ("ethtool: Request failed: %s", strerror (errno));
		close (fd);
		return FALSE;
	}

	close (fd);
	return TRUE;
}

static int
ethtool_get_stringset_index (const char *ifname, int stringset_id, const char *string)
{
	auto_g_free struct ethtool_sset_info *info = NULL;
	auto_g_free struct ethtool_gstrings *strings = NULL;
	guint32 len, i;

	info = g_malloc0 (sizeof (*info) + sizeof (guint32));
	info->cmd = ETHTOOL_GSSET_INFO;
	info->reserved = 0;
	info->sset_mask = 1ULL << stringset_id;

	if (!ethtool_get (ifname, info))
		return -1;
	if (!info->sset_mask)
		return -1;

	len = info->data[0];

	strings = g_malloc0 (sizeof (*strings) + len * ETH_GSTRING_LEN);
	strings->cmd = ETHTOOL_GSTRINGS;
	strings->string_set = stringset_id;
	strings->len = len;
	if (!ethtool_get (ifname, strings))
		return -1;

	for (i = 0; i < len; i++) {
		if (!strcmp ((char *) &strings->data[i * ETH_GSTRING_LEN], string))
			return i;
	}

	return -1;
}

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

static void
_check_support_kernel_extended_ifa_flags_init (NMLinuxPlatformPrivate *priv, struct nl_msg *msg)
{
	struct nlmsghdr *msg_hdr = nlmsg_hdr (msg);

	g_return_if_fail (priv->support_kernel_extended_ifa_flags == 0);
	g_return_if_fail (msg_hdr->nlmsg_type == RTM_NEWADDR);

	/* the extended address flags are only set for AF_INET6 */
	if (((struct ifaddrmsg *) nlmsg_data (msg_hdr))->ifa_family != AF_INET6)
		return;

	/* see if the nl_msg contains the IFA_FLAGS attribute. If it does,
	 * we assume, that the kernel supports extended flags, IFA_F_MANAGETEMPADDR
	 * and IFA_F_NOPREFIXROUTE (they were added together).
	 **/
	priv->support_kernel_extended_ifa_flags =
	    nlmsg_find_attr (msg_hdr, sizeof (struct ifaddrmsg), 8 /* IFA_FLAGS */)
	    ? 1 : -1;
}

static gboolean
check_support_kernel_extended_ifa_flags (NMPlatform *platform)
{
	NMLinuxPlatformPrivate *priv;

	g_return_val_if_fail (NM_IS_LINUX_PLATFORM (platform), FALSE);

	priv = NM_LINUX_PLATFORM_GET_PRIVATE (platform);

	if (priv->support_kernel_extended_ifa_flags == 0) {
		g_warn_if_reached ();
		priv->support_kernel_extended_ifa_flags = -1;
	}

	return priv->support_kernel_extended_ifa_flags > 0;
}


/* Object type specific utilities */

static const char *
type_to_string (NMLinkType type)
{
	/* Note that this only has to support virtual types */
	switch (type) {
	case NM_LINK_TYPE_DUMMY:
		return "dummy";
	case NM_LINK_TYPE_GRE:
		return "gre";
	case NM_LINK_TYPE_GRETAP:
		return "gretap";
	case NM_LINK_TYPE_IFB:
		return "ifb";
	case NM_LINK_TYPE_MACVLAN:
		return "macvlan";
	case NM_LINK_TYPE_MACVTAP:
		return "macvtap";
	case NM_LINK_TYPE_TAP:
		return "tap";
	case NM_LINK_TYPE_TUN:
		return "tun";
	case NM_LINK_TYPE_VETH:
		return "veth";
	case NM_LINK_TYPE_VLAN:
		return "vlan";
	case NM_LINK_TYPE_BRIDGE:
		return "bridge";
	case NM_LINK_TYPE_BOND:
		return "bond";
	case NM_LINK_TYPE_TEAM:
		return "team";
	default:
		g_warning ("Wrong type: %d", type);
		return NULL;
	}
}

#define return_type(t, name) \
	G_STMT_START { \
		if (out_name) \
			*out_name = name; \
		return t; \
	} G_STMT_END

static NMLinkType
link_type_from_udev (NMPlatform *platform, int ifindex, int arptype, const char **out_name)
{
	NMLinuxPlatformPrivate *priv = NM_LINUX_PLATFORM_GET_PRIVATE (platform);
	GUdevDevice *udev_device;
	const char *prop, *name, *sysfs_path;

	udev_device = g_hash_table_lookup (priv->udev_devices, GINT_TO_POINTER (ifindex));
	if (!udev_device)
		return_type (NM_LINK_TYPE_UNKNOWN, "unknown");

	prop = g_udev_device_get_property (udev_device, "ID_NM_OLPC_MESH");
	if (prop)
		return_type (NM_LINK_TYPE_OLPC_MESH, "olpc-mesh");

	prop = g_udev_device_get_property (udev_device, "DEVTYPE");
	name = g_udev_device_get_name (udev_device);
	sysfs_path = g_udev_device_get_sysfs_path (udev_device);
	if (g_strcmp0 (prop, "wlan") == 0 || wifi_utils_is_wifi (name, sysfs_path))
		return_type (NM_LINK_TYPE_WIFI, "wifi");
	else if (g_strcmp0 (prop, "wwan") == 0)
		return_type (NM_LINK_TYPE_WWAN_ETHERNET, "wwan");
	else if (g_strcmp0 (prop, "wimax") == 0)
		return_type (NM_LINK_TYPE_WIMAX, "wimax");

	if (arptype == ARPHRD_ETHER)
		return_type (NM_LINK_TYPE_ETHERNET, "ethernet");

	return_type (NM_LINK_TYPE_UNKNOWN, "unknown");
}

static gboolean
link_is_software (struct rtnl_link *rtnllink)
{
	const char *type;

	/* FIXME: replace somehow with NMLinkType or nm_platform_is_software(), but
	 * solve the infinite callstack problems that getting the type of a TUN/TAP
	 * device causes.
	 */

	if (   rtnl_link_get_arptype (rtnllink) == ARPHRD_INFINIBAND
	    && strchr (rtnl_link_get_name (rtnllink), '.'))
		return TRUE;

	type = rtnl_link_get_type (rtnllink);
	if (type == NULL)
		return FALSE;

	if (!strcmp (type, "dummy") ||
	    !strcmp (type, "gre") ||
	    !strcmp (type, "gretap") ||
	    !strcmp (type, "macvlan") ||
	    !strcmp (type, "macvtap") ||
	    !strcmp (type, "tun") ||
	    !strcmp (type, "veth") ||
	    !strcmp (type, "vlan") ||
	    !strcmp (type, "bridge") ||
	    !strcmp (type, "bond") ||
	    !strcmp (type, "team"))
		return TRUE;

	return FALSE;
}

static const char *
ethtool_get_driver (const char *ifname)
{
	struct ethtool_drvinfo drvinfo = { 0 };

	drvinfo.cmd = ETHTOOL_GDRVINFO;
	if (!ethtool_get (ifname, &drvinfo))
		return NULL;

	if (!*drvinfo.driver)
		return NULL;

	return g_intern_string (drvinfo.driver);
}

static gboolean
link_is_announceable (NMPlatform *platform, struct rtnl_link *rtnllink)
{
	NMLinuxPlatformPrivate *priv = NM_LINUX_PLATFORM_GET_PRIVATE (platform);

	/* Software devices are always visible outside the platform */
	if (link_is_software (rtnllink))
		return TRUE;

	/* Hardware devices must be found by udev so rules get run and tags set */
	if (g_hash_table_lookup (priv->udev_devices,
	                         GINT_TO_POINTER (rtnl_link_get_ifindex (rtnllink))))
		return TRUE;

	return FALSE;
}

static NMLinkType
link_extract_type (NMPlatform *platform, struct rtnl_link *rtnllink, const char **out_name)
{
	const char *type;

	if (!rtnllink)
		return_type (NM_LINK_TYPE_NONE, NULL);

	type = rtnl_link_get_type (rtnllink);

	if (!type) {
		int arptype = rtnl_link_get_arptype (rtnllink);
		const char *driver;

		if (arptype == ARPHRD_LOOPBACK)
			return_type (NM_LINK_TYPE_LOOPBACK, "loopback");
		else if (arptype == ARPHRD_INFINIBAND)
			return_type (NM_LINK_TYPE_INFINIBAND, "infiniband");
		else if (arptype == 256) {
			/* Some s390 CTC-type devices report 256 for the encapsulation type
			 * for some reason, but we need to call them Ethernet. FIXME: use
			 * something other than interface name to detect CTC here.
			 */
			if (g_str_has_prefix (rtnl_link_get_name (rtnllink), "ctc"))
				return_type (NM_LINK_TYPE_ETHERNET, "ethernet");
		}

		driver = ethtool_get_driver (rtnl_link_get_name (rtnllink));
		if (!g_strcmp0 (driver, "openvswitch"))
			return_type (NM_LINK_TYPE_OPENVSWITCH, "openvswitch");

		return link_type_from_udev (platform,
		                            rtnl_link_get_ifindex (rtnllink),
		                            arptype,
		                            out_name);
	} else if (!strcmp (type, "dummy"))
		return_type (NM_LINK_TYPE_DUMMY, "dummy");
	else if (!strcmp (type, "gre"))
		return_type (NM_LINK_TYPE_GRE, "gre");
	else if (!strcmp (type, "gretap"))
		return_type (NM_LINK_TYPE_GRETAP, "gretap");
	else if (!strcmp (type, "ifb"))
		return_type (NM_LINK_TYPE_IFB, "ifb");
	else if (!strcmp (type, "macvlan"))
		return_type (NM_LINK_TYPE_MACVLAN, "macvlan");
	else if (!strcmp (type, "macvtap"))
		return_type (NM_LINK_TYPE_MACVTAP, "macvtap");
	else if (!strcmp (type, "tun")) {
		NMPlatformTunProperties props;
		guint flags;

		if (nm_platform_tun_get_properties (rtnl_link_get_ifindex (rtnllink), &props)) {
			if (!g_strcmp0 (props.mode, "tap"))
				return_type (NM_LINK_TYPE_TAP, "tap");
			if (!g_strcmp0 (props.mode, "tun"))
				return_type (NM_LINK_TYPE_TUN, "tun");
		}
		flags = rtnl_link_get_flags (rtnllink);

		nm_log_dbg (LOGD_PLATFORM, "Failed to read tun properties for interface %d (link flags: %X)",
		                           rtnl_link_get_ifindex (rtnllink), flags);

		/* try guessing the type using the link flags instead... */
		if (flags & IFF_POINTOPOINT)
			return_type (NM_LINK_TYPE_TUN, "tun");
		return_type (NM_LINK_TYPE_TAP, "tap");
	} else if (!strcmp (type, "veth"))
		return_type (NM_LINK_TYPE_VETH, "veth");
	else if (!strcmp (type, "vlan"))
		return_type (NM_LINK_TYPE_VLAN, "vlan");
	else if (!strcmp (type, "bridge"))
		return_type (NM_LINK_TYPE_BRIDGE, "bridge");
	else if (!strcmp (type, "bond"))
		return_type (NM_LINK_TYPE_BOND, "bond");
	else if (!strcmp (type, "team"))
		return_type (NM_LINK_TYPE_TEAM, "team");

	return_type (NM_LINK_TYPE_UNKNOWN, type);
}

static const char *
udev_get_driver (NMPlatform *platform, GUdevDevice *device, int ifindex)
{
	GUdevDevice *parent = NULL, *grandparent = NULL;
	const char *driver, *subsys;

	driver = g_udev_device_get_driver (device);
	if (driver)
		return driver;

	/* Try the parent */
	parent = g_udev_device_get_parent (device);
	if (parent) {
		driver = g_udev_device_get_driver (parent);
		if (!driver) {
			/* Try the grandparent if it's an ibmebus device or if the
			 * subsys is NULL which usually indicates some sort of
			 * platform device like a 'gadget' net interface.
			 */
			subsys = g_udev_device_get_subsystem (parent);
			if (   (g_strcmp0 (subsys, "ibmebus") == 0)
			    || (subsys == NULL)) {
				grandparent = g_udev_device_get_parent (parent);
				if (grandparent) {
					driver = g_udev_device_get_driver (grandparent);
				}
			}
		}
	}

	/* Intern the string so we don't have to worry about memory
	 * management in NMPlatformLink.
	 */
	if (driver)
		driver = g_intern_string (driver);

	g_clear_object (&parent);
	g_clear_object (&grandparent);

	return driver;
}

static void
link_init (NMPlatform *platform, NMPlatformLink *info, struct rtnl_link *rtnllink)
{
	NMLinuxPlatformPrivate *priv = NM_LINUX_PLATFORM_GET_PRIVATE (platform);
	GUdevDevice *udev_device;

	memset (info, 0, sizeof (*info));

	g_assert (rtnllink);

	info->ifindex = rtnl_link_get_ifindex (rtnllink);
	g_strlcpy (info->name, rtnl_link_get_name (rtnllink), sizeof (info->name));
	info->type = link_extract_type (platform, rtnllink, &info->type_name);
	info->up = !!(rtnl_link_get_flags (rtnllink) & IFF_UP);
	info->connected = !!(rtnl_link_get_flags (rtnllink) & IFF_LOWER_UP);
	info->arp = !(rtnl_link_get_flags (rtnllink) & IFF_NOARP);
	info->master = rtnl_link_get_master (rtnllink);
	info->parent = rtnl_link_get_link (rtnllink);
	info->mtu = rtnl_link_get_mtu (rtnllink);

	udev_device = g_hash_table_lookup (priv->udev_devices, GINT_TO_POINTER (info->ifindex));
	if (udev_device) {
		info->driver = udev_get_driver (platform, udev_device, info->ifindex);
		if (!info->driver)
			info->driver = rtnl_link_get_type (rtnllink);
		if (!info->driver)
			info->driver = ethtool_get_driver (info->name);
		if (!info->driver)
			info->driver = "unknown";
		info->udi = g_udev_device_get_sysfs_path (udev_device);
	}
}

/* Hack: Empty bridges and bonds have IFF_LOWER_UP flag and therefore they break
 * the carrier detection. This hack makes nm-platform think they don't have the
 * IFF_LOWER_UP flag. This seems to also apply to bonds (specifically) with all
 * slaves down.
 *
 * Note: This is still a bit racy but when NetworkManager asks for enslaving a slave,
 * nm-platform will do that synchronously and will immediately ask for both master
 * and slave information after the enslaving request. After the synchronous call, the
 * master carrier is already updated with the slave carrier in mind.
 *
 * https://bugzilla.redhat.com/show_bug.cgi?id=910348
 */
static void
hack_empty_master_iff_lower_up (NMPlatform *platform, struct nl_object *object)
{
	NMLinuxPlatformPrivate *priv = NM_LINUX_PLATFORM_GET_PRIVATE (platform);
	struct rtnl_link *rtnllink;
	int ifindex;
	struct nl_object *slave;

	if (!object)
		return;
	if (strcmp (nl_object_get_type (object), "route/link"))
		return;

	rtnllink = (struct rtnl_link *) object;

	ifindex = rtnl_link_get_ifindex (rtnllink);

	switch (link_extract_type (platform, rtnllink, NULL)) {
	case NM_LINK_TYPE_BRIDGE:
	case NM_LINK_TYPE_BOND:
		for (slave = nl_cache_get_first (priv->link_cache); slave; slave = nl_cache_get_next (slave)) {
			struct rtnl_link *rtnlslave = (struct rtnl_link *) slave;
			if (rtnl_link_get_master (rtnlslave) == ifindex
					&& rtnl_link_get_flags (rtnlslave) & IFF_LOWER_UP)
				return;
		}
		break;
	default:
		return;
	}

	rtnl_link_unset_flags (rtnllink, IFF_LOWER_UP);
}

static void
init_ip4_address (NMPlatformIP4Address *address, struct rtnl_addr *rtnladdr)
{
	struct nl_addr *nladdr = rtnl_addr_get_local (rtnladdr);
	struct nl_addr *nlpeer = rtnl_addr_get_peer (rtnladdr);

	g_assert (nladdr);

	memset (address, 0, sizeof (*address));

	address->ifindex = rtnl_addr_get_ifindex (rtnladdr);
	address->plen = rtnl_addr_get_prefixlen (rtnladdr);
	address->timestamp = nm_utils_get_monotonic_timestamp_s ();
	address->lifetime = rtnl_addr_get_valid_lifetime (rtnladdr);
	address->preferred = rtnl_addr_get_preferred_lifetime (rtnladdr);
	g_assert (nl_addr_get_len (nladdr) == sizeof (address->address));
	memcpy (&address->address, nl_addr_get_binary_addr (nladdr), sizeof (address->address));
	if (nlpeer) {
		g_assert (nl_addr_get_len (nlpeer) == sizeof (address->peer_address));
		memcpy (&address->peer_address, nl_addr_get_binary_addr (nlpeer), sizeof (address->peer_address));
	}
}

static void
init_ip6_address (NMPlatformIP6Address *address, struct rtnl_addr *rtnladdr)
{
	struct nl_addr *nladdr = rtnl_addr_get_local (rtnladdr);
	struct nl_addr *nlpeer = rtnl_addr_get_peer (rtnladdr);

	memset (address, 0, sizeof (*address));

	address->ifindex = rtnl_addr_get_ifindex (rtnladdr);
	address->plen = rtnl_addr_get_prefixlen (rtnladdr);
	address->timestamp = nm_utils_get_monotonic_timestamp_s ();
	address->lifetime = rtnl_addr_get_valid_lifetime (rtnladdr);
	address->preferred = rtnl_addr_get_preferred_lifetime (rtnladdr);
	address->flags = rtnl_addr_get_flags (rtnladdr);
	g_assert (nl_addr_get_len (nladdr) == sizeof (address->address));
	memcpy (&address->address, nl_addr_get_binary_addr (nladdr), sizeof (address->address));
	if (nlpeer) {
		g_assert (nl_addr_get_len (nlpeer) == sizeof (address->peer_address));
		memcpy (&address->peer_address, nl_addr_get_binary_addr (nlpeer), sizeof (address->peer_address));
	}
}

static gboolean
init_ip4_route (NMPlatformIP4Route *route, struct rtnl_route *rtnlroute)
{
	struct nl_addr *dst, *gw;
	struct rtnl_nexthop *nexthop;

	memset (route, 0, sizeof (*route));

	/* Multi-hop routes not supported. */
	if (rtnl_route_get_nnexthops (rtnlroute) != 1)
		return FALSE;

	nexthop = rtnl_route_nexthop_n (rtnlroute, 0);
	dst = rtnl_route_get_dst (rtnlroute);
	gw = rtnl_route_nh_get_gateway (nexthop);

	route->ifindex = rtnl_route_nh_get_ifindex (nexthop);
	route->plen = nl_addr_get_prefixlen (dst);
	/* Workaround on previous workaround for libnl default route prefixlen bug. */
	if (nl_addr_get_len (dst)) {
		g_assert (nl_addr_get_len (dst) == sizeof (route->network));
		memcpy (&route->network, nl_addr_get_binary_addr (dst), sizeof (route->network));
	}
	if (gw) {
		g_assert (nl_addr_get_len (gw) == sizeof (route->network));
		memcpy (&route->gateway, nl_addr_get_binary_addr (gw), sizeof (route->gateway));
	}
	route->metric = rtnl_route_get_priority (rtnlroute);
	rtnl_route_get_metric (rtnlroute, RTAX_ADVMSS, &route->mss);

	return TRUE;
}

static gboolean
init_ip6_route (NMPlatformIP6Route *route, struct rtnl_route *rtnlroute)
{
	struct nl_addr *dst, *gw;
	struct rtnl_nexthop *nexthop;

	memset (route, 0, sizeof (*route));

	/* Multi-hop routes not supported. */
	if (rtnl_route_get_nnexthops (rtnlroute) != 1)
		return FALSE;

	nexthop = rtnl_route_nexthop_n (rtnlroute, 0);
	dst = rtnl_route_get_dst (rtnlroute);
	gw = rtnl_route_nh_get_gateway (nexthop);

	route->ifindex = rtnl_route_nh_get_ifindex (nexthop);
	route->plen = nl_addr_get_prefixlen (dst);
	/* Workaround on previous workaround for libnl default route prefixlen bug. */
	if (nl_addr_get_len (dst)) {
		g_assert (nl_addr_get_len (dst) == sizeof (route->network));
		memcpy (&route->network, nl_addr_get_binary_addr (dst), sizeof (route->network));
	}
	if (gw) {
		g_assert (nl_addr_get_len (gw) == sizeof (route->network));
		memcpy (&route->gateway, nl_addr_get_binary_addr (gw), sizeof (route->gateway));
	}
	route->metric = rtnl_route_get_priority (rtnlroute);
	rtnl_route_get_metric (rtnlroute, RTAX_ADVMSS, &route->mss);

	return TRUE;
}

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

/* Object and cache manipulation */

static const char *signal_by_type_and_status[N_TYPES][N_STATUSES] = {
	{ NM_PLATFORM_LINK_ADDED, NM_PLATFORM_LINK_CHANGED, NM_PLATFORM_LINK_REMOVED },
	{ NM_PLATFORM_IP4_ADDRESS_ADDED, NM_PLATFORM_IP4_ADDRESS_CHANGED, NM_PLATFORM_IP4_ADDRESS_REMOVED },
	{ NM_PLATFORM_IP6_ADDRESS_ADDED, NM_PLATFORM_IP6_ADDRESS_CHANGED, NM_PLATFORM_IP6_ADDRESS_REMOVED },
	{ NM_PLATFORM_IP4_ROUTE_ADDED, NM_PLATFORM_IP4_ROUTE_CHANGED, NM_PLATFORM_IP4_ROUTE_REMOVED },
	{ NM_PLATFORM_IP6_ROUTE_ADDED, NM_PLATFORM_IP6_ROUTE_CHANGED, NM_PLATFORM_IP6_ROUTE_REMOVED }
};

static struct nl_cache *
choose_cache (NMPlatform *platform, struct nl_object *object)
{
	NMLinuxPlatformPrivate *priv = NM_LINUX_PLATFORM_GET_PRIVATE (platform);

	switch (object_type_from_nl_object (object)) {
	case LINK:
		return priv->link_cache;
	case IP4_ADDRESS:
	case IP6_ADDRESS:
		return priv->address_cache;
	case IP4_ROUTE:
	case IP6_ROUTE:
		return priv->route_cache;
	default:
		g_assert_not_reached ();
	}
}

static gboolean
object_has_ifindex (struct nl_object *object, int ifindex)
{
	switch (object_type_from_nl_object (object)) {
	case IP4_ADDRESS:
	case IP6_ADDRESS:
		return ifindex == rtnl_addr_get_ifindex ((struct rtnl_addr *) object);
	case IP4_ROUTE:
	case IP6_ROUTE:
		{
			struct rtnl_route *rtnlroute = (struct rtnl_route *) object;
			struct rtnl_nexthop *nexthop;

			if (rtnl_route_get_nnexthops (rtnlroute) != 1)
				return FALSE;
			nexthop = rtnl_route_nexthop_n (rtnlroute, 0);

			return ifindex == rtnl_route_nh_get_ifindex (nexthop);
		}
	default:
		g_assert_not_reached ();
	}
}

static gboolean refresh_object (NMPlatform *platform, struct nl_object *object, gboolean removed, NMPlatformReason reason);

static void
check_cache_items (NMPlatform *platform, struct nl_cache *cache, int ifindex)
{
	auto_nl_cache struct nl_cache *cloned_cache = nl_cache_clone (cache);
	struct nl_object *object;

	for (object = nl_cache_get_first (cloned_cache); object; object = nl_cache_get_next (object)) {
		debug ("cache %p object %p", cloned_cache, object);
		g_assert (nl_object_get_cache (object) == cloned_cache);
		if (object_has_ifindex (object, ifindex))
			refresh_object (platform, object, TRUE, NM_PLATFORM_REASON_CACHE_CHECK);
	}
}

static void
announce_object (NMPlatform *platform, const struct nl_object *object, ObjectStatus status, NMPlatformReason reason)
{
	NMLinuxPlatformPrivate *priv = NM_LINUX_PLATFORM_GET_PRIVATE (platform);
	ObjectType object_type = object_type_from_nl_object (object);
	const char *sig = signal_by_type_and_status[object_type][status];

	switch (object_type) {
	case LINK:
		{
			NMPlatformLink device;
			struct rtnl_link *rtnl_link = (struct rtnl_link *) object;

			link_init (platform, &device, rtnl_link);

			/* Skip hardware devices not yet discovered by udev. They will be
			 * announced by udev_device_added(). This doesn't apply to removed
			 * devices, as those come either from udev_device_removed(),
			 * event_notification() or link_delete() which block the announcment
			 * themselves when appropriate.
			 */
			switch (status) {
			case ADDED:
			case CHANGED:
				if (!link_is_software (rtnl_link) && !device.driver)
					return;
				break;
			default:
				break;
			}

			/* Link deletion or setting down is sometimes accompanied by address
			 * and/or route deletion.
			 *
			 * More precisely, kernel removes routes when interface goes !IFF_UP and
			 * removes both addresses and routes when interface is removed.
			 */
			switch (status) {
			case CHANGED:
				if (!device.connected)
					check_cache_items (platform, priv->route_cache, device.ifindex);
				break;
			case REMOVED:
				check_cache_items (platform, priv->address_cache, device.ifindex);
				check_cache_items (platform, priv->route_cache, device.ifindex);
				break;
			default:
				break;
			}

			g_signal_emit_by_name (platform, sig, device.ifindex, &device, reason);
		}
		return;
	case IP4_ADDRESS:
		{
			NMPlatformIP4Address address;

			init_ip4_address (&address, (struct rtnl_addr *) object);

			/* Address deletion is sometimes accompanied by route deletion. We need to
			 * check all routes belonging to the same interface.
			 */
			switch (status) {
			case REMOVED:
				check_cache_items (platform, priv->route_cache, address.ifindex);
				break;
			default:
				break;
			}

			g_signal_emit_by_name (platform, sig, address.ifindex, &address, reason);
		}
		return;
	case IP6_ADDRESS:
		{
			NMPlatformIP6Address address;

			init_ip6_address (&address, (struct rtnl_addr *) object);
			g_signal_emit_by_name (platform, sig, address.ifindex, &address, reason);
		}
		return;
	case IP4_ROUTE:
		{
			NMPlatformIP4Route route;

			if (init_ip4_route (&route, (struct rtnl_route *) object))
				g_signal_emit_by_name (platform, sig, route.ifindex, &route, reason);
		}
		return;
	case IP6_ROUTE:
		{
			NMPlatformIP6Route route;

			if (init_ip6_route (&route, (struct rtnl_route *) object))
				g_signal_emit_by_name (platform, sig, route.ifindex, &route, reason);
		}
		return;
	default:
		error ("Announcing object: object type unknown: %d", object_type);
	}
}

static struct nl_object * build_rtnl_link (int ifindex, const char *name, NMLinkType type);

static gboolean
refresh_object (NMPlatform *platform, struct nl_object *object, gboolean removed, NMPlatformReason reason)
{
	NMLinuxPlatformPrivate *priv = NM_LINUX_PLATFORM_GET_PRIVATE (platform);
	auto_nl_object struct nl_object *cached_object = NULL;
	auto_nl_object struct nl_object *kernel_object = NULL;
	struct nl_cache *cache;
	int nle;

	cache = choose_cache (platform, object);
	cached_object = nl_cache_search (choose_cache (platform, object), object);
	kernel_object = get_kernel_object (priv->nlh, object);

	if (removed) {
		if (kernel_object)
			return TRUE;

		/* Only announce object if it was still in the cache. */
		if (cached_object) {
			nl_cache_remove (cached_object);

			announce_object (platform, cached_object, REMOVED, reason);
		}
	} else {
		if (!kernel_object)
			return FALSE;

		hack_empty_master_iff_lower_up (platform, kernel_object);

		if (cached_object)
			nl_cache_remove (cached_object);
		nle = nl_cache_add (cache, kernel_object);
		if (nle) {
			nm_log_dbg (LOGD_PLATFORM, "refresh_object(reason %d) failed during nl_cache_add with %d", reason, nle);
			return FALSE;
		}

		announce_object (platform, kernel_object, cached_object ? CHANGED : ADDED, reason);

		/* Refresh the master device (even on enslave/release) */
		if (object_type_from_nl_object (kernel_object) == LINK) {
			int kernel_master = rtnl_link_get_master ((struct rtnl_link *) kernel_object);
			int cached_master = cached_object ? rtnl_link_get_master ((struct rtnl_link *) cached_object) : 0;
			struct nl_object *master_object;

			if (kernel_master) {
				master_object = build_rtnl_link (kernel_master, NULL, NM_LINK_TYPE_NONE);
				refresh_object (platform, master_object, FALSE, NM_PLATFORM_REASON_INTERNAL);
				nl_object_put (master_object);
			}
			if (cached_master && cached_master != kernel_master) {
				master_object = build_rtnl_link (cached_master, NULL, NM_LINK_TYPE_NONE);
				refresh_object (platform, master_object, FALSE, NM_PLATFORM_REASON_INTERNAL);
				nl_object_put (master_object);
			}
		}
	}

	return TRUE;
}

/* Decreases the reference count if @obj for convenience */
static gboolean
add_object (NMPlatform *platform, struct nl_object *obj)
{
	auto_nl_object struct nl_object *object = obj;
	NMLinuxPlatformPrivate *priv = NM_LINUX_PLATFORM_GET_PRIVATE (platform);
	int nle;
	struct nl_dump_params dp = {
		.dp_type = NL_DUMP_DETAILS,
		.dp_fd = stderr,
	};

	nle = add_kernel_object (priv->nlh, object);

	/* NLE_EXIST is considered equivalent to success to avoid race conditions. You
	 * never know when something sends an identical object just before
	 * NetworkManager.
	 */
	switch (nle) {
	case -NLE_SUCCESS:
	case -NLE_EXIST:
		break;
	default:
		error ("Netlink error: %s", nl_geterror (nle));
		nl_object_dump (object, &dp);
		return FALSE;
	}

	return refresh_object (platform, object, FALSE, NM_PLATFORM_REASON_INTERNAL);
}

/* Decreases the reference count if @obj for convenience */
static gboolean
delete_object (NMPlatform *platform, struct nl_object *obj)
{
	NMLinuxPlatformPrivate *priv = NM_LINUX_PLATFORM_GET_PRIVATE (platform);
	auto_nl_object struct nl_object *object = obj;
	auto_nl_object struct nl_object *cached_object = NULL;
	int nle;

	/* FIXME: For some reason the result of build_rtnl_route() is not suitable
	 * for delete_kernel_object() and we need to search the cache first. If
	 * that problem is fixed, we can use 'object' directly.
	 */
	cached_object = nl_cache_search (choose_cache (platform, object), object);
	g_return_val_if_fail (cached_object, FALSE);

	nle = delete_kernel_object (priv->nlh, cached_object);

	/* NLE_OBJ_NOTFOUND is considered equivalent to success to avoid race conditions. You
	 * never know when something deletes the same object just before NetworkManager.
	 */
	switch (nle) {
	case -NLE_SUCCESS:
	case -NLE_OBJ_NOTFOUND:
		break;
	default:
		error ("Netlink error: %s", nl_geterror (nle));
		return FALSE;
	}

	refresh_object (platform, object, TRUE, NM_PLATFORM_REASON_INTERNAL);

	return TRUE;
}

static void
ref_object (struct nl_object *obj, void *data)
{
	struct nl_object **out = data;

	nl_object_get (obj);
	*out = obj;
}

/* This function does all the magic to avoid race conditions caused
 * by concurrent usage of synchronous commands and an asynchronous cache. This
 * might be a nice future addition to libnl but it requires to do all operations
 * through the cache manager. In this case, nm-linux-platform serves as the
 * cache manager instead of the one provided by libnl.
 */
static int
event_notification (struct nl_msg *msg, gpointer user_data)
{
	NMPlatform *platform = NM_PLATFORM (user_data);
	NMLinuxPlatformPrivate *priv = NM_LINUX_PLATFORM_GET_PRIVATE (platform);
	struct nl_cache *cache;
	auto_nl_object struct nl_object *object = NULL;
	auto_nl_object struct nl_object *cached_object = NULL;
	auto_nl_object struct nl_object *kernel_object = NULL;
	int event;
	int nle;

	event = nlmsg_hdr (msg)->nlmsg_type;

	if (priv->support_kernel_extended_ifa_flags == 0 && event == RTM_NEWADDR) {
		/* if kernel support for extended ifa flags is still undecided, use the opportunity
		 * now and use @msg to decide it. This saves a blocking net link request.
		 **/
		_check_support_kernel_extended_ifa_flags_init (priv, msg);
	}

	nl_msg_parse (msg, ref_object, &object);
	g_return_val_if_fail (object, NL_OK);

	cache = choose_cache (platform, object);
	cached_object = nl_cache_search (cache, object);
	kernel_object = get_kernel_object (priv->nlh, object);

	/* Just for debugging */
	if (object_type_from_nl_object (object) == LINK) {
		int ifindex = rtnl_link_get_ifindex ((struct rtnl_link *) object);
		const char *name = rtnl_link_get_name ((struct rtnl_link *) object);
		debug ("netlink event (type %d) for link: %s (%d)",
		       event, name ? name : "(unknown)", ifindex);
	} else
		debug ("netlink event (type %d)", event);

	hack_empty_master_iff_lower_up (platform, kernel_object);

	/* Removed object */
	switch (event) {
	case RTM_DELLINK:
	case RTM_DELADDR:
	case RTM_DELROUTE:
		/* Ignore inconsistent deletion
		 *
		 * Quick external deletion and addition can be occasionally
		 * seen as just a change.
		 */
		if (kernel_object)
			return NL_OK;
		/* Ignore internal deletion */
		if (!cached_object)
			return NL_OK;

		nl_cache_remove (cached_object);
		/* Don't announced removed interfaces that are not recognized by
		 * udev. They were either not yet discovered or they have been
		 * already removed and announced.
		 */
		if (event == RTM_DELLINK) {
			if (!link_is_announceable (platform, (struct rtnl_link *) object))
				return NL_OK;
		}
		announce_object (platform, cached_object, REMOVED, NM_PLATFORM_REASON_EXTERNAL);

		return NL_OK;
	case RTM_NEWLINK:
	case RTM_NEWADDR:
	case RTM_NEWROUTE:
		/* Ignore inconsistent addition or change (kernel will send a good one)
		 *
		 * Quick sequence of RTM_NEWLINK notifications can be occasionally
		 * collapsed to just one addition or deletion, depending of whether we
		 * already have the object in cache.
		 */
		if (!kernel_object)
			return NL_OK;
		/* Handle external addition */
		if (!cached_object) {
			nle = nl_cache_add (cache, kernel_object);
			if (nle) {
				error ("netlink cache error: %s", nl_geterror (nle));
				return NL_OK;
			}
			announce_object (platform, kernel_object, ADDED, NM_PLATFORM_REASON_EXTERNAL);
			return NL_OK;
		}
		/* Ignore non-change
		 *
		 * This also catches notifications for internal addition or change, unless
		 * another action occured very soon after it.
		 */
		if (!nl_object_diff (kernel_object, cached_object))
			return NL_OK;
		/* Handle external change */
		nl_cache_remove (cached_object);
		nle = nl_cache_add (cache, kernel_object);
		if (nle) {
			error ("netlink cache error: %s", nl_geterror (nle));
			return NL_OK;
		}
		announce_object (platform, kernel_object, CHANGED, NM_PLATFORM_REASON_EXTERNAL);

		return NL_OK;
	default:
		error ("Unknown netlink event: %d", event);
		return NL_OK;
	}
}

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

static gboolean
sysctl_set (NMPlatform *platform, const char *path, const char *value)
{
	int fd, len, nwrote, tries;
	char *actual;

	g_return_val_if_fail (path != NULL, FALSE);
	g_return_val_if_fail (value != NULL, FALSE);

	fd = open (path, O_WRONLY | O_TRUNC);
	if (fd == -1) {
		error ("sysctl: failed to open '%s': (%d) %s",
				path, errno, strerror (errno));
		return FALSE;
	}

	debug ("sysctl: setting '%s' to '%s'", path, value);

	/* Most sysfs and sysctl options don't care about a trailing LF, while some
	 * (like infiniband) do.  So always add the LF.  Also, neither sysfs nor
	 * sysctl support partial writes so the LF must be added to the string we're
	 * about to write.
	 */
	actual = g_strdup_printf ("%s\n", value);

	/* Try to write the entire value three times if a partial write occurs */
	len = strlen (actual);
	for (tries = 0, nwrote = 0; tries < 3 && nwrote != len; tries++) {
		errno = 0;
		nwrote = write (fd, actual, len);
		if (nwrote == -1) {
			if (errno == EINTR) {
				error ("sysctl: interrupted, will try again");
				continue;
			}
			break;
		}
	}
	if (nwrote != len && errno != EEXIST) {
		error ("sysctl: failed to set '%s' to '%s': (%d) %s",
		             path, value, errno, strerror (errno));
	}

	g_free (actual);
	close (fd);
	return (nwrote == len);
}

static char *
sysctl_get (NMPlatform *platform, const char *path, gboolean silent_on_error)
{
	GError *error = NULL;
	char *contents;

	if (!g_file_get_contents (path, &contents, NULL, &error)) {
		if (!silent_on_error)
			error ("error reading %s: %s", path, error->message);
		g_clear_error (&error);
		return NULL;
	}

	return g_strstrip (contents);
}

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

static GArray *
link_get_all (NMPlatform *platform)
{
	NMLinuxPlatformPrivate *priv = NM_LINUX_PLATFORM_GET_PRIVATE (platform);
	GArray *links = g_array_sized_new (TRUE, TRUE, sizeof (NMPlatformLink), nl_cache_nitems (priv->link_cache));
	NMPlatformLink device;
	struct nl_object *object;

	for (object = nl_cache_get_first (priv->link_cache); object; object = nl_cache_get_next (object)) {
		struct rtnl_link *rtnl_link = (struct rtnl_link *) object;

		if (link_is_announceable (platform, rtnl_link)) {
			link_init (platform, &device, rtnl_link);
			g_array_append_val (links, device);
		}
	}

	return links;
}

static struct nl_object *
build_rtnl_link (int ifindex, const char *name, NMLinkType type)
{
	struct rtnl_link *rtnllink;
	int nle;

	rtnllink = rtnl_link_alloc ();
	g_assert (rtnllink);
	if (ifindex)
		rtnl_link_set_ifindex (rtnllink, ifindex);
	if (name)
		rtnl_link_set_name (rtnllink, name);
	if (type) {
		nle = rtnl_link_set_type (rtnllink, type_to_string (type));
		g_assert (!nle);
	}

	return (struct nl_object *) rtnllink;
}

static gboolean
link_add (NMPlatform *platform, const char *name, NMLinkType type)
{
	int r;

	if (type == NM_LINK_TYPE_BOND) {
		/* When the kernel loads the bond module, either via explicit modprobe
		 * or automatically in response to creating a bond master, it will also
		 * create a 'bond0' interface.  Since the bond we're about to create may
		 * or may not be named 'bond0' prevent potential confusion about a bond
		 * that the user didn't want by telling the bonding module not to create
		 * bond0 automatically.
		 */
		if (!g_file_test ("/sys/class/net/bonding_masters", G_FILE_TEST_EXISTS))
			/* Ignore return value to shut up the compiler */
			r = system ("modprobe bonding max_bonds=0");
	}

	return add_object (platform, build_rtnl_link (0, name, type));
}

static struct rtnl_link *
link_get (NMPlatform *platform, int ifindex)
{
	NMLinuxPlatformPrivate *priv = NM_LINUX_PLATFORM_GET_PRIVATE (platform);
	auto_nl_object struct rtnl_link *rtnllink = rtnl_link_get (priv->link_cache, ifindex);

	if (!rtnllink) {
		platform->error = NM_PLATFORM_ERROR_NOT_FOUND;
		return NULL;
	}

	/* physical interfaces must be found by udev before they can be used */
	if (!link_is_announceable (platform, rtnllink)) {
		platform->error = NM_PLATFORM_ERROR_NOT_FOUND;
		return NULL;
	}

	nl_object_get ((struct nl_object *) rtnllink);
	return rtnllink;
}

static gboolean
link_change (NMPlatform *platform, int ifindex, struct rtnl_link *change)
{
	NMLinuxPlatformPrivate *priv = NM_LINUX_PLATFORM_GET_PRIVATE (platform);
	auto_nl_object struct rtnl_link *rtnllink = link_get (platform, ifindex);
	int nle;

	if (!rtnllink)
		return FALSE;

	nle = rtnl_link_change (priv->nlh, rtnllink, change, 0);

	/* NLE_EXIST is considered equivalent to success to avoid race conditions. You
	 * never know when something sends an identical object just before
	 * NetworkManager.
	 *
	 * When netlink returns NLE_OBJ_NOTFOUND, it usually means it failed to find
	 * firmware for the device, especially on nm_platform_link_set_up ().
	 * This is basically the same check as in the original code and could
	 * potentially be improved.
	 */
	switch (nle) {
	case -NLE_SUCCESS:
	case -NLE_EXIST:
		break;
	case -NLE_OBJ_NOTFOUND:
		error ("Firmware not found; Netlink error: %s)", nl_geterror (nle));
		platform->error = NM_PLATFORM_ERROR_NO_FIRMWARE;
		return FALSE;
	default:
		error ("Netlink error: %s", nl_geterror (nle));
		return FALSE;
	}

	return refresh_object (platform, (struct nl_object *) rtnllink, FALSE, NM_PLATFORM_REASON_INTERNAL);
}

static gboolean
link_delete (NMPlatform *platform, int ifindex)
{
	NMLinuxPlatformPrivate *priv = NM_LINUX_PLATFORM_GET_PRIVATE (platform);
	struct rtnl_link *rtnllink = rtnl_link_get (priv->link_cache, ifindex);

	if (!rtnllink) {
		platform->error = NM_PLATFORM_ERROR_NOT_FOUND;
		return FALSE;
	}

	return delete_object (platform, build_rtnl_link (ifindex, NULL, NM_LINK_TYPE_NONE));
}

static int
link_get_ifindex (NMPlatform *platform, const char *ifname)
{
	NMLinuxPlatformPrivate *priv = NM_LINUX_PLATFORM_GET_PRIVATE (platform);

	return rtnl_link_name2i (priv->link_cache, ifname);
}

static const char *
link_get_name (NMPlatform *platform, int ifindex)
{
	auto_nl_object struct rtnl_link *rtnllink = link_get (platform, ifindex);

	return rtnllink ? rtnl_link_get_name (rtnllink) : NULL;
}

static NMLinkType
link_get_type (NMPlatform *platform, int ifindex)
{
	auto_nl_object struct rtnl_link *rtnllink = link_get (platform, ifindex);

	return link_extract_type (platform, rtnllink, NULL);
}

static const char *
link_get_type_name (NMPlatform *platform, int ifindex)
{
	auto_nl_object struct rtnl_link *rtnllink = link_get (platform, ifindex);
	const char *type;

	link_extract_type (platform, rtnllink, &type);
	return type;
}

static guint32
link_get_flags (NMPlatform *platform, int ifindex)
{
	auto_nl_object struct rtnl_link *rtnllink = link_get (platform, ifindex);

	if (!rtnllink)
		return IFF_NOARP;

	return rtnl_link_get_flags (rtnllink);
}

static gboolean
link_is_up (NMPlatform *platform, int ifindex)
{
	return !!(link_get_flags (platform, ifindex) & IFF_UP);
}

static gboolean
link_is_connected (NMPlatform *platform, int ifindex)
{
	return !!(link_get_flags (platform, ifindex) & IFF_LOWER_UP);
}

static gboolean
link_uses_arp (NMPlatform *platform, int ifindex)
{
	return !(link_get_flags (platform, ifindex) & IFF_NOARP);
}

static gboolean
link_change_flags (NMPlatform *platform, int ifindex, unsigned int flags, gboolean value)
{
	auto_nl_object struct rtnl_link *change = rtnl_link_alloc ();

	g_return_val_if_fail (change != NULL, FALSE);

	if (value)
		rtnl_link_set_flags (change, flags);
	else
		rtnl_link_unset_flags (change, flags);

	return link_change (platform, ifindex, change);
}

static gboolean
link_set_up (NMPlatform *platform, int ifindex)
{
	return link_change_flags (platform, ifindex, IFF_UP, TRUE);
}

static gboolean
link_set_down (NMPlatform *platform, int ifindex)
{
	return link_change_flags (platform, ifindex, IFF_UP, FALSE);
}

static gboolean
link_set_arp (NMPlatform *platform, int ifindex)
{
	return link_change_flags (platform, ifindex, IFF_NOARP, FALSE);
}

static gboolean
link_set_noarp (NMPlatform *platform, int ifindex)
{
	return link_change_flags (platform, ifindex, IFF_NOARP, TRUE);
}

static gboolean
supports_ethtool_carrier_detect (const char *ifname)
{
	struct ethtool_cmd edata = { .cmd = ETHTOOL_GLINK };

	/* We ignore the result. If the ETHTOOL_GLINK call succeeded, then we
	 * assume the device supports carrier-detect, otherwise we assume it
	 * doesn't.
	 */
	return ethtool_get (ifname, &edata);
}

static gboolean
supports_mii_carrier_detect (const char *ifname)
{
	int fd;
	struct ifreq ifr;
	struct mii_ioctl_data *mii;
	gboolean supports_mii = FALSE;

	fd = socket (PF_INET, SOCK_DGRAM, 0);
	if (fd < 0) {
		nm_log_err (LOGD_PLATFORM, "couldn't open control socket.");
		return FALSE;
	}

	memset (&ifr, 0, sizeof (struct ifreq));
	strncpy (ifr.ifr_name, ifname, IFNAMSIZ);

	errno = 0;
	if (ioctl (fd, SIOCGMIIPHY, &ifr) < 0) {
		nm_log_dbg (LOGD_PLATFORM, "SIOCGMIIPHY failed: %d", errno);
		goto out;
	}

	/* If we can read the BMSR register, we assume that the card supports MII link detection */
	mii = (struct mii_ioctl_data *) &ifr.ifr_ifru;
	mii->reg_num = MII_BMSR;

	if (ioctl (fd, SIOCGMIIREG, &ifr) == 0) {
		nm_log_dbg (LOGD_PLATFORM, "SIOCGMIIREG result 0x%X", mii->val_out);
		supports_mii = TRUE;
	} else {
		nm_log_dbg (LOGD_PLATFORM, "SIOCGMIIREG failed: %d", errno);
	}

 out:
	close (fd);
	nm_log_dbg (LOGD_PLATFORM, "MII %s supported", supports_mii ? "is" : "not");
	return supports_mii;	
}

static gboolean
link_supports_carrier_detect (NMPlatform *platform, int ifindex)
{
	const char *name = nm_platform_link_get_name (ifindex);

	if (!name)
		return FALSE;

	/* We use netlink for the actual carrier detection, but netlink can't tell
	 * us whether the device actually supports carrier detection in the first
	 * place. We assume any device that does implements one of these two APIs.
	 */
	return supports_ethtool_carrier_detect (name) || supports_mii_carrier_detect (name);
}

static gboolean
link_supports_vlans (NMPlatform *platform, int ifindex)
{
	auto_nl_object struct rtnl_link *rtnllink = link_get (platform, ifindex);
	const char *name = nm_platform_link_get_name (ifindex);
	auto_g_free struct ethtool_gfeatures *features = NULL;
	int idx, block, bit, size;

	/* Only ARPHRD_ETHER links can possibly support VLANs. */
	if (!rtnllink || rtnl_link_get_arptype (rtnllink) != ARPHRD_ETHER)
		return FALSE;

	if (!name)
		return FALSE;

	idx = ethtool_get_stringset_index (name, ETH_SS_FEATURES, "vlan-challenged");
	if (idx == -1) {
		debug ("vlan-challenged ethtool feature does not exist?");
		return FALSE;
	}

	block = idx /  32;
	bit = idx % 32;
	size = block + 1;

	features = g_malloc0 (sizeof (*features) + size * sizeof (struct ethtool_get_features_block));
	features->cmd = ETHTOOL_GFEATURES;
	features->size = size;

	if (!ethtool_get (name, features))
		return FALSE;

	return !(features->features[block].active & (1 << bit));
}

static gboolean
link_set_address (NMPlatform *platform, int ifindex, gconstpointer address, size_t length)
{
	auto_nl_object struct rtnl_link *change = NULL;
	auto_nl_addr struct nl_addr *nladdr = NULL;

	change = rtnl_link_alloc ();
	g_return_val_if_fail (change, FALSE);

	nladdr = nl_addr_build (AF_LLC, address, length);
	g_return_val_if_fail (nladdr, FALSE);

	rtnl_link_set_addr (change, nladdr);

	return link_change (platform, ifindex, change);
}

static gconstpointer
link_get_address (NMPlatform *platform, int ifindex, size_t *length)
{
	auto_nl_object struct rtnl_link *rtnllink = link_get (platform, ifindex);
	struct nl_addr *nladdr;

	nladdr = rtnllink ? rtnl_link_get_addr (rtnllink) : NULL;

	if (length)
		*length = nladdr ? nl_addr_get_len (nladdr) : 0;

	return nladdr ? nl_addr_get_binary_addr (nladdr) : NULL;
}

static gboolean
link_set_mtu (NMPlatform *platform, int ifindex, guint32 mtu)
{
	auto_nl_object struct rtnl_link *change = rtnl_link_alloc ();

	g_return_val_if_fail (change != NULL, FALSE);
	rtnl_link_set_mtu (change, mtu);

	return link_change (platform, ifindex, change);
}

static guint32
link_get_mtu (NMPlatform *platform, int ifindex)
{
	auto_nl_object struct rtnl_link *rtnllink = link_get (platform, ifindex);

	return rtnllink ? rtnl_link_get_mtu (rtnllink) : 0;
}

static char *
link_get_physical_port_id (NMPlatform *platform, int ifindex)
{
	const char *ifname;
	char *path, *id;

	ifname = nm_platform_link_get_name (ifindex);
	if (!ifname)
		return NULL;

	path = g_strdup_printf ("/sys/class/net/%s/phys_port_id", ifname);
	id = sysctl_get (platform, path, TRUE);
	g_free (path);

	return id;
}

static int
vlan_add (NMPlatform *platform, const char *name, int parent, int vlan_id, guint32 vlan_flags)
{
	struct nl_object *object = build_rtnl_link (0, name, NM_LINK_TYPE_VLAN);
	struct rtnl_link *rtnllink = (struct rtnl_link *) object;
	unsigned int kernel_flags;

	kernel_flags = 0;
	if (vlan_flags & NM_VLAN_FLAG_REORDER_HEADERS)
		kernel_flags |= VLAN_FLAG_REORDER_HDR;
	if (vlan_flags & NM_VLAN_FLAG_GVRP)
		kernel_flags |= VLAN_FLAG_GVRP;
	if (vlan_flags & NM_VLAN_FLAG_LOOSE_BINDING)
		kernel_flags |= VLAN_FLAG_LOOSE_BINDING;

	rtnl_link_set_link (rtnllink, parent);
	rtnl_link_vlan_set_id (rtnllink, vlan_id);
	rtnl_link_vlan_set_flags (rtnllink, kernel_flags);

	return add_object (platform, object);
}

static gboolean
vlan_get_info (NMPlatform *platform, int ifindex, int *parent, int *vlan_id)
{
	auto_nl_object struct rtnl_link *rtnllink = link_get (platform, ifindex);

	if (parent)
		*parent = rtnllink ? rtnl_link_get_link (rtnllink) : 0;
	if (vlan_id)
		*vlan_id = rtnllink ? rtnl_link_vlan_get_id (rtnllink) : 0;

	return !!rtnllink;
}

static gboolean
vlan_set_ingress_map (NMPlatform *platform, int ifindex, int from, int to)
{
	/* We have to use link_get() because a "blank" rtnl_link won't have the
	 * right data structures to be able to call rtnl_link_vlan_set_ingress_map()
	 * on it. (Likewise below in vlan_set_egress_map().)
	 */
	auto_nl_object struct rtnl_link *change = link_get (platform, ifindex);

	if (!change)
		return FALSE;
	rtnl_link_vlan_set_ingress_map (change, from, to);

	return link_change (platform, ifindex, change);
}

static gboolean
vlan_set_egress_map (NMPlatform *platform, int ifindex, int from, int to)
{
	auto_nl_object struct rtnl_link *change = link_get (platform, ifindex);

	if (!change)
		return FALSE;
	rtnl_link_vlan_set_egress_map (change, from, to);

	return link_change (platform, ifindex, change);
}

static gboolean
link_enslave (NMPlatform *platform, int master, int slave)
{
	auto_nl_object struct rtnl_link *change = rtnl_link_alloc ();

	g_return_val_if_fail (change != NULL, FALSE);

	rtnl_link_set_master (change, master);

	return link_change (platform, slave, change);
}

static gboolean
link_release (NMPlatform *platform, int master, int slave)
{
	return link_enslave (platform, 0, slave);
}

static int
link_get_master (NMPlatform *platform, int slave)
{
	auto_nl_object struct rtnl_link *rtnllink = link_get (platform, slave);

	return rtnllink ? rtnl_link_get_master (rtnllink) : 0;
}

static char *
link_option_path (int master, const char *category, const char *option)
{
	const char *name = nm_platform_link_get_name (master);
   
	if (!name || !category || !option)
		return NULL;

	return g_strdup_printf ("/sys/class/net/%s/%s/%s", name, category, option);
}

static gboolean
link_set_option (int master, const char *category, const char *option, const char *value)
{
	auto_g_free char *path = link_option_path (master, category, option);

	return path && nm_platform_sysctl_set (path, value);
}

static char *
link_get_option (int master, const char *category, const char *option)
{
	auto_g_free char *path = link_option_path (master, category, option);

	return path ? nm_platform_sysctl_get (path, FALSE) : NULL;
}

static const char *
master_category (NMPlatform *platform, int master)
{
	switch (link_get_type (platform, master)) {
	case NM_LINK_TYPE_BRIDGE:
		return "bridge";
	case NM_LINK_TYPE_BOND:
		return "bonding";
	default:
		g_assert_not_reached ();
	}
}

static const char *
slave_category (NMPlatform *platform, int slave)
{
	int master = link_get_master (platform, slave);

	if (master <= 0) {
		platform->error = NM_PLATFORM_ERROR_NOT_SLAVE;
		return NULL;
	}

	switch (link_get_type (platform, master)) {
	case NM_LINK_TYPE_BRIDGE:
		return "brport";
	default:
		g_assert_not_reached ();
	}
}

static gboolean
master_set_option (NMPlatform *platform, int master, const char *option, const char *value)
{
	return link_set_option (master, master_category (platform, master), option, value);
}

static char *
master_get_option (NMPlatform *platform, int master, const char *option)
{
	return link_get_option (master, master_category (platform, master), option);
}

static gboolean
slave_set_option (NMPlatform *platform, int slave, const char *option, const char *value)
{
	return link_set_option (slave, slave_category (platform, slave), option, value);
}

static char *
slave_get_option (NMPlatform *platform, int slave, const char *option)
{
	return link_get_option (slave, slave_category (platform, slave), option);
}

static gboolean
infiniband_partition_add (NMPlatform *platform, int parent, int p_key)
{
	const char *parent_name;
	char *path, *id;
	gboolean success;

	parent_name = nm_platform_link_get_name (parent);
	g_return_val_if_fail (parent_name != NULL, FALSE);

	path = g_strdup_printf ("/sys/class/net/%s/create_child", parent_name);
	id = g_strdup_printf ("0x%04x", p_key);
	success = nm_platform_sysctl_set (path, id);
	g_free (id);
	g_free (path);

	if (success) {
		auto_nl_object struct rtnl_link *rtnllink = rtnl_link_alloc ();
		auto_g_free char *ifname = g_strdup_printf ("%s.%04x", parent_name, p_key);

		rtnl_link_set_name (rtnllink, ifname);
		success = refresh_object (platform, (struct nl_object *) rtnllink, FALSE, NM_PLATFORM_REASON_INTERNAL);
	}

	return success;
}

static gboolean
veth_get_properties (NMPlatform *platform, int ifindex, NMPlatformVethProperties *props)
{
	const char *ifname;
	auto_g_free struct ethtool_stats *stats = NULL;
	int peer_ifindex_stat;

	ifname = nm_platform_link_get_name (ifindex);
	if (!ifname)
		return FALSE;

	peer_ifindex_stat = ethtool_get_stringset_index (ifname, ETH_SS_STATS, "peer_ifindex");
	if (peer_ifindex_stat == -1) {
		debug ("%s: peer_ifindex ethtool stat does not exist?", ifname);
		return FALSE;
	}

	stats = g_malloc0 (sizeof (*stats) + (peer_ifindex_stat + 1) * sizeof (guint64));
	stats->cmd = ETHTOOL_GSTATS;
	stats->n_stats = peer_ifindex_stat + 1;
	if (!ethtool_get (ifname, stats))
		return FALSE;

	props->peer = stats->data[peer_ifindex_stat];
	return TRUE;
}

static gboolean
tun_get_properties (NMPlatform *platform, int ifindex, NMPlatformTunProperties *props)
{
	const char *ifname;
	char *path, *val;
	gboolean success = TRUE;

	g_return_val_if_fail (props, FALSE);

	memset (props, 0, sizeof (*props));
	props->owner = -1;
	props->group = -1;

	ifname = nm_platform_link_get_name (ifindex);
	if (!ifname || !nm_utils_iface_valid_name (ifname))
		return FALSE;

	path = g_strdup_printf ("/sys/class/net/%s/owner", ifname);
	val = nm_platform_sysctl_get (path, TRUE);
	g_free (path);
	if (val) {
		props->owner = nm_utils_ascii_str_to_int64 (val, 10, -1, G_MAXINT64, -1);
		if (errno)
			success = FALSE;
		g_free (val);
	} else
		success = FALSE;

	path = g_strdup_printf ("/sys/class/net/%s/group", ifname);
	val = nm_platform_sysctl_get (path, TRUE);
	g_free (path);
	if (val) {
		props->group = nm_utils_ascii_str_to_int64 (val, 10, -1, G_MAXINT64, -1);
		if (errno)
			success = FALSE;
		g_free (val);
	} else
		success = FALSE;

	path = g_strdup_printf ("/sys/class/net/%s/tun_flags", ifname);
	val = nm_platform_sysctl_get (path, TRUE);
	g_free (path);
	if (val) {
		gint64 flags;

		flags = nm_utils_ascii_str_to_int64 (val, 16, 0, G_MAXINT64, 0);
		if (!errno) {
#ifndef IFF_MULTI_QUEUE
			const int IFF_MULTI_QUEUE = 0x0100;
#endif
			props->mode = ((flags & TUN_TYPE_MASK) == TUN_TUN_DEV) ? "tun" : "tap";
			props->no_pi = !!(flags & IFF_NO_PI);
			props->vnet_hdr = !!(flags & IFF_VNET_HDR);
			props->multi_queue = !!(flags & IFF_MULTI_QUEUE);
		} else
			success = FALSE;
		g_free (val);
	} else
		success = FALSE;

	return success;
}

static const struct nla_policy macvlan_info_policy[IFLA_MACVLAN_MAX + 1] = {
	[IFLA_MACVLAN_MODE]  = { .type = NLA_U32 },
#ifdef IFLA_MACVLAN_FLAGS
	[IFLA_MACVLAN_FLAGS] = { .type = NLA_U16 },
#endif
};

static int
macvlan_info_data_parser (struct nlattr *info_data, gpointer parser_data)
{
	NMPlatformMacvlanProperties *props = parser_data;
	struct nlattr *tb[IFLA_MACVLAN_MAX + 1];
	int err;

	err = nla_parse_nested (tb, IFLA_MACVLAN_MAX, info_data,
	                        (struct nla_policy *) macvlan_info_policy);
	if (err < 0)
		return err;

	switch (nla_get_u32 (tb[IFLA_MACVLAN_MODE])) {
	case MACVLAN_MODE_PRIVATE:
		props->mode = "private";
		break;
	case MACVLAN_MODE_VEPA:
		props->mode = "vepa";
		break;
	case MACVLAN_MODE_BRIDGE:
		props->mode = "bridge";
		break;
	case MACVLAN_MODE_PASSTHRU:
		props->mode = "passthru";
		break;
	default:
		return -NLE_PARSE_ERR;
	}

#ifdef MACVLAN_FLAG_NOPROMISC
	props->no_promisc = !!(nla_get_u16 (tb[IFLA_MACVLAN_FLAGS]) & MACVLAN_FLAG_NOPROMISC);
#else
	props->no_promisc = FALSE;
#endif

	return 0;
}

static gboolean
macvlan_get_properties (NMPlatform *platform, int ifindex, NMPlatformMacvlanProperties *props)
{
	NMLinuxPlatformPrivate *priv = NM_LINUX_PLATFORM_GET_PRIVATE (platform);
	auto_nl_object struct rtnl_link *rtnllink = NULL;
	int err;

	rtnllink = link_get (platform, ifindex);
	if (!rtnllink)
		return FALSE;

	props->parent_ifindex = rtnl_link_get_link (rtnllink);

	err = nm_rtnl_link_parse_info_data (priv->nlh, ifindex,
	                                    macvlan_info_data_parser, props);
	return (err == 0);
}

static const struct nla_policy gre_info_policy[IFLA_GRE_MAX + 1] = {
	[IFLA_GRE_LINK]		= { .type = NLA_U32 },
	[IFLA_GRE_IFLAGS]	= { .type = NLA_U16 },
	[IFLA_GRE_OFLAGS]	= { .type = NLA_U16 },
	[IFLA_GRE_IKEY]		= { .type = NLA_U32 },
	[IFLA_GRE_OKEY]		= { .type = NLA_U32 },
	[IFLA_GRE_LOCAL]	= { .type = NLA_U32 },
	[IFLA_GRE_REMOTE]	= { .type = NLA_U32 },
	[IFLA_GRE_TTL]		= { .type = NLA_U8 },
	[IFLA_GRE_TOS]		= { .type = NLA_U8 },
	[IFLA_GRE_PMTUDISC]	= { .type = NLA_U8 },
};

static int
gre_info_data_parser (struct nlattr *info_data, gpointer parser_data)
{
	NMPlatformGreProperties *props = parser_data;
	struct nlattr *tb[IFLA_GRE_MAX + 1];
	int err;

	err = nla_parse_nested (tb, IFLA_GRE_MAX, info_data,
	                        (struct nla_policy *) gre_info_policy);
	if (err < 0)
		return err;

	props->parent_ifindex = tb[IFLA_GRE_LINK] ? nla_get_u32 (tb[IFLA_GRE_LINK]) : 0;
	props->input_flags = nla_get_u16 (tb[IFLA_GRE_IFLAGS]);
	props->output_flags = nla_get_u16 (tb[IFLA_GRE_OFLAGS]);
	props->input_key = (props->input_flags & GRE_KEY) ? nla_get_u32 (tb[IFLA_GRE_IKEY]) : 0;
	props->output_key = (props->output_flags & GRE_KEY) ? nla_get_u32 (tb[IFLA_GRE_OKEY]) : 0;
	props->local = nla_get_u32 (tb[IFLA_GRE_LOCAL]);
	props->remote = nla_get_u32 (tb[IFLA_GRE_REMOTE]);
	props->tos = nla_get_u8 (tb[IFLA_GRE_TOS]);
	props->ttl = nla_get_u8 (tb[IFLA_GRE_TTL]);
	props->path_mtu_discovery = nla_get_u8 (tb[IFLA_GRE_PMTUDISC]);

	return 0;
}

static gboolean
gre_get_properties (NMPlatform *platform, int ifindex, NMPlatformGreProperties *props)
{
	NMLinuxPlatformPrivate *priv = NM_LINUX_PLATFORM_GET_PRIVATE (platform);
	int err;

	err = nm_rtnl_link_parse_info_data (priv->nlh, ifindex,
	                                    gre_info_data_parser, props);
	return (err == 0);
}

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

static int
ip_address_mark_all (NMPlatform *platform, int family, int ifindex)
{
	NMLinuxPlatformPrivate *priv = NM_LINUX_PLATFORM_GET_PRIVATE (platform);
	struct nl_object *object;
	int count = 0;

	for (object = nl_cache_get_first (priv->address_cache); object; object = nl_cache_get_next (object)) {
		nl_object_unmark (object);
		if (rtnl_addr_get_family ((struct rtnl_addr *) object) != family)
			continue;
		if (rtnl_addr_get_ifindex ((struct rtnl_addr *) object) != ifindex)
			continue;
		nl_object_mark (object);
		count++;
	}

	return count;
}

static GArray *
ip4_address_get_all (NMPlatform *platform, int ifindex)
{
	NMLinuxPlatformPrivate *priv = NM_LINUX_PLATFORM_GET_PRIVATE (platform);
	GArray *addresses;
	NMPlatformIP4Address address;
	struct nl_object *object;
	int count;

	count = ip_address_mark_all (platform, AF_INET, ifindex);
	addresses = g_array_sized_new (TRUE, TRUE, sizeof (NMPlatformIP4Address), count);

	for (object = nl_cache_get_first (priv->address_cache); object; object = nl_cache_get_next (object)) {
		if (nl_object_is_marked (object)) {
			init_ip4_address (&address, (struct rtnl_addr *) object);
			address.source = NM_PLATFORM_SOURCE_KERNEL;
			g_array_append_val (addresses, address);
			nl_object_unmark (object);
		}
	}

	return addresses;
}

static GArray *
ip6_address_get_all (NMPlatform *platform, int ifindex)
{
	NMLinuxPlatformPrivate *priv = NM_LINUX_PLATFORM_GET_PRIVATE (platform);
	GArray *addresses;
	NMPlatformIP6Address address;
	struct nl_object *object;
	int count;

	count = ip_address_mark_all (platform, AF_INET6, ifindex);
	addresses = g_array_sized_new (TRUE, TRUE, sizeof (NMPlatformIP6Address), count);

	for (object = nl_cache_get_first (priv->address_cache); object; object = nl_cache_get_next (object)) {
		if (nl_object_is_marked (object)) {
			init_ip6_address (&address, (struct rtnl_addr *) object);
			address.source = NM_PLATFORM_SOURCE_KERNEL;
			g_array_append_val (addresses, address);
			nl_object_unmark (object);
		}
	}

	return addresses;
}

static void
addr4_to_broadcast (struct in_addr *dst, const struct in_addr *src, guint8 plen)
{
	guint nbytes = plen / 8;
	guint nbits = plen % 8;

	g_return_if_fail (plen <= 32);
	g_assert (src);
	g_assert (dst);

	if (plen >= 32)
		*dst = *src;
	else {
		dst->s_addr = 0xFFFFFFFF;
		memcpy (dst, src, nbytes);
		((guint8 *) dst)[nbytes] = (((const guint8 *) src)[nbytes] | (0xFF >> nbits));
	}
}

static struct nl_object *
build_rtnl_addr (int family,
                 int ifindex,
                 gconstpointer addr,
                 gconstpointer peer_addr,
                 int plen,
                 guint32 lifetime,
                 guint32 preferred,
                 guint flags)
{
	struct rtnl_addr *rtnladdr = rtnl_addr_alloc ();
	int addrlen = family == AF_INET ? sizeof (in_addr_t) : sizeof (struct in6_addr);
	auto_nl_addr struct nl_addr *nladdr = nl_addr_build (family, addr, addrlen);
	int nle;

	g_assert (rtnladdr && nladdr);

	/* IP address */
	rtnl_addr_set_ifindex (rtnladdr, ifindex);
	nle = rtnl_addr_set_local (rtnladdr, nladdr);
	g_assert (!nle);

	/* IPv4 Broadcast address */
	if (family == AF_INET) {
		struct in_addr bcast;
		auto_nl_addr struct nl_addr *bcaddr = NULL;

		addr4_to_broadcast (&bcast, addr, plen);
		bcaddr = nl_addr_build (family, &bcast, addrlen);
		g_assert (bcaddr);
		rtnl_addr_set_broadcast (rtnladdr, bcaddr);
	}

	/* Peer/point-to-point address */
	if (peer_addr) {
		auto_nl_addr struct nl_addr *nlpeer = nl_addr_build (family, peer_addr, addrlen);

		nle = rtnl_addr_set_peer (rtnladdr, nlpeer);
		/* IPv6 doesn't support peer addresses yet */
		g_assert (!nle || (nle == -NLE_AF_NOSUPPORT));
	}

	rtnl_addr_set_prefixlen (rtnladdr, plen);
	if (lifetime) {
		rtnl_addr_set_valid_lifetime (rtnladdr, lifetime);
		rtnl_addr_set_preferred_lifetime (rtnladdr, preferred);
	}
	if (flags)
		rtnl_addr_set_flags (rtnladdr, flags);

	return (struct nl_object *) rtnladdr;
}

static gboolean
ip4_address_add (NMPlatform *platform,
                 int ifindex,
                 in_addr_t addr,
                 in_addr_t peer_addr,
                 int plen,
                 guint32 lifetime,
                 guint32 preferred)
{
	return add_object (platform, build_rtnl_addr (AF_INET, ifindex, &addr,
	                                              peer_addr ? &peer_addr : NULL,
	                                              plen, lifetime, preferred, 0));
}

static gboolean
ip6_address_add (NMPlatform *platform,
                 int ifindex,
                 struct in6_addr addr,
                 struct in6_addr peer_addr,
                 int plen,
                 guint32 lifetime,
                 guint32 preferred,
                 guint flags)
{
	return add_object (platform, build_rtnl_addr (AF_INET6, ifindex, &addr,
	                                              IN6_IS_ADDR_UNSPECIFIED (&peer_addr) ? NULL : &peer_addr,
	                                              plen, lifetime, preferred, flags));
}

static gboolean
ip4_address_delete (NMPlatform *platform, int ifindex, in_addr_t addr, int plen)
{
	return delete_object (platform, build_rtnl_addr (AF_INET, ifindex, &addr, NULL, plen, 0, 0, 0));
}

static gboolean
ip6_address_delete (NMPlatform *platform, int ifindex, struct in6_addr addr, int plen)
{
	return delete_object (platform, build_rtnl_addr (AF_INET6, ifindex, &addr, NULL, plen, 0, 0, 0));
}

static gboolean
ip_address_exists (NMPlatform *platform, int family, int ifindex, gconstpointer addr, int plen)
{
	auto_nl_object struct nl_object *object = build_rtnl_addr (family, ifindex, addr, NULL, plen, 0, 0, 0);
	auto_nl_object struct nl_object *cached_object = nl_cache_search (choose_cache (platform, object), object);

	return !!cached_object;
}

static gboolean
ip4_address_exists (NMPlatform *platform, int ifindex, in_addr_t addr, int plen)
{
	return ip_address_exists (platform, AF_INET, ifindex, &addr, plen);
}

static gboolean
ip6_address_exists (NMPlatform *platform, int ifindex, struct in6_addr addr, int plen)
{
	return ip_address_exists (platform, AF_INET6, ifindex, &addr, plen);
}

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

static int
ip_route_mark_all (NMPlatform *platform, int family, int ifindex)
{
	NMLinuxPlatformPrivate *priv = NM_LINUX_PLATFORM_GET_PRIVATE (platform);
	struct nl_object *object;
	int count = 0;

	for (object = nl_cache_get_first (priv->route_cache); object; object = nl_cache_get_next (object)) {
		struct rtnl_route *rtnlroute = (struct rtnl_route *) object;
		struct rtnl_nexthop *nexthop;

		nl_object_unmark (object);
		if (rtnl_route_get_type (rtnlroute) != RTN_UNICAST)
			continue;
		if (rtnl_route_get_table (rtnlroute) != RT_TABLE_MAIN)
			continue;
		if (rtnl_route_get_protocol (rtnlroute) == RTPROT_KERNEL)
			continue;
		if (rtnl_route_get_family (rtnlroute) != family)
			continue;
		if (rtnl_route_get_nnexthops (rtnlroute) != 1)
			continue;
		nexthop = rtnl_route_nexthop_n (rtnlroute, 0);
		if (rtnl_route_nh_get_ifindex (nexthop) != ifindex)
			continue;
		nl_object_mark (object);
		count++;
	}

	return count;
}

static GArray *
ip4_route_get_all (NMPlatform *platform, int ifindex, gboolean include_default)
{
	NMLinuxPlatformPrivate *priv = NM_LINUX_PLATFORM_GET_PRIVATE (platform);
	GArray *routes;
	NMPlatformIP4Route route;
	struct nl_object *object;
	int count = 0;

	count = ip_route_mark_all (platform, AF_INET, ifindex);
	routes = g_array_sized_new (TRUE, TRUE, sizeof (NMPlatformIP4Route), count);

	for (object = nl_cache_get_first (priv->route_cache); object; object = nl_cache_get_next (object)) {
		if (nl_object_is_marked (object)) {
			if (init_ip4_route (&route, (struct rtnl_route *) object)) {
				route.source = NM_PLATFORM_SOURCE_KERNEL;
				if (route.plen != 0 || include_default)
					g_array_append_val (routes, route);
			}
			nl_object_unmark (object);
		}
	}

	return routes;
}

static GArray *
ip6_route_get_all (NMPlatform *platform, int ifindex, gboolean include_default)
{
	NMLinuxPlatformPrivate *priv = NM_LINUX_PLATFORM_GET_PRIVATE (platform);
	GArray *routes;
	NMPlatformIP6Route route;
	struct nl_object *object;
	int count;

	count = ip_route_mark_all (platform, AF_INET6, ifindex);
	routes = g_array_sized_new (TRUE, TRUE, sizeof (NMPlatformIP6Route), count);

	for (object = nl_cache_get_first (priv->route_cache); object; object = nl_cache_get_next (object)) {
		if (nl_object_is_marked (object)) {
			if (init_ip6_route (&route, (struct rtnl_route *) object)) {
				route.source = NM_PLATFORM_SOURCE_KERNEL;
				if (route.plen != 0 || include_default)
					g_array_append_val (routes, route);
			}
			nl_object_unmark (object);
		}
	}

	return routes;
}

static struct nl_object *
build_rtnl_route (int family, int ifindex, gconstpointer network, int plen, gconstpointer gateway, int metric, int mss)
{
	struct rtnl_route *rtnlroute = rtnl_route_alloc ();
	struct rtnl_nexthop *nexthop = rtnl_route_nh_alloc ();
	int addrlen = (family == AF_INET) ? sizeof (in_addr_t) : sizeof (struct in6_addr);
	/* Workaround a libnl bug by using zero destination address length for default routes */
	auto_nl_addr struct nl_addr *dst = nl_addr_build (family, network, plen ? addrlen : 0);
	auto_nl_addr struct nl_addr *gw = gateway ? nl_addr_build (family, gateway, addrlen) : NULL;

	g_assert (rtnlroute && dst && nexthop);

	nl_addr_set_prefixlen (dst, plen);

	rtnl_route_set_table (rtnlroute, RT_TABLE_MAIN);
	rtnl_route_set_tos (rtnlroute, 0);
	rtnl_route_set_dst (rtnlroute, dst);
	rtnl_route_set_priority (rtnlroute, metric);

	rtnl_route_nh_set_ifindex (nexthop, ifindex);
	if (gw && !nl_addr_iszero (gw))
		rtnl_route_nh_set_gateway (nexthop, gw);
	rtnl_route_add_nexthop (rtnlroute, nexthop);

	if (mss > 0)
		rtnl_route_set_metric (rtnlroute, RTAX_ADVMSS, mss);

	return (struct nl_object *) rtnlroute;
}

static gboolean
ip4_route_add (NMPlatform *platform, int ifindex, in_addr_t network, int plen, in_addr_t gateway, int metric, int mss)
{
	return add_object (platform, build_rtnl_route (AF_INET, ifindex, &network, plen, &gateway, metric, mss));
}

static gboolean
ip6_route_add (NMPlatform *platform, int ifindex, struct in6_addr network, int plen, struct in6_addr gateway, int metric, int mss)
{
	return add_object (platform, build_rtnl_route (AF_INET6, ifindex, &network, plen, &gateway, metric, mss));
}

static gboolean
ip4_route_delete (NMPlatform *platform, int ifindex, in_addr_t network, int plen, int metric)
{
	in_addr_t gateway = 0;

	return delete_object (platform, build_rtnl_route (AF_INET, ifindex, &network, plen, &gateway, metric, 0));
}

static gboolean
ip6_route_delete (NMPlatform *platform, int ifindex, struct in6_addr network, int plen, int metric)
{
	struct in6_addr gateway = in6addr_any;

	return delete_object (platform, build_rtnl_route (AF_INET6, ifindex, &network, plen, &gateway, metric, 0));
}

static gboolean
ip_route_exists (NMPlatform *platform, int family, int ifindex, gpointer network, int plen, int metric)
{
	auto_nl_object struct nl_object *object = build_rtnl_route (
			family, ifindex, network, plen, INADDR_ANY, metric, 0);
	auto_nl_object struct nl_object *cached_object = nl_cache_search (
			choose_cache (platform, object), object);

	return !!cached_object;
}

static gboolean
ip4_route_exists (NMPlatform *platform, int ifindex, in_addr_t network, int plen, int metric)
{
	return ip_route_exists (platform, AF_INET, ifindex, &network, plen, metric);
}

static gboolean
ip6_route_exists (NMPlatform *platform, int ifindex, struct in6_addr network, int plen, int metric)
{
	return ip_route_exists (platform, AF_INET6, ifindex, &network, plen, metric);
}

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

#define EVENT_CONDITIONS      ((GIOCondition) (G_IO_IN | G_IO_PRI))
#define ERROR_CONDITIONS      ((GIOCondition) (G_IO_ERR | G_IO_NVAL))
#define DISCONNECT_CONDITIONS ((GIOCondition) (G_IO_HUP))

static int
verify_source (struct nl_msg *msg, gpointer user_data)
{
	struct ucred *creds = nlmsg_get_creds (msg);

	if (!creds || creds->pid || creds->uid || creds->gid) {
		if (creds)
			warning ("netlink: received non-kernel message (pid %d uid %d gid %d)",
					creds->pid, creds->uid, creds->gid);
		else
			warning ("netlink: received message without credentials");
		return NL_STOP;
	}

	return NL_OK;
}

static gboolean
event_handler (GIOChannel *channel,
		GIOCondition io_condition,
		gpointer user_data)
{
	NMLinuxPlatformPrivate *priv = NM_LINUX_PLATFORM_GET_PRIVATE (user_data);
	int nle;

	nle = nl_recvmsgs_default (priv->nlh_event);
	if (nle)
		error ("Failed to retrieve incoming events: %s", nl_geterror (nle));
	return TRUE;
}

static struct nl_sock *
setup_socket (gboolean event, gpointer user_data)
{
	struct nl_sock *sock;
	int nle;

	sock = nl_socket_alloc ();
	g_return_val_if_fail (sock, NULL);

	/* Only ever accept messages from kernel */
	nle = nl_socket_modify_cb (sock, NL_CB_MSG_IN, NL_CB_CUSTOM, verify_source, user_data);
	g_assert (!nle);

	/* Dispatch event messages (event socket only) */
	if (event) {
		nl_socket_modify_cb (sock, NL_CB_VALID, NL_CB_CUSTOM, event_notification, user_data);
		nl_socket_disable_seq_check (sock);
	}

	nle = nl_connect (sock, NETLINK_ROUTE);
	g_assert (!nle);
	nle = nl_socket_set_passcred (sock, 1);
	g_assert (!nle);

	return sock;
}

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

static void
udev_device_added (NMPlatform *platform,
                   GUdevDevice *udev_device)
{
	NMLinuxPlatformPrivate *priv = NM_LINUX_PLATFORM_GET_PRIVATE (platform);
	auto_nl_object struct rtnl_link *rtnllink = NULL;
	const char *ifname;
	int ifindex;

	ifname = g_udev_device_get_name (udev_device);
	if (!ifname) {
		debug ("failed to get device's interface");
		return;
	}

	if (g_udev_device_get_property (udev_device, "IFINDEX"))
		ifindex = g_udev_device_get_property_as_int (udev_device, "IFINDEX");
	else {
		warning ("(%s): failed to get device's ifindex", ifname);
		return;
	}

	if (!g_udev_device_get_sysfs_path (udev_device)) {
		debug ("(%s): couldn't determine device path; ignoring...", ifname);
		return;
	}

	g_hash_table_insert (priv->udev_devices, GINT_TO_POINTER (ifindex),
	                     g_object_ref (udev_device));

	/* Don't announce devices that have not yet been discovered via Netlink. */
	rtnllink = rtnl_link_get (priv->link_cache, ifindex);
	if (!rtnllink) {
		debug ("%s: not found in link cache, ignoring...", ifname);
		return;
	}

	announce_object (platform, (struct nl_object *) rtnllink, ADDED, NM_PLATFORM_REASON_EXTERNAL);
}

static void
udev_device_removed (NMPlatform *platform,
                     GUdevDevice *udev_device)
{
	NMLinuxPlatformPrivate *priv = NM_LINUX_PLATFORM_GET_PRIVATE (platform);
	int ifindex = 0;

	if (g_udev_device_get_property (udev_device, "IFINDEX")) {
		ifindex = g_udev_device_get_property_as_int (udev_device, "IFINDEX");
		g_hash_table_remove (priv->udev_devices, GINT_TO_POINTER (ifindex));
	} else {
		GHashTableIter iter;
		gpointer key, value;

		/* This should not happen, but just to be sure.
		 * If we can't get IFINDEX, go through the devices and
		 * compare the pointers.
		 */
		g_hash_table_iter_init (&iter, priv->udev_devices);
		while (g_hash_table_iter_next (&iter, &key, &value)) {
			if ((GUdevDevice *)value == udev_device) {
				ifindex = GPOINTER_TO_INT (key);
				g_hash_table_iter_remove (&iter);
				break;
			}
		}
	}

	/* Announce device removal if it's still in the Netlink cache. */
	if (ifindex) {
		auto_nl_object struct rtnl_link *device = rtnl_link_get (priv->link_cache, ifindex);

		if (device)
			announce_object (platform, (struct nl_object *) device, REMOVED, NM_PLATFORM_REASON_EXTERNAL);
	}
}

static void
udev_device_changed (NMPlatform *platform,
                     GUdevDevice *udev_device)
{
	NMLinuxPlatformPrivate *priv = NM_LINUX_PLATFORM_GET_PRIVATE (platform);
	const char *ifname;
	int ifindex;

	ifname = g_udev_device_get_name (udev_device);
	if (!ifname) {
		debug ("failed to get device's interface");
		return;
	}

	if (g_udev_device_get_property (udev_device, "IFINDEX"))
		ifindex = g_udev_device_get_property_as_int (udev_device, "IFINDEX");
	else {
		warning ("(%s): failed to get device's ifindex", ifname);
		return;
	}

//	if (!g_hash_table_lookup (priv->udev_devices, GINT_TO_POINTER (info->ifindex))) {
//		debug ("(%s): failed to get device's ifindex", ifname, ifindex);
//		return;
//	}

	/* Replace the old udev device with the new one */
	g_hash_table_insert (priv->udev_devices, GINT_TO_POINTER (ifindex),
	                     g_object_ref (udev_device));

	/* Announce device 'move'. */
	if (ifindex) {
		auto_nl_object struct rtnl_link *rtnllink = rtnl_link_get (priv->link_cache, ifindex);

		if (rtnllink)
			announce_object (platform, (struct nl_object *) rtnllink, CHANGED, NM_PLATFORM_REASON_EXTERNAL);
	}
}

static void
handle_udev_event (GUdevClient *client,
                   const char *action,
                   GUdevDevice *udev_device,
                   gpointer user_data)
{
	NMPlatform *platform = NM_PLATFORM (user_data);
	const char *subsys;
	const char *ifindex;
	guint64 seqnum;

	g_return_if_fail (action != NULL);

	/* A bit paranoid */
	subsys = g_udev_device_get_subsystem (udev_device);
	g_return_if_fail (!g_strcmp0 (subsys, "net"));

	ifindex = g_udev_device_get_property (udev_device, "IFINDEX");
	seqnum = g_udev_device_get_seqnum (udev_device);
	debug ("UDEV event: action '%s' subsys '%s' device '%s' (%s); seqnum=%" G_GUINT64_FORMAT,
	       action, subsys, g_udev_device_get_name (udev_device),
	       ifindex ? ifindex : "unknown", seqnum);

	if (!strcmp (action, "add"))
		udev_device_added (platform, udev_device);
	if (!strcmp (action, "remove"))
		udev_device_removed (platform, udev_device);
	if (!strcmp (action, "move"))
		udev_device_changed (platform, udev_device);
}

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

static void
nm_linux_platform_init (NMLinuxPlatform *platform)
{
}

static gboolean
setup (NMPlatform *platform)
{
	NMLinuxPlatformPrivate *priv = NM_LINUX_PLATFORM_GET_PRIVATE (platform);
	const char *udev_subsys[] = { "net", NULL };
	GUdevEnumerator *enumerator;
	GList *devices, *iter;
	int channel_flags;
	gboolean status;
	int nle;

	/* Initialize netlink socket for requests */
	priv->nlh = setup_socket (FALSE, platform);
	g_assert (priv->nlh);
	debug ("Netlink socket for requests established: %d", nl_socket_get_local_port (priv->nlh));

	/* Initialize netlink socket for events */
	priv->nlh_event = setup_socket (TRUE, platform);
	g_assert (priv->nlh_event);
	/* The default buffer size wasn't enough for the testsuites. It might just
	 * as well happen with NetworkManager itself. For now let's hope 128KB is
	 * good enough.
	 */
	nle = nl_socket_set_buffer_size (priv->nlh_event, 131072, 0);
	g_assert (!nle);
	nle = nl_socket_add_memberships (priv->nlh_event,
	                                 RTNLGRP_LINK,
	                                 RTNLGRP_IPV4_IFADDR, RTNLGRP_IPV6_IFADDR,
	                                 RTNLGRP_IPV4_ROUTE,  RTNLGRP_IPV6_ROUTE,
	                                 0);
	g_assert (!nle);
	debug ("Netlink socket for events established: %d", nl_socket_get_local_port (priv->nlh_event));

	priv->event_channel = g_io_channel_unix_new (nl_socket_get_fd (priv->nlh_event));
	g_io_channel_set_encoding (priv->event_channel, NULL, NULL);
	g_io_channel_set_close_on_unref (priv->event_channel, TRUE);

	channel_flags = g_io_channel_get_flags (priv->event_channel);
	status = g_io_channel_set_flags (priv->event_channel,
		channel_flags | G_IO_FLAG_NONBLOCK, NULL);
	g_assert (status);
	priv->event_id = g_io_add_watch (priv->event_channel,
		(EVENT_CONDITIONS | ERROR_CONDITIONS | DISCONNECT_CONDITIONS),
		event_handler, platform);

	/* Allocate netlink caches */
	rtnl_link_alloc_cache (priv->nlh, AF_UNSPEC, &priv->link_cache);
	rtnl_addr_alloc_cache (priv->nlh, &priv->address_cache);
	rtnl_route_alloc_cache (priv->nlh, AF_UNSPEC, 0, &priv->route_cache);
	g_assert (priv->link_cache && priv->address_cache && priv->route_cache);

	/* Set up udev monitoring */
	priv->udev_client = g_udev_client_new (udev_subsys);
	g_signal_connect (priv->udev_client, "uevent", G_CALLBACK (handle_udev_event), platform);
	priv->udev_devices = g_hash_table_new_full (NULL, NULL, NULL, g_object_unref);

	/* And read initial device list */
	enumerator = g_udev_enumerator_new (priv->udev_client);
	g_udev_enumerator_add_match_subsystem (enumerator, "net");
	g_udev_enumerator_add_match_is_initialized (enumerator);

	devices = g_udev_enumerator_execute (enumerator);
	for (iter = devices; iter; iter = g_list_next (iter)) {
		udev_device_added (platform, G_UDEV_DEVICE (iter->data));
		g_object_unref (G_UDEV_DEVICE (iter->data));
	}
	g_list_free (devices);
	g_object_unref (enumerator);

	/* request all IPv6 addresses (hopeing that there is at least one), to check for
	 * the IFA_FLAGS attribute. */
	nle = nl_rtgen_request (priv->nlh_event, RTM_GETADDR, AF_INET6, NLM_F_DUMP);
	if (nle < 0)
		nm_log_warn (LOGD_PLATFORM, "Netlink error: requesting RTM_GETADDR failed with %s", nl_geterror (nle));

	return TRUE;
}

static void
nm_linux_platform_finalize (GObject *object)
{
	NMLinuxPlatformPrivate *priv = NM_LINUX_PLATFORM_GET_PRIVATE (object);

	/* Free netlink resources */
	g_source_remove (priv->event_id);
	g_io_channel_unref (priv->event_channel);
	nl_socket_free (priv->nlh);
	nl_socket_free (priv->nlh_event);
	nl_cache_free (priv->link_cache);
	nl_cache_free (priv->address_cache);
	nl_cache_free (priv->route_cache);

	g_object_unref (priv->udev_client);
	g_hash_table_unref (priv->udev_devices);

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

#define OVERRIDE(function) platform_class->function = function

static void
nm_linux_platform_class_init (NMLinuxPlatformClass *klass)
{
	GObjectClass *object_class = G_OBJECT_CLASS (klass);
	NMPlatformClass *platform_class = NM_PLATFORM_CLASS (klass);

	g_type_class_add_private (klass, sizeof (NMLinuxPlatformPrivate));

	/* virtual methods */
	object_class->finalize = nm_linux_platform_finalize;

	platform_class->setup = setup;

	platform_class->sysctl_set = sysctl_set;
	platform_class->sysctl_get = sysctl_get;

	platform_class->link_get_all = link_get_all;
	platform_class->link_add = link_add;
	platform_class->link_delete = link_delete;
	platform_class->link_get_ifindex = link_get_ifindex;
	platform_class->link_get_name = link_get_name;
	platform_class->link_get_type = link_get_type;
	platform_class->link_get_type_name = link_get_type_name;

	platform_class->link_set_up = link_set_up;
	platform_class->link_set_down = link_set_down;
	platform_class->link_set_arp = link_set_arp;
	platform_class->link_set_noarp = link_set_noarp;
	platform_class->link_is_up = link_is_up;
	platform_class->link_is_connected = link_is_connected;
	platform_class->link_uses_arp = link_uses_arp;

	platform_class->link_get_address = link_get_address;
	platform_class->link_set_address = link_set_address;
	platform_class->link_get_mtu = link_get_mtu;
	platform_class->link_set_mtu = link_set_mtu;

	platform_class->link_get_physical_port_id = link_get_physical_port_id;

	platform_class->link_supports_carrier_detect = link_supports_carrier_detect;
	platform_class->link_supports_vlans = link_supports_vlans;

	platform_class->link_enslave = link_enslave;
	platform_class->link_release = link_release;
	platform_class->link_get_master = link_get_master;
	platform_class->master_set_option = master_set_option;
	platform_class->master_get_option = master_get_option;
	platform_class->slave_set_option = slave_set_option;
	platform_class->slave_get_option = slave_get_option;

	platform_class->vlan_add = vlan_add;
	platform_class->vlan_get_info = vlan_get_info;
	platform_class->vlan_set_ingress_map = vlan_set_ingress_map;
	platform_class->vlan_set_egress_map = vlan_set_egress_map;

	platform_class->infiniband_partition_add = infiniband_partition_add;

	platform_class->veth_get_properties = veth_get_properties;
	platform_class->tun_get_properties = tun_get_properties;
	platform_class->macvlan_get_properties = macvlan_get_properties;
	platform_class->gre_get_properties = gre_get_properties;

	platform_class->ip4_address_get_all = ip4_address_get_all;
	platform_class->ip6_address_get_all = ip6_address_get_all;
	platform_class->ip4_address_add = ip4_address_add;
	platform_class->ip6_address_add = ip6_address_add;
	platform_class->ip4_address_delete = ip4_address_delete;
	platform_class->ip6_address_delete = ip6_address_delete;
	platform_class->ip4_address_exists = ip4_address_exists;
	platform_class->ip6_address_exists = ip6_address_exists;

	platform_class->ip4_route_get_all = ip4_route_get_all;
	platform_class->ip6_route_get_all = ip6_route_get_all;
	platform_class->ip4_route_add = ip4_route_add;
	platform_class->ip6_route_add = ip6_route_add;
	platform_class->ip4_route_delete = ip4_route_delete;
	platform_class->ip6_route_delete = ip6_route_delete;
	platform_class->ip4_route_exists = ip4_route_exists;
	platform_class->ip6_route_exists = ip6_route_exists;

	platform_class->check_support_kernel_extended_ifa_flags = check_support_kernel_extended_ifa_flags;
}