2 * Routines for Fibre Channel Protocol for SCSI (FCP)
3 * Copyright 2001, Dinesh G Dutt <ddutt@cisco.com>
7 * Ethereal - Network traffic analyzer
8 * By Gerald Combs <gerald@ethereal.com>
9 * Copyright 1998 Gerald Combs
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.
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.
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.
34 #ifdef HAVE_SYS_TYPES_H
35 # include <sys/types.h>
38 #ifdef HAVE_NETINET_IN_H
39 # include <netinet/in.h>
44 #ifdef NEED_SNPRINTF_H
45 # include "snprintf.h"
48 #include <epan/prefs.h>
49 #include <epan/packet.h>
50 #include <epan/conversation.h>
52 #include "packet-fc.h"
53 #include "packet-fcp.h"
54 #include "packet-scsi.h"
56 /* Initialize the protocol and registered fields */
57 static int proto_fcp = -1;
58 static int hf_fcp_multilun = -1;
59 static int hf_fcp_singlelun = -1;
60 static int hf_fcp_crn = -1;
61 static int hf_fcp_taskattr = -1;
62 static int hf_fcp_taskmgmt = -1;
63 static int hf_fcp_addlcdblen = -1;
64 static int hf_fcp_rddata = -1;
65 static int hf_fcp_wrdata = -1;
66 static int hf_fcp_dl = -1;
67 static int hf_fcp_data_ro = -1;
68 static int hf_fcp_burstlen = -1;
69 static int hf_fcp_rspflags = -1;
70 static int hf_fcp_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;
78 /* Initialize the subtree pointers */
79 static gint ett_fcp = -1;
80 static dissector_table_t fcp_dissector;
81 static dissector_handle_t data_handle;
83 typedef struct _fcp_conv_key {
87 typedef struct _fcp_conv_data {
94 GHashTable *fcp_req_hash = NULL;
95 GMemChunk *fcp_req_keys = NULL;
96 GMemChunk *fcp_req_vals = NULL;
97 guint32 fcp_init_count = 25;
103 fcp_equal(gconstpointer v, gconstpointer w)
105 const fcp_conv_key_t *v1 = v;
106 const fcp_conv_key_t *v2 = w;
108 return (v1->conv_idx == v2->conv_idx);
112 fcp_hash (gconstpointer v)
114 const fcp_conv_key_t *key = v;
123 * Protocol initialization
126 fcp_init_protocol(void)
129 g_mem_chunk_destroy(fcp_req_keys);
131 g_mem_chunk_destroy(fcp_req_vals);
133 g_hash_table_destroy(fcp_req_hash);
135 fcp_req_hash = g_hash_table_new(fcp_hash, fcp_equal);
136 fcp_req_keys = g_mem_chunk_new("fcp_req_keys",
137 sizeof(fcp_conv_key_t),
138 fcp_init_count * sizeof(fcp_conv_key_t),
140 fcp_req_vals = g_mem_chunk_new("fcp_req_vals",
141 sizeof(fcp_conv_data_t),
142 fcp_init_count * sizeof(fcp_conv_data_t),
147 task_mgmt_flags_to_str (guint8 flags, gchar *str)
157 strcpy (str, "Obsolete, ");
162 strcpy (&str[stroff], "Clear ACA, ");
167 strcpy (&str[stroff], "Target Reset, ");
172 strcpy (&str[stroff], "LU Reset, ");
177 strcpy (&str[stroff], "Rsvd, ");
182 strcpy (&str[stroff], "Clear Task Set, ");
187 strcpy (&str[stroff], "Abort Task Set");
195 rspflags_to_str (guint8 flags, gchar *str)
205 strcpy (str, "FCP_CONF_REQ | ");
209 strcpy (&str[stroff], "FCP_RESID_UNDER | ");
213 strcpy (&str[stroff], "FCP_RESID_OVER | ");
217 strcpy (&str[stroff], "FCP_SNS_LEN_VLD | ");
221 strcpy (&str[stroff], "FCP_RSP_LEN_VLD | ");
227 /* Code to actually dissect the packets */
229 dissect_fcp_cmnd (tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
237 proto_tree *fcp_tree = NULL;
238 conversation_t *conversation;
239 fcp_conv_data_t *cdata;
240 fcp_conv_key_t ckey, *req_key;
241 scsi_task_id_t task_key;
244 int tvb_len, tvb_rlen;
246 /* Determine the length of the FCP part of the packet */
247 flags = tvb_get_guint8 (tvb, offset+10);
249 add_len = tvb_get_guint8 (tvb, offset+11) & 0x7C;
250 add_len = add_len >> 2;
252 len = FCP_DEF_CMND_LEN + add_len;
255 len = FCP_DEF_CMND_LEN;
258 /* We track the conversation to determine how many bytes is required */
259 /* by the data that is sent back or sent next by the initiator as part */
260 /* of this command. The state is destroyed in the response dissector */
262 conversation = find_conversation (pinfo->fd->num, &pinfo->src, &pinfo->dst,
263 pinfo->ptype, pinfo->oxid,
264 pinfo->rxid, NO_PORT2);
266 conversation = conversation_new (pinfo->fd->num, &pinfo->src, &pinfo->dst,
267 pinfo->ptype, pinfo->oxid,
268 pinfo->rxid, NO_PORT2);
271 ckey.conv_idx = conversation->index;
272 task_key.conv_id = conversation->index;
273 task_key.task_id = conversation->index;
274 pinfo->private_data = (void *)&task_key;
276 cdata = (fcp_conv_data_t *)g_hash_table_lookup (fcp_req_hash,
279 * XXX - the fetch of the fcp_dl value will throw an exception on
280 * a short frame before we get a chance to dissect the stuff before
283 * XXX - this doesn't appear to store the data length with the
284 * FCP packet with the data, so this might not work correctly
285 * if you select a command packet, select the corresponding data
286 * packet, and then select another data packet with a different
290 /* Since we never free the memory used by an exchange, this maybe a
291 * case of another request using the same exchange as a previous
294 cdata->fcp_dl = tvb_get_ntohl (tvb, offset+12+16+add_len);
295 cdata->abs_usecs = pinfo->fd->abs_usecs;
296 cdata->abs_secs = pinfo->fd->abs_secs;
299 req_key = g_mem_chunk_alloc (fcp_req_keys);
300 req_key->conv_idx = conversation->index;
302 cdata = g_mem_chunk_alloc (fcp_req_vals);
303 cdata->fcp_dl = tvb_get_ntohl (tvb, offset+12+16+add_len);
304 cdata->abs_usecs = pinfo->fd->abs_usecs;
305 cdata->abs_secs = pinfo->fd->abs_secs;
307 g_hash_table_insert (fcp_req_hash, req_key, cdata);
310 /* XXX this one is redundant right? ronnie
311 dissect_scsi_cdb (tvb, pinfo, fcp_tree, offset+12, 16+add_len,
312 SCSI_DEV_UNKNOWN, lun);
316 ti = proto_tree_add_protocol_format (tree, proto_fcp, tvb, 0, len,
318 fcp_tree = proto_item_add_subtree (ti, ett_fcp);
320 proto_tree_add_uint_hidden (fcp_tree, hf_fcp_type, tvb, offset, 0, 0);
322 lun0 = tvb_get_guint8 (tvb, offset);
324 /* Display single-level LUNs in decimal for clarity */
325 /* I'm taking a shortcut here by assuming that if the first byte of the
326 * LUN field is 0, it is a single-level LUN. This is not true. For a
327 * real single-level LUN, all 8 bytes except byte 1 must be 0.
331 proto_tree_add_item (fcp_tree, hf_fcp_multilun, tvb, offset, 8, 0);
332 lun=tvb_get_guint8(tvb, offset)&0x3f;
334 lun|=tvb_get_guint8(tvb, offset+1);
337 cdata->fcp_lun = tvb_get_guint8 (tvb, offset+1);
338 proto_tree_add_item (fcp_tree, hf_fcp_singlelun, tvb, offset+1,
340 lun=tvb_get_guint8(tvb, offset+1);
343 proto_tree_add_item (fcp_tree, hf_fcp_crn, tvb, offset+8, 1, 0);
344 proto_tree_add_item (fcp_tree, hf_fcp_taskattr, tvb, offset+9, 1, 0);
345 proto_tree_add_uint_format (fcp_tree, hf_fcp_taskmgmt, tvb, offset+10,
347 "Task Management Flags: 0x%x (%s)",
349 task_mgmt_flags_to_str (flags, str));
350 proto_tree_add_item (fcp_tree, hf_fcp_addlcdblen, tvb, offset+11, 1, 0);
351 proto_tree_add_item (fcp_tree, hf_fcp_rddata, tvb, offset+11, 1, 0);
352 proto_tree_add_item (fcp_tree, hf_fcp_wrdata, tvb, offset+11, 1, 0);
354 tvb_len=tvb_length_remaining(tvb, offset+12);
355 if(tvb_len>(16+add_len))
357 tvb_rlen=tvb_reported_length_remaining(tvb, offset+12);
358 if(tvb_rlen>(16+add_len))
360 cdb_tvb=tvb_new_subset(tvb, offset+12, tvb_len, tvb_rlen);
361 dissect_scsi_cdb (cdb_tvb, pinfo, tree, SCSI_DEV_UNKNOWN, lun);
363 proto_tree_add_item (fcp_tree, hf_fcp_dl, tvb, offset+12+16+add_len,
368 dissect_fcp_data (tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
370 conversation_t *conversation;
371 fcp_conv_data_t *cdata = NULL;
374 proto_tree *fcp_tree;
375 scsi_task_id_t task_key;
377 /* Retrieve conversation state to determine expected payload */
378 conversation = find_conversation (pinfo->fd->num, &pinfo->src, &pinfo->dst,
379 pinfo->ptype, pinfo->oxid,
380 pinfo->rxid, NO_PORT2);
382 ckey.conv_idx = conversation->index;
384 cdata = (fcp_conv_data_t *)g_hash_table_lookup (fcp_req_hash,
386 task_key.conv_id = conversation->index;
387 task_key.task_id = conversation->index;
388 pinfo->private_data = (void *)&task_key;
391 pinfo->private_data = NULL;
394 ti = proto_tree_add_protocol_format (tree, proto_fcp, tvb, 0, 0,
396 fcp_tree = proto_item_add_subtree (ti, ett_fcp);
398 if (cdata->fcp_lun >= 0)
399 proto_tree_add_uint_hidden (fcp_tree, hf_fcp_singlelun, tvb,
400 0, 0, cdata->fcp_lun);
402 dissect_scsi_payload (tvb, pinfo, tree, FALSE, (guint16) cdata->fcp_lun);
405 dissect_scsi_payload (tvb, pinfo, tree, FALSE, 0xffff);
410 dissect_fcp_rsp (tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
419 proto_tree *fcp_tree;
421 conversation_t *conversation;
422 fcp_conv_data_t *cdata = NULL;
424 scsi_task_id_t task_key;
426 status = tvb_get_guint8 (tvb, offset+11);
428 if (check_col (pinfo->cinfo, COL_INFO)) {
429 col_append_fstr (pinfo->cinfo, COL_INFO, " , %s",
430 val_to_str (status, scsi_status_val, "0x%x"));
433 /* Response marks the end of the conversation. So destroy state */
434 conversation = find_conversation (pinfo->fd->num, &pinfo->src, &pinfo->dst,
435 pinfo->ptype, pinfo->oxid,
436 pinfo->rxid, NO_PORT2);
438 ckey.conv_idx = conversation->index;
440 cdata = (fcp_conv_data_t *)g_hash_table_lookup (fcp_req_hash,
442 task_key.conv_id = task_key.task_id = conversation->index;
443 pinfo->private_data = (void *)&task_key;
447 ti = proto_tree_add_protocol_format (tree, proto_fcp, tvb, 0, -1,
449 fcp_tree = proto_item_add_subtree (ti, ett_fcp);
450 proto_tree_add_uint_hidden (fcp_tree, hf_fcp_type, tvb, offset, 0, 0);
453 del_usecs = (pinfo->fd->abs_secs - cdata->abs_secs)* 1000000 +
454 (pinfo->fd->abs_usecs - cdata->abs_usecs);
455 if (del_usecs > 1000)
456 proto_tree_add_text (fcp_tree, tvb, offset, 0,
457 "Cmd Response Time: %d msecs",
460 proto_tree_add_text (fcp_tree, tvb, offset, 0,
461 "Cmd Response Time: %d usecs",
463 if (cdata->fcp_lun >= 0)
464 proto_tree_add_uint_hidden (fcp_tree, hf_fcp_singlelun, tvb,
465 offset, 0, cdata->fcp_lun);
467 flags = tvb_get_guint8 (tvb, offset+10);
468 proto_tree_add_uint_format (fcp_tree, hf_fcp_rspflags, tvb, offset+10,
469 1, flags, "Flags: 0x%02x (%s)", flags,
470 rspflags_to_str (flags, str));
471 proto_tree_add_item (fcp_tree, hf_fcp_scsistatus, tvb, offset+11, 1, 0);
473 proto_tree_add_item (fcp_tree, hf_fcp_resid, tvb, offset+12, 4, 0);
475 snslen = tvb_get_ntohl (tvb, offset+16);
476 proto_tree_add_uint (fcp_tree, hf_fcp_snslen, tvb, offset+16, 4,
480 rsplen = tvb_get_ntohl (tvb, offset+20);
481 proto_tree_add_uint (fcp_tree, hf_fcp_rsplen, tvb, offset+20, 4,
483 /* XXX - must rsplen be >= 4? What other than the code is there? */
484 proto_tree_add_item (fcp_tree, hf_fcp_rspcode, tvb, offset+27, 1,
487 /* This handles too-large rsplen values (including ones > 2^31-1) */
488 tvb_ensure_bytes_exist (tvb, offset+24, rsplen);
491 dissect_scsi_snsinfo (tvb, pinfo, tree, offset,
493 (guint16) (cdata?cdata->fcp_lun:0xffff) );
495 /* This handles too-large snslen values (including ones > 2^31-1) */
496 tvb_ensure_bytes_exist (tvb, offset, snslen);
498 proto_item_set_end (ti, tvb, offset);
501 * XXX - this isn't done if an exception is thrown.
503 g_mem_chunk_free (fcp_req_vals, cdata);
504 g_hash_table_remove (fcp_req_hash, &ckey);
510 dissect_fcp_xfer_rdy (tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
514 proto_tree *fcp_tree;
517 conversation_t *conversation;
518 fcp_conv_data_t *cdata = NULL;
519 fcp_conv_key_t ckey, *req_key;
521 /* Retrieve conversation state to determine expected payload */
522 conversation = find_conversation (pinfo->fd->num, &pinfo->src, &pinfo->dst,
523 pinfo->ptype, pinfo->oxid,
524 pinfo->rxid, NO_PORT2);
526 conversation = conversation_new (pinfo->fd->num, &pinfo->src, &pinfo->dst,
527 pinfo->ptype, pinfo->oxid,
528 pinfo->rxid, NO_PORT2);
532 ckey.conv_idx = conversation->index;
534 cdata = (fcp_conv_data_t *)g_hash_table_lookup (fcp_req_hash,
537 cdata->fcp_dl = tvb_get_ntohl (tvb, offset+4);
540 req_key = g_mem_chunk_alloc (fcp_req_keys);
541 req_key->conv_idx = conversation->index;
543 cdata = g_mem_chunk_alloc (fcp_req_vals);
544 cdata->fcp_dl = tvb_get_ntohl (tvb, offset+4);
547 g_hash_table_insert (fcp_req_hash, req_key, cdata);
552 ti = proto_tree_add_protocol_format (tree, proto_fcp, tvb, 0, 12,
554 fcp_tree = proto_item_add_subtree (ti, ett_fcp);
555 proto_tree_add_uint_hidden (fcp_tree, hf_fcp_type, tvb, offset, 0, 0);
558 del_usecs = (pinfo->fd->abs_secs - cdata->abs_secs)* 1000000 +
559 (pinfo->fd->abs_usecs - cdata->abs_usecs);
560 if (del_usecs > 1000)
561 proto_tree_add_text (fcp_tree, tvb, offset, 0,
562 "Cmd Response Time: %d msecs",
565 proto_tree_add_text (fcp_tree, tvb, offset, 0,
566 "Cmd Response Time: %d usecs",
568 if (cdata->fcp_lun >= 0)
569 proto_tree_add_uint_hidden (fcp_tree, hf_fcp_singlelun, tvb,
570 offset, 0, cdata->fcp_lun);
572 proto_tree_add_item (fcp_tree, hf_fcp_data_ro, tvb, offset, 4, 0);
573 proto_tree_add_item (fcp_tree, hf_fcp_burstlen, tvb, offset+4, 4, 0);
578 dissect_fcp (tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
581 /* Set up structures needed to add the protocol subtree and manage it */
584 /* Make entries in Protocol column and Info column on summary display */
585 if (check_col(pinfo->cinfo, COL_PROTOCOL))
586 col_set_str(pinfo->cinfo, COL_PROTOCOL, "FCP");
588 r_ctl = pinfo->r_ctl;
592 if (check_col (pinfo->cinfo, COL_INFO)) {
593 col_set_str (pinfo->cinfo, COL_INFO, val_to_str (r_ctl, fcp_iu_val,
599 dissect_fcp_data (tvb, pinfo, tree);
602 /* Nothing to be done here */
604 case FCP_IU_XFER_RDY:
605 dissect_fcp_xfer_rdy (tvb, pinfo, tree);
608 dissect_fcp_cmnd (tvb, pinfo, tree);
611 dissect_fcp_rsp (tvb, pinfo, tree);
614 call_dissector (data_handle, tvb, pinfo, tree);
619 /* Register the protocol with Ethereal */
621 /* this format is require because a script is used to build the C function
622 that calls all the protocol registration.
626 proto_register_fcp (void)
629 /* Setup list of header fields See Section 1.6.1 for details*/
630 static hf_register_info hf[] = {
632 {"Field to branch off to SCSI", "fcp.type", FT_UINT8, BASE_HEX, NULL,
635 {"Multi-Level LUN", "fcp.multilun", FT_BYTES, BASE_HEX, NULL, 0x0,
638 {"LUN", "fcp.lun", FT_UINT8, BASE_DEC, NULL, 0x0, "", HFILL}},
640 {"Command Ref Num", "fcp.crn", FT_UINT8, BASE_DEC, NULL, 0x0, "",
643 {"Task Attribute", "fcp.taskattr", FT_UINT8, BASE_HEX,
644 VALS (fcp_task_attr_val), 0x7, "", HFILL}},
646 {"Task Management Flags", "fcp.taskmgmt", FT_UINT8, BASE_HEX, NULL,
648 { &hf_fcp_addlcdblen,
649 {"Additional CDB Length", "fcp.addlcdblen", FT_UINT8, BASE_DEC, NULL,
652 {"RDDATA", "fcp.rddata", FT_BOOLEAN, 8, NULL, 0x02, "", HFILL}},
654 {"WRDATA", "fcp.wrdata", FT_BOOLEAN, 8, NULL, 0x01, "", HFILL}},
656 {"FCP_DL", "fcp.dl", FT_UINT32, BASE_DEC, NULL, 0x0, "", HFILL}},
658 {"FCP_DATA_RO", "fcp.data_ro", FT_UINT32, BASE_DEC, NULL, 0x0, "",
661 {"Burst Length", "fcp.burstlen", FT_UINT32, BASE_DEC, NULL, 0x0, "",
664 {"FCP_RSP Flags", "fcp.rspflags", FT_UINT8, BASE_HEX, NULL, 0x0, "",
667 {"FCP_RESID", "fcp.resid", FT_UINT32, BASE_DEC, NULL, 0x0, "",
670 {"FCP_SNS_LEN", "fcp.snslen", FT_UINT32, BASE_DEC, NULL, 0x0, "",
673 {"FCP_RSP_LEN", "fcp.rsplen", FT_UINT32, BASE_DEC, NULL, 0x0, "",
676 {"RSP_CODE", "fcp.rspcode", FT_UINT8, BASE_HEX,
677 VALS (fcp_rsp_code_val), 0x0, "", HFILL}},
678 { &hf_fcp_scsistatus,
679 {"SCSI Status", "fcp.status", FT_UINT8, BASE_HEX,
680 VALS (scsi_status_val), 0x0, "", HFILL}},
683 /* Setup protocol subtree array */
684 static gint *ett[] = {
688 /* Register the protocol name and description */
689 proto_fcp = proto_register_protocol("Fibre Channel Protocol for SCSI",
692 /* Required function calls to register the header fields and subtrees used */
693 proto_register_field_array(proto_fcp, hf, array_length(hf));
694 proto_register_subtree_array(ett, array_length(ett));
695 fcp_dissector = register_dissector_table ("fcp.type", "FCP Type", FT_UINT8,
697 register_init_routine (&fcp_init_protocol);
700 /* If this dissector uses sub-dissector registration add a registration routine.
701 This format is required because a script is used to find these routines and
702 create the code that calls these routines.
705 proto_reg_handoff_fcp (void)
707 dissector_handle_t fcp_handle;
709 fcp_handle = create_dissector_handle (dissect_fcp, proto_fcp);
710 dissector_add("fc.ftype", FC_FTYPE_SCSI, fcp_handle);
712 data_handle = find_dissector ("data");