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