1 /* packet-bthci_sco.c
2  * Routines for the Bluetooth SCO dissection
3  * Copyright 2002, Christoph Scholz <scholz@cs.uni-bonn.de>
4  *
5  * Refactored for wireshark checkin
6  *   Ronnie Sahlberg 2006
7  *
8  * Wireshark - Network traffic analyzer
9  * By Gerald Combs <gerald@wireshark.org>
10  * Copyright 1998 Gerald Combs
11  *
12  * SPDX-License-Identifier: GPL-2.0-or-later
13  */
14 
15 #include "config.h"
16 
17 #include <epan/packet.h>
18 #include <epan/addr_resolv.h>
19 
20 #include "packet-bluetooth.h"
21 #include "packet-bthci_sco.h"
22 
23 /* Initialize the protocol and registered fields */
24 static int proto_bthci_sco = -1;
25 static int hf_bthci_sco_reserved = -1;
26 static int hf_bthci_sco_packet_status = -1;
27 static int hf_bthci_sco_chandle = -1;
28 static int hf_bthci_sco_length = -1;
29 static int hf_bthci_sco_data = -1;
30 
31 static int hf_bthci_sco_connect_in = -1;
32 static int hf_bthci_sco_disconnect_in = -1;
33 static int hf_bthci_sco_stream_number = -1;
34 
35 /* Initialize the subtree pointers */
36 static gint ett_bthci_sco = -1;
37 
38 wmem_tree_t *bthci_sco_stream_numbers = NULL;
39 
40 static dissector_handle_t bthci_sco_handle;
41 
42 static const value_string packet_status_vals[] = {
43     { 0x00,   "Correctly Received Data"},
44     { 0x01,   "Possibly Invalid Data"},
45     { 0x02,   "No Data Received"},
46     { 0x03,   "Data Partially Lost"},
47     {0x0, NULL}
48 };
49 
50 void proto_register_bthci_sco(void);
51 void proto_reg_handoff_bthci_sco(void);
52 
53 /* Code to actually dissect the packets */
54 static gint
dissect_bthci_sco(tvbuff_t * tvb,packet_info * pinfo,proto_tree * tree,void * data)55 dissect_bthci_sco(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, void *data)
56 {
57     proto_item               *ti;
58     proto_tree               *bthci_sco_tree;
59     gint                      offset = 0;
60     guint16                   flags;
61     bluetooth_data_t         *bluetooth_data;
62     wmem_tree_key_t           key[6];
63     guint32                   k_connection_handle;
64     guint32                   k_frame_number;
65     guint32                   k_interface_id;
66     guint32                   k_adapter_id;
67     guint32                   k_bd_addr_oui;
68     guint32                   k_bd_addr_id;
69     remote_bdaddr_t          *remote_bdaddr;
70     const gchar              *localhost_name;
71     guint8                   *localhost_bdaddr;
72     const gchar              *localhost_ether_addr;
73     gchar                    *localhost_addr_name;
74     gint                      localhost_length;
75     localhost_bdaddr_entry_t *localhost_bdaddr_entry;
76     localhost_name_entry_t   *localhost_name_entry;
77     chandle_session_t        *chandle_session;
78     wmem_tree_t              *subtree;
79     proto_item               *sub_item;
80     bthci_sco_stream_number_t  *sco_stream_number;
81 
82     ti = proto_tree_add_item(tree, proto_bthci_sco, tvb, offset, tvb_captured_length(tvb), ENC_NA);
83     bthci_sco_tree = proto_item_add_subtree(ti, ett_bthci_sco);
84 
85     switch (pinfo->p2p_dir) {
86         case P2P_DIR_SENT:
87             col_set_str(pinfo->cinfo, COL_INFO, "Sent ");
88             break;
89         case P2P_DIR_RECV:
90             col_set_str(pinfo->cinfo, COL_INFO, "Rcvd ");
91             break;
92         default:
93             col_add_fstr(pinfo->cinfo, COL_INFO, "UnknownDirection ");
94             break;
95     }
96 
97     proto_tree_add_item(bthci_sco_tree, hf_bthci_sco_reserved, tvb,      offset, 2, ENC_LITTLE_ENDIAN);
98     proto_tree_add_item(bthci_sco_tree, hf_bthci_sco_packet_status, tvb, offset, 2, ENC_LITTLE_ENDIAN);
99     proto_tree_add_item(bthci_sco_tree, hf_bthci_sco_chandle, tvb,       offset, 2, ENC_LITTLE_ENDIAN);
100     flags   = tvb_get_letohs(tvb, offset);
101     offset += 2;
102 
103     proto_tree_add_item(bthci_sco_tree, hf_bthci_sco_length, tvb, offset, 1, ENC_LITTLE_ENDIAN);
104     offset++;
105 
106     bluetooth_data = (bluetooth_data_t *) data;
107     DISSECTOR_ASSERT(bluetooth_data);
108 
109     k_interface_id      = bluetooth_data->interface_id;
110     k_adapter_id        = bluetooth_data->adapter_id;
111     k_connection_handle = flags & 0x0fff;
112     k_frame_number      = pinfo->num;
113 
114     key[0].length = 1;
115     key[0].key    = &k_interface_id;
116     key[1].length = 1;
117     key[1].key    = &k_adapter_id;
118     key[2].length = 0;
119     key[2].key    = NULL;
120 
121     subtree = (wmem_tree_t *) wmem_tree_lookup32_array(bthci_sco_stream_numbers, key);
122     sco_stream_number = (subtree) ? (bthci_sco_stream_number_t *) wmem_tree_lookup32_le(subtree, pinfo->num) : NULL;
123 
124     key[2].length = 1;
125     key[2].key    = &k_connection_handle;
126     key[3].length = 0;
127     key[3].key    = NULL;
128 
129     subtree = (wmem_tree_t *) wmem_tree_lookup32_array(bluetooth_data->chandle_sessions, key);
130     chandle_session = (subtree) ? (chandle_session_t *) wmem_tree_lookup32_le(subtree, pinfo->num) : NULL;
131     if (!(chandle_session &&
132             chandle_session->connect_in_frame < pinfo->num &&
133             chandle_session->disconnect_in_frame > pinfo->num)){
134         chandle_session = NULL;
135     }
136 
137     key[3].length = 1;
138     key[3].key    = &k_frame_number;
139     key[4].length = 0;
140     key[4].key    = NULL;
141 
142     /* remote bdaddr and name */
143     remote_bdaddr = (remote_bdaddr_t *)wmem_tree_lookup32_array_le(bluetooth_data->chandle_to_bdaddr, key);
144     if (remote_bdaddr && remote_bdaddr->interface_id == bluetooth_data->interface_id &&
145             remote_bdaddr->adapter_id == bluetooth_data->adapter_id &&
146             remote_bdaddr->chandle == (flags & 0x0fff)) {
147         guint32         bd_addr_oui;
148         guint32         bd_addr_id;
149         device_name_t  *device_name;
150         const gchar    *remote_name;
151         const gchar    *remote_ether_addr;
152         gchar          *remote_addr_name;
153         gint            remote_length;
154 
155         bd_addr_oui = remote_bdaddr->bd_addr[0] << 16 | remote_bdaddr->bd_addr[1] << 8 | remote_bdaddr->bd_addr[2];
156         bd_addr_id  = remote_bdaddr->bd_addr[3] << 16 | remote_bdaddr->bd_addr[4] << 8 | remote_bdaddr->bd_addr[5];
157 
158         k_bd_addr_oui  = bd_addr_oui;
159         k_bd_addr_id   = bd_addr_id;
160         k_frame_number = pinfo->num;
161 
162         key[0].length = 1;
163         key[0].key    = &k_interface_id;
164         key[1].length = 1;
165         key[1].key    = &k_adapter_id;
166         key[2].length = 1;
167         key[2].key    = &k_bd_addr_id;
168         key[3].length = 1;
169         key[3].key    = &k_bd_addr_oui;
170         key[4].length = 1;
171         key[4].key    = &k_frame_number;
172         key[5].length = 0;
173         key[5].key    = NULL;
174 
175         device_name = (device_name_t *)wmem_tree_lookup32_array_le(bluetooth_data->bdaddr_to_name, key);
176         if (device_name && device_name->bd_addr_oui == bd_addr_oui && device_name->bd_addr_id == bd_addr_id)
177             remote_name = device_name->name;
178         else
179             remote_name = "";
180 
181         remote_ether_addr = get_ether_name(remote_bdaddr->bd_addr);
182         remote_length = (gint)(strlen(remote_ether_addr) + 3 + strlen(remote_name) + 1);
183         remote_addr_name = (gchar *)wmem_alloc(pinfo->pool, remote_length);
184 
185         g_snprintf(remote_addr_name, remote_length, "%s (%s)", remote_ether_addr, remote_name);
186 
187         if (pinfo->p2p_dir == P2P_DIR_RECV) {
188             set_address(&pinfo->net_src, AT_STRINGZ, (int)strlen(remote_name) + 1, remote_name);
189             set_address(&pinfo->dl_src, AT_ETHER, 6, remote_bdaddr->bd_addr);
190             set_address(&pinfo->src, AT_STRINGZ, (int)strlen(remote_addr_name) + 1, remote_addr_name);
191         } else if (pinfo->p2p_dir == P2P_DIR_SENT) {
192             set_address(&pinfo->net_dst, AT_STRINGZ, (int)strlen(remote_name) + 1, remote_name);
193             set_address(&pinfo->dl_dst, AT_ETHER, 6, remote_bdaddr->bd_addr);
194             set_address(&pinfo->dst, AT_STRINGZ, (int)strlen(remote_addr_name) + 1, remote_addr_name);
195         }
196     } else {
197         if (pinfo->p2p_dir == P2P_DIR_RECV) {
198             set_address(&pinfo->net_src, AT_STRINGZ, 1, "");
199             set_address(&pinfo->dl_src, AT_STRINGZ, 1, "");
200             set_address(&pinfo->src, AT_STRINGZ, 10, "remote ()");
201         } else if (pinfo->p2p_dir == P2P_DIR_SENT) {
202             set_address(&pinfo->net_dst, AT_STRINGZ, 1, "");
203             set_address(&pinfo->dl_dst, AT_STRINGZ, 1, "");
204             set_address(&pinfo->dst, AT_STRINGZ, 10, "remote ()");
205         }
206     }
207 
208     k_interface_id      = bluetooth_data->interface_id;
209     k_adapter_id        = bluetooth_data->adapter_id;
210     k_frame_number      = pinfo->num;
211 
212     /* localhost bdaddr and name */
213     key[0].length = 1;
214     key[0].key    = &k_interface_id;
215     key[1].length = 1;
216     key[1].key    = &k_adapter_id;
217     key[2].length = 1;
218     key[2].key    = &k_frame_number;
219     key[3].length = 0;
220     key[3].key    = NULL;
221 
222 
223     localhost_bdaddr_entry = (localhost_bdaddr_entry_t *)wmem_tree_lookup32_array_le(bluetooth_data->localhost_bdaddr, key);
224     localhost_bdaddr = (guint8 *) wmem_alloc(pinfo->pool, 6);
225     if (localhost_bdaddr_entry && localhost_bdaddr_entry->interface_id == bluetooth_data->interface_id &&
226         localhost_bdaddr_entry->adapter_id == bluetooth_data->adapter_id) {
227 
228         localhost_ether_addr = get_ether_name(localhost_bdaddr_entry->bd_addr);
229         memcpy(localhost_bdaddr, localhost_bdaddr_entry->bd_addr, 6);
230     } else {
231         localhost_ether_addr = "localhost";
232         /* XXX - is this the right value to use? */
233         memset(localhost_bdaddr, 0, 6);
234     }
235 
236     localhost_name_entry = (localhost_name_entry_t *)wmem_tree_lookup32_array_le(bluetooth_data->localhost_name, key);
237     if (localhost_name_entry && localhost_name_entry->interface_id == bluetooth_data->interface_id &&
238             localhost_name_entry->adapter_id == bluetooth_data->adapter_id)
239         localhost_name = localhost_name_entry->name;
240     else
241         localhost_name = "";
242 
243     localhost_length = (gint)(strlen(localhost_ether_addr) + 3 + strlen(localhost_name) + 1);
244     localhost_addr_name = (gchar *)wmem_alloc(pinfo->pool, localhost_length);
245 
246     g_snprintf(localhost_addr_name, localhost_length, "%s (%s)", localhost_ether_addr, localhost_name);
247 
248     if (pinfo->p2p_dir == P2P_DIR_RECV) {
249         set_address(&pinfo->net_dst, AT_STRINGZ, (int)strlen(localhost_name) + 1, localhost_name);
250         set_address(&pinfo->dl_dst, AT_ETHER, 6, localhost_bdaddr);
251         set_address(&pinfo->dst, AT_STRINGZ, (int)strlen(localhost_addr_name) + 1, localhost_addr_name);
252     } else if (pinfo->p2p_dir == P2P_DIR_SENT) {
253         set_address(&pinfo->net_src, AT_STRINGZ, (int)strlen(localhost_name) + 1, localhost_name);
254         set_address(&pinfo->dl_src, AT_ETHER, 6, localhost_bdaddr);
255         set_address(&pinfo->src, AT_STRINGZ, (int)strlen(localhost_addr_name) + 1, localhost_addr_name);
256     }
257 
258     proto_tree_add_item(bthci_sco_tree, hf_bthci_sco_data, tvb, offset, tvb_reported_length(tvb), ENC_NA);
259 
260     if (chandle_session) {
261         sub_item = proto_tree_add_uint(bthci_sco_tree, hf_bthci_sco_connect_in, tvb, 0, 0, chandle_session->connect_in_frame);
262         proto_item_set_generated(sub_item);
263 
264         if (chandle_session->disconnect_in_frame < G_MAXUINT32) {
265             sub_item = proto_tree_add_uint(bthci_sco_tree, hf_bthci_sco_disconnect_in, tvb, 0, 0, chandle_session->disconnect_in_frame);
266             proto_item_set_generated(sub_item);
267         }
268     }
269     if (sco_stream_number) {
270         sub_item = proto_tree_add_uint(bthci_sco_tree, hf_bthci_sco_stream_number, tvb, 0, 0, sco_stream_number->stream_number);
271         proto_item_set_generated(sub_item);
272     }
273 
274     return tvb_reported_length(tvb);
275 }
276 
277 
278 void
proto_register_bthci_sco(void)279 proto_register_bthci_sco(void)
280 {
281     static hf_register_info hf[] = {
282         { &hf_bthci_sco_reserved,
283             { "Reserved",                    "bthci_sco.reserved",
284             FT_UINT16, BASE_HEX, NULL, 0xC000,
285             NULL, HFILL }
286         },
287         { &hf_bthci_sco_packet_status,
288             { "Packet Status",               "bthci_sco.packet_status",
289             FT_UINT16, BASE_HEX, VALS(packet_status_vals), 0x3000,
290             NULL, HFILL }
291         },
292         { &hf_bthci_sco_chandle,
293             { "Connection Handle",           "bthci_sco.chandle",
294             FT_UINT16, BASE_HEX, NULL, 0x0FFF,
295             NULL, HFILL }
296         },
297         { &hf_bthci_sco_connect_in,
298             { "Connect in frame",            "bthci_sco.connect_in",
299             FT_FRAMENUM, BASE_NONE, NULL, 0x0,
300             NULL, HFILL }
301         },
302         { &hf_bthci_sco_disconnect_in,
303             { "Disconnect in frame",         "bthci_sco.disconnect_in",
304             FT_FRAMENUM, BASE_NONE, NULL, 0x0,
305             NULL, HFILL }
306         },
307         { &hf_bthci_sco_stream_number,
308             { "Stream Number",               "bthci_sco.stream_number",
309             FT_UINT32, BASE_DEC, NULL, 0x00,
310             NULL, HFILL }
311         },
312         { &hf_bthci_sco_length,
313             { "Data Total Length",           "bthci_sco.length",
314             FT_UINT8, BASE_DEC, NULL, 0x0,
315             NULL, HFILL }
316         },
317         { &hf_bthci_sco_data,
318             { "Data",                        "bthci_sco.data",
319             FT_NONE, BASE_NONE, NULL, 0x0,
320             NULL, HFILL }
321         },
322     };
323 
324     /* Setup protocol subtree array */
325     static gint *ett[] = {
326       &ett_bthci_sco
327     };
328 
329     /* Register the protocol name and description */
330     proto_bthci_sco = proto_register_protocol("Bluetooth HCI SCO Packet", "HCI_SCO", "bthci_sco");
331     bthci_sco_handle = register_dissector("bthci_sco", dissect_bthci_sco, proto_bthci_sco);
332 
333     bthci_sco_stream_numbers = wmem_tree_new_autoreset(wmem_epan_scope(), wmem_file_scope());
334 
335     /* Required function calls to register the header fields and subtrees used */
336     proto_register_field_array(proto_bthci_sco, hf, array_length(hf));
337     proto_register_subtree_array(ett, array_length(ett));
338 }
339 
340 
341 void
proto_reg_handoff_bthci_sco(void)342 proto_reg_handoff_bthci_sco(void)
343 {
344     dissector_add_uint("hci_h4.type", HCI_H4_TYPE_SCO, bthci_sco_handle);
345     dissector_add_uint("hci_h1.type", BTHCI_CHANNEL_SCO, bthci_sco_handle);
346 }
347 
348 /*
349  * Editor modelines  -  https://www.wireshark.org/tools/modelines.html
350  *
351  * Local variables:
352  * c-basic-offset: 4
353  * tab-width: 8
354  * indent-tabs-mode: nil
355  * End:
356  *
357  * vi: set shiftwidth=4 tabstop=8 expandtab:
358  * :indentSize=4:tabSize=8:noTabs=true:
359  */
360