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
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
|
/* $OpenBSD: rde.c,v 1.359 2017/01/24 23:38:12 claudio Exp $ */
/*
* Copyright (c) 2003, 2004 Henning Brauer <henning@openbsd.org>
* Copyright (c) 2016 Job Snijders <job@instituut.net>
* Copyright (c) 2016 Peter Hessler <phessler@openbsd.org>
*
* 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/socket.h>
#include <sys/time.h>
#include <sys/resource.h>
#include <errno.h>
#include <ifaddrs.h>
#include <pwd.h>
#include <poll.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <syslog.h>
#include <unistd.h>
#include <err.h>
#include "bgpd.h"
#include "mrt.h"
#include "rde.h"
#include "session.h"
#include "log.h"
#define PFD_PIPE_MAIN 0
#define PFD_PIPE_SESSION 1
#define PFD_PIPE_SESSION_CTL 2
#define PFD_PIPE_COUNT 3
void rde_sighdlr(int);
void rde_dispatch_imsg_session(struct imsgbuf *);
void rde_dispatch_imsg_parent(struct imsgbuf *);
int rde_update_dispatch(struct imsg *);
void rde_update_update(struct rde_peer *, struct rde_aspath *,
struct bgpd_addr *, u_int8_t);
void rde_update_withdraw(struct rde_peer *, struct bgpd_addr *,
u_int8_t);
int rde_attr_parse(u_char *, u_int16_t, struct rde_peer *,
struct rde_aspath *, struct mpattr *);
int rde_attr_add(struct rde_aspath *, u_char *, u_int16_t);
u_int8_t rde_attr_missing(struct rde_aspath *, int, u_int16_t);
int rde_get_mp_nexthop(u_char *, u_int16_t, u_int8_t,
struct rde_aspath *);
int rde_update_extract_prefix(u_char *, u_int16_t, void *,
u_int8_t, u_int8_t);
int rde_update_get_prefix(u_char *, u_int16_t, struct bgpd_addr *,
u_int8_t *);
int rde_update_get_prefix6(u_char *, u_int16_t, struct bgpd_addr *,
u_int8_t *);
int rde_update_get_vpn4(u_char *, u_int16_t, struct bgpd_addr *,
u_int8_t *);
void rde_update_err(struct rde_peer *, u_int8_t , u_int8_t,
void *, u_int16_t);
void rde_update_log(const char *, u_int16_t,
const struct rde_peer *, const struct bgpd_addr *,
const struct bgpd_addr *, u_int8_t);
void rde_as4byte_fixup(struct rde_peer *, struct rde_aspath *);
void rde_reflector(struct rde_peer *, struct rde_aspath *);
void rde_dump_rib_as(struct prefix *, struct rde_aspath *,pid_t,
int);
void rde_dump_filter(struct prefix *,
struct ctl_show_rib_request *);
void rde_dump_filterout(struct rde_peer *, struct prefix *,
struct ctl_show_rib_request *);
void rde_dump_upcall(struct rib_entry *, void *);
void rde_dump_prefix_upcall(struct rib_entry *, void *);
void rde_dump_ctx_new(struct ctl_show_rib_request *, pid_t,
enum imsg_type);
void rde_dump_mrt_new(struct mrt *, pid_t, int);
void rde_dump_done(void *);
int rde_rdomain_import(struct rde_aspath *, struct rdomain *);
void rde_reload_done(void);
void rde_softreconfig_out(struct rib_entry *, void *);
void rde_softreconfig_in(struct rib_entry *, void *);
void rde_softreconfig_unload_peer(struct rib_entry *, void *);
void rde_up_dump_upcall(struct rib_entry *, void *);
void rde_update_queue_runner(void);
void rde_update6_queue_runner(u_int8_t);
void peer_init(u_int32_t);
void peer_shutdown(void);
int peer_localaddrs(struct rde_peer *, struct bgpd_addr *);
struct rde_peer *peer_add(u_int32_t, struct peer_config *);
struct rde_peer *peer_get(u_int32_t);
void peer_up(u_int32_t, struct session_up *);
void peer_down(u_int32_t);
void peer_flush(struct rde_peer *, u_int8_t);
void peer_stale(u_int32_t, u_int8_t);
void peer_recv_eor(struct rde_peer *, u_int8_t);
void peer_dump(u_int32_t, u_int8_t);
void peer_send_eor(struct rde_peer *, u_int8_t);
void network_add(struct network_config *, int);
void network_delete(struct network_config *, int);
void network_dump_upcall(struct rib_entry *, void *);
void rde_shutdown(void);
int sa_cmp(struct bgpd_addr *, struct sockaddr *);
volatile sig_atomic_t rde_quit = 0;
struct bgpd_config *conf, *nconf;
time_t reloadtime;
struct rde_peer_head peerlist;
struct rde_peer *peerself;
struct filter_head *out_rules, *out_rules_tmp;
struct rdomain_head *rdomains_l, *newdomains;
struct imsgbuf *ibuf_se;
struct imsgbuf *ibuf_se_ctl;
struct imsgbuf *ibuf_main;
struct rde_memstats rdemem;
struct rde_dump_ctx {
struct rib_context ribctx;
struct ctl_show_rib_request req;
sa_family_t af;
};
struct rde_mrt_ctx {
struct mrt mrt;
struct rib_context ribctx;
LIST_ENTRY(rde_mrt_ctx) entry;
};
LIST_HEAD(, rde_mrt_ctx) rde_mrts = LIST_HEAD_INITIALIZER(rde_mrts);
u_int rde_mrt_cnt;
void
rde_sighdlr(int sig)
{
switch (sig) {
case SIGINT:
case SIGTERM:
rde_quit = 1;
break;
}
}
u_int32_t peerhashsize = 64;
u_int32_t pathhashsize = 1024;
u_int32_t attrhashsize = 512;
u_int32_t nexthophashsize = 64;
void
rde_main(int debug, int verbose)
{
struct passwd *pw;
struct pollfd *pfd = NULL;
struct rde_mrt_ctx *mctx, *xmctx;
void *newp;
u_int pfd_elms = 0, i, j;
int timeout;
u_int8_t aid;
log_init(debug, LOG_DAEMON);
log_setverbose(verbose);
bgpd_process = PROC_RDE;
log_procinit(log_procnames[bgpd_process]);
if ((pw = getpwnam(BGPD_USER)) == NULL)
fatal("getpwnam");
if (chroot(pw->pw_dir) == -1)
fatal("chroot");
if (chdir("/") == -1)
fatal("chdir(\"/\")");
setproctitle("route decision 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 route recvfd", NULL) == -1)
fatal("pledge");
signal(SIGTERM, rde_sighdlr);
signal(SIGINT, rde_sighdlr);
signal(SIGPIPE, SIG_IGN);
signal(SIGHUP, SIG_IGN);
signal(SIGALRM, SIG_IGN);
signal(SIGUSR1, SIG_IGN);
/* initialize the RIB structures */
if ((ibuf_main = malloc(sizeof(struct imsgbuf))) == NULL)
fatal(NULL);
imsg_init(ibuf_main, 3);
pt_init();
path_init(pathhashsize);
aspath_init(pathhashsize);
attr_init(attrhashsize);
nexthop_init(nexthophashsize);
peer_init(peerhashsize);
out_rules = calloc(1, sizeof(struct filter_head));
if (out_rules == NULL)
fatal(NULL);
TAILQ_INIT(out_rules);
rdomains_l = calloc(1, sizeof(struct rdomain_head));
if (rdomains_l == NULL)
fatal(NULL);
SIMPLEQ_INIT(rdomains_l);
if ((conf = calloc(1, sizeof(struct bgpd_config))) == NULL)
fatal(NULL);
log_info("route decision engine ready");
while (rde_quit == 0) {
if (pfd_elms < PFD_PIPE_COUNT + rde_mrt_cnt) {
if ((newp = reallocarray(pfd,
PFD_PIPE_COUNT + rde_mrt_cnt,
sizeof(struct pollfd))) == NULL) {
/* panic for now */
log_warn("could not resize pfd from %u -> %u"
" entries", pfd_elms, PFD_PIPE_COUNT +
rde_mrt_cnt);
fatalx("exiting");
}
pfd = newp;
pfd_elms = PFD_PIPE_COUNT + rde_mrt_cnt;
}
timeout = INFTIM;
bzero(pfd, sizeof(struct pollfd) * pfd_elms);
set_pollfd(&pfd[PFD_PIPE_MAIN], ibuf_main);
set_pollfd(&pfd[PFD_PIPE_SESSION], ibuf_se);
set_pollfd(&pfd[PFD_PIPE_SESSION_CTL], ibuf_se_ctl);
if (rib_dump_pending() &&
ibuf_se_ctl && ibuf_se_ctl->w.queued == 0)
timeout = 0;
i = PFD_PIPE_COUNT;
for (mctx = LIST_FIRST(&rde_mrts); mctx != 0; mctx = xmctx) {
xmctx = LIST_NEXT(mctx, entry);
if (mctx->mrt.wbuf.queued) {
pfd[i].fd = mctx->mrt.wbuf.fd;
pfd[i].events = POLLOUT;
i++;
} else if (mctx->mrt.state == MRT_STATE_REMOVE) {
close(mctx->mrt.wbuf.fd);
LIST_REMOVE(&mctx->ribctx, entry);
LIST_REMOVE(mctx, entry);
free(mctx);
rde_mrt_cnt--;
}
}
if (poll(pfd, i, timeout) == -1) {
if (errno != EINTR)
fatal("poll error");
continue;
}
if (handle_pollfd(&pfd[PFD_PIPE_MAIN], ibuf_main) == -1)
fatalx("Lost connection to parent");
else
rde_dispatch_imsg_parent(ibuf_main);
if (handle_pollfd(&pfd[PFD_PIPE_SESSION], ibuf_se) == -1) {
log_warnx("RDE: Lost connection to SE");
msgbuf_clear(&ibuf_se->w);
free(ibuf_se);
ibuf_se = NULL;
} else
rde_dispatch_imsg_session(ibuf_se);
if (handle_pollfd(&pfd[PFD_PIPE_SESSION_CTL], ibuf_se_ctl) ==
-1) {
log_warnx("RDE: Lost connection to SE control");
msgbuf_clear(&ibuf_se_ctl->w);
free(ibuf_se_ctl);
ibuf_se_ctl = NULL;
} else
rde_dispatch_imsg_session(ibuf_se_ctl);
for (j = PFD_PIPE_COUNT, mctx = LIST_FIRST(&rde_mrts);
j < i && mctx != 0; j++) {
if (pfd[j].fd == mctx->mrt.wbuf.fd &&
pfd[j].revents & POLLOUT)
mrt_write(&mctx->mrt);
mctx = LIST_NEXT(mctx, entry);
}
rde_update_queue_runner();
for (aid = AID_INET6; aid < AID_MAX; aid++)
rde_update6_queue_runner(aid);
if (rib_dump_pending() &&
ibuf_se_ctl && ibuf_se_ctl->w.queued <= 10)
rib_dump_runner();
}
/* close pipes */
if (ibuf_se) {
msgbuf_clear(&ibuf_se->w);
close(ibuf_se->fd);
free(ibuf_se);
}
if (ibuf_se_ctl) {
msgbuf_clear(&ibuf_se_ctl->w);
close(ibuf_se_ctl->fd);
free(ibuf_se_ctl);
}
msgbuf_clear(&ibuf_main->w);
close(ibuf_main->fd);
free(ibuf_main);
/* do not clean up on shutdown on production, it takes ages. */
if (debug)
rde_shutdown();
while ((mctx = LIST_FIRST(&rde_mrts)) != NULL) {
msgbuf_clear(&mctx->mrt.wbuf);
close(mctx->mrt.wbuf.fd);
LIST_REMOVE(&mctx->ribctx, entry);
LIST_REMOVE(mctx, entry);
free(mctx);
}
log_info("route decision engine exiting");
exit(0);
}
struct network_config netconf_s, netconf_p;
struct filter_set_head *session_set, *parent_set;
void
rde_dispatch_imsg_session(struct imsgbuf *ibuf)
{
struct imsg imsg;
struct peer p;
struct peer_config pconf;
struct session_up sup;
struct ctl_show_rib csr;
struct ctl_show_rib_request req;
struct rde_peer *peer;
struct rde_aspath *asp;
struct filter_set *s;
struct nexthop *nh;
u_int8_t *asdata;
ssize_t n;
int verbose;
u_int16_t len;
u_int8_t aid;
while (ibuf) {
if ((n = imsg_get(ibuf, &imsg)) == -1)
fatal("rde_dispatch_imsg_session: imsg_get error");
if (n == 0)
break;
switch (imsg.hdr.type) {
case IMSG_UPDATE:
rde_update_dispatch(&imsg);
break;
case IMSG_SESSION_ADD:
if (imsg.hdr.len - IMSG_HEADER_SIZE != sizeof(pconf))
fatalx("incorrect size of session request");
memcpy(&pconf, imsg.data, sizeof(pconf));
peer_add(imsg.hdr.peerid, &pconf);
break;
case IMSG_SESSION_UP:
if (imsg.hdr.len - IMSG_HEADER_SIZE != sizeof(sup))
fatalx("incorrect size of session request");
memcpy(&sup, imsg.data, sizeof(sup));
peer_up(imsg.hdr.peerid, &sup);
break;
case IMSG_SESSION_DOWN:
peer_down(imsg.hdr.peerid);
break;
case IMSG_SESSION_STALE:
if (imsg.hdr.len - IMSG_HEADER_SIZE != sizeof(aid)) {
log_warnx("rde_dispatch: wrong imsg len");
break;
}
memcpy(&aid, imsg.data, sizeof(aid));
if (aid >= AID_MAX)
fatalx("IMSG_SESSION_STALE: bad AID");
peer_stale(imsg.hdr.peerid, aid);
break;
case IMSG_SESSION_FLUSH:
if (imsg.hdr.len - IMSG_HEADER_SIZE != sizeof(aid)) {
log_warnx("rde_dispatch: wrong imsg len");
break;
}
memcpy(&aid, imsg.data, sizeof(aid));
if (aid >= AID_MAX)
fatalx("IMSG_SESSION_FLUSH: bad AID");
if ((peer = peer_get(imsg.hdr.peerid)) == NULL) {
log_warnx("rde_dispatch: unknown peer id %d",
imsg.hdr.peerid);
break;
}
peer_flush(peer, aid);
break;
case IMSG_SESSION_RESTARTED:
if (imsg.hdr.len - IMSG_HEADER_SIZE != sizeof(aid)) {
log_warnx("rde_dispatch: wrong imsg len");
break;
}
memcpy(&aid, imsg.data, sizeof(aid));
if (aid >= AID_MAX)
fatalx("IMSG_SESSION_RESTARTED: bad AID");
if ((peer = peer_get(imsg.hdr.peerid)) == NULL) {
log_warnx("rde_dispatch: unknown peer id %d",
imsg.hdr.peerid);
break;
}
if (peer->staletime[aid])
peer_flush(peer, aid);
break;
case IMSG_REFRESH:
if (imsg.hdr.len - IMSG_HEADER_SIZE != sizeof(aid)) {
log_warnx("rde_dispatch: wrong imsg len");
break;
}
memcpy(&aid, imsg.data, sizeof(aid));
if (aid >= AID_MAX)
fatalx("IMSG_REFRESH: bad AID");
peer_dump(imsg.hdr.peerid, aid);
break;
case IMSG_NETWORK_ADD:
if (imsg.hdr.len - IMSG_HEADER_SIZE !=
sizeof(struct network_config)) {
log_warnx("rde_dispatch: wrong imsg len");
break;
}
memcpy(&netconf_s, imsg.data, sizeof(netconf_s));
TAILQ_INIT(&netconf_s.attrset);
session_set = &netconf_s.attrset;
break;
case IMSG_NETWORK_ASPATH:
if (imsg.hdr.len - IMSG_HEADER_SIZE <
sizeof(struct ctl_show_rib)) {
log_warnx("rde_dispatch: wrong imsg len");
bzero(&netconf_s, sizeof(netconf_s));
break;
}
asdata = imsg.data;
asdata += sizeof(struct ctl_show_rib);
memcpy(&csr, imsg.data, sizeof(csr));
if (csr.aspath_len + sizeof(csr) > imsg.hdr.len -
IMSG_HEADER_SIZE) {
log_warnx("rde_dispatch: wrong aspath len");
bzero(&netconf_s, sizeof(netconf_s));
break;
}
asp = path_get();
asp->lpref = csr.local_pref;
asp->med = csr.med;
asp->weight = csr.weight;
asp->flags = csr.flags;
asp->origin = csr.origin;
asp->flags |= F_PREFIX_ANNOUNCED | F_ANN_DYNAMIC;
asp->aspath = aspath_get(asdata, csr.aspath_len);
netconf_s.asp = asp;
break;
case IMSG_NETWORK_ATTR:
if (imsg.hdr.len <= IMSG_HEADER_SIZE) {
log_warnx("rde_dispatch: wrong imsg len");
break;
}
/* parse path attributes */
len = imsg.hdr.len - IMSG_HEADER_SIZE;
asp = netconf_s.asp;
if (rde_attr_add(asp, imsg.data, len) == -1) {
log_warnx("rde_dispatch: bad network "
"attribute");
path_put(asp);
bzero(&netconf_s, sizeof(netconf_s));
break;
}
break;
case IMSG_NETWORK_DONE:
if (imsg.hdr.len != IMSG_HEADER_SIZE) {
log_warnx("rde_dispatch: wrong imsg len");
break;
}
session_set = NULL;
switch (netconf_s.prefix.aid) {
case AID_INET:
if (netconf_s.prefixlen > 32)
goto badnet;
network_add(&netconf_s, 0);
break;
case AID_INET6:
if (netconf_s.prefixlen > 128)
goto badnet;
network_add(&netconf_s, 0);
break;
case 0:
/* something failed beforehands */
break;
default:
badnet:
log_warnx("rde_dispatch: bad network");
break;
}
break;
case IMSG_NETWORK_REMOVE:
if (imsg.hdr.len - IMSG_HEADER_SIZE !=
sizeof(struct network_config)) {
log_warnx("rde_dispatch: wrong imsg len");
break;
}
memcpy(&netconf_s, imsg.data, sizeof(netconf_s));
TAILQ_INIT(&netconf_s.attrset);
network_delete(&netconf_s, 0);
break;
case IMSG_NETWORK_FLUSH:
if (imsg.hdr.len != IMSG_HEADER_SIZE) {
log_warnx("rde_dispatch: wrong imsg len");
break;
}
prefix_network_clean(peerself, time(NULL),
F_ANN_DYNAMIC);
break;
case IMSG_FILTER_SET:
if (imsg.hdr.len - IMSG_HEADER_SIZE !=
sizeof(struct filter_set)) {
log_warnx("rde_dispatch: wrong imsg len");
break;
}
if (session_set == NULL) {
log_warnx("rde_dispatch: "
"IMSG_FILTER_SET unexpected");
break;
}
if ((s = malloc(sizeof(struct filter_set))) == NULL)
fatal(NULL);
memcpy(s, imsg.data, sizeof(struct filter_set));
TAILQ_INSERT_TAIL(session_set, s, entry);
if (s->type == ACTION_SET_NEXTHOP) {
nh = nexthop_get(&s->action.nexthop);
nh->refcnt++;
}
break;
case IMSG_CTL_SHOW_NETWORK:
case IMSG_CTL_SHOW_RIB:
case IMSG_CTL_SHOW_RIB_AS:
case IMSG_CTL_SHOW_RIB_COMMUNITY:
case IMSG_CTL_SHOW_RIB_LARGECOMMUNITY:
case IMSG_CTL_SHOW_RIB_PREFIX:
if (imsg.hdr.len != IMSG_HEADER_SIZE + sizeof(req)) {
log_warnx("rde_dispatch: wrong imsg len");
break;
}
memcpy(&req, imsg.data, sizeof(req));
rde_dump_ctx_new(&req, imsg.hdr.pid, imsg.hdr.type);
break;
case IMSG_CTL_SHOW_NEIGHBOR:
if (imsg.hdr.len - IMSG_HEADER_SIZE !=
sizeof(struct peer)) {
log_warnx("rde_dispatch: wrong imsg len");
break;
}
memcpy(&p, imsg.data, sizeof(struct peer));
peer = peer_get(p.conf.id);
if (peer != NULL) {
p.stats.prefix_cnt = peer->prefix_cnt;
p.stats.prefix_rcvd_update =
peer->prefix_rcvd_update;
p.stats.prefix_rcvd_withdraw =
peer->prefix_rcvd_withdraw;
p.stats.prefix_rcvd_eor =
peer->prefix_rcvd_eor;
p.stats.prefix_sent_update =
peer->prefix_sent_update;
p.stats.prefix_sent_withdraw =
peer->prefix_sent_withdraw;
p.stats.prefix_sent_eor =
peer->prefix_sent_eor;
}
imsg_compose(ibuf_se_ctl, IMSG_CTL_SHOW_NEIGHBOR, 0,
imsg.hdr.pid, -1, &p, sizeof(struct peer));
break;
case IMSG_CTL_END:
imsg_compose(ibuf_se_ctl, IMSG_CTL_END, 0, imsg.hdr.pid,
-1, NULL, 0);
break;
case IMSG_CTL_SHOW_RIB_MEM:
imsg_compose(ibuf_se_ctl, IMSG_CTL_SHOW_RIB_MEM, 0,
imsg.hdr.pid, -1, &rdemem, sizeof(rdemem));
break;
case IMSG_CTL_LOG_VERBOSE:
/* already checked by SE */
memcpy(&verbose, imsg.data, sizeof(verbose));
log_setverbose(verbose);
break;
default:
break;
}
imsg_free(&imsg);
}
}
void
rde_dispatch_imsg_parent(struct imsgbuf *ibuf)
{
static struct rdomain *rd;
struct imsg imsg;
struct mrt xmrt;
struct rde_rib rn;
struct imsgbuf *i;
struct filter_head *nr;
struct filter_rule *r;
struct filter_set *s;
struct nexthop *nh;
struct rib_desc *rib;
int n, fd;
u_int16_t rid;
while (ibuf) {
if ((n = imsg_get(ibuf, &imsg)) == -1)
fatal("rde_dispatch_imsg_parent: imsg_get error");
if (n == 0)
break;
switch (imsg.hdr.type) {
case IMSG_SOCKET_CONN:
case IMSG_SOCKET_CONN_CTL:
if ((fd = imsg.fd) == -1) {
log_warnx("expected to receive imsg fd to "
"SE but didn't receive any");
break;
}
if ((i = malloc(sizeof(struct imsgbuf))) == NULL)
fatal(NULL);
imsg_init(i, fd);
if (imsg.hdr.type == IMSG_SOCKET_CONN) {
if (ibuf_se) {
log_warnx("Unexpected imsg connection "
"to SE received");
msgbuf_clear(&ibuf_se->w);
free(ibuf_se);
}
ibuf_se = i;
} else {
if (ibuf_se_ctl) {
log_warnx("Unexpected imsg ctl "
"connection to SE received");
msgbuf_clear(&ibuf_se_ctl->w);
free(ibuf_se_ctl);
}
ibuf_se_ctl = i;
}
break;
case IMSG_NETWORK_ADD:
if (imsg.hdr.len - IMSG_HEADER_SIZE !=
sizeof(struct network_config)) {
log_warnx("rde_dispatch: wrong imsg len");
break;
}
memcpy(&netconf_p, imsg.data, sizeof(netconf_p));
TAILQ_INIT(&netconf_p.attrset);
parent_set = &netconf_p.attrset;
break;
case IMSG_NETWORK_DONE:
parent_set = NULL;
network_add(&netconf_p, 1);
break;
case IMSG_NETWORK_REMOVE:
if (imsg.hdr.len - IMSG_HEADER_SIZE !=
sizeof(struct network_config)) {
log_warnx("rde_dispatch: wrong imsg len");
break;
}
memcpy(&netconf_p, imsg.data, sizeof(netconf_p));
TAILQ_INIT(&netconf_p.attrset);
network_delete(&netconf_p, 1);
break;
case IMSG_RECONF_CONF:
if (imsg.hdr.len - IMSG_HEADER_SIZE !=
sizeof(struct bgpd_config))
fatalx("IMSG_RECONF_CONF bad len");
reloadtime = time(NULL);
out_rules_tmp = calloc(1, sizeof(struct filter_head));
if (out_rules_tmp == NULL)
fatal(NULL);
TAILQ_INIT(out_rules_tmp);
newdomains = calloc(1, sizeof(struct rdomain_head));
if (newdomains == NULL)
fatal(NULL);
SIMPLEQ_INIT(newdomains);
if ((nconf = malloc(sizeof(struct bgpd_config))) ==
NULL)
fatal(NULL);
memcpy(nconf, imsg.data, sizeof(struct bgpd_config));
for (rid = 0; rid < rib_size; rid++) {
if (*ribs[rid].name == '\0')
break;
ribs[rid].state = RECONF_DELETE;
}
break;
case IMSG_RECONF_RIB:
if (imsg.hdr.len - IMSG_HEADER_SIZE !=
sizeof(struct rde_rib))
fatalx("IMSG_RECONF_RIB bad len");
memcpy(&rn, imsg.data, sizeof(rn));
rib = rib_find(rn.name);
if (rib == NULL)
rib = rib_new(rn.name, rn.rtableid, rn.flags);
else if (rib->rtableid != rn.rtableid ||
(rib->rib.flags & F_RIB_HASNOFIB) !=
(rib->rib.flags & F_RIB_HASNOFIB)) {
struct filter_head *in_rules;
/*
* Big hammer in the F_RIB_HASNOFIB case but
* not often enough used to optimise it more.
* Need to save the filters so that they're not
* lost.
*/
in_rules = rib->in_rules;
rib->in_rules = NULL;
rib_free(rib);
rib = rib_new(rn.name, rn.rtableid, rn.flags);
rib->in_rules = in_rules;
} else
rib->state = RECONF_KEEP;
break;
case IMSG_RECONF_FILTER:
if (imsg.hdr.len - IMSG_HEADER_SIZE !=
sizeof(struct filter_rule))
fatalx("IMSG_RECONF_FILTER bad len");
if ((r = malloc(sizeof(struct filter_rule))) == NULL)
fatal(NULL);
memcpy(r, imsg.data, sizeof(struct filter_rule));
TAILQ_INIT(&r->set);
if ((rib = rib_find(r->rib)) == NULL) {
log_warnx("IMSG_RECONF_FILTER: filter rule "
"for nonexistent rib %s", r->rib);
parent_set = NULL;
free(r);
break;
}
r->peer.ribid = rib->rib.id;
parent_set = &r->set;
if (r->dir == DIR_IN) {
nr = rib->in_rules_tmp;
if (nr == NULL) {
nr = calloc(1,
sizeof(struct filter_head));
if (nr == NULL)
fatal(NULL);
TAILQ_INIT(nr);
rib->in_rules_tmp = nr;
}
TAILQ_INSERT_TAIL(nr, r, entry);
} else
TAILQ_INSERT_TAIL(out_rules_tmp, r, entry);
break;
case IMSG_RECONF_RDOMAIN:
if (imsg.hdr.len - IMSG_HEADER_SIZE !=
sizeof(struct rdomain))
fatalx("IMSG_RECONF_RDOMAIN bad len");
if ((rd = malloc(sizeof(struct rdomain))) == NULL)
fatal(NULL);
memcpy(rd, imsg.data, sizeof(struct rdomain));
TAILQ_INIT(&rd->import);
TAILQ_INIT(&rd->export);
SIMPLEQ_INSERT_TAIL(newdomains, rd, entry);
break;
case IMSG_RECONF_RDOMAIN_EXPORT:
if (rd == NULL) {
log_warnx("rde_dispatch_imsg_parent: "
"IMSG_RECONF_RDOMAIN_EXPORT unexpected");
break;
}
parent_set = &rd->export;
break;
case IMSG_RECONF_RDOMAIN_IMPORT:
if (rd == NULL) {
log_warnx("rde_dispatch_imsg_parent: "
"IMSG_RECONF_RDOMAIN_IMPORT unexpected");
break;
}
parent_set = &rd->import;
break;
case IMSG_RECONF_RDOMAIN_DONE:
parent_set = NULL;
break;
case IMSG_RECONF_DONE:
if (nconf == NULL)
fatalx("got IMSG_RECONF_DONE but no config");
parent_set = NULL;
rde_reload_done();
break;
case IMSG_NEXTHOP_UPDATE:
nexthop_update(imsg.data);
break;
case IMSG_FILTER_SET:
if (imsg.hdr.len > IMSG_HEADER_SIZE +
sizeof(struct filter_set))
fatalx("IMSG_FILTER_SET bad len");
if (parent_set == NULL) {
log_warnx("rde_dispatch_imsg_parent: "
"IMSG_FILTER_SET unexpected");
break;
}
if ((s = malloc(sizeof(struct filter_set))) == NULL)
fatal(NULL);
memcpy(s, imsg.data, sizeof(struct filter_set));
TAILQ_INSERT_TAIL(parent_set, s, entry);
if (s->type == ACTION_SET_NEXTHOP) {
nh = nexthop_get(&s->action.nexthop);
nh->refcnt++;
}
break;
case IMSG_MRT_OPEN:
case IMSG_MRT_REOPEN:
if (imsg.hdr.len > IMSG_HEADER_SIZE +
sizeof(struct mrt)) {
log_warnx("wrong imsg len");
break;
}
memcpy(&xmrt, imsg.data, sizeof(xmrt));
if ((fd = imsg.fd) == -1)
log_warnx("expected to receive fd for mrt dump "
"but didn't receive any");
else if (xmrt.type == MRT_TABLE_DUMP ||
xmrt.type == MRT_TABLE_DUMP_MP ||
xmrt.type == MRT_TABLE_DUMP_V2) {
rde_dump_mrt_new(&xmrt, imsg.hdr.pid, fd);
} else
close(fd);
break;
case IMSG_MRT_CLOSE:
/* ignore end message because a dump is atomic */
break;
default:
break;
}
imsg_free(&imsg);
}
}
/* handle routing updates from the session engine. */
int
rde_update_dispatch(struct imsg *imsg)
{
struct bgpd_addr prefix;
struct mpattr mpa;
struct rde_peer *peer;
struct rde_aspath *asp = NULL;
u_char *p, *mpp = NULL;
int error = -1, pos = 0;
u_int16_t afi, len, mplen;
u_int16_t withdrawn_len;
u_int16_t attrpath_len;
u_int16_t nlri_len;
u_int8_t aid, prefixlen, safi, subtype;
u_int32_t fas;
peer = peer_get(imsg->hdr.peerid);
if (peer == NULL) /* unknown peer, cannot happen */
return (-1);
if (peer->state != PEER_UP)
return (-1); /* peer is not yet up, cannot happen */
p = imsg->data;
if (imsg->hdr.len < IMSG_HEADER_SIZE + 2) {
rde_update_err(peer, ERR_UPDATE, ERR_UPD_ATTRLIST, NULL, 0);
return (-1);
}
memcpy(&len, p, 2);
withdrawn_len = ntohs(len);
p += 2;
if (imsg->hdr.len < IMSG_HEADER_SIZE + 2 + withdrawn_len + 2) {
rde_update_err(peer, ERR_UPDATE, ERR_UPD_ATTRLIST, NULL, 0);
return (-1);
}
p += withdrawn_len;
memcpy(&len, p, 2);
attrpath_len = len = ntohs(len);
p += 2;
if (imsg->hdr.len <
IMSG_HEADER_SIZE + 2 + withdrawn_len + 2 + attrpath_len) {
rde_update_err(peer, ERR_UPDATE, ERR_UPD_ATTRLIST, NULL, 0);
return (-1);
}
nlri_len =
imsg->hdr.len - IMSG_HEADER_SIZE - 4 - withdrawn_len - attrpath_len;
bzero(&mpa, sizeof(mpa));
if (attrpath_len != 0) { /* 0 = no NLRI information in this message */
/* parse path attributes */
asp = path_get();
while (len > 0) {
if ((pos = rde_attr_parse(p, len, peer, asp,
&mpa)) < 0)
goto done;
p += pos;
len -= pos;
}
/* check for missing but necessary attributes */
if ((subtype = rde_attr_missing(asp, peer->conf.ebgp,
nlri_len))) {
rde_update_err(peer, ERR_UPDATE, ERR_UPD_MISSNG_WK_ATTR,
&subtype, sizeof(u_int8_t));
goto done;
}
rde_as4byte_fixup(peer, asp);
/* enforce remote AS if requested */
if (asp->flags & F_ATTR_ASPATH &&
peer->conf.enforce_as == ENFORCE_AS_ON) {
fas = aspath_neighbor(asp->aspath);
if (peer->conf.remote_as != fas) {
log_peer_warnx(&peer->conf, "bad path, "
"starting with %s, "
"enforce neighbor-as enabled", log_as(fas));
rde_update_err(peer, ERR_UPDATE, ERR_UPD_ASPATH,
NULL, 0);
goto done;
}
}
rde_reflector(peer, asp);
}
p = imsg->data;
len = withdrawn_len;
p += 2;
/* withdraw prefix */
while (len > 0) {
if ((pos = rde_update_get_prefix(p, len, &prefix,
&prefixlen)) == -1) {
/*
* the RFC does not mention what we should do in
* this case. Let's do the same as in the NLRI case.
*/
log_peer_warnx(&peer->conf, "bad withdraw prefix");
rde_update_err(peer, ERR_UPDATE, ERR_UPD_NETWORK,
NULL, 0);
goto done;
}
if (prefixlen > 32) {
log_peer_warnx(&peer->conf, "bad withdraw prefix");
rde_update_err(peer, ERR_UPDATE, ERR_UPD_NETWORK,
NULL, 0);
goto done;
}
p += pos;
len -= pos;
if (peer->capa.mp[AID_INET] == 0) {
log_peer_warnx(&peer->conf,
"bad withdraw, %s disabled", aid2str(AID_INET));
rde_update_err(peer, ERR_UPDATE, ERR_UPD_OPTATTR,
NULL, 0);
goto done;
}
rde_update_withdraw(peer, &prefix, prefixlen);
}
if (attrpath_len == 0) {
/* 0 = no NLRI information in this message */
if (nlri_len != 0) {
/* crap at end of update which should not be there */
rde_update_err(peer, ERR_UPDATE,
ERR_UPD_ATTRLIST, NULL, 0);
return (-1);
}
if (withdrawn_len == 0) {
/* EoR marker */
peer_recv_eor(peer, AID_INET);
}
return (0);
}
/* withdraw MP_UNREACH_NLRI if available */
if (mpa.unreach_len != 0) {
mpp = mpa.unreach;
mplen = mpa.unreach_len;
memcpy(&afi, mpp, 2);
mpp += 2;
mplen -= 2;
afi = ntohs(afi);
safi = *mpp++;
mplen--;
if (afi2aid(afi, safi, &aid) == -1) {
log_peer_warnx(&peer->conf,
"bad AFI/SAFI pair in withdraw");
rde_update_err(peer, ERR_UPDATE, ERR_UPD_OPTATTR,
NULL, 0);
goto done;
}
if (peer->capa.mp[aid] == 0) {
log_peer_warnx(&peer->conf,
"bad withdraw, %s disabled", aid2str(aid));
rde_update_err(peer, ERR_UPDATE, ERR_UPD_OPTATTR,
NULL, 0);
goto done;
}
if ((asp->flags & ~F_ATTR_MP_UNREACH) == 0 && mplen == 0) {
/* EoR marker */
peer_recv_eor(peer, aid);
}
switch (aid) {
case AID_INET6:
while (mplen > 0) {
if ((pos = rde_update_get_prefix6(mpp, mplen,
&prefix, &prefixlen)) == -1) {
log_peer_warnx(&peer->conf,
"bad IPv6 withdraw prefix");
rde_update_err(peer, ERR_UPDATE,
ERR_UPD_OPTATTR,
mpa.unreach, mpa.unreach_len);
goto done;
}
if (prefixlen > 128) {
log_peer_warnx(&peer->conf,
"bad IPv6 withdraw prefix");
rde_update_err(peer, ERR_UPDATE,
ERR_UPD_OPTATTR,
mpa.unreach, mpa.unreach_len);
goto done;
}
mpp += pos;
mplen -= pos;
rde_update_withdraw(peer, &prefix, prefixlen);
}
break;
case AID_VPN_IPv4:
while (mplen > 0) {
if ((pos = rde_update_get_vpn4(mpp, mplen,
&prefix, &prefixlen)) == -1) {
log_peer_warnx(&peer->conf,
"bad VPNv4 withdraw prefix");
rde_update_err(peer, ERR_UPDATE,
ERR_UPD_OPTATTR,
mpa.unreach, mpa.unreach_len);
goto done;
}
if (prefixlen > 32) {
log_peer_warnx(&peer->conf,
"bad VPNv4 withdraw prefix");
rde_update_err(peer, ERR_UPDATE,
ERR_UPD_OPTATTR,
mpa.unreach, mpa.unreach_len);
goto done;
}
mpp += pos;
mplen -= pos;
rde_update_withdraw(peer, &prefix, prefixlen);
}
break;
default:
/* silently ignore unsupported multiprotocol AF */
break;
}
if ((asp->flags & ~F_ATTR_MP_UNREACH) == 0) {
error = 0;
goto done;
}
}
/* shift to NLRI information */
p += 2 + attrpath_len;
/* aspath needs to be loop free nota bene this is not a hard error */
if (peer->conf.ebgp && !aspath_loopfree(asp->aspath, conf->as))
asp->flags |= F_ATTR_LOOP;
/* parse nlri prefix */
while (nlri_len > 0) {
if ((pos = rde_update_get_prefix(p, nlri_len, &prefix,
&prefixlen)) == -1) {
log_peer_warnx(&peer->conf, "bad nlri prefix");
rde_update_err(peer, ERR_UPDATE, ERR_UPD_NETWORK,
NULL, 0);
goto done;
}
if (prefixlen > 32) {
log_peer_warnx(&peer->conf, "bad nlri prefix");
rde_update_err(peer, ERR_UPDATE, ERR_UPD_NETWORK,
NULL, 0);
goto done;
}
p += pos;
nlri_len -= pos;
if (peer->capa.mp[AID_INET] == 0) {
log_peer_warnx(&peer->conf,
"bad update, %s disabled", aid2str(AID_INET));
rde_update_err(peer, ERR_UPDATE, ERR_UPD_OPTATTR,
NULL, 0);
goto done;
}
rde_update_update(peer, asp, &prefix, prefixlen);
/* max prefix checker */
if (peer->conf.max_prefix &&
peer->prefix_cnt > peer->conf.max_prefix) {
log_peer_warnx(&peer->conf, "prefix limit reached"
" (>%u/%u)", peer->prefix_cnt, peer->conf.max_prefix);
rde_update_err(peer, ERR_CEASE, ERR_CEASE_MAX_PREFIX,
NULL, 0);
goto done;
}
}
/* add MP_REACH_NLRI if available */
if (mpa.reach_len != 0) {
mpp = mpa.reach;
mplen = mpa.reach_len;
memcpy(&afi, mpp, 2);
mpp += 2;
mplen -= 2;
afi = ntohs(afi);
safi = *mpp++;
mplen--;
if (afi2aid(afi, safi, &aid) == -1) {
log_peer_warnx(&peer->conf,
"bad AFI/SAFI pair in update");
rde_update_err(peer, ERR_UPDATE, ERR_UPD_OPTATTR,
NULL, 0);
goto done;
}
if (peer->capa.mp[aid] == 0) {
log_peer_warnx(&peer->conf,
"bad update, %s disabled", aid2str(aid));
rde_update_err(peer, ERR_UPDATE, ERR_UPD_OPTATTR,
NULL, 0);
goto done;
}
/*
* this works because asp is not linked.
* But first unlock the previously locked nexthop.
*/
if (asp->nexthop) {
asp->nexthop->refcnt--;
(void)nexthop_delete(asp->nexthop);
asp->nexthop = NULL;
}
if ((pos = rde_get_mp_nexthop(mpp, mplen, aid, asp)) == -1) {
log_peer_warnx(&peer->conf, "bad nlri prefix");
rde_update_err(peer, ERR_UPDATE, ERR_UPD_OPTATTR,
mpa.reach, mpa.reach_len);
goto done;
}
mpp += pos;
mplen -= pos;
switch (aid) {
case AID_INET6:
while (mplen > 0) {
if ((pos = rde_update_get_prefix6(mpp, mplen,
&prefix, &prefixlen)) == -1) {
log_peer_warnx(&peer->conf,
"bad IPv6 nlri prefix");
rde_update_err(peer, ERR_UPDATE,
ERR_UPD_OPTATTR,
mpa.reach, mpa.reach_len);
goto done;
}
if (prefixlen > 128) {
rde_update_err(peer, ERR_UPDATE,
ERR_UPD_OPTATTR,
mpa.reach, mpa.reach_len);
goto done;
}
mpp += pos;
mplen -= pos;
rde_update_update(peer, asp, &prefix,
prefixlen);
/* max prefix checker */
if (peer->conf.max_prefix &&
peer->prefix_cnt > peer->conf.max_prefix) {
log_peer_warnx(&peer->conf,
"prefix limit reached"
" (>%u/%u)", peer->prefix_cnt,
peer->conf.max_prefix);
rde_update_err(peer, ERR_CEASE,
ERR_CEASE_MAX_PREFIX, NULL, 0);
goto done;
}
}
break;
case AID_VPN_IPv4:
while (mplen > 0) {
if ((pos = rde_update_get_vpn4(mpp, mplen,
&prefix, &prefixlen)) == -1) {
log_peer_warnx(&peer->conf,
"bad VPNv4 nlri prefix");
rde_update_err(peer, ERR_UPDATE,
ERR_UPD_OPTATTR,
mpa.reach, mpa.reach_len);
goto done;
}
if (prefixlen > 32) {
rde_update_err(peer, ERR_UPDATE,
ERR_UPD_OPTATTR,
mpa.reach, mpa.reach_len);
goto done;
}
mpp += pos;
mplen -= pos;
rde_update_update(peer, asp, &prefix,
prefixlen);
/* max prefix checker */
if (peer->conf.max_prefix &&
peer->prefix_cnt > peer->conf.max_prefix) {
log_peer_warnx(&peer->conf,
"prefix limit reached"
" (>%u/%u)", peer->prefix_cnt,
peer->conf.max_prefix);
rde_update_err(peer, ERR_CEASE,
ERR_CEASE_MAX_PREFIX, NULL, 0);
goto done;
}
}
break;
default:
/* silently ignore unsupported multiprotocol AF */
break;
}
}
done:
if (attrpath_len != 0) {
/* unlock the previously locked entry */
if (asp->nexthop) {
asp->nexthop->refcnt--;
(void)nexthop_delete(asp->nexthop);
}
/* free allocated attribute memory that is no longer used */
path_put(asp);
}
return (error);
}
void
rde_update_update(struct rde_peer *peer, struct rde_aspath *asp,
struct bgpd_addr *prefix, u_int8_t prefixlen)
{
struct rde_aspath *fasp;
enum filter_actions action;
int r = 0, f = 0;
u_int16_t i;
peer->prefix_rcvd_update++;
/* add original path to the Adj-RIB-In */
if (peer->conf.softreconfig_in)
r += path_update(&ribs[0].rib, peer, asp, prefix, prefixlen);
for (i = 1; i < rib_size; i++) {
if (*ribs[i].name == '\0')
break;
/* input filter */
action = rde_filter(ribs[i].in_rules, &fasp, peer, asp, prefix,
prefixlen, peer);
if (fasp == NULL)
fasp = asp;
if (action == ACTION_ALLOW) {
rde_update_log("update", i, peer,
&fasp->nexthop->exit_nexthop, prefix, prefixlen);
r += path_update(&ribs[i].rib, peer, fasp, prefix,
prefixlen);
} else if (prefix_remove(&ribs[i].rib, peer, prefix, prefixlen,
0)) {
rde_update_log("filtered withdraw", i, peer,
NULL, prefix, prefixlen);
f++;
}
/* free modified aspath */
if (fasp != asp)
path_put(fasp);
}
if (r)
peer->prefix_cnt++;
else if (f)
peer->prefix_cnt--;
}
void
rde_update_withdraw(struct rde_peer *peer, struct bgpd_addr *prefix,
u_int8_t prefixlen)
{
int r = 0;
u_int16_t i;
peer->prefix_rcvd_withdraw++;
for (i = rib_size - 1; ; i--) {
if (*ribs[i].name == '\0')
break;
if (prefix_remove(&ribs[i].rib, peer, prefix, prefixlen, 0)) {
rde_update_log("withdraw", i, peer, NULL, prefix,
prefixlen);
r++;
}
if (i == 0)
break;
}
if (r)
peer->prefix_cnt--;
}
/*
* BGP UPDATE parser functions
*/
/* attribute parser specific makros */
#define UPD_READ(t, p, plen, n) \
do { \
memcpy(t, p, n); \
p += n; \
plen += n; \
} while (0)
#define CHECK_FLAGS(s, t, m) \
(((s) & ~(ATTR_DEFMASK | (m))) == (t))
int
rde_attr_parse(u_char *p, u_int16_t len, struct rde_peer *peer,
struct rde_aspath *a, struct mpattr *mpa)
{
struct bgpd_addr nexthop;
u_char *op = p, *npath;
u_int32_t tmp32;
int error;
u_int16_t attr_len, nlen;
u_int16_t plen = 0;
u_int8_t flags;
u_int8_t type;
u_int8_t tmp8;
if (len < 3) {
bad_len:
rde_update_err(peer, ERR_UPDATE, ERR_UPD_ATTRLEN, op, len);
return (-1);
}
UPD_READ(&flags, p, plen, 1);
UPD_READ(&type, p, plen, 1);
if (flags & ATTR_EXTLEN) {
if (len - plen < 2)
goto bad_len;
UPD_READ(&attr_len, p, plen, 2);
attr_len = ntohs(attr_len);
} else {
UPD_READ(&tmp8, p, plen, 1);
attr_len = tmp8;
}
if (len - plen < attr_len)
goto bad_len;
/* adjust len to the actual attribute size including header */
len = plen + attr_len;
switch (type) {
case ATTR_UNDEF:
/* ignore and drop path attributes with a type code of 0 */
plen += attr_len;
break;
case ATTR_ORIGIN:
if (attr_len != 1)
goto bad_len;
if (!CHECK_FLAGS(flags, ATTR_WELL_KNOWN, 0)) {
bad_flags:
rde_update_err(peer, ERR_UPDATE, ERR_UPD_ATTRFLAGS,
op, len);
return (-1);
}
UPD_READ(&a->origin, p, plen, 1);
if (a->origin > ORIGIN_INCOMPLETE) {
rde_update_err(peer, ERR_UPDATE, ERR_UPD_ORIGIN,
op, len);
return (-1);
}
if (a->flags & F_ATTR_ORIGIN)
goto bad_list;
a->flags |= F_ATTR_ORIGIN;
break;
case ATTR_ASPATH:
if (!CHECK_FLAGS(flags, ATTR_WELL_KNOWN, 0))
goto bad_flags;
error = aspath_verify(p, attr_len, rde_as4byte(peer));
if (error == AS_ERR_SOFT) {
/*
* soft errors like unexpected segment types are
* not considered fatal and the path is just
* marked invalid.
*/
a->flags |= F_ATTR_PARSE_ERR;
log_peer_warnx(&peer->conf, "bad ASPATH, "
"path invalidated and prefix withdrawn");
} else if (error != 0) {
rde_update_err(peer, ERR_UPDATE, ERR_UPD_ASPATH,
NULL, 0);
return (-1);
}
if (a->flags & F_ATTR_ASPATH)
goto bad_list;
if (rde_as4byte(peer)) {
npath = p;
nlen = attr_len;
} else
npath = aspath_inflate(p, attr_len, &nlen);
a->flags |= F_ATTR_ASPATH;
a->aspath = aspath_get(npath, nlen);
if (npath != p)
free(npath);
plen += attr_len;
break;
case ATTR_NEXTHOP:
if (attr_len != 4)
goto bad_len;
if (!CHECK_FLAGS(flags, ATTR_WELL_KNOWN, 0))
goto bad_flags;
if (a->flags & F_ATTR_NEXTHOP)
goto bad_list;
a->flags |= F_ATTR_NEXTHOP;
bzero(&nexthop, sizeof(nexthop));
nexthop.aid = AID_INET;
UPD_READ(&nexthop.v4.s_addr, p, plen, 4);
/*
* Check if the nexthop is a valid IP address. We consider
* multicast and experimental addresses as invalid.
*/
tmp32 = ntohl(nexthop.v4.s_addr);
if (IN_MULTICAST(tmp32) || IN_BADCLASS(tmp32)) {
rde_update_err(peer, ERR_UPDATE, ERR_UPD_NETWORK,
op, len);
return (-1);
}
a->nexthop = nexthop_get(&nexthop);
/*
* lock the nexthop because it is not yet linked else
* withdraws may remove this nexthop which in turn would
* cause a use after free error.
*/
a->nexthop->refcnt++;
break;
case ATTR_MED:
if (attr_len != 4)
goto bad_len;
if (!CHECK_FLAGS(flags, ATTR_OPTIONAL, 0))
goto bad_flags;
if (a->flags & F_ATTR_MED)
goto bad_list;
a->flags |= F_ATTR_MED;
UPD_READ(&tmp32, p, plen, 4);
a->med = ntohl(tmp32);
break;
case ATTR_LOCALPREF:
if (attr_len != 4)
goto bad_len;
if (!CHECK_FLAGS(flags, ATTR_WELL_KNOWN, 0))
goto bad_flags;
if (peer->conf.ebgp) {
/* ignore local-pref attr on non ibgp peers */
plen += 4;
break;
}
if (a->flags & F_ATTR_LOCALPREF)
goto bad_list;
a->flags |= F_ATTR_LOCALPREF;
UPD_READ(&tmp32, p, plen, 4);
a->lpref = ntohl(tmp32);
break;
case ATTR_ATOMIC_AGGREGATE:
if (attr_len != 0)
goto bad_len;
if (!CHECK_FLAGS(flags, ATTR_WELL_KNOWN, 0))
goto bad_flags;
goto optattr;
case ATTR_AGGREGATOR:
if ((!rde_as4byte(peer) && attr_len != 6) ||
(rde_as4byte(peer) && attr_len != 8)) {
/*
* ignore attribute in case of error as per
* RFC 7606
*/
log_peer_warnx(&peer->conf, "bad AGGREGATOR, "
"partial attribute ignored");
plen += attr_len;
break;
}
if (!CHECK_FLAGS(flags, ATTR_OPTIONAL|ATTR_TRANSITIVE,
ATTR_PARTIAL))
goto bad_flags;
if (!rde_as4byte(peer)) {
/* need to inflate aggregator AS to 4-byte */
u_char t[8];
t[0] = t[1] = 0;
UPD_READ(&t[2], p, plen, 2);
UPD_READ(&t[4], p, plen, 4);
if (attr_optadd(a, flags, type, t,
sizeof(t)) == -1)
goto bad_list;
break;
}
/* 4-byte ready server take the default route */
goto optattr;
case ATTR_COMMUNITIES:
if (attr_len == 0 || attr_len % 4 != 0) {
/*
* mark update as bad and withdraw all routes as per
* RFC 7606
*/
a->flags |= F_ATTR_PARSE_ERR;
log_peer_warnx(&peer->conf, "bad COMMUNITIES, "
"path invalidated and prefix withdrawn");
}
if (!CHECK_FLAGS(flags, ATTR_OPTIONAL|ATTR_TRANSITIVE,
ATTR_PARTIAL))
goto bad_flags;
goto optattr;
case ATTR_LARGE_COMMUNITIES:
if (attr_len == 0 || attr_len % 12 != 0) {
/*
* mark update as bad and withdraw all routes as per
* RFC 7606
*/
a->flags |= F_ATTR_PARSE_ERR;
log_peer_warnx(&peer->conf, "bad LARGE COMMUNITIES, "
"path invalidated and prefix withdrawn");
}
if (!CHECK_FLAGS(flags, ATTR_OPTIONAL|ATTR_TRANSITIVE,
ATTR_PARTIAL))
goto bad_flags;
goto optattr;
case ATTR_EXT_COMMUNITIES:
if (attr_len == 0 || attr_len % 8 != 0) {
/*
* mark update as bad and withdraw all routes as per
* RFC 7606
*/
a->flags |= F_ATTR_PARSE_ERR;
log_peer_warnx(&peer->conf, "bad EXT_COMMUNITIES, "
"path invalidated and prefix withdrawn");
}
if (!CHECK_FLAGS(flags, ATTR_OPTIONAL|ATTR_TRANSITIVE,
ATTR_PARTIAL))
goto bad_flags;
goto optattr;
case ATTR_ORIGINATOR_ID:
if (attr_len != 4)
goto bad_len;
if (!CHECK_FLAGS(flags, ATTR_OPTIONAL, 0))
goto bad_flags;
goto optattr;
case ATTR_CLUSTER_LIST:
if (attr_len % 4 != 0)
goto bad_len;
if (!CHECK_FLAGS(flags, ATTR_OPTIONAL, 0))
goto bad_flags;
goto optattr;
case ATTR_MP_REACH_NLRI:
if (attr_len < 4)
goto bad_len;
if (!CHECK_FLAGS(flags, ATTR_OPTIONAL, 0))
goto bad_flags;
/* the validity is checked in rde_update_dispatch() */
if (a->flags & F_ATTR_MP_REACH)
goto bad_list;
a->flags |= F_ATTR_MP_REACH;
mpa->reach = p;
mpa->reach_len = attr_len;
plen += attr_len;
break;
case ATTR_MP_UNREACH_NLRI:
if (attr_len < 3)
goto bad_len;
if (!CHECK_FLAGS(flags, ATTR_OPTIONAL, 0))
goto bad_flags;
/* the validity is checked in rde_update_dispatch() */
if (a->flags & F_ATTR_MP_UNREACH)
goto bad_list;
a->flags |= F_ATTR_MP_UNREACH;
mpa->unreach = p;
mpa->unreach_len = attr_len;
plen += attr_len;
break;
case ATTR_AS4_AGGREGATOR:
if (attr_len != 8) {
/* see ATTR_AGGREGATOR ... */
if ((flags & ATTR_PARTIAL) == 0)
goto bad_len;
log_peer_warnx(&peer->conf, "bad AS4_AGGREGATOR, "
"partial attribute ignored");
plen += attr_len;
break;
}
if (!CHECK_FLAGS(flags, ATTR_OPTIONAL|ATTR_TRANSITIVE,
ATTR_PARTIAL))
goto bad_flags;
a->flags |= F_ATTR_AS4BYTE_NEW;
goto optattr;
case ATTR_AS4_PATH:
if (!CHECK_FLAGS(flags, ATTR_OPTIONAL|ATTR_TRANSITIVE,
ATTR_PARTIAL))
goto bad_flags;
if ((error = aspath_verify(p, attr_len, 1)) != 0) {
/*
* XXX RFC does not specify how to handle errors.
* XXX Instead of dropping the session because of a
* XXX bad path just mark the full update as having
* XXX a parse error which makes the update no longer
* XXX eligible and will not be considered for routing
* XXX or redistribution.
* XXX We follow draft-ietf-idr-optional-transitive
* XXX by looking at the partial bit.
* XXX Consider soft errors similar to a partial attr.
*/
if (flags & ATTR_PARTIAL || error == AS_ERR_SOFT) {
a->flags |= F_ATTR_PARSE_ERR;
log_peer_warnx(&peer->conf, "bad AS4_PATH, "
"path invalidated and prefix withdrawn");
goto optattr;
} else {
rde_update_err(peer, ERR_UPDATE, ERR_UPD_ASPATH,
NULL, 0);
return (-1);
}
}
a->flags |= F_ATTR_AS4BYTE_NEW;
goto optattr;
default:
if ((flags & ATTR_OPTIONAL) == 0) {
rde_update_err(peer, ERR_UPDATE, ERR_UPD_UNKNWN_WK_ATTR,
op, len);
return (-1);
}
optattr:
if (attr_optadd(a, flags, type, p, attr_len) == -1) {
bad_list:
rde_update_err(peer, ERR_UPDATE, ERR_UPD_ATTRLIST,
NULL, 0);
return (-1);
}
plen += attr_len;
break;
}
return (plen);
}
int
rde_attr_add(struct rde_aspath *a, u_char *p, u_int16_t len)
{
u_int16_t attr_len;
u_int16_t plen = 0;
u_int8_t flags;
u_int8_t type;
u_int8_t tmp8;
if (a == NULL) /* no aspath, nothing to do */
return (0);
if (len < 3)
return (-1);
UPD_READ(&flags, p, plen, 1);
UPD_READ(&type, p, plen, 1);
if (flags & ATTR_EXTLEN) {
if (len - plen < 2)
return (-1);
UPD_READ(&attr_len, p, plen, 2);
attr_len = ntohs(attr_len);
} else {
UPD_READ(&tmp8, p, plen, 1);
attr_len = tmp8;
}
if (len - plen < attr_len)
return (-1);
if (attr_optadd(a, flags, type, p, attr_len) == -1)
return (-1);
return (0);
}
#undef UPD_READ
#undef CHECK_FLAGS
u_int8_t
rde_attr_missing(struct rde_aspath *a, int ebgp, u_int16_t nlrilen)
{
/* ATTR_MP_UNREACH_NLRI may be sent alone */
if (nlrilen == 0 && a->flags & F_ATTR_MP_UNREACH &&
(a->flags & F_ATTR_MP_REACH) == 0)
return (0);
if ((a->flags & F_ATTR_ORIGIN) == 0)
return (ATTR_ORIGIN);
if ((a->flags & F_ATTR_ASPATH) == 0)
return (ATTR_ASPATH);
if ((a->flags & F_ATTR_MP_REACH) == 0 &&
(a->flags & F_ATTR_NEXTHOP) == 0)
return (ATTR_NEXTHOP);
if (!ebgp)
if ((a->flags & F_ATTR_LOCALPREF) == 0)
return (ATTR_LOCALPREF);
return (0);
}
int
rde_get_mp_nexthop(u_char *data, u_int16_t len, u_int8_t aid,
struct rde_aspath *asp)
{
struct bgpd_addr nexthop;
u_int8_t totlen, nhlen;
if (len == 0)
return (-1);
nhlen = *data++;
totlen = 1;
len--;
if (nhlen > len)
return (-1);
bzero(&nexthop, sizeof(nexthop));
nexthop.aid = aid;
switch (aid) {
case AID_INET6:
/*
* RFC2545 describes that there may be a link-local
* address carried in nexthop. Yikes!
* This is not only silly, it is wrong and we just ignore
* this link-local nexthop. The bgpd session doesn't run
* over the link-local address so why should all other
* traffic.
*/
if (nhlen != 16 && nhlen != 32) {
log_warnx("bad multiprotocol nexthop, bad size");
return (-1);
}
memcpy(&nexthop.v6.s6_addr, data, 16);
break;
case AID_VPN_IPv4:
/*
* Neither RFC4364 nor RFC3107 specify the format of the
* nexthop in an explicit way. The quality of RFC went down
* the toilet the larger the number got.
* RFC4364 is very confusing about VPN-IPv4 address and the
* VPN-IPv4 prefix that carries also a MPLS label.
* So the nexthop is a 12-byte address with a 64bit RD and
* an IPv4 address following. In the nexthop case the RD can
* be ignored.
* Since the nexthop has to be in the main IPv4 table just
* create an AID_INET nexthop. So we don't need to handle
* AID_VPN_IPv4 in nexthop and kroute.
*/
if (nhlen != 12) {
log_warnx("bad multiprotocol nexthop, bad size");
return (-1);
}
data += sizeof(u_int64_t);
nexthop.aid = AID_INET;
memcpy(&nexthop.v4, data, sizeof(nexthop.v4));
break;
default:
log_warnx("bad multiprotocol nexthop, bad AID");
return (-1);
}
asp->nexthop = nexthop_get(&nexthop);
/*
* lock the nexthop because it is not yet linked else
* withdraws may remove this nexthop which in turn would
* cause a use after free error.
*/
asp->nexthop->refcnt++;
/* ignore reserved (old SNPA) field as per RFC4760 */
totlen += nhlen + 1;
data += nhlen + 1;
return (totlen);
}
int
rde_update_extract_prefix(u_char *p, u_int16_t len, void *va,
u_int8_t pfxlen, u_int8_t max)
{
static u_char addrmask[] = {
0x00, 0x80, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc, 0xfe, 0xff };
u_char *a = va;
int i;
u_int16_t plen = 0;
for (i = 0; pfxlen && i < max; i++) {
if (len <= plen)
return (-1);
if (pfxlen < 8) {
a[i] = *p++ & addrmask[pfxlen];
plen++;
break;
} else {
a[i] = *p++;
plen++;
pfxlen -= 8;
}
}
return (plen);
}
int
rde_update_get_prefix(u_char *p, u_int16_t len, struct bgpd_addr *prefix,
u_int8_t *prefixlen)
{
u_int8_t pfxlen;
int plen;
if (len < 1)
return (-1);
pfxlen = *p++;
len--;
bzero(prefix, sizeof(struct bgpd_addr));
prefix->aid = AID_INET;
*prefixlen = pfxlen;
if ((plen = rde_update_extract_prefix(p, len, &prefix->v4, pfxlen,
sizeof(prefix->v4))) == -1)
return (-1);
return (plen + 1); /* pfxlen needs to be added */
}
int
rde_update_get_prefix6(u_char *p, u_int16_t len, struct bgpd_addr *prefix,
u_int8_t *prefixlen)
{
int plen;
u_int8_t pfxlen;
if (len < 1)
return (-1);
pfxlen = *p++;
len--;
bzero(prefix, sizeof(struct bgpd_addr));
prefix->aid = AID_INET6;
*prefixlen = pfxlen;
if ((plen = rde_update_extract_prefix(p, len, &prefix->v6, pfxlen,
sizeof(prefix->v6))) == -1)
return (-1);
return (plen + 1); /* pfxlen needs to be added */
}
int
rde_update_get_vpn4(u_char *p, u_int16_t len, struct bgpd_addr *prefix,
u_int8_t *prefixlen)
{
int rv, done = 0;
u_int8_t pfxlen;
u_int16_t plen;
if (len < 1)
return (-1);
memcpy(&pfxlen, p, 1);
p += 1;
plen = 1;
bzero(prefix, sizeof(struct bgpd_addr));
/* label stack */
do {
if (len - plen < 3 || pfxlen < 3 * 8)
return (-1);
if (prefix->vpn4.labellen + 3U >
sizeof(prefix->vpn4.labelstack))
return (-1);
prefix->vpn4.labelstack[prefix->vpn4.labellen++] = *p++;
prefix->vpn4.labelstack[prefix->vpn4.labellen++] = *p++;
prefix->vpn4.labelstack[prefix->vpn4.labellen] = *p++;
if (prefix->vpn4.labelstack[prefix->vpn4.labellen] &
BGP_MPLS_BOS)
done = 1;
prefix->vpn4.labellen++;
plen += 3;
pfxlen -= 3 * 8;
} while (!done);
/* RD */
if (len - plen < (int)sizeof(u_int64_t) ||
pfxlen < sizeof(u_int64_t) * 8)
return (-1);
memcpy(&prefix->vpn4.rd, p, sizeof(u_int64_t));
pfxlen -= sizeof(u_int64_t) * 8;
p += sizeof(u_int64_t);
plen += sizeof(u_int64_t);
/* prefix */
prefix->aid = AID_VPN_IPv4;
*prefixlen = pfxlen;
if ((rv = rde_update_extract_prefix(p, len, &prefix->vpn4.addr,
pfxlen, sizeof(prefix->vpn4.addr))) == -1)
return (-1);
return (plen + rv);
}
void
rde_update_err(struct rde_peer *peer, u_int8_t error, u_int8_t suberr,
void *data, u_int16_t size)
{
struct ibuf *wbuf;
if ((wbuf = imsg_create(ibuf_se, IMSG_UPDATE_ERR, peer->conf.id, 0,
size + sizeof(error) + sizeof(suberr))) == NULL)
fatal("%s %d imsg_create error", __func__, __LINE__);
if (imsg_add(wbuf, &error, sizeof(error)) == -1 ||
imsg_add(wbuf, &suberr, sizeof(suberr)) == -1 ||
imsg_add(wbuf, data, size) == -1)
fatal("%s %d imsg_add error", __func__, __LINE__);
imsg_close(ibuf_se, wbuf);
peer->state = PEER_ERR;
}
void
rde_update_log(const char *message, u_int16_t rid,
const struct rde_peer *peer, const struct bgpd_addr *next,
const struct bgpd_addr *prefix, u_int8_t prefixlen)
{
char *l = NULL;
char *n = NULL;
char *p = NULL;
if ( !((conf->log & BGPD_LOG_UPDATES) ||
(peer->conf.flags & PEERFLAG_LOG_UPDATES)) )
return;
if (next != NULL)
if (asprintf(&n, " via %s", log_addr(next)) == -1)
n = NULL;
if (asprintf(&p, "%s/%u", log_addr(prefix), prefixlen) == -1)
p = NULL;
l = log_fmt_peer(&peer->conf);
log_info("Rib %s: %s AS%s: %s %s%s", ribs[rid].name,
l, log_as(peer->conf.remote_as), message,
p ? p : "out of memory", n ? n : "");
free(l);
free(n);
free(p);
}
/*
* 4-Byte ASN helper function.
* Two scenarios need to be considered:
* - NEW session with NEW attributes present -> just remove the attributes
* - OLD session with NEW attributes present -> try to merge them
*/
void
rde_as4byte_fixup(struct rde_peer *peer, struct rde_aspath *a)
{
struct attr *nasp, *naggr, *oaggr;
u_int32_t as;
/*
* if either ATTR_AS4_AGGREGATOR or ATTR_AS4_PATH is present
* try to fixup the attributes.
* Do not fixup if F_ATTR_PARSE_ERR is set.
*/
if (!(a->flags & F_ATTR_AS4BYTE_NEW) || a->flags & F_ATTR_PARSE_ERR)
return;
/* first get the attributes */
nasp = attr_optget(a, ATTR_AS4_PATH);
naggr = attr_optget(a, ATTR_AS4_AGGREGATOR);
if (rde_as4byte(peer)) {
/* NEW session using 4-byte ASNs */
if (nasp) {
log_peer_warnx(&peer->conf, "uses 4-byte ASN "
"but sent AS4_PATH attribute.");
attr_free(a, nasp);
}
if (naggr) {
log_peer_warnx(&peer->conf, "uses 4-byte ASN "
"but sent AS4_AGGREGATOR attribute.");
attr_free(a, naggr);
}
return;
}
/* OLD session using 2-byte ASNs */
/* try to merge the new attributes into the old ones */
if ((oaggr = attr_optget(a, ATTR_AGGREGATOR))) {
memcpy(&as, oaggr->data, sizeof(as));
if (ntohl(as) != AS_TRANS) {
/* per RFC ignore AS4_PATH and AS4_AGGREGATOR */
if (nasp)
attr_free(a, nasp);
if (naggr)
attr_free(a, naggr);
return;
}
if (naggr) {
/* switch over to new AGGREGATOR */
attr_free(a, oaggr);
if (attr_optadd(a, ATTR_OPTIONAL | ATTR_TRANSITIVE,
ATTR_AGGREGATOR, naggr->data, naggr->len))
fatalx("attr_optadd failed but impossible");
}
}
/* there is no need for AS4_AGGREGATOR any more */
if (naggr)
attr_free(a, naggr);
/* merge AS4_PATH with ASPATH */
if (nasp)
aspath_merge(a, nasp);
}
/*
* route reflector helper function
*/
void
rde_reflector(struct rde_peer *peer, struct rde_aspath *asp)
{
struct attr *a;
u_int8_t *p;
u_int16_t len;
u_int32_t id;
/* do not consider updates with parse errors */
if (asp->flags & F_ATTR_PARSE_ERR)
return;
/* check for originator id if eq router_id drop */
if ((a = attr_optget(asp, ATTR_ORIGINATOR_ID)) != NULL) {
if (memcmp(&conf->bgpid, a->data, sizeof(conf->bgpid)) == 0) {
/* this is coming from myself */
asp->flags |= F_ATTR_LOOP;
return;
}
} else if (conf->flags & BGPD_FLAG_REFLECTOR) {
if (peer->conf.ebgp)
id = conf->bgpid;
else
id = htonl(peer->remote_bgpid);
if (attr_optadd(asp, ATTR_OPTIONAL, ATTR_ORIGINATOR_ID,
&id, sizeof(u_int32_t)) == -1)
fatalx("attr_optadd failed but impossible");
}
/* check for own id in the cluster list */
if (conf->flags & BGPD_FLAG_REFLECTOR) {
if ((a = attr_optget(asp, ATTR_CLUSTER_LIST)) != NULL) {
for (len = 0; len < a->len;
len += sizeof(conf->clusterid))
/* check if coming from my cluster */
if (memcmp(&conf->clusterid, a->data + len,
sizeof(conf->clusterid)) == 0) {
asp->flags |= F_ATTR_LOOP;
return;
}
/* prepend own clusterid by replacing attribute */
len = a->len + sizeof(conf->clusterid);
if (len < a->len)
fatalx("rde_reflector: cluster-list overflow");
if ((p = malloc(len)) == NULL)
fatal("rde_reflector");
memcpy(p, &conf->clusterid, sizeof(conf->clusterid));
memcpy(p + sizeof(conf->clusterid), a->data, a->len);
attr_free(asp, a);
if (attr_optadd(asp, ATTR_OPTIONAL, ATTR_CLUSTER_LIST,
p, len) == -1)
fatalx("attr_optadd failed but impossible");
free(p);
} else if (attr_optadd(asp, ATTR_OPTIONAL, ATTR_CLUSTER_LIST,
&conf->clusterid, sizeof(conf->clusterid)) == -1)
fatalx("attr_optadd failed but impossible");
}
}
/*
* control specific functions
*/
void
rde_dump_rib_as(struct prefix *p, struct rde_aspath *asp, pid_t pid, int flags)
{
struct ctl_show_rib rib;
struct ibuf *wbuf;
struct attr *a;
void *bp;
time_t staletime;
u_int8_t l;
bzero(&rib, sizeof(rib));
rib.lastchange = p->lastchange;
rib.local_pref = asp->lpref;
rib.med = asp->med;
rib.weight = asp->weight;
strlcpy(rib.descr, asp->peer->conf.descr, sizeof(rib.descr));
memcpy(&rib.remote_addr, &asp->peer->remote_addr,
sizeof(rib.remote_addr));
rib.remote_id = asp->peer->remote_bgpid;
if (asp->nexthop != NULL) {
memcpy(&rib.true_nexthop, &asp->nexthop->true_nexthop,
sizeof(rib.true_nexthop));
memcpy(&rib.exit_nexthop, &asp->nexthop->exit_nexthop,
sizeof(rib.exit_nexthop));
} else {
/* announced network may have a NULL nexthop */
bzero(&rib.true_nexthop, sizeof(rib.true_nexthop));
bzero(&rib.exit_nexthop, sizeof(rib.exit_nexthop));
rib.true_nexthop.aid = p->prefix->aid;
rib.exit_nexthop.aid = p->prefix->aid;
}
pt_getaddr(p->prefix, &rib.prefix);
rib.prefixlen = p->prefix->prefixlen;
rib.origin = asp->origin;
rib.flags = 0;
if (p->re->active == p)
rib.flags |= F_PREF_ACTIVE;
if (!asp->peer->conf.ebgp)
rib.flags |= F_PREF_INTERNAL;
if (asp->flags & F_PREFIX_ANNOUNCED)
rib.flags |= F_PREF_ANNOUNCE;
if (asp->nexthop == NULL || asp->nexthop->state == NEXTHOP_REACH)
rib.flags |= F_PREF_ELIGIBLE;
if (asp->flags & F_ATTR_LOOP)
rib.flags &= ~F_PREF_ELIGIBLE;
staletime = asp->peer->staletime[p->prefix->aid];
if (staletime && p->lastchange <= staletime)
rib.flags |= F_PREF_STALE;
rib.aspath_len = aspath_length(asp->aspath);
if ((wbuf = imsg_create(ibuf_se_ctl, IMSG_CTL_SHOW_RIB, 0, pid,
sizeof(rib) + rib.aspath_len)) == NULL)
return;
if (imsg_add(wbuf, &rib, sizeof(rib)) == -1 ||
imsg_add(wbuf, aspath_dump(asp->aspath),
rib.aspath_len) == -1)
return;
imsg_close(ibuf_se_ctl, wbuf);
if (flags & F_CTL_DETAIL)
for (l = 0; l < asp->others_len; l++) {
if ((a = asp->others[l]) == NULL)
break;
if ((wbuf = imsg_create(ibuf_se_ctl,
IMSG_CTL_SHOW_RIB_ATTR, 0, pid,
attr_optlen(a))) == NULL)
return;
if ((bp = ibuf_reserve(wbuf, attr_optlen(a))) == NULL) {
ibuf_free(wbuf);
return;
}
if (attr_write(bp, attr_optlen(a), a->flags,
a->type, a->data, a->len) == -1) {
ibuf_free(wbuf);
return;
}
imsg_close(ibuf_se_ctl, wbuf);
}
}
void
rde_dump_filterout(struct rde_peer *peer, struct prefix *p,
struct ctl_show_rib_request *req)
{
struct bgpd_addr addr;
struct rde_aspath *asp;
enum filter_actions a;
if (up_test_update(peer, p) != 1)
return;
pt_getaddr(p->prefix, &addr);
a = rde_filter(out_rules, &asp, peer, p->aspath, &addr,
p->prefix->prefixlen, p->aspath->peer);
if (asp)
asp->peer = p->aspath->peer;
else
asp = p->aspath;
if (a == ACTION_ALLOW)
rde_dump_rib_as(p, asp, req->pid, req->flags);
if (asp != p->aspath)
path_put(asp);
}
void
rde_dump_filter(struct prefix *p, struct ctl_show_rib_request *req)
{
struct rde_peer *peer;
if (req->flags & F_CTL_ADJ_IN ||
!(req->flags & (F_CTL_ADJ_IN|F_CTL_ADJ_OUT))) {
if (req->peerid && req->peerid != p->aspath->peer->conf.id)
return;
if (req->type == IMSG_CTL_SHOW_RIB_AS &&
!aspath_match(p->aspath->aspath->data,
p->aspath->aspath->len, &req->as, req->as.as))
return;
if (req->type == IMSG_CTL_SHOW_RIB_COMMUNITY &&
!community_match(p->aspath, req->community.as,
req->community.type))
return;
if (req->type == IMSG_CTL_SHOW_RIB_LARGECOMMUNITY &&
!community_large_match(p->aspath, req->large_community.as,
req->large_community.ld1, req->large_community.ld2))
return;
if ((req->flags & F_CTL_ACTIVE) && p->re->active != p)
return;
rde_dump_rib_as(p, p->aspath, req->pid, req->flags);
} else if (req->flags & F_CTL_ADJ_OUT) {
if (p->re->active != p)
/* only consider active prefix */
return;
if (req->peerid) {
if ((peer = peer_get(req->peerid)) != NULL)
rde_dump_filterout(peer, p, req);
return;
}
}
}
void
rde_dump_upcall(struct rib_entry *re, void *ptr)
{
struct prefix *p;
struct rde_dump_ctx *ctx = ptr;
LIST_FOREACH(p, &re->prefix_h, rib_l)
rde_dump_filter(p, &ctx->req);
}
void
rde_dump_prefix_upcall(struct rib_entry *re, void *ptr)
{
struct rde_dump_ctx *ctx = ptr;
struct prefix *p;
struct pt_entry *pt;
struct bgpd_addr addr;
pt = re->prefix;
pt_getaddr(pt, &addr);
if (addr.aid != ctx->req.prefix.aid)
return;
if (ctx->req.prefixlen > pt->prefixlen)
return;
if (!prefix_compare(&ctx->req.prefix, &addr, ctx->req.prefixlen))
LIST_FOREACH(p, &re->prefix_h, rib_l)
rde_dump_filter(p, &ctx->req);
}
void
rde_dump_ctx_new(struct ctl_show_rib_request *req, pid_t pid,
enum imsg_type type)
{
struct rde_dump_ctx *ctx;
struct rib_desc *rib;
struct rib_entry *re;
u_int error;
u_int8_t hostplen;
if ((ctx = calloc(1, sizeof(*ctx))) == NULL) {
log_warn("rde_dump_ctx_new");
error = CTL_RES_NOMEM;
imsg_compose(ibuf_se_ctl, IMSG_CTL_RESULT, 0, pid, -1, &error,
sizeof(error));
return;
}
if ((rib = rib_find(req->rib)) == NULL) {
log_warnx("rde_dump_ctx_new: no such rib %s", req->rib);
error = CTL_RES_NOSUCHPEER;
imsg_compose(ibuf_se_ctl, IMSG_CTL_RESULT, 0, pid, -1, &error,
sizeof(error));
free(ctx);
return;
}
memcpy(&ctx->req, req, sizeof(struct ctl_show_rib_request));
ctx->req.pid = pid;
ctx->req.type = type;
ctx->ribctx.ctx_count = RDE_RUNNER_ROUNDS;
ctx->ribctx.ctx_rib = &rib->rib;
switch (ctx->req.type) {
case IMSG_CTL_SHOW_NETWORK:
ctx->ribctx.ctx_upcall = network_dump_upcall;
break;
case IMSG_CTL_SHOW_RIB:
case IMSG_CTL_SHOW_RIB_AS:
case IMSG_CTL_SHOW_RIB_COMMUNITY:
case IMSG_CTL_SHOW_RIB_LARGECOMMUNITY:
ctx->ribctx.ctx_upcall = rde_dump_upcall;
break;
case IMSG_CTL_SHOW_RIB_PREFIX:
if (req->flags & F_LONGER) {
ctx->ribctx.ctx_upcall = rde_dump_prefix_upcall;
break;
}
switch (req->prefix.aid) {
case AID_INET:
case AID_VPN_IPv4:
hostplen = 32;
break;
case AID_INET6:
hostplen = 128;
break;
default:
fatalx("rde_dump_ctx_new: unknown af");
}
if (req->prefixlen == hostplen)
re = rib_lookup(&rib->rib, &req->prefix);
else
re = rib_get(&rib->rib, &req->prefix, req->prefixlen);
if (re)
rde_dump_upcall(re, ctx);
rde_dump_done(ctx);
return;
default:
fatalx("rde_dump_ctx_new: unsupported imsg type");
}
ctx->ribctx.ctx_done = rde_dump_done;
ctx->ribctx.ctx_arg = ctx;
ctx->ribctx.ctx_aid = ctx->req.aid;
rib_dump_r(&ctx->ribctx);
}
void
rde_dump_done(void *arg)
{
struct rde_dump_ctx *ctx = arg;
imsg_compose(ibuf_se_ctl, IMSG_CTL_END, 0, ctx->req.pid,
-1, NULL, 0);
free(ctx);
}
void
rde_dump_mrt_new(struct mrt *mrt, pid_t pid, int fd)
{
struct rde_mrt_ctx *ctx;
struct rib_desc *rib;
if ((ctx = calloc(1, sizeof(*ctx))) == NULL) {
log_warn("rde_dump_mrt_new");
return;
}
memcpy(&ctx->mrt, mrt, sizeof(struct mrt));
TAILQ_INIT(&ctx->mrt.wbuf.bufs);
ctx->mrt.wbuf.fd = fd;
ctx->mrt.state = MRT_STATE_RUNNING;
rib = rib_find(ctx->mrt.rib);
if (rib == NULL) {
log_warnx("non existing RIB %s for mrt dump", ctx->mrt.rib);
free(ctx);
return;
}
if (ctx->mrt.type == MRT_TABLE_DUMP_V2)
mrt_dump_v2_hdr(&ctx->mrt, conf, &peerlist);
ctx->ribctx.ctx_count = RDE_RUNNER_ROUNDS;
ctx->ribctx.ctx_rib = &rib->rib;
ctx->ribctx.ctx_upcall = mrt_dump_upcall;
ctx->ribctx.ctx_done = mrt_done;
ctx->ribctx.ctx_arg = &ctx->mrt;
ctx->ribctx.ctx_aid = AID_UNSPEC;
LIST_INSERT_HEAD(&rde_mrts, ctx, entry);
rde_mrt_cnt++;
rib_dump_r(&ctx->ribctx);
}
/*
* kroute specific functions
*/
int
rde_rdomain_import(struct rde_aspath *asp, struct rdomain *rd)
{
struct filter_set *s;
TAILQ_FOREACH(s, &rd->import, entry) {
if (community_ext_match(asp, &s->action.ext_community, 0))
return (1);
}
return (0);
}
void
rde_send_kroute(struct prefix *new, struct prefix *old, u_int16_t ribid)
{
struct kroute_full kr;
struct bgpd_addr addr;
struct prefix *p;
struct rdomain *rd;
enum imsg_type type;
/*
* Make sure that self announce prefixes are not committed to the
* FIB. If both prefixes are unreachable no update is needed.
*/
if ((old == NULL || old->aspath->flags & F_PREFIX_ANNOUNCED) &&
(new == NULL || new->aspath->flags & F_PREFIX_ANNOUNCED))
return;
if (new == NULL || new->aspath->flags & F_PREFIX_ANNOUNCED) {
type = IMSG_KROUTE_DELETE;
p = old;
} else {
type = IMSG_KROUTE_CHANGE;
p = new;
}
pt_getaddr(p->prefix, &addr);
bzero(&kr, sizeof(kr));
memcpy(&kr.prefix, &addr, sizeof(kr.prefix));
kr.prefixlen = p->prefix->prefixlen;
if (p->aspath->flags & F_NEXTHOP_REJECT)
kr.flags |= F_REJECT;
if (p->aspath->flags & F_NEXTHOP_BLACKHOLE)
kr.flags |= F_BLACKHOLE;
if (type == IMSG_KROUTE_CHANGE)
memcpy(&kr.nexthop, &p->aspath->nexthop->true_nexthop,
sizeof(kr.nexthop));
strlcpy(kr.label, rtlabel_id2name(p->aspath->rtlabelid),
sizeof(kr.label));
switch (addr.aid) {
case AID_VPN_IPv4:
if (ribid != 1)
/* not Loc-RIB, no update for VPNs */
break;
SIMPLEQ_FOREACH(rd, rdomains_l, entry) {
if (!rde_rdomain_import(p->aspath, rd))
continue;
/* must send exit_nexthop so that correct MPLS tunnel
* is chosen
*/
if (type == IMSG_KROUTE_CHANGE)
memcpy(&kr.nexthop,
&p->aspath->nexthop->exit_nexthop,
sizeof(kr.nexthop));
if (imsg_compose(ibuf_main, type, rd->rtableid, 0, -1,
&kr, sizeof(kr)) == -1)
fatal("%s %d imsg_compose error", __func__,
__LINE__);
}
break;
default:
if (imsg_compose(ibuf_main, type, ribs[ribid].rtableid, 0, -1,
&kr, sizeof(kr)) == -1)
fatal("%s %d imsg_compose error", __func__, __LINE__);
break;
}
}
/*
* pf table specific functions
*/
void
rde_send_pftable(u_int16_t id, struct bgpd_addr *addr,
u_int8_t len, int del)
{
struct pftable_msg pfm;
if (id == 0)
return;
/* do not run while cleaning up */
if (rde_quit)
return;
bzero(&pfm, sizeof(pfm));
strlcpy(pfm.pftable, pftable_id2name(id), sizeof(pfm.pftable));
memcpy(&pfm.addr, addr, sizeof(pfm.addr));
pfm.len = len;
if (imsg_compose(ibuf_main,
del ? IMSG_PFTABLE_REMOVE : IMSG_PFTABLE_ADD,
0, 0, -1, &pfm, sizeof(pfm)) == -1)
fatal("%s %d imsg_compose error", __func__, __LINE__);
}
void
rde_send_pftable_commit(void)
{
/* do not run while cleaning up */
if (rde_quit)
return;
if (imsg_compose(ibuf_main, IMSG_PFTABLE_COMMIT, 0, 0, -1, NULL, 0) ==
-1)
fatal("%s %d imsg_compose error", __func__, __LINE__);
}
/*
* nexthop specific functions
*/
void
rde_send_nexthop(struct bgpd_addr *next, int valid)
{
int type;
if (valid)
type = IMSG_NEXTHOP_ADD;
else
type = IMSG_NEXTHOP_REMOVE;
if (imsg_compose(ibuf_main, type, 0, 0, -1, next,
sizeof(struct bgpd_addr)) == -1)
fatal("%s %d imsg_compose error", __func__, __LINE__);
}
/*
* soft reconfig specific functions
*/
void
rde_reload_done(void)
{
struct rdomain *rd;
struct rde_peer *peer;
struct filter_head *fh;
u_int16_t rid;
/* first merge the main config */
if ((nconf->flags & BGPD_FLAG_NO_EVALUATE)
!= (conf->flags & BGPD_FLAG_NO_EVALUATE)) {
log_warnx("change to/from route-collector "
"mode ignored");
if (conf->flags & BGPD_FLAG_NO_EVALUATE)
nconf->flags |= BGPD_FLAG_NO_EVALUATE;
else
nconf->flags &= ~BGPD_FLAG_NO_EVALUATE;
}
memcpy(conf, nconf, sizeof(struct bgpd_config));
conf->listen_addrs = NULL;
conf->csock = NULL;
conf->rcsock = NULL;
free(nconf);
nconf = NULL;
/* sync peerself with conf */
peerself->remote_bgpid = ntohl(conf->bgpid);
peerself->conf.local_as = conf->as;
peerself->conf.remote_as = conf->as;
peerself->short_as = conf->short_as;
/* apply new set of rdomain, sync will be done later */
while ((rd = SIMPLEQ_FIRST(rdomains_l)) != NULL) {
SIMPLEQ_REMOVE_HEAD(rdomains_l, entry);
filterset_free(&rd->import);
filterset_free(&rd->export);
free(rd);
}
free(rdomains_l);
rdomains_l = newdomains;
/* XXX WHERE IS THE SYNC ??? */
rde_filter_calc_skip_steps(out_rules_tmp);
/*
* make the new filter rules the active one but keep the old for
* softrconfig. This is needed so that changes happening are using
* the right filters.
*/
fh = out_rules;
out_rules = out_rules_tmp;
out_rules_tmp = fh;
/* check if filter changed */
LIST_FOREACH(peer, &peerlist, peer_l) {
if (peer->conf.id == 0)
continue;
peer->reconf_out = 0;
peer->reconf_rib = 0;
if (peer->rib != rib_find(peer->conf.rib)) {
rib_dump(&peer->rib->rib,
rde_softreconfig_unload_peer, peer, AID_UNSPEC);
peer->rib = rib_find(peer->conf.rib);
if (peer->rib == NULL)
fatalx("King Bula's peer met an unknown RIB");
peer->reconf_rib = 1;
continue;
}
if (peer->conf.softreconfig_out &&
!rde_filter_equal(out_rules, out_rules_tmp, peer)) {
peer->reconf_out = 1;
}
}
/* bring ribs in sync */
for (rid = 0; rid < rib_size; rid++) {
if (*ribs[rid].name == '\0')
continue;
rde_filter_calc_skip_steps(ribs[rid].in_rules_tmp);
/* flip rules, make new active */
fh = ribs[rid].in_rules;
ribs[rid].in_rules = ribs[rid].in_rules_tmp;
ribs[rid].in_rules_tmp = fh;
switch (ribs[rid].state) {
case RECONF_DELETE:
rib_free(&ribs[rid]);
break;
case RECONF_KEEP:
if (rde_filter_equal(ribs[rid].in_rules,
ribs[rid].in_rules_tmp, NULL))
/* rib is in sync */
break;
ribs[rid].state = RECONF_RELOAD;
/* FALLTHROUGH */
case RECONF_REINIT:
rib_dump(&ribs[0].rib, rde_softreconfig_in, &ribs[rid],
AID_UNSPEC);
break;
case RECONF_RELOAD:
log_warnx("Bad rib reload state");
/* FALLTHROUGH */
case RECONF_NONE:
break;
}
}
LIST_FOREACH(peer, &peerlist, peer_l) {
if (peer->reconf_out)
rib_dump(&peer->rib->rib, rde_softreconfig_out,
peer, AID_UNSPEC);
else if (peer->reconf_rib)
/* dump the full table to neighbors that changed rib */
peer_dump(peer->conf.id, AID_UNSPEC);
}
filterlist_free(out_rules_tmp);
out_rules_tmp = NULL;
for (rid = 0; rid < rib_size; rid++) {
if (*ribs[rid].name == '\0')
continue;
filterlist_free(ribs[rid].in_rules_tmp);
ribs[rid].in_rules_tmp = NULL;
ribs[rid].state = RECONF_NONE;
}
log_info("RDE reconfigured");
imsg_compose(ibuf_main, IMSG_RECONF_DONE, 0, 0,
-1, NULL, 0);
}
void
rde_softreconfig_in(struct rib_entry *re, void *ptr)
{
struct rib_desc *rib = ptr;
struct prefix *p, *np;
struct pt_entry *pt;
struct rde_peer *peer;
struct rde_aspath *asp, *oasp, *nasp;
enum filter_actions oa, na;
struct bgpd_addr addr;
pt = re->prefix;
pt_getaddr(pt, &addr);
for (p = LIST_FIRST(&re->prefix_h); p != NULL; p = np) {
/*
* prefix_remove() and path_update() may change the object
* so cache the values.
*/
np = LIST_NEXT(p, rib_l);
asp = p->aspath;
peer = asp->peer;
/* check if prefix changed */
if (rib->state == RECONF_RELOAD) {
oa = rde_filter(rib->in_rules_tmp, &oasp, peer,
asp, &addr, pt->prefixlen, peer);
oasp = oasp != NULL ? oasp : asp;
} else {
/* make sure we update everything for RECONF_REINIT */
oa = ACTION_DENY;
oasp = asp;
}
na = rde_filter(rib->in_rules, &nasp, peer, asp,
&addr, pt->prefixlen, peer);
nasp = nasp != NULL ? nasp : asp;
/* go through all 4 possible combinations */
/* if (oa == ACTION_DENY && na == ACTION_DENY) */
/* nothing todo */
if (oa == ACTION_DENY && na == ACTION_ALLOW) {
/* update Local-RIB */
path_update(&rib->rib, peer, nasp, &addr,
pt->prefixlen);
} else if (oa == ACTION_ALLOW && na == ACTION_DENY) {
/* remove from Local-RIB */
prefix_remove(&rib->rib, peer, &addr, pt->prefixlen, 0);
} else if (oa == ACTION_ALLOW && na == ACTION_ALLOW) {
if (path_compare(nasp, oasp) != 0)
/* send update */
path_update(&rib->rib, peer, nasp, &addr,
pt->prefixlen);
}
if (oasp != asp)
path_put(oasp);
if (nasp != asp)
path_put(nasp);
}
}
void
rde_softreconfig_out(struct rib_entry *re, void *ptr)
{
struct prefix *p = re->active;
struct pt_entry *pt;
struct rde_peer *peer = ptr;
struct rde_aspath *oasp, *nasp;
enum filter_actions oa, na;
struct bgpd_addr addr;
if (peer->conf.id == 0)
fatalx("King Bula troubled by bad peer");
if (p == NULL)
return;
pt = re->prefix;
pt_getaddr(pt, &addr);
if (up_test_update(peer, p) != 1)
return;
oa = rde_filter(out_rules_tmp, &oasp, peer, p->aspath,
&addr, pt->prefixlen, p->aspath->peer);
na = rde_filter(out_rules, &nasp, peer, p->aspath,
&addr, pt->prefixlen, p->aspath->peer);
oasp = oasp != NULL ? oasp : p->aspath;
nasp = nasp != NULL ? nasp : p->aspath;
/* go through all 4 possible combinations */
/* if (oa == ACTION_DENY && na == ACTION_DENY) */
/* nothing todo */
if (oa == ACTION_DENY && na == ACTION_ALLOW) {
/* send update */
up_generate(peer, nasp, &addr, pt->prefixlen);
} else if (oa == ACTION_ALLOW && na == ACTION_DENY) {
/* send withdraw */
up_generate(peer, NULL, &addr, pt->prefixlen);
} else if (oa == ACTION_ALLOW && na == ACTION_ALLOW) {
/* send update if path attributes changed */
if (path_compare(nasp, oasp) != 0)
up_generate(peer, nasp, &addr, pt->prefixlen);
}
if (oasp != p->aspath)
path_put(oasp);
if (nasp != p->aspath)
path_put(nasp);
}
void
rde_softreconfig_unload_peer(struct rib_entry *re, void *ptr)
{
struct rde_peer *peer = ptr;
struct prefix *p = re->active;
struct pt_entry *pt;
struct rde_aspath *oasp;
enum filter_actions oa;
struct bgpd_addr addr;
pt = re->prefix;
pt_getaddr(pt, &addr);
/* check if prefix was announced */
if (up_test_update(peer, p) != 1)
return;
oa = rde_filter(out_rules_tmp, &oasp, peer, p->aspath,
&addr, pt->prefixlen, p->aspath->peer);
oasp = oasp != NULL ? oasp : p->aspath;
if (oa == ACTION_DENY)
/* nothing todo */
goto done;
/* send withdraw */
up_generate(peer, NULL, &addr, pt->prefixlen);
done:
if (oasp != p->aspath)
path_put(oasp);
}
/*
* update specific functions
*/
u_char queue_buf[4096];
void
rde_up_dump_upcall(struct rib_entry *re, void *ptr)
{
struct rde_peer *peer = ptr;
if (re_rib(re) != &peer->rib->rib)
fatalx("King Bula: monstrous evil horror.");
if (re->active == NULL)
return;
up_generate_updates(out_rules, peer, re->active, NULL);
}
void
rde_generate_updates(u_int16_t ribid, struct prefix *new, struct prefix *old)
{
struct rde_peer *peer;
/*
* If old is != NULL we know it was active and should be removed.
* If new is != NULL we know it is reachable and then we should
* generate an update.
*/
if (old == NULL && new == NULL)
return;
LIST_FOREACH(peer, &peerlist, peer_l) {
if (peer->conf.id == 0)
continue;
if (peer->rib->rib.id != ribid)
continue;
if (peer->state != PEER_UP)
continue;
up_generate_updates(out_rules, peer, new, old);
}
}
void
rde_update_queue_runner(void)
{
struct rde_peer *peer;
int r, sent, max = RDE_RUNNER_ROUNDS, eor = 0;
u_int16_t len, wd_len, wpos;
len = sizeof(queue_buf) - MSGSIZE_HEADER;
do {
sent = 0;
LIST_FOREACH(peer, &peerlist, peer_l) {
if (peer->conf.id == 0)
continue;
if (peer->state != PEER_UP)
continue;
/* first withdraws */
wpos = 2; /* reserve space for the length field */
r = up_dump_prefix(queue_buf + wpos, len - wpos - 2,
&peer->withdraws[AID_INET], peer);
wd_len = r;
/* write withdraws length filed */
wd_len = htons(wd_len);
memcpy(queue_buf, &wd_len, 2);
wpos += r;
/* now bgp path attributes */
r = up_dump_attrnlri(queue_buf + wpos, len - wpos,
peer);
switch (r) {
case -1:
eor = 1;
if (wd_len == 0) {
/* no withdraws queued just send EoR */
peer_send_eor(peer, AID_INET);
continue;
}
break;
case 2:
if (wd_len == 0) {
/*
* No packet to send. No withdraws and
* no path attributes. Skip.
*/
continue;
}
/* FALLTHROUGH */
default:
wpos += r;
break;
}
/* finally send message to SE */
if (imsg_compose(ibuf_se, IMSG_UPDATE, peer->conf.id,
0, -1, queue_buf, wpos) == -1)
fatal("%s %d imsg_compose error", __func__,
__LINE__);
sent++;
if (eor) {
eor = 0;
peer_send_eor(peer, AID_INET);
}
}
max -= sent;
} while (sent != 0 && max > 0);
}
void
rde_update6_queue_runner(u_int8_t aid)
{
struct rde_peer *peer;
u_char *b;
int r, sent, max = RDE_RUNNER_ROUNDS / 2;
u_int16_t len;
/* first withdraws ... */
do {
sent = 0;
LIST_FOREACH(peer, &peerlist, peer_l) {
if (peer->conf.id == 0)
continue;
if (peer->state != PEER_UP)
continue;
len = sizeof(queue_buf) - MSGSIZE_HEADER;
b = up_dump_mp_unreach(queue_buf, &len, peer, aid);
if (b == NULL)
continue;
/* finally send message to SE */
if (imsg_compose(ibuf_se, IMSG_UPDATE, peer->conf.id,
0, -1, b, len) == -1)
fatal("%s %d imsg_compose error", __func__,
__LINE__);
sent++;
}
max -= sent;
} while (sent != 0 && max > 0);
/* ... then updates */
max = RDE_RUNNER_ROUNDS / 2;
do {
sent = 0;
LIST_FOREACH(peer, &peerlist, peer_l) {
if (peer->conf.id == 0)
continue;
if (peer->state != PEER_UP)
continue;
len = sizeof(queue_buf) - MSGSIZE_HEADER;
r = up_dump_mp_reach(queue_buf, &len, peer, aid);
switch (r) {
case -2:
continue;
case -1:
peer_send_eor(peer, aid);
continue;
default:
b = queue_buf + r;
break;
}
/* finally send message to SE */
if (imsg_compose(ibuf_se, IMSG_UPDATE, peer->conf.id,
0, -1, b, len) == -1)
fatal("%s %d imsg_compose error", __func__,
__LINE__);
sent++;
}
max -= sent;
} while (sent != 0 && max > 0);
}
/*
* generic helper function
*/
u_int32_t
rde_local_as(void)
{
return (conf->as);
}
int
rde_noevaluate(void)
{
/* do not run while cleaning up */
if (rde_quit)
return (1);
return (conf->flags & BGPD_FLAG_NO_EVALUATE);
}
int
rde_decisionflags(void)
{
return (conf->flags & BGPD_FLAG_DECISION_MASK);
}
int
rde_as4byte(struct rde_peer *peer)
{
return (peer->capa.as4byte);
}
/*
* peer functions
*/
struct peer_table {
struct rde_peer_head *peer_hashtbl;
u_int32_t peer_hashmask;
} peertable;
#define PEER_HASH(x) \
&peertable.peer_hashtbl[(x) & peertable.peer_hashmask]
void
peer_init(u_int32_t hashsize)
{
struct peer_config pc;
u_int32_t hs, i;
for (hs = 1; hs < hashsize; hs <<= 1)
;
peertable.peer_hashtbl = calloc(hs, sizeof(struct rde_peer_head));
if (peertable.peer_hashtbl == NULL)
fatal("peer_init");
for (i = 0; i < hs; i++)
LIST_INIT(&peertable.peer_hashtbl[i]);
LIST_INIT(&peerlist);
peertable.peer_hashmask = hs - 1;
bzero(&pc, sizeof(pc));
snprintf(pc.descr, sizeof(pc.descr), "LOCAL");
peerself = peer_add(0, &pc);
if (peerself == NULL)
fatalx("peer_init add self");
peerself->state = PEER_UP;
}
void
peer_shutdown(void)
{
u_int32_t i;
for (i = 0; i <= peertable.peer_hashmask; i++)
if (!LIST_EMPTY(&peertable.peer_hashtbl[i]))
log_warnx("peer_free: free non-free table");
free(peertable.peer_hashtbl);
}
struct rde_peer *
peer_get(u_int32_t id)
{
struct rde_peer_head *head;
struct rde_peer *peer;
head = PEER_HASH(id);
LIST_FOREACH(peer, head, hash_l) {
if (peer->conf.id == id)
return (peer);
}
return (NULL);
}
struct rde_peer *
peer_add(u_int32_t id, struct peer_config *p_conf)
{
struct rde_peer_head *head;
struct rde_peer *peer;
if ((peer = peer_get(id))) {
memcpy(&peer->conf, p_conf, sizeof(struct peer_config));
return (NULL);
}
peer = calloc(1, sizeof(struct rde_peer));
if (peer == NULL)
fatal("peer_add");
LIST_INIT(&peer->path_h);
memcpy(&peer->conf, p_conf, sizeof(struct peer_config));
peer->remote_bgpid = 0;
peer->rib = rib_find(peer->conf.rib);
if (peer->rib == NULL)
fatalx("King Bula's new peer met an unknown RIB");
peer->state = PEER_NONE;
up_init(peer);
head = PEER_HASH(id);
LIST_INSERT_HEAD(head, peer, hash_l);
LIST_INSERT_HEAD(&peerlist, peer, peer_l);
return (peer);
}
int
peer_localaddrs(struct rde_peer *peer, struct bgpd_addr *laddr)
{
struct ifaddrs *ifap, *ifa, *match;
if (getifaddrs(&ifap) == -1)
fatal("getifaddrs");
for (match = ifap; match != NULL; match = match->ifa_next)
if (sa_cmp(laddr, match->ifa_addr) == 0)
break;
if (match == NULL) {
log_warnx("peer_localaddrs: local address not found");
return (-1);
}
for (ifa = ifap; ifa != NULL; ifa = ifa->ifa_next) {
if (ifa->ifa_addr->sa_family == AF_INET &&
strcmp(ifa->ifa_name, match->ifa_name) == 0) {
if (ifa->ifa_addr->sa_family ==
match->ifa_addr->sa_family)
ifa = match;
sa2addr(ifa->ifa_addr, &peer->local_v4_addr);
break;
}
}
for (ifa = ifap; ifa != NULL; ifa = ifa->ifa_next) {
if (ifa->ifa_addr->sa_family == AF_INET6 &&
strcmp(ifa->ifa_name, match->ifa_name) == 0) {
/*
* only accept global scope addresses except explicitly
* specified.
*/
if (ifa->ifa_addr->sa_family ==
match->ifa_addr->sa_family)
ifa = match;
else if (IN6_IS_ADDR_LINKLOCAL(
&((struct sockaddr_in6 *)ifa->
ifa_addr)->sin6_addr) ||
IN6_IS_ADDR_SITELOCAL(
&((struct sockaddr_in6 *)ifa->
ifa_addr)->sin6_addr))
continue;
sa2addr(ifa->ifa_addr, &peer->local_v6_addr);
break;
}
}
freeifaddrs(ifap);
return (0);
}
void
peer_up(u_int32_t id, struct session_up *sup)
{
struct rde_peer *peer;
u_int8_t i;
peer = peer_get(id);
if (peer == NULL) {
log_warnx("peer_up: unknown peer id %d", id);
return;
}
if (peer->state != PEER_DOWN && peer->state != PEER_NONE &&
peer->state != PEER_UP) {
/*
* There is a race condition when doing PEER_ERR -> PEER_DOWN.
* So just do a full reset of the peer here.
*/
for (i = 0; i < AID_MAX; i++) {
peer->staletime[i] = 0;
peer_flush(peer, i);
}
up_down(peer);
peer->prefix_cnt = 0;
peer->state = PEER_DOWN;
}
peer->remote_bgpid = ntohl(sup->remote_bgpid);
peer->short_as = sup->short_as;
memcpy(&peer->remote_addr, &sup->remote_addr,
sizeof(peer->remote_addr));
memcpy(&peer->capa, &sup->capa, sizeof(peer->capa));
if (peer_localaddrs(peer, &sup->local_addr)) {
peer->state = PEER_DOWN;
imsg_compose(ibuf_se, IMSG_SESSION_DOWN, id, 0, -1, NULL, 0);
return;
}
peer->state = PEER_UP;
up_init(peer);
if (rde_noevaluate())
/*
* no need to dump the table to the peer, there are no active
* prefixes anyway. This is a speed up hack.
*/
return;
for (i = 0; i < AID_MAX; i++) {
if (peer->capa.mp[i])
peer_dump(id, i);
}
}
void
peer_down(u_int32_t id)
{
struct rde_peer *peer;
struct rde_aspath *asp, *nasp;
peer = peer_get(id);
if (peer == NULL) {
log_warnx("peer_down: unknown peer id %d", id);
return;
}
peer->remote_bgpid = 0;
peer->state = PEER_DOWN;
up_down(peer);
/* walk through per peer RIB list and remove all prefixes. */
for (asp = LIST_FIRST(&peer->path_h); asp != NULL; asp = nasp) {
nasp = LIST_NEXT(asp, peer_l);
path_remove(asp);
}
LIST_INIT(&peer->path_h);
peer->prefix_cnt = 0;
/* Deletions are performed in path_remove() */
rde_send_pftable_commit();
LIST_REMOVE(peer, hash_l);
LIST_REMOVE(peer, peer_l);
free(peer);
}
/*
* Flush all routes older then staletime. If staletime is 0 all routes will
* be flushed.
*/
void
peer_flush(struct rde_peer *peer, u_int8_t aid)
{
struct rde_aspath *asp, *nasp;
u_int32_t rprefixes;
rprefixes = 0;
/* walk through per peer RIB list and remove all stale prefixes. */
for (asp = LIST_FIRST(&peer->path_h); asp != NULL; asp = nasp) {
nasp = LIST_NEXT(asp, peer_l);
rprefixes += path_remove_stale(asp, aid);
}
/* Deletions are performed in path_remove() */
rde_send_pftable_commit();
/* flushed no need to keep staletime */
peer->staletime[aid] = 0;
if (peer->prefix_cnt > rprefixes)
peer->prefix_cnt -= rprefixes;
else
peer->prefix_cnt = 0;
}
void
peer_stale(u_int32_t id, u_int8_t aid)
{
struct rde_peer *peer;
time_t now;
peer = peer_get(id);
if (peer == NULL) {
log_warnx("peer_stale: unknown peer id %d", id);
return;
}
/* flush the now even staler routes out */
if (peer->staletime[aid])
peer_flush(peer, aid);
peer->staletime[aid] = now = time(NULL);
/* make sure new prefixes start on a higher timestamp */
do {
sleep(1);
} while (now >= time(NULL));
}
void
peer_dump(u_int32_t id, u_int8_t aid)
{
struct rde_peer *peer;
peer = peer_get(id);
if (peer == NULL) {
log_warnx("peer_dump: unknown peer id %d", id);
return;
}
if (peer->conf.announce_type == ANNOUNCE_DEFAULT_ROUTE)
up_generate_default(out_rules, peer, aid);
else
rib_dump(&peer->rib->rib, rde_up_dump_upcall, peer, aid);
if (peer->capa.grestart.restart)
up_generate_marker(peer, aid);
}
/* End-of-RIB marker, RFC 4724 */
void
peer_recv_eor(struct rde_peer *peer, u_int8_t aid)
{
peer->prefix_rcvd_eor++;
/*
* First notify SE to avert a possible race with the restart timeout.
* If the timeout fires before this imsg is processed by the SE it will
* result in the same operation since the timeout issues a FLUSH which
* does the same as the RESTARTED action (flushing stale routes).
* The logic in the SE is so that only one of FLUSH or RESTARTED will
* be sent back to the RDE and so peer_flush is only called once.
*/
if (imsg_compose(ibuf_se, IMSG_SESSION_RESTARTED, peer->conf.id,
0, -1, &aid, sizeof(aid)) == -1)
fatal("%s %d imsg_compose error", __func__, __LINE__);
}
void
peer_send_eor(struct rde_peer *peer, u_int8_t aid)
{
u_int16_t afi;
u_int8_t safi;
peer->prefix_sent_eor++;
if (aid == AID_INET) {
u_char null[4];
bzero(&null, 4);
if (imsg_compose(ibuf_se, IMSG_UPDATE, peer->conf.id,
0, -1, &null, 4) == -1)
fatal("%s %d imsg_compose error in peer_send_eor",
__func__, __LINE__);
} else {
u_int16_t i;
u_char buf[10];
if (aid2afi(aid, &afi, &safi) == -1)
fatalx("peer_send_eor: bad AID");
i = 0; /* v4 withdrawn len */
bcopy(&i, &buf[0], sizeof(i));
i = htons(6); /* path attr len */
bcopy(&i, &buf[2], sizeof(i));
buf[4] = ATTR_OPTIONAL;
buf[5] = ATTR_MP_UNREACH_NLRI;
buf[6] = 3; /* withdrawn len */
i = htons(afi);
bcopy(&i, &buf[7], sizeof(i));
buf[9] = safi;
if (imsg_compose(ibuf_se, IMSG_UPDATE, peer->conf.id,
0, -1, &buf, 10) == -1)
fatal("%s %d imsg_compose error in peer_send_eor",
__func__, __LINE__);
}
}
/*
* network announcement stuff
*/
void
network_add(struct network_config *nc, int flagstatic)
{
struct rdomain *rd;
struct rde_aspath *asp;
struct filter_set_head *vpnset = NULL;
in_addr_t prefix4;
u_int16_t i;
if (nc->rtableid) {
SIMPLEQ_FOREACH(rd, rdomains_l, entry) {
if (rd->rtableid != nc->rtableid)
continue;
switch (nc->prefix.aid) {
case AID_INET:
prefix4 = nc->prefix.v4.s_addr;
bzero(&nc->prefix, sizeof(nc->prefix));
nc->prefix.aid = AID_VPN_IPv4;
nc->prefix.vpn4.rd = rd->rd;
nc->prefix.vpn4.addr.s_addr = prefix4;
nc->prefix.vpn4.labellen = 3;
nc->prefix.vpn4.labelstack[0] =
(rd->label >> 12) & 0xff;
nc->prefix.vpn4.labelstack[1] =
(rd->label >> 4) & 0xff;
nc->prefix.vpn4.labelstack[2] =
(rd->label << 4) & 0xf0;
nc->prefix.vpn4.labelstack[2] |= BGP_MPLS_BOS;
vpnset = &rd->export;
break;
default:
log_warnx("unable to VPNize prefix");
filterset_free(&nc->attrset);
return;
}
break;
}
if (rd == NULL) {
log_warnx("network_add: "
"prefix %s/%u in non-existing rdomain %u",
log_addr(&nc->prefix), nc->prefixlen, nc->rtableid);
return;
}
}
if (nc->type == NETWORK_MRTCLONE) {
asp = nc->asp;
} else {
asp = path_get();
asp->aspath = aspath_get(NULL, 0);
asp->origin = ORIGIN_IGP;
asp->flags = F_ATTR_ORIGIN | F_ATTR_ASPATH |
F_ATTR_LOCALPREF | F_PREFIX_ANNOUNCED;
/* the nexthop is unset unless a default set overrides it */
}
if (!flagstatic)
asp->flags |= F_ANN_DYNAMIC;
rde_apply_set(asp, &nc->attrset, nc->prefix.aid, peerself, peerself);
if (vpnset)
rde_apply_set(asp, vpnset, nc->prefix.aid, peerself, peerself);
for (i = 1; i < rib_size; i++) {
if (*ribs[i].name == '\0')
break;
path_update(&ribs[i].rib, peerself, asp, &nc->prefix,
nc->prefixlen);
}
path_put(asp);
filterset_free(&nc->attrset);
}
void
network_delete(struct network_config *nc, int flagstatic)
{
struct rdomain *rd;
in_addr_t prefix4;
u_int32_t flags = F_PREFIX_ANNOUNCED;
u_int32_t i;
if (!flagstatic)
flags |= F_ANN_DYNAMIC;
if (nc->rtableid) {
SIMPLEQ_FOREACH(rd, rdomains_l, entry) {
if (rd->rtableid != nc->rtableid)
continue;
switch (nc->prefix.aid) {
case AID_INET:
prefix4 = nc->prefix.v4.s_addr;
bzero(&nc->prefix, sizeof(nc->prefix));
nc->prefix.aid = AID_VPN_IPv4;
nc->prefix.vpn4.rd = rd->rd;
nc->prefix.vpn4.addr.s_addr = prefix4;
nc->prefix.vpn4.labellen = 3;
nc->prefix.vpn4.labelstack[0] =
(rd->label >> 12) & 0xff;
nc->prefix.vpn4.labelstack[1] =
(rd->label >> 4) & 0xff;
nc->prefix.vpn4.labelstack[2] =
(rd->label << 4) & 0xf0;
nc->prefix.vpn4.labelstack[2] |= BGP_MPLS_BOS;
break;
default:
log_warnx("unable to VPNize prefix");
return;
}
}
}
for (i = rib_size - 1; i > 0; i--) {
if (*ribs[i].name == '\0')
break;
prefix_remove(&ribs[i].rib, peerself, &nc->prefix,
nc->prefixlen, flags);
}
}
void
network_dump_upcall(struct rib_entry *re, void *ptr)
{
struct prefix *p;
struct kroute_full k;
struct bgpd_addr addr;
struct rde_dump_ctx *ctx = ptr;
LIST_FOREACH(p, &re->prefix_h, rib_l) {
if (!(p->aspath->flags & F_PREFIX_ANNOUNCED))
continue;
pt_getaddr(p->prefix, &addr);
bzero(&k, sizeof(k));
memcpy(&k.prefix, &addr, sizeof(k.prefix));
if (p->aspath->nexthop == NULL ||
p->aspath->nexthop->state != NEXTHOP_REACH)
k.nexthop.aid = k.prefix.aid;
else
memcpy(&k.nexthop, &p->aspath->nexthop->true_nexthop,
sizeof(k.nexthop));
k.prefixlen = p->prefix->prefixlen;
k.flags = F_KERNEL;
if ((p->aspath->flags & F_ANN_DYNAMIC) == 0)
k.flags = F_STATIC;
if (imsg_compose(ibuf_se_ctl, IMSG_CTL_SHOW_NETWORK, 0,
ctx->req.pid, -1, &k, sizeof(k)) == -1)
log_warnx("network_dump_upcall: "
"imsg_compose error");
}
}
/* clean up */
void
rde_shutdown(void)
{
struct rde_peer *p;
u_int32_t i;
/*
* the decision process is turned off if rde_quit = 1 and
* rde_shutdown depends on this.
*/
/*
* All peers go down
*/
for (i = 0; i <= peertable.peer_hashmask; i++)
while ((p = LIST_FIRST(&peertable.peer_hashtbl[i])) != NULL)
peer_down(p->conf.id);
/* free filters */
filterlist_free(out_rules);
for (i = 0; i < rib_size; i++) {
if (*ribs[i].name == '\0')
break;
filterlist_free(ribs[i].in_rules);
}
nexthop_shutdown();
path_shutdown();
aspath_shutdown();
attr_shutdown();
pt_shutdown();
peer_shutdown();
}
int
sa_cmp(struct bgpd_addr *a, struct sockaddr *b)
{
struct sockaddr_in *in_b;
struct sockaddr_in6 *in6_b;
if (aid2af(a->aid) != b->sa_family)
return (1);
switch (b->sa_family) {
case AF_INET:
in_b = (struct sockaddr_in *)b;
if (a->v4.s_addr != in_b->sin_addr.s_addr)
return (1);
break;
case AF_INET6:
in6_b = (struct sockaddr_in6 *)b;
#ifdef __KAME__
/* directly stolen from sbin/ifconfig/ifconfig.c */
if (IN6_IS_ADDR_LINKLOCAL(&in6_b->sin6_addr)) {
in6_b->sin6_scope_id =
ntohs(*(u_int16_t *)&in6_b->sin6_addr.s6_addr[2]);
in6_b->sin6_addr.s6_addr[2] =
in6_b->sin6_addr.s6_addr[3] = 0;
}
#endif
if (bcmp(&a->v6, &in6_b->sin6_addr,
sizeof(struct in6_addr)))
return (1);
break;
default:
fatal("king bula sez: unknown address family");
/* NOTREACHED */
}
return (0);
}
|