2 * Copyright (C) 1999 WIDE Project.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
35 static const char rcsid
[] =
36 "@(#) $Header: /tcpdump/master/tcpdump/print-bgp.c,v 1.20 2000-12-04 05:27:49 fenner Exp $";
39 #include <sys/param.h>
41 #include <sys/types.h>
42 #include <sys/socket.h>
44 #include <netinet/in.h>
51 #include "interface.h"
52 #include "addrtoname.h"
56 u_int8_t bgp_marker
[16];
60 #define BGP_SIZE 19 /* unaligned */
64 #define BGP_NOTIFICATION 3
65 #define BGP_KEEPALIVE 4
68 u_int8_t bgpo_marker
[16];
71 u_int8_t bgpo_version
;
73 u_int16_t bgpo_holdtime
;
76 /* options should follow */
85 struct bgp_notification
{
86 u_int8_t bgpn_marker
[16];
91 /* data should follow */
101 #define bgp_attr_len(p) \
102 (((p)->bgpa_flags & 0x10) ? \
103 ntohs((p)->bgpa_len.elen) : (p)->bgpa_len.len)
104 #define bgp_attr_off(p) \
105 (((p)->bgpa_flags & 0x10) ? 4 : 3)
108 #define BGPTYPE_ORIGIN 1
109 #define BGPTYPE_AS_PATH 2
110 #define BGPTYPE_NEXT_HOP 3
111 #define BGPTYPE_MULTI_EXIT_DISC 4
112 #define BGPTYPE_LOCAL_PREF 5
113 #define BGPTYPE_ATOMIC_AGGREGATE 6
114 #define BGPTYPE_AGGREGATOR 7
115 #define BGPTYPE_COMMUNITIES 8 /* RFC1997 */
116 #define BGPTYPE_ORIGINATOR_ID 9 /* RFC1998 */
117 #define BGPTYPE_CLUSTER_LIST 10 /* RFC1998 */
118 #define BGPTYPE_DPA 11 /* work in progress */
119 #define BGPTYPE_ADVERTISERS 12 /* RFC1863 */
120 #define BGPTYPE_RCID_PATH 13 /* RFC1863 */
121 #define BGPTYPE_MP_REACH_NLRI 14 /* RFC2283 */
122 #define BGPTYPE_MP_UNREACH_NLRI 15 /* RFC2283 */
125 static const char *bgptype
[] = {
126 NULL
, "OPEN", "UPDATE", "NOTIFICATION", "KEEPALIVE",
128 #define bgp_type(x) num_or_str(bgptype, sizeof(bgptype)/sizeof(bgptype[0]), (x))
130 static const char *bgpopt_type
[] = {
131 NULL
, "Authentication Information",
133 #define bgp_opttype(x) \
134 num_or_str(bgpopt_type, sizeof(bgpopt_type)/sizeof(bgpopt_type[0]), (x))
136 static const char *bgpnotify_major
[] = {
137 NULL
, "Message Header Error",
138 "OPEN Message Error", "UPDATE Message Error",
139 "Hold Timer Expired", "Finite State Machine Error",
142 #define bgp_notify_major(x) \
143 num_or_str(bgpnotify_major, \
144 sizeof(bgpnotify_major)/sizeof(bgpnotify_major[0]), (x))
146 static const char *bgpnotify_minor_1
[] = {
147 NULL
, "Connection Not Synchronized",
148 "Bad Message Length", "Bad Message Type",
151 static const char *bgpnotify_minor_2
[] = {
152 NULL
, "Unsupported Version Number",
153 "Bad Peer AS", "Bad BGP Identifier",
154 "Unsupported Optional Parameter", "Authentication Failure",
155 "Unacceptable Hold Time",
158 static const char *bgpnotify_minor_3
[] = {
159 NULL
, "Malformed Attribute List",
160 "Unrecognized Well-known Attribute", "Missing Well-known Attribute",
161 "Attribute Flags Error", "Attribute Length Error",
162 "Invalid ORIGIN Attribute", "AS Routing Loop",
163 "Invalid NEXT_HOP Attribute", "Optional Attribute Error",
164 "Invalid Network Field", "Malformed AS_PATH",
167 static const char **bgpnotify_minor
[] = {
168 NULL
, bgpnotify_minor_1
, bgpnotify_minor_2
, bgpnotify_minor_3
,
170 static const int bgpnotify_minor_siz
[] = {
171 0, sizeof(bgpnotify_minor_1
)/sizeof(bgpnotify_minor_1
[0]),
172 sizeof(bgpnotify_minor_2
)/sizeof(bgpnotify_minor_2
[0]),
173 sizeof(bgpnotify_minor_3
)/sizeof(bgpnotify_minor_3
[0]),
176 static const char *bgpattr_origin
[] = {
177 "IGP", "EGP", "INCOMPLETE",
179 #define bgp_attr_origin(x) \
180 num_or_str(bgpattr_origin, \
181 sizeof(bgpattr_origin)/sizeof(bgpattr_origin[0]), (x))
183 static const char *bgpattr_type
[] = {
184 NULL
, "ORIGIN", "AS_PATH", "NEXT_HOP",
185 "MULTI_EXIT_DISC", "LOCAL_PREF", "ATOMIC_AGGREGATE", "AGGREGATOR",
186 "COMMUNITIES", "ORIGINATOR_ID", "CLUSTER_LIST", "DPA",
187 "ADVERTISERS", "RCID_PATH", "MP_REACH_NLRI", "MP_UNREACH_NLRI",
189 #define bgp_attr_type(x) \
190 num_or_str(bgpattr_type, \
191 sizeof(bgpattr_type)/sizeof(bgpattr_type[0]), (x))
193 /* Subsequent address family identifier, RFC2283 section 7 */
194 static const char *bgpattr_nlri_safi
[] = {
195 "Reserved", "Unicast", "Multicast", "Unicast+Multicast",
197 #define bgp_attr_nlri_safi(x) \
198 num_or_str(bgpattr_nlri_safi, \
199 sizeof(bgpattr_nlri_safi)/sizeof(bgpattr_nlri_safi[0]), (x))
201 /* well-known community */
202 #define BGP_COMMUNITY_NO_EXPORT 0xffffff01
203 #define BGP_COMMUNITY_NO_ADVERT 0xffffff02
204 #define BGP_COMMUNITY_NO_EXPORT_SUBCONFED 0xffffff03
206 /* RFC1700 address family numbers */
208 #define AFNUM_INET6 2
211 #define AFNUM_BBN1822 5
216 #define AFNUM_X121 10
218 #define AFNUM_ATALK 12
219 #define AFNUM_DECNET 13
220 #define AFNUM_BANYAN 14
221 #define AFNUM_E164NSAP 15
223 static const char *afnumber
[] = {
224 "Reserved", "IPv4", "IPv6", "NSAP", "HDLC",
225 "BBN 1822", "802", "E.163", "E.164", "F.69",
226 "X.121", "IPX", "Appletalk", "Decnet IV", "Banyan Vines",
227 "E.164 with NSAP subaddress",
230 (((x) == 65535) ? afnumber[0] : \
231 num_or_str(afnumber, \
232 sizeof(afnumber)/sizeof(afnumber[0]), (x)))
236 num_or_str(const char **table
, size_t siz
, int value
)
239 if (value
< 0 || siz
<= value
|| table
[value
] == NULL
) {
240 snprintf(buf
, sizeof(buf
), "#%d", value
);
247 bgp_notify_minor(int major
, int minor
)
249 static const char **table
;
255 && major
< sizeof(bgpnotify_minor
)/sizeof(bgpnotify_minor
[0])
256 && bgpnotify_minor
[major
]) {
257 table
= bgpnotify_minor
[major
];
258 siz
= bgpnotify_minor_siz
[major
];
259 if (0 <= minor
&& minor
< siz
&& table
[minor
])
266 snprintf(buf
, sizeof(buf
), "#%d", minor
);
273 decode_prefix4(const u_char
*pd
, char *buf
, int buflen
)
279 if (plen
< 0 || 32 < plen
)
282 memset(&addr
, 0, sizeof(addr
));
283 memcpy(&addr
, &pd
[1], (plen
+ 7) / 8);
285 ((u_char
*)&addr
)[(plen
+ 7) / 8 - 1] &=
286 ((0xff00 >> (plen
% 8)) & 0xff);
288 snprintf(buf
, buflen
, "%s/%d", getname((u_char
*)&addr
), plen
);
289 return 1 + (plen
+ 7) / 8;
294 decode_prefix6(const u_char
*pd
, char *buf
, int buflen
)
296 struct in6_addr addr
;
300 if (plen
< 0 || 128 < plen
)
303 memset(&addr
, 0, sizeof(addr
));
304 memcpy(&addr
, &pd
[1], (plen
+ 7) / 8);
306 addr
.s6_addr
[(plen
+ 7) / 8 - 1] &=
307 ((0xff00 >> (plen
% 8)) & 0xff);
309 snprintf(buf
, buflen
, "%s/%d", getname6((char *)&addr
), plen
);
310 return 1 + (plen
+ 7) / 8;
315 bgp_attr_print(const struct bgp_attr
*attr
, const u_char
*dat
, int len
)
323 char buf
[MAXHOSTNAMELEN
+ 100];
327 switch (attr
->bgpa_type
) {
330 printf(" invalid len");
332 printf(" %s", bgp_attr_origin(p
[0]));
334 case BGPTYPE_AS_PATH
:
336 printf(" invalid len");
339 while (p
< dat
+ len
) {
341 * under RFC1965, p[0] means:
342 * 1: AS_SET 2: AS_SEQUENCE
343 * 3: AS_CONFED_SET 4: AS_CONFED_SEQUENCE
346 if (p
[0] == 3 || p
[0] == 4)
348 printf("%s", (p
[0] & 1) ? "{" : "");
349 for (i
= 0; i
< p
[1]; i
+= 2) {
350 printf("%s%u", i
== 0 ? "" : " ",
351 ntohs(*(u_int16_t
*)&p
[2 + i
]));
353 printf("%s", (p
[0] & 1) ? "}" : "");
357 case BGPTYPE_NEXT_HOP
:
359 printf(" invalid len");
361 printf(" %s", getname(p
));
363 case BGPTYPE_MULTI_EXIT_DISC
:
364 case BGPTYPE_LOCAL_PREF
:
366 printf(" invalid len");
368 printf(" %u", (u_int32_t
)ntohl(*(u_int32_t
*)p
));
370 case BGPTYPE_ATOMIC_AGGREGATE
:
372 printf(" invalid len");
374 case BGPTYPE_AGGREGATOR
:
376 printf(" invalid len");
379 printf(" AS #%u, origin %s", ntohs(*(u_int16_t
*)p
),
382 case BGPTYPE_COMMUNITIES
:
384 printf(" invalid len");
387 for (i
= 0; i
< len
; i
+= 4) {
389 comm
= (u_int32_t
)ntohl(*(u_int32_t
*)&p
[i
]);
391 case BGP_COMMUNITY_NO_EXPORT
:
392 printf(" NO_EXPORT");
394 case BGP_COMMUNITY_NO_ADVERT
:
395 printf(" NO_ADVERTISE");
397 case BGP_COMMUNITY_NO_EXPORT_SUBCONFED
:
398 printf(" NO_EXPORT_SUBCONFED");
401 printf(" (AS #%d value 0x%04x)",
402 (comm
>> 16) & 0xffff, comm
& 0xffff);
407 case BGPTYPE_MP_REACH_NLRI
:
408 af
= ntohs(*(u_int16_t
*)p
);
411 printf(" %s vendor specific,", af_name(af
));
413 printf(" %s %s,", af_name(af
),
414 bgp_attr_nlri_safi(safi
));
418 if (af
== AFNUM_INET
)
421 else if (af
== AFNUM_INET6
)
434 printf(" %s", getname(p
+ 1 + i
));
435 i
+= sizeof(struct in_addr
);
439 printf(" %s", getname6(p
+ 1 + i
));
440 i
+= sizeof(struct in6_addr
);
444 printf(" (unknown af)");
445 i
= tlen
; /*exit loop*/
456 printf(" %u snpa", snpa
);
457 for (/*nothing*/; snpa
> 0; snpa
--) {
458 printf("(%d bytes)", p
[0]);
465 while (len
- (p
- dat
) > 0) {
468 advance
= decode_prefix4(p
, buf
, sizeof(buf
));
473 advance
= decode_prefix6(p
, buf
, sizeof(buf
));
478 printf(" (unknown af)");
489 case BGPTYPE_MP_UNREACH_NLRI
:
490 af
= ntohs(*(u_int16_t
*)p
);
493 printf(" %s vendor specific,", af_name(af
));
495 printf(" %s %s,", af_name(af
),
496 bgp_attr_nlri_safi(safi
));
501 while (len
- (p
- dat
) > 0) {
504 advance
= decode_prefix4(p
, buf
, sizeof(buf
));
509 advance
= decode_prefix6(p
, buf
, sizeof(buf
));
514 printf(" (unknown af)");
529 bgp_open_print(const u_char
*dat
, int length
)
531 struct bgp_open bgpo
;
532 struct bgp_opt bgpopt
;
537 TCHECK2(dat
[0], sizeof(bgpo
));
538 memcpy(&bgpo
, dat
, sizeof(bgpo
));
539 hlen
= ntohs(bgpo
.bgpo_len
);
541 printf(": Version %d,", bgpo
.bgpo_version
);
542 printf(" AS #%u,", ntohs(bgpo
.bgpo_myas
));
543 printf(" Holdtime %u,", ntohs(bgpo
.bgpo_holdtime
));
544 printf(" ID %s,", getname((u_char
*)&bgpo
.bgpo_id
));
545 printf(" Option length %u", bgpo
.bgpo_optlen
);
548 opt
= &((struct bgp_open
*)dat
)->bgpo_optlen
;
551 for (i
= 0; i
< bgpo
.bgpo_optlen
; i
++) {
552 memcpy(&bgpopt
, &opt
[i
], sizeof(bgpopt
));
553 if (i
+ 2 + bgpopt
.bgpopt_len
> bgpo
.bgpo_optlen
) {
554 printf(" [|opt %d %d]", bgpopt
.bgpopt_len
, bgpopt
.bgpopt_type
);
558 printf(" (option %s, len=%d)", bgp_opttype(bgpopt
.bgpopt_type
),
560 i
+= sizeof(bgpopt
) + bgpopt
.bgpopt_len
;
568 bgp_update_print(const u_char
*dat
, int length
)
571 struct bgp_attr bgpa
;
578 TCHECK2(dat
[0], sizeof(bgp
));
579 memcpy(&bgp
, dat
, sizeof(bgp
));
580 hlen
= ntohs(bgp
.bgp_len
);
581 p
= dat
+ BGP_SIZE
; /*XXX*/
584 /* Unfeasible routes */
585 len
= EXTRACT_16BITS(p
);
587 /* Without keeping state from the original NLRI message,
588 * it's not possible to tell if this a v4 or v6 route,
589 * so only try to decode it if we're not v6 enabled.
592 printf(" (Withdrawn routes: %d bytes)", len
);
594 char buf
[MAXHOSTNAMELEN
+ 100];
599 printf(" (Withdrawn routes:", len
);
602 i
+= decode_prefix4(&p
[i
], buf
, sizeof(buf
));
611 len
= EXTRACT_16BITS(p
);
613 /* do something more useful!*/
615 printf(" (Path attributes:"); /* ) */
617 while (i
< 2 + len
) {
620 TCHECK2(p
[i
], sizeof(bgpa
));
621 memcpy(&bgpa
, &p
[i
], sizeof(bgpa
));
622 alen
= bgp_attr_len(&bgpa
);
623 aoff
= bgp_attr_off(&bgpa
);
625 if (vflag
&& newline
)
630 printf("%s", bgp_attr_type(bgpa
.bgpa_type
));
631 if (bgpa
.bgpa_flags
) {
633 bgpa
.bgpa_flags
& 0x80 ? "O" : "",
634 bgpa
.bgpa_flags
& 0x40 ? "T" : "",
635 bgpa
.bgpa_flags
& 0x20 ? "P" : "",
636 bgpa
.bgpa_flags
& 0x10 ? "E" : "");
637 if (bgpa
.bgpa_flags
& 0xf)
638 printf("+%x", bgpa
.bgpa_flags
& 0xf);
642 bgp_attr_print(&bgpa
, &p
[i
+ aoff
], alen
);
656 if (len
&& dat
+ length
> p
)
658 if (dat
+ length
> p
) {
659 printf("(NLRI:"); /* ) */
660 while (dat
+ length
> p
) {
661 char buf
[MAXHOSTNAMELEN
+ 100];
662 i
= decode_prefix4(p
, buf
, sizeof(buf
));
678 bgp_notification_print(const u_char
*dat
, int length
)
680 struct bgp_notification bgpn
;
683 TCHECK2(dat
[0], sizeof(bgpn
));
684 memcpy(&bgpn
, dat
, sizeof(bgpn
));
685 hlen
= ntohs(bgpn
.bgpn_len
);
687 printf(": error %s,", bgp_notify_major(bgpn
.bgpn_major
));
688 printf(" subcode %s",
689 bgp_notify_minor(bgpn
.bgpn_major
, bgpn
.bgpn_minor
));
696 bgp_header_print(const u_char
*dat
, int length
)
700 TCHECK2(dat
[0], sizeof(bgp
));
701 memcpy(&bgp
, dat
, sizeof(bgp
));
702 printf("(%s", bgp_type(bgp
.bgp_type
)); /* ) */
704 switch (bgp
.bgp_type
) {
706 bgp_open_print(dat
, length
);
709 bgp_update_print(dat
, length
);
711 case BGP_NOTIFICATION
:
712 bgp_notification_print(dat
, length
);
724 bgp_print(const u_char
*dat
, int length
)
729 const u_char marker
[] = {
730 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
731 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
738 if (snapend
< dat
+ length
)
746 while (p
< snapend
) {
747 if (!TTEST2(p
[0], 1))
754 if (!TTEST2(p
[0], sizeof(marker
)))
756 if (memcmp(p
, marker
, sizeof(marker
)) != 0) {
761 /* found BGP header */
762 TCHECK2(p
[0], sizeof(bgp
)); /*XXX*/
763 memcpy(&bgp
, p
, sizeof(bgp
));
768 hlen
= ntohs(bgp
.bgp_len
);
769 if (vflag
&& newline
)
773 if (TTEST2(p
[0], hlen
)) {
774 bgp_header_print(p
, hlen
);
779 printf("[|BGP %s]", bgp_type(bgp
.bgp_type
));