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
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
|
/* $OpenBSD: session.c,v 1.498 2024/11/21 13:38:14 claudio Exp $ */
/*
* Copyright (c) 2003, 2004, 2005 Henning Brauer <henning@openbsd.org>
* Copyright (c) 2017 Peter van Dijk <peter.van.dijk@powerdns.com>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <sys/types.h>
#include <sys/mman.h>
#include <sys/socket.h>
#include <sys/time.h>
#include <sys/resource.h>
#include <sys/un.h>
#include <netinet/in.h>
#include <netinet/ip.h>
#include <netinet/tcp.h>
#include <arpa/inet.h>
#include <limits.h>
#include <err.h>
#include <errno.h>
#include <fcntl.h>
#include <ifaddrs.h>
#include <poll.h>
#include <pwd.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <syslog.h>
#include <unistd.h>
#include "bgpd.h"
#include "session.h"
#include "log.h"
#define PFD_PIPE_MAIN 0
#define PFD_PIPE_ROUTE 1
#define PFD_PIPE_ROUTE_CTL 2
#define PFD_SOCK_CTL 3
#define PFD_SOCK_RCTL 4
#define PFD_LISTENERS_START 5
void session_sighdlr(int);
int setup_listeners(u_int *);
void init_peer(struct peer *);
void start_timer_holdtime(struct peer *);
void start_timer_sendholdtime(struct peer *);
void start_timer_keepalive(struct peer *);
void session_close_connection(struct peer *);
void change_state(struct peer *, enum session_state, enum session_events);
int session_setup_socket(struct peer *);
void session_accept(int);
int session_connect(struct peer *);
void session_tcp_established(struct peer *);
int session_capa_add(struct ibuf *, uint8_t, uint8_t);
int session_capa_add_mp(struct ibuf *, uint8_t);
int session_capa_add_afi(struct ibuf *, uint8_t, uint8_t);
struct ibuf *session_newmsg(enum msg_type, uint16_t);
void session_sendmsg(struct ibuf *, struct peer *, enum msg_type);
void session_open(struct peer *);
void session_keepalive(struct peer *);
void session_update(uint32_t, struct ibuf *);
void session_notification(struct peer *, uint8_t, uint8_t, struct ibuf *);
void session_notification_data(struct peer *, uint8_t, uint8_t, void *,
size_t);
void session_rrefresh(struct peer *, uint8_t, uint8_t);
int session_graceful_restart(struct peer *);
int session_graceful_stop(struct peer *);
int session_dispatch_msg(struct pollfd *, struct peer *);
void session_process_msg(struct peer *);
ssize_t parse_header(struct ibuf *, void *);
int parse_open(struct peer *, struct ibuf *);
int parse_update(struct peer *, struct ibuf *);
int parse_rrefresh(struct peer *, struct ibuf *);
void parse_notification(struct peer *, struct ibuf *);
int parse_capabilities(struct peer *, struct ibuf *, uint32_t *);
int capa_neg_calc(struct peer *);
void session_dispatch_imsg(struct imsgbuf *, int, u_int *);
void session_up(struct peer *);
void session_down(struct peer *);
int imsg_rde(int, uint32_t, void *, uint16_t);
void session_demote(struct peer *, int);
void merge_peers(struct bgpd_config *, struct bgpd_config *);
int la_cmp(struct listen_addr *, struct listen_addr *);
void session_template_clone(struct peer *, struct sockaddr *,
uint32_t, uint32_t);
int session_match_mask(struct peer *, struct bgpd_addr *);
static struct bgpd_config *conf, *nconf;
static struct imsgbuf *ibuf_rde;
static struct imsgbuf *ibuf_rde_ctl;
static struct imsgbuf *ibuf_main;
struct bgpd_sysdep sysdep;
volatile sig_atomic_t session_quit;
int pending_reconf;
int csock = -1, rcsock = -1;
u_int peer_cnt;
struct mrt_head mrthead;
time_t pauseaccept;
static const uint8_t marker[MSGSIZE_HEADER_MARKER] = {
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
};
static inline int
peer_compare(const struct peer *a, const struct peer *b)
{
return a->conf.id - b->conf.id;
}
RB_GENERATE(peer_head, peer, entry, peer_compare);
void
session_sighdlr(int sig)
{
switch (sig) {
case SIGINT:
case SIGTERM:
session_quit = 1;
break;
}
}
int
setup_listeners(u_int *la_cnt)
{
int ttl = 255;
struct listen_addr *la;
u_int cnt = 0;
TAILQ_FOREACH(la, conf->listen_addrs, entry) {
la->reconf = RECONF_NONE;
cnt++;
if (la->flags & LISTENER_LISTENING)
continue;
if (la->fd == -1) {
log_warn("cannot establish listener on %s: invalid fd",
log_sockaddr((struct sockaddr *)&la->sa,
la->sa_len));
continue;
}
if (tcp_md5_prep_listener(la, &conf->peers) == -1)
fatal("tcp_md5_prep_listener");
/* set ttl to 255 so that ttl-security works */
if (la->sa.ss_family == AF_INET && setsockopt(la->fd,
IPPROTO_IP, IP_TTL, &ttl, sizeof(ttl)) == -1) {
log_warn("setup_listeners setsockopt TTL");
continue;
}
if (la->sa.ss_family == AF_INET6 && setsockopt(la->fd,
IPPROTO_IPV6, IPV6_UNICAST_HOPS, &ttl, sizeof(ttl)) == -1) {
log_warn("setup_listeners setsockopt hoplimit");
continue;
}
if (listen(la->fd, MAX_BACKLOG)) {
close(la->fd);
fatal("listen");
}
la->flags |= LISTENER_LISTENING;
log_info("listening on %s",
log_sockaddr((struct sockaddr *)&la->sa, la->sa_len));
}
*la_cnt = cnt;
return (0);
}
void
session_main(int debug, int verbose)
{
int timeout;
unsigned int i, j, idx_peers, idx_listeners, idx_mrts;
u_int pfd_elms = 0, peer_l_elms = 0, mrt_l_elms = 0;
u_int listener_cnt, ctl_cnt, mrt_cnt;
u_int new_cnt;
struct passwd *pw;
struct peer *p, **peer_l = NULL, *next;
struct mrt *m, *xm, **mrt_l = NULL;
struct pollfd *pfd = NULL;
struct listen_addr *la;
void *newp;
time_t now;
short events;
log_init(debug, LOG_DAEMON);
log_setverbose(verbose);
log_procinit(log_procnames[PROC_SE]);
if ((pw = getpwnam(BGPD_USER)) == NULL)
fatal(NULL);
if (chroot(pw->pw_dir) == -1)
fatal("chroot");
if (chdir("/") == -1)
fatal("chdir(\"/\")");
setproctitle("session engine");
if (setgroups(1, &pw->pw_gid) ||
setresgid(pw->pw_gid, pw->pw_gid, pw->pw_gid) ||
setresuid(pw->pw_uid, pw->pw_uid, pw->pw_uid))
fatal("can't drop privileges");
if (pledge("stdio inet recvfd", NULL) == -1)
fatal("pledge");
signal(SIGTERM, session_sighdlr);
signal(SIGINT, session_sighdlr);
signal(SIGPIPE, SIG_IGN);
signal(SIGHUP, SIG_IGN);
signal(SIGALRM, SIG_IGN);
signal(SIGUSR1, SIG_IGN);
if ((ibuf_main = malloc(sizeof(struct imsgbuf))) == NULL)
fatal(NULL);
if (imsgbuf_init(ibuf_main, 3) == -1)
fatal(NULL);
imsgbuf_allow_fdpass(ibuf_main);
LIST_INIT(&mrthead);
listener_cnt = 0;
peer_cnt = 0;
ctl_cnt = 0;
conf = new_config();
log_info("session engine ready");
while (session_quit == 0) {
/* check for peers to be initialized or deleted */
if (!pending_reconf) {
RB_FOREACH_SAFE(p, peer_head, &conf->peers, next) {
/* cloned peer that idled out? */
if (p->template && (p->state == STATE_IDLE ||
p->state == STATE_ACTIVE) &&
getmonotime() - p->stats.last_updown >=
INTERVAL_HOLD_CLONED)
p->reconf_action = RECONF_DELETE;
/* new peer that needs init? */
if (p->state == STATE_NONE)
init_peer(p);
/* deletion due? */
if (p->reconf_action == RECONF_DELETE) {
if (p->demoted)
session_demote(p, -1);
p->conf.demote_group[0] = 0;
session_stop(p, ERR_CEASE_PEER_UNCONF,
NULL);
timer_remove_all(&p->timers);
tcp_md5_del_listener(conf, p);
msgbuf_free(p->wbuf);
RB_REMOVE(peer_head, &conf->peers, p);
log_peer_warnx(&p->conf, "removed");
free(p);
peer_cnt--;
continue;
}
p->reconf_action = RECONF_NONE;
}
}
if (peer_cnt > peer_l_elms) {
if ((newp = reallocarray(peer_l, peer_cnt,
sizeof(struct peer *))) == NULL) {
/* panic for now */
log_warn("could not resize peer_l from %u -> %u"
" entries", peer_l_elms, peer_cnt);
fatalx("exiting");
}
peer_l = newp;
peer_l_elms = peer_cnt;
}
mrt_cnt = 0;
for (m = LIST_FIRST(&mrthead); m != NULL; m = xm) {
xm = LIST_NEXT(m, entry);
if (m->state == MRT_STATE_REMOVE) {
mrt_clean(m);
LIST_REMOVE(m, entry);
free(m);
continue;
}
if (msgbuf_queuelen(m->wbuf) > 0)
mrt_cnt++;
}
if (mrt_cnt > mrt_l_elms) {
if ((newp = reallocarray(mrt_l, mrt_cnt,
sizeof(struct mrt *))) == NULL) {
/* panic for now */
log_warn("could not resize mrt_l from %u -> %u"
" entries", mrt_l_elms, mrt_cnt);
fatalx("exiting");
}
mrt_l = newp;
mrt_l_elms = mrt_cnt;
}
new_cnt = PFD_LISTENERS_START + listener_cnt + peer_cnt +
ctl_cnt + mrt_cnt;
if (new_cnt > pfd_elms) {
if ((newp = reallocarray(pfd, new_cnt,
sizeof(struct pollfd))) == NULL) {
/* panic for now */
log_warn("could not resize pfd from %u -> %u"
" entries", pfd_elms, new_cnt);
fatalx("exiting");
}
pfd = newp;
pfd_elms = new_cnt;
}
memset(pfd, 0, sizeof(struct pollfd) * pfd_elms);
set_pollfd(&pfd[PFD_PIPE_MAIN], ibuf_main);
set_pollfd(&pfd[PFD_PIPE_ROUTE], ibuf_rde);
set_pollfd(&pfd[PFD_PIPE_ROUTE_CTL], ibuf_rde_ctl);
if (pauseaccept == 0) {
pfd[PFD_SOCK_CTL].fd = csock;
pfd[PFD_SOCK_CTL].events = POLLIN;
pfd[PFD_SOCK_RCTL].fd = rcsock;
pfd[PFD_SOCK_RCTL].events = POLLIN;
} else {
pfd[PFD_SOCK_CTL].fd = -1;
pfd[PFD_SOCK_RCTL].fd = -1;
}
i = PFD_LISTENERS_START;
TAILQ_FOREACH(la, conf->listen_addrs, entry) {
if (pauseaccept == 0) {
pfd[i].fd = la->fd;
pfd[i].events = POLLIN;
} else
pfd[i].fd = -1;
i++;
}
idx_listeners = i;
timeout = 240; /* loop every 240s at least */
now = getmonotime();
RB_FOREACH(p, peer_head, &conf->peers) {
time_t nextaction;
struct timer *pt;
/* check timers */
if ((pt = timer_nextisdue(&p->timers, now)) != NULL) {
switch (pt->type) {
case Timer_Hold:
bgp_fsm(p, EVNT_TIMER_HOLDTIME, NULL);
break;
case Timer_SendHold:
bgp_fsm(p, EVNT_TIMER_SENDHOLD, NULL);
break;
case Timer_ConnectRetry:
bgp_fsm(p, EVNT_TIMER_CONNRETRY, NULL);
break;
case Timer_Keepalive:
bgp_fsm(p, EVNT_TIMER_KEEPALIVE, NULL);
break;
case Timer_IdleHold:
bgp_fsm(p, EVNT_START, NULL);
break;
case Timer_IdleHoldReset:
p->IdleHoldTime =
INTERVAL_IDLE_HOLD_INITIAL;
p->errcnt = 0;
timer_stop(&p->timers,
Timer_IdleHoldReset);
break;
case Timer_CarpUndemote:
timer_stop(&p->timers,
Timer_CarpUndemote);
if (p->demoted &&
p->state == STATE_ESTABLISHED)
session_demote(p, -1);
break;
case Timer_RestartTimeout:
timer_stop(&p->timers,
Timer_RestartTimeout);
session_graceful_stop(p);
break;
default:
fatalx("King Bula lost in time");
}
}
if ((nextaction = timer_nextduein(&p->timers,
now)) != -1 && nextaction < timeout)
timeout = nextaction;
/* are we waiting for a write? */
events = POLLIN;
if (msgbuf_queuelen(p->wbuf) > 0 ||
p->state == STATE_CONNECT)
events |= POLLOUT;
/* is there still work to do? */
if (p->rpending)
timeout = 0;
/* poll events */
if (p->fd != -1 && events != 0) {
pfd[i].fd = p->fd;
pfd[i].events = events;
peer_l[i - idx_listeners] = p;
i++;
}
}
idx_peers = i;
LIST_FOREACH(m, &mrthead, entry)
if (msgbuf_queuelen(m->wbuf) > 0) {
pfd[i].fd = m->fd;
pfd[i].events = POLLOUT;
mrt_l[i - idx_peers] = m;
i++;
}
idx_mrts = i;
i += control_fill_pfds(pfd + i, pfd_elms -i);
if (i > pfd_elms)
fatalx("poll pfd overflow");
if (pauseaccept && timeout > 1)
timeout = 1;
if (timeout < 0)
timeout = 0;
if (poll(pfd, i, timeout * 1000) == -1) {
if (errno == EINTR)
continue;
fatal("poll error");
}
/*
* If we previously saw fd exhaustion, we stop accept()
* for 1 second to throttle the accept() loop.
*/
if (pauseaccept && getmonotime() > pauseaccept + 1)
pauseaccept = 0;
if (handle_pollfd(&pfd[PFD_PIPE_MAIN], ibuf_main) == -1) {
log_warnx("SE: Lost connection to parent");
session_quit = 1;
continue;
} else
session_dispatch_imsg(ibuf_main, PFD_PIPE_MAIN,
&listener_cnt);
if (handle_pollfd(&pfd[PFD_PIPE_ROUTE], ibuf_rde) == -1) {
log_warnx("SE: Lost connection to RDE");
imsgbuf_clear(ibuf_rde);
free(ibuf_rde);
ibuf_rde = NULL;
} else
session_dispatch_imsg(ibuf_rde, PFD_PIPE_ROUTE,
&listener_cnt);
if (handle_pollfd(&pfd[PFD_PIPE_ROUTE_CTL], ibuf_rde_ctl) ==
-1) {
log_warnx("SE: Lost connection to RDE control");
imsgbuf_clear(ibuf_rde_ctl);
free(ibuf_rde_ctl);
ibuf_rde_ctl = NULL;
} else
session_dispatch_imsg(ibuf_rde_ctl, PFD_PIPE_ROUTE_CTL,
&listener_cnt);
if (pfd[PFD_SOCK_CTL].revents & POLLIN)
ctl_cnt += control_accept(csock, 0);
if (pfd[PFD_SOCK_RCTL].revents & POLLIN)
ctl_cnt += control_accept(rcsock, 1);
for (j = PFD_LISTENERS_START; j < idx_listeners; j++)
if (pfd[j].revents & POLLIN)
session_accept(pfd[j].fd);
for (; j < idx_peers; j++)
session_dispatch_msg(&pfd[j],
peer_l[j - idx_listeners]);
RB_FOREACH(p, peer_head, &conf->peers)
session_process_msg(p);
for (; j < idx_mrts; j++)
if (pfd[j].revents & POLLOUT)
mrt_write(mrt_l[j - idx_peers]);
for (; j < i; j++)
ctl_cnt -= control_dispatch_msg(&pfd[j], &conf->peers);
}
RB_FOREACH_SAFE(p, peer_head, &conf->peers, next) {
session_stop(p, ERR_CEASE_ADMIN_DOWN, "bgpd shutting down");
timer_remove_all(&p->timers);
tcp_md5_del_listener(conf, p);
RB_REMOVE(peer_head, &conf->peers, p);
free(p);
}
while ((m = LIST_FIRST(&mrthead)) != NULL) {
mrt_clean(m);
LIST_REMOVE(m, entry);
free(m);
}
free_config(conf);
free(peer_l);
free(mrt_l);
free(pfd);
/* close pipes */
if (ibuf_rde) {
imsgbuf_write(ibuf_rde);
imsgbuf_clear(ibuf_rde);
close(ibuf_rde->fd);
free(ibuf_rde);
}
if (ibuf_rde_ctl) {
imsgbuf_clear(ibuf_rde_ctl);
close(ibuf_rde_ctl->fd);
free(ibuf_rde_ctl);
}
imsgbuf_write(ibuf_main);
imsgbuf_clear(ibuf_main);
close(ibuf_main->fd);
free(ibuf_main);
control_shutdown(csock);
control_shutdown(rcsock);
log_info("session engine exiting");
exit(0);
}
void
init_peer(struct peer *p)
{
TAILQ_INIT(&p->timers);
p->fd = -1;
if (p->wbuf != NULL)
fatalx("%s: msgbuf already set", __func__);
if ((p->wbuf = msgbuf_new_reader(MSGSIZE_HEADER, parse_header, p)) ==
NULL)
fatal(NULL);
if (p->conf.if_depend[0])
imsg_compose(ibuf_main, IMSG_SESSION_DEPENDON, 0, 0, -1,
p->conf.if_depend, sizeof(p->conf.if_depend));
else
p->depend_ok = 1;
peer_cnt++;
change_state(p, STATE_IDLE, EVNT_NONE);
if (p->conf.down)
timer_stop(&p->timers, Timer_IdleHold); /* no autostart */
else
timer_set(&p->timers, Timer_IdleHold, SESSION_CLEAR_DELAY);
p->stats.last_updown = getmonotime();
/*
* on startup, demote if requested.
* do not handle new peers. they must reach ESTABLISHED beforehand.
* peers added at runtime have reconf_action set to RECONF_REINIT.
*/
if (p->reconf_action != RECONF_REINIT && p->conf.demote_group[0])
session_demote(p, +1);
}
void
bgp_fsm(struct peer *peer, enum session_events event, struct ibuf *msg)
{
switch (peer->state) {
case STATE_NONE:
/* nothing */
break;
case STATE_IDLE:
switch (event) {
case EVNT_START:
timer_stop(&peer->timers, Timer_Hold);
timer_stop(&peer->timers, Timer_SendHold);
timer_stop(&peer->timers, Timer_Keepalive);
timer_stop(&peer->timers, Timer_IdleHold);
if (!peer->depend_ok)
timer_stop(&peer->timers, Timer_ConnectRetry);
else if (peer->passive || peer->conf.passive ||
peer->conf.template) {
change_state(peer, STATE_ACTIVE, event);
timer_stop(&peer->timers, Timer_ConnectRetry);
} else {
change_state(peer, STATE_CONNECT, event);
timer_set(&peer->timers, Timer_ConnectRetry,
conf->connectretry);
session_connect(peer);
}
peer->passive = 0;
break;
case EVNT_STOP:
timer_stop(&peer->timers, Timer_IdleHold);
break;
default:
/* ignore */
break;
}
break;
case STATE_CONNECT:
switch (event) {
case EVNT_START:
/* ignore */
break;
case EVNT_CON_OPEN:
session_tcp_established(peer);
session_open(peer);
timer_stop(&peer->timers, Timer_ConnectRetry);
peer->holdtime = INTERVAL_HOLD_INITIAL;
start_timer_holdtime(peer);
change_state(peer, STATE_OPENSENT, event);
break;
case EVNT_CON_OPENFAIL:
timer_set(&peer->timers, Timer_ConnectRetry,
conf->connectretry);
session_close_connection(peer);
change_state(peer, STATE_ACTIVE, event);
break;
case EVNT_TIMER_CONNRETRY:
timer_set(&peer->timers, Timer_ConnectRetry,
conf->connectretry);
session_connect(peer);
break;
default:
change_state(peer, STATE_IDLE, event);
break;
}
break;
case STATE_ACTIVE:
switch (event) {
case EVNT_START:
/* ignore */
break;
case EVNT_CON_OPEN:
session_tcp_established(peer);
session_open(peer);
timer_stop(&peer->timers, Timer_ConnectRetry);
peer->holdtime = INTERVAL_HOLD_INITIAL;
start_timer_holdtime(peer);
change_state(peer, STATE_OPENSENT, event);
break;
case EVNT_CON_OPENFAIL:
timer_set(&peer->timers, Timer_ConnectRetry,
conf->connectretry);
session_close_connection(peer);
change_state(peer, STATE_ACTIVE, event);
break;
case EVNT_TIMER_CONNRETRY:
timer_set(&peer->timers, Timer_ConnectRetry,
peer->holdtime);
change_state(peer, STATE_CONNECT, event);
session_connect(peer);
break;
default:
change_state(peer, STATE_IDLE, event);
break;
}
break;
case STATE_OPENSENT:
switch (event) {
case EVNT_START:
/* ignore */
break;
case EVNT_STOP:
change_state(peer, STATE_IDLE, event);
break;
case EVNT_CON_CLOSED:
session_close_connection(peer);
timer_set(&peer->timers, Timer_ConnectRetry,
conf->connectretry);
change_state(peer, STATE_ACTIVE, event);
break;
case EVNT_CON_FATAL:
change_state(peer, STATE_IDLE, event);
break;
case EVNT_TIMER_HOLDTIME:
session_notification(peer, ERR_HOLDTIMEREXPIRED,
0, NULL);
change_state(peer, STATE_IDLE, event);
break;
case EVNT_TIMER_SENDHOLD:
session_notification(peer, ERR_SENDHOLDTIMEREXPIRED,
0, NULL);
change_state(peer, STATE_IDLE, event);
break;
case EVNT_RCVD_OPEN:
/* parse_open calls change_state itself on failure */
if (parse_open(peer, msg))
break;
session_keepalive(peer);
change_state(peer, STATE_OPENCONFIRM, event);
break;
case EVNT_RCVD_NOTIFICATION:
parse_notification(peer, msg);
break;
default:
session_notification(peer,
ERR_FSM, ERR_FSM_UNEX_OPENSENT, NULL);
change_state(peer, STATE_IDLE, event);
break;
}
break;
case STATE_OPENCONFIRM:
switch (event) {
case EVNT_START:
/* ignore */
break;
case EVNT_STOP:
change_state(peer, STATE_IDLE, event);
break;
case EVNT_CON_CLOSED:
case EVNT_CON_FATAL:
change_state(peer, STATE_IDLE, event);
break;
case EVNT_TIMER_HOLDTIME:
session_notification(peer, ERR_HOLDTIMEREXPIRED,
0, NULL);
change_state(peer, STATE_IDLE, event);
break;
case EVNT_TIMER_SENDHOLD:
session_notification(peer, ERR_SENDHOLDTIMEREXPIRED,
0, NULL);
change_state(peer, STATE_IDLE, event);
break;
case EVNT_TIMER_KEEPALIVE:
session_keepalive(peer);
break;
case EVNT_RCVD_KEEPALIVE:
start_timer_holdtime(peer);
change_state(peer, STATE_ESTABLISHED, event);
break;
case EVNT_RCVD_NOTIFICATION:
parse_notification(peer, msg);
break;
default:
session_notification(peer,
ERR_FSM, ERR_FSM_UNEX_OPENCONFIRM, NULL);
change_state(peer, STATE_IDLE, event);
break;
}
break;
case STATE_ESTABLISHED:
switch (event) {
case EVNT_START:
/* ignore */
break;
case EVNT_STOP:
change_state(peer, STATE_IDLE, event);
break;
case EVNT_CON_CLOSED:
case EVNT_CON_FATAL:
change_state(peer, STATE_IDLE, event);
break;
case EVNT_TIMER_HOLDTIME:
session_notification(peer, ERR_HOLDTIMEREXPIRED,
0, NULL);
change_state(peer, STATE_IDLE, event);
break;
case EVNT_TIMER_SENDHOLD:
session_notification(peer, ERR_SENDHOLDTIMEREXPIRED,
0, NULL);
change_state(peer, STATE_IDLE, event);
break;
case EVNT_TIMER_KEEPALIVE:
session_keepalive(peer);
break;
case EVNT_RCVD_KEEPALIVE:
start_timer_holdtime(peer);
break;
case EVNT_RCVD_UPDATE:
start_timer_holdtime(peer);
if (parse_update(peer, msg))
change_state(peer, STATE_IDLE, event);
else
start_timer_holdtime(peer);
break;
case EVNT_RCVD_NOTIFICATION:
parse_notification(peer, msg);
break;
default:
session_notification(peer,
ERR_FSM, ERR_FSM_UNEX_ESTABLISHED, NULL);
change_state(peer, STATE_IDLE, event);
break;
}
break;
}
}
void
start_timer_holdtime(struct peer *peer)
{
if (peer->holdtime > 0)
timer_set(&peer->timers, Timer_Hold, peer->holdtime);
else
timer_stop(&peer->timers, Timer_Hold);
}
void
start_timer_sendholdtime(struct peer *peer)
{
uint16_t holdtime = INTERVAL_HOLD;
if (peer->holdtime > INTERVAL_HOLD)
holdtime = peer->holdtime;
if (peer->holdtime > 0)
timer_set(&peer->timers, Timer_SendHold, holdtime);
else
timer_stop(&peer->timers, Timer_SendHold);
}
void
start_timer_keepalive(struct peer *peer)
{
if (peer->holdtime > 0)
timer_set(&peer->timers, Timer_Keepalive, peer->holdtime / 3);
else
timer_stop(&peer->timers, Timer_Keepalive);
}
void
session_close_connection(struct peer *peer)
{
if (peer->fd != -1) {
close(peer->fd);
pauseaccept = 0;
}
peer->fd = -1;
}
void
change_state(struct peer *peer, enum session_state state,
enum session_events event)
{
struct mrt *mrt;
switch (state) {
case STATE_IDLE:
/* carp demotion first. new peers handled in init_peer */
if (peer->state == STATE_ESTABLISHED &&
peer->conf.demote_group[0] && !peer->demoted)
session_demote(peer, +1);
/*
* try to write out what's buffered (maybe a notification),
* don't bother if it fails
*/
if (peer->state >= STATE_OPENSENT &&
msgbuf_queuelen(peer->wbuf) > 0)
ibuf_write(peer->fd, peer->wbuf);
/*
* we must start the timer for the next EVNT_START
* if we are coming here due to an error and the
* session was not established successfully before, the
* starttimerinterval needs to be exponentially increased
*/
if (peer->IdleHoldTime == 0)
peer->IdleHoldTime = INTERVAL_IDLE_HOLD_INITIAL;
peer->holdtime = INTERVAL_HOLD_INITIAL;
timer_stop(&peer->timers, Timer_ConnectRetry);
timer_stop(&peer->timers, Timer_Keepalive);
timer_stop(&peer->timers, Timer_Hold);
timer_stop(&peer->timers, Timer_SendHold);
timer_stop(&peer->timers, Timer_IdleHold);
timer_stop(&peer->timers, Timer_IdleHoldReset);
session_close_connection(peer);
msgbuf_clear(peer->wbuf);
peer->rpending = 0;
memset(&peer->capa.peer, 0, sizeof(peer->capa.peer));
if (!peer->template)
imsg_compose(ibuf_main, IMSG_PFKEY_RELOAD,
peer->conf.id, 0, -1, NULL, 0);
if (event != EVNT_STOP) {
timer_set(&peer->timers, Timer_IdleHold,
peer->IdleHoldTime);
if (event != EVNT_NONE &&
peer->IdleHoldTime < MAX_IDLE_HOLD/2)
peer->IdleHoldTime *= 2;
}
if (peer->state == STATE_ESTABLISHED) {
if (peer->capa.neg.grestart.restart == 2 &&
(event == EVNT_CON_CLOSED ||
event == EVNT_CON_FATAL)) {
/* don't punish graceful restart */
timer_set(&peer->timers, Timer_IdleHold, 0);
peer->IdleHoldTime /= 2;
session_graceful_restart(peer);
} else
session_down(peer);
}
if (peer->state == STATE_NONE ||
peer->state == STATE_ESTABLISHED) {
/* initialize capability negotiation structures */
memcpy(&peer->capa.ann, &peer->conf.capabilities,
sizeof(peer->capa.ann));
}
break;
case STATE_CONNECT:
if (peer->state == STATE_ESTABLISHED &&
peer->capa.neg.grestart.restart == 2) {
/* do the graceful restart dance */
session_graceful_restart(peer);
peer->holdtime = INTERVAL_HOLD_INITIAL;
timer_stop(&peer->timers, Timer_ConnectRetry);
timer_stop(&peer->timers, Timer_Keepalive);
timer_stop(&peer->timers, Timer_Hold);
timer_stop(&peer->timers, Timer_SendHold);
timer_stop(&peer->timers, Timer_IdleHold);
timer_stop(&peer->timers, Timer_IdleHoldReset);
session_close_connection(peer);
msgbuf_clear(peer->wbuf);
memset(&peer->capa.peer, 0, sizeof(peer->capa.peer));
}
break;
case STATE_ACTIVE:
if (!peer->template)
imsg_compose(ibuf_main, IMSG_PFKEY_RELOAD,
peer->conf.id, 0, -1, NULL, 0);
break;
case STATE_OPENSENT:
break;
case STATE_OPENCONFIRM:
break;
case STATE_ESTABLISHED:
timer_set(&peer->timers, Timer_IdleHoldReset,
peer->IdleHoldTime);
if (peer->demoted)
timer_set(&peer->timers, Timer_CarpUndemote,
INTERVAL_HOLD_DEMOTED);
session_up(peer);
break;
default: /* something seriously fucked */
break;
}
log_statechange(peer, state, event);
LIST_FOREACH(mrt, &mrthead, entry) {
if (!(mrt->type == MRT_ALL_IN || mrt->type == MRT_ALL_OUT))
continue;
if ((mrt->peer_id == 0 && mrt->group_id == 0) ||
mrt->peer_id == peer->conf.id || (mrt->group_id != 0 &&
mrt->group_id == peer->conf.groupid))
mrt_dump_state(mrt, peer->state, state, peer);
}
peer->prev_state = peer->state;
peer->state = state;
}
void
session_accept(int listenfd)
{
int connfd;
socklen_t len;
struct sockaddr_storage cliaddr;
struct peer *p = NULL;
len = sizeof(cliaddr);
if ((connfd = accept4(listenfd,
(struct sockaddr *)&cliaddr, &len,
SOCK_CLOEXEC | SOCK_NONBLOCK)) == -1) {
if (errno == ENFILE || errno == EMFILE)
pauseaccept = getmonotime();
else if (errno != EWOULDBLOCK && errno != EINTR &&
errno != ECONNABORTED)
log_warn("accept");
return;
}
p = getpeerbyip(conf, (struct sockaddr *)&cliaddr);
if (p != NULL && p->state == STATE_IDLE && p->errcnt < 2) {
if (timer_running(&p->timers, Timer_IdleHold, NULL)) {
/* fast reconnect after clear */
p->passive = 1;
bgp_fsm(p, EVNT_START, NULL);
}
}
if (p != NULL &&
(p->state == STATE_CONNECT || p->state == STATE_ACTIVE)) {
if (p->fd != -1) {
if (p->state == STATE_CONNECT)
session_close_connection(p);
else {
close(connfd);
return;
}
}
open:
if (p->auth_conf.method != AUTH_NONE && sysdep.no_pfkey) {
log_peer_warnx(&p->conf,
"ipsec or md5sig configured but not available");
close(connfd);
return;
}
if (tcp_md5_check(connfd, &p->auth_conf) == -1) {
log_peer_warn(&p->conf, "check md5sig");
close(connfd);
return;
}
p->fd = connfd;
if (session_setup_socket(p)) {
close(connfd);
return;
}
bgp_fsm(p, EVNT_CON_OPEN, NULL);
return;
} else if (p != NULL && p->state == STATE_ESTABLISHED &&
p->capa.neg.grestart.restart == 2) {
/* first do the graceful restart dance */
change_state(p, STATE_CONNECT, EVNT_CON_CLOSED);
/* then do part of the open dance */
goto open;
} else {
log_conn_attempt(p, (struct sockaddr *)&cliaddr, len);
close(connfd);
}
}
int
session_connect(struct peer *peer)
{
struct sockaddr *sa;
struct bgpd_addr *bind_addr;
socklen_t sa_len;
/*
* we do not need the overcomplicated collision detection RFC 1771
* describes; we simply make sure there is only ever one concurrent
* tcp connection per peer.
*/
if (peer->fd != -1)
return (-1);
if ((peer->fd = socket(aid2af(peer->conf.remote_addr.aid),
SOCK_STREAM | SOCK_CLOEXEC | SOCK_NONBLOCK, IPPROTO_TCP)) == -1) {
log_peer_warn(&peer->conf, "session_connect socket");
bgp_fsm(peer, EVNT_CON_OPENFAIL, NULL);
return (-1);
}
if (peer->auth_conf.method != AUTH_NONE && sysdep.no_pfkey) {
log_peer_warnx(&peer->conf,
"ipsec or md5sig configured but not available");
bgp_fsm(peer, EVNT_CON_OPENFAIL, NULL);
return (-1);
}
if (tcp_md5_set(peer->fd, &peer->auth_conf,
&peer->conf.remote_addr) == -1)
log_peer_warn(&peer->conf, "setting md5sig");
/* if local-address is set we need to bind() */
bind_addr = session_localaddr(peer);
if ((sa = addr2sa(bind_addr, 0, &sa_len)) != NULL) {
if (bind(peer->fd, sa, sa_len) == -1) {
log_peer_warn(&peer->conf, "session_connect bind");
bgp_fsm(peer, EVNT_CON_OPENFAIL, NULL);
return (-1);
}
}
if (session_setup_socket(peer)) {
bgp_fsm(peer, EVNT_CON_OPENFAIL, NULL);
return (-1);
}
sa = addr2sa(&peer->conf.remote_addr, peer->conf.remote_port, &sa_len);
if (connect(peer->fd, sa, sa_len) == -1) {
if (errno != EINPROGRESS) {
if (errno != peer->lasterr)
log_peer_warn(&peer->conf, "connect");
peer->lasterr = errno;
bgp_fsm(peer, EVNT_CON_OPENFAIL, NULL);
return (-1);
}
} else
bgp_fsm(peer, EVNT_CON_OPEN, NULL);
return (0);
}
int
session_setup_socket(struct peer *p)
{
int ttl = p->conf.distance;
int pre = IPTOS_PREC_INTERNETCONTROL;
int nodelay = 1;
int bsize;
switch (p->conf.remote_addr.aid) {
case AID_INET:
/* set precedence, see RFC 1771 appendix 5 */
if (setsockopt(p->fd, IPPROTO_IP, IP_TOS, &pre, sizeof(pre)) ==
-1) {
log_peer_warn(&p->conf,
"session_setup_socket setsockopt TOS");
return (-1);
}
if (p->conf.ebgp) {
/*
* set TTL to foreign router's distance
* 1=direct n=multihop with ttlsec, we always use 255
*/
if (p->conf.ttlsec) {
ttl = 256 - p->conf.distance;
if (setsockopt(p->fd, IPPROTO_IP, IP_MINTTL,
&ttl, sizeof(ttl)) == -1) {
log_peer_warn(&p->conf,
"session_setup_socket: "
"setsockopt MINTTL");
return (-1);
}
ttl = 255;
}
if (setsockopt(p->fd, IPPROTO_IP, IP_TTL, &ttl,
sizeof(ttl)) == -1) {
log_peer_warn(&p->conf,
"session_setup_socket setsockopt TTL");
return (-1);
}
}
break;
case AID_INET6:
if (setsockopt(p->fd, IPPROTO_IPV6, IPV6_TCLASS, &pre,
sizeof(pre)) == -1) {
log_peer_warn(&p->conf, "session_setup_socket "
"setsockopt TCLASS");
return (-1);
}
if (p->conf.ebgp) {
/*
* set hoplimit to foreign router's distance
* 1=direct n=multihop with ttlsec, we always use 255
*/
if (p->conf.ttlsec) {
ttl = 256 - p->conf.distance;
if (setsockopt(p->fd, IPPROTO_IPV6,
IPV6_MINHOPCOUNT, &ttl, sizeof(ttl))
== -1) {
log_peer_warn(&p->conf,
"session_setup_socket: "
"setsockopt MINHOPCOUNT");
return (-1);
}
ttl = 255;
}
if (setsockopt(p->fd, IPPROTO_IPV6, IPV6_UNICAST_HOPS,
&ttl, sizeof(ttl)) == -1) {
log_peer_warn(&p->conf,
"session_setup_socket setsockopt hoplimit");
return (-1);
}
}
break;
}
/* set TCP_NODELAY */
if (setsockopt(p->fd, IPPROTO_TCP, TCP_NODELAY, &nodelay,
sizeof(nodelay)) == -1) {
log_peer_warn(&p->conf,
"session_setup_socket setsockopt TCP_NODELAY");
return (-1);
}
/* limit bufsize. no biggie if it fails */
bsize = 65535;
setsockopt(p->fd, SOL_SOCKET, SO_RCVBUF, &bsize, sizeof(bsize));
setsockopt(p->fd, SOL_SOCKET, SO_SNDBUF, &bsize, sizeof(bsize));
return (0);
}
/*
* compare the bgpd_addr with the sockaddr by converting the latter into
* a bgpd_addr. Return true if the two are equal, including any scope
*/
static int
sa_equal(struct bgpd_addr *ba, struct sockaddr *b)
{
struct bgpd_addr bb;
sa2addr(b, &bb, NULL);
return (memcmp(ba, &bb, sizeof(*ba)) == 0);
}
static void
get_alternate_addr(struct bgpd_addr *local, struct bgpd_addr *remote,
struct bgpd_addr *alt, unsigned int *scope)
{
struct ifaddrs *ifap, *ifa, *match;
int connected = 0;
u_int8_t plen;
if (getifaddrs(&ifap) == -1)
fatal("getifaddrs");
for (match = ifap; match != NULL; match = match->ifa_next) {
if (match->ifa_addr == NULL)
continue;
if (match->ifa_addr->sa_family != AF_INET &&
match->ifa_addr->sa_family != AF_INET6)
continue;
if (sa_equal(local, match->ifa_addr)) {
if (remote->aid == AID_INET6 &&
IN6_IS_ADDR_LINKLOCAL(&remote->v6)) {
/* IPv6 LLA are by definition connected */
connected = 1;
} else if (match->ifa_flags & IFF_POINTOPOINT &&
match->ifa_dstaddr != NULL) {
if (sa_equal(remote, match->ifa_dstaddr))
connected = 1;
} else if (match->ifa_netmask != NULL) {
plen = mask2prefixlen(
match->ifa_addr->sa_family,
match->ifa_netmask);
if (prefix_compare(local, remote, plen) == 0)
connected = 1;
}
break;
}
}
if (match == NULL) {
log_warnx("%s: local address not found", __func__);
return;
}
if (connected)
*scope = if_nametoindex(match->ifa_name);
else
*scope = 0;
switch (local->aid) {
case AID_INET6:
for (ifa = ifap; ifa != NULL; ifa = ifa->ifa_next) {
if (ifa->ifa_addr != NULL &&
ifa->ifa_addr->sa_family == AF_INET &&
strcmp(ifa->ifa_name, match->ifa_name) == 0) {
sa2addr(ifa->ifa_addr, alt, NULL);
break;
}
}
break;
case AID_INET:
for (ifa = ifap; ifa != NULL; ifa = ifa->ifa_next) {
if (ifa->ifa_addr != NULL &&
ifa->ifa_addr->sa_family == AF_INET6 &&
strcmp(ifa->ifa_name, match->ifa_name) == 0) {
struct sockaddr_in6 *s =
(struct sockaddr_in6 *)ifa->ifa_addr;
/* only accept global scope addresses */
if (IN6_IS_ADDR_LINKLOCAL(&s->sin6_addr) ||
IN6_IS_ADDR_SITELOCAL(&s->sin6_addr))
continue;
sa2addr(ifa->ifa_addr, alt, NULL);
break;
}
}
break;
default:
log_warnx("%s: unsupported address family %s", __func__,
aid2str(local->aid));
break;
}
freeifaddrs(ifap);
}
void
session_tcp_established(struct peer *peer)
{
struct sockaddr_storage ss;
socklen_t len;
len = sizeof(ss);
if (getsockname(peer->fd, (struct sockaddr *)&ss, &len) == -1)
log_warn("getsockname");
sa2addr((struct sockaddr *)&ss, &peer->local, &peer->local_port);
len = sizeof(ss);
if (getpeername(peer->fd, (struct sockaddr *)&ss, &len) == -1)
log_warn("getpeername");
sa2addr((struct sockaddr *)&ss, &peer->remote, &peer->remote_port);
get_alternate_addr(&peer->local, &peer->remote, &peer->local_alt,
&peer->if_scope);
}
int
session_capa_add(struct ibuf *opb, uint8_t capa_code, uint8_t capa_len)
{
int errs = 0;
errs += ibuf_add_n8(opb, capa_code);
errs += ibuf_add_n8(opb, capa_len);
return (errs);
}
int
session_capa_add_mp(struct ibuf *buf, uint8_t aid)
{
uint16_t afi;
uint8_t safi;
int errs = 0;
if (aid2afi(aid, &afi, &safi) == -1) {
log_warn("%s: bad AID", __func__);
return (-1);
}
errs += ibuf_add_n16(buf, afi);
errs += ibuf_add_zero(buf, 1);
errs += ibuf_add_n8(buf, safi);
return (errs);
}
int
session_capa_add_afi(struct ibuf *b, uint8_t aid, uint8_t flags)
{
u_int errs = 0;
uint16_t afi;
uint8_t safi;
if (aid2afi(aid, &afi, &safi)) {
log_warn("%s: bad AID", __func__);
return (-1);
}
errs += ibuf_add_n16(b, afi);
errs += ibuf_add_n8(b, safi);
errs += ibuf_add_n8(b, flags);
return (errs);
}
struct ibuf *
session_newmsg(enum msg_type msgtype, uint16_t len)
{
struct ibuf *buf;
int errs = 0;
if ((buf = ibuf_open(len)) == NULL)
return (NULL);
errs += ibuf_add(buf, marker, sizeof(marker));
errs += ibuf_add_n16(buf, len);
errs += ibuf_add_n8(buf, msgtype);
if (errs) {
ibuf_free(buf);
return (NULL);
}
return (buf);
}
void
session_sendmsg(struct ibuf *msg, struct peer *p, enum msg_type msgtype)
{
struct mrt *mrt;
LIST_FOREACH(mrt, &mrthead, entry) {
if (!(mrt->type == MRT_ALL_OUT || (msgtype == UPDATE &&
mrt->type == MRT_UPDATE_OUT)))
continue;
if ((mrt->peer_id == 0 && mrt->group_id == 0) ||
mrt->peer_id == p->conf.id || (mrt->group_id != 0 &&
mrt->group_id == p->conf.groupid))
mrt_dump_bgp_msg(mrt, msg, p, msgtype);
}
ibuf_close(p->wbuf, msg);
if (!p->throttled && msgbuf_queuelen(p->wbuf) > SESS_MSG_HIGH_MARK) {
if (imsg_rde(IMSG_XOFF, p->conf.id, NULL, 0) == -1)
log_peer_warn(&p->conf, "imsg_compose XOFF");
else
p->throttled = 1;
}
}
/*
* Translate between internal roles and the value expected by RFC 9234.
*/
static uint8_t
role2capa(enum role role)
{
switch (role) {
case ROLE_CUSTOMER:
return CAPA_ROLE_CUSTOMER;
case ROLE_PROVIDER:
return CAPA_ROLE_PROVIDER;
case ROLE_RS:
return CAPA_ROLE_RS;
case ROLE_RS_CLIENT:
return CAPA_ROLE_RS_CLIENT;
case ROLE_PEER:
return CAPA_ROLE_PEER;
default:
fatalx("Unsupported role for role capability");
}
}
static enum role
capa2role(uint8_t val)
{
switch (val) {
case CAPA_ROLE_PROVIDER:
return ROLE_PROVIDER;
case CAPA_ROLE_RS:
return ROLE_RS;
case CAPA_ROLE_RS_CLIENT:
return ROLE_RS_CLIENT;
case CAPA_ROLE_CUSTOMER:
return ROLE_CUSTOMER;
case CAPA_ROLE_PEER:
return ROLE_PEER;
default:
return ROLE_NONE;
}
}
void
session_open(struct peer *p)
{
struct ibuf *buf, *opb;
size_t len, optparamlen;
uint16_t holdtime;
uint8_t i;
int errs = 0, extlen = 0;
int mpcapa = 0;
if ((opb = ibuf_dynamic(0, UINT16_MAX - 3)) == NULL) {
bgp_fsm(p, EVNT_CON_FATAL, NULL);
return;
}
/* multiprotocol extensions, RFC 4760 */
for (i = AID_MIN; i < AID_MAX; i++)
if (p->capa.ann.mp[i]) { /* 4 bytes data */
errs += session_capa_add(opb, CAPA_MP, 4);
errs += session_capa_add_mp(opb, i);
mpcapa++;
}
/* route refresh, RFC 2918 */
if (p->capa.ann.refresh) /* no data */
errs += session_capa_add(opb, CAPA_REFRESH, 0);
/* BGP open policy, RFC 9234, only for ebgp sessions */
if (p->conf.ebgp && p->capa.ann.policy &&
p->conf.role != ROLE_NONE &&
(p->capa.ann.mp[AID_INET] || p->capa.ann.mp[AID_INET6] ||
mpcapa == 0)) {
errs += session_capa_add(opb, CAPA_ROLE, 1);
errs += ibuf_add_n8(opb, role2capa(p->conf.role));
}
/* graceful restart and End-of-RIB marker, RFC 4724 */
if (p->capa.ann.grestart.restart) {
int rst = 0;
uint16_t hdr = 0;
for (i = AID_MIN; i < AID_MAX; i++) {
if (p->capa.neg.grestart.flags[i] & CAPA_GR_RESTARTING)
rst++;
}
/* Only set the R-flag if no graceful restart is ongoing */
if (!rst)
hdr |= CAPA_GR_R_FLAG;
errs += session_capa_add(opb, CAPA_RESTART, sizeof(hdr));
errs += ibuf_add_n16(opb, hdr);
}
/* 4-bytes AS numbers, RFC6793 */
if (p->capa.ann.as4byte) { /* 4 bytes data */
errs += session_capa_add(opb, CAPA_AS4BYTE, sizeof(uint32_t));
errs += ibuf_add_n32(opb, p->conf.local_as);
}
/* advertisement of multiple paths, RFC7911 */
if (p->capa.ann.add_path[AID_MIN]) { /* variable */
uint8_t aplen;
if (mpcapa)
aplen = 4 * mpcapa;
else /* AID_INET */
aplen = 4;
errs += session_capa_add(opb, CAPA_ADD_PATH, aplen);
if (mpcapa) {
for (i = AID_MIN; i < AID_MAX; i++) {
if (p->capa.ann.mp[i]) {
errs += session_capa_add_afi(opb,
i, p->capa.ann.add_path[i] &
CAPA_AP_MASK);
}
}
} else { /* AID_INET */
errs += session_capa_add_afi(opb, AID_INET,
p->capa.ann.add_path[AID_INET] & CAPA_AP_MASK);
}
}
/* enhanced route-refresh, RFC7313 */
if (p->capa.ann.enhanced_rr) /* no data */
errs += session_capa_add(opb, CAPA_ENHANCED_RR, 0);
if (errs) {
ibuf_free(opb);
bgp_fsm(p, EVNT_CON_FATAL, NULL);
return;
}
optparamlen = ibuf_size(opb);
len = MSGSIZE_OPEN_MIN + optparamlen;
if (optparamlen == 0) {
/* nothing */
} else if (optparamlen + 2 >= 255) {
/* RFC9072: use 255 as magic size and request extra header */
optparamlen = 255;
extlen = 1;
/* 3 byte OPT_PARAM_EXT_LEN and OPT_PARAM_CAPABILITIES */
len += 2 * 3;
} else {
/* regular capabilities header */
optparamlen += 2;
len += 2;
}
if ((buf = session_newmsg(OPEN, len)) == NULL) {
ibuf_free(opb);
bgp_fsm(p, EVNT_CON_FATAL, NULL);
return;
}
if (p->conf.holdtime)
holdtime = p->conf.holdtime;
else
holdtime = conf->holdtime;
errs += ibuf_add_n8(buf, 4);
errs += ibuf_add_n16(buf, p->conf.local_short_as);
errs += ibuf_add_n16(buf, holdtime);
/* is already in network byte order */
errs += ibuf_add_n32(buf, conf->bgpid);
errs += ibuf_add_n8(buf, optparamlen);
if (extlen) {
/* RFC9072 extra header which spans over the capabilities hdr */
errs += ibuf_add_n8(buf, OPT_PARAM_EXT_LEN);
errs += ibuf_add_n16(buf, ibuf_size(opb) + 1 + 2);
}
if (optparamlen) {
errs += ibuf_add_n8(buf, OPT_PARAM_CAPABILITIES);
if (extlen) {
/* RFC9072: 2-byte extended length */
errs += ibuf_add_n16(buf, ibuf_size(opb));
} else {
errs += ibuf_add_n8(buf, ibuf_size(opb));
}
errs += ibuf_add_ibuf(buf, opb);
}
ibuf_free(opb);
if (errs) {
ibuf_free(buf);
bgp_fsm(p, EVNT_CON_FATAL, NULL);
return;
}
session_sendmsg(buf, p, OPEN);
p->stats.msg_sent_open++;
}
void
session_keepalive(struct peer *p)
{
struct ibuf *buf;
if ((buf = session_newmsg(KEEPALIVE, MSGSIZE_KEEPALIVE)) == NULL) {
bgp_fsm(p, EVNT_CON_FATAL, NULL);
return;
}
session_sendmsg(buf, p, KEEPALIVE);
start_timer_keepalive(p);
p->stats.msg_sent_keepalive++;
}
void
session_update(uint32_t peerid, struct ibuf *ibuf)
{
struct peer *p;
struct ibuf *buf;
if ((p = getpeerbyid(conf, peerid)) == NULL) {
log_warnx("no such peer: id=%u", peerid);
return;
}
if (p->state != STATE_ESTABLISHED)
return;
if ((buf = session_newmsg(UPDATE, MSGSIZE_HEADER + ibuf_size(ibuf))) ==
NULL) {
bgp_fsm(p, EVNT_CON_FATAL, NULL);
return;
}
if (ibuf_add_ibuf(buf, ibuf)) {
ibuf_free(buf);
bgp_fsm(p, EVNT_CON_FATAL, NULL);
return;
}
session_sendmsg(buf, p, UPDATE);
start_timer_keepalive(p);
p->stats.msg_sent_update++;
}
void
session_notification_data(struct peer *p, uint8_t errcode, uint8_t subcode,
void *data, size_t datalen)
{
struct ibuf ibuf;
ibuf_from_buffer(&ibuf, data, datalen);
session_notification(p, errcode, subcode, &ibuf);
}
void
session_notification(struct peer *p, uint8_t errcode, uint8_t subcode,
struct ibuf *ibuf)
{
struct ibuf *buf;
int errs = 0;
size_t datalen = 0;
switch (p->state) {
case STATE_OPENSENT:
case STATE_OPENCONFIRM:
case STATE_ESTABLISHED:
break;
default:
/* session not open, no need to send notification */
log_notification(p, errcode, subcode, ibuf, "dropping");
return;
}
log_notification(p, errcode, subcode, ibuf, "sending");
/* cap to maximum size */
if (ibuf != NULL) {
if (ibuf_size(ibuf) >
MAX_PKTSIZE - MSGSIZE_NOTIFICATION_MIN) {
log_peer_warnx(&p->conf,
"oversized notification, data trunkated");
ibuf_truncate(ibuf, MAX_PKTSIZE -
MSGSIZE_NOTIFICATION_MIN);
}
datalen = ibuf_size(ibuf);
}
if ((buf = session_newmsg(NOTIFICATION,
MSGSIZE_NOTIFICATION_MIN + datalen)) == NULL) {
bgp_fsm(p, EVNT_CON_FATAL, NULL);
return;
}
errs += ibuf_add_n8(buf, errcode);
errs += ibuf_add_n8(buf, subcode);
if (ibuf != NULL)
errs += ibuf_add_ibuf(buf, ibuf);
if (errs) {
ibuf_free(buf);
bgp_fsm(p, EVNT_CON_FATAL, NULL);
return;
}
session_sendmsg(buf, p, NOTIFICATION);
p->stats.msg_sent_notification++;
p->stats.last_sent_errcode = errcode;
p->stats.last_sent_suberr = subcode;
}
int
session_neighbor_rrefresh(struct peer *p)
{
uint8_t i;
if (!(p->capa.neg.refresh || p->capa.neg.enhanced_rr))
return (-1);
for (i = AID_MIN; i < AID_MAX; i++) {
if (p->capa.neg.mp[i] != 0)
session_rrefresh(p, i, ROUTE_REFRESH_REQUEST);
}
return (0);
}
void
session_rrefresh(struct peer *p, uint8_t aid, uint8_t subtype)
{
struct ibuf *buf;
int errs = 0;
uint16_t afi;
uint8_t safi;
switch (subtype) {
case ROUTE_REFRESH_REQUEST:
p->stats.refresh_sent_req++;
break;
case ROUTE_REFRESH_BEGIN_RR:
case ROUTE_REFRESH_END_RR:
/* requires enhanced route refresh */
if (!p->capa.neg.enhanced_rr)
return;
if (subtype == ROUTE_REFRESH_BEGIN_RR)
p->stats.refresh_sent_borr++;
else
p->stats.refresh_sent_eorr++;
break;
default:
fatalx("session_rrefresh: bad subtype %d", subtype);
}
if (aid2afi(aid, &afi, &safi) == -1)
fatalx("session_rrefresh: bad afi/safi pair");
if ((buf = session_newmsg(RREFRESH, MSGSIZE_RREFRESH)) == NULL) {
bgp_fsm(p, EVNT_CON_FATAL, NULL);
return;
}
errs += ibuf_add_n16(buf, afi);
errs += ibuf_add_n8(buf, subtype);
errs += ibuf_add_n8(buf, safi);
if (errs) {
ibuf_free(buf);
bgp_fsm(p, EVNT_CON_FATAL, NULL);
return;
}
session_sendmsg(buf, p, RREFRESH);
p->stats.msg_sent_rrefresh++;
}
int
session_graceful_restart(struct peer *p)
{
uint8_t i;
timer_set(&p->timers, Timer_RestartTimeout,
p->capa.neg.grestart.timeout);
for (i = AID_MIN; i < AID_MAX; i++) {
if (p->capa.neg.grestart.flags[i] & CAPA_GR_PRESENT) {
if (imsg_rde(IMSG_SESSION_STALE, p->conf.id,
&i, sizeof(i)) == -1)
return (-1);
log_peer_warnx(&p->conf,
"graceful restart of %s, keeping routes",
aid2str(i));
p->capa.neg.grestart.flags[i] |= CAPA_GR_RESTARTING;
} else if (p->capa.neg.mp[i]) {
if (imsg_rde(IMSG_SESSION_NOGRACE, p->conf.id,
&i, sizeof(i)) == -1)
return (-1);
log_peer_warnx(&p->conf,
"graceful restart of %s, flushing routes",
aid2str(i));
}
}
return (0);
}
int
session_graceful_stop(struct peer *p)
{
uint8_t i;
for (i = AID_MIN; i < AID_MAX; i++) {
/*
* Only flush if the peer is restarting and the timeout fired.
* In all other cases the session was already flushed when the
* session went down or when the new open message was parsed.
*/
if (p->capa.neg.grestart.flags[i] & CAPA_GR_RESTARTING) {
log_peer_warnx(&p->conf, "graceful restart of %s, "
"time-out, flushing", aid2str(i));
if (imsg_rde(IMSG_SESSION_FLUSH, p->conf.id,
&i, sizeof(i)) == -1)
return (-1);
}
p->capa.neg.grestart.flags[i] &= ~CAPA_GR_RESTARTING;
}
return (0);
}
int
session_dispatch_msg(struct pollfd *pfd, struct peer *p)
{
socklen_t len;
int error;
if (p->state == STATE_CONNECT) {
if (pfd->revents & POLLOUT) {
if (pfd->revents & POLLIN) {
/* error occurred */
len = sizeof(error);
if (getsockopt(pfd->fd, SOL_SOCKET, SO_ERROR,
&error, &len) == -1 || error) {
if (error)
errno = error;
if (errno != p->lasterr) {
log_peer_warn(&p->conf,
"socket error");
p->lasterr = errno;
}
bgp_fsm(p, EVNT_CON_OPENFAIL, NULL);
return (1);
}
}
bgp_fsm(p, EVNT_CON_OPEN, NULL);
return (1);
}
if (pfd->revents & POLLHUP) {
bgp_fsm(p, EVNT_CON_OPENFAIL, NULL);
return (1);
}
if (pfd->revents & (POLLERR|POLLNVAL)) {
bgp_fsm(p, EVNT_CON_FATAL, NULL);
return (1);
}
return (0);
}
if (pfd->revents & POLLHUP) {
bgp_fsm(p, EVNT_CON_CLOSED, NULL);
return (1);
}
if (pfd->revents & (POLLERR|POLLNVAL)) {
bgp_fsm(p, EVNT_CON_FATAL, NULL);
return (1);
}
if (pfd->revents & POLLOUT && msgbuf_queuelen(p->wbuf) > 0) {
if (ibuf_write(p->fd, p->wbuf) == -1) {
if (errno == EPIPE)
log_peer_warnx(&p->conf, "Connection closed");
else
log_peer_warn(&p->conf, "write error");
bgp_fsm(p, EVNT_CON_FATAL, NULL);
return (1);
}
p->stats.last_write = getmonotime();
start_timer_sendholdtime(p);
if (p->throttled &&
msgbuf_queuelen(p->wbuf) < SESS_MSG_LOW_MARK) {
if (imsg_rde(IMSG_XON, p->conf.id, NULL, 0) == -1)
log_peer_warn(&p->conf, "imsg_compose XON");
else
p->throttled = 0;
}
if (!(pfd->revents & POLLIN))
return (1);
}
if (p->fd != -1 && pfd->revents & POLLIN) {
switch (ibuf_read(p->fd, p->wbuf)) {
case -1:
if (p->state == STATE_IDLE)
/* error already handled before */
return (1);
log_peer_warn(&p->conf, "read error");
bgp_fsm(p, EVNT_CON_FATAL, NULL);
return (1);
case 0:
bgp_fsm(p, EVNT_CON_CLOSED, NULL);
return (1);
}
p->stats.last_read = getmonotime();
return (1);
}
return (0);
}
void
session_process_msg(struct peer *p)
{
struct ibuf *msg;
struct mrt *mrt;
int processed = 0;
uint8_t msgtype;
p->rpending = 0;
if (p->wbuf == NULL)
return;
/*
* session might drop to IDLE -> all buffers are flushed
*/
while ((msg = msgbuf_get(p->wbuf)) != NULL) {
/* skip msg header and extract type */
if (ibuf_skip(msg, MSGSIZE_HEADER_MARKER) == -1 ||
ibuf_skip(msg, sizeof(uint16_t)) == -1 ||
ibuf_get_n8(msg, &msgtype) == -1) {
log_peer_warn(&p->conf, "process message failed");
bgp_fsm(p, EVNT_CON_FATAL, NULL);
ibuf_free(msg);
return;
}
ibuf_rewind(msg);
/* dump to MRT as soon as we have a full packet */
LIST_FOREACH(mrt, &mrthead, entry) {
if (!(mrt->type == MRT_ALL_IN || (msgtype == UPDATE &&
mrt->type == MRT_UPDATE_IN)))
continue;
if ((mrt->peer_id == 0 && mrt->group_id == 0) ||
mrt->peer_id == p->conf.id || (mrt->group_id != 0 &&
mrt->group_id == p->conf.groupid))
mrt_dump_bgp_msg(mrt, msg, p, msgtype);
}
ibuf_skip(msg, MSGSIZE_HEADER);
switch (msgtype) {
case OPEN:
bgp_fsm(p, EVNT_RCVD_OPEN, msg);
p->stats.msg_rcvd_open++;
break;
case UPDATE:
bgp_fsm(p, EVNT_RCVD_UPDATE, msg);
p->stats.msg_rcvd_update++;
break;
case NOTIFICATION:
bgp_fsm(p, EVNT_RCVD_NOTIFICATION, msg);
p->stats.msg_rcvd_notification++;
break;
case KEEPALIVE:
bgp_fsm(p, EVNT_RCVD_KEEPALIVE, msg);
p->stats.msg_rcvd_keepalive++;
break;
case RREFRESH:
parse_rrefresh(p, msg);
p->stats.msg_rcvd_rrefresh++;
break;
default: /* cannot happen */
session_notification_data(p, ERR_HEADER, ERR_HDR_TYPE,
&msgtype, 1);
log_peer_warnx(&p->conf,
"received message with unknown type %u", msgtype);
bgp_fsm(p, EVNT_CON_FATAL, NULL);
}
ibuf_free(msg);
if (++processed > MSG_PROCESS_LIMIT) {
p->rpending = 1;
break;
}
}
}
ssize_t
parse_header(struct ibuf *msg, void *arg)
{
struct peer *peer = arg;
u_char m[MSGSIZE_HEADER_MARKER];
uint16_t len;
uint8_t type;
if (ibuf_get(msg, m, sizeof(m)) == -1 ||
ibuf_get_n16(msg, &len) == -1 ||
ibuf_get_n8(msg, &type) == -1)
return (-1);
/* caller MUST make sure we are getting 19 bytes! */
if (memcmp(m, marker, sizeof(marker))) {
log_peer_warnx(&peer->conf, "sync error");
session_notification(peer, ERR_HEADER, ERR_HDR_SYNC, NULL);
bgp_fsm(peer, EVNT_CON_FATAL, NULL);
errno = EINVAL;
return (-1);
}
if (len < MSGSIZE_HEADER || len > MAX_PKTSIZE) {
log_peer_warnx(&peer->conf,
"received message: illegal length: %u byte", len);
goto badlen;
}
switch (type) {
case OPEN:
if (len < MSGSIZE_OPEN_MIN) {
log_peer_warnx(&peer->conf,
"received OPEN: illegal len: %u byte", len);
goto badlen;
}
break;
case NOTIFICATION:
if (len < MSGSIZE_NOTIFICATION_MIN) {
log_peer_warnx(&peer->conf,
"received NOTIFICATION: illegal len: %u byte", len);
goto badlen;
}
break;
case UPDATE:
if (len < MSGSIZE_UPDATE_MIN) {
log_peer_warnx(&peer->conf,
"received UPDATE: illegal len: %u byte", len);
goto badlen;
}
break;
case KEEPALIVE:
if (len != MSGSIZE_KEEPALIVE) {
log_peer_warnx(&peer->conf,
"received KEEPALIVE: illegal len: %u byte", len);
goto badlen;
}
break;
case RREFRESH:
if (len < MSGSIZE_RREFRESH_MIN) {
log_peer_warnx(&peer->conf,
"received RREFRESH: illegal len: %u byte", len);
goto badlen;
}
break;
default:
log_peer_warnx(&peer->conf,
"received msg with unknown type %u", type);
session_notification_data(peer, ERR_HEADER, ERR_HDR_TYPE,
&type, sizeof(type));
bgp_fsm(peer, EVNT_CON_FATAL, NULL);
errno = EINVAL;
return (-1);
}
return (len);
badlen:
len = htons(len);
session_notification_data(peer, ERR_HEADER, ERR_HDR_LEN,
&len, sizeof(len));
bgp_fsm(peer, EVNT_CON_FATAL, NULL);
errno = ERANGE;
return (-1);
}
int
parse_open(struct peer *peer, struct ibuf *msg)
{
uint8_t version, rversion;
uint16_t short_as;
uint16_t holdtime, myholdtime;
uint32_t as, bgpid;
uint8_t optparamlen;
if (ibuf_get_n8(msg, &version) == -1 ||
ibuf_get_n16(msg, &short_as) == -1 ||
ibuf_get_n16(msg, &holdtime) == -1 ||
ibuf_get_n32(msg, &bgpid) == -1 ||
ibuf_get_n8(msg, &optparamlen) == -1)
goto bad_len;
if (version != BGP_VERSION) {
log_peer_warnx(&peer->conf,
"peer wants unrecognized version %u", version);
if (version > BGP_VERSION)
rversion = version - BGP_VERSION;
else
rversion = BGP_VERSION;
session_notification_data(peer, ERR_OPEN, ERR_OPEN_VERSION,
&rversion, sizeof(rversion));
change_state(peer, STATE_IDLE, EVNT_RCVD_OPEN);
return (-1);
}
as = peer->short_as = short_as;
if (as == 0) {
log_peer_warnx(&peer->conf,
"peer requests unacceptable AS %u", as);
session_notification(peer, ERR_OPEN, ERR_OPEN_AS, NULL);
change_state(peer, STATE_IDLE, EVNT_RCVD_OPEN);
return (-1);
}
if (holdtime && holdtime < peer->conf.min_holdtime) {
log_peer_warnx(&peer->conf,
"peer requests unacceptable holdtime %u", holdtime);
session_notification(peer, ERR_OPEN, ERR_OPEN_HOLDTIME, NULL);
change_state(peer, STATE_IDLE, EVNT_RCVD_OPEN);
return (-1);
}
myholdtime = peer->conf.holdtime;
if (!myholdtime)
myholdtime = conf->holdtime;
if (holdtime < myholdtime)
peer->holdtime = holdtime;
else
peer->holdtime = myholdtime;
/* check bgpid for validity - just disallow 0 */
if (bgpid == 0) {
log_peer_warnx(&peer->conf, "peer BGPID 0 unacceptable");
session_notification(peer, ERR_OPEN, ERR_OPEN_BGPID, NULL);
change_state(peer, STATE_IDLE, EVNT_RCVD_OPEN);
return (-1);
}
peer->remote_bgpid = bgpid;
if (optparamlen != 0) {
struct ibuf oparams, op;
uint8_t ext_type, op_type;
uint16_t ext_len, op_len;
ibuf_from_ibuf(&oparams, msg);
/* check for RFC9072 encoding */
if (ibuf_get_n8(&oparams, &ext_type) == -1)
goto bad_len;
if (ext_type == OPT_PARAM_EXT_LEN) {
if (ibuf_get_n16(&oparams, &ext_len) == -1)
goto bad_len;
/* skip RFC9072 header */
if (ibuf_skip(msg, 3) == -1)
goto bad_len;
} else {
ext_len = optparamlen;
ibuf_rewind(&oparams);
}
if (ibuf_truncate(&oparams, ext_len) == -1 ||
ibuf_skip(msg, ext_len) == -1)
goto bad_len;
while (ibuf_size(&oparams) > 0) {
if (ibuf_get_n8(&oparams, &op_type) == -1)
goto bad_len;
if (ext_type == OPT_PARAM_EXT_LEN) {
if (ibuf_get_n16(&oparams, &op_len) == -1)
goto bad_len;
} else {
uint8_t tmp;
if (ibuf_get_n8(&oparams, &tmp) == -1)
goto bad_len;
op_len = tmp;
}
if (ibuf_get_ibuf(&oparams, op_len, &op) == -1)
goto bad_len;
switch (op_type) {
case OPT_PARAM_CAPABILITIES: /* RFC 3392 */
if (parse_capabilities(peer, &op, &as) == -1) {
session_notification(peer, ERR_OPEN, 0,
NULL);
change_state(peer, STATE_IDLE,
EVNT_RCVD_OPEN);
return (-1);
}
break;
case OPT_PARAM_AUTH: /* deprecated */
default:
/*
* unsupported type
* the RFCs tell us to leave the data section
* empty and notify the peer with ERR_OPEN,
* ERR_OPEN_OPT. How the peer should know
* _which_ optional parameter we don't support
* is beyond me.
*/
log_peer_warnx(&peer->conf,
"received OPEN message with unsupported "
"optional parameter: type %u", op_type);
session_notification(peer, ERR_OPEN,
ERR_OPEN_OPT, NULL);
change_state(peer, STATE_IDLE, EVNT_RCVD_OPEN);
return (-1);
}
}
}
if (ibuf_size(msg) != 0) {
bad_len:
log_peer_warnx(&peer->conf,
"corrupt OPEN message received: length mismatch");
session_notification(peer, ERR_OPEN, 0, NULL);
change_state(peer, STATE_IDLE, EVNT_RCVD_OPEN);
return (-1);
}
/* if remote-as is zero and it's a cloned neighbor, accept any */
if (peer->template && !peer->conf.remote_as && as != AS_TRANS) {
peer->conf.remote_as = as;
peer->conf.ebgp = (peer->conf.remote_as != peer->conf.local_as);
if (!peer->conf.ebgp)
/* force enforce_as off for iBGP sessions */
peer->conf.enforce_as = ENFORCE_AS_OFF;
}
if (peer->conf.remote_as != as) {
log_peer_warnx(&peer->conf, "peer sent wrong AS %s",
log_as(as));
session_notification(peer, ERR_OPEN, ERR_OPEN_AS, NULL);
change_state(peer, STATE_IDLE, EVNT_RCVD_OPEN);
return (-1);
}
/* on iBGP sessions check for bgpid collision */
if (!peer->conf.ebgp && peer->remote_bgpid == conf->bgpid) {
struct in_addr ina;
ina.s_addr = htonl(bgpid);
log_peer_warnx(&peer->conf, "peer BGPID %s conflicts with ours",
inet_ntoa(ina));
session_notification(peer, ERR_OPEN, ERR_OPEN_BGPID, NULL);
change_state(peer, STATE_IDLE, EVNT_RCVD_OPEN);
return (-1);
}
if (capa_neg_calc(peer) == -1) {
change_state(peer, STATE_IDLE, EVNT_RCVD_OPEN);
return (-1);
}
return (0);
}
int
parse_update(struct peer *peer, struct ibuf *msg)
{
/*
* we pass the message verbatim to the rde.
* in case of errors the whole session is reset with a
* notification anyway, we only need to know the peer
*/
if (imsg_rde(IMSG_UPDATE, peer->conf.id, ibuf_data(msg),
ibuf_size(msg)) == -1)
return (-1);
return (0);
}
int
parse_rrefresh(struct peer *peer, struct ibuf *msg)
{
struct route_refresh rr;
uint16_t afi, datalen;
uint8_t aid, safi, subtype;
datalen = ibuf_size(msg) + MSGSIZE_HEADER;
if (ibuf_get_n16(msg, &afi) == -1 ||
ibuf_get_n8(msg, &subtype) == -1 ||
ibuf_get_n8(msg, &safi) == -1) {
/* minimum size checked in session_process_msg() */
fatalx("%s: message too small", __func__);
}
/* check subtype if peer announced enhanced route refresh */
if (peer->capa.neg.enhanced_rr) {
switch (subtype) {
case ROUTE_REFRESH_REQUEST:
/* no ORF support, so no oversized RREFRESH msgs */
if (datalen != MSGSIZE_RREFRESH) {
log_peer_warnx(&peer->conf,
"received RREFRESH: illegal len: %u byte",
datalen);
datalen = htons(datalen);
session_notification_data(peer, ERR_HEADER,
ERR_HDR_LEN, &datalen, sizeof(datalen));
bgp_fsm(peer, EVNT_CON_FATAL, NULL);
return (-1);
}
peer->stats.refresh_rcvd_req++;
break;
case ROUTE_REFRESH_BEGIN_RR:
case ROUTE_REFRESH_END_RR:
/* special handling for RFC7313 */
if (datalen != MSGSIZE_RREFRESH) {
log_peer_warnx(&peer->conf,
"received RREFRESH: illegal len: %u byte",
datalen);
ibuf_rewind(msg);
session_notification(peer, ERR_RREFRESH,
ERR_RR_INV_LEN, msg);
bgp_fsm(peer, EVNT_CON_FATAL, NULL);
return (-1);
}
if (subtype == ROUTE_REFRESH_BEGIN_RR)
peer->stats.refresh_rcvd_borr++;
else
peer->stats.refresh_rcvd_eorr++;
break;
default:
log_peer_warnx(&peer->conf, "peer sent bad refresh, "
"bad subtype %d", subtype);
return (0);
}
} else {
/* force subtype to default */
subtype = ROUTE_REFRESH_REQUEST;
peer->stats.refresh_rcvd_req++;
}
/* afi/safi unchecked - unrecognized values will be ignored anyway */
if (afi2aid(afi, safi, &aid) == -1) {
log_peer_warnx(&peer->conf, "peer sent bad refresh, "
"invalid afi/safi pair");
return (0);
}
if (!peer->capa.neg.refresh && !peer->capa.neg.enhanced_rr) {
log_peer_warnx(&peer->conf, "peer sent unexpected refresh");
return (0);
}
rr.aid = aid;
rr.subtype = subtype;
if (imsg_rde(IMSG_REFRESH, peer->conf.id, &rr, sizeof(rr)) == -1)
return (-1);
return (0);
}
void
parse_notification(struct peer *peer, struct ibuf *msg)
{
uint8_t errcode, subcode;
uint8_t reason_len;
if (ibuf_get_n8(msg, &errcode) == -1 ||
ibuf_get_n8(msg, &subcode) == -1) {
log_peer_warnx(&peer->conf, "received bad notification");
goto done;
}
peer->errcnt++;
peer->stats.last_rcvd_errcode = errcode;
peer->stats.last_rcvd_suberr = subcode;
log_notification(peer, errcode, subcode, msg, "received");
CTASSERT(sizeof(peer->stats.last_reason) > UINT8_MAX);
memset(peer->stats.last_reason, 0, sizeof(peer->stats.last_reason));
if (errcode == ERR_CEASE &&
(subcode == ERR_CEASE_ADMIN_DOWN ||
subcode == ERR_CEASE_ADMIN_RESET)) {
/* check if shutdown reason is included */
if (ibuf_get_n8(msg, &reason_len) != -1 && reason_len != 0) {
if (ibuf_get(msg, peer->stats.last_reason,
reason_len) == -1)
log_peer_warnx(&peer->conf,
"received truncated shutdown reason");
}
}
done:
change_state(peer, STATE_IDLE, EVNT_RCVD_NOTIFICATION);
}
int
parse_capabilities(struct peer *peer, struct ibuf *buf, uint32_t *as)
{
struct ibuf capabuf;
uint16_t afi, gr_header;
uint8_t capa_code, capa_len;
uint8_t safi, aid, role, flags;
while (ibuf_size(buf) > 0) {
if (ibuf_get_n8(buf, &capa_code) == -1 ||
ibuf_get_n8(buf, &capa_len) == -1) {
log_peer_warnx(&peer->conf, "Bad capabilities attr "
"length: too short");
return (-1);
}
if (ibuf_get_ibuf(buf, capa_len, &capabuf) == -1) {
log_peer_warnx(&peer->conf,
"Received bad capabilities attr length: "
"len %zu smaller than capa_len %u",
ibuf_size(buf), capa_len);
return (-1);
}
switch (capa_code) {
case CAPA_MP: /* RFC 4760 */
if (capa_len != 4 ||
ibuf_get_n16(&capabuf, &afi) == -1 ||
ibuf_skip(&capabuf, 1) == -1 ||
ibuf_get_n8(&capabuf, &safi) == -1) {
log_peer_warnx(&peer->conf,
"Received bad multi protocol capability");
break;
}
if (afi2aid(afi, safi, &aid) == -1) {
log_peer_warnx(&peer->conf,
"Received multi protocol capability: "
" unknown AFI %u, safi %u pair",
afi, safi);
peer->capa.peer.mp[AID_UNSPEC] = 1;
break;
}
peer->capa.peer.mp[aid] = 1;
break;
case CAPA_REFRESH:
peer->capa.peer.refresh = 1;
break;
case CAPA_ROLE:
if (capa_len != 1 ||
ibuf_get_n8(&capabuf, &role) == -1) {
log_peer_warnx(&peer->conf,
"Received bad role capability");
break;
}
if (!peer->conf.ebgp) {
log_peer_warnx(&peer->conf,
"Received role capability on iBGP session");
break;
}
peer->capa.peer.policy = 1;
peer->remote_role = capa2role(role);
break;
case CAPA_RESTART:
if (capa_len == 2) {
/* peer only supports EoR marker */
peer->capa.peer.grestart.restart = 1;
peer->capa.peer.grestart.timeout = 0;
break;
} else if (capa_len % 4 != 2) {
log_peer_warnx(&peer->conf,
"Bad graceful restart capability");
peer->capa.peer.grestart.restart = 0;
peer->capa.peer.grestart.timeout = 0;
break;
}
if (ibuf_get_n16(&capabuf, &gr_header) == -1) {
bad_gr_restart:
log_peer_warnx(&peer->conf,
"Bad graceful restart capability");
peer->capa.peer.grestart.restart = 0;
peer->capa.peer.grestart.timeout = 0;
break;
}
peer->capa.peer.grestart.timeout =
gr_header & CAPA_GR_TIMEMASK;
if (peer->capa.peer.grestart.timeout == 0) {
log_peer_warnx(&peer->conf, "Received "
"graceful restart with zero timeout");
peer->capa.peer.grestart.restart = 0;
break;
}
while (ibuf_size(&capabuf) > 0) {
if (ibuf_get_n16(&capabuf, &afi) == -1 ||
ibuf_get_n8(&capabuf, &safi) == -1 ||
ibuf_get_n8(&capabuf, &flags) == -1)
goto bad_gr_restart;
if (afi2aid(afi, safi, &aid) == -1) {
log_peer_warnx(&peer->conf,
"Received graceful restart capa: "
" unknown AFI %u, safi %u pair",
afi, safi);
continue;
}
peer->capa.peer.grestart.flags[aid] |=
CAPA_GR_PRESENT;
if (flags & CAPA_GR_F_FLAG)
peer->capa.peer.grestart.flags[aid] |=
CAPA_GR_FORWARD;
if (gr_header & CAPA_GR_R_FLAG)
peer->capa.peer.grestart.flags[aid] |=
CAPA_GR_RESTART;
peer->capa.peer.grestart.restart = 2;
}
break;
case CAPA_AS4BYTE:
if (capa_len != 4 ||
ibuf_get_n32(&capabuf, as) == -1) {
log_peer_warnx(&peer->conf,
"Received bad AS4BYTE capability");
peer->capa.peer.as4byte = 0;
break;
}
if (*as == 0) {
log_peer_warnx(&peer->conf,
"peer requests unacceptable AS %u", *as);
session_notification(peer, ERR_OPEN,
ERR_OPEN_AS, NULL);
change_state(peer, STATE_IDLE, EVNT_RCVD_OPEN);
return (-1);
}
peer->capa.peer.as4byte = 1;
break;
case CAPA_ADD_PATH:
if (capa_len % 4 != 0) {
bad_add_path:
log_peer_warnx(&peer->conf,
"Received bad ADD-PATH capability");
memset(peer->capa.peer.add_path, 0,
sizeof(peer->capa.peer.add_path));
break;
}
while (ibuf_size(&capabuf) > 0) {
if (ibuf_get_n16(&capabuf, &afi) == -1 ||
ibuf_get_n8(&capabuf, &safi) == -1 ||
ibuf_get_n8(&capabuf, &flags) == -1)
goto bad_add_path;
if (afi2aid(afi, safi, &aid) == -1) {
log_peer_warnx(&peer->conf,
"Received ADD-PATH capa: "
" unknown AFI %u, safi %u pair",
afi, safi);
memset(peer->capa.peer.add_path, 0,
sizeof(peer->capa.peer.add_path));
break;
}
if (flags & ~CAPA_AP_BIDIR) {
log_peer_warnx(&peer->conf,
"Received ADD-PATH capa: "
" bad flags %x", flags);
memset(peer->capa.peer.add_path, 0,
sizeof(peer->capa.peer.add_path));
break;
}
peer->capa.peer.add_path[aid] = flags;
}
break;
case CAPA_ENHANCED_RR:
peer->capa.peer.enhanced_rr = 1;
break;
default:
break;
}
}
return (0);
}
int
capa_neg_calc(struct peer *p)
{
struct ibuf *ebuf;
uint8_t i, hasmp = 0, capa_code, capa_len, capa_aid = 0;
/* a capability is accepted only if both sides announced it */
p->capa.neg.refresh =
(p->capa.ann.refresh && p->capa.peer.refresh) != 0;
p->capa.neg.enhanced_rr =
(p->capa.ann.enhanced_rr && p->capa.peer.enhanced_rr) != 0;
p->capa.neg.as4byte =
(p->capa.ann.as4byte && p->capa.peer.as4byte) != 0;
/* MP: both side must agree on the AFI,SAFI pair */
if (p->capa.peer.mp[AID_UNSPEC])
hasmp = 1;
for (i = AID_MIN; i < AID_MAX; i++) {
if (p->capa.ann.mp[i] && p->capa.peer.mp[i])
p->capa.neg.mp[i] = 1;
else
p->capa.neg.mp[i] = 0;
if (p->capa.ann.mp[i] || p->capa.peer.mp[i])
hasmp = 1;
}
/* if no MP capability present default to IPv4 unicast mode */
if (!hasmp)
p->capa.neg.mp[AID_INET] = 1;
/*
* graceful restart: the peer capabilities are of interest here.
* It is necessary to compare the new values with the previous ones
* and act accordingly. AFI/SAFI that are not part in the MP capability
* are treated as not being present.
* Also make sure that a flush happens if the session stopped
* supporting graceful restart.
*/
for (i = AID_MIN; i < AID_MAX; i++) {
int8_t negflags;
/* disable GR if the AFI/SAFI is not present */
if ((p->capa.peer.grestart.flags[i] & CAPA_GR_PRESENT &&
p->capa.neg.mp[i] == 0))
p->capa.peer.grestart.flags[i] = 0; /* disable */
/* look at current GR state and decide what to do */
negflags = p->capa.neg.grestart.flags[i];
p->capa.neg.grestart.flags[i] = p->capa.peer.grestart.flags[i];
if (negflags & CAPA_GR_RESTARTING) {
if (p->capa.ann.grestart.restart != 0 &&
p->capa.peer.grestart.flags[i] & CAPA_GR_FORWARD) {
p->capa.neg.grestart.flags[i] |=
CAPA_GR_RESTARTING;
} else {
if (imsg_rde(IMSG_SESSION_FLUSH, p->conf.id,
&i, sizeof(i)) == -1) {
log_peer_warnx(&p->conf,
"imsg send failed");
return (-1);
}
log_peer_warnx(&p->conf, "graceful restart of "
"%s, not restarted, flushing", aid2str(i));
}
}
}
p->capa.neg.grestart.timeout = p->capa.peer.grestart.timeout;
p->capa.neg.grestart.restart = p->capa.peer.grestart.restart;
if (p->capa.ann.grestart.restart == 0)
p->capa.neg.grestart.restart = 0;
/*
* ADD-PATH: set only those bits where both sides agree.
* For this compare our send bit with the recv bit from the peer
* and vice versa.
* The flags are stored from this systems view point.
* At index 0 the flags are set if any per-AID flag is set.
*/
memset(p->capa.neg.add_path, 0, sizeof(p->capa.neg.add_path));
for (i = AID_MIN; i < AID_MAX; i++) {
if (p->capa.neg.mp[i] == 0)
continue;
if ((p->capa.ann.add_path[i] & CAPA_AP_RECV) &&
(p->capa.peer.add_path[i] & CAPA_AP_SEND)) {
p->capa.neg.add_path[i] |= CAPA_AP_RECV;
p->capa.neg.add_path[0] |= CAPA_AP_RECV;
}
if ((p->capa.ann.add_path[i] & CAPA_AP_SEND) &&
(p->capa.peer.add_path[i] & CAPA_AP_RECV)) {
p->capa.neg.add_path[i] |= CAPA_AP_SEND;
p->capa.neg.add_path[0] |= CAPA_AP_SEND;
}
}
/*
* Open policy: check that the policy is sensible.
*
* Make sure that the roles match and set the negotiated capability
* to the role of the peer. So the RDE can inject the OTC attribute.
* See RFC 9234, section 4.2.
* These checks should only happen on ebgp sessions.
*/
if (p->capa.ann.policy != 0 && p->capa.peer.policy != 0 &&
p->conf.ebgp) {
switch (p->conf.role) {
case ROLE_PROVIDER:
if (p->remote_role != ROLE_CUSTOMER)
goto policyfail;
break;
case ROLE_RS:
if (p->remote_role != ROLE_RS_CLIENT)
goto policyfail;
break;
case ROLE_RS_CLIENT:
if (p->remote_role != ROLE_RS)
goto policyfail;
break;
case ROLE_CUSTOMER:
if (p->remote_role != ROLE_PROVIDER)
goto policyfail;
break;
case ROLE_PEER:
if (p->remote_role != ROLE_PEER)
goto policyfail;
break;
default:
policyfail:
log_peer_warnx(&p->conf, "open policy role mismatch: "
"our role %s, their role %s",
log_policy(p->conf.role),
log_policy(p->remote_role));
session_notification(p, ERR_OPEN, ERR_OPEN_ROLE, NULL);
return (-1);
}
p->capa.neg.policy = 1;
}
/* enforce presence of open policy role capability */
if (p->capa.ann.policy == 2 && p->capa.peer.policy == 0 &&
p->conf.ebgp) {
log_peer_warnx(&p->conf, "open policy role enforced but "
"not present");
session_notification(p, ERR_OPEN, ERR_OPEN_ROLE, NULL);
return (-1);
}
/* enforce presence of other capabilities */
if (p->capa.ann.refresh == 2 && p->capa.neg.refresh == 0) {
capa_code = CAPA_REFRESH;
capa_len = 0;
goto fail;
}
if (p->capa.ann.enhanced_rr == 2 && p->capa.neg.enhanced_rr == 0) {
capa_code = CAPA_ENHANCED_RR;
capa_len = 0;
goto fail;
}
if (p->capa.ann.as4byte == 2 && p->capa.neg.as4byte == 0) {
capa_code = CAPA_AS4BYTE;
capa_len = 4;
goto fail;
}
if (p->capa.ann.grestart.restart == 2 &&
p->capa.neg.grestart.restart == 0) {
capa_code = CAPA_RESTART;
capa_len = 2;
goto fail;
}
for (i = AID_MIN; i < AID_MAX; i++) {
if (p->capa.ann.mp[i] == 2 && p->capa.neg.mp[i] == 0) {
capa_code = CAPA_MP;
capa_len = 4;
capa_aid = i;
goto fail;
}
}
for (i = AID_MIN; i < AID_MAX; i++) {
if (p->capa.neg.mp[i] == 0)
continue;
if ((p->capa.ann.add_path[i] & CAPA_AP_RECV_ENFORCE) &&
(p->capa.neg.add_path[i] & CAPA_AP_RECV) == 0) {
capa_code = CAPA_ADD_PATH;
capa_len = 4;
capa_aid = i;
goto fail;
}
if ((p->capa.ann.add_path[i] & CAPA_AP_SEND_ENFORCE) &&
(p->capa.neg.add_path[i] & CAPA_AP_SEND) == 0) {
capa_code = CAPA_ADD_PATH;
capa_len = 4;
capa_aid = i;
goto fail;
}
}
return (0);
fail:
if ((ebuf = ibuf_dynamic(2, 256)) == NULL)
return (-1);
/* best effort, no problem if it fails */
session_capa_add(ebuf, capa_code, capa_len);
if (capa_code == CAPA_MP)
session_capa_add_mp(ebuf, capa_aid);
else if (capa_code == CAPA_ADD_PATH)
session_capa_add_afi(ebuf, capa_aid, 0);
else if (capa_len > 0)
ibuf_add_zero(ebuf, capa_len);
session_notification(p, ERR_OPEN, ERR_OPEN_CAPA, ebuf);
ibuf_free(ebuf);
return (-1);
}
void
session_dispatch_imsg(struct imsgbuf *imsgbuf, int idx, u_int *listener_cnt)
{
struct imsg imsg;
struct ibuf ibuf;
struct mrt xmrt;
struct route_refresh rr;
struct mrt *mrt;
struct imsgbuf *i;
struct peer *p;
struct listen_addr *la, *next, nla;
struct session_dependon sdon;
struct bgpd_config tconf;
size_t len;
uint32_t peerid;
int n, fd, depend_ok, restricted;
uint16_t t;
uint8_t aid, errcode, subcode;
while (imsgbuf) {
if ((n = imsg_get(imsgbuf, &imsg)) == -1)
fatal("session_dispatch_imsg: imsg_get error");
if (n == 0)
break;
peerid = imsg_get_id(&imsg);
switch (imsg_get_type(&imsg)) {
case IMSG_SOCKET_CONN:
case IMSG_SOCKET_CONN_CTL:
if (idx != PFD_PIPE_MAIN)
fatalx("reconf request not from parent");
if ((fd = imsg_get_fd(&imsg)) == -1) {
log_warnx("expected to receive imsg fd to "
"RDE but didn't receive any");
break;
}
if ((i = malloc(sizeof(struct imsgbuf))) == NULL)
fatal(NULL);
if (imsgbuf_init(i, fd) == -1)
fatal(NULL);
if (imsg_get_type(&imsg) == IMSG_SOCKET_CONN) {
if (ibuf_rde) {
log_warnx("Unexpected imsg connection "
"to RDE received");
imsgbuf_clear(ibuf_rde);
free(ibuf_rde);
}
ibuf_rde = i;
} else {
if (ibuf_rde_ctl) {
log_warnx("Unexpected imsg ctl "
"connection to RDE received");
imsgbuf_clear(ibuf_rde_ctl);
free(ibuf_rde_ctl);
}
ibuf_rde_ctl = i;
}
break;
case IMSG_RECONF_CONF:
if (idx != PFD_PIPE_MAIN)
fatalx("reconf request not from parent");
if (imsg_get_data(&imsg, &tconf, sizeof(tconf)) == -1)
fatal("imsg_get_data");
nconf = new_config();
copy_config(nconf, &tconf);
pending_reconf = 1;
break;
case IMSG_RECONF_PEER:
if (idx != PFD_PIPE_MAIN)
fatalx("reconf request not from parent");
if ((p = calloc(1, sizeof(struct peer))) == NULL)
fatal("new_peer");
if (imsg_get_data(&imsg, &p->conf, sizeof(p->conf)) ==
-1)
fatal("imsg_get_data");
p->state = p->prev_state = STATE_NONE;
p->reconf_action = RECONF_REINIT;
if (RB_INSERT(peer_head, &nconf->peers, p) != NULL)
fatalx("%s: peer tree is corrupt", __func__);
break;
case IMSG_RECONF_PEER_AUTH:
if (idx != PFD_PIPE_MAIN)
fatalx("reconf request not from parent");
if ((p = getpeerbyid(nconf, peerid)) == NULL) {
log_warnx("no such peer: id=%u", peerid);
break;
}
if (pfkey_recv_conf(p, &imsg) == -1)
fatal("pfkey_recv_conf");
break;
case IMSG_RECONF_LISTENER:
if (idx != PFD_PIPE_MAIN)
fatalx("reconf request not from parent");
if (nconf == NULL)
fatalx("IMSG_RECONF_LISTENER but no config");
if (imsg_get_data(&imsg, &nla, sizeof(nla)) == -1)
fatal("imsg_get_data");
TAILQ_FOREACH(la, conf->listen_addrs, entry)
if (!la_cmp(la, &nla))
break;
if (la == NULL) {
if (nla.reconf != RECONF_REINIT)
fatalx("king bula sez: "
"expected REINIT");
if ((nla.fd = imsg_get_fd(&imsg)) == -1)
log_warnx("expected to receive fd for "
"%s but didn't receive any",
log_sockaddr((struct sockaddr *)
&nla.sa, nla.sa_len));
la = calloc(1, sizeof(struct listen_addr));
if (la == NULL)
fatal(NULL);
memcpy(&la->sa, &nla.sa, sizeof(la->sa));
la->flags = nla.flags;
la->fd = nla.fd;
la->reconf = RECONF_REINIT;
TAILQ_INSERT_TAIL(nconf->listen_addrs, la,
entry);
} else {
if (nla.reconf != RECONF_KEEP)
fatalx("king bula sez: expected KEEP");
la->reconf = RECONF_KEEP;
}
break;
case IMSG_RECONF_CTRL:
if (idx != PFD_PIPE_MAIN)
fatalx("reconf request not from parent");
if (imsg_get_data(&imsg, &restricted,
sizeof(restricted)) == -1)
fatal("imsg_get_data");
if ((fd = imsg_get_fd(&imsg)) == -1) {
log_warnx("expected to receive fd for control "
"socket but didn't receive any");
break;
}
if (restricted) {
control_shutdown(rcsock);
rcsock = fd;
} else {
control_shutdown(csock);
csock = fd;
}
break;
case IMSG_RECONF_DRAIN:
switch (idx) {
case PFD_PIPE_ROUTE:
if (nconf != NULL)
fatalx("got unexpected %s from RDE",
"IMSG_RECONF_DONE");
imsg_compose(ibuf_main, IMSG_RECONF_DONE, 0, 0,
-1, NULL, 0);
break;
case PFD_PIPE_MAIN:
if (nconf == NULL)
fatalx("got unexpected %s from parent",
"IMSG_RECONF_DONE");
imsg_compose(ibuf_main, IMSG_RECONF_DRAIN, 0, 0,
-1, NULL, 0);
break;
default:
fatalx("reconf request not from parent or RDE");
}
break;
case IMSG_RECONF_DONE:
if (idx != PFD_PIPE_MAIN)
fatalx("reconf request not from parent");
if (nconf == NULL)
fatalx("got IMSG_RECONF_DONE but no config");
copy_config(conf, nconf);
merge_peers(conf, nconf);
/* delete old listeners */
TAILQ_FOREACH_SAFE(la, conf->listen_addrs, entry,
next) {
if (la->reconf == RECONF_NONE) {
log_info("not listening on %s any more",
log_sockaddr((struct sockaddr *)
&la->sa, la->sa_len));
TAILQ_REMOVE(conf->listen_addrs, la,
entry);
close(la->fd);
free(la);
}
}
/* add new listeners */
TAILQ_CONCAT(conf->listen_addrs, nconf->listen_addrs,
entry);
setup_listeners(listener_cnt);
free_config(nconf);
nconf = NULL;
pending_reconf = 0;
log_info("SE reconfigured");
/*
* IMSG_RECONF_DONE is sent when the RDE drained
* the peer config sent in merge_peers().
*/
break;
case IMSG_SESSION_DEPENDON:
if (idx != PFD_PIPE_MAIN)
fatalx("IFINFO message not from parent");
if (imsg_get_data(&imsg, &sdon, sizeof(sdon)) == -1)
fatalx("DEPENDON imsg with wrong len");
depend_ok = sdon.depend_state;
RB_FOREACH(p, peer_head, &conf->peers)
if (!strcmp(p->conf.if_depend, sdon.ifname)) {
if (depend_ok && !p->depend_ok) {
p->depend_ok = depend_ok;
bgp_fsm(p, EVNT_START, NULL);
} else if (!depend_ok && p->depend_ok) {
p->depend_ok = depend_ok;
session_stop(p,
ERR_CEASE_OTHER_CHANGE,
NULL);
}
}
break;
case IMSG_MRT_OPEN:
case IMSG_MRT_REOPEN:
if (idx != PFD_PIPE_MAIN)
fatalx("mrt request not from parent");
if (imsg_get_data(&imsg, &xmrt, sizeof(xmrt)) == -1) {
log_warnx("mrt open, wrong imsg len");
break;
}
if ((xmrt.fd = imsg_get_fd(&imsg)) == -1) {
log_warnx("expected to receive fd for mrt dump "
"but didn't receive any");
break;
}
mrt = mrt_get(&mrthead, &xmrt);
if (mrt == NULL) {
/* new dump */
mrt = calloc(1, sizeof(struct mrt));
if (mrt == NULL)
fatal("session_dispatch_imsg");
memcpy(mrt, &xmrt, sizeof(struct mrt));
if ((mrt->wbuf = msgbuf_new()) == NULL)
fatal("session_dispatch_imsg");
LIST_INSERT_HEAD(&mrthead, mrt, entry);
} else {
/* old dump reopened */
close(mrt->fd);
}
mrt->fd = xmrt.fd;
break;
case IMSG_MRT_CLOSE:
if (idx != PFD_PIPE_MAIN)
fatalx("mrt request not from parent");
if (imsg_get_data(&imsg, &xmrt, sizeof(xmrt)) == -1) {
log_warnx("mrt close, wrong imsg len");
break;
}
mrt = mrt_get(&mrthead, &xmrt);
if (mrt != NULL)
mrt_done(mrt);
break;
case IMSG_CTL_KROUTE:
case IMSG_CTL_KROUTE_ADDR:
case IMSG_CTL_SHOW_NEXTHOP:
case IMSG_CTL_SHOW_INTERFACE:
case IMSG_CTL_SHOW_FIB_TABLES:
case IMSG_CTL_SHOW_RTR:
case IMSG_CTL_SHOW_TIMER:
if (idx != PFD_PIPE_MAIN)
fatalx("ctl kroute request not from parent");
control_imsg_relay(&imsg, NULL);
break;
case IMSG_CTL_SHOW_NEIGHBOR:
if (idx != PFD_PIPE_ROUTE_CTL)
fatalx("ctl rib request not from RDE");
p = getpeerbyid(conf, peerid);
control_imsg_relay(&imsg, p);
break;
case IMSG_CTL_SHOW_RIB:
case IMSG_CTL_SHOW_RIB_PREFIX:
case IMSG_CTL_SHOW_RIB_COMMUNITIES:
case IMSG_CTL_SHOW_RIB_ATTR:
case IMSG_CTL_SHOW_RIB_MEM:
case IMSG_CTL_SHOW_NETWORK:
case IMSG_CTL_SHOW_FLOWSPEC:
case IMSG_CTL_SHOW_SET:
if (idx != PFD_PIPE_ROUTE_CTL)
fatalx("ctl rib request not from RDE");
control_imsg_relay(&imsg, NULL);
break;
case IMSG_CTL_END:
case IMSG_CTL_RESULT:
control_imsg_relay(&imsg, NULL);
break;
case IMSG_UPDATE:
if (idx != PFD_PIPE_ROUTE)
fatalx("update request not from RDE");
len = imsg_get_len(&imsg);
if (imsg_get_ibuf(&imsg, &ibuf) == -1 ||
len > MAX_PKTSIZE - MSGSIZE_HEADER ||
len < MSGSIZE_UPDATE_MIN - MSGSIZE_HEADER)
log_warnx("RDE sent invalid update");
else
session_update(peerid, &ibuf);
break;
case IMSG_UPDATE_ERR:
if (idx != PFD_PIPE_ROUTE)
fatalx("update request not from RDE");
if ((p = getpeerbyid(conf, peerid)) == NULL) {
log_warnx("no such peer: id=%u", peerid);
break;
}
if (imsg_get_ibuf(&imsg, &ibuf) == -1 ||
ibuf_get_n8(&ibuf, &errcode) == -1 ||
ibuf_get_n8(&ibuf, &subcode) == -1) {
log_warnx("RDE sent invalid notification");
break;
}
session_notification(p, errcode, subcode, &ibuf);
switch (errcode) {
case ERR_CEASE:
switch (subcode) {
case ERR_CEASE_MAX_PREFIX:
case ERR_CEASE_MAX_SENT_PREFIX:
t = p->conf.max_out_prefix_restart;
if (subcode == ERR_CEASE_MAX_PREFIX)
t = p->conf.max_prefix_restart;
bgp_fsm(p, EVNT_STOP, NULL);
if (t)
timer_set(&p->timers,
Timer_IdleHold, 60 * t);
break;
default:
bgp_fsm(p, EVNT_CON_FATAL, NULL);
break;
}
break;
default:
bgp_fsm(p, EVNT_CON_FATAL, NULL);
break;
}
break;
case IMSG_REFRESH:
if (idx != PFD_PIPE_ROUTE)
fatalx("route refresh request not from RDE");
if (imsg_get_data(&imsg, &rr, sizeof(rr)) == -1) {
log_warnx("RDE sent invalid refresh msg");
break;
}
if ((p = getpeerbyid(conf, peerid)) == NULL) {
log_warnx("no such peer: id=%u", peerid);
break;
}
if (rr.aid < AID_MIN || rr.aid >= AID_MAX)
fatalx("IMSG_REFRESH: bad AID");
session_rrefresh(p, rr.aid, rr.subtype);
break;
case IMSG_SESSION_RESTARTED:
if (idx != PFD_PIPE_ROUTE)
fatalx("session restart not from RDE");
if (imsg_get_data(&imsg, &aid, sizeof(aid)) == -1) {
log_warnx("RDE sent invalid restart msg");
break;
}
if ((p = getpeerbyid(conf, peerid)) == NULL) {
log_warnx("no such peer: id=%u", peerid);
break;
}
if (aid < AID_MIN || aid >= AID_MAX)
fatalx("IMSG_SESSION_RESTARTED: bad AID");
if (p->capa.neg.grestart.flags[aid] &
CAPA_GR_RESTARTING) {
log_peer_warnx(&p->conf,
"graceful restart of %s finished",
aid2str(aid));
p->capa.neg.grestart.flags[aid] &=
~CAPA_GR_RESTARTING;
timer_stop(&p->timers, Timer_RestartTimeout);
/* signal back to RDE to cleanup stale routes */
if (imsg_rde(IMSG_SESSION_RESTARTED,
peerid, &aid, sizeof(aid)) == -1)
fatal("imsg_compose: "
"IMSG_SESSION_RESTARTED");
}
break;
default:
break;
}
imsg_free(&imsg);
}
}
int
la_cmp(struct listen_addr *a, struct listen_addr *b)
{
struct sockaddr_in *in_a, *in_b;
struct sockaddr_in6 *in6_a, *in6_b;
if (a->sa.ss_family != b->sa.ss_family)
return (1);
switch (a->sa.ss_family) {
case AF_INET:
in_a = (struct sockaddr_in *)&a->sa;
in_b = (struct sockaddr_in *)&b->sa;
if (in_a->sin_addr.s_addr != in_b->sin_addr.s_addr)
return (1);
if (in_a->sin_port != in_b->sin_port)
return (1);
break;
case AF_INET6:
in6_a = (struct sockaddr_in6 *)&a->sa;
in6_b = (struct sockaddr_in6 *)&b->sa;
if (memcmp(&in6_a->sin6_addr, &in6_b->sin6_addr,
sizeof(struct in6_addr)))
return (1);
if (in6_a->sin6_port != in6_b->sin6_port)
return (1);
break;
default:
fatal("king bula sez: unknown address family");
/* NOTREACHED */
}
return (0);
}
struct peer *
getpeerbydesc(struct bgpd_config *c, const char *descr)
{
struct peer *p, *res = NULL;
int match = 0;
RB_FOREACH(p, peer_head, &c->peers)
if (!strcmp(p->conf.descr, descr)) {
res = p;
match++;
}
if (match > 1)
log_info("neighbor description \"%s\" not unique, request "
"aborted", descr);
if (match == 1)
return (res);
else
return (NULL);
}
struct peer *
getpeerbyip(struct bgpd_config *c, struct sockaddr *ip)
{
struct bgpd_addr addr;
struct peer *p, *newpeer, *loose = NULL;
uint32_t id;
sa2addr(ip, &addr, NULL);
/* we might want a more effective way to find peers by IP */
RB_FOREACH(p, peer_head, &c->peers)
if (!p->conf.template &&
!memcmp(&addr, &p->conf.remote_addr, sizeof(addr)))
return (p);
/* try template matching */
RB_FOREACH(p, peer_head, &c->peers)
if (p->conf.template &&
p->conf.remote_addr.aid == addr.aid &&
session_match_mask(p, &addr))
if (loose == NULL || loose->conf.remote_masklen <
p->conf.remote_masklen)
loose = p;
if (loose != NULL) {
/* clone */
if ((newpeer = malloc(sizeof(struct peer))) == NULL)
fatal(NULL);
memcpy(newpeer, loose, sizeof(struct peer));
for (id = PEER_ID_DYN_MAX; id > PEER_ID_STATIC_MAX; id--) {
if (getpeerbyid(c, id) == NULL) /* we found a free id */
break;
}
newpeer->template = loose;
session_template_clone(newpeer, ip, id, 0);
newpeer->state = newpeer->prev_state = STATE_NONE;
newpeer->reconf_action = RECONF_KEEP;
newpeer->rpending = 0;
newpeer->wbuf = NULL;
init_peer(newpeer);
bgp_fsm(newpeer, EVNT_START, NULL);
if (RB_INSERT(peer_head, &c->peers, newpeer) != NULL)
fatalx("%s: peer tree is corrupt", __func__);
return (newpeer);
}
return (NULL);
}
struct peer *
getpeerbyid(struct bgpd_config *c, uint32_t peerid)
{
static struct peer lookup;
lookup.conf.id = peerid;
return RB_FIND(peer_head, &c->peers, &lookup);
}
int
peer_matched(struct peer *p, struct ctl_neighbor *n)
{
char *s;
if (n && n->addr.aid) {
if (memcmp(&p->conf.remote_addr, &n->addr,
sizeof(p->conf.remote_addr)))
return 0;
} else if (n && n->descr[0]) {
s = n->is_group ? p->conf.group : p->conf.descr;
/* cannot trust n->descr to be properly terminated */
if (strncmp(s, n->descr, sizeof(n->descr)))
return 0;
}
return 1;
}
void
session_template_clone(struct peer *p, struct sockaddr *ip, uint32_t id,
uint32_t as)
{
struct bgpd_addr remote_addr;
if (ip)
sa2addr(ip, &remote_addr, NULL);
else
memcpy(&remote_addr, &p->conf.remote_addr, sizeof(remote_addr));
memcpy(&p->conf, &p->template->conf, sizeof(struct peer_config));
p->conf.id = id;
if (as) {
p->conf.remote_as = as;
p->conf.ebgp = (p->conf.remote_as != p->conf.local_as);
if (!p->conf.ebgp)
/* force enforce_as off for iBGP sessions */
p->conf.enforce_as = ENFORCE_AS_OFF;
}
memcpy(&p->conf.remote_addr, &remote_addr, sizeof(remote_addr));
switch (p->conf.remote_addr.aid) {
case AID_INET:
p->conf.remote_masklen = 32;
break;
case AID_INET6:
p->conf.remote_masklen = 128;
break;
}
p->conf.template = 0;
}
int
session_match_mask(struct peer *p, struct bgpd_addr *a)
{
struct bgpd_addr masked;
applymask(&masked, a, p->conf.remote_masklen);
if (memcmp(&masked, &p->conf.remote_addr, sizeof(masked)) == 0)
return (1);
return (0);
}
void
session_down(struct peer *peer)
{
memset(&peer->capa.neg, 0, sizeof(peer->capa.neg));
peer->stats.last_updown = getmonotime();
/*
* session_down is called in the exit code path so check
* if the RDE is still around, if not there is no need to
* send the message.
*/
if (ibuf_rde == NULL)
return;
if (imsg_rde(IMSG_SESSION_DOWN, peer->conf.id, NULL, 0) == -1)
fatalx("imsg_compose error");
}
void
session_up(struct peer *p)
{
struct session_up sup;
/* clear last errors, now that the session is up */
p->stats.last_sent_errcode = 0;
p->stats.last_sent_suberr = 0;
p->stats.last_rcvd_errcode = 0;
p->stats.last_rcvd_suberr = 0;
memset(p->stats.last_reason, 0, sizeof(p->stats.last_reason));
if (imsg_rde(IMSG_SESSION_ADD, p->conf.id,
&p->conf, sizeof(p->conf)) == -1)
fatalx("imsg_compose error");
if (p->local.aid == AID_INET) {
sup.local_v4_addr = p->local;
sup.local_v6_addr = p->local_alt;
} else {
sup.local_v6_addr = p->local;
sup.local_v4_addr = p->local_alt;
}
sup.remote_addr = p->remote;
sup.if_scope = p->if_scope;
sup.remote_bgpid = p->remote_bgpid;
sup.short_as = p->short_as;
memcpy(&sup.capa, &p->capa.neg, sizeof(sup.capa));
p->stats.last_updown = getmonotime();
if (imsg_rde(IMSG_SESSION_UP, p->conf.id, &sup, sizeof(sup)) == -1)
fatalx("imsg_compose error");
}
int
imsg_ctl_parent(struct imsg *imsg)
{
return imsg_forward(ibuf_main, imsg);
}
int
imsg_ctl_rde(struct imsg *imsg)
{
if (ibuf_rde_ctl == NULL)
return (0);
/*
* Use control socket to talk to RDE to bypass the queue of the
* regular imsg socket.
*/
return imsg_forward(ibuf_rde_ctl, imsg);
}
int
imsg_ctl_rde_msg(int type, uint32_t peerid, pid_t pid)
{
if (ibuf_rde_ctl == NULL)
return (0);
/*
* Use control socket to talk to RDE to bypass the queue of the
* regular imsg socket.
*/
return imsg_compose(ibuf_rde_ctl, type, peerid, pid, -1, NULL, 0);
}
int
imsg_rde(int type, uint32_t peerid, void *data, uint16_t datalen)
{
if (ibuf_rde == NULL)
return (0);
return imsg_compose(ibuf_rde, type, peerid, 0, -1, data, datalen);
}
void
session_demote(struct peer *p, int level)
{
struct demote_msg msg;
strlcpy(msg.demote_group, p->conf.demote_group,
sizeof(msg.demote_group));
msg.level = level;
if (imsg_compose(ibuf_main, IMSG_DEMOTE, p->conf.id, 0, -1,
&msg, sizeof(msg)) == -1)
fatalx("imsg_compose error");
p->demoted += level;
}
void
session_stop(struct peer *peer, uint8_t subcode, const char *reason)
{
struct ibuf *ibuf;
if (reason != NULL)
strlcpy(peer->conf.reason, reason, sizeof(peer->conf.reason));
ibuf = ibuf_dynamic(0, REASON_LEN);
if ((subcode == ERR_CEASE_ADMIN_DOWN ||
subcode == ERR_CEASE_ADMIN_RESET) &&
reason != NULL && *reason != '\0' &&
ibuf != NULL) {
if (ibuf_add_n8(ibuf, strlen(reason)) == -1 ||
ibuf_add(ibuf, reason, strlen(reason))) {
log_peer_warnx(&peer->conf,
"trying to send overly long shutdown reason");
ibuf_free(ibuf);
ibuf = NULL;
}
}
switch (peer->state) {
case STATE_OPENSENT:
case STATE_OPENCONFIRM:
case STATE_ESTABLISHED:
session_notification(peer, ERR_CEASE, subcode, ibuf);
break;
default:
/* session not open, no need to send notification */
if (subcode >= sizeof(suberr_cease_names) / sizeof(char *) ||
suberr_cease_names[subcode] == NULL)
log_peer_warnx(&peer->conf, "session stop: %s, "
"unknown subcode %u", errnames[ERR_CEASE], subcode);
else
log_peer_warnx(&peer->conf, "session stop: %s, %s",
errnames[ERR_CEASE], suberr_cease_names[subcode]);
break;
}
ibuf_free(ibuf);
bgp_fsm(peer, EVNT_STOP, NULL);
}
struct bgpd_addr *
session_localaddr(struct peer *p)
{
switch (p->conf.remote_addr.aid) {
case AID_INET:
return &p->conf.local_addr_v4;
case AID_INET6:
return &p->conf.local_addr_v6;
}
fatalx("Unknown AID in %s", __func__);
}
void
merge_peers(struct bgpd_config *c, struct bgpd_config *nc)
{
struct peer *p, *np, *next;
RB_FOREACH(p, peer_head, &c->peers) {
/* templates are handled specially */
if (p->template != NULL)
continue;
np = getpeerbyid(nc, p->conf.id);
if (np == NULL) {
p->reconf_action = RECONF_DELETE;
continue;
}
/* peer no longer uses TCP MD5SIG so deconfigure */
if (p->auth_conf.method == AUTH_MD5SIG &&
np->auth_conf.method != AUTH_MD5SIG)
tcp_md5_del_listener(c, p);
else if (np->auth_conf.method == AUTH_MD5SIG)
tcp_md5_add_listener(c, np);
memcpy(&p->conf, &np->conf, sizeof(p->conf));
memcpy(&p->auth_conf, &np->auth_conf, sizeof(p->auth_conf));
RB_REMOVE(peer_head, &nc->peers, np);
free(np);
p->reconf_action = RECONF_KEEP;
/* had demotion, is demoted, demote removed? */
if (p->demoted && !p->conf.demote_group[0])
session_demote(p, -1);
/* if session is not open then refresh pfkey data */
if (p->state < STATE_OPENSENT && !p->template)
imsg_compose(ibuf_main, IMSG_PFKEY_RELOAD,
p->conf.id, 0, -1, NULL, 0);
/* sync the RDE in case we keep the peer */
if (imsg_rde(IMSG_SESSION_ADD, p->conf.id,
&p->conf, sizeof(struct peer_config)) == -1)
fatalx("imsg_compose error");
/* apply the config to all clones of a template */
if (p->conf.template) {
struct peer *xp;
RB_FOREACH(xp, peer_head, &c->peers) {
if (xp->template != p)
continue;
session_template_clone(xp, NULL, xp->conf.id,
xp->conf.remote_as);
if (imsg_rde(IMSG_SESSION_ADD, xp->conf.id,
&xp->conf, sizeof(xp->conf)) == -1)
fatalx("imsg_compose error");
}
}
}
if (imsg_rde(IMSG_RECONF_DRAIN, 0, NULL, 0) == -1)
fatalx("imsg_compose error");
/* pfkeys of new peers already loaded by the parent process */
RB_FOREACH_SAFE(np, peer_head, &nc->peers, next) {
RB_REMOVE(peer_head, &nc->peers, np);
if (RB_INSERT(peer_head, &c->peers, np) != NULL)
fatalx("%s: peer tree is corrupt", __func__);
if (np->auth_conf.method == AUTH_MD5SIG)
tcp_md5_add_listener(c, np);
}
}
|