Received: by 2002:ab2:6857:0:b0:1ef:ffd0:ce49 with SMTP id l23csp2661711lqp; Mon, 25 Mar 2024 06:04:04 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCW4ekCTHw6W79zdq/SmnbBV9n6UXor1f8SWen9eIq4vgiAfbCOxRDIm2shUcEjYvxKhYXbn4uVlLbL7ETdR6El1qC5QCa35YS24BW5AZA== X-Google-Smtp-Source: AGHT+IG7tdB59NRbA8VhuHChiOVnRWtATFB/MKTXe/z9HDf15GJtgq9X1KSxwCGJSe75+Fhp8dzY X-Received: by 2002:a05:6870:418d:b0:229:ec0e:7348 with SMTP id y13-20020a056870418d00b00229ec0e7348mr7971707oac.46.1711371843984; Mon, 25 Mar 2024 06:04:03 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1711371843; cv=pass; d=google.com; s=arc-20160816; b=1Jq4ckuRO8vLtpdRIUM2gnBRMqOOqaPXWefezXEHQuG5jQof0/eHHna8IsnEwhdTCO 6gSYR5A8EppbKL5PlPNytR4LBtZL5LaY8KlJ3Gw78MgN8DGiRLCP+zFquUnvC1OFGvdl N5hHwjQR7PuSz7xCo05QXaFG4CBBVxLAiPROXbysbRru5sFzz+6Auk7lFd12evu68ATQ R2ve+M5xfYB7LEIekuPFqr9Ys+jI80ekKHlgQqohArb72M5hDM5/R84HCouqT1zKsQi3 JReNeSDTqPHnYtA85Xa3N1QBCbsY7r1nuv935jcQlTerxlUM1IiLhDAKbwlncpDLYZQ3 zdSQ== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:message-id:date:subject:to:from :dkim-signature; bh=/XfgtjibyARS5nJRafQ1ktx5gaZeNUQBlANLlHqMT28=; fh=eYwwU5NtTSErMn/z/xPrZaKUhTSYf8ZkmlYDur9rkho=; b=HuHidBK31sItl/oUhEG0C1fqIob0DW93NYVo/uGr1AcHEpS89qAkJojNU2yBkNY8T/ kjlrri8JG+cRfPUQtRCJmk+0z5rRVG2IhfLKKwbc09P5OM1Xo47VXN+prLE38h2reIdz u+9tpvw31mOLm6wH55APNwn7mUNrAziOiMBcc+UKIV/HRXt+yOX4E7Ih1wNl52crEEil ZVDq3Y90Jt7YspBrSovdsHWyZ05D+4BASk8QI9R0gTLpvBTX4xIoUlEpk4DlMZaKrPjS vlI3dXKW6Dbx985PGJw2WqjvAOTpkXw0vpWAlO8XgpBQ/gfDVoIENFkRUANPJQIoTKw2 v7ig==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@canonical.com header.s=20210705 header.b=DwD6j643; 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-2716-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-bluetooth+bounces-2716-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 i6-20020a0561020a4600b00477a21b3a97si97656vss.106.2024.03.25.06.04.03 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 25 Mar 2024 06:04:03 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-bluetooth+bounces-2716-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=DwD6j643; 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-2716-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-bluetooth+bounces-2716-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 AD98B1C35F6C for ; Mon, 25 Mar 2024 13:04:03 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 840EF1552FD; Mon, 25 Mar 2024 08:34:35 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=canonical.com header.i=@canonical.com header.b="DwD6j643" 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 6C086155397 for ; Mon, 25 Mar 2024 06:19:25 +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=1711347568; cv=none; b=ktfTBpyhmdNoGVgGOGUQWUM2FgCQDrYzASE5E+VD5DdZL4+ikXGjbsiX4J50X3p5ENm9Bl9SjCJgOm18PvzgP5lmWdI630vAqescw95eRhuUDCI8jlUu8WB7Ww/y++xVhxpYe17qmjP1ODmS84hVgyjJ8p/QXapAv+xMJ2nKlbk= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711347568; c=relaxed/simple; bh=8aJAQTQbIyKL/euP/RmNjqmDZxOeB+RtT3g2oXBTYS8=; h=From:To:Subject:Date:Message-Id:MIME-Version; b=lxfJ36ceYxs37eGAOZ6WIHCKGwBqFKY+WXRyd3EaCP7Lu49LQpQln5ttnJ+d8ZiZlt7qyet7M2v25egDwuXwpDZtYvA0zi/hecDeUXXT2xAETdeZTi8DBoqRkLRKJ7IlNPWqzO6kTUEVJshA192cJYsPxFyClZSIzn3oaC+dXaI= 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=DwD6j643; 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 hwang4-ThinkPad-T14s-Gen-2a.conference (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 69E013F2AE; Mon, 25 Mar 2024 06:19:15 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=canonical.com; s=20210705; t=1711347557; bh=/XfgtjibyARS5nJRafQ1ktx5gaZeNUQBlANLlHqMT28=; h=From:To:Subject:Date:Message-Id:MIME-Version; b=DwD6j6437+iGl0eBWdWsSEvS8soHKgU7/P4n2tIlua8DQaw81p2YYvIZ6OjicC7wi 0TTWNaRdEHetFMOqZahuqUkhCEakHjVO7P8x5685VibMmGOQZFzliY337QPIXOGg53 jeXnTDpJ6QwkrWJwdod3TcPGOX1zFCJD6YAx2pl2gAtNC1HCuo8SdEtXcXdEJYlsCD JH3eLX1VtpsBW5B2OalNyavTGIadw3X+48F9InurBQnc0uNk2zjOj5Dz9MMsoKjGaP p8iS4hiRYuCXETnthnVFihvisp0K9xBQKBTsV7s7ngFT6sVk80JiLfbELIfImSkxWo kjxWJQcrnO0WA== From: Hui Wang To: linux-bluetooth@vger.kernel.org, luiz.dentz@gmail.com, marcel@holtmann.org, johan.hedberg@gmail.com Subject: [PATCH] Bluetooth: hci_event: conn is already encryped before conn establishes Date: Mon, 25 Mar 2024 14:18:41 +0800 Message-Id: <20240325061841.22387-1-hui.wang@canonical.com> X-Mailer: git-send-email 2.34.1 Precedence: bulk X-Mailing-List: linux-bluetooth@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 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. 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 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