2 * Routines for use by various SDLC-derived protocols, such as HDLC
3 * and its derivatives LAPB, IEEE 802.2 LLC, etc..
7 * Wireshark - Network traffic analyzer
8 * By Gerald Combs <gerald@wireshark.org>
9 * Copyright 1998 Gerald Combs
11 * This program is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU General Public License
13 * as published by the Free Software Foundation; either version 2
14 * of the License, or (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
32 #include <epan/packet.h>
33 #include <epan/xdlc.h>
34 #include <epan/emem.h>
36 const value_string ftype_vals[] = {
37 { XDLC_I, "Information frame" },
38 { XDLC_S, "Supervisory frame" },
39 { XDLC_U, "Unnumbered frame" },
43 const value_string stype_vals[] = {
44 { XDLC_RR>>2, "Receiver ready" },
45 { XDLC_RNR>>2, "Receiver not ready" },
46 { XDLC_REJ>>2, "Reject" },
47 { XDLC_SREJ>>2, "Selective reject" },
51 static const value_string modifier_short_vals_cmd[] = {
54 { XDLC_DISC, "DISC" },
56 { XDLC_SNRM, "SNRM" },
57 { XDLC_SNRME, "SNRME" },
58 { XDLC_TEST, "TEST" },
60 { XDLC_FRMR, "FRMR" },
61 { XDLC_CFGR, "CFGR" },
62 { XDLC_SARM, "SARM" },
63 { XDLC_SABM, "SABM" },
64 { XDLC_SARME, "SARME" },
65 { XDLC_SABME, "SABME" },
66 { XDLC_RESET, "RESET" },
68 { XDLC_SNRME, "SNRME" },
73 const value_string modifier_vals_cmd[] = {
74 { XDLC_UI>>2, "Unnumbered Information" },
75 { XDLC_UP>>2, "Unnumbered Poll" },
76 { XDLC_DISC>>2, "Disconnect" },
77 { XDLC_UA>>2, "Unnumbered Acknowledge" },
78 { XDLC_SNRM>>2, "Set Normal Response Mode" },
79 { XDLC_TEST>>2, "Test" },
80 { XDLC_SIM>>2, "Set Initialization Mode" },
81 { XDLC_FRMR>>2, "Frame reject" },
82 { XDLC_CFGR>>2, "Configure" },
83 { XDLC_SARM>>2, "Set Asynchronous Response Mode" },
84 { XDLC_SABM>>2, "Set Asynchronous Balanced Mode" },
85 { XDLC_SARME>>2, "Set Asynchronous Response Mode Extended" },
86 { XDLC_SABME>>2, "Set Asynchronous Balanced Mode Extended" },
87 { XDLC_RESET>>2, "Reset" },
88 { XDLC_XID>>2, "Exchange identification" },
89 { XDLC_SNRME>>2, "Set Normal Response Mode Extended" },
90 { XDLC_BCN>>2, "Beacon" },
94 static const value_string modifier_short_vals_resp[] = {
99 { XDLC_SNRM, "SNRM" },
100 { XDLC_TEST, "TEST" },
102 { XDLC_FRMR, "FRMR" },
103 { XDLC_CFGR, "CFGR" },
105 { XDLC_SABM, "SABM" },
106 { XDLC_SARME, "SARME" },
107 { XDLC_SABME, "SABME" },
108 { XDLC_RESET, "RESET" },
110 { XDLC_SNRME, "SNRME" },
115 const value_string modifier_vals_resp[] = {
116 { XDLC_UI>>2, "Unnumbered Information" },
117 { XDLC_UP>>2, "Unnumbered Poll" },
118 { XDLC_RD>>2, "Request Disconnect" },
119 { XDLC_UA>>2, "Unnumbered Acknowledge" },
120 { XDLC_SNRM>>2, "Set Normal Response Mode" },
121 { XDLC_TEST>>2, "Test" },
122 { XDLC_RIM>>2, "Request Initialization Mode" },
123 { XDLC_FRMR>>2, "Frame reject" },
124 { XDLC_CFGR>>2, "Configure" },
125 { XDLC_DM>>2, "Disconnected mode" },
126 { XDLC_SABM>>2, "Set Asynchronous Balanced Mode" },
127 { XDLC_SARME>>2, "Set Asynchronous Response Mode Extended" },
128 { XDLC_SABME>>2, "Set Asynchronous Balanced Mode Extended" },
129 { XDLC_RESET>>2, "Reset" },
130 { XDLC_XID>>2, "Exchange identification" },
131 { XDLC_SNRME>>2, "Set Normal Response Mode Extended" },
132 { XDLC_BCN>>2, "Beacon" },
137 get_xdlc_control(const guchar *pd, int offset, gboolean is_extended)
141 switch (pd[offset] & 0x03) {
146 * Supervisory or Information frame.
149 control = pletohs(&pd[offset]);
151 control = pd[offset];
158 * XXX - is this two octets, with a P/F bit, in HDLC extended
159 * operation? It's one octet in LLC, even though the control
160 * field of I and S frames is a 2-byte extended-operation field
161 * in LLC. Given that there are no sequence numbers in the
162 * control field of a U frame, there doesn't appear to be any
163 * need for it to be 2 bytes in extended operation.
165 control = pd[offset];
172 * Check whether the control field of the packet looks valid.
175 check_xdlc_control(tvbuff_t *tvb, int offset,
176 const value_string *u_modifier_short_vals_cmd,
177 const value_string *u_modifier_short_vals_resp, gboolean is_response,
178 gboolean is_extended _U_)
182 if (!tvb_bytes_exist(tvb, offset, 1))
183 return FALSE; /* not enough data to check */
184 switch (tvb_get_guint8(tvb, offset) & 0x03) {
189 * No fields to check for validity here.
197 * XXX - is this two octets, with a P/F bit, in HDLC extended
198 * operation? It's one octet in LLC, even though the control
199 * field of I and S frames is a 2-byte extended-operation field
200 * in LLC. Given that there are no sequence numbers in the
201 * control field of a U frame, there doesn't appear to be any
202 * need for it to be 2 bytes in extended operation.
204 if (u_modifier_short_vals_cmd == NULL)
205 u_modifier_short_vals_cmd = modifier_short_vals_cmd;
206 if (u_modifier_short_vals_resp == NULL)
207 u_modifier_short_vals_resp = modifier_short_vals_resp;
208 control = tvb_get_guint8(tvb, offset);
210 if (try_val_to_str(control & XDLC_U_MODIFIER_MASK,
211 u_modifier_short_vals_resp) == NULL)
212 return FALSE; /* unknown modifier */
214 if (try_val_to_str(control & XDLC_U_MODIFIER_MASK,
215 u_modifier_short_vals_cmd) == NULL)
216 return FALSE; /* unknown modifier */
223 * No fields to check for validity here.
230 dissect_xdlc_control(tvbuff_t *tvb, int offset, packet_info *pinfo,
231 proto_tree *xdlc_tree, int hf_xdlc_control, gint ett_xdlc_control,
232 const xdlc_cf_items *cf_items_nonext, const xdlc_cf_items *cf_items_ext,
233 const value_string *u_modifier_short_vals_cmd,
234 const value_string *u_modifier_short_vals_resp, gboolean is_response,
235 gboolean is_extended, gboolean append_info)
239 const xdlc_cf_items *cf_items;
240 const char *control_format;
243 proto_tree *tc, *control_tree;
244 const gchar *frame_type = NULL;
245 const gchar *modifier;
247 info=(char *)ep_alloc(80);
248 switch (tvb_get_guint8(tvb, offset) & 0x03) {
255 control = tvb_get_letohs(tvb, offset);
257 cf_items = cf_items_ext;
258 control_format = "Control field: %s (0x%04X)";
260 control = tvb_get_guint8(tvb, offset);
262 cf_items = cf_items_nonext;
263 control_format = "Control field: %s (0x%02X)";
265 switch (control & XDLC_S_FTYPE_MASK) {
283 poll_final = (control & XDLC_P_F_EXT);
284 g_snprintf(info, 80, "S%s, func=%s, N(R)=%u",
286 (is_response ? " F" : " P") :
289 (control & XDLC_N_R_EXT_MASK) >> XDLC_N_R_EXT_SHIFT);
291 poll_final = (control & XDLC_P_F);
292 g_snprintf(info, 80, "S%s, func=%s, N(R)=%u",
294 (is_response ? " F" : " P") :
297 (control & XDLC_N_R_MASK) >> XDLC_N_R_SHIFT);
300 col_append_str(pinfo->cinfo, COL_INFO, ", ");
301 col_append_str(pinfo->cinfo, COL_INFO, info);
303 col_add_str(pinfo->cinfo, COL_INFO, info);
306 tc = proto_tree_add_uint_format(xdlc_tree, hf_xdlc_control, tvb,
307 offset, control_len, control, control_format, info, control);
308 control_tree = proto_item_add_subtree(tc, ett_xdlc_control);
309 proto_tree_add_uint(control_tree, *cf_items->hf_xdlc_n_r,
310 tvb, offset, control_len, control);
312 proto_tree_add_boolean(control_tree,
313 (is_response ? *cf_items->hf_xdlc_f :
314 *cf_items->hf_xdlc_p),
315 tvb, offset, control_len, control);
317 proto_tree_add_uint(control_tree, *cf_items->hf_xdlc_s_ftype,
318 tvb, offset, control_len, control);
319 /* This will always say it's a supervisory frame */
320 proto_tree_add_uint(control_tree, *cf_items->hf_xdlc_ftype_s_u,
321 tvb, offset, control_len, control);
329 * XXX - is this two octets, with a P/F bit, in HDLC extended
330 * operation? It's one octet in LLC, even though the control
331 * field of I and S frames is a 2-byte extended-operation field
332 * in LLC. Given that there are no sequence numbers in the
333 * control field of a U frame, there doesn't appear to be any
334 * need for it to be 2 bytes in extended operation.
336 if (u_modifier_short_vals_cmd == NULL)
337 u_modifier_short_vals_cmd = modifier_short_vals_cmd;
338 if (u_modifier_short_vals_resp == NULL)
339 u_modifier_short_vals_resp = modifier_short_vals_resp;
340 control = tvb_get_guint8(tvb, offset);
342 cf_items = cf_items_nonext;
343 control_format = "Control field: %s (0x%02X)";
345 modifier = val_to_str(control & XDLC_U_MODIFIER_MASK,
346 u_modifier_short_vals_resp, "Unknown");
348 modifier = val_to_str(control & XDLC_U_MODIFIER_MASK,
349 u_modifier_short_vals_cmd, "Unknown");
351 poll_final = (control & XDLC_P_F);
352 g_snprintf(info, 80, "U%s, func=%s",
354 (is_response ? " F" : " P") :
358 col_append_str(pinfo->cinfo, COL_INFO, ", ");
359 col_append_str(pinfo->cinfo, COL_INFO, info);
361 col_add_str(pinfo->cinfo, COL_INFO, info);
364 tc = proto_tree_add_uint_format(xdlc_tree, hf_xdlc_control, tvb,
365 offset, control_len, control, control_format, info, control);
366 control_tree = proto_item_add_subtree(tc, ett_xdlc_control);
368 proto_tree_add_boolean(control_tree,
369 (is_response ? *cf_items->hf_xdlc_f:
370 *cf_items->hf_xdlc_p),
371 tvb, offset, control_len, control);
373 proto_tree_add_uint(control_tree,
374 (is_response ? *cf_items->hf_xdlc_u_modifier_resp :
375 *cf_items->hf_xdlc_u_modifier_cmd),
376 tvb, offset, control_len, control);
377 /* This will always say it's an unnumbered frame */
378 proto_tree_add_uint(control_tree, *cf_items->hf_xdlc_ftype_s_u,
379 tvb, offset, control_len, control);
388 control = tvb_get_letohs(tvb, offset);
390 cf_items = cf_items_ext;
391 control_format = "Control field: %s (0x%04X)";
392 poll_final = (control & XDLC_P_F_EXT);
393 g_snprintf(info, 80, "I%s, N(R)=%u, N(S)=%u",
394 ((control & XDLC_P_F_EXT) ? " P" : ""),
395 (control & XDLC_N_R_EXT_MASK) >> XDLC_N_R_EXT_SHIFT,
396 (control & XDLC_N_S_EXT_MASK) >> XDLC_N_S_EXT_SHIFT);
398 control = tvb_get_guint8(tvb, offset);
400 cf_items = cf_items_nonext;
401 control_format = "Control field: %s (0x%02X)";
402 poll_final = (control & XDLC_P_F);
403 g_snprintf(info, 80, "I%s, N(R)=%u, N(S)=%u",
404 ((control & XDLC_P_F) ? " P" : ""),
405 (control & XDLC_N_R_MASK) >> XDLC_N_R_SHIFT,
406 (control & XDLC_N_S_MASK) >> XDLC_N_S_SHIFT);
409 col_append_str(pinfo->cinfo, COL_INFO, ", ");
410 col_append_str(pinfo->cinfo, COL_INFO, info);
412 col_add_str(pinfo->cinfo, COL_INFO, info);
415 tc = proto_tree_add_uint_format(xdlc_tree, hf_xdlc_control, tvb,
416 offset, control_len, control, control_format, info, control);
417 control_tree = proto_item_add_subtree(tc, ett_xdlc_control);
418 proto_tree_add_uint(control_tree, *cf_items->hf_xdlc_n_r,
419 tvb, offset, control_len, control);
420 proto_tree_add_uint(control_tree, *cf_items->hf_xdlc_n_s,
421 tvb, offset, control_len, control);
423 proto_tree_add_boolean(control_tree, *cf_items->hf_xdlc_p,
424 tvb, offset, control_len, control);
426 /* This will always say it's an information frame */
427 proto_tree_add_uint(control_tree, *cf_items->hf_xdlc_ftype_i,
428 tvb, offset, control_len, control);