aboutsummaryrefslogtreecommitdiffstats
path: root/epan/xdlc.c
blob: d6c5453d1437b1daa4c0cbe1c32125c1918f366b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
/* xdlc.c
 * Routines for use by various SDLC-derived protocols, such as HDLC
 * and its derivatives LAPB, IEEE 802.2 LLC, etc..
 *
 * Wireshark - Network traffic analyzer
 * By Gerald Combs <gerald@wireshark.org>
 * Copyright 1998 Gerald Combs
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

#include "config.h"

#include <stdio.h>
#include <string.h>

#include <glib.h>
#include <epan/packet.h>
#include <epan/xdlc.h>
#include <wsutil/pint.h>

const value_string ftype_vals[] = {
    { XDLC_I, "Information frame" },
    { XDLC_S, "Supervisory frame" },
    { XDLC_U, "Unnumbered frame" },
    { 0,      NULL }
};

const value_string stype_vals[] = {
    { XDLC_RR>>2,   "Receiver ready" },
    { XDLC_RNR>>2,  "Receiver not ready" },
    { XDLC_REJ>>2,  "Reject" },
    { XDLC_SREJ>>2, "Selective reject" },
    { 0,            NULL }
};

static const value_string modifier_short_vals_cmd[] = {
    { XDLC_UI,    "UI" },
    { XDLC_UP,    "UP" },
    { XDLC_DISC,  "DISC" },
    { XDLC_UA,    "UA" },
    { XDLC_SNRM,  "SNRM" },
    { XDLC_SNRME, "SNRME" },
    { XDLC_TEST,  "TEST" },
    { XDLC_SIM,   "SIM" },
    { XDLC_FRMR,  "FRMR" },
    { XDLC_CFGR,  "CFGR" },
    { XDLC_SARM,  "SARM" },
    { XDLC_SABM,  "SABM" },
    { XDLC_SARME, "SARME" },
    { XDLC_SABME, "SABME" },
    { XDLC_RESET, "RESET" },
    { XDLC_XID,   "XID" },
    { XDLC_SNRME, "SNRME" },
    { XDLC_BCN,   "BCN" },
    { 0,          NULL }
};

const value_string modifier_vals_cmd[] = {
    { XDLC_UI>>2,    "Unnumbered Information" },
    { XDLC_UP>>2,    "Unnumbered Poll" },
    { XDLC_DISC>>2,  "Disconnect" },
    { XDLC_UA>>2,    "Unnumbered Acknowledge" },
    { XDLC_SNRM>>2,  "Set Normal Response Mode" },
    { XDLC_TEST>>2,  "Test" },
    { XDLC_SIM>>2,   "Set Initialization Mode" },
    { XDLC_FRMR>>2,  "Frame reject" },
    { XDLC_CFGR>>2,  "Configure" },
    { XDLC_SARM>>2,  "Set Asynchronous Response Mode" },
    { XDLC_SABM>>2,  "Set Asynchronous Balanced Mode" },
    { XDLC_SARME>>2, "Set Asynchronous Response Mode Extended" },
    { XDLC_SABME>>2, "Set Asynchronous Balanced Mode Extended" },
    { XDLC_RESET>>2, "Reset" },
    { XDLC_XID>>2,   "Exchange identification" },
    { XDLC_SNRME>>2, "Set Normal Response Mode Extended" },
    { XDLC_BCN>>2,   "Beacon" },
    { 0,             NULL }
};

static const value_string modifier_short_vals_resp[] = {
    { XDLC_UI,    "UI" },
    { XDLC_UP,    "UP" },
    { XDLC_RD,    "RD" },
    { XDLC_UA,    "UA" },
    { XDLC_SNRM,  "SNRM" },
    { XDLC_TEST,  "TEST" },
    { XDLC_RIM,   "RIM" },
    { XDLC_FRMR,  "FRMR" },
    { XDLC_CFGR,  "CFGR" },
    { XDLC_DM,    "DM" },
    { XDLC_SABM,  "SABM" },
    { XDLC_SARME, "SARME" },
    { XDLC_SABME, "SABME" },
    { XDLC_RESET, "RESET" },
    { XDLC_XID,   "XID" },
    { XDLC_SNRME, "SNRME" },
    { XDLC_BCN,   "BCN" },
    { 0,          NULL }
};

const value_string modifier_vals_resp[] = {
    { XDLC_UI>>2,    "Unnumbered Information" },
    { XDLC_UP>>2,    "Unnumbered Poll" },
    { XDLC_RD>>2,    "Request Disconnect" },
    { XDLC_UA>>2,    "Unnumbered Acknowledge" },
    { XDLC_SNRM>>2,  "Set Normal Response Mode" },
    { XDLC_TEST>>2,  "Test" },
    { XDLC_RIM>>2,   "Request Initialization Mode" },
    { XDLC_FRMR>>2,  "Frame reject" },
    { XDLC_CFGR>>2,  "Configure" },
    { XDLC_DM>>2,    "Disconnected mode" },
    { XDLC_SABM>>2,  "Set Asynchronous Balanced Mode" },
    { XDLC_SARME>>2, "Set Asynchronous Response Mode Extended" },
    { XDLC_SABME>>2, "Set Asynchronous Balanced Mode Extended" },
    { XDLC_RESET>>2, "Reset" },
    { XDLC_XID>>2,   "Exchange identification" },
    { XDLC_SNRME>>2, "Set Normal Response Mode Extended" },
    { XDLC_BCN>>2,   "Beacon" },
    { 0,             NULL }
};

int
get_xdlc_control(const guchar *pd, int offset, gboolean is_extended)
{
    guint16 control;

    switch (pd[offset] & 0x03) {

    case XDLC_S:
    default:
        /*
         * Supervisory or Information frame.
         */
        if (is_extended)
                control = pletoh16(&pd[offset]);
        else
                control = pd[offset];
        break;

    case XDLC_U:
        /*
         * Unnumbered frame.
         *
         * XXX - is this two octets, with a P/F bit, in HDLC extended
         * operation?  It's one octet in LLC, even though the control
         * field of I and S frames is a 2-byte extended-operation field
         * in LLC.  Given that there are no sequence numbers in the
         * control field of a U frame, there doesn't appear to be any
         * need for it to be 2 bytes in extended operation.
         */
        control = pd[offset];
        break;
    }
    return control;
}

