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
29 * Extensively modified by Hannes Gredler (hannes@juniper.net) for more
30 * complete BGP support.
38 static const char rcsid
[] _U_
=
39 "@(#) $Header: /tcpdump/master/tcpdump/print-bgp.c,v 1.118 2007-12-07 15:54:52 hannes Exp $";
42 #include <tcpdump-stdinc.h>
47 #include "interface.h"
48 #include "decode_prefix.h"
49 #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 */
96 #define BGP_CAP_HEADER_SIZE 2 /* some compilers may pad to 4 bytes */
98 struct bgp_notification
{
99 u_int8_t bgpn_marker
[16];
105 #define BGP_NOTIFICATION_SIZE 21 /* unaligned */
107 struct bgp_route_refresh
{
108 u_int8_t bgp_marker
[16];
111 u_int8_t afi
[2]; /* the compiler messes this structure up */
112 u_int8_t res
; /* when doing misaligned sequences of int8 and int16 */
113 u_int8_t safi
; /* afi should be int16 - so we have to access it using */
114 }; /* EXTRACT_16BITS(&bgp_route_refresh->afi) (sigh) */
115 #define BGP_ROUTE_REFRESH_SIZE 23
117 #define bgp_attr_lenlen(flags, p) \
118 (((flags) & 0x10) ? 2 : 1)
119 #define bgp_attr_len(flags, p) \
120 (((flags) & 0x10) ? EXTRACT_16BITS(p) : *(p))
122 #define BGPTYPE_ORIGIN 1
123 #define BGPTYPE_AS_PATH 2
124 #define BGPTYPE_NEXT_HOP 3
125 #define BGPTYPE_MULTI_EXIT_DISC 4
126 #define BGPTYPE_LOCAL_PREF 5
127 #define BGPTYPE_ATOMIC_AGGREGATE 6
128 #define BGPTYPE_AGGREGATOR 7
129 #define BGPTYPE_COMMUNITIES 8 /* RFC1997 */
130 #define BGPTYPE_ORIGINATOR_ID 9 /* RFC1998 */
131 #define BGPTYPE_CLUSTER_LIST 10 /* RFC1998 */
132 #define BGPTYPE_DPA 11 /* draft-ietf-idr-bgp-dpa */
133 #define BGPTYPE_ADVERTISERS 12 /* RFC1863 */
134 #define BGPTYPE_RCID_PATH 13 /* RFC1863 */
135 #define BGPTYPE_MP_REACH_NLRI 14 /* RFC2283 */
136 #define BGPTYPE_MP_UNREACH_NLRI 15 /* RFC2283 */
137 #define BGPTYPE_EXTD_COMMUNITIES 16 /* draft-ietf-idr-bgp-ext-communities */
138 #define BGPTYPE_AS4_PATH 17 /* RFC4893 */
139 #define BGPTYPE_AGGREGATOR4 18 /* RFC4893 */
140 #define BGPTYPE_PMSI_TUNNEL 22 /* draft-ietf-l3vpn-2547bis-mcast-bgp-02.txt */
141 #define BGPTYPE_ATTR_SET 128 /* draft-marques-ppvpn-ibgp */
143 #define BGP_MP_NLRI_MINSIZE 3 /* End of RIB Marker detection */
145 static struct tok bgp_attr_values
[] = {
146 { BGPTYPE_ORIGIN
, "Origin"},
147 { BGPTYPE_AS_PATH
, "AS Path"},
148 { BGPTYPE_AS4_PATH
, "AS4 Path"},
149 { BGPTYPE_NEXT_HOP
, "Next Hop"},
150 { BGPTYPE_MULTI_EXIT_DISC
, "Multi Exit Discriminator"},
151 { BGPTYPE_LOCAL_PREF
, "Local Preference"},
152 { BGPTYPE_ATOMIC_AGGREGATE
, "Atomic Aggregate"},
153 { BGPTYPE_AGGREGATOR
, "Aggregator"},
154 { BGPTYPE_AGGREGATOR4
, "Aggregator4"},
155 { BGPTYPE_COMMUNITIES
, "Community"},
156 { BGPTYPE_ORIGINATOR_ID
, "Originator ID"},
157 { BGPTYPE_CLUSTER_LIST
, "Cluster List"},
158 { BGPTYPE_DPA
, "DPA"},
159 { BGPTYPE_ADVERTISERS
, "Advertisers"},
160 { BGPTYPE_RCID_PATH
, "RCID Path / Cluster ID"},
161 { BGPTYPE_MP_REACH_NLRI
, "Multi-Protocol Reach NLRI"},
162 { BGPTYPE_MP_UNREACH_NLRI
, "Multi-Protocol Unreach NLRI"},
163 { BGPTYPE_EXTD_COMMUNITIES
, "Extended Community"},
164 { BGPTYPE_PMSI_TUNNEL
, "PMSI Tunnel"},
165 { BGPTYPE_ATTR_SET
, "Attribute Set"},
166 { 255, "Reserved for development"},
171 #define BGP_AS_SEQUENCE 2
172 #define BGP_CONFED_AS_SEQUENCE 3 /* draft-ietf-idr-rfc3065bis-01 */
173 #define BGP_CONFED_AS_SET 4 /* draft-ietf-idr-rfc3065bis-01 */
175 #define BGP_AS_SEG_TYPE_MIN BGP_AS_SET
176 #define BGP_AS_SEG_TYPE_MAX BGP_CONFED_AS_SET
178 static struct tok bgp_as_path_segment_open_values
[] = {
179 { BGP_AS_SEQUENCE
, ""},
181 { BGP_CONFED_AS_SEQUENCE
, "( "},
182 { BGP_CONFED_AS_SET
, "({ "},
186 static struct tok bgp_as_path_segment_close_values
[] = {
187 { BGP_AS_SEQUENCE
, ""},
189 { BGP_CONFED_AS_SEQUENCE
, ")"},
190 { BGP_CONFED_AS_SET
, "})"},
194 #define BGP_OPT_AUTH 1
195 #define BGP_OPT_CAP 2
198 static struct tok bgp_opt_values
[] = {
199 { BGP_OPT_AUTH
, "Authentication Information"},
200 { BGP_OPT_CAP
, "Capabilities Advertisement"},
204 #define BGP_CAPCODE_MP 1
205 #define BGP_CAPCODE_RR 2
206 #define BGP_CAPCODE_ORF 3 /* XXX */
207 #define BGP_CAPCODE_RESTART 64 /* draft-ietf-idr-restart-05 */
208 #define BGP_CAPCODE_AS_NEW 65 /* XXX */
209 #define BGP_CAPCODE_DYN_CAP 67 /* XXX */
210 #define BGP_CAPCODE_RR_CISCO 128
212 static struct tok bgp_capcode_values
[] = {
213 { BGP_CAPCODE_MP
, "Multiprotocol Extensions"},
214 { BGP_CAPCODE_RR
, "Route Refresh"},
215 { BGP_CAPCODE_ORF
, "Cooperative Route Filtering"},
216 { BGP_CAPCODE_RESTART
, "Graceful Restart"},
217 { BGP_CAPCODE_AS_NEW
, "32-Bit AS Number"},
218 { BGP_CAPCODE_DYN_CAP
, "Dynamic Capability"},
219 { BGP_CAPCODE_RR_CISCO
, "Route Refresh (Cisco)"},
223 #define BGP_NOTIFY_MAJOR_MSG 1
224 #define BGP_NOTIFY_MAJOR_OPEN 2
225 #define BGP_NOTIFY_MAJOR_UPDATE 3
226 #define BGP_NOTIFY_MAJOR_HOLDTIME 4
227 #define BGP_NOTIFY_MAJOR_FSM 5
228 #define BGP_NOTIFY_MAJOR_CEASE 6
229 #define BGP_NOTIFY_MAJOR_CAP 7
231 static struct tok bgp_notify_major_values
[] = {
232 { BGP_NOTIFY_MAJOR_MSG
, "Message Header Error"},
233 { BGP_NOTIFY_MAJOR_OPEN
, "OPEN Message Error"},
234 { BGP_NOTIFY_MAJOR_UPDATE
, "UPDATE Message Error"},
235 { BGP_NOTIFY_MAJOR_HOLDTIME
,"Hold Timer Expired"},
236 { BGP_NOTIFY_MAJOR_FSM
, "Finite State Machine Error"},
237 { BGP_NOTIFY_MAJOR_CEASE
, "Cease"},
238 { BGP_NOTIFY_MAJOR_CAP
, "Capability Message Error"},
242 /* draft-ietf-idr-cease-subcode-02 */
243 #define BGP_NOTIFY_MINOR_CEASE_MAXPRFX 1
244 static struct tok bgp_notify_minor_cease_values
[] = {
245 { BGP_NOTIFY_MINOR_CEASE_MAXPRFX
, "Maximum Number of Prefixes Reached"},
246 { 2, "Administratively Shutdown"},
247 { 3, "Peer Unconfigured"},
248 { 4, "Administratively Reset"},
249 { 5, "Connection Rejected"},
250 { 6, "Other Configuration Change"},
251 { 7, "Connection Collision Resolution"},
255 static struct tok bgp_notify_minor_msg_values
[] = {
256 { 1, "Connection Not Synchronized"},
257 { 2, "Bad Message Length"},
258 { 3, "Bad Message Type"},
262 static struct tok bgp_notify_minor_open_values
[] = {
263 { 1, "Unsupported Version Number"},
265 { 3, "Bad BGP Identifier"},
266 { 4, "Unsupported Optional Parameter"},
267 { 5, "Authentication Failure"},
268 { 6, "Unacceptable Hold Time"},
269 { 7, "Capability Message Error"},
273 static struct tok bgp_notify_minor_update_values
[] = {
274 { 1, "Malformed Attribute List"},
275 { 2, "Unrecognized Well-known Attribute"},
276 { 3, "Missing Well-known Attribute"},
277 { 4, "Attribute Flags Error"},
278 { 5, "Attribute Length Error"},
279 { 6, "Invalid ORIGIN Attribute"},
280 { 7, "AS Routing Loop"},
281 { 8, "Invalid NEXT_HOP Attribute"},
282 { 9, "Optional Attribute Error"},
283 { 10, "Invalid Network Field"},
284 { 11, "Malformed AS_PATH"},
288 static struct tok bgp_notify_minor_cap_values
[] = {
289 { 1, "Invalid Action Value" },
290 { 2, "Invalid Capability Length" },
291 { 3, "Malformed Capability Value" },
292 { 4, "Unsupported Capability Code" },
296 static struct tok bgp_origin_values
[] = {
303 #define BGP_PMSI_TUNNEL_RSVP_P2MP 1
304 #define BGP_PMSI_TUNNEL_LDP_P2MP 2
305 #define BGP_PMSI_TUNNEL_PIM_SSM 3
306 #define BGP_PMSI_TUNNEL_PIM_SM 4
307 #define BGP_PMSI_TUNNEL_PIM_BIDIR 5
308 #define BGP_PMSI_TUNNEL_INGRESS 6
309 #define BGP_PMSI_TUNNEL_LDP_MP2MP 7
311 static struct tok bgp_pmsi_tunnel_values
[] = {
312 { BGP_PMSI_TUNNEL_RSVP_P2MP
, "RSVP-TE P2MP LSP"},
313 { BGP_PMSI_TUNNEL_LDP_P2MP
, "LDP P2MP LSP"},
314 { BGP_PMSI_TUNNEL_PIM_SSM
, "PIM-SSM Tree"},
315 { BGP_PMSI_TUNNEL_PIM_SM
, "PIM-SM Tree"},
316 { BGP_PMSI_TUNNEL_PIM_BIDIR
, "PIM-Bidir Tree"},
317 { BGP_PMSI_TUNNEL_INGRESS
, "Ingress Replication"},
318 { BGP_PMSI_TUNNEL_LDP_MP2MP
, "LDP MP2MP LSP"},
322 static struct tok bgp_pmsi_flag_values
[] = {
323 { 0x01, "Leaf Information required"},
328 /* Subsequent address family identifier, RFC2283 section 7 */
330 #define SAFNUM_UNICAST 1
331 #define SAFNUM_MULTICAST 2
332 #define SAFNUM_UNIMULTICAST 3
333 /* labeled BGP RFC3107 */
334 #define SAFNUM_LABUNICAST 4
335 /* draft-ietf-l3vpn-2547bis-mcast-bgp-02.txt */
336 #define SAFNUM_MULTICAST_VPN 5
337 #define SAFNUM_TUNNEL 64 /* XXX */
338 #define SAFNUM_VPLS 65 /* XXX */
339 /* draft-nalawade-idr-mdt-safi-03 */
340 #define SAFNUM_MDT 66
341 /* Section 4.3.4 of draft-rosen-rfc2547bis-03.txt */
342 #define SAFNUM_VPNUNICAST 128
343 #define SAFNUM_VPNMULTICAST 129
344 #define SAFNUM_VPNUNIMULTICAST 130
345 /* draft-marques-ppvpn-rt-constrain-01.txt */
346 #define SAFNUM_RT_ROUTING_INFO 132
348 #define BGP_VPN_RD_LEN 8
350 static struct tok bgp_safi_values
[] = {
351 { SAFNUM_RES
, "Reserved"},
352 { SAFNUM_UNICAST
, "Unicast"},
353 { SAFNUM_MULTICAST
, "Multicast"},
354 { SAFNUM_UNIMULTICAST
, "Unicast+Multicast"},
355 { SAFNUM_LABUNICAST
, "labeled Unicast"},
356 { SAFNUM_TUNNEL
, "Tunnel"},
357 { SAFNUM_VPLS
, "VPLS"},
358 { SAFNUM_MDT
, "MDT"},
359 { SAFNUM_VPNUNICAST
, "labeled VPN Unicast"},
360 { SAFNUM_VPNMULTICAST
, "labeled VPN Multicast"},
361 { SAFNUM_VPNUNIMULTICAST
, "labeled VPN Unicast+Multicast"},
362 { SAFNUM_RT_ROUTING_INFO
, "Route Target Routing Information"},
363 { SAFNUM_MULTICAST_VPN
, "Multicast VPN"},
367 /* well-known community */
368 #define BGP_COMMUNITY_NO_EXPORT 0xffffff01
369 #define BGP_COMMUNITY_NO_ADVERT 0xffffff02
370 #define BGP_COMMUNITY_NO_EXPORT_SUBCONFED 0xffffff03
372 /* Extended community type - draft-ietf-idr-bgp-ext-communities-05 */
373 #define BGP_EXT_COM_RT_0 0x0002 /* Route Target,Format AS(2bytes):AN(4bytes) */
374 #define BGP_EXT_COM_RT_1 0x0102 /* Route Target,Format IP address:AN(2bytes) */
375 #define BGP_EXT_COM_RT_2 0x0202 /* Route Target,Format AN(4bytes):local(2bytes) */
376 #define BGP_EXT_COM_RO_0 0x0003 /* Route Origin,Format AS(2bytes):AN(4bytes) */
377 #define BGP_EXT_COM_RO_1 0x0103 /* Route Origin,Format IP address:AN(2bytes) */
378 #define BGP_EXT_COM_RO_2 0x0203 /* Route Origin,Format AN(4bytes):local(2bytes) */
379 #define BGP_EXT_COM_LINKBAND 0x4004 /* Link Bandwidth,Format AS(2B):Bandwidth(4B) */
380 /* rfc2547 bgp-mpls-vpns */
381 #define BGP_EXT_COM_VPN_ORIGIN 0x0005 /* OSPF Domain ID / VPN of Origin - draft-rosen-vpns-ospf-bgp-mpls */
382 #define BGP_EXT_COM_VPN_ORIGIN2 0x0105 /* duplicate - keep for backwards compatability */
383 #define BGP_EXT_COM_VPN_ORIGIN3 0x0205 /* duplicate - keep for backwards compatability */
384 #define BGP_EXT_COM_VPN_ORIGIN4 0x8005 /* duplicate - keep for backwards compatability */
386 #define BGP_EXT_COM_OSPF_RTYPE 0x0306 /* OSPF Route Type,Format Area(4B):RouteType(1B):Options(1B) */
387 #define BGP_EXT_COM_OSPF_RTYPE2 0x8000 /* duplicate - keep for backwards compatability */
389 #define BGP_EXT_COM_OSPF_RID 0x0107 /* OSPF Router ID,Format RouterID(4B):Unused(2B) */
390 #define BGP_EXT_COM_OSPF_RID2 0x8001 /* duplicate - keep for backwards compatability */
392 #define BGP_EXT_COM_L2INFO 0x800a /* draft-kompella-ppvpn-l2vpn */
394 #define BGP_EXT_COM_SOURCE_AS 0x0009 /* RFC-ietf-l3vpn-2547bis-mcast-bgp-08.txt */
395 #define BGP_EXT_COM_VRF_RT_IMP 0x010b /* RFC-ietf-l3vpn-2547bis-mcast-bgp-08.txt */
396 #define BGP_EXT_COM_L2VPN_RT_0 0x000a /* L2VPN Identifier,Format AS(2bytes):AN(4bytes) */
397 #define BGP_EXT_COM_L2VPN_RT_1 0xF10a /* L2VPN Identifier,Format IP address:AN(2bytes) */
400 /* http://www.cisco.com/en/US/tech/tk436/tk428/technologies_tech_note09186a00801eb09a.shtml */
401 #define BGP_EXT_COM_EIGRP_GEN 0x8800
402 #define BGP_EXT_COM_EIGRP_METRIC_AS_DELAY 0x8801
403 #define BGP_EXT_COM_EIGRP_METRIC_REL_NH_BW 0x8802
404 #define BGP_EXT_COM_EIGRP_METRIC_LOAD_MTU 0x8803
405 #define BGP_EXT_COM_EIGRP_EXT_REMAS_REMID 0x8804
406 #define BGP_EXT_COM_EIGRP_EXT_REMPROTO_REMMETRIC 0x8805
408 static struct tok bgp_extd_comm_flag_values
[] = {
409 { 0x8000, "vendor-specific"},
410 { 0x4000, "non-transitive"},
414 static struct tok bgp_extd_comm_subtype_values
[] = {
415 { BGP_EXT_COM_RT_0
, "target"},
416 { BGP_EXT_COM_RT_1
, "target"},
417 { BGP_EXT_COM_RT_2
, "target"},
418 { BGP_EXT_COM_RO_0
, "origin"},
419 { BGP_EXT_COM_RO_1
, "origin"},
420 { BGP_EXT_COM_RO_2
, "origin"},
421 { BGP_EXT_COM_LINKBAND
, "link-BW"},
422 { BGP_EXT_COM_VPN_ORIGIN
, "ospf-domain"},
423 { BGP_EXT_COM_VPN_ORIGIN2
, "ospf-domain"},
424 { BGP_EXT_COM_VPN_ORIGIN3
, "ospf-domain"},
425 { BGP_EXT_COM_VPN_ORIGIN4
, "ospf-domain"},
426 { BGP_EXT_COM_OSPF_RTYPE
, "ospf-route-type"},
427 { BGP_EXT_COM_OSPF_RTYPE2
, "ospf-route-type"},
428 { BGP_EXT_COM_OSPF_RID
, "ospf-router-id"},
429 { BGP_EXT_COM_OSPF_RID2
, "ospf-router-id"},
430 { BGP_EXT_COM_L2INFO
, "layer2-info"},
431 { BGP_EXT_COM_EIGRP_GEN
, "eigrp-general-route (flag, tag)" },
432 { BGP_EXT_COM_EIGRP_METRIC_AS_DELAY
, "eigrp-route-metric (AS, delay)" },
433 { BGP_EXT_COM_EIGRP_METRIC_REL_NH_BW
, "eigrp-route-metric (reliability, nexthop, bandwidth)" },
434 { BGP_EXT_COM_EIGRP_METRIC_LOAD_MTU
, "eigrp-route-metric (load, MTU)" },
435 { BGP_EXT_COM_EIGRP_EXT_REMAS_REMID
, "eigrp-external-route (remote-AS, remote-ID)" },
436 { BGP_EXT_COM_EIGRP_EXT_REMPROTO_REMMETRIC
, "eigrp-external-route (remote-proto, remote-metric)" },
437 { BGP_EXT_COM_SOURCE_AS
, "source-AS" },
438 { BGP_EXT_COM_VRF_RT_IMP
, "vrf-route-import"},
439 { BGP_EXT_COM_L2VPN_RT_0
, "l2vpn-id"},
440 { BGP_EXT_COM_L2VPN_RT_1
, "l2vpn-id"},
444 /* OSPF codes for BGP_EXT_COM_OSPF_RTYPE draft-rosen-vpns-ospf-bgp-mpls */
445 #define BGP_OSPF_RTYPE_RTR 1 /* OSPF Router LSA */
446 #define BGP_OSPF_RTYPE_NET 2 /* OSPF Network LSA */
447 #define BGP_OSPF_RTYPE_SUM 3 /* OSPF Summary LSA */
448 #define BGP_OSPF_RTYPE_EXT 5 /* OSPF External LSA, note that ASBR doesn't apply to MPLS-VPN */
449 #define BGP_OSPF_RTYPE_NSSA 7 /* OSPF NSSA External*/
450 #define BGP_OSPF_RTYPE_SHAM 129 /* OSPF-MPLS-VPN Sham link */
451 #define BGP_OSPF_RTYPE_METRIC_TYPE 0x1 /* LSB of RTYPE Options Field */
453 static struct tok bgp_extd_comm_ospf_rtype_values
[] = {
454 { BGP_OSPF_RTYPE_RTR
, "Router" },
455 { BGP_OSPF_RTYPE_NET
, "Network" },
456 { BGP_OSPF_RTYPE_SUM
, "Summary" },
457 { BGP_OSPF_RTYPE_EXT
, "External" },
458 { BGP_OSPF_RTYPE_NSSA
,"NSSA External" },
459 { BGP_OSPF_RTYPE_SHAM
,"MPLS-VPN Sham" },
463 #define TOKBUFSIZE 128
464 static char astostr
[20];
469 * Convert an AS number into a string and return string pointer.
471 * Bepending on bflag is set or not, AS number is converted into ASDOT notation
472 * or plain number notation.
476 as_printf (char *str
, int size
, u_int asnum
)
478 if (!bflag
|| asnum
<= 0xFFFF) {
479 snprintf(str
, size
, "%u", asnum
);
481 snprintf(str
, size
, "%u.%u", asnum
>> 16, asnum
& 0xFFFF);
486 #define ITEMCHECK(minlen) if (itemlen < minlen) goto badtlv;
489 decode_prefix4(const u_char
*pptr
, u_int itemlen
, char *buf
, u_int buflen
)
492 u_int plen
, plenbytes
;
501 memset(&addr
, 0, sizeof(addr
));
502 plenbytes
= (plen
+ 7) / 8;
503 TCHECK2(pptr
[1], plenbytes
);
504 ITEMCHECK(plenbytes
);
505 memcpy(&addr
, &pptr
[1], plenbytes
);
507 ((u_char
*)&addr
)[plenbytes
- 1] &=
508 ((0xff00 >> (plen
% 8)) & 0xff);
510 snprintf(buf
, buflen
, "%s/%d", getname((u_char
*)&addr
), plen
);
511 return 1 + plenbytes
;
521 decode_labeled_prefix4(const u_char
*pptr
, u_int itemlen
, char *buf
, u_int buflen
)
524 u_int plen
, plenbytes
;
526 /* prefix length and label = 4 bytes */
529 plen
= pptr
[0]; /* get prefix length */
531 /* this is one of the weirdnesses of rfc3107
532 the label length (actually the label + COS bits)
533 is added to the prefix length;
534 we also do only read out just one label -
535 there is no real application for advertisement of
536 stacked labels in a a single BGP message
542 plen
-=24; /* adjust prefixlen - labellength */
548 memset(&addr
, 0, sizeof(addr
));
549 plenbytes
= (plen
+ 7) / 8;
550 TCHECK2(pptr
[4], plenbytes
);
551 ITEMCHECK(plenbytes
);
552 memcpy(&addr
, &pptr
[4], plenbytes
);
554 ((u_char
*)&addr
)[plenbytes
- 1] &=
555 ((0xff00 >> (plen
% 8)) & 0xff);
557 /* the label may get offsetted by 4 bits so lets shift it right */
558 snprintf(buf
, buflen
, "%s/%d, label:%u %s",
559 getname((u_char
*)&addr
),
561 EXTRACT_24BITS(pptr
+1)>>4,
562 ((pptr
[3]&1)==0) ? "(BOGUS: Bottom of Stack NOT set!)" : "(bottom)" );
564 return 4 + plenbytes
;
576 * print an ipv4 or ipv6 address into a buffer dependend on address length.
579 bgp_vpn_ip_print (const u_char
*pptr
, u_int addr_length
) {
581 /* worst case string is s fully formatted v6 address */
582 static char addr
[sizeof("1234:5678:89ab:cdef:1234:5678:89ab:cdef")];
585 switch(addr_length
) {
586 case (sizeof(struct in_addr
) << 3): /* 32 */
587 TCHECK2(pptr
[0], sizeof(struct in_addr
));
588 snprintf(pos
, sizeof(addr
), "%s", ipaddr_string(pptr
));
591 case (sizeof(struct in6_addr
) << 3): /* 128 */
592 TCHECK2(pptr
[0], sizeof(struct in6_addr
));
593 snprintf(pos
, sizeof(addr
), "%s", ip6addr_string(pptr
));
597 snprintf(pos
, sizeof(addr
), "bogus address length %u", addr_length
);
610 * print an multicast s,g entry into a buffer.
611 * the s,g entry is encoded like this.
613 * +-----------------------------------+
614 * | Multicast Source Length (1 octet) |
615 * +-----------------------------------+
616 * | Multicast Source (Variable) |
617 * +-----------------------------------+
618 * | Multicast Group Length (1 octet) |
619 * +-----------------------------------+
620 * | Multicast Group (Variable) |
621 * +-----------------------------------+
623 * return the number of bytes read from the wire.
626 bgp_vpn_sg_print (const u_char
*pptr
, char *buf
, u_int buflen
) {
628 u_int8_t addr_length
;
629 u_int total_length
, offset
;
633 /* Source address length, encoded in bits */
635 addr_length
= *pptr
++;
638 TCHECK2(pptr
[0], (addr_length
>> 3));
639 total_length
+= (addr_length
>> 3) + 1;
640 offset
= strlen(buf
);
642 snprintf(buf
+ offset
, buflen
- offset
, ", Source %s",
643 bgp_vpn_ip_print(pptr
, addr_length
));
644 pptr
+= (addr_length
>> 3);
647 /* Group address length, encoded in bits */
649 addr_length
= *pptr
++;
652 TCHECK2(pptr
[0], (addr_length
>> 3));
653 total_length
+= (addr_length
>> 3) + 1;
654 offset
= strlen(buf
);
656 snprintf(buf
+ offset
, buflen
- offset
, ", Group %s",
657 bgp_vpn_ip_print(pptr
, addr_length
));
658 pptr
+= (addr_length
>> 3);
662 return (total_length
);
666 /* RDs and RTs share the same semantics
667 * we use bgp_vpn_rd_print for
668 * printing route targets inside a NLRI */
670 bgp_vpn_rd_print (const u_char
*pptr
) {
672 /* allocate space for the largest possible string */
673 static char rd
[sizeof("xxxxxxxxxx:xxxxx (xxx.xxx.xxx.xxx:xxxxx)")];
676 /* ok lets load the RD format */
677 switch (EXTRACT_16BITS(pptr
)) {
679 /* 2-byte-AS:number fmt*/
681 snprintf(pos
, sizeof(rd
) - (pos
- rd
), "%u:%u (= %u.%u.%u.%u)",
682 EXTRACT_16BITS(pptr
+2),
683 EXTRACT_32BITS(pptr
+4),
684 *(pptr
+4), *(pptr
+5), *(pptr
+6), *(pptr
+7));
686 /* IP-address:AS fmt*/
689 snprintf(pos
, sizeof(rd
) - (pos
- rd
), "%u.%u.%u.%u:%u",
690 *(pptr
+2), *(pptr
+3), *(pptr
+4), *(pptr
+5), EXTRACT_16BITS(pptr
+6));
693 /* 4-byte-AS:number fmt*/
695 snprintf(pos
, sizeof(rd
) - (pos
- rd
), "%s:%u (%u.%u.%u.%u:%u)",
696 as_printf(astostr
, sizeof(astostr
), EXTRACT_32BITS(pptr
+2)),
697 EXTRACT_16BITS(pptr
+6), *(pptr
+2), *(pptr
+3), *(pptr
+4),
698 *(pptr
+5), EXTRACT_16BITS(pptr
+6));
701 snprintf(pos
, sizeof(rd
) - (pos
- rd
), "unknown RD format");
710 decode_rt_routing_info(const u_char
*pptr
, char *buf
, u_int buflen
)
712 u_int8_t route_target
[8];
716 plen
= pptr
[0]; /* get prefix length */
719 return 1; /* default route target */
724 plen
-=32; /* adjust prefix length */
729 memset(&route_target
, 0, sizeof(route_target
));
730 TCHECK2(pptr
[1], (plen
+ 7) / 8);
731 memcpy(&route_target
, &pptr
[1], (plen
+ 7) / 8);
733 ((u_char
*)&route_target
)[(plen
+ 7) / 8 - 1] &=
734 ((0xff00 >> (plen
% 8)) & 0xff);
736 snprintf(buf
, buflen
, "origin AS: %s, route target %s",
737 as_printf(astostr
, sizeof(astostr
), EXTRACT_32BITS(pptr
+1)),
738 bgp_vpn_rd_print((u_char
*)&route_target
));
740 return 5 + (plen
+ 7) / 8;
747 decode_labeled_vpn_prefix4(const u_char
*pptr
, char *buf
, u_int buflen
)
753 plen
= pptr
[0]; /* get prefix length */
758 plen
-=(24+64); /* adjust prefixlen - labellength - RD len*/
763 memset(&addr
, 0, sizeof(addr
));
764 TCHECK2(pptr
[12], (plen
+ 7) / 8);
765 memcpy(&addr
, &pptr
[12], (plen
+ 7) / 8);
767 ((u_char
*)&addr
)[(plen
+ 7) / 8 - 1] &=
768 ((0xff00 >> (plen
% 8)) & 0xff);
770 /* the label may get offsetted by 4 bits so lets shift it right */
771 snprintf(buf
, buflen
, "RD: %s, %s/%d, label:%u %s",
772 bgp_vpn_rd_print(pptr
+4),
773 getname((u_char
*)&addr
),
775 EXTRACT_24BITS(pptr
+1)>>4,
776 ((pptr
[3]&1)==0) ? "(BOGUS: Bottom of Stack NOT set!)" : "(bottom)" );
778 return 12 + (plen
+ 7) / 8;
785 * +-------------------------------+
787 * | RD:IPv4-address (12 octets) |
789 * +-------------------------------+
790 * | MDT Group-address (4 octets) |
791 * +-------------------------------+
794 #define MDT_VPN_NLRI_LEN 16
797 decode_mdt_vpn_nlri(const u_char
*pptr
, char *buf
, u_int buflen
)
801 const u_char
*vpn_ip
;
805 /* if the NLRI is not predefined length, quit.*/
806 if (*pptr
!= MDT_VPN_NLRI_LEN
* NBBY
)
816 TCHECK2(pptr
[0], sizeof(struct in_addr
));
818 pptr
+=sizeof(struct in_addr
);
820 /* MDT Group Address */
821 TCHECK2(pptr
[0], sizeof(struct in_addr
));
823 snprintf(buf
, buflen
, "RD: %s, VPN IP Address: %s, MC Group Address: %s",
824 bgp_vpn_rd_print(rd
), ipaddr_string(vpn_ip
), ipaddr_string(pptr
));
826 return MDT_VPN_NLRI_LEN
+ 1;
833 #define BGP_MULTICAST_VPN_ROUTE_TYPE_INTRA_AS_I_PMSI 1
834 #define BGP_MULTICAST_VPN_ROUTE_TYPE_INTER_AS_I_PMSI 2
835 #define BGP_MULTICAST_VPN_ROUTE_TYPE_S_PMSI 3
836 #define BGP_MULTICAST_VPN_ROUTE_TYPE_INTRA_AS_SEG_LEAF 4
837 #define BGP_MULTICAST_VPN_ROUTE_TYPE_SOURCE_ACTIVE 5
838 #define BGP_MULTICAST_VPN_ROUTE_TYPE_SHARED_TREE_JOIN 6
839 #define BGP_MULTICAST_VPN_ROUTE_TYPE_SOURCE_TREE_JOIN 7
841 static struct tok bgp_multicast_vpn_route_type_values
[] = {
842 { BGP_MULTICAST_VPN_ROUTE_TYPE_INTRA_AS_I_PMSI
, "Intra-AS I-PMSI"},
843 { BGP_MULTICAST_VPN_ROUTE_TYPE_INTER_AS_I_PMSI
, "Inter-AS I-PMSI"},
844 { BGP_MULTICAST_VPN_ROUTE_TYPE_S_PMSI
, "S-PMSI"},
845 { BGP_MULTICAST_VPN_ROUTE_TYPE_INTRA_AS_SEG_LEAF
, "Intra-AS Segment-Leaf"},
846 { BGP_MULTICAST_VPN_ROUTE_TYPE_SOURCE_ACTIVE
, "Source-Active"},
847 { BGP_MULTICAST_VPN_ROUTE_TYPE_SHARED_TREE_JOIN
, "Shared Tree Join"},
848 { BGP_MULTICAST_VPN_ROUTE_TYPE_SOURCE_TREE_JOIN
, "Source Tree Join"},
852 decode_multicast_vpn(const u_char
*pptr
, char *buf
, u_int buflen
)
854 u_int8_t route_type
, route_length
, addr_length
, sg_length
;
858 route_type
= *pptr
++;
859 route_length
= *pptr
++;
861 snprintf(buf
, buflen
, "Route-Type: %s (%u), length: %u",
862 tok2str(bgp_multicast_vpn_route_type_values
,
863 "Unknown", route_type
),
864 route_type
, route_length
);
867 case BGP_MULTICAST_VPN_ROUTE_TYPE_INTRA_AS_I_PMSI
:
868 TCHECK2(pptr
[0], BGP_VPN_RD_LEN
);
869 offset
= strlen(buf
);
870 snprintf(buf
+ offset
, buflen
- offset
, ", RD: %s, Originator %s",
871 bgp_vpn_rd_print(pptr
),
872 bgp_vpn_ip_print(pptr
+ BGP_VPN_RD_LEN
,
873 (route_length
- BGP_VPN_RD_LEN
) << 3));
875 case BGP_MULTICAST_VPN_ROUTE_TYPE_INTER_AS_I_PMSI
:
876 TCHECK2(pptr
[0], BGP_VPN_RD_LEN
+ 4);
877 offset
= strlen(buf
);
878 snprintf(buf
+ offset
, buflen
- offset
, ", RD: %s, Source-AS %s",
879 bgp_vpn_rd_print(pptr
),
880 as_printf(astostr
, sizeof(astostr
),
881 EXTRACT_32BITS(pptr
+ BGP_VPN_RD_LEN
)));
884 case BGP_MULTICAST_VPN_ROUTE_TYPE_S_PMSI
:
885 TCHECK2(pptr
[0], BGP_VPN_RD_LEN
);
886 offset
= strlen(buf
);
887 snprintf(buf
+ offset
, buflen
- offset
, ", RD: %s",
888 bgp_vpn_rd_print(pptr
));
889 pptr
+= BGP_VPN_RD_LEN
;
891 sg_length
= bgp_vpn_sg_print(pptr
, buf
, buflen
);
892 addr_length
= route_length
- sg_length
;
894 TCHECK2(pptr
[0], addr_length
);
895 offset
= strlen(buf
);
896 snprintf(buf
+ offset
, buflen
- offset
, ", Originator %s",
897 bgp_vpn_ip_print(pptr
, addr_length
<< 3));
900 case BGP_MULTICAST_VPN_ROUTE_TYPE_SOURCE_ACTIVE
:
901 TCHECK2(pptr
[0], BGP_VPN_RD_LEN
);
902 offset
= strlen(buf
);
903 snprintf(buf
+ offset
, buflen
- offset
, ", RD: %s",
904 bgp_vpn_rd_print(pptr
));
905 pptr
+= BGP_VPN_RD_LEN
;
907 bgp_vpn_sg_print(pptr
, buf
, buflen
);
910 case BGP_MULTICAST_VPN_ROUTE_TYPE_SHARED_TREE_JOIN
: /* fall through */
911 case BGP_MULTICAST_VPN_ROUTE_TYPE_SOURCE_TREE_JOIN
:
912 TCHECK2(pptr
[0], BGP_VPN_RD_LEN
);
913 offset
= strlen(buf
);
914 snprintf(buf
+ offset
, buflen
- offset
, ", RD: %s, Source-AS %s",
915 bgp_vpn_rd_print(pptr
),
916 as_printf(astostr
, sizeof(astostr
),
917 EXTRACT_32BITS(pptr
+ BGP_VPN_RD_LEN
)));
918 pptr
+= BGP_VPN_RD_LEN
;
920 bgp_vpn_sg_print(pptr
, buf
, buflen
);
924 * no per route-type printing yet.
926 case BGP_MULTICAST_VPN_ROUTE_TYPE_INTRA_AS_SEG_LEAF
:
931 return route_length
+ 2;
938 * As I remember, some versions of systems have an snprintf() that
939 * returns -1 if the buffer would have overflowed. If the return
940 * value is negative, set buflen to 0, to indicate that we've filled
943 * If the return value is greater than buflen, that means that
944 * the buffer would have overflowed; again, set buflen to 0 in
947 #define UPDATE_BUF_BUFLEN(buf, buflen, strlen) \
950 else if ((u_int)strlen>buflen) \
958 decode_labeled_vpn_l2(const u_char
*pptr
, char *buf
, u_int buflen
)
960 int plen
,tlen
,strlen
,tlv_type
,tlv_len
,ttlv_len
;
963 plen
=EXTRACT_16BITS(pptr
);
966 /* Old and new L2VPN NLRI share AFI/SAFI
967 * -> Assume a 12 Byte-length NLRI is auto-discovery-only
968 * and > 17 as old format. Complain for the middle case
971 /* assume AD-only with RD, BGPNH */
974 strlen
=snprintf(buf
, buflen
, "RD: %s, BGPNH: %s",
975 bgp_vpn_rd_print(pptr
),
976 /* need something like getname() here */
979 UPDATE_BUF_BUFLEN(buf
, buflen
, strlen
);
983 } else if (plen
>17) {
984 /* assume old format */
985 /* RD, ID, LBLKOFF, LBLBASE */
989 strlen
=snprintf(buf
, buflen
, "RD: %s, CE-ID: %u, Label-Block Offset: %u, Label Base %u",
990 bgp_vpn_rd_print(pptr
),
991 EXTRACT_16BITS(pptr
+8),
992 EXTRACT_16BITS(pptr
+10),
993 EXTRACT_24BITS(pptr
+12)>>4); /* the label is offsetted by 4 bits so lets shift it right */
994 UPDATE_BUF_BUFLEN(buf
, buflen
, strlen
);
998 /* ok now the variable part - lets read out TLVs*/
1002 TCHECK2(pptr
[0], 3);
1004 tlv_len
=EXTRACT_16BITS(pptr
);
1011 strlen
=snprintf(buf
,buflen
, "\n\t\tcircuit status vector (%u) length: %u: 0x",
1014 UPDATE_BUF_BUFLEN(buf
, buflen
, strlen
);
1016 ttlv_len
=ttlv_len
/8+1; /* how many bytes do we need to read ? */
1017 while (ttlv_len
>0) {
1020 strlen
=snprintf(buf
,buflen
, "%02x",*pptr
++);
1021 UPDATE_BUF_BUFLEN(buf
, buflen
, strlen
);
1028 strlen
=snprintf(buf
,buflen
, "\n\t\tunknown TLV #%u, length: %u",
1031 UPDATE_BUF_BUFLEN(buf
, buflen
, strlen
);
1035 tlen
-=(tlv_len
<<3); /* the tlv-length is expressed in bits so lets shift it right */
1040 /* complain bitterly ? */
1051 decode_prefix6(const u_char
*pd
, u_int itemlen
, char *buf
, u_int buflen
)
1053 struct in6_addr addr
;
1054 u_int plen
, plenbytes
;
1063 memset(&addr
, 0, sizeof(addr
));
1064 plenbytes
= (plen
+ 7) / 8;
1065 TCHECK2(pd
[1], plenbytes
);
1066 ITEMCHECK(plenbytes
);
1067 memcpy(&addr
, &pd
[1], plenbytes
);
1069 addr
.s6_addr
[plenbytes
- 1] &=
1070 ((0xff00 >> (plen
% 8)) & 0xff);
1072 snprintf(buf
, buflen
, "%s/%d", getname6((u_char
*)&addr
), plen
);
1073 return 1 + plenbytes
;
1083 decode_labeled_prefix6(const u_char
*pptr
, u_int itemlen
, char *buf
, u_int buflen
)
1085 struct in6_addr addr
;
1086 u_int plen
, plenbytes
;
1088 /* prefix length and label = 4 bytes */
1089 TCHECK2(pptr
[0], 4);
1091 plen
= pptr
[0]; /* get prefix length */
1096 plen
-=24; /* adjust prefixlen - labellength */
1102 memset(&addr
, 0, sizeof(addr
));
1103 plenbytes
= (plen
+ 7) / 8;
1104 TCHECK2(pptr
[4], plenbytes
);
1105 memcpy(&addr
, &pptr
[4], plenbytes
);
1107 addr
.s6_addr
[plenbytes
- 1] &=
1108 ((0xff00 >> (plen
% 8)) & 0xff);
1110 /* the label may get offsetted by 4 bits so lets shift it right */
1111 snprintf(buf
, buflen
, "%s/%d, label:%u %s",
1112 getname6((u_char
*)&addr
),
1114 EXTRACT_24BITS(pptr
+1)>>4,
1115 ((pptr
[3]&1)==0) ? "(BOGUS: Bottom of Stack NOT set!)" : "(bottom)" );
1117 return 4 + plenbytes
;
1127 decode_labeled_vpn_prefix6(const u_char
*pptr
, char *buf
, u_int buflen
)
1129 struct in6_addr addr
;
1133 plen
= pptr
[0]; /* get prefix length */
1138 plen
-=(24+64); /* adjust prefixlen - labellength - RD len*/
1143 memset(&addr
, 0, sizeof(addr
));
1144 TCHECK2(pptr
[12], (plen
+ 7) / 8);
1145 memcpy(&addr
, &pptr
[12], (plen
+ 7) / 8);
1147 addr
.s6_addr
[(plen
+ 7) / 8 - 1] &=
1148 ((0xff00 >> (plen
% 8)) & 0xff);
1150 /* the label may get offsetted by 4 bits so lets shift it right */
1151 snprintf(buf
, buflen
, "RD: %s, %s/%d, label:%u %s",
1152 bgp_vpn_rd_print(pptr
+4),
1153 getname6((u_char
*)&addr
),
1155 EXTRACT_24BITS(pptr
+1)>>4,
1156 ((pptr
[3]&1)==0) ? "(BOGUS: Bottom of Stack NOT set!)" : "(bottom)" );
1158 return 12 + (plen
+ 7) / 8;
1166 decode_clnp_prefix(const u_char
*pptr
, char *buf
, u_int buflen
)
1172 plen
= pptr
[0]; /* get prefix length */
1177 memset(&addr
, 0, sizeof(addr
));
1178 TCHECK2(pptr
[4], (plen
+ 7) / 8);
1179 memcpy(&addr
, &pptr
[4], (plen
+ 7) / 8);
1181 addr
[(plen
+ 7) / 8 - 1] &=
1182 ((0xff00 >> (plen
% 8)) & 0xff);
1184 snprintf(buf
, buflen
, "%s/%d",
1185 isonsap_string(addr
,(plen
+ 7) / 8),
1188 return 1 + (plen
+ 7) / 8;
1195 decode_labeled_vpn_clnp_prefix(const u_char
*pptr
, char *buf
, u_int buflen
)
1201 plen
= pptr
[0]; /* get prefix length */
1206 plen
-=(24+64); /* adjust prefixlen - labellength - RD len*/
1211 memset(&addr
, 0, sizeof(addr
));
1212 TCHECK2(pptr
[12], (plen
+ 7) / 8);
1213 memcpy(&addr
, &pptr
[12], (plen
+ 7) / 8);
1215 addr
[(plen
+ 7) / 8 - 1] &=
1216 ((0xff00 >> (plen
% 8)) & 0xff);
1218 /* the label may get offsetted by 4 bits so lets shift it right */
1219 snprintf(buf
, buflen
, "RD: %s, %s/%d, label:%u %s",
1220 bgp_vpn_rd_print(pptr
+4),
1221 isonsap_string(addr
,(plen
+ 7) / 8),
1223 EXTRACT_24BITS(pptr
+1)>>4,
1224 ((pptr
[3]&1)==0) ? "(BOGUS: Bottom of Stack NOT set!)" : "(bottom)" );
1226 return 12 + (plen
+ 7) / 8;
1233 * bgp_attr_get_as_size
1235 * Try to find the size of the ASs encoded in an as-path. It is not obvious, as
1236 * both Old speakers that do not support 4 byte AS, and the new speakers that do
1237 * support, exchange AS-Path with the same path-attribute type value 0x02.
1240 bgp_attr_get_as_size (u_int8_t bgpa_type
, const u_char
*pptr
, int len
)
1242 const u_char
*tptr
= pptr
;
1245 * If the path attribute is the optional AS4 path type, then we already
1246 * know, that ASs must be encoded in 4 byte format.
1248 if (bgpa_type
== BGPTYPE_AS4_PATH
) {
1253 * Let us assume that ASs are of 2 bytes in size, and check if the AS-Path
1254 * TLV is good. If not, ask the caller to try with AS encoded as 4 bytes
1257 while (tptr
< pptr
+ len
) {
1261 * If we do not find a valid segment type, our guess might be wrong.
1263 if (tptr
[0] < BGP_AS_SEG_TYPE_MIN
|| tptr
[0] > BGP_AS_SEG_TYPE_MAX
) {
1267 tptr
+= 2 + tptr
[1] * 2;
1271 * If we correctly reached end of the AS path attribute data content,
1272 * then most likely ASs were indeed encoded as 2 bytes.
1274 if (tptr
== pptr
+ len
) {
1281 * We can come here, either we did not have enough data, or if we
1282 * try to decode 4 byte ASs in 2 byte format. Either way, return 4,
1283 * so that calller can try to decode each AS as of 4 bytes. If indeed
1284 * there was not enough data, it will crib and end the parse anyways.
1290 bgp_attr_print(u_int atype
, const u_char
*pptr
, u_int len
)
1294 u_int8_t safi
, snpa
, nhlen
;
1295 union { /* copy buffer for bandwidth values */
1302 char buf
[MAXHOSTNAMELEN
+ 100];
1303 char tokbuf
[TOKBUFSIZE
];
1310 case BGPTYPE_ORIGIN
:
1312 printf("invalid len");
1315 printf("%s", tok2strbuf(bgp_origin_values
,
1316 "Unknown Origin Typecode",
1318 tokbuf
, sizeof(tokbuf
)));
1324 * Process AS4 byte path and AS2 byte path attributes here.
1326 case BGPTYPE_AS4_PATH
:
1327 case BGPTYPE_AS_PATH
:
1329 printf("invalid len");
1338 * BGP updates exchanged between New speakers that support 4
1339 * byte AS, ASs are always encoded in 4 bytes. There is no
1340 * definitive way to find this, just by the packet's
1341 * contents. So, check for packet's TLV's sanity assuming
1342 * 2 bytes first, and it does not pass, assume that ASs are
1343 * encoded in 4 bytes format and move on.
1345 as_size
= bgp_attr_get_as_size(atype
, pptr
, len
);
1347 while (tptr
< pptr
+ len
) {
1349 printf("%s", tok2strbuf(bgp_as_path_segment_open_values
,
1351 tokbuf
, sizeof(tokbuf
)));
1352 for (i
= 0; i
< tptr
[1] * as_size
; i
+= as_size
) {
1353 TCHECK2(tptr
[2 + i
], as_size
);
1355 as_printf(astostr
, sizeof(astostr
),
1357 EXTRACT_16BITS(&tptr
[2 + i
]) :
1358 EXTRACT_32BITS(&tptr
[2 + i
])));
1361 printf("%s", tok2strbuf(bgp_as_path_segment_close_values
,
1363 tokbuf
, sizeof(tokbuf
)));
1365 tptr
+= 2 + tptr
[1] * as_size
;
1368 case BGPTYPE_NEXT_HOP
:
1370 printf("invalid len");
1372 TCHECK2(tptr
[0], 4);
1373 printf("%s", getname(tptr
));
1376 case BGPTYPE_MULTI_EXIT_DISC
:
1377 case BGPTYPE_LOCAL_PREF
:
1379 printf("invalid len");
1381 TCHECK2(tptr
[0], 4);
1382 printf("%u", EXTRACT_32BITS(tptr
));
1385 case BGPTYPE_ATOMIC_AGGREGATE
:
1387 printf("invalid len");
1389 case BGPTYPE_AGGREGATOR
:
1392 * Depending on the AS encoded is of 2 bytes or of 4 bytes,
1393 * the length of this PA can be either 6 bytes or 8 bytes.
1395 if (len
!= 6 && len
!= 8) {
1396 printf("invalid len");
1399 TCHECK2(tptr
[0], len
);
1401 printf(" AS #%s, origin %s",
1402 as_printf(astostr
, sizeof(astostr
), EXTRACT_16BITS(tptr
)),
1405 printf(" AS #%s, origin %s",
1406 as_printf(astostr
, sizeof(astostr
),
1407 EXTRACT_32BITS(tptr
)), getname(tptr
+ 4));
1410 case BGPTYPE_AGGREGATOR4
:
1412 printf("invalid len");
1415 TCHECK2(tptr
[0], 8);
1416 printf(" AS #%s, origin %s",
1417 as_printf(astostr
, sizeof(astostr
), EXTRACT_32BITS(tptr
)),
1420 case BGPTYPE_COMMUNITIES
:
1422 printf("invalid len");
1427 TCHECK2(tptr
[0], 4);
1428 comm
= EXTRACT_32BITS(tptr
);
1430 case BGP_COMMUNITY_NO_EXPORT
:
1431 printf(" NO_EXPORT");
1433 case BGP_COMMUNITY_NO_ADVERT
:
1434 printf(" NO_ADVERTISE");
1436 case BGP_COMMUNITY_NO_EXPORT_SUBCONFED
:
1437 printf(" NO_EXPORT_SUBCONFED");
1441 (comm
>> 16) & 0xffff,
1443 (tlen
>4) ? ", " : "");
1450 case BGPTYPE_ORIGINATOR_ID
:
1452 printf("invalid len");
1455 TCHECK2(tptr
[0], 4);
1456 printf("%s",getname(tptr
));
1458 case BGPTYPE_CLUSTER_LIST
:
1460 printf("invalid len");
1464 TCHECK2(tptr
[0], 4);
1467 (tlen
>4) ? ", " : "");
1472 case BGPTYPE_MP_REACH_NLRI
:
1473 TCHECK2(tptr
[0], 3);
1474 af
= EXTRACT_16BITS(tptr
);
1477 printf("\n\t AFI: %s (%u), %sSAFI: %s (%u)",
1478 tok2strbuf(af_values
, "Unknown AFI", af
,
1479 tokbuf
, sizeof(tokbuf
)),
1481 (safi
>128) ? "vendor specific " : "", /* 128 is meanwhile wellknown */
1482 tok2strbuf(bgp_safi_values
, "Unknown SAFI", safi
,
1483 tokbuf
, sizeof(tokbuf
)),
1486 switch(af
<<8 | safi
) {
1487 case (AFNUM_INET
<<8 | SAFNUM_UNICAST
):
1488 case (AFNUM_INET
<<8 | SAFNUM_MULTICAST
):
1489 case (AFNUM_INET
<<8 | SAFNUM_UNIMULTICAST
):
1490 case (AFNUM_INET
<<8 | SAFNUM_LABUNICAST
):
1491 case (AFNUM_INET
<<8 | SAFNUM_RT_ROUTING_INFO
):
1492 case (AFNUM_INET
<<8 | SAFNUM_VPNUNICAST
):
1493 case (AFNUM_INET
<<8 | SAFNUM_VPNMULTICAST
):
1494 case (AFNUM_INET
<<8 | SAFNUM_VPNUNIMULTICAST
):
1495 case (AFNUM_INET
<<8 | SAFNUM_MULTICAST_VPN
):
1496 case (AFNUM_INET
<<8 | SAFNUM_MDT
):
1498 case (AFNUM_INET6
<<8 | SAFNUM_UNICAST
):
1499 case (AFNUM_INET6
<<8 | SAFNUM_MULTICAST
):
1500 case (AFNUM_INET6
<<8 | SAFNUM_UNIMULTICAST
):
1501 case (AFNUM_INET6
<<8 | SAFNUM_LABUNICAST
):
1502 case (AFNUM_INET6
<<8 | SAFNUM_VPNUNICAST
):
1503 case (AFNUM_INET6
<<8 | SAFNUM_VPNMULTICAST
):
1504 case (AFNUM_INET6
<<8 | SAFNUM_VPNUNIMULTICAST
):
1506 case (AFNUM_NSAP
<<8 | SAFNUM_UNICAST
):
1507 case (AFNUM_NSAP
<<8 | SAFNUM_MULTICAST
):
1508 case (AFNUM_NSAP
<<8 | SAFNUM_UNIMULTICAST
):
1509 case (AFNUM_NSAP
<<8 | SAFNUM_VPNUNICAST
):
1510 case (AFNUM_NSAP
<<8 | SAFNUM_VPNMULTICAST
):
1511 case (AFNUM_NSAP
<<8 | SAFNUM_VPNUNIMULTICAST
):
1512 case (AFNUM_L2VPN
<<8 | SAFNUM_VPNUNICAST
):
1513 case (AFNUM_L2VPN
<<8 | SAFNUM_VPNMULTICAST
):
1514 case (AFNUM_L2VPN
<<8 | SAFNUM_VPNUNIMULTICAST
):
1515 case (AFNUM_VPLS
<<8 | SAFNUM_VPLS
):
1518 TCHECK2(tptr
[0], tlen
);
1519 printf("\n\t no AFI %u / SAFI %u decoder",af
,safi
);
1521 print_unknown_data(tptr
,"\n\t ",tlen
);
1534 printf("\n\t nexthop: ");
1536 switch(af
<<8 | safi
) {
1537 case (AFNUM_INET
<<8 | SAFNUM_UNICAST
):
1538 case (AFNUM_INET
<<8 | SAFNUM_MULTICAST
):
1539 case (AFNUM_INET
<<8 | SAFNUM_UNIMULTICAST
):
1540 case (AFNUM_INET
<<8 | SAFNUM_LABUNICAST
):
1541 case (AFNUM_INET
<<8 | SAFNUM_RT_ROUTING_INFO
):
1542 case (AFNUM_INET
<<8 | SAFNUM_MULTICAST_VPN
):
1543 case (AFNUM_INET
<<8 | SAFNUM_MDT
):
1544 if (tlen
< (int)sizeof(struct in_addr
)) {
1545 printf("invalid len");
1548 TCHECK2(tptr
[0], sizeof(struct in_addr
));
1549 printf("%s",getname(tptr
));
1550 tlen
-= sizeof(struct in_addr
);
1551 tptr
+= sizeof(struct in_addr
);
1554 case (AFNUM_INET
<<8 | SAFNUM_VPNUNICAST
):
1555 case (AFNUM_INET
<<8 | SAFNUM_VPNMULTICAST
):
1556 case (AFNUM_INET
<<8 | SAFNUM_VPNUNIMULTICAST
):
1557 if (tlen
< (int)(sizeof(struct in_addr
)+BGP_VPN_RD_LEN
)) {
1558 printf("invalid len");
1561 TCHECK2(tptr
[0], sizeof(struct in_addr
)+BGP_VPN_RD_LEN
);
1562 printf("RD: %s, %s",
1563 bgp_vpn_rd_print(tptr
),
1564 getname(tptr
+BGP_VPN_RD_LEN
));
1565 tlen
-= (sizeof(struct in_addr
)+BGP_VPN_RD_LEN
);
1566 tptr
+= (sizeof(struct in_addr
)+BGP_VPN_RD_LEN
);
1570 case (AFNUM_INET6
<<8 | SAFNUM_UNICAST
):
1571 case (AFNUM_INET6
<<8 | SAFNUM_MULTICAST
):
1572 case (AFNUM_INET6
<<8 | SAFNUM_UNIMULTICAST
):
1573 case (AFNUM_INET6
<<8 | SAFNUM_LABUNICAST
):
1574 if (tlen
< (int)sizeof(struct in6_addr
)) {
1575 printf("invalid len");
1578 TCHECK2(tptr
[0], sizeof(struct in6_addr
));
1579 printf("%s", getname6(tptr
));
1580 tlen
-= sizeof(struct in6_addr
);
1581 tptr
+= sizeof(struct in6_addr
);
1584 case (AFNUM_INET6
<<8 | SAFNUM_VPNUNICAST
):
1585 case (AFNUM_INET6
<<8 | SAFNUM_VPNMULTICAST
):
1586 case (AFNUM_INET6
<<8 | SAFNUM_VPNUNIMULTICAST
):
1587 if (tlen
< (int)(sizeof(struct in6_addr
)+BGP_VPN_RD_LEN
)) {
1588 printf("invalid len");
1591 TCHECK2(tptr
[0], sizeof(struct in6_addr
)+BGP_VPN_RD_LEN
);
1592 printf("RD: %s, %s",
1593 bgp_vpn_rd_print(tptr
),
1594 getname6(tptr
+BGP_VPN_RD_LEN
));
1595 tlen
-= (sizeof(struct in6_addr
)+BGP_VPN_RD_LEN
);
1596 tptr
+= (sizeof(struct in6_addr
)+BGP_VPN_RD_LEN
);
1600 case (AFNUM_VPLS
<<8 | SAFNUM_VPLS
):
1601 case (AFNUM_L2VPN
<<8 | SAFNUM_VPNUNICAST
):
1602 case (AFNUM_L2VPN
<<8 | SAFNUM_VPNMULTICAST
):
1603 case (AFNUM_L2VPN
<<8 | SAFNUM_VPNUNIMULTICAST
):
1604 if (tlen
< (int)sizeof(struct in_addr
)) {
1605 printf("invalid len");
1608 TCHECK2(tptr
[0], sizeof(struct in_addr
));
1609 printf("%s", getname(tptr
));
1610 tlen
-= (sizeof(struct in_addr
));
1611 tptr
+= (sizeof(struct in_addr
));
1614 case (AFNUM_NSAP
<<8 | SAFNUM_UNICAST
):
1615 case (AFNUM_NSAP
<<8 | SAFNUM_MULTICAST
):
1616 case (AFNUM_NSAP
<<8 | SAFNUM_UNIMULTICAST
):
1617 TCHECK2(tptr
[0], tlen
);
1618 printf("%s",isonsap_string(tptr
,tlen
));
1623 case (AFNUM_NSAP
<<8 | SAFNUM_VPNUNICAST
):
1624 case (AFNUM_NSAP
<<8 | SAFNUM_VPNMULTICAST
):
1625 case (AFNUM_NSAP
<<8 | SAFNUM_VPNUNIMULTICAST
):
1626 if (tlen
< BGP_VPN_RD_LEN
+1) {
1627 printf("invalid len");
1630 TCHECK2(tptr
[0], tlen
);
1631 printf("RD: %s, %s",
1632 bgp_vpn_rd_print(tptr
),
1633 isonsap_string(tptr
+BGP_VPN_RD_LEN
,tlen
-BGP_VPN_RD_LEN
));
1634 /* rfc986 mapped IPv4 address ? */
1635 if (EXTRACT_32BITS(tptr
+BGP_VPN_RD_LEN
) == 0x47000601)
1636 printf(" = %s", getname(tptr
+BGP_VPN_RD_LEN
+4));
1638 /* rfc1888 mapped IPv6 address ? */
1639 else if (EXTRACT_24BITS(tptr
+BGP_VPN_RD_LEN
) == 0x350000)
1640 printf(" = %s", getname6(tptr
+BGP_VPN_RD_LEN
+3));
1647 TCHECK2(tptr
[0], tlen
);
1648 printf("no AFI %u/SAFI %u decoder",af
,safi
);
1650 print_unknown_data(tptr
,"\n\t ",tlen
);
1658 printf(", nh-length: %u", nhlen
);
1666 printf("\n\t %u SNPA", snpa
);
1667 for (/*nothing*/; snpa
> 0; snpa
--) {
1669 printf("\n\t %d bytes", tptr
[0]);
1670 tptr
+= tptr
[0] + 1;
1673 printf(", no SNPA");
1676 while (len
- (tptr
- pptr
) > 0) {
1677 switch (af
<<8 | safi
) {
1678 case (AFNUM_INET
<<8 | SAFNUM_UNICAST
):
1679 case (AFNUM_INET
<<8 | SAFNUM_MULTICAST
):
1680 case (AFNUM_INET
<<8 | SAFNUM_UNIMULTICAST
):
1681 advance
= decode_prefix4(tptr
, len
, buf
, sizeof(buf
));
1683 printf("\n\t (illegal prefix length)");
1684 else if (advance
== -2)
1686 else if (advance
== -3)
1687 break; /* bytes left, but not enough */
1689 printf("\n\t %s", buf
);
1691 case (AFNUM_INET
<<8 | SAFNUM_LABUNICAST
):
1692 advance
= decode_labeled_prefix4(tptr
, len
, buf
, sizeof(buf
));
1694 printf("\n\t (illegal prefix length)");
1695 else if (advance
== -2)
1697 else if (advance
== -3)
1698 break; /* bytes left, but not enough */
1700 printf("\n\t %s", buf
);
1702 case (AFNUM_INET
<<8 | SAFNUM_VPNUNICAST
):
1703 case (AFNUM_INET
<<8 | SAFNUM_VPNMULTICAST
):
1704 case (AFNUM_INET
<<8 | SAFNUM_VPNUNIMULTICAST
):
1705 advance
= decode_labeled_vpn_prefix4(tptr
, buf
, sizeof(buf
));
1707 printf("\n\t (illegal prefix length)");
1708 else if (advance
== -2)
1711 printf("\n\t %s", buf
);
1713 case (AFNUM_INET
<<8 | SAFNUM_RT_ROUTING_INFO
):
1714 advance
= decode_rt_routing_info(tptr
, buf
, sizeof(buf
));
1716 printf("\n\t (illegal prefix length)");
1717 else if (advance
== -2)
1720 printf("\n\t %s", buf
);
1722 case (AFNUM_INET
<<8 | SAFNUM_MULTICAST_VPN
): /* fall through */
1723 case (AFNUM_INET6
<<8 | SAFNUM_MULTICAST_VPN
):
1724 advance
= decode_multicast_vpn(tptr
, buf
, sizeof(buf
));
1726 printf("\n\t (illegal prefix length)");
1727 else if (advance
== -2)
1730 printf("\n\t %s", buf
);
1733 case (AFNUM_INET
<<8 | SAFNUM_MDT
):
1734 advance
= decode_mdt_vpn_nlri(tptr
, buf
, sizeof(buf
));
1736 printf("\n\t (illegal prefix length)");
1737 else if (advance
== -2)
1740 printf("\n\t %s", buf
);
1743 case (AFNUM_INET6
<<8 | SAFNUM_UNICAST
):
1744 case (AFNUM_INET6
<<8 | SAFNUM_MULTICAST
):
1745 case (AFNUM_INET6
<<8 | SAFNUM_UNIMULTICAST
):
1746 advance
= decode_prefix6(tptr
, len
, buf
, sizeof(buf
));
1748 printf("\n\t (illegal prefix length)");
1749 else if (advance
== -2)
1751 else if (advance
== -3)
1752 break; /* bytes left, but not enough */
1754 printf("\n\t %s", buf
);
1756 case (AFNUM_INET6
<<8 | SAFNUM_LABUNICAST
):
1757 advance
= decode_labeled_prefix6(tptr
, len
, buf
, sizeof(buf
));
1759 printf("\n\t (illegal prefix length)");
1760 else if (advance
== -2)
1762 else if (advance
== -3)
1763 break; /* bytes left, but not enough */
1765 printf("\n\t %s", buf
);
1767 case (AFNUM_INET6
<<8 | SAFNUM_VPNUNICAST
):
1768 case (AFNUM_INET6
<<8 | SAFNUM_VPNMULTICAST
):
1769 case (AFNUM_INET6
<<8 | SAFNUM_VPNUNIMULTICAST
):
1770 advance
= decode_labeled_vpn_prefix6(tptr
, buf
, sizeof(buf
));
1772 printf("\n\t (illegal prefix length)");
1773 else if (advance
== -2)
1776 printf("\n\t %s", buf
);
1779 case (AFNUM_VPLS
<<8 | SAFNUM_VPLS
):
1780 case (AFNUM_L2VPN
<<8 | SAFNUM_VPNUNICAST
):
1781 case (AFNUM_L2VPN
<<8 | SAFNUM_VPNMULTICAST
):
1782 case (AFNUM_L2VPN
<<8 | SAFNUM_VPNUNIMULTICAST
):
1783 advance
= decode_labeled_vpn_l2(tptr
, buf
, sizeof(buf
));
1785 printf("\n\t (illegal length)");
1786 else if (advance
== -2)
1789 printf("\n\t %s", buf
);
1791 case (AFNUM_NSAP
<<8 | SAFNUM_UNICAST
):
1792 case (AFNUM_NSAP
<<8 | SAFNUM_MULTICAST
):
1793 case (AFNUM_NSAP
<<8 | SAFNUM_UNIMULTICAST
):
1794 advance
= decode_clnp_prefix(tptr
, buf
, sizeof(buf
));
1796 printf("\n\t (illegal prefix length)");
1797 else if (advance
== -2)
1800 printf("\n\t %s", buf
);
1802 case (AFNUM_NSAP
<<8 | SAFNUM_VPNUNICAST
):
1803 case (AFNUM_NSAP
<<8 | SAFNUM_VPNMULTICAST
):
1804 case (AFNUM_NSAP
<<8 | SAFNUM_VPNUNIMULTICAST
):
1805 advance
= decode_labeled_vpn_clnp_prefix(tptr
, buf
, sizeof(buf
));
1807 printf("\n\t (illegal prefix length)");
1808 else if (advance
== -2)
1811 printf("\n\t %s", buf
);
1814 TCHECK2(*tptr
,tlen
);
1815 printf("\n\t no AFI %u / SAFI %u decoder",af
,safi
);
1817 print_unknown_data(tptr
,"\n\t ",tlen
);
1829 case BGPTYPE_MP_UNREACH_NLRI
:
1830 TCHECK2(tptr
[0], BGP_MP_NLRI_MINSIZE
);
1831 af
= EXTRACT_16BITS(tptr
);
1834 printf("\n\t AFI: %s (%u), %sSAFI: %s (%u)",
1835 tok2strbuf(af_values
, "Unknown AFI", af
,
1836 tokbuf
, sizeof(tokbuf
)),
1838 (safi
>128) ? "vendor specific " : "", /* 128 is meanwhile wellknown */
1839 tok2strbuf(bgp_safi_values
, "Unknown SAFI", safi
,
1840 tokbuf
, sizeof(tokbuf
)),
1843 if (len
== BGP_MP_NLRI_MINSIZE
)
1844 printf("\n\t End-of-Rib Marker (empty NLRI)");
1848 while (len
- (tptr
- pptr
) > 0) {
1849 switch (af
<<8 | safi
) {
1850 case (AFNUM_INET
<<8 | SAFNUM_UNICAST
):
1851 case (AFNUM_INET
<<8 | SAFNUM_MULTICAST
):
1852 case (AFNUM_INET
<<8 | SAFNUM_UNIMULTICAST
):
1853 advance
= decode_prefix4(tptr
, len
, buf
, sizeof(buf
));
1855 printf("\n\t (illegal prefix length)");
1856 else if (advance
== -2)
1858 else if (advance
== -3)
1859 break; /* bytes left, but not enough */
1861 printf("\n\t %s", buf
);
1863 case (AFNUM_INET
<<8 | SAFNUM_LABUNICAST
):
1864 advance
= decode_labeled_prefix4(tptr
, len
, buf
, sizeof(buf
));
1866 printf("\n\t (illegal prefix length)");
1867 else if (advance
== -2)
1869 else if (advance
== -3)
1870 break; /* bytes left, but not enough */
1872 printf("\n\t %s", buf
);
1874 case (AFNUM_INET
<<8 | SAFNUM_VPNUNICAST
):
1875 case (AFNUM_INET
<<8 | SAFNUM_VPNMULTICAST
):
1876 case (AFNUM_INET
<<8 | SAFNUM_VPNUNIMULTICAST
):
1877 advance
= decode_labeled_vpn_prefix4(tptr
, buf
, sizeof(buf
));
1879 printf("\n\t (illegal prefix length)");
1880 else if (advance
== -2)
1883 printf("\n\t %s", buf
);
1886 case (AFNUM_INET6
<<8 | SAFNUM_UNICAST
):
1887 case (AFNUM_INET6
<<8 | SAFNUM_MULTICAST
):
1888 case (AFNUM_INET6
<<8 | SAFNUM_UNIMULTICAST
):
1889 advance
= decode_prefix6(tptr
, len
, buf
, sizeof(buf
));
1891 printf("\n\t (illegal prefix length)");
1892 else if (advance
== -2)
1894 else if (advance
== -3)
1895 break; /* bytes left, but not enough */
1897 printf("\n\t %s", buf
);
1899 case (AFNUM_INET6
<<8 | SAFNUM_LABUNICAST
):
1900 advance
= decode_labeled_prefix6(tptr
, len
, buf
, sizeof(buf
));
1902 printf("\n\t (illegal prefix length)");
1903 else if (advance
== -2)
1905 else if (advance
== -3)
1906 break; /* bytes left, but not enough */
1908 printf("\n\t %s", buf
);
1910 case (AFNUM_INET6
<<8 | SAFNUM_VPNUNICAST
):
1911 case (AFNUM_INET6
<<8 | SAFNUM_VPNMULTICAST
):
1912 case (AFNUM_INET6
<<8 | SAFNUM_VPNUNIMULTICAST
):
1913 advance
= decode_labeled_vpn_prefix6(tptr
, buf
, sizeof(buf
));
1915 printf("\n\t (illegal prefix length)");
1916 else if (advance
== -2)
1919 printf("\n\t %s", buf
);
1922 case (AFNUM_VPLS
<<8 | SAFNUM_VPLS
):
1923 case (AFNUM_L2VPN
<<8 | SAFNUM_VPNUNICAST
):
1924 case (AFNUM_L2VPN
<<8 | SAFNUM_VPNMULTICAST
):
1925 case (AFNUM_L2VPN
<<8 | SAFNUM_VPNUNIMULTICAST
):
1926 advance
= decode_labeled_vpn_l2(tptr
, buf
, sizeof(buf
));
1928 printf("\n\t (illegal length)");
1929 else if (advance
== -2)
1932 printf("\n\t %s", buf
);
1934 case (AFNUM_NSAP
<<8 | SAFNUM_UNICAST
):
1935 case (AFNUM_NSAP
<<8 | SAFNUM_MULTICAST
):
1936 case (AFNUM_NSAP
<<8 | SAFNUM_UNIMULTICAST
):
1937 advance
= decode_clnp_prefix(tptr
, buf
, sizeof(buf
));
1939 printf("\n\t (illegal prefix length)");
1940 else if (advance
== -2)
1943 printf("\n\t %s", buf
);
1945 case (AFNUM_NSAP
<<8 | SAFNUM_VPNUNICAST
):
1946 case (AFNUM_NSAP
<<8 | SAFNUM_VPNMULTICAST
):
1947 case (AFNUM_NSAP
<<8 | SAFNUM_VPNUNIMULTICAST
):
1948 advance
= decode_labeled_vpn_clnp_prefix(tptr
, buf
, sizeof(buf
));
1950 printf("\n\t (illegal prefix length)");
1951 else if (advance
== -2)
1954 printf("\n\t %s", buf
);
1956 case (AFNUM_INET
<<8 | SAFNUM_MDT
):
1957 advance
= decode_mdt_vpn_nlri(tptr
, buf
, sizeof(buf
));
1959 printf("\n\t (illegal prefix length)");
1960 else if (advance
== -2)
1963 printf("\n\t %s", buf
);
1965 case (AFNUM_INET
<<8 | SAFNUM_MULTICAST_VPN
): /* fall through */
1966 case (AFNUM_INET6
<<8 | SAFNUM_MULTICAST_VPN
):
1967 advance
= decode_multicast_vpn(tptr
, buf
, sizeof(buf
));
1969 printf("\n\t (illegal prefix length)");
1970 else if (advance
== -2)
1973 printf("\n\t %s", buf
);
1976 TCHECK2(*(tptr
-3),tlen
);
1977 printf("no AFI %u / SAFI %u decoder",af
,safi
);
1979 print_unknown_data(tptr
-3,"\n\t ",tlen
);
1989 case BGPTYPE_EXTD_COMMUNITIES
:
1991 printf("invalid len");
1995 u_int16_t extd_comm
;
1997 TCHECK2(tptr
[0], 2);
1998 extd_comm
=EXTRACT_16BITS(tptr
);
2000 printf("\n\t %s (0x%04x), Flags [%s]",
2001 tok2strbuf(bgp_extd_comm_subtype_values
,
2002 "unknown extd community typecode",
2003 extd_comm
, tokbuf
, sizeof(tokbuf
)),
2005 bittok2str(bgp_extd_comm_flag_values
, "none", extd_comm
));
2007 TCHECK2(*(tptr
+2), 6);
2009 case BGP_EXT_COM_RT_0
:
2010 case BGP_EXT_COM_RO_0
:
2011 case BGP_EXT_COM_L2VPN_RT_0
:
2012 printf(": %u:%u (= %s)",
2013 EXTRACT_16BITS(tptr
+2),
2014 EXTRACT_32BITS(tptr
+4),
2017 case BGP_EXT_COM_RT_1
:
2018 case BGP_EXT_COM_RO_1
:
2019 case BGP_EXT_COM_L2VPN_RT_1
:
2020 case BGP_EXT_COM_VRF_RT_IMP
:
2023 EXTRACT_16BITS(tptr
+6));
2025 case BGP_EXT_COM_RT_2
:
2026 case BGP_EXT_COM_RO_2
:
2028 as_printf(astostr
, sizeof(astostr
),
2029 EXTRACT_32BITS(tptr
+2)), EXTRACT_16BITS(tptr
+6));
2031 case BGP_EXT_COM_LINKBAND
:
2032 bw
.i
= EXTRACT_32BITS(tptr
+2);
2033 printf(": bandwidth: %.3f Mbps",
2036 case BGP_EXT_COM_VPN_ORIGIN
:
2037 case BGP_EXT_COM_VPN_ORIGIN2
:
2038 case BGP_EXT_COM_VPN_ORIGIN3
:
2039 case BGP_EXT_COM_VPN_ORIGIN4
:
2040 case BGP_EXT_COM_OSPF_RID
:
2041 case BGP_EXT_COM_OSPF_RID2
:
2042 printf("%s", getname(tptr
+2));
2044 case BGP_EXT_COM_OSPF_RTYPE
:
2045 case BGP_EXT_COM_OSPF_RTYPE2
:
2046 printf(": area:%s, router-type:%s, metric-type:%s%s",
2048 tok2strbuf(bgp_extd_comm_ospf_rtype_values
,
2051 tokbuf
, sizeof(tokbuf
)),
2052 (*(tptr
+7) & BGP_OSPF_RTYPE_METRIC_TYPE
) ? "E2" : "",
2053 ((*(tptr
+6) == BGP_OSPF_RTYPE_EXT
) || (*(tptr
+6) == BGP_OSPF_RTYPE_NSSA
)) ? "E1" : "");
2055 case BGP_EXT_COM_L2INFO
:
2056 printf(": %s Control Flags [0x%02x]:MTU %u",
2057 tok2strbuf(l2vpn_encaps_values
,
2060 tokbuf
, sizeof(tokbuf
)),
2062 EXTRACT_16BITS(tptr
+4));
2064 case BGP_EXT_COM_SOURCE_AS
:
2065 printf(": AS %u", EXTRACT_16BITS(tptr
+2));
2069 print_unknown_data(tptr
,"\n\t ",8);
2077 case BGPTYPE_PMSI_TUNNEL
:
2079 u_int8_t tunnel_type
, flags
;
2081 tunnel_type
= *(tptr
+1);
2085 TCHECK2(tptr
[0], 5);
2086 printf("\n\t Tunnel-type %s (%u), Flags [%s], MPLS Label %u",
2087 tok2str(bgp_pmsi_tunnel_values
, "Unknown", tunnel_type
),
2089 bittok2str(bgp_pmsi_flag_values
, "none", flags
),
2090 EXTRACT_24BITS(tptr
+2)>>4);
2095 switch (tunnel_type
) {
2096 case BGP_PMSI_TUNNEL_PIM_SM
: /* fall through */
2097 case BGP_PMSI_TUNNEL_PIM_BIDIR
:
2098 TCHECK2(tptr
[0], 8);
2099 printf("\n\t Sender %s, P-Group %s",
2100 ipaddr_string(tptr
),
2101 ipaddr_string(tptr
+4));
2104 case BGP_PMSI_TUNNEL_PIM_SSM
:
2105 TCHECK2(tptr
[0], 8);
2106 printf("\n\t Root-Node %s, P-Group %s",
2107 ipaddr_string(tptr
),
2108 ipaddr_string(tptr
+4));
2110 case BGP_PMSI_TUNNEL_INGRESS
:
2111 TCHECK2(tptr
[0], 4);
2112 printf("\n\t Tunnel-Endpoint %s",
2113 ipaddr_string(tptr
));
2115 case BGP_PMSI_TUNNEL_LDP_P2MP
: /* fall through */
2116 case BGP_PMSI_TUNNEL_LDP_MP2MP
:
2117 TCHECK2(tptr
[0], 8);
2118 printf("\n\t Root-Node %s, LSP-ID 0x%08x",
2119 ipaddr_string(tptr
),
2120 EXTRACT_32BITS(tptr
+4));
2122 case BGP_PMSI_TUNNEL_RSVP_P2MP
:
2123 TCHECK2(tptr
[0], 8);
2124 printf("\n\t Extended-Tunnel-ID %s, P2MP-ID 0x%08x",
2125 ipaddr_string(tptr
),
2126 EXTRACT_32BITS(tptr
+4));
2130 print_unknown_data(tptr
,"\n\t ",tlen
);
2135 case BGPTYPE_ATTR_SET
:
2136 TCHECK2(tptr
[0], 4);
2139 printf("\n\t Origin AS: %s",
2140 as_printf(astostr
, sizeof(astostr
), EXTRACT_32BITS(tptr
)));
2145 u_int aflags
, atype
, alenlen
, alen
;
2147 TCHECK2(tptr
[0], 2);
2151 atype
= *(tptr
+ 1);
2154 alenlen
= bgp_attr_lenlen(aflags
, tptr
);
2155 TCHECK2(tptr
[0], alenlen
);
2158 alen
= bgp_attr_len(aflags
, tptr
);
2162 printf("\n\t %s (%u), length: %u",
2163 tok2strbuf(bgp_attr_values
,
2164 "Unknown Attribute", atype
,
2165 tokbuf
, sizeof(tokbuf
)),
2170 printf(", Flags [%s%s%s%s",
2171 aflags
& 0x80 ? "O" : "",
2172 aflags
& 0x40 ? "T" : "",
2173 aflags
& 0x20 ? "P" : "",
2174 aflags
& 0x10 ? "E" : "");
2176 printf("+%x", aflags
& 0xf);
2179 /* FIXME check for recursion */
2180 if (!bgp_attr_print(atype
, tptr
, alen
))
2190 printf("\n\t no Attribute %u decoder",atype
); /* we have no decoder for the attribute */
2192 print_unknown_data(pptr
,"\n\t ",len
);
2195 if (vflag
> 1 && len
) { /* omit zero length attributes*/
2197 print_unknown_data(pptr
,"\n\t ",len
);
2206 bgp_capabilities_print(const u_char
*opt
, int caps_len
)
2208 char tokbuf
[TOKBUFSIZE
];
2209 char tokbuf2
[TOKBUFSIZE
];
2210 int cap_type
, cap_len
, tcap_len
, cap_offset
;
2213 while (i
< caps_len
) {
2214 TCHECK2(opt
[i
], BGP_CAP_HEADER_SIZE
);
2218 printf("\n\t %s (%u), length: %u",
2219 tok2strbuf(bgp_capcode_values
, "Unknown",
2220 cap_type
, tokbuf
, sizeof(tokbuf
)),
2223 TCHECK2(opt
[i
+2], cap_len
);
2225 case BGP_CAPCODE_MP
:
2226 printf("\n\t\tAFI %s (%u), SAFI %s (%u)",
2227 tok2strbuf(af_values
, "Unknown",
2228 EXTRACT_16BITS(opt
+i
+2),
2229 tokbuf
, sizeof(tokbuf
)),
2230 EXTRACT_16BITS(opt
+i
+2),
2231 tok2strbuf(bgp_safi_values
, "Unknown",
2233 tokbuf
, sizeof(tokbuf
)),
2236 case BGP_CAPCODE_RESTART
:
2237 printf("\n\t\tRestart Flags: [%s], Restart Time %us",
2238 ((opt
[i
+2])&0x80) ? "R" : "none",
2239 EXTRACT_16BITS(opt
+i
+2)&0xfff);
2242 while(tcap_len
>=4) {
2243 printf("\n\t\t AFI %s (%u), SAFI %s (%u), Forwarding state preserved: %s",
2244 tok2strbuf(af_values
,"Unknown",
2245 EXTRACT_16BITS(opt
+i
+cap_offset
),
2246 tokbuf
, sizeof(tokbuf
)),
2247 EXTRACT_16BITS(opt
+i
+cap_offset
),
2248 tok2strbuf(bgp_safi_values
,"Unknown",
2249 opt
[i
+cap_offset
+2],
2250 tokbuf2
, sizeof(tokbuf2
)),
2251 opt
[i
+cap_offset
+2],
2252 ((opt
[i
+cap_offset
+3])&0x80) ? "yes" : "no" );
2257 case BGP_CAPCODE_RR
:
2258 case BGP_CAPCODE_RR_CISCO
:
2260 case BGP_CAPCODE_AS_NEW
:
2263 * Extract the 4 byte AS number encoded.
2266 printf("\n\t\t 4 Byte AS %s",
2267 as_printf(astostr
, sizeof(astostr
),
2268 EXTRACT_32BITS(opt
+ i
+ 2)));
2272 printf("\n\t\tno decoder for Capability %u",
2275 print_unknown_data(&opt
[i
+2],"\n\t\t",cap_len
);
2278 if (vflag
> 1 && cap_len
> 0) {
2279 print_unknown_data(&opt
[i
+2],"\n\t\t",cap_len
);
2281 i
+= BGP_CAP_HEADER_SIZE
+ cap_len
;
2290 bgp_open_print(const u_char
*dat
, int length
)
2292 struct bgp_open bgpo
;
2293 struct bgp_opt bgpopt
;
2296 char tokbuf
[TOKBUFSIZE
];
2298 TCHECK2(dat
[0], BGP_OPEN_SIZE
);
2299 memcpy(&bgpo
, dat
, BGP_OPEN_SIZE
);
2301 printf("\n\t Version %d, ", bgpo
.bgpo_version
);
2302 printf("my AS %s, ",
2303 as_printf(astostr
, sizeof(astostr
), ntohs(bgpo
.bgpo_myas
)));
2304 printf("Holdtime %us, ", ntohs(bgpo
.bgpo_holdtime
));
2305 printf("ID %s", getname((u_char
*)&bgpo
.bgpo_id
));
2306 printf("\n\t Optional parameters, length: %u", bgpo
.bgpo_optlen
);
2308 /* some little sanity checking */
2309 if (length
< bgpo
.bgpo_optlen
+BGP_OPEN_SIZE
)
2313 opt
= &((const struct bgp_open
*)dat
)->bgpo_optlen
;
2317 while (i
< bgpo
.bgpo_optlen
) {
2318 TCHECK2(opt
[i
], BGP_OPT_SIZE
);
2319 memcpy(&bgpopt
, &opt
[i
], BGP_OPT_SIZE
);
2320 if (i
+ 2 + bgpopt
.bgpopt_len
> bgpo
.bgpo_optlen
) {
2321 printf("\n\t Option %d, length: %u", bgpopt
.bgpopt_type
, bgpopt
.bgpopt_len
);
2325 printf("\n\t Option %s (%u), length: %u",
2326 tok2strbuf(bgp_opt_values
,"Unknown",
2328 tokbuf
, sizeof(tokbuf
)),
2332 /* now let's decode the options we know*/
2333 switch(bgpopt
.bgpopt_type
) {
2336 bgp_capabilities_print(&opt
[i
+BGP_OPT_SIZE
],
2342 printf("\n\t no decoder for option %u",
2343 bgpopt
.bgpopt_type
);
2346 i
+= BGP_OPT_SIZE
+ bgpopt
.bgpopt_len
;
2354 bgp_update_print(const u_char
*dat
, int length
)
2358 int withdrawn_routes_len
;
2361 char tokbuf
[TOKBUFSIZE
];
2363 char buf
[MAXHOSTNAMELEN
+ 100];
2367 TCHECK2(dat
[0], BGP_SIZE
);
2368 if (length
< BGP_SIZE
)
2370 memcpy(&bgp
, dat
, BGP_SIZE
);
2371 p
= dat
+ BGP_SIZE
; /*XXX*/
2374 /* Unfeasible routes */
2378 withdrawn_routes_len
= EXTRACT_16BITS(p
);
2381 if (withdrawn_routes_len
) {
2383 * Without keeping state from the original NLRI message,
2384 * it's not possible to tell if this a v4 or v6 route,
2385 * so only try to decode it if we're not v6 enabled.
2387 TCHECK2(p
[0], withdrawn_routes_len
);
2388 if (length
< withdrawn_routes_len
)
2391 printf("\n\t Withdrawn routes: %d bytes", withdrawn_routes_len
);
2392 p
+= withdrawn_routes_len
;
2393 length
-= withdrawn_routes_len
;
2395 if (withdrawn_routes_len
< 2)
2398 withdrawn_routes_len
-= 2;
2401 printf("\n\t Withdrawn routes:");
2403 while(withdrawn_routes_len
> 0) {
2404 wpfx
= decode_prefix4(p
, withdrawn_routes_len
, buf
, sizeof(buf
));
2406 printf("\n\t (illegal prefix length)");
2408 } else if (wpfx
== -2)
2410 else if (wpfx
== -3)
2411 goto trunc
; /* bytes left, but not enough */
2413 printf("\n\t %s", buf
);
2416 withdrawn_routes_len
-= wpfx
;
2425 len
= EXTRACT_16BITS(p
);
2429 if (withdrawn_routes_len
== 0 && len
== 0 && length
== 0) {
2430 /* No withdrawn routes, no path attributes, no NLRI */
2431 printf("\n\t End-of-Rib Marker (empty NLRI)");
2436 /* do something more useful!*/
2438 int aflags
, atype
, alenlen
, alen
;
2450 alenlen
= bgp_attr_lenlen(aflags
, p
);
2451 TCHECK2(p
[0], alenlen
);
2454 if (length
< alenlen
)
2456 alen
= bgp_attr_len(aflags
, p
);
2461 printf("\n\t %s (%u), length: %u",
2462 tok2strbuf(bgp_attr_values
, "Unknown Attribute",
2464 tokbuf
, sizeof(tokbuf
)),
2469 printf(", Flags [%s%s%s%s",
2470 aflags
& 0x80 ? "O" : "",
2471 aflags
& 0x40 ? "T" : "",
2472 aflags
& 0x20 ? "P" : "",
2473 aflags
& 0x10 ? "E" : "");
2475 printf("+%x", aflags
& 0xf);
2482 if (!bgp_attr_print(atype
, p
, alen
))
2492 * XXX - what if they're using the "Advertisement of
2493 * Multiple Paths in BGP" feature:
2495 * https://datatracker.ietf.org/doc/draft-ietf-idr-add-paths/
2497 * http://tools.ietf.org/html/draft-ietf-idr-add-paths-06
2499 printf("\n\t Updated routes:");
2501 char buf
[MAXHOSTNAMELEN
+ 100];
2502 i
= decode_prefix4(p
, length
, buf
, sizeof(buf
));
2504 printf("\n\t (illegal prefix length)");
2509 goto trunc
; /* bytes left, but not enough */
2511 printf("\n\t %s", buf
);
2523 bgp_notification_print(const u_char
*dat
, int length
)
2525 struct bgp_notification bgpn
;
2527 char tokbuf
[TOKBUFSIZE
];
2528 char tokbuf2
[TOKBUFSIZE
];
2530 TCHECK2(dat
[0], BGP_NOTIFICATION_SIZE
);
2531 memcpy(&bgpn
, dat
, BGP_NOTIFICATION_SIZE
);
2533 /* some little sanity checking */
2534 if (length
<BGP_NOTIFICATION_SIZE
)
2538 tok2strbuf(bgp_notify_major_values
, "Unknown Error",
2539 bgpn
.bgpn_major
, tokbuf
, sizeof(tokbuf
)),
2542 switch (bgpn
.bgpn_major
) {
2544 case BGP_NOTIFY_MAJOR_MSG
:
2545 printf(", subcode %s (%u)",
2546 tok2strbuf(bgp_notify_minor_msg_values
, "Unknown",
2547 bgpn
.bgpn_minor
, tokbuf
, sizeof(tokbuf
)),
2550 case BGP_NOTIFY_MAJOR_OPEN
:
2551 printf(", subcode %s (%u)",
2552 tok2strbuf(bgp_notify_minor_open_values
, "Unknown",
2553 bgpn
.bgpn_minor
, tokbuf
, sizeof(tokbuf
)),
2556 case BGP_NOTIFY_MAJOR_UPDATE
:
2557 printf(", subcode %s (%u)",
2558 tok2strbuf(bgp_notify_minor_update_values
, "Unknown",
2559 bgpn
.bgpn_minor
, tokbuf
, sizeof(tokbuf
)),
2562 case BGP_NOTIFY_MAJOR_CAP
:
2563 printf(" subcode %s (%u)",
2564 tok2strbuf(bgp_notify_minor_cap_values
, "Unknown",
2565 bgpn
.bgpn_minor
, tokbuf
, sizeof(tokbuf
)),
2567 case BGP_NOTIFY_MAJOR_CEASE
:
2568 printf(", subcode %s (%u)",
2569 tok2strbuf(bgp_notify_minor_cease_values
, "Unknown",
2570 bgpn
.bgpn_minor
, tokbuf
, sizeof(tokbuf
)),
2573 /* draft-ietf-idr-cease-subcode-02 mentions optionally 7 bytes
2574 * for the maxprefix subtype, which may contain AFI, SAFI and MAXPREFIXES
2576 if(bgpn
.bgpn_minor
== BGP_NOTIFY_MINOR_CEASE_MAXPRFX
&& length
>= BGP_NOTIFICATION_SIZE
+ 7) {
2577 tptr
= dat
+ BGP_NOTIFICATION_SIZE
;
2579 printf(", AFI %s (%u), SAFI %s (%u), Max Prefixes: %u",
2580 tok2strbuf(af_values
, "Unknown",
2581 EXTRACT_16BITS(tptr
), tokbuf
, sizeof(tokbuf
)),
2582 EXTRACT_16BITS(tptr
),
2583 tok2strbuf(bgp_safi_values
, "Unknown", *(tptr
+2),
2584 tokbuf2
, sizeof(tokbuf
)),
2586 EXTRACT_32BITS(tptr
+3));
2599 bgp_route_refresh_print(const u_char
*pptr
, int len
) {
2601 const struct bgp_route_refresh
*bgp_route_refresh_header
;
2602 char tokbuf
[TOKBUFSIZE
];
2603 char tokbuf2
[TOKBUFSIZE
];
2605 TCHECK2(pptr
[0], BGP_ROUTE_REFRESH_SIZE
);
2607 /* some little sanity checking */
2608 if (len
<BGP_ROUTE_REFRESH_SIZE
)
2611 bgp_route_refresh_header
= (const struct bgp_route_refresh
*)pptr
;
2613 printf("\n\t AFI %s (%u), SAFI %s (%u)",
2614 tok2strbuf(af_values
,"Unknown",
2615 /* this stinks but the compiler pads the structure
2617 EXTRACT_16BITS(&bgp_route_refresh_header
->afi
),
2618 tokbuf
, sizeof(tokbuf
)),
2619 EXTRACT_16BITS(&bgp_route_refresh_header
->afi
),
2620 tok2strbuf(bgp_safi_values
,"Unknown",
2621 bgp_route_refresh_header
->safi
,
2622 tokbuf2
, sizeof(tokbuf2
)),
2623 bgp_route_refresh_header
->safi
);
2626 TCHECK2(*pptr
, len
);
2627 print_unknown_data(pptr
,"\n\t ", len
);
2636 bgp_header_print(const u_char
*dat
, int length
)
2639 char tokbuf
[TOKBUFSIZE
];
2641 TCHECK2(dat
[0], BGP_SIZE
);
2642 memcpy(&bgp
, dat
, BGP_SIZE
);
2643 printf("\n\t%s Message (%u), length: %u",
2644 tok2strbuf(bgp_msg_values
, "Unknown", bgp
.bgp_type
,
2645 tokbuf
, sizeof(tokbuf
)),
2649 switch (bgp
.bgp_type
) {
2651 bgp_open_print(dat
, length
);
2654 bgp_update_print(dat
, length
);
2656 case BGP_NOTIFICATION
:
2657 bgp_notification_print(dat
, length
);
2661 case BGP_ROUTE_REFRESH
:
2662 bgp_route_refresh_print(dat
, length
);
2665 /* we have no decoder for the BGP message */
2666 TCHECK2(*dat
, length
);
2667 printf("\n\t no Message %u decoder",bgp
.bgp_type
);
2668 print_unknown_data(dat
,"\n\t ",length
);
2678 bgp_print(const u_char
*dat
, int length
)
2682 const u_char
*start
;
2683 const u_char marker
[] = {
2684 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
2685 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
2689 char tokbuf
[TOKBUFSIZE
];
2692 if (snapend
< dat
+ length
)
2695 printf(": BGP, length: %u",length
);
2697 if (vflag
< 1) /* lets be less chatty */
2703 if (!TTEST2(p
[0], 1))
2710 if (!TTEST2(p
[0], sizeof(marker
)))
2712 if (memcmp(p
, marker
, sizeof(marker
)) != 0) {
2717 /* found BGP header */
2718 TCHECK2(p
[0], BGP_SIZE
); /*XXX*/
2719 memcpy(&bgp
, p
, BGP_SIZE
);
2724 hlen
= ntohs(bgp
.bgp_len
);
2725 if (hlen
< BGP_SIZE
) {
2726 printf("\n[|BGP Bogus header length %u < %u]", hlen
,
2731 if (TTEST2(p
[0], hlen
)) {
2732 if (!bgp_header_print(p
, hlen
))
2737 printf("\n[|BGP %s]",
2738 tok2strbuf(bgp_msg_values
,
2739 "Unknown Message Type",
2741 tokbuf
, sizeof(tokbuf
)));
2754 * c-style: whitesmith