Skip to content

Commit 76b1399

Browse files
liveusrjhedberg
authored andcommitted
Bluetooth: Terminate the link if pairing is cancelled
If user decides to cancel the ongoing pairing process (e.g. by clicking the cancel button on pairing/passkey window), abort any ongoing pairing and then terminate the link if it was created because of the pair device action. Signed-off-by: Manish Mandlik <mmandlik@google.com> Signed-off-by: Marcel Holtmann <marcel@holtmann.org> Signed-off-by: Johan Hedberg <johan.hedberg@intel.com>
1 parent 8208f5a commit 76b1399

4 files changed

Lines changed: 42 additions & 11 deletions

File tree

‎include/net/bluetooth/hci_core.h‎

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -564,6 +564,12 @@ struct hci_dev {
564564

565565
#define HCI_PHY_HANDLE(handle) (handle & 0xff)
566566

567+
enum conn_reasons {
568+
CONN_REASON_PAIR_DEVICE,
569+
CONN_REASON_L2CAP_CHAN,
570+
CONN_REASON_SCO_CONNECT,
571+
};
572+
567573
struct hci_conn {
568574
struct list_head list;
569575

@@ -615,6 +621,8 @@ struct hci_conn {
615621
__s8 max_tx_power;
616622
unsigned long flags;
617623

624+
enum conn_reasons conn_reason;
625+
618626
__u32 clock;
619627
__u16 clock_accuracy;
620628

@@ -1040,12 +1048,14 @@ struct hci_chan *hci_chan_lookup_handle(struct hci_dev *hdev, __u16 handle);
10401048

10411049
struct hci_conn *hci_connect_le_scan(struct hci_dev *hdev, bdaddr_t *dst,
10421050
u8 dst_type, u8 sec_level,
1043-
u16 conn_timeout);
1051+
u16 conn_timeout,
1052+
enum conn_reasons conn_reason);
10441053
struct hci_conn *hci_connect_le(struct hci_dev *hdev, bdaddr_t *dst,
10451054
u8 dst_type, u8 sec_level, u16 conn_timeout,
10461055
u8 role, bdaddr_t *direct_rpa);
10471056
struct hci_conn *hci_connect_acl(struct hci_dev *hdev, bdaddr_t *dst,
1048-
u8 sec_level, u8 auth_type);
1057+
u8 sec_level, u8 auth_type,
1058+
enum conn_reasons conn_reason);
10491059
struct hci_conn *hci_connect_sco(struct hci_dev *hdev, int type, bdaddr_t *dst,
10501060
__u16 setting);
10511061
int hci_conn_check_link_mode(struct hci_conn *conn);

‎net/bluetooth/hci_conn.c‎

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1174,7 +1174,8 @@ static int hci_explicit_conn_params_set(struct hci_dev *hdev,
11741174
/* This function requires the caller holds hdev->lock */
11751175
struct hci_conn *hci_connect_le_scan(struct hci_dev *hdev, bdaddr_t *dst,
11761176
u8 dst_type, u8 sec_level,
1177-
u16 conn_timeout)
1177+
u16 conn_timeout,
1178+
enum conn_reasons conn_reason)
11781179
{
11791180
struct hci_conn *conn;
11801181

@@ -1219,6 +1220,7 @@ struct hci_conn *hci_connect_le_scan(struct hci_dev *hdev, bdaddr_t *dst,
12191220
conn->sec_level = BT_SECURITY_LOW;
12201221
conn->pending_sec_level = sec_level;
12211222
conn->conn_timeout = conn_timeout;
1223+
conn->conn_reason = conn_reason;
12221224

12231225
hci_update_background_scan(hdev);
12241226

@@ -1228,7 +1230,8 @@ struct hci_conn *hci_connect_le_scan(struct hci_dev *hdev, bdaddr_t *dst,
12281230
}
12291231

12301232
struct hci_conn *hci_connect_acl(struct hci_dev *hdev, bdaddr_t *dst,
1231-
u8 sec_level, u8 auth_type)
1233+
u8 sec_level, u8 auth_type,
1234+
enum conn_reasons conn_reason)
12321235
{
12331236
struct hci_conn *acl;
12341237

@@ -1248,6 +1251,7 @@ struct hci_conn *hci_connect_acl(struct hci_dev *hdev, bdaddr_t *dst,
12481251

12491252
hci_conn_hold(acl);
12501253

1254+
acl->conn_reason = conn_reason;
12511255
if (acl->state == BT_OPEN || acl->state == BT_CLOSED) {
12521256
acl->sec_level = BT_SECURITY_LOW;
12531257
acl->pending_sec_level = sec_level;
@@ -1264,7 +1268,8 @@ struct hci_conn *hci_connect_sco(struct hci_dev *hdev, int type, bdaddr_t *dst,
12641268
struct hci_conn *acl;
12651269
struct hci_conn *sco;
12661270

1267-
acl = hci_connect_acl(hdev, dst, BT_SECURITY_LOW, HCI_AT_NO_BONDING);
1271+
acl = hci_connect_acl(hdev, dst, BT_SECURITY_LOW, HCI_AT_NO_BONDING,
1272+
CONN_REASON_SCO_CONNECT);
12681273
if (IS_ERR(acl))
12691274
return acl;
12701275

‎net/bluetooth/l2cap_core.c‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7893,11 +7893,13 @@ int l2cap_chan_connect(struct l2cap_chan *chan, __le16 psm, u16 cid,
78937893
else
78947894
hcon = hci_connect_le_scan(hdev, dst, dst_type,
78957895
chan->sec_level,
7896-
HCI_LE_CONN_TIMEOUT);
7896+
HCI_LE_CONN_TIMEOUT,
7897+
CONN_REASON_L2CAP_CHAN);
78977898

78987899
} else {
78997900
u8 auth_type = l2cap_get_auth_type(chan);
7900-
hcon = hci_connect_acl(hdev, dst, chan->sec_level, auth_type);
7901+
hcon = hci_connect_acl(hdev, dst, chan->sec_level, auth_type,
7902+
CONN_REASON_L2CAP_CHAN);
79017903
}
79027904

79037905
if (IS_ERR(hcon)) {

‎net/bluetooth/mgmt.c‎

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2931,7 +2931,7 @@ static int pair_device(struct sock *sk, struct hci_dev *hdev, void *data,
29312931

29322932
if (cp->addr.type == BDADDR_BREDR) {
29332933
conn = hci_connect_acl(hdev, &cp->addr.bdaddr, sec_level,
2934-
auth_type);
2934+
auth_type, CONN_REASON_PAIR_DEVICE);
29352935
} else {
29362936
u8 addr_type = le_addr_type(cp->addr.type);
29372937
struct hci_conn_params *p;
@@ -2950,9 +2950,9 @@ static int pair_device(struct sock *sk, struct hci_dev *hdev, void *data,
29502950
if (p->auto_connect == HCI_AUTO_CONN_EXPLICIT)
29512951
p->auto_connect = HCI_AUTO_CONN_DISABLED;
29522952

2953-
conn = hci_connect_le_scan(hdev, &cp->addr.bdaddr,
2954-
addr_type, sec_level,
2955-
HCI_LE_CONN_TIMEOUT);
2953+
conn = hci_connect_le_scan(hdev, &cp->addr.bdaddr, addr_type,
2954+
sec_level, HCI_LE_CONN_TIMEOUT,
2955+
CONN_REASON_PAIR_DEVICE);
29562956
}
29572957

29582958
if (IS_ERR(conn)) {
@@ -3053,6 +3053,20 @@ static int cancel_pair_device(struct sock *sk, struct hci_dev *hdev, void *data,
30533053

30543054
err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_CANCEL_PAIR_DEVICE, 0,
30553055
addr, sizeof(*addr));
3056+
3057+
/* Since user doesn't want to proceed with the connection, abort any
3058+
* ongoing pairing and then terminate the link if it was created
3059+
* because of the pair device action.
3060+
*/
3061+
if (addr->type == BDADDR_BREDR)
3062+
hci_remove_link_key(hdev, &addr->bdaddr);
3063+
else
3064+
smp_cancel_and_remove_pairing(hdev, &addr->bdaddr,
3065+
le_addr_type(addr->type));
3066+
3067+
if (conn->conn_reason == CONN_REASON_PAIR_DEVICE)
3068+
hci_abort_conn(conn, HCI_ERROR_REMOTE_USER_TERM);
3069+
30563070
unlock:
30573071
hci_dev_unlock(hdev);
30583072
return err;

0 commit comments

Comments
 (0)