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 *****************************************************************************************************
55 * Wireshark - Network traffic analyzer
56 * By Gerald Combs <gerald@wireshark.org>
57 * Copyright 1998 Gerald Combs
59 * This program is free software; you can redistribute it and/or
60 * modify it under the terms of the GNU General Public License
61 * as published by the Free Software Foundation; either version 2
62 * of the License, or (at your option) any later version.
64 * This program is distributed in the hope that it will be useful,
65 * but WITHOUT ANY WARRANTY; without even the implied warranty of
66 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
67 * GNU General Public License for more details.
69 * You should have received a copy of the GNU General Public License
70 * along with this program; if not, write to the Free Software
71 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
76 #include <epan/packet.h>
77 #include <epan/wmem/wmem.h>
78 #include "packet-tcp.h"
79 #include "packet-mbtcp.h"
80 #include <epan/prefs.h>
81 #include <epan/expert.h>
82 #include <epan/crc16-tvb.h> /* For CRC verification */
84 void proto_register_modbus(void);
85 void proto_reg_handoff_mbtcp(void);
86 void proto_reg_handoff_mbrtu(void);
88 /* Initialize the protocol and registered fields */
89 static int proto_mbtcp = -1;
90 static int proto_mbrtu = -1;
91 static int proto_modbus = -1;
92 static int hf_mbtcp_transid = -1;
93 static int hf_mbtcp_protid = -1;
94 static int hf_mbtcp_len = -1;
95 static int hf_mbtcp_unitid = -1;
96 static int hf_mbtcp_functioncode = -1;
97 static int hf_modbus_reference = -1;
98 static int hf_modbus_lreference = -1;
99 static int hf_modbus_reftype = -1;
100 static int hf_modbus_readref = -1;
101 static int hf_modbus_writeref = -1;
102 static int hf_modbus_wordcnt = -1;
103 static int hf_modbus_readwordcnt = -1;
104 static int hf_modbus_writewordcnt = -1;
105 static int hf_modbus_bytecnt = -1;
106 static int hf_modbus_lbytecnt = -1;
107 static int hf_modbus_bitcnt = -1;
108 static int hf_modbus_exceptioncode = -1;
109 static int hf_modbus_diag_sf = -1;
110 static int hf_modbus_diag_return_query_data_request = -1;
111 static int hf_modbus_diag_return_query_data_echo = -1;
112 static int hf_modbus_diag_restart_communication_option = -1;
113 static int hf_modbus_diag_return_diag_register = -1;
114 static int hf_modbus_diag_ascii_input_delimiter = -1;
115 static int hf_modbus_diag_return_bus_message_count = -1;
116 static int hf_modbus_diag_return_bus_comm_error_count = -1;
117 static int hf_modbus_diag_return_bus_exception_error_count = -1;
118 static int hf_modbus_diag_return_slave_message_count = -1;
119 static int hf_modbus_diag_return_no_slave_response_count = -1;
120 static int hf_modbus_diag_return_slave_nak_count = -1;
121 static int hf_modbus_diag_return_slave_busy_count = -1;
122 static int hf_modbus_diag_return_bus_char_overrun_count = -1;
123 static int hf_modbus_status = -1;
124 static int hf_modbus_event_count = -1;
125 static int hf_modbus_message_count = -1;
126 static int hf_modbus_event_recv_comm_err = -1;
127 static int hf_modbus_event_recv_char_over = -1;
128 static int hf_modbus_event_recv_lo_mode = -1;
129 static int hf_modbus_event_recv_broadcast = -1;
130 static int hf_modbus_event_send_read_ex = -1;
131 static int hf_modbus_event_send_slave_abort_ex = -1;
132 static int hf_modbus_event_send_slave_busy_ex = -1;
133 static int hf_modbus_event_send_slave_nak_ex = -1;
134 static int hf_modbus_event_send_write_timeout = -1;
135 static int hf_modbus_event_send_lo_mode = -1;
136 static int hf_modbus_andmask = -1;
137 static int hf_modbus_ormask = -1;
138 static int hf_modbus_data = -1;
139 static int hf_modbus_mei = -1;
140 static int hf_modbus_read_device_id = -1;
141 static int hf_modbus_object_id = -1;
142 static int hf_modbus_num_objects = -1;
143 static int hf_modbus_list_object_len = -1;
144 static int hf_modbus_conformity_level = -1;
145 static int hf_modbus_more_follows = -1;
146 static int hf_modbus_next_object_id = -1;
147 static int hf_modbus_object_str_value = -1;
148 static int hf_modbus_reg_uint16 = -1;
149 static int hf_modbus_reg_uint32 = -1;
150 static int hf_modbus_reg_ieee_float = -1;
151 static int hf_modbus_reg_modicon_float = -1;
152 static int hf_mbrtu_unitid = -1;
153 static int hf_mbrtu_crc16 = -1;
155 /* Initialize the subtree pointers */
156 static gint ett_mbtcp = -1;
157 static gint ett_mbrtu = -1;
158 static gint ett_modbus_hdr = -1;
159 static gint ett_group_hdr = -1;
160 static gint ett_events = -1;
161 static gint ett_events_recv = -1;
162 static gint ett_events_send = -1;
163 static gint ett_device_id_objects = -1;
164 static gint ett_device_id_object_items = -1;
166 static expert_field ei_mbrtu_crc16_incorrect = EI_INIT;
167 static expert_field ei_modbus_data_decode = EI_INIT;
169 static dissector_handle_t modbus_handle;
170 static dissector_handle_t mbtcp_handle;
171 static dissector_handle_t mbrtu_handle;
173 static dissector_table_t modbus_data_dissector_table;
174 static dissector_table_t modbus_dissector_table;
177 /* Globals for Modbus/TCP Preferences */
178 static gboolean mbtcp_desegment = TRUE;
179 static guint global_mbus_tcp_port = PORT_MBTCP; /* Port 502, by default */
180 static gint global_mbus_tcp_register_format = MBTCP_PREF_REGISTER_FORMAT_UINT16;
181 static gint global_mbus_tcp_register_addr_type = MBTCP_PREF_REGISTER_ADDR_RAW;
183 /* Globals for Modbus RTU over TCP Preferences */
184 static gboolean mbrtu_desegment = TRUE;
185 static guint global_mbus_rtu_port = PORT_MBRTU; /* 0, by default */
186 static gint global_mbus_rtu_register_format = MBTCP_PREF_REGISTER_FORMAT_UINT16;
187 static gint global_mbus_rtu_register_addr_type = MBTCP_PREF_REGISTER_ADDR_RAW;
188 static gboolean mbrtu_crc = FALSE;
191 classify_mbtcp_packet(packet_info *pinfo)
193 /* see if nature of packets can be derived from src/dst ports */
194 /* if so, return as found */
196 /* XXX Update Oct 2012 - It can be difficult to determine if a packet is a query or response; some way to track */
197 /* the Modbus/TCP transaction ID for each pair of messages would allow for detection based on a new seq. number. */
198 /* Otherwise, we can stick with this method; a configurable port option has been added to allow for usage of */
199 /* user ports either than the default of 502. */
200 if (( pinfo->srcport == global_mbus_tcp_port ) && ( pinfo->destport != global_mbus_tcp_port ))
201 return RESPONSE_PACKET;
202 if (( pinfo->srcport != global_mbus_tcp_port ) && ( pinfo->destport == global_mbus_tcp_port ))
205 /* else, cannot classify */
206 return CANNOT_CLASSIFY;
210 classify_mbrtu_packet(packet_info *pinfo, tvbuff_t *tvb)
212 /* see if nature of packets can be derived from src/dst ports */
213 /* if so, return as found */
214 if (( pinfo->srcport == global_mbus_rtu_port ) && ( pinfo->destport != global_mbus_rtu_port ))
215 return RESPONSE_PACKET;
216 if (( pinfo->srcport != global_mbus_rtu_port ) && ( pinfo->destport == global_mbus_rtu_port ))
219 /* Special case for serial-captured packets that don't have an Ethernet header */
220 /* Dig into these a little deeper to try to guess the message type */
221 if (!pinfo->srcport) {
222 /* If length is 8, this is either a query or very short response */
223 if (tvb_length(tvb) == 8) {
224 /* Only possible to get a response message of 8 bytes with Discrete or Coils */
225 if ((tvb_get_guint8(tvb, 1) == READ_COILS) || (tvb_get_guint8(tvb, 1) == READ_DISCRETE_INPUTS)) {
226 /* If this is, in fact, a response then the data byte count will be 3 */
227 /* This will correctly identify all messages except for those that are discrete or coil polls */
228 /* where the base address range happens to have 0x03 in the upper 16-bit address register */
229 if (tvb_get_guint8(tvb, 2) == 3) {
230 return RESPONSE_PACKET;
241 return RESPONSE_PACKET;
245 /* else, cannot classify */
246 return CANNOT_CLASSIFY;
249 /* Translate function to string, as given on p6 of
250 * "Open Modbus/TCP Specification", release 1 by Andy Swales.
252 static const value_string function_code_vals[] = {
253 { READ_COILS, "Read Coils" },
254 { READ_DISCRETE_INPUTS, "Read Discrete Inputs" },
255 { READ_HOLDING_REGS, "Read Holding Registers" },
256 { READ_INPUT_REGS, "Read Input Registers" },
257 { WRITE_SINGLE_COIL, "Write Single Coil" },
258 { WRITE_SINGLE_REG, "Write Single Register" },
259 { READ_EXCEPT_STAT, "Read Exception Status" },
260 { DIAGNOSTICS, "Diagnostics" },
261 { GET_COMM_EVENT_CTRS, "Get Comm. Event Counters" },
262 { GET_COMM_EVENT_LOG, "Get Comm. Event Log" },
263 { WRITE_MULT_COILS, "Write Multiple Coils" },
264 { WRITE_MULT_REGS, "Write Multiple Registers" },
265 { REPORT_SLAVE_ID, "Report Slave ID" },
266 { READ_FILE_RECORD, "Read File Record" },
267 { WRITE_FILE_RECORD, "Write File Record" },
268 { MASK_WRITE_REG, "Mask Write Register" },
269 { READ_WRITE_REG, "Read Write Register" },
270 { READ_FIFO_QUEUE, "Read FIFO Queue" },
271 { ENCAP_INTERFACE_TRANSP, "Encapsulated Interface Transport" },
275 /* Translate exception code to string */
276 static const value_string exception_code_vals[] = {
277 { ILLEGAL_FUNCTION, "Illegal function" },
278 { ILLEGAL_ADDRESS, "Illegal data address" },
279 { ILLEGAL_VALUE, "Illegal data value" },
280 { SLAVE_FAILURE, "Slave device failure" },
281 { ACKNOWLEDGE, "Acknowledge" },
282 { SLAVE_BUSY, "Slave device busy" },
283 { MEMORY_ERR, "Memory parity error" },
284 { GATEWAY_UNAVAILABLE, "Gateway path unavailable" },
285 { GATEWAY_TRGT_FAIL, "Gateway target device failed to respond" },
289 /* Translate Modbus Encapsulation Interface (MEI) code to string */
290 static const value_string encap_interface_code_vals[] = {
291 { CANOPEN_REQ_RESP, "CANopen Request/Response " },
292 { READ_DEVICE_ID, "Read Device Identification" },
296 /* Translate Modbus Diagnostic subfunction code to string */
297 static const value_string diagnostic_code_vals[] = {
298 { RETURN_QUERY_DATA, "Return Query Data" },
299 { RESTART_COMMUNICATION_OPTION, "Restart Communications Option" },
300 { RETURN_DIAGNOSTIC_REGISTER, "Return Diagnostic Register" },
301 { CHANGE_ASCII_INPUT_DELIMITER, "Change ASCII Input Delimiter" },
302 { FORCE_LISTEN_ONLY_MODE, "Force Listen Only Mode" },
303 { CLEAR_COUNTERS_AND_DIAG_REG, "Clear Counters and Diagnostic Register" },
304 { RETURN_BUS_MESSAGE_COUNT, "Return Bus Message Count" },
305 { RETURN_BUS_COMM_ERROR_COUNT, "Return Bus Communication Error Count" },
306 { RETURN_BUS_EXCEPTION_ERROR_COUNT, "Return Bus Exception Error Count" },
307 { RETURN_SLAVE_MESSAGE_COUNT, "Return Slave Message Count" },
308 { RETURN_SLAVE_NO_RESPONSE_COUNT, "Return Slave No Response Count" },
309 { RETURN_SLAVE_NAK_COUNT, "Return Slave NAK Count" },
310 { RETURN_SLAVE_BUSY_COUNT, "Return Slave Busy Count" },
311 { RETURN_BUS_CHAR_OVERRUN_COUNT, "Return Bus Character Overrun Count" },
312 { CLEAR_OVERRUN_COUNTER_AND_FLAG, "Clear Overrun Counter and Flag" },
316 static const value_string diagnostic_restart_communication_option_vals[] = {
318 { 0xFF, "Clear Log" },
322 /* Translate read device code to string */
323 static const value_string read_device_id_vals[] = {
324 { 1, "Basic Device Identification" },
325 { 2, "Regular Device Identification" },
326 { 3, "Extended Device Identification" },
327 { 4, "Specific Identification Object" },
332 /* Translate read device code to string */
333 static const value_string object_id_vals[] = {
335 { 1, "ProductCode" },
336 { 2, "MajorMinorRevision" },
338 { 4, "ProductName" },
340 { 6, "UserApplicationName" },
345 static const value_string conformity_level_vals[] = {
346 { 0x01, "Basic Device Identification (stream)" },
347 { 0x02, "Regular Device Identification (stream)" },
348 { 0x03, "Extended Device Identification (stream)" },
349 { 0x81, "Basic Device Identification (stream and individual)" },
350 { 0x82, "Regular Device Identification (stream and individual)" },
351 { 0x83, "Extended Device Identification (stream and individual)" },
356 static const enum_val_t mbus_register_format[] = {
357 { "UINT16 ", "UINT16 ", MBTCP_PREF_REGISTER_FORMAT_UINT16 },
358 { "UINT32 ", "UINT32 ", MBTCP_PREF_REGISTER_FORMAT_UINT32 },
359 { "IEEE FLT ", "IEEE FLT ", MBTCP_PREF_REGISTER_FORMAT_IEEE_FLOAT },
360 { "MODICON FLT", "MODICON FLT", MBTCP_PREF_REGISTER_FORMAT_MODICON_FLOAT },
364 static const enum_val_t mbus_register_addr_type[] = {
365 { "RAW ", "RAW ", MBTCP_PREF_REGISTER_ADDR_RAW },
366 { "MODICON 5-DIGIT", "MODICON 5-DIGIT", MBTCP_PREF_REGISTER_ADDR_MOD5 },
367 { "MODICON 6-DIGIT", "MODICON 6-DIGIT", MBTCP_PREF_REGISTER_ADDR_MOD6 },
371 /* Code to dissect Modbus/TCP packets */
373 dissect_mbtcp_pdu(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, void* data _U_)
375 /* Set up structures needed to add the protocol subtree and manage it */
377 proto_tree *mbtcp_tree;
378 int offset, packet_type;
380 const char *func_string = "";
381 const char *pkt_type_str = "";
382 const char *err_str = "";
383 guint16 transaction_id, protocol_id, len;
384 guint8 unit_id, function_code, exception_code, subfunction_code;
385 void *p_save_proto_data;
386 modbus_request_info_t *request_info;
388 /* Make entries in Protocol column on summary display */
389 col_set_str(pinfo->cinfo, COL_PROTOCOL, "Modbus/TCP");
390 col_clear(pinfo->cinfo, COL_INFO);
392 transaction_id = tvb_get_ntohs(tvb, 0);
393 protocol_id = tvb_get_ntohs(tvb, 2);
394 len = tvb_get_ntohs(tvb, 4);
396 unit_id = tvb_get_guint8(tvb, 6);
397 function_code = tvb_get_guint8(tvb, 7) & 0x7F;
399 /* Make entries in Info column on summary display */
402 /* Find exception - last bit set in function code */
403 if (tvb_get_guint8(tvb, 7) & 0x80) {
404 exception_code = tvb_get_guint8(tvb, offset + 8);
410 if ((function_code == ENCAP_INTERFACE_TRANSP) && (exception_code == 0)) {
411 func_string = val_to_str_const(tvb_get_guint8(tvb, offset + 8), encap_interface_code_vals, "Encapsulated Interface Transport");
412 subfunction_code = 1;
414 else if ((function_code == DIAGNOSTICS) && (exception_code == 0)) {
415 func_string = val_to_str_const(tvb_get_ntohs(tvb, offset + 8), diagnostic_code_vals, "Diagnostics");
416 subfunction_code = 1;
419 func_string = val_to_str(function_code, function_code_vals, "Unknown function (%d)");
420 subfunction_code = 0;
423 /* "Request" or "Response" */
424 packet_type = classify_mbtcp_packet(pinfo);
426 switch ( packet_type ) {
428 pkt_type_str="Query";
430 case RESPONSE_PACKET :
431 pkt_type_str="Response";
433 case CANNOT_CLASSIFY :
434 err_str="Unable to classify as query or response.";
435 pkt_type_str="unknown";
440 if ( exception_code != 0 )
441 err_str="Exception returned ";
443 if (subfunction_code == 0) {
444 if (strlen(err_str) > 0) {
445 col_add_fstr(pinfo->cinfo, COL_INFO,
446 "%8s: Trans: %5u; Unit: %3u, Func: %3u: %s. %s",
447 pkt_type_str, transaction_id, unit_id,
448 function_code, func_string, err_str);
451 col_add_fstr(pinfo->cinfo, COL_INFO,
452 "%8s: Trans: %5u; Unit: %3u, Func: %3u: %s",
453 pkt_type_str, transaction_id, unit_id,
454 function_code, func_string);
458 if (strlen(err_str) > 0) {
459 col_add_fstr(pinfo->cinfo, COL_INFO,
460 "%8s: Trans: %5u; Unit: %3u, Func: %3u/%3u: %s. %s",
461 pkt_type_str, transaction_id, unit_id,
462 function_code, subfunction_code, func_string, err_str);
465 col_add_fstr(pinfo->cinfo, COL_INFO,
466 "%8s: Trans: %5u; Unit: %3u, Func: %3u/%3u: %s",
467 pkt_type_str, transaction_id, unit_id,
468 function_code, subfunction_code, func_string);
472 mi = proto_tree_add_protocol_format(tree, proto_mbtcp, tvb, offset,
473 len+6, "Modbus/TCP");
474 mbtcp_tree = proto_item_add_subtree(mi, ett_mbtcp);
476 /* Add items to protocol tree specific to Modbus/TCP */
477 proto_tree_add_uint(mbtcp_tree, hf_mbtcp_transid, tvb, offset, 2, transaction_id);
478 proto_tree_add_uint(mbtcp_tree, hf_mbtcp_protid, tvb, offset + 2, 2, protocol_id);
479 proto_tree_add_uint(mbtcp_tree, hf_mbtcp_len, tvb, offset + 4, 2, len);
480 proto_tree_add_uint(mbtcp_tree, hf_mbtcp_unitid, tvb, offset + 6, 1, unit_id);
482 /* dissect the Modbus PDU */
483 next_tvb = tvb_new_subset( tvb, offset+7, len-1, len-1);
485 /* keep existing context */
486 p_save_proto_data = p_get_proto_data(wmem_file_scope(), pinfo, proto_modbus, 0 );
487 p_remove_proto_data(wmem_file_scope(), pinfo, proto_modbus, 0);
489 /* Create enough context for Modbus dissector */
490 request_info = wmem_new(wmem_packet_scope(), modbus_request_info_t);
491 request_info->packet_type = (guint8)packet_type;
492 request_info->register_addr_type = (guint8)global_mbus_tcp_register_addr_type;
493 request_info->register_format = (guint8)global_mbus_tcp_register_format;
494 p_add_proto_data(wmem_file_scope(), pinfo, proto_modbus, 0, request_info);
496 /* Continue with dissection of Modbus data payload following Modbus/TCP frame */
497 if( tvb_length_remaining(tvb, offset) > 0 )
498 call_dissector(modbus_handle, next_tvb, pinfo, tree);
500 p_remove_proto_data(wmem_file_scope(), pinfo, proto_modbus, 0);
501 p_add_proto_data(wmem_file_scope(), pinfo, proto_modbus, 0, p_save_proto_data);
502 return tvb_length(tvb);
505 /* Code to dissect Modbus RTU over TCP packets */
507 dissect_mbrtu_pdu(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, void* data _U_)
509 /* Set up structures needed to add the protocol subtree and manage it */
510 proto_item *mi, *crc_item;
511 proto_tree *mbrtu_tree;
512 gint offset, packet_type;
514 const char *func_string = "";
515 const char *pkt_type_str = "";
516 const char *err_str = "";
517 guint16 len, crc16, calc_crc16;
518 guint8 unit_id, function_code, exception_code, subfunction_code;
519 void *p_save_proto_data;
520 modbus_request_info_t *request_info;
522 /* Make entries in Protocol column on summary display */
523 col_set_str(pinfo->cinfo, COL_PROTOCOL, "Modbus RTU");
524 col_clear(pinfo->cinfo, COL_INFO);
526 len = tvb_length(tvb);
528 unit_id = tvb_get_guint8(tvb, 0);
529 function_code = tvb_get_guint8(tvb, 1) & 0x7F;
530 crc16 = tvb_get_ntohs(tvb, len-2);
532 /* Make entries in Info column on summary display */
535 /* Find exception - last bit set in function code */
536 if (tvb_get_guint8(tvb, 1) & 0x80) {
537 exception_code = tvb_get_guint8(tvb, offset + 2);
543 if ((function_code == ENCAP_INTERFACE_TRANSP) && (exception_code == 0)) {
544 func_string = val_to_str_const(tvb_get_guint8(tvb, offset + 2), encap_interface_code_vals, "Encapsulated Interface Transport");
545 subfunction_code = 1;
547 else if ((function_code == DIAGNOSTICS) && (exception_code == 0)) {
548 func_string = val_to_str_const(tvb_get_ntohs(tvb, offset + 2), diagnostic_code_vals, "Diagnostics");
549 subfunction_code = 1;
552 func_string = val_to_str(function_code, function_code_vals, "Unknown function (%d)");
553 subfunction_code = 0;
556 /* "Request" or "Response" */
557 packet_type = classify_mbrtu_packet(pinfo, tvb);
559 switch ( packet_type ) {
561 pkt_type_str="Query";
563 case RESPONSE_PACKET :
564 pkt_type_str="Response";
566 case CANNOT_CLASSIFY :
567 err_str="Unable to classify as query or response.";
568 pkt_type_str="unknown";
573 if ( exception_code != 0 )
574 err_str="Exception returned ";
576 if (subfunction_code == 0) {
577 if (strlen(err_str) > 0) {
578 col_add_fstr(pinfo->cinfo, COL_INFO,
579 "%8s: Unit: %3u, Func: %3u: %s. %s",
580 pkt_type_str, unit_id,
581 function_code, func_string, err_str);
584 col_add_fstr(pinfo->cinfo, COL_INFO,
585 "%8s: Unit: %3u, Func: %3u: %s",
586 pkt_type_str, unit_id,
587 function_code, func_string);
591 if (strlen(err_str) > 0) {
592 col_add_fstr(pinfo->cinfo, COL_INFO,
593 "%8s: Unit: %3u, Func: %3u/%3u: %s. %s",
594 pkt_type_str, unit_id,
595 function_code, subfunction_code, func_string, err_str);
598 col_add_fstr(pinfo->cinfo, COL_INFO,
599 "%8s: Unit: %3u, Func: %3u/%3u: %s",
600 pkt_type_str, unit_id,
601 function_code, subfunction_code, func_string);
605 mi = proto_tree_add_protocol_format(tree, proto_mbrtu, tvb, offset,
607 mbrtu_tree = proto_item_add_subtree(mi, ett_mbrtu);
609 /* Add items to protocol tree specific to Modbus RTU */
610 proto_tree_add_uint(mbrtu_tree, hf_mbrtu_unitid, tvb, offset, 1, unit_id);
611 crc_item = proto_tree_add_uint(mbrtu_tree, hf_mbrtu_crc16, tvb, len-2, 2, crc16);
616 calc_crc16 = crc16_plain_tvb_offset_seed(tvb, offset, len-2, 0xFFFF);
617 if (g_htons(calc_crc16) != crc16)
618 expert_add_info_format(pinfo, crc_item, &ei_mbrtu_crc16_incorrect, "Incorrect CRC - should be 0x%04x", g_htons(calc_crc16));
621 /* make sure to ignore the CRC-16 footer bytes */
624 /* dissect the Modbus PDU */
625 next_tvb = tvb_new_subset( tvb, offset+1, len-1, len-1);
627 /* keep existing context */
628 p_save_proto_data = p_get_proto_data(wmem_file_scope(), pinfo, proto_modbus, 0 );
629 p_remove_proto_data(wmem_file_scope(), pinfo, proto_modbus, 0);
631 /* Create enough context for Modbus dissector */
632 request_info = wmem_new(wmem_packet_scope(), modbus_request_info_t);
633 request_info->packet_type = (guint8)packet_type;
634 request_info->register_addr_type = (guint8)global_mbus_rtu_register_addr_type;
635 request_info->register_format = (guint8)global_mbus_rtu_register_format;
636 p_add_proto_data(wmem_file_scope(), pinfo, proto_modbus, 0, request_info);
638 /* Continue with dissection of Modbus data payload following Modbus RTU frame */
639 if( tvb_length_remaining(tvb, offset) > 0 )
640 call_dissector(modbus_handle, next_tvb, pinfo, tree);
642 p_remove_proto_data(wmem_file_scope(), pinfo, proto_modbus, 0);
643 p_add_proto_data(wmem_file_scope(), pinfo, proto_modbus, 0, p_save_proto_data);
644 return tvb_length(tvb);
648 /* Return length of Modbus/TCP message */
650 get_mbtcp_pdu_len(packet_info *pinfo _U_, tvbuff_t *tvb, int offset)
655 * Get the length of the data from the encapsulation header.
657 plen = tvb_get_ntohs(tvb, offset + 4);
660 * That length doesn't include the encapsulation header itself;
666 /* Return length of Modbus RTU over TCP message */
668 get_mbrtu_pdu_len(packet_info *pinfo _U_, tvbuff_t *tvb, int offset _U_)
671 /* Modbus/TCP frames include a "length" word in each message; Modbus RTU over TCP does not, so don't attempt to get one */
672 return tvb_length(tvb);
676 /* Code to dissect Modbus/TCP messages */
678 dissect_mbtcp(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, void *data)
681 /* Make sure there's at least enough data to determine it's a Modbus TCP packet */
682 if (!tvb_bytes_exist(tvb, 0, 8))
685 /* check that it actually looks like Modbus/TCP */
686 /* protocol id == 0 */
687 if(tvb_get_ntohs(tvb, 2) != 0 ){
690 /* length is at least 2 (unit_id + function_code) */
691 if(tvb_get_ntohs(tvb, 4) < 2 ){
695 /* build up protocol tree and iterate over multiple packets */
696 tcp_dissect_pdus(tvb, pinfo, tree, mbtcp_desegment, 6,
697 get_mbtcp_pdu_len, dissect_mbtcp_pdu, data);
699 return tvb_length(tvb);
702 /* Code to dissect Modbus RTU over TCP messages */
704 dissect_mbrtu(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, void *data)
707 /* Make sure there's at least enough data to determine it's a Modbus packet */
708 if (!tvb_bytes_exist(tvb, 0, 6))
711 /* For Modbus RTU mode, confirm that the first byte is a valid address (non-zero), */
712 /* so we can eliminate false-posititves on Modbus TCP messages loaded as RTU */
713 if(tvb_get_guint8(tvb, 0) == 0 )
716 /* build up protocol tree and iterate over multiple packets */
717 tcp_dissect_pdus(tvb, pinfo, tree, mbrtu_desegment, 6,
718 get_mbrtu_pdu_len, dissect_mbrtu_pdu, data);
720 return tvb_length(tvb);
724 /* Code to allow further dissection of Modbus data payload */
725 /* Common to both Modbus/TCP and Modbus RTU dissectors */
727 dissect_modbus_data(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, guint8 function_code,
728 gint payload_start, gint payload_len, guint8 register_format)
730 gint reported_len, data_offset, reg_num = 0;
731 guint16 data16, modflt_lo, modflt_hi;
732 guint32 data32, modflt_comb;
733 gfloat data_float, modfloat;
734 proto_item *register_item = NULL;
737 reported_len = tvb_reported_length_remaining(tvb, payload_start);
740 if ( payload_start < 0 || ( payload_len + payload_start ) == 0 )
743 /* If calculated length from remaining tvb data != bytes in packet, do not attempt to decode */
744 if ( payload_len != reported_len ) {
745 proto_tree_add_item(tree, hf_modbus_data, tvb, payload_start, reported_len, ENC_NA);
749 /* If data type of payload is Holding or Input registers */
751 /* if payload length is not a multiple of 4, don't attempt to decode anything in 32-bit format */
752 if ((function_code == READ_HOLDING_REGS) || (function_code == READ_INPUT_REGS) || (function_code == WRITE_MULT_REGS)) {
753 if ((payload_len % 4 != 0) && ( (register_format == MBTCP_PREF_REGISTER_FORMAT_UINT32) ||
754 (register_format == MBTCP_PREF_REGISTER_FORMAT_IEEE_FLOAT) ||
755 (register_format == MBTCP_PREF_REGISTER_FORMAT_MODICON_FLOAT) ) ) {
756 register_item = proto_tree_add_item(tree, hf_modbus_data, tvb, payload_start, payload_len, ENC_NA);
757 expert_add_info(pinfo, register_item, &ei_modbus_data_decode);
762 /* Build a new tvb containing just the data payload */
763 next_tvb = tvb_new_subset(tvb, payload_start, payload_len, reported_len);
765 switch ( function_code ) {
767 case READ_HOLDING_REGS:
768 case READ_INPUT_REGS:
769 case WRITE_MULT_REGS:
770 while (data_offset < payload_len) {
771 /* Use "Preferences" options to determine decoding format of register data, as no format is implied by the protocol itself. */
772 /* Based on a standard register size of 16-bits, use decoding format preference to step through each register and display */
773 /* it in an appropriate fashion. */
774 switch (register_format) {
775 case MBTCP_PREF_REGISTER_FORMAT_UINT16: /* Standard-size unsigned integer 16-bit register */
776 data16 = tvb_get_ntohs(next_tvb, data_offset);
777 register_item = proto_tree_add_uint(tree, hf_modbus_reg_uint16, next_tvb, data_offset, 2, data16);
778 proto_item_set_text(register_item, "Register %u (UINT16): %u", reg_num, data16);
783 case MBTCP_PREF_REGISTER_FORMAT_UINT32: /* Double-size unsigned integer 2 x 16-bit registers */
784 data32 = tvb_get_ntohl(next_tvb, data_offset);
785 register_item = proto_tree_add_uint(tree, hf_modbus_reg_uint32, next_tvb, data_offset, 4, data32);
786 proto_item_set_text(register_item, "Register %u (UINT32): %u", reg_num, data32);
791 case MBTCP_PREF_REGISTER_FORMAT_IEEE_FLOAT: /* IEEE Floating Point, 2 x 16-bit registers */
792 data_float = tvb_get_ntohieee_float(next_tvb, data_offset);
793 register_item = proto_tree_add_float(tree, hf_modbus_reg_ieee_float, next_tvb, data_offset, 4, data_float);
794 proto_item_set_text(register_item, "Register %u (IEEE Float): %f", reg_num, data_float);
799 case MBTCP_PREF_REGISTER_FORMAT_MODICON_FLOAT: /* Modicon Floating Point (word-swap), 2 x 16-bit registers */
800 /* Modicon-style Floating Point values are stored in reverse-word order. */
801 /* ie: a standard IEEE float value 59.991459 is equal to 0x426ff741 */
802 /* while the Modicon equivalent to this value is 0xf741426f */
803 /* To re-assemble a proper IEEE float, we must retrieve the 2 x 16-bit words, bit-shift the */
804 /* "hi" component by 16-bits and then OR them together into a combined 32-bit int. */
805 /* Following that operation, use some memcpy magic to copy the 4 raw data bytes from the */
806 /* 32-bit integer into a standard float. Not sure if there is a cleaner way possible using */
807 /* the Wireshark libaries, but this seems to work OK. */
809 modflt_lo = tvb_get_ntohs(next_tvb, data_offset);
810 modflt_hi = tvb_get_ntohs(next_tvb, data_offset+2);
811 modflt_comb = (guint32)(modflt_hi<<16) | modflt_lo;
812 memcpy(&modfloat, &modflt_comb, 4);
814 register_item = proto_tree_add_float(tree, hf_modbus_reg_modicon_float, next_tvb, data_offset, 4, modfloat);
815 proto_item_set_text(register_item, "Register %u (Modicon Float): %f", reg_num, modfloat);
821 /* Avoid any chance of an infinite loop */
822 data_offset = payload_len;
824 } /* register format switch */
831 if ( ! dissector_try_string(modbus_data_dissector_table, "data", next_tvb, pinfo, tree, NULL) )
832 proto_tree_add_item(tree, hf_modbus_data, tvb, payload_start, payload_len, ENC_NA);
837 /* Code to actually dissect the packets */
839 dissect_modbus(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, void *data _U_)
841 proto_tree *modbus_tree, *group_tree, *event_tree,
842 *event_item_tree, *device_objects_tree,
843 *device_objects_item_tree;
844 proto_item *mi, *mf, *me, *mei, *doe, *doie;
845 int offset = 0, group_offset;
846 gint payload_start, payload_len, event_index,
847 ii, byte_cnt, len, num_objects, object_index,
849 guint32 group_byte_cnt, group_word_cnt;
850 guint8 function_code, exception_code, mei_code, event_code, object_type;
851 guint8 packet_type, register_format; /*register_addr_type*/
852 guint16 diagnostic_code;
853 modbus_request_info_t *request_info;
855 len = tvb_length_remaining(tvb, 0);
857 /* If the packet is zero-length, we should not attempt to dissect any further */
861 function_code = tvb_get_guint8(tvb, offset) & 0x7F;
863 /* Find exception - last bit set in function code */
864 if (tvb_get_guint8(tvb, offset) & 0x80 ) {
865 exception_code = tvb_get_guint8(tvb, offset+1);
871 /* See if we have any context */
872 request_info = (modbus_request_info_t *)p_get_proto_data(wmem_file_scope(), pinfo, proto_modbus, 0 );
873 if (request_info != NULL)
875 packet_type = request_info->packet_type;
876 register_format = request_info->register_format;
877 /*register_addr_type = request_info->register_addr_type;*/
881 /* Default to a response packet to at least attempt to decode a good chunk of data */
882 packet_type = RESPONSE_PACKET;
883 register_format = MBTCP_PREF_REGISTER_FORMAT_UINT16;
884 /* register_addr_type = MBTCP_PREF_REGISTER_ADDR_RAW;*/
887 /* Add items to protocol tree specific to Modbus generic */
888 mf = proto_tree_add_text(tree, tvb, offset, len, "Modbus");
889 modbus_tree = proto_item_add_subtree(mf, ett_modbus_hdr);
891 mi = proto_tree_add_uint(modbus_tree, hf_mbtcp_functioncode, tvb, offset, 1,
894 payload_start = offset + 1;
895 payload_len = len - 1;
896 if (exception_code != 0) {
897 proto_item_set_text(mi, "Function %u: %s. Exception: %s",
899 val_to_str_const(function_code, function_code_vals, "Unknown Function"),
900 val_to_str(exception_code,
902 "Unknown Exception Code (%u)"));
903 proto_tree_add_uint(modbus_tree, hf_modbus_exceptioncode, tvb, payload_start, 1,
907 switch (function_code) {
910 case READ_DISCRETE_INPUTS:
912 if (packet_type == QUERY_PACKET) {
913 proto_tree_add_item(modbus_tree, hf_modbus_reference, tvb, payload_start, 2, ENC_BIG_ENDIAN);
914 proto_tree_add_item(modbus_tree, hf_modbus_bitcnt, tvb, payload_start + 2, 2, ENC_BIG_ENDIAN);
916 else if (packet_type == RESPONSE_PACKET) {
917 byte_cnt = (guint32)tvb_get_guint8(tvb, payload_start);
918 proto_tree_add_uint(modbus_tree, hf_modbus_bytecnt, tvb, payload_start, 1, byte_cnt);
919 dissect_modbus_data(tvb, pinfo, modbus_tree, function_code, payload_start + 1, byte_cnt, register_format);
923 case READ_HOLDING_REGS:
924 case READ_INPUT_REGS:
925 if (packet_type == QUERY_PACKET) {
926 proto_tree_add_item(modbus_tree, hf_modbus_reference, tvb, payload_start, 2, ENC_BIG_ENDIAN);
927 proto_tree_add_item(modbus_tree, hf_modbus_wordcnt, tvb, payload_start + 2, 2, ENC_BIG_ENDIAN);
929 else if (packet_type == RESPONSE_PACKET) {
930 byte_cnt = (guint32)tvb_get_guint8(tvb, payload_start);
931 proto_tree_add_uint(modbus_tree, hf_modbus_bytecnt, tvb, payload_start, 1, byte_cnt);
932 dissect_modbus_data(tvb, pinfo, modbus_tree, function_code, payload_start + 1, byte_cnt, register_format);
936 case WRITE_SINGLE_COIL:
937 if (packet_type == QUERY_PACKET) {
938 proto_tree_add_item(modbus_tree, hf_modbus_reference, tvb, payload_start, 2, ENC_BIG_ENDIAN);
939 dissect_modbus_data(tvb, pinfo, modbus_tree, function_code, payload_start + 2, 1, register_format);
940 proto_tree_add_text(modbus_tree, tvb, payload_start + 3, 1, "Padding");
942 else if (packet_type == RESPONSE_PACKET) {
943 proto_tree_add_item(modbus_tree, hf_modbus_reference, tvb, payload_start, 2, ENC_BIG_ENDIAN);
944 dissect_modbus_data(tvb, pinfo, modbus_tree, function_code, payload_start + 2, 1, register_format);
945 proto_tree_add_text(modbus_tree, tvb, payload_start + 3, 1, "Padding");
949 case WRITE_SINGLE_REG:
950 if (packet_type == QUERY_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);
954 else if (packet_type == RESPONSE_PACKET) {
955 proto_tree_add_item(modbus_tree, hf_modbus_reference, tvb, payload_start, 2, ENC_BIG_ENDIAN);
956 dissect_modbus_data(tvb, pinfo, modbus_tree, function_code, payload_start + 2, 2, register_format);
960 case READ_EXCEPT_STAT:
961 if (packet_type == RESPONSE_PACKET)
962 dissect_modbus_data(tvb, pinfo, modbus_tree, function_code, payload_start, 1, register_format);
966 if ((packet_type == QUERY_PACKET) || (packet_type == RESPONSE_PACKET)) {
967 diagnostic_code = tvb_get_ntohs(tvb, payload_start);
968 proto_tree_add_uint(modbus_tree, hf_modbus_diag_sf, tvb, payload_start, 2, diagnostic_code);
969 switch(diagnostic_code)
971 case RETURN_QUERY_DATA:
972 if (packet_type == QUERY_PACKET) {
974 proto_tree_add_item(modbus_tree, hf_modbus_diag_return_query_data_request, tvb, payload_start+2, payload_len-2, ENC_NA);
976 else if (packet_type == RESPONSE_PACKET) {
978 proto_tree_add_item(modbus_tree, hf_modbus_diag_return_query_data_echo, tvb, payload_start+2, payload_len-2, ENC_NA);
981 case RESTART_COMMUNICATION_OPTION:
982 proto_tree_add_item(modbus_tree, hf_modbus_diag_restart_communication_option, tvb, payload_start+2, 2, ENC_BIG_ENDIAN);
984 case RETURN_DIAGNOSTIC_REGISTER:
985 if (packet_type == QUERY_PACKET) {
987 dissect_modbus_data(tvb, pinfo, modbus_tree, function_code, payload_start+2, payload_len-2, register_format);
989 else if (packet_type == RESPONSE_PACKET) {
990 proto_tree_add_item(modbus_tree, hf_modbus_diag_return_diag_register, tvb, payload_start+2, 2, ENC_BIG_ENDIAN);
993 case CHANGE_ASCII_INPUT_DELIMITER:
994 proto_tree_add_item(modbus_tree, hf_modbus_diag_ascii_input_delimiter, tvb, payload_start+2, 1, ENC_BIG_ENDIAN);
996 case RETURN_BUS_MESSAGE_COUNT:
997 if (packet_type == QUERY_PACKET) {
999 dissect_modbus_data(tvb, pinfo, modbus_tree, function_code, payload_start+2, payload_len-2, register_format);
1001 else if (packet_type == RESPONSE_PACKET) {
1002 proto_tree_add_item(modbus_tree, hf_modbus_diag_return_bus_message_count, tvb, payload_start+2, 2, ENC_BIG_ENDIAN);
1005 case RETURN_BUS_COMM_ERROR_COUNT:
1006 if (packet_type == QUERY_PACKET) {
1007 if (payload_len > 2)
1008 dissect_modbus_data(tvb, pinfo, modbus_tree, function_code, payload_start+2, payload_len-2, register_format);
1010 else if (packet_type == RESPONSE_PACKET) {
1011 proto_tree_add_item(modbus_tree, hf_modbus_diag_return_bus_comm_error_count, tvb, payload_start+2, 2, ENC_BIG_ENDIAN);
1014 case RETURN_BUS_EXCEPTION_ERROR_COUNT:
1015 if (packet_type == QUERY_PACKET) {
1016 if (payload_len > 2)
1017 dissect_modbus_data(tvb, pinfo, modbus_tree, function_code, payload_start+2, payload_len-2, register_format);
1019 else if (packet_type == RESPONSE_PACKET) {
1020 proto_tree_add_item(modbus_tree, hf_modbus_diag_return_bus_exception_error_count, tvb, payload_start+2, 2, ENC_BIG_ENDIAN);
1023 case RETURN_SLAVE_MESSAGE_COUNT:
1024 if (packet_type == QUERY_PACKET) {
1025 if (payload_len > 2)
1026 dissect_modbus_data(tvb, pinfo, modbus_tree, function_code, payload_start+2, payload_len-2, register_format);
1028 else if (packet_type == RESPONSE_PACKET) {
1029 proto_tree_add_item(modbus_tree, hf_modbus_diag_return_slave_message_count, tvb, payload_start+2, 2, ENC_BIG_ENDIAN);
1032 case RETURN_SLAVE_NO_RESPONSE_COUNT:
1033 if (packet_type == QUERY_PACKET) {
1034 if (payload_len > 2)
1035 dissect_modbus_data(tvb, pinfo, modbus_tree, function_code, payload_start+2, payload_len-2, register_format);
1037 else if (packet_type == RESPONSE_PACKET) {
1038 proto_tree_add_item(modbus_tree, hf_modbus_diag_return_no_slave_response_count, tvb, payload_start+2, 2, ENC_BIG_ENDIAN);
1041 case RETURN_SLAVE_NAK_COUNT:
1042 if (packet_type == QUERY_PACKET) {
1043 if (payload_len > 2)
1044 dissect_modbus_data(tvb, pinfo, modbus_tree, function_code, payload_start+2, payload_len-2, register_format);
1046 else if (packet_type == RESPONSE_PACKET) {
1047 proto_tree_add_item(modbus_tree, hf_modbus_diag_return_slave_nak_count, tvb, payload_start+2, 2, ENC_BIG_ENDIAN);
1050 case RETURN_SLAVE_BUSY_COUNT:
1051 if (packet_type == QUERY_PACKET) {
1052 if (payload_len > 2)
1053 dissect_modbus_data(tvb, pinfo, modbus_tree, function_code, payload_start+2, payload_len-2, register_format);
1055 else if (packet_type == RESPONSE_PACKET) {
1056 proto_tree_add_item(modbus_tree, hf_modbus_diag_return_slave_busy_count, tvb, payload_start+2, 2, ENC_BIG_ENDIAN);
1059 case RETURN_BUS_CHAR_OVERRUN_COUNT:
1060 if (packet_type == QUERY_PACKET) {
1061 if (payload_len > 2)
1062 dissect_modbus_data(tvb, pinfo, modbus_tree, function_code, payload_start+2, payload_len-2, register_format);
1064 else if (packet_type == RESPONSE_PACKET) {
1065 proto_tree_add_item(modbus_tree, hf_modbus_diag_return_bus_char_overrun_count, tvb, payload_start+2, 2, ENC_BIG_ENDIAN);
1068 case CLEAR_OVERRUN_COUNTER_AND_FLAG:
1069 case FORCE_LISTEN_ONLY_MODE:
1070 case CLEAR_COUNTERS_AND_DIAG_REG:
1072 if (payload_len > 2)
1073 dissect_modbus_data(tvb, pinfo, modbus_tree, function_code, payload_start+2, payload_len-2, register_format);
1079 case GET_COMM_EVENT_CTRS:
1080 if (packet_type == RESPONSE_PACKET) {
1081 proto_tree_add_item(modbus_tree, hf_modbus_status, tvb, payload_start, 2, ENC_BIG_ENDIAN);
1082 proto_tree_add_item(modbus_tree, hf_modbus_event_count, tvb, payload_start+2, 2, ENC_BIG_ENDIAN);
1086 case GET_COMM_EVENT_LOG:
1087 if (packet_type == RESPONSE_PACKET) {
1088 byte_cnt = (guint32)tvb_get_guint8(tvb, payload_start);
1089 proto_tree_add_uint(modbus_tree, hf_modbus_bytecnt, tvb, payload_start, 1, byte_cnt);
1090 proto_tree_add_item(modbus_tree, hf_modbus_status, tvb, payload_start+1, 2, ENC_BIG_ENDIAN);
1091 proto_tree_add_item(modbus_tree, hf_modbus_event_count, tvb, payload_start+3, 2, ENC_BIG_ENDIAN);
1092 proto_tree_add_item(modbus_tree, hf_modbus_message_count, tvb, payload_start+5, 2, ENC_BIG_ENDIAN);
1093 if (byte_cnt-6 > 0) {
1096 me = proto_tree_add_text(modbus_tree, tvb, payload_start+7, byte_cnt, "Events");
1097 event_tree = proto_item_add_subtree(me, ett_events);
1098 while (byte_cnt > 0) {
1099 event_code = tvb_get_guint8(tvb, payload_start+7+event_index);
1100 if (event_code == 0) {
1101 proto_tree_add_text(event_tree, tvb, payload_start+7+event_index, 1, "Initiated Communication Restart");
1103 else if (event_code == 4) {
1104 proto_tree_add_text(event_tree, tvb, payload_start+7+event_index, 1, "Entered Listen Only Mode");
1106 else if (event_code & REMOTE_DEVICE_RECV_EVENT_MASK) {
1107 mei = proto_tree_add_text(event_tree, tvb, payload_start+7+event_index, 1, "Receive Event: 0x%02X", event_code);
1108 event_item_tree = proto_item_add_subtree(mei, ett_events_recv);
1110 /* add subtrees to describe each event bit */
1111 proto_tree_add_item(event_item_tree, hf_modbus_event_recv_comm_err,
1112 tvb, payload_start+7+event_index, 1, ENC_LITTLE_ENDIAN );
1113 proto_tree_add_item(event_item_tree, hf_modbus_event_recv_char_over,
1114 tvb, payload_start+7+event_index, 1, ENC_LITTLE_ENDIAN );
1115 proto_tree_add_item(event_item_tree, hf_modbus_event_recv_lo_mode,
1116 tvb, payload_start+7+event_index, 1, ENC_LITTLE_ENDIAN );
1117 proto_tree_add_item(event_item_tree, hf_modbus_event_recv_broadcast,
1118 tvb, payload_start+7+event_index, 1, ENC_LITTLE_ENDIAN );
1120 else if ((event_code & REMOTE_DEVICE_SEND_EVENT_MASK) == REMOTE_DEVICE_SEND_EVENT_VALUE) {
1121 mei = proto_tree_add_text(event_tree, tvb, payload_start+7+event_index, 1, "Send Event: 0x%02X", event_code);
1122 event_item_tree = proto_item_add_subtree(mei, ett_events_send);
1124 /* add subtrees to describe each event bit */
1125 proto_tree_add_item(event_item_tree, hf_modbus_event_send_read_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_abort_ex,
1128 tvb, payload_start+7+event_index, 1, ENC_LITTLE_ENDIAN );
1129 proto_tree_add_item(event_item_tree, hf_modbus_event_send_slave_busy_ex,
1130 tvb, payload_start+7+event_index, 1, ENC_LITTLE_ENDIAN );
1131 proto_tree_add_item(event_item_tree, hf_modbus_event_send_slave_nak_ex,
1132 tvb, payload_start+7+event_index, 1, ENC_LITTLE_ENDIAN );
1133 proto_tree_add_item(event_item_tree, hf_modbus_event_send_write_timeout,
1134 tvb, payload_start+7+event_index, 1, ENC_LITTLE_ENDIAN );
1135 proto_tree_add_item(event_item_tree, hf_modbus_event_send_lo_mode,
1136 tvb, payload_start+7+event_index, 1, ENC_LITTLE_ENDIAN );
1139 proto_tree_add_text(event_tree, tvb, payload_start+7+event_index, 1, "Unknown Event");
1149 case WRITE_MULT_COILS:
1150 if (packet_type == QUERY_PACKET) {
1151 proto_tree_add_item(modbus_tree, hf_modbus_reference, tvb, payload_start, 2, ENC_BIG_ENDIAN);
1152 proto_tree_add_item(modbus_tree, hf_modbus_bitcnt, tvb, payload_start + 2, 2, ENC_BIG_ENDIAN);
1153 byte_cnt = (guint32)tvb_get_guint8(tvb, payload_start + 4);
1154 proto_tree_add_uint(modbus_tree, hf_modbus_bytecnt, tvb, payload_start + 4, 1,
1156 dissect_modbus_data(tvb, pinfo, modbus_tree, function_code, payload_start + 5, byte_cnt, register_format);
1158 else if (packet_type == RESPONSE_PACKET) {
1159 proto_tree_add_item(modbus_tree, hf_modbus_reference, tvb, payload_start, 2, ENC_BIG_ENDIAN);
1160 proto_tree_add_item(modbus_tree, hf_modbus_bitcnt, tvb, payload_start + 2, 2, ENC_BIG_ENDIAN);
1164 case WRITE_MULT_REGS:
1165 if (packet_type == QUERY_PACKET) {
1166 proto_tree_add_item(modbus_tree, hf_modbus_reference, tvb, payload_start, 2, ENC_BIG_ENDIAN);
1167 proto_tree_add_item(modbus_tree, hf_modbus_wordcnt, tvb, payload_start + 2, 2, ENC_BIG_ENDIAN);
1168 byte_cnt = (guint32)tvb_get_guint8(tvb, payload_start + 4);
1169 proto_tree_add_uint(modbus_tree, hf_modbus_bytecnt, tvb, payload_start + 4, 1,
1171 dissect_modbus_data(tvb, pinfo, modbus_tree, function_code, payload_start + 5, byte_cnt, register_format);
1173 else if (packet_type == RESPONSE_PACKET) {
1174 proto_tree_add_item(modbus_tree, hf_modbus_reference, tvb, payload_start, 2, ENC_BIG_ENDIAN);
1175 proto_tree_add_item(modbus_tree, hf_modbus_wordcnt, tvb, payload_start + 2, 2, ENC_BIG_ENDIAN);
1179 case READ_FILE_RECORD:
1180 if (packet_type == QUERY_PACKET) {
1181 byte_cnt = (guint32)tvb_get_guint8(tvb, payload_start);
1182 proto_tree_add_uint(modbus_tree, hf_modbus_bytecnt, tvb, payload_start, 1,
1185 /* add subtrees to describe each group of packet */
1186 group_offset = payload_start + 1;
1187 for (ii = 0; ii < byte_cnt / 7; ii++) {
1188 mi = proto_tree_add_text( modbus_tree, tvb, group_offset, 7,
1190 group_tree = proto_item_add_subtree(mi, ett_group_hdr);
1191 proto_tree_add_item(group_tree, hf_modbus_reftype, tvb, group_offset, 1, ENC_BIG_ENDIAN);
1192 proto_tree_add_item(group_tree, hf_modbus_lreference, tvb, group_offset + 1, 4, ENC_BIG_ENDIAN);
1193 proto_tree_add_item(group_tree, hf_modbus_wordcnt, tvb, group_offset + 5, 2, ENC_BIG_ENDIAN);
1197 else if (packet_type == RESPONSE_PACKET) {
1198 byte_cnt = (guint32)tvb_get_guint8(tvb, payload_start);
1199 proto_tree_add_uint(modbus_tree, hf_modbus_bytecnt, tvb, payload_start, 1,
1202 /* add subtrees to describe each group of packet */
1203 group_offset = payload_start + 1;
1205 while (byte_cnt > 0) {
1206 group_byte_cnt = (guint32)tvb_get_guint8(tvb, group_offset);
1207 mi = proto_tree_add_text( modbus_tree, tvb, group_offset, group_byte_cnt + 1,
1209 group_tree = proto_item_add_subtree(mi, ett_group_hdr);
1210 proto_tree_add_uint(group_tree, hf_modbus_bytecnt, tvb, group_offset, 1,
1212 proto_tree_add_item(group_tree, hf_modbus_reftype, tvb, group_offset + 1, 1, ENC_BIG_ENDIAN);
1213 dissect_modbus_data(tvb, pinfo, group_tree, function_code, group_offset + 2, group_byte_cnt - 1, register_format);
1214 group_offset += (group_byte_cnt + 1);
1215 byte_cnt -= (group_byte_cnt + 1);
1221 case WRITE_FILE_RECORD:
1222 if ((packet_type == QUERY_PACKET) || (packet_type == RESPONSE_PACKET)) {
1223 byte_cnt = (guint32)tvb_get_guint8(tvb, payload_start);
1224 proto_tree_add_uint(modbus_tree, hf_modbus_bytecnt, tvb, payload_start, 1,
1227 /* add subtrees to describe each group of packet */
1228 group_offset = payload_start + 1;
1230 while (byte_cnt > 0) {
1231 group_word_cnt = tvb_get_ntohs(tvb, group_offset + 5);
1232 group_byte_cnt = (2 * group_word_cnt) + 7;
1233 mi = proto_tree_add_text( modbus_tree, tvb, group_offset,
1234 group_byte_cnt, "Group %u", ii);
1235 group_tree = proto_item_add_subtree(mi, ett_group_hdr);
1236 proto_tree_add_item(group_tree, hf_modbus_reftype, tvb, group_offset, 1, ENC_BIG_ENDIAN);
1237 proto_tree_add_item(group_tree, hf_modbus_lreference, tvb, group_offset + 1, 4, ENC_BIG_ENDIAN);
1238 proto_tree_add_uint(group_tree, hf_modbus_wordcnt, tvb, group_offset + 5, 2,
1240 dissect_modbus_data(tvb, pinfo, group_tree, function_code, group_offset + 7, group_byte_cnt - 7, register_format);
1241 group_offset += group_byte_cnt;
1242 byte_cnt -= group_byte_cnt;
1248 case MASK_WRITE_REG:
1249 if ((packet_type == QUERY_PACKET) || (packet_type == RESPONSE_PACKET)) {
1250 proto_tree_add_item(modbus_tree, hf_modbus_reference, tvb, payload_start, 2, ENC_BIG_ENDIAN);
1251 proto_tree_add_item(modbus_tree, hf_modbus_andmask, tvb, payload_start + 2, 2, ENC_BIG_ENDIAN);
1252 proto_tree_add_item(modbus_tree, hf_modbus_ormask, tvb, payload_start + 4, 2, ENC_BIG_ENDIAN);
1256 case READ_WRITE_REG:
1257 if (packet_type == QUERY_PACKET) {
1258 proto_tree_add_item(modbus_tree, hf_modbus_readref, tvb, payload_start, 2, ENC_BIG_ENDIAN);
1259 proto_tree_add_item(modbus_tree, hf_modbus_readwordcnt, tvb, payload_start + 2, 2, ENC_BIG_ENDIAN);
1260 proto_tree_add_item(modbus_tree, hf_modbus_writeref, tvb, payload_start + 4, 2, ENC_BIG_ENDIAN);
1261 proto_tree_add_item(modbus_tree, hf_modbus_writewordcnt, tvb, payload_start + 6, 2, ENC_BIG_ENDIAN);
1262 byte_cnt = (guint32)tvb_get_guint8(tvb, payload_start + 8);
1263 proto_tree_add_uint(modbus_tree, hf_modbus_bytecnt, tvb, payload_start + 8, 1,
1265 dissect_modbus_data(tvb, pinfo, modbus_tree, function_code, payload_start + 9, byte_cnt, register_format);
1267 else if (packet_type == RESPONSE_PACKET) {
1268 byte_cnt = (guint32)tvb_get_guint8(tvb, payload_start);
1269 proto_tree_add_uint(modbus_tree, hf_modbus_bytecnt, tvb, payload_start, 1,
1271 dissect_modbus_data(tvb, pinfo, modbus_tree, function_code, payload_start + 1, byte_cnt, register_format);
1275 case READ_FIFO_QUEUE:
1276 if (packet_type == QUERY_PACKET)
1277 proto_tree_add_item(modbus_tree, hf_modbus_reference, tvb, payload_start, 2, ENC_BIG_ENDIAN);
1278 else if (packet_type == RESPONSE_PACKET) {
1279 byte_cnt = (guint32)tvb_get_ntohs(tvb, payload_start);
1280 proto_tree_add_uint(modbus_tree, hf_modbus_lbytecnt, tvb, payload_start, 2,
1282 proto_tree_add_item(modbus_tree, hf_modbus_wordcnt, tvb, payload_start + 2, 2, ENC_BIG_ENDIAN);
1283 dissect_modbus_data(tvb, pinfo, modbus_tree, function_code, payload_start + 4, byte_cnt - 2, register_format);
1287 case ENCAP_INTERFACE_TRANSP:
1288 if (packet_type == QUERY_PACKET) {
1289 proto_tree_add_item(modbus_tree, hf_modbus_mei, tvb, payload_start, 1, ENC_BIG_ENDIAN);
1290 mei_code = tvb_get_guint8(tvb, payload_start);
1293 case READ_DEVICE_ID:
1294 proto_tree_add_item(modbus_tree, hf_modbus_read_device_id, tvb, payload_start+1, 1, ENC_BIG_ENDIAN);
1295 proto_tree_add_item(modbus_tree, hf_modbus_object_id, tvb, payload_start+2, 1, ENC_BIG_ENDIAN);
1298 case CANOPEN_REQ_RESP:
1299 /* CANopen protocol not part of the Modbus/TCP specification */
1301 if (payload_len > 1)
1302 dissect_modbus_data(tvb, pinfo, modbus_tree, function_code, payload_start, payload_len-1, register_format);
1306 else if (packet_type == RESPONSE_PACKET) {
1307 proto_tree_add_item(modbus_tree, hf_modbus_mei, tvb, payload_start, 1, ENC_BIG_ENDIAN);
1308 mei_code = tvb_get_guint8(tvb, payload_start);
1311 case READ_DEVICE_ID:
1312 proto_tree_add_item(modbus_tree, hf_modbus_read_device_id, tvb, payload_start+1, 1, ENC_BIG_ENDIAN);
1313 proto_tree_add_item(modbus_tree, hf_modbus_conformity_level, tvb, payload_start+2, 1, ENC_BIG_ENDIAN);
1314 proto_tree_add_item(modbus_tree, hf_modbus_more_follows, tvb, payload_start+3, 1, ENC_BIG_ENDIAN);
1315 proto_tree_add_item(modbus_tree, hf_modbus_next_object_id, tvb, payload_start+4, 1, ENC_BIG_ENDIAN);
1316 num_objects = tvb_get_guint8(tvb, payload_start+5);
1317 proto_tree_add_uint(modbus_tree, hf_modbus_num_objects, tvb, payload_start+5, 1, num_objects);
1318 doe = proto_tree_add_text(modbus_tree, tvb, payload_start+6, payload_len-6, "Objects");
1321 for (ii = 0; ii < num_objects; ii++)
1323 device_objects_tree = proto_item_add_subtree(doe, ett_device_id_objects);
1325 /* add each "object item" as its own subtree */
1327 /* compute length of object */
1328 object_type = tvb_get_guint8(tvb, payload_start+6+object_index);
1329 object_len = tvb_get_guint8(tvb, payload_start+6+object_index+1);
1331 doie = proto_tree_add_text(device_objects_tree, tvb, payload_start+6+object_index, 2+object_len, "Object #%d", ii+1);
1332 device_objects_item_tree = proto_item_add_subtree(doie, ett_device_id_object_items);
1334 proto_tree_add_item(device_objects_item_tree, hf_modbus_object_id, tvb, payload_start+6+object_index, 1, ENC_BIG_ENDIAN);
1337 proto_tree_add_uint(device_objects_item_tree, hf_modbus_list_object_len, tvb, payload_start+6+object_index, 1, object_len);
1340 if (object_type < 7)
1342 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);
1347 proto_tree_add_text(device_objects_item_tree, tvb, payload_start+6+object_index, object_len, "Object Value");
1349 object_index += object_len;
1353 case CANOPEN_REQ_RESP:
1354 /* CANopen protocol not part of the Modbus/TCP specification */
1356 if (payload_len > 1)
1357 dissect_modbus_data(tvb, pinfo, modbus_tree, function_code, payload_start, payload_len-1, register_format);
1363 case REPORT_SLAVE_ID:
1365 if (payload_len > 0)
1366 dissect_modbus_data(tvb, pinfo, modbus_tree, function_code, payload_start, payload_len, register_format);
1371 return tvb_length(tvb);
1375 /* Register the protocol with Wireshark */
1378 proto_register_modbus(void)
1380 /* Modbus/TCP header fields */
1381 static hf_register_info mbtcp_hf[] = {
1382 { &hf_mbtcp_transid,
1383 { "Transaction Identifier", "mbtcp.trans_id",
1384 FT_UINT16, BASE_DEC, NULL, 0x0,
1388 { "Protocol Identifier", "mbtcp.prot_id",
1389 FT_UINT16, BASE_DEC, NULL, 0x0,
1393 { "Length", "mbtcp.len",
1394 FT_UINT16, BASE_DEC, NULL, 0x0,
1398 { "Unit Identifier", "mbtcp.unit_id",
1399 FT_UINT8, BASE_DEC, NULL, 0x0,
1404 static hf_register_info mbrtu_hf[] = {
1406 { "Unit ID", "mbrtu.unit_id",
1407 FT_UINT8, BASE_DEC, NULL, 0x0,
1411 { "CRC-16", "mbrtu.crc16",
1412 FT_UINT16, BASE_HEX, NULL, 0x0,
1417 static ei_register_info mbrtu_ei[] = {
1418 { &ei_mbrtu_crc16_incorrect, { "mbrtu.crc16.incorrect", PI_CHECKSUM, PI_WARN, "Incorrect CRC", EXPFILL }},
1421 static hf_register_info hf[] = {
1422 /* Modbus header fields */
1423 { &hf_mbtcp_functioncode,
1424 { "Function Code", "modbus.func_code",
1425 FT_UINT8, BASE_DEC, VALS(function_code_vals), 0x0,
1428 { &hf_modbus_reference,
1429 { "Reference Number", "modbus.reference_num",
1430 FT_UINT16, BASE_DEC, NULL, 0x0,
1433 { &hf_modbus_lreference,
1434 { "Reference Number (32 bit)", "modbus.reference_num_32",
1435 FT_UINT32, BASE_DEC, NULL, 0x0,
1438 { &hf_modbus_reftype,
1439 { "Reference Type", "modbus.reference_type",
1440 FT_UINT8, BASE_DEC, NULL, 0x0,
1443 { &hf_modbus_readref,
1444 { "Read Reference Number", "modbus.read_reference_num",
1445 FT_UINT16, BASE_DEC, NULL, 0x0,
1448 { &hf_modbus_writeref,
1449 { "Write Reference Number", "modbus.write_reference_num",
1450 FT_UINT16, BASE_DEC, NULL, 0x0,
1453 { &hf_modbus_wordcnt,
1454 { "Word Count", "modbus.word_cnt",
1455 FT_UINT16, BASE_DEC, NULL, 0x0,
1458 { &hf_modbus_readwordcnt,
1459 { "Read Word Count", "modbus.read_word_cnt",
1460 FT_UINT16, BASE_DEC, NULL, 0x0,
1463 { &hf_modbus_writewordcnt,
1464 { "Write Word Count", "modbus.write_word_cnt",
1465 FT_UINT16, BASE_DEC, NULL, 0x0,
1468 { &hf_modbus_bitcnt,
1469 { "Bit Count", "modbus.bit_cnt",
1470 FT_UINT16, BASE_DEC, NULL, 0x0,
1473 { &hf_modbus_bytecnt,
1474 { "Byte Count", "modbus.byte_cnt",
1475 FT_UINT8, BASE_DEC, NULL, 0x0,
1478 { &hf_modbus_lbytecnt,
1479 { "Byte Count (16-bit)", "modbus.byte_cnt_16",
1480 FT_UINT8, BASE_DEC, NULL, 0x0,
1483 { &hf_modbus_exceptioncode,
1484 { "Exception Code", "modbus.exception_code",
1485 FT_UINT8, BASE_DEC, VALS(exception_code_vals), 0x0,
1488 { &hf_modbus_diag_sf,
1489 { "Diagnostic Code", "modbus.diagnostic_code",
1490 FT_UINT16, BASE_DEC, VALS(diagnostic_code_vals), 0x0,
1493 { &hf_modbus_diag_return_query_data_request,
1494 { "Request Data", "modbus.diagnostic.return_query_data.request",
1495 FT_BYTES, BASE_NONE, NULL, 0x0,
1498 { &hf_modbus_diag_return_query_data_echo,
1499 { "Echo Data", "modbus.diagnostic.return_query_data.echo",
1500 FT_BYTES, BASE_NONE, NULL, 0x0,
1503 { &hf_modbus_diag_restart_communication_option,
1504 { "Restart Communication Option", "modbus.diagnostic.restart_communication_option",
1505 FT_UINT16, BASE_HEX, VALS(diagnostic_restart_communication_option_vals), 0x0,
1508 { &hf_modbus_diag_return_diag_register,
1509 { "Diagnostic Register Contents", "modbus.diagnostic.return_diag_register",
1510 FT_UINT16, BASE_HEX, NULL, 0x0,
1513 { &hf_modbus_diag_ascii_input_delimiter,
1514 { "CHAR", "modbus.diagnostic.ascii_input_delimiter",
1515 FT_UINT8, BASE_HEX, NULL, 0x0,
1518 { &hf_modbus_diag_return_bus_message_count,
1519 { "Total Message Count", "modbus.diagnostic.bus_message_count",
1520 FT_UINT16, BASE_DEC, NULL, 0x0,
1523 { &hf_modbus_diag_return_bus_comm_error_count,
1524 { "CRC Error Count", "modbus.diagnostic.bus_comm_error_count",
1525 FT_UINT16, BASE_DEC, NULL, 0x0,
1528 { &hf_modbus_diag_return_bus_exception_error_count,
1529 { "Exception Error Count", "modbus.diagnostic.bus_exception_error_count",
1530 FT_UINT16, BASE_DEC, NULL, 0x0,
1533 { &hf_modbus_diag_return_slave_message_count,
1534 { "Slave Message Count", "modbus.diagnostic.slave_message_count",
1535 FT_UINT16, BASE_DEC, NULL, 0x0,
1538 { &hf_modbus_diag_return_no_slave_response_count,
1539 { "Slave No Response Count", "modbus.diagnostic.no_slave_response_count",
1540 FT_UINT16, BASE_DEC, NULL, 0x0,
1543 { &hf_modbus_diag_return_slave_nak_count,
1544 { "Slave NAK Count", "modbus.diagnostic.slave_nak_count",
1545 FT_UINT16, BASE_DEC, NULL, 0x0,
1548 { &hf_modbus_diag_return_slave_busy_count,
1549 { "Slave Device Busy Count", "modbus.diagnostic.slave_busy_count",
1550 FT_UINT16, BASE_DEC, NULL, 0x0,
1553 { &hf_modbus_diag_return_bus_char_overrun_count,
1554 { "Slave Character Overrun Count", "modbus.diagnostic.bus_char_overrun_count",
1555 FT_UINT16, BASE_DEC, NULL, 0x0,
1558 { &hf_modbus_status,
1559 { "Status", "modbus.ev_status",
1560 FT_UINT16, BASE_HEX, NULL, 0x0,
1563 { &hf_modbus_event_count,
1564 { "Event Vount", "modbus.ev_count",
1565 FT_UINT16, BASE_DEC, NULL, 0x0,
1568 { &hf_modbus_message_count,
1569 { "Message Count", "modbus.ev_msg_count",
1570 FT_UINT16, BASE_DEC, NULL, 0x0,
1573 { &hf_modbus_event_recv_comm_err,
1574 { "Communication Error", "modbus.ev_recv_comm_err",
1575 FT_UINT8, BASE_DEC, NULL, 0x02,
1578 { &hf_modbus_event_recv_char_over,
1579 { "Character Overrun", "modbus.ev_recv_char_over",
1580 FT_UINT8, BASE_DEC, NULL, 0x10,
1583 { &hf_modbus_event_recv_lo_mode,
1584 { "Currently in Listen Only Mode", "modbus.ev_recv_lo_mode",
1585 FT_UINT8, BASE_DEC, NULL, 0x20,
1588 { &hf_modbus_event_recv_broadcast,
1589 { "Broadcast Received", "modbus.ev_recv_broadcast",
1590 FT_UINT8, BASE_DEC, NULL, 0x40,
1593 { &hf_modbus_event_send_read_ex,
1594 { "Read Exception Sent", "modbus.ev_send_read_ex",
1595 FT_UINT8, BASE_DEC, NULL, 0x01,
1598 { &hf_modbus_event_send_slave_abort_ex,
1599 { "Slave Abort Exception Sent", "modbus.ev_send_slave_abort_ex",
1600 FT_UINT8, BASE_DEC, NULL, 0x02,
1603 { &hf_modbus_event_send_slave_busy_ex,
1604 { "Slave Busy Exception Sent", "modbus.ev_send_slave_busy_ex",
1605 FT_UINT8, BASE_DEC, NULL, 0x04,
1608 { &hf_modbus_event_send_slave_nak_ex,
1609 { "Slave Program NAK Exception Sent", "modbus.ev_send_slave_nak_ex",
1610 FT_UINT8, BASE_DEC, NULL, 0x08,
1613 { &hf_modbus_event_send_write_timeout,
1614 { "Write Timeout Error Occurred", "modbus.ev_send_write_timeout",
1615 FT_UINT8, BASE_DEC, NULL, 0x10,
1618 { &hf_modbus_event_send_lo_mode,
1619 { "Currently in Listen Only Mode", "modbus.ev_send_lo_mode",
1620 FT_UINT8, BASE_DEC, NULL, 0x20,
1623 { &hf_modbus_andmask,
1624 { "AND mask", "modbus.and_mask",
1625 FT_UINT16, BASE_HEX, NULL, 0x0,
1628 { &hf_modbus_ormask,
1629 { "OR mask", "modbus.or_mask",
1630 FT_UINT16, BASE_HEX, NULL, 0x0,
1634 { "Data", "modbus.data",
1635 FT_BYTES, BASE_NONE, NULL, 0x0, NULL, HFILL }
1638 { "MEI type", "modbus.mei",
1639 FT_UINT8, BASE_DEC, VALS(encap_interface_code_vals), 0x0,
1642 { &hf_modbus_read_device_id,
1643 { "Read Device ID", "modbus.read_device_id",
1644 FT_UINT8, BASE_DEC, VALS(read_device_id_vals), 0x0,
1647 { &hf_modbus_object_id,
1648 { "Object ID", "modbus.object_id",
1649 FT_UINT8, BASE_DEC, VALS(object_id_vals), 0x0,
1652 { &hf_modbus_num_objects,
1653 { "Number of Objects", "modbus.num_objects",
1654 FT_UINT8, BASE_DEC, NULL, 0x0,
1657 { &hf_modbus_list_object_len,
1658 { "Object length", "modbus.objects_len",
1659 FT_UINT8, BASE_DEC, NULL, 0x0,
1662 { &hf_modbus_conformity_level,
1663 { "Conformity Level", "modbus.conformity_level",
1664 FT_UINT8, BASE_HEX, VALS(conformity_level_vals), 0x0,
1667 { &hf_modbus_more_follows,
1668 { "More Follows", "modbus.more_follows",
1669 FT_UINT8, BASE_HEX, NULL, 0x0,
1672 { &hf_modbus_next_object_id,
1673 { "Next Object ID", "modbus.next_object_id",
1674 FT_UINT8, BASE_DEC, NULL, 0x0,
1677 { &hf_modbus_object_str_value,
1678 { "Object String Value", "modbus.object_str_value",
1679 FT_STRING, BASE_NONE, NULL, 0x0,
1682 { &hf_modbus_reg_uint16,
1683 { "Register (UINT16)", "modbus.register.uint16",
1684 FT_UINT16, BASE_DEC, NULL, 0x0,
1687 { &hf_modbus_reg_uint32,
1688 { "Register (UINT32)", "modbus.register.uint32",
1689 FT_UINT32, BASE_DEC, NULL, 0x0,
1692 { &hf_modbus_reg_ieee_float,
1693 { "Register (IEEE Float)", "modbus.register.ieee_float",
1694 FT_FLOAT, BASE_NONE, NULL, 0x0,
1697 { &hf_modbus_reg_modicon_float,
1698 { "Register (Modicon Float)", "modbus.register.modicon_float",
1699 FT_FLOAT, BASE_NONE, NULL, 0x0,
1704 /* Setup protocol subtree array */
1705 static gint *ett[] = {
1713 &ett_device_id_objects,
1714 &ett_device_id_object_items
1717 static ei_register_info ei[] = {
1718 { &ei_modbus_data_decode, { "modbus.data.decode", PI_PROTOCOL, PI_WARN, "Invalid decoding options, register data not a multiple of 4!", EXPFILL }},
1721 module_t *mbtcp_module;
1722 module_t *mbrtu_module;
1723 expert_module_t* expert_mbrtu;
1724 expert_module_t* expert_modbus;
1726 /* Register the protocol name and description */
1727 proto_mbtcp = proto_register_protocol("Modbus/TCP", "Modbus/TCP", "mbtcp");
1728 proto_mbrtu = proto_register_protocol("Modbus RTU", "Modbus RTU", "mbrtu");
1729 proto_modbus = proto_register_protocol("Modbus", "Modbus", "modbus");
1731 /* Registering protocol to be called by another dissector */
1732 modbus_handle = new_register_dissector("modbus", dissect_modbus, proto_modbus);
1733 mbtcp_handle = new_register_dissector("mbtcp", dissect_mbtcp, proto_mbtcp);
1734 mbrtu_handle = new_register_dissector("mbrtu", dissect_mbrtu, proto_mbrtu);
1736 /* Registering subdissectors table */
1737 modbus_data_dissector_table = register_dissector_table("modbus.data", "Modbus Data", FT_STRING, BASE_NONE);
1738 modbus_dissector_table = register_dissector_table("mbtcp.prot_id", "Modbus/TCP protocol identifier", FT_UINT16, BASE_DEC);
1740 /* Required function calls to register the header fields and subtrees used */
1741 proto_register_field_array(proto_mbtcp, mbtcp_hf, array_length(mbtcp_hf));
1742 proto_register_field_array(proto_mbrtu, mbrtu_hf, array_length(mbrtu_hf));
1743 proto_register_field_array(proto_modbus, hf, array_length(hf));
1744 proto_register_subtree_array(ett, array_length(ett));
1745 expert_mbrtu = expert_register_protocol(proto_mbrtu);
1746 expert_register_field_array(expert_mbrtu, mbrtu_ei, array_length(mbrtu_ei));
1747 expert_modbus = expert_register_protocol(proto_modbus);
1748 expert_register_field_array(expert_modbus, ei, array_length(ei));
1751 /* Register required preferences for Modbus Protocol register decoding */
1752 mbtcp_module = prefs_register_protocol(proto_mbtcp, proto_reg_handoff_mbtcp);
1753 mbrtu_module = prefs_register_protocol(proto_mbrtu, proto_reg_handoff_mbrtu);
1755 /* Modbus RTU Preference - Desegment, defaults to TRUE for TCP desegmentation */
1756 prefs_register_bool_preference(mbtcp_module, "desegment",
1757 "Desegment all Modbus RTU packets spanning multiple TCP segments",
1758 "Whether the Modbus RTU dissector should desegment all messages spanning multiple TCP segments",
1761 /* Modbus/TCP Preference - Default TCP Port, allows for "user" port either than 502. */
1762 prefs_register_uint_preference(mbtcp_module, "tcp.port", "Modbus/TCP Port",
1763 "Set the TCP port for Modbus/TCP packets (if other"
1764 " than the default of 502)",
1765 10, &global_mbus_tcp_port);
1767 /* Modbus/TCP Preference - Holding/Input Register format, this allows for deeper dissection of response data */
1768 prefs_register_enum_preference(mbtcp_module, "mbus_register_format",
1769 "Holding/Input Register Format",
1771 &global_mbus_tcp_register_format,
1772 mbus_register_format,
1775 /* Modbus/TCP Preference - Register addressing format, this allows for a configurable display option to determine addressing used */
1776 prefs_register_enum_preference(mbtcp_module, "mbus_register_addr_type",
1777 "Register Addressing Type",
1778 "Register Addressing Type (Raw, Modicon 5 or 6). This option has no effect on the underlying protocol, but changes the register address display format",
1779 &global_mbus_tcp_register_addr_type,
1780 mbus_register_addr_type,
1783 /* Modbus RTU Preference - Desegment, defaults to TRUE for TCP desegmentation */
1784 prefs_register_bool_preference(mbrtu_module, "desegment",
1785 "Desegment all Modbus RTU packets spanning multiple TCP segments",
1786 "Whether the Modbus RTU dissector should desegment all messages spanning multiple TCP segments",
1789 /* Modbus RTU Preference - CRC verification, defaults to FALSE (not do verification)*/
1790 prefs_register_bool_preference(mbrtu_module, "crc_verification",
1792 "Whether to validate the CRC",
1795 /* Modbus RTU Preference - Default TCP Port, defaults to zero, allows custom user port. */
1796 prefs_register_uint_preference(mbrtu_module, "tcp.port", "Modbus RTU Port",
1797 "Set the TCP port for encapsulated Modbus RTU packets",
1798 10, &global_mbus_rtu_port);
1800 /* Modbus RTU Preference - Holding/Input Register format, this allows for deeper dissection of response data */
1801 prefs_register_enum_preference(mbrtu_module, "mbus_register_format",
1802 "Holding/Input Register Format",
1804 &global_mbus_rtu_register_format,
1805 mbus_register_format,
1808 /* Modbus RTU Preference - Register addressing format, this allows for a configurable display option to determine addressing used */
1809 prefs_register_enum_preference(mbrtu_module, "mbus_register_addr_type",
1810 "Register Addressing Type",
1811 "Register Addressing Type (Raw, Modicon 5 or 6). This option has no effect on the underlying protocol, but changes the register address display format",
1812 &global_mbus_rtu_register_addr_type,
1813 mbus_register_addr_type,
1819 /* If this dissector uses sub-dissector registration add a registration routine.
1820 This format is required because a script is used to find these routines and
1821 create the code that calls these routines.
1824 proto_reg_handoff_mbtcp(void)
1826 static unsigned int mbtcp_port;
1828 /* Make sure to use Modbus/TCP Preferences field to determine default TCP port */
1830 if(mbtcp_port != 0 && mbtcp_port != global_mbus_tcp_port){
1831 dissector_delete_uint("tcp.port", mbtcp_port, mbtcp_handle);
1834 if(global_mbus_tcp_port != 0 && mbtcp_port != global_mbus_tcp_port) {
1835 dissector_add_uint("tcp.port", global_mbus_tcp_port, mbtcp_handle);
1838 mbtcp_port = global_mbus_tcp_port;
1840 dissector_add_uint("mbtcp.prot_id", MODBUS_PROTOCOL_ID, modbus_handle);
1845 proto_reg_handoff_mbrtu(void)
1847 static unsigned int mbrtu_port = 0;
1849 /* Make sure to use Modbus RTU Preferences field to determine default TCP port */
1851 if(mbrtu_port != 0 && mbrtu_port != global_mbus_rtu_port){
1852 dissector_delete_uint("tcp.port", mbrtu_port, mbrtu_handle);
1855 if(global_mbus_rtu_port != 0 && mbrtu_port != global_mbus_rtu_port) {
1856 dissector_add_uint("tcp.port", global_mbus_rtu_port, mbrtu_handle);
1859 mbrtu_port = global_mbus_rtu_port;
1861 dissector_add_uint("mbtcp.prot_id", MODBUS_PROTOCOL_ID, modbus_handle);