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.28 2002-05-16 10:20:47 guy 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 */
78 #define BGP_OPEN_SIZE 29 /* unaligned */
85 #define BGP_OPT_SIZE 2 /* some compilers may pad to 4 bytes */
87 struct bgp_notification
{
88 u_int8_t bgpn_marker
[16];
93 /* data should follow */
95 #define BGP_NOTIFICATION_SIZE 21 /* unaligned */
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)
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 */
128 static const char *bgptype
[] = {
129 NULL
, "OPEN", "UPDATE", "NOTIFICATION", "KEEPALIVE",
131 #define bgp_type(x) num_or_str(bgptype, sizeof(bgptype)/sizeof(bgptype[0]), (x))
133 static const char *bgpopt_type
[] = {
134 NULL
, "Authentication Information", "Capabilities Advertisement",
136 #define bgp_opttype(x) \
137 num_or_str(bgpopt_type, sizeof(bgpopt_type)/sizeof(bgpopt_type[0]), (x))
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",
145 #define bgp_notify_major(x) \
146 num_or_str(bgpnotify_major, \
147 sizeof(bgpnotify_major)/sizeof(bgpnotify_major[0]), (x))
149 static const char *bgpnotify_minor_1
[] = {
150 NULL
, "Connection Not Synchronized",
151 "Bad Message Length", "Bad Message Type",
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",
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",
170 static const char **bgpnotify_minor
[] = {
171 NULL
, bgpnotify_minor_1
, bgpnotify_minor_2
, bgpnotify_minor_3
,
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]),
179 static const char *bgpattr_origin
[] = {
180 "IGP", "EGP", "INCOMPLETE",
182 #define bgp_attr_origin(x) \
183 num_or_str(bgpattr_origin, \
184 sizeof(bgpattr_origin)/sizeof(bgpattr_origin[0]), (x))
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",
192 #define bgp_attr_type(x) \
193 num_or_str(bgpattr_type, \
194 sizeof(bgpattr_type)/sizeof(bgpattr_type[0]), (x))
196 /* Subsequent address family identifier, RFC2283 section 7 */
198 #define SAFNUM_UNICAST 1
199 #define SAFNUM_MULTICAST 2
200 #define SAFNUM_UNIMULTICAST 3
201 /* labeled BGP RFC3107 */
202 #define SAFNUM_LABUNICAST 4
204 static const char *bgpattr_nlri_safi
[] = {
205 "Reserved", "Unicast", "Multicast", "Unicast+Multicast", "labeled Unicast"
207 #define bgp_attr_nlri_safi(x) \
208 num_or_str(bgpattr_nlri_safi, \
209 sizeof(bgpattr_nlri_safi)/sizeof(bgpattr_nlri_safi[0]), (x))
211 /* well-known community */
212 #define BGP_COMMUNITY_NO_EXPORT 0xffffff01
213 #define BGP_COMMUNITY_NO_ADVERT 0xffffff02
214 #define BGP_COMMUNITY_NO_EXPORT_SUBCONFED 0xffffff03
216 /* RFC1700 address family numbers */
218 #define AFNUM_INET6 2
221 #define AFNUM_BBN1822 5
226 #define AFNUM_X121 10
228 #define AFNUM_ATALK 12
229 #define AFNUM_DECNET 13
230 #define AFNUM_BANYAN 14
231 #define AFNUM_E164NSAP 15
233 static const char *afnumber
[] = {
234 "Reserved", "IPv4", "IPv6", "NSAP", "HDLC",
235 "BBN 1822", "802", "E.163", "E.164", "F.69",
236 "X.121", "IPX", "Appletalk", "Decnet IV", "Banyan Vines",
237 "E.164 with NSAP subaddress",
240 (((x) == 65535) ? afnumber[0] : \
241 num_or_str(afnumber, \
242 sizeof(afnumber)/sizeof(afnumber[0]), (x)))
246 num_or_str(const char **table
, size_t siz
, int value
)
249 if (value
< 0 || siz
<= value
|| table
[value
] == NULL
) {
250 snprintf(buf
, sizeof(buf
), "#%d", value
);
257 bgp_notify_minor(int major
, int minor
)
259 static const char **table
;
265 && major
< sizeof(bgpnotify_minor
)/sizeof(bgpnotify_minor
[0])
266 && bgpnotify_minor
[major
]) {
267 table
= bgpnotify_minor
[major
];
268 siz
= bgpnotify_minor_siz
[major
];
269 if (0 <= minor
&& minor
< siz
&& table
[minor
])
276 snprintf(buf
, sizeof(buf
), "#%d", minor
);
283 decode_prefix4(const u_char
*pd
, char *buf
, u_int buflen
)
289 if (plen
< 0 || 32 < plen
)
292 memset(&addr
, 0, sizeof(addr
));
293 memcpy(&addr
, &pd
[1], (plen
+ 7) / 8);
295 ((u_char
*)&addr
)[(plen
+ 7) / 8 - 1] &=
296 ((0xff00 >> (plen
% 8)) & 0xff);
298 snprintf(buf
, buflen
, "%s/%d", getname((u_char
*)&addr
), plen
);
299 return 1 + (plen
+ 7) / 8;
303 decode_labeled_prefix4(const u_char
*pd
, char *buf
, u_int buflen
)
308 plen
= pd
[0]; /* get prefix length */
310 /* this is one of the weirdnesses of rfc3107
311 the label length (actually the label + COS bits)
312 is added of the prefix length;
313 hence we do nod check for 0-32 but rather
314 for 24-56 - hannes@juniper.net
316 if (plen
< 24 || 56 < plen
)
318 plen
-=24; /* adjust prefixlen - labellength */
320 memset(&addr
, 0, sizeof(addr
));
321 memcpy(&addr
, &pd
[4], (plen
+ 7) / 8);
323 ((u_char
*)&addr
)[(plen
+ 7) / 8 - 1] &=
324 ((0xff00 >> (plen
% 8)) & 0xff);
326 /* the label may get offsetted by 4 bits so lets shift it right */
327 snprintf(buf
, buflen
, "%s/%d (label:%u EXP:%u, S:%u)",
328 getname((u_char
*)&addr
),
330 EXTRACT_24BITS(pd
+1)>>4,
333 return 4 + (plen
+ 7) / 8;
338 decode_prefix6(const u_char
*pd
, char *buf
, u_int buflen
)
340 struct in6_addr addr
;
344 if (plen
< 0 || 128 < plen
)
347 memset(&addr
, 0, sizeof(addr
));
348 memcpy(&addr
, &pd
[1], (plen
+ 7) / 8);
350 addr
.s6_addr
[(plen
+ 7) / 8 - 1] &=
351 ((0xff00 >> (plen
% 8)) & 0xff);
353 snprintf(buf
, buflen
, "%s/%d", getname6((u_char
*)&addr
), plen
);
354 return 1 + (plen
+ 7) / 8;
359 bgp_attr_print(const struct bgp_attr
*attr
, const u_char
*dat
, int len
)
367 char buf
[MAXHOSTNAMELEN
+ 100];
371 switch (attr
->bgpa_type
) {
374 printf(" invalid len");
376 printf(" %s", bgp_attr_origin(p
[0]));
378 case BGPTYPE_AS_PATH
:
380 printf(" invalid len");
383 while (p
< dat
+ len
) {
385 * under RFC1965, p[0] means:
386 * 1: AS_SET 2: AS_SEQUENCE
387 * 3: AS_CONFED_SET 4: AS_CONFED_SEQUENCE
390 if (p
[0] == 3 || p
[0] == 4)
392 printf("%s", (p
[0] & 1) ? "{" : "");
393 for (i
= 0; i
< p
[1] * 2; i
+= 2) {
394 printf("%s%u", i
== 0 ? "" : " ",
395 EXTRACT_16BITS(&p
[2 + i
]));
397 printf("%s", (p
[0] & 1) ? "}" : "");
401 case BGPTYPE_NEXT_HOP
:
403 printf(" invalid len");
405 printf(" %s", getname(p
));
407 case BGPTYPE_MULTI_EXIT_DISC
:
408 case BGPTYPE_LOCAL_PREF
:
410 printf(" invalid len");
412 printf(" %u", EXTRACT_32BITS(p
));
414 case BGPTYPE_ATOMIC_AGGREGATE
:
416 printf(" invalid len");
418 case BGPTYPE_AGGREGATOR
:
420 printf(" invalid len");
423 printf(" AS #%u, origin %s", EXTRACT_16BITS(p
),
426 case BGPTYPE_COMMUNITIES
:
428 printf(" invalid len");
431 for (i
= 0; i
< len
; i
+= 4) {
433 comm
= EXTRACT_32BITS(&p
[i
]);
435 case BGP_COMMUNITY_NO_EXPORT
:
436 printf(" NO_EXPORT");
438 case BGP_COMMUNITY_NO_ADVERT
:
439 printf(" NO_ADVERTISE");
441 case BGP_COMMUNITY_NO_EXPORT_SUBCONFED
:
442 printf(" NO_EXPORT_SUBCONFED");
445 printf(" (AS #%d value 0x%04x)",
446 (comm
>> 16) & 0xffff, comm
& 0xffff);
451 case BGPTYPE_MP_REACH_NLRI
:
452 af
= EXTRACT_16BITS(p
);
455 printf(" %s vendor specific,", af_name(af
));
457 printf(" AFI %s SAFI %s,", af_name(af
),
458 bgp_attr_nlri_safi(safi
));
462 if (af
== AFNUM_INET
)
465 else if (af
== AFNUM_INET6
)
478 printf(" %s", getname(p
+ 1 + i
));
479 i
+= sizeof(struct in_addr
);
483 printf(" %s", getname6(p
+ 1 + i
));
484 i
+= sizeof(struct in6_addr
);
488 printf(" (unknown af)");
489 i
= tlen
; /*exit loop*/
500 printf(" %u snpa", snpa
);
501 for (/*nothing*/; snpa
> 0; snpa
--) {
502 printf("(%d bytes)", p
[0]);
511 while (len
- (p
- dat
) > 0) {
514 if(safi
==SAFNUM_LABUNICAST
) {
515 advance
= decode_labeled_prefix4(p
, buf
, sizeof(buf
));
517 advance
= decode_prefix4(p
, buf
, sizeof(buf
));
525 advance
= decode_prefix6(p
, buf
, sizeof(buf
));
530 printf(" (unknown af)");
541 case BGPTYPE_MP_UNREACH_NLRI
:
542 af
= EXTRACT_16BITS(p
);
545 printf(" %s vendor specific,", af_name(af
));
547 printf(" AFI %s SAFI %s,", af_name(af
),
548 bgp_attr_nlri_safi(safi
));
553 while (len
- (p
- dat
) > 0) {
556 if(safi
==SAFNUM_LABUNICAST
) {
557 advance
= decode_labeled_prefix4(p
, buf
, sizeof(buf
));
559 advance
= decode_prefix4(p
, buf
, sizeof(buf
));
567 advance
= decode_prefix6(p
, buf
, sizeof(buf
));
572 printf(" (unknown af)");
587 bgp_open_print(const u_char
*dat
, int length
)
589 struct bgp_open bgpo
;
590 struct bgp_opt bgpopt
;
595 TCHECK2(dat
[0], BGP_OPEN_SIZE
);
596 memcpy(&bgpo
, dat
, BGP_OPEN_SIZE
);
597 hlen
= ntohs(bgpo
.bgpo_len
);
599 printf(": Version %d,", bgpo
.bgpo_version
);
600 printf(" AS #%u,", ntohs(bgpo
.bgpo_myas
));
601 printf(" Holdtime %u,", ntohs(bgpo
.bgpo_holdtime
));
602 printf(" ID %s,", getname((u_char
*)&bgpo
.bgpo_id
));
603 printf(" Option length %u", bgpo
.bgpo_optlen
);
606 opt
= &((const struct bgp_open
*)dat
)->bgpo_optlen
;
610 while (i
< bgpo
.bgpo_optlen
) {
611 TCHECK2(opt
[i
], BGP_OPT_SIZE
);
612 memcpy(&bgpopt
, &opt
[i
], BGP_OPT_SIZE
);
613 if (i
+ 2 + bgpopt
.bgpopt_len
> bgpo
.bgpo_optlen
) {
614 printf(" [|opt %d %d]", bgpopt
.bgpopt_len
, bgpopt
.bgpopt_type
);
618 printf(" (option %s, len=%d)", bgp_opttype(bgpopt
.bgpopt_type
),
620 i
+= BGP_OPT_SIZE
+ bgpopt
.bgpopt_len
;
628 bgp_update_print(const u_char
*dat
, int length
)
631 struct bgp_attr bgpa
;
638 TCHECK2(dat
[0], BGP_SIZE
);
639 memcpy(&bgp
, dat
, BGP_SIZE
);
640 hlen
= ntohs(bgp
.bgp_len
);
641 p
= dat
+ BGP_SIZE
; /*XXX*/
644 /* Unfeasible routes */
645 len
= EXTRACT_16BITS(p
);
648 * Without keeping state from the original NLRI message,
649 * it's not possible to tell if this a v4 or v6 route,
650 * so only try to decode it if we're not v6 enabled.
653 printf(" (Withdrawn routes: %d bytes)", len
);
655 char buf
[MAXHOSTNAMELEN
+ 100];
660 printf(" (Withdrawn routes:");
663 i
+= decode_prefix4(&p
[i
], buf
, sizeof(buf
));
672 len
= EXTRACT_16BITS(p
);
674 /* do something more useful!*/
676 printf(" (Path attributes:"); /* ) */
678 while (i
< 2 + len
) {
681 TCHECK2(p
[i
], sizeof(bgpa
));
682 memcpy(&bgpa
, &p
[i
], sizeof(bgpa
));
683 alen
= bgp_attr_len(&bgpa
);
684 aoff
= bgp_attr_off(&bgpa
);
686 if (vflag
&& newline
)
691 printf("%s", bgp_attr_type(bgpa
.bgpa_type
));
692 if (bgpa
.bgpa_flags
) {
694 bgpa
.bgpa_flags
& 0x80 ? "O" : "",
695 bgpa
.bgpa_flags
& 0x40 ? "T" : "",
696 bgpa
.bgpa_flags
& 0x20 ? "P" : "",
697 bgpa
.bgpa_flags
& 0x10 ? "E" : "");
698 if (bgpa
.bgpa_flags
& 0xf)
699 printf("+%x", bgpa
.bgpa_flags
& 0xf);
703 bgp_attr_print(&bgpa
, &p
[i
+ aoff
], alen
);
717 if (len
&& dat
+ length
> p
)
719 if (dat
+ length
> p
) {
720 printf("(NLRI:"); /* ) */
721 while (dat
+ length
> p
) {
722 char buf
[MAXHOSTNAMELEN
+ 100];
723 i
= decode_prefix4(p
, buf
, sizeof(buf
));
739 bgp_notification_print(const u_char
*dat
, int length
)
741 struct bgp_notification bgpn
;
744 TCHECK2(dat
[0], BGP_NOTIFICATION_SIZE
);
745 memcpy(&bgpn
, dat
, BGP_NOTIFICATION_SIZE
);
746 hlen
= ntohs(bgpn
.bgpn_len
);
748 printf(": error %s,", bgp_notify_major(bgpn
.bgpn_major
));
749 printf(" subcode %s",
750 bgp_notify_minor(bgpn
.bgpn_major
, bgpn
.bgpn_minor
));
757 bgp_header_print(const u_char
*dat
, int length
)
761 TCHECK2(dat
[0], BGP_SIZE
);
762 memcpy(&bgp
, dat
, BGP_SIZE
);
763 printf("(%s", bgp_type(bgp
.bgp_type
)); /* ) */
765 switch (bgp
.bgp_type
) {
767 bgp_open_print(dat
, length
);
770 bgp_update_print(dat
, length
);
772 case BGP_NOTIFICATION
:
773 bgp_notification_print(dat
, length
);
785 bgp_print(const u_char
*dat
, int length
)
790 const u_char marker
[] = {
791 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
792 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
799 if (snapend
< dat
+ length
)
807 while (p
< snapend
) {
808 if (!TTEST2(p
[0], 1))
815 if (!TTEST2(p
[0], sizeof(marker
)))
817 if (memcmp(p
, marker
, sizeof(marker
)) != 0) {
822 /* found BGP header */
823 TCHECK2(p
[0], BGP_SIZE
); /*XXX*/
824 memcpy(&bgp
, p
, BGP_SIZE
);
829 hlen
= ntohs(bgp
.bgp_len
);
830 if (vflag
&& newline
)
834 if (TTEST2(p
[0], hlen
)) {
835 bgp_header_print(p
, hlen
);
840 printf("[|BGP %s]", bgp_type(bgp
.bgp_type
));