2 * Routines for iFCP dissection
5 * Copyright 2005 Aboo Valappil (valappil_aboo@emc.com)
6 * 2006 ronnie sahlberg major refactoring
9 * Significantly based on packet-fcip.c by
10 * Copyright 2001, Dinesh G Dutt (ddutt@cisco.com)
14 * Wireshark - Network traffic analyzer
15 * By Gerald Combs <gerald@wireshark.org>
16 * Copyright 1998 Gerald Combs
18 * This program is free software; you can redistribute it and/or
19 * modify it under the terms of the GNU General Public License
20 * as published by the Free Software Foundation; either version 2
21 * of the License, or (at your option) any later version.
23 * This program is distributed in the hope that it will be useful,
24 * but WITHOUT ANY WARRANTY; without even the implied warranty of
25 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26 * GNU General Public License for more details.
28 * You should have received a copy of the GNU General Public License
29 * along with this program; if not, write to the Free Software
30 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
43 #include <epan/packet.h>
44 #include <epan/prefs.h>
45 #include <epan/emem.h>
46 #include <epan/conversation.h>
47 #include "packet-tcp.h"
49 #define iFCP_ENCAP_HEADER_LEN 28
50 #define iFCP_MIN_HEADER_LEN 16 /* upto frame len field */
75 FCENCAP_PROTO_FCIP = 1,
76 FCENCAP_PROTO_iFCP = 2
79 static const value_string ifcp_eof_vals[] = {
82 {iFCP_EOFrt, "EOFrt" },
83 {iFCP_EOFdt, "EOFdt" },
84 {iFCP_EOFni, "EOFni" },
85 {iFCP_EOFdti, "EOFdti" },
86 {iFCP_EOFrti, "EOFrti" },
91 static const value_string ifcp_sof_vals[] = {
93 {iFCP_SOFi4, "SOFi4" },
94 {iFCP_SOFi2, "SOFi2" },
95 {iFCP_SOFi3, "SOFi3" },
96 {iFCP_SOFn4, "SOFn4" },
97 {iFCP_SOFn2, "SOFn2" },
98 {iFCP_SOFn3, "SOFn3" },
99 {iFCP_SOFc4, "SOFc4" },
103 static const value_string fcencap_proto_vals[] = {
104 {FCENCAP_PROTO_iFCP, "iFCP"},
105 {FCENCAP_PROTO_iFCP, "iFCP"},
109 static const guint8 ifcp_header_8_bytes[8] = {
110 0x02, 0x01, 0xFD, 0xFE,
111 0x00, 0x00, 0x00, 0x00
114 static int proto_ifcp = -1;
116 static int hf_ifcp_protocol = -1;
117 static int hf_ifcp_protocol_c = -1;
118 static int hf_ifcp_version = -1;
119 static int hf_ifcp_version_c = -1;
120 static int hf_ifcp_encap_flags_c=-1;
121 static int hf_ifcp_framelen = -1;
122 static int hf_ifcp_framelen_c = -1;
123 static int hf_ifcp_tsec = -1;
124 static int hf_ifcp_tusec = -1;
125 static int hf_ifcp_encap_crc = -1;
126 static int hf_ifcp_sof = -1;
127 static int hf_ifcp_sof_c = -1;
128 static int hf_ifcp_eof = -1;
129 static int hf_ifcp_eof_c = -1;
130 static int hf_ifcp_ls_command_acc = -1;
131 static int hf_ifcp_flags = -1;
132 static int hf_ifcp_flags_ses = -1;
133 static int hf_ifcp_flags_trp = -1;
134 static int hf_ifcp_flags_spc = -1;
135 static int hf_ifcp_common_flags = -1;
136 static int hf_ifcp_common_flags_crcv = -1;
138 static int ett_ifcp = -1;
139 static int ett_ifcp_sof = -1;
140 static int ett_ifcp_eof = -1;
141 static int ett_ifcp_flags = -1;
142 static int ett_ifcp_common_flags = -1;
143 static int ett_ifcp_protocol = -1;
144 static int ett_ifcp_version = -1;
145 static int ett_ifcp_frame_len = -1;
147 static gboolean ifcp_desegment = TRUE;
149 static dissector_handle_t ifcp_handle=NULL;
150 static dissector_handle_t data_handle=NULL;
151 static dissector_handle_t fc_handle=NULL;
154 /* This function checks the first 16 bytes of the "header" that it looks sane
155 * and returns TRUE if this looks like iFCP and FALSE if it doesnt.
158 ifcp_header_test(tvbuff_t *tvb, int offset)
162 /* we can only do this test if we have 16 bytes or more */
163 if(tvb_length_remaining(tvb, offset)<iFCP_MIN_HEADER_LEN){
168 * As per the iFCP 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 * iFCP 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 iFCP standard, in addition, at least 3 of the following
181 * set of tests must be performed to identify that we've located the
182 * start of an iFCP frame.
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); (DONT DO THIS 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 iFCP 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
205 if(tvb_memeql(tvb, offset, ifcp_header_8_bytes, 8) != 0){
209 /* check the frame length */
210 flen=tvb_get_ntohs(tvb, offset+12)&0x03FF;
211 if((flen < 15) || (flen > 545)){
215 /* check the complement of the frame length */
216 flen1=tvb_get_ntohs(tvb, offset+14)&0x03FF;
217 if(flen!=((~flen1)&0x03FF)){
222 /* this should be good enough for our heuristics */
227 #define IFCP_FLAGS_SES 0x04
228 #define IFCP_FLAGS_TRP 0x02
229 #define IFCP_FLAGS_SPC 0x01
231 static const true_false_string ifcp_flags_ses_tfs = {
232 "This is a SESSION CONTROL FRAME",
233 "This is a normal frame"
236 static const true_false_string ifcp_flags_trp_tfs = {
237 "Address TRANSPARENT Mode Enabled",
238 "Address TRANSLATION Mode Enabled"
241 static const true_false_string ifcp_flags_spc_tfs = {
242 "This frame requires SPECIAL PROCESSING",
243 "This is a normal frame"
247 dissect_ifcpflags(tvbuff_t *tvb, int offset, proto_tree *parent_tree)
249 proto_item *item=NULL;
250 proto_tree *tree=NULL;
254 item=proto_tree_add_item(parent_tree, hf_ifcp_flags, tvb, offset, 1, 0);
255 tree=proto_item_add_subtree (item, ett_ifcp_flags);
258 flags=tvb_get_guint8(tvb, offset);
261 proto_tree_add_boolean(tree, hf_ifcp_flags_ses, tvb, offset, 1, flags);
262 if(flags&IFCP_FLAGS_SES){
263 proto_item_append_text(item, " SES");
265 flags&=(~IFCP_FLAGS_SES);
268 proto_tree_add_boolean(tree, hf_ifcp_flags_trp, tvb, offset, 1, flags);
269 if(flags&IFCP_FLAGS_TRP){
270 proto_item_append_text(item, " TRP");
272 flags&=(~IFCP_FLAGS_TRP);
275 proto_tree_add_boolean(tree, hf_ifcp_flags_spc, tvb, offset, 1, flags);
276 if(flags&IFCP_FLAGS_SPC){
277 proto_item_append_text(item, " SPC");
279 flags&=(~IFCP_FLAGS_SPC);
287 #define IFCP_COMMON_FLAGS_CRCV 0x04
289 static const true_false_string ifcp_common_flags_crcv_tfs = {
295 dissect_commonflags(tvbuff_t *tvb, int offset, proto_tree *parent_tree)
297 proto_item *item=NULL;
298 proto_tree *tree=NULL;
302 item=proto_tree_add_item(parent_tree, hf_ifcp_common_flags, tvb, offset, 1, 0);
303 tree=proto_item_add_subtree (item, ett_ifcp_common_flags);
306 flags=tvb_get_guint8(tvb, offset);
309 proto_tree_add_boolean(tree, hf_ifcp_common_flags_crcv, tvb, offset, 1, flags);
310 if(flags&IFCP_COMMON_FLAGS_CRCV){
311 proto_item_append_text(item, " CRCV");
313 flags&=(~IFCP_COMMON_FLAGS_CRCV);
317 dissect_ifcp_pdu(tvbuff_t *tvb, packet_info *pinfo, proto_tree *parent_tree)
319 gint offset = 0, frame_len = 0;
320 guint8 sof = 0, eof = 0;
322 proto_tree *tree = NULL;
325 proto_tree *protocol_tree=NULL;
326 proto_tree *version_tree=NULL;
327 proto_tree *frame_len_tree=NULL;
328 proto_tree *sof_tree=NULL;
329 proto_tree *eof_tree=NULL;
331 /* verify we have a full header (do we need to do this? */
332 if(tvb_length(tvb)<iFCP_ENCAP_HEADER_LEN){
336 if(check_col(pinfo->cinfo, COL_PROTOCOL)){
337 col_set_str(pinfo->cinfo, COL_PROTOCOL, "iFCP");
340 frame_len = (tvb_get_ntohs (tvb, offset+12) & 0x03FF)*4;
344 if (tvb_bytes_exist (tvb, offset, frame_len-4)) {
345 sof = tvb_get_guint8 (tvb, offset+iFCP_ENCAP_HEADER_LEN);
346 eof = tvb_get_guint8 (tvb, offset+frame_len - 4);
348 ti = proto_tree_add_protocol_format (parent_tree, proto_ifcp, tvb, offset,
349 iFCP_ENCAP_HEADER_LEN,
351 val_to_str (sof, ifcp_sof_vals,
353 val_to_str (eof, ifcp_eof_vals,
356 sof = tvb_get_guint8 (tvb, offset+iFCP_ENCAP_HEADER_LEN);
358 ti = proto_tree_add_protocol_format (parent_tree, proto_ifcp, tvb, offset,
359 iFCP_ENCAP_HEADER_LEN,
361 val_to_str (sof, ifcp_sof_vals,
365 tree = proto_item_add_subtree (ti, ett_ifcp);
370 /* The Common FC Encap header */
372 protocol = tvb_get_guint8 (tvb, offset);
373 ti=proto_tree_add_item(tree, hf_ifcp_protocol, tvb, offset, 1, 0);
375 protocol_tree=proto_item_add_subtree(ti, ett_ifcp_protocol);
380 ti=proto_tree_add_item(tree, hf_ifcp_version, tvb, offset, 1, 0);
382 version_tree=proto_item_add_subtree(ti, ett_ifcp_version);
386 /* protocol complement */
387 proto_tree_add_item(protocol_tree, hf_ifcp_protocol_c, tvb, offset, 1, 0);
390 /* version complement */
391 proto_tree_add_item(version_tree, hf_ifcp_version_c, tvb, offset, 1, 0);
394 /* 4 reserved bytes */
397 /* iFCP specific fields */
398 if(protocol==FCENCAP_PROTO_iFCP){
400 proto_tree_add_item(tree, hf_ifcp_ls_command_acc, tvb, offset, 1, 0);
404 offset=dissect_ifcpflags(tvb, offset, tree);
407 ti=proto_tree_add_item(tree, hf_ifcp_sof, tvb, offset, 1, 0);
409 sof_tree=proto_item_add_subtree(ti, ett_ifcp_sof);
414 ti=proto_tree_add_item(tree, hf_ifcp_eof, tvb, offset, 1, 0);
416 eof_tree=proto_item_add_subtree(ti, ett_ifcp_eof);
421 sof_tree=tree; /* better than nothing */
426 dissect_commonflags(tvb, offset, tree);
429 ti=proto_tree_add_item(tree, hf_ifcp_framelen, tvb, offset, 2, 0);
431 frame_len_tree=proto_item_add_subtree(ti, ett_ifcp_frame_len);
435 /* complement of flags and frame len */
436 proto_tree_add_item(frame_len_tree, hf_ifcp_encap_flags_c, tvb, offset, 1, 0);
437 proto_tree_add_item(frame_len_tree, hf_ifcp_framelen_c, tvb, offset, 2, 0);
440 /* timestamp seconds */
441 proto_tree_add_item(tree, hf_ifcp_tsec, tvb, offset, 4, 0);
444 /* timestamp fractions */
445 proto_tree_add_item(tree, hf_ifcp_tusec, tvb, offset, 4, 0);
449 proto_tree_add_item(tree, hf_ifcp_encap_crc, tvb, offset, 4, 0);
454 proto_tree_add_item(sof_tree, hf_ifcp_sof, tvb, offset, 1, 0);
456 proto_tree_add_item(sof_tree, hf_ifcp_sof, tvb, offset, 1, 0);
458 proto_tree_add_item(sof_tree, hf_ifcp_sof_c, tvb, offset, 1, 0);
460 proto_tree_add_item(sof_tree, hf_ifcp_sof_c, tvb, offset, 1, 0);
464 if(tvb_bytes_exist(tvb, frame_len-4, 4)) {
465 proto_tree_add_item(eof_tree, hf_ifcp_eof, tvb, frame_len-4, 1, 0);
466 proto_tree_add_item(eof_tree, hf_ifcp_eof, tvb, frame_len-3, 1, 0);
467 proto_tree_add_item(eof_tree, hf_ifcp_eof_c, tvb, frame_len-2, 1, 0);
468 proto_tree_add_item(eof_tree, hf_ifcp_eof_c, tvb, frame_len-1, 1, 0);
472 /* Call the FC Dissector if this is carrying an FC frame */
473 /* Set the SOF/EOF flags in the packet_info header */
480 pinfo->sof_eof = PINFO_SOF_FIRST_FRAME;
483 pinfo->sof_eof = PINFO_SOF_SOFF;
487 if (eof != iFCP_EOFn) {
488 pinfo->sof_eof |= PINFO_EOF_LAST_FRAME;
489 } else if (eof != iFCP_EOFt) {
490 pinfo->sof_eof |= PINFO_EOF_INVALID;
495 next_tvb=tvb_new_subset(tvb, offset, frame_len-offset-4, frame_len-offset-4);
498 call_dissector(fc_handle, next_tvb, pinfo, parent_tree);
499 } else if(data_handle){
500 call_dissector(data_handle, next_tvb, pinfo, parent_tree);
507 get_ifcp_pdu_len(packet_info *pinfo _U_, tvbuff_t *tvb, int offset)
511 if(!ifcp_header_test(tvb, offset)){
515 pdu_len=tvb_get_ntohs(tvb, offset+12)*4;
520 dissect_ifcp(tvbuff_t *tvb, packet_info *pinfo, proto_tree *parent_tree)
522 tcp_dissect_pdus(tvb, pinfo, parent_tree, ifcp_desegment, iFCP_MIN_HEADER_LEN, get_ifcp_pdu_len, dissect_ifcp_pdu);
526 /* This is called for those sessions where we have explicitely said
527 * this to be iFCP using "Decode As..."
528 * In this case we will not check the port number for sanity and just
529 * do as the user said.
532 dissect_ifcp_handle(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
534 dissect_ifcp(tvb, pinfo, tree);
538 dissect_ifcp_heur(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
540 if(!ifcp_header_test(tvb, 0)){
544 dissect_ifcp(tvb, pinfo, tree);
546 /* our heuristics are so strong that if the heuristics above passed
547 * and the dissection of the pdu did not cause any exceptions
548 * then we can set this as our conversation dissector
551 conversation_t* ifcp_conv;
553 ifcp_conv=find_conversation(pinfo->fd->num, &pinfo->src, &pinfo->dst, pinfo->ptype, pinfo->srcport, pinfo->destport, 0);
555 ifcp_conv=conversation_new(pinfo->fd->num, &pinfo->src,
556 &pinfo->dst, pinfo->ptype, pinfo->srcport,
559 /* XXX why does this not work? it doesnt result in dissect_ifcp_handle being called look into later*/
560 conversation_set_dissector(ifcp_conv, ifcp_handle);
567 proto_register_ifcp (void)
570 /* Setup list of header fields See Section 1.6.1 for details*/
571 static hf_register_info hf[] = {
573 { "Protocol", "fcencap.proto", FT_UINT8, BASE_DEC,
574 VALS(fcencap_proto_vals), 0, "Protocol", HFILL }},
575 { &hf_ifcp_protocol_c,
576 {"Protocol (1's Complement)", "fcencap.protoc", FT_UINT8, BASE_DEC, NULL,
577 0, "Protocol (1's Complement)", HFILL}},
579 {"Version", "fcencap.version", FT_UINT8, BASE_DEC, NULL, 0, "",
581 { &hf_ifcp_version_c,
582 {"Version (1's Complement)", "fcencap.versionc", FT_UINT8, BASE_DEC,
583 NULL, 0, "", HFILL}},
584 { &hf_ifcp_encap_flags_c,
585 {"iFCP Encapsulation Flags (1's Complement)", "ifcp.encap_flagsc", FT_UINT8, BASE_HEX,
586 NULL, 0xFC, "", HFILL}},
588 {"Frame Length (in Words)", "fcencap.framelen", FT_UINT16, BASE_DEC,
589 NULL, 0x03FF, "", HFILL}},
590 { &hf_ifcp_framelen_c,
591 {"Frame Length (1's Complement)", "fcencap.framelenc", FT_UINT16,
592 BASE_DEC, NULL, 0x03FF, "", HFILL}},
594 {"Time (secs)", "fcencap.tsec", FT_UINT32, BASE_DEC, NULL, 0, "",
597 {"Time (fraction)", "fcencap.tusec", FT_UINT32, BASE_DEC, NULL, 0,
599 { &hf_ifcp_encap_crc,
600 {"CRC", "fcencap.crc", FT_UINT32, BASE_HEX, NULL, 0, "", HFILL}},
602 {"SOF", "ifcp.sof", FT_UINT8, BASE_HEX, VALS (&ifcp_sof_vals), 0,
605 {"EOF", "ifcp.eof", FT_UINT8, BASE_HEX, VALS (&ifcp_eof_vals), 0,
608 {"SOF Compliment", "ifcp.sof_c", FT_UINT8, BASE_HEX, NULL , 0,
611 {"EOF Compliment", "ifcp.eof_c", FT_UINT8, BASE_HEX, NULL , 0,
613 { &hf_ifcp_ls_command_acc,
614 {"Ls Command Acc", "ifcp.ls_command_acc", FT_UINT8, BASE_HEX, NULL, 0,
616 { &hf_ifcp_common_flags,
617 {"Flags", "ifcp.common_flags", FT_UINT8, BASE_HEX , NULL, 0xfc, "", HFILL }},
618 { &hf_ifcp_common_flags_crcv,
619 {"CRCV", "ifcp.common_flags.crcv", FT_BOOLEAN, 8, TFS(&ifcp_common_flags_crcv_tfs), IFCP_COMMON_FLAGS_CRCV, "Is the CRC field valid?", HFILL }},
621 {"iFCP Flags", "ifcp.flags", FT_UINT8, BASE_HEX , NULL, 0, "", HFILL }},
622 { &hf_ifcp_flags_ses,
623 {"SES", "ifcp.flags.ses", FT_BOOLEAN, 8, TFS(&ifcp_flags_ses_tfs), IFCP_FLAGS_SES, "Is this a Session control frame", HFILL }},
624 { &hf_ifcp_flags_trp,
625 {"TRP", "ifcp.flags.trp", FT_BOOLEAN, 8, TFS(&ifcp_flags_trp_tfs), IFCP_FLAGS_TRP, "Is address transparent mode enabled", HFILL }},
626 { &hf_ifcp_flags_spc,
627 {"SPC", "ifcp.flags.spc", FT_BOOLEAN, 8, TFS(&ifcp_flags_spc_tfs), IFCP_FLAGS_SPC, "Is frame part of link service", HFILL }},
630 static gint *ett[] = {
638 &ett_ifcp_common_flags,
641 module_t *ifcp_module;
643 /* Register the protocol name and description */
644 proto_ifcp = proto_register_protocol("iFCP", "iFCP", "ifcp");
646 /* Required function calls to register the header fields and
648 proto_register_field_array(proto_ifcp, hf, array_length(hf));
649 proto_register_subtree_array(ett, array_length(ett));
651 ifcp_module = prefs_register_protocol(proto_ifcp, NULL);
652 prefs_register_bool_preference(ifcp_module,
654 "Reassemble iFCP messages spanning multiple TCP segments",
655 "Whether the iFCP dissector should reassemble messages spanning multiple TCP segments."
656 " To use this option, you must also enable \"Allow subdissectors to reassemble TCP streams\" in the TCP protocol settings.",
658 prefs_register_obsolete_preference(ifcp_module, "target_port");
663 * If this dissector uses sub-dissector registration add a
664 * registration routine.
668 * This format is required because a script is used to find these
669 * routines and create the code that calls these routines.
672 proto_reg_handoff_ifcp (void)
674 heur_dissector_add("tcp", dissect_ifcp_heur, proto_ifcp);
676 ifcp_handle = create_dissector_handle(dissect_ifcp_handle, proto_ifcp);
677 dissector_add_handle("tcp.port", ifcp_handle);
679 data_handle = find_dissector("data");
680 fc_handle = find_dissector("fc_ifcp");