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.17 2000-09-29 04:58:34 guy Exp $";
39 #include <sys/param.h>
41 #include <sys/types.h>
42 #include <sys/socket.h>
44 #include <netinet/in.h>
50 #include "interface.h"
51 #include "addrtoname.h"
54 u_int8_t bgp_marker
[16];
58 #define BGP_SIZE 19 /* unaligned */
62 #define BGP_NOTIFICATION 3
63 #define BGP_KEEPALIVE 4
66 u_int8_t bgpo_marker
[16];
69 u_int8_t bgpo_version
;
71 u_int16_t bgpo_holdtime
;
74 /* options should follow */
83 struct bgp_notification
{
84 u_int8_t bgpn_marker
[16];
89 /* data should follow */
99 #define bgp_attr_len(p) \
100 (((p)->bgpa_flags & 0x10) ? \
101 ntohs((p)->bgpa_len.elen) : (p)->bgpa_len.len)
102 #define bgp_attr_off(p) \
103 (((p)->bgpa_flags & 0x10) ? 4 : 3)
106 #define BGPTYPE_ORIGIN 1
107 #define BGPTYPE_AS_PATH 2
108 #define BGPTYPE_NEXT_HOP 3
109 #define BGPTYPE_MULTI_EXIT_DISC 4
110 #define BGPTYPE_LOCAL_PREF 5
111 #define BGPTYPE_ATOMIC_AGGREGATE 6
112 #define BGPTYPE_AGGREGATOR 7
113 #define BGPTYPE_COMMUNITIES 8 /* RFC1997 */
114 #define BGPTYPE_ORIGINATOR_ID 9 /* RFC1998 */
115 #define BGPTYPE_CLUSTER_LIST 10 /* RFC1998 */
116 #define BGPTYPE_DPA 11 /* work in progress */
117 #define BGPTYPE_ADVERTISERS 12 /* RFC1863 */
118 #define BGPTYPE_RCID_PATH 13 /* RFC1863 */
119 #define BGPTYPE_MP_REACH_NLRI 14 /* RFC2283 */
120 #define BGPTYPE_MP_UNREACH_NLRI 15 /* RFC2283 */
123 static const char *bgptype
[] = {
124 NULL
, "OPEN", "UPDATE", "NOTIFICATION", "KEEPALIVE",
126 #define bgp_type(x) num_or_str(bgptype, sizeof(bgptype)/sizeof(bgptype[0]), (x))
128 static const char *bgpopt_type
[] = {
129 NULL
, "Authentication Information",
131 #define bgp_opttype(x) \
132 num_or_str(bgpopt_type, sizeof(bgpopt_type)/sizeof(bgpopt_type[0]), (x))
134 static const char *bgpnotify_major
[] = {
135 NULL
, "Message Header Error",
136 "OPEN Message Error", "UPDATE Message Error",
137 "Hold Timer Expired", "Finite State Machine Error",
140 #define bgp_notify_major(x) \
141 num_or_str(bgpnotify_major, \
142 sizeof(bgpnotify_major)/sizeof(bgpnotify_major[0]), (x))
144 static const char *bgpnotify_minor_1
[] = {
145 NULL
, "Connection Not Synchronized",
146 "Bad Message Length", "Bad Message Type",
149 static const char *bgpnotify_minor_2
[] = {
150 NULL
, "Unsupported Version Number",
151 "Bad Peer AS", "Bad BGP Identifier",
152 "Unsupported Optional Parameter", "Authentication Failure",
153 "Unacceptable Hold Time",
156 static const char *bgpnotify_minor_3
[] = {
157 NULL
, "Malformed Attribute List",
158 "Unrecognized Well-known Attribute", "Missing Well-known Attribute",
159 "Attribute Flags Error", "Attribute Length Error",
160 "Invalid ORIGIN Attribute", "AS Routing Loop",
161 "Invalid NEXT_HOP Attribute", "Optional Attribute Error",
162 "Invalid Network Field", "Malformed AS_PATH",
165 static const char **bgpnotify_minor
[] = {
166 NULL
, bgpnotify_minor_1
, bgpnotify_minor_2
, bgpnotify_minor_3
,
168 static const int bgpnotify_minor_siz
[] = {
169 0, sizeof(bgpnotify_minor_1
)/sizeof(bgpnotify_minor_1
[0]),
170 sizeof(bgpnotify_minor_2
)/sizeof(bgpnotify_minor_2
[0]),
171 sizeof(bgpnotify_minor_3
)/sizeof(bgpnotify_minor_3
[0]),
174 static const char *bgpattr_origin
[] = {
175 "IGP", "EGP", "INCOMPLETE",
177 #define bgp_attr_origin(x) \
178 num_or_str(bgpattr_origin, \
179 sizeof(bgpattr_origin)/sizeof(bgpattr_origin[0]), (x))
181 static const char *bgpattr_type
[] = {
182 NULL
, "ORIGIN", "AS_PATH", "NEXT_HOP",
183 "MULTI_EXIT_DISC", "LOCAL_PREF", "ATOMIC_AGGREGATE", "AGGREGATOR",
184 "COMMUNITIES", "ORIGINATOR_ID", "CLUSTER_LIST", "DPA",
185 "ADVERTISERS", "RCID_PATH", "MP_REACH_NLRI", "MP_UNREACH_NLRI",
187 #define bgp_attr_type(x) \
188 num_or_str(bgpattr_type, \
189 sizeof(bgpattr_type)/sizeof(bgpattr_type[0]), (x))
191 /* Subsequent address family identifier, RFC2283 section 7 */
192 static const char *bgpattr_nlri_safi
[] = {
193 "Reserved", "Unicast", "Multicast", "Unicast+Multicast",
195 #define bgp_attr_nlri_safi(x) \
196 num_or_str(bgpattr_nlri_safi, \
197 sizeof(bgpattr_nlri_safi)/sizeof(bgpattr_nlri_safi[0]), (x))
199 /* well-known community */
200 #define BGP_COMMUNITY_NO_EXPORT 0xffffff01
201 #define BGP_COMMUNITY_NO_ADVERT 0xffffff02
202 #define BGP_COMMUNITY_NO_EXPORT_SUBCONFED 0xffffff03
204 /* RFC1700 address family numbers */
206 #define AFNUM_INET6 2
209 #define AFNUM_BBN1822 5
214 #define AFNUM_X121 10
216 #define AFNUM_ATALK 12
217 #define AFNUM_DECNET 13
218 #define AFNUM_BANYAN 14
219 #define AFNUM_E164NSAP 15
221 static const char *afnumber
[] = {
222 "Reserved", "IPv4", "IPv6", "NSAP", "HDLC",
223 "BBN 1822", "802", "E.163", "E.164", "F.69",
224 "X.121", "IPX", "Appletalk", "Decnet IV", "Banyan Vines",
225 "E.164 with NSAP subaddress",
228 (((x) == 65535) ? afnumber[0] : \
229 num_or_str(afnumber, \
230 sizeof(afnumber)/sizeof(afnumber[0]), (x)))
234 num_or_str(const char **table
, size_t siz
, int value
)
237 if (value
< 0 || siz
<= value
|| table
[value
] == NULL
) {
238 snprintf(buf
, sizeof(buf
), "#%d", value
);
245 bgp_notify_minor(int major
, int minor
)
247 static const char **table
;
253 && major
< sizeof(bgpnotify_minor
)/sizeof(bgpnotify_minor
[0])
254 && bgpnotify_minor
[major
]) {
255 table
= bgpnotify_minor
[major
];
256 siz
= bgpnotify_minor_siz
[major
];
257 if (0 <= minor
&& minor
< siz
&& table
[minor
])
264 snprintf(buf
, sizeof(buf
), "#%d", minor
);
271 decode_prefix4(const u_char
*pd
, char *buf
, int buflen
)
277 if (plen
< 0 || 32 < plen
)
280 memset(&addr
, 0, sizeof(addr
));
281 memcpy(&addr
, &pd
[1], (plen
+ 7) / 8);
283 ((u_char
*)&addr
)[(plen
+ 7) / 8 - 1] &=
284 ((0xff00 >> (plen
% 8)) & 0xff);
286 snprintf(buf
, buflen
, "%s/%d", getname((u_char
*)&addr
), plen
);
287 return 1 + (plen
+ 7) / 8;
292 decode_prefix6(const u_char
*pd
, char *buf
, int buflen
)
294 struct in6_addr addr
;
298 if (plen
< 0 || 128 < plen
)
301 memset(&addr
, 0, sizeof(addr
));
302 memcpy(&addr
, &pd
[1], (plen
+ 7) / 8);
304 addr
.s6_addr
[(plen
+ 7) / 8 - 1] &=
305 ((0xff00 >> (plen
% 8)) & 0xff);
307 snprintf(buf
, buflen
, "%s/%d", getname6((char *)&addr
), plen
);
308 return 1 + (plen
+ 7) / 8;
313 bgp_attr_print(const struct bgp_attr
*attr
, const u_char
*dat
, int len
)
325 switch (attr
->bgpa_type
) {
328 printf(" invalid len");
330 printf(" %s", bgp_attr_origin(p
[0]));
332 case BGPTYPE_AS_PATH
:
334 printf(" invalid len");
337 while (p
< dat
+ len
) {
339 * under RFC1965, p[0] means:
340 * 1: AS_SET 2: AS_SEQUENCE
341 * 3: AS_CONFED_SET 4: AS_CONFED_SEQUENCE
344 if (p
[0] == 3 || p
[0] == 4)
346 printf("%s", (p
[0] & 1) ? "{" : "");
347 for (i
= 0; i
< p
[1]; i
+= 2) {
348 printf("%s%u", i
== 0 ? "" : " ",
349 ntohs(*(u_int16_t
*)&p
[2 + i
]));
351 printf("%s", (p
[0] & 1) ? "}" : "");
355 case BGPTYPE_NEXT_HOP
:
357 printf(" invalid len");
359 printf(" %s", getname(p
));
361 case BGPTYPE_MULTI_EXIT_DISC
:
362 case BGPTYPE_LOCAL_PREF
:
364 printf(" invalid len");
366 printf(" %u", (u_int32_t
)ntohl(*(u_int32_t
*)p
));
368 case BGPTYPE_ATOMIC_AGGREGATE
:
370 printf(" invalid len");
372 case BGPTYPE_AGGREGATOR
:
374 printf(" invalid len");
377 printf(" AS #%u, origin %s", ntohs(*(u_int16_t
*)p
),
380 case BGPTYPE_COMMUNITIES
:
382 printf(" invalid len");
385 for (i
= 0; i
< len
; i
+= 4) {
387 comm
= (u_int32_t
)ntohl(*(u_int32_t
*)&p
[i
]);
389 case BGP_COMMUNITY_NO_EXPORT
:
390 printf(" NO_EXPORT");
392 case BGP_COMMUNITY_NO_ADVERT
:
393 printf(" NO_ADVERTISE");
395 case BGP_COMMUNITY_NO_EXPORT_SUBCONFED
:
396 printf(" NO_EXPORT_SUBCONFED");
399 printf(" (AS #%d value 0x%04x)",
400 (comm
>> 16) & 0xffff, comm
& 0xffff);
405 case BGPTYPE_MP_REACH_NLRI
:
406 af
= ntohs(*(u_int16_t
*)p
);
409 printf(" %s vendor specific,", af_name(af
));
411 printf(" %s %s,", af_name(af
),
412 bgp_attr_nlri_safi(safi
));
416 if (af
== AFNUM_INET
)
419 else if (af
== AFNUM_INET6
)
432 printf(" %s", getname(p
+ 1 + i
));
433 i
+= sizeof(struct in_addr
);
437 printf(" %s", getname6(p
+ 1 + i
));
438 i
+= sizeof(struct in6_addr
);
442 printf(" (unknown af)");
443 i
= tlen
; /*exit loop*/
454 printf(" %u snpa", snpa
);
455 for (/*nothing*/; snpa
> 0; snpa
--) {
456 printf("(%d bytes)", p
[0]);
463 while (len
- (p
- dat
) > 0) {
466 advance
= decode_prefix4(p
, buf
, sizeof(buf
));
471 advance
= decode_prefix6(p
, buf
, sizeof(buf
));
476 printf(" (unknown af)");
487 case BGPTYPE_MP_UNREACH_NLRI
:
488 af
= ntohs(*(u_int16_t
*)p
);
491 printf(" %s vendor specific,", af_name(af
));
493 printf(" %s %s,", af_name(af
),
494 bgp_attr_nlri_safi(safi
));
499 while (len
- (p
- dat
) > 0) {
502 advance
= decode_prefix4(p
, buf
, sizeof(buf
));
507 advance
= decode_prefix6(p
, buf
, sizeof(buf
));
512 printf(" (unknown af)");
527 bgp_open_print(const u_char
*dat
, int length
)
529 struct bgp_open bgpo
;
530 struct bgp_opt bgpopt
;
535 memcpy(&bgpo
, dat
, sizeof(bgpo
));
536 hlen
= ntohs(bgpo
.bgpo_len
);
538 printf(": Version %d,", bgpo
.bgpo_version
);
539 printf(" AS #%u,", ntohs(bgpo
.bgpo_myas
));
540 printf(" Holdtime %u,", ntohs(bgpo
.bgpo_holdtime
));
541 printf(" ID %s,", getname((u_char
*)&bgpo
.bgpo_id
));
542 printf(" Option length %u", bgpo
.bgpo_optlen
);
545 opt
= &((struct bgp_open
*)dat
)->bgpo_optlen
;
548 for (i
= 0; i
< bgpo
.bgpo_optlen
; i
++) {
549 memcpy(&bgpopt
, &opt
[i
], sizeof(bgpopt
));
550 if (i
+ 2 + bgpopt
.bgpopt_len
> bgpo
.bgpo_optlen
) {
551 printf(" [|opt %d %d]", bgpopt
.bgpopt_len
, bgpopt
.bgpopt_type
);
555 printf(" (option %s, len=%d)", bgp_opttype(bgpopt
.bgpopt_type
),
557 i
+= sizeof(bgpopt
) + bgpopt
.bgpopt_len
;
562 bgp_update_print(const u_char
*dat
, int length
)
565 struct bgp_attr bgpa
;
572 memcpy(&bgp
, dat
, sizeof(bgp
));
573 hlen
= ntohs(bgp
.bgp_len
);
574 p
= dat
+ BGP_SIZE
; /*XXX*/
577 /* Unfeasible routes */
578 len
= ntohs(*(u_int16_t
*)p
);
580 printf(" (Withdrawn routes: %d bytes)", len
);
584 len
= ntohs(*(u_int16_t
*)p
);
586 /* do something more useful!*/
588 printf(" (Path attributes:"); /* ) */
590 while (i
< 2 + len
) {
593 memcpy(&bgpa
, &p
[i
], sizeof(bgpa
));
594 alen
= bgp_attr_len(&bgpa
);
595 aoff
= bgp_attr_off(&bgpa
);
597 if (vflag
&& newline
)
602 printf("%s", bgp_attr_type(bgpa
.bgpa_type
));
603 if (bgpa
.bgpa_flags
) {
605 bgpa
.bgpa_flags
& 0x80 ? "O" : "",
606 bgpa
.bgpa_flags
& 0x40 ? "T" : "",
607 bgpa
.bgpa_flags
& 0x20 ? "P" : "",
608 bgpa
.bgpa_flags
& 0x00 ? "E" : "");
611 bgp_attr_print(&bgpa
, &p
[i
+ aoff
], alen
);
625 if (len
&& dat
+ length
> p
)
627 if (dat
+ length
> p
) {
628 printf("(NLRI:"); /* ) */
629 while (dat
+ length
> p
) {
631 i
= decode_prefix4(p
, buf
, sizeof(buf
));
644 bgp_notification_print(const u_char
*dat
, int length
)
646 struct bgp_notification bgpn
;
649 memcpy(&bgpn
, dat
, sizeof(bgpn
));
650 hlen
= ntohs(bgpn
.bgpn_len
);
652 printf(": error %s,", bgp_notify_major(bgpn
.bgpn_major
));
653 printf(" subcode %s",
654 bgp_notify_minor(bgpn
.bgpn_major
, bgpn
.bgpn_minor
));
658 bgp_header_print(const u_char
*dat
, int length
)
662 memcpy(&bgp
, dat
, sizeof(bgp
));
663 printf("(%s", bgp_type(bgp
.bgp_type
)); /* ) */
665 switch (bgp
.bgp_type
) {
667 bgp_open_print(dat
, length
);
670 bgp_update_print(dat
, length
);
672 case BGP_NOTIFICATION
:
673 bgp_notification_print(dat
, length
);
682 bgp_print(const u_char
*dat
, int length
)
687 const u_char marker
[] = {
688 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
689 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
696 if (snapend
< dat
+ length
)
704 while (p
< snapend
) {
705 if (!TTEST2(p
[0], 1))
712 if (!TTEST2(p
[0], sizeof(marker
)))
714 if (memcmp(p
, marker
, sizeof(marker
)) != 0) {
719 /* found BGP header */
720 TCHECK2(p
[0], BGP_SIZE
); /*XXX*/
721 memcpy(&bgp
, p
, sizeof(bgp
));
726 hlen
= ntohs(bgp
.bgp_len
);
727 if (vflag
&& newline
)
731 if (TTEST2(p
[0], hlen
)) {
732 bgp_header_print(p
, hlen
);
737 printf("[|BGP %s]", bgp_type(bgp
.bgp_type
));