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