aboutsummaryrefslogtreecommitdiffstats
path: root/epan/dissectors/packet-pw-hdlc.c
blob: 80995ca6a26f88d4f59292908af836132e96178c (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
/* packet-pw-hdlc.c
 * Routines for HDLC PW dissection as per RFC4618.
 * Copyright 2009, Dmitry Trebich, Artem Tamazov <artem.tamazov@tellabs.com>
 *
 * Wireshark - Network traffic analyzer
 * By Gerald Combs <gerald@wireshark.org>
 * Copyright 1998 Gerald Combs
 *
 * SPDX-License-Identifier: GPL-2.0-or-later
 *
 * History:
 * ---------------------------------
 * 02.03.2009 Initial implementation, supports:
 * - HDLC mode (rfc4618 5.1), no CW, payload is PPP (PPP in HDLC-like Framing (rfc1662)).
 * - FR port mode (rfc4618 5.2), no CW.
 *
 * [informative: Not supported yet:
 * - All kinds of HDLC PW with CW.
 * - PPP mode (rfc4618 5.3).
 * - For HDLC mode, decoding payloads other than PPP.]
 */

#include "config.h"

#include <epan/packet.h>


void proto_register_pw_hdlc(void);
void proto_reg_handoff_pw_hdlc(void);

static dissector_handle_t ppp_handle;
static dissector_handle_t fr_handle;
static dissector_handle_t pw_hdlc_nocw_fr_handle;
static dissector_handle_t pw_ppp_handle;

static gint proto_pw_hdlc_nocw_fr = -1;
static gint proto_pw_hdlc_nocw_hdlc_ppp = -1;

static gint ett_pw_hdlc = -1;

/* static int hf_pw_hdlc = -1; */
static int hf_pw_hdlc_address_field = -1;
static int hf_pw_hdlc_address = -1;
static int hf_pw_hdlc_cr_bit = -1;
static int hf_pw_hdlc_control_field = -1;
static int hf_pw_hdlc_pf_bit = -1;
static int hf_pw_hdlc_modifier = -1;
static int hf_pw_hdlc_frame = -1;

static const value_string pw_hdlc_modifier_vals[] = {
	{0x00, "UI - Unnumbered information" },
	{0x08, "UP - Unnumbered poll" },
	{0x10, "DISC/RD - Disconnect/Request disconnect" },
	{0x18, "UA - Unnumbered acknowledgment" },
	{0x20, "SNRM - Set normal response mode" },
	{0x38, "TEST - Test" },
	{0x01, "SIM/RIM - Set initialization mode/Request initialization mode" },
	{0x21, "FRMR - Frame reject" },
	{0x03, "SARM/DM - Set asynchronous response mode/Disconnect mode" },
	{0x0B, "SABM - Set asynchronous balanced mode" },
	{0x13, "SARME - Set asynchronous response extended mode" },
	{0x1B, "SABME - Set asynchronous balanced extended mode" },
	{0x23, "RSET - Reset" },
	{0x2B, "XID - Exchange identification" },
	{0x33, "SNRME - Set normal response extended mode" },
	{0, NULL }
};

static int dissect_pw_hdlc_nocw_fr( tvbuff_t * tvb, packet_info * pinfo, proto_tree * tree, void* data _U_ )
{
	return call_dissector( fr_handle, tvb, pinfo, tree );
}


static int dissect_pw_hdlc_nocw_hdlc_ppp( tvbuff_t * tvb, packet_info * pinfo, proto_tree * tree, void* data _U_ )
{
	if (tvb_reported_length_remaining(tvb, 0) < 2)
	{
		return 0;
	}

	if (tree)
	{
		proto_tree  *tr;
		proto_item  *item;
		proto_item  *item_address;
		proto_item  *item_control;
		guint8      addr;
		guint8      control;

		addr	= tvb_get_guint8(tvb, 0);
		control	= tvb_get_guint8(tvb, 1);

		item = proto_tree_add_item( tree, proto_pw_hdlc_nocw_hdlc_ppp, tvb, 0, 2, ENC_NA );

		tr = proto_item_add_subtree( item, ett_pw_hdlc );

		item_address = proto_tree_add_item( tr, hf_pw_hdlc_address_field, tvb, 0, 1, ENC_BIG_ENDIAN );
		item_control = proto_tree_add_item( tr, hf_pw_hdlc_control_field, tvb, 1, 1, ENC_BIG_ENDIAN );

		tr = proto_item_add_subtree( item_address, ett_pw_hdlc );

		if ( 0x3F == (( addr & 0xFC ) >> 2 ))
			proto_tree_add_uint_format_value( tr, hf_pw_hdlc_address, tvb, 0, 1, 0xFC, "0x%x (All stations)", 0x3F );
		else
			proto_tree_add_uint( tr, hf_pw_hdlc_address, tvb, 0, 1, ( addr & 0xFC ) >> 2 );

		proto_tree_add_uint( tr, hf_pw_hdlc_cr_bit, tvb, 0, 1, ( addr & 2 ) >> 1 );

		tr = proto_item_add_subtree( item_control, ett_pw_hdlc );

		if ( control & 1 )
		{
			if ( control & 2 )
			{
				proto_tree_add_uint_format( tr, hf_pw_hdlc_frame, tvb, 1, 1, control, "U frame" );

				proto_tree_add_uint( tr, hf_pw_hdlc_pf_bit, tvb, 1, 1, ( control & 0x10 ) >> 4 );
				proto_tree_add_uint( tr, hf_pw_hdlc_modifier, tvb, 1, 1, (control & 0xEC) >> 2);
			}
			else
			{
				proto_tree_add_uint_format( tr, hf_pw_hdlc_frame, tvb, 1, 1, control, "S frame" );
			}
		}
		else
		{
			proto_tree_add_uint_format( tr, hf_pw_hdlc_frame, tvb, 1, 1, control, "I frame" );
		}
	}

	call_dissector( ppp_handle, tvb_new_subset_remaining(tvb, 2), pinfo, tree );
	return tvb_captured_length(tvb);
}

void proto_register_pw_hdlc(void)
{
	static hf_register_info hf[] = {
#if 0
		{
			&hf_pw_hdlc,
			{
				"PW HDLC", "pw_hdlc", FT_NONE, BASE_NONE, NULL, 0x0, NULL, HFILL
			}
		},
#endif
		{
			&hf_pw_hdlc_address_field,
			{
				"Address field", "pw_hdlc.address_field",
				FT_UINT8, BASE_HEX, NULL, 0x0, NULL, HFILL
			}
		},
		{
			&hf_pw_hdlc_address,
			{
				"Address", "pw_hdlc.address",
				FT_UINT8, BASE_HEX, NULL, 0x0, NULL, HFILL
			}
		},
		{
			&hf_pw_hdlc_cr_bit,
			{
				"C/R bit", "pw_hdlc.cr_bit",
				FT_UINT8, BASE_DEC, NULL, 0x0, NULL, HFILL
			}
		},
		{
			&hf_pw_hdlc_control_field,
			{
				"Control field", "pw_hdlc.control_field",
				FT_UINT8, BASE_HEX, NULL, 0x0, NULL, HFILL
			}
		},
		{
			&hf_pw_hdlc_pf_bit,
			{
				"Poll/Final bit", "pw_hdlc.pf_bit",
				FT_UINT8, BASE_DEC, NULL, 0x0, NULL, HFILL
			}
		},
		{
			&hf_pw_hdlc_modifier,
			{
				"Modifier", "pw_hdlc.modifier",
				FT_UINT8, BASE_HEX, VALS(pw_hdlc_modifier_vals), 0x0, NULL, HFILL
			}
		},
		{
			&hf_pw_hdlc_frame,
			{
				"Frame type", "pw_hdlc.frame",
				FT_UINT8, BASE_DEC, NULL, 0x0, NULL, HFILL
			}
		},
	};

	static gint *ett[] = {
		&ett_pw_hdlc
	};

	proto_pw_hdlc_nocw_fr = proto_register_protocol("HDLC PW, FR port mode (no CW)", /*not displayed*/
							"HDLC PW, FR port mode (no CW)",
							"pw_hdlc_nocw_fr" );
	proto_pw_hdlc_nocw_hdlc_ppp = proto_register_protocol("HDLC-like framing for PPP",
							      "HDLC PW with PPP payload (no CW)",
							      "pw_hdlc_nocw_hdlc_ppp" );

	proto_register_field_array(proto_pw_hdlc_nocw_fr, hf, array_length(hf));
	proto_register_subtree_array(ett, array_length(ett));

	pw_hdlc_nocw_fr_handle = register_dissector("pw_hdlc_nocw_fr", dissect_pw_hdlc_nocw_fr, proto_pw_hdlc_nocw_fr );
	pw_ppp_handle = register_dissector("pw_hdlc_nocw_hdlc_ppp", dissect_pw_hdlc_nocw_hdlc_ppp, proto_pw_hdlc_nocw_hdlc_ppp );
}

void proto_reg_handoff_pw_hdlc(void)
{
	dissector_add_for_decode_as( "mpls.label", pw_hdlc_nocw_fr_handle );
	dissector_add_for_decode_as( "mpls.label", pw_ppp_handle );

	ppp_handle = find_dissector_add_dependency( "ppp", proto_pw_hdlc_nocw_hdlc_ppp );
	fr_handle = find_dissector_add_dependency( "fr", proto_pw_hdlc_nocw_fr );
}

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