2 * Routines for kingfisher packet dissection
7 * Wireshark - Network traffic analyzer
8 * By Gerald Combs <gerald@wireshark.org>
9 * Copyright 1998 Gerald Combs
11 * Copied from packet-pop.c
13 * This program is free software; you can redistribute it and/or
14 * modify it under the terms of the GNU General Public License
15 * as published by the Free Software Foundation; either version 2
16 * of the License, or (at your option) any later version.
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
23 * You should have received a copy of the GNU General Public License
24 * along with this program; if not, write to the Free Software
25 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
30 #include <epan/packet.h>
31 #include <epan/wmem/wmem.h>
32 #include <epan/conversation.h>
34 #define SUPPORT_KINGFISHER_SERIES_2
36 #define TCP_PORT_KINGFISHER 4058
37 #define UDP_PORT_KINGFISHER 4058
38 #ifdef SUPPORT_KINGFISHER_SERIES_2
39 #define TCP_PORT_KINGFISHER_OLD 473
40 #define UDP_PORT_KINGFISHER_OLD 473
43 static int proto_kingfisher
= -1;
44 static int hf_kingfisher_version
= -1;
45 static int hf_kingfisher_system
= -1;
46 static int hf_kingfisher_length
= -1;
47 static int hf_kingfisher_from
= -1;
48 static int hf_kingfisher_target
= -1;
49 static int hf_kingfisher_via
= -1;
50 static int hf_kingfisher_message
= -1;
51 static int hf_kingfisher_function
= -1;
52 static int hf_kingfisher_checksum
= -1;
54 static dissector_handle_t kingfisher_conv_handle
;
57 typedef struct _kingfisher_packet_t
68 } kingfisher_packet_t
;
70 static gint ett_kingfisher
= -1;
72 static const value_string function_code_vals
[] =
74 { 0x00, "Acknowledgement" },
75 { 0x01, "Negative Acknowledgement" },
76 { 0x02, "No Access" },
77 { 0x03, "Message Buffer Full" },
78 { 0x0a, "Get Data Frame" },
79 { 0x0b, "Send Data Frame" },
80 { 0x0c, "Get Data Blocks" },
81 { 0x0d, "Send Data Blocks" },
82 { 0x0e, "Check RTU Update" },
83 { 0x0f, "Send RTU Update" },
84 { 0x10, "Get Multiple Data" },
85 { 0x11, "Send Multiple Data" },
86 { 0x12, "Get Multiple Network Data" },
87 { 0x13, "Send Multiple Network Data" },
88 { 0x1e, "Cold Start" },
89 { 0x1f, "Warm Start" },
90 { 0x21, "Program Control" },
91 { 0x22, "Get RTU Status" },
92 { 0x23, "Send RTU Status" },
94 { 0x25, "Swap Master CPU" },
95 { 0x26, "Send I/O Module Message" },
96 { 0x28, "Get Diagnostic Information" },
97 { 0x29, "Send Diagnostic Information" },
98 { 0x2b, "Send Pager Information" },
99 { 0x2c, "Get Pager Information" },
100 { 0x2d, "Send Port Data Information" },
101 { 0x2e, "Get Port Data Information" },
102 { 0x2f, "Send RTU Data Information" },
103 { 0x30, "Get RTU Data Information" },
104 { 0x31, "Unlock Port" },
105 { 0x33, "Carrier Test" },
106 { 0x34, "Program Flash RAM" },
107 { 0x35, "Get I/O Values" },
108 { 0x36, "Send I/O Values" },
109 { 0x37, "Synchronise Clock" },
110 { 0x38, "Send Communications Module Message" },
111 { 0x39, "Get Communications Module Message" },
112 { 0x3a, "Get Driver Information" },
113 { 0x3b, "Send Driver Information" },
114 { 0x3c, "Communications Analyser" },
115 { 0x41, "Dial Site" },
116 { 0x42, "Hang-up Site" },
117 { 0x46, "Send File" },
118 { 0x47, "Get File" },
119 { 0x50, "Get Event Logging" },
120 { 0x51, "Send Event Logging" },
121 { 0x80, "Acknowledgement" },
122 { 0x81, "Negative Acknowledgement" },
123 { 0x84, "Get Named Variable" },
124 { 0x85, "Send Named Variable" },
125 { 0x87, "Get Module Information" },
126 { 0x88, "Send Module Information" },
127 { 0x89, "Get I/O Values" },
128 { 0x8a, "Send I/O Values" },
129 { 0x9e, "Cold Start" },
130 { 0x9f, "Warm Start" },
131 { 0xa2, "Get RTU Status" },
132 { 0xa3, "Send RTU Status" },
134 { 0xa8, "Get Diagnostic Information" },
135 { 0xa9, "Send Diagnostic Information" },
136 { 0xd1, "Set Event Log" },
137 { 0xd2, "Clear Event Log" },
138 { 0xd3, "Get Number of Events" },
139 { 0xd4, "Send Number of Events" },
140 { 0xd5, "Get Event Log" },
141 { 0xd6, "Continue Event Log" },
142 { 0xd7, "Send Event Log" },
143 { 0xe0, "Send File Start" },
144 { 0xe1, "Send File Start Acknowledgement" },
145 { 0xe2, "Send File Data" },
146 { 0xe3, "Send File Data Acknowledgement" },
151 static unsigned short
152 kingfisher_checksum(tvbuff_t
*tvb
, int offset
)
158 len
= tvb_reported_length_remaining(tvb
, offset
) - 2;
159 for( i
= 1; i
< len
; i
++ )
161 c
= ( ( unsigned char ) tvb_get_guint8( tvb
, i
) ) & 0xff;
162 for( j
= 0; j
< 8; ++j
)
167 crc
+= ( ( ( c
<<= 1 ) & 0x100 ) != 0 );
173 crc
+= ( ( ( c
<<= 1 ) & 0x100 ) != 0 );
182 dissect_kingfisher(tvbuff_t
*tvb
, packet_info
*pinfo
, proto_tree
*tree
, gboolean is_conv_dissector
)
184 kingfisher_packet_t kfp
;
185 proto_tree
*kingfisher_tree
=NULL
;
186 proto_item
*item
=NULL
;
187 const char *func_string
= NULL
;
188 unsigned short checksum
;
192 /* There can be one byte reply packets. we only test for these when we
193 are called from the conversation dissector since that is the only time
194 we can be certain this is kingfisher
196 if(is_conv_dissector
&& (tvb_reported_length(tvb
)==1)){
198 Perform a check to see if the message is a single byte acknowledgement
199 message - Note that in this instance there is no information in the packet
200 with regard to source or destination RTU address which can be used in the
201 population of dissector fields.
203 switch(tvb_get_guint8(tvb
, 0)){
208 col_set_str(pinfo
->cinfo
, COL_PROTOCOL
, "Kingfisher");
209 func_string
= val_to_str_const(tvb_get_guint8(tvb
, 0), function_code_vals
, "Unknown function");
210 col_add_fstr(pinfo
->cinfo
, COL_INFO
, "(%s)", func_string
);
211 proto_tree_add_protocol_format(tree
, proto_kingfisher
, tvb
, 0, -1, "Kingfisher Protocol, %s", func_string
);
214 /* othervise it is way too short to be kingfisger */
219 /* Verify that it looks like kingfisher */
220 /* the packet must be at least 9 bytes */
221 if(tvb_reported_length(tvb
)<9){
225 /* the function code must be known */
226 kfp
.function
= tvb_get_guint8( tvb
, 6 );
227 if (try_val_to_str(kfp
.function
, function_code_vals
) == NULL
) {
228 /* This appears not to be a kingfisher packet */
232 /* verify the length */
233 kfp
.length
= tvb_get_guint8(tvb
, 2);
234 if((kfp
.length
+1) != (guint8
)tvb_length(tvb
)){
238 /* verify the checksum */
239 kfp
.checksum
= tvb_get_ntohs(tvb
, kfp
.length
- 1);
240 checksum
= kingfisher_checksum(tvb
, 0);
241 if(kfp
.checksum
!=checksum
){
246 kfp
.version
= (kfp
.function
& 0x80)?3:2;
247 kfp
.system
= tvb_get_guint8( tvb
, 0 );
248 kfp
.message
= tvb_get_guint8( tvb
, 5 );
250 kfp
.target
= tvb_get_guint8( tvb
, 1 );
251 kfp
.from
= tvb_get_guint8( tvb
, 3 );
252 kfp
.via
= tvb_get_guint8( tvb
, 4 );
254 if( kfp
.version
== 3 )
256 kfp
.target
|= ( tvb_get_guint8( tvb
, 7 ) << 8 );
257 kfp
.from
|= ( tvb_get_guint8( tvb
, 8 ) << 8 );
258 kfp
.via
|= ( tvb_get_guint8( tvb
, 9 ) << 8 );
262 /* Ok this does look like Kingfisher, so lets dissect it */
263 func_string
= val_to_str_const(kfp
.function
, function_code_vals
, "Unknown function");
265 col_set_str(pinfo
->cinfo
, COL_PROTOCOL
, "Kingfisher");
266 col_add_fstr(pinfo
->cinfo
, COL_INFO
, "%u > %u (%s)", kfp
.from
, kfp
.target
, func_string
);
269 message
= (kfp
.message
& 0x0f) | ((kfp
.message
& 0xf0) >> 4);
272 item
= proto_tree_add_protocol_format(tree
, proto_kingfisher
, tvb
, 0, -1, "Kingfisher Protocol, From RTU: %d, Target RTU: %d", kfp
.from
, kfp
.target
);
273 kingfisher_tree
= proto_item_add_subtree( item
, ett_kingfisher
);
277 proto_tree_add_uint(kingfisher_tree
, hf_kingfisher_version
, tvb
, 6, 1, kfp
.version
);
280 proto_tree_add_uint(kingfisher_tree
, hf_kingfisher_system
, tvb
, 0, 1, kfp
.system
);
283 proto_tree_add_uint(kingfisher_tree
, hf_kingfisher_target
, tvb
, 1, 1, kfp
.target
);
286 proto_tree_add_uint(kingfisher_tree
, hf_kingfisher_length
, tvb
, 2, 1, kfp
.length
);
289 proto_tree_add_uint(kingfisher_tree
, hf_kingfisher_from
, tvb
, 3, 1, kfp
.from
);
292 proto_tree_add_uint(kingfisher_tree
, hf_kingfisher_via
, tvb
, 4, 1, kfp
.via
);
295 proto_tree_add_uint_format_value(kingfisher_tree
, hf_kingfisher_message
, tvb
, 5, 1, kfp
.message
, "%u (0x%02X, %s)", message
, kfp
.message
, ((kfp
.message
& 0xf0)?"Response":"Request"));
297 /* message function code */
298 proto_tree_add_uint_format(kingfisher_tree
, hf_kingfisher_function
, tvb
, 6, 1, kfp
.function
, "Message Function Code: %u (0x%02X, %s)", kfp
.function
, kfp
.function
, func_string
);
301 if(kfp
.length
> ((kfp
.version
==3)?11:8)){
302 proto_tree_add_text(kingfisher_tree
, tvb
, ((kfp
.version
==3)?10:7), kfp
.length
- ((kfp
.version
==3)?11:8), "Message Data");
306 proto_tree_add_uint_format_value(kingfisher_tree
, hf_kingfisher_checksum
, tvb
, kfp
.length
-1, 2, kfp
.checksum
, "0x%04X [%s]", kfp
.checksum
, ((checksum
!= kfp
.checksum
)?"incorrect":"correct"));
315 dissect_kingfisher_heur(tvbuff_t
*tvb
, packet_info
*pinfo
, proto_tree
*tree
, void *data _U_
)
317 gboolean was_kingfisher
;
320 was_kingfisher
=dissect_kingfisher(tvb
, pinfo
, tree
, FALSE
);
323 conversation_t
*conversation
;
325 /* Ok this was a genuine kingfisher packet. Now create a conversation
326 dissector for this tcp/udp socket and attach a conversation
329 conversation
= find_or_create_conversation(pinfo
);
331 conversation_set_dissector(conversation
, kingfisher_conv_handle
);
334 return was_kingfisher
;
338 dissect_kingfisher_conv(tvbuff_t
*tvb
, packet_info
*pinfo
, proto_tree
*tree
, void *data _U_
)
340 return dissect_kingfisher(tvb
, pinfo
, tree
, TRUE
);
344 proto_register_kingfisher( void )
346 static hf_register_info hf
[] =
348 { &hf_kingfisher_version
, { "Version", "kingfisher.version", FT_UINT8
, BASE_DEC
, NULL
, 0x0, NULL
, HFILL
} },
349 { &hf_kingfisher_system
, { "System Identifier", "kingfisher.system", FT_UINT8
, BASE_DEC_HEX
, NULL
, 0x0, NULL
, HFILL
} },
350 { &hf_kingfisher_length
, { "Length", "kingfisher.length", FT_UINT8
, BASE_DEC_HEX
, NULL
, 0x0, NULL
, HFILL
} },
351 { &hf_kingfisher_from
, { "From RTU", "kingfisher.from", FT_UINT16
, BASE_DEC_HEX
, NULL
, 0x0, NULL
, HFILL
} },
352 { &hf_kingfisher_target
, { "Target RTU", "kingfisher.target", FT_UINT16
, BASE_DEC_HEX
, NULL
, 0x0, NULL
, HFILL
} },
353 { &hf_kingfisher_via
, { "Via RTU", "kingfisher.via", FT_UINT16
, BASE_DEC_HEX
, NULL
, 0x0, NULL
, HFILL
} },
354 { &hf_kingfisher_message
, { "Message Number", "kingfisher.message", FT_UINT8
, BASE_DEC
, NULL
, 0x0, NULL
, HFILL
} },
355 { &hf_kingfisher_function
, { "Function Code", "kingfisher.function", FT_UINT8
, BASE_DEC
, VALS( function_code_vals
), 0x0, NULL
, HFILL
} },
356 { &hf_kingfisher_checksum
, { "Checksum", "kingfisher.checksum", FT_UINT16
, BASE_DEC
, NULL
, 0x0, NULL
, HFILL
} },
359 static gint
*ett
[] = {
363 proto_kingfisher
= proto_register_protocol( "Kingfisher", "Kingfisher", "kf" );
364 proto_register_field_array( proto_kingfisher
, hf
, array_length( hf
) );
365 proto_register_subtree_array( ett
, array_length( ett
) );
370 proto_reg_handoff_kingfisher( void )
372 dissector_handle_t kingfisher_handle
=NULL
;
374 kingfisher_handle
= new_create_dissector_handle(dissect_kingfisher_heur
, proto_kingfisher
);
375 dissector_add_uint("tcp.port", TCP_PORT_KINGFISHER
, kingfisher_handle
);
376 dissector_add_uint("udp.port", UDP_PORT_KINGFISHER
, kingfisher_handle
);
378 #ifdef SUPPORT_KINGFISHER_SERIES_2
379 dissector_add_uint("tcp.port", TCP_PORT_KINGFISHER_OLD
, kingfisher_handle
);
380 dissector_add_uint("udp.port", UDP_PORT_KINGFISHER_OLD
, kingfisher_handle
);
382 kingfisher_conv_handle
= new_create_dissector_handle(dissect_kingfisher_conv
, proto_kingfisher
);