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