Enable Lua tcp tap userdata.
[obnox/wireshark/wip.git] / epan / dissectors / packet-gsm_um.c
1 /* packet-gsm_um.c
2  * Routines for GSM Um packet disassembly
3  * Duncan Salerno <duncan.salerno@googlemail.com>
4  *
5  * $Id$
6  *
7  * Wireshark - Network traffic analyzer
8  * By Gerald Combs <gerald@wireshark.org>
9  * Copyright 1998 Gerald Combs
10  *
11  * This program is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU General Public License
13  * as published by the Free Software Foundation; either version 2
14  * of the License, or (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software
23  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
24  */
25
26
27 #ifdef HAVE_CONFIG_H
28 # include "config.h"
29 #endif
30
31 #include <stdio.h>
32 #include <glib.h>
33 #include <epan/packet.h>
34 #include <epan/prefs.h>
35 #include <epan/circuit.h>
36
37 static int proto_gsm_um = -1;
38 static int hf_gsm_um_direction = -1;
39 static int hf_gsm_um_channel = -1;
40 static int hf_gsm_um_bsic = -1;
41 static int hf_gsm_um_arfcn = -1;
42 static int hf_gsm_um_frame = -1;
43 static int hf_gsm_um_error = -1;
44 static int hf_gsm_um_timeshift = -1;
45 static int hf_gsm_um_l2_pseudo_len = -1;
46
47 static gint ett_gsm_um = -1;
48
49 static dissector_handle_t lapdm_handle;
50 static dissector_handle_t dtap_handle;
51 static dissector_handle_t data_handle;
52
53 static gboolean dcs1800_gsm = TRUE;
54
55 #define GSM_UM_L2_PSEUDO_LEN            0xfc
56
57
58 static void
59 decode_arfcn(guint16 arfcn, const char **band, guint *uplink, guint *downlink)
60 {
61         /* Decode ARFCN to frequency using GSM 05.05 */
62         if( arfcn >= 1 && arfcn <= 124 ) {
63                 *band = "P-GSM 900";
64                 *uplink = 890000 + 200 * arfcn;
65                 *downlink = *uplink + 45000;
66         }
67         else if( arfcn == 0 ) {
68                 *band = "E-GSM 900";
69                 *uplink = 890000 + 200 * arfcn;
70                 *downlink = *uplink + 45000;
71         }
72         else if( arfcn >= 975 && arfcn <= 1023 ) {
73                 *band = "E-GSM 900";
74                 *uplink = 890000 + 200 * (arfcn - 1024);
75                 *downlink = *uplink + 45000;
76         }
77         else if( arfcn >= 955 && arfcn <= 1023 ) {
78                 *band = "R-GSM 900";
79                 *uplink = 890000 + 200 * (arfcn - 1024);
80                 *downlink = *uplink + 45000;
81         }
82         else if( arfcn >= 512 && arfcn <= 885 && dcs1800_gsm) {
83                 *band = "DCS 1800";
84                 *uplink = 1710200 + 200 * (arfcn - 512);
85                 *downlink = *uplink + 95000;
86         }
87         else if( arfcn >= 512 && arfcn <= 810 && !dcs1800_gsm) {
88                 *band = "PCS 1900";
89                 *uplink = 1850200 + 200 * (arfcn - 512);
90                 *downlink = *uplink + 80000;
91         }
92         else if( arfcn >= 259 && arfcn <= 293 ) {
93                 *band = "GSM 450";
94                 *uplink = 450600 + 200 * (arfcn - 259);
95                 *downlink = *uplink + 10000;
96         }
97         else if( arfcn >= 306 && arfcn <= 340 ) {
98                 *band = "GSM 480";
99                 *uplink = 479000 + 200 * (arfcn - 306);
100                 *downlink = *uplink + 10000;
101         }
102         else if( arfcn >= 128 && arfcn <= 251 ) {
103                 *band = "GSM 850";
104                 *uplink = 824200 + 200 * (arfcn - 128);
105                 *downlink = *uplink + 45000;
106         }
107         else {
108                 *band = "Unknown";
109                 *uplink = *downlink = 0;
110         }
111 }
112
113
114 static void
115 dissect_gsm_um(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
116 {
117         proto_tree *gsm_um_tree = NULL;
118         proto_item *ti;
119
120         if (check_col(pinfo->cinfo, COL_PROTOCOL))
121                 col_set_str(pinfo->cinfo, COL_PROTOCOL, "GSM Um");
122
123         if (pinfo->pseudo_header->gsm_um.uplink) {
124                 if (check_col(pinfo->cinfo, COL_RES_DL_DST))
125                         col_set_str(pinfo->cinfo, COL_RES_DL_DST, "BTS");
126                 if (check_col(pinfo->cinfo, COL_RES_DL_SRC))
127                         col_set_str(pinfo->cinfo, COL_RES_DL_SRC, "MS");
128         }
129         else {
130                 switch (pinfo->pseudo_header->gsm_um.channel) {
131                         case GSM_UM_CHANNEL_BCCH:
132                         case GSM_UM_CHANNEL_CCCH:
133                         case GSM_UM_CHANNEL_PCH:
134                         case GSM_UM_CHANNEL_AGCH:
135                                 if (check_col(pinfo->cinfo, COL_RES_DL_DST))
136                                     col_set_str(pinfo->cinfo, COL_RES_DL_DST, "Broadcast");
137                                 break;
138                         default:
139                                 if (check_col(pinfo->cinfo, COL_RES_DL_DST))
140                                     col_set_str(pinfo->cinfo, COL_RES_DL_DST, "MS");
141                                 break;
142                 }
143                 if (check_col(pinfo->cinfo, COL_RES_DL_SRC))
144                     col_set_str(pinfo->cinfo, COL_RES_DL_SRC, "BTS");
145         }
146
147         if (tree) {
148                 const char *channel;
149
150                 ti = proto_tree_add_item(tree, proto_gsm_um, tvb, 0, 0, FALSE);
151                 gsm_um_tree = proto_item_add_subtree(ti, ett_gsm_um);
152
153                 switch( pinfo->pseudo_header->gsm_um.channel ) {
154                         case GSM_UM_CHANNEL_BCCH: channel = "BCCH"; break;
155                         case GSM_UM_CHANNEL_CCCH: channel = "CCCH"; break;
156                         case GSM_UM_CHANNEL_PCH: channel = "PCH"; break;
157                         case GSM_UM_CHANNEL_AGCH: channel = "AGCH"; break;
158                         case GSM_UM_CHANNEL_SACCH: channel = "SACCH"; break;
159                         case GSM_UM_CHANNEL_FACCH: channel = "FACCH"; break;
160                         case GSM_UM_CHANNEL_SDCCH: channel = "SDCCH"; break;
161                         default: channel = "Unknown"; break;
162                 }
163
164                 if( pinfo->pseudo_header->gsm_um.uplink ) {
165                         proto_tree_add_string(gsm_um_tree, hf_gsm_um_direction, tvb, 0, 0, "Uplink");
166                 }
167                 else {
168                         proto_tree_add_string(gsm_um_tree, hf_gsm_um_direction, tvb, 0, 0, "Downlink");
169                 }
170
171                 proto_tree_add_string(gsm_um_tree, hf_gsm_um_channel, tvb, 0, 0, channel);
172
173                 /* Show the other fields, if we have them (ie. downlink, BTS->MS) */
174                 if( !pinfo->pseudo_header->gsm_um.uplink ) {
175                         const char *band;
176                         guint downlink, uplink;
177
178                         decode_arfcn(pinfo->pseudo_header->gsm_um.arfcn, &band, &uplink, &downlink);
179
180                         proto_tree_add_uint(gsm_um_tree, hf_gsm_um_arfcn, tvb, 0, 0,
181                                 pinfo->pseudo_header->gsm_um.arfcn);
182                         proto_tree_add_text(gsm_um_tree, tvb, 0, 0,
183                                 "Band: %s, Frequency: %u.%03uMHz", band,
184                                 downlink / 1000, downlink % 1000);
185                         proto_tree_add_uint(gsm_um_tree, hf_gsm_um_bsic, tvb, 0, 0,
186                                 pinfo->pseudo_header->gsm_um.bsic);
187                         proto_tree_add_uint(gsm_um_tree, hf_gsm_um_frame, tvb, 0, 0,
188                                 pinfo->pseudo_header->gsm_um.tdma_frame);
189                         proto_tree_add_uint(gsm_um_tree, hf_gsm_um_error, tvb, 0, 0,
190                                 pinfo->pseudo_header->gsm_um.error);
191                         proto_tree_add_uint(gsm_um_tree, hf_gsm_um_timeshift, tvb, 0, 0,
192                                 pinfo->pseudo_header->gsm_um.timeshift);
193                 }
194         }
195
196         /* TODO: If CCCH downlink could work out of PCH or AGCH by peeking at next bytes, uplink is RACH */
197
198         switch( pinfo->pseudo_header->gsm_um.channel ) {
199                 case GSM_UM_CHANNEL_BCCH:
200                 case GSM_UM_CHANNEL_CCCH:
201                 case GSM_UM_CHANNEL_PCH:
202                 case GSM_UM_CHANNEL_AGCH:
203                         if( !pinfo->pseudo_header->gsm_um.uplink ) {
204                                 tvbuff_t *next_tvb;
205                                 guint8 pseudo_len, len_left, len_byte;
206
207                                 len_left = tvb_length(tvb);
208                                 len_byte = tvb_get_guint8(tvb, 0);
209                                 pseudo_len = len_byte >> 2;
210                                 next_tvb = tvb_new_subset(tvb, 1, MIN(len_left, pseudo_len), -1);
211
212                                 if (tree) {
213                                         proto_tree_add_uint(gsm_um_tree, hf_gsm_um_l2_pseudo_len, tvb, 0, 1,
214                                                 len_byte);
215                                 }
216
217                                 /* Only dissect non-empty frames */
218                                 if( tvb_length(next_tvb) ) {
219                                         call_dissector(dtap_handle, next_tvb, pinfo, tree);
220                                 }
221                         }
222                         else {
223                                 /* Either RACH, or something invalid */
224                                 call_dissector(data_handle, tvb, pinfo, tree);
225                         }
226                         break;
227                 case GSM_UM_CHANNEL_SACCH:
228                 case GSM_UM_CHANNEL_FACCH:
229                 case GSM_UM_CHANNEL_SDCCH:
230                         call_dissector(lapdm_handle, tvb, pinfo, tree);
231                         break;
232                 default:
233                         call_dissector(data_handle, tvb, pinfo, tree);
234                         break;
235         }
236 }
237
238 void
239 proto_register_gsm_um(void)
240 {
241         static hf_register_info hf[] = {
242                 { &hf_gsm_um_direction,
243                 { "Direction",  "gsm_um.direction", FT_STRINGZ, BASE_NONE,
244                   NULL, 0x0, NULL, HFILL }},
245
246                 { &hf_gsm_um_channel,
247                 { "Channel",    "gsm_um.channel", FT_STRINGZ, BASE_NONE,
248                   NULL, 0x0, NULL, HFILL }},
249
250                 { &hf_gsm_um_bsic,
251                 { "BSIC",       "gsm_um.bsic", FT_UINT8, BASE_DEC,
252                   NULL, 0x0, "Base station identity code", HFILL }},
253
254                 { &hf_gsm_um_arfcn,
255                 { "ARFCN",      "gsm_um.arfcn", FT_UINT16, BASE_DEC,
256                   NULL, 0x0, "Absolute radio frequency channel number", HFILL }},
257
258                 { &hf_gsm_um_frame,
259                 { "TDMA Frame", "gsm_um.frame", FT_UINT32, BASE_DEC,
260                   NULL, 0x0, NULL, HFILL }},
261
262                 { &hf_gsm_um_error,
263                 { "Error",      "gsm_um.error", FT_UINT8, BASE_DEC,
264                   NULL, 0x0, NULL, HFILL }},
265
266                 { &hf_gsm_um_timeshift,
267                 { "Timeshift",  "gsm_um.timeshift", FT_UINT16, BASE_DEC,
268                   NULL, 0x0, NULL, HFILL }},
269
270                 { &hf_gsm_um_l2_pseudo_len,
271                 { "L2 Pseudo Length",   "gsm_um.l2_pseudo_len", FT_UINT8, BASE_DEC,
272                   NULL, GSM_UM_L2_PSEUDO_LEN, NULL, HFILL }}
273
274         };
275         static gint *ett[] = {
276                 &ett_gsm_um
277         };
278         module_t *gsm_um_module;
279
280         proto_gsm_um = proto_register_protocol("GSM Um Interface", "GSM Um", "gsm_um");
281         proto_register_field_array(proto_gsm_um, hf, array_length(hf));
282         proto_register_subtree_array(ett, array_length(ett));
283
284         gsm_um_module = prefs_register_protocol(proto_gsm_um, NULL);
285         prefs_register_bool_preference(gsm_um_module, "dcs1800",
286                                    "Treat ARFCN 512-810 as DCS 1800 rather than PCS 1900",
287                                    "Treat ARFCN 512-810 as DCS 1800 rather than PCS 1900",
288                                    &dcs1800_gsm);
289
290 }
291
292 void
293 proto_reg_handoff_gsm_um(void)
294 {
295         dissector_handle_t gsm_um_handle;
296
297         lapdm_handle = find_dissector("lapdm");
298         dtap_handle = find_dissector("gsm_a_dtap");
299         data_handle = find_dissector("data");
300
301         gsm_um_handle = create_dissector_handle(dissect_gsm_um, proto_gsm_um);
302
303         dissector_add("wtap_encap", WTAP_ENCAP_GSM_UM, gsm_um_handle);
304 }
305