Maybe the problem is that the compilers used on AIX weren't defining
[obnox/wireshark/wip.git] / packet-bssap.c
1 /* packet-bssap.c
2  * Routines for Base Station Subsystem Application Part (BSSAP/BSAP) dissection
3  * Specifications from 3GPP2 (www.3gpp2.org) and 3GPP (www.3gpp.org)
4  *      IOS 4.0.1 (BSAP)
5  *      GSM 08.06 (BSSAP)
6  *
7  * Copyright 2003, Michael Lum <mlum [AT] telostech.com>
8  * In association with Telos Technology Inc.
9  *
10  * $Id: packet-bssap.c,v 1.3 2003/10/28 18:08:52 guy Exp $
11  *
12  * Ethereal - Network traffic analyzer
13  * By Gerald Combs <gerald@ethereal.com>
14  * Copyright 1998 Gerald Combs
15  *
16  * This program is free software; you can redistribute it and/or
17  * modify it under the terms of the GNU General Public License
18  * as published by the Free Software Foundation; either version 2
19  * of the License, or (at your option) any later version.
20  *
21  * This program is distributed in the hope that it will be useful,
22  * but WITHOUT ANY WARRANTY; without even the implied warranty of
23  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24  * GNU General Public License for more details.
25  *
26  * You should have received a copy of the GNU General Public License
27  * along with this program; if not, write to the Free Software
28  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
29  */
30
31 #ifdef HAVE_CONFIG_H
32 # include "config.h"
33 #endif
34
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <gmodule.h>
38
39 #ifdef HAVE_SYS_TYPES_H
40 # include <sys/types.h>
41 #endif
42
43 #ifdef HAVE_NETINET_IN_H
44 # include <netinet/in.h>
45 #endif
46
47 #include <string.h>
48
49 #include "epan/packet.h"
50 #include "prefs.h"
51 #include "packet-bssap.h"
52
53 #define BSSAP 0
54 #define BSAP  1
55
56 #define BSSAP_OR_BSAP_DEFAULT BSSAP
57
58 #define PDU_TYPE_OFFSET 0
59 #define PDU_TYPE_LENGTH 1
60
61 /* Same as below but with names typed out */
62 static const value_string bssap_pdu_type_values[] = {
63     { BSSAP_PDU_TYPE_BSSMAP,    "BSS Management" },
64     { BSSAP_PDU_TYPE_DTAP,      "Direct Transfer" },
65     { 0,                        NULL } };
66
67 static const value_string bsap_pdu_type_values[] = {
68     { BSSAP_PDU_TYPE_BSSMAP,    "BS Management" },
69     { BSSAP_PDU_TYPE_DTAP,      "Direct Transfer" },
70     { 0,                        NULL } };
71
72 /* Same as above but in acronym for (for the Info column) */
73 static const value_string bssap_pdu_type_acro_values[] = {
74     { BSSAP_PDU_TYPE_BSSMAP,    "BSSMAP" },
75     { BSSAP_PDU_TYPE_DTAP,      "DTAP" },
76     { 0,                        NULL } };
77
78 /* Same as above but in acronym for (for the Info column) */
79 static const value_string bsap_pdu_type_acro_values[] = {
80     { BSSAP_PDU_TYPE_BSSMAP,    "BSMAP" },
81     { BSSAP_PDU_TYPE_DTAP,      "DTAP" },
82     { 0,                        NULL } };
83
84 #define PARAMETER_DLCI          0x00
85 #define PARAMETER_LENGTH        0x01
86 #define PARAMETER_DATA          0x02
87
88 #define DLCI_LENGTH             1
89 #define LENGTH_LENGTH           1
90 #define DATA_LENGTH             1
91
92 #define CC_MASK                 0xc0
93 #define SPARE_MASK              0x38
94 #define SAPI_MASK               0x07
95
96 static const value_string bssap_cc_values[] = {
97     { 0x00,             "not further specified" },
98     { 0x80,             "FACCH or SDCCH" },
99     { 0xc0,             "SACCH" },
100     { 0,                NULL } };
101
102 static const value_string bsap_cc_values[] = {
103     { 0x00,             "default for TIA/EIA/IS-2000" },
104     { 0,                NULL } };
105
106 static const value_string bssap_sapi_values[] = {
107     { 0x00,             "RR/MM/CC" },
108     { 0x03,             "SMS" },
109     { 0,                NULL } };
110
111 static const value_string bsap_sapi_values[] = {
112     { 0x00,             "Not used" },
113     { 0,                NULL } };
114
115
116 /* Initialize the protocol and registered fields */
117 static int proto_bssap = -1;
118 static int hf_bssap_pdu_type = -1;
119 static int hf_bsap_pdu_type = -1;
120 static int hf_bssap_dlci_cc = -1;
121 static int hf_bsap_dlci_cc = -1;
122 static int hf_bssap_dlci_spare = -1;
123 static int hf_bsap_dlci_rsvd = -1;
124 static int hf_bssap_dlci_sapi = -1;
125 static int hf_bsap_dlci_sapi = -1;
126 static int hf_bssap_length = -1;
127
128 /* Initialize the subtree pointers */
129 static gint ett_bssap = -1;
130 static gint ett_bssap_dlci = -1;
131
132 static dissector_handle_t data_handle;
133
134 static dissector_table_t bssap_dissector_table;
135 static dissector_table_t bsap_dissector_table;
136
137 /*
138  * Keep track of pdu_type so we can call appropriate sub-dissector
139  */
140 static guint8   pdu_type = 0xFF;
141
142 static guint    bssap_or_bsap_global = BSSAP_OR_BSAP_DEFAULT;
143
144
145 /* FORWARD DECLARATIONS */
146
147 void proto_reg_handoff_bssap(void);
148
149 static void
150 dissect_bssap_unknown_message(tvbuff_t *message_tvb, proto_tree *bssap_tree)
151 {
152     guint32     message_length;
153
154     message_length = tvb_length(message_tvb);
155
156     proto_tree_add_text(bssap_tree, message_tvb, 0, message_length,
157         "Unknown message (%u byte%s)",
158         message_length, plurality(message_length, "", "s"));
159 }
160
161 static void
162 dissect_bssap_unknown_param(tvbuff_t *tvb, proto_tree *tree, guint8 type, guint16 length)
163 {
164     proto_tree_add_text(tree, tvb, 0, length,
165         "Unknown parameter 0x%x (%u byte%s)",
166         type, length, plurality(length, "", "s"));
167 }
168
169 static void
170 dissect_bssap_data_param(tvbuff_t *tvb, packet_info *pinfo,
171                         proto_tree *bssap_tree, proto_tree *tree)
172 {
173     if ((pdu_type <= 0x01))
174     {
175         if (bssap_or_bsap_global == BSSAP)
176         {
177             /* BSSAP */
178             if (dissector_try_port(bssap_dissector_table, pdu_type, tvb, pinfo, tree)) return;
179         }
180         else
181         {
182             /* BSAP */
183             if (dissector_try_port(bsap_dissector_table, pdu_type, tvb, pinfo, tree)) return;
184         }
185     }
186
187     /* No sub-dissection occured, treat it as raw data */
188     call_dissector(data_handle, tvb, pinfo, bssap_tree);
189 }
190
191 static void
192 dissect_bssap_dlci_param(tvbuff_t *tvb, proto_tree *tree, guint8 length)
193 {
194     proto_item  *dlci_item = 0;
195     proto_tree  *dlci_tree = 0;
196     guint8      oct;
197
198     dlci_item =
199         proto_tree_add_text(tree, tvb, 0, length,
200             "Data Link Connection Identifier");
201
202     dlci_tree = proto_item_add_subtree(dlci_item, ett_bssap_dlci);
203
204     oct = tvb_get_guint8(tvb, 0);
205
206     if (bssap_or_bsap_global == BSSAP)
207     {
208         proto_tree_add_uint(dlci_tree, hf_bssap_dlci_cc, tvb, 0, length, oct);
209         proto_tree_add_uint(dlci_tree, hf_bssap_dlci_spare, tvb, 0, length, oct);
210         proto_tree_add_uint(dlci_tree, hf_bssap_dlci_sapi, tvb, 0, length, oct);
211     }
212     else
213     {
214         proto_tree_add_uint(dlci_tree, hf_bsap_dlci_cc, tvb, 0, length, oct);
215         proto_tree_add_uint(dlci_tree, hf_bsap_dlci_rsvd, tvb, 0, length, oct);
216         proto_tree_add_uint(dlci_tree, hf_bsap_dlci_sapi, tvb, 0, length, oct);
217     }
218 }
219
220 static void
221 dissect_bssap_length_param(tvbuff_t *tvb, proto_tree *tree, guint8 length)
222 {
223     guint8      data_length;
224
225     data_length = tvb_get_guint8(tvb, 0);
226     proto_tree_add_uint(tree, hf_bssap_length, tvb, 0, length, data_length);
227 }
228
229 /*
230  * Dissect a parameter given its type, offset into tvb, and length.
231  */
232 static guint16
233 dissect_bssap_parameter(tvbuff_t *tvb, packet_info *pinfo, proto_tree *bssap_tree,
234                        proto_tree *tree, guint8 parameter_type, guint8 offset,
235                        guint16 parameter_length)
236 {
237     tvbuff_t *parameter_tvb;
238
239     parameter_tvb = tvb_new_subset(tvb, offset, parameter_length, parameter_length);
240
241     switch (parameter_type)
242     {
243     case PARAMETER_DLCI:
244         dissect_bssap_dlci_param(parameter_tvb, bssap_tree, parameter_length);
245         break;
246
247     case PARAMETER_LENGTH:
248         dissect_bssap_length_param(parameter_tvb, bssap_tree, parameter_length);
249         break;
250
251     case PARAMETER_DATA:
252         dissect_bssap_data_param(parameter_tvb, pinfo, bssap_tree, tree);
253         break;
254
255     default:
256         dissect_bssap_unknown_param(parameter_tvb, bssap_tree, parameter_type,
257             parameter_length);
258         break;
259     }
260
261     return(parameter_length);
262 }
263
264 static guint16
265 dissect_bssap_var_parameter(tvbuff_t *tvb, packet_info *pinfo,
266                                 proto_tree *bssap_tree, proto_tree *tree,
267                                 guint8 parameter_type, guint8 offset)
268 {
269     guint16     parameter_length;
270     guint8      length_length;
271
272     parameter_length = tvb_get_guint8(tvb, offset);
273     length_length = LENGTH_LENGTH;
274
275     offset += length_length;
276
277     dissect_bssap_parameter(tvb, pinfo, bssap_tree, tree, parameter_type,
278         offset, parameter_length);
279
280     return(parameter_length + length_length);
281 }
282
283 static void
284 dissect_bssap_message(tvbuff_t *tvb, packet_info *pinfo, proto_tree *bssap_tree,
285                      proto_tree *tree)
286 {
287     guint8      offset = 0;
288
289     /*
290      * Extract the PDU type
291      */
292     pdu_type = tvb_get_guint8(tvb, PDU_TYPE_OFFSET);
293     offset = PDU_TYPE_LENGTH;
294
295     if (bssap_tree)
296     {
297         /*
298          * add the message type to the protocol tree
299          */
300         proto_tree_add_uint(bssap_tree,
301             (bssap_or_bsap_global == BSSAP) ? hf_bssap_pdu_type : hf_bsap_pdu_type,
302             tvb, PDU_TYPE_OFFSET, PDU_TYPE_LENGTH, pdu_type);
303     }
304
305     /* Starting a new message dissection */
306
307     switch (pdu_type)
308     {
309     case BSSAP_PDU_TYPE_BSSMAP:
310         offset += dissect_bssap_parameter(tvb, pinfo, bssap_tree, tree,
311                                      PARAMETER_LENGTH, offset,
312                                      LENGTH_LENGTH);
313         offset += dissect_bssap_var_parameter(tvb, pinfo, bssap_tree, tree,
314                                     PARAMETER_DATA,
315                                     (offset - LENGTH_LENGTH));
316         break;
317
318     case BSSAP_PDU_TYPE_DTAP:
319         offset += dissect_bssap_parameter(tvb, pinfo, bssap_tree, tree,
320                                      PARAMETER_DLCI,
321                                      offset, DLCI_LENGTH);
322         offset += dissect_bssap_parameter(tvb, pinfo, bssap_tree, tree,
323                                      PARAMETER_LENGTH, offset,
324                                      LENGTH_LENGTH);
325         offset += dissect_bssap_var_parameter(tvb, pinfo, bssap_tree, tree,
326                                     PARAMETER_DATA,
327                                     (offset - LENGTH_LENGTH));
328         break;
329
330     default:
331         if (check_col(pinfo->cinfo, COL_INFO))
332         {
333             col_append_fstr(pinfo->cinfo, COL_INFO, "%s ",
334                 val_to_str(pdu_type, ((bssap_or_bsap_global == BSSAP) ?
335                     bssap_pdu_type_acro_values : bsap_pdu_type_acro_values),
336                     "Unknown"));
337
338         }
339
340         dissect_bssap_unknown_message(tvb, bssap_tree);
341         break;
342     }
343 }
344
345 static void
346 dissect_bssap(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
347 {
348     proto_item  *bssap_item;
349     proto_tree  *bssap_tree = NULL;
350
351     /*
352      * Make entry in the Protocol column on summary display
353      */
354     if (check_col(pinfo->cinfo, COL_PROTOCOL))
355     {
356         col_set_str(pinfo->cinfo, COL_PROTOCOL, ((bssap_or_bsap_global == BSSAP) ? "BSSAP" : "BSAP"));
357     }
358
359     if (tree)
360     {
361         /*
362          * create the bssap protocol tree
363          */
364         proto_tree_add_item_hidden(tree, proto_bssap, tvb, 0, -1, FALSE);
365         bssap_item = proto_tree_add_text(tree, tvb, 0, -1, (bssap_or_bsap_global == BSSAP) ? "BSSAP" : "BSAP");
366         bssap_tree = proto_item_add_subtree(bssap_item, ett_bssap);
367     }
368
369     /* dissect the message */
370
371     dissect_bssap_message(tvb, pinfo, bssap_tree, tree);
372 }
373
374 static gboolean
375 dissect_bssap_heur(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
376 {
377     /* Is it a BSSAP/BSAP packet?
378      *    If octet_1 == 0x00 and octet_2 == length(tvb) - 2
379      * or if octet_1 == 0x01 and octet_3 == length(tvb) - 3
380      * then we'll assume it is a bssap packet
381      */
382     switch (tvb_get_guint8(tvb, 0))
383     {
384     case 0x00:
385         if (tvb_get_guint8(tvb, 1) != (tvb_length(tvb) - 2)) { return(FALSE); }
386         break;
387
388     case 0x01:
389         if (tvb_get_guint8(tvb, 2) != (tvb_length(tvb) - 3)) { return(FALSE); }
390         break;
391
392     default:
393         return(FALSE);
394     }
395
396     dissect_bssap(tvb, pinfo, tree);
397
398     return(TRUE);
399 }
400
401 /* Register the protocol with Ethereal */
402 void
403 proto_register_bssap(void)
404 {
405     module_t    *bssap_module;
406
407     /* Setup list of header fields */
408     static hf_register_info hf[] = {
409         { &hf_bssap_pdu_type,
410             { "Message Type", "bssap.pdu_type",
411                 FT_UINT8, BASE_HEX, VALS(bssap_pdu_type_values), 0x0,
412                 "", HFILL}},
413         { &hf_bsap_pdu_type,
414             { "Message Type", "bsap.pdu_type",
415                 FT_UINT8, BASE_HEX, VALS(bsap_pdu_type_values), 0x0,
416                 "", HFILL}},
417         { &hf_bssap_dlci_cc,
418             { "Control Channel", "bssap.dlci.cc",
419                 FT_UINT8, BASE_HEX, VALS(bssap_cc_values), CC_MASK,
420                 "", HFILL}},
421         { &hf_bsap_dlci_cc,
422             { "Control Channel", "bsap.dlci.cc",
423                 FT_UINT8, BASE_HEX, VALS(bsap_cc_values), CC_MASK,
424                 "", HFILL}},
425         { &hf_bssap_dlci_spare,
426             { "Spare", "bssap.dlci.spare",
427                 FT_UINT8, BASE_HEX, NULL, SPARE_MASK,
428                 "", HFILL}},
429         { &hf_bsap_dlci_rsvd,
430             { "Reserved", "bsap.dlci.rsvd",
431                 FT_UINT8, BASE_HEX, NULL, SPARE_MASK,
432                 "", HFILL}},
433         { &hf_bssap_dlci_sapi,
434             { "SAPI", "bssap.dlci.sapi",
435                 FT_UINT8, BASE_HEX, VALS(bssap_sapi_values), SAPI_MASK,
436                 "", HFILL}},
437         { &hf_bsap_dlci_sapi,
438             { "SAPI", "bsap.dlci.sapi",
439                 FT_UINT8, BASE_HEX, VALS(bsap_sapi_values), SAPI_MASK,
440                 "", HFILL}},
441         { &hf_bssap_length,
442             { "Length", "bssap.length",
443                 FT_UINT8, BASE_DEC, NULL, 0x0,
444                 "", HFILL}},
445     };
446
447     /* Setup protocol subtree array */
448     static gint *ett[] = {
449         &ett_bssap,
450         &ett_bssap_dlci,
451     };
452
453     static enum_val_t bssap_or_bsap_options[] = {
454         { "BSSAP",              BSSAP },
455         { "BSAP",               BSAP  },
456         { NULL,                 0 }
457     };
458
459
460     /* Register the protocol name and description */
461     proto_bssap = proto_register_protocol("BSSAP/BSAP", "BSSAP", "bssap");
462
463     /* Required function calls to register the header fields and subtrees used */
464     proto_register_field_array(proto_bssap, hf, array_length(hf));
465     proto_register_subtree_array(ett, array_length(ett));
466
467     bssap_module = prefs_register_protocol(proto_bssap, proto_reg_handoff_bssap);
468
469     prefs_register_enum_preference(bssap_module,
470         "bsap_or_bssap",
471         "Identify to sub-dissector as",
472         "For the sake of sub-dissectors registering to accept data from the BSSAP/BSAP dissector, this defines whether it is identified as BSSAP or BSAP.",
473         &bssap_or_bsap_global,
474         bssap_or_bsap_options,
475         FALSE);
476
477     bssap_dissector_table = register_dissector_table("bssap.pdu_type", "BSSAP Message Type", FT_UINT8, BASE_DEC);
478     bsap_dissector_table = register_dissector_table("bsap.pdu_type", "BSAP Message Type", FT_UINT8, BASE_DEC);
479 }
480
481 void
482 proto_reg_handoff_bssap(void)
483 {
484     static gboolean bssap_prefs_initialized = FALSE;
485
486
487     if (!bssap_prefs_initialized)
488     {
489         heur_dissector_add("sccp", dissect_bssap_heur, proto_bssap);
490
491         bssap_prefs_initialized = TRUE;
492     }
493
494     data_handle = find_dissector("data");
495 }