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.16 2000-09-24 07:48:19 guy Exp $";
39 #include <sys/param.h>
41 #include <sys/types.h>
42 #include <sys/socket.h>
44 #include <netinet/in.h>
45 #include <netinet/in_systm.h>
51 #include "interface.h"
52 #include "addrtoname.h"
55 u_int8_t bgp_marker
[16];
59 #define BGP_SIZE 19 /* unaligned */
63 #define BGP_NOTIFICATION 3
64 #define BGP_KEEPALIVE 4
67 u_int8_t bgpo_marker
[16];
70 u_int8_t bgpo_version
;
72 u_int16_t bgpo_holdtime
;
75 /* options should follow */
84 struct bgp_notification
{
85 u_int8_t bgpn_marker
[16];
90 /* data should follow */
100 #define bgp_attr_len(p) \
101 (((p)->bgpa_flags & 0x10) ? \
102 ntohs((p)->bgpa_len.elen) : (p)->bgpa_len.len)
103 #define bgp_attr_off(p) \
104 (((p)->bgpa_flags & 0x10) ? 4 : 3)
107 #define BGPTYPE_ORIGIN 1
108 #define BGPTYPE_AS_PATH 2
109 #define BGPTYPE_NEXT_HOP 3
110 #define BGPTYPE_MULTI_EXIT_DISC 4
111 #define BGPTYPE_LOCAL_PREF 5
112 #define BGPTYPE_ATOMIC_AGGREGATE 6
113 #define BGPTYPE_AGGREGATOR 7
114 #define BGPTYPE_COMMUNITIES 8 /* RFC1997 */
115 #define BGPTYPE_ORIGINATOR_ID 9 /* RFC1998 */
116 #define BGPTYPE_CLUSTER_LIST 10 /* RFC1998 */
117 #define BGPTYPE_DPA 11 /* work in progress */
118 #define BGPTYPE_ADVERTISERS 12 /* RFC1863 */
119 #define BGPTYPE_RCID_PATH 13 /* RFC1863 */
120 #define BGPTYPE_MP_REACH_NLRI 14 /* RFC2283 */
121 #define BGPTYPE_MP_UNREACH_NLRI 15 /* RFC2283 */
124 static const char *bgptype
[] = {
125 NULL
, "OPEN", "UPDATE", "NOTIFICATION", "KEEPALIVE",
127 #define bgp_type(x) num_or_str(bgptype, sizeof(bgptype)/sizeof(bgptype[0]), (x))
129 static const char *bgpopt_type
[] = {
130 NULL
, "Authentication Information",
132 #define bgp_opttype(x) \
133 num_or_str(bgpopt_type, sizeof(bgpopt_type)/sizeof(bgpopt_type[0]), (x))
135 static const char *bgpnotify_major
[] = {
136 NULL
, "Message Header Error",
137 "OPEN Message Error", "UPDATE Message Error",
138 "Hold Timer Expired", "Finite State Machine Error",
141 #define bgp_notify_major(x) \
142 num_or_str(bgpnotify_major, \
143 sizeof(bgpnotify_major)/sizeof(bgpnotify_major[0]), (x))
145 static const char *bgpnotify_minor_1
[] = {
146 NULL
, "Connection Not Synchronized",
147 "Bad Message Length", "Bad Message Type",
150 static const char *bgpnotify_minor_2
[] = {
151 NULL
, "Unsupported Version Number",
152 "Bad Peer AS", "Bad BGP Identifier",
153 "Unsupported Optional Parameter", "Authentication Failure",
154 "Unacceptable Hold Time",
157 static const char *bgpnotify_minor_3
[] = {
158 NULL
, "Malformed Attribute List",
159 "Unrecognized Well-known Attribute", "Missing Well-known Attribute",
160 "Attribute Flags Error", "Attribute Length Error",
161 "Invalid ORIGIN Attribute", "AS Routing Loop",
162 "Invalid NEXT_HOP Attribute", "Optional Attribute Error",
163 "Invalid Network Field", "Malformed AS_PATH",
166 static const char **bgpnotify_minor
[] = {
167 NULL
, bgpnotify_minor_1
, bgpnotify_minor_2
, bgpnotify_minor_3
,
169 static const int bgpnotify_minor_siz
[] = {
170 0, sizeof(bgpnotify_minor_1
)/sizeof(bgpnotify_minor_1
[0]),
171 sizeof(bgpnotify_minor_2
)/sizeof(bgpnotify_minor_2
[0]),
172 sizeof(bgpnotify_minor_3
)/sizeof(bgpnotify_minor_3
[0]),
175 static const char *bgpattr_origin
[] = {
176 "IGP", "EGP", "INCOMPLETE",
178 #define bgp_attr_origin(x) \
179 num_or_str(bgpattr_origin, \
180 sizeof(bgpattr_origin)/sizeof(bgpattr_origin[0]), (x))
182 static const char *bgpattr_type
[] = {
183 NULL
, "ORIGIN", "AS_PATH", "NEXT_HOP",
184 "MULTI_EXIT_DISC", "LOCAL_PREF", "ATOMIC_AGGREGATE", "AGGREGATOR",
185 "COMMUNITIES", "ORIGINATOR_ID", "CLUSTER_LIST", "DPA",
186 "ADVERTISERS", "RCID_PATH", "MP_REACH_NLRI", "MP_UNREACH_NLRI",
188 #define bgp_attr_type(x) \
189 num_or_str(bgpattr_type, \
190 sizeof(bgpattr_type)/sizeof(bgpattr_type[0]), (x))
192 /* Subsequent address family identifier, RFC2283 section 7 */
193 static const char *bgpattr_nlri_safi
[] = {
194 "Reserved", "Unicast", "Multicast", "Unicast+Multicast",
196 #define bgp_attr_nlri_safi(x) \
197 num_or_str(bgpattr_nlri_safi, \
198 sizeof(bgpattr_nlri_safi)/sizeof(bgpattr_nlri_safi[0]), (x))
200 /* well-known community */
201 #define BGP_COMMUNITY_NO_EXPORT 0xffffff01
202 #define BGP_COMMUNITY_NO_ADVERT 0xffffff02
203 #define BGP_COMMUNITY_NO_EXPORT_SUBCONFED 0xffffff03
205 /* RFC1700 address family numbers */
207 #define AFNUM_INET6 2
210 #define AFNUM_BBN1822 5
215 #define AFNUM_X121 10
217 #define AFNUM_ATALK 12
218 #define AFNUM_DECNET 13
219 #define AFNUM_BANYAN 14
220 #define AFNUM_E164NSAP 15
222 static const char *afnumber
[] = {
223 "Reserved", "IPv4", "IPv6", "NSAP", "HDLC",
224 "BBN 1822", "802", "E.163", "E.164", "F.69",
225 "X.121", "IPX", "Appletalk", "Decnet IV", "Banyan Vines",
226 "E.164 with NSAP subaddress",
229 (((x) == 65535) ? afnumber[0] : \
230 num_or_str(afnumber, \
231 sizeof(afnumber)/sizeof(afnumber[0]), (x)))
235 num_or_str(const char **table
, size_t siz
, int value
)
238 if (value
< 0 || siz
<= value
|| table
[value
] == NULL
) {
239 snprintf(buf
, sizeof(buf
), "#%d", value
);
246 bgp_notify_minor(int major
, int minor
)
248 static const char **table
;
254 && major
< sizeof(bgpnotify_minor
)/sizeof(bgpnotify_minor
[0])
255 && bgpnotify_minor
[major
]) {
256 table
= bgpnotify_minor
[major
];
257 siz
= bgpnotify_minor_siz
[major
];
258 if (0 <= minor
&& minor
< siz
&& table
[minor
])
265 snprintf(buf
, sizeof(buf
), "#%d", minor
);
272 decode_prefix4(const u_char
*pd
, char *buf
, int buflen
)
278 if (plen
< 0 || 32 < plen
)
281 memset(&addr
, 0, sizeof(addr
));
282 memcpy(&addr
, &pd
[1], (plen
+ 7) / 8);
284 ((u_char
*)&addr
)[(plen
+ 7) / 8 - 1] &=
285 ((0xff00 >> (plen
% 8)) & 0xff);
287 snprintf(buf
, buflen
, "%s/%d", getname((u_char
*)&addr
), plen
);
288 return 1 + (plen
+ 7) / 8;
293 decode_prefix6(const u_char
*pd
, char *buf
, int buflen
)
295 struct in6_addr addr
;
299 if (plen
< 0 || 128 < plen
)
302 memset(&addr
, 0, sizeof(addr
));
303 memcpy(&addr
, &pd
[1], (plen
+ 7) / 8);
305 addr
.s6_addr
[(plen
+ 7) / 8 - 1] &=
306 ((0xff00 >> (plen
% 8)) & 0xff);
308 snprintf(buf
, buflen
, "%s/%d", getname6((char *)&addr
), plen
);
309 return 1 + (plen
+ 7) / 8;
314 bgp_attr_print(const struct bgp_attr
*attr
, const u_char
*dat
, int len
)
326 switch (attr
->bgpa_type
) {
329 printf(" invalid len");
331 printf(" %s", bgp_attr_origin(p
[0]));
333 case BGPTYPE_AS_PATH
:
335 printf(" invalid len");
338 while (p
< dat
+ len
) {
340 * under RFC1965, p[0] means:
341 * 1: AS_SET 2: AS_SEQUENCE
342 * 3: AS_CONFED_SET 4: AS_CONFED_SEQUENCE
345 if (p
[0] == 3 || p
[0] == 4)
347 printf("%s", (p
[0] & 1) ? "{" : "");
348 for (i
= 0; i
< p
[1]; i
+= 2) {
349 printf("%s%u", i
== 0 ? "" : " ",
350 ntohs(*(u_int16_t
*)&p
[2 + i
]));
352 printf("%s", (p
[0] & 1) ? "}" : "");
356 case BGPTYPE_NEXT_HOP
:
358 printf(" invalid len");
360 printf(" %s", getname(p
));
362 case BGPTYPE_MULTI_EXIT_DISC
:
363 case BGPTYPE_LOCAL_PREF
:
365 printf(" invalid len");
367 printf(" %u", (u_int32_t
)ntohl(*(u_int32_t
*)p
));
369 case BGPTYPE_ATOMIC_AGGREGATE
:
371 printf(" invalid len");
373 case BGPTYPE_AGGREGATOR
:
375 printf(" invalid len");
378 printf(" AS #%u, origin %s", ntohs(*(u_int16_t
*)p
),
381 case BGPTYPE_COMMUNITIES
:
383 printf(" invalid len");
386 for (i
= 0; i
< len
; i
+= 4) {
388 comm
= (u_int32_t
)ntohl(*(u_int32_t
*)&p
[i
]);
390 case BGP_COMMUNITY_NO_EXPORT
:
391 printf(" NO_EXPORT");
393 case BGP_COMMUNITY_NO_ADVERT
:
394 printf(" NO_ADVERTISE");
396 case BGP_COMMUNITY_NO_EXPORT_SUBCONFED
:
397 printf(" NO_EXPORT_SUBCONFED");
400 printf(" (AS #%d value 0x%04x)",
401 (comm
>> 16) & 0xffff, comm
& 0xffff);
406 case BGPTYPE_MP_REACH_NLRI
:
407 af
= ntohs(*(u_int16_t
*)p
);
410 printf(" %s vendor specific,", af_name(af
));
412 printf(" %s %s,", af_name(af
),
413 bgp_attr_nlri_safi(safi
));
417 if (af
== AFNUM_INET
)
420 else if (af
== AFNUM_INET6
)
433 printf(" %s", getname(p
+ 1 + i
));
434 i
+= sizeof(struct in_addr
);
438 printf(" %s", getname6(p
+ 1 + i
));
439 i
+= sizeof(struct in6_addr
);
443 printf(" (unknown af)");
444 i
= tlen
; /*exit loop*/
455 printf(" %u snpa", snpa
);
456 for (/*nothing*/; snpa
> 0; snpa
--) {
457 printf("(%d bytes)", p
[0]);
464 while (len
- (p
- dat
) > 0) {
467 advance
= decode_prefix4(p
, buf
, sizeof(buf
));
472 advance
= decode_prefix6(p
, buf
, sizeof(buf
));
477 printf(" (unknown af)");
488 case BGPTYPE_MP_UNREACH_NLRI
:
489 af
= ntohs(*(u_int16_t
*)p
);
492 printf(" %s vendor specific,", af_name(af
));
494 printf(" %s %s,", af_name(af
),
495 bgp_attr_nlri_safi(safi
));
500 while (len
- (p
- dat
) > 0) {
503 advance
= decode_prefix4(p
, buf
, sizeof(buf
));
508 advance
= decode_prefix6(p
, buf
, sizeof(buf
));
513 printf(" (unknown af)");
528 bgp_open_print(const u_char
*dat
, int length
)
530 struct bgp_open bgpo
;
531 struct bgp_opt bgpopt
;
536 memcpy(&bgpo
, dat
, sizeof(bgpo
));
537 hlen
= ntohs(bgpo
.bgpo_len
);
539 printf(": Version %d,", bgpo
.bgpo_version
);
540 printf(" AS #%u,", ntohs(bgpo
.bgpo_myas
));
541 printf(" Holdtime %u,", ntohs(bgpo
.bgpo_holdtime
));
542 printf(" ID %s,", getname((u_char
*)&bgpo
.bgpo_id
));
543 printf(" Option length %u", bgpo
.bgpo_optlen
);
546 opt
= &((struct bgp_open
*)dat
)->bgpo_optlen
;
549 for (i
= 0; i
< bgpo
.bgpo_optlen
; i
++) {
550 memcpy(&bgpopt
, &opt
[i
], sizeof(bgpopt
));
551 if (i
+ 2 + bgpopt
.bgpopt_len
> bgpo
.bgpo_optlen
) {
552 printf(" [|opt %d %d]", bgpopt
.bgpopt_len
, bgpopt
.bgpopt_type
);
556 printf(" (option %s, len=%d)", bgp_opttype(bgpopt
.bgpopt_type
),
558 i
+= sizeof(bgpopt
) + bgpopt
.bgpopt_len
;
563 bgp_update_print(const u_char
*dat
, int length
)
566 struct bgp_attr bgpa
;
573 memcpy(&bgp
, dat
, sizeof(bgp
));
574 hlen
= ntohs(bgp
.bgp_len
);
575 p
= dat
+ BGP_SIZE
; /*XXX*/
578 /* Unfeasible routes */
579 len
= ntohs(*(u_int16_t
*)p
);
581 printf(" (Withdrawn routes: %d bytes)", len
);
585 len
= ntohs(*(u_int16_t
*)p
);
587 /* do something more useful!*/
589 printf(" (Path attributes:"); /* ) */
591 while (i
< 2 + len
) {
594 memcpy(&bgpa
, &p
[i
], sizeof(bgpa
));
595 alen
= bgp_attr_len(&bgpa
);
596 aoff
= bgp_attr_off(&bgpa
);
598 if (vflag
&& newline
)
603 printf("%s", bgp_attr_type(bgpa
.bgpa_type
));
604 if (bgpa
.bgpa_flags
) {
606 bgpa
.bgpa_flags
& 0x80 ? "O" : "",
607 bgpa
.bgpa_flags
& 0x40 ? "T" : "",
608 bgpa
.bgpa_flags
& 0x20 ? "P" : "",
609 bgpa
.bgpa_flags
& 0x00 ? "E" : "");
612 bgp_attr_print(&bgpa
, &p
[i
+ aoff
], alen
);
626 if (len
&& dat
+ length
> p
)
628 if (dat
+ length
> p
) {
629 printf("(NLRI:"); /* ) */
630 while (dat
+ length
> p
) {
632 i
= decode_prefix4(p
, buf
, sizeof(buf
));
645 bgp_notification_print(const u_char
*dat
, int length
)
647 struct bgp_notification bgpn
;
650 memcpy(&bgpn
, dat
, sizeof(bgpn
));
651 hlen
= ntohs(bgpn
.bgpn_len
);
653 printf(": error %s,", bgp_notify_major(bgpn
.bgpn_major
));
654 printf(" subcode %s",
655 bgp_notify_minor(bgpn
.bgpn_major
, bgpn
.bgpn_minor
));
659 bgp_header_print(const u_char
*dat
, int length
)
663 memcpy(&bgp
, dat
, sizeof(bgp
));
664 printf("(%s", bgp_type(bgp
.bgp_type
)); /* ) */
666 switch (bgp
.bgp_type
) {
668 bgp_open_print(dat
, length
);
671 bgp_update_print(dat
, length
);
673 case BGP_NOTIFICATION
:
674 bgp_notification_print(dat
, length
);
683 bgp_print(const u_char
*dat
, int length
)
688 const u_char marker
[] = {
689 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
690 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
697 if (snapend
< dat
+ length
)
705 while (p
< snapend
) {
706 if (!TTEST2(p
[0], 1))
713 if (!TTEST2(p
[0], sizeof(marker
)))
715 if (memcmp(p
, marker
, sizeof(marker
)) != 0) {
720 /* found BGP header */
721 TCHECK2(p
[0], BGP_SIZE
); /*XXX*/
722 memcpy(&bgp
, p
, sizeof(bgp
));
727 hlen
= ntohs(bgp
.bgp_len
);
728 if (vflag
&& newline
)
732 if (TTEST2(p
[0], hlen
)) {
733 bgp_header_print(p
, hlen
);
738 printf("[|BGP %s]", bgp_type(bgp
.bgp_type
));