Move the GSM SMS dissection to a dedicated subdissector (currently still within
[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.9 2003/12/17 23:54:44 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 static void
146 dissect_bssap_unknown_message(tvbuff_t *message_tvb, proto_tree *bssap_tree)
147 {
148     guint32     message_length;
149
150     message_length = tvb_length(message_tvb);
151
152     proto_tree_add_text(bssap_tree, message_tvb, 0, message_length,
153         "Unknown message (%u byte%s)",
154         message_length, plurality(message_length, "", "s"));
155 }
156
157 static void
158 dissect_bssap_unknown_param(tvbuff_t *tvb, proto_tree *tree, guint8 type, guint16 length)
159 {
160     proto_tree_add_text(tree, tvb, 0, length,
161         "Unknown parameter 0x%x (%u byte%s)",
162         type, length, plurality(length, "", "s"));
163 }
164
165 static void
166 dissect_bssap_data_param(tvbuff_t *tvb, packet_info *pinfo,
167                         proto_tree *bssap_tree, proto_tree *tree)
168 {
169     if ((pdu_type <= 0x01))
170     {
171         if (bssap_or_bsap_global == BSSAP)
172         {
173             /* BSSAP */
174             if (dissector_try_port(bssap_dissector_table, pdu_type, tvb, pinfo, tree)) return;
175         }
176         else
177         {
178             /* BSAP */
179             if (dissector_try_port(bsap_dissector_table, pdu_type, tvb, pinfo, tree)) return;
180         }
181     }
182
183     /* No sub-dissection occured, treat it as raw data */
184     call_dissector(data_handle, tvb, pinfo, bssap_tree);
185 }
186
187 static void
188 dissect_bssap_dlci_param(tvbuff_t *tvb, proto_tree *tree, guint16 length)
189 {
190     proto_item  *dlci_item = 0;
191     proto_tree  *dlci_tree = 0;
192     guint8      oct;
193
194     dlci_item =
195         proto_tree_add_text(tree, tvb, 0, length,
196             "Data Link Connection Identifier");
197
198     dlci_tree = proto_item_add_subtree(dlci_item, ett_bssap_dlci);
199
200     oct = tvb_get_guint8(tvb, 0);
201
202     if (bssap_or_bsap_global == BSSAP)
203     {
204         proto_tree_add_uint(dlci_tree, hf_bssap_dlci_cc, tvb, 0, length, oct);
205         proto_tree_add_uint(dlci_tree, hf_bssap_dlci_spare, tvb, 0, length, oct);
206         proto_tree_add_uint(dlci_tree, hf_bssap_dlci_sapi, tvb, 0, length, oct);
207     }
208     else
209     {
210         proto_tree_add_uint(dlci_tree, hf_bsap_dlci_cc, tvb, 0, length, oct);
211         proto_tree_add_uint(dlci_tree, hf_bsap_dlci_rsvd, tvb, 0, length, oct);
212         proto_tree_add_uint(dlci_tree, hf_bsap_dlci_sapi, tvb, 0, length, oct);
213     }
214 }
215
216 static void
217 dissect_bssap_length_param(tvbuff_t *tvb, proto_tree *tree, guint16 length)
218 {
219     guint8      data_length;
220
221     data_length = tvb_get_guint8(tvb, 0);
222     proto_tree_add_uint(tree, hf_bssap_length, tvb, 0, length, data_length);
223 }
224
225 /*
226  * Dissect a parameter given its type, offset into tvb, and length.
227  */
228 static guint16
229 dissect_bssap_parameter(tvbuff_t *tvb, packet_info *pinfo, proto_tree *bssap_tree,
230                        proto_tree *tree, guint8 parameter_type, gint offset,
231                        guint16 parameter_length)
232 {
233     tvbuff_t *parameter_tvb;
234
235     parameter_tvb = tvb_new_subset(tvb, offset, parameter_length, parameter_length);
236
237     switch (parameter_type)
238     {
239     case PARAMETER_DLCI:
240         dissect_bssap_dlci_param(parameter_tvb, bssap_tree, parameter_length);
241         break;
242
243     case PARAMETER_LENGTH:
244         dissect_bssap_length_param(parameter_tvb, bssap_tree, parameter_length);
245         break;
246
247     case PARAMETER_DATA:
248         dissect_bssap_data_param(parameter_tvb, pinfo, bssap_tree, tree);
249         break;
250
251     default:
252         dissect_bssap_unknown_param(parameter_tvb, bssap_tree, parameter_type,
253             parameter_length);
254         break;
255     }
256
257     return(parameter_length);
258 }
259
260 static guint16
261 dissect_bssap_var_parameter(tvbuff_t *tvb, packet_info *pinfo,
262                                 proto_tree *bssap_tree, proto_tree *tree,
263                                 guint8 parameter_type, gint offset)
264 {
265     guint16     parameter_length;
266     guint8      length_length;
267
268     parameter_length = tvb_get_guint8(tvb, offset);
269     length_length = LENGTH_LENGTH;
270
271     offset += length_length;
272
273     dissect_bssap_parameter(tvb, pinfo, bssap_tree, tree, parameter_type,
274         offset, parameter_length);
275
276     return(parameter_length + length_length);
277 }
278
279 static void
280 dissect_bssap_message(tvbuff_t *tvb, packet_info *pinfo, proto_tree *bssap_tree,
281                      proto_tree *tree)
282 {
283     gint        offset = 0;
284
285     /*
286      * Extract the PDU type
287      */
288     pdu_type = tvb_get_guint8(tvb, PDU_TYPE_OFFSET);
289     offset = PDU_TYPE_LENGTH;
290
291     if (bssap_tree)
292     {
293         /*
294          * add the message type to the protocol tree
295          */
296         proto_tree_add_uint(bssap_tree,
297             (bssap_or_bsap_global == BSSAP) ? hf_bssap_pdu_type : hf_bsap_pdu_type,
298             tvb, PDU_TYPE_OFFSET, PDU_TYPE_LENGTH, pdu_type);
299     }
300
301     /* Starting a new message dissection */
302
303     switch (pdu_type)
304     {
305     case BSSAP_PDU_TYPE_BSSMAP:
306         offset += dissect_bssap_parameter(tvb, pinfo, bssap_tree, tree,
307                                      PARAMETER_LENGTH, offset,
308                                      LENGTH_LENGTH);
309         offset += dissect_bssap_var_parameter(tvb, pinfo, bssap_tree, tree,
310                                     PARAMETER_DATA,
311                                     (offset - LENGTH_LENGTH));
312         break;
313
314     case BSSAP_PDU_TYPE_DTAP:
315         offset += dissect_bssap_parameter(tvb, pinfo, bssap_tree, tree,
316                                      PARAMETER_DLCI,
317                                      offset, DLCI_LENGTH);
318         offset += dissect_bssap_parameter(tvb, pinfo, bssap_tree, tree,
319                                      PARAMETER_LENGTH, offset,
320                                      LENGTH_LENGTH);
321         offset += dissect_bssap_var_parameter(tvb, pinfo, bssap_tree, tree,
322                                     PARAMETER_DATA,
323                                     (offset - LENGTH_LENGTH));
324         break;
325
326     default:
327         if (check_col(pinfo->cinfo, COL_INFO))
328         {
329             col_append_fstr(pinfo->cinfo, COL_INFO, "%s ",
330                 val_to_str(pdu_type, ((bssap_or_bsap_global == BSSAP) ?
331                     bssap_pdu_type_acro_values : bsap_pdu_type_acro_values),
332                     "Unknown"));
333
334         }
335
336         dissect_bssap_unknown_message(tvb, bssap_tree);
337         break;
338     }
339 }
340
341 static void
342 dissect_bssap(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
343 {
344     proto_item  *bssap_item;
345     proto_tree  *bssap_tree = NULL;
346
347     /*
348      * Make entry in the Protocol column on summary display
349      */
350     if (check_col(pinfo->cinfo, COL_PROTOCOL))
351     {
352         col_set_str(pinfo->cinfo, COL_PROTOCOL, ((bssap_or_bsap_global == BSSAP) ? "BSSAP" : "BSAP"));
353     }
354
355     /*
356      * create the bssap protocol tree
357      */
358     proto_tree_add_item_hidden(tree, proto_bssap, tvb, 0, -1, FALSE);
359     bssap_item = proto_tree_add_text(tree, tvb, 0, -1, (bssap_or_bsap_global == BSSAP) ? "BSSAP" : "BSAP");
360     bssap_tree = proto_item_add_subtree(bssap_item, ett_bssap);
361
362     /* dissect the message */
363
364     dissect_bssap_message(tvb, pinfo, bssap_tree, tree);
365 }
366
367 static gboolean
368 dissect_bssap_heur(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
369 {
370     /* Is it a BSSAP/BSAP packet?
371      *    If octet_1 == 0x00 and octet_2 == length(tvb) - 2
372      * or if octet_1 == 0x01 and octet_3 == length(tvb) - 3
373      * then we'll assume it is a bssap packet
374      */
375     switch (tvb_get_guint8(tvb, 0))
376     {
377     case 0x00:
378         if (tvb_get_guint8(tvb, 1) != (tvb_length(tvb) - 2)) { return(FALSE); }
379         break;
380
381     case 0x01:
382         if (tvb_get_guint8(tvb, 2) != (tvb_length(tvb) - 3)) { return(FALSE); }
383         break;
384
385     default:
386         return(FALSE);
387     }
388
389     dissect_bssap(tvb, pinfo, tree);
390
391     return(TRUE);
392 }
393
394 /* Register the protocol with Ethereal */
395 void
396 proto_register_bssap(void)
397 {
398     module_t    *bssap_module;
399
400     /* Setup list of header fields */
401     static hf_register_info hf[] = {
402         { &hf_bssap_pdu_type,
403             { "Message Type", "bssap.pdu_type",
404                 FT_UINT8, BASE_HEX, VALS(bssap_pdu_type_values), 0x0,
405                 "", HFILL}},
406         { &hf_bsap_pdu_type,
407             { "Message Type", "bsap.pdu_type",
408                 FT_UINT8, BASE_HEX, VALS(bsap_pdu_type_values), 0x0,
409                 "", HFILL}},
410         { &hf_bssap_dlci_cc,
411             { "Control Channel", "bssap.dlci.cc",
412                 FT_UINT8, BASE_HEX, VALS(bssap_cc_values), CC_MASK,
413                 "", HFILL}},
414         { &hf_bsap_dlci_cc,
415             { "Control Channel", "bsap.dlci.cc",
416                 FT_UINT8, BASE_HEX, VALS(bsap_cc_values), CC_MASK,
417                 "", HFILL}},
418         { &hf_bssap_dlci_spare,
419             { "Spare", "bssap.dlci.spare",
420                 FT_UINT8, BASE_HEX, NULL, SPARE_MASK,
421                 "", HFILL}},
422         { &hf_bsap_dlci_rsvd,
423             { "Reserved", "bsap.dlci.rsvd",
424                 FT_UINT8, BASE_HEX, NULL, SPARE_MASK,
425                 "", HFILL}},
426         { &hf_bssap_dlci_sapi,
427             { "SAPI", "bssap.dlci.sapi",
428                 FT_UINT8, BASE_HEX, VALS(bssap_sapi_values), SAPI_MASK,
429                 "", HFILL}},
430         { &hf_bsap_dlci_sapi,
431             { "SAPI", "bsap.dlci.sapi",
432                 FT_UINT8, BASE_HEX, VALS(bsap_sapi_values), SAPI_MASK,
433                 "", HFILL}},
434         { &hf_bssap_length,
435             { "Length", "bssap.length",
436                 FT_UINT8, BASE_DEC, NULL, 0x0,
437                 "", HFILL}},
438     };
439
440     /* Setup protocol subtree array */
441     static gint *ett[] = {
442         &ett_bssap,
443         &ett_bssap_dlci,
444     };
445
446     static enum_val_t bssap_or_bsap_options[] = {
447         { "BSSAP",              BSSAP },
448         { "BSAP",               BSAP  },
449         { NULL,                 0 }
450     };
451
452
453     /* Register the protocol name and description */
454     proto_bssap = proto_register_protocol("BSSAP/BSAP", "BSSAP", "bssap");
455
456     /* Required function calls to register the header fields and subtrees used */
457     proto_register_field_array(proto_bssap, hf, array_length(hf));
458     proto_register_subtree_array(ett, array_length(ett));
459
460     bssap_module = prefs_register_protocol(proto_bssap, NULL);
461
462     prefs_register_enum_preference(bssap_module,
463         "bsap_or_bssap",
464         "Identify to sub-dissector as",
465         "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.",
466         &bssap_or_bsap_global,
467         bssap_or_bsap_options,
468         FALSE);
469
470     bssap_dissector_table = register_dissector_table("bssap.pdu_type", "BSSAP Message Type", FT_UINT8, BASE_DEC);
471     bsap_dissector_table = register_dissector_table("bsap.pdu_type", "BSAP Message Type", FT_UINT8, BASE_DEC);
472 }
473
474 void
475 proto_reg_handoff_bssap(void)
476 {
477     heur_dissector_add("sccp", dissect_bssap_heur, proto_bssap);
478
479     data_handle = find_dissector("data");
480 }