+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)) {
+
+ case BGP_EXT_COM_RT_0:
+ case BGP_EXT_COM_RO_0:
+ case BGP_EXT_COM_L2VPN_RT_0:
+ ND_PRINT("%u:%u (= %s)",
+ GET_BE_U_2(pptr + 2),
+ GET_BE_U_4(pptr + 4),
+ GET_IPADDR_STRING(pptr+4));
+ break;
+
+ case BGP_EXT_COM_RT_1:
+ case BGP_EXT_COM_RO_1:
+ case BGP_EXT_COM_L2VPN_RT_1:
+ case BGP_EXT_COM_VRF_RT_IMP:
+ ND_PRINT("%s:%u",
+ GET_IPADDR_STRING(pptr+2),
+ GET_BE_U_2(pptr + 6));
+ break;
+
+ case BGP_EXT_COM_RT_2:
+ case BGP_EXT_COM_RO_2:
+ ND_PRINT("%s:%u",
+ as_printf(ndo, astostr, sizeof(astostr),
+ GET_BE_U_4(pptr + 2)), GET_BE_U_2(pptr + 6));
+ break;
+
+ case BGP_EXT_COM_LINKBAND:
+ bw.i = GET_BE_U_4(pptr + 4);
+ ND_PRINT("bandwidth: %.3f Mbps",
+ bw.f*8/1000000);
+ break;
+
+ case BGP_EXT_COM_VPN_ORIGIN:
+ case BGP_EXT_COM_VPN_ORIGIN2:
+ case BGP_EXT_COM_VPN_ORIGIN3:
+ case BGP_EXT_COM_VPN_ORIGIN4:
+ case BGP_EXT_COM_OSPF_RID:
+ case BGP_EXT_COM_OSPF_RID2:
+ ND_PRINT("%s", GET_IPADDR_STRING(pptr+2));
+ break;
+
+ case BGP_EXT_COM_OSPF_RTYPE:
+ case BGP_EXT_COM_OSPF_RTYPE2:
+ ND_PRINT("area:%s, router-type:%s, metric-type:%s%s",
+ GET_IPADDR_STRING(pptr+2),
+ tok2str(bgp_extd_comm_ospf_rtype_values,
+ "unknown (0x%02x)",
+ GET_U_1((pptr + 6))),
+ (GET_U_1(pptr + 7) & BGP_OSPF_RTYPE_METRIC_TYPE) ? "E2" : "",
+ ((GET_U_1(pptr + 6) == BGP_OSPF_RTYPE_EXT) || (GET_U_1(pptr + 6) == BGP_OSPF_RTYPE_NSSA)) ? "E1" : "");
+ break;
+
+ case BGP_EXT_COM_L2INFO:
+ ND_PRINT("%s Control Flags [0x%02x]:MTU %u",
+ tok2str(l2vpn_encaps_values,
+ "unknown encaps",
+ GET_U_1((pptr + 2))),
+ GET_U_1((pptr + 3)),
+ GET_BE_U_2(pptr + 4));
+ break;
+
+ case BGP_EXT_COM_SOURCE_AS:
+ ND_PRINT("AS %u", GET_BE_U_2(pptr + 2));
+ break;
+
+ case BGP_EXT_COM_ENCAP:
+ ND_PRINT("Tunnel type: %s", tok2str(bgp_extd_comm_encap_tunnel_values,
+ "unknown encaps",
+ GET_BE_U_2(pptr + 6)));
+ break;
+
+ default:
+ ND_PRINT("%02x%02x%02x%02x%02x%02x",
+ GET_U_1(pptr + 2),
+ GET_U_1(pptr + 3),
+ GET_U_1(pptr + 4),
+ GET_U_1(pptr + 5),
+ GET_U_1(pptr + 6),
+ GET_U_1(pptr + 7));
+ break;
+ }
+}
+
+/*
+ * RFC4684 (Section 4)/RFC2858 (Section 4).
+ * RTC membership prefix is structured as follows
+ * [prefix-len] [origin-as] [route-target]
+ * The route-target is encoded as RT ext-comms.
+ * Prefix-len may be 0, 32..96
+ *
+ * Note that pptr is not packet data - it is
+ * a buffer owned by our caller - therefore GET_*
+ * macros can not be used.
+ */
+static char *
+bgp_rt_prefix_print(netdissect_options *ndo,
+ const u_char *pptr,
+ u_int plen)
+{
+ /* allocate space for the largest possible string */
+ 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;
+
+ if (plen == 0) {
+ snprintf(output, sizeof(output), "route-target: 0:0/0");
+ return (output);
+ }
+
+ /* hex representation of the prefix */
+ octet_count = (plen+7)/8;
+ for (i=0; i<octet_count; i++) {
+ rtc_prefix_in_hex_len += snprintf(rtc_prefix_in_hex+rtc_prefix_in_hex_len,
+ sizeof(rtc_prefix_in_hex)-rtc_prefix_in_hex_len,
+ "%02x%s", *(pptr+i),
+ ((i%2 == 1) && (i<octet_count-1)) ? " " : "");
+ }
+
+ if (plen < 16) {
+ /*
+ * The prefix is too short to include the full ext-comm type,
+ * so we have no way to parse it further.
+ */
+ snprintf(output, sizeof(output), "route-target: partial-type: (%s/%d)",
+ rtc_prefix_in_hex, plen);
+ return (output);
+ }
+
+ /*
+ * get the ext-comm type
+ * Note: pptr references a static 8 octet buffer with unused bits set to 0,
+ * hense EXTRACT_*() macros are safe.
+ */
+ ec_type = EXTRACT_BE_U_2(pptr);
+ switch (ec_type) {
+ case BGP_EXT_COM_RT_0:
+ /* 2-byte-AS:number fmt */
+ snprintf(output, sizeof(output), "route-target: %u:%u/%d (%s)",
+ EXTRACT_BE_U_2(pptr+2),
+ EXTRACT_BE_U_4(pptr+4),
+ plen, rtc_prefix_in_hex);
+ break;
+
+ case BGP_EXT_COM_RT_1:
+ /* IP-address:AS fmt */
+ snprintf(output, sizeof(output), "route-target: %u.%u.%u.%u:%u/%d (%s)",
+ *(pptr+2), *(pptr+3), *(pptr+4), *(pptr+5),
+ EXTRACT_BE_U_2(pptr+6), plen, rtc_prefix_in_hex);
+ break;
+
+ case BGP_EXT_COM_RT_2:
+ /* 4-byte-AS:number fmt */
+ snprintf(output, sizeof(output), "route-target: %s:%u/%d (%s)",
+ as_printf(ndo, astostr, sizeof(astostr), EXTRACT_BE_U_4(pptr+2)),
+ EXTRACT_BE_U_2(pptr+6), plen, rtc_prefix_in_hex);
+ break;
+
+ default:
+ snprintf(output, sizeof(output), "route target: unknown-type(%04x) (%s/%d)",
+ ec_type,
+ rtc_prefix_in_hex, plen);
+ break;
+ }
+ return (output);
+}
+
+/* RFC 4684 */
+static int
+decode_rt_routing_info(netdissect_options *ndo,
+ const u_char *pptr)
+{
+ uint8_t route_target[8];
+ u_int plen;
+ /* allocate space for the largest possible string */
+ char astostr[AS_STR_SIZE];
+ u_int num_octets;
+
+ /* NLRI "prefix length" from RFC 2858 Section 4. */
+ plen = GET_U_1(pptr); /* get prefix length */
+
+ /* NLRI "prefix" (ibid), valid lengths are { 0, 32, 33, ..., 96 } bits.
+ * RFC 4684 Section 4 defines the layout of "origin AS" and "route
+ * target" fields inside the "prefix" depending on its length.
+ */
+ if (0 == plen) {
+ /* Without "origin AS", without "route target". */
+ ND_PRINT("\n\t default route target");
+ return 1;
+ }
+
+ if (32 > plen) {
+ ND_PRINT("\n\t (illegal prefix length)");
+ return -1;
+ }
+
+ /* With at least "origin AS", possibly with "route target". */
+ as_printf(ndo, astostr, sizeof(astostr), GET_BE_U_4(pptr + 1));
+
+ plen -= 32; /* adjust prefix length */
+
+ if (64 < plen) {
+ ND_PRINT("\n\t (illegal prefix length)");
+ return -1;
+ }
+
+ /* From now on (plen + 7) / 8 evaluates to { 0, 1, 2, ..., 8 }
+ * and gives the number of octets in the variable-length "route
+ * target" field inside this NLRI "prefix". Look for it.
+ */
+ memset(&route_target, 0, sizeof(route_target));
+ num_octets = (plen + 7) / 8;
+ 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",
+ astostr,
+ bgp_rt_prefix_print(ndo, (u_char *)&route_target, plen));
+
+ return 5 + num_octets;
+}
+
+static int
+decode_labeled_vpn_prefix4(netdissect_options *ndo,
+ const u_char *pptr, char *buf, size_t buflen)
+{
+ nd_ipv4 addr;
+ u_int plen;
+
+ plen = GET_U_1(pptr); /* get prefix length */
+
+ if ((24+64) > plen)
+ return -1;
+
+ plen -= (24+64); /* adjust prefixlen - labellength - RD len*/
+
+ if (32 < plen)
+ return -1;
+
+ memset(&addr, 0, sizeof(addr));
+ 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",
+ bgp_vpn_rd_print(ndo, pptr+4),
+ ipaddr_string(ndo, (const u_char *)&addr),
+ plen,
+ GET_BE_U_3(pptr + 1)>>4,
+ ((GET_U_1(pptr + 3) & 1) == 0) ? "(BOGUS: Bottom of Stack NOT set!)" : "(bottom)" );
+
+ return 12 + (plen + 7) / 8;
+}
+
+/*
+ * +-------------------------------+
+ * | |
+ * | RD:IPv4-address (12 octets) |
+ * | |
+ * +-------------------------------+
+ * | MDT Group-address (4 octets) |
+ * +-------------------------------+
+ */
+
+#define MDT_VPN_NLRI_LEN 16
+
+static int
+decode_mdt_vpn_nlri(netdissect_options *ndo,
+ const u_char *pptr, char *buf, size_t buflen)
+{
+ 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)
+ return -1;
+ pptr++;
+
+ /* RD */
+ ND_TCHECK_8(pptr);
+ rd = pptr;
+ pptr += 8;
+
+ /* IPv4 address */
+ vpn_ip = pptr;
+ 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));
+
+ return MDT_VPN_NLRI_LEN + 1;
+
+ trunc:
+ return -2;
+}
+
+#define BGP_MULTICAST_VPN_ROUTE_TYPE_INTRA_AS_I_PMSI 1
+#define BGP_MULTICAST_VPN_ROUTE_TYPE_INTER_AS_I_PMSI 2
+#define BGP_MULTICAST_VPN_ROUTE_TYPE_S_PMSI 3
+#define BGP_MULTICAST_VPN_ROUTE_TYPE_INTRA_AS_SEG_LEAF 4
+#define BGP_MULTICAST_VPN_ROUTE_TYPE_SOURCE_ACTIVE 5
+#define BGP_MULTICAST_VPN_ROUTE_TYPE_SHARED_TREE_JOIN 6
+#define BGP_MULTICAST_VPN_ROUTE_TYPE_SOURCE_TREE_JOIN 7
+
+static const struct tok bgp_multicast_vpn_route_type_values[] = {
+ { BGP_MULTICAST_VPN_ROUTE_TYPE_INTRA_AS_I_PMSI, "Intra-AS I-PMSI"},
+ { BGP_MULTICAST_VPN_ROUTE_TYPE_INTER_AS_I_PMSI, "Inter-AS I-PMSI"},
+ { BGP_MULTICAST_VPN_ROUTE_TYPE_S_PMSI, "S-PMSI"},
+ { BGP_MULTICAST_VPN_ROUTE_TYPE_INTRA_AS_SEG_LEAF, "Intra-AS Segment-Leaf"},
+ { BGP_MULTICAST_VPN_ROUTE_TYPE_SOURCE_ACTIVE, "Source-Active"},
+ { BGP_MULTICAST_VPN_ROUTE_TYPE_SHARED_TREE_JOIN, "Shared Tree Join"},
+ { BGP_MULTICAST_VPN_ROUTE_TYPE_SOURCE_TREE_JOIN, "Source Tree Join"},
+ { 0, NULL}
+};
+
+static int
+decode_multicast_vpn(netdissect_options *ndo,
+ const u_char *pptr, char *buf, size_t buflen)
+{
+ /* 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",
+ tok2str(bgp_multicast_vpn_route_type_values,
+ "Unknown", route_type),
+ route_type, route_length);
+
+ 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",
+ 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)));
+ 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));
+ pptr += BGP_VPN_RD_LEN;
+
+ sg_length = bgp_vpn_sg_print(ndo, pptr, buf, buflen);
+ 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));
+ 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));
+ pptr += BGP_VPN_RD_LEN;
+
+ bgp_vpn_sg_print(ndo, pptr, buf, buflen);
+ 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)));
+ pptr += BGP_VPN_RD_LEN + 4;
+
+ bgp_vpn_sg_print(ndo, pptr, buf, buflen);
+ break;
+
+ /*
+ * no per route-type printing yet.
+ */
+ case BGP_MULTICAST_VPN_ROUTE_TYPE_INTRA_AS_SEG_LEAF:
+ default:
+ break;
+ }
+
+ return route_length + 2;
+
+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)
+{
+ u_int plen, tlen, tlv_type, tlv_len, ttlv_len;
+ int stringlen;
+
+ plen = GET_BE_U_2(pptr);
+ tlen = plen;
+ pptr += 2;
+ /* Old and new L2VPN NLRI share AFI/SAFI
+ * -> Assume a 12 Byte-length NLRI is auto-discovery-only
+ * and > 17 as old format. Complain for the middle case
+ */
+ 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",
+ 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",
+ 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);
+ }
+ return plen + 2;
+ }
+ tlv_type = GET_U_1(pptr);
+ pptr++;
+ tlv_len = GET_BE_U_2(pptr); /* length, in *bits* */
+ ttlv_len = (tlv_len + 7)/8; /* length, in *bytes* */
+ pptr += 2;
+
+ 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);
+ }
+ while (ttlv_len != 0) {
+ if (tlen < 1) {
+ if (buflen != 0) {
+ stringlen=snprintf(buf,buflen, " (ran past the end)");
+ UPDATE_BUF_BUFLEN(buf, buflen, stringlen);
+ }
+ 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);
+ }
+ 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);
+ }
+ if (tlen < ttlv_len) {
+ if (buflen != 0) {
+ stringlen=snprintf(buf,buflen, " (ran past the end)");
+ UPDATE_BUF_BUFLEN(buf, buflen, stringlen);
+ }
+ return plen + 2;
+ }
+ tlen -= ttlv_len;
+ break;
+ }
+ }
+ return plen + 2;
+ } else {
+ /* complain bitterly ? */
+ /* fall through */
+ goto trunc;
+ }
+
+trunc:
+ return -2;
+}
+
+int
+decode_prefix6(netdissect_options *ndo,
+ const u_char *pd, u_int itemlen, char *buf, size_t buflen)
+{
+ nd_ipv6 addr;
+ u_int plen, plenbytes;
+
+ ITEMCHECK(1);
+ plen = GET_U_1(pd);
+ if (128 < plen)
+ return -1;
+ itemlen -= 1;
+
+ memset(&addr, 0, sizeof(addr));
+ plenbytes = (plen + 7) / 8;
+ ITEMCHECK(plenbytes);
+ GET_CPY_BYTES(&addr, pd + 1, plenbytes);
+ if (plen % 8) {
+ addr[plenbytes - 1] &=
+ ((0xff00 >> (plen % 8)) & 0xff);
+ }
+ snprintf(buf, buflen, "%s/%u", ip6addr_string(ndo, (const u_char *)&addr), plen);
+ return 1 + plenbytes;
+
+badtlv:
+ return -2;
+}
+
+static int
+decode_labeled_prefix6(netdissect_options *ndo,
+ const u_char *pptr, u_int itemlen, char *buf, size_t buflen)
+{
+ nd_ipv6 addr;
+ u_int plen, plenbytes;
+
+ /* prefix length and label = 4 bytes */
+ ND_TCHECK_4(pptr);
+ ITEMCHECK(4);
+ plen = GET_U_1(pptr); /* get prefix length */
+
+ if (24 > plen)
+ return -1;
+
+ plen -= 24; /* adjust prefixlen - labellength */
+
+ if (128 < plen)
+ return -1;
+ itemlen -= 4;
+
+ memset(&addr, 0, sizeof(addr));
+ plenbytes = (plen + 7) / 8;
+ 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",
+ ip6addr_string(ndo, (const u_char *)&addr),
+ plen,
+ GET_BE_U_3(pptr + 1)>>4,
+ ((GET_U_1(pptr + 3) & 1) == 0) ? "(BOGUS: Bottom of Stack NOT set!)" : "(bottom)" );
+
+ return 4 + plenbytes;
+
+trunc:
+ return -2;
+
+badtlv:
+ return -3;
+}
+
+static int
+decode_labeled_vpn_prefix6(netdissect_options *ndo,
+ const u_char *pptr, char *buf, size_t buflen)
+{
+ nd_ipv6 addr;
+ u_int plen;
+
+ plen = GET_U_1(pptr); /* get prefix length */
+
+ if ((24+64) > plen)
+ return -1;
+
+ plen -= (24+64); /* adjust prefixlen - labellength - RD len*/
+
+ if (128 < plen)
+ return -1;
+
+ memset(&addr, 0, sizeof(addr));
+ 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",
+ bgp_vpn_rd_print(ndo, pptr+4),
+ ip6addr_string(ndo, (const u_char *)&addr),
+ plen,
+ GET_BE_U_3(pptr + 1)>>4,
+ ((GET_U_1(pptr + 3) & 1) == 0) ? "(BOGUS: Bottom of Stack NOT set!)" : "(bottom)" );
+
+ return 12 + (plen + 7) / 8;
+}
+
+static int
+decode_clnp_prefix(netdissect_options *ndo,
+ const u_char *pptr, char *buf, size_t buflen)
+{
+ uint8_t addr[19];
+ u_int plen;
+
+ plen = GET_U_1(pptr); /* get prefix length */
+
+ if (152 < plen)
+ return -1;
+
+ memset(&addr, 0, sizeof(addr));
+ 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",
+ isonsap_string(ndo, addr,(plen + 7) / 8),
+ plen);
+
+ return 1 + (plen + 7) / 8;
+}
+
+static int
+decode_labeled_vpn_clnp_prefix(netdissect_options *ndo,
+ const u_char *pptr, char *buf, size_t buflen)
+{
+ uint8_t addr[19];
+ u_int plen;
+
+ plen = GET_U_1(pptr); /* get prefix length */
+
+ if ((24+64) > plen)
+ return -1;
+
+ plen -= (24+64); /* adjust prefixlen - labellength - RD len*/
+
+ if (152 < plen)
+ return -1;
+
+ memset(&addr, 0, sizeof(addr));
+ 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",
+ bgp_vpn_rd_print(ndo, pptr+4),
+ isonsap_string(ndo, addr,(plen + 7) / 8),
+ plen,
+ GET_BE_U_3(pptr + 1)>>4,
+ ((GET_U_1(pptr + 3) & 1) == 0) ? "(BOGUS: Bottom of Stack NOT set!)" : "(bottom)" );
+
+ return 12 + (plen + 7) / 8;
+}
+
+/*
+ * bgp_attr_get_as_size
+ *
+ * Try to find the size of the ASs encoded in an as-path. It is not obvious, as
+ * both Old speakers that do not support 4 byte AS, and the new speakers that do
+ * support, exchange AS-Path with the same path-attribute type value 0x02.
+ */
+static u_int
+bgp_attr_get_as_size(netdissect_options *ndo,
+ uint8_t bgpa_type, const u_char *pptr, u_int len)
+{
+ const u_char *tptr = pptr;
+
+ /*
+ * If the path attribute is the optional AS4 path type, then we already
+ * know, that ASs must be encoded in 4 byte format.
+ */
+ if (bgpa_type == BGPTYPE_AS4_PATH) {
+ return 4;
+ }
+
+ /*
+ * Let us assume that ASs are of 2 bytes in size, and check if the AS-Path
+ * TLV is good. If not, ask the caller to try with AS encoded as 4 bytes
+ * each.
+ */
+ while (tptr < pptr + len) {
+ /*
+ * If we do not find a valid segment type, our guess might be wrong.
+ */
+ if (GET_U_1(tptr) < BGP_AS_SEG_TYPE_MIN || GET_U_1(tptr) > BGP_AS_SEG_TYPE_MAX) {
+ goto trunc;
+ }
+ tptr += 2 + GET_U_1(tptr + 1) * 2;
+ }
+
+ /*
+ * If we correctly reached end of the AS path attribute data content,
+ * then most likely ASs were indeed encoded as 2 bytes.
+ */
+ if (tptr == pptr + len) {
+ return 2;
+ }
+
+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
+ * there was not enough data, it will crib and end the parse anyways.
+ */
+ return 4;
+}
+
+/*
+ * The only way to know that a BGP UPDATE message is using add path is
+ * by checking if the capability is in the OPEN message which we may have missed.
+ * So this function checks if it is possible that the update could contain add path
+ * and if so it checks that standard BGP doesn't make sense.
+ */
+static int
+check_add_path(netdissect_options *ndo, const u_char *pptr, u_int length,
+ u_int max_prefix_length)
+{
+ u_int offset, prefix_length;
+
+ if (length < 5) {
+ return 0;
+ }
+
+ /*
+ * Scan through the NLRI information under the assumpetion that
+ * it doesn't have path IDs.
+ */
+ for (offset = 0; offset < length;) {
+ offset += 4;
+ if (!ND_TTEST_1(pptr + offset)) {
+ /* We ran out of captured data; quit scanning. */
+ break;
+ }
+ prefix_length = GET_U_1(pptr + offset);
+ /*
+ * Add 4 to cover the path id
+ * and check the prefix length isn't greater than 32/128.
+ */
+ if (prefix_length > max_prefix_length) {
+ return 0;
+ }
+ /* Add 1 for the prefix_length byte and prefix_length to cover the address */
+ offset += 1 + ((prefix_length + 7) / 8);
+ }
+ /* check we haven't gone past the end of the section */
+ if (offset > length) {
+ return 0;
+ }
+
+ /* check it's not standard BGP */
+ for (offset = 0; offset < length; ) {
+ if (!ND_TTEST_1(pptr + offset)) {
+ /* We ran out of captured data; quit scanning. */
+ break;
+ }
+ prefix_length = GET_U_1(pptr + offset);
+ /*
+ * If the prefix_length is zero (0.0.0.0/0)
+ * and since it's not the only address (length >= 5)
+ * then it is add-path
+ */
+ if (prefix_length < 1 || prefix_length > max_prefix_length) {
+ return 1;
+ }
+ offset += 1 + ((prefix_length + 7) / 8);
+ }
+ if (offset > length) {
+ return 1;
+ }
+
+ /* assume not add-path by default */
+ return 0;
+}
+
+static int
+bgp_mp_af_print(netdissect_options *ndo,
+ const u_char *tptr, u_int tlen,
+ uint16_t *afp, uint8_t *safip)