2 * Routines for FCIP dissection
3 * Copyright 2001, Dinesh G Dutt (ddutt@cisco.com)
5 * $Id: packet-fcip.c,v 1.4 2003/07/09 06:24:48 guy Exp $
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.
36 #include <epan/packet.h>
38 #include <epan/conversation.h>
40 #define FCIP_ENCAP_HEADER_LEN 28
41 #define FCIP_MIN_HEADER_LEN 16 /* upto frame len field */
42 #define FCIP_ENCAP_PROTO_VER 0xFEFE0101
43 #define FCIP_IS_SF(pflags) ((pflags & 0x1) == 0x1)
44 #define FCIP_IS_CH(pflags) ((pflags & 0x80) == 0x80)
69 FCENCAP_PROTO_FCIP = 1,
70 FCENCAP_PROTO_iFCP = 2,
73 static const value_string fcip_eof_vals[] = {
76 {FCIP_EOFrt, "EOFrt" },
77 {FCIP_EOFdt, "EOFdt" },
78 {FCIP_EOFni, "EOFni" },
79 {FCIP_EOFdti, "EOFdti" },
80 {FCIP_EOFrti, "EOFrti" },
85 static const value_string fcip_sof_vals[] = {
87 {FCIP_SOFi4, "SOFi4" },
88 {FCIP_SOFi2, "SOFi2" },
89 {FCIP_SOFi3, "SOFi3" },
90 {FCIP_SOFn4, "SOFn4" },
91 {FCIP_SOFn2, "SOFn2" },
92 {FCIP_SOFn3, "SOFn3" },
93 {FCIP_SOFc4, "SOFc4" },
97 static const value_string fcencap_proto_vals[] = {
98 {FCENCAP_PROTO_FCIP, "FCIP"},
99 {FCENCAP_PROTO_iFCP, "iFCP"},
102 static const value_string fsf_conn_flag_vals[] = {
106 static const gchar * sof_strings[] = {
107 "SOFf", "SOFi4", "", "", "", "SOFi2", "SOFi3", "", "", "SOFn4", "", "", "",
108 "SOFn2", "SOFn3", "", "", "SOFc4", "",
111 static const gchar *eof_strings[] = {
112 "EOFn", "EOFt", "", "EOFrt", "", "EOFdt", "", "", "EOFni", "", "", "", "",
113 "EOFdti", "EOFrti", "EOFa", "",
116 static guint fcip_header_2_bytes[2] = {FCIP_ENCAP_PROTO_VER,
117 FCIP_ENCAP_PROTO_VER};
119 static int proto_fcip = -1;
121 static int hf_fcip_protocol = -1;
122 static int hf_fcip_protocol_c = -1;
123 static int hf_fcip_version = -1;
124 static int hf_fcip_version_c = -1;
125 static int hf_fcip_encap_word1 = -1;
126 static int hf_fcip_flags = -1;
127 static int hf_fcip_flags_c = -1;
128 static int hf_fcip_framelen = -1;
129 static int hf_fcip_framelen_c = -1;
130 static int hf_fcip_tsec = -1;
131 static int hf_fcip_tusec = -1;
132 static int hf_fcip_encap_crc = -1;
133 static int hf_fcip_sof = -1;
134 static int hf_fcip_sof_c = -1;
135 static int hf_fcip_eof = -1;
136 static int hf_fcip_eof_c = -1;
137 static int hf_fcip_pflags_changed = -1;
138 static int hf_fcip_pflags_special = -1;
139 static int hf_fcip_pflags_c = -1;
140 static int hf_fcip_src_wwn = -1;
141 static int hf_fcip_dst_wwn = -1;
142 static int hf_fcip_conn_code = -1;
143 static int hf_fcip_katov = -1;
144 static int hf_fcip_src_entity_id = -1;
145 static int hf_fcip_conn_nonce = -1;
146 static int hf_fcip_conn_flags = -1;
148 static int ett_fcip = -1;
150 static guint fcip_port = 3225;
151 static gboolean fcip_desegment = TRUE;
153 static dissector_handle_t data_handle;
154 static dissector_handle_t fc_handle;
156 /* This routine attempts to locate the position of the next header in the
160 get_next_fcip_header_offset (tvbuff_t *tvb, packet_info *pinfo, gint offset)
162 gint bytes_remaining = tvb_length_remaining (tvb, offset);
165 fcip_eof_t eof, eofc;
168 * As per the FCIP standard, the following tests must PASS:
169 * 1) Frame Length field validation -- 15 < Frame Length < 545;
170 * 2) Comparison of Frame Length field to its ones complement; and
171 * 3) A valid EOF is found in the word preceding the start of the next
172 * FCIP header as indicated by the Frame Length field, to be tested
174 * 1) Bits 24-31 and 16-23 contain identical legal EOF values (the
175 * list of legal EOF values is in the FC Frame Encapsulation
177 * 2) Bits 8-15 and 0-7 contain the ones complement of the EOF
178 * value found in bits 24-31.
180 * As per the FCIP standard, in addition, at least 3 of the following set
181 * of tests must be performed to identify that we've located the start of
183 * a) Protocol# ones complement field (1 test);
184 * b) Version ones complement field (1 test);
185 * c) Replication of encapsulation word 0 in word 1 (1 test);
186 * d) Reserved field and its ones complement (2 tests);
187 * e) Flags field and its ones complement (2 tests);
188 * f) CRC field is equal to zero (1 test);
189 * g) SOF fields and ones complement fields (4 tests);
190 * h) Format and values of FC header (1 test);
191 * i) CRC of FC Frame (2 tests);
192 * j) FC Frame Encapsulation header information in the next FCIP Frame
195 * At least 3 of the 16 tests listed above SHALL be performed. Failure
196 * of any of the above tests actually performed SHALL indicate an
197 * encapsulation error and the FC Frame SHALL NOT be forwarded on to
201 NXT_BYTE: while (bytes_remaining) {
202 if (bytes_remaining < FCIP_ENCAP_HEADER_LEN) {
203 if(fcip_desegment && pinfo->can_desegment) {
205 * This frame doesn't have all of the data for
206 * this message, but we can do reassembly on it.
208 * Tell the TCP dissector where the data for this
209 * message starts in the data it handed us, and
210 * how many more bytes we need, and return.
212 pinfo->desegment_offset = offset;
213 pinfo->desegment_len = FCIP_ENCAP_HEADER_LEN;
218 /* I check that we have a valid header before checking for the frame
219 * length and the other initial tests.
225 if (memcmp ((void *)tvb_get_ptr (tvb, offset, 8),
226 (void *)fcip_header_2_bytes, 8) != 0) {
232 flen = (tvb_get_ntohs (tvb, offset+12)) & 0x03FF;
233 frame_len = (tvb_get_ntohs (tvb, offset+12) & 0x03FF)*4;
235 if ((flen < 15) || (flen > 545)) {
236 /* Frame length check failed. Skip byte and try again */
242 flen1 = (tvb_get_ntohs (tvb, offset+14)) & 0x03FF;
244 if ((flen & 0x03FF) != ((~flen1)&0x03FF)) {
245 /* frame_len and its one's complement are not the same */
251 /* Valid EOF check */
252 if (tvb_bytes_exist (tvb, offset+(frame_len-1)*4, 4)) {
253 eof = (fcip_eof_t)tvb_get_guint8 (tvb, offset+(frame_len-1)*4);
254 eofc = (fcip_eof_t)tvb_get_guint8 (tvb, offset+(frame_len-1)*4+2);
256 if ((eof != FCIP_EOFn) && (eof != FCIP_EOFt) && (eof != FCIP_EOFrt)
257 && (eof != FCIP_EOFdt) && (eof != FCIP_EOFni) &&
258 (eof != FCIP_EOFdti) && (eof != FCIP_EOFrti) &&
259 (eof != FCIP_EOFa)) {
265 if ((eof != ~eofc) ||
266 (eof != tvb_get_guint8 (tvb, offset+(frame_len-1)*4+1)) ||
267 (eofc != tvb_get_guint8 (tvb, offset+(frame_len-1)*4+3))) {
275 if ((tvb_get_guint8 (tvb, offset+9) != 0) ||
276 (tvb_get_guint8 (tvb, offset+11) != 0xFF)) {
286 if (tvb_get_ntohl (tvb, offset+24)) {
293 if (bytes_remaining >= (frame_len)) {
294 if (tvb_bytes_exist (tvb, offset+frame_len, 8)) {
295 /* The start of the next header matches what we wish to see */
296 if (memcmp ((void *)tvb_get_ptr (tvb, offset+frame_len, 8),
297 (void *)fcip_header_2_bytes, 8) == 0) {
311 if(fcip_desegment && pinfo->can_desegment) {
313 * This frame doesn't have all of the data for
314 * this message, but we can do reassembly on it.
316 * Tell the TCP dissector where the data for this
317 * message starts in the data it handed us, and
318 * how many more bytes we need, and return.
320 pinfo->desegment_offset = offset;
321 pinfo->desegment_len = frame_len - bytes_remaining;
330 return (-1); /* Unable to find FCIP header */
334 dissect_fcencap_header (tvbuff_t *tvb, proto_tree *tree, gint offset)
336 guint8 protocol = tvb_get_guint8 (tvb, offset);
339 proto_tree_add_uint (tree, hf_fcip_protocol, tvb, offset, 1, protocol);
340 proto_tree_add_item (tree, hf_fcip_version, tvb, offset+1, 1, 0);
341 proto_tree_add_item (tree, hf_fcip_protocol_c, tvb, offset+2, 1, 0);
342 proto_tree_add_item (tree, hf_fcip_version_c, tvb, offset+3, 1, 0);
344 if (protocol == FCENCAP_PROTO_FCIP) {
345 proto_tree_add_item (tree, hf_fcip_encap_word1, tvb, offset+4,
347 proto_tree_add_item (tree, hf_fcip_pflags_changed, tvb, offset+8,
349 proto_tree_add_item (tree, hf_fcip_pflags_special, tvb, offset+8,
351 proto_tree_add_item (tree, hf_fcip_pflags_c, tvb, offset+10, 1, 0);
354 proto_tree_add_item (tree, hf_fcip_flags, tvb, offset+12, 1, 0);
355 proto_tree_add_item (tree, hf_fcip_framelen, tvb, offset+12, 2, 0);
356 proto_tree_add_item (tree, hf_fcip_flags_c, tvb, offset+14, 1, 0);
357 proto_tree_add_item (tree, hf_fcip_framelen_c, tvb, offset+14, 2, 0);
358 proto_tree_add_item (tree, hf_fcip_tsec, tvb, offset+16, 4, 0);
359 proto_tree_add_item (tree, hf_fcip_tusec, tvb, offset+20, 4, 0);
360 proto_tree_add_item (tree, hf_fcip_encap_crc, tvb, offset+24, 4, 0);
365 dissect_fcip_sf (tvbuff_t *tvb, proto_tree *tree, gint offset)
368 proto_tree_add_string (tree, hf_fcip_src_wwn, tvb, offset, 8,
369 fcwwn_to_str (tvb_get_ptr (tvb, offset, 8)));
370 proto_tree_add_bytes (tree, hf_fcip_src_entity_id, tvb, offset+8, 8,
371 tvb_get_ptr (tvb, offset+8, 8));
372 proto_tree_add_bytes (tree, hf_fcip_conn_nonce, tvb, offset+16, 8,
373 tvb_get_ptr (tvb, offset+16, 8));
374 proto_tree_add_item (tree, hf_fcip_conn_flags, tvb, offset+24, 1, 0);
375 proto_tree_add_item (tree, hf_fcip_conn_code, tvb, offset+26, 2, 0);
376 proto_tree_add_string (tree, hf_fcip_dst_wwn, tvb, offset+30, 8,
377 fcwwn_to_str (tvb_get_ptr (tvb, offset+30, 8)));
378 proto_tree_add_item (tree, hf_fcip_katov, tvb, offset+38, 4, 0);
383 dissect_fcip (tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
388 guint bytes_remaining = tvb_length_remaining (tvb, offset);
389 guint8 pflags, sof, eof;
390 /* Set up structures needed to add the protocol subtree and manage it */
392 proto_tree *fcip_tree = NULL;
395 if (!proto_is_protocol_enabled(proto_fcip))
396 return FALSE; /* iSCSI has been disabled */
398 if (bytes_remaining < FCIP_ENCAP_HEADER_LEN) {
402 if ((pinfo->srcport != fcip_port) && (pinfo->destport != fcip_port)) {
406 while (bytes_remaining > FCIP_ENCAP_HEADER_LEN) {
407 if ((offset = get_next_fcip_header_offset (tvb, pinfo, offset)) == -1) {
410 else if (offset == -2) {
411 /* We need more data to desegment */
416 if (check_col(pinfo->cinfo, COL_PROTOCOL))
417 col_set_str(pinfo->cinfo, COL_PROTOCOL, "FCIP");
419 frame_len = (tvb_get_ntohs (tvb, offset+12) & 0x03FF)*4;
421 pflags = tvb_get_guint8 (tvb, start+8);
424 if (FCIP_IS_SF (pflags)) {
425 ti = proto_tree_add_protocol_format (tree, proto_fcip, tvb, 0,
426 FCIP_ENCAP_HEADER_LEN,
430 sof = tvb_get_guint8 (tvb, offset+FCIP_ENCAP_HEADER_LEN) - 0x28;
431 eof = tvb_get_guint8 (tvb, offset+frame_len - 4) - 0x41;
434 sof = 18; /* The last SOF */
440 ti = proto_tree_add_protocol_format (tree, proto_fcip, tvb, 0,
441 FCIP_ENCAP_HEADER_LEN,
446 fcip_tree = proto_item_add_subtree (ti, ett_fcip);
447 /* Dissect the Common FC Encap header */
448 dissect_fcencap_header (tvb, fcip_tree, offset);
450 offset += FCIP_ENCAP_HEADER_LEN;
452 if (!FCIP_IS_SF (pflags)) {
454 proto_tree_add_item (fcip_tree, hf_fcip_sof, tvb, offset, 1, 0);
455 proto_tree_add_item (fcip_tree, hf_fcip_sof_c, tvb, offset+2, 1, 0);
458 offset += (frame_len-FCIP_ENCAP_HEADER_LEN-4);
459 proto_tree_add_item (fcip_tree, hf_fcip_eof, tvb, offset, 1, 0);
460 proto_tree_add_item (fcip_tree, hf_fcip_eof_c, tvb, offset+2, 1, 0);
464 /* Call the FC Dissector if this is carrying an FC frame */
465 if (!FCIP_IS_SF(pflags)) {
466 /* Special frame bit is not set */
467 next_tvb = tvb_new_subset (tvb, FCIP_ENCAP_HEADER_LEN+4, -1, -1);
469 call_dissector (fc_handle, next_tvb, pinfo, tree);
471 else if (data_handle) {
472 call_dissector (data_handle, next_tvb, pinfo, tree);
476 if (check_col(pinfo->cinfo, COL_INFO))
477 col_set_str(pinfo->cinfo, COL_INFO, "Special Frame");
478 if (FCIP_IS_CH (pflags)) {
479 if (check_col(pinfo->cinfo, COL_INFO))
480 col_append_str(pinfo->cinfo, COL_INFO, "(Changed)");
483 dissect_fcip_sf (tvb, fcip_tree, offset+4);
486 bytes_remaining -= frame_len;
493 proto_register_fcip (void)
496 /* Setup list of header fields See Section 1.6.1 for details*/
497 static hf_register_info hf[] = {
499 { "Protocol", "fcencap.proto", FT_UINT8, BASE_DEC,
500 VALS(fcencap_proto_vals), 0, "Protocol", HFILL }},
501 { &hf_fcip_protocol_c,
502 {"Protocol (1's Complement)", "fcencap.protoc", FT_UINT8, BASE_DEC, NULL,
503 0, "Protocol (1's Complement)", HFILL}},
505 {"Version", "fcencap.version", FT_UINT8, BASE_DEC, NULL, 0, "",
507 { &hf_fcip_version_c,
508 {"Version (1's Complement)", "fcencap.versionc", FT_UINT8, BASE_DEC,
509 NULL, 0, "", HFILL}},
510 { &hf_fcip_encap_word1,
511 {"FCIP Encapsulation Word1", "fcip.word1", FT_UINT32, BASE_HEX, NULL,
514 {"Flags", "fcencap.flags", FT_UINT8, BASE_HEX, NULL, 0xFC, "", HFILL}},
516 {"Flags (1's Complement)", "fcencap.flagsc", FT_UINT8, BASE_HEX,
517 NULL, 0xFC, "", HFILL}},
519 {"Frame Length (in Words)", "fcencap.framelen", FT_UINT16, BASE_DEC,
520 NULL, 0x03FF, "", HFILL}},
521 { &hf_fcip_framelen_c,
522 {"Frame Length (1's Complement)", "fcencap.framelenc", FT_UINT16,
523 BASE_DEC, NULL, 0x03FF, "", HFILL}},
525 {"Time (secs)", "fcencap.tsec", FT_UINT32, BASE_DEC, NULL, 0, "",
528 {"Time (fraction)", "fcencap.tusec", FT_UINT32, BASE_DEC, NULL, 0,
530 { &hf_fcip_encap_crc,
531 {"CRC", "fcencap.crc", FT_UINT32, BASE_HEX, NULL, 0, "", HFILL}},
533 {"SOF", "fcip.sof", FT_UINT8, BASE_HEX, VALS (&fcip_sof_vals), 0,
536 {"SOF (1's Complement)", "fcip.sofc", FT_UINT8, BASE_HEX, NULL,
539 {"EOF", "fcip.eof", FT_UINT8, BASE_HEX, VALS (&fcip_eof_vals), 0,
542 {"EOF (1's Complement)", "fcip.eofc", FT_UINT8, BASE_HEX, NULL,
544 { &hf_fcip_pflags_changed,
545 {"Changed Flag", "fcip.pflags.ch", FT_BOOLEAN, BASE_DEC, NULL, 0x80,
547 { &hf_fcip_pflags_special,
548 {"Special Frame Flag", "fcip.pflags.sf", FT_BOOLEAN, BASE_DEC, NULL,
551 {"Pflags (1's Complement)", "fcip.pflagsc", FT_UINT8, BASE_HEX, NULL,
554 {"Source Fabric WWN", "fcip.srcwwn", FT_STRING, BASE_HEX, NULL, 0x0,
557 {"Destination Fabric WWN", "fcip.dstwwn", FT_STRING, BASE_HEX, NULL,
559 { &hf_fcip_src_entity_id,
560 {"FC/FCIP Entity Id", "fcip.srcid", FT_BYTES, BASE_HEX, NULL, 0x0,
562 { &hf_fcip_conn_flags,
563 {"Connection Usage Flags", "fcip.connflags", FT_UINT8, BASE_HEX,
564 NULL, 0x0, "", HFILL}},
565 { &hf_fcip_conn_code,
566 {"Connection Usage Code", "fcip.conncode", FT_UINT16, BASE_HEX, NULL,
569 {"K_A_TOV", "fcip.katov", FT_UINT32, BASE_DEC, NULL, 0x0, "", HFILL}},
570 { &hf_fcip_conn_nonce,
571 {"Connection Nonce", "fcip.nonce", FT_BYTES, BASE_HEX, NULL, 0x0, "",
575 static gint *ett[] = {
579 module_t *fcip_module;
581 /* Register the protocol name and description */
582 proto_fcip = proto_register_protocol("FCIP", "FCIP", "fcip");
584 /* Required function calls to register the header fields and
586 proto_register_field_array(proto_fcip, hf, array_length(hf));
587 proto_register_subtree_array(ett, array_length(ett));
589 fcip_module = prefs_register_protocol(proto_fcip, NULL);
590 prefs_register_bool_preference(fcip_module,
592 "Desegment FCIP messages",
593 "When enabled, FCIP messages that span multiple TCP segments are desegmented",
595 prefs_register_uint_preference(fcip_module,
598 "Port number used for FCIP",
605 * If this dissector uses sub-dissector registration add a
606 * registration routine.
610 * This format is required because a script is used to find these
611 * routines and create the code that calls these routines.
614 proto_reg_handoff_fcip (void)
616 heur_dissector_add("tcp", dissect_fcip, proto_fcip);
617 data_handle = find_dissector("data");
618 fc_handle = find_dissector("fc");