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