All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH BlueZ 0/4] monitor: Add an OBEX protocol decoder to btmon
@ 2026-11-23 10:25 Frédéric Danis
  2026-11-23 10:25 ` [PATCH BlueZ 1/4] monitor: Strip trailing L2CAP FCS before upper layer dissectors Frédéric Danis
                   ` (3 more replies)
  0 siblings, 4 replies; 13+ messages in thread
From: Frédéric Danis @ 2026-11-23 10:25 UTC (permalink / raw)
  To: linux-bluetooth

From: Frédéric Danis <frederic.danis.oss@gmail.com>

btmon currently hexdumps all OBEX traffic (RFCOMM and L2CAP GOEP), making
PBAP/MAP/BIP/OPP sessions hard to follow in a trace. This series adds a
dedicated OBEX dissector (monitor/obex.c) and wires it into the RFCOMM,
L2CAP and SDP monitors.

Patch 1 adds the core decoder: opcodes, response codes, CONNECT/SETPATH
fixed fields, all generic header IDs, BIP's IMG-Handle/IMG-Descriptor
extension headers, and Target/Who UUID recognition for the well known
OBEX profiles.
It tracks sessions on both RFCOMM (learned from SDP
ProtocolDescriptorList) and L2CAP (fixed PSM 0x1001 or a dynamically
negotiated one learned from the GoepL2capPsm SDP attribute), keyed
direction-independently so CONNECT requests and responses are correctly
matched to the same session.

Patch 2 adds per-profile Application Parameters (Tag/Len/Value) decoding
for PBAP, MAP and BIP, replacing the generic 'Tag 0xNN len N' hexdump with
named, typed values, using the profile learned from each session's
Target/Who header.

Patch 3 adds SDU reassembly for L2CAP ERTM/Streaming mode, since OBEX PDUs
routinely span multiple SAR fragments; without it, any segmented PDU
failed to parse ('frame too short'). Reassembled PDUs are decoded once, on
the End fragment, while raw per-fragment hexdumps are kept for
Start/Continuation fragments so no bytes are lost from the trace.

Sample decode, a PBAP CONNECT followed by a GET with Application
Parameters:

OBEX: Connect (0x00) final 1 len 35
   Version: 1.0
   Flags: 0x00
   Max Packet Length: 8087
   Target: Phonebook Access (PBAP)
   Application Parameters: [len 6]
     Supported Features: 1023 (0x000003ff)
...
OBEX: Get (0x03) final 1 len 87
   Connection ID: 1 (0x00000001)
   Single Response Mode: 1 (0x01)
   Name: telecom/fav.vcf
   Type: x-bt/phonebook
   Application Parameters: [len 21]
     Format: 1 (0x01)
     Max List Count: 65535 (0xffff)
     Filter: 0x0000000018b111af
     List Start Offset: 0 (0x0000)

Frédéric Danis (4):
  monitor: Strip trailing L2CAP FCS before upper layer dissectors
  monitor: Add OBEX protocol decoder
  monitor: Decode PBAP/MAP/BIP Application Parameters tags
  monitor: Reassemble L2CAP ERTM/Streaming SAR fragments before
    dissecting

 Makefile.tools   |    1 +
 monitor/l2cap.c  |  246 +++++++-
 monitor/obex.c   | 1390 ++++++++++++++++++++++++++++++++++++++++++++++
 monitor/obex.h   |  119 ++++
 monitor/packet.c |    5 +-
 monitor/rfcomm.c |   20 +-
 monitor/sdp.c    |   12 +-
 7 files changed, 1768 insertions(+), 25 deletions(-)
 create mode 100644 monitor/obex.c
 create mode 100644 monitor/obex.h

-- 
2.43.0


