+/*
+ * 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,
+ * hence 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);
+}
+