From 0e72230ef31b828cedfa2c73cd24a3be91f5878c Mon Sep 17 00:00:00 2001 From: Eugene Syromyatnikov Date: Sun, 6 May 2018 21:07:31 +0200 Subject: [PATCH] sockaddr: decode Bluetooth L2 PSM values * xlat/bluetooth_l2_psm.in: New file. * sockaddr.c: Include xlat/bluetooth_l2_psm.h. (print_bluetooth_l2_psm): New function. (print_sockaddr_data_bt): Use it to decode struct sockaddr_l2.l2_psm field. * tests/net-sockaddr.c (check_l2): Update expected output. --- sockaddr.c | 46 +++++++++++++++++++++++++++++++++++++--- tests/net-sockaddr.c | 14 ++++++------ xlat/bluetooth_l2_psm.in | 10 +++++++++ 3 files changed, 61 insertions(+), 9 deletions(-) create mode 100644 xlat/bluetooth_l2_psm.in diff --git a/sockaddr.c b/sockaddr.c index 149872c2..785e651f 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_psm.h" #include "xlat/hci_channels.h" #define SIZEOF_SA_FAMILY sizeof(((struct sockaddr *) 0)->sa_family) @@ -265,6 +266,46 @@ btohs(uint16_t val) #endif } +static void +print_bluetooth_l2_psm(const char *prefix, uint16_t psm) +{ + const uint16_t psm_he = btohs(psm); + const char *psm_name = xlookup(bluetooth_l2_psm, psm_he); + const bool psm_str = psm_name || (psm_he >= L2CAP_PSM_LE_DYN_START + && psm_he <= L2CAP_PSM_LE_DYN_END) + || (psm_he >= L2CAP_PSM_DYN_START); + + tprintf("%shtobs(", prefix); + + if (xlat_verbose(xlat_verbosity) != XLAT_STYLE_ABBREV || !psm_str) + tprintf("%#x", psm_he); + + if (xlat_verbose(xlat_verbosity) == XLAT_STYLE_RAW) + goto print_bluetooth_l2_psm_end; + + if (xlat_verbose(xlat_verbosity) == XLAT_STYLE_VERBOSE || !psm_str) + tprints(" /* "); + + if (psm_name) { + tprints(psm_name); + } else if (psm_he >= L2CAP_PSM_LE_DYN_START + && psm_he <= L2CAP_PSM_LE_DYN_END) { + print_xlat(L2CAP_PSM_LE_DYN_START); + tprintf(" + %u", psm_he - L2CAP_PSM_LE_DYN_START); + } else if (psm_he >= L2CAP_PSM_DYN_START) { + print_xlat(L2CAP_PSM_DYN_START); + tprintf(" + %u", psm_he - L2CAP_PSM_DYN_START); + } else { + tprints("L2CAP_PSM_???"); + } + + if (xlat_verbose(xlat_verbosity) == XLAT_STYLE_VERBOSE || !psm_str) + tprints(" */"); + +print_bluetooth_l2_psm_end: + tprints(")"); +} + static void print_sockaddr_data_bt(const void *const buf, const int addrlen) { @@ -326,10 +367,9 @@ print_sockaddr_data_bt(const void *const buf, const int addrlen) } case sizeof(struct sockaddr_l2): { const struct sockaddr_l2 *const l2 = buf; - tprintf("l2_psm=htobs(%hu)" - ", l2_bdaddr=%02x:%02x:%02x:%02x:%02x:%02x" + 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=", - btohs(l2->l2_psm), 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], diff --git a/tests/net-sockaddr.c b/tests/net-sockaddr.c index 29921b40..fe07d141 100644 --- a/tests/net-sockaddr.c +++ b/tests/net-sockaddr.c @@ -448,36 +448,38 @@ check_l2(void) int ret = connect(-1, l2, len); printf("connect(-1, {sa_family=AF_BLUETOOTH" - ", l2_psm=htobs(%hu)" + ", 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_??? */}" - ", %u) = %d EBADF (%m)\n", h_psm, + ", %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); + c_l2.l2_psm = 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(%hu)" + ", 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", h_psm, + ", %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); + c_l2.l2_psm = htobs(0xbad); 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(%hu)" + ", l2_psm=htobs(0xbad /* L2CAP_PSM_??? */)" ", l2_bdaddr=%02x:%02x:%02x:%02x:%02x:%02x" ", l2_cid=htobs(%hu), l2_bdaddr_type=0x3 /* BDADDR_??? */}" - ", %u) = %d EBADF (%m)\n", h_psm, + ", %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], diff --git a/xlat/bluetooth_l2_psm.in b/xlat/bluetooth_l2_psm.in new file mode 100644 index 00000000..afac026a --- /dev/null +++ b/xlat/bluetooth_l2_psm.in @@ -0,0 +1,10 @@ +/* sort -k2,2 */ +L2CAP_PSM_SDP 0x0001 +L2CAP_PSM_RFCOMM 0x0003 +L2CAP_PSM_3DSP 0x0021 +L2CAP_PSM_IPSP 0x0023 +L2CAP_PSM_LE_DYN_START 0x0080 +L2CAP_PSM_LE_DYN_END 0x00ff +L2CAP_PSM_DYN_START 0x1001 +L2CAP_PSM_AUTO_END 0x10ff +L2CAP_PSM_DYN_END 0xffff -- 2.40.0