1 /* packet-v5dl.c
2  * Routines for V5 data link frame disassembly
3  * Rolf Fiedler <rolf.fiedler@innoventif.de> using the LAPD code of
4  * Gilbert Ramirez <gram@alumni.rice.edu>
5  *
6  * Wireshark - Network traffic analyzer
7  * By Gerald Combs <gerald@wireshark.org>
8  * Copyright 1998
9  *
10  * SPDX-License-Identifier: GPL-2.0-or-later
11  */
12 /*
13  * V5 Data Link Layer
14  *
15  * V5 references:
16  * ETS 300 324-1
17  * ETS 300 347-1
18  *
19  */
20 
21 #include "config.h"
22 
23 #include <epan/packet.h>
24 #include <epan/expert.h>
25 #include <epan/xdlc.h>
26 
27 void proto_register_v5dl(void);
28 
29 static int proto_v5dl = -1;
30 static int hf_v5dl_direction = -1;
31 /* static int hf_v5dl_address = -1; */
32 static int hf_v5dl_ef = -1;
33 static int hf_v5dl_eah = -1;
34 static int hf_v5dl_cr = -1;
35 static int hf_v5dl_ea1 = -1;
36 static int hf_v5dl_eal = -1;
37 static int hf_v5dl_ea2 = -1;
38 static int hf_v5dl_control = -1;
39 static int hf_v5dl_n_r = -1;
40 static int hf_v5dl_n_s = -1;
41 static int hf_v5dl_p = -1;
42 static int hf_v5dl_p_ext = -1;
43 static int hf_v5dl_f = -1;
44 static int hf_v5dl_f_ext = -1;
45 static int hf_v5dl_s_ftype = -1;
46 static int hf_v5dl_u_modifier_cmd = -1;
47 static int hf_v5dl_u_modifier_resp = -1;
48 static int hf_v5dl_ftype_i = -1;
49 static int hf_v5dl_ftype_s_u = -1;
50 static int hf_v5dl_ftype_s_u_ext = -1;
51 #if 0
52 static int hf_v5dl_checksum = -1;
53 static int hf_v5dl_checksum_status = -1;
54 #endif
55 static gint ett_v5dl = -1;
56 static gint ett_v5dl_address = -1;
57 static gint ett_v5dl_control = -1;
58 /* static gint ett_v5dl_checksum = -1; */
59 
60 static expert_field ei_v5dl_checksum = EI_INIT;
61 
62 static dissector_handle_t v52_handle;
63 
64 /*
65  * Bits in the address field.
66  */
67 #define	V5DL_EAH		0xfc00	/* Service Access Point Identifier */
68 #define	V5DL_EAH_SHIFT		10
69 #define	V5DL_CR			0x0200	/* Command/Response bit */
70 #define	V5DL_EA1		0x0100	/* First Address Extension bit */
71 #define	V5DL_EAL		0x00fe	/* Terminal Endpoint Identifier */
72 #define	V5DL_EAL_SHIFT		1
73 #define	V5DL_EA2		0x0001	/* Second Address Extension bit */
74 
75 static const value_string v5dl_direction_vals[] = {
76 	{ P2P_DIR_RECV,		"Network->User"},
77 	{ P2P_DIR_SENT,		"User->Network"},
78 	{ 0,			NULL }
79 };
80 
81 static const value_string v5dl_addr_vals[] = {
82 	{ 8175, "ISDN Protocol" },
83 	{ 8176, "PSTN Protocol" },
84 	{ 8177, "CONTROL Protocol" },
85 	{ 8178, "BCC Protocol" },
86 	{ 8179, "PROT Protocol" },
87 	{ 8180, "Link Control Protocol" },
88 	{ 8191, "VALUE RESERVED" },
89 	{ 0,	NULL } };
90 
91 /* Used only for U frames */
92 static const xdlc_cf_items v5dl_cf_items = {
93 	NULL,
94 	NULL,
95 	&hf_v5dl_p,
96 	&hf_v5dl_f,
97 	NULL,
98 	&hf_v5dl_u_modifier_cmd,
99 	&hf_v5dl_u_modifier_resp,
100 	NULL,
101 	&hf_v5dl_ftype_s_u
102 };
103 
104 /* Used only for I and S frames */
105 static const xdlc_cf_items v5dl_cf_items_ext = {
106 	&hf_v5dl_n_r,
107 	&hf_v5dl_n_s,
108 	&hf_v5dl_p_ext,
109 	&hf_v5dl_f_ext,
110 	&hf_v5dl_s_ftype,
111 	NULL,
112 	NULL,
113 	&hf_v5dl_ftype_i,
114 	&hf_v5dl_ftype_s_u_ext
115 };
116 
117 
118 #define MAX_V5DL_PACKET_LEN 1024
119 
120 static int
dissect_v5dl(tvbuff_t * tvb,packet_info * pinfo,proto_tree * tree,void * data _U_)121 dissect_v5dl(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, void* data _U_)
122 {
123 	proto_tree	*v5dl_tree, *addr_tree;
124 	proto_item	*v5dl_ti, *addr_ti;
125 	int		direction;
126 	guint		v5dl_header_len;
127 	guint16		control;
128 #if 0
129 	proto_tree	*checksum_tree;
130 	proto_item	*checksum_ti;
131 	guint16		checksum, checksum_calculated;
132 	guint		checksum_offset;
133 #endif
134 	guint16		addr, cr, eah, eal, v5addr;
135 	gboolean	is_response = 0;
136 #if 0
137 	guint		length, reported_length;
138 #endif
139 	tvbuff_t	*next_tvb;
140 	const char	*srcname = "?";
141 	const char	*dstname = "?";
142 
143 	col_set_str(pinfo->cinfo, COL_PROTOCOL, "V5DL");
144 	col_clear(pinfo->cinfo, COL_INFO);
145 
146 	addr = tvb_get_ntohs(tvb, 0);
147 	cr = addr & V5DL_CR;
148 	eal = (addr & V5DL_EAL) >> V5DL_EAL_SHIFT;
149 	eah = (addr & V5DL_EAH) >> V5DL_EAH_SHIFT;
150 	v5addr = (eah << 7) + eal;
151 	v5dl_header_len = 2;	/* addr */
152 
153 	direction = pinfo->p2p_dir;
154 	if (pinfo->p2p_dir == P2P_DIR_RECV) {
155 	    is_response = cr ? FALSE : TRUE;
156 	    srcname = "Network";
157 	    dstname = "User";
158 	}
159 	else if (pinfo->p2p_dir == P2P_DIR_SENT) {
160 	    is_response = cr ? TRUE : FALSE;
161 	    srcname = "User";
162 	    dstname = "Network";
163 	}
164 
165 	col_set_str(pinfo->cinfo, COL_RES_DL_SRC, srcname);
166 	col_set_str(pinfo->cinfo, COL_RES_DL_DST, dstname);
167 
168 	if (tree) {
169 		proto_item *direction_ti;
170 
171 		v5dl_ti = proto_tree_add_item(tree, proto_v5dl, tvb, 0, -1,
172 		    ENC_NA);
173 		v5dl_tree = proto_item_add_subtree(v5dl_ti, ett_v5dl);
174 
175 		/*
176 		 * Don't show the direction if we don't know it.
177 		 */
178 		if (direction != P2P_DIR_UNKNOWN) {
179 			direction_ti = proto_tree_add_uint(v5dl_tree, hf_v5dl_direction,
180 			                                   tvb, 0, 0, pinfo->p2p_dir);
181 			proto_item_set_generated(direction_ti);
182 		}
183 
184 		addr_ti = proto_tree_add_uint(v5dl_tree, hf_v5dl_ef, tvb,
185 		    0, 2, v5addr);
186 		addr_tree = proto_item_add_subtree(addr_ti, ett_v5dl_address);
187 		proto_tree_add_uint(addr_tree, hf_v5dl_eah, tvb, 0, 1, addr);
188 		proto_tree_add_uint(addr_tree, hf_v5dl_cr,  tvb, 0, 1, addr);
189 		proto_tree_add_uint(addr_tree, hf_v5dl_ea1, tvb, 0, 1, addr);
190 		proto_tree_add_uint(addr_tree, hf_v5dl_eal, tvb, 1, 1, addr);
191 		proto_tree_add_uint(addr_tree, hf_v5dl_ea2, tvb, 1, 1, addr);
192 	}
193 	else {
194 		v5dl_ti = NULL;
195 		v5dl_tree = NULL;
196 	}
197 
198 	control = dissect_xdlc_control(tvb, 2, pinfo, v5dl_tree, hf_v5dl_control,
199 	    ett_v5dl_control, &v5dl_cf_items, &v5dl_cf_items_ext, NULL, NULL,
200 	    is_response, TRUE, FALSE);
201 	v5dl_header_len += XDLC_CONTROL_LEN(control, TRUE);
202 
203 	if (tree)
204 		proto_item_set_len(v5dl_ti, v5dl_header_len);
205 
206 	/*
207 	 * XXX - the sample capture supplied with bug 7027 does not
208 	 * appear to include checksums in the packets.
209 	 */
210 #if 0
211 	/*
212 	 * Check the checksum, if available.
213 	 * The checksum is a CCITT CRC-16 at the end of the packet, so
214 	 * if we don't have the entire packet in the capture - i.e., if
215 	 * tvb_captured_length(tvb) != tvb_reported_length(tvb) we can't check it.
216 	 */
217 	length = tvb_captured_length(tvb);
218 	reported_length = tvb_reported_length(tvb);
219 
220 	/*
221 	 * If the reported length isn't big enough for the V5DL header
222 	 * and 2 bytes of checksum, the packet is malformed, as the
223 	 * checksum overlaps the header.
224 	 */
225 	if (reported_length < v5dl_header_len + 2)
226 		return;
227 
228 	if (length == reported_length) {
229 		/*
230 		 * There's no snapshot length cutting off any of the
231 		 * packet.
232 		 */
233 		checksum_offset = reported_length - 2;
234 		checksum_calculated = crc16_ccitt_tvb(tvb, checksum_offset);
235 		checksum_calculated = g_htons(checksum_calculated);  /* Note: g_htons() macro may eval arg multiple times */
236 
237 		proto_tree_add_checksum(v5dl_tree, tvb, checksum_offset, hf_v5dl_checksum, hf_v5dl_checksum_status, &ei_v5dl_checksum,
238 								pinfo, checksum_calculated, ENC_BIG_ENDIAN, PROTO_CHECKSUM_VERIFY);
239 		/*
240 		 * Remove the V5DL header *and* the checksum.
241 		 */
242 		next_tvb = tvb_new_subset_length_caplen(tvb, v5dl_header_len,
243 		    tvb_captured_length_remaining(tvb, v5dl_header_len) - 2,
244 		    tvb_reported_length_remaining(tvb, v5dl_header_len) - 2);
245 	} else {
246 		/*
247 		 * Some or all of the packet is cut off by a snapshot
248 		 * length.
249 		 */
250 		if (length == reported_length - 1) {
251 			/*
252 			 * One byte is cut off, so there's only one
253 			 * byte of checksum in the captured data.
254 			 * Remove that byte from the captured length
255 			 * and both bytes from the reported length.
256 			 */
257 			next_tvb = tvb_new_subset_length_caplen(tvb, v5dl_header_len,
258 			    tvb_captured_length_remaining(tvb, v5dl_header_len) - 1,
259 			    tvb_reported_length_remaining(tvb, v5dl_header_len) - 2);
260 		} else {
261 			/*
262 			 * Two or more bytes are cut off, so there are
263 			 * no bytes of checksum in the captured data.
264 			 * Just remove the checksum from the reported
265 			 * length.
266 			 */
267 			next_tvb = tvb_new_subset_length_caplen(tvb, v5dl_header_len,
268 			    tvb_captured_length_remaining(tvb, v5dl_header_len),
269 			    tvb_reported_length_remaining(tvb, v5dl_header_len) - 2);
270 		}
271 	}
272 #else
273 	next_tvb = tvb_new_subset_remaining(tvb, v5dl_header_len);
274 #endif
275 
276 	if (XDLC_IS_INFORMATION(control)) {
277 		/* call V5.2 dissector */
278 	        call_dissector(v52_handle, next_tvb, pinfo, tree);
279 	}
280 	return tvb_captured_length(tvb);
281 }
282 
283 void
284 proto_reg_handoff_v5dl(void);
285 
286 void
proto_register_v5dl(void)287 proto_register_v5dl(void)
288 {
289 	static hf_register_info hf[] = {
290 
291 	{ &hf_v5dl_direction,
292 	  { "Direction", "v5dl.direction", FT_UINT8, BASE_DEC, VALS(v5dl_direction_vals), 0x0,
293 	  	NULL, HFILL }},
294 
295 #if 0
296 	{ &hf_v5dl_address,
297 	  { "Address Field", "v5dl.address", FT_UINT16, BASE_HEX, NULL, 0x0,
298 	  	"Address", HFILL }},
299 #endif
300 
301 	{ &hf_v5dl_ef,
302 	  { "EF", "v5dl.ef", FT_UINT16, BASE_DEC, VALS(v5dl_addr_vals), 0x0,
303 	  	"Envelope Function Address", HFILL }},
304 
305 	{ &hf_v5dl_eah,
306 	  { "EAH", "v5dl.eah", FT_UINT16, BASE_DEC, NULL, V5DL_EAH,
307 	  	"Envelope Address High", HFILL }},
308 
309 	{ &hf_v5dl_cr,
310 	  { "C/R", "v5dl.cr", FT_UINT16, BASE_DEC, NULL, V5DL_CR,
311 	  	"Command/Response bit", HFILL }},
312 
313 	{ &hf_v5dl_ea1,
314 	  { "EA1", "v5dl.ea1", FT_UINT16, BASE_DEC, NULL, V5DL_EA1,
315 	  	"First Address Extension bit", HFILL }},
316 
317 	{ &hf_v5dl_eal,
318 	  { "EAL", "v5dl.eal", FT_UINT16, BASE_DEC, NULL, V5DL_EAL,
319 	  	"Envelope Address Low", HFILL }},
320 
321 	{ &hf_v5dl_ea2,
322 	  { "EA2", "v5dl.ea2", FT_UINT16, BASE_DEC, NULL, V5DL_EA2,
323 	  	"Second Address Extension bit", HFILL }},
324 
325 	{ &hf_v5dl_control,
326 	  { "Control Field", "v5dl.control", FT_UINT16, BASE_HEX, NULL, 0x0,
327 	  	NULL, HFILL }},
328 
329 	{ &hf_v5dl_n_r,
330 	  { "N(R)", "v5dl.control.n_r", FT_UINT16, BASE_DEC,
331 		NULL, XDLC_N_R_EXT_MASK, NULL, HFILL }},
332 
333 	{ &hf_v5dl_n_s,
334 	  { "N(S)", "v5dl.control.n_s", FT_UINT16, BASE_DEC,
335 		NULL, XDLC_N_S_EXT_MASK, NULL, HFILL }},
336 
337 	{ &hf_v5dl_p,
338 	  { "Poll", "v5dl.control.p", FT_BOOLEAN, 8,
339 		TFS(&tfs_set_notset), XDLC_P_F, NULL, HFILL }},
340 
341 	{ &hf_v5dl_p_ext,
342 	  { "Poll", "v5dl.control.p", FT_BOOLEAN, 16,
343 		TFS(&tfs_set_notset), XDLC_P_F_EXT, NULL, HFILL }},
344 
345 	{ &hf_v5dl_f,
346 	  { "Final", "v5dl.control.f", FT_BOOLEAN, 8,
347 		TFS(&tfs_set_notset), XDLC_P_F, NULL, HFILL }},
348 
349 	{ &hf_v5dl_f_ext,
350 	  { "Final", "v5dl.control.f", FT_BOOLEAN, 16,
351 		TFS(&tfs_set_notset), XDLC_P_F_EXT, NULL, HFILL }},
352 
353 	{ &hf_v5dl_s_ftype,
354 	  { "Supervisory frame type", "v5dl.control.s_ftype", FT_UINT16, BASE_HEX,
355 		VALS(stype_vals), XDLC_S_FTYPE_MASK, NULL, HFILL }},
356 
357 	{ &hf_v5dl_u_modifier_cmd,
358 	  { "Command", "v5dl.control.u_modifier_cmd", FT_UINT8, BASE_HEX,
359 		VALS(modifier_vals_cmd), XDLC_U_MODIFIER_MASK, NULL, HFILL }},
360 
361 	{ &hf_v5dl_u_modifier_resp,
362 	  { "Response", "v5dl.control.u_modifier_resp", FT_UINT8, BASE_HEX,
363 		VALS(modifier_vals_resp), XDLC_U_MODIFIER_MASK, NULL, HFILL }},
364 
365 	{ &hf_v5dl_ftype_i,
366 	  { "Frame type", "v5dl.control.ftype", FT_UINT16, BASE_HEX,
367 		VALS(ftype_vals), XDLC_I_MASK, NULL, HFILL }},
368 
369 	{ &hf_v5dl_ftype_s_u,
370 	  { "Frame type", "v5dl.control.ftype", FT_UINT8, BASE_HEX,
371 		VALS(ftype_vals), XDLC_S_U_MASK, NULL, HFILL }},
372 
373 	{ &hf_v5dl_ftype_s_u_ext,
374 	  { "Frame type", "v5dl.control.ftype", FT_UINT16, BASE_HEX,
375 		VALS(ftype_vals), XDLC_S_U_MASK, NULL, HFILL }},
376 
377 #if 0
378 	{ &hf_v5dl_checksum,
379 	  { "Checksum", "v5dl.checksum", FT_UINT16, BASE_HEX,
380 		NULL, 0x0, "Details at: https://www.wireshark.org/docs/wsug_html_chunked/ChAdvChecksums.html", HFILL }},
381 
382 	{ &hf_v5dl_checksum_status,
383 	  { "Checksum Status", "v5dl.checksum.status", FT_UINT8, BASE_NONE,
384 		VALS(proto_checksum_vals), 0x0, NULL, HFILL }},
385 #endif
386 	};
387 
388 	static gint *ett[] = {
389 		&ett_v5dl,
390 		&ett_v5dl_address,
391 		&ett_v5dl_control,
392 		/* &ett_v5dl_checksum */
393 	};
394 
395 	static ei_register_info ei[] = {
396 		{ &ei_v5dl_checksum, { "v5dl.bad_checksum", PI_CHECKSUM, PI_ERROR, "Bad checksum", EXPFILL }},
397 	};
398 
399 	expert_module_t* expert_v5dl;
400 
401 	proto_v5dl = proto_register_protocol("V5 Data Link Layer", "V5DL", "v5dl");
402 	proto_register_field_array (proto_v5dl, hf, array_length(hf));
403 	proto_register_subtree_array(ett, array_length(ett));
404 	expert_v5dl = expert_register_protocol(proto_v5dl);
405 	expert_register_field_array(expert_v5dl, ei, array_length(ei));
406 
407 	register_dissector("v5dl", dissect_v5dl, proto_v5dl);
408 }
409 
410 void
proto_reg_handoff_v5dl(void)411 proto_reg_handoff_v5dl(void)
412 {
413 	v52_handle = find_dissector_add_dependency("v52", proto_v5dl);
414 }
415 
416 /*
417  * Editor modelines  -  https://www.wireshark.org/tools/modelines.html
418  *
419  * Local variables:
420  * c-basic-offset: 8
421  * tab-width: 8
422  * indent-tabs-mode: t
423  * End:
424  *
425  * vi: set shiftwidth=8 tabstop=8 noexpandtab:
426  * :indentSize=8:tabSize=8:noTabs=false:
427  */
428