/*
 * Check whether the control field of the packet looks valid.
 */
gboolean
check_xdlc_control(tvbuff_t *tvb, int offset,
  const value_string *u_modifier_short_vals_cmd,
  const value_string *u_modifier_short_vals_resp, gboolean is_response,
  gboolean is_extended _U_)
{
    guint16 control;

    if (!tvb_bytes_exist(tvb, offset, 1))
        return FALSE;   /* not enough data to check */
    switch (tvb_get_guint8(tvb, offset) & 0x03) {

    case XDLC_S:
        /*
         * Supervisory frame.
         * No fields to check for validity here.
         */
        return TRUE;

    case XDLC_U:
        /*
         * Unnumbered frame.
         *
         * XXX - is this two octets, with a P/F bit, in HDLC extended
         * operation?  It's one octet in LLC, even though the control
         * field of I and S frames is a 2-byte extended-operation field
         * in LLC.  Given that there are no sequence numbers in the
         * control field of a U frame, there doesn't appear to be any
         * need for it to be 2 bytes in extended operation.
         */
        if (u_modifier_short_vals_cmd == NULL)
                u_modifier_short_vals_cmd = modifier_short_vals_cmd;
        if (u_modifier_short_vals_resp == NULL)
                u_modifier_short_vals_resp = modifier_short_vals_resp;
        control = tvb_get_guint8(tvb, offset);
        if (is_response) {
                if (try_val_to_str(control & XDLC_U_MODIFIER_MASK,
                    u_modifier_short_vals_resp) == NULL)
                        return FALSE;   /* unknown modifier */
        } else {
                if (try_val_to_str(control & XDLC_U_MODIFIER_MASK,
                    u_modifier_short_vals_cmd) == NULL)
                        return FALSE;   /* unknown modifier */
        }
        return TRUE;

    default:
        /*
         * Information frame.
         * No fields to check for validity here.
         */
        return TRUE;
    }
}

int
dissect_xdlc_control(tvbuff_t *tvb, int offset, packet_info *pinfo,
  proto_tree *xdlc_tree, int hf_xdlc_control, gint ett_xdlc_control,
  const xdlc_cf_items *cf_items_nonext, const xdlc_cf_items *cf_items_ext,
  const value_string *u_modifier_short_vals_cmd,
  const value_string *u_modifier_short_vals_resp, gboolean is_response,
  gboolean is_extended, gboolean append_info)
{
    guint16 control;
    int control_len;
    const xdlc_cf_items *cf_items;
    const char *control_format;
    guint16 poll_final;
    char *info;
    proto_tree *tc, *control_tree;
    const gchar *frame_type = NULL;
    const gchar *modifier;

    info=(char *)wmem_alloc(wmem_packet_scope(), 80);
    switch (tvb_get_guint8(tvb, offset) & 0x03) {

    case XDLC_S:
        /*
         * Supervisory frame.
         */
        if (is_extended) {
            control = tvb_get_letohs(tvb, offset);
            control_len = 2;
            cf_items = cf_items_ext;
            control_format = "Control field: %s (0x%04X)";
        } else {
            control = tvb_get_guint8(tvb, offset);
            control_len = 1;
            cf_items = cf_items_nonext;
            control_format = "Control field: %s (0x%02X)";
        }
        switch (control & XDLC_S_FTYPE_MASK) {
        case XDLC_RR:
            frame_type = "RR";
            break;

        case XDLC_RNR:
            frame_type = "RNR";
            break;

        case XDLC_REJ:
            frame_type = "REJ";
            break;

        case XDLC_SREJ:
            frame_type = "SREJ";
            break;
        }
        if (is_extended) {
            poll_final = (control & XDLC_P_F_EXT);
            g_snprintf(info, 80, "S%s, func=%s, N(R)=%u",
                        (poll_final ?
                            (is_response ? " F" : " P") :
                            ""),
                        frame_type,
                        (control & XDLC_N_R_EXT_MASK) >> XDLC_N_R_EXT_SHIFT);
        } else {
            poll_final = (control & XDLC_P_F);
            g_snprintf(info, 80, "S%s, func=%s, N(R)=%u",
                        (poll_final ?
                            (is_response ? " F" : " P") :
                            ""),
                        frame_type,
                        (control & XDLC_N_R_MASK) >> XDLC_N_R_SHIFT);
        }
        if (append_info) {
            col_append_str(pinfo->cinfo, COL_INFO, ", ");
            col_append_str(pinfo->cinfo, COL_INFO, info);
        } else {
            col_add_str(pinfo->cinfo, COL_INFO, info);
        }
        if (xdlc_tree) {
            tc = proto_tree_add_uint_format(xdlc_tree, hf_xdlc_control, tvb,
                offset, control_len, control, control_format, info, control);
            control_tree = proto_item_add_subtree(tc, ett_xdlc_control);
            proto_tree_add_uint(control_tree, *cf_items->hf_xdlc_n_r,
                tvb, offset, control_len, control);
            if (poll_final) {
                proto_tree_add_boolean(control_tree,
                        (is_response ? *cf_items->hf_xdlc_f :
                                       *cf_items->hf_xdlc_p),
                        tvb, offset, control_len, control);
            }
            proto_tree_add_uint(control_tree, *cf_items->hf_xdlc_s_ftype,
                tvb, offset, control_len, control);
            /* This will always say it's a supervisory frame */
            proto_tree_add_uint(control_tree, *cf_items->hf_xdlc_ftype_s_u,
                tvb, offset, control_len, control);
        }
        break;

    case XDLC_U:
        /*
         * Unnumbered frame.
         *
         * XXX - is this two octets, with a P/F bit, in HDLC extended
         * operation?  It's one octet in LLC, even though the control
         * field of I and S frames is a 2-byte extended-operation field
         * in LLC.  Given that there are no sequence numbers in the
         * control field of a U frame, there doesn't appear to be any
         * need for it to be 2 bytes in extended operation.
         */
        if (u_modifier_short_vals_cmd == NULL)
                u_modifier_short_vals_cmd = modifier_short_vals_cmd;
        if (u_modifier_short_vals_resp == NULL)
                u_modifier_short_vals_resp = modifier_short_vals_resp;
        control = tvb_get_guint8(tvb, offset);
        control_len = 1;
        cf_items = cf_items_nonext;
        control_format = "Control field: %s (0x%02X)";
        if (is_response) {
                modifier = val_to_str(control & XDLC_U_MODIFIER_MASK,
                        u_modifier_short_vals_resp, "Unknown");
        } else {
                modifier = val_to_str(control & XDLC_U_MODIFIER_MASK,
                        u_modifier_short_vals_cmd, "Unknown");
        }
        poll_final = (control & XDLC_P_F);
        g_snprintf(info, 80, "U%s, func=%s",
                (poll_final ?
                    (is_response ? " F" : " P") :
                    ""),
                modifier);
        if (append_info) {
            col_append_str(pinfo->cinfo, COL_INFO, ", ");
        col_append_str(pinfo->cinfo, COL_INFO, info);
        } else {
            col_add_str(pinfo->cinfo, COL_INFO, info);
        }
        if (xdlc_tree) {
            tc = proto_tree_add_uint_format(xdlc_tree, hf_xdlc_control, tvb,
                offset, control_len, control, control_format, info, control);
            control_tree = proto_item_add_subtree(tc, ett_xdlc_control);
            if (poll_final) {
                proto_tree_add_boolean(control_tree,
                        (is_response ? *cf_items->hf_xdlc_f:
                                       *cf_items->hf_xdlc_p),
                        tvb, offset, control_len, control);
            }
            proto_tree_add_uint(control_tree,
                (is_response ? *cf_items->hf_xdlc_u_modifier_resp :
                               *cf_items->hf_xdlc_u_modifier_cmd),
                tvb, offset, control_len, control);
            /* This will always say it's an unnumbered frame */
            proto_tree_add_uint(control_tree, *cf_items->hf_xdlc_ftype_s_u,
                tvb, offset, control_len, control);
        }
        break;

    default:
        /*
         * Information frame.
         */
        if (is_extended) {
            control = tvb_get_letohs(tvb, offset);
            control_len = 2;
            cf_items = cf_items_ext;
            control_format = "Control field: %s (0x%04X)";
            poll_final = (control & XDLC_P_F_EXT);
            g_snprintf(info, 80, "I%s, N(R)=%u, N(S)=%u",
                        ((control & XDLC_P_F_EXT) ? " P" : ""),
                        (control & XDLC_N_R_EXT_MASK) >> XDLC_N_R_EXT_SHIFT,
                        (control & XDLC_N_S_EXT_MASK) >> XDLC_N_S_EXT_SHIFT);
        } else {
            control = tvb_get_guint8(tvb, offset);
            control_len = 1;
            cf_items = cf_items_nonext;
            control_format = "Control field: %s (0x%02X)";
            poll_final = (control & XDLC_P_F);
            g_snprintf(info, 80, "I%s, N(R)=%u, N(S)=%u",
                        ((control & XDLC_P_F) ? " P" : ""),
                        (control & XDLC_N_R_MASK) >> XDLC_N_R_SHIFT,
                        (control & XDLC_N_S_MASK) >> XDLC_N_S_SHIFT);
        }
        if (append_info) {
            col_append_str(pinfo->cinfo, COL_INFO, ", ");
        col_append_str(pinfo->cinfo, COL_INFO, info);
        } else {
            col_add_str(pinfo->cinfo, COL_INFO, info);
        }
        if (xdlc_tree) {
            tc = proto_tree_add_uint_format(xdlc_tree, hf_xdlc_control, tvb,
                offset, control_len, control, control_format, info, control);
            control_tree = proto_item_add_subtree(tc, ett_xdlc_control);
            proto_tree_add_uint(control_tree, *cf_items->hf_xdlc_n_r,
                tvb, offset, control_len, control);
            proto_tree_add_uint(control_tree, *cf_items->hf_xdlc_n_s,
                tvb, offset, control_len, control);
            if (poll_final) {
                proto_tree_add_boolean(control_tree, *cf_items->hf_xdlc_p,
                        tvb, offset, control_len, control);
            }
            /* This will always say it's an information frame */
            proto_tree_add_uint(control_tree, *cf_items->hf_xdlc_ftype_i,
                tvb, offset, control_len, control);
        }
        break;
    }
    return control;
}

/*
 * Editor modelines  -  http://www.wireshark.org/tools/modelines.html
 *
 * Local variables:
 * c-basic-offset: 4
 * tab-width: 8
 * indent-tabs-mode: nil
 * End:
 *
 * vi: set shiftwidth=4 tabstop=8 expandtab:
 * :indentSize=4:tabSize=8:noTabs=true:
 */