1 /* packet-netanalyzer.c
2 * Dissector for Hilscher netANALYZER frames.
3 * Copyright 2008-2011, Hilscher GmbH, Holger Pfrommer hpfrommer[AT]hilscher.com
6 * +---------------------------+
9 * +---------------------------+
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.
37 * Wireshark - Network traffic analyzer
38 * By Gerald Combs <gerald[AT]wireshark.org>
39 * Copyright 1999 Gerald Combs
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.
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.
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.
59 #include <epan/packet.h>
60 #include <epan/expert.h>
61 #include <wiretap/wtap.h>
63 void proto_register_netanalyzer(void);
64 void proto_reg_handoff_netanalyzer(void);
67 #define INFO_TYPE_OFFSET 18
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
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 */
89 #define SRT_PORT_NUM 6
91 #define SRT_GPIO_FLAG 0
92 #define MSK_PACKET_STATUS 0xff
93 #define MSK_LENGTH 0x0fff
94 #define MSK_TRANSPARENT_MODE 0x02
97 static const value_string gpio_number[] = {
105 static const value_string gpio_edge_vals[] = {
106 { 0x0, "rising edge" },
107 { 0x1, "falling edge" },
112 static dissector_handle_t eth_dissector_handle;
113 static dissector_handle_t data_dissector_handle;
115 static gint proto_netanalyzer = -1;
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;
131 static gint ett_netanalyzer = -1;
132 static gint ett_netanalyzer_status = -1;
133 static gint ett_netanalyzer_transparent = -1;
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;
141 /* common routine for Ethernet and transparent mode */
143 dissect_netanalyzer_common(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
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;
151 guint32 frame_length;
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);
165 is_gpio = (tvb_get_guint8(tvb, 1) >> SRT_GPIO_FLAG) & 0x1;
169 /* normal packet, no GPIO */
172 version = (tvb_get_guint8(tvb, 1) >> SRT_VERSION) & 0xf;
175 /* something is wrong */
176 expert_add_info(pinfo, ti, &ei_netanalyzer_header_version_wrong);
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);
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");
191 proto_item_append_text(ti, "Status: ");
192 packet_status = tvb_get_guint8(tvb, 0);
193 if (packet_status == 0)
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)");
201 wmem_strbuf_t *strbuf;
202 gboolean first = TRUE;
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++)
209 if (packet_status & (1 << idx))
217 wmem_strbuf_append(strbuf, ", ");
219 wmem_strbuf_append(strbuf, msk_strings[idx]);
222 proto_item_append_text(ti, "%s)", wmem_strbuf_get_str(strbuf));
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);
235 /* decode transparent mode */
236 if (tvb_get_guint8(tvb, 1) & MSK_TRANSPARENT_MODE)
238 proto_tree_add_expert(netanalyzer_header_tree, pinfo, &ei_netanalyzer_transparent_frame, tvb, 0, 4);
239 proto_item_append_text(ti, ", Transparent Mode");
241 if (packet_status & MSK_ALIGN_ERR)
243 proto_tree_add_expert(netanalyzer_header_tree, pinfo, &ei_netanalyzer_alignment_error, tvb, tvb_captured_length(tvb)-1, 1);
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) )
263 #define MAX_BUFFER 255
264 szTemp=(guchar *)wmem_alloc(wmem_epan_scope(), MAX_BUFFER);
267 col_set_str(pinfo->cinfo, COL_PROTOCOL, "netANALYZER");
269 offset = INFO_TYPE_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);
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);
281 g_snprintf(szTemp, MAX_BUFFER,
282 "GPIO event on GPIO %d (%sing edge)", gpio_num, (gpio_edge == 0x00) ? "ris" : "fall");
284 col_add_fstr(pinfo->cinfo, COL_INFO, "%s", szTemp);
285 proto_item_append_text(ti, " %s", szTemp);
289 /* something is wrong */
290 expert_add_info(pinfo, ti, &ei_netanalyzer_gpio_def_none);
299 /* Ethernet capture mode */
301 dissect_netanalyzer(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
305 if (tvb_reported_length(tvb) >= 4)
307 /* generate tvb subset for Ethernet frame */
308 if (dissect_netanalyzer_common(tvb, pinfo, tree))
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);
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");
324 /* Transparent capture mode */
326 dissect_netanalyzer_transparent(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
328 proto_tree *transparent_payload_tree = NULL;
331 if (tvb_reported_length(tvb) >= 4)
333 /* generate tvb subset for Ethernet frame */
334 if (dissect_netanalyzer_common(tvb, pinfo, tree))
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);
345 col_set_str(pinfo->cinfo, COL_PROTOCOL, "netANALYZER");
346 col_set_str(pinfo->cinfo, COL_INFO, "Frame captured in transparent mode");
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");
358 void proto_register_netanalyzer(void)
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 }
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 }
371 { &hf_netanalyzer_port,
372 { "Reception Port", "netanalyzer.port",
373 FT_UINT8, BASE_DEC, NULL, 0x0,
374 "netANALYZER reception port", HFILL }
376 { &hf_netanalyzer_length,
377 { "Ethernet frame length", "netanalyzer.framelen",
378 FT_UINT16, BASE_DEC, NULL, 0x0,
379 "Actual Ethernet frame length", HFILL }
381 { &hf_netanalyzer_status,
382 { "Status", "netanalyzer.packetstatus",
383 FT_UINT8, BASE_HEX, NULL, MSK_PACKET_STATUS,
384 "Status of Ethernet frame", HFILL }
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 }
391 { &hf_netanalyzer_status_align_err,
392 { "Alignment error", "netanalyzer.packetstatus.alignment_error",
393 FT_BOOLEAN, 8, NULL, MSK_ALIGN_ERR,
396 { &hf_netanalyzer_status_fcs,
397 { "FCS error", "netanalyzer.packetstatus.fcs_error",
398 FT_BOOLEAN, 8, NULL, MSK_FCS_ERROR,
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 }
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 }
411 { &hf_netanalyzer_status_short_frame,
412 { "Frame smaller 64 bytes", "netanalyzer.packetstatus.short_frame",
413 FT_BOOLEAN, 8, NULL, MSK_SHORT_FRAME,
416 { &hf_netanalyzer_status_short_preamble,
417 { "Preamble shorter than 7 bytes", "netanalyzer.packetstatus.short_preamble",
418 FT_BOOLEAN, 8, NULL, MSK_SHORT_PREAMBLE,
421 { &hf_netanalyzer_status_long_preamble,
422 { "Preamble longer than 7 bytes", "netanalyzer.packetstatus.long_preamble",
423 FT_BOOLEAN, 8, NULL, MSK_LONG_PREAMBLE,
428 static gint *ett[] = {
430 &ett_netanalyzer_status,
431 &ett_netanalyzer_transparent,
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 }},
442 expert_module_t* expert_netanalyzer;
444 proto_netanalyzer = proto_register_protocol (
445 "netANALYZER", /* name */
446 "netANALYZER", /* short name */
447 "netanalyzer" ); /* abbrev */
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));
457 void proto_reg_handoff_netanalyzer(void)
459 dissector_handle_t netana_handle;
460 dissector_handle_t netana_handle_transparent;
462 eth_dissector_handle = find_dissector("eth_withfcs");
463 data_dissector_handle = find_dissector("data");
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);
472 * Editor modelines - http://www.wireshark.org/tools/modelines.html
477 * indent-tabs-mode: nil
480 * ex: set shiftwidth=2 tabstop=8 expandtab:
481 * :indentSize=2:tabSize=8:noTabs=true: