From: Eugene Syromyatnikov Date: Sun, 6 May 2018 19:24:32 +0000 (+0200) Subject: sockaddr: decode Bluetooth L2 CID values X-Git-Tag: v4.23~157 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=2d82c2c50a3e087f94a7aef3511fac3543783700;p=strace 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 --- diff --git a/sockaddr.c b/sockaddr.c index 785e651f..ea07c0b0 100644 --- a/sockaddr.c +++ b/sockaddr.c @@ -55,6 +55,7 @@ #include "xlat/af_packet_types.h" #include "xlat/bdaddr_types.h" +#include "xlat/bluetooth_l2_cid.h" #include "xlat/bluetooth_l2_psm.h" #include "xlat/hci_channels.h" @@ -306,6 +307,40 @@ print_bluetooth_l2_psm_end: tprints(")"); } +static void +print_bluetooth_l2_cid(const char *prefix, uint16_t cid) +{ + const uint16_t cid_he = btohs(cid); + const char *cid_name = xlookup(bluetooth_l2_cid, cid_he); + const bool cid_str = cid_name || (cid_he >= L2CAP_CID_DYN_START); + + tprintf("%shtobs(", prefix); + + if (xlat_verbose(xlat_verbosity) != XLAT_STYLE_ABBREV || !cid_str) + tprintf("%#x", cid_he); + + if (xlat_verbose(xlat_verbosity) == XLAT_STYLE_RAW) + goto print_bluetooth_l2_cid_end; + + if (xlat_verbose(xlat_verbosity) == XLAT_STYLE_VERBOSE || !cid_str) + tprints(" /* "); + + if (cid_name) { + tprints(cid_name); + } else if (cid_he >= L2CAP_CID_DYN_START) { + print_xlat(L2CAP_CID_DYN_START); + tprintf(" + %u", cid_he - L2CAP_CID_DYN_START); + } else { + tprints("L2CAP_CID_???"); + } + + if (xlat_verbose(xlat_verbosity) == XLAT_STYLE_VERBOSE || !cid_str) + tprints(" */"); + +print_bluetooth_l2_cid_end: + tprints(")"); +} + static void print_sockaddr_data_bt(const void *const buf, const int addrlen) { @@ -368,12 +403,12 @@ print_sockaddr_data_bt(const void *const buf, const int addrlen) case sizeof(struct sockaddr_l2): { const struct sockaddr_l2 *const l2 = buf; print_bluetooth_l2_psm("l2_psm=", l2->l2_psm); - tprintf(", l2_bdaddr=%02x:%02x:%02x:%02x:%02x:%02x" - ", l2_cid=htobs(%hu), l2_bdaddr_type=", + tprintf(", l2_bdaddr=%02x:%02x:%02x:%02x:%02x:%02x", l2->l2_bdaddr.b[0], l2->l2_bdaddr.b[1], l2->l2_bdaddr.b[2], l2->l2_bdaddr.b[3], - l2->l2_bdaddr.b[4], l2->l2_bdaddr.b[5], - btohs(l2->l2_cid)); + l2->l2_bdaddr.b[4], l2->l2_bdaddr.b[5]); + print_bluetooth_l2_cid(", l2_cid=", l2->l2_cid); + tprints(", l2_bdaddr_type="); printxval_index(bdaddr_types, l2->l2_bdaddr_type, "BDADDR_???"); break; diff --git a/tests/net-sockaddr.c b/tests/net-sockaddr.c index fe07d141..149ba2d8 100644 --- a/tests/net-sockaddr.c +++ b/tests/net-sockaddr.c @@ -450,40 +450,44 @@ check_l2(void) printf("connect(-1, {sa_family=AF_BLUETOOTH" ", l2_psm=htobs(L2CAP_PSM_DYN_START + %hu)" ", l2_bdaddr=%02x:%02x:%02x:%02x:%02x:%02x" - ", l2_cid=htobs(%hu), l2_bdaddr_type=0xce /* BDADDR_??? */}" + ", l2_cid=htobs(L2CAP_CID_DYN_START + %hu)" + ", l2_bdaddr_type=0xce /* BDADDR_??? */}" ", %u) = %d EBADF (%m)\n", h_psm - 0x1001, c_l2.l2_bdaddr.b[0], c_l2.l2_bdaddr.b[1], c_l2.l2_bdaddr.b[2], c_l2.l2_bdaddr.b[3], c_l2.l2_bdaddr.b[4], c_l2.l2_bdaddr.b[5], - h_cid, len, ret); + h_cid - 0x40, len, ret); c_l2.l2_psm = htobs(1); + c_l2.l2_cid = htobs(1); c_l2.l2_bdaddr_type = BDADDR_LE_RANDOM; memcpy(l2, &c_l2, sizeof(c_l2)); ret = connect(-1, l2, len); printf("connect(-1, {sa_family=AF_BLUETOOTH" ", l2_psm=htobs(L2CAP_PSM_SDP)" ", l2_bdaddr=%02x:%02x:%02x:%02x:%02x:%02x" - ", l2_cid=htobs(%hu), l2_bdaddr_type=BDADDR_LE_RANDOM}" - ", %u) = %d EBADF (%m)\n", + ", l2_cid=htobs(L2CAP_CID_SIGNALING)" + ", l2_bdaddr_type=BDADDR_LE_RANDOM}, %u) = %d EBADF (%m)\n", c_l2.l2_bdaddr.b[0], c_l2.l2_bdaddr.b[1], c_l2.l2_bdaddr.b[2], c_l2.l2_bdaddr.b[3], c_l2.l2_bdaddr.b[4], c_l2.l2_bdaddr.b[5], - h_cid, len, ret); + len, ret); c_l2.l2_psm = htobs(0xbad); + c_l2.l2_cid = htobs(8); c_l2.l2_bdaddr_type = 3; memcpy(l2, &c_l2, sizeof(c_l2)); ret = connect(-1, l2, len); printf("connect(-1, {sa_family=AF_BLUETOOTH" ", l2_psm=htobs(0xbad /* L2CAP_PSM_??? */)" ", l2_bdaddr=%02x:%02x:%02x:%02x:%02x:%02x" - ", l2_cid=htobs(%hu), l2_bdaddr_type=0x3 /* BDADDR_??? */}" + ", l2_cid=htobs(0x8 /* L2CAP_CID_??? */)" + ", l2_bdaddr_type=0x3 /* BDADDR_??? */}" ", %u) = %d EBADF (%m)\n", c_l2.l2_bdaddr.b[0], c_l2.l2_bdaddr.b[1], c_l2.l2_bdaddr.b[2], c_l2.l2_bdaddr.b[3], c_l2.l2_bdaddr.b[4], c_l2.l2_bdaddr.b[5], - h_cid, len, ret); + len, ret); } #endif diff --git a/xlat/bluetooth_l2_cid.in b/xlat/bluetooth_l2_cid.in new file mode 100644 index 00000000..5fd8fd17 --- /dev/null +++ b/xlat/bluetooth_l2_cid.in @@ -0,0 +1,11 @@ +/* sort -k2,2 */ +L2CAP_CID_SIGNALING 0x0001 +L2CAP_CID_CONN_LESS 0x0002 +L2CAP_CID_A2MP 0x0003 +L2CAP_CID_ATT 0x0004 +L2CAP_CID_LE_SIGNALING 0x0005 +L2CAP_CID_SMP 0x0006 +L2CAP_CID_SMP_BREDR 0x0007 +L2CAP_CID_DYN_START 0x0040 +L2CAP_CID_LE_DYN_END 0x007f +L2CAP_CID_DYN_END 0xffff