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.12 2023/08/17 20:19:40 christos Exp $"); 36 #endif 37 38 /* \summary: Border Gateway Protocol (BGP) printer */ 39 40 /* specification: RFC 4271 */ 41 42 #ifdef HAVE_CONFIG_H 43 #include <config.h> 44 #endif 45 46 #include "netdissect-stdinc.h" 47 48 #include <stdio.h> 49 #include <string.h> 50 51 #include "netdissect.h" 52 #include "addrtoname.h" 53 #include "extract.h" 54 #include "af.h" 55 #include "l2vpn.h" 56 57 struct bgp { 58 nd_byte bgp_marker[16]; 59 nd_uint16_t bgp_len; 60 nd_uint8_t bgp_type; 61 }; 62 #define BGP_SIZE 19 /* unaligned */ 63 64 #define BGP_OPEN 1 65 #define BGP_UPDATE 2 66 #define BGP_NOTIFICATION 3 67 #define BGP_KEEPALIVE 4 68 #define BGP_ROUTE_REFRESH 5 69 70 static const struct tok bgp_msg_values[] = { 71 { BGP_OPEN, "Open"}, 72 { BGP_UPDATE, "Update"}, 73 { BGP_NOTIFICATION, "Notification"}, 74 { BGP_KEEPALIVE, "Keepalive"}, 75 { BGP_ROUTE_REFRESH, "Route Refresh"}, 76 { 0, NULL} 77 }; 78 79 struct bgp_open { 80 nd_byte bgpo_marker[16]; 81 nd_uint16_t bgpo_len; 82 nd_uint8_t bgpo_type; 83 nd_uint8_t bgpo_version; 84 nd_uint16_t bgpo_myas; 85 nd_uint16_t bgpo_holdtime; 86 nd_uint32_t bgpo_id; 87 nd_uint8_t bgpo_optlen; 88 /* options should follow */ 89 }; 90 #define BGP_OPEN_SIZE 29 /* unaligned */ 91 92 struct bgp_opt { 93 nd_uint8_t bgpopt_type; 94 nd_uint8_t bgpopt_len; 95 /* variable length */ 96 }; 97 #define BGP_OPT_SIZE 2 /* some compilers may pad to 4 bytes */ 98 #define BGP_CAP_HEADER_SIZE 2 /* some compilers may pad to 4 bytes */ 99 100 struct bgp_notification { 101 nd_byte bgpn_marker[16]; 102 nd_uint16_t bgpn_len; 103 nd_uint8_t bgpn_type; 104 nd_uint8_t bgpn_major; 105 nd_uint8_t bgpn_minor; 106 }; 107 #define BGP_NOTIFICATION_SIZE 21 /* unaligned */ 108 109 struct bgp_route_refresh { 110 nd_byte bgp_marker[16]; 111 nd_uint16_t len; 112 nd_uint8_t type; /* No padding after this; afi is, in fact, not aligned */ 113 nd_uint16_t afi; 114 nd_uint8_t res; 115 nd_uint8_t safi; 116 }; 117 #define BGP_ROUTE_REFRESH_SIZE 23 118 119 #define bgp_attr_lenlen(flags, p) \ 120 (((flags) & 0x10) ? 2U : 1U) 121 #define bgp_attr_len(flags, p) \ 122 (((flags) & 0x10) ? GET_BE_U_2(p) : GET_U_1(p)) 123 124 #define BGPTYPE_ORIGIN 1 125 #define BGPTYPE_AS_PATH 2 126 #define BGPTYPE_NEXT_HOP 3 127 #define BGPTYPE_MULTI_EXIT_DISC 4 128 #define BGPTYPE_LOCAL_PREF 5 129 #define BGPTYPE_ATOMIC_AGGREGATE 6 130 #define BGPTYPE_AGGREGATOR 7 131 #define BGPTYPE_COMMUNITIES 8 /* RFC1997 */ 132 #define BGPTYPE_ORIGINATOR_ID 9 /* RFC4456 */ 133 #define BGPTYPE_CLUSTER_LIST 10 /* RFC4456 */ 134 #define BGPTYPE_DPA 11 /* deprecated, draft-ietf-idr-bgp-dpa */ 135 #define BGPTYPE_ADVERTISERS 12 /* deprecated RFC1863 */ 136 #define BGPTYPE_RCID_PATH 13 /* deprecated RFC1863 */ 137 #define BGPTYPE_MP_REACH_NLRI 14 /* RFC4760 */ 138 #define BGPTYPE_MP_UNREACH_NLRI 15 /* RFC4760 */ 139 #define BGPTYPE_EXTD_COMMUNITIES 16 /* RFC4360 */ 140 #define BGPTYPE_AS4_PATH 17 /* RFC6793 */ 141 #define BGPTYPE_AGGREGATOR4 18 /* RFC6793 */ 142 #define BGPTYPE_PMSI_TUNNEL 22 /* RFC6514 */ 143 #define BGPTYPE_TUNNEL_ENCAP 23 /* RFC5512 */ 144 #define BGPTYPE_TRAFFIC_ENG 24 /* RFC5543 */ 145 #define BGPTYPE_IPV6_EXTD_COMMUNITIES 25 /* RFC5701 */ 146 #define BGPTYPE_AIGP 26 /* RFC7311 */ 147 #define BGPTYPE_PE_DISTINGUISHER_LABEL 27 /* RFC6514 */ 148 #define BGPTYPE_ENTROPY_LABEL 28 /* RFC6790 */ 149 #define BGPTYPE_LARGE_COMMUNITY 32 /* draft-ietf-idr-large-community-05 */ 150 #define BGPTYPE_ATTR_SET 128 /* RFC6368 */ 151 152 #define BGP_MP_NLRI_MINSIZE 3 /* End of RIB Marker detection */ 153 154 static const struct tok bgp_attr_values[] = { 155 { BGPTYPE_ORIGIN, "Origin"}, 156 { BGPTYPE_AS_PATH, "AS Path"}, 157 { BGPTYPE_AS4_PATH, "AS4 Path"}, 158 { BGPTYPE_NEXT_HOP, "Next Hop"}, 159 { BGPTYPE_MULTI_EXIT_DISC, "Multi Exit Discriminator"}, 160 { BGPTYPE_LOCAL_PREF, "Local Preference"}, 161 { BGPTYPE_ATOMIC_AGGREGATE, "Atomic Aggregate"}, 162 { BGPTYPE_AGGREGATOR, "Aggregator"}, 163 { BGPTYPE_AGGREGATOR4, "Aggregator4"}, 164 { BGPTYPE_COMMUNITIES, "Community"}, 165 { BGPTYPE_ORIGINATOR_ID, "Originator ID"}, 166 { BGPTYPE_CLUSTER_LIST, "Cluster List"}, 167 { BGPTYPE_DPA, "DPA"}, 168 { BGPTYPE_ADVERTISERS, "Advertisers"}, 169 { BGPTYPE_RCID_PATH, "RCID Path / Cluster ID"}, 170 { BGPTYPE_MP_REACH_NLRI, "Multi-Protocol Reach NLRI"}, 171 { BGPTYPE_MP_UNREACH_NLRI, "Multi-Protocol Unreach NLRI"}, 172 { BGPTYPE_EXTD_COMMUNITIES, "Extended Community"}, 173 { BGPTYPE_PMSI_TUNNEL, "PMSI Tunnel"}, 174 { BGPTYPE_TUNNEL_ENCAP, "Tunnel Encapsulation"}, 175 { BGPTYPE_TRAFFIC_ENG, "Traffic Engineering"}, 176 { BGPTYPE_IPV6_EXTD_COMMUNITIES, "IPv6 Extended Community"}, 177 { BGPTYPE_AIGP, "Accumulated IGP Metric"}, 178 { BGPTYPE_PE_DISTINGUISHER_LABEL, "PE Distinguisher Label"}, 179 { BGPTYPE_ENTROPY_LABEL, "Entropy Label"}, 180 { BGPTYPE_LARGE_COMMUNITY, "Large Community"}, 181 { BGPTYPE_ATTR_SET, "Attribute Set"}, 182 { 255, "Reserved for development"}, 183 { 0, NULL} 184 }; 185 186 #define BGP_AS_SET 1 187 #define BGP_AS_SEQUENCE 2 188 #define BGP_CONFED_AS_SEQUENCE 3 /* draft-ietf-idr-rfc3065bis-01 */ 189 #define BGP_CONFED_AS_SET 4 /* draft-ietf-idr-rfc3065bis-01 */ 190 191 #define BGP_AS_SEG_TYPE_MIN BGP_AS_SET 192 #define BGP_AS_SEG_TYPE_MAX BGP_CONFED_AS_SET 193 194 static const struct tok bgp_as_path_segment_open_values[] = { 195 { BGP_AS_SEQUENCE, ""}, 196 { BGP_AS_SET, "{ "}, 197 { BGP_CONFED_AS_SEQUENCE, "( "}, 198 { BGP_CONFED_AS_SET, "({ "}, 199 { 0, NULL} 200 }; 201 202 static const struct tok bgp_as_path_segment_close_values[] = { 203 { BGP_AS_SEQUENCE, ""}, 204 { BGP_AS_SET, "}"}, 205 { BGP_CONFED_AS_SEQUENCE, ")"}, 206 { BGP_CONFED_AS_SET, "})"}, 207 { 0, NULL} 208 }; 209 210 #define BGP_OPT_AUTH 1 211 #define BGP_OPT_CAP 2 212 213 static const struct tok bgp_opt_values[] = { 214 { BGP_OPT_AUTH, "Authentication Information"}, 215 { BGP_OPT_CAP, "Capabilities Advertisement"}, 216 { 0, NULL} 217 }; 218 219 #define BGP_CAPCODE_MP 1 /* RFC2858 */ 220 #define BGP_CAPCODE_RR 2 /* RFC2918 */ 221 #define BGP_CAPCODE_ORF 3 /* RFC5291 */ 222 #define BGP_CAPCODE_MR 4 /* RFC3107 */ 223 #define BGP_CAPCODE_EXT_NH 5 /* RFC5549 */ 224 #define BGP_CAPCODE_ML 8 /* RFC8277 */ 225 #define BGP_CAPCODE_RESTART 64 /* RFC4724 */ 226 #define BGP_CAPCODE_AS_NEW 65 /* RFC6793 */ 227 #define BGP_CAPCODE_DYN_CAP 67 /* draft-ietf-idr-dynamic-cap */ 228 #define BGP_CAPCODE_MULTISESS 68 /* draft-ietf-idr-bgp-multisession */ 229 #define BGP_CAPCODE_ADD_PATH 69 /* RFC7911 */ 230 #define BGP_CAPCODE_ENH_RR 70 /* draft-keyur-bgp-enhanced-route-refresh */ 231 #define BGP_CAPCODE_LLGR 71 /* draft-uttaro-idr-bgp-persistence-05 */ 232 #define BGP_CAPCODE_RR_CISCO 128 233 234 static const struct tok bgp_capcode_values[] = { 235 { BGP_CAPCODE_MP, "Multiprotocol Extensions"}, 236 { BGP_CAPCODE_RR, "Route Refresh"}, 237 { BGP_CAPCODE_ORF, "Cooperative Route Filtering"}, 238 { BGP_CAPCODE_MR, "Multiple Routes to a Destination"}, 239 { BGP_CAPCODE_EXT_NH, "Extended Next Hop Encoding"}, 240 { BGP_CAPCODE_ML, "Multiple Labels"}, 241 { BGP_CAPCODE_RESTART, "Graceful Restart"}, 242 { BGP_CAPCODE_AS_NEW, "32-Bit AS Number"}, 243 { BGP_CAPCODE_DYN_CAP, "Dynamic Capability"}, 244 { BGP_CAPCODE_MULTISESS, "Multisession BGP"}, 245 { BGP_CAPCODE_ADD_PATH, "Multiple Paths"}, 246 { BGP_CAPCODE_ENH_RR, "Enhanced Route Refresh"}, 247 { BGP_CAPCODE_LLGR, "Long-lived Graceful Restart"}, 248 { BGP_CAPCODE_RR_CISCO, "Route Refresh (Cisco)"}, 249 { 0, NULL} 250 }; 251 252 #define BGP_NOTIFY_MAJOR_MSG 1 253 #define BGP_NOTIFY_MAJOR_OPEN 2 254 #define BGP_NOTIFY_MAJOR_UPDATE 3 255 #define BGP_NOTIFY_MAJOR_HOLDTIME 4 256 #define BGP_NOTIFY_MAJOR_FSM 5 257 #define BGP_NOTIFY_MAJOR_CEASE 6 258 #define BGP_NOTIFY_MAJOR_CAP 7 259 260 static const struct tok bgp_notify_major_values[] = { 261 { BGP_NOTIFY_MAJOR_MSG, "Message Header Error"}, 262 { BGP_NOTIFY_MAJOR_OPEN, "OPEN Message Error"}, 263 { BGP_NOTIFY_MAJOR_UPDATE, "UPDATE Message Error"}, 264 { BGP_NOTIFY_MAJOR_HOLDTIME,"Hold Timer Expired"}, 265 { BGP_NOTIFY_MAJOR_FSM, "Finite State Machine Error"}, 266 { BGP_NOTIFY_MAJOR_CEASE, "Cease"}, 267 { BGP_NOTIFY_MAJOR_CAP, "Capability Message Error"}, 268 { 0, NULL} 269 }; 270 271 /* RFC 4486 */ 272 #define BGP_NOTIFY_MINOR_CEASE_MAXPRFX 1 273 #define BGP_NOTIFY_MINOR_CEASE_SHUT 2 274 #define BGP_NOTIFY_MINOR_CEASE_RESET 4 275 static const struct tok bgp_notify_minor_cease_values[] = { 276 { BGP_NOTIFY_MINOR_CEASE_MAXPRFX, "Maximum Number of Prefixes Reached"}, 277 { BGP_NOTIFY_MINOR_CEASE_SHUT, "Administrative Shutdown"}, 278 { 3, "Peer Unconfigured"}, 279 { BGP_NOTIFY_MINOR_CEASE_RESET, "Administrative Reset"}, 280 { 5, "Connection Rejected"}, 281 { 6, "Other Configuration Change"}, 282 { 7, "Connection Collision Resolution"}, 283 { 0, NULL} 284 }; 285 286 static const struct tok bgp_notify_minor_msg_values[] = { 287 { 1, "Connection Not Synchronized"}, 288 { 2, "Bad Message Length"}, 289 { 3, "Bad Message Type"}, 290 { 0, NULL} 291 }; 292 293 static const struct tok bgp_notify_minor_open_values[] = { 294 { 1, "Unsupported Version Number"}, 295 { 2, "Bad Peer AS"}, 296 { 3, "Bad BGP Identifier"}, 297 { 4, "Unsupported Optional Parameter"}, 298 { 5, "Authentication Failure"}, 299 { 6, "Unacceptable Hold Time"}, 300 { 7, "Capability Message Error"}, 301 { 0, NULL} 302 }; 303 304 static const struct tok bgp_notify_minor_update_values[] = { 305 { 1, "Malformed Attribute List"}, 306 { 2, "Unrecognized Well-known Attribute"}, 307 { 3, "Missing Well-known Attribute"}, 308 { 4, "Attribute Flags Error"}, 309 { 5, "Attribute Length Error"}, 310 { 6, "Invalid ORIGIN Attribute"}, 311 { 7, "AS Routing Loop"}, 312 { 8, "Invalid NEXT_HOP Attribute"}, 313 { 9, "Optional Attribute Error"}, 314 { 10, "Invalid Network Field"}, 315 { 11, "Malformed AS_PATH"}, 316 { 0, NULL} 317 }; 318 319 static const struct tok bgp_notify_minor_fsm_values[] = { 320 { 0, "Unspecified Error"}, 321 { 1, "In OpenSent State"}, 322 { 2, "In OpenConfirm State"}, 323 { 3, "In Established State"}, 324 { 0, NULL } 325 }; 326 327 static const struct tok bgp_notify_minor_cap_values[] = { 328 { 1, "Invalid Action Value" }, 329 { 2, "Invalid Capability Length" }, 330 { 3, "Malformed Capability Value" }, 331 { 4, "Unsupported Capability Code" }, 332 { 0, NULL } 333 }; 334 335 static const struct tok bgp_origin_values[] = { 336 { 0, "IGP"}, 337 { 1, "EGP"}, 338 { 2, "Incomplete"}, 339 { 0, NULL} 340 }; 341 342 #define BGP_PMSI_TUNNEL_RSVP_P2MP 1 343 #define BGP_PMSI_TUNNEL_LDP_P2MP 2 344 #define BGP_PMSI_TUNNEL_PIM_SSM 3 345 #define BGP_PMSI_TUNNEL_PIM_SM 4 346 #define BGP_PMSI_TUNNEL_PIM_BIDIR 5 347 #define BGP_PMSI_TUNNEL_INGRESS 6 348 #define BGP_PMSI_TUNNEL_LDP_MP2MP 7 349 350 static const struct tok bgp_pmsi_tunnel_values[] = { 351 { BGP_PMSI_TUNNEL_RSVP_P2MP, "RSVP-TE P2MP LSP"}, 352 { BGP_PMSI_TUNNEL_LDP_P2MP, "LDP P2MP LSP"}, 353 { BGP_PMSI_TUNNEL_PIM_SSM, "PIM-SSM Tree"}, 354 { BGP_PMSI_TUNNEL_PIM_SM, "PIM-SM Tree"}, 355 { BGP_PMSI_TUNNEL_PIM_BIDIR, "PIM-Bidir Tree"}, 356 { BGP_PMSI_TUNNEL_INGRESS, "Ingress Replication"}, 357 { BGP_PMSI_TUNNEL_LDP_MP2MP, "LDP MP2MP LSP"}, 358 { 0, NULL} 359 }; 360 361 static const struct tok bgp_pmsi_flag_values[] = { 362 { 0x01, "Leaf Information required"}, 363 { 0, NULL} 364 }; 365 366 #define BGP_AIGP_TLV 1 367 368 static const struct tok bgp_aigp_values[] = { 369 { BGP_AIGP_TLV, "AIGP"}, 370 { 0, NULL} 371 }; 372 373 /* Subsequent address family identifier, RFC2283 section 7 */ 374 #define SAFNUM_RES 0 375 #define SAFNUM_UNICAST 1 376 #define SAFNUM_MULTICAST 2 377 #define SAFNUM_UNIMULTICAST 3 /* deprecated now */ 378 /* labeled BGP RFC3107 */ 379 #define SAFNUM_LABUNICAST 4 380 /* RFC6514 */ 381 #define SAFNUM_MULTICAST_VPN 5 382 /* draft-nalawade-kapoor-tunnel-safi */ 383 #define SAFNUM_TUNNEL 64 384 /* RFC4761 */ 385 #define SAFNUM_VPLS 65 386 /* RFC6037 */ 387 #define SAFNUM_MDT 66 388 /* RFC7432 */ 389 #define SAFNUM_EVPN 70 390 /* RFC4364 */ 391 #define SAFNUM_VPNUNICAST 128 392 /* RFC6513 */ 393 #define SAFNUM_VPNMULTICAST 129 394 #define SAFNUM_VPNUNIMULTICAST 130 /* deprecated now */ 395 /* RFC4684 */ 396 #define SAFNUM_RT_ROUTING_INFO 132 397 398 #define BGP_VPN_RD_LEN 8 399 400 static const struct tok bgp_safi_values[] = { 401 { SAFNUM_RES, "Reserved"}, 402 { SAFNUM_UNICAST, "Unicast"}, 403 { SAFNUM_MULTICAST, "Multicast"}, 404 { SAFNUM_UNIMULTICAST, "Unicast+Multicast"}, 405 { SAFNUM_LABUNICAST, "labeled Unicast"}, 406 { SAFNUM_TUNNEL, "Tunnel"}, 407 { SAFNUM_VPLS, "VPLS"}, 408 { SAFNUM_MDT, "MDT"}, 409 { SAFNUM_EVPN, "EVPN"}, 410 { SAFNUM_VPNUNICAST, "labeled VPN Unicast"}, 411 { SAFNUM_VPNMULTICAST, "labeled VPN Multicast"}, 412 { SAFNUM_VPNUNIMULTICAST, "labeled VPN Unicast+Multicast"}, 413 { SAFNUM_RT_ROUTING_INFO, "Route Target Routing Information"}, 414 { SAFNUM_MULTICAST_VPN, "Multicast VPN"}, 415 { 0, NULL } 416 }; 417 418 /* well-known community */ 419 #define BGP_COMMUNITY_NO_EXPORT 0xffffff01 420 #define BGP_COMMUNITY_NO_ADVERT 0xffffff02 421 #define BGP_COMMUNITY_NO_EXPORT_SUBCONFED 0xffffff03 422 423 /* Extended community type - RFC 4360 */ 424 #define BGP_EXT_COM_RT_0 0x0002 /* Route Target,Format AS(2bytes):AN(4bytes) */ 425 #define BGP_EXT_COM_RT_1 0x0102 /* Route Target,Format IP address:AN(2bytes) */ 426 #define BGP_EXT_COM_RT_2 0x0202 /* Route Target,Format AN(4bytes):local(2bytes) */ 427 #define BGP_EXT_COM_RO_0 0x0003 /* Route Origin,Format AS(2bytes):AN(4bytes) */ 428 #define BGP_EXT_COM_RO_1 0x0103 /* Route Origin,Format IP address:AN(2bytes) */ 429 #define BGP_EXT_COM_RO_2 0x0203 /* Route Origin,Format AN(4bytes):local(2bytes) */ 430 #define BGP_EXT_COM_LINKBAND 0x4004 /* Link Bandwidth,Format AS(2B):Bandwidth(4B) */ 431 /* rfc2547 bgp-mpls-vpns */ 432 #define BGP_EXT_COM_VPN_ORIGIN 0x0005 /* OSPF Domain ID / VPN of Origin - draft-rosen-vpns-ospf-bgp-mpls */ 433 #define BGP_EXT_COM_VPN_ORIGIN2 0x0105 /* duplicate - keep for backwards compatibility */ 434 #define BGP_EXT_COM_VPN_ORIGIN3 0x0205 /* duplicate - keep for backwards compatibility */ 435 #define BGP_EXT_COM_VPN_ORIGIN4 0x8005 /* duplicate - keep for backwards compatibility */ 436 437 #define BGP_EXT_COM_OSPF_RTYPE 0x0306 /* OSPF Route Type,Format Area(4B):RouteType(1B):Options(1B) */ 438 #define BGP_EXT_COM_OSPF_RTYPE2 0x8000 /* duplicate - keep for backwards compatibility */ 439 #define BGP_EXT_COM_ENCAP 0x030c /* rfc5512 */ 440 441 #define BGP_EXT_COM_OSPF_RID 0x0107 /* OSPF Router ID,Format RouterID(4B):Unused(2B) */ 442 #define BGP_EXT_COM_OSPF_RID2 0x8001 /* duplicate - keep for backwards compatibility */ 443 444 #define BGP_EXT_COM_L2INFO 0x800a /* draft-kompella-ppvpn-l2vpn */ 445 446 #define BGP_EXT_COM_SOURCE_AS 0x0009 /* RFC-ietf-l3vpn-2547bis-mcast-bgp-08.txt */ 447 #define BGP_EXT_COM_VRF_RT_IMP 0x010b /* RFC-ietf-l3vpn-2547bis-mcast-bgp-08.txt */ 448 #define BGP_EXT_COM_L2VPN_RT_0 0x000a /* L2VPN Identifier,Format AS(2bytes):AN(4bytes) */ 449 #define BGP_EXT_COM_L2VPN_RT_1 0xF10a /* L2VPN Identifier,Format IP address:AN(2bytes) */ 450 451 /* https://www.cisco.com/en/US/tech/tk436/tk428/technologies_tech_note09186a00801eb09a.shtml */ 452 #define BGP_EXT_COM_EIGRP_GEN 0x8800 453 #define BGP_EXT_COM_EIGRP_METRIC_AS_DELAY 0x8801 454 #define BGP_EXT_COM_EIGRP_METRIC_REL_NH_BW 0x8802 455 #define BGP_EXT_COM_EIGRP_METRIC_LOAD_MTU 0x8803 456 #define BGP_EXT_COM_EIGRP_EXT_REMAS_REMID 0x8804 457 #define BGP_EXT_COM_EIGRP_EXT_REMPROTO_REMMETRIC 0x8805 458 459 static const struct tok bgp_extd_comm_flag_values[] = { 460 { 0x8000, "vendor-specific"}, 461 { 0x4000, "non-transitive"}, 462 { 0, NULL}, 463 }; 464 465 static const struct tok bgp_extd_comm_subtype_values[] = { 466 { BGP_EXT_COM_RT_0, "target"}, 467 { BGP_EXT_COM_RT_1, "target"}, 468 { BGP_EXT_COM_RT_2, "target"}, 469 { BGP_EXT_COM_RO_0, "origin"}, 470 { BGP_EXT_COM_RO_1, "origin"}, 471 { BGP_EXT_COM_RO_2, "origin"}, 472 { BGP_EXT_COM_LINKBAND, "link-BW"}, 473 { BGP_EXT_COM_VPN_ORIGIN, "ospf-domain"}, 474 { BGP_EXT_COM_VPN_ORIGIN2, "ospf-domain"}, 475 { BGP_EXT_COM_VPN_ORIGIN3, "ospf-domain"}, 476 { BGP_EXT_COM_VPN_ORIGIN4, "ospf-domain"}, 477 { BGP_EXT_COM_OSPF_RTYPE, "ospf-route-type"}, 478 { BGP_EXT_COM_OSPF_RTYPE2, "ospf-route-type"}, 479 { BGP_EXT_COM_ENCAP, "encapsulation"}, 480 { BGP_EXT_COM_OSPF_RID, "ospf-router-id"}, 481 { BGP_EXT_COM_OSPF_RID2, "ospf-router-id"}, 482 { BGP_EXT_COM_L2INFO, "layer2-info"}, 483 { BGP_EXT_COM_EIGRP_GEN, "eigrp-general-route (flag, tag)" }, 484 { BGP_EXT_COM_EIGRP_METRIC_AS_DELAY, "eigrp-route-metric (AS, delay)" }, 485 { BGP_EXT_COM_EIGRP_METRIC_REL_NH_BW, "eigrp-route-metric (reliability, nexthop, bandwidth)" }, 486 { BGP_EXT_COM_EIGRP_METRIC_LOAD_MTU, "eigrp-route-metric (load, MTU)" }, 487 { BGP_EXT_COM_EIGRP_EXT_REMAS_REMID, "eigrp-external-route (remote-AS, remote-ID)" }, 488 { BGP_EXT_COM_EIGRP_EXT_REMPROTO_REMMETRIC, "eigrp-external-route (remote-proto, remote-metric)" }, 489 { BGP_EXT_COM_SOURCE_AS, "source-AS" }, 490 { BGP_EXT_COM_VRF_RT_IMP, "vrf-route-import"}, 491 { BGP_EXT_COM_L2VPN_RT_0, "l2vpn-id"}, 492 { BGP_EXT_COM_L2VPN_RT_1, "l2vpn-id"}, 493 { 0, NULL}, 494 }; 495 496 /* RFC RFC5512 BGP Tunnel Encapsulation Attribute Tunnel Types */ 497 #define BGP_ENCAP_TUNNEL_L2TPV3_IP 1 498 #define BGP_ENCAP_TUNNEL_GRE 2 499 #define BGP_ENCAP_TUNNEL_TRANSMIT 3 500 #define BGP_ENCAP_TUNNEL_IPSEC 4 501 #define BGP_ENCAP_TUNNEL_IP_IPSEC 5 502 #define BGP_ENCAP_TUNNEL_MPLS_IP 6 503 #define BGP_ENCAP_TUNNEL_IP_IP 7 504 #define BGP_ENCAP_TUNNEL_VXLAN 8 505 #define BGP_ENCAP_TUNNEL_NVGRE 9 506 #define BGP_ENCAP_TUNNEL_MPLS 10 507 #define BGP_ENCAP_TUNNEL_MPLS_GRE 11 508 #define BGP_ENCAP_TUNNEL_VXLAN_GPE 12 509 #define BGP_ENCAP_TUNNEL_MPLS_UDP 13 510 #define BGP_ENCAP_TUNNEL_IPV6 14 511 #define BGP_ENCAP_TUNNEL_SR_TE 15 512 #define BGP_ENCAP_TUNNEL_BARE 16 513 #define BGP_ENCAP_TUNNEL_SR 17 514 515 static const struct tok bgp_extd_comm_encap_tunnel_values[] = { 516 { BGP_ENCAP_TUNNEL_L2TPV3_IP, "L2TPv3 over IP"}, 517 { BGP_ENCAP_TUNNEL_GRE, "GRE"}, 518 { BGP_ENCAP_TUNNEL_TRANSMIT, "Transmit Tunnel"}, 519 { BGP_ENCAP_TUNNEL_IPSEC, "IPsec"}, 520 { BGP_ENCAP_TUNNEL_IP_IPSEC, "IP in IP with IPsec"}, 521 { BGP_ENCAP_TUNNEL_MPLS_IP, "MPLS in IP with IPsec"}, 522 { BGP_ENCAP_TUNNEL_IP_IP, "IP in IP"}, 523 { BGP_ENCAP_TUNNEL_VXLAN, "VXLAN"}, 524 { BGP_ENCAP_TUNNEL_NVGRE, "NVGRE"}, 525 { BGP_ENCAP_TUNNEL_MPLS, "MPLS"}, 526 { BGP_ENCAP_TUNNEL_MPLS_GRE, "MPLS in GRE"}, 527 { BGP_ENCAP_TUNNEL_VXLAN_GPE, "VXLAN GPE"}, 528 { BGP_ENCAP_TUNNEL_MPLS_UDP, "MPLS in UDP"}, 529 { BGP_ENCAP_TUNNEL_IPV6, "IPv6"}, 530 { BGP_ENCAP_TUNNEL_SR_TE, "SR TE"}, 531 { BGP_ENCAP_TUNNEL_BARE, "Bare"}, 532 { BGP_ENCAP_TUNNEL_SR, "SR"}, 533 { 0, NULL}, 534 }; 535 536 /* OSPF codes for BGP_EXT_COM_OSPF_RTYPE draft-rosen-vpns-ospf-bgp-mpls */ 537 #define BGP_OSPF_RTYPE_RTR 1 /* OSPF Router LSA */ 538 #define BGP_OSPF_RTYPE_NET 2 /* OSPF Network LSA */ 539 #define BGP_OSPF_RTYPE_SUM 3 /* OSPF Summary LSA */ 540 #define BGP_OSPF_RTYPE_EXT 5 /* OSPF External LSA, note that ASBR doesn't apply to MPLS-VPN */ 541 #define BGP_OSPF_RTYPE_NSSA 7 /* OSPF NSSA External*/ 542 #define BGP_OSPF_RTYPE_SHAM 129 /* OSPF-MPLS-VPN Sham link */ 543 #define BGP_OSPF_RTYPE_METRIC_TYPE 0x1 /* LSB of RTYPE Options Field */ 544 545 static const struct tok bgp_extd_comm_ospf_rtype_values[] = { 546 { BGP_OSPF_RTYPE_RTR, "Router" }, 547 { BGP_OSPF_RTYPE_NET, "Network" }, 548 { BGP_OSPF_RTYPE_SUM, "Summary" }, 549 { BGP_OSPF_RTYPE_EXT, "External" }, 550 { BGP_OSPF_RTYPE_NSSA,"NSSA External" }, 551 { BGP_OSPF_RTYPE_SHAM,"MPLS-VPN Sham" }, 552 { 0, NULL }, 553 }; 554 555 /* ADD-PATH Send/Receive field values */ 556 static const struct tok bgp_add_path_recvsend[] = { 557 { 1, "Receive" }, 558 { 2, "Send" }, 559 { 3, "Both" }, 560 { 0, NULL }, 561 }; 562 563 #define AS_STR_SIZE sizeof("xxxxx.xxxxx") 564 565 /* 566 * as_printf 567 * 568 * Convert an AS number into a string and return string pointer. 569 * 570 * Depending on bflag is set or not, AS number is converted into ASDOT notation 571 * or plain number notation. 572 * 573 */ 574 static char * 575 as_printf(netdissect_options *ndo, 576 char *str, size_t size, u_int asnum) 577 { 578 if (!ndo->ndo_bflag || asnum <= 0xFFFF) { 579 snprintf(str, size, "%u", asnum); 580 } else { 581 snprintf(str, size, "%u.%u", asnum >> 16, asnum & 0xFFFF); 582 } 583 return str; 584 } 585 586 #define ITEMCHECK(minlen) if (itemlen < minlen) goto badtlv; 587 588 int 589 decode_prefix4(netdissect_options *ndo, 590 const u_char *pptr, u_int itemlen, char *buf, size_t buflen) 591 { 592 nd_ipv4 addr; 593 u_int plen, plenbytes; 594 595 ITEMCHECK(1); 596 plen = GET_U_1(pptr); 597 if (32 < plen) 598 return -1; 599 itemlen -= 1; 600 601 memset(&addr, 0, sizeof(addr)); 602 plenbytes = (plen + 7) / 8; 603 ITEMCHECK(plenbytes); 604 GET_CPY_BYTES(&addr, pptr + 1, plenbytes); 605 if (plen % 8) { 606 ((u_char *)&addr)[plenbytes - 1] &= ((0xff00 >> (plen % 8)) & 0xff); 607 } 608 snprintf(buf, buflen, "%s/%u", ipaddr_string(ndo, (const u_char *)&addr), plen); 609 return 1 + plenbytes; 610 611 badtlv: 612 return -2; 613 } 614 615 static int 616 decode_labeled_prefix4(netdissect_options *ndo, 617 const u_char *pptr, u_int itemlen, char *buf, 618 size_t buflen) 619 { 620 nd_ipv4 addr; 621 u_int plen, plenbytes; 622 623 /* prefix length and label = 4 bytes */ 624 ND_TCHECK_4(pptr); 625 ITEMCHECK(4); 626 plen = GET_U_1(pptr); /* get prefix length */ 627 628 /* this is one of the weirdnesses of rfc3107 629 the label length (actually the label + COS bits) 630 is added to the prefix length; 631 we also do only read out just one label - 632 there is no real application for advertisement of 633 stacked labels in a single BGP message 634 */ 635 636 if (24 > plen) 637 return -1; 638 639 plen-=24; /* adjust prefixlen - labellength */ 640 641 if (32 < plen) 642 return -1; 643 itemlen -= 4; 644 645 memset(&addr, 0, sizeof(addr)); 646 plenbytes = (plen + 7) / 8; 647 ITEMCHECK(plenbytes); 648 GET_CPY_BYTES(&addr, pptr + 4, plenbytes); 649 if (plen % 8) { 650 ((u_char *)&addr)[plenbytes - 1] &= ((0xff00 >> (plen % 8)) & 0xff); 651 } 652 /* the label may get offsetted by 4 bits so lets shift it right */ 653 snprintf(buf, buflen, "%s/%u, label:%u %s", 654 ipaddr_string(ndo, (const u_char *)&addr), 655 plen, 656 GET_BE_U_3(pptr + 1)>>4, 657 ((GET_U_1(pptr + 3) & 1) == 0) ? "(BOGUS: Bottom of Stack NOT set!)" : "(bottom)" ); 658 659 return 4 + plenbytes; 660 661 trunc: 662 return -2; 663 664 badtlv: 665 return -3; 666 } 667 668 /* 669 * bgp_vpn_ip_print 670 * 671 * print an ipv4 or ipv6 address into a buffer dependent on address length. 672 */ 673 static char * 674 bgp_vpn_ip_print(netdissect_options *ndo, 675 const u_char *pptr, u_int addr_length) 676 { 677 678 /* worst case string is s fully formatted v6 address */ 679 static char addr[sizeof("1234:5678:89ab:cdef:1234:5678:89ab:cdef")]; 680 char *pos = addr; 681 682 switch(addr_length) { 683 case (sizeof(nd_ipv4) << 3): /* 32 */ 684 snprintf(pos, sizeof(addr), "%s", GET_IPADDR_STRING(pptr)); 685 break; 686 case (sizeof(nd_ipv6) << 3): /* 128 */ 687 snprintf(pos, sizeof(addr), "%s", GET_IP6ADDR_STRING(pptr)); 688 break; 689 default: 690 snprintf(pos, sizeof(addr), "bogus address length %u", addr_length); 691 break; 692 } 693 pos += strlen(pos); 694 695 *(pos) = '\0'; 696 return (addr); 697 } 698 699 /* 700 * bgp_vpn_sg_print 701 * 702 * print an multicast s,g entry into a buffer. 703 * the s,g entry is encoded like this. 704 * 705 * +-----------------------------------+ 706 * | Multicast Source Length (1 octet) | 707 * +-----------------------------------+ 708 * | Multicast Source (Variable) | 709 * +-----------------------------------+ 710 * | Multicast Group Length (1 octet) | 711 * +-----------------------------------+ 712 * | Multicast Group (Variable) | 713 * +-----------------------------------+ 714 * 715 * return the number of bytes read from the wire. 716 */ 717 static u_int 718 bgp_vpn_sg_print(netdissect_options *ndo, 719 const u_char *pptr, char *buf, size_t buflen) 720 { 721 uint8_t addr_length; 722 u_int total_length, offset; 723 724 total_length = 0; 725 726 /* Source address length, encoded in bits */ 727 addr_length = GET_U_1(pptr); 728 pptr++; 729 730 /* Source address */ 731 ND_TCHECK_LEN(pptr, (addr_length >> 3)); 732 total_length += (addr_length >> 3) + 1; 733 offset = (u_int)strlen(buf); 734 if (addr_length) { 735 snprintf(buf + offset, buflen - offset, ", Source %s", 736 bgp_vpn_ip_print(ndo, pptr, addr_length)); 737 pptr += (addr_length >> 3); 738 } 739 740 /* Group address length, encoded in bits */ 741 addr_length = GET_U_1(pptr); 742 pptr++; 743 744 /* Group address */ 745 ND_TCHECK_LEN(pptr, (addr_length >> 3)); 746 total_length += (addr_length >> 3) + 1; 747 offset = (u_int)strlen(buf); 748 if (addr_length) { 749 snprintf(buf + offset, buflen - offset, ", Group %s", 750 bgp_vpn_ip_print(ndo, pptr, addr_length)); 751 pptr += (addr_length >> 3); 752 } 753 754 trunc: 755 return (total_length); 756 } 757 758 /* Print an RFC 4364 Route Distinguisher */ 759 const char * 760 bgp_vpn_rd_print(netdissect_options *ndo, 761 const u_char *pptr) 762 { 763 /* allocate space for the largest possible string */ 764 static char rd[sizeof("xxxxx.xxxxx:xxxxx (xxx.xxx.xxx.xxx:xxxxx)")]; 765 char *pos = rd; 766 /* allocate space for the largest possible string */ 767 char astostr[AS_STR_SIZE]; 768 769 /* ok lets load the RD format */ 770 switch (GET_BE_U_2(pptr)) { 771 772 case 0: 773 /* 2-byte-AS:number fmt */ 774 snprintf(pos, sizeof(rd) - (pos - rd), "%u:%u (= %u.%u.%u.%u)", 775 GET_BE_U_2(pptr + 2), 776 GET_BE_U_4(pptr + 4), 777 GET_U_1(pptr + 4), GET_U_1(pptr + 5), 778 GET_U_1(pptr + 6), GET_U_1(pptr + 7)); 779 break; 780 781 case 1: 782 /* IP-address:AS fmt */ 783 snprintf(pos, sizeof(rd) - (pos - rd), "%u.%u.%u.%u:%u", 784 GET_U_1(pptr + 2), GET_U_1(pptr + 3), 785 GET_U_1(pptr + 4), GET_U_1(pptr + 5), 786 GET_BE_U_2(pptr + 6)); 787 break; 788 789 case 2: 790 /* 4-byte-AS:number fmt */ 791 snprintf(pos, sizeof(rd) - (pos - rd), "%s:%u (%u.%u.%u.%u:%u)", 792 as_printf(ndo, astostr, sizeof(astostr), GET_BE_U_4(pptr + 2)), 793 GET_BE_U_2(pptr + 6), GET_U_1(pptr + 2), 794 GET_U_1(pptr + 3), GET_U_1(pptr + 4), 795 GET_U_1(pptr + 5), GET_BE_U_2(pptr + 6)); 796 break; 797 default: 798 snprintf(pos, sizeof(rd) - (pos - rd), "unknown RD format"); 799 break; 800 } 801 pos += strlen(pos); 802 *(pos) = '\0'; 803 return (rd); 804 } 805 806 /* 807 * Print an RFC 4360 Extended Community. 808 */ 809 static void 810 bgp_extended_community_print(netdissect_options *ndo, 811 const u_char *pptr) 812 { 813 union { /* copy buffer for bandwidth values */ 814 float f; 815 uint32_t i; 816 } bw; 817 /* allocate space for the largest possible string */ 818 char astostr[AS_STR_SIZE]; 819 820 switch (GET_BE_U_2(pptr)) { 821 822 case BGP_EXT_COM_RT_0: 823 case BGP_EXT_COM_RO_0: 824 case BGP_EXT_COM_L2VPN_RT_0: 825 ND_PRINT("%u:%u (= %s)", 826 GET_BE_U_2(pptr + 2), 827 GET_BE_U_4(pptr + 4), 828 GET_IPADDR_STRING(pptr+4)); 829 break; 830 831 case BGP_EXT_COM_RT_1: 832 case BGP_EXT_COM_RO_1: 833 case BGP_EXT_COM_L2VPN_RT_1: 834 case BGP_EXT_COM_VRF_RT_IMP: 835 ND_PRINT("%s:%u", 836 GET_IPADDR_STRING(pptr+2), 837 GET_BE_U_2(pptr + 6)); 838 break; 839 840 case BGP_EXT_COM_RT_2: 841 case BGP_EXT_COM_RO_2: 842 ND_PRINT("%s:%u", 843 as_printf(ndo, astostr, sizeof(astostr), 844 GET_BE_U_4(pptr + 2)), GET_BE_U_2(pptr + 6)); 845 break; 846 847 case BGP_EXT_COM_LINKBAND: 848 bw.i = GET_BE_U_4(pptr + 4); 849 ND_PRINT("bandwidth: %.3f Mbps", 850 bw.f*8/1000000); 851 break; 852 853 case BGP_EXT_COM_VPN_ORIGIN: 854 case BGP_EXT_COM_VPN_ORIGIN2: 855 case BGP_EXT_COM_VPN_ORIGIN3: 856 case BGP_EXT_COM_VPN_ORIGIN4: 857 case BGP_EXT_COM_OSPF_RID: 858 case BGP_EXT_COM_OSPF_RID2: 859 ND_PRINT("%s", GET_IPADDR_STRING(pptr+2)); 860 break; 861 862 case BGP_EXT_COM_OSPF_RTYPE: 863 case BGP_EXT_COM_OSPF_RTYPE2: 864 ND_PRINT("area:%s, router-type:%s, metric-type:%s%s", 865 GET_IPADDR_STRING(pptr+2), 866 tok2str(bgp_extd_comm_ospf_rtype_values, 867 "unknown (0x%02x)", 868 GET_U_1((pptr + 6))), 869 (GET_U_1(pptr + 7) & BGP_OSPF_RTYPE_METRIC_TYPE) ? "E2" : "", 870 ((GET_U_1(pptr + 6) == BGP_OSPF_RTYPE_EXT) || (GET_U_1(pptr + 6) == BGP_OSPF_RTYPE_NSSA)) ? "E1" : ""); 871 break; 872 873 case BGP_EXT_COM_L2INFO: 874 ND_PRINT("%s Control Flags [0x%02x]:MTU %u", 875 tok2str(l2vpn_encaps_values, 876 "unknown encaps", 877 GET_U_1((pptr + 2))), 878 GET_U_1((pptr + 3)), 879 GET_BE_U_2(pptr + 4)); 880 break; 881 882 case BGP_EXT_COM_SOURCE_AS: 883 ND_PRINT("AS %u", GET_BE_U_2(pptr + 2)); 884 break; 885 886 case BGP_EXT_COM_ENCAP: 887 ND_PRINT("Tunnel type: %s", tok2str(bgp_extd_comm_encap_tunnel_values, 888 "unknown encaps", 889 GET_BE_U_2(pptr + 6))); 890 break; 891 892 default: 893 ND_PRINT("%02x%02x%02x%02x%02x%02x", 894 GET_U_1(pptr + 2), 895 GET_U_1(pptr + 3), 896 GET_U_1(pptr + 4), 897 GET_U_1(pptr + 5), 898 GET_U_1(pptr + 6), 899 GET_U_1(pptr + 7)); 900 break; 901 } 902 } 903 904 /* 905 * RFC4684 (Section 4)/RFC2858 (Section 4). 906 * RTC membership prefix is structured as follows 907 * [prefix-len] [origin-as] [route-target] 908 * The route-target is encoded as RT ext-comms. 909 * Prefix-len may be 0, 32..96 910 * 911 * Note that pptr is not packet data - it is 912 * a buffer owned by our caller - therefore GET_* 913 * macros can not be used. 914 */ 915 static char * 916 bgp_rt_prefix_print(netdissect_options *ndo, 917 const u_char *pptr, 918 u_int plen) 919 { 920 /* allocate space for the largest possible string */ 921 char rtc_prefix_in_hex[sizeof("0000 0000 0000 0000")] = ""; 922 u_int rtc_prefix_in_hex_len = 0; 923 static char output[61]; /* max response string */ 924 /* allocate space for the largest possible string */ 925 char astostr[AS_STR_SIZE]; 926 uint16_t ec_type = 0; 927 u_int octet_count; 928 u_int i; 929 930 if (plen == 0) { 931 snprintf(output, sizeof(output), "route-target: 0:0/0"); 932 return (output); 933 } 934 935 /* hex representation of the prefix */ 936 octet_count = (plen+7)/8; 937 for (i=0; i<octet_count; i++) { 938 rtc_prefix_in_hex_len += snprintf(rtc_prefix_in_hex+rtc_prefix_in_hex_len, 939 sizeof(rtc_prefix_in_hex)-rtc_prefix_in_hex_len, 940 "%02x%s", *(pptr+i), 941 ((i%2 == 1) && (i<octet_count-1)) ? " " : ""); 942 } 943 944 if (plen < 16) { 945 /* 946 * The prefix is too short to include the full ext-comm type, 947 * so we have no way to parse it further. 948 */ 949 snprintf(output, sizeof(output), "route-target: partial-type: (%s/%d)", 950 rtc_prefix_in_hex, plen); 951 return (output); 952 } 953 954 /* 955 * get the ext-comm type 956 * Note: pptr references a static 8 octet buffer with unused bits set to 0, 957 * hence EXTRACT_*() macros are safe. 958 */ 959 ec_type = EXTRACT_BE_U_2(pptr); 960 switch (ec_type) { 961 case BGP_EXT_COM_RT_0: 962 /* 2-byte-AS:number fmt */ 963 snprintf(output, sizeof(output), "route-target: %u:%u/%d (%s)", 964 EXTRACT_BE_U_2(pptr+2), 965 EXTRACT_BE_U_4(pptr+4), 966 plen, rtc_prefix_in_hex); 967 break; 968 969 case BGP_EXT_COM_RT_1: 970 /* IP-address:AS fmt */ 971 snprintf(output, sizeof(output), "route-target: %u.%u.%u.%u:%u/%d (%s)", 972 *(pptr+2), *(pptr+3), *(pptr+4), *(pptr+5), 973 EXTRACT_BE_U_2(pptr+6), plen, rtc_prefix_in_hex); 974 break; 975 976 case BGP_EXT_COM_RT_2: 977 /* 4-byte-AS:number fmt */ 978 snprintf(output, sizeof(output), "route-target: %s:%u/%d (%s)", 979 as_printf(ndo, astostr, sizeof(astostr), EXTRACT_BE_U_4(pptr+2)), 980 EXTRACT_BE_U_2(pptr+6), plen, rtc_prefix_in_hex); 981 break; 982 983 default: 984 snprintf(output, sizeof(output), "route target: unknown-type(%04x) (%s/%d)", 985 ec_type, 986 rtc_prefix_in_hex, plen); 987 break; 988 } 989 return (output); 990 } 991 992 /* RFC 4684 */ 993 static int 994 decode_rt_routing_info(netdissect_options *ndo, 995 const u_char *pptr) 996 { 997 uint8_t route_target[8]; 998 u_int plen; 999 /* allocate space for the largest possible string */ 1000 char astostr[AS_STR_SIZE]; 1001 u_int num_octets; 1002 1003 /* NLRI "prefix length" from RFC 2858 Section 4. */ 1004 plen = GET_U_1(pptr); /* get prefix length */ 1005 1006 /* NLRI "prefix" (ibid), valid lengths are { 0, 32, 33, ..., 96 } bits. 1007 * RFC 4684 Section 4 defines the layout of "origin AS" and "route 1008 * target" fields inside the "prefix" depending on its length. 1009 */ 1010 if (0 == plen) { 1011 /* Without "origin AS", without "route target". */ 1012 ND_PRINT("\n\t default route target"); 1013 return 1; 1014 } 1015 1016 if (32 > plen) { 1017 ND_PRINT("\n\t (illegal prefix length)"); 1018 return -1; 1019 } 1020 1021 /* With at least "origin AS", possibly with "route target". */ 1022 as_printf(ndo, astostr, sizeof(astostr), GET_BE_U_4(pptr + 1)); 1023 1024 plen -= 32; /* adjust prefix length */ 1025 1026 if (64 < plen) { 1027 ND_PRINT("\n\t (illegal prefix length)"); 1028 return -1; 1029 } 1030 1031 /* From now on (plen + 7) / 8 evaluates to { 0, 1, 2, ..., 8 } 1032 * and gives the number of octets in the variable-length "route 1033 * target" field inside this NLRI "prefix". Look for it. 1034 */ 1035 memset(&route_target, 0, sizeof(route_target)); 1036 num_octets = (plen + 7) / 8; 1037 GET_CPY_BYTES(&route_target, pptr + 5, num_octets); 1038 /* If mask-len is not on octet boundary, ensure all extra bits are 0 */ 1039 if (plen % 8) { 1040 ((u_char *)&route_target)[num_octets - 1] &= 1041 ((0xff00 >> (plen % 8)) & 0xff); 1042 } 1043 ND_PRINT("\n\t origin AS: %s, %s", 1044 astostr, 1045 bgp_rt_prefix_print(ndo, (u_char *)&route_target, plen)); 1046 1047 return 5 + num_octets; 1048 } 1049 1050 static int 1051 decode_labeled_vpn_prefix4(netdissect_options *ndo, 1052 const u_char *pptr, char *buf, size_t buflen) 1053 { 1054 nd_ipv4 addr; 1055 u_int plen; 1056 1057 plen = GET_U_1(pptr); /* get prefix length */ 1058 1059 if ((24+64) > plen) 1060 return -1; 1061 1062 plen -= (24+64); /* adjust prefixlen - labellength - RD len*/ 1063 1064 if (32 < plen) 1065 return -1; 1066 1067 memset(&addr, 0, sizeof(addr)); 1068 GET_CPY_BYTES(&addr, pptr + 12, (plen + 7) / 8); 1069 if (plen % 8) { 1070 ((u_char *)&addr)[(plen + 7) / 8 - 1] &= 1071 ((0xff00 >> (plen % 8)) & 0xff); 1072 } 1073 /* the label may get offsetted by 4 bits so lets shift it right */ 1074 snprintf(buf, buflen, "RD: %s, %s/%u, label:%u %s", 1075 bgp_vpn_rd_print(ndo, pptr+4), 1076 ipaddr_string(ndo, (const u_char *)&addr), 1077 plen, 1078 GET_BE_U_3(pptr + 1)>>4, 1079 ((GET_U_1(pptr + 3) & 1) == 0) ? "(BOGUS: Bottom of Stack NOT set!)" : "(bottom)" ); 1080 1081 return 12 + (plen + 7) / 8; 1082 } 1083 1084 /* 1085 * +-------------------------------+ 1086 * | | 1087 * | RD:IPv4-address (12 octets) | 1088 * | | 1089 * +-------------------------------+ 1090 * | MDT Group-address (4 octets) | 1091 * +-------------------------------+ 1092 */ 1093 1094 #define MDT_VPN_NLRI_LEN 16 1095 1096 static int 1097 decode_mdt_vpn_nlri(netdissect_options *ndo, 1098 const u_char *pptr, char *buf, size_t buflen) 1099 { 1100 const u_char *rd; 1101 const u_char *vpn_ip; 1102 1103 /* if the NLRI is not predefined length, quit.*/ 1104 if (GET_U_1(pptr) != MDT_VPN_NLRI_LEN * 8) 1105 return -1; 1106 pptr++; 1107 1108 /* RD */ 1109 ND_TCHECK_8(pptr); 1110 rd = pptr; 1111 pptr += 8; 1112 1113 /* IPv4 address */ 1114 vpn_ip = pptr; 1115 pptr += sizeof(nd_ipv4); 1116 1117 /* MDT Group Address */ 1118 snprintf(buf, buflen, "RD: %s, VPN IP Address: %s, MC Group Address: %s", 1119 bgp_vpn_rd_print(ndo, rd), GET_IPADDR_STRING(vpn_ip), GET_IPADDR_STRING(pptr)); 1120 1121 return MDT_VPN_NLRI_LEN + 1; 1122 1123 trunc: 1124 return -2; 1125 } 1126 1127 #define BGP_MULTICAST_VPN_ROUTE_TYPE_INTRA_AS_I_PMSI 1 1128 #define BGP_MULTICAST_VPN_ROUTE_TYPE_INTER_AS_I_PMSI 2 1129 #define BGP_MULTICAST_VPN_ROUTE_TYPE_S_PMSI 3 1130 #define BGP_MULTICAST_VPN_ROUTE_TYPE_INTRA_AS_SEG_LEAF 4 1131 #define BGP_MULTICAST_VPN_ROUTE_TYPE_SOURCE_ACTIVE 5 1132 #define BGP_MULTICAST_VPN_ROUTE_TYPE_SHARED_TREE_JOIN 6 1133 #define BGP_MULTICAST_VPN_ROUTE_TYPE_SOURCE_TREE_JOIN 7 1134 1135 static const struct tok bgp_multicast_vpn_route_type_values[] = { 1136 { BGP_MULTICAST_VPN_ROUTE_TYPE_INTRA_AS_I_PMSI, "Intra-AS I-PMSI"}, 1137 { BGP_MULTICAST_VPN_ROUTE_TYPE_INTER_AS_I_PMSI, "Inter-AS I-PMSI"}, 1138 { BGP_MULTICAST_VPN_ROUTE_TYPE_S_PMSI, "S-PMSI"}, 1139 { BGP_MULTICAST_VPN_ROUTE_TYPE_INTRA_AS_SEG_LEAF, "Intra-AS Segment-Leaf"}, 1140 { BGP_MULTICAST_VPN_ROUTE_TYPE_SOURCE_ACTIVE, "Source-Active"}, 1141 { BGP_MULTICAST_VPN_ROUTE_TYPE_SHARED_TREE_JOIN, "Shared Tree Join"}, 1142 { BGP_MULTICAST_VPN_ROUTE_TYPE_SOURCE_TREE_JOIN, "Source Tree Join"}, 1143 { 0, NULL} 1144 }; 1145 1146 UNALIGNED_OK 1147 static int 1148 decode_multicast_vpn(netdissect_options *ndo, 1149 const u_char *pptr, char *buf, size_t buflen) 1150 { 1151 /* allocate space for the largest possible string */ 1152 char astostr[AS_STR_SIZE]; 1153 uint8_t route_type, route_length; 1154 u_int addr_length, sg_length; 1155 u_int offset; 1156 1157 route_type = GET_U_1(pptr); 1158 pptr++; 1159 route_length = GET_U_1(pptr); 1160 pptr++; 1161 1162 snprintf(buf, buflen, "Route-Type: %s (%u), length: %u", 1163 tok2str(bgp_multicast_vpn_route_type_values, 1164 "Unknown", route_type), 1165 route_type, route_length); 1166 1167 switch(route_type) { 1168 case BGP_MULTICAST_VPN_ROUTE_TYPE_INTRA_AS_I_PMSI: 1169 ND_TCHECK_LEN(pptr, BGP_VPN_RD_LEN); 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 calller 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 assumpetion 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, (len-ND_BYTES_BETWEEN(tptr, pptr)), 32); 2241 add_path6 = check_add_path(ndo, tptr, (len-ND_BYTES_BETWEEN(tptr, pptr)), 128); 2242 2243 while (tptr < pptr + len) { 2244 advance = bgp_nlri_print(ndo, af, safi, tptr, len, buf, sizeof(buf), 2245 add_path4, add_path6); 2246 if (advance == -2) 2247 goto trunc; 2248 if (advance < 0) 2249 break; 2250 tptr += advance; 2251 } 2252 break; 2253 2254 case BGPTYPE_MP_UNREACH_NLRI: 2255 ND_TCHECK_LEN(tptr, BGP_MP_NLRI_MINSIZE); 2256 ret = bgp_mp_af_print(ndo, tptr, tlen, &af, &safi); 2257 if (ret == -2) 2258 goto trunc; 2259 if (ret < 0) 2260 break; 2261 2262 if (len == BGP_MP_NLRI_MINSIZE) 2263 ND_PRINT("\n\t End-of-Rib Marker (empty NLRI)"); 2264 2265 tptr += 3; 2266 2267 add_path4 = check_add_path(ndo, tptr, (len-ND_BYTES_BETWEEN(tptr, pptr)), 32); 2268 add_path6 = check_add_path(ndo, tptr, (len-ND_BYTES_BETWEEN(tptr, pptr)), 128); 2269 2270 while (tptr < pptr + len) { 2271 advance = bgp_nlri_print(ndo, af, safi, tptr, len, buf, sizeof(buf), 2272 add_path4, add_path6); 2273 if (advance == -2) 2274 goto trunc; 2275 if (advance < 0) 2276 break; 2277 tptr += advance; 2278 } 2279 break; 2280 case BGPTYPE_EXTD_COMMUNITIES: 2281 if (len % 8) { 2282 ND_PRINT("invalid len"); 2283 break; 2284 } 2285 while (tlen != 0) { 2286 uint16_t extd_comm; 2287 2288 ND_TCHECK_2(tptr); 2289 if (tlen < 2) 2290 goto trunc; 2291 extd_comm=GET_BE_U_2(tptr); 2292 2293 ND_PRINT("\n\t %s (0x%04x), Flags [%s]", 2294 tok2str(bgp_extd_comm_subtype_values, 2295 "unknown extd community typecode", 2296 extd_comm), 2297 extd_comm, 2298 bittok2str(bgp_extd_comm_flag_values, "none", extd_comm)); 2299 2300 ND_TCHECK_8(tptr); 2301 if (tlen < 8) 2302 goto trunc; 2303 ND_PRINT(": "); 2304 bgp_extended_community_print(ndo, tptr); 2305 tlen -= 8; 2306 tptr += 8; 2307 } 2308 break; 2309 2310 case BGPTYPE_PMSI_TUNNEL: 2311 { 2312 uint8_t tunnel_type, flags; 2313 2314 ND_TCHECK_5(tptr); 2315 if (tlen < 5) 2316 goto trunc; 2317 flags = GET_U_1(tptr); 2318 tunnel_type = GET_U_1(tptr + 1); 2319 2320 ND_PRINT("\n\t Tunnel-type %s (%u), Flags [%s], MPLS Label %u", 2321 tok2str(bgp_pmsi_tunnel_values, "Unknown", tunnel_type), 2322 tunnel_type, 2323 bittok2str(bgp_pmsi_flag_values, "none", flags), 2324 GET_BE_U_3(tptr + 2)>>4); 2325 2326 tptr +=5; 2327 tlen -= 5; 2328 2329 switch (tunnel_type) { 2330 case BGP_PMSI_TUNNEL_PIM_SM: /* fall through */ 2331 case BGP_PMSI_TUNNEL_PIM_BIDIR: 2332 ND_PRINT("\n\t Sender %s, P-Group %s", 2333 GET_IPADDR_STRING(tptr), 2334 GET_IPADDR_STRING(tptr+4)); 2335 break; 2336 2337 case BGP_PMSI_TUNNEL_PIM_SSM: 2338 ND_PRINT("\n\t Root-Node %s, P-Group %s", 2339 GET_IPADDR_STRING(tptr), 2340 GET_IPADDR_STRING(tptr+4)); 2341 break; 2342 case BGP_PMSI_TUNNEL_INGRESS: 2343 ND_PRINT("\n\t Tunnel-Endpoint %s", 2344 GET_IPADDR_STRING(tptr)); 2345 break; 2346 case BGP_PMSI_TUNNEL_LDP_P2MP: /* fall through */ 2347 case BGP_PMSI_TUNNEL_LDP_MP2MP: 2348 ND_PRINT("\n\t Root-Node %s, LSP-ID 0x%08x", 2349 GET_IPADDR_STRING(tptr), 2350 GET_BE_U_4(tptr + 4)); 2351 break; 2352 case BGP_PMSI_TUNNEL_RSVP_P2MP: 2353 ND_PRINT("\n\t Extended-Tunnel-ID %s, P2MP-ID 0x%08x", 2354 GET_IPADDR_STRING(tptr), 2355 GET_BE_U_4(tptr + 4)); 2356 break; 2357 default: 2358 if (ndo->ndo_vflag <= 1) { 2359 print_unknown_data(ndo, tptr, "\n\t ", tlen); 2360 } 2361 } 2362 break; 2363 } 2364 case BGPTYPE_AIGP: 2365 { 2366 uint8_t type; 2367 uint16_t length; 2368 2369 while (tlen >= 3) { 2370 type = GET_U_1(tptr); 2371 length = GET_BE_U_2(tptr + 1); 2372 tptr += 3; 2373 tlen -= 3; 2374 2375 ND_PRINT("\n\t %s TLV (%u), length %u", 2376 tok2str(bgp_aigp_values, "Unknown", type), 2377 type, length); 2378 2379 if (length < 3) 2380 goto trunc; 2381 length -= 3; 2382 2383 /* 2384 * Check if we can read the TLV data. 2385 */ 2386 if (tlen < length) 2387 goto trunc; 2388 2389 switch (type) { 2390 2391 case BGP_AIGP_TLV: 2392 if (length < 8) 2393 goto trunc; 2394 ND_PRINT(", metric %" PRIu64, 2395 GET_BE_U_8(tptr)); 2396 break; 2397 2398 default: 2399 if (ndo->ndo_vflag <= 1) { 2400 print_unknown_data(ndo, tptr,"\n\t ", length); 2401 } 2402 } 2403 2404 tptr += length; 2405 tlen -= length; 2406 } 2407 break; 2408 } 2409 case BGPTYPE_ATTR_SET: 2410 ND_TCHECK_4(tptr); 2411 if (len < 4) 2412 goto trunc; 2413 ND_PRINT("\n\t Origin AS: %s", 2414 as_printf(ndo, astostr, sizeof(astostr), GET_BE_U_4(tptr))); 2415 tptr += 4; 2416 len -= 4; 2417 2418 while (len) { 2419 u_int aflags, alenlen, alen; 2420 2421 ND_TCHECK_2(tptr); 2422 if (len < 2) { 2423 ND_PRINT(" [path attr too short]"); 2424 tptr += len; 2425 break; 2426 } 2427 aflags = GET_U_1(tptr); 2428 atype = GET_U_1(tptr + 1); 2429 tptr += 2; 2430 len -= 2; 2431 alenlen = bgp_attr_lenlen(aflags, tptr); 2432 ND_TCHECK_LEN(tptr, alenlen); 2433 if (len < alenlen) { 2434 ND_PRINT(" [path attr too short]"); 2435 tptr += len; 2436 break; 2437 } 2438 alen = bgp_attr_len(aflags, tptr); 2439 tptr += alenlen; 2440 len -= alenlen; 2441 2442 ND_PRINT("\n\t %s (%u), length: %u", 2443 tok2str(bgp_attr_values, 2444 "Unknown Attribute", atype), 2445 atype, 2446 alen); 2447 2448 if (aflags) { 2449 ND_PRINT(", Flags [%s%s%s%s", 2450 aflags & 0x80 ? "O" : "", 2451 aflags & 0x40 ? "T" : "", 2452 aflags & 0x20 ? "P" : "", 2453 aflags & 0x10 ? "E" : ""); 2454 if (aflags & 0xf) 2455 ND_PRINT("+%x", aflags & 0xf); 2456 ND_PRINT("]"); 2457 } 2458 ND_PRINT(": "); 2459 if (len < alen) { 2460 ND_PRINT(" [path attr too short]"); 2461 tptr += len; 2462 break; 2463 } 2464 /* 2465 * The protocol encoding per se allows ATTR_SET to be nested 2466 * as many times as the message can accommodate. This printer 2467 * used to be able to recurse into ATTR_SET contents until the 2468 * stack exhaustion, but now there is a limit on that (if live 2469 * protocol exchange goes that many levels deep, something is 2470 * probably wrong anyway). Feel free to refine this value if 2471 * you can find the spec with respective normative text. 2472 */ 2473 if (attr_set_level == 10) 2474 ND_PRINT("(too many nested levels, not recursing)"); 2475 else if (!bgp_attr_print(ndo, atype, tptr, alen, attr_set_level + 1)) 2476 return 0; 2477 tptr += alen; 2478 len -= alen; 2479 } 2480 break; 2481 2482 case BGPTYPE_LARGE_COMMUNITY: 2483 if (len == 0 || len % 12) { 2484 ND_PRINT("invalid len"); 2485 break; 2486 } 2487 ND_PRINT("\n\t "); 2488 while (len != 0) { 2489 ND_PRINT("%u:%u:%u%s", 2490 GET_BE_U_4(tptr), 2491 GET_BE_U_4(tptr + 4), 2492 GET_BE_U_4(tptr + 8), 2493 (len > 12) ? ", " : ""); 2494 tptr += 12; 2495 /* 2496 * len will always be a multiple of 12, as per the above, 2497 * so this will never underflow. 2498 */ 2499 len -= 12; 2500 } 2501 break; 2502 default: 2503 ND_TCHECK_LEN(pptr, len); 2504 ND_PRINT("\n\t no Attribute %u decoder", atype); /* we have no decoder for the attribute */ 2505 if (ndo->ndo_vflag <= 1) 2506 print_unknown_data(ndo, pptr, "\n\t ", len); 2507 break; 2508 } 2509 done: 2510 if (ndo->ndo_vflag > 1 && len) { /* omit zero length attributes*/ 2511 ND_TCHECK_LEN(pptr, len); 2512 print_unknown_data(ndo, pptr, "\n\t ", len); 2513 } 2514 return 1; 2515 2516 trunc: 2517 return 0; 2518 } 2519 2520 static void 2521 bgp_capabilities_print(netdissect_options *ndo, 2522 const u_char *opt, u_int caps_len) 2523 { 2524 /* allocate space for the largest possible string */ 2525 char astostr[AS_STR_SIZE]; 2526 u_int cap_type, cap_len, tcap_len, cap_offset; 2527 u_int i = 0; 2528 2529 while (i < caps_len) { 2530 ND_TCHECK_LEN(opt + i, BGP_CAP_HEADER_SIZE); 2531 cap_type=GET_U_1(opt + i); 2532 cap_len=GET_U_1(opt + i + 1); 2533 ND_PRINT("\n\t %s (%u), length: %u", 2534 tok2str(bgp_capcode_values, "Unknown", cap_type), 2535 cap_type, 2536 cap_len); 2537 ND_TCHECK_LEN(opt + 2 + i, cap_len); 2538 switch (cap_type) { 2539 case BGP_CAPCODE_MP: 2540 /* AFI (16 bits), Reserved (8 bits), SAFI (8 bits) */ 2541 if (cap_len < 4) { 2542 ND_PRINT(" (too short, < 4)"); 2543 return; 2544 } 2545 ND_PRINT("\n\t\tAFI %s (%u), SAFI %s (%u)", 2546 tok2str(af_values, "Unknown", GET_BE_U_2(opt + i + 2)), 2547 GET_BE_U_2(opt + i + 2), 2548 tok2str(bgp_safi_values, "Unknown", GET_U_1(opt + i + 5)), 2549 GET_U_1(opt + i + 5)); 2550 break; 2551 case BGP_CAPCODE_ML: 2552 cap_offset = 2; 2553 tcap_len = cap_len; 2554 while (tcap_len >= 4) { 2555 ND_PRINT( "\n\t\tAFI %s (%u), SAFI %s (%u), Count: %u", 2556 tok2str(af_values, "Unknown", 2557 GET_BE_U_2(opt + i + cap_offset)), 2558 GET_BE_U_2(opt + i + cap_offset), 2559 tok2str(bgp_safi_values, "Unknown", 2560 GET_U_1(opt + i + cap_offset + 2)), 2561 GET_U_1(opt + i + cap_offset + 2), 2562 GET_U_1(opt + i + cap_offset + 3)); 2563 tcap_len -= 4; 2564 cap_offset += 4; 2565 } 2566 break; 2567 case BGP_CAPCODE_RESTART: 2568 /* Restart Flags (4 bits), Restart Time in seconds (12 bits) */ 2569 if (cap_len < 2) { 2570 ND_PRINT(" (too short, < 2)"); 2571 return; 2572 } 2573 tcap_len=cap_len; 2574 ND_PRINT("\n\t\tRestart Flags: [%s], Restart Time %us", 2575 ((GET_U_1(opt + i + 2))&0x80) ? "R" : "none", 2576 GET_BE_U_2(opt + i + 2)&0xfff); 2577 tcap_len-=2; 2578 cap_offset=4; 2579 while(tcap_len>=4) { 2580 ND_PRINT("\n\t\t AFI %s (%u), SAFI %s (%u), Forwarding state preserved: %s", 2581 tok2str(af_values,"Unknown", 2582 GET_BE_U_2(opt + i + cap_offset)), 2583 GET_BE_U_2(opt + i + cap_offset), 2584 tok2str(bgp_safi_values,"Unknown", 2585 GET_U_1(opt + i + cap_offset + 2)), 2586 GET_U_1(opt + (i + cap_offset + 2)), 2587 ((GET_U_1(opt + (i + cap_offset + 3)))&0x80) ? "yes" : "no" ); 2588 tcap_len -= 4; 2589 cap_offset += 4; 2590 } 2591 break; 2592 case BGP_CAPCODE_RR: 2593 case BGP_CAPCODE_LLGR: 2594 case BGP_CAPCODE_RR_CISCO: 2595 break; 2596 case BGP_CAPCODE_AS_NEW: 2597 /* 2598 * Extract the 4 byte AS number encoded. 2599 */ 2600 if (cap_len < 4) { 2601 ND_PRINT(" (too short, < 4)"); 2602 return; 2603 } 2604 ND_PRINT("\n\t\t 4 Byte AS %s", 2605 as_printf(ndo, astostr, sizeof(astostr), 2606 GET_BE_U_4(opt + i + 2))); 2607 break; 2608 case BGP_CAPCODE_ADD_PATH: 2609 if (cap_len == 0) { 2610 ND_PRINT(" (bogus)"); /* length */ 2611 break; 2612 } 2613 tcap_len=cap_len; 2614 cap_offset=2; 2615 while (tcap_len != 0) { 2616 if (tcap_len < 4) { 2617 nd_print_invalid(ndo); 2618 break; 2619 } 2620 ND_PRINT("\n\t\tAFI %s (%u), SAFI %s (%u), Send/Receive: %s", 2621 tok2str(af_values,"Unknown",GET_BE_U_2(opt + i + cap_offset)), 2622 GET_BE_U_2(opt + i + cap_offset), 2623 tok2str(bgp_safi_values,"Unknown",GET_U_1(opt + i + cap_offset + 2)), 2624 GET_U_1(opt + (i + cap_offset + 2)), 2625 tok2str(bgp_add_path_recvsend,"Bogus (0x%02x)",GET_U_1(opt + i + cap_offset + 3)) 2626 ); 2627 tcap_len -= 4; 2628 cap_offset += 4; 2629 } 2630 break; 2631 default: 2632 ND_PRINT("\n\t\tno decoder for Capability %u", 2633 cap_type); 2634 if (ndo->ndo_vflag <= 1) 2635 print_unknown_data(ndo, opt + i + 2, "\n\t\t", 2636 cap_len); 2637 break; 2638 } 2639 if (ndo->ndo_vflag > 1 && cap_len != 0) { 2640 print_unknown_data(ndo, opt + i + 2, "\n\t\t", cap_len); 2641 } 2642 i += BGP_CAP_HEADER_SIZE + cap_len; 2643 } 2644 return; 2645 2646 trunc: 2647 nd_print_trunc(ndo); 2648 } 2649 2650 static void 2651 bgp_open_print(netdissect_options *ndo, 2652 const u_char *dat, u_int length) 2653 { 2654 /* allocate space for the largest possible string */ 2655 char astostr[AS_STR_SIZE]; 2656 const struct bgp_open *bgp_open_header; 2657 u_int optslen; 2658 const struct bgp_opt *bgpopt; 2659 const u_char *opt; 2660 u_int i; 2661 2662 ND_TCHECK_LEN(dat, BGP_OPEN_SIZE); 2663 if (length < BGP_OPEN_SIZE) 2664 goto trunc; 2665 2666 bgp_open_header = (const struct bgp_open *)dat; 2667 2668 ND_PRINT("\n\t Version %u, ", 2669 GET_U_1(bgp_open_header->bgpo_version)); 2670 ND_PRINT("my AS %s, ", 2671 as_printf(ndo, astostr, sizeof(astostr), GET_BE_U_2(bgp_open_header->bgpo_myas))); 2672 ND_PRINT("Holdtime %us, ", 2673 GET_BE_U_2(bgp_open_header->bgpo_holdtime)); 2674 ND_PRINT("ID %s", GET_IPADDR_STRING(bgp_open_header->bgpo_id)); 2675 optslen = GET_U_1(bgp_open_header->bgpo_optlen); 2676 ND_PRINT("\n\t Optional parameters, length: %u", optslen); 2677 2678 opt = dat + BGP_OPEN_SIZE; 2679 length -= BGP_OPEN_SIZE; 2680 2681 i = 0; 2682 while (i < optslen) { 2683 uint8_t opt_type, opt_len; 2684 2685 ND_TCHECK_LEN(opt + i, BGP_OPT_SIZE); 2686 if (length < BGP_OPT_SIZE + i) 2687 goto trunc; 2688 bgpopt = (const struct bgp_opt *)(opt + i); 2689 opt_type = GET_U_1(bgpopt->bgpopt_type); 2690 opt_len = GET_U_1(bgpopt->bgpopt_len); 2691 if (BGP_OPT_SIZE + i + opt_len > optslen) { 2692 ND_PRINT("\n\t Option %u, length: %u, goes past the end of the options", 2693 opt_type, opt_len); 2694 break; 2695 } 2696 2697 ND_PRINT("\n\t Option %s (%u), length: %u", 2698 tok2str(bgp_opt_values,"Unknown",opt_type), 2699 opt_type, 2700 opt_len); 2701 2702 /* now let's decode the options we know*/ 2703 switch(opt_type) { 2704 2705 case BGP_OPT_CAP: 2706 bgp_capabilities_print(ndo, opt + BGP_OPT_SIZE + i, 2707 opt_len); 2708 break; 2709 2710 case BGP_OPT_AUTH: 2711 default: 2712 ND_PRINT("\n\t no decoder for option %u", 2713 opt_type); 2714 break; 2715 } 2716 i += BGP_OPT_SIZE + opt_len; 2717 } 2718 return; 2719 trunc: 2720 nd_print_trunc(ndo); 2721 } 2722 2723 static void 2724 bgp_update_print(netdissect_options *ndo, 2725 const u_char *dat, u_int length) 2726 { 2727 const u_char *p; 2728 u_int withdrawn_routes_len; 2729 char buf[MAXHOSTNAMELEN + 100]; 2730 int wpfx; 2731 u_int len; 2732 int i; 2733 int add_path; 2734 u_int path_id = 0; 2735 2736 ND_TCHECK_LEN(dat, BGP_SIZE); 2737 if (length < BGP_SIZE) 2738 goto trunc; 2739 p = dat + BGP_SIZE; 2740 length -= BGP_SIZE; 2741 2742 /* Unfeasible routes */ 2743 ND_TCHECK_2(p); 2744 if (length < 2) 2745 goto trunc; 2746 withdrawn_routes_len = GET_BE_U_2(p); 2747 p += 2; 2748 length -= 2; 2749 if (withdrawn_routes_len > 1) { 2750 /* 2751 * Without keeping state from the original NLRI message, 2752 * it's not possible to tell if this a v4 or v6 route, 2753 * so only try to decode it if we're not v6 enabled. 2754 */ 2755 ND_TCHECK_LEN(p, withdrawn_routes_len); 2756 if (length < withdrawn_routes_len) 2757 goto trunc; 2758 ND_PRINT("\n\t Withdrawn routes:"); 2759 add_path = check_add_path(ndo, p, withdrawn_routes_len, 32); 2760 while (withdrawn_routes_len != 0) { 2761 if (add_path) { 2762 if (withdrawn_routes_len < 4) { 2763 p += withdrawn_routes_len; 2764 length -= withdrawn_routes_len; 2765 break; 2766 } 2767 path_id = GET_BE_U_4(p); 2768 p += 4; 2769 length -= 4; 2770 withdrawn_routes_len -= 4; 2771 } 2772 wpfx = decode_prefix4(ndo, p, withdrawn_routes_len, buf, sizeof(buf)); 2773 if (wpfx == -1) { 2774 ND_PRINT("\n\t (illegal prefix length)"); 2775 break; 2776 } else if (wpfx == -2) 2777 goto trunc; /* bytes left, but not enough */ 2778 else { 2779 ND_PRINT("\n\t %s", buf); 2780 if (add_path) { 2781 ND_PRINT(" Path Id: %u", path_id); 2782 } 2783 p += wpfx; 2784 length -= wpfx; 2785 withdrawn_routes_len -= wpfx; 2786 } 2787 } 2788 } else { 2789 ND_TCHECK_LEN(p, withdrawn_routes_len); 2790 if (length < withdrawn_routes_len) 2791 goto trunc; 2792 p += withdrawn_routes_len; 2793 length -= withdrawn_routes_len; 2794 } 2795 2796 ND_TCHECK_2(p); 2797 if (length < 2) 2798 goto trunc; 2799 len = GET_BE_U_2(p); 2800 p += 2; 2801 length -= 2; 2802 2803 if (withdrawn_routes_len == 0 && len == 0 && length == 0) { 2804 /* No withdrawn routes, no path attributes, no NLRI */ 2805 ND_PRINT("\n\t End-of-Rib Marker (empty NLRI)"); 2806 return; 2807 } 2808 2809 if (len) { 2810 /* Make sure the path attributes don't go past the end of the packet */ 2811 if (length < len) 2812 goto trunc; 2813 /* do something more useful!*/ 2814 while (len) { 2815 uint8_t aflags, atype, alenlen; 2816 uint16_t alen; 2817 2818 ND_TCHECK_2(p); 2819 if (length < 2) 2820 goto trunc; 2821 if (len < 2) { 2822 ND_PRINT("\n\t [path attrs too short]"); 2823 p += len; 2824 length -= len; 2825 break; 2826 } 2827 aflags = GET_U_1(p); 2828 atype = GET_U_1(p + 1); 2829 p += 2; 2830 len -= 2; 2831 length -= 2; 2832 alenlen = bgp_attr_lenlen(aflags, p); 2833 ND_TCHECK_LEN(p, alenlen); 2834 if (length < alenlen) 2835 goto trunc; 2836 if (len < alenlen) { 2837 ND_PRINT("\n\t [path attrs too short]"); 2838 p += len; 2839 length -= len; 2840 break; 2841 } 2842 alen = bgp_attr_len(aflags, p); 2843 p += alenlen; 2844 len -= alenlen; 2845 length -= alenlen; 2846 2847 ND_PRINT("\n\t %s (%u), length: %u", 2848 tok2str(bgp_attr_values, "Unknown Attribute", atype), 2849 atype, 2850 alen); 2851 2852 if (aflags) { 2853 ND_PRINT(", Flags [%s%s%s%s", 2854 aflags & 0x80 ? "O" : "", 2855 aflags & 0x40 ? "T" : "", 2856 aflags & 0x20 ? "P" : "", 2857 aflags & 0x10 ? "E" : ""); 2858 if (aflags & 0xf) 2859 ND_PRINT("+%x", aflags & 0xf); 2860 ND_PRINT("]: "); 2861 } 2862 if (len < alen) { 2863 ND_PRINT(" [path attrs too short]"); 2864 p += len; 2865 length -= len; 2866 break; 2867 } 2868 if (length < alen) 2869 goto trunc; 2870 if (!bgp_attr_print(ndo, atype, p, alen, 0)) 2871 goto trunc; 2872 p += alen; 2873 len -= alen; 2874 length -= alen; 2875 } 2876 } 2877 2878 if (length) { 2879 add_path = check_add_path(ndo, p, length, 32); 2880 ND_PRINT("\n\t Updated routes:"); 2881 while (length != 0) { 2882 if (add_path) { 2883 ND_TCHECK_4(p); 2884 if (length < 4) 2885 goto trunc; 2886 path_id = GET_BE_U_4(p); 2887 p += 4; 2888 length -= 4; 2889 } 2890 i = decode_prefix4(ndo, p, length, buf, sizeof(buf)); 2891 if (i == -1) { 2892 ND_PRINT("\n\t (illegal prefix length)"); 2893 break; 2894 } else if (i == -2) 2895 goto trunc; /* bytes left, but not enough */ 2896 else { 2897 ND_PRINT("\n\t %s", buf); 2898 if (add_path) { 2899 ND_PRINT(" Path Id: %u", path_id); 2900 } 2901 p += i; 2902 length -= i; 2903 } 2904 } 2905 } 2906 return; 2907 trunc: 2908 nd_print_trunc(ndo); 2909 } 2910 2911 static void 2912 bgp_notification_print(netdissect_options *ndo, 2913 const u_char *dat, u_int length) 2914 { 2915 const struct bgp_notification *bgp_notification_header; 2916 const u_char *tptr; 2917 uint8_t bgpn_major, bgpn_minor; 2918 2919 ND_TCHECK_LEN(dat, BGP_NOTIFICATION_SIZE); 2920 if (length<BGP_NOTIFICATION_SIZE) 2921 return; 2922 2923 bgp_notification_header = (const struct bgp_notification *)dat; 2924 bgpn_major = GET_U_1(bgp_notification_header->bgpn_major); 2925 bgpn_minor = GET_U_1(bgp_notification_header->bgpn_minor); 2926 2927 ND_PRINT(", %s (%u)", 2928 tok2str(bgp_notify_major_values, "Unknown Error", 2929 bgpn_major), 2930 bgpn_major); 2931 2932 switch (bgpn_major) { 2933 2934 case BGP_NOTIFY_MAJOR_MSG: 2935 ND_PRINT(", subcode %s (%u)", 2936 tok2str(bgp_notify_minor_msg_values, "Unknown", 2937 bgpn_minor), 2938 bgpn_minor); 2939 break; 2940 case BGP_NOTIFY_MAJOR_OPEN: 2941 ND_PRINT(", subcode %s (%u)", 2942 tok2str(bgp_notify_minor_open_values, "Unknown", 2943 bgpn_minor), 2944 bgpn_minor); 2945 break; 2946 case BGP_NOTIFY_MAJOR_UPDATE: 2947 ND_PRINT(", subcode %s (%u)", 2948 tok2str(bgp_notify_minor_update_values, "Unknown", 2949 bgpn_minor), 2950 bgpn_minor); 2951 break; 2952 case BGP_NOTIFY_MAJOR_FSM: 2953 ND_PRINT(" subcode %s (%u)", 2954 tok2str(bgp_notify_minor_fsm_values, "Unknown", 2955 bgpn_minor), 2956 bgpn_minor); 2957 break; 2958 case BGP_NOTIFY_MAJOR_CAP: 2959 ND_PRINT(" subcode %s (%u)", 2960 tok2str(bgp_notify_minor_cap_values, "Unknown", 2961 bgpn_minor), 2962 bgpn_minor); 2963 break; 2964 case BGP_NOTIFY_MAJOR_CEASE: 2965 ND_PRINT(", subcode %s (%u)", 2966 tok2str(bgp_notify_minor_cease_values, "Unknown", 2967 bgpn_minor), 2968 bgpn_minor); 2969 2970 /* RFC 4486 mentions optionally 7 bytes 2971 * for the maxprefix subtype, which may contain AFI, SAFI and MAXPREFIXES 2972 */ 2973 if(bgpn_minor == BGP_NOTIFY_MINOR_CEASE_MAXPRFX && length >= BGP_NOTIFICATION_SIZE + 7) { 2974 tptr = dat + BGP_NOTIFICATION_SIZE; 2975 ND_PRINT(", AFI %s (%u), SAFI %s (%u), Max Prefixes: %u", 2976 tok2str(af_values, "Unknown", GET_BE_U_2(tptr)), 2977 GET_BE_U_2(tptr), 2978 tok2str(bgp_safi_values, "Unknown", GET_U_1((tptr + 2))), 2979 GET_U_1((tptr + 2)), 2980 GET_BE_U_4(tptr + 3)); 2981 } 2982 /* 2983 * RFC 9003 describes a method to send a communication 2984 * intended for human consumption regarding the Administrative Shutdown 2985 */ 2986 if ((bgpn_minor == BGP_NOTIFY_MINOR_CEASE_SHUT || 2987 bgpn_minor == BGP_NOTIFY_MINOR_CEASE_RESET) && 2988 length >= BGP_NOTIFICATION_SIZE + 1) { 2989 tptr = dat + BGP_NOTIFICATION_SIZE; 2990 uint8_t shutdown_comm_length = GET_U_1(tptr); 2991 uint8_t remainder_offset = 0; 2992 /* garbage, hexdump it all */ 2993 if (shutdown_comm_length > length - (BGP_NOTIFICATION_SIZE + 1)) { 2994 ND_PRINT(", invalid Shutdown Communication length"); 2995 } 2996 else if (shutdown_comm_length == 0) { 2997 ND_PRINT(", empty Shutdown Communication"); 2998 remainder_offset += 1; 2999 } 3000 /* a proper shutdown communication */ 3001 else { 3002 ND_PRINT(", Shutdown Communication (length: %u): \"", shutdown_comm_length); 3003 (void)nd_printn(ndo, tptr+1, shutdown_comm_length, NULL); 3004 ND_PRINT("\""); 3005 remainder_offset += shutdown_comm_length + 1; 3006 } 3007 /* if there is trailing data, hexdump it */ 3008 if(length - (remainder_offset + BGP_NOTIFICATION_SIZE) > 0) { 3009 ND_PRINT(", Data: (length: %u)", length - (remainder_offset + BGP_NOTIFICATION_SIZE)); 3010 hex_print(ndo, "\n\t\t", tptr + remainder_offset, length - (remainder_offset + BGP_NOTIFICATION_SIZE)); 3011 } 3012 } 3013 break; 3014 default: 3015 break; 3016 } 3017 3018 return; 3019 trunc: 3020 nd_print_trunc(ndo); 3021 } 3022 3023 static void 3024 bgp_route_refresh_print(netdissect_options *ndo, 3025 const u_char *pptr, u_int len) 3026 { 3027 const struct bgp_route_refresh *bgp_route_refresh_header; 3028 3029 ND_TCHECK_LEN(pptr, BGP_ROUTE_REFRESH_SIZE); 3030 3031 /* some little sanity checking */ 3032 if (len<BGP_ROUTE_REFRESH_SIZE) 3033 return; 3034 3035 bgp_route_refresh_header = (const struct bgp_route_refresh *)pptr; 3036 3037 ND_PRINT("\n\t AFI %s (%u), SAFI %s (%u)", 3038 tok2str(af_values,"Unknown", 3039 GET_BE_U_2(bgp_route_refresh_header->afi)), 3040 GET_BE_U_2(bgp_route_refresh_header->afi), 3041 tok2str(bgp_safi_values,"Unknown", 3042 GET_U_1(bgp_route_refresh_header->safi)), 3043 GET_U_1(bgp_route_refresh_header->safi)); 3044 3045 if (ndo->ndo_vflag > 1) { 3046 ND_TCHECK_LEN(pptr, len); 3047 print_unknown_data(ndo, pptr, "\n\t ", len); 3048 } 3049 3050 return; 3051 trunc: 3052 nd_print_trunc(ndo); 3053 } 3054 3055 static int 3056 bgp_pdu_print(netdissect_options *ndo, 3057 const u_char *dat, u_int length) 3058 { 3059 const struct bgp *bgp_header; 3060 uint8_t bgp_type; 3061 3062 ND_TCHECK_LEN(dat, BGP_SIZE); 3063 bgp_header = (const struct bgp *)dat; 3064 bgp_type = GET_U_1(bgp_header->bgp_type); 3065 3066 ND_PRINT("\n\t%s Message (%u), length: %u", 3067 tok2str(bgp_msg_values, "Unknown", bgp_type), 3068 bgp_type, 3069 length); 3070 3071 switch (bgp_type) { 3072 case BGP_OPEN: 3073 bgp_open_print(ndo, dat, length); 3074 break; 3075 case BGP_UPDATE: 3076 bgp_update_print(ndo, dat, length); 3077 break; 3078 case BGP_NOTIFICATION: 3079 bgp_notification_print(ndo, dat, length); 3080 break; 3081 case BGP_KEEPALIVE: 3082 break; 3083 case BGP_ROUTE_REFRESH: 3084 bgp_route_refresh_print(ndo, dat, length); 3085 break; 3086 default: 3087 /* we have no decoder for the BGP message */ 3088 ND_TCHECK_LEN(dat, length); 3089 ND_PRINT("\n\t no Message %u decoder", bgp_type); 3090 print_unknown_data(ndo, dat, "\n\t ", length); 3091 break; 3092 } 3093 return 1; 3094 trunc: 3095 nd_print_trunc(ndo); 3096 return 0; 3097 } 3098 3099 void 3100 bgp_print(netdissect_options *ndo, 3101 const u_char *dat, u_int length _U_) 3102 { 3103 const u_char *p; 3104 const u_char *ep = ndo->ndo_snapend; 3105 const u_char *start; 3106 const u_char marker[] = { 3107 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 3108 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 3109 }; 3110 const struct bgp *bgp_header; 3111 uint16_t hlen; 3112 3113 ndo->ndo_protocol = "bgp"; 3114 ND_PRINT(": BGP"); 3115 3116 if (ndo->ndo_vflag < 1) /* lets be less chatty */ 3117 return; 3118 3119 p = dat; 3120 start = p; 3121 while (p < ep) { 3122 if (!ND_TTEST_1(p)) 3123 break; 3124 if (GET_U_1(p) != 0xff) { 3125 p++; 3126 continue; 3127 } 3128 3129 if (!ND_TTEST_LEN(p, sizeof(marker))) 3130 break; 3131 if (memcmp(p, marker, sizeof(marker)) != 0) { 3132 p++; 3133 continue; 3134 } 3135 3136 /* found BGP header */ 3137 ND_TCHECK_LEN(p, BGP_SIZE); 3138 bgp_header = (const struct bgp *)p; 3139 3140 if (start != p) 3141 nd_print_trunc(ndo); 3142 3143 hlen = GET_BE_U_2(bgp_header->bgp_len); 3144 if (hlen < BGP_SIZE) { 3145 ND_PRINT("\nmessage length %u < %u", hlen, BGP_SIZE); 3146 nd_print_invalid(ndo); 3147 break; 3148 } 3149 3150 if (ND_TTEST_LEN(p, hlen)) { 3151 if (!bgp_pdu_print(ndo, p, hlen)) 3152 return; 3153 p += hlen; 3154 start = p; 3155 } else { 3156 ND_PRINT("\n[|BGP %s]", 3157 tok2str(bgp_msg_values, 3158 "Unknown Message Type", 3159 GET_U_1(bgp_header->bgp_type))); 3160 break; 3161 } 3162 } 3163 3164 return; 3165 3166 trunc: 3167 nd_print_trunc(ndo); 3168 } 3169