Skip to content

Commit 227a0cd

Browse files
committed
Bluetooth: MGMT: Fix not generating command complete for MGMT_OP_DISCONNECT
MGMT_OP_DISCONNECT can be called while mgmt_device_connected has not been called yet, which will cause the connection procedure to be aborted, so mgmt_device_disconnected shall still respond with command complete to MGMT_OP_DISCONNECT and just not emit MGMT_EV_DEVICE_DISCONNECTED since MGMT_EV_DEVICE_CONNECTED was never sent. To fix this MGMT_OP_DISCONNECT is changed to work similarly to other command which do use hci_cmd_sync_queue and then use hci_conn_abort to disconnect and returns the result, in order for hci_conn_abort to be used from hci_cmd_sync context it now uses hci_cmd_sync_run_once. Link: bluez/bluez#932 Fixes: 12d4a3b ("Bluetooth: Move check for MGMT_CONNECTED flag into mgmt.c") Signed-off-by: Luiz Augusto von Dentz <luiz.von.dentz@intel.com>
1 parent c898f6d commit 227a0cd

2 files changed

Lines changed: 47 additions & 43 deletions

File tree

net/bluetooth/hci_conn.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2952,5 +2952,9 @@ int hci_abort_conn(struct hci_conn *conn, u8 reason)
29522952
return 0;
29532953
}
29542954

2955-
return hci_cmd_sync_queue_once(hdev, abort_conn_sync, conn, NULL);
2955+
/* Run immediately if on cmd_sync_work since this may be called
2956+
* as a result to MGMT_OP_DISCONNECT/MGMT_OP_UNPAIR which does
2957+
* already queue its callback on cmd_sync_work.
2958+
*/
2959+
return hci_cmd_sync_run_once(hdev, abort_conn_sync, conn, NULL);
29562960
}

net/bluetooth/mgmt.c

Lines changed: 42 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -2921,7 +2921,12 @@ static int unpair_device_sync(struct hci_dev *hdev, void *data)
29212921
if (!conn)
29222922
return 0;
29232923

2924-
return hci_abort_conn_sync(hdev, conn, HCI_ERROR_REMOTE_USER_TERM);
2924+
/* Disregard any possible error since the likes of hci_abort_conn_sync
2925+
* will clean up the connection no matter the error.
2926+
*/
2927+
hci_abort_conn(conn, HCI_ERROR_REMOTE_USER_TERM);
2928+
2929+
return 0;
29252930
}
29262931

