Fix memory leaks involving tvb_get_string[z]().
[obnox/wireshark/wip.git] / epan / dissectors / packet-gsm_ipa.c
1 /* packet-gsm_ipa.c
2  * Routines for packet dissection of ip.access GSM over IP
3  * Copyright 2009 by Harald Welte <laforge@gnumonks.org>
4  * Copyright 2009, 2010 by Holger Hans Peter Freyther <zecke@selfish.org>
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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
25  */
26
27 #ifdef HAVE_CONFIG_H
28 # include "config.h"
29 #endif
30
31 #include <glib.h>
32
33 #include <epan/packet.h>
34 #include <epan/ipproto.h>
35 #include <epan/prefs.h>
36
37 /* http://www.iana.org/assignments/service-names-port-numbers/service-names-port-numbers.xml
38  *
39  * exlm-agent     3002
40  * cgms           3003
41  * ii-admin       3006
42  * vrml-multi-use 4200-4299
43  * commplex-main  5000
44  */
45 #define IPA_TCP_PORTS "3002,3003,3006,4249,4250,5000"
46 #define IPA_UDP_PORTS "3006"
47
48 static dissector_handle_t ipa_handle;
49 static range_t *global_ipa_tcp_ports = NULL;
50 static range_t *global_ipa_udp_ports = NULL;
51 static gboolean global_ipa_in_root = FALSE;
52 static gboolean global_ipa_in_info = FALSE;
53
54 /* Initialize the protocol and registered fields */
55 static int proto_ipa = -1;
56 static int proto_ipaccess = -1;
57
58 static int hf_ipa_data_len = -1;
59 static int hf_ipa_protocol = -1;
60 static int hf_ipa_hsl_debug = -1;
61 static int hf_ipa_osmo_proto = -1;
62 static int hf_ipa_osmo_ctrl_data = -1;
63
64 static int hf_ipaccess_msgtype = -1;
65 static int hf_ipaccess_attr_tag = -1;
66 static int hf_ipaccess_attr_string = -1;
67
68 /* Initialize the subtree pointers */
69 static gint ett_ipa = -1;
70 static gint ett_ipaccess = -1;
71
72 enum {
73         SUB_OML,
74         SUB_RSL,
75         SUB_SCCP,
76         SUB_MGCP,
77 /*      SUB_IPACCESS, */
78         SUB_DATA,
79
80         SUB_MAX
81 };
82
83 static dissector_handle_t sub_handles[SUB_MAX];
84 static dissector_table_t osmo_dissector_table;
85
86
87 #define ABISIP_RSL_MAX  0x20
88 #define HSL_DEBUG       0xdd
89 #define OSMO_EXT        0xee
90 #define IPA_MGCP        0xfc
91 #define AIP_SCCP        0xfd
92 #define ABISIP_IPACCESS 0xfe
93 #define ABISIP_OML      0xff
94 #define IPAC_PROTO_EXT_CTRL     0x00
95 #define IPAC_PROTO_EXT_MGCP     0x01
96
97 static const value_string ipa_protocol_vals[] = {
98         { 0x00,         "RSL" },
99         { 0xdd,         "HSL Debug" },
100         { 0xee,         "OSMO EXT" },
101         { 0xfc,         "MGCP (old)" },
102         { 0xfd,         "SCCP" },
103         { 0xfe,         "IPA" },
104         { 0xff,         "OML" },
105         { 0,            NULL }
106 };
107
108 static const value_string ipaccess_msgtype_vals[] = {
109         { 0x00,         "PING?" },
110         { 0x01,         "PONG!" },
111         { 0x04,         "IDENTITY REQUEST" },
112         { 0x05,         "IDENTITY RESPONSE" },
113         { 0x06,         "IDENTITY ACK" },
114         { 0x07,         "IDENTITY NACK" },
115         { 0x08,         "PROXY REQUEST" },
116         { 0x09,         "PROXY ACK" },
117         { 0x0a,         "PROXY NACK" },
118         { 0,            NULL }
119 };
120
121 static const value_string ipaccess_idtag_vals[] = {
122         { 0x00,         "Serial Number" },
123         { 0x01,         "Unit Name" },
124         { 0x02,         "Location" },
125         { 0x03,         "Unit Type" },
126         { 0x04,         "Equipment Version" },
127         { 0x05,         "Software Version" },
128         { 0x06,         "IP Address" },
129         { 0x07,         "MAC Address" },
130         { 0x08,         "Unit ID" },
131         { 0,            NULL }
132 };
133
134 static const value_string ipa_osmo_proto_vals[] = {
135         { 0x00,         "CTRL" },
136         { 0x01,         "MGCP" },
137         { 0x02,         "LAC" },
138         { 0x03,         "SMSC" },
139         { 0,            NULL }
140 };
141
142
143 static gint
144 dissect_ipa_attr(tvbuff_t *tvb, int base_offs, proto_tree *tree)
145 {
146         guint8 len, attr_type;
147
148         int offset = base_offs;
149
150         while (tvb_reported_length_remaining(tvb, offset) > 0) {
151                 attr_type = tvb_get_guint8(tvb, offset);
152
153                 switch (attr_type) {
154                 case 0x00:      /* a string prefixed by its length */
155                         len = tvb_get_guint8(tvb, offset+1);
156                         proto_tree_add_item(tree, hf_ipaccess_attr_tag,
157                                             tvb, offset+2, 1, ENC_BIG_ENDIAN);
158                         proto_tree_add_item(tree, hf_ipaccess_attr_string,
159                                             tvb, offset+3, len-1, ENC_ASCII|ENC_NA);
160                         break;
161                 case 0x01:      /* a single-byte reqest for a certain attr */
162                         len = 0;
163                         proto_tree_add_item(tree, hf_ipaccess_attr_tag,
164                                             tvb, offset+1, 1, ENC_BIG_ENDIAN);
165                         break;
166                 default:
167                         len = 0;
168                         proto_tree_add_text(tree, tvb, offset+1, 1,
169                                             "unknown attribute type 0x%02x",
170                                             attr_type);
171                         break;
172                 };
173                 offset += len + 2;
174         };
175         return offset;
176 }
177
178 /* Dissect an ip.access specific message */
179 static gint
180 dissect_ipaccess(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
181 {
182         proto_item *ti;
183         proto_tree *ipaccess_tree;
184         guint8 msg_type;
185
186         msg_type = tvb_get_guint8(tvb, 0);
187
188         col_append_fstr(pinfo->cinfo, COL_INFO, "%s ",
189                         val_to_str(msg_type, ipaccess_msgtype_vals,
190                                    "unknown 0x%02x"));
191         if (tree) {
192                 ti = proto_tree_add_item(tree, proto_ipaccess, tvb, 0, -1, ENC_NA);
193                 ipaccess_tree = proto_item_add_subtree(ti, ett_ipaccess);
194                 proto_tree_add_item(ipaccess_tree, hf_ipaccess_msgtype,
195                                     tvb, 0, 1, ENC_BIG_ENDIAN);
196                 switch (msg_type) {
197                 case 4:
198                 case 5:
199                         dissect_ipa_attr(tvb, 1, ipaccess_tree);
200                         break;
201                 }
202         }
203
204         return 1;
205 }
206
207 /* Dissect the osmocom extension header */
208 static gint
209 dissect_osmo(tvbuff_t *tvb, packet_info *pinfo, proto_tree *ipatree, proto_tree *tree)
210 {
211         tvbuff_t *next_tvb;
212         guint8 osmo_proto;
213
214         osmo_proto = tvb_get_guint8(tvb, 0);
215
216         col_append_fstr(pinfo->cinfo, COL_INFO, "%s ",
217                         val_to_str(osmo_proto, ipa_osmo_proto_vals,
218                                    "unknown 0x%02x"));
219         if (ipatree) {
220                 proto_tree_add_item(ipatree, hf_ipa_osmo_proto,
221                                     tvb, 0, 1, ENC_BIG_ENDIAN);
222         }
223
224         next_tvb = tvb_new_subset_remaining(tvb, 1);
225
226         /* Call any subdissectors that registered for this protocol */
227         if (dissector_try_uint(osmo_dissector_table, osmo_proto, next_tvb, pinfo, tree))
228                 return 1;
229
230         /* Fallback to the standard MGCP dissector */
231         if (osmo_proto == IPAC_PROTO_EXT_MGCP) {
232                 call_dissector(sub_handles[SUB_MGCP], next_tvb, pinfo, tree);
233                 return 1;
234         /* Simply display the CTRL data as text */
235         } else if (osmo_proto == IPAC_PROTO_EXT_CTRL) {
236                 if (tree) {
237                         proto_tree_add_item(tree, hf_ipa_osmo_ctrl_data, next_tvb, 0, -1, ENC_ASCII|ENC_NA);
238                 }
239                 return 1;
240         }
241
242         call_dissector(sub_handles[SUB_DATA], next_tvb, pinfo, tree);
243
244         return 1;
245 }
246
247
248
249 /* Code to actually dissect the packets */
250 static void
251 dissect_ipa(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
252 {
253         gint remaining;
254         gint header_length = 3;
255         int offset = 0;
256
257         col_set_str(pinfo->cinfo, COL_PROTOCOL, "IPA");
258         col_clear(pinfo->cinfo, COL_INFO);
259
260         while ((remaining = tvb_reported_length_remaining(tvb, offset)) > 0) {
261                 proto_item *ti;
262                 proto_tree *ipa_tree = NULL;
263                 guint16 len, msg_type;
264                 tvbuff_t *next_tvb;
265
266                 len = tvb_get_ntohs(tvb, offset);
267                 msg_type = tvb_get_guint8(tvb, offset+2);
268
269                 col_append_fstr(pinfo->cinfo, COL_INFO, "%s ",
270                                 val_to_str(msg_type, ipa_protocol_vals,
271                                            "unknown 0x%02x"));
272
273                 /*
274                  * The IPA header is different depending on the transport protocol.
275                  * With UDP there seems to be a fourth byte for the IPA header.
276                  * We attempt to detect this by checking if the length from the
277                  * header + four bytes of the IPA header equals the remaining size.
278                  */
279                 if ((pinfo->ipproto == IP_PROTO_UDP) && (len + 4 == remaining)) {
280                         header_length++;
281                 }
282
283                 if (tree) {
284                         ti = proto_tree_add_protocol_format(tree, proto_ipa,
285                                         tvb, offset, len+header_length,
286                                         "IPA protocol ip.access, type: %s",
287                                         val_to_str(msg_type, ipa_protocol_vals,
288                                                    "unknown 0x%02x"));
289                         ipa_tree = proto_item_add_subtree(ti, ett_ipa);
290                         proto_tree_add_item(ipa_tree, hf_ipa_data_len,
291                                             tvb, offset, 2, ENC_BIG_ENDIAN);
292                         proto_tree_add_item(ipa_tree, hf_ipa_protocol,
293                                             tvb, offset+2, 1, ENC_BIG_ENDIAN);
294                 }
295
296                 next_tvb = tvb_new_subset(tvb, offset+header_length, len, len);
297
298                 switch (msg_type) {
299                 case ABISIP_OML:
300                         /* hand this off to the standard A-bis OML dissector */
301                         if (sub_handles[SUB_OML])
302                                 call_dissector(sub_handles[SUB_OML], next_tvb,
303                                                  pinfo, tree);
304                         break;
305                 case ABISIP_IPACCESS:
306                         dissect_ipaccess(next_tvb, pinfo, tree);
307                         break;
308                 case AIP_SCCP:
309                         /* hand this off to the standard SCCP dissector */
310                         call_dissector(sub_handles[SUB_SCCP], next_tvb, pinfo, tree);
311                         break;
312                 case IPA_MGCP:
313                         /* hand this off to the standard MGCP dissector */
314                         call_dissector(sub_handles[SUB_MGCP], next_tvb, pinfo, tree);
315                         break;
316                 case OSMO_EXT:
317                         dissect_osmo(next_tvb, pinfo, ipa_tree, tree);
318                         break;
319                 case HSL_DEBUG:
320                         if (tree) {
321                                 proto_tree_add_item(ipa_tree, hf_ipa_hsl_debug,
322                                                     next_tvb, 0, len, ENC_ASCII|ENC_NA);
323                                 if (global_ipa_in_root == TRUE)
324                                         proto_tree_add_item(tree, hf_ipa_hsl_debug,
325                                                             next_tvb, 0, len, ENC_ASCII|ENC_NA);
326                         }
327                         if (global_ipa_in_info == TRUE)
328                                 col_append_fstr(pinfo->cinfo, COL_INFO, "%s ",
329                                                 tvb_get_ephemeral_stringz(next_tvb, 0, NULL));
330                         break;
331                 default:
332                         if (msg_type < ABISIP_RSL_MAX) {
333                                 /* hand this off to the standard A-bis RSL dissector */
334                                 call_dissector(sub_handles[SUB_RSL], next_tvb, pinfo, tree);
335                         }
336                         break;
337                 }
338                 offset += len + header_length;
339         }
340 }
341
342 void proto_reg_handoff_gsm_ipa(void);
343
344 void proto_register_ipa(void)
345 {
346         module_t *ipa_module;
347
348         static hf_register_info hf[] = {
349                 {&hf_ipa_data_len,
350                  {"DataLen", "ipa.data_len",
351                   FT_UINT16, BASE_DEC, NULL, 0x0,
352                   "The length of the data (in bytes)", HFILL}
353                  },
354                 {&hf_ipa_protocol,
355                  {"Protocol", "ipa.protocol",
356                   FT_UINT8, BASE_HEX, VALS(ipa_protocol_vals), 0x0,
357                   "The IPA Sub-Protocol", HFILL}
358                  },
359                 {&hf_ipa_hsl_debug,
360                  {"Debug Message", "ipa.hsl_debug",
361                   FT_STRING, BASE_NONE, NULL, 0,
362                   "Hay Systems Limited debug message", HFILL}
363                 },
364                 {&hf_ipa_osmo_proto,
365                  {"Osmo ext protocol", "ipa.osmo.protocol",
366                   FT_UINT8, BASE_HEX, VALS(ipa_osmo_proto_vals), 0x0,
367                   "The osmo extension protocol", HFILL}
368                 },
369
370                 {&hf_ipa_osmo_ctrl_data,
371                  {"CTRL data", "ipa.ctrl.data",
372                   FT_STRING, BASE_NONE, NULL, 0x0,
373                   "Control interface data", HFILL}
374                 },
375
376         };
377         static hf_register_info hf_ipa[] = {
378                 {&hf_ipaccess_msgtype,
379                  {"MessageType", "ipaccess.msg_type",
380                   FT_UINT8, BASE_HEX, VALS(ipaccess_msgtype_vals), 0x0,
381                   "Type of ip.access messsage", HFILL}
382                  },
383                 {&hf_ipaccess_attr_tag,
384                  {"Tag", "ipaccess.attr_tag",
385                   FT_UINT8, BASE_HEX, VALS(ipaccess_idtag_vals), 0x0,
386                   "Attribute Tag", HFILL}
387                  },
388                 {&hf_ipaccess_attr_string,
389                  {"String", "ipaccess.attr_string",
390                   FT_STRING, BASE_NONE, NULL, 0x0,
391                   "String attribute", HFILL}
392                  },
393         };
394
395         static gint *ett[] = {
396                 &ett_ipa,
397                 &ett_ipaccess,
398         };
399
400         proto_ipa =
401             proto_register_protocol("GSM over IP protocol as used by ip.access",
402                                     "GSM over IP", "gsm_ipa");
403         proto_ipaccess =
404             proto_register_protocol("GSM over IP ip.access CCM sub-protocol",
405                                     "IPA", "ipaccess");
406
407         proto_register_field_array(proto_ipa, hf, array_length(hf));
408         proto_register_field_array(proto_ipaccess, hf_ipa, array_length(hf_ipa));
409         proto_register_subtree_array(ett, array_length(ett));
410
411         register_dissector("gsm_ipa", dissect_ipa, proto_ipa);
412
413         /* Register table for subdissectors */
414         osmo_dissector_table = register_dissector_table("ipa.osmo.protocol",
415                                         "ip.access Protocol", FT_UINT8, BASE_DEC);
416
417
418         range_convert_str(&global_ipa_tcp_ports, IPA_TCP_PORTS, MAX_TCP_PORT);
419         range_convert_str(&global_ipa_udp_ports, IPA_UDP_PORTS, MAX_UDP_PORT);
420         ipa_module = prefs_register_protocol(proto_ipa,
421                                              proto_reg_handoff_gsm_ipa);
422
423         prefs_register_range_preference(ipa_module, "tcp_ports",
424                                         "GSM IPA TCP Port(s)",
425                                         "Set the port(s) for ip.access IPA"
426                                         " (default: " IPA_TCP_PORTS ")",
427                                         &global_ipa_tcp_ports, MAX_TCP_PORT);
428         prefs_register_range_preference(ipa_module, "udp_ports",
429                                         "GSM IPA UDP Port(s)",
430                                         "Set the port(s) for ip.access IPA"
431                                         " (default: " IPA_UDP_PORTS ")",
432                                         &global_ipa_udp_ports, MAX_UDP_PORT);
433
434         prefs_register_bool_preference(ipa_module, "hsl_debug_in_root_tree",
435                                         "HSL Debug messages in root protocol tree",
436                                         NULL, &global_ipa_in_root);
437         prefs_register_bool_preference(ipa_module, "hsl_debug_in_info",
438                                         "HSL Debug messages in INFO column",
439                                         NULL, &global_ipa_in_info);
440 }
441
442 static void ipa_tcp_delete_callback(guint32 port)
443 {
444         if (port)
445                 dissector_delete_uint("tcp.port", port, ipa_handle);
446 }
447
448 static void ipa_udp_delete_callback(guint32 port)
449 {
450         if (port)
451                 dissector_delete_uint("udp.port", port, ipa_handle);
452 }
453
454 static void ipa_tcp_add_callback(guint32 port)
455 {
456         if (port)
457                 dissector_add_uint("tcp.port", port, ipa_handle);
458 }
459
460 static void ipa_udp_add_callback(guint32 port)
461 {
462         if (port)
463                 dissector_add_uint("udp.port", port, ipa_handle);
464 }
465
466 void proto_reg_handoff_gsm_ipa(void)
467 {
468         static gboolean ipa_initialized = FALSE;
469         static range_t *ipa_tcp_ports, *ipa_udp_ports;
470
471         if (!ipa_initialized) {
472                 sub_handles[SUB_RSL] = find_dissector("gsm_abis_rsl");
473                 sub_handles[SUB_OML] = find_dissector("gsm_abis_oml");
474                 sub_handles[SUB_SCCP] = find_dissector("sccp");
475                 sub_handles[SUB_MGCP] = find_dissector("mgcp");
476                 sub_handles[SUB_DATA] = find_dissector("data");
477
478                 ipa_handle = create_dissector_handle(dissect_ipa, proto_ipa);
479                 ipa_initialized = TRUE;
480         } else {
481                 range_foreach(ipa_tcp_ports, ipa_tcp_delete_callback);
482                 g_free(ipa_tcp_ports);
483                 range_foreach(ipa_udp_ports, ipa_udp_delete_callback);
484                 g_free(ipa_udp_ports);
485         }
486
487         ipa_tcp_ports = range_copy(global_ipa_tcp_ports);
488         ipa_udp_ports = range_copy(global_ipa_udp_ports);
489
490         range_foreach(ipa_tcp_ports, ipa_tcp_add_callback);
491         range_foreach(ipa_udp_ports, ipa_udp_add_callback);
492 }