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.40 2002-07-22 23:00:22 hannes Exp $";
39 #include <sys/param.h>
41 #include <sys/types.h>
42 #include <sys/socket.h>
44 #include <netinet/in.h>
51 #include "interface.h"
52 #include "addrtoname.h"
56 u_int8_t bgp_marker
[16];
60 #define BGP_SIZE 19 /* unaligned */
64 #define BGP_NOTIFICATION 3
65 #define BGP_KEEPALIVE 4
66 #define BGP_ROUTE_REFRESH 5
68 static struct tok bgp_msg_values
[] = {
70 { BGP_UPDATE
, "Update"},
71 { BGP_NOTIFICATION
, "Notification"},
72 { BGP_KEEPALIVE
, "Keepalive"},
73 { BGP_ROUTE_REFRESH
, "Route Refresh"},
78 u_int8_t bgpo_marker
[16];
81 u_int8_t bgpo_version
;
83 u_int16_t bgpo_holdtime
;
86 /* options should follow */
88 #define BGP_OPEN_SIZE 29 /* unaligned */
95 #define BGP_OPT_SIZE 2 /* some compilers may pad to 4 bytes */
97 struct bgp_notification
{
98 u_int8_t bgpn_marker
[16];
103 /* data should follow */
105 #define BGP_NOTIFICATION_SIZE 21 /* unaligned */
114 #define bgp_attr_len(p) \
115 (((p)->bgpa_flags & 0x10) ? \
116 ntohs((p)->bgpa_len.elen) : (p)->bgpa_len.len)
117 #define bgp_attr_off(p) \
118 (((p)->bgpa_flags & 0x10) ? 4 : 3)
121 #define BGPTYPE_ORIGIN 1
122 #define BGPTYPE_AS_PATH 2
123 #define BGPTYPE_NEXT_HOP 3
124 #define BGPTYPE_MULTI_EXIT_DISC 4
125 #define BGPTYPE_LOCAL_PREF 5
126 #define BGPTYPE_ATOMIC_AGGREGATE 6
127 #define BGPTYPE_AGGREGATOR 7
128 #define BGPTYPE_COMMUNITIES 8 /* RFC1997 */
129 #define BGPTYPE_ORIGINATOR_ID 9 /* RFC1998 */
130 #define BGPTYPE_CLUSTER_LIST 10 /* RFC1998 */
131 #define BGPTYPE_DPA 11 /* draft-ietf-idr-bgp-dpa */
132 #define BGPTYPE_ADVERTISERS 12 /* RFC1863 */
133 #define BGPTYPE_RCID_PATH 13 /* RFC1863 */
134 #define BGPTYPE_MP_REACH_NLRI 14 /* RFC2283 */
135 #define BGPTYPE_MP_UNREACH_NLRI 15 /* RFC2283 */
136 #define BGPTYPE_EXTD_COMMUNITIES 16 /* draft-ietf-idr-bgp-ext-communities */
138 static struct tok bgp_attr_values
[] = {
139 { BGPTYPE_ORIGIN
, "Origin"},
140 { BGPTYPE_AS_PATH
, "AS Path"},
141 { BGPTYPE_NEXT_HOP
, "Next Hop"},
142 { BGPTYPE_MULTI_EXIT_DISC
, "Multi Exit Discriminator"},
143 { BGPTYPE_LOCAL_PREF
, "Local Preference"},
144 { BGPTYPE_ATOMIC_AGGREGATE
, "Atomic Aggregate"},
145 { BGPTYPE_AGGREGATOR
, "Aggregator"},
146 { BGPTYPE_COMMUNITIES
, "Community"},
147 { BGPTYPE_ORIGINATOR_ID
, "Originator ID"},
148 { BGPTYPE_CLUSTER_LIST
, "Cluster List"},
149 { BGPTYPE_DPA
, "DPA"},
150 { BGPTYPE_ADVERTISERS
, "Advertisers"},
151 { BGPTYPE_RCID_PATH
, "RCID Path / Cluster ID"},
152 { BGPTYPE_MP_REACH_NLRI
, "Multi-Protocol Reach NLRI"},
153 { BGPTYPE_MP_UNREACH_NLRI
, "Multi-Protocol Unreach NLRI"},
154 { BGPTYPE_EXTD_COMMUNITIES
, "Extended Community"},
155 { 255, "Reserved for development"},
159 #define BGP_OPT_AUTH 1
160 #define BGP_OPT_CAP 2
163 static struct tok bgp_opt_values
[] = {
164 { BGP_OPT_AUTH
, "Authentication Information"},
165 { BGP_OPT_CAP
, "Capabilities Advertisement"},
169 #define BGP_CAPCODE_MP 1
170 #define BGP_CAPCODE_RR 2
171 #define BGP_CAPCODE_RR_CISCO 128
173 static struct tok bgp_capcode_values
[] = {
174 { BGP_CAPCODE_MP
, "Multiprotocol Extensions"},
175 { BGP_CAPCODE_RR
, "Route Refresh"},
176 { BGP_CAPCODE_RR_CISCO
, "Route Refresh (Cisco)"},
180 #define BGP_NOTIFY_MAJOR_MSG 1
181 #define BGP_NOTIFY_MAJOR_OPEN 2
182 #define BGP_NOTIFY_MAJOR_UPDATE 3
183 #define BGP_NOTIFY_MAJOR_HOLDTIME 4
184 #define BGP_NOTIFY_MAJOR_FSM 5
185 #define BGP_NOTIFY_MAJOR_CEASE 6
187 static struct tok bgp_notify_major_values
[] = {
188 { BGP_NOTIFY_MAJOR_MSG
, "Message Header Error"},
189 { BGP_NOTIFY_MAJOR_OPEN
, "OPEN Message Error"},
190 { BGP_NOTIFY_MAJOR_UPDATE
, "UPDATE Message Error"},
191 { BGP_NOTIFY_MAJOR_HOLDTIME
,"Hold Timer Expired"},
192 { BGP_NOTIFY_MAJOR_FSM
, "Finite State Machine Error"},
193 { BGP_NOTIFY_MAJOR_CEASE
, "Cease"},
197 static struct tok bgp_notify_minor_msg_values
[] = {
198 { 1, "Connection Not Synchronized"},
199 { 2, "Bad Message Length"},
200 { 3, "Bad Message Type"},
204 static struct tok bgp_notify_minor_open_values
[] = {
205 { 1, "Unsupported Version Number"},
207 { 3, "Bad BGP Identifier"},
208 { 4, "Unsupported Optional Parameter"},
209 { 5, "Authentication Failure"},
210 { 6, "Unacceptable Hold Time"},
214 static struct tok bgp_notify_minor_update_values
[] = {
215 { 1, "Malformed Attribute List"},
216 { 2, "Unrecognized Well-known Attribute"},
217 { 3, "Missing Well-known Attribute"},
218 { 4, "Attribute Flags Error"},
219 { 5, "Attribute Length Error"},
220 { 6, "Invalid ORIGIN Attribute"},
221 { 7, "AS Routing Loop"},
222 { 8, "Invalid NEXT_HOP Attribute"},
223 { 9, "Optional Attribute Error"},
224 { 10, "Invalid Network Field"},
225 { 11, "Malformed AS_PATH"},
229 static struct tok bgp_origin_values
[] = {
236 /* Subsequent address family identifier, RFC2283 section 7 */
238 #define SAFNUM_UNICAST 1
239 #define SAFNUM_MULTICAST 2
240 #define SAFNUM_UNIMULTICAST 3
241 /* labeled BGP RFC3107 */
242 #define SAFNUM_LABUNICAST 4
243 /* Section 4.3.4 of draft-rosen-rfc2547bis-03.txt */
244 #define SAFNUM_VPNUNICAST 128
245 #define SAFNUM_VPNMULTICAST 129
246 #define SAFNUM_VPNUNIMULTICAST 130
248 #define BGP_VPN_RD_LEN 8
250 static struct tok bgp_safi_values
[] = {
251 { SAFNUM_RES
, "Reserved"},
252 { SAFNUM_UNICAST
, "Unicast"},
253 { SAFNUM_MULTICAST
, "Multicast"},
254 { SAFNUM_UNIMULTICAST
, "Unicast+Multicast"},
255 { SAFNUM_LABUNICAST
, "labeled Unicast"},
256 { SAFNUM_VPNUNICAST
, "labeled VPN Unicast"},
257 { SAFNUM_VPNMULTICAST
, "labeled VPN Multicast"},
258 { SAFNUM_VPNUNIMULTICAST
, "labeled VPN Unicast+Multicast"},
262 /* well-known community */
263 #define BGP_COMMUNITY_NO_EXPORT 0xffffff01
264 #define BGP_COMMUNITY_NO_ADVERT 0xffffff02
265 #define BGP_COMMUNITY_NO_EXPORT_SUBCONFED 0xffffff03
267 /* RFC1700 address family numbers */
269 #define AFNUM_INET6 2
272 #define AFNUM_BBN1822 5
277 #define AFNUM_X121 10
279 #define AFNUM_ATALK 12
280 #define AFNUM_DECNET 13
281 #define AFNUM_BANYAN 14
282 #define AFNUM_E164NSAP 15
283 /* draft-kompella-ppvpn-l2vpn */
284 #define AFNUM_L2VPN 196 /* still to be approved by IANA */
286 static struct tok bgp_afi_values
[] = {
288 { AFNUM_INET
, "IPv4"},
289 { AFNUM_INET6
, "IPv6"},
290 { AFNUM_NSAP
, "NSAP"},
291 { AFNUM_HDLC
, "HDLC"},
292 { AFNUM_BBN1822
, "BBN 1822"},
294 { AFNUM_E163
, "E.163"},
295 { AFNUM_E164
, "E.164"},
296 { AFNUM_F69
, "F.69"},
297 { AFNUM_X121
, "X.121"},
298 { AFNUM_IPX
, "Novell IPX"},
299 { AFNUM_ATALK
, "Appletalk"},
300 { AFNUM_DECNET
, "Decnet IV"},
301 { AFNUM_BANYAN
, "Banyan Vines"},
302 { AFNUM_E164NSAP
, "E.164 with NSAP subaddress"},
303 { AFNUM_L2VPN
, "Layer-2 VPN"},
307 static struct tok bgp_extd_comm_subtype_values
[] = {
315 decode_prefix4(const u_char
*pptr
, char *buf
, u_int buflen
)
321 if (plen
< 0 || 32 < plen
)
324 memset(&addr
, 0, sizeof(addr
));
325 memcpy(&addr
, &pptr
[1], (plen
+ 7) / 8);
327 ((u_char
*)&addr
)[(plen
+ 7) / 8 - 1] &=
328 ((0xff00 >> (plen
% 8)) & 0xff);
330 snprintf(buf
, buflen
, "%s/%d", getname((u_char
*)&addr
), plen
);
331 return 1 + (plen
+ 7) / 8;
335 decode_labeled_prefix4(const u_char
*pptr
, char *buf
, u_int buflen
)
340 plen
= pptr
[0]; /* get prefix length */
342 /* this is one of the weirdnesses of rfc3107
343 the label length (actually the label + COS bits)
344 is added to the prefix length;
345 we also do only read out just one label -
346 there is no real application for advertisement of
347 stacked labels in a a single BGP message
350 plen
-=24; /* adjust prefixlen - labellength */
352 if (plen
< 0 || 32 < plen
)
355 memset(&addr
, 0, sizeof(addr
));
356 memcpy(&addr
, &pptr
[4], (plen
+ 7) / 8);
358 ((u_char
*)&addr
)[(plen
+ 7) / 8 - 1] &=
359 ((0xff00 >> (plen
% 8)) & 0xff);
361 /* the label may get offsetted by 4 bits so lets shift it right */
362 snprintf(buf
, buflen
, "%s/%d, label:%u %s",
363 getname((u_char
*)&addr
),
365 EXTRACT_24BITS(pptr
+1)>>4,
366 ((pptr
[3]&1)==0) ? "(BOGUS: Bottom of Stack NOT set!)" : "(bottom)" );
368 return 4 + (plen
+ 7) / 8;
373 bgp_vpn_rd_print (const u_char
*pptr
) {
375 /* allocate space for the following string
376 * xxx.xxx.xxx.xxx:xxxxx
377 * 21 bytes plus one termination byte */
381 /* ok lets load the RD format */
382 switch (EXTRACT_16BITS(pptr
)) {
383 /* AS:IP-address fmt*/
385 pos
+=sprintf(pos
, "%u:%s",
386 EXTRACT_16BITS(pptr
+2),
389 /* IP-address:AS fmt*/
391 pos
+=sprintf(pos
, "%s:%u",
393 EXTRACT_16BITS(pptr
+6));
396 pos
+=sprintf(pos
, "unknown RD format");
404 decode_labeled_vpn_prefix4(const u_char
*pptr
, char *buf
, u_int buflen
)
409 plen
= pptr
[0]; /* get prefix length */
411 plen
-=(24+64); /* adjust prefixlen - labellength - RD len*/
413 if (plen
< 0 || 32 < plen
)
416 memset(&addr
, 0, sizeof(addr
));
417 memcpy(&addr
, &pptr
[12], (plen
+ 7) / 8);
419 ((u_char
*)&addr
)[(plen
+ 7) / 8 - 1] &=
420 ((0xff00 >> (plen
% 8)) & 0xff);
422 /* the label may get offsetted by 4 bits so lets shift it right */
423 snprintf(buf
, buflen
, "RD: %s, %s/%d, label:%u %s",
424 bgp_vpn_rd_print(pptr
+4),
425 getname((u_char
*)&addr
),
427 EXTRACT_24BITS(pptr
+1)>>4,
428 ((pptr
[3]&1)==0) ? "(BOGUS: Bottom of Stack NOT set!)" : "(bottom)" );
430 return 12 + (plen
+ 7) / 8;
435 decode_prefix6(const u_char
*pd
, char *buf
, u_int buflen
)
437 struct in6_addr addr
;
441 if (plen
< 0 || 128 < plen
)
444 memset(&addr
, 0, sizeof(addr
));
445 memcpy(&addr
, &pd
[1], (plen
+ 7) / 8);
447 addr
.s6_addr
[(plen
+ 7) / 8 - 1] &=
448 ((0xff00 >> (plen
% 8)) & 0xff);
450 snprintf(buf
, buflen
, "%s/%d", getname6((u_char
*)&addr
), plen
);
451 return 1 + (plen
+ 7) / 8;
456 bgp_attr_print(const struct bgp_attr
*attr
, const u_char
*pptr
, int len
)
464 char buf
[MAXHOSTNAMELEN
+ 100];
469 switch (attr
->bgpa_type
) {
472 printf("invalid len");
474 printf("%s", tok2str(bgp_origin_values
, "Unknown Origin Typecode", tptr
[0]));
476 case BGPTYPE_AS_PATH
:
478 printf("invalid len");
485 while (tptr
< pptr
+ len
) {
487 * under RFC1965, p[0] means:
488 * 1: AS_SET 2: AS_SEQUENCE
489 * 3: AS_CONFED_SET 4: AS_CONFED_SEQUENCE
491 if (tptr
[0] == 3 || tptr
[0] == 4)
493 printf("%s", (tptr
[0] & 1) ? "{" : "");
494 for (i
= 0; i
< tptr
[1] * 2; i
+= 2) {
495 printf("%s%u", i
== 0 ? "" : " ",
496 EXTRACT_16BITS(&tptr
[2 + i
]));
498 printf("%s", (tptr
[0] & 1) ? "}" : "");
499 tptr
+= 2 + tptr
[1] * 2;
502 case BGPTYPE_NEXT_HOP
:
504 printf("invalid len");
506 printf("%s", getname(tptr
));
508 case BGPTYPE_MULTI_EXIT_DISC
:
509 case BGPTYPE_LOCAL_PREF
:
511 printf("invalid len");
513 printf("%u", EXTRACT_32BITS(tptr
));
515 case BGPTYPE_ATOMIC_AGGREGATE
:
517 printf("invalid len");
519 case BGPTYPE_AGGREGATOR
:
521 printf("invalid len");
524 printf(" AS #%u, origin %s", EXTRACT_16BITS(tptr
),
527 case BGPTYPE_COMMUNITIES
:
529 printf("invalid len");
534 comm
= EXTRACT_32BITS(tptr
);
536 case BGP_COMMUNITY_NO_EXPORT
:
537 printf(" NO_EXPORT");
539 case BGP_COMMUNITY_NO_ADVERT
:
540 printf(" NO_ADVERTISE");
542 case BGP_COMMUNITY_NO_EXPORT_SUBCONFED
:
543 printf(" NO_EXPORT_SUBCONFED");
547 (comm
>> 16) & 0xffff,
549 (tlen
>4) ? ", " : "");
556 case BGPTYPE_ORIGINATOR_ID
:
558 printf("invalid len");
561 printf("%s",getname(tptr
));
563 case BGPTYPE_CLUSTER_LIST
:
567 (tlen
>4) ? ", " : "");
572 case BGPTYPE_MP_REACH_NLRI
:
573 af
= EXTRACT_16BITS(tptr
);
576 printf("\n\t AFI: %s (%u), %sSAFI: %s (%u)",
577 tok2str(bgp_afi_values
, "Unknown AFI", af
),
579 (safi
>128) ? "vendor specific " : "", /* 128 is meanwhile wellknown */
580 tok2str(bgp_safi_values
, "Unknown SAFI", safi
),
583 if (af
== AFNUM_INET
)
586 else if (af
== AFNUM_INET6
)
590 printf("\n\t no AFI %u decoder",af
);
592 print_unknown_data(tptr
,"\n\t ",tlen
);
602 printf("\n\t nexthop: ");
608 case SAFNUM_MULTICAST
:
609 case SAFNUM_UNIMULTICAST
:
610 case SAFNUM_LABUNICAST
:
611 printf("%s",getname(tptr
));
612 tlen
-= sizeof(struct in_addr
);
613 tptr
+= sizeof(struct in_addr
);
615 case SAFNUM_VPNUNICAST
:
616 case SAFNUM_VPNMULTICAST
:
617 case SAFNUM_VPNUNIMULTICAST
:
619 bgp_vpn_rd_print(tptr
),
620 getname(tptr
+BGP_VPN_RD_LEN
));
621 tlen
-= (sizeof(struct in_addr
)+BGP_VPN_RD_LEN
);
622 tptr
+= (sizeof(struct in_addr
)+BGP_VPN_RD_LEN
);
625 printf("no SAFI %u decoder",safi
);
627 print_unknown_data(tptr
,"\n\t ",tlen
);
635 case SAFNUM_MULTICAST
:
636 case SAFNUM_UNIMULTICAST
:
637 case SAFNUM_LABUNICAST
:
638 printf("%s", getname6(tptr
));
639 tlen
-= sizeof(struct in6_addr
);
640 tptr
+= sizeof(struct in6_addr
);
641 case SAFNUM_VPNUNICAST
:
642 case SAFNUM_VPNMULTICAST
:
643 case SAFNUM_VPNUNIMULTICAST
:
645 bgp_vpn_rd_print(tptr
),
646 getname6(tptr
+BGP_VPN_RD_LEN
));
647 tlen
-= (sizeof(struct in6_addr
)+BGP_VPN_RD_LEN
);
648 tptr
+= (sizeof(struct in6_addr
)+BGP_VPN_RD_LEN
);
651 printf("no SAFI %u decoder",safi
);
653 print_unknown_data(tptr
,"\n\t ",tlen
);
658 printf("no AFI %u decoder",af
);
660 print_unknown_data(tptr
,"\n\t ",tlen
);
671 printf("\n\t %u SNPA", snpa
);
672 for (/*nothing*/; snpa
> 0; snpa
--) {
673 printf("\n\t %d bytes", tptr
[0]);
680 while (len
- (tptr
- pptr
) > 0) {
685 case SAFNUM_MULTICAST
:
686 case SAFNUM_UNIMULTICAST
:
687 advance
= decode_prefix4(tptr
, buf
, sizeof(buf
));
688 printf("\n\t %s", buf
);
690 case SAFNUM_LABUNICAST
:
691 advance
= decode_labeled_prefix4(tptr
, buf
, sizeof(buf
));
692 printf("\n\t %s", buf
);
694 case SAFNUM_VPNUNICAST
:
695 case SAFNUM_VPNMULTICAST
:
696 case SAFNUM_VPNUNIMULTICAST
:
697 advance
= decode_labeled_vpn_prefix4(tptr
, buf
, sizeof(buf
));
698 printf("\n\t %s", buf
);
701 printf("\n\t no SAFI %u decoder",safi
);
703 print_unknown_data(tptr
-3,"\n\t ",tlen
);
713 case SAFNUM_MULTICAST
:
714 case SAFNUM_UNIMULTICAST
:
715 advance
= decode_prefix6(tptr
, buf
, sizeof(buf
));
716 printf("\n\t %s", buf
);
719 printf("\n\t no SAFI %u decoder ",safi
);
721 print_unknown_data(tptr
-3,"\n\t ",tlen
);
729 printf("\n\t no AFI %u decoder ",af
);
731 print_unknown_data(tptr
-3,"\n\t ",tlen
);
742 case BGPTYPE_MP_UNREACH_NLRI
:
743 af
= EXTRACT_16BITS(tptr
);
746 printf("\n\t AFI: %s (%u), %sSAFI: %s (%u)",
747 tok2str(bgp_afi_values
, "Unknown AFI", af
),
749 (safi
>128) ? "vendor specific " : "", /* 128 is meanwhile wellknown */
750 tok2str(bgp_safi_values
, "Unknown SAFI", safi
),
755 while (len
- (tptr
- pptr
) > 0) {
760 case SAFNUM_MULTICAST
:
761 case SAFNUM_UNIMULTICAST
:
762 advance
= decode_prefix4(tptr
, buf
, sizeof(buf
));
763 printf("\n\t %s", buf
);
765 case SAFNUM_LABUNICAST
:
766 advance
= decode_labeled_prefix4(tptr
, buf
, sizeof(buf
));
767 printf("\n\t %s", buf
);
769 case SAFNUM_VPNUNICAST
:
770 case SAFNUM_VPNMULTICAST
:
771 case SAFNUM_VPNUNIMULTICAST
:
772 advance
= decode_labeled_vpn_prefix4(tptr
, buf
, sizeof(buf
));
773 printf("\n\t %s", buf
);
776 printf("\n\t no SAFI %u decoder",safi
);
778 print_unknown_data(tptr
-3,"\n\t ",tlen
);
789 case SAFNUM_MULTICAST
:
790 case SAFNUM_UNIMULTICAST
:
791 advance
= decode_prefix6(tptr
, buf
, sizeof(buf
));
792 printf("\n\t %s", buf
);
795 printf("\n\t no SAFI %u decoder",safi
);
797 print_unknown_data(tptr
-3,"\n\t ",tlen
);
805 printf("\n\t no AFI %u decoder",af
);
807 print_unknown_data(tptr
-3,"\n\t ",tlen
);
816 case BGPTYPE_EXTD_COMMUNITIES
:
818 printf("invalid len");
822 u_int8_t extd_comm
,extd_comm_type
,extd_comm_subtype
;
824 extd_comm_type
=extd_comm
&0x3f;
825 extd_comm_subtype
=*(tptr
+1);
826 switch(extd_comm_type
) {
828 printf("%s%s%s:%u:%s%s",
829 (extd_comm
&0x80) ? "vendor-specific: " : "",
830 (extd_comm
&0x40) ? "non-transitive:" : "",
831 tok2str(bgp_extd_comm_subtype_values
,
833 extd_comm_subtype
&0x3f),
834 EXTRACT_16BITS(tptr
+2),
836 (tlen
>8) ? ", " : "");
839 printf("%s%s%s:%s:%u%s",
840 (extd_comm
&0x80) ? "vendor-specific: " : "",
841 (extd_comm
&0x40) ? "non-transitive:" : "",
842 tok2str(bgp_extd_comm_subtype_values
,
844 extd_comm_subtype
&0x3f),
846 EXTRACT_16BITS(tptr
+6),
847 (tlen
>8) ? ", " : "");
850 printf("%s%s%s:%u:%u%s",
851 (extd_comm
&0x80) ? "vendor-specific: " : "",
852 (extd_comm
&0x40) ? "non-transitive:" : "",
853 tok2str(bgp_extd_comm_subtype_values
,
855 extd_comm_subtype
&0x3f),
856 EXTRACT_32BITS(tptr
+2),
857 EXTRACT_16BITS(tptr
+6),
858 (tlen
>8) ? ", " : "");
862 printf("\n\t no typecode %u decoder",
864 print_unknown_data(tptr
,"\n\t ",8);
873 printf("\n\t no Attribute %u decoder",attr
->bgpa_type
); /* we have no decoder for the attribute */
875 print_unknown_data(pptr
,"\n\t ",len
);
878 if (vflag
&&len
) /* omit zero length attributes*/
879 print_unknown_data(pptr
,"\n\t ",len
);
883 bgp_open_print(const u_char
*dat
, int length
)
885 struct bgp_open bgpo
;
886 struct bgp_opt bgpopt
;
889 int i
,cap_type
,cap_len
;
891 TCHECK2(dat
[0], BGP_OPEN_SIZE
);
892 memcpy(&bgpo
, dat
, BGP_OPEN_SIZE
);
893 hlen
= ntohs(bgpo
.bgpo_len
);
895 printf("\n\t Version %d, ", bgpo
.bgpo_version
);
896 printf("my AS %u, ", ntohs(bgpo
.bgpo_myas
));
897 printf("Holdtime %us, ", ntohs(bgpo
.bgpo_holdtime
));
898 printf("ID %s", getname((u_char
*)&bgpo
.bgpo_id
));
899 printf("\n\t Optional parameters, length: %u", bgpo
.bgpo_optlen
);
902 opt
= &((const struct bgp_open
*)dat
)->bgpo_optlen
;
906 while (i
< bgpo
.bgpo_optlen
) {
907 TCHECK2(opt
[i
], BGP_OPT_SIZE
);
908 memcpy(&bgpopt
, &opt
[i
], BGP_OPT_SIZE
);
909 if (i
+ 2 + bgpopt
.bgpopt_len
> bgpo
.bgpo_optlen
) {
910 printf("\n\t Option %d, length: %u", bgpopt
.bgpopt_type
, bgpopt
.bgpopt_len
);
914 printf("\n\t Option %s (%u), length: %u",
915 tok2str(bgp_opt_values
,"Unknown", bgpopt
.bgpopt_type
),
919 /* now lets decode the options we know*/
920 switch(bgpopt
.bgpopt_type
) {
922 cap_type
=opt
[i
+BGP_OPT_SIZE
];
923 cap_len
=opt
[i
+BGP_OPT_SIZE
+1];
924 printf("\n\t %s, length: %u",
925 tok2str(bgp_capcode_values
,"Unknown", cap_type
),
929 printf("\n\t\tAFI %s (%u), SAFI %s (%u)",
930 tok2str(bgp_afi_values
,"Unknown", EXTRACT_16BITS(opt
+i
+BGP_OPT_SIZE
+2)),
931 EXTRACT_16BITS(opt
+i
+BGP_OPT_SIZE
+2),
932 tok2str(bgp_safi_values
,"Unknown", opt
[i
+BGP_OPT_SIZE
+5]),
933 opt
[i
+BGP_OPT_SIZE
+5]);
936 case BGP_CAPCODE_RR_CISCO
:
939 printf("\n\t\tno decoder for Capability %u",
946 printf("\n\t no decoder for option %u",
951 i
+= BGP_OPT_SIZE
+ bgpopt
.bgpopt_len
;
959 bgp_update_print(const u_char
*dat
, int length
)
962 struct bgp_attr bgpa
;
968 TCHECK2(dat
[0], BGP_SIZE
);
969 memcpy(&bgp
, dat
, BGP_SIZE
);
970 hlen
= ntohs(bgp
.bgp_len
);
971 p
= dat
+ BGP_SIZE
; /*XXX*/
973 /* Unfeasible routes */
974 len
= EXTRACT_16BITS(p
);
977 * Without keeping state from the original NLRI message,
978 * it's not possible to tell if this a v4 or v6 route,
979 * so only try to decode it if we're not v6 enabled.
982 printf("\n\t Withdrawn routes: %d bytes", len
);
984 char buf
[MAXHOSTNAMELEN
+ 100];
989 printf("\n\t Withdrawn routes:");
992 i
+= decode_prefix4(&p
[i
], buf
, sizeof(buf
));
993 printf("\n\t %s", buf
);
1000 len
= EXTRACT_16BITS(p
);
1002 /* do something more useful!*/
1004 while (i
< 2 + len
) {
1007 TCHECK2(p
[i
], sizeof(bgpa
));
1008 memcpy(&bgpa
, &p
[i
], sizeof(bgpa
));
1009 alen
= bgp_attr_len(&bgpa
);
1010 aoff
= bgp_attr_off(&bgpa
);
1012 printf("\n\t %s (%u), length: %u",
1013 tok2str(bgp_attr_values
, "Unknown Attribute", bgpa
.bgpa_type
),
1017 if (bgpa
.bgpa_flags
) {
1018 printf(", flags [%s%s%s%s",
1019 bgpa
.bgpa_flags
& 0x80 ? "O" : "",
1020 bgpa
.bgpa_flags
& 0x40 ? "T" : "",
1021 bgpa
.bgpa_flags
& 0x20 ? "P" : "",
1022 bgpa
.bgpa_flags
& 0x10 ? "E" : "");
1023 if (bgpa
.bgpa_flags
& 0xf)
1024 printf("+%x", bgpa
.bgpa_flags
& 0xf);
1027 bgp_attr_print(&bgpa
, &p
[i
+ aoff
], alen
);
1033 if (dat
+ length
> p
) {
1034 printf("\n\t Updated routes:");
1035 while (dat
+ length
> p
) {
1036 char buf
[MAXHOSTNAMELEN
+ 100];
1037 i
= decode_prefix4(p
, buf
, sizeof(buf
));
1038 printf("\n\t %s", buf
);
1050 bgp_notification_print(const u_char
*dat
, int length
)
1052 struct bgp_notification bgpn
;
1055 TCHECK2(dat
[0], BGP_NOTIFICATION_SIZE
);
1056 memcpy(&bgpn
, dat
, BGP_NOTIFICATION_SIZE
);
1057 hlen
= ntohs(bgpn
.bgpn_len
);
1059 printf(", Error - %s", tok2str(bgp_notify_major_values
, "Unknown", bgpn
.bgpn_major
));
1061 switch (bgpn
.bgpn_major
) {
1063 case BGP_NOTIFY_MAJOR_MSG
:
1064 printf(" subcode %s", tok2str(bgp_notify_minor_msg_values
, "Unknown", bgpn
.bgpn_minor
));
1066 case BGP_NOTIFY_MAJOR_OPEN
:
1067 printf(" subcode %s", tok2str(bgp_notify_minor_open_values
, "Unknown", bgpn
.bgpn_minor
));
1069 case BGP_NOTIFY_MAJOR_UPDATE
:
1070 printf(" subcode %s", tok2str(bgp_notify_minor_update_values
, "Unknown", bgpn
.bgpn_minor
));
1082 bgp_header_print(const u_char
*dat
, int length
)
1086 TCHECK2(dat
[0], BGP_SIZE
);
1087 memcpy(&bgp
, dat
, BGP_SIZE
);
1088 printf("\n\t%s Message (%u), length: %u ",
1089 tok2str(bgp_msg_values
, "Unknown", bgp
.bgp_type
),
1093 switch (bgp
.bgp_type
) {
1095 bgp_open_print(dat
, length
);
1098 bgp_update_print(dat
, length
);
1100 case BGP_NOTIFICATION
:
1101 bgp_notification_print(dat
, length
);
1106 /* we have no decoder for the BGP message */
1107 printf("\n\t no Message %u decoder",bgp
.bgp_type
);
1108 print_unknown_data(dat
,"\n\t ",length
);
1117 bgp_print(const u_char
*dat
, int length
)
1121 const u_char
*start
;
1122 const u_char marker
[] = {
1123 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
1124 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
1130 if (snapend
< dat
+ length
)
1137 while (p
< snapend
) {
1138 if (!TTEST2(p
[0], 1))
1145 if (!TTEST2(p
[0], sizeof(marker
)))
1147 if (memcmp(p
, marker
, sizeof(marker
)) != 0) {
1152 /* found BGP header */
1153 TCHECK2(p
[0], BGP_SIZE
); /*XXX*/
1154 memcpy(&bgp
, p
, BGP_SIZE
);
1159 hlen
= ntohs(bgp
.bgp_len
);
1161 if (TTEST2(p
[0], hlen
)) {
1162 bgp_header_print(p
, hlen
);
1166 printf("[|BGP %s]", tok2str(bgp_msg_values
, "Unknown Message Type",bgp
.bgp_type
));