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 v8 6/6] monitor: Reassemble OBEX PDUs split across RFCOMM UIH frames
Date: Fri,  4 Dec 2026 17:04:16 +0100	[thread overview]
Message-ID: <20261204160416.1254097-7-frederic.danis@collabora.com> (raw)
In-Reply-To: <20261204160416.1254097-1-frederic.danis@collabora.com>

An OBEX PDU can be split across several UIH frames, which were decoded
independently.

Buffer the UIH payload per index/handle/DLCI/direction until a complete
PDU is available, hexdumping the payload of the frames still being
buffered, and limit OBEX hexdumps to the bytes carried by the current
frame. Key this buffer and the OBEX session state on the DLCI rather
than the server channel, as two connections can use the same server
channel.

Drop them on DISC/DM of the DLCI, and for all DLCIs of the multiplexer
on DISC/DM of DLCI 0 or disconnection of its L2CAP channel. On ACL
disconnection, also release the L2CAP channels still tracked for the
handle (l2cap_remove_handle()).

RFCOMM server channels and dynamic PSMs are allocated by each device
independently, so a channel or PSM learned from the SDP record of one
device must not match a session served by the other one. Record the
server role with each mapping learned from SDP, i.e. the device
answering the query, and reject a session with the other role. The
role of an RFCOMM session is derived from the device sending the SABM
on DLCI 0 and from the DLCI direction bit, and the role of an L2CAP
channel from the direction of its Connection Request, now also looked
up for Basic mode. The check is skipped while the role isn't known.

SDP isn't necessarily repeated after an ACL reconnection, so keep the
channel and PSM mappings across ACL disconnection and match them by
peer address on the new handle.

Assisted-by: Claude:claude-sonnet-5
Assisted-by: Claude:claude-opus-5-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.

v6->v7:
  - Pass the DLCI into rfcomm_local_is_server() and derive the server
    role from both the mux initiator/responder tracking and the
    DLCI's direction bit, instead of always treating the mux responder
    as server: the direction bit only identifies which side opened
    that particular data channel as client, so the server is the
    other side, not unconditionally the responder.

v7->v8:
  - Fix the server role derived from the DLCI direction bit, which was
    inverted.
  - Mark used multiplexer slots with in_use, as handle 0 is valid.
  - Record the server role with the PSM mappings too, and reject an
    L2CAP channel with the other role. The role comes from the
    Connection Request direction, and is also looked up for Basic
    mode.
  - Keep the channel and PSM mappings across ACL disconnection and
    match them by peer address on the new handle, as SDP isn't
    necessarily repeated after a reconnection.
  - Look up the L2CAP channel to detect the RFCOMM transport on
    Disconnection Request, instead of decoding the l2cap_chan_key()
    value, which can be a raw CID with bit 15 set.
  - Limit OBEX hexdumps to the bytes carried by the current UIH frame,
    and only hexdump the bytes of a buffered frame not decoded yet.
  - Trim comments and commit message.

 monitor/l2cap.c  | 142 ++++++++++++++++++-------
 monitor/l2cap.h  |   4 +
 monitor/obex.c   | 237 ++++++++++++++++++++++++++++++++---------
 monitor/obex.h   |  12 ++-
 monitor/packet.c |   2 +
 monitor/rfcomm.c | 271 ++++++++++++++++++++++++++++++++++++++++++++++-
 6 files changed, 570 insertions(+), 98 deletions(-)

diff --git a/monitor/l2cap.c b/monitor/l2cap.c
index 76a16e22c..ddca385ac 100644
--- a/monitor/l2cap.c
+++ b/monitor/l2cap.c
@@ -153,6 +153,7 @@ struct chan_data {
 	uint8_t  fcs_dir[2];
 	int8_t   req_fcs[2];
 	int8_t   rsp_fcs[2];
+	int8_t   local_is_server;
 	struct l2cap_sdu sdu_rx[2];
 	struct packet_latency tx_l;
 };
