Update Free Software Foundation address.
[metze/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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 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 "packet-usb.h"
33 #include "packet-scsi.h"
34
35 /* protocols and header fields */
36 static int proto_usb_ms = -1;
37 static int hf_usb_ms_dCBWSignature = -1;
38 static int hf_usb_ms_dCBWTag = -1;
39 static int hf_usb_ms_dCBWDataTransferLength = -1;
40 static int hf_usb_ms_dCBWFlags = -1;
41 static int hf_usb_ms_dCBWLUN = -1;
42 static int hf_usb_ms_dCBWCBLength = -1;
43 static int hf_usb_ms_dCSWSignature = -1;
44 static int hf_usb_ms_dCSWDataResidue = -1;
45 static int hf_usb_ms_dCSWStatus = -1;
46 static int hf_usb_ms_request = -1;
47 static int hf_usb_ms_value = -1;
48 static int hf_usb_ms_index = -1;
49 static int hf_usb_ms_length = -1;
50 static int hf_usb_ms_maxlun = -1;
51
52 static gint ett_usb_ms = -1;
53
54
55 /* there is one such structure for each masstorage conversation */
56 typedef struct _usb_ms_conv_info_t {
57     emem_tree_t *itl;           /* indexed by LUN */
58     emem_tree_t *itlq;          /* pinfo->fd->num */
59 } usb_ms_conv_info_t;
60
61
62 static const value_string status_vals[] = {
63     {0x00,      "Command Passed"},
64     {0x01,      "Command Failed"},
65     {0x02,      "Phase Error"},
66     {0, NULL}
67 };
68
69
70
71
72 static void
73 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_)
74 {
75     if(is_request){
76         proto_tree_add_item(tree, hf_usb_ms_value, tvb, offset, 2, ENC_BIG_ENDIAN);
77         offset += 2;
78
79         proto_tree_add_item(tree, hf_usb_ms_index, tvb, offset, 2, ENC_BIG_ENDIAN);
80         offset += 2;
81
82         proto_tree_add_item(tree, hf_usb_ms_length, tvb, offset, 2, ENC_BIG_ENDIAN);
83         /*offset += 2;*/
84     } else {
85         /* no data in reset response */
86     }
87 }
88
89 static void
90 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_)
91 {
92     if(is_request){
93         proto_tree_add_item(tree, hf_usb_ms_value, tvb, offset, 2, ENC_BIG_ENDIAN);
94         offset += 2;
95
96         proto_tree_add_item(tree, hf_usb_ms_index, tvb, offset, 2, ENC_BIG_ENDIAN);
97         offset += 2;
98
99         proto_tree_add_item(tree, hf_usb_ms_length, tvb, offset, 2, ENC_BIG_ENDIAN);
100         /*offset += 2;*/
101     } else {
102         proto_tree_add_item(tree, hf_usb_ms_maxlun, tvb, offset, 1, ENC_BIG_ENDIAN);
103         offset++;
104     }
105 }
106
107
108 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);
109
110 typedef struct _usb_setup_dissector_table_t {
111     guint8 request;
112     usb_setup_dissector dissector;
113 } usb_setup_dissector_table_t;
114 #define USB_SETUP_RESET               0xff
115 #define USB_SETUP_GET_MAX_LUN         0xfe
116 static const usb_setup_dissector_table_t setup_dissectors[] = {
117     {USB_SETUP_RESET,          dissect_usb_ms_reset},
118     {USB_SETUP_GET_MAX_LUN,    dissect_usb_ms_get_max_lun},
119     {0, NULL}
120 };
121 static const value_string setup_request_names_vals[] = {
122     {USB_SETUP_RESET,          "RESET"},
123     {USB_SETUP_GET_MAX_LUN,    "GET MAX LUN"},
124     {0, NULL}
125 };
126
127 /* Dissector for mass storage control .
128  * Returns TRUE if a class specific dissector was found
129  * and FALSE othervise.
130  */
131 static gint
132 dissect_usb_ms_control(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
133 {
134     gboolean is_request;
135     usb_conv_info_t *usb_conv_info;
136     usb_trans_info_t *usb_trans_info;
137     int offset=0;
138     usb_setup_dissector dissector;
139     const usb_setup_dissector_table_t *tmp;
140
141
142     is_request=(pinfo->srcport==NO_ENDPOINT);
143
144     usb_conv_info=pinfo->usb_conv_info;
145     usb_trans_info=usb_conv_info->usb_trans_info;
146
147
148     /* See if we can find a class specific dissector for this request */
149     dissector=NULL;
150     for(tmp=setup_dissectors;tmp->dissector;tmp++){
151         if (tmp->request == usb_trans_info->setup.request){
152             dissector=tmp->dissector;
153             break;
154         }
155     }
156     /* No we could not find any class specific dissector for this request
157      * return FALSE and let USB try any of the standard requests.
158      */
159     if(!dissector){
160         return FALSE;
161     }
162
163
164     col_set_str(pinfo->cinfo, COL_PROTOCOL, "USBMS");
165
166     if (check_col(pinfo->cinfo, COL_INFO)) {
167         col_add_fstr(pinfo->cinfo, COL_INFO, "%s %s",
168             val_to_str(usb_trans_info->setup.request, setup_request_names_vals, "Unknown type %x"),
169             is_request?"Request":"Response");
170     }
171
172     if(is_request){
173         proto_tree_add_item(tree, hf_usb_ms_request, tvb, offset, 1, ENC_LITTLE_ENDIAN);
174         offset += 1;
175     }
176
177     dissector(pinfo, tree, tvb, offset, is_request, usb_trans_info, usb_conv_info);
178     return TRUE;
179 }
180
181
182 /* dissector for mass storage bulk data */
183 static void
184 dissect_usb_ms_bulk(tvbuff_t *tvb, packet_info *pinfo, proto_tree *parent_tree)
185 {
186     usb_conv_info_t *usb_conv_info;
187     usb_ms_conv_info_t *usb_ms_conv_info;
188     proto_tree *tree=NULL;
189     guint32 signature=0;
190     int offset=0;
191     gboolean is_request;
192     itl_nexus_t *itl;
193     itlq_nexus_t *itlq;
194
195     usb_conv_info=pinfo->usb_conv_info;
196     /* verify that we do have a usb_ms_conv_info */
197     usb_ms_conv_info=usb_conv_info->class_data;
198     if(!usb_ms_conv_info){
199         usb_ms_conv_info=se_alloc(sizeof(usb_ms_conv_info_t));
200         usb_ms_conv_info->itl=se_tree_create_non_persistent(EMEM_TREE_TYPE_RED_BLACK, "USB ITL");
201         usb_ms_conv_info->itlq=se_tree_create_non_persistent(EMEM_TREE_TYPE_RED_BLACK, "USB ITLQ");
202         usb_conv_info->class_data=usb_ms_conv_info;
203     }
204
205
206     is_request=(pinfo->srcport==NO_ENDPOINT);
207
208     col_set_str(pinfo->cinfo, COL_PROTOCOL, "USBMS");
209
210     col_clear(pinfo->cinfo, COL_INFO);
211
212
213     if(parent_tree){
214         proto_item *ti = NULL;
215         ti = proto_tree_add_protocol_format(parent_tree, proto_usb_ms, tvb, 0, -1, "USB Mass Storage");
216
217         tree = proto_item_add_subtree(ti, ett_usb_ms);
218     }
219
220     signature=tvb_get_letohl(tvb, offset);
221
222
223     /*
224      * SCSI CDB inside CBW
225      */
226     if(is_request&&(signature==0x43425355)&&(tvb_length(tvb)==31)){
227         tvbuff_t *cdb_tvb;
228         int cdbrlen, cdblen;
229         guint8 lun, flags;
230         guint32 datalen;
231
232         /* dCBWSignature */
233         proto_tree_add_item(tree, hf_usb_ms_dCBWSignature, tvb, offset, 4, ENC_LITTLE_ENDIAN);
234         offset+=4;
235
236         /* dCBWTag */
237         proto_tree_add_item(tree, hf_usb_ms_dCBWTag, tvb, offset, 4, ENC_LITTLE_ENDIAN);
238         offset+=4;
239
240         /* dCBWDataTransferLength */
241         proto_tree_add_item(tree, hf_usb_ms_dCBWDataTransferLength, tvb, offset, 4, ENC_LITTLE_ENDIAN);
242         datalen=tvb_get_letohl(tvb, offset);
243         offset+=4;
244
245         /* dCBWFlags */
246         proto_tree_add_item(tree, hf_usb_ms_dCBWFlags, tvb, offset, 1, ENC_LITTLE_ENDIAN);
247         flags=tvb_get_guint8(tvb, offset);
248         offset+=1;
249
250         /* dCBWLUN */
251         proto_tree_add_item(tree, hf_usb_ms_dCBWLUN, tvb, offset, 1, ENC_LITTLE_ENDIAN);
252         lun=tvb_get_guint8(tvb, offset)&0x0f;
253         offset+=1;
254
255         /* make sure we have a ITL structure for this LUN */
256         itl=(itl_nexus_t *)se_tree_lookup32(usb_ms_conv_info->itl, lun);
257         if(!itl){
258             itl=se_alloc(sizeof(itl_nexus_t));
259             itl->cmdset=0xff;
260             itl->conversation=NULL;
261             se_tree_insert32(usb_ms_conv_info->itl, lun, itl);
262         }
263
264         /* make sure we have an ITLQ structure for this LUN/transaction */
265         itlq=(itlq_nexus_t *)se_tree_lookup32(usb_ms_conv_info->itlq, pinfo->fd->num);
266         if(!itlq){
267             itlq=se_alloc(sizeof(itlq_nexus_t));
268             itlq->lun=lun;
269             itlq->scsi_opcode=0xffff;
270             itlq->task_flags=0;
271             if(datalen){
272                 if(flags&0x80){
273                     itlq->task_flags|=SCSI_DATA_READ;
274                 } else {
275                     itlq->task_flags|=SCSI_DATA_WRITE;
276                 }
277             }
278             itlq->data_length=datalen;
279             itlq->bidir_data_length=0;
280             itlq->fc_time=pinfo->fd->abs_ts;
281             itlq->first_exchange_frame=pinfo->fd->num;
282             itlq->last_exchange_frame=0;
283             itlq->flags=0;
284             itlq->alloc_len=0;
285             itlq->extra_data=NULL;
286             se_tree_insert32(usb_ms_conv_info->itlq, pinfo->fd->num, itlq);
287         }
288
289         /* dCBWCBLength */
290         proto_tree_add_item(tree, hf_usb_ms_dCBWCBLength, tvb, offset, 1, ENC_LITTLE_ENDIAN);
291         cdbrlen=tvb_get_guint8(tvb, offset)&0x1f;
292         offset+=1;
293
294         cdblen=cdbrlen;
295         if(cdblen>tvb_length_remaining(tvb, offset)){
296             cdblen=tvb_length_remaining(tvb, offset);
297         }
298         if(cdblen){
299             cdb_tvb=tvb_new_subset(tvb, offset, cdblen, cdbrlen);
300             dissect_scsi_cdb(cdb_tvb, pinfo, parent_tree, SCSI_DEV_UNKNOWN, itlq, itl);
301         }
302         return;
303     }
304
305
306     /*
307      * SCSI RESPONSE inside CSW
308      */
309     if((!is_request)&&(signature==0x53425355)&&(tvb_length(tvb)==13)){
310         guint8 status;
311
312         /* dCSWSignature */
313         proto_tree_add_item(tree, hf_usb_ms_dCSWSignature, tvb, offset, 4, ENC_LITTLE_ENDIAN);
314         offset+=4;
315
316         /* dCSWTag */
317         proto_tree_add_item(tree, hf_usb_ms_dCBWTag, tvb, offset, 4, ENC_LITTLE_ENDIAN);
318         offset+=4;
319
320         /* dCSWDataResidue */
321         proto_tree_add_item(tree, hf_usb_ms_dCSWDataResidue, tvb, offset, 4, ENC_LITTLE_ENDIAN);
322         offset+=4;
323
324         /* dCSWStatus */
325         proto_tree_add_item(tree, hf_usb_ms_dCSWStatus, tvb, offset, 1, ENC_LITTLE_ENDIAN);
326         status=tvb_get_guint8(tvb, offset);
327         /*offset+=1;*/
328
329         itlq=(itlq_nexus_t *)se_tree_lookup32_le(usb_ms_conv_info->itlq, pinfo->fd->num);
330         if(!itlq){
331             return;
332         }
333         itlq->last_exchange_frame=pinfo->fd->num;
334
335         itl=(itl_nexus_t *)se_tree_lookup32(usb_ms_conv_info->itl, itlq->lun);
336         if(!itl){
337             return;
338         }
339
340         if(!status){
341             dissect_scsi_rsp(tvb, pinfo, parent_tree, itlq, itl, 0);
342         } else {
343             /* just send "check condition" */
344             dissect_scsi_rsp(tvb, pinfo, parent_tree, itlq, itl, 0x02);
345         }
346         return;
347     }
348
349     /*
350      * Ok it was neither CDB not STATUS so just assume it is either data in/out
351      */
352     itlq=(itlq_nexus_t *)se_tree_lookup32_le(usb_ms_conv_info->itlq, pinfo->fd->num);
353     if(!itlq){
354         return;
355     }
356
357     itl=(itl_nexus_t *)se_tree_lookup32(usb_ms_conv_info->itl, itlq->lun);
358     if(!itl){
359         return;
360     }
361
362     dissect_scsi_payload(tvb, pinfo, parent_tree, is_request, itlq, itl, 0);
363
364 }
365
366 void
367 proto_register_usb_ms(void)
368 {
369     static hf_register_info hf[] = {
370         { &hf_usb_ms_dCBWSignature,
371         { "Signature", "usbms.dCBWSignature", FT_UINT32, BASE_HEX,
372           NULL, 0x0, NULL, HFILL }},
373
374         { &hf_usb_ms_dCBWTag,
375         { "Tag", "usbms.dCBWTag", FT_UINT32, BASE_HEX,
376           NULL, 0x0, NULL, HFILL }},
377
378         { &hf_usb_ms_dCBWDataTransferLength,
379         { "DataTransferLength", "usbms.dCBWDataTransferLength", FT_UINT32, BASE_DEC,
380           NULL, 0x0, NULL, HFILL }},
381
382         { &hf_usb_ms_dCBWFlags,
383         { "Flags", "usbms.dCBWFlags", FT_UINT8, BASE_HEX,
384           NULL, 0x0, NULL, HFILL }},
385
386         { &hf_usb_ms_dCBWLUN,
387         { "LUN", "usbms.dCBWLUN", FT_UINT8, BASE_HEX,
388           NULL, 0x0f, NULL, HFILL }},
389
390         { &hf_usb_ms_dCBWCBLength,
391         { "CDB Length", "usbms.dCBWCBLength", FT_UINT8, BASE_HEX,
392           NULL, 0x1f, NULL, HFILL }},
393
394         { &hf_usb_ms_dCSWSignature,
395         { "Signature", "usbms.dCSWSignature", FT_UINT32, BASE_HEX,
396           NULL, 0x0, NULL, HFILL }},
397
398         { &hf_usb_ms_dCSWDataResidue,
399         { "DataResidue", "usbms.dCSWDataResidue", FT_UINT32, BASE_DEC,
400           NULL, 0x0, NULL, HFILL }},
401
402         { &hf_usb_ms_dCSWStatus,
403         { "Status", "usbms.dCSWStatus", FT_UINT8, BASE_HEX,
404           VALS(status_vals), 0x0, NULL, HFILL }},
405
406         { &hf_usb_ms_request,
407         { "bRequest", "usbms.setup.bRequest", FT_UINT8, BASE_HEX, VALS(setup_request_names_vals), 0x0,
408                 NULL, HFILL }},
409
410         { &hf_usb_ms_value,
411         { "wValue", "usbms.setup.wValue", FT_UINT16, BASE_HEX, NULL, 0x0,
412                 NULL, HFILL }},
413
414         { &hf_usb_ms_index,
415         { "wIndex", "usbms.setup.wIndex", FT_UINT16, BASE_DEC, NULL, 0x0,
416                 NULL, HFILL }},
417
418         { &hf_usb_ms_length,
419         { "wLength", "usbms.setup.wLength", FT_UINT16, BASE_DEC, NULL, 0x0,
420                 NULL, HFILL }},
421
422         { &hf_usb_ms_maxlun,
423         { "Max LUN", "usbms.setup.maxlun", FT_UINT8, BASE_DEC, NULL, 0x0,
424                 NULL, HFILL }},
425
426     };
427
428     static gint *usb_ms_subtrees[] = {
429             &ett_usb_ms,
430     };
431
432
433     proto_usb_ms = proto_register_protocol("USB Mass Storage", "USBMS", "usbms");
434     proto_register_field_array(proto_usb_ms, hf, array_length(hf));
435     proto_register_subtree_array(usb_ms_subtrees, array_length(usb_ms_subtrees));
436
437     register_dissector("usbms", dissect_usb_ms_bulk, proto_usb_ms);
438 }
439
440 void
441 proto_reg_handoff_usb_ms(void)
442 {
443     dissector_handle_t usb_ms_bulk_handle;
444     dissector_handle_t usb_ms_control_handle;
445
446     usb_ms_bulk_handle = find_dissector("usbms");
447     dissector_add_uint("usb.bulk", IF_CLASS_MASSTORAGE, usb_ms_bulk_handle);
448
449     usb_ms_control_handle = new_create_dissector_handle(dissect_usb_ms_control, proto_usb_ms);
450     dissector_add_uint("usb.control", IF_CLASS_MASSTORAGE, usb_ms_control_handle);
451 }