1 /* packet-bthsp.c
2  * Routines for Bluetooth Headset Profile (HSP)
3  *
4  * Copyright 2013, Michal Labedzki for Tieto Corporation
5  *
6  * Wireshark - Network traffic analyzer
7  * By Gerald Combs <gerald@wireshark.org>
8  * Copyright 1998 Gerald Combs
9  *
10  * SPDX-License-Identifier: GPL-2.0-or-later
11  */
12 
13 #include "config.h"
14 
15 #include <epan/packet.h>
16 #include <epan/prefs.h>
17 #include <epan/expert.h>
18 #include "packet-btrfcomm.h"
19 #include "packet-btsdp.h"
20 
21 static int proto_bthsp = -1;
22 
23 static int hf_command                                                      = -1;
24 static int hf_parameters                                                   = -1;
25 static int hf_command_in                                                   = -1;
26 static int hf_unsolicited                                                  = -1;
27 static int hf_role                                                         = -1;
28 static int hf_at_cmd                                                       = -1;
29 static int hf_at_cmd_type                                                  = -1;
30 static int hf_at_command_line_prefix                                       = -1;
31 static int hf_at_ignored                                                   = -1;
32 static int hf_parameter                                                    = -1;
33 static int hf_unknown_parameter                                            = -1;
34 static int hf_data                                                         = -1;
35 static int hf_fragment                                                     = -1;
36 static int hf_fragmented                                                   = -1;
37 static int hf_vgs                                                          = -1;
38 static int hf_vgm                                                          = -1;
39 static int hf_ckpd                                                         = -1;
40 
41 static expert_field ei_non_mandatory_command                          = EI_INIT;
42 static expert_field ei_invalid_usage                                  = EI_INIT;
43 static expert_field ei_unknown_parameter                              = EI_INIT;
44 static expert_field ei_vgm_gain                                       = EI_INIT;
45 static expert_field ei_vgs_gain                                       = EI_INIT;
46 static expert_field ei_ckpd                                           = EI_INIT;
47 
48 static gint ett_bthsp            = -1;
49 static gint ett_bthsp_command    = -1;
50 static gint ett_bthsp_parameters = -1;
51 
52 static dissector_handle_t bthsp_handle;
53 
54 static wmem_tree_t *fragments = NULL;
55 
56 #define ROLE_UNKNOWN  0
57 #define ROLE_AG       1
58 #define ROLE_HS       2
59 
60 #define TYPE_UNKNOWN       0x0000
61 #define TYPE_RESPONSE_ACK  0x0d0a
62 #define TYPE_RESPONSE      0x003a
63 #define TYPE_ACTION        0x003d
64 #define TYPE_ACTION_SIMPLY 0x000d
65 #define TYPE_READ          0x003f
66 #define TYPE_TEST          0x3d3f
67 
68 static gint hsp_role = ROLE_UNKNOWN;
69 
70 enum reassemble_state_t {
71     REASSEMBLE_FRAGMENT,
72     REASSEMBLE_PARTIALLY,
73     REASSEMBLE_DONE
74 };
75 
76 typedef struct _fragment_t {
77     guint32                  interface_id;
78     guint32                  adapter_id;
79     guint32                  chandle;
80     guint32                  dlci;
81     guint32                  role;
82 
83     guint                    idx;
84     guint                    length;
85     guint8                  *data;
86     struct _fragment_t      *previous_fragment;
87 
88     guint                    reassemble_start_offset;
89     guint                    reassemble_end_offset;
90     enum reassemble_state_t  reassemble_state;
91 } fragment_t;
92 
93 typedef struct _at_cmd_t {
94     const char *name;
95     const char *long_name;
96 
97     gboolean (*check_command)(gint role, guint16 type);
98     gboolean (*dissect_parameter)(tvbuff_t *tvb, packet_info *pinfo,
99             proto_tree *tree, gint offset, gint role, guint16 type,
100             guint8 *parameter_stream, guint parameter_number,
101             gint parameter_length, void **data);
102 } at_cmd_t;
103 
104 static const value_string role_vals[] = {
105     { ROLE_UNKNOWN,   "Unknown" },
106     { ROLE_AG,        "AG - Audio Gate" },
107     { ROLE_HS,        "HS - Headset" },
108     { 0, NULL }
109 };
110 
111 static const value_string at_cmd_type_vals[] = {
112     { 0x0d,   "Action Command" },
113     { 0x3a,   "Response" },
114     { 0x3d,   "Action Command" },
115     { 0x3f,   "Read Command" },
116     { 0x0d0a, "Response" },
117     { 0x3d3f, "Test Command" },
118     { 0, NULL }
119 };
120 
121 static const enum_val_t pref_hsp_role[] = {
122     { "off",     "Off",                    ROLE_UNKNOWN },
123     { "ag",      "Sent is AG, Rcvd is HS", ROLE_AG },
124     { "hs",      "Sent is HS, Rcvd is AG", ROLE_HS },
125     { NULL, NULL, 0 }
126 };
127 
128 static const unit_name_string units_slash15 = { "/15", NULL };
129 
130 void proto_register_bthsp(void);
131 void proto_reg_handoff_bthsp(void);
132 
get_uint_parameter(guint8 * parameter_stream,gint parameter_length)133 static guint32 get_uint_parameter(guint8 *parameter_stream, gint parameter_length)
134 {
135     guint32      value;
136     gchar *val;
137 
138     val = (guint8 *) wmem_alloc(wmem_packet_scope(), parameter_length + 1);
139     memcpy(val, parameter_stream, parameter_length);
140     val[parameter_length] = '\0';
141     value = (guint32) g_ascii_strtoull(val, NULL, 10);
142 
143     return value;
144 }
145 
check_vgs(gint role,guint16 type)146 static gboolean check_vgs(gint role, guint16 type) {
147     if (role == ROLE_HS && type == TYPE_ACTION) return TRUE;
148     if (role == ROLE_AG && type == TYPE_RESPONSE) return TRUE;
149 
150     return FALSE;
151 }
152 
check_vgm(gint role,guint16 type)153 static gboolean check_vgm(gint role, guint16 type) {
154     if (role == ROLE_HS && type == TYPE_ACTION) return TRUE;
155     if (role == ROLE_AG && type == TYPE_RESPONSE) return TRUE;
156 
157     return FALSE;
158 }
159 
check_ckpd(gint role,guint16 type)160 static gboolean check_ckpd(gint role, guint16 type) {
161     if (role == ROLE_HS && type == TYPE_ACTION) return TRUE;
162 
163     return FALSE;
164 }
165 
check_only_ag_role(gint role,guint16 type)166 static gboolean check_only_ag_role(gint role, guint16 type) {
167     if (role == ROLE_AG && type == TYPE_RESPONSE_ACK) return TRUE;
168 
169     return FALSE;
170 }
171 
172 static gint
dissect_vgs_parameter(tvbuff_t * tvb,packet_info * pinfo,proto_tree * tree,gint offset,gint role,guint16 type,guint8 * parameter_stream,guint parameter_number,gint parameter_length,void ** data _U_)173 dissect_vgs_parameter(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree,
174         gint offset, gint role, guint16 type, guint8 *parameter_stream,
175         guint parameter_number, gint parameter_length, void **data _U_)
176 {
177     proto_item  *pitem;
178     guint32      value;
179 
180     if (!check_vgs(role, type)) return FALSE;
181 
182     if (parameter_number > 0) return FALSE;
183 
184     value = get_uint_parameter(parameter_stream, parameter_length);
185 
186     pitem = proto_tree_add_uint(tree, hf_vgs, tvb, offset, parameter_length, value);
187 
188     if (value > 15) {
189         expert_add_info(pinfo, pitem, &ei_vgs_gain);
190     }
191 
192     return TRUE;
193 }
194 
195 static gint
dissect_vgm_parameter(tvbuff_t * tvb,packet_info * pinfo,proto_tree * tree,gint offset,gint role,guint16 type,guint8 * parameter_stream,guint parameter_number,gint parameter_length,void ** data _U_)196 dissect_vgm_parameter(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree,
197         gint offset, gint role, guint16 type, guint8 *parameter_stream,
198         guint parameter_number, gint parameter_length, void **data _U_)
199 {
200     proto_item  *pitem;
201     guint32      value;
202 
203     if (!check_vgm(role, type)) return FALSE;
204 
205     if (parameter_number > 0) return FALSE;
206 
207     value = get_uint_parameter(parameter_stream, parameter_length);
208 
209     pitem = proto_tree_add_uint(tree, hf_vgm, tvb, offset, parameter_length, value);
210 
211     if (value > 15) {
212         expert_add_info(pinfo, pitem, &ei_vgm_gain);
213     }
214 
215     return TRUE;
216 }
217 
218 static gint
dissect_ckpd_parameter(tvbuff_t * tvb,packet_info * pinfo,proto_tree * tree,gint offset,gint role,guint16 type,guint8 * parameter_stream,guint parameter_number,gint parameter_length,void ** data _U_)219 dissect_ckpd_parameter(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree,
220         gint offset, gint role, guint16 type, guint8 *parameter_stream,
221         guint parameter_number, gint parameter_length, void **data _U_)
222 {
223     proto_item  *pitem;
224     guint32      value;
225 
226     if (!check_ckpd(role, type)) return FALSE;
227 
228 
229     if (parameter_number > 0) return FALSE;
230 
231     value = get_uint_parameter(parameter_stream, parameter_length);
232 
233     pitem = proto_tree_add_uint(tree, hf_ckpd, tvb, offset, parameter_length, value);
234 
235     if (value != 200) {
236         expert_add_info(pinfo, pitem, &ei_ckpd);
237     }
238 
239     return TRUE;
240 }
241 
242 static gint
dissect_no_parameter(tvbuff_t * tvb _U_,packet_info * pinfo _U_,proto_tree * tree _U_,gint offset _U_,gint role _U_,guint16 type _U_,guint8 * parameter_stream _U_,guint parameter_number _U_,gint parameter_length _U_,void ** data _U_)243 dissect_no_parameter(tvbuff_t *tvb _U_, packet_info *pinfo _U_, proto_tree *tree _U_,
244         gint offset _U_, gint role _U_, guint16 type _U_, guint8 *parameter_stream _U_,
245         guint parameter_number _U_, gint parameter_length _U_, void **data _U_)
246 {
247     return FALSE;
248 }
249 
250 static const at_cmd_t at_cmds[] = {
251     { "+VGS",       "Gain of Speaker",                          check_vgs,  dissect_vgs_parameter  },
252     { "+VGM",       "Gain of Microphone",                       check_vgm,  dissect_vgm_parameter  },
253     { "+CKPD",      "Control Keypad",                           check_ckpd, dissect_ckpd_parameter },
254     { "ERROR",      "ERROR",                                    check_only_ag_role, dissect_no_parameter },
255     { "RING",       "Incoming Call Indication",                 check_only_ag_role, dissect_no_parameter },
256     { "OK",         "OK",                                       check_only_ag_role, dissect_no_parameter },
257     { NULL, NULL, NULL, NULL }
258 };
259 
260 
261 static gint
dissect_at_command(tvbuff_t * tvb,packet_info * pinfo,proto_tree * tree,gint offset,guint32 role,gint command_number)262 dissect_at_command(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree,
263         gint offset, guint32 role, gint command_number)
264 {
265     proto_item      *pitem;
266     proto_tree      *command_item;
267     proto_item      *command_tree;
268     proto_tree      *parameters_item = NULL;
269     proto_item      *parameters_tree = NULL;
270     char            *col_str = NULL;
271     char            *at_stream;
272     char            *at_command = NULL;
273     gint             i_char = 0;
274     guint            i_char_fix = 0;
275     gint             length;
276     const at_cmd_t  *i_at_cmd;
277     gint             parameter_length;
278     guint            parameter_number = 0;
279     gint             first_parameter_offset = offset;
280     gint             last_parameter_offset  = offset;
281     guint16          type = TYPE_UNKNOWN;
282     guint32          brackets;
283     gboolean         quotation;
284     gboolean         next;
285     void            *data;
286 
287     length = tvb_reported_length_remaining(tvb, offset);
288     if (length <= 0)
289         return tvb_reported_length(tvb);
290 
291     if (!command_number) {
292         proto_tree_add_item(tree, hf_data, tvb, offset, length, ENC_NA | ENC_ASCII);
293         col_str = (char *) wmem_alloc(wmem_packet_scope(), length + 1);
294         tvb_memcpy(tvb, col_str, offset, length);
295         col_str[length] = '\0';
296     }
297 
298     at_stream = (char *) wmem_alloc(wmem_packet_scope(), length + 1);
299     tvb_memcpy(tvb, at_stream, offset, length);
300     at_stream[length] = '\0';
301     while (at_stream[i_char]) {
302         at_stream[i_char] = g_ascii_toupper(at_stream[i_char]);
303         if (!command_number) {
304             col_str[i_char] = g_ascii_toupper(col_str[i_char]);
305             if (!g_ascii_isgraph(col_str[i_char])) col_str[i_char] = ' ';
306         }
307         i_char += 1;
308     }
309 
310     command_item = proto_tree_add_none_format(tree, hf_command, tvb,
311             offset, 0, "Command %u", command_number);
312     command_tree = proto_item_add_subtree(command_item, ett_bthsp_command);
313 
314     if (!command_number) col_append_str(pinfo->cinfo, COL_INFO, col_str);
315 
316     if (role == ROLE_HS) {
317         if (command_number) {
318             at_command = at_stream;
319             i_char = 0;
320         } else {
321             at_command = g_strstr_len(at_stream, length, "AT");
322 
323             if (at_command) {
324                 i_char = (gint) (at_command - at_stream);
325 
326                 if (i_char) {
327                     proto_tree_add_item(command_tree, hf_at_ignored, tvb, offset,
328                         i_char, ENC_NA | ENC_ASCII);
329                     offset += i_char;
330                 }
331 
332                 proto_tree_add_item(command_tree, hf_at_command_line_prefix,
333                         tvb, offset, 2, ENC_NA | ENC_ASCII);
334                 offset += 2;
335                 i_char += 2;
336                 at_command = at_stream;
337 
338                 at_command += i_char;
339                 length -= i_char;
340                 i_char_fix += i_char;
341                 i_char = 0;
342             }
343         }
344     } else {
345         at_command = at_stream;
346         i_char = 0;
347         while (i_char <= length &&
348                 (at_command[i_char] == '\r' || at_command[i_char] == '\n' ||
349                 at_command[i_char] == ' ' || at_command[i_char] == '\t')) {
350             /* ignore white characters */
351             i_char += 1;
352         }
353 
354         offset += i_char;
355         at_command += i_char;
356         length -= i_char;
357         i_char_fix += i_char;
358         i_char = 0;
359     }
360 
361     if (at_command) {
362 
363         while (i_char < length &&
364                         (at_command[i_char] != '\r' && at_command[i_char] != '=' &&
365                         at_command[i_char] != ';' && at_command[i_char] != '?' &&
366                         at_command[i_char] != ':')) {
367             i_char += 1;
368         }
369 
370         i_at_cmd = at_cmds;
371         if (at_command[0] == '\r') {
372             pitem = proto_tree_add_item(command_tree, hf_at_cmd, tvb, offset - 2,
373                     2, ENC_NA | ENC_ASCII);
374             i_at_cmd = NULL;
375         } else {
376             pitem = NULL;
377             while (i_at_cmd->name) {
378                 if (g_str_has_prefix(&at_command[0], i_at_cmd->name)) {
379                     pitem = proto_tree_add_item(command_tree, hf_at_cmd, tvb, offset,
380                             (gint) strlen(i_at_cmd->name), ENC_NA | ENC_ASCII);
381                     proto_item_append_text(pitem, " (%s)", i_at_cmd->long_name);
382                     break;
383                 }
384                 i_at_cmd += 1;
385             }
386 
387             if (!pitem) {
388                 pitem = proto_tree_add_item(command_tree, hf_at_cmd, tvb, offset,
389                         i_char, ENC_NA | ENC_ASCII);
390             }
391         }
392 
393 
394         if (i_at_cmd && i_at_cmd->name == NULL) {
395             char *name;
396 
397             name = (char *) wmem_alloc(wmem_packet_scope(), i_char + 2);
398             (void) g_strlcpy(name, at_command, i_char + 1);
399             name[i_char + 1] = '\0';
400             proto_item_append_text(command_item, ": %s (Unknown)", name);
401             proto_item_append_text(pitem, " (Unknown - Non-Standard HSP Command)");
402             expert_add_info(pinfo, pitem, &ei_non_mandatory_command);
403         } else if (i_at_cmd == NULL) {
404             proto_item_append_text(command_item, ": AT");
405         } else {
406             proto_item_append_text(command_item, ": %s", i_at_cmd->name);
407         }
408 
409         offset += i_char;
410 
411         if (i_at_cmd && g_strcmp0(i_at_cmd->name, "D")) {
412             if (length >= 2 && at_command[i_char] == '=' && at_command[i_char + 1] == '?') {
413                 type = at_command[i_char] << 8 | at_command[i_char + 1];
414                 proto_tree_add_uint(command_tree, hf_at_cmd_type, tvb, offset, 2, type);
415                 offset += 2;
416                 i_char += 2;
417             } else if (role == ROLE_AG && length >= 2 && at_command[i_char] == '\r' && at_command[i_char + 1] == '\n') {
418                 type = at_command[i_char] << 8 | at_command[i_char + 1];
419                 proto_tree_add_uint(command_tree, hf_at_cmd_type, tvb, offset, 2, type);
420                 offset += 2;
421                 i_char += 2;
422             } else if (length >= 1 && (at_command[i_char] == '=' ||
423                         at_command[i_char] == '\r' ||
424                         at_command[i_char] == ':' ||
425                         at_command[i_char] == '?')) {
426                 type = at_command[i_char];
427                 proto_tree_add_uint(command_tree, hf_at_cmd_type, tvb, offset, 1, type);
428                 offset += 1;
429                 i_char += 1;
430             }
431         }
432 
433         if (i_at_cmd && i_at_cmd->check_command && !i_at_cmd->check_command(role, type)) {
434             expert_add_info(pinfo, command_item, &ei_invalid_usage);
435         }
436 
437         parameters_item = proto_tree_add_none_format(command_tree, hf_parameters, tvb,
438                 offset, 0, "Parameters");
439         parameters_tree = proto_item_add_subtree(parameters_item, ett_bthsp_parameters);
440 
441         data = NULL;
442 
443         while (i_char < length) {
444 
445             while (at_command[i_char] == ' ' || at_command[i_char]  == '\t') {
446                 offset += 1;
447                 i_char += 1;
448             }
449 
450             parameter_length = 0;
451             brackets = 0;
452             quotation = FALSE;
453             next = FALSE;
454 
455             if (at_command[i_char + parameter_length] != '\r') {
456                 while (i_char + parameter_length < length &&
457                         at_command[i_char + parameter_length] != '\r') {
458 
459                     if (at_command[i_char + parameter_length] == ';') {
460                         next = TRUE;
461                         break;
462                     }
463 
464                     if (at_command[i_char + parameter_length] == '"') {
465                         quotation = quotation ? FALSE : TRUE;
466                     }
467 
468                     if (quotation == TRUE) {
469                         parameter_length += 1;
470                         continue;
471                     }
472 
473                     if (at_command[i_char + parameter_length] == '(') {
474                         brackets += 1;
475                     }
476                     if (at_command[i_char + parameter_length] == ')') {
477                         brackets -= 1;
478                     }
479 
480                     if (brackets == 0 && at_command[i_char + parameter_length] == ',') {
481                         break;
482                     }
483 
484                     parameter_length += 1;
485                 }
486 
487 /* TODO: Save bthsp.at_cmd, bthsp.at_cmd.type, frame_time  and frame_num here in
488 
489                 if (role == ROLE_HS && pinfo->fd->visited == 0) {
490 
491     at_cmd_db = wmem_tree_new_autoreset(wmem_epan_scope(), wmem_file_scope());
492 
493     interface_id
494     adapter_id
495     chandle
496     dlci
497 
498     frame_number
499 -------------------
500     at_command
501     at_type
502     frame_num
503     frame_time
504     status
505     first_response_in (if 0 - no response)
506 
507 
508             interface_id = interface_id;
509             adapter_id   = adapter_id;
510             chandle      = chandle;
511             dlci         = dlci;
512             frame_number = pinfo->num;
513 
514 
515             key[0].length = 1;
516             key[0].key = &interface_id;
517             key[1].length = 1;
518             key[1].key = &adapter_id;
519             key[2].length = 1;
520             key[2].key = &chandle;
521             key[3].length = 1;
522             key[3].key = &dlci;
523             key[4].length = 1;
524             key[4].key = &frame_number;
525             key[5].length = 0;
526             key[5].key = NULL;
527 
528             cmd = wmem_new(wmem_file_scope(), at_cmd_entry_t);
529             cmd->interface_id = interface_id;
530             cmd->adapter_id   = adapter_id;
531             cmd->chandle      = chandle;
532             cmd->dlci         = dlci;
533 
534             cmd->frame_number = pinfo->num;
535             cmd->status = STATUS_NO_RESPONSE;
536             cmd->time = pinfo->abs_ts;
537             cmd->at_command
538             cmd->at_type
539             cmd->first_response_in = 0;
540 
541             wmem_tree_insert32_array(at_cmd_db, key, cmd);
542     }
543 
544 */
545 
546                 first_parameter_offset = offset;
547                 if (type == TYPE_ACTION || type == TYPE_RESPONSE) {
548                     if (i_at_cmd && (i_at_cmd->dissect_parameter != NULL &&
549                             !i_at_cmd->dissect_parameter(tvb, pinfo, parameters_tree, offset, role,
550                             type, &at_command[i_char], parameter_number, parameter_length, &data) )) {
551                         pitem = proto_tree_add_item(parameters_tree,
552                                 hf_unknown_parameter, tvb, offset,
553                                 parameter_length, ENC_NA | ENC_ASCII);
554                         expert_add_info(pinfo, pitem, &ei_unknown_parameter);
555                     } else if (i_at_cmd && i_at_cmd->dissect_parameter == NULL) {
556                         proto_tree_add_item(parameters_tree, hf_parameter, tvb, offset,
557                                 parameter_length, ENC_NA | ENC_ASCII);
558                     }
559                 }
560             }
561 
562             if (type != TYPE_ACTION_SIMPLY && type != TYPE_RESPONSE_ACK && type != TYPE_TEST && type != TYPE_READ)
563                 parameter_number += 1;
564             i_char += parameter_length;
565             offset += parameter_length;
566             last_parameter_offset = offset;
567 
568             if (role == ROLE_AG &&
569                     i_char + 1 <= length &&
570                     at_command[i_char] == '\r' &&
571                     at_command[i_char + 1] == '\n') {
572                 offset += 2;
573                 i_char += 2;
574                 break;
575             } else if (at_command[i_char] == ',' ||
576                         at_command[i_char] == '\r' ||
577                         at_command[i_char] == ';') {
578                     i_char += 1;
579                     offset += 1;
580             }
581 
582             if (next) break;
583         }
584 
585         i_char += i_char_fix;
586         proto_item_set_len(command_item, i_char);
587     } else {
588         length = tvb_reported_length_remaining(tvb, offset);
589         if (length < 0)
590             length = 0;
591         proto_item_set_len(command_item, length);
592         offset += length;
593     }
594 
595     if (parameter_number > 0 && last_parameter_offset - first_parameter_offset > 0)
596         proto_item_set_len(parameters_item, last_parameter_offset - first_parameter_offset);
597     else
598         proto_item_append_text(parameters_item, ": No");
599 
600     if (role == ROLE_AG) {
601         guint command_frame_number = 0;
602 
603         if (command_frame_number) {
604             pitem = proto_tree_add_uint(command_tree, hf_command_in, tvb, offset,
605                     0, command_frame_number);
606             proto_item_set_generated(pitem);
607         } else {
608             pitem = proto_tree_add_item(command_tree, hf_unsolicited, tvb, offset, 0, ENC_NA);
609             proto_item_set_generated(pitem);
610         }
611     }
612 
613     return offset;
614 }
615 
616 static gint
dissect_bthsp(tvbuff_t * tvb,packet_info * pinfo,proto_tree * tree,void * data)617 dissect_bthsp(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, void *data)
618 {
619     proto_item       *main_item;
620     proto_tree       *main_tree;
621     proto_item       *pitem;
622     gint              offset = 0;
623     guint32           role = ROLE_UNKNOWN;
624     wmem_tree_key_t   key[10];
625     guint32           interface_id;
626     guint32           adapter_id;
627     guint32           chandle;
628     guint32           dlci;
629     guint32           frame_number;
630     guint32           direction;
631     guint32           bd_addr_oui;
632     guint32           bd_addr_id;
633     fragment_t       *fragment;
634     fragment_t       *previous_fragment;
635     fragment_t       *i_fragment;
636     guint8           *at_stream;
637     gint              length;
638     gint              command_number;
639     gint              i_length;
640     tvbuff_t         *reassembled_tvb = NULL;
641     guint             reassemble_start_offset = 0;
642     guint             reassemble_end_offset   = 0;
643     gint              previous_proto;
644 
645     previous_proto = (GPOINTER_TO_INT(wmem_list_frame_data(wmem_list_frame_prev(wmem_list_tail(pinfo->layers)))));
646     if (data && previous_proto == proto_btrfcomm) {
647         btrfcomm_data_t  *rfcomm_data;
648 
649         rfcomm_data = (btrfcomm_data_t *) data;
650 
651         interface_id = rfcomm_data->interface_id;
652         adapter_id   = rfcomm_data->adapter_id;
653         chandle      = rfcomm_data->chandle;
654         dlci         = rfcomm_data->dlci;
655         direction    = (rfcomm_data->is_local_psm) ? P2P_DIR_SENT : P2P_DIR_RECV;
656 
657         if (direction == P2P_DIR_RECV) {
658             bd_addr_oui     = rfcomm_data->remote_bd_addr_oui;
659             bd_addr_id      = rfcomm_data->remote_bd_addr_id;
660         } else {
661             bd_addr_oui     = 0;
662             bd_addr_id      = 0;
663         }
664     } else {
665         interface_id = HCI_INTERFACE_DEFAULT;
666         adapter_id   = HCI_ADAPTER_DEFAULT;
667         chandle      = 0;
668         dlci         = 0;
669         direction    = P2P_DIR_UNKNOWN;
670 
671         bd_addr_oui     = 0;
672         bd_addr_id      = 0;
673     }
674 
675     main_item = proto_tree_add_item(tree, proto_bthsp, tvb, 0, tvb_captured_length(tvb), ENC_NA);
676     main_tree = proto_item_add_subtree(main_item, ett_bthsp);
677 
678     col_set_str(pinfo->cinfo, COL_PROTOCOL, "HSP");
679 
680     switch (pinfo->p2p_dir) {
681         case P2P_DIR_SENT:
682             col_set_str(pinfo->cinfo, COL_INFO, "Sent ");
683             break;
684         case P2P_DIR_RECV:
685             col_set_str(pinfo->cinfo, COL_INFO, "Rcvd ");
686             break;
687         default:
688             col_set_str(pinfo->cinfo, COL_INFO, "UnknownDirection ");
689             break;
690     }
691 
692     if ((hsp_role == ROLE_AG && pinfo->p2p_dir == P2P_DIR_SENT) ||
693             (hsp_role == ROLE_HS && pinfo->p2p_dir == P2P_DIR_RECV)) {
694         role = ROLE_AG;
695     } else if (hsp_role != ROLE_UNKNOWN) {
696         role = ROLE_HS;
697     }
698 
699     if (role == ROLE_UNKNOWN) {
700         guint32          sdp_psm;
701         guint32          service_type;
702         guint32          service_channel;
703         service_info_t  *service_info;
704 
705         sdp_psm         = SDP_PSM_DEFAULT;
706 
707         service_type    = BTSDP_RFCOMM_PROTOCOL_UUID;
708         service_channel = dlci >> 1;
709         frame_number    = pinfo->num;
710 
711         key[0].length = 1;
712         key[0].key = &interface_id;
713         key[1].length = 1;
714         key[1].key = &adapter_id;
715         key[2].length = 1;
716         key[2].key = &sdp_psm;
717         key[3].length = 1;
718         key[3].key = &direction;
719         key[4].length = 1;
720         key[4].key = &bd_addr_oui;
721         key[5].length = 1;
722         key[5].key = &bd_addr_id;
723         key[6].length = 1;
724         key[6].key = &service_type;
725         key[7].length = 1;
726         key[7].key = &service_channel;
727         key[8].length = 1;
728         key[8].key = &frame_number;
729         key[9].length = 0;
730         key[9].key = NULL;
731 
732         service_info = btsdp_get_service_info(key);
733         if (service_info && service_info->interface_id == interface_id &&
734                 service_info->adapter_id == adapter_id &&
735                 service_info->sdp_psm == SDP_PSM_DEFAULT &&
736                 ((service_info->direction == P2P_DIR_RECV &&
737                 service_info->bd_addr_oui == bd_addr_oui &&
738                 service_info->bd_addr_id == bd_addr_id) ||
739                 (service_info->direction != P2P_DIR_RECV &&
740                 service_info->bd_addr_oui == 0 &&
741                 service_info->bd_addr_id == 0)) &&
742                 service_info->type == BTSDP_RFCOMM_PROTOCOL_UUID &&
743                 service_info->channel == (dlci >> 1)) {
744             if ((service_info->uuid.bt_uuid == BTSDP_HSP_GW_SERVICE_UUID && service_info->direction == P2P_DIR_RECV && pinfo->p2p_dir == P2P_DIR_SENT) ||
745                 (service_info->uuid.bt_uuid == BTSDP_HSP_GW_SERVICE_UUID && service_info->direction == P2P_DIR_SENT && pinfo->p2p_dir == P2P_DIR_RECV) ||
746                 ((service_info->uuid.bt_uuid == BTSDP_HSP_SERVICE_UUID || service_info->uuid.bt_uuid == BTSDP_HSP_HS_SERVICE_UUID) && service_info->direction == P2P_DIR_RECV && pinfo->p2p_dir == P2P_DIR_RECV) ||
747                 ((service_info->uuid.bt_uuid == BTSDP_HSP_SERVICE_UUID || service_info->uuid.bt_uuid == BTSDP_HSP_HS_SERVICE_UUID) && service_info->direction == P2P_DIR_SENT && pinfo->p2p_dir == P2P_DIR_SENT)) {
748                 role = ROLE_HS;
749             } else {
750                 role = ROLE_AG;
751             }
752         }
753     }
754 
755     pitem = proto_tree_add_uint(main_tree, hf_role, tvb, 0, 0, role);
756     proto_item_set_generated(pitem);
757 
758     if (role == ROLE_UNKNOWN) {
759         col_append_fstr(pinfo->cinfo, COL_INFO, "Data: %s",
760                 tvb_format_text(pinfo->pool, tvb, 0, tvb_reported_length(tvb)));
761         proto_tree_add_item(main_tree, hf_data, tvb, 0, tvb_captured_length(tvb), ENC_NA | ENC_ASCII);
762         return tvb_reported_length(tvb);
763     }
764 
765     /* save fragments */
766     if (!pinfo->fd->visited) {
767         frame_number = pinfo->num - 1;
768 
769         key[0].length = 1;
770         key[0].key = &interface_id;
771         key[1].length = 1;
772         key[1].key = &adapter_id;
773         key[2].length = 1;
774         key[2].key = &chandle;
775         key[3].length = 1;
776         key[3].key = &dlci;
777         key[4].length = 1;
778         key[4].key = &role;
779         key[5].length = 1;
780         key[5].key = &frame_number;
781         key[6].length = 0;
782         key[6].key = NULL;
783 
784         previous_fragment = (fragment_t *) wmem_tree_lookup32_array_le(fragments, key);
785         if (!(previous_fragment && previous_fragment->interface_id == interface_id &&
786                 previous_fragment->adapter_id == adapter_id &&
787                 previous_fragment->chandle == chandle &&
788                 previous_fragment->dlci == dlci &&
789                 previous_fragment->role == role &&
790                 previous_fragment->reassemble_state != REASSEMBLE_DONE)) {
791             previous_fragment = NULL;
792         }
793 
794         frame_number = pinfo->num;
795 
796         key[0].length = 1;
797         key[0].key = &interface_id;
798         key[1].length = 1;
799         key[1].key = &adapter_id;
800         key[2].length = 1;
801         key[2].key = &chandle;
802         key[3].length = 1;
803         key[3].key = &dlci;
804         key[4].length = 1;
805         key[4].key = &role;
806         key[5].length = 1;
807         key[5].key = &frame_number;
808         key[6].length = 0;
809         key[6].key = NULL;
810 
811         fragment = wmem_new(wmem_file_scope(), fragment_t);
812         fragment->interface_id      = interface_id;
813         fragment->adapter_id        = adapter_id;
814         fragment->chandle           = chandle;
815         fragment->dlci              = dlci;
816         fragment->role              = role;
817         fragment->idx               = previous_fragment ? previous_fragment->idx + previous_fragment->length : 0;
818         fragment->reassemble_state  = REASSEMBLE_FRAGMENT;
819         fragment->length            = tvb_reported_length(tvb);
820         fragment->data              = (guint8 *) wmem_alloc(wmem_file_scope(), fragment->length);
821         fragment->previous_fragment = previous_fragment;
822         tvb_memcpy(tvb, fragment->data, offset, fragment->length);
823 
824         wmem_tree_insert32_array(fragments, key, fragment);
825 
826         /* Detect reassemble end character: \r for HS or \n for AG */
827         length = tvb_reported_length(tvb);
828         at_stream = tvb_get_string_enc(wmem_packet_scope(), tvb, 0, length, ENC_ASCII);
829 
830         reassemble_start_offset = 0;
831 
832         for (i_length = 0; i_length < length; i_length += 1) {
833             if (!((role == ROLE_HS && at_stream[i_length] == '\r') ||
834                     (role == ROLE_AG && at_stream[i_length] == '\n'))) {
835                 continue;
836             }
837 
838             if (role == ROLE_HS && at_stream[i_length] == '\r') {
839                 reassemble_start_offset = i_length + 1;
840                 if (reassemble_end_offset == 0) reassemble_end_offset = i_length + 1;
841             }
842 
843             if (role == ROLE_AG && at_stream[i_length] == '\n') {
844                 reassemble_start_offset = i_length + 1;
845             }
846 
847             frame_number = pinfo->num;
848 
849             key[0].length = 1;
850             key[0].key = &interface_id;
851             key[1].length = 1;
852             key[1].key = &adapter_id;
853             key[2].length = 1;
854             key[2].key = &chandle;
855             key[3].length = 1;
856             key[3].key = &dlci;
857             key[4].length = 1;
858             key[4].key = &role;
859             key[5].length = 1;
860             key[5].key = &frame_number;
861             key[6].length = 0;
862             key[6].key = NULL;
863 
864             fragment = (fragment_t *) wmem_tree_lookup32_array_le(fragments, key);
865             if (fragment && fragment->interface_id == interface_id &&
866                     fragment->adapter_id == adapter_id &&
867                     fragment->chandle == chandle &&
868                     fragment->dlci == dlci &&
869                     fragment->role == role) {
870                 i_fragment = fragment;
871                 while (i_fragment && i_fragment->idx > 0) {
872                     i_fragment = i_fragment->previous_fragment;
873                 }
874 
875                 if (i_length + 1 == length &&
876                         role == ROLE_HS &&
877                         at_stream[i_length] == '\r') {
878                     fragment->reassemble_state = REASSEMBLE_DONE;
879                 } else if (i_length + 1 == length &&
880                         role == ROLE_AG &&
881                         i_length >= 4 &&
882                         at_stream[i_length] == '\n' &&
883                         at_stream[i_length - 1] == '\r' &&
884                         at_stream[0] == '\r' &&
885                         at_stream[1] == '\n') {
886                     fragment->reassemble_state = REASSEMBLE_DONE;
887                 } else if (i_length + 1 == length &&
888                         role == ROLE_AG &&
889                         i_length >= 2 &&
890                         at_stream[i_length] == '\n' &&
891                         at_stream[i_length - 1] == '\r' &&
892                         i_fragment &&
893                         i_fragment->reassemble_state == REASSEMBLE_FRAGMENT &&
894                         i_fragment->length >= 2 &&
895                         i_fragment->data[0] == '\r' &&
896                         i_fragment->data[1] == '\n') {
897                     fragment->reassemble_state = REASSEMBLE_DONE;
898                 } else if (role == ROLE_HS) {
899 /* XXX: Temporary disable reassembling of partial message, it seems to be broken */
900 /*                    fragment->reassemble_state = REASSEMBLE_PARTIALLY;*/
901                 }
902                 fragment->reassemble_start_offset = reassemble_start_offset;
903                 fragment->reassemble_end_offset = reassemble_end_offset;
904             }
905         }
906     }
907 
908     /* recover reassembled payload */
909     frame_number = pinfo->num;
910 
911     key[0].length = 1;
912     key[0].key = &interface_id;
913     key[1].length = 1;
914     key[1].key = &adapter_id;
915     key[2].length = 1;
916     key[2].key = &chandle;
917     key[3].length = 1;
918     key[3].key = &dlci;
919     key[4].length = 1;
920     key[4].key = &role;
921     key[5].length = 1;
922     key[5].key = &frame_number;
923     key[6].length = 0;
924     key[6].key = NULL;
925 
926     fragment = (fragment_t *) wmem_tree_lookup32_array_le(fragments, key);
927     if (fragment && fragment->interface_id == interface_id &&
928             fragment->adapter_id == adapter_id &&
929             fragment->chandle == chandle &&
930             fragment->dlci == dlci &&
931             fragment->role == role &&
932             fragment->reassemble_state != REASSEMBLE_FRAGMENT) {
933         guint8    *at_data;
934         guint      i_data_offset;
935 
936         i_data_offset = fragment->idx + fragment->length;
937         at_data = (guint8 *) wmem_alloc(pinfo->pool, fragment->idx + fragment->length);
938 
939         i_fragment = fragment;
940 
941         if (i_fragment && i_fragment->reassemble_state == REASSEMBLE_PARTIALLY) {
942             i_data_offset -= i_fragment->reassemble_end_offset;
943             memcpy(at_data + i_data_offset, i_fragment->data, i_fragment->reassemble_end_offset);
944             i_fragment = i_fragment->previous_fragment;
945         }
946 
947         if (i_fragment) {
948             while (i_fragment && i_fragment->idx > 0) {
949                 i_data_offset -= i_fragment->length;
950                 memcpy(at_data + i_data_offset, i_fragment->data, i_fragment->length);
951                 i_fragment = i_fragment->previous_fragment;
952             }
953 
954             if (i_fragment && i_fragment->reassemble_state == REASSEMBLE_PARTIALLY) {
955                 i_data_offset -= (i_fragment->length - i_fragment->reassemble_start_offset);
956                 memcpy(at_data + i_data_offset, i_fragment->data + i_fragment->reassemble_start_offset,
957                         i_fragment->length - i_fragment->reassemble_start_offset);
958             } else if (i_fragment) {
959                 i_data_offset -= i_fragment->length;
960                 memcpy(at_data + i_data_offset, i_fragment->data, i_fragment->length);
961             }
962         }
963 
964         if (fragment->idx > 0 && fragment->length > 0) {
965             proto_tree_add_item(main_tree, hf_fragment, tvb, offset,
966                     tvb_captured_length_remaining(tvb, offset), ENC_ASCII | ENC_NA);
967             reassembled_tvb = tvb_new_child_real_data(tvb, at_data,
968                     fragment->idx + fragment->length, fragment->idx + fragment->length);
969             add_new_data_source(pinfo, reassembled_tvb, "Reassembled HSP");
970         }
971 
972         command_number = 0;
973         if (reassembled_tvb) {
974             guint reassembled_offset = 0;
975 
976             while (tvb_reported_length(reassembled_tvb) > reassembled_offset) {
977                 reassembled_offset = dissect_at_command(reassembled_tvb,
978                         pinfo, main_tree, reassembled_offset, role, command_number);
979                 command_number += 1;
980             }
981             offset = tvb_captured_length(tvb);
982         } else {
983             while (tvb_reported_length(tvb) > (guint) offset) {
984                 offset = dissect_at_command(tvb, pinfo, main_tree, offset, role, command_number);
985                 command_number += 1;
986             }
987         }
988     } else {
989         col_append_fstr(pinfo->cinfo, COL_INFO, "Fragment: %s",
990                 tvb_format_text_wsp(wmem_packet_scope(), tvb, offset, tvb_captured_length_remaining(tvb, offset)));
991         pitem = proto_tree_add_item(main_tree, hf_fragmented, tvb, 0, 0, ENC_NA);
992         proto_item_set_generated(pitem);
993         proto_tree_add_item(main_tree, hf_fragment, tvb, offset,
994                 tvb_captured_length_remaining(tvb, offset), ENC_ASCII | ENC_NA);
995         offset = tvb_captured_length(tvb);
996     }
997 
998     return offset;
999 }
1000 
1001 void
proto_register_bthsp(void)1002 proto_register_bthsp(void)
1003 {
1004     module_t         *module;
1005     expert_module_t  *expert_bthsp;
1006 
1007     static hf_register_info hf[] = {
1008         { &hf_command,
1009            { "Command",                          "bthsp.command",
1010            FT_NONE, BASE_NONE, NULL, 0,
1011            NULL, HFILL}
1012         },
1013         { &hf_parameters,
1014            { "Parameters",                       "bthsp.parameters",
1015            FT_NONE, BASE_NONE, NULL, 0,
1016            NULL, HFILL}
1017         },
1018         { &hf_command_in,
1019            { "Command frame number in",          "bthsp.command_in",
1020            FT_FRAMENUM, BASE_NONE, NULL, 0,
1021            NULL, HFILL}
1022         },
1023         { &hf_unsolicited,
1024            { "Unsolicited",                      "bthsp.unsolicited",
1025            FT_NONE, BASE_NONE, NULL, 0,
1026            NULL, HFILL}
1027         },
1028         { &hf_data,
1029            { "AT Stream",                        "bthsp.data",
1030            FT_STRING, BASE_NONE, NULL, 0,
1031            NULL, HFILL}
1032         },
1033         { &hf_fragment,
1034            { "Fragment",                         "bthsp.fragment",
1035            FT_STRING, BASE_NONE, NULL, 0,
1036            NULL, HFILL}
1037         },
1038         { &hf_fragmented,
1039            { "Fragmented",                       "bthsp.fragmented",
1040            FT_NONE, BASE_NONE, NULL, 0,
1041            NULL, HFILL}
1042         },
1043         { &hf_at_ignored,
1044            { "Ignored",                          "bthsp.ignored",
1045            FT_BYTES, BASE_NONE, NULL, 0,
1046            NULL, HFILL}
1047         },
1048         { &hf_at_cmd,
1049            { "Command",                          "bthsp.at_cmd",
1050            FT_STRING, BASE_NONE, NULL, 0,
1051            NULL, HFILL}
1052         },
1053         { &hf_at_cmd_type,
1054            { "Type",                             "bthsp.at_cmd.type",
1055            FT_UINT16, BASE_HEX, VALS(at_cmd_type_vals), 0,
1056            NULL, HFILL}
1057         },
1058         { &hf_at_command_line_prefix,
1059            { "Command Line Prefix",              "bthsp.command_line_prefix",
1060            FT_STRING, BASE_NONE, NULL, 0,
1061            NULL, HFILL}
1062         },
1063         { &hf_parameter,
1064            { "Parameter",                        "bthsp.parameter",
1065            FT_STRING, BASE_NONE, NULL, 0,
1066            NULL, HFILL}
1067         },
1068         { &hf_unknown_parameter,
1069            { "Unknown Parameter",                "bthsp.unknown_parameter",
1070            FT_STRING, BASE_NONE, NULL, 0,
1071            NULL, HFILL}
1072         },
1073         { &hf_role,
1074            { "Role",                             "bthsp.role",
1075            FT_UINT8, BASE_DEC, VALS(role_vals), 0,
1076            NULL, HFILL}
1077         },
1078         { &hf_vgs,
1079            { "Gain",                             "bthsp.vgs",
1080            FT_UINT8, BASE_DEC|BASE_UNIT_STRING, &units_slash15, 0,
1081            NULL, HFILL}
1082         },
1083         { &hf_vgm,
1084            { "Gain",                             "bthsp.vgm",
1085            FT_UINT8, BASE_DEC|BASE_UNIT_STRING, &units_slash15, 0,
1086            NULL, HFILL}
1087         },
1088         { &hf_ckpd,
1089            { "Key",                             "bthsp.ckpd",
1090            FT_UINT8, BASE_DEC, NULL, 0,
1091            NULL, HFILL}
1092         }
1093     };
1094 
1095     static ei_register_info ei[] = {
1096         { &ei_non_mandatory_command, { "bthsp.expert.non_mandatory_command", PI_PROTOCOL, PI_NOTE, "Non-mandatory command in HSP", EXPFILL }},
1097         { &ei_invalid_usage,         { "bthsp.expert.invalid_usage", PI_PROTOCOL, PI_WARN, "Non mandatory type or command in this role", EXPFILL }},
1098         { &ei_unknown_parameter,     { "bthsp.expert.unknown_parameter", PI_PROTOCOL, PI_WARN, "Unknown parameter", EXPFILL }},
1099         { &ei_vgm_gain,              { "bthsp.expert.vgm", PI_PROTOCOL, PI_WARN, "Gain of microphone exceeds range 0-15", EXPFILL }},
1100         { &ei_vgs_gain,              { "bthsp.expert.vgs", PI_PROTOCOL, PI_WARN, "Gain of speaker exceeds range 0-15", EXPFILL }},
1101         { &ei_ckpd,              { "bthsp.expert.ckpd", PI_PROTOCOL, PI_WARN, "Only key 200 is covered in HSP", EXPFILL }}    };
1102 
1103     static gint *ett[] = {
1104         &ett_bthsp,
1105         &ett_bthsp_command,
1106         &ett_bthsp_parameters
1107     };
1108 
1109     fragments = wmem_tree_new_autoreset(wmem_epan_scope(), wmem_file_scope());
1110 
1111     proto_bthsp = proto_register_protocol("Bluetooth HSP Profile", "BT HSP", "bthsp");
1112     bthsp_handle = register_dissector("bthsp", dissect_bthsp, proto_bthsp);
1113 
1114     proto_register_field_array(proto_bthsp, hf, array_length(hf));
1115     proto_register_subtree_array(ett, array_length(ett));
1116 
1117     module = prefs_register_protocol_subtree("Bluetooth", proto_bthsp, NULL);
1118     prefs_register_static_text_preference(module, "hsp.version",
1119             "Bluetooth Profile HSP version: 1.2",
1120             "Version of profile supported by this dissector.");
1121 
1122     prefs_register_enum_preference(module, "hsp.hsp_role",
1123             "Force treat packets as AG or HS role",
1124             "Force treat packets as AG or HS role",
1125             &hsp_role, pref_hsp_role, TRUE);
1126 
1127     expert_bthsp = expert_register_protocol(proto_bthsp);
1128     expert_register_field_array(expert_bthsp, ei, array_length(ei));
1129 }
1130 
1131 void
proto_reg_handoff_bthsp(void)1132 proto_reg_handoff_bthsp(void)
1133 {
1134     dissector_add_string("bluetooth.uuid",  "1108", bthsp_handle);
1135     dissector_add_string("bluetooth.uuid",  "1112", bthsp_handle);
1136     dissector_add_string("bluetooth.uuid",  "1131", bthsp_handle);
1137 
1138     dissector_add_for_decode_as("btrfcomm.dlci", bthsp_handle);
1139 }
1140 
1141 /*
1142  * Editor modelines  -  https://www.wireshark.org/tools/modelines.html
1143  *
1144  * Local variables:
1145  * c-basic-offset: 4
1146  * tab-width: 8
1147  * indent-tabs-mode: nil
1148  * End:
1149  *
1150  * vi: set shiftwidth=4 tabstop=8 expandtab:
1151  * :indentSize=4:tabSize=8:noTabs=true:
1152  */
1153