MSWSP: fix dissect_mswsp_smb()
[wireshark-wip.git] / epan / dissectors / packet-mdshdr.c
blob9d8513f9d49db3bf5a58add918075d067fbc4f3a
1 /* packet-mdshdr.c
2 * Routines for dissection of Cisco MDS Switch Internal Header
3 * Copyright 2001, Dinesh G Dutt <ddutt@andiamo.com>
5 * $Id$
7 * Wireshark - Network traffic analyzer
8 * By Gerald Combs <gerald@wireshark.org>
9 * Copyright 1998 Gerald Combs
11 * This program is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU General Public License
13 * as published by the Free Software Foundation; either version 2
14 * of the License, or (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
26 #include "config.h"
28 #include <glib.h>
30 #include <epan/packet.h>
31 #include <etypes.h>
32 #include <epan/prefs.h>
34 #define MDSHDR_VERSION_OFFSET 0
36 /* Mdshdr Control bits */
37 #define MDSHDR_CTL_IDXDIRECT 1
38 #define MDSHDR_CTL_IGNACLO 2
39 #define MDSHDR_CTL_DRP 4
41 /* OFFSETS OF FIELDS */
42 #define MDSHDR_VER_OFFSET 0
43 #define MDSHDR_SOF_OFFSET 1
44 #define MDSHDR_PKTLEN_OFFSET 2
45 #define MDSHDR_DIDX_OFFSET 5
46 #define MDSHDR_SIDX_OFFSET 6
47 #define MDSHDR_VSAN_OFFSET 13
49 /* Two size definitions are sufficient */
50 #define MDSHDR_SIZE_BYTE sizeof(gchar)
51 #define MDSHDR_SIZE_INT16 sizeof(guint16)
52 #define MDSHDR_SIZE_INT32 sizeof(guint32)
54 /* Other miscellaneous defines; can't rely on sizeof structs */
55 #define MDSHDR_MAX_VERSION 0
56 #define MDSHDR_HEADER_SIZE 16
57 #define MDSHDR_TRAILER_SIZE 6
59 /* SOF Encodings */
60 #define MDSHDR_SOFc1 0x1
61 #define MDSHDR_SOFi1 0x2
62 #define MDSHDR_SOFn1 0x3
63 #define MDSHDR_SOFi2 0x4
64 #define MDSHDR_SOFn2 0x5
65 #define MDSHDR_SOFi3 0x6
66 #define MDSHDR_SOFn3 0x7
67 #define MDSHDR_SOFf 0x8
68 #define MDSHDR_SOFc4 0x9
69 #define MDSHDR_SOFi4 0xa
70 #define MDSHDR_SOFn4 0xb
72 /* EOF Encodings */
73 #define MDSHDR_EOFt 0x1
74 #define MDSHDR_EOFdt 0x2
75 #define MDSHDR_EOFa 0x4
76 #define MDSHDR_EOFn 0x3
77 #define MDSHDR_EOFdti 0x6
78 #define MDSHDR_EOFni 0x7
79 #define MDSHDR_EOFrt 0xa
80 #define MDSHDR_EOFrti 0xe
81 #define MDSHDR_EOF_UNKNOWN 0xb
83 /* Initialize the protocol and registered fields */
84 static int proto_mdshdr = -1;
85 static int hf_mdshdr_sof = -1;
86 static int hf_mdshdr_pkt_len = -1;
87 static int hf_mdshdr_dstidx = -1;
88 static int hf_mdshdr_srcidx = -1;
89 static int hf_mdshdr_vsan = -1;
90 static int hf_mdshdr_eof = -1;
91 static int hf_mdshdr_span = -1;
92 static int hf_mdshdr_fccrc = -1;
94 /* Initialize the subtree pointers */
95 static gint ett_mdshdr = -1;
96 static gint ett_mdshdr_hdr = -1;
97 static gint ett_mdshdr_trlr = -1;
99 static dissector_handle_t data_handle, fc_dissector_handle;
101 static gboolean decode_if_zero_etype = TRUE;
103 static const value_string sof_vals[] = {
104 {MDSHDR_SOFc1, "SOFc1"},
105 {MDSHDR_SOFi1, "SOFi1"},
106 {MDSHDR_SOFn1, "SOFn1"},
107 {MDSHDR_SOFi2, "SOFi2"},
108 {MDSHDR_SOFn2, "SOFn2"},
109 {MDSHDR_SOFi3, "SOFi3"},
110 {MDSHDR_SOFn3, "SOFn3"},
111 {MDSHDR_SOFc4, "SOFc4"},
112 {MDSHDR_SOFi4, "SOFi4"},
113 {MDSHDR_SOFn4, "SOFn4"},
114 {MDSHDR_SOFf, "SOFf"},
115 {0, NULL},
118 static const value_string eof_vals[] = {
119 {MDSHDR_EOFt, "EOFt"},
120 {MDSHDR_EOFdt, "EOFdt"},
121 {MDSHDR_EOFa, "EOFa"},
122 {MDSHDR_EOFn, "EOFn"},
123 {MDSHDR_EOFdti, "EOFdti"},
124 {MDSHDR_EOFni, "EOFni"},
125 {MDSHDR_EOFrt, "EOFrt"},
126 {MDSHDR_EOFrti, "EOFrti"},
127 /*{MDSHDR_EOF_UNKNOWN, ""}, intentionally removed*/
128 {0, NULL},
131 void proto_reg_handoff_mdshdr(void);
133 static void
134 dissect_mdshdr(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
137 /* Set up structures needed to add the protocol subtree and manage it */
138 proto_item *ti_main, *ti_hdr, *ti_trlr;
139 proto_item *hidden_item;
140 proto_tree *mdshdr_tree_main, *mdshdr_tree_hdr, *mdshdr_tree_trlr;
141 int offset = 0;
142 guint pktlen;
143 tvbuff_t *next_tvb;
144 guint8 sof, eof;
145 int trailer_start = 0; /*0 means "no trailer found"*/
147 col_set_str(pinfo->cinfo, COL_PROTOCOL, "MDS Header");
149 col_clear(pinfo->cinfo, COL_INFO);
151 sof = tvb_get_guint8(tvb, offset+MDSHDR_SOF_OFFSET) & 0x0F;
152 pktlen = tvb_get_ntohs(tvb, offset+MDSHDR_PKTLEN_OFFSET) & 0x1FFF;
154 /* The Mdshdr trailer is at the end of the frame */
155 if ((tvb_length(tvb) >= (MDSHDR_HEADER_SIZE + pktlen))
156 /* Avoid header/trailer overlap if something wrong */
157 && (pktlen >= MDSHDR_TRAILER_SIZE)) {
158 trailer_start = MDSHDR_HEADER_SIZE + pktlen - MDSHDR_TRAILER_SIZE;
160 eof = tvb_get_guint8(tvb, trailer_start);
161 tvb_set_reported_length(tvb, MDSHDR_HEADER_SIZE+pktlen);
163 else {
164 eof = MDSHDR_EOF_UNKNOWN;
167 pinfo->sof_eof = 0;
169 if ((sof == MDSHDR_SOFi3) || (sof == MDSHDR_SOFi2) || (sof == MDSHDR_SOFi1)
170 || (sof == MDSHDR_SOFi4)) {
171 pinfo->sof_eof = PINFO_SOF_FIRST_FRAME;
173 else if (sof == MDSHDR_SOFf) {
174 pinfo->sof_eof = PINFO_SOF_SOFF;
177 if (eof != MDSHDR_EOFn) {
178 pinfo->sof_eof |= PINFO_EOF_LAST_FRAME;
180 else if (eof != MDSHDR_EOFt) {
181 pinfo->sof_eof |= PINFO_EOF_INVALID;
184 if (tree) {
185 ti_main = proto_tree_add_protocol_format(tree, proto_mdshdr, tvb, 0,
186 MDSHDR_HEADER_SIZE+pktlen,
187 "MDS Header(%s/%s)",
188 val_to_str(sof, sof_vals, "Unknown(%u)"),
189 val_to_str(eof, eof_vals, "Unknown(%u)"));
191 mdshdr_tree_main = proto_item_add_subtree(ti_main, ett_mdshdr);
193 /* Add Header part as subtree first */
194 ti_hdr = proto_tree_add_text(mdshdr_tree_main, tvb, MDSHDR_VER_OFFSET,
195 MDSHDR_HEADER_SIZE, "MDS Header");
197 mdshdr_tree_hdr = proto_item_add_subtree(ti_hdr, ett_mdshdr_hdr);
198 hidden_item = proto_tree_add_item(mdshdr_tree_hdr, hf_mdshdr_sof, tvb, MDSHDR_SOF_OFFSET,
199 MDSHDR_SIZE_BYTE, ENC_BIG_ENDIAN);
200 PROTO_ITEM_SET_HIDDEN(hidden_item);
201 proto_tree_add_item(mdshdr_tree_hdr, hf_mdshdr_pkt_len, tvb, MDSHDR_PKTLEN_OFFSET,
202 MDSHDR_SIZE_INT16, ENC_BIG_ENDIAN);
203 proto_tree_add_item(mdshdr_tree_hdr, hf_mdshdr_dstidx, tvb, MDSHDR_DIDX_OFFSET,
204 MDSHDR_SIZE_INT16, ENC_BIG_ENDIAN);
205 proto_tree_add_item(mdshdr_tree_hdr, hf_mdshdr_srcidx, tvb, MDSHDR_SIDX_OFFSET,
206 MDSHDR_SIZE_INT16, ENC_BIG_ENDIAN);
207 proto_tree_add_item(mdshdr_tree_hdr, hf_mdshdr_vsan, tvb, MDSHDR_VSAN_OFFSET,
208 MDSHDR_SIZE_INT16, ENC_BIG_ENDIAN);
209 hidden_item = proto_tree_add_item(mdshdr_tree_hdr, hf_mdshdr_span,
210 tvb, MDSHDR_VSAN_OFFSET,
211 MDSHDR_SIZE_INT16, ENC_BIG_ENDIAN);
212 PROTO_ITEM_SET_HIDDEN(hidden_item);
214 /* Add Mdshdr Trailer part */
215 if (tvb_length(tvb) >= MDSHDR_HEADER_SIZE + pktlen
216 && 0 != trailer_start) {
217 ti_trlr = proto_tree_add_text(mdshdr_tree_main, tvb, trailer_start,
218 MDSHDR_TRAILER_SIZE,
219 "MDS Trailer");
220 mdshdr_tree_trlr = proto_item_add_subtree(ti_trlr, ett_mdshdr_trlr);
222 proto_tree_add_item(mdshdr_tree_trlr, hf_mdshdr_eof, tvb,
223 trailer_start, MDSHDR_SIZE_BYTE, ENC_BIG_ENDIAN);
224 proto_tree_add_item(mdshdr_tree_trlr, hf_mdshdr_fccrc, tvb,
225 trailer_start+2, MDSHDR_SIZE_INT32, ENC_BIG_ENDIAN);
227 else {
228 proto_tree_add_text(mdshdr_tree_main, tvb, 0, 0, "MDS Trailer: Not Found");
232 if (tvb_length(tvb) >= MDSHDR_HEADER_SIZE + pktlen
233 && 0 != pktlen /*if something wrong*/) {
234 next_tvb = tvb_new_subset(tvb, MDSHDR_HEADER_SIZE, pktlen, pktlen);
235 /* XXX what to do with the rest of this frame? --ArtemTamazov */
237 else {
238 next_tvb = tvb_new_subset_remaining(tvb, MDSHDR_HEADER_SIZE);
241 /* Call the Fibre Channel dissector */
242 if (fc_dissector_handle) {
243 call_dissector(fc_dissector_handle, next_tvb, pinfo, tree);
245 else {
246 call_dissector(data_handle, next_tvb, pinfo, tree);
251 void
252 proto_register_mdshdr(void)
255 static hf_register_info hf[] = {
256 { &hf_mdshdr_sof,
257 {"SOF", "mdshdr.sof", FT_UINT8, BASE_DEC, VALS(sof_vals), 0x0, NULL, HFILL}},
259 { &hf_mdshdr_pkt_len,
260 {"Packet Len", "mdshdr.plen", FT_UINT16, BASE_DEC, NULL, 0x1FFF, NULL, HFILL}},
262 { &hf_mdshdr_dstidx,
263 {"Dst Index", "mdshdr.dstidx", FT_UINT16, BASE_HEX, NULL, 0xFFC, NULL, HFILL}},
265 { &hf_mdshdr_srcidx,
266 {"Src Index", "mdshdr.srcidx", FT_UINT16, BASE_HEX, NULL, 0x3FF, NULL, HFILL}},
268 { &hf_mdshdr_vsan,
269 {"VSAN", "mdshdr.vsan", FT_UINT16, BASE_DEC, NULL, 0x0FFF, NULL, HFILL}},
271 { &hf_mdshdr_eof,
272 {"EOF", "mdshdr.eof", FT_UINT8, BASE_DEC, VALS(eof_vals), 0x0, NULL, HFILL}},
274 { &hf_mdshdr_span,
275 {"SPAN Frame", "mdshdr.span", FT_UINT16, BASE_DEC, NULL, 0xF000, NULL, HFILL}},
277 { &hf_mdshdr_fccrc,
278 {"CRC", "mdshdr.crc", FT_UINT32, BASE_HEX, NULL, 0x0, NULL, HFILL}},
281 /* Setup protocol subtree array */
282 static gint *ett[] = {
283 &ett_mdshdr,
284 &ett_mdshdr_hdr,
285 &ett_mdshdr_trlr
287 module_t *mdshdr_module;
289 /* Register the protocol name and description */
290 proto_mdshdr = proto_register_protocol("MDS Header", "MDS Header", "mdshdr");
292 proto_register_field_array(proto_mdshdr, hf, array_length(hf));
293 proto_register_subtree_array(ett, array_length(ett));
295 mdshdr_module = prefs_register_protocol(proto_mdshdr, proto_reg_handoff_mdshdr);
296 prefs_register_bool_preference(mdshdr_module, "decode_if_etype_zero",
297 "Decode as MDS Header if Ethertype == 0",
298 "A frame is considered for decoding as MDSHDR if either "
299 "ethertype is 0xFCFC or zero. Turn this flag off if you "
300 "don't want ethertype zero to be decoded as MDSHDR. "
301 "This might be useful to avoid problems with test frames.",
302 &decode_if_zero_etype);
305 void
306 proto_reg_handoff_mdshdr(void)
308 static dissector_handle_t mdshdr_handle;
309 static gboolean registered_for_zero_etype = FALSE;
310 static gboolean mdshdr_prefs_initialized = FALSE;
312 if (!mdshdr_prefs_initialized) {
314 * This is the first time this has been called (i.e.,
315 * Wireshark/TShark is starting up), so create a handle for
316 * the MDS Header dissector, register the dissector for
317 * ethertype ETHERTYPE_FCFT, and fetch the data and Fibre
318 * Channel handles.
320 mdshdr_handle = create_dissector_handle(dissect_mdshdr, proto_mdshdr);
321 dissector_add_uint("ethertype", ETHERTYPE_FCFT, mdshdr_handle);
322 data_handle = find_dissector("data");
323 fc_dissector_handle = find_dissector("fc");
324 mdshdr_prefs_initialized = TRUE;
328 * Only register the dissector for ethertype 0 if the preference
329 * is set to do so.
331 if (decode_if_zero_etype) {
333 * The preference to register for ethertype ETHERTYPE_UNK (0)
334 * is set; if we're not registered for ethertype ETHERTYPE_UNK,
335 * do so.
337 if (!registered_for_zero_etype) {
338 dissector_add_uint("ethertype", ETHERTYPE_UNK, mdshdr_handle);
339 registered_for_zero_etype = TRUE;
341 } else {
343 * The preference to register for ethertype ETHERTYPE_UNK (0)
344 * is not set; if we're registered for ethertype ETHERTYPE_UNK,
345 * undo that registration.
347 if (registered_for_zero_etype) {
348 dissector_delete_uint("ethertype", ETHERTYPE_UNK, mdshdr_handle);
349 registered_for_zero_etype = FALSE;