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