]> git.proxmox.com Git - mirror_frr.git/blame - tests/bgpd/test_mp_attr.c
*: reindent
[mirror_frr.git] / tests / bgpd / test_mp_attr.c
CommitLineData
ac4d0be5 1/*
46f4a4d2
PJ
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
ac4d0be5 19 * 02111-1307, USA.
46f4a4d2
PJ
20 */
21
1eab26bb
PJ
22#include <zebra.h>
23
1bf9f027 24#include "qobj.h"
1eab26bb
PJ
25#include "vty.h"
26#include "stream.h"
27#include "privs.h"
28#include "memory.h"
3f9c7369 29#include "queue.h"
039f3a34 30#include "filter.h"
1eab26bb
PJ
31
32#include "bgpd/bgpd.h"
33#include "bgpd/bgp_attr.h"
34#include "bgpd/bgp_open.h"
35#include "bgpd/bgp_debug.h"
48a5452b 36#include "bgpd/bgp_route.h"
96e52474 37#include "bgpd/bgp_packet.h"
48a5452b
PJ
38#include "bgpd/bgp_mplsvpn.h"
39#include "bgpd/bgp_nexthop.h"
1eab26bb
PJ
40
41#define VT100_RESET "\x1b[0m"
42#define VT100_RED "\x1b[31m"
43#define VT100_GREEN "\x1b[32m"
44#define VT100_YELLOW "\x1b[33m"
45
1eab26bb
PJ
46#define CAPABILITY 0
47#define DYNCAP 1
48#define OPT_PARAM 2
49
50/* need these to link in libbgp */
51struct zebra_privs_t *bgpd_privs = NULL;
52struct thread_master *master = NULL;
53
54static int failed = 0;
55static int tty = 0;
56
57/* test segments to parse and validate, and use for other tests */
58static struct test_segment {
ac4d0be5 59 const char *name;
60 const char *desc;
61 const u_char data[1024];
62 int len;
1eab26bb
PJ
63#define SHOULD_PARSE 0
64#define SHOULD_ERR -1
ac4d0be5 65 int parses; /* whether it should parse or not */
66
67 /* AFI/SAFI validation */
68 afi_t afi;
69 safi_t safi;
1eab26bb
PJ
70#define VALID_AFI 1
71#define INVALID_AFI 0
ac4d0be5 72 int afi_valid;
73} mp_reach_segments[] = {
74 {
75 "IPv6",
76 "IPV6 MP Reach, global nexthop, 1 NLRI",
77 {
78 /* AFI / SAFI */ 0x0,
79 AFI_IP6,
80 SAFI_UNICAST,
81 /* nexthop bytes */ 16,
82 /* Nexthop (global) */ 0xff,
83 0xfe,
84 0x1,
85 0x2,
86 0xaa,
87 0xbb,
88 0xcc,
89 0xdd,
90 0x3,
91 0x4,
92 0x5,
93 0x6,
94 0xa1,
95 0xa2,
96 0xa3,
97 0xa4,
98 /* SNPA (defunct, MBZ) */ 0x0,
99 /* NLRI tuples */ 32,
100 0xff,
101 0xfe,
102 0x1,
103 0x2, /* fffe:102::/32 */
104 },
105 (4 + 16 + 1 + 5),
106 SHOULD_PARSE,
107 AFI_IP6,
108 SAFI_UNICAST,
109 VALID_AFI,
110 },
111 {
112 "IPv6-2",
113 "IPV6 MP Reach, global nexthop, 2 NLRIs",
114 {
115 /* AFI / SAFI */ 0x0,
116 AFI_IP6,
117 SAFI_UNICAST,
118 /* nexthop bytes */ 16,
119 /* Nexthop (global) */ 0xff,
120 0xfe,
121 0x1,
122 0x2, /* ffee:102:... */
123 0xaa,
124 0xbb,
125 0xcc,
126 0xdd,
127 0x3,
128 0x4,
129 0x5,
130 0x6,
131 0xa1,
132 0xa2,
133 0xa3,
134 0xa4,
135 /* SNPA (defunct, MBZ) */ 0x0,
136 /* NLRI tuples */ 32,
137 0xff,
138 0xfe,
139 0x1,
140 0x2, /* fffe:102::/32 */
141 64,
142 0xff,
143 0xfe,
144 0x0,
145 0x1, /* fffe:1:2:3::/64 */
146 0x0,
147 0x2,
148 0x0,
149 0x3,
150 },
151 (4 + 16 + 1 + 5 + 9),
152 SHOULD_PARSE,
153 AFI_IP6,
154 SAFI_UNICAST,
155 VALID_AFI,
156 },
157 {
158 "IPv6-default",
159 "IPV6 MP Reach, global nexthop, 2 NLRIs + default",
160 {
161 /* AFI / SAFI */ 0x0,
162 AFI_IP6,
163 SAFI_UNICAST,
164 /* nexthop bytes */ 16,
165 /* Nexthop (global) */ 0xff,
166 0xfe,
167 0x1,
168 0x2,
169 0xaa,
170 0xbb,
171 0xcc,
172 0xdd,
173 0x3,
174 0x4,
175 0x5,
176 0x6,
177 0xa1,
178 0xa2,
179 0xa3,
180 0xa4,
181 /* SNPA (defunct, MBZ) */ 0x0,
182 /* NLRI tuples */ 32,
183 0xff,
184 0xfe,
185 0x1,
186 0x2, /* fffe:102::/32 */
187 64,
188 0xff,
189 0xfe,
190 0x0,
191 0x1, /* fffe:1:2:3::/64 */
192 0x0,
193 0x2,
194 0x0,
195 0x3,
196 0x0, /* ::/0 */
197 },
198 (4 + 16 + 1 + 5 + 9 + 1),
199 SHOULD_PARSE,
200 AFI_IP6,
201 SAFI_UNICAST,
202 VALID_AFI,
203 },
204 {
205 "IPv6-lnh",
206 "IPV6 MP Reach, global+local nexthops, 2 NLRIs + default",
207 {
208 /* AFI / SAFI */ 0x0,
209 AFI_IP6,
210 SAFI_UNICAST,
211 /* nexthop bytes */ 32,
212 /* Nexthop (global) */ 0xff,
213 0xfe,
214 0x1,
215 0x2, /* fffe:102:... */
216 0xaa,
217 0xbb,
218 0xcc,
219 0xdd,
220 0x3,
221 0x4,
222 0x5,
223 0x6,
224 0xa1,
225 0xa2,
226 0xa3,
227 0xa4,
228 /* Nexthop (local) */ 0xfe,
229 0x80,
230 0x0,
231 0x0, /* fe80::210:2ff:.. */
232 0x0,
233 0x0,
234 0x0,
235 0x0,
236 0x2,
237 0x10,
238 0x2,
239 0xff,
240 0x1,
241 0x2,
242 0x3,
243 0x4,
244 /* SNPA (defunct, MBZ) */ 0x0,
245 /* NLRI tuples */ 32,
246 0xff,
247 0xfe,
248 0x1,
249 0x2, /* fffe:102::/32 */
250 64,
251 0xff,
252 0xfe,
253 0x0,
254 0x1, /* fffe:1:2:3::/64 */
255 0x0,
256 0x2,
257 0x0,
258 0x3,
259 0x0, /* ::/0 */
260 },
261 (4 + 32 + 1 + 5 + 9 + 1),
262 SHOULD_PARSE,
263 AFI_IP6,
264 SAFI_UNICAST,
265 VALID_AFI,
266 },
267 {
268 "IPv6-nhlen",
269 "IPV6 MP Reach, inappropriate nexthop length",
270 {
271 /* AFI / SAFI */ 0x0,
272 AFI_IP6,
273 SAFI_UNICAST,
274 /* nexthop bytes */ 4,
275 /* Nexthop (global) */ 0xff,
276 0xfe,
277 0x1,
278 0x2, /* fffe:102:... */
279 0xaa,
280 0xbb,
281 0xcc,
282 0xdd,
283 0x3,
284 0x4,
285 0x5,
286 0x6,
287 0xa1,
288 0xa2,
289 0xa3,
290 0xa4,
291 /* Nexthop (local) */ 0xfe,
292 0x80,
293 0x0,
294 0x0, /* fe80::210:2ff:.. */
295 0x0,
296 0x0,
297 0x0,
298 0x0,
299 0x2,
300 0x10,
301 0x2,
302 0xff,
303 0x1,
304 0x2,
305 0x3,
306 0x4,
307 /* SNPA (defunct, MBZ) */ 0x0,
308 /* NLRI tuples */ 32,
309 0xff,
310 0xfe,
311 0x1,
312 0x2, /* fffe:102::/32 */
313 64,
314 0xff,
315 0xfe,
316 0x0,
317 0x1, /* fffe:1:2:3::/64 */
318 0x0,
319 0x2,
320 0x0,
321 0x3,
322 0x0, /* ::/0 */
323 },
324 (4 + 32 + 1 + 5 + 9 + 1),
325 SHOULD_ERR,
326 AFI_IP6,
327 SAFI_UNICAST,
328 VALID_AFI,
329 },
330 {
331 "IPv6-nhlen2",
332 "IPV6 MP Reach, invalid nexthop length",
333 {
334 /* AFI / SAFI */ 0x0,
335 AFI_IP6,
336 SAFI_UNICAST,
337 /* nexthop bytes */ 5,
338 /* Nexthop (global) */ 0xff,
339 0xfe,
340 0x1,
341 0x2, /* fffe:102:... */
342 0xaa,
343 0xbb,
344 0xcc,
345 0xdd,
346 0x3,
347 0x4,
348 0x5,
349 0x6,
350 0xa1,
351 0xa2,
352 0xa3,
353 0xa4,
354 /* Nexthop (local) */ 0xfe,
355 0x80,
356 0x0,
357 0x0, /* fe80::210:2ff:.. */
358 0x0,
359 0x0,
360 0x0,
361 0x0,
362 0x2,
363 0x10,
364 0x2,
365 0xff,
366 0x1,
367 0x2,
368 0x3,
369 0x4,
370 /* SNPA (defunct, MBZ) */ 0x0,
371 /* NLRI tuples */ 32,
372 0xff,
373 0xfe,
374 0x1,
375 0x2, /* fffe:102::/32 */
376 64,
377 0xff,
378 0xfe,
379 0x0,
380 0x1, /* fffe:1:2:3::/64 */
381 0x0,
382 0x2,
383 0x0,
384 0x3,
385 0x0, /* ::/0 */
386 },
387 (4 + 32 + 1 + 5 + 9 + 1),
388 SHOULD_ERR,
389 AFI_IP6,
390 SAFI_UNICAST,
391 VALID_AFI,
392 },
393 {
394 "IPv6-nhlen3",
395 "IPV6 MP Reach, nexthop length overflow",
396 {
397 /* AFI / SAFI */ 0x0,
398 AFI_IP6,
399 SAFI_UNICAST,
400 /* nexthop bytes */ 32,
401 /* Nexthop (global) */ 0xff,
402 0xfe,
403 0x1,
404 0x2, /* fffe:102:... */
405 0xaa,
406 0xbb,
407 0xcc,
408 0xdd,
409 0x3,
410 0x4,
411 0x5,
412 0x6,
413 0xa1,
414 0xa2,
415 0xa3,
416 0xa4,
417 },
418 (4 + 16),
419 SHOULD_ERR,
420 AFI_IP6,
421 SAFI_UNICAST,
422 VALID_AFI,
423 },
424 {
425 "IPv6-nhlen4",
426 "IPV6 MP Reach, nexthop length short",
427 {
428 /* AFI / SAFI */ 0x0,
429 AFI_IP6,
430 SAFI_UNICAST,
431 /* nexthop bytes */ 16,
432 /* Nexthop (global) */ 0xff,
433 0xfe,
434 0x1,
435 0x2, /* fffe:102:... */
436 0xaa,
437 0xbb,
438 0xcc,
439 0xdd,
440 0x3,
441 0x4,
442 0x5,
443 0x6,
444 0xa1,
445 0xa2,
446 0xa3,
447 0xa4,
448 /* Nexthop (local) */ 0xfe,
449 0x80,
450 0x0,
451 0x0, /* fe80::210:2ff:.. */
452 0x0,
453 0x0,
454 0x0,
455 0x0,
456 0x2,
457 0x10,
458 0x2,
459 0xff,
460 0x1,
461 0x2,
462 0x3,
463 0x4,
464 /* SNPA (defunct, MBZ) */ 0x0,
465 /* NLRI tuples */ 32,
466 0xff,
467 0xfe,
468 0x1,
469 0x2, /* fffe:102::/32 */
470 64,
471 0xff,
472 0xfe,
473 0x0,
474 0x1, /* fffe:1:2:3::/64 */
475 0x0,
476 0x2,
477 0x0,
478 0x3,
479 0x0, /* ::/0 */
480 },
481 (4 + 32 + 1 + 5 + 9 + 1),
482 SHOULD_ERR,
483 AFI_IP6,
484 SAFI_UNICAST,
485 VALID_AFI,
486 },
487 {
488 "IPv6-nlri",
489 "IPV6 MP Reach, NLRI bitlen overflow",
490 {
491 /* AFI / SAFI */ 0x0,
492 AFI_IP6,
493 SAFI_UNICAST,
494 /* nexthop bytes */ 32,
495 /* Nexthop (global) */ 0xff,
496 0xfe,
497 0x1,
498 0x2, /* fffe:102:... */
499 0xaa,
500 0xbb,
501 0xcc,
502 0xdd,
503 0x3,
504 0x4,
505 0x5,
506 0x6,
507 0xa1,
508 0xa2,
509 0xa3,
510 0xa4,
511 /* Nexthop (local) */ 0xfe,
512 0x80,
513 0x0,
514 0x0, /* fe80::210:2ff:.. */
515 0x0,
516 0x0,
517 0x0,
518 0x0,
519 0x2,
520 0x10,
521 0x2,
522 0xff,
523 0x1,
524 0x2,
525 0x3,
526 0x4,
527 /* SNPA (defunct, MBZ) */ 0x0,
528 /* NLRI tuples */ 120,
529 0xff,
530 0xfe,
531 0x1,
532 0x2, /* fffe:102::/32 */
533 64,
534 0xff,
535 0xfe,
536 0x0,
537 0x1, /* fffe:1:2:3::/64 */
538 0x0,
539 0x2,
540 0x0,
541 0x3,
542 0, /* ::/0 */
543 },
544 (4 + 32 + 1 + 5 + 9 + 1),
545 SHOULD_ERR,
546 AFI_IP6,
547 SAFI_UNICAST,
548 VALID_AFI,
549 },
550 {
551 "IPv4",
552 "IPv4 MP Reach, 2 NLRIs + default",
553 {
554 /* AFI / SAFI */ 0x0, AFI_IP, SAFI_UNICAST,
555 /* nexthop bytes */ 4,
556 /* Nexthop */ 192, 168, 0, 1,
557 /* SNPA (defunct, MBZ) */ 0x0,
558 /* NLRI tuples */ 16, 10, 1, /* 10.1/16 */
559 17, 10, 2, 3, /* 10.2.3/17 */
560 0, /* 0/0 */
561 },
562 (4 + 4 + 1 + 3 + 4 + 1),
563 SHOULD_PARSE,
564 AFI_IP,
565 SAFI_UNICAST,
566 VALID_AFI,
567 },
568 {
569 "IPv4-nhlen",
570 "IPv4 MP Reach, nexthop lenth overflow",
571 {
572 /* AFI / SAFI */ 0x0, AFI_IP, SAFI_UNICAST,
573 /* nexthop bytes */ 32,
574 /* Nexthop */ 192, 168, 0, 1,
575 /* SNPA (defunct, MBZ) */ 0x0,
576 /* NLRI tuples */ 16, 10, 1, /* 10.1/16 */
577 17, 10, 2, 3, /* 10.2.3/17 */
578 0, /* 0/0 */
579 },
580 (4 + 4 + 1 + 3 + 4 + 1),
581 SHOULD_ERR,
582 AFI_IP,
583 SAFI_UNICAST,
584 VALID_AFI,
585 },
586 {
587 "IPv4-nlrilen",
588 "IPv4 MP Reach, nlri lenth overflow",
589 {
590 /* AFI / SAFI */ 0x0, AFI_IP, SAFI_UNICAST,
591 /* nexthop bytes */ 4,
592 /* Nexthop */ 192, 168, 0, 1,
593 /* SNPA (defunct, MBZ) */ 0x0,
594 /* NLRI tuples */ 16, 10, 1, /* 10.1/16 */
595 30, 10, 0, /* 0/0 */
596 },
597 (4 + 4 + 1 + 3 + 2 + 1),
598 SHOULD_ERR,
599 AFI_IP,
600 SAFI_UNICAST,
601 VALID_AFI,
602 },
603 {
604 "IPv4-VPNv4",
605 "IPv4/VPNv4 MP Reach, RD, Nexthop, 2 NLRIs",
606 {
607 /* AFI / SAFI */ 0x0, AFI_IP, IANA_SAFI_MPLS_VPN,
608 /* nexthop bytes */ 12,
609 /* RD */ 0, 0, 0, 0, /* RD defined to be 0 */
610 0, 0, 0, 0,
611 /* Nexthop */ 192, 168, 0, 1,
612 /* SNPA (defunct, MBZ) */ 0x0,
613 /* NLRI tuples */ 88 + 16, 0, 1, 2, /* tag */
614 /* rd, 8 octets */
615 0, 0, /* RD_TYPE_AS */
616 0, 2, 0, 0xff, 3, 4, /* AS(2):val(4) */
617 10, 1, /* 10.1/16 */
618 88 + 17, 0xff, 0, 0, /* tag */
619 /* rd, 8 octets */
620 0, 0, /* RD_TYPE_IP */
621 192, 168, 0, 1, /* IPv4 */
622 10, 2, 3, /* 10.2.3/17 */
623 },
624 (4 + 12 + 1 + (1 + 3 + 8 + 2) + (1 + 3 + 8 + 3)),
625 SHOULD_PARSE,
626 AFI_IP,
627 IANA_SAFI_MPLS_VPN,
628 VALID_AFI,
629 },
630 {
631 "IPv4-VPNv4-bogus-plen",
632 "IPv4/MPLS-labeled VPN MP Reach, RD, Nexthop, NLRI / bogus p'len",
633 {
634 /* AFI / SAFI */ 0x0,
635 AFI_IP,
636 IANA_SAFI_MPLS_VPN,
637 /* nexthop bytes */ 12,
638 /* RD */ 0,
639 0,
640 1,
641 2,
642 0,
643 0xff,
644 3,
645 4,
646 /* Nexthop */ 192,
647 168,
648 0,
649 1,
650 /* SNPA (defunct, MBZ) */ 0x0,
651 /* NLRI tuples */ 16,
652 10,
653 1, /* 10.1/16 */
654 17,
655 10,
656 2,
657 3, /* 10.2.3/17 */
658 0, /* 0/0 */
659 },
660 (3 + 1 + 3 * 4 + 1 + 3 + 4 + 1),
661 SHOULD_ERR,
662 AFI_IP,
663 IANA_SAFI_MPLS_VPN,
664 VALID_AFI,
665 },
666 {
667 "IPv4-VPNv4-plen1-short",
668 "IPv4/VPNv4 MP Reach, RD, Nexthop, 2 NLRIs, 1st plen short",
669 {
670 /* AFI / SAFI */ 0x0, AFI_IP, IANA_SAFI_MPLS_VPN,
671 /* nexthop bytes */ 12,
672 /* RD */ 0, 0, 0, 0, /* RD defined to be 0 */
673 0, 0, 0, 0,
674 /* Nexthop */ 192, 168, 0, 1,
675 /* SNPA (defunct, MBZ) */ 0x0,
676 /* NLRI tuples */ 88 + 1, 0, 1, 2, /* tag */
677 /* rd, 8 octets */
678 0, 0, /* RD_TYPE_AS */
679 0, 2, 0, 0xff, 3, 4, /* AS(2):val(4) */
680 10, 1, /* 10.1/16 */
681 88 + 17, 0xff, 0, 0, /* tag */
682 /* rd, 8 octets */
683 0, 0, /* RD_TYPE_IP */
684 192, 168, 0, 1, /* IPv4 */
685 10, 2, 3, /* 10.2.3/17 */
686 },
687 (4 + 12 + 1 + (1 + 3 + 8 + 2) + (1 + 3 + 8 + 3)),
688 SHOULD_ERR,
689 AFI_IP,
690 IANA_SAFI_MPLS_VPN,
691 VALID_AFI,
692 },
693 {
694 "IPv4-VPNv4-plen1-long",
695 "IPv4/VPNv4 MP Reach, RD, Nexthop, 2 NLRIs, 1st plen long",
696 {
697 /* AFI / SAFI */ 0x0, AFI_IP, IANA_SAFI_MPLS_VPN,
698 /* nexthop bytes */ 12,
699 /* RD */ 0, 0, 0, 0, /* RD defined to be 0 */
700 0, 0, 0, 0,
701 /* Nexthop */ 192, 168, 0, 1,
702 /* SNPA (defunct, MBZ) */ 0x0,
703 /* NLRI tuples */ 88 + 32, 0, 1, 2, /* tag */
704 /* rd, 8 octets */
705 0, 0, /* RD_TYPE_AS */
706 0, 2, 0, 0xff, 3, 4, /* AS(2):val(4) */
707 10, 1, /* 10.1/16 */
708 88 + 17, 0xff, 0, 0, /* tag */
709 /* rd, 8 octets */
710 0, 0, /* RD_TYPE_IP */
711 192, 168, 0, 1, /* IPv4 */
712 10, 2, 3, /* 10.2.3/17 */
713 },
714 (4 + 12 + 1 + (1 + 3 + 8 + 2) + (1 + 3 + 8 + 3)),
715 SHOULD_ERR,
716 AFI_IP,
717 IANA_SAFI_MPLS_VPN,
718 VALID_AFI,
719 },
720 {
721 "IPv4-VPNv4-plenn-long",
722 "IPv4/VPNv4 MP Reach, RD, Nexthop, 3 NLRIs, last plen long",
723 {
724 /* AFI / SAFI */ 0x0, AFI_IP, IANA_SAFI_MPLS_VPN,
725 /* nexthop bytes */ 12,
726 /* RD */ 0, 0, 0, 0, /* RD defined to be 0 */
727 0, 0, 0, 0,
728 /* Nexthop */ 192, 168, 0, 1,
729 /* SNPA (defunct, MBZ) */ 0x0,
730 /* NLRI tuples */ 88 + 16, 0, 1, 2, /* tag */
731 /* rd, 8 octets */
732 0, 0, /* RD_TYPE_AS */
733 0, 2, 0, 0xff, 3, 4, /* AS(2):val(4) */
734 10, 1, /* 10.1/16 */
735 88 + 17, 0xff, 0, 0, /* tag */
736 /* rd, 8 octets */
737 0, 0, /* RD_TYPE_IP */
738 192, 168, 0, 1, /* IPv4 */
739 10, 2, 3, /* 10.2.3/17 */
740 88 + 1, /* bogus */
741 },
742 (4 + 12 + 1 + (1 + 3 + 8 + 2) + (1 + 3 + 8 + 3) + 1),
743 SHOULD_ERR,
744 AFI_IP,
745 IANA_SAFI_MPLS_VPN,
746 VALID_AFI,
747 },
748 {
749 "IPv4-VPNv4-plenn-short",
750 "IPv4/VPNv4 MP Reach, RD, Nexthop, 2 NLRIs, last plen short",
751 {
752 /* AFI / SAFI */ 0x0, AFI_IP, IANA_SAFI_MPLS_VPN,
753 /* nexthop bytes */ 12,
754 /* RD */ 0, 0, 0, 0, /* RD defined to be 0 */
755 0, 0, 0, 0,
756 /* Nexthop */ 192, 168, 0, 1,
757 /* SNPA (defunct, MBZ) */ 0x0,
758 /* NLRI tuples */ 88 + 16, 0, 1, 2, /* tag */
759 /* rd, 8 octets */
760 0, 0, /* RD_TYPE_AS */
761 0, 2, 0, 0xff, 3, 4, /* AS(2):val(4) */
762 10, 1, /* 10.1/16 */
763 88 + 2, 0xff, 0, 0, /* tag */
764 /* rd, 8 octets */
765 0, 0, /* RD_TYPE_IP */
766 192, 168, 0, 1, /* IPv4 */
767 10, 2, 3, /* 10.2.3/17 */
768 },
769 (4 + 12 + 1 + (1 + 3 + 8 + 2) + (1 + 3 + 8 + 3)),
770 SHOULD_ERR,
771 AFI_IP,
772 IANA_SAFI_MPLS_VPN,
773 VALID_AFI,
774 },
775 {
776 "IPv4-VPNv4-bogus-rd-type",
777 "IPv4/VPNv4 MP Reach, RD, NH, 2 NLRI, unknown RD in 1st (log, but parse)",
778 {
779 /* AFI / SAFI */ 0x0, AFI_IP, IANA_SAFI_MPLS_VPN,
780 /* nexthop bytes */ 12,
781 /* RD */ 0, 0, 0, 0, /* RD defined to be 0 */
782 0, 0, 0, 0,
783 /* Nexthop */ 192, 168, 0, 1,
784 /* SNPA (defunct, MBZ) */ 0x0,
785 /* NLRI tuples */ 88 + 16, 0, 1, 2, /* tag */
786 /* rd, 8 octets */
787 0xff, 0, /* Bogus RD */
788 0, 2, 0, 0xff, 3, 4, /* AS(2):val(4) */
789 10, 1, /* 10.1/16 */
790 88 + 17, 0xff, 0, 0, /* tag */
791 /* rd, 8 octets */
792 0, 0, /* RD_TYPE_IP */
793 192, 168, 0, 1, /* IPv4 */
794 10, 2, 3, /* 10.2.3/17 */
795 },
796 (4 + 12 + 1 + (1 + 3 + 8 + 2) + (1 + 3 + 8 + 3)),
797 SHOULD_PARSE,
798 AFI_IP,
799 IANA_SAFI_MPLS_VPN,
800 VALID_AFI,
801 },
802 {
803 "IPv4-VPNv4-0-nlri",
804 "IPv4/VPNv4 MP Reach, RD, Nexthop, 3 NLRI, 3rd 0 bogus",
805 {
806 /* AFI / SAFI */ 0x0, AFI_IP, IANA_SAFI_MPLS_VPN,
807 /* nexthop bytes */ 12,
808 /* RD */ 0, 0, 0, 0, /* RD defined to be 0 */
809 0, 0, 0, 0,
810 /* Nexthop */ 192, 168, 0, 1,
811 /* SNPA (defunct, MBZ) */ 0x0,
812 /* NLRI tuples */ 88 + 16, 0, 1, 2, /* tag */
813 /* rd, 8 octets */
814 0, 0, /* RD_TYPE_AS */
815 0, 2, 0, 0xff, 3, 4, /* AS(2):val(4) */
816 10, 1, /* 10.1/16 */
817 88 + 17, 0xff, 0, 0, /* tag */
818 /* rd, 8 octets */
819 0, 0, /* RD_TYPE_IP */
820 192, 168, 0, 1, /* IPv4 */
821 10, 2, 3, /* 10.2.3/17 */
822 0 /* 0/0, bogus for vpnv4 ?? */
823 },
824 (4 + 12 + 1 + (1 + 3 + 8 + 2) + (1 + 3 + 8 + 3) + 1),
825 SHOULD_ERR,
826 AFI_IP,
827 IANA_SAFI_MPLS_VPN,
828 VALID_AFI,
829 },
830
831 /* From bug #385 */
832 {
833 "IPv6-bug",
834 "IPv6, global nexthop, 1 default NLRI",
835 {
836 /* AFI / SAFI */ 0x0,
837 0x2,
838 0x1,
839 /* nexthop bytes */ 0x20,
840 /* Nexthop (global) */ 0x20,
841 0x01,
842 0x04,
843 0x70,
844 0x00,
845 0x01,
846 0x00,
847 0x06,
848 0x00,
849 0x00,
850 0x00,
851 0x00,
852 0x00,
853 0x00,
854 0x00,
855 0x01,
856 /* Nexthop (local) */ 0xfe,
857 0x80,
858 0x00,
859 0x00,
860 0x00,
861 0x00,
862 0x00,
863 0x00,
864 0x02,
865 0x0c,
866 0xdb,
867 0xff,
868 0xfe,
869 0xfe,
870 0xeb,
871 0x00,
872 /* SNPA (defunct, MBZ) */ 0,
873 /* NLRI tuples */ /* Should have 0 here for ::/0, but
874 dont */
875 },
876 37,
877 SHOULD_ERR,
878 AFI_IP6,
879 SAFI_UNICAST,
880 VALID_AFI,
881 },
882
883 {NULL, NULL, {0}, 0, 0}};
1eab26bb
PJ
884
885/* MP_UNREACH_NLRI tests */
ac4d0be5 886static struct test_segment mp_unreach_segments[] = {
887 {
888 "IPv6-unreach",
889 "IPV6 MP Unreach, 1 NLRI",
890 {
891 /* AFI / SAFI */ 0x0, AFI_IP6, SAFI_UNICAST,
892 /* NLRI tuples */ 32, 0xff, 0xfe, 0x1,
893 0x2, /* fffe:102::/32 */
894 },
895 (3 + 5),
896 SHOULD_PARSE,
897 AFI_IP6,
898 SAFI_UNICAST,
899 VALID_AFI,
900 },
901 {
902 "IPv6-unreach2",
903 "IPV6 MP Unreach, 2 NLRIs",
904 {
905 /* AFI / SAFI */ 0x0, AFI_IP6, SAFI_UNICAST,
906 /* NLRI tuples */ 32, 0xff, 0xfe, 0x1,
907 0x2, /* fffe:102::/32 */
908 64, 0xff, 0xfe, 0x0, 0x1, /* fffe:1:2:3::/64 */
909 0x0, 0x2, 0x0, 0x3,
910 },
911 (3 + 5 + 9),
912 SHOULD_PARSE,
913 AFI_IP6,
914 SAFI_UNICAST,
915 VALID_AFI,
916 },
917 {
918 "IPv6-unreach-default",
919 "IPV6 MP Unreach, 2 NLRIs + default",
920 {
921 /* AFI / SAFI */ 0x0, AFI_IP6, SAFI_UNICAST,
922 /* NLRI tuples */ 32, 0xff, 0xfe, 0x1,
923 0x2, /* fffe:102::/32 */
924 64, 0xff, 0xfe, 0x0, 0x1, /* fffe:1:2:3::/64 */
925 0x0, 0x2, 0x0, 0x3, 0x0, /* ::/0 */
926 },
927 (3 + 5 + 9 + 1),
928 SHOULD_PARSE,
929 AFI_IP6,
930 SAFI_UNICAST,
931 VALID_AFI,
932 },
933 {
934 "IPv6-unreach-nlri",
935 "IPV6 MP Unreach, NLRI bitlen overflow",
936 {
937 /* AFI / SAFI */ 0x0, AFI_IP6, SAFI_UNICAST,
938 /* NLRI tuples */ 120, 0xff, 0xfe, 0x1,
939 0x2, /* fffe:102::/32 */
940 64, 0xff, 0xfe, 0x0, 0x1, /* fffe:1:2:3::/64 */
941 0x0, 0x2, 0x0, 0x3, 0, /* ::/0 */
942 },
943 (3 + 5 + 9 + 1),
944 SHOULD_ERR,
945 AFI_IP6,
946 SAFI_UNICAST,
947 VALID_AFI,
948 },
949 {
950 "IPv4-unreach",
951 "IPv4 MP Unreach, 2 NLRIs + default",
952 {
953 /* AFI / SAFI */ 0x0, AFI_IP, SAFI_UNICAST,
954 /* NLRI tuples */ 16, 10, 1, /* 10.1/16 */
955 17, 10, 2, 3, /* 10.2.3/17 */
956 0, /* 0/0 */
957 },
958 (3 + 3 + 4 + 1),
959 SHOULD_PARSE,
960 AFI_IP,
961 SAFI_UNICAST,
962 VALID_AFI,
963 },
964 {
965 "IPv4-unreach-nlrilen",
966 "IPv4 MP Unreach, nlri length overflow",
967 {
968 /* AFI / SAFI */ 0x0, AFI_IP, SAFI_UNICAST,
969 /* NLRI tuples */ 16, 10, 1, /* 10.1/16 */
970 30, 10, 0, /* 0/0 */
971 },
972 (3 + 3 + 2 + 1),
973 SHOULD_ERR,
974 AFI_IP,
975 SAFI_UNICAST,
976 VALID_AFI,
977 },
978 {
979 "IPv4-unreach-VPNv4",
980 "IPv4/MPLS-labeled VPN MP Unreach, RD, 3 NLRIs",
981 {
982 /* AFI / SAFI */ 0x0, AFI_IP, IANA_SAFI_MPLS_VPN,
983 /* NLRI tuples */ 88 + 16, 0, 1, 2, /* tag */
984 /* rd, 8 octets */
985 0, 0, /* RD_TYPE_AS */
986 0, 2, 0, 0xff, 3, 4, /* AS(2):val(4) */
987 10, 1, /* 10.1/16 */
988 88 + 17, 0xff, 0, 0, /* tag */
989 /* rd, 8 octets */
990 0, 0, /* RD_TYPE_IP */
991 192, 168, 0, 1, /* IPv4 */
992 10, 2, 3, /* 10.2.3/17 */
993 },
994 (3 + (1 + 3 + 8 + 2) + (1 + 3 + 8 + 3)),
995 SHOULD_PARSE,
996 AFI_IP,
997 IANA_SAFI_MPLS_VPN,
998 VALID_AFI,
999 },
1000 {NULL, NULL, {0}, 0, 0}};
1eab26bb 1001
48a5452b
PJ
1002/* nlri_parse indicates 0 on successful parse, and -1 otherwise.
1003 * attr_parse indicates BGP_ATTR_PARSE_PROCEED/0 on success,
1004 * and BGP_ATTR_PARSE_ERROR/-1 or lower negative ret on err.
1005 */
ac4d0be5 1006static void handle_result(struct peer *peer, struct test_segment *t,
1007 int parse_ret, int nlri_ret)
48a5452b 1008{
ac4d0be5 1009 int oldfailed = failed;
1010
1011 printf("mp attr parsed?: %s\n", parse_ret ? "no" : "yes");
1012 if (!parse_ret)
1013 printf("nrli parsed?: %s\n", nlri_ret ? "no" : "yes");
1014 printf("should parse?: %s\n", t->parses ? "no" : "yes");
1015
1016 if ((parse_ret != 0 || nlri_ret != 0) != (t->parses != 0))
1017 failed++;
1018
1019
1020 if (tty)
1021 printf("%s",
1022 (failed > oldfailed) ? VT100_RED "failed!" VT100_RESET
1023 : VT100_GREEN "OK" VT100_RESET);
1024 else
1025 printf("%s", (failed > oldfailed) ? "failed!" : "OK");
1026
1027 if (failed)
1028 printf(" (%u)", failed);
1029
1030 printf("\n\n");
48a5452b 1031}
1eab26bb
PJ
1032
1033/* basic parsing test */
ac4d0be5 1034static void parse_test(struct peer *peer, struct test_segment *t, int type)
1eab26bb 1035{
ac4d0be5 1036 int parse_ret = 0, nlri_ret = 0;
1037 struct attr attr = {};
1038 struct bgp_nlri nlri = {};
1039 struct bgp_attr_parser_args attr_args = {
1040 .peer = peer,
1041 .length = t->len,
1042 .total = 1,
1043 .attr = &attr,
1044 .type = type,
1045 .flags = BGP_ATTR_FLAG_OPTIONAL,
1046 .startp = BGP_INPUT_PNT(peer),
1047 };
1eab26bb 1048#define RANDOM_FUZZ 35
ac4d0be5 1049 stream_reset(peer->ibuf);
1050 stream_put(peer->ibuf, NULL, RANDOM_FUZZ);
1051 stream_set_getp(peer->ibuf, RANDOM_FUZZ);
1052
1053 stream_write(peer->ibuf, t->data, t->len);
1054
1055 printf("%s: %s\n", t->name, t->desc);
1056
1057 if (type == BGP_ATTR_MP_REACH_NLRI)
1058 parse_ret = bgp_mp_reach_parse(&attr_args, &nlri);
1059 else
1060 parse_ret = bgp_mp_unreach_parse(&attr_args, &nlri);
1061 if (!parse_ret) {
1062 iana_afi_t pkt_afi;
1063 safi_t pkt_safi;
1064
1065 /* Convert AFI, SAFI to internal values, check. */
1066 if (bgp_map_afi_safi_int2iana(nlri.afi, nlri.safi, &pkt_afi,
1067 &pkt_safi))
1068 assert(0);
1069
1070 printf("MP: %u(%u)/%u(%u): recv %u, nego %u\n", nlri.afi,
1071 pkt_afi, nlri.safi, pkt_safi,
1072 peer->afc_recv[nlri.afi][nlri.safi],
1073 peer->afc_nego[nlri.afi][nlri.safi]);
1074 }
1075
1076 if (!parse_ret) {
1077 if (type == BGP_ATTR_MP_REACH_NLRI)
1078 nlri_ret = bgp_nlri_parse(peer, &attr, &nlri, 0);
1079 else
1080 nlri_ret = bgp_nlri_parse(peer, &attr, &nlri, 1);
1081 }
1082 handle_result(peer, t, parse_ret, nlri_ret);
1eab26bb
PJ
1083}
1084
1085static struct bgp *bgp;
1086static as_t asn = 100;
1087
ac4d0be5 1088int main(void)
1eab26bb 1089{
ac4d0be5 1090 struct peer *peer;
1091 int i, j;
1092
1093 conf_bgp_debug_neighbor_events = -1UL;
1094 conf_bgp_debug_packet = -1UL;
1095 conf_bgp_debug_as4 = -1UL;
1096 term_bgp_debug_neighbor_events = -1UL;
1097 term_bgp_debug_packet = -1UL;
1098 term_bgp_debug_as4 = -1UL;
1099
1100 qobj_init();
1101 master = thread_master_create();
1102 bgp_master_init(master);
1103 vrf_init();
1104 bgp_option_set(BGP_OPT_NO_LISTEN);
1105 bgp_attr_init();
1106
1107 if (fileno(stdout) >= 0)
1108 tty = isatty(fileno(stdout));
1109
1110 if (bgp_get(&bgp, &asn, NULL, BGP_INSTANCE_TYPE_DEFAULT))
1111 return -1;
1112
1113 peer = peer_create_accept(bgp);
1114 peer->host = (char *)"foo";
1115 peer->status = Established;
1116
1117 for (i = AFI_IP; i < AFI_MAX; i++)
1118 for (j = SAFI_UNICAST; j < SAFI_MAX; j++) {
1119 peer->afc[i][j] = 1;
1120 peer->afc_adv[i][j] = 1;
1121 }
1122
1123 i = 0;
1124 while (mp_reach_segments[i].name)
1125 parse_test(peer, &mp_reach_segments[i++],
1126 BGP_ATTR_MP_REACH_NLRI);
1127
1128 i = 0;
1129 while (mp_unreach_segments[i].name)
1130 parse_test(peer, &mp_unreach_segments[i++],
1131 BGP_ATTR_MP_UNREACH_NLRI);
1132
1133 printf("failures: %d\n", failed);
1134 return failed;
1eab26bb 1135}