(Trivial) Fix spellling in a comment.
[metze/wireshark/wip.git] / epan / xdlc.c
1 /* xdlc.c
2  * Routines for use by various SDLC-derived protocols, such as HDLC
3  * and its derivatives LAPB, IEEE 802.2 LLC, etc..
4  *
5  * $Id$
6  *
7  * Wireshark - Network traffic analyzer
8  * By Gerald Combs <gerald@wireshark.org>
9  * Copyright 1998 Gerald Combs
10  *
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.
15  *
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.
20  *
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.
24  */
25
26 #include "config.h"
27
28 #include <stdio.h>
29 #include <string.h>
30
31 #include <glib.h>
32 #include <epan/packet.h>
33 #include <epan/xdlc.h>
34 #include <epan/emem.h>
35 #include <wsutil/pint.h>
36
37 const value_string ftype_vals[] = {
38     { XDLC_I, "Information frame" },
39     { XDLC_S, "Supervisory frame" },
40     { XDLC_U, "Unnumbered frame" },
41     { 0,      NULL }
42 };
43
44 const value_string stype_vals[] = {
45     { XDLC_RR>>2,   "Receiver ready" },
46     { XDLC_RNR>>2,  "Receiver not ready" },
47     { XDLC_REJ>>2,  "Reject" },
48     { XDLC_SREJ>>2, "Selective reject" },
49     { 0,            NULL }
50 };
51
52 static const value_string modifier_short_vals_cmd[] = {
53     { XDLC_UI,    "UI" },
54     { XDLC_UP,    "UP" },
55     { XDLC_DISC,  "DISC" },
56     { XDLC_UA,    "UA" },
57     { XDLC_SNRM,  "SNRM" },
58     { XDLC_SNRME, "SNRME" },
59     { XDLC_TEST,  "TEST" },
60     { XDLC_SIM,   "SIM" },
61     { XDLC_FRMR,  "FRMR" },
62     { XDLC_CFGR,  "CFGR" },
63     { XDLC_SARM,  "SARM" },
64     { XDLC_SABM,  "SABM" },
65     { XDLC_SARME, "SARME" },
66     { XDLC_SABME, "SABME" },
67     { XDLC_RESET, "RESET" },
68     { XDLC_XID,   "XID" },
69     { XDLC_SNRME, "SNRME" },
70     { XDLC_BCN,   "BCN" },
71     { 0,          NULL }
72 };
73
74 const value_string modifier_vals_cmd[] = {
75     { XDLC_UI>>2,    "Unnumbered Information" },
76     { XDLC_UP>>2,    "Unnumbered Poll" },
77     { XDLC_DISC>>2,  "Disconnect" },
78     { XDLC_UA>>2,    "Unnumbered Acknowledge" },
79     { XDLC_SNRM>>2,  "Set Normal Response Mode" },
80     { XDLC_TEST>>2,  "Test" },
81     { XDLC_SIM>>2,   "Set Initialization Mode" },
82     { XDLC_FRMR>>2,  "Frame reject" },
83     { XDLC_CFGR>>2,  "Configure" },
84     { XDLC_SARM>>2,  "Set Asynchronous Response Mode" },
85     { XDLC_SABM>>2,  "Set Asynchronous Balanced Mode" },
86     { XDLC_SARME>>2, "Set Asynchronous Response Mode Extended" },
87     { XDLC_SABME>>2, "Set Asynchronous Balanced Mode Extended" },
88     { XDLC_RESET>>2, "Reset" },
89     { XDLC_XID>>2,   "Exchange identification" },
90     { XDLC_SNRME>>2, "Set Normal Response Mode Extended" },
91     { XDLC_BCN>>2,   "Beacon" },
92     { 0,             NULL }
93 };
94
95 static const value_string modifier_short_vals_resp[] = {
96     { XDLC_UI,    "UI" },
97     { XDLC_UP,    "UP" },
98     { XDLC_RD,    "RD" },
99     { XDLC_UA,    "UA" },
100     { XDLC_SNRM,  "SNRM" },
101     { XDLC_TEST,  "TEST" },
102     { XDLC_RIM,   "RIM" },
103     { XDLC_FRMR,  "FRMR" },
104     { XDLC_CFGR,  "CFGR" },
105     { XDLC_DM,    "DM" },
106     { XDLC_SABM,  "SABM" },
107     { XDLC_SARME, "SARME" },
108     { XDLC_SABME, "SABME" },
109     { XDLC_RESET, "RESET" },
110     { XDLC_XID,   "XID" },
111     { XDLC_SNRME, "SNRME" },
112     { XDLC_BCN,   "BCN" },
113     { 0,          NULL }
114 };
115
116 const value_string modifier_vals_resp[] = {
117     { XDLC_UI>>2,    "Unnumbered Information" },
118     { XDLC_UP>>2,    "Unnumbered Poll" },
119     { XDLC_RD>>2,    "Request Disconnect" },
120     { XDLC_UA>>2,    "Unnumbered Acknowledge" },
121     { XDLC_SNRM>>2,  "Set Normal Response Mode" },
122     { XDLC_TEST>>2,  "Test" },
123     { XDLC_RIM>>2,   "Request Initialization Mode" },
124     { XDLC_FRMR>>2,  "Frame reject" },
125     { XDLC_CFGR>>2,  "Configure" },
126     { XDLC_DM>>2,    "Disconnected mode" },
127     { XDLC_SABM>>2,  "Set Asynchronous Balanced Mode" },
128     { XDLC_SARME>>2, "Set Asynchronous Response Mode Extended" },
129     { XDLC_SABME>>2, "Set Asynchronous Balanced Mode Extended" },
130     { XDLC_RESET>>2, "Reset" },
131     { XDLC_XID>>2,   "Exchange identification" },
132     { XDLC_SNRME>>2, "Set Normal Response Mode Extended" },
133     { XDLC_BCN>>2,   "Beacon" },
134     { 0,             NULL }
135 };
136
137 int
138 get_xdlc_control(const guchar *pd, int offset, gboolean is_extended)
139 {
140     guint16 control;
141
142     switch (pd[offset] & 0x03) {
143
144     case XDLC_S:
145     default:
146         /*
147          * Supervisory or Information frame.
148          */
149         if (is_extended)
150                 control = pletoh16(&pd[offset]);
151         else
152                 control = pd[offset];
153         break;
154
155     case XDLC_U:
156         /*
157          * Unnumbered frame.
158          *
159          * XXX - is this two octets, with a P/F bit, in HDLC extended
160          * operation?  It's one octet in LLC, even though the control
161          * field of I and S frames is a 2-byte extended-operation field
162          * in LLC.  Given that there are no sequence numbers in the
163          * control field of a U frame, there doesn't appear to be any
164          * need for it to be 2 bytes in extended operation.
165          */
166         control = pd[offset];
167         break;
168     }
169     return control;
170 }
171
172 /*
173  * Check whether the control field of the packet looks valid.
174  */
175 gboolean
176 check_xdlc_control(tvbuff_t *tvb, int offset,
177   const value_string *u_modifier_short_vals_cmd,
178   const value_string *u_modifier_short_vals_resp, gboolean is_response,
179   gboolean is_extended _U_)
180 {
181     guint16 control;
182
183     if (!tvb_bytes_exist(tvb, offset, 1))
184         return FALSE;   /* not enough data to check */
185     switch (tvb_get_guint8(tvb, offset) & 0x03) {
186
187     case XDLC_S:
188         /*
189          * Supervisory frame.
190          * No fields to check for validity here.
191          */
192         return TRUE;
193
194     case XDLC_U:
195         /*
196          * Unnumbered frame.
197          *
198          * XXX - is this two octets, with a P/F bit, in HDLC extended
199          * operation?  It's one octet in LLC, even though the control
200          * field of I and S frames is a 2-byte extended-operation field
201          * in LLC.  Given that there are no sequence numbers in the
202          * control field of a U frame, there doesn't appear to be any
203          * need for it to be 2 bytes in extended operation.
204          */
205         if (u_modifier_short_vals_cmd == NULL)
206                 u_modifier_short_vals_cmd = modifier_short_vals_cmd;
207         if (u_modifier_short_vals_resp == NULL)
208                 u_modifier_short_vals_resp = modifier_short_vals_resp;
209         control = tvb_get_guint8(tvb, offset);
210         if (is_response) {
211                 if (try_val_to_str(control & XDLC_U_MODIFIER_MASK,
212                     u_modifier_short_vals_resp) == NULL)
213                         return FALSE;   /* unknown modifier */
214         } else {
215                 if (try_val_to_str(control & XDLC_U_MODIFIER_MASK,
216                     u_modifier_short_vals_cmd) == NULL)
217                         return FALSE;   /* unknown modifier */
218         }
219         return TRUE;
220
221     default:
222         /*
223          * Information frame.
224          * No fields to check for validity here.
225          */
226         return TRUE;
227     }
228 }
229
230 int
231 dissect_xdlc_control(tvbuff_t *tvb, int offset, packet_info *pinfo,
232   proto_tree *xdlc_tree, int hf_xdlc_control, gint ett_xdlc_control,
233   const xdlc_cf_items *cf_items_nonext, const xdlc_cf_items *cf_items_ext,
234   const value_string *u_modifier_short_vals_cmd,
235   const value_string *u_modifier_short_vals_resp, gboolean is_response,
236   gboolean is_extended, gboolean append_info)
237 {
238     guint16 control;
239     int control_len;
240     const xdlc_cf_items *cf_items;
241     const char *control_format;
242     guint16 poll_final;
243     char *info;
244     proto_tree *tc, *control_tree;
245     const gchar *frame_type = NULL;
246     const gchar *modifier;
247
248     info=(char *)ep_alloc(80);
249     switch (tvb_get_guint8(tvb, offset) & 0x03) {
250
251     case XDLC_S:
252         /*
253          * Supervisory frame.
254          */
255         if (is_extended) {
256             control = tvb_get_letohs(tvb, offset);
257             control_len = 2;
258             cf_items = cf_items_ext;
259             control_format = "Control field: %s (0x%04X)";
260         } else {
261             control = tvb_get_guint8(tvb, offset);
262             control_len = 1;
263             cf_items = cf_items_nonext;
264             control_format = "Control field: %s (0x%02X)";
265         }
266         switch (control & XDLC_S_FTYPE_MASK) {
267         case XDLC_RR:
268             frame_type = "RR";
269             break;
270
271         case XDLC_RNR:
272             frame_type = "RNR";
273             break;
274
275         case XDLC_REJ:
276             frame_type = "REJ";
277             break;
278
279         case XDLC_SREJ:
280             frame_type = "SREJ";
281             break;
282         }
283         if (is_extended) {
284             poll_final = (control & XDLC_P_F_EXT);
285             g_snprintf(info, 80, "S%s, func=%s, N(R)=%u",
286                         (poll_final ?
287                             (is_response ? " F" : " P") :
288                             ""),
289                         frame_type,
290                         (control & XDLC_N_R_EXT_MASK) >> XDLC_N_R_EXT_SHIFT);
291         } else {
292             poll_final = (control & XDLC_P_F);
293             g_snprintf(info, 80, "S%s, func=%s, N(R)=%u",
294                         (poll_final ?
295                             (is_response ? " F" : " P") :
296                             ""),
297                         frame_type,
298                         (control & XDLC_N_R_MASK) >> XDLC_N_R_SHIFT);
299         }
300         if (append_info) {
301             col_append_str(pinfo->cinfo, COL_INFO, ", ");
302             col_append_str(pinfo->cinfo, COL_INFO, info);
303         } else {
304             col_add_str(pinfo->cinfo, COL_INFO, info);
305         }
306         if (xdlc_tree) {
307             tc = proto_tree_add_uint_format(xdlc_tree, hf_xdlc_control, tvb,
308                 offset, control_len, control, control_format, info, control);
309             control_tree = proto_item_add_subtree(tc, ett_xdlc_control);
310             proto_tree_add_uint(control_tree, *cf_items->hf_xdlc_n_r,
311                 tvb, offset, control_len, control);
312             if (poll_final) {
313                 proto_tree_add_boolean(control_tree,
314                         (is_response ? *cf_items->hf_xdlc_f :
315                                        *cf_items->hf_xdlc_p),
316                         tvb, offset, control_len, control);
317             }
318             proto_tree_add_uint(control_tree, *cf_items->hf_xdlc_s_ftype,
319                 tvb, offset, control_len, control);
320             /* This will always say it's a supervisory frame */
321             proto_tree_add_uint(control_tree, *cf_items->hf_xdlc_ftype_s_u,
322                 tvb, offset, control_len, control);
323         }
324         break;
325
326     case XDLC_U:
327         /*
328          * Unnumbered frame.
329          *
330          * XXX - is this two octets, with a P/F bit, in HDLC extended
331          * operation?  It's one octet in LLC, even though the control
332          * field of I and S frames is a 2-byte extended-operation field
333          * in LLC.  Given that there are no sequence numbers in the
334          * control field of a U frame, there doesn't appear to be any
335          * need for it to be 2 bytes in extended operation.
336          */
337         if (u_modifier_short_vals_cmd == NULL)
338                 u_modifier_short_vals_cmd = modifier_short_vals_cmd;
339         if (u_modifier_short_vals_resp == NULL)
340                 u_modifier_short_vals_resp = modifier_short_vals_resp;
341         control = tvb_get_guint8(tvb, offset);
342         control_len = 1;
343         cf_items = cf_items_nonext;
344         control_format = "Control field: %s (0x%02X)";
345         if (is_response) {
346                 modifier = val_to_str(control & XDLC_U_MODIFIER_MASK,
347                         u_modifier_short_vals_resp, "Unknown");
348         } else {
349                 modifier = val_to_str(control & XDLC_U_MODIFIER_MASK,
350                         u_modifier_short_vals_cmd, "Unknown");
351         }
352         poll_final = (control & XDLC_P_F);
353         g_snprintf(info, 80, "U%s, func=%s",
354                 (poll_final ?
355                     (is_response ? " F" : " P") :
356                     ""),
357                 modifier);
358         if (append_info) {
359             col_append_str(pinfo->cinfo, COL_INFO, ", ");
360         col_append_str(pinfo->cinfo, COL_INFO, info);
361         } else {
362             col_add_str(pinfo->cinfo, COL_INFO, info);
363         }
364         if (xdlc_tree) {
365             tc = proto_tree_add_uint_format(xdlc_tree, hf_xdlc_control, tvb,
366                 offset, control_len, control, control_format, info, control);
367             control_tree = proto_item_add_subtree(tc, ett_xdlc_control);
368             if (poll_final) {
369                 proto_tree_add_boolean(control_tree,
370                         (is_response ? *cf_items->hf_xdlc_f:
371                                        *cf_items->hf_xdlc_p),
372                         tvb, offset, control_len, control);
373             }
374             proto_tree_add_uint(control_tree,
375                 (is_response ? *cf_items->hf_xdlc_u_modifier_resp :
376                                *cf_items->hf_xdlc_u_modifier_cmd),
377                 tvb, offset, control_len, control);
378             /* This will always say it's an unnumbered frame */
379             proto_tree_add_uint(control_tree, *cf_items->hf_xdlc_ftype_s_u,
380                 tvb, offset, control_len, control);
381         }
382         break;
383
384     default:
385         /*
386          * Information frame.
387          */
388         if (is_extended) {
389             control = tvb_get_letohs(tvb, offset);
390             control_len = 2;
391             cf_items = cf_items_ext;
392             control_format = "Control field: %s (0x%04X)";
393             poll_final = (control & XDLC_P_F_EXT);
394             g_snprintf(info, 80, "I%s, N(R)=%u, N(S)=%u",
395                         ((control & XDLC_P_F_EXT) ? " P" : ""),
396                         (control & XDLC_N_R_EXT_MASK) >> XDLC_N_R_EXT_SHIFT,
397                         (control & XDLC_N_S_EXT_MASK) >> XDLC_N_S_EXT_SHIFT);
398         } else {
399             control = tvb_get_guint8(tvb, offset);
400             control_len = 1;
401             cf_items = cf_items_nonext;
402             control_format = "Control field: %s (0x%02X)";
403             poll_final = (control & XDLC_P_F);
404             g_snprintf(info, 80, "I%s, N(R)=%u, N(S)=%u",
405                         ((control & XDLC_P_F) ? " P" : ""),
406                         (control & XDLC_N_R_MASK) >> XDLC_N_R_SHIFT,
407                         (control & XDLC_N_S_MASK) >> XDLC_N_S_SHIFT);
408         }
409         if (append_info) {
410             col_append_str(pinfo->cinfo, COL_INFO, ", ");
411         col_append_str(pinfo->cinfo, COL_INFO, info);
412         } else {
413             col_add_str(pinfo->cinfo, COL_INFO, info);
414         }
415         if (xdlc_tree) {
416             tc = proto_tree_add_uint_format(xdlc_tree, hf_xdlc_control, tvb,
417                 offset, control_len, control, control_format, info, control);
418             control_tree = proto_item_add_subtree(tc, ett_xdlc_control);
419             proto_tree_add_uint(control_tree, *cf_items->hf_xdlc_n_r,
420                 tvb, offset, control_len, control);
421             proto_tree_add_uint(control_tree, *cf_items->hf_xdlc_n_s,
422                 tvb, offset, control_len, control);
423             if (poll_final) {
424                 proto_tree_add_boolean(control_tree, *cf_items->hf_xdlc_p,
425                         tvb, offset, control_len, control);
426             }
427             /* This will always say it's an information frame */
428             proto_tree_add_uint(control_tree, *cf_items->hf_xdlc_ftype_i,
429                 tvb, offset, control_len, control);
430         }
431         break;
432     }
433     return control;
434 }