9
#include "bgpd/bgp_open.h"
10
#include "bgpd/bgp_debug.h"
15
/* need these to link in libbgp */
16
struct zebra_privs_t *bgpd_privs = NULL;
17
struct thread_master *master = NULL;
19
static int failed = 0;
21
/* test segments to parse and validate, and use for other tests */
22
static struct test_segment {
25
const u_char data[1024];
27
#define SHOULD_PARSE 0
29
int parses; /* whether it should parse or not */
34
"capability header, and no more",
35
{ CAPABILITY_CODE_REFRESH, 0x0 },
40
"header, no data but length says there is",
46
"valid, with padding",
47
{ CAPABILITY_CODE_REFRESH, 0x2, 0x0, 0x0 },
52
"violates minsize requirement",
53
{ CAPABILITY_CODE_ORF, 0x2, 0x0, 0x0 },
59
{ 0x1, 0x4, 0x0, 0x1, 0x0, 0x1 },
65
{ CAPABILITY_CODE_MP, 0x4, 0x0, 0x1, 0x0, 0x2 },
71
{ CAPABILITY_CODE_MP, 0x4, 0x0, 0x2, 0x0, 0x80 },
72
6, SHOULD_PARSE, /* parses, but invalid afi,safi */
77
{ CAPABILITY_CODE_MP, 0x4, 0x0, 0x2, 0x0, 0x4 },
83
{ CAPABILITY_CODE_MP, 0x4, 0x0, 0x1, 0x0, 0x80 },
89
{ CAPABILITY_CODE_MP, 0x4, 0x0, 0x1, 0x0, 0x81 },
90
6, SHOULD_PARSE, /* parses, but invalid afi,safi tuple! - manually inspect */
95
{ CAPABILITY_CODE_MP, 0x4, 0x0, 0xa, 0x0, 0x81 },
96
6, SHOULD_PARSE, /* parses, but unknown */
100
"MP IP4/Unicast, length too short (< minimum)",
101
{ CAPABILITY_CODE_MP, 0x2, 0x0, 0x1, 0x0, 0x1 },
106
"MP IP4/Unicast, length too long",
107
{ CAPABILITY_CODE_MP, 0x6, 0x0, 0x1, 0x0, 0x1 },
112
"ORF, simple, single entry, single tuple",
113
{ /* hdr */ CAPABILITY_CODE_ORF, 0x7,
114
/* mpc */ 0x0, 0x1, 0x0, 0x1,
116
/* tuples */ 0x40, 0x3
122
"ORF, multi entry/tuple",
123
{ /* hdr */ CAPABILITY_CODE_ORF, 0x21,
124
/* mpc */ 0x0, 0x1, 0x0, 0x1,
126
/* tuples */ 0x40, ORF_MODE_BOTH,
127
0x80, ORF_MODE_RECEIVE,
129
/* mpc */ 0x0, 0x2, 0x0, 0x1,
131
/* tuples */ 0x40, ORF_MODE_BOTH,
132
0x80, ORF_MODE_RECEIVE,
134
/* mpc */ 0x0, 0x2, 0x0, 0x2,
136
/* tuples */ 0x40, ORF_MODE_RECEIVE,
144
"ORF, multi entry/tuple, hdr length too short",
145
{ /* hdr */ CAPABILITY_CODE_ORF, 0x15,
146
/* mpc */ 0x0, 0x1, 0x0, 0x1,
148
/* tuples */ 0x40, 0x3,
151
/* mpc */ 0x0, 0x1, 0x0, 0x1,
153
/* tuples */ 0x40, 0x3,
156
/* mpc */ 0x0, 0x2, 0x0, 0x2,
158
/* tuples */ 0x40, 0x3,
162
35, SHOULD_ERR, /* It should error on invalid Route-Refresh.. */
166
"ORF, multi entry/tuple, length too long",
167
{ /* hdr */ 0x3, 0x22,
168
/* mpc */ 0x0, 0x1, 0x0, 0x1,
170
/* tuples */ 0x40, 0x3,
173
/* mpc */ 0x0, 0x2, 0x0, 0x1,
175
/* tuples */ 0x40, 0x3,
178
/* mpc */ 0x0, 0x2, 0x0, 0x2,
180
/* tuples */ 0x40, 0x3,
188
"ORF, multi entry/tuple, entry number too long",
189
{ /* hdr */ 0x3, 0x21,
190
/* mpc */ 0x0, 0x1, 0x0, 0x1,
192
/* tuples */ 0x40, 0x3,
195
/* mpc */ 0x0, 0x2, 0x0, 0x1,
197
/* tuples */ 0x40, 0x3,
200
/* mpc */ 0x0, 0x2, 0x0, 0x2,
202
/* tuples */ 0x40, 0x3,
206
35, SHOULD_PARSE, /* parses, but last few tuples should be gibberish */
210
"ORF, multi entry/tuple, entry number too short",
211
{ /* hdr */ 0x3, 0x21,
212
/* mpc */ 0x0, 0x1, 0x0, 0x1,
214
/* tuples */ 0x40, 0x3,
217
/* mpc */ 0x0, 0x2, 0x0, 0x1,
219
/* tuples */ 0x40, 0x3,
222
/* mpc */ 0x0, 0x2, 0x0, 0x2,
224
/* tuples */ 0x40, 0x3,
228
35, SHOULD_PARSE, /* Parses, but should get gibberish afi/safis */
232
"ORF, multi entry/tuple, padded to align",
233
{ /* hdr */ 0x3, 0x22,
234
/* mpc */ 0x0, 0x1, 0x0, 0x1,
236
/* tuples */ 0x40, 0x3,
239
/* mpc */ 0x0, 0x2, 0x0, 0x1,
241
/* tuples */ 0x40, 0x3,
244
/* mpc */ 0x0, 0x2, 0x0, 0x2,
246
/* tuples */ 0x40, 0x3,
256
{ 0x41, 0x4, 0xab, 0xcd, 0xef, 0x12 },
262
{ /* hdr */ CAPABILITY_CODE_RESTART, 0xe,
263
/* R-bit, time */ 0xf1, 0x12,
278
"GR capability, but header length too short",
279
{ /* hdr */ 0x40, 0xa,
280
/* R-bit, time */ 0xf1, 0x12,
295
"GR capability, but header length too long",
296
{ /* hdr */ 0x40, 0xf,
297
/* R-bit, time */ 0xf1, 0x12,
310
"GR capability, but truncated",
311
{ /* hdr */ 0x40, 0xf,
312
/* R-bit, time */ 0xf1, 0x12,
326
"Dynamic capability (deprecated version)",
327
{ CAPABILITY_CODE_DYNAMIC, 0x0 },
330
{ NULL, NULL, {0}, 0, 0}
334
struct test_segment dynamic_cap_msgs[] =
337
"Dynamic Capability Message, IP/Multicast",
338
{ 0x0, 0x1, 0x4, 0x0, 0x1, 0x0, 0x2 },
339
7, SHOULD_PARSE, /* horrible alignment, just as with ORF */
342
"Dynamic Capability Message, IP/Multicast, truncated",
343
{ 0x0, 0x1, 0x4, 0x0, 0x1, 0x0, 0x2 },
347
"Dynamic Capability Message, IP/Multicast, padded",
348
{ 0x0, 0x1, 0x4, 0x0, 0x1, 0x0, 0x2, 0x0 },
349
8, SHOULD_ERR, /* No way to tell padding from data.. */
352
"Dynamic Capability Message, IP/Multicast, cap data padded",
353
{ 0x0, 0x1, 0x5, 0x0, 0x1, 0x0, 0x2, 0x0 },
354
8, SHOULD_PARSE, /* You can though add padding to the capability data */
356
{ "DynCapMPCoverflow",
357
"Dynamic Capability Message, IP/Multicast, cap data != length",
358
{ 0x0, 0x1, 0x3, 0x0, 0x1, 0x0, 0x2, 0x0 },
361
{ NULL, NULL, {0}, 0, 0}
363
/* basic parsing test */
365
parse_test (struct peer *peer, struct test_segment *t, int type)
370
stream_reset (peer->ibuf);
374
stream_putc (peer->ibuf, BGP_OPEN_OPT_CAP);
375
stream_putc (peer->ibuf, t->len);
378
/* for (i = 0; i < BGP_MARKER_SIZE; i++)
379
stream_putc (peer->, 0xff);
381
stream_putc (s, BGP_MSG_CAPABILITY);*/
384
stream_write (peer->ibuf, t->data, t->len);
386
printf ("%s: %s\n", t->name, t->desc);
391
ret = bgp_open_option_parse (peer, t->len + 2, &capability);
394
ret = bgp_capability_receive (peer, t->len);
397
printf ("unknown type %u\n", type);
401
printf ("parsed?: %s\n", ret ? "no" : "yes");
403
if (ret == t->parses)
414
static struct bgp *bgp;
415
static as_t asn = 100;
423
conf_bgp_debug_fsm = -1UL;
424
conf_bgp_debug_events = -1UL;
425
conf_bgp_debug_packet = -1UL;
426
conf_bgp_debug_normal = -1UL;
427
term_bgp_debug_fsm = -1UL;
428
term_bgp_debug_events = -1UL;
429
term_bgp_debug_packet = -1UL;
430
term_bgp_debug_normal = -1UL;
432
master = thread_master_create ();
435
if (bgp_get (&bgp, &asn, NULL))
438
peer = peer_create_accept (bgp);
440
for (i = AFI_IP; i < AFI_MAX; i++)
441
for (j = SAFI_UNICAST; j < SAFI_MAX; j++)
442
peer->afc_nego[i][j] = 1;
445
while (test_segments[i].name)
446
parse_test (peer, &test_segments[i++], OPEN);
448
SET_FLAG (peer->cap, PEER_CAP_DYNAMIC_ADV);
449
peer->status = Established;
452
while (dynamic_cap_msgs[i].name)
453
parse_test (peer, &dynamic_cap_msgs[i++], DYNCAP);
455
printf ("failures: %d\n", failed);