summaryrefslogtreecommitdiffstats
path: root/epan/dissectors/packet-gsm_um.c
blob: a8064f9679ba5961b8a403fe26cde6c52ef5e50e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
/* packet-gsm_um.c
 * Routines for GSM Um packet disassembly
 * Duncan Salerno <duncan.salerno@googlemail.com>
 *
 * Wireshark - Network traffic analyzer
 * By Gerald Combs <gerald@wireshark.org>
 * Copyright 1998 Gerald Combs
 *
 * SPDX-License-Identifier: GPL-2.0-or-later
 */


#include "config.h"

#include <epan/packet.h>
#include <epan/prefs.h>
#include <wiretap/wtap.h>

void proto_register_gsm_um(void);
void proto_reg_handoff_gsm_um(void);

static int proto_gsm_um = -1;
static int hf_gsm_um_direction = -1;
static int hf_gsm_um_channel = -1;
static int hf_gsm_um_bsic = -1;
static int hf_gsm_um_arfcn = -1;
static int hf_gsm_um_band = -1;
static int hf_gsm_um_frequency = -1;
static int hf_gsm_um_frame = -1;
static int hf_gsm_um_error = -1;
static int hf_gsm_um_timeshift = -1;
static int hf_gsm_um_l2_pseudo_len = -1;

static gint ett_gsm_um = -1;

static dissector_handle_t gsm_um_handle;

static dissector_handle_t lapdm_handle;
static dissector_handle_t dtap_handle;

static gboolean dcs1800_gsm = TRUE;

#define	GSM_UM_L2_PSEUDO_LEN		0xfc


static void
decode_arfcn(guint16 arfcn, const char **band, guint *uplink, guint *downlink)
{
	/* Decode ARFCN to frequency using GSM 05.05 */
	if( arfcn >= 1 && arfcn <= 124 ) {
		*band = "P-GSM 900";
		*uplink = 890000 + 200 * arfcn;
		*downlink = *uplink + 45000;
	}
	else if( arfcn == 0 ) {
		*band = "E-GSM 900";
		*uplink = 890000 + 200 * arfcn;
		*downlink = *uplink + 45000;
	}
	else if( arfcn >= 975 && arfcn <= 1023 ) {
		*band = "E-GSM 900";
		*uplink = 890000 + 200 * (arfcn - 1024);
		*downlink = *uplink + 45000;
	}
	else if( arfcn >= 955 && arfcn <= 974 ) {
		*band = "R-GSM 900";
		*uplink = 890000 + 200 * (arfcn - 1024);
		*downlink = *uplink + 45000;
	}
	else if( arfcn >= 512 && arfcn <= 885 && dcs1800_gsm) {
		*band = "DCS 1800";
		*uplink = 1710200 + 200 * (arfcn - 512);
		*downlink = *uplink + 95000;
	}
	else if( arfcn >= 512 && arfcn <= 810 && !dcs1800_gsm) {
		*band = "PCS 1900";
		*uplink = 1850200 + 200 * (arfcn - 512);
		*downlink = *uplink + 80000;
	}
	else if( arfcn >= 259 && arfcn <= 293 ) {
		*band = "GSM 450";
		*uplink = 450600 + 200 * (arfcn - 259);
		*downlink = *uplink + 10000;
	}
	else if( arfcn >= 306 && arfcn <= 340 ) {
		*band = "GSM 480";
		*uplink = 479000 + 200 * (arfcn - 306);
		*downlink = *uplink + 10000;
	}
	else if( arfcn >= 128 && arfcn <= 251 ) {
		*band = "GSM 850";
		*uplink = 824200 + 200 * (arfcn - 128);
		*downlink = *uplink + 45000;
	}
	else {
		*band = "Unknown";
		*uplink = *downlink = 0;
	}
}