@@ -233,6 +234,9 @@ static void assign_scid(const struct l2cap_frame *frame, uint16_t scid,
 	chan_list[n].ctrlid = ctrlid;
 	chan_list[n].mode = mode;
 
+	/* Called for a Connection Request, sent by the client */
+	chan_list[n].local_is_server = frame->in ? 1 : 0;
+
 	/* FCS is used unless both directions negotiate "No FCS" */
 	chan_list[n].fcs = 1;
 	chan_list[n].fcs_dir[0] = 1;
@@ -272,6 +276,25 @@ static void release_scid(const struct l2cap_frame *frame, uint16_t scid)
 	}
 }
 
+/* Channels of a disconnected ACL link may not have seen a Disconnection
+ * Response.
+ */
+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)
 {
@@ -576,6 +599,23 @@ uint16_t l2cap_chan_key(uint16_t index, bool in, uint16_t handle, uint16_t cid)
 	return cid;
 }
 
+static bool is_rfcomm_chan(uint16_t index, bool in, uint16_t handle,
+							uint16_t cid)
+{
+	struct l2cap_frame frame;
+	int i;
+
+	memset(&frame, 0, sizeof(frame));
+	frame.index = index;
+	frame.in = in;
+	frame.handle = handle;
+	frame.cid = cid;
+
+	i = get_chan_data_index(&frame);
+
+	return i >= 0 && chan_list[i].psm == 0x0003;
+}
+
 static struct chan_data *get_chan(const struct l2cap_frame *frame)
 {
 	int i;
@@ -1522,17 +1562,26 @@ 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);
 
 	/* SCID belongs to the sender and DCID to the recipient */
-	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 (is_rfcomm_chan(frame->index, !frame->in, frame->handle,
+					le16_to_cpu(pdu->scid)) ||
+			is_rfcomm_chan(frame->index, frame->in, frame->handle,
+					le16_to_cpu(pdu->dcid))) {
+		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)
@@ -3403,6 +3452,48 @@ static bool l2cap_sdu_reassemble(struct chan_data *chan, bool in,
 	return true;
 }
 
+/* frag_len is the number of trailing bytes of frame->data carried by the
+ * fragment completing the SDU.
+ */
+static void l2cap_sdu_dispatch(uint16_t index, bool in, uint16_t handle,
+				uint16_t cid, struct l2cap_frame *frame,
+				struct chan_data *chan, uint16_t frag_len)
+{
+	switch (frame->psm) {
+	case 0x0001:
+		sdp_packet(frame);
+		break;
+	case 0x0003:
+		rfcomm_packet(frame);
+		break;
+	case 0x000f:
+		bnep_packet(frame);
+		break;
+	case 0x001f:
+	case 0x0027:
+		/* att_packet() reads the opcode before checking the size */
+		if (frame->size)
+			att_packet(index, in, handle, cid, frame->data,
+							frame->size);
+		break;
+	case 0x0017:
+	case 0x001B:
+		avctp_packet(frame);
+		break;
+	case 0x0019:
+		avdtp_packet(frame);
+		break;
+	default:
+		if (obex_psm(index, handle, frame->psm,
+				chan ? chan->local_is_server : -1))
+			obex_packet(frame, l2cap_chan_key(index, in,
+						handle, cid), frag_len);
+		else
+			packet_hexdump(frame->data, frame->size);
+		break;
+	}
+}
+
 void l2cap_frame(uint16_t index, bool in, uint16_t handle, uint16_t cid,
 			uint16_t psm, const void *data, uint16_t size)
 {
@@ -3470,6 +3561,8 @@ void l2cap_frame(uint16_t index, bool in, uint16_t handle, uint16_t cid,
 					"{chan %d}", cid, size, frame.psm,
 					mode2str(frame.mode), frame.mode,
 					frame.chan);
+			/* For the OBEX PSM role check */
+			chan = get_chan(&frame);
 			break;
 		default:
 			ext_ctrl = get_ext_ctrl(&frame);
@@ -3543,42 +3636,11 @@ void l2cap_frame(uint16_t index, bool in, uint16_t handle, uint16_t cid,
 			break;
 		}
 
-		switch (frame.psm) {
-		case 0x0001:
-			sdp_packet(&frame);
-			break;
-		case 0x0003:
-			rfcomm_packet(&frame);
-			break;
-		case 0x000f:
-			bnep_packet(&frame);
-			break;
-		case 0x001f:
-		case 0x0027:
-			/* att_packet() reads the opcode before checking the
-			 * size.
-			 */
-			if (frame.size)
-				att_packet(index, in, handle, cid, frame.data,
-								frame.size);
-			break;
-		case 0x0017:
-		case 0x001B:
-			avctp_packet(&frame);
-			break;
-		case 0x0019:
-			avdtp_packet(&frame);
-			break;
-		default:
-			if (obex_psm(index, handle, frame.psm))
-				obex_packet(&frame, l2cap_chan_key(index, in,
-							handle, cid), frag_len);
-			else
-				packet_hexdump(frame.data, frame.size);
-			break;
-		}
+		l2cap_sdu_dispatch(index, in, handle, cid, &frame, chan,
+								frag_len);
 
 		free(sdu_buf);
+
 		break;
 	}
 }
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 d48706feb..54f142e43 100644
--- a/monitor/obex.c
+++ b/monitor/obex.c
@@ -49,10 +49,74 @@ enum obex_profile {
 	OBEX_PROFILE_BIP,
 };
 
