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
|
/* Zebra daemon server routine.
* Copyright (C) 1997, 98, 99 Kunihiro Ishiguro
*
* This file is part of GNU Zebra.
*
* GNU Zebra is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation; either version 2, or (at your option) any
* later version.
*
* GNU Zebra is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with GNU Zebra; see the file COPYING. If not, write to the
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*/
#include <zebra.h>
#include "prefix.h"
#include "command.h"
#include "if.h"
#include "thread.h"
#include "stream.h"
#include "memory.h"
#include "zebra_memory.h"
#include "table.h"
#include "rib.h"
#include "network.h"
#include "sockunion.h"
#include "log.h"
#include "zclient.h"
#include "privs.h"
#include "network.h"
#include "buffer.h"
#include "nexthop.h"
#include "vrf.h"
#include "zebra/zserv.h"
#include "zebra/zebra_ns.h"
#include "zebra/zebra_vrf.h"
#include "zebra/router-id.h"
#include "zebra/redistribute.h"
#include "zebra/debug.h"
#include "zebra/ipforward.h"
#include "zebra/zebra_rnh.h"
#include "zebra/rt_netlink.h"
#include "zebra/interface.h"
#include "zebra/zebra_ptm.h"
#include "zebra/rtadv.h"
/* Event list of zebra. */
enum event { ZEBRA_SERV, ZEBRA_READ, ZEBRA_WRITE };
static void zebra_event (enum event event, int sock, struct zserv *client);
extern struct zebra_privs_t zserv_privs;
static void zebra_client_close (struct zserv *client);
static int
zserv_delayed_close(struct thread *thread)
{
struct zserv *client = THREAD_ARG(thread);
client->t_suicide = NULL;
zebra_client_close(client);
return 0;
}
static int
zserv_flush_data(struct thread *thread)
{
struct zserv *client = THREAD_ARG(thread);
client->t_write = NULL;
if (client->t_suicide)
{
zebra_client_close(client);
return -1;
}
switch (buffer_flush_available(client->wb, client->sock))
{
case BUFFER_ERROR:
zlog_warn("%s: buffer_flush_available failed on zserv client fd %d, "
"closing", __func__, client->sock);
zebra_client_close(client);
break;
case BUFFER_PENDING:
client->t_write = thread_add_write(zebrad.master, zserv_flush_data,
client, client->sock);
break;
case BUFFER_EMPTY:
break;
}
client->last_write_time = quagga_monotime();
return 0;
}
int
zebra_server_send_message(struct zserv *client)
{
if (client->t_suicide)
return -1;
stream_set_getp(client->obuf, 0);
client->last_write_cmd = stream_getw_from(client->obuf, 6);
switch (buffer_write(client->wb, client->sock, STREAM_DATA(client->obuf),
stream_get_endp(client->obuf)))
{
case BUFFER_ERROR:
zlog_warn("%s: buffer_write failed to zserv client fd %d, closing",
__func__, client->sock);
/* Schedule a delayed close since many of the functions that call this
one do not check the return code. They do not allow for the
possibility that an I/O error may have caused the client to be
deleted. */
client->t_suicide = thread_add_event(zebrad.master, zserv_delayed_close,
client, 0);
return -1;
case BUFFER_EMPTY:
THREAD_OFF(client->t_write);
break;
case BUFFER_PENDING:
THREAD_WRITE_ON(zebrad.master, client->t_write,
zserv_flush_data, client, client->sock);
break;
}
client->last_write_time = quagga_monotime();
return 0;
}
void
zserv_create_header (struct stream *s, uint16_t cmd, vrf_id_t vrf_id)
{
/* length placeholder, caller can update */
stream_putw (s, ZEBRA_HEADER_SIZE);
stream_putc (s, ZEBRA_HEADER_MARKER);
stream_putc (s, ZSERV_VERSION);
stream_putw (s, vrf_id);
stream_putw (s, cmd);
}
static void
zserv_encode_interface (struct stream *s, struct interface *ifp)
{
/* Interface information. */
stream_put (s, ifp->name, INTERFACE_NAMSIZ);
stream_putl (s, ifp->ifindex);
stream_putc (s, ifp->status);
stream_putq (s, ifp->flags);
stream_putc (s, ifp->ptm_enable);
stream_putc (s, ifp->ptm_status);
stream_putl (s, ifp->metric);
stream_putl (s, ifp->mtu);
stream_putl (s, ifp->mtu6);
stream_putl (s, ifp->bandwidth);
stream_putl (s, ifp->ll_type);
stream_putl (s, ifp->hw_addr_len);
if (ifp->hw_addr_len)
stream_put (s, ifp->hw_addr, ifp->hw_addr_len);
/* Then, Traffic Engineering parameters if any */
if (HAS_LINK_PARAMS(ifp) && IS_LINK_PARAMS_SET(ifp->link_params))
{
stream_putc (s, 1);
zebra_interface_link_params_write (s, ifp);
}
else
stream_putc (s, 0);
/* Write packet size. */
stream_putw_at (s, 0, stream_get_endp (s));
}
static void
zserv_encode_vrf (struct stream *s, struct zebra_vrf *zvrf)
{
/* Interface information. */
stream_put (s, zvrf->name, VRF_NAMSIZ);
/* Write packet size. */
stream_putw_at (s, 0, stream_get_endp (s));
}
/* Interface is added. Send ZEBRA_INTERFACE_ADD to client. */
/*
* This function is called in the following situations:
* - in response to a 3-byte ZEBRA_INTERFACE_ADD request
* from the client.
* - at startup, when zebra figures out the available interfaces
* - when an interface is added (where support for
* RTM_IFANNOUNCE or AF_NETLINK sockets is available), or when
* an interface is marked IFF_UP (i.e., an RTM_IFINFO message is
* received)
*/
int
zsend_interface_add (struct zserv *client, struct interface *ifp)
{
struct stream *s;
s = client->obuf;
stream_reset (s);
zserv_create_header (s, ZEBRA_INTERFACE_ADD, ifp->vrf_id);
zserv_encode_interface (s, ifp);
client->ifadd_cnt++;
return zebra_server_send_message(client);
}
/* Interface deletion from zebra daemon. */
int
zsend_interface_delete (struct zserv *client, struct interface *ifp)
{
struct stream *s;
s = client->obuf;
stream_reset (s);
zserv_create_header (s, ZEBRA_INTERFACE_DELETE, ifp->vrf_id);
zserv_encode_interface (s, ifp);
client->ifdel_cnt++;
return zebra_server_send_message (client);
}
int
zsend_vrf_add (struct zserv *client, struct zebra_vrf *zvrf)
{
struct stream *s;
s = client->obuf;
stream_reset (s);
zserv_create_header (s, ZEBRA_VRF_ADD, zvrf->vrf_id);
zserv_encode_vrf (s, zvrf);
client->vrfadd_cnt++;
return zebra_server_send_message(client);
}
/* VRF deletion from zebra daemon. */
int
zsend_vrf_delete (struct zserv *client, struct zebra_vrf *zvrf)
{
struct stream *s;
s = client->obuf;
stream_reset (s);
zserv_create_header (s, ZEBRA_VRF_DELETE, zvrf->vrf_id);
zserv_encode_vrf (s, zvrf);
client->vrfdel_cnt++;
return zebra_server_send_message (client);
}
int
zsend_interface_link_params (struct zserv *client, struct interface *ifp)
{
struct stream *s;
/* Check this client need interface information. */
if (! client->ifinfo)
return 0;
if (!ifp->link_params)
return 0;
s = client->obuf;
stream_reset (s);
zserv_create_header (s, ZEBRA_INTERFACE_LINK_PARAMS, ifp->vrf_id);
/* Add Interface Index */
stream_putl (s, ifp->ifindex);
/* Then TE Link Parameters */
if (zebra_interface_link_params_write (s, ifp) == 0)
return 0;
/* Write packet size. */
stream_putw_at (s, 0, stream_get_endp (s));
return zebra_server_send_message (client);
}
/* Interface address is added/deleted. Send ZEBRA_INTERFACE_ADDRESS_ADD or
* ZEBRA_INTERFACE_ADDRESS_DELETE to the client.
*
* A ZEBRA_INTERFACE_ADDRESS_ADD is sent in the following situations:
* - in response to a 3-byte ZEBRA_INTERFACE_ADD request
* from the client, after the ZEBRA_INTERFACE_ADD has been
* sent from zebra to the client
* - redistribute new address info to all clients in the following situations
* - at startup, when zebra figures out the available interfaces
* - when an interface is added (where support for
* RTM_IFANNOUNCE or AF_NETLINK sockets is available), or when
* an interface is marked IFF_UP (i.e., an RTM_IFINFO message is
* received)
* - for the vty commands "ip address A.B.C.D/M [<secondary>|<label LINE>]"
* and "no bandwidth <1-10000000>", "ipv6 address X:X::X:X/M"
* - when an RTM_NEWADDR message is received from the kernel,
*
* The call tree that triggers ZEBRA_INTERFACE_ADDRESS_DELETE:
*
* zsend_interface_address(DELETE)
* ^
* |
* zebra_interface_address_delete_update
* ^ ^ ^
* | | if_delete_update
* | |
* ip_address_uninstall connected_delete_ipv4
* [ipv6_addresss_uninstall] [connected_delete_ipv6]
* ^ ^
* | |
* | RTM_NEWADDR on routing/netlink socket
* |
* vty commands:
* "no ip address A.B.C.D/M [label LINE]"
* "no ip address A.B.C.D/M secondary"
* ["no ipv6 address X:X::X:X/M"]
*
*/
int
zsend_interface_address (int cmd, struct zserv *client,
struct interface *ifp, struct connected *ifc)
{
int blen;
struct stream *s;
struct prefix *p;
s = client->obuf;
stream_reset (s);
zserv_create_header (s, cmd, ifp->vrf_id);
stream_putl (s, ifp->ifindex);
/* Interface address flag. */
stream_putc (s, ifc->flags);
/* Prefix information. */
p = ifc->address;
stream_putc (s, p->family);
blen = prefix_blen (p);
stream_put (s, &p->u.prefix, blen);
/*
* XXX gnu version does not send prefixlen for ZEBRA_INTERFACE_ADDRESS_DELETE
* but zebra_interface_address_delete_read() in the gnu version
* expects to find it
*/
stream_putc (s, p->prefixlen);
/* Destination. */
p = ifc->destination;
if (p)
stream_put (s, &p->u.prefix, blen);
else
stream_put (s, NULL, blen);
/* Write packet size. */
stream_putw_at (s, 0, stream_get_endp (s));
client->connected_rt_add_cnt++;
return zebra_server_send_message(client);
}
static int
zsend_interface_nbr_address (int cmd, struct zserv *client,
struct interface *ifp, struct nbr_connected *ifc)
{
int blen;
struct stream *s;
struct prefix *p;
s = client->obuf;
stream_reset (s);
zserv_create_header (s, cmd, ifp->vrf_id);
stream_putl (s, ifp->ifindex);
/* Prefix information. */
p = ifc->address;
stream_putc (s, p->family);
blen = prefix_blen (p);
stream_put (s, &p->u.prefix, blen);
/*
* XXX gnu version does not send prefixlen for ZEBRA_INTERFACE_ADDRESS_DELETE
* but zebra_interface_address_delete_read() in the gnu version
* expects to find it
*/
stream_putc (s, p->prefixlen);
/* Write packet size. */
stream_putw_at (s, 0, stream_get_endp (s));
return zebra_server_send_message(client);
}
/* Interface address addition. */
static void
zebra_interface_nbr_address_add_update (struct interface *ifp,
struct nbr_connected *ifc)
{
struct listnode *node, *nnode;
struct zserv *client;
struct prefix *p;
if (IS_ZEBRA_DEBUG_EVENT)
{
char buf[INET6_ADDRSTRLEN];
p = ifc->address;
zlog_debug ("MESSAGE: ZEBRA_INTERFACE_NBR_ADDRESS_ADD %s/%d on %s",
inet_ntop (p->family, &p->u.prefix, buf, INET6_ADDRSTRLEN),
p->prefixlen, ifc->ifp->name);
}
for (ALL_LIST_ELEMENTS (zebrad.client_list, node, nnode, client))
zsend_interface_nbr_address (ZEBRA_INTERFACE_NBR_ADDRESS_ADD, client, ifp, ifc);
}
/* Interface address deletion. */
static void
zebra_interface_nbr_address_delete_update (struct interface *ifp,
struct nbr_connected *ifc)
{
struct listnode *node, *nnode;
struct zserv *client;
struct prefix *p;
if (IS_ZEBRA_DEBUG_EVENT)
{
char buf[INET6_ADDRSTRLEN];
p = ifc->address;
zlog_debug ("MESSAGE: ZEBRA_INTERFACE_NBR_ADDRESS_DELETE %s/%d on %s",
inet_ntop (p->family, &p->u.prefix, buf, INET6_ADDRSTRLEN),
p->prefixlen, ifc->ifp->name);
}
for (ALL_LIST_ELEMENTS (zebrad.client_list, node, nnode, client))
zsend_interface_nbr_address (ZEBRA_INTERFACE_NBR_ADDRESS_DELETE, client, ifp, ifc);
}
/* Send addresses on interface to client */
int
zsend_interface_addresses (struct zserv *client, struct interface *ifp)
{
struct listnode *cnode, *cnnode;
struct connected *c;
struct nbr_connected *nc;
/* Send interface addresses. */
for (ALL_LIST_ELEMENTS (ifp->connected, cnode, cnnode, c))
{
if (!CHECK_FLAG (c->conf, ZEBRA_IFC_REAL))
continue;
if (zsend_interface_address (ZEBRA_INTERFACE_ADDRESS_ADD, client,
ifp, c) < 0)
return -1;
}
/* Send interface neighbors. */
for (ALL_LIST_ELEMENTS (ifp->nbr_connected, cnode, cnnode, nc))
{
if (zsend_interface_nbr_address (ZEBRA_INTERFACE_NBR_ADDRESS_ADD,
client, ifp, nc) < 0)
return -1;
}
return 0;
}
/* Notify client about interface moving from one VRF to another.
* Whether client is interested in old and new VRF is checked by caller.
*/
int
zsend_interface_vrf_update (struct zserv *client, struct interface *ifp,
vrf_id_t vrf_id)
{
struct stream *s;
s = client->obuf;
stream_reset (s);
zserv_create_header (s, ZEBRA_INTERFACE_VRF_UPDATE, ifp->vrf_id);
/* Fill in the ifIndex of the interface and its new VRF (id) */
stream_putl (s, ifp->ifindex);
stream_putw (s, vrf_id);
/* Write packet size. */
stream_putw_at (s, 0, stream_get_endp (s));
client->if_vrfchg_cnt++;
return zebra_server_send_message(client);
}
/* Add new nbr connected IPv6 address */
void
nbr_connected_add_ipv6 (struct interface *ifp, struct in6_addr *address)
{
struct nbr_connected *ifc;
struct prefix p;
p.family = AF_INET6;
IPV6_ADDR_COPY (&p.u.prefix, address);
p.prefixlen = IPV6_MAX_PREFIXLEN;
if (!(ifc = listnode_head(ifp->nbr_connected)))
{
/* new addition */
ifc = nbr_connected_new ();
ifc->address = prefix_new();
ifc->ifp = ifp;
listnode_add (ifp->nbr_connected, ifc);
}
prefix_copy(ifc->address, &p);
zebra_interface_nbr_address_add_update (ifp, ifc);
if_nbr_ipv6ll_to_ipv4ll_neigh_update (ifp, address, 1);
}
void
nbr_connected_delete_ipv6 (struct interface *ifp, struct in6_addr *address)
{
struct nbr_connected *ifc;
struct prefix p;
p.family = AF_INET6;
IPV6_ADDR_COPY (&p.u.prefix, address);
p.prefixlen = IPV6_MAX_PREFIXLEN;
ifc = nbr_connected_check(ifp, &p);
if (!ifc)
return;
listnode_delete (ifp->nbr_connected, ifc);
zebra_interface_nbr_address_delete_update (ifp, ifc);
if_nbr_ipv6ll_to_ipv4ll_neigh_update (ifp, address, 0);
nbr_connected_free (ifc);
}
/*
* The cmd passed to zsend_interface_update may be ZEBRA_INTERFACE_UP or
* ZEBRA_INTERFACE_DOWN.
*
* The ZEBRA_INTERFACE_UP message is sent from the zebra server to
* the clients in one of 2 situations:
* - an if_up is detected e.g., as a result of an RTM_IFINFO message
* - a vty command modifying the bandwidth of an interface is received.
* The ZEBRA_INTERFACE_DOWN message is sent when an if_down is detected.
*/
int
zsend_interface_update (int cmd, struct zserv *client, struct interface *ifp)
{
struct stream *s;
s = client->obuf;
stream_reset (s);
zserv_create_header (s, cmd, ifp->vrf_id);
zserv_encode_interface (s, ifp);
if (cmd == ZEBRA_INTERFACE_UP)
client->ifup_cnt++;
else
client->ifdown_cnt++;
return zebra_server_send_message(client);
}
/*
* This is the new function to announce and withdraw redistributed routes, used
* by Zebra. This is the old zsend_route_multipath() function. That function
* was duplicating code to send a lot of information that was essentially thrown
* away or ignored by the receiver. This is the leaner function that is not a
* duplicate of the zapi_ipv4_route_add/del.
*
* The primary difference is that this function merely sends a single NH instead of
* all the nexthops.
*/
int
zsend_redistribute_route (int cmd, struct zserv *client, struct prefix *p,
struct rib *rib)
{
int psize;
struct stream *s;
struct nexthop *nexthop;
unsigned long nhnummark = 0, messmark = 0;
int nhnum = 0;
u_char zapi_flags = 0;
struct nexthop dummy_nh;
/* Came from VRF lib patch, is this really needed? callers of this routine
do check for redist.., so may be its not needed.
Check this client need this route.
if (!vrf_bitmap_check (client->redist[family2afi(p->family)][rib->type],
rib->vrf_id) &&
!(is_default (p) &&
vrf_bitmap_check (client->redist_default, rib->vrf_id)))
return 0;
*/
s = client->obuf;
stream_reset (s);
memset(&dummy_nh, 0, sizeof(struct nexthop));
zserv_create_header (s, cmd, rib->vrf_id);
/* Put type and nexthop. */
stream_putc (s, rib->type);
stream_putw (s, rib->instance);
stream_putc (s, rib->flags);
/* marker for message flags field */
messmark = stream_get_endp (s);
stream_putc (s, 0);
/* Prefix. */
psize = PSIZE (p->prefixlen);
stream_putc (s, p->prefixlen);
stream_write (s, (u_char *) & p->u.prefix, psize);
for (nexthop = rib->nexthop; nexthop; nexthop = nexthop->next)
{
/* We don't send any nexthops when there's a multipath */
if (rib->nexthop_active_num > 1)
{
SET_FLAG (zapi_flags, ZAPI_MESSAGE_NEXTHOP);
SET_FLAG (zapi_flags, ZAPI_MESSAGE_IFINDEX);
stream_putc(s, 1);
if (p->family == AF_INET)
{
stream_put_in_addr (s, &dummy_nh.gate.ipv4);
}
else if (p->family == AF_INET6)
{
stream_write (s, (u_char *) &dummy_nh.gate.ipv6, 16);
}
else
{
/* We don't handle anything else now, abort */
zlog_err("%s: Unable to redistribute route of unknown family, %d\n",
__func__, p->family);
return -1;
}
stream_putc (s, 1);
stream_putl (s, 0); /* dummy ifindex */
break;
}
if (CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_FIB)
|| nexthop_has_fib_child(nexthop))
{
SET_FLAG (zapi_flags, ZAPI_MESSAGE_NEXTHOP);
SET_FLAG (zapi_flags, ZAPI_MESSAGE_IFINDEX);
if (nhnummark == 0)
{
nhnummark = stream_get_endp (s);
stream_putc (s, 1); /* placeholder */
}
nhnum++;
switch(nexthop->type)
{
case NEXTHOP_TYPE_IPV4:
case NEXTHOP_TYPE_IPV4_IFINDEX:
stream_put_in_addr (s, &nexthop->gate.ipv4);
break;
case NEXTHOP_TYPE_IPV6:
case NEXTHOP_TYPE_IPV6_IFINDEX:
/* Only BGP supports IPv4 prefix with IPv6 NH, so kill this */
if (p->family == AF_INET)
stream_put_in_addr(s, &dummy_nh.gate.ipv4);
else
stream_write (s, (u_char *) &nexthop->gate.ipv6, 16);
break;
default:
if (cmd == ZEBRA_REDISTRIBUTE_IPV4_ADD
|| cmd == ZEBRA_REDISTRIBUTE_IPV4_DEL)
{
struct in_addr empty;
memset (&empty, 0, sizeof (struct in_addr));
stream_write (s, (u_char *) &empty, IPV4_MAX_BYTELEN);
}
else
{
struct in6_addr empty;
memset (&empty, 0, sizeof (struct in6_addr));
stream_write (s, (u_char *) &empty, IPV6_MAX_BYTELEN);
}
}
/* Interface index. */
stream_putc (s, 1);
stream_putl (s, nexthop->ifindex);
break;
}
}
/* Metric */
if (cmd == ZEBRA_REDISTRIBUTE_IPV4_ADD || cmd == ZEBRA_REDISTRIBUTE_IPV6_ADD)
{
SET_FLAG (zapi_flags, ZAPI_MESSAGE_DISTANCE);
stream_putc (s, rib->distance);
SET_FLAG (zapi_flags, ZAPI_MESSAGE_METRIC);
stream_putl (s, rib->metric);
/* tag */
if (rib->tag)
{
SET_FLAG(zapi_flags, ZAPI_MESSAGE_TAG);
stream_putw(s, rib->tag);
}
SET_FLAG (zapi_flags, ZAPI_MESSAGE_MTU);
stream_putl (s, rib->mtu);
}
/* write real message flags value */
stream_putc_at (s, messmark, zapi_flags);
/* Write next-hop number */
if (nhnummark)
stream_putc_at (s, nhnummark, nhnum);
/* Write packet size. */
stream_putw_at (s, 0, stream_get_endp (s));
return zebra_server_send_message(client);
}
static int
zsend_write_nexthop (struct stream *s, struct nexthop *nexthop)
{
stream_putc (s, nexthop->type);
switch (nexthop->type)
{
case NEXTHOP_TYPE_IPV4:
stream_put_in_addr (s, &nexthop->gate.ipv4);
break;
case NEXTHOP_TYPE_IPV4_IFINDEX:
stream_put_in_addr (s, &nexthop->gate.ipv4);
stream_putl (s, nexthop->ifindex);
break;
case NEXTHOP_TYPE_IPV6:
stream_put (s, &nexthop->gate.ipv6, 16);
break;
case NEXTHOP_TYPE_IPV6_IFINDEX:
stream_put (s, &nexthop->gate.ipv6, 16);
stream_putl (s, nexthop->ifindex);
break;
case NEXTHOP_TYPE_IFINDEX:
stream_putl (s, nexthop->ifindex);
break;
default:
/* do nothing */
break;
}
return 1;
}
/* Nexthop register */
static int
zserv_rnh_register (struct zserv *client, int sock, u_short length,
rnh_type_t type, struct zebra_vrf *zvrf)
{
struct rnh *rnh;
struct stream *s;
struct prefix p;
u_short l = 0;
u_char flags = 0;
if (IS_ZEBRA_DEBUG_NHT)
zlog_debug("rnh_register msg from client %s: length=%d, type=%s\n",
zebra_route_string(client->proto), length,
(type == RNH_NEXTHOP_TYPE) ? "nexthop" : "route");
s = client->ibuf;
client->nh_reg_time = quagga_monotime();
while (l < length)
{
flags = stream_getc(s);
p.family = stream_getw(s);
p.prefixlen = stream_getc(s);
l += 4;
if (p.family == AF_INET)
{
p.u.prefix4.s_addr = stream_get_ipv4(s);
l += IPV4_MAX_BYTELEN;
}
else if (p.family == AF_INET6)
{
stream_get(&p.u.prefix6, s, IPV6_MAX_BYTELEN);
l += IPV6_MAX_BYTELEN;
}
else
{
zlog_err("rnh_register: Received unknown family type %d\n",
p.family);
return -1;
}
rnh = zebra_add_rnh(&p, zvrf->vrf_id, type);
if (type == RNH_NEXTHOP_TYPE)
{
if (flags && !CHECK_FLAG(rnh->flags, ZEBRA_NHT_CONNECTED))
SET_FLAG(rnh->flags, ZEBRA_NHT_CONNECTED);
else if (!flags && CHECK_FLAG(rnh->flags, ZEBRA_NHT_CONNECTED))
UNSET_FLAG(rnh->flags, ZEBRA_NHT_CONNECTED);
}
else if (type == RNH_IMPORT_CHECK_TYPE)
{
if (flags && !CHECK_FLAG(rnh->flags, ZEBRA_NHT_EXACT_MATCH))
SET_FLAG(rnh->flags, ZEBRA_NHT_EXACT_MATCH);
else if (!flags && CHECK_FLAG(rnh->flags, ZEBRA_NHT_EXACT_MATCH))
UNSET_FLAG(rnh->flags, ZEBRA_NHT_EXACT_MATCH);
}
zebra_add_rnh_client(rnh, client, type, zvrf->vrf_id);
/* Anything not AF_INET/INET6 has been filtered out above */
zebra_evaluate_rnh(zvrf->vrf_id, p.family, 1, type, &p);
}
return 0;
}
/* Nexthop register */
static int
zserv_rnh_unregister (struct zserv *client, int sock, u_short length,
rnh_type_t type, struct zebra_vrf *zvrf)
{
struct rnh *rnh;
struct stream *s;
struct prefix p;
u_short l = 0;
if (IS_ZEBRA_DEBUG_NHT)
zlog_debug("rnh_unregister msg from client %s: length=%d\n",
zebra_route_string(client->proto), length);
s = client->ibuf;
while (l < length)
{
(void)stream_getc(s); //Connected or not. Not used in this function
p.family = stream_getw(s);
p.prefixlen = stream_getc(s);
l += 4;
if (p.family == AF_INET)
{
p.u.prefix4.s_addr = stream_get_ipv4(s);
l += IPV4_MAX_BYTELEN;
}
else if (p.family == AF_INET6)
{
stream_get(&p.u.prefix6, s, IPV6_MAX_BYTELEN);
l += IPV6_MAX_BYTELEN;
}
else
{
zlog_err("rnh_register: Received unknown family type %d\n",
p.family);
return -1;
}
rnh = zebra_lookup_rnh(&p, zvrf->vrf_id, type);
if (rnh)
{
client->nh_dereg_time = quagga_monotime();
zebra_remove_rnh_client(rnh, client, type);
}
}
return 0;
}
/*
Modified version of zsend_ipv4_nexthop_lookup():
Query unicast rib if nexthop is not found on mrib.
Returns both route metric and protocol distance.
*/
static int
zsend_ipv4_nexthop_lookup_mrib (struct zserv *client, struct in_addr addr, struct rib *rib, struct zebra_vrf *zvrf)
{
struct stream *s;
unsigned long nump;
u_char num;
struct nexthop *nexthop;
/* Get output stream. */
s = client->obuf;
stream_reset (s);
/* Fill in result. */
zserv_create_header (s, ZEBRA_IPV4_NEXTHOP_LOOKUP_MRIB, zvrf->vrf_id);
stream_put_in_addr (s, &addr);
if (rib)
{
stream_putc (s, rib->distance);
stream_putl (s, rib->metric);
num = 0;
nump = stream_get_endp(s); /* remember position for nexthop_num */
stream_putc (s, 0); /* reserve room for nexthop_num */
/* Only non-recursive routes are elegible to resolve the nexthop we
* are looking up. Therefore, we will just iterate over the top
* chain of nexthops. */
for (nexthop = rib->nexthop; nexthop; nexthop = nexthop->next)
if (CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_FIB))
num += zsend_write_nexthop (s, nexthop);
stream_putc_at (s, nump, num); /* store nexthop_num */
}
else
{
stream_putc (s, 0); /* distance */
stream_putl (s, 0); /* metric */
stream_putc (s, 0); /* nexthop_num */
}
stream_putw_at (s, 0, stream_get_endp (s));
return zebra_server_send_message(client);
}
/* Router-id is updated. Send ZEBRA_ROUTER_ID_ADD to client. */
int
zsend_router_id_update (struct zserv *client, struct prefix *p,
vrf_id_t vrf_id)
{
struct stream *s;
int blen;
/* Check this client need interface information. */
if (! vrf_bitmap_check (client->ridinfo, vrf_id))
return 0;
s = client->obuf;
stream_reset (s);
/* Message type. */
zserv_create_header (s, ZEBRA_ROUTER_ID_UPDATE, vrf_id);
/* Prefix information. */
stream_putc (s, p->family);
blen = prefix_blen (p);
stream_put (s, &p->u.prefix, blen);
stream_putc (s, p->prefixlen);
/* Write packet size. */
stream_putw_at (s, 0, stream_get_endp (s));
return zebra_server_send_message(client);
}
/* Register zebra server interface information. Send current all
interface and address information. */
static int
zread_interface_add (struct zserv *client, u_short length, struct zebra_vrf *zvrf)
{
struct listnode *ifnode, *ifnnode;
vrf_iter_t iter;
struct interface *ifp;
struct zebra_vrf *zvrf_iter;
/* Interface information is needed. */
vrf_bitmap_set (client->ifinfo, zvrf->vrf_id);
for (iter = vrf_first (); iter != VRF_ITER_INVALID; iter = vrf_next (iter))
{
zvrf_iter = vrf_iter2info (iter);
for (ALL_LIST_ELEMENTS (vrf_iflist (zvrf_iter->vrf_id), ifnode, ifnnode, ifp))
{
/* Skip pseudo interface. */
if (! CHECK_FLAG (ifp->status, ZEBRA_INTERFACE_ACTIVE))
continue;
if (zsend_interface_add (client, ifp) < 0)
return -1;
if (zsend_interface_addresses (client, ifp) < 0)
return -1;
}
}
return 0;
}
/* Unregister zebra server interface information. */
static int
zread_interface_delete (struct zserv *client, u_short length, struct zebra_vrf *zvrf)
{
vrf_bitmap_unset (client->ifinfo, zvrf->vrf_id);
return 0;
}
void
zserv_nexthop_num_warn (const char *caller, const struct prefix *p, const unsigned int nexthop_num)
{
if (nexthop_num > MULTIPATH_NUM)
{
char buff[PREFIX2STR_BUFFER];
prefix2str(p, buff, sizeof (buff));
zlog_warn("%s: Prefix %s has %d nexthops, but we can only use the first %d",
caller, buff, nexthop_num, MULTIPATH_NUM);
}
}
/* This function support multiple nexthop. */
/*
* Parse the ZEBRA_IPV4_ROUTE_ADD sent from client. Update rib and
* add kernel route.
*/
static int
zread_ipv4_add (struct zserv *client, u_short length, struct zebra_vrf *zvrf)
{
int i;
struct rib *rib;
struct prefix p;
u_char message;
struct in_addr nexthop;
u_char nexthop_num;
u_char nexthop_type;
struct stream *s;
ifindex_t ifindex;
safi_t safi;
int ret;
/* Get input stream. */
s = client->ibuf;
/* Allocate new rib. */
rib = XCALLOC (MTYPE_RIB, sizeof (struct rib));
/* Type, flags, message. */
rib->type = stream_getc (s);
rib->instance = stream_getw (s);
rib->flags = stream_getc (s);
message = stream_getc (s);
safi = stream_getw (s);
rib->uptime = time (NULL);
/* IPv4 prefix. */
memset (&p, 0, sizeof (struct prefix_ipv4));
p.family = AF_INET;
p.prefixlen = stream_getc (s);
stream_get (&p.u.prefix4, s, PSIZE (p.prefixlen));
/* VRF ID */
rib->vrf_id = zvrf->vrf_id;
/* Nexthop parse. */
if (CHECK_FLAG (message, ZAPI_MESSAGE_NEXTHOP))
{
nexthop_num = stream_getc (s);
zserv_nexthop_num_warn(__func__, (const struct prefix *)&p, nexthop_num);
for (i = 0; i < nexthop_num; i++)
{
nexthop_type = stream_getc (s);
switch (nexthop_type)
{
case NEXTHOP_TYPE_IFINDEX:
ifindex = stream_getl (s);
rib_nexthop_ifindex_add (rib, ifindex);
break;
case NEXTHOP_TYPE_IPV4:
nexthop.s_addr = stream_get_ipv4 (s);
rib_nexthop_ipv4_add (rib, &nexthop, NULL);
break;
case NEXTHOP_TYPE_IPV4_IFINDEX:
nexthop.s_addr = stream_get_ipv4 (s);
ifindex = stream_getl (s);
rib_nexthop_ipv4_ifindex_add (rib, &nexthop, NULL, ifindex);
break;
case NEXTHOP_TYPE_IPV6:
stream_forward_getp (s, IPV6_MAX_BYTELEN);
break;
case NEXTHOP_TYPE_BLACKHOLE:
rib_nexthop_blackhole_add (rib);
break;
}
}
}
/* Distance. */
if (CHECK_FLAG (message, ZAPI_MESSAGE_DISTANCE))
rib->distance = stream_getc (s);
/* Metric. */
if (CHECK_FLAG (message, ZAPI_MESSAGE_METRIC))
rib->metric = stream_getl (s);
/* Tag */
if (CHECK_FLAG (message, ZAPI_MESSAGE_TAG))
rib->tag = stream_getw (s);
else
rib->tag = 0;
if (CHECK_FLAG (message, ZAPI_MESSAGE_MTU))
rib->mtu = stream_getl (s);
else
rib->mtu = 0;
/* Table */
rib->table = zvrf->table_id;
ret = rib_add_multipath (AFI_IP, safi, &p, rib);
/* Stats */
if (ret > 0)
client->v4_route_add_cnt++;
else if (ret < 0)
client->v4_route_upd8_cnt++;
return 0;
}
/* Zebra server IPv4 prefix delete function. */
static int
zread_ipv4_delete (struct zserv *client, u_short length, struct zebra_vrf *zvrf)
{
int i;
struct stream *s;
struct zapi_ipv4 api;
struct in_addr nexthop;
union g_addr *nexthop_p;
unsigned long ifindex;
struct prefix p;
u_char nexthop_num;
u_char nexthop_type;
u_int32_t table_id;
s = client->ibuf;
ifindex = 0;
nexthop.s_addr = 0;
nexthop_p = NULL;
/* Type, flags, message. */
api.type = stream_getc (s);
api.instance = stream_getw (s);
api.flags = stream_getc (s);
api.message = stream_getc (s);
api.safi = stream_getw (s);
/* IPv4 prefix. */
memset (&p, 0, sizeof (struct prefix_ipv4));
p.family = AF_INET;
p.prefixlen = stream_getc (s);
stream_get (&p.u.prefix4, s, PSIZE (p.prefixlen));
/* Nexthop, ifindex, distance, metric. */
if (CHECK_FLAG (api.message, ZAPI_MESSAGE_NEXTHOP))
{
nexthop_num = stream_getc (s);
for (i = 0; i < nexthop_num; i++)
{
nexthop_type = stream_getc (s);
switch (nexthop_type)
{
case NEXTHOP_TYPE_IFINDEX:
ifindex = stream_getl (s);
break;
case NEXTHOP_TYPE_IPV4:
nexthop.s_addr = stream_get_ipv4 (s);
nexthop_p = (union g_addr *)&nexthop;
break;
case NEXTHOP_TYPE_IPV4_IFINDEX:
nexthop.s_addr = stream_get_ipv4 (s);
nexthop_p = (union g_addr *)&nexthop;
ifindex = stream_getl (s);
break;
case NEXTHOP_TYPE_IPV6:
stream_forward_getp (s, IPV6_MAX_BYTELEN);
break;
}
}
}
/* Distance. */
if (CHECK_FLAG (api.message, ZAPI_MESSAGE_DISTANCE))
api.distance = stream_getc (s);
else
api.distance = 0;
/* Metric. */
if (CHECK_FLAG (api.message, ZAPI_MESSAGE_METRIC))
api.metric = stream_getl (s);
else
api.metric = 0;
/* tag */
if (CHECK_FLAG (api.message, ZAPI_MESSAGE_TAG))
api.tag = stream_getw (s);
else
api.tag = 0;
table_id = zvrf->table_id;
rib_delete (AFI_IP, api.safi, zvrf->vrf_id, api.type, api.instance,
api.flags, &p, nexthop_p, ifindex, table_id);
client->v4_route_del_cnt++;
return 0;
}
/* MRIB Nexthop lookup for IPv4. */
static int
zread_ipv4_nexthop_lookup_mrib (struct zserv *client, u_short length, struct zebra_vrf *zvrf)
{
struct in_addr addr;
struct rib *rib;
addr.s_addr = stream_get_ipv4 (client->ibuf);
rib = rib_match_ipv4_multicast (zvrf->vrf_id, addr, NULL);
return zsend_ipv4_nexthop_lookup_mrib (client, addr, rib, zvrf);
}
/* Zebra server IPv6 prefix add function. */
static int
zread_ipv4_route_ipv6_nexthop_add (struct zserv *client, u_short length, struct zebra_vrf *zvrf)
{
int i;
struct stream *s;
struct in6_addr nexthop;
struct rib *rib;
u_char message;
u_char nexthop_num;
u_char nexthop_type;
struct prefix p;
safi_t safi;
static struct in6_addr nexthops[MULTIPATH_NUM];
static unsigned int ifindices[MULTIPATH_NUM];
int ret;
/* Get input stream. */
s = client->ibuf;
memset (&nexthop, 0, sizeof (struct in6_addr));
/* Allocate new rib. */
rib = XCALLOC (MTYPE_RIB, sizeof (struct rib));
/* Type, flags, message. */
rib->type = stream_getc (s);
rib->instance = stream_getw (s);
rib->flags = stream_getc (s);
message = stream_getc (s);
safi = stream_getw (s);
rib->uptime = time (NULL);
/* IPv4 prefix. */
memset (&p, 0, sizeof (struct prefix_ipv4));
p.family = AF_INET;
p.prefixlen = stream_getc (s);
stream_get (&p.u.prefix4, s, PSIZE (p.prefixlen));
/* VRF ID */
rib->vrf_id = zvrf->vrf_id;
/* We need to give nh-addr, nh-ifindex with the same next-hop object
* to the rib to ensure that IPv6 multipathing works; need to coalesce
* these. Clients should send the same number of paired set of
* next-hop-addr/next-hop-ifindices. */
if (CHECK_FLAG (message, ZAPI_MESSAGE_NEXTHOP))
{
int nh_count = 0;
int if_count = 0;
int max_nh_if = 0;
nexthop_num = stream_getc (s);
zserv_nexthop_num_warn(__func__, (const struct prefix *)&p, nexthop_num);
for (i = 0; i < nexthop_num; i++)
{
nexthop_type = stream_getc (s);
switch (nexthop_type)
{
case NEXTHOP_TYPE_IPV6:
stream_get (&nexthop, s, 16);
if (nh_count < MULTIPATH_NUM) {
nexthops[nh_count++] = nexthop;
}
break;
case NEXTHOP_TYPE_IFINDEX:
if (if_count < MULTIPATH_NUM) {
ifindices[if_count++] = stream_getl (s);
}
break;
case NEXTHOP_TYPE_BLACKHOLE:
rib_nexthop_blackhole_add (rib);
break;
}
}
max_nh_if = (nh_count > if_count) ? nh_count : if_count;
for (i = 0; i < max_nh_if; i++)
{
if ((i < nh_count) && !IN6_IS_ADDR_UNSPECIFIED (&nexthops[i])) {
if ((i < if_count) && ifindices[i]) {
rib_nexthop_ipv6_ifindex_add (rib, &nexthops[i], ifindices[i]);
}
else {
rib_nexthop_ipv6_add (rib, &nexthops[i]);
}
}
else {
if ((i < if_count) && ifindices[i]) {
rib_nexthop_ifindex_add (rib, ifindices[i]);
}
}
}
}
/* Distance. */
if (CHECK_FLAG (message, ZAPI_MESSAGE_DISTANCE))
rib->distance = stream_getc (s);
/* Metric. */
if (CHECK_FLAG (message, ZAPI_MESSAGE_METRIC))
rib->metric = stream_getl (s);
/* Tag */
if (CHECK_FLAG (message, ZAPI_MESSAGE_TAG))
rib->tag = stream_getw (s);
else
rib->tag = 0;
if (CHECK_FLAG (message, ZAPI_MESSAGE_MTU))
rib->mtu = stream_getl (s);
else
rib->mtu = 0;
/* Table */
rib->table = zvrf->table_id;
ret = rib_add_multipath (AFI_IP6, safi, &p, rib);
/* Stats */
if (ret > 0)
client->v4_route_add_cnt++;
else if (ret < 0)
client->v4_route_upd8_cnt++;
return 0;
}
static int
zread_ipv6_add (struct zserv *client, u_short length, struct zebra_vrf *zvrf)
{
int i;
struct stream *s;
struct in6_addr nexthop;
struct rib *rib;
u_char message;
u_char nexthop_num;
u_char nexthop_type;
struct prefix p;
safi_t safi;
static struct in6_addr nexthops[MULTIPATH_NUM];
static unsigned int ifindices[MULTIPATH_NUM];
int ret;
/* Get input stream. */
s = client->ibuf;
memset (&nexthop, 0, sizeof (struct in6_addr));
/* Allocate new rib. */
rib = XCALLOC (MTYPE_RIB, sizeof (struct rib));
/* Type, flags, message. */
rib->type = stream_getc (s);
rib->instance = stream_getw (s);
rib->flags = stream_getc (s);
message = stream_getc (s);
safi = stream_getw (s);
rib->uptime = time (NULL);
/* IPv6 prefix. */
memset (&p, 0, sizeof (struct prefix_ipv6));
p.family = AF_INET6;
p.prefixlen = stream_getc (s);
stream_get (&p.u.prefix6, s, PSIZE (p.prefixlen));
/* We need to give nh-addr, nh-ifindex with the same next-hop object
* to the rib to ensure that IPv6 multipathing works; need to coalesce
* these. Clients should send the same number of paired set of
* next-hop-addr/next-hop-ifindices. */
if (CHECK_FLAG (message, ZAPI_MESSAGE_NEXTHOP))
{
int nh_count = 0;
int if_count = 0;
int max_nh_if = 0;
nexthop_num = stream_getc (s);
zserv_nexthop_num_warn(__func__, (const struct prefix *)&p, nexthop_num);
for (i = 0; i < nexthop_num; i++)
{
nexthop_type = stream_getc (s);
switch (nexthop_type)
{
case NEXTHOP_TYPE_IPV6:
stream_get (&nexthop, s, 16);
if (nh_count < MULTIPATH_NUM) {
nexthops[nh_count++] = nexthop;
}
break;
case NEXTHOP_TYPE_IFINDEX:
if (if_count < MULTIPATH_NUM) {
ifindices[if_count++] = stream_getl (s);
}
break;
case NEXTHOP_TYPE_BLACKHOLE:
rib_nexthop_blackhole_add (rib);
break;
}
}
max_nh_if = (nh_count > if_count) ? nh_count : if_count;
for (i = 0; i < max_nh_if; i++)
{
if ((i < nh_count) && !IN6_IS_ADDR_UNSPECIFIED (&nexthops[i])) {
if ((i < if_count) && ifindices[i])
rib_nexthop_ipv6_ifindex_add (rib, &nexthops[i], ifindices[i]);
else
rib_nexthop_ipv6_add (rib, &nexthops[i]);
}
else {
if ((i < if_count) && ifindices[i])
rib_nexthop_ifindex_add (rib, ifindices[i]);
}
}
}
/* Distance. */
if (CHECK_FLAG (message, ZAPI_MESSAGE_DISTANCE))
rib->distance = stream_getc (s);
/* Metric. */
if (CHECK_FLAG (message, ZAPI_MESSAGE_METRIC))
rib->metric = stream_getl (s);
/* Tag */
if (CHECK_FLAG (message, ZAPI_MESSAGE_TAG))
rib->tag = stream_getw (s);
else
rib->tag = 0;
if (CHECK_FLAG (message, ZAPI_MESSAGE_MTU))
rib->mtu = stream_getl (s);
else
rib->mtu = 0;
/* VRF ID */
rib->vrf_id = zvrf->vrf_id;
rib->table = zvrf->table_id;
ret = rib_add_multipath (AFI_IP6, safi, &p, rib);
/* Stats */
if (ret > 0)
client->v6_route_add_cnt++;
else if (ret < 0)
client->v6_route_upd8_cnt++;
return 0;
}
/* Zebra server IPv6 prefix delete function. */
static int
zread_ipv6_delete (struct zserv *client, u_short length, struct zebra_vrf *zvrf)
{
int i;
struct stream *s;
struct zapi_ipv6 api;
struct in6_addr nexthop;
union g_addr *pnexthop;
unsigned long ifindex;
struct prefix p;
s = client->ibuf;
ifindex = 0;
memset (&nexthop, 0, sizeof (struct in6_addr));
/* Type, flags, message. */
api.type = stream_getc (s);
api.instance = stream_getw (s);
api.flags = stream_getc (s);
api.message = stream_getc (s);
api.safi = stream_getw (s);
/* IPv4 prefix. */
memset (&p, 0, sizeof (struct prefix_ipv6));
p.family = AF_INET6;
p.prefixlen = stream_getc (s);
stream_get (&p.u.prefix6, s, PSIZE (p.prefixlen));
/* Nexthop, ifindex, distance, metric. */
if (CHECK_FLAG (api.message, ZAPI_MESSAGE_NEXTHOP))
{
u_char nexthop_type;
api.nexthop_num = stream_getc (s);
for (i = 0; i < api.nexthop_num; i++)
{
nexthop_type = stream_getc (s);
switch (nexthop_type)
{
case NEXTHOP_TYPE_IPV6:
stream_get (&nexthop, s, 16);
pnexthop = (union g_addr *)&nexthop;
break;
case NEXTHOP_TYPE_IFINDEX:
ifindex = stream_getl (s);
break;
}
}
}
/* Distance. */
if (CHECK_FLAG (api.message, ZAPI_MESSAGE_DISTANCE))
api.distance = stream_getc (s);
else
api.distance = 0;
/* Metric. */
if (CHECK_FLAG (api.message, ZAPI_MESSAGE_METRIC))
api.metric = stream_getl (s);
else
api.metric = 0;
/* tag */
if (CHECK_FLAG (api.message, ZAPI_MESSAGE_TAG))
api.tag = stream_getw (s);
else
api.tag = 0;
if (IN6_IS_ADDR_UNSPECIFIED (&nexthop))
rib_delete (AFI_IP6, api.safi, zvrf->vrf_id, api.type, api.instance,
api.flags, &p, NULL, ifindex, client->rtm_table);
else
rib_delete (AFI_IP6, api.safi, zvrf->vrf_id, api.type, api.instance,
api.flags, &p, pnexthop, ifindex, client->rtm_table);
client->v6_route_del_cnt++;
return 0;
}
/* Register zebra server router-id information. Send current router-id */
static int
zread_router_id_add (struct zserv *client, u_short length, struct zebra_vrf *zvrf)
{
struct prefix p;
/* Router-id information is needed. */
vrf_bitmap_set (client->ridinfo, zvrf->vrf_id);
router_id_get (&p, zvrf->vrf_id);
return zsend_router_id_update (client, &p, zvrf->vrf_id);
}
/* Unregister zebra server router-id information. */
static int
zread_router_id_delete (struct zserv *client, u_short length, struct zebra_vrf *zvrf)
{
vrf_bitmap_unset (client->ridinfo, zvrf->vrf_id);
return 0;
}
/* Tie up route-type and client->sock */
static void
zread_hello (struct zserv *client)
{
/* type of protocol (lib/zebra.h) */
u_char proto;
u_short instance;
proto = stream_getc (client->ibuf);
instance = stream_getw (client->ibuf);
/* accept only dynamic routing protocols */
if ((proto < ZEBRA_ROUTE_MAX)
&& (proto > ZEBRA_ROUTE_STATIC))
{
zlog_notice ("client %d says hello and bids fair to announce only %s routes",
client->sock, zebra_route_string(proto));
if (instance)
zlog_notice ("client protocol instance %d", instance);
client->proto = proto;
client->instance = instance;
}
}
/* Unregister all information in a VRF. */
static int
zread_vrf_unregister (struct zserv *client, u_short length, struct zebra_vrf *zvrf)
{
int i;
afi_t afi;
for (afi = AFI_IP; afi < AFI_MAX; afi++)
for (i = 0; i < ZEBRA_ROUTE_MAX; i++)
vrf_bitmap_unset (client->redist[afi][i], zvrf->vrf_id);
vrf_bitmap_unset (client->redist_default, zvrf->vrf_id);
vrf_bitmap_unset (client->ifinfo, zvrf->vrf_id);
vrf_bitmap_unset (client->ridinfo, zvrf->vrf_id);
return 0;
}
/* Cleanup registered nexthops (across VRFs) upon client disconnect. */
static void
zebra_client_close_cleanup_rnh (struct zserv *client)
{
vrf_iter_t iter;
struct zebra_vrf *zvrf;
for (iter = vrf_first (); iter != VRF_ITER_INVALID; iter = vrf_next (iter))
{
if ((zvrf = vrf_iter2info (iter)) != NULL)
{
zebra_cleanup_rnh_client(zvrf->vrf_id, AF_INET, client, RNH_NEXTHOP_TYPE);
zebra_cleanup_rnh_client(zvrf->vrf_id, AF_INET6, client, RNH_NEXTHOP_TYPE);
zebra_cleanup_rnh_client(zvrf->vrf_id, AF_INET, client, RNH_IMPORT_CHECK_TYPE);
zebra_cleanup_rnh_client(zvrf->vrf_id, AF_INET6, client, RNH_IMPORT_CHECK_TYPE);
}
}
}
/* Close zebra client. */
static void
zebra_client_close (struct zserv *client)
{
/* Send client de-registration to BFD */
zebra_ptm_bfd_client_deregister(client->proto);
/* Cleanup any registered nexthops - across all VRFs. */
zebra_client_close_cleanup_rnh (client);
/* Close file descriptor. */
if (client->sock)
{
unsigned long nroutes;
close (client->sock);
nroutes = rib_score_proto (client->proto, client->instance);
zlog_notice ("client %d disconnected. %lu %s routes removed from the rib",
client->sock, nroutes, zebra_route_string (client->proto));
client->sock = -1;
}
/* Free stream buffers. */
if (client->ibuf)
stream_free (client->ibuf);
if (client->obuf)
stream_free (client->obuf);
if (client->wb)
buffer_free(client->wb);
/* Release threads. */
if (client->t_read)
thread_cancel (client->t_read);
if (client->t_write)
thread_cancel (client->t_write);
if (client->t_suicide)
thread_cancel (client->t_suicide);
/* Free client structure. */
listnode_delete (zebrad.client_list, client);
XFREE (MTYPE_TMP, client);
}
/* Make new client. */
static void
zebra_client_create (int sock)
{
struct zserv *client;
int i;
afi_t afi;
client = XCALLOC (MTYPE_TMP, sizeof (struct zserv));
/* Make client input/output buffer. */
client->sock = sock;
client->ibuf = stream_new (ZEBRA_MAX_PACKET_SIZ);
client->obuf = stream_new (ZEBRA_MAX_PACKET_SIZ);
client->wb = buffer_new(0);
/* Set table number. */
client->rtm_table = zebrad.rtm_table_default;
client->connect_time = quagga_monotime();
/* Initialize flags */
for (afi = AFI_IP; afi < AFI_MAX; afi++)
for (i = 0; i < ZEBRA_ROUTE_MAX; i++)
client->redist[afi][i] = vrf_bitmap_init ();
client->redist_default = vrf_bitmap_init ();
client->ifinfo = vrf_bitmap_init ();
client->ridinfo = vrf_bitmap_init ();
/* Add this client to linked list. */
listnode_add (zebrad.client_list, client);
/* Make new read thread. */
zebra_event (ZEBRA_READ, sock, client);
zebra_vrf_update_all (client);
}
/* Handler of zebra service request. */
static int
zebra_client_read (struct thread *thread)
{
int sock;
struct zserv *client;
size_t already;
uint16_t length, command;
uint8_t marker, version;
vrf_id_t vrf_id;
struct zebra_vrf *zvrf;
/* Get thread data. Reset reading thread because I'm running. */
sock = THREAD_FD (thread);
client = THREAD_ARG (thread);
client->t_read = NULL;
if (client->t_suicide)
{
zebra_client_close(client);
return -1;
}
/* Read length and command (if we don't have it already). */
if ((already = stream_get_endp(client->ibuf)) < ZEBRA_HEADER_SIZE)
{
ssize_t nbyte;
if (((nbyte = stream_read_try (client->ibuf, sock,
ZEBRA_HEADER_SIZE-already)) == 0) ||
(nbyte == -1))
{
if (IS_ZEBRA_DEBUG_EVENT)
zlog_debug ("connection closed socket [%d]", sock);
zebra_client_close (client);
return -1;
}
if (nbyte != (ssize_t)(ZEBRA_HEADER_SIZE-already))
{
/* Try again later. */
zebra_event (ZEBRA_READ, sock, client);
return 0;
}
already = ZEBRA_HEADER_SIZE;
}
/* Reset to read from the beginning of the incoming packet. */
stream_set_getp(client->ibuf, 0);
/* Fetch header values */
length = stream_getw (client->ibuf);
marker = stream_getc (client->ibuf);
version = stream_getc (client->ibuf);
vrf_id = stream_getw (client->ibuf);
command = stream_getw (client->ibuf);
if (marker != ZEBRA_HEADER_MARKER || version != ZSERV_VERSION)
{
zlog_err("%s: socket %d version mismatch, marker %d, version %d",
__func__, sock, marker, version);
zebra_client_close (client);
return -1;
}
if (length < ZEBRA_HEADER_SIZE)
{
zlog_warn("%s: socket %d message length %u is less than header size %d",
__func__, sock, length, ZEBRA_HEADER_SIZE);
zebra_client_close (client);
return -1;
}
if (length > STREAM_SIZE(client->ibuf))
{
zlog_warn("%s: socket %d message length %u exceeds buffer size %lu",
__func__, sock, length, (u_long)STREAM_SIZE(client->ibuf));
zebra_client_close (client);
return -1;
}
/* Read rest of data. */
if (already < length)
{
ssize_t nbyte;
if (((nbyte = stream_read_try (client->ibuf, sock,
length-already)) == 0) ||
(nbyte == -1))
{
if (IS_ZEBRA_DEBUG_EVENT)
zlog_debug ("connection closed [%d] when reading zebra data", sock);
zebra_client_close (client);
return -1;
}
if (nbyte != (ssize_t)(length-already))
{
/* Try again later. */
zebra_event (ZEBRA_READ, sock, client);
return 0;
}
}
length -= ZEBRA_HEADER_SIZE;
/* Debug packet information. */
if (IS_ZEBRA_DEBUG_EVENT)
zlog_debug ("zebra message comes from socket [%d]", sock);
if (IS_ZEBRA_DEBUG_PACKET && IS_ZEBRA_DEBUG_RECV)
zlog_debug ("zebra message received [%s] %d in VRF %u",
zserv_command_string (command), length, vrf_id);
client->last_read_time = quagga_monotime();
client->last_read_cmd = command;
zvrf = zebra_vrf_lookup (vrf_id);
if (!zvrf)
{
if (IS_ZEBRA_DEBUG_PACKET && IS_ZEBRA_DEBUG_RECV)
zlog_debug ("zebra received unknown VRF[%u]", vrf_id);
goto zclient_read_out;
}
switch (command)
{
case ZEBRA_ROUTER_ID_ADD:
zread_router_id_add (client, length, zvrf);
break;
case ZEBRA_ROUTER_ID_DELETE:
zread_router_id_delete (client, length, zvrf);
break;
case ZEBRA_INTERFACE_ADD:
zread_interface_add (client, length, zvrf);
break;
case ZEBRA_INTERFACE_DELETE:
zread_interface_delete (client, length, zvrf);
break;
case ZEBRA_IPV4_ROUTE_ADD:
zread_ipv4_add (client, length, zvrf);
break;
case ZEBRA_IPV4_ROUTE_DELETE:
zread_ipv4_delete (client, length, zvrf);
break;
case ZEBRA_IPV4_ROUTE_IPV6_NEXTHOP_ADD:
zread_ipv4_route_ipv6_nexthop_add (client, length, zvrf);
break;
case ZEBRA_IPV6_ROUTE_ADD:
zread_ipv6_add (client, length, zvrf);
break;
case ZEBRA_IPV6_ROUTE_DELETE:
zread_ipv6_delete (client, length, zvrf);
break;
case ZEBRA_REDISTRIBUTE_ADD:
zebra_redistribute_add (command, client, length, zvrf);
break;
case ZEBRA_REDISTRIBUTE_DELETE:
zebra_redistribute_delete (command, client, length, zvrf);
break;
case ZEBRA_REDISTRIBUTE_DEFAULT_ADD:
zebra_redistribute_default_add (command, client, length, zvrf);
break;
case ZEBRA_REDISTRIBUTE_DEFAULT_DELETE:
zebra_redistribute_default_delete (command, client, length, zvrf);
break;
case ZEBRA_IPV4_NEXTHOP_LOOKUP_MRIB:
zread_ipv4_nexthop_lookup_mrib (client, length, zvrf);
break;
case ZEBRA_HELLO:
zread_hello (client);
break;
case ZEBRA_NEXTHOP_REGISTER:
zserv_rnh_register(client, sock, length, RNH_NEXTHOP_TYPE, zvrf);
break;
case ZEBRA_NEXTHOP_UNREGISTER:
zserv_rnh_unregister(client, sock, length, RNH_NEXTHOP_TYPE, zvrf);
break;
case ZEBRA_IMPORT_ROUTE_REGISTER:
zserv_rnh_register(client, sock, length, RNH_IMPORT_CHECK_TYPE, zvrf);
break;
case ZEBRA_IMPORT_ROUTE_UNREGISTER:
zserv_rnh_unregister(client, sock, length, RNH_IMPORT_CHECK_TYPE, zvrf);
break;
case ZEBRA_BFD_DEST_UPDATE:
case ZEBRA_BFD_DEST_REGISTER:
zebra_ptm_bfd_dst_register(client, sock, length, command, zvrf);
break;
case ZEBRA_BFD_DEST_DEREGISTER:
zebra_ptm_bfd_dst_deregister(client, sock, length, zvrf);
break;
case ZEBRA_VRF_UNREGISTER:
zread_vrf_unregister (client, length, zvrf);
break;
case ZEBRA_BFD_CLIENT_REGISTER:
zebra_ptm_bfd_client_register(client, sock, length);
break;
case ZEBRA_INTERFACE_ENABLE_RADV:
zebra_interface_radv_set (client, sock, length, zvrf, 1);
break;
case ZEBRA_INTERFACE_DISABLE_RADV:
zebra_interface_radv_set (client, sock, length, zvrf, 0);
break;
default:
zlog_info ("Zebra received unknown command %d", command);
break;
}
if (client->t_suicide)
{
/* No need to wait for thread callback, just kill immediately. */
zebra_client_close(client);
return -1;
}
zclient_read_out:
stream_reset (client->ibuf);
zebra_event (ZEBRA_READ, sock, client);
return 0;
}
/* Accept code of zebra server socket. */
static int
zebra_accept (struct thread *thread)
{
int accept_sock;
int client_sock;
struct sockaddr_in client;
socklen_t len;
accept_sock = THREAD_FD (thread);
/* Reregister myself. */
zebra_event (ZEBRA_SERV, accept_sock, NULL);
len = sizeof (struct sockaddr_in);
client_sock = accept (accept_sock, (struct sockaddr *) &client, &len);
if (client_sock < 0)
{
zlog_warn ("Can't accept zebra socket: %s", safe_strerror (errno));
return -1;
}
/* Make client socket non-blocking. */
set_nonblocking(client_sock);
/* Create new zebra client. */
zebra_client_create (client_sock);
return 0;
}
#ifdef HAVE_TCP_ZEBRA
/* Make zebra's server socket. */
static void
zebra_serv ()
{
int ret;
int accept_sock;
struct sockaddr_in addr;
accept_sock = socket (AF_INET, SOCK_STREAM, 0);
if (accept_sock < 0)
{
zlog_warn ("Can't create zserv stream socket: %s",
safe_strerror (errno));
zlog_warn ("zebra can't provice full functionality due to above error");
return;
}
memset (&addr, 0, sizeof (struct sockaddr_in));
addr.sin_family = AF_INET;
addr.sin_port = htons (ZEBRA_PORT);
#ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
addr.sin_len = sizeof (struct sockaddr_in);
#endif /* HAVE_STRUCT_SOCKADDR_IN_SIN_LEN */
addr.sin_addr.s_addr = htonl (INADDR_LOOPBACK);
sockopt_reuseaddr (accept_sock);
sockopt_reuseport (accept_sock);
if ( zserv_privs.change(ZPRIVS_RAISE) )
zlog (NULL, LOG_ERR, "Can't raise privileges");
ret = bind (accept_sock, (struct sockaddr *)&addr,
sizeof (struct sockaddr_in));
if (ret < 0)
{
zlog_warn ("Can't bind to stream socket: %s",
safe_strerror (errno));
zlog_warn ("zebra can't provice full functionality due to above error");
close (accept_sock); /* Avoid sd leak. */
return;
}
if ( zserv_privs.change(ZPRIVS_LOWER) )
zlog (NULL, LOG_ERR, "Can't lower privileges");
ret = listen (accept_sock, 1);
if (ret < 0)
{
zlog_warn ("Can't listen to stream socket: %s",
safe_strerror (errno));
zlog_warn ("zebra can't provice full functionality due to above error");
close (accept_sock); /* Avoid sd leak. */
return;
}
zebra_event (ZEBRA_SERV, accept_sock, NULL);
}
#else /* HAVE_TCP_ZEBRA */
/* For sockaddr_un. */
#include <sys/un.h>
/* zebra server UNIX domain socket. */
static void
zebra_serv_un (const char *path)
{
int ret;
int sock, len;
struct sockaddr_un serv;
mode_t old_mask;
/* First of all, unlink existing socket */
unlink (path);
/* Set umask */
old_mask = umask (0077);
/* Make UNIX domain socket. */
sock = socket (AF_UNIX, SOCK_STREAM, 0);
if (sock < 0)
{
zlog_warn ("Can't create zserv unix socket: %s",
safe_strerror (errno));
zlog_warn ("zebra can't provide full functionality due to above error");
return;
}
/* Make server socket. */
memset (&serv, 0, sizeof (struct sockaddr_un));
serv.sun_family = AF_UNIX;
strncpy (serv.sun_path, path, strlen (path));
#ifdef HAVE_STRUCT_SOCKADDR_UN_SUN_LEN
len = serv.sun_len = SUN_LEN(&serv);
#else
len = sizeof (serv.sun_family) + strlen (serv.sun_path);
#endif /* HAVE_STRUCT_SOCKADDR_UN_SUN_LEN */
ret = bind (sock, (struct sockaddr *) &serv, len);
if (ret < 0)
{
zlog_warn ("Can't bind to unix socket %s: %s",
path, safe_strerror (errno));
zlog_warn ("zebra can't provide full functionality due to above error");
close (sock);
return;
}
ret = listen (sock, 5);
if (ret < 0)
{
zlog_warn ("Can't listen to unix socket %s: %s",
path, safe_strerror (errno));
zlog_warn ("zebra can't provide full functionality due to above error");
close (sock);
return;
}
umask (old_mask);
zebra_event (ZEBRA_SERV, sock, NULL);
}
#endif /* HAVE_TCP_ZEBRA */
static void
zebra_event (enum event event, int sock, struct zserv *client)
{
switch (event)
{
case ZEBRA_SERV:
thread_add_read (zebrad.master, zebra_accept, client, sock);
break;
case ZEBRA_READ:
client->t_read =
thread_add_read (zebrad.master, zebra_client_read, client, sock);
break;
case ZEBRA_WRITE:
/**/
break;
}
}
#define ZEBRA_TIME_BUF 32
static char *
zserv_time_buf(time_t *time1, char *buf, int buflen)
{
struct tm *tm;
time_t now;
assert (buf != NULL);
assert (buflen >= ZEBRA_TIME_BUF);
assert (time1 != NULL);
if (!*time1)
{
snprintf(buf, buflen, "never ");
return (buf);
}
now = quagga_monotime();
now -= *time1;
tm = gmtime(&now);
/* Making formatted timer strings. */
#define ONE_DAY_SECOND 60*60*24
#define ONE_WEEK_SECOND 60*60*24*7
if (now < ONE_DAY_SECOND)
snprintf (buf, buflen, "%02d:%02d:%02d",
tm->tm_hour, tm->tm_min, tm->tm_sec);
else if (now < ONE_WEEK_SECOND)
snprintf (buf, buflen, "%dd%02dh%02dm",
tm->tm_yday, tm->tm_hour, tm->tm_min);
else
snprintf (buf, buflen, "%02dw%dd%02dh",
tm->tm_yday/7, tm->tm_yday - ((tm->tm_yday/7) * 7), tm->tm_hour);
return buf;
}
static void
zebra_show_client_detail (struct vty *vty, struct zserv *client)
{
char cbuf[ZEBRA_TIME_BUF], rbuf[ZEBRA_TIME_BUF];
char wbuf[ZEBRA_TIME_BUF], nhbuf[ZEBRA_TIME_BUF], mbuf[ZEBRA_TIME_BUF];
vty_out (vty, "Client: %s", zebra_route_string(client->proto));
if (client->instance)
vty_out (vty, " Instance: %d", client->instance);
vty_out (vty, "%s", VTY_NEWLINE);
vty_out (vty, "------------------------ %s", VTY_NEWLINE);
vty_out (vty, "FD: %d %s", client->sock, VTY_NEWLINE);
vty_out (vty, "Route Table ID: %d %s", client->rtm_table, VTY_NEWLINE);
vty_out (vty, "Connect Time: %s %s",
zserv_time_buf(&client->connect_time, cbuf, ZEBRA_TIME_BUF),
VTY_NEWLINE);
if (client->nh_reg_time)
{
vty_out (vty, "Nexthop Registry Time: %s %s",
zserv_time_buf(&client->nh_reg_time, nhbuf, ZEBRA_TIME_BUF),
VTY_NEWLINE);
if (client->nh_last_upd_time)
vty_out (vty, "Nexthop Last Update Time: %s %s",
zserv_time_buf(&client->nh_last_upd_time, mbuf, ZEBRA_TIME_BUF),
VTY_NEWLINE);
else
vty_out (vty, "No Nexthop Update sent%s", VTY_NEWLINE);
}
else
vty_out (vty, "Not registered for Nexthop Updates%s", VTY_NEWLINE);
vty_out (vty, "Last Msg Rx Time: %s %s",
zserv_time_buf(&client->last_read_time, rbuf, ZEBRA_TIME_BUF),
VTY_NEWLINE);
vty_out (vty, "Last Msg Tx Time: %s %s",
zserv_time_buf(&client->last_write_time, wbuf, ZEBRA_TIME_BUF),
VTY_NEWLINE);
if (client->last_read_time)
vty_out (vty, "Last Rcvd Cmd: %s %s",
zserv_command_string(client->last_read_cmd), VTY_NEWLINE);
if (client->last_write_time)
vty_out (vty, "Last Sent Cmd: %s %s",
zserv_command_string(client->last_write_cmd), VTY_NEWLINE);
vty_out (vty, "%s", VTY_NEWLINE);
vty_out (vty, "Type Add Update Del %s", VTY_NEWLINE);
vty_out (vty, "================================================== %s", VTY_NEWLINE);
vty_out (vty, "IPv4 %-12d%-12d%-12d%s", client->v4_route_add_cnt,
client->v4_route_upd8_cnt, client->v4_route_del_cnt, VTY_NEWLINE);
vty_out (vty, "IPv6 %-12d%-12d%-12d%s", client->v6_route_add_cnt,
client->v6_route_upd8_cnt, client->v6_route_del_cnt, VTY_NEWLINE);
vty_out (vty, "Redist:v4 %-12d%-12d%-12d%s", client->redist_v4_add_cnt, 0,
client->redist_v4_del_cnt, VTY_NEWLINE);
vty_out (vty, "Redist:v6 %-12d%-12d%-12d%s", client->redist_v6_add_cnt, 0,
client->redist_v6_del_cnt, VTY_NEWLINE);
vty_out (vty, "Connected %-12d%-12d%-12d%s", client->ifadd_cnt, 0,
client->ifdel_cnt, VTY_NEWLINE);
vty_out (vty, "BFD peer %-12d%-12d%-12d%s", client->bfd_peer_add_cnt,
client->bfd_peer_upd8_cnt, client->bfd_peer_del_cnt, VTY_NEWLINE);
vty_out (vty, "Interface Up Notifications: %d%s", client->ifup_cnt,
VTY_NEWLINE);
vty_out (vty, "Interface Down Notifications: %d%s", client->ifdown_cnt,
VTY_NEWLINE);
vty_out (vty, "%s", VTY_NEWLINE);
return;
}
static void
zebra_show_client_brief (struct vty *vty, struct zserv *client)
{
char cbuf[ZEBRA_TIME_BUF], rbuf[ZEBRA_TIME_BUF];
char wbuf[ZEBRA_TIME_BUF];
vty_out (vty, "%-8s%12s %12s%12s%8d/%-8d%8d/%-8d%s",
zebra_route_string(client->proto),
zserv_time_buf(&client->connect_time, cbuf, ZEBRA_TIME_BUF),
zserv_time_buf(&client->last_read_time, rbuf, ZEBRA_TIME_BUF),
zserv_time_buf(&client->last_write_time, wbuf, ZEBRA_TIME_BUF),
client->v4_route_add_cnt+client->v4_route_upd8_cnt,
client->v4_route_del_cnt,
client->v6_route_add_cnt+client->v6_route_upd8_cnt,
client->v6_route_del_cnt, VTY_NEWLINE);
}
/* Display default rtm_table for all clients. */
DEFUN (show_table,
show_table_cmd,
"show table",
SHOW_STR
"default routing table to use for all clients\n")
{
vty_out (vty, "table %d%s", zebrad.rtm_table_default,
VTY_NEWLINE);
return CMD_SUCCESS;
}
DEFUN (config_table,
config_table_cmd,
"table TABLENO",
"Configure target kernel routing table\n"
"TABLE integer\n")
{
zebrad.rtm_table_default = strtol (argv[0]->arg, (char**)0, 10);
return CMD_SUCCESS;
}
DEFUN (no_config_table,
no_config_table_cmd,
"no table TABLENO",
NO_STR
"Configure target kernel routing table\n"
"TABLE integer\n")
{
zebrad.rtm_table_default = 0;
return CMD_SUCCESS;
}
DEFUN (ip_forwarding,
ip_forwarding_cmd,
"ip forwarding",
IP_STR
"Turn on IP forwarding")
{
int ret;
ret = ipforward ();
if (ret == 0)
ret = ipforward_on ();
if (ret == 0)
{
vty_out (vty, "Can't turn on IP forwarding%s", VTY_NEWLINE);
return CMD_WARNING;
}
return CMD_SUCCESS;
}
DEFUN (no_ip_forwarding,
no_ip_forwarding_cmd,
"no ip forwarding",
NO_STR
IP_STR
"Turn off IP forwarding")
{
int ret;
ret = ipforward ();
if (ret != 0)
ret = ipforward_off ();
if (ret != 0)
{
vty_out (vty, "Can't turn off IP forwarding%s", VTY_NEWLINE);
return CMD_WARNING;
}
return CMD_SUCCESS;
}
/* This command is for debugging purpose. */
DEFUN (show_zebra_client,
show_zebra_client_cmd,
"show zebra client",
SHOW_STR
"Zebra information"
"Client information")
{
struct listnode *node;
struct zserv *client;
for (ALL_LIST_ELEMENTS_RO (zebrad.client_list, node, client))
zebra_show_client_detail(vty, client);
return CMD_SUCCESS;
}
/* This command is for debugging purpose. */
DEFUN (show_zebra_client_summary,
show_zebra_client_summary_cmd,
"show zebra client summary",
SHOW_STR
"Zebra information brief"
"Client information brief")
{
struct listnode *node;
struct zserv *client;
vty_out (vty, "Name Connect Time Last Read Last Write IPv4 Routes IPv6 Routes %s",
VTY_NEWLINE);
vty_out (vty,"--------------------------------------------------------------------------------%s",
VTY_NEWLINE);
for (ALL_LIST_ELEMENTS_RO (zebrad.client_list, node, client))
zebra_show_client_brief(vty, client);
vty_out (vty, "Routes column shows (added+updated)/deleted%s", VTY_NEWLINE);
return CMD_SUCCESS;
}
/* Table configuration write function. */
static int
config_write_table (struct vty *vty)
{
if (zebrad.rtm_table_default)
vty_out (vty, "table %d%s", zebrad.rtm_table_default,
VTY_NEWLINE);
return 0;
}
/* table node for routing tables. */
static struct cmd_node table_node =
{
TABLE_NODE,
"", /* This node has no interface. */
1
};
/* Only display ip forwarding is enabled or not. */
DEFUN (show_ip_forwarding,
show_ip_forwarding_cmd,
"show ip forwarding",
SHOW_STR
IP_STR
"IP forwarding status\n")
{
int ret;
ret = ipforward ();
if (ret == 0)
vty_out (vty, "IP forwarding is off%s", VTY_NEWLINE);
else
vty_out (vty, "IP forwarding is on%s", VTY_NEWLINE);
return CMD_SUCCESS;
}
#ifdef HAVE_IPV6
/* Only display ipv6 forwarding is enabled or not. */
DEFUN (show_ipv6_forwarding,
show_ipv6_forwarding_cmd,
"show ipv6 forwarding",
SHOW_STR
"IPv6 information\n"
"Forwarding status\n")
{
int ret;
ret = ipforward_ipv6 ();
switch (ret)
{
case -1:
vty_out (vty, "ipv6 forwarding is unknown%s", VTY_NEWLINE);
break;
case 0:
vty_out (vty, "ipv6 forwarding is %s%s", "off", VTY_NEWLINE);
break;
case 1:
vty_out (vty, "ipv6 forwarding is %s%s", "on", VTY_NEWLINE);
break;
default:
vty_out (vty, "ipv6 forwarding is %s%s", "off", VTY_NEWLINE);
break;
}
return CMD_SUCCESS;
}
DEFUN (ipv6_forwarding,
ipv6_forwarding_cmd,
"ipv6 forwarding",
IPV6_STR
"Turn on IPv6 forwarding")
{
int ret;
ret = ipforward_ipv6 ();
if (ret == 0)
ret = ipforward_ipv6_on ();
if (ret == 0)
{
vty_out (vty, "Can't turn on IPv6 forwarding%s", VTY_NEWLINE);
return CMD_WARNING;
}
return CMD_SUCCESS;
}
DEFUN (no_ipv6_forwarding,
no_ipv6_forwarding_cmd,
"no ipv6 forwarding",
NO_STR
IPV6_STR
"Turn off IPv6 forwarding")
{
int ret;
ret = ipforward_ipv6 ();
if (ret != 0)
ret = ipforward_ipv6_off ();
if (ret != 0)
{
vty_out (vty, "Can't turn off IPv6 forwarding%s", VTY_NEWLINE);
return CMD_WARNING;
}
return CMD_SUCCESS;
}
#endif /* HAVE_IPV6 */
/* IPForwarding configuration write function. */
static int
config_write_forwarding (struct vty *vty)
{
/* FIXME: Find better place for that. */
router_id_write (vty);
if (!ipforward ())
vty_out (vty, "no ip forwarding%s", VTY_NEWLINE);
#ifdef HAVE_IPV6
if (!ipforward_ipv6 ())
vty_out (vty, "no ipv6 forwarding%s", VTY_NEWLINE);
#endif /* HAVE_IPV6 */
vty_out (vty, "!%s", VTY_NEWLINE);
return 0;
}
/* table node for routing tables. */
static struct cmd_node forwarding_node =
{
FORWARDING_NODE,
"", /* This node has no interface. */
1
};
/* Initialisation of zebra and installation of commands. */
void
zebra_init (void)
{
/* Client list init. */
zebrad.client_list = list_new ();
/* Install configuration write function. */
install_node (&table_node, config_write_table);
install_node (&forwarding_node, config_write_forwarding);
install_element (VIEW_NODE, &show_ip_forwarding_cmd);
install_element (ENABLE_NODE, &show_ip_forwarding_cmd);
install_element (CONFIG_NODE, &ip_forwarding_cmd);
install_element (CONFIG_NODE, &no_ip_forwarding_cmd);
install_element (ENABLE_NODE, &show_zebra_client_cmd);
install_element (ENABLE_NODE, &show_zebra_client_summary_cmd);
#ifdef HAVE_NETLINK
install_element (VIEW_NODE, &show_table_cmd);
install_element (ENABLE_NODE, &show_table_cmd);
install_element (CONFIG_NODE, &config_table_cmd);
install_element (CONFIG_NODE, &no_config_table_cmd);
#endif /* HAVE_NETLINK */
#ifdef HAVE_IPV6
install_element (VIEW_NODE, &show_ipv6_forwarding_cmd);
install_element (ENABLE_NODE, &show_ipv6_forwarding_cmd);
install_element (CONFIG_NODE, &ipv6_forwarding_cmd);
install_element (CONFIG_NODE, &no_ipv6_forwarding_cmd);
#endif /* HAVE_IPV6 */
/* Route-map */
zebra_route_map_init ();
}
/* Make zebra server socket, wiping any existing one (see bug #403). */
void
zebra_zserv_socket_init (char *path)
{
#ifdef HAVE_TCP_ZEBRA
zebra_serv ();
#else
zebra_serv_un (path ? path : ZEBRA_SERV_PATH);
#endif /* HAVE_TCP_ZEBRA */
}
|