29272932
static int unpair_device(struct sock *sk, struct hci_dev *hdev, void *data,
@@ -3053,13 +3058,44 @@ static int unpair_device(struct sock *sk, struct hci_dev *hdev, void *data,
30533058
return err;
30543059
}
30553060

3061+
static void disconnect_complete(struct hci_dev *hdev, void *data, int err)
3062+
{
3063+
struct mgmt_pending_cmd *cmd = data;
3064+
3065+
cmd->cmd_complete(cmd, mgmt_status(err));
3066+
mgmt_pending_free(cmd);
3067+
}
3068+
3069+
static int disconnect_sync(struct hci_dev *hdev, void *data)
3070+
{
3071+
struct mgmt_pending_cmd *cmd = data;
3072+
struct mgmt_cp_disconnect *cp = cmd->param;
3073+
struct hci_conn *conn;
3074+
3075+
if (cp->addr.type == BDADDR_BREDR)
3076+
conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK,
3077+
&cp->addr.bdaddr);
3078+
else
3079+
conn = hci_conn_hash_lookup_le(hdev, &cp->addr.bdaddr,
3080+
le_addr_type(cp->addr.type));
3081+
3082+
if (!conn)
3083+
return -ENOTCONN;
3084+
3085+
/* Disregard any possible error since the likes of hci_abort_conn_sync
3086+
* will clean up the connection no matter the error.
3087+
*/
3088+
hci_abort_conn(conn, HCI_ERROR_REMOTE_USER_TERM);
3089+
3090+
return 0;
3091+
}
3092+
30563093
static int disconnect(struct sock *sk, struct hci_dev *hdev, void *data,
30573094
u16 len)
30583095
{
30593096
struct mgmt_cp_disconnect *cp = data;
30603097
struct mgmt_rp_disconnect rp;
30613098
struct mgmt_pending_cmd *cmd;
3062-
struct hci_conn *conn;
30633099
int err;
30643100

30653101
bt_dev_dbg(hdev, "sock %p", sk);
@@ -3082,37 +3118,18 @@ static int disconnect(struct sock *sk, struct hci_dev *hdev, void *data,
30823118
goto failed;
30833119
}
30843120

3085-
if (pending_find(MGMT_OP_DISCONNECT, hdev)) {
3086-
err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_DISCONNECT,
3087-
MGMT_STATUS_BUSY, &rp, sizeof(rp));
3088-
goto failed;
3089-
}
3090-
3091-
if (cp->addr.type == BDADDR_BREDR)
3092-
conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK,
3093-
&cp->addr.bdaddr);
3094-
else
3095-
conn = hci_conn_hash_lookup_le(hdev, &cp->addr.bdaddr,
3096-
le_addr_type(cp->addr.type));
3097-
3098-
if (!conn || conn->state == BT_OPEN || conn->state == BT_CLOSED) {
3099-
err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_DISCONNECT,
3100-
MGMT_STATUS_NOT_CONNECTED, &rp,
3101-
sizeof(rp));
3102-
goto failed;
3103-
}
3104-
3105-
cmd = mgmt_pending_add(sk, MGMT_OP_DISCONNECT, hdev, data, len);
3121+
cmd = mgmt_pending_new(sk, MGMT_OP_DISCONNECT, hdev, data, len);
31063122
if (!cmd) {
31073123
err = -ENOMEM;
31083124
goto failed;
31093125
}
31103126

31113127
cmd->cmd_complete = generic_cmd_complete;
31123128

3113-
err = hci_disconnect(conn, HCI_ERROR_REMOTE_USER_TERM);
3129+
err = hci_cmd_sync_queue(hdev, disconnect_sync, cmd,
3130+
disconnect_complete);
31143131
if (err < 0)
3115-
mgmt_pending_remove(cmd);
3132+
mgmt_pending_free(cmd);
31163133

31173134
failed:
31183135
hci_dev_unlock(hdev);
@@ -9689,18 +9706,6 @@ void mgmt_device_connected(struct hci_dev *hdev, struct hci_conn *conn,
96899706
mgmt_event_skb(skb, NULL);
96909707
}
96919708

9692-
static void disconnect_rsp(struct mgmt_pending_cmd *cmd, void *data)
9693-
{
9694-
struct sock **sk = data;
9695-
9696-
cmd->cmd_complete(cmd, 0);
9697-
9698-
*sk = cmd->sk;
9699-
sock_hold(*sk);
9700-
9701-
mgmt_pending_remove(cmd);
9702-
}
9703-
97049709
static void unpair_device_rsp(struct mgmt_pending_cmd *cmd, void *data)
97059710
{
97069711
struct hci_dev *hdev = data;
@@ -9744,8 +9749,6 @@ void mgmt_device_disconnected(struct hci_dev *hdev, bdaddr_t *bdaddr,
97449749
if (link_type != ACL_LINK && link_type != LE_LINK)
97459750
return;
97469751

9747-
mgmt_pending_foreach(MGMT_OP_DISCONNECT, hdev, disconnect_rsp, &sk);
9748-
97499752
bacpy(&ev.addr.bdaddr, bdaddr);
97509753
ev.addr.type = link_to_bdaddr(link_type, addr_type);
97519754
ev.reason = reason;
@@ -9758,9 +9761,6 @@ void mgmt_device_disconnected(struct hci_dev *hdev, bdaddr_t *bdaddr,
97589761

97599762
if (sk)
97609763
sock_put(sk);
9761-
9762-
mgmt_pending_foreach(MGMT_OP_UNPAIR_DEVICE, hdev, unpair_device_rsp,
9763-
hdev);
97649764
}
97659765

97669766
void mgmt_disconnect_failed(struct hci_dev *hdev, bdaddr_t *bdaddr,

0 commit comments

Comments
 (0)