epan/dissectors/pidl/samr/samr.cnf cnf_dissect_lsa_BinaryString => lsarpc_dissect_str...
[wireshark-sm.git] / epan / dissectors / packet-fcoib.c
blobe80177f0497afe2699ace2e616bf312002cfc716
1 /*
2 * packet-fcoib.c
3 * Routines for FCoIB dissection - Fibre Channel over Infiniband
4 * Copyright (c) 2010 Mellanox Technologies Ltd. (slavak@mellanox.co.il)
6 * Wireshark - Network traffic analyzer
7 * By Gerald Combs <gerald@wireshark.org>
8 * Copyright 1998 Gerald Combs
10 * Based on packet-fcoe.c, Copyright (c) 2006 Nuova Systems, Inc. (jre@nuovasystems.com)
12 * SPDX-License-Identifier: GPL-2.0-or-later
15 #include "config.h"
17 #include <stdlib.h>
19 #include <epan/packet.h>
20 #include <epan/prefs.h>
21 #include <epan/crc32-tvb.h>
22 #include <epan/expert.h>
23 #include <epan/addr_resolv.h>
24 #include "packet-fc.h"
26 void proto_register_fcoib(void);
27 void proto_reg_handoff_fcoib(void);
29 #define FCOIB_HEADER_LEN 16 /* header: encap. header, SOF, and padding */
30 #define FCOIB_TRAILER_LEN 8 /* trailer: FC-CRC, EOF and padding */
31 #define FCOIB_VER_OFFSET 2 /* offset of ver field (in bytes) inside FCoIB Encap. header */
33 typedef enum {
34 FCOIB_EOFn = 0x41,
35 FCOIB_EOFt = 0x42,
36 FCOIB_EOFrt = 0x44,
37 FCOIB_EOFdt = 0x46,
38 FCOIB_EOFni = 0x49,
39 FCOIB_EOFdti = 0x4E,
40 FCOIB_EOFrti = 0x4F,
41 FCOIB_EOFa = 0x50
42 } fcoib_eof_t;
44 typedef enum {
45 FCOIB_SOFf = 0x28,
46 FCOIB_SOFi4 = 0x29,
47 FCOIB_SOFi2 = 0x2D,
48 FCOIB_SOFi3 = 0x2E,
49 FCOIB_SOFn4 = 0x31,
50 FCOIB_SOFn2 = 0x35,
51 FCOIB_SOFn3 = 0x36,
52 FCOIB_SOFc4 = 0x39
53 } fcoib_sof_t;
55 static const value_string fcoib_eof_vals[] = {
56 {FCOIB_EOFn, "EOFn" },
57 {FCOIB_EOFt, "EOFt" },
58 {FCOIB_EOFrt, "EOFrt" },
59 {FCOIB_EOFdt, "EOFdt" },
60 {FCOIB_EOFni, "EOFni" },
61 {FCOIB_EOFdti, "EOFdti" },
62 {FCOIB_EOFrti, "EOFrti" },
63 {FCOIB_EOFa, "EOFa" },
64 {0, NULL}
67 static const value_string fcoib_sof_vals[] = {
68 {FCOIB_SOFf, "SOFf" },
69 {FCOIB_SOFi4, "SOFi4" },
70 {FCOIB_SOFi2, "SOFi2" },
71 {FCOIB_SOFi3, "SOFi3" },
72 {FCOIB_SOFn4, "SOFn4" },
73 {FCOIB_SOFn2, "SOFn2" },
74 {FCOIB_SOFn3, "SOFn3" },
75 {FCOIB_SOFc4, "SOFc4" },
76 {0, NULL}
79 static int proto_fcoib;
80 static int hf_fcoib_ver;
81 static int hf_fcoib_sig;
82 static int hf_fcoib_sof;
83 static int hf_fcoib_eof;
84 static int hf_fcoib_crc;
85 static int hf_fcoib_crc_status;
87 static int ett_fcoib;
89 static expert_field ei_fcoib_crc;
91 static dissector_handle_t fc_handle;
93 static int
94 dissect_fcoib(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, void *data _U_)
96 int crc_offset;
97 int eof_offset;
98 int sof_offset;
99 int frame_len;
100 unsigned version;
101 const char *ver;
102 uint8_t sof = 0;
103 uint8_t eof = 0;
104 uint8_t sig = 0;
105 const char *eof_str;
106 const char *sof_str;
107 const char *crc_msg;
108 const char *len_msg;
109 proto_item *ti;
110 proto_tree *fcoib_tree;
111 tvbuff_t *next_tvb;
112 bool crc_exists;
113 uint32_t crc_computed = 0;
114 uint32_t crc = 0;
115 fc_data_t fc_data;
117 frame_len = tvb_reported_length_remaining(tvb, 0) -
118 FCOIB_HEADER_LEN - FCOIB_TRAILER_LEN;
119 crc_offset = FCOIB_HEADER_LEN + frame_len;
120 eof_offset = crc_offset + 4;
121 sof_offset = FCOIB_HEADER_LEN - 1;
123 if (frame_len <= 0)
124 return 0; /* this packet isn't even long enough to contain the header+trailer w/o FC payload! */
126 col_set_str(pinfo->cinfo, COL_PROTOCOL, "FCoIB");
127 next_tvb = tvb_new_subset_length(tvb, FCOIB_HEADER_LEN, frame_len);
130 * Only version 0 is defined at this point.
131 * Don't print the version in the short summary if it is zero.
133 ver = "";
134 version = tvb_get_uint8(tvb, 0 + FCOIB_VER_OFFSET) >> 4;
135 if (version != 0)
136 ver = wmem_strdup_printf(pinfo->pool, ver, "ver %d ", version);
138 if (tvb_bytes_exist(tvb, 0, 1))
139 sig = tvb_get_uint8(tvb, 0) >> 6;
141 eof_str = "none";
142 if (tvb_bytes_exist(tvb, eof_offset, 1)) {
143 eof = tvb_get_uint8(tvb, eof_offset);
144 eof_str = val_to_str(eof, fcoib_eof_vals, "0x%x");
147 sof_str = "none";
148 if (tvb_bytes_exist(tvb, sof_offset, 1)) {
149 sof = tvb_get_uint8(tvb, sof_offset);
150 sof_str = val_to_str(sof, fcoib_sof_vals, "0x%x");
154 * Check the CRC.
156 crc_msg = "";
157 crc_exists = tvb_bytes_exist(tvb, crc_offset, 4);
158 if (crc_exists) {
159 crc = tvb_get_ntohl(tvb, crc_offset);
160 crc_computed = crc32_802_tvb(next_tvb, frame_len);
161 if (crc != crc_computed) {
162 crc_msg = " [bad FC CRC]";
165 len_msg = "";
166 if ((frame_len % 4) != 0 || frame_len < 24) {
167 len_msg = " [invalid length]";
170 ti = proto_tree_add_protocol_format(tree, proto_fcoib, tvb, 0,
171 FCOIB_HEADER_LEN,
172 "FCoIB %s(%s/%s) %d bytes%s%s", ver,
173 sof_str, eof_str,
174 frame_len, crc_msg,
175 len_msg);
177 /* Dissect the FCoIB Encapsulation header */
179 fcoib_tree = proto_item_add_subtree(ti, ett_fcoib);
180 proto_tree_add_uint(fcoib_tree, hf_fcoib_sig, tvb, 0, 1, sig);
181 proto_tree_add_uint(fcoib_tree, hf_fcoib_ver, tvb, FCOIB_VER_OFFSET, 1, version);
182 proto_tree_add_uint(fcoib_tree, hf_fcoib_sof, tvb, sof_offset, 1, sof);
185 * Create the CRC information.
187 if (crc_exists) {
188 proto_tree_add_checksum(fcoib_tree, tvb, crc_offset, hf_fcoib_crc, hf_fcoib_crc_status, &ei_fcoib_crc, pinfo, crc_computed, ENC_BIG_ENDIAN, PROTO_CHECKSUM_VERIFY);
189 proto_tree_set_appendix(fcoib_tree, tvb, crc_offset,
190 tvb_captured_length_remaining (tvb, crc_offset));
191 } else {
192 proto_tree_add_checksum(fcoib_tree, tvb, crc_offset, hf_fcoib_crc, hf_fcoib_crc_status, &ei_fcoib_crc, pinfo, 0, ENC_BIG_ENDIAN, PROTO_CHECKSUM_NOT_PRESENT);
196 * Interpret the EOF.
198 if (tvb_bytes_exist(tvb, eof_offset, 1)) {
199 proto_tree_add_item(fcoib_tree, hf_fcoib_eof, tvb, eof_offset, 1, ENC_BIG_ENDIAN);
202 /* Set the SOF/EOF flags in the packet_info header */
203 fc_data.sof_eof = 0;
204 if (sof == FCOIB_SOFi3 || sof == FCOIB_SOFi2 || sof == FCOIB_SOFi4) {
205 fc_data.sof_eof = FC_DATA_SOF_FIRST_FRAME;
206 } else if (sof == FCOIB_SOFf) {
207 fc_data.sof_eof = FC_DATA_SOF_SOFF;
210 if (eof != FCOIB_EOFn) {
211 fc_data.sof_eof |= FC_DATA_EOF_LAST_FRAME;
212 if (eof != FCOIB_EOFt) {
213 fc_data.sof_eof |= FC_DATA_EOF_INVALID;
217 /* Call the FC Dissector if this is carrying an FC frame */
218 fc_data.ethertype = ETHERTYPE_UNK;
220 if (fc_handle) {
221 call_dissector_with_data(fc_handle, next_tvb, pinfo, tree, &fc_data);
222 } else {
223 call_data_dissector(next_tvb, pinfo, tree);
226 return tvb_captured_length(tvb);
229 static bool
230 dissect_fcoib_heur(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, void *data)
232 int crc_offset;
233 int eof_offset;
234 int sof_offset;
235 int frame_len;
236 uint8_t sof = 0;
237 uint8_t eof = 0;
238 uint8_t sig = 0;
240 frame_len = tvb_reported_length_remaining(tvb, 0) -
241 FCOIB_HEADER_LEN - FCOIB_TRAILER_LEN;
242 crc_offset = FCOIB_HEADER_LEN + frame_len;
243 eof_offset = crc_offset + 4;
244 sof_offset = FCOIB_HEADER_LEN - 1;
246 if (frame_len <= 0)
247 return false; /* this packet isn't even long enough to contain the header+trailer w/o FC payload! */
249 /* we start off with some basic heuristics checks to make sure this could be a FCoIB packet */
251 if (tvb_bytes_exist(tvb, 0, 1))
252 sig = tvb_get_uint8(tvb, 0) >> 6;
253 if (tvb_bytes_exist(tvb, eof_offset, 1))
254 eof = tvb_get_uint8(tvb, eof_offset);
255 if (tvb_bytes_exist(tvb, sof_offset, 1))
256 sof = tvb_get_uint8(tvb, sof_offset);
258 if (sig != 1)
259 return false; /* the sig field in the FCoIB Encap. header MUST be 2'b01*/
260 if (!tvb_bytes_exist(tvb, eof_offset + 1, 3) || tvb_get_ntoh24(tvb, eof_offset + 1) != 0)
261 return false; /* 3 bytes of RESERVED field immediately after eEOF MUST be 0 */
262 if (!try_val_to_str(sof, fcoib_sof_vals))
263 return false; /* invalid value for SOF */
264 if (!try_val_to_str(eof, fcoib_eof_vals))
265 return false; /* invalid value for EOF */
267 dissect_fcoib(tvb, pinfo, tree, data);
268 return true;
271 void
272 proto_register_fcoib(void)
274 module_t *fcoib_module;
276 /* Setup list of header fields See Section 1.6.1 for details*/
277 static hf_register_info hf[] = {
278 { &hf_fcoib_sof,
279 {"SOF", "fcoib.sof", FT_UINT8, BASE_HEX, VALS(fcoib_sof_vals), 0,
280 NULL, HFILL}},
281 { &hf_fcoib_eof,
282 {"EOF", "fcoib.eof", FT_UINT8, BASE_HEX, VALS(fcoib_eof_vals), 0,
283 NULL, HFILL}},
284 { &hf_fcoib_sig,
285 {"Signature", "fcoib.sig", FT_UINT8, BASE_HEX, NULL, 0, NULL, HFILL}},
286 { &hf_fcoib_ver,
287 {"Version", "fcoib.ver", FT_UINT32, BASE_DEC, NULL, 0, NULL, HFILL}},
288 { &hf_fcoib_crc,
289 {"CRC", "fcoib.crc", FT_UINT32, BASE_HEX, NULL, 0, NULL, HFILL}},
290 { &hf_fcoib_crc_status,
291 {"CRC Status", "fcoib.crc.status", FT_UINT8, BASE_NONE, VALS(proto_checksum_vals), 0x0,
292 NULL, HFILL }},
294 static int *ett[] = {
295 &ett_fcoib,
298 static ei_register_info ei[] = {
299 { &ei_fcoib_crc, { "fcoib.crc.bad", PI_CHECKSUM, PI_ERROR, "Bad checksum", EXPFILL }},
302 expert_module_t* expert_fcoib;
304 /* Register the protocol name and description */
305 proto_fcoib = proto_register_protocol("Fibre Channel over Infiniband",
306 "FCoIB", "fcoib");
308 /* Required function calls to register the header fields and
309 * subtrees used */
310 proto_register_field_array(proto_fcoib, hf, array_length(hf));
311 proto_register_subtree_array(ett, array_length(ett));
312 expert_fcoib = expert_register_protocol(proto_fcoib);
313 expert_register_field_array(expert_fcoib, ei, array_length(ei));
315 fcoib_module = prefs_register_protocol(proto_fcoib, NULL);
317 prefs_register_static_text_preference(fcoib_module, "use_decode_as",
318 "Heuristic matching preferences removed. Use Infiniband protocol preferences or Decode As.",
319 "Simple heuristics can still be enable (may generate false positives) through Infiniband protocol preferences."
320 "To force FCoIB dissection use Decode As");
322 prefs_register_obsolete_preference(fcoib_module, "heur_en");
323 prefs_register_obsolete_preference(fcoib_module, "manual_en");
325 prefs_register_obsolete_preference(fcoib_module, "addr_a");
326 prefs_register_obsolete_preference(fcoib_module, "addr_a_type");
327 prefs_register_obsolete_preference(fcoib_module, "addr_a_id");
328 prefs_register_obsolete_preference(fcoib_module, "addr_a_qp");
330 prefs_register_obsolete_preference(fcoib_module, "addr_b");
331 prefs_register_obsolete_preference(fcoib_module, "addr_b_type");
332 prefs_register_obsolete_preference(fcoib_module, "addr_b_id");
333 prefs_register_obsolete_preference(fcoib_module, "addr_b_qp");
336 void
337 proto_reg_handoff_fcoib(void)
339 heur_dissector_add("infiniband.payload", dissect_fcoib_heur, "Fibre Channel over Infiniband", "fc_infiniband", proto_fcoib, HEURISTIC_ENABLE);
341 dissector_add_for_decode_as("infiniband", create_dissector_handle( dissect_fcoib, proto_fcoib ) );
343 fc_handle = find_dissector_add_dependency("fc", proto_fcoib);
347 * Editor modelines - https://www.wireshark.org/tools/modelines.html
349 * Local variables:
350 * c-basic-offset: 4
351 * tab-width: 8
352 * indent-tabs-mode: nil
353 * End:
355 * vi: set shiftwidth=4 tabstop=8 expandtab:
356 * :indentSize=4:tabSize=8:noTabs=true: