Skip to content

Commit 2d82c2c

Browse files
esyrldv-alt
andcommitted
sockaddr: decode Bluetooth L2 CID values
* xlat/bluetooth_l2_cid.in: New file. * sockaddr.c: Include xlat/bluetooth_l2_cid.h. (print_bluetooth_l2_cid): New function. (print_sockaddr_data_bt): Use it to decode struct sockaddr_l2.l2_cid field. * tests/net-sockaddr.c (check_l2): Update expected output. Co-Authored-by: Dmitry V. Levin <[email protected]>
1 parent 0e72230 commit 2d82c2c

File tree

3 files changed

+61
-11
lines changed

3 files changed

+61
-11
lines changed

sockaddr.c

Lines changed: 39 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@
5555
#include "xlat/af_packet_types.h"
5656

5757
#include "xlat/bdaddr_types.h"
58+
#include "xlat/bluetooth_l2_cid.h"
5859
#include "xlat/bluetooth_l2_psm.h"
5960
#include "xlat/hci_channels.h"
6061

@@ -306,6 +307,40 @@ print_bluetooth_l2_psm(const char *prefix, uint16_t psm)
306307
tprints(")");
307308
}
308309

310+
static void
311+
print_bluetooth_l2_cid(const char *prefix, uint16_t cid)
312+
{
313+
const uint16_t cid_he = btohs(cid);
314+
const char *cid_name = xlookup(bluetooth_l2_cid, cid_he);
315+
const bool cid_str = cid_name || (cid_he >= L2CAP_CID_DYN_START);
316+
317+
tprintf("%shtobs(", prefix);
318+
319+
if (xlat_verbose(xlat_verbosity) != XLAT_STYLE_ABBREV || !cid_str)
320+
tprintf("%#x", cid_he);
321+
322+
if (xlat_verbose(xlat_verbosity) == XLAT_STYLE_RAW)
323+
goto print_bluetooth_l2_cid_end;
324+
325+
if (xlat_verbose(xlat_verbosity) == XLAT_STYLE_VERBOSE || !cid_str)
326+
tprints(" /* ");
327+
328+
if (cid_name) {
329+
tprints(cid_name);
330+
} else if (cid_he >= L2CAP_CID_DYN_START) {
331+
print_xlat(L2CAP_CID_DYN_START);
332+
tprintf(" + %u", cid_he - L2CAP_CID_DYN_START);
333+
} else {
334+
tprints("L2CAP_CID_???");
335+
}
336+
337+
if (xlat_verbose(xlat_verbosity) == XLAT_STYLE_VERBOSE || !cid_str)
338+
tprints(" */");
339+
340+
print_bluetooth_l2_cid_end:
341+
tprints(")");
342+
}
343+
309344
static void
310345
print_sockaddr_data_bt(const void *const buf, const int addrlen)
311346
{
@@ -368,12 +403,12 @@ print_sockaddr_data_bt(const void *const buf, const int addrlen)
368403
case sizeof(struct sockaddr_l2): {
369404
const struct sockaddr_l2 *const l2 = buf;
370405
print_bluetooth_l2_psm("l2_psm=", l2->l2_psm);
371-
tprintf(", l2_bdaddr=%02x:%02x:%02x:%02x:%02x:%02x"
372-
", l2_cid=htobs(%hu), l2_bdaddr_type=",
406+
tprintf(", l2_bdaddr=%02x:%02x:%02x:%02x:%02x:%02x",
373407
l2->l2_bdaddr.b[0], l2->l2_bdaddr.b[1],
374408
l2->l2_bdaddr.b[2], l2->l2_bdaddr.b[3],
375-
l2->l2_bdaddr.b[4], l2->l2_bdaddr.b[5],
376-
btohs(l2->l2_cid));
409+
l2->l2_bdaddr.b[4], l2->l2_bdaddr.b[5]);
410+
print_bluetooth_l2_cid(", l2_cid=", l2->l2_cid);
411+
tprints(", l2_bdaddr_type=");
377412
printxval_index(bdaddr_types, l2->l2_bdaddr_type,
378413
"BDADDR_???");
379414
break;

tests/net-sockaddr.c

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -450,40 +450,44 @@ check_l2(void)
450450
printf("connect(-1, {sa_family=AF_BLUETOOTH"
451451
", l2_psm=htobs(L2CAP_PSM_DYN_START + %hu)"
452452
", l2_bdaddr=%02x:%02x:%02x:%02x:%02x:%02x"
453-
", l2_cid=htobs(%hu), l2_bdaddr_type=0xce /* BDADDR_??? */}"
453+
", l2_cid=htobs(L2CAP_CID_DYN_START + %hu)"
454+
", l2_bdaddr_type=0xce /* BDADDR_??? */}"
454455
", %u) = %d EBADF (%m)\n", h_psm - 0x1001,
455456
c_l2.l2_bdaddr.b[0], c_l2.l2_bdaddr.b[1],
456457
c_l2.l2_bdaddr.b[2], c_l2.l2_bdaddr.b[3],
457458
c_l2.l2_bdaddr.b[4], c_l2.l2_bdaddr.b[5],
458-
h_cid, len, ret);
459+
h_cid - 0x40, len, ret);
459460

460461
c_l2.l2_psm = htobs(1);
462+
c_l2.l2_cid = htobs(1);
461463
c_l2.l2_bdaddr_type = BDADDR_LE_RANDOM;
462464
memcpy(l2, &c_l2, sizeof(c_l2));
463465
ret = connect(-1, l2, len);
464466
printf("connect(-1, {sa_family=AF_BLUETOOTH"
465467
", l2_psm=htobs(L2CAP_PSM_SDP)"
466468
", l2_bdaddr=%02x:%02x:%02x:%02x:%02x:%02x"
467-
", l2_cid=htobs(%hu), l2_bdaddr_type=BDADDR_LE_RANDOM}"
468-
", %u) = %d EBADF (%m)\n",
469+
", l2_cid=htobs(L2CAP_CID_SIGNALING)"
470+
", l2_bdaddr_type=BDADDR_LE_RANDOM}, %u) = %d EBADF (%m)\n",
469471
c_l2.l2_bdaddr.b[0], c_l2.l2_bdaddr.b[1],
470472
c_l2.l2_bdaddr.b[2], c_l2.l2_bdaddr.b[3],
471473
c_l2.l2_bdaddr.b[4], c_l2.l2_bdaddr.b[5],
472-
h_cid, len, ret);
474+
len, ret);
473475

