]>
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>
46 #include <netinet/ip6.h>
49 #include "interface.h"
50 #include "addrtoname.h"
53 u_int8_t bgp_marker
[16];
57 #define BGP_SIZE 19 /* unaligned */
61 #define BGP_NOTIFICATION 3
62 #define BGP_KEEPALIVE 4
65 u_int8_t bgpo_marker
[16];
68 u_int8_t bgpo_version
;
70 u_int16_t bgpo_holdtime
;
73 /* options should follow */
76 struct bgp_notification
{
77 u_int8_t bgpn_marker
[16];
82 /* data should follow */
92 #define bgp_attr_len(p) \
93 (((p)->bgpa_flags & 0x10) ? \
94 ntohs((p)->bgpa_len.elen) : (p)->bgpa_len.len)
95 #define bgp_attr_off(p) \
96 (((p)->bgpa_flags & 0x10) ? 4 : 3)
99 #define BGPTYPE_ORIGIN 1
100 #define BGPTYPE_AS_PATH 2
101 #define BGPTYPE_NEXT_HOP 3
102 #define BGPTYPE_MULTI_EXIT_DISC 4
103 #define BGPTYPE_LOCAL_PREF 5
104 #define BGPTYPE_ATOMIC_AGGREGATE 6
105 #define BGPTYPE_AGGREGATOR 7
106 #define BGPTYPE_MP_REACH_NLRI 14 /* RFC2283 */
107 #define BGPTYPE_MP_UNREACH_NLRI 15 /* RFC2283 */
110 static const char *bgptype
[] = {
111 NULL
, "OPEN", "UPDATE", "NOTIFICATION", "KEEPALIVE",
113 #define bgp_type(x) num_or_str(bgptype, sizeof(bgptype)/sizeof(bgptype[0]), (x))
115 static const char *bgpnotify_major
[] = {
116 NULL
, "Message Header Error",
117 "OPEN Message Error", "UPDATE Message Error",
118 "Hold Timer Expired", "Finite State Machine Error",
121 #define bgp_notify_major(x) \
122 num_or_str(bgpnotify_major, \
123 sizeof(bgpnotify_major)/sizeof(bgpnotify_major[0]), (x))
125 static const char *bgpnotify_minor_1
[] = {
126 NULL
, "Connection Not Synchronized",
127 "Bad Message Length", "Bad Message Type",
130 static const char *bgpnotify_minor_2
[] = {
131 NULL
, "Unsupported Version Number",
132 "Bad Peer AS", "Bad BGP Identifier",
133 "Unsupported Optional Parameter", "Authentication Failure",
134 "Unacceptable Hold Time",
137 static const char *bgpnotify_minor_3
[] = {
138 NULL
, "Malformed Attribute List",
139 "Unrecognized Well-known Attribute", "Missing Well-known Attribute",
140 "Attribute Flags Error", "Attribute Length Error",
141 "Invalid ORIGIN Attribute", "AS Routing Loop",
142 "Invalid NEXT_HOP Attribute", "Optional Attribute Error",
143 "Invalid Network Field", "Malformed AS_PATH",
146 static const char **bgpnotify_minor
[] = {
147 NULL
, bgpnotify_minor_1
, bgpnotify_minor_2
, bgpnotify_minor_3
,
149 static const int bgpnotify_minor_siz
[] = {
150 0, sizeof(bgpnotify_minor_1
)/sizeof(bgpnotify_minor_1
[0]),
151 sizeof(bgpnotify_minor_2
)/sizeof(bgpnotify_minor_2
[0]),
152 sizeof(bgpnotify_minor_3
)/sizeof(bgpnotify_minor_3
[0]),
155 static const char *bgpattr_origin
[] = {
156 "IGP", "EGP", "INCOMPLETE",
158 #define bgp_attr_origin(x) \
159 num_or_str(bgpattr_origin, \
160 sizeof(bgpattr_origin)/sizeof(bgpattr_origin[0]), (x))
162 static const char *bgpattr_type
[] = {
163 NULL
, "ORIGIN", "AS_PATH", "NEXT_HOP",
164 "MULTI_EXIT_DISC", "LOCAL_PREF", "ATOMIC_AGGREGATE", "AGGREGATOR",
165 NULL
, NULL
, NULL
, NULL
,
166 NULL
, NULL
, "MP_REACH_NLRI", "MP_UNREACH_NLRI",
168 #define bgp_attr_type(x) \
169 num_or_str(bgpattr_type, \
170 sizeof(bgpattr_type)/sizeof(bgpattr_type[0]), (x))
172 /* Subsequent address family identifier, RFC2283 section 7 */
173 static const char *bgpattr_nlri_safi
[] = {
174 "Reserved", "Unicast", "Multicast", "Unicast+Multicast",
176 #define bgp_attr_nlri_safi(x) \
177 num_or_str(bgpattr_nlri_safi, \
178 sizeof(bgpattr_nlri_safi)/sizeof(bgpattr_nlri_safi[0]), (x))
180 /* RFC1700 address family numbers */
182 #define AFNUM_INET6 2
185 #define AFNUM_BBN1822 5
190 #define AFNUM_X121 10
192 #define AFNUM_ATALK 12
193 #define AFNUM_DECNET 13
194 #define AFNUM_BANYAN 14
195 #define AFNUM_E164NSAP 15
197 static const char *afnumber
[] = {
198 "Reserved", "IPv4", "IPv6", "NSAP", "HDLC",
199 "BBN 1822", "802", "E.163", "E.164", "F.69",
200 "X.121", "IPX", "Appletalk", "Decnet IV", "Banyan Vines",
201 "E.164 with NSAP subaddress",
204 (((x) == 65535) ? afnumber[0] : \
205 num_or_str(afnumber, \
206 sizeof(afnumber)/sizeof(afnumber[0]), (x)))
210 num_or_str(const char **table
, size_t siz
, int value
)
213 if (value
< 0 || siz
<= value
|| table
[value
] == NULL
) {
214 sprintf(buf
, "#%d", value
);
221 bgp_notify_minor(int major
, int minor
)
223 static const char **table
;
229 && major
< sizeof(bgpnotify_minor
)/sizeof(bgpnotify_minor
[0])
230 && bgpnotify_minor
[major
]) {
231 table
= bgpnotify_minor
[major
];
232 siz
= bgpnotify_minor_siz
[major
];
233 if (0 <= minor
&& minor
< siz
&& table
[minor
])
240 sprintf(buf
, "#%d", minor
);
247 decode_prefix4(const u_char
*pd
, char *buf
, int buflen
)
253 if (plen
< 0 || 32 < plen
)
256 memset(&addr
, 0, sizeof(addr
));
257 memcpy(&addr
, &pd
[1], (plen
+ 7) / 8);
259 ((u_char
*)&addr
)[(plen
+ 7) / 8 - 1] &=
260 ((0xff00 >> (plen
% 8)) & 0xff);
262 sprintf(buf
, "%s/%d", getname((char *)&addr
), plen
);
263 return 1 + (plen
+ 7) / 8;
268 decode_prefix6(const u_char
*pd
, char *buf
, int buflen
)
270 struct in6_addr addr
;
274 if (plen
< 0 || 128 < plen
)
277 memset(&addr
, 0, sizeof(addr
));
278 memcpy(&addr
, &pd
[1], (plen
+ 7) / 8);
280 addr
.s6_addr
[(plen
+ 7) / 8 - 1] &=
281 ((0xff00 >> (plen
% 8)) & 0xff);
283 sprintf(buf
, "%s/%d", getname6((char *)&addr
), plen
);
284 return 1 + (plen
+ 7) / 8;
289 bgp_attr_print(const struct bgp_attr
*attr
, const u_char
*dat
, int len
)
302 switch (attr
->bgpa_type
) {
305 printf(" invalid len");
307 printf(" %s", bgp_attr_origin(p
[0]));
309 case BGPTYPE_AS_PATH
:
311 printf(" invalid len");
314 for (i
= 0; i
< len
; i
+= 2)
315 printf(" %u", ntohs(*(u_int16_t
*)&p
[i
]));
317 case BGPTYPE_NEXT_HOP
:
319 printf(" invalid len");
321 printf(" %s", getname(p
));
323 case BGPTYPE_MULTI_EXIT_DISC
:
324 case BGPTYPE_LOCAL_PREF
:
326 printf(" invalid len");
328 printf(" %u", (u_int32_t
)ntohl(*(u_int32_t
*)p
));
330 case BGPTYPE_ATOMIC_AGGREGATE
:
332 printf(" invalid len");
334 case BGPTYPE_AGGREGATOR
:
336 printf(" invalid len");
339 printf(" AS #%u, origin %s", ntohs(*(u_int16_t
*)p
),
342 case BGPTYPE_MP_REACH_NLRI
:
343 af
= ntohs(*(u_int16_t
*)p
);
346 printf(" %s vendor specific,", af_name(af
));
348 printf(" %s %s,", af_name(af
),
349 bgp_attr_nlri_safi(safi
));
353 if (af
== AFNUM_INET
)
356 else if (af
== AFNUM_INET6
)
365 if (af
== AFNUM_INET
)
368 else if (af
== AFNUM_INET6
)
372 for (i
= 0; i
< tlen
; i
+= advance
) {
373 if (af
== AFNUM_INET
)
374 printf(" %s", getname(p
+ 1 + i
));
376 else if (af
== AFNUM_INET6
)
377 printf(" %s", getname6(p
+ 1 + i
));
387 printf(" %u snpa", snpa
);
388 for (/*nothing*/; snpa
> 0; snpa
--) {
389 printf("(%d bytes)", p
[0]);
396 while (len
- (p
- dat
) > 0) {
397 if (af
== AFNUM_INET
)
398 advance
= decode_prefix4(p
, buf
, sizeof(buf
));
400 else if (af
== AFNUM_INET6
)
401 advance
= decode_prefix6(p
, buf
, sizeof(buf
));
410 case BGPTYPE_MP_UNREACH_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
));
422 while (len
- (p
- dat
) > 0) {
423 if (af
== AFNUM_INET
)
424 advance
= decode_prefix4(p
, buf
, sizeof(buf
));
426 else if (af
== AFNUM_INET6
)
427 advance
= decode_prefix6(p
, buf
, sizeof(buf
));
440 bgp_open_print(const u_char
*dat
, int length
)
442 struct bgp_open bgpo
;
445 memcpy(&bgpo
, dat
, sizeof(bgpo
));
446 hlen
= ntohs(bgpo
.bgpo_len
);
448 printf(": Version %d,", bgpo
.bgpo_version
);
449 printf(" AS #%u,", ntohs(bgpo
.bgpo_myas
));
450 printf(" Holdtime %u,", ntohs(bgpo
.bgpo_holdtime
));
451 printf(" ID %s,", getname((char *)&bgpo
.bgpo_id
));
452 printf(" Option length %u", bgpo
.bgpo_optlen
);
456 bgp_update_print(const u_char
*dat
, int length
)
459 struct bgp_attr bgpa
;
465 memcpy(&bgp
, dat
, sizeof(bgp
));
466 hlen
= ntohs(bgp
.bgp_len
);
467 p
= dat
+ BGP_SIZE
; /*XXX*/
470 /* Unfeasible routes */
471 len
= ntohs(*(u_int16_t
*)p
);
473 printf(" (Withdrawn routes: %d bytes)", len
);
477 len
= ntohs(*(u_int16_t
*)p
);
479 /* do something more useful!*/
481 printf(" (Path attributes:"); /* ) */
482 while (i
< 2 + len
) {
485 memcpy(&bgpa
, &p
[i
], sizeof(bgpa
));
486 alen
= bgp_attr_len(&bgpa
);
487 aoff
= bgp_attr_off(&bgpa
);
489 printf(" ("); /* ) */
490 printf("%s", bgp_attr_type(bgpa
.bgpa_type
));
491 if (bgpa
.bgpa_flags
) {
493 bgpa
.bgpa_flags
& 0x80 ? "O" : "",
494 bgpa
.bgpa_flags
& 0x40 ? "T" : "",
495 bgpa
.bgpa_flags
& 0x20 ? "P" : "",
496 bgpa
.bgpa_flags
& 0x00 ? "E" : "");
499 bgp_attr_print(&bgpa
, &p
[i
+ aoff
], alen
);
503 proto_tree_add_text(subtree2
, p
- pd
+ i
+ aoff
, alen
,
504 "Unknown (%d bytes)", alen
);
523 bgp_notification_print(const u_char
*dat
, int length
)
525 struct bgp_notification bgpn
;
528 memcpy(&bgpn
, dat
, sizeof(bgpn
));
529 hlen
= ntohs(bgpn
.bgpn_len
);
531 printf(": error %s,", bgp_notify_major(bgpn
.bgpn_major
));
532 printf(" subcode %s",
533 bgp_notify_minor(bgpn
.bgpn_major
, bgpn
.bgpn_minor
));
537 bgp_header_print(const u_char
*dat
, int length
, int newline
)
541 memcpy(&bgp
, dat
, sizeof(bgp
));
542 printf(newline
? "\n\t" : " ");
543 printf("(%s", bgp_type(bgp
.bgp_type
)); /* ) */
545 switch (bgp
.bgp_type
) {
547 bgp_open_print(dat
, length
);
550 bgp_update_print(dat
, length
);
552 case BGP_NOTIFICATION
:
553 bgp_notification_print(dat
, length
);
562 bgp_print(const u_char
*dat
, int length
)
566 const u_char marker
[] = {
567 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
568 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
575 if (snapend
< dat
+ length
)
582 while (p
< snapend
) {
583 if (!TTEST2(p
[0], 1))
590 if (!TTEST2(p
[0], sizeof(marker
)))
592 if (memcmp(p
, marker
, sizeof(marker
)) != 0) {
597 /* found BGP header */
598 TCHECK2(p
[0], BGP_SIZE
); /*XXX*/
599 memcpy(&bgp
, p
, sizeof(bgp
));
601 hlen
= ntohs(bgp
.bgp_len
);
602 if (TTEST2(p
[0], hlen
)) {
603 bgp_header_print(p
, hlen
, vflag
? newline
: 0);
607 printf(" [|BGP %s]", bgp_type(bgp
.bgp_type
));