+static void
+cdp_print_duplex(netdissect_options *ndo,
+ const u_char *cp, const u_int len _U_)
+{
+ ND_PRINT("%s", GET_U_1(cp) ? "full": "half");
+}
+
+/* https://www.cisco.com/c/en/us/td/docs/voice_ip_comm/cata/186/2_12_m/english/release/notes/186rn21m.html
+* plus more details from other sources
+*
+* There are apparently versions of the request with both
+* 2 bytes and 3 bytes of value. The 3 bytes of value
+* appear to be a 1-byte application type followed by a
+* 2-byte VLAN ID; the 2 bytes of value are unknown
+* (they're 0x20 0x00 in some captures I've seen; that
+* is not a valid VLAN ID, as VLAN IDs are 12 bits).
+*
+* The replies all appear to be 3 bytes long.
+*/
+static void
+cdp_print_ata186(netdissect_options *ndo,
+ const u_char *cp, const u_int len)
+{
+ if (len == 2)
+ ND_PRINT("unknown 0x%04x", GET_BE_U_2(cp));
+ else
+ ND_PRINT("app %u, vlan %u", GET_U_1(cp), GET_BE_U_2(cp + 1));
+}
+
+static void
+cdp_print_mtu(netdissect_options *ndo,
+ const u_char *cp, const u_int len _U_)
+{
+ ND_PRINT("%u bytes", GET_BE_U_4(cp));
+}
+
+static void
+cdp_print_uint8x(netdissect_options *ndo,
+ const u_char *cp, const u_int len _U_)
+{
+ ND_PRINT("0x%02x", GET_U_1(cp));
+}
+
+static void
+cdp_print_phys_loc(netdissect_options *ndo,
+ const u_char *cp, const u_int len)
+{
+ ND_PRINT("0x%02x", GET_U_1(cp));
+ if (len > 1) {
+ ND_PRINT("/");
+ nd_printjn(ndo, cp + 1, len - 1);
+ }
+}
+
+static void
+cdp_print_power_avail(netdissect_options *ndo,
+ const u_char *cp, const u_int len)
+{
+ ND_PRINT("reqid %u, mgmtid %u", GET_BE_U_2(cp), GET_BE_U_2(cp + 2));
+ if (len > 4) {
+ const u_char *powerp;
+ u_int powerlen;
+
+ ND_PRINT(", pwravail");
+ for (powerp = cp + 4, powerlen = len - 4;
+ powerlen >= 4; powerp += 4, powerlen -= 4)
+ ND_PRINT(" %u mW", GET_BE_U_4(powerp));
+ }
+}
+
+typedef enum {
+ VERBOSE_OR_NOT_VERBOSE,
+ VERBOSE_ONLY
+} when_to_print_t;
+
+struct cdp_tlvinfo {
+ const char *name;
+ void (*printer)(netdissect_options *ndo, const u_char *, u_int);
+ when_to_print_t when_to_print;
+ int min_len, max_len;
+};
+
+static const struct cdp_tlvinfo cdptlvs[] = {
+ /* 0x00 */
+ [ 0x01 ] = { "Device-ID", cdp_print_string, VERBOSE_OR_NOT_VERBOSE, -1, -1 },
+ [ 0x02 ] = { "Address", cdp_print_addr, VERBOSE_ONLY, 4, -1 },
+ [ 0x03 ] = { "Port-ID", cdp_print_string, VERBOSE_ONLY, -1, -1 },
+ [ 0x04 ] = { "Capability", cdp_print_capability, VERBOSE_ONLY, 4, 4 },
+ [ 0x05 ] = { "Version String", cdp_print_version, VERBOSE_ONLY, -1, -1 },
+ [ 0x06 ] = { "Platform", cdp_print_string, VERBOSE_ONLY, -1, -1 },
+ [ 0x07 ] = { "Prefixes", cdp_print_prefixes, VERBOSE_ONLY, -1, -1 },
+ /* not documented */
+ [ 0x08 ] = { "Protocol-Hello option", NULL, VERBOSE_ONLY, -1, -1 },
+ /* CDPv2 */
+ [ 0x09 ] = { "VTP Management Domain", cdp_print_string, VERBOSE_ONLY, -1, -1 },
+ /* CDPv2 */
+ [ 0x0a ] = { "Native VLAN ID", cdp_print_uint16, VERBOSE_ONLY, 2, 2 },
+ /* CDPv2 */
+ [ 0x0b ] = { "Duplex", cdp_print_duplex, VERBOSE_ONLY, 1, 1 },
+ /* 0x0c */
+ /* 0x0d */
+ /* incomplete doc. */
+ [ 0x0e ] = { "ATA-186 VoIP VLAN assignment", cdp_print_ata186, VERBOSE_ONLY, 3, 3 },
+ /* incomplete doc. */
+ [ 0x0f ] = { "ATA-186 VoIP VLAN request", cdp_print_ata186, VERBOSE_ONLY, 2, 3 },
+ /* not documented */
+ [ 0x10 ] = { "power consumption", cdp_print_power, VERBOSE_ONLY, 1, 3 },
+ /* not documented */
+ [ 0x11 ] = { "MTU", cdp_print_mtu, VERBOSE_ONLY, 4, 4 },
+ /* not documented */
+ [ 0x12 ] = { "AVVID trust bitmap", cdp_print_uint8x, VERBOSE_ONLY, 1, 1 },
+ /* not documented */
+ [ 0x13 ] = { "AVVID untrusted ports CoS", cdp_print_uint8x, VERBOSE_ONLY, 1, 1 },
+ /* not documented */
+ [ 0x14 ] = { "System Name", cdp_print_string, VERBOSE_ONLY, -1, -1 },
+ /* not documented */
+ [ 0x15 ] = { "System Object ID (not decoded)", NULL, VERBOSE_ONLY, -1, -1 },
+ [ 0x16 ] = { "Management Addresses", cdp_print_addr, VERBOSE_ONLY, 4, -1 },
+ /* not documented */
+ [ 0x17 ] = { "Physical Location", cdp_print_phys_loc, VERBOSE_ONLY, 1, -1 },
+ /* not documented */
+ [ 0x1a ] = { "Power available", cdp_print_power_avail, VERBOSE_ONLY, 8, -1 },
+};
+
+#define T_MAX (sizeof cdptlvs / sizeof cdptlvs[0])
+
+void
+cdp_print(netdissect_options *ndo,
+ const u_char *tptr, u_int length)
+{
+ u_int orig_length = length;
+ uint16_t checksum;
+
+ ndo->ndo_protocol = "cdp";
+
+ if (length < CDP_HEADER_LEN) {
+ ND_PRINT(" (packet length %u < %u)", length, CDP_HEADER_LEN);
+ goto invalid;
+ }
+ ND_PRINT("CDPv%u, ttl: %us",
+ GET_U_1(tptr + CDP_HEADER_VERSION_OFFSET),
+ GET_U_1(tptr + CDP_HEADER_TTL_OFFSET));
+ checksum = GET_BE_U_2(tptr + CDP_HEADER_CHECKSUM_OFFSET);
+ if (ndo->ndo_vflag)
+ ND_PRINT(", checksum: 0x%04x (unverified), length %u",
+ checksum, orig_length);