Keep result of [new_]register_dissector in some dissectors.
[metze/wireshark/wip.git] / epan / dissectors / packet-ax25-kiss.c
1 /* packet-ax25-kiss.c
2  *
3  * Routines for AX.25 KISS protocol dissection
4  * Copyright 2010,2012 R.W. Stearn <richard@rns-stearn.demon.co.uk>
5  *
6  * $Id$
7  *
8  * Wireshark - Network traffic analyzer
9  * By Gerald Combs <gerald@wireshark.org>
10  * Copyright 1998 Gerald Combs
11  *
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.
16  *
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.
21  *
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.
25  */
26
27 /*
28  * This dissector handles the "KISS" protocol as implemented by the
29  * Linux kernel.
30  *
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
35  *
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
43  *
44  * Data frames are prefixed with a "Data Frame" command but the port appears to
45  * be always 0.
46  *
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.
53  *
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.
58  *
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).
63  *
64  *    2           P                The next byte  is  the  persistence
65  *                                 parameter,  p, scaled to the range
66  *                                 0 - 255 with the following
67  *                                 formula:
68  *
69  *                                          P = p * 256 - 1
70  *
71  *                                 The  default  value  is  P  =  63
72  *                                 (i.e.,  p  =  0.25).
73  *
74  *    3           SlotTime         The next byte is the slot interval
75  *                                 in 10 ms units.
76  *                                 The default is 10 (i.e., 100ms).
77  *
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.
84  *
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).
89  *
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.
95  *
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.
101  *
102 */
103
104 #include "config.h"
105
106 #include <glib.h>
107
108 #include <epan/packet.h>
109 #include <epan/emem.h>
110
111 #include "packet-ax25-kiss.h"
112 #include "packet-ax25.h"
113
114 #define STRLEN  80
115
116 #define KISS_HEADER_SIZE         1 /* length of the KISS type header */
117
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
127
128 #define KISS_CMD_MASK           0x0f
129 #define KISS_PORT_MASK          0xf0
130
131 void proto_register_ax25_kiss(void);
132 void proto_reg_handoff_ax25_kiss(void);
133
134 /* Initialize the protocol and registered fields */
135 static int proto_ax25_kiss           = -1;
136
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;
145
146 /* Initialize the subtree pointers */
147 static gint ett_ax25_kiss = -1;
148
149 static dissector_handle_t kiss_handle;
150
151 /* Dissector handles - all the possibles are listed */
152 static dissector_handle_t ax25_handle;
153
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 }
164 };
165
166 void
167 capture_ax25_kiss( const guchar *pd, int offset, int len, packet_counts *ld)
168 {
169         int    l_offset;
170         guint8 kiss_cmd;
171
172         if ( ! BYTES_ARE_IN_FRAME( offset, len, KISS_HEADER_SIZE ) )
173                 {
174                 ld->other++;
175                 return;
176                 }
177
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 )
182                 {
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;
192                 }
193 }
194
195 /* Code to actually dissect the packets */
196 static void
197 dissect_ax25_kiss( tvbuff_t *tvb, packet_info *pinfo, proto_tree *parent_tree )
198 {
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         void       *saved_private_data;
210         tvbuff_t   *next_tvb = NULL;
211
212         info_buffer    = (char *)ep_alloc( STRLEN );
213         info_buffer[0] = '\0';
214
215         col_set_str( pinfo->cinfo, COL_PROTOCOL, "AX.25 KISS" );
216         col_clear( pinfo->cinfo, COL_INFO );
217
218         /* protocol offset for the KISS header */
219         offset = 0;
220
221         kiss_cmd   = tvb_get_guint8( tvb, offset ) & 0xff;
222         kiss_type  = kiss_cmd & KISS_CMD_MASK;
223         kiss_port  = (kiss_cmd & KISS_PORT_MASK) >> 4;
224         offset    += KISS_HEADER_SIZE;
225
226         kiss_param      = 0;
227         kiss_param_len  = 0;
228         switch ( kiss_type )
229                 {
230                 case KISS_TXDELAY       : kiss_param_len = 1; kiss_param = tvb_get_guint8( tvb, offset ) & 0xff; break;
231                 case KISS_PERSISTENCE   : kiss_param_len = 1; kiss_param = tvb_get_guint8( tvb, offset ) & 0xff; break;
232                 case KISS_SLOT_TIME     : kiss_param_len = 1; kiss_param = tvb_get_guint8( tvb, offset ) & 0xff; break;
233                 case KISS_TXTAIL        : kiss_param_len = 1; kiss_param = tvb_get_guint8( tvb, offset ) & 0xff; break;
234                 case KISS_FULLDUPLEX    : kiss_param_len = 1; kiss_param = tvb_get_guint8( tvb, offset ) & 0xff; break;
235                 case KISS_SETHARDWARE   : kiss_param_len = 1; kiss_param = tvb_get_guint8( tvb, offset ) & 0xff; break;
236                 default                 : break;
237                 }
238         frame_type_text = val_to_str(kiss_type, kiss_frame_types, "Unknown (%u)");
239         g_snprintf( info_buffer, STRLEN, "%s, Port %u", frame_type_text, kiss_port );
240         if ( kiss_param_len > 0 )
241                 g_snprintf( info_buffer, STRLEN, "%s %u, Port %u", frame_type_text, kiss_param, kiss_port );
242
243         offset += kiss_param_len;
244
245         col_add_str( pinfo->cinfo, COL_INFO, info_buffer );
246
247         if ( parent_tree )
248                 {
249                 /* protocol offset for the KISS header */
250                 offset = 0;
251
252                 /* create display subtree for the protocol */
253                 ti = proto_tree_add_protocol_format( parent_tree, proto_ax25_kiss, tvb, offset,
254                         KISS_HEADER_SIZE + kiss_param_len,
255                         "KISS: %s",
256                         info_buffer
257                         );
258
259                 kiss_tree = proto_item_add_subtree( ti, ett_ax25_kiss );
260
261                 proto_tree_add_uint( kiss_tree, hf_ax25_kiss_cmd,  tvb, offset, KISS_HEADER_SIZE,
262                                         kiss_cmd );
263                 proto_tree_add_uint( kiss_tree, hf_ax25_kiss_port, tvb, offset, KISS_HEADER_SIZE,
264                                         kiss_cmd );
265                 offset += KISS_HEADER_SIZE;
266
267                 switch ( kiss_type  )
268                         {
269                         case KISS_DATA_FRAME    : break;
270                         case KISS_TXDELAY       :
271                                                 proto_tree_add_uint( kiss_tree, hf_ax25_kiss_txdelay,
272                                                         tvb, offset, kiss_param_len, kiss_param );
273                                                 offset += kiss_param_len;
274                                                 break;
275                         case KISS_PERSISTENCE   :
276                                                 proto_tree_add_uint( kiss_tree, hf_ax25_kiss_persistence,
277                                                         tvb, offset, kiss_param_len, kiss_param );
278                                                 offset += kiss_param_len;
279                                                 break;
280                         case KISS_SLOT_TIME     :
281                                                 proto_tree_add_uint( kiss_tree, hf_ax25_kiss_slottime,
282                                                         tvb, offset, kiss_param_len, kiss_param );
283                                                 offset += kiss_param_len;
284                                                 break;
285                         case KISS_TXTAIL        :
286                                                 proto_tree_add_uint( kiss_tree, hf_ax25_kiss_txtail,
287                                                         tvb, offset, kiss_param_len, kiss_param );
288                                                 offset += kiss_param_len;
289                                                 break;
290                         case KISS_FULLDUPLEX    :
291                                                 proto_tree_add_uint( kiss_tree, hf_ax25_kiss_fullduplex,
292                                                         tvb, offset, kiss_param_len, kiss_param );
293                                                 offset += kiss_param_len;
294                                                 break;
295                         case KISS_SETHARDWARE   :
296                                                 proto_tree_add_uint( kiss_tree, hf_ax25_kiss_sethardware,
297                                                         tvb, offset, kiss_param_len, kiss_param );
298                                                 offset += kiss_param_len;
299                                                 break;
300                         case KISS_RETURN        : break;
301                         default                 : break;
302                         }
303
304         }
305         /* Call sub-dissectors here */
306
307         if ( kiss_type == KISS_DATA_FRAME )
308                 {
309                 saved_private_data = pinfo->private_data;
310                 next_tvb = tvb_new_subset_remaining( tvb, offset );
311                 call_dissector( ax25_handle, next_tvb, pinfo, parent_tree );
312                 pinfo->private_data = saved_private_data;
313                 }
314 }
315
316 void
317 proto_register_ax25_kiss(void)
318 {
319         /* Setup list of header fields */
320         static hf_register_info hf[] = {
321                 { &hf_ax25_kiss_cmd,
322                         { "Cmd",                        "ax25_kiss.cmd",
323                         FT_UINT8, BASE_DEC, VALS(kiss_frame_types), KISS_CMD_MASK,
324                         NULL, HFILL }
325                 },
326                 { &hf_ax25_kiss_port,
327                         { "Port",                       "ax25_kiss.port",
328                         FT_UINT8, BASE_DEC, NULL, KISS_PORT_MASK,
329                         NULL, HFILL }
330                 },
331                 { &hf_ax25_kiss_txdelay,
332                         { "Tx delay",                   "ax25_kiss.txdelay",
333                         FT_UINT8, BASE_DEC, NULL, 0x0,
334                         NULL, HFILL }
335                 },
336                 { &hf_ax25_kiss_persistence,
337                         { "Persistence",                "ax25_kiss.persistence",
338                         FT_UINT8, BASE_DEC, NULL, 0x0,
339                         NULL, HFILL }
340                 },
341                 { &hf_ax25_kiss_slottime,
342                         { "Slot time",                  "ax25_kiss.slottime",
343                         FT_UINT8, BASE_DEC, NULL, 0x0,
344                         NULL, HFILL }
345                 },
346                 { &hf_ax25_kiss_txtail,
347                         { "Tx tail",                    "ax25_kiss.txtail",
348                         FT_UINT8, BASE_DEC, NULL, 0x0,
349                         NULL, HFILL }
350                 },
351                 { &hf_ax25_kiss_fullduplex,
352                         { "Full duplex",                "ax25_kiss.fullduplex",
353                         FT_UINT8, BASE_DEC, NULL, 0x0,
354                         NULL, HFILL }
355                 },
356                 { &hf_ax25_kiss_sethardware,
357                         { "Set hardware",               "ax25_kiss.sethardware",
358                         FT_UINT8, BASE_DEC, NULL, 0x0,
359                         NULL, HFILL }
360                 },
361         };
362
363         /* Setup protocol subtree array */
364         static gint *ett[] = {
365                 &ett_ax25_kiss,
366         };
367
368         /* Register the protocol name and description */
369         proto_ax25_kiss = proto_register_protocol( "AX.25 KISS", "AX.25 KISS", "ax25_kiss" );
370
371         /* Register the dissector */
372         kiss_handle = register_dissector( "ax25_kiss", dissect_ax25_kiss, proto_ax25_kiss );
373
374         /* Required function calls to register the header fields and subtrees used */
375         proto_register_field_array( proto_ax25_kiss, hf, array_length( hf ) );
376         proto_register_subtree_array( ett, array_length( ett ) );
377 }
378
379 void
380 proto_reg_handoff_ax25_kiss(void)
381 {
382         dissector_add_uint( "wtap_encap", WTAP_ENCAP_AX25_KISS, kiss_handle );
383
384         /* only currently implemented for AX.25 */
385         ax25_handle = find_dissector( "ax25" );
386 }
387