print-cdp.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  1. /*
  2. * Copyright (c) 1992, 1993, 1994, 1995, 1996, 1997
  3. * The Regents of the University of California. All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that: (1) source code distributions
  7. * retain the above copyright notice and this paragraph in its entirety, (2)
  8. * distributions including binary code include the above copyright notice and
  9. * this paragraph in its entirety in the documentation or other materials
  10. * provided with the distribution, and (3) all advertising materials mentioning
  11. * features or use of this software display the following acknowledgement:
  12. * ``This product includes software developed by the University of California,
  13. * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
  14. * the University nor the names of its contributors may be used to endorse
  15. * or promote products derived from this software without specific prior
  16. * written permission.
  17. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
  18. * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
  19. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  20. *
  21. * Code by Gert Doering, SpaceNet GmbH, gert@space.net
  22. *
  23. * Reference documentation:
  24. * http://www.cisco.com/univercd/cc/td/doc/product/lan/trsrb/frames.htm
  25. */
  26. /* \summary: Cisco Discovery Protocol (CDP) printer */
  27. #ifdef HAVE_CONFIG_H
  28. #include "config.h"
  29. #endif
  30. #include <netdissect-stdinc.h>
  31. #include <string.h>
  32. #include "netdissect.h"
  33. #include "addrtoname.h"
  34. #include "extract.h"
  35. #include "nlpid.h"
  36. static const char tstr[] = "[|cdp]";
  37. #define CDP_HEADER_LEN 4
  38. #define CDP_HEADER_VERSION_OFFSET 0
  39. #define CDP_HEADER_TTL_OFFSET 1
  40. #define CDP_HEADER_CHECKSUM_OFFSET 2
  41. #define CDP_TLV_HEADER_LEN 4
  42. #define CDP_TLV_TYPE_OFFSET 0
  43. #define CDP_TLV_LEN_OFFSET 2
  44. static const struct tok cdp_tlv_values[] = {
  45. { 0x01, "Device-ID"},
  46. { 0x02, "Address"},
  47. { 0x03, "Port-ID"},
  48. { 0x04, "Capability"},
  49. { 0x05, "Version String"},
  50. { 0x06, "Platform"},
  51. { 0x07, "Prefixes"},
  52. { 0x08, "Protocol-Hello option"},
  53. { 0x09, "VTP Management Domain"},
  54. { 0x0a, "Native VLAN ID"},
  55. { 0x0b, "Duplex"},
  56. { 0x0e, "ATA-186 VoIP VLAN request"},
  57. { 0x0f, "ATA-186 VoIP VLAN assignment"},
  58. { 0x10, "power consumption"},
  59. { 0x11, "MTU"},
  60. { 0x12, "AVVID trust bitmap"},
  61. { 0x13, "AVVID untrusted ports CoS"},
  62. { 0x14, "System Name"},
  63. { 0x15, "System Object ID (not decoded)"},
  64. { 0x16, "Management Addresses"},
  65. { 0x17, "Physical Location"},
  66. { 0, NULL}
  67. };
  68. static const struct tok cdp_capability_values[] = {
  69. { 0x01, "Router" },
  70. { 0x02, "Transparent Bridge" },
  71. { 0x04, "Source Route Bridge" },
  72. { 0x08, "L2 Switch" },
  73. { 0x10, "L3 capable" },
  74. { 0x20, "IGMP snooping" },
  75. { 0x40, "L1 capable" },
  76. { 0, NULL }
  77. };
  78. static int cdp_print_addr(netdissect_options *, const u_char *, int);
  79. static int cdp_print_prefixes(netdissect_options *, const u_char *, int);
  80. static unsigned long cdp_get_number(const u_char *, int);
  81. void
  82. cdp_print(netdissect_options *ndo,
  83. const u_char *pptr, u_int length, u_int caplen)
  84. {
  85. int type, len, i, j;
  86. const u_char *tptr;
  87. if (caplen < CDP_HEADER_LEN) {
  88. ND_PRINT((ndo, "%s", tstr));
  89. return;
  90. }
  91. tptr = pptr; /* temporary pointer */
  92. ND_TCHECK2(*tptr, CDP_HEADER_LEN);
  93. ND_PRINT((ndo, "CDPv%u, ttl: %us", *(tptr + CDP_HEADER_VERSION_OFFSET),
  94. *(tptr + CDP_HEADER_TTL_OFFSET)));
  95. if (ndo->ndo_vflag)
  96. ND_PRINT((ndo, ", checksum: 0x%04x (unverified), length %u", EXTRACT_16BITS(tptr+CDP_HEADER_CHECKSUM_OFFSET), length));
  97. tptr += CDP_HEADER_LEN;
  98. while (tptr < (pptr+length)) {
  99. ND_TCHECK2(*tptr, CDP_TLV_HEADER_LEN); /* read out Type and Length */
  100. type = EXTRACT_16BITS(tptr+CDP_TLV_TYPE_OFFSET);
  101. len = EXTRACT_16BITS(tptr+CDP_TLV_LEN_OFFSET); /* object length includes the 4 bytes header length */
  102. if (len < CDP_TLV_HEADER_LEN) {
  103. if (ndo->ndo_vflag)
  104. ND_PRINT((ndo, "\n\t%s (0x%02x), TLV length: %u byte%s (too short)",
  105. tok2str(cdp_tlv_values,"unknown field type", type),
  106. type,
  107. len,
  108. PLURAL_SUFFIX(len))); /* plural */
  109. else
  110. ND_PRINT((ndo, ", %s TLV length %u too short",
  111. tok2str(cdp_tlv_values,"unknown field type", type),
  112. len));
  113. break;
  114. }
  115. tptr += CDP_TLV_HEADER_LEN;
  116. len -= CDP_TLV_HEADER_LEN;
  117. ND_TCHECK2(*tptr, len);
  118. if (ndo->ndo_vflag || type == 1) { /* in non-verbose mode just print Device-ID */
  119. if (ndo->ndo_vflag)
  120. ND_PRINT((ndo, "\n\t%s (0x%02x), value length: %u byte%s: ",
  121. tok2str(cdp_tlv_values,"unknown field type", type),
  122. type,
  123. len,
  124. PLURAL_SUFFIX(len))); /* plural */
  125. switch (type) {
  126. case 0x01: /* Device-ID */
  127. if (!ndo->ndo_vflag)
  128. ND_PRINT((ndo, ", Device-ID "));
  129. ND_PRINT((ndo, "'"));
  130. (void)fn_printn(ndo, tptr, len, NULL);
  131. ND_PRINT((ndo, "'"));
  132. break;
  133. case 0x02: /* Address */
  134. if (cdp_print_addr(ndo, tptr, len) < 0)
  135. goto trunc;
  136. break;
  137. case 0x03: /* Port-ID */
  138. ND_PRINT((ndo, "'"));
  139. (void)fn_printn(ndo, tptr, len, NULL);
  140. ND_PRINT((ndo, "'"));
  141. break;
  142. case 0x04: /* Capabilities */
  143. if (len < 4)
  144. goto trunc;
  145. ND_PRINT((ndo, "(0x%08x): %s",
  146. EXTRACT_32BITS(tptr),
  147. bittok2str(cdp_capability_values, "none", EXTRACT_32BITS(tptr))));
  148. break;
  149. case 0x05: /* Version */
  150. ND_PRINT((ndo, "\n\t "));
  151. for (i=0;i<len;i++) {
  152. j = *(tptr+i);
  153. if (j == '\n') /* lets rework the version string to
  154. get a nice indentation */
  155. ND_PRINT((ndo, "\n\t "));
  156. else
  157. fn_print_char(ndo, j);
  158. }
  159. break;
  160. case 0x06: /* Platform */
  161. ND_PRINT((ndo, "'"));
  162. (void)fn_printn(ndo, tptr, len, NULL);
  163. ND_PRINT((ndo, "'"));
  164. break;
  165. case 0x07: /* Prefixes */
  166. if (cdp_print_prefixes(ndo, tptr, len) < 0)
  167. goto trunc;
  168. break;
  169. case 0x08: /* Protocol Hello Option - not documented */
  170. break;
  171. case 0x09: /* VTP Mgmt Domain - CDPv2 */
  172. ND_PRINT((ndo, "'"));
  173. (void)fn_printn(ndo, tptr, len, NULL);
  174. ND_PRINT((ndo, "'"));
  175. break;
  176. case 0x0a: /* Native VLAN ID - CDPv2 */
  177. if (len < 2)
  178. goto trunc;
  179. ND_PRINT((ndo, "%d", EXTRACT_16BITS(tptr)));
  180. break;
  181. case 0x0b: /* Duplex - CDPv2 */
  182. if (len < 1)
  183. goto trunc;
  184. ND_PRINT((ndo, "%s", *(tptr) ? "full": "half"));
  185. break;
  186. /* http://www.cisco.com/c/en/us/td/docs/voice_ip_comm/cata/186/2_12_m/english/release/notes/186rn21m.html
  187. * plus more details from other sources
  188. */
  189. case 0x0e: /* ATA-186 VoIP VLAN request - incomplete doc. */
  190. if (len < 3)
  191. goto trunc;
  192. ND_PRINT((ndo, "app %d, vlan %d", *(tptr), EXTRACT_16BITS(tptr + 1)));
  193. break;
  194. case 0x10: /* ATA-186 VoIP VLAN assignment - incomplete doc. */
  195. ND_PRINT((ndo, "%1.2fW", cdp_get_number(tptr, len) / 1000.0));
  196. break;
  197. case 0x11: /* MTU - not documented */
  198. if (len < 4)
  199. goto trunc;
  200. ND_PRINT((ndo, "%u bytes", EXTRACT_32BITS(tptr)));
  201. break;
  202. case 0x12: /* AVVID trust bitmap - not documented */
  203. if (len < 1)
  204. goto trunc;
  205. ND_PRINT((ndo, "0x%02x", *(tptr)));
  206. break;
  207. case 0x13: /* AVVID untrusted port CoS - not documented */
  208. if (len < 1)
  209. goto trunc;
  210. ND_PRINT((ndo, "0x%02x", *(tptr)));
  211. break;
  212. case 0x14: /* System Name - not documented */
  213. ND_PRINT((ndo, "'"));
  214. (void)fn_printn(ndo, tptr, len, NULL);
  215. ND_PRINT((ndo, "'"));
  216. break;
  217. case 0x16: /* System Object ID - not documented */
  218. if (cdp_print_addr(ndo, tptr, len) < 0)
  219. goto trunc;
  220. break;
  221. case 0x17: /* Physical Location - not documented */
  222. if (len < 1)
  223. goto trunc;
  224. ND_PRINT((ndo, "0x%02x", *(tptr)));
  225. if (len > 1) {
  226. ND_PRINT((ndo, "/"));
  227. (void)fn_printn(ndo, tptr + 1, len - 1, NULL);
  228. }
  229. break;
  230. default:
  231. print_unknown_data(ndo, tptr, "\n\t ", len);
  232. break;
  233. }
  234. }
  235. tptr = tptr+len;
  236. }
  237. if (ndo->ndo_vflag < 1)
  238. ND_PRINT((ndo, ", length %u", caplen));
  239. return;
  240. trunc:
  241. ND_PRINT((ndo, "%s", tstr));
  242. }
  243. /*
  244. * Protocol type values.
  245. *
  246. * PT_NLPID means that the protocol type field contains an OSI NLPID.
  247. *
  248. * PT_IEEE_802_2 means that the protocol type field contains an IEEE 802.2
  249. * LLC header that specifies that the payload is for that protocol.
  250. */
  251. #define PT_NLPID 1 /* OSI NLPID */
  252. #define PT_IEEE_802_2 2 /* IEEE 802.2 LLC header */
  253. static int
  254. cdp_print_addr(netdissect_options *ndo,
  255. const u_char * p, int l)
  256. {
  257. int pt, pl, al, num;
  258. const u_char *endp = p + l;
  259. static const u_char prot_ipv6[] = {
  260. 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00, 0x86, 0xdd
  261. };
  262. ND_TCHECK2(*p, 4);
  263. if (p + 4 > endp)
  264. goto trunc;
  265. num = EXTRACT_32BITS(p);
  266. p += 4;
  267. while (p < endp && num >= 0) {
  268. ND_TCHECK2(*p, 2);
  269. if (p + 2 > endp)
  270. goto trunc;
  271. pt = p[0]; /* type of "protocol" field */
  272. pl = p[1]; /* length of "protocol" field */
  273. p += 2;
  274. ND_TCHECK2(p[pl], 2);
  275. if (p + pl + 2 > endp)
  276. goto trunc;
  277. al = EXTRACT_16BITS(&p[pl]); /* address length */
  278. if (pt == PT_NLPID && pl == 1 && *p == NLPID_IP && al == 4) {
  279. /*
  280. * IPv4: protocol type = NLPID, protocol length = 1
  281. * (1-byte NLPID), protocol = 0xcc (NLPID for IPv4),
  282. * address length = 4
  283. */
  284. p += 3;
  285. ND_TCHECK2(*p, 4);
  286. if (p + 4 > endp)
  287. goto trunc;
  288. ND_PRINT((ndo, "IPv4 (%u) %s", num, ipaddr_string(ndo, p)));
  289. p += 4;
  290. }
  291. else if (pt == PT_IEEE_802_2 && pl == 8 &&
  292. memcmp(p, prot_ipv6, 8) == 0 && al == 16) {
  293. /*
  294. * IPv6: protocol type = IEEE 802.2 header,
  295. * protocol length = 8 (size of LLC+SNAP header),
  296. * protocol = LLC+SNAP header with the IPv6
  297. * Ethertype, address length = 16
  298. */
  299. p += 10;
  300. ND_TCHECK2(*p, al);
  301. if (p + al > endp)
  302. goto trunc;
  303. ND_PRINT((ndo, "IPv6 (%u) %s", num, ip6addr_string(ndo, p)));
  304. p += al;
  305. }
  306. else {
  307. /*
  308. * Generic case: just print raw data
  309. */
  310. ND_TCHECK2(*p, pl);
  311. if (p + pl > endp)
  312. goto trunc;
  313. ND_PRINT((ndo, "pt=0x%02x, pl=%d, pb=", *(p - 2), pl));
  314. while (pl-- > 0)
  315. ND_PRINT((ndo, " %02x", *p++));
  316. ND_TCHECK2(*p, 2);
  317. if (p + 2 > endp)
  318. goto trunc;
  319. al = (*p << 8) + *(p + 1);
  320. ND_PRINT((ndo, ", al=%d, a=", al));
  321. p += 2;
  322. ND_TCHECK2(*p, al);
  323. if (p + al > endp)
  324. goto trunc;
  325. while (al-- > 0)
  326. ND_PRINT((ndo, " %02x", *p++));
  327. }
  328. num--;
  329. if (num)
  330. ND_PRINT((ndo, " "));
  331. }
  332. return 0;
  333. trunc:
  334. return -1;
  335. }
  336. static int
  337. cdp_print_prefixes(netdissect_options *ndo,
  338. const u_char * p, int l)
  339. {
  340. if (l % 5)
  341. goto trunc;
  342. ND_PRINT((ndo, " IPv4 Prefixes (%d):", l / 5));
  343. while (l > 0) {
  344. ND_PRINT((ndo, " %u.%u.%u.%u/%u", p[0], p[1], p[2], p[3], p[4]));
  345. l -= 5;
  346. p += 5;
  347. }
  348. return 0;
  349. trunc:
  350. return -1;
  351. }
  352. /* read in a <n>-byte number, MSB first
  353. * (of course this can handle max sizeof(long))
  354. */
  355. static unsigned long cdp_get_number(const u_char * p, int l)
  356. {
  357. unsigned long res=0;
  358. while( l>0 )
  359. {
  360. res = (res<<8) + *p;
  361. p++; l--;
  362. }
  363. return res;
  364. }