Mark an unused parameter as such
[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
36 const value_string ftype_vals[] = {
37     { XDLC_I, "Information frame" },
38     { XDLC_S, "Supervisory frame" },
39     { XDLC_U, "Unnumbered frame" },
40     { 0,      NULL }
41 };
42
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" },
48     { 0,            NULL }
49 };
50
51 static const value_string modifier_short_vals_cmd[] = {
52     { XDLC_UI,    "UI" },
53     { XDLC_UP,    "UP" },
54     { XDLC_DISC,  "DISC" },
55     { XDLC_UA,    "UA" },
56     { XDLC_SNRM,  "SNRM" },
57     { XDLC_SNRME, "SNRME" },
58     { XDLC_TEST,  "TEST" },
59     { XDLC_SIM,   "SIM" },
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" },
67     { XDLC_XID,   "XID" },
68     { XDLC_SNRME, "SNRME" },
69     { XDLC_BCN,   "BCN" },
70     { 0,          NULL }
71 };
72
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" },
91     { 0,             NULL }
92 };
93
94 static const value_string modifier_short_vals_resp[] = {
95     { XDLC_UI,    "UI" },
96     { XDLC_UP,    "UP" },
97     { XDLC_RD,    "RD" },
98     { XDLC_UA,    "UA" },
99     { XDLC_SNRM,  "SNRM" },
100     { XDLC_TEST,  "TEST" },
101     { XDLC_RIM,   "RIM" },
102     { XDLC_FRMR,  "FRMR" },
103     { XDLC_CFGR,  "CFGR" },
104     { XDLC_DM,    "DM" },
105     { XDLC_SABM,  "SABM" },
106     { XDLC_SARME, "SARME" },
107     { XDLC_SABME, "SABME" },
108     { XDLC_RESET, "RESET" },
109     { XDLC_XID,   "XID" },
110     { XDLC_SNRME, "SNRME" },
111     { XDLC_BCN,   "BCN" },
112     { 0,          NULL }
113 };
114
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" },
133     { 0,             NULL }
134 };
135
136 int
137 get_xdlc_control(const guchar *pd, int offset, gboolean is_extended)
138 {
139     guint16 control;
140
141     switch (pd[offset] & 0x03) {
142
143     case XDLC_S:
144     default:
145         /*
146          * Supervisory or Information frame.
147          */
148         if (is_extended)
149                 control = pletohs(&pd[offset]);
150         else
151                 control = pd[offset];
152         break;
153
154     case XDLC_U:
155         /*
156          * Unnumbered frame.
157          *
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.
164          */
165         control = pd[offset];
166         break;
167     }
168     return control;
169 }
170
171 /*
172  * Check whether the control field of the packet looks valid.
173  */
174 gboolean
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_)
179 {
180     guint16 control;
181
182     if (!tvb_bytes_exist(tvb, offset, 1))
183         return FALSE;   /* not enough data to check */
184     switch (tvb_get_guint8(tvb, offset) & 0x03) {
185
186     case XDLC_S:
187         /*
188          * Supervisory frame.
189          * No fields to check for validity here.
190          */
191         return TRUE;
192
193     case XDLC_U:
194         /*
195          * Unnumbered frame.
196          *
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.
203          */
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);
209         if (is_response) {
210                 if (try_val_to_str(control & XDLC_U_MODIFIER_MASK,
211                     u_modifier_short_vals_resp) == NULL)
212                         return FALSE;   /* unknown modifier */
213         } else {
214                 if (try_val_to_str(control & XDLC_U_MODIFIER_MASK,
215                     u_modifier_short_vals_cmd) == NULL)
216                         return FALSE;   /* unknown modifier */
217         }
218         return TRUE;
219
220     default:
221         /*
222          * Information frame.
223          * No fields to check for validity here.
224          */
225         return TRUE;
226     }
227 }
228
229 int
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)
236 {
237     guint16 control;
238     int control_len;
239     const xdlc_cf_items *cf_items;
240     const char *control_format;
241     guint16 poll_final;
242     char *info;
243     proto_tree *tc, *control_tree;
244     const gchar *frame_type = NULL;
245     const gchar *modifier;
246
247     info=(char *)ep_alloc(80);
248     switch (tvb_get_guint8(tvb, offset) & 0x03) {
249
250     case XDLC_S:
251         /*
252          * Supervisory frame.
253          */
254         if (is_extended) {
255             control = tvb_get_letohs(tvb, offset);
256             control_len = 2;
257             cf_items = cf_items_ext;
258             control_format = "Control field: %s (0x%04X)";
259         } else {
260             control = tvb_get_guint8(tvb, offset);
261             control_len = 1;
262             cf_items = cf_items_nonext;
263             control_format = "Control field: %s (0x%02X)";
264         }
265         switch (control & XDLC_S_FTYPE_MASK) {
266         case XDLC_RR:
267             frame_type = "RR";
268             break;
269
270         case XDLC_RNR:
271             frame_type = "RNR";
272             break;
273
274         case XDLC_REJ:
275             frame_type = "REJ";
276             break;
277
278         case XDLC_SREJ:
279             frame_type = "SREJ";
280             break;
281         }
282         if (is_extended) {
283             poll_final = (control & XDLC_P_F_EXT);
284             g_snprintf(info, 80, "S%s, func=%s, N(R)=%u",
285                         (poll_final ?
286                             (is_response ? " F" : " P") :
287                             ""),
288                         frame_type,
289                         (control & XDLC_N_R_EXT_MASK) >> XDLC_N_R_EXT_SHIFT);
290         } else {
291             poll_final = (control & XDLC_P_F);
292             g_snprintf(info, 80, "S%s, func=%s, N(R)=%u",
293                         (poll_final ?
294                             (is_response ? " F" : " P") :
295                             ""),
296                         frame_type,
297                         (control & XDLC_N_R_MASK) >> XDLC_N_R_SHIFT);
298         }
299         if (append_info) {
300             col_append_str(pinfo->cinfo, COL_INFO, ", ");
301             col_append_str(pinfo->cinfo, COL_INFO, info);
302         } else {
303             col_add_str(pinfo->cinfo, COL_INFO, info);
304         }
305         if (xdlc_tree) {
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);
311             if (poll_final) {
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);
316             }
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);
322         }
323         break;
324
325     case XDLC_U:
326         /*
327          * Unnumbered frame.
328          *
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.
335          */
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);
341         control_len = 1;
342         cf_items = cf_items_nonext;
343         control_format = "Control field: %s (0x%02X)";
344         if (is_response) {
345                 modifier = val_to_str(control & XDLC_U_MODIFIER_MASK,
346                         u_modifier_short_vals_resp, "Unknown");
347         } else {
348                 modifier = val_to_str(control & XDLC_U_MODIFIER_MASK,
349                         u_modifier_short_vals_cmd, "Unknown");
350         }
351         poll_final = (control & XDLC_P_F);
352         g_snprintf(info, 80, "U%s, func=%s",
353                 (poll_final ?
354                     (is_response ? " F" : " P") :
355                     ""),
356                 modifier);
357         if (append_info) {
358             col_append_str(pinfo->cinfo, COL_INFO, ", ");
359         col_append_str(pinfo->cinfo, COL_INFO, info);
360         } else {
361             col_add_str(pinfo->cinfo, COL_INFO, info);
362         }
363         if (xdlc_tree) {
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);
367             if (poll_final) {
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);
372             }
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);
380         }
381         break;
382
383     default:
384         /*
385          * Information frame.
386          */
387         if (is_extended) {
388             control = tvb_get_letohs(tvb, offset);
389             control_len = 2;
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);
397         } else {
398             control = tvb_get_guint8(tvb, offset);
399             control_len = 1;
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);
407         }
408         if (append_info) {
409             col_append_str(pinfo->cinfo, COL_INFO, ", ");
410         col_append_str(pinfo->cinfo, COL_INFO, info);
411         } else {
412             col_add_str(pinfo->cinfo, COL_INFO, info);
413         }
414         if (xdlc_tree) {
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);
422             if (poll_final) {
423                 proto_tree_add_boolean(control_tree, *cf_items->hf_xdlc_p,
424                         tvb, offset, control_len, control);
425             }
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);
429         }
430         break;
431     }
432     return control;
433 }