print-vqp.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. /*
  2. * Copyright (c) 1998-2006 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@bsc.es>
  16. */
  17. /* \summary: Cisco VLAN Query Protocol (VQP) printer */
  18. #ifdef HAVE_CONFIG_H
  19. #include "config.h"
  20. #endif
  21. #include <netdissect-stdinc.h>
  22. #include "netdissect.h"
  23. #include "extract.h"
  24. #include "addrtoname.h"
  25. #include "ether.h"
  26. #define VQP_VERSION 1
  27. #define VQP_EXTRACT_VERSION(x) ((x)&0xFF)
  28. /*
  29. * VQP common header
  30. *
  31. * 0 1 2 3
  32. * 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
  33. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  34. * | Constant | Packet type | Error Code | nitems |
  35. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  36. * | Packet Sequence Number (4 bytes) |
  37. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  38. */
  39. struct vqp_common_header_t {
  40. uint8_t version;
  41. uint8_t msg_type;
  42. uint8_t error_code;
  43. uint8_t nitems;
  44. uint8_t sequence[4];
  45. };
  46. struct vqp_obj_tlv_t {
  47. uint8_t obj_type[4];
  48. uint8_t obj_length[2];
  49. };
  50. #define VQP_OBJ_REQ_JOIN_PORT 0x01
  51. #define VQP_OBJ_RESP_VLAN 0x02
  52. #define VQP_OBJ_REQ_RECONFIRM 0x03
  53. #define VQP_OBJ_RESP_RECONFIRM 0x04
  54. static const struct tok vqp_msg_type_values[] = {
  55. { VQP_OBJ_REQ_JOIN_PORT, "Request, Join Port"},
  56. { VQP_OBJ_RESP_VLAN, "Response, VLAN"},
  57. { VQP_OBJ_REQ_RECONFIRM, "Request, Reconfirm"},
  58. { VQP_OBJ_RESP_RECONFIRM, "Response, Reconfirm"},
  59. { 0, NULL}
  60. };
  61. static const struct tok vqp_error_code_values[] = {
  62. { 0x00, "No error"},
  63. { 0x03, "Access denied"},
  64. { 0x04, "Shutdown port"},
  65. { 0x05, "Wrong VTP domain"},
  66. { 0, NULL}
  67. };
  68. /* FIXME the heading 0x0c looks ugly - those must be flags etc. */
  69. #define VQP_OBJ_IP_ADDRESS 0x0c01
  70. #define VQP_OBJ_PORT_NAME 0x0c02
  71. #define VQP_OBJ_VLAN_NAME 0x0c03
  72. #define VQP_OBJ_VTP_DOMAIN 0x0c04
  73. #define VQP_OBJ_ETHERNET_PKT 0x0c05
  74. #define VQP_OBJ_MAC_NULL 0x0c06
  75. #define VQP_OBJ_MAC_ADDRESS 0x0c08
  76. static const struct tok vqp_obj_values[] = {
  77. { VQP_OBJ_IP_ADDRESS, "Client IP Address" },
  78. { VQP_OBJ_PORT_NAME, "Port Name" },
  79. { VQP_OBJ_VLAN_NAME, "VLAN Name" },
  80. { VQP_OBJ_VTP_DOMAIN, "VTP Domain" },
  81. { VQP_OBJ_ETHERNET_PKT, "Ethernet Packet" },
  82. { VQP_OBJ_MAC_NULL, "MAC Null" },
  83. { VQP_OBJ_MAC_ADDRESS, "MAC Address" },
  84. { 0, NULL}
  85. };
  86. void
  87. vqp_print(netdissect_options *ndo, register const u_char *pptr, register u_int len)
  88. {
  89. const struct vqp_common_header_t *vqp_common_header;
  90. const struct vqp_obj_tlv_t *vqp_obj_tlv;
  91. const u_char *tptr;
  92. uint16_t vqp_obj_len;
  93. uint32_t vqp_obj_type;
  94. u_int tlen;
  95. uint8_t nitems;
  96. tptr=pptr;
  97. tlen = len;
  98. vqp_common_header = (const struct vqp_common_header_t *)pptr;
  99. ND_TCHECK(*vqp_common_header);
  100. if (sizeof(struct vqp_common_header_t) > tlen)
  101. goto trunc;
  102. /*
  103. * Sanity checking of the header.
  104. */
  105. if (VQP_EXTRACT_VERSION(vqp_common_header->version) != VQP_VERSION) {
  106. ND_PRINT((ndo, "VQP version %u packet not supported",
  107. VQP_EXTRACT_VERSION(vqp_common_header->version)));
  108. return;
  109. }
  110. /* in non-verbose mode just lets print the basic Message Type */
  111. if (ndo->ndo_vflag < 1) {
  112. ND_PRINT((ndo, "VQPv%u %s Message, error-code %s (%u), length %u",
  113. VQP_EXTRACT_VERSION(vqp_common_header->version),
  114. tok2str(vqp_msg_type_values, "unknown (%u)",vqp_common_header->msg_type),
  115. tok2str(vqp_error_code_values, "unknown (%u)",vqp_common_header->error_code),
  116. vqp_common_header->error_code,
  117. len));
  118. return;
  119. }
  120. /* ok they seem to want to know everything - lets fully decode it */
  121. nitems = vqp_common_header->nitems;
  122. ND_PRINT((ndo, "\n\tVQPv%u, %s Message, error-code %s (%u), seq 0x%08x, items %u, length %u",
  123. VQP_EXTRACT_VERSION(vqp_common_header->version),
  124. tok2str(vqp_msg_type_values, "unknown (%u)",vqp_common_header->msg_type),
  125. tok2str(vqp_error_code_values, "unknown (%u)",vqp_common_header->error_code),
  126. vqp_common_header->error_code,
  127. EXTRACT_32BITS(&vqp_common_header->sequence),
  128. nitems,
  129. len));
  130. /* skip VQP Common header */
  131. tptr+=sizeof(const struct vqp_common_header_t);
  132. tlen-=sizeof(const struct vqp_common_header_t);
  133. while (nitems > 0 && tlen > 0) {
  134. vqp_obj_tlv = (const struct vqp_obj_tlv_t *)tptr;
  135. ND_TCHECK(*vqp_obj_tlv);
  136. if (sizeof(struct vqp_obj_tlv_t) > tlen)
  137. goto trunc;
  138. vqp_obj_type = EXTRACT_32BITS(vqp_obj_tlv->obj_type);
  139. vqp_obj_len = EXTRACT_16BITS(vqp_obj_tlv->obj_length);
  140. tptr+=sizeof(struct vqp_obj_tlv_t);
  141. tlen-=sizeof(struct vqp_obj_tlv_t);
  142. ND_PRINT((ndo, "\n\t %s Object (0x%08x), length %u, value: ",
  143. tok2str(vqp_obj_values, "Unknown", vqp_obj_type),
  144. vqp_obj_type, vqp_obj_len));
  145. /* basic sanity check */
  146. if (vqp_obj_type == 0 || vqp_obj_len ==0) {
  147. return;
  148. }
  149. /* did we capture enough for fully decoding the object ? */
  150. ND_TCHECK2(*tptr, vqp_obj_len);
  151. if (vqp_obj_len > tlen)
  152. goto trunc;
  153. switch(vqp_obj_type) {
  154. case VQP_OBJ_IP_ADDRESS:
  155. if (vqp_obj_len != 4)
  156. goto trunc;
  157. ND_PRINT((ndo, "%s (0x%08x)", ipaddr_string(ndo, tptr), EXTRACT_32BITS(tptr)));
  158. break;
  159. /* those objects have similar semantics - fall through */
  160. case VQP_OBJ_PORT_NAME:
  161. case VQP_OBJ_VLAN_NAME:
  162. case VQP_OBJ_VTP_DOMAIN:
  163. case VQP_OBJ_ETHERNET_PKT:
  164. safeputs(ndo, tptr, vqp_obj_len);
  165. break;
  166. /* those objects have similar semantics - fall through */
  167. case VQP_OBJ_MAC_ADDRESS:
  168. case VQP_OBJ_MAC_NULL:
  169. if (vqp_obj_len != ETHER_ADDR_LEN)
  170. goto trunc;
  171. ND_PRINT((ndo, "%s", etheraddr_string(ndo, tptr)));
  172. break;
  173. default:
  174. if (ndo->ndo_vflag <= 1)
  175. print_unknown_data(ndo,tptr, "\n\t ", vqp_obj_len);
  176. break;
  177. }
  178. tptr += vqp_obj_len;
  179. tlen -= vqp_obj_len;
  180. nitems--;
  181. }
  182. return;
  183. trunc:
  184. ND_PRINT((ndo, "\n\t[|VQP]"));
  185. }