9 #include "bgpd/bgp_attr.h"
10 #include "bgpd/bgp_open.h"
11 #include "bgpd/bgp_debug.h"
13 #define VT100_RESET "\x1b[0m"
14 #define VT100_RED "\x1b[31m"
15 #define VT100_GREEN "\x1b[32m"
16 #define VT100_YELLOW "\x1b[33m"
23 /* need these to link in libbgp */
24 struct zebra_privs_t
*bgpd_privs
= NULL
;
25 struct thread_master
*master
= NULL
;
27 static int failed
= 0;
30 /* test segments to parse and validate, and use for other tests */
31 static struct test_segment
{
34 const u_char data
[1024];
36 #define SHOULD_PARSE 0
38 int parses
; /* whether it should parse or not */
40 /* AFI/SAFI validation */
46 } mp_reach_segments
[] =
49 "IPV6 MP Reach, global nexthop, 1 NLRI",
51 /* AFI / SAFI */ 0x0, AFI_IP6
, SAFI_UNICAST
,
52 /* nexthop bytes */ 16,
53 /* Nexthop (global) */ 0xff, 0xfe, 0x1, 0x2,
54 0xaa, 0xbb, 0xcc, 0xdd,
56 0xa1, 0xa2, 0xa3, 0xa4,
57 /* SNPA (defunct, MBZ) */ 0x0,
58 /* NLRI tuples */ 32, 0xff, 0xfe, 0x1, 0x2, /* fffe:102::/32 */
62 AFI_IP6
, SAFI_UNICAST
, VALID_AFI
,
65 "IPV6 MP Reach, global nexthop, 2 NLRIs",
67 /* AFI / SAFI */ 0x0, AFI_IP6
, SAFI_UNICAST
,
68 /* nexthop bytes */ 16,
69 /* Nexthop (global) */ 0xff, 0xfe, 0x1, 0x2, /* ffee:102:... */
70 0xaa, 0xbb, 0xcc, 0xdd,
72 0xa1, 0xa2, 0xa3, 0xa4,
73 /* SNPA (defunct, MBZ) */ 0x0,
75 0xff, 0xfe, 0x1, 0x2, /* fffe:102::/32 */
77 0xff, 0xfe, 0x0, 0x1, /* fffe:1:2:3::/64 */
82 AFI_IP6
, SAFI_UNICAST
, VALID_AFI
,
85 "IPV6 MP Reach, global nexthop, 2 NLRIs + default",
87 /* AFI / SAFI */ 0x0, AFI_IP6
, SAFI_UNICAST
,
88 /* nexthop bytes */ 16,
89 /* Nexthop (global) */ 0xff, 0xfe, 0x1, 0x2,
90 0xaa, 0xbb, 0xcc, 0xdd,
92 0xa1, 0xa2, 0xa3, 0xa4,
93 /* SNPA (defunct, MBZ) */ 0x0,
95 0xff, 0xfe, 0x1, 0x2, /* fffe:102::/32 */
97 0xff, 0xfe, 0x0, 0x1, /* fffe:1:2:3::/64 */
101 (4 + 16 + 1 + 5 + 9 + 1),
103 AFI_IP6
, SAFI_UNICAST
, VALID_AFI
,
106 "IPV6 MP Reach, global+local nexthops, 2 NLRIs + default",
108 /* AFI / SAFI */ 0x0, AFI_IP6
, SAFI_UNICAST
,
109 /* nexthop bytes */ 32,
110 /* Nexthop (global) */ 0xff, 0xfe, 0x1, 0x2, /* fffe:102:... */
111 0xaa, 0xbb, 0xcc, 0xdd,
113 0xa1, 0xa2, 0xa3, 0xa4,
114 /* Nexthop (local) */ 0xfe, 0x80, 0x0, 0x0, /* fe80::210:2ff:.. */
116 0x2, 0x10, 0x2, 0xff,
118 /* SNPA (defunct, MBZ) */ 0x0,
119 /* NLRI tuples */ 32,
120 0xff, 0xfe, 0x1, 0x2, /* fffe:102::/32 */
122 0xff, 0xfe, 0x0, 0x1, /* fffe:1:2:3::/64 */
126 (4 + 32 + 1 + 5 + 9 + 1),
128 AFI_IP6
, SAFI_UNICAST
, VALID_AFI
,
131 "IPV6 MP Reach, inappropriate nexthop length",
133 /* AFI / SAFI */ 0x0, AFI_IP6
, SAFI_UNICAST
,
134 /* nexthop bytes */ 4,
135 /* Nexthop (global) */ 0xff, 0xfe, 0x1, 0x2, /* fffe:102:... */
136 0xaa, 0xbb, 0xcc, 0xdd,
138 0xa1, 0xa2, 0xa3, 0xa4,
139 /* Nexthop (local) */ 0xfe, 0x80, 0x0, 0x0, /* fe80::210:2ff:.. */
141 0x2, 0x10, 0x2, 0xff,
143 /* SNPA (defunct, MBZ) */ 0x0,
144 /* NLRI tuples */ 32,
145 0xff, 0xfe, 0x1, 0x2, /* fffe:102::/32 */
147 0xff, 0xfe, 0x0, 0x1, /* fffe:1:2:3::/64 */
151 (4 + 32 + 1 + 5 + 9 + 1),
153 AFI_IP6
, SAFI_UNICAST
, VALID_AFI
,
156 "IPV6 MP Reach, invalid nexthop length",
158 /* AFI / SAFI */ 0x0, AFI_IP6
, SAFI_UNICAST
,
159 /* nexthop bytes */ 5,
160 /* Nexthop (global) */ 0xff, 0xfe, 0x1, 0x2, /* fffe:102:... */
161 0xaa, 0xbb, 0xcc, 0xdd,
163 0xa1, 0xa2, 0xa3, 0xa4,
164 /* Nexthop (local) */ 0xfe, 0x80, 0x0, 0x0, /* fe80::210:2ff:.. */
166 0x2, 0x10, 0x2, 0xff,
168 /* SNPA (defunct, MBZ) */ 0x0,
169 /* NLRI tuples */ 32,
170 0xff, 0xfe, 0x1, 0x2, /* fffe:102::/32 */
172 0xff, 0xfe, 0x0, 0x1, /* fffe:1:2:3::/64 */
176 (4 + 32 + 1 + 5 + 9 + 1),
178 AFI_IP6
, SAFI_UNICAST
, VALID_AFI
,
181 "IPV6 MP Reach, nexthop length overflow",
183 /* AFI / SAFI */ 0x0, AFI_IP6
, SAFI_UNICAST
,
184 /* nexthop bytes */ 32,
185 /* Nexthop (global) */ 0xff, 0xfe, 0x1, 0x2, /* fffe:102:... */
186 0xaa, 0xbb, 0xcc, 0xdd,
188 0xa1, 0xa2, 0xa3, 0xa4,
192 AFI_IP6
, SAFI_UNICAST
, VALID_AFI
,
195 "IPV6 MP Reach, nexthop length short",
197 /* AFI / SAFI */ 0x0, AFI_IP6
, SAFI_UNICAST
,
198 /* nexthop bytes */ 16,
199 /* Nexthop (global) */ 0xff, 0xfe, 0x1, 0x2, /* fffe:102:... */
200 0xaa, 0xbb, 0xcc, 0xdd,
202 0xa1, 0xa2, 0xa3, 0xa4,
203 /* Nexthop (local) */ 0xfe, 0x80, 0x0, 0x0, /* fe80::210:2ff:.. */
205 0x2, 0x10, 0x2, 0xff,
207 /* SNPA (defunct, MBZ) */ 0x0,
208 /* NLRI tuples */ 32,
209 0xff, 0xfe, 0x1, 0x2, /* fffe:102::/32 */
211 0xff, 0xfe, 0x0, 0x1, /* fffe:1:2:3::/64 */
215 (4 + 32 + 1 + 5 + 9 + 1),
217 AFI_IP6
, SAFI_UNICAST
, VALID_AFI
,
220 "IPV6 MP Reach, NLRI bitlen overflow",
222 /* AFI / SAFI */ 0x0, AFI_IP6
, SAFI_UNICAST
,
223 /* nexthop bytes */ 32,
224 /* Nexthop (global) */ 0xff, 0xfe, 0x1, 0x2, /* fffe:102:... */
225 0xaa, 0xbb, 0xcc, 0xdd,
227 0xa1, 0xa2, 0xa3, 0xa4,
228 /* Nexthop (local) */ 0xfe, 0x80, 0x0, 0x0, /* fe80::210:2ff:.. */
230 0x2, 0x10, 0x2, 0xff,
232 /* SNPA (defunct, MBZ) */ 0x0,
233 /* NLRI tuples */ 120,
234 0xff, 0xfe, 0x1, 0x2, /* fffe:102::/32 */
236 0xff, 0xfe, 0x0, 0x1, /* fffe:1:2:3::/64 */
240 (4 + 32 + 1 + 5 + 9 + 1),
242 AFI_IP6
, SAFI_UNICAST
, VALID_AFI
,
245 "IPv4 MP Reach, 2 NLRIs + default",
247 /* AFI / SAFI */ 0x0, AFI_IP
, SAFI_UNICAST
,
248 /* nexthop bytes */ 4,
249 /* Nexthop */ 192, 168, 0, 1,
250 /* SNPA (defunct, MBZ) */ 0x0,
251 /* NLRI tuples */ 16, 10, 1, /* 10.1/16 */
252 17, 10, 2, 3, /* 10.2.3/17 */
255 (4 + 4 + 1 + 3 + 4 + 1),
257 AFI_IP
, SAFI_UNICAST
, VALID_AFI
,
260 "IPv4 MP Reach, nexthop lenth overflow",
262 /* AFI / SAFI */ 0x0, AFI_IP
, SAFI_UNICAST
,
263 /* nexthop bytes */ 32,
264 /* Nexthop */ 192, 168, 0, 1,
265 /* SNPA (defunct, MBZ) */ 0x0,
266 /* NLRI tuples */ 16, 10, 1, /* 10.1/16 */
267 17, 10, 2, 3, /* 10.2.3/17 */
270 (4 + 4 + 1 + 3 + 4 + 1),
272 AFI_IP
, SAFI_UNICAST
, VALID_AFI
,
275 "IPv4 MP Reach, nlri lenth overflow",
277 /* AFI / SAFI */ 0x0, AFI_IP
, SAFI_UNICAST
,
278 /* nexthop bytes */ 4,
279 /* Nexthop */ 192, 168, 0, 1,
280 /* SNPA (defunct, MBZ) */ 0x0,
281 /* NLRI tuples */ 16, 10, 1, /* 10.1/16 */
285 (4 + 4 + 1 + 3 + 2 + 1),
287 AFI_IP
, SAFI_UNICAST
, VALID_AFI
,
290 "IPv4/VPNv4 MP Reach, RD, Nexthop, 3 NLRIs",
292 /* AFI / SAFI */ 0x0, AFI_IP
, BGP_SAFI_VPNV4
,
293 /* nexthop bytes */ 12,
296 /* Nexthop */ 192, 168, 0, 1,
297 /* SNPA (defunct, MBZ) */ 0x0,
298 /* NLRI tuples */ 16, 10, 1, /* 10.1/16 */
299 17, 10, 2, 3, /* 10.2.3/17 */
302 (4 + 12 + 1 + 3 + 4 + 1),
304 AFI_IP
, SAFI_UNICAST
, VALID_AFI
,
308 "IPv6, global nexthop, 1 default NLRI",
310 /* AFI / SAFI */ 0x0, 0x2, 0x1,
311 /* nexthop bytes */ 0x20,
312 /* Nexthop (global) */ 0x20, 0x01, 0x04, 0x70,
313 0x00, 0x01, 0x00, 0x06,
314 0x00, 0x00, 0x00, 0x00,
315 0x00, 0x00, 0x00, 0x01,
316 /* Nexthop (local) */ 0xfe, 0x80, 0x00, 0x00,
317 0x00, 0x00, 0x00, 0x00,
318 0x02, 0x0c, 0xdb, 0xff,
319 0xfe, 0xfe, 0xeb, 0x00,
320 /* SNPA (defunct, MBZ) */ 0,
321 /* NLRI tuples */ /* Should have 0 here for ::/0, but dont */
325 AFI_IP6
, SAFI_UNICAST
, VALID_AFI
,
328 { NULL
, NULL
, {0}, 0, 0}
331 /* MP_UNREACH_NLRI tests */
332 static struct test_segment mp_unreach_segments
[] =
335 "IPV6 MP Unreach, 1 NLRI",
337 /* AFI / SAFI */ 0x0, AFI_IP6
, SAFI_UNICAST
,
338 /* NLRI tuples */ 32, 0xff, 0xfe, 0x1, 0x2, /* fffe:102::/32 */
342 AFI_IP6
, SAFI_UNICAST
, VALID_AFI
,
345 "IPV6 MP Unreach, 2 NLRIs",
347 /* AFI / SAFI */ 0x0, AFI_IP6
, SAFI_UNICAST
,
348 /* NLRI tuples */ 32,
349 0xff, 0xfe, 0x1, 0x2, /* fffe:102::/32 */
351 0xff, 0xfe, 0x0, 0x1, /* fffe:1:2:3::/64 */
356 AFI_IP6
, SAFI_UNICAST
, VALID_AFI
,
358 { "IPv6-unreach-default",
359 "IPV6 MP Unreach, 2 NLRIs + default",
361 /* AFI / SAFI */ 0x0, AFI_IP6
, SAFI_UNICAST
,
362 /* NLRI tuples */ 32,
363 0xff, 0xfe, 0x1, 0x2, /* fffe:102::/32 */
365 0xff, 0xfe, 0x0, 0x1, /* fffe:1:2:3::/64 */
371 AFI_IP6
, SAFI_UNICAST
, VALID_AFI
,
373 { "IPv6-unreach-nlri",
374 "IPV6 MP Unreach, NLRI bitlen overflow",
376 /* AFI / SAFI */ 0x0, AFI_IP6
, SAFI_UNICAST
,
377 /* NLRI tuples */ 120,
378 0xff, 0xfe, 0x1, 0x2, /* fffe:102::/32 */
380 0xff, 0xfe, 0x0, 0x1, /* fffe:1:2:3::/64 */
386 AFI_IP6
, SAFI_UNICAST
, VALID_AFI
,
389 "IPv4 MP Unreach, 2 NLRIs + default",
391 /* AFI / SAFI */ 0x0, AFI_IP
, SAFI_UNICAST
,
392 /* NLRI tuples */ 16, 10, 1, /* 10.1/16 */
393 17, 10, 2, 3, /* 10.2.3/17 */
398 AFI_IP
, SAFI_UNICAST
, VALID_AFI
,
400 { "IPv4-unreach-nlrilen",
401 "IPv4 MP Unreach, nlri length overflow",
403 /* AFI / SAFI */ 0x0, AFI_IP
, SAFI_UNICAST
,
404 /* NLRI tuples */ 16, 10, 1, /* 10.1/16 */
410 AFI_IP
, SAFI_UNICAST
, VALID_AFI
,
412 { "IPv4-unreach-vpnv4",
413 "IPv4/VPNv4 MP Unreach, RD, 3 NLRIs",
415 /* AFI / SAFI */ 0x0, AFI_IP
, BGP_SAFI_VPNV4
,
416 /* nexthop bytes */ 12,
419 /* Nexthop */ 192, 168, 0, 1,
420 /* SNPA (defunct, MBZ) */ 0x0,
421 /* NLRI tuples */ 16, 10, 1, /* 10.1/16 */
422 17, 10, 2, 3, /* 10.2.3/17 */
427 AFI_IP
, SAFI_UNICAST
, VALID_AFI
,
429 { NULL
, NULL
, {0}, 0, 0}
433 /* basic parsing test */
435 parse_test (struct peer
*peer
, struct test_segment
*t
, int type
)
438 int oldfailed
= failed
;
440 struct bgp_nlri nlri
;
441 #define RANDOM_FUZZ 35
443 stream_reset (peer
->ibuf
);
444 stream_put (peer
->ibuf
, NULL
, RANDOM_FUZZ
);
445 stream_set_getp (peer
->ibuf
, RANDOM_FUZZ
);
447 stream_write (peer
->ibuf
, t
->data
, t
->len
);
449 printf ("%s: %s\n", t
->name
, t
->desc
);
451 if (type
== BGP_ATTR_MP_REACH_NLRI
)
452 ret
= bgp_mp_reach_parse (peer
, t
->len
, &attr
, &nlri
);
454 ret
= bgp_mp_unreach_parse (peer
, t
->len
, &nlri
);
458 safi_t safi
= t
->safi
;
460 if (bgp_afi_safi_valid_indices (t
->afi
, &safi
) != t
->afi_valid
)
463 printf ("MP: %u/%u (%u): recv %u, nego %u\n",
464 t
->afi
, t
->safi
, safi
,
465 peer
->afc_recv
[t
->afi
][safi
],
466 peer
->afc_nego
[t
->afi
][safi
]);
469 printf ("parsed?: %s\n", ret
? "no" : "yes");
471 if (ret
!= t
->parses
)
475 printf ("%s", (failed
> oldfailed
) ? VT100_RED
"failed!" VT100_RESET
476 : VT100_GREEN
"OK" VT100_RESET
);
478 printf ("%s", (failed
> oldfailed
) ? "failed!" : "OK" );
481 printf (" (%u)", failed
);
486 static struct bgp
*bgp
;
487 static as_t asn
= 100;
495 conf_bgp_debug_fsm
= -1UL;
496 conf_bgp_debug_events
= -1UL;
497 conf_bgp_debug_packet
= -1UL;
498 conf_bgp_debug_normal
= -1UL;
499 conf_bgp_debug_as4
= -1UL;
500 term_bgp_debug_fsm
= -1UL;
501 term_bgp_debug_events
= -1UL;
502 term_bgp_debug_packet
= -1UL;
503 term_bgp_debug_normal
= -1UL;
504 term_bgp_debug_as4
= -1UL;
506 master
= thread_master_create ();
509 if (fileno (stdout
) >= 0)
510 tty
= isatty (fileno (stdout
));
512 if (bgp_get (&bgp
, &asn
, NULL
))
515 peer
= peer_create_accept (bgp
);
518 for (i
= AFI_IP
; i
< AFI_MAX
; i
++)
519 for (j
= SAFI_UNICAST
; j
< SAFI_MAX
; j
++)
522 peer
->afc_adv
[i
][j
] = 1;
526 while (mp_reach_segments
[i
].name
)
527 parse_test (peer
, &mp_reach_segments
[i
++], BGP_ATTR_MP_REACH_NLRI
);
530 while (mp_unreach_segments
[i
].name
)
531 parse_test (peer
, &mp_unreach_segments
[i
++], BGP_ATTR_MP_UNREACH_NLRI
);
533 printf ("failures: %d\n", failed
);