^ permalink raw reply	[flat|nested] 13+ messages in thread
* [PATCH BlueZ v2 1/4] monitor: Strip trailing L2CAP FCS before upper layer dissectors
@ 2026-11-23 14:30 Frédéric Danis
  2026-11-24  2:05 ` monitor: Add an OBEX protocol decoder to btmon bluez.test.bot
  0 siblings, 1 reply; 13+ messages in thread
From: Frédéric Danis @ 2026-11-23 14:30 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
---
 monitor/l2cap.c | 51 +++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 51 insertions(+)

diff --git a/monitor/l2cap.c b/monitor/l2cap.c
index ddc2388d5..a1c90af1d 100644
--- a/monitor/l2cap.c
+++ b/monitor/l2cap.c
@@ -100,6 +100,7 @@ struct chan_data {
 	uint8_t  ext_ctrl;
 	uint8_t  seq_num;
 	uint16_t sdu;
+	uint8_t  fcs;
 	struct packet_latency tx_l;
 };
 
@@ -158,6 +159,12 @@ 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].seq_num = seq_num;
 }
 
@@ -255,6 +262,37 @@ 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;
+			}
+		}
+	}
+}
+
 static int get_chan_data_index(const struct l2cap_frame *frame)
 {
 	int i;
@@ -882,6 +920,9 @@ 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);
+
 			switch (data[consumed + 2]) {
 			case 0x00:
 				str = "No FCS";
@@ -2740,6 +2781,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] 13+ messages in thread
* [PATCH BlueZ v3 1/6] monitor: Strip trailing L2CAP FCS before upper layer dissectors
@ 2026-11-24  9:22 Frédéric Danis
  2026-11-24 11:38 ` monitor: Add an OBEX protocol decoder to btmon bluez.test.bot
  0 siblings, 1 reply; 13+ messages in thread
From: Frédéric Danis @ 2026-11-24  9:22 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
---
 monitor/l2cap.c | 51 +++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 51 insertions(+)

diff --git a/monitor/l2cap.c b/monitor/l2cap.c
index ddc2388d5..a1c90af1d 100644
--- a/monitor/l2cap.c
+++ b/monitor/l2cap.c
@@ -100,6 +100,7 @@ struct chan_data {
 	uint8_t  ext_ctrl;
 	uint8_t  seq_num;
 	uint16_t sdu;
+	uint8_t  fcs;
 	struct packet_latency tx_l;
 };
 
@@ -158,6 +159,12 @@ 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].seq_num = seq_num;
 }
 
@@ -255,6 +262,37 @@ 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;
+			}
+		}
+	}
+}
+
 static int get_chan_data_index(const struct l2cap_frame *frame)
 {
 	int i;
@@ -882,6 +920,9 @@ 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);
+
 			switch (data[consumed + 2]) {
 			case 0x00:
 				str = "No FCS";
@@ -2740,6 +2781,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] 13+ messages in thread
* [PATCH BlueZ v4 1/6] monitor: Strip trailing L2CAP FCS before upper layer dissectors
@ 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
  0 siblings, 1 reply; 13+ 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] 13+ messages in thread
* [PATCH BlueZ v5 1/6] monitor: Strip trailing L2CAP FCS before upper layer dissectors
@ 2026-11-25 13:37 Frédéric Danis
  2026-11-25 16:15 ` monitor: Add an OBEX protocol decoder to btmon bluez.test.bot
  0 siblings, 1 reply; 13+ messages in thread
From: Frédéric Danis @ 2026-11-25 13:37 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.

Discard a channel's pending "No FCS" request if its Config Request is
rejected with Unknown Options, so a later, unrelated successful Config
Response in the same renegotiation doesn't apply the rejected value
and wrongly stop stripping the FCS.

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.
v4->v5:
  - Discard a pending requested FCS value when its Config Request is
    rejected with Unknown Options (discard_req_fcs()), so a later,
    unrelated successful Config Response in the same renegotiation
    doesn't apply the rejected value.

 monitor/l2cap.c | 163 +++++++++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 161 insertions(+), 2 deletions(-)

diff --git a/monitor/l2cap.c b/monitor/l2cap.c
index ddc2388d5..62c7641ed 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,131 @@ 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;
+			}
+		}
+	}
+}
+
+/* Discard a pending FCS value requested by a Config Request that is
+ * then rejected with Unknown Options, so a later, unrelated
+ * successful Config Response in the same renegotiation (sent without
+ * repeating the option) doesn't apply the rejected value. Left alone
+ * on a continuation response, since the rejection there isn't final
+ * and a later fragment of the same response still carries the result
+ * that matters.
+ */
+static void discard_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) {
+				chan_list[i].req_fcs = -1;
+				break;
+			}
+		} else {
+			if (chan_list[i].dcid == scid) {
+				chan_list[i].req_fcs = -1;
+				break;
+			}
+		}
+	}
+}
+
 static int get_chan_data_index(const struct l2cap_frame *frame)
 {
 	int i;
@@ -882,6 +1016,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 +1327,20 @@ 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) {
+		/* Unknown Options: the rejected Config Request (and any
+		 * FCS value it proposed) won't take effect, unless this
+		 * is a non-final fragment of a continued response, in
+		 * which case the result isn't final yet.
+		 */
+		if (!(le16_to_cpu(pdu->flags) & 0x0001))
+			discard_req_fcs(frame, le16_to_cpu(pdu->scid));
 		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 +2889,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] 13+ messages in thread
* [PATCH BlueZ v6 1/6] monitor: Strip trailing L2CAP FCS before upper layer dissectors
@ 2026-11-30  9:10 Frédéric Danis
  2026-11-30 10:44 ` monitor: Add an OBEX protocol decoder to btmon bluez.test.bot
  0 siblings, 1 reply; 13+ messages in thread
