Don't guard col_set_str (COL_PROTOCOL) with col_check
[obnox/wireshark/wip.git] / epan / dissectors / packet-mdshdr.c
1 /* packet-mdshdr.c
2  * Routines for dissection of Cisco MDS Switch Internal Header
3  * Copyright 2001, Dinesh G Dutt <ddutt@andiamo.com>
4  *
5  * $Id$
6  *
7  * Wireshark - Network traffic analyzer
8  * By Gerald Combs <gerald@wireshark.org>
9  * Copyright 1998 Gerald Combs
10  *
11  * Copied from WHATEVER_FILE_YOU_USED (where "WHATEVER_FILE_YOU_USED"
12  * is a dissector file; if you just copied this from README.developer,
13  * don't bother with the "Copied from" - you don't even need to put
14  * in a "Copied from" if you copied an existing dissector, especially
15  * if the bulk of the code in the new dissector is your code)
16  * 
17  * This program is free software; you can redistribute it and/or
18  * modify it under the terms of the GNU General Public License
19  * as published by the Free Software Foundation; either version 2
20  * of the License, or (at your option) any later version.
21  * 
22  * This program is distributed in the hope that it will be useful,
23  * but WITHOUT ANY WARRANTY; without even the implied warranty of
24  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
25  * GNU General Public License for more details.
26  * 
27  * You should have received a copy of the GNU General Public License
28  * along with this program; if not, write to the Free Software
29  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
30  */
31
32 #ifdef HAVE_CONFIG_H
33 # include "config.h"
34 #endif
35
36 #include <stdio.h>
37 #include <stdlib.h>
38 #include <string.h>
39
40 #ifdef HAVE_SYS_TYPES_H
41 # include <sys/types.h>
42 #endif
43
44 #ifdef HAVE_NETINET_IN_H
45 # include <netinet/in.h>
46 #endif
47
48 #include <glib.h>
49
50 #include <epan/value_string.h>
51 #include <etypes.h>
52 #include <epan/packet.h>
53 #include <epan/prefs.h>
54
55 #define MDSHDR_VERSION_OFFSET           0
56
57 /* Mdshdr Control bits */
58 #define MDSHDR_CTL_IDXDIRECT             1
59 #define MDSHDR_CTL_IGNACLO               2
60 #define MDSHDR_CTL_DRP                   4
61
62 /* OFFSETS OF FIELDS */
63 #define MDSHDR_VER_OFFSET                0
64 #define MDSHDR_SOF_OFFSET                1
65 #define MDSHDR_PKTLEN_OFFSET             2
66 #define MDSHDR_DIDX_OFFSET               5
67 #define MDSHDR_SIDX_OFFSET               6
68 #define MDSHDR_VSAN_OFFSET               13
69
70 /* Two size definitions are sufficient */
71 #define MDSHDR_SIZE_BYTE                 sizeof (gchar)
72 #define MDSHDR_SIZE_INT16                sizeof (guint16)
73 #define MDSHDR_SIZE_INT32                sizeof (guint32)
74
75 /* Other miscellaneous defines; can't rely on sizeof structs */
76 #define MDSHDR_MAX_VERSION               0
77 #define MDSHDR_HEADER_SIZE               16
78 #define MDSHDR_TRAILER_SIZE              6
79
80 /* SOF Encodings */
81 #define MDSHDR_SOFc1                     0x1
82 #define MDSHDR_SOFi1                     0x2
83 #define MDSHDR_SOFn1                     0x3
84 #define MDSHDR_SOFi2                     0x4
85 #define MDSHDR_SOFn2                     0x5
86 #define MDSHDR_SOFi3                     0x6
87 #define MDSHDR_SOFn3                     0x7
88 #define MDSHDR_SOFf                      0x8
89 #define MDSHDR_SOFc4                     0x9
90 #define MDSHDR_SOFi4                     0xa
91 #define MDSHDR_SOFn4                     0xb
92
93 /* EOF Encodings */
94 #define MDSHDR_EOFt                      0x1
95 #define MDSHDR_EOFdt                     0x2
96 #define MDSHDR_EOFa                      0x4
97 #define MDSHDR_EOFn                      0x3
98 #define MDSHDR_EOFdti                    0x6
99 #define MDSHDR_EOFni                     0x7
100 #define MDSHDR_EOFrt                     0xa
101 #define MDSHDR_EOFrti                    0xe
102 #define MDSHDR_EOF_UNKNOWN               0xb
103
104 /* Initialize the protocol and registered fields */
105 static int proto_mdshdr = -1;
106 static int hf_mdshdr_sof = -1;
107 static int hf_mdshdr_pkt_len = -1;
108 static int hf_mdshdr_dstidx = -1;
109 static int hf_mdshdr_srcidx = -1;
110 static int hf_mdshdr_vsan = -1;
111 static int hf_mdshdr_eof = -1;
112 static int hf_mdshdr_span = -1;
113 static int hf_mdshdr_fccrc = -1;
114
115 /* Initialize the subtree pointers */
116 static gint ett_mdshdr = -1;
117 static gint ett_mdshdr_hdr = -1;
118 static gint ett_mdshdr_trlr = -1;
119
120 static dissector_handle_t data_handle, fc_dissector_handle;
121
122 static gboolean decode_if_zero_etype = TRUE;
123
124 static const value_string sof_vals[] = {
125     {MDSHDR_SOFc1,               "SOFc1"},
126     {MDSHDR_SOFi1,               "SOFi1"},
127     {MDSHDR_SOFn1,               "SOFn1"},
128     {MDSHDR_SOFi2,               "SOFi2"},
129     {MDSHDR_SOFn2,               "SOFn2"},
130     {MDSHDR_SOFi3,               "SOFi3"},
131     {MDSHDR_SOFn3,               "SOFn3"},
132     {MDSHDR_SOFc4,               "SOFc4"},
133     {MDSHDR_SOFi4,               "SOFi4"},
134     {MDSHDR_SOFn4,               "SOFn4"},
135     {MDSHDR_SOFf,                "SOFf"},
136     {0,                         NULL},
137 };
138
139 static const value_string eof_vals[] = {
140     {MDSHDR_EOFt,                "EOFt"},
141     {MDSHDR_EOFdt,               "EOFdt"},
142     {MDSHDR_EOFa,                "EOFa"},
143     {MDSHDR_EOFn,                "EOFn"},
144     {MDSHDR_EOFdti,              "EOFdti"},
145     {MDSHDR_EOFni,               "EOFni"},
146     {MDSHDR_EOFrt,               "EOFrt"},
147     {MDSHDR_EOFrti,              "EOFrti"},
148     {MDSHDR_EOF_UNKNOWN,         ""},
149     {0,                          NULL},
150 };
151
152 void proto_reg_handoff_mdshdr(void);
153
154 /* Code to actually dissect the packets */
155 static void
156 dissect_mdshdr(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
157 {
158
159 /* Set up structures needed to add the protocol subtree and manage it */
160     proto_item *ti_main, *ti_hdr, *ti_trlr;
161     proto_item *hidden_item; 
162     proto_tree *mdshdr_tree_main, *mdshdr_tree_hdr, *mdshdr_tree_trlr;
163     int offset = 0;
164     guint    pktlen;
165     tvbuff_t *next_tvb;
166     guint8 sof, eof;
167     guint16 vsan;
168     guint8 span_id;
169     int trailer_start = 0;
170
171     /* Make entries in Protocol column and Info column on summary display */
172     col_set_str(pinfo->cinfo, COL_PROTOCOL, "MDS Header");
173     
174     if (check_col (pinfo->cinfo, COL_INFO))
175         col_clear (pinfo->cinfo, COL_INFO);
176
177     sof = tvb_get_guint8 (tvb, offset+MDSHDR_SOF_OFFSET) & 0x0F;
178     pktlen = tvb_get_ntohs (tvb, offset+MDSHDR_PKTLEN_OFFSET) & 0x1FFF;
179     vsan = tvb_get_ntohs (tvb, offset+MDSHDR_VSAN_OFFSET) & 0x0FFF;
180     span_id = (tvb_get_ntohs (tvb, offset+MDSHDR_VSAN_OFFSET) & 0xF000) >> 12;
181     
182     /* The Mdshdr trailer is at the end of the frame */
183     if (tvb_length (tvb) >= MDSHDR_HEADER_SIZE + pktlen) {
184         trailer_start = MDSHDR_HEADER_SIZE + pktlen - MDSHDR_TRAILER_SIZE; 
185     
186         eof = tvb_get_guint8 (tvb, trailer_start);
187         tvb_set_reported_length (tvb, MDSHDR_HEADER_SIZE+pktlen);
188     }
189     else {
190         eof = MDSHDR_EOF_UNKNOWN;
191     }
192
193     pinfo->src_idx = (tvb_get_ntohs (tvb, MDSHDR_SIDX_OFFSET) & 0x3FF);
194     pinfo->dst_idx = (tvb_get_ntohs (tvb, MDSHDR_DIDX_OFFSET) & 0xFFC) >> 2;
195     pinfo->vsan = vsan;
196     pinfo->sof_eof = 0;
197
198     if ((sof == MDSHDR_SOFi3) || (sof == MDSHDR_SOFi2) || (sof == MDSHDR_SOFi1)
199         || (sof == MDSHDR_SOFi4)) {
200         pinfo->sof_eof = PINFO_SOF_FIRST_FRAME;
201     }
202     else if (sof == MDSHDR_SOFf) {
203         pinfo->sof_eof = PINFO_SOF_SOFF;      
204     }
205
206     if (eof != MDSHDR_EOFn) {
207         pinfo->sof_eof |= PINFO_EOF_LAST_FRAME;
208     }
209     else if (eof != MDSHDR_EOFt) {
210         pinfo->sof_eof |= PINFO_EOF_INVALID;
211     }
212     
213     /* In the interest of speed, if "tree" is NULL, don't do any work not
214        necessary to generate protocol tree items. */
215     if (tree) {
216
217         /* create display subtree for the protocol */
218         ti_main = proto_tree_add_protocol_format (tree, proto_mdshdr, tvb, 0,
219                                                   MDSHDR_HEADER_SIZE+pktlen,
220                                                   "MDS Header(%s/%s)", 
221                                                   val_to_str(sof, sof_vals, "Unknown(%u)"),
222                                                   val_to_str(eof, eof_vals, "Unknown(%u)"));
223
224         mdshdr_tree_main = proto_item_add_subtree (ti_main, ett_mdshdr);
225
226         /* Add Header part as subtree first */
227         ti_hdr = proto_tree_add_text (mdshdr_tree_main, tvb, MDSHDR_VER_OFFSET,
228                                       MDSHDR_HEADER_SIZE, "MDS Header");
229
230         mdshdr_tree_hdr = proto_item_add_subtree (ti_hdr, ett_mdshdr_hdr);
231         hidden_item = proto_tree_add_item (mdshdr_tree_hdr, hf_mdshdr_sof, tvb, MDSHDR_SOF_OFFSET,
232                                     MDSHDR_SIZE_BYTE, 0);
233         PROTO_ITEM_SET_HIDDEN(hidden_item);
234         proto_tree_add_item (mdshdr_tree_hdr, hf_mdshdr_pkt_len, tvb, MDSHDR_PKTLEN_OFFSET, 
235                              MDSHDR_SIZE_INT16, 0);
236         proto_tree_add_item (mdshdr_tree_hdr, hf_mdshdr_dstidx, tvb, MDSHDR_DIDX_OFFSET,
237                              MDSHDR_SIZE_INT16, 0);
238         proto_tree_add_item (mdshdr_tree_hdr, hf_mdshdr_srcidx, tvb, MDSHDR_SIDX_OFFSET,
239                              MDSHDR_SIZE_INT16, 0);
240         proto_tree_add_item (mdshdr_tree_hdr, hf_mdshdr_vsan, tvb, MDSHDR_VSAN_OFFSET,
241                              MDSHDR_SIZE_INT16, 0);
242         hidden_item = proto_tree_add_uint(mdshdr_tree_hdr, hf_mdshdr_span,
243                                    tvb, MDSHDR_VSAN_OFFSET,
244                                    MDSHDR_SIZE_BYTE, span_id);
245         PROTO_ITEM_SET_HIDDEN(hidden_item);
246         
247         /* Add Mdshdr Trailer part */
248         if (tvb_length (tvb) >= MDSHDR_HEADER_SIZE + pktlen) {
249             ti_trlr = proto_tree_add_text (mdshdr_tree_main, tvb, trailer_start,
250                                            MDSHDR_TRAILER_SIZE,
251                                            "MDS Trailer");
252             mdshdr_tree_trlr = proto_item_add_subtree (ti_trlr, ett_mdshdr_trlr);
253         
254             proto_tree_add_item (mdshdr_tree_trlr, hf_mdshdr_eof, tvb,
255                                  trailer_start, MDSHDR_SIZE_BYTE, 0);
256             proto_tree_add_item (mdshdr_tree_trlr, hf_mdshdr_fccrc, tvb,
257                                  trailer_start+2, MDSHDR_SIZE_INT32, 0);
258         }
259     }
260     
261     /* If this protocol has a sub-dissector call it here, see section 1.8 */
262     if (tvb_length (tvb) >= MDSHDR_HEADER_SIZE + pktlen) {
263         next_tvb = tvb_new_subset (tvb, MDSHDR_HEADER_SIZE, pktlen, pktlen);
264     }
265     else {
266         next_tvb = tvb_new_subset (tvb, MDSHDR_HEADER_SIZE, -1, -1);
267     }
268
269     /* Call the Fibre Channel dissector */
270     if (fc_dissector_handle) {
271         call_dissector (fc_dissector_handle, next_tvb, pinfo, tree);
272     }
273     else {
274         call_dissector (data_handle, next_tvb, pinfo, tree);
275     }
276 }
277
278
279 /* Register the protocol with Wireshark. This format is require because a script
280  * is used to build the C function that calls all the protocol registration.
281  */
282
283 void
284 proto_register_mdshdr(void)
285 {                 
286
287 /* Setup list of header fields  See Section 1.6.1 for details*/
288     static hf_register_info hf[] = {
289         { &hf_mdshdr_sof,
290           {"SOF", "mdshdr.sof", FT_UINT8, BASE_DEC, VALS(sof_vals), 0x0, NULL, HFILL}},
291         { &hf_mdshdr_pkt_len,
292           {"Packet Len", "mdshdr.plen", FT_UINT16, BASE_DEC, NULL, 0x1FFF, NULL, HFILL}},
293         { &hf_mdshdr_dstidx,
294           {"Dst Index", "mdshdr.dstidx", FT_UINT16, BASE_HEX, NULL, 0xFFC, NULL, HFILL}},
295         { &hf_mdshdr_srcidx,
296           {"Src Index", "mdshdr.srcidx", FT_UINT16, BASE_HEX, NULL, 0x3FF, NULL, HFILL}},
297         { &hf_mdshdr_vsan,
298           {"VSAN", "mdshdr.vsan", FT_UINT16, BASE_DEC, NULL, 0x0FFF, NULL, HFILL}},
299         { &hf_mdshdr_eof,
300           {"EOF", "mdshdr.eof", FT_UINT8, BASE_DEC, VALS(eof_vals), 0x0, NULL, HFILL}},
301         { &hf_mdshdr_span,
302           {"SPAN Frame", "mdshdr.span", FT_UINT8, BASE_DEC, NULL, 0x0,
303            NULL, HFILL}},
304         { &hf_mdshdr_fccrc,
305           {"CRC", "mdshdr.crc", FT_UINT32, BASE_HEX, NULL, 0x0, NULL, HFILL}},
306     };
307
308 /* Setup protocol subtree array */
309     static gint *ett[] = {
310         &ett_mdshdr,
311         &ett_mdshdr_hdr,
312         &ett_mdshdr_trlr
313     };
314     module_t *mdshdr_module;
315
316 /* Register the protocol name and description */
317     proto_mdshdr = proto_register_protocol("MDS Header", "MDS Header", "mdshdr");
318
319 /* Required function calls to register the header fields and subtrees used */
320     proto_register_field_array(proto_mdshdr, hf, array_length(hf));
321     proto_register_subtree_array(ett, array_length(ett));
322
323     mdshdr_module = prefs_register_protocol (proto_mdshdr, proto_reg_handoff_mdshdr);
324     prefs_register_bool_preference (mdshdr_module, "decode_if_etype_zero",
325                                     "Decode as MDS Header if Ethertype == 0",
326                                     "A frame is considered for decoding as MDSHDR if either "
327                                     "ethertype is 0xFCFC or zero. Turn this flag off if you "
328                                     "you don't want ethertype zero to be decoded as MDSHDR. "
329                                     "This might be useful to avoid problems with test frames.",
330                                     &decode_if_zero_etype);
331 }
332
333
334 /* If this dissector uses sub-dissector registration add a registration routine.
335    This format is required because a script is used to find these routines and
336    create the code that calls these routines.
337 */
338 void
339 proto_reg_handoff_mdshdr(void)
340 {
341     static dissector_handle_t mdshdr_handle;
342     static gboolean registered_for_zero_etype = FALSE;
343     static gboolean mdshdr_prefs_initialized = FALSE;
344
345     if (!mdshdr_prefs_initialized) {
346         /*
347          * This is the first time this has been called (i.e.,
348          * Wireshark/TShark is starting up), so create a handle for
349          * the MDS Header dissector, register the dissector for
350          * ethertype ETHERTYPE_FCFT, and fetch the data and Fibre
351          * Channel handles.
352          */
353         mdshdr_handle = create_dissector_handle (dissect_mdshdr, proto_mdshdr);
354         dissector_add ("ethertype", ETHERTYPE_FCFT, mdshdr_handle);
355         data_handle = find_dissector ("data");
356         fc_dissector_handle = find_dissector ("fc");
357         mdshdr_prefs_initialized = TRUE;
358     }
359
360     /*
361      * Only register the dissector for ethertype 0 if the preference
362      * is set to do so.
363      */
364     if (decode_if_zero_etype) {
365         /*
366          * The preference to register for ethertype ETHERTYPE_UNK (0)
367          * is set; if we're not registered for ethertype ETHERTYPE_UNK,
368          * do so.
369          */
370         if (!registered_for_zero_etype) {
371             dissector_add ("ethertype", ETHERTYPE_UNK, mdshdr_handle);
372             registered_for_zero_etype = TRUE;
373         }
374     } else {
375         /*
376          * The preference to register for ethertype ETHERTYPE_UNK (0)
377          * is not set; if we're registered for ethertype ETHERTYPE_UNK,
378          * undo that registration.
379          */
380         if (registered_for_zero_etype) {
381             dissector_delete ("ethertype", ETHERTYPE_UNK, mdshdr_handle);
382             registered_for_zero_etype = FALSE;
383         }
384     }
385 }