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