1 /* 2 * Copyright (C) 1999 WIDE Project. 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 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. 16 * 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 27 * SUCH DAMAGE. 28 * 29 * Extensively modified by Hannes Gredler (hannes@juniper.net) for more 30 * complete BGP support. 31 */ 32 33 #ifdef HAVE_CONFIG_H 34 #include "config.h" 35 #endif 36 37 #include <sys/cdefs.h> 38 #ifndef lint 39 #if 0 40 static const char rcsid[] _U_ = 41 "@(#) Header: /tcpdump/master/tcpdump/print-bgp.c,v 1.118 2007-12-07 15:54:52 hannes Exp "; 42 #else 43 __RCSID("$NetBSD: print-bgp.c,v 1.3 2013/04/06 19:33:08 christos Exp $"); 44 #endif 45 #endif 46 47 #include <tcpdump-stdinc.h> 48 49 #include <stdio.h> 50 #include <string.h> 51 52 #include "interface.h" 53 #include "decode_prefix.h" 54 #include "addrtoname.h" 55 #include "extract.h" 56 #include "bgp.h" 57 #include "af.h" 58 #include "l2vpn.h" 59 60 struct bgp { 61 u_int8_t bgp_marker[16]; 62 u_int16_t bgp_len; 63 u_int8_t bgp_type; 64 }; 65 #define BGP_SIZE 19 /* unaligned */ 66 67 #define BGP_OPEN 1 68 #define BGP_UPDATE 2 69 #define BGP_NOTIFICATION 3 70 #define BGP_KEEPALIVE 4 71 #define BGP_ROUTE_REFRESH 5 72 73 static struct tok bgp_msg_values[] = { 74 { BGP_OPEN, "Open"}, 75 { BGP_UPDATE, "Update"}, 76 { BGP_NOTIFICATION, "Notification"}, 77 { BGP_KEEPALIVE, "Keepalive"}, 78 { BGP_ROUTE_REFRESH, "Route Refresh"}, 79 { 0, NULL} 80 }; 81 82 struct bgp_open { 83 u_int8_t bgpo_marker[16]; 84 u_int16_t bgpo_len; 85 u_int8_t bgpo_type; 86 u_int8_t bgpo_version; 87 u_int16_t bgpo_myas; 88 u_int16_t bgpo_holdtime; 89 u_int32_t bgpo_id; 90 u_int8_t bgpo_optlen; 91 /* options should follow */ 92 }; 93 #define BGP_OPEN_SIZE 29 /* unaligned */ 94 95 struct bgp_opt { 96 u_int8_t bgpopt_type; 97 u_int8_t bgpopt_len; 98 /* variable length */ 99 }; 100 #define BGP_OPT_SIZE 2 /* some compilers may pad to 4 bytes */ 101 #define BGP_CAP_HEADER_SIZE 2 /* some compilers may pad to 4 bytes */ 102 103 struct bgp_notification { 104 u_int8_t bgpn_marker[16]; 105 u_int16_t bgpn_len; 106 u_int8_t bgpn_type; 107 u_int8_t bgpn_major; 108 u_int8_t bgpn_minor; 109 }; 110 #define BGP_NOTIFICATION_SIZE 21 /* unaligned */ 111 112 struct bgp_route_refresh { 113 u_int8_t bgp_marker[16]; 114 u_int16_t len; 115 u_int8_t type; 116 u_int8_t afi[2]; /* the compiler messes this structure up */ 117 u_int8_t res; /* when doing misaligned sequences of int8 and int16 */ 118 u_int8_t safi; /* afi should be int16 - so we have to access it using */ 119 }; /* EXTRACT_16BITS(&bgp_route_refresh->afi) (sigh) */ 120 #define BGP_ROUTE_REFRESH_SIZE 23 121 122 #define bgp_attr_lenlen(flags, p) \ 123 (((flags) & 0x10) ? 2 : 1) 124 #define bgp_attr_len(flags, p) \ 125 (((flags) & 0x10) ? EXTRACT_16BITS(p) : *(p)) 126 127 #define BGPTYPE_ORIGIN 1 128 #define BGPTYPE_AS_PATH 2 129 #define BGPTYPE_NEXT_HOP 3 130 #define BGPTYPE_MULTI_EXIT_DISC 4 131 #define BGPTYPE_LOCAL_PREF 5 132 #define BGPTYPE_ATOMIC_AGGREGATE 6 133 #define BGPTYPE_AGGREGATOR 7 134 #define BGPTYPE_COMMUNITIES 8 /* RFC1997 */ 135 #define BGPTYPE_ORIGINATOR_ID 9 /* RFC1998 */ 136 #define BGPTYPE_CLUSTER_LIST 10 /* RFC1998 */ 137 #define BGPTYPE_DPA 11 /* draft-ietf-idr-bgp-dpa */ 138 #define BGPTYPE_ADVERTISERS 12 /* RFC1863 */ 139 #define BGPTYPE_RCID_PATH 13 /* RFC1863 */ 140 #define BGPTYPE_MP_REACH_NLRI 14 /* RFC2283 */ 141 #define BGPTYPE_MP_UNREACH_NLRI 15 /* RFC2283 */ 142 #define BGPTYPE_EXTD_COMMUNITIES 16 /* draft-ietf-idr-bgp-ext-communities */ 143 #define BGPTYPE_AS4_PATH 17 /* RFC4893 */ 144 #define BGPTYPE_AGGREGATOR4 18 /* RFC4893 */ 145 #define BGPTYPE_PMSI_TUNNEL 22 /* draft-ietf-l3vpn-2547bis-mcast-bgp-02.txt */ 146 #define BGPTYPE_ATTR_SET 128 /* draft-marques-ppvpn-ibgp */ 147 148 #define BGP_MP_NLRI_MINSIZE 3 /* End of RIB Marker detection */ 149 150 static struct tok bgp_attr_values[] = { 151 { BGPTYPE_ORIGIN, "Origin"}, 152 { BGPTYPE_AS_PATH, "AS Path"}, 153 { BGPTYPE_AS4_PATH, "AS4 Path"}, 154 { BGPTYPE_NEXT_HOP, "Next Hop"}, 155 { BGPTYPE_MULTI_EXIT_DISC, "Multi Exit Discriminator"}, 156 { BGPTYPE_LOCAL_PREF, "Local Preference"}, 157 { BGPTYPE_ATOMIC_AGGREGATE, "Atomic Aggregate"}, 158 { BGPTYPE_AGGREGATOR, "Aggregator"}, 159 { BGPTYPE_AGGREGATOR4, "Aggregator4"}, 160 { BGPTYPE_COMMUNITIES, "Community"}, 161 { BGPTYPE_ORIGINATOR_ID, "Originator ID"}, 162 { BGPTYPE_CLUSTER_LIST, "Cluster List"}, 163 { BGPTYPE_DPA, "DPA"}, 164 { BGPTYPE_ADVERTISERS, "Advertisers"}, 165 { BGPTYPE_RCID_PATH, "RCID Path / Cluster ID"}, 166 { BGPTYPE_MP_REACH_NLRI, "Multi-Protocol Reach NLRI"}, 167 { BGPTYPE_MP_UNREACH_NLRI, "Multi-Protocol Unreach NLRI"}, 168 { BGPTYPE_EXTD_COMMUNITIES, "Extended Community"}, 169 { BGPTYPE_PMSI_TUNNEL, "PMSI Tunnel"}, 170 { BGPTYPE_ATTR_SET, "Attribute Set"}, 171 { 255, "Reserved for development"}, 172 { 0, NULL} 173 }; 174 175 #define BGP_AS_SET 1 176 #define BGP_AS_SEQUENCE 2 177 #define BGP_CONFED_AS_SEQUENCE 3 /* draft-ietf-idr-rfc3065bis-01 */ 178 #define BGP_CONFED_AS_SET 4 /* draft-ietf-idr-rfc3065bis-01 */ 179 180 #define BGP_AS_SEG_TYPE_MIN BGP_AS_SET 181 #define BGP_AS_SEG_TYPE_MAX BGP_CONFED_AS_SET 182 183 static struct tok bgp_as_path_segment_open_values[] = { 184 { BGP_AS_SEQUENCE, ""}, 185 { BGP_AS_SET, "{ "}, 186 { BGP_CONFED_AS_SEQUENCE, "( "}, 187 { BGP_CONFED_AS_SET, "({ "}, 188 { 0, NULL} 189 }; 190 191 static struct tok bgp_as_path_segment_close_values[] = { 192 { BGP_AS_SEQUENCE, ""}, 193 { BGP_AS_SET, "}"}, 194 { BGP_CONFED_AS_SEQUENCE, ")"}, 195 { BGP_CONFED_AS_SET, "})"}, 196 { 0, NULL} 197 }; 198 199 #define BGP_OPT_AUTH 1 200 #define BGP_OPT_CAP 2 201 202 203 static struct tok bgp_opt_values[] = { 204 { BGP_OPT_AUTH, "Authentication Information"}, 205 { BGP_OPT_CAP, "Capabilities Advertisement"}, 206 { 0, NULL} 207 }; 208 209 #define BGP_CAPCODE_MP 1 210 #define BGP_CAPCODE_RR 2 211 #define BGP_CAPCODE_ORF 3 /* XXX */ 212 #define BGP_CAPCODE_RESTART 64 /* draft-ietf-idr-restart-05 */ 213 #define BGP_CAPCODE_AS_NEW 65 /* XXX */ 214 #define BGP_CAPCODE_DYN_CAP 67 /* XXX */ 215 #define BGP_CAPCODE_RR_CISCO 128 216 217 static struct tok bgp_capcode_values[] = { 218 { BGP_CAPCODE_MP, "Multiprotocol Extensions"}, 219 { BGP_CAPCODE_RR, "Route Refresh"}, 220 { BGP_CAPCODE_ORF, "Cooperative Route Filtering"}, 221 { BGP_CAPCODE_RESTART, "Graceful Restart"}, 222 { BGP_CAPCODE_AS_NEW, "32-Bit AS Number"}, 223 { BGP_CAPCODE_DYN_CAP, "Dynamic Capability"}, 224 { BGP_CAPCODE_RR_CISCO, "Route Refresh (Cisco)"}, 225 { 0, NULL} 226 }; 227 228 #define BGP_NOTIFY_MAJOR_MSG 1 229 #define BGP_NOTIFY_MAJOR_OPEN 2 230 #define BGP_NOTIFY_MAJOR_UPDATE 3 231 #define BGP_NOTIFY_MAJOR_HOLDTIME 4 232 #define BGP_NOTIFY_MAJOR_FSM 5 233 #define BGP_NOTIFY_MAJOR_CEASE 6 234 #define BGP_NOTIFY_MAJOR_CAP 7 235 236 static struct tok bgp_notify_major_values[] = { 237 { BGP_NOTIFY_MAJOR_MSG, "Message Header Error"}, 238 { BGP_NOTIFY_MAJOR_OPEN, "OPEN Message Error"}, 239 { BGP_NOTIFY_MAJOR_UPDATE, "UPDATE Message Error"}, 240 { BGP_NOTIFY_MAJOR_HOLDTIME,"Hold Timer Expired"}, 241 { BGP_NOTIFY_MAJOR_FSM, "Finite State Machine Error"}, 242 { BGP_NOTIFY_MAJOR_CEASE, "Cease"}, 243 { BGP_NOTIFY_MAJOR_CAP, "Capability Message Error"}, 244 { 0, NULL} 245 }; 246 247 /* draft-ietf-idr-cease-subcode-02 */ 248 #define BGP_NOTIFY_MINOR_CEASE_MAXPRFX 1 249 static struct tok bgp_notify_minor_cease_values[] = { 250 { BGP_NOTIFY_MINOR_CEASE_MAXPRFX, "Maximum Number of Prefixes Reached"}, 251 { 2, "Administratively Shutdown"}, 252 { 3, "Peer Unconfigured"}, 253 { 4, "Administratively Reset"}, 254 { 5, "Connection Rejected"}, 255 { 6, "Other Configuration Change"}, 256 { 7, "Connection Collision Resolution"}, 257 { 0, NULL} 258 }; 259 260 static struct tok bgp_notify_minor_msg_values[] = { 261 { 1, "Connection Not Synchronized"}, 262 { 2, "Bad Message Length"}, 263 { 3, "Bad Message Type"}, 264 { 0, NULL} 265 }; 266 267 static struct tok bgp_notify_minor_open_values[] = { 268 { 1, "Unsupported Version Number"}, 269 { 2, "Bad Peer AS"}, 270 { 3, "Bad BGP Identifier"}, 271 { 4, "Unsupported Optional Parameter"}, 272 { 5, "Authentication Failure"}, 273 { 6, "Unacceptable Hold Time"}, 274 { 7, "Capability Message Error"}, 275 { 0, NULL} 276 }; 277 278 static struct tok bgp_notify_minor_update_values[] = { 279 { 1, "Malformed Attribute List"}, 280 { 2, "Unrecognized Well-known Attribute"}, 281 { 3, "Missing Well-known Attribute"}, 282 { 4, "Attribute Flags Error"}, 283 { 5, "Attribute Length Error"}, 284 { 6, "Invalid ORIGIN Attribute"}, 285 { 7, "AS Routing Loop"}, 286 { 8, "Invalid NEXT_HOP Attribute"}, 287 { 9, "Optional Attribute Error"}, 288 { 10, "Invalid Network Field"}, 289 { 11, "Malformed AS_PATH"}, 290 { 0, NULL} 291 }; 292 293 static struct tok bgp_notify_minor_cap_values[] = { 294 { 1, "Invalid Action Value" }, 295 { 2, "Invalid Capability Length" }, 296 { 3, "Malformed Capability Value" }, 297 { 4, "Unsupported Capability Code" }, 298 { 0, NULL } 299 }; 300 301 static struct tok bgp_origin_values[] = { 302 { 0, "IGP"}, 303 { 1, "EGP"}, 304 { 2, "Incomplete"}, 305 { 0, NULL} 306 }; 307 308 #define BGP_PMSI_TUNNEL_RSVP_P2MP 1 309 #define BGP_PMSI_TUNNEL_LDP_P2MP 2 310 #define BGP_PMSI_TUNNEL_PIM_SSM 3 311 #define BGP_PMSI_TUNNEL_PIM_SM 4 312 #define BGP_PMSI_TUNNEL_PIM_BIDIR 5 313 #define BGP_PMSI_TUNNEL_INGRESS 6 314 #define BGP_PMSI_TUNNEL_LDP_MP2MP 7 315 316 static struct tok bgp_pmsi_tunnel_values[] = { 317 { BGP_PMSI_TUNNEL_RSVP_P2MP, "RSVP-TE P2MP LSP"}, 318 { BGP_PMSI_TUNNEL_LDP_P2MP, "LDP P2MP LSP"}, 319 { BGP_PMSI_TUNNEL_PIM_SSM, "PIM-SSM Tree"}, 320 { BGP_PMSI_TUNNEL_PIM_SM, "PIM-SM Tree"}, 321 { BGP_PMSI_TUNNEL_PIM_BIDIR, "PIM-Bidir Tree"}, 322 { BGP_PMSI_TUNNEL_INGRESS, "Ingress Replication"}, 323 { BGP_PMSI_TUNNEL_LDP_MP2MP, "LDP MP2MP LSP"}, 324 { 0, NULL} 325 }; 326 327 static struct tok bgp_pmsi_flag_values[] = { 328 { 0x01, "Leaf Information required"}, 329 { 0, NULL} 330 }; 331 332 333 /* Subsequent address family identifier, RFC2283 section 7 */ 334 #define SAFNUM_RES 0 335 #define SAFNUM_UNICAST 1 336 #define SAFNUM_MULTICAST 2 337 #define SAFNUM_UNIMULTICAST 3 338 /* labeled BGP RFC3107 */ 339 #define SAFNUM_LABUNICAST 4 340 /* draft-ietf-l3vpn-2547bis-mcast-bgp-02.txt */ 341 #define SAFNUM_MULTICAST_VPN 5 342 #define SAFNUM_TUNNEL 64 /* XXX */ 343 #define SAFNUM_VPLS 65 /* XXX */ 344 /* draft-nalawade-idr-mdt-safi-03 */ 345 #define SAFNUM_MDT 66 346 /* Section 4.3.4 of draft-rosen-rfc2547bis-03.txt */ 347 #define SAFNUM_VPNUNICAST 128 348 #define SAFNUM_VPNMULTICAST 129 349 #define SAFNUM_VPNUNIMULTICAST 130 350 /* draft-marques-ppvpn-rt-constrain-01.txt */ 351 #define SAFNUM_RT_ROUTING_INFO 132 352 353 #define BGP_VPN_RD_LEN 8 354 355 static struct tok bgp_safi_values[] = { 356 { SAFNUM_RES, "Reserved"}, 357 { SAFNUM_UNICAST, "Unicast"}, 358 { SAFNUM_MULTICAST, "Multicast"}, 359 { SAFNUM_UNIMULTICAST, "Unicast+Multicast"}, 360 { SAFNUM_LABUNICAST, "labeled Unicast"}, 361 { SAFNUM_TUNNEL, "Tunnel"}, 362 { SAFNUM_VPLS, "VPLS"}, 363 { SAFNUM_MDT, "MDT"}, 364 { SAFNUM_VPNUNICAST, "labeled VPN Unicast"}, 365 { SAFNUM_VPNMULTICAST, "labeled VPN Multicast"}, 366 { SAFNUM_VPNUNIMULTICAST, "labeled VPN Unicast+Multicast"}, 367 { SAFNUM_RT_ROUTING_INFO, "Route Target Routing Information"}, 368 { SAFNUM_MULTICAST_VPN, "Multicast VPN"}, 369 { 0, NULL } 370 }; 371 372 /* well-known community */ 373 #define BGP_COMMUNITY_NO_EXPORT 0xffffff01 374 #define BGP_COMMUNITY_NO_ADVERT 0xffffff02 375 #define BGP_COMMUNITY_NO_EXPORT_SUBCONFED 0xffffff03 376 377 /* Extended community type - draft-ietf-idr-bgp-ext-communities-05 */ 378 #define BGP_EXT_COM_RT_0 0x0002 /* Route Target,Format AS(2bytes):AN(4bytes) */ 379 #define BGP_EXT_COM_RT_1 0x0102 /* Route Target,Format IP address:AN(2bytes) */ 380 #define BGP_EXT_COM_RT_2 0x0202 /* Route Target,Format AN(4bytes):local(2bytes) */ 381 #define BGP_EXT_COM_RO_0 0x0003 /* Route Origin,Format AS(2bytes):AN(4bytes) */ 382 #define BGP_EXT_COM_RO_1 0x0103 /* Route Origin,Format IP address:AN(2bytes) */ 383 #define BGP_EXT_COM_RO_2 0x0203 /* Route Origin,Format AN(4bytes):local(2bytes) */ 384 #define BGP_EXT_COM_LINKBAND 0x4004 /* Link Bandwidth,Format AS(2B):Bandwidth(4B) */ 385 /* rfc2547 bgp-mpls-vpns */ 386 #define BGP_EXT_COM_VPN_ORIGIN 0x0005 /* OSPF Domain ID / VPN of Origin - draft-rosen-vpns-ospf-bgp-mpls */ 387 #define BGP_EXT_COM_VPN_ORIGIN2 0x0105 /* duplicate - keep for backwards compatability */ 388 #define BGP_EXT_COM_VPN_ORIGIN3 0x0205 /* duplicate - keep for backwards compatability */ 389 #define BGP_EXT_COM_VPN_ORIGIN4 0x8005 /* duplicate - keep for backwards compatability */ 390 391 #define BGP_EXT_COM_OSPF_RTYPE 0x0306 /* OSPF Route Type,Format Area(4B):RouteType(1B):Options(1B) */ 392 #define BGP_EXT_COM_OSPF_RTYPE2 0x8000 /* duplicate - keep for backwards compatability */ 393 394 #define BGP_EXT_COM_OSPF_RID 0x0107 /* OSPF Router ID,Format RouterID(4B):Unused(2B) */ 395 #define BGP_EXT_COM_OSPF_RID2 0x8001 /* duplicate - keep for backwards compatability */ 396 397 #define BGP_EXT_COM_L2INFO 0x800a /* draft-kompella-ppvpn-l2vpn */ 398 399 #define BGP_EXT_COM_SOURCE_AS 0x0009 /* RFC-ietf-l3vpn-2547bis-mcast-bgp-08.txt */ 400 #define BGP_EXT_COM_VRF_RT_IMP 0x010b /* RFC-ietf-l3vpn-2547bis-mcast-bgp-08.txt */ 401 #define BGP_EXT_COM_L2VPN_RT_0 0x000a /* L2VPN Identifier,Format AS(2bytes):AN(4bytes) */ 402 #define BGP_EXT_COM_L2VPN_RT_1 0xF10a /* L2VPN Identifier,Format IP address:AN(2bytes) */ 403 404 405 /* http://www.cisco.com/en/US/tech/tk436/tk428/technologies_tech_note09186a00801eb09a.shtml */ 406 #define BGP_EXT_COM_EIGRP_GEN 0x8800 407 #define BGP_EXT_COM_EIGRP_METRIC_AS_DELAY 0x8801 408 #define BGP_EXT_COM_EIGRP_METRIC_REL_NH_BW 0x8802 409 #define BGP_EXT_COM_EIGRP_METRIC_LOAD_MTU 0x8803 410 #define BGP_EXT_COM_EIGRP_EXT_REMAS_REMID 0x8804 411 #define BGP_EXT_COM_EIGRP_EXT_REMPROTO_REMMETRIC 0x8805 412 413 static struct tok bgp_extd_comm_flag_values[] = { 414 { 0x8000, "vendor-specific"}, 415 { 0x4000, "non-transitive"}, 416 { 0, NULL}, 417 }; 418 419 static struct tok bgp_extd_comm_subtype_values[] = { 420 { BGP_EXT_COM_RT_0, "target"}, 421 { BGP_EXT_COM_RT_1, "target"}, 422 { BGP_EXT_COM_RT_2, "target"}, 423 { BGP_EXT_COM_RO_0, "origin"}, 424 { BGP_EXT_COM_RO_1, "origin"}, 425 { BGP_EXT_COM_RO_2, "origin"}, 426 { BGP_EXT_COM_LINKBAND, "link-BW"}, 427 { BGP_EXT_COM_VPN_ORIGIN, "ospf-domain"}, 428 { BGP_EXT_COM_VPN_ORIGIN2, "ospf-domain"}, 429 { BGP_EXT_COM_VPN_ORIGIN3, "ospf-domain"}, 430 { BGP_EXT_COM_VPN_ORIGIN4, "ospf-domain"}, 431 { BGP_EXT_COM_OSPF_RTYPE, "ospf-route-type"}, 432 { BGP_EXT_COM_OSPF_RTYPE2, "ospf-route-type"}, 433 { BGP_EXT_COM_OSPF_RID, "ospf-router-id"}, 434 { BGP_EXT_COM_OSPF_RID2, "ospf-router-id"}, 435 { BGP_EXT_COM_L2INFO, "layer2-info"}, 436 { BGP_EXT_COM_EIGRP_GEN , "eigrp-general-route (flag, tag)" }, 437 { BGP_EXT_COM_EIGRP_METRIC_AS_DELAY , "eigrp-route-metric (AS, delay)" }, 438 { BGP_EXT_COM_EIGRP_METRIC_REL_NH_BW , "eigrp-route-metric (reliability, nexthop, bandwidth)" }, 439 { BGP_EXT_COM_EIGRP_METRIC_LOAD_MTU , "eigrp-route-metric (load, MTU)" }, 440 { BGP_EXT_COM_EIGRP_EXT_REMAS_REMID , "eigrp-external-route (remote-AS, remote-ID)" }, 441 { BGP_EXT_COM_EIGRP_EXT_REMPROTO_REMMETRIC , "eigrp-external-route (remote-proto, remote-metric)" }, 442 { BGP_EXT_COM_SOURCE_AS, "source-AS" }, 443 { BGP_EXT_COM_VRF_RT_IMP, "vrf-route-import"}, 444 { BGP_EXT_COM_L2VPN_RT_0, "l2vpn-id"}, 445 { BGP_EXT_COM_L2VPN_RT_1, "l2vpn-id"}, 446 { 0, NULL}, 447 }; 448 449 /* OSPF codes for BGP_EXT_COM_OSPF_RTYPE draft-rosen-vpns-ospf-bgp-mpls */ 450 #define BGP_OSPF_RTYPE_RTR 1 /* OSPF Router LSA */ 451 #define BGP_OSPF_RTYPE_NET 2 /* OSPF Network LSA */ 452 #define BGP_OSPF_RTYPE_SUM 3 /* OSPF Summary LSA */ 453 #define BGP_OSPF_RTYPE_EXT 5 /* OSPF External LSA, note that ASBR doesn't apply to MPLS-VPN */ 454 #define BGP_OSPF_RTYPE_NSSA 7 /* OSPF NSSA External*/ 455 #define BGP_OSPF_RTYPE_SHAM 129 /* OSPF-MPLS-VPN Sham link */ 456 #define BGP_OSPF_RTYPE_METRIC_TYPE 0x1 /* LSB of RTYPE Options Field */ 457 458 static struct tok bgp_extd_comm_ospf_rtype_values[] = { 459 { BGP_OSPF_RTYPE_RTR, "Router" }, 460 { BGP_OSPF_RTYPE_NET, "Network" }, 461 { BGP_OSPF_RTYPE_SUM, "Summary" }, 462 { BGP_OSPF_RTYPE_EXT, "External" }, 463 { BGP_OSPF_RTYPE_NSSA,"NSSA External" }, 464 { BGP_OSPF_RTYPE_SHAM,"MPLS-VPN Sham" }, 465 { 0, NULL }, 466 }; 467 468 #define TOKBUFSIZE 128 469 static char astostr[20]; 470 471 /* 472 * as_printf 473 * 474 * Convert an AS number into a string and return string pointer. 475 * 476 * Bepending on bflag is set or not, AS number is converted into ASDOT notation 477 * or plain number notation. 478 * 479 */ 480 static char * 481 as_printf (char *str, int size, u_int asnum) 482 { 483 if (!bflag || asnum <= 0xFFFF) { 484 snprintf(str, size, "%u", asnum); 485 } else { 486 snprintf(str, size, "%u.%u", asnum >> 16, asnum & 0xFFFF); 487 } 488 return str; 489 } 490 491 #define ITEMCHECK(minlen) if (itemlen < minlen) goto badtlv; 492 493 int 494 decode_prefix4(const u_char *pptr, u_int itemlen, char *buf, u_int buflen) 495 { 496 struct in_addr addr; 497 u_int plen, plenbytes; 498 499 TCHECK(pptr[0]); 500 ITEMCHECK(1); 501 plen = pptr[0]; 502 if (32 < plen) 503 return -1; 504 itemlen -= 1; 505 506 memset(&addr, 0, sizeof(addr)); 507 plenbytes = (plen + 7) / 8; 508 TCHECK2(pptr[1], plenbytes); 509 ITEMCHECK(plenbytes); 510 memcpy(&addr, &pptr[1], plenbytes); 511 if (plen % 8) { 512 ((u_char *)&addr)[plenbytes - 1] &= 513 ((0xff00 >> (plen % 8)) & 0xff); 514 } 515 snprintf(buf, buflen, "%s/%d", getname((u_char *)&addr), plen); 516 return 1 + plenbytes; 517 518 trunc: 519 return -2; 520 521 badtlv: 522 return -3; 523 } 524 525 static int 526 decode_labeled_prefix4(const u_char *pptr, u_int itemlen, char *buf, u_int buflen) 527 { 528 struct in_addr addr; 529 u_int plen, plenbytes; 530 531 /* prefix length and label = 4 bytes */ 532 TCHECK2(pptr[0], 4); 533 ITEMCHECK(4); 534 plen = pptr[0]; /* get prefix length */ 535 536 /* this is one of the weirdnesses of rfc3107 537 the label length (actually the label + COS bits) 538 is added to the prefix length; 539 we also do only read out just one label - 540 there is no real application for advertisement of 541 stacked labels in a a single BGP message 542 */ 543 544 if (24 > plen) 545 return -1; 546 547 plen-=24; /* adjust prefixlen - labellength */ 548 549 if (32 < plen) 550 return -1; 551 itemlen -= 4; 552 553 memset(&addr, 0, sizeof(addr)); 554 plenbytes = (plen + 7) / 8; 555 TCHECK2(pptr[4], plenbytes); 556 ITEMCHECK(plenbytes); 557 memcpy(&addr, &pptr[4], plenbytes); 558 if (plen % 8) { 559 ((u_char *)&addr)[plenbytes - 1] &= 560 ((0xff00 >> (plen % 8)) & 0xff); 561 } 562 /* the label may get offsetted by 4 bits so lets shift it right */ 563 snprintf(buf, buflen, "%s/%d, label:%u %s", 564 getname((u_char *)&addr), 565 plen, 566 EXTRACT_24BITS(pptr+1)>>4, 567 ((pptr[3]&1)==0) ? "(BOGUS: Bottom of Stack NOT set!)" : "(bottom)" ); 568 569 return 4 + plenbytes; 570 571 trunc: 572 return -2; 573 574 badtlv: 575 return -3; 576 } 577 578 /* 579 * bgp_vpn_ip_print 580 * 581 * print an ipv4 or ipv6 address into a buffer dependend on address length. 582 */ 583 static char * 584 bgp_vpn_ip_print (const u_char *pptr, u_int addr_length) { 585 586 /* worst case string is s fully formatted v6 address */ 587 static char addr[sizeof("1234:5678:89ab:cdef:1234:5678:89ab:cdef")]; 588 char *pos = addr; 589 590 switch(addr_length) { 591 case (sizeof(struct in_addr) << 3): /* 32 */ 592 TCHECK2(pptr[0], sizeof(struct in_addr)); 593 snprintf(pos, sizeof(addr), "%s", ipaddr_string(pptr)); 594 break; 595 #ifdef INET6 596 case (sizeof(struct in6_addr) << 3): /* 128 */ 597 TCHECK2(pptr[0], sizeof(struct in6_addr)); 598 snprintf(pos, sizeof(addr), "%s", ip6addr_string(pptr)); 599 break; 600 #endif 601 default: 602 snprintf(pos, sizeof(addr), "bogus address length %u", addr_length); 603 break; 604 } 605 pos += strlen(pos); 606 607 trunc: 608 *(pos) = '\0'; 609 return (addr); 610 } 611 612 /* 613 * bgp_vpn_sg_print 614 * 615 * print an multicast s,g entry into a buffer. 616 * the s,g entry is encoded like this. 617 * 618 * +-----------------------------------+ 619 * | Multicast Source Length (1 octet) | 620 * +-----------------------------------+ 621 * | Multicast Source (Variable) | 622 * +-----------------------------------+ 623 * | Multicast Group Length (1 octet) | 624 * +-----------------------------------+ 625 * | Multicast Group (Variable) | 626 * +-----------------------------------+ 627 * 628 * return the number of bytes read from the wire. 629 */ 630 static int 631 bgp_vpn_sg_print (const u_char *pptr, char *buf, u_int buflen) { 632 633 u_int8_t addr_length; 634 u_int total_length, offset; 635 636 total_length = 0; 637 638 /* Source address length, encoded in bits */ 639 TCHECK2(pptr[0], 1); 640 addr_length = *pptr++; 641 642 /* Source address */ 643 TCHECK2(pptr[0], (addr_length >> 3)); 644 total_length += (addr_length >> 3) + 1; 645 offset = strlen(buf); 646 if (addr_length) { 647 snprintf(buf + offset, buflen - offset, ", Source %s", 648 bgp_vpn_ip_print(pptr, addr_length)); 649 pptr += (addr_length >> 3); 650 } 651 652 /* Group address length, encoded in bits */ 653 TCHECK2(pptr[0], 1); 654 addr_length = *pptr++; 655 656 /* Group address */ 657 TCHECK2(pptr[0], (addr_length >> 3)); 658 total_length += (addr_length >> 3) + 1; 659 offset = strlen(buf); 660 if (addr_length) { 661 snprintf(buf + offset, buflen - offset, ", Group %s", 662 bgp_vpn_ip_print(pptr, addr_length)); 663 pptr += (addr_length >> 3); 664 } 665 666 trunc: 667 return (total_length); 668 } 669 670 671 /* RDs and RTs share the same semantics 672 * we use bgp_vpn_rd_print for 673 * printing route targets inside a NLRI */ 674 char * 675 bgp_vpn_rd_print (const u_char *pptr) { 676 677 /* allocate space for the largest possible string */ 678 static char rd[sizeof("xxxxxxxxxx:xxxxx (xxx.xxx.xxx.xxx:xxxxx)")]; 679 char *pos = rd; 680 681 /* ok lets load the RD format */ 682 switch (EXTRACT_16BITS(pptr)) { 683 684 /* 2-byte-AS:number fmt*/ 685 case 0: 686 snprintf(pos, sizeof(rd) - (pos - rd), "%u:%u (= %u.%u.%u.%u)", 687 EXTRACT_16BITS(pptr+2), 688 EXTRACT_32BITS(pptr+4), 689 *(pptr+4), *(pptr+5), *(pptr+6), *(pptr+7)); 690 break; 691 /* IP-address:AS fmt*/ 692 693 case 1: 694 snprintf(pos, sizeof(rd) - (pos - rd), "%u.%u.%u.%u:%u", 695 *(pptr+2), *(pptr+3), *(pptr+4), *(pptr+5), EXTRACT_16BITS(pptr+6)); 696 break; 697 698 /* 4-byte-AS:number fmt*/ 699 case 2: 700 snprintf(pos, sizeof(rd) - (pos - rd), "%s:%u (%u.%u.%u.%u:%u)", 701 as_printf(astostr, sizeof(astostr), EXTRACT_32BITS(pptr+2)), 702 EXTRACT_16BITS(pptr+6), *(pptr+2), *(pptr+3), *(pptr+4), 703 *(pptr+5), EXTRACT_16BITS(pptr+6)); 704 break; 705 default: 706 snprintf(pos, sizeof(rd) - (pos - rd), "unknown RD format"); 707 break; 708 } 709 pos += strlen(pos); 710 *(pos) = '\0'; 711 return (rd); 712 } 713 714 static int 715 decode_rt_routing_info(const u_char *pptr, char *buf, u_int buflen) 716 { 717 u_int8_t route_target[8]; 718 u_int plen; 719 720 TCHECK(pptr[0]); 721 plen = pptr[0]; /* get prefix length */ 722 723 if (0 == plen) 724 return 1; /* default route target */ 725 726 if (32 > plen) 727 return -1; 728 729 plen-=32; /* adjust prefix length */ 730 731 if (64 < plen) 732 return -1; 733 734 memset(&route_target, 0, sizeof(route_target)); 735 TCHECK2(pptr[1], (plen + 7) / 8); 736 memcpy(&route_target, &pptr[1], (plen + 7) / 8); 737 if (plen % 8) { 738 ((u_char *)&route_target)[(plen + 7) / 8 - 1] &= 739 ((0xff00 >> (plen % 8)) & 0xff); 740 } 741 snprintf(buf, buflen, "origin AS: %s, route target %s", 742 as_printf(astostr, sizeof(astostr), EXTRACT_32BITS(pptr+1)), 743 bgp_vpn_rd_print((u_char *)&route_target)); 744 745 return 5 + (plen + 7) / 8; 746 747 trunc: 748 return -2; 749 } 750 751 static int 752 decode_labeled_vpn_prefix4(const u_char *pptr, char *buf, u_int buflen) 753 { 754 struct in_addr addr; 755 u_int plen; 756 757 TCHECK(pptr[0]); 758 plen = pptr[0]; /* get prefix length */ 759 760 if ((24+64) > plen) 761 return -1; 762 763 plen-=(24+64); /* adjust prefixlen - labellength - RD len*/ 764 765 if (32 < plen) 766 return -1; 767 768 memset(&addr, 0, sizeof(addr)); 769 TCHECK2(pptr[12], (plen + 7) / 8); 770 memcpy(&addr, &pptr[12], (plen + 7) / 8); 771 if (plen % 8) { 772 ((u_char *)&addr)[(plen + 7) / 8 - 1] &= 773 ((0xff00 >> (plen % 8)) & 0xff); 774 } 775 /* the label may get offsetted by 4 bits so lets shift it right */ 776 snprintf(buf, buflen, "RD: %s, %s/%d, label:%u %s", 777 bgp_vpn_rd_print(pptr+4), 778 getname((u_char *)&addr), 779 plen, 780 EXTRACT_24BITS(pptr+1)>>4, 781 ((pptr[3]&1)==0) ? "(BOGUS: Bottom of Stack NOT set!)" : "(bottom)" ); 782 783 return 12 + (plen + 7) / 8; 784 785 trunc: 786 return -2; 787 } 788 789 /* 790 * +-------------------------------+ 791 * | | 792 * | RD:IPv4-address (12 octets) | 793 * | | 794 * +-------------------------------+ 795 * | MDT Group-address (4 octets) | 796 * +-------------------------------+ 797 */ 798 799 #define MDT_VPN_NLRI_LEN 16 800 801 static int 802 decode_mdt_vpn_nlri(const u_char *pptr, char *buf, u_int buflen) 803 { 804 805 const u_char *rd; 806 const u_char *vpn_ip; 807 808 TCHECK(pptr[0]); 809 810 /* if the NLRI is not predefined length, quit.*/ 811 if (*pptr != MDT_VPN_NLRI_LEN * NBBY) 812 return -1; 813 pptr++; 814 815 /* RD */ 816 TCHECK2(pptr[0], 8); 817 rd = pptr; 818 pptr+=8; 819 820 /* IPv4 address */ 821 TCHECK2(pptr[0], sizeof(struct in_addr)); 822 vpn_ip = pptr; 823 pptr+=sizeof(struct in_addr); 824 825 /* MDT Group Address */ 826 TCHECK2(pptr[0], sizeof(struct in_addr)); 827 828 snprintf(buf, buflen, "RD: %s, VPN IP Address: %s, MC Group Address: %s", 829 bgp_vpn_rd_print(rd), ipaddr_string(vpn_ip), ipaddr_string(pptr)); 830 831 return MDT_VPN_NLRI_LEN + 1; 832 833 trunc: 834 835 return -2; 836 } 837 838 #define BGP_MULTICAST_VPN_ROUTE_TYPE_INTRA_AS_I_PMSI 1 839 #define BGP_MULTICAST_VPN_ROUTE_TYPE_INTER_AS_I_PMSI 2 840 #define BGP_MULTICAST_VPN_ROUTE_TYPE_S_PMSI 3 841 #define BGP_MULTICAST_VPN_ROUTE_TYPE_INTRA_AS_SEG_LEAF 4 842 #define BGP_MULTICAST_VPN_ROUTE_TYPE_SOURCE_ACTIVE 5 843 #define BGP_MULTICAST_VPN_ROUTE_TYPE_SHARED_TREE_JOIN 6 844 #define BGP_MULTICAST_VPN_ROUTE_TYPE_SOURCE_TREE_JOIN 7 845 846 static struct tok bgp_multicast_vpn_route_type_values[] = { 847 { BGP_MULTICAST_VPN_ROUTE_TYPE_INTRA_AS_I_PMSI, "Intra-AS I-PMSI"}, 848 { BGP_MULTICAST_VPN_ROUTE_TYPE_INTER_AS_I_PMSI, "Inter-AS I-PMSI"}, 849 { BGP_MULTICAST_VPN_ROUTE_TYPE_S_PMSI, "S-PMSI"}, 850 { BGP_MULTICAST_VPN_ROUTE_TYPE_INTRA_AS_SEG_LEAF, "Intra-AS Segment-Leaf"}, 851 { BGP_MULTICAST_VPN_ROUTE_TYPE_SOURCE_ACTIVE, "Source-Active"}, 852 { BGP_MULTICAST_VPN_ROUTE_TYPE_SHARED_TREE_JOIN, "Shared Tree Join"}, 853 { BGP_MULTICAST_VPN_ROUTE_TYPE_SOURCE_TREE_JOIN, "Source Tree Join"}, 854 }; 855 856 static int 857 decode_multicast_vpn(const u_char *pptr, char *buf, u_int buflen) 858 { 859 u_int8_t route_type, route_length, addr_length, sg_length; 860 u_int offset; 861 862 TCHECK2(pptr[0], 2); 863 route_type = *pptr++; 864 route_length = *pptr++; 865 866 snprintf(buf, buflen, "Route-Type: %s (%u), length: %u", 867 tok2str(bgp_multicast_vpn_route_type_values, 868 "Unknown", route_type), 869 route_type, route_length); 870 871 switch(route_type) { 872 case BGP_MULTICAST_VPN_ROUTE_TYPE_INTRA_AS_I_PMSI: 873 TCHECK2(pptr[0], BGP_VPN_RD_LEN); 874 offset = strlen(buf); 875 snprintf(buf + offset, buflen - offset, ", RD: %s, Originator %s", 876 bgp_vpn_rd_print(pptr), 877 bgp_vpn_ip_print(pptr + BGP_VPN_RD_LEN, 878 (route_length - BGP_VPN_RD_LEN) << 3)); 879 break; 880 case BGP_MULTICAST_VPN_ROUTE_TYPE_INTER_AS_I_PMSI: 881 TCHECK2(pptr[0], BGP_VPN_RD_LEN + 4); 882 offset = strlen(buf); 883 snprintf(buf + offset, buflen - offset, ", RD: %s, Source-AS %s", 884 bgp_vpn_rd_print(pptr), 885 as_printf(astostr, sizeof(astostr), 886 EXTRACT_32BITS(pptr + BGP_VPN_RD_LEN))); 887 break; 888 889 case BGP_MULTICAST_VPN_ROUTE_TYPE_S_PMSI: 890 TCHECK2(pptr[0], BGP_VPN_RD_LEN); 891 offset = strlen(buf); 892 snprintf(buf + offset, buflen - offset, ", RD: %s", 893 bgp_vpn_rd_print(pptr)); 894 pptr += BGP_VPN_RD_LEN; 895 896 sg_length = bgp_vpn_sg_print(pptr, buf, buflen); 897 addr_length = route_length - sg_length; 898 899 TCHECK2(pptr[0], addr_length); 900 offset = strlen(buf); 901 snprintf(buf + offset, buflen - offset, ", Originator %s", 902 bgp_vpn_ip_print(pptr, addr_length << 3)); 903 break; 904 905 case BGP_MULTICAST_VPN_ROUTE_TYPE_SOURCE_ACTIVE: 906 TCHECK2(pptr[0], BGP_VPN_RD_LEN); 907 offset = strlen(buf); 908 snprintf(buf + offset, buflen - offset, ", RD: %s", 909 bgp_vpn_rd_print(pptr)); 910 pptr += BGP_VPN_RD_LEN; 911 912 bgp_vpn_sg_print(pptr, buf, buflen); 913 break; 914 915 case BGP_MULTICAST_VPN_ROUTE_TYPE_SHARED_TREE_JOIN: /* fall through */ 916 case BGP_MULTICAST_VPN_ROUTE_TYPE_SOURCE_TREE_JOIN: 917 TCHECK2(pptr[0], BGP_VPN_RD_LEN); 918 offset = strlen(buf); 919 snprintf(buf + offset, buflen - offset, ", RD: %s, Source-AS %s", 920 bgp_vpn_rd_print(pptr), 921 as_printf(astostr, sizeof(astostr), 922 EXTRACT_32BITS(pptr + BGP_VPN_RD_LEN))); 923 pptr += BGP_VPN_RD_LEN; 924 925 bgp_vpn_sg_print(pptr, buf, buflen); 926 break; 927 928 /* 929 * no per route-type printing yet. 930 */ 931 case BGP_MULTICAST_VPN_ROUTE_TYPE_INTRA_AS_SEG_LEAF: 932 default: 933 break; 934 } 935 936 return route_length + 2; 937 938 trunc: 939 return -2; 940 } 941 942 /* 943 * As I remember, some versions of systems have an snprintf() that 944 * returns -1 if the buffer would have overflowed. If the return 945 * value is negative, set buflen to 0, to indicate that we've filled 946 * the buffer up. 947 * 948 * If the return value is greater than buflen, that means that 949 * the buffer would have overflowed; again, set buflen to 0 in 950 * that case. 951 */ 952 #define UPDATE_BUF_BUFLEN(buf, buflen, strlen) \ 953 if (strlen<0) \ 954 buflen=0; \ 955 else if ((u_int)strlen>buflen) \ 956 buflen=0; \ 957 else { \ 958 buflen-=strlen; \ 959 buf+=strlen; \ 960 } 961 962 static int 963 decode_labeled_vpn_l2(const u_char *pptr, char *buf, u_int buflen) 964 { 965 int plen,tlen,strlen,tlv_type,tlv_len,ttlv_len; 966 967 TCHECK2(pptr[0], 2); 968 plen=EXTRACT_16BITS(pptr); 969 tlen=plen; 970 pptr+=2; 971 /* Old and new L2VPN NLRI share AFI/SAFI 972 * -> Assume a 12 Byte-length NLRI is auto-discovery-only 973 * and > 17 as old format. Complain for the middle case 974 */ 975 if (plen==12) { 976 /* assume AD-only with RD, BGPNH */ 977 TCHECK2(pptr[0],12); 978 buf[0]='\0'; 979 strlen=snprintf(buf, buflen, "RD: %s, BGPNH: %s", 980 bgp_vpn_rd_print(pptr), 981 /* need something like getname() here */ 982 getname(pptr+8) 983 ); 984 UPDATE_BUF_BUFLEN(buf, buflen, strlen); 985 pptr+=12; 986 tlen-=12; 987 return plen; 988 } else if (plen>17) { 989 /* assume old format */ 990 /* RD, ID, LBLKOFF, LBLBASE */ 991 992 TCHECK2(pptr[0],15); 993 buf[0]='\0'; 994 strlen=snprintf(buf, buflen, "RD: %s, CE-ID: %u, Label-Block Offset: %u, Label Base %u", 995 bgp_vpn_rd_print(pptr), 996 EXTRACT_16BITS(pptr+8), 997 EXTRACT_16BITS(pptr+10), 998 EXTRACT_24BITS(pptr+12)>>4); /* the label is offsetted by 4 bits so lets shift it right */ 999 UPDATE_BUF_BUFLEN(buf, buflen, strlen); 1000 pptr+=15; 1001 tlen-=15; 1002 1003 /* ok now the variable part - lets read out TLVs*/ 1004 while (tlen>0) { 1005 if (tlen < 3) 1006 return -1; 1007 TCHECK2(pptr[0], 3); 1008 tlv_type=*pptr++; 1009 tlv_len=EXTRACT_16BITS(pptr); 1010 ttlv_len=tlv_len; 1011 pptr+=2; 1012 1013 switch(tlv_type) { 1014 case 1: 1015 if (buflen!=0) { 1016 strlen=snprintf(buf,buflen, "\n\t\tcircuit status vector (%u) length: %u: 0x", 1017 tlv_type, 1018 tlv_len); 1019 UPDATE_BUF_BUFLEN(buf, buflen, strlen); 1020 } 1021 ttlv_len=ttlv_len/8+1; /* how many bytes do we need to read ? */ 1022 while (ttlv_len>0) { 1023 TCHECK(pptr[0]); 1024 if (buflen!=0) { 1025 strlen=snprintf(buf,buflen, "%02x",*pptr++); 1026 UPDATE_BUF_BUFLEN(buf, buflen, strlen); 1027 } 1028 ttlv_len--; 1029 } 1030 break; 1031 default: 1032 if (buflen!=0) { 1033 strlen=snprintf(buf,buflen, "\n\t\tunknown TLV #%u, length: %u", 1034 tlv_type, 1035 tlv_len); 1036 UPDATE_BUF_BUFLEN(buf, buflen, strlen); 1037 } 1038 break; 1039 } 1040 tlen-=(tlv_len<<3); /* the tlv-length is expressed in bits so lets shift it right */ 1041 } 1042 return plen+2; 1043 1044 } else { 1045 /* complain bitterly ? */ 1046 /* fall through */ 1047 goto trunc; 1048 } 1049 1050 trunc: 1051 return -2; 1052 } 1053 1054 #ifdef INET6 1055 int 1056 decode_prefix6(const u_char *pd, u_int itemlen, char *buf, u_int buflen) 1057 { 1058 struct in6_addr addr; 1059 u_int plen, plenbytes; 1060 1061 TCHECK(pd[0]); 1062 ITEMCHECK(1); 1063 plen = pd[0]; 1064 if (128 < plen) 1065 return -1; 1066 itemlen -= 1; 1067 1068 memset(&addr, 0, sizeof(addr)); 1069 plenbytes = (plen + 7) / 8; 1070 TCHECK2(pd[1], plenbytes); 1071 ITEMCHECK(plenbytes); 1072 memcpy(&addr, &pd[1], plenbytes); 1073 if (plen % 8) { 1074 addr.s6_addr[plenbytes - 1] &= 1075 ((0xff00 >> (plen % 8)) & 0xff); 1076 } 1077 snprintf(buf, buflen, "%s/%d", getname6((u_char *)&addr), plen); 1078 return 1 + plenbytes; 1079 1080 trunc: 1081 return -2; 1082 1083 badtlv: 1084 return -3; 1085 } 1086 1087 static int 1088 decode_labeled_prefix6(const u_char *pptr, u_int itemlen, char *buf, u_int buflen) 1089 { 1090 struct in6_addr addr; 1091 u_int plen, plenbytes; 1092 1093 /* prefix length and label = 4 bytes */ 1094 TCHECK2(pptr[0], 4); 1095 ITEMCHECK(4); 1096 plen = pptr[0]; /* get prefix length */ 1097 1098 if (24 > plen) 1099 return -1; 1100 1101 plen-=24; /* adjust prefixlen - labellength */ 1102 1103 if (128 < plen) 1104 return -1; 1105 itemlen -= 4; 1106 1107 memset(&addr, 0, sizeof(addr)); 1108 plenbytes = (plen + 7) / 8; 1109 TCHECK2(pptr[4], plenbytes); 1110 memcpy(&addr, &pptr[4], plenbytes); 1111 if (plen % 8) { 1112 addr.s6_addr[plenbytes - 1] &= 1113 ((0xff00 >> (plen % 8)) & 0xff); 1114 } 1115 /* the label may get offsetted by 4 bits so lets shift it right */ 1116 snprintf(buf, buflen, "%s/%d, label:%u %s", 1117 getname6((u_char *)&addr), 1118 plen, 1119 EXTRACT_24BITS(pptr+1)>>4, 1120 ((pptr[3]&1)==0) ? "(BOGUS: Bottom of Stack NOT set!)" : "(bottom)" ); 1121 1122 return 4 + plenbytes; 1123 1124 trunc: 1125 return -2; 1126 1127 badtlv: 1128 return -3; 1129 } 1130 1131 static int 1132 decode_labeled_vpn_prefix6(const u_char *pptr, char *buf, u_int buflen) 1133 { 1134 struct in6_addr addr; 1135 u_int plen; 1136 1137 TCHECK(pptr[0]); 1138 plen = pptr[0]; /* get prefix length */ 1139 1140 if ((24+64) > plen) 1141 return -1; 1142 1143 plen-=(24+64); /* adjust prefixlen - labellength - RD len*/ 1144 1145 if (128 < plen) 1146 return -1; 1147 1148 memset(&addr, 0, sizeof(addr)); 1149 TCHECK2(pptr[12], (plen + 7) / 8); 1150 memcpy(&addr, &pptr[12], (plen + 7) / 8); 1151 if (plen % 8) { 1152 addr.s6_addr[(plen + 7) / 8 - 1] &= 1153 ((0xff00 >> (plen % 8)) & 0xff); 1154 } 1155 /* the label may get offsetted by 4 bits so lets shift it right */ 1156 snprintf(buf, buflen, "RD: %s, %s/%d, label:%u %s", 1157 bgp_vpn_rd_print(pptr+4), 1158 getname6((u_char *)&addr), 1159 plen, 1160 EXTRACT_24BITS(pptr+1)>>4, 1161 ((pptr[3]&1)==0) ? "(BOGUS: Bottom of Stack NOT set!)" : "(bottom)" ); 1162 1163 return 12 + (plen + 7) / 8; 1164 1165 trunc: 1166 return -2; 1167 } 1168 #endif 1169 1170 static int 1171 decode_clnp_prefix(const u_char *pptr, char *buf, u_int buflen) 1172 { 1173 u_int8_t addr[19]; 1174 u_int plen; 1175 1176 TCHECK(pptr[0]); 1177 plen = pptr[0]; /* get prefix length */ 1178 1179 if (152 < plen) 1180 return -1; 1181 1182 memset(&addr, 0, sizeof(addr)); 1183 TCHECK2(pptr[4], (plen + 7) / 8); 1184 memcpy(&addr, &pptr[4], (plen + 7) / 8); 1185 if (plen % 8) { 1186 addr[(plen + 7) / 8 - 1] &= 1187 ((0xff00 >> (plen % 8)) & 0xff); 1188 } 1189 snprintf(buf, buflen, "%s/%d", 1190 isonsap_string(addr,(plen + 7) / 8), 1191 plen); 1192 1193 return 1 + (plen + 7) / 8; 1194 1195 trunc: 1196 return -2; 1197 } 1198 1199 static int 1200 decode_labeled_vpn_clnp_prefix(const u_char *pptr, char *buf, u_int buflen) 1201 { 1202 u_int8_t addr[19]; 1203 u_int plen; 1204 1205 TCHECK(pptr[0]); 1206 plen = pptr[0]; /* get prefix length */ 1207 1208 if ((24+64) > plen) 1209 return -1; 1210 1211 plen-=(24+64); /* adjust prefixlen - labellength - RD len*/ 1212 1213 if (152 < plen) 1214 return -1; 1215 1216 memset(&addr, 0, sizeof(addr)); 1217 TCHECK2(pptr[12], (plen + 7) / 8); 1218 memcpy(&addr, &pptr[12], (plen + 7) / 8); 1219 if (plen % 8) { 1220 addr[(plen + 7) / 8 - 1] &= 1221 ((0xff00 >> (plen % 8)) & 0xff); 1222 } 1223 /* the label may get offsetted by 4 bits so lets shift it right */ 1224 snprintf(buf, buflen, "RD: %s, %s/%d, label:%u %s", 1225 bgp_vpn_rd_print(pptr+4), 1226 isonsap_string(addr,(plen + 7) / 8), 1227 plen, 1228 EXTRACT_24BITS(pptr+1)>>4, 1229 ((pptr[3]&1)==0) ? "(BOGUS: Bottom of Stack NOT set!)" : "(bottom)" ); 1230 1231 return 12 + (plen + 7) / 8; 1232 1233 trunc: 1234 return -2; 1235 } 1236 1237 /* 1238 * bgp_attr_get_as_size 1239 * 1240 * Try to find the size of the ASs encoded in an as-path. It is not obvious, as 1241 * both Old speakers that do not support 4 byte AS, and the new speakers that do 1242 * support, exchange AS-Path with the same path-attribute type value 0x02. 1243 */ 1244 static int 1245 bgp_attr_get_as_size (u_int8_t bgpa_type, const u_char *pptr, int len) 1246 { 1247 const u_char *tptr = pptr; 1248 1249 /* 1250 * If the path attribute is the optional AS4 path type, then we already 1251 * know, that ASs must be encoded in 4 byte format. 1252 */ 1253 if (bgpa_type == BGPTYPE_AS4_PATH) { 1254 return 4; 1255 } 1256 1257 /* 1258 * Let us assume that ASs are of 2 bytes in size, and check if the AS-Path 1259 * TLV is good. If not, ask the caller to try with AS encoded as 4 bytes 1260 * each. 1261 */ 1262 while (tptr < pptr + len) { 1263 TCHECK(tptr[0]); 1264 1265 /* 1266 * If we do not find a valid segment type, our guess might be wrong. 1267 */ 1268 if (tptr[0] < BGP_AS_SEG_TYPE_MIN || tptr[0] > BGP_AS_SEG_TYPE_MAX) { 1269 goto trunc; 1270 } 1271 TCHECK(tptr[1]); 1272 tptr += 2 + tptr[1] * 2; 1273 } 1274 1275 /* 1276 * If we correctly reached end of the AS path attribute data content, 1277 * then most likely ASs were indeed encoded as 2 bytes. 1278 */ 1279 if (tptr == pptr + len) { 1280 return 2; 1281 } 1282 1283 trunc: 1284 1285 /* 1286 * We can come here, either we did not have enough data, or if we 1287 * try to decode 4 byte ASs in 2 byte format. Either way, return 4, 1288 * so that calller can try to decode each AS as of 4 bytes. If indeed 1289 * there was not enough data, it will crib and end the parse anyways. 1290 */ 1291 return 4; 1292 } 1293 1294 static int 1295 bgp_attr_print(u_int atype, const u_char *pptr, u_int len) 1296 { 1297 int i; 1298 u_int16_t af; 1299 u_int8_t safi, snpa, nhlen; 1300 union { /* copy buffer for bandwidth values */ 1301 float f; 1302 u_int32_t i; 1303 } bw; 1304 int advance; 1305 u_int tlen; 1306 const u_char *tptr; 1307 char buf[MAXHOSTNAMELEN + 100]; 1308 char tokbuf[TOKBUFSIZE]; 1309 int as_size; 1310 1311 tptr = pptr; 1312 tlen=len; 1313 1314 switch (atype) { 1315 case BGPTYPE_ORIGIN: 1316 if (len != 1) 1317 printf("invalid len"); 1318 else { 1319 TCHECK(*tptr); 1320 printf("%s", tok2strbuf(bgp_origin_values, 1321 "Unknown Origin Typecode", 1322 tptr[0], 1323 tokbuf, sizeof(tokbuf))); 1324 } 1325 break; 1326 1327 1328 /* 1329 * Process AS4 byte path and AS2 byte path attributes here. 1330 */ 1331 case BGPTYPE_AS4_PATH: 1332 case BGPTYPE_AS_PATH: 1333 if (len % 2) { 1334 printf("invalid len"); 1335 break; 1336 } 1337 if (!len) { 1338 printf("empty"); 1339 break; 1340 } 1341 1342 /* 1343 * BGP updates exchanged between New speakers that support 4 1344 * byte AS, ASs are always encoded in 4 bytes. There is no 1345 * definitive way to find this, just by the packet's 1346 * contents. So, check for packet's TLV's sanity assuming 1347 * 2 bytes first, and it does not pass, assume that ASs are 1348 * encoded in 4 bytes format and move on. 1349 */ 1350 as_size = bgp_attr_get_as_size(atype, pptr, len); 1351 1352 while (tptr < pptr + len) { 1353 TCHECK(tptr[0]); 1354 printf("%s", tok2strbuf(bgp_as_path_segment_open_values, 1355 "?", tptr[0], 1356 tokbuf, sizeof(tokbuf))); 1357 for (i = 0; i < tptr[1] * as_size; i += as_size) { 1358 TCHECK2(tptr[2 + i], as_size); 1359 printf("%s ", 1360 as_printf(astostr, sizeof(astostr), 1361 as_size == 2 ? 1362 EXTRACT_16BITS(&tptr[2 + i]) : 1363 EXTRACT_32BITS(&tptr[2 + i]))); 1364 } 1365 TCHECK(tptr[0]); 1366 printf("%s", tok2strbuf(bgp_as_path_segment_close_values, 1367 "?", tptr[0], 1368 tokbuf, sizeof(tokbuf))); 1369 TCHECK(tptr[1]); 1370 tptr += 2 + tptr[1] * as_size; 1371 } 1372 break; 1373 case BGPTYPE_NEXT_HOP: 1374 if (len != 4) 1375 printf("invalid len"); 1376 else { 1377 TCHECK2(tptr[0], 4); 1378 printf("%s", getname(tptr)); 1379 } 1380 break; 1381 case BGPTYPE_MULTI_EXIT_DISC: 1382 case BGPTYPE_LOCAL_PREF: 1383 if (len != 4) 1384 printf("invalid len"); 1385 else { 1386 TCHECK2(tptr[0], 4); 1387 printf("%u", EXTRACT_32BITS(tptr)); 1388 } 1389 break; 1390 case BGPTYPE_ATOMIC_AGGREGATE: 1391 if (len != 0) 1392 printf("invalid len"); 1393 break; 1394 case BGPTYPE_AGGREGATOR: 1395 1396 /* 1397 * Depending on the AS encoded is of 2 bytes or of 4 bytes, 1398 * the length of this PA can be either 6 bytes or 8 bytes. 1399 */ 1400 if (len != 6 && len != 8) { 1401 printf("invalid len"); 1402 break; 1403 } 1404 TCHECK2(tptr[0], len); 1405 if (len == 6) { 1406 printf(" AS #%s, origin %s", 1407 as_printf(astostr, sizeof(astostr), EXTRACT_16BITS(tptr)), 1408 getname(tptr + 2)); 1409 } else { 1410 printf(" AS #%s, origin %s", 1411 as_printf(astostr, sizeof(astostr), 1412 EXTRACT_32BITS(tptr)), getname(tptr + 4)); 1413 } 1414 break; 1415 case BGPTYPE_AGGREGATOR4: 1416 if (len != 8) { 1417 printf("invalid len"); 1418 break; 1419 } 1420 TCHECK2(tptr[0], 8); 1421 printf(" AS #%s, origin %s", 1422 as_printf(astostr, sizeof(astostr), EXTRACT_32BITS(tptr)), 1423 getname(tptr + 4)); 1424 break; 1425 case BGPTYPE_COMMUNITIES: 1426 if (len % 4) { 1427 printf("invalid len"); 1428 break; 1429 } 1430 while (tlen>0) { 1431 u_int32_t comm; 1432 TCHECK2(tptr[0], 4); 1433 comm = EXTRACT_32BITS(tptr); 1434 switch (comm) { 1435 case BGP_COMMUNITY_NO_EXPORT: 1436 printf(" NO_EXPORT"); 1437 break; 1438 case BGP_COMMUNITY_NO_ADVERT: 1439 printf(" NO_ADVERTISE"); 1440 break; 1441 case BGP_COMMUNITY_NO_EXPORT_SUBCONFED: 1442 printf(" NO_EXPORT_SUBCONFED"); 1443 break; 1444 default: 1445 printf("%u:%u%s", 1446 (comm >> 16) & 0xffff, 1447 comm & 0xffff, 1448 (tlen>4) ? ", " : ""); 1449 break; 1450 } 1451 tlen -=4; 1452 tptr +=4; 1453 } 1454 break; 1455 case BGPTYPE_ORIGINATOR_ID: 1456 if (len != 4) { 1457 printf("invalid len"); 1458 break; 1459 } 1460 TCHECK2(tptr[0], 4); 1461 printf("%s",getname(tptr)); 1462 break; 1463 case BGPTYPE_CLUSTER_LIST: 1464 if (len % 4) { 1465 printf("invalid len"); 1466 break; 1467 } 1468 while (tlen>0) { 1469 TCHECK2(tptr[0], 4); 1470 printf("%s%s", 1471 getname(tptr), 1472 (tlen>4) ? ", " : ""); 1473 tlen -=4; 1474 tptr +=4; 1475 } 1476 break; 1477 case BGPTYPE_MP_REACH_NLRI: 1478 TCHECK2(tptr[0], 3); 1479 af = EXTRACT_16BITS(tptr); 1480 safi = tptr[2]; 1481 1482 printf("\n\t AFI: %s (%u), %sSAFI: %s (%u)", 1483 tok2strbuf(af_values, "Unknown AFI", af, 1484 tokbuf, sizeof(tokbuf)), 1485 af, 1486 (safi>128) ? "vendor specific " : "", /* 128 is meanwhile wellknown */ 1487 tok2strbuf(bgp_safi_values, "Unknown SAFI", safi, 1488 tokbuf, sizeof(tokbuf)), 1489 safi); 1490 1491 switch(af<<8 | safi) { 1492 case (AFNUM_INET<<8 | SAFNUM_UNICAST): 1493 case (AFNUM_INET<<8 | SAFNUM_MULTICAST): 1494 case (AFNUM_INET<<8 | SAFNUM_UNIMULTICAST): 1495 case (AFNUM_INET<<8 | SAFNUM_LABUNICAST): 1496 case (AFNUM_INET<<8 | SAFNUM_RT_ROUTING_INFO): 1497 case (AFNUM_INET<<8 | SAFNUM_VPNUNICAST): 1498 case (AFNUM_INET<<8 | SAFNUM_VPNMULTICAST): 1499 case (AFNUM_INET<<8 | SAFNUM_VPNUNIMULTICAST): 1500 case (AFNUM_INET<<8 | SAFNUM_MULTICAST_VPN): 1501 case (AFNUM_INET<<8 | SAFNUM_MDT): 1502 #ifdef INET6 1503 case (AFNUM_INET6<<8 | SAFNUM_UNICAST): 1504 case (AFNUM_INET6<<8 | SAFNUM_MULTICAST): 1505 case (AFNUM_INET6<<8 | SAFNUM_UNIMULTICAST): 1506 case (AFNUM_INET6<<8 | SAFNUM_LABUNICAST): 1507 case (AFNUM_INET6<<8 | SAFNUM_VPNUNICAST): 1508 case (AFNUM_INET6<<8 | SAFNUM_VPNMULTICAST): 1509 case (AFNUM_INET6<<8 | SAFNUM_VPNUNIMULTICAST): 1510 #endif 1511 case (AFNUM_NSAP<<8 | SAFNUM_UNICAST): 1512 case (AFNUM_NSAP<<8 | SAFNUM_MULTICAST): 1513 case (AFNUM_NSAP<<8 | SAFNUM_UNIMULTICAST): 1514 case (AFNUM_NSAP<<8 | SAFNUM_VPNUNICAST): 1515 case (AFNUM_NSAP<<8 | SAFNUM_VPNMULTICAST): 1516 case (AFNUM_NSAP<<8 | SAFNUM_VPNUNIMULTICAST): 1517 case (AFNUM_L2VPN<<8 | SAFNUM_VPNUNICAST): 1518 case (AFNUM_L2VPN<<8 | SAFNUM_VPNMULTICAST): 1519 case (AFNUM_L2VPN<<8 | SAFNUM_VPNUNIMULTICAST): 1520 case (AFNUM_VPLS<<8 | SAFNUM_VPLS): 1521 break; 1522 default: 1523 TCHECK2(tptr[0], tlen); 1524 printf("\n\t no AFI %u / SAFI %u decoder",af,safi); 1525 if (vflag <= 1) 1526 print_unknown_data(tptr,"\n\t ",tlen); 1527 goto done; 1528 break; 1529 } 1530 1531 tptr +=3; 1532 1533 TCHECK(tptr[0]); 1534 nhlen = tptr[0]; 1535 tlen = nhlen; 1536 tptr++; 1537 1538 if (tlen) { 1539 printf("\n\t nexthop: "); 1540 while (tlen > 0) { 1541 switch(af<<8 | safi) { 1542 case (AFNUM_INET<<8 | SAFNUM_UNICAST): 1543 case (AFNUM_INET<<8 | SAFNUM_MULTICAST): 1544 case (AFNUM_INET<<8 | SAFNUM_UNIMULTICAST): 1545 case (AFNUM_INET<<8 | SAFNUM_LABUNICAST): 1546 case (AFNUM_INET<<8 | SAFNUM_RT_ROUTING_INFO): 1547 case (AFNUM_INET<<8 | SAFNUM_MULTICAST_VPN): 1548 case (AFNUM_INET<<8 | SAFNUM_MDT): 1549 if (tlen < (int)sizeof(struct in_addr)) { 1550 printf("invalid len"); 1551 tlen = 0; 1552 } else { 1553 TCHECK2(tptr[0], sizeof(struct in_addr)); 1554 printf("%s",getname(tptr)); 1555 tlen -= sizeof(struct in_addr); 1556 tptr += sizeof(struct in_addr); 1557 } 1558 break; 1559 case (AFNUM_INET<<8 | SAFNUM_VPNUNICAST): 1560 case (AFNUM_INET<<8 | SAFNUM_VPNMULTICAST): 1561 case (AFNUM_INET<<8 | SAFNUM_VPNUNIMULTICAST): 1562 if (tlen < (int)(sizeof(struct in_addr)+BGP_VPN_RD_LEN)) { 1563 printf("invalid len"); 1564 tlen = 0; 1565 } else { 1566 TCHECK2(tptr[0], sizeof(struct in_addr)+BGP_VPN_RD_LEN); 1567 printf("RD: %s, %s", 1568 bgp_vpn_rd_print(tptr), 1569 getname(tptr+BGP_VPN_RD_LEN)); 1570 tlen -= (sizeof(struct in_addr)+BGP_VPN_RD_LEN); 1571 tptr += (sizeof(struct in_addr)+BGP_VPN_RD_LEN); 1572 } 1573 break; 1574 #ifdef INET6 1575 case (AFNUM_INET6<<8 | SAFNUM_UNICAST): 1576 case (AFNUM_INET6<<8 | SAFNUM_MULTICAST): 1577 case (AFNUM_INET6<<8 | SAFNUM_UNIMULTICAST): 1578 case (AFNUM_INET6<<8 | SAFNUM_LABUNICAST): 1579 if (tlen < (int)sizeof(struct in6_addr)) { 1580 printf("invalid len"); 1581 tlen = 0; 1582 } else { 1583 TCHECK2(tptr[0], sizeof(struct in6_addr)); 1584 printf("%s", getname6(tptr)); 1585 tlen -= sizeof(struct in6_addr); 1586 tptr += sizeof(struct in6_addr); 1587 } 1588 break; 1589 case (AFNUM_INET6<<8 | SAFNUM_VPNUNICAST): 1590 case (AFNUM_INET6<<8 | SAFNUM_VPNMULTICAST): 1591 case (AFNUM_INET6<<8 | SAFNUM_VPNUNIMULTICAST): 1592 if (tlen < (int)(sizeof(struct in6_addr)+BGP_VPN_RD_LEN)) { 1593 printf("invalid len"); 1594 tlen = 0; 1595 } else { 1596 TCHECK2(tptr[0], sizeof(struct in6_addr)+BGP_VPN_RD_LEN); 1597 printf("RD: %s, %s", 1598 bgp_vpn_rd_print(tptr), 1599 getname6(tptr+BGP_VPN_RD_LEN)); 1600 tlen -= (sizeof(struct in6_addr)+BGP_VPN_RD_LEN); 1601 tptr += (sizeof(struct in6_addr)+BGP_VPN_RD_LEN); 1602 } 1603 break; 1604 #endif 1605 case (AFNUM_VPLS<<8 | SAFNUM_VPLS): 1606 case (AFNUM_L2VPN<<8 | SAFNUM_VPNUNICAST): 1607 case (AFNUM_L2VPN<<8 | SAFNUM_VPNMULTICAST): 1608 case (AFNUM_L2VPN<<8 | SAFNUM_VPNUNIMULTICAST): 1609 if (tlen < (int)sizeof(struct in_addr)) { 1610 printf("invalid len"); 1611 tlen = 0; 1612 } else { 1613 TCHECK2(tptr[0], sizeof(struct in_addr)); 1614 printf("%s", getname(tptr)); 1615 tlen -= (sizeof(struct in_addr)); 1616 tptr += (sizeof(struct in_addr)); 1617 } 1618 break; 1619 case (AFNUM_NSAP<<8 | SAFNUM_UNICAST): 1620 case (AFNUM_NSAP<<8 | SAFNUM_MULTICAST): 1621 case (AFNUM_NSAP<<8 | SAFNUM_UNIMULTICAST): 1622 TCHECK2(tptr[0], tlen); 1623 printf("%s",isonsap_string(tptr,tlen)); 1624 tptr += tlen; 1625 tlen = 0; 1626 break; 1627 1628 case (AFNUM_NSAP<<8 | SAFNUM_VPNUNICAST): 1629 case (AFNUM_NSAP<<8 | SAFNUM_VPNMULTICAST): 1630 case (AFNUM_NSAP<<8 | SAFNUM_VPNUNIMULTICAST): 1631 if (tlen < BGP_VPN_RD_LEN+1) { 1632 printf("invalid len"); 1633 tlen = 0; 1634 } else { 1635 TCHECK2(tptr[0], tlen); 1636 printf("RD: %s, %s", 1637 bgp_vpn_rd_print(tptr), 1638 isonsap_string(tptr+BGP_VPN_RD_LEN,tlen-BGP_VPN_RD_LEN)); 1639 /* rfc986 mapped IPv4 address ? */ 1640 if (EXTRACT_32BITS(tptr+BGP_VPN_RD_LEN) == 0x47000601) 1641 printf(" = %s", getname(tptr+BGP_VPN_RD_LEN+4)); 1642 #ifdef INET6 1643 /* rfc1888 mapped IPv6 address ? */ 1644 else if (EXTRACT_24BITS(tptr+BGP_VPN_RD_LEN) == 0x350000) 1645 printf(" = %s", getname6(tptr+BGP_VPN_RD_LEN+3)); 1646 #endif 1647 tptr += tlen; 1648 tlen = 0; 1649 } 1650 break; 1651 default: 1652 TCHECK2(tptr[0], tlen); 1653 printf("no AFI %u/SAFI %u decoder",af,safi); 1654 if (vflag <= 1) 1655 print_unknown_data(tptr,"\n\t ",tlen); 1656 tptr += tlen; 1657 tlen = 0; 1658 goto done; 1659 break; 1660 } 1661 } 1662 } 1663 printf(", nh-length: %u", nhlen); 1664 tptr += tlen; 1665 1666 TCHECK(tptr[0]); 1667 snpa = tptr[0]; 1668 tptr++; 1669 1670 if (snpa) { 1671 printf("\n\t %u SNPA", snpa); 1672 for (/*nothing*/; snpa > 0; snpa--) { 1673 TCHECK(tptr[0]); 1674 printf("\n\t %d bytes", tptr[0]); 1675 tptr += tptr[0] + 1; 1676 } 1677 } else { 1678 printf(", no SNPA"); 1679 } 1680 1681 while (len - (tptr - pptr) > 0) { 1682 switch (af<<8 | safi) { 1683 case (AFNUM_INET<<8 | SAFNUM_UNICAST): 1684 case (AFNUM_INET<<8 | SAFNUM_MULTICAST): 1685 case (AFNUM_INET<<8 | SAFNUM_UNIMULTICAST): 1686 advance = decode_prefix4(tptr, len, buf, sizeof(buf)); 1687 if (advance == -1) 1688 printf("\n\t (illegal prefix length)"); 1689 else if (advance == -2) 1690 goto trunc; 1691 else if (advance == -3) 1692 break; /* bytes left, but not enough */ 1693 else 1694 printf("\n\t %s", buf); 1695 break; 1696 case (AFNUM_INET<<8 | SAFNUM_LABUNICAST): 1697 advance = decode_labeled_prefix4(tptr, len, buf, sizeof(buf)); 1698 if (advance == -1) 1699 printf("\n\t (illegal prefix length)"); 1700 else if (advance == -2) 1701 goto trunc; 1702 else if (advance == -3) 1703 break; /* bytes left, but not enough */ 1704 else 1705 printf("\n\t %s", buf); 1706 break; 1707 case (AFNUM_INET<<8 | SAFNUM_VPNUNICAST): 1708 case (AFNUM_INET<<8 | SAFNUM_VPNMULTICAST): 1709 case (AFNUM_INET<<8 | SAFNUM_VPNUNIMULTICAST): 1710 advance = decode_labeled_vpn_prefix4(tptr, buf, sizeof(buf)); 1711 if (advance == -1) 1712 printf("\n\t (illegal prefix length)"); 1713 else if (advance == -2) 1714 goto trunc; 1715 else 1716 printf("\n\t %s", buf); 1717 break; 1718 case (AFNUM_INET<<8 | SAFNUM_RT_ROUTING_INFO): 1719 advance = decode_rt_routing_info(tptr, buf, sizeof(buf)); 1720 if (advance == -1) 1721 printf("\n\t (illegal prefix length)"); 1722 else if (advance == -2) 1723 goto trunc; 1724 else 1725 printf("\n\t %s", buf); 1726 break; 1727 case (AFNUM_INET<<8 | SAFNUM_MULTICAST_VPN): /* fall through */ 1728 case (AFNUM_INET6<<8 | SAFNUM_MULTICAST_VPN): 1729 advance = decode_multicast_vpn(tptr, buf, sizeof(buf)); 1730 if (advance == -1) 1731 printf("\n\t (illegal prefix length)"); 1732 else if (advance == -2) 1733 goto trunc; 1734 else 1735 printf("\n\t %s", buf); 1736 break; 1737 1738 case (AFNUM_INET<<8 | SAFNUM_MDT): 1739 advance = decode_mdt_vpn_nlri(tptr, buf, sizeof(buf)); 1740 if (advance == -1) 1741 printf("\n\t (illegal prefix length)"); 1742 else if (advance == -2) 1743 goto trunc; 1744 else 1745 printf("\n\t %s", buf); 1746 break; 1747 #ifdef INET6 1748 case (AFNUM_INET6<<8 | SAFNUM_UNICAST): 1749 case (AFNUM_INET6<<8 | SAFNUM_MULTICAST): 1750 case (AFNUM_INET6<<8 | SAFNUM_UNIMULTICAST): 1751 advance = decode_prefix6(tptr, len, buf, sizeof(buf)); 1752 if (advance == -1) 1753 printf("\n\t (illegal prefix length)"); 1754 else if (advance == -2) 1755 goto trunc; 1756 else if (advance == -3) 1757 break; /* bytes left, but not enough */ 1758 else 1759 printf("\n\t %s", buf); 1760 break; 1761 case (AFNUM_INET6<<8 | SAFNUM_LABUNICAST): 1762 advance = decode_labeled_prefix6(tptr, len, buf, sizeof(buf)); 1763 if (advance == -1) 1764 printf("\n\t (illegal prefix length)"); 1765 else if (advance == -2) 1766 goto trunc; 1767 else if (advance == -3) 1768 break; /* bytes left, but not enough */ 1769 else 1770 printf("\n\t %s", buf); 1771 break; 1772 case (AFNUM_INET6<<8 | SAFNUM_VPNUNICAST): 1773 case (AFNUM_INET6<<8 | SAFNUM_VPNMULTICAST): 1774 case (AFNUM_INET6<<8 | SAFNUM_VPNUNIMULTICAST): 1775 advance = decode_labeled_vpn_prefix6(tptr, buf, sizeof(buf)); 1776 if (advance == -1) 1777 printf("\n\t (illegal prefix length)"); 1778 else if (advance == -2) 1779 goto trunc; 1780 else 1781 printf("\n\t %s", buf); 1782 break; 1783 #endif 1784 case (AFNUM_VPLS<<8 | SAFNUM_VPLS): 1785 case (AFNUM_L2VPN<<8 | SAFNUM_VPNUNICAST): 1786 case (AFNUM_L2VPN<<8 | SAFNUM_VPNMULTICAST): 1787 case (AFNUM_L2VPN<<8 | SAFNUM_VPNUNIMULTICAST): 1788 advance = decode_labeled_vpn_l2(tptr, buf, sizeof(buf)); 1789 if (advance == -1) 1790 printf("\n\t (illegal length)"); 1791 else if (advance == -2) 1792 goto trunc; 1793 else 1794 printf("\n\t %s", buf); 1795 break; 1796 case (AFNUM_NSAP<<8 | SAFNUM_UNICAST): 1797 case (AFNUM_NSAP<<8 | SAFNUM_MULTICAST): 1798 case (AFNUM_NSAP<<8 | SAFNUM_UNIMULTICAST): 1799 advance = decode_clnp_prefix(tptr, buf, sizeof(buf)); 1800 if (advance == -1) 1801 printf("\n\t (illegal prefix length)"); 1802 else if (advance == -2) 1803 goto trunc; 1804 else 1805 printf("\n\t %s", buf); 1806 break; 1807 case (AFNUM_NSAP<<8 | SAFNUM_VPNUNICAST): 1808 case (AFNUM_NSAP<<8 | SAFNUM_VPNMULTICAST): 1809 case (AFNUM_NSAP<<8 | SAFNUM_VPNUNIMULTICAST): 1810 advance = decode_labeled_vpn_clnp_prefix(tptr, buf, sizeof(buf)); 1811 if (advance == -1) 1812 printf("\n\t (illegal prefix length)"); 1813 else if (advance == -2) 1814 goto trunc; 1815 else 1816 printf("\n\t %s", buf); 1817 break; 1818 default: 1819 TCHECK2(*tptr,tlen); 1820 printf("\n\t no AFI %u / SAFI %u decoder",af,safi); 1821 if (vflag <= 1) 1822 print_unknown_data(tptr,"\n\t ",tlen); 1823 advance = 0; 1824 tptr = pptr + len; 1825 break; 1826 } 1827 if (advance < 0) 1828 break; 1829 tptr += advance; 1830 } 1831 done: 1832 break; 1833 1834 case BGPTYPE_MP_UNREACH_NLRI: 1835 TCHECK2(tptr[0], BGP_MP_NLRI_MINSIZE); 1836 af = EXTRACT_16BITS(tptr); 1837 safi = tptr[2]; 1838 1839 printf("\n\t AFI: %s (%u), %sSAFI: %s (%u)", 1840 tok2strbuf(af_values, "Unknown AFI", af, 1841 tokbuf, sizeof(tokbuf)), 1842 af, 1843 (safi>128) ? "vendor specific " : "", /* 128 is meanwhile wellknown */ 1844 tok2strbuf(bgp_safi_values, "Unknown SAFI", safi, 1845 tokbuf, sizeof(tokbuf)), 1846 safi); 1847 1848 if (len == BGP_MP_NLRI_MINSIZE) 1849 printf("\n\t End-of-Rib Marker (empty NLRI)"); 1850 1851 tptr += 3; 1852 1853 while (len - (tptr - pptr) > 0) { 1854 switch (af<<8 | safi) { 1855 case (AFNUM_INET<<8 | SAFNUM_UNICAST): 1856 case (AFNUM_INET<<8 | SAFNUM_MULTICAST): 1857 case (AFNUM_INET<<8 | SAFNUM_UNIMULTICAST): 1858 advance = decode_prefix4(tptr, len, buf, sizeof(buf)); 1859 if (advance == -1) 1860 printf("\n\t (illegal prefix length)"); 1861 else if (advance == -2) 1862 goto trunc; 1863 else if (advance == -3) 1864 break; /* bytes left, but not enough */ 1865 else 1866 printf("\n\t %s", buf); 1867 break; 1868 case (AFNUM_INET<<8 | SAFNUM_LABUNICAST): 1869 advance = decode_labeled_prefix4(tptr, len, buf, sizeof(buf)); 1870 if (advance == -1) 1871 printf("\n\t (illegal prefix length)"); 1872 else if (advance == -2) 1873 goto trunc; 1874 else if (advance == -3) 1875 break; /* bytes left, but not enough */ 1876 else 1877 printf("\n\t %s", buf); 1878 break; 1879 case (AFNUM_INET<<8 | SAFNUM_VPNUNICAST): 1880 case (AFNUM_INET<<8 | SAFNUM_VPNMULTICAST): 1881 case (AFNUM_INET<<8 | SAFNUM_VPNUNIMULTICAST): 1882 advance = decode_labeled_vpn_prefix4(tptr, buf, sizeof(buf)); 1883 if (advance == -1) 1884 printf("\n\t (illegal prefix length)"); 1885 else if (advance == -2) 1886 goto trunc; 1887 else 1888 printf("\n\t %s", buf); 1889 break; 1890 #ifdef INET6 1891 case (AFNUM_INET6<<8 | SAFNUM_UNICAST): 1892 case (AFNUM_INET6<<8 | SAFNUM_MULTICAST): 1893 case (AFNUM_INET6<<8 | SAFNUM_UNIMULTICAST): 1894 advance = decode_prefix6(tptr, len, buf, sizeof(buf)); 1895 if (advance == -1) 1896 printf("\n\t (illegal prefix length)"); 1897 else if (advance == -2) 1898 goto trunc; 1899 else if (advance == -3) 1900 break; /* bytes left, but not enough */ 1901 else 1902 printf("\n\t %s", buf); 1903 break; 1904 case (AFNUM_INET6<<8 | SAFNUM_LABUNICAST): 1905 advance = decode_labeled_prefix6(tptr, len, buf, sizeof(buf)); 1906 if (advance == -1) 1907 printf("\n\t (illegal prefix length)"); 1908 else if (advance == -2) 1909 goto trunc; 1910 else if (advance == -3) 1911 break; /* bytes left, but not enough */ 1912 else 1913 printf("\n\t %s", buf); 1914 break; 1915 case (AFNUM_INET6<<8 | SAFNUM_VPNUNICAST): 1916 case (AFNUM_INET6<<8 | SAFNUM_VPNMULTICAST): 1917 case (AFNUM_INET6<<8 | SAFNUM_VPNUNIMULTICAST): 1918 advance = decode_labeled_vpn_prefix6(tptr, buf, sizeof(buf)); 1919 if (advance == -1) 1920 printf("\n\t (illegal prefix length)"); 1921 else if (advance == -2) 1922 goto trunc; 1923 else 1924 printf("\n\t %s", buf); 1925 break; 1926 #endif 1927 case (AFNUM_VPLS<<8 | SAFNUM_VPLS): 1928 case (AFNUM_L2VPN<<8 | SAFNUM_VPNUNICAST): 1929 case (AFNUM_L2VPN<<8 | SAFNUM_VPNMULTICAST): 1930 case (AFNUM_L2VPN<<8 | SAFNUM_VPNUNIMULTICAST): 1931 advance = decode_labeled_vpn_l2(tptr, buf, sizeof(buf)); 1932 if (advance == -1) 1933 printf("\n\t (illegal length)"); 1934 else if (advance == -2) 1935 goto trunc; 1936 else 1937 printf("\n\t %s", buf); 1938 break; 1939 case (AFNUM_NSAP<<8 | SAFNUM_UNICAST): 1940 case (AFNUM_NSAP<<8 | SAFNUM_MULTICAST): 1941 case (AFNUM_NSAP<<8 | SAFNUM_UNIMULTICAST): 1942 advance = decode_clnp_prefix(tptr, buf, sizeof(buf)); 1943 if (advance == -1) 1944 printf("\n\t (illegal prefix length)"); 1945 else if (advance == -2) 1946 goto trunc; 1947 else 1948 printf("\n\t %s", buf); 1949 break; 1950 case (AFNUM_NSAP<<8 | SAFNUM_VPNUNICAST): 1951 case (AFNUM_NSAP<<8 | SAFNUM_VPNMULTICAST): 1952 case (AFNUM_NSAP<<8 | SAFNUM_VPNUNIMULTICAST): 1953 advance = decode_labeled_vpn_clnp_prefix(tptr, buf, sizeof(buf)); 1954 if (advance == -1) 1955 printf("\n\t (illegal prefix length)"); 1956 else if (advance == -2) 1957 goto trunc; 1958 else 1959 printf("\n\t %s", buf); 1960 break; 1961 case (AFNUM_INET<<8 | SAFNUM_MDT): 1962 advance = decode_mdt_vpn_nlri(tptr, buf, sizeof(buf)); 1963 if (advance == -1) 1964 printf("\n\t (illegal prefix length)"); 1965 else if (advance == -2) 1966 goto trunc; 1967 else 1968 printf("\n\t %s", buf); 1969 break; 1970 case (AFNUM_INET<<8 | SAFNUM_MULTICAST_VPN): /* fall through */ 1971 case (AFNUM_INET6<<8 | SAFNUM_MULTICAST_VPN): 1972 advance = decode_multicast_vpn(tptr, buf, sizeof(buf)); 1973 if (advance == -1) 1974 printf("\n\t (illegal prefix length)"); 1975 else if (advance == -2) 1976 goto trunc; 1977 else 1978 printf("\n\t %s", buf); 1979 break; 1980 default: 1981 TCHECK2(*(tptr-3),tlen); 1982 printf("no AFI %u / SAFI %u decoder",af,safi); 1983 if (vflag <= 1) 1984 print_unknown_data(tptr-3,"\n\t ",tlen); 1985 advance = 0; 1986 tptr = pptr + len; 1987 break; 1988 } 1989 if (advance < 0) 1990 break; 1991 tptr += advance; 1992 } 1993 break; 1994 case BGPTYPE_EXTD_COMMUNITIES: 1995 if (len % 8) { 1996 printf("invalid len"); 1997 break; 1998 } 1999 while (tlen>0) { 2000 u_int16_t extd_comm; 2001 2002 TCHECK2(tptr[0], 2); 2003 extd_comm=EXTRACT_16BITS(tptr); 2004 2005 printf("\n\t %s (0x%04x), Flags [%s]", 2006 tok2strbuf(bgp_extd_comm_subtype_values, 2007 "unknown extd community typecode", 2008 extd_comm, tokbuf, sizeof(tokbuf)), 2009 extd_comm, 2010 bittok2str(bgp_extd_comm_flag_values, "none", extd_comm)); 2011 2012 TCHECK2(*(tptr+2), 6); 2013 switch(extd_comm) { 2014 case BGP_EXT_COM_RT_0: 2015 case BGP_EXT_COM_RO_0: 2016 case BGP_EXT_COM_L2VPN_RT_0: 2017 printf(": %u:%u (= %s)", 2018 EXTRACT_16BITS(tptr+2), 2019 EXTRACT_32BITS(tptr+4), 2020 getname(tptr+4)); 2021 break; 2022 case BGP_EXT_COM_RT_1: 2023 case BGP_EXT_COM_RO_1: 2024 case BGP_EXT_COM_L2VPN_RT_1: 2025 case BGP_EXT_COM_VRF_RT_IMP: 2026 printf(": %s:%u", 2027 getname(tptr+2), 2028 EXTRACT_16BITS(tptr+6)); 2029 break; 2030 case BGP_EXT_COM_RT_2: 2031 case BGP_EXT_COM_RO_2: 2032 printf(": %s:%u", 2033 as_printf(astostr, sizeof(astostr), 2034 EXTRACT_32BITS(tptr+2)), EXTRACT_16BITS(tptr+6)); 2035 break; 2036 case BGP_EXT_COM_LINKBAND: 2037 bw.i = EXTRACT_32BITS(tptr+2); 2038 printf(": bandwidth: %.3f Mbps", 2039 bw.f*8/1000000); 2040 break; 2041 case BGP_EXT_COM_VPN_ORIGIN: 2042 case BGP_EXT_COM_VPN_ORIGIN2: 2043 case BGP_EXT_COM_VPN_ORIGIN3: 2044 case BGP_EXT_COM_VPN_ORIGIN4: 2045 case BGP_EXT_COM_OSPF_RID: 2046 case BGP_EXT_COM_OSPF_RID2: 2047 printf("%s", getname(tptr+2)); 2048 break; 2049 case BGP_EXT_COM_OSPF_RTYPE: 2050 case BGP_EXT_COM_OSPF_RTYPE2: 2051 printf(": area:%s, router-type:%s, metric-type:%s%s", 2052 getname(tptr+2), 2053 tok2strbuf(bgp_extd_comm_ospf_rtype_values, 2054 "unknown (0x%02x)", 2055 *(tptr+6), 2056 tokbuf, sizeof(tokbuf)), 2057 (*(tptr+7) & BGP_OSPF_RTYPE_METRIC_TYPE) ? "E2" : "", 2058 ((*(tptr+6) == BGP_OSPF_RTYPE_EXT) || (*(tptr+6) == BGP_OSPF_RTYPE_NSSA)) ? "E1" : ""); 2059 break; 2060 case BGP_EXT_COM_L2INFO: 2061 printf(": %s Control Flags [0x%02x]:MTU %u", 2062 tok2strbuf(l2vpn_encaps_values, 2063 "unknown encaps", 2064 *(tptr+2), 2065 tokbuf, sizeof(tokbuf)), 2066 *(tptr+3), 2067 EXTRACT_16BITS(tptr+4)); 2068 break; 2069 case BGP_EXT_COM_SOURCE_AS: 2070 printf(": AS %u", EXTRACT_16BITS(tptr+2)); 2071 break; 2072 default: 2073 TCHECK2(*tptr,8); 2074 print_unknown_data(tptr,"\n\t ",8); 2075 break; 2076 } 2077 tlen -=8; 2078 tptr +=8; 2079 } 2080 break; 2081 2082 case BGPTYPE_PMSI_TUNNEL: 2083 { 2084 u_int8_t tunnel_type, flags; 2085 2086 tunnel_type = *(tptr+1); 2087 flags = *tptr; 2088 tlen = len; 2089 2090 TCHECK2(tptr[0], 5); 2091 printf("\n\t Tunnel-type %s (%u), Flags [%s], MPLS Label %u", 2092 tok2str(bgp_pmsi_tunnel_values, "Unknown", tunnel_type), 2093 tunnel_type, 2094 bittok2str(bgp_pmsi_flag_values, "none", flags), 2095 EXTRACT_24BITS(tptr+2)>>4); 2096 2097 tptr +=5; 2098 tlen -= 5; 2099 2100 switch (tunnel_type) { 2101 case BGP_PMSI_TUNNEL_PIM_SM: /* fall through */ 2102 case BGP_PMSI_TUNNEL_PIM_BIDIR: 2103 TCHECK2(tptr[0], 8); 2104 printf("\n\t Sender %s, P-Group %s", 2105 ipaddr_string(tptr), 2106 ipaddr_string(tptr+4)); 2107 break; 2108 2109 case BGP_PMSI_TUNNEL_PIM_SSM: 2110 TCHECK2(tptr[0], 8); 2111 printf("\n\t Root-Node %s, P-Group %s", 2112 ipaddr_string(tptr), 2113 ipaddr_string(tptr+4)); 2114 break; 2115 case BGP_PMSI_TUNNEL_INGRESS: 2116 TCHECK2(tptr[0], 4); 2117 printf("\n\t Tunnel-Endpoint %s", 2118 ipaddr_string(tptr)); 2119 break; 2120 case BGP_PMSI_TUNNEL_LDP_P2MP: /* fall through */ 2121 case BGP_PMSI_TUNNEL_LDP_MP2MP: 2122 TCHECK2(tptr[0], 8); 2123 printf("\n\t Root-Node %s, LSP-ID 0x%08x", 2124 ipaddr_string(tptr), 2125 EXTRACT_32BITS(tptr+4)); 2126 break; 2127 case BGP_PMSI_TUNNEL_RSVP_P2MP: 2128 TCHECK2(tptr[0], 8); 2129 printf("\n\t Extended-Tunnel-ID %s, P2MP-ID 0x%08x", 2130 ipaddr_string(tptr), 2131 EXTRACT_32BITS(tptr+4)); 2132 break; 2133 default: 2134 if (vflag <= 1) { 2135 print_unknown_data(tptr,"\n\t ",tlen); 2136 } 2137 } 2138 break; 2139 } 2140 case BGPTYPE_ATTR_SET: 2141 TCHECK2(tptr[0], 4); 2142 if (len < 4) 2143 goto trunc; 2144 printf("\n\t Origin AS: %s", 2145 as_printf(astostr, sizeof(astostr), EXTRACT_32BITS(tptr))); 2146 tptr+=4; 2147 len -=4; 2148 2149 while (len) { 2150 u_int aflags, atype, alenlen, alen; 2151 2152 TCHECK2(tptr[0], 2); 2153 if (len < 2) 2154 goto trunc; 2155 aflags = *tptr; 2156 atype = *(tptr + 1); 2157 tptr += 2; 2158 len -= 2; 2159 alenlen = bgp_attr_lenlen(aflags, tptr); 2160 TCHECK2(tptr[0], alenlen); 2161 if (len < alenlen) 2162 goto trunc; 2163 alen = bgp_attr_len(aflags, tptr); 2164 tptr += alenlen; 2165 len -= alenlen; 2166 2167 printf("\n\t %s (%u), length: %u", 2168 tok2strbuf(bgp_attr_values, 2169 "Unknown Attribute", atype, 2170 tokbuf, sizeof(tokbuf)), 2171 atype, 2172 alen); 2173 2174 if (aflags) { 2175 printf(", Flags [%s%s%s%s", 2176 aflags & 0x80 ? "O" : "", 2177 aflags & 0x40 ? "T" : "", 2178 aflags & 0x20 ? "P" : "", 2179 aflags & 0x10 ? "E" : ""); 2180 if (aflags & 0xf) 2181 printf("+%x", aflags & 0xf); 2182 printf("]: "); 2183 } 2184 /* FIXME check for recursion */ 2185 if (!bgp_attr_print(atype, tptr, alen)) 2186 return 0; 2187 tptr += alen; 2188 len -= alen; 2189 } 2190 break; 2191 2192 2193 default: 2194 TCHECK2(*pptr,len); 2195 printf("\n\t no Attribute %u decoder",atype); /* we have no decoder for the attribute */ 2196 if (vflag <= 1) 2197 print_unknown_data(pptr,"\n\t ",len); 2198 break; 2199 } 2200 if (vflag > 1 && len) { /* omit zero length attributes*/ 2201 TCHECK2(*pptr,len); 2202 print_unknown_data(pptr,"\n\t ",len); 2203 } 2204 return 1; 2205 2206 trunc: 2207 return 0; 2208 } 2209 2210 static void 2211 bgp_capabilities_print(const u_char *opt, int caps_len) 2212 { 2213 char tokbuf[TOKBUFSIZE]; 2214 char tokbuf2[TOKBUFSIZE]; 2215 int cap_type, cap_len, tcap_len, cap_offset; 2216 int i = 0; 2217 2218 while (i < caps_len) { 2219 TCHECK2(opt[i], BGP_CAP_HEADER_SIZE); 2220 cap_type=opt[i]; 2221 cap_len=opt[i+1]; 2222 tcap_len=cap_len; 2223 printf("\n\t %s (%u), length: %u", 2224 tok2strbuf(bgp_capcode_values, "Unknown", 2225 cap_type, tokbuf, sizeof(tokbuf)), 2226 cap_type, 2227 cap_len); 2228 TCHECK2(opt[i+2], cap_len); 2229 switch (cap_type) { 2230 case BGP_CAPCODE_MP: 2231 printf("\n\t\tAFI %s (%u), SAFI %s (%u)", 2232 tok2strbuf(af_values, "Unknown", 2233 EXTRACT_16BITS(opt+i+2), 2234 tokbuf, sizeof(tokbuf)), 2235 EXTRACT_16BITS(opt+i+2), 2236 tok2strbuf(bgp_safi_values, "Unknown", 2237 opt[i+5], 2238 tokbuf, sizeof(tokbuf)), 2239 opt[i+5]); 2240 break; 2241 case BGP_CAPCODE_RESTART: 2242 printf("\n\t\tRestart Flags: [%s], Restart Time %us", 2243 ((opt[i+2])&0x80) ? "R" : "none", 2244 EXTRACT_16BITS(opt+i+2)&0xfff); 2245 tcap_len-=2; 2246 cap_offset=4; 2247 while(tcap_len>=4) { 2248 printf("\n\t\t AFI %s (%u), SAFI %s (%u), Forwarding state preserved: %s", 2249 tok2strbuf(af_values,"Unknown", 2250 EXTRACT_16BITS(opt+i+cap_offset), 2251 tokbuf, sizeof(tokbuf)), 2252 EXTRACT_16BITS(opt+i+cap_offset), 2253 tok2strbuf(bgp_safi_values,"Unknown", 2254 opt[i+cap_offset+2], 2255 tokbuf2, sizeof(tokbuf2)), 2256 opt[i+cap_offset+2], 2257 ((opt[i+cap_offset+3])&0x80) ? "yes" : "no" ); 2258 tcap_len-=4; 2259 cap_offset+=4; 2260 } 2261 break; 2262 case BGP_CAPCODE_RR: 2263 case BGP_CAPCODE_RR_CISCO: 2264 break; 2265 case BGP_CAPCODE_AS_NEW: 2266 2267 /* 2268 * Extract the 4 byte AS number encoded. 2269 */ 2270 if (cap_len == 4) { 2271 printf("\n\t\t 4 Byte AS %s", 2272 as_printf(astostr, sizeof(astostr), 2273 EXTRACT_32BITS(opt + i + 2))); 2274 } 2275 break; 2276 default: 2277 printf("\n\t\tno decoder for Capability %u", 2278 cap_type); 2279 if (vflag <= 1) 2280 print_unknown_data(&opt[i+2],"\n\t\t",cap_len); 2281 break; 2282 } 2283 if (vflag > 1 && cap_len > 0) { 2284 print_unknown_data(&opt[i+2],"\n\t\t",cap_len); 2285 } 2286 i += BGP_CAP_HEADER_SIZE + cap_len; 2287 } 2288 return; 2289 2290 trunc: 2291 printf("[|BGP]"); 2292 } 2293 2294 static void 2295 bgp_open_print(const u_char *dat, int length) 2296 { 2297 struct bgp_open bgpo; 2298 struct bgp_opt bgpopt; 2299 const u_char *opt; 2300 int i; 2301 char tokbuf[TOKBUFSIZE]; 2302 2303 TCHECK2(dat[0], BGP_OPEN_SIZE); 2304 memcpy(&bgpo, dat, BGP_OPEN_SIZE); 2305 2306 printf("\n\t Version %d, ", bgpo.bgpo_version); 2307 printf("my AS %s, ", 2308 as_printf(astostr, sizeof(astostr), ntohs(bgpo.bgpo_myas))); 2309 printf("Holdtime %us, ", ntohs(bgpo.bgpo_holdtime)); 2310 printf("ID %s", getname((u_char *)&bgpo.bgpo_id)); 2311 printf("\n\t Optional parameters, length: %u", bgpo.bgpo_optlen); 2312 2313 /* some little sanity checking */ 2314 if (length < bgpo.bgpo_optlen+BGP_OPEN_SIZE) 2315 return; 2316 2317 /* ugly! */ 2318 opt = &((const struct bgp_open *)dat)->bgpo_optlen; 2319 opt++; 2320 2321 i = 0; 2322 while (i < bgpo.bgpo_optlen) { 2323 TCHECK2(opt[i], BGP_OPT_SIZE); 2324 memcpy(&bgpopt, &opt[i], BGP_OPT_SIZE); 2325 if (i + 2 + bgpopt.bgpopt_len > bgpo.bgpo_optlen) { 2326 printf("\n\t Option %d, length: %u", bgpopt.bgpopt_type, bgpopt.bgpopt_len); 2327 break; 2328 } 2329 2330 printf("\n\t Option %s (%u), length: %u", 2331 tok2strbuf(bgp_opt_values,"Unknown", 2332 bgpopt.bgpopt_type, 2333 tokbuf, sizeof(tokbuf)), 2334 bgpopt.bgpopt_type, 2335 bgpopt.bgpopt_len); 2336 2337 /* now let's decode the options we know*/ 2338 switch(bgpopt.bgpopt_type) { 2339 2340 case BGP_OPT_CAP: 2341 bgp_capabilities_print(&opt[i+BGP_OPT_SIZE], 2342 bgpopt.bgpopt_len); 2343 break; 2344 2345 case BGP_OPT_AUTH: 2346 default: 2347 printf("\n\t no decoder for option %u", 2348 bgpopt.bgpopt_type); 2349 break; 2350 } 2351 i += BGP_OPT_SIZE + bgpopt.bgpopt_len; 2352 } 2353 return; 2354 trunc: 2355 printf("[|BGP]"); 2356 } 2357 2358 static void 2359 bgp_update_print(const u_char *dat, int length) 2360 { 2361 struct bgp bgp; 2362 const u_char *p; 2363 int withdrawn_routes_len; 2364 int len; 2365 int i; 2366 char tokbuf[TOKBUFSIZE]; 2367 #ifndef INET6 2368 char buf[MAXHOSTNAMELEN + 100]; 2369 int wpfx; 2370 #endif 2371 2372 TCHECK2(dat[0], BGP_SIZE); 2373 if (length < BGP_SIZE) 2374 goto trunc; 2375 memcpy(&bgp, dat, BGP_SIZE); 2376 p = dat + BGP_SIZE; /*XXX*/ 2377 length -= BGP_SIZE; 2378 2379 /* Unfeasible routes */ 2380 TCHECK2(p[0], 2); 2381 if (length < 2) 2382 goto trunc; 2383 withdrawn_routes_len = EXTRACT_16BITS(p); 2384 p += 2; 2385 length -= 2; 2386 if (withdrawn_routes_len) { 2387 /* 2388 * Without keeping state from the original NLRI message, 2389 * it's not possible to tell if this a v4 or v6 route, 2390 * so only try to decode it if we're not v6 enabled. 2391 */ 2392 TCHECK2(p[0], withdrawn_routes_len); 2393 if (length < withdrawn_routes_len) 2394 goto trunc; 2395 #ifdef INET6 2396 printf("\n\t Withdrawn routes: %d bytes", withdrawn_routes_len); 2397 p += withdrawn_routes_len; 2398 length -= withdrawn_routes_len; 2399 #else 2400 if (withdrawn_routes_len < 2) 2401 goto trunc; 2402 length -= 2; 2403 withdrawn_routes_len -= 2; 2404 2405 2406 printf("\n\t Withdrawn routes:"); 2407 2408 while(withdrawn_routes_len > 0) { 2409 wpfx = decode_prefix4(p, withdrawn_routes_len, buf, sizeof(buf)); 2410 if (wpfx == -1) { 2411 printf("\n\t (illegal prefix length)"); 2412 break; 2413 } else if (wpfx == -2) 2414 goto trunc; 2415 else if (wpfx == -3) 2416 goto trunc; /* bytes left, but not enough */ 2417 else { 2418 printf("\n\t %s", buf); 2419 p += wpfx; 2420 length -= wpfx; 2421 withdrawn_routes_len -= wpfx; 2422 } 2423 } 2424 #endif 2425 } 2426 2427 TCHECK2(p[0], 2); 2428 if (length < 2) 2429 goto trunc; 2430 len = EXTRACT_16BITS(p); 2431 p += 2; 2432 length -= 2; 2433 2434 if (withdrawn_routes_len == 0 && len == 0 && length == 0) { 2435 /* No withdrawn routes, no path attributes, no NLRI */ 2436 printf("\n\t End-of-Rib Marker (empty NLRI)"); 2437 return; 2438 } 2439 2440 if (len) { 2441 /* do something more useful!*/ 2442 while (len) { 2443 int aflags, atype, alenlen, alen; 2444 2445 TCHECK2(p[0], 2); 2446 if (len < 2) 2447 goto trunc; 2448 if (length < 2) 2449 goto trunc; 2450 aflags = *p; 2451 atype = *(p + 1); 2452 p += 2; 2453 len -= 2; 2454 length -= 2; 2455 alenlen = bgp_attr_lenlen(aflags, p); 2456 TCHECK2(p[0], alenlen); 2457 if (len < alenlen) 2458 goto trunc; 2459 if (length < alenlen) 2460 goto trunc; 2461 alen = bgp_attr_len(aflags, p); 2462 p += alenlen; 2463 len -= alenlen; 2464 length -= alenlen; 2465 2466 printf("\n\t %s (%u), length: %u", 2467 tok2strbuf(bgp_attr_values, "Unknown Attribute", 2468 atype, 2469 tokbuf, sizeof(tokbuf)), 2470 atype, 2471 alen); 2472 2473 if (aflags) { 2474 printf(", Flags [%s%s%s%s", 2475 aflags & 0x80 ? "O" : "", 2476 aflags & 0x40 ? "T" : "", 2477 aflags & 0x20 ? "P" : "", 2478 aflags & 0x10 ? "E" : ""); 2479 if (aflags & 0xf) 2480 printf("+%x", aflags & 0xf); 2481 printf("]: "); 2482 } 2483 if (len < alen) 2484 goto trunc; 2485 if (length < alen) 2486 goto trunc; 2487 if (!bgp_attr_print(atype, p, alen)) 2488 goto trunc; 2489 p += alen; 2490 len -= alen; 2491 length -= alen; 2492 } 2493 } 2494 2495 if (length) { 2496 /* 2497 * XXX - what if they're using the "Advertisement of 2498 * Multiple Paths in BGP" feature: 2499 * 2500 * https://datatracker.ietf.org/doc/draft-ietf-idr-add-paths/ 2501 * 2502 * http://tools.ietf.org/html/draft-ietf-idr-add-paths-06 2503 */ 2504 printf("\n\t Updated routes:"); 2505 while (length) { 2506 char buf[MAXHOSTNAMELEN + 100]; 2507 i = decode_prefix4(p, length, buf, sizeof(buf)); 2508 if (i == -1) { 2509 printf("\n\t (illegal prefix length)"); 2510 break; 2511 } else if (i == -2) 2512 goto trunc; 2513 else if (i == -3) 2514 goto trunc; /* bytes left, but not enough */ 2515 else { 2516 printf("\n\t %s", buf); 2517 p += i; 2518 length -= i; 2519 } 2520 } 2521 } 2522 return; 2523 trunc: 2524 printf("[|BGP]"); 2525 } 2526 2527 static void 2528 bgp_notification_print(const u_char *dat, int length) 2529 { 2530 struct bgp_notification bgpn; 2531 const u_char *tptr; 2532 char tokbuf[TOKBUFSIZE]; 2533 char tokbuf2[TOKBUFSIZE]; 2534 2535 TCHECK2(dat[0], BGP_NOTIFICATION_SIZE); 2536 memcpy(&bgpn, dat, BGP_NOTIFICATION_SIZE); 2537 2538 /* some little sanity checking */ 2539 if (length<BGP_NOTIFICATION_SIZE) 2540 return; 2541 2542 printf(", %s (%u)", 2543 tok2strbuf(bgp_notify_major_values, "Unknown Error", 2544 bgpn.bgpn_major, tokbuf, sizeof(tokbuf)), 2545 bgpn.bgpn_major); 2546 2547 switch (bgpn.bgpn_major) { 2548 2549 case BGP_NOTIFY_MAJOR_MSG: 2550 printf(", subcode %s (%u)", 2551 tok2strbuf(bgp_notify_minor_msg_values, "Unknown", 2552 bgpn.bgpn_minor, tokbuf, sizeof(tokbuf)), 2553 bgpn.bgpn_minor); 2554 break; 2555 case BGP_NOTIFY_MAJOR_OPEN: 2556 printf(", subcode %s (%u)", 2557 tok2strbuf(bgp_notify_minor_open_values, "Unknown", 2558 bgpn.bgpn_minor, tokbuf, sizeof(tokbuf)), 2559 bgpn.bgpn_minor); 2560 break; 2561 case BGP_NOTIFY_MAJOR_UPDATE: 2562 printf(", subcode %s (%u)", 2563 tok2strbuf(bgp_notify_minor_update_values, "Unknown", 2564 bgpn.bgpn_minor, tokbuf, sizeof(tokbuf)), 2565 bgpn.bgpn_minor); 2566 break; 2567 case BGP_NOTIFY_MAJOR_CAP: 2568 printf(" subcode %s (%u)", 2569 tok2strbuf(bgp_notify_minor_cap_values, "Unknown", 2570 bgpn.bgpn_minor, tokbuf, sizeof(tokbuf)), 2571 bgpn.bgpn_minor); 2572 case BGP_NOTIFY_MAJOR_CEASE: 2573 printf(", subcode %s (%u)", 2574 tok2strbuf(bgp_notify_minor_cease_values, "Unknown", 2575 bgpn.bgpn_minor, tokbuf, sizeof(tokbuf)), 2576 bgpn.bgpn_minor); 2577 2578 /* draft-ietf-idr-cease-subcode-02 mentions optionally 7 bytes 2579 * for the maxprefix subtype, which may contain AFI, SAFI and MAXPREFIXES 2580 */ 2581 if(bgpn.bgpn_minor == BGP_NOTIFY_MINOR_CEASE_MAXPRFX && length >= BGP_NOTIFICATION_SIZE + 7) { 2582 tptr = dat + BGP_NOTIFICATION_SIZE; 2583 TCHECK2(*tptr, 7); 2584 printf(", AFI %s (%u), SAFI %s (%u), Max Prefixes: %u", 2585 tok2strbuf(af_values, "Unknown", 2586 EXTRACT_16BITS(tptr), tokbuf, sizeof(tokbuf)), 2587 EXTRACT_16BITS(tptr), 2588 tok2strbuf(bgp_safi_values, "Unknown", *(tptr+2), 2589 tokbuf2, sizeof(tokbuf)), 2590 *(tptr+2), 2591 EXTRACT_32BITS(tptr+3)); 2592 } 2593 break; 2594 default: 2595 break; 2596 } 2597 2598 return; 2599 trunc: 2600 printf("[|BGP]"); 2601 } 2602 2603 static void 2604 bgp_route_refresh_print(const u_char *pptr, int len) { 2605 2606 const struct bgp_route_refresh *bgp_route_refresh_header; 2607 char tokbuf[TOKBUFSIZE]; 2608 char tokbuf2[TOKBUFSIZE]; 2609 2610 TCHECK2(pptr[0], BGP_ROUTE_REFRESH_SIZE); 2611 2612 /* some little sanity checking */ 2613 if (len<BGP_ROUTE_REFRESH_SIZE) 2614 return; 2615 2616 bgp_route_refresh_header = (const struct bgp_route_refresh *)pptr; 2617 2618 printf("\n\t AFI %s (%u), SAFI %s (%u)", 2619 tok2strbuf(af_values,"Unknown", 2620 /* this stinks but the compiler pads the structure 2621 * weird */ 2622 EXTRACT_16BITS(&bgp_route_refresh_header->afi), 2623 tokbuf, sizeof(tokbuf)), 2624 EXTRACT_16BITS(&bgp_route_refresh_header->afi), 2625 tok2strbuf(bgp_safi_values,"Unknown", 2626 bgp_route_refresh_header->safi, 2627 tokbuf2, sizeof(tokbuf2)), 2628 bgp_route_refresh_header->safi); 2629 2630 if (vflag > 1) { 2631 TCHECK2(*pptr, len); 2632 print_unknown_data(pptr,"\n\t ", len); 2633 } 2634 2635 return; 2636 trunc: 2637 printf("[|BGP]"); 2638 } 2639 2640 static int 2641 bgp_header_print(const u_char *dat, int length) 2642 { 2643 struct bgp bgp; 2644 char tokbuf[TOKBUFSIZE]; 2645 2646 TCHECK2(dat[0], BGP_SIZE); 2647 memcpy(&bgp, dat, BGP_SIZE); 2648 printf("\n\t%s Message (%u), length: %u", 2649 tok2strbuf(bgp_msg_values, "Unknown", bgp.bgp_type, 2650 tokbuf, sizeof(tokbuf)), 2651 bgp.bgp_type, 2652 length); 2653 2654 switch (bgp.bgp_type) { 2655 case BGP_OPEN: 2656 bgp_open_print(dat, length); 2657 break; 2658 case BGP_UPDATE: 2659 bgp_update_print(dat, length); 2660 break; 2661 case BGP_NOTIFICATION: 2662 bgp_notification_print(dat, length); 2663 break; 2664 case BGP_KEEPALIVE: 2665 break; 2666 case BGP_ROUTE_REFRESH: 2667 bgp_route_refresh_print(dat, length); 2668 break; 2669 default: 2670 /* we have no decoder for the BGP message */ 2671 TCHECK2(*dat, length); 2672 printf("\n\t no Message %u decoder",bgp.bgp_type); 2673 print_unknown_data(dat,"\n\t ",length); 2674 break; 2675 } 2676 return 1; 2677 trunc: 2678 printf("[|BGP]"); 2679 return 0; 2680 } 2681 2682 void 2683 bgp_print(const u_char *dat, int length) 2684 { 2685 const u_char *p; 2686 const u_char *ep; 2687 const u_char *start; 2688 const u_char marker[] = { 2689 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 2690 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 2691 }; 2692 struct bgp bgp; 2693 u_int16_t hlen; 2694 char tokbuf[TOKBUFSIZE]; 2695 2696 ep = dat + length; 2697 if (snapend < dat + length) 2698 ep = snapend; 2699 2700 printf(": BGP, length: %u",length); 2701 2702 if (vflag < 1) /* lets be less chatty */ 2703 return; 2704 2705 p = dat; 2706 start = p; 2707 while (p < ep) { 2708 if (!TTEST2(p[0], 1)) 2709 break; 2710 if (p[0] != 0xff) { 2711 p++; 2712 continue; 2713 } 2714 2715 if (!TTEST2(p[0], sizeof(marker))) 2716 break; 2717 if (memcmp(p, marker, sizeof(marker)) != 0) { 2718 p++; 2719 continue; 2720 } 2721 2722 /* found BGP header */ 2723 TCHECK2(p[0], BGP_SIZE); /*XXX*/ 2724 memcpy(&bgp, p, BGP_SIZE); 2725 2726 if (start != p) 2727 printf(" [|BGP]"); 2728 2729 hlen = ntohs(bgp.bgp_len); 2730 if (hlen < BGP_SIZE) { 2731 printf("\n[|BGP Bogus header length %u < %u]", hlen, 2732 BGP_SIZE); 2733 break; 2734 } 2735 2736 if (TTEST2(p[0], hlen)) { 2737 if (!bgp_header_print(p, hlen)) 2738 return; 2739 p += hlen; 2740 start = p; 2741 } else { 2742 printf("\n[|BGP %s]", 2743 tok2strbuf(bgp_msg_values, 2744 "Unknown Message Type", 2745 bgp.bgp_type, 2746 tokbuf, sizeof(tokbuf))); 2747 break; 2748 } 2749 } 2750 2751 return; 2752 2753 trunc: 2754 printf(" [|BGP]"); 2755 } 2756 2757 /* 2758 * Local Variables: 2759 * c-style: whitesmith 2760 * c-basic-offset: 4 2761 * End: 2762 */ 2763