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.11 2000-01-17 06:24:24 itojun 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>
46 #include <netinet/ip.h>
47 #include <netinet/ip_var.h>
48 #include <netinet/udp.h>
49 #include <netinet/udp_var.h>
55 #include "interface.h"
56 #include "addrtoname.h"
59 u_int8_t bgp_marker
[16];
63 #define BGP_SIZE 19 /* unaligned */
67 #define BGP_NOTIFICATION 3
68 #define BGP_KEEPALIVE 4
71 u_int8_t bgpo_marker
[16];
74 u_int8_t bgpo_version
;
76 u_int16_t bgpo_holdtime
;
79 /* options should follow */
88 struct bgp_notification
{
89 u_int8_t bgpn_marker
[16];
94 /* data should follow */
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",
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 */
197 static const char *bgpattr_nlri_safi
[] = {
198 "Reserved", "Unicast", "Multicast", "Unicast+Multicast",
200 #define bgp_attr_nlri_safi(x) \
201 num_or_str(bgpattr_nlri_safi, \
202 sizeof(bgpattr_nlri_safi)/sizeof(bgpattr_nlri_safi[0]), (x))
204 /* well-known community */
205 #define BGP_COMMUNITY_NO_EXPORT 0xffffff01
206 #define BGP_COMMUNITY_NO_ADVERT 0xffffff02
207 #define BGP_COMMUNITY_NO_EXPORT_SUBCONFED 0xffffff03
209 /* RFC1700 address family numbers */
211 #define AFNUM_INET6 2
214 #define AFNUM_BBN1822 5
219 #define AFNUM_X121 10
221 #define AFNUM_ATALK 12
222 #define AFNUM_DECNET 13
223 #define AFNUM_BANYAN 14
224 #define AFNUM_E164NSAP 15
226 static const char *afnumber
[] = {
227 "Reserved", "IPv4", "IPv6", "NSAP", "HDLC",
228 "BBN 1822", "802", "E.163", "E.164", "F.69",
229 "X.121", "IPX", "Appletalk", "Decnet IV", "Banyan Vines",
230 "E.164 with NSAP subaddress",
233 (((x) == 65535) ? afnumber[0] : \
234 num_or_str(afnumber, \
235 sizeof(afnumber)/sizeof(afnumber[0]), (x)))
239 num_or_str(const char **table
, size_t siz
, int value
)
242 if (value
< 0 || siz
<= value
|| table
[value
] == NULL
) {
243 snprintf(buf
, sizeof(buf
), "#%d", value
);
250 bgp_notify_minor(int major
, int minor
)
252 static const char **table
;
258 && major
< sizeof(bgpnotify_minor
)/sizeof(bgpnotify_minor
[0])
259 && bgpnotify_minor
[major
]) {
260 table
= bgpnotify_minor
[major
];
261 siz
= bgpnotify_minor_siz
[major
];
262 if (0 <= minor
&& minor
< siz
&& table
[minor
])
269 snprintf(buf
, sizeof(buf
), "#%d", minor
);
276 decode_prefix4(const u_char
*pd
, char *buf
, int buflen
)
282 if (plen
< 0 || 32 < plen
)
285 memset(&addr
, 0, sizeof(addr
));
286 memcpy(&addr
, &pd
[1], (plen
+ 7) / 8);
288 ((u_char
*)&addr
)[(plen
+ 7) / 8 - 1] &=
289 ((0xff00 >> (plen
% 8)) & 0xff);
291 snprintf(buf
, buflen
, "%s/%d", getname((char *)&addr
), plen
);
292 return 1 + (plen
+ 7) / 8;
297 decode_prefix6(const u_char
*pd
, char *buf
, int buflen
)
299 struct in6_addr addr
;
303 if (plen
< 0 || 128 < plen
)
306 memset(&addr
, 0, sizeof(addr
));
307 memcpy(&addr
, &pd
[1], (plen
+ 7) / 8);
309 addr
.s6_addr
[(plen
+ 7) / 8 - 1] &=
310 ((0xff00 >> (plen
% 8)) & 0xff);
312 snprintf(buf
, buflen
, "%s/%d", getname6((char *)&addr
), plen
);
313 return 1 + (plen
+ 7) / 8;
318 bgp_attr_print(const struct bgp_attr
*attr
, const u_char
*dat
, int len
)
330 switch (attr
->bgpa_type
) {
333 printf(" invalid len");
335 printf(" %s", bgp_attr_origin(p
[0]));
337 case BGPTYPE_AS_PATH
:
339 printf(" invalid len");
342 while (p
< dat
+ len
) {
344 * under RFC1965, p[0] means:
345 * 1: AS_SET 2: AS_SEQUENCE
346 * 3: AS_CONFED_SET 4: AS_CONFED_SEQUENCE
349 if (p
[0] == 3 || p
[0] == 4)
351 printf("%s", (p
[0] & 1) ? "{" : "");
352 for (i
= 0; i
< p
[1]; i
+= 2) {
353 printf("%s%u", i
== 0 ? "" : " ",
354 ntohs(*(u_int16_t
*)&p
[2 + i
]));
356 printf("%s", (p
[0] & 1) ? "}" : "");
360 case BGPTYPE_NEXT_HOP
:
362 printf(" invalid len");
364 printf(" %s", getname(p
));
366 case BGPTYPE_MULTI_EXIT_DISC
:
367 case BGPTYPE_LOCAL_PREF
:
369 printf(" invalid len");
371 printf(" %u", (u_int32_t
)ntohl(*(u_int32_t
*)p
));
373 case BGPTYPE_ATOMIC_AGGREGATE
:
375 printf(" invalid len");
377 case BGPTYPE_AGGREGATOR
:
379 printf(" invalid len");
382 printf(" AS #%u, origin %s", ntohs(*(u_int16_t
*)p
),
385 case BGPTYPE_COMMUNITIES
:
387 printf(" invalid len");
390 for (i
= 0; i
< len
; i
++) {
392 comm
= (u_int32_t
)ntohl(*(u_int32_t
*)&p
[i
]);
394 case BGP_COMMUNITY_NO_EXPORT
:
395 printf(" NO_EXPORT");
397 case BGP_COMMUNITY_NO_ADVERT
:
398 printf(" NO_ADVERTISE");
400 case BGP_COMMUNITY_NO_EXPORT_SUBCONFED
:
401 printf(" NO_EXPORT_SUBCONFED");
404 printf(" (AS #%d value 0x%04x)",
405 (comm
>> 16) & 0xffff, comm
& 0xfffff);
410 case BGPTYPE_MP_REACH_NLRI
:
411 af
= ntohs(*(u_int16_t
*)p
);
414 printf(" %s vendor specific,", af_name(af
));
416 printf(" %s %s,", af_name(af
),
417 bgp_attr_nlri_safi(safi
));
421 if (af
== AFNUM_INET
)
424 else if (af
== AFNUM_INET6
)
433 if (af
== AFNUM_INET
)
436 else if (af
== AFNUM_INET6
)
440 for (i
= 0; i
< tlen
; i
+= advance
) {
441 if (af
== AFNUM_INET
)
442 printf(" %s", getname(p
+ 1 + i
));
444 else if (af
== AFNUM_INET6
)
445 printf(" %s", getname6(p
+ 1 + i
));
455 printf(" %u snpa", snpa
);
456 for (/*nothing*/; snpa
> 0; snpa
--) {
457 printf("(%d bytes)", p
[0]);
464 while (len
- (p
- dat
) > 0) {
465 if (af
== AFNUM_INET
)
466 advance
= decode_prefix4(p
, buf
, sizeof(buf
));
468 else if (af
== AFNUM_INET6
)
469 advance
= decode_prefix6(p
, buf
, sizeof(buf
));
478 case BGPTYPE_MP_UNREACH_NLRI
:
479 af
= ntohs(*(u_int16_t
*)p
);
482 printf(" %s vendor specific,", af_name(af
));
484 printf(" %s %s,", af_name(af
),
485 bgp_attr_nlri_safi(safi
));
490 while (len
- (p
- dat
) > 0) {
491 if (af
== AFNUM_INET
)
492 advance
= decode_prefix4(p
, buf
, sizeof(buf
));
494 else if (af
== AFNUM_INET6
)
495 advance
= decode_prefix6(p
, buf
, sizeof(buf
));
508 bgp_open_print(const u_char
*dat
, int length
)
510 struct bgp_open bgpo
;
511 struct bgp_opt bgpopt
;
516 memcpy(&bgpo
, dat
, sizeof(bgpo
));
517 hlen
= ntohs(bgpo
.bgpo_len
);
519 printf(": Version %d,", bgpo
.bgpo_version
);
520 printf(" AS #%u,", ntohs(bgpo
.bgpo_myas
));
521 printf(" Holdtime %u,", ntohs(bgpo
.bgpo_holdtime
));
522 printf(" ID %s,", getname((char *)&bgpo
.bgpo_id
));
523 printf(" Option length %u", bgpo
.bgpo_optlen
);
526 opt
= &((struct bgp_open
*)dat
)->bgpo_optlen
;
529 for (i
= 0; i
< bgpo
.bgpo_optlen
; i
++) {
530 memcpy(&bgpopt
, &opt
[i
], sizeof(bgpopt
));
531 if (i
+ 2 + bgpopt
.bgpopt_len
> bgpo
.bgpo_optlen
) {
532 printf(" [|opt %d %d]", bgpopt
.bgpopt_len
, bgpopt
.bgpopt_type
);
536 printf(" (option %s, len=%d)", bgp_opttype(bgpopt
.bgpopt_type
),
538 i
+= sizeof(bgpopt
) + bgpopt
.bgpopt_len
;
543 bgp_update_print(const u_char
*dat
, int length
)
546 struct bgp_attr bgpa
;
553 memcpy(&bgp
, dat
, sizeof(bgp
));
554 hlen
= ntohs(bgp
.bgp_len
);
555 p
= dat
+ BGP_SIZE
; /*XXX*/
558 /* Unfeasible routes */
559 len
= ntohs(*(u_int16_t
*)p
);
561 printf(" (Withdrawn routes: %d bytes)", len
);
565 len
= ntohs(*(u_int16_t
*)p
);
567 /* do something more useful!*/
569 printf(" (Path attributes:"); /* ) */
571 while (i
< 2 + len
) {
574 memcpy(&bgpa
, &p
[i
], sizeof(bgpa
));
575 alen
= bgp_attr_len(&bgpa
);
576 aoff
= bgp_attr_off(&bgpa
);
578 if (vflag
&& newline
)
583 printf("%s", bgp_attr_type(bgpa
.bgpa_type
));
584 if (bgpa
.bgpa_flags
) {
586 bgpa
.bgpa_flags
& 0x80 ? "O" : "",
587 bgpa
.bgpa_flags
& 0x40 ? "T" : "",
588 bgpa
.bgpa_flags
& 0x20 ? "P" : "",
589 bgpa
.bgpa_flags
& 0x00 ? "E" : "");
592 bgp_attr_print(&bgpa
, &p
[i
+ aoff
], alen
);
606 if (len
&& dat
+ length
> p
)
608 if (dat
+ length
> p
) {
609 printf("(NLRI:"); /* ) */
610 while (dat
+ length
> p
) {
612 i
= decode_prefix4(p
, buf
, sizeof(buf
));
625 bgp_notification_print(const u_char
*dat
, int length
)
627 struct bgp_notification bgpn
;
630 memcpy(&bgpn
, dat
, sizeof(bgpn
));
631 hlen
= ntohs(bgpn
.bgpn_len
);
633 printf(": error %s,", bgp_notify_major(bgpn
.bgpn_major
));
634 printf(" subcode %s",
635 bgp_notify_minor(bgpn
.bgpn_major
, bgpn
.bgpn_minor
));
639 bgp_header_print(const u_char
*dat
, int length
)
643 memcpy(&bgp
, dat
, sizeof(bgp
));
644 printf("(%s", bgp_type(bgp
.bgp_type
)); /* ) */
646 switch (bgp
.bgp_type
) {
648 bgp_open_print(dat
, length
);
651 bgp_update_print(dat
, length
);
653 case BGP_NOTIFICATION
:
654 bgp_notification_print(dat
, length
);
663 bgp_print(const u_char
*dat
, int length
)
668 const u_char marker
[] = {
669 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
670 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
677 if (snapend
< dat
+ length
)
685 while (p
< snapend
) {
686 if (!TTEST2(p
[0], 1))
693 if (!TTEST2(p
[0], sizeof(marker
)))
695 if (memcmp(p
, marker
, sizeof(marker
)) != 0) {
700 /* found BGP header */
701 TCHECK2(p
[0], BGP_SIZE
); /*XXX*/
702 memcpy(&bgp
, p
, sizeof(bgp
));
707 hlen
= ntohs(bgp
.bgp_len
);
708 if (vflag
&& newline
)
712 if (TTEST2(p
[0], hlen
)) {
713 bgp_header_print(p
, hlen
);
718 printf("[|BGP %s]", bgp_type(bgp
.bgp_type
));