Commit 0736cfa8 authored by Marcel Holtmann's avatar Marcel Holtmann Committed by Gustavo Padovan

Bluetooth: Introduce user channel flag for HCI devices

This patch introduces a new user channel flag that allows to give full
control of a HCI device to a user application. The kernel will stay away
from the device and does not allow any further modifications of the
device states.

The existing raw flag is not used since it has a bit of unclear meaning
due to its legacy. Using a new flag makes the code clearer.

A device with the user channel flag set can still be enumerate using the
legacy API, but it does not longer enumerate using the new management
interface used by BlueZ 5 and beyond. This is intentional to not confuse
users of modern systems.
Signed-off-by: default avatarMarcel Holtmann <marcel@holtmann.org>
Signed-off-by: default avatarGustavo Padovan <gustavo.padovan@collabora.co.uk>
parent c1c4f956
...@@ -109,6 +109,7 @@ enum { ...@@ -109,6 +109,7 @@ enum {
HCI_SERVICE_CACHE, HCI_SERVICE_CACHE,
HCI_DEBUG_KEYS, HCI_DEBUG_KEYS,
HCI_UNREGISTER, HCI_UNREGISTER,
HCI_USER_CHANNEL,
HCI_LE_SCAN, HCI_LE_SCAN,
HCI_SSP_ENABLED, HCI_SSP_ENABLED,
......
...@@ -984,6 +984,11 @@ int hci_inquiry(void __user *arg) ...@@ -984,6 +984,11 @@ int hci_inquiry(void __user *arg)
if (!hdev) if (!hdev)
return -ENODEV; return -ENODEV;
if (test_bit(HCI_USER_CHANNEL, &hdev->dev_flags)) {
err = -EBUSY;
goto done;
}
hci_dev_lock(hdev); hci_dev_lock(hdev);
if (inquiry_cache_age(hdev) > INQUIRY_CACHE_AGE_MAX || if (inquiry_cache_age(hdev) > INQUIRY_CACHE_AGE_MAX ||
inquiry_cache_empty(hdev) || ir.flags & IREQ_CACHE_FLUSH) { inquiry_cache_empty(hdev) || ir.flags & IREQ_CACHE_FLUSH) {
...@@ -1177,7 +1182,8 @@ int hci_dev_open(__u16 dev) ...@@ -1177,7 +1182,8 @@ int hci_dev_open(__u16 dev)
if (test_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks)) if (test_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks))
set_bit(HCI_RAW, &hdev->flags); set_bit(HCI_RAW, &hdev->flags);
if (!test_bit(HCI_RAW, &hdev->flags)) if (!test_bit(HCI_RAW, &hdev->flags) &&
!test_bit(HCI_USER_CHANNEL, &hdev->dev_flags))
ret = __hci_init(hdev); ret = __hci_init(hdev);
} }
...@@ -1188,6 +1194,7 @@ int hci_dev_open(__u16 dev) ...@@ -1188,6 +1194,7 @@ int hci_dev_open(__u16 dev)
set_bit(HCI_UP, &hdev->flags); set_bit(HCI_UP, &hdev->flags);
hci_notify(hdev, HCI_DEV_UP); hci_notify(hdev, HCI_DEV_UP);
if (!test_bit(HCI_SETUP, &hdev->dev_flags) && if (!test_bit(HCI_SETUP, &hdev->dev_flags) &&
!test_bit(HCI_USER_CHANNEL, &hdev->dev_flags) &&
mgmt_valid_hdev(hdev)) { mgmt_valid_hdev(hdev)) {
hci_dev_lock(hdev); hci_dev_lock(hdev);
mgmt_powered(hdev, 1); mgmt_powered(hdev, 1);
...@@ -1324,11 +1331,17 @@ int hci_dev_close(__u16 dev) ...@@ -1324,11 +1331,17 @@ int hci_dev_close(__u16 dev)
if (!hdev) if (!hdev)
return -ENODEV; return -ENODEV;
if (test_bit(HCI_USER_CHANNEL, &hdev->dev_flags)) {
err = -EBUSY;
goto done;
}
if (test_and_clear_bit(HCI_AUTO_OFF, &hdev->dev_flags)) if (test_and_clear_bit(HCI_AUTO_OFF, &hdev->dev_flags))
cancel_delayed_work(&hdev->power_off); cancel_delayed_work(&hdev->power_off);
err = hci_dev_do_close(hdev); err = hci_dev_do_close(hdev);
done:
hci_dev_put(hdev); hci_dev_put(hdev);
return err; return err;
} }
...@@ -1349,6 +1362,11 @@ int hci_dev_reset(__u16 dev) ...@@ -1349,6 +1362,11 @@ int hci_dev_reset(__u16 dev)
goto done; goto done;
} }
if (test_bit(HCI_USER_CHANNEL, &hdev->dev_flags)) {
ret = -EBUSY;
goto done;
}
/* Drop queues */ /* Drop queues */
skb_queue_purge(&hdev->rx_q); skb_queue_purge(&hdev->rx_q);
skb_queue_purge(&hdev->cmd_q); skb_queue_purge(&hdev->cmd_q);
...@@ -1382,10 +1400,15 @@ int hci_dev_reset_stat(__u16 dev) ...@@ -1382,10 +1400,15 @@ int hci_dev_reset_stat(__u16 dev)
if (!hdev) if (!hdev)
return -ENODEV; return -ENODEV;
if (test_bit(HCI_USER_CHANNEL, &hdev->dev_flags)) {
ret = -EBUSY;
goto done;
}
memset(&hdev->stat, 0, sizeof(struct hci_dev_stats)); memset(&hdev->stat, 0, sizeof(struct hci_dev_stats));
done:
hci_dev_put(hdev); hci_dev_put(hdev);
return ret; return ret;
} }
...@@ -1402,6 +1425,11 @@ int hci_dev_cmd(unsigned int cmd, void __user *arg) ...@@ -1402,6 +1425,11 @@ int hci_dev_cmd(unsigned int cmd, void __user *arg)
if (!hdev) if (!hdev)
return -ENODEV; return -ENODEV;
if (test_bit(HCI_USER_CHANNEL, &hdev->dev_flags)) {
err = -EBUSY;
goto done;
}
switch (cmd) { switch (cmd) {
case HCISETAUTH: case HCISETAUTH:
err = hci_req_sync(hdev, hci_auth_req, dr.dev_opt, err = hci_req_sync(hdev, hci_auth_req, dr.dev_opt,
...@@ -1460,6 +1488,7 @@ int hci_dev_cmd(unsigned int cmd, void __user *arg) ...@@ -1460,6 +1488,7 @@ int hci_dev_cmd(unsigned int cmd, void __user *arg)
break; break;
} }
done:
hci_dev_put(hdev); hci_dev_put(hdev);
return err; return err;
} }
...@@ -1568,6 +1597,9 @@ static int hci_rfkill_set_block(void *data, bool blocked) ...@@ -1568,6 +1597,9 @@ static int hci_rfkill_set_block(void *data, bool blocked)
BT_DBG("%p name %s blocked %d", hdev, hdev->name, blocked); BT_DBG("%p name %s blocked %d", hdev, hdev->name, blocked);
if (test_bit(HCI_USER_CHANNEL, &hdev->dev_flags))
return -EBUSY;
if (!blocked) if (!blocked)
return 0; return 0;
...@@ -3459,7 +3491,8 @@ static void hci_rx_work(struct work_struct *work) ...@@ -3459,7 +3491,8 @@ static void hci_rx_work(struct work_struct *work)
hci_send_to_sock(hdev, skb); hci_send_to_sock(hdev, skb);
} }
if (test_bit(HCI_RAW, &hdev->flags)) { if (test_bit(HCI_RAW, &hdev->flags) ||
test_bit(HCI_USER_CHANNEL, &hdev->dev_flags)) {
kfree_skb(skb); kfree_skb(skb);
continue; continue;
} }
......
...@@ -500,6 +500,9 @@ static int hci_sock_bound_ioctl(struct sock *sk, unsigned int cmd, ...@@ -500,6 +500,9 @@ static int hci_sock_bound_ioctl(struct sock *sk, unsigned int cmd,
if (!hdev) if (!hdev)
return -EBADFD; return -EBADFD;
if (test_bit(HCI_USER_CHANNEL, &hdev->dev_flags))
return -EBUSY;
switch (cmd) { switch (cmd) {
case HCISETRAW: case HCISETRAW:
if (!capable(CAP_NET_ADMIN)) if (!capable(CAP_NET_ADMIN))
...@@ -530,19 +533,19 @@ static int hci_sock_bound_ioctl(struct sock *sk, unsigned int cmd, ...@@ -530,19 +533,19 @@ static int hci_sock_bound_ioctl(struct sock *sk, unsigned int cmd,
if (!capable(CAP_NET_ADMIN)) if (!capable(CAP_NET_ADMIN))
return -EPERM; return -EPERM;
return hci_sock_blacklist_del(hdev, (void __user *) arg); return hci_sock_blacklist_del(hdev, (void __user *) arg);
default:
if (hdev->ioctl)
return hdev->ioctl(hdev, cmd, arg);
return -EINVAL;
} }
if (hdev->ioctl)
return hdev->ioctl(hdev, cmd, arg);
return -EINVAL;
} }
static int hci_sock_ioctl(struct socket *sock, unsigned int cmd, static int hci_sock_ioctl(struct socket *sock, unsigned int cmd,
unsigned long arg) unsigned long arg)
{ {
struct sock *sk = sock->sk;
void __user *argp = (void __user *) arg; void __user *argp = (void __user *) arg;
struct sock *sk = sock->sk;
int err; int err;
BT_DBG("cmd %x arg %lx", cmd, arg); BT_DBG("cmd %x arg %lx", cmd, arg);
......
...@@ -339,6 +339,9 @@ static int read_index_list(struct sock *sk, struct hci_dev *hdev, void *data, ...@@ -339,6 +339,9 @@ static int read_index_list(struct sock *sk, struct hci_dev *hdev, void *data,
if (test_bit(HCI_SETUP, &d->dev_flags)) if (test_bit(HCI_SETUP, &d->dev_flags))
continue; continue;
if (test_bit(HCI_USER_CHANNEL, &d->dev_flags))
continue;
if (!mgmt_valid_hdev(d)) if (!mgmt_valid_hdev(d))
continue; continue;
...@@ -3320,6 +3323,12 @@ int mgmt_control(struct sock *sk, struct msghdr *msg, size_t msglen) ...@@ -3320,6 +3323,12 @@ int mgmt_control(struct sock *sk, struct msghdr *msg, size_t msglen)
MGMT_STATUS_INVALID_INDEX); MGMT_STATUS_INVALID_INDEX);
goto done; goto done;
} }
if (test_bit(HCI_USER_CHANNEL, &hdev->dev_flags)) {
err = cmd_status(sk, index, opcode,
MGMT_STATUS_INVALID_INDEX);
goto done;
}
} }
if (opcode >= ARRAY_SIZE(mgmt_handlers) || if (opcode >= ARRAY_SIZE(mgmt_handlers) ||
......
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