static int
dissect_gsm_um(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, void* data _U_)
{
	proto_tree *gsm_um_tree = NULL;
	proto_item *ti;

	col_set_str(pinfo->cinfo, COL_PROTOCOL, "GSM Um");

	if (pinfo->pseudo_header->gsm_um.uplink) {
		col_set_str(pinfo->cinfo, COL_RES_DL_DST, "BTS");
		col_set_str(pinfo->cinfo, COL_RES_DL_SRC, "MS");
	}
	else {
		switch (pinfo->pseudo_header->gsm_um.channel) {
			case GSM_UM_CHANNEL_BCCH:
			case GSM_UM_CHANNEL_CCCH:
			case GSM_UM_CHANNEL_PCH:
			case GSM_UM_CHANNEL_AGCH:
				col_set_str(pinfo->cinfo, COL_RES_DL_DST, "Broadcast");
				break;
			default:
				col_set_str(pinfo->cinfo, COL_RES_DL_DST, "MS");
				break;
		}
		col_set_str(pinfo->cinfo, COL_RES_DL_SRC, "BTS");
	}

	if (tree) {
		const char *channel;

		ti = proto_tree_add_item(tree, proto_gsm_um, tvb, 0, 0, ENC_NA);
		gsm_um_tree = proto_item_add_subtree(ti, ett_gsm_um);

		switch( pinfo->pseudo_header->gsm_um.channel ) {
			case GSM_UM_CHANNEL_BCCH: channel = "BCCH"; break;
			case GSM_UM_CHANNEL_CCCH: channel = "CCCH"; break;
			case GSM_UM_CHANNEL_PCH: channel = "PCH"; break;
			case GSM_UM_CHANNEL_AGCH: channel = "AGCH"; break;
			case GSM_UM_CHANNEL_SACCH: channel = "SACCH"; break;
			case GSM_UM_CHANNEL_FACCH: channel = "FACCH"; break;
			case GSM_UM_CHANNEL_SDCCH: channel = "SDCCH"; break;
			default: channel = "Unknown"; break;
		}

		if( pinfo->pseudo_header->gsm_um.uplink ) {
			proto_tree_add_string(gsm_um_tree, hf_gsm_um_direction, tvb, 0, 0, "Uplink");
		}
		else {
			proto_tree_add_string(gsm_um_tree, hf_gsm_um_direction, tvb, 0, 0, "Downlink");
		}

		proto_tree_add_string(gsm_um_tree, hf_gsm_um_channel, tvb, 0, 0, channel);

		/* Show the other fields, if we have them (ie. downlink, BTS->MS) */
		if( !pinfo->pseudo_header->gsm_um.uplink ) {
			const char *band;
			guint downlink, uplink;

			decode_arfcn(pinfo->pseudo_header->gsm_um.arfcn, &band, &uplink, &downlink);

			proto_tree_add_uint(gsm_um_tree, hf_gsm_um_arfcn, tvb, 0, 0,
				pinfo->pseudo_header->gsm_um.arfcn);
			proto_tree_add_string(gsm_um_tree, hf_gsm_um_band, tvb, 0, 0,
				band);
			proto_tree_add_uint_format_value(gsm_um_tree, hf_gsm_um_frequency, tvb, 0, 0,
				downlink, "%u.%03uMHz", downlink / 1000, downlink % 1000);
			proto_tree_add_uint(gsm_um_tree, hf_gsm_um_bsic, tvb, 0, 0,
				pinfo->pseudo_header->gsm_um.bsic);
			proto_tree_add_uint(gsm_um_tree, hf_gsm_um_frame, tvb, 0, 0,
				pinfo->pseudo_header->gsm_um.tdma_frame);
			proto_tree_add_uint(gsm_um_tree, hf_gsm_um_error, tvb, 0, 0,
				pinfo->pseudo_header->gsm_um.error);
			proto_tree_add_uint(gsm_um_tree, hf_gsm_um_timeshift, tvb, 0, 0,
				pinfo->pseudo_header->gsm_um.timeshift);
		}
	}

	/* TODO: If CCCH downlink could work out of PCH or AGCH by peeking at next bytes, uplink is RACH */

	switch( pinfo->pseudo_header->gsm_um.channel ) {
		case GSM_UM_CHANNEL_BCCH:
		case GSM_UM_CHANNEL_CCCH:
		case GSM_UM_CHANNEL_PCH:
		case GSM_UM_CHANNEL_AGCH:
			if( !pinfo->pseudo_header->gsm_um.uplink ) {
				tvbuff_t *next_tvb;
				guint8 pseudo_len, len_left, len_byte;

				len_left = tvb_reported_length(tvb);
				len_byte = tvb_get_guint8(tvb, 0);
				pseudo_len = len_byte >> 2;
				next_tvb = tvb_new_subset_length_caplen(tvb, 1, MIN(len_left, pseudo_len), -1);

				if (tree) {
					proto_tree_add_uint(gsm_um_tree, hf_gsm_um_l2_pseudo_len, tvb, 0, 1,
						len_byte);
				}

				/* Only dissect non-empty frames */
				if( tvb_reported_length(next_tvb) ) {
					call_dissector(dtap_handle, next_tvb, pinfo, tree);
				}
			}
			else {
				/* Either RACH, or something invalid */
				call_data_dissector(tvb, pinfo, tree);
			}
			break;
		case GSM_UM_CHANNEL_SACCH:
		case GSM_UM_CHANNEL_FACCH:
		case GSM_UM_CHANNEL_SDCCH:
			call_dissector(lapdm_handle, tvb, pinfo, tree);
			break;
		default:
			call_data_dissector(tvb, pinfo, tree);
			break;
	}
	return tvb_captured_length(tvb);
}

