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