PageRenderTime 46ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/wireshark-1.8.0/epan/dissectors/packet-usb-masstorage.c

#
C | 451 lines | 303 code | 89 blank | 59 comment | 27 complexity | c023f8cdb018a58a3118f0ef74256925 MD5 | raw file
Possible License(s): GPL-2.0, BSD-3-Clause
  1. /* packet-usb-masstorage.c
  2. *
  3. * $Id: packet-usb-masstorage.c 42165 2012-04-20 15:33:23Z alagoutte $
  4. *
  5. * usb mass storage dissector
  6. * Ronnie Sahlberg 2006
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License
  10. * as published by the Free Software Foundation; either version 2
  11. * of the License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  21. */
  22. #ifdef HAVE_CONFIG_H
  23. # include "config.h"
  24. #endif
  25. #include <glib.h>
  26. #include <epan/packet.h>
  27. #include <epan/emem.h>
  28. #include <epan/conversation.h>
  29. #include "packet-usb.h"
  30. #include "packet-scsi.h"
  31. /* protocols and header fields */
  32. static int proto_usb_ms = -1;
  33. static int hf_usb_ms_dCBWSignature = -1;
  34. static int hf_usb_ms_dCBWTag = -1;
  35. static int hf_usb_ms_dCBWDataTransferLength = -1;
  36. static int hf_usb_ms_dCBWFlags = -1;
  37. static int hf_usb_ms_dCBWLUN = -1;
  38. static int hf_usb_ms_dCBWCBLength = -1;
  39. static int hf_usb_ms_dCSWSignature = -1;
  40. static int hf_usb_ms_dCSWDataResidue = -1;
  41. static int hf_usb_ms_dCSWStatus = -1;
  42. static int hf_usb_ms_request = -1;
  43. static int hf_usb_ms_value = -1;
  44. static int hf_usb_ms_index = -1;
  45. static int hf_usb_ms_length = -1;
  46. static int hf_usb_ms_maxlun = -1;
  47. static gint ett_usb_ms = -1;
  48. /* there is one such structure for each masstorage conversation */
  49. typedef struct _usb_ms_conv_info_t {
  50. emem_tree_t *itl; /* indexed by LUN */
  51. emem_tree_t *itlq; /* pinfo->fd->num */
  52. } usb_ms_conv_info_t;
  53. static const value_string status_vals[] = {
  54. {0x00, "Command Passed"},
  55. {0x01, "Command Failed"},
  56. {0x02, "Phase Error"},
  57. {0, NULL}
  58. };
  59. static void
  60. dissect_usb_ms_reset(packet_info *pinfo _U_, proto_tree *tree, tvbuff_t *tvb, int offset, gboolean is_request, usb_trans_info_t *usb_trans_info _U_, usb_conv_info_t *usb_conv_info _U_)
  61. {
  62. if(is_request){
  63. proto_tree_add_item(tree, hf_usb_ms_value, tvb, offset, 2, ENC_BIG_ENDIAN);
  64. offset += 2;
  65. proto_tree_add_item(tree, hf_usb_ms_index, tvb, offset, 2, ENC_BIG_ENDIAN);
  66. offset += 2;
  67. proto_tree_add_item(tree, hf_usb_ms_length, tvb, offset, 2, ENC_BIG_ENDIAN);
  68. /*offset += 2;*/
  69. } else {
  70. /* no data in reset response */
  71. }
  72. }
  73. static void
  74. dissect_usb_ms_get_max_lun(packet_info *pinfo _U_, proto_tree *tree, tvbuff_t *tvb, int offset, gboolean is_request, usb_trans_info_t *usb_trans_info _U_, usb_conv_info_t *usb_conv_info _U_)
  75. {
  76. if(is_request){
  77. proto_tree_add_item(tree, hf_usb_ms_value, tvb, offset, 2, ENC_BIG_ENDIAN);
  78. offset += 2;
  79. proto_tree_add_item(tree, hf_usb_ms_index, tvb, offset, 2, ENC_BIG_ENDIAN);
  80. offset += 2;
  81. proto_tree_add_item(tree, hf_usb_ms_length, tvb, offset, 2, ENC_BIG_ENDIAN);
  82. /*offset += 2;*/
  83. } else {
  84. proto_tree_add_item(tree, hf_usb_ms_maxlun, tvb, offset, 1, ENC_BIG_ENDIAN);
  85. offset++;
  86. }
  87. }
  88. typedef void (*usb_setup_dissector)(packet_info *pinfo, proto_tree *tree, tvbuff_t *tvb, int offset, gboolean is_request, usb_trans_info_t *usb_trans_info, usb_conv_info_t *usb_conv_info);
  89. typedef struct _usb_setup_dissector_table_t {
  90. guint8 request;
  91. usb_setup_dissector dissector;
  92. } usb_setup_dissector_table_t;
  93. #define USB_SETUP_RESET 0xff
  94. #define USB_SETUP_GET_MAX_LUN 0xfe
  95. static const usb_setup_dissector_table_t setup_dissectors[] = {
  96. {USB_SETUP_RESET, dissect_usb_ms_reset},
  97. {USB_SETUP_GET_MAX_LUN, dissect_usb_ms_get_max_lun},
  98. {0, NULL}
  99. };
  100. static const value_string setup_request_names_vals[] = {
  101. {USB_SETUP_RESET, "RESET"},
  102. {USB_SETUP_GET_MAX_LUN, "GET MAX LUN"},
  103. {0, NULL}
  104. };
  105. /* Dissector for mass storage control .
  106. * Returns TRUE if a class specific dissector was found
  107. * and FALSE othervise.
  108. */
  109. static gint
  110. dissect_usb_ms_control(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
  111. {
  112. gboolean is_request;
  113. usb_conv_info_t *usb_conv_info;
  114. usb_trans_info_t *usb_trans_info;
  115. int offset=0;
  116. usb_setup_dissector dissector;
  117. const usb_setup_dissector_table_t *tmp;
  118. is_request=(pinfo->srcport==NO_ENDPOINT);
  119. usb_conv_info=pinfo->usb_conv_info;
  120. usb_trans_info=usb_conv_info->usb_trans_info;
  121. /* See if we can find a class specific dissector for this request */
  122. dissector=NULL;
  123. for(tmp=setup_dissectors;tmp->dissector;tmp++){
  124. if (tmp->request == usb_trans_info->setup.request){
  125. dissector=tmp->dissector;
  126. break;
  127. }
  128. }
  129. /* No we could not find any class specific dissector for this request
  130. * return FALSE and let USB try any of the standard requests.
  131. */
  132. if(!dissector){
  133. return FALSE;
  134. }
  135. col_set_str(pinfo->cinfo, COL_PROTOCOL, "USBMS");
  136. if (check_col(pinfo->cinfo, COL_INFO)) {
  137. col_add_fstr(pinfo->cinfo, COL_INFO, "%s %s",
  138. val_to_str(usb_trans_info->setup.request, setup_request_names_vals, "Unknown type %x"),
  139. is_request?"Request":"Response");
  140. }
  141. if(is_request){
  142. proto_tree_add_item(tree, hf_usb_ms_request, tvb, offset, 1, ENC_LITTLE_ENDIAN);
  143. offset += 1;
  144. }
  145. dissector(pinfo, tree, tvb, offset, is_request, usb_trans_info, usb_conv_info);
  146. return TRUE;
  147. }
  148. /* dissector for mass storage bulk data */
  149. static void
  150. dissect_usb_ms_bulk(tvbuff_t *tvb, packet_info *pinfo, proto_tree *parent_tree)
  151. {
  152. usb_conv_info_t *usb_conv_info;
  153. usb_ms_conv_info_t *usb_ms_conv_info;
  154. proto_tree *tree=NULL;
  155. guint32 signature=0;
  156. int offset=0;
  157. gboolean is_request;
  158. itl_nexus_t *itl;
  159. itlq_nexus_t *itlq;
  160. usb_conv_info=pinfo->usb_conv_info;
  161. /* verify that we do have a usb_ms_conv_info */
  162. usb_ms_conv_info=usb_conv_info->class_data;
  163. if(!usb_ms_conv_info){
  164. usb_ms_conv_info=se_alloc(sizeof(usb_ms_conv_info_t));
  165. usb_ms_conv_info->itl=se_tree_create_non_persistent(EMEM_TREE_TYPE_RED_BLACK, "USB ITL");
  166. usb_ms_conv_info->itlq=se_tree_create_non_persistent(EMEM_TREE_TYPE_RED_BLACK, "USB ITLQ");
  167. usb_conv_info->class_data=usb_ms_conv_info;
  168. }
  169. is_request=(pinfo->srcport==NO_ENDPOINT);
  170. col_set_str(pinfo->cinfo, COL_PROTOCOL, "USBMS");
  171. col_clear(pinfo->cinfo, COL_INFO);
  172. if(parent_tree){
  173. proto_item *ti = NULL;
  174. ti = proto_tree_add_protocol_format(parent_tree, proto_usb_ms, tvb, 0, -1, "USB Mass Storage");
  175. tree = proto_item_add_subtree(ti, ett_usb_ms);
  176. }
  177. signature=tvb_get_letohl(tvb, offset);
  178. /*
  179. * SCSI CDB inside CBW
  180. */
  181. if(is_request&&(signature==0x43425355)&&(tvb_length(tvb)==31)){
  182. tvbuff_t *cdb_tvb;
  183. int cdbrlen, cdblen;
  184. guint8 lun, flags;
  185. guint32 datalen;
  186. /* dCBWSignature */
  187. proto_tree_add_item(tree, hf_usb_ms_dCBWSignature, tvb, offset, 4, ENC_LITTLE_ENDIAN);
  188. offset+=4;
  189. /* dCBWTag */
  190. proto_tree_add_item(tree, hf_usb_ms_dCBWTag, tvb, offset, 4, ENC_LITTLE_ENDIAN);
  191. offset+=4;
  192. /* dCBWDataTransferLength */
  193. proto_tree_add_item(tree, hf_usb_ms_dCBWDataTransferLength, tvb, offset, 4, ENC_LITTLE_ENDIAN);
  194. datalen=tvb_get_letohl(tvb, offset);
  195. offset+=4;
  196. /* dCBWFlags */
  197. proto_tree_add_item(tree, hf_usb_ms_dCBWFlags, tvb, offset, 1, ENC_LITTLE_ENDIAN);
  198. flags=tvb_get_guint8(tvb, offset);
  199. offset+=1;
  200. /* dCBWLUN */
  201. proto_tree_add_item(tree, hf_usb_ms_dCBWLUN, tvb, offset, 1, ENC_LITTLE_ENDIAN);
  202. lun=tvb_get_guint8(tvb, offset)&0x0f;
  203. offset+=1;
  204. /* make sure we have a ITL structure for this LUN */
  205. itl=(itl_nexus_t *)se_tree_lookup32(usb_ms_conv_info->itl, lun);
  206. if(!itl){
  207. itl=se_alloc(sizeof(itl_nexus_t));
  208. itl->cmdset=0xff;
  209. itl->conversation=NULL;
  210. se_tree_insert32(usb_ms_conv_info->itl, lun, itl);
  211. }
  212. /* make sure we have an ITLQ structure for this LUN/transaction */
  213. itlq=(itlq_nexus_t *)se_tree_lookup32(usb_ms_conv_info->itlq, pinfo->fd->num);
  214. if(!itlq){
  215. itlq=se_alloc(sizeof(itlq_nexus_t));
  216. itlq->lun=lun;
  217. itlq->scsi_opcode=0xffff;
  218. itlq->task_flags=0;
  219. if(datalen){
  220. if(flags&0x80){
  221. itlq->task_flags|=SCSI_DATA_READ;
  222. } else {
  223. itlq->task_flags|=SCSI_DATA_WRITE;
  224. }
  225. }
  226. itlq->data_length=datalen;
  227. itlq->bidir_data_length=0;
  228. itlq->fc_time=pinfo->fd->abs_ts;
  229. itlq->first_exchange_frame=pinfo->fd->num;
  230. itlq->last_exchange_frame=0;
  231. itlq->flags=0;
  232. itlq->alloc_len=0;
  233. itlq->extra_data=NULL;
  234. se_tree_insert32(usb_ms_conv_info->itlq, pinfo->fd->num, itlq);
  235. }
  236. /* dCBWCBLength */
  237. proto_tree_add_item(tree, hf_usb_ms_dCBWCBLength, tvb, offset, 1, ENC_LITTLE_ENDIAN);
  238. cdbrlen=tvb_get_guint8(tvb, offset)&0x1f;
  239. offset+=1;
  240. cdblen=cdbrlen;
  241. if(cdblen>tvb_length_remaining(tvb, offset)){
  242. cdblen=tvb_length_remaining(tvb, offset);
  243. }
  244. if(cdblen){
  245. cdb_tvb=tvb_new_subset(tvb, offset, cdblen, cdbrlen);
  246. dissect_scsi_cdb(cdb_tvb, pinfo, parent_tree, SCSI_DEV_UNKNOWN, itlq, itl);
  247. }
  248. return;
  249. }
  250. /*
  251. * SCSI RESPONSE inside CSW
  252. */
  253. if((!is_request)&&(signature==0x53425355)&&(tvb_length(tvb)==13)){
  254. guint8 status;
  255. /* dCSWSignature */
  256. proto_tree_add_item(tree, hf_usb_ms_dCSWSignature, tvb, offset, 4, ENC_LITTLE_ENDIAN);
  257. offset+=4;
  258. /* dCSWTag */
  259. proto_tree_add_item(tree, hf_usb_ms_dCBWTag, tvb, offset, 4, ENC_LITTLE_ENDIAN);
  260. offset+=4;
  261. /* dCSWDataResidue */
  262. proto_tree_add_item(tree, hf_usb_ms_dCSWDataResidue, tvb, offset, 4, ENC_LITTLE_ENDIAN);
  263. offset+=4;
  264. /* dCSWStatus */
  265. proto_tree_add_item(tree, hf_usb_ms_dCSWStatus, tvb, offset, 1, ENC_LITTLE_ENDIAN);
  266. status=tvb_get_guint8(tvb, offset);
  267. /*offset+=1;*/
  268. itlq=(itlq_nexus_t *)se_tree_lookup32_le(usb_ms_conv_info->itlq, pinfo->fd->num);
  269. if(!itlq){
  270. return;
  271. }
  272. itlq->last_exchange_frame=pinfo->fd->num;
  273. itl=(itl_nexus_t *)se_tree_lookup32(usb_ms_conv_info->itl, itlq->lun);
  274. if(!itl){
  275. return;
  276. }
  277. if(!status){
  278. dissect_scsi_rsp(tvb, pinfo, parent_tree, itlq, itl, 0);
  279. } else {
  280. /* just send "check condition" */
  281. dissect_scsi_rsp(tvb, pinfo, parent_tree, itlq, itl, 0x02);
  282. }
  283. return;
  284. }
  285. /*
  286. * Ok it was neither CDB not STATUS so just assume it is either data in/out
  287. */
  288. itlq=(itlq_nexus_t *)se_tree_lookup32_le(usb_ms_conv_info->itlq, pinfo->fd->num);
  289. if(!itlq){
  290. return;
  291. }
  292. itl=(itl_nexus_t *)se_tree_lookup32(usb_ms_conv_info->itl, itlq->lun);
  293. if(!itl){
  294. return;
  295. }
  296. dissect_scsi_payload(tvb, pinfo, parent_tree, is_request, itlq, itl, 0);
  297. }
  298. void
  299. proto_register_usb_ms(void)
  300. {
  301. static hf_register_info hf[] = {
  302. { &hf_usb_ms_dCBWSignature,
  303. { "Signature", "usbms.dCBWSignature", FT_UINT32, BASE_HEX,
  304. NULL, 0x0, NULL, HFILL }},
  305. { &hf_usb_ms_dCBWTag,
  306. { "Tag", "usbms.dCBWTag", FT_UINT32, BASE_HEX,
  307. NULL, 0x0, NULL, HFILL }},
  308. { &hf_usb_ms_dCBWDataTransferLength,
  309. { "DataTransferLength", "usbms.dCBWDataTransferLength", FT_UINT32, BASE_DEC,
  310. NULL, 0x0, NULL, HFILL }},
  311. { &hf_usb_ms_dCBWFlags,
  312. { "Flags", "usbms.dCBWFlags", FT_UINT8, BASE_HEX,
  313. NULL, 0x0, NULL, HFILL }},
  314. { &hf_usb_ms_dCBWLUN,
  315. { "LUN", "usbms.dCBWLUN", FT_UINT8, BASE_HEX,
  316. NULL, 0x0f, NULL, HFILL }},
  317. { &hf_usb_ms_dCBWCBLength,
  318. { "CDB Length", "usbms.dCBWCBLength", FT_UINT8, BASE_HEX,
  319. NULL, 0x1f, NULL, HFILL }},
  320. { &hf_usb_ms_dCSWSignature,
  321. { "Signature", "usbms.dCSWSignature", FT_UINT32, BASE_HEX,
  322. NULL, 0x0, NULL, HFILL }},
  323. { &hf_usb_ms_dCSWDataResidue,
  324. { "DataResidue", "usbms.dCSWDataResidue", FT_UINT32, BASE_DEC,
  325. NULL, 0x0, NULL, HFILL }},
  326. { &hf_usb_ms_dCSWStatus,
  327. { "Status", "usbms.dCSWStatus", FT_UINT8, BASE_HEX,
  328. VALS(status_vals), 0x0, NULL, HFILL }},
  329. { &hf_usb_ms_request,
  330. { "bRequest", "usbms.setup.bRequest", FT_UINT8, BASE_HEX, VALS(setup_request_names_vals), 0x0,
  331. NULL, HFILL }},
  332. { &hf_usb_ms_value,
  333. { "wValue", "usbms.setup.wValue", FT_UINT16, BASE_HEX, NULL, 0x0,
  334. NULL, HFILL }},
  335. { &hf_usb_ms_index,
  336. { "wIndex", "usbms.setup.wIndex", FT_UINT16, BASE_DEC, NULL, 0x0,
  337. NULL, HFILL }},
  338. { &hf_usb_ms_length,
  339. { "wLength", "usbms.setup.wLength", FT_UINT16, BASE_DEC, NULL, 0x0,
  340. NULL, HFILL }},
  341. { &hf_usb_ms_maxlun,
  342. { "Max LUN", "usbms.setup.maxlun", FT_UINT8, BASE_DEC, NULL, 0x0,
  343. NULL, HFILL }},
  344. };
  345. static gint *usb_ms_subtrees[] = {
  346. &ett_usb_ms,
  347. };
  348. proto_usb_ms = proto_register_protocol("USB Mass Storage", "USBMS", "usbms");
  349. proto_register_field_array(proto_usb_ms, hf, array_length(hf));
  350. proto_register_subtree_array(usb_ms_subtrees, array_length(usb_ms_subtrees));
  351. register_dissector("usbms", dissect_usb_ms_bulk, proto_usb_ms);
  352. }
  353. void
  354. proto_reg_handoff_usb_ms(void)
  355. {
  356. dissector_handle_t usb_ms_bulk_handle;
  357. dissector_handle_t usb_ms_control_handle;
  358. usb_ms_bulk_handle = find_dissector("usbms");
  359. dissector_add_uint("usb.bulk", IF_CLASS_MASSTORAGE, usb_ms_bulk_handle);
  360. usb_ms_control_handle = new_create_dissector_handle(dissect_usb_ms_control, proto_usb_ms);
  361. dissector_add_uint("usb.control", IF_CLASS_MASSTORAGE, usb_ms_control_handle);
  362. }