-struct obex_chan_data {
+/* SDP isn't necessarily repeated after an ACL reconnection, so the peer
+ * address is kept to match the new handle.
+ */
+struct obex_link {
 	uint16_t index;
 	uint16_t handle;
+	bool connected;
+	bool has_bdaddr;
+	uint8_t bdaddr[6];
+};
+
+static void obex_link_set(struct obex_link *link, uint16_t index,
+							uint16_t handle)
+{
+	struct packet_conn_data *conn = packet_get_conn_data(handle);
+
+	link->index = index;
+	link->handle = handle;
+	link->connected = true;
+	link->has_bdaddr = conn && conn->index == index;
+	if (link->has_bdaddr)
+		memcpy(link->bdaddr, conn->dst, sizeof(link->bdaddr));
+}
+
+static bool obex_link_match(struct obex_link *link, uint16_t index,
+							uint16_t handle)
+{
+	struct packet_conn_data *conn;
+
+	if (link->index != index)
+		return false;
+
+	if (link->connected && link->handle == handle)
+		return true;
+
+	if (!link->has_bdaddr)
+		return false;
+
+	conn = packet_get_conn_data(handle);
+	if (!conn || conn->index != index)
+		return false;
+
+	if (memcmp(link->bdaddr, conn->dst, sizeof(link->bdaddr)))
+		return false;
+
+	link->handle = handle;
+	link->connected = true;
+
+	return true;
+}
+
+/* Returns whether the entry should be released */
+static bool obex_link_disconnect(struct obex_link *link, uint16_t index,
+							uint16_t handle)
+{
+	if (link->index != index || !link->connected ||
+						link->handle != handle)
+		return false;
+
+	link->connected = false;
+
+	return !link->has_bdaddr;
+}
+
+struct obex_chan_data {
+	struct obex_link link;
 	uint8_t channel;
+	bool local_is_server;
 	char profile[32];
 };
 
@@ -68,9 +132,9 @@ 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;
+	int i, n = -1, stale = -1;
 
 	if (!channel)
 		return;
@@ -82,20 +146,27 @@ void obex_add_channel(uint16_t index, uint16_t handle, uint8_t channel,
 			continue;
 		}
 
-		if (obex_chan_list[i].index == index &&
-				obex_chan_list[i].handle == handle &&
-				obex_chan_list[i].channel == channel) {
+		if (obex_link_match(&obex_chan_list[i].link, index, handle) &&
+				obex_chan_list[i].channel == channel &&
+				obex_chan_list[i].local_is_server ==
+							local_is_server) {
 			n = i;
 			break;
 		}
+
+		if (stale < 0 && !obex_chan_list[i].link.connected)
+			stale = i;
 	}
 
+	if (n < 0)
+		n = stale;
+
 	if (n < 0)
 		return;
 
-	obex_chan_list[n].index = index;
-	obex_chan_list[n].handle = handle;
+	obex_link_set(&obex_chan_list[n].link, index, 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,
@@ -105,8 +176,11 @@ void obex_add_channel(uint16_t index, uint16_t handle, uint8_t channel,
 		obex_chan_list[n].profile[0] = '\0';
 }
 
+/* local_is_server is 1 if the local host is the server, 0 if the peer
+ * is, or -1 if unknown, in which case the role isn't checked.
+ */
 bool obex_channel(uint16_t index, uint16_t handle, uint8_t channel,
-							const char **profile)
+				int local_is_server, const char **profile)
 {
 	int i;
 
@@ -117,9 +191,13 @@ bool obex_channel(uint16_t index, uint16_t handle, uint8_t channel,
 		if (!obex_chan_list[i].channel)
 			continue;
 
-		if (obex_chan_list[i].index == index &&
-				obex_chan_list[i].handle == handle &&
+		if (obex_link_match(&obex_chan_list[i].link, index, 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;
@@ -132,16 +210,17 @@ bool obex_channel(uint16_t index, uint16_t handle, uint8_t channel,
 #define MAX_OBEX_PSM 16
 
 struct obex_psm_data {
-	uint16_t index;
-	uint16_t handle;
+	struct obex_link link;
 	uint16_t psm;
+	bool local_is_server;
 };
 
 static struct obex_psm_data obex_psm_list[MAX_OBEX_PSM];
 
-void obex_add_psm(uint16_t index, uint16_t handle, uint16_t psm)
+void obex_add_psm(uint16_t index, uint16_t handle, uint16_t psm,
+						bool local_is_server)
 {
-	int i, n = -1;
+	int i, n = -1, stale = -1;
 
 	if (!psm)
 		return;
@@ -153,23 +232,32 @@ void obex_add_psm(uint16_t index, uint16_t handle, uint16_t psm)
 			continue;
 		}
 
-		if (obex_psm_list[i].index == index &&
-				obex_psm_list[i].handle == handle &&
-				obex_psm_list[i].psm == psm) {
+		if (obex_link_match(&obex_psm_list[i].link, index, handle) &&
+				obex_psm_list[i].psm == psm &&
+				obex_psm_list[i].local_is_server ==
+							local_is_server) {
 			n = i;
 			break;
 		}
+
+		if (stale < 0 && !obex_psm_list[i].link.connected)
+			stale = i;
 	}
 
+	if (n < 0)
+		n = stale;
+
 	if (n < 0)
 		return;
 
-	obex_psm_list[n].index = index;
-	obex_psm_list[n].handle = handle;
+	obex_link_set(&obex_psm_list[n].link, index, handle);
 	obex_psm_list[n].psm = psm;
+	obex_psm_list[n].local_is_server = local_is_server;
 }
 
-bool obex_psm(uint16_t index, uint16_t handle, uint16_t psm)
+/* local_is_server as for obex_channel() */
+bool obex_psm(uint16_t index, uint16_t handle, uint16_t psm,
+						int local_is_server)
 {
 	int i;
 
@@ -180,9 +268,14 @@ bool obex_psm(uint16_t index, uint16_t handle, uint16_t psm)
 		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)
+		if (obex_psm_list[i].psm != psm ||
+				!obex_link_match(&obex_psm_list[i].link, index,
+								handle))
+			continue;
+
+		if (local_is_server < 0 ||
+				!!obex_psm_list[i].local_is_server ==
+							!!local_is_server)
 			return true;
 	}
 
@@ -246,38 +339,47 @@ void obex_remove_session(uint16_t index, uint16_t handle, uint16_t channel)
 	obex_clear_profile(index, handle, channel);
 }
 
-void obex_disconnect_handle(uint16_t index, uint16_t handle)
+void obex_remove_all_sessions(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)
+	for (i = 0; i < MAX_OBEX_PROFILE; i++) {
+		if (!obex_profile_list[i].channel)
 			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]));
+		if (obex_profile_list[i].index == index &&
+				obex_profile_list[i].handle == handle)
+			memset(&obex_profile_list[i], 0,
+					sizeof(obex_profile_list[i]));
+	}
+}
+
+/* Session keys above it are L2CAP ones, see l2cap_chan_key() */
+#define MAX_RFCOMM_DLCI 0x3f
+
+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)
+		if (!obex_profile_list[i].channel ||
+				obex_profile_list[i].channel >
+							MAX_RFCOMM_DLCI)
 			continue;
 
 		if (obex_profile_list[i].index == index &&
@@ -287,6 +389,33 @@ void obex_disconnect_handle(uint16_t index, uint16_t handle)
 	}
 }
 
