summaryrefslogtreecommitdiffstats
path: root/epan/dissectors/packet-v5dl.c
blob: 2066c1a395862a8f06ccb343c460dc043732b2b1 (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
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
/* packet-v5dl.c
 * Routines for V5 data link frame disassembly
 * Rolf Fiedler <rolf.fiedler@innoventif.de> using the LAPD code of
 * Gilbert Ramirez <gram@alumni.rice.edu>
 *
 * Wireshark - Network traffic analyzer
 * By Gerald Combs <gerald@wireshark.org>
 * Copyright 1998
 *
 * SPDX-License-Identifier: GPL-2.0-or-later
 */
/*
 * V5 Data Link Layer
 *
 * V5 references:
 * ETS 300 324-1
 * ETS 300 347-1
 *
 */

#include "config.h"

#include <epan/packet.h>
#include <epan/expert.h>
#include <epan/xdlc.h>

void proto_register_v5dl(void);

static int proto_v5dl = -1;
static int hf_v5dl_direction = -1;
/* static int hf_v5dl_address = -1; */
static int hf_v5dl_ef = -1;
static int hf_v5dl_eah = -1;
static int hf_v5dl_cr = -1;
static int hf_v5dl_ea1 = -1;
static int hf_v5dl_eal = -1;
static int hf_v5dl_ea2 = -1;
static int hf_v5dl_control = -1;
static int hf_v5dl_n_r = -1;
static int hf_v5dl_n_s = -1;
static int hf_v5dl_p = -1;
static int hf_v5dl_p_ext = -1;
static int hf_v5dl_f = -1;
static int hf_v5dl_f_ext = -1;
static int hf_v5dl_s_ftype = -1;
static int hf_v5dl_u_modifier_cmd = -1;
static int hf_v5dl_u_modifier_resp = -1;
static int hf_v5dl_ftype_i = -1;
static int hf_v5dl_ftype_s_u = -1;
static int hf_v5dl_ftype_s_u_ext = -1;
#if 0
static int hf_v5dl_checksum = -1;
static int hf_v5dl_checksum_status = -1;
#endif
static gint ett_v5dl = -1;
static gint ett_v5dl_address = -1;
static gint ett_v5dl_control = -1;
/* static gint ett_v5dl_checksum = -1; */

static expert_field ei_v5dl_checksum = EI_INIT;

static dissector_handle_t v52_handle;

/*
 * Bits in the address field.
 */
#define	V5DL_EAH		0xfc00	/* Service Access Point Identifier */
#define	V5DL_EAH_SHIFT		10
#define	V5DL_CR			0x0200	/* Command/Response bit */
#define	V5DL_EA1		0x0100	/* First Address Extension bit */
#define	V5DL_EAL		0x00fe	/* Terminal Endpoint Identifier */
#define	V5DL_EAL_SHIFT		1
#define	V5DL_EA2		0x0001	/* Second Address Extension bit */

static const value_string v5dl_direction_vals[] = {
	{ P2P_DIR_RECV,		"Network->User"},
	{ P2P_DIR_SENT,		"User->Network"},
	{ 0,			NULL }
};

static const value_string v5dl_addr_vals[] = {
	{ 8175, "ISDN Protocol" },
	{ 8176, "PSTN Protocol" },
	{ 8177, "CONTROL Protocol" },
	{ 8178, "BCC Protocol" },
	{ 8179, "PROT Protocol" },
	{ 8180, "Link Control Protocol" },
	{ 8191, "VALUE RESERVED" },
	{ 0,	NULL } };

/* Used only for U frames */
static const xdlc_cf_items v5dl_cf_items = {
	NULL,
	NULL,
	&hf_v5dl_p,
	&hf_v5dl_f,
	NULL,
	&hf_v5dl_u_modifier_cmd,
	&hf_v5dl_u_modifier_resp,
	NULL,
	&hf_v5dl_ftype_s_u
};

/* Used only for I and S frames */
static const xdlc_cf_items v5dl_cf_items_ext = {
	&hf_v5dl_n_r,
	&hf_v5dl_n_s,
	&hf_v5dl_p_ext,
	&hf_v5dl_f_ext,
	&hf_v5dl_s_ftype,
	NULL,
	NULL,
	&hf_v5dl_ftype_i,
	&hf_v5dl_ftype_s_u_ext
};


#define MAX_V5DL_PACKET_LEN 1024

static int
dissect_v5dl(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, void* data _U_)
{
	proto_tree	*v5dl_tree, *addr_tree;
	proto_item	*v5dl_ti, *addr_ti;
	int		direction;
	guint		v5dl_header_len;
	guint16		control;
#if 0
	proto_tree	*checksum_tree;
	proto_item	*checksum_ti;
	guint16		checksum, checksum_calculated;
	guint		checksum_offset;
#endif
	guint16		addr, cr, eah, eal, v5addr;
	gboolean	is_response = 0;
#if 0
	guint		length, reported_length;
#endif
	tvbuff_t	*next_tvb;
	const char	*srcname = "?";
	const char	*dstname = "?";

	col_set_str(pinfo->cinfo, COL_PROTOCOL, "V5DL");
	col_clear(pinfo->cinfo, COL_INFO);

	addr = tvb_get_ntohs(tvb, 0);
	cr = addr & V5DL_CR;
	eal = (addr & V5DL_EAL) >> V5DL_EAL_SHIFT;
	eah = (addr & V5DL_EAH) >> V5DL_EAH_SHIFT;
	v5addr = (eah << 7) + eal;
	v5dl_header_len = 2;	/* addr */

	direction = pinfo->p2p_dir;
	if (pinfo->p2p_dir == P2P_DIR_RECV) {
	    is_response = cr ? FALSE : TRUE;
	    srcname = "Network";
	    dstname = "User";
	}
	else if (pinfo->p2p_dir == P2P_DIR_SENT) {
	    is_response = cr ? TRUE : FALSE;
	    srcname = "User";
	    dstname = "Network";
	}

	col_set_str(pinfo->cinfo, COL_RES_DL_SRC, srcname);
	col_set_str(pinfo->cinfo, COL_RES_DL_DST, dstname);

	if (tree) {
		proto_item *direction_ti;

		v5dl_ti = proto_tree_add_item(tree, proto_v5dl, tvb, 0, -1,
		    ENC_NA);
		v5dl_tree = proto_item_add_subtree(v5dl_ti, ett_v5dl);

		/*
		 * Don't show the direction if we don't know it.
		 */
		if (direction != P2P_DIR_UNKNOWN) {
			direction_ti = proto_tree_add_uint(v5dl_tree, hf_v5dl_direction,
			                                   tvb, 0, 0, pinfo->p2p_dir);
			proto_item_set_generated(direction_ti);
		}

		addr_ti = proto_tree_add_uint(v5dl_tree, hf_v5dl_ef, tvb,
		    0, 2, v5addr);
		addr_tree = proto_item_add_subtree(addr_ti, ett_v5dl_address);
		proto_tree_add_uint(addr_tree, hf_v5dl_eah, tvb, 0, 1, addr);
		proto_tree_add_uint(addr_tree, hf_v5dl_cr,  tvb, 0, 1, addr);
		proto_tree_add_uint(addr_tree, hf_v5dl_ea1, tvb, 0, 1, addr);
		proto_tree_add_uint(addr_tree, hf_v5dl_eal, tvb, 1, 1, addr);
		proto_tree_add_uint(addr_tree, hf_v5dl_ea2, tvb, 1, 1, addr);
	}
	else {
		v5dl_ti = NULL;
		v5dl_tree = NULL;
	}

	control = dissect_xdlc_control(tvb, 2, pinfo, v5dl_tree, hf_v5dl_control,
	    ett_v5dl_control, &v5dl_cf_items, &v5dl_cf_items_ext, NULL, NULL,
	    is_response, TRUE, FALSE);
	v5dl_header_len += XDLC_CONTROL_LEN(control, TRUE);

	if (tree)
		proto_item_set_len(v5dl_ti, v5dl_header_len);

	/*
	 * XXX - the sample capture supplied with bug 7027 does not
	 * appear to include checksums in the packets.
	 */
#if 0
	/*
	 * Check the checksum, if available.
	 * The checksum is a CCITT CRC-16 at the end of the packet, so
	 * if we don't have the entire packet in the capture - i.e., if
	 * tvb_captured_length(tvb) != tvb_reported_length(tvb) we can't check it.
	 */
	length = tvb_captured_length(tvb);
	reported_length = tvb_reported_length(tvb);

	/*
	 * If the reported length isn't big enough for the V5DL header
	 * and 2 bytes of checksum, the packet is malformed, as the
	 * checksum overlaps the header.
	 */
	if (reported_length < v5dl_header_len + 2)
		return;

	if (length == reported_length) {
		/*
		 * There's no snapshot length cutting off any of the
		 * packet.
		 */
		checksum_offset = reported_length - 2;
		checksum_calculated = crc16_ccitt_tvb(tvb, checksum_offset);
		checksum_calculated = g_htons(checksum_calculated);  /* Note: g_htons() macro may eval arg multiple times */

		proto_tree_add_checksum(v5dl_tree, tvb, checksum_offset, hf_v5dl_checksum, hf_v5dl_checksum_status, &ei_v5dl_checksum,
								pinfo, checksum_calculated, ENC_BIG_ENDIAN, PROTO_CHECKSUM_VERIFY);
		/*
		 * Remove the V5DL header *and* the checksum.
		 */
		next_tvb = tvb_new_subset_length_caplen(tvb, v5dl_header_len,
		    tvb_captured_length_remaining(tvb, v5dl_header_len) - 2,
		    tvb_reported_length_remaining(tvb, v5dl_header_len) - 2);
	} else {
		/*
		 * Some or all of the packet is cut off by a snapshot
		 * length.
		 */
		if (length == reported_length - 1) {
			/*
			 * One byte is cut off, so there's only one
			 * byte of checksum in the captured data.
			 * Remove that byte from the captured length
			 * and both bytes from the reported length.
			 */
			next_tvb = tvb_new_subset_length_caplen(tvb, v5dl_header_len,
			    tvb_captured_length_remaining(tvb, v5dl_header_len) - 1,
			    tvb_reported_length_remaining(tvb, v5dl_header_len) - 2);
		} else {
			/*
			 * Two or more bytes are cut off, so there are
			 * no bytes of checksum in the captured data.
			 * Just remove the checksum from the reported
			 * length.
			 */
			next_tvb = tvb_new_subset_length_caplen(tvb, v5dl_header_len,
			    tvb_captured_length_remaining(tvb, v5dl_header_len),
			    tvb_reported_length_remaining(tvb, v5dl_header_len) - 2);
		}
	}
#else
	next_tvb = tvb_new_subset_remaining(tvb, v5dl_header_len);
#endif

	if (XDLC_IS_INFORMATION(control)) {
		/* call V5.2 dissector */
	        call_dissector(v52_handle, next_tvb, pinfo, tree);
	}
	return tvb_captured_length(tvb);
}

void
proto_reg_handoff_v5dl(void);

void
proto_register_v5dl(void)
{
	static hf_register_info hf[] = {

	{ &hf_v5dl_direction,
	  { "Direction", "v5dl.direction", FT_UINT8, BASE_DEC, VALS(v5dl_direction_vals), 0x0,
	  	NULL, HFILL }},

#if 0
	{ &hf_v5dl_address,
	  { "Address Field", "v5dl.address", FT_UINT16, BASE_HEX, NULL, 0x0,
	  	"Address", HFILL }},
#endif

	{ &hf_v5dl_ef,
	  { "EF", "v5dl.ef", FT_UINT16, BASE_DEC, VALS(v5dl_addr_vals), 0x0,
	  	"Envelope Function Address", HFILL }},

	{ &hf_v5dl_eah,
	  { "EAH", "v5dl.eah", FT_UINT16, BASE_DEC, NULL, V5DL_EAH,
	  	"Envelope Address High", HFILL }},

	{ &hf_v5dl_cr,
	  { "C/R", "v5dl.cr", FT_UINT16, BASE_DEC, NULL, V5DL_CR,
	  	"Command/Response bit", HFILL }},

	{ &hf_v5dl_ea1,
	  { "EA1", "v5dl.ea1", FT_UINT16, BASE_DEC, NULL, V5DL_EA1,
	  	"First Address Extension bit", HFILL }},

	{ &hf_v5dl_eal,
	  { "EAL", "v5dl.eal", FT_UINT16, BASE_DEC, NULL, V5DL_EAL,
	  	"Envelope Address Low", HFILL }},

	{ &hf_v5dl_ea2,
	  { "EA2", "v5dl.ea2", FT_UINT16, BASE_DEC, NULL, V5DL_EA2,
	  	"Second Address Extension bit", HFILL }},

	{ &hf_v5dl_control,
	  { "Control Field", "v5dl.control", FT_UINT16, BASE_HEX, NULL, 0x0,
	  	NULL, HFILL }},

	{ &hf_v5dl_n_r,
	  { "N(R)", "v5dl.control.n_r", FT_UINT16, BASE_DEC,
		NULL, XDLC_N_R_EXT_MASK, NULL, HFILL }},

	{ &hf_v5dl_n_s,
	  { "N(S)", "v5dl.control.n_s", FT_UINT16, BASE_DEC,
		NULL, XDLC_N_S_EXT_MASK, NULL, HFILL }},

	{ &hf_v5dl_p,
	  { "Poll", "v5dl.control.p", FT_BOOLEAN, 8,
		TFS(&tfs_set_notset), XDLC_P_F, NULL, HFILL }},

	{ &hf_v5dl_p_ext,
	  { "Poll", "v5dl.control.p", FT_BOOLEAN, 16,
		TFS(&tfs_set_notset), XDLC_P_F_EXT, NULL, HFILL }},

	{ &hf_v5dl_f,
	  { "Final", "v5dl.control.f", FT_BOOLEAN, 8,
		TFS(&tfs_set_notset), XDLC_P_F, NULL, HFILL }},

	{ &hf_v5dl_f_ext,
	  { "Final", "v5dl.control.f", FT_BOOLEAN, 16,
		TFS(&tfs_set_notset), XDLC_P_F_EXT, NULL, HFILL }},

	{ &hf_v5dl_s_ftype,
	  { "Supervisory frame type", "v5dl.control.s_ftype", FT_UINT16, BASE_HEX,
		VALS(stype_vals), XDLC_S_FTYPE_MASK, NULL, HFILL }},

	{ &hf_v5dl_u_modifier_cmd,
	  { "Command", "v5dl.control.u_modifier_cmd", FT_UINT8, BASE_HEX,
		VALS(modifier_vals_cmd), XDLC_U_MODIFIER_MASK, NULL, HFILL }},

	{ &hf_v5dl_u_modifier_resp,
	  { "Response", "v5dl.control.u_modifier_resp", FT_UINT8, BASE_HEX,
		VALS(modifier_vals_resp), XDLC_U_MODIFIER_MASK, NULL, HFILL }},

	{ &hf_v5dl_ftype_i,
	  { "Frame type", "v5dl.control.ftype", FT_UINT16, BASE_HEX,
		VALS(ftype_vals), XDLC_I_MASK, NULL, HFILL }},

	{ &hf_v5dl_ftype_s_u,
	  { "Frame type", "v5dl.control.ftype", FT_UINT8, BASE_HEX,
		VALS(ftype_vals), XDLC_S_U_MASK, NULL, HFILL }},

	{ &hf_v5dl_ftype_s_u_ext,
	  { "Frame type", "v5dl.control.ftype", FT_UINT16, BASE_HEX,
		VALS(ftype_vals), XDLC_S_U_MASK, NULL, HFILL }},

#if 0
	{ &hf_v5dl_checksum,
	  { "Checksum", "v5dl.checksum", FT_UINT16, BASE_HEX,
		NULL, 0x0, "Details at: https://www.wireshark.org/docs/wsug_html_chunked/ChAdvChecksums.html", HFILL }},

	{ &hf_v5dl_checksum_status,
	  { "Checksum Status", "v5dl.checksum.status", FT_UINT8, BASE_NONE,
		VALS(proto_checksum_vals), 0x0, NULL, HFILL }},
#endif
	};

	static gint *ett[] = {
		&ett_v5dl,
		&ett_v5dl_address,
		&ett_v5dl_control,
		/* &ett_v5dl_checksum */
	};

	static ei_register_info ei[] = {
		{ &ei_v5dl_checksum, { "v5dl.bad_checksum", PI_CHECKSUM, PI_ERROR, "Bad checksum", EXPFILL }},
	};

	expert_module_t* expert_v5dl;

	proto_v5dl = proto_register_protocol("V5 Data Link Layer", "V5DL", "v5dl");
	proto_register_field_array (proto_v5dl, hf, array_length(hf));
	proto_register_subtree_array(ett, array_length(ett));
	expert_v5dl = expert_register_protocol(proto_v5dl);
	expert_register_field_array(expert_v5dl, ei, array_length(ei));

	register_dissector("v5dl", dissect_v5dl, proto_v5dl);
}

void
proto_reg_handoff_v5dl(void)
{
	v52_handle = find_dissector_add_dependency("v52", proto_v5dl);
}

/*
 * 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:
 */