Use MAC address documentation range in filter examples
[metze/wireshark/wip.git] / epan / dissectors / packet-netanalyzer.c
1 /* packet-netanalyzer.c
2  * Dissector for Hilscher netANALYZER frames.
3  * Copyright 2008-2011, Hilscher GmbH, Holger Pfrommer hpfrommer[AT]hilscher.com
4  *
5  * Packet structure:
6  * +---------------------------+
7  * |           Header          |
8  * |         (4 Octets)        |
9  * +---------------------------+
10  * |           Payload         |
11  * .                           .
12  * .                           .
13  * .                           .
14  *
15  * Description:
16  * The header field contains a 32-bit value in little-endian byte order.
17  * The low-order 8 bits are a set of error flags for the packet:
18  *     0x00000001 - MII RX_ER
19  *     0x00000002 - alignment error
20  *     0x00000004 - FCS error
21  *     0x00000008 - frame too long
22  *     0x00000010 - SFD error
23  *     0x00000020 - frame shorter than 64 bytes
24  *     0x00000040 - preamble shorter than 7 bytes
25  *     0x00000080 - preamble longer than 7 bytes/li>
26  * The next bit, 0x00000100, is set if the packet arrived on the GPIO port rather tha the Ethernet port.
27  * The next bit, 0x00000200, is set if the packet was received in transparent capture mode.
28  *   That should never be set for LINKTYPE_NETANALYZER and should always be set for LINKTYPE_NETANALYZER_TRANSPARENT.
29  * The next 4 bits, 0x00003C00, are a bitfield giving the version of the header field; the current version is 1.
30  * The next 2 bits, 0x0000C000, are the capture port/GPIO number, from 0 to 3.
31  * The next 12 bits, 0x0FFF0000, are the frame length, in bytes.
32  * The topmost 4 bits, 0xF0000000, are reserved.
33  * The payload is an Ethernet frame, beginning with the MAC header and ending with the FCS, for LINKTYPE_NETANALYZER,
34  *   and an Ethernet frame, beginning with the preamble and ending with the FCS, for LINKTYPE_NETANALYZER_TRANSPARENT.
35  *
36  *
37  * Wireshark - Network traffic analyzer
38  * By Gerald Combs <gerald[AT]wireshark.org>
39  * Copyright 1999 Gerald Combs
40  *
41  * This program is free software; you can redistribute it and/or
42  * modify it under the terms of the GNU General Public License
43  * as published by the Free Software Foundation; either version 2
44  * of the License, or (at your option) any later version.
45  *
46  * This program is distributed in the hope that it will be useful,
47  * but WITHOUT ANY WARRANTY; without even the implied warranty of
48  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
49  * GNU General Public License for more details.
50  *
51  * You should have received a copy of the GNU General Public License
52  * along with this program; if not, write to the Free Software
53  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
54  */
55
56
57 #include "config.h"
58
59 #include <epan/packet.h>
60 #include <epan/expert.h>
61 #include <wiretap/wtap.h>
62
63 void proto_register_netanalyzer(void);
64 void proto_reg_handoff_netanalyzer(void);
65
66 #define HEADER_SIZE  4
67 #define INFO_TYPE_OFFSET    18
68
69 #define MSK_RX_ERR             0x01
70 #define MSK_ALIGN_ERR          0x02
71 #define MSK_FCS_ERROR          0x04
72 #define MSK_TOO_LONG           0x08
73 #define MSK_SFD_ERROR          0x10
74 #define MSK_SHORT_FRAME        0x20
75 #define MSK_SHORT_PREAMBLE     0x40
76 #define MSK_LONG_PREAMBLE      0x80
77
78 static const char *msk_strings[] = {
79   "MII RX_ER error",                /* 0x01 */
80   "Alignment error",                /* 0x02 */
81   "FCS error",                      /* 0x04 */
82   "Frame too long",                 /* 0x08 */
83   "No valid SFD found",             /* 0x10 */
84   "Frame smaller 64 bytes",         /* 0x20 */
85   "Preamble shorter than 7 bytes",  /* 0x40 */
86   "Preamble longer than 7 bytes"    /* 0x80 */
87 };
88
89 #define SRT_PORT_NUM           6
90 #define SRT_VERSION            2
91 #define SRT_GPIO_FLAG          0
92 #define MSK_PACKET_STATUS      0xff
93 #define MSK_LENGTH             0x0fff
94 #define MSK_TRANSPARENT_MODE   0x02
95
96
97 static const value_string gpio_number[] = {
98   { 0x0, "GPIO 0" },
99   { 0x1, "GPIO 1" },
100   { 0x2, "GPIO 2" },
101   { 0x3, "GPIO 3" },
102   { 0,   NULL }
103 };
104
105 static const value_string gpio_edge_vals[] = {
106   { 0x0, "rising edge" },
107   { 0x1, "falling edge" },
108   { 0,   NULL }
109 };
110
111
112 static dissector_handle_t  eth_dissector_handle;
113 static dissector_handle_t  data_dissector_handle;
114
115 static gint  proto_netanalyzer           = -1;
116
117 static gint  hf_netanalyzer_gpio_number              = -1;
118 static gint  hf_netanalyzer_gpio_edge                = -1;
119 static gint  hf_netanalyzer_port                     = -1;
120 static gint  hf_netanalyzer_length                   = -1;
121 static gint  hf_netanalyzer_status                   = -1;
122 static gint  hf_netanalyzer_status_rx_err            = -1;
123 static gint  hf_netanalyzer_status_align_err         = -1;
124 static gint  hf_netanalyzer_status_fcs               = -1;
125 static gint  hf_netanalyzer_status_too_long          = -1;
126 static gint  hf_netanalyzer_status_sfd_error         = -1;
127 static gint  hf_netanalyzer_status_short_frame       = -1;
128 static gint  hf_netanalyzer_status_short_preamble    = -1;
129 static gint  hf_netanalyzer_status_long_preamble     = -1;
130
131 static gint  ett_netanalyzer                         = -1;
132 static gint  ett_netanalyzer_status                  = -1;
133 static gint  ett_netanalyzer_transparent             = -1;
134
135 static expert_field ei_netanalyzer_header_version_wrong = EI_INIT;
136 static expert_field ei_netanalyzer_gpio_def_none = EI_INIT;
137 static expert_field ei_netanalyzer_header_version_none = EI_INIT;
138 static expert_field ei_netanalyzer_transparent_frame = EI_INIT;
139 static expert_field ei_netanalyzer_alignment_error = EI_INIT;
140
141 /* common routine for Ethernet and transparent mode */
142 static int
143 dissect_netanalyzer_common(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
144 {
145   proto_item              *ti = NULL;
146   proto_tree              *netanalyzer_header_tree = NULL;
147   proto_item              *ti_status = NULL;
148   proto_tree              *netanalyzer_status_tree = NULL;
149   guint32                 packet_status;
150   guint32                 port_num;
151   guint32                 frame_length;
152   guint                   is_gpio;
153   guint32                 offset;
154   guint                   gpio_num;
155   guint                   gpio_edge;
156   guint                   version;
157   guint                   idx;
158
159   if (tree)
160   {
161     /* generate netANALYZER tree */
162     ti = proto_tree_add_item(tree, proto_netanalyzer, tvb, 0, HEADER_SIZE, ENC_NA);
163     netanalyzer_header_tree = proto_item_add_subtree(ti, ett_netanalyzer);
164
165     is_gpio = (tvb_get_guint8(tvb, 1) >> SRT_GPIO_FLAG) & 0x1;
166
167     if (!is_gpio)
168     {
169       /* normal packet, no GPIO */
170
171       /* decode version */
172       version = (tvb_get_guint8(tvb, 1) >> SRT_VERSION) & 0xf;
173       if (version != 1)
174       {
175         /* something is wrong */
176         expert_add_info(pinfo, ti, &ei_netanalyzer_header_version_wrong);
177         return FALSE;
178       }
179
180       /* decode port */
181       port_num = (tvb_get_guint8(tvb, 1) >> SRT_PORT_NUM) & 0x3;
182       proto_tree_add_uint(netanalyzer_header_tree, hf_netanalyzer_port, tvb, 0, 4, port_num);
183       proto_item_append_text(ti, " (Port: %u, ", port_num);
184
185       /* decode length */
186       frame_length = tvb_get_letohs(tvb, 2) & MSK_LENGTH;
187       proto_tree_add_uint(netanalyzer_header_tree, hf_netanalyzer_length, tvb, 0, 4, frame_length);
188       proto_item_append_text(ti, "Length: %u byte%s, ", frame_length, (frame_length == 1) ? "" : "s");
189
190       /* decode status */
191       proto_item_append_text(ti, "Status: ");
192       packet_status = tvb_get_guint8(tvb, 0);
193       if (packet_status == 0)
194       {
195         ti_status = proto_tree_add_uint_format_value(netanalyzer_header_tree, hf_netanalyzer_status, tvb, 0, 1,
196                                                packet_status, "No Error");
197         proto_item_append_text(ti, "No Error)");
198       }
199       else
200       {
201         wmem_strbuf_t      *strbuf;
202         gboolean            first = TRUE;
203
204         ti_status = proto_tree_add_uint_format_value(netanalyzer_header_tree, hf_netanalyzer_status, tvb, 0, 1,
205                                                packet_status, "Error present (expand tree for details)");
206         strbuf = wmem_strbuf_new_label(wmem_epan_scope());
207         for (idx = 0; idx < 8; idx++)
208         {
209           if (packet_status & (1 << idx))
210           {
211             if (first)
212             {
213               first = FALSE;
214             }
215             else
216             {
217               wmem_strbuf_append(strbuf, ", ");
218             }
219             wmem_strbuf_append(strbuf, msk_strings[idx]);
220           }
221         }
222         proto_item_append_text(ti, "%s)", wmem_strbuf_get_str(strbuf));
223       }
224
225       netanalyzer_status_tree = proto_item_add_subtree(ti_status, ett_netanalyzer_status);
226       proto_tree_add_item(netanalyzer_status_tree, hf_netanalyzer_status_rx_err, tvb, 0, 1, ENC_LITTLE_ENDIAN);
227       proto_tree_add_item(netanalyzer_status_tree, hf_netanalyzer_status_align_err, tvb, 0, 1, ENC_LITTLE_ENDIAN);
228       proto_tree_add_item(netanalyzer_status_tree, hf_netanalyzer_status_fcs, tvb, 0, 1, ENC_LITTLE_ENDIAN);
229       proto_tree_add_item(netanalyzer_status_tree, hf_netanalyzer_status_too_long, tvb, 0, 1, ENC_LITTLE_ENDIAN);
230       proto_tree_add_item(netanalyzer_status_tree, hf_netanalyzer_status_sfd_error, tvb, 0, 1, ENC_LITTLE_ENDIAN);
231       proto_tree_add_item(netanalyzer_status_tree, hf_netanalyzer_status_short_frame, tvb, 0, 1, ENC_LITTLE_ENDIAN);
232       proto_tree_add_item(netanalyzer_status_tree, hf_netanalyzer_status_short_preamble, tvb, 0, 1, ENC_LITTLE_ENDIAN);
233       proto_tree_add_item(netanalyzer_status_tree, hf_netanalyzer_status_long_preamble, tvb, 0, 1, ENC_LITTLE_ENDIAN);
234
235       /* decode transparent mode */
236       if (tvb_get_guint8(tvb, 1) & MSK_TRANSPARENT_MODE)
237       {
238         proto_tree_add_expert(netanalyzer_header_tree, pinfo, &ei_netanalyzer_transparent_frame, tvb, 0, 4);
239         proto_item_append_text(ti, ", Transparent Mode");
240
241         if (packet_status & MSK_ALIGN_ERR)
242         {
243           proto_tree_add_expert(netanalyzer_header_tree, pinfo, &ei_netanalyzer_alignment_error, tvb, tvb_captured_length(tvb)-1, 1);
244         }
245       }
246     }
247     else
248     {
249       guchar *szTemp;
250
251       /* GPIO pseudo packet */
252       /* check consistency */
253       if ( (tvb_get_guint8(tvb, 10) == 0x00) &&
254            (tvb_get_guint8(tvb, 11) == 0x02) &&
255            (tvb_get_guint8(tvb, 12) == 0xa2) &&
256            (tvb_get_guint8(tvb, 13) == 0xff) &&
257            (tvb_get_guint8(tvb, 14) == 0xff) &&
258            (tvb_get_guint8(tvb, 15) == 0xff) &&
259            (tvb_get_guint8(tvb, 16) == 0x88) &&
260            (tvb_get_guint8(tvb, 17) == 0xff) &&
261            (tvb_get_guint8(tvb, INFO_TYPE_OFFSET) == 0x00) )
262       {
263 #define MAX_BUFFER 255
264         szTemp=(guchar *)wmem_alloc(wmem_epan_scope(), MAX_BUFFER);
265
266         /* everything ok */
267         col_set_str(pinfo->cinfo, COL_PROTOCOL, "netANALYZER");
268
269         offset = INFO_TYPE_OFFSET;
270
271         /* GPIO number */
272         offset++;
273         proto_tree_add_item (netanalyzer_header_tree, hf_netanalyzer_gpio_number, tvb, offset, 1, ENC_LITTLE_ENDIAN);
274         gpio_num = (tvb_get_guint8(tvb, offset) & 0x03);
275
276         /* GPIO edge */
277         offset++;
278         ti = proto_tree_add_item (netanalyzer_header_tree, hf_netanalyzer_gpio_edge, tvb, offset, 1, ENC_LITTLE_ENDIAN);
279         gpio_edge = (tvb_get_guint8(tvb, offset) & 0x01);
280
281         g_snprintf(szTemp, MAX_BUFFER,
282                    "GPIO event on GPIO %d (%sing edge)", gpio_num, (gpio_edge == 0x00) ? "ris" : "fall");
283
284         col_add_fstr(pinfo->cinfo, COL_INFO, "%s", szTemp);
285         proto_item_append_text(ti, " %s", szTemp);
286       }
287       else
288       {
289         /* something is wrong */
290         expert_add_info(pinfo, ti, &ei_netanalyzer_gpio_def_none);
291       }
292       return FALSE;
293     }
294   }
295   return TRUE;
296 }
297
298
299 /* Ethernet capture mode */
300 static void
301 dissect_netanalyzer(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
302 {
303   tvbuff_t                *next_tvb;
304
305   if (tvb_reported_length(tvb) >= 4)
306   {
307     /* generate tvb subset for Ethernet frame */
308     if (dissect_netanalyzer_common(tvb, pinfo, tree))
309     {
310       /* hand off to eth dissector with the new tvb subset */
311       next_tvb = tvb_new_subset_remaining(tvb, 4);
312       call_dissector(eth_dissector_handle, next_tvb, pinfo, tree);
313     }
314   }
315   else
316   {
317     /* something is wrong */
318     proto_tree_add_expert_format(tree, pinfo, &ei_netanalyzer_header_version_none, tvb, 4, -1,
319         "netANALYZER - No netANALYZER header found");
320   }
321 }
322
323
324 /* Transparent capture mode */
325 static void
326 dissect_netanalyzer_transparent(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
327 {
328   proto_tree              *transparent_payload_tree = NULL;
329   tvbuff_t                *next_tvb;
330
331   if (tvb_reported_length(tvb) >= 4)
332   {
333     /* generate tvb subset for Ethernet frame */
334     if (dissect_netanalyzer_common(tvb, pinfo, tree))
335     {
336       /* do not hand off transparent packet for further Ethernet dissectors
337        * as normally the transparent mode is used for low level analysis
338        * where dissecting the frame's content wouldn't make much sense
339        * use data dissector instead */
340       transparent_payload_tree = proto_tree_add_subtree(tree, tvb, 4, tvb_captured_length(tvb)-4,
341                                     ett_netanalyzer_transparent, NULL, "Raw packet data");
342       next_tvb = tvb_new_subset_remaining(tvb, 4);
343       call_dissector(data_dissector_handle, next_tvb, pinfo, transparent_payload_tree);
344
345       col_set_str(pinfo->cinfo, COL_PROTOCOL, "netANALYZER");
346       col_set_str(pinfo->cinfo, COL_INFO, "Frame captured in transparent mode");
347     }
348   }
349   else
350   {
351     /* something is wrong */
352     proto_tree_add_expert_format(tree, pinfo, &ei_netanalyzer_header_version_none, tvb, 4, -1,
353         "netANALYZER transparent mode - No netANALYZER header found");
354   }
355 }
356
357
358 void proto_register_netanalyzer(void)
359 {
360   static hf_register_info hf[] = {
361     { &hf_netanalyzer_gpio_number,
362       { "Event on", "netanalyzer.gpio_event.gpio_number",
363         FT_UINT8, BASE_HEX, VALS(gpio_number), 0x0,
364         "Event on GPIO number", HFILL }
365     },
366     { &hf_netanalyzer_gpio_edge,
367       { "Event type", "netanalyzer.gpio_event.gpio_edge",
368         FT_UINT8, BASE_HEX, VALS(gpio_edge_vals), 0x0,
369         "Edge of GPIO event", HFILL }
370     },
371     { &hf_netanalyzer_port,
372       { "Reception Port", "netanalyzer.port",
373         FT_UINT8, BASE_DEC, NULL, 0x0,
374         "netANALYZER reception port", HFILL }
375     },
376     { &hf_netanalyzer_length,
377       { "Ethernet frame length", "netanalyzer.framelen",
378         FT_UINT16, BASE_DEC, NULL, 0x0,
379         "Actual Ethernet frame length", HFILL }
380     },
381     { &hf_netanalyzer_status,
382       { "Status", "netanalyzer.packetstatus",
383         FT_UINT8, BASE_HEX, NULL, MSK_PACKET_STATUS,
384         "Status of Ethernet frame", HFILL }
385     },
386     { &hf_netanalyzer_status_rx_err,
387       { "MII RX_ER error", "netanalyzer.packetstatus.rx_er",
388         FT_BOOLEAN, 8, NULL, MSK_RX_ERR,
389         "RX_ER detected in frame", HFILL }
390     },
391     { &hf_netanalyzer_status_align_err,
392       { "Alignment error", "netanalyzer.packetstatus.alignment_error",
393         FT_BOOLEAN, 8, NULL, MSK_ALIGN_ERR,
394         NULL, HFILL }
395     },
396     { &hf_netanalyzer_status_fcs,
397       { "FCS error", "netanalyzer.packetstatus.fcs_error",
398         FT_BOOLEAN, 8, NULL, MSK_FCS_ERROR,
399         NULL, HFILL }
400     },
401     { &hf_netanalyzer_status_too_long,
402       { "Frame too long", "netanalyzer.packetstatus.too_long",
403         FT_BOOLEAN, 8, NULL, MSK_TOO_LONG,
404         "Frame too long (capture truncated)", HFILL }
405     },
406     { &hf_netanalyzer_status_sfd_error,
407       { "No valid SFD found", "netanalyzer.packetstatus.sfd_error",
408         FT_BOOLEAN, 8, NULL, MSK_SFD_ERROR,
409         "SDF error detected in frame", HFILL }
410     },
411     { &hf_netanalyzer_status_short_frame,
412       { "Frame smaller 64 bytes", "netanalyzer.packetstatus.short_frame",
413         FT_BOOLEAN, 8, NULL, MSK_SHORT_FRAME,
414         NULL, HFILL }
415     },
416     { &hf_netanalyzer_status_short_preamble,
417       { "Preamble shorter than 7 bytes", "netanalyzer.packetstatus.short_preamble",
418         FT_BOOLEAN, 8, NULL, MSK_SHORT_PREAMBLE,
419         NULL, HFILL }
420     },
421     { &hf_netanalyzer_status_long_preamble,
422       { "Preamble longer than 7 bytes", "netanalyzer.packetstatus.long_preamble",
423         FT_BOOLEAN, 8, NULL, MSK_LONG_PREAMBLE,
424         NULL, HFILL }
425     },
426   };
427
428   static gint *ett[] = {
429     &ett_netanalyzer,
430     &ett_netanalyzer_status,
431     &ett_netanalyzer_transparent,
432   };
433
434   static ei_register_info ei[] = {
435     { &ei_netanalyzer_header_version_wrong, { "netanalyzer.header_version.wrong", PI_PROTOCOL, PI_ERROR, "Wrong netANALYZER header version", EXPFILL }},
436     { &ei_netanalyzer_gpio_def_none, { "netanalyzer.gpio_def_none", PI_MALFORMED, PI_ERROR, "No valid netANALYZER GPIO definition found", EXPFILL }},
437     { &ei_netanalyzer_header_version_none, { "netanalyzer.header_version.none", PI_MALFORMED, PI_ERROR, "No netANALYZER header found", EXPFILL }},
438     { &ei_netanalyzer_transparent_frame, { "netanalyzer.transparent_frame", PI_PROTOCOL, PI_NOTE, "This frame was captured in transparent mode", EXPFILL }},
439     { &ei_netanalyzer_alignment_error, { "netanalyzer.alignment_error", PI_PROTOCOL, PI_WARN, "Displayed frame data contains additional nibble due to alignment error (upper nibble is not valid)", EXPFILL }},
440   };
441
442   expert_module_t* expert_netanalyzer;
443
444   proto_netanalyzer = proto_register_protocol (
445     "netANALYZER",            /* name */
446     "netANALYZER",            /* short name */
447     "netanalyzer" );          /* abbrev */
448
449   proto_register_field_array(proto_netanalyzer, hf, array_length(hf));
450   proto_register_subtree_array(ett, array_length(ett));
451   expert_netanalyzer = expert_register_protocol(proto_netanalyzer);
452   expert_register_field_array(expert_netanalyzer, ei, array_length(ei));
453
454 }
455
456
457 void proto_reg_handoff_netanalyzer(void)
458 {
459   dissector_handle_t netana_handle;
460   dissector_handle_t netana_handle_transparent;
461
462   eth_dissector_handle  = find_dissector("eth_withfcs");
463   data_dissector_handle = find_dissector("data");
464
465   netana_handle             = create_dissector_handle(dissect_netanalyzer,             proto_netanalyzer);
466   netana_handle_transparent = create_dissector_handle(dissect_netanalyzer_transparent, proto_netanalyzer);
467   dissector_add_uint("wtap_encap", WTAP_ENCAP_NETANALYZER,             netana_handle);
468   dissector_add_uint("wtap_encap", WTAP_ENCAP_NETANALYZER_TRANSPARENT, netana_handle_transparent);
469 }
470
471 /*
472  * Editor modelines  -  http://www.wireshark.org/tools/modelines.html
473  *
474  * Local Variables:
475  * c-basic-offset: 2
476  * tab-width: 8
477  * indent-tabs-mode: nil
478  * End:
479  *
480  * ex: set shiftwidth=2 tabstop=8 expandtab:
481  * :indentSize=2:tabSize=8:noTabs=true:
482  */