+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_link_disconnect(&obex_chan_list[i].link, index,
+								handle))
+			memset(&obex_chan_list[i], 0,
+						sizeof(obex_chan_list[i]));
+	}
+
+	obex_remove_all_sessions(index, handle);
+
+	for (i = 0; i < MAX_OBEX_PSM; i++) {
+		if (!obex_psm_list[i].psm)
+			continue;
+
+		if (obex_link_disconnect(&obex_psm_list[i].link, index,
+								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)
 {
@@ -485,6 +614,7 @@ static uint16_t obex_de_uuid16(const uint8_t *data, uint32_t size)
  * carrying OBEX. Returns whether it carries OBEX.
  */
 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)
 {
@@ -546,9 +676,10 @@ 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);
+		obex_add_psm(index, handle, psm, local_is_server);
 
 	return have_obex;
 }
@@ -577,6 +708,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;
@@ -612,7 +744,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;
@@ -633,7 +767,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;
@@ -655,7 +791,7 @@ static void obex_scan_attr_list(uint16_t index, uint16_t handle,
 	}
 
 	if (have_obex && goep_psm)
-		obex_add_psm(index, handle, goep_psm);
+		obex_add_psm(index, handle, goep_psm, local_is_server);
 }
 
 /* nested is set for a Service Search Attribute Response, which holds a
@@ -668,13 +804,16 @@ void obex_sdp_scan(const struct l2cap_frame *frame, const uint8_t *data,
 	const uint8_t *val;
 	uint32_t vsize;
 	uint32_t pos = 0;
+	/* The device sending the record is the server */
+	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;
 	}
 
