/src/wireshark/epan/dissectors/packet-pw-fr.c
Line | Count | Source |
1 | | /* packet-pw-fr.c |
2 | | * Routines for Frame Relay MPLS PW dissection as per RFC4619. |
3 | | * Copyright 2009, Dmitry Trebich, Artem Tamazov <artem.tamazov@tellabs.com> |
4 | | * |
5 | | * Wireshark - Network traffic analyzer |
6 | | * By Gerald Combs <gerald@wireshark.org> |
7 | | * Copyright 1998 Gerald Combs |
8 | | * |
9 | | * SPDX-License-Identifier: GPL-2.0-or-later |
10 | | * |
11 | | * History: |
12 | | * --------------------------------- |
13 | | * 18.03.2009 initial implementation |
14 | | * - FR DLCI mode |
15 | | * Not supported yet: |
16 | | * - Correct FR decode for encapsulations which contain fragmented FR frames. |
17 | | * - FR DLCI Martini (legacy) mode (i.e. legacy CW). |
18 | | */ |
19 | | |
20 | | #include "config.h" |
21 | | |
22 | | #include <epan/packet.h> |
23 | | #include <epan/expert.h> |
24 | | |
25 | | #include "packet-mpls.h" |
26 | | |
27 | | void proto_register_pw_fr(void); |
28 | | void proto_reg_handoff_pw_fr(void); |
29 | | |
30 | | static int proto_encaps; |
31 | | static int ett_encaps; |
32 | | |
33 | | /* static int hf_pw_fr; */ |
34 | | static int hf_cw_bits03; |
35 | | static int hf_cw_fecn; |
36 | | static int hf_cw_becn; |
37 | | static int hf_cw_de; |
38 | | static int hf_cw_cr; |
39 | | static int hf_cw_frg; |
40 | | static int hf_cw_len; |
41 | | static int hf_cw_seq; |
42 | | static int hf_cw_padding; |
43 | | |
44 | | static expert_field ei_payload_size_invalid; |
45 | | static expert_field ei_cw_bits03; |
46 | | static expert_field ei_cw_packet_size_too_small; |
47 | | |
48 | | static const value_string vals_frg[] = { |
49 | | { 0x0, "Unfragmented" }, |
50 | | { 0x1, "First fragment" }, |
51 | | { 0x2, "Last fragment" }, |
52 | | { 0x3, "Intermediate fragment" }, |
53 | | { 0, NULL } |
54 | | }; |
55 | | |
56 | | |
57 | | static dissector_handle_t fr_stripped_address_handle; |
58 | | static dissector_handle_t pw_fr_mpls_handle; |
59 | | |
60 | | static int |
61 | | dissect_pw_fr( tvbuff_t * tvb, packet_info * pinfo, proto_tree * tree, void* data _U_ ) |
62 | 0 | { |
63 | 0 | int packet_size; |
64 | 0 | int payload_size; |
65 | 0 | int payload_padding; |
66 | 0 | const int encaps_size = 4; /*encapsulation consists of mandatory CW only*/ |
67 | 0 | enum { |
68 | 0 | PQ_CW_BAD = 0x001 |
69 | 0 | ,PQ_CW_BAD_BITS03 = 0x002 |
70 | 0 | ,PQ_CW_BAD_LEN_GT_PACKET = 0x004 |
71 | 0 | ,PQ_CW_BAD_LEN_MUST_BE_ZERO = 0x008 |
72 | 0 | ,PQ_CW_BAD_LEN_MUST_BE_NONZERO = 0x010 |
73 | 0 | ,PQ_PAYLOAD_SIZE_ZERO = 0x020 |
74 | 0 | }; |
75 | 0 | int packet_quality; |
76 | |
|
77 | 0 | packet_size = tvb_reported_length_remaining(tvb, 0); |
78 | 0 | if (packet_size < encaps_size) |
79 | 0 | { |
80 | 0 | { |
81 | 0 | proto_item *item; |
82 | 0 | item = proto_tree_add_item(tree, proto_encaps, tvb, 0, -1, ENC_NA); |
83 | 0 | expert_add_info_format(pinfo, item, &ei_cw_packet_size_too_small, |
84 | 0 | "PW packet (%d) is smaller than PW encapsulation header (%d)", |
85 | 0 | (int)packet_size,(int)encaps_size); |
86 | 0 | } |
87 | 0 | col_set_str(pinfo->cinfo, COL_PROTOCOL, "FR PW"); |
88 | 0 | col_set_str(pinfo->cinfo, COL_INFO, "Malformed: PW packet < PW encapsulation header"); |
89 | 0 | return 1; |
90 | 0 | } |
91 | | |
92 | 0 | if (dissect_try_cw_first_nibble(tvb,pinfo,tree)) |
93 | 0 | { |
94 | 0 | return tvb_captured_length(tvb); |
95 | 0 | } |
96 | | |
97 | | /* check how "good" is this packet */ |
98 | | /* also decide payload length from packet size and CW */ |
99 | 0 | packet_quality = 0; |
100 | 0 | if (0 != (tvb_get_uint8(tvb, 0) & 0xf0 /*bits03*/)) |
101 | 0 | { |
102 | 0 | packet_quality |= PQ_CW_BAD + PQ_CW_BAD_BITS03; |
103 | 0 | } |
104 | 0 | { |
105 | | /* RFC4619: |
106 | | * [ If the frame's length (defined as the |
107 | | * length of the layer 2 payload plus the length of the control word) |
108 | | * is less than 64 octets, the length field MUST be set to the PW |
109 | | * payload length. Otherwise, the length field MUST be set to zero. ] |
110 | | * |
111 | | * Note difference from RFC4385 which states that: |
112 | | * [..the length field MUST be set to the length of the PW payload |
113 | | * *plus* the length of the *PWMCW*. ] |
114 | | */ |
115 | 0 | int cw_len; |
116 | 0 | int payload_size_packet; /*derived from packet size*/ |
117 | |
|
118 | 0 | cw_len = tvb_get_uint8(tvb, 1) & 0x3f; |
119 | 0 | payload_size_packet = packet_size - encaps_size; |
120 | | |
121 | | /* |
122 | | * Initial assumptions. |
123 | | */ |
124 | 0 | payload_size = payload_size_packet; |
125 | 0 | payload_padding = 0; |
126 | |
|
127 | 0 | if (payload_size_packet < 64) |
128 | 0 | { |
129 | 0 | int payload_size_cw; /*derived from cw*/ |
130 | 0 | payload_size_cw = cw_len; /*RFC4619-specific*/ |
131 | 0 | if (payload_size_cw == 0) |
132 | 0 | { |
133 | 0 | packet_quality |= PQ_CW_BAD + PQ_CW_BAD_LEN_MUST_BE_NONZERO; |
134 | 0 | } |
135 | 0 | else if (payload_size_cw > payload_size_packet) |
136 | 0 | { |
137 | 0 | packet_quality |= PQ_CW_BAD + PQ_CW_BAD_LEN_GT_PACKET; |
138 | 0 | } |
139 | 0 | else /* ok */ |
140 | 0 | { |
141 | 0 | payload_size = payload_size_cw; |
142 | 0 | payload_padding = payload_size_packet - payload_size_cw; /* >=0 */ |
143 | 0 | } |
144 | 0 | } |
145 | 0 | else /* payload_size_packet >= 64 */ |
146 | 0 | { |
147 | 0 | if (cw_len != 0) |
148 | 0 | { |
149 | 0 | packet_quality |= PQ_CW_BAD + PQ_CW_BAD_LEN_MUST_BE_ZERO; |
150 | 0 | } |
151 | 0 | } |
152 | 0 | } |
153 | 0 | if (payload_size == 0) |
154 | 0 | { |
155 | 0 | packet_quality |= PQ_PAYLOAD_SIZE_ZERO; |
156 | 0 | } |
157 | |
|
158 | 0 | col_set_str(pinfo->cinfo, COL_PROTOCOL, "FR PW"); |
159 | 0 | col_clear(pinfo->cinfo, COL_INFO); |
160 | 0 | if (packet_quality & PQ_CW_BAD) |
161 | 0 | { |
162 | 0 | col_set_str(pinfo->cinfo, COL_INFO, "CW:Malformed, "); |
163 | 0 | } |
164 | 0 | col_append_fstr(pinfo->cinfo, COL_INFO, "%d payload octets", (int)payload_size); |
165 | |
|
166 | 0 | if (payload_padding != 0) |
167 | 0 | { |
168 | 0 | col_append_fstr(pinfo->cinfo, COL_INFO, ", %d padding", (int)payload_padding); |
169 | 0 | } |
170 | |
|
171 | 0 | { |
172 | 0 | proto_tree* subtree; |
173 | 0 | proto_item* item_headline; |
174 | 0 | proto_item* item; |
175 | |
|
176 | 0 | item_headline = proto_tree_add_item(tree, proto_encaps, tvb, 0, 4, ENC_NA); |
177 | 0 | proto_item_append_text(item_headline, ": 0x%.8" PRIx32, tvb_get_ntohl(tvb, 0)); |
178 | 0 | subtree = proto_item_add_subtree(item_headline, ett_encaps); |
179 | |
|
180 | 0 | if (packet_quality & PQ_CW_BAD_BITS03) /*display only if value is wrong*/ |
181 | 0 | { |
182 | 0 | item = proto_tree_add_item(subtree, hf_cw_bits03, tvb, 0, 1, ENC_BIG_ENDIAN); |
183 | 0 | expert_add_info(pinfo, item, &ei_cw_bits03); |
184 | 0 | } |
185 | |
|
186 | 0 | proto_tree_add_item( subtree, hf_cw_fecn, tvb, 0, 1, ENC_BIG_ENDIAN ); |
187 | 0 | proto_tree_add_item( subtree, hf_cw_becn, tvb, 0, 1, ENC_BIG_ENDIAN ); |
188 | 0 | proto_tree_add_item( subtree, hf_cw_de, tvb, 0, 1, ENC_BIG_ENDIAN ); |
189 | 0 | proto_tree_add_item( subtree, hf_cw_cr, tvb, 0, 1, ENC_BIG_ENDIAN ); |
190 | 0 | proto_tree_add_item( subtree, hf_cw_frg, tvb, 1, 1, ENC_BIG_ENDIAN ); |
191 | |
|
192 | 0 | item = proto_tree_add_item( subtree, hf_cw_len, tvb, 1, 1, ENC_BIG_ENDIAN ); |
193 | 0 | if (packet_quality & PQ_CW_BAD_LEN_GT_PACKET) |
194 | 0 | { |
195 | 0 | expert_add_info_format(pinfo, item, &ei_payload_size_invalid, |
196 | 0 | "Bad Length: greater than FR payload size (%d)", |
197 | 0 | (int)payload_size); |
198 | 0 | } |
199 | 0 | if (packet_quality & PQ_CW_BAD_LEN_MUST_BE_NONZERO) |
200 | 0 | { |
201 | 0 | expert_add_info_format(pinfo, item, &ei_payload_size_invalid, |
202 | 0 | "Bad Length: must be non-zero if FR PW packet size (%d) is < 64", |
203 | 0 | (int)(payload_size+encaps_size)); |
204 | 0 | } |
205 | 0 | if (packet_quality & PQ_CW_BAD_LEN_MUST_BE_ZERO) |
206 | 0 | { |
207 | 0 | expert_add_info_format(pinfo, item, &ei_payload_size_invalid, |
208 | 0 | "Bad Length: must be 0 if FR PW packet size (%d) is >= 64", |
209 | 0 | (int)(payload_size+encaps_size)); |
210 | 0 | } |
211 | |
|
212 | 0 | proto_tree_add_item( subtree, hf_cw_seq, tvb, 2, 2, ENC_BIG_ENDIAN ); |
213 | |
|
214 | 0 | if (payload_padding > 0) |
215 | 0 | { |
216 | 0 | proto_tree_add_item(subtree, hf_cw_padding, tvb, |
217 | 0 | encaps_size+payload_size, payload_padding, ENC_NA); |
218 | 0 | } |
219 | |
|
220 | 0 | if (packet_quality & PQ_PAYLOAD_SIZE_ZERO) |
221 | 0 | { |
222 | 0 | expert_add_info_format(pinfo, item_headline, &ei_payload_size_invalid, |
223 | 0 | "FR payload size must be non-zero"); |
224 | 0 | } |
225 | |
|
226 | 0 | } |
227 | 0 | if (payload_size > 0) |
228 | 0 | { |
229 | 0 | tvbuff_t *tvb_payload; |
230 | 0 | tvb_payload = tvb_new_subset_length(tvb, encaps_size, payload_size); |
231 | 0 | call_dissector( fr_stripped_address_handle, tvb_payload, pinfo, tree ); |
232 | 0 | } |
233 | 0 | return tvb_captured_length(tvb); |
234 | 0 | } |
235 | | |
236 | | |
237 | | void |
238 | | proto_register_pw_fr(void) |
239 | 14 | { |
240 | 14 | static hf_register_info hf[] = { |
241 | 14 | {&hf_cw_bits03 ,{"Bits 0 to 3" ,"pwfr.bits03" ,FT_UINT8 ,BASE_HEX |
242 | 14 | ,NULL ,0xf0 ,NULL |
243 | 14 | ,HFILL }}, |
244 | | |
245 | 14 | {&hf_cw_fecn ,{"FR FECN" ,"pwfr.fecn" ,FT_UINT8 ,BASE_DEC |
246 | 14 | ,NULL ,0x08 ,"FR Forward Explicit Congestion Notification bit" |
247 | 14 | ,HFILL}}, |
248 | | |
249 | 14 | {&hf_cw_becn ,{"FR BECN" ,"pwfr.becn" ,FT_UINT8 ,BASE_DEC |
250 | 14 | ,NULL ,0x04 ,"FR Backward Explicit Congestion Notification bit" |
251 | 14 | ,HFILL}}, |
252 | | |
253 | 14 | {&hf_cw_de ,{"FR DE bit" ,"pwfr.de" ,FT_UINT8 ,BASE_DEC |
254 | 14 | ,NULL ,0x02 ,"FR Discard Eligibility bit" |
255 | 14 | ,HFILL}}, |
256 | | |
257 | 14 | {&hf_cw_cr ,{"FR Frame C/R" ,"pwfr.cr" ,FT_UINT8 ,BASE_DEC |
258 | 14 | ,NULL ,0x01 ,"FR frame Command/Response bit" |
259 | 14 | ,HFILL}}, |
260 | | |
261 | 14 | {&hf_cw_frg ,{"Fragmentation" ,"pwfr.frag" ,FT_UINT8 ,BASE_DEC |
262 | 14 | ,VALS(vals_frg) ,0xc0 ,NULL |
263 | 14 | ,HFILL}}, |
264 | | |
265 | 14 | {&hf_cw_len ,{"Length" ,"pwfr.length" ,FT_UINT8 ,BASE_DEC |
266 | 14 | ,NULL ,0x3f ,NULL |
267 | 14 | ,HFILL}}, |
268 | | |
269 | 14 | {&hf_cw_seq ,{"Sequence number" ,"pwfr.seqno" ,FT_UINT16 ,BASE_DEC |
270 | 14 | ,NULL ,0 ,NULL |
271 | 14 | ,HFILL}}, |
272 | | |
273 | 14 | {&hf_cw_padding,{"Padding" ,"pwfr.padding" ,FT_BYTES, BASE_NONE |
274 | 14 | ,NULL ,0 ,NULL |
275 | 14 | ,HFILL}} |
276 | 14 | }; |
277 | | |
278 | 14 | static int *ett[] = { |
279 | 14 | &ett_encaps |
280 | 14 | }; |
281 | | |
282 | 14 | static ei_register_info ei[] = { |
283 | 14 | { &ei_cw_packet_size_too_small, { "pwfr.packet_size_too_small", PI_MALFORMED, PI_ERROR, "PW packet is smaller than PW encapsulation header", EXPFILL }}, |
284 | 14 | { &ei_cw_bits03, { "pwfr.cw.bits03.not_zero", PI_MALFORMED, PI_ERROR, "Bits 0..3 of Control Word must be 0", EXPFILL }}, |
285 | 14 | { &ei_payload_size_invalid, { "pwfr.payload.size_invalid", PI_MALFORMED, PI_ERROR, "Bad Length: greater than FR payload size", EXPFILL }}, |
286 | 14 | }; |
287 | 14 | expert_module_t* expert_pwfr; |
288 | | |
289 | 14 | proto_encaps = proto_register_protocol( "PW Frame Relay DLCI Control Word", |
290 | 14 | "Frame Relay DLCI PW", |
291 | 14 | "pwfr"); |
292 | 14 | proto_register_field_array(proto_encaps, hf, array_length(hf)); |
293 | 14 | proto_register_subtree_array(ett, array_length(ett)); |
294 | 14 | expert_pwfr = expert_register_protocol(proto_encaps); |
295 | 14 | expert_register_field_array(expert_pwfr, ei, array_length(ei)); |
296 | 14 | pw_fr_mpls_handle = register_dissector("pw_fr", dissect_pw_fr, proto_encaps ); |
297 | 14 | } |
298 | | |
299 | | |
300 | | void |
301 | | proto_reg_handoff_pw_fr(void) |
302 | 14 | { |
303 | 14 | dissector_add_for_decode_as("mpls.label", pw_fr_mpls_handle); |
304 | 14 | dissector_add_for_decode_as("mpls.pfn", pw_fr_mpls_handle); |
305 | 14 | fr_stripped_address_handle = find_dissector_add_dependency("fr_stripped_address", proto_encaps); |
306 | 14 | } |
307 | | |
308 | | /* |
309 | | * Editor modelines - https://www.wireshark.org/tools/modelines.html |
310 | | * |
311 | | * Local variables: |
312 | | * c-basic-offset: 8 |
313 | | * tab-width: 8 |
314 | | * indent-tabs-mode: t |
315 | | * End: |
316 | | * |
317 | | * vi: set shiftwidth=8 tabstop=8 noexpandtab: |
318 | | * :indentSize=8:tabSize=8:noTabs=false: |
319 | | */ |