]> git.proxmox.com Git - mirror_frr.git/blob - tests/bgpd/test_mp_attr.c
*: add frr_init() infrastructure
[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
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
19 * 02111-1307, USA.
20 */
21
22 #include <zebra.h>
23
24 #include "qobj.h"
25 #include "vty.h"
26 #include "stream.h"
27 #include "privs.h"
28 #include "memory.h"
29 #include "queue.h"
30 #include "filter.h"
31
32 #include "bgpd/bgpd.h"
33 #include "bgpd/bgp_attr.h"
34 #include "bgpd/bgp_open.h"
35 #include "bgpd/bgp_debug.h"
36 #include "bgpd/bgp_route.h"
37 #include "bgpd/bgp_packet.h"
38 #include "bgpd/bgp_mplsvpn.h"
39 #include "bgpd/bgp_nexthop.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
47 #define CAPABILITY 0
48 #define DYNCAP 1
49 #define OPT_PARAM 2
50
51 /* need these to link in libbgp */
52 struct zebra_privs_t *bgpd_privs = NULL;
53 struct thread_master *master = NULL;
54
55 static int failed = 0;
56 static int tty = 0;
57
58 /* test segments to parse and validate, and use for other tests */
59 static struct test_segment {
60 const char *name;
61 const char *desc;
62 const u_char data[1024];
63 int len;
64 #define SHOULD_PARSE 0
65 #define SHOULD_ERR -1
66 int parses; /* whether it should parse or not */
67
68 /* AFI/SAFI validation */
69 afi_t afi;
70 safi_t safi;
71 #define VALID_AFI 1
72 #define INVALID_AFI 0
73 int afi_valid;
74 } mp_reach_segments [] =
75 {
76 { "IPv6",
77 "IPV6 MP Reach, global nexthop, 1 NLRI",
78 {
79 /* AFI / SAFI */ 0x0, AFI_IP6, SAFI_UNICAST,
80 /* nexthop bytes */ 16,
81 /* Nexthop (global) */ 0xff, 0xfe, 0x1, 0x2,
82 0xaa, 0xbb, 0xcc, 0xdd,
83 0x3, 0x4, 0x5, 0x6,
84 0xa1, 0xa2, 0xa3, 0xa4,
85 /* SNPA (defunct, MBZ) */ 0x0,
86 /* NLRI tuples */ 32, 0xff, 0xfe, 0x1, 0x2, /* fffe:102::/32 */
87 },
88 (4 + 16 + 1 + 5),
89 SHOULD_PARSE,
90 AFI_IP6, SAFI_UNICAST, VALID_AFI,
91 },
92 { "IPv6-2",
93 "IPV6 MP Reach, global nexthop, 2 NLRIs",
94 {
95 /* AFI / SAFI */ 0x0, AFI_IP6, SAFI_UNICAST,
96 /* nexthop bytes */ 16,
97 /* Nexthop (global) */ 0xff, 0xfe, 0x1, 0x2, /* ffee:102:... */
98 0xaa, 0xbb, 0xcc, 0xdd,
99 0x3, 0x4, 0x5, 0x6,
100 0xa1, 0xa2, 0xa3, 0xa4,
101 /* SNPA (defunct, MBZ) */ 0x0,
102 /* NLRI tuples */ 32,
103 0xff, 0xfe, 0x1, 0x2, /* fffe:102::/32 */
104 64,
105 0xff, 0xfe, 0x0, 0x1, /* fffe:1:2:3::/64 */
106 0x0, 0x2, 0x0, 0x3,
107 },
108 (4 + 16 + 1 + 5 + 9),
109 SHOULD_PARSE,
110 AFI_IP6, SAFI_UNICAST, VALID_AFI,
111 },
112 { "IPv6-default",
113 "IPV6 MP Reach, global nexthop, 2 NLRIs + default",
114 {
115 /* AFI / SAFI */ 0x0, AFI_IP6, SAFI_UNICAST,
116 /* nexthop bytes */ 16,
117 /* Nexthop (global) */ 0xff, 0xfe, 0x1, 0x2,
118 0xaa, 0xbb, 0xcc, 0xdd,
119 0x3, 0x4, 0x5, 0x6,
120 0xa1, 0xa2, 0xa3, 0xa4,
121 /* SNPA (defunct, MBZ) */ 0x0,
122 /* NLRI tuples */ 32,
123 0xff, 0xfe, 0x1, 0x2, /* fffe:102::/32 */
124 64,
125 0xff, 0xfe, 0x0, 0x1, /* fffe:1:2:3::/64 */
126 0x0, 0x2, 0x0, 0x3,
127 0x0, /* ::/0 */
128 },
129 (4 + 16 + 1 + 5 + 9 + 1),
130 SHOULD_PARSE,
131 AFI_IP6, SAFI_UNICAST, VALID_AFI,
132 },
133 { "IPv6-lnh",
134 "IPV6 MP Reach, global+local nexthops, 2 NLRIs + default",
135 {
136 /* AFI / SAFI */ 0x0, AFI_IP6, SAFI_UNICAST,
137 /* nexthop bytes */ 32,
138 /* Nexthop (global) */ 0xff, 0xfe, 0x1, 0x2, /* fffe:102:... */
139 0xaa, 0xbb, 0xcc, 0xdd,
140 0x3, 0x4, 0x5, 0x6,
141 0xa1, 0xa2, 0xa3, 0xa4,
142 /* Nexthop (local) */ 0xfe, 0x80, 0x0, 0x0, /* fe80::210:2ff:.. */
143 0x0, 0x0, 0x0, 0x0,
144 0x2, 0x10, 0x2, 0xff,
145 0x1, 0x2, 0x3, 0x4,
146 /* SNPA (defunct, MBZ) */ 0x0,
147 /* NLRI tuples */ 32,
148 0xff, 0xfe, 0x1, 0x2, /* fffe:102::/32 */
149 64,
150 0xff, 0xfe, 0x0, 0x1, /* fffe:1:2:3::/64 */
151 0x0, 0x2, 0x0, 0x3,
152 0x0, /* ::/0 */
153 },
154 (4 + 32 + 1 + 5 + 9 + 1),
155 SHOULD_PARSE,
156 AFI_IP6, SAFI_UNICAST, VALID_AFI,
157 },
158 { "IPv6-nhlen",
159 "IPV6 MP Reach, inappropriate nexthop length",
160 {
161 /* AFI / SAFI */ 0x0, AFI_IP6, SAFI_UNICAST,
162 /* nexthop bytes */ 4,
163 /* Nexthop (global) */ 0xff, 0xfe, 0x1, 0x2, /* fffe:102:... */
164 0xaa, 0xbb, 0xcc, 0xdd,
165 0x3, 0x4, 0x5, 0x6,
166 0xa1, 0xa2, 0xa3, 0xa4,
167 /* Nexthop (local) */ 0xfe, 0x80, 0x0, 0x0, /* fe80::210:2ff:.. */
168 0x0, 0x0, 0x0, 0x0,
169 0x2, 0x10, 0x2, 0xff,
170 0x1, 0x2, 0x3, 0x4,
171 /* SNPA (defunct, MBZ) */ 0x0,
172 /* NLRI tuples */ 32,
173 0xff, 0xfe, 0x1, 0x2, /* fffe:102::/32 */
174 64,
175 0xff, 0xfe, 0x0, 0x1, /* fffe:1:2:3::/64 */
176 0x0, 0x2, 0x0, 0x3,
177 0x0, /* ::/0 */
178 },
179 (4 + 32 + 1 + 5 + 9 + 1),
180 SHOULD_ERR,
181 AFI_IP6, SAFI_UNICAST, VALID_AFI,
182 },
183 { "IPv6-nhlen2",
184 "IPV6 MP Reach, invalid nexthop length",
185 {
186 /* AFI / SAFI */ 0x0, AFI_IP6, SAFI_UNICAST,
187 /* nexthop bytes */ 5,
188 /* Nexthop (global) */ 0xff, 0xfe, 0x1, 0x2, /* fffe:102:... */
189 0xaa, 0xbb, 0xcc, 0xdd,
190 0x3, 0x4, 0x5, 0x6,
191 0xa1, 0xa2, 0xa3, 0xa4,
192 /* Nexthop (local) */ 0xfe, 0x80, 0x0, 0x0, /* fe80::210:2ff:.. */
193 0x0, 0x0, 0x0, 0x0,
194 0x2, 0x10, 0x2, 0xff,
195 0x1, 0x2, 0x3, 0x4,
196 /* SNPA (defunct, MBZ) */ 0x0,
197 /* NLRI tuples */ 32,
198 0xff, 0xfe, 0x1, 0x2, /* fffe:102::/32 */
199 64,
200 0xff, 0xfe, 0x0, 0x1, /* fffe:1:2:3::/64 */
201 0x0, 0x2, 0x0, 0x3,
202 0x0, /* ::/0 */
203 },
204 (4 + 32 + 1 + 5 + 9 + 1),
205 SHOULD_ERR,
206 AFI_IP6, SAFI_UNICAST, VALID_AFI,
207 },
208 { "IPv6-nhlen3",
209 "IPV6 MP Reach, nexthop length overflow",
210 {
211 /* AFI / SAFI */ 0x0, AFI_IP6, SAFI_UNICAST,
212 /* nexthop bytes */ 32,
213 /* Nexthop (global) */ 0xff, 0xfe, 0x1, 0x2, /* fffe:102:... */
214 0xaa, 0xbb, 0xcc, 0xdd,
215 0x3, 0x4, 0x5, 0x6,
216 0xa1, 0xa2, 0xa3, 0xa4,
217 },
218 (4 + 16),
219 SHOULD_ERR,
220 AFI_IP6, SAFI_UNICAST, VALID_AFI,
221 },
222 { "IPv6-nhlen4",
223 "IPV6 MP Reach, nexthop length short",
224 {
225 /* AFI / SAFI */ 0x0, AFI_IP6, SAFI_UNICAST,
226 /* nexthop bytes */ 16,
227 /* Nexthop (global) */ 0xff, 0xfe, 0x1, 0x2, /* fffe:102:... */
228 0xaa, 0xbb, 0xcc, 0xdd,
229 0x3, 0x4, 0x5, 0x6,
230 0xa1, 0xa2, 0xa3, 0xa4,
231 /* Nexthop (local) */ 0xfe, 0x80, 0x0, 0x0, /* fe80::210:2ff:.. */
232 0x0, 0x0, 0x0, 0x0,
233 0x2, 0x10, 0x2, 0xff,
234 0x1, 0x2, 0x3, 0x4,
235 /* SNPA (defunct, MBZ) */ 0x0,
236 /* NLRI tuples */ 32,
237 0xff, 0xfe, 0x1, 0x2, /* fffe:102::/32 */
238 64,
239 0xff, 0xfe, 0x0, 0x1, /* fffe:1:2:3::/64 */
240 0x0, 0x2, 0x0, 0x3,
241 0x0, /* ::/0 */
242 },
243 (4 + 32 + 1 + 5 + 9 + 1),
244 SHOULD_ERR,
245 AFI_IP6, SAFI_UNICAST, VALID_AFI,
246 },
247 { "IPv6-nlri",
248 "IPV6 MP Reach, NLRI bitlen overflow",
249 {
250 /* AFI / SAFI */ 0x0, AFI_IP6, SAFI_UNICAST,
251 /* nexthop bytes */ 32,
252 /* Nexthop (global) */ 0xff, 0xfe, 0x1, 0x2, /* fffe:102:... */
253 0xaa, 0xbb, 0xcc, 0xdd,
254 0x3, 0x4, 0x5, 0x6,
255 0xa1, 0xa2, 0xa3, 0xa4,
256 /* Nexthop (local) */ 0xfe, 0x80, 0x0, 0x0, /* fe80::210:2ff:.. */
257 0x0, 0x0, 0x0, 0x0,
258 0x2, 0x10, 0x2, 0xff,
259 0x1, 0x2, 0x3, 0x4,
260 /* SNPA (defunct, MBZ) */ 0x0,
261 /* NLRI tuples */ 120,
262 0xff, 0xfe, 0x1, 0x2, /* fffe:102::/32 */
263 64,
264 0xff, 0xfe, 0x0, 0x1, /* fffe:1:2:3::/64 */
265 0x0, 0x2, 0x0, 0x3,
266 0, /* ::/0 */
267 },
268 (4 + 32 + 1 + 5 + 9 + 1),
269 SHOULD_ERR,
270 AFI_IP6, SAFI_UNICAST, VALID_AFI,
271 },
272 { "IPv4",
273 "IPv4 MP Reach, 2 NLRIs + default",
274 {
275 /* AFI / SAFI */ 0x0, AFI_IP, SAFI_UNICAST,
276 /* nexthop bytes */ 4,
277 /* Nexthop */ 192, 168, 0, 1,
278 /* SNPA (defunct, MBZ) */ 0x0,
279 /* NLRI tuples */ 16, 10, 1, /* 10.1/16 */
280 17, 10, 2, 3, /* 10.2.3/17 */
281 0, /* 0/0 */
282 },
283 (4 + 4 + 1 + 3 + 4 + 1),
284 SHOULD_PARSE,
285 AFI_IP, SAFI_UNICAST, VALID_AFI,
286 },
287 { "IPv4-nhlen",
288 "IPv4 MP Reach, nexthop lenth overflow",
289 {
290 /* AFI / SAFI */ 0x0, AFI_IP, SAFI_UNICAST,
291 /* nexthop bytes */ 32,
292 /* Nexthop */ 192, 168, 0, 1,
293 /* SNPA (defunct, MBZ) */ 0x0,
294 /* NLRI tuples */ 16, 10, 1, /* 10.1/16 */
295 17, 10, 2, 3, /* 10.2.3/17 */
296 0, /* 0/0 */
297 },
298 (4 + 4 + 1 + 3 + 4 + 1),
299 SHOULD_ERR,
300 AFI_IP, SAFI_UNICAST, VALID_AFI,
301 },
302 { "IPv4-nlrilen",
303 "IPv4 MP Reach, nlri lenth overflow",
304 {
305 /* AFI / SAFI */ 0x0, AFI_IP, SAFI_UNICAST,
306 /* nexthop bytes */ 4,
307 /* Nexthop */ 192, 168, 0, 1,
308 /* SNPA (defunct, MBZ) */ 0x0,
309 /* NLRI tuples */ 16, 10, 1, /* 10.1/16 */
310 30, 10,
311 0, /* 0/0 */
312 },
313 (4 + 4 + 1 + 3 + 2 + 1),
314 SHOULD_ERR,
315 AFI_IP, SAFI_UNICAST, VALID_AFI,
316 },
317 { "IPv4-VPNv4",
318 "IPv4/VPNv4 MP Reach, RD, Nexthop, 2 NLRIs",
319 {
320 /* AFI / SAFI */ 0x0, AFI_IP, IANA_SAFI_MPLS_VPN,
321 /* nexthop bytes */ 12,
322 /* RD */ 0, 0, 0, 0, /* RD defined to be 0 */
323 0, 0, 0, 0,
324 /* Nexthop */ 192, 168, 0, 1,
325 /* SNPA (defunct, MBZ) */ 0x0,
326 /* NLRI tuples */ 88 + 16,
327 0, 1, 2, /* tag */
328 /* rd, 8 octets */
329 0, 0, /* RD_TYPE_AS */
330 0, 2, 0, 0xff, 3, 4, /* AS(2):val(4) */
331 10, 1, /* 10.1/16 */
332 88 + 17,
333 0xff, 0, 0, /* tag */
334 /* rd, 8 octets */
335 0, 0, /* RD_TYPE_IP */
336 192, 168, 0, 1, /* IPv4 */
337 10, 2, 3, /* 10.2.3/17 */
338 },
339 (4 + 12 + 1 + (1+3+8+2) + (1+3+8+3)),
340 SHOULD_PARSE,
341 AFI_IP, IANA_SAFI_MPLS_VPN, VALID_AFI,
342 },
343 { "IPv4-VPNv4-bogus-plen",
344 "IPv4/MPLS-labeled VPN MP Reach, RD, Nexthop, NLRI / bogus p'len",
345 {
346 /* AFI / SAFI */ 0x0, AFI_IP, IANA_SAFI_MPLS_VPN,
347 /* nexthop bytes */ 12,
348 /* RD */ 0, 0, 1, 2,
349 0, 0xff, 3, 4,
350 /* Nexthop */ 192, 168, 0, 1,
351 /* SNPA (defunct, MBZ) */ 0x0,
352 /* NLRI tuples */ 16, 10, 1, /* 10.1/16 */
353 17, 10, 2, 3, /* 10.2.3/17 */
354 0, /* 0/0 */
355 },
356 (3 + 1 + 3*4 + 1 + 3 + 4 + 1),
357 SHOULD_ERR,
358 AFI_IP, IANA_SAFI_MPLS_VPN, VALID_AFI,
359 },
360 { "IPv4-VPNv4-plen1-short",
361 "IPv4/VPNv4 MP Reach, RD, Nexthop, 2 NLRIs, 1st plen short",
362 {
363 /* AFI / SAFI */ 0x0, AFI_IP, IANA_SAFI_MPLS_VPN,
364 /* nexthop bytes */ 12,
365 /* RD */ 0, 0, 0, 0, /* RD defined to be 0 */
366 0, 0, 0, 0,
367 /* Nexthop */ 192, 168, 0, 1,
368 /* SNPA (defunct, MBZ) */ 0x0,
369 /* NLRI tuples */ 88 + 1,
370 0, 1, 2, /* tag */
371 /* rd, 8 octets */
372 0, 0, /* RD_TYPE_AS */
373 0, 2, 0, 0xff, 3, 4, /* AS(2):val(4) */
374 10, 1, /* 10.1/16 */
375 88 + 17,
376 0xff, 0, 0, /* tag */
377 /* rd, 8 octets */
378 0, 0, /* RD_TYPE_IP */
379 192, 168, 0, 1, /* IPv4 */
380 10, 2, 3, /* 10.2.3/17 */
381 },
382 (4 + 12 + 1 + (1+3+8+2) + (1+3+8+3)),
383 SHOULD_ERR,
384 AFI_IP, IANA_SAFI_MPLS_VPN, VALID_AFI,
385 },
386 { "IPv4-VPNv4-plen1-long",
387 "IPv4/VPNv4 MP Reach, RD, Nexthop, 2 NLRIs, 1st plen long",
388 {
389 /* AFI / SAFI */ 0x0, AFI_IP, IANA_SAFI_MPLS_VPN,
390 /* nexthop bytes */ 12,
391 /* RD */ 0, 0, 0, 0, /* RD defined to be 0 */
392 0, 0, 0, 0,
393 /* Nexthop */ 192, 168, 0, 1,
394 /* SNPA (defunct, MBZ) */ 0x0,
395 /* NLRI tuples */ 88 + 32,
396 0, 1, 2, /* tag */
397 /* rd, 8 octets */
398 0, 0, /* RD_TYPE_AS */
399 0, 2, 0, 0xff, 3, 4, /* AS(2):val(4) */
400 10, 1, /* 10.1/16 */
401 88 + 17,
402 0xff, 0, 0, /* tag */
403 /* rd, 8 octets */
404 0, 0, /* RD_TYPE_IP */
405 192, 168, 0, 1, /* IPv4 */
406 10, 2, 3, /* 10.2.3/17 */
407 },
408 (4 + 12 + 1 + (1+3+8+2) + (1+3+8+3)),
409 SHOULD_ERR,
410 AFI_IP, IANA_SAFI_MPLS_VPN, VALID_AFI,
411 },
412 { "IPv4-VPNv4-plenn-long",
413 "IPv4/VPNv4 MP Reach, RD, Nexthop, 3 NLRIs, last plen long",
414 {
415 /* AFI / SAFI */ 0x0, AFI_IP, IANA_SAFI_MPLS_VPN,
416 /* nexthop bytes */ 12,
417 /* RD */ 0, 0, 0, 0, /* RD defined to be 0 */
418 0, 0, 0, 0,
419 /* Nexthop */ 192, 168, 0, 1,
420 /* SNPA (defunct, MBZ) */ 0x0,
421 /* NLRI tuples */ 88 + 16,
422 0, 1, 2, /* tag */
423 /* rd, 8 octets */
424 0, 0, /* RD_TYPE_AS */
425 0, 2, 0, 0xff, 3, 4, /* AS(2):val(4) */
426 10, 1, /* 10.1/16 */
427 88 + 17,
428 0xff, 0, 0, /* tag */
429 /* rd, 8 octets */
430 0, 0, /* RD_TYPE_IP */
431 192, 168, 0, 1, /* IPv4 */
432 10, 2, 3, /* 10.2.3/17 */
433 88 + 1, /* bogus */
434 },
435 (4 + 12 + 1 + (1+3+8+2) + (1+3+8+3) + 1),
436 SHOULD_ERR,
437 AFI_IP, IANA_SAFI_MPLS_VPN, VALID_AFI,
438 },
439 { "IPv4-VPNv4-plenn-short",
440 "IPv4/VPNv4 MP Reach, RD, Nexthop, 2 NLRIs, last plen short",
441 {
442 /* AFI / SAFI */ 0x0, AFI_IP, IANA_SAFI_MPLS_VPN,
443 /* nexthop bytes */ 12,
444 /* RD */ 0, 0, 0, 0, /* RD defined to be 0 */
445 0, 0, 0, 0,
446 /* Nexthop */ 192, 168, 0, 1,
447 /* SNPA (defunct, MBZ) */ 0x0,
448 /* NLRI tuples */ 88 + 16,
449 0, 1, 2, /* tag */
450 /* rd, 8 octets */
451 0, 0, /* RD_TYPE_AS */
452 0, 2, 0, 0xff, 3, 4, /* AS(2):val(4) */
453 10, 1, /* 10.1/16 */
454 88 + 2,
455 0xff, 0, 0, /* tag */
456 /* rd, 8 octets */
457 0, 0, /* RD_TYPE_IP */
458 192, 168, 0, 1, /* IPv4 */
459 10, 2, 3, /* 10.2.3/17 */
460 },
461 (4 + 12 + 1 + (1+3+8+2) + (1+3+8+3)),
462 SHOULD_ERR,
463 AFI_IP, IANA_SAFI_MPLS_VPN, VALID_AFI,
464 },
465 { "IPv4-VPNv4-bogus-rd-type",
466 "IPv4/VPNv4 MP Reach, RD, NH, 2 NLRI, unknown RD in 1st (log, but parse)",
467 {
468 /* AFI / SAFI */ 0x0, AFI_IP, IANA_SAFI_MPLS_VPN,
469 /* nexthop bytes */ 12,
470 /* RD */ 0, 0, 0, 0, /* RD defined to be 0 */
471 0, 0, 0, 0,
472 /* Nexthop */ 192, 168, 0, 1,
473 /* SNPA (defunct, MBZ) */ 0x0,
474 /* NLRI tuples */ 88 + 16,
475 0, 1, 2, /* tag */
476 /* rd, 8 octets */
477 0xff, 0, /* Bogus RD */
478 0, 2, 0, 0xff, 3, 4, /* AS(2):val(4) */
479 10, 1, /* 10.1/16 */
480 88 + 17,
481 0xff, 0, 0, /* tag */
482 /* rd, 8 octets */
483 0, 0, /* RD_TYPE_IP */
484 192, 168, 0, 1, /* IPv4 */
485 10, 2, 3, /* 10.2.3/17 */
486 },
487 (4 + 12 + 1 + (1+3+8+2) + (1+3+8+3)),
488 SHOULD_PARSE,
489 AFI_IP, IANA_SAFI_MPLS_VPN, VALID_AFI,
490 },
491 { "IPv4-VPNv4-0-nlri",
492 "IPv4/VPNv4 MP Reach, RD, Nexthop, 3 NLRI, 3rd 0 bogus",
493 {
494 /* AFI / SAFI */ 0x0, AFI_IP, IANA_SAFI_MPLS_VPN,
495 /* nexthop bytes */ 12,
496 /* RD */ 0, 0, 0, 0, /* RD defined to be 0 */
497 0, 0, 0, 0,
498 /* Nexthop */ 192, 168, 0, 1,
499 /* SNPA (defunct, MBZ) */ 0x0,
500 /* NLRI tuples */ 88 + 16,
501 0, 1, 2, /* tag */
502 /* rd, 8 octets */
503 0, 0, /* RD_TYPE_AS */
504 0, 2, 0, 0xff, 3, 4, /* AS(2):val(4) */
505 10, 1, /* 10.1/16 */
506 88 + 17,
507 0xff, 0, 0, /* tag */
508 /* rd, 8 octets */
509 0, 0, /* RD_TYPE_IP */
510 192, 168, 0, 1, /* IPv4 */
511 10, 2, 3, /* 10.2.3/17 */
512 0 /* 0/0, bogus for vpnv4 ?? */
513 },
514 (4 + 12 + 1 + (1+3+8+2) + (1+3+8+3) + 1),
515 SHOULD_ERR,
516 AFI_IP, IANA_SAFI_MPLS_VPN, VALID_AFI,
517 },
518
519 /* From bug #385 */
520 { "IPv6-bug",
521 "IPv6, global nexthop, 1 default NLRI",
522 {
523 /* AFI / SAFI */ 0x0, 0x2, 0x1,
524 /* nexthop bytes */ 0x20,
525 /* Nexthop (global) */ 0x20, 0x01, 0x04, 0x70,
526 0x00, 0x01, 0x00, 0x06,
527 0x00, 0x00, 0x00, 0x00,
528 0x00, 0x00, 0x00, 0x01,
529 /* Nexthop (local) */ 0xfe, 0x80, 0x00, 0x00,
530 0x00, 0x00, 0x00, 0x00,
531 0x02, 0x0c, 0xdb, 0xff,
532 0xfe, 0xfe, 0xeb, 0x00,
533 /* SNPA (defunct, MBZ) */ 0,
534 /* NLRI tuples */ /* Should have 0 here for ::/0, but dont */
535 },
536 37,
537 SHOULD_ERR,
538 AFI_IP6, SAFI_UNICAST, VALID_AFI,
539 },
540
541 { NULL, NULL, {0}, 0, 0}
542 };
543
544 /* MP_UNREACH_NLRI tests */
545 static struct test_segment mp_unreach_segments [] =
546 {
547 { "IPv6-unreach",
548 "IPV6 MP Unreach, 1 NLRI",
549 {
550 /* AFI / SAFI */ 0x0, AFI_IP6, SAFI_UNICAST,
551 /* NLRI tuples */ 32, 0xff, 0xfe, 0x1, 0x2, /* fffe:102::/32 */
552 },
553 (3 + 5),
554 SHOULD_PARSE,
555 AFI_IP6, SAFI_UNICAST, VALID_AFI,
556 },
557 { "IPv6-unreach2",
558 "IPV6 MP Unreach, 2 NLRIs",
559 {
560 /* AFI / SAFI */ 0x0, AFI_IP6, SAFI_UNICAST,
561 /* NLRI tuples */ 32,
562 0xff, 0xfe, 0x1, 0x2, /* fffe:102::/32 */
563 64,
564 0xff, 0xfe, 0x0, 0x1, /* fffe:1:2:3::/64 */
565 0x0, 0x2, 0x0, 0x3,
566 },
567 (3 + 5 + 9),
568 SHOULD_PARSE,
569 AFI_IP6, SAFI_UNICAST, VALID_AFI,
570 },
571 { "IPv6-unreach-default",
572 "IPV6 MP Unreach, 2 NLRIs + default",
573 {
574 /* AFI / SAFI */ 0x0, AFI_IP6, SAFI_UNICAST,
575 /* NLRI tuples */ 32,
576 0xff, 0xfe, 0x1, 0x2, /* fffe:102::/32 */
577 64,
578 0xff, 0xfe, 0x0, 0x1, /* fffe:1:2:3::/64 */
579 0x0, 0x2, 0x0, 0x3,
580 0x0, /* ::/0 */
581 },
582 (3 + 5 + 9 + 1),
583 SHOULD_PARSE,
584 AFI_IP6, SAFI_UNICAST, VALID_AFI,
585 },
586 { "IPv6-unreach-nlri",
587 "IPV6 MP Unreach, NLRI bitlen overflow",
588 {
589 /* AFI / SAFI */ 0x0, AFI_IP6, SAFI_UNICAST,
590 /* NLRI tuples */ 120,
591 0xff, 0xfe, 0x1, 0x2, /* fffe:102::/32 */
592 64,
593 0xff, 0xfe, 0x0, 0x1, /* fffe:1:2:3::/64 */
594 0x0, 0x2, 0x0, 0x3,
595 0, /* ::/0 */
596 },
597 (3 + 5 + 9 + 1),
598 SHOULD_ERR,
599 AFI_IP6, SAFI_UNICAST, VALID_AFI,
600 },
601 { "IPv4-unreach",
602 "IPv4 MP Unreach, 2 NLRIs + default",
603 {
604 /* AFI / SAFI */ 0x0, AFI_IP, SAFI_UNICAST,
605 /* NLRI tuples */ 16, 10, 1, /* 10.1/16 */
606 17, 10, 2, 3, /* 10.2.3/17 */
607 0, /* 0/0 */
608 },
609 (3 + 3 + 4 + 1),
610 SHOULD_PARSE,
611 AFI_IP, SAFI_UNICAST, VALID_AFI,
612 },
613 { "IPv4-unreach-nlrilen",
614 "IPv4 MP Unreach, nlri length overflow",
615 {
616 /* AFI / SAFI */ 0x0, AFI_IP, SAFI_UNICAST,
617 /* NLRI tuples */ 16, 10, 1, /* 10.1/16 */
618 30, 10,
619 0, /* 0/0 */
620 },
621 (3 + 3 + 2 + 1),
622 SHOULD_ERR,
623 AFI_IP, SAFI_UNICAST, VALID_AFI,
624 },
625 { "IPv4-unreach-VPNv4",
626 "IPv4/MPLS-labeled VPN MP Unreach, RD, 3 NLRIs",
627 {
628 /* AFI / SAFI */ 0x0, AFI_IP, IANA_SAFI_MPLS_VPN,
629 /* NLRI tuples */ 88 + 16,
630 0, 1, 2, /* tag */
631 /* rd, 8 octets */
632 0, 0, /* RD_TYPE_AS */
633 0, 2, 0, 0xff, 3, 4, /* AS(2):val(4) */
634 10, 1, /* 10.1/16 */
635 88 + 17,
636 0xff, 0, 0, /* tag */
637 /* rd, 8 octets */
638 0, 0, /* RD_TYPE_IP */
639 192, 168, 0, 1, /* IPv4 */
640 10, 2, 3, /* 10.2.3/17 */
641 },
642 (3 + (1+3+8+2) + (1+3+8+3)),
643 SHOULD_PARSE,
644 AFI_IP, IANA_SAFI_MPLS_VPN, VALID_AFI,
645 },
646 { NULL, NULL, {0}, 0, 0}
647 };
648
649 /* nlri_parse indicates 0 on successful parse, and -1 otherwise.
650 * attr_parse indicates BGP_ATTR_PARSE_PROCEED/0 on success,
651 * and BGP_ATTR_PARSE_ERROR/-1 or lower negative ret on err.
652 */
653 static void
654 handle_result (struct peer *peer, struct test_segment *t,
655 int parse_ret, int nlri_ret)
656 {
657 int oldfailed = failed;
658
659 printf ("mp attr parsed?: %s\n", parse_ret ? "no" : "yes");
660 if (!parse_ret)
661 printf ("nrli parsed?: %s\n", nlri_ret ? "no" : "yes");
662 printf ("should parse?: %s\n", t->parses ? "no" : "yes");
663
664 if ((parse_ret != 0 || nlri_ret != 0) != (t->parses != 0))
665 failed++;
666
667
668 if (tty)
669 printf ("%s", (failed > oldfailed) ? VT100_RED "failed!" VT100_RESET
670 : VT100_GREEN "OK" VT100_RESET);
671 else
672 printf ("%s", (failed > oldfailed) ? "failed!" : "OK" );
673
674 if (failed)
675 printf (" (%u)", failed);
676
677 printf ("\n\n");
678 }
679
680 /* basic parsing test */
681 static void
682 parse_test (struct peer *peer, struct test_segment *t, int type)
683 {
684 int parse_ret = 0, nlri_ret = 0;
685 struct attr attr = { };
686 struct bgp_nlri nlri = { };
687 struct bgp_attr_parser_args attr_args = {
688 .peer = peer,
689 .length = t->len,
690 .total = 1,
691 .attr = &attr,
692 .type = type,
693 .flags = BGP_ATTR_FLAG_OPTIONAL,
694 .startp = BGP_INPUT_PNT (peer),
695 };
696 #define RANDOM_FUZZ 35
697
698 stream_reset (peer->ibuf);
699 stream_put (peer->ibuf, NULL, RANDOM_FUZZ);
700 stream_set_getp (peer->ibuf, RANDOM_FUZZ);
701
702 stream_write (peer->ibuf, t->data, t->len);
703
704 printf ("%s: %s\n", t->name, t->desc);
705
706 if (type == BGP_ATTR_MP_REACH_NLRI)
707 parse_ret = bgp_mp_reach_parse (&attr_args, &nlri);
708 else
709 parse_ret = bgp_mp_unreach_parse (&attr_args, &nlri);
710 if (!parse_ret)
711 {
712 iana_afi_t pkt_afi;
713 safi_t pkt_safi;
714
715 /* Convert AFI, SAFI to internal values, check. */
716 if (bgp_map_afi_safi_int2iana (nlri.afi, nlri.safi, &pkt_afi, &pkt_safi))
717 assert (0);
718
719 printf ("MP: %u(%u)/%u(%u): recv %u, nego %u\n",
720 nlri.afi , pkt_afi, nlri.safi, pkt_safi,
721 peer->afc_recv[nlri.afi][nlri.safi],
722 peer->afc_nego[nlri.afi][nlri.safi]);
723 }
724
725 if (!parse_ret)
726 {
727 if (type == BGP_ATTR_MP_REACH_NLRI)
728 nlri_ret = bgp_nlri_parse (peer, &attr, &nlri, 0);
729 else
730 nlri_ret = bgp_nlri_parse (peer, &attr, &nlri, 1);
731 }
732 handle_result (peer, t, parse_ret, nlri_ret);
733 }
734
735 static struct bgp *bgp;
736 static as_t asn = 100;
737
738 int
739 main (void)
740 {
741 struct peer *peer;
742 int i, j;
743
744 conf_bgp_debug_neighbor_events = -1UL;
745 conf_bgp_debug_packet = -1UL;
746 conf_bgp_debug_as4 = -1UL;
747 term_bgp_debug_neighbor_events = -1UL;
748 term_bgp_debug_packet = -1UL;
749 term_bgp_debug_as4 = -1UL;
750
751 qobj_init ();
752 master = thread_master_create ();
753 bgp_master_init (master);
754 vrf_init ();
755 bgp_option_set (BGP_OPT_NO_LISTEN);
756 bgp_attr_init ();
757
758 if (fileno (stdout) >= 0)
759 tty = isatty (fileno (stdout));
760
761 if (bgp_get (&bgp, &asn, NULL, BGP_INSTANCE_TYPE_DEFAULT))
762 return -1;
763
764 peer = peer_create_accept (bgp);
765 peer->host = (char *)"foo";
766 peer->status = Established;
767
768 for (i = AFI_IP; i < AFI_MAX; i++)
769 for (j = SAFI_UNICAST; j < SAFI_MAX; j++)
770 {
771 peer->afc[i][j] = 1;
772 peer->afc_adv[i][j] = 1;
773 }
774
775 i = 0;
776 while (mp_reach_segments[i].name)
777 parse_test (peer, &mp_reach_segments[i++], BGP_ATTR_MP_REACH_NLRI);
778
779 i = 0;
780 while (mp_unreach_segments[i].name)
781 parse_test (peer, &mp_unreach_segments[i++], BGP_ATTR_MP_UNREACH_NLRI);
782
783 printf ("failures: %d\n", failed);
784 return failed;
785 }