1 /* packet-usb-masstorage.c
5 * usb mass storage dissector
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.
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.
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.
29 #include <epan/packet.h>
30 #include <epan/emem.h>
31 #include <epan/conversation.h>
33 #include "packet-usb.h"
34 #include "packet-scsi.h"
36 /* protocols and header fields */
37 static int proto_usb_ms = -1;
38 static int hf_usb_ms_dCBWSignature = -1;
39 static int hf_usb_ms_dCBWTag = -1;
40 static int hf_usb_ms_dCBWDataTransferLength = -1;
41 static int hf_usb_ms_dCBWFlags = -1;
42 static int hf_usb_ms_dCBWLUN = -1;
43 static int hf_usb_ms_dCBWCBLength = -1;
44 static int hf_usb_ms_dCSWSignature = -1;
45 static int hf_usb_ms_dCSWDataResidue = -1;
46 static int hf_usb_ms_dCSWStatus = -1;
48 static gint ett_usb_ms = -1;
51 /* there is one such structure for each masstorage conversation */
52 typedef struct _usb_ms_conv_info_t {
53 emem_tree_t *itl; /* indexed by LUN */
54 emem_tree_t *itlq; /* pinfo->fd->num */
58 static const value_string status_vals[] = {
59 {0x00, "Command Passed"},
60 {0x01, "Command Failed"},
61 {0x02, "Phase Error"},
66 dissect_usb_ms(tvbuff_t *tvb, packet_info *pinfo, proto_tree *parent_tree)
68 usb_conv_info_t *usb_conv_info;
69 usb_ms_conv_info_t *usb_ms_conv_info;
70 proto_tree *tree=NULL;
77 usb_conv_info=pinfo->usb_conv_info;
78 /* verify that we do have a usb_ms_conv_info */
79 usb_ms_conv_info=usb_conv_info->masstorage;
80 if(!usb_ms_conv_info){
81 usb_ms_conv_info=se_alloc(sizeof(usb_ms_conv_info_t));
82 usb_ms_conv_info->itl=se_tree_create_non_persistent(EMEM_TREE_TYPE_RED_BLACK, "USB ITL");
83 usb_ms_conv_info->itlq=se_tree_create_non_persistent(EMEM_TREE_TYPE_RED_BLACK, "USB ITLQ");
84 usb_conv_info->masstorage=usb_ms_conv_info;
88 is_request=(pinfo->srcport==NO_ENDPOINT);
90 if(check_col(pinfo->cinfo, COL_PROTOCOL))
91 col_set_str(pinfo->cinfo, COL_PROTOCOL, "USBMS");
93 if(check_col(pinfo->cinfo, COL_INFO))
94 col_clear(pinfo->cinfo, COL_INFO);
98 proto_item *ti = NULL;
99 ti = proto_tree_add_protocol_format(parent_tree, proto_usb_ms, tvb, 0, -1, "USB Mass Storage");
101 tree = proto_item_add_subtree(ti, ett_usb_ms);
104 signature=tvb_get_letohl(tvb, offset);
108 * SCSI CDB inside CBW
110 if(is_request&&(signature==0x43425355)&&(tvb_length(tvb)==31)){
117 proto_tree_add_item(tree, hf_usb_ms_dCBWSignature, tvb, offset, 4, TRUE);
121 proto_tree_add_item(tree, hf_usb_ms_dCBWTag, tvb, offset, 4, TRUE);
124 /* dCBWDataTransferLength */
125 proto_tree_add_item(tree, hf_usb_ms_dCBWDataTransferLength, tvb, offset, 4, TRUE);
126 datalen=tvb_get_letohl(tvb, offset);
130 proto_tree_add_item(tree, hf_usb_ms_dCBWFlags, tvb, offset, 1, TRUE);
131 flags=tvb_get_guint8(tvb, offset);
135 proto_tree_add_item(tree, hf_usb_ms_dCBWLUN, tvb, offset, 1, TRUE);
136 lun=tvb_get_guint8(tvb, offset)&0x0f;
139 /* make sure we have a ITL structure for this LUN */
140 itl=(itl_nexus_t *)se_tree_lookup32(usb_ms_conv_info->itl, lun);
142 itl=se_alloc(sizeof(itl_nexus_t));
144 itl->conversation=NULL;
145 se_tree_insert32(usb_ms_conv_info->itl, lun, itl);
148 /* make sure we have an ITLQ structure for this LUN/transaction */
149 itlq=(itlq_nexus_t *)se_tree_lookup32(usb_ms_conv_info->itlq, pinfo->fd->num);
151 itlq=se_alloc(sizeof(itlq_nexus_t));
153 itlq->scsi_opcode=0xffff;
157 itlq->task_flags|=SCSI_DATA_READ;
159 itlq->task_flags|=SCSI_DATA_WRITE;
162 itlq->data_length=datalen;
163 itlq->bidir_data_length=0;
164 itlq->fc_time=pinfo->fd->abs_ts;
165 itlq->first_exchange_frame=pinfo->fd->num;
166 itlq->last_exchange_frame=0;
169 itlq->extra_data=NULL;
170 se_tree_insert32(usb_ms_conv_info->itlq, pinfo->fd->num, itlq);
174 proto_tree_add_item(tree, hf_usb_ms_dCBWCBLength, tvb, offset, 1, TRUE);
175 cdbrlen=tvb_get_guint8(tvb, offset)&0x1f;
179 if(cdblen>tvb_length_remaining(tvb, offset)){
180 cdblen=tvb_length_remaining(tvb, offset);
183 cdb_tvb=tvb_new_subset(tvb, offset, cdblen, cdbrlen);
184 dissect_scsi_cdb(cdb_tvb, pinfo, parent_tree, SCSI_DEV_UNKNOWN, itlq, itl);
191 * SCSI RESPONSE inside CSW
193 if((!is_request)&&(signature==0x53425355)&&(tvb_length(tvb)==13)){
197 proto_tree_add_item(tree, hf_usb_ms_dCSWSignature, tvb, offset, 4, TRUE);
201 proto_tree_add_item(tree, hf_usb_ms_dCBWTag, tvb, offset, 4, TRUE);
204 /* dCSWDataResidue */
205 proto_tree_add_item(tree, hf_usb_ms_dCSWDataResidue, tvb, offset, 4, TRUE);
209 proto_tree_add_item(tree, hf_usb_ms_dCSWStatus, tvb, offset, 1, TRUE);
210 status=tvb_get_guint8(tvb, offset);
213 itlq=(itlq_nexus_t *)se_tree_lookup32_le(usb_ms_conv_info->itlq, pinfo->fd->num);
217 itlq->last_exchange_frame=pinfo->fd->num;
219 itl=(itl_nexus_t *)se_tree_lookup32(usb_ms_conv_info->itl, itlq->lun);
225 dissect_scsi_rsp(tvb, pinfo, parent_tree, itlq, itl, 0);
227 /* just send "check condition" */
228 dissect_scsi_rsp(tvb, pinfo, parent_tree, itlq, itl, 0x02);
234 * Ok it was neither CDB not STATUS so just assume it is either data in/out
236 itlq=(itlq_nexus_t *)se_tree_lookup32_le(usb_ms_conv_info->itlq, pinfo->fd->num);
241 itl=(itl_nexus_t *)se_tree_lookup32(usb_ms_conv_info->itl, itlq->lun);
246 dissect_scsi_payload(tvb, pinfo, parent_tree, is_request, itlq, itl, 0);
251 proto_register_usb_ms(void)
253 static hf_register_info hf[] = {
254 { &hf_usb_ms_dCBWSignature,
255 { "Signature", "usbms.dCBWSignature", FT_UINT32, BASE_HEX,
256 NULL, 0x0, "", HFILL }},
258 { &hf_usb_ms_dCBWTag,
259 { "Tag", "usbms.dCBWTag", FT_UINT32, BASE_HEX,
260 NULL, 0x0, "", HFILL }},
262 { &hf_usb_ms_dCBWDataTransferLength,
263 { "DataTransferLength", "usbms.dCBWDataTransferLength", FT_UINT32, BASE_DEC,
264 NULL, 0x0, "", HFILL }},
266 { &hf_usb_ms_dCBWFlags,
267 { "Flags", "usbms.dCBWFlags", FT_UINT8, BASE_HEX,
268 NULL, 0x0, "", HFILL }},
270 { &hf_usb_ms_dCBWLUN,
271 { "LUN", "usbms.dCBWLUN", FT_UINT8, BASE_HEX,
272 NULL, 0x0f, "", HFILL }},
274 { &hf_usb_ms_dCBWCBLength,
275 { "CDB Length", "usbms.dCBWCBLength", FT_UINT8, BASE_HEX,
276 NULL, 0x1f, "", HFILL }},
278 { &hf_usb_ms_dCSWSignature,
279 { "Signature", "usbms.dCSWSignature", FT_UINT32, BASE_HEX,
280 NULL, 0x0, "", HFILL }},
282 { &hf_usb_ms_dCSWDataResidue,
283 { "DataResidue", "usbms.dCSWDataResidue", FT_UINT32, BASE_DEC,
284 NULL, 0x0, "", HFILL }},
286 { &hf_usb_ms_dCSWStatus,
287 { "Status", "usbms.dCSWStatus", FT_UINT8, BASE_HEX,
288 VALS(status_vals), 0x0, "", HFILL }},
292 static gint *usb_ms_subtrees[] = {
297 proto_usb_ms = proto_register_protocol("USB Mass Storage", "USBMS", "usbms");
298 proto_register_field_array(proto_usb_ms, hf, array_length(hf));
299 proto_register_subtree_array(usb_ms_subtrees, array_length(usb_ms_subtrees));
301 register_dissector("usbms", dissect_usb_ms, proto_usb_ms);
305 proto_reg_handoff_usb_ms(void)
307 dissector_handle_t usb_ms_handle;
308 usb_ms_handle = create_dissector_handle(dissect_usb_ms, proto_usb_ms);
310 dissector_add("usb.bulk", IF_CLASS_MASSTORAGE, usb_ms_handle);