From: "Frédéric Danis" <frederic.danis@collabora.com>
To: linux-bluetooth@vger.kernel.org
Subject: [PATCH BlueZ v6 6/6] monitor: Reassemble OBEX PDUs split across RFCOMM UIH frames
Date: Mon, 30 Nov 2026 10:10:52 +0100 [thread overview]
Message-ID: <20261130091052.383139-7-frederic.danis@collabora.com> (raw)
In-Reply-To: <20261130091052.383139-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/DLCI/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. Key this reassembly, and
the OBEX session/request state tracked alongside it, on the full DLCI
rather than just the server channel number, since two concurrent
connections can share a server channel while using distinct DLCIs.
Drop the buffer and session state for a DLCI when it is torn down
(rfcomm_remove_channel(), called alongside obex_remove_channel() on
DISC/DM) so a later renegotiation of the same DLCI doesn't resume
from stale fragments. A DISC/DM on DLCI 0 closes the whole RFCOMM
multiplexer, taking every data channel running over it down with it
without a DISC/DM of its own for each; clear all of their OBEX state
in that case too, via rfcomm_remove_handle() and the new
obex_remove_rfcomm_sessions(), while keeping SDP-learned channel
mappings for a future reconnect.
The RFCOMM L2CAP transport (PSM 0x0003) itself can also close while
the ACL link stays up, leaving the same per-DLCI state stale without
any RFCOMM-level DISC/DM being seen. Detect this in sig_disconn_req()
and clear it the same way.
While a PDU is still incomplete and buffered, show the UIH frame's
own payload so partial OBEX data isn't silently dropped from the
trace, mirroring the L2CAP SAR reassembly path.
sig_disconn_req() tested this with chan_list[key & 0x7fff].psm, but
l2cap_chan_key() only sets the 0x8000 bit itself when it resolves to
a chan_list[] entry; it falls back to the raw CID otherwise, which
isn't restricted to below 0x8000, so an untracked CID could read past
the end of chan_list[]. Add is_rfcomm_chan_key(), bounds-checking the
index against MAX_CHAN before dereferencing.
An RFCOMM server channel number only has meaning per owning device: a
channel learned via SDP from one peer's service record must not be
confused with the same channel number happening to be used by the
other peer for something else. Track which device sent the SABM that
established each ACL's RFCOMM multiplexer session (DLCI 0): per the
RFCOMM/GSM 07.10 'direction bit' convention that device stays the
session's initiator, and the other device the server/responder, for
every data channel subsequently opened over it. obex_channel() and
obex_add_channel() now take this role alongside the channel number,
and skip a would-be match whose stored role disagrees with the live
session's, while still falling back to matching on channel number
alone when the session's initiator hasn't been observed yet (e.g. a
capture starting mid-session). The role used when learning a channel
mapping from an SDP response is derived from which peer answered the
query, since the SDP/RFCOMM server for a service is conventionally
also its RFCOMM responder.
HCI Disconnection Complete can also tear down the ACL without an
L2CAP Disconnection Response for each of its channels; add
l2cap_remove_handle(), mirroring release_scid(), to release every
chan_list[] entry (and its SAR reassembly buffers) still tracked for
that handle, called alongside the existing release_handle()/
obex_disconnect_handle()/rfcomm_remove_handle() cleanup.
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.
v4->v5:
- While a PDU is still incomplete and buffered, hexdump the UIH
frame's own payload instead of showing nothing, mirroring the
L2CAP SAR reassembly path, so partial OBEX data isn't silently
dropped from the trace.
- Key the reassembly buffer and its OBEX session/request state on
the full DLCI (server channel plus direction bit) instead of just
the server channel number, since two concurrent connections can
share a server channel while using distinct DLCIs.
- Treat a DISC/DM on DLCI 0 as closing the whole RFCOMM multiplexer,
clearing every data channel's OBEX state via the new
obex_remove_rfcomm_sessions() and rfcomm_remove_handle(), since no
per-channel DISC/DM is seen for each of them, while keeping the
SDP-learned channel mappings for a future reconnect.
- Detect the RFCOMM L2CAP transport (PSM 0x0003) itself closing
while the ACL link stays up, in sig_disconn_req(), and clear the
same per-DLCI state in that case too.
- Add is_rfcomm_chan_key(), bounds-checking the chan_list[] index
against MAX_CHAN before dereferencing it in sig_disconn_req():
l2cap_chan_key() only sets the 0x8000 bit when it resolves to a
chan_list[] entry, but falls back to the raw CID otherwise, which
isn't restricted to below 0x8000, risking a read past the end of
chan_list[].
v5->v6:
- Track which device sent the DLCI 0 SABM establishing each ACL's
RFCOMM multiplexer session, and have obex_channel()/
obex_add_channel() skip a channel-number match whose stored role
disagrees with the live session's, so a server channel number
that happens to be reused independently by each peer is no longer
confused between them; still falls back to matching on channel
number alone when the session's initiator hasn't been observed
yet.
- Add l2cap_remove_handle(), mirroring release_scid(), to release
every chan_list[] entry (and its SAR reassembly buffers) still
tracked for a handle torn down by HCI Disconnection Complete
without an L2CAP Disconnection Response for each of its channels.
monitor/l2cap.c | 61 +++++++++-
monitor/l2cap.h | 4 +
monitor/obex.c | 132 +++++++++++++++++----
monitor/obex.h | 6 +-
monitor/packet.c | 2 +
monitor/rfcomm.c | 299 +++++++++++++++++++++++++++++++++++++++++++++--
6 files changed, 464 insertions(+), 40 deletions(-)
diff --git a/monitor/l2cap.c b/monitor/l2cap.c
index 7f74d91ea..a5727c4d6 100644
--- a/monitor/l2cap.c
+++ b/monitor/l2cap.c
@@ -231,6 +231,29 @@ static void release_scid(const struct l2cap_frame *frame, uint16_t scid)
}
}
+/* Release every channel still tracked for a baseband connection handle
+ * that has been torn down (HCI Disconnection Complete) without an
+ * L2CAP Disconnection Response for each of its channels, e.g. when the
+ * ACL link drops first. Without this, reset_sdu_rx()'s SAR reassembly
+ * buffers for those channels would otherwise never be freed, and a
+ * later reused handle could inherit stale sdu_rx state.
+ */
+void l2cap_remove_handle(uint16_t index, uint16_t handle)
+{
+ int i;
+
+ for (i = 0; i < MAX_CHAN; i++) {
+ if (chan_list[i].index != index)
+ continue;
+
+ if (chan_list[i].handle != handle)
+ continue;
+
+ reset_sdu_rx(&chan_list[i]);
+ chan_list[i].handle = 0;
+ }
+}
+
static void assign_dcid(const struct l2cap_frame *frame, uint16_t dcid,
uint16_t scid)
{
@@ -477,6 +500,19 @@ uint16_t l2cap_chan_key(uint16_t index, bool in, uint16_t handle, uint16_t cid)
return cid;
}
+/* True if key is a l2cap_chan_key() value resolved to a chan_list[] entry
+ * (as opposed to a raw, unresolved CID) for the RFCOMM transport PSM
+ * (0x0003). The 0x8000 bit alone doesn't guarantee key was resolved, since
+ * a raw dynamic CID may also have it set; the low bits must additionally
+ * be bounds-checked against MAX_CHAN before indexing chan_list[].
+ */
+static bool is_rfcomm_chan_key(uint16_t key)
+{
+ uint16_t i = key & 0x7fff;
+
+ return (key & 0x8000) && i < MAX_CHAN && chan_list[i].psm == 0x0003;
+}
+
static struct chan_data *get_chan(const struct l2cap_frame *frame)
{
int i;
@@ -1407,6 +1443,7 @@ static void sig_config_rsp(const struct l2cap_frame *frame)
static void sig_disconn_req(const struct l2cap_frame *frame)
{
const struct bt_l2cap_pdu_disconn_req *pdu = frame->data;
+ uint16_t scid_key, dcid_key;
print_cid("Destination", pdu->dcid);
print_cid("Source", pdu->scid);
@@ -1420,12 +1457,24 @@ static void sig_disconn_req(const struct l2cap_frame *frame)
* 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)));
+ scid_key = l2cap_chan_key(frame->index, !frame->in, frame->handle,
+ le16_to_cpu(pdu->scid));
+ dcid_key = l2cap_chan_key(frame->index, frame->in, frame->handle,
+ le16_to_cpu(pdu->dcid));
+ obex_remove_session(frame->index, frame->handle, scid_key);
+ obex_remove_session(frame->index, frame->handle, dcid_key);
+
+ /* If the closing channel is the RFCOMM transport (PSM 0x0003),
+ * the ACL link stays up but every RFCOMM data channel (DLCI)
+ * multiplexed over it loses its L2CAP transport without a
+ * per-channel DISC/DM of its own. Drop their OBEX session and
+ * fragment reassembly state too, keeping the SDP-learned channel
+ * mappings for a future reconnect.
+ */
+ if (is_rfcomm_chan_key(scid_key) || is_rfcomm_chan_key(dcid_key)) {
+ obex_remove_rfcomm_sessions(frame->index, frame->handle);
+ rfcomm_remove_handle(frame->index, frame->handle);
+ }
}
static void sig_disconn_rsp(const struct l2cap_frame *frame)
diff --git a/monitor/l2cap.h b/monitor/l2cap.h
index 1c1f7e609..18b8d2f88 100644
--- a/monitor/l2cap.h
+++ b/monitor/l2cap.h
@@ -364,4 +364,8 @@ 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_remove_handle(uint16_t index, uint16_t handle);
+
void l2cap_dequeue_frame(struct timeval *delta, struct packet_conn_data *conn);
diff --git a/monitor/obex.c b/monitor/obex.c
index 84b30a6d4..58e176385 100644
--- a/monitor/obex.c
+++ b/monitor/obex.c
@@ -61,6 +61,7 @@ struct obex_chan_data {
uint16_t index;
uint16_t handle;
uint8_t channel;
+ bool local_is_server;
char profile[32];
};
@@ -76,7 +77,7 @@ struct obex_req_data {
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)
+ bool local_is_server, const char *profile)
{
int i, n = -1;
@@ -92,7 +93,9 @@ void obex_add_channel(uint16_t index, uint16_t handle, uint8_t channel,
if (obex_chan_list[i].index == index &&
obex_chan_list[i].handle == handle &&
- obex_chan_list[i].channel == channel) {
+ obex_chan_list[i].channel == channel &&
+ obex_chan_list[i].local_is_server ==
+ local_is_server) {
n = i;
break;
}
@@ -104,6 +107,7 @@ void obex_add_channel(uint16_t index, uint16_t handle, uint8_t channel,
obex_chan_list[n].index = index;
obex_chan_list[n].handle = handle;
obex_chan_list[n].channel = channel;
+ obex_chan_list[n].local_is_server = local_is_server;
if (profile)
snprintf(obex_chan_list[n].profile,
@@ -113,8 +117,18 @@ void obex_add_channel(uint16_t index, uint16_t handle, uint8_t channel,
obex_chan_list[n].profile[0] = '\0';
}
+/* local_is_server is the live RFCOMM session's known role for this
+ * channel (1 if the local host is the responder/server, 0 if the
+ * remote peer is, or -1 if the multiplexer's initiator hasn't been
+ * observed yet). Each device numbers its own RFCOMM server channels
+ * independently, so a channel learned from one peer's SDP record must
+ * not match a session whose server is actually the other peer, even
+ * if both happen to use the same channel number - skip a role
+ * mismatch rather than returning a false positive. Left unenforced
+ * (matching on channel number alone) when the role isn't known yet.
+ */
bool obex_channel(uint16_t index, uint16_t handle, uint8_t channel,
- const char **profile)
+ int local_is_server, const char **profile)
{
int i;
@@ -128,6 +142,11 @@ bool obex_channel(uint16_t index, uint16_t handle, uint8_t channel,
if (obex_chan_list[i].index == index &&
obex_chan_list[i].handle == handle &&
obex_chan_list[i].channel == channel) {
+ if (local_is_server >= 0 &&
+ !!obex_chan_list[i].local_is_server !=
+ !!local_is_server)
+ continue;
+
if (profile)
*profile = obex_chan_list[i].profile;
return true;
@@ -269,6 +288,73 @@ void obex_remove_session(uint16_t index, uint16_t handle, uint16_t channel)
obex_clear_profile(index, handle, channel);
}
+/* Drop every session's request/profile state for this index/handle,
+ * regardless of channel, used when the whole ACL link is going away
+ * (see obex_disconnect_handle()).
+ */
+void obex_remove_all_sessions(uint16_t index, uint16_t handle)
+{
+ int 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_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]));
+ }
+}
+
+/* RFCOMM DLCIs only use 6 bits (0-63), while channel values used for
+ * direct L2CAP OBEX sessions are either a raw dynamic CID (>= 0x0040)
+ * or an index into l2cap.c's channel table with the 0x8000 bit set
+ * (see l2cap_chan_key()), so this bound reliably tells the two apart.
+ */
+#define MAX_RFCOMM_DLCI 0x3f
+
+/* The RFCOMM multiplexer (DLCI 0) or its underlying L2CAP transport is
+ * being torn down, which takes every RFCOMM data channel multiplexed
+ * over it down with it, even though no per-channel DISC/DM is seen for
+ * each of them. Drop their request/profile state only, same as
+ * obex_remove_channel() would for a single channel, but keep the
+ * SDP-learned channel mappings so a future reconnect on the same ACL
+ * link doesn't need SDP repeated, and leave alone any unrelated direct
+ * L2CAP OBEX session sharing the same ACL handle.
+ */
+void obex_remove_rfcomm_sessions(uint16_t index, uint16_t handle)
+{
+ int i;
+
+ for (i = 0; i < MAX_OBEX_REQ; i++) {
+ if (obex_req_list[i].channel > MAX_RFCOMM_DLCI)
+ continue;
+
+ 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_PROFILE; i++) {
+ if (!obex_profile_list[i].channel ||
+ obex_profile_list[i].channel >
+ MAX_RFCOMM_DLCI)
+ 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]));
+ }
+}
+
void obex_disconnect_handle(uint16_t index, uint16_t handle)
{
int i;
@@ -283,11 +369,7 @@ void obex_disconnect_handle(uint16_t index, uint16_t handle)
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]));
- }
+ obex_remove_all_sessions(index, handle);
for (i = 0; i < MAX_OBEX_PSM; i++) {
if (!obex_psm_list[i].psm)
@@ -298,16 +380,6 @@ 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,
@@ -534,6 +606,7 @@ static uint16_t obex_de_uuid16(const uint8_t *data, uint32_t size)
* attribute ID with an unrelated profile.
*/
static bool obex_scan_proto_list(uint16_t index, uint16_t handle,
+ bool local_is_server,
const uint8_t *data, uint32_t size,
const char *profile)
{
@@ -597,7 +670,8 @@ next:
}
if (have_obex && channel)
- obex_add_channel(index, handle, channel, profile);
+ obex_add_channel(index, handle, channel, local_is_server,
+ profile);
else if (have_obex && psm)
obex_add_psm(index, handle, psm);
@@ -631,6 +705,7 @@ static bool obex_scan_svclass(const uint8_t *data, uint32_t size,
}
static void obex_scan_attr_list(uint16_t index, uint16_t handle,
+ bool local_is_server,
const uint8_t *data, uint32_t size)
{
uint32_t pos = 0;
@@ -666,7 +741,9 @@ static void obex_scan_attr_list(uint16_t index, uint16_t handle,
sizeof(profile));
break;
case SDP_ATTR_PROTO_DESC_LIST:
- if (obex_scan_proto_list(index, handle, attr_val,
+ if (obex_scan_proto_list(index, handle,
+ local_is_server,
+ attr_val,
attr_size, profile))
have_obex = true;
break;
@@ -687,7 +764,9 @@ static void obex_scan_attr_list(uint16_t index, uint16_t handle,
if (atype == SDP_DE_SEQ &&
obex_scan_proto_list(index,
- handle, aval,
+ handle,
+ local_is_server,
+ aval,
avsize,
profile))
have_obex = true;
@@ -736,13 +815,20 @@ void obex_sdp_scan(const struct l2cap_frame *frame, const uint8_t *data,
const uint8_t *val;
uint32_t vsize;
uint32_t pos = 0;
+ /* This SDP response is answered by whichever device actually
+ * hosts/advertises the service; frame->in is true when it was
+ * received from the remote peer, meaning the remote is the SDP
+ * (and RFCOMM) server for every channel/PSM scanned out of it.
+ */
+ bool local_is_server = !frame->in;
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);
+ obex_scan_attr_list(frame->index, frame->handle,
+ local_is_server, val, vsize);
return;
}
@@ -758,7 +844,7 @@ void obex_sdp_scan(const struct l2cap_frame *frame, const uint8_t *data,
if (rtype == SDP_DE_SEQ)
obex_scan_attr_list(frame->index, frame->handle,
- rval, rvsize);
+ local_is_server, rval, rvsize);
pos += relemlen;
}
diff --git a/monitor/obex.h b/monitor/obex.h
index 019ed6174..68e0c1cd6 100644
--- a/monitor/obex.h
+++ b/monitor/obex.h
@@ -105,11 +105,13 @@ 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);
+ bool local_is_server, const char *profile);
bool obex_channel(uint16_t index, uint16_t handle, uint8_t channel,
- const char **profile);
+ int local_is_server, 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_remove_all_sessions(uint16_t index, uint16_t handle);
+void obex_remove_rfcomm_sessions(uint16_t index, uint16_t handle);
void obex_disconnect_handle(uint16_t index, uint16_t handle);
void obex_add_psm(uint16_t index, uint16_t handle, uint16_t psm);
diff --git a/monitor/packet.c b/monitor/packet.c
index 7414910c7..badf6b6f1 100644
--- a/monitor/packet.c
+++ b/monitor/packet.c
@@ -12315,6 +12315,8 @@ 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));
+ l2cap_remove_handle(index, le16_to_cpu(evt->handle));
}
}
diff --git a/monitor/rfcomm.c b/monitor/rfcomm.c
index 90ddc3596..afd874ee5 100644
--- a/monitor/rfcomm.c
+++ b/monitor/rfcomm.c
@@ -386,12 +386,191 @@ 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/DLCI 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.
+ *
+ * The full DLCI (server channel plus direction bit), not just the
+ * server channel number, identifies the session: two connections to
+ * the same server channel but opened by different initiators use
+ * distinct DLCIs and must not share a reassembly buffer.
+ */
+#define MAX_OBEX_FRAG 64
+
+struct obex_frag_data {
+ uint16_t index;
+ uint16_t handle;
+ uint8_t dlci;
+ 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 dlci, bool in)
+{
+ int i, n = -1;
+
+ for (i = 0; i < MAX_OBEX_FRAG; i++) {
+ if (!obex_frag_list[i].dlci) {
+ if (n < 0)
+ n = i;
+ continue;
+ }
+
+ if (obex_frag_list[i].index == index &&
+ obex_frag_list[i].handle == handle &&
+ obex_frag_list[i].dlci == dlci &&
+ 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].dlci = dlci;
+ 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;
+}
+
+/* Tracks which device sent the SABM establishing the RFCOMM
+ * multiplexer session (DLCI 0) for an ACL handle: that device is the
+ * session's initiator, and per the RFCOMM/GSM 07.10 "direction bit"
+ * convention it stays the initiator - and the other device the
+ * server/responder - for every data channel subsequently opened over
+ * that same session. Used by uih_frame() alongside the SDP-learned
+ * advertising endpoint to tell apart a server channel number that
+ * happens to be reused independently by each peer.
+ */
+#define MAX_RFCOMM_MUX 16
+
+struct rfcomm_mux_data {
+ uint16_t index;
+ uint16_t handle;
+ bool local_is_initiator;
+};
+
+static struct rfcomm_mux_data rfcomm_mux_list[MAX_RFCOMM_MUX];
+
+static void rfcomm_set_mux_initiator(uint16_t index, uint16_t handle,
+ bool local_is_initiator)
+{
+ int i, n = -1;
+
+ for (i = 0; i < MAX_RFCOMM_MUX; i++) {
+ if (!rfcomm_mux_list[i].handle) {
+ if (n < 0)
+ n = i;
+ continue;
+ }
+
+ if (rfcomm_mux_list[i].index == index &&
+ rfcomm_mux_list[i].handle == handle) {
+ n = i;
+ break;
+ }
+ }
+
+ if (n < 0)
+ return;
+
+ rfcomm_mux_list[n].index = index;
+ rfcomm_mux_list[n].handle = handle;
+ rfcomm_mux_list[n].local_is_initiator = local_is_initiator;
+}
+
+/* Returns 1 if the local host is known to be the RFCOMM server
+ * (responder) for this ACL handle's multiplexer session, 0 if the
+ * remote peer is, or -1 if the session's initiator hasn't been
+ * observed yet (e.g. the capture started after the DLCI 0 SABM).
+ */
+static int rfcomm_local_is_server(uint16_t index, uint16_t handle)
+{
+ int i;
+
+ for (i = 0; i < MAX_RFCOMM_MUX; i++) {
+ if (rfcomm_mux_list[i].handle &&
+ rfcomm_mux_list[i].index == index &&
+ rfcomm_mux_list[i].handle == handle)
+ return rfcomm_mux_list[i].local_is_initiator ? 0 : 1;
+ }
+
+ return -1;
+}
+
+static void rfcomm_remove_channel(uint16_t index, uint16_t handle,
+ uint8_t dlci)
+{
+ int i;
+
+ for (i = 0; i < MAX_OBEX_FRAG; i++) {
+ if (obex_frag_list[i].dlci == dlci &&
+ 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].dlci)
+ 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]));
+ }
+ }
+
+ for (i = 0; i < MAX_RFCOMM_MUX; i++) {
+ if (rfcomm_mux_list[i].handle &&
+ rfcomm_mux_list[i].index == index &&
+ rfcomm_mux_list[i].handle == handle)
+ memset(&rfcomm_mux_list[i], 0,
+ sizeof(rfcomm_mux_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);
+ uint8_t dlci = RFCOMM_GET_DLCI(hdr->address);
+ struct obex_frag_data *frag;
+ uint8_t *buf;
+ uint32_t pos;
if (!channel)
return mcc_frame(rfcomm_frame, indent);
@@ -410,11 +589,85 @@ 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
+ /* The server channel number is only used to resolve the
+ * SDP-learned mapping; the DLCI (which also encodes which side
+ * opened the connection) is the actual session/reassembly key,
+ * since two connections can share a server channel number.
+ */
+ if (!obex_channel(frame->index, frame->handle, channel,
+ rfcomm_local_is_server(frame->index, frame->handle),
+ NULL)) {
+ packet_hexdump(frame->data, frame->size);
+ return true;
+ }
+
+ frag = obex_frag_get(frame->index, frame->handle, dlci, frame->in);
+ if (!frag) {
+ obex_packet(frame, dlci, frame->size);
+ return true;
+ }
+
+ buf = realloc(frag->buf, frag->len + frame->size);
+ if (!buf) {
+ obex_frag_reset(frag);
+ obex_packet(frame, dlci, 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, dlci, 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, dlci, pdu_len);
+
+ pos += pdu_len;
+ }
+
+ if (pos == frag->len) {
+ obex_frag_reset(frag);
+ } else {
+ /* Still waiting for more fragments: show this frame's own
+ * payload so partial OBEX data isn't silently dropped from
+ * the trace, mirroring the L2CAP SAR reassembly path.
+ */
packet_hexdump(frame->data, frame->size);
+ if (pos) {
+ memmove(frag->buf, frag->buf + pos, frag->len - pos);
+ frag->len -= pos;
+ }
+ }
+
return true;
}
@@ -532,13 +785,41 @@ void rfcomm_packet(const struct l2cap_frame *frame)
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.
+ } else if (ctype == 0x2f) {
+ /* SABM on DLCI 0 establishes the RFCOMM multiplexer
+ * session; the device that sends it is the session's
+ * initiator for every data channel subsequently opened
+ * over it (see rfcomm_set_mux_initiator()).
*/
- obex_remove_channel(frame->index, frame->handle,
- RFCOMM_GET_CHANNEL(hdr.address));
+ if (!RFCOMM_GET_DLCI(hdr.address))
+ rfcomm_set_mux_initiator(frame->index, frame->handle,
+ !frame->in);
+ } else if (ctype == 0x43 || ctype == 0x0f) {
+ uint8_t dlci = RFCOMM_GET_DLCI(hdr.address);
+
+ if (!dlci) {
+ /* DISC or DM on DLCI 0: the whole RFCOMM
+ * multiplexer is being torn down, taking every
+ * data channel running over it down with it, even
+ * though no per-channel DISC/DM is seen for each of
+ * them. Drop all of their OBEX state, but keep the
+ * SDP-learned channel mappings so a reconnect on
+ * the same ACL link doesn't need SDP repeated.
+ */
+ obex_remove_rfcomm_sessions(frame->index,
+ frame->handle);
+ rfcomm_remove_handle(frame->index, frame->handle);
+ } else {
+ /* DISC or DM: the RFCOMM channel is being torn
+ * down, drop any OBEX state tracked for it so a
+ * later renegotiation of the same DLCI isn't
+ * misdecoded.
+ */
+ obex_remove_channel(frame->index, frame->handle,
+ dlci);
+ rfcomm_remove_channel(frame->index, frame->handle,
+ dlci);
+ }
}
return;
--
2.43.0
prev parent reply other threads:[~2026-11-30 9:11 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-30 9:10 [PATCH BlueZ v6 0/6] monitor: Add an OBEX protocol decoder to btmon Frédéric Danis
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 9:10 ` [PATCH BlueZ v6 2/6] monitor: Add OBEX protocol decoder Frédéric Danis
2026-11-30 9:10 ` [PATCH BlueZ v6 3/6] monitor: Decode PBAP/MAP/BIP Application Parameters tags Frédéric Danis
2026-11-30 9:10 ` [PATCH BlueZ v6 4/6] monitor: Reassemble L2CAP ERTM/Streaming SDUs Frédéric Danis
2026-11-30 9:10 ` [PATCH BlueZ v6 5/6] monitor: Fix RFCOMM UIH frame length and credit-only handling Frédéric Danis
2026-11-30 9:10 ` 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=20261130091052.383139-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