@@ -690,7 +829,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..c911766b5 100644
--- a/monitor/obex.h
+++ b/monitor/obex.h
@@ -105,15 +105,19 @@ 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);
-bool obex_psm(uint16_t index, uint16_t handle, uint16_t psm);
+void obex_add_psm(uint16_t index, uint16_t handle, uint16_t psm,
+						bool local_is_server);
+bool obex_psm(uint16_t index, uint16_t handle, uint16_t psm,
+						int local_is_server);
 
 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 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 ca5deeba2..be4a3333e 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;
 }
 
+/* OBEX PDUs split across UIH frames, keyed on the DLCI as two
+ * connections can use the same server channel.
+ */
+#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;
+}
+
+/* Multiplexer initiator, from the SABM on DLCI 0 */
+#define MAX_RFCOMM_MUX 16
+
+struct rfcomm_mux_data {
+	bool in_use;
+	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].in_use) {
+			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].in_use = true;
+	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 the server of dlci, 0 if the peer is,
+ * or -1 if the multiplexer initiator is unknown.
+ */
+static int rfcomm_local_is_server(uint16_t index, uint16_t handle,
+							uint8_t dlci)
+{
+	int i;
+
+	for (i = 0; i < MAX_RFCOMM_MUX; i++) {
+		if (rfcomm_mux_list[i].in_use &&
+				rfcomm_mux_list[i].index == index &&
+				rfcomm_mux_list[i].handle == handle) {
+			bool local_is_initiator =
+					rfcomm_mux_list[i].local_is_initiator;
+
+			if (dlci & 1)
+				return local_is_initiator ? 1 : 0;
+
+			return 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]));
+		}
+	}
+}
+
+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].in_use &&
+				rfcomm_mux_list[i].index == index &&
+				rfcomm_mux_list[i].handle == handle)
+			memset(&rfcomm_mux_list[i], 0,
+					sizeof(rfcomm_mux_list[i]));
+	}
+}
+
+/* Number of bytes of the PDU at pos/len within a reassembly buffer
+ * which come from the frame appended at cur, i.e. not shown yet.
+ */
+static uint16_t obex_frag_new(uint32_t pos, uint16_t len, uint32_t cur)
+{
+	if (pos >= cur)
+		return len;
+
+	if (pos + len <= cur)
+		return 0;
+
+	return pos + len - cur;
+}
+
 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, cur;
 
 	if (!channel)
 		return mcc_frame(rfcomm_frame, indent);
