Add HP Switch Protocol SAP value
[obnox/wireshark/wip.git] / epan / dissectors / packet-fcp.c
1 /* packet-fcp.c
2  * Routines for Fibre Channel Protocol for SCSI (FCP)
3  * Copyright 2001, Dinesh G Dutt <ddutt@cisco.com>
4  *
5  * $Id$
6  *
7  * Wireshark - Network traffic analyzer
8  * By Gerald Combs <gerald@wireshark.org>
9  * Copyright 1998 Gerald Combs
10  *
11  * This program is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU General Public License
13  * as published by the Free Software Foundation; either version 2
14  * of the License, or (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software
23  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
24  */
25
26 #ifdef HAVE_CONFIG_H
27 # include "config.h"
28 #endif
29
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <string.h>
33
34 #ifdef HAVE_SYS_TYPES_H
35 # include <sys/types.h>
36 #endif
37
38 #ifdef HAVE_NETINET_IN_H
39 # include <netinet/in.h>
40 #endif
41
42 #include <glib.h>
43
44 #include <epan/prefs.h>
45 #include <epan/emem.h>
46 #include <epan/packet.h>
47 #include <epan/conversation.h>
48 #include <epan/etypes.h>
49 #include "packet-scsi.h"
50 #include "packet-fc.h"
51 #include "packet-fcp.h"
52
53 /* Initialize the protocol and registered fields */
54 static int proto_fcp         = -1;
55 static int hf_fcp_multilun   = -1;
56 static int hf_fcp_singlelun  = -1;
57 static int hf_fcp_crn        = -1;
58 static int hf_fcp_taskattr   = -1;
59 static int hf_fcp_taskmgmt   = -1;
60 static int hf_fcp_addlcdblen = -1;
61 static int hf_fcp_rddata     = -1;
62 static int hf_fcp_wrdata     = -1;
63 static int hf_fcp_dl         = -1;
64 static int hf_fcp_bidir_dl   = -1;
65 static int hf_fcp_data_ro    = -1;
66 static int hf_fcp_burstlen   = -1;
67 static int hf_fcp_rspflags   = -1;
68 static int hf_fcp_retry_delay_timer   = -1;
69 static int hf_fcp_resid      = -1;
70 static int hf_fcp_bidir_resid = -1;
71 static int hf_fcp_snslen     = -1;
72 static int hf_fcp_rsplen     = -1;
73 static int hf_fcp_rspcode    = -1;
74 static int hf_fcp_scsistatus = -1;
75 static int hf_fcp_type = -1;
76 static int hf_fcp_mgmt_flags_obsolete = -1;
77 static int hf_fcp_mgmt_flags_clear_aca = -1;
78 static int hf_fcp_mgmt_flags_target_reset = -1;
79 static int hf_fcp_mgmt_flags_lu_reset = -1;
80 static int hf_fcp_mgmt_flags_rsvd = -1;
81 static int hf_fcp_mgmt_flags_clear_task_set = -1;
82 static int hf_fcp_mgmt_flags_abort_task_set = -1;
83 static int hf_fcp_rsp_flags_bidi = -1;
84 static int hf_fcp_rsp_flags_bidi_rru = -1;
85 static int hf_fcp_rsp_flags_bidi_rro = -1;
86 static int hf_fcp_rsp_flags_conf_req = -1;
87 static int hf_fcp_rsp_flags_resid_under = -1;
88 static int hf_fcp_rsp_flags_resid_over = -1;
89 static int hf_fcp_rsp_flags_sns_vld = -1;
90 static int hf_fcp_rsp_flags_res_vld = -1;
91 static int hf_fcp_request_in = -1;
92 static int hf_fcp_response_in = -1;
93 static int hf_fcp_time = -1;
94
95 /* Initialize the subtree pointers */
96 static gint ett_fcp = -1;
97 static gint ett_fcp_taskmgmt = -1;
98 static gint ett_fcp_rsp_flags = -1;
99
100 typedef struct _fcp_conv_data_t {
101     emem_tree_t *luns;
102 } fcp_conv_data_t;
103
104 static dissector_table_t fcp_dissector;
105 static dissector_handle_t data_handle;
106
107 /* Information Categories based on lower 4 bits of R_CTL */
108 #define FCP_IU_DATA              0x1
109 #define FCP_IU_CONFIRM           0x3
110 #define FCP_IU_XFER_RDY          0x5
111 #define FCP_IU_CMD               0x6
112 #define FCP_IU_RSP               0x7
113
114 static const value_string fcp_iu_val[] = {
115     {FCP_IU_DATA      , "FCP_DATA"},
116     {FCP_IU_CONFIRM   , "Confirm"},
117     {FCP_IU_XFER_RDY  , "XFER_RDY"},
118     {FCP_IU_CMD       , "FCP_CMND"},
119     {FCP_IU_RSP       , "FCP_RSP"},
120     {0, NULL},
121 };
122
123
124 /* Task Attribute Values */
125 static const value_string fcp_task_attr_val[] = {
126     {0, "Simple"},
127     {1, "Head of Queue"},
128     {2, "Ordered"},
129     {4, "ACA"},
130     {5, "Untagged"},
131     {0, NULL},
132 };
133
134 /* RSP Code Definitions (from FCP_RSP_INFO) */
135 static const value_string fcp_rsp_code_val[] = {
136     {0, "Task Management Function Complete"},
137     {1, "FCP_DATA length Different from FCP_BURST_LEN"},
138     {2, "FCP_CMND Fields Invalid"},
139     {3, "FCP_DATA Parameter Mismatch With FCP_DATA_RO"},
140     {4, "Task Management Function Rejected"},
141     {5, "Task Management Function Failed"},
142     {9, "Task Management Function Incorrect LUN"},
143     {0, NULL},
144 };
145
146
147
148 static const true_false_string fcp_mgmt_flags_obsolete_tfs = {
149    "OBSOLETE BIT is SET",
150    "OBSOLETE BIT is NOT set",
151 };
152 static const true_false_string fcp_mgmt_flags_clear_aca_tfs = {
153    "CLEAR ACA is SET",
154    "Clear aca is NOT set",
155 };
156 static const true_false_string fcp_mgmt_flags_target_reset_tfs = {
157    "TARGET RESET is SET",
158    "Target reset is NOT set",
159 };
160 static const true_false_string fcp_mgmt_flags_lu_reset_tfs = {
161    "LU RESET is SET",
162    "Lu reset is NOT set",
163 };
164 static const true_false_string fcp_mgmt_flags_rsvd_tfs = {
165    "RSVD is SET",
166    "Rsvd is NOT set",
167 };
168 static const true_false_string fcp_mgmt_flags_clear_task_set_tfs = {
169    "CLEAR TASK SET is SET",
170    "Clear task set is NOT set",
171 };
172 static const true_false_string fcp_mgmt_flags_abort_task_set_tfs = {
173    "ABORT TASK SET is SET",
174    "Abort task set is NOT set",
175 };
176
177 static void
178 dissect_task_mgmt_flags (packet_info *pinfo, proto_tree *parent_tree, tvbuff_t *tvb, int offset)
179 {
180         proto_item *item = NULL;
181         proto_tree *tree = NULL;
182
183         guint8 flags;
184
185         if(parent_tree) {
186                 item = proto_tree_add_item(parent_tree, hf_fcp_taskmgmt, tvb, offset, 1, TRUE);
187                 tree = proto_item_add_subtree(item, ett_fcp_taskmgmt);
188         }
189
190         flags = tvb_get_guint8 (tvb, offset);
191
192         if (!flags)
193                 proto_item_append_text(item, " (No values set)");
194
195         proto_tree_add_boolean(tree, hf_fcp_mgmt_flags_obsolete, tvb, offset, 1, flags);
196         if (flags&0x80){
197                 proto_item_append_text(item, "  OBSOLETE");
198                 if(check_col(pinfo->cinfo, COL_INFO)){
199                         col_prepend_fence_fstr(pinfo->cinfo, COL_INFO, "[FCP OBSOLETE] ");
200                 }
201         }
202         flags&=(~( 0x80 ));
203
204         proto_tree_add_boolean(tree, hf_fcp_mgmt_flags_clear_aca, tvb, offset, 1, flags);
205         if (flags&0x40){
206                 proto_item_append_text(item, "  CLEAR ACA");
207                 if(check_col(pinfo->cinfo, COL_INFO)){
208                         col_prepend_fence_fstr(pinfo->cinfo, COL_INFO, "[FCP CLEAR_ACA] ");
209                 }
210         }
211         flags&=(~( 0x40 ));
212
213         proto_tree_add_boolean(tree, hf_fcp_mgmt_flags_target_reset, tvb, offset, 1, flags);
214         if (flags&0x20){
215                 proto_item_append_text(item, "  TARGET RESET");
216                 if(check_col(pinfo->cinfo, COL_INFO)){
217                         col_prepend_fence_fstr(pinfo->cinfo, COL_INFO, "[FCP TARGET_RESET] ");
218                 }
219         }
220         flags&=(~( 0x20 ));
221
222         proto_tree_add_boolean(tree, hf_fcp_mgmt_flags_lu_reset, tvb, offset, 1, flags);
223         if (flags&0x10){
224                 proto_item_append_text(item, "  LU RESET");
225                 if(check_col(pinfo->cinfo, COL_INFO)){
226                         col_prepend_fence_fstr(pinfo->cinfo, COL_INFO, "[FCP LU_RESET] ");
227                 }
228         }
229         flags&=(~( 0x10 ));
230
231         proto_tree_add_boolean(tree, hf_fcp_mgmt_flags_rsvd, tvb, offset, 1, flags);
232         if (flags&0x08){
233                 proto_item_append_text(item, "  RSVD");
234                 if(check_col(pinfo->cinfo, COL_INFO)){
235                         col_prepend_fence_fstr(pinfo->cinfo, COL_INFO, "[FCP RSVD] ");
236                 }
237         }
238         flags&=(~( 0x08 ));
239
240         proto_tree_add_boolean(tree, hf_fcp_mgmt_flags_clear_task_set, tvb, offset, 1, flags);
241         if (flags&0x04){
242                 proto_item_append_text(item, "  CLEAR TASK SET");
243                 if(check_col(pinfo->cinfo, COL_INFO)){
244                         col_prepend_fence_fstr(pinfo->cinfo, COL_INFO, "[FCP CLEAR_TASK_SET] ");
245                 }
246         }
247         flags&=(~( 0x04 ));
248
249         proto_tree_add_boolean(tree, hf_fcp_mgmt_flags_abort_task_set, tvb, offset, 1, flags);
250         if (flags&0x02){
251                 proto_item_append_text(item, "  ABORT TASK SET");
252                 if(check_col(pinfo->cinfo, COL_INFO)){
253                         col_prepend_fence_fstr(pinfo->cinfo, COL_INFO, "[FCP ABORT_TASK_SET] ");
254                 }
255         }
256         flags&=(~( 0x02 ));
257
258         if(flags){
259                 proto_item_append_text(item, " Unknown bitmap value 0x%x", flags);
260         }
261 }
262
263 static const true_false_string fcp_rsp_flags_bidi_tfs = {
264    "Bidirectional residual fields are PRESENT",
265    "Bidirectional residual fields are NOT present",
266 };
267 static const true_false_string fcp_rsp_flags_bidi_rru_tfs = {
268    "Bidirectional residual underflow is PRESENT",
269    "Bidirectional residual underflow is NOT present",
270 };
271 static const true_false_string fcp_rsp_flags_bidi_rro_tfs = {
272    "Bidirectional residual overflow is PRESENT",
273    "Bidirectional residual overflow is NOT present",
274 };
275 static const true_false_string fcp_rsp_flags_conf_req_tfs = {
276    "CONF REQ is SET",
277    "Conf req set is NOT set",
278 };
279 static const true_false_string fcp_rsp_flags_resid_under_tfs = {
280    "RESID UNDER is SET",
281    "Resid under is NOT set",
282 };
283 static const true_false_string fcp_rsp_flags_resid_over_tfs = {
284    "RESID OVER is SET",
285    "Resid over is NOT set",
286 };
287 static const true_false_string fcp_rsp_flags_sns_vld_tfs = {
288    "SNS VLD is SET",
289    "Sns vld is NOT set",
290 };
291 static const true_false_string fcp_rsp_flags_res_vld_tfs = {
292    "RES VLD is SET",
293    "Res vld is NOT set",
294 };
295
296 static void
297 dissect_rsp_flags(proto_tree *parent_tree, tvbuff_t *tvb, int offset)
298 {
299         proto_item *item = NULL;
300         proto_tree *tree = NULL;
301         gboolean bidi_resid_present=FALSE;
302         guint8 flags;
303
304         if(parent_tree) {
305                 item = proto_tree_add_item(parent_tree, hf_fcp_rspflags, tvb, offset, 1, TRUE);
306                 tree = proto_item_add_subtree(item, ett_fcp_rsp_flags);
307         }
308
309         flags = tvb_get_guint8 (tvb, offset);
310
311         if (!flags)
312                 proto_item_append_text(item, " (No values set)");
313
314         /* BIDI RSP */
315         proto_tree_add_boolean(tree, hf_fcp_rsp_flags_bidi, tvb, offset, 1, flags);
316         if (flags&0x80){
317                 bidi_resid_present=TRUE;
318                 proto_item_append_text(item, " BIDI_RSP");
319                 if (flags & (~( 0x80 )))
320                         proto_item_append_text(item, ",");
321         }
322         flags&=(~( 0x80 ));
323
324         /* these two bits are only defined if the bidi bit is set */
325         if(bidi_resid_present){
326                 /* BIDI READ RESID UNDER */
327                 proto_tree_add_boolean(tree, hf_fcp_rsp_flags_bidi_rru, tvb, offset, 1, flags);
328                 if (flags&0x40){
329                         proto_item_append_text(item, " BIDI_RRU");
330                         if (flags & (~( 0x40 )))
331                                 proto_item_append_text(item, ",");
332                 }
333                 flags&=(~( 0x40 ));
334
335                 /* BIDI READ RESID OVER */
336                 proto_tree_add_boolean(tree, hf_fcp_rsp_flags_bidi_rro, tvb, offset, 1, flags);
337                 if (flags&0x20){
338                         proto_item_append_text(item, " BIDI_RRO");
339                         if (flags & (~( 0x20 )))
340                                 proto_item_append_text(item, ",");
341                 }
342                 flags&=(~( 0x20 ));
343         }
344
345         /* Conf Req */
346         proto_tree_add_boolean(tree, hf_fcp_rsp_flags_conf_req, tvb, offset, 1, flags);
347         if (flags&0x10){
348                 proto_item_append_text(item, " CONF REQ");
349                 if (flags & (~( 0x10 )))
350                         proto_item_append_text(item, ",");
351         }
352         flags&=(~( 0x10 ));
353
354         /* Resid Under */
355         proto_tree_add_boolean(tree, hf_fcp_rsp_flags_resid_under, tvb, offset, 1, flags);
356         if (flags&0x08){
357                 proto_item_append_text(item, " RESID UNDER");
358                 if (flags & (~( 0x08 )))
359                         proto_item_append_text(item, ",");
360         }
361         flags&=(~( 0x08 ));
362
363         /* Resid Over */
364         proto_tree_add_boolean(tree, hf_fcp_rsp_flags_resid_over, tvb, offset, 1, flags);
365         if (flags&0x04){
366                 proto_item_append_text(item, " RESID OVER");
367                 if (flags & (~( 0x04 )))
368                         proto_item_append_text(item, ",");
369         }
370         flags&=(~( 0x04 ));
371
372         /* SNS len valid */
373         proto_tree_add_boolean(tree, hf_fcp_rsp_flags_sns_vld, tvb, offset, 1, flags);
374         if (flags&0x02){
375                 proto_item_append_text(item, " SNS VLD");
376                 if (flags & (~( 0x02 )))
377                         proto_item_append_text(item, ",");
378         }
379         flags&=(~( 0x02 ));
380
381         /* rsp len valid */
382         proto_tree_add_boolean(tree, hf_fcp_rsp_flags_res_vld, tvb, offset, 1, flags);
383         if (flags&0x01){
384                 proto_item_append_text(item, " RES VLD");
385                 if (flags & (~( 0x01 )))
386                         proto_item_append_text(item, ",");
387         }
388         flags&=(~( 0x01 ));
389
390         if(flags){
391                 proto_item_append_text(item, " Unknown bitmap value 0x%x", flags);
392         }
393 }
394
395 static void
396 dissect_fcp_cmnd(tvbuff_t *tvb, packet_info *pinfo, proto_tree *parent_tree, proto_tree *tree, conversation_t *conversation, fc_hdr *fchdr, fcp_conv_data_t *fcp_conv_data)
397 {
398     int offset = 0;
399     int len,
400         add_len = 0;
401     guint8 flags, rwflags, lun0;
402         guint16 lun=0xffff;
403     tvbuff_t *cdb_tvb;
404     int tvb_len, tvb_rlen;
405     itl_nexus_t *itl=NULL;
406
407     /* Determine the length of the FCP part of the packet */
408     flags = tvb_get_guint8 (tvb, offset+10);
409     if (flags) {
410         add_len = tvb_get_guint8 (tvb, offset+11) & 0x7C;
411         add_len = add_len >> 2;
412
413         len = FCP_DEF_CMND_LEN + add_len;
414     }
415     else {
416         len = FCP_DEF_CMND_LEN;
417     }
418
419     proto_tree_add_uint_hidden(tree, hf_fcp_type, tvb, offset, 0, 0);
420
421     lun0 = tvb_get_guint8 (tvb, offset);
422
423     /* Display single-level LUNs in decimal for clarity */
424     /* I'm taking a shortcut here by assuming that if the first byte of the
425      * LUN field is 0, it is a single-level LUN. This is not true. For a
426      * real single-level LUN, all 8 bytes except byte 1 must be 0.
427      */
428     if (lun0) {
429       proto_tree_add_item(tree, hf_fcp_multilun, tvb, offset, 8, 0);
430       lun=tvb_get_guint8(tvb, offset)&0x3f;
431       lun<<=8;
432       lun|=tvb_get_guint8(tvb, offset+1);
433     } else {
434       proto_tree_add_item(tree, hf_fcp_singlelun, tvb, offset+1,
435                            1, 0);
436       lun=tvb_get_guint8(tvb, offset+1);
437     }
438
439     if (fchdr->itlq)
440         fchdr->itlq->lun=lun;
441
442     itl=(itl_nexus_t *)se_tree_lookup32(fcp_conv_data->luns, lun);
443     if(!itl){
444         itl=se_alloc(sizeof(itl_nexus_t));
445         itl->cmdset=0xff;
446         itl->conversation=conversation;
447         se_tree_insert32(fcp_conv_data->luns, lun, itl);
448     }
449
450     proto_tree_add_item(tree, hf_fcp_crn, tvb, offset+8, 1, 0);
451     proto_tree_add_item(tree, hf_fcp_taskattr, tvb, offset+9, 1, 0);
452     dissect_task_mgmt_flags(pinfo, tree, tvb, offset+10);
453     proto_tree_add_item(tree, hf_fcp_addlcdblen, tvb, offset+11, 1, 0);
454     rwflags=tvb_get_guint8(tvb, offset+11);
455     if(fchdr->itlq){
456         if(rwflags&0x02){
457             fchdr->itlq->task_flags|=SCSI_DATA_READ;
458         }
459         if(rwflags&0x01){
460             fchdr->itlq->task_flags|=SCSI_DATA_WRITE;
461         }
462     }
463     proto_tree_add_item(tree, hf_fcp_rddata, tvb, offset+11, 1, 0);
464     proto_tree_add_item(tree, hf_fcp_wrdata, tvb, offset+11, 1, 0);
465
466     tvb_len=tvb_length_remaining(tvb, offset+12);
467     if(tvb_len>(16+add_len))
468       tvb_len=16+add_len;
469     tvb_rlen=tvb_reported_length_remaining(tvb, offset+12);
470     if(tvb_rlen>(16+add_len))
471       tvb_rlen=16+add_len;
472     cdb_tvb=tvb_new_subset(tvb, offset+12, tvb_len, tvb_rlen);
473     dissect_scsi_cdb(cdb_tvb, pinfo, parent_tree, SCSI_DEV_UNKNOWN, fchdr->itlq, itl);
474
475     proto_tree_add_item(tree, hf_fcp_dl, tvb, offset+12+16+add_len,
476                          4, 0);
477     if(fchdr->itlq){
478         fchdr->itlq->data_length=tvb_get_ntohl(tvb, offset+12+16+add_len);
479     }
480
481     if( ((rwflags&0x03)==0x03)
482     &&  tvb_length_remaining(tvb, offset+12+16+add_len+4)>=4){
483         proto_tree_add_item(tree, hf_fcp_bidir_dl, tvb, offset+12+16+add_len+4,
484                          4, 0);
485         if(fchdr->itlq){
486             fchdr->itlq->bidir_data_length=tvb_get_ntohl(tvb, offset+12+16+add_len+4);
487         }
488
489     }
490
491 }
492
493 static void
494 dissect_fcp_data(tvbuff_t *tvb, packet_info *pinfo, proto_tree *parent_tree, conversation_t *conversation, fc_hdr *fchdr, itl_nexus_t *itl)
495 {
496     dissect_scsi_payload(tvb, pinfo, parent_tree, FALSE, fchdr->itlq, itl, fchdr->relative_offset);
497 }
498
499 /* fcp-3  9.5 table 24 */
500 static void
501 dissect_fcp_rspinfo(tvbuff_t *tvb, proto_tree *tree, int offset)
502 {
503         /* 2 reserved bytes */
504         offset+=2;
505
506         /* rsp code */
507         proto_tree_add_item(tree, hf_fcp_rspcode, tvb, offset, 1, 0);
508         offset++;
509
510         /* 4 reserved bytes */
511         offset+=4;
512 }
513
514 static void
515 dissect_fcp_rsp(tvbuff_t *tvb, packet_info *pinfo, proto_tree *parent_tree, proto_tree *tree, conversation_t *conversation, fc_hdr *fchdr, itl_nexus_t *itl)
516 {
517     guint32 offset = 0;
518     gint32 snslen = 0,
519            rsplen = 0;
520     guint8 flags;
521     guint8 status;
522
523     status = tvb_get_guint8 (tvb, offset+11);
524
525     if (check_col (pinfo->cinfo, COL_INFO)) {
526         col_append_fstr (pinfo->cinfo, COL_INFO, ":%s",
527                          val_to_str (status, scsi_status_val, "0x%x"));
528     }
529
530     proto_tree_add_uint_hidden(tree, hf_fcp_type, tvb, offset, 0, 0);
531
532
533         /* 8 reserved bytes */
534         offset+=8;
535
536         /* retry delay timer */
537         proto_tree_add_item(tree, hf_fcp_retry_delay_timer, tvb, offset, 2, 0);
538         offset+=2;
539
540         /* flags */
541         flags = tvb_get_guint8 (tvb, offset);
542         dissect_rsp_flags(tree, tvb, offset);
543         offset++;
544
545         /* scsi status code */
546         proto_tree_add_item(tree, hf_fcp_scsistatus, tvb, offset, 1, 0);
547         dissect_scsi_rsp(tvb, pinfo, parent_tree, fchdr->itlq, itl, tvb_get_guint8(tvb, offset));
548         offset++;
549
550         /* residual count */
551         if(flags & 0x0e){
552             proto_tree_add_item(tree, hf_fcp_resid, tvb, offset, 4, 0);
553         }
554         offset+=4;
555
556         /* sense length */
557         if (flags & 0x2) {
558             snslen=tvb_get_ntohl(tvb, offset);
559             proto_tree_add_uint(tree, hf_fcp_snslen, tvb, offset, 4,
560                                  snslen);
561         }
562         offset+=4;
563
564         /* response length */
565         if (flags & 0x1) {
566             rsplen=tvb_get_ntohl(tvb, offset);
567             proto_tree_add_uint(tree, hf_fcp_rsplen, tvb, offset, 4,
568                                  rsplen);
569         }
570         offset+=4;
571
572         /* rsp_info */
573         if(rsplen){
574             tvbuff_t *rspinfo_tvb;
575
576             rspinfo_tvb=tvb_new_subset(tvb, offset, MIN(rsplen, tvb_length_remaining(tvb, offset)), rsplen);
577             dissect_fcp_rspinfo(tvb, tree, 0);
578
579             offset+=rsplen;
580         }
581
582         /* sense info */
583         if(snslen){
584             tvbuff_t *sns_tvb;
585
586             sns_tvb=tvb_new_subset(tvb, offset, MIN(snslen, tvb_length_remaining(tvb, offset)), snslen);
587             dissect_scsi_snsinfo (sns_tvb, pinfo, parent_tree, 0,
588                                   snslen,
589                                   fchdr->itlq, itl);
590
591             offset+=snslen;
592         }
593
594         /* bidir read resid (only present for bidirectional responses) */
595         if(flags&0x80){
596             if(flags&0x60){
597                 proto_tree_add_item(tree, hf_fcp_bidir_resid, tvb, offset, 4, 0);
598             }
599             offset+=4;
600         }
601 }
602
603 static void
604 dissect_fcp_xfer_rdy(tvbuff_t *tvb, proto_tree *tree)
605 {
606     int offset = 0;
607
608
609     proto_tree_add_uint_hidden(tree, hf_fcp_type, tvb, offset, 0, 0);
610
611     proto_tree_add_item(tree, hf_fcp_data_ro, tvb, offset, 4, 0);
612     proto_tree_add_item(tree, hf_fcp_burstlen, tvb, offset+4, 4, 0);
613 }
614
615 static void
616 dissect_fcp(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
617 {
618     proto_item *ti=NULL;
619     proto_tree *fcp_tree = NULL;
620     fc_hdr *fchdr;
621     guint8 r_ctl;
622     fcp_conv_data_t *fcp_conv_data=NULL;
623     itl_nexus_t *itl=NULL;
624
625     fchdr=(fc_hdr *)pinfo->private_data;
626
627     /* Make entries in Protocol column and Info column on summary display */
628     if (check_col(pinfo->cinfo, COL_PROTOCOL))
629         col_set_str(pinfo->cinfo, COL_PROTOCOL, "FCP");
630
631     r_ctl = pinfo->r_ctl;
632
633     r_ctl &= 0xF;
634
635     if (check_col (pinfo->cinfo, COL_INFO)) {
636         col_set_str (pinfo->cinfo, COL_INFO, val_to_str (r_ctl, fcp_iu_val,
637                                                       "0x%x"));
638     }
639
640
641     if (tree) {
642         ti = proto_tree_add_protocol_format(tree, proto_fcp, tvb, 0, -1,
643                                              "FCP: %s", val_to_str(r_ctl, fcp_iu_val, "Unknown 0x%02x"));
644         fcp_tree = proto_item_add_subtree(ti, ett_fcp);
645     }
646
647
648     fcp_conv_data=conversation_get_proto_data(fchdr->conversation, proto_fcp);
649     if(!fcp_conv_data){
650         fcp_conv_data=se_alloc(sizeof(fcp_conv_data_t));
651         fcp_conv_data->luns=se_tree_create_non_persistent(EMEM_TREE_TYPE_RED_BLACK, "FCP Luns");
652         conversation_add_proto_data(fchdr->conversation, proto_fcp, fcp_conv_data);
653     }
654
655     if(r_ctl!=FCP_IU_CMD){
656         itl=(itl_nexus_t *)se_tree_lookup32(fcp_conv_data->luns, fchdr->itlq->lun);
657     }
658
659     /* put a request_in in all frames except the command frame */
660     if((r_ctl!=FCP_IU_CMD)&&(fchdr->itlq->first_exchange_frame)){
661         proto_item *it;
662         it=proto_tree_add_uint(fcp_tree, hf_fcp_singlelun, tvb, 0, 0, fchdr->itlq->lun);
663         PROTO_ITEM_SET_GENERATED(it);
664         it=proto_tree_add_uint(fcp_tree, hf_fcp_request_in, tvb, 0, 0, fchdr->itlq->first_exchange_frame);
665         PROTO_ITEM_SET_GENERATED(it);
666         /* only put the response time in the actual response frame */
667         if(r_ctl==FCP_IU_RSP){
668             nstime_t delta_ts;
669             nstime_delta(&delta_ts, &pinfo->fd->abs_ts, &fchdr->itlq->fc_time);
670             it=proto_tree_add_time(ti, hf_fcp_time, tvb, 0, 0, &delta_ts);
671             PROTO_ITEM_SET_GENERATED(it);
672         }
673     }
674     /* put a response_in in all frames except the response frame */
675     if((r_ctl!=FCP_IU_RSP)&&(fchdr->itlq->last_exchange_frame)){
676         proto_item *it;
677         it=proto_tree_add_uint(fcp_tree, hf_fcp_response_in, tvb, 0, 0, fchdr->itlq->last_exchange_frame);
678         PROTO_ITEM_SET_GENERATED(it);
679     }
680
681     switch (r_ctl) {
682     case FCP_IU_DATA:
683         dissect_fcp_data(tvb, pinfo, tree, fchdr->conversation, fchdr, itl);
684         break;
685     case FCP_IU_CONFIRM:
686         /* Nothing to be done here */
687         break;
688     case FCP_IU_XFER_RDY:
689         dissect_fcp_xfer_rdy(tvb, fcp_tree);
690         break;
691     case FCP_IU_CMD:
692         dissect_fcp_cmnd(tvb, pinfo, tree, fcp_tree, fchdr->conversation, fchdr, fcp_conv_data);
693         break;
694     case FCP_IU_RSP:
695         dissect_fcp_rsp(tvb, pinfo, tree, fcp_tree, fchdr->conversation, fchdr, itl);
696         break;
697     default:
698         call_dissector(data_handle, tvb, pinfo, tree);
699         break;
700     }
701 /*xxx once the subdissectors return bytes consumed:  proto_item_set_end(ti, tvb, offset);*/
702
703 }
704
705 /* Register the protocol with Wireshark */
706
707 /* this format is require because a script is used to build the C function
708    that calls all the protocol registration.
709 */
710
711 void
712 proto_register_fcp (void)
713 {
714
715     /* Setup list of header fields  See Section 1.6.1 for details*/
716     static hf_register_info hf[] = {
717         { &hf_fcp_type,
718           {"Field to branch off to SCSI", "fcp.type", FT_UINT8, BASE_HEX, NULL,
719            0x0, "", HFILL}},
720         {&hf_fcp_multilun,
721          {"Multi-Level LUN", "fcp.multilun", FT_BYTES, BASE_HEX, NULL, 0x0,
722           "", HFILL}},
723         { &hf_fcp_singlelun,
724           {"LUN", "fcp.lun", FT_UINT8, BASE_HEX, NULL, 0x0, "", HFILL}},
725         { &hf_fcp_crn,
726           {"Command Ref Num", "fcp.crn", FT_UINT8, BASE_DEC, NULL, 0x0, "",
727            HFILL}},
728         { &hf_fcp_taskattr,
729           {"Task Attribute", "fcp.taskattr", FT_UINT8, BASE_HEX,
730            VALS (fcp_task_attr_val), 0x7, "", HFILL}},
731         { &hf_fcp_taskmgmt,
732           {"Task Management Flags", "fcp.taskmgmt", FT_UINT8, BASE_HEX, NULL,
733            0x0, "", HFILL}},
734         { &hf_fcp_addlcdblen,
735           {"Additional CDB Length", "fcp.addlcdblen", FT_UINT8, BASE_DEC, NULL,
736            0xFC, "", HFILL}},
737         { &hf_fcp_rddata,
738           {"RDDATA", "fcp.rddata", FT_BOOLEAN, 8, NULL, 0x02, "", HFILL}},
739         { &hf_fcp_wrdata,
740           {"WRDATA", "fcp.wrdata", FT_BOOLEAN, 8, NULL, 0x01, "", HFILL}},
741         { &hf_fcp_dl,
742           {"FCP_DL", "fcp.dl", FT_UINT32, BASE_DEC, NULL, 0x0, "", HFILL}},
743         { &hf_fcp_bidir_dl,
744           {"FCP_BIDIRECTIONAL_READ_DL", "fcp.bidir_dl", FT_UINT32, BASE_DEC, NULL, 0x0, "", HFILL}},
745         { &hf_fcp_data_ro,
746           {"FCP_DATA_RO", "fcp.data_ro", FT_UINT32, BASE_DEC, NULL, 0x0, "",
747            HFILL}},
748         { &hf_fcp_burstlen,
749           {"Burst Length", "fcp.burstlen", FT_UINT32, BASE_DEC, NULL, 0x0, "",
750            HFILL}},
751         { &hf_fcp_retry_delay_timer,
752           {"Retry Delay Timer", "fcp.rsp.retry_delay_timer", FT_UINT16, BASE_DEC, NULL, 0x0, "",
753            HFILL}},
754         { &hf_fcp_rspflags,
755           {"FCP_RSP Flags", "fcp.rspflags", FT_UINT8, BASE_HEX, NULL, 0x0, "",
756            HFILL}},
757         { &hf_fcp_resid,
758           {"FCP_RESID", "fcp.resid", FT_UINT32, BASE_DEC, NULL, 0x0, "",
759            HFILL}},
760         { &hf_fcp_bidir_resid,
761           {"Bidirectional Read Resid", "fcp.bidir_resid", FT_UINT32, BASE_DEC, NULL, 0x0, "",
762            HFILL}},
763         { &hf_fcp_snslen,
764           {"FCP_SNS_LEN", "fcp.snslen", FT_UINT32, BASE_DEC, NULL, 0x0, "",
765            HFILL}},
766         { &hf_fcp_rsplen,
767           {"FCP_RSP_LEN", "fcp.rsplen", FT_UINT32, BASE_DEC, NULL, 0x0, "",
768            HFILL}},
769         { &hf_fcp_rspcode,
770           {"RSP_CODE", "fcp.rspcode", FT_UINT8, BASE_HEX,
771            VALS (fcp_rsp_code_val), 0x0, "", HFILL}},
772         { &hf_fcp_scsistatus,
773           {"SCSI Status", "fcp.status", FT_UINT8, BASE_HEX,
774            VALS (scsi_status_val), 0x0, "", HFILL}},
775         { &hf_fcp_mgmt_flags_obsolete,
776           { "Obsolete", "fcp.mgmt.flags.obsolete", FT_BOOLEAN, 8, TFS(&fcp_mgmt_flags_obsolete_tfs), 0x80, "", HFILL }},
777         { &hf_fcp_mgmt_flags_clear_aca,
778           { "Clear ACA", "fcp.mgmt.flags.clear_aca", FT_BOOLEAN, 8, TFS(&fcp_mgmt_flags_clear_aca_tfs), 0x40, "", HFILL }},
779         { &hf_fcp_mgmt_flags_target_reset,
780           { "Target Reset", "fcp.mgmt.flags.target_reset", FT_BOOLEAN, 8, TFS(&fcp_mgmt_flags_target_reset_tfs), 0x20, "", HFILL }},
781         { &hf_fcp_mgmt_flags_lu_reset,
782           { "LU Reset", "fcp.mgmt.flags.lu_reset", FT_BOOLEAN, 8, TFS(&fcp_mgmt_flags_lu_reset_tfs), 0x10, "", HFILL }},
783         { &hf_fcp_mgmt_flags_rsvd,
784           { "Rsvd", "fcp.mgmt.flags.rsvd", FT_BOOLEAN, 8, TFS(&fcp_mgmt_flags_rsvd_tfs), 0x08, "", HFILL }},
785         { &hf_fcp_mgmt_flags_clear_task_set,
786           { "Clear Task Set", "fcp.mgmt.flags.clear_task_set", FT_BOOLEAN, 8, TFS(&fcp_mgmt_flags_clear_task_set_tfs), 0x04, "", HFILL }},
787         { &hf_fcp_mgmt_flags_abort_task_set,
788           { "Abort Task Set", "fcp.mgmt.flags.abort_task_set", FT_BOOLEAN, 8, TFS(&fcp_mgmt_flags_abort_task_set_tfs), 0x02, "", HFILL }},
789         { &hf_fcp_rsp_flags_bidi,
790           { "Bidi Rsp", "fcp.rsp.flags.bidi", FT_BOOLEAN, 8, TFS(&fcp_rsp_flags_bidi_tfs), 0x80, "", HFILL }},
791         { &hf_fcp_rsp_flags_bidi_rru,
792           { "Bidi Read Resid Under", "fcp.rsp.flags.bidi_rru", FT_BOOLEAN, 8, TFS(&fcp_rsp_flags_bidi_rru_tfs), 0x40, "", HFILL }},
793         { &hf_fcp_rsp_flags_bidi_rro,
794           { "Bidi Read Resid Over", "fcp.rsp.flags.bidi_rro", FT_BOOLEAN, 8, TFS(&fcp_rsp_flags_bidi_rro_tfs), 0x20, "", HFILL }},
795         { &hf_fcp_rsp_flags_conf_req,
796           { "Conf Req", "fcp.rsp.flags.conf_req", FT_BOOLEAN, 8, TFS(&fcp_rsp_flags_conf_req_tfs), 0x10, "", HFILL }},
797         { &hf_fcp_rsp_flags_resid_under,
798           { "Resid Under", "fcp.rsp.flags.resid_under", FT_BOOLEAN, 8, TFS(&fcp_rsp_flags_resid_under_tfs), 0x08, "", HFILL }},
799         { &hf_fcp_rsp_flags_resid_over,
800           { "Resid Over", "fcp.rsp.flags.resid_over", FT_BOOLEAN, 8, TFS(&fcp_rsp_flags_resid_over_tfs), 0x04, "", HFILL }},
801         { &hf_fcp_rsp_flags_sns_vld,
802           { "SNS Vld", "fcp.rsp.flags.sns_vld", FT_BOOLEAN, 8, TFS(&fcp_rsp_flags_sns_vld_tfs), 0x02, "", HFILL }},
803         { &hf_fcp_rsp_flags_res_vld,
804           { "RES Vld", "fcp.rsp.flags.res_vld", FT_BOOLEAN, 8, TFS(&fcp_rsp_flags_res_vld_tfs), 0x01, "", HFILL }},
805         { &hf_fcp_request_in,
806           { "Request In", "fcp.request_in", FT_FRAMENUM, BASE_NONE, NULL,
807            0, "The frame number for the request", HFILL }},
808         { &hf_fcp_response_in,
809           { "Response In", "fcp.response_in", FT_FRAMENUM, BASE_NONE, NULL,
810            0, "The frame number of the response", HFILL }},
811         { &hf_fcp_time,
812           { "Time from FCP_CMND", "fcp.time", FT_RELATIVE_TIME, BASE_NONE, NULL,
813            0, "Time since the FCP_CMND frame", HFILL }},
814     };
815
816     /* Setup protocol subtree array */
817     static gint *ett[] = {
818         &ett_fcp,
819         &ett_fcp_taskmgmt,
820         &ett_fcp_rsp_flags,
821     };
822
823     /* Register the protocol name and description */
824     proto_fcp = proto_register_protocol("Fibre Channel Protocol for SCSI",
825                                         "FCP", "fcp");
826
827     /* Required function calls to register the header fields and subtrees used */
828     proto_register_field_array(proto_fcp, hf, array_length(hf));
829     proto_register_subtree_array(ett, array_length(ett));
830     fcp_dissector = register_dissector_table ("fcp.type", "FCP Type", FT_UINT8,
831                                               BASE_HEX);
832 }
833
834 /* If this dissector uses sub-dissector registration add a registration routine.
835    This format is required because a script is used to find these routines and
836    create the code that calls these routines.
837 */
838 void
839 proto_reg_handoff_fcp (void)
840 {
841     dissector_handle_t fcp_handle;
842
843     fcp_handle = create_dissector_handle (dissect_fcp, proto_fcp);
844     dissector_add("fc.ftype", FC_FTYPE_SCSI, fcp_handle);
845
846     data_handle = find_dissector ("data");
847 }