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