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
|
/*
* Interface looking up by netlink.
* Copyright (C) 1998 Kunihiro Ishiguro
*
* This file is part of GNU Zebra.
*
* GNU Zebra 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.
*
* GNU Zebra 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; see the file COPYING; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <zebra.h>
#ifdef GNU_LINUX
/* The following definition is to workaround an issue in the Linux kernel
* header files with redefinition of 'struct in6_addr' in both
* netinet/in.h and linux/in6.h.
* Reference - https://sourceware.org/ml/libc-alpha/2013-01/msg00599.html
*/
#define _LINUX_IN6_H
#define _LINUX_IF_H
#define _LINUX_IP_H
#include <netinet/if_ether.h>
#include <linux/if_bridge.h>
#include <linux/if_link.h>
#include <linux/if_tunnel.h>
#include <net/if_arp.h>
#include <linux/sockios.h>
#include <linux/ethtool.h>
#include "linklist.h"
#include "if.h"
#include "log.h"
#include "prefix.h"
#include "connected.h"
#include "table.h"
#include "memory.h"
#include "rib.h"
#include "thread.h"
#include "privs.h"
#include "nexthop.h"
#include "vrf.h"
#include "vrf_int.h"
#include "mpls.h"
#include "lib_errors.h"
#include "vty.h"
#include "zebra/zserv.h"
#include "zebra/zebra_ns.h"
#include "zebra/zebra_vrf.h"
#include "zebra/rt.h"
#include "zebra/redistribute.h"
#include "zebra/interface.h"
#include "zebra/debug.h"
#include "zebra/rtadv.h"
#include "zebra/zebra_ptm.h"
#include "zebra/zebra_mpls.h"
#include "zebra/kernel_netlink.h"
#include "zebra/rt_netlink.h"
#include "zebra/if_netlink.h"
#include "zebra/zebra_errors.h"
#include "zebra/zebra_vxlan.h"
#include "zebra/zebra_evpn_mh.h"
#include "zebra/zebra_l2.h"
#include "zebra/netconf_netlink.h"
#include "zebra/zebra_trace.h"
extern struct zebra_privs_t zserv_privs;
uint8_t frr_protodown_r_bit = FRR_PROTODOWN_REASON_DEFAULT_BIT;
/* Note: on netlink systems, there should be a 1-to-1 mapping between interface
names and ifindex values. */
static void set_ifindex(struct interface *ifp, ifindex_t ifi_index,
struct zebra_ns *zns)
{
struct interface *oifp;
if (((oifp = if_lookup_by_index_per_ns(zns, ifi_index)) != NULL)
&& (oifp != ifp)) {
if (ifi_index == IFINDEX_INTERNAL)
flog_err(
EC_LIB_INTERFACE,
"Netlink is setting interface %s ifindex to reserved internal value %u",
ifp->name, ifi_index);
else {
if (IS_ZEBRA_DEBUG_KERNEL)
zlog_debug(
"interface index %d was renamed from %s to %s",
ifi_index, oifp->name, ifp->name);
if (if_is_up(oifp))
flog_err(
EC_LIB_INTERFACE,
"interface rename detected on up interface: index %d was renamed from %s to %s, results are uncertain!",
ifi_index, oifp->name, ifp->name);
if_delete_update(&oifp);
}
}
if_set_index(ifp, ifi_index);
}
/* Utility function to parse hardware link-layer address and update ifp */
static void netlink_interface_update_hw_addr(struct rtattr **tb,
struct interface *ifp)
{
int i;
if (tb[IFLA_ADDRESS]) {
int hw_addr_len;
hw_addr_len = RTA_PAYLOAD(tb[IFLA_ADDRESS]);
if (hw_addr_len > INTERFACE_HWADDR_MAX)
zlog_debug("Hardware address is too large: %d",
hw_addr_len);
else {
ifp->hw_addr_len = hw_addr_len;
memcpy(ifp->hw_addr, RTA_DATA(tb[IFLA_ADDRESS]),
hw_addr_len);
for (i = 0; i < hw_addr_len; i++)
if (ifp->hw_addr[i] != 0)
break;
if (i == hw_addr_len)
ifp->hw_addr_len = 0;
else
ifp->hw_addr_len = hw_addr_len;
}
}
}
static enum zebra_link_type netlink_to_zebra_link_type(unsigned int hwt)
{
switch (hwt) {
case ARPHRD_ETHER:
return ZEBRA_LLT_ETHER;
case ARPHRD_EETHER:
return ZEBRA_LLT_EETHER;
case ARPHRD_AX25:
return ZEBRA_LLT_AX25;
case ARPHRD_PRONET:
return ZEBRA_LLT_PRONET;
case ARPHRD_IEEE802:
return ZEBRA_LLT_IEEE802;
case ARPHRD_ARCNET:
return ZEBRA_LLT_ARCNET;
case ARPHRD_APPLETLK:
return ZEBRA_LLT_APPLETLK;
case ARPHRD_DLCI:
return ZEBRA_LLT_DLCI;
case ARPHRD_ATM:
return ZEBRA_LLT_ATM;
case ARPHRD_METRICOM:
return ZEBRA_LLT_METRICOM;
case ARPHRD_IEEE1394:
return ZEBRA_LLT_IEEE1394;
case ARPHRD_EUI64:
return ZEBRA_LLT_EUI64;
case ARPHRD_INFINIBAND:
return ZEBRA_LLT_INFINIBAND;
case ARPHRD_SLIP:
return ZEBRA_LLT_SLIP;
case ARPHRD_CSLIP:
return ZEBRA_LLT_CSLIP;
case ARPHRD_SLIP6:
return ZEBRA_LLT_SLIP6;
case ARPHRD_CSLIP6:
return ZEBRA_LLT_CSLIP6;
case ARPHRD_RSRVD:
return ZEBRA_LLT_RSRVD;
case ARPHRD_ADAPT:
return ZEBRA_LLT_ADAPT;
case ARPHRD_ROSE:
return ZEBRA_LLT_ROSE;
case ARPHRD_X25:
return ZEBRA_LLT_X25;
case ARPHRD_PPP:
return ZEBRA_LLT_PPP;
case ARPHRD_CISCO:
return ZEBRA_LLT_CHDLC;
case ARPHRD_LAPB:
return ZEBRA_LLT_LAPB;
case ARPHRD_RAWHDLC:
return ZEBRA_LLT_RAWHDLC;
case ARPHRD_TUNNEL:
return ZEBRA_LLT_IPIP;
case ARPHRD_TUNNEL6:
return ZEBRA_LLT_IPIP6;
case ARPHRD_FRAD:
return ZEBRA_LLT_FRAD;
case ARPHRD_SKIP:
return ZEBRA_LLT_SKIP;
case ARPHRD_LOOPBACK:
return ZEBRA_LLT_LOOPBACK;
case ARPHRD_LOCALTLK:
return ZEBRA_LLT_LOCALTLK;
case ARPHRD_FDDI:
return ZEBRA_LLT_FDDI;
case ARPHRD_SIT:
return ZEBRA_LLT_SIT;
case ARPHRD_IPDDP:
return ZEBRA_LLT_IPDDP;
case ARPHRD_IPGRE:
return ZEBRA_LLT_IPGRE;
case ARPHRD_PIMREG:
return ZEBRA_LLT_PIMREG;
case ARPHRD_HIPPI:
return ZEBRA_LLT_HIPPI;
case ARPHRD_ECONET:
return ZEBRA_LLT_ECONET;
case ARPHRD_IRDA:
return ZEBRA_LLT_IRDA;
case ARPHRD_FCPP:
return ZEBRA_LLT_FCPP;
case ARPHRD_FCAL:
return ZEBRA_LLT_FCAL;
case ARPHRD_FCPL:
return ZEBRA_LLT_FCPL;
case ARPHRD_FCFABRIC:
return ZEBRA_LLT_FCFABRIC;
case ARPHRD_IEEE802_TR:
return ZEBRA_LLT_IEEE802_TR;
case ARPHRD_IEEE80211:
return ZEBRA_LLT_IEEE80211;
#ifdef ARPHRD_IEEE802154
case ARPHRD_IEEE802154:
return ZEBRA_LLT_IEEE802154;
#endif
#ifdef ARPHRD_IP6GRE
case ARPHRD_IP6GRE:
return ZEBRA_LLT_IP6GRE;
#endif
#ifdef ARPHRD_IEEE802154_PHY
case ARPHRD_IEEE802154_PHY:
return ZEBRA_LLT_IEEE802154_PHY;
#endif
default:
return ZEBRA_LLT_UNKNOWN;
}
}
static inline void zebra_if_set_ziftype(struct interface *ifp,
enum zebra_iftype zif_type,
enum zebra_slave_iftype zif_slave_type)
{
struct zebra_if *zif;
zif = (struct zebra_if *)ifp->info;
zif->zif_slave_type = zif_slave_type;
if (zif->zif_type != zif_type) {
zif->zif_type = zif_type;
/* If the if_type has been set to bond initialize ES info
* against it. XXX - note that we don't handle the case where
* a zif changes from bond to non-bond; it is really
* an unexpected/error condition.
*/
zebra_evpn_if_init(zif);
}
}
static void netlink_determine_zebra_iftype(const char *kind,
enum zebra_iftype *zif_type)
{
*zif_type = ZEBRA_IF_OTHER;
if (!kind)
return;
if (strcmp(kind, "vrf") == 0)
*zif_type = ZEBRA_IF_VRF;
else if (strcmp(kind, "bridge") == 0)
*zif_type = ZEBRA_IF_BRIDGE;
else if (strcmp(kind, "vlan") == 0)
*zif_type = ZEBRA_IF_VLAN;
else if (strcmp(kind, "vxlan") == 0)
*zif_type = ZEBRA_IF_VXLAN;
else if (strcmp(kind, "macvlan") == 0)
*zif_type = ZEBRA_IF_MACVLAN;
else if (strcmp(kind, "veth") == 0)
*zif_type = ZEBRA_IF_VETH;
else if (strcmp(kind, "bond") == 0)
*zif_type = ZEBRA_IF_BOND;
else if (strcmp(kind, "bond_slave") == 0)
*zif_type = ZEBRA_IF_BOND_SLAVE;
else if (strcmp(kind, "gre") == 0)
*zif_type = ZEBRA_IF_GRE;
}
static void netlink_vrf_change(struct nlmsghdr *h, struct rtattr *tb,
uint32_t ns_id, const char *name)
{
struct ifinfomsg *ifi;
struct rtattr *linkinfo[IFLA_INFO_MAX + 1];
struct rtattr *attr[IFLA_VRF_MAX + 1];
struct vrf *vrf = NULL;
struct zebra_vrf *zvrf;
uint32_t nl_table_id;
ifi = NLMSG_DATA(h);
netlink_parse_rtattr_nested(linkinfo, IFLA_INFO_MAX, tb);
if (!linkinfo[IFLA_INFO_DATA]) {
if (IS_ZEBRA_DEBUG_KERNEL)
zlog_debug(
"%s: IFLA_INFO_DATA missing from VRF message: %s",
__func__, name);
return;
}
netlink_parse_rtattr_nested(attr, IFLA_VRF_MAX,
linkinfo[IFLA_INFO_DATA]);
if (!attr[IFLA_VRF_TABLE]) {
if (IS_ZEBRA_DEBUG_KERNEL)
zlog_debug(
"%s: IFLA_VRF_TABLE missing from VRF message: %s",
__func__, name);
return;
}
nl_table_id = *(uint32_t *)RTA_DATA(attr[IFLA_VRF_TABLE]);
if (h->nlmsg_type == RTM_NEWLINK) {
if (IS_ZEBRA_DEBUG_KERNEL)
zlog_debug("RTM_NEWLINK for VRF %s(%u) table %u", name,
ifi->ifi_index, nl_table_id);
if (!vrf_lookup_by_id((vrf_id_t)ifi->ifi_index)) {
vrf_id_t exist_id;
exist_id = vrf_lookup_by_table(nl_table_id, ns_id);
if (exist_id != VRF_DEFAULT) {
vrf = vrf_lookup_by_id(exist_id);
flog_err(
EC_ZEBRA_VRF_MISCONFIGURED,
"VRF %s id %u table id overlaps existing vrf %s, misconfiguration exiting",
name, ifi->ifi_index, vrf->name);
exit(-1);
}
}
vrf = vrf_update((vrf_id_t)ifi->ifi_index, name);
if (!vrf) {
flog_err(EC_LIB_INTERFACE, "VRF %s id %u not created",
name, ifi->ifi_index);
return;
}
/*
* This is the only place that we get the actual kernel table_id
* being used. We need it to set the table_id of the routes
* we are passing to the kernel.... And to throw some totally
* awesome parties. that too.
*
* At this point we *must* have a zvrf because the vrf_create
* callback creates one. We *must* set the table id
* before the vrf_enable because of( at the very least )
* static routes being delayed for installation until
* during the vrf_enable callbacks.
*/
zvrf = (struct zebra_vrf *)vrf->info;
zvrf->table_id = nl_table_id;
/* Enable the created VRF. */
if (!vrf_enable(vrf)) {
flog_err(EC_LIB_INTERFACE,
"Failed to enable VRF %s id %u", name,
ifi->ifi_index);
return;
}
} else // h->nlmsg_type == RTM_DELLINK
{
if (IS_ZEBRA_DEBUG_KERNEL)
zlog_debug("RTM_DELLINK for VRF %s(%u)", name,
ifi->ifi_index);
vrf = vrf_lookup_by_id((vrf_id_t)ifi->ifi_index);
if (!vrf) {
flog_warn(EC_ZEBRA_VRF_NOT_FOUND, "%s: vrf not found",
__func__);
return;
}
vrf_delete(vrf);
}
}
static uint32_t get_iflink_speed(struct interface *interface, int *error)
{
struct ifreq ifdata;
struct ethtool_cmd ecmd;
int sd;
int rc;
const char *ifname = interface->name;
if (error)
*error = 0;
/* initialize struct */
memset(&ifdata, 0, sizeof(ifdata));
/* set interface name */
strlcpy(ifdata.ifr_name, ifname, sizeof(ifdata.ifr_name));
/* initialize ethtool interface */
memset(&ecmd, 0, sizeof(ecmd));
ecmd.cmd = ETHTOOL_GSET; /* ETHTOOL_GLINK */
ifdata.ifr_data = (caddr_t)&ecmd;
/* use ioctl to get speed of an interface */
frr_with_privs(&zserv_privs) {
sd = vrf_socket(PF_INET, SOCK_DGRAM, IPPROTO_IP,
interface->vrf->vrf_id, NULL);
if (sd < 0) {
if (IS_ZEBRA_DEBUG_KERNEL)
zlog_debug("Failure to read interface %s speed: %d %s",
ifname, errno, safe_strerror(errno));
/* no vrf socket creation may probably mean vrf issue */
if (error)
*error = -1;
return 0;
}
/* Get the current link state for the interface */
rc = vrf_ioctl(interface->vrf->vrf_id, sd, SIOCETHTOOL,
(char *)&ifdata);
}
if (rc < 0) {
if (errno != EOPNOTSUPP && IS_ZEBRA_DEBUG_KERNEL)
zlog_debug(
"IOCTL failure to read interface %s speed: %d %s",
ifname, errno, safe_strerror(errno));
/* no device means interface unreachable */
if (errno == ENODEV && error)
*error = -1;
ecmd.speed_hi = 0;
ecmd.speed = 0;
}
close(sd);
return ((uint32_t)ecmd.speed_hi << 16) | ecmd.speed;
}
uint32_t kernel_get_speed(struct interface *ifp, int *error)
{
return get_iflink_speed(ifp, error);
}
static ssize_t
netlink_gre_set_msg_encoder(struct zebra_dplane_ctx *ctx, void *buf,
size_t buflen)
{
struct {
struct nlmsghdr n;
struct ifinfomsg ifi;
char buf[];
} *req = buf;
uint32_t link_idx;
unsigned int mtu;
struct rtattr *rta_info, *rta_data;
const struct zebra_l2info_gre *gre_info;
if (buflen < sizeof(*req))
return 0;
memset(req, 0, sizeof(*req));
req->n.nlmsg_type = RTM_NEWLINK;
req->n.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifinfomsg));
req->n.nlmsg_flags = NLM_F_REQUEST;
req->ifi.ifi_index = dplane_ctx_get_ifindex(ctx);
gre_info = dplane_ctx_gre_get_info(ctx);
if (!gre_info)
return 0;
req->ifi.ifi_change = 0xFFFFFFFF;
link_idx = dplane_ctx_gre_get_link_ifindex(ctx);
mtu = dplane_ctx_gre_get_mtu(ctx);
if (mtu && !nl_attr_put32(&req->n, buflen, IFLA_MTU, mtu))
return 0;
rta_info = nl_attr_nest(&req->n, buflen, IFLA_LINKINFO);
if (!rta_info)
return 0;
if (!nl_attr_put(&req->n, buflen, IFLA_INFO_KIND, "gre", 3))
return 0;
rta_data = nl_attr_nest(&req->n, buflen, IFLA_INFO_DATA);
if (!rta_data)
return 0;
if (!nl_attr_put32(&req->n, buflen, IFLA_GRE_LINK, link_idx))
return 0;
if (gre_info->vtep_ip.s_addr &&
!nl_attr_put32(&req->n, buflen, IFLA_GRE_LOCAL,
gre_info->vtep_ip.s_addr))
return 0;
if (gre_info->vtep_ip_remote.s_addr &&
!nl_attr_put32(&req->n, buflen, IFLA_GRE_REMOTE,
gre_info->vtep_ip_remote.s_addr))
return 0;
if (gre_info->ikey &&
!nl_attr_put32(&req->n, buflen, IFLA_GRE_IKEY,
gre_info->ikey))
return 0;
if (gre_info->okey &&
!nl_attr_put32(&req->n, buflen, IFLA_GRE_IKEY,
gre_info->okey))
return 0;
nl_attr_nest_end(&req->n, rta_data);
nl_attr_nest_end(&req->n, rta_info);
return NLMSG_ALIGN(req->n.nlmsg_len);
}
static int netlink_extract_bridge_info(struct rtattr *link_data,
struct zebra_l2info_bridge *bridge_info)
{
struct rtattr *attr[IFLA_BR_MAX + 1];
memset(bridge_info, 0, sizeof(*bridge_info));
netlink_parse_rtattr_nested(attr, IFLA_BR_MAX, link_data);
if (attr[IFLA_BR_VLAN_FILTERING])
bridge_info->bridge.vlan_aware =
*(uint8_t *)RTA_DATA(attr[IFLA_BR_VLAN_FILTERING]);
return 0;
}
static int netlink_extract_vlan_info(struct rtattr *link_data,
struct zebra_l2info_vlan *vlan_info)
{
struct rtattr *attr[IFLA_VLAN_MAX + 1];
vlanid_t vid_in_msg;
memset(vlan_info, 0, sizeof(*vlan_info));
netlink_parse_rtattr_nested(attr, IFLA_VLAN_MAX, link_data);
if (!attr[IFLA_VLAN_ID]) {
if (IS_ZEBRA_DEBUG_KERNEL)
zlog_debug("IFLA_VLAN_ID missing from VLAN IF message");
return -1;
}
vid_in_msg = *(vlanid_t *)RTA_DATA(attr[IFLA_VLAN_ID]);
vlan_info->vid = vid_in_msg;
return 0;
}
static int netlink_extract_gre_info(struct rtattr *link_data,
struct zebra_l2info_gre *gre_info)
{
struct rtattr *attr[IFLA_GRE_MAX + 1];
memset(gre_info, 0, sizeof(*gre_info));
memset(attr, 0, sizeof(attr));
netlink_parse_rtattr_nested(attr, IFLA_GRE_MAX, link_data);
if (!attr[IFLA_GRE_LOCAL]) {
if (IS_ZEBRA_DEBUG_KERNEL)
zlog_debug(
"IFLA_GRE_LOCAL missing from GRE IF message");
} else
gre_info->vtep_ip =
*(struct in_addr *)RTA_DATA(attr[IFLA_GRE_LOCAL]);
if (!attr[IFLA_GRE_REMOTE]) {
if (IS_ZEBRA_DEBUG_KERNEL)
zlog_debug(
"IFLA_GRE_REMOTE missing from GRE IF message");
} else
gre_info->vtep_ip_remote =
*(struct in_addr *)RTA_DATA(attr[IFLA_GRE_REMOTE]);
if (!attr[IFLA_GRE_LINK]) {
if (IS_ZEBRA_DEBUG_KERNEL)
zlog_debug("IFLA_GRE_LINK missing from GRE IF message");
} else {
gre_info->ifindex_link =
*(ifindex_t *)RTA_DATA(attr[IFLA_GRE_LINK]);
if (IS_ZEBRA_DEBUG_KERNEL)
zlog_debug("IFLA_GRE_LINK obtained is %u",
gre_info->ifindex_link);
}
if (attr[IFLA_GRE_IKEY])
gre_info->ikey = *(uint32_t *)RTA_DATA(attr[IFLA_GRE_IKEY]);
if (attr[IFLA_GRE_OKEY])
gre_info->okey = *(uint32_t *)RTA_DATA(attr[IFLA_GRE_OKEY]);
return 0;
}
static int netlink_extract_vxlan_info(struct rtattr *link_data,
struct zebra_l2info_vxlan *vxl_info)
{
uint8_t svd = 0;
struct rtattr *attr[IFLA_VXLAN_MAX + 1];
vni_t vni_in_msg;
struct in_addr vtep_ip_in_msg;
ifindex_t ifindex_link;
memset(vxl_info, 0, sizeof(*vxl_info));
netlink_parse_rtattr_nested(attr, IFLA_VXLAN_MAX, link_data);
if (attr[IFLA_VXLAN_COLLECT_METADATA]) {
svd = *(uint8_t *)RTA_DATA(attr[IFLA_VXLAN_COLLECT_METADATA]);
if (IS_ZEBRA_DEBUG_KERNEL)
zlog_debug(
"IFLA_VXLAN_COLLECT_METADATA=%u in VXLAN IF message",
svd);
}
if (!svd) {
/*
* In case of svd we will not get vni info directly from the
* device
*/
if (!attr[IFLA_VXLAN_ID]) {
if (IS_ZEBRA_DEBUG_KERNEL)
zlog_debug(
"IFLA_VXLAN_ID missing from VXLAN IF message");
return -1;
}
vxl_info->vni_info.iftype = ZEBRA_VXLAN_IF_VNI;
vni_in_msg = *(vni_t *)RTA_DATA(attr[IFLA_VXLAN_ID]);
vxl_info->vni_info.vni.vni = vni_in_msg;
} else {
vxl_info->vni_info.iftype = ZEBRA_VXLAN_IF_SVD;
}
if (!attr[IFLA_VXLAN_LOCAL]) {
if (IS_ZEBRA_DEBUG_KERNEL)
zlog_debug(
"IFLA_VXLAN_LOCAL missing from VXLAN IF message");
} else {
vtep_ip_in_msg =
*(struct in_addr *)RTA_DATA(attr[IFLA_VXLAN_LOCAL]);
vxl_info->vtep_ip = vtep_ip_in_msg;
}
if (attr[IFLA_VXLAN_GROUP]) {
if (!svd)
vxl_info->vni_info.vni.mcast_grp =
*(struct in_addr *)RTA_DATA(
attr[IFLA_VXLAN_GROUP]);
}
if (!attr[IFLA_VXLAN_LINK]) {
if (IS_ZEBRA_DEBUG_KERNEL)
zlog_debug("IFLA_VXLAN_LINK missing from VXLAN IF message");
} else {
ifindex_link =
*(ifindex_t *)RTA_DATA(attr[IFLA_VXLAN_LINK]);
vxl_info->ifindex_link = ifindex_link;
}
return 0;
}
/*
* Extract and save L2 params (of interest) for an interface. When a
* bridge interface is added or updated, take further actions to map
* its members. Likewise, for VxLAN interface.
*/
static void netlink_interface_update_l2info(struct interface *ifp,
struct rtattr *link_data, int add,
ns_id_t link_nsid)
{
if (!link_data)
return;
if (IS_ZEBRA_IF_BRIDGE(ifp)) {
struct zebra_l2info_bridge bridge_info;
netlink_extract_bridge_info(link_data, &bridge_info);
zebra_l2_bridge_add_update(ifp, &bridge_info, add);
} else if (IS_ZEBRA_IF_VLAN(ifp)) {
struct zebra_l2info_vlan vlan_info;
netlink_extract_vlan_info(link_data, &vlan_info);
zebra_l2_vlanif_update(ifp, &vlan_info);
zebra_evpn_acc_bd_svi_set(ifp->info, NULL,
!!if_is_operative(ifp));
} else if (IS_ZEBRA_IF_VXLAN(ifp)) {
struct zebra_l2info_vxlan vxlan_info;
netlink_extract_vxlan_info(link_data, &vxlan_info);
vxlan_info.link_nsid = link_nsid;
zebra_l2_vxlanif_add_update(ifp, &vxlan_info, add);
if (link_nsid != NS_UNKNOWN &&
vxlan_info.ifindex_link)
zebra_if_update_link(ifp, vxlan_info.ifindex_link,
link_nsid);
} else if (IS_ZEBRA_IF_GRE(ifp)) {
struct zebra_l2info_gre gre_info;
netlink_extract_gre_info(link_data, &gre_info);
gre_info.link_nsid = link_nsid;
zebra_l2_greif_add_update(ifp, &gre_info, add);
if (link_nsid != NS_UNKNOWN &&
gre_info.ifindex_link)
zebra_if_update_link(ifp, gre_info.ifindex_link,
link_nsid);
}
}
static int netlink_bridge_vxlan_vlan_vni_map_update(struct interface *ifp,
struct rtattr *af_spec)
{
int rem;
vni_t vni_id;
vlanid_t vid;
uint16_t flags;
struct rtattr *i;
struct zebra_vxlan_vni vni;
struct zebra_vxlan_vni *vnip;
struct hash *vni_table = NULL;
struct zebra_vxlan_vni vni_end;
struct zebra_vxlan_vni vni_start;
struct rtattr *aftb[IFLA_BRIDGE_VLAN_TUNNEL_MAX + 1];
memset(&vni_start, 0, sizeof(vni_start));
memset(&vni_end, 0, sizeof(vni_end));
for (i = RTA_DATA(af_spec), rem = RTA_PAYLOAD(af_spec); RTA_OK(i, rem);
i = RTA_NEXT(i, rem)) {
if (i->rta_type != IFLA_BRIDGE_VLAN_TUNNEL_INFO)
continue;
memset(aftb, 0, sizeof(aftb));
netlink_parse_rtattr_nested(aftb, IFLA_BRIDGE_VLAN_TUNNEL_MAX,
i);
if (!aftb[IFLA_BRIDGE_VLAN_TUNNEL_ID] ||
!aftb[IFLA_BRIDGE_VLAN_TUNNEL_VID])
/* vlan-vni info missing */
return 0;
flags = 0;
memset(&vni, 0, sizeof(vni));
vni.vni = *(vni_t *)RTA_DATA(aftb[IFLA_BRIDGE_VLAN_TUNNEL_ID]);
vni.access_vlan = *(vlanid_t *)RTA_DATA(
aftb[IFLA_BRIDGE_VLAN_TUNNEL_VID]);
if (aftb[IFLA_BRIDGE_VLAN_TUNNEL_FLAGS])
flags = *(uint16_t *)RTA_DATA(
aftb[IFLA_BRIDGE_VLAN_TUNNEL_FLAGS]);
if (flags & BRIDGE_VLAN_INFO_RANGE_BEGIN) {
vni_start = vni;
continue;
}
if (flags & BRIDGE_VLAN_INFO_RANGE_END)
vni_end = vni;
if (!(flags & BRIDGE_VLAN_INFO_RANGE_END)) {
vni_start = vni;
vni_end = vni;
}
if (IS_ZEBRA_DEBUG_KERNEL)
zlog_debug(
"Vlan-Vni(%d:%d-%d:%d) update for VxLAN IF %s(%u)",
vni_start.access_vlan, vni_end.access_vlan,
vni_start.vni, vni_end.vni, ifp->name,
ifp->ifindex);
if (!vni_table) {
vni_table = zebra_vxlan_vni_table_create();
if (!vni_table)
return 0;
}
for (vid = vni_start.access_vlan, vni_id = vni_start.vni;
vid <= vni_end.access_vlan; vid++, vni_id++) {
memset(&vni, 0, sizeof(vni));
vni.vni = vni_id;
vni.access_vlan = vid;
vnip = hash_get(vni_table, &vni, zebra_vxlan_vni_alloc);
if (!vnip)
return 0;
}
memset(&vni_start, 0, sizeof(vni_start));
memset(&vni_end, 0, sizeof(vni_end));
}
if (vni_table)
zebra_vxlan_if_vni_table_add_update(ifp, vni_table);
return 0;
}
static int netlink_bridge_vxlan_update(struct interface *ifp,
struct rtattr *af_spec)
{
struct rtattr *aftb[IFLA_BRIDGE_MAX + 1];
struct bridge_vlan_info *vinfo;
struct zebra_if *zif;
vlanid_t access_vlan;
if (!af_spec)
return 0;
zif = (struct zebra_if *)ifp->info;
/* Single vxlan devices has vni-vlan range to update */
if (IS_ZEBRA_VXLAN_IF_SVD(zif))
return netlink_bridge_vxlan_vlan_vni_map_update(ifp, af_spec);
/* There is a 1-to-1 mapping of VLAN to VxLAN - hence
* only 1 access VLAN is accepted.
*/
netlink_parse_rtattr_nested(aftb, IFLA_BRIDGE_MAX, af_spec);
if (!aftb[IFLA_BRIDGE_VLAN_INFO])
return 0;
vinfo = RTA_DATA(aftb[IFLA_BRIDGE_VLAN_INFO]);
if (!(vinfo->flags & BRIDGE_VLAN_INFO_PVID))
return 0;
access_vlan = (vlanid_t)vinfo->vid;
if (IS_ZEBRA_DEBUG_KERNEL)
zlog_debug("Access VLAN %u for VxLAN IF %s(%u)", access_vlan,
ifp->name, ifp->ifindex);
zebra_l2_vxlanif_update_access_vlan(ifp, access_vlan);
return 0;
}
static void netlink_bridge_vlan_update(struct interface *ifp,
struct rtattr *af_spec)
{
struct rtattr *i;
int rem;
uint16_t vid_range_start = 0;
struct zebra_if *zif;
bitfield_t old_vlan_bitmap;
struct bridge_vlan_info *vinfo;
zif = (struct zebra_if *)ifp->info;
/* cache the old bitmap addrs */
old_vlan_bitmap = zif->vlan_bitmap;
/* create a new bitmap space for re-eval */
bf_init(zif->vlan_bitmap, IF_VLAN_BITMAP_MAX);
if (af_spec) {
for (i = RTA_DATA(af_spec), rem = RTA_PAYLOAD(af_spec);
RTA_OK(i, rem); i = RTA_NEXT(i, rem)) {
if (i->rta_type != IFLA_BRIDGE_VLAN_INFO)
continue;
vinfo = RTA_DATA(i);
if (vinfo->flags & BRIDGE_VLAN_INFO_RANGE_BEGIN) {
vid_range_start = vinfo->vid;
continue;
}
if (!(vinfo->flags & BRIDGE_VLAN_INFO_RANGE_END))
vid_range_start = vinfo->vid;
zebra_vlan_bitmap_compute(ifp, vid_range_start,
vinfo->vid);
}
}
zebra_vlan_mbr_re_eval(ifp, old_vlan_bitmap);
bf_free(old_vlan_bitmap);
}
static int netlink_bridge_interface(struct nlmsghdr *h, int len, ns_id_t ns_id,
int startup)
{
char *name = NULL;
struct ifinfomsg *ifi;
struct rtattr *tb[IFLA_MAX + 1];
struct interface *ifp;
struct zebra_if *zif;
struct rtattr *af_spec;
/* Fetch name and ifindex */
ifi = NLMSG_DATA(h);
netlink_parse_rtattr(tb, IFLA_MAX, IFLA_RTA(ifi), len);
if (tb[IFLA_IFNAME] == NULL)
return -1;
name = (char *)RTA_DATA(tb[IFLA_IFNAME]);
/* The interface should already be known, if not discard. */
ifp = if_lookup_by_index_per_ns(zebra_ns_lookup(ns_id), ifi->ifi_index);
if (!ifp) {
zlog_debug("Cannot find bridge IF %s(%u)", name,
ifi->ifi_index);
return 0;
}
/* We are only interested in the access VLAN i.e., AF_SPEC */
af_spec = tb[IFLA_AF_SPEC];
if (IS_ZEBRA_IF_VXLAN(ifp))
return netlink_bridge_vxlan_update(ifp, af_spec);
/* build vlan bitmap associated with this interface if that
* device type is interested in the vlans
*/
zif = (struct zebra_if *)ifp->info;
if (bf_is_inited(zif->vlan_bitmap))
netlink_bridge_vlan_update(ifp, af_spec);
return 0;
}
static bool is_if_protodown_reason_only_frr(uint32_t rc_bitfield)
{
/* This shouldn't be possible */
assert(frr_protodown_r_bit < 32);
return (rc_bitfield == (((uint32_t)1) << frr_protodown_r_bit));
}
/*
* Process interface protodown dplane update.
*
* If the interface is an es bond member then it must follow EVPN's
* protodown setting.
*/
static void netlink_proc_dplane_if_protodown(struct zebra_if *zif,
struct rtattr **tb)
{
bool protodown;
bool old_protodown;
uint32_t rc_bitfield = 0;
struct rtattr *pd_reason_info[IFLA_MAX + 1];
protodown = !!*(uint8_t *)RTA_DATA(tb[IFLA_PROTO_DOWN]);
if (tb[IFLA_PROTO_DOWN_REASON]) {
netlink_parse_rtattr_nested(pd_reason_info, IFLA_INFO_MAX,
tb[IFLA_PROTO_DOWN_REASON]);
if (pd_reason_info[IFLA_PROTO_DOWN_REASON_VALUE])
rc_bitfield = *(uint32_t *)RTA_DATA(
pd_reason_info[IFLA_PROTO_DOWN_REASON_VALUE]);
}
/*
* Set our reason code to note it wasn't us.
* If the reason we got from the kernel is ONLY frr though, don't
* set it.
*/
COND_FLAG(zif->protodown_rc, ZEBRA_PROTODOWN_EXTERNAL,
protodown && rc_bitfield &&
!is_if_protodown_reason_only_frr(rc_bitfield));
old_protodown = !!ZEBRA_IF_IS_PROTODOWN(zif);
if (protodown == old_protodown)
return;
if (IS_ZEBRA_DEBUG_EVPN_MH_ES || IS_ZEBRA_DEBUG_KERNEL)
zlog_debug("interface %s dplane change, protdown %s",
zif->ifp->name, protodown ? "on" : "off");
/* Set protodown, respectively */
COND_FLAG(zif->flags, ZIF_FLAG_PROTODOWN, protodown);
if (zebra_evpn_is_es_bond_member(zif->ifp)) {
/* Check it's not already being sent to the dplane first */
if (protodown &&
CHECK_FLAG(zif->flags, ZIF_FLAG_SET_PROTODOWN)) {
if (IS_ZEBRA_DEBUG_EVPN_MH_ES || IS_ZEBRA_DEBUG_KERNEL)
zlog_debug(
"bond mbr %s protodown on recv'd but already sent protodown on to the dplane",
zif->ifp->name);
return;
}
if (!protodown &&
CHECK_FLAG(zif->flags, ZIF_FLAG_UNSET_PROTODOWN)) {
if (IS_ZEBRA_DEBUG_EVPN_MH_ES || IS_ZEBRA_DEBUG_KERNEL)
zlog_debug(
"bond mbr %s protodown off recv'd but already sent protodown off to the dplane",
zif->ifp->name);
return;
}
if (IS_ZEBRA_DEBUG_EVPN_MH_ES || IS_ZEBRA_DEBUG_KERNEL)
zlog_debug(
"bond mbr %s reinstate protodown %s in the dplane",
zif->ifp->name, old_protodown ? "on" : "off");
if (old_protodown)
SET_FLAG(zif->flags, ZIF_FLAG_SET_PROTODOWN);
else
SET_FLAG(zif->flags, ZIF_FLAG_UNSET_PROTODOWN);
dplane_intf_update(zif->ifp);
}
}
static uint8_t netlink_parse_lacp_bypass(struct rtattr **linkinfo)
{
uint8_t bypass = 0;
struct rtattr *mbrinfo[IFLA_BOND_SLAVE_MAX + 1];
netlink_parse_rtattr_nested(mbrinfo, IFLA_BOND_SLAVE_MAX,
linkinfo[IFLA_INFO_SLAVE_DATA]);
if (mbrinfo[IFLA_BOND_SLAVE_AD_RX_BYPASS])
bypass = *(uint8_t *)RTA_DATA(
mbrinfo[IFLA_BOND_SLAVE_AD_RX_BYPASS]);
return bypass;
}
/*
* Only called at startup to cleanup leftover protodown reasons we may
* have not cleaned up. We leave protodown set though.
*/
static void if_sweep_protodown(struct zebra_if *zif)
{
bool protodown;
protodown = !!ZEBRA_IF_IS_PROTODOWN(zif);
if (!protodown)
return;
if (IS_ZEBRA_DEBUG_KERNEL)
zlog_debug("interface %s sweeping protodown %s reason 0x%x",
zif->ifp->name, protodown ? "on" : "off",
zif->protodown_rc);
/* Only clear our reason codes, leave external if it was set */
UNSET_FLAG(zif->protodown_rc, ZEBRA_PROTODOWN_ALL);
dplane_intf_update(zif->ifp);
}
/*
* Called from interface_lookup_netlink(). This function is only used
* during bootstrap.
*/
static int netlink_interface(struct nlmsghdr *h, ns_id_t ns_id, int startup)
{
int len;
struct ifinfomsg *ifi;
struct rtattr *tb[IFLA_MAX + 1];
struct rtattr *linkinfo[IFLA_MAX + 1];
struct interface *ifp;
char *name = NULL;
char *kind = NULL;
char *desc = NULL;
char *slave_kind = NULL;
struct zebra_ns *zns = NULL;
vrf_id_t vrf_id = VRF_DEFAULT;
enum zebra_iftype zif_type = ZEBRA_IF_OTHER;
enum zebra_slave_iftype zif_slave_type = ZEBRA_IF_SLAVE_NONE;
ifindex_t bridge_ifindex = IFINDEX_INTERNAL;
ifindex_t link_ifindex = IFINDEX_INTERNAL;
ifindex_t bond_ifindex = IFINDEX_INTERNAL;
struct zebra_if *zif;
ns_id_t link_nsid = ns_id;
uint8_t bypass = 0;
frrtrace(3, frr_zebra, netlink_interface, h, ns_id, startup);
zns = zebra_ns_lookup(ns_id);
ifi = NLMSG_DATA(h);
if (h->nlmsg_type != RTM_NEWLINK)
return 0;
len = h->nlmsg_len - NLMSG_LENGTH(sizeof(struct ifinfomsg));
if (len < 0) {
zlog_err(
"%s: Message received from netlink is of a broken size: %d %zu",
__func__, h->nlmsg_len,
(size_t)NLMSG_LENGTH(sizeof(struct ifinfomsg)));
return -1;
}
/* We are interested in some AF_BRIDGE notifications. */
if (ifi->ifi_family == AF_BRIDGE)
return netlink_bridge_interface(h, len, ns_id, startup);
/* Looking up interface name. */
memset(linkinfo, 0, sizeof(linkinfo));
netlink_parse_rtattr_flags(tb, IFLA_MAX, IFLA_RTA(ifi), len,
NLA_F_NESTED);
/* check for wireless messages to ignore */
if ((tb[IFLA_WIRELESS] != NULL) && (ifi->ifi_change == 0)) {
if (IS_ZEBRA_DEBUG_KERNEL)
zlog_debug("%s: ignoring IFLA_WIRELESS message",
__func__);
return 0;
}
if (tb[IFLA_IFNAME] == NULL)
return -1;
name = (char *)RTA_DATA(tb[IFLA_IFNAME]);
if (tb[IFLA_IFALIAS])
desc = (char *)RTA_DATA(tb[IFLA_IFALIAS]);
if (tb[IFLA_LINKINFO]) {
netlink_parse_rtattr_nested(linkinfo, IFLA_INFO_MAX,
tb[IFLA_LINKINFO]);
if (linkinfo[IFLA_INFO_KIND])
kind = RTA_DATA(linkinfo[IFLA_INFO_KIND]);
if (linkinfo[IFLA_INFO_SLAVE_KIND])
slave_kind = RTA_DATA(linkinfo[IFLA_INFO_SLAVE_KIND]);
if ((slave_kind != NULL) && strcmp(slave_kind, "bond") == 0)
netlink_determine_zebra_iftype("bond_slave", &zif_type);
else
netlink_determine_zebra_iftype(kind, &zif_type);
}
/* If VRF, create the VRF structure itself. */
if (zif_type == ZEBRA_IF_VRF && !vrf_is_backend_netns()) {
netlink_vrf_change(h, tb[IFLA_LINKINFO], ns_id, name);
vrf_id = (vrf_id_t)ifi->ifi_index;
}
if (tb[IFLA_MASTER]) {
if (slave_kind && (strcmp(slave_kind, "vrf") == 0)
&& !vrf_is_backend_netns()) {
zif_slave_type = ZEBRA_IF_SLAVE_VRF;
vrf_id = *(uint32_t *)RTA_DATA(tb[IFLA_MASTER]);
} else if (slave_kind && (strcmp(slave_kind, "bridge") == 0)) {
zif_slave_type = ZEBRA_IF_SLAVE_BRIDGE;
bridge_ifindex =
*(ifindex_t *)RTA_DATA(tb[IFLA_MASTER]);
} else if (slave_kind && (strcmp(slave_kind, "bond") == 0)) {
zif_slave_type = ZEBRA_IF_SLAVE_BOND;
bond_ifindex = *(ifindex_t *)RTA_DATA(tb[IFLA_MASTER]);
bypass = netlink_parse_lacp_bypass(linkinfo);
} else
zif_slave_type = ZEBRA_IF_SLAVE_OTHER;
}
if (vrf_is_backend_netns())
vrf_id = (vrf_id_t)ns_id;
/* If linking to another interface, note it. */
if (tb[IFLA_LINK])
link_ifindex = *(ifindex_t *)RTA_DATA(tb[IFLA_LINK]);
if (tb[IFLA_LINK_NETNSID]) {
link_nsid = *(ns_id_t *)RTA_DATA(tb[IFLA_LINK_NETNSID]);
link_nsid = ns_id_get_absolute(ns_id, link_nsid);
}
ifp = if_get_by_name(name, vrf_id, NULL);
set_ifindex(ifp, ifi->ifi_index, zns); /* add it to ns struct */
ifp->flags = ifi->ifi_flags & 0x0000fffff;
ifp->mtu6 = ifp->mtu = *(uint32_t *)RTA_DATA(tb[IFLA_MTU]);
ifp->metric = 0;
ifp->speed = get_iflink_speed(ifp, NULL);
ifp->ptm_status = ZEBRA_PTM_STATUS_UNKNOWN;
/* Set zebra interface type */
zebra_if_set_ziftype(ifp, zif_type, zif_slave_type);
if (IS_ZEBRA_IF_VRF(ifp))
SET_FLAG(ifp->status, ZEBRA_INTERFACE_VRF_LOOPBACK);
/*
* Just set the @link/lower-device ifindex. During nldump interfaces are
* not ordered in any fashion so we may end up getting upper devices
* before lower devices. We will setup the real linkage once the dump
* is complete.
*/
zif = (struct zebra_if *)ifp->info;
zif->link_ifindex = link_ifindex;
if (desc) {
XFREE(MTYPE_ZIF_DESC, zif->desc);
zif->desc = XSTRDUP(MTYPE_ZIF_DESC, desc);
}
/* Hardware type and address. */
ifp->ll_type = netlink_to_zebra_link_type(ifi->ifi_type);
netlink_interface_update_hw_addr(tb, ifp);
if_add_update(ifp);
/* Extract and save L2 interface information, take additional actions.
*/
netlink_interface_update_l2info(ifp, linkinfo[IFLA_INFO_DATA],
1, link_nsid);
if (IS_ZEBRA_IF_BOND(ifp))
zebra_l2if_update_bond(ifp, true);
if (IS_ZEBRA_IF_BRIDGE_SLAVE(ifp))
zebra_l2if_update_bridge_slave(ifp, bridge_ifindex, ns_id,
ZEBRA_BRIDGE_NO_ACTION);
else if (IS_ZEBRA_IF_BOND_SLAVE(ifp))
zebra_l2if_update_bond_slave(ifp, bond_ifindex, !!bypass);
if (tb[IFLA_PROTO_DOWN]) {
netlink_proc_dplane_if_protodown(zif, tb);
if_sweep_protodown(zif);
}
return 0;
}
/* Request for specific interface or address information from the kernel */
static int netlink_request_intf_addr(struct nlsock *netlink_cmd, int family,
int type, uint32_t filter_mask)
{
struct {
struct nlmsghdr n;
struct ifinfomsg ifm;
char buf[256];
} req;
frrtrace(4, frr_zebra, netlink_request_intf_addr, netlink_cmd, family,
type, filter_mask);
/* Form the request, specifying filter (rtattr) if needed. */
memset(&req, 0, sizeof(req));
req.n.nlmsg_type = type;
req.n.nlmsg_flags = NLM_F_ROOT | NLM_F_MATCH | NLM_F_REQUEST;
req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifinfomsg));
req.ifm.ifi_family = family;
/* Include filter, if specified. */
if (filter_mask)
nl_attr_put32(&req.n, sizeof(req), IFLA_EXT_MASK, filter_mask);
return netlink_request(netlink_cmd, &req);
}
enum netlink_msg_status
netlink_put_gre_set_msg(struct nl_batch *bth, struct zebra_dplane_ctx *ctx)
{
enum dplane_op_e op;
enum netlink_msg_status ret;
op = dplane_ctx_get_op(ctx);
assert(op == DPLANE_OP_GRE_SET);
ret = netlink_batch_add_msg(bth, ctx, netlink_gre_set_msg_encoder, false);
return ret;
}
/* Interface lookup by netlink socket. */
int interface_lookup_netlink(struct zebra_ns *zns)
{
int ret;
struct zebra_dplane_info dp_info;
struct nlsock *netlink_cmd = &zns->netlink_cmd;
/* Capture key info from ns struct */
zebra_dplane_info_from_zns(&dp_info, zns, true /*is_cmd*/);
/* Get interface information. */
ret = netlink_request_intf_addr(netlink_cmd, AF_PACKET, RTM_GETLINK, 0);
if (ret < 0)
return ret;
ret = netlink_parse_info(netlink_interface, netlink_cmd, &dp_info, 0,
true);
if (ret < 0)
return ret;
/* Get interface information - for bridge interfaces. */
ret = netlink_request_intf_addr(netlink_cmd, AF_BRIDGE, RTM_GETLINK,
RTEXT_FILTER_BRVLAN);
if (ret < 0)
return ret;
ret = netlink_parse_info(netlink_interface, netlink_cmd, &dp_info, 0,
true);
if (ret < 0)
return ret;
/*
* So netlink_tunneldump_read will initiate a request
* per tunnel to get data. If we are on a kernel that
* does not support this then we will get X error messages
* (one per tunnel request )back which netlink_parse_info will
* stop after the first one. So we need to read equivalent
* error messages per tunnel then we can continue.
* if we do not gather all the read failures then
* later requests will not work right.
*/
ret = netlink_tunneldump_read(zns);
if (ret < 0)
return ret;
/* fixup linkages */
zebra_if_update_all_links(zns);
return 0;
}
/**
* interface_addr_lookup_netlink() - Look up interface addresses
*
* @zns: Zebra netlink socket
* Return: Result status
*/
static int interface_addr_lookup_netlink(struct zebra_ns *zns)
{
int ret;
struct zebra_dplane_info dp_info;
struct nlsock *netlink_cmd = &zns->netlink_cmd;
/* Capture key info from ns struct */
zebra_dplane_info_from_zns(&dp_info, zns, true /*is_cmd*/);
/* Get IPv4 address of the interfaces. */
ret = netlink_request_intf_addr(netlink_cmd, AF_INET, RTM_GETADDR, 0);
if (ret < 0)
return ret;
ret = netlink_parse_info(netlink_interface_addr, netlink_cmd, &dp_info,
0, true);
if (ret < 0)
return ret;
/* Get IPv6 address of the interfaces. */
ret = netlink_request_intf_addr(netlink_cmd, AF_INET6, RTM_GETADDR, 0);
if (ret < 0)
return ret;
ret = netlink_parse_info(netlink_interface_addr, netlink_cmd, &dp_info,
0, true);
if (ret < 0)
return ret;
return 0;
}
int kernel_interface_set_master(struct interface *master,
struct interface *slave)
{
struct zebra_ns *zns = zebra_ns_lookup(NS_DEFAULT);
struct {
struct nlmsghdr n;
struct ifinfomsg ifa;
char buf[NL_PKT_BUF_SIZE];
} req;
memset(&req, 0, sizeof(req));
req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifinfomsg));
req.n.nlmsg_flags = NLM_F_REQUEST;
req.n.nlmsg_type = RTM_SETLINK;
req.n.nlmsg_pid = zns->netlink_cmd.snl.nl_pid;
req.ifa.ifi_index = slave->ifindex;
nl_attr_put32(&req.n, sizeof(req), IFLA_MASTER, master->ifindex);
nl_attr_put32(&req.n, sizeof(req), IFLA_LINK, slave->ifindex);
return netlink_talk(netlink_talk_filter, &req.n, &zns->netlink_cmd, zns,
false);
}
/* Interface address modification. */
static ssize_t netlink_address_msg_encoder(struct zebra_dplane_ctx *ctx,
void *buf, size_t buflen)
{
int bytelen;
const struct prefix *p;
int cmd;
const char *label;
struct {
struct nlmsghdr n;
struct ifaddrmsg ifa;
char buf[0];
} *req = buf;
if (buflen < sizeof(*req))
return 0;
p = dplane_ctx_get_intf_addr(ctx);
memset(req, 0, sizeof(*req));
bytelen = (p->family == AF_INET ? 4 : 16);
req->n.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifaddrmsg));
req->n.nlmsg_flags = NLM_F_REQUEST;
if (dplane_ctx_get_op(ctx) == DPLANE_OP_ADDR_INSTALL)
cmd = RTM_NEWADDR;
else
cmd = RTM_DELADDR;
req->n.nlmsg_type = cmd;
req->ifa.ifa_family = p->family;
req->ifa.ifa_index = dplane_ctx_get_ifindex(ctx);
if (!nl_attr_put(&req->n, buflen, IFA_LOCAL, &p->u.prefix, bytelen))
return 0;
if (p->family == AF_INET) {
if (dplane_ctx_intf_is_connected(ctx)) {
p = dplane_ctx_get_intf_dest(ctx);
if (!nl_attr_put(&req->n, buflen, IFA_ADDRESS,
&p->u.prefix, bytelen))
return 0;
} else if (cmd == RTM_NEWADDR) {
struct in_addr broad = {
.s_addr = ipv4_broadcast_addr(p->u.prefix4.s_addr,
p->prefixlen)
};
if (!nl_attr_put(&req->n, buflen, IFA_BROADCAST, &broad,
bytelen))
return 0;
}
}
/* p is now either address or destination/bcast addr */
req->ifa.ifa_prefixlen = p->prefixlen;
if (dplane_ctx_intf_is_secondary(ctx))
SET_FLAG(req->ifa.ifa_flags, IFA_F_SECONDARY);
if (dplane_ctx_intf_has_label(ctx)) {
label = dplane_ctx_get_intf_label(ctx);
if (!nl_attr_put(&req->n, buflen, IFA_LABEL, label,
strlen(label) + 1))
return 0;
}
return NLMSG_ALIGN(req->n.nlmsg_len);
}
enum netlink_msg_status
netlink_put_address_update_msg(struct nl_batch *bth,
struct zebra_dplane_ctx *ctx)
{
return netlink_batch_add_msg(bth, ctx, netlink_address_msg_encoder,
false);
}
static ssize_t netlink_intf_msg_encoder(struct zebra_dplane_ctx *ctx, void *buf,
size_t buflen)
{
enum dplane_op_e op;
int cmd = 0;
op = dplane_ctx_get_op(ctx);
switch (op) {
case DPLANE_OP_INTF_UPDATE:
cmd = RTM_SETLINK;
break;
case DPLANE_OP_INTF_INSTALL:
cmd = RTM_NEWLINK;
break;
case DPLANE_OP_INTF_DELETE:
cmd = RTM_DELLINK;
break;
case DPLANE_OP_NONE:
case DPLANE_OP_ROUTE_INSTALL:
case DPLANE_OP_ROUTE_UPDATE:
case DPLANE_OP_ROUTE_DELETE:
case DPLANE_OP_ROUTE_NOTIFY:
case DPLANE_OP_NH_INSTALL:
case DPLANE_OP_NH_UPDATE:
case DPLANE_OP_NH_DELETE:
case DPLANE_OP_LSP_INSTALL:
case DPLANE_OP_LSP_DELETE:
case DPLANE_OP_LSP_NOTIFY:
case DPLANE_OP_LSP_UPDATE:
case DPLANE_OP_PW_INSTALL:
case DPLANE_OP_PW_UNINSTALL:
case DPLANE_OP_SYS_ROUTE_ADD:
case DPLANE_OP_SYS_ROUTE_DELETE:
case DPLANE_OP_ADDR_INSTALL:
case DPLANE_OP_ADDR_UNINSTALL:
case DPLANE_OP_MAC_INSTALL:
case DPLANE_OP_MAC_DELETE:
case DPLANE_OP_NEIGH_INSTALL:
case DPLANE_OP_NEIGH_UPDATE:
case DPLANE_OP_NEIGH_DELETE:
case DPLANE_OP_NEIGH_DISCOVER:
case DPLANE_OP_VTEP_ADD:
case DPLANE_OP_VTEP_DELETE:
case DPLANE_OP_RULE_ADD:
case DPLANE_OP_RULE_DELETE:
case DPLANE_OP_RULE_UPDATE:
case DPLANE_OP_BR_PORT_UPDATE:
case DPLANE_OP_IPTABLE_ADD:
case DPLANE_OP_IPTABLE_DELETE:
case DPLANE_OP_IPSET_ADD:
case DPLANE_OP_IPSET_ENTRY_ADD:
case DPLANE_OP_IPSET_ENTRY_DELETE:
case DPLANE_OP_IPSET_DELETE:
case DPLANE_OP_NEIGH_IP_INSTALL:
case DPLANE_OP_NEIGH_IP_DELETE:
case DPLANE_OP_NEIGH_TABLE_UPDATE:
case DPLANE_OP_GRE_SET:
case DPLANE_OP_INTF_ADDR_ADD:
case DPLANE_OP_INTF_ADDR_DEL:
case DPLANE_OP_INTF_NETCONFIG:
case DPLANE_OP_TC_QDISC_INSTALL:
case DPLANE_OP_TC_QDISC_UNINSTALL:
case DPLANE_OP_TC_CLASS_ADD:
case DPLANE_OP_TC_CLASS_DELETE:
case DPLANE_OP_TC_CLASS_UPDATE:
case DPLANE_OP_TC_FILTER_ADD:
case DPLANE_OP_TC_FILTER_DELETE:
case DPLANE_OP_TC_FILTER_UPDATE:
flog_err(
EC_ZEBRA_NHG_FIB_UPDATE,
"Context received for kernel interface update with incorrect OP code (%u)",
op);
return -1;
}
return netlink_intf_msg_encode(cmd, ctx, buf, buflen);
}
enum netlink_msg_status
netlink_put_intf_update_msg(struct nl_batch *bth, struct zebra_dplane_ctx *ctx)
{
return netlink_batch_add_msg(bth, ctx, netlink_intf_msg_encoder, false);
}
int netlink_interface_addr(struct nlmsghdr *h, ns_id_t ns_id, int startup)
{
int len;
struct ifaddrmsg *ifa;
struct rtattr *tb[IFA_MAX + 1];
struct interface *ifp;
void *addr;
void *broad;
uint8_t flags = 0;
char *label = NULL;
struct zebra_ns *zns;
uint32_t metric = METRIC_MAX;
uint32_t kernel_flags = 0;
frrtrace(3, frr_zebra, netlink_interface_addr, h, ns_id, startup);
zns = zebra_ns_lookup(ns_id);
ifa = NLMSG_DATA(h);
if (ifa->ifa_family != AF_INET && ifa->ifa_family != AF_INET6) {
flog_warn(
EC_ZEBRA_UNKNOWN_FAMILY,
"Invalid address family: %u received from kernel interface addr change: %s",
ifa->ifa_family, nl_msg_type_to_str(h->nlmsg_type));
return 0;
}
if (h->nlmsg_type != RTM_NEWADDR && h->nlmsg_type != RTM_DELADDR)
return 0;
len = h->nlmsg_len - NLMSG_LENGTH(sizeof(struct ifaddrmsg));
if (len < 0) {
zlog_err(
"%s: Message received from netlink is of a broken size: %d %zu",
__func__, h->nlmsg_len,
(size_t)NLMSG_LENGTH(sizeof(struct ifaddrmsg)));
return -1;
}
netlink_parse_rtattr(tb, IFA_MAX, IFA_RTA(ifa), len);
ifp = if_lookup_by_index_per_ns(zns, ifa->ifa_index);
if (ifp == NULL) {
if (startup) {
/* During startup, failure to lookup the referenced
* interface should not be an error, so we have
* downgraded this condition to warning, and we permit
* the startup interface state retrieval to continue.
*/
flog_warn(EC_LIB_INTERFACE,
"%s: can't find interface by index %d",
__func__, ifa->ifa_index);
return 0;
} else {
flog_err(EC_LIB_INTERFACE,
"%s: can't find interface by index %d",
__func__, ifa->ifa_index);
return -1;
}
}
/* Flags passed through */
if (tb[IFA_FLAGS])
kernel_flags = *(int *)RTA_DATA(tb[IFA_FLAGS]);
else
kernel_flags = ifa->ifa_flags;
if (IS_ZEBRA_DEBUG_KERNEL) /* remove this line to see initial ifcfg */
{
char buf[BUFSIZ];
zlog_debug("%s %s %s flags 0x%x:", __func__,
nl_msg_type_to_str(h->nlmsg_type), ifp->name,
kernel_flags);
if (tb[IFA_LOCAL])
zlog_debug(" IFA_LOCAL %s/%d",
inet_ntop(ifa->ifa_family,
RTA_DATA(tb[IFA_LOCAL]), buf,
BUFSIZ),
ifa->ifa_prefixlen);
if (tb[IFA_ADDRESS])
zlog_debug(" IFA_ADDRESS %s/%d",
inet_ntop(ifa->ifa_family,
RTA_DATA(tb[IFA_ADDRESS]), buf,
BUFSIZ),
ifa->ifa_prefixlen);
if (tb[IFA_BROADCAST])
zlog_debug(" IFA_BROADCAST %s/%d",
inet_ntop(ifa->ifa_family,
RTA_DATA(tb[IFA_BROADCAST]), buf,
BUFSIZ),
ifa->ifa_prefixlen);
if (tb[IFA_LABEL] && strcmp(ifp->name, RTA_DATA(tb[IFA_LABEL])))
zlog_debug(" IFA_LABEL %s",
(char *)RTA_DATA(tb[IFA_LABEL]));
if (tb[IFA_CACHEINFO]) {
struct ifa_cacheinfo *ci = RTA_DATA(tb[IFA_CACHEINFO]);
zlog_debug(" IFA_CACHEINFO pref %d, valid %d",
ci->ifa_prefered, ci->ifa_valid);
}
}
/* logic copied from iproute2/ip/ipaddress.c:print_addrinfo() */
if (tb[IFA_LOCAL] == NULL)
tb[IFA_LOCAL] = tb[IFA_ADDRESS];
if (tb[IFA_ADDRESS] == NULL)
tb[IFA_ADDRESS] = tb[IFA_LOCAL];
/* local interface address */
addr = (tb[IFA_LOCAL] ? RTA_DATA(tb[IFA_LOCAL]) : NULL);
/* is there a peer address? */
if (tb[IFA_ADDRESS]
&& memcmp(RTA_DATA(tb[IFA_ADDRESS]), RTA_DATA(tb[IFA_LOCAL]),
RTA_PAYLOAD(tb[IFA_ADDRESS]))) {
broad = RTA_DATA(tb[IFA_ADDRESS]);
SET_FLAG(flags, ZEBRA_IFA_PEER);
} else
/* seeking a broadcast address */
broad = (tb[IFA_BROADCAST] ? RTA_DATA(tb[IFA_BROADCAST])
: NULL);
/* addr is primary key, SOL if we don't have one */
if (addr == NULL) {
zlog_debug("%s: Local Interface Address is NULL for %s",
__func__, ifp->name);
return -1;
}
/* Flags. */
if (kernel_flags & IFA_F_SECONDARY)
SET_FLAG(flags, ZEBRA_IFA_SECONDARY);
/* Label */
if (tb[IFA_LABEL])
label = (char *)RTA_DATA(tb[IFA_LABEL]);
if (label && strcmp(ifp->name, label) == 0)
label = NULL;
if (tb[IFA_RT_PRIORITY])
metric = *(uint32_t *)RTA_DATA(tb[IFA_RT_PRIORITY]);
/* Register interface address to the interface. */
if (ifa->ifa_family == AF_INET) {
if (ifa->ifa_prefixlen > IPV4_MAX_BITLEN) {
zlog_err(
"Invalid prefix length: %u received from kernel interface addr change: %s",
ifa->ifa_prefixlen,
nl_msg_type_to_str(h->nlmsg_type));
return -1;
}
if (h->nlmsg_type == RTM_NEWADDR)
connected_add_ipv4(ifp, flags, (struct in_addr *)addr,
ifa->ifa_prefixlen,
(struct in_addr *)broad, label,
metric);
else if (CHECK_FLAG(flags, ZEBRA_IFA_PEER)) {
/* Delete with a peer address */
connected_delete_ipv4(
ifp, flags, (struct in_addr *)addr,
ifa->ifa_prefixlen, broad);
} else
connected_delete_ipv4(
ifp, flags, (struct in_addr *)addr,
ifa->ifa_prefixlen, NULL);
}
if (ifa->ifa_family == AF_INET6) {
if (ifa->ifa_prefixlen > IPV6_MAX_BITLEN) {
zlog_err(
"Invalid prefix length: %u received from kernel interface addr change: %s",
ifa->ifa_prefixlen,
nl_msg_type_to_str(h->nlmsg_type));
return -1;
}
if (h->nlmsg_type == RTM_NEWADDR) {
/* Only consider valid addresses; we'll not get a
* notification from
* the kernel till IPv6 DAD has completed, but at init
* time, Quagga
* does query for and will receive all addresses.
*/
if (!(kernel_flags
& (IFA_F_DADFAILED | IFA_F_TENTATIVE)))
connected_add_ipv6(ifp, flags,
(struct in6_addr *)addr,
(struct in6_addr *)broad,
ifa->ifa_prefixlen, label,
metric);
} else
connected_delete_ipv6(ifp, (struct in6_addr *)addr,
NULL, ifa->ifa_prefixlen);
}
/*
* Linux kernel does not send route delete on interface down/addr del
* so we have to re-process routes it owns (i.e. kernel routes)
*/
if (h->nlmsg_type != RTM_NEWADDR)
rib_update(RIB_UPDATE_KERNEL);
return 0;
}
/*
* Parse and validate an incoming interface address change message,
* generating a dplane context object.
* This runs in the dplane pthread; the context is enqueued to the
* main pthread for processing.
*/
int netlink_interface_addr_dplane(struct nlmsghdr *h, ns_id_t ns_id,
int startup /*ignored*/)
{
int len;
struct ifaddrmsg *ifa;
struct rtattr *tb[IFA_MAX + 1];
void *addr;
void *broad;
char *label = NULL;
uint32_t metric = METRIC_MAX;
uint32_t kernel_flags = 0;
struct zebra_dplane_ctx *ctx;
struct prefix p;
ifa = NLMSG_DATA(h);
/* Validate message types */
if (h->nlmsg_type != RTM_NEWADDR && h->nlmsg_type != RTM_DELADDR)
return 0;
if (ifa->ifa_family != AF_INET && ifa->ifa_family != AF_INET6) {
if (IS_ZEBRA_DEBUG_KERNEL)
zlog_debug("%s: %s: Invalid address family: %u",
__func__, nl_msg_type_to_str(h->nlmsg_type),
ifa->ifa_family);
return 0;
}
len = h->nlmsg_len - NLMSG_LENGTH(sizeof(struct ifaddrmsg));
if (len < 0) {
if (IS_ZEBRA_DEBUG_KERNEL)
zlog_debug("%s: %s: netlink msg bad size: %d %zu",
__func__, nl_msg_type_to_str(h->nlmsg_type),
h->nlmsg_len,
(size_t)NLMSG_LENGTH(
sizeof(struct ifaddrmsg)));
return -1;
}
netlink_parse_rtattr(tb, IFA_MAX, IFA_RTA(ifa), len);
/* Flags passed through */
if (tb[IFA_FLAGS])
kernel_flags = *(int *)RTA_DATA(tb[IFA_FLAGS]);
else
kernel_flags = ifa->ifa_flags;
if (IS_ZEBRA_DEBUG_KERNEL) { /* remove this line to see initial ifcfg */
char buf[PREFIX_STRLEN];
zlog_debug("%s: %s nsid %u ifindex %u flags 0x%x:", __func__,
nl_msg_type_to_str(h->nlmsg_type), ns_id,
ifa->ifa_index, kernel_flags);
if (tb[IFA_LOCAL])
zlog_debug(" IFA_LOCAL %s/%d",
inet_ntop(ifa->ifa_family,
RTA_DATA(tb[IFA_LOCAL]), buf,
sizeof(buf)),
ifa->ifa_prefixlen);
if (tb[IFA_ADDRESS])
zlog_debug(" IFA_ADDRESS %s/%d",
inet_ntop(ifa->ifa_family,
RTA_DATA(tb[IFA_ADDRESS]), buf,
sizeof(buf)),
ifa->ifa_prefixlen);
if (tb[IFA_BROADCAST])
zlog_debug(" IFA_BROADCAST %s/%d",
inet_ntop(ifa->ifa_family,
RTA_DATA(tb[IFA_BROADCAST]), buf,
sizeof(buf)),
ifa->ifa_prefixlen);
if (tb[IFA_LABEL])
zlog_debug(" IFA_LABEL %s",
(const char *)RTA_DATA(tb[IFA_LABEL]));
if (tb[IFA_CACHEINFO]) {
struct ifa_cacheinfo *ci = RTA_DATA(tb[IFA_CACHEINFO]);
zlog_debug(" IFA_CACHEINFO pref %d, valid %d",
ci->ifa_prefered, ci->ifa_valid);
}
}
/* Validate prefix length */
if (ifa->ifa_family == AF_INET
&& ifa->ifa_prefixlen > IPV4_MAX_BITLEN) {
if (IS_ZEBRA_DEBUG_KERNEL)
zlog_debug("%s: %s: Invalid prefix length: %u",
__func__, nl_msg_type_to_str(h->nlmsg_type),
ifa->ifa_prefixlen);
return -1;
}
if (ifa->ifa_family == AF_INET6) {
if (ifa->ifa_prefixlen > IPV6_MAX_BITLEN) {
if (IS_ZEBRA_DEBUG_KERNEL)
zlog_debug("%s: %s: Invalid prefix length: %u",
__func__,
nl_msg_type_to_str(h->nlmsg_type),
ifa->ifa_prefixlen);
return -1;
}
/* Only consider valid addresses; we'll not get a kernel
* notification till IPv6 DAD has completed, but at init
* time, FRR does query for and will receive all addresses.
*/
if (h->nlmsg_type == RTM_NEWADDR
&& (kernel_flags & (IFA_F_DADFAILED | IFA_F_TENTATIVE))) {
if (IS_ZEBRA_DEBUG_KERNEL)
zlog_debug("%s: %s: Invalid/tentative addr",
__func__,
nl_msg_type_to_str(h->nlmsg_type));
return 0;
}
}
/* logic copied from iproute2/ip/ipaddress.c:print_addrinfo() */
if (tb[IFA_LOCAL] == NULL)
tb[IFA_LOCAL] = tb[IFA_ADDRESS];
if (tb[IFA_ADDRESS] == NULL)
tb[IFA_ADDRESS] = tb[IFA_LOCAL];
/* local interface address */
addr = (tb[IFA_LOCAL] ? RTA_DATA(tb[IFA_LOCAL]) : NULL);
/* addr is primary key, SOL if we don't have one */
if (addr == NULL) {
if (IS_ZEBRA_DEBUG_KERNEL)
zlog_debug("%s: %s: No local interface address",
__func__, nl_msg_type_to_str(h->nlmsg_type));
return -1;
}
/* Allocate a context object, now that validation is done. */
ctx = dplane_ctx_alloc();
if (h->nlmsg_type == RTM_NEWADDR)
dplane_ctx_set_op(ctx, DPLANE_OP_INTF_ADDR_ADD);
else
dplane_ctx_set_op(ctx, DPLANE_OP_INTF_ADDR_DEL);
dplane_ctx_set_ifindex(ctx, ifa->ifa_index);
dplane_ctx_set_ns_id(ctx, ns_id);
/* Convert addr to prefix */
memset(&p, 0, sizeof(p));
p.family = ifa->ifa_family;
p.prefixlen = ifa->ifa_prefixlen;
if (p.family == AF_INET)
p.u.prefix4 = *(struct in_addr *)addr;
else
p.u.prefix6 = *(struct in6_addr *)addr;
dplane_ctx_set_intf_addr(ctx, &p);
/* is there a peer address? */
if (tb[IFA_ADDRESS]
&& memcmp(RTA_DATA(tb[IFA_ADDRESS]), RTA_DATA(tb[IFA_LOCAL]),
RTA_PAYLOAD(tb[IFA_ADDRESS]))) {
broad = RTA_DATA(tb[IFA_ADDRESS]);
dplane_ctx_intf_set_connected(ctx);
} else if (tb[IFA_BROADCAST]) {
/* seeking a broadcast address */
broad = RTA_DATA(tb[IFA_BROADCAST]);
dplane_ctx_intf_set_broadcast(ctx);
} else
broad = NULL;
if (broad) {
/* Convert addr to prefix */
memset(&p, 0, sizeof(p));
p.family = ifa->ifa_family;
p.prefixlen = ifa->ifa_prefixlen;
if (p.family == AF_INET)
p.u.prefix4 = *(struct in_addr *)broad;
else
p.u.prefix6 = *(struct in6_addr *)broad;
dplane_ctx_set_intf_dest(ctx, &p);
}
/* Flags. */
if (kernel_flags & IFA_F_SECONDARY)
dplane_ctx_intf_set_secondary(ctx);
/* Label */
if (tb[IFA_LABEL]) {
label = (char *)RTA_DATA(tb[IFA_LABEL]);
dplane_ctx_set_intf_label(ctx, label);
}
if (tb[IFA_RT_PRIORITY])
metric = *(uint32_t *)RTA_DATA(tb[IFA_RT_PRIORITY]);
dplane_ctx_set_intf_metric(ctx, metric);
/* Enqueue ctx for main pthread to process */
dplane_provider_enqueue_to_zebra(ctx);
return 0;
}
int netlink_link_change(struct nlmsghdr *h, ns_id_t ns_id, int startup)
{
int len;
struct ifinfomsg *ifi;
struct rtattr *tb[IFLA_MAX + 1];
struct rtattr *linkinfo[IFLA_MAX + 1];
struct interface *ifp;
char *name = NULL;
char *kind = NULL;
char *desc = NULL;
char *slave_kind = NULL;
struct zebra_ns *zns;
vrf_id_t vrf_id = VRF_DEFAULT;
enum zebra_iftype zif_type = ZEBRA_IF_OTHER;
enum zebra_slave_iftype zif_slave_type = ZEBRA_IF_SLAVE_NONE;
ifindex_t bridge_ifindex = IFINDEX_INTERNAL;
ifindex_t bond_ifindex = IFINDEX_INTERNAL;
ifindex_t link_ifindex = IFINDEX_INTERNAL;
uint8_t old_hw_addr[INTERFACE_HWADDR_MAX];
struct zebra_if *zif;
ns_id_t link_nsid = ns_id;
ifindex_t master_infindex = IFINDEX_INTERNAL;
uint8_t bypass = 0;
zns = zebra_ns_lookup(ns_id);
ifi = NLMSG_DATA(h);
/* assume if not default zns, then new VRF */
if (!(h->nlmsg_type == RTM_NEWLINK || h->nlmsg_type == RTM_DELLINK)) {
/* If this is not link add/delete message so print warning. */
zlog_debug("%s: wrong kernel message %s", __func__,
nl_msg_type_to_str(h->nlmsg_type));
return 0;
}
if (!(ifi->ifi_family == AF_UNSPEC || ifi->ifi_family == AF_BRIDGE
|| ifi->ifi_family == AF_INET6)) {
flog_warn(
EC_ZEBRA_UNKNOWN_FAMILY,
"Invalid address family: %u received from kernel link change: %s",
ifi->ifi_family, nl_msg_type_to_str(h->nlmsg_type));
return 0;
}
len = h->nlmsg_len - NLMSG_LENGTH(sizeof(struct ifinfomsg));
if (len < 0) {
zlog_err(
"%s: Message received from netlink is of a broken size %d %zu",
__func__, h->nlmsg_len,
(size_t)NLMSG_LENGTH(sizeof(struct ifinfomsg)));
return -1;
}
/* We are interested in some AF_BRIDGE notifications. */
if (ifi->ifi_family == AF_BRIDGE)
return netlink_bridge_interface(h, len, ns_id, startup);
/* Looking up interface name. */
memset(linkinfo, 0, sizeof(linkinfo));
netlink_parse_rtattr_flags(tb, IFLA_MAX, IFLA_RTA(ifi), len,
NLA_F_NESTED);
/* check for wireless messages to ignore */
if ((tb[IFLA_WIRELESS] != NULL) && (ifi->ifi_change == 0)) {
if (IS_ZEBRA_DEBUG_KERNEL)
zlog_debug("%s: ignoring IFLA_WIRELESS message",
__func__);
return 0;
}
if (tb[IFLA_IFNAME] == NULL)
return -1;
name = (char *)RTA_DATA(tb[IFLA_IFNAME]);
/* Must be valid string. */
len = RTA_PAYLOAD(tb[IFLA_IFNAME]);
if (len < 2 || name[len - 1] != '\0') {
if (IS_ZEBRA_DEBUG_KERNEL)
zlog_debug("%s: invalid intf name", __func__);
return -1;
}
if (tb[IFLA_LINKINFO]) {
netlink_parse_rtattr_nested(linkinfo, IFLA_INFO_MAX,
tb[IFLA_LINKINFO]);
if (linkinfo[IFLA_INFO_KIND])
kind = RTA_DATA(linkinfo[IFLA_INFO_KIND]);
if (linkinfo[IFLA_INFO_SLAVE_KIND])
slave_kind = RTA_DATA(linkinfo[IFLA_INFO_SLAVE_KIND]);
netlink_determine_zebra_iftype(kind, &zif_type);
}
/* If linking to another interface, note it. */
if (tb[IFLA_LINK])
link_ifindex = *(ifindex_t *)RTA_DATA(tb[IFLA_LINK]);
if (tb[IFLA_LINK_NETNSID]) {
link_nsid = *(ns_id_t *)RTA_DATA(tb[IFLA_LINK_NETNSID]);
link_nsid = ns_id_get_absolute(ns_id, link_nsid);
}
if (tb[IFLA_IFALIAS]) {
desc = (char *)RTA_DATA(tb[IFLA_IFALIAS]);
}
/* See if interface is present. */
ifp = if_lookup_by_name_per_ns(zns, name);
if (h->nlmsg_type == RTM_NEWLINK) {
/* If VRF, create or update the VRF structure itself. */
if (zif_type == ZEBRA_IF_VRF && !vrf_is_backend_netns()) {
netlink_vrf_change(h, tb[IFLA_LINKINFO], ns_id, name);
vrf_id = (vrf_id_t)ifi->ifi_index;
}
if (tb[IFLA_MASTER]) {
if (slave_kind && (strcmp(slave_kind, "vrf") == 0)
&& !vrf_is_backend_netns()) {
zif_slave_type = ZEBRA_IF_SLAVE_VRF;
master_infindex = vrf_id =
*(uint32_t *)RTA_DATA(tb[IFLA_MASTER]);
} else if (slave_kind
&& (strcmp(slave_kind, "bridge") == 0)) {
zif_slave_type = ZEBRA_IF_SLAVE_BRIDGE;
master_infindex = bridge_ifindex =
*(ifindex_t *)RTA_DATA(tb[IFLA_MASTER]);
} else if (slave_kind
&& (strcmp(slave_kind, "bond") == 0)) {
zif_slave_type = ZEBRA_IF_SLAVE_BOND;
master_infindex = bond_ifindex =
*(ifindex_t *)RTA_DATA(tb[IFLA_MASTER]);
bypass = netlink_parse_lacp_bypass(linkinfo);
} else
zif_slave_type = ZEBRA_IF_SLAVE_OTHER;
}
if (vrf_is_backend_netns())
vrf_id = (vrf_id_t)ns_id;
if (ifp == NULL
|| !CHECK_FLAG(ifp->status, ZEBRA_INTERFACE_ACTIVE)) {
/* Add interface notification from kernel */
if (IS_ZEBRA_DEBUG_KERNEL)
zlog_debug(
"RTM_NEWLINK ADD for %s(%u) vrf_id %u type %d sl_type %d master %u flags 0x%x",
name, ifi->ifi_index, vrf_id, zif_type,
zif_slave_type, master_infindex,
ifi->ifi_flags);
if (ifp == NULL) {
/* unknown interface */
ifp = if_get_by_name(name, vrf_id, NULL);
} else {
/* pre-configured interface, learnt now */
if (ifp->vrf->vrf_id != vrf_id)
if_update_to_new_vrf(ifp, vrf_id);
}
/* Update interface information. */
set_ifindex(ifp, ifi->ifi_index, zns);
ifp->flags = ifi->ifi_flags & 0x0000fffff;
if (!tb[IFLA_MTU]) {
zlog_debug(
"RTM_NEWLINK for interface %s(%u) without MTU set",
name, ifi->ifi_index);
return 0;
}
ifp->mtu6 = ifp->mtu = *(int *)RTA_DATA(tb[IFLA_MTU]);
ifp->metric = 0;
ifp->ptm_status = ZEBRA_PTM_STATUS_UNKNOWN;
/* Set interface type */
zebra_if_set_ziftype(ifp, zif_type, zif_slave_type);
if (IS_ZEBRA_IF_VRF(ifp))
SET_FLAG(ifp->status,
ZEBRA_INTERFACE_VRF_LOOPBACK);
/* Update link. */
zebra_if_update_link(ifp, link_ifindex, link_nsid);
ifp->ll_type =
netlink_to_zebra_link_type(ifi->ifi_type);
netlink_interface_update_hw_addr(tb, ifp);
/* Inform clients, install any configured addresses. */
if_add_update(ifp);
/* Extract and save L2 interface information, take
* additional actions. */
netlink_interface_update_l2info(
ifp, linkinfo[IFLA_INFO_DATA],
1, link_nsid);
if (IS_ZEBRA_IF_BRIDGE_SLAVE(ifp))
zebra_l2if_update_bridge_slave(
ifp, bridge_ifindex, ns_id,
ZEBRA_BRIDGE_NO_ACTION);
else if (IS_ZEBRA_IF_BOND_SLAVE(ifp))
zebra_l2if_update_bond_slave(ifp, bond_ifindex,
!!bypass);
if (tb[IFLA_PROTO_DOWN])
netlink_proc_dplane_if_protodown(ifp->info, tb);
} else if (ifp->vrf->vrf_id != vrf_id) {
/* VRF change for an interface. */
if (IS_ZEBRA_DEBUG_KERNEL)
zlog_debug(
"RTM_NEWLINK vrf-change for %s(%u) vrf_id %u -> %u flags 0x%x",
name, ifp->ifindex, ifp->vrf->vrf_id,
vrf_id, ifi->ifi_flags);
if_handle_vrf_change(ifp, vrf_id);
} else {
bool was_bridge_slave, was_bond_slave;
uint8_t chgflags = ZEBRA_BRIDGE_NO_ACTION;
zif = ifp->info;
/* Interface update. */
if (IS_ZEBRA_DEBUG_KERNEL)
zlog_debug(
"RTM_NEWLINK update for %s(%u) sl_type %d master %u flags 0x%x",
name, ifp->ifindex, zif_slave_type,
master_infindex, ifi->ifi_flags);
set_ifindex(ifp, ifi->ifi_index, zns);
if (!tb[IFLA_MTU]) {
zlog_debug(
"RTM_NEWLINK for interface %s(%u) without MTU set",
name, ifi->ifi_index);
return 0;
}
ifp->mtu6 = ifp->mtu = *(int *)RTA_DATA(tb[IFLA_MTU]);
ifp->metric = 0;
/* Update interface type - NOTE: Only slave_type can
* change. */
was_bridge_slave = IS_ZEBRA_IF_BRIDGE_SLAVE(ifp);
was_bond_slave = IS_ZEBRA_IF_BOND_SLAVE(ifp);
zebra_if_set_ziftype(ifp, zif_type, zif_slave_type);
memcpy(old_hw_addr, ifp->hw_addr, INTERFACE_HWADDR_MAX);
/* Update link. */
zebra_if_update_link(ifp, link_ifindex, link_nsid);
ifp->ll_type =
netlink_to_zebra_link_type(ifi->ifi_type);
netlink_interface_update_hw_addr(tb, ifp);
if (tb[IFLA_PROTO_DOWN])
netlink_proc_dplane_if_protodown(ifp->info, tb);
if (if_is_no_ptm_operative(ifp)) {
bool is_up = if_is_operative(ifp);
ifp->flags = ifi->ifi_flags & 0x0000fffff;
if (!if_is_no_ptm_operative(ifp) ||
CHECK_FLAG(zif->flags,
ZIF_FLAG_PROTODOWN)) {
if (IS_ZEBRA_DEBUG_KERNEL)
zlog_debug(
"Intf %s(%u) has gone DOWN",
name, ifp->ifindex);
if_down(ifp);
rib_update(RIB_UPDATE_KERNEL);
} else if (if_is_operative(ifp)) {
bool mac_updated = false;
/* Must notify client daemons of new
* interface status. */
if (IS_ZEBRA_DEBUG_KERNEL)
zlog_debug(
"Intf %s(%u) PTM up, notifying clients",
name, ifp->ifindex);
if_up(ifp, !is_up);
/* Update EVPN VNI when SVI MAC change
*/
if (memcmp(old_hw_addr, ifp->hw_addr,
INTERFACE_HWADDR_MAX))
mac_updated = true;
if (IS_ZEBRA_IF_VLAN(ifp)
&& mac_updated) {
struct interface *link_if;
link_if =
if_lookup_by_index_per_ns(
zebra_ns_lookup(NS_DEFAULT),
link_ifindex);
if (link_if)
zebra_vxlan_svi_up(ifp,
link_if);
} else if (mac_updated
&& IS_ZEBRA_IF_BRIDGE(ifp)) {
zlog_debug(
"Intf %s(%u) bridge changed MAC address",
name, ifp->ifindex);
chgflags =
ZEBRA_BRIDGE_MASTER_MAC_CHANGE;
}
}
} else {
ifp->flags = ifi->ifi_flags & 0x0000fffff;
if (if_is_operative(ifp) &&
!CHECK_FLAG(zif->flags,
ZIF_FLAG_PROTODOWN)) {
if (IS_ZEBRA_DEBUG_KERNEL)
zlog_debug(
"Intf %s(%u) has come UP",
name, ifp->ifindex);
if_up(ifp, true);
if (IS_ZEBRA_IF_BRIDGE(ifp))
chgflags =
ZEBRA_BRIDGE_MASTER_UP;
} else {
if (IS_ZEBRA_DEBUG_KERNEL)
zlog_debug(
"Intf %s(%u) has gone DOWN",
name, ifp->ifindex);
if_down(ifp);
rib_update(RIB_UPDATE_KERNEL);
}
}
/* Extract and save L2 interface information, take
* additional actions. */
netlink_interface_update_l2info(
ifp, linkinfo[IFLA_INFO_DATA],
0, link_nsid);
if (IS_ZEBRA_IF_BRIDGE(ifp))
zebra_l2if_update_bridge(ifp, chgflags);
if (IS_ZEBRA_IF_BOND(ifp))
zebra_l2if_update_bond(ifp, true);
if (IS_ZEBRA_IF_BRIDGE_SLAVE(ifp) || was_bridge_slave)
zebra_l2if_update_bridge_slave(
ifp, bridge_ifindex, ns_id, chgflags);
else if (IS_ZEBRA_IF_BOND_SLAVE(ifp) || was_bond_slave)
zebra_l2if_update_bond_slave(ifp, bond_ifindex,
!!bypass);
}
zif = ifp->info;
if (zif) {
XFREE(MTYPE_ZIF_DESC, zif->desc);
if (desc)
zif->desc = XSTRDUP(MTYPE_ZIF_DESC, desc);
}
} else {
/* Delete interface notification from kernel */
if (ifp == NULL) {
if (IS_ZEBRA_DEBUG_KERNEL)
zlog_debug(
"RTM_DELLINK for unknown interface %s(%u)",
name, ifi->ifi_index);
return 0;
}
if (IS_ZEBRA_DEBUG_KERNEL)
zlog_debug("RTM_DELLINK for %s(%u)", name,
ifp->ifindex);
UNSET_FLAG(ifp->status, ZEBRA_INTERFACE_VRF_LOOPBACK);
if (IS_ZEBRA_IF_BOND(ifp))
zebra_l2if_update_bond(ifp, false);
if (IS_ZEBRA_IF_BOND_SLAVE(ifp))
zebra_l2if_update_bond_slave(ifp, bond_ifindex, false);
/* Special handling for bridge or VxLAN interfaces. */
if (IS_ZEBRA_IF_BRIDGE(ifp))
zebra_l2_bridge_del(ifp);
else if (IS_ZEBRA_IF_VXLAN(ifp))
zebra_l2_vxlanif_del(ifp);
if_delete_update(&ifp);
/* If VRF, delete the VRF structure itself. */
if (zif_type == ZEBRA_IF_VRF && !vrf_is_backend_netns())
netlink_vrf_change(h, tb[IFLA_LINKINFO], ns_id, name);
}
return 0;
}
/**
* Interface encoding helper function.
*
* \param[in] cmd netlink command.
* \param[in] ctx dataplane context (information snapshot).
* \param[out] buf buffer to hold the packet.
* \param[in] buflen amount of buffer bytes.
*/
ssize_t netlink_intf_msg_encode(uint16_t cmd,
const struct zebra_dplane_ctx *ctx, void *buf,
size_t buflen)
{
struct {
struct nlmsghdr n;
struct ifinfomsg ifa;
char buf[];
} *req = buf;
struct rtattr *nest_protodown_reason;
ifindex_t ifindex = dplane_ctx_get_ifindex(ctx);
bool down = dplane_ctx_intf_is_protodown(ctx);
bool pd_reason_val = dplane_ctx_get_intf_pd_reason_val(ctx);
struct nlsock *nl =
kernel_netlink_nlsock_lookup(dplane_ctx_get_ns_sock(ctx));
if (buflen < sizeof(*req))
return 0;
memset(req, 0, sizeof(*req));
if (cmd != RTM_SETLINK)
flog_err(
EC_ZEBRA_INTF_UPDATE_FAILURE,
"Only RTM_SETLINK message type currently supported in dplane pthread");
req->n.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifinfomsg));
req->n.nlmsg_flags = NLM_F_REQUEST;
req->n.nlmsg_type = cmd;
req->n.nlmsg_pid = nl->snl.nl_pid;
req->ifa.ifi_index = ifindex;
nl_attr_put8(&req->n, buflen, IFLA_PROTO_DOWN, down);
nl_attr_put32(&req->n, buflen, IFLA_LINK, ifindex);
/* Reason info nest */
nest_protodown_reason =
nl_attr_nest(&req->n, buflen, IFLA_PROTO_DOWN_REASON);
if (!nest_protodown_reason)
return -1;
nl_attr_put32(&req->n, buflen, IFLA_PROTO_DOWN_REASON_MASK,
(1 << frr_protodown_r_bit));
nl_attr_put32(&req->n, buflen, IFLA_PROTO_DOWN_REASON_VALUE,
((int)pd_reason_val) << frr_protodown_r_bit);
nl_attr_nest_end(&req->n, nest_protodown_reason);
if (IS_ZEBRA_DEBUG_KERNEL)
zlog_debug("%s: %s, protodown=%d reason_val=%d ifindex=%u",
__func__, nl_msg_type_to_str(cmd), down,
pd_reason_val, ifindex);
return NLMSG_ALIGN(req->n.nlmsg_len);
}
/* Interface information read by netlink. */
void interface_list(struct zebra_ns *zns)
{
interface_lookup_netlink(zns);
/* We add routes for interface address,
* so we need to get the nexthop info
* from the kernel before we can do that
*/
netlink_nexthop_read(zns);
interface_addr_lookup_netlink(zns);
}
void if_netlink_set_frr_protodown_r_bit(uint8_t bit)
{
if (IS_ZEBRA_DEBUG_KERNEL)
zlog_debug(
"Protodown reason bit index changed: bit-index %u -> bit-index %u",
frr_protodown_r_bit, bit);
frr_protodown_r_bit = bit;
}
void if_netlink_unset_frr_protodown_r_bit(void)
{
if (IS_ZEBRA_DEBUG_KERNEL)
zlog_debug(
"Protodown reason bit index changed: bit-index %u -> bit-index %u",
frr_protodown_r_bit, FRR_PROTODOWN_REASON_DEFAULT_BIT);
frr_protodown_r_bit = FRR_PROTODOWN_REASON_DEFAULT_BIT;
}
bool if_netlink_frr_protodown_r_bit_is_set(void)
{
return (frr_protodown_r_bit != FRR_PROTODOWN_REASON_DEFAULT_BIT);
}
uint8_t if_netlink_get_frr_protodown_r_bit(void)
{
return frr_protodown_r_bit;
}
/**
* netlink_request_tunneldump() - Request all tunnels from the linux kernel
*
* @zns: Zebra namespace
* @family: AF_* netlink family
* @type: RTM_* (RTM_GETTUNNEL) route type
*
* Return: Result status
*/
static int netlink_request_tunneldump(struct zebra_ns *zns, int family,
int ifindex)
{
struct {
struct nlmsghdr n;
struct tunnel_msg tmsg;
char buf[256];
} req;
/* Form the request */
memset(&req, 0, sizeof(req));
req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct tunnel_msg));
req.n.nlmsg_type = RTM_GETTUNNEL;
req.n.nlmsg_flags = NLM_F_ROOT | NLM_F_MATCH | NLM_F_REQUEST;
req.tmsg.family = family;
req.tmsg.ifindex = ifindex;
return netlink_request(&zns->netlink_cmd, &req);
}
/*
* Currently we only ask for vxlan l3svd vni information.
* In the future this can be expanded.
*/
int netlink_tunneldump_read(struct zebra_ns *zns)
{
int ret = 0;
struct zebra_dplane_info dp_info;
struct route_node *rn;
struct interface *tmp_if = NULL;
struct zebra_if *zif;
struct nlsock *netlink_cmd = &zns->netlink_cmd;
zebra_dplane_info_from_zns(&dp_info, zns, true /*is_cmd*/);
for (rn = route_top(zns->if_table); rn; rn = route_next(rn)) {
tmp_if = (struct interface *)rn->info;
if (!tmp_if)
continue;
zif = tmp_if->info;
if (!zif || zif->zif_type != ZEBRA_IF_VXLAN)
continue;
ret = netlink_request_tunneldump(zns, PF_BRIDGE,
tmp_if->ifindex);
if (ret < 0)
return ret;
ret = netlink_parse_info(netlink_interface, netlink_cmd,
&dp_info, 0, true);
if (ret < 0)
return ret;
}
return 0;
}
static const char *port_state2str(uint8_t state)
{
switch (state) {
case BR_STATE_DISABLED:
return "DISABLED";
case BR_STATE_LISTENING:
return "LISTENING";
case BR_STATE_LEARNING:
return "LEARNING";
case BR_STATE_FORWARDING:
return "FORWARDING";
case BR_STATE_BLOCKING:
return "BLOCKING";
}
return "UNKNOWN";
}
static void vxlan_vni_state_change(struct zebra_if *zif, uint16_t id,
uint8_t state)
{
struct zebra_vxlan_vni *vnip;
vnip = zebra_vxlan_if_vlanid_vni_find(zif, id);
if (!vnip) {
if (IS_ZEBRA_DEBUG_VXLAN)
zlog_debug(
"Cannot find VNI for VID (%u) IF %s for vlan state update",
id, zif->ifp->name);
return;
}
switch (state) {
case BR_STATE_FORWARDING:
zebra_vxlan_if_vni_up(zif->ifp, vnip);
break;
case BR_STATE_BLOCKING:
zebra_vxlan_if_vni_down(zif->ifp, vnip);
break;
case BR_STATE_DISABLED:
case BR_STATE_LISTENING:
case BR_STATE_LEARNING:
default:
/* Not used for anything at the moment */
break;
}
}
static void vlan_id_range_state_change(struct interface *ifp, uint16_t id_start,
uint16_t id_end, uint8_t state)
{
struct zebra_if *zif;
zif = (struct zebra_if *)ifp->info;
if (!zif)
return;
for (uint16_t i = id_start; i <= id_end; i++)
vxlan_vni_state_change(zif, i, state);
}
/**
* netlink_vlan_change() - Read in change about vlans from the kernel
*
* @h: Netlink message header
* @ns_id: Namspace id
* @startup: Are we reading under startup conditions?
*
* Return: Result status
*/
int netlink_vlan_change(struct nlmsghdr *h, ns_id_t ns_id, int startup)
{
int len, rem;
struct interface *ifp;
struct br_vlan_msg *bvm;
struct bridge_vlan_info *vinfo;
struct rtattr *vtb[BRIDGE_VLANDB_ENTRY_MAX + 1] = {};
struct rtattr *attr;
uint8_t state;
uint32_t vrange;
int type;
/* We only care about state changes for now */
if (!(h->nlmsg_type == RTM_NEWVLAN))
return 0;
len = h->nlmsg_len - NLMSG_LENGTH(sizeof(struct br_vlan_msg));
if (len < 0) {
zlog_warn(
"%s: Message received from netlink is of a broken size %d %zu",
__func__, h->nlmsg_len,
(size_t)NLMSG_LENGTH(sizeof(struct br_vlan_msg)));
return -1;
}
bvm = NLMSG_DATA(h);
if (bvm->family != AF_BRIDGE)
return 0;
ifp = if_lookup_by_index_per_ns(zebra_ns_lookup(ns_id), bvm->ifindex);
if (!ifp) {
zlog_debug("Cannot find bridge-vlan IF (%u) for vlan update",
bvm->ifindex);
return 0;
}
if (!IS_ZEBRA_IF_VXLAN(ifp)) {
if (IS_ZEBRA_DEBUG_KERNEL)
zlog_debug("Ignoring non-vxlan IF (%s) for vlan update",
ifp->name);
return 0;
}
if (IS_ZEBRA_DEBUG_KERNEL || IS_ZEBRA_DEBUG_VXLAN)
zlog_debug("%s %s IF %s NS %u",
nl_msg_type_to_str(h->nlmsg_type),
nl_family_to_str(bvm->family), ifp->name, ns_id);
/* Loop over "ALL" BRIDGE_VLANDB_ENTRY */
rem = len;
for (attr = BRVLAN_RTA(bvm); RTA_OK(attr, rem);
attr = RTA_NEXT(attr, rem)) {
vinfo = NULL;
vrange = 0;
type = attr->rta_type & NLA_TYPE_MASK;
if (type != BRIDGE_VLANDB_ENTRY)
continue;
/* Parse nested entry data */
netlink_parse_rtattr_nested(vtb, BRIDGE_VLANDB_ENTRY_MAX, attr);
/* It must have info for the ID */
if (!vtb[BRIDGE_VLANDB_ENTRY_INFO])
continue;
vinfo = (struct bridge_vlan_info *)RTA_DATA(
vtb[BRIDGE_VLANDB_ENTRY_INFO]);
/*
* We only care about state info, if there is none, just ignore
* it.
*/
if (!vtb[BRIDGE_VLANDB_ENTRY_STATE])
continue;
state = *(uint8_t *)RTA_DATA(vtb[BRIDGE_VLANDB_ENTRY_STATE]);
if (vtb[BRIDGE_VLANDB_ENTRY_RANGE])
vrange = *(uint32_t *)RTA_DATA(
vtb[BRIDGE_VLANDB_ENTRY_RANGE]);
if (IS_ZEBRA_DEBUG_KERNEL || IS_ZEBRA_DEBUG_VXLAN) {
if (vrange)
zlog_debug("VLANDB_ENTRY: VID (%u-%u) state=%s",
vinfo->vid, vrange,
port_state2str(state));
else
zlog_debug("VLANDB_ENTRY: VID (%u) state=%s",
vinfo->vid, port_state2str(state));
}
vlan_id_range_state_change(
ifp, vinfo->vid, (vrange ? vrange : vinfo->vid), state);
}
return 0;
}
/**
* netlink_request_vlan() - Request vlan information from the kernel
* @zns: Zebra namespace
* @family: AF_* netlink family
* @type: RTM_* type
*
* Return: Result status
*/
static int netlink_request_vlan(struct zebra_ns *zns, int family, int type)
{
struct {
struct nlmsghdr n;
struct br_vlan_msg bvm;
char buf[256];
} req;
/* Form the request, specifying filter (rtattr) if needed. */
memset(&req, 0, sizeof(req));
req.n.nlmsg_type = type;
req.n.nlmsg_flags = NLM_F_ROOT | NLM_F_MATCH | NLM_F_REQUEST;
req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct br_vlan_msg));
req.bvm.family = family;
nl_attr_put32(&req.n, sizeof(req), BRIDGE_VLANDB_DUMP_FLAGS,
BRIDGE_VLANDB_DUMPF_STATS);
return netlink_request(&zns->netlink_cmd, &req);
}
/**
* netlink_vlan_read() - Vlan read function using netlink interface
*
* @zns: Zebra name space
*
* Return: Result status
* Only called at bootstrap time.
*/
int netlink_vlan_read(struct zebra_ns *zns)
{
int ret;
struct zebra_dplane_info dp_info;
zebra_dplane_info_from_zns(&dp_info, zns, true /*is_cmd*/);
/* Get bridg vlan info */
ret = netlink_request_vlan(zns, PF_BRIDGE, RTM_GETVLAN);
if (ret < 0)
return ret;
ret = netlink_parse_info(netlink_vlan_change, &zns->netlink_cmd,
&dp_info, 0, 1);
return ret;
}
#endif /* GNU_LINUX */
|