From: Frédéric Danis @ 2026-11-30  9:10 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.

Discard a channel's pending "No FCS" request if its Config Response
ends in any final failure result (Unacceptable Parameters, Rejected,
Unknown Options, Flow Spec Rejected), so a later, unrelated successful
Config Response in the same renegotiation doesn't apply the rejected
value and wrongly stop stripping the FCS; leave it alone while the
result is still Pending or the response is a non-final fragment.

Only apply a Config Response's FCS option on a definitively successful
result, instead of on any response, so a failed negotiation doesn't
change the channel's tracked FCS mode.

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.
v4->v5:
  - Discard a pending requested FCS value when its Config Request is
    rejected with Unknown Options (discard_req_fcs()), so a later,
    unrelated successful Config Response in the same renegotiation
    doesn't apply the rejected value.
v5->v6:
  - Extend discard_req_fcs() to any final failure result (Unacceptable
    Parameters, Rejected, Flow Spec Rejected), not just Unknown
    Options, leaving the pending value alone while the result is
    Pending or the response is a non-final fragment.
  - Only apply a Config Response's FCS option (print_config_options())
    on a definitively successful result, instead of on any response,
    so a failed negotiation doesn't change the channel's tracked FCS
    mode.

 monitor/l2cap.c | 189 ++++++++++++++++++++++++++++++++++++++++++++++--
 1 file changed, 184 insertions(+), 5 deletions(-)

