aboutsummaryrefslogtreecommitdiffstats
path: root/plugins/wimax/packet-wmx.c
blob: 1cdbd97e82ee02630d8eeca4f3cf979b5151063e (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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
/* packet-wmx.c
 * WiMax Protocol and dissectors
 *
 * Copyright (c) 2007 by Intel Corporation.
 *
 * Author: Lu Pan <lu.pan@intel.com>
 *
 * Wireshark - Network traffic analyzer
 * By Gerald Combs <gerald@wireshark.org>
 * Copyright 1999 Gerald Combs
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

/* Include files */

#include "config.h"

#include <string.h>

#include <epan/packet.h>
#include <epan/prefs.h>
#include "wimax-int.h"
#include "wimax_tlv.h"
#include "wimax_utils.h"

void proto_register_wimax(void);

/* Global variables */
gint	proto_wimax = -1;
gint8	arq_enabled = 0;
gint	scheduling_service_type = 0;
gint	mac_sdu_length = 49; /* default SDU size is 49 bytes (11.13.16) */
extern	guint global_cid_max_basic;
extern	gboolean include_cor2_changes;

address bs_address = ADDRESS_INIT_NONE;


static int hf_tlv_type = -1;
static int hf_tlv_length = -1;
static int hf_tlv_length_size = -1;

#define MAX_NUM_TLVS	256
/* Global TLV array to retrieve unique subtree identifiers */
static gint ett_tlv[MAX_NUM_TLVS];

static const gchar tlv_val_1byte[] = "TLV value: %s (0x%02x)";
static const gchar tlv_val_2byte[] = "TLV value: %s (0x%04x)";
static const gchar tlv_val_3byte[] = "TLV value: %s (0x%06x)";
static const gchar tlv_val_4byte[] = "TLV value: %s (0x%08x)";
static const gchar tlv_val_5byte[] = "TLV value: %s (0x%08x...)";

/*************************************************************/
/* add_tlv_subtree()                                         */
/* Return a pointer to a proto_item of a TLV value that      */
/* already contains the type and length of the given TLV.    */
/*   tree          - the parent to which the new tree will   */
/*                   be attached                             */
/*   hfindex       - the index of the item to be attached    */
/*   tvb           - a pointer to the packet data            */
/*   start         - offset within the packet                */
/*   length        - length of this item                     */
/*   encoding      - encoding for proto_tree_add_item        */
/* return:                                                   */
/*   pointer to a proto_item                                 */
/*************************************************************/
proto_item *add_tlv_subtree(tlv_info_t *self, proto_tree *tree, int hfindex, tvbuff_t *tvb, gint start, const guint encoding)
{
	header_field_info *hf;
	proto_tree *tlv_tree;
	proto_item *tlv_item;
	gint tlv_value_length, tlv_val_offset;
	guint8 size_of_tlv_length_field;
	guint8 tlv_type;

	/* Make sure we're dealing with a valid TLV here */
	if (get_tlv_type(self) < 0)
		return tree;

	/* Retrieve the necessary TLV information */
	tlv_val_offset = get_tlv_value_offset(self);
	tlv_value_length = get_tlv_length(self);
	size_of_tlv_length_field = get_tlv_size_of_length(self);
	tlv_type = get_tlv_type(self);

	hf = proto_registrar_get_nth(hfindex);

	tlv_tree = proto_tree_add_subtree(tree, tvb, start, tlv_value_length+tlv_val_offset, ett_tlv[tlv_type], NULL, hf->name);

	proto_tree_add_uint(tlv_tree, hf_tlv_type, tvb, start, 1, tlv_type);
	if (size_of_tlv_length_field > 0) /* It is */
	{
		/* display the length of the length field TLV */
		proto_tree_add_uint(tlv_tree, hf_tlv_length_size, tvb, start+1, 1, size_of_tlv_length_field);
		/* display the TLV length */
		proto_tree_add_uint(tlv_tree, hf_tlv_length, tvb, start+2, size_of_tlv_length_field, tlv_value_length);
	} else { /* It is not */
		/* display the TLV length */
		proto_tree_add_uint(tlv_tree, hf_tlv_length, tvb, start+1, 1, tlv_value_length);
	}

	tlv_item = proto_tree_add_item(tlv_tree, hfindex, tvb, start+tlv_val_offset, tlv_value_length, encoding);

	/* Return a pointer to the value level */
	return tlv_item;
}

/*************************************************************/
/* add_tlv_subtree_no_item()                                 */
/* Return a pointer to a proto_tree of a TLV value that      */
/* already contains the type and length, but no value        */
/*   tree          - the parent to which the new tree will   */
/*                   be attached                             */
/*   hfindex       - the index of the item to be attached    */
/*   tvb           - a pointer to the packet data            */
/*   start         - offset within the packet                */
/*   length        - length of this item                     */
/* return:                                                   */
/*   pointer to a proto_tree (to then add value)             */
/*************************************************************/
proto_tree *add_tlv_subtree_no_item(tlv_info_t *self, proto_tree *tree, int hfindex, tvbuff_t *tvb, gint start)
{
	header_field_info *hf;
	proto_tree *tlv_tree;
	gint tlv_value_length, tlv_val_offset;
	guint8 size_of_tlv_length_field;
	guint8 tlv_type;

	/* Make sure we're dealing with a valid TLV here */
	if (get_tlv_type(self) < 0)
		return tree;

	/* Retrieve the necessary TLV information */
	tlv_val_offset = get_tlv_value_offset(self);
	tlv_value_length = get_tlv_length(self);
	size_of_tlv_length_field = get_tlv_size_of_length(self);
	tlv_type = get_tlv_type(self);

	hf = proto_registrar_get_nth(hfindex);

	tlv_tree = proto_tree_add_subtree(tree, tvb, start, tlv_value_length+tlv_val_offset, ett_tlv[tlv_type], NULL, hf->name);

	proto_tree_add_uint(tlv_tree, hf_tlv_type, tvb, start, 1, tlv_type);
	if (size_of_tlv_length_field > 0) /* It is */
	{
		/* display the length of the length field TLV */
		proto_tree_add_uint(tlv_tree, hf_tlv_length_size, tvb, start+1, 1, size_of_tlv_length_field);
		/* display the TLV length */
		proto_tree_add_uint(tlv_tree, hf_tlv_length, tvb, start+2, size_of_tlv_length_field, tlv_value_length);
	} else { /* It is not */
		/* display the TLV length */
		proto_tree_add_uint(tlv_tree, hf_tlv_length, tvb, start+1, 1, tlv_value_length);
	}

	/* Return a pointer to the tree level (to manually add item) */
	return tlv_tree;
}

/*************************************************************/
/* add_protocol_subtree()                                    */
/* Return a pointer to a proto_tree that already contains    */
/* the type and length of a given TLV.                       */
/*   tree          - the parent to which the new tree will   */
/*                   be attached                             */
/*   hfindex       - the index of the item to be attached    */
/*   tvb           - a pointer to the packet data            */
/*   start         - offset within the packet                */
/*   length        - length of this item                     */
/*   format        - printf style formatting string          */
/*   ...	   - arguments to format                     */
/* return:                                                   */
/*   pointer to a proto_tree                                 */
/*************************************************************/
proto_tree *add_protocol_subtree(tlv_info_t *self, gint idx, proto_tree *tree, int hfindex, tvbuff_t *tvb, gint start, gint length _U_, const char *label)
{
	/* Declare local variables */
	proto_tree *tlv_tree;
	proto_item *tlv_item;
	gint tlv_value_length, tlv_val_offset;
	guint8 size_of_tlv_length_field;
	guint8 tlv_type;
	guint32 tlv_value;
	const gchar *hex_fmt;

	/* Make sure we're dealing with a valid TLV here */
	if (get_tlv_type(self) < 0)
		return tree;

	/* Retrieve the necessary TLV information */
	tlv_val_offset = get_tlv_value_offset(self);
	tlv_value_length = get_tlv_length(self);
	size_of_tlv_length_field = get_tlv_size_of_length(self);
	tlv_type = get_tlv_type(self);

	/* display the TLV name and display the value in hex. Highlight type, length, and value. */
	tlv_item = proto_tree_add_protocol_format(tree, hfindex, tvb, start, tlv_value_length+tlv_val_offset, "%s (%u byte(s))", label, tlv_value_length);
	tlv_tree = proto_item_add_subtree(tlv_item, ett_tlv[tlv_type]);

	proto_tree_add_uint(tlv_tree, hf_tlv_type, tvb, start, 1, tlv_type);
	if (size_of_tlv_length_field > 0) /* It is */
	{
		/* display the length of the length field TLV */
		proto_tree_add_uint(tlv_tree, hf_tlv_length_size, tvb, start+1, 1, size_of_tlv_length_field);
		/* display the TLV length */
		proto_tree_add_uint(tlv_tree, hf_tlv_length, tvb, start+2, size_of_tlv_length_field, tlv_value_length);
	} else { /* It is not */
		/* display the TLV length */
		proto_tree_add_uint(tlv_tree, hf_tlv_length, tvb, start+1, 1, tlv_value_length);
	}

	/* display the TLV value and make it a subtree */
	switch (tlv_value_length)
	{
		case 1:
			tlv_value = tvb_get_guint8(tvb, start+tlv_val_offset);
			hex_fmt = tlv_val_1byte;
			break;
		case 2:
			tlv_value = tvb_get_ntohs(tvb, start+tlv_val_offset);
			hex_fmt = tlv_val_2byte;
			break;
		case 3:
			tlv_value = tvb_get_ntoh24(tvb, start+tlv_val_offset);
			hex_fmt = tlv_val_3byte;
			break;
		case 4:
			tlv_value = tvb_get_ntohl(tvb, start+tlv_val_offset);
			hex_fmt = tlv_val_4byte;
			break;
		default:
			tlv_value = tvb_get_ntohl(tvb, start+tlv_val_offset);
			hex_fmt = tlv_val_5byte;
			break;
	}
	/* Show "TLV value: " */
	tlv_tree = proto_tree_add_subtree_format(tlv_tree, tvb, start+tlv_val_offset, tlv_value_length, idx, NULL, hex_fmt, label, tlv_value);

	/* Return a pointer to the value level */
	return tlv_tree;
}



/* WiMax protocol dissector */
static int dissect_wimax(tvbuff_t *tvb _U_, packet_info *pinfo, proto_tree *tree _U_, void* data _U_)
{
	/* display the WiMax protocol name */
	col_set_str(pinfo->cinfo, COL_PROTOCOL, "WiMax");
	/* Clear out stuff in the info column */
	col_clear(pinfo->cinfo, COL_INFO);

	return tvb_captured_length(tvb);
}

gboolean is_down_link(packet_info *pinfo)
{
	if (pinfo->p2p_dir == P2P_DIR_RECV)
		return TRUE;
	if (pinfo->p2p_dir == P2P_DIR_UNKNOWN)
		if(bs_address.len && !cmp_address(&bs_address, &pinfo->src))
			return TRUE;
	return FALSE;
}


/* Register Wimax Protocol */
void proto_register_wimax(void)
{
	int i;
	module_t *wimax_module;

	static hf_register_info hf[] = {
		{ &hf_tlv_type, { "TLV type", "wmx.tlv_type", FT_UINT8, BASE_DEC, NULL, 0x0, NULL, HFILL }},
		{ &hf_tlv_length, { "TLV length", "wmx.tlv_length", FT_UINT32, BASE_DEC, NULL, 0x0, NULL, HFILL }},
		{ &hf_tlv_length_size, { "Size of TLV length field", "wmx.tlv_length_size", FT_UINT8, BASE_DEC, NULL, 0x0, NULL, HFILL }},
	};

	gint *ett_reg[MAX_NUM_TLVS];

	/* Register the WiMax protocols here */
	proto_wimax = proto_register_protocol (
		"WiMax Protocol", /* name       */
		"WiMax (wmx)",    /* short name */
		"wmx"             /* abbrev     */
		);

	proto_register_field_array(proto_wimax, hf, array_length(hf));

	/* Register the ett TLV array to retrieve unique subtree identifiers */
	for (i = 0; i < MAX_NUM_TLVS; i++)
	{
		ett_tlv[i] = -1;
		ett_reg[i] = &ett_tlv[i];
	}

	proto_register_subtree_array(ett_reg, array_length(ett_reg));

	/* Register the WiMax dissector */
	register_dissector("wmx", dissect_wimax, proto_wimax);

	wimax_module = prefs_register_protocol(proto_wimax, NULL);

	prefs_register_uint_preference(wimax_module, "basic_cid_max",
				       "Maximum Basic CID",
				       "Set the maximum Basic CID"
				       " used in the Wimax decoder"
				       " (if other than the default of 320)."
				       "  Note: The maximum Primary CID is"
				       " double the maximum Basic CID.",
				       10, &global_cid_max_basic);

	prefs_register_bool_preference(wimax_module, "corrigendum_2_version",
				       "Corrigendum 2 Version",
				       "Set to TRUE to use the Corrigendum"
				       " 2 version of Wimax message decoding."
				       " Set to FALSE to use the 802.16e-2005"
				       "  version.",
				       &include_cor2_changes);
	prefs_register_obsolete_preference(wimax_module, "wimax.basic_cid_max");
	prefs_register_obsolete_preference(wimax_module, "wimax.corrigendum_2_version");

	/*
	 * Call sub-registrations in the correct order because they depend
	 * on proto_register_wimax() being run first.
	 */
	wimax_proto_register_wimax_cdma();
	wimax_proto_register_wimax_compact_dlmap_ie();
	wimax_proto_register_wimax_compact_ulmap_ie();
	wimax_proto_register_wimax_fch();
	wimax_proto_register_wimax_ffb();
	wimax_proto_register_wimax_hack();
	wimax_proto_register_wimax_harq_map();
	wimax_proto_register_wimax_pdu();
	wimax_proto_register_wimax_phy_attributes();
	wimax_proto_register_wimax_utility_decoders();
	wimax_proto_register_mac_header_generic();
	wimax_proto_register_mac_header_type_1();
	wimax_proto_register_mac_header_type_2();
}

/*
 * If we're going to give the register routines for the above files special
 * names to ensure that they're called in the above order in the above
 * routine, we have to do the same with their handoff routines, if they
 * have any - that's the way the registration generation stuff now works.
 */
void proto_reg_handoff_wimax(void)
{
	wimax_proto_reg_handoff_wimax_pdu();
	wimax_proto_reg_handoff_mac_header_generic();
}

/*
 * Editor modelines  -  http://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:
 */