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