Linux bluetooth development
 help / color / mirror / Atom feed
From: Simon Mikuda <simon.mikuda@streamunlimited.com>
To: linux-bluetooth@vger.kernel.org
Cc: Simon Mikuda <simon.mikuda@streamunlimited.com>
Subject: [PATCH BlueZ 2/8] shared/att: Use the smaller L2CAP MTU as ATT MTU
Date: Tue, 24 Nov 2026 15:46:36 +0100	[thread overview]
Message-ID: <20261124144642.298366-3-simon.mikuda@streamunlimited.com> (raw)
In-Reply-To: <20261124144642.298366-1-simon.mikuda@streamunlimited.com>

On an L2CAP channel with a dynamic CID (BR/EDR, EATT) bt_att uses the
outgoing MTU as ATT_MTU and reads into a buffer of that size. The
ATT_MTU is the smaller of the two MTUs of the channel (Core Vol 3,
Part G, 5.1.1 for BR/EDR and 5.3.1 for EATT), and a PDU the remote
sends up to our incoming MTU is cut short.

Use the smaller MTU as ATT_MTU, and read up to the incoming MTU.

Assisted-by: Claude:claude-opus-5-5
---
 src/shared/att.c | 43 ++++++++++++++++++++++++++++++++++++-------
 1 file changed, 36 insertions(+), 7 deletions(-)

diff --git a/src/shared/att.c b/src/shared/att.c
index 366ee83d8..45ae9698e 100644
--- a/src/shared/att.c
+++ b/src/shared/att.c
@@ -53,6 +53,7 @@ struct bt_att_chan {
 	bool in_req;			/* There's a pending incoming request */
 
 	uint8_t *buf;
+	uint16_t buf_len;
 	uint16_t mtu;
 };
 
@@ -1104,7 +1105,7 @@ static bool can_read_data(struct io *io, void *user_data)
 	uint8_t *pdu;
 	ssize_t bytes_read;
 
-	bytes_read = read(chan->fd, chan->buf, chan->mtu);
+	bytes_read = read(chan->fd, chan->buf, chan->buf_len);
 	if (bytes_read < 0)
 		return false;
 
@@ -1237,6 +1238,21 @@ static uint16_t io_get_mtu(int fd)
 	return 0;
 }
 
+static uint16_t io_get_imtu(int fd)
+{
+	socklen_t len;
+	struct l2cap_options l2o;
+
+	len = sizeof(l2o);
+	if (!getsockopt(fd, SOL_L2CAP, L2CAP_OPTIONS, &l2o, &len))
+		return l2o.imtu;
+
+	if (!getsockopt(fd, SOL_BLUETOOTH, BT_RCVMTU, &l2o.imtu, &len))
+		return l2o.imtu;
+
+	return 0;
+}
+
 static uint8_t io_get_type(int fd)
 {
 	struct sockaddr_l2 src;
@@ -1259,6 +1275,7 @@ static uint8_t io_get_type(int fd)
 static struct bt_att_chan *bt_att_chan_new(int fd, uint8_t type)
 {
 	struct bt_att_chan *chan;
+	uint16_t imtu;
 
 	if (fd < 0)
 		return NULL;
@@ -1283,15 +1300,24 @@ static struct bt_att_chan *bt_att_chan_new(int fd, uint8_t type)
 		/* fall through */
 	case BT_ATT_LE:
 		chan->mtu = BT_ATT_DEFAULT_LE_MTU;
+		chan->buf_len = chan->mtu;
 		break;
 	default:
+		/* The ATT_MTU of an L2CAP channel is the smaller of its two
+		 * MTUs (Core Vol 3, Part G, 5.1.1 and 5.3.1). Read up to the
+		 * incoming one, as L2CAP lets the remote send that much.
+		 */
 		chan->mtu = io_get_mtu(chan->fd);
+		imtu = io_get_imtu(chan->fd);
+		if (imtu)
+			chan->mtu = MIN(chan->mtu, imtu);
+		chan->buf_len = MAX(chan->mtu, imtu);
 	}
 
 	if (chan->mtu < BT_ATT_DEFAULT_LE_MTU)
 		goto fail;
 
-	chan->buf = malloc(chan->mtu);
+	chan->buf = malloc(chan->buf_len);
 	if (!chan->buf)
 		goto fail;
 
@@ -1487,14 +1513,17 @@ bool bt_att_set_mtu(struct bt_att *att, uint16_t mtu)
 	if (!chan)
 		return -ENOTCONN;
 
-	buf = malloc(mtu);
-	if (!buf)
-		return false;
+	if (mtu > chan->buf_len) {
+		buf = malloc(mtu);
+		if (!buf)
+			return false;
 
-	free(chan->buf);
+		free(chan->buf);
+		chan->buf = buf;
+		chan->buf_len = mtu;
+	}
 
 	chan->mtu = mtu;
-	chan->buf = buf;
 
 	if (chan->mtu > att->mtu) {
 		att->mtu = chan->mtu;
-- 
2.43.0


  parent reply	other threads:[~2026-11-24 14:46 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-24 14:46 [PATCH BlueZ 0/8] Fix GATT over BR/EDR Simon Mikuda
2026-11-24 14:46 ` [PATCH BlueZ 1/8] tools/btgatt-client: Add BR/EDR support Simon Mikuda
2026-11-24 20:44   ` Fix GATT over BR/EDR bluez.test.bot
2026-11-24 14:46 ` Simon Mikuda [this message]
2026-11-24 14:46 ` [PATCH BlueZ 3/8] device: Don't override the ATT MTU " Simon Mikuda
2026-11-24 14:46 ` [PATCH BlueZ 4/8] device: Don't resolve services on GATT " Simon Mikuda
2026-11-24 14:46 ` [PATCH BlueZ 5/8] gatt-client: Don't connect EATT on an ATT bearer " Simon Mikuda
2026-11-24 14:46 ` [PATCH BlueZ 6/8] profiles: Set the LE bearer for TMAP, GMAP and RAP Simon Mikuda
2026-11-24 14:46 ` [PATCH BlueZ 7/8] device: Don't accept LE profiles over ATT on BR/EDR Simon Mikuda
2026-11-24 14:46 ` [PATCH BlueZ 8/8] device: Connect GATT over BR/EDR with ConnectProfile Simon Mikuda

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=20261124144642.298366-3-simon.mikuda@streamunlimited.com \
    --to=simon.mikuda@streamunlimited.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