2 * Routines for FCIP dissection
3 * Copyright 2001, Dinesh G Dutt (ddutt@cisco.com)
5 * $Id: packet-fcip.c,v 1.7 2003/09/09 05:02:35 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>
42 * draft-ietf-ips-fcovertcpip-12
44 * draft-ietf-ips-fcencapsulation-08
47 #define FCIP_ENCAP_HEADER_LEN 28
48 #define FCIP_MIN_HEADER_LEN 16 /* upto frame len field */
49 #define FCIP_ENCAP_PROTO_VER 0xFEFE0101
50 #define FCIP_IS_SF(pflags) ((pflags & 0x1) == 0x1)
51 #define FCIP_IS_CH(pflags) ((pflags & 0x80) == 0x80)
76 FCENCAP_PROTO_FCIP = 1,
77 FCENCAP_PROTO_iFCP = 2,
80 static const value_string fcip_eof_vals[] = {
83 {FCIP_EOFrt, "EOFrt" },
84 {FCIP_EOFdt, "EOFdt" },
85 {FCIP_EOFni, "EOFni" },
86 {FCIP_EOFdti, "EOFdti" },
87 {FCIP_EOFrti, "EOFrti" },
92 static const value_string fcip_sof_vals[] = {
94 {FCIP_SOFi4, "SOFi4" },
95 {FCIP_SOFi2, "SOFi2" },
96 {FCIP_SOFi3, "SOFi3" },
97 {FCIP_SOFn4, "SOFn4" },
98 {FCIP_SOFn2, "SOFn2" },
99 {FCIP_SOFn3, "SOFn3" },
100 {FCIP_SOFc4, "SOFc4" },
104 static const value_string fcencap_proto_vals[] = {
105 {FCENCAP_PROTO_FCIP, "FCIP"},
106 {FCENCAP_PROTO_iFCP, "iFCP"},
109 static guint fcip_header_2_bytes[2] = {FCIP_ENCAP_PROTO_VER,
110 FCIP_ENCAP_PROTO_VER};
112 static int proto_fcip = -1;
114 static int hf_fcip_protocol = -1;
115 static int hf_fcip_protocol_c = -1;
116 static int hf_fcip_version = -1;
117 static int hf_fcip_version_c = -1;
118 static int hf_fcip_encap_word1 = -1;
119 static int hf_fcip_flags = -1;
120 static int hf_fcip_flags_c = -1;
121 static int hf_fcip_framelen = -1;
122 static int hf_fcip_framelen_c = -1;
123 static int hf_fcip_tsec = -1;
124 static int hf_fcip_tusec = -1;
125 static int hf_fcip_encap_crc = -1;
126 static int hf_fcip_sof = -1;
127 static int hf_fcip_sof_c = -1;
128 static int hf_fcip_eof = -1;
129 static int hf_fcip_eof_c = -1;
130 static int hf_fcip_pflags_changed = -1;
131 static int hf_fcip_pflags_special = -1;
132 static int hf_fcip_pflags_c = -1;
133 static int hf_fcip_src_wwn = -1;
134 static int hf_fcip_dst_wwn = -1;
135 static int hf_fcip_conn_code = -1;
136 static int hf_fcip_katov = -1;
137 static int hf_fcip_src_entity_id = -1;
138 static int hf_fcip_conn_nonce = -1;
139 static int hf_fcip_conn_flags = -1;
141 static int ett_fcip = -1;
143 static guint fcip_port = 3225;
144 static gboolean fcip_desegment = TRUE;
146 static dissector_handle_t data_handle;
147 static dissector_handle_t fc_handle;
149 /* This routine attempts to locate the position of the next header in the
153 get_next_fcip_header_offset (tvbuff_t *tvb, packet_info *pinfo, gint offset)
155 gint bytes_remaining = tvb_length_remaining (tvb, offset);
158 fcip_eof_t eof, eofc;
161 * As per the FCIP standard, the following tests must PASS:
162 * 1) Frame Length field validation -- 15 < Frame Length < 545;
163 * 2) Comparison of Frame Length field to its ones complement; and
164 * 3) A valid EOF is found in the word preceding the start of the next
165 * FCIP header as indicated by the Frame Length field, to be tested
167 * 1) Bits 24-31 and 16-23 contain identical legal EOF values (the
168 * list of legal EOF values is in the FC Frame Encapsulation
170 * 2) Bits 8-15 and 0-7 contain the ones complement of the EOF
171 * value found in bits 24-31.
173 * As per the FCIP standard, in addition, at least 3 of the following set
174 * of tests must be performed to identify that we've located the start of
176 * a) Protocol# ones complement field (1 test);
177 * b) Version ones complement field (1 test);
178 * c) Replication of encapsulation word 0 in word 1 (1 test);
179 * d) Reserved field and its ones complement (2 tests);
180 * e) Flags field and its ones complement (2 tests);
181 * f) CRC field is equal to zero (1 test);
182 * g) SOF fields and ones complement fields (4 tests);
183 * h) Format and values of FC header (1 test);
184 * i) CRC of FC Frame (2 tests);
185 * j) FC Frame Encapsulation header information in the next FCIP Frame
188 * At least 3 of the 16 tests listed above SHALL be performed. Failure
189 * of any of the above tests actually performed SHALL indicate an
190 * encapsulation error and the FC Frame SHALL NOT be forwarded on to
194 NXT_BYTE: while (bytes_remaining) {
195 if (bytes_remaining < FCIP_ENCAP_HEADER_LEN) {
196 if(fcip_desegment && pinfo->can_desegment) {
198 * This frame doesn't have all of the data for
199 * this message, but we can do reassembly on it.
201 * Tell the TCP dissector where the data for this
202 * message starts in the data it handed us, and
203 * how many more bytes we need, and return.
205 pinfo->desegment_offset = offset;
206 pinfo->desegment_len = FCIP_ENCAP_HEADER_LEN;
211 /* I check that we have a valid header before checking for the frame
212 * length and the other initial tests.
218 if (memcmp ((void *)tvb_get_ptr (tvb, offset, 8),
219 (void *)fcip_header_2_bytes, 8) != 0) {
225 flen = (tvb_get_ntohs (tvb, offset+12)) & 0x03FF;
226 frame_len = (tvb_get_ntohs (tvb, offset+12) & 0x03FF)*4;
228 if ((flen < 15) || (flen > 545)) {
229 /* Frame length check failed. Skip byte and try again */
235 flen1 = (tvb_get_ntohs (tvb, offset+14)) & 0x03FF;
237 if ((flen & 0x03FF) != ((~flen1)&0x03FF)) {
238 /* frame_len and its one's complement are not the same */
244 /* Valid EOF check */
245 if (tvb_bytes_exist (tvb, offset+(frame_len-1)*4, 4)) {
246 eof = (fcip_eof_t)tvb_get_guint8 (tvb, offset+(frame_len-1)*4);
247 eofc = (fcip_eof_t)tvb_get_guint8 (tvb, offset+(frame_len-1)*4+2);
249 if ((eof != FCIP_EOFn) && (eof != FCIP_EOFt) && (eof != FCIP_EOFrt)
250 && (eof != FCIP_EOFdt) && (eof != FCIP_EOFni) &&
251 (eof != FCIP_EOFdti) && (eof != FCIP_EOFrti) &&
252 (eof != FCIP_EOFa)) {
258 if ((eof != ~eofc) ||
259 (eof != tvb_get_guint8 (tvb, offset+(frame_len-1)*4+1)) ||
260 (eofc != tvb_get_guint8 (tvb, offset+(frame_len-1)*4+3))) {
268 if ((tvb_get_guint8 (tvb, offset+9) != 0) ||
269 (tvb_get_guint8 (tvb, offset+11) != 0xFF)) {
279 if (tvb_get_ntohl (tvb, offset+24)) {
286 if (bytes_remaining >= (frame_len)) {
287 if (tvb_bytes_exist (tvb, offset+frame_len, 8)) {
288 /* The start of the next header matches what we wish to see */
289 if (memcmp ((void *)tvb_get_ptr (tvb, offset+frame_len, 8),
290 (void *)fcip_header_2_bytes, 8) == 0) {
304 if(fcip_desegment && pinfo->can_desegment) {
306 * This frame doesn't have all of the data for
307 * this message, but we can do reassembly on it.
309 * Tell the TCP dissector where the data for this
310 * message starts in the data it handed us, and
311 * how many more bytes we need, and return.
313 pinfo->desegment_offset = offset;
314 pinfo->desegment_len = frame_len - bytes_remaining;
323 return (-1); /* Unable to find FCIP header */
327 dissect_fcencap_header (tvbuff_t *tvb, proto_tree *tree, gint offset)
329 guint8 protocol = tvb_get_guint8 (tvb, offset);
332 proto_tree_add_uint (tree, hf_fcip_protocol, tvb, offset, 1, protocol);
333 proto_tree_add_item (tree, hf_fcip_version, tvb, offset+1, 1, 0);
334 proto_tree_add_item (tree, hf_fcip_protocol_c, tvb, offset+2, 1, 0);
335 proto_tree_add_item (tree, hf_fcip_version_c, tvb, offset+3, 1, 0);
337 if (protocol == FCENCAP_PROTO_FCIP) {
338 proto_tree_add_item (tree, hf_fcip_encap_word1, tvb, offset+4,
340 proto_tree_add_item (tree, hf_fcip_pflags_changed, tvb, offset+8,
342 proto_tree_add_item (tree, hf_fcip_pflags_special, tvb, offset+8,
344 proto_tree_add_item (tree, hf_fcip_pflags_c, tvb, offset+10, 1, 0);
347 /* XXX - break out CRCV flag. */
348 proto_tree_add_item (tree, hf_fcip_flags, tvb, offset+12, 1, 0);
349 proto_tree_add_item (tree, hf_fcip_framelen, tvb, offset+12, 2, 0);
350 proto_tree_add_item (tree, hf_fcip_flags_c, tvb, offset+14, 1, 0);
351 proto_tree_add_item (tree, hf_fcip_framelen_c, tvb, offset+14, 2, 0);
352 proto_tree_add_item (tree, hf_fcip_tsec, tvb, offset+16, 4, 0);
353 proto_tree_add_item (tree, hf_fcip_tusec, tvb, offset+20, 4, 0);
354 /* XXX - check CRC if CRCV is set? */
355 proto_tree_add_item (tree, hf_fcip_encap_crc, tvb, offset+24, 4, 0);
360 dissect_fcip_sf (tvbuff_t *tvb, proto_tree *tree, gint offset)
363 proto_tree_add_string (tree, hf_fcip_src_wwn, tvb, offset, 8,
364 fcwwn_to_str (tvb_get_ptr (tvb, offset, 8)));
365 proto_tree_add_bytes (tree, hf_fcip_src_entity_id, tvb, offset+8, 8,
366 tvb_get_ptr (tvb, offset+8, 8));
367 proto_tree_add_bytes (tree, hf_fcip_conn_nonce, tvb, offset+16, 8,
368 tvb_get_ptr (tvb, offset+16, 8));
369 /* XXX - break out these flags */
370 proto_tree_add_item (tree, hf_fcip_conn_flags, tvb, offset+24, 1, 0);
371 proto_tree_add_item (tree, hf_fcip_conn_code, tvb, offset+26, 2, 0);
372 proto_tree_add_string (tree, hf_fcip_dst_wwn, tvb, offset+30, 8,
373 fcwwn_to_str (tvb_get_ptr (tvb, offset+30, 8)));
374 proto_tree_add_item (tree, hf_fcip_katov, tvb, offset+38, 4, 0);
379 dissect_fcip (tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
384 gint bytes_remaining = tvb_length_remaining (tvb, offset);
385 guint8 pflags, sof, eof;
386 /* Set up structures needed to add the protocol subtree and manage it */
388 proto_tree *fcip_tree = NULL;
391 if (!proto_is_protocol_enabled(proto_fcip))
392 return FALSE; /* iSCSI has been disabled */
394 if (bytes_remaining < FCIP_ENCAP_HEADER_LEN) {
398 if ((pinfo->srcport != fcip_port) && (pinfo->destport != fcip_port)) {
402 while (bytes_remaining > FCIP_ENCAP_HEADER_LEN) {
403 if ((offset = get_next_fcip_header_offset (tvb, pinfo, offset)) == -1) {
406 else if (offset == -2) {
407 /* We need more data to desegment */
412 if (check_col(pinfo->cinfo, COL_PROTOCOL))
413 col_set_str(pinfo->cinfo, COL_PROTOCOL, "FCIP");
415 frame_len = (tvb_get_ntohs (tvb, offset+12) & 0x03FF)*4;
417 if (bytes_remaining < frame_len) {
418 if(fcip_desegment && pinfo->can_desegment) {
420 * This frame doesn't have all of the data for
421 * this message, but we can do reassembly on it.
423 * Tell the TCP dissector where the data for this
424 * message starts in the data it handed us, and
425 * how many more bytes we need, and return.
427 pinfo->desegment_offset = offset;
428 pinfo->desegment_len = frame_len - bytes_remaining;
433 pflags = tvb_get_guint8 (tvb, start+8);
436 if (FCIP_IS_SF (pflags)) {
437 ti = proto_tree_add_protocol_format (tree, proto_fcip, tvb, 0,
438 FCIP_ENCAP_HEADER_LEN,
441 else if (tvb_bytes_exist (tvb, offset, offset+frame_len-4)) {
442 sof = tvb_get_guint8 (tvb, offset+FCIP_ENCAP_HEADER_LEN);
443 eof = tvb_get_guint8 (tvb, offset+frame_len - 4);
445 ti = proto_tree_add_protocol_format (tree, proto_fcip, tvb, 0,
446 FCIP_ENCAP_HEADER_LEN,
448 val_to_str (sof, fcip_sof_vals,
450 val_to_str (eof, fcip_eof_vals,
454 sof = tvb_get_guint8 (tvb, offset+FCIP_ENCAP_HEADER_LEN);
456 ti = proto_tree_add_protocol_format (tree, proto_fcip, tvb, 0,
457 FCIP_ENCAP_HEADER_LEN,
459 val_to_str (sof, fcip_sof_vals,
463 fcip_tree = proto_item_add_subtree (ti, ett_fcip);
464 /* Dissect the Common FC Encap header */
465 dissect_fcencap_header (tvb, fcip_tree, offset);
467 offset += FCIP_ENCAP_HEADER_LEN;
469 if (!FCIP_IS_SF (pflags)) {
471 proto_tree_add_item (fcip_tree, hf_fcip_sof, tvb, offset, 1, 0);
472 proto_tree_add_item (fcip_tree, hf_fcip_sof_c, tvb, offset+2, 1, 0);
475 offset += (frame_len-FCIP_ENCAP_HEADER_LEN-4);
476 if (tvb_bytes_exist (tvb, offset, 4)) {
477 proto_tree_add_item (fcip_tree, hf_fcip_eof, tvb, offset, 1, 0);
478 proto_tree_add_item (fcip_tree, hf_fcip_eof_c, tvb, offset+2, 1, 0);
483 /* Call the FC Dissector if this is carrying an FC frame */
484 if (!FCIP_IS_SF(pflags)) {
485 /* Special frame bit is not set */
486 next_tvb = tvb_new_subset (tvb, FCIP_ENCAP_HEADER_LEN+4, -1, -1);
488 call_dissector (fc_handle, next_tvb, pinfo, tree);
490 else if (data_handle) {
491 call_dissector (data_handle, next_tvb, pinfo, tree);
495 if (check_col(pinfo->cinfo, COL_INFO))
496 col_set_str(pinfo->cinfo, COL_INFO, "Special Frame");
497 if (FCIP_IS_CH (pflags)) {
498 if (check_col(pinfo->cinfo, COL_INFO))
499 col_append_str(pinfo->cinfo, COL_INFO, "(Changed)");
502 dissect_fcip_sf (tvb, fcip_tree, offset+4);
505 bytes_remaining -= frame_len;
512 proto_register_fcip (void)
515 /* Setup list of header fields See Section 1.6.1 for details*/
516 static hf_register_info hf[] = {
518 { "Protocol", "fcencap.proto", FT_UINT8, BASE_DEC,
519 VALS(fcencap_proto_vals), 0, "Protocol", HFILL }},
520 { &hf_fcip_protocol_c,
521 {"Protocol (1's Complement)", "fcencap.protoc", FT_UINT8, BASE_DEC, NULL,
522 0, "Protocol (1's Complement)", HFILL}},
524 {"Version", "fcencap.version", FT_UINT8, BASE_DEC, NULL, 0, "",
526 { &hf_fcip_version_c,
527 {"Version (1's Complement)", "fcencap.versionc", FT_UINT8, BASE_DEC,
528 NULL, 0, "", HFILL}},
529 { &hf_fcip_encap_word1,
530 {"FCIP Encapsulation Word1", "fcip.word1", FT_UINT32, BASE_HEX, NULL,
533 {"Flags", "fcencap.flags", FT_UINT8, BASE_HEX, NULL, 0xFC, "", HFILL}},
535 {"Flags (1's Complement)", "fcencap.flagsc", FT_UINT8, BASE_HEX,
536 NULL, 0xFC, "", HFILL}},
538 {"Frame Length (in Words)", "fcencap.framelen", FT_UINT16, BASE_DEC,
539 NULL, 0x03FF, "", HFILL}},
540 { &hf_fcip_framelen_c,
541 {"Frame Length (1's Complement)", "fcencap.framelenc", FT_UINT16,
542 BASE_DEC, NULL, 0x03FF, "", HFILL}},
544 {"Time (secs)", "fcencap.tsec", FT_UINT32, BASE_DEC, NULL, 0, "",
547 {"Time (fraction)", "fcencap.tusec", FT_UINT32, BASE_DEC, NULL, 0,
549 { &hf_fcip_encap_crc,
550 {"CRC", "fcencap.crc", FT_UINT32, BASE_HEX, NULL, 0, "", HFILL}},
552 {"SOF", "fcip.sof", FT_UINT8, BASE_HEX, VALS (&fcip_sof_vals), 0,
555 {"SOF (1's Complement)", "fcip.sofc", FT_UINT8, BASE_HEX, NULL,
558 {"EOF", "fcip.eof", FT_UINT8, BASE_HEX, VALS (&fcip_eof_vals), 0,
561 {"EOF (1's Complement)", "fcip.eofc", FT_UINT8, BASE_HEX, NULL,
563 { &hf_fcip_pflags_changed,
564 {"Changed Flag", "fcip.pflags.ch", FT_BOOLEAN, BASE_DEC, NULL, 0x80,
566 { &hf_fcip_pflags_special,
567 {"Special Frame Flag", "fcip.pflags.sf", FT_BOOLEAN, BASE_DEC, NULL,
570 {"Pflags (1's Complement)", "fcip.pflagsc", FT_UINT8, BASE_HEX, NULL,
573 {"Source Fabric WWN", "fcip.srcwwn", FT_STRING, BASE_HEX, NULL, 0x0,
576 {"Destination Fabric WWN", "fcip.dstwwn", FT_STRING, BASE_HEX, NULL,
578 { &hf_fcip_src_entity_id,
579 {"FC/FCIP Entity Id", "fcip.srcid", FT_BYTES, BASE_HEX, NULL, 0x0,
581 { &hf_fcip_conn_flags,
582 {"Connection Usage Flags", "fcip.connflags", FT_UINT8, BASE_HEX,
583 NULL, 0x0, "", HFILL}},
584 { &hf_fcip_conn_code,
585 {"Connection Usage Code", "fcip.conncode", FT_UINT16, BASE_HEX, NULL,
588 {"K_A_TOV", "fcip.katov", FT_UINT32, BASE_DEC, NULL, 0x0, "", HFILL}},
589 { &hf_fcip_conn_nonce,
590 {"Connection Nonce", "fcip.nonce", FT_BYTES, BASE_HEX, NULL, 0x0, "",
594 static gint *ett[] = {
598 module_t *fcip_module;
600 /* Register the protocol name and description */
601 proto_fcip = proto_register_protocol("FCIP", "FCIP", "fcip");
603 /* Required function calls to register the header fields and
605 proto_register_field_array(proto_fcip, hf, array_length(hf));
606 proto_register_subtree_array(ett, array_length(ett));
608 fcip_module = prefs_register_protocol(proto_fcip, NULL);
609 prefs_register_bool_preference(fcip_module,
611 "Desegment FCIP messages",
612 "When enabled, FCIP messages that span multiple TCP segments are desegmented",
614 prefs_register_uint_preference(fcip_module,
617 "Port number used for FCIP",
624 * If this dissector uses sub-dissector registration add a
625 * registration routine.
629 * This format is required because a script is used to find these
630 * routines and create the code that calls these routines.
633 proto_reg_handoff_fcip (void)
635 heur_dissector_add("tcp", dissect_fcip, proto_fcip);
636 data_handle = find_dissector("data");
637 fc_handle = find_dissector("fc");