]> The Tcpdump Group git mirrors - tcpdump/blob - print-bgp.c
replace some macros for printing AFI and SAFI information with tok2str()
[tcpdump] / print-bgp.c
1 /*
2 * Copyright (C) 1999 WIDE Project.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of the project nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 */
29
30 #ifdef HAVE_CONFIG_H
31 #include "config.h"
32 #endif
33
34 #ifndef lint
35 static const char rcsid[] =
36 "@(#) $Header: /tcpdump/master/tcpdump/print-bgp.c,v 1.32 2002-07-03 19:47:53 hannes Exp $";
37 #endif
38
39 #include <sys/param.h>
40 #include <sys/time.h>
41 #include <sys/types.h>
42 #include <sys/socket.h>
43
44 #include <netinet/in.h>
45
46 #include <errno.h>
47 #include <stdio.h>
48 #include <string.h>
49 #include <netdb.h>
50
51 #include "interface.h"
52 #include "addrtoname.h"
53 #include "extract.h"
54
55 struct bgp {
56 u_int8_t bgp_marker[16];
57 u_int16_t bgp_len;
58 u_int8_t bgp_type;
59 };
60 #define BGP_SIZE 19 /* unaligned */
61
62 #define BGP_OPEN 1
63 #define BGP_UPDATE 2
64 #define BGP_NOTIFICATION 3
65 #define BGP_KEEPALIVE 4
66
67 struct bgp_open {
68 u_int8_t bgpo_marker[16];
69 u_int16_t bgpo_len;
70 u_int8_t bgpo_type;
71 u_int8_t bgpo_version;
72 u_int16_t bgpo_myas;
73 u_int16_t bgpo_holdtime;
74 u_int32_t bgpo_id;
75 u_int8_t bgpo_optlen;
76 /* options should follow */
77 };
78 #define BGP_OPEN_SIZE 29 /* unaligned */
79
80 struct bgp_opt {
81 u_int8_t bgpopt_type;
82 u_int8_t bgpopt_len;
83 /* variable length */
84 };
85 #define BGP_OPT_SIZE 2 /* some compilers may pad to 4 bytes */
86
87 struct bgp_notification {
88 u_int8_t bgpn_marker[16];
89 u_int16_t bgpn_len;
90 u_int8_t bgpn_type;
91 u_int8_t bgpn_major;
92 u_int8_t bgpn_minor;
93 /* data should follow */
94 };
95 #define BGP_NOTIFICATION_SIZE 21 /* unaligned */
96
97 struct bgp_attr {
98 u_int8_t bgpa_flags;
99 u_int8_t bgpa_type;
100 union {
101 u_int8_t len;
102 u_int16_t elen;
103 } bgpa_len;
104 #define bgp_attr_len(p) \
105 (((p)->bgpa_flags & 0x10) ? \
106 ntohs((p)->bgpa_len.elen) : (p)->bgpa_len.len)
107 #define bgp_attr_off(p) \
108 (((p)->bgpa_flags & 0x10) ? 4 : 3)
109 };
110
111 #define BGPTYPE_ORIGIN 1
112 #define BGPTYPE_AS_PATH 2
113 #define BGPTYPE_NEXT_HOP 3
114 #define BGPTYPE_MULTI_EXIT_DISC 4
115 #define BGPTYPE_LOCAL_PREF 5
116 #define BGPTYPE_ATOMIC_AGGREGATE 6
117 #define BGPTYPE_AGGREGATOR 7
118 #define BGPTYPE_COMMUNITIES 8 /* RFC1997 */
119 #define BGPTYPE_ORIGINATOR_ID 9 /* RFC1998 */
120 #define BGPTYPE_CLUSTER_LIST 10 /* RFC1998 */
121 #define BGPTYPE_DPA 11 /* work in progress */
122 #define BGPTYPE_ADVERTISERS 12 /* RFC1863 */
123 #define BGPTYPE_RCID_PATH 13 /* RFC1863 */
124 #define BGPTYPE_MP_REACH_NLRI 14 /* RFC2283 */
125 #define BGPTYPE_MP_UNREACH_NLRI 15 /* RFC2283 */
126
127
128 static const char *bgptype[] = {
129 NULL, "OPEN", "UPDATE", "NOTIFICATION", "KEEPALIVE",
130 };
131 #define bgp_type(x) num_or_str(bgptype, sizeof(bgptype)/sizeof(bgptype[0]), (x))
132
133 static const char *bgpopt_type[] = {
134 NULL, "Authentication Information", "Capabilities Advertisement",
135 };
136 #define bgp_opttype(x) \
137 num_or_str(bgpopt_type, sizeof(bgpopt_type)/sizeof(bgpopt_type[0]), (x))
138
139 static const char *bgpnotify_major[] = {
140 NULL, "Message Header Error",
141 "OPEN Message Error", "UPDATE Message Error",
142 "Hold Timer Expired", "Finite State Machine Error",
143 "Cease",
144 };
145 #define bgp_notify_major(x) \
146 num_or_str(bgpnotify_major, \
147 sizeof(bgpnotify_major)/sizeof(bgpnotify_major[0]), (x))
148
149 static const char *bgpnotify_minor_1[] = {
150 NULL, "Connection Not Synchronized",
151 "Bad Message Length", "Bad Message Type",
152 };
153
154 static const char *bgpnotify_minor_2[] = {
155 NULL, "Unsupported Version Number",
156 "Bad Peer AS", "Bad BGP Identifier",
157 "Unsupported Optional Parameter", "Authentication Failure",
158 "Unacceptable Hold Time",
159 };
160
161 static const char *bgpnotify_minor_3[] = {
162 NULL, "Malformed Attribute List",
163 "Unrecognized Well-known Attribute", "Missing Well-known Attribute",
164 "Attribute Flags Error", "Attribute Length Error",
165 "Invalid ORIGIN Attribute", "AS Routing Loop",
166 "Invalid NEXT_HOP Attribute", "Optional Attribute Error",
167 "Invalid Network Field", "Malformed AS_PATH",
168 };
169
170 static const char **bgpnotify_minor[] = {
171 NULL, bgpnotify_minor_1, bgpnotify_minor_2, bgpnotify_minor_3,
172 };
173 static const int bgpnotify_minor_siz[] = {
174 0, sizeof(bgpnotify_minor_1)/sizeof(bgpnotify_minor_1[0]),
175 sizeof(bgpnotify_minor_2)/sizeof(bgpnotify_minor_2[0]),
176 sizeof(bgpnotify_minor_3)/sizeof(bgpnotify_minor_3[0]),
177 };
178
179 static const char *bgpattr_origin[] = {
180 "IGP", "EGP", "INCOMPLETE",
181 };
182 #define bgp_attr_origin(x) \
183 num_or_str(bgpattr_origin, \
184 sizeof(bgpattr_origin)/sizeof(bgpattr_origin[0]), (x))
185
186 static const char *bgpattr_type[] = {
187 NULL, "ORIGIN", "AS_PATH", "NEXT_HOP",
188 "MULTI_EXIT_DISC", "LOCAL_PREF", "ATOMIC_AGGREGATE", "AGGREGATOR",
189 "COMMUNITIES", "ORIGINATOR_ID", "CLUSTER_LIST", "DPA",
190 "ADVERTISERS", "RCID_PATH", "MP_REACH_NLRI", "MP_UNREACH_NLRI",
191 };
192 #define bgp_attr_type(x) \
193 num_or_str(bgpattr_type, \
194 sizeof(bgpattr_type)/sizeof(bgpattr_type[0]), (x))
195
196 /* Subsequent address family identifier, RFC2283 section 7 */
197 #define SAFNUM_RES 0
198 #define SAFNUM_UNICAST 1
199 #define SAFNUM_MULTICAST 2
200 #define SAFNUM_UNIMULTICAST 3
201 /* labeled BGP RFC3107 */
202 #define SAFNUM_LABUNICAST 4
203 /* Section 4.3.4 of draft-rosen-rfc2547bis-03.txt */
204 #define SAFNUM_VPNUNICAST 128
205 #define SAFNUM_VPNMULTICAST 129
206 #define SAFNUM_VPNUNIMULTICAST 130
207
208 static struct tok bgp_safi_values[] = {
209 { SAFNUM_RES, "Reserved"},
210 { SAFNUM_UNICAST, "Unicast"},
211 { SAFNUM_MULTICAST, "Multicast"},
212 { SAFNUM_UNIMULTICAST, "Unicast+Multicast"},
213 { SAFNUM_LABUNICAST, "labeled Unicast"},
214 { SAFNUM_VPNUNICAST, "labeled VPN Unicast"},
215 { SAFNUM_VPNMULTICAST, "labeled VPN Multicast"},
216 { SAFNUM_VPNUNIMULTICAST, "labeled VPN Unicast+Multicast"},
217 { 0, NULL }
218 };
219
220 /* well-known community */
221 #define BGP_COMMUNITY_NO_EXPORT 0xffffff01
222 #define BGP_COMMUNITY_NO_ADVERT 0xffffff02
223 #define BGP_COMMUNITY_NO_EXPORT_SUBCONFED 0xffffff03
224
225 /* RFC1700 address family numbers */
226 #define AFNUM_INET 1
227 #define AFNUM_INET6 2
228 #define AFNUM_NSAP 3
229 #define AFNUM_HDLC 4
230 #define AFNUM_BBN1822 5
231 #define AFNUM_802 6
232 #define AFNUM_E163 7
233 #define AFNUM_E164 8
234 #define AFNUM_F69 9
235 #define AFNUM_X121 10
236 #define AFNUM_IPX 11
237 #define AFNUM_ATALK 12
238 #define AFNUM_DECNET 13
239 #define AFNUM_BANYAN 14
240 #define AFNUM_E164NSAP 15
241 /* draft-kompella-ppvpn-l2vpn */
242 #define AFNUM_L2VPN 196 /* still to be approved by IANA */
243
244 static struct tok bgp_afi_values[] = {
245 { 0, "Reserved"},
246 { AFNUM_INET, "IPv4"},
247 { AFNUM_INET6, "IPv6"},
248 { AFNUM_NSAP, "NSAP"},
249 { AFNUM_HDLC, "HDLC"},
250 { AFNUM_BBN1822, "BBN 1822"},
251 { AFNUM_802, "802"},
252 { AFNUM_E163, "E.163"},
253 { AFNUM_E164, "E.164"},
254 { AFNUM_F69, "F.69"},
255 { AFNUM_X121, "X.121"},
256 { AFNUM_IPX, "Novell IPX"},
257 { AFNUM_ATALK, "Appletalk"},
258 { AFNUM_DECNET, "Decnet IV"},
259 { AFNUM_BANYAN, "Banyan Vines"},
260 { AFNUM_E164NSAP, "E.164 with NSAP subaddress"},
261 { AFNUM_L2VPN, "Layer-2 VPN"},
262 { 0, NULL},
263 };
264
265 static const char *
266 num_or_str(const char **table, size_t siz, int value)
267 {
268 static char buf[20];
269 if (value < 0 || siz <= value || table[value] == NULL) {
270 snprintf(buf, sizeof(buf), "#%d", value);
271 return buf;
272 } else
273 return table[value];
274 }
275
276 static const char *
277 bgp_notify_minor(int major, int minor)
278 {
279 static const char **table;
280 int siz;
281 static char buf[20];
282 const char *p;
283
284 if (0 <= major
285 && major < sizeof(bgpnotify_minor)/sizeof(bgpnotify_minor[0])
286 && bgpnotify_minor[major]) {
287 table = bgpnotify_minor[major];
288 siz = bgpnotify_minor_siz[major];
289 if (0 <= minor && minor < siz && table[minor])
290 p = table[minor];
291 else
292 p = NULL;
293 } else
294 p = NULL;
295 if (p == NULL) {
296 snprintf(buf, sizeof(buf), "#%d", minor);
297 return buf;
298 } else
299 return p;
300 }
301
302 static int
303 decode_prefix4(const u_char *pd, char *buf, u_int buflen)
304 {
305 struct in_addr addr;
306 u_int plen;
307
308 plen = pd[0];
309 if (plen < 0 || 32 < plen)
310 return -1;
311
312 memset(&addr, 0, sizeof(addr));
313 memcpy(&addr, &pd[1], (plen + 7) / 8);
314 if (plen % 8) {
315 ((u_char *)&addr)[(plen + 7) / 8 - 1] &=
316 ((0xff00 >> (plen % 8)) & 0xff);
317 }
318 snprintf(buf, buflen, "%s/%d", getname((u_char *)&addr), plen);
319 return 1 + (plen + 7) / 8;
320 }
321
322 static int
323 decode_labeled_prefix4(const u_char *pd, char *buf, u_int buflen)
324 {
325 struct in_addr addr;
326 u_int plen;
327
328 plen = pd[0]; /* get prefix length */
329
330 /* this is one of the weirdnesses of rfc3107
331 the label length (actually the label + COS bits)
332 is added of the prefix length;
333 we also do only read out just one label -
334 there is no real application for advertisment of
335 stacked labels in a a single BGP message
336 */
337
338 plen-=24; /* adjust prefixlen - labellength */
339
340 if (plen < 0 || 32 < plen)
341 return -1;
342
343 memset(&addr, 0, sizeof(addr));
344 memcpy(&addr, &pd[4], (plen + 7) / 8);
345 if (plen % 8) {
346 ((u_char *)&addr)[(plen + 7) / 8 - 1] &=
347 ((0xff00 >> (plen % 8)) & 0xff);
348 }
349 /* the label may get offsetted by 4 bits so lets shift it right */
350 snprintf(buf, buflen, "%s/%d label:%u %s",
351 getname((u_char *)&addr),
352 plen,
353 EXTRACT_24BITS(pd+1)>>4,
354 ((pd[3]&1)==0) ? "(BOGUS: Bottom of Stack NOT set!)" : "(bottom)" );
355
356 return 4 + (plen + 7) / 8;
357 }
358
359 #ifdef INET6
360 static int
361 decode_prefix6(const u_char *pd, char *buf, u_int buflen)
362 {
363 struct in6_addr addr;
364 u_int plen;
365
366 plen = pd[0];
367 if (plen < 0 || 128 < plen)
368 return -1;
369
370 memset(&addr, 0, sizeof(addr));
371 memcpy(&addr, &pd[1], (plen + 7) / 8);
372 if (plen % 8) {
373 addr.s6_addr[(plen + 7) / 8 - 1] &=
374 ((0xff00 >> (plen % 8)) & 0xff);
375 }
376 snprintf(buf, buflen, "%s/%d", getname6((u_char *)&addr), plen);
377 return 1 + (plen + 7) / 8;
378 }
379 #endif
380
381 static void
382 bgp_attr_print(const struct bgp_attr *attr, const u_char *dat, int len)
383 {
384 int i;
385 u_int16_t af;
386 u_int8_t safi, snpa;
387 int advance;
388 int tlen;
389 const u_char *p;
390 char buf[MAXHOSTNAMELEN + 100];
391
392 p = dat;
393
394 switch (attr->bgpa_type) {
395 case BGPTYPE_ORIGIN:
396 if (len != 1)
397 printf(" invalid len");
398 else
399 printf(" %s", bgp_attr_origin(p[0]));
400 break;
401 case BGPTYPE_AS_PATH:
402 if (len % 2) {
403 printf(" invalid len");
404 break;
405 }
406 while (p < dat + len) {
407 /*
408 * under RFC1965, p[0] means:
409 * 1: AS_SET 2: AS_SEQUENCE
410 * 3: AS_CONFED_SET 4: AS_CONFED_SEQUENCE
411 */
412 printf(" ");
413 if (p[0] == 3 || p[0] == 4)
414 printf("confed");
415 printf("%s", (p[0] & 1) ? "{" : "");
416 for (i = 0; i < p[1] * 2; i += 2) {
417 printf("%s%u", i == 0 ? "" : " ",
418 EXTRACT_16BITS(&p[2 + i]));
419 }
420 printf("%s", (p[0] & 1) ? "}" : "");
421 p += 2 + p[1] * 2;
422 }
423 break;
424 case BGPTYPE_NEXT_HOP:
425 if (len != 4)
426 printf(" invalid len");
427 else
428 printf(" %s", getname(p));
429 break;
430 case BGPTYPE_MULTI_EXIT_DISC:
431 case BGPTYPE_LOCAL_PREF:
432 if (len != 4)
433 printf(" invalid len");
434 else
435 printf(" %u", EXTRACT_32BITS(p));
436 break;
437 case BGPTYPE_ATOMIC_AGGREGATE:
438 if (len != 0)
439 printf(" invalid len");
440 break;
441 case BGPTYPE_AGGREGATOR:
442 if (len != 6) {
443 printf(" invalid len");
444 break;
445 }
446 printf(" AS #%u, origin %s", EXTRACT_16BITS(p),
447 getname(p + 2));
448 break;
449 case BGPTYPE_COMMUNITIES:
450 if (len % 4) {
451 printf(" invalid len");
452 break;
453 }
454 for (i = 0; i < len; i += 4) {
455 u_int32_t comm;
456 comm = EXTRACT_32BITS(&p[i]);
457 switch (comm) {
458 case BGP_COMMUNITY_NO_EXPORT:
459 printf(" NO_EXPORT");
460 break;
461 case BGP_COMMUNITY_NO_ADVERT:
462 printf(" NO_ADVERTISE");
463 break;
464 case BGP_COMMUNITY_NO_EXPORT_SUBCONFED:
465 printf(" NO_EXPORT_SUBCONFED");
466 break;
467 default:
468 printf(" (AS #%d value 0x%04x)",
469 (comm >> 16) & 0xffff, comm & 0xffff);
470 break;
471 }
472 }
473 break;
474 case BGPTYPE_MP_REACH_NLRI:
475 af = EXTRACT_16BITS(p);
476 safi = p[2];
477 if (safi >= 128)
478 printf(" %s vendor specific,",
479 tok2str(bgp_afi_values, "Unknown", af));
480 else {
481 printf(" AFI %s SAFI %s,",
482 tok2str(bgp_afi_values, "Unknown", af),
483 tok2str(bgp_safi_values, "Unknown", safi));
484 }
485 p += 3;
486
487 if (af == AFNUM_INET)
488 ;
489 #ifdef INET6
490 else if (af == AFNUM_INET6)
491 ;
492 #endif
493 else
494 break;
495
496 tlen = p[0];
497 if (tlen) {
498 printf(" nexthop");
499 i = 0;
500 while (i < tlen) {
501 switch (af) {
502 case AFNUM_INET:
503 printf(" %s", getname(p + 1 + i));
504 i += sizeof(struct in_addr);
505 break;
506 #ifdef INET6
507 case AFNUM_INET6:
508 printf(" %s", getname6(p + 1 + i));
509 i += sizeof(struct in6_addr);
510 break;
511 #endif
512 default:
513 printf(" (unknown af)");
514 i = tlen; /*exit loop*/
515 break;
516 }
517 }
518 printf(",");
519 }
520 p += 1 + tlen;
521
522 snpa = p[0];
523 p++;
524 if (snpa) {
525 printf(" %u snpa", snpa);
526 for (/*nothing*/; snpa > 0; snpa--) {
527 printf("(%d bytes)", p[0]);
528 p += p[0] + 1;
529 }
530 printf(",");
531 } else {
532 printf(" no spna,");
533 }
534
535 printf(" NLRI");
536 while (len - (p - dat) > 0) {
537 switch (af) {
538 case AFNUM_INET:
539 if(safi==SAFNUM_LABUNICAST) {
540 advance = decode_labeled_prefix4(p, buf, sizeof(buf));
541 } else {
542 advance = decode_prefix4(p, buf, sizeof(buf));
543 }
544 if (advance<0)
545 break;
546 printf(" %s", buf);
547 break;
548 #ifdef INET6
549 case AFNUM_INET6:
550 advance = decode_prefix6(p, buf, sizeof(buf));
551 printf(" %s", buf);
552 break;
553 #endif
554 default:
555 printf(" (unknown af)");
556 advance = 0;
557 p = dat + len;
558 break;
559 }
560
561 p += advance;
562 }
563
564 break;
565
566 case BGPTYPE_MP_UNREACH_NLRI:
567 af = EXTRACT_16BITS(p);
568 safi = p[2];
569 if (safi >= 128)
570 printf(" %s vendor specific,",
571 tok2str(bgp_afi_values, "Unknown", af));
572 else {
573 printf(" AFI %s SAFI %s,",
574 tok2str(bgp_afi_values, "Unknown", af),
575 tok2str(bgp_safi_values, "Unknown", safi));
576 }
577 p += 3;
578
579 printf(" Withdraw");
580 while (len - (p - dat) > 0) {
581 switch (af) {
582 case AFNUM_INET:
583 if(safi==SAFNUM_LABUNICAST) {
584 advance = decode_labeled_prefix4(p, buf, sizeof(buf));
585 } else {
586 advance = decode_prefix4(p, buf, sizeof(buf));
587 }
588 if (advance<0)
589 break;
590 printf(" %s", buf);
591 break;
592 #ifdef INET6
593 case AFNUM_INET6:
594 advance = decode_prefix6(p, buf, sizeof(buf));
595 printf(" %s", buf);
596 break;
597 #endif
598 default:
599 printf(" (unknown af)");
600 advance = 0;
601 p = dat + len;
602 break;
603 }
604
605 p += advance;
606 }
607 break;
608 default:
609 break;
610 }
611 }
612
613 static void
614 bgp_open_print(const u_char *dat, int length)
615 {
616 struct bgp_open bgpo;
617 struct bgp_opt bgpopt;
618 int hlen;
619 const u_char *opt;
620 int i;
621
622 TCHECK2(dat[0], BGP_OPEN_SIZE);
623 memcpy(&bgpo, dat, BGP_OPEN_SIZE);
624 hlen = ntohs(bgpo.bgpo_len);
625
626 printf(": Version %d,", bgpo.bgpo_version);
627 printf(" AS #%u,", ntohs(bgpo.bgpo_myas));
628 printf(" Holdtime %u,", ntohs(bgpo.bgpo_holdtime));
629 printf(" ID %s,", getname((u_char *)&bgpo.bgpo_id));
630 printf(" Option length %u", bgpo.bgpo_optlen);
631
632 /* ugly! */
633 opt = &((const struct bgp_open *)dat)->bgpo_optlen;
634 opt++;
635
636 i = 0;
637 while (i < bgpo.bgpo_optlen) {
638 TCHECK2(opt[i], BGP_OPT_SIZE);
639 memcpy(&bgpopt, &opt[i], BGP_OPT_SIZE);
640 if (i + 2 + bgpopt.bgpopt_len > bgpo.bgpo_optlen) {
641 printf(" [|opt %d %d]", bgpopt.bgpopt_len, bgpopt.bgpopt_type);
642 break;
643 }
644
645 printf(" (option %s, len=%d)", bgp_opttype(bgpopt.bgpopt_type),
646 bgpopt.bgpopt_len);
647 i += BGP_OPT_SIZE + bgpopt.bgpopt_len;
648 }
649 return;
650 trunc:
651 printf("[|BGP]");
652 }
653
654 static void
655 bgp_update_print(const u_char *dat, int length)
656 {
657 struct bgp bgp;
658 struct bgp_attr bgpa;
659 int hlen;
660 const u_char *p;
661 int len;
662 int i;
663 int newline;
664
665 TCHECK2(dat[0], BGP_SIZE);
666 memcpy(&bgp, dat, BGP_SIZE);
667 hlen = ntohs(bgp.bgp_len);
668 p = dat + BGP_SIZE; /*XXX*/
669 printf(":");
670
671 /* Unfeasible routes */
672 len = EXTRACT_16BITS(p);
673 if (len) {
674 /*
675 * Without keeping state from the original NLRI message,
676 * it's not possible to tell if this a v4 or v6 route,
677 * so only try to decode it if we're not v6 enabled.
678 */
679 #ifdef INET6
680 printf(" (Withdrawn routes: %d bytes)", len);
681 #else
682 char buf[MAXHOSTNAMELEN + 100];
683
684 TCHECK2(p[2], len);
685 i = 2;
686
687 printf(" (Withdrawn routes:");
688
689 while(i < 2 + len) {
690 i += decode_prefix4(&p[i], buf, sizeof(buf));
691 printf(" %s", buf);
692 }
693 printf(")\n");
694 #endif
695 }
696 p += 2 + len;
697
698 TCHECK2(p[0], 2);
699 len = EXTRACT_16BITS(p);
700 if (len) {
701 /* do something more useful!*/
702 i = 2;
703 printf(" (Path attributes:"); /* ) */
704 newline = 0;
705 while (i < 2 + len) {
706 int alen, aoff;
707
708 TCHECK2(p[i], sizeof(bgpa));
709 memcpy(&bgpa, &p[i], sizeof(bgpa));
710 alen = bgp_attr_len(&bgpa);
711 aoff = bgp_attr_off(&bgpa);
712
713 if (vflag && newline)
714 printf("\n\t\t");
715 else
716 printf(" ");
717 printf("("); /* ) */
718 printf("%s", bgp_attr_type(bgpa.bgpa_type));
719 if (bgpa.bgpa_flags) {
720 printf("[%s%s%s%s",
721 bgpa.bgpa_flags & 0x80 ? "O" : "",
722 bgpa.bgpa_flags & 0x40 ? "T" : "",
723 bgpa.bgpa_flags & 0x20 ? "P" : "",
724 bgpa.bgpa_flags & 0x10 ? "E" : "");
725 if (bgpa.bgpa_flags & 0xf)
726 printf("+%x", bgpa.bgpa_flags & 0xf);
727 printf("]");
728 }
729
730 bgp_attr_print(&bgpa, &p[i + aoff], alen);
731 newline = 1;
732
733 /* ( */
734 printf(")");
735
736 i += aoff + alen;
737 }
738
739 /* ( */
740 printf(")");
741 }
742 p += 2 + len;
743
744 if (len && dat + length > p)
745 printf("\n\t\t");
746 if (dat + length > p) {
747 printf("(NLRI:"); /* ) */
748 while (dat + length > p) {
749 char buf[MAXHOSTNAMELEN + 100];
750 i = decode_prefix4(p, buf, sizeof(buf));
751 printf(" %s", buf);
752 if (i < 0)
753 break;
754 p += i;
755 }
756
757 /* ( */
758 printf(")");
759 }
760 return;
761 trunc:
762 printf("[|BGP]");
763 }
764
765 static void
766 bgp_notification_print(const u_char *dat, int length)
767 {
768 struct bgp_notification bgpn;
769 int hlen;
770
771 TCHECK2(dat[0], BGP_NOTIFICATION_SIZE);
772 memcpy(&bgpn, dat, BGP_NOTIFICATION_SIZE);
773 hlen = ntohs(bgpn.bgpn_len);
774
775 printf(": error %s,", bgp_notify_major(bgpn.bgpn_major));
776 printf(" subcode %s",
777 bgp_notify_minor(bgpn.bgpn_major, bgpn.bgpn_minor));
778 return;
779 trunc:
780 printf("[|BGP]");
781 }
782
783 static void
784 bgp_header_print(const u_char *dat, int length)
785 {
786 struct bgp bgp;
787
788 TCHECK2(dat[0], BGP_SIZE);
789 memcpy(&bgp, dat, BGP_SIZE);
790 printf("(%s", bgp_type(bgp.bgp_type)); /* ) */
791
792 switch (bgp.bgp_type) {
793 case BGP_OPEN:
794 bgp_open_print(dat, length);
795 break;
796 case BGP_UPDATE:
797 bgp_update_print(dat, length);
798 break;
799 case BGP_NOTIFICATION:
800 bgp_notification_print(dat, length);
801 break;
802 }
803
804 /* ( */
805 printf(")");
806 return;
807 trunc:
808 printf("[|BGP]");
809 }
810
811 void
812 bgp_print(const u_char *dat, int length)
813 {
814 const u_char *p;
815 const u_char *ep;
816 const u_char *start;
817 const u_char marker[] = {
818 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
819 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
820 };
821 struct bgp bgp;
822 u_int16_t hlen;
823 int newline;
824
825 ep = dat + length;
826 if (snapend < dat + length)
827 ep = snapend;
828
829 printf(": BGP");
830
831 p = dat;
832 newline = 0;
833 start = p;
834 while (p < snapend) {
835 if (!TTEST2(p[0], 1))
836 break;
837 if (p[0] != 0xff) {
838 p++;
839 continue;
840 }
841
842 if (!TTEST2(p[0], sizeof(marker)))
843 break;
844 if (memcmp(p, marker, sizeof(marker)) != 0) {
845 p++;
846 continue;
847 }
848
849 /* found BGP header */
850 TCHECK2(p[0], BGP_SIZE); /*XXX*/
851 memcpy(&bgp, p, BGP_SIZE);
852
853 if (start != p)
854 printf(" [|BGP]");
855
856 hlen = ntohs(bgp.bgp_len);
857 if (vflag && newline)
858 printf("\n\t");
859 else
860 printf(" ");
861 if (TTEST2(p[0], hlen)) {
862 bgp_header_print(p, hlen);
863 newline = 1;
864 p += hlen;
865 start = p;
866 } else {
867 printf("[|BGP %s]", bgp_type(bgp.bgp_type));
868 break;
869 }
870 }
871
872 return;
873
874 trunc:
875 printf(" [|BGP]");
876 }
877
878
879
880
881
882