print-udld.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. /*
  2. * Copyright (c) 1998-2007 The TCPDUMP project
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that: (1) source code
  6. * distributions retain the above copyright notice and this paragraph
  7. * in its entirety, and (2) distributions including binary code include
  8. * the above copyright notice and this paragraph in its entirety in
  9. * the documentation or other materials provided with the distribution.
  10. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND
  11. * WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, WITHOUT
  12. * LIMITATION, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  13. * FOR A PARTICULAR PURPOSE.
  14. *
  15. * Original code by Carles Kishimoto <carles.kishimoto@gmail.com>
  16. */
  17. /* \summary: Cisco UniDirectional Link Detection (UDLD) protocol printer */
  18. /* specification: RFC 5171 */
  19. #ifdef HAVE_CONFIG_H
  20. #include "config.h"
  21. #endif
  22. #include <netdissect-stdinc.h>
  23. #include "netdissect.h"
  24. #include "extract.h"
  25. static const char tstr[] = " [|udld]";
  26. #define UDLD_HEADER_LEN 4
  27. #define UDLD_DEVICE_ID_TLV 0x0001
  28. #define UDLD_PORT_ID_TLV 0x0002
  29. #define UDLD_ECHO_TLV 0x0003
  30. #define UDLD_MESSAGE_INTERVAL_TLV 0x0004
  31. #define UDLD_TIMEOUT_INTERVAL_TLV 0x0005
  32. #define UDLD_DEVICE_NAME_TLV 0x0006
  33. #define UDLD_SEQ_NUMBER_TLV 0x0007
  34. static const struct tok udld_tlv_values[] = {
  35. { UDLD_DEVICE_ID_TLV, "Device-ID TLV"},
  36. { UDLD_PORT_ID_TLV, "Port-ID TLV"},
  37. { UDLD_ECHO_TLV, "Echo TLV"},
  38. { UDLD_MESSAGE_INTERVAL_TLV, "Message Interval TLV"},
  39. { UDLD_TIMEOUT_INTERVAL_TLV, "Timeout Interval TLV"},
  40. { UDLD_DEVICE_NAME_TLV, "Device Name TLV"},
  41. { UDLD_SEQ_NUMBER_TLV,"Sequence Number TLV"},
  42. { 0, NULL}
  43. };
  44. static const struct tok udld_code_values[] = {
  45. { 0x00, "Reserved"},
  46. { 0x01, "Probe message"},
  47. { 0x02, "Echo message"},
  48. { 0x03, "Flush message"},
  49. { 0, NULL}
  50. };
  51. static const struct tok udld_flags_values[] = {
  52. { 0x00, "RT"},
  53. { 0x01, "RSY"},
  54. { 0, NULL}
  55. };
  56. /*
  57. * UDLD's Protocol Data Unit format:
  58. *
  59. * 0 1 2 3
  60. * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
  61. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  62. * | Ver | Opcode | Flags | Checksum |
  63. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  64. * | List of TLVs (variable length list) |
  65. * | ... |
  66. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  67. *
  68. * TLV format:
  69. *
  70. * 0 1 2 3
  71. * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
  72. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  73. * | TYPE | LENGTH |
  74. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  75. * | VALUE |
  76. * | ... |
  77. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  78. *
  79. * LENGTH: Length in bytes of the Type, Length, and Value fields.
  80. */
  81. #define UDLD_EXTRACT_VERSION(x) (((x)&0xe0)>>5)
  82. #define UDLD_EXTRACT_OPCODE(x) ((x)&0x1f)
  83. void
  84. udld_print (netdissect_options *ndo, const u_char *pptr, u_int length)
  85. {
  86. int code, type, len;
  87. const u_char *tptr;
  88. if (length < UDLD_HEADER_LEN)
  89. goto trunc;
  90. tptr = pptr;
  91. ND_TCHECK2(*tptr, UDLD_HEADER_LEN);
  92. code = UDLD_EXTRACT_OPCODE(*tptr);
  93. ND_PRINT((ndo, "UDLDv%u, Code %s (%x), Flags [%s] (0x%02x), length %u",
  94. UDLD_EXTRACT_VERSION(*tptr),
  95. tok2str(udld_code_values, "Reserved", code),
  96. code,
  97. bittok2str(udld_flags_values, "none", *(tptr+1)),
  98. *(tptr+1),
  99. length));
  100. /*
  101. * In non-verbose mode, just print version and opcode type
  102. */
  103. if (ndo->ndo_vflag < 1) {
  104. return;
  105. }
  106. ND_PRINT((ndo, "\n\tChecksum 0x%04x (unverified)", EXTRACT_16BITS(tptr+2)));
  107. tptr += UDLD_HEADER_LEN;
  108. while (tptr < (pptr+length)) {
  109. ND_TCHECK2(*tptr, 4);
  110. type = EXTRACT_16BITS(tptr);
  111. len = EXTRACT_16BITS(tptr+2);
  112. ND_PRINT((ndo, "\n\t%s (0x%04x) TLV, length %u",
  113. tok2str(udld_tlv_values, "Unknown", type),
  114. type, len));
  115. if (type == 0)
  116. goto invalid;
  117. /* infinite loop check */
  118. if (len <= 4)
  119. goto invalid;
  120. len -= 4;
  121. tptr += 4;
  122. ND_TCHECK2(*tptr, len);
  123. switch (type) {
  124. case UDLD_DEVICE_ID_TLV:
  125. case UDLD_PORT_ID_TLV:
  126. case UDLD_DEVICE_NAME_TLV:
  127. ND_PRINT((ndo, ", "));
  128. fn_printzp(ndo, tptr, len, NULL);
  129. break;
  130. case UDLD_ECHO_TLV:
  131. ND_PRINT((ndo, ", "));
  132. (void)fn_printn(ndo, tptr, len, NULL);
  133. break;
  134. case UDLD_MESSAGE_INTERVAL_TLV:
  135. case UDLD_TIMEOUT_INTERVAL_TLV:
  136. if (len != 1)
  137. goto invalid;
  138. ND_PRINT((ndo, ", %us", (*tptr)));
  139. break;
  140. case UDLD_SEQ_NUMBER_TLV:
  141. if (len != 4)
  142. goto invalid;
  143. ND_PRINT((ndo, ", %u", EXTRACT_32BITS(tptr)));
  144. break;
  145. default:
  146. break;
  147. }
  148. tptr += len;
  149. }
  150. return;
  151. invalid:
  152. ND_PRINT((ndo, "%s", istr));
  153. return;
  154. trunc:
  155. ND_PRINT((ndo, "%s", tstr));
  156. }
  157. /*
  158. * Local Variables:
  159. * c-style: whitesmith
  160. * c-basic-offset: 4
  161. * End:
  162. */