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