Linux bluetooth development
 help / color / mirror / Atom feed
From: "Frédéric Danis" <frederic.danis@collabora.com>
To: linux-bluetooth@vger.kernel.org
Subject: [PATCH BlueZ v3 6/6] monitor: Reassemble OBEX PDUs split across RFCOMM UIH frames
Date: Tue, 24 Nov 2026 10:22:47 +0100	[thread overview]
Message-ID: <20261124092248.207565-7-frederic.danis@collabora.com> (raw)
In-Reply-To: <20261124092248.207565-1-frederic.danis@collabora.com>

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.

 monitor/rfcomm.c | 138 +++++++++++++++++++++++++++++++++++++++++++++--
 1 file changed, 135 insertions(+), 3 deletions(-)

diff --git a/monitor/rfcomm.c b/monitor/rfcomm.c
index 8a44a2199..b49adb324 100644
--- a/monitor/rfcomm.c
+++ b/monitor/rfcomm.c
@@ -386,12 +386,90 @@ 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;
+	uint16_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]));
+		}
+	}
+}
+
 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;
+	uint16_t pos;
 
 	if (!channel)
 		return mcc_frame(rfcomm_frame, indent);
@@ -410,10 +488,62 @@ 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 the rest
+			 * of the buffer as-is and let obex_packet() report
+			 * the error, rather than buffering indefinitely.
+			 */
+			pdu_frame.data = frag->buf + pos;
+			pdu_frame.size = frag->len - pos;
+			obex_packet(&pdu_frame, channel, pdu_frame.size);
+			pos = frag->len;
+			break;
+		}
+
+		if ((uint16_t) (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 +653,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


      parent reply	other threads:[~2026-11-24  9:23 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-24  9:22 [PATCH BlueZ v3 0/6] monitor: Add an OBEX protocol decoder to btmon Frédéric Danis
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  9:22 ` [PATCH BlueZ v3 2/6] monitor: Add OBEX protocol decoder Frédéric Danis
2026-11-24  9:22 ` [PATCH BlueZ v3 3/6] monitor: Decode PBAP/MAP/BIP Application Parameters tags Frédéric Danis
2026-11-24  9:22 ` [PATCH BlueZ v3 4/6] monitor: Reassemble L2CAP ERTM/Streaming SDUs Frédéric Danis
2026-11-24  9:22 ` [PATCH BlueZ v3 5/6] monitor: Fix RFCOMM UIH frame length and credit-only handling Frédéric Danis
2026-11-24  9:22 ` Frédéric Danis [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261124092248.207565-7-frederic.danis@collabora.com \
    --to=frederic.danis@collabora.com \
    --cc=linux-bluetooth@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox