10 #include "bgpd/bgp_attr.h"
11 #include "bgpd/bgp_open.h"
12 #include "bgpd/bgp_debug.h"
14 #define VT100_RESET "\x1b[0m"
15 #define VT100_RED "\x1b[31m"
16 #define VT100_GREEN "\x1b[32m"
17 #define VT100_YELLOW "\x1b[33m"
24 /* need these to link in libbgp */
25 struct zebra_privs_t
*bgpd_privs
= NULL
;
26 struct thread_master
*master
= NULL
;
28 static int failed
= 0;
31 /* test segments to parse and validate, and use for other tests */
32 static struct test_segment
{
35 const u_char data
[1024];
37 #define SHOULD_PARSE 0
39 int parses
; /* whether it should parse or not */
41 /* AFI/SAFI validation */
47 } mp_reach_segments
[] =
50 "IPV6 MP Reach, global nexthop, 1 NLRI",
52 /* AFI / SAFI */ 0x0, AFI_IP6
, SAFI_UNICAST
,
53 /* nexthop bytes */ 16,
54 /* Nexthop (global) */ 0xff, 0xfe, 0x1, 0x2,
55 0xaa, 0xbb, 0xcc, 0xdd,
57 0xa1, 0xa2, 0xa3, 0xa4,
58 /* SNPA (defunct, MBZ) */ 0x0,
59 /* NLRI tuples */ 32, 0xff, 0xfe, 0x1, 0x2, /* fffe:102::/32 */
63 AFI_IP6
, SAFI_UNICAST
, VALID_AFI
,
66 "IPV6 MP Reach, global nexthop, 2 NLRIs",
68 /* AFI / SAFI */ 0x0, AFI_IP6
, SAFI_UNICAST
,
69 /* nexthop bytes */ 16,
70 /* Nexthop (global) */ 0xff, 0xfe, 0x1, 0x2, /* ffee:102:... */
71 0xaa, 0xbb, 0xcc, 0xdd,
73 0xa1, 0xa2, 0xa3, 0xa4,
74 /* SNPA (defunct, MBZ) */ 0x0,
76 0xff, 0xfe, 0x1, 0x2, /* fffe:102::/32 */
78 0xff, 0xfe, 0x0, 0x1, /* fffe:1:2:3::/64 */
83 AFI_IP6
, SAFI_UNICAST
, VALID_AFI
,
86 "IPV6 MP Reach, global nexthop, 2 NLRIs + default",
88 /* AFI / SAFI */ 0x0, AFI_IP6
, SAFI_UNICAST
,
89 /* nexthop bytes */ 16,
90 /* Nexthop (global) */ 0xff, 0xfe, 0x1, 0x2,
91 0xaa, 0xbb, 0xcc, 0xdd,
93 0xa1, 0xa2, 0xa3, 0xa4,
94 /* SNPA (defunct, MBZ) */ 0x0,
96 0xff, 0xfe, 0x1, 0x2, /* fffe:102::/32 */
98 0xff, 0xfe, 0x0, 0x1, /* fffe:1:2:3::/64 */
102 (4 + 16 + 1 + 5 + 9 + 1),
104 AFI_IP6
, SAFI_UNICAST
, VALID_AFI
,
107 "IPV6 MP Reach, global+local nexthops, 2 NLRIs + default",
109 /* AFI / SAFI */ 0x0, AFI_IP6
, SAFI_UNICAST
,
110 /* nexthop bytes */ 32,
111 /* Nexthop (global) */ 0xff, 0xfe, 0x1, 0x2, /* fffe:102:... */
112 0xaa, 0xbb, 0xcc, 0xdd,
114 0xa1, 0xa2, 0xa3, 0xa4,
115 /* Nexthop (local) */ 0xfe, 0x80, 0x0, 0x0, /* fe80::210:2ff:.. */
117 0x2, 0x10, 0x2, 0xff,
119 /* SNPA (defunct, MBZ) */ 0x0,
120 /* NLRI tuples */ 32,
121 0xff, 0xfe, 0x1, 0x2, /* fffe:102::/32 */
123 0xff, 0xfe, 0x0, 0x1, /* fffe:1:2:3::/64 */
127 (4 + 32 + 1 + 5 + 9 + 1),
129 AFI_IP6
, SAFI_UNICAST
, VALID_AFI
,
132 "IPV6 MP Reach, inappropriate nexthop length",
134 /* AFI / SAFI */ 0x0, AFI_IP6
, SAFI_UNICAST
,
135 /* nexthop bytes */ 4,
136 /* Nexthop (global) */ 0xff, 0xfe, 0x1, 0x2, /* fffe:102:... */
137 0xaa, 0xbb, 0xcc, 0xdd,
139 0xa1, 0xa2, 0xa3, 0xa4,
140 /* Nexthop (local) */ 0xfe, 0x80, 0x0, 0x0, /* fe80::210:2ff:.. */
142 0x2, 0x10, 0x2, 0xff,
144 /* SNPA (defunct, MBZ) */ 0x0,
145 /* NLRI tuples */ 32,
146 0xff, 0xfe, 0x1, 0x2, /* fffe:102::/32 */
148 0xff, 0xfe, 0x0, 0x1, /* fffe:1:2:3::/64 */
152 (4 + 32 + 1 + 5 + 9 + 1),
154 AFI_IP6
, SAFI_UNICAST
, VALID_AFI
,
157 "IPV6 MP Reach, invalid nexthop length",
159 /* AFI / SAFI */ 0x0, AFI_IP6
, SAFI_UNICAST
,
160 /* nexthop bytes */ 5,
161 /* Nexthop (global) */ 0xff, 0xfe, 0x1, 0x2, /* fffe:102:... */
162 0xaa, 0xbb, 0xcc, 0xdd,
164 0xa1, 0xa2, 0xa3, 0xa4,
165 /* Nexthop (local) */ 0xfe, 0x80, 0x0, 0x0, /* fe80::210:2ff:.. */
167 0x2, 0x10, 0x2, 0xff,
169 /* SNPA (defunct, MBZ) */ 0x0,
170 /* NLRI tuples */ 32,
171 0xff, 0xfe, 0x1, 0x2, /* fffe:102::/32 */
173 0xff, 0xfe, 0x0, 0x1, /* fffe:1:2:3::/64 */
177 (4 + 32 + 1 + 5 + 9 + 1),
179 AFI_IP6
, SAFI_UNICAST
, VALID_AFI
,
182 "IPV6 MP Reach, nexthop length overflow",
184 /* AFI / SAFI */ 0x0, AFI_IP6
, SAFI_UNICAST
,
185 /* nexthop bytes */ 32,
186 /* Nexthop (global) */ 0xff, 0xfe, 0x1, 0x2, /* fffe:102:... */
187 0xaa, 0xbb, 0xcc, 0xdd,
189 0xa1, 0xa2, 0xa3, 0xa4,
193 AFI_IP6
, SAFI_UNICAST
, VALID_AFI
,
196 "IPV6 MP Reach, nexthop length short",
198 /* AFI / SAFI */ 0x0, AFI_IP6
, SAFI_UNICAST
,
199 /* nexthop bytes */ 16,
200 /* Nexthop (global) */ 0xff, 0xfe, 0x1, 0x2, /* fffe:102:... */
201 0xaa, 0xbb, 0xcc, 0xdd,
203 0xa1, 0xa2, 0xa3, 0xa4,
204 /* Nexthop (local) */ 0xfe, 0x80, 0x0, 0x0, /* fe80::210:2ff:.. */
206 0x2, 0x10, 0x2, 0xff,
208 /* SNPA (defunct, MBZ) */ 0x0,
209 /* NLRI tuples */ 32,
210 0xff, 0xfe, 0x1, 0x2, /* fffe:102::/32 */
212 0xff, 0xfe, 0x0, 0x1, /* fffe:1:2:3::/64 */
216 (4 + 32 + 1 + 5 + 9 + 1),
218 AFI_IP6
, SAFI_UNICAST
, VALID_AFI
,
221 "IPV6 MP Reach, NLRI bitlen overflow",
223 /* AFI / SAFI */ 0x0, AFI_IP6
, SAFI_UNICAST
,
224 /* nexthop bytes */ 32,
225 /* Nexthop (global) */ 0xff, 0xfe, 0x1, 0x2, /* fffe:102:... */
226 0xaa, 0xbb, 0xcc, 0xdd,
228 0xa1, 0xa2, 0xa3, 0xa4,
229 /* Nexthop (local) */ 0xfe, 0x80, 0x0, 0x0, /* fe80::210:2ff:.. */
231 0x2, 0x10, 0x2, 0xff,
233 /* SNPA (defunct, MBZ) */ 0x0,
234 /* NLRI tuples */ 120,
235 0xff, 0xfe, 0x1, 0x2, /* fffe:102::/32 */
237 0xff, 0xfe, 0x0, 0x1, /* fffe:1:2:3::/64 */
241 (4 + 32 + 1 + 5 + 9 + 1),
243 AFI_IP6
, SAFI_UNICAST
, VALID_AFI
,
246 "IPv4 MP Reach, 2 NLRIs + default",
248 /* AFI / SAFI */ 0x0, AFI_IP
, SAFI_UNICAST
,
249 /* nexthop bytes */ 4,
250 /* Nexthop */ 192, 168, 0, 1,
251 /* SNPA (defunct, MBZ) */ 0x0,
252 /* NLRI tuples */ 16, 10, 1, /* 10.1/16 */
253 17, 10, 2, 3, /* 10.2.3/17 */
256 (4 + 4 + 1 + 3 + 4 + 1),
258 AFI_IP
, SAFI_UNICAST
, VALID_AFI
,
261 "IPv4 MP Reach, nexthop lenth overflow",
263 /* AFI / SAFI */ 0x0, AFI_IP
, SAFI_UNICAST
,
264 /* nexthop bytes */ 32,
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 */
271 (4 + 4 + 1 + 3 + 4 + 1),
273 AFI_IP
, SAFI_UNICAST
, VALID_AFI
,
276 "IPv4 MP Reach, nlri lenth overflow",
278 /* AFI / SAFI */ 0x0, AFI_IP
, SAFI_UNICAST
,
279 /* nexthop bytes */ 4,
280 /* Nexthop */ 192, 168, 0, 1,
281 /* SNPA (defunct, MBZ) */ 0x0,
282 /* NLRI tuples */ 16, 10, 1, /* 10.1/16 */
286 (4 + 4 + 1 + 3 + 2 + 1),
288 AFI_IP
, SAFI_UNICAST
, VALID_AFI
,
291 "IPv4/MPLS-labeled VPN MP Reach, RD, Nexthop, 3 NLRIs",
293 /* AFI / SAFI */ 0x0, AFI_IP
, SAFI_MPLS_LABELED_VPN
,
294 /* nexthop bytes */ 12,
297 /* Nexthop */ 192, 168, 0, 1,
298 /* SNPA (defunct, MBZ) */ 0x0,
299 /* NLRI tuples */ 16, 10, 1, /* 10.1/16 */
300 17, 10, 2, 3, /* 10.2.3/17 */
303 (4 + 12 + 1 + 3 + 4 + 1),
305 AFI_IP
, SAFI_UNICAST
, VALID_AFI
,
309 "IPv6, global nexthop, 1 default NLRI",
311 /* AFI / SAFI */ 0x0, 0x2, 0x1,
312 /* nexthop bytes */ 0x20,
313 /* Nexthop (global) */ 0x20, 0x01, 0x04, 0x70,
314 0x00, 0x01, 0x00, 0x06,
315 0x00, 0x00, 0x00, 0x00,
316 0x00, 0x00, 0x00, 0x01,
317 /* Nexthop (local) */ 0xfe, 0x80, 0x00, 0x00,
318 0x00, 0x00, 0x00, 0x00,
319 0x02, 0x0c, 0xdb, 0xff,
320 0xfe, 0xfe, 0xeb, 0x00,
321 /* SNPA (defunct, MBZ) */ 0,
322 /* NLRI tuples */ /* Should have 0 here for ::/0, but dont */
326 AFI_IP6
, SAFI_UNICAST
, VALID_AFI
,
329 { NULL
, NULL
, {0}, 0, 0}
332 /* MP_UNREACH_NLRI tests */
333 static struct test_segment mp_unreach_segments
[] =
336 "IPV6 MP Unreach, 1 NLRI",
338 /* AFI / SAFI */ 0x0, AFI_IP6
, SAFI_UNICAST
,
339 /* NLRI tuples */ 32, 0xff, 0xfe, 0x1, 0x2, /* fffe:102::/32 */
343 AFI_IP6
, SAFI_UNICAST
, VALID_AFI
,
346 "IPV6 MP Unreach, 2 NLRIs",
348 /* AFI / SAFI */ 0x0, AFI_IP6
, SAFI_UNICAST
,
349 /* NLRI tuples */ 32,
350 0xff, 0xfe, 0x1, 0x2, /* fffe:102::/32 */
352 0xff, 0xfe, 0x0, 0x1, /* fffe:1:2:3::/64 */
357 AFI_IP6
, SAFI_UNICAST
, VALID_AFI
,
359 { "IPv6-unreach-default",
360 "IPV6 MP Unreach, 2 NLRIs + default",
362 /* AFI / SAFI */ 0x0, AFI_IP6
, SAFI_UNICAST
,
363 /* NLRI tuples */ 32,
364 0xff, 0xfe, 0x1, 0x2, /* fffe:102::/32 */
366 0xff, 0xfe, 0x0, 0x1, /* fffe:1:2:3::/64 */
372 AFI_IP6
, SAFI_UNICAST
, VALID_AFI
,
374 { "IPv6-unreach-nlri",
375 "IPV6 MP Unreach, NLRI bitlen overflow",
377 /* AFI / SAFI */ 0x0, AFI_IP6
, SAFI_UNICAST
,
378 /* NLRI tuples */ 120,
379 0xff, 0xfe, 0x1, 0x2, /* fffe:102::/32 */
381 0xff, 0xfe, 0x0, 0x1, /* fffe:1:2:3::/64 */
387 AFI_IP6
, SAFI_UNICAST
, VALID_AFI
,
390 "IPv4 MP Unreach, 2 NLRIs + default",
392 /* AFI / SAFI */ 0x0, AFI_IP
, SAFI_UNICAST
,
393 /* NLRI tuples */ 16, 10, 1, /* 10.1/16 */
394 17, 10, 2, 3, /* 10.2.3/17 */
399 AFI_IP
, SAFI_UNICAST
, VALID_AFI
,
401 { "IPv4-unreach-nlrilen",
402 "IPv4 MP Unreach, nlri length overflow",
404 /* AFI / SAFI */ 0x0, AFI_IP
, SAFI_UNICAST
,
405 /* NLRI tuples */ 16, 10, 1, /* 10.1/16 */
411 AFI_IP
, SAFI_UNICAST
, VALID_AFI
,
413 { "IPv4-unreach-MLVPN",
414 "IPv4/MPLS-labeled VPN MP Unreach, RD, 3 NLRIs",
416 /* AFI / SAFI */ 0x0, AFI_IP
, SAFI_MPLS_LABELED_VPN
,
417 /* nexthop bytes */ 12,
420 /* Nexthop */ 192, 168, 0, 1,
421 /* SNPA (defunct, MBZ) */ 0x0,
422 /* NLRI tuples */ 16, 10, 1, /* 10.1/16 */
423 17, 10, 2, 3, /* 10.2.3/17 */
428 AFI_IP
, SAFI_UNICAST
, VALID_AFI
,
430 { NULL
, NULL
, {0}, 0, 0}
434 /* basic parsing test */
436 parse_test (struct peer
*peer
, struct test_segment
*t
, int type
)
439 int oldfailed
= failed
;
440 struct attr attr
= { };
441 struct bgp_nlri nlri
= { };
442 struct bgp_attr_parser_args attr_args
= {
447 .type
= BGP_ATTR_MP_REACH_NLRI
,
448 .flags
= BGP_ATTR_FLAG_OPTIONAL
,
449 .startp
= BGP_INPUT_PNT (peer
),
451 #define RANDOM_FUZZ 35
453 stream_reset (peer
->ibuf
);
454 stream_put (peer
->ibuf
, NULL
, RANDOM_FUZZ
);
455 stream_set_getp (peer
->ibuf
, RANDOM_FUZZ
);
457 stream_write (peer
->ibuf
, t
->data
, t
->len
);
459 printf ("%s: %s\n", t
->name
, t
->desc
);
462 if (type
== BGP_ATTR_MP_REACH_NLRI
)
463 ret
= bgp_mp_reach_parse (&attr_args
, &nlri
);
465 ret
= bgp_mp_unreach_parse (&attr_args
, &nlri
);
469 safi_t safi
= t
->safi
;
471 if (bgp_afi_safi_valid_indices (t
->afi
, &safi
) != t
->afi_valid
)
474 printf ("MP: %u/%u (%u): recv %u, nego %u\n",
475 t
->afi
, t
->safi
, safi
,
476 peer
->afc_recv
[t
->afi
][safi
],
477 peer
->afc_nego
[t
->afi
][safi
]);
480 printf ("parsed?: %s\n", ret
? "no" : "yes");
482 if ((ret
== 0) != (t
->parses
== 0))
486 printf ("%s", (failed
> oldfailed
) ? VT100_RED
"failed!" VT100_RESET
487 : VT100_GREEN
"OK" VT100_RESET
);
489 printf ("%s", (failed
> oldfailed
) ? "failed!" : "OK" );
492 printf (" (%u)", failed
);
497 static struct bgp
*bgp
;
498 static as_t asn
= 100;
506 conf_bgp_debug_neighbor_events
= -1UL;
507 conf_bgp_debug_packet
= -1UL;
508 conf_bgp_debug_as4
= -1UL;
509 term_bgp_debug_neighbor_events
= -1UL;
510 term_bgp_debug_packet
= -1UL;
511 term_bgp_debug_as4
= -1UL;
513 master
= thread_master_create ();
515 bgp_option_set (BGP_OPT_NO_LISTEN
);
517 if (fileno (stdout
) >= 0)
518 tty
= isatty (fileno (stdout
));
520 if (bgp_get (&bgp
, &asn
, NULL
))
523 peer
= peer_create_accept (bgp
);
526 for (i
= AFI_IP
; i
< AFI_MAX
; i
++)
527 for (j
= SAFI_UNICAST
; j
< SAFI_MAX
; j
++)
530 peer
->afc_adv
[i
][j
] = 1;
534 while (mp_reach_segments
[i
].name
)
535 parse_test (peer
, &mp_reach_segments
[i
++], BGP_ATTR_MP_REACH_NLRI
);
538 while (mp_unreach_segments
[i
].name
)
539 parse_test (peer
, &mp_unreach_segments
[i
++], BGP_ATTR_MP_UNREACH_NLRI
);
541 printf ("failures: %d\n", failed
);