Bluetooth: L2CAP: Use a reserved ident for L2CAP_LE_CREDITS
Subsystems (2)
Luiz Augusto von Dentz
maintainerPATCH v3 1/1 Bluetooth: L2CAP: Use a reserved ident for L2CAP_LE_CREDITS
From: Nguyen Ngoc Thang <[email protected]>
L2CAP_LE_CREDITS has no response, so an ident taken from conn->tx_ida for it is never released by l2cap_put_ident() and l2cap_chan_le_send_credits() has to free it right away after sending, otherwise the range is exhausted after 254 packets and credits would be sent with the invalid ident 0, which some remote stacks ignore, stalling the channel.
tx_ida only exists to track requests awaiting a response, so a command without one has no business in it: every credit update allocates and frees an ident, and the ident is visible in tx_ida while the credits are in flight, so an answer to them (a L2CAP_COMMAND_REJ) is processed by l2cap_put_ident() like the answer to a request.
Using a counter that doesn't consult tx_ida is not an option either, it can return the ident of a request still outstanding, and a reject of the credits would then release that request's ident and close its channel in l2cap_le_command_rej().
Reserve ident 255 for L2CAP_LE_CREDITS instead: l2cap_get_ident() never returns it (the BR/EDR range already ends at 128), so it can't collide with any request, it doesn't have to be tracked in conn->tx_ida, and a reject of it matches no outstanding request.
--- include/net/bluetooth/l2cap.h | 5 +++++ net/bluetooth/l2cap_core.c | 28 +++++++++++----------------- 2 files changed, 16 insertions(+), 17 deletions(-)
include/net/bluetooth/l2cap.h +5 -0
@@ -45,6 +45,11 @@#define L2CAP_DEFAULT_ACC_LAT 0xFFFFFFFF#define L2CAP_BREDR_MAX_PAYLOAD 1019/* 3-DH5 packet */ #define L2CAP_LE_MIN_MTU 23 + +/* Identifier used by L2CAP_LE_CREDITS, it has no response so it is not +* tracked in conn->tx_ida and is never allocated for any other command. +*/ +#define L2CAP_IDENT_LE_CREDITS 255#define L2CAP_ECRED_CONN_SCID_MAX 5#define L2CAP_DISC_TIMEOUT msecs_to_jiffies(100)
net/bluetooth/l2cap_core.c +11 -17
@@ -1046,9 +1046,11 @@ static int l2cap_get_ident(struct l2cap_conn *conn)u8 max;int ident; -/* LE link does not support tools like l2ping so use the full range */ +/* LE link does not support tools like l2ping so use the full range, +* except for L2CAP_IDENT_LE_CREDITS which is reserved. +*/ if (conn->hcon->type ==LE_LINK ) - max =255 ; + max =L2CAP_IDENT_LE_CREDITS -1 ;/* Get next available identificator. * 1 - 128 are used by kernel. * 129 - 199 are reserved.
@@ -7204,7 +7206,6 @@ static void l2cap_chan_le_send_credits(struct l2cap_chan *chan)struct l2cap_conn *conn = chan->conn;struct l2cap_le_credits pkt;u16 return_credits =l2cap_le_rx_credits (chan); -int ident;if (chan->mode !=L2CAP_MODE_LE_FLOWCTL && chan->mode !=L2CAP_MODE_EXT_FLOWCTL )
@@ -7222,21 +7223,14 @@ static void l2cap_chan_le_send_credits(struct l2cap_chan *chan) pkt.cid =cpu_to_le16 (chan->scid); pkt.credits =cpu_to_le16 (return_credits); -/* Keeps l2cap_put_ident() from releasing ident before we do */ -lockdep_assert_held (&conn->lock); - - ident =l2cap_get_ident (conn); - -l2cap_send_cmd (conn, ident,L2CAP_LE_CREDITS ,sizeof (pkt), &pkt); - -/* L2CAP_LE_CREDITS has no response so the ident is never released by -* l2cap_put_ident() - release it right away, otherwise the tx_ida -* range is exhausted after 254 packets and from then on credits are -* sent with the invalid ident 0, which some remote stacks ignore, -* stalling the channel. +/* L2CAP_LE_CREDITS has no response so it uses the reserved ident rather +* than one from conn->tx_ida, which would have to be released right +* away or would be exhausted after 254 packets, and could otherwise +* collide with the ident of a request still outstanding. A reject of it +* can't match any request either. */ -if (ident >0 ) -ida_free (&conn->tx_ida, ident); +l2cap_send_cmd (conn,L2CAP_IDENT_LE_CREDITS ,L2CAP_LE_CREDITS , +sizeof (pkt), &pkt); }void l2cap_chan_rx_avail (struct l2cap_chan *chan,ssize_t rx_avail)
base-commit: f42f5cae154b90a0cb6d8232dc84c7614ca2e6a0
Signature · 1 line
2.56.0
This patch was applied to bluetooth/bluetooth-next.git (master) by Luiz Augusto von Dentz <[email protected]>:
Hello:
This patch was applied to bluetooth/bluetooth-next.git (master) by Luiz Augusto von Dentz <[email protected]>:
Quoted 9 lines · On Wed, 18 Nov 2026 09:27:32 -0500 you wrote:
From: Nguyen Ngoc Thang <[email protected]>
L2CAP_LE_CREDITShas no response, so an ident taken fromconn->tx_idafor it is never released byl2cap_put_ident()andl2cap_chan_le_send_credits()has to free it right away after sending, otherwise the range is exhausted after 254 packets and credits would be sent with the invalid ident 0, which some remote stacks ignore, stalling the channel.[...]
Here is the summary with links:
- [v3] Bluetooth: L2CAP: Use a reserved ident for L2CAP_LE_CREDITS
https://git.kernel.org/bluetooth/bluetooth-next/c/06b6fbe05f5c
You are awesome, thank you!
Signature · 2 lines
Deet-doot-dot, I am a bot. https://korg.docs.kernel.org/patchwork/pwbot.html