A quantity dissected as 6 unknown bytes in a logon reply actually
[metze/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.4 2003/03/05 07:41:23 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
118 /* Initialize the subtree pointers */
119 static gint ett_mdshdr = -1;
120 static gint ett_mdshdr_hdr = -1;
121 static gint ett_mdshdr_trlr = -1;
122
123 static dissector_handle_t data_handle, fc_dissector_handle;
124
125 static const value_string sof_vals[] = {
126     {MDSHDR_SOFc1,               "SOFc1"},
127     {MDSHDR_SOFi1,               "SOFi1"},
128     {MDSHDR_SOFn1,               "SOFn1"},
129     {MDSHDR_SOFi2,               "SOFi2"},
130     {MDSHDR_SOFn2,               "SOFn2"},
131     {MDSHDR_SOFi3,               "SOFi3"},
132     {MDSHDR_SOFn3,               "SOFn3"},
133     {MDSHDR_SOFc4,               "SOFc4"},
134     {MDSHDR_SOFi4,               "SOFi4"},
135     {MDSHDR_SOFn4,               "SOFn4"},
136     {MDSHDR_SOFf,                "SOFf"},
137     {0,                         NULL},
138 };
139
140 static const value_string eof_vals[] = {
141     {MDSHDR_EOFt,                "EOFt"},
142     {MDSHDR_EOFdt,               "EOFdt"},
143     {MDSHDR_EOFa,                "EOFa"},
144     {MDSHDR_EOFn,                "EOFn"},
145     {MDSHDR_EOFdti,              "EOFdti"},
146     {MDSHDR_EOFni,               "EOFni"},
147     {MDSHDR_EOFrt,               "EOFrt"},
148     {MDSHDR_EOFrti,              "EOFrti"},
149     {MDSHDR_EOF_UNKNOWN,         ""},
150     {0,                          NULL},
151 };
152
153 static const gchar * sof_strings[] = {
154     "Null", "SOFc1", "SOFi1", "SOFn1", "SOFi2", "SOFn2", "SOFi3", "SOFn3",
155     "SOFf", "SOFc4", "SOFi4", "SOFn4",
156 };
157
158 static const gchar *eof_strings[] = {
159     "Null", "EOFt", "EOFdt", "EOFn", "EOFa", "", "EOFdti", "EOFni",
160     "", "", "EOFrt", "", "", "", "EOFrti", 
161 };
162
163 /* Code to actually dissect the packets */
164 static void
165 dissect_mdshdr(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
166 {
167
168 /* Set up structures needed to add the protocol subtree and manage it */
169     proto_item *ti_main, *ti_hdr, *ti_trlr;
170     proto_tree *mdshdr_tree_main, *mdshdr_tree_hdr, *mdshdr_tree_trlr;
171     int offset = 0,
172         pktlen;
173     tvbuff_t *next_tvb;
174     guint8 sof, eof;
175     guint16 vsan;
176     guint8 span_id;
177     int trailer_start = 0;
178
179     /* Make entries in Protocol column and Info column on summary display */
180     if (check_col(pinfo->cinfo, COL_PROTOCOL)) 
181         col_set_str(pinfo->cinfo, COL_PROTOCOL, "MDS Header");
182     
183     if (check_col (pinfo->cinfo, COL_INFO))
184         col_clear (pinfo->cinfo, COL_INFO);
185
186     sof = tvb_get_guint8 (tvb, offset+MDSHDR_SOF_OFFSET) & 0x0F;
187     pktlen = tvb_get_ntohs (tvb, offset+MDSHDR_PKTLEN_OFFSET) & 0x1FFF;
188     vsan = tvb_get_ntohs (tvb, offset+MDSHDR_VSAN_OFFSET) & 0x0FFF;
189     span_id = (tvb_get_ntohs (tvb, offset+MDSHDR_VSAN_OFFSET) & 0xF000) >> 12;
190     
191     /* The Mdshdr trailer is at the end of the frame */
192     if (tvb_bytes_exist (tvb, 0, MDSHDR_HEADER_SIZE + pktlen)) {
193         trailer_start = MDSHDR_HEADER_SIZE + pktlen - MDSHDR_TRAILER_SIZE; 
194     
195         eof = tvb_get_guint8 (tvb, trailer_start);
196         
197         tvb_set_reported_length (tvb, MDSHDR_HEADER_SIZE+pktlen);
198     }
199     else {
200         eof = MDSHDR_EOF_UNKNOWN;
201     }
202
203     pinfo->src_idx = (tvb_get_ntohs (tvb, MDSHDR_SIDX_OFFSET) & 0x3FF);
204     pinfo->dst_idx = (tvb_get_ntohs (tvb, MDSHDR_DIDX_OFFSET) & 0xFFC) >> 6;
205     pinfo->vsan = vsan;
206     
207     /* In the interest of speed, if "tree" is NULL, don't do any work not
208        necessary to generate protocol tree items. */
209     if (tree) {
210
211         /* create display subtree for the protocol */
212         ti_main = proto_tree_add_protocol_format (tree, proto_mdshdr, tvb, 0,
213                                                   MDSHDR_HEADER_SIZE+pktlen,
214                                                   "MDS Header(%s/%s)", 
215                                                   sof_strings[sof], eof_strings[eof]);
216
217         mdshdr_tree_main = proto_item_add_subtree (ti_main, ett_mdshdr);
218
219         /* Add Header part as subtree first */
220         ti_hdr = proto_tree_add_text (mdshdr_tree_main, tvb, MDSHDR_VER_OFFSET,
221                                       MDSHDR_HEADER_SIZE, "MDS Header");
222
223         mdshdr_tree_hdr = proto_item_add_subtree (ti_hdr, ett_mdshdr_hdr);
224         proto_tree_add_item_hidden (mdshdr_tree_hdr, hf_mdshdr_sof, tvb, MDSHDR_SOF_OFFSET,
225                                     MDSHDR_SIZE_BYTE, 0);
226         proto_tree_add_item (mdshdr_tree_hdr, hf_mdshdr_pkt_len, tvb, MDSHDR_PKTLEN_OFFSET, 
227                              MDSHDR_SIZE_INT16, 0);
228         proto_tree_add_item (mdshdr_tree_hdr, hf_mdshdr_dstidx, tvb, MDSHDR_DIDX_OFFSET,
229                              MDSHDR_SIZE_INT16, 0);
230         proto_tree_add_item (mdshdr_tree_hdr, hf_mdshdr_srcidx, tvb, MDSHDR_SIDX_OFFSET,
231                              MDSHDR_SIZE_INT16, 0);
232         proto_tree_add_item (mdshdr_tree_hdr, hf_mdshdr_vsan, tvb, MDSHDR_VSAN_OFFSET,
233                              MDSHDR_SIZE_INT16, 0);
234         proto_tree_add_uint_hidden(mdshdr_tree_hdr, hf_mdshdr_span,
235                                    tvb, MDSHDR_VSAN_OFFSET,
236                                    MDSHDR_SIZE_BYTE, span_id);
237         
238         /* Add Mdshdr Trailer part */
239         if (tvb_bytes_exist (tvb, 0, MDSHDR_HEADER_SIZE+pktlen)) {
240             ti_trlr = proto_tree_add_text (mdshdr_tree_main, tvb, trailer_start,
241                                            MDSHDR_TRAILER_SIZE,
242                                            "MDS Trailer");
243             mdshdr_tree_trlr = proto_item_add_subtree (ti_trlr, ett_mdshdr_trlr);
244         
245             proto_tree_add_item (mdshdr_tree_trlr, hf_mdshdr_eof, tvb,
246                                  trailer_start, MDSHDR_SIZE_BYTE, 0);
247         }
248     }
249     
250     /* If this protocol has a sub-dissector call it here, see section 1.8 */
251     if (tvb_bytes_exist (tvb, 0, MDSHDR_HEADER_SIZE+pktlen)) {
252         next_tvb = tvb_new_subset (tvb, MDSHDR_HEADER_SIZE, pktlen, pktlen);
253     }
254     else {
255         next_tvb = tvb_new_subset (tvb, MDSHDR_HEADER_SIZE, -1, -1);
256     }
257
258     /* Call the Fibre Channel dissector */
259     if (fc_dissector_handle) {
260         call_dissector (fc_dissector_handle, next_tvb, pinfo, tree);
261     }
262     else {
263         call_dissector (data_handle, next_tvb, pinfo, tree);
264     }
265 }
266
267
268 /* Register the protocol with Ethereal. This format is require because a script
269  * is used to build the C function that calls all the protocol registration.
270  */
271
272 void
273 proto_register_mdshdr(void)
274 {                 
275
276 /* Setup list of header fields  See Section 1.6.1 for details*/
277     static hf_register_info hf[] = {
278         { &hf_mdshdr_sof,
279           {"SOF", "mdshdr.sof", FT_UINT8, BASE_DEC, VALS(sof_vals), 0x0, "", HFILL}},
280         { &hf_mdshdr_pkt_len,
281           {"Packet Len", "mdshdr.plen", FT_UINT16, BASE_DEC, NULL, 0x0, "", HFILL}},
282         { &hf_mdshdr_dstidx,
283           {"Dst Index", "mdshdr.dstidx", FT_UINT16, BASE_HEX, NULL, 0xFFC, "", HFILL}},
284         { &hf_mdshdr_srcidx,
285           {"Src Index", "mdshdr.srcidx", FT_UINT16, BASE_HEX, NULL, 0x3FF, "", HFILL}},
286         { &hf_mdshdr_vsan,
287           {"VSAN", "mdshdr.vsan", FT_UINT16, BASE_DEC, NULL, 0x0FFF, "", HFILL}},
288         { &hf_mdshdr_eof,
289           {"EOF", "mdshdr.eof", FT_UINT8, BASE_DEC, VALS(eof_vals), 0x0, "", HFILL}},
290         { &hf_mdshdr_span,
291           {"SPAN Frame", "mdshdr.span", FT_UINT8, BASE_DEC, NULL, 0x0,
292            "", HFILL}}, 
293     };
294
295 /* Setup protocol subtree array */
296     static gint *ett[] = {
297         &ett_mdshdr,
298         &ett_mdshdr_hdr,
299         &ett_mdshdr_trlr
300     };
301
302 /* Register the protocol name and description */
303     proto_mdshdr = proto_register_protocol("MDS Header", "mdshdr", "mdshdr");
304
305 /* Required function calls to register the header fields and subtrees used */
306     proto_register_field_array(proto_mdshdr, hf, array_length(hf));
307     proto_register_subtree_array(ett, array_length(ett));
308 }
309
310
311 /* If this dissector uses sub-dissector registration add a registration routine.
312    This format is required because a script is used to find these routines and
313    create the code that calls these routines.
314 */
315 void
316 proto_reg_handoff_mdshdr(void)
317 {
318     dissector_handle_t mdshdr_handle;
319
320     mdshdr_handle = create_dissector_handle (dissect_mdshdr, proto_mdshdr);
321     dissector_add ("ethertype", ETHERTYPE_UNK, mdshdr_handle);
322     dissector_add ("ethertype", ETHERTYPE_FCFT, mdshdr_handle);
323
324     data_handle = find_dissector ("data");
325     fc_dissector_handle = find_dissector ("fc");
326 }