+ return advance;
+trunc: /* we rely on the caller to recognize -2 return value */
+ return -2;
+}
+
+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;
+
+ tptr = pptr;
+ tlen = len;
+
+ switch (atype) {
+ case BGPTYPE_ORIGIN:
+ if (len != 1)
+ ND_PRINT("invalid len");
+ else {
+ ND_PRINT("%s", tok2str(bgp_origin_values,
+ "Unknown Origin Typecode",
+ GET_U_1(tptr)));
+ }
+ break;
+
+ /*
+ * Process AS4 byte path and AS2 byte path attributes here.
+ */
+ case BGPTYPE_AS4_PATH:
+ case BGPTYPE_AS_PATH:
+ if (len % 2) {
+ ND_PRINT("invalid len");
+ break;
+ }
+ if (!len) {
+ ND_PRINT("empty");
+ break;
+ }
+
+ /*
+ * BGP updates exchanged between New speakers that support 4
+ * byte AS, ASs are always encoded in 4 bytes. There is no
+ * definitive way to find this, just by the packet's
+ * contents. So, check for packet's TLV's sanity assuming
+ * 2 bytes first, and it does not pass, assume that ASs are
+ * encoded in 4 bytes format and move on.
+ */
+ as_size = bgp_attr_get_as_size(ndo, atype, pptr, len);
+
+ while (tptr < pptr + len) {
+ ND_PRINT("%s", tok2str(bgp_as_path_segment_open_values,
+ "?", GET_U_1(tptr)));
+ for (i = 0; i < GET_U_1(tptr + 1) * as_size; i += as_size) {
+ ND_TCHECK_LEN(tptr + 2 + i, as_size);
+ ND_PRINT("%s ",
+ as_printf(ndo, astostr, sizeof(astostr),
+ as_size == 2 ?
+ GET_BE_U_2(tptr + i + 2) :
+ GET_BE_U_4(tptr + i + 2)));
+ }
+ ND_PRINT("%s", tok2str(bgp_as_path_segment_close_values,
+ "?", GET_U_1(tptr)));
+ tptr += 2 + GET_U_1(tptr + 1) * as_size;
+ }
+ break;
+ case BGPTYPE_NEXT_HOP:
+ if (len != 4)
+ ND_PRINT("invalid len");
+ else {
+ ND_PRINT("%s", GET_IPADDR_STRING(tptr));
+ }
+ break;
+ case BGPTYPE_MULTI_EXIT_DISC:
+ case BGPTYPE_LOCAL_PREF:
+ if (len != 4)
+ ND_PRINT("invalid len");
+ else {
+ ND_PRINT("%u", GET_BE_U_4(tptr));
+ }
+ break;
+ case BGPTYPE_ATOMIC_AGGREGATE:
+ if (len != 0)
+ ND_PRINT("invalid len");
+ break;
+ case BGPTYPE_AGGREGATOR:
+
+ /*
+ * Depending on the AS encoded is of 2 bytes or of 4 bytes,
+ * the length of this PA can be either 6 bytes or 8 bytes.
+ */
+ if (len != 6 && len != 8) {
+ ND_PRINT("invalid len");
+ break;
+ }
+ ND_TCHECK_LEN(tptr, len);
+ if (len == 6) {
+ ND_PRINT(" AS #%s, origin %s",
+ as_printf(ndo, astostr, sizeof(astostr), GET_BE_U_2(tptr)),
+ GET_IPADDR_STRING(tptr + 2));
+ } else {
+ ND_PRINT(" AS #%s, origin %s",
+ as_printf(ndo, astostr, sizeof(astostr),
+ GET_BE_U_4(tptr)), GET_IPADDR_STRING(tptr + 4));
+ }
+ break;
+ case BGPTYPE_AGGREGATOR4:
+ if (len != 8) {
+ ND_PRINT("invalid len");
+ break;
+ }
+ ND_PRINT(" AS #%s, origin %s",
+ as_printf(ndo, astostr, sizeof(astostr), GET_BE_U_4(tptr)),
+ GET_IPADDR_STRING(tptr + 4));
+ break;
+ case BGPTYPE_COMMUNITIES:
+ if (len % 4) {
+ ND_PRINT("invalid len");
+ break;
+ }
+ while (tlen != 0) {
+ uint32_t comm;
+ ND_TCHECK_4(tptr);
+ if (tlen < 4)
+ goto trunc;
+ comm = GET_BE_U_4(tptr);
+ switch (comm) {
+ case BGP_COMMUNITY_NO_EXPORT:
+ ND_PRINT(" NO_EXPORT");
+ break;
+ case BGP_COMMUNITY_NO_ADVERT:
+ ND_PRINT(" NO_ADVERTISE");
+ break;
+ case BGP_COMMUNITY_NO_EXPORT_SUBCONFED:
+ ND_PRINT(" NO_EXPORT_SUBCONFED");
+ break;
+ default:
+ ND_PRINT("%u:%u%s",
+ (comm >> 16) & 0xffff,
+ comm & 0xffff,
+ (tlen>4) ? ", " : "");
+ break;
+ }
+ tlen -=4;
+ tptr +=4;
+ }
+ break;
+ case BGPTYPE_ORIGINATOR_ID:
+ if (len != 4) {
+ ND_PRINT("invalid len");
+ break;
+ }
+ ND_PRINT("%s",GET_IPADDR_STRING(tptr));
+ break;
+ case BGPTYPE_CLUSTER_LIST:
+ if (len % 4) {
+ ND_PRINT("invalid len");
+ break;
+ }
+ while (tlen != 0) {
+ if (tlen < 4)
+ goto trunc;
+ ND_PRINT("%s%s",
+ GET_IPADDR_STRING(tptr),
+ (tlen>4) ? ", " : "");
+ tlen -=4;
+ tptr +=4;
+ }
+ break;
+ case BGPTYPE_MP_REACH_NLRI:
+ ND_TCHECK_3(tptr);
+ if (tlen < 3)
+ goto trunc;
+ ret = bgp_mp_af_print(ndo, tptr, tlen, &af, &safi);
+ if (ret == -2)
+ goto trunc;
+ if (ret < 0)
+ break;
+
+ tptr += 3;
+ tlen -= 3;
+
+ ND_TCHECK_1(tptr);
+ if (tlen < 1)
+ goto trunc;
+ nhlen = GET_U_1(tptr);
+ tptr++;
+ tlen--;
+
+ if (nhlen) {
+ u_int nnh = 0;
+ uint8_t tnhlen = nhlen;
+ if (tlen < tnhlen)
+ goto trunc;
+ ND_PRINT("\n\t nexthop: ");
+ while (tnhlen != 0) {
+ if (nnh++ > 0) {
+ 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):
+ if (tnhlen < sizeof(nd_ipv4)) {
+ ND_PRINT("invalid len");
+ tptr += tnhlen;
+ tlen -= tnhlen;
+ tnhlen = 0;
+ } else {
+ ND_PRINT("%s",GET_IPADDR_STRING(tptr));
+ tptr += sizeof(nd_ipv4);
+ tnhlen -= sizeof(nd_ipv4);
+ tlen -= sizeof(nd_ipv4);
+ }
+ break;
+ case (AFNUM_INET<<8 | SAFNUM_VPNUNICAST):
+ case (AFNUM_INET<<8 | SAFNUM_VPNMULTICAST):
+ case (AFNUM_INET<<8 | SAFNUM_VPNUNIMULTICAST):
+ if (tnhlen < sizeof(nd_ipv4)+BGP_VPN_RD_LEN) {
+ ND_PRINT("invalid len");
+ tptr += tnhlen;
+ tlen -= tnhlen;
+ tnhlen = 0;
+ } else {
+ ND_PRINT("RD: %s, %s",
+ bgp_vpn_rd_print(ndo, tptr),
+ GET_IPADDR_STRING(tptr+BGP_VPN_RD_LEN));
+ tptr += (sizeof(nd_ipv4)+BGP_VPN_RD_LEN);
+ tlen -= (sizeof(nd_ipv4)+BGP_VPN_RD_LEN);
+ 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):
+ if (tnhlen < sizeof(nd_ipv6)) {
+ ND_PRINT("invalid len");
+ tptr += tnhlen;
+ tlen -= tnhlen;
+ tnhlen = 0;
+ } else {
+ ND_PRINT("%s", GET_IP6ADDR_STRING(tptr));
+ tptr += sizeof(nd_ipv6);
+ tlen -= sizeof(nd_ipv6);
+ tnhlen -= sizeof(nd_ipv6);
+ }
+ break;
+ case (AFNUM_INET6<<8 | SAFNUM_VPNUNICAST):
+ case (AFNUM_INET6<<8 | SAFNUM_VPNMULTICAST):
+ case (AFNUM_INET6<<8 | SAFNUM_VPNUNIMULTICAST):
+ if (tnhlen < sizeof(nd_ipv6)+BGP_VPN_RD_LEN) {
+ ND_PRINT("invalid len");
+ tptr += tnhlen;
+ tlen -= tnhlen;
+ tnhlen = 0;
+ } else {
+ ND_PRINT("RD: %s, %s",
+ bgp_vpn_rd_print(ndo, tptr),
+ GET_IP6ADDR_STRING(tptr+BGP_VPN_RD_LEN));
+ tptr += (sizeof(nd_ipv6)+BGP_VPN_RD_LEN);
+ tlen -= (sizeof(nd_ipv6)+BGP_VPN_RD_LEN);
+ tnhlen -= (sizeof(nd_ipv6)+BGP_VPN_RD_LEN);
+ }
+ 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):
+ if (tnhlen < sizeof(nd_ipv4)) {
+ ND_PRINT("invalid len");
+ tptr += tnhlen;
+ tlen -= tnhlen;
+ tnhlen = 0;
+ } else {
+ ND_PRINT("%s", GET_IPADDR_STRING(tptr));
+ tptr += (sizeof(nd_ipv4));
+ tlen -= (sizeof(nd_ipv4));
+ tnhlen -= (sizeof(nd_ipv4));
+ }
+ break;
+ case (AFNUM_NSAP<<8 | SAFNUM_UNICAST):
+ case (AFNUM_NSAP<<8 | SAFNUM_MULTICAST):
+ case (AFNUM_NSAP<<8 | SAFNUM_UNIMULTICAST):
+ ND_PRINT("%s", GET_ISONSAP_STRING(tptr, tnhlen));
+ tptr += tnhlen;
+ tlen -= tnhlen;
+ tnhlen = 0;
+ break;
+
+ case (AFNUM_NSAP<<8 | SAFNUM_VPNUNICAST):
+ case (AFNUM_NSAP<<8 | SAFNUM_VPNMULTICAST):
+ case (AFNUM_NSAP<<8 | SAFNUM_VPNUNIMULTICAST):
+ if (tnhlen < BGP_VPN_RD_LEN+1) {
+ ND_PRINT("invalid len");
+ tptr += tnhlen;
+ tlen -= tnhlen;
+ tnhlen = 0;
+ } else {
+ ND_TCHECK_LEN(tptr, tnhlen);
+ ND_PRINT("RD: %s, %s",
+ bgp_vpn_rd_print(ndo, tptr),
+ GET_ISONSAP_STRING(tptr+BGP_VPN_RD_LEN,tnhlen-BGP_VPN_RD_LEN));
+ /* rfc986 mapped IPv4 address ? */
+ if (GET_BE_U_4(tptr + BGP_VPN_RD_LEN) == 0x47000601)
+ ND_PRINT(" = %s", GET_IPADDR_STRING(tptr+BGP_VPN_RD_LEN+4));
+ /* rfc1888 mapped IPv6 address ? */
+ else if (GET_BE_U_3(tptr + BGP_VPN_RD_LEN) == 0x350000)
+ ND_PRINT(" = %s", GET_IP6ADDR_STRING(tptr+BGP_VPN_RD_LEN+3));
+ tptr += tnhlen;
+ tlen -= tnhlen;
+ tnhlen = 0;
+ }
+ break;
+ default:
+ /*
+ * bgp_mp_af_print() should have saved us from
+ * an unsupported AFI/SAFI.
+ */
+ ND_PRINT("ERROR: no AFI %u/SAFI %u nexthop decoder", af, safi);
+ tptr += tnhlen;
+ tlen -= tnhlen;
+ tnhlen = 0;
+ goto done;
+ break;
+ }
+ }
+ }
+ ND_PRINT(", nh-length: %u", nhlen);
+
+ /* As per RFC 2858; this is reserved in RFC 4760 */
+ if (tlen < 1)
+ goto trunc;
+ snpa = GET_U_1(tptr);
+ tptr++;
+ tlen--;
+
+ if (snpa) {
+ ND_PRINT("\n\t %u SNPA", snpa);
+ for (/*nothing*/; snpa != 0; snpa--) {
+ uint8_t snpalen;
+ if (tlen < 1)
+ goto trunc;
+ snpalen = GET_U_1(tptr);
+ ND_PRINT("\n\t %u bytes", snpalen);
+ tptr++;
+ tlen--;
+ if (tlen < snpalen)
+ goto trunc;
+ ND_TCHECK_LEN(tptr, snpalen);
+ tptr += snpalen;
+ tlen -= snpalen;
+ }
+ } else {
+ 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);
+
+ while (tptr < pptr + len) {
+ advance = bgp_nlri_print(ndo, af, safi, tptr, len, buf, sizeof(buf),
+ add_path4, add_path6);
+ if (advance == -2)
+ goto trunc;
+ if (advance < 0)
+ break;
+ tptr += advance;
+ }
+ break;
+
+ case BGPTYPE_MP_UNREACH_NLRI:
+ ND_TCHECK_LEN(tptr, BGP_MP_NLRI_MINSIZE);
+ ret = bgp_mp_af_print(ndo, tptr, tlen, &af, &safi);
+ if (ret == -2)
+ goto trunc;
+ if (ret < 0)
+ break;
+
+ if (len == BGP_MP_NLRI_MINSIZE)
+ ND_PRINT("\n\t End-of-Rib Marker (empty NLRI)");
+
+ 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);
+
+ while (tptr < pptr + len) {
+ advance = bgp_nlri_print(ndo, af, safi, tptr, len, buf, sizeof(buf),
+ add_path4, add_path6);
+ if (advance == -2)
+ goto trunc;
+ if (advance < 0)
+ break;
+ tptr += advance;
+ }
+ break;
+ case BGPTYPE_EXTD_COMMUNITIES:
+ if (len % 8) {
+ ND_PRINT("invalid len");
+ break;
+ }
+ while (tlen != 0) {
+ uint16_t extd_comm;
+
+ ND_TCHECK_2(tptr);
+ if (tlen < 2)
+ goto trunc;
+ extd_comm=GET_BE_U_2(tptr);
+
+ ND_PRINT("\n\t %s (0x%04x), Flags [%s]",
+ tok2str(bgp_extd_comm_subtype_values,
+ "unknown extd community typecode",
+ extd_comm),
+ extd_comm,
+ bittok2str(bgp_extd_comm_flag_values, "none", extd_comm));
+
+ ND_TCHECK_8(tptr);
+ if (tlen < 8)
+ goto trunc;
+ ND_PRINT(": ");
+ bgp_extended_community_print(ndo, tptr);
+ tlen -= 8;
+ tptr += 8;
+ }
+ break;
+
+ case BGPTYPE_PMSI_TUNNEL:
+ {
+ uint8_t tunnel_type, flags;
+
+ ND_TCHECK_5(tptr);
+ if (tlen < 5)
+ goto trunc;
+ flags = GET_U_1(tptr);
+ tunnel_type = GET_U_1(tptr + 1);
+
+ ND_PRINT("\n\t Tunnel-type %s (%u), Flags [%s], MPLS Label %u",
+ tok2str(bgp_pmsi_tunnel_values, "Unknown", tunnel_type),
+ tunnel_type,
+ bittok2str(bgp_pmsi_flag_values, "none", flags),
+ GET_BE_U_3(tptr + 2)>>4);
+
+ tptr +=5;
+ tlen -= 5;
+
+ switch (tunnel_type) {
+ case BGP_PMSI_TUNNEL_PIM_SM: /* fall through */
+ case BGP_PMSI_TUNNEL_PIM_BIDIR:
+ ND_PRINT("\n\t Sender %s, P-Group %s",
+ GET_IPADDR_STRING(tptr),
+ GET_IPADDR_STRING(tptr+4));
+ break;
+
+ case BGP_PMSI_TUNNEL_PIM_SSM:
+ ND_PRINT("\n\t Root-Node %s, P-Group %s",
+ GET_IPADDR_STRING(tptr),
+ GET_IPADDR_STRING(tptr+4));
+ break;
+ case BGP_PMSI_TUNNEL_INGRESS:
+ ND_PRINT("\n\t Tunnel-Endpoint %s",
+ GET_IPADDR_STRING(tptr));
+ break;
+ case BGP_PMSI_TUNNEL_LDP_P2MP: /* fall through */
+ case BGP_PMSI_TUNNEL_LDP_MP2MP:
+ ND_PRINT("\n\t Root-Node %s, LSP-ID 0x%08x",
+ GET_IPADDR_STRING(tptr),
+ GET_BE_U_4(tptr + 4));
+ break;
+ case BGP_PMSI_TUNNEL_RSVP_P2MP:
+ ND_PRINT("\n\t Extended-Tunnel-ID %s, P2MP-ID 0x%08x",
+ GET_IPADDR_STRING(tptr),
+ GET_BE_U_4(tptr + 4));
+ break;
+ default:
+ if (ndo->ndo_vflag <= 1) {
+ print_unknown_data(ndo, tptr, "\n\t ", tlen);
+ }
+ }
+ break;
+ }
+ case BGPTYPE_AIGP:
+ {
+ uint8_t type;
+ uint16_t length;
+
+ while (tlen >= 3) {
+ type = GET_U_1(tptr);
+ length = GET_BE_U_2(tptr + 1);
+ tptr += 3;
+ tlen -= 3;
+
+ ND_PRINT("\n\t %s TLV (%u), length %u",
+ tok2str(bgp_aigp_values, "Unknown", type),
+ type, length);
+
+ if (length < 3)
+ goto trunc;
+ length -= 3;
+
+ /*
+ * Check if we can read the TLV data.
+ */
+ if (tlen < length)
+ goto trunc;
+
+ switch (type) {
+
+ case BGP_AIGP_TLV:
+ if (length < 8)
+ goto trunc;
+ ND_PRINT(", metric %" PRIu64,
+ GET_BE_U_8(tptr));
+ break;
+
+ default:
+ if (ndo->ndo_vflag <= 1) {
+ print_unknown_data(ndo, tptr,"\n\t ", length);
+ }
+ }
+
+ tptr += length;
+ tlen -= length;
+ }
+ break;
+ }
+ case BGPTYPE_ATTR_SET:
+ ND_TCHECK_4(tptr);
+ if (len < 4)
+ goto trunc;
+ ND_PRINT("\n\t Origin AS: %s",
+ as_printf(ndo, astostr, sizeof(astostr), GET_BE_U_4(tptr)));
+ tptr += 4;
+ len -= 4;
+
+ while (len) {
+ u_int aflags, alenlen, alen;
+
+ ND_TCHECK_2(tptr);
+ if (len < 2) {
+ ND_PRINT(" [path attr too short]");
+ tptr += len;
+ break;
+ }
+ aflags = GET_U_1(tptr);
+ atype = GET_U_1(tptr + 1);
+ tptr += 2;
+ len -= 2;
+ alenlen = bgp_attr_lenlen(aflags, tptr);
+ ND_TCHECK_LEN(tptr, alenlen);
+ if (len < alenlen) {
+ ND_PRINT(" [path attr too short]");
+ tptr += len;
+ break;
+ }
+ alen = bgp_attr_len(aflags, tptr);
+ tptr += alenlen;
+ len -= alenlen;
+
+ ND_PRINT("\n\t %s (%u), length: %u",
+ tok2str(bgp_attr_values,
+ "Unknown Attribute", atype),
+ atype,
+ alen);
+
+ if (aflags) {
+ ND_PRINT(", Flags [%s%s%s%s",
+ aflags & 0x80 ? "O" : "",
+ aflags & 0x40 ? "T" : "",
+ aflags & 0x20 ? "P" : "",
+ aflags & 0x10 ? "E" : "");
+ if (aflags & 0xf)
+ ND_PRINT("+%x", aflags & 0xf);
+ ND_PRINT("]");
+ }
+ ND_PRINT(": ");
+ if (len < alen) {
+ ND_PRINT(" [path attr too short]");
+ tptr += len;
+ break;
+ }
+ /*
+ * The protocol encoding per se allows ATTR_SET to be nested
+ * as many times as the message can accommodate. This printer
+ * used to be able to recurse into ATTR_SET contents until the
+ * stack exhaustion, but now there is a limit on that (if live
+ * protocol exchange goes that many levels deep, something is
+ * probably wrong anyway). Feel free to refine this value if
+ * you can find the spec with respective normative text.
+ */
+ if (attr_set_level == 10)
+ ND_PRINT("(too many nested levels, not recursing)");
+ else if (!bgp_attr_print(ndo, atype, tptr, alen, attr_set_level + 1))
+ return 0;
+ tptr += alen;
+ len -= alen;
+ }
+ break;
+
+ case BGPTYPE_LARGE_COMMUNITY:
+ if (len == 0 || len % 12) {
+ ND_PRINT("invalid len");
+ break;
+ }
+ ND_PRINT("\n\t ");
+ while (len != 0) {
+ ND_PRINT("%u:%u:%u%s",
+ GET_BE_U_4(tptr),
+ GET_BE_U_4(tptr + 4),
+ GET_BE_U_4(tptr + 8),
+ (len > 12) ? ", " : "");
+ tptr += 12;
+ /*
+ * len will always be a multiple of 12, as per the above,
+ * so this will never underflow.
+ */
+ len -= 12;
+ }
+ break;
+ default:
+ ND_TCHECK_LEN(pptr, len);
+ ND_PRINT("\n\t no Attribute %u decoder", atype); /* we have no decoder for the attribute */
+ if (ndo->ndo_vflag <= 1)
+ print_unknown_data(ndo, pptr, "\n\t ", len);
+ break;
+ }
+done:
+ if (ndo->ndo_vflag > 1 && len) { /* omit zero length attributes*/
+ ND_TCHECK_LEN(pptr, len);
+ print_unknown_data(ndo, pptr, "\n\t ", len);
+ }
+ return 1;
+
+trunc:
+ return 0;
+}
+
+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;
+
+ while (i < caps_len) {
+ ND_TCHECK_LEN(opt + i, BGP_CAP_HEADER_SIZE);
+ cap_type=GET_U_1(opt + i);
+ cap_len=GET_U_1(opt + i + 1);
+ ND_PRINT("\n\t %s (%u), length: %u",
+ tok2str(bgp_capcode_values, "Unknown", cap_type),
+ cap_type,
+ cap_len);
+ ND_TCHECK_LEN(opt + 2 + i, cap_len);
+ switch (cap_type) {
+ case BGP_CAPCODE_MP:
+ /* AFI (16 bits), Reserved (8 bits), SAFI (8 bits) */
+ if (cap_len < 4) {
+ ND_PRINT(" (too short, < 4)");
+ return;
+ }
+ ND_PRINT("\n\t\tAFI %s (%u), SAFI %s (%u)",
+ tok2str(af_values, "Unknown", GET_BE_U_2(opt + i + 2)),
+ GET_BE_U_2(opt + i + 2),
+ tok2str(bgp_safi_values, "Unknown", GET_U_1(opt + i + 5)),
+ GET_U_1(opt + i + 5));
+ break;
+ case BGP_CAPCODE_ML:
+ cap_offset = 2;
+ tcap_len = cap_len;
+ while (tcap_len >= 4) {
+ ND_PRINT( "\n\t\tAFI %s (%u), SAFI %s (%u), Count: %u",
+ tok2str(af_values, "Unknown",
+ GET_BE_U_2(opt + i + cap_offset)),
+ GET_BE_U_2(opt + i + cap_offset),
+ tok2str(bgp_safi_values, "Unknown",
+ GET_U_1(opt + i + cap_offset + 2)),
+ GET_U_1(opt + i + cap_offset + 2),
+ GET_U_1(opt + i + cap_offset + 3));
+ tcap_len -= 4;
+ cap_offset += 4;
+ }
+ break;
+ case BGP_CAPCODE_RESTART:
+ /* Restart Flags (4 bits), Restart Time in seconds (12 bits) */
+ if (cap_len < 2) {
+ ND_PRINT(" (too short, < 2)");
+ return;
+ }
+ tcap_len=cap_len;
+ ND_PRINT("\n\t\tRestart Flags: [%s], Restart Time %us",
+ ((GET_U_1(opt + i + 2))&0x80) ? "R" : "none",
+ GET_BE_U_2(opt + i + 2)&0xfff);
+ tcap_len-=2;
+ cap_offset=4;
+ while(tcap_len>=4) {
+ ND_PRINT("\n\t\t AFI %s (%u), SAFI %s (%u), Forwarding state preserved: %s",
+ tok2str(af_values,"Unknown",
+ GET_BE_U_2(opt + i + cap_offset)),
+ GET_BE_U_2(opt + i + cap_offset),
+ tok2str(bgp_safi_values,"Unknown",
+ GET_U_1(opt + i + cap_offset + 2)),
+ GET_U_1(opt + (i + cap_offset + 2)),
+ ((GET_U_1(opt + (i + cap_offset + 3)))&0x80) ? "yes" : "no" );
+ tcap_len -= 4;
+ cap_offset += 4;
+ }
+ 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:
+ /*
+ * Extract the 4 byte AS number encoded.
+ */
+ if (cap_len < 4) {
+ ND_PRINT(" (too short, < 4)");
+ return;
+ }
+ ND_PRINT("\n\t\t 4 Byte AS %s",
+ as_printf(ndo, astostr, sizeof(astostr),
+ GET_BE_U_4(opt + i + 2)));
+ break;
+ case BGP_CAPCODE_ADD_PATH:
+ if (cap_len == 0) {
+ ND_PRINT(" (bogus)"); /* length */
+ break;
+ }
+ tcap_len=cap_len;
+ cap_offset=2;
+ while (tcap_len != 0) {
+ if (tcap_len < 4) {
+ nd_print_invalid(ndo);
+ break;
+ }
+ ND_PRINT("\n\t\tAFI %s (%u), SAFI %s (%u), Send/Receive: %s",
+ tok2str(af_values,"Unknown",GET_BE_U_2(opt + i + cap_offset)),
+ GET_BE_U_2(opt + i + cap_offset),
+ tok2str(bgp_safi_values,"Unknown",GET_U_1(opt + i + cap_offset + 2)),
+ GET_U_1(opt + (i + cap_offset + 2)),
+ tok2str(bgp_add_path_recvsend,"Bogus (0x%02x)",GET_U_1(opt + i + cap_offset + 3))
+ );
+ tcap_len -= 4;
+ cap_offset += 4;
+ }
+ break;
+ default:
+ ND_PRINT("\n\t\tno decoder for Capability %u",
+ cap_type);
+ if (ndo->ndo_vflag <= 1)
+ print_unknown_data(ndo, opt + i + 2, "\n\t\t",
+ cap_len);
+ break;
+ }
+ if (ndo->ndo_vflag > 1 && cap_len != 0) {
+ print_unknown_data(ndo, opt + i + 2, "\n\t\t", cap_len);
+ }
+ i += BGP_CAP_HEADER_SIZE + cap_len;
+ }
+ return;
+
+trunc:
+ nd_print_trunc(ndo);
+}
+
+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)
+ goto trunc;
+
+ bgp_open_header = (const struct bgp_open *)dat;
+
+ ND_PRINT("\n\t Version %u, ",
+ GET_U_1(bgp_open_header->bgpo_version));
+ ND_PRINT("my AS %s, ",
+ as_printf(ndo, astostr, sizeof(astostr), GET_BE_U_2(bgp_open_header->bgpo_myas)));
+ ND_PRINT("Holdtime %us, ",
+ 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);
+ 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 + open_size;
+ length -= open_size;
+
+ i = 0;
+ while (i < optslen) {
+ uint8_t opt_type;
+ uint16_t opt_len;
+
+ 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 = 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;
+ }
+
+ ND_PRINT("\n\t Option %s (%u), length: %u",
+ tok2str(bgp_opt_values,"Unknown",opt_type),
+ opt_type,
+ opt_len);
+
+ /* now let's decode the options we know*/
+ switch(opt_type) {
+
+ case BGP_OPT_CAP:
+ bgp_capabilities_print(ndo, opt + opt_size + i,
+ opt_len);
+ break;
+
+ case BGP_OPT_AUTH:
+ default:
+ ND_PRINT("\n\t no decoder for option %u",
+ opt_type);
+ break;
+ }
+ i += opt_size + opt_len;
+ }
+ return;
+trunc:
+ nd_print_trunc(ndo);
+}
+
+static void
+bgp_update_print(netdissect_options *ndo,
+ const u_char *dat, u_int length)
+{
+ const u_char *p;
+ u_int withdrawn_routes_len;
+ char buf[MAXHOSTNAMELEN + 100];
+ int wpfx;
+ u_int len;
+ int i;
+ int add_path;
+ u_int path_id = 0;
+
+ ND_TCHECK_LEN(dat, BGP_SIZE);
+ if (length < BGP_SIZE)
+ goto trunc;
+ p = dat + BGP_SIZE;
+ length -= BGP_SIZE;
+
+ /* Unfeasible routes */
+ ND_TCHECK_2(p);
+ if (length < 2)
+ goto trunc;
+ withdrawn_routes_len = GET_BE_U_2(p);
+ p += 2;
+ length -= 2;
+ if (withdrawn_routes_len > 1) {
+ /*
+ * Without keeping state from the original NLRI message,
+ * it's not possible to tell if this a v4 or v6 route,
+ * so only try to decode it if we're not v6 enabled.
+ */
+ ND_TCHECK_LEN(p, withdrawn_routes_len);
+ if (length < withdrawn_routes_len)
+ goto trunc;
+ ND_PRINT("\n\t Withdrawn routes:");
+ add_path = check_add_path(ndo, p, withdrawn_routes_len, 32);
+ while (withdrawn_routes_len != 0) {
+ if (add_path) {
+ if (withdrawn_routes_len < 4) {
+ p += withdrawn_routes_len;
+ length -= withdrawn_routes_len;
+ break;
+ }
+ path_id = GET_BE_U_4(p);
+ p += 4;
+ length -= 4;
+ withdrawn_routes_len -= 4;
+ }
+ wpfx = decode_prefix4(ndo, p, withdrawn_routes_len, buf, sizeof(buf));
+ if (wpfx == -1) {
+ ND_PRINT("\n\t (illegal prefix length)");
+ break;
+ } else if (wpfx == -2)
+ goto trunc; /* bytes left, but not enough */
+ else {
+ ND_PRINT("\n\t %s", buf);
+ if (add_path) {
+ ND_PRINT(" Path Id: %u", path_id);
+ }
+ p += wpfx;
+ length -= wpfx;
+ withdrawn_routes_len -= wpfx;
+ }
+ }
+ } else {
+ ND_TCHECK_LEN(p, withdrawn_routes_len);
+ if (length < withdrawn_routes_len)
+ goto trunc;
+ p += withdrawn_routes_len;
+ length -= withdrawn_routes_len;
+ }
+
+ ND_TCHECK_2(p);
+ if (length < 2)
+ goto trunc;
+ len = GET_BE_U_2(p);
+ p += 2;
+ length -= 2;
+
+ if (withdrawn_routes_len == 0 && len == 0 && length == 0) {
+ /* No withdrawn routes, no path attributes, no NLRI */
+ ND_PRINT("\n\t End-of-Rib Marker (empty NLRI)");
+ return;
+ }
+
+ 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;
+ uint16_t alen;
+
+ ND_TCHECK_2(p);
+ if (length < 2)
+ goto trunc;
+ if (len < 2) {
+ ND_PRINT("\n\t [path attrs too short]");
+ p += len;
+ length -= len;
+ break;
+ }
+ aflags = GET_U_1(p);
+ atype = GET_U_1(p + 1);
+ p += 2;
+ len -= 2;
+ length -= 2;
+ alenlen = bgp_attr_lenlen(aflags, p);
+ ND_TCHECK_LEN(p, alenlen);
+ if (length < alenlen)
+ goto trunc;
+ if (len < alenlen) {
+ ND_PRINT("\n\t [path attrs too short]");
+ p += len;
+ length -= len;
+ break;
+ }
+ alen = bgp_attr_len(aflags, p);
+ p += alenlen;
+ len -= alenlen;
+ length -= alenlen;
+
+ ND_PRINT("\n\t %s (%u), length: %u",
+ tok2str(bgp_attr_values, "Unknown Attribute", atype),
+ atype,
+ alen);
+
+ if (aflags) {
+ ND_PRINT(", Flags [%s%s%s%s",
+ aflags & 0x80 ? "O" : "",
+ aflags & 0x40 ? "T" : "",
+ aflags & 0x20 ? "P" : "",
+ aflags & 0x10 ? "E" : "");
+ if (aflags & 0xf)
+ ND_PRINT("+%x", aflags & 0xf);
+ ND_PRINT("]: ");
+ }
+ if (len < alen) {
+ ND_PRINT(" [path attrs too short]");
+ p += len;
+ length -= len;
+ break;
+ }
+ if (length < alen)
+ goto trunc;
+ if (!bgp_attr_print(ndo, atype, p, alen, 0))
+ goto trunc;
+ p += alen;
+ len -= alen;
+ length -= alen;
+ }
+ }
+
+ if (length) {
+ add_path = check_add_path(ndo, p, length, 32);
+ ND_PRINT("\n\t Updated routes:");
+ while (length != 0) {
+ if (add_path) {
+ ND_TCHECK_4(p);
+ if (length < 4)
+ goto trunc;
+ path_id = GET_BE_U_4(p);
+ p += 4;
+ length -= 4;
+ }
+ i = decode_prefix4(ndo, p, length, buf, sizeof(buf));
+ if (i == -1) {
+ ND_PRINT("\n\t (illegal prefix length)");
+ break;
+ } else if (i == -2)
+ goto trunc; /* bytes left, but not enough */
+ else {
+ ND_PRINT("\n\t %s", buf);
+ if (add_path) {
+ ND_PRINT(" Path Id: %u", path_id);
+ }
+ p += i;
+ length -= i;
+ }
+ }
+ }
+ return;
+trunc:
+ nd_print_trunc(ndo);
+}
+
+static void
+bgp_notification_print(netdissect_options *ndo,
+ const u_char *dat, u_int length)
+{
+ const struct bgp_notification *bgp_notification_header;
+ const u_char *tptr;
+ uint8_t bgpn_major, bgpn_minor;
+
+ ND_TCHECK_LEN(dat, BGP_NOTIFICATION_SIZE);
+ if (length<BGP_NOTIFICATION_SIZE)
+ return;
+
+ bgp_notification_header = (const struct bgp_notification *)dat;
+ bgpn_major = GET_U_1(bgp_notification_header->bgpn_major);
+ bgpn_minor = GET_U_1(bgp_notification_header->bgpn_minor);
+
+ ND_PRINT(", %s (%u)",
+ tok2str(bgp_notify_major_values, "Unknown Error",
+ bgpn_major),
+ bgpn_major);
+
+ switch (bgpn_major) {
+
+ case BGP_NOTIFY_MAJOR_MSG:
+ ND_PRINT(", subcode %s (%u)",
+ tok2str(bgp_notify_minor_msg_values, "Unknown",
+ bgpn_minor),
+ bgpn_minor);
+ break;
+ case BGP_NOTIFY_MAJOR_OPEN:
+ ND_PRINT(", subcode %s (%u)",
+ tok2str(bgp_notify_minor_open_values, "Unknown",
+ bgpn_minor),
+ bgpn_minor);
+ break;
+ case BGP_NOTIFY_MAJOR_UPDATE:
+ ND_PRINT(", subcode %s (%u)",
+ tok2str(bgp_notify_minor_update_values, "Unknown",
+ bgpn_minor),
+ bgpn_minor);
+ break;
+ case BGP_NOTIFY_MAJOR_FSM:
+ ND_PRINT(" subcode %s (%u)",
+ tok2str(bgp_notify_minor_fsm_values, "Unknown",
+ bgpn_minor),
+ bgpn_minor);
+ break;
+ case BGP_NOTIFY_MAJOR_CAP:
+ ND_PRINT(" subcode %s (%u)",
+ tok2str(bgp_notify_minor_cap_values, "Unknown",
+ bgpn_minor),
+ bgpn_minor);
+ break;
+ case BGP_NOTIFY_MAJOR_CEASE:
+ ND_PRINT(", subcode %s (%u)",
+ tok2str(bgp_notify_minor_cease_values, "Unknown",
+ bgpn_minor),
+ bgpn_minor);
+
+ /* 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;
+ 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));
+ }
+ /*
+ * 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;
+ uint8_t shutdown_comm_length = GET_U_1(tptr);
+ uint8_t remainder_offset = 0;
+ /* garbage, hexdump it all */
+ if (shutdown_comm_length > length - (BGP_NOTIFICATION_SIZE + 1)) {
+ ND_PRINT(", invalid Shutdown Communication length");
+ }
+ else if (shutdown_comm_length == 0) {
+ ND_PRINT(", empty Shutdown Communication");
+ remainder_offset += 1;
+ }
+ /* a proper shutdown communication */
+ else {
+ ND_PRINT(", Shutdown Communication (length: %u): \"", shutdown_comm_length);
+ nd_printjn(ndo, tptr+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));
+ }
+ }
+ break;
+ default:
+ break;
+ }
+
+ return;
+trunc:
+ nd_print_trunc(ndo);
+}
+
+static void
+bgp_route_refresh_print(netdissect_options *ndo,
+ const u_char *pptr, u_int len)
+{
+ const struct bgp_route_refresh *bgp_route_refresh_header;
+
+ ND_TCHECK_LEN(pptr, BGP_ROUTE_REFRESH_SIZE);
+
+ /* some little sanity checking */
+ if (len<BGP_ROUTE_REFRESH_SIZE)
+ return;
+
+ bgp_route_refresh_header = (const struct bgp_route_refresh *)pptr;
+
+ 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));
+
+ if (ndo->ndo_vflag > 1) {
+ ND_TCHECK_LEN(pptr, len);
+ print_unknown_data(ndo, pptr, "\n\t ", len);
+ }
+
+ return;
+trunc:
+ nd_print_trunc(ndo);
+}
+
+static int
+bgp_pdu_print(netdissect_options *ndo,
+ const u_char *dat, u_int length)
+{
+ const struct bgp *bgp_header;
+ uint8_t bgp_type;
+
+ ND_TCHECK_LEN(dat, BGP_SIZE);
+ bgp_header = (const struct bgp *)dat;
+ bgp_type = GET_U_1(bgp_header->bgp_type);
+
+ ND_PRINT("\n\t%s Message (%u), length: %u",
+ tok2str(bgp_msg_values, "Unknown", bgp_type),
+ bgp_type,
+ length);
+
+ switch (bgp_type) {
+ case BGP_OPEN:
+ bgp_open_print(ndo, dat, length);
+ break;
+ case BGP_UPDATE:
+ bgp_update_print(ndo, dat, length);
+ break;
+ case BGP_NOTIFICATION:
+ bgp_notification_print(ndo, dat, length);
+ break;
+ case BGP_KEEPALIVE:
+ break;
+ case BGP_ROUTE_REFRESH:
+ bgp_route_refresh_print(ndo, dat, length);
+ break;
+ default:
+ /* we have no decoder for the BGP message */
+ ND_TCHECK_LEN(dat, length);
+ ND_PRINT("\n\t no Message %u decoder", bgp_type);
+ print_unknown_data(ndo, dat, "\n\t ", length);
+ break;
+ }
+ return 1;
+trunc:
+ nd_print_trunc(ndo);
+ return 0;
+}
+
+void
+bgp_print(netdissect_options *ndo,
+ const u_char *dat, u_int length _U_)
+{
+ const u_char *p;
+ const u_char *ep = ndo->ndo_snapend;
+ const u_char *start;
+ const u_char marker[] = {
+ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
+ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
+ };
+ const struct bgp *bgp_header;
+ uint16_t hlen;
+
+ ndo->ndo_protocol = "bgp";
+ ND_PRINT(": BGP");
+
+ if (ndo->ndo_vflag < 1) /* lets be less chatty */
+ return;
+
+ p = dat;
+ start = p;
+ while (p < ep) {
+ if (!ND_TTEST_1(p))
+ break;
+ if (GET_U_1(p) != 0xff) {
+ p++;
+ continue;
+ }
+
+ if (!ND_TTEST_LEN(p, sizeof(marker)))
+ break;
+ if (memcmp(p, marker, sizeof(marker)) != 0) {
+ p++;
+ continue;
+ }
+
+ /* found BGP header */
+ ND_TCHECK_LEN(p, BGP_SIZE);
+ bgp_header = (const struct bgp *)p;
+
+ if (start != p)
+ nd_print_trunc(ndo);
+
+ hlen = GET_BE_U_2(bgp_header->bgp_len);
+ if (hlen < BGP_SIZE) {
+ ND_PRINT("\nmessage length %u < %u", hlen, BGP_SIZE);
+ nd_print_invalid(ndo);
+ break;
+ }
+
+ if (ND_TTEST_LEN(p, hlen)) {
+ if (!bgp_pdu_print(ndo, p, hlen))
+ return;
+ p += hlen;
+ start = p;
+ } else {
+ ND_PRINT("\n[|BGP %s]",
+ tok2str(bgp_msg_values,
+ "Unknown Message Type",
+ GET_U_1(bgp_header->bgp_type)));
+ break;
+ }
+ }