diff --git a/monitor/l2cap.c b/monitor/l2cap.c
index ddc2388d5..558234a67 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,131 @@ 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;
+			}
+		}
+	}
+}
+
+/* Discard a pending FCS value requested by a Config Request that ends
+ * in a final failure response (Unacceptable Parameters, Rejected,
+ * Unknown Options, Flow Spec Rejected), so a later, unrelated
+ * successful Config Response in the same renegotiation (sent without
+ * repeating the option) doesn't apply the rejected value. Left alone
+ * on Pending or on a continuation response, since the result isn't
+ * final yet there.
+ */
+static void discard_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) {
+				chan_list[i].req_fcs = -1;
+				break;
+			}
+		} else {
+			if (chan_list[i].dcid == scid) {
+				chan_list[i].req_fcs = -1;
+				break;
+			}
+		}
+	}
+}
+
 static int get_chan_data_index(const struct l2cap_frame *frame)
 {
 	int i;
@@ -797,7 +931,8 @@ static void print_unknown_options(const struct l2cap_frame *source,
 }
 
 static void print_config_options(const struct l2cap_frame *frame,
-				uint8_t offset, uint16_t cid, bool response)
+				uint8_t offset, uint16_t cid, bool response,
+				bool success)
 {
 	const uint8_t *data = frame->data + offset;
 	uint16_t size = frame->size - offset;
@@ -882,6 +1017,18 @@ static void print_config_options(const struct l2cap_frame *frame,
 					get_le16(data + consumed + 9));
 			break;
 		case 0x05:
+			if (response) {
+				/* Only a successful result accepts the
+				 * proposed FCS value; a failed response
+				 * (e.g. Unacceptable Parameters, Pending)
+				 * doesn't change the channel's FCS mode.
+				 */
+				if (success)
+					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";
@@ -1178,20 +1325,42 @@ static void sig_config_req(const struct l2cap_frame *frame)
 
 	print_cid("Destination", pdu->dcid);
 	print_config_flags(pdu->flags);
-	print_config_options(frame, 4, le16_to_cpu(pdu->dcid), false);
+	print_config_options(frame, 4, le16_to_cpu(pdu->dcid), false, false);
 }
 
 static void sig_config_rsp(const struct l2cap_frame *frame)
 {
 	const struct bt_l2cap_pdu_config_rsp *pdu = frame->data;
+	bool final = !(le16_to_cpu(pdu->flags) & 0x0001);
+	bool success = pdu->result == 0x0000;
 
 	print_cid("Source", pdu->scid);
 	print_config_flags(pdu->flags);
 	print_config_result(pdu->result);
-	if (pdu->result == 0x0003)
+	if (pdu->result == 0x0003) {
+		/* Unknown Options: the rejected Config Request (and any
+		 * FCS value it proposed) won't take effect, unless this
+		 * is a non-final fragment of a continued response, in
+		 * which case the result isn't final yet.
+		 */
+		if (final)
+			discard_req_fcs(frame, le16_to_cpu(pdu->scid));
 		print_unknown_options(frame, 6);
-	else
-		print_config_options(frame, 6, le16_to_cpu(pdu->scid), true);
+	} else {
+		/* Any other final failure result (Unacceptable Parameters,
+		 * Rejected, Flow Spec Rejected) also means the proposed FCS
+		 * value won't take effect; only Pending leaves it undecided
+		 * until a later, final response.
+		 */
+		if (final) {
+			if (success)
+				apply_req_fcs(frame, le16_to_cpu(pdu->scid));
+			else if (pdu->result != 0x0004)
+				discard_req_fcs(frame, le16_to_cpu(pdu->scid));
+		}
+		print_config_options(frame, 6, le16_to_cpu(pdu->scid), true,
+								success);
+	}
 }
 
 static void sig_disconn_req(const struct l2cap_frame *frame)
@@ -2740,6 +2909,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] 13+ messages in thread

end of thread, other threads:[~2026-12-01 15:52 UTC | newest]

Thread overview: 13+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-23 10:25 [PATCH BlueZ 0/4] monitor: Add an OBEX protocol decoder to btmon Frédéric Danis
2026-11-23 10:25 ` [PATCH BlueZ 1/4] monitor: Strip trailing L2CAP FCS before upper layer dissectors Frédéric Danis
2026-11-23 14:15   ` monitor: Add an OBEX protocol decoder to btmon bluez.test.bot
2026-11-23 15:00     ` Luiz Augusto von Dentz
2026-11-23 10:25 ` [PATCH BlueZ 2/4] monitor: Add OBEX protocol decoder Frédéric Danis
2026-11-23 10:25 ` [PATCH BlueZ 3/4] monitor: Decode PBAP/MAP/BIP Application Parameters tags Frédéric Danis
2026-11-23 10:25 ` [PATCH BlueZ 4/4] monitor: Reassemble L2CAP ERTM/Streaming SAR fragments before dissecting Frédéric Danis
  -- strict thread matches above, loose matches on Subject: below --
2026-11-23 14:30 [PATCH BlueZ v2 1/4] monitor: Strip trailing L2CAP FCS before upper layer dissectors Frédéric Danis
2026-11-24  2:05 ` monitor: Add an OBEX protocol decoder to btmon bluez.test.bot
2026-11-24  9:22 [PATCH BlueZ v3 1/6] monitor: Strip trailing L2CAP FCS before upper layer dissectors Frédéric Danis
2026-11-24 11:38 ` monitor: Add an OBEX protocol decoder to btmon bluez.test.bot
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 ` monitor: Add an OBEX protocol decoder to btmon bluez.test.bot
2026-11-25 13:37 [PATCH BlueZ v5 1/6] monitor: Strip trailing L2CAP FCS before upper layer dissectors Frédéric Danis
2026-11-25 16:15 ` monitor: Add an OBEX protocol decoder to btmon bluez.test.bot
2026-11-30  9:10 [PATCH BlueZ v6 1/6] monitor: Strip trailing L2CAP FCS before upper layer dissectors Frédéric Danis
2026-11-30 10:44 ` monitor: Add an OBEX protocol decoder to btmon bluez.test.bot
2026-12-01 15:52   ` Luiz Augusto von Dentz

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.