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
|
/* $OpenBSD: session.c,v 1.229 2005/07/01 13:38:14 claudio Exp $ */
/*
* Copyright (c) 2003, 2004, 2005 Henning Brauer <henning@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/param.h>
#include <sys/types.h>
#include <sys/mman.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <netinet/in.h>
#include <netinet/in_systm.h>
#include <netinet/ip.h>
#include <netinet/tcp.h>
#include <arpa/inet.h>
#include <err.h>
#include <errno.h>
#include <fcntl.h>
#include <poll.h>
#include <pwd.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "bgpd.h"
#include "mrt.h"
#include "session.h"
#define PFD_PIPE_MAIN 0
#define PFD_PIPE_ROUTE 1
#define PFD_SOCK_CTL 2
#define PFD_LISTENERS_START 3
void session_sighdlr(int);
int setup_listeners(u_int *);
void init_conf(struct bgpd_config *);
void init_peer(struct peer *);
int timer_due(time_t);
void start_timer_holdtime(struct peer *);
void start_timer_keepalive(struct peer *);
void session_close_connection(struct peer *);
void change_state(struct peer *, enum session_state, enum session_events);
int session_setup_socket(struct peer *);
void session_accept(int);
int session_connect(struct peer *);
void session_tcp_established(struct peer *);
void session_capa_ann_none(struct peer *);
int session_capa_mp_add(struct buf *, u_int16_t, u_int8_t);
void session_open(struct peer *);
void session_keepalive(struct peer *);
void session_update(u_int32_t, void *, size_t);
void session_notification(struct peer *, u_int8_t, u_int8_t, void *,
ssize_t);
int session_dispatch_msg(struct pollfd *, struct peer *);
int parse_header(struct peer *, u_char *, u_int16_t *, u_int8_t *);
int parse_open(struct peer *);
int parse_update(struct peer *);
int parse_refresh(struct peer *);
int parse_notification(struct peer *);
int parse_keepalive(struct peer *);
int parse_capabilities(struct peer *, u_char *, u_int16_t);
void session_dispatch_imsg(struct imsgbuf *, int, u_int *);
void session_up(struct peer *);
void session_down(struct peer *);
int la_cmp(struct listen_addr *, struct listen_addr *);
struct peer *getpeerbyip(struct sockaddr *);
int session_match_mask(struct peer *, struct sockaddr *);
struct peer *getpeerbyid(u_int32_t);
static struct sockaddr *addr2sa(struct bgpd_addr *, u_int16_t);
struct bgpd_config *conf, *nconf = NULL;
struct bgpd_sysdep sysdep;
struct peer *npeers;
volatile sig_atomic_t session_quit = 0;
int pending_reconf = 0;
int csock = -1;
u_int peer_cnt;
struct imsgbuf *ibuf_rde;
struct imsgbuf *ibuf_main;
struct mrt_head mrthead;
void
session_sighdlr(int sig)
{
switch (sig) {
case SIGINT:
case SIGTERM:
session_quit = 1;
break;
}
}
int
setup_listeners(u_int *la_cnt)
{
int opt;
struct listen_addr *la;
u_int cnt = 0;
TAILQ_FOREACH(la, conf->listen_addrs, entry) {
la->reconf = RECONF_NONE;
cnt++;
if (la->flags & LISTENER_LISTENING)
continue;
if (la->fd == -1) {
log_warn("cannot establish listener on %s: invalid fd",
log_sockaddr((struct sockaddr *)&la->sa));
continue;
}
opt = 1;
if (setsockopt(la->fd, IPPROTO_TCP, TCP_MD5SIG,
&opt, sizeof(opt)) == -1) {
if (errno == ENOPROTOOPT) { /* system w/o md5sig */
log_warnx("md5sig not available, disabling");
sysdep.no_md5sig = 1;
} else
fatal("setsockopt TCP_MD5SIG");
}
session_socket_blockmode(la->fd, BM_NONBLOCK);
if (listen(la->fd, MAX_BACKLOG)) {
close(la->fd);
fatal("listen");
}
la->flags |= LISTENER_LISTENING;
log_info("listening on %s",
log_sockaddr((struct sockaddr *)&la->sa));
}
*la_cnt = cnt;
return (0);
}
pid_t
session_main(struct bgpd_config *config, struct peer *cpeers,
struct network_head *net_l, struct filter_head *rules,
struct mrt_head *m_l, int pipe_m2s[2], int pipe_s2r[2], int pipe_m2r[2])
{
int nfds, i, j, timeout;
int idx_peers, idx_listeners, idx_mrts;
pid_t pid;
time_t nextaction;
u_int pfd_elms = 0, peer_l_elms = 0, mrt_l_elms = 0;
u_int listener_cnt, ctl_cnt, mrt_cnt;
u_int new_cnt;
struct passwd *pw;
struct peer *p, **peer_l = NULL, *last, *next;
struct network *net;
struct mrt *m, **mrt_l = NULL;
struct filter_rule *r;
struct pollfd *pfd = NULL;
struct ctl_conn *ctl_conn;
struct listen_addr *la;
void *newp;
short events;
conf = config;
peers = cpeers;
switch (pid = fork()) {
case -1:
fatal("cannot fork");
case 0:
break;
default:
return (pid);
}
/* control socket is outside chroot */
if ((csock = control_init()) == -1)
fatalx("control socket setup failed");
if ((pw = getpwnam(BGPD_USER)) == NULL)
fatal(NULL);
if (chroot(pw->pw_dir) == -1)
fatal("chroot");
if (chdir("/") == -1)
fatal("chdir(\"/\")");
setproctitle("session engine");
bgpd_process = PROC_SE;
if (pfkey_init(&sysdep) == -1)
fatalx("pfkey setup failed");
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");
listener_cnt = 0;
setup_listeners(&listener_cnt);
signal(SIGTERM, session_sighdlr);
signal(SIGINT, session_sighdlr);
signal(SIGPIPE, SIG_IGN);
log_info("session engine ready");
close(pipe_m2s[0]);
close(pipe_s2r[1]);
close(pipe_m2r[0]);
close(pipe_m2r[1]);
init_conf(conf);
if ((ibuf_rde = malloc(sizeof(struct imsgbuf))) == NULL ||
(ibuf_main = malloc(sizeof(struct imsgbuf))) == NULL)
fatal(NULL);
imsg_init(ibuf_rde, pipe_s2r[0]);
imsg_init(ibuf_main, pipe_m2s[1]);
TAILQ_INIT(&ctl_conns);
csock = control_listen();
LIST_INIT(&mrthead);
peer_cnt = 0;
ctl_cnt = 0;
/* filter rules are not used in the SE */
while ((r = TAILQ_FIRST(rules)) != NULL) {
TAILQ_REMOVE(rules, r, entry);
free(r);
}
free(rules);
/* network list is not used in the SE */
while ((net = TAILQ_FIRST(net_l)) != NULL) {
TAILQ_REMOVE(net_l, net, entry);
free(net);
}
/* main mrt list is not used in the SE */
while ((m = LIST_FIRST(m_l)) != NULL) {
LIST_REMOVE(m, entry);
free(m);
}
while (session_quit == 0) {
/* check for peers to be initialized or deleted */
last = NULL;
for (p = peers; p != NULL; p = next) {
next = p->next;
if (!pending_reconf) {
/* cloned peer that idled out? */
if (p->state == STATE_IDLE && p->conf.cloned &&
time(NULL) - p->stats.last_updown >=
INTERVAL_HOLD_CLONED)
p->conf.reconf_action = RECONF_DELETE;
/* new peer that needs init? */
if (p->state == STATE_NONE)
init_peer(p);
/* reinit due? */
if (p->conf.reconf_action == RECONF_REINIT) {
bgp_fsm(p, EVNT_STOP);
p->IdleHoldTimer = time(NULL);
}
/* deletion due? */
if (p->conf.reconf_action == RECONF_DELETE) {
bgp_fsm(p, EVNT_STOP);
log_peer_warnx(&p->conf, "removed");
if (last != NULL)
last->next = next;
else
peers = next;
free(p);
peer_cnt--;
continue;
}
p->conf.reconf_action = RECONF_NONE;
}
last = p;
}
if (peer_cnt > peer_l_elms) {
if ((newp = realloc(peer_l, sizeof(struct peer *) *
peer_cnt)) == NULL) {
/* panic for now */
log_warn("could not resize peer_l from %u -> %u"
" entries", peer_l_elms, peer_cnt);
fatalx("exiting");
}
peer_l = newp;
peer_l_elms = peer_cnt;
}
mrt_cnt = 0;
LIST_FOREACH(m, &mrthead, entry)
if (m->queued)
mrt_cnt++;
if (mrt_cnt > mrt_l_elms) {
if ((newp = realloc(mrt_l, sizeof(struct mrt *) *
mrt_cnt)) == NULL) {
/* panic for now */
log_warn("could not resize mrt_l from %u -> %u"
" entries", mrt_l_elms, mrt_cnt);
fatalx("exiting");
}
mrt_l = newp;
mrt_l_elms = mrt_cnt;
}
new_cnt = PFD_LISTENERS_START + listener_cnt + peer_cnt +
ctl_cnt + mrt_cnt;
if (new_cnt > pfd_elms) {
if ((newp = realloc(pfd, sizeof(struct pollfd) *
new_cnt)) == NULL) {
/* panic for now */
log_warn("could not resize pfd from %u -> %u"
" entries", pfd_elms, new_cnt);
fatalx("exiting");
}
pfd = newp;
pfd_elms = new_cnt;
}
bzero(pfd, sizeof(struct pollfd) * pfd_elms);
pfd[PFD_PIPE_MAIN].fd = ibuf_main->fd;
pfd[PFD_PIPE_MAIN].events = POLLIN;
if (ibuf_main->w.queued > 0)
pfd[PFD_PIPE_MAIN].events |= POLLOUT;
pfd[PFD_PIPE_ROUTE].fd = ibuf_rde->fd;
pfd[PFD_PIPE_ROUTE].events = POLLIN;
if (ibuf_rde->w.queued > 0)
pfd[PFD_PIPE_ROUTE].events |= POLLOUT;
pfd[PFD_SOCK_CTL].fd = csock;
pfd[PFD_SOCK_CTL].events = POLLIN;
nextaction = time(NULL) + 240; /* loop every 240s at least */
i = PFD_LISTENERS_START;
TAILQ_FOREACH(la, conf->listen_addrs, entry) {
pfd[i].fd = la->fd;
pfd[i].events = POLLIN;
i++;
}
idx_listeners = i;
for (p = peers; p != NULL; p = p->next) {
/* check timers */
if (timer_due(p->HoldTimer))
bgp_fsm(p, EVNT_TIMER_HOLDTIME);
if (timer_due(p->ConnectRetryTimer))
bgp_fsm(p, EVNT_TIMER_CONNRETRY);
if (timer_due(p->KeepaliveTimer))
bgp_fsm(p, EVNT_TIMER_KEEPALIVE);
if (timer_due(p->IdleHoldTimer))
bgp_fsm(p, EVNT_START);
if (timer_due(p->IdleHoldResetTimer)) {
p->IdleHoldTime /= 2;
if (p->IdleHoldTime <=
INTERVAL_IDLE_HOLD_INITIAL) {
p->IdleHoldTime =
INTERVAL_IDLE_HOLD_INITIAL;
p->IdleHoldResetTimer = 0;
} else
p->IdleHoldResetTimer =
time(NULL) + p->IdleHoldTime;
}
/* set nextaction to the first expiring timer */
if (p->ConnectRetryTimer &&
p->ConnectRetryTimer < nextaction)
nextaction = p->ConnectRetryTimer;
if (p->HoldTimer && p->HoldTimer < nextaction)
nextaction = p->HoldTimer;
if (p->KeepaliveTimer && p->KeepaliveTimer < nextaction)
nextaction = p->KeepaliveTimer;
if (p->IdleHoldTimer && p->IdleHoldTimer < nextaction)
nextaction = p->IdleHoldTimer;
if (p->IdleHoldResetTimer &&
p->IdleHoldResetTimer < nextaction)
nextaction = p->IdleHoldResetTimer;
/* are we waiting for a write? */
events = POLLIN;
if (p->wbuf.queued > 0 || p->state == STATE_CONNECT)
events |= POLLOUT;
/* poll events */
if (p->fd != -1 && events != 0) {
pfd[i].fd = p->fd;
pfd[i].events = events;
peer_l[i - idx_listeners] = p;
i++;
}
}
idx_peers = i;
LIST_FOREACH(m, &mrthead, entry)
if (m->queued) {
pfd[i].fd = m->fd;
pfd[i].events = POLLOUT;
mrt_l[i - idx_peers] = m;
i++;
}
idx_mrts = i;
TAILQ_FOREACH(ctl_conn, &ctl_conns, entry) {
pfd[i].fd = ctl_conn->ibuf.fd;
pfd[i].events = POLLIN;
if (ctl_conn->ibuf.w.queued > 0)
pfd[i].events |= POLLOUT;
i++;
}
timeout = nextaction - time(NULL);
if (timeout < 0)
timeout = 0;
if ((nfds = poll(pfd, i, timeout * 1000)) == -1)
if (errno != EINTR)
fatal("poll error");
if (nfds > 0 && pfd[PFD_PIPE_MAIN].revents & POLLOUT)
if (msgbuf_write(&ibuf_main->w) < 0)
fatal("pipe write error");
if (nfds > 0 && pfd[PFD_PIPE_MAIN].revents & POLLIN) {
nfds--;
session_dispatch_imsg(ibuf_main, PFD_PIPE_MAIN,
&listener_cnt);
}
if (nfds > 0 && pfd[PFD_PIPE_ROUTE].revents & POLLOUT)
if (msgbuf_write(&ibuf_rde->w) < 0)
fatal("pipe write error");
if (nfds > 0 && pfd[PFD_PIPE_ROUTE].revents & POLLIN) {
nfds--;
session_dispatch_imsg(ibuf_rde, PFD_PIPE_ROUTE,
&listener_cnt);
}
if (nfds > 0 && pfd[PFD_SOCK_CTL].revents & POLLIN) {
nfds--;
ctl_cnt += control_accept(csock);
}
for (j = PFD_LISTENERS_START; nfds > 0 && j < idx_listeners;
j++)
if (pfd[j].revents & POLLIN) {
nfds--;
session_accept(pfd[j].fd);
}
for (; nfds > 0 && j < idx_peers; j++)
nfds -= session_dispatch_msg(&pfd[j],
peer_l[j - idx_listeners]);
for (; nfds > 0 && j < idx_mrts; j++)
if (pfd[j].revents & POLLOUT) {
nfds--;
mrt_write(mrt_l[j - idx_peers]);
}
for (; nfds > 0 && j < i; j++)
nfds -= control_dispatch_msg(&pfd[j], &ctl_cnt);
}
while ((p = peers) != NULL) {
peers = p->next;
bgp_fsm(p, EVNT_STOP);
pfkey_remove(p);
free(p);
}
while ((m = LIST_FIRST(&mrthead)) != NULL) {
mrt_clean(m);
LIST_REMOVE(m, entry);
free(m);
}
while ((la = TAILQ_FIRST(conf->listen_addrs)) != NULL) {
TAILQ_REMOVE(conf->listen_addrs, la, entry);
free(la);
}
free(conf->listen_addrs);
free(peer_l);
free(mrt_l);
free(pfd);
msgbuf_write(&ibuf_rde->w);
msgbuf_clear(&ibuf_rde->w);
free(ibuf_rde);
msgbuf_write(&ibuf_main->w);
msgbuf_clear(&ibuf_main->w);
free(ibuf_main);
control_shutdown();
log_info("session engine exiting");
_exit(0);
}
void
init_conf(struct bgpd_config *c)
{
if (!c->holdtime)
c->holdtime = INTERVAL_HOLD;
}
void
init_peer(struct peer *p)
{
p->fd = p->wbuf.fd = -1;
memcpy(&p->capa.ann, &p->conf.capabilities, sizeof(p->capa.ann));
if (!p->conf.announce_capa)
session_capa_ann_none(p);
if (p->conf.if_depend[0])
imsg_compose(ibuf_main, IMSG_IFINFO, 0, 0, -1,
p->conf.if_depend, sizeof(p->conf.if_depend));
else
p->depend_ok = 1;
peer_cnt++;
change_state(p, STATE_IDLE, EVNT_NONE);
p->IdleHoldTimer = time(NULL); /* start ASAP */
}
void
bgp_fsm(struct peer *peer, enum session_events event)
{
switch (peer->state) {
case STATE_NONE:
/* nothing */
break;
case STATE_IDLE:
switch (event) {
case EVNT_START:
peer->HoldTimer = 0;
peer->KeepaliveTimer = 0;
peer->IdleHoldTimer = 0;
/* allocate read buffer */
peer->rbuf = calloc(1, sizeof(struct buf_read));
if (peer->rbuf == NULL)
fatal(NULL);
peer->rbuf->wpos = 0;
/* init write buffer */
msgbuf_init(&peer->wbuf);
/* init pfkey - remove old if any, load new ones */
pfkey_remove(peer);
if (pfkey_establish(peer) == -1) {
log_peer_warnx(&peer->conf,
"pfkey setup failed");
return;
}
peer->stats.last_sent_errcode = 0;
peer->stats.last_sent_suberr = 0;
if (!peer->depend_ok)
peer->ConnectRetryTimer = 0;
else if (peer->conf.passive || peer->conf.template) {
change_state(peer, STATE_ACTIVE, event);
peer->ConnectRetryTimer = 0;
} else {
change_state(peer, STATE_CONNECT, event);
session_connect(peer);
peer->ConnectRetryTimer =
time(NULL) + INTERVAL_CONNECTRETRY;
}
break;
default:
/* ignore */
break;
}
break;
case STATE_CONNECT:
switch (event) {
case EVNT_START:
/* ignore */
break;
case EVNT_CON_OPEN:
session_tcp_established(peer);
session_open(peer);
peer->ConnectRetryTimer = 0;
peer->holdtime = INTERVAL_HOLD_INITIAL;
start_timer_holdtime(peer);
change_state(peer, STATE_OPENSENT, event);
break;
case EVNT_CON_OPENFAIL:
peer->ConnectRetryTimer =
time(NULL) + INTERVAL_CONNECTRETRY;
session_close_connection(peer);
change_state(peer, STATE_ACTIVE, event);
break;
case EVNT_TIMER_CONNRETRY:
peer->ConnectRetryTimer =
time(NULL) + INTERVAL_CONNECTRETRY;
session_connect(peer);
break;
default:
change_state(peer, STATE_IDLE, event);
break;
}
break;
case STATE_ACTIVE:
switch (event) {
case EVNT_START:
/* ignore */
break;
case EVNT_CON_OPEN:
session_tcp_established(peer);
session_open(peer);
peer->ConnectRetryTimer = 0;
peer->holdtime = INTERVAL_HOLD_INITIAL;
start_timer_holdtime(peer);
change_state(peer, STATE_OPENSENT, event);
break;
case EVNT_CON_OPENFAIL:
peer->ConnectRetryTimer =
time(NULL) + INTERVAL_CONNECTRETRY;
session_close_connection(peer);
change_state(peer, STATE_ACTIVE, event);
break;
case EVNT_TIMER_CONNRETRY:
peer->ConnectRetryTimer =
time(NULL) + peer->holdtime;
change_state(peer, STATE_CONNECT, event);
session_connect(peer);
break;
default:
change_state(peer, STATE_IDLE, event);
break;
}
break;
case STATE_OPENSENT:
switch (event) {
case EVNT_START:
/* ignore */
break;
case EVNT_STOP:
session_notification(peer, ERR_CEASE, 0, NULL, 0);
change_state(peer, STATE_IDLE, event);
break;
case EVNT_CON_CLOSED:
session_close_connection(peer);
peer->ConnectRetryTimer =
time(NULL) + INTERVAL_CONNECTRETRY;
change_state(peer, STATE_ACTIVE, event);
break;
case EVNT_CON_FATAL:
change_state(peer, STATE_IDLE, event);
break;
case EVNT_TIMER_HOLDTIME:
session_notification(peer, ERR_HOLDTIMEREXPIRED,
0, NULL, 0);
change_state(peer, STATE_IDLE, event);
break;
case EVNT_RCVD_OPEN:
/* parse_open calls change_state itself on failure */
if (parse_open(peer))
break;
session_keepalive(peer);
change_state(peer, STATE_OPENCONFIRM, event);
break;
case EVNT_RCVD_NOTIFICATION:
if (parse_notification(peer)) {
change_state(peer, STATE_IDLE, event);
/* don't punish, capa negotiation */
peer->IdleHoldTimer = time(NULL);
peer->IdleHoldTime /= 2;
} else
change_state(peer, STATE_IDLE, event);
break;
default:
session_notification(peer, ERR_FSM, 0, NULL, 0);
change_state(peer, STATE_IDLE, event);
break;
}
break;
case STATE_OPENCONFIRM:
switch (event) {
case EVNT_START:
/* ignore */
break;
case EVNT_STOP:
session_notification(peer, ERR_CEASE, 0, NULL, 0);
change_state(peer, STATE_IDLE, event);
break;
case EVNT_CON_CLOSED:
case EVNT_CON_FATAL:
change_state(peer, STATE_IDLE, event);
break;
case EVNT_TIMER_HOLDTIME:
session_notification(peer, ERR_HOLDTIMEREXPIRED,
0, NULL, 0);
change_state(peer, STATE_IDLE, event);
break;
case EVNT_TIMER_KEEPALIVE:
session_keepalive(peer);
break;
case EVNT_RCVD_KEEPALIVE:
start_timer_holdtime(peer);
change_state(peer, STATE_ESTABLISHED, event);
break;
case EVNT_RCVD_NOTIFICATION:
parse_notification(peer);
change_state(peer, STATE_IDLE, event);
break;
default:
session_notification(peer, ERR_FSM, 0, NULL, 0);
change_state(peer, STATE_IDLE, event);
break;
}
break;
case STATE_ESTABLISHED:
switch (event) {
case EVNT_START:
/* ignore */
break;
case EVNT_STOP:
session_notification(peer, ERR_CEASE, 0, NULL, 0);
change_state(peer, STATE_IDLE, event);
break;
case EVNT_CON_CLOSED:
case EVNT_CON_FATAL:
change_state(peer, STATE_IDLE, event);
break;
case EVNT_TIMER_HOLDTIME:
session_notification(peer, ERR_HOLDTIMEREXPIRED,
0, NULL, 0);
change_state(peer, STATE_IDLE, event);
break;
case EVNT_TIMER_KEEPALIVE:
session_keepalive(peer);
break;
case EVNT_RCVD_KEEPALIVE:
start_timer_holdtime(peer);
break;
case EVNT_RCVD_UPDATE:
start_timer_holdtime(peer);
if (parse_update(peer))
change_state(peer, STATE_IDLE, event);
else
start_timer_holdtime(peer);
break;
case EVNT_RCVD_NOTIFICATION:
parse_notification(peer);
change_state(peer, STATE_IDLE, event);
break;
default:
session_notification(peer, ERR_FSM, 0, NULL, 0);
change_state(peer, STATE_IDLE, event);
break;
}
break;
}
}
int
timer_due(time_t timer)
{
if (timer > 0 && timer <= time(NULL))
return (1);
return (0);
}
void
start_timer_holdtime(struct peer *peer)
{
if (peer->holdtime > 0)
peer->HoldTimer = time(NULL) + peer->holdtime;
else
peer->HoldTimer = 0;
}
void
start_timer_keepalive(struct peer *peer)
{
if (peer->holdtime > 0)
peer->KeepaliveTimer = time(NULL) + peer->holdtime / 3;
else
peer->KeepaliveTimer = 0;
}
void
session_close_connection(struct peer *peer)
{
if (peer->fd != -1)
close(peer->fd);
peer->fd = peer->wbuf.fd = -1;
}
void
change_state(struct peer *peer, enum session_state state,
enum session_events event)
{
struct mrt *mrt;
switch (state) {
case STATE_IDLE:
/*
* try to write out what's buffered (maybe a notification),
* don't bother if it fails
*/
if (peer->state >= STATE_OPENSENT && peer->wbuf.queued)
msgbuf_write(&peer->wbuf);
/*
* we must start the timer for the next EVNT_START
* if we are coming here due to an error and the
* session was not established successfully before, the
* starttimerinterval needs to be exponentially increased
*/
if (peer->IdleHoldTime == 0)
peer->IdleHoldTime = INTERVAL_IDLE_HOLD_INITIAL;
peer->holdtime = INTERVAL_HOLD_INITIAL;
peer->ConnectRetryTimer = 0;
peer->KeepaliveTimer = 0;
peer->HoldTimer = 0;
peer->IdleHoldResetTimer = 0;
session_close_connection(peer);
msgbuf_clear(&peer->wbuf);
free(peer->rbuf);
peer->rbuf = NULL;
bzero(&peer->capa.peer, sizeof(peer->capa.peer));
if (peer->state == STATE_ESTABLISHED)
session_down(peer);
if (event != EVNT_STOP) {
peer->IdleHoldTimer = time(NULL) + peer->IdleHoldTime;
if (event != EVNT_NONE &&
peer->IdleHoldTime < MAX_IDLE_HOLD/2)
peer->IdleHoldTime *= 2;
}
break;
case STATE_CONNECT:
break;
case STATE_ACTIVE:
break;
case STATE_OPENSENT:
break;
case STATE_OPENCONFIRM:
break;
case STATE_ESTABLISHED:
if (peer->IdleHoldTime > INTERVAL_IDLE_HOLD_INITIAL)
peer->IdleHoldResetTimer =
time(NULL) + peer->IdleHoldTime;
session_up(peer);
break;
default: /* something seriously fucked */
break;
}
log_statechange(peer, state, event);
LIST_FOREACH(mrt, &mrthead, entry) {
if (mrt->type != MRT_ALL_IN && mrt->type != MRT_ALL_OUT)
continue;
if ((mrt->peer_id == 0 && mrt->group_id == 0) ||
mrt->peer_id == peer->conf.id ||
mrt->group_id == peer->conf.groupid)
mrt_dump_state(mrt, peer->state, state,
peer, conf);
}
peer->prev_state = peer->state;
peer->state = state;
}
void
session_accept(int listenfd)
{
int connfd;
int opt;
socklen_t len;
struct sockaddr_storage cliaddr;
struct peer *p = NULL;
len = sizeof(cliaddr);
if ((connfd = accept(listenfd,
(struct sockaddr *)&cliaddr, &len)) == -1) {
if (errno == EWOULDBLOCK || errno == EINTR)
return;
else
log_warn("accept");
}
p = getpeerbyip((struct sockaddr *)&cliaddr);
if (p != NULL &&
(p->state == STATE_CONNECT || p->state == STATE_ACTIVE)) {
if (p->fd != -1) {
if (p->state == STATE_CONNECT)
session_close_connection(p);
else {
close(connfd);
return;
}
}
if (p->conf.auth.method != AUTH_NONE && sysdep.no_pfkey) {
log_peer_warnx(&p->conf,
"ipsec or md5sig configured but not available");
close(connfd);
return;
}
if (p->conf.auth.method == AUTH_MD5SIG) {
if (sysdep.no_md5sig) {
log_peer_warnx(&p->conf,
"md5sig configured but not available");
close(connfd);
return;
}
len = sizeof(opt);
if (getsockopt(connfd, IPPROTO_TCP, TCP_MD5SIG,
&opt, &len) == -1)
fatal("getsockopt TCP_MD5SIG");
if (!opt) { /* non-md5'd connection! */
log_peer_warnx(&p->conf,
"connection attempt without md5 signature");
close(connfd);
return;
}
}
p->fd = p->wbuf.fd = connfd;
if (session_setup_socket(p)) {
close(connfd);
return;
}
session_socket_blockmode(connfd, BM_NONBLOCK);
bgp_fsm(p, EVNT_CON_OPEN);
} else {
log_conn_attempt(p, (struct sockaddr *)&cliaddr);
close(connfd);
}
}
int
session_connect(struct peer *peer)
{
int opt = 1;
struct sockaddr *sa;
/*
* we do not need the overcomplicated collision detection rfc1771
* describes; we simply make sure there is only ever one concurrent
* tcp connection per peer.
*/
if (peer->fd != -1)
return (-1);
if ((peer->fd = socket(peer->conf.remote_addr.af, SOCK_STREAM,
IPPROTO_TCP)) == -1) {
log_peer_warn(&peer->conf, "session_connect socket");
bgp_fsm(peer, EVNT_CON_OPENFAIL);
return (-1);
}
if (peer->conf.auth.method != AUTH_NONE && sysdep.no_pfkey) {
log_peer_warnx(&peer->conf,
"ipsec or md5sig configured but not available");
bgp_fsm(peer, EVNT_CON_OPENFAIL);
return (-1);
}
if (peer->conf.auth.method == AUTH_MD5SIG) {
if (sysdep.no_md5sig) {
log_peer_warnx(&peer->conf,
"md5sig configured but not available");
bgp_fsm(peer, EVNT_CON_OPENFAIL);
return (-1);
}
if (setsockopt(peer->fd, IPPROTO_TCP, TCP_MD5SIG,
&opt, sizeof(opt)) == -1) {
log_peer_warn(&peer->conf, "setsockopt md5sig");
bgp_fsm(peer, EVNT_CON_OPENFAIL);
return (-1);
}
}
peer->wbuf.fd = peer->fd;
/* if update source is set we need to bind() */
if (peer->conf.local_addr.af) {
sa = addr2sa(&peer->conf.local_addr, 0);
if (bind(peer->fd, sa, sa->sa_len) == -1) {
log_peer_warn(&peer->conf, "session_connect bind");
bgp_fsm(peer, EVNT_CON_OPENFAIL);
return (-1);
}
}
if (session_setup_socket(peer)) {
bgp_fsm(peer, EVNT_CON_OPENFAIL);
return (-1);
}
session_socket_blockmode(peer->fd, BM_NONBLOCK);
sa = addr2sa(&peer->conf.remote_addr, BGP_PORT);
if (connect(peer->fd, sa, sa->sa_len) == -1) {
if (errno != EINPROGRESS) {
if (errno != peer->lasterr)
log_peer_warn(&peer->conf, "connect");
peer->lasterr = errno;
bgp_fsm(peer, EVNT_CON_OPENFAIL);
return (-1);
}
} else
bgp_fsm(peer, EVNT_CON_OPEN);
return (0);
}
int
session_setup_socket(struct peer *p)
{
int ttl = p->conf.distance;
int pre = IPTOS_PREC_INTERNETCONTROL;
int nodelay = 1;
int bsize;
if (p->conf.ebgp && p->sa_remote.ss_family == AF_INET)
/* set TTL to foreign router's distance - 1=direct n=multihop */
if (setsockopt(p->fd, IPPROTO_IP, IP_TTL, &ttl,
sizeof(ttl)) == -1) {
log_peer_warn(&p->conf,
"session_setup_socket setsockopt TTL");
return (-1);
}
if (p->conf.ebgp && p->sa_remote.ss_family == AF_INET6)
/* set hoplimit to foreign router's distance */
if (setsockopt(p->fd, IPPROTO_IPV6, IPV6_HOPLIMIT, &ttl,
sizeof(ttl)) == -1) {
log_peer_warn(&p->conf,
"session_setup_socket setsockopt hoplimit");
return (-1);
}
/* set TCP_NODELAY */
if (setsockopt(p->fd, IPPROTO_TCP, TCP_NODELAY, &nodelay,
sizeof(nodelay)) == -1) {
log_peer_warn(&p->conf,
"session_setup_socket setsockopt TCP_NODELAY");
return (-1);
}
/* set precedence, see rfc1771 appendix 5 */
if (p->sa_remote.ss_family == AF_INET &&
setsockopt(p->fd, IPPROTO_IP, IP_TOS, &pre, sizeof(pre)) == -1) {
log_peer_warn(&p->conf,
"session_setup_socket setsockopt TOS");
return (-1);
}
/* only increase bufsize (and thus window) if md5 or ipsec is in use */
if (p->conf.auth.method != AUTH_NONE) {
/* try to increase bufsize. no biggie if it fails */
bsize = 65535;
while (setsockopt(p->fd, SOL_SOCKET, SO_RCVBUF, &bsize,
sizeof(bsize)) == -1)
bsize /= 2;
bsize = 65535;
while (setsockopt(p->fd, SOL_SOCKET, SO_SNDBUF, &bsize,
sizeof(bsize)) == -1)
bsize /= 2;
}
return (0);
}
void
session_socket_blockmode(int fd, enum blockmodes bm)
{
int flags;
if ((flags = fcntl(fd, F_GETFL, 0)) == -1)
fatal("fnctl F_GETFL");
if (bm == BM_NONBLOCK)
flags |= O_NONBLOCK;
else
flags &= ~O_NONBLOCK;
if ((flags = fcntl(fd, F_SETFL, flags)) == -1)
fatal("fnctl F_SETFL");
}
void
session_tcp_established(struct peer *peer)
{
socklen_t len;
len = sizeof(peer->sa_local);
if (getsockname(peer->fd, (struct sockaddr *)&peer->sa_local,
&len) == -1)
log_warn("getsockname");
len = sizeof(peer->sa_remote);
if (getpeername(peer->fd, (struct sockaddr *)&peer->sa_remote,
&len) == -1)
log_warn("getpeername");
}
void
session_capa_ann_none(struct peer *peer)
{
peer->capa.ann.mp_v4 = SAFI_NONE;
peer->capa.ann.mp_v4 = SAFI_NONE;
peer->capa.ann.refresh = 0;
}
int
session_capa_mp_add(struct buf *buf, u_int16_t afi, u_int8_t safi)
{
u_int8_t capa_code, capa_len, pad = 0;
int errs = 0;
afi = htons(afi);
capa_code = CAPA_MP;
capa_len = 4;
errs += buf_add(buf, &capa_code, sizeof(capa_code));
errs += buf_add(buf, &capa_len, sizeof(capa_len));
errs += buf_add(buf, &afi, sizeof(afi));
errs += buf_add(buf, &pad, sizeof(pad));
errs += buf_add(buf, &safi, sizeof(safi));
return (errs);
}
void
session_open(struct peer *p)
{
struct msg_open msg;
struct buf *buf;
struct mrt *mrt;
u_int16_t len;
int errs = 0;
u_int8_t op_type, op_len = 0, optparamlen = 0;
u_int8_t capa_code, capa_len;
/* multiprotocol extensions, RFC 2858 */
if (p->capa.ann.mp_v4)
op_len += 2 + 4; /* 1 code + 1 len + 4 data */
if (p->capa.ann.mp_v6)
op_len += 2 + 4; /* 1 code + 1 len + 4 data */
/* route refresh, RFC 2918 */
if (p->capa.ann.refresh)
op_len += 2 + 0; /* 1 code + 1 len, no data */
if (op_len > 0)
optparamlen = sizeof(op_type) + sizeof(op_len) + op_len;
len = MSGSIZE_OPEN_MIN + optparamlen;
memset(&msg.header.marker, 0xff, sizeof(msg.header.marker));
msg.header.len = htons(len);
msg.header.type = OPEN;
msg.version = 4;
msg.myas = htons(conf->as);
if (p->conf.holdtime)
msg.holdtime = htons(p->conf.holdtime);
else
msg.holdtime = htons(conf->holdtime);
msg.bgpid = conf->bgpid; /* is already in network byte order */
msg.optparamlen = optparamlen;
if ((buf = buf_open(len)) == NULL) {
bgp_fsm(p, EVNT_CON_FATAL);
return;
}
errs += buf_add(buf, &msg.header.marker, sizeof(msg.header.marker));
errs += buf_add(buf, &msg.header.len, sizeof(msg.header.len));
errs += buf_add(buf, &msg.header.type, sizeof(msg.header.type));
errs += buf_add(buf, &msg.version, sizeof(msg.version));
errs += buf_add(buf, &msg.myas, sizeof(msg.myas));
errs += buf_add(buf, &msg.holdtime, sizeof(msg.holdtime));
errs += buf_add(buf, &msg.bgpid, sizeof(msg.bgpid));
errs += buf_add(buf, &msg.optparamlen, sizeof(msg.optparamlen));
if (optparamlen) {
op_type = OPT_PARAM_CAPABILITIES;
errs += buf_add(buf, &op_type, sizeof(op_type));
errs += buf_add(buf, &op_len, sizeof(op_len));
/* multiprotocol extensions, RFC 2858 */
if (p->capa.ann.mp_v4)
errs += session_capa_mp_add(buf, AFI_IPv4,
p->capa.ann.mp_v4);
if (p->capa.ann.mp_v6)
errs += session_capa_mp_add(buf, AFI_IPv6,
p->capa.ann.mp_v6);
/* route refresh, RFC 2918 */
if (p->capa.ann.refresh) {
capa_code = CAPA_REFRESH;
capa_len = 0;
errs += buf_add(buf, &capa_code, sizeof(capa_code));
errs += buf_add(buf, &capa_len, sizeof(capa_len));
}
}
if (errs == 0) {
LIST_FOREACH(mrt, &mrthead, entry) {
if (mrt->type != MRT_ALL_OUT)
continue;
if ((mrt->peer_id == 0 && mrt->group_id == 0) ||
mrt->peer_id == p->conf.id ||
mrt->group_id == p->conf.groupid)
mrt_dump_bgp_msg(mrt, buf->buf, len,
p, conf);
}
if (buf_close(&p->wbuf, buf) == -1) {
log_peer_warn(&p->conf, "session_open buf_close");
buf_free(buf);
bgp_fsm(p, EVNT_CON_FATAL);
return;
}
} else {
buf_free(buf);
bgp_fsm(p, EVNT_CON_FATAL);
return;
}
p->stats.msg_sent_open++;
}
void
session_keepalive(struct peer *peer)
{
struct msg_header msg;
struct buf *buf;
struct mrt *mrt;
ssize_t len;
int errs = 0;
len = MSGSIZE_KEEPALIVE;
memset(&msg.marker, 0xff, sizeof(msg.marker));
msg.len = htons(len);
msg.type = KEEPALIVE;
if ((buf = buf_open(len)) == NULL) {
bgp_fsm(peer, EVNT_CON_FATAL);
return;
}
errs += buf_add(buf, &msg.marker, sizeof(msg.marker));
errs += buf_add(buf, &msg.len, sizeof(msg.len));
errs += buf_add(buf, &msg.type, sizeof(msg.type));
if (errs > 0) {
buf_free(buf);
bgp_fsm(peer, EVNT_CON_FATAL);
return;
}
LIST_FOREACH(mrt, &mrthead, entry) {
if (mrt->type != MRT_ALL_OUT)
continue;
if ((mrt->peer_id == 0 && mrt->group_id == 0) ||
mrt->peer_id == peer->conf.id ||
mrt->group_id == peer->conf.groupid)
mrt_dump_bgp_msg(mrt, buf->buf, len, peer, conf);
}
if (buf_close(&peer->wbuf, buf) == -1) {
log_peer_warn(&peer->conf, "session_keepalive buf_close");
buf_free(buf);
bgp_fsm(peer, EVNT_CON_FATAL);
return;
}
start_timer_keepalive(peer);
peer->stats.msg_sent_keepalive++;
}
void
session_update(u_int32_t peerid, void *data, size_t datalen)
{
struct peer *p;
struct msg_header msg;
struct buf *buf;
struct mrt *mrt;
ssize_t len;
int errs = 0;
if ((p = getpeerbyid(peerid)) == NULL) {
log_warnx("no such peer: id=%u", peerid);
return;
}
if (p->state != STATE_ESTABLISHED)
return;
len = MSGSIZE_HEADER + datalen;
memset(&msg.marker, 0xff, sizeof(msg.marker));
msg.len = htons(len);
msg.type = UPDATE;
if ((buf = buf_open(len)) == NULL) {
bgp_fsm(p, EVNT_CON_FATAL);
return;
}
errs += buf_add(buf, &msg.marker, sizeof(msg.marker));
errs += buf_add(buf, &msg.len, sizeof(msg.len));
errs += buf_add(buf, &msg.type, sizeof(msg.type));
errs += buf_add(buf, data, datalen);
if (errs > 0) {
buf_free(buf);
bgp_fsm(p, EVNT_CON_FATAL);
return;
}
LIST_FOREACH(mrt, &mrthead, entry) {
if (mrt->type != MRT_ALL_OUT && mrt->type != MRT_UPDATE_OUT)
continue;
if ((mrt->peer_id == 0 && mrt->group_id == 0) ||
mrt->peer_id == p->conf.id ||
mrt->group_id == p->conf.groupid)
mrt_dump_bgp_msg(mrt, buf->buf, len, p, conf);
}
if (buf_close(&p->wbuf, buf) == -1) {
log_peer_warn(&p->conf, "session_update: buf_close");
buf_free(buf);
bgp_fsm(p, EVNT_CON_FATAL);
return;
}
start_timer_keepalive(p);
p->stats.msg_sent_update++;
}
void
session_notification(struct peer *peer, u_int8_t errcode, u_int8_t subcode,
void *data, ssize_t datalen)
{
struct msg_header msg;
struct buf *buf;
struct mrt *mrt;
ssize_t len;
int errs = 0;
u_int8_t null8 = 0;
if (peer->stats.last_sent_errcode) /* some notifctn already sent */
return;
len = MSGSIZE_NOTIFICATION_MIN + datalen;
memset(&msg.marker, 0xff, sizeof(msg.marker));
msg.len = htons(len);
msg.type = NOTIFICATION;
if ((buf = buf_open(len)) == NULL) {
bgp_fsm(peer, EVNT_CON_FATAL);
return;
}
errs += buf_add(buf, &msg.marker, sizeof(msg.marker));
errs += buf_add(buf, &msg.len, sizeof(msg.len));
errs += buf_add(buf, &msg.type, sizeof(msg.type));
errs += buf_add(buf, &errcode, sizeof(errcode));
if (errcode == ERR_CEASE)
errs += buf_add(buf, &null8, sizeof(null8));
else
errs += buf_add(buf, &subcode, sizeof(subcode));
if (datalen > 0)
errs += buf_add(buf, data, datalen);
if (errs > 0) {
buf_free(buf);
bgp_fsm(peer, EVNT_CON_FATAL);
return;
}
LIST_FOREACH(mrt, &mrthead, entry) {
if (mrt->type != MRT_ALL_OUT)
continue;
if ((mrt->peer_id == 0 && mrt->group_id == 0) ||
mrt->peer_id == peer->conf.id ||
mrt->group_id == peer->conf.groupid)
mrt_dump_bgp_msg(mrt, buf->buf, len, peer, conf);
}
if (buf_close(&peer->wbuf, buf) == -1) {
log_peer_warn(&peer->conf, "session_notification: buf_close");
buf_free(buf);
bgp_fsm(peer, EVNT_CON_FATAL);
return;
}
peer->stats.msg_sent_notification++;
peer->stats.last_sent_errcode = errcode;
peer->stats.last_sent_suberr = subcode;
}
int
session_dispatch_msg(struct pollfd *pfd, struct peer *p)
{
ssize_t n, rpos, av, left;
socklen_t len;
int error, processed = 0;
u_int16_t msglen;
u_int8_t msgtype;
if (p->state == STATE_CONNECT) {
if (pfd->revents & POLLOUT) {
if (pfd->revents & POLLIN) {
/* error occurred */
len = sizeof(error);
if (getsockopt(pfd->fd, SOL_SOCKET, SO_ERROR,
&error, &len) == -1 || error) {
if (error)
errno = error;
if (errno != p->lasterr) {
log_peer_warn(&p->conf,
"socket error");
p->lasterr = errno;
}
bgp_fsm(p, EVNT_CON_OPENFAIL);
return (1);
}
}
bgp_fsm(p, EVNT_CON_OPEN);
return (1);
}
if (pfd->revents & POLLHUP) {
bgp_fsm(p, EVNT_CON_OPENFAIL);
return (1);
}
if (pfd->revents & (POLLERR|POLLNVAL)) {
bgp_fsm(p, EVNT_CON_FATAL);
return (1);
}
return (0);
}
if (pfd->revents & POLLHUP) {
bgp_fsm(p, EVNT_CON_CLOSED);
return (1);
}
if (pfd->revents & (POLLERR|POLLNVAL)) {
bgp_fsm(p, EVNT_CON_FATAL);
return (1);
}
if (pfd->revents & POLLOUT && p->wbuf.queued) {
if ((error = msgbuf_write(&p->wbuf)) < 0) {
if (error == -2)
log_peer_warnx(&p->conf, "Connection closed");
else
log_peer_warn(&p->conf, "write error");
bgp_fsm(p, EVNT_CON_FATAL);
return (1);
}
if (!(pfd->revents & POLLIN))
return (1);
}
if (pfd->revents & POLLIN) {
if ((n = read(p->fd, p->rbuf->buf + p->rbuf->wpos,
sizeof(p->rbuf->buf) - p->rbuf->wpos)) == -1) {
if (errno != EINTR && errno != EAGAIN) {
log_peer_warn(&p->conf, "read error");
bgp_fsm(p, EVNT_CON_FATAL);
}
return (1);
}
if (n == 0) { /* connection closed */
bgp_fsm(p, EVNT_CON_CLOSED);
return (1);
}
rpos = 0;
av = p->rbuf->wpos + n;
p->stats.last_read = time(NULL);
/*
* session might drop to IDLE -> buffers deallocated
* we MUST check rbuf != NULL before use
*/
for (;;) {
if (rpos + MSGSIZE_HEADER > av)
break;
if (p->rbuf == NULL)
break;
if (parse_header(p, p->rbuf->buf + rpos,
&msglen, &msgtype) == -1)
return (0);
if (rpos + msglen > av)
break;
p->rbuf->rptr = p->rbuf->buf + rpos;
switch (msgtype) {
case OPEN:
bgp_fsm(p, EVNT_RCVD_OPEN);
p->stats.msg_rcvd_open++;
break;
case UPDATE:
bgp_fsm(p, EVNT_RCVD_UPDATE);
p->stats.msg_rcvd_update++;
break;
case NOTIFICATION:
bgp_fsm(p, EVNT_RCVD_NOTIFICATION);
p->stats.msg_rcvd_notification++;
break;
case KEEPALIVE:
bgp_fsm(p, EVNT_RCVD_KEEPALIVE);
p->stats.msg_rcvd_keepalive++;
break;
case RREFRESH:
parse_refresh(p);
p->stats.msg_rcvd_rrefresh++;
break;
default: /* cannot happen */
session_notification(p, ERR_HEADER,
ERR_HDR_TYPE, &msgtype, 1);
log_warnx("received message with "
"unknown type %u", msgtype);
bgp_fsm(p, EVNT_CON_FATAL);
}
rpos += msglen;
if (++processed > MSG_PROCESS_LIMIT)
break;
}
if (p->rbuf == NULL)
return (1);
if (rpos < av) {
left = av - rpos;
memcpy(&p->rbuf->buf, p->rbuf->buf + rpos,
left);
p->rbuf->wpos = left;
} else
p->rbuf->wpos = 0;
return (1);
}
return (0);
}
int
parse_header(struct peer *peer, u_char *data, u_int16_t *len, u_int8_t *type)
{
struct mrt *mrt;
u_char *p;
u_int16_t olen;
static const u_int8_t marker[MSGSIZE_HEADER_MARKER] = { 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
/* caller MUST make sure we are getting 19 bytes! */
p = data;
if (memcmp(p, marker, sizeof(marker))) {
log_peer_warnx(&peer->conf, "sync error");
session_notification(peer, ERR_HEADER, ERR_HDR_SYNC, NULL, 0);
bgp_fsm(peer, EVNT_CON_FATAL);
return (-1);
}
p += MSGSIZE_HEADER_MARKER;
memcpy(&olen, p, 2);
*len = ntohs(olen);
p += 2;
memcpy(type, p, 1);
if (*len < MSGSIZE_HEADER || *len > MAX_PKTSIZE) {
log_peer_warnx(&peer->conf,
"received message: illegal length: %u byte", *len);
session_notification(peer, ERR_HEADER, ERR_HDR_LEN,
&olen, sizeof(olen));
bgp_fsm(peer, EVNT_CON_FATAL);
return (-1);
}
switch (*type) {
case OPEN:
if (*len < MSGSIZE_OPEN_MIN) {
log_peer_warnx(&peer->conf,
"received OPEN: illegal len: %u byte", *len);
session_notification(peer, ERR_HEADER, ERR_HDR_LEN,
&olen, sizeof(olen));
bgp_fsm(peer, EVNT_CON_FATAL);
return (-1);
}
break;
case NOTIFICATION:
if (*len < MSGSIZE_NOTIFICATION_MIN) {
log_peer_warnx(&peer->conf,
"received NOTIFICATION: illegal len: %u byte",
*len);
session_notification(peer, ERR_HEADER, ERR_HDR_LEN,
&olen, sizeof(olen));
bgp_fsm(peer, EVNT_CON_FATAL);
return (-1);
}
break;
case UPDATE:
if (*len < MSGSIZE_UPDATE_MIN) {
log_peer_warnx(&peer->conf,
"received UPDATE: illegal len: %u byte", *len);
session_notification(peer, ERR_HEADER, ERR_HDR_LEN,
&olen, sizeof(olen));
bgp_fsm(peer, EVNT_CON_FATAL);
return (-1);
}
break;
case KEEPALIVE:
if (*len != MSGSIZE_KEEPALIVE) {
log_peer_warnx(&peer->conf,
"received KEEPALIVE: illegal len: %u byte", *len);
session_notification(peer, ERR_HEADER, ERR_HDR_LEN,
&olen, sizeof(olen));
bgp_fsm(peer, EVNT_CON_FATAL);
return (-1);
}
break;
case RREFRESH:
if (*len != MSGSIZE_RREFRESH) {
log_peer_warnx(&peer->conf,
"received RREFRESH: illegal len: %u byte", *len);
session_notification(peer, ERR_HEADER, ERR_HDR_LEN,
&olen, sizeof(olen));
bgp_fsm(peer, EVNT_CON_FATAL);
return (-1);
}
break;
default:
log_peer_warnx(&peer->conf,
"received msg with unknown type %u", *type);
session_notification(peer, ERR_HEADER, ERR_HDR_TYPE,
type, 1);
bgp_fsm(peer, EVNT_CON_FATAL);
return (-1);
}
LIST_FOREACH(mrt, &mrthead, entry) {
if (mrt->type != MRT_ALL_IN && (mrt->type != MRT_UPDATE_IN ||
*type != UPDATE))
continue;
if ((mrt->peer_id == 0 && mrt->group_id == 0) ||
mrt->peer_id == peer->conf.id ||
mrt->group_id == peer->conf.groupid)
mrt_dump_bgp_msg(mrt, data, *len, peer, conf);
}
return (0);
}
int
parse_open(struct peer *peer)
{
u_char *p, *op_val;
u_int8_t version, rversion;
u_int16_t as, msglen;
u_int16_t holdtime, oholdtime, myholdtime;
u_int32_t bgpid;
u_int8_t optparamlen, plen;
u_int8_t op_type, op_len;
p = peer->rbuf->rptr;
p += MSGSIZE_HEADER_MARKER;
memcpy(&msglen, p, sizeof(msglen));
msglen = ntohs(msglen);
p = peer->rbuf->rptr;
p += MSGSIZE_HEADER; /* header is already checked */
memcpy(&version, p, sizeof(version));
p += sizeof(version);
if (version != BGP_VERSION) {
log_peer_warnx(&peer->conf,
"peer wants unrecognized version %u", version);
if (version > BGP_VERSION)
rversion = version - BGP_VERSION;
else
rversion = BGP_VERSION;
session_notification(peer, ERR_OPEN, ERR_OPEN_VERSION,
&rversion, sizeof(rversion));
change_state(peer, STATE_IDLE, EVNT_RCVD_OPEN);
return (-1);
}
memcpy(&as, p, sizeof(as));
p += sizeof(as);
/* if remote-as is zero and it's a cloned neighbor, accept any */
if (peer->conf.cloned && !peer->conf.remote_as) {
peer->conf.remote_as = ntohs(as);
peer->conf.ebgp = (peer->conf.remote_as != conf->as);
}
if (peer->conf.remote_as != ntohs(as)) {
log_peer_warnx(&peer->conf, "peer sent wrong AS %u", ntohs(as));
session_notification(peer, ERR_OPEN, ERR_OPEN_AS, NULL, 0);
change_state(peer, STATE_IDLE, EVNT_RCVD_OPEN);
return (-1);
}
memcpy(&oholdtime, p, sizeof(oholdtime));
p += sizeof(oholdtime);
holdtime = ntohs(oholdtime);
if (holdtime && holdtime < peer->conf.min_holdtime) {
log_peer_warnx(&peer->conf,
"peer requests unacceptable holdtime %u", holdtime);
session_notification(peer, ERR_OPEN, ERR_OPEN_HOLDTIME,
NULL, 0);
change_state(peer, STATE_IDLE, EVNT_RCVD_OPEN);
return (-1);
}
myholdtime = peer->conf.holdtime;
if (!myholdtime)
myholdtime = conf->holdtime;
if (holdtime < myholdtime)
peer->holdtime = holdtime;
else
peer->holdtime = myholdtime;
memcpy(&bgpid, p, sizeof(bgpid));
p += sizeof(bgpid);
/* check bgpid for validity - just disallow 0 */
if (ntohl(bgpid) == 0) {
log_peer_warnx(&peer->conf, "peer BGPID %lu unacceptable",
ntohl(bgpid));
session_notification(peer, ERR_OPEN, ERR_OPEN_BGPID,
NULL, 0);
change_state(peer, STATE_IDLE, EVNT_RCVD_OPEN);
return (-1);
}
peer->remote_bgpid = bgpid;
memcpy(&optparamlen, p, sizeof(optparamlen));
p += sizeof(optparamlen);
if (optparamlen > msglen - MSGSIZE_OPEN_MIN) {
log_peer_warnx(&peer->conf,
"corrupt OPEN message received: length mismatch");
session_notification(peer, ERR_OPEN, 0, NULL, 0);
change_state(peer, STATE_IDLE, EVNT_RCVD_OPEN);
return (-1);
}
plen = optparamlen;
while (plen > 0) {
if (plen < 2) {
log_peer_warnx(&peer->conf,
"corrupt OPEN message received, len wrong");
session_notification(peer, ERR_OPEN, 0, NULL, 0);
change_state(peer, STATE_IDLE, EVNT_RCVD_OPEN);
return (-1);
}
memcpy(&op_type, p, sizeof(op_type));
p += sizeof(op_type);
plen -= sizeof(op_type);
memcpy(&op_len, p, sizeof(op_len));
p += sizeof(op_len);
plen -= sizeof(op_len);
if (op_len > 0) {
if (plen < op_len) {
log_peer_warnx(&peer->conf,
"corrupt OPEN message received, len wrong");
session_notification(peer, ERR_OPEN, 0,
NULL, 0);
change_state(peer, STATE_IDLE, EVNT_RCVD_OPEN);
return (-1);
}
op_val = p;
p += op_len;
plen -= op_len;
} else
op_val = NULL;
switch (op_type) {
case OPT_PARAM_CAPABILITIES: /* RFC 3392 */
if (parse_capabilities(peer, op_val, op_len) == -1) {
session_notification(peer, ERR_OPEN, 0,
NULL, 0);
change_state(peer, STATE_IDLE, EVNT_RCVD_OPEN);
return (-1);
}
break;
case OPT_PARAM_AUTH: /* deprecated */
default:
/*
* unsupported type
* the RFCs tell us to leave the data section empty
* and notify the peer with ERR_OPEN, ERR_OPEN_OPT.
* How the peer should know _which_ optional parameter
* we don't support is beyond me.
*/
log_peer_warnx(&peer->conf,
"received OPEN message with unsupported optional "
"parameter: type %u", op_type);
session_notification(peer, ERR_OPEN, ERR_OPEN_OPT,
NULL, 0);
change_state(peer, STATE_IDLE, EVNT_RCVD_OPEN);
peer->IdleHoldTimer = time(NULL); /* no punish */
peer->IdleHoldTime /= 2;
return (-1);
}
}
return (0);
}
int
parse_update(struct peer *peer)
{
u_char *p;
u_int16_t datalen;
/*
* we pass the message verbatim to the rde.
* in case of errors the whole session is reset with a
* notification anyway, we only need to know the peer
*/
p = peer->rbuf->rptr;
p += MSGSIZE_HEADER_MARKER;
memcpy(&datalen, p, sizeof(datalen));
datalen = ntohs(datalen);
p = peer->rbuf->rptr;
p += MSGSIZE_HEADER; /* header is already checked */
datalen -= MSGSIZE_HEADER;
if (imsg_compose(ibuf_rde, IMSG_UPDATE, peer->conf.id, 0, -1, p,
datalen) == -1)
return (-1);
return (0);
}
int
parse_refresh(struct peer *peer)
{
u_char *p;
struct rrefresh r;
p = peer->rbuf->rptr;
p += MSGSIZE_HEADER; /* header is already checked */
/* afi, 2 byte */
memcpy(&r.afi, p, sizeof(r.afi));
r.afi = ntohs(r.afi);
p += 2;
/* reserved, 1 byte */
p += 1;
/* safi, 1 byte */
memcpy(&r.safi, p, sizeof(r.safi));
/* afi/safi unchecked - unrecognized values will be ignored anyway */
if (imsg_compose(ibuf_rde, IMSG_REFRESH, peer->conf.id, 0, -1, &r,
sizeof(r)) == -1)
return (-1);
return (0);
}
int
parse_notification(struct peer *peer)
{
u_char *p;
u_int8_t errcode;
u_int8_t subcode;
u_int16_t datalen;
u_int8_t capa_code;
u_int8_t capa_len;
/* just log */
p = peer->rbuf->rptr;
p += MSGSIZE_HEADER_MARKER;
memcpy(&datalen, p, sizeof(datalen));
datalen = ntohs(datalen);
p = peer->rbuf->rptr;
p += MSGSIZE_HEADER; /* header is already checked */
datalen -= MSGSIZE_HEADER;
memcpy(&errcode, p, sizeof(errcode));
p += sizeof(errcode);
datalen -= sizeof(errcode);
memcpy(&subcode, p, sizeof(subcode));
p += sizeof(subcode);
datalen -= sizeof(subcode);
log_notification(peer, errcode, subcode, p, datalen);
if (errcode == ERR_OPEN && subcode == ERR_OPEN_CAPA) {
if (datalen == 0) { /* zebra likes to send those.. humbug */
log_peer_warnx(&peer->conf, "received \"unsupported "
"capability\" notification without data part, "
"disabling capability announcements altogether");
session_capa_ann_none(peer);
}
while (datalen > 0) {
if (datalen < 2) {
log_peer_warnx(&peer->conf,
"parse_notification: "
"expect len >= 2, len is %u", datalen);
return (-1);
}
memcpy(&capa_code, p, sizeof(capa_code));
p += sizeof(capa_code);
datalen -= sizeof(capa_code);
memcpy(&capa_len, p, sizeof(capa_len));
p += sizeof(capa_len);
datalen -= sizeof(capa_len);
if (datalen < capa_len) {
log_peer_warnx(&peer->conf,
"parse_notification: capa_len %u exceeds"
"remaining msg length", capa_len);
return (-1);
}
p += capa_len;
datalen -= capa_len;
switch (capa_code) {
case CAPA_MP:
peer->capa.ann.mp_v4 = SAFI_NONE;
peer->capa.ann.mp_v6 = SAFI_NONE;
log_peer_warnx(&peer->conf,
"disabling multiprotocol capability");
break;
case CAPA_REFRESH:
peer->capa.ann.refresh = 0;
log_peer_warnx(&peer->conf,
"disabling route refresh capability");
break;
default: /* should not happen... */
log_peer_warnx(&peer->conf, "received "
"\"unsupported capability\" notification "
"for unknown capability %u, disabling "
"capability announcements altogether",
capa_code);
session_capa_ann_none(peer);
break;
}
}
return (1);
}
if (errcode == ERR_OPEN && subcode == ERR_OPEN_OPT) {
session_capa_ann_none(peer);
return (1);
}
return (0);
}
int
parse_capabilities(struct peer *peer, u_char *d, u_int16_t dlen)
{
u_int16_t len;
u_int8_t capa_code;
u_int8_t capa_len;
u_char *capa_val;
u_int16_t mp_afi;
u_int8_t mp_safi;
len = dlen;
while (len > 0) {
if (len < 2) {
log_peer_warnx(&peer->conf, "parse_capabilities: "
"expect len >= 2, len is %u", len);
return (-1);
}
memcpy(&capa_code, d, sizeof(capa_code));
d += sizeof(capa_code);
len -= sizeof(capa_code);
memcpy(&capa_len, d, sizeof(capa_len));
d += sizeof(capa_len);
len -= sizeof(capa_len);
if (capa_len > 0) {
if (len < capa_len) {
log_peer_warnx(&peer->conf,
"parse_capabilities: "
"len %u smaller than capa_len %u",
len, capa_len);
return (-1);
}
capa_val = d;
d += capa_len;
len -= capa_len;
} else
capa_val = NULL;
switch (capa_code) {
case CAPA_MP: /* RFC 2858 */
if (capa_len != 4) {
log_peer_warnx(&peer->conf,
"parse_capabilities: "
"expect len 4, len is %u", capa_len);
return (-1);
}
memcpy(&mp_afi, capa_val, sizeof(mp_afi));
mp_afi = ntohs(mp_afi);
memcpy(&mp_safi, capa_val + 3, sizeof(mp_safi));
switch (mp_afi) {
case AFI_IPv4:
if (mp_safi < 1 || mp_safi > 3) {
log_peer_warnx(&peer->conf,
"parse_capabilities: AFI IPv4, "
"mp_safi %u illegal", mp_safi);
return (-1);
}
peer->capa.peer.mp_v4 = mp_safi;
break;
case AFI_IPv6:
if (mp_safi < 1 || mp_safi > 3) {
log_peer_warnx(&peer->conf,
"parse_capabilities: AFI IPv6, "
"mp_safi %u illegal", mp_safi);
return (-1);
}
peer->capa.peer.mp_v6 = mp_safi;
break;
default: /* ignore */
break;
}
break;
case CAPA_REFRESH:
peer->capa.peer.refresh = 1;
break;
default:
break;
}
}
return (0);
}
struct filter_set_head *session_set;
void
session_dispatch_imsg(struct imsgbuf *ibuf, int idx, u_int *listener_cnt)
{
struct imsg imsg;
struct mrt xmrt;
struct mrt *mrt;
struct peer_config *pconf;
struct peer *p, *next;
struct listen_addr *la, *nla;
struct kif *kif;
struct filter_set *s;
u_char *data;
enum reconf_action reconf;
int n, depend_ok;
u_int8_t errcode, subcode;
if ((n = imsg_read(ibuf)) == -1)
fatal("session_dispatch_imsg: imsg_read error");
if (n == 0) /* connection closed */
fatal("session_dispatch_imsg: pipe closed");
for (;;) {
if ((n = imsg_get(ibuf, &imsg)) == -1)
fatal("session_dispatch_imsg: imsg_get error");
if (n == 0)
break;
switch (imsg.hdr.type) {
case IMSG_RECONF_CONF:
if (idx != PFD_PIPE_MAIN)
fatalx("reconf request not from parent");
if ((nconf = malloc(sizeof(struct bgpd_config))) ==
NULL)
fatal(NULL);
memcpy(nconf, imsg.data, sizeof(struct bgpd_config));
if ((nconf->listen_addrs = calloc(1,
sizeof(struct listen_addrs))) == NULL)
fatal(NULL);
TAILQ_INIT(nconf->listen_addrs);
npeers = NULL;
init_conf(nconf);
pending_reconf = 1;
break;
case IMSG_RECONF_PEER:
if (idx != PFD_PIPE_MAIN)
fatalx("reconf request not from parent");
pconf = imsg.data;
p = getpeerbyaddr(&pconf->remote_addr);
if (p == NULL) {
if ((p = calloc(1, sizeof(struct peer))) ==
NULL)
fatal("new_peer");
p->state = p->prev_state = STATE_NONE;
p->next = npeers;
npeers = p;
reconf = RECONF_REINIT;
} else
reconf = RECONF_KEEP;
memcpy(&p->conf, pconf, sizeof(struct peer_config));
TAILQ_INIT(&p->conf.attrset);
session_set = &p->conf.attrset;
p->conf.reconf_action = reconf;
break;
case IMSG_RECONF_LISTENER:
if (idx != PFD_PIPE_MAIN)
fatalx("reconf request not from parent");
nla = imsg.data;
TAILQ_FOREACH(la, conf->listen_addrs, entry)
if (!la_cmp(la, nla))
break;
if (la == NULL) {
if (nla->reconf != RECONF_REINIT)
fatal("king bula sez: expected REINIT");
if ((nla->fd = imsg_get_fd(ibuf)) == -1)
log_warnx("expected to receive fd for "
"%s but didn't receive any",
log_sockaddr((struct sockaddr *)
&nla->sa));
la = calloc(1, sizeof(struct listen_addr));
if (la == NULL)
fatal(NULL);
memcpy(&la->sa, &nla->sa, sizeof(la->sa));
la->flags = nla->flags;
la->fd = nla->fd;
la->reconf = RECONF_REINIT;
TAILQ_INSERT_TAIL(nconf->listen_addrs, la,
entry);
} else {
if (nla->reconf != RECONF_KEEP)
fatal("king bula sez: expected KEEP");
la->reconf = RECONF_KEEP;
}
break;
case IMSG_RECONF_DONE:
if (idx != PFD_PIPE_MAIN)
fatalx("reconf request not from parent");
if (nconf == NULL)
fatalx("got IMSG_RECONF_DONE but no config");
conf->as = nconf->as;
conf->holdtime = nconf->holdtime;
conf->bgpid = nconf->bgpid;
conf->min_holdtime = nconf->min_holdtime;
/* add new peers */
for (p = npeers; p != NULL; p = next) {
next = p->next;
p->next = peers;
peers = p;
}
/* find ones to be deleted */
for (p = peers; p != NULL; p = p->next)
if (p->conf.reconf_action == RECONF_NONE &&
!p->conf.cloned)
p->conf.reconf_action = RECONF_DELETE;
/* delete old listeners */
for (la = TAILQ_FIRST(conf->listen_addrs); la != NULL;
la = nla) {
nla = TAILQ_NEXT(la, entry);
if (la->reconf == RECONF_NONE) {
log_info("not listening on %s any more",
log_sockaddr(
(struct sockaddr *)&la->sa));
TAILQ_REMOVE(conf->listen_addrs, la,
entry);
close(la->fd);
free(la);
}
}
/* add new listeners */
while ((la = TAILQ_FIRST(nconf->listen_addrs)) !=
NULL) {
TAILQ_REMOVE(nconf->listen_addrs, la, entry);
TAILQ_INSERT_TAIL(conf->listen_addrs, la,
entry);
}
setup_listeners(listener_cnt);
free(nconf->listen_addrs);
free(nconf);
nconf = NULL;
session_set = NULL;
pending_reconf = 0;
log_info("SE reconfigured");
break;
case IMSG_FILTER_SET:
if (idx != PFD_PIPE_MAIN)
fatalx("reconf request not from parent");
if (session_set == NULL) {
log_warnx("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);
break;
case IMSG_IFINFO:
if (idx != PFD_PIPE_MAIN)
fatalx("IFINFO message not from parent");
if (imsg.hdr.len != IMSG_HEADER_SIZE +
sizeof(struct kif))
fatalx("IFINFO imsg with wrong len");
kif = imsg.data;
depend_ok = (kif->flags & IFF_UP) &&
(kif->link_state == LINK_STATE_UP ||
(kif->link_state == LINK_STATE_UNKNOWN &&
strncmp(kif->ifname, "carp", 4)));
for (p = peers; p != NULL; p = p->next)
if (!strcmp(p->conf.if_depend, kif->ifname)) {
if (depend_ok && !p->depend_ok) {
p->depend_ok = depend_ok;
bgp_fsm(p, EVNT_START);
} else if (!depend_ok && p->depend_ok) {
p->depend_ok = depend_ok;
bgp_fsm(p, EVNT_STOP);
}
}
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(struct mrt));
if ((xmrt.fd = imsg_get_fd(ibuf)) == -1)
log_warnx("expected to receive fd for mrt dump "
"but didn't receive any");
mrt = mrt_get(&mrthead, &xmrt);
if (mrt == NULL) {
/* new dump */
mrt = calloc(1, sizeof(struct mrt));
if (mrt == NULL)
fatal("session_dispatch_imsg");
memcpy(mrt, &xmrt, sizeof(struct mrt));
TAILQ_INIT(&mrt->bufs);
LIST_INSERT_HEAD(&mrthead, mrt, entry);
} else {
/* old dump reopened */
close(mrt->fd);
mrt->fd = xmrt.fd;
}
break;
case IMSG_MRT_CLOSE:
if (imsg.hdr.len > IMSG_HEADER_SIZE +
sizeof(struct mrt)) {
log_warnx("wrong imsg len");
break;
}
memcpy(&xmrt, imsg.data, sizeof(struct mrt));
mrt = mrt_get(&mrthead, &xmrt);
if (mrt != NULL) {
mrt_clean(mrt);
LIST_REMOVE(mrt, entry);
free(mrt);
}
break;
case IMSG_CTL_KROUTE:
case IMSG_CTL_KROUTE6:
case IMSG_CTL_KROUTE_ADDR:
case IMSG_CTL_SHOW_NEXTHOP:
case IMSG_CTL_SHOW_INTERFACE:
if (idx != PFD_PIPE_MAIN)
fatalx("ctl kroute request not from parent");
control_imsg_relay(&imsg);
break;
case IMSG_CTL_SHOW_RIB:
case IMSG_CTL_SHOW_RIB_PREFIX:
case IMSG_CTL_SHOW_NETWORK:
case IMSG_CTL_SHOW_NEIGHBOR:
if (idx != PFD_PIPE_ROUTE)
fatalx("ctl rib request not from RDE");
control_imsg_relay(&imsg);
break;
case IMSG_CTL_END:
control_imsg_relay(&imsg);
break;
case IMSG_UPDATE:
if (idx != PFD_PIPE_ROUTE)
fatalx("update request not from RDE");
if (imsg.hdr.len > IMSG_HEADER_SIZE +
MAX_PKTSIZE - MSGSIZE_HEADER ||
imsg.hdr.len < IMSG_HEADER_SIZE +
MSGSIZE_UPDATE_MIN - MSGSIZE_HEADER)
log_warnx("RDE sent invalid update");
else
session_update(imsg.hdr.peerid, imsg.data,
imsg.hdr.len - IMSG_HEADER_SIZE);
break;
case IMSG_UPDATE_ERR:
if (idx != PFD_PIPE_ROUTE)
fatalx("update request not from RDE");
if (imsg.hdr.len < IMSG_HEADER_SIZE + 2) {
log_warnx("RDE sent invalid notification");
break;
}
if ((p = getpeerbyid(imsg.hdr.peerid)) == NULL) {
log_warnx("no such peer: id=%u",
imsg.hdr.peerid);
break;
}
data = imsg.data;
errcode = *data++;
subcode = *data++;
if (imsg.hdr.len == IMSG_HEADER_SIZE + 2)
data = NULL;
session_notification(p, errcode, subcode,
data, imsg.hdr.len - IMSG_HEADER_SIZE - 2);
switch (errcode) {
case ERR_CEASE:
switch (subcode) {
case ERR_CEASE_MAX_PREFIX:
bgp_fsm(p, EVNT_STOP);
break;
default:
bgp_fsm(p, EVNT_CON_FATAL);
break;
}
break;
default:
bgp_fsm(p, EVNT_CON_FATAL);
break;
}
break;
default:
break;
}
imsg_free(&imsg);
}
}
int
la_cmp(struct listen_addr *a, struct listen_addr *b)
{
struct sockaddr_in *in_a, *in_b;
struct sockaddr_in6 *in6_a, *in6_b;
if (a->sa.ss_family != b->sa.ss_family)
return (1);
switch (a->sa.ss_family) {
case AF_INET:
in_a = (struct sockaddr_in *)&a->sa;
in_b = (struct sockaddr_in *)&b->sa;
if (in_a->sin_addr.s_addr != in_b->sin_addr.s_addr)
return (1);
if (in_a->sin_port != in_b->sin_port)
return (1);
break;
case AF_INET6:
in6_a = (struct sockaddr_in6 *)&a->sa;
in6_b = (struct sockaddr_in6 *)&b->sa;
if (bcmp(&in6_a->sin6_addr, &in6_b->sin6_addr,
sizeof(struct in6_addr)))
return (1);
if (in6_a->sin6_port != in6_b->sin6_port)
return (1);
break;
default:
fatal("king bula sez: unknown address family");
/* NOTREACHED */
}
return (0);
}
struct peer *
getpeerbyaddr(struct bgpd_addr *addr)
{
struct peer *p;
/* we might want a more effective way to find peers by IP */
for (p = peers; p != NULL &&
memcmp(&p->conf.remote_addr, addr, sizeof(p->conf.remote_addr));
p = p->next)
; /* nothing */
return (p);
}
struct peer *
getpeerbydesc(const char *descr)
{
struct peer *p, *res = NULL;
int match = 0;
for (p = peers; p != NULL; p = p->next)
if (!strcmp(p->conf.descr, descr)) {
res = p;
match++;
}
if (match > 1)
log_info("neighbor description \"%s\" not unique, request "
"aborted", descr);
if (match == 1)
return (res);
else
return (NULL);
}
struct peer *
getpeerbyip(struct sockaddr *ip)
{
struct peer *p, *newpeer, *loose = NULL;
u_int32_t id;
/* we might want a more effective way to find peers by IP */
for (p = peers; p != NULL; p = p->next)
if (!p->conf.template &&
p->conf.remote_addr.af == ip->sa_family) {
if (p->conf.remote_addr.af == AF_INET &&
p->conf.remote_addr.v4.s_addr ==
((struct sockaddr_in *)ip)->sin_addr.s_addr)
return (p);
if (p->conf.remote_addr.af == AF_INET6 &&
!bcmp(&p->conf.remote_addr.v6,
&((struct sockaddr_in6 *)ip)->sin6_addr,
sizeof(p->conf.remote_addr.v6)))
return (p);
}
/* try template matching */
for (p = peers; p != NULL; p = p->next)
if (p->conf.template &&
p->conf.remote_addr.af == ip->sa_family &&
session_match_mask(p, ip))
if (loose == NULL || loose->conf.remote_masklen <
p->conf.remote_masklen)
loose = p;
if (loose != NULL) {
/* clone */
if ((newpeer = malloc(sizeof(struct peer))) == NULL)
fatal(NULL);
memcpy(newpeer, loose, sizeof(struct peer));
for (id = UINT_MAX; id > UINT_MAX / 2; id--) {
for (p = peers; p != NULL && p->conf.id != id;
p = p->next)
; /* nothing */
if (p == NULL) { /* we found a free id */
newpeer->conf.id = id;
break;
}
}
if (newpeer->conf.remote_addr.af == AF_INET) {
newpeer->conf.remote_addr.v4.s_addr =
((struct sockaddr_in *)ip)->sin_addr.s_addr;
newpeer->conf.remote_masklen = 32;
}
if (newpeer->conf.remote_addr.af == AF_INET6) {
memcpy(&p->conf.remote_addr.v6,
&((struct sockaddr_in6 *)ip)->sin6_addr,
sizeof(newpeer->conf.remote_addr.v6));
newpeer->conf.remote_masklen = 128;
}
newpeer->conf.template = 0;
newpeer->conf.cloned = 1;
newpeer->state = newpeer->prev_state = STATE_NONE;
newpeer->rbuf = NULL;
init_peer(newpeer);
bgp_fsm(newpeer, EVNT_START);
newpeer->next = peers;
peers = newpeer;
return (newpeer);
}
return (NULL);
}
int
session_match_mask(struct peer *p, struct sockaddr *ip)
{
int i;
in_addr_t v4mask;
struct in6_addr *in;
struct in6_addr mask;
if (p->conf.remote_addr.af == AF_INET) {
v4mask = htonl(prefixlen2mask(p->conf.remote_masklen));
if (p->conf.remote_addr.v4.s_addr ==
((((struct sockaddr_in *)ip)->sin_addr.s_addr) & v4mask))
return (1);
else
return (0);
}
if (p->conf.remote_addr.af == AF_INET6) {
bzero(&mask, sizeof(mask));
for (i = 0; i < p->conf.remote_masklen / 8; i++)
mask.s6_addr[i] = 0xff;
i = p->conf.remote_masklen % 8;
if (i)
mask.s6_addr[p->conf.remote_masklen / 8] = 0xff00 >> i;
in = &((struct sockaddr_in6 *)ip)->sin6_addr;
for (i = 0; i < 16; i++)
if ((in->s6_addr[i] & mask.s6_addr[i]) !=
p->conf.remote_addr.addr8[i])
return (0);
return (1);
}
return (0);
}
struct peer *
getpeerbyid(u_int32_t peerid)
{
struct peer *p;
/* we might want a more effective way to find peers by IP */
for (p = peers; p != NULL &&
p->conf.id != peerid; p = p->next)
; /* nothing */
return (p);
}
void
session_down(struct peer *peer)
{
peer->stats.last_updown = time(NULL);
if (imsg_compose(ibuf_rde, IMSG_SESSION_DOWN, peer->conf.id, 0, -1,
NULL, 0) == -1)
fatalx("imsg_compose error");
}
void
session_up(struct peer *p)
{
struct session_up sup;
struct filter_set *s;
if (imsg_compose(ibuf_rde, IMSG_SESSION_ADD, p->conf.id, 0, -1,
&p->conf, sizeof(p->conf)) == -1)
fatalx("imsg_compose error");
TAILQ_FOREACH(s, &p->conf.attrset, entry) {
if (imsg_compose(ibuf_rde, IMSG_FILTER_SET, p->conf.id, 0, -1,
s, sizeof(struct filter_set)) == -1)
fatalx("imsg_compose error");
}
switch (p->sa_local.ss_family) {
case AF_INET:
sup.local_addr.af = AF_INET;
memcpy(&sup.local_addr.v4,
&((struct sockaddr_in *)&p->sa_local)->sin_addr,
sizeof(sup.local_addr.v4));
sup.remote_addr.af = AF_INET;
memcpy(&sup.remote_addr.v4,
&((struct sockaddr_in *)&p->sa_remote)->sin_addr,
sizeof(sup.remote_addr.v4));
break;
case AF_INET6:
sup.local_addr.af = AF_INET6;
memcpy(&sup.local_addr.v6,
&((struct sockaddr_in6 *)&p->sa_local)->sin6_addr,
sizeof(sup.local_addr.v6));
sup.remote_addr.af = AF_INET6;
memcpy(&sup.remote_addr.v6,
&((struct sockaddr_in6 *)&p->sa_remote)->sin6_addr,
sizeof(sup.remote_addr.v6));
break;
default:
fatalx("session_up: unsupported address family");
}
sup.remote_bgpid = p->remote_bgpid;
memcpy(&sup.capa_announced, &p->capa.ann, sizeof(sup.capa_announced));
memcpy(&sup.capa_received, &p->capa.peer, sizeof(sup.capa_received));
p->stats.last_updown = time(NULL);
if (imsg_compose(ibuf_rde, IMSG_SESSION_UP, p->conf.id, 0, -1,
&sup, sizeof(sup)) == -1)
fatalx("imsg_compose error");
}
int
imsg_compose_parent(int type, pid_t pid, void *data, u_int16_t datalen)
{
return (imsg_compose(ibuf_main, type, 0, pid, -1, data, datalen));
}
int
imsg_compose_rde(int type, pid_t pid, void *data, u_int16_t datalen)
{
return (imsg_compose(ibuf_rde, type, 0, pid, -1, data, datalen));
}
static struct sockaddr *
addr2sa(struct bgpd_addr *addr, u_int16_t port)
{
static struct sockaddr_storage ss;
struct sockaddr_in *sa_in = (struct sockaddr_in *)&ss;
struct sockaddr_in6 *sa_in6 = (struct sockaddr_in6 *)&ss;
bzero(&ss, sizeof(ss));
switch (addr->af) {
case AF_INET:
sa_in->sin_family = AF_INET;
sa_in->sin_len = sizeof(struct sockaddr_in);
sa_in->sin_addr.s_addr = addr->v4.s_addr;
sa_in->sin_port = htons(port);
break;
case AF_INET6:
sa_in6->sin6_family = AF_INET6;
sa_in6->sin6_len = sizeof(struct sockaddr_in6);
memcpy(&sa_in6->sin6_addr, &addr->v6,
sizeof(sa_in6->sin6_addr));
sa_in6->sin6_port = htons(port);
sa_in6->sin6_scope_id = addr->scope_id;
break;
}
return ((struct sockaddr *)&ss);
}
|