81669d1dc24fdeb6125ada0cf10a557521117426
[obnox/wireshark/wip.git] / epan / dissectors / packet-usb-masstorage.c
1 /* packet-usb-masstorage.c
2  *
3  * $Id$
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  
23
24 #ifdef HAVE_CONFIG_H
25 # include "config.h"
26 #endif
27
28 #include <glib.h>
29 #include <epan/packet.h>
30 #include <epan/emem.h>
31 #include <epan/conversation.h>
32 #include <string.h>
33 #include "packet-usb.h"
34 #include "packet-scsi.h"
35
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;
47
48 static gint ett_usb_ms = -1;
49
50
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 */
55 } usb_ms_conv_info_t;
56
57
58 static const value_string status_vals[] = {
59     {0x00,      "Command Passed"},
60     {0x01,      "Command Failed"},
61     {0x02,      "Phase Error"},
62     {0, NULL}
63 };
64
65 static void
66 dissect_usb_ms(tvbuff_t *tvb, packet_info *pinfo, proto_tree *parent_tree)
67 {
68     usb_conv_info_t *usb_conv_info;
69     usb_ms_conv_info_t *usb_ms_conv_info;
70     proto_tree *tree=NULL;
71     guint32 signature=0;
72     int offset=0;
73     gboolean is_request;
74     itl_nexus_t *itl;
75     itlq_nexus_t *itlq;
76
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;
85     }
86
87
88     is_request=(pinfo->srcport==NO_ENDPOINT);
89
90     if(check_col(pinfo->cinfo, COL_PROTOCOL))
91         col_set_str(pinfo->cinfo, COL_PROTOCOL, "USBMS");
92
93     if(check_col(pinfo->cinfo, COL_INFO))
94         col_clear(pinfo->cinfo, COL_INFO);
95
96
97     if(parent_tree){
98         proto_item *ti = NULL;
99         ti = proto_tree_add_protocol_format(parent_tree, proto_usb_ms, tvb, 0, -1, "USB Mass Storage");
100
101         tree = proto_item_add_subtree(ti, ett_usb_ms);
102     }
103
104     signature=tvb_get_letohl(tvb, offset);
105
106
107     /* 
108      * SCSI CDB inside CBW 
109      */
110     if(is_request&&(signature==0x43425355)&&(tvb_length(tvb)==31)){
111         tvbuff_t *cdb_tvb;
112         int cdbrlen, cdblen;
113         guint8 lun, flags;
114         guint32 datalen;
115
116         /* dCBWSignature */
117         proto_tree_add_item(tree, hf_usb_ms_dCBWSignature, tvb, offset, 4, TRUE);
118         offset+=4;
119
120         /* dCBWTag */
121         proto_tree_add_item(tree, hf_usb_ms_dCBWTag, tvb, offset, 4, TRUE);
122         offset+=4;
123
124         /* dCBWDataTransferLength */
125         proto_tree_add_item(tree, hf_usb_ms_dCBWDataTransferLength, tvb, offset, 4, TRUE);
126         datalen=tvb_get_letohl(tvb, offset);
127         offset+=4;
128
129         /* dCBWFlags */
130         proto_tree_add_item(tree, hf_usb_ms_dCBWFlags, tvb, offset, 1, TRUE);
131         flags=tvb_get_guint8(tvb, offset);
132         offset+=1;
133
134         /* dCBWLUN */
135         proto_tree_add_item(tree, hf_usb_ms_dCBWLUN, tvb, offset, 1, TRUE);
136         lun=tvb_get_guint8(tvb, offset)&0x0f;
137         offset+=1;
138
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);
141         if(!itl){
142             itl=se_alloc(sizeof(itl_nexus_t));
143             itl->cmdset=0xff;
144             itl->conversation=NULL;
145             se_tree_insert32(usb_ms_conv_info->itl, lun, itl);
146         }
147
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);
150         if(!itlq){
151             itlq=se_alloc(sizeof(itlq_nexus_t));
152             itlq->lun=lun;
153             itlq->scsi_opcode=0xffff;
154             itlq->task_flags=0;
155             if(datalen){
156                 if(flags&0x80){
157                     itlq->task_flags|=SCSI_DATA_READ;
158                 } else {
159                     itlq->task_flags|=SCSI_DATA_WRITE;
160                 }
161             }
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;
167             itlq->flags=0;
168             itlq->alloc_len=0;
169             itlq->extra_data=NULL;
170             se_tree_insert32(usb_ms_conv_info->itlq, pinfo->fd->num, itlq);
171         }
172
173         /* dCBWCBLength */
174         proto_tree_add_item(tree, hf_usb_ms_dCBWCBLength, tvb, offset, 1, TRUE);
175         cdbrlen=tvb_get_guint8(tvb, offset)&0x1f;
176         offset+=1;
177
178         cdblen=cdbrlen;
179         if(cdblen>tvb_length_remaining(tvb, offset)){
180             cdblen=tvb_length_remaining(tvb, offset);
181         }
182         if(cdblen){
183             cdb_tvb=tvb_new_subset(tvb, offset, cdblen, cdbrlen);
184             dissect_scsi_cdb(cdb_tvb, pinfo, parent_tree, SCSI_DEV_UNKNOWN, itlq, itl);
185         }
186         return;
187     }
188
189
190     /* 
191      * SCSI RESPONSE inside CSW 
192      */
193     if((!is_request)&&(signature==0x53425355)&&(tvb_length(tvb)==13)){
194         guint8 status;
195
196         /* dCSWSignature */
197         proto_tree_add_item(tree, hf_usb_ms_dCSWSignature, tvb, offset, 4, TRUE);
198         offset+=4;
199
200         /* dCSWTag */
201         proto_tree_add_item(tree, hf_usb_ms_dCBWTag, tvb, offset, 4, TRUE);
202         offset+=4;
203
204         /* dCSWDataResidue */
205         proto_tree_add_item(tree, hf_usb_ms_dCSWDataResidue, tvb, offset, 4, TRUE);
206         offset+=4;
207
208         /* dCSWStatus */
209         proto_tree_add_item(tree, hf_usb_ms_dCSWStatus, tvb, offset, 1, TRUE);
210         status=tvb_get_guint8(tvb, offset);
211         offset+=1;
212
213         itlq=(itlq_nexus_t *)se_tree_lookup32_le(usb_ms_conv_info->itlq, pinfo->fd->num);
214         if(!itlq){
215             return;
216         }
217         itlq->last_exchange_frame=pinfo->fd->num;
218             
219         itl=(itl_nexus_t *)se_tree_lookup32(usb_ms_conv_info->itl, itlq->lun);
220         if(!itl){
221             return;
222         }
223        
224         if(!status){
225             dissect_scsi_rsp(tvb, pinfo, parent_tree, itlq, itl, 0);
226         } else {
227             /* just send "check condition" */
228             dissect_scsi_rsp(tvb, pinfo, parent_tree, itlq, itl, 0x02);
229         }
230         return;
231     }
232
233     /*
234      * Ok it was neither CDB not STATUS so just assume it is either data in/out
235      */
236     itlq=(itlq_nexus_t *)se_tree_lookup32_le(usb_ms_conv_info->itlq, pinfo->fd->num);
237     if(!itlq){
238         return;
239     }
240             
241     itl=(itl_nexus_t *)se_tree_lookup32(usb_ms_conv_info->itl, itlq->lun);
242     if(!itl){
243         return;
244     }
245
246     dissect_scsi_payload(tvb, pinfo, parent_tree, is_request, itlq, itl, 0);
247
248 }
249
250 void
251 proto_register_usb_ms(void)
252 {
253     static hf_register_info hf[] = {
254         { &hf_usb_ms_dCBWSignature,
255         { "Signature", "usbms.dCBWSignature", FT_UINT32, BASE_HEX, 
256           NULL, 0x0, "", HFILL }},
257
258         { &hf_usb_ms_dCBWTag,
259         { "Tag", "usbms.dCBWTag", FT_UINT32, BASE_HEX, 
260           NULL, 0x0, "", HFILL }},
261
262         { &hf_usb_ms_dCBWDataTransferLength,
263         { "DataTransferLength", "usbms.dCBWDataTransferLength", FT_UINT32, BASE_DEC, 
264           NULL, 0x0, "", HFILL }},
265
266         { &hf_usb_ms_dCBWFlags,
267         { "Flags", "usbms.dCBWFlags", FT_UINT8, BASE_HEX, 
268           NULL, 0x0, "", HFILL }},
269
270         { &hf_usb_ms_dCBWLUN,
271         { "LUN", "usbms.dCBWLUN", FT_UINT8, BASE_HEX, 
272           NULL, 0x0f, "", HFILL }},
273
274         { &hf_usb_ms_dCBWCBLength,
275         { "CDB Length", "usbms.dCBWCBLength", FT_UINT8, BASE_HEX, 
276           NULL, 0x1f, "", HFILL }},
277
278         { &hf_usb_ms_dCSWSignature,
279         { "Signature", "usbms.dCSWSignature", FT_UINT32, BASE_HEX, 
280           NULL, 0x0, "", HFILL }},
281
282         { &hf_usb_ms_dCSWDataResidue,
283         { "DataResidue", "usbms.dCSWDataResidue", FT_UINT32, BASE_DEC, 
284           NULL, 0x0, "", HFILL }},
285
286         { &hf_usb_ms_dCSWStatus,
287         { "Status", "usbms.dCSWStatus", FT_UINT8, BASE_HEX, 
288           VALS(status_vals), 0x0, "", HFILL }},
289
290     };
291     
292     static gint *usb_ms_subtrees[] = {
293             &ett_usb_ms,
294     };
295
296      
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));
300
301     register_dissector("usbms", dissect_usb_ms, proto_usb_ms);
302 }
303
304 void
305 proto_reg_handoff_usb_ms(void)
306 {
307     dissector_handle_t usb_ms_handle;
308     usb_ms_handle = create_dissector_handle(dissect_usb_ms, proto_usb_ms);
309
310     dissector_add("usb.bulk", IF_CLASS_MASSTORAGE, usb_ms_handle);
311 }