bgp_mp_attr_test.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791
  1. /*
  2. * Copyright (C) 2008 Sun Microsystems, Inc.
  3. *
  4. * This file is part of Quagga.
  5. *
  6. * Quagga is free software; you can redistribute it and/or modify it
  7. * under the terms of the GNU General Public License as published by the
  8. * Free Software Foundation; either version 2, or (at your option) any
  9. * later version.
  10. *
  11. * Quagga is distributed in the hope that it will be useful, but
  12. * WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with Quagga; see the file COPYING. If not, write to the Free
  18. * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
  19. * 02111-1307, USA.
  20. */
  21. #include <zebra.h>
  22. #include "vty.h"
  23. #include "stream.h"
  24. #include "privs.h"
  25. #include "memory.h"
  26. #include "filter.h"
  27. #include "bgpd/bgpd.h"
  28. #include "bgpd/bgp_attr.h"
  29. #include "bgpd/bgp_open.h"
  30. #include "bgpd/bgp_debug.h"
  31. #include "bgpd/bgp_route.h"
  32. #include "bgpd/bgp_packet.h"
  33. #include "bgpd/bgp_mplsvpn.h"
  34. #include "bgpd/bgp_nexthop.h"
  35. #define VT100_RESET "\x1b[0m"
  36. #define VT100_RED "\x1b[31m"
  37. #define VT100_GREEN "\x1b[32m"
  38. #define VT100_YELLOW "\x1b[33m"
  39. #define CAPABILITY 0
  40. #define DYNCAP 1
  41. #define OPT_PARAM 2
  42. /* need these to link in libbgp */
  43. struct zebra_privs_t *bgpd_privs = NULL;
  44. struct thread_master *master = NULL;
  45. static int failed = 0;
  46. static int tty = 0;
  47. /* test segments to parse and validate, and use for other tests */
  48. static struct test_segment {
  49. const char *name;
  50. const char *desc;
  51. const u_char data[1024];
  52. int len;
  53. #define SHOULD_PARSE 0
  54. #define SHOULD_ERR -1
  55. int parses; /* whether it should parse or not */
  56. /* AFI/SAFI validation */
  57. afi_t afi;
  58. safi_t safi;
  59. #define VALID_AFI 1
  60. #define INVALID_AFI 0
  61. int afi_valid;
  62. } mp_reach_segments [] =
  63. {
  64. { "IPv6",
  65. "IPV6 MP Reach, global nexthop, 1 NLRI",
  66. {
  67. /* AFI / SAFI */ 0x0, AFI_IP6, SAFI_UNICAST,
  68. /* nexthop bytes */ 16,
  69. /* Nexthop (global) */ 0xff, 0xfe, 0x1, 0x2,
  70. 0xaa, 0xbb, 0xcc, 0xdd,
  71. 0x3, 0x4, 0x5, 0x6,
  72. 0xa1, 0xa2, 0xa3, 0xa4,
  73. /* SNPA (defunct, MBZ) */ 0x0,
  74. /* NLRI tuples */ 32, 0xff, 0xfe, 0x1, 0x2, /* fffe:102::/32 */
  75. },
  76. (4 + 16 + 1 + 5),
  77. SHOULD_PARSE,
  78. AFI_IP6, SAFI_UNICAST, VALID_AFI,
  79. },
  80. { "IPv6-2",
  81. "IPV6 MP Reach, global nexthop, 2 NLRIs",
  82. {
  83. /* AFI / SAFI */ 0x0, AFI_IP6, SAFI_UNICAST,
  84. /* nexthop bytes */ 16,
  85. /* Nexthop (global) */ 0xff, 0xfe, 0x1, 0x2, /* ffee:102:... */
  86. 0xaa, 0xbb, 0xcc, 0xdd,
  87. 0x3, 0x4, 0x5, 0x6,
  88. 0xa1, 0xa2, 0xa3, 0xa4,
  89. /* SNPA (defunct, MBZ) */ 0x0,
  90. /* NLRI tuples */ 32,
  91. 0xff, 0xfe, 0x1, 0x2, /* fffe:102::/32 */
  92. 64,
  93. 0xff, 0xfe, 0x0, 0x1, /* fffe:1:2:3::/64 */
  94. 0x0, 0x2, 0x0, 0x3,
  95. },
  96. (4 + 16 + 1 + 5 + 9),
  97. SHOULD_PARSE,
  98. AFI_IP6, SAFI_UNICAST, VALID_AFI,
  99. },
  100. { "IPv6-default",
  101. "IPV6 MP Reach, global nexthop, 2 NLRIs + default",
  102. {
  103. /* AFI / SAFI */ 0x0, AFI_IP6, SAFI_UNICAST,
  104. /* nexthop bytes */ 16,
  105. /* Nexthop (global) */ 0xff, 0xfe, 0x1, 0x2,
  106. 0xaa, 0xbb, 0xcc, 0xdd,
  107. 0x3, 0x4, 0x5, 0x6,
  108. 0xa1, 0xa2, 0xa3, 0xa4,
  109. /* SNPA (defunct, MBZ) */ 0x0,
  110. /* NLRI tuples */ 32,
  111. 0xff, 0xfe, 0x1, 0x2, /* fffe:102::/32 */
  112. 64,
  113. 0xff, 0xfe, 0x0, 0x1, /* fffe:1:2:3::/64 */
  114. 0x0, 0x2, 0x0, 0x3,
  115. 0x0, /* ::/0 */
  116. },
  117. (4 + 16 + 1 + 5 + 9 + 1),
  118. SHOULD_PARSE,
  119. AFI_IP6, SAFI_UNICAST, VALID_AFI,
  120. },
  121. { "IPv6-lnh",
  122. "IPV6 MP Reach, global+local nexthops, 2 NLRIs + default",
  123. {
  124. /* AFI / SAFI */ 0x0, AFI_IP6, SAFI_UNICAST,
  125. /* nexthop bytes */ 32,
  126. /* Nexthop (global) */ 0xff, 0xfe, 0x1, 0x2, /* fffe:102:... */
  127. 0xaa, 0xbb, 0xcc, 0xdd,
  128. 0x3, 0x4, 0x5, 0x6,
  129. 0xa1, 0xa2, 0xa3, 0xa4,
  130. /* Nexthop (local) */ 0xfe, 0x80, 0x0, 0x0, /* fe80::210:2ff:.. */
  131. 0x0, 0x0, 0x0, 0x0,
  132. 0x2, 0x10, 0x2, 0xff,
  133. 0x1, 0x2, 0x3, 0x4,
  134. /* SNPA (defunct, MBZ) */ 0x0,
  135. /* NLRI tuples */ 32,
  136. 0xff, 0xfe, 0x1, 0x2, /* fffe:102::/32 */
  137. 64,
  138. 0xff, 0xfe, 0x0, 0x1, /* fffe:1:2:3::/64 */
  139. 0x0, 0x2, 0x0, 0x3,
  140. 0x0, /* ::/0 */
  141. },
  142. (4 + 32 + 1 + 5 + 9 + 1),
  143. SHOULD_PARSE,
  144. AFI_IP6, SAFI_UNICAST, VALID_AFI,
  145. },
  146. { "IPv6-nhlen",
  147. "IPV6 MP Reach, inappropriate nexthop length",
  148. {
  149. /* AFI / SAFI */ 0x0, AFI_IP6, SAFI_UNICAST,
  150. /* nexthop bytes */ 4,
  151. /* Nexthop (global) */ 0xff, 0xfe, 0x1, 0x2, /* fffe:102:... */
  152. 0xaa, 0xbb, 0xcc, 0xdd,
  153. 0x3, 0x4, 0x5, 0x6,
  154. 0xa1, 0xa2, 0xa3, 0xa4,
  155. /* Nexthop (local) */ 0xfe, 0x80, 0x0, 0x0, /* fe80::210:2ff:.. */
  156. 0x0, 0x0, 0x0, 0x0,
  157. 0x2, 0x10, 0x2, 0xff,
  158. 0x1, 0x2, 0x3, 0x4,
  159. /* SNPA (defunct, MBZ) */ 0x0,
  160. /* NLRI tuples */ 32,
  161. 0xff, 0xfe, 0x1, 0x2, /* fffe:102::/32 */
  162. 64,
  163. 0xff, 0xfe, 0x0, 0x1, /* fffe:1:2:3::/64 */
  164. 0x0, 0x2, 0x0, 0x3,
  165. 0x0, /* ::/0 */
  166. },
  167. (4 + 32 + 1 + 5 + 9 + 1),
  168. SHOULD_ERR,
  169. AFI_IP6, SAFI_UNICAST, VALID_AFI,
  170. },
  171. { "IPv6-nhlen2",
  172. "IPV6 MP Reach, invalid nexthop length",
  173. {
  174. /* AFI / SAFI */ 0x0, AFI_IP6, SAFI_UNICAST,
  175. /* nexthop bytes */ 5,
  176. /* Nexthop (global) */ 0xff, 0xfe, 0x1, 0x2, /* fffe:102:... */
  177. 0xaa, 0xbb, 0xcc, 0xdd,
  178. 0x3, 0x4, 0x5, 0x6,
  179. 0xa1, 0xa2, 0xa3, 0xa4,
  180. /* Nexthop (local) */ 0xfe, 0x80, 0x0, 0x0, /* fe80::210:2ff:.. */
  181. 0x0, 0x0, 0x0, 0x0,
  182. 0x2, 0x10, 0x2, 0xff,
  183. 0x1, 0x2, 0x3, 0x4,
  184. /* SNPA (defunct, MBZ) */ 0x0,
  185. /* NLRI tuples */ 32,
  186. 0xff, 0xfe, 0x1, 0x2, /* fffe:102::/32 */
  187. 64,
  188. 0xff, 0xfe, 0x0, 0x1, /* fffe:1:2:3::/64 */
  189. 0x0, 0x2, 0x0, 0x3,
  190. 0x0, /* ::/0 */
  191. },
  192. (4 + 32 + 1 + 5 + 9 + 1),
  193. SHOULD_ERR,
  194. AFI_IP6, SAFI_UNICAST, VALID_AFI,
  195. },
  196. { "IPv6-nhlen3",
  197. "IPV6 MP Reach, nexthop length overflow",
  198. {
  199. /* AFI / SAFI */ 0x0, AFI_IP6, SAFI_UNICAST,
  200. /* nexthop bytes */ 32,
  201. /* Nexthop (global) */ 0xff, 0xfe, 0x1, 0x2, /* fffe:102:... */
  202. 0xaa, 0xbb, 0xcc, 0xdd,
  203. 0x3, 0x4, 0x5, 0x6,
  204. 0xa1, 0xa2, 0xa3, 0xa4,
  205. },
  206. (4 + 16),
  207. SHOULD_ERR,
  208. AFI_IP6, SAFI_UNICAST, VALID_AFI,
  209. },
  210. { "IPv6-nhlen4",
  211. "IPV6 MP Reach, nexthop length short",
  212. {
  213. /* AFI / SAFI */ 0x0, AFI_IP6, SAFI_UNICAST,
  214. /* nexthop bytes */ 16,
  215. /* Nexthop (global) */ 0xff, 0xfe, 0x1, 0x2, /* fffe:102:... */
  216. 0xaa, 0xbb, 0xcc, 0xdd,
  217. 0x3, 0x4, 0x5, 0x6,
  218. 0xa1, 0xa2, 0xa3, 0xa4,
  219. /* Nexthop (local) */ 0xfe, 0x80, 0x0, 0x0, /* fe80::210:2ff:.. */
  220. 0x0, 0x0, 0x0, 0x0,
  221. 0x2, 0x10, 0x2, 0xff,
  222. 0x1, 0x2, 0x3, 0x4,
  223. /* SNPA (defunct, MBZ) */ 0x0,
  224. /* NLRI tuples */ 32,
  225. 0xff, 0xfe, 0x1, 0x2, /* fffe:102::/32 */
  226. 64,
  227. 0xff, 0xfe, 0x0, 0x1, /* fffe:1:2:3::/64 */
  228. 0x0, 0x2, 0x0, 0x3,
  229. 0x0, /* ::/0 */
  230. },
  231. (4 + 32 + 1 + 5 + 9 + 1),
  232. SHOULD_ERR,
  233. AFI_IP6, SAFI_UNICAST, VALID_AFI,
  234. },
  235. { "IPv6-nlri",
  236. "IPV6 MP Reach, NLRI bitlen overflow",
  237. {
  238. /* AFI / SAFI */ 0x0, AFI_IP6, SAFI_UNICAST,
  239. /* nexthop bytes */ 32,
  240. /* Nexthop (global) */ 0xff, 0xfe, 0x1, 0x2, /* fffe:102:... */
  241. 0xaa, 0xbb, 0xcc, 0xdd,
  242. 0x3, 0x4, 0x5, 0x6,
  243. 0xa1, 0xa2, 0xa3, 0xa4,
  244. /* Nexthop (local) */ 0xfe, 0x80, 0x0, 0x0, /* fe80::210:2ff:.. */
  245. 0x0, 0x0, 0x0, 0x0,
  246. 0x2, 0x10, 0x2, 0xff,
  247. 0x1, 0x2, 0x3, 0x4,
  248. /* SNPA (defunct, MBZ) */ 0x0,
  249. /* NLRI tuples */ 120,
  250. 0xff, 0xfe, 0x1, 0x2, /* fffe:102::/32 */
  251. 64,
  252. 0xff, 0xfe, 0x0, 0x1, /* fffe:1:2:3::/64 */
  253. 0x0, 0x2, 0x0, 0x3,
  254. 0, /* ::/0 */
  255. },
  256. (4 + 32 + 1 + 5 + 9 + 1),
  257. SHOULD_ERR,
  258. AFI_IP6, SAFI_UNICAST, VALID_AFI,
  259. },
  260. { "IPv4",
  261. "IPv4 MP Reach, 2 NLRIs + default",
  262. {
  263. /* AFI / SAFI */ 0x0, AFI_IP, SAFI_UNICAST,
  264. /* nexthop bytes */ 4,
  265. /* Nexthop */ 192, 168, 0, 1,
  266. /* SNPA (defunct, MBZ) */ 0x0,
  267. /* NLRI tuples */ 16, 10, 1, /* 10.1/16 */
  268. 17, 10, 2, 3, /* 10.2.3/17 */
  269. 0, /* 0/0 */
  270. },
  271. (4 + 4 + 1 + 3 + 4 + 1),
  272. SHOULD_PARSE,
  273. AFI_IP, SAFI_UNICAST, VALID_AFI,
  274. },
  275. { "IPv4-nhlen",
  276. "IPv4 MP Reach, nexthop lenth overflow",
  277. {
  278. /* AFI / SAFI */ 0x0, AFI_IP, SAFI_UNICAST,
  279. /* nexthop bytes */ 32,
  280. /* Nexthop */ 192, 168, 0, 1,
  281. /* SNPA (defunct, MBZ) */ 0x0,
  282. /* NLRI tuples */ 16, 10, 1, /* 10.1/16 */
  283. 17, 10, 2, 3, /* 10.2.3/17 */
  284. 0, /* 0/0 */
  285. },
  286. (4 + 4 + 1 + 3 + 4 + 1),
  287. SHOULD_ERR,
  288. AFI_IP, SAFI_UNICAST, VALID_AFI,
  289. },
  290. { "IPv4-nlrilen",
  291. "IPv4 MP Reach, nlri lenth overflow",
  292. {
  293. /* AFI / SAFI */ 0x0, AFI_IP, SAFI_UNICAST,
  294. /* nexthop bytes */ 4,
  295. /* Nexthop */ 192, 168, 0, 1,
  296. /* SNPA (defunct, MBZ) */ 0x0,
  297. /* NLRI tuples */ 16, 10, 1, /* 10.1/16 */
  298. 30, 10,
  299. 0, /* 0/0 */
  300. },
  301. (4 + 4 + 1 + 3 + 2 + 1),
  302. SHOULD_ERR,
  303. AFI_IP, SAFI_UNICAST, VALID_AFI,
  304. },
  305. { "IPv4-VPNv4",
  306. "IPv4/VPNv4 MP Reach, RD, Nexthop, 2 NLRIs",
  307. {
  308. /* AFI / SAFI */ 0x0, AFI_IP, SAFI_MPLS_LABELED_VPN,
  309. /* nexthop bytes */ 12,
  310. /* RD */ 0, 0, 0, 0, /* RD defined to be 0 */
  311. 0, 0, 0, 0,
  312. /* Nexthop */ 192, 168, 0, 1,
  313. /* SNPA (defunct, MBZ) */ 0x0,
  314. /* NLRI tuples */ 88 + 16,
  315. 0, 1, 2, /* tag */
  316. /* rd, 8 octets */
  317. 0, 0, /* RD_TYPE_AS */
  318. 0, 2, 0, 0xff, 3, 4, /* AS(2):val(4) */
  319. 10, 1, /* 10.1/16 */
  320. 88 + 17,
  321. 0xff, 0, 0, /* tag */
  322. /* rd, 8 octets */
  323. 0, 0, /* RD_TYPE_IP */
  324. 192, 168, 0, 1, /* IPv4 */
  325. 10, 2, 3, /* 10.2.3/17 */
  326. },
  327. (4 + 12 + 1 + (1+3+8+2) + (1+3+8+3)),
  328. SHOULD_PARSE,
  329. AFI_IP, SAFI_MPLS_LABELED_VPN, VALID_AFI,
  330. },
  331. { "IPv4-VPNv4-bogus-plen",
  332. "IPv4/MPLS-labeled VPN MP Reach, RD, Nexthop, NLRI / bogus p'len",
  333. {
  334. /* AFI / SAFI */ 0x0, AFI_IP, SAFI_MPLS_LABELED_VPN,
  335. /* nexthop bytes */ 12,
  336. /* RD */ 0, 0, 1, 2,
  337. 0, 0xff, 3, 4,
  338. /* Nexthop */ 192, 168, 0, 1,
  339. /* SNPA (defunct, MBZ) */ 0x0,
  340. /* NLRI tuples */ 16, 10, 1, /* 10.1/16 */
  341. 17, 10, 2, 3, /* 10.2.3/17 */
  342. 0, /* 0/0 */
  343. },
  344. (3 + 1 + 3*4 + 1 + 3 + 4 + 1),
  345. SHOULD_ERR,
  346. AFI_IP, SAFI_MPLS_LABELED_VPN, VALID_AFI,
  347. },
  348. { "IPv4-VPNv4-plen1-short",
  349. "IPv4/VPNv4 MP Reach, RD, Nexthop, 2 NLRIs, 1st plen short",
  350. {
  351. /* AFI / SAFI */ 0x0, AFI_IP, SAFI_MPLS_LABELED_VPN,
  352. /* nexthop bytes */ 12,
  353. /* RD */ 0, 0, 0, 0, /* RD defined to be 0 */
  354. 0, 0, 0, 0,
  355. /* Nexthop */ 192, 168, 0, 1,
  356. /* SNPA (defunct, MBZ) */ 0x0,
  357. /* NLRI tuples */ 88 + 1,
  358. 0, 1, 2, /* tag */
  359. /* rd, 8 octets */
  360. 0, 0, /* RD_TYPE_AS */
  361. 0, 2, 0, 0xff, 3, 4, /* AS(2):val(4) */
  362. 10, 1, /* 10.1/16 */
  363. 88 + 17,
  364. 0xff, 0, 0, /* tag */
  365. /* rd, 8 octets */
  366. 0, 0, /* RD_TYPE_IP */
  367. 192, 168, 0, 1, /* IPv4 */
  368. 10, 2, 3, /* 10.2.3/17 */
  369. },
  370. (4 + 12 + 1 + (1+3+8+2) + (1+3+8+3)),
  371. SHOULD_ERR,
  372. AFI_IP, SAFI_MPLS_LABELED_VPN, VALID_AFI,
  373. },
  374. { "IPv4-VPNv4-plen1-long",
  375. "IPv4/VPNv4 MP Reach, RD, Nexthop, 2 NLRIs, 1st plen long",
  376. {
  377. /* AFI / SAFI */ 0x0, AFI_IP, SAFI_MPLS_LABELED_VPN,
  378. /* nexthop bytes */ 12,
  379. /* RD */ 0, 0, 0, 0, /* RD defined to be 0 */
  380. 0, 0, 0, 0,
  381. /* Nexthop */ 192, 168, 0, 1,
  382. /* SNPA (defunct, MBZ) */ 0x0,
  383. /* NLRI tuples */ 88 + 32,
  384. 0, 1, 2, /* tag */
  385. /* rd, 8 octets */
  386. 0, 0, /* RD_TYPE_AS */
  387. 0, 2, 0, 0xff, 3, 4, /* AS(2):val(4) */
  388. 10, 1, /* 10.1/16 */
  389. 88 + 17,
  390. 0xff, 0, 0, /* tag */
  391. /* rd, 8 octets */
  392. 0, 0, /* RD_TYPE_IP */
  393. 192, 168, 0, 1, /* IPv4 */
  394. 10, 2, 3, /* 10.2.3/17 */
  395. },
  396. (4 + 12 + 1 + (1+3+8+2) + (1+3+8+3)),
  397. SHOULD_ERR,
  398. AFI_IP, SAFI_MPLS_LABELED_VPN, VALID_AFI,
  399. },
  400. { "IPv4-VPNv4-plenn-long",
  401. "IPv4/VPNv4 MP Reach, RD, Nexthop, 3 NLRIs, last plen long",
  402. {
  403. /* AFI / SAFI */ 0x0, AFI_IP, SAFI_MPLS_LABELED_VPN,
  404. /* nexthop bytes */ 12,
  405. /* RD */ 0, 0, 0, 0, /* RD defined to be 0 */
  406. 0, 0, 0, 0,
  407. /* Nexthop */ 192, 168, 0, 1,
  408. /* SNPA (defunct, MBZ) */ 0x0,
  409. /* NLRI tuples */ 88 + 16,
  410. 0, 1, 2, /* tag */
  411. /* rd, 8 octets */
  412. 0, 0, /* RD_TYPE_AS */
  413. 0, 2, 0, 0xff, 3, 4, /* AS(2):val(4) */
  414. 10, 1, /* 10.1/16 */
  415. 88 + 17,
  416. 0xff, 0, 0, /* tag */
  417. /* rd, 8 octets */
  418. 0, 0, /* RD_TYPE_IP */
  419. 192, 168, 0, 1, /* IPv4 */
  420. 10, 2, 3, /* 10.2.3/17 */
  421. 88 + 1, /* bogus */
  422. },
  423. (4 + 12 + 1 + (1+3+8+2) + (1+3+8+3) + 1),
  424. SHOULD_ERR,
  425. AFI_IP, SAFI_MPLS_LABELED_VPN, VALID_AFI,
  426. },
  427. { "IPv4-VPNv4-plenn-short",
  428. "IPv4/VPNv4 MP Reach, RD, Nexthop, 2 NLRIs, last plen short",
  429. {
  430. /* AFI / SAFI */ 0x0, AFI_IP, SAFI_MPLS_LABELED_VPN,
  431. /* nexthop bytes */ 12,
  432. /* RD */ 0, 0, 0, 0, /* RD defined to be 0 */
  433. 0, 0, 0, 0,
  434. /* Nexthop */ 192, 168, 0, 1,
  435. /* SNPA (defunct, MBZ) */ 0x0,
  436. /* NLRI tuples */ 88 + 16,
  437. 0, 1, 2, /* tag */
  438. /* rd, 8 octets */
  439. 0, 0, /* RD_TYPE_AS */
  440. 0, 2, 0, 0xff, 3, 4, /* AS(2):val(4) */
  441. 10, 1, /* 10.1/16 */
  442. 88 + 2,
  443. 0xff, 0, 0, /* tag */
  444. /* rd, 8 octets */
  445. 0, 0, /* RD_TYPE_IP */
  446. 192, 168, 0, 1, /* IPv4 */
  447. 10, 2, 3, /* 10.2.3/17 */
  448. },
  449. (4 + 12 + 1 + (1+3+8+2) + (1+3+8+3)),
  450. SHOULD_ERR,
  451. AFI_IP, SAFI_MPLS_LABELED_VPN, VALID_AFI,
  452. },
  453. { "IPv4-VPNv4-bogus-rd-type",
  454. "IPv4/VPNv4 MP Reach, RD, NH, 2 NLRI, unknown RD in 1st (log, but parse)",
  455. {
  456. /* AFI / SAFI */ 0x0, AFI_IP, SAFI_MPLS_LABELED_VPN,
  457. /* nexthop bytes */ 12,
  458. /* RD */ 0, 0, 0, 0, /* RD defined to be 0 */
  459. 0, 0, 0, 0,
  460. /* Nexthop */ 192, 168, 0, 1,
  461. /* SNPA (defunct, MBZ) */ 0x0,
  462. /* NLRI tuples */ 88 + 16,
  463. 0, 1, 2, /* tag */
  464. /* rd, 8 octets */
  465. 0xff, 0, /* Bogus RD */
  466. 0, 2, 0, 0xff, 3, 4, /* AS(2):val(4) */
  467. 10, 1, /* 10.1/16 */
  468. 88 + 17,
  469. 0xff, 0, 0, /* tag */
  470. /* rd, 8 octets */
  471. 0, 0, /* RD_TYPE_IP */
  472. 192, 168, 0, 1, /* IPv4 */
  473. 10, 2, 3, /* 10.2.3/17 */
  474. },
  475. (4 + 12 + 1 + (1+3+8+2) + (1+3+8+3)),
  476. SHOULD_PARSE,
  477. AFI_IP, SAFI_MPLS_LABELED_VPN, VALID_AFI,
  478. },
  479. { "IPv4-VPNv4-0-nlri",
  480. "IPv4/VPNv4 MP Reach, RD, Nexthop, 3 NLRI, 3rd 0 bogus",
  481. {
  482. /* AFI / SAFI */ 0x0, AFI_IP, SAFI_MPLS_LABELED_VPN,
  483. /* nexthop bytes */ 12,
  484. /* RD */ 0, 0, 0, 0, /* RD defined to be 0 */
  485. 0, 0, 0, 0,
  486. /* Nexthop */ 192, 168, 0, 1,
  487. /* SNPA (defunct, MBZ) */ 0x0,
  488. /* NLRI tuples */ 88 + 16,
  489. 0, 1, 2, /* tag */
  490. /* rd, 8 octets */
  491. 0, 0, /* RD_TYPE_AS */
  492. 0, 2, 0, 0xff, 3, 4, /* AS(2):val(4) */
  493. 10, 1, /* 10.1/16 */
  494. 88 + 17,
  495. 0xff, 0, 0, /* tag */
  496. /* rd, 8 octets */
  497. 0, 0, /* RD_TYPE_IP */
  498. 192, 168, 0, 1, /* IPv4 */
  499. 10, 2, 3, /* 10.2.3/17 */
  500. 0 /* 0/0, bogus for vpnv4 ?? */
  501. },
  502. (4 + 12 + 1 + (1+3+8+2) + (1+3+8+3) + 1),
  503. SHOULD_ERR,
  504. AFI_IP, SAFI_MPLS_LABELED_VPN, VALID_AFI,
  505. },
  506. /* From bug #385 */
  507. { "IPv6-bug",
  508. "IPv6, global nexthop, 1 default NLRI",
  509. {
  510. /* AFI / SAFI */ 0x0, 0x2, 0x1,
  511. /* nexthop bytes */ 0x20,
  512. /* Nexthop (global) */ 0x20, 0x01, 0x04, 0x70,
  513. 0x00, 0x01, 0x00, 0x06,
  514. 0x00, 0x00, 0x00, 0x00,
  515. 0x00, 0x00, 0x00, 0x01,
  516. /* Nexthop (local) */ 0xfe, 0x80, 0x00, 0x00,
  517. 0x00, 0x00, 0x00, 0x00,
  518. 0x02, 0x0c, 0xdb, 0xff,
  519. 0xfe, 0xfe, 0xeb, 0x00,
  520. /* SNPA (defunct, MBZ) */ 0,
  521. /* NLRI tuples */ /* Should have 0 here for ::/0, but dont */
  522. },
  523. 37,
  524. SHOULD_ERR,
  525. AFI_IP6, SAFI_UNICAST, VALID_AFI,
  526. },
  527. { NULL, NULL, {0}, 0, 0}
  528. };
  529. /* MP_UNREACH_NLRI tests */
  530. static struct test_segment mp_unreach_segments [] =
  531. {
  532. { "IPv6-unreach",
  533. "IPV6 MP Unreach, 1 NLRI",
  534. {
  535. /* AFI / SAFI */ 0x0, AFI_IP6, SAFI_UNICAST,
  536. /* NLRI tuples */ 32, 0xff, 0xfe, 0x1, 0x2, /* fffe:102::/32 */
  537. },
  538. (3 + 5),
  539. SHOULD_PARSE,
  540. AFI_IP6, SAFI_UNICAST, VALID_AFI,
  541. },
  542. { "IPv6-unreach2",
  543. "IPV6 MP Unreach, 2 NLRIs",
  544. {
  545. /* AFI / SAFI */ 0x0, AFI_IP6, SAFI_UNICAST,
  546. /* NLRI tuples */ 32,
  547. 0xff, 0xfe, 0x1, 0x2, /* fffe:102::/32 */
  548. 64,
  549. 0xff, 0xfe, 0x0, 0x1, /* fffe:1:2:3::/64 */
  550. 0x0, 0x2, 0x0, 0x3,
  551. },
  552. (3 + 5 + 9),
  553. SHOULD_PARSE,
  554. AFI_IP6, SAFI_UNICAST, VALID_AFI,
  555. },
  556. { "IPv6-unreach-default",
  557. "IPV6 MP Unreach, 2 NLRIs + default",
  558. {
  559. /* AFI / SAFI */ 0x0, AFI_IP6, SAFI_UNICAST,
  560. /* NLRI tuples */ 32,
  561. 0xff, 0xfe, 0x1, 0x2, /* fffe:102::/32 */
  562. 64,
  563. 0xff, 0xfe, 0x0, 0x1, /* fffe:1:2:3::/64 */
  564. 0x0, 0x2, 0x0, 0x3,
  565. 0x0, /* ::/0 */
  566. },
  567. (3 + 5 + 9 + 1),
  568. SHOULD_PARSE,
  569. AFI_IP6, SAFI_UNICAST, VALID_AFI,
  570. },
  571. { "IPv6-unreach-nlri",
  572. "IPV6 MP Unreach, NLRI bitlen overflow",
  573. {
  574. /* AFI / SAFI */ 0x0, AFI_IP6, SAFI_UNICAST,
  575. /* NLRI tuples */ 120,
  576. 0xff, 0xfe, 0x1, 0x2, /* fffe:102::/32 */
  577. 64,
  578. 0xff, 0xfe, 0x0, 0x1, /* fffe:1:2:3::/64 */
  579. 0x0, 0x2, 0x0, 0x3,
  580. 0, /* ::/0 */
  581. },
  582. (3 + 5 + 9 + 1),
  583. SHOULD_ERR,
  584. AFI_IP6, SAFI_UNICAST, VALID_AFI,
  585. },
  586. { "IPv4-unreach",
  587. "IPv4 MP Unreach, 2 NLRIs + default",
  588. {
  589. /* AFI / SAFI */ 0x0, AFI_IP, SAFI_UNICAST,
  590. /* NLRI tuples */ 16, 10, 1, /* 10.1/16 */
  591. 17, 10, 2, 3, /* 10.2.3/17 */
  592. 0, /* 0/0 */
  593. },
  594. (3 + 3 + 4 + 1),
  595. SHOULD_PARSE,
  596. AFI_IP, SAFI_UNICAST, VALID_AFI,
  597. },
  598. { "IPv4-unreach-nlrilen",
  599. "IPv4 MP Unreach, nlri length overflow",
  600. {
  601. /* AFI / SAFI */ 0x0, AFI_IP, SAFI_UNICAST,
  602. /* NLRI tuples */ 16, 10, 1, /* 10.1/16 */
  603. 30, 10,
  604. 0, /* 0/0 */
  605. },
  606. (3 + 3 + 2 + 1),
  607. SHOULD_ERR,
  608. AFI_IP, SAFI_UNICAST, VALID_AFI,
  609. },
  610. { "IPv4-unreach-VPNv4",
  611. "IPv4/MPLS-labeled VPN MP Unreach, RD, 3 NLRIs",
  612. {
  613. /* AFI / SAFI */ 0x0, AFI_IP, SAFI_MPLS_LABELED_VPN,
  614. /* NLRI tuples */ 88 + 16,
  615. 0, 1, 2, /* tag */
  616. /* rd, 8 octets */
  617. 0, 0, /* RD_TYPE_AS */
  618. 0, 2, 0, 0xff, 3, 4, /* AS(2):val(4) */
  619. 10, 1, /* 10.1/16 */
  620. 88 + 17,
  621. 0xff, 0, 0, /* tag */
  622. /* rd, 8 octets */
  623. 0, 0, /* RD_TYPE_IP */
  624. 192, 168, 0, 1, /* IPv4 */
  625. 10, 2, 3, /* 10.2.3/17 */
  626. },
  627. (3 + (1+3+8+2) + (1+3+8+3)),
  628. SHOULD_PARSE,
  629. AFI_IP, SAFI_MPLS_LABELED_VPN, VALID_AFI,
  630. },
  631. { NULL, NULL, {0}, 0, 0}
  632. };
  633. /* nlri_parse indicates 0 on successful parse, and -1 otherwise.
  634. * attr_parse indicates BGP_ATTR_PARSE_PROCEED/0 on success,
  635. * and BGP_ATTR_PARSE_ERROR/-1 or lower negative ret on err.
  636. */
  637. static void
  638. handle_result (struct peer *peer, struct test_segment *t,
  639. int parse_ret, int nlri_ret)
  640. {
  641. int oldfailed = failed;
  642. if (!parse_ret)
  643. {
  644. safi_t safi = t->safi;
  645. if (bgp_afi_safi_valid_indices (t->afi, &safi) != t->afi_valid)
  646. failed++;
  647. printf ("MP: %u/%u (%u): recv %u, nego %u\n",
  648. t->afi, t->safi, safi,
  649. peer->afc_recv[t->afi][safi],
  650. peer->afc_nego[t->afi][safi]);
  651. }
  652. printf ("mp attr parsed?: %s\n", parse_ret ? "no" : "yes");
  653. if (!parse_ret)
  654. printf ("nrli parsed?: %s\n", nlri_ret ? "no" : "yes");
  655. printf ("should parse?: %s\n", t->parses ? "no" : "yes");
  656. if ((parse_ret != 0 || nlri_ret != 0) != (t->parses != 0))
  657. failed++;
  658. if (tty)
  659. printf ("%s", (failed > oldfailed) ? VT100_RED "failed!" VT100_RESET
  660. : VT100_GREEN "OK" VT100_RESET);
  661. else
  662. printf ("%s", (failed > oldfailed) ? "failed!" : "OK" );
  663. if (failed)
  664. printf (" (%u)", failed);
  665. printf ("\n\n");
  666. }
  667. /* basic parsing test */
  668. static void
  669. parse_test (struct peer *peer, struct test_segment *t, int type)
  670. {
  671. int parse_ret = 0, nlri_ret = 0;
  672. struct attr attr = { };
  673. struct bgp_nlri nlri = { };
  674. struct bgp_attr_parser_args attr_args = {
  675. .peer = peer,
  676. .length = t->len,
  677. .total = 1,
  678. .attr = &attr,
  679. .type = type,
  680. .flags = BGP_ATTR_FLAG_OPTIONAL,
  681. .startp = BGP_INPUT_PNT (peer),
  682. };
  683. #define RANDOM_FUZZ 35
  684. stream_reset (peer->ibuf);
  685. stream_put (peer->ibuf, NULL, RANDOM_FUZZ);
  686. stream_set_getp (peer->ibuf, RANDOM_FUZZ);
  687. stream_write (peer->ibuf, t->data, t->len);
  688. printf ("%s: %s\n", t->name, t->desc);
  689. if (type == BGP_ATTR_MP_REACH_NLRI)
  690. parse_ret = bgp_mp_reach_parse (&attr_args, &nlri);
  691. else
  692. parse_ret = bgp_mp_unreach_parse (&attr_args, &nlri);
  693. if (parse_ret == 0 && t->afi_valid == VALID_AFI)
  694. assert (nlri.afi == t->afi && nlri.safi == t->safi);
  695. if (!parse_ret)
  696. {
  697. if (type == BGP_ATTR_MP_REACH_NLRI)
  698. nlri_ret = bgp_nlri_parse (peer, &attr, &nlri);
  699. else
  700. nlri_ret = bgp_nlri_parse (peer, NULL, &nlri);
  701. }
  702. handle_result (peer, t, parse_ret, nlri_ret);
  703. }
  704. static struct bgp *bgp;
  705. static as_t asn = 100;
  706. int
  707. main (void)
  708. {
  709. struct peer *peer;
  710. int i, j;
  711. conf_bgp_debug_fsm = -1UL;
  712. conf_bgp_debug_events = -1UL;
  713. conf_bgp_debug_packet = -1UL;
  714. conf_bgp_debug_normal = -1UL;
  715. conf_bgp_debug_as4 = -1UL;
  716. term_bgp_debug_fsm = -1UL;
  717. term_bgp_debug_events = -1UL;
  718. term_bgp_debug_packet = -1UL;
  719. term_bgp_debug_normal = -1UL;
  720. term_bgp_debug_as4 = -1UL;
  721. master = thread_master_create ();
  722. bgp_master_init ();
  723. bgp_option_set (BGP_OPT_NO_LISTEN);
  724. bgp_attr_init ();
  725. bgp_address_init ();
  726. bgp_scan_init ();
  727. if (fileno (stdout) >= 0)
  728. tty = isatty (fileno (stdout));
  729. if (bgp_get (&bgp, &asn, NULL))
  730. return -1;
  731. peer = peer_create_accept (bgp);
  732. peer->host = (char *)"foo";
  733. peer->status = Established;
  734. for (i = AFI_IP; i < AFI_MAX; i++)
  735. for (j = SAFI_UNICAST; j < SAFI_MAX; j++)
  736. {
  737. peer->afc[i][j] = 1;
  738. peer->afc_adv[i][j] = 1;
  739. }
  740. i = 0;
  741. while (mp_reach_segments[i].name)
  742. parse_test (peer, &mp_reach_segments[i++], BGP_ATTR_MP_REACH_NLRI);
  743. i = 0;
  744. while (mp_unreach_segments[i].name)
  745. parse_test (peer, &mp_unreach_segments[i++], BGP_ATTR_MP_UNREACH_NLRI);
  746. printf ("failures: %d\n", failed);
  747. return failed;
  748. }