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
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
|
// SPDX-License-Identifier: GPL-2.0-or-later
/* BGP packet management routine.
* Contains utility functions for constructing and consuming BGP messages.
* Copyright (C) 2017 Cumulus Networks
* Copyright (C) 1999 Kunihiro Ishiguro
*/
#include <zebra.h>
#include <sys/time.h>
#include "frrevent.h"
#include "stream.h"
#include "network.h"
#include "prefix.h"
#include "command.h"
#include "log.h"
#include "memory.h"
#include "sockunion.h" /* for inet_ntop () */
#include "sockopt.h"
#include "linklist.h"
#include "plist.h"
#include "queue.h"
#include "filter.h"
#include "lib_errors.h"
#include "bgpd/bgpd.h"
#include "bgpd/bgp_table.h"
#include "bgpd/bgp_dump.h"
#include "bgpd/bgp_bmp.h"
#include "bgpd/bgp_attr.h"
#include "bgpd/bgp_debug.h"
#include "bgpd/bgp_errors.h"
#include "bgpd/bgp_fsm.h"
#include "bgpd/bgp_route.h"
#include "bgpd/bgp_packet.h"
#include "bgpd/bgp_open.h"
#include "bgpd/bgp_aspath.h"
#include "bgpd/bgp_community.h"
#include "bgpd/bgp_ecommunity.h"
#include "bgpd/bgp_lcommunity.h"
#include "bgpd/bgp_network.h"
#include "bgpd/bgp_mplsvpn.h"
#include "bgpd/bgp_evpn.h"
#include "bgpd/bgp_advertise.h"
#include "bgpd/bgp_vty.h"
#include "bgpd/bgp_updgrp.h"
#include "bgpd/bgp_label.h"
#include "bgpd/bgp_io.h"
#include "bgpd/bgp_keepalives.h"
#include "bgpd/bgp_flowspec.h"
#include "bgpd/bgp_linkstate_tlv.h"
#include "bgpd/bgp_trace.h"
DEFINE_HOOK(bgp_packet_dump,
(struct peer *peer, uint8_t type, bgp_size_t size,
struct stream *s),
(peer, type, size, s));
DEFINE_HOOK(bgp_packet_send,
(struct peer *peer, uint8_t type, bgp_size_t size,
struct stream *s),
(peer, type, size, s));
/**
* Sets marker and type fields for a BGP message.
*
* @param s the stream containing the packet
* @param type the packet type
* @return the size of the stream
*/
int bgp_packet_set_marker(struct stream *s, uint8_t type)
{
int i;
/* Fill in marker. */
for (i = 0; i < BGP_MARKER_SIZE; i++)
stream_putc(s, 0xff);
/* Dummy total length. This field is should be filled in later on. */
stream_putw(s, 0);
/* BGP packet type. */
stream_putc(s, type);
/* Return current stream size. */
return stream_get_endp(s);
}
/**
* Sets size field for a BGP message.
*
* Size field is set to the size of the stream passed.
*
* @param s the stream containing the packet
*/
void bgp_packet_set_size(struct stream *s)
{
int cp;
/* Preserve current pointer. */
cp = stream_get_endp(s);
stream_putw_at(s, BGP_MARKER_SIZE, cp);
}
/*
* Push a packet onto the beginning of the peer's output queue.
* This function acquires the peer's write mutex before proceeding.
*/
static void bgp_packet_add(struct peer_connection *connection,
struct peer *peer, struct stream *s)
{
intmax_t delta;
uint32_t holdtime;
intmax_t sendholdtime;
frr_with_mutex (&connection->io_mtx) {
/* if the queue is empty, reset the "last OK" timestamp to
* now, otherwise if we write another packet immediately
* after it'll get confused
*/
if (!stream_fifo_count_safe(connection->obuf))
peer->last_sendq_ok = monotime(NULL);
stream_fifo_push(connection->obuf, s);
delta = monotime(NULL) - peer->last_sendq_ok;
if (CHECK_FLAG(peer->flags, PEER_FLAG_TIMER))
holdtime = atomic_load_explicit(&peer->holdtime,
memory_order_relaxed);
else
holdtime = peer->bgp->default_holdtime;
sendholdtime = holdtime * 2;
/* Note that when we're here, we're adding some packet to the
* OutQ. That includes keepalives when there is nothing to
* do, so there's a guarantee we pass by here once in a while.
*
* That implies there is no need to go set up another separate
* timer that ticks down SendHoldTime, as we'll be here sooner
* or later anyway and will see the checks below failing.
*/
if (!holdtime) {
/* no holdtime, do nothing. */
} else if (delta > sendholdtime) {
flog_err(
EC_BGP_SENDQ_STUCK_PROPER,
"%pBP has not made any SendQ progress for 2 holdtimes (%jds), terminating session",
peer, sendholdtime);
BGP_EVENT_ADD(connection, TCP_fatal_error);
} else if (delta > (intmax_t)holdtime &&
monotime(NULL) - peer->last_sendq_warn > 5) {
flog_warn(
EC_BGP_SENDQ_STUCK_WARN,
"%pBP has not made any SendQ progress for 1 holdtime (%us), peer overloaded?",
peer, holdtime);
peer->last_sendq_warn = monotime(NULL);
}
}
}
static struct stream *bgp_update_packet_eor(struct peer *peer, afi_t afi,
safi_t safi)
{
struct stream *s;
iana_afi_t pkt_afi = IANA_AFI_IPV4;
iana_safi_t pkt_safi = IANA_SAFI_UNICAST;
if (DISABLE_BGP_ANNOUNCE)
return NULL;
if (bgp_debug_neighbor_events(peer))
zlog_debug("send End-of-RIB for %s to %s",
get_afi_safi_str(afi, safi, false), peer->host);
s = stream_new(peer->max_packet_size);
/* Make BGP update packet. */
bgp_packet_set_marker(s, BGP_MSG_UPDATE);
/* Unfeasible Routes Length */
stream_putw(s, 0);
if (afi == AFI_IP && safi == SAFI_UNICAST) {
/* Total Path Attribute Length */
stream_putw(s, 0);
} else {
/* Convert AFI, SAFI to values for packet. */
bgp_map_afi_safi_int2iana(afi, safi, &pkt_afi, &pkt_safi);
/* Total Path Attribute Length */
stream_putw(s, 6);
stream_putc(s, BGP_ATTR_FLAG_OPTIONAL);
stream_putc(s, BGP_ATTR_MP_UNREACH_NLRI);
stream_putc(s, 3);
stream_putw(s, pkt_afi);
stream_putc(s, pkt_safi);
}
bgp_packet_set_size(s);
return s;
}
/* Called when there is a change in the EOR(implicit or explicit) status of a
* peer. Ends the update-delay if all expected peers are done with EORs. */
void bgp_check_update_delay(struct bgp *bgp)
{
struct listnode *node, *nnode;
struct peer *peer = NULL;
if (bgp_debug_neighbor_events(peer))
zlog_debug("Checking update delay, T: %d R: %d I:%d E: %d",
bgp->established, bgp->restarted_peers,
bgp->implicit_eors, bgp->explicit_eors);
if (bgp->established
<= bgp->restarted_peers + bgp->implicit_eors + bgp->explicit_eors) {
/*
* This is an extra sanity check to make sure we wait for all
* the eligible configured peers. This check is performed if
* establish wait timer is on, or establish wait option is not
* given with the update-delay command
*/
if (bgp->t_establish_wait
|| (bgp->v_establish_wait == bgp->v_update_delay))
for (ALL_LIST_ELEMENTS(bgp->peer, node, nnode, peer)) {
if (CHECK_FLAG(peer->flags,
PEER_FLAG_CONFIG_NODE)
&& !CHECK_FLAG(peer->flags,
PEER_FLAG_SHUTDOWN)
&& !CHECK_FLAG(peer->bgp->flags,
BGP_FLAG_SHUTDOWN)
&& !peer->update_delay_over) {
if (bgp_debug_neighbor_events(peer))
zlog_debug(
" Peer %s pending, continuing read-only mode",
peer->host);
return;
}
}
zlog_info(
"Update delay ended, restarted: %d, EORs implicit: %d, explicit: %d",
bgp->restarted_peers, bgp->implicit_eors,
bgp->explicit_eors);
bgp_update_delay_end(bgp);
}
}
/*
* Called if peer is known to have restarted. The restart-state bit in
* Graceful-Restart capability is used for that
*/
void bgp_update_restarted_peers(struct peer *peer)
{
if (!bgp_update_delay_active(peer->bgp))
return; /* BGP update delay has ended */
if (peer->update_delay_over)
return; /* This peer has already been considered */
if (bgp_debug_neighbor_events(peer))
zlog_debug("Peer %s: Checking restarted", peer->host);
if (peer_established(peer->connection)) {
peer->update_delay_over = 1;
peer->bgp->restarted_peers++;
bgp_check_update_delay(peer->bgp);
}
}
/*
* Called as peer receives a keep-alive. Determines if this occurence can be
* taken as an implicit EOR for this peer.
* NOTE: The very first keep-alive after the Established state of a peer is
* considered implicit EOR for the update-delay purposes
*/
void bgp_update_implicit_eors(struct peer *peer)
{
if (!bgp_update_delay_active(peer->bgp))
return; /* BGP update delay has ended */
if (peer->update_delay_over)
return; /* This peer has already been considered */
if (bgp_debug_neighbor_events(peer))
zlog_debug("Peer %s: Checking implicit EORs", peer->host);
if (peer_established(peer->connection)) {
peer->update_delay_over = 1;
peer->bgp->implicit_eors++;
bgp_check_update_delay(peer->bgp);
}
}
/*
* Should be called only when there is a change in the EOR_RECEIVED status
* for any afi/safi on a peer.
*/
static void bgp_update_explicit_eors(struct peer *peer)
{
afi_t afi;
safi_t safi;
if (!bgp_update_delay_active(peer->bgp))
return; /* BGP update delay has ended */
if (peer->update_delay_over)
return; /* This peer has already been considered */
if (bgp_debug_neighbor_events(peer))
zlog_debug("Peer %s: Checking explicit EORs", peer->host);
FOREACH_AFI_SAFI (afi, safi) {
if (peer->afc_nego[afi][safi]
&& !CHECK_FLAG(peer->af_sflags[afi][safi],
PEER_STATUS_EOR_RECEIVED)) {
if (bgp_debug_neighbor_events(peer))
zlog_debug(
" afi %d safi %d didn't receive EOR",
afi, safi);
return;
}
}
peer->update_delay_over = 1;
peer->bgp->explicit_eors++;
bgp_check_update_delay(peer->bgp);
}
/**
* Frontend for NLRI parsing, to fan-out to AFI/SAFI specific parsers.
*
* mp_withdraw, if set, is used to nullify attr structure on most of the
* calling safi function and for evpn, passed as parameter
*/
int bgp_nlri_parse(struct peer *peer, struct attr *attr,
struct bgp_nlri *packet, bool mp_withdraw)
{
switch (packet->safi) {
case SAFI_UNICAST:
case SAFI_MULTICAST:
return bgp_nlri_parse_ip(peer, mp_withdraw ? NULL : attr,
packet);
case SAFI_LABELED_UNICAST:
return bgp_nlri_parse_label(peer, mp_withdraw ? NULL : attr,
packet);
case SAFI_MPLS_VPN:
return bgp_nlri_parse_vpn(peer, mp_withdraw ? NULL : attr,
packet);
case SAFI_EVPN:
return bgp_nlri_parse_evpn(peer, attr, packet, mp_withdraw);
case SAFI_FLOWSPEC:
return bgp_nlri_parse_flowspec(peer, attr, packet, mp_withdraw);
case SAFI_LINKSTATE:
return bgp_nlri_parse_linkstate(peer, attr, packet,
mp_withdraw);
}
return BGP_NLRI_PARSE_ERROR;
}
/*
* Check if route-refresh request from peer is pending (received before EoR),
* and process it now.
*/
static void bgp_process_pending_refresh(struct peer *peer, afi_t afi,
safi_t safi)
{
if (CHECK_FLAG(peer->af_sflags[afi][safi],
PEER_STATUS_REFRESH_PENDING)) {
UNSET_FLAG(peer->af_sflags[afi][safi],
PEER_STATUS_REFRESH_PENDING);
bgp_route_refresh_send(peer, afi, safi, 0, 0, 0,
BGP_ROUTE_REFRESH_BORR);
if (bgp_debug_neighbor_events(peer))
zlog_debug(
"%pBP sending route-refresh (BoRR) for %s/%s (for pending REQUEST)",
peer, afi2str(afi), safi2str(safi));
SET_FLAG(peer->af_sflags[afi][safi], PEER_STATUS_BORR_SEND);
UNSET_FLAG(peer->af_sflags[afi][safi], PEER_STATUS_EORR_SEND);
bgp_announce_route(peer, afi, safi, true);
}
}
/*
* Checks a variety of conditions to determine whether the peer needs to be
* rescheduled for packet generation again, and does so if necessary.
*
* @param peer to check for rescheduling
*/
static void bgp_write_proceed_actions(struct peer *peer)
{
afi_t afi;
safi_t safi;
struct peer_af *paf;
struct bpacket *next_pkt;
struct update_subgroup *subgrp;
enum bgp_af_index index;
struct peer_connection *connection = peer->connection;
for (index = BGP_AF_START; index < BGP_AF_MAX; index++) {
paf = peer->peer_af_array[index];
if (!paf)
continue;
subgrp = paf->subgroup;
if (!subgrp)
continue;
next_pkt = paf->next_pkt_to_send;
if (next_pkt && next_pkt->buffer) {
BGP_TIMER_ON(connection->t_generate_updgrp_packets,
bgp_generate_updgrp_packets, 0);
return;
}
/* No packets readily available for AFI/SAFI, are there
* subgroup packets
* that need to be generated? */
if (bpacket_queue_is_full(SUBGRP_INST(subgrp),
SUBGRP_PKTQ(subgrp))
|| subgroup_packets_to_build(subgrp)) {
BGP_TIMER_ON(connection->t_generate_updgrp_packets,
bgp_generate_updgrp_packets, 0);
return;
}
afi = paf->afi;
safi = paf->safi;
/* No packets to send, see if EOR is pending */
if (CHECK_FLAG(peer->cap, PEER_CAP_RESTART_RCV)) {
if (!subgrp->t_coalesce && peer->afc_nego[afi][safi]
&& peer->synctime
&& !CHECK_FLAG(peer->af_sflags[afi][safi],
PEER_STATUS_EOR_SEND)
&& safi != SAFI_MPLS_VPN) {
BGP_TIMER_ON(connection->t_generate_updgrp_packets,
bgp_generate_updgrp_packets, 0);
return;
}
}
}
}
/*
* Generate advertisement information (withdraws, updates, EOR) from each
* update group a peer belongs to, encode this information into packets, and
* enqueue the packets onto the peer's output buffer.
*/
void bgp_generate_updgrp_packets(struct event *thread)
{
struct peer_connection *connection = EVENT_ARG(thread);
struct peer *peer = connection->peer;
struct stream *s;
struct peer_af *paf;
struct bpacket *next_pkt;
uint32_t wpq;
uint32_t generated = 0;
afi_t afi;
safi_t safi;
wpq = atomic_load_explicit(&peer->bgp->wpkt_quanta,
memory_order_relaxed);
/*
* The code beyond this part deals with update packets, proceed only
* if peer is Established and updates are not on hold (as part of
* update-delay processing).
*/
if (!peer_established(peer->connection))
return;
if ((peer->bgp->main_peers_update_hold)
|| bgp_update_delay_active(peer->bgp))
return;
if (peer->connection->t_routeadv)
return;
/*
* Since the following is a do while loop
* let's stop adding to the outq if we are
* already at the limit.
*/
if (connection->obuf->count >= bm->outq_limit) {
bgp_write_proceed_actions(peer);
return;
}
do {
enum bgp_af_index index;
s = NULL;
for (index = BGP_AF_START; index < BGP_AF_MAX; index++) {
paf = peer->peer_af_array[index];
if (!paf || !PAF_SUBGRP(paf))
continue;
afi = paf->afi;
safi = paf->safi;
next_pkt = paf->next_pkt_to_send;
/*
* Try to generate a packet for the peer if we are at
* the end of the list. Always try to push out
* WITHDRAWs first.
*/
if (!next_pkt || !next_pkt->buffer) {
next_pkt = subgroup_withdraw_packet(
PAF_SUBGRP(paf));
if (!next_pkt || !next_pkt->buffer)
subgroup_update_packet(PAF_SUBGRP(paf));
next_pkt = paf->next_pkt_to_send;
}
/*
* If we still don't have a packet to send to the peer,
* then try to find out out if we have to send eor or
* if not, skip to the next AFI, SAFI. Don't send the
* EOR prematurely; if the subgroup's coalesce timer is
* running, the adjacency-out structure is not created
* yet.
*/
if (!next_pkt || !next_pkt->buffer) {
if (!paf->t_announce_route) {
/* Make sure we supress BGP UPDATES
* for normal processing later again.
*/
UNSET_FLAG(paf->subgroup->sflags,
SUBGRP_STATUS_FORCE_UPDATES);
/* If route-refresh BoRR message was
* already sent and we are done with
* re-announcing tables for a decent
* afi/safi, we ready to send
* EoRR request.
*/
if (CHECK_FLAG(
peer->af_sflags[afi][safi],
PEER_STATUS_BORR_SEND)) {
bgp_route_refresh_send(
peer, afi, safi, 0, 0,
0,
BGP_ROUTE_REFRESH_EORR);
SET_FLAG(peer->af_sflags[afi]
[safi],
PEER_STATUS_EORR_SEND);
UNSET_FLAG(
peer->af_sflags[afi]
[safi],
PEER_STATUS_BORR_SEND);
if (bgp_debug_neighbor_events(
peer))
zlog_debug(
"%pBP sending route-refresh (EoRR) for %s/%s",
peer,
afi2str(afi),
safi2str(safi));
}
}
if (CHECK_FLAG(peer->cap,
PEER_CAP_RESTART_RCV)) {
if (!(PAF_SUBGRP(paf))->t_coalesce
&& peer->afc_nego[afi][safi]
&& peer->synctime
&& !CHECK_FLAG(
peer->af_sflags[afi][safi],
PEER_STATUS_EOR_SEND)) {
/* If EOR is disabled,
* the message is not sent
*/
if (BGP_SEND_EOR(peer->bgp, afi,
safi)) {
SET_FLAG(
peer->af_sflags
[afi]
[safi],
PEER_STATUS_EOR_SEND);
/* Update EOR
* send time
*/
peer->eor_stime[afi]
[safi] =
monotime(NULL);
BGP_UPDATE_EOR_PKT(
peer, afi, safi,
s);
bgp_process_pending_refresh(
peer, afi,
safi);
}
}
}
continue;
}
/* Update packet send time */
peer->pkt_stime[afi][safi] = monotime(NULL);
/* Found a packet template to send, overwrite
* packet with appropriate attributes from peer
* and advance peer */
s = bpacket_reformat_for_peer(next_pkt, paf);
bgp_packet_add(connection, peer, s);
bpacket_queue_advance_peer(paf);
}
} while (s && (++generated < wpq) &&
(connection->obuf->count <= bm->outq_limit));
if (generated)
bgp_writes_on(connection);
bgp_write_proceed_actions(peer);
}
/*
* Creates a BGP Keepalive packet and appends it to the peer's output queue.
*/
void bgp_keepalive_send(struct peer *peer)
{
struct stream *s;
s = stream_new(BGP_STANDARD_MESSAGE_MAX_PACKET_SIZE);
/* Make keepalive packet. */
bgp_packet_set_marker(s, BGP_MSG_KEEPALIVE);
/* Set packet size. */
bgp_packet_set_size(s);
/* Dump packet if debug option is set. */
/* bgp_packet_dump (s); */
if (bgp_debug_keepalive(peer))
zlog_debug("%s sending KEEPALIVE", peer->host);
/* Add packet to the peer. */
bgp_packet_add(peer->connection, peer, s);
bgp_writes_on(peer->connection);
}
/*
* Creates a BGP Open packet and appends it to the peer's output queue.
* Sets capabilities as necessary.
*/
void bgp_open_send(struct peer_connection *connection)
{
struct stream *s;
uint16_t send_holdtime;
as_t local_as;
struct peer *peer = connection->peer;
if (CHECK_FLAG(peer->flags, PEER_FLAG_TIMER))
send_holdtime = peer->holdtime;
else
send_holdtime = peer->bgp->default_holdtime;
/* local-as Change */
if (peer->change_local_as)
local_as = peer->change_local_as;
else
local_as = peer->local_as;
s = stream_new(BGP_STANDARD_MESSAGE_MAX_PACKET_SIZE);
/* Make open packet. */
bgp_packet_set_marker(s, BGP_MSG_OPEN);
/* Set open packet values. */
stream_putc(s, BGP_VERSION_4); /* BGP version */
stream_putw(s, (local_as <= BGP_AS_MAX) ? (uint16_t)local_as
: BGP_AS_TRANS);
stream_putw(s, send_holdtime); /* Hold Time */
stream_put_in_addr(s, &peer->local_id); /* BGP Identifier */
/* Set capabilities */
if (CHECK_FLAG(peer->flags, PEER_FLAG_EXTENDED_OPT_PARAMS)) {
(void)bgp_open_capability(s, peer, true);
} else {
struct stream *tmp = stream_new(STREAM_SIZE(s));
stream_copy(tmp, s);
if (bgp_open_capability(tmp, peer, false)
> BGP_OPEN_NON_EXT_OPT_LEN) {
stream_free(tmp);
(void)bgp_open_capability(s, peer, true);
} else {
stream_copy(s, tmp);
stream_free(tmp);
}
}
/* Set BGP packet length. */
bgp_packet_set_size(s);
if (bgp_debug_neighbor_events(peer))
zlog_debug(
"%s sending OPEN, version %d, my as %u, holdtime %d, id %pI4",
peer->host, BGP_VERSION_4, local_as, send_holdtime,
&peer->local_id);
/* Dump packet if debug option is set. */
/* bgp_packet_dump (s); */
hook_call(bgp_packet_send, peer, BGP_MSG_OPEN, stream_get_endp(s), s);
/* Add packet to the peer. */
bgp_packet_add(connection, peer, s);
bgp_writes_on(connection);
}
/*
* Writes NOTIFICATION message directly to a peer socket without waiting for
* the I/O thread.
*
* There must be exactly one stream on the peer->connection->obuf FIFO, and the
* data within this stream must match the format of a BGP NOTIFICATION message.
* Transmission is best-effort.
*
* @requires peer->connection->io_mtx
* @param peer
* @return 0
*/
static void bgp_write_notify(struct peer_connection *connection,
struct peer *peer)
{
int ret, val;
uint8_t type;
struct stream *s;
/* There should be at least one packet. */
s = stream_fifo_pop(connection->obuf);
if (!s)
return;
assert(stream_get_endp(s) >= BGP_HEADER_SIZE);
/*
* socket is in nonblocking mode, if we can't deliver the NOTIFY, well,
* we only care about getting a clean shutdown at this point.
*/
ret = write(connection->fd, STREAM_DATA(s), stream_get_endp(s));
/*
* only connection reset/close gets counted as TCP_fatal_error, failure
* to write the entire NOTIFY doesn't get different FSM treatment
*/
if (ret <= 0) {
stream_free(s);
BGP_EVENT_ADD(connection, TCP_fatal_error);
return;
}
/* Disable Nagle, make NOTIFY packet go out right away */
val = 1;
(void)setsockopt(connection->fd, IPPROTO_TCP, TCP_NODELAY, (char *)&val,
sizeof(val));
/* Retrieve BGP packet type. */
stream_set_getp(s, BGP_MARKER_SIZE + 2);
type = stream_getc(s);
assert(type == BGP_MSG_NOTIFY);
/* Type should be notify. */
atomic_fetch_add_explicit(&peer->notify_out, 1, memory_order_relaxed);
/* Double start timer. */
peer->v_start *= 2;
/* Overflow check. */
if (peer->v_start >= (60 * 2))
peer->v_start = (60 * 2);
/*
* Handle Graceful Restart case where the state changes to
* Connect instead of Idle
*/
BGP_EVENT_ADD(connection, BGP_Stop);
stream_free(s);
}
/*
* Encapsulate an original BGP CEASE Notification into Hard Reset
*/
static uint8_t *bgp_notify_encapsulate_hard_reset(uint8_t code, uint8_t subcode,
uint8_t *data, size_t datalen)
{
uint8_t *message = XCALLOC(MTYPE_BGP_NOTIFICATION, datalen + 2);
/* ErrCode */
message[0] = code;
/* Subcode */
message[1] = subcode;
/* Data */
if (datalen)
memcpy(message + 2, data, datalen);
return message;
}
/*
* Decapsulate an original BGP CEASE Notification from Hard Reset
*/
struct bgp_notify bgp_notify_decapsulate_hard_reset(struct bgp_notify *notify)
{
struct bgp_notify bn = {};
bn.code = notify->raw_data[0];
bn.subcode = notify->raw_data[1];
bn.length = notify->length - 2;
bn.raw_data = XMALLOC(MTYPE_BGP_NOTIFICATION, bn.length);
memcpy(bn.raw_data, notify->raw_data + 2, bn.length);
return bn;
}
/* Check if Graceful-Restart N-bit is exchanged */
bool bgp_has_graceful_restart_notification(struct peer *peer)
{
return CHECK_FLAG(peer->cap, PEER_CAP_GRACEFUL_RESTART_N_BIT_RCV) &&
CHECK_FLAG(peer->cap, PEER_CAP_GRACEFUL_RESTART_N_BIT_ADV);
}
/*
* Check if to send BGP CEASE Notification/Hard Reset?
*/
bool bgp_notify_send_hard_reset(struct peer *peer, uint8_t code,
uint8_t subcode)
{
/* When the "N" bit has been exchanged, a Hard Reset message is used to
* indicate to the peer that the session is to be fully terminated.
*/
if (!bgp_has_graceful_restart_notification(peer))
return false;
/*
* https://datatracker.ietf.org/doc/html/rfc8538#section-5.1
*/
if (code == BGP_NOTIFY_CEASE) {
switch (subcode) {
case BGP_NOTIFY_CEASE_MAX_PREFIX:
case BGP_NOTIFY_CEASE_ADMIN_SHUTDOWN:
case BGP_NOTIFY_CEASE_PEER_UNCONFIG:
case BGP_NOTIFY_CEASE_HARD_RESET:
case BGP_NOTIFY_CEASE_BFD_DOWN:
return true;
case BGP_NOTIFY_CEASE_ADMIN_RESET:
/* Provide user control:
* `bgp hard-adminstrative-reset`
*/
if (CHECK_FLAG(peer->bgp->flags,
BGP_FLAG_HARD_ADMIN_RESET))
return true;
else
return false;
default:
break;
}
}
return false;
}
/*
* Check if received BGP CEASE Notification/Hard Reset?
*/
bool bgp_notify_received_hard_reset(struct peer *peer, uint8_t code,
uint8_t subcode)
{
/* When the "N" bit has been exchanged, a Hard Reset message is used to
* indicate to the peer that the session is to be fully terminated.
*/
if (!bgp_has_graceful_restart_notification(peer))
return false;
if (code == BGP_NOTIFY_CEASE && subcode == BGP_NOTIFY_CEASE_HARD_RESET)
return true;
return false;
}
/*
* Creates a BGP Notify and appends it to the peer's output queue.
*
* This function attempts to write the packet from the thread it is called
* from, to ensure the packet gets out ASAP.
*
* This function may be called from multiple threads. Since the function
* modifies I/O buffer(s) in the peer, these are locked for the duration of the
* call to prevent tampering from other threads.
*
* Delivery of the NOTIFICATION is attempted once and is best-effort. After
* return, the peer structure *must* be reset; no assumptions about session
* state are valid.
*
* @param peer
* @param code BGP error code
* @param sub_code BGP error subcode
* @param data Data portion
* @param datalen length of data portion
*/
static void bgp_notify_send_internal(struct peer_connection *connection,
uint8_t code, uint8_t sub_code,
uint8_t *data, size_t datalen,
bool use_curr)
{
struct stream *s;
struct peer *peer = connection->peer;
bool hard_reset = bgp_notify_send_hard_reset(peer, code, sub_code);
/* Lock I/O mutex to prevent other threads from pushing packets */
frr_mutex_lock_autounlock(&connection->io_mtx);
/* ============================================== */
/* Allocate new stream. */
s = stream_new(peer->max_packet_size);
/* Make notify packet. */
bgp_packet_set_marker(s, BGP_MSG_NOTIFY);
/* Check if we should send Hard Reset Notification or not */
if (hard_reset) {
uint8_t *hard_reset_message = bgp_notify_encapsulate_hard_reset(
code, sub_code, data, datalen);
/* Hard Reset encapsulates another NOTIFICATION message
* in its data portion.
*/
stream_putc(s, BGP_NOTIFY_CEASE);
stream_putc(s, BGP_NOTIFY_CEASE_HARD_RESET);
stream_write(s, hard_reset_message, datalen + 2);
XFREE(MTYPE_BGP_NOTIFICATION, hard_reset_message);
} else {
stream_putc(s, code);
stream_putc(s, sub_code);
if (data)
stream_write(s, data, datalen);
}
/* Set BGP packet length. */
bgp_packet_set_size(s);
/* wipe output buffer */
stream_fifo_clean(connection->obuf);
/*
* If possible, store last packet for debugging purposes. This check is
* in place because we are sometimes called with a doppelganger peer,
* who tends to have a plethora of fields nulled out.
*
* Some callers should not attempt this - the io pthread for example
* should not touch internals of the peer struct.
*/
if (use_curr && peer->curr) {
size_t packetsize = stream_get_endp(peer->curr);
assert(packetsize <= peer->max_packet_size);
if (peer->last_reset_cause)
stream_free(peer->last_reset_cause);
peer->last_reset_cause = stream_dup(peer->curr);
}
/* For debug */
{
struct bgp_notify bgp_notify;
int first = 0;
int i;
char c[4];
bgp_notify.code = code;
bgp_notify.subcode = sub_code;
bgp_notify.data = NULL;
bgp_notify.length = datalen;
bgp_notify.raw_data = data;
peer->notify.code = bgp_notify.code;
peer->notify.subcode = bgp_notify.subcode;
peer->notify.length = bgp_notify.length;
if (bgp_notify.length && data) {
bgp_notify.data = XMALLOC(MTYPE_BGP_NOTIFICATION,
bgp_notify.length * 3);
for (i = 0; i < bgp_notify.length; i++)
if (first) {
snprintf(c, sizeof(c), " %02x",
data[i]);
strlcat(bgp_notify.data, c,
bgp_notify.length);
} else {
first = 1;
snprintf(c, sizeof(c), "%02x", data[i]);
strlcpy(bgp_notify.data, c,
bgp_notify.length);
}
}
bgp_notify_print(peer, &bgp_notify, "sending", hard_reset);
if (bgp_notify.data) {
if (data) {
XFREE(MTYPE_BGP_NOTIFICATION,
peer->notify.data);
peer->notify.data = XCALLOC(
MTYPE_BGP_NOTIFICATION, datalen);
memcpy(peer->notify.data, data, datalen);
}
XFREE(MTYPE_BGP_NOTIFICATION, bgp_notify.data);
bgp_notify.length = 0;
}
}
/* peer reset cause */
if (code == BGP_NOTIFY_CEASE) {
if (sub_code == BGP_NOTIFY_CEASE_ADMIN_RESET)
peer->last_reset = PEER_DOWN_USER_RESET;
else if (sub_code == BGP_NOTIFY_CEASE_ADMIN_SHUTDOWN) {
if (CHECK_FLAG(peer->sflags, PEER_STATUS_RTT_SHUTDOWN))
peer->last_reset = PEER_DOWN_RTT_SHUTDOWN;
else
peer->last_reset = PEER_DOWN_USER_SHUTDOWN;
} else
peer->last_reset = PEER_DOWN_NOTIFY_SEND;
} else
peer->last_reset = PEER_DOWN_NOTIFY_SEND;
/* Add packet to peer's output queue */
stream_fifo_push(connection->obuf, s);
bgp_peer_gr_flags_update(peer);
BGP_GR_ROUTER_DETECT_AND_SEND_CAPABILITY_TO_ZEBRA(peer->bgp,
peer->bgp->peer);
bgp_write_notify(connection, peer);
}
/*
* Creates a BGP Notify and appends it to the peer's output queue.
*
* This function attempts to write the packet from the thread it is called
* from, to ensure the packet gets out ASAP.
*
* @param peer
* @param code BGP error code
* @param sub_code BGP error subcode
*/
void bgp_notify_send(struct peer_connection *connection, uint8_t code,
uint8_t sub_code)
{
bgp_notify_send_internal(connection, code, sub_code, NULL, 0, true);
}
/*
* Enqueue notification; called from the main pthread, peer object access is ok.
*/
void bgp_notify_send_with_data(struct peer_connection *connection, uint8_t code,
uint8_t sub_code, uint8_t *data, size_t datalen)
{
bgp_notify_send_internal(connection, code, sub_code, data, datalen,
true);
}
/*
* For use by the io pthread, queueing a notification but avoiding access to
* the peer object.
*/
void bgp_notify_io_invalid(struct peer *peer, uint8_t code, uint8_t sub_code,
uint8_t *data, size_t datalen)
{
/* Avoid touching the peer object */
bgp_notify_send_internal(peer->connection, code, sub_code, data,
datalen, false);
}
/*
* Creates BGP Route Refresh packet and appends it to the peer's output queue.
*
* @param peer
* @param afi Address Family Identifier
* @param safi Subsequent Address Family Identifier
* @param orf_type Outbound Route Filtering type
* @param when_to_refresh Whether to refresh immediately or defer
* @param remove Whether to remove ORF for specified AFI/SAFI
* @param subtype BGP enhanced route refresh optional subtypes
*/
void bgp_route_refresh_send(struct peer *peer, afi_t afi, safi_t safi,
uint8_t orf_type, uint8_t when_to_refresh,
int remove, uint8_t subtype)
{
struct stream *s;
struct bgp_filter *filter;
int orf_refresh = 0;
iana_afi_t pkt_afi = IANA_AFI_IPV4;
iana_safi_t pkt_safi = IANA_SAFI_UNICAST;
if (DISABLE_BGP_ANNOUNCE)
return;
filter = &peer->filter[afi][safi];
/* Convert AFI, SAFI to values for packet. */
bgp_map_afi_safi_int2iana(afi, safi, &pkt_afi, &pkt_safi);
s = stream_new(peer->max_packet_size);
/* Make BGP update packet. */
if (CHECK_FLAG(peer->cap, PEER_CAP_REFRESH_RCV))
bgp_packet_set_marker(s, BGP_MSG_ROUTE_REFRESH_NEW);
else
bgp_packet_set_marker(s, BGP_MSG_ROUTE_REFRESH_OLD);
/* Encode Route Refresh message. */
stream_putw(s, pkt_afi);
if (subtype)
stream_putc(s, subtype);
else
stream_putc(s, 0);
stream_putc(s, pkt_safi);
if (orf_type == ORF_TYPE_PREFIX)
if (remove || filter->plist[FILTER_IN].plist) {
uint16_t orf_len;
unsigned long orfp;
orf_refresh = 1;
stream_putc(s, when_to_refresh);
stream_putc(s, orf_type);
orfp = stream_get_endp(s);
stream_putw(s, 0);
if (remove) {
UNSET_FLAG(peer->af_sflags[afi][safi],
PEER_STATUS_ORF_PREFIX_SEND);
stream_putc(s, ORF_COMMON_PART_REMOVE_ALL);
if (bgp_debug_neighbor_events(peer))
zlog_debug(
"%pBP sending REFRESH_REQ to remove ORF(%d) (%s) for afi/safi: %s/%s",
peer, orf_type,
(when_to_refresh ==
REFRESH_DEFER
? "defer"
: "immediate"),
iana_afi2str(pkt_afi),
iana_safi2str(pkt_safi));
} else {
SET_FLAG(peer->af_sflags[afi][safi],
PEER_STATUS_ORF_PREFIX_SEND);
prefix_bgp_orf_entry(
s, filter->plist[FILTER_IN].plist,
ORF_COMMON_PART_ADD,
ORF_COMMON_PART_PERMIT,
ORF_COMMON_PART_DENY);
if (bgp_debug_neighbor_events(peer))
zlog_debug(
"%pBP sending REFRESH_REQ with pfxlist ORF(%d) (%s) for afi/safi: %s/%s",
peer, orf_type,
(when_to_refresh ==
REFRESH_DEFER
? "defer"
: "immediate"),
iana_afi2str(pkt_afi),
iana_safi2str(pkt_safi));
}
/* Total ORF Entry Len. */
orf_len = stream_get_endp(s) - orfp - 2;
stream_putw_at(s, orfp, orf_len);
}
/* Set packet size. */
bgp_packet_set_size(s);
if (bgp_debug_neighbor_events(peer)) {
if (!orf_refresh)
zlog_debug(
"%pBP sending REFRESH_REQ for afi/safi: %s/%s",
peer, iana_afi2str(pkt_afi),
iana_safi2str(pkt_safi));
}
/* Add packet to the peer. */
bgp_packet_add(peer->connection, peer, s);
bgp_writes_on(peer->connection);
}
/*
* Create a BGP Capability packet and append it to the peer's output queue.
*
* @param peer
* @param afi Address Family Identifier
* @param safi Subsequent Address Family Identifier
* @param capability_code BGP Capability Code
* @param action Set or Remove capability
*/
void bgp_capability_send(struct peer *peer, afi_t afi, safi_t safi,
int capability_code, int action)
{
struct stream *s;
iana_afi_t pkt_afi = IANA_AFI_IPV4;
iana_safi_t pkt_safi = IANA_SAFI_UNICAST;
unsigned long cap_len;
uint16_t len;
uint32_t gr_restart_time;
const char *capability = lookup_msg(capcode_str, capability_code,
"Unknown");
if (!peer_established(peer->connection))
return;
if (!CHECK_FLAG(peer->cap, PEER_CAP_DYNAMIC_RCV) &&
!CHECK_FLAG(peer->cap, PEER_CAP_DYNAMIC_ADV))
return;
/* Convert AFI, SAFI to values for packet. */
bgp_map_afi_safi_int2iana(afi, safi, &pkt_afi, &pkt_safi);
s = stream_new(peer->max_packet_size);
/* Make BGP update packet. */
bgp_packet_set_marker(s, BGP_MSG_CAPABILITY);
/* Encode MP_EXT capability. */
switch (capability_code) {
case CAPABILITY_CODE_SOFT_VERSION:
SET_FLAG(peer->cap, PEER_CAP_SOFT_VERSION_ADV);
stream_putc(s, action);
stream_putc(s, CAPABILITY_CODE_SOFT_VERSION);
cap_len = stream_get_endp(s);
stream_putc(s, 0); /* Capability Length */
/* The Capability Length SHOULD be no greater than 64.
* This is the limit to allow other capabilities as much
* space as they require.
*/
const char *soft_version = cmd_software_version_get();
len = strlen(soft_version);
if (len > BGP_MAX_SOFT_VERSION)
len = BGP_MAX_SOFT_VERSION;
stream_putc(s, len);
stream_put(s, soft_version, len);
/* Software Version capability Len. */
len = stream_get_endp(s) - cap_len - 1;
stream_putc_at(s, cap_len, len);
if (bgp_debug_neighbor_events(peer))
zlog_debug("%pBP sending CAPABILITY has %s %s for afi/safi: %s/%s",
peer,
action == CAPABILITY_ACTION_SET
? "Advertising"
: "Removing",
capability, iana_afi2str(pkt_afi),
iana_safi2str(pkt_safi));
break;
case CAPABILITY_CODE_MP:
stream_putc(s, action);
stream_putc(s, CAPABILITY_CODE_MP);
stream_putc(s, CAPABILITY_CODE_MP_LEN);
stream_putw(s, pkt_afi);
stream_putc(s, 0);
stream_putc(s, pkt_safi);
if (bgp_debug_neighbor_events(peer))
zlog_debug("%pBP sending CAPABILITY has %s %s for afi/safi: %s/%s",
peer,
action == CAPABILITY_ACTION_SET
? "Advertising"
: "Removing",
capability, iana_afi2str(pkt_afi),
iana_safi2str(pkt_safi));
break;
case CAPABILITY_CODE_RESTART:
if (!CHECK_FLAG(peer->flags, PEER_FLAG_GRACEFUL_RESTART) &&
!CHECK_FLAG(peer->flags, PEER_FLAG_GRACEFUL_RESTART_HELPER))
return;
SET_FLAG(peer->cap, PEER_CAP_RESTART_ADV);
stream_putc(s, action);
stream_putc(s, CAPABILITY_CODE_RESTART);
cap_len = stream_get_endp(s);
stream_putc(s, 0);
gr_restart_time = peer->bgp->restart_time;
if (peer->bgp->t_startup) {
SET_FLAG(gr_restart_time, GRACEFUL_RESTART_R_BIT);
SET_FLAG(peer->cap, PEER_CAP_GRACEFUL_RESTART_R_BIT_ADV);
}
if (CHECK_FLAG(peer->bgp->flags,
BGP_FLAG_GRACEFUL_NOTIFICATION)) {
SET_FLAG(gr_restart_time, GRACEFUL_RESTART_N_BIT);
SET_FLAG(peer->cap, PEER_CAP_GRACEFUL_RESTART_N_BIT_ADV);
}
stream_putw(s, gr_restart_time);
if (CHECK_FLAG(peer->flags, PEER_FLAG_GRACEFUL_RESTART)) {
FOREACH_AFI_SAFI (afi, safi) {
if (!peer->afc[afi][safi])
continue;
bgp_map_afi_safi_int2iana(afi, safi, &pkt_afi,
&pkt_safi);
stream_putw(s, pkt_afi);
stream_putc(s, pkt_safi);
if (CHECK_FLAG(peer->bgp->flags,
BGP_FLAG_GR_PRESERVE_FWD))
stream_putc(s, GRACEFUL_RESTART_F_BIT);
else
stream_putc(s, 0);
}
}
len = stream_get_endp(s) - cap_len - 1;
stream_putc_at(s, cap_len, len);
if (bgp_debug_neighbor_events(peer))
zlog_debug("%pBP sending CAPABILITY has %s %s for afi/safi: %s/%s",
peer,
action == CAPABILITY_ACTION_SET
? "Advertising"
: "Removing",
capability, iana_afi2str(pkt_afi),
iana_safi2str(pkt_safi));
break;
case CAPABILITY_CODE_LLGR:
if (!CHECK_FLAG(peer->cap, PEER_CAP_RESTART_ADV))
return;
SET_FLAG(peer->cap, PEER_CAP_LLGR_ADV);
stream_putc(s, action);
stream_putc(s, CAPABILITY_CODE_LLGR);
cap_len = stream_get_endp(s);
stream_putc(s, 0);
FOREACH_AFI_SAFI (afi, safi) {
if (!peer->afc[afi][safi])
continue;
bgp_map_afi_safi_int2iana(afi, safi, &pkt_afi,
&pkt_safi);
stream_putw(s, pkt_afi);
stream_putc(s, pkt_safi);
stream_putc(s, LLGR_F_BIT);
stream_put3(s, peer->bgp->llgr_stale_time);
SET_FLAG(peer->af_cap[afi][safi], PEER_CAP_LLGR_AF_ADV);
}
len = stream_get_endp(s) - cap_len - 1;
stream_putc_at(s, cap_len, len);
if (bgp_debug_neighbor_events(peer))
zlog_debug("%pBP sending CAPABILITY has %s %s for afi/safi: %s/%s",
peer,
action == CAPABILITY_ACTION_SET
? "Advertising"
: "Removing",
capability, iana_afi2str(pkt_afi),
iana_safi2str(pkt_safi));
break;
case CAPABILITY_CODE_REFRESH:
case CAPABILITY_CODE_ORF:
case CAPABILITY_CODE_AS4:
case CAPABILITY_CODE_DYNAMIC:
case CAPABILITY_CODE_ADDPATH:
case CAPABILITY_CODE_ENHANCED_RR:
case CAPABILITY_CODE_FQDN:
case CAPABILITY_CODE_ENHE:
case CAPABILITY_CODE_EXT_MESSAGE:
break;
case CAPABILITY_CODE_ROLE:
if (peer->local_role != ROLE_UNDEFINED) {
SET_FLAG(peer->cap, PEER_CAP_ROLE_ADV);
stream_putc(s, action);
stream_putc(s, CAPABILITY_CODE_ROLE);
stream_putc(s, CAPABILITY_CODE_ROLE_LEN);
stream_putc(s, peer->local_role);
}
break;
default:
break;
}
/* Set packet size. */
bgp_packet_set_size(s);
/* Add packet to the peer. */
bgp_packet_add(peer->connection, peer, s);
bgp_writes_on(peer->connection);
}
/* RFC1771 6.8 Connection collision detection. */
static int bgp_collision_detect(struct peer_connection *connection,
struct peer *new, struct in_addr remote_id)
{
struct peer *peer;
struct peer_connection *other;
/*
* Upon receipt of an OPEN message, the local system must examine
* all of its connections that are in the OpenConfirm state. A BGP
* speaker may also examine connections in an OpenSent state if it
* knows the BGP Identifier of the peer by means outside of the
* protocol. If among these connections there is a connection to a
* remote BGP speaker whose BGP Identifier equals the one in the
* OPEN message, then the local system performs the following
* collision resolution procedure:
*/
peer = new->doppelganger;
if (peer == NULL)
return 0;
other = peer->connection;
/*
* Do not accept the new connection in Established or Clearing
* states. Note that a peer GR is handled by closing the existing
* connection upon receipt of new one.
*/
if (peer_established(other) ||
other->status == Clearing) {
bgp_notify_send(connection, BGP_NOTIFY_CEASE,
BGP_NOTIFY_CEASE_COLLISION_RESOLUTION);
return -1;
}
if ((other->status != OpenConfirm) &&
(other->status != OpenSent))
return 0;
/*
* 1. The BGP Identifier of the local system is
* compared to the BGP Identifier of the remote
* system (as specified in the OPEN message).
*
* If the BGP Identifiers of the peers
* involved in the connection collision
* are identical, then the connection
* initiated by the BGP speaker with the
* larger AS number is preserved.
*/
if (ntohl(peer->local_id.s_addr) < ntohl(remote_id.s_addr)
|| (ntohl(peer->local_id.s_addr) == ntohl(remote_id.s_addr)
&& peer->local_as < peer->as))
if (!CHECK_FLAG(peer->sflags, PEER_STATUS_ACCEPT_PEER)) {
/*
* 2. If the value of the local BGP
* Identifier is less than the remote one,
* the local system closes BGP connection
* that already exists (the one that is
* already in the OpenConfirm state),
* and accepts BGP connection initiated by
* the remote system.
*/
bgp_notify_send(other, BGP_NOTIFY_CEASE,
BGP_NOTIFY_CEASE_COLLISION_RESOLUTION);
return 1;
} else {
bgp_notify_send(connection, BGP_NOTIFY_CEASE,
BGP_NOTIFY_CEASE_COLLISION_RESOLUTION);
return -1;
}
else {
if (ntohl(peer->local_id.s_addr) == ntohl(remote_id.s_addr)
&& peer->local_as == peer->as)
flog_err(EC_BGP_ROUTER_ID_SAME,
"Peer's router-id %pI4 is the same as ours",
&remote_id);
/*
* 3. Otherwise, the local system closes newly
* created BGP connection (the one associated with the
* newly received OPEN message), and continues to use
* the existing one (the one that is already in the
* OpenConfirm state).
*/
if (CHECK_FLAG(peer->sflags, PEER_STATUS_ACCEPT_PEER)) {
bgp_notify_send(other, BGP_NOTIFY_CEASE,
BGP_NOTIFY_CEASE_COLLISION_RESOLUTION);
return 1;
} else {
bgp_notify_send(connection, BGP_NOTIFY_CEASE,
BGP_NOTIFY_CEASE_COLLISION_RESOLUTION);
return -1;
}
}
}
/* Packet processing routines ---------------------------------------------- */
/*
* This is a family of functions designed to be called from
* bgp_process_packet(). These functions all share similar behavior and should
* adhere to the following invariants and restrictions:
*
* Return codes
* ------------
* The return code of any one of those functions should be one of the FSM event
* codes specified in bgpd.h. If a NOTIFY was sent, this event code MUST be
* BGP_Stop. Otherwise, the code SHOULD correspond to the function's expected
* packet type. For example, bgp_open_receive() should return BGP_Stop upon
* error and Receive_OPEN_message otherwise.
*
* If no action is necessary, the correct return code is BGP_PACKET_NOOP as
* defined below.
*
* Side effects
* ------------
* - May send NOTIFY messages
* - May not modify peer->connection->status
* - May not call bgp_event_update()
*/
#define BGP_PACKET_NOOP 0
/**
* Process BGP OPEN message for peer.
*
* If any errors are encountered in the OPEN message, immediately sends NOTIFY
* and returns BGP_Stop.
*
* @param peer
* @param size size of the packet
* @return as in summary
*/
static int bgp_open_receive(struct peer_connection *connection,
struct peer *peer, bgp_size_t size)
{
int ret;
uint8_t version;
uint16_t optlen;
uint16_t holdtime;
uint16_t send_holdtime;
as_t remote_as;
as_t as4 = 0, as4_be;
struct in_addr remote_id;
int mp_capability;
uint8_t notify_data_remote_as[2];
uint8_t notify_data_remote_as4[4];
uint8_t notify_data_remote_id[4];
uint16_t *holdtime_ptr;
/* Parse open packet. */
version = stream_getc(peer->curr);
memcpy(notify_data_remote_as, stream_pnt(peer->curr), 2);
remote_as = stream_getw(peer->curr);
holdtime_ptr = (uint16_t *)stream_pnt(peer->curr);
holdtime = stream_getw(peer->curr);
memcpy(notify_data_remote_id, stream_pnt(peer->curr), 4);
remote_id.s_addr = stream_get_ipv4(peer->curr);
/* BEGIN to read the capability here, but dont do it yet */
mp_capability = 0;
optlen = stream_getc(peer->curr);
/* Extended Optional Parameters Length for BGP OPEN Message */
if (optlen == BGP_OPEN_NON_EXT_OPT_LEN
|| CHECK_FLAG(peer->flags, PEER_FLAG_EXTENDED_OPT_PARAMS)) {
uint8_t opttype;
if (STREAM_READABLE(peer->curr) < 1) {
flog_err(
EC_BGP_PKT_OPEN,
"%s: stream does not have enough bytes for extended optional parameters",
peer->host);
bgp_notify_send(connection, BGP_NOTIFY_OPEN_ERR,
BGP_NOTIFY_OPEN_MALFORMED_ATTR);
return BGP_Stop;
}
opttype = stream_getc(peer->curr);
if (opttype == BGP_OPEN_NON_EXT_OPT_TYPE_EXTENDED_LENGTH) {
if (STREAM_READABLE(peer->curr) < 2) {
flog_err(
EC_BGP_PKT_OPEN,
"%s: stream does not have enough bytes to read the extended optional parameters optlen",
peer->host);
bgp_notify_send(connection, BGP_NOTIFY_OPEN_ERR,
BGP_NOTIFY_OPEN_MALFORMED_ATTR);
return BGP_Stop;
}
optlen = stream_getw(peer->curr);
SET_FLAG(peer->sflags,
PEER_STATUS_EXT_OPT_PARAMS_LENGTH);
}
}
/* Receive OPEN message log */
if (bgp_debug_neighbor_events(peer))
zlog_debug(
"%s rcv OPEN%s, version %d, remote-as (in open) %u, holdtime %d, id %pI4",
peer->host,
CHECK_FLAG(peer->sflags,
PEER_STATUS_EXT_OPT_PARAMS_LENGTH)
? " (Extended)"
: "",
version, remote_as, holdtime, &remote_id);
if (optlen != 0) {
/* If not enough bytes, it is an error. */
if (STREAM_READABLE(peer->curr) < optlen) {
flog_err(EC_BGP_PKT_OPEN,
"%s: stream has not enough bytes (%u)",
peer->host, optlen);
bgp_notify_send(connection, BGP_NOTIFY_OPEN_ERR,
BGP_NOTIFY_OPEN_MALFORMED_ATTR);
return BGP_Stop;
}
/* We need the as4 capability value *right now* because
* if it is there, we have not got the remote_as yet, and
* without
* that we do not know which peer is connecting to us now.
*/
as4 = peek_for_as4_capability(peer, optlen);
}
as4_be = htonl(as4);
memcpy(notify_data_remote_as4, &as4_be, 4);
/* Just in case we have a silly peer who sends AS4 capability set to 0
*/
if (CHECK_FLAG(peer->cap, PEER_CAP_AS4_RCV) && !as4) {
flog_err(EC_BGP_PKT_OPEN,
"%s bad OPEN, got AS4 capability, but AS4 set to 0",
peer->host);
bgp_notify_send_with_data(connection, BGP_NOTIFY_OPEN_ERR,
BGP_NOTIFY_OPEN_BAD_PEER_AS,
notify_data_remote_as4, 4);
return BGP_Stop;
}
/* Codification of AS 0 Processing */
if (remote_as == BGP_AS_ZERO) {
flog_err(EC_BGP_PKT_OPEN, "%s bad OPEN, got AS set to 0",
peer->host);
bgp_notify_send(connection, BGP_NOTIFY_OPEN_ERR,
BGP_NOTIFY_OPEN_BAD_PEER_AS);
return BGP_Stop;
}
if (remote_as == BGP_AS_TRANS) {
/* Take the AS4 from the capability. We must have received the
* capability now! Otherwise we have a asn16 peer who uses
* BGP_AS_TRANS, for some unknown reason.
*/
if (as4 == BGP_AS_TRANS) {
flog_err(
EC_BGP_PKT_OPEN,
"%s [AS4] NEW speaker using AS_TRANS for AS4, not allowed",
peer->host);
bgp_notify_send_with_data(connection,
BGP_NOTIFY_OPEN_ERR,
BGP_NOTIFY_OPEN_BAD_PEER_AS,
notify_data_remote_as4, 4);
return BGP_Stop;
}
if (!as4 && BGP_DEBUG(as4, AS4))
zlog_debug(
"%s [AS4] OPEN remote_as is AS_TRANS, but no AS4. Odd, but proceeding.",
peer->host);
else if (as4 < BGP_AS_MAX && BGP_DEBUG(as4, AS4))
zlog_debug(
"%s [AS4] OPEN remote_as is AS_TRANS, but AS4 (%u) fits in 2-bytes, very odd peer.",
peer->host, as4);
if (as4)
remote_as = as4;
} else {
/* We may have a partner with AS4 who has an asno < BGP_AS_MAX
*/
/* If we have got the capability, peer->as4cap must match
* remote_as */
if (CHECK_FLAG(peer->cap, PEER_CAP_AS4_RCV)
&& as4 != remote_as) {
/* raise error, log this, close session */
flog_err(
EC_BGP_PKT_OPEN,
"%s bad OPEN, got AS4 capability, but remote_as %u mismatch with 16bit 'myasn' %u in open",
peer->host, as4, remote_as);
bgp_notify_send_with_data(connection,
BGP_NOTIFY_OPEN_ERR,
BGP_NOTIFY_OPEN_BAD_PEER_AS,
notify_data_remote_as4, 4);
return BGP_Stop;
}
}
/* rfc6286:
* If the BGP Identifier field of the OPEN message
* is zero, or if it is the same as the BGP Identifier
* of the local BGP speaker and the message is from an
* internal peer, then the Error Subcode is set to
* "Bad BGP Identifier".
*/
if (remote_id.s_addr == INADDR_ANY
|| (peer->sort == BGP_PEER_IBGP
&& ntohl(peer->local_id.s_addr) == ntohl(remote_id.s_addr))) {
if (bgp_debug_neighbor_events(peer))
zlog_debug("%s bad OPEN, wrong router identifier %pI4",
peer->host, &remote_id);
bgp_notify_send_with_data(connection, BGP_NOTIFY_OPEN_ERR,
BGP_NOTIFY_OPEN_BAD_BGP_IDENT,
notify_data_remote_id, 4);
return BGP_Stop;
}
/* Peer BGP version check. */
if (version != BGP_VERSION_4) {
uint16_t maxver = htons(BGP_VERSION_4);
/* XXX this reply may not be correct if version < 4 XXX */
if (bgp_debug_neighbor_events(peer))
zlog_debug(
"%s bad protocol version, remote requested %d, local request %d",
peer->host, version, BGP_VERSION_4);
/* Data must be in network byte order here */
bgp_notify_send_with_data(connection, BGP_NOTIFY_OPEN_ERR,
BGP_NOTIFY_OPEN_UNSUP_VERSION,
(uint8_t *)&maxver, 2);
return BGP_Stop;
}
/* Check neighbor as number. */
if (peer->as_type == AS_UNSPECIFIED) {
if (bgp_debug_neighbor_events(peer))
zlog_debug(
"%s bad OPEN, remote AS is unspecified currently",
peer->host);
bgp_notify_send_with_data(connection, BGP_NOTIFY_OPEN_ERR,
BGP_NOTIFY_OPEN_BAD_PEER_AS,
notify_data_remote_as, 2);
return BGP_Stop;
} else if (peer->as_type == AS_INTERNAL) {
if (remote_as != peer->bgp->as) {
if (bgp_debug_neighbor_events(peer))
zlog_debug(
"%s bad OPEN, remote AS is %u, internal specified",
peer->host, remote_as);
bgp_notify_send_with_data(connection,
BGP_NOTIFY_OPEN_ERR,
BGP_NOTIFY_OPEN_BAD_PEER_AS,
notify_data_remote_as, 2);
return BGP_Stop;
}
peer->as = peer->local_as;
} else if (peer->as_type == AS_EXTERNAL) {
if (remote_as == peer->bgp->as) {
if (bgp_debug_neighbor_events(peer))
zlog_debug(
"%s bad OPEN, remote AS is %u, external specified",
peer->host, remote_as);
bgp_notify_send_with_data(connection,
BGP_NOTIFY_OPEN_ERR,
BGP_NOTIFY_OPEN_BAD_PEER_AS,
notify_data_remote_as, 2);
return BGP_Stop;
}
peer->as = remote_as;
} else if ((peer->as_type == AS_SPECIFIED) && (remote_as != peer->as)) {
if (bgp_debug_neighbor_events(peer))
zlog_debug("%s bad OPEN, remote AS is %u, expected %u",
peer->host, remote_as, peer->as);
bgp_notify_send_with_data(connection, BGP_NOTIFY_OPEN_ERR,
BGP_NOTIFY_OPEN_BAD_PEER_AS,
notify_data_remote_as, 2);
return BGP_Stop;
}
/*
* When collision is detected and this peer is closed.
* Return immediately.
*/
ret = bgp_collision_detect(connection, peer, remote_id);
if (ret < 0)
return BGP_Stop;
/* Get sockname. */
if (bgp_getsockname(peer) < 0) {
flog_err_sys(EC_LIB_SOCKET,
"%s: bgp_getsockname() failed for peer: %s",
__func__, peer->host);
return BGP_Stop;
}
/* Set remote router-id */
peer->remote_id = remote_id;
/* From the rfc: Upon receipt of an OPEN message, a BGP speaker MUST
calculate the value of the Hold Timer by using the smaller of its
configured Hold Time and the Hold Time received in the OPEN message.
The Hold Time MUST be either zero or at least three seconds. An
implementation may reject connections on the basis of the Hold Time.
*/
if (holdtime < 3 && holdtime != 0) {
bgp_notify_send_with_data(connection, BGP_NOTIFY_OPEN_ERR,
BGP_NOTIFY_OPEN_UNACEP_HOLDTIME,
(uint8_t *)holdtime_ptr, 2);
return BGP_Stop;
}
/* Send notification message when Hold Time received in the OPEN message
* is smaller than configured minimum Hold Time. */
if (holdtime < peer->bgp->default_min_holdtime
&& peer->bgp->default_min_holdtime != 0) {
bgp_notify_send_with_data(connection, BGP_NOTIFY_OPEN_ERR,
BGP_NOTIFY_OPEN_UNACEP_HOLDTIME,
(uint8_t *)holdtime_ptr, 2);
return BGP_Stop;
}
/* From the rfc: A reasonable maximum time between KEEPALIVE messages
would be one third of the Hold Time interval. KEEPALIVE messages
MUST NOT be sent more frequently than one per second. An
implementation MAY adjust the rate at which it sends KEEPALIVE
messages as a function of the Hold Time interval. */
if (CHECK_FLAG(peer->flags, PEER_FLAG_TIMER))
send_holdtime = peer->holdtime;
else
send_holdtime = peer->bgp->default_holdtime;
if (holdtime < send_holdtime)
peer->v_holdtime = holdtime;
else
peer->v_holdtime = send_holdtime;
/* Set effective keepalive to 1/3 the effective holdtime.
* Use configured keeplive when < effective keepalive.
*/
peer->v_keepalive = peer->v_holdtime / 3;
if (CHECK_FLAG(peer->flags, PEER_FLAG_TIMER)) {
if (peer->keepalive && peer->keepalive < peer->v_keepalive)
peer->v_keepalive = peer->keepalive;
} else {
if (peer->bgp->default_keepalive
&& peer->bgp->default_keepalive < peer->v_keepalive)
peer->v_keepalive = peer->bgp->default_keepalive;
}
/* If another side disabled sending Software Version capability,
* we MUST drop the previous from showing in the outputs to avoid
* stale information and due to security reasons.
*/
if (peer->soft_version)
XFREE(MTYPE_BGP_SOFT_VERSION, peer->soft_version);
/* Open option part parse. */
if (optlen != 0) {
if (bgp_open_option_parse(peer, optlen, &mp_capability) < 0)
return BGP_Stop;
} else {
if (bgp_debug_neighbor_events(peer))
zlog_debug("%s rcvd OPEN w/ OPTION parameter len: 0",
peer->host);
}
/*
* Assume that the peer supports the locally configured set of
* AFI/SAFIs if the peer did not send us any Mulitiprotocol
* capabilities, or if 'override-capability' is configured.
*/
if (!mp_capability
|| CHECK_FLAG(peer->flags, PEER_FLAG_OVERRIDE_CAPABILITY)) {
peer->afc_nego[AFI_IP][SAFI_UNICAST] =
peer->afc[AFI_IP][SAFI_UNICAST];
peer->afc_nego[AFI_IP][SAFI_MULTICAST] =
peer->afc[AFI_IP][SAFI_MULTICAST];
peer->afc_nego[AFI_IP][SAFI_LABELED_UNICAST] =
peer->afc[AFI_IP][SAFI_LABELED_UNICAST];
peer->afc_nego[AFI_IP][SAFI_FLOWSPEC] =
peer->afc[AFI_IP][SAFI_FLOWSPEC];
peer->afc_nego[AFI_IP6][SAFI_UNICAST] =
peer->afc[AFI_IP6][SAFI_UNICAST];
peer->afc_nego[AFI_IP6][SAFI_MULTICAST] =
peer->afc[AFI_IP6][SAFI_MULTICAST];
peer->afc_nego[AFI_IP6][SAFI_LABELED_UNICAST] =
peer->afc[AFI_IP6][SAFI_LABELED_UNICAST];
peer->afc_nego[AFI_L2VPN][SAFI_EVPN] =
peer->afc[AFI_L2VPN][SAFI_EVPN];
peer->afc_nego[AFI_IP6][SAFI_FLOWSPEC] =
peer->afc[AFI_IP6][SAFI_FLOWSPEC];
peer->afc_nego[AFI_LINKSTATE][SAFI_LINKSTATE] =
peer->afc[AFI_LINKSTATE][SAFI_LINKSTATE];
}
/* Verify valid local address present based on negotiated
* address-families. */
if (peer->afc_nego[AFI_IP][SAFI_UNICAST]
|| peer->afc_nego[AFI_IP][SAFI_LABELED_UNICAST]
|| peer->afc_nego[AFI_IP][SAFI_MULTICAST]
|| peer->afc_nego[AFI_IP][SAFI_MPLS_VPN]
|| peer->afc_nego[AFI_IP][SAFI_ENCAP]) {
if (peer->nexthop.v4.s_addr == INADDR_ANY) {
#if defined(HAVE_CUMULUS)
zlog_warn("%s: No local IPv4 addr, BGP routing may not work",
peer->host);
#endif
}
}
if (peer->afc_nego[AFI_IP6][SAFI_UNICAST]
|| peer->afc_nego[AFI_IP6][SAFI_LABELED_UNICAST]
|| peer->afc_nego[AFI_IP6][SAFI_MULTICAST]
|| peer->afc_nego[AFI_IP6][SAFI_MPLS_VPN]
|| peer->afc_nego[AFI_IP6][SAFI_ENCAP]) {
if (IN6_IS_ADDR_UNSPECIFIED(&peer->nexthop.v6_global) &&
!bm->v6_with_v4_nexthops) {
flog_err(EC_BGP_SND_FAIL,
"%s: No local IPv6 address, and zebra does not support V6 routing with v4 nexthops, BGP routing for V6 will not work",
peer->host);
bgp_notify_send(connection, BGP_NOTIFY_CEASE,
BGP_NOTIFY_SUBCODE_UNSPECIFIC);
return BGP_Stop;
}
}
peer->rtt = sockopt_tcp_rtt(connection->fd);
return Receive_OPEN_message;
}
/**
* Process BGP KEEPALIVE message for peer.
*
* @param peer
* @param size size of the packet
* @return as in summary
*/
static int bgp_keepalive_receive(struct peer_connection *connection,
struct peer *peer, bgp_size_t size)
{
if (bgp_debug_keepalive(peer))
zlog_debug("%s KEEPALIVE rcvd", peer->host);
bgp_update_implicit_eors(peer);
peer->rtt = sockopt_tcp_rtt(connection->fd);
/* If the peer's RTT is higher than expected, shutdown
* the peer automatically.
*/
if (!CHECK_FLAG(peer->flags, PEER_FLAG_RTT_SHUTDOWN))
return Receive_KEEPALIVE_message;
if (peer->rtt > peer->rtt_expected) {
peer->rtt_keepalive_rcv++;
if (peer->rtt_keepalive_rcv > peer->rtt_keepalive_conf) {
char rtt_shutdown_reason[BUFSIZ] = {};
snprintfrr(
rtt_shutdown_reason,
sizeof(rtt_shutdown_reason),
"shutdown due to high round-trip-time (%dms > %dms, hit %u times)",
peer->rtt, peer->rtt_expected,
peer->rtt_keepalive_rcv);
zlog_warn("%s %s", peer->host, rtt_shutdown_reason);
SET_FLAG(peer->sflags, PEER_STATUS_RTT_SHUTDOWN);
peer_tx_shutdown_message_set(peer, rtt_shutdown_reason);
peer_flag_set(peer, PEER_FLAG_SHUTDOWN);
}
} else {
if (peer->rtt_keepalive_rcv)
peer->rtt_keepalive_rcv--;
}
return Receive_KEEPALIVE_message;
}
static void bgp_refresh_stalepath_timer_expire(struct event *thread)
{
struct peer_af *paf;
paf = EVENT_ARG(thread);
afi_t afi = paf->afi;
safi_t safi = paf->safi;
struct peer *peer = paf->peer;
peer->t_refresh_stalepath = NULL;
if (peer->nsf[afi][safi])
bgp_clear_stale_route(peer, afi, safi);
if (bgp_debug_neighbor_events(peer))
zlog_debug(
"%pBP route-refresh (BoRR) timer expired for afi/safi: %d/%d",
peer, afi, safi);
bgp_timer_set(peer->connection);
}
/**
* Process BGP UPDATE message for peer.
*
* Parses UPDATE and creates attribute object.
*
* @param peer
* @param size size of the packet
* @return as in summary
*/
static int bgp_update_receive(struct peer_connection *connection,
struct peer *peer, bgp_size_t size)
{
int ret, nlri_ret;
uint8_t *end;
struct stream *s;
struct attr attr;
bgp_size_t attribute_len;
bgp_size_t update_len;
bgp_size_t withdraw_len;
bool restart = false;
enum NLRI_TYPES {
NLRI_UPDATE,
NLRI_WITHDRAW,
NLRI_MP_UPDATE,
NLRI_MP_WITHDRAW,
NLRI_TYPE_MAX
};
struct bgp_nlri nlris[NLRI_TYPE_MAX];
/* Status must be Established. */
if (!peer_established(connection)) {
flog_err(EC_BGP_INVALID_STATUS,
"%s [FSM] Update packet received under status %s",
peer->host,
lookup_msg(bgp_status_msg, peer->connection->status,
NULL));
bgp_notify_send(connection, BGP_NOTIFY_FSM_ERR,
bgp_fsm_error_subcode(peer->connection->status));
return BGP_Stop;
}
/* Set initial values. */
memset(&attr, 0, sizeof(attr));
attr.label_index = BGP_INVALID_LABEL_INDEX;
attr.label = MPLS_INVALID_LABEL;
memset(&nlris, 0, sizeof(nlris));
memset(peer->rcvd_attr_str, 0, BUFSIZ);
peer->rcvd_attr_printed = 0;
s = peer->curr;
end = stream_pnt(s) + size;
/* RFC1771 6.3 If the Unfeasible Routes Length or Total Attribute
Length is too large (i.e., if Unfeasible Routes Length + Total
Attribute Length + 23 exceeds the message Length), then the Error
Subcode is set to Malformed Attribute List. */
if (stream_pnt(s) + 2 > end) {
flog_err(EC_BGP_UPDATE_RCV,
"%s [Error] Update packet error (packet length is short for unfeasible length)",
peer->host);
bgp_notify_send(connection, BGP_NOTIFY_UPDATE_ERR,
BGP_NOTIFY_UPDATE_MAL_ATTR);
return BGP_Stop;
}
/* Unfeasible Route Length. */
withdraw_len = stream_getw(s);
/* Unfeasible Route Length check. */
if (stream_pnt(s) + withdraw_len > end) {
flog_err(EC_BGP_UPDATE_RCV,
"%s [Error] Update packet error (packet unfeasible length overflow %d)",
peer->host, withdraw_len);
bgp_notify_send(connection, BGP_NOTIFY_UPDATE_ERR,
BGP_NOTIFY_UPDATE_MAL_ATTR);
return BGP_Stop;
}
/* Unfeasible Route packet format check. */
if (withdraw_len > 0) {
nlris[NLRI_WITHDRAW].afi = AFI_IP;
nlris[NLRI_WITHDRAW].safi = SAFI_UNICAST;
nlris[NLRI_WITHDRAW].nlri = stream_pnt(s);
nlris[NLRI_WITHDRAW].length = withdraw_len;
stream_forward_getp(s, withdraw_len);
}
/* Attribute total length check. */
if (stream_pnt(s) + 2 > end) {
flog_warn(
EC_BGP_UPDATE_PACKET_SHORT,
"%s [Error] Packet Error (update packet is short for attribute length)",
peer->host);
bgp_notify_send(peer->connection, BGP_NOTIFY_UPDATE_ERR,
BGP_NOTIFY_UPDATE_MAL_ATTR);
return BGP_Stop;
}
/* Fetch attribute total length. */
attribute_len = stream_getw(s);
/* Attribute length check. */
if (stream_pnt(s) + attribute_len > end) {
flog_warn(
EC_BGP_UPDATE_PACKET_LONG,
"%s [Error] Packet Error (update packet attribute length overflow %d)",
peer->host, attribute_len);
bgp_notify_send(connection, BGP_NOTIFY_UPDATE_ERR,
BGP_NOTIFY_UPDATE_MAL_ATTR);
return BGP_Stop;
}
/* Certain attribute parsing errors should not be considered bad enough
* to reset the session for, most particularly any partial/optional
* attributes that have 'tunneled' over speakers that don't understand
* them. Instead we withdraw only the prefix concerned.
*
* Complicates the flow a little though..
*/
enum bgp_attr_parse_ret attr_parse_ret = BGP_ATTR_PARSE_PROCEED;
/* This define morphs the update case into a withdraw when lower levels
* have signalled an error condition where this is best.
*/
#define NLRI_ATTR_ARG (attr_parse_ret != BGP_ATTR_PARSE_WITHDRAW ? &attr : NULL)
/* Parse attribute when it exists. */
if (attribute_len) {
attr_parse_ret = bgp_attr_parse(peer, &attr, attribute_len,
&nlris[NLRI_MP_UPDATE],
&nlris[NLRI_MP_WITHDRAW]);
if (attr_parse_ret == BGP_ATTR_PARSE_ERROR) {
bgp_attr_unintern_sub(&attr);
return BGP_Stop;
}
}
/* Logging the attribute. */
if (attr_parse_ret == BGP_ATTR_PARSE_WITHDRAW
|| BGP_DEBUG(update, UPDATE_IN)
|| BGP_DEBUG(update, UPDATE_PREFIX)) {
ret = bgp_dump_attr(&attr, peer->rcvd_attr_str,
sizeof(peer->rcvd_attr_str));
peer->stat_upd_7606++;
if (attr_parse_ret == BGP_ATTR_PARSE_WITHDRAW)
flog_err(
EC_BGP_UPDATE_RCV,
"%pBP rcvd UPDATE with errors in attr(s)!! Withdrawing route.",
peer);
if (ret && bgp_debug_update(peer, NULL, NULL, 1)) {
zlog_debug("%pBP rcvd UPDATE w/ attr: %s", peer,
peer->rcvd_attr_str);
peer->rcvd_attr_printed = 1;
}
}
/* Network Layer Reachability Information. */
update_len = end - stream_pnt(s);
if (update_len && attribute_len) {
/* Set NLRI portion to structure. */
nlris[NLRI_UPDATE].afi = AFI_IP;
nlris[NLRI_UPDATE].safi = SAFI_UNICAST;
nlris[NLRI_UPDATE].nlri = stream_pnt(s);
nlris[NLRI_UPDATE].length = update_len;
stream_forward_getp(s, update_len);
if (CHECK_FLAG(attr.flag, ATTR_FLAG_BIT(BGP_ATTR_MP_REACH_NLRI))) {
/*
* We skipped nexthop attribute validation earlier so
* validate the nexthop now.
*/
if (bgp_attr_nexthop_valid(peer, &attr) < 0) {
bgp_attr_unintern_sub(&attr);
return BGP_Stop;
}
}
}
if (BGP_DEBUG(update, UPDATE_IN))
zlog_debug("%pBP rcvd UPDATE wlen %d attrlen %d alen %d", peer,
withdraw_len, attribute_len, update_len);
/* Parse any given NLRIs */
for (int i = NLRI_UPDATE; i < NLRI_TYPE_MAX; i++) {
if (!nlris[i].nlri)
continue;
/* NLRI is processed iff the peer if configured for the specific
* afi/safi */
if (!peer->afc[nlris[i].afi][nlris[i].safi]) {
zlog_info(
"%s [Info] UPDATE for non-enabled AFI/SAFI %u/%u",
peer->host, nlris[i].afi, nlris[i].safi);
continue;
}
/* EoR handled later */
if (nlris[i].length == 0)
continue;
switch (i) {
case NLRI_UPDATE:
case NLRI_MP_UPDATE:
nlri_ret = bgp_nlri_parse(peer, NLRI_ATTR_ARG,
&nlris[i], 0);
break;
case NLRI_WITHDRAW:
case NLRI_MP_WITHDRAW:
nlri_ret = bgp_nlri_parse(peer, NLRI_ATTR_ARG,
&nlris[i], 1);
break;
default:
nlri_ret = BGP_NLRI_PARSE_ERROR;
}
if (nlri_ret < BGP_NLRI_PARSE_OK
&& nlri_ret != BGP_NLRI_PARSE_ERROR_PREFIX_OVERFLOW) {
flog_err(EC_BGP_UPDATE_RCV,
"%s [Error] Error parsing NLRI", peer->host);
if (peer_established(connection))
bgp_notify_send(connection,
BGP_NOTIFY_UPDATE_ERR,
i <= NLRI_WITHDRAW
? BGP_NOTIFY_UPDATE_INVAL_NETWORK
: BGP_NOTIFY_UPDATE_OPT_ATTR_ERR);
bgp_attr_unintern_sub(&attr);
return BGP_Stop;
}
}
/* EoR checks
*
* Non-MP IPv4/Unicast EoR is a completely empty UPDATE
* and MP EoR should have only an empty MP_UNREACH
*/
if ((!update_len && !withdraw_len && nlris[NLRI_MP_UPDATE].length == 0)
|| (attr_parse_ret == BGP_ATTR_PARSE_EOR)) {
afi_t afi = 0;
safi_t safi;
struct graceful_restart_info *gr_info;
/* Restarting router */
if (BGP_PEER_GRACEFUL_RESTART_CAPABLE(peer)
&& BGP_PEER_RESTARTING_MODE(peer))
restart = true;
/* Non-MP IPv4/Unicast is a completely emtpy UPDATE - already
* checked
* update and withdraw NLRI lengths are 0.
*/
if (!attribute_len) {
afi = AFI_IP;
safi = SAFI_UNICAST;
} else if (attr.flag & ATTR_FLAG_BIT(BGP_ATTR_MP_UNREACH_NLRI)
&& nlris[NLRI_MP_WITHDRAW].length == 0) {
afi = nlris[NLRI_MP_WITHDRAW].afi;
safi = nlris[NLRI_MP_WITHDRAW].safi;
} else if (attr_parse_ret == BGP_ATTR_PARSE_EOR) {
afi = nlris[NLRI_MP_UPDATE].afi;
safi = nlris[NLRI_MP_UPDATE].safi;
}
if (afi && peer->afc[afi][safi]) {
struct vrf *vrf = vrf_lookup_by_id(peer->bgp->vrf_id);
/* End-of-RIB received */
if (!CHECK_FLAG(peer->af_sflags[afi][safi],
PEER_STATUS_EOR_RECEIVED)) {
SET_FLAG(peer->af_sflags[afi][safi],
PEER_STATUS_EOR_RECEIVED);
bgp_update_explicit_eors(peer);
/* Update graceful restart information */
gr_info = &(peer->bgp->gr_info[afi][safi]);
if (restart)
gr_info->eor_received++;
/* If EOR received from all peers and selection
* deferral timer is running, cancel the timer
* and invoke the best path calculation
*/
if (gr_info->eor_required
== gr_info->eor_received) {
if (bgp_debug_neighbor_events(peer))
zlog_debug(
"%s %d, %s %d",
"EOR REQ",
gr_info->eor_required,
"EOR RCV",
gr_info->eor_received);
if (gr_info->t_select_deferral) {
void *info = EVENT_ARG(
gr_info->t_select_deferral);
XFREE(MTYPE_TMP, info);
}
EVENT_OFF(gr_info->t_select_deferral);
gr_info->eor_required = 0;
gr_info->eor_received = 0;
/* Best path selection */
bgp_best_path_select_defer(peer->bgp,
afi, safi);
}
}
/* NSF delete stale route */
if (peer->nsf[afi][safi])
bgp_clear_stale_route(peer, afi, safi);
zlog_info(
"%s: rcvd End-of-RIB for %s from %s in vrf %s",
__func__, get_afi_safi_str(afi, safi, false),
peer->host, vrf ? vrf->name : VRF_DEFAULT_NAME);
}
}
/* Everything is done. We unintern temporary structures which
interned in bgp_attr_parse(). */
bgp_attr_unintern_sub(&attr);
peer->update_time = monotime(NULL);
/* Notify BGP Conditional advertisement scanner process */
peer->advmap_table_change = true;
return Receive_UPDATE_message;
}
/**
* Process BGP NOTIFY message for peer.
*
* @param peer
* @param size size of the packet
* @return as in summary
*/
static int bgp_notify_receive(struct peer_connection *connection,
struct peer *peer, bgp_size_t size)
{
struct bgp_notify outer = {};
struct bgp_notify inner = {};
bool hard_reset = false;
if (peer->notify.data) {
XFREE(MTYPE_BGP_NOTIFICATION, peer->notify.data);
peer->notify.length = 0;
peer->notify.hard_reset = false;
}
outer.code = stream_getc(peer->curr);
outer.subcode = stream_getc(peer->curr);
outer.length = size - 2;
outer.data = NULL;
outer.raw_data = NULL;
if (outer.length) {
outer.raw_data = XMALLOC(MTYPE_BGP_NOTIFICATION, outer.length);
memcpy(outer.raw_data, stream_pnt(peer->curr), outer.length);
}
hard_reset =
bgp_notify_received_hard_reset(peer, outer.code, outer.subcode);
if (hard_reset && outer.length) {
inner = bgp_notify_decapsulate_hard_reset(&outer);
peer->notify.hard_reset = true;
} else {
inner = outer;
}
/* Preserv notify code and sub code. */
peer->notify.code = inner.code;
peer->notify.subcode = inner.subcode;
/* For further diagnostic record returned Data. */
if (inner.length) {
peer->notify.length = inner.length;
peer->notify.data =
XMALLOC(MTYPE_BGP_NOTIFICATION, inner.length);
memcpy(peer->notify.data, inner.raw_data, inner.length);
}
/* For debug */
{
int i;
int first = 0;
char c[4];
if (inner.length) {
inner.data = XMALLOC(MTYPE_BGP_NOTIFICATION,
inner.length * 3);
for (i = 0; i < inner.length; i++)
if (first) {
snprintf(c, sizeof(c), " %02x",
stream_getc(peer->curr));
strlcat(inner.data, c,
inner.length * 3);
} else {
first = 1;
snprintf(c, sizeof(c), "%02x",
stream_getc(peer->curr));
strlcpy(inner.data, c,
inner.length * 3);
}
}
bgp_notify_print(peer, &inner, "received", hard_reset);
if (inner.length) {
XFREE(MTYPE_BGP_NOTIFICATION, inner.data);
inner.length = 0;
}
if (outer.length) {
XFREE(MTYPE_BGP_NOTIFICATION, outer.data);
XFREE(MTYPE_BGP_NOTIFICATION, outer.raw_data);
/* If this is a Hard Reset notification, we MUST free
* the inner (encapsulated) notification too.
*/
if (hard_reset)
XFREE(MTYPE_BGP_NOTIFICATION, inner.raw_data);
outer.length = 0;
}
}
/* peer count update */
atomic_fetch_add_explicit(&peer->notify_in, 1, memory_order_relaxed);
peer->last_reset = PEER_DOWN_NOTIFY_RECEIVED;
/* We have to check for Notify with Unsupported Optional Parameter.
in that case we fallback to open without the capability option.
But this done in bgp_stop. We just mark it here to avoid changing
the fsm tables. */
if (inner.code == BGP_NOTIFY_OPEN_ERR &&
inner.subcode == BGP_NOTIFY_OPEN_UNSUP_PARAM)
UNSET_FLAG(peer->sflags, PEER_STATUS_CAPABILITY_OPEN);
/* If Graceful-Restart N-bit (Notification) is exchanged,
* and it's not a Hard Reset, let's retain the routes.
*/
if (bgp_has_graceful_restart_notification(peer) && !hard_reset &&
CHECK_FLAG(peer->sflags, PEER_STATUS_NSF_MODE))
SET_FLAG(peer->sflags, PEER_STATUS_NSF_WAIT);
bgp_peer_gr_flags_update(peer);
BGP_GR_ROUTER_DETECT_AND_SEND_CAPABILITY_TO_ZEBRA(peer->bgp,
peer->bgp->peer);
return Receive_NOTIFICATION_message;
}
/**
* Process BGP ROUTEREFRESH message for peer.
*
* @param peer
* @param size size of the packet
* @return as in summary
*/
static int bgp_route_refresh_receive(struct peer_connection *connection,
struct peer *peer, bgp_size_t size)
{
iana_afi_t pkt_afi;
afi_t afi;
iana_safi_t pkt_safi;
safi_t safi;
struct stream *s;
struct peer_af *paf;
struct update_group *updgrp;
struct peer *updgrp_peer;
uint8_t subtype;
bool force_update = false;
bgp_size_t msg_length =
size - (BGP_MSG_ROUTE_REFRESH_MIN_SIZE - BGP_HEADER_SIZE);
/* If peer does not have the capability, send notification. */
if (!CHECK_FLAG(peer->cap, PEER_CAP_REFRESH_ADV)) {
flog_err(EC_BGP_NO_CAP,
"%s [Error] BGP route refresh is not enabled",
peer->host);
bgp_notify_send(connection, BGP_NOTIFY_HEADER_ERR,
BGP_NOTIFY_HEADER_BAD_MESTYPE);
return BGP_Stop;
}
/* Status must be Established. */
if (!peer_established(connection)) {
flog_err(EC_BGP_INVALID_STATUS,
"%s [Error] Route refresh packet received under status %s",
peer->host,
lookup_msg(bgp_status_msg, peer->connection->status,
NULL));
bgp_notify_send(connection, BGP_NOTIFY_FSM_ERR,
bgp_fsm_error_subcode(peer->connection->status));
return BGP_Stop;
}
s = peer->curr;
/* Parse packet. */
pkt_afi = stream_getw(s);
subtype = stream_getc(s);
pkt_safi = stream_getc(s);
/* Convert AFI, SAFI to internal values and check. */
if (bgp_map_afi_safi_iana2int(pkt_afi, pkt_safi, &afi, &safi)) {
zlog_info(
"%s REFRESH_REQ for unrecognized afi/safi: %s/%s - ignored",
peer->host, iana_afi2str(pkt_afi),
iana_safi2str(pkt_safi));
return BGP_PACKET_NOOP;
}
if (size != BGP_MSG_ROUTE_REFRESH_MIN_SIZE - BGP_HEADER_SIZE) {
uint8_t *end;
uint8_t when_to_refresh;
uint8_t orf_type;
uint16_t orf_len;
if (subtype) {
/* If the length, excluding the fixed-size message
* header, of the received ROUTE-REFRESH message with
* Message Subtype 1 and 2 is not 4, then the BGP
* speaker MUST send a NOTIFICATION message with the
* Error Code of "ROUTE-REFRESH Message Error" and the
* subcode of "Invalid Message Length".
*/
if (msg_length != 4) {
zlog_err(
"%s Enhanced Route Refresh message length error",
peer->host);
bgp_notify_send(connection,
BGP_NOTIFY_ROUTE_REFRESH_ERR,
BGP_NOTIFY_ROUTE_REFRESH_INVALID_MSG_LEN);
}
/* When the BGP speaker receives a ROUTE-REFRESH message
* with a "Message Subtype" field other than 0, 1, or 2,
* it MUST ignore the received ROUTE-REFRESH message.
*/
if (subtype > 2)
zlog_err(
"%s Enhanced Route Refresh invalid subtype",
peer->host);
}
if (msg_length < 5) {
zlog_info("%s ORF route refresh length error",
peer->host);
bgp_notify_send(connection, BGP_NOTIFY_CEASE,
BGP_NOTIFY_SUBCODE_UNSPECIFIC);
return BGP_Stop;
}
when_to_refresh = stream_getc(s);
end = stream_pnt(s) + (size - 5);
while ((stream_pnt(s) + 2) < end) {
orf_type = stream_getc(s);
orf_len = stream_getw(s);
/* orf_len in bounds? */
if ((stream_pnt(s) + orf_len) > end)
break; /* XXX: Notify instead?? */
if (orf_type == ORF_TYPE_PREFIX) {
uint8_t *p_pnt = stream_pnt(s);
uint8_t *p_end = stream_pnt(s) + orf_len;
struct orf_prefix orfp;
uint8_t common = 0;
uint32_t seq;
int psize;
char name[BUFSIZ];
int ret = CMD_SUCCESS;
if (bgp_debug_neighbor_events(peer)) {
zlog_debug(
"%pBP rcvd Prefixlist ORF(%d) length %d",
peer, orf_type, orf_len);
}
/* ORF prefix-list name */
snprintf(name, sizeof(name), "%s.%d.%d",
peer->host, afi, safi);
/* we're going to read at least 1 byte of common
* ORF header,
* and 7 bytes of ORF Address-filter entry from
* the stream
*/
if (p_pnt < p_end &&
*p_pnt & ORF_COMMON_PART_REMOVE_ALL) {
if (bgp_debug_neighbor_events(peer))
zlog_debug(
"%pBP rcvd Remove-All pfxlist ORF request",
peer);
prefix_bgp_orf_remove_all(afi, name);
break;
}
if (orf_len < 7)
break;
while (p_pnt < p_end) {
/* If the ORF entry is malformed, want
* to read as much of it
* as possible without going beyond the
* bounds of the entry,
* to maximise debug information.
*/
int ok;
memset(&orfp, 0, sizeof(orfp));
common = *p_pnt++;
/* after ++: p_pnt <= p_end */
ok = ((uint32_t)(p_end - p_pnt)
>= sizeof(uint32_t));
if (ok) {
memcpy(&seq, p_pnt,
sizeof(uint32_t));
p_pnt += sizeof(uint32_t);
orfp.seq = ntohl(seq);
} else
p_pnt = p_end;
/* val checked in prefix_bgp_orf_set */
if (p_pnt < p_end)
orfp.ge = *p_pnt++;
/* val checked in prefix_bgp_orf_set */
if (p_pnt < p_end)
orfp.le = *p_pnt++;
if ((ok = (p_pnt < p_end)))
orfp.p.prefixlen = *p_pnt++;
/* afi checked already */
orfp.p.family = afi2family(afi);
/* 0 if not ok */
psize = PSIZE(orfp.p.prefixlen);
/* valid for family ? */
if (psize > prefix_blen(&orfp.p)) {
ok = 0;
psize = prefix_blen(&orfp.p);
}
/* valid for packet ? */
if (psize > (p_end - p_pnt)) {
ok = 0;
psize = p_end - p_pnt;
}
if (psize > 0)
memcpy(&orfp.p.u.prefix, p_pnt,
psize);
p_pnt += psize;
if (bgp_debug_neighbor_events(peer)) {
char buf[INET6_BUFSIZ];
zlog_debug(
"%pBP rcvd %s %s seq %u %s/%d ge %d le %d%s",
peer,
(common & ORF_COMMON_PART_REMOVE
? "Remove"
: "Add"),
(common & ORF_COMMON_PART_DENY
? "deny"
: "permit"),
orfp.seq,
inet_ntop(
orfp.p.family,
&orfp.p.u.prefix,
buf,
INET6_BUFSIZ),
orfp.p.prefixlen,
orfp.ge, orfp.le,
ok ? "" : " MALFORMED");
}
if (ok)
ret = prefix_bgp_orf_set(
name, afi, &orfp,
(common & ORF_COMMON_PART_DENY
? 0
: 1),
(common & ORF_COMMON_PART_REMOVE
? 0
: 1));
if (!ok || (ok && ret != CMD_SUCCESS)) {
zlog_info(
"%pBP Received misformatted prefixlist ORF. Remove All pfxlist",
peer);
prefix_bgp_orf_remove_all(afi,
name);
break;
}
}
peer->orf_plist[afi][safi] =
prefix_bgp_orf_lookup(afi, name);
}
stream_forward_getp(s, orf_len);
}
if (bgp_debug_neighbor_events(peer))
zlog_debug("%pBP rcvd Refresh %s ORF request", peer,
when_to_refresh == REFRESH_DEFER
? "Defer"
: "Immediate");
if (when_to_refresh == REFRESH_DEFER)
return BGP_PACKET_NOOP;
}
/* First update is deferred until ORF or ROUTE-REFRESH is received */
if (CHECK_FLAG(peer->af_sflags[afi][safi],
PEER_STATUS_ORF_WAIT_REFRESH))
UNSET_FLAG(peer->af_sflags[afi][safi],
PEER_STATUS_ORF_WAIT_REFRESH);
paf = peer_af_find(peer, afi, safi);
if (paf && paf->subgroup) {
if (peer->orf_plist[afi][safi]) {
updgrp = PAF_UPDGRP(paf);
updgrp_peer = UPDGRP_PEER(updgrp);
updgrp_peer->orf_plist[afi][safi] =
peer->orf_plist[afi][safi];
}
/* Avoid supressing duplicate routes later
* when processing in subgroup_announce_table().
*/
force_update = true;
/* If the peer is configured for default-originate clear the
* SUBGRP_STATUS_DEFAULT_ORIGINATE flag so that we will
* re-advertise the
* default
*/
if (CHECK_FLAG(paf->subgroup->sflags,
SUBGRP_STATUS_DEFAULT_ORIGINATE))
UNSET_FLAG(paf->subgroup->sflags,
SUBGRP_STATUS_DEFAULT_ORIGINATE);
}
if (subtype == BGP_ROUTE_REFRESH_BORR) {
/* A BGP speaker that has received the Graceful Restart
* Capability from its neighbor MUST ignore any BoRRs for
* an <AFI, SAFI> from the neighbor before the speaker
* receives the EoR for the given <AFI, SAFI> from the
* neighbor.
*/
if (CHECK_FLAG(peer->cap, PEER_CAP_RESTART_RCV)
&& !CHECK_FLAG(peer->af_sflags[afi][safi],
PEER_STATUS_EOR_RECEIVED)) {
if (bgp_debug_neighbor_events(peer))
zlog_debug(
"%pBP rcvd route-refresh (BoRR) for %s/%s before EoR",
peer, afi2str(afi), safi2str(safi));
return BGP_PACKET_NOOP;
}
if (peer->t_refresh_stalepath) {
if (bgp_debug_neighbor_events(peer))
zlog_debug(
"%pBP rcvd route-refresh (BoRR) for %s/%s, whereas BoRR already received",
peer, afi2str(afi), safi2str(safi));
return BGP_PACKET_NOOP;
}
SET_FLAG(peer->af_sflags[afi][safi], PEER_STATUS_BORR_RECEIVED);
UNSET_FLAG(peer->af_sflags[afi][safi],
PEER_STATUS_EORR_RECEIVED);
/* When a BGP speaker receives a BoRR message from
* a peer, it MUST mark all the routes with the given
* Address Family Identifier and Subsequent Address
* Family Identifier, <AFI, SAFI> [RFC2918], from
* that peer as stale.
*/
if (peer_active_nego(peer)) {
SET_FLAG(peer->af_sflags[afi][safi],
PEER_STATUS_ENHANCED_REFRESH);
bgp_set_stale_route(peer, afi, safi);
}
if (peer_established(peer->connection))
event_add_timer(bm->master,
bgp_refresh_stalepath_timer_expire, paf,
peer->bgp->stalepath_time,
&peer->t_refresh_stalepath);
if (bgp_debug_neighbor_events(peer))
zlog_debug(
"%pBP rcvd route-refresh (BoRR) for %s/%s, triggering timer for %u seconds",
peer, afi2str(afi), safi2str(safi),
peer->bgp->stalepath_time);
} else if (subtype == BGP_ROUTE_REFRESH_EORR) {
if (!peer->t_refresh_stalepath) {
zlog_err(
"%pBP rcvd route-refresh (EoRR) for %s/%s, whereas no BoRR received",
peer, afi2str(afi), safi2str(safi));
return BGP_PACKET_NOOP;
}
EVENT_OFF(peer->t_refresh_stalepath);
SET_FLAG(peer->af_sflags[afi][safi], PEER_STATUS_EORR_RECEIVED);
UNSET_FLAG(peer->af_sflags[afi][safi],
PEER_STATUS_BORR_RECEIVED);
if (bgp_debug_neighbor_events(peer))
zlog_debug(
"%pBP rcvd route-refresh (EoRR) for %s/%s, stopping BoRR timer",
peer, afi2str(afi), safi2str(safi));
if (peer->nsf[afi][safi])
bgp_clear_stale_route(peer, afi, safi);
} else {
if (bgp_debug_neighbor_events(peer))
zlog_debug(
"%pBP rcvd route-refresh (REQUEST) for %s/%s",
peer, afi2str(afi), safi2str(safi));
/* In response to a "normal route refresh request" from the
* peer, the speaker MUST send a BoRR message.
*/
if (CHECK_FLAG(peer->cap, PEER_CAP_ENHANCED_RR_RCV)) {
/* For a BGP speaker that supports the BGP Graceful
* Restart, it MUST NOT send a BoRR for an <AFI, SAFI>
* to a neighbor before it sends the EoR for the
* <AFI, SAFI> to the neighbor.
*/
if (!CHECK_FLAG(peer->af_sflags[afi][safi],
PEER_STATUS_EOR_SEND)) {
if (bgp_debug_neighbor_events(peer))
zlog_debug(
"%pBP rcvd route-refresh (REQUEST) for %s/%s before EoR",
peer, afi2str(afi),
safi2str(safi));
/* Can't send BoRR now, postpone after EoR */
SET_FLAG(peer->af_sflags[afi][safi],
PEER_STATUS_REFRESH_PENDING);
return BGP_PACKET_NOOP;
}
bgp_route_refresh_send(peer, afi, safi, 0, 0, 0,
BGP_ROUTE_REFRESH_BORR);
if (bgp_debug_neighbor_events(peer))
zlog_debug(
"%pBP sending route-refresh (BoRR) for %s/%s",
peer, afi2str(afi), safi2str(safi));
/* Set flag Ready-To-Send to know when we can send EoRR
* message.
*/
SET_FLAG(peer->af_sflags[afi][safi],
PEER_STATUS_BORR_SEND);
UNSET_FLAG(peer->af_sflags[afi][safi],
PEER_STATUS_EORR_SEND);
}
}
/* Perform route refreshment to the peer */
bgp_announce_route(peer, afi, safi, force_update);
/* No FSM action necessary */
return BGP_PACKET_NOOP;
}
static void bgp_dynamic_capability_llgr(uint8_t *pnt, int action,
struct capability_header *hdr,
struct peer *peer)
{
uint8_t *data = pnt + 3;
uint8_t *end = data + hdr->length;
size_t len = end - data;
afi_t afi;
safi_t safi;
if (action == CAPABILITY_ACTION_SET) {
if (len < BGP_CAP_LLGR_MIN_PACKET_LEN) {
zlog_err("%pBP: Received invalid Long-Lived Graceful-Restart capability length %zu",
peer, len);
return;
}
SET_FLAG(peer->cap, PEER_CAP_LLGR_RCV);
while (data + BGP_CAP_LLGR_MIN_PACKET_LEN <= end) {
afi_t afi;
safi_t safi;
iana_afi_t pkt_afi;
iana_safi_t pkt_safi;
struct graceful_restart_af graf;
memcpy(&graf, data, sizeof(graf));
pkt_afi = ntohs(graf.afi);
pkt_safi = safi_int2iana(graf.safi);
/* Stale time is after AFI/SAFI/flags.
* It's encoded as 24 bits (= 3 bytes), so we need to
* put it into 32 bits.
*/
uint32_t stale_time;
uint8_t *stale_time_ptr = data + 4;
stale_time = stale_time_ptr[0] << 16;
stale_time |= stale_time_ptr[1] << 8;
stale_time |= stale_time_ptr[2];
if (bgp_map_afi_safi_iana2int(pkt_afi, pkt_safi, &afi,
&safi)) {
if (bgp_debug_neighbor_events(peer))
zlog_debug("%s Addr-family %s/%s(afi/safi) not supported. Ignore the Long-lived Graceful Restart capability for this AFI/SAFI",
peer->host,
iana_afi2str(pkt_afi),
iana_safi2str(pkt_safi));
} else if (!peer->afc[afi][safi] ||
!CHECK_FLAG(peer->af_cap[afi][safi],
PEER_CAP_RESTART_AF_RCV)) {
if (bgp_debug_neighbor_events(peer))
zlog_debug("%s Addr-family %s/%s(afi/safi) not enabled. Ignore the Long-lived Graceful Restart capability",
peer->host,
iana_afi2str(pkt_afi),
iana_safi2str(pkt_safi));
} else {
if (bgp_debug_neighbor_events(peer))
zlog_debug("%s Addr-family %s/%s(afi/safi) Long-lived Graceful Restart capability stale time %u sec",
peer->host,
iana_afi2str(pkt_afi),
iana_safi2str(pkt_safi),
stale_time);
peer->llgr[afi][safi].flags = graf.flag;
peer->llgr[afi][safi].stale_time =
MIN(stale_time,
peer->bgp->llgr_stale_time);
SET_FLAG(peer->af_cap[afi][safi],
PEER_CAP_LLGR_AF_RCV);
}
data += BGP_CAP_LLGR_MIN_PACKET_LEN;
}
} else {
FOREACH_AFI_SAFI (afi, safi) {
UNSET_FLAG(peer->af_cap[afi][safi],
PEER_CAP_LLGR_AF_RCV);
peer->llgr[afi][safi].flags = 0;
peer->llgr[afi][safi].stale_time =
BGP_DEFAULT_LLGR_STALE_TIME;
}
UNSET_FLAG(peer->cap, PEER_CAP_LLGR_RCV);
}
}
static void bgp_dynamic_capability_graceful_restart(uint8_t *pnt, int action,
struct capability_header *hdr,
struct peer *peer)
{
#define GRACEFUL_RESTART_CAPABILITY_PER_AFI_SAFI_SIZE 4
uint16_t gr_restart_flag_time;
uint8_t *data = pnt + 3;
uint8_t *end = pnt + hdr->length;
size_t len = end - data;
afi_t afi;
safi_t safi;
if (action == CAPABILITY_ACTION_SET) {
if (len < sizeof(gr_restart_flag_time)) {
zlog_err("%pBP: Received invalid Graceful-Restart capability length %d",
peer, hdr->length);
return;
}
SET_FLAG(peer->cap, PEER_CAP_RESTART_RCV);
ptr_get_be16(data, &gr_restart_flag_time);
data += sizeof(gr_restart_flag_time);
if (CHECK_FLAG(gr_restart_flag_time, GRACEFUL_RESTART_R_BIT))
SET_FLAG(peer->cap, PEER_CAP_GRACEFUL_RESTART_R_BIT_RCV);
else
UNSET_FLAG(peer->cap,
PEER_CAP_GRACEFUL_RESTART_R_BIT_RCV);
if (CHECK_FLAG(gr_restart_flag_time, GRACEFUL_RESTART_N_BIT))
SET_FLAG(peer->cap, PEER_CAP_GRACEFUL_RESTART_N_BIT_RCV);
else
UNSET_FLAG(peer->cap,
PEER_CAP_GRACEFUL_RESTART_N_BIT_RCV);
UNSET_FLAG(gr_restart_flag_time, 0xF000);
peer->v_gr_restart = gr_restart_flag_time;
while (data + GRACEFUL_RESTART_CAPABILITY_PER_AFI_SAFI_SIZE <=
end) {
afi_t afi;
safi_t safi;
iana_afi_t pkt_afi;
iana_safi_t pkt_safi;
struct graceful_restart_af graf;
memcpy(&graf, data, sizeof(graf));
pkt_afi = ntohs(graf.afi);
pkt_safi = safi_int2iana(graf.safi);
/* Convert AFI, SAFI to internal values, check. */
if (bgp_map_afi_safi_iana2int(pkt_afi, pkt_safi, &afi,
&safi)) {
if (bgp_debug_neighbor_events(peer))
zlog_debug("%pBP: Addr-family %s/%s(afi/safi) not supported. Ignore the Graceful Restart capability for this AFI/SAFI",
peer, iana_afi2str(pkt_afi),
iana_safi2str(pkt_safi));
} else if (!peer->afc[afi][safi]) {
if (bgp_debug_neighbor_events(peer))
zlog_debug("%pBP: Addr-family %s/%s(afi/safi) not enabled. Ignore the Graceful Restart capability",
peer, iana_afi2str(pkt_afi),
iana_safi2str(pkt_safi));
} else {
if (bgp_debug_neighbor_events(peer))
zlog_debug("%pBP: Address family %s is%spreserved",
peer,
get_afi_safi_str(afi, safi,
false),
CHECK_FLAG(peer->af_cap[afi]
[safi],
PEER_CAP_RESTART_AF_PRESERVE_RCV)
? " "
: " not ");
SET_FLAG(peer->af_cap[afi][safi],
PEER_CAP_RESTART_AF_RCV);
if (CHECK_FLAG(graf.flag,
GRACEFUL_RESTART_F_BIT))
SET_FLAG(peer->af_cap[afi][safi],
PEER_CAP_RESTART_AF_PRESERVE_RCV);
}
data += GRACEFUL_RESTART_CAPABILITY_PER_AFI_SAFI_SIZE;
}
} else {
FOREACH_AFI_SAFI (afi, safi) {
UNSET_FLAG(peer->af_cap[afi][safi],
PEER_CAP_RESTART_AF_RCV);
UNSET_FLAG(peer->af_cap[afi][safi],
PEER_CAP_RESTART_AF_PRESERVE_RCV);
}
UNSET_FLAG(peer->cap, PEER_CAP_GRACEFUL_RESTART_R_BIT_RCV);
UNSET_FLAG(peer->cap, PEER_CAP_GRACEFUL_RESTART_N_BIT_RCV);
UNSET_FLAG(peer->cap, PEER_CAP_RESTART_RCV);
}
}
/**
* Parse BGP CAPABILITY message for peer.
*
* @param peer
* @param size size of the packet
* @return as in summary
*/
static int bgp_capability_msg_parse(struct peer *peer, uint8_t *pnt,
bgp_size_t length)
{
uint8_t *end;
struct capability_mp_data mpc;
struct capability_header *hdr;
uint8_t action;
iana_afi_t pkt_afi;
afi_t afi;
iana_safi_t pkt_safi;
safi_t safi;
char soft_version[BGP_MAX_SOFT_VERSION + 1] = {};
const char *capability;
end = pnt + length;
while (pnt < end) {
/* We need at least action, capability code and capability
* length. */
if (pnt + 3 > end) {
zlog_err("%pBP: Capability length error", peer);
bgp_notify_send(peer->connection, BGP_NOTIFY_CEASE,
BGP_NOTIFY_SUBCODE_UNSPECIFIC);
return BGP_Stop;
}
action = *pnt;
hdr = (struct capability_header *)(pnt + 1);
/* Action value check. */
if (action != CAPABILITY_ACTION_SET
&& action != CAPABILITY_ACTION_UNSET) {
zlog_err("%pBP: Capability Action Value error %d", peer,
action);
bgp_notify_send(peer->connection, BGP_NOTIFY_CEASE,
BGP_NOTIFY_SUBCODE_UNSPECIFIC);
return BGP_Stop;
}
if (bgp_debug_neighbor_events(peer))
zlog_debug("%pBP: CAPABILITY has action: %d, code: %u, length %u",
peer, action, hdr->code, hdr->length);
/* Capability length check. */
if ((pnt + hdr->length + 3) > end) {
zlog_err("%pBP: Capability length error", peer);
bgp_notify_send(peer->connection, BGP_NOTIFY_CEASE,
BGP_NOTIFY_SUBCODE_UNSPECIFIC);
return BGP_Stop;
}
/* Ignore capability when override-capability is set. */
if (CHECK_FLAG(peer->flags, PEER_FLAG_OVERRIDE_CAPABILITY))
continue;
capability = lookup_msg(capcode_str, hdr->code, "Unknown");
switch (hdr->code) {
case CAPABILITY_CODE_SOFT_VERSION:
if (action == CAPABILITY_ACTION_SET) {
SET_FLAG(peer->cap, PEER_CAP_SOFT_VERSION_RCV);
memcpy(&soft_version, pnt + 3, hdr->length);
soft_version[hdr->length] = '\0';
XFREE(MTYPE_BGP_SOFT_VERSION,
peer->soft_version);
peer->soft_version =
XSTRDUP(MTYPE_BGP_SOFT_VERSION,
soft_version);
} else {
UNSET_FLAG(peer->cap, PEER_CAP_SOFT_VERSION_RCV);
XFREE(MTYPE_BGP_SOFT_VERSION,
peer->soft_version);
}
break;
case CAPABILITY_CODE_MP:
if (hdr->length < sizeof(struct capability_mp_data)) {
zlog_err("%pBP: Capability (%s) structure is not properly filled out, expected at least %zu bytes but header length specified is %d",
peer, capability,
sizeof(struct capability_mp_data),
hdr->length);
return BGP_Stop;
}
memcpy(&mpc, pnt + 3, sizeof(struct capability_mp_data));
pkt_afi = ntohs(mpc.afi);
pkt_safi = mpc.safi;
/* Convert AFI, SAFI to internal values. */
if (bgp_map_afi_safi_iana2int(pkt_afi, pkt_safi, &afi,
&safi)) {
if (bgp_debug_neighbor_events(peer))
zlog_debug("%pBP: Dynamic Capability %s afi/safi invalid (%s/%s)",
peer, capability,
iana_afi2str(pkt_afi),
iana_safi2str(pkt_safi));
continue;
}
/* Address family check. */
if (bgp_debug_neighbor_events(peer))
zlog_debug("%pBP: CAPABILITY has %s %s CAP for afi/safi: %s/%s",
peer,
action == CAPABILITY_ACTION_SET
? "Advertising"
: "Removing",
capability, iana_afi2str(pkt_afi),
iana_safi2str(pkt_safi));
if (action == CAPABILITY_ACTION_SET) {
peer->afc_recv[afi][safi] = 1;
if (peer->afc[afi][safi]) {
peer->afc_nego[afi][safi] = 1;
bgp_announce_route(peer, afi, safi,
false);
}
} else {
peer->afc_recv[afi][safi] = 0;
peer->afc_nego[afi][safi] = 0;
if (peer_active_nego(peer))
bgp_clear_route(peer, afi, safi);
else
return BGP_Stop;
}
break;
case CAPABILITY_CODE_RESTART:
if ((hdr->length - 2) % 4) {
zlog_err("%pBP: Received invalid Graceful-Restart capability length %d",
peer, hdr->length);
bgp_notify_send(peer->connection,
BGP_NOTIFY_CEASE,
BGP_NOTIFY_SUBCODE_UNSPECIFIC);
return BGP_Stop;
}
bgp_dynamic_capability_graceful_restart(pnt, action,
hdr, peer);
break;
case CAPABILITY_CODE_LLGR:
bgp_dynamic_capability_llgr(pnt, action, hdr, peer);
break;
case CAPABILITY_CODE_REFRESH:
case CAPABILITY_CODE_ORF:
case CAPABILITY_CODE_AS4:
case CAPABILITY_CODE_DYNAMIC:
case CAPABILITY_CODE_ADDPATH:
case CAPABILITY_CODE_ENHANCED_RR:
case CAPABILITY_CODE_FQDN:
case CAPABILITY_CODE_ENHE:
case CAPABILITY_CODE_EXT_MESSAGE:
break;
case CAPABILITY_CODE_ROLE:
if (hdr->length != CAPABILITY_CODE_ROLE_LEN) {
zlog_err("%pBP: Capability (%s) length error",
peer, capability);
bgp_notify_send(peer->connection,
BGP_NOTIFY_CEASE,
BGP_NOTIFY_SUBCODE_UNSPECIFIC);
return BGP_Stop;
}
uint8_t role;
if (action == CAPABILITY_ACTION_SET) {
SET_FLAG(peer->cap, PEER_CAP_ROLE_RCV);
memcpy(&role, pnt + 3, sizeof(role));
peer->remote_role = role;
} else {
UNSET_FLAG(peer->cap, PEER_CAP_ROLE_RCV);
peer->remote_role = ROLE_UNDEFINED;
}
break;
default:
flog_warn(EC_BGP_UNRECOGNIZED_CAPABILITY,
"%pBP: unrecognized capability code: %d - ignored",
peer, hdr->code);
break;
}
pnt += hdr->length + 3;
}
/* No FSM action necessary */
return BGP_PACKET_NOOP;
}
/**
* Parse BGP CAPABILITY message for peer.
*
* Exported for unit testing.
*
* @param peer
* @param size size of the packet
* @return as in summary
*/
int bgp_capability_receive(struct peer_connection *connection,
struct peer *peer, bgp_size_t size)
{
uint8_t *pnt;
/* Fetch pointer. */
pnt = stream_pnt(peer->curr);
if (bgp_debug_neighbor_events(peer))
zlog_debug("%s rcv CAPABILITY", peer->host);
/* If peer does not have the capability, send notification. */
if (!CHECK_FLAG(peer->cap, PEER_CAP_DYNAMIC_ADV)) {
flog_err(EC_BGP_NO_CAP,
"%s [Error] BGP dynamic capability is not enabled",
peer->host);
bgp_notify_send(connection, BGP_NOTIFY_HEADER_ERR,
BGP_NOTIFY_HEADER_BAD_MESTYPE);
return BGP_Stop;
}
/* Status must be Established. */
if (!peer_established(connection)) {
flog_err(EC_BGP_NO_CAP,
"%s [Error] Dynamic capability packet received under status %s",
peer->host,
lookup_msg(bgp_status_msg, connection->status, NULL));
bgp_notify_send(connection, BGP_NOTIFY_FSM_ERR,
bgp_fsm_error_subcode(connection->status));
return BGP_Stop;
}
/* Parse packet. */
return bgp_capability_msg_parse(peer, pnt, size);
}
/**
* Processes a peer's input buffer.
*
* This function sidesteps the event loop and directly calls bgp_event_update()
* after processing each BGP message. This is necessary to ensure proper
* ordering of FSM events and unifies the behavior that was present previously,
* whereby some of the packet handling functions would update the FSM and some
* would not, making event flow difficult to understand. Please think twice
* before hacking this.
*
* Thread type: EVENT_EVENT
* @param thread
* @return 0
*/
void bgp_process_packet(struct event *thread)
{
/* Yes first of all get peer pointer. */
struct peer *peer; // peer
struct peer_connection *connection;
uint32_t rpkt_quanta_old; // how many packets to read
int fsm_update_result; // return code of bgp_event_update()
int mprc; // message processing return code
connection = EVENT_ARG(thread);
peer = connection->peer;
rpkt_quanta_old = atomic_load_explicit(&peer->bgp->rpkt_quanta,
memory_order_relaxed);
fsm_update_result = 0;
/* Guard against scheduled events that occur after peer deletion. */
if (connection->status == Deleted || connection->status == Clearing)
return;
unsigned int processed = 0;
while (processed < rpkt_quanta_old) {
uint8_t type = 0;
bgp_size_t size;
char notify_data_length[2];
frr_with_mutex (&connection->io_mtx) {
peer->curr = stream_fifo_pop(connection->ibuf);
}
if (peer->curr == NULL) // no packets to process, hmm...
return;
/* skip the marker and copy the packet length */
stream_forward_getp(peer->curr, BGP_MARKER_SIZE);
memcpy(notify_data_length, stream_pnt(peer->curr), 2);
/* read in the packet length and type */
size = stream_getw(peer->curr);
type = stream_getc(peer->curr);
hook_call(bgp_packet_dump, peer, type, size, peer->curr);
/* adjust size to exclude the marker + length + type */
size -= BGP_HEADER_SIZE;
/* Read rest of the packet and call each sort of packet routine
*/
switch (type) {
case BGP_MSG_OPEN:
frrtrace(2, frr_bgp, open_process, peer, size);
atomic_fetch_add_explicit(&peer->open_in, 1,
memory_order_relaxed);
mprc = bgp_open_receive(connection, peer, size);
if (mprc == BGP_Stop)
flog_err(
EC_BGP_PKT_OPEN,
"%s: BGP OPEN receipt failed for peer: %s",
__func__, peer->host);
break;
case BGP_MSG_UPDATE:
frrtrace(2, frr_bgp, update_process, peer, size);
atomic_fetch_add_explicit(&peer->update_in, 1,
memory_order_relaxed);
peer->readtime = monotime(NULL);
mprc = bgp_update_receive(connection, peer, size);
if (mprc == BGP_Stop)
flog_err(
EC_BGP_UPDATE_RCV,
"%s: BGP UPDATE receipt failed for peer: %s",
__func__, peer->host);
break;
case BGP_MSG_NOTIFY:
frrtrace(2, frr_bgp, notification_process, peer, size);
atomic_fetch_add_explicit(&peer->notify_in, 1,
memory_order_relaxed);
mprc = bgp_notify_receive(connection, peer, size);
if (mprc == BGP_Stop)
flog_err(
EC_BGP_NOTIFY_RCV,
"%s: BGP NOTIFY receipt failed for peer: %s",
__func__, peer->host);
break;
case BGP_MSG_KEEPALIVE:
frrtrace(2, frr_bgp, keepalive_process, peer, size);
peer->readtime = monotime(NULL);
atomic_fetch_add_explicit(&peer->keepalive_in, 1,
memory_order_relaxed);
mprc = bgp_keepalive_receive(connection, peer, size);
if (mprc == BGP_Stop)
flog_err(
EC_BGP_KEEP_RCV,
"%s: BGP KEEPALIVE receipt failed for peer: %s",
__func__, peer->host);
break;
case BGP_MSG_ROUTE_REFRESH_NEW:
case BGP_MSG_ROUTE_REFRESH_OLD:
frrtrace(2, frr_bgp, refresh_process, peer, size);
atomic_fetch_add_explicit(&peer->refresh_in, 1,
memory_order_relaxed);
mprc = bgp_route_refresh_receive(connection, peer, size);
if (mprc == BGP_Stop)
flog_err(
EC_BGP_RFSH_RCV,
"%s: BGP ROUTEREFRESH receipt failed for peer: %s",
__func__, peer->host);
break;
case BGP_MSG_CAPABILITY:
frrtrace(2, frr_bgp, capability_process, peer, size);
atomic_fetch_add_explicit(&peer->dynamic_cap_in, 1,
memory_order_relaxed);
mprc = bgp_capability_receive(connection, peer, size);
if (mprc == BGP_Stop)
flog_err(
EC_BGP_CAP_RCV,
"%s: BGP CAPABILITY receipt failed for peer: %s",
__func__, peer->host);
break;
default:
/* Suppress uninitialized variable warning */
mprc = 0;
(void)mprc;
/*
* The message type should have been sanitized before
* we ever got here. Receipt of a message with an
* invalid header at this point is indicative of a
* security issue.
*/
assert (!"Message of invalid type received during input processing");
}
/* delete processed packet */
stream_free(peer->curr);
peer->curr = NULL;
processed++;
/* Update FSM */
if (mprc != BGP_PACKET_NOOP)
fsm_update_result = bgp_event_update(connection, mprc);
else
continue;
/*
* If peer was deleted, do not process any more packets. This
* is usually due to executing BGP_Stop or a stub deletion.
*/
if (fsm_update_result == FSM_PEER_TRANSFERRED
|| fsm_update_result == FSM_PEER_STOPPED)
break;
}
if (fsm_update_result != FSM_PEER_TRANSFERRED
&& fsm_update_result != FSM_PEER_STOPPED) {
frr_with_mutex (&connection->io_mtx) {
// more work to do, come back later
if (connection->ibuf->count > 0)
event_add_event(bm->master, bgp_process_packet,
connection, 0,
&connection->t_process_packet);
}
}
}
/* Send EOR when routes are processed by selection deferral timer */
void bgp_send_delayed_eor(struct bgp *bgp)
{
struct peer *peer;
struct listnode *node, *nnode;
/* EOR message sent in bgp_write_proceed_actions */
for (ALL_LIST_ELEMENTS(bgp->peer, node, nnode, peer))
bgp_write_proceed_actions(peer);
}
/*
* Task callback to handle socket error encountered in the io pthread. We avoid
* having the io pthread try to enqueue fsm events or mess with the peer
* struct.
*/
void bgp_packet_process_error(struct event *thread)
{
struct peer_connection *connection;
struct peer *peer;
int code;
connection = EVENT_ARG(thread);
peer = connection->peer;
code = EVENT_VAL(thread);
if (bgp_debug_neighbor_events(peer))
zlog_debug("%s [Event] BGP error %d on fd %d", peer->host, code,
connection->fd);
/* Closed connection or error on the socket */
if (peer_established(connection)) {
if ((CHECK_FLAG(peer->flags, PEER_FLAG_GRACEFUL_RESTART)
|| CHECK_FLAG(peer->flags,
PEER_FLAG_GRACEFUL_RESTART_HELPER))
&& CHECK_FLAG(peer->sflags, PEER_STATUS_NSF_MODE)) {
peer->last_reset = PEER_DOWN_NSF_CLOSE_SESSION;
SET_FLAG(peer->sflags, PEER_STATUS_NSF_WAIT);
} else
peer->last_reset = PEER_DOWN_CLOSE_SESSION;
}
bgp_event_update(connection, code);
}
|