wslua: fix nstime memory leak after passing unknown encoding to TvbRange_nstime()
[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  * Wireshark - Network traffic analyzer
6  * By Gerald Combs <gerald@wireshark.org>
7  * Copyright 1998 Gerald Combs
8  *
9  * This program is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU General Public License
11  * as published by the Free Software Foundation; either version 2
12  * of the License, or (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22  */
23
24 #include "config.h"
25
26 #include <stdio.h>
27 #include <string.h>
28
29 #include <glib.h>
30 #include <epan/packet.h>
31 #include <epan/xdlc.h>
32 #include <wsutil/pint.h>
33
34 const value_string ftype_vals[] = {
35     { XDLC_I, "Information frame" },
36     { XDLC_S, "Supervisory frame" },
37     { XDLC_U, "Unnumbered frame" },
38     { 0,      NULL }
39 };
40
41 const value_string stype_vals[] = {
42     { XDLC_RR>>2,   "Receiver ready" },
43     { XDLC_RNR>>2,  "Receiver not ready" },
44     { XDLC_REJ>>2,  "Reject" },
45     { XDLC_SREJ>>2, "Selective reject" },
46     { 0,            NULL }
47 };
48
49 static const value_string modifier_short_vals_cmd[] = {
50     { XDLC_UI,    "UI" },
51     { XDLC_UP,    "UP" },
52     { XDLC_DISC,  "DISC" },
53     { XDLC_UA,    "UA" },
54     { XDLC_SNRM,  "SNRM" },
55     { XDLC_SNRME, "SNRME" },
56     { XDLC_TEST,  "TEST" },
57     { XDLC_SIM,   "SIM" },
58     { XDLC_FRMR,  "FRMR" },
59     { XDLC_CFGR,  "CFGR" },
60     { XDLC_SARM,  "SARM" },
61     { XDLC_SABM,  "SABM" },
62     { XDLC_SARME, "SARME" },
63     { XDLC_SABME, "SABME" },
64     { XDLC_RESET, "RESET" },
65     { XDLC_XID,   "XID" },
66     { XDLC_SNRME, "SNRME" },
67     { XDLC_BCN,   "BCN" },
68     { 0,          NULL }
69 };
70
71 const value_string modifier_vals_cmd[] = {
72     { XDLC_UI>>2,    "Unnumbered Information" },
73     { XDLC_UP>>2,    "Unnumbered Poll" },
74     { XDLC_DISC>>2,  "Disconnect" },
75     { XDLC_UA>>2,    "Unnumbered Acknowledge" },
76     { XDLC_SNRM>>2,  "Set Normal Response Mode" },
77     { XDLC_TEST>>2,  "Test" },
78     { XDLC_SIM>>2,   "Set Initialization Mode" },
79     { XDLC_FRMR>>2,  "Frame reject" },
80     { XDLC_CFGR>>2,  "Configure" },
81     { XDLC_SARM>>2,  "Set Asynchronous Response Mode" },
82     { XDLC_SABM>>2,  "Set Asynchronous Balanced Mode" },
83     { XDLC_SARME>>2, "Set Asynchronous Response Mode Extended" },
84     { XDLC_SABME>>2, "Set Asynchronous Balanced Mode Extended" },
85     { XDLC_RESET>>2, "Reset" },
86     { XDLC_XID>>2,   "Exchange identification" },
87     { XDLC_SNRME>>2, "Set Normal Response Mode Extended" },
88     { XDLC_BCN>>2,   "Beacon" },
89     { 0,             NULL }
90 };
91
92 static const value_string modifier_short_vals_resp[] = {
93     { XDLC_UI,    "UI" },
94     { XDLC_UP,    "UP" },
95     { XDLC_RD,    "RD" },
96     { XDLC_UA,    "UA" },
97     { XDLC_SNRM,  "SNRM" },
98     { XDLC_TEST,  "TEST" },
99     { XDLC_RIM,   "RIM" },
100     { XDLC_FRMR,  "FRMR" },
101     { XDLC_CFGR,  "CFGR" },
102     { XDLC_DM,    "DM" },
103     { XDLC_SABM,  "SABM" },
104     { XDLC_SARME, "SARME" },
105     { XDLC_SABME, "SABME" },
106     { XDLC_RESET, "RESET" },
107     { XDLC_XID,   "XID" },
108     { XDLC_SNRME, "SNRME" },
109     { XDLC_BCN,   "BCN" },
110     { 0,          NULL }
111 };
112
113 const value_string modifier_vals_resp[] = {
114     { XDLC_UI>>2,    "Unnumbered Information" },
115     { XDLC_UP>>2,    "Unnumbered Poll" },
116     { XDLC_RD>>2,    "Request Disconnect" },
117     { XDLC_UA>>2,    "Unnumbered Acknowledge" },
118     { XDLC_SNRM>>2,  "Set Normal Response Mode" },
119     { XDLC_TEST>>2,  "Test" },
120     { XDLC_RIM>>2,   "Request Initialization Mode" },
121     { XDLC_FRMR>>2,  "Frame reject" },
122     { XDLC_CFGR>>2,  "Configure" },
123     { XDLC_DM>>2,    "Disconnected mode" },
124     { XDLC_SABM>>2,  "Set Asynchronous Balanced Mode" },
125     { XDLC_SARME>>2, "Set Asynchronous Response Mode Extended" },
126     { XDLC_SABME>>2, "Set Asynchronous Balanced Mode Extended" },
127     { XDLC_RESET>>2, "Reset" },
128     { XDLC_XID>>2,   "Exchange identification" },
129     { XDLC_SNRME>>2, "Set Normal Response Mode Extended" },
130     { XDLC_BCN>>2,   "Beacon" },
131     { 0,             NULL }
132 };
133
134 int
135 get_xdlc_control(const guchar *pd, int offset, gboolean is_extended)
136 {
137     guint16 control;
138
139     switch (pd[offset] & 0x03) {
140
141     case XDLC_S:
142     default:
143         /*
144          * Supervisory or Information frame.
145          */
146         if (is_extended)
147                 control = pletoh16(&pd[offset]);
148         else
149                 control = pd[offset];
150         break;
151
152     case XDLC_U:
153         /*
154          * Unnumbered frame.
155          *
156          * XXX - is this two octets, with a P/F bit, in HDLC extended
157          * operation?  It's one octet in LLC, even though the control
158          * field of I and S frames is a 2-byte extended-operation field
159          * in LLC.  Given that there are no sequence numbers in the
160          * control field of a U frame, there doesn't appear to be any
161          * need for it to be 2 bytes in extended operation.
162          */
163         control = pd[offset];
164         break;
165     }
166     return control;
167 }
168
169 int
170 dissect_xdlc_control(tvbuff_t *tvb, int offset, packet_info *pinfo,
171   proto_tree *xdlc_tree, int hf_xdlc_control, gint ett_xdlc_control,
172   const xdlc_cf_items *cf_items_nonext, const xdlc_cf_items *cf_items_ext,
173   const value_string *u_modifier_short_vals_cmd,
174   const value_string *u_modifier_short_vals_resp, gboolean is_response,
175   gboolean is_extended, gboolean append_info)
176 {
177     guint16 control;
178     int control_len;
179     const xdlc_cf_items *cf_items;
180     const char *control_format;
181     guint16 poll_final;
182     char *info;
183     proto_tree *tc, *control_tree;
184     const gchar *frame_type = NULL;
185     const gchar *modifier;
186
187     info=(char *)wmem_alloc(wmem_packet_scope(), 80);
188     switch (tvb_get_guint8(tvb, offset) & 0x03) {
189
190     case XDLC_S:
191         /*
192          * Supervisory frame.
193          */
194         if (is_extended) {
195             control = tvb_get_letohs(tvb, offset);
196             control_len = 2;
197             cf_items = cf_items_ext;
198             control_format = "Control field: %s (0x%04X)";
199         } else {
200             control = tvb_get_guint8(tvb, offset);
201             control_len = 1;
202             cf_items = cf_items_nonext;
203             control_format = "Control field: %s (0x%02X)";
204         }
205         switch (control & XDLC_S_FTYPE_MASK) {
206         case XDLC_RR:
207             frame_type = "RR";
208             break;
209
210         case XDLC_RNR:
211             frame_type = "RNR";
212             break;
213
214         case XDLC_REJ:
215             frame_type = "REJ";
216             break;
217
218         case XDLC_SREJ:
219             frame_type = "SREJ";
220             break;
221         }
222         if (is_extended) {
223             poll_final = (control & XDLC_P_F_EXT);
224             g_snprintf(info, 80, "S%s, func=%s, N(R)=%u",
225                         (poll_final ?
226                             (is_response ? " F" : " P") :
227                             ""),
228                         frame_type,
229                         (control & XDLC_N_R_EXT_MASK) >> XDLC_N_R_EXT_SHIFT);
230         } else {
231             poll_final = (control & XDLC_P_F);
232             g_snprintf(info, 80, "S%s, func=%s, N(R)=%u",
233                         (poll_final ?
234                             (is_response ? " F" : " P") :
235                             ""),
236                         frame_type,
237                         (control & XDLC_N_R_MASK) >> XDLC_N_R_SHIFT);
238         }
239         if (append_info) {
240             col_append_str(pinfo->cinfo, COL_INFO, ", ");
241             col_append_str(pinfo->cinfo, COL_INFO, info);
242         } else {
243             col_add_str(pinfo->cinfo, COL_INFO, info);
244         }
245         if (xdlc_tree) {
246             tc = proto_tree_add_uint_format(xdlc_tree, hf_xdlc_control, tvb,
247                 offset, control_len, control, control_format, info, control);
248             control_tree = proto_item_add_subtree(tc, ett_xdlc_control);
249             proto_tree_add_uint(control_tree, *cf_items->hf_xdlc_n_r,
250                 tvb, offset, control_len, control);
251             if (poll_final) {
252                 proto_tree_add_boolean(control_tree,
253                         (is_response ? *cf_items->hf_xdlc_f :
254                                        *cf_items->hf_xdlc_p),
255                         tvb, offset, control_len, control);
256             }
257             proto_tree_add_uint(control_tree, *cf_items->hf_xdlc_s_ftype,
258                 tvb, offset, control_len, control);
259             /* This will always say it's a supervisory frame */
260             proto_tree_add_uint(control_tree, *cf_items->hf_xdlc_ftype_s_u,
261                 tvb, offset, control_len, control);
262         }
263         break;
264
265     case XDLC_U:
266         /*
267          * Unnumbered frame.
268          *
269          * XXX - is this two octets, with a P/F bit, in HDLC extended
270          * operation?  It's one octet in LLC, even though the control
271          * field of I and S frames is a 2-byte extended-operation field
272          * in LLC.  Given that there are no sequence numbers in the
273          * control field of a U frame, there doesn't appear to be any
274          * need for it to be 2 bytes in extended operation.
275          */
276         if (u_modifier_short_vals_cmd == NULL)
277                 u_modifier_short_vals_cmd = modifier_short_vals_cmd;
278         if (u_modifier_short_vals_resp == NULL)
279                 u_modifier_short_vals_resp = modifier_short_vals_resp;
280         control = tvb_get_guint8(tvb, offset);
281         control_len = 1;
282         cf_items = cf_items_nonext;
283         control_format = "Control field: %s (0x%02X)";
284         if (is_response) {
285                 modifier = val_to_str(control & XDLC_U_MODIFIER_MASK,
286                         u_modifier_short_vals_resp, "Unknown");
287         } else {
288                 modifier = val_to_str(control & XDLC_U_MODIFIER_MASK,
289                         u_modifier_short_vals_cmd, "Unknown");
290         }
291         poll_final = (control & XDLC_P_F);
292         g_snprintf(info, 80, "U%s, func=%s",
293                 (poll_final ?
294                     (is_response ? " F" : " P") :
295                     ""),
296                 modifier);
297         if (append_info) {
298             col_append_str(pinfo->cinfo, COL_INFO, ", ");
299         col_append_str(pinfo->cinfo, COL_INFO, info);
300         } else {
301             col_add_str(pinfo->cinfo, COL_INFO, info);
302         }
303         if (xdlc_tree) {
304             tc = proto_tree_add_uint_format(xdlc_tree, hf_xdlc_control, tvb,
305                 offset, control_len, control, control_format, info, control);
306             control_tree = proto_item_add_subtree(tc, ett_xdlc_control);
307             if (poll_final) {
308                 proto_tree_add_boolean(control_tree,
309                         (is_response ? *cf_items->hf_xdlc_f:
310                                        *cf_items->hf_xdlc_p),
311                         tvb, offset, control_len, control);
312             }
313             proto_tree_add_uint(control_tree,
314                 (is_response ? *cf_items->hf_xdlc_u_modifier_resp :
315                                *cf_items->hf_xdlc_u_modifier_cmd),
316                 tvb, offset, control_len, control);
317             /* This will always say it's an unnumbered frame */
318             proto_tree_add_uint(control_tree, *cf_items->hf_xdlc_ftype_s_u,
319                 tvb, offset, control_len, control);
320         }
321         break;
322
323     default:
324         /*
325          * Information frame.
326          */
327         if (is_extended) {
328             control = tvb_get_letohs(tvb, offset);
329             control_len = 2;
330             cf_items = cf_items_ext;
331             control_format = "Control field: %s (0x%04X)";
332             poll_final = (control & XDLC_P_F_EXT);
333             g_snprintf(info, 80, "I%s, N(R)=%u, N(S)=%u",
334                         ((control & XDLC_P_F_EXT) ? " P" : ""),
335                         (control & XDLC_N_R_EXT_MASK) >> XDLC_N_R_EXT_SHIFT,
336                         (control & XDLC_N_S_EXT_MASK) >> XDLC_N_S_EXT_SHIFT);
337         } else {
338             control = tvb_get_guint8(tvb, offset);
339             control_len = 1;
340             cf_items = cf_items_nonext;
341             control_format = "Control field: %s (0x%02X)";
342             poll_final = (control & XDLC_P_F);
343             g_snprintf(info, 80, "I%s, N(R)=%u, N(S)=%u",
344                         ((control & XDLC_P_F) ? " P" : ""),
345                         (control & XDLC_N_R_MASK) >> XDLC_N_R_SHIFT,
346                         (control & XDLC_N_S_MASK) >> XDLC_N_S_SHIFT);
347         }
348         if (append_info) {
349             col_append_str(pinfo->cinfo, COL_INFO, ", ");
350         col_append_str(pinfo->cinfo, COL_INFO, info);
351         } else {
352             col_add_str(pinfo->cinfo, COL_INFO, info);
353         }
354         if (xdlc_tree) {
355             tc = proto_tree_add_uint_format(xdlc_tree, hf_xdlc_control, tvb,
356                 offset, control_len, control, control_format, info, control);
357             control_tree = proto_item_add_subtree(tc, ett_xdlc_control);
358             proto_tree_add_uint(control_tree, *cf_items->hf_xdlc_n_r,
359                 tvb, offset, control_len, control);
360             proto_tree_add_uint(control_tree, *cf_items->hf_xdlc_n_s,
361                 tvb, offset, control_len, control);
362             if (poll_final) {
363                 proto_tree_add_boolean(control_tree, *cf_items->hf_xdlc_p,
364                         tvb, offset, control_len, control);
365             }
366             /* This will always say it's an information frame */
367             proto_tree_add_uint(control_tree, *cf_items->hf_xdlc_ftype_i,
368                 tvb, offset, control_len, control);
369         }
370         break;
371     }
372     return control;
373 }
374
375 /*
376  * Editor modelines  -  http://www.wireshark.org/tools/modelines.html
377  *
378  * Local variables:
379  * c-basic-offset: 4
380  * tab-width: 8
381  * indent-tabs-mode: nil
382  * End:
383  *
384  * vi: set shiftwidth=4 tabstop=8 expandtab:
385  * :indentSize=4:tabSize=8:noTabs=true:
386  */