@@ -407,10 +586,78 @@ 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,
+			rfcomm_local_is_server(frame->index, frame->handle,
+								dlci),
+			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;
+	cur = frag->len;
+	frag->len += frame->size;
+
+	/* Bytes before cur were already shown with earlier frames */
+	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) {
+			/* Let obex_packet() report the invalid length */
+			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,
+					obex_frag_new(pos, remaining, cur));
+			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,
+					obex_frag_new(pos, pdu_len, cur));
+
+		pos += pdu_len;
+	}
+
+	if (pos == frag->len) {
+		obex_frag_reset(frag);
+	} else {
+		if (pos > cur)
+			packet_hexdump(frag->buf + pos, frag->len - pos);
+		else
+			packet_hexdump(frame->data, frame->size);
+
+		if (pos) {
+			memmove(frag->buf, frag->buf + pos, frag->len - pos);
+			frag->len -= pos;
+		}
+	}
 
 	return true;
 }
@@ -524,9 +771,23 @@ static uint16_t rfcomm_frame_packet(const struct l2cap_frame *frame)
 	if (ctype == 0xef) {
 		if (!uih_frame(&rfcomm_frame, indent))
 			goto fail_frame;
+	} else if (ctype == 0x2f) {
+		if (!RFCOMM_GET_DLCI(hdr.address))
+			rfcomm_set_mux_initiator(frame->index, frame->handle,
+								!frame->in);
 	} else if (ctype == 0x43 || ctype == 0x0f) {
-		obex_remove_channel(frame->index, frame->handle,
-					RFCOMM_GET_CHANNEL(hdr.address));
+		uint8_t dlci = RFCOMM_GET_DLCI(hdr.address);
+
+		if (!dlci) {
+			obex_remove_rfcomm_sessions(frame->index,
+								frame->handle);
+			rfcomm_remove_handle(frame->index, frame->handle);
+		} else {
+			obex_remove_channel(frame->index, frame->handle,
+									dlci);
+			rfcomm_remove_channel(frame->index, frame->handle,
+									dlci);
+		}
 	}
 
 	return consumed;
-- 
2.43.0


      parent reply	other threads:[~2026-12-04 16:04 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-04 16:04 [PATCH BlueZ v8 0/6] monitor: Add an OBEX protocol decoder to btmon Frédéric Danis
2026-12-04 16:04 ` [PATCH BlueZ v8 1/6] monitor: Strip trailing L2CAP FCS before upper layer dissectors Frédéric Danis
2026-12-04 17:12   ` monitor: Add an OBEX protocol decoder to btmon bluez.test.bot
2026-12-04 16:04 ` [PATCH BlueZ v8 2/6] monitor: Add OBEX protocol decoder Frédéric Danis
2026-12-04 16:04 ` [PATCH BlueZ v8 3/6] monitor: Decode PBAP/MAP/BIP Application Parameters tags Frédéric Danis
2026-12-04 16:04 ` [PATCH BlueZ v8 4/6] monitor: Reassemble L2CAP ERTM/Streaming SDUs Frédéric Danis
2026-12-04 16:04 ` [PATCH BlueZ v8 5/6] monitor: Fix RFCOMM UIH frame length and credit-only handling Frédéric Danis
2026-12-04 16:04 ` 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=20261204160416.1254097-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