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