void
proto_register_gsm_um(void)
{
	static hf_register_info hf[] = {
		{ &hf_gsm_um_direction,
		{ "Direction",	"gsm_um.direction", FT_STRINGZ, BASE_NONE,
		  NULL, 0x0, NULL, HFILL }},

		{ &hf_gsm_um_channel,
		{ "Channel",	"gsm_um.channel", FT_STRINGZ, BASE_NONE,
		  NULL, 0x0, NULL, HFILL }},

		{ &hf_gsm_um_bsic,
		{ "BSIC",	"gsm_um.bsic", FT_UINT8, BASE_DEC,
		  NULL, 0x0, "Base station identity code", HFILL }},

		{ &hf_gsm_um_arfcn,
		{ "ARFCN",	"gsm_um.arfcn", FT_UINT16, BASE_DEC,
		  NULL, 0x0, "Absolute radio frequency channel number", HFILL }},

		{ &hf_gsm_um_band,
		{ "Band",	"gsm_um.band", FT_STRING, BASE_NONE,
		  NULL, 0x0, NULL, HFILL }},

		{ &hf_gsm_um_frequency,
		{ "Frequency",	"gsm_um.frequency", FT_UINT32, BASE_DEC,
		  NULL, 0x0, NULL, HFILL }},

		{ &hf_gsm_um_frame,
		{ "TDMA Frame",	"gsm_um.frame", FT_UINT32, BASE_DEC,
		  NULL, 0x0, NULL, HFILL }},

		{ &hf_gsm_um_error,
		{ "Error",	"gsm_um.error", FT_UINT8, BASE_DEC,
		  NULL, 0x0, NULL, HFILL }},

		{ &hf_gsm_um_timeshift,
		{ "Timeshift",	"gsm_um.timeshift", FT_UINT16, BASE_DEC,
		  NULL, 0x0, NULL, HFILL }},

		{ &hf_gsm_um_l2_pseudo_len,
		{ "L2 Pseudo Length",	"gsm_um.l2_pseudo_len", FT_UINT8, BASE_DEC,
		  NULL, GSM_UM_L2_PSEUDO_LEN, NULL, HFILL }}

	};
	static gint *ett[] = {
		&ett_gsm_um
	};
	module_t *gsm_um_module;

	proto_gsm_um = proto_register_protocol("GSM Um Interface", "GSM Um", "gsm_um");
	proto_register_field_array(proto_gsm_um, hf, array_length(hf));
	proto_register_subtree_array(ett, array_length(ett));

	gsm_um_module = prefs_register_protocol(proto_gsm_um, NULL);
	prefs_register_bool_preference(gsm_um_module, "dcs1800",
				   "Treat ARFCN 512-810 as DCS 1800 rather than PCS 1900",
				   "Treat ARFCN 512-810 as DCS 1800 rather than PCS 1900",
				   &dcs1800_gsm);

	gsm_um_handle = register_dissector("gsm_um", dissect_gsm_um, proto_gsm_um);
}

void
proto_reg_handoff_gsm_um(void)
{
	lapdm_handle = find_dissector_add_dependency("lapdm", proto_gsm_um);
	dtap_handle = find_dissector_add_dependency("gsm_a_dtap", proto_gsm_um);

	dissector_add_uint("wtap_encap", WTAP_ENCAP_GSM_UM, gsm_um_handle);
}

/*
 * Editor modelines  -  https://www.wireshark.org/tools/modelines.html
 *
 * Local variables:
 * c-basic-offset: 8
 * tab-width: 8
 * indent-tabs-mode: t
 * End:
 *
 * vi: set shiftwidth=8 tabstop=8 noexpandtab:
 * :indentSize=8:tabSize=8:noTabs=false:
 */