474476
c_l2.l2_psm = htobs(0xbad);
477+
c_l2.l2_cid = htobs(8);
475478
c_l2.l2_bdaddr_type = 3;
476479
memcpy(l2, &c_l2, sizeof(c_l2));
477480
ret = connect(-1, l2, len);
478481
printf("connect(-1, {sa_family=AF_BLUETOOTH"
479482
", l2_psm=htobs(0xbad /* L2CAP_PSM_??? */)"
480483
", l2_bdaddr=%02x:%02x:%02x:%02x:%02x:%02x"
481-
", l2_cid=htobs(%hu), l2_bdaddr_type=0x3 /* BDADDR_??? */}"
484+
", l2_cid=htobs(0x8 /* L2CAP_CID_??? */)"
485+
", l2_bdaddr_type=0x3 /* BDADDR_??? */}"
482486
", %u) = %d EBADF (%m)\n",
483487
c_l2.l2_bdaddr.b[0], c_l2.l2_bdaddr.b[1],
484488
c_l2.l2_bdaddr.b[2], c_l2.l2_bdaddr.b[3],
485489
c_l2.l2_bdaddr.b[4], c_l2.l2_bdaddr.b[5],
486-
h_cid, len, ret);
490+
len, ret);
487491
}
488492
#endif
489493

xlat/bluetooth_l2_cid.in

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
/* sort -k2,2 */
2+
L2CAP_CID_SIGNALING 0x0001
3+
L2CAP_CID_CONN_LESS 0x0002
4+
L2CAP_CID_A2MP 0x0003
5+
L2CAP_CID_ATT 0x0004
6+
L2CAP_CID_LE_SIGNALING 0x0005
7+
L2CAP_CID_SMP 0x0006
8+
L2CAP_CID_SMP_BREDR 0x0007
9+
L2CAP_CID_DYN_START 0x0040
10+
L2CAP_CID_LE_DYN_END 0x007f
11+
L2CAP_CID_DYN_END 0xffff

0 commit comments

Comments
 (0)