Received: by 2002:ab2:23c8:0:b0:1f2:fdbc:cb93 with SMTP id a8csp52155lqe; Tue, 26 Mar 2024 20:22:24 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCUnSHC1ao+Adl15nt0OjN14p8PFmLLAqhouKCdAm0m7EPoCVRUa0LiSr9DR8zhHN1YzGkc8SW+CWGPpCc9xIkUgy48odAYyj0M1K+Vk8A== X-Google-Smtp-Source: AGHT+IF9jUbhmL5pbKjM97tw4zVcuZxv9jIe7ab9MkB2ug50dCZ2W1lEaQtyw/UnePIdllCu6QsH X-Received: by 2002:a05:6214:21ad:b0:696:9aae:5d9f with SMTP id t13-20020a05621421ad00b006969aae5d9fmr1693377qvc.3.1711509744036; Tue, 26 Mar 2024 20:22:24 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1711509744; cv=pass; d=google.com; s=arc-20160816; b=DT3CLx5Ho34wlBttP1o7RxpJOo+aq/45Do0QkcyttM/fnFSEaNj2141ngrteafcjX9 zPIEWPCDCOH3xLOufk/tc+CyFWjn9PLQd2IzisK812yK7pgA8rAaYGUp58IiGsnEcJ5r OUvN+MNMFN9b7IZb3pLjoDTxVBLLCefIIlrC9xpW1GYnDLsKUpHVlRYF87SR3vAu1KGc 6aLVBjlGYRHmH+Jf/m5n89Zj0LYJDfFUKL4ASdRoXVxQSNgC9ONl7XrjDnyjpiEaybxp tF83Z1ogRVLBmr+KuCYYq+KFbTqEPBaswhBexAN71H9VlDYvsbwjVm8jwYVeSRXAEV3H xvMw== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:in-reply-to:from:references:cc:to :content-language:subject:user-agent:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:date:message-id:dkim-signature; bh=MZ3c9h096T33Eu5/duuVeVeDk8cclsgCLQJtUuzUXnE=; fh=HLDvd6b7ndsqkA43oiXosoRg08FXXdVWzEm6f/3zVMA=; b=MdLCMln6IvEAjuzAjxJabSZAXO2K2ilUOiYHCLDEGdHZbZ5rSYV6avqN5xW9x+ZpqD ipf0jKS1j4YAnnnTo6Lv5lg57rL2bzSMBj0mJCq8gONyUu0bTas5kpXUotoe3NKAt3Fu OiV76lZ9XCJnIVk8J8hnwO6OUT/gPWSpy981Vhc8D/qqeYJ+gyRR5qx4uTnyskHLL3+w GV744RtyebgUByjl4mYwCwP9LfA1DbFbicJ6Hz1Z9oC6MX61oeTWLyLVAtARXmB3BWJr PRBOd4y9DpR+a381LjzOguG2VOGlEAp0MdfyJArVyPSM6QwpWBeubNmONCPSgUBF6S1L AGjw==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@canonical.com header.s=20210705 header.b=bhbUsBnF; arc=pass (i=1 spf=pass spfdomain=canonical.com dkim=pass dkdomain=canonical.com dmarc=pass fromdomain=canonical.com); spf=pass (google.com: domain of linux-bluetooth+bounces-2809-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-bluetooth+bounces-2809-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=canonical.com Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [2604:1380:45d1:ec00::1]) by mx.google.com with ESMTPS id iw10-20020a0562140f2a00b0069698c6c014si3766299qvb.242.2024.03.26.20.22.23 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 26 Mar 2024 20:22:24 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-bluetooth+bounces-2809-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) client-ip=2604:1380:45d1:ec00::1; Authentication-Results: mx.google.com; dkim=pass header.i=@canonical.com header.s=20210705 header.b=bhbUsBnF; arc=pass (i=1 spf=pass spfdomain=canonical.com dkim=pass dkdomain=canonical.com dmarc=pass fromdomain=canonical.com); spf=pass (google.com: domain of linux-bluetooth+bounces-2809-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-bluetooth+bounces-2809-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=canonical.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ny.mirrors.kernel.org (Postfix) with ESMTPS id B18301C22F37 for ; Wed, 27 Mar 2024 03:22:23 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 1AD67224DC; Wed, 27 Mar 2024 03:22:19 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=canonical.com header.i=@canonical.com header.b="bhbUsBnF" X-Original-To: linux-bluetooth@vger.kernel.org Received: from smtp-relay-canonical-0.canonical.com (smtp-relay-canonical-0.canonical.com [185.125.188.120]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id CA84E17545 for ; Wed, 27 Mar 2024 03:22:15 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=185.125.188.120 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711509738; cv=none; b=ODAAzKF0P/YFKaX24whAIuhge1L4CBR61eH5FCD1w+mHRoSDlCZebc9k6u0PSqk8GRpSY/pkPDIcJP3osGsCwyODl3urhFeKAaSCqhOJd8903yykUeqmKVGKXVarfRz4MZFWlv17VtLNSQC0sO+mo9LrNS+o9wi6FKM2gzk8W2k= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711509738; c=relaxed/simple; bh=puAktRmiHC/LhWygH1Fql4azP1pr8tgq5ea9PVJfvR4=; h=Message-ID:Date:MIME-Version:Subject:To:Cc:References:From: In-Reply-To:Content-Type; b=Dod1B0VFParsbNQu7i49VWB9pPb4rxKhS3e00d2/4ji8cPCW8xblUx3TFVym5VRbexaUf6vwJkIm65d1iHgInXhWf3LB2agVpDhBfulYA8bU5aIly4S3nDlLWDu7VwsUbVcRFC3JcQnewuhCQIRUe2+j7+H6DR2XkWx4vjjafb8= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=canonical.com; spf=pass smtp.mailfrom=canonical.com; dkim=pass (2048-bit key) header.d=canonical.com header.i=@canonical.com header.b=bhbUsBnF; arc=none smtp.client-ip=185.125.188.120 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=canonical.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=canonical.com Received: from [192.168.0.106] (unknown [123.112.66.108]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by smtp-relay-canonical-0.canonical.com (Postfix) with ESMTPSA id 01B863F313; Wed, 27 Mar 2024 03:22:11 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=canonical.com; s=20210705; t=1711509733; bh=MZ3c9h096T33Eu5/duuVeVeDk8cclsgCLQJtUuzUXnE=; h=Message-ID:Date:MIME-Version:Subject:To:Cc:References:From: In-Reply-To:Content-Type; b=bhbUsBnFprOunkyuBwzsVVgpLQwBUQSEKdTSQBY7PaEQz/93uMNrPXvYICoTgYb1w natMiERHsythmHsRoVMfCdgXsTLsuH31+3+GmICy/gpBfO6kg4QB8R45p8rFJu0c27 WMpYY0Pv6/t+MFsgEFMF3er1H32dknYH76g++jHIXPYUp75qySqX0zAI5WrfdHPFb6 863tKN78HjnDTDNqiPgQMJqoeFjhzYQgDSgrqxw7HmigIx0H1LnqQW/H7VB0vuBKF8 QTGX+eQI19C2Q/OHo4XNhYcb2nw6I92ijjAUbFb0FwNScL4YX4lWK/SCjNDipCEKnR XmHpCWWsuuMwQ== Message-ID: Date: Wed, 27 Mar 2024 11:22:08 +0800 Precedence: bulk X-Mailing-List: linux-bluetooth@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH] Bluetooth: hci_event: conn is already encryped before conn establishes Content-Language: en-US To: Luiz Augusto von Dentz Cc: linux-bluetooth@vger.kernel.org, marcel@holtmann.org, johan.hedberg@gmail.com References: <20240325061841.22387-1-hui.wang@canonical.com> From: Hui Wang In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit On 3/26/24 00:59, Luiz Augusto von Dentz wrote: > Hi Hui, > > On Mon, Mar 25, 2024 at 2:19 AM Hui Wang wrote: >> We have a BT headset (Lenovo Thinkplus XT99), the pairing and >> connecting has no problem, once this headset is paired, bluez will >> remember this device and will auto re-connect it whenever the device >> is power on. The auto re-connecting works well with Windows and >> Android, but with Linux, it always fails. Through debugging, we found >> at the rfcomm connection stage, the bluetooth stack reports >> "Connection refused - security block (0x0003)". >> >> For this device, the re-connecting negotiation process is different >> from other BT headsets, it sends the Link_KEY_REQUEST command before >> the CONNECT_REQUEST completes, and it doesn't send ENCRYPT_CHANGE >> command during the negotiation. When the device sends the "connect >> complete" to hci, the ev->encr_mode is 1. >> >> So here in the conn_complete_evt(), if ev->encr_mode is 1, link type >> is ACL and HCI_CONN_ENCRYPT is not set, we set HCI_CONN_ENCRYPT to >> this conn, and update conn->enc_key_size accordingly. >> >> After this change, this BT headset could re-connect with Linux >> successfully. > I suspect it is doing Security Mode 3, so it establishes the > encryption _before_ the connection handle which probably disables > EncryptionChange since there is no handle at that point. That said I > don't remember what we shall do with respect to AES since the > Encryption_Enabled field can only be set to 0x00 or 0x01 so I assume > the later can only be: > > Link Level Encryption is ON with E0 for BR/EDR. Thanks for sharing this, it is sth I will study. :-) >> Signed-off-by: Hui Wang >> --- >> This is the btmon log for auto re-connecting negotiation: >> Before applying this patch: >> https://pastebin.com/NUa9RJv8 >> >> After applying the patch: >> https://pastebin.com/GqviChTC > Lets add these to the patch description to be more informative about > the change, you can strip the unrelated traffic at the start and end > and just focus on what changes to the traffic the patches introduces. OK, will address it in the v2. Thanks, Hui. > >> net/bluetooth/hci_event.c | 26 ++++++++++++++++++++++++++ >> 1 file changed, 26 insertions(+) >> >> diff --git a/net/bluetooth/hci_event.c b/net/bluetooth/hci_event.c >> index 4ae224824012..0c45beb08cb2 100644 >> --- a/net/bluetooth/hci_event.c >> +++ b/net/bluetooth/hci_event.c >> @@ -3208,6 +3208,32 @@ static void hci_conn_complete_evt(struct hci_dev *hdev, void *data, >> if (test_bit(HCI_ENCRYPT, &hdev->flags)) >> set_bit(HCI_CONN_ENCRYPT, &conn->flags); >> >> + /* "Link key request" completed ahead of "connect request" completes */ >> + if (ev->encr_mode == 1 && !test_bit(HCI_CONN_ENCRYPT, &conn->flags) && >> + ev->link_type == ACL_LINK) { >> + struct link_key *key; >> + struct hci_cp_read_enc_key_size cp; >> + >> + key = hci_find_link_key(hdev, &ev->bdaddr); >> + if (key) { >> + set_bit(HCI_CONN_ENCRYPT, &conn->flags); >> + >> + if (!(hdev->commands[20] & 0x10)) { >> + conn->enc_key_size = HCI_LINK_KEY_SIZE; >> + goto notify; >> + } >> + >> + cp.handle = cpu_to_le16(conn->handle); >> + if (hci_send_cmd(hdev, HCI_OP_READ_ENC_KEY_SIZE, >> + sizeof(cp), &cp)) { >> + bt_dev_err(hdev, "sending read key size failed"); >> + conn->enc_key_size = HCI_LINK_KEY_SIZE; >> + } >> +notify: >> + hci_encrypt_cfm(conn, ev->status); >> + } >> + } >> + >> /* Get remote features */ >> if (conn->type == ACL_LINK) { >> struct hci_cp_read_remote_features cp; >> -- >> 2.34.1 >> > > -- > Luiz Augusto von Dentz