HACK: 2nd try to match RowsetProperties
[wireshark-wip.git] / epan / dissectors / packet-ax25-kiss.c
blobc545627d86dd37ba87103a7ca4c439dc4d6d0e8f
1 /* packet-ax25-kiss.c
3 * Routines for AX.25 KISS protocol dissection
4 * Copyright 2010,2012 R.W. Stearn <richard@rns-stearn.demon.co.uk>
6 * $Id$
8 * Wireshark - Network traffic analyzer
9 * By Gerald Combs <gerald@wireshark.org>
10 * Copyright 1998 Gerald Combs
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
28 * This dissector handles the "KISS" protocol as implemented by the
29 * Linux kernel.
31 * The original definition of the KISS protocol can be found here:
32 * http://www.ka9q.net/papers/kiss.html
33 * and here:
34 * http://www.ax25.net/kiss.aspx
36 * The Linux implementation does not appear to attempt to implement that
37 * protocol in full. It does provide the ability to send a KISS command via
38 * ax25_kiss_cmd() and internally will send FULLDUPLEX KISS commands if
39 * DAMA is enabled/disabled.
40 * i.e.:
41 * ax25_dev_dama_on sends FullDuplex ON
42 * ax25_dev_dama_off sends FullDuplex OFF
44 * Data frames are prefixed with a "Data Frame" command but the port appears to
45 * be always 0.
47 * Abstract from http://www.ka9q.net/papers/kiss.html
48 * --------------------------------------------------
49 * For reference the first byte of a KISS frame is the frame type and the TNC
50 * port number,
51 * LSB 4 bits = frame type,
52 * MSB 4 bits = port number.
54 * The frame types are:
55 * Command Function Comments
56 * 0 Data frame The rest of the frame is data to
57 * be sent on the HDLC channel.
59 * 1 TXDELAY The next byte is the transmitter
60 * keyup delay in 10 ms units.
61 * The default start-up value is 50
62 * (i.e., 500 ms).
64 * 2 P The next byte is the persistence
65 * parameter, p, scaled to the range
66 * 0 - 255 with the following
67 * formula:
69 * P = p * 256 - 1
71 * The default value is P = 63
72 * (i.e., p = 0.25).
74 * 3 SlotTime The next byte is the slot interval
75 * in 10 ms units.
76 * The default is 10 (i.e., 100ms).
78 * 4 TXtail The next byte is the time to hold
79 * up the TX after the FCS has been
80 * sent, in 10 ms units. This command
81 * is obsolete, and is included here
82 * only for compatibility with some
83 * existing implementations.
85 * 5 FullDuplex The next byte is 0 for half duplex,
86 * nonzero for full duplex.
87 * The default is 0
88 * (i.e., half duplex).
90 * 6 SetHardware Specific for each TNC. In the
91 * TNC-1, this command sets the
92 * modem speed. Other implementations
93 * may use this function for other
94 * hardware-specific functions.
96 * FF Return Exit KISS and return control to a
97 * higher-level program. This is useful
98 * only when KISS is incorporated
99 * into the TNC along with other
100 * applications.
104 #include "config.h"
106 #include <glib.h>
108 #include <epan/packet.h>
109 #include <epan/wmem/wmem.h>
111 #include "packet-ax25-kiss.h"
112 #include "packet-ax25.h"
114 #define STRLEN 80
116 #define KISS_HEADER_SIZE 1 /* length of the KISS type header */
118 /* KISS frame types */
119 #define KISS_DATA_FRAME 0
120 #define KISS_TXDELAY 1
121 #define KISS_PERSISTENCE 2
122 #define KISS_SLOT_TIME 3
123 #define KISS_TXTAIL 4
124 #define KISS_FULLDUPLEX 5
125 #define KISS_SETHARDWARE 6
126 #define KISS_RETURN 15
128 #define KISS_CMD_MASK 0x0f
129 #define KISS_PORT_MASK 0xf0
131 void proto_register_ax25_kiss(void);
132 void proto_reg_handoff_ax25_kiss(void);
134 /* Initialize the protocol and registered fields */
135 static int proto_ax25_kiss = -1;
137 static int hf_ax25_kiss_cmd = -1;
138 static int hf_ax25_kiss_port = -1;
139 static int hf_ax25_kiss_txdelay = -1;
140 static int hf_ax25_kiss_persistence = -1;
141 static int hf_ax25_kiss_slottime = -1;
142 static int hf_ax25_kiss_txtail = -1;
143 static int hf_ax25_kiss_fullduplex = -1;
144 static int hf_ax25_kiss_sethardware = -1;
146 /* Initialize the subtree pointers */
147 static gint ett_ax25_kiss = -1;
149 static dissector_handle_t kiss_handle;
151 /* Dissector handles - all the possibles are listed */
152 static dissector_handle_t ax25_handle;
154 static const value_string kiss_frame_types[] = {
155 { KISS_DATA_FRAME, "Data frame" },
156 { KISS_TXDELAY, "Tx Delay" },
157 { KISS_PERSISTENCE, "Persistence" },
158 { KISS_SLOT_TIME, "Slot time" },
159 { KISS_TXTAIL, "Tx tail" },
160 { KISS_FULLDUPLEX, "Full duplex" },
161 { KISS_SETHARDWARE, "Set hardware" },
162 { KISS_RETURN, "Return" },
163 { 0, NULL }
166 void
167 capture_ax25_kiss( const guchar *pd, int offset, int len, packet_counts *ld)
169 int l_offset;
170 guint8 kiss_cmd;
172 if ( ! BYTES_ARE_IN_FRAME( offset, len, KISS_HEADER_SIZE ) )
174 ld->other++;
175 return;
178 l_offset = offset;
179 kiss_cmd = pd[ l_offset ];
180 l_offset += KISS_HEADER_SIZE; /* step over kiss header */
181 switch ( kiss_cmd & KISS_CMD_MASK )
183 case KISS_DATA_FRAME : capture_ax25( pd, l_offset, len, ld ); break;
184 case KISS_TXDELAY : break;
185 case KISS_PERSISTENCE : break;
186 case KISS_SLOT_TIME : break;
187 case KISS_TXTAIL : break;
188 case KISS_FULLDUPLEX : break;
189 case KISS_SETHARDWARE : break;
190 case KISS_RETURN : break;
191 default : break;
195 /* Code to actually dissect the packets */
196 static void
197 dissect_ax25_kiss( tvbuff_t *tvb, packet_info *pinfo, proto_tree *parent_tree )
199 proto_item *ti;
200 proto_tree *kiss_tree;
201 int offset;
202 int kiss_cmd;
203 int kiss_type;
204 int kiss_port;
205 int kiss_param;
206 int kiss_param_len;
207 const char *frame_type_text;
208 char *info_buffer;
209 tvbuff_t *next_tvb = NULL;
211 info_buffer = (char *)wmem_alloc( wmem_packet_scope(), STRLEN );
212 info_buffer[0] = '\0';
214 col_set_str( pinfo->cinfo, COL_PROTOCOL, "AX.25 KISS" );
215 col_clear( pinfo->cinfo, COL_INFO );
217 /* protocol offset for the KISS header */
218 offset = 0;
220 kiss_cmd = tvb_get_guint8( tvb, offset ) & 0xff;
221 kiss_type = kiss_cmd & KISS_CMD_MASK;
222 kiss_port = (kiss_cmd & KISS_PORT_MASK) >> 4;
223 offset += KISS_HEADER_SIZE;
225 kiss_param = 0;
226 kiss_param_len = 0;
227 switch ( kiss_type )
229 case KISS_TXDELAY : kiss_param_len = 1; kiss_param = tvb_get_guint8( tvb, offset ) & 0xff; break;
230 case KISS_PERSISTENCE : kiss_param_len = 1; kiss_param = tvb_get_guint8( tvb, offset ) & 0xff; break;
231 case KISS_SLOT_TIME : kiss_param_len = 1; kiss_param = tvb_get_guint8( tvb, offset ) & 0xff; break;
232 case KISS_TXTAIL : kiss_param_len = 1; kiss_param = tvb_get_guint8( tvb, offset ) & 0xff; break;
233 case KISS_FULLDUPLEX : kiss_param_len = 1; kiss_param = tvb_get_guint8( tvb, offset ) & 0xff; break;
234 case KISS_SETHARDWARE : kiss_param_len = 1; kiss_param = tvb_get_guint8( tvb, offset ) & 0xff; break;
235 default : break;
237 frame_type_text = val_to_str(kiss_type, kiss_frame_types, "Unknown (%u)");
238 g_snprintf( info_buffer, STRLEN, "%s, Port %u", frame_type_text, kiss_port );
239 if ( kiss_param_len > 0 )
240 g_snprintf( info_buffer, STRLEN, "%s %u, Port %u", frame_type_text, kiss_param, kiss_port );
242 offset += kiss_param_len;
244 col_add_str( pinfo->cinfo, COL_INFO, info_buffer );
246 if ( parent_tree )
248 /* protocol offset for the KISS header */
249 offset = 0;
251 /* create display subtree for the protocol */
252 ti = proto_tree_add_protocol_format( parent_tree, proto_ax25_kiss, tvb, offset,
253 KISS_HEADER_SIZE + kiss_param_len,
254 "KISS: %s",
255 info_buffer
258 kiss_tree = proto_item_add_subtree( ti, ett_ax25_kiss );
260 proto_tree_add_uint( kiss_tree, hf_ax25_kiss_cmd, tvb, offset, KISS_HEADER_SIZE,
261 kiss_cmd );
262 proto_tree_add_uint( kiss_tree, hf_ax25_kiss_port, tvb, offset, KISS_HEADER_SIZE,
263 kiss_port );
264 offset += KISS_HEADER_SIZE;
266 switch ( kiss_type )
268 case KISS_DATA_FRAME : break;
269 case KISS_TXDELAY :
270 proto_tree_add_uint( kiss_tree, hf_ax25_kiss_txdelay,
271 tvb, offset, kiss_param_len, kiss_param );
272 offset += kiss_param_len;
273 break;
274 case KISS_PERSISTENCE :
275 proto_tree_add_uint( kiss_tree, hf_ax25_kiss_persistence,
276 tvb, offset, kiss_param_len, kiss_param );
277 offset += kiss_param_len;
278 break;
279 case KISS_SLOT_TIME :
280 proto_tree_add_uint( kiss_tree, hf_ax25_kiss_slottime,
281 tvb, offset, kiss_param_len, kiss_param );
282 offset += kiss_param_len;
283 break;
284 case KISS_TXTAIL :
285 proto_tree_add_uint( kiss_tree, hf_ax25_kiss_txtail,
286 tvb, offset, kiss_param_len, kiss_param );
287 offset += kiss_param_len;
288 break;
289 case KISS_FULLDUPLEX :
290 proto_tree_add_uint( kiss_tree, hf_ax25_kiss_fullduplex,
291 tvb, offset, kiss_param_len, kiss_param );
292 offset += kiss_param_len;
293 break;
294 case KISS_SETHARDWARE :
295 proto_tree_add_uint( kiss_tree, hf_ax25_kiss_sethardware,
296 tvb, offset, kiss_param_len, kiss_param );
297 offset += kiss_param_len;
298 break;
299 case KISS_RETURN : break;
300 default : break;
304 /* Call sub-dissectors here */
306 if ( kiss_type == KISS_DATA_FRAME )
308 next_tvb = tvb_new_subset_remaining( tvb, offset );
309 call_dissector( ax25_handle, next_tvb, pinfo, parent_tree );
313 void
314 proto_register_ax25_kiss(void)
316 /* Setup list of header fields */
317 static hf_register_info hf[] = {
318 { &hf_ax25_kiss_cmd,
319 { "Cmd", "ax25_kiss.cmd",
320 FT_UINT8, BASE_DEC, VALS(kiss_frame_types), KISS_CMD_MASK,
321 NULL, HFILL }
323 { &hf_ax25_kiss_port,
324 { "Port", "ax25_kiss.port",
325 FT_UINT8, BASE_DEC, NULL, KISS_PORT_MASK,
326 NULL, HFILL }
328 { &hf_ax25_kiss_txdelay,
329 { "Tx delay", "ax25_kiss.txdelay",
330 FT_UINT8, BASE_DEC, NULL, 0x0,
331 NULL, HFILL }
333 { &hf_ax25_kiss_persistence,
334 { "Persistence", "ax25_kiss.persistence",
335 FT_UINT8, BASE_DEC, NULL, 0x0,
336 NULL, HFILL }
338 { &hf_ax25_kiss_slottime,
339 { "Slot time", "ax25_kiss.slottime",
340 FT_UINT8, BASE_DEC, NULL, 0x0,
341 NULL, HFILL }
343 { &hf_ax25_kiss_txtail,
344 { "Tx tail", "ax25_kiss.txtail",
345 FT_UINT8, BASE_DEC, NULL, 0x0,
346 NULL, HFILL }
348 { &hf_ax25_kiss_fullduplex,
349 { "Full duplex", "ax25_kiss.fullduplex",
350 FT_UINT8, BASE_DEC, NULL, 0x0,
351 NULL, HFILL }
353 { &hf_ax25_kiss_sethardware,
354 { "Set hardware", "ax25_kiss.sethardware",
355 FT_UINT8, BASE_DEC, NULL, 0x0,
356 NULL, HFILL }
360 /* Setup protocol subtree array */
361 static gint *ett[] = {
362 &ett_ax25_kiss,
365 /* Register the protocol name and description */
366 proto_ax25_kiss = proto_register_protocol( "AX.25 KISS", "AX.25 KISS", "ax25_kiss" );
368 /* Register the dissector */
369 kiss_handle = register_dissector( "ax25_kiss", dissect_ax25_kiss, proto_ax25_kiss );
371 /* Required function calls to register the header fields and subtrees used */
372 proto_register_field_array( proto_ax25_kiss, hf, array_length( hf ) );
373 proto_register_subtree_array( ett, array_length( ett ) );
376 void
377 proto_reg_handoff_ax25_kiss(void)
379 dissector_add_uint( "wtap_encap", WTAP_ENCAP_AX25_KISS, kiss_handle );
381 /* only currently implemented for AX.25 */
382 ax25_handle = find_dissector( "ax25" );