]>
The Tcpdump Group git mirrors - tcpdump/blob - print-bgp.c
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
30 #include <sys/param.h>
32 #include <sys/types.h>
33 #include <sys/socket.h>
35 #include <netinet/in.h>
36 #include <netinet/in_systm.h>
37 #include <netinet/ip.h>
38 #include <netinet/ip_var.h>
39 #include <netinet/udp.h>
40 #include <netinet/udp_var.h>
45 #include "interface.h"
46 #include "addrtoname.h"
49 u_int8_t bgp_marker
[16];
53 #define BGP_SIZE 19 /* unaligned */
57 #define BGP_NOTIFICATION 3
58 #define BGP_KEEPALIVE 4
61 u_int8_t bgpo_marker
[16];
64 u_int8_t bgpo_version
;
66 u_int16_t bgpo_holdtime
;
69 /* options should follow */
72 struct bgp_notification
{
73 u_int8_t bgpn_marker
[16];
78 /* data should follow */
88 #define bgp_attr_len(p) \
89 (((p)->bgpa_flags & 0x10) ? \
90 ntohs((p)->bgpa_len.elen) : (p)->bgpa_len.len)
91 #define bgp_attr_off(p) \
92 (((p)->bgpa_flags & 0x10) ? 4 : 3)
95 #define BGPTYPE_ORIGIN 1
96 #define BGPTYPE_AS_PATH 2
97 #define BGPTYPE_NEXT_HOP 3
98 #define BGPTYPE_MULTI_EXIT_DISC 4
99 #define BGPTYPE_LOCAL_PREF 5
100 #define BGPTYPE_ATOMIC_AGGREGATE 6
101 #define BGPTYPE_AGGREGATOR 7
102 #define BGPTYPE_MP_REACH_NLRI 14 /* RFC2283 */
103 #define BGPTYPE_MP_UNREACH_NLRI 15 /* RFC2283 */
106 static const char *bgptype
[] = {
107 NULL
, "OPEN", "UPDATE", "NOTIFICATION", "KEEPALIVE",
109 #define bgp_type(x) num_or_str(bgptype, sizeof(bgptype)/sizeof(bgptype[0]), (x))
111 static const char *bgpnotify_major
[] = {
112 NULL
, "Message Header Error",
113 "OPEN Message Error", "UPDATE Message Error",
114 "Hold Timer Expired", "Finite State Machine Error",
117 #define bgp_notify_major(x) \
118 num_or_str(bgpnotify_major, \
119 sizeof(bgpnotify_major)/sizeof(bgpnotify_major[0]), (x))
121 static const char *bgpnotify_minor_1
[] = {
122 NULL
, "Connection Not Synchronized",
123 "Bad Message Length", "Bad Message Type",
126 static const char *bgpnotify_minor_2
[] = {
127 NULL
, "Unsupported Version Number",
128 "Bad Peer AS", "Bad BGP Identifier",
129 "Unsupported Optional Parameter", "Authentication Failure",
130 "Unacceptable Hold Time",
133 static const char *bgpnotify_minor_3
[] = {
134 NULL
, "Malformed Attribute List",
135 "Unrecognized Well-known Attribute", "Missing Well-known Attribute",
136 "Attribute Flags Error", "Attribute Length Error",
137 "Invalid ORIGIN Attribute", "AS Routing Loop",
138 "Invalid NEXT_HOP Attribute", "Optional Attribute Error",
139 "Invalid Network Field", "Malformed AS_PATH",
142 static const char **bgpnotify_minor
[] = {
143 NULL
, bgpnotify_minor_1
, bgpnotify_minor_2
, bgpnotify_minor_3
,
145 static const int bgpnotify_minor_siz
[] = {
146 0, sizeof(bgpnotify_minor_1
)/sizeof(bgpnotify_minor_1
[0]),
147 sizeof(bgpnotify_minor_2
)/sizeof(bgpnotify_minor_2
[0]),
148 sizeof(bgpnotify_minor_3
)/sizeof(bgpnotify_minor_3
[0]),
151 static const char *bgpattr_origin
[] = {
152 "IGP", "EGP", "INCOMPLETE",
154 #define bgp_attr_origin(x) \
155 num_or_str(bgpattr_origin, \
156 sizeof(bgpattr_origin)/sizeof(bgpattr_origin[0]), (x))
158 static const char *bgpattr_type
[] = {
159 NULL
, "ORIGIN", "AS_PATH", "NEXT_HOP",
160 "MULTI_EXIT_DISC", "LOCAL_PREF", "ATOMIC_AGGREGATE", "AGGREGATOR",
161 NULL
, NULL
, NULL
, NULL
,
162 NULL
, NULL
, "MP_REACH_NLRI", "MP_UNREACH_NLRI",
164 #define bgp_attr_type(x) \
165 num_or_str(bgpattr_type, \
166 sizeof(bgpattr_type)/sizeof(bgpattr_type[0]), (x))
168 /* Subsequent address family identifier, RFC2283 section 7 */
169 static const char *bgpattr_nlri_safi
[] = {
170 "Reserved", "Unicast", "Multicast", "Unicast+Multicast",
172 #define bgp_attr_nlri_safi(x) \
173 num_or_str(bgpattr_nlri_safi, \
174 sizeof(bgpattr_nlri_safi)/sizeof(bgpattr_nlri_safi[0]), (x))
176 /* RFC1700 address family numbers */
178 #define AFNUM_INET6 2
181 #define AFNUM_BBN1822 5
186 #define AFNUM_X121 10
188 #define AFNUM_ATALK 12
189 #define AFNUM_DECNET 13
190 #define AFNUM_BANYAN 14
191 #define AFNUM_E164NSAP 15
193 static const char *afnumber
[] = {
194 "Reserved", "IPv4", "IPv6", "NSAP", "HDLC",
195 "BBN 1822", "802", "E.163", "E.164", "F.69",
196 "X.121", "IPX", "Appletalk", "Decnet IV", "Banyan Vines",
197 "E.164 with NSAP subaddress",
200 (((x) == 65535) ? afnumber[0] : \
201 num_or_str(afnumber, \
202 sizeof(afnumber)/sizeof(afnumber[0]), (x)))
206 num_or_str(const char **table
, size_t siz
, int value
)
209 if (value
< 0 || siz
<= value
|| table
[value
] == NULL
) {
210 sprintf(buf
, "#%d", value
);
217 bgp_notify_minor(int major
, int minor
)
219 static const char **table
;
225 && major
< sizeof(bgpnotify_minor
)/sizeof(bgpnotify_minor
[0])
226 && bgpnotify_minor
[major
]) {
227 table
= bgpnotify_minor
[major
];
228 siz
= bgpnotify_minor_siz
[major
];
229 if (0 <= minor
&& minor
< siz
&& table
[minor
])
236 sprintf(buf
, "#%d", minor
);
243 decode_prefix4(const u_char
*pd
, char *buf
, int buflen
)
249 if (plen
< 0 || 32 < plen
)
252 memset(&addr
, 0, sizeof(addr
));
253 memcpy(&addr
, &pd
[1], (plen
+ 7) / 8);
255 ((u_char
*)&addr
)[(plen
+ 7) / 8 - 1] &=
256 ((0xff00 >> (plen
% 8)) & 0xff);
258 sprintf(buf
, "%s/%d", getname((char *)&addr
), plen
);
259 return 1 + (plen
+ 7) / 8;
264 decode_prefix6(const u_char
*pd
, char *buf
, int buflen
)
266 struct in6_addr addr
;
270 if (plen
< 0 || 128 < plen
)
273 memset(&addr
, 0, sizeof(addr
));
274 memcpy(&addr
, &pd
[1], (plen
+ 7) / 8);
276 addr
.s6_addr
[(plen
+ 7) / 8 - 1] &=
277 ((0xff00 >> (plen
% 8)) & 0xff);
279 sprintf(buf
, "%s/%d", getname6((char *)&addr
), plen
);
280 return 1 + (plen
+ 7) / 8;
285 bgp_attr_print(const struct bgp_attr
*attr
, const u_char
*dat
, int len
)
298 switch (attr
->bgpa_type
) {
301 printf(" invalid len");
303 printf(" %s", bgp_attr_origin(p
[0]));
305 case BGPTYPE_AS_PATH
:
307 printf(" invalid len");
310 for (i
= 0; i
< len
; i
+= 2)
311 printf(" %u", ntohs(*(u_int16_t
*)&p
[i
]));
313 case BGPTYPE_NEXT_HOP
:
315 printf(" invalid len");
317 printf(" %s", getname(p
));
319 case BGPTYPE_MULTI_EXIT_DISC
:
320 case BGPTYPE_LOCAL_PREF
:
322 printf(" invalid len");
324 printf(" %u", (u_int32_t
)ntohl(*(u_int32_t
*)p
));
326 case BGPTYPE_ATOMIC_AGGREGATE
:
328 printf(" invalid len");
330 case BGPTYPE_AGGREGATOR
:
332 printf(" invalid len");
335 printf(" AS #%u, origin %s", ntohs(*(u_int16_t
*)p
),
338 case BGPTYPE_MP_REACH_NLRI
:
339 af
= ntohs(*(u_int16_t
*)p
);
342 printf(" %s vendor specific,", af_name(af
));
344 printf(" %s %s,", af_name(af
),
345 bgp_attr_nlri_safi(safi
));
349 if (af
== AFNUM_INET
)
352 else if (af
== AFNUM_INET6
)
361 if (af
== AFNUM_INET
)
364 else if (af
== AFNUM_INET6
)
368 for (i
= 0; i
< tlen
; i
+= advance
) {
369 if (af
== AFNUM_INET
)
370 printf(" %s", getname(p
+ 1 + i
));
372 else if (af
== AFNUM_INET6
)
373 printf(" %s", getname6(p
+ 1 + i
));
383 printf(" %u snpa", snpa
);
384 for (/*nothing*/; snpa
> 0; snpa
--) {
385 printf("(%d bytes)", p
[0]);
392 while (len
- (p
- dat
) > 0) {
393 if (af
== AFNUM_INET
)
394 advance
= decode_prefix4(p
, buf
, sizeof(buf
));
396 else if (af
== AFNUM_INET6
)
397 advance
= decode_prefix6(p
, buf
, sizeof(buf
));
406 case BGPTYPE_MP_UNREACH_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
));
418 while (len
- (p
- dat
) > 0) {
419 if (af
== AFNUM_INET
)
420 advance
= decode_prefix4(p
, buf
, sizeof(buf
));
422 else if (af
== AFNUM_INET6
)
423 advance
= decode_prefix6(p
, buf
, sizeof(buf
));
436 bgp_open_print(const u_char
*dat
, int length
)
438 struct bgp_open bgpo
;
441 memcpy(&bgpo
, dat
, sizeof(bgpo
));
442 hlen
= ntohs(bgpo
.bgpo_len
);
444 printf(": Version %d,", bgpo
.bgpo_version
);
445 printf(" AS #%u,", ntohs(bgpo
.bgpo_myas
));
446 printf(" Holdtime %u,", ntohs(bgpo
.bgpo_holdtime
));
447 printf(" ID %s,", getname((char *)&bgpo
.bgpo_id
));
448 printf(" Option length %u", bgpo
.bgpo_optlen
);
452 bgp_update_print(const u_char
*dat
, int length
)
455 struct bgp_attr bgpa
;
462 memcpy(&bgp
, dat
, sizeof(bgp
));
463 hlen
= ntohs(bgp
.bgp_len
);
464 p
= dat
+ BGP_SIZE
; /*XXX*/
467 /* Unfeasible routes */
468 len
= ntohs(*(u_int16_t
*)p
);
470 printf(" (Withdrawn routes: %d bytes)", len
);
474 len
= ntohs(*(u_int16_t
*)p
);
476 /* do something more useful!*/
478 printf(" (Path attributes:"); /* ) */
480 while (i
< 2 + len
) {
483 memcpy(&bgpa
, &p
[i
], sizeof(bgpa
));
484 alen
= bgp_attr_len(&bgpa
);
485 aoff
= bgp_attr_off(&bgpa
);
487 if (vflag
&& newline
)
492 printf("%s", bgp_attr_type(bgpa
.bgpa_type
));
493 if (bgpa
.bgpa_flags
) {
495 bgpa
.bgpa_flags
& 0x80 ? "O" : "",
496 bgpa
.bgpa_flags
& 0x40 ? "T" : "",
497 bgpa
.bgpa_flags
& 0x20 ? "P" : "",
498 bgpa
.bgpa_flags
& 0x00 ? "E" : "");
501 bgp_attr_print(&bgpa
, &p
[i
+ aoff
], alen
);
506 proto_tree_add_text(subtree2
, p
- pd
+ i
+ aoff
, alen
,
507 "Unknown (%d bytes)", alen
);
526 bgp_notification_print(const u_char
*dat
, int length
)
528 struct bgp_notification bgpn
;
531 memcpy(&bgpn
, dat
, sizeof(bgpn
));
532 hlen
= ntohs(bgpn
.bgpn_len
);
534 printf(": error %s,", bgp_notify_major(bgpn
.bgpn_major
));
535 printf(" subcode %s",
536 bgp_notify_minor(bgpn
.bgpn_major
, bgpn
.bgpn_minor
));
540 bgp_header_print(const u_char
*dat
, int length
)
544 memcpy(&bgp
, dat
, sizeof(bgp
));
545 printf("(%s", bgp_type(bgp
.bgp_type
)); /* ) */
547 switch (bgp
.bgp_type
) {
549 bgp_open_print(dat
, length
);
552 bgp_update_print(dat
, length
);
554 case BGP_NOTIFICATION
:
555 bgp_notification_print(dat
, length
);
564 bgp_print(const u_char
*dat
, int length
)
568 const u_char
*start
, *end
;
569 const u_char marker
[] = {
570 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
571 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
578 if (snapend
< dat
+ length
)
586 while (p
< snapend
) {
587 if (!TTEST2(p
[0], 1))
594 if (!TTEST2(p
[0], sizeof(marker
)))
596 if (memcmp(p
, marker
, sizeof(marker
)) != 0) {
601 /* found BGP header */
602 TCHECK2(p
[0], BGP_SIZE
); /*XXX*/
603 memcpy(&bgp
, p
, sizeof(bgp
));
608 hlen
= ntohs(bgp
.bgp_len
);
609 if (vflag
&& newline
)
613 if (TTEST2(p
[0], hlen
)) {
614 if (vflag
&& newline
)
615 bgp_header_print(p
, hlen
);
620 printf("[|BGP %s]", bgp_type(bgp
.bgp_type
));