The function pointer in a "per_choice_t" or a "per_sequence_t" is to a
[obnox/wireshark/wip.git] / 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: xdlc.c,v 1.26 2004/03/04 07:51:47 guy Exp $
6  *
7  * Ethereal - Network traffic analyzer
8  * By Gerald Combs <gerald@ethereal.com>
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., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
24  */
25
26 #ifdef HAVE_CONFIG_H
27 # include "config.h"
28 #endif
29
30 #include <stdio.h>
31 #include <string.h>
32
33 #include <glib.h>
34 #include <epan/packet.h>
35 #include "xdlc.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, int 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 = pletohs(&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 int
173 dissect_xdlc_control(tvbuff_t *tvb, int offset, packet_info *pinfo,
174   proto_tree *xdlc_tree, int hf_xdlc_control, gint ett_xdlc_control,
175   const xdlc_cf_items *cf_items_nonext, const xdlc_cf_items *cf_items_ext,
176   const value_string *u_modifier_short_vals_cmd,
177   const value_string *u_modifier_short_vals_resp, int is_response,
178   int is_extended, int append_info)
179 {
180     guint16 control;
181     int control_len;
182     const xdlc_cf_items *cf_items;
183     char *control_format;
184     guint16 poll_final;
185     char info[80];
186     proto_tree *tc, *control_tree;
187     gchar *frame_type = NULL;
188     gchar *modifier;
189
190     switch (tvb_get_guint8(tvb, offset) & 0x03) {
191
192     case XDLC_S:
193         if (is_extended) {
194             control = tvb_get_letohs(tvb, offset);
195             control_len = 2;
196             cf_items = cf_items_ext;
197             control_format = "Control field: %s (0x%04X)";
198         } else {
199             control = tvb_get_guint8(tvb, offset);
200             control_len = 1;
201             cf_items = cf_items_nonext;
202             control_format = "Control field: %s (0x%02X)";
203         }
204         /*
205          * Supervisory frame.
206          */
207         switch (control & XDLC_S_FTYPE_MASK) {
208         case XDLC_RR:
209             frame_type = "RR";
210             break;
211
212         case XDLC_RNR:
213             frame_type = "RNR";
214             break;
215
216         case XDLC_REJ:
217             frame_type = "REJ";
218             break;
219
220         case XDLC_SREJ:
221             frame_type = "SREJ";
222             break;
223         }
224         if (is_extended) {
225             poll_final = (control & XDLC_P_F_EXT);
226             sprintf(info, "S%s, func=%s, N(R)=%u",
227                         (poll_final ?
228                             (is_response ? " F" : " P") :
229                             ""),
230                         frame_type,
231                         (control & XDLC_N_R_EXT_MASK) >> XDLC_N_R_EXT_SHIFT);
232         } else {
233             poll_final = (control & XDLC_P_F);
234             sprintf(info, "S%s, func=%s, N(R)=%u",
235                         (poll_final ?
236                             (is_response ? " F" : " P") :
237                             ""),
238                         frame_type,
239                         (control & XDLC_N_R_MASK) >> XDLC_N_R_SHIFT);
240         }
241         if (check_col(pinfo->cinfo, COL_INFO)) {
242             if (append_info) {
243                 col_append_str(pinfo->cinfo, COL_INFO, ", ");
244                 col_append_str(pinfo->cinfo, COL_INFO, info);
245             } else
246                 col_add_str(pinfo->cinfo, COL_INFO, info);
247         }
248         if (xdlc_tree) {
249             tc = proto_tree_add_uint_format(xdlc_tree, hf_xdlc_control, tvb,
250                 offset, control_len, control, control_format, info, control);
251             control_tree = proto_item_add_subtree(tc, ett_xdlc_control);
252             proto_tree_add_uint(control_tree, *cf_items->hf_xdlc_n_r,
253                 tvb, offset, control_len, control);
254             if (poll_final) {
255                 proto_tree_add_boolean(control_tree,
256                         (is_response ? *cf_items->hf_xdlc_f :
257                                        *cf_items->hf_xdlc_p),
258                         tvb, offset, control_len, control);
259             }
260             proto_tree_add_uint(control_tree, *cf_items->hf_xdlc_s_ftype,
261                 tvb, offset, control_len, control);
262             /* This will always say it's a supervisory frame */
263             proto_tree_add_uint(control_tree, *cf_items->hf_xdlc_ftype_s_u,
264                 tvb, offset, control_len, control);
265         }
266         break;
267
268     case XDLC_U:
269         /*
270          * Unnumbered frame.
271          *
272          * XXX - is this two octets, with a P/F bit, in HDLC extended
273          * operation?  It's one octet in LLC, even though the control
274          * field of I and S frames is a 2-byte extended-operation field
275          * in LLC.  Given that there are no sequence numbers in the
276          * control field of a U frame, there doesn't appear to be any
277          * need for it to be 2 bytes in extended operation.
278          */
279         if (u_modifier_short_vals_cmd == NULL)
280                 u_modifier_short_vals_cmd = modifier_short_vals_cmd;
281         if (u_modifier_short_vals_resp == NULL)
282                 u_modifier_short_vals_resp = modifier_short_vals_resp;
283         control = tvb_get_guint8(tvb, offset);
284         control_len = 1;
285         cf_items = cf_items_nonext;
286         control_format = "Control field: %s (0x%02X)";
287         if (is_response) {
288                 modifier = match_strval(control & XDLC_U_MODIFIER_MASK,
289                         u_modifier_short_vals_resp);
290         } else {
291                 modifier = match_strval(control & XDLC_U_MODIFIER_MASK,
292                         u_modifier_short_vals_cmd);
293         }
294         if (modifier == NULL)
295                 modifier = "Unknown";
296         poll_final = (control & XDLC_P_F);
297         sprintf(info, "U%s, func=%s",
298                 (poll_final ?
299                     (is_response ? " F" : " P") :
300                     ""),
301                 modifier);
302         if (check_col(pinfo->cinfo, COL_INFO)) {
303             if (append_info) {
304                 col_append_str(pinfo->cinfo, COL_INFO, ", ");
305                 col_append_str(pinfo->cinfo, COL_INFO, info);
306             } else
307                 col_add_str(pinfo->cinfo, COL_INFO, info);
308         }
309         if (xdlc_tree) {
310             tc = proto_tree_add_uint_format(xdlc_tree, hf_xdlc_control, tvb,
311                 offset, control_len, control, control_format, info, control);
312             control_tree = proto_item_add_subtree(tc, ett_xdlc_control);
313             if (poll_final) {
314                 proto_tree_add_boolean(control_tree,
315                         (is_response ? *cf_items->hf_xdlc_f:
316                                        *cf_items->hf_xdlc_p),
317                         tvb, offset, control_len, control);
318             }
319             proto_tree_add_uint(control_tree,
320                 (is_response ? *cf_items->hf_xdlc_u_modifier_resp :
321                                *cf_items->hf_xdlc_u_modifier_cmd),
322                 tvb, offset, control_len, control);
323             /* This will always say it's an unnumbered frame */
324             proto_tree_add_uint(control_tree, *cf_items->hf_xdlc_ftype_s_u,
325                 tvb, offset, control_len, control);
326         }
327         break;
328
329     default:
330         /*
331          * Information frame.
332          */
333         if (is_extended) {
334             control = tvb_get_letohs(tvb, offset);
335             control_len = 2;
336             cf_items = cf_items_ext;
337             control_format = "Control field: %s (0x%04X)";
338             poll_final = (control & XDLC_P_F_EXT);
339             sprintf(info, "I%s, N(R)=%u, N(S)=%u",
340                         ((control & XDLC_P_F_EXT) ? " P" : ""),
341                         (control & XDLC_N_R_EXT_MASK) >> XDLC_N_R_EXT_SHIFT,
342                         (control & XDLC_N_S_EXT_MASK) >> XDLC_N_S_EXT_SHIFT);
343         } else {
344             control = tvb_get_guint8(tvb, offset);
345             control_len = 1;
346             cf_items = cf_items_nonext;
347             control_format = "Control field: %s (0x%02X)";
348             poll_final = (control & XDLC_P_F);
349             sprintf(info, "I%s, N(R)=%u, N(S)=%u",
350                         ((control & XDLC_P_F) ? " P" : ""),
351                         (control & XDLC_N_R_MASK) >> XDLC_N_R_SHIFT,
352                         (control & XDLC_N_S_MASK) >> XDLC_N_S_SHIFT);
353         }
354         if (check_col(pinfo->cinfo, COL_INFO)) {
355             if (append_info) {
356                 col_append_str(pinfo->cinfo, COL_INFO, ", ");
357                 col_append_str(pinfo->cinfo, COL_INFO, info);
358             } else
359                 col_add_str(pinfo->cinfo, COL_INFO, info);
360         }
361         if (xdlc_tree) {
362             tc = proto_tree_add_uint_format(xdlc_tree, hf_xdlc_control, tvb,
363                 offset, control_len, control, control_format, info, control);
364             control_tree = proto_item_add_subtree(tc, ett_xdlc_control);
365             proto_tree_add_uint(control_tree, *cf_items->hf_xdlc_n_r,
366                 tvb, offset, control_len, control);
367             proto_tree_add_uint(control_tree, *cf_items->hf_xdlc_n_s,
368                 tvb, offset, control_len, control);
369             if (poll_final) {
370                 proto_tree_add_boolean(control_tree, *cf_items->hf_xdlc_p,
371                         tvb, offset, control_len, control);
372             }
373             /* This will always say it's an information frame */
374             proto_tree_add_uint(control_tree, *cf_items->hf_xdlc_ftype_i,
375                 tvb, offset, control_len, control);
376         }
377         break;
378     }
379     return control;
380 }