From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.221.175 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796151815; b=CIGV3jd9SXHUp4UgpU82fHAlfI3r0bCxXWOieaDn1ZhhH6w9Z3yWdeCuOHA9G+ORih8S jJ0HmjOKWoGEy1FOoLbLkWBMwPxMptTPAemwK0UsVRa4BqEEw7sFSnHp5HOb0kXeBSWRw SSwSED/i1XFVE9A8fCKRR79cQwevdbQO6WbsTNEeAa3ZICNw8uA701pS1Hii44pzE8LVZ 9taL1cUFX2iZ99eoJMcdMFbYD4lK0ma/853RwKQANMed5aD8KvjmhxdFNqpfOKicEaw1A tTbQshejNkkGBNi5Vj7EDV4rFF4nX5pHXqesufiRln5TzpKCrffyIKl82JcNpopWzQg== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796151815; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Gm-Message-State:X-Gm-Gg:X-Received:Received:From:To:Subject:Date: Message-ID:X-Mailer:In-Reply-To:References:MIME-Version: Content-Transfer-Encoding; bh=1h+crFKlL0Gm0gLrDPJek5c0UQpFnzfYQOQ1yRlJM2s=; b=Ckr6+gHVntthzfNy/hRpEKVP3y8TjRtUP3cq0yOU10drPVUbc02BIqokpfZdeA0qoLJ2 bnVCsvkX1gHUSM+yWkdRJA0F/bU0H7aNKBTTcDxbUX/BlMVcxiWDcuQ7cDZYENk1JsAVn 34M4xi2/C+oSm/kWK9U/Vx6XHCHsTCzDsKzqRBW+JQDOMr2B5FZaVMRh0pdHx3izmPuP/ a7RTofKFQDAcRjbsMG1RIbCPKzpYvY/pzr00eM6gfTy/iiWOsH+51gOjhA8DKSvRL4xnb h6GcoUU1EgU0OMO1HRyVPK2ZkNAIS+CX46mqs/yGKtofT7aYYU8rn3fRbbljlMYe1TA== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=BTrFqR3u; arc=none smtp.remote-ip=209.85.221.175 Received: from mail-vk1-f175.google.com (mail-vk1-f175.google.com [209.85.221.175]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 69791606A32F for ; Tue, 1 Dec 2026 19:03:35 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 69791606A32F Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=BTrFqR3u Received: by mail-vk1-f175.google.com with SMTP id 71dfb90a1353d-5eac319a6c2so8210509e0c.0 for ; Tue, 01 Dec 2026 11:03:35 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20260707; t=1796151814; x=1796756614; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:from:to:cc:subject:date:message-id :reply-to:content-type; bh=1h+crFKlL0Gm0gLrDPJek5c0UQpFnzfYQOQ1yRlJM2s=; b=BTrFqR3uP4FxJ8wWWqoIzT9jDE+79SpazgIV8mYIC+Z87UopgZScqjQyVMWuQqGEbT o3VvIYOMVcrqeoYQeeypTvfefwHj0Gc78iS9MN5ATGHUVnwIzKt+JLfyO8LsyU8tr625 bHFPBrbuhGNYrYVUxKUdoHTNPiX+dtYdwZ1QQC/Nur0dYy2+iYnELE/k/JjmcCUJwIqR uULv6ZjFvAZV5vtoL/aYir6z/STybSw3dNPmt6wsN5EUe34kfhAL9jrnR3x5FCHHpSmC Zizvdc/8iU7/OEP+uVw5FOljgKvSuNhDz6PkYJiohaeu2XTnIf/ZFsPMDuBzOiojZkny alOg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796151814; x=1796756614; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:x-gm-gg:x-gm-message-state:from:to :cc:subject:date:message-id:reply-to:content-type; bh=1h+crFKlL0Gm0gLrDPJek5c0UQpFnzfYQOQ1yRlJM2s=; b=J/lED+lm8qRTMdYYJLBAhyuOXG3n6ucVf72Q9OSvZuTGyiMqUF2Hs5fQGIv7CQ2ovI ltRUIRY22wczI1iROlpaF/cjnbis8qNlAehHaDnzeQOynNZxG8jqIgbflkUFu4WxMIQH zUWZo+dMZNykk5Ak5TJXWguDAwIZOGqDIdZXPwgEknEGs720rVNSnStvJrYigVWgUXIj c4sZpu6IVFC4uRGaQZHwtTZR4sAux/oqXGmWEFD6d/b1x86QcRifOeDf+x0Yp5/W94m/ b9FP39BtEeSqdC0mD+AtrfHHVT2r+di/C66ob3an2DtfJ1kfL3cAwX9fMu7gC6/DINzK ro7g== X-Gm-Message-State: APuU4Z/YvPEsbriL7BwVAeysMiTZTOkHBpIcRnZmwA6GgIJXddZyIIYZ 9eRMIGIWscxx6MbcdMXna6L1FpjN/Nk99a6kAyKqUXhpIS4LDPctg6AxwDixoo3d2dg= X-Gm-Gg: AZFdTZdvItqqB9U06r9lKTKZ1z6clw3kqkYl2isOGcdQ73RnEFvZfBZ/dhEEuULIoTh vjNU1RAnvTGuzmaF7cV+qnSOKJHDgbLotzKbUkEjSmyxEy8Tp+MXr2RcK3rUnmIyYoeMOzZ2Gym N5fI7eT7rl3wtOKTrVHKgaRI8/iMoz7T6BiX6zRC3TgfEFY3PdsDDvd27vYuezNjEVtmsYczx9W oTOGPWc7vm5e8p3eIBqQySrR5fvxrZal41hNSUqXFGTHn0BCvhoDpUUWUo1o2vFUbjTjVRt+EwX EJb8ffdrI/Oi4IRQTtb3eAjru8YYsWIxHLJ7AByOxI17I3mX3EB6t5j9GiULBCMUD0/2Ip8mUxK gWn6S8M9ZHLMCm0vy8in/Ks2i/NSYlMrg42bDyV2Lfi5cjzB3nizFPdIeOVkdoJ0OOE4NsOmg9Y FV9bAt5BrJZnisumaMqFsL2N5TQu7aEIoS/iLG6MwtejbRxccEmrlr3tzxKXjYMpZOCEVMd4ru6 vKkRfYuFivK15jeG4xip3PdOB0XaFibHvhgT3bhD8zjsPEQllCJD0hprig4aIV6WkoA60JAa6tD I2p3H0PBfPHPzjq/fOCjfCF/JTkoxJdl9FoIcoIVshvK8uSfoAwZYn7JYATxoCVEsWc/jVuGEUC SQIl05Bv8uPO27SvlymJ2ZozcZYQ1lL3CmtehlU/3kS5nSU/u/FEw4Ws= X-Received: by 2002:a05:6122:4687:b0:5d0:1207:b693 with SMTP id 71dfb90a1353d-5ee8adf6ba3mr27531684e0c.1.1796151814289; Tue, 01 Dec 2026 11:03:34 -0800 (PST) Received: from lvondent-mobl5 ([72.188.211.115]) by smtp.gmail.com with ESMTPSA id a1e0cc1a2514c-99e6dfc0b22sm38919229241.6.2026.12.01.11.03.33 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 01 Dec 2026 11:03:33 -0800 (PST) From: Luiz Augusto von Dentz To: linux-bluetooth@vger.kernel.org Subject: [PATCH v12 2/6] Bluetooth: L2CAP: Fix deferred LE channels being disconnected once accepted Date: Tue, 1 Dec 2026 14:03:21 -0500 Message-ID: <20261201190325.110874-3-luiz.dentz@gmail.com> X-Mailer: git-send-email 2.56.1 In-Reply-To: <20261201190325.110874-1-luiz.dentz@gmail.com> References: <20261201190325.110874-1-luiz.dentz@gmail.com> X-Mailing-List: linux-bluetooth@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit From: Luiz Augusto von Dentz When userspace authorizes a deferred LE Credit Based or Enhanced Credit Based channel, l2cap_sock_defer() moves it to BT_CONNECTED directly instead of with l2cap_chan_ready(), so the timer armed when the request was received is not stopped and CONF_NOT_COMPLETE is not cleared. The timer then fires on the established channel and disconnects it, and since CONF_NOT_COMPLETE is still set l2cap_chan_del() returns before purging its tx queue. Fix this by completing LE Credit Based channels with l2cap_chan_ready() once the response is sent, as done for the ones that are not deferred. Enhanced Credit Based channels stay in BT_CONNECTED while the response of their request is pending, so their timer is stopped when they are authorized, as only the channels still pending shall time out, and the rest of what l2cap_chan_ready() does is done once the response is sent. Since the response may wait for other channels of the request, they are also suspended until then so they can't send data before it, and their configuration is cleared before deleting them if they end up refused, so their queue is purged. l2cap_sock_resume_cb() now locks the socket with the nesting level of the channel, like l2cap_sock_teardown_cb(), since it is called by the response with a sibling channel locked. Fixes: 27e2d4c8d28b ("Bluetooth: Add basic LE L2CAP connect request receiving support") Fixes: 15f02b910562 ("Bluetooth: L2CAP: Add initial code for Enhanced Credit Based Mode") Assisted-by: OpenCode:claude-fable-5.2 Signed-off-by: Luiz Augusto von Dentz --- include/net/bluetooth/l2cap.h | 3 ++- net/bluetooth/l2cap_core.c | 27 +++++++++++++++++++++++++-- net/bluetooth/l2cap_sock.c | 16 +++++++++++++--- 3 files changed, 40 insertions(+), 6 deletions(-) diff --git a/include/net/bluetooth/l2cap.h b/include/net/bluetooth/l2cap.h index 72d9d56c6f2a..e1820b7dca67 100644 --- a/include/net/bluetooth/l2cap.h +++ b/include/net/bluetooth/l2cap.h @@ -978,7 +978,8 @@ int l2cap_init_sockets(void); void l2cap_cleanup_sockets(void); bool l2cap_is_socket(struct socket *sock); -void __l2cap_le_connect_rsp_defer(struct l2cap_chan *chan); +void __l2cap_le_connect_rsp_defer(struct l2cap_chan *chan) + __must_hold(&chan->lock) __must_hold(&chan->conn->lock); void __l2cap_ecred_conn_rsp_defer(struct l2cap_chan *chan) __must_hold(&chan->lock) __must_hold(&chan->conn->lock); void __l2cap_connect_rsp_defer(struct l2cap_chan *chan); diff --git a/net/bluetooth/l2cap_core.c b/net/bluetooth/l2cap_core.c index 943effc86740..be07b7b9cfb0 100644 --- a/net/bluetooth/l2cap_core.c +++ b/net/bluetooth/l2cap_core.c @@ -4027,6 +4027,8 @@ static int l2cap_build_conf_rsp(struct l2cap_chan *chan, void *data, } void __l2cap_le_connect_rsp_defer(struct l2cap_chan *chan) + __must_hold(&chan->lock) + __must_hold(&chan->conn->lock) { struct l2cap_le_conn_rsp rsp; struct l2cap_conn *conn = chan->conn; @@ -4042,6 +4044,11 @@ void __l2cap_le_connect_rsp_defer(struct l2cap_chan *chan) l2cap_send_cmd(conn, chan->ident, L2CAP_LE_CONN_RSP, sizeof(rsp), &rsp); chan->ident = 0; + + /* The response establishes the channel, which also stops its timer + * and completes its configuration like for one that is not deferred. + */ + l2cap_chan_ready(chan); } static void l2cap_ecred_list_defer(struct l2cap_chan *chan, void *data) @@ -4145,10 +4152,26 @@ static void l2cap_ecred_rsp_defer(struct l2cap_chan *chan, void *data) rsp->pdu.rsp.result = cpu_to_le16(L2CAP_CR_LE_NO_MEM); /* Include all channels pending with the same ident */ - if (!rsp->pdu.rsp.result) + if (!rsp->pdu.rsp.result) { rsp_flex->dcid[rsp->count++] = cpu_to_le16(chan->scid); - else + + /* The response establishes the channel, which was already + * moved to BT_CONNECTED when userspace authorized it, so + * complete what l2cap_chan_ready() does, and let it send + * data, which was suspended until now, if it has credits. + */ + chan->conf_state = 0; + if (!chan->tx_credits) + chan->ops->suspend(chan); + else + chan->ops->resume(chan); + } else { + /* Nothing was sent by the channel, but clear its configuration + * so its queue is purged like for any other. + */ + chan->conf_state = 0; l2cap_chan_del(chan, ECONNRESET); + } done: if (locked) diff --git a/net/bluetooth/l2cap_sock.c b/net/bluetooth/l2cap_sock.c index d5663a4642a9..9ead47ca846e 100644 --- a/net/bluetooth/l2cap_sock.c +++ b/net/bluetooth/l2cap_sock.c @@ -1287,11 +1287,17 @@ static int l2cap_sock_defer(struct sock *sk) } else if (chan->mode == L2CAP_MODE_EXT_FLOWCTL) { sk->sk_state = BT_CONNECTED; chan->state = BT_CONNECTED; + /* Data can't be sent until the response of the request + * is sent, which may wait for other channels of it. + */ + set_bit(BT_SK_SUSPEND, &bt_sk(sk)->flags); release_sock(sk); + /* The channel is accepted, only the ones still pending + * shall time out while waiting for the response. + */ + __clear_chan_timer(chan); __l2cap_ecred_conn_rsp_defer(chan); } else if (bdaddr_type_is_le(chan->src_type)) { - sk->sk_state = BT_CONNECTED; - chan->state = BT_CONNECTED; release_sock(sk); __l2cap_le_connect_rsp_defer(chan); } else { @@ -1894,7 +1900,11 @@ static void l2cap_sock_resume_cb(struct l2cap_chan *chan) if (!sk) return; - lock_sock(sk); + /* Like l2cap_sock_teardown_cb(), lock the socket with the nesting level + * of the channel since this may be called with a sibling channel + * locked, e.g. by the response of an Enhanced Credit Based request. + */ + lock_sock_nested(sk, atomic_read(&chan->nesting)); if (test_and_clear_bit(FLAG_PENDING_SECURITY, &chan->flags)) { sk->sk_state = BT_CONNECTED; -- 2.56.1