HACK: 2nd try to match RowsetProperties
[wireshark-wip.git] / epan / dissectors / packet-rtacser.c
blobf2d75a6d92d1de0f7a5479c6f7ec098fca589b25
1 /* packet-rtacser.c
2 * Routines for Schweitzer Engineering Laboratories "Real-Time Automation Controller" (RTAC) Serial Line Dissection
3 * By Chris Bontje (cbontje[AT]gmail.com)
4 * Copyright May 2013
6 * $Id$
8 ************************************************************************************************
9 * Wireshark - Network traffic analyzer
10 * By Gerald Combs <gerald@wireshark.org>
11 * Copyright 1998 Gerald Combs
13 * This program is free software; you can redistribute it and/or
14 * modify it under the terms of the GNU General Public License
15 * as published by the Free Software Foundation; either version 2
16 * of the License, or (at your option) any later version.
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
23 * You should have received a copy of the GNU General Public License
24 * along with this program; if not, write to the Free Software
25 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
27 ************************************************************************************************
28 * Dissector Notes:
30 * The RTAC product family (SEL-3530, SEL-2241, SEL-3505) is a Linux-based Automation Controller
31 * product that is capable of interfacing with SEL and 3rd-party equipment using a variety of
32 * standard industrial protocols such as SEL FM, DNP3, Modbus, C37.118, Telegyr 8979 and others.
33 * Each protocol instance (master/client or slave/server) is configured to utilize either Ethernet
34 * or EIA-232/485 serial connectivity with protocol variations for each medium taken into account.
36 * The configuration software for the RTAC platform is named AcSELerator RTAC (SEL-5033) and
37 * is used to set up all communications and user logic for the controller as well as provide
38 * downloading and online debugging facilities. One particularly useful aspect of the online
39 * debugging capabilities is a robust Communication Monitor tool that can show raw data streams
40 * from either serial or Ethernet interfaces. Many similar products have this same capability
41 * but the RTAC software goes a step beyond by providing a "save-as" function to save all captured
42 * data into pcap format for further analysis in Wireshark.
44 * All Ethernet-style capture files will have a packets with a "Linux Cooked Capture" header
45 * including the "source" MAC address of the device responsible for the generation of the message
46 * and the TCP/IP header(s) maintained from the original conversation. The application data from the
47 * message will follow as per a standard Wireshark packet.
49 * Serial-based pcap capture files were orignally stored using "User 0" DLT type 147 to specify a
50 * user-defined dissector for pcap data but this format was later modified to specify a custom DLT type
51 * known as LINKTYPE_RTAC_SERIAL (DLT 250). The pcap file data portion contains a standard 12-byte serial
52 * header followed by the application payload data from actual rx/tx activity on the line. Some useful
53 * information can be retrieved from the 12-byte header information, such as conversation time-stamps,
54 * UART function and EIA-232 serial control line states at the time of the message.
56 * This dissector will automatically be used for any newer-style DLT 250 files, and the payload protocol
57 * can be configured via built-in preferences to use whatever standardized industrial protocol is present
58 * on the line for attempted dissection (selfm, mbrtu, dnp3.udp, synphasor). Older pcap files of DLT type 147
59 * can be used by setting the DLT_USER preferences configuration of User 0 (DLT=147) with a 'Header Size'
60 * of '12' and a 'Header Protocol' of 'rtacser'. The payload protocol should be set to use the protocol
61 * dissector for the data that is present on the line (again, selfm, mbrtu, dnp3.udp or synphasor). */
63 #include "config.h"
65 #include <epan/packet.h>
66 #include <epan/prefs.h>
68 /* Initialize the protocol and registered fields */
69 static int proto_rtacser = -1;
70 static int hf_rtacser_timestamp = -1;
71 static int hf_rtacser_event_type = -1;
72 static int hf_rtacser_ctrl_cts = -1;
73 static int hf_rtacser_ctrl_dcd = -1;
74 static int hf_rtacser_ctrl_dsr = -1;
75 static int hf_rtacser_ctrl_rts = -1;
76 static int hf_rtacser_ctrl_dtr = -1;
77 static int hf_rtacser_ctrl_ring = -1;
78 static int hf_rtacser_ctrl_mbok = -1;
79 static int hf_rtacser_footer = -1;
80 static int hf_rtacser_data = -1;
82 /* Initialize the subtree pointers */
83 static gint ett_rtacser = -1;
84 static gint ett_rtacser_cl = -1;
86 /* Globals for RTAC Serial Preferences */
87 static guint global_rtacser_payload_proto = 0; /* No Payload, by default */
89 /* Handles for Payload Protocols */
90 static dissector_handle_t selfm_handle;
91 static dissector_handle_t dnp3_handle;
92 static dissector_handle_t modbus_handle;
93 static dissector_handle_t synphasor_handle;
95 #define RTACSER_HEADER_LEN 12
97 /* Bit-masks for EIA-232 serial control lines */
98 #define RTACSER_CTRL_CTS 0x01
99 #define RTACSER_CTRL_DCD 0x02
100 #define RTACSER_CTRL_DSR 0x04
101 #define RTACSER_CTRL_RTS 0x08
102 #define RTACSER_CTRL_DTR 0x10
103 #define RTACSER_CTRL_RING 0x20
104 #define RTACSER_CTRL_MBOK 0x40
106 /* Payload Protocol Types from Preferences */
107 #define RTACSER_PAYLOAD_NONE 0
108 #define RTACSER_PAYLOAD_SELFM 1
109 #define RTACSER_PAYLOAD_DNP3 2
110 #define RTACSER_PAYLOAD_MODBUS 3
111 #define RTACSER_PAYLOAD_SYNPHASOR 4
113 /* Event Types */
114 static const value_string rtacser_eventtype_vals[] = {
115 { 0x00, "STATUS_CHANGE" },
116 { 0x01, "DATA_TX_START" },
117 { 0x02, "DATA_RX_START" },
118 { 0x03, "DATA_TX_END" },
119 { 0x04, "DATA_RX_END" },
120 { 0x05, "CAPTURE_DATA_LOST" },
121 { 0x06, "CAPTURE_COMPLETE" },
122 { 0x07, "FRAMING_ERROR" },
123 { 0x08, "PARITY_ERROR" },
124 { 0x09, "SERIAL_BREAK_EVENT" },
125 { 0x0A, "SERIAL_OVERFLOW_EVENT" },
126 { 0, NULL }
129 static const enum_val_t rtacser_payload_proto_type[] = {
130 { "NONE ", "NONE ", RTACSER_PAYLOAD_NONE },
131 { "SEL FM ", "SEL FM ", RTACSER_PAYLOAD_SELFM },
132 { "DNP3 ", "DNP3 ", RTACSER_PAYLOAD_DNP3 },
133 { "MODBUS RTU", "MODBUS RTU", RTACSER_PAYLOAD_MODBUS },
134 { "SYNPHASOR ", "SYNPHASOR ", RTACSER_PAYLOAD_SYNPHASOR },
135 { NULL, NULL, 0 }
139 /******************************************************************************************************/
140 /* Code to dissect RTAC Serial-Line Protocol packets */
141 /******************************************************************************************************/
142 static void
143 dissect_rtacser_data(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
145 /* Set up structures needed to add the protocol subtree and manage it */
146 proto_item *rtacser_item, *ts_item, *cl_item, *data_payload;
147 proto_tree *rtacser_tree, *cl_tree;
148 int offset=0, len=0;
149 guint event_type;
150 guint32 timestamp1, timestamp2;
151 gboolean cts, dcd, dsr, rts, dtr, ring, mbok;
152 tvbuff_t *payload_tvb;
154 len = RTACSER_HEADER_LEN;
156 /* Make entries in Protocol column on summary display */
157 col_set_str(pinfo->cinfo, COL_PROTOCOL, "RTAC Serial");
158 col_clear(pinfo->cinfo, COL_INFO);
160 if (tree) {
162 rtacser_item = proto_tree_add_protocol_format(tree, proto_rtacser, tvb, 0, len, "RTAC Serial Line");
163 rtacser_tree = proto_item_add_subtree(rtacser_item, ett_rtacser);
165 /* Time-stamp is stored as 2 x 32-bit unsigned integers, the left and right-hand side of the decimal point respectively */
166 /* The format mirrors the timeval struct - absolute Epoch time (seconds since 1/1/1970) with an added microsecond component */
167 timestamp1 = tvb_get_ntohl(tvb, offset);
168 timestamp2 = tvb_get_ntohl(tvb, offset+4);
169 ts_item = proto_tree_add_item(rtacser_tree, hf_rtacser_timestamp, tvb, offset, 8, ENC_BIG_ENDIAN);
170 proto_item_set_text(ts_item, "Arrived At Time: %u.%u" , timestamp1, timestamp2);
171 offset += 8;
173 /* Set INFO column with RTAC Serial Event Type */
174 event_type = tvb_get_guint8(tvb, offset);
175 col_add_fstr(pinfo->cinfo, COL_INFO, "%-21s", val_to_str_const(event_type, rtacser_eventtype_vals, "Unknown Type"));
177 /* Add event type to tree */
178 proto_tree_add_item(rtacser_tree, hf_rtacser_event_type, tvb, offset, 1, ENC_BIG_ENDIAN);
179 offset += 1;
181 /* Retrieve EIA-232 serial control line states */
182 cts = tvb_get_guint8(tvb, offset) & RTACSER_CTRL_CTS;
183 dcd = tvb_get_guint8(tvb, offset) & RTACSER_CTRL_DCD;
184 dsr = tvb_get_guint8(tvb, offset) & RTACSER_CTRL_DSR;
185 rts = tvb_get_guint8(tvb, offset) & RTACSER_CTRL_RTS;
186 dtr = tvb_get_guint8(tvb, offset) & RTACSER_CTRL_DTR;
187 ring = tvb_get_guint8(tvb, offset) & RTACSER_CTRL_RING;
188 mbok = tvb_get_guint8(tvb, offset) & RTACSER_CTRL_MBOK;
190 cl_item = proto_tree_add_text(rtacser_tree, tvb, offset, 1, "Control Lines");
191 cl_tree = proto_item_add_subtree(cl_item, ett_rtacser_cl);
193 /* Add UART Control Line information to INFO column */
194 col_append_str(pinfo->cinfo, COL_INFO, " ( ");
195 (cts) ? col_append_str(pinfo->cinfo, COL_INFO, "CTS") : col_append_str(pinfo->cinfo, COL_INFO, "/CTS");
196 (dcd) ? col_append_sep_str(pinfo->cinfo, COL_INFO, NULL, "DCD") : col_append_sep_str(pinfo->cinfo, COL_INFO, NULL, "/DCD");
197 (dsr) ? col_append_sep_str(pinfo->cinfo, COL_INFO, NULL, "DSR") : col_append_sep_str(pinfo->cinfo, COL_INFO, NULL, "/DSR");
198 (rts) ? col_append_sep_str(pinfo->cinfo, COL_INFO, NULL, "RTS") : col_append_sep_str(pinfo->cinfo, COL_INFO, NULL, "/RTS");
199 (dtr) ? col_append_sep_str(pinfo->cinfo, COL_INFO, NULL, "DTR") : col_append_sep_str(pinfo->cinfo, COL_INFO, NULL, "/DTR");
200 (ring) ? col_append_sep_str(pinfo->cinfo, COL_INFO, NULL, "RING") : col_append_sep_str(pinfo->cinfo, COL_INFO, NULL, "/RING");
201 (mbok) ? col_append_sep_str(pinfo->cinfo, COL_INFO, NULL, "MBOK") : col_append_sep_str(pinfo->cinfo, COL_INFO, NULL, "/MBOK");
202 col_append_str(pinfo->cinfo, COL_INFO, " )");
204 /* Add UART Control Line information to tree */
205 proto_item_append_text(cl_item, " (");
206 (cts) ? proto_item_append_text(cl_item, "CTS, ") : proto_item_append_text(cl_item, "/CTS, ");
207 (dcd) ? proto_item_append_text(cl_item, "DCD, ") : proto_item_append_text(cl_item, "/DCD, ");
208 (dsr) ? proto_item_append_text(cl_item, "DSR, ") : proto_item_append_text(cl_item, "/DSR, ");
209 (rts) ? proto_item_append_text(cl_item, "RTS, ") : proto_item_append_text(cl_item, "/RTS, ");
210 (dtr) ? proto_item_append_text(cl_item, "DTR, ") : proto_item_append_text(cl_item, "/DTR, ");
211 (ring) ? proto_item_append_text(cl_item, "RING, ") : proto_item_append_text(cl_item, "/RING, ");
212 (mbok) ? proto_item_append_text(cl_item, "MBOK") : proto_item_append_text(cl_item, "/MBOK");
213 proto_item_append_text(cl_item, ")");
215 proto_tree_add_item(cl_tree, hf_rtacser_ctrl_cts, tvb, offset, 1, ENC_BIG_ENDIAN);
216 proto_tree_add_item(cl_tree, hf_rtacser_ctrl_dcd, tvb, offset, 1, ENC_BIG_ENDIAN);
217 proto_tree_add_item(cl_tree, hf_rtacser_ctrl_dsr, tvb, offset, 1, ENC_BIG_ENDIAN);
218 proto_tree_add_item(cl_tree, hf_rtacser_ctrl_rts, tvb, offset, 1, ENC_BIG_ENDIAN);
219 proto_tree_add_item(cl_tree, hf_rtacser_ctrl_dtr, tvb, offset, 1, ENC_BIG_ENDIAN);
220 proto_tree_add_item(cl_tree, hf_rtacser_ctrl_ring, tvb, offset, 1, ENC_BIG_ENDIAN);
221 proto_tree_add_item(cl_tree, hf_rtacser_ctrl_mbok, tvb, offset, 1, ENC_BIG_ENDIAN);
222 offset += 1;
224 /* 2-byte footer */
225 proto_tree_add_item(rtacser_tree, hf_rtacser_footer, tvb, offset, 2, ENC_BIG_ENDIAN);
226 offset += 2;
228 /* If no payload dissector has been selected, indicate to the user the preferences options */
229 if ((tvb_reported_length_remaining(tvb, offset) > 0) && (global_rtacser_payload_proto == RTACSER_PAYLOAD_NONE)) {
230 data_payload = proto_tree_add_item(tree, hf_rtacser_data, tvb, offset, -1, ENC_NA);
231 proto_item_set_text(data_payload,"Payload Protocol not selected. Check 'Preferences-> Protocols-> RTAC Serial' for options");
232 return;
235 } /* tree */
237 /* Determine correct message type and call appropriate dissector */
238 if (tvb_reported_length_remaining(tvb, RTACSER_HEADER_LEN) > 0) {
240 switch (global_rtacser_payload_proto) {
241 case RTACSER_PAYLOAD_SELFM:
242 payload_tvb = tvb_new_subset_remaining(tvb, RTACSER_HEADER_LEN);
243 call_dissector(selfm_handle, payload_tvb, pinfo, tree);
244 break;
245 case RTACSER_PAYLOAD_DNP3:
246 payload_tvb = tvb_new_subset_remaining(tvb, RTACSER_HEADER_LEN);
247 call_dissector(dnp3_handle, payload_tvb, pinfo, tree);
248 break;
249 case RTACSER_PAYLOAD_MODBUS:
250 payload_tvb = tvb_new_subset_remaining(tvb, RTACSER_HEADER_LEN);
251 call_dissector(modbus_handle, payload_tvb, pinfo, tree);
252 break;
253 case RTACSER_PAYLOAD_SYNPHASOR:
254 payload_tvb = tvb_new_subset_remaining(tvb, RTACSER_HEADER_LEN);
255 call_dissector(synphasor_handle, payload_tvb, pinfo, tree);
256 break;
257 default:
258 break;
266 /******************************************************************************************************/
267 /* Dissect RTAC Serial-line protocol payload */
268 /******************************************************************************************************/
269 static int
270 dissect_rtacser(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, void *data _U_)
272 gint length = tvb_length(tvb);
274 /* Check for a RTAC Serial packet. It should be at least 12 bytes */
275 if(length < 12) {
277 return 0;
280 dissect_rtacser_data(tvb, pinfo, tree);
282 return tvb_length(tvb);
285 /******************************************************************************************************/
286 /* Register the protocol with Wireshark */
287 /******************************************************************************************************/
288 void proto_reg_handoff_rtacser(void);
290 void
291 proto_register_rtacser(void)
293 /* RTAC Serial Protocol header fields */
294 static hf_register_info rtacser_hf[] = {
295 { &hf_rtacser_timestamp,
296 { "Timestamp", "rtacser.timestamp", FT_UINT64, BASE_DEC, NULL, 0x0, NULL, HFILL }},
297 { &hf_rtacser_event_type,
298 { "Event Type", "rtacser.eventtype", FT_UINT8, BASE_HEX, rtacser_eventtype_vals, 0x0, NULL, HFILL }},
299 { &hf_rtacser_ctrl_cts,
300 { "CTS", "rtacser.cts", FT_UINT8, BASE_DEC, NULL, RTACSER_CTRL_CTS, NULL, HFILL }},
301 { &hf_rtacser_ctrl_dcd,
302 { "DCD", "rtacser.dcd", FT_UINT8, BASE_DEC, NULL, RTACSER_CTRL_DCD, NULL, HFILL }},
303 { &hf_rtacser_ctrl_dsr,
304 { "DSR", "rtacser.dsr", FT_UINT8, BASE_DEC, NULL, RTACSER_CTRL_DSR, NULL, HFILL }},
305 { &hf_rtacser_ctrl_rts,
306 { "RTS", "rtacser.rts", FT_UINT8, BASE_DEC, NULL, RTACSER_CTRL_RTS, NULL, HFILL }},
307 { &hf_rtacser_ctrl_dtr,
308 { "DTR", "rtacser.dtr", FT_UINT8, BASE_DEC, NULL, RTACSER_CTRL_DTR, NULL, HFILL }},
309 { &hf_rtacser_ctrl_ring,
310 { "RING", "rtacser.ring", FT_UINT8, BASE_DEC, NULL, RTACSER_CTRL_RING, NULL, HFILL }},
311 { &hf_rtacser_ctrl_mbok,
312 { "MBOK", "rtacser.mbok", FT_UINT8, BASE_DEC, NULL, RTACSER_CTRL_MBOK, NULL, HFILL }},
313 { &hf_rtacser_footer,
314 { "Footer", "rtacser.footer", FT_UINT16, BASE_HEX, NULL, 0x0, NULL, HFILL }},
315 { &hf_rtacser_data,
316 { "Payload data", "rtacser.data", FT_BYTES, BASE_NONE, NULL, 0x0, NULL, HFILL }},
320 /* Setup protocol subtree array */
321 static gint *ett[] = {
322 &ett_rtacser,
323 &ett_rtacser_cl,
326 module_t *rtacser_module;
328 /* Register the protocol name and description */
329 proto_rtacser = proto_register_protocol("RTAC Serial", "RTAC Serial", "rtacser");
331 /* Registering protocol to be called by another dissector */
332 new_register_dissector("rtacser", dissect_rtacser, proto_rtacser);
334 /* Required function calls to register the header fields and subtrees used */
335 proto_register_field_array(proto_rtacser, rtacser_hf, array_length(rtacser_hf));
336 proto_register_subtree_array(ett, array_length(ett));
338 /* Register required preferences for RTAC Serial Payload Protocol */
339 rtacser_module = prefs_register_protocol(proto_rtacser, proto_reg_handoff_rtacser);
341 /* RTAC Serial Preference - Payload Protocol in use */
342 prefs_register_enum_preference(rtacser_module, "rtacserial_payload_proto",
343 "Payload Protocol Type",
344 "Payload Protocol Type",
345 &global_rtacser_payload_proto,
346 rtacser_payload_proto_type,
347 TRUE);
352 /******************************************************************************************************/
353 /* If this dissector uses sub-dissector registration add a registration routine.
354 This format is required because a script is used to find these routines and
355 create the code that calls these routines.
357 /******************************************************************************************************/
358 void
359 proto_reg_handoff_rtacser(void)
361 static int rtacser_prefs_initialized = FALSE;
362 static dissector_handle_t rtacser_handle;
364 /* Make sure to use RTAC Serial Protocol Preferences field to determine payload protocol to use for decoding */
365 if (! rtacser_prefs_initialized) {
366 rtacser_handle = new_create_dissector_handle(dissect_rtacser, proto_rtacser);
367 rtacser_prefs_initialized = TRUE;
370 /* Create a handle for each expected payload protocol that can be called via the Preferences */
371 selfm_handle = find_dissector("selfm");
372 dnp3_handle = find_dissector("dnp3.udp");
373 modbus_handle = find_dissector("mbrtu");
374 synphasor_handle = find_dissector("synphasor");
376 dissector_add_uint("wtap_encap", WTAP_ENCAP_RTAC_SERIAL, rtacser_handle);