Fix up a bunch of arguments to "dissect_ber_identifier()" to match its
[obnox/wireshark/wip.git] / epan / dissectors / 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$
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 <string.h>
37
38 #include <glib.h>
39
40 #include <epan/packet.h>
41 #include <epan/prefs.h>
42
43 void proto_reg_handoff_cpfi(void);
44
45 #define CPFI_DEFAULT_UDP_PORT      5000
46 #define CPFI_DEFAULT_TTOT_UDP_PORT 5001
47 #define FIRST_TIO_CARD_ADDRESS    0x380
48
49
50 /* SOF defines */
51 #define CPFI_FRAME_TYPE_MASK  0xF0000000
52 #define CPFI_FRAME_TYPE_SHIFT 28
53 #define CPFI_SOURCE_MASK      0x0FFC0000
54 #define CPFI_SOURCE_SHIFT     18
55 #define CPFI_DEST_MASK        0x0003FF00
56 #define CPFI_DEST_SHIFT       8
57 #define CPFI_SOF_TYPE_MASK    0x000000F0
58 #define CPFI_SPEED_MASK       0x0000000C
59 #define CPFI_OPM_ERROR_MASK   0x00000002
60 #define CPFI_FROM_LCM_MASK    0x00000001
61
62 /* EOF defines */
63 #define CPFI_EOF_TYPE_MASK    0x78000000
64 #define CPFI_EOF_ERROR_MASK   0x7FE00000
65
66 /* configurable parameters */
67 static guint cpfi_udp_port = CPFI_DEFAULT_UDP_PORT;
68 static guint cpfi_ttot_udp_port = CPFI_DEFAULT_TTOT_UDP_PORT;
69 static gboolean cpfi_arrow_moves = TRUE;
70
71 /* Initialize the protocol and registered fields */
72 static int proto_cpfi = -1;
73 static int hf_cpfi_word_one = -1;
74 static int hf_cpfi_word_two = -1;
75 /* SOF word 1: */
76 static int hf_cpfi_frame_type = -1;
77 static int hf_cpfi_source = -1;
78 static int hf_cpfi_dest = -1;
79 static int hf_cpfi_SOF_type = -1;
80 static int hf_cpfi_speed = -1;
81 static int hf_cpfi_OPM_error = -1;
82 static int hf_cpfi_from_LCM = -1;
83 /* EOF */
84 static int hf_cpfi_CRC_32 = -1;
85 static int hf_cpfi_EOF_type = -1;
86 /* Hidden items */
87 static int hf_cpfi_t_instance = -1;
88 static int hf_cpfi_t_src_instance = -1;
89 static int hf_cpfi_t_dst_instance = -1;
90 static int hf_cpfi_t_board = -1;
91 static int hf_cpfi_t_src_board = -1;
92 static int hf_cpfi_t_dst_board = -1;
93 static int hf_cpfi_t_port = -1;
94 static int hf_cpfi_t_src_port = -1;
95 static int hf_cpfi_t_dst_port = -1;
96
97 static guint32 word1;
98 static guint32 word2;
99 static guint8 frame_type;
100 static char src_str[20];
101 static char dst_str[20];
102 static char l_to_r_arrow[] = "-->";
103 static char r_to_l_arrow[] = "<--";
104 static const char *left = src_str;
105 static const char *right = dst_str;
106 static const char *arrow = l_to_r_arrow;
107 static const char direction_and_port_string[] = "[%s %s %s] ";
108
109
110 /* Initialize the subtree pointers */
111 static gint ett_cpfi = -1;
112 static gint ett_cpfi_header = -1;
113 static gint ett_cpfi_footer = -1;
114
115 static dissector_handle_t fc_handle;
116 static dissector_handle_t data_handle;
117
118
119 static const value_string sof_type_vals[] = {
120     {0,     "SOFf"},
121     {1,     "SOFi2"},
122     {2,     "SOFn2"},
123     {3,     "SOFi3"},
124     {4,     "SOFn3"},
125     {5,     "SOFc1"},
126     {6,     "SOFi1"},
127     {7,     "SOFn1"},
128     {8,     "SOFc4"},
129     {9,     "SOFi4"},
130     {10,    "SOFn4"},
131     {0, NULL},
132 };
133
134 static const value_string speed_vals[] = {
135     {0,     "1 GBIT"},
136     {1,     "2 GBIT"},
137     {2,     "4 GBIT"},
138     {3,     "10 GBIT"},
139     {0, NULL},
140 };
141
142 static const value_string eof_type_vals[] = {
143     {0,     "EOFn"},
144     {1,     "EOFt"},
145     {2,     "EOFni"},
146     {3,     "EOFa"},
147     {4,     "EOFdt"},
148     {5,     "EOFdti"},
149     {6,     "EOFrt"},
150     {7,     "EOFrti"},
151     {0, NULL},
152 };
153
154 /* Header */
155 static void
156 dissect_cpfi_header(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
157 {
158   guint32 tda;
159   guint32 src;
160   guint8 src_instance = 0;
161   guint8 src_board = 0;
162   guint8 src_port = 0;
163   guint32 dst;
164   guint8 dst_instance = 0;
165   guint8 dst_board = 0;
166   guint8 dst_port = 0;
167   proto_item *extra_item = NULL;
168   proto_tree *extra_tree = NULL;
169
170   /* add a tree for the header */
171   if ( tree != NULL)
172   {
173     extra_item = proto_tree_add_protocol_format(tree, proto_cpfi, tvb, 0, -1, "Header");
174     extra_tree = proto_item_add_subtree(extra_item, ett_cpfi_header);
175   }
176
177   /* Extract the common header, and get the bits we need */
178   word1 = tvb_get_ntohl (tvb, 0);
179   word2 = tvb_get_ntohl (tvb, sizeof(word1));
180
181   /* Get the frame type, for later use */
182   frame_type = (word1 & CPFI_FRAME_TYPE_MASK) >> CPFI_FRAME_TYPE_SHIFT;
183
184   /* Figure out where the frame came from. dstTDA is source of frame! */
185   tda = (word1 & CPFI_DEST_MASK) >> CPFI_DEST_SHIFT;
186   if ( tda >= FIRST_TIO_CARD_ADDRESS )
187   {
188     strncpy(src_str, " CPFI", sizeof(src_str));
189     src = 0;                            /* Make it smallest */
190   }
191   else
192   {
193     src_instance = pinfo->src.data[2]-1;
194     src_board = tda >> 4;
195     src_port = tda & 0x0f;
196     src = (1 << 24)  +  (src_instance << 16) + (src_board << 8) + src_port;
197     snprintf(src_str, sizeof(src_str), "%u.%u.%u", src_instance, src_board, src_port);
198   }
199
200   /* Figure out where the frame is going. srcTDA is destination of frame! */
201   tda = (word1 & CPFI_SOURCE_MASK) >> CPFI_SOURCE_SHIFT;
202   if ( tda >= FIRST_TIO_CARD_ADDRESS )
203   {
204     strncpy(dst_str, " CPFI", sizeof(dst_str));
205     dst = 0;                            /* Make it smallest */
206   }
207   else
208   {
209     dst_instance = pinfo->dst.data[2]-1;
210     dst_board = tda >> 4;
211     dst_port = tda & 0x0f;
212     dst = (1 << 24)  +  (dst_instance << 16) + (dst_board << 8) + dst_port;
213     snprintf(dst_str, sizeof(dst_str), "%u.%u.%u", dst_instance, dst_board, dst_port);
214   }
215
216   /* Set up the source and destination and arrow per user configuration. */
217   if ( cpfi_arrow_moves  &&  (dst < src) )
218   {
219     left = dst_str;
220     arrow = r_to_l_arrow;
221     right = src_str;
222   }
223   else
224   {
225     left = src_str;
226     arrow = l_to_r_arrow;
227     right = dst_str;
228   }
229
230   if (extra_tree) {
231     /* For "real" TDAs (i.e. not for microTDAs), add hidden addresses to allow filtering */
232     if ( src != 0 )
233     {
234         proto_tree_add_bytes_hidden(extra_tree, hf_cpfi_t_instance, tvb, 0, 1, &src_instance);
235         proto_tree_add_bytes_hidden(extra_tree, hf_cpfi_t_src_instance, tvb, 0, 1, &src_instance);
236         proto_tree_add_bytes_hidden(extra_tree, hf_cpfi_t_board, tvb, 0, 1, &src_board);
237         proto_tree_add_bytes_hidden(extra_tree, hf_cpfi_t_src_board, tvb, 0, 1, &src_board);
238         proto_tree_add_bytes_hidden(extra_tree, hf_cpfi_t_port, tvb, 0, 1, &src_port);
239         proto_tree_add_bytes_hidden(extra_tree, hf_cpfi_t_src_port, tvb, 0, 1, &src_port);
240     }
241     if ( dst != 0 )
242     {
243         proto_tree_add_bytes_hidden(extra_tree, hf_cpfi_t_instance, tvb, 0, 1, &dst_instance);
244         proto_tree_add_bytes_hidden(extra_tree, hf_cpfi_t_dst_instance, tvb, 0, 1, &dst_instance);
245         proto_tree_add_bytes_hidden(extra_tree, hf_cpfi_t_board, tvb, 0, 1, &dst_board);
246         proto_tree_add_bytes_hidden(extra_tree, hf_cpfi_t_dst_board, tvb, 0, 1, &dst_board);
247         proto_tree_add_bytes_hidden(extra_tree, hf_cpfi_t_port, tvb, 0, 1, &dst_port);
248         proto_tree_add_bytes_hidden(extra_tree, hf_cpfi_t_dst_port, tvb, 0, 1, &dst_port);
249     }
250
251     /* add word 1 components to the protocol tree */
252     proto_tree_add_item(extra_tree, hf_cpfi_word_one  , tvb, 0, 4, FALSE);
253
254     proto_tree_add_item(extra_tree, hf_cpfi_frame_type, tvb, 0, 4, FALSE);
255     proto_tree_add_item(extra_tree, hf_cpfi_source    , tvb, 0, 4, FALSE);
256     proto_tree_add_item(extra_tree, hf_cpfi_dest      , tvb, 0, 4, FALSE);
257     proto_tree_add_item(extra_tree, hf_cpfi_SOF_type  , tvb, 0, 4, FALSE);
258     proto_tree_add_item(extra_tree, hf_cpfi_speed     , tvb, 0, 4, FALSE);
259     proto_tree_add_item(extra_tree, hf_cpfi_OPM_error , tvb, 0, 4, FALSE);
260     proto_tree_add_item(extra_tree, hf_cpfi_from_LCM  , tvb, 0, 4, FALSE);
261
262     /* add word 2 components to the protocol tree */
263     proto_tree_add_item(extra_tree, hf_cpfi_word_two  , tvb, 4, 4, FALSE);
264   };
265 }
266
267 /* Footer */
268 static void
269 dissect_cpfi_footer(tvbuff_t *tvb, proto_tree *tree)
270 {
271   proto_item *extra_item = NULL;
272   proto_tree *extra_tree = NULL;
273
274   /* add a tree for the footer */
275   if ( tree != NULL)
276   {
277     extra_item = proto_tree_add_protocol_format(tree, proto_cpfi, tvb, 0, -1, "Footer");
278     extra_tree = proto_item_add_subtree(extra_item, ett_cpfi_footer);
279   }
280
281   if (extra_tree) {
282     proto_tree_add_item(extra_tree, hf_cpfi_CRC_32  , tvb, 0, 4, FALSE);
283     proto_tree_add_item(extra_tree, hf_cpfi_EOF_type, tvb, 4, 4, FALSE);
284   }
285 }
286
287 /* CPFI */
288 static void
289 dissect_cpfi(tvbuff_t *message_tvb, packet_info *pinfo, proto_tree *tree)
290 {
291   tvbuff_t *header_tvb, *body_tvb, *footer_tvb;
292   proto_item *cpfi_item = NULL;
293   proto_tree *cpfi_tree = NULL;
294   gint length, reported_length, body_length, reported_body_length;
295
296   /* In the interest of speed, if "tree" is NULL, don't do any work not
297      necessary to generate protocol tree items. */
298   if (tree) {
299     /* create the protocol tree */
300     cpfi_item = proto_tree_add_item(tree, proto_cpfi, message_tvb, 0, -1, FALSE);
301     cpfi_tree = proto_item_add_subtree(cpfi_item, ett_cpfi);
302   }
303
304   /* Set up the frame controls - can we do better than this? */
305   pinfo->sof_eof = 0;
306   pinfo->sof_eof = PINFO_SOF_FIRST_FRAME;
307   pinfo->sof_eof |= PINFO_EOF_LAST_FRAME;
308   pinfo->sof_eof |= PINFO_EOF_INVALID;
309
310   /* dissect the message */
311
312   /* extract and process the header */
313   header_tvb = tvb_new_subset(message_tvb, 0, 8, 8);
314   dissect_cpfi_header(header_tvb, pinfo, cpfi_tree);
315
316   /* Got good CPFI frame? */
317   if ( (frame_type == 9) && fc_handle )
318   {
319     /* create a tvb for the rest of the fc frame (exclude the checksum and CPFI trailer) */
320     length = tvb_length_remaining(message_tvb, 8);
321     reported_length = tvb_reported_length_remaining(message_tvb, 8);
322     if (reported_length < 8)
323     {
324       /* We don't even have enough for the footer. */
325       body_tvb = tvb_new_subset(message_tvb, 8, -1, -1);
326       call_dissector(data_handle, body_tvb, pinfo, tree);
327       return;
328     }
329     /* Length of packet, minus the footer. */
330     reported_body_length = reported_length - 8;
331     /* How much of that do we have in the tvbuff? */
332     body_length = length;
333     if (body_length > reported_body_length)
334       body_length = reported_body_length;
335     body_tvb = tvb_new_subset(message_tvb, 8, body_length, reported_body_length);
336     call_dissector(fc_handle, body_tvb, pinfo, tree);
337
338     /* add more info, now that FC added it's */
339     proto_item_append_text(cpfi_item, direction_and_port_string, left, arrow, right);
340     if (check_col(pinfo->cinfo, COL_INFO))
341     {
342       col_prepend_fstr(pinfo->cinfo, COL_INFO, direction_and_port_string, left, arrow, right);
343     }
344
345     /* Do the footer */
346     length = tvb_length_remaining(message_tvb, 8+body_length);
347     if (length < 0)
348     {
349       /* The footer wasn't captured at all.
350          XXX - we'd like to throw a BoundsError if that's the case. */
351       return;
352     }
353     footer_tvb = tvb_new_subset(message_tvb, 8+body_length, length, 8);
354     dissect_cpfi_footer(footer_tvb, cpfi_tree);
355   }
356   else if ( data_handle )
357   {
358     /* create a tvb for the rest of the frame */
359     body_tvb = tvb_new_subset(message_tvb, 8, -1, -1);
360     call_dissector(data_handle, body_tvb, pinfo, tree);
361   }
362
363 }
364
365 /* Register the protocol with Ethereal */
366 void
367 proto_register_cpfi(void)
368 {
369   module_t *cpfi_module;
370
371   /* Setup list of header fields */
372   static hf_register_info hf[] = {
373
374     { &hf_cpfi_word_one,
375       { "Word one", "cpfi.word_one",
376         FT_UINT32, BASE_HEX, NULL, 0x0,
377         "", HFILL}},
378     { &hf_cpfi_word_two,
379       { "Word two", "cfpi.word_two",
380         FT_UINT32, BASE_HEX, NULL, 0x0,
381         "", HFILL}},
382
383     { &hf_cpfi_frame_type,
384       { "FrmType", "cpfi.frmtype",
385         FT_UINT32, BASE_HEX, NULL, CPFI_FRAME_TYPE_MASK,
386         "Frame Type", HFILL}},
387     { &hf_cpfi_source,
388       { "srcTDA", "cpfi.srcTDA",
389         FT_UINT32, BASE_HEX, NULL, CPFI_SOURCE_MASK,
390         "Source TDA (10 bits)", HFILL}},
391     { &hf_cpfi_dest,
392       { "dstTDA", "cpfi.dstTDA",
393         FT_UINT32, BASE_HEX, NULL, CPFI_DEST_MASK,
394         "Source TDA (10 bits)", HFILL}},
395     { &hf_cpfi_SOF_type,
396       { "SOFtype", "cpfi.SOFtype",
397         FT_UINT32, BASE_HEX, VALS(sof_type_vals), CPFI_SOF_TYPE_MASK,
398         "SOF Type", HFILL}},
399     { &hf_cpfi_speed,
400       { "speed", "cpfi.speed",
401         FT_UINT32, BASE_HEX, VALS(speed_vals), CPFI_SPEED_MASK,
402         "SOF Type", HFILL}},
403     { &hf_cpfi_OPM_error,
404       { "OPMerror", "cpfi.OPMerror",
405         FT_BOOLEAN, 32, NULL, CPFI_OPM_ERROR_MASK,
406         "OPM Error?", HFILL}},
407     { &hf_cpfi_from_LCM,
408       { "fromLCM", "cpfi.fromLCM",
409         FT_BOOLEAN, 32, NULL, CPFI_FROM_LCM_MASK,
410         "from LCM?", HFILL}},
411
412     { &hf_cpfi_CRC_32,
413       { "CRC-32", "cpfi.crc-32",
414         FT_UINT32, BASE_HEX, NULL, 0x0,
415         "", HFILL}},
416
417     { &hf_cpfi_EOF_type,
418       { "EOFtype", "cpfi.EOFtype",
419         FT_UINT32, BASE_HEX, VALS(eof_type_vals), CPFI_EOF_TYPE_MASK,
420         "EOF Type", HFILL}},
421
422     { &hf_cpfi_t_instance,
423       { "Instance", "cpfi.instance",
424         FT_BYTES, BASE_DEC,
425        NULL, 0x0, "", HFILL}},
426
427     { &hf_cpfi_t_src_instance,
428       { "Source Instance", "cpfi.src_instance",
429         FT_BYTES, BASE_DEC,
430        NULL, 0x0, "", HFILL}},
431
432     { &hf_cpfi_t_dst_instance,
433       { "Destination Instance", "cpfi.dst_instance",
434         FT_BYTES, BASE_DEC,
435        NULL, 0x0, "", HFILL}},
436
437     { &hf_cpfi_t_board,
438       { "Board", "cpfi.board",
439         FT_BYTES, BASE_DEC,
440        NULL, 0x0, "", HFILL}},
441
442     { &hf_cpfi_t_src_board,
443       { "Source Board", "cpfi.src_board",
444         FT_BYTES, BASE_DEC,
445        NULL, 0x0, "", HFILL}},
446
447     { &hf_cpfi_t_dst_board,
448       { "Destination Board", "cpfi.dst_board",
449         FT_BYTES, BASE_DEC,
450        NULL, 0x0, "", HFILL}},
451
452     { &hf_cpfi_t_port,
453       { "Port", "cpfi.port",
454         FT_BYTES, BASE_DEC,
455        NULL, 0x0, "", HFILL}},
456
457     { &hf_cpfi_t_src_port,
458       { "Source Port", "cpfi.src_port",
459         FT_BYTES, BASE_DEC,
460        NULL, 0x0, "", HFILL}},
461
462     { &hf_cpfi_t_dst_port,
463       { "Destination Port", "cpfi.dst_port",
464         FT_BYTES, BASE_DEC,
465        NULL, 0x0, "", HFILL}},
466   };
467
468
469   /* Setup protocol subtree array */
470   static gint *ett[] = {
471     &ett_cpfi,
472     &ett_cpfi_header,
473     &ett_cpfi_footer
474   };
475
476
477   /* Register the protocol name and description */
478   proto_cpfi = proto_register_protocol("Cross Point Frame Injector ", "CPFI",  "cpfi");
479
480   /* Required function calls to register the header fields and subtrees used */
481   proto_register_field_array(proto_cpfi, hf, array_length(hf));
482   proto_register_subtree_array(ett, array_length(ett));
483
484   /* Register our configuration options for CPFI */
485   cpfi_module = prefs_register_protocol(proto_cpfi, proto_reg_handoff_cpfi);
486   prefs_register_uint_preference(cpfi_module, "udp.port", "CPFI UDP Port",
487                  "Set the port for CPFI messages (if other"
488                  " than the default of 5000)",
489                  10, &cpfi_udp_port);
490   prefs_register_uint_preference(cpfi_module, "udp.port2", "InstanceToInstance UDP Port",
491                  "Set the port for InstanceToInstance messages (if other"
492                  " than the default of 5001)",
493                  10, &cpfi_ttot_udp_port);
494   prefs_register_bool_preference(cpfi_module, "arrow_ctl",
495                 "Enable Active Arrow Control",
496                 "Control the way the '-->' is displayed."
497                 " When enabled, keeps the 'lowest valued' endpoint of the src-dest pair"
498                 " on the left, and the arrow moves to distinguish source from dest."
499                 " When disabled, keeps the arrow pointing right so the source of the frame"
500                 " is always on the left.",
501                 &cpfi_arrow_moves);
502
503 }
504
505 void
506 proto_reg_handoff_cpfi(void)
507 {
508   static int cpfi_init_complete = FALSE;
509   static dissector_handle_t cpfi_handle;
510   static dissector_handle_t ttot_handle;
511   if ( !cpfi_init_complete )
512   {
513     cpfi_init_complete = TRUE;
514     fc_handle     = find_dissector("fc");
515     data_handle   = find_dissector("data");
516     cpfi_handle   = create_dissector_handle(dissect_cpfi, proto_cpfi);
517     ttot_handle   = create_dissector_handle(dissect_cpfi, proto_cpfi);
518   }
519   else
520   {
521     dissector_delete("udp.port", cpfi_udp_port, cpfi_handle);
522     dissector_delete("udp.port", cpfi_ttot_udp_port, ttot_handle);
523   }
524
525   dissector_add("udp.port", cpfi_udp_port, cpfi_handle);
526   dissector_add("udp.port", cpfi_ttot_udp_port, ttot_handle);
527 }