8accf2eb8e5842b468f08fffd77ef21adafb1830
[metze/wireshark/wip.git] / epan / dissectors / packet-fcoe.c
1 /*
2  * packet-fcoe.c
3  * Routines for FCoE dissection - Fibre Channel over Ethernet
4  * Copyright (c) 2006 Nuova Systems, Inc. (jre@nuovasystems.com)
5  *
6  * $Id$
7  *
8  * Wireshark - Network traffic analyzer
9  * By Gerald Combs <gerald@wireshark.org>
10  * Copyright 1998 Gerald Combs
11  *
12  * Based on packet-fcip.c, Copyright 2001, Dinesh G Dutt (ddutt@cisco.com)
13  *
14  * This program is free software; you can redistribute it and/or
15  * modify it under the terms of the GNU General Public License
16  * as published by the Free Software Foundation; either version 2
17  * of the License, or (at your option) any later version.
18  *
19  * This program is distributed in the hope that it will be useful,
20  * but WITHOUT ANY WARRANTY; without even the implied warranty of
21  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22  * GNU General Public License for more details.
23  *
24  * You should have received a copy of the GNU General Public License
25  * along with this program; if not, write to the Free Software
26  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
27  */
28
29 /*
30  * For FCoE protocol details, see http://fcoe.com.
31  */
32
33 #include "config.h"
34
35 #include <glib.h>
36
37 #include <epan/packet.h>
38 #include <epan/prefs.h>
39 #include <epan/crc32-tvb.h>
40 #include <epan/etypes.h>
41 #include <epan/expert.h>
42
43 #define FCOE_HEADER_LEN   14        /* header: version, SOF, and padding */
44 #define FCOE_TRAILER_LEN   8        /* trailer: CRC, EOF, and padding */
45
46 typedef enum {
47     FCOE_EOFn    = 0x41,
48     FCOE_EOFt    = 0x42,
49     FCOE_EOFrt   = 0x44,
50     FCOE_EOFdt   = 0x46,
51     FCOE_EOFni   = 0x49,
52     FCOE_EOFdti  = 0x4E,
53     FCOE_EOFrti  = 0x4F,
54     FCOE_EOFa    = 0x50
55 } fcoe_eof_t;
56
57 typedef enum {
58     FCOE_SOFf    = 0x28,
59     FCOE_SOFi4   = 0x29,
60     FCOE_SOFi2   = 0x2D,
61     FCOE_SOFi3   = 0x2E,
62     FCOE_SOFn4   = 0x31,
63     FCOE_SOFn2   = 0x35,
64     FCOE_SOFn3   = 0x36,
65     FCOE_SOFc4   = 0x39
66 } fcoe_sof_t;
67
68 static const value_string fcoe_eof_vals[] = {
69     {FCOE_EOFn,   "EOFn" },
70     {FCOE_EOFt,   "EOFt" },
71     {FCOE_EOFrt,  "EOFrt" },
72     {FCOE_EOFdt,  "EOFdt" },
73     {FCOE_EOFni,  "EOFni" },
74     {FCOE_EOFdti, "EOFdti" },
75     {FCOE_EOFrti, "EOFrti" },
76     {FCOE_EOFa,   "EOFa" },
77     {0, NULL}
78 };
79
80 static const value_string fcoe_sof_vals[] = {
81     {FCOE_SOFf, "SOFf" },
82     {FCOE_SOFi4, "SOFi4" },
83     {FCOE_SOFi2, "SOFi2" },
84     {FCOE_SOFi3, "SOFi3" },
85     {FCOE_SOFn4, "SOFn4" },
86     {FCOE_SOFn2, "SOFn2" },
87     {FCOE_SOFn3, "SOFn3" },
88     {FCOE_SOFc4, "SOFc4" },
89     {0, NULL}
90 };
91
92 static int proto_fcoe          = -1;
93 static int hf_fcoe_ver         = -1;
94 static int hf_fcoe_len         = -1;
95 static int hf_fcoe_sof         = -1;
96 static int hf_fcoe_eof         = -1;
97 static int hf_fcoe_crc         = -1;
98 static int hf_fcoe_crc_bad     = -1;
99 static int hf_fcoe_crc_good    = -1;
100
101 static int ett_fcoe            = -1;
102 static int ett_fcoe_crc        = -1;
103
104 static dissector_handle_t data_handle;
105 static dissector_handle_t fc_handle;
106
107 static void
108 dissect_fcoe(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
109 {
110     gint        crc_offset;
111     gint        eof_offset;
112     gint        frame_len    = 0;
113     gint        header_len   = FCOE_HEADER_LEN;
114     guint       version;
115     const char *ver;
116     guint16     len_sof;
117     gint        bytes_remaining;
118     guint8      sof          = 0;
119     guint8      eof          = 0;
120     const char *eof_str;
121     const char *crc_msg;
122     const char *len_msg;
123     proto_item *ti;
124     proto_item *item;
125     proto_tree *fcoe_tree;
126     proto_tree *crc_tree;
127     tvbuff_t   *next_tvb;
128     gboolean    crc_exists;
129     guint32     crc_computed = 0;
130     guint32     crc          = 0;
131
132     /*
133      * For now, handle both the version defined before and after August 2007.
134      * In the newer version, byte 1 is reserved and always zero.  In the old
135      * version, it'll never be zero.
136      */
137     if (tvb_get_guint8(tvb, 1)) {
138         header_len = 2;
139         len_sof = tvb_get_ntohs(tvb, 0);
140         frame_len = ((len_sof & 0x3ff0) >> 2) - 4;
141         sof = len_sof & 0xf;
142         sof |= (sof < 8) ? 0x30 : 0x20;
143         version = len_sof >> 14;
144         ver = "pre-T11 ";
145         if (version != 0)
146             ver = ep_strdup_printf(ver, "pre-T11 ver %d ", version);
147     } else {
148         frame_len = tvb_reported_length_remaining(tvb, 0) -
149           FCOE_HEADER_LEN - FCOE_TRAILER_LEN;
150         sof = tvb_get_guint8(tvb, FCOE_HEADER_LEN - 1);
151
152         /*
153          * Only version 0 is defined at this point.
154          * Don't print the version in the short summary if it is zero.
155          */
156         ver = "";
157         version = tvb_get_guint8(tvb, 0) >> 4;
158         if (version != 0)
159             ver = ep_strdup_printf(ver, "ver %d ", version);
160     }
161     col_set_str(pinfo->cinfo, COL_PROTOCOL, "FCoE");
162     crc_offset = header_len + frame_len;
163     eof_offset = crc_offset + 4;
164     bytes_remaining = tvb_length_remaining(tvb, header_len);
165     if (bytes_remaining > frame_len)
166         bytes_remaining = frame_len;        /* backing length */
167     next_tvb = tvb_new_subset(tvb, header_len, bytes_remaining, frame_len);
168
169     eof_str = "none";
170     if (tvb_bytes_exist(tvb, eof_offset, 1)) {
171         eof = tvb_get_guint8(tvb, eof_offset);
172         eof_str = val_to_str(eof, fcoe_eof_vals, "0x%x");
173     }
174
175     /*
176      * Check the CRC.
177      */
178     crc_msg = "";
179     crc_exists = tvb_bytes_exist(tvb, crc_offset, 4);
180     if (crc_exists) {
181         crc = tvb_get_ntohl(tvb, crc_offset);
182         crc_computed = crc32_802_tvb(next_tvb, frame_len);
183         if (crc != crc_computed) {
184             crc_msg = " [bad FC CRC]";
185         }
186     }
187     len_msg = "";
188     if ((frame_len % 4) != 0 || frame_len < 24) {
189         len_msg = " [invalid length]";
190     }
191
192     ti = proto_tree_add_protocol_format(tree, proto_fcoe, tvb, 0,
193                                         header_len,
194                                         "FCoE %s(%s/%s) %d bytes%s%s", ver,
195                                         val_to_str(sof, fcoe_sof_vals,
196                                                    "0x%x"),
197                                         eof_str, frame_len, crc_msg,
198                                         len_msg);
199
200     /* Dissect the FCoE header */
201
202     fcoe_tree = proto_item_add_subtree(ti, ett_fcoe);
203     proto_tree_add_uint(fcoe_tree, hf_fcoe_ver, tvb, 0, 1, version);
204     if (tvb_get_guint8(tvb, 1)) {
205         proto_tree_add_uint(fcoe_tree, hf_fcoe_len, tvb, 0, 2, frame_len);
206     }
207     proto_tree_add_uint(fcoe_tree, hf_fcoe_sof, tvb,
208                         header_len - 1, 1, sof);
209
210     /*
211      * Create the CRC information.
212      */
213     if (crc_exists) {
214         if (crc == crc_computed) {
215             item = proto_tree_add_uint_format(fcoe_tree, hf_fcoe_crc, tvb,
216                                               crc_offset, 4, crc,
217                                               "CRC: %8.8x [valid]", crc);
218         } else {
219             item = proto_tree_add_uint_format(fcoe_tree, hf_fcoe_crc, tvb,
220                                               crc_offset, 4, crc,
221                                               "CRC: %8.8x "
222                                               "[error: should be %8.8x]",
223                                               crc, crc_computed);
224             expert_add_info_format(pinfo, item, PI_CHECKSUM, PI_ERROR,
225                                    "Bad FC CRC %8.8x %8.x",
226                                    crc, crc_computed);
227         }
228         proto_tree_set_appendix(fcoe_tree, tvb, crc_offset,
229                                 tvb_length_remaining (tvb, crc_offset));
230     } else {
231         item = proto_tree_add_text(fcoe_tree, tvb, crc_offset, 0,
232                                    "CRC: [missing]");
233     }
234     crc_tree = proto_item_add_subtree(item, ett_fcoe_crc);
235     ti = proto_tree_add_boolean(crc_tree, hf_fcoe_crc_bad, tvb,
236                                 crc_offset, 4,
237                                 crc_exists && crc != crc_computed);
238     PROTO_ITEM_SET_GENERATED(ti);
239     ti = proto_tree_add_boolean(crc_tree, hf_fcoe_crc_good, tvb,
240                                 crc_offset, 4,
241                                 crc_exists && crc == crc_computed);
242     PROTO_ITEM_SET_GENERATED(ti);
243
244     /*
245      * Interpret the EOF.
246      */
247     if (tvb_bytes_exist(tvb, eof_offset, 1)) {
248         proto_tree_add_item(fcoe_tree, hf_fcoe_eof, tvb, eof_offset, 1, ENC_BIG_ENDIAN);
249     }
250
251     /* Set the SOF/EOF flags in the packet_info header */
252     pinfo->sof_eof = 0;
253     if (sof == FCOE_SOFi3 || sof == FCOE_SOFi2 || sof == FCOE_SOFi4) {
254         pinfo->sof_eof = PINFO_SOF_FIRST_FRAME;
255     } else if (sof == FCOE_SOFf) {
256         pinfo->sof_eof = PINFO_SOF_SOFF;
257     }
258
259     if (eof != FCOE_EOFn) {
260         pinfo->sof_eof |= PINFO_EOF_LAST_FRAME;
261     } else if (eof != FCOE_EOFt) {
262         pinfo->sof_eof |= PINFO_EOF_INVALID;
263     }
264
265     /* Call the FC Dissector if this is carrying an FC frame */
266
267     if (fc_handle) {
268         call_dissector(fc_handle, next_tvb, pinfo, tree);
269     } else if (data_handle) {
270         call_dissector(data_handle, next_tvb, pinfo, tree);
271     }
272 }
273
274 void
275 proto_register_fcoe(void)
276 {
277     module_t *fcoe_module;
278
279     /* Setup list of header fields  See Section 1.6.1 for details*/
280     static hf_register_info hf[] = {
281         { &hf_fcoe_sof,
282           {"SOF", "fcoe.sof", FT_UINT8, BASE_HEX, VALS(fcoe_sof_vals), 0,
283            NULL, HFILL}},
284         { &hf_fcoe_eof,
285           {"EOF", "fcoe.eof", FT_UINT8, BASE_HEX, VALS(fcoe_eof_vals), 0,
286            NULL, HFILL}},
287         { &hf_fcoe_ver,
288           {"Version", "fcoe.ver", FT_UINT32, BASE_DEC, NULL, 0, NULL, HFILL}},
289         { &hf_fcoe_len,
290           {"Frame length", "fcoe.len", FT_UINT32,
291             BASE_DEC, NULL, 0, NULL, HFILL}},
292         { &hf_fcoe_crc,
293           {"CRC", "fcoe.crc", FT_UINT32, BASE_HEX, NULL, 0, NULL, HFILL}},
294         { &hf_fcoe_crc_good,
295           {"CRC good", "fcoe.crc_good", FT_BOOLEAN, BASE_NONE, NULL, 0x0,
296             "True: CRC matches packet content; False: doesn't match or not checked.", HFILL }},
297         { &hf_fcoe_crc_bad,
298           {"CRC bad", "fcoe.crc_bad", FT_BOOLEAN, BASE_NONE, NULL, 0x0,
299             "True: CRC doesn't match packet content; False: matches or not checked.", HFILL }}
300     };
301     static gint *ett[] = {
302         &ett_fcoe,
303         &ett_fcoe_crc
304     };
305
306     /* Register the protocol name and description */
307     proto_fcoe = proto_register_protocol("Fibre Channel over Ethernet",
308         "FCoE", "fcoe");
309
310     /* Required function calls to register the header fields and
311      * subtrees used */
312     proto_register_field_array(proto_fcoe, hf, array_length(hf));
313     proto_register_subtree_array(ett, array_length(ett));
314
315     fcoe_module = prefs_register_protocol(proto_fcoe, NULL);
316
317     prefs_register_obsolete_preference(fcoe_module, "ethertype");
318 }
319
320 void
321 proto_reg_handoff_fcoe(void)
322 {
323     dissector_handle_t fcoe_handle;
324
325     fcoe_handle = create_dissector_handle(dissect_fcoe, proto_fcoe);
326     dissector_add_uint("ethertype", ETHERTYPE_FCOE, fcoe_handle);
327     data_handle = find_dissector("data");
328     fc_handle   = find_dissector("fc");
329 }