added some options and enhancements to the print output:
[obnox/wireshark/wip.git] / packet-cpfi.c
1 /* packet-cpfi.c
2  * Routines for CPFI Cross Point Frame Injector dissection
3  * CPFI - Cross Point Frame Injector is a CNT proprietary
4  * protocol used to carry Fibre Channel data over UDP
5  *
6  * Copyright 2003, Dave Sclarsky <dave_sclarsky[AT]cnt.com>
7  *
8  * $Id: packet-cpfi.c,v 1.6 2003/12/21 05:51:33 jmayer Exp $
9  *
10  * Ethereal - Network traffic analyzer
11  * By Gerald Combs <gerald@ethereal.com>
12  * Copyright 1998 Gerald Combs
13  *
14  * Copied from packet-m2tp.c
15  * Thanks to Heinz Prantner for his motivation and assistance
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 <glib.h>
37
38 #include <epan/packet.h>
39 #include "prefs.h"
40
41 void proto_reg_handoff_cpfi(void);
42
43 #define CPFI_DEFAULT_UDP_PORT      5000
44
45 /* SOF defines */
46 #define CPFI_FRAME_TYPE_MASK  0xF0000000
47 #define CPFI_FRAME_TYPE_SHIFT 28
48 #define CPFI_SOURCE_MASK      0x0FFC0000
49 #define CPFI_SOURCE_SHIFT     18
50 #define CPFI_DEST_MASK        0x0003FF00
51 #define CPFI_DEST_SHIFT       8
52 #define CPFI_SOF_TYPE_MASK    0x000000F0
53 #define CPFI_SPEED_MASK       0x0000000C
54 #define CPFI_OPM_ERROR_MASK   0x00000002
55 #define CPFI_FROM_LCM_MASK    0x00000001
56
57 /* EOF defines */
58 #define CPFI_EOF_TYPE_MASK    0x78000000
59 #define CPFI_EOF_ERROR_MASK   0x7FE00000
60
61
62 /* Initialize the protocol and registered fields */
63 static int proto_cpfi = -1;
64 static guint cpfi_configured_udp_port = CPFI_DEFAULT_UDP_PORT;
65 static guint cpfi_current_udp_port;
66 static int hf_cpfi_word_one = -1;
67 static int hf_cpfi_word_two = -1;
68 /* SOF word 1: */
69 static int hf_cpfi_frame_type = -1;
70 static int hf_cpfi_source = -1;
71 static int hf_cpfi_dest = -1;
72 static int hf_cpfi_SOF_type = -1;
73 static int hf_cpfi_speed = -1;
74 static int hf_cpfi_OPM_error = -1;
75 static int hf_cpfi_from_LCM = -1;
76 /* EOF */
77 static int hf_cpfi_CRC_32 = -1;
78 static int hf_cpfi_EOF_type = -1;
79
80 static guint32 word1;
81 static guint32 word2;
82 static guint8 frame_type;
83 static guint8 board;
84 static guint8 port;
85 const char *direction_and_port_string = "%u:%u";
86
87 /* Initialize the subtree pointers */
88 static gint ett_cpfi = -1;
89 static gint ett_cpfi_header = -1;
90 static gint ett_cpfi_footer = -1;
91
92 static dissector_handle_t fc_handle;
93 static dissector_handle_t data_handle;
94
95
96 static const value_string sof_type_vals[] = {
97     {0,     "SOFf"},
98     {1,     "SOFi2"},
99     {2,     "SOFn2"},
100     {3,     "SOFi3"},
101     {4,     "SOFn3"},
102     {5,     "SOFc1"},
103     {6,     "SOFi1"},
104     {7,     "SOFn1"},
105     {8,     "SOFc4"},
106     {9,     "SOFi4"},
107     {10,    "SOFn4"},
108     {0, NULL},
109 };
110
111 static const value_string speed_vals[] = {
112     {0,     "1 GBIT"},
113     {1,     "2 GBIT"},
114     {2,     "4 GBIT"},
115     {3,     "10 GBIT"},
116     {0, NULL},
117 };
118
119 static const value_string eof_type_vals[] = {
120     {0,     "EOFn"},
121     {1,     "EOFt"},
122     {2,     "EOFni"},
123     {3,     "EOFa"},
124     {4,     "EOFdt"},
125     {5,     "EOFdti"},
126     {6,     "EOFrt"},
127     {7,     "EOFrti"},
128     {0, NULL},
129 };
130
131 /* Header */
132 static void
133 dissect_cpfi_header(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
134 {
135   guint32 tda;
136   proto_item *extra_item = NULL;
137   proto_tree *extra_tree = NULL;
138
139   /* add a tree for the header */
140   if ( tree != NULL)
141   {
142     extra_item = proto_tree_add_protocol_format(tree, proto_cpfi, tvb, 0, -1, "Header");
143     extra_tree = proto_item_add_subtree(extra_item, ett_cpfi_header);
144   }
145
146   /* Extract the common header, and get the bits we need */
147   word1 = tvb_get_ntohl (tvb, 0);
148   word2 = tvb_get_ntohl (tvb, sizeof(word1));
149
150   frame_type = (word1 & CPFI_FRAME_TYPE_MASK) >> CPFI_FRAME_TYPE_SHIFT;
151   /* Choose either source or dest as the one to display */
152   if (pinfo->srcport == cpfi_current_udp_port)
153   {
154     direction_and_port_string = "[CPFI <-- % 2u/%-2u] ";
155     tda = (word1 & CPFI_DEST_MASK) >> CPFI_DEST_SHIFT;
156   }
157   else
158   {
159     direction_and_port_string = "[CPFI --> % 2u/%-2u] ";
160     tda = (word1 & CPFI_SOURCE_MASK) >> CPFI_SOURCE_SHIFT;
161   }
162   board = tda >> 4;
163   port = tda & 0x0f;
164
165   if (extra_tree) {
166     /* add word 1 components to the protocol tree */
167     proto_tree_add_item(extra_tree, hf_cpfi_word_one  , tvb, 0, 4, FALSE);
168
169     proto_tree_add_item(extra_tree, hf_cpfi_frame_type, tvb, 0, 4, FALSE);
170     proto_tree_add_item(extra_tree, hf_cpfi_source    , tvb, 0, 4, FALSE);
171     proto_tree_add_item(extra_tree, hf_cpfi_dest      , tvb, 0, 4, FALSE);
172     proto_tree_add_item(extra_tree, hf_cpfi_SOF_type  , tvb, 0, 4, FALSE);
173     proto_tree_add_item(extra_tree, hf_cpfi_speed     , tvb, 0, 4, FALSE);
174     proto_tree_add_item(extra_tree, hf_cpfi_OPM_error , tvb, 0, 4, FALSE);
175     proto_tree_add_item(extra_tree, hf_cpfi_from_LCM  , tvb, 0, 4, FALSE);
176
177     /* add word 2 components to the protocol tree */
178     proto_tree_add_item(extra_tree, hf_cpfi_word_two  , tvb, 4, 4, FALSE);
179   };
180 }
181
182 /* Footer */
183 static void
184 dissect_cpfi_footer(tvbuff_t *tvb, proto_tree *tree)
185 {
186   proto_item *extra_item = NULL;
187   proto_tree *extra_tree = NULL;
188
189   /* add a tree for the footer */
190   if ( tree != NULL)
191   {
192     extra_item = proto_tree_add_protocol_format(tree, proto_cpfi, tvb, 0, -1, "Footer");
193     extra_tree = proto_item_add_subtree(extra_item, ett_cpfi_footer);
194   }
195
196   if (extra_tree) {
197     proto_tree_add_item(extra_tree, hf_cpfi_CRC_32  , tvb, 0, 4, FALSE);
198     proto_tree_add_item(extra_tree, hf_cpfi_EOF_type, tvb, 4, 4, FALSE);
199   }
200 }
201
202 /* CPFI */
203 static void
204 dissect_cpfi(tvbuff_t *message_tvb, packet_info *pinfo, proto_tree *tree)
205 {
206   tvbuff_t *header_tvb, *body_tvb, *footer_tvb;
207   proto_item *cpfi_item = NULL;
208   proto_tree *cpfi_tree = NULL;
209   gint length, reported_length, body_length, reported_body_length;
210
211   /* In the interest of speed, if "tree" is NULL, don't do any work not
212      necessary to generate protocol tree items. */
213   if (tree) {
214     /* create the protocol tree */
215     cpfi_item = proto_tree_add_item(tree, proto_cpfi, message_tvb, 0, -1, FALSE);
216     cpfi_tree = proto_item_add_subtree(cpfi_item, ett_cpfi);
217   }
218
219   /* Set up the frame controls - can we do better than this? */
220   pinfo->sof_eof = 0;
221   pinfo->sof_eof = PINFO_SOF_FIRST_FRAME;
222   pinfo->sof_eof |= PINFO_EOF_LAST_FRAME;
223   pinfo->sof_eof |= PINFO_EOF_INVALID;
224
225   /* dissect the message */
226
227   /* extract and process the header */
228   header_tvb = tvb_new_subset(message_tvb, 0, 8, 8);
229   dissect_cpfi_header(header_tvb, pinfo, cpfi_tree);
230
231   /* Got good CPFI frame? */
232   if ( (frame_type == 9) && fc_handle )
233   {
234     /* create a tvb for the rest of the fc frame (exclude the checksum and CPFI trailer) */
235     length = tvb_length_remaining(message_tvb, 8);
236     reported_length = tvb_reported_length_remaining(message_tvb, 8);
237     if (reported_length < 8)
238     {
239       /* We don't even have enough for the footer. */
240       body_tvb = tvb_new_subset(message_tvb, 8, -1, -1);
241       call_dissector(data_handle, body_tvb, pinfo, tree);
242       return;
243     }
244     /* Length of packet, minus the footer. */
245     reported_body_length = reported_length - 8;
246     /* How much of that do we have in the tvbuff? */
247     body_length = length;
248     if (body_length > reported_body_length)
249       body_length = reported_body_length;
250     body_tvb = tvb_new_subset(message_tvb, 8, body_length, reported_body_length);
251     call_dissector(fc_handle, body_tvb, pinfo, tree);
252
253     /* add more info, now that FC added it's */
254     proto_item_append_text(cpfi_item, direction_and_port_string, board, port);
255     if (check_col(pinfo->cinfo, COL_INFO))
256     {
257       col_prepend_fstr(pinfo->cinfo, COL_INFO, direction_and_port_string, board, port);
258     }
259
260     /* Do the footer */
261     length = tvb_length_remaining(message_tvb, 8+body_length);
262     if (length < 0)
263     {
264       /* The footer wasn't captured at all.
265          XXX - we'd like to throw a BoundsError if that's the case. */
266       return;
267     }
268     footer_tvb = tvb_new_subset(message_tvb, 8+body_length, length, 8);
269     dissect_cpfi_footer(footer_tvb, cpfi_tree);
270   }
271   else if ( data_handle )
272   {
273     /* create a tvb for the rest of the frame */
274     body_tvb = tvb_new_subset(message_tvb, 8, -1, -1);
275     call_dissector(data_handle, body_tvb, pinfo, tree);
276   }
277
278 }
279
280 /* Register the protocol with Ethereal */
281 void
282 proto_register_cpfi(void)
283 {
284   module_t *cpfi_module;
285
286   /* Setup list of header fields */
287   static hf_register_info hf[] = {
288     { &hf_cpfi_word_one,
289       { "Word one", "cpfi.word_one",
290         FT_UINT32, BASE_HEX, NULL, 0x0,
291         "", HFILL}},
292     { &hf_cpfi_word_two,
293       { "Word two", "cfpi.word_two",
294         FT_UINT32, BASE_HEX, NULL, 0x0,
295         "", HFILL}},
296
297     { &hf_cpfi_frame_type,
298       { "FrmType", "cpfi.frmtype",
299         FT_UINT32, BASE_HEX, NULL, CPFI_FRAME_TYPE_MASK,
300         "Frame Type", HFILL}},
301     { &hf_cpfi_source,
302       { "srcTDA", "cpfi.srcTDA",
303         FT_UINT32, BASE_HEX, NULL, CPFI_SOURCE_MASK,
304         "Source TDA (10 bits)", HFILL}},
305     { &hf_cpfi_dest,
306       { "dstTDA", "cpfi.dstTDA",
307         FT_UINT32, BASE_HEX, NULL, CPFI_DEST_MASK,
308         "Source TDA (10 bits)", HFILL}},
309     { &hf_cpfi_SOF_type,
310       { "SOFtype", "cpfi.SOFtype",
311         FT_UINT32, BASE_HEX, VALS(sof_type_vals), CPFI_SOF_TYPE_MASK,
312         "SOF Type", HFILL}},
313     { &hf_cpfi_speed,
314       { "speed", "cpfi.speed",
315         FT_UINT32, BASE_HEX, VALS(speed_vals), CPFI_SPEED_MASK,
316         "SOF Type", HFILL}},
317     { &hf_cpfi_OPM_error,
318       { "OPMerror", "cpfi.OPMerror",
319         FT_BOOLEAN, 32, NULL, CPFI_OPM_ERROR_MASK,
320         "OPM Error?", HFILL}},
321     { &hf_cpfi_from_LCM,
322       { "fromLCM", "cpfi.fromLCM",
323         FT_BOOLEAN, 32, NULL, CPFI_FROM_LCM_MASK,
324         "from LCM?", HFILL}},
325
326     { &hf_cpfi_CRC_32,
327       { "CRC-32", "cpfi.crc-32",
328         FT_UINT32, BASE_HEX, NULL, 0x0,
329         "", HFILL}},
330
331     { &hf_cpfi_EOF_type,
332       { "EOFtype", "cpfi.EOFtype",
333         FT_UINT32, BASE_HEX, VALS(eof_type_vals), CPFI_EOF_TYPE_MASK,
334         "EOF Type", HFILL}},
335   };
336
337
338   /* Setup protocol subtree array */
339   static gint *ett[] = {
340     &ett_cpfi,
341     &ett_cpfi_header,
342     &ett_cpfi_footer
343   };
344
345   /* Register the protocol name and description */
346   proto_cpfi = proto_register_protocol("Cross Point Frame Injector ", "CPFI",  "cpfi");
347
348   /* Required function calls to register the header fields and subtrees used */
349   proto_register_field_array(proto_cpfi, hf, array_length(hf));
350   proto_register_subtree_array(ett, array_length(ett));
351
352   /* Register our configuration options for CPFI */
353   cpfi_module = prefs_register_protocol(proto_cpfi, proto_reg_handoff_cpfi);
354   prefs_register_uint_preference(cpfi_module, "udp.port", "CPFI UDP Port",
355                  "Set the port for CPFI messages (if other"
356                  " than the default of 5000)",
357                  10, &cpfi_configured_udp_port);
358 }
359
360 void
361 proto_reg_handoff_cpfi(void)
362 {
363   static int cpfi_init_complete = FALSE;
364   static dissector_handle_t cpfi_handle;
365   if ( !cpfi_init_complete )
366   {
367     cpfi_init_complete = TRUE;
368     fc_handle     = find_dissector("fc");
369     data_handle   = find_dissector("data");
370     cpfi_handle   = create_dissector_handle(dissect_cpfi, proto_cpfi);
371   }
372   else
373   {
374     dissector_delete("udp.port", cpfi_current_udp_port, cpfi_handle);
375   }
376
377   /* remember udp port for later use */
378   cpfi_current_udp_port = cpfi_configured_udp_port;
379
380   dissector_add("udp.port", cpfi_current_udp_port, cpfi_handle);
381 }