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
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
|
/* Routing Information Base.
* Copyright (C) 1997, 98, 99, 2001 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>
#include "command.h"
#include "if.h"
#include "linklist.h"
#include "log.h"
#include "log_int.h"
#include "memory.h"
#include "mpls.h"
#include "nexthop.h"
#include "prefix.h"
#include "prefix.h"
#include "routemap.h"
#include "sockunion.h"
#include "srcdest_table.h"
#include "table.h"
#include "thread.h"
#include "vrf.h"
#include "workqueue.h"
#include "zebra/connected.h"
#include "zebra/debug.h"
#include "zebra/interface.h"
#include "zebra/redistribute.h"
#include "zebra/rib.h"
#include "zebra/rt.h"
#include "zebra/zapi_msg.h"
#include "zebra/zebra_errors.h"
#include "zebra/zebra_memory.h"
#include "zebra/zebra_ns.h"
#include "zebra/zebra_rnh.h"
#include "zebra/zebra_routemap.h"
#include "zebra/zebra_vrf.h"
#include "zebra/zebra_vxlan.h"
DEFINE_HOOK(rib_update, (struct route_node * rn, const char *reason),
(rn, reason))
/* Should we allow non Quagga processes to delete our routes */
extern int allow_delete;
/* Each route type's string and default distance value. */
static const struct {
int key;
int distance;
} route_info[ZEBRA_ROUTE_MAX] = {
[ZEBRA_ROUTE_SYSTEM] = {ZEBRA_ROUTE_SYSTEM, 0},
[ZEBRA_ROUTE_KERNEL] = {ZEBRA_ROUTE_KERNEL, 0},
[ZEBRA_ROUTE_CONNECT] = {ZEBRA_ROUTE_CONNECT, 0},
[ZEBRA_ROUTE_STATIC] = {ZEBRA_ROUTE_STATIC, 1},
[ZEBRA_ROUTE_RIP] = {ZEBRA_ROUTE_RIP, 120},
[ZEBRA_ROUTE_RIPNG] = {ZEBRA_ROUTE_RIPNG, 120},
[ZEBRA_ROUTE_OSPF] = {ZEBRA_ROUTE_OSPF, 110},
[ZEBRA_ROUTE_OSPF6] = {ZEBRA_ROUTE_OSPF6, 110},
[ZEBRA_ROUTE_ISIS] = {ZEBRA_ROUTE_ISIS, 115},
[ZEBRA_ROUTE_BGP] = {ZEBRA_ROUTE_BGP, 20 /* IBGP is 200. */},
[ZEBRA_ROUTE_PIM] = {ZEBRA_ROUTE_PIM, 255},
[ZEBRA_ROUTE_EIGRP] = {ZEBRA_ROUTE_EIGRP, 90},
[ZEBRA_ROUTE_NHRP] = {ZEBRA_ROUTE_NHRP, 10},
[ZEBRA_ROUTE_HSLS] = {ZEBRA_ROUTE_HSLS, 255},
[ZEBRA_ROUTE_OLSR] = {ZEBRA_ROUTE_OLSR, 255},
[ZEBRA_ROUTE_TABLE] = {ZEBRA_ROUTE_TABLE, 150},
[ZEBRA_ROUTE_LDP] = {ZEBRA_ROUTE_LDP, 150},
[ZEBRA_ROUTE_VNC] = {ZEBRA_ROUTE_VNC, 20},
[ZEBRA_ROUTE_VNC_DIRECT] = {ZEBRA_ROUTE_VNC_DIRECT, 20},
[ZEBRA_ROUTE_VNC_DIRECT_RH] = {ZEBRA_ROUTE_VNC_DIRECT_RH, 20},
[ZEBRA_ROUTE_BGP_DIRECT] = {ZEBRA_ROUTE_BGP_DIRECT, 20},
[ZEBRA_ROUTE_BGP_DIRECT_EXT] = {ZEBRA_ROUTE_BGP_DIRECT_EXT, 20},
[ZEBRA_ROUTE_BABEL] = {ZEBRA_ROUTE_BABEL, 100},
[ZEBRA_ROUTE_SHARP] = {ZEBRA_ROUTE_SHARP, 150},
/* no entry/default: 150 */
};
/* RPF lookup behaviour */
static enum multicast_mode ipv4_multicast_mode = MCAST_NO_CONFIG;
static void __attribute__((format(printf, 5, 6)))
_rnode_zlog(const char *_func, vrf_id_t vrf_id, struct route_node *rn,
int priority, const char *msgfmt, ...)
{
char buf[SRCDEST2STR_BUFFER + sizeof(" (MRIB)")];
char msgbuf[512];
va_list ap;
va_start(ap, msgfmt);
vsnprintf(msgbuf, sizeof(msgbuf), msgfmt, ap);
va_end(ap);
if (rn) {
rib_table_info_t *info = srcdest_rnode_table_info(rn);
srcdest_rnode2str(rn, buf, sizeof(buf));
if (info->safi == SAFI_MULTICAST)
strcat(buf, " (MRIB)");
} else {
snprintf(buf, sizeof(buf), "{(route_node *) NULL}");
}
zlog(priority, "%s: %d:%s: %s", _func, vrf_id, buf, msgbuf);
}
#define rnode_debug(node, vrf_id, ...) \
_rnode_zlog(__func__, vrf_id, node, LOG_DEBUG, __VA_ARGS__)
#define rnode_info(node, ...) \
_rnode_zlog(__func__, vrf_id, node, LOG_INFO, __VA_ARGS__)
uint8_t route_distance(int type)
{
uint8_t distance;
if ((unsigned)type >= array_size(route_info))
distance = 150;
else
distance = route_info[type].distance;
return distance;
}
int is_zebra_valid_kernel_table(uint32_t table_id)
{
#ifdef linux
if ((table_id == RT_TABLE_UNSPEC) || (table_id == RT_TABLE_LOCAL)
|| (table_id == RT_TABLE_COMPAT))
return 0;
#endif
return 1;
}
int is_zebra_main_routing_table(uint32_t table_id)
{
if ((table_id == RT_TABLE_MAIN)
|| (table_id == zebrad.rtm_table_default))
return 1;
return 0;
}
int zebra_check_addr(const struct prefix *p)
{
if (p->family == AF_INET) {
uint32_t addr;
addr = p->u.prefix4.s_addr;
addr = ntohl(addr);
if (IPV4_NET127(addr) || IN_CLASSD(addr)
|| IPV4_LINKLOCAL(addr))
return 0;
}
if (p->family == AF_INET6) {
if (IN6_IS_ADDR_LOOPBACK(&p->u.prefix6))
return 0;
if (IN6_IS_ADDR_LINKLOCAL(&p->u.prefix6))
return 0;
}
return 1;
}
/* Add nexthop to the end of a rib node's nexthop list */
void route_entry_nexthop_add(struct route_entry *re, struct nexthop *nexthop)
{
nexthop_add(&re->ng.nexthop, nexthop);
re->nexthop_num++;
}
/**
* copy_nexthop - copy a nexthop to the rib structure.
*/
void route_entry_copy_nexthops(struct route_entry *re, struct nexthop *nh)
{
assert(!re->ng.nexthop);
copy_nexthops(&re->ng.nexthop, nh, NULL);
for (struct nexthop *nexthop = nh; nexthop; nexthop = nexthop->next)
re->nexthop_num++;
}
/* Delete specified nexthop from the list. */
void route_entry_nexthop_delete(struct route_entry *re, struct nexthop *nexthop)
{
if (nexthop->next)
nexthop->next->prev = nexthop->prev;
if (nexthop->prev)
nexthop->prev->next = nexthop->next;
else
re->ng.nexthop = nexthop->next;
re->nexthop_num--;
}
struct nexthop *route_entry_nexthop_ifindex_add(struct route_entry *re,
ifindex_t ifindex,
vrf_id_t nh_vrf_id)
{
struct nexthop *nexthop;
nexthop = nexthop_new();
nexthop->type = NEXTHOP_TYPE_IFINDEX;
nexthop->ifindex = ifindex;
nexthop->vrf_id = nh_vrf_id;
route_entry_nexthop_add(re, nexthop);
return nexthop;
}
struct nexthop *route_entry_nexthop_ipv4_add(struct route_entry *re,
struct in_addr *ipv4,
struct in_addr *src,
vrf_id_t nh_vrf_id)
{
struct nexthop *nexthop;
nexthop = nexthop_new();
nexthop->type = NEXTHOP_TYPE_IPV4;
nexthop->vrf_id = nh_vrf_id;
nexthop->gate.ipv4 = *ipv4;
if (src)
nexthop->src.ipv4 = *src;
route_entry_nexthop_add(re, nexthop);
return nexthop;
}
struct nexthop *route_entry_nexthop_ipv4_ifindex_add(struct route_entry *re,
struct in_addr *ipv4,
struct in_addr *src,
ifindex_t ifindex,
vrf_id_t nh_vrf_id)
{
struct nexthop *nexthop;
struct interface *ifp;
nexthop = nexthop_new();
nexthop->vrf_id = nh_vrf_id;
nexthop->type = NEXTHOP_TYPE_IPV4_IFINDEX;
nexthop->gate.ipv4 = *ipv4;
if (src)
nexthop->src.ipv4 = *src;
nexthop->ifindex = ifindex;
ifp = if_lookup_by_index(nexthop->ifindex, nh_vrf_id);
/*Pending: need to think if null ifp here is ok during bootup?
There was a crash because ifp here was coming to be NULL */
if (ifp)
if (connected_is_unnumbered(ifp)
|| CHECK_FLAG(re->flags, ZEBRA_FLAG_EVPN_ROUTE)
|| CHECK_FLAG(re->flags, ZEBRA_FLAG_ONLINK)) {
SET_FLAG(nexthop->flags, NEXTHOP_FLAG_ONLINK);
}
route_entry_nexthop_add(re, nexthop);
return nexthop;
}
struct nexthop *route_entry_nexthop_ipv6_add(struct route_entry *re,
struct in6_addr *ipv6,
vrf_id_t nh_vrf_id)
{
struct nexthop *nexthop;
nexthop = nexthop_new();
nexthop->vrf_id = nh_vrf_id;
nexthop->type = NEXTHOP_TYPE_IPV6;
nexthop->gate.ipv6 = *ipv6;
route_entry_nexthop_add(re, nexthop);
return nexthop;
}
struct nexthop *route_entry_nexthop_ipv6_ifindex_add(struct route_entry *re,
struct in6_addr *ipv6,
ifindex_t ifindex,
vrf_id_t nh_vrf_id)
{
struct nexthop *nexthop;
nexthop = nexthop_new();
nexthop->vrf_id = nh_vrf_id;
nexthop->type = NEXTHOP_TYPE_IPV6_IFINDEX;
nexthop->gate.ipv6 = *ipv6;
nexthop->ifindex = ifindex;
if (CHECK_FLAG(re->flags, ZEBRA_FLAG_EVPN_ROUTE)
|| CHECK_FLAG(re->flags, ZEBRA_FLAG_ONLINK)) {
SET_FLAG(nexthop->flags, NEXTHOP_FLAG_ONLINK);
}
route_entry_nexthop_add(re, nexthop);
return nexthop;
}
struct nexthop *route_entry_nexthop_blackhole_add(struct route_entry *re,
enum blackhole_type bh_type)
{
struct nexthop *nexthop;
nexthop = nexthop_new();
nexthop->vrf_id = VRF_DEFAULT;
nexthop->type = NEXTHOP_TYPE_BLACKHOLE;
nexthop->bh_type = bh_type;
route_entry_nexthop_add(re, nexthop);
return nexthop;
}
static void nexthop_set_resolved(afi_t afi, const struct nexthop *newhop,
struct nexthop *nexthop)
{
struct nexthop *resolved_hop;
resolved_hop = nexthop_new();
SET_FLAG(resolved_hop->flags, NEXTHOP_FLAG_ACTIVE);
resolved_hop->vrf_id = nexthop->vrf_id;
switch (newhop->type) {
case NEXTHOP_TYPE_IPV4:
case NEXTHOP_TYPE_IPV4_IFINDEX:
/* If the resolving route specifies a gateway, use it */
resolved_hop->type = newhop->type;
resolved_hop->gate.ipv4 = newhop->gate.ipv4;
if (newhop->ifindex) {
resolved_hop->type = NEXTHOP_TYPE_IPV4_IFINDEX;
resolved_hop->ifindex = newhop->ifindex;
if (newhop->flags & NEXTHOP_FLAG_ONLINK)
resolved_hop->flags |= NEXTHOP_FLAG_ONLINK;
}
break;
case NEXTHOP_TYPE_IPV6:
case NEXTHOP_TYPE_IPV6_IFINDEX:
resolved_hop->type = newhop->type;
resolved_hop->gate.ipv6 = newhop->gate.ipv6;
if (newhop->ifindex) {
resolved_hop->type = NEXTHOP_TYPE_IPV6_IFINDEX;
resolved_hop->ifindex = newhop->ifindex;
}
break;
case NEXTHOP_TYPE_IFINDEX:
/* If the resolving route is an interface route,
* it means the gateway we are looking up is connected
* to that interface. (The actual network is _not_ onlink).
* Therefore, the resolved route should have the original
* gateway as nexthop as it is directly connected.
*
* On Linux, we have to set the onlink netlink flag because
* otherwise, the kernel won't accept the route.
*/
resolved_hop->flags |= NEXTHOP_FLAG_ONLINK;
if (afi == AFI_IP) {
resolved_hop->type = NEXTHOP_TYPE_IPV4_IFINDEX;
resolved_hop->gate.ipv4 = nexthop->gate.ipv4;
} else if (afi == AFI_IP6) {
resolved_hop->type = NEXTHOP_TYPE_IPV6_IFINDEX;
resolved_hop->gate.ipv6 = nexthop->gate.ipv6;
}
resolved_hop->ifindex = newhop->ifindex;
break;
case NEXTHOP_TYPE_BLACKHOLE:
resolved_hop->type = NEXTHOP_TYPE_BLACKHOLE;
resolved_hop->bh_type = nexthop->bh_type;
break;
}
/* Copy labels of the resolved route */
if (newhop->nh_label)
nexthop_add_labels(resolved_hop, newhop->nh_label_type,
newhop->nh_label->num_labels,
&newhop->nh_label->label[0]);
resolved_hop->rparent = nexthop;
nexthop_add(&nexthop->resolved, resolved_hop);
}
/* If force flag is not set, do not modify falgs at all for uninstall
the route from FIB. */
static int nexthop_active(afi_t afi, struct route_entry *re,
struct nexthop *nexthop, int set,
struct route_node *top)
{
struct prefix p;
struct route_table *table;
struct route_node *rn;
struct route_entry *match = NULL;
int resolved;
struct nexthop *newhop;
struct interface *ifp;
rib_dest_t *dest;
if ((nexthop->type == NEXTHOP_TYPE_IPV4)
|| nexthop->type == NEXTHOP_TYPE_IPV6)
nexthop->ifindex = 0;
if (set) {
UNSET_FLAG(nexthop->flags, NEXTHOP_FLAG_RECURSIVE);
nexthops_free(nexthop->resolved);
nexthop->resolved = NULL;
re->nexthop_mtu = 0;
}
/* Next hops (remote VTEPs) for EVPN routes are fully resolved. */
if (CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_EVPN_RVTEP))
return 1;
/* Skip nexthops that have been filtered out due to route-map */
/* The nexthops are specific to this route and so the same */
/* nexthop for a different route may not have this flag set */
if (CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_FILTERED)) {
if (IS_ZEBRA_DEBUG_RIB_DETAILED)
zlog_debug("\t%s: Nexthop Filtered",
__PRETTY_FUNCTION__);
return 0;
}
/*
* Check to see if we should trust the passed in information
* for UNNUMBERED interfaces as that we won't find the GW
* address in the routing table.
*/
if (CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_ONLINK)) {
ifp = if_lookup_by_index(nexthop->ifindex, nexthop->vrf_id);
if ((ifp && connected_is_unnumbered(ifp))
|| CHECK_FLAG(re->flags, ZEBRA_FLAG_ONLINK)) {
if (if_is_operative(ifp))
return 1;
else {
if (IS_ZEBRA_DEBUG_RIB_DETAILED)
zlog_debug(
"\t%s: Onlink and interface %s is not operative",
__PRETTY_FUNCTION__, ifp->name);
return 0;
}
} else {
if (IS_ZEBRA_DEBUG_RIB_DETAILED)
zlog_debug(
"\t%s: Interface %s is not unnumbered",
__PRETTY_FUNCTION__,
ifp ? ifp->name : "Unknown");
return 0;
}
}
/* Make lookup prefix. */
memset(&p, 0, sizeof(struct prefix));
switch (afi) {
case AFI_IP:
p.family = AF_INET;
p.prefixlen = IPV4_MAX_PREFIXLEN;
p.u.prefix4 = nexthop->gate.ipv4;
break;
case AFI_IP6:
p.family = AF_INET6;
p.prefixlen = IPV6_MAX_PREFIXLEN;
p.u.prefix6 = nexthop->gate.ipv6;
break;
default:
assert(afi != AFI_IP && afi != AFI_IP6);
break;
}
/* Lookup table. */
table = zebra_vrf_table(afi, SAFI_UNICAST, nexthop->vrf_id);
if (!table) {
if (IS_ZEBRA_DEBUG_RIB_DETAILED)
zlog_debug("\t%s: Table not found",
__PRETTY_FUNCTION__);
return 0;
}
rn = route_node_match(table, (struct prefix *)&p);
while (rn) {
route_unlock_node(rn);
/* Lookup should halt if we've matched against ourselves ('top',
* if specified) - i.e., we cannot have a nexthop NH1 is
* resolved by a route NH1. The exception is if the route is a
* host route.
*/
if (top && rn == top)
if (((afi == AFI_IP) && (rn->p.prefixlen != 32))
|| ((afi == AFI_IP6) && (rn->p.prefixlen != 128))) {
if (IS_ZEBRA_DEBUG_RIB_DETAILED)
zlog_debug(
"\t%s: Matched against ourself and prefix length is not max bit length",
__PRETTY_FUNCTION__);
return 0;
}
/* Pick up selected route. */
/* However, do not resolve over default route unless explicitly
* allowed. */
if (is_default_prefix(&rn->p)
&& !rnh_resolve_via_default(p.family)) {
if (IS_ZEBRA_DEBUG_RIB_DETAILED)
zlog_debug(
"\t:%s: Resolved against default route",
__PRETTY_FUNCTION__);
return 0;
}
dest = rib_dest_from_rnode(rn);
if (dest && dest->selected_fib
&& !CHECK_FLAG(dest->selected_fib->status,
ROUTE_ENTRY_REMOVED)
&& dest->selected_fib->type != ZEBRA_ROUTE_TABLE)
match = dest->selected_fib;
/* If there is no selected route or matched route is EGP, go up
tree. */
if (!match) {
do {
rn = rn->parent;
} while (rn && rn->info == NULL);
if (rn)
route_lock_node(rn);
continue;
}
if (match->type == ZEBRA_ROUTE_CONNECT) {
/* Directly point connected route. */
newhop = match->ng.nexthop;
if (newhop) {
if (nexthop->type == NEXTHOP_TYPE_IPV4
|| nexthop->type == NEXTHOP_TYPE_IPV6)
nexthop->ifindex = newhop->ifindex;
}
return 1;
} else if (CHECK_FLAG(re->flags, ZEBRA_FLAG_ALLOW_RECURSION)) {
resolved = 0;
for (ALL_NEXTHOPS(match->ng, newhop)) {
if (!CHECK_FLAG(newhop->flags,
NEXTHOP_FLAG_FIB))
continue;
if (CHECK_FLAG(newhop->flags,
NEXTHOP_FLAG_RECURSIVE))
continue;
if (set) {
SET_FLAG(nexthop->flags,
NEXTHOP_FLAG_RECURSIVE);
SET_FLAG(re->status,
ROUTE_ENTRY_NEXTHOPS_CHANGED);
nexthop_set_resolved(afi, newhop,
nexthop);
}
resolved = 1;
}
if (resolved && set)
re->nexthop_mtu = match->mtu;
if (!resolved && IS_ZEBRA_DEBUG_RIB_DETAILED)
zlog_debug("\t%s: Recursion failed to find",
__PRETTY_FUNCTION__);
return resolved;
} else if (re->type == ZEBRA_ROUTE_STATIC) {
resolved = 0;
for (ALL_NEXTHOPS(match->ng, newhop)) {
if (!CHECK_FLAG(newhop->flags,
NEXTHOP_FLAG_FIB))
continue;
if (set) {
SET_FLAG(nexthop->flags,
NEXTHOP_FLAG_RECURSIVE);
nexthop_set_resolved(afi, newhop,
nexthop);
}
resolved = 1;
}
if (resolved && set)
re->nexthop_mtu = match->mtu;
if (!resolved && IS_ZEBRA_DEBUG_RIB_DETAILED)
zlog_debug(
"\t%s: Static route unable to resolve",
__PRETTY_FUNCTION__);
return resolved;
} else {
return 0;
}
}
return 0;
}
struct route_entry *rib_match(afi_t afi, safi_t safi, vrf_id_t vrf_id,
union g_addr *addr, struct route_node **rn_out)
{
struct prefix p;
struct route_table *table;
struct route_node *rn;
struct route_entry *match = NULL;
struct nexthop *newhop;
/* Lookup table. */
table = zebra_vrf_table(afi, safi, vrf_id);
if (!table)
return 0;
memset(&p, 0, sizeof(struct prefix));
p.family = afi;
if (afi == AFI_IP) {
p.u.prefix4 = addr->ipv4;
p.prefixlen = IPV4_MAX_PREFIXLEN;
} else {
p.u.prefix6 = addr->ipv6;
p.prefixlen = IPV6_MAX_PREFIXLEN;
}
rn = route_node_match(table, (struct prefix *)&p);
while (rn) {
rib_dest_t *dest;
route_unlock_node(rn);
dest = rib_dest_from_rnode(rn);
if (dest && dest->selected_fib
&& !CHECK_FLAG(dest->selected_fib->status,
ROUTE_ENTRY_REMOVED))
match = dest->selected_fib;
/* If there is no selected route or matched route is EGP, go up
tree. */
if (!match) {
do {
rn = rn->parent;
} while (rn && rn->info == NULL);
if (rn)
route_lock_node(rn);
} else {
if (match->type != ZEBRA_ROUTE_CONNECT) {
int found = 0;
for (ALL_NEXTHOPS(match->ng, newhop))
if (CHECK_FLAG(newhop->flags,
NEXTHOP_FLAG_FIB)) {
found = 1;
break;
}
if (!found)
return NULL;
}
if (rn_out)
*rn_out = rn;
return match;
}
}
return NULL;
}
struct route_entry *rib_match_ipv4_multicast(vrf_id_t vrf_id,
struct in_addr addr,
struct route_node **rn_out)
{
struct route_entry *re = NULL, *mre = NULL, *ure = NULL;
struct route_node *m_rn = NULL, *u_rn = NULL;
union g_addr gaddr = {.ipv4 = addr};
switch (ipv4_multicast_mode) {
case MCAST_MRIB_ONLY:
return rib_match(AFI_IP, SAFI_MULTICAST, vrf_id, &gaddr,
rn_out);
case MCAST_URIB_ONLY:
return rib_match(AFI_IP, SAFI_UNICAST, vrf_id, &gaddr, rn_out);
case MCAST_NO_CONFIG:
case MCAST_MIX_MRIB_FIRST:
re = mre = rib_match(AFI_IP, SAFI_MULTICAST, vrf_id, &gaddr,
&m_rn);
if (!mre)
re = ure = rib_match(AFI_IP, SAFI_UNICAST, vrf_id,
&gaddr, &u_rn);
break;
case MCAST_MIX_DISTANCE:
mre = rib_match(AFI_IP, SAFI_MULTICAST, vrf_id, &gaddr, &m_rn);
ure = rib_match(AFI_IP, SAFI_UNICAST, vrf_id, &gaddr, &u_rn);
if (mre && ure)
re = ure->distance < mre->distance ? ure : mre;
else if (mre)
re = mre;
else if (ure)
re = ure;
break;
case MCAST_MIX_PFXLEN:
mre = rib_match(AFI_IP, SAFI_MULTICAST, vrf_id, &gaddr, &m_rn);
ure = rib_match(AFI_IP, SAFI_UNICAST, vrf_id, &gaddr, &u_rn);
if (mre && ure)
re = u_rn->p.prefixlen > m_rn->p.prefixlen ? ure : mre;
else if (mre)
re = mre;
else if (ure)
re = ure;
break;
}
if (rn_out)
*rn_out = (re == mre) ? m_rn : u_rn;
if (IS_ZEBRA_DEBUG_RIB) {
char buf[BUFSIZ];
inet_ntop(AF_INET, &addr, buf, BUFSIZ);
zlog_debug("%s: %s: vrf: %u found %s, using %s",
__func__, buf, vrf_id,
mre ? (ure ? "MRIB+URIB" : "MRIB")
: ure ? "URIB" : "nothing",
re == ure ? "URIB" : re == mre ? "MRIB" : "none");
}
return re;
}
void multicast_mode_ipv4_set(enum multicast_mode mode)
{
if (IS_ZEBRA_DEBUG_RIB)
zlog_debug("%s: multicast lookup mode set (%d)", __func__,
mode);
ipv4_multicast_mode = mode;
}
enum multicast_mode multicast_mode_ipv4_get(void)
{
return ipv4_multicast_mode;
}
struct route_entry *rib_lookup_ipv4(struct prefix_ipv4 *p, vrf_id_t vrf_id)
{
struct route_table *table;
struct route_node *rn;
struct route_entry *match = NULL;
struct nexthop *nexthop;
rib_dest_t *dest;
/* Lookup table. */
table = zebra_vrf_table(AFI_IP, SAFI_UNICAST, vrf_id);
if (!table)
return 0;
rn = route_node_lookup(table, (struct prefix *)p);
/* No route for this prefix. */
if (!rn)
return NULL;
/* Unlock node. */
route_unlock_node(rn);
dest = rib_dest_from_rnode(rn);
if (dest && dest->selected_fib
&& !CHECK_FLAG(dest->selected_fib->status, ROUTE_ENTRY_REMOVED))
match = dest->selected_fib;
if (!match)
return NULL;
if (match->type == ZEBRA_ROUTE_CONNECT)
return match;
for (ALL_NEXTHOPS(match->ng, nexthop))
if (CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_FIB))
return match;
return NULL;
}
/*
* This clone function, unlike its original rib_lookup_ipv4(), checks
* if specified IPv4 route record (prefix/mask -> gate) exists in
* the whole RIB and has ROUTE_ENTRY_SELECTED_FIB set.
*
* Return values:
* -1: error
* 0: exact match found
* 1: a match was found with a different gate
* 2: connected route found
* 3: no matches found
*/
int rib_lookup_ipv4_route(struct prefix_ipv4 *p, union sockunion *qgate,
vrf_id_t vrf_id)
{
struct route_table *table;
struct route_node *rn;
struct route_entry *match = NULL;
struct nexthop *nexthop;
int nexthops_active;
rib_dest_t *dest;
/* Lookup table. */
table = zebra_vrf_table(AFI_IP, SAFI_UNICAST, vrf_id);
if (!table)
return ZEBRA_RIB_LOOKUP_ERROR;
/* Scan the RIB table for exactly matching RIB entry. */
rn = route_node_lookup(table, (struct prefix *)p);
/* No route for this prefix. */
if (!rn)
return ZEBRA_RIB_NOTFOUND;
/* Unlock node. */
route_unlock_node(rn);
dest = rib_dest_from_rnode(rn);
/* Find out if a "selected" RR for the discovered RIB entry exists ever.
*/
if (dest && dest->selected_fib
&& !CHECK_FLAG(dest->selected_fib->status, ROUTE_ENTRY_REMOVED))
match = dest->selected_fib;
/* None such found :( */
if (!match)
return ZEBRA_RIB_NOTFOUND;
if (match->type == ZEBRA_ROUTE_CONNECT)
return ZEBRA_RIB_FOUND_CONNECTED;
/* Ok, we have a cood candidate, let's check it's nexthop list... */
nexthops_active = 0;
for (ALL_NEXTHOPS(match->ng, nexthop))
if (CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_FIB)) {
nexthops_active = 1;
if (nexthop->gate.ipv4.s_addr == sockunion2ip(qgate))
return ZEBRA_RIB_FOUND_EXACT;
if (IS_ZEBRA_DEBUG_RIB) {
char gate_buf[INET_ADDRSTRLEN],
qgate_buf[INET_ADDRSTRLEN];
inet_ntop(AF_INET, &nexthop->gate.ipv4.s_addr,
gate_buf, INET_ADDRSTRLEN);
inet_ntop(AF_INET, &sockunion2ip(qgate),
qgate_buf, INET_ADDRSTRLEN);
zlog_debug("%s: qgate == %s, %s == %s",
__func__, qgate_buf,
nexthop->rparent ? "rgate" : "gate",
gate_buf);
}
}
if (nexthops_active)
return ZEBRA_RIB_FOUND_NOGATE;
return ZEBRA_RIB_NOTFOUND;
}
#define RIB_SYSTEM_ROUTE(R) \
((R)->type == ZEBRA_ROUTE_KERNEL || (R)->type == ZEBRA_ROUTE_CONNECT)
/* This function verifies reachability of one given nexthop, which can be
* numbered or unnumbered, IPv4 or IPv6. The result is unconditionally stored
* in nexthop->flags field. If the 4th parameter, 'set', is non-zero,
* nexthop->ifindex will be updated appropriately as well.
* An existing route map can turn (otherwise active) nexthop into inactive, but
* not vice versa.
*
* The return value is the final value of 'ACTIVE' flag.
*/
static unsigned nexthop_active_check(struct route_node *rn,
struct route_entry *re,
struct nexthop *nexthop, int set)
{
struct interface *ifp;
route_map_result_t ret = RMAP_MATCH;
int family;
char buf[SRCDEST2STR_BUFFER];
const struct prefix *p, *src_p;
srcdest_rnode_prefixes(rn, &p, &src_p);
if (rn->p.family == AF_INET)
family = AFI_IP;
else if (rn->p.family == AF_INET6)
family = AFI_IP6;
else
family = 0;
switch (nexthop->type) {
case NEXTHOP_TYPE_IFINDEX:
ifp = if_lookup_by_index(nexthop->ifindex, nexthop->vrf_id);
if (ifp && if_is_operative(ifp))
SET_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE);
else
UNSET_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE);
break;
case NEXTHOP_TYPE_IPV4:
case NEXTHOP_TYPE_IPV4_IFINDEX:
family = AFI_IP;
if (nexthop_active(AFI_IP, re, nexthop, set, rn))
SET_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE);
else
UNSET_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE);
break;
case NEXTHOP_TYPE_IPV6:
family = AFI_IP6;
if (nexthop_active(AFI_IP6, re, nexthop, set, rn))
SET_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE);
else
UNSET_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE);
break;
case NEXTHOP_TYPE_IPV6_IFINDEX:
/* RFC 5549, v4 prefix with v6 NH */
if (rn->p.family != AF_INET)
family = AFI_IP6;
if (IN6_IS_ADDR_LINKLOCAL(&nexthop->gate.ipv6)) {
ifp = if_lookup_by_index(nexthop->ifindex,
nexthop->vrf_id);
if (ifp && if_is_operative(ifp))
SET_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE);
else
UNSET_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE);
} else {
if (nexthop_active(AFI_IP6, re, nexthop, set, rn))
SET_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE);
else
UNSET_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE);
}
break;
case NEXTHOP_TYPE_BLACKHOLE:
SET_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE);
break;
default:
break;
}
if (!CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE)) {
if (IS_ZEBRA_DEBUG_RIB_DETAILED)
zlog_debug("\t%s: Unable to find a active nexthop",
__PRETTY_FUNCTION__);
return 0;
}
/* XXX: What exactly do those checks do? Do we support
* e.g. IPv4 routes with IPv6 nexthops or vice versa? */
if (RIB_SYSTEM_ROUTE(re) || (family == AFI_IP && p->family != AF_INET)
|| (family == AFI_IP6 && p->family != AF_INET6))
return CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE);
/* The original code didn't determine the family correctly
* e.g. for NEXTHOP_TYPE_IFINDEX. Retrieve the correct afi
* from the rib_table_info in those cases.
* Possibly it may be better to use only the rib_table_info
* in every case.
*/
if (!family) {
rib_table_info_t *info;
info = srcdest_rnode_table_info(rn);
family = info->afi;
}
memset(&nexthop->rmap_src.ipv6, 0, sizeof(union g_addr));
/* It'll get set if required inside */
ret = zebra_route_map_check(family, re->type, re->instance, p, nexthop,
nexthop->vrf_id, re->tag);
if (ret == RMAP_DENYMATCH) {
if (IS_ZEBRA_DEBUG_RIB) {
srcdest_rnode2str(rn, buf, sizeof(buf));
zlog_debug(
"%u:%s: Filtering out with NH out %s due to route map",
re->vrf_id, buf,
ifindex2ifname(nexthop->ifindex,
nexthop->vrf_id));
}
UNSET_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE);
}
return CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE);
}
/* Iterate over all nexthops of the given RIB entry and refresh their
* ACTIVE flag. re->nexthop_active_num is updated accordingly. If any
* nexthop is found to toggle the ACTIVE flag, the whole re structure
* is flagged with ROUTE_ENTRY_CHANGED. The 4th 'set' argument is
* transparently passed to nexthop_active_check().
*
* Return value is the new number of active nexthops.
*/
static int nexthop_active_update(struct route_node *rn, struct route_entry *re,
int set)
{
struct nexthop *nexthop;
union g_addr prev_src;
unsigned int prev_active, new_active, old_num_nh;
ifindex_t prev_index;
old_num_nh = re->nexthop_active_num;
re->nexthop_active_num = 0;
UNSET_FLAG(re->status, ROUTE_ENTRY_CHANGED);
for (nexthop = re->ng.nexthop; nexthop; nexthop = nexthop->next) {
/* No protocol daemon provides src and so we're skipping
* tracking it */
prev_src = nexthop->rmap_src;
prev_active = CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE);
prev_index = nexthop->ifindex;
if ((new_active = nexthop_active_check(rn, re, nexthop, set)))
re->nexthop_active_num++;
/* Don't allow src setting on IPv6 addr for now */
if (prev_active != new_active || prev_index != nexthop->ifindex
|| ((nexthop->type >= NEXTHOP_TYPE_IFINDEX
&& nexthop->type < NEXTHOP_TYPE_IPV6)
&& prev_src.ipv4.s_addr
!= nexthop->rmap_src.ipv4.s_addr)
|| ((nexthop->type >= NEXTHOP_TYPE_IPV6
&& nexthop->type < NEXTHOP_TYPE_BLACKHOLE)
&& !(IPV6_ADDR_SAME(&prev_src.ipv6,
&nexthop->rmap_src.ipv6)))) {
SET_FLAG(re->status, ROUTE_ENTRY_CHANGED);
SET_FLAG(re->status, ROUTE_ENTRY_NEXTHOPS_CHANGED);
}
}
if (old_num_nh != re->nexthop_active_num)
SET_FLAG(re->status, ROUTE_ENTRY_CHANGED);
if (CHECK_FLAG(re->status, ROUTE_ENTRY_CHANGED)) {
SET_FLAG(re->status, ROUTE_ENTRY_NEXTHOPS_CHANGED);
}
return re->nexthop_active_num;
}
/*
* Is this RIB labeled-unicast? It must be of type BGP and all paths
* (nexthops) must have a label.
*/
int zebra_rib_labeled_unicast(struct route_entry *re)
{
struct nexthop *nexthop = NULL;
if (re->type != ZEBRA_ROUTE_BGP)
return 0;
for (ALL_NEXTHOPS(re->ng, nexthop))
if (!nexthop->nh_label || !nexthop->nh_label->num_labels)
return 0;
return 1;
}
void kernel_route_rib_pass_fail(struct route_node *rn, const struct prefix *p,
struct route_entry *re,
enum zebra_dplane_status res)
{
struct nexthop *nexthop;
char buf[PREFIX_STRLEN];
rib_dest_t *dest;
dest = rib_dest_from_rnode(rn);
switch (res) {
case ZEBRA_DPLANE_INSTALL_SUCCESS:
dest->selected_fib = re;
for (ALL_NEXTHOPS(re->ng, nexthop)) {
if (CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_RECURSIVE))
continue;
if (CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE))
SET_FLAG(nexthop->flags, NEXTHOP_FLAG_FIB);
else
UNSET_FLAG(nexthop->flags, NEXTHOP_FLAG_FIB);
}
zsend_route_notify_owner(re, p, ZAPI_ROUTE_INSTALLED);
break;
case ZEBRA_DPLANE_INSTALL_FAILURE:
/*
* I am not sure this is the right thing to do here
* but the code always set selected_fib before
* this assignment was moved here.
*/
dest->selected_fib = re;
zsend_route_notify_owner(re, p, ZAPI_ROUTE_FAIL_INSTALL);
flog_err(EC_ZEBRA_DP_INSTALL_FAIL,
"%u:%s: Route install failed", re->vrf_id,
prefix2str(p, buf, sizeof(buf)));
break;
case ZEBRA_DPLANE_DELETE_SUCCESS:
/*
* The case where selected_fib is not re is
* when we have received a system route
* that is overriding our installed route
* as such we should leave the selected_fib
* pointer alone
*/
if (dest->selected_fib == re)
dest->selected_fib = NULL;
for (ALL_NEXTHOPS(re->ng, nexthop))
UNSET_FLAG(nexthop->flags, NEXTHOP_FLAG_FIB);
zsend_route_notify_owner(re, p, ZAPI_ROUTE_REMOVED);
break;
case ZEBRA_DPLANE_DELETE_FAILURE:
/*
* Should we set this to NULL if the
* delete fails?
*/
dest->selected_fib = NULL;
flog_err(EC_ZEBRA_DP_DELETE_FAIL,
"%u:%s: Route Deletion failure", re->vrf_id,
prefix2str(p, buf, sizeof(buf)));
zsend_route_notify_owner(re, p, ZAPI_ROUTE_REMOVE_FAIL);
break;
case ZEBRA_DPLANE_STATUS_NONE:
break;
}
}
/* Update flag indicates whether this is a "replace" or not. Currently, this
* is only used for IPv4.
*/
void rib_install_kernel(struct route_node *rn, struct route_entry *re,
struct route_entry *old)
{
struct nexthop *nexthop;
rib_table_info_t *info = srcdest_rnode_table_info(rn);
const struct prefix *p, *src_p;
struct zebra_vrf *zvrf = vrf_info_lookup(re->vrf_id);
srcdest_rnode_prefixes(rn, &p, &src_p);
if (info->safi != SAFI_UNICAST) {
for (ALL_NEXTHOPS(re->ng, nexthop))
SET_FLAG(nexthop->flags, NEXTHOP_FLAG_FIB);
return;
} else {
struct nexthop *prev;
for (ALL_NEXTHOPS(re->ng, nexthop)) {
UNSET_FLAG (nexthop->flags, NEXTHOP_FLAG_DUPLICATE);
for (ALL_NEXTHOPS(re->ng, prev)) {
if (prev == nexthop)
break;
if (nexthop_same_firsthop(nexthop, prev)) {
SET_FLAG(nexthop->flags,
NEXTHOP_FLAG_DUPLICATE);
break;
}
}
}
}
/*
* If this is a replace to a new RE let the originator of the RE
* know that they've lost
*/
if (old && (old != re) && (old->type != re->type))
zsend_route_notify_owner(old, p, ZAPI_ROUTE_BETTER_ADMIN_WON);
/*
* Make sure we update the FPM any time we send new information to
* the kernel.
*/
hook_call(rib_update, rn, "installing in kernel");
switch (kernel_route_rib(rn, p, src_p, old, re)) {
case ZEBRA_DPLANE_REQUEST_QUEUED:
flog_err(
EC_ZEBRA_DP_INVALID_RC,
"No current known DataPlane interfaces can return this, please fix");
break;
case ZEBRA_DPLANE_REQUEST_FAILURE:
flog_err(
EC_ZEBRA_DP_INSTALL_FAIL,
"No current known Rib Install Failure cases, please fix");
break;
case ZEBRA_DPLANE_REQUEST_SUCCESS:
zvrf->installs++;
break;
}
return;
}
/* Uninstall the route from kernel. */
void rib_uninstall_kernel(struct route_node *rn, struct route_entry *re)
{
struct nexthop *nexthop;
rib_table_info_t *info = srcdest_rnode_table_info(rn);
const struct prefix *p, *src_p;
struct zebra_vrf *zvrf = vrf_info_lookup(re->vrf_id);
srcdest_rnode_prefixes(rn, &p, &src_p);
if (info->safi != SAFI_UNICAST) {
for (ALL_NEXTHOPS(re->ng, nexthop))
UNSET_FLAG(nexthop->flags, NEXTHOP_FLAG_FIB);
return;
}
/*
* Make sure we update the FPM any time we send new information to
* the kernel.
*/
hook_call(rib_update, rn, "uninstalling from kernel");
switch (kernel_route_rib(rn, p, src_p, re, NULL)) {
case ZEBRA_DPLANE_REQUEST_QUEUED:
flog_err(
EC_ZEBRA_DP_INVALID_RC,
"No current known DataPlane interfaces can return this, please fix");
break;
case ZEBRA_DPLANE_REQUEST_FAILURE:
flog_err(
EC_ZEBRA_DP_INSTALL_FAIL,
"No current known RIB Install Failure cases, please fix");
break;
case ZEBRA_DPLANE_REQUEST_SUCCESS:
if (zvrf)
zvrf->removals++;
break;
}
return;
}
/* Uninstall the route from kernel. */
static void rib_uninstall(struct route_node *rn, struct route_entry *re)
{
rib_table_info_t *info = srcdest_rnode_table_info(rn);
rib_dest_t *dest = rib_dest_from_rnode(rn);
if (dest && dest->selected_fib == re) {
if (info->safi == SAFI_UNICAST)
hook_call(rib_update, rn, "rib_uninstall");
if (!RIB_SYSTEM_ROUTE(re))
rib_uninstall_kernel(rn, re);
/* If labeled-unicast route, uninstall transit LSP. */
if (zebra_rib_labeled_unicast(re))
zebra_mpls_lsp_uninstall(info->zvrf, rn, re);
}
if (CHECK_FLAG(re->flags, ZEBRA_FLAG_SELECTED)) {
const struct prefix *p, *src_p;
srcdest_rnode_prefixes(rn, &p, &src_p);
redistribute_delete(p, src_p, re);
UNSET_FLAG(re->flags, ZEBRA_FLAG_SELECTED);
}
}
/*
* rib_can_delete_dest
*
* Returns TRUE if the given dest can be deleted from the table.
*/
static int rib_can_delete_dest(rib_dest_t *dest)
{
if (dest->routes) {
return 0;
}
/*
* Don't delete the dest if we have to update the FPM about this
* prefix.
*/
if (CHECK_FLAG(dest->flags, RIB_DEST_UPDATE_FPM)
|| CHECK_FLAG(dest->flags, RIB_DEST_SENT_TO_FPM))
return 0;
return 1;
}
/*
* rib_gc_dest
*
* Garbage collect the rib dest corresponding to the given route node
* if appropriate.
*
* Returns TRUE if the dest was deleted, FALSE otherwise.
*/
int rib_gc_dest(struct route_node *rn)
{
rib_dest_t *dest;
dest = rib_dest_from_rnode(rn);
if (!dest)
return 0;
if (!rib_can_delete_dest(dest))
return 0;
if (IS_ZEBRA_DEBUG_RIB) {
struct zebra_vrf *zvrf;
zvrf = rib_dest_vrf(dest);
rnode_debug(rn, zvrf_id(zvrf), "removing dest from table");
}
dest->rnode = NULL;
XFREE(MTYPE_RIB_DEST, dest);
rn->info = NULL;
/*
* Release the one reference that we keep on the route node.
*/
route_unlock_node(rn);
return 1;
}
static void rib_process_add_fib(struct zebra_vrf *zvrf, struct route_node *rn,
struct route_entry *new)
{
rib_dest_t *dest = rib_dest_from_rnode(rn);
hook_call(rib_update, rn, "new route selected");
/* Update real nexthop. This may actually determine if nexthop is active
* or not. */
if (!nexthop_active_update(rn, new, 1)) {
UNSET_FLAG(new->status, ROUTE_ENTRY_CHANGED);
return;
}
if (IS_ZEBRA_DEBUG_RIB) {
char buf[SRCDEST2STR_BUFFER];
srcdest_rnode2str(rn, buf, sizeof(buf));
zlog_debug("%u:%s: Adding route rn %p, re %p (type %d)",
zvrf_id(zvrf), buf, rn, new, new->type);
}
/* If labeled-unicast route, install transit LSP. */
if (zebra_rib_labeled_unicast(new))
zebra_mpls_lsp_install(zvrf, rn, new);
if (!RIB_SYSTEM_ROUTE(new))
rib_install_kernel(rn, new, NULL);
else
dest->selected_fib = new;
UNSET_FLAG(new->status, ROUTE_ENTRY_CHANGED);
}
static void rib_process_del_fib(struct zebra_vrf *zvrf, struct route_node *rn,
struct route_entry *old)
{
rib_dest_t *dest = rib_dest_from_rnode(rn);
hook_call(rib_update, rn, "removing existing route");
/* Uninstall from kernel. */
if (IS_ZEBRA_DEBUG_RIB) {
char buf[SRCDEST2STR_BUFFER];
srcdest_rnode2str(rn, buf, sizeof(buf));
zlog_debug("%u:%s: Deleting route rn %p, re %p (type %d)",
zvrf_id(zvrf), buf, rn, old, old->type);
}
/* If labeled-unicast route, uninstall transit LSP. */
if (zebra_rib_labeled_unicast(old))
zebra_mpls_lsp_uninstall(zvrf, rn, old);
if (!RIB_SYSTEM_ROUTE(old))
rib_uninstall_kernel(rn, old);
else {
/*
* We are setting this to NULL here
* because that is what we traditionally
* have been doing. I am not positive
* that this is the right thing to do
* but let's leave the code alone
* for the RIB_SYSTEM_ROUTE case
*/
dest->selected_fib = NULL;
}
/* Update nexthop for route, reset changed flag. */
nexthop_active_update(rn, old, 1);
UNSET_FLAG(old->status, ROUTE_ENTRY_CHANGED);
}
static void rib_process_update_fib(struct zebra_vrf *zvrf,
struct route_node *rn,
struct route_entry *old,
struct route_entry *new)
{
struct nexthop *nexthop = NULL;
int nh_active = 0;
rib_dest_t *dest = rib_dest_from_rnode(rn);
/*
* We have to install or update if a new route has been selected or
* something has changed.
*/
if (new != old || CHECK_FLAG(new->status, ROUTE_ENTRY_CHANGED)) {
hook_call(rib_update, rn, "updating existing route");
/* Update the nexthop; we could determine here that nexthop is
* inactive. */
if (nexthop_active_update(rn, new, 1))
nh_active = 1;
/* If nexthop is active, install the selected route, if
* appropriate. If
* the install succeeds, cleanup flags for prior route, if
* different from
* newly selected.
*/
if (nh_active) {
if (IS_ZEBRA_DEBUG_RIB) {
char buf[SRCDEST2STR_BUFFER];
srcdest_rnode2str(rn, buf, sizeof(buf));
if (new != old)
zlog_debug(
"%u:%s: Updating route rn %p, re %p (type %d) "
"old %p (type %d)",
zvrf_id(zvrf), buf, rn, new,
new->type, old, old->type);
else
zlog_debug(
"%u:%s: Updating route rn %p, re %p (type %d)",
zvrf_id(zvrf), buf, rn, new,
new->type);
}
/* If labeled-unicast route, uninstall transit LSP. */
if (zebra_rib_labeled_unicast(old))
zebra_mpls_lsp_uninstall(zvrf, rn, old);
/* Non-system route should be installed. */
if (!RIB_SYSTEM_ROUTE(new)) {
/* If labeled-unicast route, install transit
* LSP. */
if (zebra_rib_labeled_unicast(new))
zebra_mpls_lsp_install(zvrf, rn, new);
rib_install_kernel(rn, new, old);
} else {
/*
* We do not need to install the
* selected route because it
* is already isntalled by
* the system( ie not us )
* so just mark it as winning
* we do need to ensure that
* if we uninstall a route
* from ourselves we don't
* over write this pointer
*/
dest->selected_fib = NULL;
}
/* If install succeeded or system route, cleanup flags
* for prior route. */
if (new != old) {
if (RIB_SYSTEM_ROUTE(new)) {
if (!RIB_SYSTEM_ROUTE(old))
rib_uninstall_kernel(rn, old);
} else {
for (nexthop = old->ng.nexthop; nexthop;
nexthop = nexthop->next)
UNSET_FLAG(nexthop->flags,
NEXTHOP_FLAG_FIB);
}
}
}
/*
* If nexthop for selected route is not active or install
* failed, we
* may need to uninstall and delete for redistribution.
*/
if (!nh_active) {
if (IS_ZEBRA_DEBUG_RIB) {
char buf[SRCDEST2STR_BUFFER];
srcdest_rnode2str(rn, buf, sizeof(buf));
if (new != old)
zlog_debug(
"%u:%s: Deleting route rn %p, re %p (type %d) "
"old %p (type %d) - nexthop inactive",
zvrf_id(zvrf), buf, rn, new,
new->type, old, old->type);
else
zlog_debug(
"%u:%s: Deleting route rn %p, re %p (type %d) - nexthop inactive",
zvrf_id(zvrf), buf, rn, new,
new->type);
}
/* If labeled-unicast route, uninstall transit LSP. */
if (zebra_rib_labeled_unicast(old))
zebra_mpls_lsp_uninstall(zvrf, rn, old);
if (!RIB_SYSTEM_ROUTE(old))
rib_uninstall_kernel(rn, old);
else
dest->selected_fib = NULL;
}
} else {
/*
* Same route selected; check if in the FIB and if not,
* re-install. This
* is housekeeping code to deal with race conditions in kernel
* with linux
* netlink reporting interface up before IPv4 or IPv6 protocol
* is ready
* to add routes.
*/
if (!RIB_SYSTEM_ROUTE(new)) {
bool in_fib = false;
for (ALL_NEXTHOPS(new->ng, nexthop))
if (CHECK_FLAG(nexthop->flags,
NEXTHOP_FLAG_FIB)) {
in_fib = true;
break;
}
if (!in_fib)
rib_install_kernel(rn, new, NULL);
}
}
/* Update prior route. */
if (new != old) {
/* Set real nexthop. */
nexthop_active_update(rn, old, 1);
UNSET_FLAG(old->status, ROUTE_ENTRY_CHANGED);
}
/* Clear changed flag. */
UNSET_FLAG(new->status, ROUTE_ENTRY_CHANGED);
}
/* Check if 'alternate' RIB entry is better than 'current'. */
static struct route_entry *rib_choose_best(struct route_entry *current,
struct route_entry *alternate)
{
if (current == NULL)
return alternate;
/* filter route selection in following order:
* - connected beats other types
* - if both connected, loopback or vrf wins
* - lower distance beats higher
* - lower metric beats higher for equal distance
* - last, hence oldest, route wins tie break.
*/
/* Connected routes. Check to see if either are a vrf
* or loopback interface. If not, pick the last connected
* route of the set of lowest metric connected routes.
*/
if (alternate->type == ZEBRA_ROUTE_CONNECT) {
if (current->type != ZEBRA_ROUTE_CONNECT)
return alternate;
/* both are connected. are either loop or vrf? */
struct nexthop *nexthop = NULL;
for (ALL_NEXTHOPS(alternate->ng, nexthop)) {
if (if_is_loopback_or_vrf(if_lookup_by_index(
nexthop->ifindex, alternate->vrf_id)))
return alternate;
}
for (ALL_NEXTHOPS(current->ng, nexthop)) {
if (if_is_loopback_or_vrf(if_lookup_by_index(
nexthop->ifindex, current->vrf_id)))
return current;
}
/* Neither are loop or vrf so pick best metric */
if (alternate->metric <= current->metric)
return alternate;
return current;
}
if (current->type == ZEBRA_ROUTE_CONNECT)
return current;
/* higher distance loses */
if (alternate->distance < current->distance)
return alternate;
if (current->distance < alternate->distance)
return current;
/* metric tie-breaks equal distance */
if (alternate->metric <= current->metric)
return alternate;
return current;
}
/* Core function for processing routing information base. */
static void rib_process(struct route_node *rn)
{
struct route_entry *re;
struct route_entry *next;
struct route_entry *old_selected = NULL;
struct route_entry *new_selected = NULL;
struct route_entry *old_fib = NULL;
struct route_entry *new_fib = NULL;
struct route_entry *best = NULL;
char buf[SRCDEST2STR_BUFFER];
rib_dest_t *dest;
struct zebra_vrf *zvrf = NULL;
const struct prefix *p, *src_p;
srcdest_rnode_prefixes(rn, &p, &src_p);
vrf_id_t vrf_id = VRF_UNKNOWN;
assert(rn);
dest = rib_dest_from_rnode(rn);
if (dest) {
zvrf = rib_dest_vrf(dest);
vrf_id = zvrf_id(zvrf);
}
if (IS_ZEBRA_DEBUG_RIB)
srcdest_rnode2str(rn, buf, sizeof(buf));
if (IS_ZEBRA_DEBUG_RIB_DETAILED)
zlog_debug("%u:%s: Processing rn %p", vrf_id, buf, rn);
/*
* we can have rn's that have a NULL info pointer
* (dest). As such let's not let the deref happen
* additionally we know RNODE_FOREACH_RE_SAFE
* will not iterate so we are ok.
*/
if (dest)
old_fib = dest->selected_fib;
RNODE_FOREACH_RE_SAFE (rn, re, next) {
if (IS_ZEBRA_DEBUG_RIB_DETAILED)
zlog_debug(
"%u:%s: Examine re %p (type %d) status %x flags %x "
"dist %d metric %d",
vrf_id, buf, re, re->type, re->status,
re->flags, re->distance, re->metric);
UNSET_FLAG(re->status, ROUTE_ENTRY_NEXTHOPS_CHANGED);
/* Currently selected re. */
if (CHECK_FLAG(re->flags, ZEBRA_FLAG_SELECTED)) {
assert(old_selected == NULL);
old_selected = re;
}
/* Skip deleted entries from selection */
if (CHECK_FLAG(re->status, ROUTE_ENTRY_REMOVED))
continue;
/* Skip unreachable nexthop. */
/* This first call to nexthop_active_update is merely to
* determine if
* there's any change to nexthops associated with this RIB
* entry. Now,
* rib_process() can be invoked due to an external event such as
* link
* down or due to next-hop-tracking evaluation. In the latter
* case,
* a decision has already been made that the NHs have changed.
* So, no
* need to invoke a potentially expensive call again. Further,
* since
* the change might be in a recursive NH which is not caught in
* the nexthop_active_update() code. Thus, we might miss changes
* to
* recursive NHs.
*/
if (!CHECK_FLAG(re->status, ROUTE_ENTRY_CHANGED)
&& !nexthop_active_update(rn, re, 0)) {
if (re->type == ZEBRA_ROUTE_TABLE) {
/* XXX: HERE BE DRAGONS!!!!!
* In all honesty, I have not yet figured out
* what this part
* does or why the ROUTE_ENTRY_CHANGED test
* above is correct
* or why we need to delete a route here, and
* also not whether
* this concerns both selected and fib route, or
* only selected
* or only fib */
/* This entry was denied by the 'ip protocol
* table' route-map, we
* need to delete it */
if (re != old_selected) {
if (IS_ZEBRA_DEBUG_RIB)
zlog_debug(
"%s: %u:%s: imported via import-table but denied "
"by the ip protocol table route-map",
__func__, vrf_id, buf);
rib_unlink(rn, re);
} else
SET_FLAG(re->status,
ROUTE_ENTRY_REMOVED);
}
continue;
}
/* Infinite distance. */
if (re->distance == DISTANCE_INFINITY) {
UNSET_FLAG(re->status, ROUTE_ENTRY_CHANGED);
continue;
}
if (CHECK_FLAG(re->flags, ZEBRA_FLAG_FIB_OVERRIDE)) {
best = rib_choose_best(new_fib, re);
if (new_fib && best != new_fib)
UNSET_FLAG(new_fib->status,
ROUTE_ENTRY_CHANGED);
new_fib = best;
} else {
best = rib_choose_best(new_selected, re);
if (new_selected && best != new_selected)
UNSET_FLAG(new_selected->status,
ROUTE_ENTRY_CHANGED);
new_selected = best;
}
if (best != re)
UNSET_FLAG(re->status, ROUTE_ENTRY_CHANGED);
} /* RNODE_FOREACH_RE */
/* If no FIB override route, use the selected route also for FIB */
if (new_fib == NULL)
new_fib = new_selected;
/* After the cycle is finished, the following pointers will be set:
* old_selected --- RE entry currently having SELECTED
* new_selected --- RE entry that is newly SELECTED
* old_fib --- RE entry currently in kernel FIB
* new_fib --- RE entry that is newly to be in kernel FIB
*
* new_selected will get SELECTED flag, and is going to be redistributed
* the zclients. new_fib (which can be new_selected) will be installed
* in kernel.
*/
if (IS_ZEBRA_DEBUG_RIB_DETAILED) {
zlog_debug(
"%u:%s: After processing: old_selected %p new_selected %p old_fib %p new_fib %p",
vrf_id, buf, (void *)old_selected, (void *)new_selected,
(void *)old_fib, (void *)new_fib);
}
/* Buffer ROUTE_ENTRY_CHANGED here, because it will get cleared if
* fib == selected */
bool selected_changed = new_selected && CHECK_FLAG(new_selected->status,
ROUTE_ENTRY_CHANGED);
/* Update fib according to selection results */
if (new_fib && old_fib)
rib_process_update_fib(zvrf, rn, old_fib, new_fib);
else if (new_fib)
rib_process_add_fib(zvrf, rn, new_fib);
else if (old_fib)
rib_process_del_fib(zvrf, rn, old_fib);
/* Redistribute SELECTED entry */
if (old_selected != new_selected || selected_changed) {
struct nexthop *nexthop = NULL;
/* Check if we have a FIB route for the destination, otherwise,
* don't redistribute it */
if (new_fib) {
for (ALL_NEXTHOPS(new_fib->ng, nexthop)) {
if (CHECK_FLAG(nexthop->flags,
NEXTHOP_FLAG_FIB)) {
break;
}
}
}
if (!nexthop)
new_selected = NULL;
if (new_selected && new_selected != new_fib) {
nexthop_active_update(rn, new_selected, 1);
UNSET_FLAG(new_selected->status, ROUTE_ENTRY_CHANGED);
}
if (old_selected) {
if (!new_selected)
redistribute_delete(p, src_p, old_selected);
if (old_selected != new_selected)
UNSET_FLAG(old_selected->flags,
ZEBRA_FLAG_SELECTED);
}
if (new_selected) {
/* Install new or replace existing redistributed entry
*/
SET_FLAG(new_selected->flags, ZEBRA_FLAG_SELECTED);
redistribute_update(p, src_p, new_selected,
old_selected);
}
}
/* Remove all RE entries queued for removal */
RNODE_FOREACH_RE_SAFE (rn, re, next) {
if (CHECK_FLAG(re->status, ROUTE_ENTRY_REMOVED)) {
if (IS_ZEBRA_DEBUG_RIB) {
rnode_debug(rn, vrf_id, "rn %p, removing re %p",
(void *)rn, (void *)re);
}
rib_unlink(rn, re);
}
}
/*
* Check if the dest can be deleted now.
*/
rib_gc_dest(rn);
}
/* Take a list of route_node structs and return 1, if there was a record
* picked from it and processed by rib_process(). Don't process more,
* than one RN record; operate only in the specified sub-queue.
*/
static unsigned int process_subq(struct list *subq, uint8_t qindex)
{
struct listnode *lnode = listhead(subq);
struct route_node *rnode;
rib_dest_t *dest;
struct zebra_vrf *zvrf = NULL;
if (!lnode)
return 0;
rnode = listgetdata(lnode);
dest = rib_dest_from_rnode(rnode);
if (dest)
zvrf = rib_dest_vrf(dest);
rib_process(rnode);
if (IS_ZEBRA_DEBUG_RIB_DETAILED) {
char buf[SRCDEST2STR_BUFFER];
srcdest_rnode2str(rnode, buf, sizeof(buf));
zlog_debug("%u:%s: rn %p dequeued from sub-queue %u",
zvrf ? zvrf_id(zvrf) : 0, buf, rnode, qindex);
}
if (rnode->info)
UNSET_FLAG(rib_dest_from_rnode(rnode)->flags,
RIB_ROUTE_QUEUED(qindex));
#if 0
else
{
zlog_debug ("%s: called for route_node (%p, %d) with no ribs",
__func__, rnode, rnode->lock);
zlog_backtrace(LOG_DEBUG);
}
#endif
route_unlock_node(rnode);
list_delete_node(subq, lnode);
return 1;
}
/*
* All meta queues have been processed. Trigger next-hop evaluation.
*/
static void meta_queue_process_complete(struct work_queue *dummy)
{
struct vrf *vrf;
struct zebra_vrf *zvrf;
/* Evaluate nexthops for those VRFs which underwent route processing.
* This
* should limit the evaluation to the necessary VRFs in most common
* situations.
*/
RB_FOREACH (vrf, vrf_id_head, &vrfs_by_id) {
zvrf = vrf->info;
if (zvrf == NULL || !(zvrf->flags & ZEBRA_VRF_RIB_SCHEDULED))
continue;
zvrf->flags &= ~ZEBRA_VRF_RIB_SCHEDULED;
zebra_evaluate_rnh(zvrf_id(zvrf), AF_INET, 0, RNH_NEXTHOP_TYPE,
NULL);
zebra_evaluate_rnh(zvrf_id(zvrf), AF_INET, 0,
RNH_IMPORT_CHECK_TYPE, NULL);
zebra_evaluate_rnh(zvrf_id(zvrf), AF_INET6, 0, RNH_NEXTHOP_TYPE,
NULL);
zebra_evaluate_rnh(zvrf_id(zvrf), AF_INET6, 0,
RNH_IMPORT_CHECK_TYPE, NULL);
}
/* Schedule LSPs for processing, if needed. */
zvrf = vrf_info_lookup(VRF_DEFAULT);
if (mpls_should_lsps_be_processed(zvrf)) {
if (IS_ZEBRA_DEBUG_MPLS)
zlog_debug(
"%u: Scheduling all LSPs upon RIB completion",
zvrf_id(zvrf));
zebra_mpls_lsp_schedule(zvrf);
mpls_unmark_lsps_for_processing(zvrf);
}
}
/* Dispatch the meta queue by picking, processing and unlocking the next RN from
* a non-empty sub-queue with lowest priority. wq is equal to zebra->ribq and
* data
* is pointed to the meta queue structure.
*/
static wq_item_status meta_queue_process(struct work_queue *dummy, void *data)
{
struct meta_queue *mq = data;
unsigned i;
for (i = 0; i < MQ_SIZE; i++)
if (process_subq(mq->subq[i], i)) {
mq->size--;
break;
}
return mq->size ? WQ_REQUEUE : WQ_SUCCESS;
}
/*
* Map from rib types to queue type (priority) in meta queue
*/
static const uint8_t meta_queue_map[ZEBRA_ROUTE_MAX] = {
[ZEBRA_ROUTE_SYSTEM] = 4,
[ZEBRA_ROUTE_KERNEL] = 0,
[ZEBRA_ROUTE_CONNECT] = 0,
[ZEBRA_ROUTE_STATIC] = 1,
[ZEBRA_ROUTE_RIP] = 2,
[ZEBRA_ROUTE_RIPNG] = 2,
[ZEBRA_ROUTE_OSPF] = 2,
[ZEBRA_ROUTE_OSPF6] = 2,
[ZEBRA_ROUTE_ISIS] = 2,
[ZEBRA_ROUTE_BGP] = 3,
[ZEBRA_ROUTE_PIM] = 4, // Shouldn't happen but for safety
[ZEBRA_ROUTE_EIGRP] = 2,
[ZEBRA_ROUTE_NHRP] = 2,
[ZEBRA_ROUTE_HSLS] = 4,
[ZEBRA_ROUTE_OLSR] = 4,
[ZEBRA_ROUTE_TABLE] = 1,
[ZEBRA_ROUTE_LDP] = 4,
[ZEBRA_ROUTE_VNC] = 3,
[ZEBRA_ROUTE_VNC_DIRECT] = 3,
[ZEBRA_ROUTE_VNC_DIRECT_RH] = 3,
[ZEBRA_ROUTE_BGP_DIRECT] = 3,
[ZEBRA_ROUTE_BGP_DIRECT_EXT] = 3,
[ZEBRA_ROUTE_BABEL] = 2,
[ZEBRA_ROUTE_ALL] = 4, // Shouldn't happen but for safety
};
/* Look into the RN and queue it into one or more priority queues,
* increasing the size for each data push done.
*/
static void rib_meta_queue_add(struct meta_queue *mq, struct route_node *rn)
{
struct route_entry *re;
RNODE_FOREACH_RE (rn, re) {
uint8_t qindex = meta_queue_map[re->type];
struct zebra_vrf *zvrf;
/* Invariant: at this point we always have rn->info set. */
if (CHECK_FLAG(rib_dest_from_rnode(rn)->flags,
RIB_ROUTE_QUEUED(qindex))) {
if (IS_ZEBRA_DEBUG_RIB_DETAILED)
rnode_debug(
rn, re->vrf_id,
"rn %p is already queued in sub-queue %u",
(void *)rn, qindex);
continue;
}
SET_FLAG(rib_dest_from_rnode(rn)->flags,
RIB_ROUTE_QUEUED(qindex));
listnode_add(mq->subq[qindex], rn);
route_lock_node(rn);
mq->size++;
if (IS_ZEBRA_DEBUG_RIB_DETAILED)
rnode_debug(rn, re->vrf_id,
"queued rn %p into sub-queue %u",
(void *)rn, qindex);
zvrf = zebra_vrf_lookup_by_id(re->vrf_id);
if (zvrf)
zvrf->flags |= ZEBRA_VRF_RIB_SCHEDULED;
}
}
/* Add route_node to work queue and schedule processing */
void rib_queue_add(struct route_node *rn)
{
assert(rn);
/* Pointless to queue a route_node with no RIB entries to add or remove
*/
if (!rnode_to_ribs(rn)) {
zlog_debug("%s: called for route_node (%p, %d) with no ribs",
__func__, (void *)rn, rn->lock);
zlog_backtrace(LOG_DEBUG);
return;
}
if (zebrad.ribq == NULL) {
flog_err(EC_ZEBRA_WQ_NONEXISTENT,
"%s: work_queue does not exist!", __func__);
return;
}
/*
* The RIB queue should normally be either empty or holding the only
* work_queue_item element. In the latter case this element would
* hold a pointer to the meta queue structure, which must be used to
* actually queue the route nodes to process. So create the MQ
* holder, if necessary, then push the work into it in any case.
* This semantics was introduced after 0.99.9 release.
*/
if (work_queue_empty(zebrad.ribq))
work_queue_add(zebrad.ribq, zebrad.mq);
rib_meta_queue_add(zebrad.mq, rn);
return;
}
/* Create new meta queue.
A destructor function doesn't seem to be necessary here.
*/
static struct meta_queue *meta_queue_new(void)
{
struct meta_queue *new;
unsigned i;
new = XCALLOC(MTYPE_WORK_QUEUE, sizeof(struct meta_queue));
for (i = 0; i < MQ_SIZE; i++) {
new->subq[i] = list_new();
assert(new->subq[i]);
}
return new;
}
void meta_queue_free(struct meta_queue *mq)
{
unsigned i;
for (i = 0; i < MQ_SIZE; i++)
list_delete_and_null(&mq->subq[i]);
XFREE(MTYPE_WORK_QUEUE, mq);
}
/* initialise zebra rib work queue */
static void rib_queue_init(struct zebra_t *zebra)
{
assert(zebra);
if (!(zebra->ribq =
work_queue_new(zebra->master, "route_node processing"))) {
flog_err(EC_ZEBRA_WQ_NONEXISTENT,
"%s: could not initialise work queue!", __func__);
return;
}
/* fill in the work queue spec */
zebra->ribq->spec.workfunc = &meta_queue_process;
zebra->ribq->spec.errorfunc = NULL;
zebra->ribq->spec.completion_func = &meta_queue_process_complete;
/* XXX: TODO: These should be runtime configurable via vty */
zebra->ribq->spec.max_retries = 3;
zebra->ribq->spec.hold = ZEBRA_RIB_PROCESS_HOLD_TIME;
if (!(zebra->mq = meta_queue_new())) {
flog_err(EC_ZEBRA_WQ_NONEXISTENT,
"%s: could not initialise meta queue!", __func__);
return;
}
return;
}
/* RIB updates are processed via a queue of pointers to route_nodes.
*
* The queue length is bounded by the maximal size of the routing table,
* as a route_node will not be requeued, if already queued.
*
* REs are submitted via rib_addnode or rib_delnode which set minimal
* state, or static_install_route (when an existing RE is updated)
* and then submit route_node to queue for best-path selection later.
* Order of add/delete state changes are preserved for any given RE.
*
* Deleted REs are reaped during best-path selection.
*
* rib_addnode
* |-> rib_link or unset ROUTE_ENTRY_REMOVE |->Update kernel with
* |-------->| | best RE, if required
* | |
* static_install->|->rib_addqueue...... -> rib_process
* | |
* |-------->| |-> rib_unlink
* |-> set ROUTE_ENTRY_REMOVE |
* rib_delnode (RE freed)
*
* The 'info' pointer of a route_node points to a rib_dest_t
* ('dest'). Queueing state for a route_node is kept on the dest. The
* dest is created on-demand by rib_link() and is kept around at least
* as long as there are ribs hanging off it (@see rib_gc_dest()).
*
* Refcounting (aka "locking" throughout the GNU Zebra and Quagga code):
*
* - route_nodes: refcounted by:
* - dest attached to route_node:
* - managed by: rib_link/rib_gc_dest
* - route_node processing queue
* - managed by: rib_addqueue, rib_process.
*
*/
/* Add RE to head of the route node. */
static void rib_link(struct route_node *rn, struct route_entry *re, int process)
{
struct route_entry *head;
rib_dest_t *dest;
afi_t afi;
const char *rmap_name;
assert(re && rn);
dest = rib_dest_from_rnode(rn);
if (!dest) {
if (IS_ZEBRA_DEBUG_RIB_DETAILED)
rnode_debug(rn, re->vrf_id, "rn %p adding dest", rn);
dest = XCALLOC(MTYPE_RIB_DEST, sizeof(rib_dest_t));
route_lock_node(rn); /* rn route table reference */
rn->info = dest;
dest->rnode = rn;
}
head = dest->routes;
if (head) {
head->prev = re;
}
re->next = head;
dest->routes = re;
afi = (rn->p.family == AF_INET)
? AFI_IP
: (rn->p.family == AF_INET6) ? AFI_IP6 : AFI_MAX;
if (is_zebra_import_table_enabled(afi, re->table)) {
rmap_name = zebra_get_import_table_route_map(afi, re->table);
zebra_add_import_table_entry(rn, re, rmap_name);
} else if (process)
rib_queue_add(rn);
}
static void rib_addnode(struct route_node *rn,
struct route_entry *re, int process)
{
/* RE node has been un-removed before route-node is processed.
* route_node must hence already be on the queue for processing..
*/
if (CHECK_FLAG(re->status, ROUTE_ENTRY_REMOVED)) {
if (IS_ZEBRA_DEBUG_RIB)
rnode_debug(rn, re->vrf_id, "rn %p, un-removed re %p",
(void *)rn, (void *)re);
UNSET_FLAG(re->status, ROUTE_ENTRY_REMOVED);
return;
}
rib_link(rn, re, process);
}
/*
* rib_unlink
*
* Detach a rib structure from a route_node.
*
* Note that a call to rib_unlink() should be followed by a call to
* rib_gc_dest() at some point. This allows a rib_dest_t that is no
* longer required to be deleted.
*/
void rib_unlink(struct route_node *rn, struct route_entry *re)
{
rib_dest_t *dest;
assert(rn && re);
if (IS_ZEBRA_DEBUG_RIB)
rnode_debug(rn, re->vrf_id, "rn %p, re %p", (void *)rn,
(void *)re);
dest = rib_dest_from_rnode(rn);
if (re->next)
re->next->prev = re->prev;
if (re->prev)
re->prev->next = re->next;
else {
dest->routes = re->next;
}
if (dest->selected_fib == re)
dest->selected_fib = NULL;
nexthops_free(re->ng.nexthop);
XFREE(MTYPE_RE, re);
}
void rib_delnode(struct route_node *rn, struct route_entry *re)
{
afi_t afi;
if (IS_ZEBRA_DEBUG_RIB)
rnode_debug(rn, re->vrf_id, "rn %p, re %p, removing",
(void *)rn, (void *)re);
SET_FLAG(re->status, ROUTE_ENTRY_REMOVED);
afi = (rn->p.family == AF_INET)
? AFI_IP
: (rn->p.family == AF_INET6) ? AFI_IP6 : AFI_MAX;
if (is_zebra_import_table_enabled(afi, re->table)) {
zebra_del_import_table_entry(rn, re);
/* Just clean up if non main table */
if (IS_ZEBRA_DEBUG_RIB) {
char buf[SRCDEST2STR_BUFFER];
srcdest_rnode2str(rn, buf, sizeof(buf));
zlog_debug(
"%u:%s: Freeing route rn %p, re %p (type %d)",
re->vrf_id, buf, rn, re, re->type);
}
rib_unlink(rn, re);
} else {
rib_queue_add(rn);
}
}
/* This function dumps the contents of a given RE entry into
* standard debug log. Calling function name and IP prefix in
* question are passed as 1st and 2nd arguments.
*/
void _route_entry_dump(const char *func, union prefixconstptr pp,
union prefixconstptr src_pp,
const struct route_entry *re)
{
const struct prefix *src_p = src_pp.p;
bool is_srcdst = src_p && src_p->prefixlen;
char straddr[PREFIX_STRLEN];
char srcaddr[PREFIX_STRLEN];
struct nexthop *nexthop;
zlog_debug("%s: dumping RE entry %p for %s%s%s vrf %u", func,
(const void *)re, prefix2str(pp, straddr, sizeof(straddr)),
is_srcdst ? " from " : "",
is_srcdst ? prefix2str(src_pp, srcaddr, sizeof(srcaddr))
: "",
re->vrf_id);
zlog_debug("%s: uptime == %lu, type == %u, instance == %d, table == %d",
func, (unsigned long)re->uptime, re->type, re->instance,
re->table);
zlog_debug(
"%s: metric == %u, mtu == %u, distance == %u, flags == %u, status == %u",
func, re->metric, re->mtu, re->distance, re->flags, re->status);
zlog_debug("%s: nexthop_num == %u, nexthop_active_num == %u", func,
re->nexthop_num, re->nexthop_active_num);
for (ALL_NEXTHOPS(re->ng, nexthop)) {
struct interface *ifp;
struct vrf *vrf = vrf_lookup_by_id(nexthop->vrf_id);
switch (nexthop->type) {
case NEXTHOP_TYPE_BLACKHOLE:
sprintf(straddr, "Blackhole");
break;
case NEXTHOP_TYPE_IFINDEX:
ifp = if_lookup_by_index(nexthop->ifindex,
nexthop->vrf_id);
sprintf(straddr, "%s", ifp ? ifp->name : "Unknown");
break;
case NEXTHOP_TYPE_IPV4:
/* fallthrough */
case NEXTHOP_TYPE_IPV4_IFINDEX:
inet_ntop(AF_INET, &nexthop->gate, straddr,
INET6_ADDRSTRLEN);
break;
case NEXTHOP_TYPE_IPV6:
case NEXTHOP_TYPE_IPV6_IFINDEX:
inet_ntop(AF_INET6, &nexthop->gate, straddr,
INET6_ADDRSTRLEN);
break;
}
zlog_debug("%s: %s %s[%u] vrf %s(%u) with flags %s%s%s", func,
(nexthop->rparent ? " NH" : "NH"), straddr,
nexthop->ifindex, vrf ? vrf->name : "Unknown",
nexthop->vrf_id,
(CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE)
? "ACTIVE "
: ""),
(CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_FIB)
? "FIB "
: ""),
(CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_RECURSIVE)
? "RECURSIVE"
: ""));
}
zlog_debug("%s: dump complete", func);
}
/* This is an exported helper to rtm_read() to dump the strange
* RE entry found by rib_lookup_ipv4_route()
*/
void rib_lookup_and_dump(struct prefix_ipv4 *p, vrf_id_t vrf_id)
{
struct route_table *table;
struct route_node *rn;
struct route_entry *re;
char prefix_buf[INET_ADDRSTRLEN];
/* Lookup table. */
table = zebra_vrf_table(AFI_IP, SAFI_UNICAST, vrf_id);
if (!table) {
flog_err(EC_ZEBRA_TABLE_LOOKUP_FAILED,
"%s:%u zebra_vrf_table() returned NULL", __func__,
vrf_id);
return;
}
/* Scan the RIB table for exactly matching RE entry. */
rn = route_node_lookup(table, (struct prefix *)p);
/* No route for this prefix. */
if (!rn) {
zlog_debug("%s:%u lookup failed for %s", __func__, vrf_id,
prefix2str((struct prefix *)p, prefix_buf,
sizeof(prefix_buf)));
return;
}
/* Unlock node. */
route_unlock_node(rn);
/* let's go */
RNODE_FOREACH_RE (rn, re) {
zlog_debug("%s:%u rn %p, re %p: %s, %s",
__func__, vrf_id,
(void *)rn, (void *)re,
(CHECK_FLAG(re->status, ROUTE_ENTRY_REMOVED)
? "removed"
: "NOT removed"),
(CHECK_FLAG(re->flags, ZEBRA_FLAG_SELECTED)
? "selected"
: "NOT selected"));
route_entry_dump(p, NULL, re);
}
}
/* Check if requested address assignment will fail due to another
* route being installed by zebra in FIB already. Take necessary
* actions, if needed: remove such a route from FIB and deSELECT
* corresponding RE entry. Then put affected RN into RIBQ head.
*/
void rib_lookup_and_pushup(struct prefix_ipv4 *p, vrf_id_t vrf_id)
{
struct route_table *table;
struct route_node *rn;
unsigned changed = 0;
rib_dest_t *dest;
if (NULL == (table = zebra_vrf_table(AFI_IP, SAFI_UNICAST, vrf_id))) {
flog_err(EC_ZEBRA_TABLE_LOOKUP_FAILED,
"%s:%u zebra_vrf_table() returned NULL", __func__,
vrf_id);
return;
}
/* No matches would be the simplest case. */
if (NULL == (rn = route_node_lookup(table, (struct prefix *)p)))
return;
/* Unlock node. */
route_unlock_node(rn);
dest = rib_dest_from_rnode(rn);
/* Check all RE entries. In case any changes have to be done, requeue
* the RN into RIBQ head. If the routing message about the new connected
* route (generated by the IP address we are going to assign very soon)
* comes before the RIBQ is processed, the new RE entry will join
* RIBQ record already on head. This is necessary for proper
* revalidation
* of the rest of the RE.
*/
if (dest->selected_fib && !RIB_SYSTEM_ROUTE(dest->selected_fib)) {
changed = 1;
if (IS_ZEBRA_DEBUG_RIB) {
char buf[PREFIX_STRLEN];
zlog_debug("%u:%s: freeing way for connected prefix",
dest->selected_fib->vrf_id,
prefix2str(&rn->p, buf, sizeof(buf)));
route_entry_dump(&rn->p, NULL, dest->selected_fib);
}
rib_uninstall(rn, dest->selected_fib);
}
if (changed)
rib_queue_add(rn);
}
int rib_add_multipath(afi_t afi, safi_t safi, struct prefix *p,
struct prefix_ipv6 *src_p, struct route_entry *re)
{
struct route_table *table;
struct route_node *rn;
struct route_entry *same = NULL;
struct nexthop *nexthop;
int ret = 0;
if (!re)
return 0;
assert(!src_p || !src_p->prefixlen || afi == AFI_IP6);
/* Lookup table. */
table = zebra_vrf_table_with_table_id(afi, safi, re->vrf_id, re->table);
if (!table) {
XFREE(MTYPE_RE, re);
return 0;
}
/* Make it sure prefixlen is applied to the prefix. */
apply_mask(p);
if (src_p)
apply_mask_ipv6(src_p);
/* Set default distance by route type. */
if (re->distance == 0) {
re->distance = route_distance(re->type);
/* iBGP distance is 200. */
if (re->type == ZEBRA_ROUTE_BGP
&& CHECK_FLAG(re->flags, ZEBRA_FLAG_IBGP))
re->distance = 200;
}
/* Lookup route node.*/
rn = srcdest_rnode_get(table, p, src_p);
/*
* If same type of route are installed, treat it as a implicit
* withdraw.
* If the user has specified the No route replace semantics
* for the install don't do a route replace.
*/
RNODE_FOREACH_RE (rn, same) {
if (CHECK_FLAG(same->status, ROUTE_ENTRY_REMOVED))
continue;
if (same->type != re->type)
continue;
if (same->instance != re->instance)
continue;
if (same->type == ZEBRA_ROUTE_KERNEL
&& same->metric != re->metric)
continue;
if (CHECK_FLAG(re->flags, ZEBRA_FLAG_RR_USE_DISTANCE) &&
same->distance != re->distance)
continue;
/*
* We should allow duplicate connected routes
* because of IPv6 link-local routes and unnumbered
* interfaces on Linux.
*/
if (same->type != ZEBRA_ROUTE_CONNECT)
break;
}
/* If this route is kernel route, set FIB flag to the route. */
if (RIB_SYSTEM_ROUTE(re))
for (nexthop = re->ng.nexthop; nexthop; nexthop = nexthop->next)
SET_FLAG(nexthop->flags, NEXTHOP_FLAG_FIB);
/* Link new re to node.*/
if (IS_ZEBRA_DEBUG_RIB) {
rnode_debug(
rn, re->vrf_id,
"Inserting route rn %p, re %p (type %d) existing %p",
(void *)rn, (void *)re, re->type, (void *)same);
if (IS_ZEBRA_DEBUG_RIB_DETAILED)
route_entry_dump(p, src_p, re);
}
rib_addnode(rn, re, 1);
ret = 1;
/* Free implicit route.*/
if (same) {
rib_delnode(rn, same);
ret = -1;
}
route_unlock_node(rn);
return ret;
}
void rib_delete(afi_t afi, safi_t safi, vrf_id_t vrf_id, int type,
unsigned short instance, int flags, struct prefix *p,
struct prefix_ipv6 *src_p, const struct nexthop *nh,
uint32_t table_id, uint32_t metric, uint8_t distance,
bool fromkernel)
{
struct route_table *table;
struct route_node *rn;
struct route_entry *re;
struct route_entry *fib = NULL;
struct route_entry *same = NULL;
struct nexthop *rtnh;
char buf2[INET6_ADDRSTRLEN];
rib_dest_t *dest;
assert(!src_p || !src_p->prefixlen || afi == AFI_IP6);
/* Lookup table. */
table = zebra_vrf_table_with_table_id(afi, safi, vrf_id, table_id);
if (!table)
return;
/* Apply mask. */
apply_mask(p);
if (src_p)
apply_mask_ipv6(src_p);
/* Lookup route node. */
rn = srcdest_rnode_lookup(table, p, src_p);
if (!rn) {
char dst_buf[PREFIX_STRLEN], src_buf[PREFIX_STRLEN];
prefix2str(p, dst_buf, sizeof(dst_buf));
if (src_p && src_p->prefixlen)
prefix2str(src_p, src_buf, sizeof(src_buf));
else
src_buf[0] = '\0';
if (IS_ZEBRA_DEBUG_RIB)
zlog_debug("%u:%s%s%s doesn't exist in rib", vrf_id,
dst_buf,
(src_buf[0] != '\0') ? " from " : "",
src_buf);
return;
}
dest = rib_dest_from_rnode(rn);
fib = dest->selected_fib;
/* Lookup same type route. */
RNODE_FOREACH_RE (rn, re) {
if (CHECK_FLAG(re->status, ROUTE_ENTRY_REMOVED))
continue;
if (re->type != type)
continue;
if (re->instance != instance)
continue;
if (CHECK_FLAG(re->flags, ZEBRA_FLAG_RR_USE_DISTANCE) &&
distance != re->distance)
continue;
if (re->type == ZEBRA_ROUTE_KERNEL && re->metric != metric)
continue;
if (re->type == ZEBRA_ROUTE_CONNECT && (rtnh = re->ng.nexthop)
&& rtnh->type == NEXTHOP_TYPE_IFINDEX && nh) {
if (rtnh->ifindex != nh->ifindex)
continue;
same = re;
break;
}
/* Make sure that the route found has the same gateway. */
else {
if (nh == NULL) {
same = re;
break;
}
for (ALL_NEXTHOPS(re->ng, rtnh))
if (nexthop_same_no_recurse(rtnh, nh)) {
same = re;
break;
}
if (same)
break;
}
}
/* If same type of route can't be found and this message is from
kernel. */
if (!same) {
/*
* In the past(HA!) we could get here because
* we were receiving a route delete from the
* kernel and we're not marking the proto
* as coming from it's appropriate originator.
* Now that we are properly noticing the fact
* that the kernel has deleted our route we
* are not going to get called in this path
* I am going to leave this here because
* this might still work this way on non-linux
* platforms as well as some weird state I have
* not properly thought of yet.
* If we can show that this code path is
* dead then we can remove it.
*/
if (fib && CHECK_FLAG(flags, ZEBRA_FLAG_SELFROUTE)) {
if (IS_ZEBRA_DEBUG_RIB) {
rnode_debug(
rn, vrf_id,
"rn %p, re %p (type %d) was deleted from kernel, adding",
rn, fib, fib->type);
}
if (allow_delete) {
/* Unset flags. */
for (rtnh = fib->ng.nexthop; rtnh;
rtnh = rtnh->next)
UNSET_FLAG(rtnh->flags,
NEXTHOP_FLAG_FIB);
/*
* This is a non FRR route
* as such we should mark
* it as deleted
*/
dest->selected_fib = NULL;
} else {
/* This means someone else, other than Zebra,
* has deleted
* a Zebra router from the kernel. We will add
* it back */
rib_install_kernel(rn, fib, NULL);
}
} else {
if (IS_ZEBRA_DEBUG_RIB) {
if (nh)
rnode_debug(
rn, vrf_id,
"via %s ifindex %d type %d "
"doesn't exist in rib",
inet_ntop(afi2family(afi),
&nh->gate, buf2,
sizeof(buf2)),
nh->ifindex, type);
else
rnode_debug(
rn, vrf_id,
"type %d doesn't exist in rib",
type);
}
route_unlock_node(rn);
return;
}
}
if (same) {
if (fromkernel && CHECK_FLAG(flags, ZEBRA_FLAG_SELFROUTE)
&& !allow_delete) {
rib_install_kernel(rn, same, NULL);
route_unlock_node(rn);
return;
}
if (CHECK_FLAG(flags, ZEBRA_FLAG_EVPN_ROUTE)) {
struct nexthop *tmp_nh;
for (ALL_NEXTHOPS(re->ng, tmp_nh)) {
struct ipaddr vtep_ip;
memset(&vtep_ip, 0, sizeof(struct ipaddr));
if (afi == AFI_IP) {
vtep_ip.ipa_type = IPADDR_V4;
memcpy(&(vtep_ip.ipaddr_v4),
&(tmp_nh->gate.ipv4),
sizeof(struct in_addr));
} else {
vtep_ip.ipa_type = IPADDR_V6;
memcpy(&(vtep_ip.ipaddr_v6),
&(tmp_nh->gate.ipv6),
sizeof(struct in6_addr));
}
zebra_vxlan_evpn_vrf_route_del(re->vrf_id,
&vtep_ip, p);
}
}
rib_delnode(rn, same);
}
route_unlock_node(rn);
return;
}
int rib_add(afi_t afi, safi_t safi, vrf_id_t vrf_id, int type,
unsigned short instance, int flags, struct prefix *p,
struct prefix_ipv6 *src_p, const struct nexthop *nh,
uint32_t table_id, uint32_t metric, uint32_t mtu, uint8_t distance,
route_tag_t tag)
{
struct route_entry *re;
struct nexthop *nexthop;
/* Allocate new route_entry structure. */
re = XCALLOC(MTYPE_RE, sizeof(struct route_entry));
re->type = type;
re->instance = instance;
re->distance = distance;
re->flags = flags;
re->metric = metric;
re->mtu = mtu;
re->table = table_id;
re->vrf_id = vrf_id;
re->nexthop_num = 0;
re->uptime = time(NULL);
re->tag = tag;
/* Add nexthop. */
nexthop = nexthop_new();
*nexthop = *nh;
route_entry_nexthop_add(re, nexthop);
return rib_add_multipath(afi, safi, p, src_p, re);
}
/* Schedule routes of a particular table (address-family) based on event. */
void rib_update_table(struct route_table *table, rib_update_event_t event)
{
struct route_node *rn;
struct route_entry *re, *next;
/* Walk all routes and queue for processing, if appropriate for
* the trigger event.
*/
for (rn = route_top(table); rn; rn = srcdest_route_next(rn)) {
/*
* If we are looking at a route node and the node
* has already been queued we don't
* need to queue it up again
*/
if (rn->info && CHECK_FLAG(rib_dest_from_rnode(rn)->flags,
RIB_ROUTE_ANY_QUEUED))
continue;
switch (event) {
case RIB_UPDATE_IF_CHANGE:
/* Examine all routes that won't get processed by the
* protocol or
* triggered by nexthop evaluation (NHT). This would be
* system,
* kernel and certain static routes. Note that NHT will
* get
* triggered upon an interface event as connected routes
* always
* get queued for processing.
*/
RNODE_FOREACH_RE_SAFE (rn, re, next) {
struct nexthop *nh;
if (re->type != ZEBRA_ROUTE_SYSTEM
&& re->type != ZEBRA_ROUTE_KERNEL
&& re->type != ZEBRA_ROUTE_CONNECT
&& re->type != ZEBRA_ROUTE_STATIC)
continue;
if (re->type != ZEBRA_ROUTE_STATIC) {
rib_queue_add(rn);
continue;
}
for (nh = re->ng.nexthop; nh; nh = nh->next)
if (!(nh->type == NEXTHOP_TYPE_IPV4
|| nh->type == NEXTHOP_TYPE_IPV6))
break;
/* If we only have nexthops to a
* gateway, NHT will
* take care.
*/
if (nh)
rib_queue_add(rn);
}
break;
case RIB_UPDATE_RMAP_CHANGE:
case RIB_UPDATE_OTHER:
/* Right now, examine all routes. Can restrict to a
* protocol in
* some cases (TODO).
*/
if (rnode_to_ribs(rn))
rib_queue_add(rn);
break;
default:
break;
}
}
}
/* RIB update function. */
void rib_update(vrf_id_t vrf_id, rib_update_event_t event)
{
struct route_table *table;
/* Process routes of interested address-families. */
table = zebra_vrf_table(AFI_IP, SAFI_UNICAST, vrf_id);
if (table) {
if (IS_ZEBRA_DEBUG_EVENT)
zlog_debug("%s : AFI_IP event %d", __func__, event);
rib_update_table(table, event);
}
table = zebra_vrf_table(AFI_IP6, SAFI_UNICAST, vrf_id);
if (table) {
if (IS_ZEBRA_DEBUG_EVENT)
zlog_debug("%s : AFI_IP6 event %d", __func__, event);
rib_update_table(table, event);
}
}
/* Delete self installed routes after zebra is relaunched. */
void rib_sweep_table(struct route_table *table)
{
struct route_node *rn;
struct route_entry *re;
struct route_entry *next;
struct nexthop *nexthop;
if (!table)
return;
for (rn = route_top(table); rn; rn = srcdest_route_next(rn)) {
RNODE_FOREACH_RE_SAFE (rn, re, next) {
if (IS_ZEBRA_DEBUG_RIB)
route_entry_dump(&rn->p, NULL, re);
if (CHECK_FLAG(re->status, ROUTE_ENTRY_REMOVED))
continue;
if (!CHECK_FLAG(re->flags, ZEBRA_FLAG_SELFROUTE))
continue;
/*
* So we are starting up and have received
* routes from the kernel that we have installed
* from a previous run of zebra but not cleaned
* up ( say a kill -9 )
* But since we haven't actually installed
* them yet( we received them from the kernel )
* we don't think they are active.
* So let's pretend they are active to actually
* remove them.
* In all honesty I'm not sure if we should
* mark them as active when we receive them
* This is startup only so probably ok.
*
* If we ever decide to move rib_sweep_table
* to a different spot (ie startup )
* this decision needs to be revisited
*/
for (ALL_NEXTHOPS(re->ng, nexthop))
SET_FLAG(nexthop->flags, NEXTHOP_FLAG_FIB);
rib_uninstall_kernel(rn, re);
rib_delnode(rn, re);
}
}
}
/* Sweep all RIB tables. */
void rib_sweep_route(void)
{
struct vrf *vrf;
struct zebra_vrf *zvrf;
RB_FOREACH (vrf, vrf_id_head, &vrfs_by_id) {
if ((zvrf = vrf->info) == NULL)
continue;
rib_sweep_table(zvrf->table[AFI_IP][SAFI_UNICAST]);
rib_sweep_table(zvrf->table[AFI_IP6][SAFI_UNICAST]);
}
zebra_ns_sweep_route();
}
/* Remove specific by protocol routes from 'table'. */
unsigned long rib_score_proto_table(uint8_t proto, unsigned short instance,
struct route_table *table)
{
struct route_node *rn;
struct route_entry *re;
struct route_entry *next;
unsigned long n = 0;
if (table)
for (rn = route_top(table); rn; rn = srcdest_route_next(rn))
RNODE_FOREACH_RE_SAFE (rn, re, next) {
if (CHECK_FLAG(re->status, ROUTE_ENTRY_REMOVED))
continue;
if (re->type == proto
&& re->instance == instance) {
rib_delnode(rn, re);
n++;
}
}
return n;
}
/* Remove specific by protocol routes. */
unsigned long rib_score_proto(uint8_t proto, unsigned short instance)
{
struct vrf *vrf;
struct zebra_vrf *zvrf;
unsigned long cnt = 0;
RB_FOREACH (vrf, vrf_id_head, &vrfs_by_id)
if ((zvrf = vrf->info) != NULL)
cnt += rib_score_proto_table(
proto, instance,
zvrf->table[AFI_IP][SAFI_UNICAST])
+ rib_score_proto_table(
proto, instance,
zvrf->table[AFI_IP6][SAFI_UNICAST]);
cnt += zebra_ns_score_proto(proto, instance);
return cnt;
}
/* Close RIB and clean up kernel routes. */
void rib_close_table(struct route_table *table)
{
struct route_node *rn;
rib_table_info_t *info;
rib_dest_t *dest;
if (!table)
return;
info = route_table_get_info(table);
for (rn = route_top(table); rn; rn = srcdest_route_next(rn)) {
dest = rib_dest_from_rnode(rn);
if (dest && dest->selected_fib) {
if (info->safi == SAFI_UNICAST)
hook_call(rib_update, rn, NULL);
if (!RIB_SYSTEM_ROUTE(dest->selected_fib))
rib_uninstall_kernel(rn, dest->selected_fib);
}
}
}
/* Routing information base initialize. */
void rib_init(void)
{
rib_queue_init(&zebrad);
}
/*
* vrf_id_get_next
*
* Get the first vrf id that is greater than the given vrf id if any.
*
* Returns TRUE if a vrf id was found, FALSE otherwise.
*/
static inline int vrf_id_get_next(vrf_id_t vrf_id, vrf_id_t *next_id_p)
{
struct vrf *vrf;
vrf = vrf_lookup_by_id(vrf_id);
if (vrf) {
vrf = RB_NEXT(vrf_id_head, vrf);
if (vrf) {
*next_id_p = vrf->vrf_id;
return 1;
}
}
return 0;
}
/*
* rib_tables_iter_next
*
* Returns the next table in the iteration.
*/
struct route_table *rib_tables_iter_next(rib_tables_iter_t *iter)
{
struct route_table *table;
/*
* Array that helps us go over all AFI/SAFI combinations via one
* index.
*/
static struct {
afi_t afi;
safi_t safi;
} afi_safis[] = {
{AFI_IP, SAFI_UNICAST}, {AFI_IP, SAFI_MULTICAST},
{AFI_IP, SAFI_LABELED_UNICAST}, {AFI_IP6, SAFI_UNICAST},
{AFI_IP6, SAFI_MULTICAST}, {AFI_IP6, SAFI_LABELED_UNICAST},
};
table = NULL;
switch (iter->state) {
case RIB_TABLES_ITER_S_INIT:
iter->vrf_id = VRF_DEFAULT;
iter->afi_safi_ix = -1;
/* Fall through */
case RIB_TABLES_ITER_S_ITERATING:
iter->afi_safi_ix++;
while (1) {
while (iter->afi_safi_ix
< (int)ZEBRA_NUM_OF(afi_safis)) {
table = zebra_vrf_table(
afi_safis[iter->afi_safi_ix].afi,
afi_safis[iter->afi_safi_ix].safi,
iter->vrf_id);
if (table)
break;
iter->afi_safi_ix++;
}
/*
* Found another table in this vrf.
*/
if (table)
break;
/*
* Done with all tables in the current vrf, go to the
* next
* one.
*/
if (!vrf_id_get_next(iter->vrf_id, &iter->vrf_id))
break;
iter->afi_safi_ix = 0;
}
break;
case RIB_TABLES_ITER_S_DONE:
return NULL;
}
if (table)
iter->state = RIB_TABLES_ITER_S_ITERATING;
else
iter->state = RIB_TABLES_ITER_S_DONE;
return table;
}
|