2 * Routines for Modbus/TCP and Modbus/UDP dissection
3 * By Riaan Swart <rswart@cs.sun.ac.za>
4 * Copyright 2001, Institute for Applied Computer Science
5 * University of Stellenbosch
7 * See http://www.modbus.org/ for information on Modbus/TCP.
9 * Updated to v1.1b of the Modbus Application Protocol specification
10 * Michael Mann * Copyright 2011
12 * Updates Oct/Nov 2012 (Chris Bontje, cbontje[*at*]gmail.com)
13 * - Some re-factoring to include support for serial Modbus RTU encapsulated in TCP messages
14 * - Minor text/syntax clean-up
15 * - Include decoding of holding/input response register data
16 * - Optionally decode holding/input registers as UINT16, UINT32, 32-bit Float IEEE/Modicon
17 * - Add various register address formatting options as "Raw", "Modicon 5-digit", "Modicon 6-digit"
19 * Updates Aug 2013 (Chris Bontje)
20 * - Improved dissection support for serial Modbus RTU with detection of query or response messages
22 *****************************************************************************************************
23 * A brief explanation of the distinction between Modbus/TCP and Modbus RTU over TCP:
25 * Consider a Modbus poll message: Unit 01, Scan Holding Register Address 0 for 30 Registers
27 * The Modbus/TCP message structure will follow the pattern below:
28 * 00 00 00 00 00 06 01 03 00 00 00 1E
29 * AA AA BB BB CC CC DD EE FF FF GG GG
31 * A = 16-bit Transaction Identifier (typically increments, or is locked at zero)
32 * B = 16-bit Protocol Identifier (typically zero)
33 * C = 16-bit Length of data payload following (and inclusive of) the length byte
34 * D = 8-bit Unit / Slave ID
35 * E = 8-bit Modbus Function Code
36 * F = 16-bit Reference Number / Register Base Address
37 * G = 16-bit Word Count / Number of Registers to scan
39 * A identical Modbus RTU (or Modbus RTU over TCP) message will overlay partially with the msg above
40 * and contain 16-bit CRC at the end:
41 * 00 00 00 00 00 06 01 03 00 00 00 1E -- -- (Modbus/TCP message, repeated from above)
42 * -- -- -- -- -- -- 01 03 00 00 00 1E C5 C2 (Modbus RTU over TCP message, includes 16-bit CRC footer)
43 * AA AA BB BB CC CC DD EE FF FF GG GG HH HH
45 * A = Not present in Modbus RTU message
46 * B = Not present in Modbus RTU message
47 * C = Not present in Modbus RTU message
48 * D = 8-bit Unit / Slave ID
49 * E = 8-bit Modbus Function Code
50 * F = 16-bit Reference Number / Register Base Address
51 * G = 16-bit Word Count / Number of Registers to scan
54 *****************************************************************************************************
57 * Wireshark - Network traffic analyzer
58 * By Gerald Combs <gerald@wireshark.org>
59 * Copyright 1998 Gerald Combs
61 * This program is free software; you can redistribute it and/or
62 * modify it under the terms of the GNU General Public License
63 * as published by the Free Software Foundation; either version 2
64 * of the License, or (at your option) any later version.
66 * This program is distributed in the hope that it will be useful,
67 * but WITHOUT ANY WARRANTY; without even the implied warranty of
68 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
69 * GNU General Public License for more details.
71 * You should have received a copy of the GNU General Public License
72 * along with this program; if not, write to the Free Software
73 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
78 #include <epan/packet.h>
79 #include "packet-tcp.h"
80 #include "packet-mbtcp.h"
81 #include <epan/prefs.h>
82 #include <epan/expert.h>
83 #include <epan/crc16-tvb.h> /* For CRC verification */\
85 /* Initialize the protocol and registered fields */
86 static int proto_mbtcp = -1;
87 static int proto_mbrtu = -1;
88 static int proto_modbus = -1;
89 static int hf_mbtcp_transid = -1;
90 static int hf_mbtcp_protid = -1;
91 static int hf_mbtcp_len = -1;
92 static int hf_mbtcp_unitid = -1;
93 static int hf_mbtcp_functioncode = -1;
94 static int hf_modbus_reference = -1;
95 static int hf_modbus_lreference = -1;
96 static int hf_modbus_reftype = -1;
97 static int hf_modbus_readref = -1;
98 static int hf_modbus_writeref = -1;
99 static int hf_modbus_wordcnt = -1;
100 static int hf_modbus_readwordcnt = -1;
101 static int hf_modbus_writewordcnt = -1;
102 static int hf_modbus_bytecnt = -1;
103 static int hf_modbus_lbytecnt = -1;
104 static int hf_modbus_bitcnt = -1;
105 static int hf_modbus_exceptioncode = -1;
106 static int hf_modbus_diag_sf = -1;
107 static int hf_modbus_diag_return_query_data_request = -1;
108 static int hf_modbus_diag_return_query_data_echo = -1;
109 static int hf_modbus_diag_restart_communication_option = -1;
110 static int hf_modbus_diag_return_diag_register = -1;
111 static int hf_modbus_diag_ascii_input_delimiter = -1;
112 static int hf_modbus_diag_return_bus_message_count = -1;
113 static int hf_modbus_diag_return_bus_comm_error_count = -1;
114 static int hf_modbus_diag_return_bus_exception_error_count = -1;
115 static int hf_modbus_diag_return_slave_message_count = -1;
116 static int hf_modbus_diag_return_no_slave_response_count = -1;
117 static int hf_modbus_diag_return_slave_nak_count = -1;
118 static int hf_modbus_diag_return_slave_busy_count = -1;
119 static int hf_modbus_diag_return_bus_char_overrun_count = -1;
120 static int hf_modbus_status = -1;
121 static int hf_modbus_event_count = -1;
122 static int hf_modbus_message_count = -1;
123 static int hf_modbus_event_recv_comm_err = -1;
124 static int hf_modbus_event_recv_char_over = -1;
125 static int hf_modbus_event_recv_lo_mode = -1;
126 static int hf_modbus_event_recv_broadcast = -1;
127 static int hf_modbus_event_send_read_ex = -1;
128 static int hf_modbus_event_send_slave_abort_ex = -1;
129 static int hf_modbus_event_send_slave_busy_ex = -1;
130 static int hf_modbus_event_send_slave_nak_ex = -1;
131 static int hf_modbus_event_send_write_timeout = -1;
132 static int hf_modbus_event_send_lo_mode = -1;
133 static int hf_modbus_andmask = -1;
134 static int hf_modbus_ormask = -1;
135 static int hf_modbus_data = -1;
136 static int hf_modbus_mei = -1;
137 static int hf_modbus_read_device_id = -1;
138 static int hf_modbus_object_id = -1;
139 static int hf_modbus_num_objects = -1;
140 static int hf_modbus_list_object_len = -1;
141 static int hf_modbus_conformity_level = -1;
142 static int hf_modbus_more_follows = -1;
143 static int hf_modbus_next_object_id = -1;
144 static int hf_modbus_object_str_value = -1;
145 static int hf_modbus_reg_uint16 = -1;
146 static int hf_modbus_reg_uint32 = -1;
147 static int hf_modbus_reg_ieee_float = -1;
148 static int hf_modbus_reg_modicon_float = -1;
149 static int hf_mbrtu_unitid = -1;
150 static int hf_mbrtu_crc16 = -1;
152 /* Initialize the subtree pointers */
153 static gint ett_mbtcp = -1;
154 static gint ett_mbrtu = -1;
155 static gint ett_modbus_hdr = -1;
156 static gint ett_group_hdr = -1;
157 static gint ett_events = -1;
158 static gint ett_events_recv = -1;
159 static gint ett_events_send = -1;
160 static gint ett_device_id_objects = -1;
161 static gint ett_device_id_object_items = -1;
163 static expert_field ei_mbrtu_crc16_incorrect = EI_INIT;
164 static expert_field ei_modbus_data_decode = EI_INIT;
166 static dissector_table_t modbus_data_dissector_table;
167 static dissector_table_t modbus_dissector_table;
168 static dissector_handle_t modbus_handle;
171 /* Globals for Modbus/TCP Preferences */
172 static gboolean mbtcp_desegment = TRUE;
173 static guint global_mbus_tcp_port = PORT_MBTCP; /* Port 502, by default */
174 static gint global_mbus_tcp_register_format = MBTCP_PREF_REGISTER_FORMAT_UINT16;
175 static gint global_mbus_tcp_register_addr_type = MBTCP_PREF_REGISTER_ADDR_RAW;
177 /* Globals for Modbus RTU over TCP Preferences */
178 static gboolean mbrtu_desegment = TRUE;
179 static guint global_mbus_rtu_port = PORT_MBRTU; /* 0, by default */
180 static gint global_mbus_rtu_register_format = MBTCP_PREF_REGISTER_FORMAT_UINT16;
181 static gint global_mbus_rtu_register_addr_type = MBTCP_PREF_REGISTER_ADDR_RAW;
182 static gboolean mbrtu_crc = FALSE;
185 classify_mbtcp_packet(packet_info *pinfo)
187 /* see if nature of packets can be derived from src/dst ports */
188 /* if so, return as found */
190 /* XXX Update Oct 2012 - It can be difficult to determine if a packet is a query or response; some way to track */
191 /* the Modbus/TCP transaction ID for each pair of messages would allow for detection based on a new seq. number. */
192 /* Otherwise, we can stick with this method; a configurable port option has been added to allow for usage of */
193 /* user ports either than the default of 502. */
194 if (( pinfo->srcport == global_mbus_tcp_port ) && ( pinfo->destport != global_mbus_tcp_port ))
195 return RESPONSE_PACKET;
196 if (( pinfo->srcport != global_mbus_tcp_port ) && ( pinfo->destport == global_mbus_tcp_port ))
199 /* else, cannot classify */
200 return CANNOT_CLASSIFY;
204 classify_mbrtu_packet(packet_info *pinfo, tvbuff_t *tvb)
206 /* see if nature of packets can be derived from src/dst ports */
207 /* if so, return as found */
208 if (( pinfo->srcport == global_mbus_rtu_port ) && ( pinfo->destport != global_mbus_rtu_port ))
209 return RESPONSE_PACKET;
210 if (( pinfo->srcport != global_mbus_rtu_port ) && ( pinfo->destport == global_mbus_rtu_port ))
213 /* Special case for serial-captured packets that don't have an Ethernet header */
214 /* Dig into these a little deeper to try to guess the message type */
215 if (!pinfo->srcport) {
216 /* If length is 8, this is either a query or very short response */
217 if (tvb_length(tvb) == 8) {
218 /* Only possible to get a response message of 8 bytes with Discrete or Coils */
219 if ((tvb_get_guint8(tvb, 1) == READ_COILS) || (tvb_get_guint8(tvb, 1) == READ_DISCRETE_INPUTS)) {
220 /* If this is, in fact, a response then the data byte count will be 3 */
221 /* This will correctly identify all messages except for those that are discrete or coil polls */
222 /* where the base address range happens to have 0x03 in the upper 16-bit address register */
223 if (tvb_get_guint8(tvb, 2) == 3) {
224 return RESPONSE_PACKET;
235 return RESPONSE_PACKET;
239 /* else, cannot classify */
240 return CANNOT_CLASSIFY;
243 /* Translate function to string, as given on p6 of
244 * "Open Modbus/TCP Specification", release 1 by Andy Swales.
246 static const value_string function_code_vals[] = {
247 { READ_COILS, "Read Coils" },
248 { READ_DISCRETE_INPUTS, "Read Discrete Inputs" },
249 { READ_HOLDING_REGS, "Read Holding Registers" },
250 { READ_INPUT_REGS, "Read Input Registers" },
251 { WRITE_SINGLE_COIL, "Write Single Coil" },
252 { WRITE_SINGLE_REG, "Write Single Register" },
253 { READ_EXCEPT_STAT, "Read Exception Status" },
254 { DIAGNOSTICS, "Diagnostics" },
255 { GET_COMM_EVENT_CTRS, "Get Comm. Event Counters" },
256 { GET_COMM_EVENT_LOG, "Get Comm. Event Log" },
257 { WRITE_MULT_COILS, "Write Multiple Coils" },
258 { WRITE_MULT_REGS, "Write Multiple Registers" },
259 { REPORT_SLAVE_ID, "Report Slave ID" },
260 { READ_FILE_RECORD, "Read File Record" },
261 { WRITE_FILE_RECORD, "Write File Record" },
262 { MASK_WRITE_REG, "Mask Write Register" },
263 { READ_WRITE_REG, "Read Write Register" },
264 { READ_FIFO_QUEUE, "Read FIFO Queue" },
265 { ENCAP_INTERFACE_TRANSP, "Encapsulated Interface Transport" },
269 /* Translate exception code to string */
270 static const value_string exception_code_vals[] = {
271 { ILLEGAL_FUNCTION, "Illegal function" },
272 { ILLEGAL_ADDRESS, "Illegal data address" },
273 { ILLEGAL_VALUE, "Illegal data value" },
274 { SLAVE_FAILURE, "Slave device failure" },
275 { ACKNOWLEDGE, "Acknowledge" },
276 { SLAVE_BUSY, "Slave device busy" },
277 { MEMORY_ERR, "Memory parity error" },
278 { GATEWAY_UNAVAILABLE, "Gateway path unavailable" },
279 { GATEWAY_TRGT_FAIL, "Gateway target device failed to respond" },
283 /* Translate Modbus Encapsulation Interface (MEI) code to string */
284 static const value_string encap_interface_code_vals[] = {
285 { CANOPEN_REQ_RESP, "CANopen Request/Response " },
286 { READ_DEVICE_ID, "Read Device Identification" },
290 /* Translate Modbus Diagnostic subfunction code to string */
291 static const value_string diagnostic_code_vals[] = {
292 { RETURN_QUERY_DATA, "Return Query Data" },
293 { RESTART_COMMUNICATION_OPTION, "Restart Communications Option" },
294 { RETURN_DIAGNOSTIC_REGISTER, "Return Diagnostic Register" },
295 { CHANGE_ASCII_INPUT_DELIMITER, "Change ASCII Input Delimiter" },
296 { FORCE_LISTEN_ONLY_MODE, "Force Listen Only Mode" },
297 { CLEAR_COUNTERS_AND_DIAG_REG, "Clear Counters and Diagnostic Register" },
298 { RETURN_BUS_MESSAGE_COUNT, "Return Bus Message Count" },
299 { RETURN_BUS_COMM_ERROR_COUNT, "Return Bus Communication Error Count" },
300 { RETURN_BUS_EXCEPTION_ERROR_COUNT, "Return Bus Exception Error Count" },
301 { RETURN_SLAVE_MESSAGE_COUNT, "Return Slave Message Count" },
302 { RETURN_SLAVE_NO_RESPONSE_COUNT, "Return Slave No Response Count" },
303 { RETURN_SLAVE_NAK_COUNT, "Return Slave NAK Count" },
304 { RETURN_SLAVE_BUSY_COUNT, "Return Slave Busy Count" },
305 { RETURN_BUS_CHAR_OVERRUN_COUNT, "Return Bus Character Overrun Count" },
306 { CLEAR_OVERRUN_COUNTER_AND_FLAG, "Clear Overrun Counter and Flag" },
310 static const value_string diagnostic_restart_communication_option_vals[] = {
312 { 0xFF, "Clear Log" },
316 /* Translate read device code to string */
317 static const value_string read_device_id_vals[] = {
318 { 1, "Basic Device Identification" },
319 { 2, "Regular Device Identification" },
320 { 3, "Extended Device Identification" },
321 { 4, "Specific Identification Object" },
326 /* Translate read device code to string */
327 static const value_string object_id_vals[] = {
329 { 1, "ProductCode" },
330 { 2, "MajorMinorRevision" },
332 { 4, "ProductName" },
334 { 6, "UserApplicationName" },
339 static const value_string conformity_level_vals[] = {
340 { 0x01, "Basic Device Identification (stream)" },
341 { 0x02, "Regular Device Identification (stream)" },
342 { 0x03, "Extended Device Identification (stream)" },
343 { 0x81, "Basic Device Identification (stream and individual)" },
344 { 0x82, "Regular Device Identification (stream and individual)" },
345 { 0x83, "Extended Device Identification (stream and individual)" },
350 static const enum_val_t mbus_register_format[] = {
351 { "UINT16 ", "UINT16 ", MBTCP_PREF_REGISTER_FORMAT_UINT16 },
352 { "UINT32 ", "UINT32 ", MBTCP_PREF_REGISTER_FORMAT_UINT32 },
353 { "IEEE FLT ", "IEEE FLT ", MBTCP_PREF_REGISTER_FORMAT_IEEE_FLOAT },
354 { "MODICON FLT", "MODICON FLT", MBTCP_PREF_REGISTER_FORMAT_MODICON_FLOAT },
358 static const enum_val_t mbus_register_addr_type[] = {
359 { "RAW ", "RAW ", MBTCP_PREF_REGISTER_ADDR_RAW },
360 { "MODICON 5-DIGIT", "MODICON 5-DIGIT", MBTCP_PREF_REGISTER_ADDR_MOD5 },
361 { "MODICON 6-DIGIT", "MODICON 6-DIGIT", MBTCP_PREF_REGISTER_ADDR_MOD6 },
365 /* Code to dissect Modbus/TCP packets */
367 dissect_mbtcp_pdu(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
369 /* Set up structures needed to add the protocol subtree and manage it */
371 proto_tree *mbtcp_tree;
372 int offset, packet_type;
374 const char *func_string = "";
375 const char *pkt_type_str = "";
376 const char *err_str = "";
377 guint16 transaction_id, protocol_id, len;
378 guint8 unit_id, function_code, exception_code, subfunction_code;
379 void *p_save_proto_data;
380 modbus_request_info_t *request_info;
382 /* Make entries in Protocol column on summary display */
383 col_set_str(pinfo->cinfo, COL_PROTOCOL, "Modbus/TCP");
384 col_clear(pinfo->cinfo, COL_INFO);
386 transaction_id = tvb_get_ntohs(tvb, 0);
387 protocol_id = tvb_get_ntohs(tvb, 2);
388 len = tvb_get_ntohs(tvb, 4);
390 unit_id = tvb_get_guint8(tvb, 6);
391 function_code = tvb_get_guint8(tvb, 7) & 0x7F;
393 /* Make entries in Info column on summary display */
396 /* Find exception - last bit set in function code */
397 if (tvb_get_guint8(tvb, 7) & 0x80) {
398 exception_code = tvb_get_guint8(tvb, offset + 8);
404 if ((function_code == ENCAP_INTERFACE_TRANSP) && (exception_code == 0)) {
405 func_string = val_to_str_const(tvb_get_guint8(tvb, offset + 8), encap_interface_code_vals, "Encapsulated Interface Transport");
406 subfunction_code = 1;
408 else if ((function_code == DIAGNOSTICS) && (exception_code == 0)) {
409 func_string = val_to_str_const(tvb_get_ntohs(tvb, offset + 8), diagnostic_code_vals, "Diagnostics");
410 subfunction_code = 1;
413 func_string = val_to_str(function_code, function_code_vals, "Unknown function (%d)");
414 subfunction_code = 0;
417 /* "Request" or "Response" */
418 packet_type = classify_mbtcp_packet(pinfo);
420 switch ( packet_type ) {
422 pkt_type_str="Query";
424 case RESPONSE_PACKET :
425 pkt_type_str="Response";
427 case CANNOT_CLASSIFY :
428 err_str="Unable to classify as query or response.";
429 pkt_type_str="unknown";
434 if ( exception_code != 0 )
435 err_str="Exception returned ";
437 if (subfunction_code == 0) {
438 if (strlen(err_str) > 0) {
439 col_add_fstr(pinfo->cinfo, COL_INFO,
440 "%8s: Trans: %5u; Unit: %3u, Func: %3u: %s. %s",
441 pkt_type_str, transaction_id, unit_id,
442 function_code, func_string, err_str);
445 col_add_fstr(pinfo->cinfo, COL_INFO,
446 "%8s: Trans: %5u; Unit: %3u, Func: %3u: %s",
447 pkt_type_str, transaction_id, unit_id,
448 function_code, func_string);
452 if (strlen(err_str) > 0) {
453 col_add_fstr(pinfo->cinfo, COL_INFO,
454 "%8s: Trans: %5u; Unit: %3u, Func: %3u/%3u: %s. %s",
455 pkt_type_str, transaction_id, unit_id,
456 function_code, subfunction_code, func_string, err_str);
459 col_add_fstr(pinfo->cinfo, COL_INFO,
460 "%8s: Trans: %5u; Unit: %3u, Func: %3u/%3u: %s",
461 pkt_type_str, transaction_id, unit_id,
462 function_code, subfunction_code, func_string);
466 mi = proto_tree_add_protocol_format(tree, proto_mbtcp, tvb, offset,
467 len+6, "Modbus/TCP");
468 mbtcp_tree = proto_item_add_subtree(mi, ett_mbtcp);
470 /* Add items to protocol tree specific to Modbus/TCP */
471 proto_tree_add_uint(mbtcp_tree, hf_mbtcp_transid, tvb, offset, 2, transaction_id);
472 proto_tree_add_uint(mbtcp_tree, hf_mbtcp_protid, tvb, offset + 2, 2, protocol_id);
473 proto_tree_add_uint(mbtcp_tree, hf_mbtcp_len, tvb, offset + 4, 2, len);
474 proto_tree_add_uint(mbtcp_tree, hf_mbtcp_unitid, tvb, offset + 6, 1, unit_id);
476 /* dissect the Modbus PDU */
477 next_tvb = tvb_new_subset( tvb, offset+7, len-1, len-1);
479 /* keep existing context */
480 p_save_proto_data = p_get_proto_data( pinfo->fd, proto_modbus, 0 );
481 p_remove_proto_data(pinfo->fd, proto_modbus, 0);
483 /* Create enough context for Modbus dissector */
484 request_info = ep_new(modbus_request_info_t);
485 request_info->packet_type = (guint8)packet_type;
486 request_info->register_addr_type = (guint8)global_mbus_tcp_register_addr_type;
487 request_info->register_format = (guint8)global_mbus_tcp_register_format;
488 p_add_proto_data(pinfo->fd, proto_modbus, 0, request_info);
490 /* Continue with dissection of Modbus data payload following Modbus/TCP frame */
491 if( tvb_length_remaining(tvb, offset) > 0 )
492 call_dissector(modbus_handle, next_tvb, pinfo, tree);
494 p_remove_proto_data(pinfo->fd, proto_modbus, 0);
495 p_add_proto_data(pinfo->fd, proto_modbus, 0, p_save_proto_data);
498 /* Code to dissect Modbus RTU over TCP packets */
500 dissect_mbrtu_pdu(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
502 /* Set up structures needed to add the protocol subtree and manage it */
503 proto_item *mi, *crc_item;
504 proto_tree *mbrtu_tree;
505 gint offset, packet_type;
507 const char *func_string = "";
508 const char *pkt_type_str = "";
509 const char *err_str = "";
510 guint16 len, crc16, calc_crc16;
511 guint8 unit_id, function_code, exception_code, subfunction_code;
512 void *p_save_proto_data;
513 modbus_request_info_t *request_info;
515 /* Make entries in Protocol column on summary display */
516 col_set_str(pinfo->cinfo, COL_PROTOCOL, "Modbus RTU");
517 col_clear(pinfo->cinfo, COL_INFO);
519 len = tvb_length(tvb);
521 unit_id = tvb_get_guint8(tvb, 0);
522 function_code = tvb_get_guint8(tvb, 1) & 0x7F;
523 crc16 = tvb_get_ntohs(tvb, len-2);
525 /* Make entries in Info column on summary display */
528 /* Find exception - last bit set in function code */
529 if (tvb_get_guint8(tvb, 1) & 0x80) {
530 exception_code = tvb_get_guint8(tvb, offset + 2);
536 if ((function_code == ENCAP_INTERFACE_TRANSP) && (exception_code == 0)) {
537 func_string = val_to_str_const(tvb_get_guint8(tvb, offset + 2), encap_interface_code_vals, "Encapsulated Interface Transport");
538 subfunction_code = 1;
540 else if ((function_code == DIAGNOSTICS) && (exception_code == 0)) {
541 func_string = val_to_str_const(tvb_get_ntohs(tvb, offset + 2), diagnostic_code_vals, "Diagnostics");
542 subfunction_code = 1;
545 func_string = val_to_str(function_code, function_code_vals, "Unknown function (%d)");
546 subfunction_code = 0;
549 /* "Request" or "Response" */
550 packet_type = classify_mbrtu_packet(pinfo, tvb);
552 switch ( packet_type ) {
554 pkt_type_str="Query";
556 case RESPONSE_PACKET :
557 pkt_type_str="Response";
559 case CANNOT_CLASSIFY :
560 err_str="Unable to classify as query or response.";
561 pkt_type_str="unknown";
566 if ( exception_code != 0 )
567 err_str="Exception returned ";
569 if (subfunction_code == 0) {
570 if (strlen(err_str) > 0) {
571 col_add_fstr(pinfo->cinfo, COL_INFO,
572 "%8s: Unit: %3u, Func: %3u: %s. %s",
573 pkt_type_str, unit_id,
574 function_code, func_string, err_str);
577 col_add_fstr(pinfo->cinfo, COL_INFO,
578 "%8s: Unit: %3u, Func: %3u: %s",
579 pkt_type_str, unit_id,
580 function_code, func_string);
584 if (strlen(err_str) > 0) {
585 col_add_fstr(pinfo->cinfo, COL_INFO,
586 "%8s: Unit: %3u, Func: %3u/%3u: %s. %s",
587 pkt_type_str, unit_id,
588 function_code, subfunction_code, func_string, err_str);
591 col_add_fstr(pinfo->cinfo, COL_INFO,
592 "%8s: Unit: %3u, Func: %3u/%3u: %s",
593 pkt_type_str, unit_id,
594 function_code, subfunction_code, func_string);
598 mi = proto_tree_add_protocol_format(tree, proto_mbrtu, tvb, offset,
600 mbrtu_tree = proto_item_add_subtree(mi, ett_mbrtu);
602 /* Add items to protocol tree specific to Modbus RTU */
603 proto_tree_add_uint(mbrtu_tree, hf_mbrtu_unitid, tvb, offset, 1, unit_id);
604 crc_item = proto_tree_add_uint(mbrtu_tree, hf_mbrtu_crc16, tvb, len-2, 2, crc16);
609 calc_crc16 = crc16_plain_tvb_offset_seed(tvb, offset, len-2, 0xFFFF);
610 if (g_htons(calc_crc16) != crc16)
611 expert_add_info_format_text(pinfo, crc_item, &ei_mbrtu_crc16_incorrect, "Incorrect CRC - should be 0x%04x", g_htons(calc_crc16));
614 /* make sure to ignore the CRC-16 footer bytes */
617 /* dissect the Modbus PDU */
618 next_tvb = tvb_new_subset( tvb, offset+1, len-1, len-1);
620 /* keep existing context */
621 p_save_proto_data = p_get_proto_data( pinfo->fd, proto_modbus, 0 );
622 p_remove_proto_data(pinfo->fd, proto_modbus, 0);
624 /* Create enough context for Modbus dissector */
625 request_info = ep_new(modbus_request_info_t);
626 request_info->packet_type = (guint8)packet_type;
627 request_info->register_addr_type = (guint8)global_mbus_rtu_register_addr_type;
628 request_info->register_format = (guint8)global_mbus_rtu_register_format;
629 p_add_proto_data(pinfo->fd, proto_modbus, 0, request_info);
631 /* Continue with dissection of Modbus data payload following Modbus RTU frame */
632 if( tvb_length_remaining(tvb, offset) > 0 )
633 call_dissector(modbus_handle, next_tvb, pinfo, tree);
635 p_remove_proto_data(pinfo->fd, proto_modbus, 0);
636 p_add_proto_data(pinfo->fd, proto_modbus, 0, p_save_proto_data);
640 /* Return length of Modbus/TCP message */
642 get_mbtcp_pdu_len(packet_info *pinfo _U_, tvbuff_t *tvb, int offset)
647 * Get the length of the data from the encapsulation header.
649 plen = tvb_get_ntohs(tvb, offset + 4);
652 * That length doesn't include the encapsulation header itself;
658 /* Return length of Modbus RTU over TCP message */
660 get_mbrtu_pdu_len(packet_info *pinfo _U_, tvbuff_t *tvb, int offset _U_)
663 /* Modbus/TCP frames include a "length" word in each message; Modbus RTU over TCP does not, so don't attempt to get one */
664 return tvb_length(tvb);
668 /* Code to dissect Modbus/TCP messages */
670 dissect_mbtcp(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, void *data _U_)
673 /* Make sure there's at least enough data to determine it's a Modbus TCP packet */
674 if (!tvb_bytes_exist(tvb, 0, 8))
677 /* check that it actually looks like Modbus/TCP */
678 /* protocol id == 0 */
679 if(tvb_get_ntohs(tvb, 2) != 0 ){
682 /* length is at least 2 (unit_id + function_code) */
683 if(tvb_get_ntohs(tvb, 4) < 2 ){
687 /* build up protocol tree and iterate over multiple packets */
688 tcp_dissect_pdus(tvb, pinfo, tree, mbtcp_desegment, 6,
689 get_mbtcp_pdu_len, dissect_mbtcp_pdu);
691 return tvb_length(tvb);
694 /* Code to dissect Modbus RTU over TCP messages */
696 dissect_mbrtu(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, void *data _U_)
699 /* Make sure there's at least enough data to determine it's a Modbus packet */
700 if (!tvb_bytes_exist(tvb, 0, 6))
703 /* For Modbus RTU mode, confirm that the first byte is a valid address (non-zero), */
704 /* so we can eliminate false-posititves on Modbus TCP messages loaded as RTU */
705 if(tvb_get_guint8(tvb, 0) == 0 )
708 /* build up protocol tree and iterate over multiple packets */
709 tcp_dissect_pdus(tvb, pinfo, tree, mbrtu_desegment, 6,
710 get_mbrtu_pdu_len, dissect_mbrtu_pdu);
712 return tvb_length(tvb);
716 /* Code to allow further dissection of Modbus data payload */
717 /* Common to both Modbus/TCP and Modbus RTU dissectors */
719 dissect_modbus_data(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, guint8 function_code,
720 gint payload_start, gint payload_len, guint8 register_format)
722 gint reported_len, data_offset, reg_num = 0;
723 guint16 data16, modflt_lo, modflt_hi;
724 guint32 data32, modflt_comb;
725 gfloat data_float, modfloat;
726 proto_item *register_item = NULL;
729 reported_len = tvb_reported_length_remaining(tvb, payload_start);
732 if ( payload_start < 0 || ( payload_len + payload_start ) == 0 )
735 /* If calculated length from remaining tvb data != bytes in packet, do not attempt to decode */
736 if ( payload_len != reported_len ) {
737 proto_tree_add_item(tree, hf_modbus_data, tvb, payload_start, reported_len, ENC_NA);
741 /* If data type of payload is Holding or Input registers */
743 /* if payload length is not a multiple of 4, don't attempt to decode anything in 32-bit format */
744 if ((function_code == READ_HOLDING_REGS) || (function_code == READ_INPUT_REGS) || (function_code == WRITE_MULT_REGS)) {
745 if ((payload_len % 4 != 0) && ( (register_format == MBTCP_PREF_REGISTER_FORMAT_UINT32) ||
746 (register_format == MBTCP_PREF_REGISTER_FORMAT_IEEE_FLOAT) ||
747 (register_format == MBTCP_PREF_REGISTER_FORMAT_MODICON_FLOAT) ) ) {
748 register_item = proto_tree_add_item(tree, hf_modbus_data, tvb, payload_start, payload_len, ENC_NA);
749 expert_add_info(pinfo, register_item, &ei_modbus_data_decode);
754 /* Build a new tvb containing just the data payload */
755 next_tvb = tvb_new_subset(tvb, payload_start, payload_len, reported_len);
757 switch ( function_code ) {
759 case READ_HOLDING_REGS:
760 case READ_INPUT_REGS:
761 case WRITE_MULT_REGS:
762 while (data_offset < payload_len) {
763 /* Use "Preferences" options to determine decoding format of register data, as no format is implied by the protocol itself. */
764 /* Based on a standard register size of 16-bits, use decoding format preference to step through each register and display */
765 /* it in an appropriate fashion. */
766 switch (register_format) {
767 case MBTCP_PREF_REGISTER_FORMAT_UINT16: /* Standard-size unsigned integer 16-bit register */
768 data16 = tvb_get_ntohs(next_tvb, data_offset);
769 register_item = proto_tree_add_uint(tree, hf_modbus_reg_uint16, next_tvb, data_offset, 2, data16);
770 proto_item_set_text(register_item, "Register %u (UINT16): %u", reg_num, data16);
775 case MBTCP_PREF_REGISTER_FORMAT_UINT32: /* Double-size unsigned integer 2 x 16-bit registers */
776 data32 = tvb_get_ntohl(next_tvb, data_offset);
777 register_item = proto_tree_add_uint(tree, hf_modbus_reg_uint32, next_tvb, data_offset, 4, data32);
778 proto_item_set_text(register_item, "Register %u (UINT32): %u", reg_num, data32);
783 case MBTCP_PREF_REGISTER_FORMAT_IEEE_FLOAT: /* IEEE Floating Point, 2 x 16-bit registers */
784 data_float = tvb_get_ntohieee_float(next_tvb, data_offset);
785 register_item = proto_tree_add_float(tree, hf_modbus_reg_ieee_float, next_tvb, data_offset, 4, data_float);
786 proto_item_set_text(register_item, "Register %u (IEEE Float): %f", reg_num, data_float);
791 case MBTCP_PREF_REGISTER_FORMAT_MODICON_FLOAT: /* Modicon Floating Point (word-swap), 2 x 16-bit registers */
792 /* Modicon-style Floating Point values are stored in reverse-word order. */
793 /* ie: a standard IEEE float value 59.991459 is equal to 0x426ff741 */
794 /* while the Modicon equivalent to this value is 0xf741426f */
795 /* To re-assemble a proper IEEE float, we must retrieve the 2 x 16-bit words, bit-shift the */
796 /* "hi" component by 16-bits and then OR them together into a combined 32-bit int. */
797 /* Following that operation, use some memcpy magic to copy the 4 raw data bytes from the */
798 /* 32-bit integer into a standard float. Not sure if there is a cleaner way possible using */
799 /* the Wireshark libaries, but this seems to work OK. */
801 modflt_lo = tvb_get_ntohs(next_tvb, data_offset);
802 modflt_hi = tvb_get_ntohs(next_tvb, data_offset+2);
803 modflt_comb = (guint32)(modflt_hi<<16) | modflt_lo;
804 memcpy(&modfloat, &modflt_comb, 4);
806 register_item = proto_tree_add_float(tree, hf_modbus_reg_modicon_float, next_tvb, data_offset, 4, modfloat);
807 proto_item_set_text(register_item, "Register %u (Modicon Float): %f", reg_num, modfloat);
813 /* Avoid any chance of an infinite loop */
814 data_offset = payload_len;
816 } /* register format switch */
823 if ( ! dissector_try_string(modbus_data_dissector_table, "data", next_tvb, pinfo, tree) )
824 proto_tree_add_item(tree, hf_modbus_data, tvb, payload_start, payload_len, ENC_NA);
829 /* Code to actually dissect the packets */
831 dissect_modbus(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, void *data _U_)
833 proto_tree *modbus_tree, *group_tree, *event_tree,
834 *event_item_tree, *device_objects_tree,
835 *device_objects_item_tree;
836 proto_item *mi, *mf, *me, *mei, *doe, *doie;
837 int offset = 0, group_offset;
838 gint payload_start, payload_len, event_index,
839 ii, byte_cnt, len, num_objects, object_index,
841 guint32 group_byte_cnt, group_word_cnt;
842 guint8 function_code, exception_code, mei_code, event_code, object_type;
843 guint8 packet_type, register_format; /*register_addr_type*/
844 guint16 diagnostic_code;
845 modbus_request_info_t *request_info;
847 /* Don't need to do anything if there's no tree */
849 return tvb_length(tvb);
851 len = tvb_length_remaining(tvb, 0);
853 /* If the packet is zero-length, we should not attempt to dissect any further */
857 function_code = tvb_get_guint8(tvb, offset) & 0x7F;
859 /* Find exception - last bit set in function code */
860 if (tvb_get_guint8(tvb, offset) & 0x80 ) {
861 exception_code = tvb_get_guint8(tvb, offset+1);
867 /* See if we have any context */
868 request_info = (modbus_request_info_t *)p_get_proto_data( pinfo->fd, proto_modbus, 0 );
869 if (request_info != NULL)
871 packet_type = request_info->packet_type;
872 register_format = request_info->register_format;
873 /*register_addr_type = request_info->register_addr_type;*/
877 /* Default to a response packet to at least attempt to decode a good chunk of data */
878 packet_type = RESPONSE_PACKET;
879 register_format = MBTCP_PREF_REGISTER_FORMAT_UINT16;
880 /* register_addr_type = MBTCP_PREF_REGISTER_ADDR_RAW;*/
883 /* Add items to protocol tree specific to Modbus generic */
884 mf = proto_tree_add_text(tree, tvb, offset, len, "Modbus");
885 modbus_tree = proto_item_add_subtree(mf, ett_modbus_hdr);
887 mi = proto_tree_add_uint(modbus_tree, hf_mbtcp_functioncode, tvb, offset, 1,
890 payload_start = offset + 1;
891 payload_len = len - 1;
892 if (exception_code != 0) {
893 proto_item_set_text(mi, "Function %u: %s. Exception: %s",
895 val_to_str_const(function_code, function_code_vals, "Unknown Function"),
896 val_to_str(exception_code,
898 "Unknown Exception Code (%u)"));
899 proto_tree_add_uint(modbus_tree, hf_modbus_exceptioncode, tvb, payload_start, 1,
903 switch (function_code) {
906 case READ_DISCRETE_INPUTS:
908 if (packet_type == QUERY_PACKET) {
909 proto_tree_add_item(modbus_tree, hf_modbus_reference, tvb, payload_start, 2, ENC_BIG_ENDIAN);
910 proto_tree_add_item(modbus_tree, hf_modbus_bitcnt, tvb, payload_start + 2, 2, ENC_BIG_ENDIAN);
912 else if (packet_type == RESPONSE_PACKET) {
913 byte_cnt = (guint32)tvb_get_guint8(tvb, payload_start);
914 proto_tree_add_uint(modbus_tree, hf_modbus_bytecnt, tvb, payload_start, 1, byte_cnt);
915 dissect_modbus_data(tvb, pinfo, modbus_tree, function_code, payload_start + 1, byte_cnt, register_format);
919 case READ_HOLDING_REGS:
920 case READ_INPUT_REGS:
921 if (packet_type == QUERY_PACKET) {
922 proto_tree_add_item(modbus_tree, hf_modbus_reference, tvb, payload_start, 2, ENC_BIG_ENDIAN);
923 proto_tree_add_item(modbus_tree, hf_modbus_wordcnt, tvb, payload_start + 2, 2, ENC_BIG_ENDIAN);
925 else if (packet_type == RESPONSE_PACKET) {
926 byte_cnt = (guint32)tvb_get_guint8(tvb, payload_start);
927 proto_tree_add_uint(modbus_tree, hf_modbus_bytecnt, tvb, payload_start, 1, byte_cnt);
928 dissect_modbus_data(tvb, pinfo, modbus_tree, function_code, payload_start + 1, byte_cnt, register_format);
932 case WRITE_SINGLE_COIL:
933 if (packet_type == QUERY_PACKET) {
934 proto_tree_add_item(modbus_tree, hf_modbus_reference, tvb, payload_start, 2, ENC_BIG_ENDIAN);
935 dissect_modbus_data(tvb, pinfo, modbus_tree, function_code, payload_start + 2, 1, register_format);
936 proto_tree_add_text(modbus_tree, tvb, payload_start + 3, 1, "Padding");
938 else if (packet_type == RESPONSE_PACKET) {
939 proto_tree_add_item(modbus_tree, hf_modbus_reference, tvb, payload_start, 2, ENC_BIG_ENDIAN);
940 dissect_modbus_data(tvb, pinfo, modbus_tree, function_code, payload_start + 2, 1, register_format);
941 proto_tree_add_text(modbus_tree, tvb, payload_start + 3, 1, "Padding");
945 case WRITE_SINGLE_REG:
946 if (packet_type == QUERY_PACKET) {
947 proto_tree_add_item(modbus_tree, hf_modbus_reference, tvb, payload_start, 2, ENC_BIG_ENDIAN);
948 dissect_modbus_data(tvb, pinfo, modbus_tree, function_code, payload_start + 2, 2, register_format);
950 else if (packet_type == RESPONSE_PACKET) {
951 proto_tree_add_item(modbus_tree, hf_modbus_reference, tvb, payload_start, 2, ENC_BIG_ENDIAN);
952 dissect_modbus_data(tvb, pinfo, modbus_tree, function_code, payload_start + 2, 2, register_format);
956 case READ_EXCEPT_STAT:
957 if (packet_type == RESPONSE_PACKET)
958 dissect_modbus_data(tvb, pinfo, modbus_tree, function_code, payload_start, 1, register_format);
962 if ((packet_type == QUERY_PACKET) || (packet_type == RESPONSE_PACKET)) {
963 diagnostic_code = tvb_get_ntohs(tvb, payload_start);
964 proto_tree_add_uint(modbus_tree, hf_modbus_diag_sf, tvb, payload_start, 2, diagnostic_code);
965 switch(diagnostic_code)
967 case RETURN_QUERY_DATA:
968 if (packet_type == QUERY_PACKET) {
970 proto_tree_add_item(modbus_tree, hf_modbus_diag_return_query_data_request, tvb, payload_start+2, payload_len-2, ENC_NA);
972 else if (packet_type == RESPONSE_PACKET) {
974 proto_tree_add_item(modbus_tree, hf_modbus_diag_return_query_data_echo, tvb, payload_start+2, payload_len-2, ENC_NA);
977 case RESTART_COMMUNICATION_OPTION:
978 proto_tree_add_item(modbus_tree, hf_modbus_diag_restart_communication_option, tvb, payload_start+2, 2, ENC_BIG_ENDIAN);
980 case RETURN_DIAGNOSTIC_REGISTER:
981 if (packet_type == QUERY_PACKET) {
983 dissect_modbus_data(tvb, pinfo, modbus_tree, function_code, payload_start+2, payload_len-2, register_format);
985 else if (packet_type == RESPONSE_PACKET) {
986 proto_tree_add_item(modbus_tree, hf_modbus_diag_return_diag_register, tvb, payload_start+2, 2, ENC_BIG_ENDIAN);
989 case CHANGE_ASCII_INPUT_DELIMITER:
990 proto_tree_add_item(modbus_tree, hf_modbus_diag_ascii_input_delimiter, tvb, payload_start+2, 1, ENC_BIG_ENDIAN);
992 case RETURN_BUS_MESSAGE_COUNT:
993 if (packet_type == QUERY_PACKET) {
995 dissect_modbus_data(tvb, pinfo, modbus_tree, function_code, payload_start+2, payload_len-2, register_format);
997 else if (packet_type == RESPONSE_PACKET) {
998 proto_tree_add_item(modbus_tree, hf_modbus_diag_return_bus_message_count, tvb, payload_start+2, 2, ENC_BIG_ENDIAN);
1001 case RETURN_BUS_COMM_ERROR_COUNT:
1002 if (packet_type == QUERY_PACKET) {
1003 if (payload_len > 2)
1004 dissect_modbus_data(tvb, pinfo, modbus_tree, function_code, payload_start+2, payload_len-2, register_format);
1006 else if (packet_type == RESPONSE_PACKET) {
1007 proto_tree_add_item(modbus_tree, hf_modbus_diag_return_bus_comm_error_count, tvb, payload_start+2, 2, ENC_BIG_ENDIAN);
1010 case RETURN_BUS_EXCEPTION_ERROR_COUNT:
1011 if (packet_type == QUERY_PACKET) {
1012 if (payload_len > 2)
1013 dissect_modbus_data(tvb, pinfo, modbus_tree, function_code, payload_start+2, payload_len-2, register_format);
1015 else if (packet_type == RESPONSE_PACKET) {
1016 proto_tree_add_item(modbus_tree, hf_modbus_diag_return_bus_exception_error_count, tvb, payload_start+2, 2, ENC_BIG_ENDIAN);
1019 case RETURN_SLAVE_MESSAGE_COUNT:
1020 if (packet_type == QUERY_PACKET) {
1021 if (payload_len > 2)
1022 dissect_modbus_data(tvb, pinfo, modbus_tree, function_code, payload_start+2, payload_len-2, register_format);
1024 else if (packet_type == RESPONSE_PACKET) {
1025 proto_tree_add_item(modbus_tree, hf_modbus_diag_return_slave_message_count, tvb, payload_start+2, 2, ENC_BIG_ENDIAN);
1028 case RETURN_SLAVE_NO_RESPONSE_COUNT:
1029 if (packet_type == QUERY_PACKET) {
1030 if (payload_len > 2)
1031 dissect_modbus_data(tvb, pinfo, modbus_tree, function_code, payload_start+2, payload_len-2, register_format);
1033 else if (packet_type == RESPONSE_PACKET) {
1034 proto_tree_add_item(modbus_tree, hf_modbus_diag_return_no_slave_response_count, tvb, payload_start+2, 2, ENC_BIG_ENDIAN);
1037 case RETURN_SLAVE_NAK_COUNT:
1038 if (packet_type == QUERY_PACKET) {
1039 if (payload_len > 2)
1040 dissect_modbus_data(tvb, pinfo, modbus_tree, function_code, payload_start+2, payload_len-2, register_format);
1042 else if (packet_type == RESPONSE_PACKET) {
1043 proto_tree_add_item(modbus_tree, hf_modbus_diag_return_slave_nak_count, tvb, payload_start+2, 2, ENC_BIG_ENDIAN);
1046 case RETURN_SLAVE_BUSY_COUNT:
1047 if (packet_type == QUERY_PACKET) {
1048 if (payload_len > 2)
1049 dissect_modbus_data(tvb, pinfo, modbus_tree, function_code, payload_start+2, payload_len-2, register_format);
1051 else if (packet_type == RESPONSE_PACKET) {
1052 proto_tree_add_item(modbus_tree, hf_modbus_diag_return_slave_busy_count, tvb, payload_start+2, 2, ENC_BIG_ENDIAN);
1055 case RETURN_BUS_CHAR_OVERRUN_COUNT:
1056 if (packet_type == QUERY_PACKET) {
1057 if (payload_len > 2)
1058 dissect_modbus_data(tvb, pinfo, modbus_tree, function_code, payload_start+2, payload_len-2, register_format);
1060 else if (packet_type == RESPONSE_PACKET) {
1061 proto_tree_add_item(modbus_tree, hf_modbus_diag_return_bus_char_overrun_count, tvb, payload_start+2, 2, ENC_BIG_ENDIAN);
1064 case CLEAR_OVERRUN_COUNTER_AND_FLAG:
1065 case FORCE_LISTEN_ONLY_MODE:
1066 case CLEAR_COUNTERS_AND_DIAG_REG:
1068 if (payload_len > 2)
1069 dissect_modbus_data(tvb, pinfo, modbus_tree, function_code, payload_start+2, payload_len-2, register_format);
1075 case GET_COMM_EVENT_CTRS:
1076 if (packet_type == RESPONSE_PACKET) {
1077 proto_tree_add_item(modbus_tree, hf_modbus_status, tvb, payload_start, 2, ENC_BIG_ENDIAN);
1078 proto_tree_add_item(modbus_tree, hf_modbus_event_count, tvb, payload_start+2, 2, ENC_BIG_ENDIAN);
1082 case GET_COMM_EVENT_LOG:
1083 if (packet_type == RESPONSE_PACKET) {
1084 byte_cnt = (guint32)tvb_get_guint8(tvb, payload_start);
1085 proto_tree_add_uint(modbus_tree, hf_modbus_bytecnt, tvb, payload_start, 1, byte_cnt);
1086 proto_tree_add_item(modbus_tree, hf_modbus_status, tvb, payload_start+1, 2, ENC_BIG_ENDIAN);
1087 proto_tree_add_item(modbus_tree, hf_modbus_event_count, tvb, payload_start+3, 2, ENC_BIG_ENDIAN);
1088 proto_tree_add_item(modbus_tree, hf_modbus_message_count, tvb, payload_start+5, 2, ENC_BIG_ENDIAN);
1089 if (byte_cnt-6 > 0) {
1092 me = proto_tree_add_text(modbus_tree, tvb, payload_start+7, byte_cnt, "Events");
1093 event_tree = proto_item_add_subtree(me, ett_events);
1094 while (byte_cnt > 0) {
1095 event_code = tvb_get_guint8(tvb, payload_start+7+event_index);
1096 if (event_code == 0) {
1097 proto_tree_add_text(event_tree, tvb, payload_start+7+event_index, 1, "Initiated Communication Restart");
1099 else if (event_code == 4) {
1100 proto_tree_add_text(event_tree, tvb, payload_start+7+event_index, 1, "Entered Listen Only Mode");
1102 else if (event_code & REMOTE_DEVICE_RECV_EVENT_MASK) {
1103 mei = proto_tree_add_text(event_tree, tvb, payload_start+7+event_index, 1, "Receive Event: 0x%02X", event_code);
1104 event_item_tree = proto_item_add_subtree(mei, ett_events_recv);
1106 /* add subtrees to describe each event bit */
1107 proto_tree_add_item(event_item_tree, hf_modbus_event_recv_comm_err,
1108 tvb, payload_start+7+event_index, 1, ENC_LITTLE_ENDIAN );
1109 proto_tree_add_item(event_item_tree, hf_modbus_event_recv_char_over,
1110 tvb, payload_start+7+event_index, 1, ENC_LITTLE_ENDIAN );
1111 proto_tree_add_item(event_item_tree, hf_modbus_event_recv_lo_mode,
1112 tvb, payload_start+7+event_index, 1, ENC_LITTLE_ENDIAN );
1113 proto_tree_add_item(event_item_tree, hf_modbus_event_recv_broadcast,
1114 tvb, payload_start+7+event_index, 1, ENC_LITTLE_ENDIAN );
1116 else if ((event_code & REMOTE_DEVICE_SEND_EVENT_MASK) == REMOTE_DEVICE_SEND_EVENT_VALUE) {
1117 mei = proto_tree_add_text(event_tree, tvb, payload_start+7+event_index, 1, "Send Event: 0x%02X", event_code);
1118 event_item_tree = proto_item_add_subtree(mei, ett_events_send);
1120 /* add subtrees to describe each event bit */
1121 proto_tree_add_item(event_item_tree, hf_modbus_event_send_read_ex,
1122 tvb, payload_start+7+event_index, 1, ENC_LITTLE_ENDIAN );
1123 proto_tree_add_item(event_item_tree, hf_modbus_event_send_slave_abort_ex,
1124 tvb, payload_start+7+event_index, 1, ENC_LITTLE_ENDIAN );
1125 proto_tree_add_item(event_item_tree, hf_modbus_event_send_slave_busy_ex,
1126 tvb, payload_start+7+event_index, 1, ENC_LITTLE_ENDIAN );
1127 proto_tree_add_item(event_item_tree, hf_modbus_event_send_slave_nak_ex,
1128 tvb, payload_start+7+event_index, 1, ENC_LITTLE_ENDIAN );
1129 proto_tree_add_item(event_item_tree, hf_modbus_event_send_write_timeout,
1130 tvb, payload_start+7+event_index, 1, ENC_LITTLE_ENDIAN );
1131 proto_tree_add_item(event_item_tree, hf_modbus_event_send_lo_mode,
1132 tvb, payload_start+7+event_index, 1, ENC_LITTLE_ENDIAN );
1135 proto_tree_add_text(event_tree, tvb, payload_start+7+event_index, 1, "Unknown Event");
1145 case WRITE_MULT_COILS:
1146 if (packet_type == QUERY_PACKET) {
1147 proto_tree_add_item(modbus_tree, hf_modbus_reference, tvb, payload_start, 2, ENC_BIG_ENDIAN);
1148 proto_tree_add_item(modbus_tree, hf_modbus_bitcnt, tvb, payload_start + 2, 2, ENC_BIG_ENDIAN);
1149 byte_cnt = (guint32)tvb_get_guint8(tvb, payload_start + 4);
1150 proto_tree_add_uint(modbus_tree, hf_modbus_bytecnt, tvb, payload_start + 4, 1,
1152 dissect_modbus_data(tvb, pinfo, modbus_tree, function_code, payload_start + 5, byte_cnt, register_format);
1154 else if (packet_type == RESPONSE_PACKET) {
1155 proto_tree_add_item(modbus_tree, hf_modbus_reference, tvb, payload_start, 2, ENC_BIG_ENDIAN);
1156 proto_tree_add_item(modbus_tree, hf_modbus_bitcnt, tvb, payload_start + 2, 2, ENC_BIG_ENDIAN);
1160 case WRITE_MULT_REGS:
1161 if (packet_type == QUERY_PACKET) {
1162 proto_tree_add_item(modbus_tree, hf_modbus_reference, tvb, payload_start, 2, ENC_BIG_ENDIAN);
1163 proto_tree_add_item(modbus_tree, hf_modbus_wordcnt, tvb, payload_start + 2, 2, ENC_BIG_ENDIAN);
1164 byte_cnt = (guint32)tvb_get_guint8(tvb, payload_start + 4);
1165 proto_tree_add_uint(modbus_tree, hf_modbus_bytecnt, tvb, payload_start + 4, 1,
1167 dissect_modbus_data(tvb, pinfo, modbus_tree, function_code, payload_start + 5, byte_cnt, register_format);
1169 else if (packet_type == RESPONSE_PACKET) {
1170 proto_tree_add_item(modbus_tree, hf_modbus_reference, tvb, payload_start, 2, ENC_BIG_ENDIAN);
1171 proto_tree_add_item(modbus_tree, hf_modbus_wordcnt, tvb, payload_start + 2, 2, ENC_BIG_ENDIAN);
1175 case READ_FILE_RECORD:
1176 if (packet_type == QUERY_PACKET) {
1177 byte_cnt = (guint32)tvb_get_guint8(tvb, payload_start);
1178 proto_tree_add_uint(modbus_tree, hf_modbus_bytecnt, tvb, payload_start, 1,
1181 /* add subtrees to describe each group of packet */
1182 group_offset = payload_start + 1;
1183 for (ii = 0; ii < byte_cnt / 7; ii++) {
1184 mi = proto_tree_add_text( modbus_tree, tvb, group_offset, 7,
1186 group_tree = proto_item_add_subtree(mi, ett_group_hdr);
1187 proto_tree_add_item(group_tree, hf_modbus_reftype, tvb, group_offset, 1, ENC_BIG_ENDIAN);
1188 proto_tree_add_item(group_tree, hf_modbus_lreference, tvb, group_offset + 1, 4, ENC_BIG_ENDIAN);
1189 proto_tree_add_item(group_tree, hf_modbus_wordcnt, tvb, group_offset + 5, 2, ENC_BIG_ENDIAN);
1193 else if (packet_type == RESPONSE_PACKET) {
1194 byte_cnt = (guint32)tvb_get_guint8(tvb, payload_start);
1195 proto_tree_add_uint(modbus_tree, hf_modbus_bytecnt, tvb, payload_start, 1,
1198 /* add subtrees to describe each group of packet */
1199 group_offset = payload_start + 1;
1201 while (byte_cnt > 0) {
1202 group_byte_cnt = (guint32)tvb_get_guint8(tvb, group_offset);
1203 mi = proto_tree_add_text( modbus_tree, tvb, group_offset, group_byte_cnt + 1,
1205 group_tree = proto_item_add_subtree(mi, ett_group_hdr);
1206 proto_tree_add_uint(group_tree, hf_modbus_bytecnt, tvb, group_offset, 1,
1208 proto_tree_add_item(group_tree, hf_modbus_reftype, tvb, group_offset + 1, 1, ENC_BIG_ENDIAN);
1209 dissect_modbus_data(tvb, pinfo, group_tree, function_code, group_offset + 2, group_byte_cnt - 1, register_format);
1210 group_offset += (group_byte_cnt + 1);
1211 byte_cnt -= (group_byte_cnt + 1);
1217 case WRITE_FILE_RECORD:
1218 if ((packet_type == QUERY_PACKET) || (packet_type == RESPONSE_PACKET)) {
1219 byte_cnt = (guint32)tvb_get_guint8(tvb, payload_start);
1220 proto_tree_add_uint(modbus_tree, hf_modbus_bytecnt, tvb, payload_start, 1,
1223 /* add subtrees to describe each group of packet */
1224 group_offset = payload_start + 1;
1226 while (byte_cnt > 0) {
1227 group_word_cnt = tvb_get_ntohs(tvb, group_offset + 5);
1228 group_byte_cnt = (2 * group_word_cnt) + 7;
1229 mi = proto_tree_add_text( modbus_tree, tvb, group_offset,
1230 group_byte_cnt, "Group %u", ii);
1231 group_tree = proto_item_add_subtree(mi, ett_group_hdr);
1232 proto_tree_add_item(group_tree, hf_modbus_reftype, tvb, group_offset, 1, ENC_BIG_ENDIAN);
1233 proto_tree_add_item(group_tree, hf_modbus_lreference, tvb, group_offset + 1, 4, ENC_BIG_ENDIAN);
1234 proto_tree_add_uint(group_tree, hf_modbus_wordcnt, tvb, group_offset + 5, 2,
1236 dissect_modbus_data(tvb, pinfo, group_tree, function_code, group_offset + 7, group_byte_cnt - 7, register_format);
1237 group_offset += group_byte_cnt;
1238 byte_cnt -= group_byte_cnt;
1244 case MASK_WRITE_REG:
1245 if ((packet_type == QUERY_PACKET) || (packet_type == RESPONSE_PACKET)) {
1246 proto_tree_add_item(modbus_tree, hf_modbus_reference, tvb, payload_start, 2, ENC_BIG_ENDIAN);
1247 proto_tree_add_item(modbus_tree, hf_modbus_andmask, tvb, payload_start + 2, 2, ENC_BIG_ENDIAN);
1248 proto_tree_add_item(modbus_tree, hf_modbus_ormask, tvb, payload_start + 4, 2, ENC_BIG_ENDIAN);
1252 case READ_WRITE_REG:
1253 if (packet_type == QUERY_PACKET) {
1254 proto_tree_add_item(modbus_tree, hf_modbus_readref, tvb, payload_start, 2, ENC_BIG_ENDIAN);
1255 proto_tree_add_item(modbus_tree, hf_modbus_readwordcnt, tvb, payload_start + 2, 2, ENC_BIG_ENDIAN);
1256 proto_tree_add_item(modbus_tree, hf_modbus_writeref, tvb, payload_start + 4, 2, ENC_BIG_ENDIAN);
1257 proto_tree_add_item(modbus_tree, hf_modbus_writewordcnt, tvb, payload_start + 6, 2, ENC_BIG_ENDIAN);
1258 byte_cnt = (guint32)tvb_get_guint8(tvb, payload_start + 8);
1259 proto_tree_add_uint(modbus_tree, hf_modbus_bytecnt, tvb, payload_start + 8, 1,
1261 dissect_modbus_data(tvb, pinfo, modbus_tree, function_code, payload_start + 9, byte_cnt, register_format);
1263 else if (packet_type == RESPONSE_PACKET) {
1264 byte_cnt = (guint32)tvb_get_guint8(tvb, payload_start);
1265 proto_tree_add_uint(modbus_tree, hf_modbus_bytecnt, tvb, payload_start, 1,
1267 dissect_modbus_data(tvb, pinfo, modbus_tree, function_code, payload_start + 1, byte_cnt, register_format);
1271 case READ_FIFO_QUEUE:
1272 if (packet_type == QUERY_PACKET)
1273 proto_tree_add_item(modbus_tree, hf_modbus_reference, tvb, payload_start, 2, ENC_BIG_ENDIAN);
1274 else if (packet_type == RESPONSE_PACKET) {
1275 byte_cnt = (guint32)tvb_get_ntohs(tvb, payload_start);
1276 proto_tree_add_uint(modbus_tree, hf_modbus_lbytecnt, tvb, payload_start, 2,
1278 proto_tree_add_item(modbus_tree, hf_modbus_wordcnt, tvb, payload_start + 2, 2, ENC_BIG_ENDIAN);
1279 dissect_modbus_data(tvb, pinfo, modbus_tree, function_code, payload_start + 4, byte_cnt - 2, register_format);
1283 case ENCAP_INTERFACE_TRANSP:
1284 if (packet_type == QUERY_PACKET) {
1285 proto_tree_add_item(modbus_tree, hf_modbus_mei, tvb, payload_start, 1, ENC_BIG_ENDIAN);
1286 mei_code = tvb_get_guint8(tvb, payload_start);
1289 case READ_DEVICE_ID:
1290 proto_tree_add_item(modbus_tree, hf_modbus_read_device_id, tvb, payload_start+1, 1, ENC_BIG_ENDIAN);
1291 proto_tree_add_item(modbus_tree, hf_modbus_object_id, tvb, payload_start+2, 1, ENC_BIG_ENDIAN);
1294 case CANOPEN_REQ_RESP:
1295 /* CANopen protocol not part of the Modbus/TCP specification */
1297 if (payload_len > 1)
1298 dissect_modbus_data(tvb, pinfo, modbus_tree, function_code, payload_start, payload_len-1, register_format);
1302 else if (packet_type == RESPONSE_PACKET) {
1303 proto_tree_add_item(modbus_tree, hf_modbus_mei, tvb, payload_start, 1, ENC_BIG_ENDIAN);
1304 mei_code = tvb_get_guint8(tvb, payload_start);
1307 case READ_DEVICE_ID:
1308 proto_tree_add_item(modbus_tree, hf_modbus_read_device_id, tvb, payload_start+1, 1, ENC_BIG_ENDIAN);
1309 proto_tree_add_item(modbus_tree, hf_modbus_conformity_level, tvb, payload_start+2, 1, ENC_BIG_ENDIAN);
1310 proto_tree_add_item(modbus_tree, hf_modbus_more_follows, tvb, payload_start+3, 1, ENC_BIG_ENDIAN);
1311 proto_tree_add_item(modbus_tree, hf_modbus_next_object_id, tvb, payload_start+4, 1, ENC_BIG_ENDIAN);
1312 num_objects = tvb_get_guint8(tvb, payload_start+5);
1313 proto_tree_add_uint(modbus_tree, hf_modbus_num_objects, tvb, payload_start+5, 1, num_objects);
1314 doe = proto_tree_add_text(modbus_tree, tvb, payload_start+6, payload_len-6, "Objects");
1317 for (ii = 0; ii < num_objects; ii++)
1319 device_objects_tree = proto_item_add_subtree(doe, ett_device_id_objects);
1321 /* add each "object item" as its own subtree */
1323 /* compute length of object */
1324 object_type = tvb_get_guint8(tvb, payload_start+6+object_index);
1325 object_len = tvb_get_guint8(tvb, payload_start+6+object_index+1);
1327 doie = proto_tree_add_text(device_objects_tree, tvb, payload_start+6+object_index, 2+object_len, "Object #%d", ii+1);
1328 device_objects_item_tree = proto_item_add_subtree(doie, ett_device_id_object_items);
1330 proto_tree_add_item(device_objects_item_tree, hf_modbus_object_id, tvb, payload_start+6+object_index, 1, ENC_BIG_ENDIAN);
1333 proto_tree_add_uint(device_objects_item_tree, hf_modbus_list_object_len, tvb, payload_start+6+object_index, 1, object_len);
1336 if (object_type < 7)
1338 proto_tree_add_item(device_objects_item_tree, hf_modbus_object_str_value, tvb, payload_start+6+object_index, object_len, ENC_ASCII|ENC_NA);
1343 proto_tree_add_text(device_objects_item_tree, tvb, payload_start+6+object_index, object_len, "Object Value");
1345 object_index += object_len;
1349 case CANOPEN_REQ_RESP:
1350 /* CANopen protocol not part of the Modbus/TCP specification */
1352 if (payload_len > 1)
1353 dissect_modbus_data(tvb, pinfo, modbus_tree, function_code, payload_start, payload_len-1, register_format);
1359 case REPORT_SLAVE_ID:
1361 if (payload_len > 0)
1362 dissect_modbus_data(tvb, pinfo, modbus_tree, function_code, payload_start, payload_len, register_format);
1367 return tvb_length(tvb);
1371 /* Register the protocol with Wireshark */
1373 void proto_reg_handoff_mbtcp(void);
1374 void proto_reg_handoff_mbrtu(void);
1377 proto_register_modbus(void)
1379 /* Modbus/TCP header fields */
1380 static hf_register_info mbtcp_hf[] = {
1381 { &hf_mbtcp_transid,
1382 { "Transaction Identifier", "mbtcp.trans_id",
1383 FT_UINT16, BASE_DEC, NULL, 0x0,
1387 { "Protocol Identifier", "mbtcp.prot_id",
1388 FT_UINT16, BASE_DEC, NULL, 0x0,
1392 { "Length", "mbtcp.len",
1393 FT_UINT16, BASE_DEC, NULL, 0x0,
1397 { "Unit Identifier", "mbtcp.unit_id",
1398 FT_UINT8, BASE_DEC, NULL, 0x0,
1403 static hf_register_info mbrtu_hf[] = {
1405 { "Unit ID", "mbrtu.unit_id",
1406 FT_UINT8, BASE_DEC, NULL, 0x0,
1410 { "CRC-16", "mbrtu.crc16",
1411 FT_UINT16, BASE_HEX, NULL, 0x0,
1416 static ei_register_info mbrtu_ei[] = {
1417 { &ei_mbrtu_crc16_incorrect, { "mbrtu.crc16.incorrect", PI_CHECKSUM, PI_WARN, "Incorrect CRC", EXPFILL }},
1420 static hf_register_info hf[] = {
1421 /* Modbus header fields */
1422 { &hf_mbtcp_functioncode,
1423 { "Function Code", "modbus.func_code",
1424 FT_UINT8, BASE_DEC, VALS(function_code_vals), 0x0,
1427 { &hf_modbus_reference,
1428 { "Reference Number", "modbus.reference_num",
1429 FT_UINT16, BASE_DEC, NULL, 0x0,
1432 { &hf_modbus_lreference,
1433 { "Reference Number (32 bit)", "modbus.reference_num_32",
1434 FT_UINT32, BASE_DEC, NULL, 0x0,
1437 { &hf_modbus_reftype,
1438 { "Reference Type", "modbus.reference_type",
1439 FT_UINT8, BASE_DEC, NULL, 0x0,
1442 { &hf_modbus_readref,
1443 { "Read Reference Number", "modbus.read_reference_num",
1444 FT_UINT16, BASE_DEC, NULL, 0x0,
1447 { &hf_modbus_writeref,
1448 { "Write Reference Number", "modbus.write_reference_num",
1449 FT_UINT16, BASE_DEC, NULL, 0x0,
1452 { &hf_modbus_wordcnt,
1453 { "Word Count", "modbus.word_cnt",
1454 FT_UINT16, BASE_DEC, NULL, 0x0,
1457 { &hf_modbus_readwordcnt,
1458 { "Read Word Count", "modbus.read_word_cnt",
1459 FT_UINT16, BASE_DEC, NULL, 0x0,
1462 { &hf_modbus_writewordcnt,
1463 { "Write Word Count", "modbus.write_word_cnt",
1464 FT_UINT16, BASE_DEC, NULL, 0x0,
1467 { &hf_modbus_bitcnt,
1468 { "Bit Count", "modbus.bit_cnt",
1469 FT_UINT16, BASE_DEC, NULL, 0x0,
1472 { &hf_modbus_bytecnt,
1473 { "Byte Count", "modbus.byte_cnt",
1474 FT_UINT8, BASE_DEC, NULL, 0x0,
1477 { &hf_modbus_lbytecnt,
1478 { "Byte Count (16-bit)", "modbus.byte_cnt_16",
1479 FT_UINT8, BASE_DEC, NULL, 0x0,
1482 { &hf_modbus_exceptioncode,
1483 { "Exception Code", "modbus.exception_code",
1484 FT_UINT8, BASE_DEC, VALS(exception_code_vals), 0x0,
1487 { &hf_modbus_diag_sf,
1488 { "Diagnostic Code", "modbus.diagnostic_code",
1489 FT_UINT16, BASE_DEC, VALS(diagnostic_code_vals), 0x0,
1492 { &hf_modbus_diag_return_query_data_request,
1493 { "Request Data", "modbus.diagnostic.return_query_data.request",
1494 FT_BYTES, BASE_NONE, NULL, 0x0,
1497 { &hf_modbus_diag_return_query_data_echo,
1498 { "Echo Data", "modbus.diagnostic.return_query_data.echo",
1499 FT_BYTES, BASE_NONE, NULL, 0x0,
1502 { &hf_modbus_diag_restart_communication_option,
1503 { "Restart Communication Option", "modbus.diagnostic.restart_communication_option",
1504 FT_UINT16, BASE_HEX, VALS(diagnostic_restart_communication_option_vals), 0x0,
1507 { &hf_modbus_diag_return_diag_register,
1508 { "Diagnostic Register Contents", "modbus.diagnostic.return_diag_register",
1509 FT_UINT16, BASE_HEX, NULL, 0x0,
1512 { &hf_modbus_diag_ascii_input_delimiter,
1513 { "CHAR", "modbus.diagnostic.ascii_input_delimiter",
1514 FT_UINT8, BASE_HEX, NULL, 0x0,
1517 { &hf_modbus_diag_return_bus_message_count,
1518 { "Total Message Count", "modbus.diagnostic.bus_message_count",
1519 FT_UINT16, BASE_DEC, NULL, 0x0,
1522 { &hf_modbus_diag_return_bus_comm_error_count,
1523 { "CRC Error Count", "modbus.diagnostic.bus_comm_error_count",
1524 FT_UINT16, BASE_DEC, NULL, 0x0,
1527 { &hf_modbus_diag_return_bus_exception_error_count,
1528 { "Exception Error Count", "modbus.diagnostic.bus_exception_error_count",
1529 FT_UINT16, BASE_DEC, NULL, 0x0,
1532 { &hf_modbus_diag_return_slave_message_count,
1533 { "Slave Message Count", "modbus.diagnostic.slave_message_count",
1534 FT_UINT16, BASE_DEC, NULL, 0x0,
1537 { &hf_modbus_diag_return_no_slave_response_count,
1538 { "Slave No Response Count", "modbus.diagnostic.no_slave_response_count",
1539 FT_UINT16, BASE_DEC, NULL, 0x0,
1542 { &hf_modbus_diag_return_slave_nak_count,
1543 { "Slave NAK Count", "modbus.diagnostic.slave_nak_count",
1544 FT_UINT16, BASE_DEC, NULL, 0x0,
1547 { &hf_modbus_diag_return_slave_busy_count,
1548 { "Slave Device Busy Count", "modbus.diagnostic.slave_busy_count",
1549 FT_UINT16, BASE_DEC, NULL, 0x0,
1552 { &hf_modbus_diag_return_bus_char_overrun_count,
1553 { "Slave Character Overrun Count", "modbus.diagnostic.bus_char_overrun_count",
1554 FT_UINT16, BASE_DEC, NULL, 0x0,
1557 { &hf_modbus_status,
1558 { "Status", "modbus.ev_status",
1559 FT_UINT16, BASE_HEX, NULL, 0x0,
1562 { &hf_modbus_event_count,
1563 { "Event Vount", "modbus.ev_count",
1564 FT_UINT16, BASE_DEC, NULL, 0x0,
1567 { &hf_modbus_message_count,
1568 { "Message Count", "modbus.ev_msg_count",
1569 FT_UINT16, BASE_DEC, NULL, 0x0,
1572 { &hf_modbus_event_recv_comm_err,
1573 { "Communication Error", "modbus.ev_recv_comm_err",
1574 FT_UINT8, BASE_DEC, NULL, 0x02,
1577 { &hf_modbus_event_recv_char_over,
1578 { "Character Overrun", "modbus.ev_recv_char_over",
1579 FT_UINT8, BASE_DEC, NULL, 0x10,
1582 { &hf_modbus_event_recv_lo_mode,
1583 { "Currently in Listen Only Mode", "modbus.ev_recv_lo_mode",
1584 FT_UINT8, BASE_DEC, NULL, 0x20,
1587 { &hf_modbus_event_recv_broadcast,
1588 { "Broadcast Received", "modbus.ev_recv_broadcast",
1589 FT_UINT8, BASE_DEC, NULL, 0x40,
1592 { &hf_modbus_event_send_read_ex,
1593 { "Read Exception Sent", "modbus.ev_send_read_ex",
1594 FT_UINT8, BASE_DEC, NULL, 0x01,
1597 { &hf_modbus_event_send_slave_abort_ex,
1598 { "Slave Abort Exception Sent", "modbus.ev_send_slave_abort_ex",
1599 FT_UINT8, BASE_DEC, NULL, 0x02,
1602 { &hf_modbus_event_send_slave_busy_ex,
1603 { "Slave Busy Exception Sent", "modbus.ev_send_slave_busy_ex",
1604 FT_UINT8, BASE_DEC, NULL, 0x04,
1607 { &hf_modbus_event_send_slave_nak_ex,
1608 { "Slave Program NAK Exception Sent", "modbus.ev_send_slave_nak_ex",
1609 FT_UINT8, BASE_DEC, NULL, 0x08,
1612 { &hf_modbus_event_send_write_timeout,
1613 { "Write Timeout Error Occurred", "modbus.ev_send_write_timeout",
1614 FT_UINT8, BASE_DEC, NULL, 0x10,
1617 { &hf_modbus_event_send_lo_mode,
1618 { "Currently in Listen Only Mode", "modbus.ev_send_lo_mode",
1619 FT_UINT8, BASE_DEC, NULL, 0x20,
1622 { &hf_modbus_andmask,
1623 { "AND mask", "modbus.and_mask",
1624 FT_UINT16, BASE_HEX, NULL, 0x0,
1627 { &hf_modbus_ormask,
1628 { "OR mask", "modbus.or_mask",
1629 FT_UINT16, BASE_HEX, NULL, 0x0,
1633 { "Data", "modbus.data",
1634 FT_BYTES, BASE_NONE, NULL, 0x0, NULL, HFILL }
1637 { "MEI type", "modbus.mei",
1638 FT_UINT8, BASE_DEC, VALS(encap_interface_code_vals), 0x0,
1641 { &hf_modbus_read_device_id,
1642 { "Read Device ID", "modbus.read_device_id",
1643 FT_UINT8, BASE_DEC, VALS(read_device_id_vals), 0x0,
1646 { &hf_modbus_object_id,
1647 { "Object ID", "modbus.object_id",
1648 FT_UINT8, BASE_DEC, VALS(object_id_vals), 0x0,
1651 { &hf_modbus_num_objects,
1652 { "Number of Objects", "modbus.num_objects",
1653 FT_UINT8, BASE_DEC, NULL, 0x0,
1656 { &hf_modbus_list_object_len,
1657 { "Object length", "modbus.objects_len",
1658 FT_UINT8, BASE_DEC, NULL, 0x0,
1661 { &hf_modbus_conformity_level,
1662 { "Conformity Level", "modbus.conformity_level",
1663 FT_UINT8, BASE_HEX, VALS(conformity_level_vals), 0x0,
1666 { &hf_modbus_more_follows,
1667 { "More Follows", "modbus.more_follows",
1668 FT_UINT8, BASE_HEX, NULL, 0x0,
1671 { &hf_modbus_next_object_id,
1672 { "Next Object ID", "modbus.next_object_id",
1673 FT_UINT8, BASE_DEC, NULL, 0x0,
1676 { &hf_modbus_object_str_value,
1677 { "Object String Value", "modbus.object_str_value",
1678 FT_STRING, BASE_NONE, NULL, 0x0,
1681 { &hf_modbus_reg_uint16,
1682 { "Register (UINT16)", "modbus.register.uint16",
1683 FT_UINT16, BASE_DEC, NULL, 0x0,
1686 { &hf_modbus_reg_uint32,
1687 { "Register (UINT32)", "modbus.register.uint32",
1688 FT_UINT32, BASE_DEC, NULL, 0x0,
1691 { &hf_modbus_reg_ieee_float,
1692 { "Register (IEEE Float)", "modbus.register.ieee_float",
1693 FT_FLOAT, BASE_NONE, NULL, 0x0,
1696 { &hf_modbus_reg_modicon_float,
1697 { "Register (Modicon Float)", "modbus.register.modicon_float",
1698 FT_FLOAT, BASE_NONE, NULL, 0x0,
1703 /* Setup protocol subtree array */
1704 static gint *ett[] = {
1712 &ett_device_id_objects,
1713 &ett_device_id_object_items
1716 static ei_register_info ei[] = {
1717 { &ei_modbus_data_decode, { "modbus.data.decode", PI_PROTOCOL, PI_WARN, "Invalid decoding options, register data not a multiple of 4!", EXPFILL }},
1720 module_t *mbtcp_module;
1721 module_t *mbrtu_module;
1722 expert_module_t* expert_mbrtu;
1723 expert_module_t* expert_modbus;
1725 /* Register the protocol name and description */
1726 proto_mbtcp = proto_register_protocol("Modbus/TCP", "Modbus/TCP", "mbtcp");
1727 proto_mbrtu = proto_register_protocol("Modbus RTU", "Modbus RTU", "mbrtu");
1728 proto_modbus = proto_register_protocol("Modbus", "Modbus", "modbus");
1730 /* Registering protocol to be called by another dissector */
1731 new_register_dissector("mbtcp", dissect_mbtcp, proto_mbtcp);
1732 new_register_dissector("mbrtu", dissect_mbrtu, proto_mbrtu);
1733 new_register_dissector("modbus", dissect_modbus, proto_modbus);
1735 /* Registering subdissectors table */
1736 modbus_data_dissector_table = register_dissector_table("modbus.data", "Modbus Data", FT_STRING, BASE_NONE);
1737 modbus_dissector_table = register_dissector_table("mbtcp.prot_id", "protocol identifier", FT_UINT16, BASE_DEC);
1739 /* Required function calls to register the header fields and subtrees used */
1740 proto_register_field_array(proto_mbtcp, mbtcp_hf, array_length(mbtcp_hf));
1741 proto_register_field_array(proto_mbrtu, mbrtu_hf, array_length(mbrtu_hf));
1742 proto_register_field_array(proto_modbus, hf, array_length(hf));
1743 proto_register_subtree_array(ett, array_length(ett));
1744 expert_mbrtu = expert_register_protocol(proto_mbrtu);
1745 expert_register_field_array(expert_mbrtu, mbrtu_ei, array_length(mbrtu_ei));
1746 expert_modbus = expert_register_protocol(proto_modbus);
1747 expert_register_field_array(expert_modbus, ei, array_length(ei));
1750 /* Register required preferences for Modbus Protocol register decoding */
1751 mbtcp_module = prefs_register_protocol(proto_mbtcp, proto_reg_handoff_mbtcp);
1752 mbrtu_module = prefs_register_protocol(proto_mbrtu, proto_reg_handoff_mbrtu);
1754 /* Modbus RTU Preference - Desegment, defaults to TRUE for TCP desegmentation */
1755 prefs_register_bool_preference(mbtcp_module, "desegment",
1756 "Desegment all Modbus RTU packets spanning multiple TCP segments",
1757 "Whether the Modbus RTU dissector should desegment all messages spanning multiple TCP segments",
1760 /* Modbus/TCP Preference - Default TCP Port, allows for "user" port either than 502. */
1761 prefs_register_uint_preference(mbtcp_module, "tcp.port", "Modbus/TCP Port",
1762 "Set the TCP port for Modbus/TCP packets (if other"
1763 " than the default of 502)",
1764 10, &global_mbus_tcp_port);
1766 /* Modbus/TCP Preference - Holding/Input Register format, this allows for deeper dissection of response data */
1767 prefs_register_enum_preference(mbtcp_module, "mbus_register_format",
1768 "Holding/Input Register Format",
1770 &global_mbus_tcp_register_format,
1771 mbus_register_format,
1774 /* Modbus/TCP Preference - Register addressing format, this allows for a configurable display option to determine addressing used */
1775 prefs_register_enum_preference(mbtcp_module, "mbus_register_addr_type",
1776 "Register Addressing Type",
1777 "Register Addressing Type (Raw, Modicon 5 or 6). This option has no effect on the underlying protocol, but changes the register address display format",
1778 &global_mbus_tcp_register_addr_type,
1779 mbus_register_addr_type,
1782 /* Modbus RTU Preference - Desegment, defaults to TRUE for TCP desegmentation */
1783 prefs_register_bool_preference(mbrtu_module, "desegment",
1784 "Desegment all Modbus RTU packets spanning multiple TCP segments",
1785 "Whether the Modbus RTU dissector should desegment all messages spanning multiple TCP segments",
1788 /* Modbus RTU Preference - CRC verification, defaults to FALSE (not do verification)*/
1789 prefs_register_bool_preference(mbrtu_module, "crc_verification",
1791 "Whether to validate the CRC",
1794 /* Modbus RTU Preference - Default TCP Port, defaults to zero, allows custom user port. */
1795 prefs_register_uint_preference(mbrtu_module, "tcp.port", "Modbus RTU Port",
1796 "Set the TCP port for encapsulated Modbus RTU packets",
1797 10, &global_mbus_rtu_port);
1799 /* Modbus RTU Preference - Holding/Input Register format, this allows for deeper dissection of response data */
1800 prefs_register_enum_preference(mbrtu_module, "mbus_register_format",
1801 "Holding/Input Register Format",
1803 &global_mbus_rtu_register_format,
1804 mbus_register_format,
1807 /* Modbus RTU Preference - Register addressing format, this allows for a configurable display option to determine addressing used */
1808 prefs_register_enum_preference(mbrtu_module, "mbus_register_addr_type",
1809 "Register Addressing Type",
1810 "Register Addressing Type (Raw, Modicon 5 or 6). This option has no effect on the underlying protocol, but changes the register address display format",
1811 &global_mbus_rtu_register_addr_type,
1812 mbus_register_addr_type,
1818 /* If this dissector uses sub-dissector registration add a registration routine.
1819 This format is required because a script is used to find these routines and
1820 create the code that calls these routines.
1823 proto_reg_handoff_mbtcp(void)
1825 static int mbtcp_prefs_initialized = FALSE;
1826 static dissector_handle_t mbtcp_handle;
1827 static unsigned int mbtcp_port;
1829 /* Make sure to use Modbus/TCP Preferences field to determine default TCP port */
1830 if (! mbtcp_prefs_initialized) {
1831 mbtcp_handle = new_create_dissector_handle(dissect_mbtcp, proto_mbtcp);
1832 mbtcp_prefs_initialized = TRUE;
1835 if(mbtcp_port != 0 && mbtcp_port != global_mbus_tcp_port){
1836 dissector_delete_uint("tcp.port", mbtcp_port, mbtcp_handle);
1839 if(global_mbus_tcp_port != 0 && mbtcp_port != global_mbus_tcp_port) {
1840 dissector_add_uint("tcp.port", global_mbus_tcp_port, mbtcp_handle);
1843 mbtcp_port = global_mbus_tcp_port;
1845 modbus_handle = new_create_dissector_handle(dissect_modbus, proto_modbus);
1846 dissector_add_uint("mbtcp.prot_id", MODBUS_PROTOCOL_ID, modbus_handle);
1851 proto_reg_handoff_mbrtu(void)
1853 static int mbrtu_prefs_initialized = FALSE;
1854 static dissector_handle_t mbrtu_handle;
1855 static unsigned int mbrtu_port = 0;
1857 /* Make sure to use Modbus RTU Preferences field to determine default TCP port */
1858 if (! mbrtu_prefs_initialized) {
1859 mbrtu_handle = new_create_dissector_handle(dissect_mbrtu, proto_mbrtu);
1860 mbrtu_prefs_initialized = TRUE;
1863 if(mbrtu_port != 0 && mbrtu_port != global_mbus_rtu_port){
1864 dissector_delete_uint("tcp.port", mbrtu_port, mbrtu_handle);
1867 if(global_mbus_rtu_port != 0 && mbrtu_port != global_mbus_rtu_port) {
1868 dissector_add_uint("tcp.port", global_mbus_rtu_port, mbrtu_handle);
1871 mbrtu_port = global_mbus_rtu_port;
1873 modbus_handle = new_create_dissector_handle(dissect_modbus, proto_modbus);
1874 dissector_add_uint("mbtcp.prot_id", MODBUS_PROTOCOL_ID, modbus_handle);