2 * Routines for dissection of Cisco MDS Switch Internal Header
3 * Copyright 2001, Dinesh G Dutt <ddutt@andiamo.com>
5 * Wireshark - Network traffic analyzer
6 * By Gerald Combs <gerald@wireshark.org>
7 * Copyright 1998 Gerald Combs
9 * SPDX-License-Identifier: GPL-2.0-or-later
14 #include <epan/packet.h>
15 #include <epan/etypes.h>
16 #include <epan/prefs.h>
17 #include "packet-fc.h"
19 void proto_register_mdshdr(void);
20 void proto_reg_handoff_mdshdr(void);
22 #define MDSHDR_VERSION_OFFSET 0
24 /* Mdshdr Control bits */
25 #define MDSHDR_CTL_IDXDIRECT 1
26 #define MDSHDR_CTL_IGNACLO 2
27 #define MDSHDR_CTL_DRP 4
29 /* OFFSETS OF FIELDS */
30 #define MDSHDR_VER_OFFSET 0
31 #define MDSHDR_SOF_OFFSET 1
32 #define MDSHDR_PKTLEN_OFFSET 2
33 #define MDSHDR_DIDX_OFFSET 5
34 #define MDSHDR_SIDX_OFFSET 6
35 #define MDSHDR_VSAN_OFFSET 13
37 /* Two size definitions are sufficient */
38 #define MDSHDR_SIZE_BYTE sizeof(char)
39 #define MDSHDR_SIZE_INT16 sizeof(uint16_t)
40 #define MDSHDR_SIZE_INT32 sizeof(uint32_t)
42 /* Other miscellaneous defines; can't rely on sizeof structs */
43 #define MDSHDR_MAX_VERSION 0
44 #define MDSHDR_HEADER_SIZE 16
45 #define MDSHDR_TRAILER_SIZE 6
48 #define MDSHDR_SOFc1 0x1
49 #define MDSHDR_SOFi1 0x2
50 #define MDSHDR_SOFn1 0x3
51 #define MDSHDR_SOFi2 0x4
52 #define MDSHDR_SOFn2 0x5
53 #define MDSHDR_SOFi3 0x6
54 #define MDSHDR_SOFn3 0x7
55 #define MDSHDR_SOFf 0x8
56 #define MDSHDR_SOFc4 0x9
57 #define MDSHDR_SOFi4 0xa
58 #define MDSHDR_SOFn4 0xb
61 #define MDSHDR_EOFt 0x1
62 #define MDSHDR_EOFdt 0x2
63 #define MDSHDR_EOFa 0x4
64 #define MDSHDR_EOFn 0x3
65 #define MDSHDR_EOFdti 0x6
66 #define MDSHDR_EOFni 0x7
67 #define MDSHDR_EOFrt 0xa
68 #define MDSHDR_EOFrti 0xe
69 #define MDSHDR_EOF_UNKNOWN 0xb
71 /* Initialize the protocol and registered fields */
72 static int proto_mdshdr
;
73 static int hf_mdshdr_sof
;
74 static int hf_mdshdr_pkt_len
;
75 static int hf_mdshdr_dstidx
;
76 static int hf_mdshdr_srcidx
;
77 static int hf_mdshdr_vsan
;
78 static int hf_mdshdr_eof
;
79 static int hf_mdshdr_no_trailer
;
80 static int hf_mdshdr_span
;
81 static int hf_mdshdr_fccrc
;
83 /* Initialize the subtree pointers */
84 static int ett_mdshdr
;
85 static int ett_mdshdr_hdr
;
86 static int ett_mdshdr_trlr
;
88 static dissector_handle_t mdshdr_handle
;
89 static dissector_handle_t fc_dissector_handle
;
91 static bool decode_if_zero_etype
;
93 static const value_string sof_vals
[] = {
94 {MDSHDR_SOFc1
, "SOFc1"},
95 {MDSHDR_SOFi1
, "SOFi1"},
96 {MDSHDR_SOFn1
, "SOFn1"},
97 {MDSHDR_SOFi2
, "SOFi2"},
98 {MDSHDR_SOFn2
, "SOFn2"},
99 {MDSHDR_SOFi3
, "SOFi3"},
100 {MDSHDR_SOFn3
, "SOFn3"},
101 {MDSHDR_SOFc4
, "SOFc4"},
102 {MDSHDR_SOFi4
, "SOFi4"},
103 {MDSHDR_SOFn4
, "SOFn4"},
104 {MDSHDR_SOFf
, "SOFf"},
108 static const value_string eof_vals
[] = {
109 {MDSHDR_EOFt
, "EOFt"},
110 {MDSHDR_EOFdt
, "EOFdt"},
111 {MDSHDR_EOFa
, "EOFa"},
112 {MDSHDR_EOFn
, "EOFn"},
113 {MDSHDR_EOFdti
, "EOFdti"},
114 {MDSHDR_EOFni
, "EOFni"},
115 {MDSHDR_EOFrt
, "EOFrt"},
116 {MDSHDR_EOFrti
, "EOFrti"},
117 /*{MDSHDR_EOF_UNKNOWN, ""}, intentionally removed*/
122 dissect_mdshdr(tvbuff_t
*tvb
, packet_info
*pinfo
, proto_tree
*tree
, void* data _U_
)
125 /* Set up structures needed to add the protocol subtree and manage it */
127 proto_item
*hidden_item
;
128 proto_tree
*mdshdr_tree_main
, *mdshdr_tree_hdr
, *mdshdr_tree_trlr
;
133 int trailer_start
= 0; /*0 means "no trailer found"*/
136 col_set_str(pinfo
->cinfo
, COL_PROTOCOL
, "MDS Header");
138 col_clear(pinfo
->cinfo
, COL_INFO
);
140 sof
= tvb_get_uint8(tvb
, offset
+MDSHDR_SOF_OFFSET
) & 0x0F;
141 pktlen
= tvb_get_ntohs(tvb
, offset
+MDSHDR_PKTLEN_OFFSET
) & 0x1FFF;
143 /* The Mdshdr trailer is at the end of the frame */
144 if ((tvb_captured_length(tvb
) >= (MDSHDR_HEADER_SIZE
+ pktlen
))
145 /* Avoid header/trailer overlap if something wrong */
146 && (pktlen
>= MDSHDR_TRAILER_SIZE
)) {
147 trailer_start
= MDSHDR_HEADER_SIZE
+ pktlen
- MDSHDR_TRAILER_SIZE
;
149 eof
= tvb_get_uint8(tvb
, trailer_start
);
150 tvb_set_reported_length(tvb
, MDSHDR_HEADER_SIZE
+pktlen
);
153 eof
= MDSHDR_EOF_UNKNOWN
;
158 if ((sof
== MDSHDR_SOFi3
) || (sof
== MDSHDR_SOFi2
) || (sof
== MDSHDR_SOFi1
)
159 || (sof
== MDSHDR_SOFi4
)) {
160 fc_data
.sof_eof
= FC_DATA_SOF_FIRST_FRAME
;
162 else if (sof
== MDSHDR_SOFf
) {
163 fc_data
.sof_eof
= FC_DATA_SOF_SOFF
;
166 if (eof
!= MDSHDR_EOFn
) {
167 fc_data
.sof_eof
|= FC_DATA_EOF_LAST_FRAME
;
169 else if (eof
!= MDSHDR_EOFt
) {
170 fc_data
.sof_eof
|= FC_DATA_EOF_INVALID
;
174 ti_main
= proto_tree_add_protocol_format(tree
, proto_mdshdr
, tvb
, 0,
175 MDSHDR_HEADER_SIZE
+pktlen
,
177 val_to_str(sof
, sof_vals
, "Unknown(%u)"),
178 val_to_str(eof
, eof_vals
, "Unknown(%u)"));
180 mdshdr_tree_main
= proto_item_add_subtree(ti_main
, ett_mdshdr
);
182 /* Add Header part as subtree first */
183 mdshdr_tree_hdr
= proto_tree_add_subtree(mdshdr_tree_main
, tvb
, MDSHDR_VER_OFFSET
,
184 MDSHDR_HEADER_SIZE
, ett_mdshdr_hdr
, NULL
, "MDS Header");
186 hidden_item
= proto_tree_add_item(mdshdr_tree_hdr
, hf_mdshdr_sof
, tvb
, MDSHDR_SOF_OFFSET
,
187 MDSHDR_SIZE_BYTE
, ENC_BIG_ENDIAN
);
188 proto_item_set_hidden(hidden_item
);
189 proto_tree_add_item(mdshdr_tree_hdr
, hf_mdshdr_pkt_len
, tvb
, MDSHDR_PKTLEN_OFFSET
,
190 MDSHDR_SIZE_INT16
, ENC_BIG_ENDIAN
);
191 proto_tree_add_item(mdshdr_tree_hdr
, hf_mdshdr_dstidx
, tvb
, MDSHDR_DIDX_OFFSET
,
192 MDSHDR_SIZE_INT16
, ENC_BIG_ENDIAN
);
193 proto_tree_add_item(mdshdr_tree_hdr
, hf_mdshdr_srcidx
, tvb
, MDSHDR_SIDX_OFFSET
,
194 MDSHDR_SIZE_INT16
, ENC_BIG_ENDIAN
);
195 proto_tree_add_item(mdshdr_tree_hdr
, hf_mdshdr_vsan
, tvb
, MDSHDR_VSAN_OFFSET
,
196 MDSHDR_SIZE_INT16
, ENC_BIG_ENDIAN
);
197 hidden_item
= proto_tree_add_item(mdshdr_tree_hdr
, hf_mdshdr_span
,
198 tvb
, MDSHDR_VSAN_OFFSET
,
199 MDSHDR_SIZE_INT16
, ENC_BIG_ENDIAN
);
200 proto_item_set_hidden(hidden_item
);
202 /* Add Mdshdr Trailer part */
203 if (tvb_reported_length(tvb
) >= MDSHDR_HEADER_SIZE
+ pktlen
204 && 0 != trailer_start
) {
205 mdshdr_tree_trlr
= proto_tree_add_subtree(mdshdr_tree_main
, tvb
, trailer_start
,
207 ett_mdshdr_trlr
, NULL
, "MDS Trailer");
209 proto_tree_add_item(mdshdr_tree_trlr
, hf_mdshdr_eof
, tvb
,
210 trailer_start
, MDSHDR_SIZE_BYTE
, ENC_BIG_ENDIAN
);
211 proto_tree_add_item(mdshdr_tree_trlr
, hf_mdshdr_fccrc
, tvb
,
212 trailer_start
+2, MDSHDR_SIZE_INT32
, ENC_BIG_ENDIAN
);
215 proto_tree_add_item(mdshdr_tree_main
, hf_mdshdr_no_trailer
, tvb
, 0, 0, ENC_NA
);
219 if (tvb_reported_length(tvb
) >= MDSHDR_HEADER_SIZE
+ pktlen
220 && 0 != pktlen
/*if something wrong*/) {
221 next_tvb
= tvb_new_subset_length(tvb
, MDSHDR_HEADER_SIZE
, pktlen
);
222 /* XXX what to do with the rest of this frame? --ArtemTamazov */
225 next_tvb
= tvb_new_subset_remaining(tvb
, MDSHDR_HEADER_SIZE
);
228 /* Call the Fibre Channel dissector */
229 if (fc_dissector_handle
) {
230 fc_data
.ethertype
= ETHERTYPE_FCFT
;
231 call_dissector_with_data(fc_dissector_handle
, next_tvb
, pinfo
, tree
, &fc_data
);
234 call_data_dissector(next_tvb
, pinfo
, tree
);
236 return tvb_captured_length(tvb
);
241 proto_register_mdshdr(void)
244 static hf_register_info hf
[] = {
246 {"SOF", "mdshdr.sof", FT_UINT8
, BASE_DEC
, VALS(sof_vals
), 0x0, NULL
, HFILL
}},
248 { &hf_mdshdr_pkt_len
,
249 {"Packet Len", "mdshdr.plen", FT_UINT16
, BASE_DEC
, NULL
, 0x1FFF, NULL
, HFILL
}},
252 {"Dst Index", "mdshdr.dstidx", FT_UINT16
, BASE_HEX
, NULL
, 0x0FFC, NULL
, HFILL
}},
255 {"Src Index", "mdshdr.srcidx", FT_UINT16
, BASE_HEX
, NULL
, 0x03FF, NULL
, HFILL
}},
258 {"VSAN", "mdshdr.vsan", FT_UINT16
, BASE_DEC
, NULL
, 0x0FFF, NULL
, HFILL
}},
261 {"EOF", "mdshdr.eof", FT_UINT8
, BASE_DEC
, VALS(eof_vals
), 0x0, NULL
, HFILL
}},
263 { &hf_mdshdr_no_trailer
,
264 {"MDS Trailer: Not Found", "mdshdr.no_trailer", FT_NONE
, BASE_NONE
, NULL
, 0x0, NULL
, HFILL
}},
267 {"SPAN Frame", "mdshdr.span", FT_UINT16
, BASE_DEC
, NULL
, 0xF000, NULL
, HFILL
}},
270 {"CRC", "mdshdr.crc", FT_UINT32
, BASE_HEX
, NULL
, 0x0, NULL
, HFILL
}},
273 /* Setup protocol subtree array */
274 static int *ett
[] = {
279 module_t
*mdshdr_module
;
281 /* Register the protocol name and description */
282 proto_mdshdr
= proto_register_protocol("MDS Header", "MDS Header", "mdshdr");
284 proto_register_field_array(proto_mdshdr
, hf
, array_length(hf
));
285 proto_register_subtree_array(ett
, array_length(ett
));
287 mdshdr_module
= prefs_register_protocol(proto_mdshdr
, proto_reg_handoff_mdshdr
);
288 prefs_register_bool_preference(mdshdr_module
, "decode_if_etype_zero",
289 "Decode as MDS Header if Ethertype == 0",
290 "A frame is considered for decoding as MDSHDR if either "
291 "ethertype is 0xFCFC or zero. Turn this flag off if you "
292 "don't want ethertype zero to be decoded as MDSHDR. "
293 "This might be useful to avoid problems with test frames.",
294 &decode_if_zero_etype
);
296 /* Register the dissector */
297 mdshdr_handle
= register_dissector("mdshdr", dissect_mdshdr
, proto_mdshdr
);
301 proto_reg_handoff_mdshdr(void)
303 static bool registered_for_zero_etype
= false;
304 static bool mdshdr_prefs_initialized
= false;
306 if (!mdshdr_prefs_initialized
) {
308 * This is the first time this has been called (i.e.,
309 * Wireshark/TShark is starting up), so create a handle for
310 * the MDS Header dissector, register the dissector for
311 * ethertype ETHERTYPE_FCFT, and fetch the data and Fibre
314 dissector_add_uint("ethertype", ETHERTYPE_FCFT
, mdshdr_handle
);
315 fc_dissector_handle
= find_dissector_add_dependency("fc", proto_mdshdr
);
316 mdshdr_prefs_initialized
= true;
320 * Only register the dissector for ethertype 0 if the preference
323 if (decode_if_zero_etype
) {
325 * The preference to register for ethertype ETHERTYPE_UNK (0)
326 * is set; if we're not registered for ethertype ETHERTYPE_UNK,
329 if (!registered_for_zero_etype
) {
330 dissector_add_uint("ethertype", ETHERTYPE_UNK
, mdshdr_handle
);
331 registered_for_zero_etype
= true;
335 * The preference to register for ethertype ETHERTYPE_UNK (0)
336 * is not set; if we're registered for ethertype ETHERTYPE_UNK,
337 * undo that registration.
339 if (registered_for_zero_etype
) {
340 dissector_delete_uint("ethertype", ETHERTYPE_UNK
, mdshdr_handle
);
341 registered_for_zero_etype
= false;
347 * Editor modelines - https://www.wireshark.org/tools/modelines.html
352 * indent-tabs-mode: nil
355 * vi: set shiftwidth=4 tabstop=8 expandtab:
356 * :indentSize=4:tabSize=8:noTabs=true: