* [PATCH BlueZ v4 1/6] monitor: Strip trailing L2CAP FCS before upper layer dissectors
2026-11-24 14:56 [PATCH BlueZ v4 0/6] monitor: Add an OBEX protocol decoder to btmon Frédéric Danis
@ 2026-11-24 14:56 ` Frédéric Danis
2026-11-24 20:45 ` monitor: Add an OBEX protocol decoder to btmon bluez.test.bot
2026-11-24 14:56 ` [PATCH BlueZ v4 2/6] monitor: Add OBEX protocol decoder Frédéric Danis
` (4 subsequent siblings)
5 siblings, 1 reply; 12+ messages in thread
From: Frédéric Danis @ 2026-11-24 14:56 UTC (permalink / raw)
To: linux-bluetooth
From: Frédéric Danis <frederic.danis.oss@gmail.com>
ERTM/Streaming mode I-frames append a 2-byte FCS after the payload,
which was never stripped before handing the frame to protocol
dissectors (OBEX, RFCOMM, SDP, ...). OBEX's own PDU length enforcement
was masking this as spurious "Trailing data" on every ERTM frame, two
bytes that are actually the L2CAP checksum, not protocol payload.
Track the negotiated FCS option per channel (defaulting to the 16-bit
FCS enabled by the Core Spec unless a peer explicitly negotiates "No
FCS"), and trim the trailing 2 bytes off ERTM/Streaming mode frames
before dispatching to the PSM-specific dissector.
Verified against a real btsnoop capture: the spurious 2-byte "Trailing
data" hexdump after every OBEX PDU on an ERTM channel is gone, while
decode output is otherwise unchanged (109 OBEX PDUs decoded before and
after).
Assisted-by: Claude:claude-sonnet-5
---
v3->v4:
- Track the FCS value requested by a Config Request and apply it on
a successful Config Response that doesn't repeat it (a successful
response need not repeat accepted options), instead of only
reading the FCS option when the response itself carries it.
monitor/l2cap.c | 123 +++++++++++++++++++++++++++++++++++++++++++++++-
1 file changed, 121 insertions(+), 2 deletions(-)
diff --git a/monitor/l2cap.c b/monitor/l2cap.c
index ddc2388d5..3fc876f5a 100644
--- a/monitor/l2cap.c
+++ b/monitor/l2cap.c
@@ -100,6 +100,8 @@ struct chan_data {
uint8_t ext_ctrl;
uint8_t seq_num;
uint16_t sdu;
+ uint8_t fcs;
+ int8_t req_fcs;
struct packet_latency tx_l;
};
@@ -158,6 +160,13 @@ static void assign_scid(const struct l2cap_frame *frame, uint16_t scid,
chan_list[n].ctrlid = ctrlid;
chan_list[n].mode = mode;
+ /* 16-bit FCS is the default for ERTM/Streaming mode unless the
+ * peer explicitly negotiates "No FCS" via the FCS configuration
+ * option (see assign_fcs()).
+ */
+ chan_list[n].fcs = 1;
+ chan_list[n].req_fcs = -1;
+
chan_list[n].seq_num = seq_num;
}
@@ -255,6 +264,98 @@ static void assign_mode(const struct l2cap_frame *frame,
}
}
+/* Record whether the peer negotiated "No FCS" (0x00) or "16-bit FCS"
+ * (0x01) via the FCS configuration option, so the trailing 2-byte FCS
+ * on ERTM/Streaming mode frames can be stripped before handing the
+ * payload to upper layer dissectors.
+ */
+static void assign_fcs(const struct l2cap_frame *frame,
+ uint8_t fcs, uint16_t dcid)
+{
+ int i;
+
+ for (i = 0; i < MAX_CHAN; i++) {
+ if (chan_list[i].index != frame->index)
+ continue;
+
+ if (chan_list[i].handle != frame->handle)
+ continue;
+
+ if (frame->in) {
+ if (chan_list[i].scid == dcid) {
+ chan_list[i].fcs = fcs;
+ break;
+ }
+ } else {
+ if (chan_list[i].dcid == dcid) {
+ chan_list[i].fcs = fcs;
+ break;
+ }
+ }
+ }
+}
+
+/* Record the FCS value requested by a Config Request, so it can be
+ * applied on a successful Config Response even if that response omits
+ * the FCS option (a successful response need not repeat accepted
+ * options).
+ */
+static void assign_req_fcs(const struct l2cap_frame *frame,
+ uint8_t fcs, uint16_t dcid)
+{
+ int i;
+
+ for (i = 0; i < MAX_CHAN; i++) {
+ if (chan_list[i].index != frame->index)
+ continue;
+
+ if (chan_list[i].handle != frame->handle)
+ continue;
+
+ if (frame->in) {
+ if (chan_list[i].scid == dcid) {
+ chan_list[i].req_fcs = fcs;
+ break;
+ }
+ } else {
+ if (chan_list[i].dcid == dcid) {
+ chan_list[i].req_fcs = fcs;
+ break;
+ }
+ }
+ }
+}
+
+/* Apply a previously requested FCS value on a successful Config
+ * Response, in case the response is option-free.
+ */
+static void apply_req_fcs(const struct l2cap_frame *frame, uint16_t scid)
+{
+ int i;
+
+ for (i = 0; i < MAX_CHAN; i++) {
+ if (chan_list[i].index != frame->index)
+ continue;
+
+ if (chan_list[i].handle != frame->handle)
+ continue;
+
+ if (frame->in) {
+ if (chan_list[i].scid == scid) {
+ if (chan_list[i].req_fcs >= 0)
+ chan_list[i].fcs = chan_list[i].req_fcs;
+ break;
+ }
+ } else {
+ if (chan_list[i].dcid == scid) {
+ if (chan_list[i].req_fcs >= 0)
+ chan_list[i].fcs = chan_list[i].req_fcs;
+ break;
+ }
+ }
+ }
+}
+
static int get_chan_data_index(const struct l2cap_frame *frame)
{
int i;
@@ -882,6 +983,11 @@ static void print_config_options(const struct l2cap_frame *frame,
get_le16(data + consumed + 9));
break;
case 0x05:
+ if (response)
+ assign_fcs(frame, data[consumed + 2], cid);
+ else
+ assign_req_fcs(frame, data[consumed + 2], cid);
+
switch (data[consumed + 2]) {
case 0x00:
str = "No FCS";
@@ -1188,10 +1294,13 @@ static void sig_config_rsp(const struct l2cap_frame *frame)
print_cid("Source", pdu->scid);
print_config_flags(pdu->flags);
print_config_result(pdu->result);
- if (pdu->result == 0x0003)
+ if (pdu->result == 0x0003) {
print_unknown_options(frame, 6);
- else
+ } else {
+ if (pdu->result == 0x0000)
+ apply_req_fcs(frame, le16_to_cpu(pdu->scid));
print_config_options(frame, 6, le16_to_cpu(pdu->scid), true);
+ }
}
static void sig_disconn_req(const struct l2cap_frame *frame)
@@ -2740,6 +2849,16 @@ void l2cap_frame(uint16_t index, bool in, uint16_t handle, uint16_t cid,
}
display_printf("\n");
+
+ /* ERTM/Streaming mode frames carry a trailing 2-byte
+ * FCS (unless explicitly negotiated off via the FCS
+ * configuration option) that is not part of the
+ * upper layer payload.
+ */
+ chan = get_chan(&frame);
+ if (chan && chan->fcs && frame.size >= 2)
+ frame.size -= 2;
+
break;
}
--
2.43.0
^ permalink raw reply related [flat|nested] 12+ messages in thread* RE: monitor: Add an OBEX protocol decoder to btmon
2026-11-24 14:56 ` [PATCH BlueZ v4 1/6] monitor: Strip trailing L2CAP FCS before upper layer dissectors Frédéric Danis
@ 2026-11-24 20:45 ` bluez.test.bot
0 siblings, 0 replies; 12+ messages in thread
From: bluez.test.bot @ 2026-11-24 20:45 UTC (permalink / raw)
To: linux-bluetooth, frederic.danis
[-- Attachment #1: Type: text/plain, Size: 1578 bytes --]
This is automated email and please do not reply to this email!
Dear submitter,
Thank you for submitting the patches to the linux bluetooth mailing list.
This is a CI test results with your patch series:
PW Link:https://patchwork.kernel.org/series/1220979/
---Test result---
Test Summary:
CheckPatch PASS 3.44 seconds
GitLint PASS 1.64 seconds
BuildEll PASS 13.50 seconds
BluezMake PASS 204.97 seconds
MakeCheck PASS 12.53 seconds
MakeDistcheck PASS 103.94 seconds
CheckValgrind PASS 250.68 seconds
CheckSmatch WARNING 185.22 seconds
bluezmakeextell PASS 65.59 seconds
TestFunctional PASS 917.26 seconds
IncrementalBuild PASS 250.70 seconds
ScanBuild PASS 691.04 seconds
Details
##############################
Test: CheckSmatch - WARNING
Desc: Run smatch tool with source
Output:
monitor/packet.c:2293:26: warning: Variable length array is used.monitor/packet.c: note: in included file:monitor/bt.h:4220:52: warning: array of flexible structuresmonitor/bt.h:4208:40: warning: array of flexible structuresmonitor/packet.c:2293:26: warning: Variable length array is used.monitor/packet.c: note: in included file:monitor/bt.h:4220:52: warning: array of flexible structuresmonitor/bt.h:4208:40: warning: array of flexible structures
https://github.com/bluez/bluez/pull/2908
---
Regards,
Linux Bluetooth
^ permalink raw reply [flat|nested] 12+ messages in thread
* [PATCH BlueZ v4 2/6] monitor: Add OBEX protocol decoder
2026-11-24 14:56 [PATCH BlueZ v4 0/6] monitor: Add an OBEX protocol decoder to btmon Frédéric Danis
2026-11-24 14:56 ` [PATCH BlueZ v4 1/6] monitor: Strip trailing L2CAP FCS before upper layer dissectors Frédéric Danis
@ 2026-11-24 14:56 ` Frédéric Danis
2026-11-24 14:56 ` [PATCH BlueZ v4 3/6] monitor: Decode PBAP/MAP/BIP Application Parameters tags Frédéric Danis
` (3 subsequent siblings)
5 siblings, 0 replies; 12+ messages in thread
From: Frédéric Danis @ 2026-11-24 14:56 UTC (permalink / raw)
To: linux-bluetooth
From: Frédéric Danis <frederic.danis.oss@gmail.com>
Add a dedicated OBEX dissector so btmon can decode OBEX traffic
instead of hexdumping it. OBEX reaches btmon over two transports:
- RFCOMM, on a channel number only knowable by correlating the SDP
service record that advertised it.
- L2CAP, on a dynamically negotiated PSM (advertised in a service
record via the GoepL2capPsm attribute, 0x0200), as used by
GOEP-over-L2CAP profiles such as MAP/PBAP/BIP.
monitor/obex.c tracks RFCOMM channel and L2CAP PSM to OBEX-profile
mappings learned from SDP ProtocolDescriptorList/ServiceClassIDList
attributes (via an obex_add_psm()/obex_psm() table keyed like the
existing RFCOMM channel table by index/handle/psm, cleared on ACL
disconnect), and decodes opcodes, response codes, CONNECT/SETPATH
fixed fields, and all known header IDs (Unicode text, byte sequence,
1-byte and 4-byte headers), including a generic Tag/Len/Value walk
for Application Parameters.
Sessions are keyed by l2cap_chan_key(), the direction-independent
session key already used by sdp.c/avctp.c/avdtp.c, rather than the
raw L2CAP CID (which is SCID from one peer's perspective and DCID
from the other's), so a CONNECT request seen on one direction and its
response seen on the other are correctly recognized as the same
session and the response's fixed fields (OBEX version, flags, max
packet length) are parsed instead of being mistaken for headers.
The OBEX CONNECT Target/Who headers carry a profile-specific UUID,
distinct from the SDP service class UUID; a lookup table decodes the
Target/Who values used by well known OBEX profiles - PBAP, FTP, MAP,
BIP (AVRCP Cover Art) and IrMC/SyncML.
Basic Imaging Profile uses two extension header IDs not covered by
the generic header table: IMG-Handle (0x30, Unicode text) and
IMG-Descriptor (0x71, byte sequence containing an XML document). Both
are added to the header table so they decode with a readable label
instead of falling back to 'Unknown'; IMG-Descriptor is printed as
plain text like Type, since it is always a UTF-8 XML string.
monitor/rfcomm.c dispatches UIH frames on a tracked OBEX channel to the
new decoder instead of hexdumping them. monitor/l2cap.c dispatches a
PSM the same way once it has been learned from an SDP record via
obex_add_psm()/obex_psm(). monitor/sdp.c feeds attribute responses
(including continuation-reassembled ones) to the SDP scanner so
RFCOMM channels and L2CAP PSMs can be learned; obex_sdp_scan() peels
the outer Sequence wrapper(s) sdp.c's callers pass (and, for Service
Search Attribute Response, one Sequence per matched service record)
with obex_de_next() before handing each flat attribute list to
obex_scan_attr_list(), mirroring how sdp.c's own
print_attr_list()/print_attr_lists() unwrap the same buffer for
display.
Assisted-by: Claude:claude-sonnet-5
---
v2->v3:
- Fix a possible uint32_t overflow in obex_de_next()'s SDP
element-length arithmetic by computing it in 64-bit space before
comparing it against the available buffer size.
- Fix obex_de_uuid16() extracting the short value from the wrong half
(high instead of low 16 bits) of a 32-bit SDP UUID.
- Stop special-casing L2CAP PSM 0x1001: only dispatch to the OBEX
decoder once a PSM has actually been learned from an SDP record via
obex_add_psm()/obex_psm().
v3->v4:
- Fix the disconnect handler's SCID/DCID resolution to the
direction-independent session key: SCID is the sender's own CID so
it matches the local chan_list entry on a locally-initiated
disconnect, and DCID matches it on a peer-initiated one (the two
were swapped).
- Drop obex_remove_channel()'s clearing of the SDP-learned
obex_chan_list mapping itself, so a peer can reopen the same
RFCOMM service on the still-up ACL link without repeating SDP;
only the per-session request/profile state is cleared on teardown
now.
- Use a channel/handle-zero check (!channel) instead of an explicit
handle == 0x0000 comparison to detect free obex_req_list slots.
- Validate a 128-bit UUID against the Bluetooth Base UUID suffix
before treating it as a 16-bit assigned number, instead of
assuming any 128-bit value in that attribute is Base UUID-derived.
- Recognize OBEX running directly over a dedicated L2CAP PSM (e.g.
BIP's AVRCP Cover Art) by also scanning the Protocol Descriptor
List for an L2CAP PSM parameter, not just an RFCOMM channel
number.
- Only trust a record's attribute 0x0200 as a GOEP L2CAP PSM once
the record has been confirmed to actually speak OBEX via its
protocol descriptor list(s), since other profiles reuse the same
attribute ID for unrelated UINT16 values; remember the candidate
PSM and decide once the whole record has been scanned.
- Decode UTF-16 surrogate pairs into their combined supplementary-
plane code point instead of encoding each surrogate half as its
own (invalid) UTF-8 sequence.
Makefile.tools | 1 +
monitor/l2cap.c | 23 +-
monitor/obex.c | 1122 ++++++++++++++++++++++++++++++++++++++++++++++
monitor/obex.h | 118 +++++
monitor/packet.c | 5 +-
monitor/rfcomm.c | 20 +-
monitor/sdp.c | 12 +-
7 files changed, 1293 insertions(+), 8 deletions(-)
create mode 100644 monitor/obex.c
create mode 100644 monitor/obex.h
diff --git a/Makefile.tools b/Makefile.tools
index 3a376b3d0..f67a89085 100644
--- a/Makefile.tools
+++ b/Makefile.tools
@@ -49,6 +49,7 @@ monitor_btmon_SOURCES = monitor/main.c monitor/bt.h \
monitor/a2dp.h monitor/a2dp.c \
monitor/rfcomm.h monitor/rfcomm.c \
monitor/bnep.h monitor/bnep.c \
+ monitor/obex.h monitor/obex.c \
monitor/hwdb.h monitor/hwdb.c \
monitor/keys.h monitor/keys.c \
monitor/analyze.h monitor/analyze.c \
diff --git a/monitor/l2cap.c b/monitor/l2cap.c
index 3fc876f5a..94beca9e1 100644
--- a/monitor/l2cap.c
+++ b/monitor/l2cap.c
@@ -35,6 +35,7 @@
#include "rfcomm.h"
#include "bnep.h"
#include "att.h"
+#include "obex.h"
#define L2CAP_MODE_BASIC 0x00
#define L2CAP_MODE_RETRANS 0x01
@@ -1309,6 +1310,22 @@ static void sig_disconn_req(const struct l2cap_frame *frame)
print_cid("Destination", pdu->dcid);
print_cid("Source", pdu->scid);
+
+ /* The channel is being torn down; drop any OBEX request state
+ * tracked for it so a later reuse of the same CID isn't
+ * misdecoded. Resolve both endpoints to the same
+ * direction-independent session key used by obex_packet(). SCID is
+ * the sender's own CID, so it matches the chan_list "scid" (local)
+ * entry when this request came from the local host (!frame->in);
+ * DCID is the recipient's own CID, which matches "scid" when the
+ * request came from the peer (frame->in) - see l2cap_chan_key().
+ */
+ obex_remove_session(frame->index, frame->handle,
+ l2cap_chan_key(frame->index, !frame->in, frame->handle,
+ le16_to_cpu(pdu->scid)));
+ obex_remove_session(frame->index, frame->handle,
+ l2cap_chan_key(frame->index, frame->in, frame->handle,
+ le16_to_cpu(pdu->dcid)));
}
static void sig_disconn_rsp(const struct l2cap_frame *frame)
@@ -2886,7 +2903,11 @@ void l2cap_frame(uint16_t index, bool in, uint16_t handle, uint16_t cid,
avdtp_packet(&frame);
break;
default:
- packet_hexdump(data, size);
+ if (obex_psm(index, handle, frame.psm))
+ obex_packet(&frame, l2cap_chan_key(index, in,
+ handle, cid));
+ else
+ packet_hexdump(data, size);
break;
}
break;
diff --git a/monitor/obex.c b/monitor/obex.c
new file mode 100644
index 000000000..30efcbaae
--- /dev/null
+++ b/monitor/obex.c
@@ -0,0 +1,1122 @@
+// SPDX-License-Identifier: LGPL-2.1-or-later
+/*
+ *
+ * BlueZ - Bluetooth protocol stack for Linux
+ *
+ * Copyright © 2025 Collabora Ltd.
+ *
+ *
+ */
+
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+#define _GNU_SOURCE
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <inttypes.h>
+
+#include "bluetooth/bluetooth.h"
+#include "bluetooth/uuid.h"
+
+#include "src/shared/util.h"
+#include "bt.h"
+#include "packet.h"
+#include "display.h"
+#include "l2cap.h"
+#include "obex.h"
+
+/* SDP Data Element types (see monitor/sdp.c type_table) */
+#define SDP_DE_UINT 1
+#define SDP_DE_UUID 3
+#define SDP_DE_SEQ 6
+
+/* SDP attribute IDs we care about */
+#define SDP_ATTR_SVCLASS_ID_LIST 0x0001
+#define SDP_ATTR_PROTO_DESC_LIST 0x0004
+#define SDP_ATTR_ADD_PROTO_DESC_LISTS 0x000d
+/* GOEP (OBEX) L2CAP PSM, shared by MAP/PBAP/OPP service records that
+ * support an L2CAP transport alongside (or instead of) RFCOMM.
+ */
+#define SDP_ATTR_GOEP_L2CAP_PSM 0x0200
+
+#define MAX_OBEX_CHAN 64
+#define MAX_OBEX_REQ 32
+
+struct obex_chan_data {
+ uint16_t index;
+ uint16_t handle;
+ uint8_t channel;
+ char profile[32];
+};
+
+static struct obex_chan_data obex_chan_list[MAX_OBEX_CHAN];
+
+struct obex_req_data {
+ uint16_t index;
+ uint16_t handle;
+ uint16_t channel;
+ uint8_t opcode;
+};
+
+static struct obex_req_data obex_req_list[MAX_OBEX_REQ];
+
+void obex_add_channel(uint16_t index, uint16_t handle, uint8_t channel,
+ const char *profile)
+{
+ int i, n = -1;
+
+ if (!channel)
+ return;
+
+ for (i = 0; i < MAX_OBEX_CHAN; i++) {
+ if (!obex_chan_list[i].channel) {
+ if (n < 0)
+ n = i;
+ continue;
+ }
+
+ if (obex_chan_list[i].index == index &&
+ obex_chan_list[i].handle == handle &&
+ obex_chan_list[i].channel == channel) {
+ n = i;
+ break;
+ }
+ }
+
+ if (n < 0)
+ return;
+
+ obex_chan_list[n].index = index;
+ obex_chan_list[n].handle = handle;
+ obex_chan_list[n].channel = channel;
+
+ if (profile)
+ snprintf(obex_chan_list[n].profile,
+ sizeof(obex_chan_list[n].profile),
+ "%s", profile);
+ else
+ obex_chan_list[n].profile[0] = '\0';
+}
+
+bool obex_channel(uint16_t index, uint16_t handle, uint8_t channel,
+ const char **profile)
+{
+ int i;
+
+ if (!channel)
+ return false;
+
+ for (i = 0; i < MAX_OBEX_CHAN; i++) {
+ if (!obex_chan_list[i].channel)
+ continue;
+
+ if (obex_chan_list[i].index == index &&
+ obex_chan_list[i].handle == handle &&
+ obex_chan_list[i].channel == channel) {
+ if (profile)
+ *profile = obex_chan_list[i].profile;
+ return true;
+ }
+ }
+
+ return false;
+}
+
+#define MAX_OBEX_PSM 16
+
+/* Dynamic L2CAP PSMs advertised via the SDP GoepL2capPsm attribute
+ * (0x0200), as used by GOEP-based profiles (MAP/PBAP/OPP) that support
+ * an L2CAP transport for OBEX in addition to (or instead of) RFCOMM.
+ */
+struct obex_psm_data {
+ uint16_t index;
+ uint16_t handle;
+ uint16_t psm;
+};
+
+static struct obex_psm_data obex_psm_list[MAX_OBEX_PSM];
+
+void obex_add_psm(uint16_t index, uint16_t handle, uint16_t psm)
+{
+ int i, n = -1;
+
+ if (!psm)
+ return;
+
+ for (i = 0; i < MAX_OBEX_PSM; i++) {
+ if (!obex_psm_list[i].psm) {
+ if (n < 0)
+ n = i;
+ continue;
+ }
+
+ if (obex_psm_list[i].index == index &&
+ obex_psm_list[i].handle == handle &&
+ obex_psm_list[i].psm == psm) {
+ n = i;
+ break;
+ }
+ }
+
+ if (n < 0)
+ return;
+
+ obex_psm_list[n].index = index;
+ obex_psm_list[n].handle = handle;
+ obex_psm_list[n].psm = psm;
+}
+
+bool obex_psm(uint16_t index, uint16_t handle, uint16_t psm)
+{
+ int i;
+
+ if (!psm)
+ return false;
+
+ for (i = 0; i < MAX_OBEX_PSM; i++) {
+ if (!obex_psm_list[i].psm)
+ continue;
+
+ if (obex_psm_list[i].index == index &&
+ obex_psm_list[i].handle == handle &&
+ obex_psm_list[i].psm == psm)
+ return true;
+ }
+
+ return false;
+}
+
+static void obex_clear_request(uint16_t index, uint16_t handle,
+ uint16_t channel)
+{
+ int i;
+
+ for (i = 0; i < MAX_OBEX_REQ; i++) {
+ if (obex_req_list[i].index == index &&
+ obex_req_list[i].handle == handle &&
+ obex_req_list[i].channel == channel)
+ memset(&obex_req_list[i], 0, sizeof(obex_req_list[i]));
+ }
+}
+
+void obex_remove_channel(uint16_t index, uint16_t handle, uint8_t channel)
+{
+ if (!channel)
+ return;
+
+ obex_clear_request(index, handle, channel);
+}
+
+void obex_remove_session(uint16_t index, uint16_t handle, uint16_t channel)
+{
+ obex_clear_request(index, handle, channel);
+}
+
+void obex_disconnect_handle(uint16_t index, uint16_t handle)
+{
+ int i;
+
+ for (i = 0; i < MAX_OBEX_CHAN; i++) {
+ if (!obex_chan_list[i].channel)
+ continue;
+
+ if (obex_chan_list[i].index == index &&
+ obex_chan_list[i].handle == handle)
+ memset(&obex_chan_list[i], 0,
+ sizeof(obex_chan_list[i]));
+ }
+
+ for (i = 0; i < MAX_OBEX_REQ; i++) {
+ if (obex_req_list[i].index == index &&
+ obex_req_list[i].handle == handle)
+ memset(&obex_req_list[i], 0, sizeof(obex_req_list[i]));
+ }
+
+ for (i = 0; i < MAX_OBEX_PSM; i++) {
+ if (!obex_psm_list[i].psm)
+ continue;
+
+ if (obex_psm_list[i].index == index &&
+ obex_psm_list[i].handle == handle)
+ memset(&obex_psm_list[i], 0,
+ sizeof(obex_psm_list[i]));
+ }
+}
+
+static void obex_set_last_request(uint16_t index, uint16_t handle,
+ uint16_t channel, uint8_t opcode)
+{
+ int i, n = -1;
+
+ for (i = 0; i < MAX_OBEX_REQ; i++) {
+ if (!obex_req_list[i].channel && n < 0) {
+ n = i;
+ continue;
+ }
+
+ if (obex_req_list[i].index == index &&
+ obex_req_list[i].handle == handle &&
+ obex_req_list[i].channel == channel) {
+ n = i;
+ break;
+ }
+ }
+
+ if (n < 0)
+ return;
+
+ obex_req_list[n].index = index;
+ obex_req_list[n].handle = handle;
+ obex_req_list[n].channel = channel;
+ obex_req_list[n].opcode = opcode;
+}
+
+static bool obex_last_request_is(uint16_t index, uint16_t handle,
+ uint16_t channel, uint8_t opcode)
+{
+ int i;
+
+ for (i = 0; i < MAX_OBEX_REQ; i++) {
+ if (obex_req_list[i].handle != handle ||
+ obex_req_list[i].index != index ||
+ obex_req_list[i].channel != channel)
+ continue;
+
+ return obex_req_list[i].opcode == opcode;
+ }
+
+ return false;
+}
+
+/*
+ * Minimal SDP Data Element reader, mirroring monitor/sdp.c's
+ * decode_data_elements() size/type decoding, but returning the decoded
+ * element instead of printing it so callers can inspect the value.
+ */
+static uint32_t obex_de_next(const uint8_t *data, uint32_t size,
+ uint8_t *type, const uint8_t **value,
+ uint32_t *vsize)
+{
+ uint32_t extrabytes, datalen, elemlen;
+ uint64_t total;
+
+ if (size < 1)
+ return 0;
+
+ *type = (data[0] & 0xf8) >> 3;
+
+ switch (data[0] & 0x07) {
+ case 0:
+ extrabytes = 0;
+ datalen = 1;
+ break;
+ case 1:
+ extrabytes = 0;
+ datalen = 2;
+ break;
+ case 2:
+ extrabytes = 0;
+ datalen = 4;
+ break;
+ case 3:
+ extrabytes = 0;
+ datalen = 8;
+ break;
+ case 4:
+ extrabytes = 0;
+ datalen = 16;
+ break;
+ case 5:
+ extrabytes = 1;
+ if (size < 2)
+ return 0;
+ datalen = data[1];
+ break;
+ case 6:
+ extrabytes = 2;
+ if (size < 3)
+ return 0;
+ datalen = get_be16(data + 1);
+ break;
+ case 7:
+ extrabytes = 4;
+ if (size < 5)
+ return 0;
+ datalen = get_be32(data + 1);
+ break;
+ default:
+ return 0;
+ }
+
+ /* datalen can be as large as UINT32_MAX for the 4-extrabyte
+ * encoding, so add it up in 64-bit space before comparing
+ * against the available buffer to avoid wrapping elemlen.
+ */
+ total = (uint64_t) 1 + extrabytes + datalen;
+
+ if (total > size)
+ return 0;
+
+ elemlen = (uint32_t) total;
+
+ *value = data + 1 + extrabytes;
+ *vsize = datalen;
+
+ return elemlen;
+}
+
+static uint16_t obex_de_uuid16(const uint8_t *data, uint32_t size)
+{
+ /* Bluetooth Base UUID (00000000-0000-1000-8000-00805F9B34FB),
+ * big-endian encoded; a 128-bit UUID only maps to a 16/32-bit
+ * assigned number when it shares this suffix.
+ */
+ static const uint8_t base_uuid_suffix[12] = {
+ 0x00, 0x00, 0x10, 0x00, 0x80, 0x00, 0x00, 0x80,
+ 0x5f, 0x9b, 0x34, 0xfb
+ };
+
+ switch (size) {
+ case 2:
+ return get_be16(data);
+ case 4: {
+ /* A 32-bit UUID only maps to a 16-bit assigned number when
+ * its upper 16 bits are zero, in which case the short value
+ * is encoded in the low 16 bits (see bt_uuid32_to_str()).
+ */
+ uint32_t uuid32 = get_be32(data);
+
+ if (uuid32 & 0xffff0000)
+ return 0;
+
+ return (uint16_t) uuid32;
+ }
+ case 16:
+ if (memcmp(data + 4, base_uuid_suffix,
+ sizeof(base_uuid_suffix)))
+ return 0;
+
+ if (data[0] || data[1])
+ return 0;
+
+ return get_be16(data + 2);
+ default:
+ return 0;
+ }
+}
+
+/* Scan a single Protocol Descriptor List value (a sequence of protocol
+ * entries, each of which is itself a sequence starting with a protocol
+ * UUID) looking for an RFCOMM channel number or L2CAP PSM alongside an
+ * OBEX marker. OBEX normally runs over RFCOMM, but profiles such as
+ * BIP's AVRCP Cover Art run OBEX directly over a dedicated L2CAP PSM
+ * with no RFCOMM entry in the list.
+ *
+ * Returns true if the list carries the OBEX protocol UUID, so the
+ * caller can tell a genuine OBEX record from one that merely shares an
+ * attribute ID with an unrelated profile.
+ */
+static bool obex_scan_proto_list(uint16_t index, uint16_t handle,
+ const uint8_t *data, uint32_t size,
+ const char *profile)
+{
+ uint32_t pos = 0;
+ uint8_t channel = 0;
+ uint16_t psm = 0;
+ bool have_obex = false;
+
+ while (pos < size) {
+ uint8_t type;
+ const uint8_t *entry;
+ uint32_t entry_size;
+ uint32_t elemlen = obex_de_next(data + pos, size - pos,
+ &type, &entry,
+ &entry_size);
+ uint32_t epos;
+ uint16_t uuid16 = 0;
+ bool have_uuid = false;
+
+ if (!elemlen)
+ break;
+
+ if (type != SDP_DE_SEQ)
+ goto next;
+
+ epos = 0;
+
+ while (epos < entry_size) {
+ uint8_t etype;
+ const uint8_t *eval;
+ uint32_t evsize;
+ uint32_t eelemlen = obex_de_next(entry + epos,
+ entry_size - epos,
+ &etype, &eval,
+ &evsize);
+
+ if (!eelemlen)
+ break;
+
+ if (!have_uuid) {
+ if (etype == SDP_DE_UUID) {
+ uuid16 = obex_de_uuid16(eval, evsize);
+ have_uuid = true;
+ }
+ } else if (uuid16 == 0x0003 && evsize >= 1) {
+ /* RFCOMM channel parameter */
+ channel = eval[0];
+ } else if (uuid16 == 0x0100 && evsize >= 2) {
+ /* L2CAP PSM parameter */
+ psm = get_be16(eval);
+ }
+
+ epos += eelemlen;
+ }
+
+ if (uuid16 == 0x0008)
+ have_obex = true;
+
+next:
+ pos += elemlen;
+ }
+
+ if (have_obex && channel)
+ obex_add_channel(index, handle, channel, profile);
+ else if (have_obex && psm)
+ obex_add_psm(index, handle, psm);
+
+ return have_obex;
+}
+
+/* Pull a short human-readable profile name out of a ServiceClassIDList
+ * attribute value (first UUID in the list).
+ */
+static bool obex_scan_svclass(const uint8_t *data, uint32_t size,
+ char *buf, size_t len)
+{
+ uint8_t type;
+ const uint8_t *value;
+ uint32_t vsize;
+ uint16_t uuid16;
+
+ if (!obex_de_next(data, size, &type, &value, &vsize))
+ return false;
+
+ if (type != SDP_DE_UUID)
+ return false;
+
+ uuid16 = obex_de_uuid16(value, vsize);
+ if (!uuid16)
+ return false;
+
+ snprintf(buf, len, "%s", bt_uuid16_to_str(uuid16));
+
+ return true;
+}
+
+static void obex_scan_attr_list(uint16_t index, uint16_t handle,
+ const uint8_t *data, uint32_t size)
+{
+ uint32_t pos = 0;
+ char profile[32] = { 0 };
+ bool have_obex = false;
+ uint16_t goep_psm = 0;
+
+ while (pos < size) {
+ uint8_t type;
+ const uint8_t *id_val;
+ uint32_t id_size;
+ uint32_t idlen = obex_de_next(data + pos, size - pos, &type,
+ &id_val, &id_size);
+ uint16_t attr_id;
+ uint32_t vallen;
+ const uint8_t *attr_val;
+ uint32_t attr_size;
+
+ if (!idlen || id_size < 2)
+ break;
+
+ attr_id = get_be16(id_val);
+ pos += idlen;
+
+ vallen = obex_de_next(data + pos, size - pos, &type,
+ &attr_val, &attr_size);
+ if (!vallen)
+ break;
+
+ switch (attr_id) {
+ case SDP_ATTR_SVCLASS_ID_LIST:
+ obex_scan_svclass(attr_val, attr_size, profile,
+ sizeof(profile));
+ break;
+ case SDP_ATTR_PROTO_DESC_LIST:
+ if (obex_scan_proto_list(index, handle, attr_val,
+ attr_size, profile))
+ have_obex = true;
+ break;
+ case SDP_ATTR_ADD_PROTO_DESC_LISTS: {
+ uint32_t apos = 0;
+
+ while (apos < attr_size) {
+ uint8_t atype;
+ const uint8_t *aval;
+ uint32_t avsize;
+ uint32_t aelemlen = obex_de_next(
+ attr_val + apos,
+ attr_size - apos,
+ &atype, &aval, &avsize);
+
+ if (!aelemlen)
+ break;
+
+ if (atype == SDP_DE_SEQ &&
+ obex_scan_proto_list(index,
+ handle, aval,
+ avsize,
+ profile))
+ have_obex = true;
+
+ apos += aelemlen;
+ }
+ break;
+ }
+ case SDP_ATTR_GOEP_L2CAP_PSM:
+ /* Attribute 0x0200 is profile-specific; other
+ * profiles (e.g. Device ID, HID) reuse the same
+ * numeric ID for unrelated UINT16 attributes, so
+ * only trust it as a PSM once the record has been
+ * confirmed to actually speak OBEX via its protocol
+ * descriptor list(s). Attribute order isn't
+ * guaranteed, so remember the candidate value and
+ * decide once the whole record has been scanned.
+ */
+ if (type == SDP_DE_UINT && attr_size == 2)
+ goep_psm = get_be16(attr_val);
+ break;
+ }
+
+ pos += vallen;
+ }
+
+ if (have_obex && goep_psm)
+ obex_add_psm(index, handle, goep_psm);
+}
+
+/*
+ * The buffer handed to obex_sdp_scan() is still wrapped in the raw SDP
+ * Data Element Sequence(s) used on the wire: a single outer Sequence
+ * whose contents are either a flat attribute id/value list (one service
+ * record, SDP_ServiceAttributeResponse) or, when "nested" is set, a
+ * Sequence of such attribute lists, one per matched service record
+ * (SDP_ServiceSearchAttributeResponse). Peel those wrapper(s) off before
+ * handing each flat attribute list to obex_scan_attr_list(), mirroring
+ * how monitor/sdp.c's print_attr_list()/print_attr_lists() unwrap the
+ * same buffer for display.
+ */
+void obex_sdp_scan(const struct l2cap_frame *frame, const uint8_t *data,
+ uint32_t size, bool nested)
+{
+ uint8_t type;
+ const uint8_t *val;
+ uint32_t vsize;
+ uint32_t pos = 0;
+
+ if (!obex_de_next(data, size, &type, &val, &vsize) ||
+ type != SDP_DE_SEQ)
+ return;
+
+ if (!nested) {
+ obex_scan_attr_list(frame->index, frame->handle, val, vsize);
+ return;
+ }
+
+ while (pos < vsize) {
+ uint8_t rtype;
+ const uint8_t *rval;
+ uint32_t rvsize;
+ uint32_t relemlen = obex_de_next(val + pos, vsize - pos,
+ &rtype, &rval, &rvsize);
+
+ if (!relemlen)
+ break;
+
+ if (rtype == SDP_DE_SEQ)
+ obex_scan_attr_list(frame->index, frame->handle,
+ rval, rvsize);
+
+ pos += relemlen;
+ }
+}
+
+struct obex_opcode_data {
+ uint8_t code;
+ const char *str;
+};
+
+static const struct obex_opcode_data obex_req_table[] = {
+ { OBEX_OP_CONNECT, "Connect" },
+ { OBEX_OP_DISCONNECT, "Disconnect" },
+ { OBEX_OP_PUT, "Put" },
+ { OBEX_OP_GET, "Get" },
+ { OBEX_OP_SETPATH, "Set Path" },
+ { OBEX_OP_ACTION, "Action" },
+ { OBEX_OP_SESSION, "Session" },
+ { OBEX_OP_ABORT, "Abort" },
+ { }
+};
+
+static const struct obex_opcode_data obex_rsp_table[] = {
+ { OBEX_RSP_CONTINUE, "Continue" },
+ { OBEX_RSP_SUCCESS, "OK, Success" },
+ { OBEX_RSP_CREATED, "Created" },
+ { OBEX_RSP_ACCEPTED, "Accepted" },
+ { OBEX_RSP_NON_AUTHORITATIVE, "Non-Authoritative" },
+ { OBEX_RSP_NO_CONTENT, "No Content" },
+ { OBEX_RSP_RESET_CONTENT, "Reset Content" },
+ { OBEX_RSP_PARTIAL_CONTENT, "Partial Content" },
+ { OBEX_RSP_MULTIPLE_CHOICES, "Multiple Choices" },
+ { OBEX_RSP_MOVED_PERMANENTLY, "Moved Permanently" },
+ { OBEX_RSP_MOVED_TEMPORARILY, "Moved Temporarily" },
+ { OBEX_RSP_SEE_OTHER, "See Other" },
+ { OBEX_RSP_NOT_MODIFIED, "Not Modified" },
+ { OBEX_RSP_USE_PROXY, "Use Proxy" },
+ { OBEX_RSP_BAD_REQUEST, "Bad Request" },
+ { OBEX_RSP_UNAUTHORIZED, "Unauthorized" },
+ { OBEX_RSP_PAYMENT_REQUIRED, "Payment Required" },
+ { OBEX_RSP_FORBIDDEN, "Forbidden" },
+ { OBEX_RSP_NOT_FOUND, "Not Found" },
+ { OBEX_RSP_METHOD_NOT_ALLOWED, "Method Not Allowed" },
+ { OBEX_RSP_NOT_ACCEPTABLE, "Not Acceptable" },
+ { OBEX_RSP_PROXY_AUTH_REQUIRED,
+ "Proxy Authentication Required" },
+ { OBEX_RSP_REQUEST_TIME_OUT, "Request Time Out" },
+ { OBEX_RSP_CONFLICT, "Conflict" },
+ { OBEX_RSP_GONE, "Gone" },
+ { OBEX_RSP_LENGTH_REQUIRED, "Length Required" },
+ { OBEX_RSP_PRECONDITION_FAILED, "Precondition Failed" },
+ { OBEX_RSP_REQ_ENTITY_TOO_LARGE,
+ "Requested Entity Too Large" },
+ { OBEX_RSP_REQ_URL_TOO_LARGE, "Requested URL Too Large" },
+ { OBEX_RSP_UNSUPPORTED_MEDIA_TYPE, "Unsupported Media Type" },
+ { OBEX_RSP_INTERNAL_SERVER_ERROR, "Internal Server Error" },
+ { OBEX_RSP_NOT_IMPLEMENTED, "Not Implemented" },
+ { OBEX_RSP_BAD_GATEWAY, "Bad Gateway" },
+ { OBEX_RSP_SERVICE_UNAVAILABLE, "Service Unavailable" },
+ { OBEX_RSP_GATEWAY_TIMEOUT, "Gateway Timeout" },
+ { OBEX_RSP_VERSION_NOT_SUPPORTED, "HTTP Version Not Supported" },
+ { OBEX_RSP_DATABASE_FULL, "Database Full" },
+ { OBEX_RSP_DATABASE_LOCKED, "Database Locked" },
+ { }
+};
+
+static const char *obex_opcode_str(uint8_t code, bool *is_request)
+{
+ int i;
+
+ for (i = 0; obex_req_table[i].str; i++) {
+ if (obex_req_table[i].code == code) {
+ *is_request = true;
+ return obex_req_table[i].str;
+ }
+ }
+
+ for (i = 0; obex_rsp_table[i].str; i++) {
+ if (obex_rsp_table[i].code == code) {
+ *is_request = false;
+ return obex_rsp_table[i].str;
+ }
+ }
+
+ return NULL;
+}
+
+static const struct {
+ uint8_t id;
+ const char *str;
+} obex_hdr_table[] = {
+ { OBEX_HDR_COUNT, "Count" },
+ { OBEX_HDR_NAME, "Name" },
+ { OBEX_HDR_TYPE, "Type" },
+ { OBEX_HDR_LENGTH, "Length" },
+ { OBEX_HDR_TIME, "Time" },
+ { OBEX_HDR_DESCRIPTION, "Description" },
+ { OBEX_HDR_TARGET, "Target" },
+ { OBEX_HDR_HTTP, "HTTP" },
+ { OBEX_HDR_BODY, "Body" },
+ { OBEX_HDR_BODY_END, "End of Body" },
+ { OBEX_HDR_WHO, "Who" },
+ { OBEX_HDR_CONNECTION, "Connection ID" },
+ { OBEX_HDR_APPARAM, "Application Parameters" },
+ { OBEX_HDR_AUTHCHAL, "Authentication Challenge" },
+ { OBEX_HDR_AUTHRESP, "Authentication Response" },
+ { OBEX_HDR_CREATOR, "Creator ID" },
+ { OBEX_HDR_WANUUID, "WAN UUID" },
+ { OBEX_HDR_OBJECTCLASS, "Object Class" },
+ { OBEX_HDR_SESSIONPARAM, "Session Parameters" },
+ { OBEX_HDR_SESSIONSEQ, "Session Sequence Number" },
+ { OBEX_HDR_ACTION, "Action ID" },
+ { OBEX_HDR_DESTNAME, "Destination Name" },
+ { OBEX_HDR_PERMISSIONS, "Permissions" },
+ { OBEX_HDR_SRM, "Single Response Mode" },
+ { OBEX_HDR_SRMP, "SRM Parameters" },
+ { OBEX_HDR_IMG_HANDLE, "Image Handle" },
+ { OBEX_HDR_IMG_DESC, "Image Descriptor" },
+ { }
+};
+
+static const char *obex_hdr_str(uint8_t id)
+{
+ int i;
+
+ for (i = 0; obex_hdr_table[i].str; i++) {
+ if (obex_hdr_table[i].id == id)
+ return obex_hdr_table[i].str;
+ }
+
+ return "Unknown";
+}
+
+/* Target/Who header values for well known OBEX based profiles. Values
+ * taken from the obexd client implementations (obexd/client/pbap.c,
+ * ftp.c, map.c, sync.c, bip.c), which carry the actual wire-format
+ * UUIDs used in the CONNECT request/response Target and Who headers
+ * (distinct from the 16-bit SDP service class UUIDs in lib/uuid.h).
+ */
+static const struct {
+ const uint8_t uuid[16];
+ uint8_t len;
+ const char *str;
+} obex_target_table[] = {
+ { { 0x79, 0x61, 0x35, 0xf0, 0xf0, 0xc5, 0x11, 0xd8,
+ 0x09, 0x66, 0x08, 0x00, 0x20, 0x0c, 0x9a, 0x66 }, 16,
+ "Phonebook Access (PBAP)" },
+ { { 0xf9, 0xec, 0x7b, 0xc4, 0x95, 0x3c, 0x11, 0xd2,
+ 0x98, 0x4e, 0x52, 0x54, 0x00, 0xdc, 0x9e, 0x09 }, 16,
+ "File Transfer (FTP)" },
+ { { 0xbb, 0x58, 0x2b, 0x40, 0x42, 0x0c, 0x11, 0xdb,
+ 0xb0, 0xde, 0x08, 0x00, 0x20, 0x0c, 0x9a, 0x66 }, 16,
+ "Message Access (MAP)" },
+ { { 0xbb, 0x58, 0x2b, 0x41, 0x42, 0x0c, 0x11, 0xdb,
+ 0xb0, 0xde, 0x08, 0x00, 0x20, 0x0c, 0x9a, 0x66 }, 16,
+ "Message Notification (MNS)" },
+ { { 0x71, 0x63, 0xdd, 0x54, 0x4a, 0x7e, 0x11, 0xe2,
+ 0xb4, 0x7c, 0x00, 0x50, 0xc2, 0x49, 0x00, 0x48 }, 16,
+ "Basic Imaging (BIP, AVRCP Cover Art)" },
+ { { 'I', 'R', 'M', 'C', '-', 'S', 'Y', 'N', 'C' }, 9,
+ "Synchronization (IrMC/SyncML)" },
+ { }
+};
+
+static const char *obex_target_str(const uint8_t *data, uint16_t len)
+{
+ int i;
+
+ for (i = 0; obex_target_table[i].len; i++) {
+ if (obex_target_table[i].len != len)
+ continue;
+
+ if (!memcmp(obex_target_table[i].uuid, data, len))
+ return obex_target_table[i].str;
+ }
+
+ return NULL;
+}
+
+static void print_unicode_header(const char *label, const uint8_t *data,
+ uint16_t len, uint8_t indent)
+{
+ char buf[256];
+ size_t n = 0;
+ uint16_t i;
+
+ for (i = 0; i + 1 < len && n + 4 < sizeof(buf); i += 2) {
+ uint16_t ch = get_be16(data + i);
+ uint32_t cp;
+
+ if (ch == 0x0000)
+ break;
+
+ if (ch >= 0xd800 && ch <= 0xdbff) {
+ /* High surrogate: combine with a following low
+ * surrogate into a single supplementary-plane code
+ * point, or fall back to the replacement character
+ * if unpaired.
+ */
+ uint16_t lo = 0;
+
+ if (i + 3 < len)
+ lo = get_be16(data + i + 2);
+
+ if (lo >= 0xdc00 && lo <= 0xdfff) {
+ cp = 0x10000 + (((uint32_t) ch - 0xd800) << 10)
+ + (lo - 0xdc00);
+ i += 2;
+ } else {
+ cp = 0xfffd;
+ }
+ } else if (ch >= 0xdc00 && ch <= 0xdfff) {
+ /* Unpaired low surrogate. */
+ cp = 0xfffd;
+ } else {
+ cp = ch;
+ }
+
+ if (cp < 0x80) {
+ buf[n++] = (char) cp;
+ } else if (cp < 0x800) {
+ buf[n++] = 0xc0 | (cp >> 6);
+ buf[n++] = 0x80 | (cp & 0x3f);
+ } else if (cp < 0x10000) {
+ buf[n++] = 0xe0 | (cp >> 12);
+ buf[n++] = 0x80 | ((cp >> 6) & 0x3f);
+ buf[n++] = 0x80 | (cp & 0x3f);
+ } else {
+ buf[n++] = 0xf0 | (cp >> 18);
+ buf[n++] = 0x80 | ((cp >> 12) & 0x3f);
+ buf[n++] = 0x80 | ((cp >> 6) & 0x3f);
+ buf[n++] = 0x80 | (cp & 0x3f);
+ }
+ }
+
+ buf[n] = '\0';
+
+ print_field("%*c%s: %s", indent, ' ', label, buf);
+}
+
+static void obex_print_apparam(const uint8_t *data, uint16_t size,
+ uint8_t indent)
+{
+ uint16_t pos = 0;
+
+ while (pos + 2 <= size) {
+ uint8_t tag = data[pos];
+ uint8_t len = data[pos + 1];
+
+ if ((uint32_t) pos + 2 + len > size) {
+ print_text(COLOR_ERROR, "invalid app parameter length");
+ break;
+ }
+
+ print_field("%*cTag 0x%02x len %d", indent, ' ', tag, len);
+ if (len)
+ packet_hexdump(data + pos + 2, len);
+
+ pos += 2 + len;
+ }
+}
+
+static bool obex_print_headers(struct l2cap_frame *frame, uint8_t indent)
+{
+ while (frame->size > 0) {
+ uint8_t hdr_id;
+ const char *label;
+
+ if (!l2cap_frame_get_u8(frame, &hdr_id))
+ return false;
+
+ label = obex_hdr_str(hdr_id);
+
+ switch (hdr_id & OBEX_HDR_TYPE_MASK) {
+ case OBEX_HDR_TYPE_UNICODE:
+ case OBEX_HDR_TYPE_BYTES: {
+ uint16_t hlen, vlen;
+ const uint8_t *data;
+
+ if (!l2cap_frame_get_be16(frame, &hlen))
+ return false;
+
+ if (hlen < 3 || hlen - 3 > frame->size) {
+ print_text(COLOR_ERROR,
+ "invalid header length");
+ return false;
+ }
+
+ vlen = hlen - 3;
+ data = frame->data;
+
+ if ((hdr_id & OBEX_HDR_TYPE_MASK) ==
+ OBEX_HDR_TYPE_UNICODE) {
+ print_unicode_header(label, data, vlen,
+ indent);
+ } else if (hdr_id == OBEX_HDR_TARGET ||
+ hdr_id == OBEX_HDR_WHO) {
+ const char *profile =
+ obex_target_str(data, vlen);
+
+ if (profile) {
+ print_field("%*c%s: %s", indent, ' ',
+ label, profile);
+ } else {
+ print_field("%*c%s: [len %d]", indent,
+ ' ', label, vlen);
+ if (vlen)
+ packet_hexdump(data, vlen);
+ }
+ } else if (hdr_id == OBEX_HDR_TYPE ||
+ hdr_id == OBEX_HDR_IMG_DESC) {
+ print_field("%*c%s: %.*s", indent, ' ',
+ label, vlen, data);
+ } else {
+ print_field("%*c%s: [len %d]", indent, ' ',
+ label, vlen);
+
+ if (hdr_id == OBEX_HDR_TIME)
+ print_field("%*c%.*s", indent + 2, ' ',
+ vlen, data);
+
+ if (hdr_id == OBEX_HDR_APPARAM)
+ obex_print_apparam(data, vlen,
+ indent + 2);
+ else if (vlen)
+ packet_hexdump(data, vlen);
+ }
+
+ l2cap_frame_pull(frame, frame, vlen);
+ break;
+ }
+ case OBEX_HDR_TYPE_UINT8: {
+ uint8_t value;
+
+ if (!l2cap_frame_get_u8(frame, &value))
+ return false;
+
+ print_field("%*c%s: %d (0x%02x)", indent, ' ', label,
+ value, value);
+ break;
+ }
+ case OBEX_HDR_TYPE_UINT32: {
+ uint32_t value;
+
+ if (!l2cap_frame_get_be32(frame, &value))
+ return false;
+
+ print_field("%*c%s: %u (0x%08x)", indent, ' ', label,
+ value, value);
+ break;
+ }
+ }
+ }
+
+ return true;
+}
+
+static bool obex_connect_fields(struct l2cap_frame *frame, uint8_t indent)
+{
+ uint8_t version, flags;
+ uint16_t mtu;
+
+ if (!l2cap_frame_get_u8(frame, &version) ||
+ !l2cap_frame_get_u8(frame, &flags) ||
+ !l2cap_frame_get_be16(frame, &mtu))
+ return false;
+
+ print_field("%*cVersion: %d.%d", indent, ' ', version >> 4,
+ version & 0x0f);
+ print_field("%*cFlags: 0x%02x", indent, ' ', flags);
+ print_field("%*cMax Packet Length: %d", indent, ' ', mtu);
+
+ return true;
+}
+
+static bool obex_setpath_fields(struct l2cap_frame *frame, uint8_t indent)
+{
+ uint8_t flags, constants;
+
+ if (!l2cap_frame_get_u8(frame, &flags) ||
+ !l2cap_frame_get_u8(frame, &constants))
+ return false;
+
+ print_field("%*cFlags: 0x%02x", indent, ' ', flags);
+ print_field("%*cConstants: 0x%02x", indent, ' ', constants);
+
+ return true;
+}
+
+void obex_packet(const struct l2cap_frame *frame, uint16_t channel)
+{
+ struct l2cap_frame obex_frame;
+ struct l2cap_frame *f = &obex_frame;
+ uint8_t opcode, code, indent = 1;
+ uint16_t length, pdu_len;
+ const char *opcode_str;
+ const char *color;
+ bool is_request = false;
+ const uint8_t *trailer;
+ uint16_t trailer_len;
+
+ l2cap_frame_pull(f, frame, 0);
+
+ if (f->size < 3)
+ goto fail;
+
+ if (!l2cap_frame_get_u8(f, &opcode) ||
+ !l2cap_frame_get_be16(f, &length))
+ goto fail;
+
+ /* The 3-byte opcode+length field has just been consumed. Clamp
+ * parsing to the declared OBEX PDU boundary so trailing bytes
+ * (capture noise, malformed frames, concatenated payloads) are
+ * not decoded as if they belonged to this packet.
+ */
+ if (length < 3)
+ goto fail;
+
+ pdu_len = length - 3;
+
+ if (f->size < pdu_len)
+ goto fail;
+
+ trailer = (const uint8_t *) f->data + pdu_len;
+ trailer_len = f->size - pdu_len;
+
+ f->size = pdu_len;
+
+ code = opcode & ~OBEX_FINAL_BIT;
+
+ opcode_str = obex_opcode_str(code, &is_request);
+
+ if (opcode_str)
+ color = frame->in ? COLOR_MAGENTA : COLOR_BLUE;
+ else {
+ opcode_str = "Unknown";
+ color = COLOR_WHITE_BG;
+ }
+
+ print_indent(6, color, "OBEX: ", opcode_str, COLOR_OFF,
+ " (0x%2.2x) final %d len %d", code,
+ (opcode & OBEX_FINAL_BIT) ? 1 : 0, length);
+
+ if (is_request && code == OBEX_OP_CONNECT) {
+ if (!obex_connect_fields(f, indent))
+ goto fail;
+ } else if (!is_request && obex_last_request_is(frame->index,
+ frame->handle, channel,
+ OBEX_OP_CONNECT)) {
+ if (!obex_connect_fields(f, indent))
+ goto fail;
+ } else if (is_request && code == OBEX_OP_SETPATH) {
+ if (!obex_setpath_fields(f, indent))
+ goto fail;
+ }
+
+ if (!obex_print_headers(f, indent))
+ goto fail;
+
+ if (is_request)
+ obex_set_last_request(frame->index, frame->handle, channel,
+ code);
+
+ if (trailer_len) {
+ print_field("%*cTrailing data: [len %d]", indent, ' ',
+ trailer_len);
+ packet_hexdump(trailer, trailer_len);
+ }
+
+ return;
+
+fail:
+ print_text(COLOR_ERROR, "frame too short");
+ packet_hexdump(frame->data, frame->size);
+}
diff --git a/monitor/obex.h b/monitor/obex.h
new file mode 100644
index 000000000..5af28ea8b
--- /dev/null
+++ b/monitor/obex.h
@@ -0,0 +1,118 @@
+// SPDX-License-Identifier: LGPL-2.1-or-later
+/*
+ *
+ * BlueZ - Bluetooth protocol stack for Linux
+ *
+ * Copyright (C) 2024 BlueZ project
+ *
+ *
+ */
+
+/* OBEX request opcodes (FINAL bit 0x80 is masked out before matching) */
+#define OBEX_OP_CONNECT 0x00
+#define OBEX_OP_DISCONNECT 0x01
+#define OBEX_OP_PUT 0x02
+#define OBEX_OP_GET 0x03
+#define OBEX_OP_SETPATH 0x05
+#define OBEX_OP_ACTION 0x06
+#define OBEX_OP_SESSION 0x07
+#define OBEX_OP_ABORT 0x7f
+
+#define OBEX_FINAL_BIT 0x80
+
+/* OBEX response codes */
+#define OBEX_RSP_CONTINUE 0x10
+#define OBEX_RSP_SUCCESS 0x20
+#define OBEX_RSP_CREATED 0x21
+#define OBEX_RSP_ACCEPTED 0x22
+#define OBEX_RSP_NON_AUTHORITATIVE 0x23
+#define OBEX_RSP_NO_CONTENT 0x24
+#define OBEX_RSP_RESET_CONTENT 0x25
+#define OBEX_RSP_PARTIAL_CONTENT 0x26
+#define OBEX_RSP_MULTIPLE_CHOICES 0x30
+#define OBEX_RSP_MOVED_PERMANENTLY 0x31
+#define OBEX_RSP_MOVED_TEMPORARILY 0x32
+#define OBEX_RSP_SEE_OTHER 0x33
+#define OBEX_RSP_NOT_MODIFIED 0x34
+#define OBEX_RSP_USE_PROXY 0x35
+#define OBEX_RSP_BAD_REQUEST 0x40
+#define OBEX_RSP_UNAUTHORIZED 0x41
+#define OBEX_RSP_PAYMENT_REQUIRED 0x42
+#define OBEX_RSP_FORBIDDEN 0x43
+#define OBEX_RSP_NOT_FOUND 0x44
+#define OBEX_RSP_METHOD_NOT_ALLOWED 0x45
+#define OBEX_RSP_NOT_ACCEPTABLE 0x46
+#define OBEX_RSP_PROXY_AUTH_REQUIRED 0x47
+#define OBEX_RSP_REQUEST_TIME_OUT 0x48
+#define OBEX_RSP_CONFLICT 0x49
+#define OBEX_RSP_GONE 0x4a
+#define OBEX_RSP_LENGTH_REQUIRED 0x4b
+#define OBEX_RSP_PRECONDITION_FAILED 0x4c
+#define OBEX_RSP_REQ_ENTITY_TOO_LARGE 0x4d
+#define OBEX_RSP_REQ_URL_TOO_LARGE 0x4e
+#define OBEX_RSP_UNSUPPORTED_MEDIA_TYPE 0x4f
+#define OBEX_RSP_INTERNAL_SERVER_ERROR 0x50
+#define OBEX_RSP_NOT_IMPLEMENTED 0x51
+#define OBEX_RSP_BAD_GATEWAY 0x52
+#define OBEX_RSP_SERVICE_UNAVAILABLE 0x53
+#define OBEX_RSP_GATEWAY_TIMEOUT 0x54
+#define OBEX_RSP_VERSION_NOT_SUPPORTED 0x55
+#define OBEX_RSP_DATABASE_FULL 0x60
+#define OBEX_RSP_DATABASE_LOCKED 0x61
+
+/* OBEX header identifiers */
+#define OBEX_HDR_COUNT 0xc0
+#define OBEX_HDR_NAME 0x01
+#define OBEX_HDR_TYPE 0x42
+#define OBEX_HDR_LENGTH 0xc3
+#define OBEX_HDR_TIME 0x44
+#define OBEX_HDR_DESCRIPTION 0x05
+#define OBEX_HDR_TARGET 0x46
+#define OBEX_HDR_HTTP 0x47
+#define OBEX_HDR_BODY 0x48
+#define OBEX_HDR_BODY_END 0x49
+#define OBEX_HDR_WHO 0x4a
+#define OBEX_HDR_CONNECTION 0xcb
+#define OBEX_HDR_APPARAM 0x4c
+#define OBEX_HDR_AUTHCHAL 0x4d
+#define OBEX_HDR_AUTHRESP 0x4e
+#define OBEX_HDR_CREATOR 0xcf
+#define OBEX_HDR_WANUUID 0x50
+#define OBEX_HDR_OBJECTCLASS 0x51
+#define OBEX_HDR_SESSIONPARAM 0x52
+#define OBEX_HDR_SESSIONSEQ 0x93
+#define OBEX_HDR_ACTION 0x94
+#define OBEX_HDR_DESTNAME 0x15
+#define OBEX_HDR_PERMISSIONS 0xd6
+#define OBEX_HDR_SRM 0x97
+#define OBEX_HDR_SRMP 0x98
+
+/* BIP (Basic Imaging Profile) specific headers, see BIP spec section
+ * 5.1. Only meaningful on a BIP channel, but the header IDs are not
+ * reused by any other known profile so they can be decoded generically.
+ */
+#define OBEX_HDR_IMG_HANDLE 0x30
+#define OBEX_HDR_IMG_DESC 0x71
+
+/* Header ID encoding (top 2 bits of the header ID byte) */
+#define OBEX_HDR_TYPE_MASK 0xc0
+#define OBEX_HDR_TYPE_UNICODE 0x00
+#define OBEX_HDR_TYPE_BYTES 0x40
+#define OBEX_HDR_TYPE_UINT8 0x80
+#define OBEX_HDR_TYPE_UINT32 0xc0
+
+void obex_packet(const struct l2cap_frame *frame, uint16_t channel);
+
+void obex_add_channel(uint16_t index, uint16_t handle, uint8_t channel,
+ const char *profile);
+bool obex_channel(uint16_t index, uint16_t handle, uint8_t channel,
+ const char **profile);
+void obex_remove_channel(uint16_t index, uint16_t handle, uint8_t channel);
+void obex_remove_session(uint16_t index, uint16_t handle, uint16_t channel);
+void obex_disconnect_handle(uint16_t index, uint16_t handle);
+
+void obex_add_psm(uint16_t index, uint16_t handle, uint16_t psm);
+bool obex_psm(uint16_t index, uint16_t handle, uint16_t psm);
+
+void obex_sdp_scan(const struct l2cap_frame *frame, const uint8_t *data,
+ uint32_t size, bool nested);
diff --git a/monitor/packet.c b/monitor/packet.c
index eddf8706a..7414910c7 100644
--- a/monitor/packet.c
+++ b/monitor/packet.c
@@ -47,6 +47,7 @@
#include "keys.h"
#include "packet.h"
#include "l2cap.h"
+#include "obex.h"
#include "control.h"
#include "vendor.h"
#include "msft.h"
@@ -12311,8 +12312,10 @@ static void disconnect_complete_evt(struct timeval *tv, uint16_t index,
print_handle(evt->handle);
print_reason(evt->reason);
- if (evt->status == 0x00)
+ if (evt->status == 0x00) {
release_handle(le16_to_cpu(evt->handle));
+ obex_disconnect_handle(index, le16_to_cpu(evt->handle));
+ }
}
static void auth_complete_evt(struct timeval *tv, uint16_t index,
diff --git a/monitor/rfcomm.c b/monitor/rfcomm.c
index 419910127..89cbf6087 100644
--- a/monitor/rfcomm.c
+++ b/monitor/rfcomm.c
@@ -31,6 +31,7 @@
#include "keys.h"
#include "sdp.h"
#include "rfcomm.h"
+#include "obex.h"
static const char *cr_str[] = {
"RSP",
@@ -390,8 +391,9 @@ static bool uih_frame(struct rfcomm_frame *rfcomm_frame, uint8_t indent)
uint8_t credits;
struct l2cap_frame *frame = &rfcomm_frame->l2cap_frame;
struct rfcomm_lhdr *hdr = &rfcomm_frame->hdr;
+ uint8_t channel = RFCOMM_GET_CHANNEL(hdr->address);
- if (!RFCOMM_GET_CHANNEL(hdr->address))
+ if (!channel)
return mcc_frame(rfcomm_frame, indent);
/* fetching credits from UIH frame */
@@ -402,7 +404,11 @@ static bool uih_frame(struct rfcomm_frame *rfcomm_frame, uint8_t indent)
print_field("%*cCredits: %d", indent, ' ', hdr->credits);
}
- packet_hexdump(frame->data, frame->size);
+ if (obex_channel(frame->index, frame->handle, channel, NULL))
+ obex_packet(frame, channel);
+ else
+ packet_hexdump(frame->data, frame->size);
+
return true;
}
@@ -494,9 +500,17 @@ void rfcomm_packet(const struct l2cap_frame *frame)
print_rfcomm_hdr(&rfcomm_frame, indent);
/* UIH frame */
- if (ctype == 0xef)
+ if (ctype == 0xef) {
if (!uih_frame(&rfcomm_frame, indent))
goto fail;
+ } else if (ctype == 0x43 || ctype == 0x0f) {
+ /* DISC or DM: the RFCOMM channel is being torn down, drop
+ * any OBEX state tracked for it so a later renegotiation
+ * of the same channel number isn't misdecoded.
+ */
+ obex_remove_channel(frame->index, frame->handle,
+ RFCOMM_GET_CHANNEL(hdr.address));
+ }
return;
diff --git a/monitor/sdp.c b/monitor/sdp.c
index ce06b73ec..25a718be8 100644
--- a/monitor/sdp.c
+++ b/monitor/sdp.c
@@ -29,6 +29,7 @@
#include "display.h"
#include "l2cap.h"
#include "sdp.h"
+#include "obex.h"
#define MAX_TID 16
#define MAX_CONT_SIZE 17
@@ -423,7 +424,8 @@ struct cont_data {
static struct cont_data cont_list[MAX_CONT];
-static void handle_continuation(struct tid_data *tid, bool nested,
+static void handle_continuation(const struct l2cap_frame *frame,
+ struct tid_data *tid, bool nested,
uint16_t bytes, const uint8_t *data, uint16_t size)
{
uint8_t *newdata;
@@ -437,6 +439,7 @@ static void handle_continuation(struct tid_data *tid, bool nested,
if (tid->cont[0] == 0x00 && data[bytes] == 0x00) {
decode_data_elements(0, 2, data, bytes,
nested ? print_attr_lists : print_attr_list);
+ obex_sdp_scan(frame, data, bytes, nested);
print_continuation(data + bytes, size - bytes);
return;
@@ -489,6 +492,8 @@ static void handle_continuation(struct tid_data *tid, bool nested,
decode_data_elements(0, 2, cont_list[n].data, cont_list[n].size,
nested ? print_attr_lists : print_attr_list);
+ obex_sdp_scan(frame, cont_list[n].data, cont_list[n].size,
+ nested);
free(cont_list[n].data);
cont_list[n].data = NULL;
@@ -641,7 +646,7 @@ static void attr_rsp(const struct l2cap_frame *frame, struct tid_data *tid)
bytes = common_rsp(frame, tid);
- handle_continuation(tid, false, bytes,
+ handle_continuation(frame, tid, false, bytes,
frame->data + 2, frame->size - 2);
clear_tid(tid);
@@ -689,7 +694,8 @@ static void search_attr_rsp(const struct l2cap_frame *frame,
bytes = common_rsp(frame, tid);
- handle_continuation(tid, true, bytes, frame->data + 2, frame->size - 2);
+ handle_continuation(frame, tid, true, bytes, frame->data + 2,
+ frame->size - 2);
clear_tid(tid);
}
--
2.43.0
^ permalink raw reply related [flat|nested] 12+ messages in thread* [PATCH BlueZ v4 3/6] monitor: Decode PBAP/MAP/BIP Application Parameters tags
2026-11-24 14:56 [PATCH BlueZ v4 0/6] monitor: Add an OBEX protocol decoder to btmon Frédéric Danis
2026-11-24 14:56 ` [PATCH BlueZ v4 1/6] monitor: Strip trailing L2CAP FCS before upper layer dissectors Frédéric Danis
2026-11-24 14:56 ` [PATCH BlueZ v4 2/6] monitor: Add OBEX protocol decoder Frédéric Danis
@ 2026-11-24 14:56 ` Frédéric Danis
2026-11-24 14:56 ` [PATCH BlueZ v4 4/6] monitor: Reassemble L2CAP ERTM/Streaming SDUs Frédéric Danis
` (2 subsequent siblings)
5 siblings, 0 replies; 12+ messages in thread
From: Frédéric Danis @ 2026-11-24 14:56 UTC (permalink / raw)
To: linux-bluetooth
From: Frédéric Danis <frederic.danis.oss@gmail.com>
Application Parameters (Tag/Len/Value) were only shown generically
as 'Tag 0xNN len N' plus a hexdump, with no knowledge of what each
tag means. Add per-profile tag tables (name + value type) for PBAP,
MAP and BIP, sourced from their respective specifications, and decode
known tag values as numbers/strings instead of a raw hexdump.
Since Application Parameters headers don't repeat which profile a
session belongs to, remember the profile classification learned from
a session's CONNECT Target/Who header (already decoded by the
existing Target/Who profile lookup) in a small per-session table,
keyed the same way as the existing last-request tracking. Sessions
with no recognized Target/Who (or none yet seen) keep today's
generic Tag/Len/Value dump.
The matching obex_profile_list entries are cleared from
obex_remove_channel(), obex_remove_session() and
obex_disconnect_handle() whenever a session, channel, or handle goes
away, and on every CONNECT request, so a later session reusing the
same index/handle/channel triplet cannot inherit a profile learned by
an unrelated earlier session.
Assisted-by: Claude:claude-sonnet-5
---
v2->v3:
- Clear the learned profile classification from
obex_remove_channel(), obex_remove_session() and
obex_disconnect_handle() on session/channel/handle teardown, and
also reset it on every CONNECT, so a later session reusing the same
index/handle/channel triplet can't inherit a stale profile from an
unrelated earlier session.
v3->v4:
- Explain in a comment above obex_remove_channel() why the
SDP-learned obex_chan_list mapping itself is kept across RFCOMM
teardown (a reconnect on the same ACL link must still resolve the
channel), and bail out early when called with a zero channel.
- Use a channel-zero check (!channel) instead of an explicit
handle == 0x0000 comparison to detect free obex_profile_list
slots.
monitor/obex.c | 365 +++++++++++++++++++++++++++++++++++++++++++++++--
1 file changed, 352 insertions(+), 13 deletions(-)
diff --git a/monitor/obex.c b/monitor/obex.c
index 30efcbaae..6e69412ff 100644
--- a/monitor/obex.c
+++ b/monitor/obex.c
@@ -45,6 +45,18 @@
#define MAX_OBEX_CHAN 64
#define MAX_OBEX_REQ 32
+/* Profiles with a known Application Parameters tag set, used to
+ * select the right tag table when decoding the Application Parameters
+ * header. OBEX_PROFILE_UNKNOWN falls back to a generic Tag/Len/Value
+ * dump.
+ */
+enum obex_profile {
+ OBEX_PROFILE_UNKNOWN,
+ OBEX_PROFILE_PBAP,
+ OBEX_PROFILE_MAP,
+ OBEX_PROFILE_BIP,
+};
+
struct obex_chan_data {
uint16_t index;
uint16_t handle;
@@ -202,17 +214,59 @@ static void obex_clear_request(uint16_t index, uint16_t handle,
}
}
+/* Profile classification (PBAP/MAP/BIP/unknown) of an ongoing OBEX
+ * session, learned from a CONNECT's Target/Who header and remembered
+ * per session so that later Application Parameters headers (which
+ * don't repeat the Target/Who UUID) can be decoded using the right
+ * profile-specific tag table.
+ */
+#define MAX_OBEX_PROFILE 32
+
+struct obex_profile_data {
+ uint16_t index;
+ uint16_t handle;
+ uint16_t channel;
+ enum obex_profile profile;
+};
+
+static struct obex_profile_data obex_profile_list[MAX_OBEX_PROFILE];
+
+static void obex_clear_profile(uint16_t index, uint16_t handle,
+ uint16_t channel)
+{
+ int i;
+
+ for (i = 0; i < MAX_OBEX_PROFILE; i++) {
+ if (obex_profile_list[i].index == index &&
+ obex_profile_list[i].handle == handle &&
+ obex_profile_list[i].channel == channel)
+ memset(&obex_profile_list[i], 0,
+ sizeof(obex_profile_list[i]));
+ }
+}
+
+/* The RFCOMM channel is being torn down (DISC/DM), but the SDP-learned
+ * service-to-channel mapping in obex_chan_list is kept: a peer can
+ * reopen the same RFCOMM service on the still-up ACL link without
+ * repeating SDP, and obex_channel() needs the mapping to still resolve
+ * for that reconnection. Only the per-session request/profile state is
+ * cleared here; the mapping itself is only dropped by a later SDP
+ * update (via obex_add_channel()) or when the ACL handle disconnects
+ * (obex_disconnect_handle()).
+ */
void obex_remove_channel(uint16_t index, uint16_t handle, uint8_t channel)
{
if (!channel)
return;
obex_clear_request(index, handle, channel);
+ obex_clear_profile(index, handle, channel);
}
void obex_remove_session(uint16_t index, uint16_t handle, uint16_t channel)
{
obex_clear_request(index, handle, channel);
+ obex_clear_profile(index, handle, channel);
}
void obex_disconnect_handle(uint16_t index, uint16_t handle)
@@ -244,6 +298,16 @@ void obex_disconnect_handle(uint16_t index, uint16_t handle)
memset(&obex_psm_list[i], 0,
sizeof(obex_psm_list[i]));
}
+
+ for (i = 0; i < MAX_OBEX_PROFILE; i++) {
+ if (!obex_profile_list[i].channel)
+ continue;
+
+ if (obex_profile_list[i].index == index &&
+ obex_profile_list[i].handle == handle)
+ memset(&obex_profile_list[i], 0,
+ sizeof(obex_profile_list[i]));
+ }
}
static void obex_set_last_request(uint16_t index, uint16_t handle,
@@ -291,6 +355,52 @@ static bool obex_last_request_is(uint16_t index, uint16_t handle,
return false;
}
+static void obex_set_profile(uint16_t index, uint16_t handle,
+ uint16_t channel,
+ enum obex_profile profile)
+{
+ int i, n = -1;
+
+ for (i = 0; i < MAX_OBEX_PROFILE; i++) {
+ if (!obex_profile_list[i].channel && n < 0) {
+ n = i;
+ continue;
+ }
+
+ if (obex_profile_list[i].index == index &&
+ obex_profile_list[i].handle == handle &&
+ obex_profile_list[i].channel == channel) {
+ n = i;
+ break;
+ }
+ }
+
+ if (n < 0)
+ return;
+
+ obex_profile_list[n].index = index;
+ obex_profile_list[n].handle = handle;
+ obex_profile_list[n].channel = channel;
+ obex_profile_list[n].profile = profile;
+}
+
+static enum obex_profile obex_get_profile(uint16_t index, uint16_t handle,
+ uint16_t channel)
+{
+ int i;
+
+ for (i = 0; i < MAX_OBEX_PROFILE; i++) {
+ if (obex_profile_list[i].handle != handle ||
+ obex_profile_list[i].index != index ||
+ obex_profile_list[i].channel != channel)
+ continue;
+
+ return obex_profile_list[i].profile;
+ }
+
+ return OBEX_PROFILE_UNKNOWN;
+}
+
/*
* Minimal SDP Data Element reader, mirroring monitor/sdp.c's
* decode_data_elements() size/type decoding, but returning the decoded
@@ -788,24 +898,31 @@ static const struct {
const uint8_t uuid[16];
uint8_t len;
const char *str;
+ enum obex_profile profile;
} obex_target_table[] = {
{ { 0x79, 0x61, 0x35, 0xf0, 0xf0, 0xc5, 0x11, 0xd8,
0x09, 0x66, 0x08, 0x00, 0x20, 0x0c, 0x9a, 0x66 }, 16,
- "Phonebook Access (PBAP)" },
+ "Phonebook Access (PBAP)",
+ OBEX_PROFILE_PBAP },
{ { 0xf9, 0xec, 0x7b, 0xc4, 0x95, 0x3c, 0x11, 0xd2,
0x98, 0x4e, 0x52, 0x54, 0x00, 0xdc, 0x9e, 0x09 }, 16,
- "File Transfer (FTP)" },
+ "File Transfer (FTP)",
+ OBEX_PROFILE_UNKNOWN },
{ { 0xbb, 0x58, 0x2b, 0x40, 0x42, 0x0c, 0x11, 0xdb,
0xb0, 0xde, 0x08, 0x00, 0x20, 0x0c, 0x9a, 0x66 }, 16,
- "Message Access (MAP)" },
+ "Message Access (MAP)",
+ OBEX_PROFILE_MAP },
{ { 0xbb, 0x58, 0x2b, 0x41, 0x42, 0x0c, 0x11, 0xdb,
0xb0, 0xde, 0x08, 0x00, 0x20, 0x0c, 0x9a, 0x66 }, 16,
- "Message Notification (MNS)" },
+ "Message Notification (MNS)",
+ OBEX_PROFILE_MAP },
{ { 0x71, 0x63, 0xdd, 0x54, 0x4a, 0x7e, 0x11, 0xe2,
0xb4, 0x7c, 0x00, 0x50, 0xc2, 0x49, 0x00, 0x48 }, 16,
- "Basic Imaging (BIP, AVRCP Cover Art)" },
+ "Basic Imaging (BIP, AVRCP Cover Art)",
+ OBEX_PROFILE_BIP },
{ { 'I', 'R', 'M', 'C', '-', 'S', 'Y', 'N', 'C' }, 9,
- "Synchronization (IrMC/SyncML)" },
+ "Synchronization (IrMC/SyncML)",
+ OBEX_PROFILE_UNKNOWN },
{ }
};
@@ -824,6 +941,22 @@ static const char *obex_target_str(const uint8_t *data, uint16_t len)
return NULL;
}
+static enum obex_profile obex_target_profile(const uint8_t *data,
+ uint16_t len)
+{
+ int i;
+
+ for (i = 0; obex_target_table[i].len; i++) {
+ if (obex_target_table[i].len != len)
+ continue;
+
+ if (!memcmp(obex_target_table[i].uuid, data, len))
+ return obex_target_table[i].profile;
+ }
+
+ return OBEX_PROFILE_UNKNOWN;
+}
+
static void print_unicode_header(const char *label, const uint8_t *data,
uint16_t len, uint8_t indent)
{
@@ -885,8 +1018,200 @@ static void print_unicode_header(const char *label, const uint8_t *data,
print_field("%*c%s: %s", indent, ' ', label, buf);
}
+/* Application Parameters (Tag/Len/Value) tag tables for profiles whose
+ * tag assignments are known. Tag IDs and names are taken from the
+ * PBAP/MAP/BIP specifications; types indicate how to decode a tag's
+ * value when its length matches what the spec defines for it.
+ */
+enum obex_aparam_type {
+ OBEX_APARAM_U8,
+ OBEX_APARAM_U16,
+ OBEX_APARAM_U32,
+ OBEX_APARAM_U64,
+ OBEX_APARAM_U128,
+ OBEX_APARAM_STR,
+ OBEX_APARAM_BYTES,
+};
+
+struct obex_aparam_tag {
+ uint8_t tag;
+ const char *str;
+ enum obex_aparam_type type;
+};
+
+static const struct obex_aparam_tag obex_pbap_aparam_table[] = {
+ { 0x01, "Order", OBEX_APARAM_U8 },
+ { 0x02, "Search Value", OBEX_APARAM_STR },
+ { 0x03, "Search Attribute", OBEX_APARAM_U8 },
+ { 0x04, "Max List Count", OBEX_APARAM_U16 },
+ { 0x05, "List Start Offset", OBEX_APARAM_U16 },
+ { 0x06, "Filter", OBEX_APARAM_U64 },
+ { 0x07, "Format", OBEX_APARAM_U8 },
+ { 0x08, "Phonebook Size", OBEX_APARAM_U16 },
+ { 0x09, "New Missed Calls", OBEX_APARAM_U8 },
+ { 0x0a, "Primary Folder Version", OBEX_APARAM_U128 },
+ { 0x0b, "Secondary Folder Version", OBEX_APARAM_U128 },
+ { 0x0c, "vCard Selector", OBEX_APARAM_U64 },
+ { 0x0d, "Database Identifier", OBEX_APARAM_U128 },
+ { 0x0e, "vCard Selector Operator", OBEX_APARAM_U8 },
+ { 0x0f, "Reset New Missed Calls", OBEX_APARAM_U8 },
+ { 0x10, "Supported Features", OBEX_APARAM_U32 },
+ { }
+};
+
+static const struct obex_aparam_tag obex_map_aparam_table[] = {
+ { 0x01, "Max List Count", OBEX_APARAM_U16 },
+ { 0x02, "List Start Offset", OBEX_APARAM_U16 },
+ { 0x03, "Filter Message Type", OBEX_APARAM_U8 },
+ { 0x04, "Filter Period Begin", OBEX_APARAM_STR },
+ { 0x05, "Filter Period End", OBEX_APARAM_STR },
+ { 0x06, "Filter Read Status", OBEX_APARAM_U8 },
+ { 0x07, "Filter Recipient", OBEX_APARAM_STR },
+ { 0x08, "Filter Originator", OBEX_APARAM_STR },
+ { 0x09, "Filter Priority", OBEX_APARAM_U8 },
+ { 0x0a, "Attachment", OBEX_APARAM_U8 },
+ { 0x0b, "Transparent", OBEX_APARAM_U8 },
+ { 0x0c, "Retry", OBEX_APARAM_U8 },
+ { 0x0d, "New Message", OBEX_APARAM_U8 },
+ { 0x0e, "Notification Status", OBEX_APARAM_U8 },
+ { 0x0f, "MAS Instance ID", OBEX_APARAM_U8 },
+ { 0x10, "Parameter Mask", OBEX_APARAM_U32 },
+ { 0x11, "Folder Listing Size", OBEX_APARAM_U16 },
+ { 0x12, "Listing Size", OBEX_APARAM_U16 },
+ { 0x13, "Subject Length", OBEX_APARAM_U8 },
+ { 0x14, "Charset", OBEX_APARAM_U8 },
+ { 0x15, "Fraction Request", OBEX_APARAM_U8 },
+ { 0x16, "Fraction Deliver", OBEX_APARAM_U8 },
+ { 0x17, "Status Indicator", OBEX_APARAM_U8 },
+ { 0x18, "Status Value", OBEX_APARAM_U8 },
+ { 0x19, "MSE Time", OBEX_APARAM_STR },
+ { 0x1a, "Database Identifier", OBEX_APARAM_BYTES },
+ { 0x1b, "Conversation Listing Version Counter", OBEX_APARAM_BYTES },
+ { 0x1c, "Presence Availability", OBEX_APARAM_U8 },
+ { 0x1d, "Presence Text", OBEX_APARAM_STR },
+ { 0x1e, "Last Activity", OBEX_APARAM_STR },
+ { 0x1f, "Filter Last Activity Begin", OBEX_APARAM_STR },
+ { 0x20, "Filter Last Activity End", OBEX_APARAM_STR },
+ { 0x21, "Chat State", OBEX_APARAM_U8 },
+ { 0x22, "Conversation ID", OBEX_APARAM_BYTES },
+ { 0x23, "Folder Version Counter", OBEX_APARAM_BYTES },
+ { 0x24, "Filter Message Handle", OBEX_APARAM_STR },
+ { 0x25, "Notification Filter Mask", OBEX_APARAM_U32 },
+ { 0x26, "Conversation Parameter Mask", OBEX_APARAM_U32 },
+ { 0x27, "Filter Message Source Type", OBEX_APARAM_U8 },
+ { 0x29, "MAP Supported Features", OBEX_APARAM_U32 },
+ { }
+};
+
+static const struct obex_aparam_tag obex_bip_aparam_table[] = {
+ { 0x01, "Number of Returned Handles", OBEX_APARAM_U16 },
+ { 0x02, "List Start Offset", OBEX_APARAM_U16 },
+ { 0x03, "Latest Captured Images", OBEX_APARAM_U8 },
+ { 0x04, "Partial File Length", OBEX_APARAM_U32 },
+ { 0x05, "Partial File Start Offset", OBEX_APARAM_U32 },
+ { 0x06, "Total File Size", OBEX_APARAM_U32 },
+ { 0x07, "End Flag", OBEX_APARAM_U8 },
+ { 0x08, "Remote Display", OBEX_APARAM_U8 },
+ { }
+};
+
+static void obex_print_apparam_tag(enum obex_profile profile, uint8_t tag,
+ const uint8_t *data, uint8_t len,
+ uint8_t indent)
+{
+ const struct obex_aparam_tag *table;
+ int i;
+
+ switch (profile) {
+ case OBEX_PROFILE_PBAP:
+ table = obex_pbap_aparam_table;
+ break;
+ case OBEX_PROFILE_MAP:
+ table = obex_map_aparam_table;
+ break;
+ case OBEX_PROFILE_BIP:
+ table = obex_bip_aparam_table;
+ break;
+ case OBEX_PROFILE_UNKNOWN:
+ default:
+ table = NULL;
+ break;
+ }
+
+ for (i = 0; table && table[i].str; i++) {
+ if (table[i].tag != tag)
+ continue;
+
+ switch (table[i].type) {
+ case OBEX_APARAM_U8:
+ if (len == 1) {
+ print_field("%*c%s: %d (0x%02x)", indent, ' ',
+ table[i].str, data[0],
+ data[0]);
+ return;
+ }
+ break;
+ case OBEX_APARAM_U16:
+ if (len == 2) {
+ uint16_t val = get_be16(data);
+
+ print_field("%*c%s: %u (0x%04x)", indent, ' ',
+ table[i].str, val, val);
+ return;
+ }
+ break;
+ case OBEX_APARAM_U32:
+ if (len == 4) {
+ uint32_t val = get_be32(data);
+
+ print_field("%*c%s: %u (0x%08x)", indent, ' ',
+ table[i].str, val, val);
+ return;
+ }
+ break;
+ case OBEX_APARAM_U64:
+ if (len == 8) {
+ print_field("%*c%s: 0x%016" PRIx64, indent,
+ ' ', table[i].str,
+ get_be64(data));
+ return;
+ }
+ break;
+ case OBEX_APARAM_U128:
+ if (len == 16) {
+ print_field("%*c%s: 0x%016" PRIx64 "%016"
+ PRIx64, indent, ' ',
+ table[i].str, get_be64(data),
+ get_be64(data + 8));
+ return;
+ }
+ break;
+ case OBEX_APARAM_STR:
+ print_field("%*c%s: %.*s", indent, ' ', table[i].str,
+ len, data);
+ return;
+ case OBEX_APARAM_BYTES:
+ break;
+ }
+
+ /* Known tag but unexpected length (or a raw-bytes type):
+ * still show the tag's name, with a length + hexdump
+ * instead of a decoded value.
+ */
+ print_field("%*c%s: [len %d]", indent, ' ', table[i].str,
+ len);
+ if (len)
+ packet_hexdump(data, len);
+ return;
+ }
+
+ print_field("%*cTag 0x%02x len %d", indent, ' ', tag, len);
+ if (len)
+ packet_hexdump(data, len);
+}
+
static void obex_print_apparam(const uint8_t *data, uint16_t size,
- uint8_t indent)
+ uint8_t indent, enum obex_profile profile)
{
uint16_t pos = 0;
@@ -899,15 +1224,15 @@ static void obex_print_apparam(const uint8_t *data, uint16_t size,
break;
}
- print_field("%*cTag 0x%02x len %d", indent, ' ', tag, len);
- if (len)
- packet_hexdump(data + pos + 2, len);
+ obex_print_apparam_tag(profile, tag, data + pos + 2, len,
+ indent);
pos += 2 + len;
}
}
-static bool obex_print_headers(struct l2cap_frame *frame, uint8_t indent)
+static bool obex_print_headers(struct l2cap_frame *frame, uint8_t indent,
+ uint16_t channel)
{
while (frame->size > 0) {
uint8_t hdr_id;
@@ -948,6 +1273,10 @@ static bool obex_print_headers(struct l2cap_frame *frame, uint8_t indent)
if (profile) {
print_field("%*c%s: %s", indent, ' ',
label, profile);
+ obex_set_profile(frame->index,
+ frame->handle, channel,
+ obex_target_profile(data,
+ vlen));
} else {
print_field("%*c%s: [len %d]", indent,
' ', label, vlen);
@@ -968,7 +1297,10 @@ static bool obex_print_headers(struct l2cap_frame *frame, uint8_t indent)
if (hdr_id == OBEX_HDR_APPARAM)
obex_print_apparam(data, vlen,
- indent + 2);
+ indent + 2,
+ obex_get_profile(frame->index,
+ frame->handle,
+ channel));
else if (vlen)
packet_hexdump(data, vlen);
}
@@ -1089,6 +1421,13 @@ void obex_packet(const struct l2cap_frame *frame, uint16_t channel)
(opcode & OBEX_FINAL_BIT) ? 1 : 0, length);
if (is_request && code == OBEX_OP_CONNECT) {
+ /* A new CONNECT drops any profile classification learned
+ * from a previous session reusing this index/handle/channel
+ * key, in case this CONNECT's Target/Who header (if any)
+ * doesn't repeat it.
+ */
+ obex_clear_profile(frame->index, frame->handle, channel);
+
if (!obex_connect_fields(f, indent))
goto fail;
} else if (!is_request && obex_last_request_is(frame->index,
@@ -1101,7 +1440,7 @@ void obex_packet(const struct l2cap_frame *frame, uint16_t channel)
goto fail;
}
- if (!obex_print_headers(f, indent))
+ if (!obex_print_headers(f, indent, channel))
goto fail;
if (is_request)
--
2.43.0
^ permalink raw reply related [flat|nested] 12+ messages in thread* [PATCH BlueZ v4 4/6] monitor: Reassemble L2CAP ERTM/Streaming SDUs
2026-11-24 14:56 [PATCH BlueZ v4 0/6] monitor: Add an OBEX protocol decoder to btmon Frédéric Danis
` (2 preceding siblings ...)
2026-11-24 14:56 ` [PATCH BlueZ v4 3/6] monitor: Decode PBAP/MAP/BIP Application Parameters tags Frédéric Danis
@ 2026-11-24 14:56 ` Frédéric Danis
2026-11-24 14:56 ` [PATCH BlueZ v4 5/6] monitor: Fix RFCOMM UIH frame length and credit-only handling Frédéric Danis
2026-11-24 14:56 ` [PATCH BlueZ v4 6/6] monitor: Reassemble OBEX PDUs split across RFCOMM UIH frames Frédéric Danis
5 siblings, 0 replies; 12+ messages in thread
From: Frédéric Danis @ 2026-11-24 14:56 UTC (permalink / raw)
To: linux-bluetooth
From: Frédéric Danis <frederic.danis.oss@gmail.com>
L2CAP ERTM/Streaming mode can segment a single upper-layer SDU across
several I-frames (Start/Continuation/End SAR values). Previously each
fragment was dissected independently, so OBEX PDUs spanning more than
one fragment failed to parse ('frame too short') and S-frames (which
carry no SAR field or payload) were incorrectly sent to the upper
layer dissector as empty frames, adding further false positives.
Add per-channel, per-direction SDU reassembly buffers
(chan->sdu_rx[]), filled across Start/Continuation/End I-frames and
handed to the upper layer dissector only once the full SDU is
available. S-frames are now skipped entirely before PSM dispatch.
Each direction's expected TxSeq is tracked alongside its buffer: a
fragment matching the previous TxSeq is a retransmitted duplicate and
is discarded instead of being appended again, and a fragment whose
TxSeq doesn't match the expected value means a prior fragment was
lost, so the in-progress SDU is discarded rather than reassembled
past the gap.
While an SDU is still being reassembled (Start/Continuation
fragments), the raw per-fragment payload is still hexdumped so no
bytes are lost from the trace, but OBEX decoding itself only runs
once on the complete SDU (on the End fragment).
To avoid printing bytes twice (once as a raw Start/Continuation
fragment dump, and again inside a header value dump once the full
PDU is decoded), obex_packet() now also takes the base and size of
the fragment that triggered the call (the whole buffer for RFCOMM/
Unsegmented/untracked channels, or just the last physical I-frame's
payload for a just-completed SAR reassembly). Internal hexdumps in
obex.c are routed through a small obex_hexdump() helper that clips
output to only the bytes carried by that fragment.
Since headers are serialized near the start of a PDU while a SAR
reassembly's last fragment carries only its final bytes, restrict
this clipping to Body/End-of-Body: Application Parameters and
Target/Who header values are always shown unclipped, otherwise their
contents would be dropped whenever they land in an earlier fragment
than the one that completed reassembly.
Assisted-by: Claude:claude-sonnet-5
---
v1->v2: Fix commit title issue
v2->v3:
- Retitled from "monitor: Reassemble L2CAP ERTM/Streaming fragments
before dissecting" (subject line was exceeding the mailing list's
80-character limit with the series prefix).
- Track the expected TxSeq per direction alongside each reassembly
buffer: a fragment matching the previous TxSeq is a retransmitted
duplicate and is discarded instead of being appended again, and a
fragment whose TxSeq doesn't match the expected value means a prior
fragment was lost, so the in-progress SDU is discarded rather than
reassembled past the gap.
monitor/l2cap.c | 245 ++++++++++++++++++++++++++++++++++++++++++-----
monitor/obex.c | 63 ++++++++++--
monitor/obex.h | 3 +-
monitor/rfcomm.c | 2 +-
4 files changed, 282 insertions(+), 31 deletions(-)
diff --git a/monitor/l2cap.c b/monitor/l2cap.c
index 94beca9e1..f50e1144d 100644
--- a/monitor/l2cap.c
+++ b/monitor/l2cap.c
@@ -86,9 +86,24 @@
#define L2CAP_SAR_START 0x01
#define L2CAP_SAR_END 0x02
#define L2CAP_SAR_CONTINUE 0x03
+#define L2CAP_SAR_NONE 0xff /* S-frame, no SAR field */
#define MAX_CHAN 64
+/* Reassembled L2CAP SDU for ERTM/Streaming mode channels using
+ * segmentation (SAR Start/Continuation/End). Tracked separately per
+ * direction since a bidirectional channel may have independent SDUs
+ * in flight each way.
+ */
+struct l2cap_sdu {
+ uint8_t *data;
+ uint16_t len;
+ uint16_t total;
+ int32_t next_seq; /* expected TxSeq of the next fragment, or
+ * -1 if no reassembly is in progress
+ */
+};
+
struct chan_data {
uint16_t index;
uint16_t handle;
@@ -103,11 +118,21 @@ struct chan_data {
uint16_t sdu;
uint8_t fcs;
int8_t req_fcs;
+ struct l2cap_sdu sdu_rx[2];
struct packet_latency tx_l;
};
static struct chan_data chan_list[MAX_CHAN];
+static void reset_sdu_rx(struct chan_data *chan)
+{
+ free(chan->sdu_rx[0].data);
+ free(chan->sdu_rx[1].data);
+ memset(chan->sdu_rx, 0, sizeof(chan->sdu_rx));
+ chan->sdu_rx[0].next_seq = -1;
+ chan->sdu_rx[1].next_seq = -1;
+}
+
static void assign_scid(const struct l2cap_frame *frame, uint16_t scid,
uint16_t psm, uint8_t mode, uint8_t ctrlid)
{
@@ -147,6 +172,7 @@ static void assign_scid(const struct l2cap_frame *frame, uint16_t scid,
if (n < 0)
return;
+ reset_sdu_rx(&chan_list[n]);
memset(&chan_list[n], 0, sizeof(chan_list[n]));
chan_list[n].index = frame->index;
chan_list[n].handle = frame->handle;
@@ -169,6 +195,8 @@ static void assign_scid(const struct l2cap_frame *frame, uint16_t scid,
chan_list[n].req_fcs = -1;
chan_list[n].seq_num = seq_num;
+ chan_list[n].sdu_rx[0].next_seq = -1;
+ chan_list[n].sdu_rx[1].next_seq = -1;
}
static void release_scid(const struct l2cap_frame *frame, uint16_t scid)
@@ -184,11 +212,13 @@ static void release_scid(const struct l2cap_frame *frame, uint16_t scid)
if (frame->in) {
if (chan_list[i].scid == scid) {
+ reset_sdu_rx(&chan_list[i]);
chan_list[i].handle = 0;
break;
}
} else {
if (chan_list[i].dcid == scid) {
+ reset_sdu_rx(&chan_list[i]);
chan_list[i].handle = 0;
break;
}
@@ -543,8 +573,11 @@ static char *mode2str(uint8_t mode)
}
}
-static void l2cap_ctrl_ext_parse(struct l2cap_frame *frame, uint32_t ctrl)
+static uint8_t l2cap_ctrl_ext_parse(struct l2cap_frame *frame, uint32_t ctrl,
+ uint16_t *sdu_len, uint16_t *txseq)
{
+ uint8_t sar = L2CAP_SAR_NONE;
+
display_printf(" %s:",
ctrl & L2CAP_EXT_CTRL_FRAME_TYPE ? "S-frame" : "I-frame");
@@ -556,20 +589,18 @@ static void l2cap_ctrl_ext_parse(struct l2cap_frame *frame, uint32_t ctrl)
if (ctrl & L2CAP_EXT_CTRL_POLL)
display_printf(" P-bit");
} else {
- uint8_t sar = (ctrl & L2CAP_EXT_CTRL_SAR_MASK) >>
+ sar = (ctrl & L2CAP_EXT_CTRL_SAR_MASK) >>
L2CAP_EXT_CTRL_SAR_SHIFT;
display_printf(" %s", sar2str(sar));
if (sar == L2CAP_SAR_START) {
- uint16_t len;
+ if (!l2cap_frame_get_le16(frame, sdu_len))
+ return L2CAP_SAR_NONE;
- if (!l2cap_frame_get_le16(frame, &len))
- return;
-
- display_printf(" (len %d)", len);
+ display_printf(" (len %d)", *sdu_len);
}
- display_printf(" TxSeq %d",
- (ctrl & L2CAP_EXT_CTRL_TXSEQ_MASK) >>
- L2CAP_EXT_CTRL_TXSEQ_SHIFT);
+ *txseq = (ctrl & L2CAP_EXT_CTRL_TXSEQ_MASK) >>
+ L2CAP_EXT_CTRL_TXSEQ_SHIFT;
+ display_printf(" TxSeq %d", *txseq);
}
display_printf(" ReqSeq %d", (ctrl & L2CAP_EXT_CTRL_REQSEQ_MASK) >>
@@ -577,10 +608,15 @@ static void l2cap_ctrl_ext_parse(struct l2cap_frame *frame, uint32_t ctrl)
if (ctrl & L2CAP_EXT_CTRL_FINAL)
display_printf(" F-bit");
+
+ return sar;
}
-static void l2cap_ctrl_parse(struct l2cap_frame *frame, uint32_t ctrl)
+static uint8_t l2cap_ctrl_parse(struct l2cap_frame *frame, uint32_t ctrl,
+ uint16_t *sdu_len, uint16_t *txseq)
{
+ uint8_t sar = L2CAP_SAR_NONE;
+
display_printf(" %s:",
ctrl & L2CAP_CTRL_FRAME_TYPE ? "S-frame" : "I-frame");
@@ -592,20 +628,17 @@ static void l2cap_ctrl_parse(struct l2cap_frame *frame, uint32_t ctrl)
if (ctrl & L2CAP_CTRL_POLL)
display_printf(" P-bit");
} else {
- uint8_t sar;
-
sar = (ctrl & L2CAP_CTRL_SAR_MASK) >> L2CAP_CTRL_SAR_SHIFT;
display_printf(" %s", sar2str(sar));
if (sar == L2CAP_SAR_START) {
- uint16_t len;
-
- if (!l2cap_frame_get_le16(frame, &len))
- return;
+ if (!l2cap_frame_get_le16(frame, sdu_len))
+ return L2CAP_SAR_NONE;
- display_printf(" (len %d)", len);
+ display_printf(" (len %d)", *sdu_len);
}
- display_printf(" TxSeq %d", (ctrl & L2CAP_CTRL_TXSEQ_MASK) >>
- L2CAP_CTRL_TXSEQ_SHIFT);
+ *txseq = (ctrl & L2CAP_CTRL_TXSEQ_MASK) >>
+ L2CAP_CTRL_TXSEQ_SHIFT;
+ display_printf(" TxSeq %d", *txseq);
}
display_printf(" ReqSeq %d", (ctrl & L2CAP_CTRL_REQSEQ_MASK) >>
@@ -613,6 +646,8 @@ static void l2cap_ctrl_parse(struct l2cap_frame *frame, uint32_t ctrl)
if (ctrl & L2CAP_CTRL_FINAL)
display_printf(" F-bit");
+
+ return sar;
}
#define MAX_INDEX 16
@@ -2775,6 +2810,117 @@ static void smp_packet(uint16_t index, bool in, uint16_t handle,
opcode_data->func(&frame);
}
+/* Reassemble ERTM/Streaming mode SAR fragments (Start/Continuation/End)
+ * into a single SDU before handing it to the upper layer dissector.
+ * Returns true when frame now holds a complete payload ready for
+ * dispatch (either because sar is the end of a multi-fragment SDU, or
+ * because reassembly could not be tracked and the single fragment
+ * should be dispatched as-is). Returns false while more fragments are
+ * still expected. *owned is set to true only when frame->data was
+ * replaced by a heap-allocated reassembled SDU that the caller must
+ * free; it is left untouched (false) whenever frame->data still
+ * points into the original (borrowed) packet buffer.
+ */
+static bool l2cap_sdu_reassemble(struct chan_data *chan, bool in,
+ uint8_t sar, uint16_t sdu_len,
+ uint16_t txseq, uint16_t seq_mod,
+ struct l2cap_frame *frame, bool *owned)
+{
+ struct l2cap_sdu *sdu;
+ uint32_t len;
+ uint16_t prev_seq;
+
+ *owned = false;
+
+ if (!chan)
+ return true;
+
+ sdu = &chan->sdu_rx[in ? 1 : 0];
+
+ if (sar == L2CAP_SAR_START) {
+ if (sdu->data) {
+ print_text(COLOR_ERROR,
+ "incomplete SDU replaced by new Start segment");
+ free(sdu->data);
+ }
+
+ sdu->len = 0;
+ sdu->total = sdu_len;
+ sdu->data = malloc(sdu_len);
+
+ if (!sdu->data) {
+ sdu->total = 0;
+ sdu->next_seq = -1;
+ return true;
+ }
+
+ sdu->next_seq = (txseq + 1) % seq_mod;
+ } else {
+ if (!sdu->data) {
+ print_text(COLOR_ERROR,
+ "unexpected continuation segment");
+ return true;
+ }
+
+ prev_seq = (sdu->next_seq + seq_mod - 1) % seq_mod;
+
+ if (sdu->next_seq >= 0 && txseq == prev_seq) {
+ /* Retransmitted duplicate of the last fragment
+ * already appended: discard it and keep waiting
+ * for the genuine next fragment.
+ */
+ print_text(COLOR_ERROR,
+ "duplicate segment TxSeq %u discarded",
+ txseq);
+ return false;
+ }
+
+ if (sdu->next_seq >= 0 && txseq != (uint16_t) sdu->next_seq) {
+ /* A gap means a fragment was lost (or capture
+ * started mid-SDU): the SDU can no longer be
+ * reassembled correctly, so discard it rather
+ * than silently producing a corrupted payload.
+ */
+ print_text(COLOR_ERROR,
+ "gap in reassembly (expected TxSeq %u, got"
+ " %u), discarding SDU", sdu->next_seq, txseq);
+ free(sdu->data);
+ sdu->data = NULL;
+ sdu->len = 0;
+ sdu->total = 0;
+ sdu->next_seq = -1;
+ return true;
+ }
+
+ sdu->next_seq = (txseq + 1) % seq_mod;
+ }
+
+ len = sdu->len + (uint32_t) frame->size;
+ if (len > sdu->total)
+ len = sdu->total;
+
+ if (len > sdu->len)
+ memcpy(sdu->data + sdu->len, frame->data, len - sdu->len);
+ sdu->len = len;
+
+ if (sar != L2CAP_SAR_END)
+ return false;
+
+ if (sdu->len != sdu->total)
+ print_text(COLOR_ERROR, "reassembled SDU length mismatch");
+
+ frame->data = sdu->data;
+ frame->size = sdu->len;
+ *owned = true;
+
+ sdu->data = NULL;
+ sdu->len = 0;
+ sdu->total = 0;
+ sdu->next_seq = -1;
+
+ return true;
+}
+
void l2cap_frame(uint16_t index, bool in, uint16_t handle, uint16_t cid,
uint16_t psm, const void *data, uint16_t size)
{
@@ -2783,6 +2929,13 @@ void l2cap_frame(uint16_t index, bool in, uint16_t handle, uint16_t cid,
uint32_t ctrl32 = 0;
uint16_t ctrl16 = 0;
uint8_t ext_ctrl;
+ uint8_t sar = L2CAP_SAR_UNSEGMENTED;
+ uint16_t sdu_len = 0;
+ uint16_t txseq = 0;
+ uint16_t seq_mod = 64;
+ uint16_t frag_len = size;
+ void *sdu_buf = NULL;
+ bool owned;
switch (cid) {
case 0x0001:
@@ -2849,7 +3002,9 @@ void l2cap_frame(uint16_t index, bool in, uint16_t handle, uint16_t cid,
frame.psm, mode2str(frame.mode),
frame.mode, frame.chan);
- l2cap_ctrl_ext_parse(&frame, ctrl32);
+ sar = l2cap_ctrl_ext_parse(&frame, ctrl32,
+ &sdu_len, &txseq);
+ seq_mod = 16384;
} else {
if (!l2cap_frame_get_le16(&frame, &ctrl16))
return;
@@ -2862,7 +3017,9 @@ void l2cap_frame(uint16_t index, bool in, uint16_t handle, uint16_t cid,
frame.psm, mode2str(frame.mode),
frame.mode, frame.chan);
- l2cap_ctrl_parse(&frame, ctrl16);
+ sar = l2cap_ctrl_parse(&frame, ctrl16,
+ &sdu_len, &txseq);
+ seq_mod = 64;
}
display_printf("\n");
@@ -2876,6 +3033,46 @@ void l2cap_frame(uint16_t index, bool in, uint16_t handle, uint16_t cid,
if (chan && chan->fcs && frame.size >= 2)
frame.size -= 2;
+ /* Size of just this I-frame's own payload, before
+ * it is possibly replaced by the full reassembled
+ * SDU below. Used so the OBEX dissector can limit
+ * fallback/binary hexdumps to the bytes carried by
+ * the final fragment, instead of repeating bytes
+ * already shown while earlier fragments (Start/
+ * Continuation) were buffered.
+ */
+ frag_len = frame.size;
+
+ if (sar != L2CAP_SAR_UNSEGMENTED &&
+ sar != L2CAP_SAR_NONE) {
+ if (!l2cap_sdu_reassemble(chan, frame.in, sar,
+ sdu_len, txseq,
+ seq_mod,
+ &frame,
+ &owned)) {
+ /* Still buffering (Start/Continue):
+ * show the raw fragment payload, the
+ * upper layer dissector only runs
+ * once the SDU is fully reassembled.
+ */
+ if (frame.size)
+ packet_hexdump(frame.data,
+ frame.size);
+ return;
+ }
+
+ if (owned)
+ sdu_buf = (void *) frame.data;
+ }
+
+ break;
+ }
+
+ /* S-frames (supervisory, e.g. RR/REJ) carry no SDU payload
+ * and must not be handed to upper layer dissectors.
+ */
+ if (sar == L2CAP_SAR_NONE) {
+ free(sdu_buf);
break;
}
@@ -2905,11 +3102,13 @@ void l2cap_frame(uint16_t index, bool in, uint16_t handle, uint16_t cid,
default:
if (obex_psm(index, handle, frame.psm))
obex_packet(&frame, l2cap_chan_key(index, in,
- handle, cid));
+ handle, cid), frag_len);
else
packet_hexdump(data, size);
break;
}
+
+ free(sdu_buf);
break;
}
}
diff --git a/monitor/obex.c b/monitor/obex.c
index 6e69412ff..14f471c62 100644
--- a/monitor/obex.c
+++ b/monitor/obex.c
@@ -1018,6 +1018,36 @@ static void print_unicode_header(const char *label, const uint8_t *data,
print_field("%*c%s: %s", indent, ' ', label, buf);
}
+/* When an OBEX PDU was reassembled from multiple L2CAP SAR fragments,
+ * the Start/Continuation fragments have already been hexdumped as raw
+ * bytes by the L2CAP layer while they were buffered. Limit hexdumps
+ * done while decoding the fully reassembled PDU to the bytes carried
+ * by the final (End) fragment, so the same bytes aren't printed twice.
+ * base is the start of the reassembled PDU and from is the byte offset
+ * (from base) where the final fragment begins, or 0 when the whole PDU
+ * came from a single, unsegmented frame.
+ */
+static void obex_hexdump(const uint8_t *base, uint16_t from,
+ const uint8_t *data, uint16_t len)
+{
+ uint32_t offset;
+
+ if (!len)
+ return;
+
+ offset = data - base;
+
+ if (offset + len <= from)
+ return;
+
+ if (offset < from) {
+ len -= from - offset;
+ data += from - offset;
+ }
+
+ packet_hexdump(data, len);
+}
+
/* Application Parameters (Tag/Len/Value) tag tables for profiles whose
* tag assignments are known. Tag IDs and names are taken from the
* PBAP/MAP/BIP specifications; types indicate how to decode a tag's
@@ -1196,7 +1226,10 @@ static void obex_print_apparam_tag(enum obex_profile profile, uint8_t tag,
/* Known tag but unexpected length (or a raw-bytes type):
* still show the tag's name, with a length + hexdump
- * instead of a decoded value.
+ * instead of a decoded value. Application Parameters are
+ * small labeled values (not bulk payload like Body), so
+ * always show them in full instead of clipping to the
+ * current SAR fragment.
*/
print_field("%*c%s: [len %d]", indent, ' ', table[i].str,
len);
@@ -1232,7 +1265,8 @@ static void obex_print_apparam(const uint8_t *data, uint16_t size,
}
static bool obex_print_headers(struct l2cap_frame *frame, uint8_t indent,
- uint16_t channel)
+ uint16_t channel, const uint8_t *base,
+ uint16_t dump_from)
{
while (frame->size > 0) {
uint8_t hdr_id;
@@ -1301,6 +1335,16 @@ static bool obex_print_headers(struct l2cap_frame *frame, uint8_t indent,
obex_get_profile(frame->index,
frame->handle,
channel));
+ else if (vlen && (hdr_id == OBEX_HDR_BODY ||
+ hdr_id == OBEX_HDR_BODY_END))
+ /* Body/End-of-Body can carry large,
+ * reassembled payloads spanning
+ * several SAR fragments already
+ * hexdumped individually; clip to
+ * avoid duplicating those bytes.
+ */
+ obex_hexdump(base, dump_from, data,
+ vlen);
else if (vlen)
packet_hexdump(data, vlen);
}
@@ -1366,7 +1410,8 @@ static bool obex_setpath_fields(struct l2cap_frame *frame, uint8_t indent)
return true;
}
-void obex_packet(const struct l2cap_frame *frame, uint16_t channel)
+void obex_packet(const struct l2cap_frame *frame, uint16_t channel,
+ uint16_t frag_len)
{
struct l2cap_frame obex_frame;
struct l2cap_frame *f = &obex_frame;
@@ -1377,9 +1422,15 @@ void obex_packet(const struct l2cap_frame *frame, uint16_t channel)
bool is_request = false;
const uint8_t *trailer;
uint16_t trailer_len;
+ const uint8_t *dump_base;
+ uint16_t dump_from;
l2cap_frame_pull(f, frame, 0);
+ dump_base = frame->data;
+ dump_from = (frag_len < frame->size) ?
+ frame->size - frag_len : 0;
+
if (f->size < 3)
goto fail;
@@ -1440,7 +1491,7 @@ void obex_packet(const struct l2cap_frame *frame, uint16_t channel)
goto fail;
}
- if (!obex_print_headers(f, indent, channel))
+ if (!obex_print_headers(f, indent, channel, dump_base, dump_from))
goto fail;
if (is_request)
@@ -1450,12 +1501,12 @@ void obex_packet(const struct l2cap_frame *frame, uint16_t channel)
if (trailer_len) {
print_field("%*cTrailing data: [len %d]", indent, ' ',
trailer_len);
- packet_hexdump(trailer, trailer_len);
+ obex_hexdump(dump_base, dump_from, trailer, trailer_len);
}
return;
fail:
print_text(COLOR_ERROR, "frame too short");
- packet_hexdump(frame->data, frame->size);
+ obex_hexdump(dump_base, dump_from, frame->data, frame->size);
}
diff --git a/monitor/obex.h b/monitor/obex.h
index 5af28ea8b..019ed6174 100644
--- a/monitor/obex.h
+++ b/monitor/obex.h
@@ -101,7 +101,8 @@
#define OBEX_HDR_TYPE_UINT8 0x80
#define OBEX_HDR_TYPE_UINT32 0xc0
-void obex_packet(const struct l2cap_frame *frame, uint16_t channel);
+void obex_packet(const struct l2cap_frame *frame, uint16_t channel,
+ uint16_t frag_len);
void obex_add_channel(uint16_t index, uint16_t handle, uint8_t channel,
const char *profile);
diff --git a/monitor/rfcomm.c b/monitor/rfcomm.c
index 89cbf6087..d830684a9 100644
--- a/monitor/rfcomm.c
+++ b/monitor/rfcomm.c
@@ -405,7 +405,7 @@ static bool uih_frame(struct rfcomm_frame *rfcomm_frame, uint8_t indent)
}
if (obex_channel(frame->index, frame->handle, channel, NULL))
- obex_packet(frame, channel);
+ obex_packet(frame, channel, frame->size);
else
packet_hexdump(frame->data, frame->size);
--
2.43.0
^ permalink raw reply related [flat|nested] 12+ messages in thread* [PATCH BlueZ v4 5/6] monitor: Fix RFCOMM UIH frame length and credit-only handling
2026-11-24 14:56 [PATCH BlueZ v4 0/6] monitor: Add an OBEX protocol decoder to btmon Frédéric Danis
` (3 preceding siblings ...)
2026-11-24 14:56 ` [PATCH BlueZ v4 4/6] monitor: Reassemble L2CAP ERTM/Streaming SDUs Frédéric Danis
@ 2026-11-24 14:56 ` Frédéric Danis
2026-11-24 14:56 ` [PATCH BlueZ v4 6/6] monitor: Reassemble OBEX PDUs split across RFCOMM UIH frames Frédéric Danis
5 siblings, 0 replies; 12+ messages in thread
From: Frédéric Danis @ 2026-11-24 14:56 UTC (permalink / raw)
To: linux-bluetooth
rfcomm_packet() consumed the trailing 1-byte FCS via a separate
tmp_frame view over the same bytes, but never trimmed it from
l2cap_frame itself, so the FCS byte was still included in the
payload handed to uih_frame()/mcc_frame(). For OBEX traffic this
showed up as a spurious trailing byte once the OBEX decoder started
enforcing PDU lengths strictly; drop the byte from l2cap_frame->size
right after it is read.
uih_frame() also handed credit-only UIH frames (an RFCOMM credit flow
control update carrying no information payload, size 0 once the
optional credits octet is consumed) to the OBEX channel check and
decoder, which could misdecode them as a 0-length OBEX PDU. Skip
decoding entirely when no payload bytes remain.
Assisted-by: Claude:claude-sonnet-5
---
v2-v3: New in v3.
monitor/rfcomm.c | 13 +++++++++++++
1 file changed, 13 insertions(+)
diff --git a/monitor/rfcomm.c b/monitor/rfcomm.c
index d830684a9..8a44a2199 100644
--- a/monitor/rfcomm.c
+++ b/monitor/rfcomm.c
@@ -404,6 +404,12 @@ static bool uih_frame(struct rfcomm_frame *rfcomm_frame, uint8_t indent)
print_field("%*cCredits: %d", indent, ' ', hdr->credits);
}
+ /* A credit-only UIH frame (credit-based flow control update)
+ * carries no information payload; nothing to decode.
+ */
+ if (!frame->size)
+ return true;
+
if (obex_channel(frame->index, frame->handle, channel, NULL))
obex_packet(frame, channel, frame->size);
else
@@ -467,6 +473,13 @@ void rfcomm_packet(const struct l2cap_frame *frame)
if (!l2cap_frame_get_u8(&tmp_frame, &hdr.fcs))
goto fail;
+ /* The trailing FCS byte just consumed via tmp_frame is still
+ * included in l2cap_frame's size (tmp_frame is a separate view
+ * over the same bytes). Drop it so the information payload
+ * handed to uih_frame()/mcc_frame() doesn't include it.
+ */
+ l2cap_frame->size -= 1;
+
/* Decoding frame type */
ctype = RFCOMM_GET_TYPE(hdr.control);
--
2.43.0
^ permalink raw reply related [flat|nested] 12+ messages in thread* [PATCH BlueZ v4 6/6] monitor: Reassemble OBEX PDUs split across RFCOMM UIH frames
2026-11-24 14:56 [PATCH BlueZ v4 0/6] monitor: Add an OBEX protocol decoder to btmon Frédéric Danis
` (4 preceding siblings ...)
2026-11-24 14:56 ` [PATCH BlueZ v4 5/6] monitor: Fix RFCOMM UIH frame length and credit-only handling Frédéric Danis
@ 2026-11-24 14:56 ` Frédéric Danis
5 siblings, 0 replies; 12+ messages in thread
From: Frédéric Danis @ 2026-11-24 14:56 UTC (permalink / raw)
To: linux-bluetooth
The RFCOMM MTU is typically much smaller than the maximum OBEX PDU
length, so a single OBEX PDU can be split across several UIH frames.
uih_frame() decoded each UIH frame's payload independently, so any
OBEX PDU spanning more than one frame failed to parse ('frame too
short').
Buffer UIH information bytes per index/handle/channel/direction
(obex_frag_list[]/obex_frag_get()) until a complete PDU (3-byte
opcode+length header plus body) is available, then decode it and
keep any leftover bytes for the next frame. Drop the buffer for a
channel when it is torn down (rfcomm_remove_channel(), called
alongside obex_remove_channel() on DISC/DM) so a later renegotiation
of the same channel number doesn't resume from stale fragments.
Assisted-by: Claude:claude-sonnet-5
---
v2->v3: New in v3.
v3->v4:
- Add rfcomm_remove_handle(), called from the HCI disconnect
complete handler, to free every fragment buffer still tracked for
an ACL handle that goes away without an RFCOMM DISC/DM on each of
its channels.
- Widen the reassembly buffer's length/position tracking from
uint16_t to uint32_t, and clamp the amount handed to obex_packet()
on a malformed declared length to l2cap_frame.size's uint16_t
range, keeping any excess buffered for the next call instead of
reporting an incorrect remaining size.
monitor/l2cap.h | 2 +
monitor/packet.c | 1 +
monitor/rfcomm.c | 167 ++++++++++++++++++++++++++++++++++++++++++++++-
3 files changed, 167 insertions(+), 3 deletions(-)
diff --git a/monitor/l2cap.h b/monitor/l2cap.h
index 1c1f7e609..6cfc25f8c 100644
--- a/monitor/l2cap.h
+++ b/monitor/l2cap.h
@@ -364,4 +364,6 @@ void l2cap_packet(uint16_t index, bool in, uint16_t handle, uint8_t flags,
void rfcomm_packet(const struct l2cap_frame *frame);
+void rfcomm_remove_handle(uint16_t index, uint16_t handle);
+
void l2cap_dequeue_frame(struct timeval *delta, struct packet_conn_data *conn);
diff --git a/monitor/packet.c b/monitor/packet.c
index 7414910c7..ca68c54ae 100644
--- a/monitor/packet.c
+++ b/monitor/packet.c
@@ -12315,6 +12315,7 @@ static void disconnect_complete_evt(struct timeval *tv, uint16_t index,
if (evt->status == 0x00) {
release_handle(le16_to_cpu(evt->handle));
obex_disconnect_handle(index, le16_to_cpu(evt->handle));
+ rfcomm_remove_handle(index, le16_to_cpu(evt->handle));
}
}
diff --git a/monitor/rfcomm.c b/monitor/rfcomm.c
index 8a44a2199..a17d7acfa 100644
--- a/monitor/rfcomm.c
+++ b/monitor/rfcomm.c
@@ -386,12 +386,112 @@ static inline bool mcc_frame(struct rfcomm_frame *rfcomm_frame, uint8_t indent)
return true;
}
+/* An OBEX PDU may be split across several RFCOMM UIH frames since the
+ * RFCOMM MTU is typically much smaller than the maximum OBEX PDU
+ * length. Information bytes are buffered per index/handle/channel and
+ * direction until a complete PDU (3-byte opcode+length header plus
+ * body) is available; complete PDUs are then decoded and any leftover
+ * bytes are kept for the next frame.
+ */
+#define MAX_OBEX_FRAG 64
+
+struct obex_frag_data {
+ uint16_t index;
+ uint16_t handle;
+ uint8_t channel;
+ bool in;
+ uint8_t *buf;
+ uint32_t len;
+};
+
+static struct obex_frag_data obex_frag_list[MAX_OBEX_FRAG];
+
+static struct obex_frag_data *obex_frag_get(uint16_t index, uint16_t handle,
+ uint8_t channel, bool in)
+{
+ int i, n = -1;
+
+ for (i = 0; i < MAX_OBEX_FRAG; i++) {
+ if (!obex_frag_list[i].channel) {
+ if (n < 0)
+ n = i;
+ continue;
+ }
+
+ if (obex_frag_list[i].index == index &&
+ obex_frag_list[i].handle == handle &&
+ obex_frag_list[i].channel == channel &&
+ obex_frag_list[i].in == in)
+ return &obex_frag_list[i];
+ }
+
+ if (n < 0)
+ return NULL;
+
+ obex_frag_list[n].index = index;
+ obex_frag_list[n].handle = handle;
+ obex_frag_list[n].channel = channel;
+ obex_frag_list[n].in = in;
+ obex_frag_list[n].buf = NULL;
+ obex_frag_list[n].len = 0;
+
+ return &obex_frag_list[n];
+}
+
+static void obex_frag_reset(struct obex_frag_data *frag)
+{
+ free(frag->buf);
+ frag->buf = NULL;
+ frag->len = 0;
+}
+
+static void rfcomm_remove_channel(uint16_t index, uint16_t handle,
+ uint8_t channel)
+{
+ int i;
+
+ for (i = 0; i < MAX_OBEX_FRAG; i++) {
+ if (obex_frag_list[i].channel == channel &&
+ obex_frag_list[i].index == index &&
+ obex_frag_list[i].handle == handle) {
+ obex_frag_reset(&obex_frag_list[i]);
+ memset(&obex_frag_list[i], 0,
+ sizeof(obex_frag_list[i]));
+ }
+ }
+}
+
+/* An ACL handle may be disconnected without an RFCOMM DISC/DM on each
+ * of its channels (e.g. the link is simply dropped), which would
+ * otherwise leave both directions' reassembly buffers and their slots
+ * allocated. Free every fragment buffer still tracked for this handle.
+ */
+void rfcomm_remove_handle(uint16_t index, uint16_t handle)
+{
+ int i;
+
+ for (i = 0; i < MAX_OBEX_FRAG; i++) {
+ if (!obex_frag_list[i].channel)
+ continue;
+
+ if (obex_frag_list[i].index == index &&
+ obex_frag_list[i].handle == handle) {
+ obex_frag_reset(&obex_frag_list[i]);
+ memset(&obex_frag_list[i], 0,
+ sizeof(obex_frag_list[i]));
+ }
+ }
+}
+
static bool uih_frame(struct rfcomm_frame *rfcomm_frame, uint8_t indent)
{
uint8_t credits;
struct l2cap_frame *frame = &rfcomm_frame->l2cap_frame;
struct rfcomm_lhdr *hdr = &rfcomm_frame->hdr;
uint8_t channel = RFCOMM_GET_CHANNEL(hdr->address);
+ struct obex_frag_data *frag;
+ uint8_t *buf;
+ uint32_t pos;
if (!channel)
return mcc_frame(rfcomm_frame, indent);
@@ -410,10 +510,69 @@ static bool uih_frame(struct rfcomm_frame *rfcomm_frame, uint8_t indent)
if (!frame->size)
return true;
- if (obex_channel(frame->index, frame->handle, channel, NULL))
- obex_packet(frame, channel, frame->size);
- else
+ if (!obex_channel(frame->index, frame->handle, channel, NULL)) {
packet_hexdump(frame->data, frame->size);
+ return true;
+ }
+
+ frag = obex_frag_get(frame->index, frame->handle, channel, frame->in);
+ if (!frag) {
+ obex_packet(frame, channel, frame->size);
+ return true;
+ }
+
+ buf = realloc(frag->buf, frag->len + frame->size);
+ if (!buf) {
+ obex_frag_reset(frag);
+ obex_packet(frame, channel, frame->size);
+ return true;
+ }
+
+ memcpy(buf + frag->len, frame->data, frame->size);
+ frag->buf = buf;
+ frag->len += frame->size;
+
+ pos = 0;
+
+ while (frag->len - pos >= 3) {
+ struct l2cap_frame pdu_frame = *frame;
+ uint16_t pdu_len = get_be16(frag->buf + pos + 1);
+
+ if (pdu_len < 3) {
+ /* Malformed declared length: hand over as much of
+ * the rest of the buffer as fits in l2cap_frame.size
+ * (uint16_t) and let obex_packet() report the error,
+ * rather than buffering indefinitely. Any excess
+ * stays buffered and is retried on the next call.
+ */
+ uint32_t remaining = frag->len - pos;
+
+ if (remaining > UINT16_MAX)
+ remaining = UINT16_MAX;
+
+ pdu_frame.data = frag->buf + pos;
+ pdu_frame.size = remaining;
+ obex_packet(&pdu_frame, channel, pdu_frame.size);
+ pos += remaining;
+ break;
+ }
+
+ if (frag->len - pos < pdu_len)
+ break;
+
+ pdu_frame.data = frag->buf + pos;
+ pdu_frame.size = pdu_len;
+ obex_packet(&pdu_frame, channel, pdu_len);
+
+ pos += pdu_len;
+ }
+
+ if (pos == frag->len) {
+ obex_frag_reset(frag);
+ } else if (pos) {
+ memmove(frag->buf, frag->buf + pos, frag->len - pos);
+ frag->len -= pos;
+ }
return true;
}
@@ -523,6 +682,8 @@ void rfcomm_packet(const struct l2cap_frame *frame)
*/
obex_remove_channel(frame->index, frame->handle,
RFCOMM_GET_CHANNEL(hdr.address));
+ rfcomm_remove_channel(frame->index, frame->handle,
+ RFCOMM_GET_CHANNEL(hdr.address));
}
return;
--
2.43.0
^ permalink raw reply related [flat|nested] 12+ messages in thread