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
|
/* packet-bthci_sco.c
* Routines for the Bluetooth SCO dissection
* Copyright 2002, Christoph Scholz <scholz@cs.uni-bonn.de>
*
* Refactored for wireshark checkin
* Ronnie Sahlberg 2006
*
* 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/addr_resolv.h>
#include "packet-bluetooth.h"
#include "packet-bthci_sco.h"
/* Initialize the protocol and registered fields */
static int proto_bthci_sco;
static int hf_bthci_sco_reserved;
static int hf_bthci_sco_packet_status;
static int hf_bthci_sco_chandle;
static int hf_bthci_sco_length;
static int hf_bthci_sco_data;
static int hf_bthci_sco_connect_in;
static int hf_bthci_sco_disconnect_in;
static int hf_bthci_sco_stream_number;
/* Initialize the subtree pointers */
static int ett_bthci_sco;
wmem_tree_t *bthci_sco_stream_numbers;
static dissector_handle_t bthci_sco_handle;
static const value_string packet_status_vals[] = {
{ 0x00, "Correctly Received Data"},
{ 0x01, "Possibly Invalid Data"},
{ 0x02, "No Data Received"},
{ 0x03, "Data Partially Lost"},
{0x0, NULL}
};
void proto_register_bthci_sco(void);
void proto_reg_handoff_bthci_sco(void);
/* Code to actually dissect the packets */
static int
dissect_bthci_sco(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, void *data)
{
proto_item *ti;
proto_tree *bthci_sco_tree;
int offset = 0;
uint16_t flags;
bluetooth_data_t *bluetooth_data;
wmem_tree_key_t key[6];
uint32_t k_connection_handle;
uint32_t k_frame_number;
uint32_t k_interface_id;
uint32_t k_adapter_id;
uint32_t k_bd_addr_oui;
uint32_t k_bd_addr_id;
uint16_t packet_status;
remote_bdaddr_t *remote_bdaddr;
const char *localhost_name;
uint8_t *localhost_bdaddr;
const char *localhost_ether_addr;
char *localhost_addr_name;
int localhost_length;
localhost_bdaddr_entry_t *localhost_bdaddr_entry;
localhost_name_entry_t *localhost_name_entry;
chandle_session_t *chandle_session;
wmem_tree_t *subtree;
proto_item *sub_item;
bthci_sco_stream_number_t *sco_stream_number;
ti = proto_tree_add_item(tree, proto_bthci_sco, tvb, offset, tvb_captured_length(tvb), ENC_NA);
bthci_sco_tree = proto_item_add_subtree(ti, ett_bthci_sco);
col_set_str(pinfo->cinfo, COL_PROTOCOL, "HCI_SCO");
switch (pinfo->p2p_dir) {
case P2P_DIR_SENT:
col_set_str(pinfo->cinfo, COL_INFO, "Sent ");
break;
case P2P_DIR_RECV:
col_set_str(pinfo->cinfo, COL_INFO, "Rcvd ");
break;
default:
col_set_str(pinfo->cinfo, COL_INFO, "UnknownDirection ");
break;
}
proto_tree_add_item(bthci_sco_tree, hf_bthci_sco_reserved, tvb, offset, 2, ENC_LITTLE_ENDIAN);
proto_tree_add_item(bthci_sco_tree, hf_bthci_sco_packet_status, tvb, offset, 2, ENC_LITTLE_ENDIAN);
proto_tree_add_item(bthci_sco_tree, hf_bthci_sco_chandle, tvb, offset, 2, ENC_LITTLE_ENDIAN);
flags = tvb_get_letohs(tvb, offset);
offset += 2;
packet_status = (flags >> 12) & 0x03;
col_append_fstr(pinfo->cinfo, COL_INFO, "SCO - %s", val_to_str(packet_status, packet_status_vals, "%u"));
proto_tree_add_item(bthci_sco_tree, hf_bthci_sco_length, tvb, offset, 1, ENC_LITTLE_ENDIAN);
offset++;
bluetooth_data = (bluetooth_data_t *) data;
DISSECTOR_ASSERT(bluetooth_data);
k_interface_id = bluetooth_data->interface_id;
k_adapter_id = bluetooth_data->adapter_id;
k_connection_handle = flags & 0x0fff;
k_frame_number = pinfo->num;
key[0].length = 1;
key[0].key = &k_interface_id;
key[1].length = 1;
key[1].key = &k_adapter_id;
key[2].length = 0;
key[2].key = NULL;
subtree = (wmem_tree_t *) wmem_tree_lookup32_array(bthci_sco_stream_numbers, key);
sco_stream_number = (subtree) ? (bthci_sco_stream_number_t *) wmem_tree_lookup32_le(subtree, pinfo->num) : NULL;
key[2].length = 1;
key[2].key = &k_connection_handle;
key[3].length = 0;
key[3].key = NULL;
subtree = (wmem_tree_t *) wmem_tree_lookup32_array(bluetooth_data->chandle_sessions, key);
chandle_session = (subtree) ? (chandle_session_t *) wmem_tree_lookup32_le(subtree, pinfo->num) : NULL;
if (!(chandle_session &&
chandle_session->connect_in_frame < pinfo->num &&
chandle_session->disconnect_in_frame > pinfo->num)){
chandle_session = NULL;
}
key[3].length = 1;
key[3].key = &k_frame_number;
key[4].length = 0;
key[4].key = NULL;
/* remote bdaddr and name */
remote_bdaddr = (remote_bdaddr_t *)wmem_tree_lookup32_array_le(bluetooth_data->chandle_to_bdaddr, key);
if (remote_bdaddr && remote_bdaddr->interface_id == bluetooth_data->interface_id &&
remote_bdaddr->adapter_id == bluetooth_data->adapter_id &&
remote_bdaddr->chandle == (flags & 0x0fff)) {
uint32_t bd_addr_oui;
uint32_t bd_addr_id;
device_name_t *device_name;
const char *remote_name;
const char *remote_ether_addr;
char *remote_addr_name;
int remote_length;
bd_addr_oui = remote_bdaddr->bd_addr[0] << 16 | remote_bdaddr->bd_addr[1] << 8 | remote_bdaddr->bd_addr[2];
bd_addr_id = remote_bdaddr->bd_addr[3] << 16 | remote_bdaddr->bd_addr[4] << 8 | remote_bdaddr->bd_addr[5];
k_bd_addr_oui = bd_addr_oui;
k_bd_addr_id = bd_addr_id;
k_frame_number = pinfo->num;
key[0].length = 1;
key[0].key = &k_interface_id;
key[1].length = 1;
key[1].key = &k_adapter_id;
key[2].length = 1;
key[2].key = &k_bd_addr_id;
key[3].length = 1;
key[3].key = &k_bd_addr_oui;
key[4].length = 1;
key[4].key = &k_frame_number;
key[5].length = 0;
key[5].key = NULL;
device_name = (device_name_t *)wmem_tree_lookup32_array_le(bluetooth_data->bdaddr_to_name, key);
if (device_name && device_name->bd_addr_oui == bd_addr_oui && device_name->bd_addr_id == bd_addr_id)
remote_name = device_name->name;
else
remote_name = "";
remote_ether_addr = get_ether_name(remote_bdaddr->bd_addr);
remote_length = (int)(strlen(remote_ether_addr) + 3 + strlen(remote_name) + 1);
remote_addr_name = (char *)wmem_alloc(pinfo->pool, remote_length);
snprintf(remote_addr_name, remote_length, "%s (%s)", remote_ether_addr, remote_name);
if (pinfo->p2p_dir == P2P_DIR_RECV) {
set_address(&pinfo->net_src, AT_STRINGZ, (int)strlen(remote_name) + 1, remote_name);
set_address(&pinfo->dl_src, AT_ETHER, 6, remote_bdaddr->bd_addr);
set_address(&pinfo->src, AT_STRINGZ, (int)strlen(remote_addr_name) + 1, remote_addr_name);
} else if (pinfo->p2p_dir == P2P_DIR_SENT) {
set_address(&pinfo->net_dst, AT_STRINGZ, (int)strlen(remote_name) + 1, remote_name);
set_address(&pinfo->dl_dst, AT_ETHER, 6, remote_bdaddr->bd_addr);
set_address(&pinfo->dst, AT_STRINGZ, (int)strlen(remote_addr_name) + 1, remote_addr_name);
}
} else {
if (pinfo->p2p_dir == P2P_DIR_RECV) {
set_address(&pinfo->net_src, AT_STRINGZ, 1, "");
set_address(&pinfo->dl_src, AT_STRINGZ, 1, "");
set_address(&pinfo->src, AT_STRINGZ, 10, "remote ()");
} else if (pinfo->p2p_dir == P2P_DIR_SENT) {
set_address(&pinfo->net_dst, AT_STRINGZ, 1, "");
set_address(&pinfo->dl_dst, AT_STRINGZ, 1, "");
set_address(&pinfo->dst, AT_STRINGZ, 10, "remote ()");
}
}
k_interface_id = bluetooth_data->interface_id;
k_adapter_id = bluetooth_data->adapter_id;
k_frame_number = pinfo->num;
/* localhost bdaddr and name */
key[0].length = 1;
key[0].key = &k_interface_id;
key[1].length = 1;
key[1].key = &k_adapter_id;
key[2].length = 1;
key[2].key = &k_frame_number;
key[3].length = 0;
key[3].key = NULL;
localhost_bdaddr_entry = (localhost_bdaddr_entry_t *)wmem_tree_lookup32_array_le(bluetooth_data->localhost_bdaddr, key);
localhost_bdaddr = (uint8_t *) wmem_alloc(pinfo->pool, 6);
if (localhost_bdaddr_entry && localhost_bdaddr_entry->interface_id == bluetooth_data->interface_id &&
localhost_bdaddr_entry->adapter_id == bluetooth_data->adapter_id) {
localhost_ether_addr = get_ether_name(localhost_bdaddr_entry->bd_addr);
memcpy(localhost_bdaddr, localhost_bdaddr_entry->bd_addr, 6);
} else {
localhost_ether_addr = "localhost";
/* XXX - is this the right value to use? */
memset(localhost_bdaddr, 0, 6);
}
localhost_name_entry = (localhost_name_entry_t *)wmem_tree_lookup32_array_le(bluetooth_data->localhost_name, key);
if (localhost_name_entry && localhost_name_entry->interface_id == bluetooth_data->interface_id &&
localhost_name_entry->adapter_id == bluetooth_data->adapter_id)
localhost_name = localhost_name_entry->name;
else
localhost_name = "";
localhost_length = (int)(strlen(localhost_ether_addr) + 3 + strlen(localhost_name) + 1);
localhost_addr_name = (char *)wmem_alloc(pinfo->pool, localhost_length);
snprintf(localhost_addr_name, localhost_length, "%s (%s)", localhost_ether_addr, localhost_name);
if (pinfo->p2p_dir == P2P_DIR_RECV) {
set_address(&pinfo->net_dst, AT_STRINGZ, (int)strlen(localhost_name) + 1, localhost_name);
set_address(&pinfo->dl_dst, AT_ETHER, 6, localhost_bdaddr);
set_address(&pinfo->dst, AT_STRINGZ, (int)strlen(localhost_addr_name) + 1, localhost_addr_name);
} else if (pinfo->p2p_dir == P2P_DIR_SENT) {
set_address(&pinfo->net_src, AT_STRINGZ, (int)strlen(localhost_name) + 1, localhost_name);
set_address(&pinfo->dl_src, AT_ETHER, 6, localhost_bdaddr);
set_address(&pinfo->src, AT_STRINGZ, (int)strlen(localhost_addr_name) + 1, localhost_addr_name);
}
proto_tree_add_item(bthci_sco_tree, hf_bthci_sco_data, tvb, offset, tvb_reported_length(tvb), ENC_NA);
if (chandle_session) {
sub_item = proto_tree_add_uint(bthci_sco_tree, hf_bthci_sco_connect_in, tvb, 0, 0, chandle_session->connect_in_frame);
proto_item_set_generated(sub_item);
if (chandle_session->disconnect_in_frame < UINT32_MAX) {
sub_item = proto_tree_add_uint(bthci_sco_tree, hf_bthci_sco_disconnect_in, tvb, 0, 0, chandle_session->disconnect_in_frame);
proto_item_set_generated(sub_item);
}
}
if (sco_stream_number) {
sub_item = proto_tree_add_uint(bthci_sco_tree, hf_bthci_sco_stream_number, tvb, 0, 0, sco_stream_number->stream_number);
proto_item_set_generated(sub_item);
}
return tvb_reported_length(tvb);
}
void
proto_register_bthci_sco(void)
{
static hf_register_info hf[] = {
{ &hf_bthci_sco_reserved,
{ "Reserved", "bthci_sco.reserved",
FT_UINT16, BASE_HEX, NULL, 0xC000,
NULL, HFILL }
},
{ &hf_bthci_sco_packet_status,
{ "Packet Status", "bthci_sco.packet_status",
FT_UINT16, BASE_HEX, VALS(packet_status_vals), 0x3000,
NULL, HFILL }
},
{ &hf_bthci_sco_chandle,
{ "Connection Handle", "bthci_sco.chandle",
FT_UINT16, BASE_HEX, NULL, 0x0FFF,
NULL, HFILL }
},
{ &hf_bthci_sco_connect_in,
{ "Connect in frame", "bthci_sco.connect_in",
FT_FRAMENUM, BASE_NONE, NULL, 0x0,
NULL, HFILL }
},
{ &hf_bthci_sco_disconnect_in,
{ "Disconnect in frame", "bthci_sco.disconnect_in",
FT_FRAMENUM, BASE_NONE, NULL, 0x0,
NULL, HFILL }
},
{ &hf_bthci_sco_stream_number,
{ "Stream Number", "bthci_sco.stream_number",
FT_UINT32, BASE_DEC, NULL, 0x00,
NULL, HFILL }
},
{ &hf_bthci_sco_length,
{ "Data Total Length", "bthci_sco.length",
FT_UINT8, BASE_DEC, NULL, 0x0,
NULL, HFILL }
},
{ &hf_bthci_sco_data,
{ "Data", "bthci_sco.data",
FT_NONE, BASE_NONE, NULL, 0x0,
NULL, HFILL }
},
};
/* Setup protocol subtree array */
static int *ett[] = {
&ett_bthci_sco
};
/* Register the protocol name and description */
proto_bthci_sco = proto_register_protocol("Bluetooth HCI SCO Packet", "HCI_SCO", "bthci_sco");
bthci_sco_handle = register_dissector("bthci_sco", dissect_bthci_sco, proto_bthci_sco);
bthci_sco_stream_numbers = wmem_tree_new_autoreset(wmem_epan_scope(), wmem_file_scope());
/* Required function calls to register the header fields and subtrees used */
proto_register_field_array(proto_bthci_sco, hf, array_length(hf));
proto_register_subtree_array(ett, array_length(ett));
}
void
proto_reg_handoff_bthci_sco(void)
{
dissector_add_uint("hci_h4.type", HCI_H4_TYPE_SCO, bthci_sco_handle);
dissector_add_uint("hci_h1.type", BTHCI_CHANNEL_SCO, bthci_sco_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:
*/
|