From: "Frédéric Danis" <frederic.danis@collabora.com>
To: linux-bluetooth@vger.kernel.org
Subject: [PATCH BlueZ v4 6/6] monitor: Reassemble OBEX PDUs split across RFCOMM UIH frames
Date: Tue, 24 Nov 2026 15:56:52 +0100 [thread overview]
Message-ID: <20261124145653.336273-7-frederic.danis@collabora.com> (raw)
In-Reply-To: <20261124145653.336273-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.
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
prev parent reply other threads:[~2026-11-24 14:57 UTC|newest]
Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top
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 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
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 ` [PATCH BlueZ v4 4/6] monitor: Reassemble L2CAP ERTM/Streaming SDUs 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 ` 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=20261124145653.336273-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 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.