summaryrefslogtreecommitdiffstats
path: root/epan/dissectors/packet-rfid-pn532-hci.c
blob: 732667ba73e615243409a000a04df9749580b265 (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
/* packet-pn532_hci.c
 * Routines for NXP PN532 HCI Protocol
 *
 * http://www.nxp.com/documents/user_manual/141520.pdf
 *
 * Copyright 2013, Michal Labedzki for Tieto Corporation
 *
 * Wireshark - Network traffic analyzer
 * By Gerald Combs <gerald@wireshark.org>
 * Copyright 1998 Gerald Combs
 *
 * SPDX-License-Identifier: GPL-2.0-or-later
 */

#include "config.h"

#include <epan/packet.h>
#include <epan/prefs.h>
#include <epan/expert.h>
#include "packet-usb.h"

static int proto_pn532_hci                                                 = -1;
static int hf_preamble                                                     = -1;
static int hf_start_code                                                   = -1;
static int hf_length                                                       = -1;
static int hf_length_checksum                                              = -1;
static int hf_length_checksum_status                                       = -1;
static int hf_extended_length                                              = -1;
static int hf_packet_code                                                  = -1;
static int hf_postable                                                     = -1;
static int hf_specific_application_level_error_code                        = -1;
static int hf_data_checksum                                                = -1;
static int hf_data_checksum_status                                         = -1;
static int hf_ignored                                                      = -1;

static gint ett_pn532_hci                                                  = -1;

static expert_field ei_invalid_length_checksum                        = EI_INIT;
static expert_field ei_invalid_data_checksum                          = EI_INIT;

static dissector_handle_t  pn532_handle;
static dissector_handle_t  pn532_hci_handle;

static const value_string packet_code_vals[] = {
    { 0x00FF,  "ACK Frame" },
    { 0x01FF,  "Error Frame" },
    { 0xFF00,  "NACK Frame" },
    { 0xFFFF,  "Extended Information Frame" },
    { 0, NULL }
};

void proto_register_pn532_hci(void);
void proto_reg_handoff_pn532_hci(void);

static gint
dissect_pn532_hci(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, void *data)
{
    proto_item      *main_item;
    proto_tree      *main_tree;
    gint             offset = 0;
    tvbuff_t        *next_tvb;
    guint16          packet_code;
    guint16          length;
    guint8           checksum;
    usb_conv_info_t *usb_conv_info;

    /* Reject the packet if data is NULL */
    if (data == NULL)
        return 0;
    usb_conv_info = (usb_conv_info_t *)data;

    length = tvb_captured_length_remaining(tvb, offset);
    if (length < 6) return offset;

    col_set_str(pinfo->cinfo, COL_PROTOCOL, "PN532_HCI");
    col_clear(pinfo->cinfo, COL_INFO);

    main_item = proto_tree_add_item(tree, proto_pn532_hci, tvb, offset, -1, ENC_NA);
    main_tree = proto_item_add_subtree(main_item, ett_pn532_hci);

    length = 0;
    while (tvb_captured_length_remaining(tvb, length) >= 2 && tvb_get_ntohs(tvb, length) != 0x00FF) {
        length += 1;
    }
    if (length) {
        proto_tree_add_item(main_tree, hf_preamble, tvb, offset, length, ENC_NA);
        offset += length;
    }

    proto_tree_add_item(main_tree, hf_start_code, tvb, offset, 2, ENC_BIG_ENDIAN);
    offset += 2;

    packet_code = tvb_get_ntohs(tvb, offset);
    if (packet_code == 0x00FF) { /* ACK Frame */
        col_set_str(pinfo->cinfo, COL_INFO, val_to_str_const(packet_code, packet_code_vals, "Unknown frame"));

        proto_tree_add_item(main_tree, hf_packet_code, tvb, offset, 2, ENC_BIG_ENDIAN);
        offset += 2;
    } else if (packet_code == 0xFF00) { /* NACK Frame */
        col_set_str(pinfo->cinfo, COL_INFO, val_to_str_const(packet_code, packet_code_vals, "Unknown frame"));

        proto_tree_add_item(main_tree, hf_packet_code, tvb, offset, 2, ENC_BIG_ENDIAN);
        offset += 2;
    } else if (packet_code == 0x01FF) { /* Error Frame */
        col_set_str(pinfo->cinfo, COL_INFO, val_to_str_const(packet_code, packet_code_vals, "Unknown frame"));

        proto_tree_add_item(main_tree, hf_packet_code, tvb, offset, 2, ENC_BIG_ENDIAN);
        offset += 2;

        proto_tree_add_item(main_tree, hf_specific_application_level_error_code, tvb, offset, 1, ENC_BIG_ENDIAN);
        offset += 1;
    } else if (packet_code == 0xFFFF) { /* Extended Information Frame */
        col_set_str(pinfo->cinfo, COL_INFO, "Extended Information Frame");

        proto_tree_add_item(main_tree, hf_extended_length, tvb, offset, 2, ENC_BIG_ENDIAN);
        length = tvb_get_ntohs(tvb, offset);
        offset += 2;

        checksum = (length >> 8) + (length & 0xFF) + tvb_get_guint8(tvb, offset);
        proto_tree_add_checksum(main_tree, tvb, offset, hf_length_checksum, hf_length_checksum_status, &ei_invalid_length_checksum,
                            pinfo, checksum, ENC_BIG_ENDIAN, PROTO_CHECKSUM_VERIFY|PROTO_CHECKSUM_ZERO);
        offset += 1;

        next_tvb = tvb_new_subset_length(tvb, offset, length);
        call_dissector_with_data(pn532_handle, next_tvb, pinfo, tree, usb_conv_info);
        offset += length;

        checksum = tvb_get_guint8(tvb, offset);
        while (length) {
            checksum += tvb_get_guint8(tvb, offset - length);
            length -= 1;
        }
        proto_tree_add_checksum(main_tree, tvb, offset, hf_data_checksum, hf_data_checksum_status, &ei_invalid_data_checksum, pinfo, 0,
                            ENC_BIG_ENDIAN, PROTO_CHECKSUM_VERIFY|PROTO_CHECKSUM_ZERO);
        offset += 1;
    } else { /* Normal Information Frame */
        col_set_str(pinfo->cinfo, COL_INFO, "Normal Information Frame");

        proto_tree_add_item(main_tree, hf_length, tvb, offset, 1, ENC_BIG_ENDIAN);
        length = tvb_get_guint8(tvb, offset);
        offset += 1;

        checksum = length + tvb_get_guint8(tvb, offset);
        proto_tree_add_checksum(main_tree, tvb, offset, hf_length_checksum, hf_length_checksum_status, &ei_invalid_length_checksum,
                            pinfo, checksum, ENC_BIG_ENDIAN, PROTO_CHECKSUM_VERIFY|PROTO_CHECKSUM_ZERO);
        offset += 1;

        next_tvb = tvb_new_subset_length(tvb, offset, length);
        call_dissector_with_data(pn532_handle, next_tvb, pinfo, tree, usb_conv_info);
        offset += length;

        proto_tree_add_item(main_tree, hf_data_checksum, tvb, offset, 1, ENC_BIG_ENDIAN);
        checksum = tvb_get_guint8(tvb, offset);
        while (length) {
            checksum += tvb_get_guint8(tvb, offset - length);
            length -= 1;
        }
        if (checksum != 0) {
            proto_tree_add_expert(main_tree, pinfo, &ei_invalid_data_checksum, tvb, offset, 1);
        }
        offset += 1;
    }

    length = 0;
    if (tvb_captured_length_remaining(tvb, offset) == 1) {
        length = 1;
    } else while (tvb_captured_length_remaining(tvb, offset + length) >= 2 && tvb_get_ntohs(tvb, offset + length) != 0x00FF) {
        length += 1;
    }
    if (length) {
        proto_tree_add_item(main_tree, hf_postable, tvb, offset, length, ENC_NA);
        offset += length;
    }

    if (tvb_captured_length_remaining(tvb, offset)) {
        proto_tree_add_item(main_tree, hf_ignored, tvb, offset, tvb_captured_length_remaining(tvb, offset), ENC_NA);
        offset += tvb_captured_length_remaining(tvb, offset);
    }

    return offset;
}

void
proto_register_pn532_hci(void)
{
    module_t         *module;
    expert_module_t  *expert_module;

    static hf_register_info hf[] = {
        { &hf_preamble,
            { "Preamble",                        "pn532_hci.preamble",
            FT_BYTES, BASE_NONE, NULL, 0x00,
            NULL, HFILL }
        },
        { &hf_start_code,
            { "Start Code",                      "pn532_hci.start_code",
            FT_UINT16, BASE_HEX, NULL, 0x00,
            NULL, HFILL }
        },
        { &hf_packet_code,
            { "Packet Code",                     "pn532_hci.packet_code",
            FT_UINT16, BASE_HEX, VALS(packet_code_vals), 0x00,
            NULL, HFILL }
        },
        { &hf_length,
            { "Length",                          "pn532_hci.length",
            FT_UINT8, BASE_DEC, NULL, 0x00,
            NULL, HFILL }
        },
        { &hf_extended_length,
            { "Extended Length",                 "pn532_hci.extended_length",
            FT_UINT16, BASE_DEC, NULL, 0x00,
            NULL, HFILL }
        },
        { &hf_length_checksum,
            { "Length Checksum",                 "pn532_hci.length_checksum",
            FT_UINT8, BASE_HEX, NULL, 0x00,
            NULL, HFILL }
        },
        { &hf_length_checksum_status,
            { "Length Checksum Status",            "pn532_hci.length_checksum.status",
            FT_UINT8, BASE_NONE, VALS(proto_checksum_vals), 0x00,
            NULL, HFILL }
        },
        { &hf_data_checksum,
            { "Data Checksum",                   "pn532_hci.data_checksum",
            FT_UINT8, BASE_HEX, NULL, 0x00,
            NULL, HFILL }
        },
        { &hf_data_checksum_status,
            { "Data Checksum Status",            "pn532_hci.data_checksum.status",
            FT_UINT8, BASE_NONE, VALS(proto_checksum_vals), 0x00,
            NULL, HFILL }
        },
        { &hf_specific_application_level_error_code,
            { "Specific Application Level Error Code","pn532_hci.specific_application_level_error_code",
            FT_UINT8, BASE_HEX, NULL, 0x00,
            NULL, HFILL }
        },
        { &hf_postable,
            { "Postamble",                       "pn532_hci.postamble",
            FT_BYTES, BASE_NONE, NULL, 0x00,
            NULL, HFILL }
        },
        { &hf_ignored,
            { "Ignored",                         "pn532_hci.ignored",
            FT_BYTES, BASE_NONE, NULL, 0x00,
            NULL, HFILL }
        }
    };

    static gint *ett[] = {
        &ett_pn532_hci
    };

    static ei_register_info ei[] = {
        { &ei_invalid_length_checksum, { "pn532_hci.expert.invalid_length_checksum", PI_PROTOCOL, PI_WARN, "Invalid Length Checksum", EXPFILL }},
        { &ei_invalid_data_checksum,   { "pn532_hci.expert.invalid_data_checksum", PI_PROTOCOL, PI_WARN, "Invalid Data Checksum", EXPFILL }},
    };

    proto_pn532_hci = proto_register_protocol("NXP PN532 HCI", "PN532_HCI", "pn532_hci");
    pn532_hci_handle = register_dissector("pn532_hci", dissect_pn532_hci, proto_pn532_hci);

    proto_register_field_array(proto_pn532_hci, hf, array_length(hf));
    proto_register_subtree_array(ett, array_length(ett));
    expert_module = expert_register_protocol(proto_pn532_hci);
    expert_register_field_array(expert_module, ei, array_length(ei));

    module = prefs_register_protocol(proto_pn532_hci, NULL);
    prefs_register_static_text_preference(module, "version",
            "PN532 HCI protocol version is based on: \"UM0701-02; PN532 User Manual\"",
            "Version of protocol supported by this dissector.");
}

void
proto_reg_handoff_pn532_hci(void)
{
    pn532_handle = find_dissector_add_dependency("pn532", proto_pn532_hci);

    dissector_add_uint("usb.product", (0x04e6 << 16) | 0x5591, pn532_hci_handle);

    dissector_add_for_decode_as("usb.device", pn532_hci_handle);
    dissector_add_for_decode_as("usb.protocol", pn532_hci_handle);
}

/*
 * Editor modelines  -  https://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:
 */