X-Git-Url: https://git.tcpdump.org/tcpdump/blobdiff_plain/5ef0bcb5edd748de9d9af13c40da0395dfdd94e8..refs/heads/master:/print-bgp.c diff --git a/print-bgp.c b/print-bgp.c index ab3e2835..bb302545 100644 --- a/print-bgp.c +++ b/print-bgp.c @@ -34,9 +34,7 @@ /* specification: RFC 4271 */ -#ifdef HAVE_CONFIG_H #include -#endif #include "netdissect-stdinc.h" @@ -80,13 +78,15 @@ struct bgp_open { nd_uint16_t bgpo_holdtime; nd_uint32_t bgpo_id; nd_uint8_t bgpo_optlen; + nd_uint8_t bgpo_opttype; /* RFC9072 */ + nd_uint16_t bgpo_optlen_extended; /* RFC9072 */ /* options should follow */ }; #define BGP_OPEN_SIZE 29 /* unaligned */ struct bgp_opt { - nd_uint8_t bgpopt_type; - nd_uint8_t bgpopt_len; + nd_uint8_t bgpopt_type; + nd_uint16_t bgpopt_len; /* Can be one or two bytes, depending on RFC9072 */ /* variable length */ }; #define BGP_OPT_SIZE 2 /* some compilers may pad to 4 bytes */ @@ -101,15 +101,47 @@ struct bgp_notification { }; #define BGP_NOTIFICATION_SIZE 21 /* unaligned */ +struct bgp_route_refresh_orf { + nd_uint8_t refresh; + nd_uint8_t type; + nd_uint16_t len; +}; + struct bgp_route_refresh { nd_byte bgp_marker[16]; nd_uint16_t len; nd_uint8_t type; /* No padding after this; afi is, in fact, not aligned */ nd_uint16_t afi; - nd_uint8_t res; + nd_uint8_t subtype; nd_uint8_t safi; }; + +static const struct tok bgp_orf_refresh_type[] = { + { 1, "Immediate"}, + { 2, "Defer"}, + { 0, NULL } +}; + +static const struct tok bgp_orf_type[] = { + { 64, "Address Prefix ORF"}, + { 65, "CP-ORF"}, + { 0, NULL } +}; + #define BGP_ROUTE_REFRESH_SIZE 23 +#define BGP_ROUTE_REFRESH_SIZE_ORF BGP_ROUTE_REFRESH_SIZE + 5 +#define BGP_ROUTE_REFRESH_SUBTYPE_NORMAL 0 +#define BGP_ROUTE_REFRESH_SUBTYPE_BORR 1 +#define BGP_ROUTE_REFRESH_SUBTYPE_EORR 2 +#define BGP_ROUTE_REFRESH_SUBTYPE_RESERVED 255 + +static const struct tok bgp_route_refresh_subtype_values[] = { + {BGP_ROUTE_REFRESH_SUBTYPE_NORMAL, "Normal route refresh request"}, + {BGP_ROUTE_REFRESH_SUBTYPE_BORR, + "Demarcation of the beginning of a route refresh"}, + {BGP_ROUTE_REFRESH_SUBTYPE_EORR, + "Demarcation of the ending of a route refresh"}, + {0, NULL}}; #define bgp_attr_lenlen(flags, p) \ (((flags) & 0x10) ? 2U : 1U) @@ -126,9 +158,6 @@ struct bgp_route_refresh { #define BGPTYPE_COMMUNITIES 8 /* RFC1997 */ #define BGPTYPE_ORIGINATOR_ID 9 /* RFC4456 */ #define BGPTYPE_CLUSTER_LIST 10 /* RFC4456 */ -#define BGPTYPE_DPA 11 /* deprecated, draft-ietf-idr-bgp-dpa */ -#define BGPTYPE_ADVERTISERS 12 /* deprecated RFC1863 */ -#define BGPTYPE_RCID_PATH 13 /* deprecated RFC1863 */ #define BGPTYPE_MP_REACH_NLRI 14 /* RFC4760 */ #define BGPTYPE_MP_UNREACH_NLRI 15 /* RFC4760 */ #define BGPTYPE_EXTD_COMMUNITIES 16 /* RFC4360 */ @@ -142,6 +171,8 @@ struct bgp_route_refresh { #define BGPTYPE_PE_DISTINGUISHER_LABEL 27 /* RFC6514 */ #define BGPTYPE_ENTROPY_LABEL 28 /* RFC6790 */ #define BGPTYPE_LARGE_COMMUNITY 32 /* draft-ietf-idr-large-community-05 */ +#define BGPTYPE_BGPSEC_PATH 33 /* RFC8205 */ +#define BGPTYPE_OTC 35 /* RFC9234 */ #define BGPTYPE_ATTR_SET 128 /* RFC6368 */ #define BGP_MP_NLRI_MINSIZE 3 /* End of RIB Marker detection */ @@ -159,9 +190,6 @@ static const struct tok bgp_attr_values[] = { { BGPTYPE_COMMUNITIES, "Community"}, { BGPTYPE_ORIGINATOR_ID, "Originator ID"}, { BGPTYPE_CLUSTER_LIST, "Cluster List"}, - { BGPTYPE_DPA, "DPA"}, - { BGPTYPE_ADVERTISERS, "Advertisers"}, - { BGPTYPE_RCID_PATH, "RCID Path / Cluster ID"}, { BGPTYPE_MP_REACH_NLRI, "Multi-Protocol Reach NLRI"}, { BGPTYPE_MP_UNREACH_NLRI, "Multi-Protocol Unreach NLRI"}, { BGPTYPE_EXTD_COMMUNITIES, "Extended Community"}, @@ -173,6 +201,8 @@ static const struct tok bgp_attr_values[] = { { BGPTYPE_PE_DISTINGUISHER_LABEL, "PE Distinguisher Label"}, { BGPTYPE_ENTROPY_LABEL, "Entropy Label"}, { BGPTYPE_LARGE_COMMUNITY, "Large Community"}, + { BGPTYPE_BGPSEC_PATH, "BGPsec Path"}, + { BGPTYPE_OTC, "Only to Customer (OTC)"}, { BGPTYPE_ATTR_SET, "Attribute Set"}, { 255, "Reserved for development"}, { 0, NULL} @@ -216,13 +246,16 @@ static const struct tok bgp_opt_values[] = { #define BGP_CAPCODE_ORF 3 /* RFC5291 */ #define BGP_CAPCODE_MR 4 /* RFC3107 */ #define BGP_CAPCODE_EXT_NH 5 /* RFC5549 */ +#define BGP_CAPCODE_EXT_MSG 6 /* RFC8654 */ +#define BGP_CAPCODE_BGPSEC 7 /* RFC8205 */ #define BGP_CAPCODE_ML 8 /* RFC8277 */ +#define BGP_CAPCODE_ROLE 9 /* RFC9234 */ #define BGP_CAPCODE_RESTART 64 /* RFC4724 */ #define BGP_CAPCODE_AS_NEW 65 /* RFC6793 */ #define BGP_CAPCODE_DYN_CAP 67 /* draft-ietf-idr-dynamic-cap */ #define BGP_CAPCODE_MULTISESS 68 /* draft-ietf-idr-bgp-multisession */ #define BGP_CAPCODE_ADD_PATH 69 /* RFC7911 */ -#define BGP_CAPCODE_ENH_RR 70 /* draft-keyur-bgp-enhanced-route-refresh */ +#define BGP_CAPCODE_ENH_RR 70 /* RFC7313 */ #define BGP_CAPCODE_LLGR 71 /* draft-uttaro-idr-bgp-persistence-05 */ #define BGP_CAPCODE_RR_CISCO 128 @@ -232,6 +265,8 @@ static const struct tok bgp_capcode_values[] = { { BGP_CAPCODE_ORF, "Cooperative Route Filtering"}, { BGP_CAPCODE_MR, "Multiple Routes to a Destination"}, { BGP_CAPCODE_EXT_NH, "Extended Next Hop Encoding"}, + { BGP_CAPCODE_EXT_MSG, "BGP Extended Message"}, + { BGP_CAPCODE_BGPSEC, "BGPsec"}, { BGP_CAPCODE_ML, "Multiple Labels"}, { BGP_CAPCODE_RESTART, "Graceful Restart"}, { BGP_CAPCODE_AS_NEW, "32-Bit AS Number"}, @@ -241,6 +276,7 @@ static const struct tok bgp_capcode_values[] = { { BGP_CAPCODE_ENH_RR, "Enhanced Route Refresh"}, { BGP_CAPCODE_LLGR, "Long-lived Graceful Restart"}, { BGP_CAPCODE_RR_CISCO, "Route Refresh (Cisco)"}, + { BGP_CAPCODE_ROLE, "Role Capability"}, { 0, NULL} }; @@ -250,7 +286,8 @@ static const struct tok bgp_capcode_values[] = { #define BGP_NOTIFY_MAJOR_HOLDTIME 4 #define BGP_NOTIFY_MAJOR_FSM 5 #define BGP_NOTIFY_MAJOR_CEASE 6 -#define BGP_NOTIFY_MAJOR_CAP 7 +#define BGP_NOTIFY_MAJOR_ROUTEREFRESH 7 +#define BGP_NOTIFY_MAJOR_SENDHOLDTIME 8 static const struct tok bgp_notify_major_values[] = { { BGP_NOTIFY_MAJOR_MSG, "Message Header Error"}, @@ -259,16 +296,18 @@ static const struct tok bgp_notify_major_values[] = { { BGP_NOTIFY_MAJOR_HOLDTIME,"Hold Timer Expired"}, { BGP_NOTIFY_MAJOR_FSM, "Finite State Machine Error"}, { BGP_NOTIFY_MAJOR_CEASE, "Cease"}, - { BGP_NOTIFY_MAJOR_CAP, "Capability Message Error"}, + { BGP_NOTIFY_MAJOR_ROUTEREFRESH, "ROUTE-REFRESH Message Error"}, + { BGP_NOTIFY_MAJOR_SENDHOLDTIME, "Send Hold Timer Expired"}, { 0, NULL} }; -/* draft-ietf-idr-cease-subcode-02 */ +/* RFC 4486 */ #define BGP_NOTIFY_MINOR_CEASE_MAXPRFX 1 -/* draft-ietf-idr-shutdown-07 */ +/* RFC 9003 */ #define BGP_NOTIFY_MINOR_CEASE_SHUT 2 #define BGP_NOTIFY_MINOR_CEASE_RESET 4 -#define BGP_NOTIFY_MINOR_CEASE_ADMIN_SHUTDOWN_LEN 128 +/* RFC 8538 */ +#define BGP_NOTIFY_MINOR_CEASE_HARDRESET 9 static const struct tok bgp_notify_minor_cease_values[] = { { BGP_NOTIFY_MINOR_CEASE_MAXPRFX, "Maximum Number of Prefixes Reached"}, { BGP_NOTIFY_MINOR_CEASE_SHUT, "Administrative Shutdown"}, @@ -277,6 +316,9 @@ static const struct tok bgp_notify_minor_cease_values[] = { { 5, "Connection Rejected"}, { 6, "Other Configuration Change"}, { 7, "Connection Collision Resolution"}, + { 8, "Out of Resources"}, + { BGP_NOTIFY_MINOR_CEASE_HARDRESET, "Hard Reset"}, + { 10, "BFD Down"}, { 0, NULL} }; @@ -321,11 +363,8 @@ static const struct tok bgp_notify_minor_fsm_values[] = { { 0, NULL } }; -static const struct tok bgp_notify_minor_cap_values[] = { - { 1, "Invalid Action Value" }, - { 2, "Invalid Capability Length" }, - { 3, "Malformed Capability Value" }, - { 4, "Unsupported Capability Code" }, +static const struct tok bgp_notify_minor_routerefresh_values[] = { + { 1, "Invalid Message Length" }, { 0, NULL } }; @@ -367,6 +406,21 @@ static const struct tok bgp_aigp_values[] = { { 0, NULL} }; +#define BGP_ROLE_PROVIDER 0 +#define BGP_ROLE_RS 1 +#define BGP_ROLE_RS_CLIENT 2 +#define BGP_ROLE_CUSTOMER 3 +#define BGP_ROLE_PEER 4 + +static const struct tok bgp_role_values[] = { + { BGP_ROLE_PROVIDER, "Provider"}, + { BGP_ROLE_RS, "RS"}, + { BGP_ROLE_RS_CLIENT, "RS-Client"}, + { BGP_ROLE_CUSTOMER, "Customer"}, + { BGP_ROLE_PEER, "Peer"}, + { 0, NULL} +}; + /* Subsequent address family identifier, RFC2283 section 7 */ #define SAFNUM_RES 0 #define SAFNUM_UNICAST 1 @@ -412,6 +466,12 @@ static const struct tok bgp_safi_values[] = { { 0, NULL } }; +static const struct tok bgp_graceful_restart_comm_flag_values[] = { + { 0x8, "R" }, + { 0x4, "N" }, + { 0, NULL } +}; + /* well-known community */ #define BGP_COMMUNITY_NO_EXPORT 0xffffff01 #define BGP_COMMUNITY_NO_ADVERT 0xffffff02 @@ -425,6 +485,7 @@ static const struct tok bgp_safi_values[] = { #define BGP_EXT_COM_RO_1 0x0103 /* Route Origin,Format IP address:AN(2bytes) */ #define BGP_EXT_COM_RO_2 0x0203 /* Route Origin,Format AN(4bytes):local(2bytes) */ #define BGP_EXT_COM_LINKBAND 0x4004 /* Link Bandwidth,Format AS(2B):Bandwidth(4B) */ +#define BGP_EXT_COM_OVS 0x4300 /* BGP Prefix Origin Validation State Extended Community */ /* rfc2547 bgp-mpls-vpns */ #define BGP_EXT_COM_VPN_ORIGIN 0x0005 /* OSPF Domain ID / VPN of Origin - draft-rosen-vpns-ospf-bgp-mpls */ #define BGP_EXT_COM_VPN_ORIGIN2 0x0105 /* duplicate - keep for backwards compatibility */ @@ -453,12 +514,23 @@ static const struct tok bgp_safi_values[] = { #define BGP_EXT_COM_EIGRP_EXT_REMAS_REMID 0x8804 #define BGP_EXT_COM_EIGRP_EXT_REMPROTO_REMMETRIC 0x8805 +/* Optional Parameters */ +#define BGP_OPEN_NON_EXT_OPT_TYPE_EXTENDED_LENGTH 255 /* Non-Ext OP Type */ + static const struct tok bgp_extd_comm_flag_values[] = { { 0x8000, "vendor-specific"}, { 0x4000, "non-transitive"}, { 0, NULL}, }; +/* rfc8097 */ +static const struct tok bgp_prefix_origin_validation_state[] = { + { 0, "valid" }, + { 1, "not found" }, + { 2, "invalid" }, + { 0, NULL }, +}; + static const struct tok bgp_extd_comm_subtype_values[] = { { BGP_EXT_COM_RT_0, "target"}, { BGP_EXT_COM_RT_1, "target"}, @@ -467,6 +539,7 @@ static const struct tok bgp_extd_comm_subtype_values[] = { { BGP_EXT_COM_RO_1, "origin"}, { BGP_EXT_COM_RO_2, "origin"}, { BGP_EXT_COM_LINKBAND, "link-BW"}, + { BGP_EXT_COM_OVS, "origin-validation-state"}, { BGP_EXT_COM_VPN_ORIGIN, "ospf-domain"}, { BGP_EXT_COM_VPN_ORIGIN2, "ospf-domain"}, { BGP_EXT_COM_VPN_ORIGIN3, "ospf-domain"}, @@ -557,8 +630,19 @@ static const struct tok bgp_add_path_recvsend[] = { { 0, NULL }, }; -/* allocate space for the largest possible string */ -static char astostr[sizeof("xxxxx.xxxxx")]; +static const struct tok bgp_bgpsec_bitmap_str[] = { + { 1U << 0, "MBZ-0" }, + { 1U << 1, "MBZ-1" }, + { 1U << 2, "MBZ-2" }, + { 1U << 3, "MBZ-3" }, + { 1U << 4, "MBZ-4" }, + { 1U << 5, "MBZ-5" }, + { 1U << 6, "MBZ-6" }, + { 1U << 7, "C" }, + { 0, NULL} +}; + +#define AS_STR_SIZE sizeof("xxxxx.xxxxx") /* * as_printf @@ -598,26 +682,21 @@ decode_prefix4(netdissect_options *ndo, memset(&addr, 0, sizeof(addr)); plenbytes = (plen + 7) / 8; - ND_TCHECK_LEN(pptr + 1, plenbytes); ITEMCHECK(plenbytes); - memcpy(&addr, pptr + 1, plenbytes); + GET_CPY_BYTES(&addr, pptr + 1, plenbytes); if (plen % 8) { ((u_char *)&addr)[plenbytes - 1] &= ((0xff00 >> (plen % 8)) & 0xff); } snprintf(buf, buflen, "%s/%u", ipaddr_string(ndo, (const u_char *)&addr), plen); return 1 + plenbytes; -trunc: - return -2; - badtlv: - return -3; + return -2; } static int -decode_labeled_prefix4(netdissect_options *ndo, - const u_char *pptr, u_int itemlen, char *buf, - size_t buflen) +print_labeled_prefix4(netdissect_options *ndo, + const u_char *pptr, u_int itemlen) { nd_ipv4 addr; u_int plen, plenbytes; @@ -636,24 +715,23 @@ decode_labeled_prefix4(netdissect_options *ndo, */ if (24 > plen) - return -1; + goto badplen; plen-=24; /* adjust prefixlen - labellength */ if (32 < plen) - return -1; + goto badplen; itemlen -= 4; memset(&addr, 0, sizeof(addr)); plenbytes = (plen + 7) / 8; - ND_TCHECK_LEN(pptr + 4, plenbytes); ITEMCHECK(plenbytes); - memcpy(&addr, pptr + 4, plenbytes); + GET_CPY_BYTES(&addr, pptr + 4, plenbytes); if (plen % 8) { ((u_char *)&addr)[plenbytes - 1] &= ((0xff00 >> (plen % 8)) & 0xff); } /* the label may get offsetted by 4 bits so lets shift it right */ - snprintf(buf, buflen, "%s/%u, label:%u %s", + ND_PRINT("\n\t %s/%u, label:%u %s", ipaddr_string(ndo, (const u_char *)&addr), plen, GET_BE_U_3(pptr + 1)>>4, @@ -661,6 +739,10 @@ decode_labeled_prefix4(netdissect_options *ndo, return 4 + plenbytes; +badplen: + ND_PRINT("\n\t (illegal prefix length)"); + return -1; + trunc: return -2; @@ -671,7 +753,7 @@ badtlv: /* * bgp_vpn_ip_print * - * print an ipv4 or ipv6 address into a buffer dependend on address length. + * print an ipv4 or ipv6 address into a buffer dependent on address length. */ static char * bgp_vpn_ip_print(netdissect_options *ndo, @@ -718,11 +800,10 @@ bgp_vpn_ip_print(netdissect_options *ndo, * return the number of bytes read from the wire. */ static u_int -bgp_vpn_sg_print(netdissect_options *ndo, - const u_char *pptr, char *buf, size_t buflen) +bgp_vpn_sg_print(netdissect_options *ndo, const u_char *pptr) { uint8_t addr_length; - u_int total_length, offset; + u_int total_length; total_length = 0; @@ -733,10 +814,9 @@ bgp_vpn_sg_print(netdissect_options *ndo, /* Source address */ ND_TCHECK_LEN(pptr, (addr_length >> 3)); total_length += (addr_length >> 3) + 1; - offset = (u_int)strlen(buf); if (addr_length) { - snprintf(buf + offset, buflen - offset, ", Source %s", - bgp_vpn_ip_print(ndo, pptr, addr_length)); + ND_PRINT(", Source %s", + bgp_vpn_ip_print(ndo, pptr, addr_length)); pptr += (addr_length >> 3); } @@ -747,10 +827,9 @@ bgp_vpn_sg_print(netdissect_options *ndo, /* Group address */ ND_TCHECK_LEN(pptr, (addr_length >> 3)); total_length += (addr_length >> 3) + 1; - offset = (u_int)strlen(buf); if (addr_length) { - snprintf(buf + offset, buflen - offset, ", Group %s", - bgp_vpn_ip_print(ndo, pptr, addr_length)); + ND_PRINT(", Group %s", + bgp_vpn_ip_print(ndo, pptr, addr_length)); pptr += (addr_length >> 3); } @@ -760,12 +839,13 @@ trunc: /* Print an RFC 4364 Route Distinguisher */ const char * -bgp_vpn_rd_print(netdissect_options *ndo, - const u_char *pptr) +bgp_vpn_rd_print(netdissect_options *ndo, const u_char *pptr) { /* allocate space for the largest possible string */ static char rd[sizeof("xxxxx.xxxxx:xxxxx (xxx.xxx.xxx.xxx:xxxxx)")]; char *pos = rd; + /* allocate space for the largest possible string */ + char astostr[AS_STR_SIZE]; /* ok lets load the RD format */ switch (GET_BE_U_2(pptr)) { @@ -811,10 +891,8 @@ static void bgp_extended_community_print(netdissect_options *ndo, const u_char *pptr) { - union { /* copy buffer for bandwidth values */ - float f; - uint32_t i; - } bw; + /* allocate space for the largest possible string */ + char astostr[AS_STR_SIZE]; switch (GET_BE_U_2(pptr)) { @@ -844,11 +922,27 @@ bgp_extended_community_print(netdissect_options *ndo, break; case BGP_EXT_COM_LINKBAND: - bw.i = GET_BE_U_4(pptr + 2); ND_PRINT("bandwidth: %.3f Mbps", - bw.f*8/1000000); + GET_BE_F_4(pptr + 4)*8/1000000); break; + case BGP_EXT_COM_OVS: + /* The Reserved field MUST be set to 0 and ignored upon the + * receipt of this community. + */ + { + uint64_t reserved = GET_BE_U_5(pptr + 2); + + if (reserved) + ND_PRINT("[the reserved field 0x%" PRIx64 " MUST be 0] ", + reserved); + ND_PRINT("ovs: %s", + tok2str(bgp_prefix_origin_validation_state, + "unknown origin validation state", + GET_U_1(pptr + 7))); + } + break; + case BGP_EXT_COM_VPN_ORIGIN: case BGP_EXT_COM_VPN_ORIGIN2: case BGP_EXT_COM_VPN_ORIGIN3: @@ -917,9 +1011,11 @@ bgp_rt_prefix_print(netdissect_options *ndo, u_int plen) { /* allocate space for the largest possible string */ - char rtc_prefix_in_hex[20] = ""; + char rtc_prefix_in_hex[sizeof("0000 0000 0000 0000")] = ""; u_int rtc_prefix_in_hex_len = 0; static char output[61]; /* max response string */ + /* allocate space for the largest possible string */ + char astostr[AS_STR_SIZE]; uint16_t ec_type = 0; u_int octet_count; u_int i; @@ -951,7 +1047,7 @@ bgp_rt_prefix_print(netdissect_options *ndo, /* * get the ext-comm type * Note: pptr references a static 8 octet buffer with unused bits set to 0, - * hense EXTRACT_*() macros are safe. + * hence EXTRACT_*() macros are safe. */ ec_type = EXTRACT_BE_U_2(pptr); switch (ec_type) { @@ -988,12 +1084,12 @@ bgp_rt_prefix_print(netdissect_options *ndo, /* RFC 4684 */ static int -decode_rt_routing_info(netdissect_options *ndo, - const u_char *pptr) +print_rt_routing_info(netdissect_options *ndo, const u_char *pptr) { uint8_t route_target[8]; u_int plen; - char asbuf[sizeof(astostr)]; /* bgp_vpn_rd_print() overwrites astostr */ + /* allocate space for the largest possible string */ + char astostr[AS_STR_SIZE]; u_int num_octets; /* NLRI "prefix length" from RFC 2858 Section 4. */ @@ -1015,7 +1111,7 @@ decode_rt_routing_info(netdissect_options *ndo, } /* With at least "origin AS", possibly with "route target". */ - as_printf(ndo, asbuf, sizeof(asbuf), GET_BE_U_4(pptr + 1)); + as_printf(ndo, astostr, sizeof(astostr), GET_BE_U_4(pptr + 1)); plen -= 32; /* adjust prefix length */ @@ -1030,25 +1126,21 @@ decode_rt_routing_info(netdissect_options *ndo, */ memset(&route_target, 0, sizeof(route_target)); num_octets = (plen + 7) / 8; - ND_TCHECK_LEN(pptr + 5, num_octets); - memcpy(&route_target, pptr + 5, num_octets); + GET_CPY_BYTES(&route_target, pptr + 5, num_octets); /* If mask-len is not on octet boundary, ensure all extra bits are 0 */ if (plen % 8) { ((u_char *)&route_target)[num_octets - 1] &= ((0xff00 >> (plen % 8)) & 0xff); } ND_PRINT("\n\t origin AS: %s, %s", - asbuf, + astostr, bgp_rt_prefix_print(ndo, (u_char *)&route_target, plen)); return 5 + num_octets; -trunc: - return -2; } static int -decode_labeled_vpn_prefix4(netdissect_options *ndo, - const u_char *pptr, char *buf, size_t buflen) +print_labeled_vpn_prefix4(netdissect_options *ndo, const u_char *pptr) { nd_ipv4 addr; u_int plen; @@ -1056,22 +1148,21 @@ decode_labeled_vpn_prefix4(netdissect_options *ndo, plen = GET_U_1(pptr); /* get prefix length */ if ((24+64) > plen) - return -1; + goto badplen; plen -= (24+64); /* adjust prefixlen - labellength - RD len*/ if (32 < plen) - return -1; + goto badplen; memset(&addr, 0, sizeof(addr)); - ND_TCHECK_LEN(pptr + 12, (plen + 7) / 8); - memcpy(&addr, pptr + 12, (plen + 7) / 8); + GET_CPY_BYTES(&addr, pptr + 12, (plen + 7) / 8); if (plen % 8) { ((u_char *)&addr)[(plen + 7) / 8 - 1] &= ((0xff00 >> (plen % 8)) & 0xff); } /* the label may get offsetted by 4 bits so lets shift it right */ - snprintf(buf, buflen, "RD: %s, %s/%u, label:%u %s", + ND_PRINT("\n\t RD: %s, %s/%u, label:%u %s", bgp_vpn_rd_print(ndo, pptr+4), ipaddr_string(ndo, (const u_char *)&addr), plen, @@ -1080,8 +1171,9 @@ decode_labeled_vpn_prefix4(netdissect_options *ndo, return 12 + (plen + 7) / 8; -trunc: - return -2; +badplen: + ND_PRINT("\n\t (illegal prefix length)"); + return -1; } /* @@ -1097,15 +1189,16 @@ trunc: #define MDT_VPN_NLRI_LEN 16 static int -decode_mdt_vpn_nlri(netdissect_options *ndo, - const u_char *pptr, char *buf, size_t buflen) +print_mdt_vpn_nlri(netdissect_options *ndo, const u_char *pptr) { const u_char *rd; const u_char *vpn_ip; /* if the NLRI is not predefined length, quit.*/ - if (GET_U_1(pptr) != MDT_VPN_NLRI_LEN * 8) + if (GET_U_1(pptr) != MDT_VPN_NLRI_LEN * 8) { + ND_PRINT("\n\t (illegal prefix length)"); return -1; + } pptr++; /* RD */ @@ -1118,8 +1211,10 @@ decode_mdt_vpn_nlri(netdissect_options *ndo, pptr += sizeof(nd_ipv4); /* MDT Group Address */ - snprintf(buf, buflen, "RD: %s, VPN IP Address: %s, MC Group Address: %s", - bgp_vpn_rd_print(ndo, rd), GET_IPADDR_STRING(vpn_ip), GET_IPADDR_STRING(pptr)); + ND_PRINT("\n\t RD: %s, VPN IP Address: %s, MC Group Address: %s", + bgp_vpn_rd_print(ndo, rd), + GET_IPADDR_STRING(vpn_ip), + GET_IPADDR_STRING(pptr)); return MDT_VPN_NLRI_LEN + 1; @@ -1147,19 +1242,19 @@ static const struct tok bgp_multicast_vpn_route_type_values[] = { }; static int -decode_multicast_vpn(netdissect_options *ndo, - const u_char *pptr, char *buf, size_t buflen) +print_multicast_vpn(netdissect_options *ndo, const u_char *pptr) { + /* allocate space for the largest possible string */ + char astostr[AS_STR_SIZE]; uint8_t route_type, route_length; u_int addr_length, sg_length; - u_int offset; route_type = GET_U_1(pptr); pptr++; route_length = GET_U_1(pptr); pptr++; - snprintf(buf, buflen, "Route-Type: %s (%u), length: %u", + ND_PRINT("\n\t Route-Type: %s (%u), length: %u", tok2str(bgp_multicast_vpn_route_type_values, "Unknown", route_type), route_type, route_length); @@ -1167,58 +1262,52 @@ decode_multicast_vpn(netdissect_options *ndo, switch(route_type) { case BGP_MULTICAST_VPN_ROUTE_TYPE_INTRA_AS_I_PMSI: ND_TCHECK_LEN(pptr, BGP_VPN_RD_LEN); - offset = (u_int)strlen(buf); - snprintf(buf + offset, buflen - offset, ", RD: %s, Originator %s", + if (route_length < BGP_VPN_RD_LEN) + goto trunc; + ND_PRINT(", RD: %s, Originator %s", bgp_vpn_rd_print(ndo, pptr), bgp_vpn_ip_print(ndo, pptr + BGP_VPN_RD_LEN, (route_length - BGP_VPN_RD_LEN) << 3)); break; case BGP_MULTICAST_VPN_ROUTE_TYPE_INTER_AS_I_PMSI: ND_TCHECK_LEN(pptr, BGP_VPN_RD_LEN + 4); - offset = (u_int)strlen(buf); - snprintf(buf + offset, buflen - offset, ", RD: %s, Source-AS %s", - bgp_vpn_rd_print(ndo, pptr), - as_printf(ndo, astostr, sizeof(astostr), - GET_BE_U_4(pptr + BGP_VPN_RD_LEN))); + ND_PRINT(", RD: %s, Source-AS %s", + bgp_vpn_rd_print(ndo, pptr), + as_printf(ndo, astostr, sizeof(astostr), + GET_BE_U_4(pptr + BGP_VPN_RD_LEN))); break; case BGP_MULTICAST_VPN_ROUTE_TYPE_S_PMSI: ND_TCHECK_LEN(pptr, BGP_VPN_RD_LEN); - offset = (u_int)strlen(buf); - snprintf(buf + offset, buflen - offset, ", RD: %s", - bgp_vpn_rd_print(ndo, pptr)); + ND_PRINT(", RD: %s", bgp_vpn_rd_print(ndo, pptr)); pptr += BGP_VPN_RD_LEN; - sg_length = bgp_vpn_sg_print(ndo, pptr, buf, buflen); + sg_length = bgp_vpn_sg_print(ndo, pptr); addr_length = route_length - sg_length; ND_TCHECK_LEN(pptr, addr_length); - offset = (u_int)strlen(buf); - snprintf(buf + offset, buflen - offset, ", Originator %s", - bgp_vpn_ip_print(ndo, pptr, addr_length << 3)); + ND_PRINT(", Originator %s", + bgp_vpn_ip_print(ndo, pptr, addr_length << 3)); break; case BGP_MULTICAST_VPN_ROUTE_TYPE_SOURCE_ACTIVE: ND_TCHECK_LEN(pptr, BGP_VPN_RD_LEN); - offset = (u_int)strlen(buf); - snprintf(buf + offset, buflen - offset, ", RD: %s", - bgp_vpn_rd_print(ndo, pptr)); + ND_PRINT(", RD: %s", bgp_vpn_rd_print(ndo, pptr)); pptr += BGP_VPN_RD_LEN; - bgp_vpn_sg_print(ndo, pptr, buf, buflen); + bgp_vpn_sg_print(ndo, pptr); break; case BGP_MULTICAST_VPN_ROUTE_TYPE_SHARED_TREE_JOIN: /* fall through */ case BGP_MULTICAST_VPN_ROUTE_TYPE_SOURCE_TREE_JOIN: ND_TCHECK_LEN(pptr, BGP_VPN_RD_LEN + 4); - offset = (u_int)strlen(buf); - snprintf(buf + offset, buflen - offset, ", RD: %s, Source-AS %s", - bgp_vpn_rd_print(ndo, pptr), - as_printf(ndo, astostr, sizeof(astostr), - GET_BE_U_4(pptr + BGP_VPN_RD_LEN))); + ND_PRINT(", RD: %s, Source-AS %s", + bgp_vpn_rd_print(ndo, pptr), + as_printf(ndo, astostr, sizeof(astostr), + GET_BE_U_4(pptr + BGP_VPN_RD_LEN))); pptr += BGP_VPN_RD_LEN + 4; - bgp_vpn_sg_print(ndo, pptr, buf, buflen); + bgp_vpn_sg_print(ndo, pptr); break; /* @@ -1235,32 +1324,10 @@ trunc: return -2; } -/* - * As I remember, some versions of systems have an snprintf() that - * returns -1 if the buffer would have overflowed. If the return - * value is negative, set buflen to 0, to indicate that we've filled - * the buffer up. - * - * If the return value is greater than buflen, that means that - * the buffer would have overflowed; again, set buflen to 0 in - * that case. - */ -#define UPDATE_BUF_BUFLEN(buf, buflen, stringlen) \ - if (stringlen<0) \ - buflen=0; \ - else if ((u_int)stringlen>buflen) \ - buflen=0; \ - else { \ - buflen-=stringlen; \ - buf+=stringlen; \ - } - static int -decode_labeled_vpn_l2(netdissect_options *ndo, - const u_char *pptr, char *buf, size_t buflen) +print_labeled_vpn_l2(netdissect_options *ndo, const u_char *pptr) { u_int plen, tlen, tlv_type, tlv_len, ttlv_len; - int stringlen; plen = GET_BE_U_2(pptr); tlen = plen; @@ -1272,36 +1339,27 @@ decode_labeled_vpn_l2(netdissect_options *ndo, if (plen == 12) { /* assume AD-only with RD, BGPNH */ ND_TCHECK_LEN(pptr, 12); - buf[0] = '\0'; - stringlen = snprintf(buf, buflen, "RD: %s, BGPNH: %s", + ND_PRINT("\n\t RD: %s, BGPNH: %s", bgp_vpn_rd_print(ndo, pptr), GET_IPADDR_STRING(pptr+8)); - UPDATE_BUF_BUFLEN(buf, buflen, stringlen); - pptr += 12; - tlen -= 12; return plen + 2; } else if (plen > 17) { /* assume old format */ /* RD, ID, LBLKOFF, LBLBASE */ ND_TCHECK_LEN(pptr, 15); - buf[0] = '\0'; - stringlen = snprintf(buf, buflen, "RD: %s, CE-ID: %u, Label-Block Offset: %u, Label Base %u", + ND_PRINT("\n\t RD: %s, CE-ID: %u, Label-Block Offset: %u, Label Base %u", bgp_vpn_rd_print(ndo, pptr), GET_BE_U_2(pptr + 8), GET_BE_U_2(pptr + 10), GET_BE_U_3(pptr + 12)>>4); /* the label is offsetted by 4 bits so lets shift it right */ - UPDATE_BUF_BUFLEN(buf, buflen, stringlen); pptr += 15; tlen -= 15; /* ok now the variable part - lets read out TLVs*/ while (tlen != 0) { if (tlen < 3) { - if (buflen != 0) { - stringlen=snprintf(buf,buflen, "\n\t\tran past the end"); - UPDATE_BUF_BUFLEN(buf, buflen, stringlen); - } + ND_PRINT("\n\t\tran past the end"); return plen + 2; } tlv_type = GET_U_1(pptr); @@ -1312,43 +1370,27 @@ decode_labeled_vpn_l2(netdissect_options *ndo, switch(tlv_type) { case 1: - if (buflen != 0) { - stringlen=snprintf(buf,buflen, "\n\t\tcircuit status vector (%u) length: %u: 0x", - tlv_type, - tlv_len); - UPDATE_BUF_BUFLEN(buf, buflen, stringlen); - } + ND_PRINT("\n\t\tcircuit status vector (%u) length: %u: 0x", + tlv_type, + tlv_len); while (ttlv_len != 0) { if (tlen < 1) { - if (buflen != 0) { - stringlen=snprintf(buf,buflen, " (ran past the end)"); - UPDATE_BUF_BUFLEN(buf, buflen, stringlen); - } + ND_PRINT(" (ran past the end)"); return plen + 2; } ND_TCHECK_1(pptr); - if (buflen != 0) { - stringlen=snprintf(buf,buflen, "%02x", - GET_U_1(pptr)); - pptr++; - UPDATE_BUF_BUFLEN(buf, buflen, stringlen); - } + ND_PRINT("%02x", GET_U_1(pptr)); + pptr++; ttlv_len--; tlen--; } break; default: - if (buflen != 0) { - stringlen=snprintf(buf,buflen, "\n\t\tunknown TLV #%u, length: %u", - tlv_type, - tlv_len); - UPDATE_BUF_BUFLEN(buf, buflen, stringlen); - } + ND_PRINT("\n\t\tunknown TLV #%u, length: %u", + tlv_type, + tlv_len); if (tlen < ttlv_len) { - if (buflen != 0) { - stringlen=snprintf(buf,buflen, " (ran past the end)"); - UPDATE_BUF_BUFLEN(buf, buflen, stringlen); - } + ND_PRINT(" (ran past the end)"); return plen + 2; } tlen -= ttlv_len; @@ -1381,9 +1423,8 @@ decode_prefix6(netdissect_options *ndo, memset(&addr, 0, sizeof(addr)); plenbytes = (plen + 7) / 8; - ND_TCHECK_LEN(pd + 1, plenbytes); ITEMCHECK(plenbytes); - memcpy(&addr, pd + 1, plenbytes); + GET_CPY_BYTES(&addr, pd + 1, plenbytes); if (plen % 8) { addr[plenbytes - 1] &= ((0xff00 >> (plen % 8)) & 0xff); @@ -1391,16 +1432,13 @@ decode_prefix6(netdissect_options *ndo, snprintf(buf, buflen, "%s/%u", ip6addr_string(ndo, (const u_char *)&addr), plen); return 1 + plenbytes; -trunc: - return -2; - badtlv: - return -3; + return -2; } static int -decode_labeled_prefix6(netdissect_options *ndo, - const u_char *pptr, u_int itemlen, char *buf, size_t buflen) +print_labeled_prefix6(netdissect_options *ndo, + const u_char *pptr, u_int itemlen) { nd_ipv6 addr; u_int plen, plenbytes; @@ -1411,24 +1449,23 @@ decode_labeled_prefix6(netdissect_options *ndo, plen = GET_U_1(pptr); /* get prefix length */ if (24 > plen) - return -1; + goto badplen; plen -= 24; /* adjust prefixlen - labellength */ if (128 < plen) - return -1; + goto badplen; itemlen -= 4; memset(&addr, 0, sizeof(addr)); plenbytes = (plen + 7) / 8; - ND_TCHECK_LEN(pptr + 4, plenbytes); - memcpy(&addr, pptr + 4, plenbytes); + GET_CPY_BYTES(&addr, pptr + 4, plenbytes); if (plen % 8) { addr[plenbytes - 1] &= ((0xff00 >> (plen % 8)) & 0xff); } /* the label may get offsetted by 4 bits so lets shift it right */ - snprintf(buf, buflen, "%s/%u, label:%u %s", + ND_PRINT("\n\t %s/%u, label:%u %s", ip6addr_string(ndo, (const u_char *)&addr), plen, GET_BE_U_3(pptr + 1)>>4, @@ -1436,6 +1473,10 @@ decode_labeled_prefix6(netdissect_options *ndo, return 4 + plenbytes; +badplen: + ND_PRINT("\n\t (illegal prefix length)"); + return -1; + trunc: return -2; @@ -1444,8 +1485,7 @@ badtlv: } static int -decode_labeled_vpn_prefix6(netdissect_options *ndo, - const u_char *pptr, char *buf, size_t buflen) +print_labeled_vpn_prefix6(netdissect_options *ndo, const u_char *pptr) { nd_ipv6 addr; u_int plen; @@ -1453,22 +1493,21 @@ decode_labeled_vpn_prefix6(netdissect_options *ndo, plen = GET_U_1(pptr); /* get prefix length */ if ((24+64) > plen) - return -1; + goto badplen; plen -= (24+64); /* adjust prefixlen - labellength - RD len*/ if (128 < plen) - return -1; + goto badplen; memset(&addr, 0, sizeof(addr)); - ND_TCHECK_LEN(pptr + 12, (plen + 7) / 8); - memcpy(&addr, pptr + 12, (plen + 7) / 8); + GET_CPY_BYTES(&addr, pptr + 12, (plen + 7) / 8); if (plen % 8) { addr[(plen + 7) / 8 - 1] &= ((0xff00 >> (plen % 8)) & 0xff); } /* the label may get offsetted by 4 bits so lets shift it right */ - snprintf(buf, buflen, "RD: %s, %s/%u, label:%u %s", + ND_PRINT("\n\t RD: %s, %s/%u, label:%u %s", bgp_vpn_rd_print(ndo, pptr+4), ip6addr_string(ndo, (const u_char *)&addr), plen, @@ -1477,13 +1516,13 @@ decode_labeled_vpn_prefix6(netdissect_options *ndo, return 12 + (plen + 7) / 8; -trunc: - return -2; +badplen: + ND_PRINT("\n\t (illegal prefix length)"); + return -1; } static int -decode_clnp_prefix(netdissect_options *ndo, - const u_char *pptr, char *buf, size_t buflen) +print_clnp_prefix(netdissect_options *ndo, const u_char *pptr) { uint8_t addr[19]; u_int plen; @@ -1491,29 +1530,28 @@ decode_clnp_prefix(netdissect_options *ndo, plen = GET_U_1(pptr); /* get prefix length */ if (152 < plen) - return -1; + goto badplen; memset(&addr, 0, sizeof(addr)); - ND_TCHECK_LEN(pptr + 4, (plen + 7) / 8); - memcpy(&addr, pptr + 4, (plen + 7) / 8); + GET_CPY_BYTES(&addr, pptr + 4, (plen + 7) / 8); if (plen % 8) { addr[(plen + 7) / 8 - 1] &= ((0xff00 >> (plen % 8)) & 0xff); } /* Cannot use GET_ISONSAP_STRING (not packet buffer pointer) */ - snprintf(buf, buflen, "%s/%u", + ND_PRINT("\n\t %s/%u", isonsap_string(ndo, addr,(plen + 7) / 8), plen); return 1 + (plen + 7) / 8; -trunc: - return -2; +badplen: + ND_PRINT("\n\t (illegal prefix length)"); + return -1; } static int -decode_labeled_vpn_clnp_prefix(netdissect_options *ndo, - const u_char *pptr, char *buf, size_t buflen) +print_labeled_vpn_clnp_prefix(netdissect_options *ndo, const u_char *pptr) { uint8_t addr[19]; u_int plen; @@ -1521,22 +1559,21 @@ decode_labeled_vpn_clnp_prefix(netdissect_options *ndo, plen = GET_U_1(pptr); /* get prefix length */ if ((24+64) > plen) - return -1; + goto badplen; plen -= (24+64); /* adjust prefixlen - labellength - RD len*/ if (152 < plen) - return -1; + goto badplen; memset(&addr, 0, sizeof(addr)); - ND_TCHECK_LEN(pptr + 12, (plen + 7) / 8); - memcpy(&addr, pptr + 12, (plen + 7) / 8); + GET_CPY_BYTES(&addr, pptr + 12, (plen + 7) / 8); if (plen % 8) { addr[(plen + 7) / 8 - 1] &= ((0xff00 >> (plen % 8)) & 0xff); } /* the label may get offsetted by 4 bits so lets shift it right */ /* Cannot use GET_ISONSAP_STRING (not packet buffer pointer) */ - snprintf(buf, buflen, "RD: %s, %s/%u, label:%u %s", + ND_PRINT("\n\t RD: %s, %s/%u, label:%u %s", bgp_vpn_rd_print(ndo, pptr+4), isonsap_string(ndo, addr,(plen + 7) / 8), plen, @@ -1545,8 +1582,9 @@ decode_labeled_vpn_clnp_prefix(netdissect_options *ndo, return 12 + (plen + 7) / 8; -trunc: - return -2; +badplen: + ND_PRINT("\n\t (illegal prefix length)"); + return -1; } /* @@ -1598,7 +1636,7 @@ trunc: /* * We can come here, either we did not have enough data, or if we * try to decode 4 byte ASs in 2 byte format. Either way, return 4, - * so that calller can try to decode each AS as of 4 bytes. If indeed + * so that caller can try to decode each AS as of 4 bytes. If indeed * there was not enough data, it will crib and end the parse anyways. */ return 4; @@ -1621,7 +1659,7 @@ check_add_path(netdissect_options *ndo, const u_char *pptr, u_int length, } /* - * Scan through the NLRI information under the assumpetion that + * Scan through the NLRI information under the assumption that * it doesn't have path IDs. */ for (offset = 0; offset < length;) { @@ -1692,23 +1730,23 @@ bgp_mp_af_print(netdissect_options *ndo, safi); switch(af<<8 | safi) { - case (AFNUM_INET<<8 | SAFNUM_UNICAST): - case (AFNUM_INET<<8 | SAFNUM_MULTICAST): - case (AFNUM_INET<<8 | SAFNUM_UNIMULTICAST): - case (AFNUM_INET<<8 | SAFNUM_LABUNICAST): - case (AFNUM_INET<<8 | SAFNUM_RT_ROUTING_INFO): - case (AFNUM_INET<<8 | SAFNUM_VPNUNICAST): - case (AFNUM_INET<<8 | SAFNUM_VPNMULTICAST): - case (AFNUM_INET<<8 | SAFNUM_VPNUNIMULTICAST): - case (AFNUM_INET<<8 | SAFNUM_MULTICAST_VPN): - case (AFNUM_INET<<8 | SAFNUM_MDT): - case (AFNUM_INET6<<8 | SAFNUM_UNICAST): - case (AFNUM_INET6<<8 | SAFNUM_MULTICAST): - case (AFNUM_INET6<<8 | SAFNUM_UNIMULTICAST): - case (AFNUM_INET6<<8 | SAFNUM_LABUNICAST): - case (AFNUM_INET6<<8 | SAFNUM_VPNUNICAST): - case (AFNUM_INET6<<8 | SAFNUM_VPNMULTICAST): - case (AFNUM_INET6<<8 | SAFNUM_VPNUNIMULTICAST): + case (AFNUM_IP<<8 | SAFNUM_UNICAST): + case (AFNUM_IP<<8 | SAFNUM_MULTICAST): + case (AFNUM_IP<<8 | SAFNUM_UNIMULTICAST): + case (AFNUM_IP<<8 | SAFNUM_LABUNICAST): + case (AFNUM_IP<<8 | SAFNUM_RT_ROUTING_INFO): + case (AFNUM_IP<<8 | SAFNUM_VPNUNICAST): + case (AFNUM_IP<<8 | SAFNUM_VPNMULTICAST): + case (AFNUM_IP<<8 | SAFNUM_VPNUNIMULTICAST): + case (AFNUM_IP<<8 | SAFNUM_MULTICAST_VPN): + case (AFNUM_IP<<8 | SAFNUM_MDT): + case (AFNUM_IP6<<8 | SAFNUM_UNICAST): + case (AFNUM_IP6<<8 | SAFNUM_MULTICAST): + case (AFNUM_IP6<<8 | SAFNUM_UNIMULTICAST): + case (AFNUM_IP6<<8 | SAFNUM_LABUNICAST): + case (AFNUM_IP6<<8 | SAFNUM_VPNUNICAST): + case (AFNUM_IP6<<8 | SAFNUM_VPNMULTICAST): + case (AFNUM_IP6<<8 | SAFNUM_VPNUNIMULTICAST): case (AFNUM_NSAP<<8 | SAFNUM_UNICAST): case (AFNUM_NSAP<<8 | SAFNUM_MULTICAST): case (AFNUM_NSAP<<8 | SAFNUM_UNIMULTICAST): @@ -1735,26 +1773,24 @@ trunc: static int bgp_nlri_print(netdissect_options *ndo, uint16_t af, uint8_t safi, const u_char *tptr, u_int len, - char *buf, size_t buflen, int add_path4, int add_path6) { int advance; u_int path_id = 0; + char buf[512]; switch (af<<8 | safi) { - case (AFNUM_INET<<8 | SAFNUM_UNICAST): - case (AFNUM_INET<<8 | SAFNUM_MULTICAST): - case (AFNUM_INET<<8 | SAFNUM_UNIMULTICAST): + case (AFNUM_IP<<8 | SAFNUM_UNICAST): + case (AFNUM_IP<<8 | SAFNUM_MULTICAST): + case (AFNUM_IP<<8 | SAFNUM_UNIMULTICAST): if (add_path4) { path_id = GET_BE_U_4(tptr); tptr += 4; } - advance = decode_prefix4(ndo, tptr, len, buf, buflen); + advance = decode_prefix4(ndo, tptr, len, buf, sizeof(buf)); if (advance == -1) ND_PRINT("\n\t (illegal prefix length)"); else if (advance == -2) - goto trunc; - else if (advance == -3) break; /* bytes left, but not enough */ else ND_PRINT("\n\t %s", buf); @@ -1763,66 +1799,42 @@ bgp_nlri_print(netdissect_options *ndo, uint16_t af, uint8_t safi, advance += 4; } break; - case (AFNUM_INET<<8 | SAFNUM_LABUNICAST): - advance = decode_labeled_prefix4(ndo, tptr, len, buf, buflen); - if (advance == -1) - ND_PRINT("\n\t (illegal prefix length)"); - else if (advance == -2) + case (AFNUM_IP<<8 | SAFNUM_LABUNICAST): + advance = print_labeled_prefix4(ndo, tptr, len); + if (advance == -2) goto trunc; - else if (advance == -3) - break; /* bytes left, but not enough */ - else - ND_PRINT("\n\t %s", buf); break; - case (AFNUM_INET<<8 | SAFNUM_VPNUNICAST): - case (AFNUM_INET<<8 | SAFNUM_VPNMULTICAST): - case (AFNUM_INET<<8 | SAFNUM_VPNUNIMULTICAST): - advance = decode_labeled_vpn_prefix4(ndo, tptr, buf, buflen); - if (advance == -1) - ND_PRINT("\n\t (illegal prefix length)"); - else if (advance == -2) - goto trunc; - else - ND_PRINT("\n\t %s", buf); + case (AFNUM_IP<<8 | SAFNUM_VPNUNICAST): + case (AFNUM_IP<<8 | SAFNUM_VPNMULTICAST): + case (AFNUM_IP<<8 | SAFNUM_VPNUNIMULTICAST): + advance = print_labeled_vpn_prefix4(ndo, tptr); break; - case (AFNUM_INET<<8 | SAFNUM_RT_ROUTING_INFO): - advance = decode_rt_routing_info(ndo, tptr); - if (advance == -2) - goto trunc; + case (AFNUM_IP<<8 | SAFNUM_RT_ROUTING_INFO): + advance = print_rt_routing_info(ndo, tptr); break; - case (AFNUM_INET<<8 | SAFNUM_MULTICAST_VPN): /* fall through */ - case (AFNUM_INET6<<8 | SAFNUM_MULTICAST_VPN): - advance = decode_multicast_vpn(ndo, tptr, buf, buflen); - if (advance == -1) - ND_PRINT("\n\t (illegal prefix length)"); - else if (advance == -2) + case (AFNUM_IP<<8 | SAFNUM_MULTICAST_VPN): /* fall through */ + case (AFNUM_IP6<<8 | SAFNUM_MULTICAST_VPN): + advance = print_multicast_vpn(ndo, tptr); + if (advance == -2) goto trunc; - else - ND_PRINT("\n\t %s", buf); break; - case (AFNUM_INET<<8 | SAFNUM_MDT): - advance = decode_mdt_vpn_nlri(ndo, tptr, buf, buflen); - if (advance == -1) - ND_PRINT("\n\t (illegal prefix length)"); - else if (advance == -2) + case (AFNUM_IP<<8 | SAFNUM_MDT): + advance = print_mdt_vpn_nlri(ndo, tptr); + if (advance == -2) goto trunc; - else - ND_PRINT("\n\t %s", buf); break; - case (AFNUM_INET6<<8 | SAFNUM_UNICAST): - case (AFNUM_INET6<<8 | SAFNUM_MULTICAST): - case (AFNUM_INET6<<8 | SAFNUM_UNIMULTICAST): + case (AFNUM_IP6<<8 | SAFNUM_UNICAST): + case (AFNUM_IP6<<8 | SAFNUM_MULTICAST): + case (AFNUM_IP6<<8 | SAFNUM_UNIMULTICAST): if (add_path6) { path_id = GET_BE_U_4(tptr); tptr += 4; } - advance = decode_prefix6(ndo, tptr, len, buf, buflen); + advance = decode_prefix6(ndo, tptr, len, buf, sizeof(buf)); if (advance == -1) ND_PRINT("\n\t (illegal prefix length)"); else if (advance == -2) - goto trunc; - else if (advance == -3) break; /* bytes left, but not enough */ else ND_PRINT("\n\t %s", buf); @@ -1831,61 +1843,33 @@ bgp_nlri_print(netdissect_options *ndo, uint16_t af, uint8_t safi, advance += 4; } break; - case (AFNUM_INET6<<8 | SAFNUM_LABUNICAST): - advance = decode_labeled_prefix6(ndo, tptr, len, buf, buflen); - if (advance == -1) - ND_PRINT("\n\t (illegal prefix length)"); - else if (advance == -2) + case (AFNUM_IP6<<8 | SAFNUM_LABUNICAST): + advance = print_labeled_prefix6(ndo, tptr, len); + if (advance == -2) goto trunc; - else if (advance == -3) - break; /* bytes left, but not enough */ - else - ND_PRINT("\n\t %s", buf); break; - case (AFNUM_INET6<<8 | SAFNUM_VPNUNICAST): - case (AFNUM_INET6<<8 | SAFNUM_VPNMULTICAST): - case (AFNUM_INET6<<8 | SAFNUM_VPNUNIMULTICAST): - advance = decode_labeled_vpn_prefix6(ndo, tptr, buf, buflen); - if (advance == -1) - ND_PRINT("\n\t (illegal prefix length)"); - else if (advance == -2) - goto trunc; - else - ND_PRINT("\n\t %s", buf); + case (AFNUM_IP6<<8 | SAFNUM_VPNUNICAST): + case (AFNUM_IP6<<8 | SAFNUM_VPNMULTICAST): + case (AFNUM_IP6<<8 | SAFNUM_VPNUNIMULTICAST): + advance = print_labeled_vpn_prefix6(ndo, tptr); break; case (AFNUM_VPLS<<8 | SAFNUM_VPLS): case (AFNUM_L2VPN<<8 | SAFNUM_VPNUNICAST): case (AFNUM_L2VPN<<8 | SAFNUM_VPNMULTICAST): case (AFNUM_L2VPN<<8 | SAFNUM_VPNUNIMULTICAST): - advance = decode_labeled_vpn_l2(ndo, tptr, buf, buflen); - if (advance == -1) - ND_PRINT("\n\t (illegal length)"); - else if (advance == -2) + advance = print_labeled_vpn_l2(ndo, tptr); + if (advance == -2) goto trunc; - else - ND_PRINT("\n\t %s", buf); break; case (AFNUM_NSAP<<8 | SAFNUM_UNICAST): case (AFNUM_NSAP<<8 | SAFNUM_MULTICAST): case (AFNUM_NSAP<<8 | SAFNUM_UNIMULTICAST): - advance = decode_clnp_prefix(ndo, tptr, buf, buflen); - if (advance == -1) - ND_PRINT("\n\t (illegal prefix length)"); - else if (advance == -2) - goto trunc; - else - ND_PRINT("\n\t %s", buf); + advance = print_clnp_prefix(ndo, tptr); break; case (AFNUM_NSAP<<8 | SAFNUM_VPNUNICAST): case (AFNUM_NSAP<<8 | SAFNUM_VPNMULTICAST): case (AFNUM_NSAP<<8 | SAFNUM_VPNUNIMULTICAST): - advance = decode_labeled_vpn_clnp_prefix(ndo, tptr, buf, buflen); - if (advance == -1) - ND_PRINT("\n\t (illegal prefix length)"); - else if (advance == -2) - goto trunc; - else - ND_PRINT("\n\t %s", buf); + advance = print_labeled_vpn_clnp_prefix(ndo, tptr); break; default: /* @@ -1901,18 +1885,27 @@ trunc: /* we rely on the caller to recognize -2 return value */ return -2; } +static const struct tok bgp_flags[] = { + { 0x80, "O"}, + { 0x40, "T"}, + { 0x20, "P"}, + { 0x10, "E"}, + { 0, NULL } +}; + static int bgp_attr_print(netdissect_options *ndo, uint8_t atype, const u_char *pptr, u_int len, const unsigned attr_set_level) { + /* allocate space for the largest possible string */ + char astostr[AS_STR_SIZE]; u_int i; uint16_t af; uint8_t safi, snpa, nhlen; int advance; u_int tlen; const u_char *tptr; - char buf[MAXHOSTNAMELEN + 100]; u_int as_size; int add_path4, add_path6; int ret; @@ -2105,13 +2098,13 @@ bgp_attr_print(netdissect_options *ndo, ND_PRINT(", " ); } switch(af<<8 | safi) { - case (AFNUM_INET<<8 | SAFNUM_UNICAST): - case (AFNUM_INET<<8 | SAFNUM_MULTICAST): - case (AFNUM_INET<<8 | SAFNUM_UNIMULTICAST): - case (AFNUM_INET<<8 | SAFNUM_LABUNICAST): - case (AFNUM_INET<<8 | SAFNUM_RT_ROUTING_INFO): - case (AFNUM_INET<<8 | SAFNUM_MULTICAST_VPN): - case (AFNUM_INET<<8 | SAFNUM_MDT): + case (AFNUM_IP<<8 | SAFNUM_UNICAST): + case (AFNUM_IP<<8 | SAFNUM_MULTICAST): + case (AFNUM_IP<<8 | SAFNUM_UNIMULTICAST): + case (AFNUM_IP<<8 | SAFNUM_LABUNICAST): + case (AFNUM_IP<<8 | SAFNUM_RT_ROUTING_INFO): + case (AFNUM_IP<<8 | SAFNUM_MULTICAST_VPN): + case (AFNUM_IP<<8 | SAFNUM_MDT): if (tnhlen < sizeof(nd_ipv4)) { ND_PRINT("invalid len"); tptr += tnhlen; @@ -2124,9 +2117,9 @@ bgp_attr_print(netdissect_options *ndo, tlen -= sizeof(nd_ipv4); } break; - case (AFNUM_INET<<8 | SAFNUM_VPNUNICAST): - case (AFNUM_INET<<8 | SAFNUM_VPNMULTICAST): - case (AFNUM_INET<<8 | SAFNUM_VPNUNIMULTICAST): + case (AFNUM_IP<<8 | SAFNUM_VPNUNICAST): + case (AFNUM_IP<<8 | SAFNUM_VPNMULTICAST): + case (AFNUM_IP<<8 | SAFNUM_VPNUNIMULTICAST): if (tnhlen < sizeof(nd_ipv4)+BGP_VPN_RD_LEN) { ND_PRINT("invalid len"); tptr += tnhlen; @@ -2141,10 +2134,10 @@ bgp_attr_print(netdissect_options *ndo, tnhlen -= (sizeof(nd_ipv4)+BGP_VPN_RD_LEN); } break; - case (AFNUM_INET6<<8 | SAFNUM_UNICAST): - case (AFNUM_INET6<<8 | SAFNUM_MULTICAST): - case (AFNUM_INET6<<8 | SAFNUM_UNIMULTICAST): - case (AFNUM_INET6<<8 | SAFNUM_LABUNICAST): + case (AFNUM_IP6<<8 | SAFNUM_UNICAST): + case (AFNUM_IP6<<8 | SAFNUM_MULTICAST): + case (AFNUM_IP6<<8 | SAFNUM_UNIMULTICAST): + case (AFNUM_IP6<<8 | SAFNUM_LABUNICAST): if (tnhlen < sizeof(nd_ipv6)) { ND_PRINT("invalid len"); tptr += tnhlen; @@ -2157,9 +2150,9 @@ bgp_attr_print(netdissect_options *ndo, tnhlen -= sizeof(nd_ipv6); } break; - case (AFNUM_INET6<<8 | SAFNUM_VPNUNICAST): - case (AFNUM_INET6<<8 | SAFNUM_VPNMULTICAST): - case (AFNUM_INET6<<8 | SAFNUM_VPNUNIMULTICAST): + case (AFNUM_IP6<<8 | SAFNUM_VPNUNICAST): + case (AFNUM_IP6<<8 | SAFNUM_VPNMULTICAST): + case (AFNUM_IP6<<8 | SAFNUM_VPNUNIMULTICAST): if (tnhlen < sizeof(nd_ipv6)+BGP_VPN_RD_LEN) { ND_PRINT("invalid len"); tptr += tnhlen; @@ -2233,7 +2226,6 @@ bgp_attr_print(netdissect_options *ndo, tlen -= tnhlen; tnhlen = 0; goto done; - break; } } } @@ -2250,8 +2242,8 @@ bgp_attr_print(netdissect_options *ndo, ND_PRINT("\n\t %u SNPA", snpa); for (/*nothing*/; snpa != 0; snpa--) { uint8_t snpalen; - if (tlen < 1) - goto trunc; + if (tlen < 1) + goto trunc; snpalen = GET_U_1(tptr); ND_PRINT("\n\t %u bytes", snpalen); tptr++; @@ -2266,11 +2258,13 @@ bgp_attr_print(netdissect_options *ndo, ND_PRINT(", no SNPA"); } - add_path4 = check_add_path(ndo, tptr, (len-ND_BYTES_BETWEEN(tptr, pptr)), 32); - add_path6 = check_add_path(ndo, tptr, (len-ND_BYTES_BETWEEN(tptr, pptr)), 128); + add_path4 = check_add_path(ndo, tptr, + (len-ND_BYTES_BETWEEN(pptr, tptr)), 32); + add_path6 = check_add_path(ndo, tptr, + (len-ND_BYTES_BETWEEN(pptr, tptr)), 128); while (tptr < pptr + len) { - advance = bgp_nlri_print(ndo, af, safi, tptr, len, buf, sizeof(buf), + advance = bgp_nlri_print(ndo, af, safi, tptr, len, add_path4, add_path6); if (advance == -2) goto trunc; @@ -2293,11 +2287,13 @@ bgp_attr_print(netdissect_options *ndo, tptr += 3; - add_path4 = check_add_path(ndo, tptr, (len-ND_BYTES_BETWEEN(tptr, pptr)), 32); - add_path6 = check_add_path(ndo, tptr, (len-ND_BYTES_BETWEEN(tptr, pptr)), 128); + add_path4 = check_add_path(ndo, tptr, + (len-ND_BYTES_BETWEEN(pptr, tptr)), 32); + add_path6 = check_add_path(ndo, tptr, + (len-ND_BYTES_BETWEEN(pptr, tptr)), 128); while (tptr < pptr + len) { - advance = bgp_nlri_print(ndo, af, safi, tptr, len, buf, sizeof(buf), + advance = bgp_nlri_print(ndo, af, safi, tptr, len, add_path4, add_path6); if (advance == -2) goto trunc; @@ -2412,7 +2408,6 @@ bgp_attr_print(netdissect_options *ndo, /* * Check if we can read the TLV data. */ - ND_TCHECK_LEN(tptr + 3, length); if (tlen < length) goto trunc; @@ -2476,11 +2471,8 @@ bgp_attr_print(netdissect_options *ndo, alen); if (aflags) { - ND_PRINT(", Flags [%s%s%s%s", - aflags & 0x80 ? "O" : "", - aflags & 0x40 ? "T" : "", - aflags & 0x20 ? "P" : "", - aflags & 0x10 ? "E" : ""); + ND_PRINT(", Flags [%s", + bittok2str_nosep(bgp_flags, "", aflags)); if (aflags & 0xf) ND_PRINT("+%x", aflags & 0xf); ND_PRINT("]"); @@ -2529,6 +2521,83 @@ bgp_attr_print(netdissect_options *ndo, len -= 12; } break; + case BGPTYPE_BGPSEC_PATH: + { + uint16_t sblen, splen; + + splen = GET_BE_U_2(tptr); + + /* + * A secure path has a minimum length of 8 bytes: + * 2 bytes length field + * 6 bytes per secure path segment + */ + ND_ICHECKMSG_U("secure path length", splen, <, 8); + + ND_PRINT("\n\t Secure Path Length: %u", splen); + + tptr += 2; + splen -= 2; + /* Make sure the secure path length does not signal trailing bytes */ + if (splen % 6) { + ND_PRINT(" [total segments length %u != N x 6]", splen); + goto invalid; + } + + /* Parse secure path segments */ + while (splen != 0) { + uint8_t pcount = GET_U_1(tptr); + uint8_t flags = GET_U_1(tptr + 1); + uint32_t asn = GET_BE_U_4(tptr + 2); + ND_PRINT("\n\t Secure Path Segment: pCount: %u, Flags: [%s] (0x%02x), AS: %u", + pcount, + bittok2str(bgp_bgpsec_bitmap_str, "none", flags), + flags, + asn); + tptr += 6; + splen -= 6; + } + + sblen = GET_BE_U_2(tptr); + + ND_PRINT("\n\t Signature Block: Length: %u, Algo ID: %u", + sblen, + GET_U_1(tptr + 2)); + + tptr += 3; + sblen -= 3; + /* Parse signature segments */ + while (sblen != 0) { + uint16_t siglen; + + ND_PRINT("\n\t Signature Segment:\n\t SKI: "); + ND_ICHECKMSG_U("remaining length", sblen, <, 20); + hex_print(ndo, "\n\t ", tptr, 20); + tptr += 20; + sblen -= 20; + ND_ICHECKMSG_U("remaining length", sblen, <, 2); + siglen = GET_BE_U_2(tptr); + tptr += 2; + sblen -= 2; + + ND_PRINT("\n\t Length: %u", siglen); + ND_ICHECKMSG_U("remaining length", sblen, <, siglen); + ND_PRINT("\n\t Signature:"); + hex_print(ndo, "\n\t ", tptr, siglen); + tptr += siglen; + sblen -= siglen; + } + break; + } + case BGPTYPE_OTC: + { + if (len < 4) { + ND_PRINT("invalid len"); + break; + } + ND_PRINT("\n\t OTC %u", GET_BE_U_4(pptr)); + break; + } default: ND_TCHECK_LEN(pptr, len); ND_PRINT("\n\t no Attribute %u decoder", atype); /* we have no decoder for the attribute */ @@ -2543,6 +2612,10 @@ done: } return 1; +invalid: + nd_print_invalid(ndo); + return 1; + trunc: return 0; } @@ -2551,6 +2624,8 @@ static void bgp_capabilities_print(netdissect_options *ndo, const u_char *opt, u_int caps_len) { + /* allocate space for the largest possible string */ + char astostr[AS_STR_SIZE]; u_int cap_type, cap_len, tcap_len, cap_offset; u_int i = 0; @@ -2576,6 +2651,23 @@ bgp_capabilities_print(netdissect_options *ndo, tok2str(bgp_safi_values, "Unknown", GET_U_1(opt + i + 5)), GET_U_1(opt + i + 5)); break; + case BGP_CAPCODE_BGPSEC: + /* Version (4 bits), Direction (1 bit), Flags (3 bits), AFI (16 bits) */ + cap_offset = 1; + /* The capability length [...] MUST be set to 3. */ + if (cap_len != 3) { + ND_PRINT(" [%u != 3]", cap_len); + return; + } + + ND_PRINT("\n\t\tVersion %u, Direction %s (%u), AFI %s (%u)", + GET_U_1(opt + i + 2)&0xf0, + (GET_U_1(opt + i + 2)&0x08) ? "Send" : "Receive", + (GET_U_1(opt + i + 2)&0x08)>>3, + bittok2str(af_values, "Unknown", + GET_BE_U_2(opt + i + cap_offset + 2)), + GET_BE_U_2(opt + i + cap_offset + 2)); + break; case BGP_CAPCODE_ML: cap_offset = 2; tcap_len = cap_len; @@ -2600,7 +2692,8 @@ bgp_capabilities_print(netdissect_options *ndo, } tcap_len=cap_len; ND_PRINT("\n\t\tRestart Flags: [%s], Restart Time %us", - ((GET_U_1(opt + i + 2))&0x80) ? "R" : "none", + bittok2str(bgp_graceful_restart_comm_flag_values, + "none", GET_U_1(opt + i + 2) >> 4), GET_BE_U_2(opt + i + 2)&0xfff); tcap_len-=2; cap_offset=4; @@ -2617,9 +2710,20 @@ bgp_capabilities_print(netdissect_options *ndo, cap_offset += 4; } break; + case BGP_CAPCODE_ROLE: + if (cap_len < 1) { + ND_PRINT(" (too short, < 1)"); + return; + } + ND_PRINT("\n\t\tRole name %s (%u)", + tok2str(bgp_role_values, "Unassigned", + GET_U_1(opt + i + 2)), GET_U_1(opt + i + 2)); + break; case BGP_CAPCODE_RR: case BGP_CAPCODE_LLGR: case BGP_CAPCODE_RR_CISCO: + case BGP_CAPCODE_EXT_MSG: + case BGP_CAPCODE_ENH_RR: break; case BGP_CAPCODE_AS_NEW: /* @@ -2679,11 +2783,17 @@ static void bgp_open_print(netdissect_options *ndo, const u_char *dat, u_int length) { + /* allocate space for the largest possible string */ + char astostr[AS_STR_SIZE]; const struct bgp_open *bgp_open_header; u_int optslen; + uint8_t opsttype; const struct bgp_opt *bgpopt; const u_char *opt; u_int i; + uint8_t extended_opt_params = 0; + u_int open_size = BGP_OPEN_SIZE; + u_int opt_size = BGP_OPT_SIZE; ND_TCHECK_LEN(dat, BGP_OPEN_SIZE); if (length < BGP_OPEN_SIZE) @@ -2699,22 +2809,32 @@ bgp_open_print(netdissect_options *ndo, GET_BE_U_2(bgp_open_header->bgpo_holdtime)); ND_PRINT("ID %s", GET_IPADDR_STRING(bgp_open_header->bgpo_id)); optslen = GET_U_1(bgp_open_header->bgpo_optlen); - ND_PRINT("\n\t Optional parameters, length: %u", optslen); + opsttype = GET_U_1(bgp_open_header->bgpo_opttype); + if (opsttype == BGP_OPEN_NON_EXT_OPT_TYPE_EXTENDED_LENGTH) { + optslen = GET_BE_U_2(bgp_open_header->bgpo_optlen_extended); + extended_opt_params = 1; + open_size += 3; + opt_size += 1; + } + ND_PRINT("\n\t Optional parameters%s, length: %u", + extended_opt_params ? " (Extended)" : "", optslen); - opt = dat + BGP_OPEN_SIZE; - length -= BGP_OPEN_SIZE; + opt = dat + open_size; + length -= open_size; i = 0; while (i < optslen) { - uint8_t opt_type, opt_len; + uint8_t opt_type; + uint16_t opt_len; - ND_TCHECK_LEN(opt + i, BGP_OPT_SIZE); - if (length < BGP_OPT_SIZE + i) + ND_TCHECK_LEN(opt + i, opt_size); + if (length < opt_size + i) goto trunc; bgpopt = (const struct bgp_opt *)(opt + i); opt_type = GET_U_1(bgpopt->bgpopt_type); - opt_len = GET_U_1(bgpopt->bgpopt_len); - if (BGP_OPT_SIZE + i + opt_len > optslen) { + opt_len = extended_opt_params ? GET_BE_U_2(bgpopt->bgpopt_len) + : GET_U_1(bgpopt->bgpopt_len); + if (opt_size + i + opt_len > optslen) { ND_PRINT("\n\t Option %u, length: %u, goes past the end of the options", opt_type, opt_len); break; @@ -2729,7 +2849,7 @@ bgp_open_print(netdissect_options *ndo, switch(opt_type) { case BGP_OPT_CAP: - bgp_capabilities_print(ndo, opt + BGP_OPT_SIZE + i, + bgp_capabilities_print(ndo, opt + opt_size + i, opt_len); break; @@ -2739,7 +2859,7 @@ bgp_open_print(netdissect_options *ndo, opt_type); break; } - i += BGP_OPT_SIZE + opt_len; + i += opt_size + opt_len; } return; trunc: @@ -2752,7 +2872,7 @@ bgp_update_print(netdissect_options *ndo, { const u_char *p; u_int withdrawn_routes_len; - char buf[MAXHOSTNAMELEN + 100]; + char buf[512]; int wpfx; u_int len; int i; @@ -2800,8 +2920,6 @@ bgp_update_print(netdissect_options *ndo, ND_PRINT("\n\t (illegal prefix length)"); break; } else if (wpfx == -2) - goto trunc; - else if (wpfx == -3) goto trunc; /* bytes left, but not enough */ else { ND_PRINT("\n\t %s", buf); @@ -2835,6 +2953,9 @@ bgp_update_print(netdissect_options *ndo, } if (len) { + /* Make sure the path attributes don't go past the end of the packet */ + if (length < len) + goto trunc; /* do something more useful!*/ while (len) { uint8_t aflags, atype, alenlen; @@ -2875,11 +2996,8 @@ bgp_update_print(netdissect_options *ndo, alen); if (aflags) { - ND_PRINT(", Flags [%s%s%s%s", - aflags & 0x80 ? "O" : "", - aflags & 0x40 ? "T" : "", - aflags & 0x20 ? "P" : "", - aflags & 0x10 ? "E" : ""); + ND_PRINT(", Flags [%s", + bittok2str_nosep(bgp_flags, "", aflags)); if (aflags & 0xf) ND_PRINT("+%x", aflags & 0xf); ND_PRINT("]: "); @@ -2917,8 +3035,6 @@ bgp_update_print(netdissect_options *ndo, ND_PRINT("\n\t (illegal prefix length)"); break; } else if (i == -2) - goto trunc; - else if (i == -3) goto trunc; /* bytes left, but not enough */ else { ND_PRINT("\n\t %s", buf); @@ -2936,23 +3052,10 @@ trunc: } static void -bgp_notification_print(netdissect_options *ndo, - const u_char *dat, u_int length) +bgp_notification_print_code(netdissect_options *ndo, + const u_char *dat, u_int length, + uint8_t bgpn_major, uint8_t bgpn_minor) { - const struct bgp_notification *bgp_notification_header; - const u_char *tptr; - uint8_t bgpn_major, bgpn_minor; - uint8_t shutdown_comm_length; - uint8_t remainder_offset; - - ND_TCHECK_LEN(dat, BGP_NOTIFICATION_SIZE); - if (lengthbgpn_major); - bgpn_minor = GET_U_1(bgp_notification_header->bgpn_minor); - ND_PRINT(", %s (%u)", tok2str(bgp_notify_major_values, "Unknown Error", bgpn_major), @@ -2984,9 +3087,9 @@ bgp_notification_print(netdissect_options *ndo, bgpn_minor), bgpn_minor); break; - case BGP_NOTIFY_MAJOR_CAP: + case BGP_NOTIFY_MAJOR_ROUTEREFRESH: ND_PRINT(" subcode %s (%u)", - tok2str(bgp_notify_minor_cap_values, "Unknown", + tok2str(bgp_notify_minor_routerefresh_values, "Unknown", bgpn_minor), bgpn_minor); break; @@ -2996,57 +3099,84 @@ bgp_notification_print(netdissect_options *ndo, bgpn_minor), bgpn_minor); - /* draft-ietf-idr-cease-subcode-02 mentions optionally 7 bytes + /* RFC 4486 mentions optionally 7 bytes * for the maxprefix subtype, which may contain AFI, SAFI and MAXPREFIXES */ - if(bgpn_minor == BGP_NOTIFY_MINOR_CEASE_MAXPRFX && length >= BGP_NOTIFICATION_SIZE + 7) { - tptr = dat + BGP_NOTIFICATION_SIZE; + if(bgpn_minor == BGP_NOTIFY_MINOR_CEASE_MAXPRFX && length >= 7) { ND_PRINT(", AFI %s (%u), SAFI %s (%u), Max Prefixes: %u", - tok2str(af_values, "Unknown", GET_BE_U_2(tptr)), - GET_BE_U_2(tptr), - tok2str(bgp_safi_values, "Unknown", GET_U_1((tptr + 2))), - GET_U_1((tptr + 2)), - GET_BE_U_4(tptr + 3)); + tok2str(af_values, "Unknown", GET_BE_U_2(dat)), + GET_BE_U_2(dat), + tok2str(bgp_safi_values, "Unknown", GET_U_1((dat + 2))), + GET_U_1((dat + 2)), + GET_BE_U_4(dat + 3)); } /* - * draft-ietf-idr-shutdown describes a method to send a communication + * RFC 9003 describes a method to send a communication * intended for human consumption regarding the Administrative Shutdown */ if ((bgpn_minor == BGP_NOTIFY_MINOR_CEASE_SHUT || bgpn_minor == BGP_NOTIFY_MINOR_CEASE_RESET) && - length >= BGP_NOTIFICATION_SIZE + 1) { - tptr = dat + BGP_NOTIFICATION_SIZE; - shutdown_comm_length = GET_U_1(tptr); - remainder_offset = 0; + length >= 1) { + uint8_t shutdown_comm_length = GET_U_1(dat); + uint8_t remainder_offset = 0; /* garbage, hexdump it all */ - if (shutdown_comm_length > BGP_NOTIFY_MINOR_CEASE_ADMIN_SHUTDOWN_LEN || - shutdown_comm_length > length - (BGP_NOTIFICATION_SIZE + 1)) { + if (shutdown_comm_length > length - 1) { ND_PRINT(", invalid Shutdown Communication length"); - } - else if (shutdown_comm_length == 0) { + } else if (shutdown_comm_length == 0) { ND_PRINT(", empty Shutdown Communication"); remainder_offset += 1; } /* a proper shutdown communication */ else { - ND_TCHECK_LEN(tptr + 1, shutdown_comm_length); ND_PRINT(", Shutdown Communication (length: %u): \"", shutdown_comm_length); - (void)nd_printn(ndo, tptr+1, shutdown_comm_length, NULL); + nd_printjn(ndo, dat+1, shutdown_comm_length); ND_PRINT("\""); remainder_offset += shutdown_comm_length + 1; } /* if there is trailing data, hexdump it */ - if(length - (remainder_offset + BGP_NOTIFICATION_SIZE) > 0) { - ND_PRINT(", Data: (length: %u)", length - (remainder_offset + BGP_NOTIFICATION_SIZE)); - hex_print(ndo, "\n\t\t", tptr + remainder_offset, length - (remainder_offset + BGP_NOTIFICATION_SIZE)); + if(length - remainder_offset > 0) { + ND_PRINT(", Data: (length: %u)", length - remainder_offset); + hex_print(ndo, "\n\t\t", dat + remainder_offset, length - remainder_offset); } } + /* + * RFC8538 describes the Hard Reset cease subcode, which contains another + * notification code and subcode. + */ + if (bgpn_minor == BGP_NOTIFY_MINOR_CEASE_HARDRESET && length >= 2) { + bgpn_major = GET_U_1(dat++); + bgpn_minor = GET_U_1(dat++); + length -= 2; + bgp_notification_print_code(ndo, dat, length, bgpn_major, bgpn_minor); + } break; default: + if (bgpn_minor != 0) { + ND_PRINT(", subcode %u", bgpn_minor); + } break; } return; +} + +static void +bgp_notification_print(netdissect_options *ndo, + const u_char *dat, u_int length) +{ + const struct bgp_notification *bgp_notification_header; + uint8_t bgpn_major, bgpn_minor; + + ND_TCHECK_LEN(dat, BGP_NOTIFICATION_SIZE); + if (lengthbgpn_major); + bgpn_minor = GET_U_1(bgp_notification_header->bgpn_minor); + bgp_notification_print_code(ndo, dat + BGP_NOTIFICATION_SIZE, + length - BGP_NOTIFICATION_SIZE, bgpn_major, bgpn_minor); + return; trunc: nd_print_trunc(ndo); } @@ -3057,21 +3187,37 @@ bgp_route_refresh_print(netdissect_options *ndo, { const struct bgp_route_refresh *bgp_route_refresh_header; - ND_TCHECK_LEN(pptr, BGP_ROUTE_REFRESH_SIZE); - /* some little sanity checking */ if (lenafi)), - GET_BE_U_2(bgp_route_refresh_header->afi), - tok2str(bgp_safi_values,"Unknown", - GET_U_1(bgp_route_refresh_header->safi)), - GET_U_1(bgp_route_refresh_header->safi)); + ND_PRINT("\n\t AFI %s (%u), SAFI %s (%u), Subtype %s (%u)", + tok2str(af_values, "Unknown", + GET_BE_U_2(bgp_route_refresh_header->afi)), + GET_BE_U_2(bgp_route_refresh_header->afi), + tok2str(bgp_safi_values, "Unknown", + GET_U_1(bgp_route_refresh_header->safi)), + GET_U_1(bgp_route_refresh_header->safi), + tok2str(bgp_route_refresh_subtype_values, "Unknown", + GET_U_1(bgp_route_refresh_header->subtype)), + GET_U_1(bgp_route_refresh_header->subtype)); + + /* ORF */ + if (len >= BGP_ROUTE_REFRESH_SIZE_ORF) { + const struct bgp_route_refresh_orf *orf_header; + + orf_header = + (const struct bgp_route_refresh_orf *)(pptr + BGP_ROUTE_REFRESH_SIZE); + + ND_PRINT("\n\t ORF refresh %s (%u), ORF type %s (%u), ORF length %u", + tok2str(bgp_orf_refresh_type, "Unknown", + GET_U_1(orf_header->refresh)), + GET_U_1(orf_header->refresh), + tok2str(bgp_orf_type, "Unknown", GET_U_1(orf_header->type)), + GET_U_1(orf_header->type), GET_BE_U_2(orf_header->len)); + } if (ndo->ndo_vflag > 1) { ND_TCHECK_LEN(pptr, len);