Commit 2e58ef3e authored by Johan Hedberg's avatar Johan Hedberg Committed by Gustavo F. Padovan

Bluetooth: Move pending management command list into struct hci_dev

This patch moves the pending management command list (previously global
to mgmt.c) into struct hci_dev. This makes it possible to do proper
locking when accessing it (through the existing hci_dev locks) and
thereby avoid race conditions.
Signed-off-by: default avatarJohan Hedberg <johan.hedberg@intel.com>
Acked-by: default avatarMarcel Holtmann <marcel@holtmann.org>
Signed-off-by: default avatarGustavo F. Padovan <padovan@profusion.mobi>
parent 744cf19e
...@@ -217,6 +217,8 @@ struct hci_dev { ...@@ -217,6 +217,8 @@ struct hci_dev {
__u16 init_last_cmd; __u16 init_last_cmd;
struct list_head mgmt_pending;
struct inquiry_cache inq_cache; struct inquiry_cache inq_cache;
struct hci_conn_hash conn_hash; struct hci_conn_hash conn_hash;
struct list_head blacklist; struct list_head blacklist;
......
...@@ -1481,6 +1481,8 @@ int hci_register_dev(struct hci_dev *hdev) ...@@ -1481,6 +1481,8 @@ int hci_register_dev(struct hci_dev *hdev)
hci_conn_hash_init(hdev); hci_conn_hash_init(hdev);
INIT_LIST_HEAD(&hdev->mgmt_pending);
INIT_LIST_HEAD(&hdev->blacklist); INIT_LIST_HEAD(&hdev->blacklist);
INIT_LIST_HEAD(&hdev->uuids); INIT_LIST_HEAD(&hdev->uuids);
...@@ -1562,6 +1564,10 @@ void hci_unregister_dev(struct hci_dev *hdev) ...@@ -1562,6 +1564,10 @@ void hci_unregister_dev(struct hci_dev *hdev)
!test_bit(HCI_SETUP, &hdev->flags)) !test_bit(HCI_SETUP, &hdev->flags))
mgmt_index_removed(hdev); mgmt_index_removed(hdev);
/* mgmt_index_removed should take care of emptying the
* pending list */
BUG_ON(!list_empty(&hdev->mgmt_pending));
hci_notify(hdev, HCI_DEV_UNREG); hci_notify(hdev, HCI_DEV_UNREG);
if (hdev->rfkill) { if (hdev->rfkill) {
......
...@@ -43,8 +43,6 @@ struct pending_cmd { ...@@ -43,8 +43,6 @@ struct pending_cmd {
void *user_data; void *user_data;
}; };
static LIST_HEAD(cmd_list);
static int cmd_status(struct sock *sk, u16 index, u16 cmd, u8 status) static int cmd_status(struct sock *sk, u16 index, u16 cmd, u8 status)
{ {
struct sk_buff *skb; struct sk_buff *skb;
...@@ -227,7 +225,8 @@ static void mgmt_pending_free(struct pending_cmd *cmd) ...@@ -227,7 +225,8 @@ static void mgmt_pending_free(struct pending_cmd *cmd)
} }
static struct pending_cmd *mgmt_pending_add(struct sock *sk, u16 opcode, static struct pending_cmd *mgmt_pending_add(struct sock *sk, u16 opcode,
u16 index, void *data, u16 len) struct hci_dev *hdev,
void *data, u16 len)
{ {
struct pending_cmd *cmd; struct pending_cmd *cmd;
...@@ -236,7 +235,7 @@ static struct pending_cmd *mgmt_pending_add(struct sock *sk, u16 opcode, ...@@ -236,7 +235,7 @@ static struct pending_cmd *mgmt_pending_add(struct sock *sk, u16 opcode,
return NULL; return NULL;
cmd->opcode = opcode; cmd->opcode = opcode;
cmd->index = index; cmd->index = hdev->id;
cmd->param = kmalloc(len, GFP_ATOMIC); cmd->param = kmalloc(len, GFP_ATOMIC);
if (!cmd->param) { if (!cmd->param) {
...@@ -250,7 +249,7 @@ static struct pending_cmd *mgmt_pending_add(struct sock *sk, u16 opcode, ...@@ -250,7 +249,7 @@ static struct pending_cmd *mgmt_pending_add(struct sock *sk, u16 opcode,
cmd->sk = sk; cmd->sk = sk;
sock_hold(sk); sock_hold(sk);
list_add(&cmd->list, &cmd_list); list_add(&cmd->list, &hdev->mgmt_pending);
return cmd; return cmd;
} }
...@@ -261,7 +260,7 @@ static void mgmt_pending_foreach(u16 opcode, struct hci_dev *hdev, ...@@ -261,7 +260,7 @@ static void mgmt_pending_foreach(u16 opcode, struct hci_dev *hdev,
{ {
struct list_head *p, *n; struct list_head *p, *n;
list_for_each_safe(p, n, &cmd_list) { list_for_each_safe(p, n, &hdev->mgmt_pending) {
struct pending_cmd *cmd; struct pending_cmd *cmd;
cmd = list_entry(p, struct pending_cmd, list); cmd = list_entry(p, struct pending_cmd, list);
...@@ -276,15 +275,15 @@ static void mgmt_pending_foreach(u16 opcode, struct hci_dev *hdev, ...@@ -276,15 +275,15 @@ static void mgmt_pending_foreach(u16 opcode, struct hci_dev *hdev,
} }
} }
static struct pending_cmd *mgmt_pending_find(u16 opcode, int index) static struct pending_cmd *mgmt_pending_find(u16 opcode, struct hci_dev *hdev)
{ {
struct pending_cmd *cmd; struct pending_cmd *cmd;
list_for_each_entry(cmd, &cmd_list, list) { list_for_each_entry(cmd, &hdev->mgmt_pending, list) {
if (cmd->opcode != opcode) if (cmd->opcode != opcode)
continue; continue;
if (index >= 0 && cmd->index != index) if (hdev && cmd->index != hdev->id)
continue; continue;
return cmd; return cmd;
...@@ -325,12 +324,12 @@ static int set_powered(struct sock *sk, u16 index, unsigned char *data, u16 len) ...@@ -325,12 +324,12 @@ static int set_powered(struct sock *sk, u16 index, unsigned char *data, u16 len)
goto failed; goto failed;
} }
if (mgmt_pending_find(MGMT_OP_SET_POWERED, index)) { if (mgmt_pending_find(MGMT_OP_SET_POWERED, hdev)) {
err = cmd_status(sk, index, MGMT_OP_SET_POWERED, EBUSY); err = cmd_status(sk, index, MGMT_OP_SET_POWERED, EBUSY);
goto failed; goto failed;
} }
cmd = mgmt_pending_add(sk, MGMT_OP_SET_POWERED, index, data, len); cmd = mgmt_pending_add(sk, MGMT_OP_SET_POWERED, hdev, data, len);
if (!cmd) { if (!cmd) {
err = -ENOMEM; err = -ENOMEM;
goto failed; goto failed;
...@@ -376,8 +375,8 @@ static int set_discoverable(struct sock *sk, u16 index, unsigned char *data, ...@@ -376,8 +375,8 @@ static int set_discoverable(struct sock *sk, u16 index, unsigned char *data,
goto failed; goto failed;
} }
if (mgmt_pending_find(MGMT_OP_SET_DISCOVERABLE, index) || if (mgmt_pending_find(MGMT_OP_SET_DISCOVERABLE, hdev) ||
mgmt_pending_find(MGMT_OP_SET_CONNECTABLE, index)) { mgmt_pending_find(MGMT_OP_SET_CONNECTABLE, hdev)) {
err = cmd_status(sk, index, MGMT_OP_SET_DISCOVERABLE, EBUSY); err = cmd_status(sk, index, MGMT_OP_SET_DISCOVERABLE, EBUSY);
goto failed; goto failed;
} }
...@@ -388,7 +387,7 @@ static int set_discoverable(struct sock *sk, u16 index, unsigned char *data, ...@@ -388,7 +387,7 @@ static int set_discoverable(struct sock *sk, u16 index, unsigned char *data,
goto failed; goto failed;
} }
cmd = mgmt_pending_add(sk, MGMT_OP_SET_DISCOVERABLE, index, data, len); cmd = mgmt_pending_add(sk, MGMT_OP_SET_DISCOVERABLE, hdev, data, len);
if (!cmd) { if (!cmd) {
err = -ENOMEM; err = -ENOMEM;
goto failed; goto failed;
...@@ -442,8 +441,8 @@ static int set_connectable(struct sock *sk, u16 index, unsigned char *data, ...@@ -442,8 +441,8 @@ static int set_connectable(struct sock *sk, u16 index, unsigned char *data,
goto failed; goto failed;
} }
if (mgmt_pending_find(MGMT_OP_SET_DISCOVERABLE, index) || if (mgmt_pending_find(MGMT_OP_SET_DISCOVERABLE, hdev) ||
mgmt_pending_find(MGMT_OP_SET_CONNECTABLE, index)) { mgmt_pending_find(MGMT_OP_SET_CONNECTABLE, hdev)) {
err = cmd_status(sk, index, MGMT_OP_SET_CONNECTABLE, EBUSY); err = cmd_status(sk, index, MGMT_OP_SET_CONNECTABLE, EBUSY);
goto failed; goto failed;
} }
...@@ -453,7 +452,7 @@ static int set_connectable(struct sock *sk, u16 index, unsigned char *data, ...@@ -453,7 +452,7 @@ static int set_connectable(struct sock *sk, u16 index, unsigned char *data,
goto failed; goto failed;
} }
cmd = mgmt_pending_add(sk, MGMT_OP_SET_CONNECTABLE, index, data, len); cmd = mgmt_pending_add(sk, MGMT_OP_SET_CONNECTABLE, hdev, data, len);
if (!cmd) { if (!cmd) {
err = -ENOMEM; err = -ENOMEM;
goto failed; goto failed;
...@@ -1038,7 +1037,7 @@ static int disconnect(struct sock *sk, u16 index, unsigned char *data, u16 len) ...@@ -1038,7 +1037,7 @@ static int disconnect(struct sock *sk, u16 index, unsigned char *data, u16 len)
goto failed; goto failed;
} }
if (mgmt_pending_find(MGMT_OP_DISCONNECT, index)) { if (mgmt_pending_find(MGMT_OP_DISCONNECT, hdev)) {
err = cmd_status(sk, index, MGMT_OP_DISCONNECT, EBUSY); err = cmd_status(sk, index, MGMT_OP_DISCONNECT, EBUSY);
goto failed; goto failed;
} }
...@@ -1052,7 +1051,7 @@ static int disconnect(struct sock *sk, u16 index, unsigned char *data, u16 len) ...@@ -1052,7 +1051,7 @@ static int disconnect(struct sock *sk, u16 index, unsigned char *data, u16 len)
goto failed; goto failed;
} }
cmd = mgmt_pending_add(sk, MGMT_OP_DISCONNECT, index, data, len); cmd = mgmt_pending_add(sk, MGMT_OP_DISCONNECT, hdev, data, len);
if (!cmd) { if (!cmd) {
err = -ENOMEM; err = -ENOMEM;
goto failed; goto failed;
...@@ -1143,7 +1142,7 @@ static int send_pin_code_neg_reply(struct sock *sk, u16 index, ...@@ -1143,7 +1142,7 @@ static int send_pin_code_neg_reply(struct sock *sk, u16 index,
struct pending_cmd *cmd; struct pending_cmd *cmd;
int err; int err;
cmd = mgmt_pending_add(sk, MGMT_OP_PIN_CODE_NEG_REPLY, index, cp, cmd = mgmt_pending_add(sk, MGMT_OP_PIN_CODE_NEG_REPLY, hdev, cp,
sizeof(*cp)); sizeof(*cp));
if (!cmd) if (!cmd)
return -ENOMEM; return -ENOMEM;
...@@ -1204,7 +1203,7 @@ static int pin_code_reply(struct sock *sk, u16 index, unsigned char *data, ...@@ -1204,7 +1203,7 @@ static int pin_code_reply(struct sock *sk, u16 index, unsigned char *data,
goto failed; goto failed;
} }
cmd = mgmt_pending_add(sk, MGMT_OP_PIN_CODE_REPLY, index, data, len); cmd = mgmt_pending_add(sk, MGMT_OP_PIN_CODE_REPLY, hdev, data, len);
if (!cmd) { if (!cmd) {
err = -ENOMEM; err = -ENOMEM;
goto failed; goto failed;
...@@ -1297,7 +1296,7 @@ static inline struct pending_cmd *find_pairing(struct hci_conn *conn) ...@@ -1297,7 +1296,7 @@ static inline struct pending_cmd *find_pairing(struct hci_conn *conn)
struct hci_dev *hdev = conn->hdev; struct hci_dev *hdev = conn->hdev;
struct pending_cmd *cmd; struct pending_cmd *cmd;
list_for_each_entry(cmd, &cmd_list, list) { list_for_each_entry(cmd, &hdev->mgmt_pending, list) {
if (cmd->opcode != MGMT_OP_PAIR_DEVICE) if (cmd->opcode != MGMT_OP_PAIR_DEVICE)
continue; continue;
...@@ -1396,7 +1395,7 @@ static int pair_device(struct sock *sk, u16 index, unsigned char *data, u16 len) ...@@ -1396,7 +1395,7 @@ static int pair_device(struct sock *sk, u16 index, unsigned char *data, u16 len)
goto unlock; goto unlock;
} }
cmd = mgmt_pending_add(sk, MGMT_OP_PAIR_DEVICE, index, data, len); cmd = mgmt_pending_add(sk, MGMT_OP_PAIR_DEVICE, hdev, data, len);
if (!cmd) { if (!cmd) {
err = -ENOMEM; err = -ENOMEM;
hci_conn_put(conn); hci_conn_put(conn);
...@@ -1458,7 +1457,7 @@ static int user_confirm_reply(struct sock *sk, u16 index, unsigned char *data, ...@@ -1458,7 +1457,7 @@ static int user_confirm_reply(struct sock *sk, u16 index, unsigned char *data,
goto failed; goto failed;
} }
cmd = mgmt_pending_add(sk, mgmt_op, index, data, len); cmd = mgmt_pending_add(sk, mgmt_op, hdev, data, len);
if (!cmd) { if (!cmd) {
err = -ENOMEM; err = -ENOMEM;
goto failed; goto failed;
...@@ -1495,7 +1494,7 @@ static int set_local_name(struct sock *sk, u16 index, unsigned char *data, ...@@ -1495,7 +1494,7 @@ static int set_local_name(struct sock *sk, u16 index, unsigned char *data,
hci_dev_lock_bh(hdev); hci_dev_lock_bh(hdev);
cmd = mgmt_pending_add(sk, MGMT_OP_SET_LOCAL_NAME, index, data, len); cmd = mgmt_pending_add(sk, MGMT_OP_SET_LOCAL_NAME, hdev, data, len);
if (!cmd) { if (!cmd) {
err = -ENOMEM; err = -ENOMEM;
goto failed; goto failed;
...@@ -1541,12 +1540,12 @@ static int read_local_oob_data(struct sock *sk, u16 index) ...@@ -1541,12 +1540,12 @@ static int read_local_oob_data(struct sock *sk, u16 index)
goto unlock; goto unlock;
} }
if (mgmt_pending_find(MGMT_OP_READ_LOCAL_OOB_DATA, index)) { if (mgmt_pending_find(MGMT_OP_READ_LOCAL_OOB_DATA, hdev)) {
err = cmd_status(sk, index, MGMT_OP_READ_LOCAL_OOB_DATA, EBUSY); err = cmd_status(sk, index, MGMT_OP_READ_LOCAL_OOB_DATA, EBUSY);
goto unlock; goto unlock;
} }
cmd = mgmt_pending_add(sk, MGMT_OP_READ_LOCAL_OOB_DATA, index, NULL, 0); cmd = mgmt_pending_add(sk, MGMT_OP_READ_LOCAL_OOB_DATA, hdev, NULL, 0);
if (!cmd) { if (!cmd) {
err = -ENOMEM; err = -ENOMEM;
goto unlock; goto unlock;
...@@ -1650,7 +1649,7 @@ static int start_discovery(struct sock *sk, u16 index) ...@@ -1650,7 +1649,7 @@ static int start_discovery(struct sock *sk, u16 index)
goto failed; goto failed;
} }
cmd = mgmt_pending_add(sk, MGMT_OP_START_DISCOVERY, index, NULL, 0); cmd = mgmt_pending_add(sk, MGMT_OP_START_DISCOVERY, hdev, NULL, 0);
if (!cmd) { if (!cmd) {
err = -ENOMEM; err = -ENOMEM;
goto failed; goto failed;
...@@ -1681,7 +1680,7 @@ static int stop_discovery(struct sock *sk, u16 index) ...@@ -1681,7 +1680,7 @@ static int stop_discovery(struct sock *sk, u16 index)
hci_dev_lock_bh(hdev); hci_dev_lock_bh(hdev);
cmd = mgmt_pending_add(sk, MGMT_OP_STOP_DISCOVERY, index, NULL, 0); cmd = mgmt_pending_add(sk, MGMT_OP_STOP_DISCOVERY, hdev, NULL, 0);
if (!cmd) { if (!cmd) {
err = -ENOMEM; err = -ENOMEM;
goto failed; goto failed;
...@@ -2147,7 +2146,7 @@ int mgmt_disconnect_failed(struct hci_dev *hdev) ...@@ -2147,7 +2146,7 @@ int mgmt_disconnect_failed(struct hci_dev *hdev)
struct pending_cmd *cmd; struct pending_cmd *cmd;
int err; int err;
cmd = mgmt_pending_find(MGMT_OP_DISCONNECT, hdev->id); cmd = mgmt_pending_find(MGMT_OP_DISCONNECT, hdev);
if (!cmd) if (!cmd)
return -ENOENT; return -ENOENT;
...@@ -2188,7 +2187,7 @@ int mgmt_pin_code_reply_complete(struct hci_dev *hdev, bdaddr_t *bdaddr, ...@@ -2188,7 +2187,7 @@ int mgmt_pin_code_reply_complete(struct hci_dev *hdev, bdaddr_t *bdaddr,
struct mgmt_rp_pin_code_reply rp; struct mgmt_rp_pin_code_reply rp;
int err; int err;
cmd = mgmt_pending_find(MGMT_OP_PIN_CODE_REPLY, hdev->id); cmd = mgmt_pending_find(MGMT_OP_PIN_CODE_REPLY, hdev);
if (!cmd) if (!cmd)
return -ENOENT; return -ENOENT;
...@@ -2210,7 +2209,7 @@ int mgmt_pin_code_neg_reply_complete(struct hci_dev *hdev, bdaddr_t *bdaddr, ...@@ -2210,7 +2209,7 @@ int mgmt_pin_code_neg_reply_complete(struct hci_dev *hdev, bdaddr_t *bdaddr,
struct mgmt_rp_pin_code_reply rp; struct mgmt_rp_pin_code_reply rp;
int err; int err;
cmd = mgmt_pending_find(MGMT_OP_PIN_CODE_NEG_REPLY, hdev->id); cmd = mgmt_pending_find(MGMT_OP_PIN_CODE_NEG_REPLY, hdev);
if (!cmd) if (!cmd)
return -ENOENT; return -ENOENT;
...@@ -2247,7 +2246,7 @@ static int confirm_reply_complete(struct hci_dev *hdev, bdaddr_t *bdaddr, ...@@ -2247,7 +2246,7 @@ static int confirm_reply_complete(struct hci_dev *hdev, bdaddr_t *bdaddr,
struct mgmt_rp_user_confirm_reply rp; struct mgmt_rp_user_confirm_reply rp;
int err; int err;
cmd = mgmt_pending_find(opcode, hdev->id); cmd = mgmt_pending_find(opcode, hdev);
if (!cmd) if (!cmd)
return -ENOENT; return -ENOENT;
...@@ -2293,7 +2292,7 @@ int mgmt_set_local_name_complete(struct hci_dev *hdev, u8 *name, u8 status) ...@@ -2293,7 +2292,7 @@ int mgmt_set_local_name_complete(struct hci_dev *hdev, u8 *name, u8 status)
memset(&ev, 0, sizeof(ev)); memset(&ev, 0, sizeof(ev));
memcpy(ev.name, name, HCI_MAX_NAME_LENGTH); memcpy(ev.name, name, HCI_MAX_NAME_LENGTH);
cmd = mgmt_pending_find(MGMT_OP_SET_LOCAL_NAME, hdev->id); cmd = mgmt_pending_find(MGMT_OP_SET_LOCAL_NAME, hdev);
if (!cmd) if (!cmd)
goto send_event; goto send_event;
...@@ -2330,7 +2329,7 @@ int mgmt_read_local_oob_data_reply_complete(struct hci_dev *hdev, u8 *hash, ...@@ -2330,7 +2329,7 @@ int mgmt_read_local_oob_data_reply_complete(struct hci_dev *hdev, u8 *hash,
BT_DBG("%s status %u", hdev->name, status); BT_DBG("%s status %u", hdev->name, status);
cmd = mgmt_pending_find(MGMT_OP_READ_LOCAL_OOB_DATA, hdev->id); cmd = mgmt_pending_find(MGMT_OP_READ_LOCAL_OOB_DATA, hdev);
if (!cmd) if (!cmd)
return -ENOENT; return -ENOENT;
...@@ -2390,7 +2389,7 @@ int mgmt_inquiry_failed(struct hci_dev *hdev, u8 status) ...@@ -2390,7 +2389,7 @@ int mgmt_inquiry_failed(struct hci_dev *hdev, u8 status)
struct pending_cmd *cmd; struct pending_cmd *cmd;
int err; int err;
cmd = mgmt_pending_find(MGMT_OP_START_DISCOVERY, hdev->id); cmd = mgmt_pending_find(MGMT_OP_START_DISCOVERY, hdev);
if (!cmd) if (!cmd)
return -ENOENT; return -ENOENT;
...@@ -2405,9 +2404,9 @@ int mgmt_discovering(struct hci_dev *hdev, u8 discovering) ...@@ -2405,9 +2404,9 @@ int mgmt_discovering(struct hci_dev *hdev, u8 discovering)
struct pending_cmd *cmd; struct pending_cmd *cmd;
if (discovering) if (discovering)
cmd = mgmt_pending_find(MGMT_OP_START_DISCOVERY, hdev->id); cmd = mgmt_pending_find(MGMT_OP_START_DISCOVERY, hdev);
else else
cmd = mgmt_pending_find(MGMT_OP_STOP_DISCOVERY, hdev->id); cmd = mgmt_pending_find(MGMT_OP_STOP_DISCOVERY, hdev);
if (cmd != NULL) { if (cmd != NULL) {
cmd_complete(cmd->sk, hdev->id, cmd->opcode, NULL, 0); cmd_complete(cmd->sk, hdev->id, cmd->opcode, NULL, 0);
...@@ -2423,7 +2422,7 @@ int mgmt_device_blocked(struct hci_dev *hdev, bdaddr_t *bdaddr) ...@@ -2423,7 +2422,7 @@ int mgmt_device_blocked(struct hci_dev *hdev, bdaddr_t *bdaddr)
struct pending_cmd *cmd; struct pending_cmd *cmd;
struct mgmt_ev_device_blocked ev; struct mgmt_ev_device_blocked ev;
cmd = mgmt_pending_find(MGMT_OP_BLOCK_DEVICE, hdev->id); cmd = mgmt_pending_find(MGMT_OP_BLOCK_DEVICE, hdev);
bacpy(&ev.bdaddr, bdaddr); bacpy(&ev.bdaddr, bdaddr);
...@@ -2436,7 +2435,7 @@ int mgmt_device_unblocked(struct hci_dev *hdev, bdaddr_t *bdaddr) ...@@ -2436,7 +2435,7 @@ int mgmt_device_unblocked(struct hci_dev *hdev, bdaddr_t *bdaddr)
struct pending_cmd *cmd; struct pending_cmd *cmd;
struct mgmt_ev_device_unblocked ev; struct mgmt_ev_device_unblocked ev;
cmd = mgmt_pending_find(MGMT_OP_UNBLOCK_DEVICE, hdev->id); cmd = mgmt_pending_find(MGMT_OP_UNBLOCK_DEVICE, hdev);
bacpy(&ev.bdaddr, bdaddr); bacpy(&ev.bdaddr, bdaddr);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment