Skip to content

Commit c569242

Browse files
jason77-wangVudentz
authored andcommitted
Bluetooth: hci_event: set the conn encrypted before conn establishes
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 powered 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. This is the btmon log after applying the patch, after receiving the "Connect Complete" with "Encryption: Enabled", will send the command to read encryption key size: > HCI Event: Connect Request (0x04) plen 10 Address: 8C:3C:AA:D8:11:67 (OUI 8C-3C-AA) Class: 0x240404 Major class: Audio/Video (headset, speaker, stereo, video, vcr) Minor class: Wearable Headset Device Rendering (Printing, Speaker) Audio (Speaker, Microphone, Headset) Link type: ACL (0x01) ... > HCI Event: Link Key Request (0x17) plen 6 Address: 8C:3C:AA:D8:11:67 (OUI 8C-3C-AA) < HCI Command: Link Key Request Reply (0x01|0x000b) plen 22 Address: 8C:3C:AA:D8:11:67 (OUI 8C-3C-AA) Link key: ${32-hex-digits-key} ... > HCI Event: Connect Complete (0x03) plen 11 Status: Success (0x00) Handle: 256 Address: 8C:3C:AA:D8:11:67 (OUI 8C-3C-AA) Link type: ACL (0x01) Encryption: Enabled (0x01) < HCI Command: Read Encryption Key... (0x05|0x0008) plen 2 Handle: 256 < ACL Data TX: Handle 256 flags 0x00 dlen 10 L2CAP: Information Request (0x0a) ident 1 len 2 Type: Extended features supported (0x0002) > HCI Event: Command Complete (0x0e) plen 7 Read Encryption Key Size (0x05|0x0008) ncmd 1 Status: Success (0x00) Handle: 256 Key size: 16 Cc: [email protected] Link: bluez/bluez#704 Reviewed-by: Paul Menzel <[email protected]> Reviewed-by: Luiz Augusto von Dentz <[email protected]> Signed-off-by: Hui Wang <[email protected]> Signed-off-by: Luiz Augusto von Dentz <[email protected]>
1 parent 6946b9c commit c569242

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

net/bluetooth/hci_event.c

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3208,6 +3208,31 @@ static void hci_conn_complete_evt(struct hci_dev *hdev, void *data,
32083208
if (test_bit(HCI_ENCRYPT, &hdev->flags))
32093209
set_bit(HCI_CONN_ENCRYPT, &conn->flags);
32103210

3211+
/* "Link key request" completed ahead of "connect request" completes */
3212+
if (ev->encr_mode == 1 && !test_bit(HCI_CONN_ENCRYPT, &conn->flags) &&
3213+
ev->link_type == ACL_LINK) {
3214+
struct link_key *key;
3215+
struct hci_cp_read_enc_key_size cp;
3216+
3217+
key = hci_find_link_key(hdev, &ev->bdaddr);
3218+
if (key) {
3219+
set_bit(HCI_CONN_ENCRYPT, &conn->flags);
3220+
3221+
if (!(hdev->commands[20] & 0x10)) {
3222+
conn->enc_key_size = HCI_LINK_KEY_SIZE;
3223+
} else {
3224+
cp.handle = cpu_to_le16(conn->handle);
3225+
if (hci_send_cmd(hdev, HCI_OP_READ_ENC_KEY_SIZE,
3226+
sizeof(cp), &cp)) {
3227+
bt_dev_err(hdev, "sending read key size failed");
3228+
conn->enc_key_size = HCI_LINK_KEY_SIZE;
3229+
}
3230+
}
3231+
3232+
hci_encrypt_cfm(conn, ev->status);
3233+
}
3234+
}
3235+
32113236
/* Get remote features */
32123237
if (conn->type == ACL_LINK) {
32133238
struct hci_cp_read_remote_features cp;

0 commit comments

Comments
 (0)