Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1 | /* |
| 2 | BlueZ - Bluetooth protocol stack for Linux |
| 3 | Copyright (C) 2010 Nokia Corporation |
| 4 | |
| 5 | This program is free software; you can redistribute it and/or modify |
| 6 | it under the terms of the GNU General Public License version 2 as |
| 7 | published by the Free Software Foundation; |
| 8 | |
| 9 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS |
| 10 | OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 11 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS. |
| 12 | IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) AND AUTHOR(S) BE LIABLE FOR ANY |
| 13 | CLAIM, OR ANY SPECIAL INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES |
| 14 | WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
| 15 | ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF |
| 16 | OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| 17 | |
| 18 | ALL LIABILITY, INCLUDING LIABILITY FOR INFRINGEMENT OF ANY PATENTS, |
| 19 | COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS, RELATING TO USE OF THIS |
| 20 | SOFTWARE IS DISCLAIMED. |
| 21 | */ |
| 22 | |
| 23 | /* Bluetooth HCI Management interface */ |
| 24 | |
Szymon Janc | 7235975 | 2011-02-17 14:16:32 +0100 | [diff] [blame] | 25 | #include <linux/uaccess.h> |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 26 | #include <asm/unaligned.h> |
| 27 | |
| 28 | #include <net/bluetooth/bluetooth.h> |
| 29 | #include <net/bluetooth/hci_core.h> |
| 30 | #include <net/bluetooth/mgmt.h> |
| 31 | |
Johan Hedberg | 02d9812 | 2010-12-13 21:07:04 +0200 | [diff] [blame] | 32 | #define MGMT_VERSION 0 |
| 33 | #define MGMT_REVISION 1 |
| 34 | |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 35 | struct pending_cmd { |
| 36 | struct list_head list; |
| 37 | __u16 opcode; |
| 38 | int index; |
Szymon Janc | c68fb7f | 2011-03-22 13:12:19 +0100 | [diff] [blame^] | 39 | void *param; |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 40 | struct sock *sk; |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 41 | void *user_data; |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 42 | }; |
| 43 | |
| 44 | LIST_HEAD(cmd_list); |
| 45 | |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 46 | static int cmd_status(struct sock *sk, u16 index, u16 cmd, u8 status) |
Johan Hedberg | f7b64e6 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 47 | { |
| 48 | struct sk_buff *skb; |
| 49 | struct mgmt_hdr *hdr; |
| 50 | struct mgmt_ev_cmd_status *ev; |
| 51 | |
Szymon Janc | 34eb525 | 2011-02-28 14:10:08 +0100 | [diff] [blame] | 52 | BT_DBG("sock %p, index %u, cmd %u, status %u", sk, index, cmd, status); |
Johan Hedberg | f7b64e6 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 53 | |
| 54 | skb = alloc_skb(sizeof(*hdr) + sizeof(*ev), GFP_ATOMIC); |
| 55 | if (!skb) |
| 56 | return -ENOMEM; |
| 57 | |
| 58 | hdr = (void *) skb_put(skb, sizeof(*hdr)); |
| 59 | |
| 60 | hdr->opcode = cpu_to_le16(MGMT_EV_CMD_STATUS); |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 61 | hdr->index = cpu_to_le16(index); |
Johan Hedberg | f7b64e6 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 62 | hdr->len = cpu_to_le16(sizeof(*ev)); |
| 63 | |
| 64 | ev = (void *) skb_put(skb, sizeof(*ev)); |
| 65 | ev->status = status; |
| 66 | put_unaligned_le16(cmd, &ev->opcode); |
| 67 | |
| 68 | if (sock_queue_rcv_skb(sk, skb) < 0) |
| 69 | kfree_skb(skb); |
| 70 | |
| 71 | return 0; |
| 72 | } |
| 73 | |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 74 | static int cmd_complete(struct sock *sk, u16 index, u16 cmd, void *rp, |
| 75 | size_t rp_len) |
Johan Hedberg | 02d9812 | 2010-12-13 21:07:04 +0200 | [diff] [blame] | 76 | { |
| 77 | struct sk_buff *skb; |
| 78 | struct mgmt_hdr *hdr; |
| 79 | struct mgmt_ev_cmd_complete *ev; |
Johan Hedberg | 02d9812 | 2010-12-13 21:07:04 +0200 | [diff] [blame] | 80 | |
| 81 | BT_DBG("sock %p", sk); |
| 82 | |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 83 | skb = alloc_skb(sizeof(*hdr) + sizeof(*ev) + rp_len, GFP_ATOMIC); |
Johan Hedberg | 02d9812 | 2010-12-13 21:07:04 +0200 | [diff] [blame] | 84 | if (!skb) |
| 85 | return -ENOMEM; |
| 86 | |
| 87 | hdr = (void *) skb_put(skb, sizeof(*hdr)); |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 88 | |
Johan Hedberg | 02d9812 | 2010-12-13 21:07:04 +0200 | [diff] [blame] | 89 | hdr->opcode = cpu_to_le16(MGMT_EV_CMD_COMPLETE); |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 90 | hdr->index = cpu_to_le16(index); |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 91 | hdr->len = cpu_to_le16(sizeof(*ev) + rp_len); |
Johan Hedberg | 02d9812 | 2010-12-13 21:07:04 +0200 | [diff] [blame] | 92 | |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 93 | ev = (void *) skb_put(skb, sizeof(*ev) + rp_len); |
| 94 | put_unaligned_le16(cmd, &ev->opcode); |
Szymon Janc | 8020c16 | 2011-02-28 14:09:50 +0100 | [diff] [blame] | 95 | |
| 96 | if (rp) |
| 97 | memcpy(ev->data, rp, rp_len); |
Johan Hedberg | 02d9812 | 2010-12-13 21:07:04 +0200 | [diff] [blame] | 98 | |
| 99 | if (sock_queue_rcv_skb(sk, skb) < 0) |
| 100 | kfree_skb(skb); |
| 101 | |
| 102 | return 0; |
| 103 | } |
| 104 | |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 105 | static int read_version(struct sock *sk) |
| 106 | { |
| 107 | struct mgmt_rp_read_version rp; |
| 108 | |
| 109 | BT_DBG("sock %p", sk); |
| 110 | |
| 111 | rp.version = MGMT_VERSION; |
| 112 | put_unaligned_le16(MGMT_REVISION, &rp.revision); |
| 113 | |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 114 | return cmd_complete(sk, MGMT_INDEX_NONE, MGMT_OP_READ_VERSION, &rp, |
| 115 | sizeof(rp)); |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 116 | } |
| 117 | |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 118 | static int read_index_list(struct sock *sk) |
| 119 | { |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 120 | struct mgmt_rp_read_index_list *rp; |
| 121 | struct list_head *p; |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 122 | size_t rp_len; |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 123 | u16 count; |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 124 | int i, err; |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 125 | |
| 126 | BT_DBG("sock %p", sk); |
| 127 | |
| 128 | read_lock(&hci_dev_list_lock); |
| 129 | |
| 130 | count = 0; |
| 131 | list_for_each(p, &hci_dev_list) { |
| 132 | count++; |
| 133 | } |
| 134 | |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 135 | rp_len = sizeof(*rp) + (2 * count); |
| 136 | rp = kmalloc(rp_len, GFP_ATOMIC); |
| 137 | if (!rp) { |
Jesper Juhl | b2c60d4 | 2011-01-14 00:18:49 +0100 | [diff] [blame] | 138 | read_unlock(&hci_dev_list_lock); |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 139 | return -ENOMEM; |
Jesper Juhl | b2c60d4 | 2011-01-14 00:18:49 +0100 | [diff] [blame] | 140 | } |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 141 | |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 142 | put_unaligned_le16(count, &rp->num_controllers); |
| 143 | |
| 144 | i = 0; |
| 145 | list_for_each(p, &hci_dev_list) { |
| 146 | struct hci_dev *d = list_entry(p, struct hci_dev, list); |
Johan Hedberg | ab81cbf | 2010-12-15 13:53:18 +0200 | [diff] [blame] | 147 | |
| 148 | hci_del_off_timer(d); |
| 149 | |
Johan Hedberg | ebc99fe | 2011-01-04 11:54:26 +0200 | [diff] [blame] | 150 | set_bit(HCI_MGMT, &d->flags); |
| 151 | |
Johan Hedberg | ab81cbf | 2010-12-15 13:53:18 +0200 | [diff] [blame] | 152 | if (test_bit(HCI_SETUP, &d->flags)) |
| 153 | continue; |
| 154 | |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 155 | put_unaligned_le16(d->id, &rp->index[i++]); |
| 156 | BT_DBG("Added hci%u", d->id); |
| 157 | } |
| 158 | |
| 159 | read_unlock(&hci_dev_list_lock); |
| 160 | |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 161 | err = cmd_complete(sk, MGMT_INDEX_NONE, MGMT_OP_READ_INDEX_LIST, rp, |
| 162 | rp_len); |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 163 | |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 164 | kfree(rp); |
| 165 | |
| 166 | return err; |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 167 | } |
| 168 | |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 169 | static int read_controller_info(struct sock *sk, u16 index) |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 170 | { |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 171 | struct mgmt_rp_read_info rp; |
Johan Hedberg | f7b64e6 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 172 | struct hci_dev *hdev; |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 173 | |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 174 | BT_DBG("sock %p hci%u", sk, index); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 175 | |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 176 | hdev = hci_dev_get(index); |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 177 | if (!hdev) |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 178 | return cmd_status(sk, index, MGMT_OP_READ_INFO, ENODEV); |
Johan Hedberg | f7b64e6 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 179 | |
Johan Hedberg | ab81cbf | 2010-12-15 13:53:18 +0200 | [diff] [blame] | 180 | hci_del_off_timer(hdev); |
| 181 | |
Johan Hedberg | f7b64e6 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 182 | hci_dev_lock_bh(hdev); |
| 183 | |
Johan Hedberg | ebc99fe | 2011-01-04 11:54:26 +0200 | [diff] [blame] | 184 | set_bit(HCI_MGMT, &hdev->flags); |
| 185 | |
Johan Hedberg | dc4fe30 | 2011-03-16 14:29:36 +0200 | [diff] [blame] | 186 | memset(&rp, 0, sizeof(rp)); |
| 187 | |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 188 | rp.type = hdev->dev_type; |
Johan Hedberg | f7b64e6 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 189 | |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 190 | rp.powered = test_bit(HCI_UP, &hdev->flags); |
| 191 | rp.connectable = test_bit(HCI_PSCAN, &hdev->flags); |
| 192 | rp.discoverable = test_bit(HCI_ISCAN, &hdev->flags); |
| 193 | rp.pairable = test_bit(HCI_PSCAN, &hdev->flags); |
Johan Hedberg | f7b64e6 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 194 | |
| 195 | if (test_bit(HCI_AUTH, &hdev->flags)) |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 196 | rp.sec_mode = 3; |
Johan Hedberg | f7b64e6 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 197 | else if (hdev->ssp_mode > 0) |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 198 | rp.sec_mode = 4; |
Johan Hedberg | f7b64e6 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 199 | else |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 200 | rp.sec_mode = 2; |
Johan Hedberg | f7b64e6 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 201 | |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 202 | bacpy(&rp.bdaddr, &hdev->bdaddr); |
| 203 | memcpy(rp.features, hdev->features, 8); |
| 204 | memcpy(rp.dev_class, hdev->dev_class, 3); |
| 205 | put_unaligned_le16(hdev->manufacturer, &rp.manufacturer); |
| 206 | rp.hci_ver = hdev->hci_ver; |
| 207 | put_unaligned_le16(hdev->hci_rev, &rp.hci_rev); |
Johan Hedberg | f7b64e6 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 208 | |
Johan Hedberg | dc4fe30 | 2011-03-16 14:29:36 +0200 | [diff] [blame] | 209 | memcpy(rp.name, hdev->dev_name, sizeof(hdev->dev_name)); |
| 210 | |
Johan Hedberg | f7b64e6 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 211 | hci_dev_unlock_bh(hdev); |
| 212 | hci_dev_put(hdev); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 213 | |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 214 | return cmd_complete(sk, index, MGMT_OP_READ_INFO, &rp, sizeof(rp)); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 215 | } |
| 216 | |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 217 | static void mgmt_pending_free(struct pending_cmd *cmd) |
| 218 | { |
| 219 | sock_put(cmd->sk); |
Szymon Janc | c68fb7f | 2011-03-22 13:12:19 +0100 | [diff] [blame^] | 220 | kfree(cmd->param); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 221 | kfree(cmd); |
| 222 | } |
| 223 | |
Johan Hedberg | 366a033 | 2011-02-19 12:05:55 -0300 | [diff] [blame] | 224 | static struct pending_cmd *mgmt_pending_add(struct sock *sk, u16 opcode, |
| 225 | u16 index, void *data, u16 len) |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 226 | { |
| 227 | struct pending_cmd *cmd; |
| 228 | |
| 229 | cmd = kmalloc(sizeof(*cmd), GFP_ATOMIC); |
| 230 | if (!cmd) |
Johan Hedberg | 366a033 | 2011-02-19 12:05:55 -0300 | [diff] [blame] | 231 | return NULL; |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 232 | |
| 233 | cmd->opcode = opcode; |
| 234 | cmd->index = index; |
| 235 | |
Szymon Janc | c68fb7f | 2011-03-22 13:12:19 +0100 | [diff] [blame^] | 236 | cmd->param = kmalloc(len, GFP_ATOMIC); |
| 237 | if (!cmd->param) { |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 238 | kfree(cmd); |
Johan Hedberg | 366a033 | 2011-02-19 12:05:55 -0300 | [diff] [blame] | 239 | return NULL; |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 240 | } |
| 241 | |
Szymon Janc | c68fb7f | 2011-03-22 13:12:19 +0100 | [diff] [blame^] | 242 | memcpy(cmd->param, data, len); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 243 | |
| 244 | cmd->sk = sk; |
| 245 | sock_hold(sk); |
| 246 | |
| 247 | list_add(&cmd->list, &cmd_list); |
| 248 | |
Johan Hedberg | 366a033 | 2011-02-19 12:05:55 -0300 | [diff] [blame] | 249 | return cmd; |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 250 | } |
| 251 | |
| 252 | static void mgmt_pending_foreach(u16 opcode, int index, |
| 253 | void (*cb)(struct pending_cmd *cmd, void *data), |
| 254 | void *data) |
| 255 | { |
| 256 | struct list_head *p, *n; |
| 257 | |
| 258 | list_for_each_safe(p, n, &cmd_list) { |
| 259 | struct pending_cmd *cmd; |
| 260 | |
| 261 | cmd = list_entry(p, struct pending_cmd, list); |
| 262 | |
| 263 | if (cmd->opcode != opcode) |
| 264 | continue; |
| 265 | |
| 266 | if (index >= 0 && cmd->index != index) |
| 267 | continue; |
| 268 | |
| 269 | cb(cmd, data); |
| 270 | } |
| 271 | } |
| 272 | |
| 273 | static struct pending_cmd *mgmt_pending_find(u16 opcode, int index) |
| 274 | { |
| 275 | struct list_head *p; |
| 276 | |
| 277 | list_for_each(p, &cmd_list) { |
| 278 | struct pending_cmd *cmd; |
| 279 | |
| 280 | cmd = list_entry(p, struct pending_cmd, list); |
| 281 | |
| 282 | if (cmd->opcode != opcode) |
| 283 | continue; |
| 284 | |
| 285 | if (index >= 0 && cmd->index != index) |
| 286 | continue; |
| 287 | |
| 288 | return cmd; |
| 289 | } |
| 290 | |
| 291 | return NULL; |
| 292 | } |
| 293 | |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 294 | static void mgmt_pending_remove(struct pending_cmd *cmd) |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 295 | { |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 296 | list_del(&cmd->list); |
| 297 | mgmt_pending_free(cmd); |
| 298 | } |
| 299 | |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 300 | static int set_powered(struct sock *sk, u16 index, unsigned char *data, u16 len) |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 301 | { |
Johan Hedberg | 72a734e | 2010-12-30 00:38:22 +0200 | [diff] [blame] | 302 | struct mgmt_mode *cp; |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 303 | struct hci_dev *hdev; |
Johan Hedberg | 366a033 | 2011-02-19 12:05:55 -0300 | [diff] [blame] | 304 | struct pending_cmd *cmd; |
Johan Hedberg | 366a033 | 2011-02-19 12:05:55 -0300 | [diff] [blame] | 305 | int err, up; |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 306 | |
| 307 | cp = (void *) data; |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 308 | |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 309 | BT_DBG("request for hci%u", index); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 310 | |
Szymon Janc | bdce7ba | 2011-02-25 19:05:49 +0100 | [diff] [blame] | 311 | if (len != sizeof(*cp)) |
| 312 | return cmd_status(sk, index, MGMT_OP_SET_POWERED, EINVAL); |
| 313 | |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 314 | hdev = hci_dev_get(index); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 315 | if (!hdev) |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 316 | return cmd_status(sk, index, MGMT_OP_SET_POWERED, ENODEV); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 317 | |
| 318 | hci_dev_lock_bh(hdev); |
| 319 | |
| 320 | up = test_bit(HCI_UP, &hdev->flags); |
Johan Hedberg | 72a734e | 2010-12-30 00:38:22 +0200 | [diff] [blame] | 321 | if ((cp->val && up) || (!cp->val && !up)) { |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 322 | err = cmd_status(sk, index, MGMT_OP_SET_POWERED, EALREADY); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 323 | goto failed; |
| 324 | } |
| 325 | |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 326 | if (mgmt_pending_find(MGMT_OP_SET_POWERED, index)) { |
| 327 | err = cmd_status(sk, index, MGMT_OP_SET_POWERED, EBUSY); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 328 | goto failed; |
| 329 | } |
| 330 | |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 331 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_POWERED, index, data, len); |
Johan Hedberg | 366a033 | 2011-02-19 12:05:55 -0300 | [diff] [blame] | 332 | if (!cmd) { |
| 333 | err = -ENOMEM; |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 334 | goto failed; |
Johan Hedberg | 366a033 | 2011-02-19 12:05:55 -0300 | [diff] [blame] | 335 | } |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 336 | |
Johan Hedberg | 72a734e | 2010-12-30 00:38:22 +0200 | [diff] [blame] | 337 | if (cp->val) |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 338 | queue_work(hdev->workqueue, &hdev->power_on); |
| 339 | else |
| 340 | queue_work(hdev->workqueue, &hdev->power_off); |
| 341 | |
Johan Hedberg | 366a033 | 2011-02-19 12:05:55 -0300 | [diff] [blame] | 342 | err = 0; |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 343 | |
| 344 | failed: |
| 345 | hci_dev_unlock_bh(hdev); |
| 346 | hci_dev_put(hdev); |
Johan Hedberg | 366a033 | 2011-02-19 12:05:55 -0300 | [diff] [blame] | 347 | return err; |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 348 | } |
| 349 | |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 350 | static int set_discoverable(struct sock *sk, u16 index, unsigned char *data, |
| 351 | u16 len) |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 352 | { |
Johan Hedberg | 72a734e | 2010-12-30 00:38:22 +0200 | [diff] [blame] | 353 | struct mgmt_mode *cp; |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 354 | struct hci_dev *hdev; |
Johan Hedberg | 366a033 | 2011-02-19 12:05:55 -0300 | [diff] [blame] | 355 | struct pending_cmd *cmd; |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 356 | u8 scan; |
| 357 | int err; |
| 358 | |
| 359 | cp = (void *) data; |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 360 | |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 361 | BT_DBG("request for hci%u", index); |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 362 | |
Szymon Janc | bdce7ba | 2011-02-25 19:05:49 +0100 | [diff] [blame] | 363 | if (len != sizeof(*cp)) |
| 364 | return cmd_status(sk, index, MGMT_OP_SET_DISCOVERABLE, EINVAL); |
| 365 | |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 366 | hdev = hci_dev_get(index); |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 367 | if (!hdev) |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 368 | return cmd_status(sk, index, MGMT_OP_SET_DISCOVERABLE, ENODEV); |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 369 | |
| 370 | hci_dev_lock_bh(hdev); |
| 371 | |
| 372 | if (!test_bit(HCI_UP, &hdev->flags)) { |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 373 | err = cmd_status(sk, index, MGMT_OP_SET_DISCOVERABLE, ENETDOWN); |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 374 | goto failed; |
| 375 | } |
| 376 | |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 377 | if (mgmt_pending_find(MGMT_OP_SET_DISCOVERABLE, index) || |
| 378 | mgmt_pending_find(MGMT_OP_SET_CONNECTABLE, index)) { |
| 379 | err = cmd_status(sk, index, MGMT_OP_SET_DISCOVERABLE, EBUSY); |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 380 | goto failed; |
| 381 | } |
| 382 | |
Johan Hedberg | 72a734e | 2010-12-30 00:38:22 +0200 | [diff] [blame] | 383 | if (cp->val == test_bit(HCI_ISCAN, &hdev->flags) && |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 384 | test_bit(HCI_PSCAN, &hdev->flags)) { |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 385 | err = cmd_status(sk, index, MGMT_OP_SET_DISCOVERABLE, EALREADY); |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 386 | goto failed; |
| 387 | } |
| 388 | |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 389 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_DISCOVERABLE, index, data, len); |
Johan Hedberg | 366a033 | 2011-02-19 12:05:55 -0300 | [diff] [blame] | 390 | if (!cmd) { |
| 391 | err = -ENOMEM; |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 392 | goto failed; |
Johan Hedberg | 366a033 | 2011-02-19 12:05:55 -0300 | [diff] [blame] | 393 | } |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 394 | |
| 395 | scan = SCAN_PAGE; |
| 396 | |
Johan Hedberg | 72a734e | 2010-12-30 00:38:22 +0200 | [diff] [blame] | 397 | if (cp->val) |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 398 | scan |= SCAN_INQUIRY; |
| 399 | |
| 400 | err = hci_send_cmd(hdev, HCI_OP_WRITE_SCAN_ENABLE, 1, &scan); |
| 401 | if (err < 0) |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 402 | mgmt_pending_remove(cmd); |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 403 | |
| 404 | failed: |
| 405 | hci_dev_unlock_bh(hdev); |
| 406 | hci_dev_put(hdev); |
| 407 | |
| 408 | return err; |
| 409 | } |
| 410 | |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 411 | static int set_connectable(struct sock *sk, u16 index, unsigned char *data, |
| 412 | u16 len) |
Johan Hedberg | 9fbcbb4 | 2010-12-30 00:18:33 +0200 | [diff] [blame] | 413 | { |
Johan Hedberg | 72a734e | 2010-12-30 00:38:22 +0200 | [diff] [blame] | 414 | struct mgmt_mode *cp; |
Johan Hedberg | 9fbcbb4 | 2010-12-30 00:18:33 +0200 | [diff] [blame] | 415 | struct hci_dev *hdev; |
Johan Hedberg | 366a033 | 2011-02-19 12:05:55 -0300 | [diff] [blame] | 416 | struct pending_cmd *cmd; |
Johan Hedberg | 9fbcbb4 | 2010-12-30 00:18:33 +0200 | [diff] [blame] | 417 | u8 scan; |
| 418 | int err; |
| 419 | |
| 420 | cp = (void *) data; |
Johan Hedberg | 9fbcbb4 | 2010-12-30 00:18:33 +0200 | [diff] [blame] | 421 | |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 422 | BT_DBG("request for hci%u", index); |
Johan Hedberg | 9fbcbb4 | 2010-12-30 00:18:33 +0200 | [diff] [blame] | 423 | |
Szymon Janc | bdce7ba | 2011-02-25 19:05:49 +0100 | [diff] [blame] | 424 | if (len != sizeof(*cp)) |
| 425 | return cmd_status(sk, index, MGMT_OP_SET_CONNECTABLE, EINVAL); |
| 426 | |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 427 | hdev = hci_dev_get(index); |
Johan Hedberg | 9fbcbb4 | 2010-12-30 00:18:33 +0200 | [diff] [blame] | 428 | if (!hdev) |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 429 | return cmd_status(sk, index, MGMT_OP_SET_CONNECTABLE, ENODEV); |
Johan Hedberg | 9fbcbb4 | 2010-12-30 00:18:33 +0200 | [diff] [blame] | 430 | |
| 431 | hci_dev_lock_bh(hdev); |
| 432 | |
| 433 | if (!test_bit(HCI_UP, &hdev->flags)) { |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 434 | err = cmd_status(sk, index, MGMT_OP_SET_CONNECTABLE, ENETDOWN); |
Johan Hedberg | 9fbcbb4 | 2010-12-30 00:18:33 +0200 | [diff] [blame] | 435 | goto failed; |
| 436 | } |
| 437 | |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 438 | if (mgmt_pending_find(MGMT_OP_SET_DISCOVERABLE, index) || |
| 439 | mgmt_pending_find(MGMT_OP_SET_CONNECTABLE, index)) { |
| 440 | err = cmd_status(sk, index, MGMT_OP_SET_CONNECTABLE, EBUSY); |
Johan Hedberg | 9fbcbb4 | 2010-12-30 00:18:33 +0200 | [diff] [blame] | 441 | goto failed; |
| 442 | } |
| 443 | |
Johan Hedberg | 72a734e | 2010-12-30 00:38:22 +0200 | [diff] [blame] | 444 | if (cp->val == test_bit(HCI_PSCAN, &hdev->flags)) { |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 445 | err = cmd_status(sk, index, MGMT_OP_SET_CONNECTABLE, EALREADY); |
Johan Hedberg | 9fbcbb4 | 2010-12-30 00:18:33 +0200 | [diff] [blame] | 446 | goto failed; |
| 447 | } |
| 448 | |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 449 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_CONNECTABLE, index, data, len); |
Johan Hedberg | 366a033 | 2011-02-19 12:05:55 -0300 | [diff] [blame] | 450 | if (!cmd) { |
| 451 | err = -ENOMEM; |
Johan Hedberg | 9fbcbb4 | 2010-12-30 00:18:33 +0200 | [diff] [blame] | 452 | goto failed; |
Johan Hedberg | 366a033 | 2011-02-19 12:05:55 -0300 | [diff] [blame] | 453 | } |
Johan Hedberg | 9fbcbb4 | 2010-12-30 00:18:33 +0200 | [diff] [blame] | 454 | |
Johan Hedberg | 72a734e | 2010-12-30 00:38:22 +0200 | [diff] [blame] | 455 | if (cp->val) |
Johan Hedberg | 9fbcbb4 | 2010-12-30 00:18:33 +0200 | [diff] [blame] | 456 | scan = SCAN_PAGE; |
| 457 | else |
| 458 | scan = 0; |
| 459 | |
| 460 | err = hci_send_cmd(hdev, HCI_OP_WRITE_SCAN_ENABLE, 1, &scan); |
| 461 | if (err < 0) |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 462 | mgmt_pending_remove(cmd); |
Johan Hedberg | 9fbcbb4 | 2010-12-30 00:18:33 +0200 | [diff] [blame] | 463 | |
| 464 | failed: |
| 465 | hci_dev_unlock_bh(hdev); |
| 466 | hci_dev_put(hdev); |
| 467 | |
| 468 | return err; |
| 469 | } |
| 470 | |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 471 | static int mgmt_event(u16 event, u16 index, void *data, u16 data_len, |
| 472 | struct sock *skip_sk) |
Johan Hedberg | c542a06 | 2011-01-26 13:11:03 +0200 | [diff] [blame] | 473 | { |
| 474 | struct sk_buff *skb; |
| 475 | struct mgmt_hdr *hdr; |
| 476 | |
| 477 | skb = alloc_skb(sizeof(*hdr) + data_len, GFP_ATOMIC); |
| 478 | if (!skb) |
| 479 | return -ENOMEM; |
| 480 | |
| 481 | bt_cb(skb)->channel = HCI_CHANNEL_CONTROL; |
| 482 | |
| 483 | hdr = (void *) skb_put(skb, sizeof(*hdr)); |
| 484 | hdr->opcode = cpu_to_le16(event); |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 485 | hdr->index = cpu_to_le16(index); |
Johan Hedberg | c542a06 | 2011-01-26 13:11:03 +0200 | [diff] [blame] | 486 | hdr->len = cpu_to_le16(data_len); |
| 487 | |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 488 | if (data) |
| 489 | memcpy(skb_put(skb, data_len), data, data_len); |
Johan Hedberg | c542a06 | 2011-01-26 13:11:03 +0200 | [diff] [blame] | 490 | |
| 491 | hci_send_to_sock(NULL, skb, skip_sk); |
| 492 | kfree_skb(skb); |
| 493 | |
| 494 | return 0; |
| 495 | } |
| 496 | |
Johan Hedberg | 053f021 | 2011-01-26 13:07:10 +0200 | [diff] [blame] | 497 | static int send_mode_rsp(struct sock *sk, u16 opcode, u16 index, u8 val) |
| 498 | { |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 499 | struct mgmt_mode rp; |
Johan Hedberg | 053f021 | 2011-01-26 13:07:10 +0200 | [diff] [blame] | 500 | |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 501 | rp.val = val; |
Johan Hedberg | 053f021 | 2011-01-26 13:07:10 +0200 | [diff] [blame] | 502 | |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 503 | return cmd_complete(sk, index, opcode, &rp, sizeof(rp)); |
Johan Hedberg | 053f021 | 2011-01-26 13:07:10 +0200 | [diff] [blame] | 504 | } |
| 505 | |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 506 | static int set_pairable(struct sock *sk, u16 index, unsigned char *data, |
| 507 | u16 len) |
Johan Hedberg | c542a06 | 2011-01-26 13:11:03 +0200 | [diff] [blame] | 508 | { |
| 509 | struct mgmt_mode *cp, ev; |
| 510 | struct hci_dev *hdev; |
Johan Hedberg | c542a06 | 2011-01-26 13:11:03 +0200 | [diff] [blame] | 511 | int err; |
| 512 | |
| 513 | cp = (void *) data; |
Johan Hedberg | c542a06 | 2011-01-26 13:11:03 +0200 | [diff] [blame] | 514 | |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 515 | BT_DBG("request for hci%u", index); |
Johan Hedberg | c542a06 | 2011-01-26 13:11:03 +0200 | [diff] [blame] | 516 | |
Szymon Janc | bdce7ba | 2011-02-25 19:05:49 +0100 | [diff] [blame] | 517 | if (len != sizeof(*cp)) |
| 518 | return cmd_status(sk, index, MGMT_OP_SET_PAIRABLE, EINVAL); |
| 519 | |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 520 | hdev = hci_dev_get(index); |
Johan Hedberg | c542a06 | 2011-01-26 13:11:03 +0200 | [diff] [blame] | 521 | if (!hdev) |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 522 | return cmd_status(sk, index, MGMT_OP_SET_PAIRABLE, ENODEV); |
Johan Hedberg | c542a06 | 2011-01-26 13:11:03 +0200 | [diff] [blame] | 523 | |
| 524 | hci_dev_lock_bh(hdev); |
| 525 | |
| 526 | if (cp->val) |
| 527 | set_bit(HCI_PAIRABLE, &hdev->flags); |
| 528 | else |
| 529 | clear_bit(HCI_PAIRABLE, &hdev->flags); |
| 530 | |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 531 | err = send_mode_rsp(sk, MGMT_OP_SET_PAIRABLE, index, cp->val); |
Johan Hedberg | c542a06 | 2011-01-26 13:11:03 +0200 | [diff] [blame] | 532 | if (err < 0) |
| 533 | goto failed; |
| 534 | |
Johan Hedberg | c542a06 | 2011-01-26 13:11:03 +0200 | [diff] [blame] | 535 | ev.val = cp->val; |
| 536 | |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 537 | err = mgmt_event(MGMT_EV_PAIRABLE, index, &ev, sizeof(ev), sk); |
Johan Hedberg | c542a06 | 2011-01-26 13:11:03 +0200 | [diff] [blame] | 538 | |
| 539 | failed: |
| 540 | hci_dev_unlock_bh(hdev); |
| 541 | hci_dev_put(hdev); |
| 542 | |
| 543 | return err; |
| 544 | } |
| 545 | |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 546 | static u8 get_service_classes(struct hci_dev *hdev) |
| 547 | { |
| 548 | struct list_head *p; |
| 549 | u8 val = 0; |
| 550 | |
| 551 | list_for_each(p, &hdev->uuids) { |
| 552 | struct bt_uuid *uuid = list_entry(p, struct bt_uuid, list); |
| 553 | |
| 554 | val |= uuid->svc_hint; |
| 555 | } |
| 556 | |
| 557 | return val; |
| 558 | } |
| 559 | |
| 560 | static int update_class(struct hci_dev *hdev) |
| 561 | { |
| 562 | u8 cod[3]; |
| 563 | |
| 564 | BT_DBG("%s", hdev->name); |
| 565 | |
| 566 | if (test_bit(HCI_SERVICE_CACHE, &hdev->flags)) |
| 567 | return 0; |
| 568 | |
| 569 | cod[0] = hdev->minor_class; |
| 570 | cod[1] = hdev->major_class; |
| 571 | cod[2] = get_service_classes(hdev); |
| 572 | |
| 573 | if (memcmp(cod, hdev->dev_class, 3) == 0) |
| 574 | return 0; |
| 575 | |
| 576 | return hci_send_cmd(hdev, HCI_OP_WRITE_CLASS_OF_DEV, sizeof(cod), cod); |
| 577 | } |
| 578 | |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 579 | static int add_uuid(struct sock *sk, u16 index, unsigned char *data, u16 len) |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 580 | { |
| 581 | struct mgmt_cp_add_uuid *cp; |
| 582 | struct hci_dev *hdev; |
| 583 | struct bt_uuid *uuid; |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 584 | int err; |
| 585 | |
| 586 | cp = (void *) data; |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 587 | |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 588 | BT_DBG("request for hci%u", index); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 589 | |
Szymon Janc | bdce7ba | 2011-02-25 19:05:49 +0100 | [diff] [blame] | 590 | if (len != sizeof(*cp)) |
| 591 | return cmd_status(sk, index, MGMT_OP_ADD_UUID, EINVAL); |
| 592 | |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 593 | hdev = hci_dev_get(index); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 594 | if (!hdev) |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 595 | return cmd_status(sk, index, MGMT_OP_ADD_UUID, ENODEV); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 596 | |
| 597 | hci_dev_lock_bh(hdev); |
| 598 | |
| 599 | uuid = kmalloc(sizeof(*uuid), GFP_ATOMIC); |
| 600 | if (!uuid) { |
| 601 | err = -ENOMEM; |
| 602 | goto failed; |
| 603 | } |
| 604 | |
| 605 | memcpy(uuid->uuid, cp->uuid, 16); |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 606 | uuid->svc_hint = cp->svc_hint; |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 607 | |
| 608 | list_add(&uuid->list, &hdev->uuids); |
| 609 | |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 610 | err = update_class(hdev); |
| 611 | if (err < 0) |
| 612 | goto failed; |
| 613 | |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 614 | err = cmd_complete(sk, index, MGMT_OP_ADD_UUID, NULL, 0); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 615 | |
| 616 | failed: |
| 617 | hci_dev_unlock_bh(hdev); |
| 618 | hci_dev_put(hdev); |
| 619 | |
| 620 | return err; |
| 621 | } |
| 622 | |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 623 | static int remove_uuid(struct sock *sk, u16 index, unsigned char *data, u16 len) |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 624 | { |
| 625 | struct list_head *p, *n; |
Szymon Janc | 779cb85 | 2011-02-25 19:05:47 +0100 | [diff] [blame] | 626 | struct mgmt_cp_remove_uuid *cp; |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 627 | struct hci_dev *hdev; |
| 628 | u8 bt_uuid_any[] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 629 | int err, found; |
| 630 | |
| 631 | cp = (void *) data; |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 632 | |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 633 | BT_DBG("request for hci%u", index); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 634 | |
Szymon Janc | bdce7ba | 2011-02-25 19:05:49 +0100 | [diff] [blame] | 635 | if (len != sizeof(*cp)) |
| 636 | return cmd_status(sk, index, MGMT_OP_REMOVE_UUID, EINVAL); |
| 637 | |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 638 | hdev = hci_dev_get(index); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 639 | if (!hdev) |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 640 | return cmd_status(sk, index, MGMT_OP_REMOVE_UUID, ENODEV); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 641 | |
| 642 | hci_dev_lock_bh(hdev); |
| 643 | |
| 644 | if (memcmp(cp->uuid, bt_uuid_any, 16) == 0) { |
| 645 | err = hci_uuids_clear(hdev); |
| 646 | goto unlock; |
| 647 | } |
| 648 | |
| 649 | found = 0; |
| 650 | |
| 651 | list_for_each_safe(p, n, &hdev->uuids) { |
| 652 | struct bt_uuid *match = list_entry(p, struct bt_uuid, list); |
| 653 | |
| 654 | if (memcmp(match->uuid, cp->uuid, 16) != 0) |
| 655 | continue; |
| 656 | |
| 657 | list_del(&match->list); |
| 658 | found++; |
| 659 | } |
| 660 | |
| 661 | if (found == 0) { |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 662 | err = cmd_status(sk, index, MGMT_OP_REMOVE_UUID, ENOENT); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 663 | goto unlock; |
| 664 | } |
| 665 | |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 666 | err = update_class(hdev); |
| 667 | if (err < 0) |
| 668 | goto unlock; |
| 669 | |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 670 | err = cmd_complete(sk, index, MGMT_OP_REMOVE_UUID, NULL, 0); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 671 | |
| 672 | unlock: |
| 673 | hci_dev_unlock_bh(hdev); |
| 674 | hci_dev_put(hdev); |
| 675 | |
| 676 | return err; |
| 677 | } |
| 678 | |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 679 | static int set_dev_class(struct sock *sk, u16 index, unsigned char *data, |
| 680 | u16 len) |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 681 | { |
| 682 | struct hci_dev *hdev; |
| 683 | struct mgmt_cp_set_dev_class *cp; |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 684 | int err; |
| 685 | |
| 686 | cp = (void *) data; |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 687 | |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 688 | BT_DBG("request for hci%u", index); |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 689 | |
Szymon Janc | bdce7ba | 2011-02-25 19:05:49 +0100 | [diff] [blame] | 690 | if (len != sizeof(*cp)) |
| 691 | return cmd_status(sk, index, MGMT_OP_SET_DEV_CLASS, EINVAL); |
| 692 | |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 693 | hdev = hci_dev_get(index); |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 694 | if (!hdev) |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 695 | return cmd_status(sk, index, MGMT_OP_SET_DEV_CLASS, ENODEV); |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 696 | |
| 697 | hci_dev_lock_bh(hdev); |
| 698 | |
| 699 | hdev->major_class = cp->major; |
| 700 | hdev->minor_class = cp->minor; |
| 701 | |
| 702 | err = update_class(hdev); |
| 703 | |
| 704 | if (err == 0) |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 705 | err = cmd_complete(sk, index, MGMT_OP_SET_DEV_CLASS, NULL, 0); |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 706 | |
| 707 | hci_dev_unlock_bh(hdev); |
| 708 | hci_dev_put(hdev); |
| 709 | |
| 710 | return err; |
| 711 | } |
| 712 | |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 713 | static int set_service_cache(struct sock *sk, u16 index, unsigned char *data, |
| 714 | u16 len) |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 715 | { |
| 716 | struct hci_dev *hdev; |
| 717 | struct mgmt_cp_set_service_cache *cp; |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 718 | int err; |
| 719 | |
| 720 | cp = (void *) data; |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 721 | |
Szymon Janc | bdce7ba | 2011-02-25 19:05:49 +0100 | [diff] [blame] | 722 | if (len != sizeof(*cp)) |
Szymon Janc | b8534e0f | 2011-03-01 16:55:34 +0100 | [diff] [blame] | 723 | return cmd_status(sk, index, MGMT_OP_SET_SERVICE_CACHE, EINVAL); |
Szymon Janc | bdce7ba | 2011-02-25 19:05:49 +0100 | [diff] [blame] | 724 | |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 725 | hdev = hci_dev_get(index); |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 726 | if (!hdev) |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 727 | return cmd_status(sk, index, MGMT_OP_SET_SERVICE_CACHE, ENODEV); |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 728 | |
| 729 | hci_dev_lock_bh(hdev); |
| 730 | |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 731 | BT_DBG("hci%u enable %d", index, cp->enable); |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 732 | |
| 733 | if (cp->enable) { |
| 734 | set_bit(HCI_SERVICE_CACHE, &hdev->flags); |
| 735 | err = 0; |
| 736 | } else { |
| 737 | clear_bit(HCI_SERVICE_CACHE, &hdev->flags); |
| 738 | err = update_class(hdev); |
| 739 | } |
| 740 | |
| 741 | if (err == 0) |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 742 | err = cmd_complete(sk, index, MGMT_OP_SET_SERVICE_CACHE, NULL, |
| 743 | 0); |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 744 | |
| 745 | hci_dev_unlock_bh(hdev); |
| 746 | hci_dev_put(hdev); |
| 747 | |
| 748 | return err; |
| 749 | } |
| 750 | |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 751 | static int load_keys(struct sock *sk, u16 index, unsigned char *data, u16 len) |
Johan Hedberg | 55ed8ca | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 752 | { |
| 753 | struct hci_dev *hdev; |
| 754 | struct mgmt_cp_load_keys *cp; |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 755 | u16 key_count, expected_len; |
Johan Hedberg | 55ed8ca | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 756 | int i; |
| 757 | |
| 758 | cp = (void *) data; |
Szymon Janc | bdce7ba | 2011-02-25 19:05:49 +0100 | [diff] [blame] | 759 | |
| 760 | if (len < sizeof(*cp)) |
| 761 | return -EINVAL; |
| 762 | |
Johan Hedberg | 55ed8ca | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 763 | key_count = get_unaligned_le16(&cp->key_count); |
| 764 | |
| 765 | expected_len = sizeof(*cp) + key_count * sizeof(struct mgmt_key_info); |
| 766 | if (expected_len != len) { |
| 767 | BT_ERR("load_keys: expected %u bytes, got %u bytes", |
| 768 | len, expected_len); |
| 769 | return -EINVAL; |
| 770 | } |
| 771 | |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 772 | hdev = hci_dev_get(index); |
Johan Hedberg | 55ed8ca | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 773 | if (!hdev) |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 774 | return cmd_status(sk, index, MGMT_OP_LOAD_KEYS, ENODEV); |
Johan Hedberg | 55ed8ca | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 775 | |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 776 | BT_DBG("hci%u debug_keys %u key_count %u", index, cp->debug_keys, |
Johan Hedberg | 55ed8ca | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 777 | key_count); |
| 778 | |
| 779 | hci_dev_lock_bh(hdev); |
| 780 | |
| 781 | hci_link_keys_clear(hdev); |
| 782 | |
| 783 | set_bit(HCI_LINK_KEYS, &hdev->flags); |
| 784 | |
| 785 | if (cp->debug_keys) |
| 786 | set_bit(HCI_DEBUG_KEYS, &hdev->flags); |
| 787 | else |
| 788 | clear_bit(HCI_DEBUG_KEYS, &hdev->flags); |
| 789 | |
| 790 | for (i = 0; i < key_count; i++) { |
| 791 | struct mgmt_key_info *key = &cp->keys[i]; |
| 792 | |
| 793 | hci_add_link_key(hdev, 0, &key->bdaddr, key->val, key->type, |
| 794 | key->pin_len); |
| 795 | } |
| 796 | |
| 797 | hci_dev_unlock_bh(hdev); |
| 798 | hci_dev_put(hdev); |
| 799 | |
| 800 | return 0; |
| 801 | } |
| 802 | |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 803 | static int remove_key(struct sock *sk, u16 index, unsigned char *data, u16 len) |
Johan Hedberg | 55ed8ca | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 804 | { |
| 805 | struct hci_dev *hdev; |
| 806 | struct mgmt_cp_remove_key *cp; |
| 807 | struct hci_conn *conn; |
Johan Hedberg | 55ed8ca | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 808 | int err; |
| 809 | |
| 810 | cp = (void *) data; |
Johan Hedberg | 55ed8ca | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 811 | |
Szymon Janc | bdce7ba | 2011-02-25 19:05:49 +0100 | [diff] [blame] | 812 | if (len != sizeof(*cp)) |
| 813 | return cmd_status(sk, index, MGMT_OP_REMOVE_KEY, EINVAL); |
| 814 | |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 815 | hdev = hci_dev_get(index); |
Johan Hedberg | 55ed8ca | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 816 | if (!hdev) |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 817 | return cmd_status(sk, index, MGMT_OP_REMOVE_KEY, ENODEV); |
Johan Hedberg | 55ed8ca | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 818 | |
| 819 | hci_dev_lock_bh(hdev); |
| 820 | |
| 821 | err = hci_remove_link_key(hdev, &cp->bdaddr); |
| 822 | if (err < 0) { |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 823 | err = cmd_status(sk, index, MGMT_OP_REMOVE_KEY, -err); |
Johan Hedberg | 55ed8ca | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 824 | goto unlock; |
| 825 | } |
| 826 | |
| 827 | err = 0; |
| 828 | |
| 829 | if (!test_bit(HCI_UP, &hdev->flags) || !cp->disconnect) |
| 830 | goto unlock; |
| 831 | |
| 832 | conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &cp->bdaddr); |
| 833 | if (conn) { |
| 834 | struct hci_cp_disconnect dc; |
| 835 | |
| 836 | put_unaligned_le16(conn->handle, &dc.handle); |
| 837 | dc.reason = 0x13; /* Remote User Terminated Connection */ |
| 838 | err = hci_send_cmd(hdev, HCI_OP_DISCONNECT, 0, NULL); |
| 839 | } |
| 840 | |
| 841 | unlock: |
| 842 | hci_dev_unlock_bh(hdev); |
| 843 | hci_dev_put(hdev); |
| 844 | |
| 845 | return err; |
| 846 | } |
| 847 | |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 848 | static int disconnect(struct sock *sk, u16 index, unsigned char *data, u16 len) |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 849 | { |
| 850 | struct hci_dev *hdev; |
| 851 | struct mgmt_cp_disconnect *cp; |
| 852 | struct hci_cp_disconnect dc; |
Johan Hedberg | 366a033 | 2011-02-19 12:05:55 -0300 | [diff] [blame] | 853 | struct pending_cmd *cmd; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 854 | struct hci_conn *conn; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 855 | int err; |
| 856 | |
| 857 | BT_DBG(""); |
| 858 | |
| 859 | cp = (void *) data; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 860 | |
Szymon Janc | bdce7ba | 2011-02-25 19:05:49 +0100 | [diff] [blame] | 861 | if (len != sizeof(*cp)) |
| 862 | return cmd_status(sk, index, MGMT_OP_DISCONNECT, EINVAL); |
| 863 | |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 864 | hdev = hci_dev_get(index); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 865 | if (!hdev) |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 866 | return cmd_status(sk, index, MGMT_OP_DISCONNECT, ENODEV); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 867 | |
| 868 | hci_dev_lock_bh(hdev); |
| 869 | |
| 870 | if (!test_bit(HCI_UP, &hdev->flags)) { |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 871 | err = cmd_status(sk, index, MGMT_OP_DISCONNECT, ENETDOWN); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 872 | goto failed; |
| 873 | } |
| 874 | |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 875 | if (mgmt_pending_find(MGMT_OP_DISCONNECT, index)) { |
| 876 | err = cmd_status(sk, index, MGMT_OP_DISCONNECT, EBUSY); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 877 | goto failed; |
| 878 | } |
| 879 | |
| 880 | conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &cp->bdaddr); |
| 881 | if (!conn) { |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 882 | err = cmd_status(sk, index, MGMT_OP_DISCONNECT, ENOTCONN); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 883 | goto failed; |
| 884 | } |
| 885 | |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 886 | cmd = mgmt_pending_add(sk, MGMT_OP_DISCONNECT, index, data, len); |
Johan Hedberg | 366a033 | 2011-02-19 12:05:55 -0300 | [diff] [blame] | 887 | if (!cmd) { |
| 888 | err = -ENOMEM; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 889 | goto failed; |
Johan Hedberg | 366a033 | 2011-02-19 12:05:55 -0300 | [diff] [blame] | 890 | } |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 891 | |
| 892 | put_unaligned_le16(conn->handle, &dc.handle); |
| 893 | dc.reason = 0x13; /* Remote User Terminated Connection */ |
| 894 | |
| 895 | err = hci_send_cmd(hdev, HCI_OP_DISCONNECT, sizeof(dc), &dc); |
| 896 | if (err < 0) |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 897 | mgmt_pending_remove(cmd); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 898 | |
| 899 | failed: |
| 900 | hci_dev_unlock_bh(hdev); |
| 901 | hci_dev_put(hdev); |
| 902 | |
| 903 | return err; |
| 904 | } |
| 905 | |
Szymon Janc | 8ce6284 | 2011-03-01 16:55:32 +0100 | [diff] [blame] | 906 | static int get_connections(struct sock *sk, u16 index) |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 907 | { |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 908 | struct mgmt_rp_get_connections *rp; |
| 909 | struct hci_dev *hdev; |
| 910 | struct list_head *p; |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 911 | size_t rp_len; |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 912 | u16 count; |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 913 | int i, err; |
| 914 | |
| 915 | BT_DBG(""); |
| 916 | |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 917 | hdev = hci_dev_get(index); |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 918 | if (!hdev) |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 919 | return cmd_status(sk, index, MGMT_OP_GET_CONNECTIONS, ENODEV); |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 920 | |
| 921 | hci_dev_lock_bh(hdev); |
| 922 | |
| 923 | count = 0; |
| 924 | list_for_each(p, &hdev->conn_hash.list) { |
| 925 | count++; |
| 926 | } |
| 927 | |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 928 | rp_len = sizeof(*rp) + (count * sizeof(bdaddr_t)); |
| 929 | rp = kmalloc(rp_len, GFP_ATOMIC); |
| 930 | if (!rp) { |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 931 | err = -ENOMEM; |
| 932 | goto unlock; |
| 933 | } |
| 934 | |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 935 | put_unaligned_le16(count, &rp->conn_count); |
| 936 | |
| 937 | read_lock(&hci_dev_list_lock); |
| 938 | |
| 939 | i = 0; |
| 940 | list_for_each(p, &hdev->conn_hash.list) { |
| 941 | struct hci_conn *c = list_entry(p, struct hci_conn, list); |
| 942 | |
| 943 | bacpy(&rp->conn[i++], &c->dst); |
| 944 | } |
| 945 | |
| 946 | read_unlock(&hci_dev_list_lock); |
| 947 | |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 948 | err = cmd_complete(sk, index, MGMT_OP_GET_CONNECTIONS, rp, rp_len); |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 949 | |
| 950 | unlock: |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 951 | kfree(rp); |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 952 | hci_dev_unlock_bh(hdev); |
| 953 | hci_dev_put(hdev); |
| 954 | return err; |
| 955 | } |
| 956 | |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 957 | static int pin_code_reply(struct sock *sk, u16 index, unsigned char *data, |
| 958 | u16 len) |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 959 | { |
| 960 | struct hci_dev *hdev; |
| 961 | struct mgmt_cp_pin_code_reply *cp; |
| 962 | struct hci_cp_pin_code_reply reply; |
Johan Hedberg | 366a033 | 2011-02-19 12:05:55 -0300 | [diff] [blame] | 963 | struct pending_cmd *cmd; |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 964 | int err; |
| 965 | |
| 966 | BT_DBG(""); |
| 967 | |
| 968 | cp = (void *) data; |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 969 | |
Szymon Janc | bdce7ba | 2011-02-25 19:05:49 +0100 | [diff] [blame] | 970 | if (len != sizeof(*cp)) |
| 971 | return cmd_status(sk, index, MGMT_OP_PIN_CODE_REPLY, EINVAL); |
| 972 | |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 973 | hdev = hci_dev_get(index); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 974 | if (!hdev) |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 975 | return cmd_status(sk, index, MGMT_OP_PIN_CODE_REPLY, ENODEV); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 976 | |
| 977 | hci_dev_lock_bh(hdev); |
| 978 | |
| 979 | if (!test_bit(HCI_UP, &hdev->flags)) { |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 980 | err = cmd_status(sk, index, MGMT_OP_PIN_CODE_REPLY, ENETDOWN); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 981 | goto failed; |
| 982 | } |
| 983 | |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 984 | cmd = mgmt_pending_add(sk, MGMT_OP_PIN_CODE_REPLY, index, data, len); |
Johan Hedberg | 366a033 | 2011-02-19 12:05:55 -0300 | [diff] [blame] | 985 | if (!cmd) { |
| 986 | err = -ENOMEM; |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 987 | goto failed; |
Johan Hedberg | 366a033 | 2011-02-19 12:05:55 -0300 | [diff] [blame] | 988 | } |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 989 | |
| 990 | bacpy(&reply.bdaddr, &cp->bdaddr); |
| 991 | reply.pin_len = cp->pin_len; |
| 992 | memcpy(reply.pin_code, cp->pin_code, 16); |
| 993 | |
| 994 | err = hci_send_cmd(hdev, HCI_OP_PIN_CODE_REPLY, sizeof(reply), &reply); |
| 995 | if (err < 0) |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 996 | mgmt_pending_remove(cmd); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 997 | |
| 998 | failed: |
| 999 | hci_dev_unlock_bh(hdev); |
| 1000 | hci_dev_put(hdev); |
| 1001 | |
| 1002 | return err; |
| 1003 | } |
| 1004 | |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 1005 | static int pin_code_neg_reply(struct sock *sk, u16 index, unsigned char *data, |
| 1006 | u16 len) |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 1007 | { |
| 1008 | struct hci_dev *hdev; |
| 1009 | struct mgmt_cp_pin_code_neg_reply *cp; |
Johan Hedberg | 366a033 | 2011-02-19 12:05:55 -0300 | [diff] [blame] | 1010 | struct pending_cmd *cmd; |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 1011 | int err; |
| 1012 | |
| 1013 | BT_DBG(""); |
| 1014 | |
| 1015 | cp = (void *) data; |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 1016 | |
Szymon Janc | bdce7ba | 2011-02-25 19:05:49 +0100 | [diff] [blame] | 1017 | if (len != sizeof(*cp)) |
| 1018 | return cmd_status(sk, index, MGMT_OP_PIN_CODE_NEG_REPLY, |
| 1019 | EINVAL); |
| 1020 | |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 1021 | hdev = hci_dev_get(index); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 1022 | if (!hdev) |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 1023 | return cmd_status(sk, index, MGMT_OP_PIN_CODE_NEG_REPLY, |
| 1024 | ENODEV); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 1025 | |
| 1026 | hci_dev_lock_bh(hdev); |
| 1027 | |
| 1028 | if (!test_bit(HCI_UP, &hdev->flags)) { |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 1029 | err = cmd_status(sk, index, MGMT_OP_PIN_CODE_NEG_REPLY, |
| 1030 | ENETDOWN); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 1031 | goto failed; |
| 1032 | } |
| 1033 | |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 1034 | cmd = mgmt_pending_add(sk, MGMT_OP_PIN_CODE_NEG_REPLY, index, |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 1035 | data, len); |
Johan Hedberg | 366a033 | 2011-02-19 12:05:55 -0300 | [diff] [blame] | 1036 | if (!cmd) { |
| 1037 | err = -ENOMEM; |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 1038 | goto failed; |
Johan Hedberg | 366a033 | 2011-02-19 12:05:55 -0300 | [diff] [blame] | 1039 | } |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 1040 | |
Szymon Janc | 3cf2a4f | 2011-03-01 16:55:33 +0100 | [diff] [blame] | 1041 | err = hci_send_cmd(hdev, HCI_OP_PIN_CODE_NEG_REPLY, sizeof(cp->bdaddr), |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 1042 | &cp->bdaddr); |
| 1043 | if (err < 0) |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 1044 | mgmt_pending_remove(cmd); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 1045 | |
| 1046 | failed: |
| 1047 | hci_dev_unlock_bh(hdev); |
| 1048 | hci_dev_put(hdev); |
| 1049 | |
| 1050 | return err; |
| 1051 | } |
| 1052 | |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 1053 | static int set_io_capability(struct sock *sk, u16 index, unsigned char *data, |
| 1054 | u16 len) |
Johan Hedberg | 17fa4b9 | 2011-01-25 13:28:33 +0200 | [diff] [blame] | 1055 | { |
| 1056 | struct hci_dev *hdev; |
| 1057 | struct mgmt_cp_set_io_capability *cp; |
Johan Hedberg | 17fa4b9 | 2011-01-25 13:28:33 +0200 | [diff] [blame] | 1058 | |
| 1059 | BT_DBG(""); |
| 1060 | |
| 1061 | cp = (void *) data; |
Johan Hedberg | 17fa4b9 | 2011-01-25 13:28:33 +0200 | [diff] [blame] | 1062 | |
Szymon Janc | bdce7ba | 2011-02-25 19:05:49 +0100 | [diff] [blame] | 1063 | if (len != sizeof(*cp)) |
Szymon Janc | b8534e0f | 2011-03-01 16:55:34 +0100 | [diff] [blame] | 1064 | return cmd_status(sk, index, MGMT_OP_SET_IO_CAPABILITY, EINVAL); |
Szymon Janc | bdce7ba | 2011-02-25 19:05:49 +0100 | [diff] [blame] | 1065 | |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 1066 | hdev = hci_dev_get(index); |
Johan Hedberg | 17fa4b9 | 2011-01-25 13:28:33 +0200 | [diff] [blame] | 1067 | if (!hdev) |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 1068 | return cmd_status(sk, index, MGMT_OP_SET_IO_CAPABILITY, ENODEV); |
Johan Hedberg | 17fa4b9 | 2011-01-25 13:28:33 +0200 | [diff] [blame] | 1069 | |
| 1070 | hci_dev_lock_bh(hdev); |
| 1071 | |
| 1072 | hdev->io_capability = cp->io_capability; |
| 1073 | |
| 1074 | BT_DBG("%s IO capability set to 0x%02x", hdev->name, |
Szymon Janc | b8534e0f | 2011-03-01 16:55:34 +0100 | [diff] [blame] | 1075 | hdev->io_capability); |
Johan Hedberg | 17fa4b9 | 2011-01-25 13:28:33 +0200 | [diff] [blame] | 1076 | |
| 1077 | hci_dev_unlock_bh(hdev); |
| 1078 | hci_dev_put(hdev); |
| 1079 | |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 1080 | return cmd_complete(sk, index, MGMT_OP_SET_IO_CAPABILITY, NULL, 0); |
Johan Hedberg | 17fa4b9 | 2011-01-25 13:28:33 +0200 | [diff] [blame] | 1081 | } |
| 1082 | |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 1083 | static inline struct pending_cmd *find_pairing(struct hci_conn *conn) |
| 1084 | { |
| 1085 | struct hci_dev *hdev = conn->hdev; |
| 1086 | struct list_head *p; |
| 1087 | |
| 1088 | list_for_each(p, &cmd_list) { |
| 1089 | struct pending_cmd *cmd; |
| 1090 | |
| 1091 | cmd = list_entry(p, struct pending_cmd, list); |
| 1092 | |
| 1093 | if (cmd->opcode != MGMT_OP_PAIR_DEVICE) |
| 1094 | continue; |
| 1095 | |
| 1096 | if (cmd->index != hdev->id) |
| 1097 | continue; |
| 1098 | |
| 1099 | if (cmd->user_data != conn) |
| 1100 | continue; |
| 1101 | |
| 1102 | return cmd; |
| 1103 | } |
| 1104 | |
| 1105 | return NULL; |
| 1106 | } |
| 1107 | |
| 1108 | static void pairing_complete(struct pending_cmd *cmd, u8 status) |
| 1109 | { |
| 1110 | struct mgmt_rp_pair_device rp; |
| 1111 | struct hci_conn *conn = cmd->user_data; |
| 1112 | |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 1113 | bacpy(&rp.bdaddr, &conn->dst); |
| 1114 | rp.status = status; |
| 1115 | |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 1116 | cmd_complete(cmd->sk, cmd->index, MGMT_OP_PAIR_DEVICE, &rp, sizeof(rp)); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 1117 | |
| 1118 | /* So we don't get further callbacks for this connection */ |
| 1119 | conn->connect_cfm_cb = NULL; |
| 1120 | conn->security_cfm_cb = NULL; |
| 1121 | conn->disconn_cfm_cb = NULL; |
| 1122 | |
| 1123 | hci_conn_put(conn); |
| 1124 | |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 1125 | mgmt_pending_remove(cmd); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 1126 | } |
| 1127 | |
| 1128 | static void pairing_complete_cb(struct hci_conn *conn, u8 status) |
| 1129 | { |
| 1130 | struct pending_cmd *cmd; |
| 1131 | |
| 1132 | BT_DBG("status %u", status); |
| 1133 | |
| 1134 | cmd = find_pairing(conn); |
| 1135 | if (!cmd) { |
| 1136 | BT_DBG("Unable to find a pending command"); |
| 1137 | return; |
| 1138 | } |
| 1139 | |
| 1140 | pairing_complete(cmd, status); |
| 1141 | } |
| 1142 | |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 1143 | static int pair_device(struct sock *sk, u16 index, unsigned char *data, u16 len) |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 1144 | { |
| 1145 | struct hci_dev *hdev; |
| 1146 | struct mgmt_cp_pair_device *cp; |
| 1147 | struct pending_cmd *cmd; |
| 1148 | u8 sec_level, auth_type; |
| 1149 | struct hci_conn *conn; |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 1150 | int err; |
| 1151 | |
| 1152 | BT_DBG(""); |
| 1153 | |
| 1154 | cp = (void *) data; |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 1155 | |
Szymon Janc | bdce7ba | 2011-02-25 19:05:49 +0100 | [diff] [blame] | 1156 | if (len != sizeof(*cp)) |
| 1157 | return cmd_status(sk, index, MGMT_OP_PAIR_DEVICE, EINVAL); |
| 1158 | |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 1159 | hdev = hci_dev_get(index); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 1160 | if (!hdev) |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 1161 | return cmd_status(sk, index, MGMT_OP_PAIR_DEVICE, ENODEV); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 1162 | |
| 1163 | hci_dev_lock_bh(hdev); |
| 1164 | |
| 1165 | if (cp->io_cap == 0x03) { |
| 1166 | sec_level = BT_SECURITY_MEDIUM; |
| 1167 | auth_type = HCI_AT_DEDICATED_BONDING; |
| 1168 | } else { |
| 1169 | sec_level = BT_SECURITY_HIGH; |
| 1170 | auth_type = HCI_AT_DEDICATED_BONDING_MITM; |
| 1171 | } |
| 1172 | |
| 1173 | conn = hci_connect(hdev, ACL_LINK, &cp->bdaddr, sec_level, auth_type); |
Ville Tervo | 30e7627 | 2011-02-22 16:10:53 -0300 | [diff] [blame] | 1174 | if (IS_ERR(conn)) { |
| 1175 | err = PTR_ERR(conn); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 1176 | goto unlock; |
| 1177 | } |
| 1178 | |
| 1179 | if (conn->connect_cfm_cb) { |
| 1180 | hci_conn_put(conn); |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 1181 | err = cmd_status(sk, index, MGMT_OP_PAIR_DEVICE, EBUSY); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 1182 | goto unlock; |
| 1183 | } |
| 1184 | |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 1185 | cmd = mgmt_pending_add(sk, MGMT_OP_PAIR_DEVICE, index, data, len); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 1186 | if (!cmd) { |
| 1187 | err = -ENOMEM; |
| 1188 | hci_conn_put(conn); |
| 1189 | goto unlock; |
| 1190 | } |
| 1191 | |
| 1192 | conn->connect_cfm_cb = pairing_complete_cb; |
| 1193 | conn->security_cfm_cb = pairing_complete_cb; |
| 1194 | conn->disconn_cfm_cb = pairing_complete_cb; |
| 1195 | conn->io_capability = cp->io_cap; |
| 1196 | cmd->user_data = conn; |
| 1197 | |
| 1198 | if (conn->state == BT_CONNECTED && |
| 1199 | hci_conn_security(conn, sec_level, auth_type)) |
| 1200 | pairing_complete(cmd, 0); |
| 1201 | |
| 1202 | err = 0; |
| 1203 | |
| 1204 | unlock: |
| 1205 | hci_dev_unlock_bh(hdev); |
| 1206 | hci_dev_put(hdev); |
| 1207 | |
| 1208 | return err; |
| 1209 | } |
| 1210 | |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 1211 | static int user_confirm_reply(struct sock *sk, u16 index, unsigned char *data, |
| 1212 | u16 len, int success) |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 1213 | { |
| 1214 | struct mgmt_cp_user_confirm_reply *cp = (void *) data; |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 1215 | u16 mgmt_op, hci_op; |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 1216 | struct pending_cmd *cmd; |
| 1217 | struct hci_dev *hdev; |
| 1218 | int err; |
| 1219 | |
| 1220 | BT_DBG(""); |
| 1221 | |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 1222 | if (success) { |
| 1223 | mgmt_op = MGMT_OP_USER_CONFIRM_REPLY; |
| 1224 | hci_op = HCI_OP_USER_CONFIRM_REPLY; |
| 1225 | } else { |
| 1226 | mgmt_op = MGMT_OP_USER_CONFIRM_NEG_REPLY; |
| 1227 | hci_op = HCI_OP_USER_CONFIRM_NEG_REPLY; |
| 1228 | } |
| 1229 | |
Szymon Janc | bdce7ba | 2011-02-25 19:05:49 +0100 | [diff] [blame] | 1230 | if (len != sizeof(*cp)) |
| 1231 | return cmd_status(sk, index, mgmt_op, EINVAL); |
| 1232 | |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 1233 | hdev = hci_dev_get(index); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 1234 | if (!hdev) |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 1235 | return cmd_status(sk, index, mgmt_op, ENODEV); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 1236 | |
| 1237 | if (!test_bit(HCI_UP, &hdev->flags)) { |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 1238 | err = cmd_status(sk, index, mgmt_op, ENETDOWN); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 1239 | goto failed; |
| 1240 | } |
| 1241 | |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 1242 | cmd = mgmt_pending_add(sk, mgmt_op, index, data, len); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 1243 | if (!cmd) { |
| 1244 | err = -ENOMEM; |
| 1245 | goto failed; |
| 1246 | } |
| 1247 | |
| 1248 | err = hci_send_cmd(hdev, hci_op, sizeof(cp->bdaddr), &cp->bdaddr); |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 1249 | if (err < 0) |
| 1250 | mgmt_pending_remove(cmd); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 1251 | |
| 1252 | failed: |
| 1253 | hci_dev_unlock_bh(hdev); |
| 1254 | hci_dev_put(hdev); |
| 1255 | |
| 1256 | return err; |
| 1257 | } |
| 1258 | |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 1259 | static int set_local_name(struct sock *sk, u16 index, unsigned char *data, |
| 1260 | u16 len) |
| 1261 | { |
| 1262 | struct mgmt_cp_set_local_name *mgmt_cp = (void *) data; |
| 1263 | struct hci_cp_write_local_name hci_cp; |
| 1264 | struct hci_dev *hdev; |
| 1265 | struct pending_cmd *cmd; |
| 1266 | int err; |
| 1267 | |
| 1268 | BT_DBG(""); |
| 1269 | |
| 1270 | if (len != sizeof(*mgmt_cp)) |
| 1271 | return cmd_status(sk, index, MGMT_OP_SET_LOCAL_NAME, EINVAL); |
| 1272 | |
| 1273 | hdev = hci_dev_get(index); |
| 1274 | if (!hdev) |
| 1275 | return cmd_status(sk, index, MGMT_OP_SET_LOCAL_NAME, ENODEV); |
| 1276 | |
| 1277 | hci_dev_lock_bh(hdev); |
| 1278 | |
| 1279 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_LOCAL_NAME, index, data, len); |
| 1280 | if (!cmd) { |
| 1281 | err = -ENOMEM; |
| 1282 | goto failed; |
| 1283 | } |
| 1284 | |
| 1285 | memcpy(hci_cp.name, mgmt_cp->name, sizeof(hci_cp.name)); |
| 1286 | err = hci_send_cmd(hdev, HCI_OP_WRITE_LOCAL_NAME, sizeof(hci_cp), |
| 1287 | &hci_cp); |
| 1288 | if (err < 0) |
| 1289 | mgmt_pending_remove(cmd); |
| 1290 | |
| 1291 | failed: |
| 1292 | hci_dev_unlock_bh(hdev); |
| 1293 | hci_dev_put(hdev); |
| 1294 | |
| 1295 | return err; |
| 1296 | } |
| 1297 | |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1298 | int mgmt_control(struct sock *sk, struct msghdr *msg, size_t msglen) |
| 1299 | { |
| 1300 | unsigned char *buf; |
| 1301 | struct mgmt_hdr *hdr; |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 1302 | u16 opcode, index, len; |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1303 | int err; |
| 1304 | |
| 1305 | BT_DBG("got %zu bytes", msglen); |
| 1306 | |
| 1307 | if (msglen < sizeof(*hdr)) |
| 1308 | return -EINVAL; |
| 1309 | |
| 1310 | buf = kmalloc(msglen, GFP_ATOMIC); |
| 1311 | if (!buf) |
| 1312 | return -ENOMEM; |
| 1313 | |
| 1314 | if (memcpy_fromiovec(buf, msg->msg_iov, msglen)) { |
| 1315 | err = -EFAULT; |
| 1316 | goto done; |
| 1317 | } |
| 1318 | |
| 1319 | hdr = (struct mgmt_hdr *) buf; |
| 1320 | opcode = get_unaligned_le16(&hdr->opcode); |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 1321 | index = get_unaligned_le16(&hdr->index); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1322 | len = get_unaligned_le16(&hdr->len); |
| 1323 | |
| 1324 | if (len != msglen - sizeof(*hdr)) { |
| 1325 | err = -EINVAL; |
| 1326 | goto done; |
| 1327 | } |
| 1328 | |
| 1329 | switch (opcode) { |
Johan Hedberg | 02d9812 | 2010-12-13 21:07:04 +0200 | [diff] [blame] | 1330 | case MGMT_OP_READ_VERSION: |
| 1331 | err = read_version(sk); |
| 1332 | break; |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 1333 | case MGMT_OP_READ_INDEX_LIST: |
| 1334 | err = read_index_list(sk); |
| 1335 | break; |
Johan Hedberg | f7b64e6 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 1336 | case MGMT_OP_READ_INFO: |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 1337 | err = read_controller_info(sk, index); |
Johan Hedberg | f7b64e6 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 1338 | break; |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1339 | case MGMT_OP_SET_POWERED: |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 1340 | err = set_powered(sk, index, buf + sizeof(*hdr), len); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1341 | break; |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 1342 | case MGMT_OP_SET_DISCOVERABLE: |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 1343 | err = set_discoverable(sk, index, buf + sizeof(*hdr), len); |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 1344 | break; |
Johan Hedberg | 9fbcbb4 | 2010-12-30 00:18:33 +0200 | [diff] [blame] | 1345 | case MGMT_OP_SET_CONNECTABLE: |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 1346 | err = set_connectable(sk, index, buf + sizeof(*hdr), len); |
Johan Hedberg | 9fbcbb4 | 2010-12-30 00:18:33 +0200 | [diff] [blame] | 1347 | break; |
Johan Hedberg | c542a06 | 2011-01-26 13:11:03 +0200 | [diff] [blame] | 1348 | case MGMT_OP_SET_PAIRABLE: |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 1349 | err = set_pairable(sk, index, buf + sizeof(*hdr), len); |
Johan Hedberg | c542a06 | 2011-01-26 13:11:03 +0200 | [diff] [blame] | 1350 | break; |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 1351 | case MGMT_OP_ADD_UUID: |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 1352 | err = add_uuid(sk, index, buf + sizeof(*hdr), len); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 1353 | break; |
| 1354 | case MGMT_OP_REMOVE_UUID: |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 1355 | err = remove_uuid(sk, index, buf + sizeof(*hdr), len); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 1356 | break; |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 1357 | case MGMT_OP_SET_DEV_CLASS: |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 1358 | err = set_dev_class(sk, index, buf + sizeof(*hdr), len); |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 1359 | break; |
| 1360 | case MGMT_OP_SET_SERVICE_CACHE: |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 1361 | err = set_service_cache(sk, index, buf + sizeof(*hdr), len); |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 1362 | break; |
Johan Hedberg | 55ed8ca | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 1363 | case MGMT_OP_LOAD_KEYS: |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 1364 | err = load_keys(sk, index, buf + sizeof(*hdr), len); |
Johan Hedberg | 55ed8ca | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 1365 | break; |
| 1366 | case MGMT_OP_REMOVE_KEY: |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 1367 | err = remove_key(sk, index, buf + sizeof(*hdr), len); |
Johan Hedberg | 55ed8ca | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 1368 | break; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 1369 | case MGMT_OP_DISCONNECT: |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 1370 | err = disconnect(sk, index, buf + sizeof(*hdr), len); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 1371 | break; |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 1372 | case MGMT_OP_GET_CONNECTIONS: |
Szymon Janc | 8ce6284 | 2011-03-01 16:55:32 +0100 | [diff] [blame] | 1373 | err = get_connections(sk, index); |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 1374 | break; |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 1375 | case MGMT_OP_PIN_CODE_REPLY: |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 1376 | err = pin_code_reply(sk, index, buf + sizeof(*hdr), len); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 1377 | break; |
| 1378 | case MGMT_OP_PIN_CODE_NEG_REPLY: |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 1379 | err = pin_code_neg_reply(sk, index, buf + sizeof(*hdr), len); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 1380 | break; |
Johan Hedberg | 17fa4b9 | 2011-01-25 13:28:33 +0200 | [diff] [blame] | 1381 | case MGMT_OP_SET_IO_CAPABILITY: |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 1382 | err = set_io_capability(sk, index, buf + sizeof(*hdr), len); |
Johan Hedberg | 17fa4b9 | 2011-01-25 13:28:33 +0200 | [diff] [blame] | 1383 | break; |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 1384 | case MGMT_OP_PAIR_DEVICE: |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 1385 | err = pair_device(sk, index, buf + sizeof(*hdr), len); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 1386 | break; |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 1387 | case MGMT_OP_USER_CONFIRM_REPLY: |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 1388 | err = user_confirm_reply(sk, index, buf + sizeof(*hdr), len, 1); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 1389 | break; |
| 1390 | case MGMT_OP_USER_CONFIRM_NEG_REPLY: |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 1391 | err = user_confirm_reply(sk, index, buf + sizeof(*hdr), len, 0); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 1392 | break; |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 1393 | case MGMT_OP_SET_LOCAL_NAME: |
| 1394 | err = set_local_name(sk, index, buf + sizeof(*hdr), len); |
| 1395 | break; |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1396 | default: |
| 1397 | BT_DBG("Unknown op %u", opcode); |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 1398 | err = cmd_status(sk, index, opcode, 0x01); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1399 | break; |
| 1400 | } |
| 1401 | |
Johan Hedberg | e41d8b4 | 2010-12-13 21:07:03 +0200 | [diff] [blame] | 1402 | if (err < 0) |
| 1403 | goto done; |
| 1404 | |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1405 | err = msglen; |
| 1406 | |
| 1407 | done: |
| 1408 | kfree(buf); |
| 1409 | return err; |
| 1410 | } |
Johan Hedberg | c71e97b | 2010-12-13 21:07:07 +0200 | [diff] [blame] | 1411 | |
Johan Hedberg | c71e97b | 2010-12-13 21:07:07 +0200 | [diff] [blame] | 1412 | int mgmt_index_added(u16 index) |
| 1413 | { |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 1414 | return mgmt_event(MGMT_EV_INDEX_ADDED, index, NULL, 0, NULL); |
Johan Hedberg | c71e97b | 2010-12-13 21:07:07 +0200 | [diff] [blame] | 1415 | } |
| 1416 | |
| 1417 | int mgmt_index_removed(u16 index) |
| 1418 | { |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 1419 | return mgmt_event(MGMT_EV_INDEX_REMOVED, index, NULL, 0, NULL); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1420 | } |
| 1421 | |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 1422 | struct cmd_lookup { |
Johan Hedberg | 72a734e | 2010-12-30 00:38:22 +0200 | [diff] [blame] | 1423 | u8 val; |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1424 | struct sock *sk; |
| 1425 | }; |
| 1426 | |
Johan Hedberg | 72a734e | 2010-12-30 00:38:22 +0200 | [diff] [blame] | 1427 | static void mode_rsp(struct pending_cmd *cmd, void *data) |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1428 | { |
Szymon Janc | c68fb7f | 2011-03-22 13:12:19 +0100 | [diff] [blame^] | 1429 | struct mgmt_mode *cp = cmd->param; |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 1430 | struct cmd_lookup *match = data; |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1431 | |
Johan Hedberg | 72a734e | 2010-12-30 00:38:22 +0200 | [diff] [blame] | 1432 | if (cp->val != match->val) |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1433 | return; |
| 1434 | |
Johan Hedberg | 053f021 | 2011-01-26 13:07:10 +0200 | [diff] [blame] | 1435 | send_mode_rsp(cmd->sk, cmd->opcode, cmd->index, cp->val); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1436 | |
| 1437 | list_del(&cmd->list); |
| 1438 | |
| 1439 | if (match->sk == NULL) { |
| 1440 | match->sk = cmd->sk; |
| 1441 | sock_hold(match->sk); |
| 1442 | } |
| 1443 | |
| 1444 | mgmt_pending_free(cmd); |
Johan Hedberg | c71e97b | 2010-12-13 21:07:07 +0200 | [diff] [blame] | 1445 | } |
Johan Hedberg | 5add6af | 2010-12-16 10:00:37 +0200 | [diff] [blame] | 1446 | |
| 1447 | int mgmt_powered(u16 index, u8 powered) |
| 1448 | { |
Johan Hedberg | 72a734e | 2010-12-30 00:38:22 +0200 | [diff] [blame] | 1449 | struct mgmt_mode ev; |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 1450 | struct cmd_lookup match = { powered, NULL }; |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1451 | int ret; |
Johan Hedberg | 5add6af | 2010-12-16 10:00:37 +0200 | [diff] [blame] | 1452 | |
Johan Hedberg | 72a734e | 2010-12-30 00:38:22 +0200 | [diff] [blame] | 1453 | mgmt_pending_foreach(MGMT_OP_SET_POWERED, index, mode_rsp, &match); |
Johan Hedberg | 5add6af | 2010-12-16 10:00:37 +0200 | [diff] [blame] | 1454 | |
Johan Hedberg | 72a734e | 2010-12-30 00:38:22 +0200 | [diff] [blame] | 1455 | ev.val = powered; |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1456 | |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 1457 | ret = mgmt_event(MGMT_EV_POWERED, index, &ev, sizeof(ev), match.sk); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1458 | |
| 1459 | if (match.sk) |
| 1460 | sock_put(match.sk); |
| 1461 | |
| 1462 | return ret; |
Johan Hedberg | 5add6af | 2010-12-16 10:00:37 +0200 | [diff] [blame] | 1463 | } |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 1464 | |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 1465 | int mgmt_discoverable(u16 index, u8 discoverable) |
| 1466 | { |
Johan Hedberg | 72a734e | 2010-12-30 00:38:22 +0200 | [diff] [blame] | 1467 | struct mgmt_mode ev; |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 1468 | struct cmd_lookup match = { discoverable, NULL }; |
| 1469 | int ret; |
| 1470 | |
Szymon Janc | b8534e0f | 2011-03-01 16:55:34 +0100 | [diff] [blame] | 1471 | mgmt_pending_foreach(MGMT_OP_SET_DISCOVERABLE, index, mode_rsp, &match); |
Johan Hedberg | 72a734e | 2010-12-30 00:38:22 +0200 | [diff] [blame] | 1472 | |
Johan Hedberg | 72a734e | 2010-12-30 00:38:22 +0200 | [diff] [blame] | 1473 | ev.val = discoverable; |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 1474 | |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 1475 | ret = mgmt_event(MGMT_EV_DISCOVERABLE, index, &ev, sizeof(ev), |
| 1476 | match.sk); |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 1477 | |
| 1478 | if (match.sk) |
| 1479 | sock_put(match.sk); |
| 1480 | |
| 1481 | return ret; |
| 1482 | } |
Johan Hedberg | 9fbcbb4 | 2010-12-30 00:18:33 +0200 | [diff] [blame] | 1483 | |
Johan Hedberg | 9fbcbb4 | 2010-12-30 00:18:33 +0200 | [diff] [blame] | 1484 | int mgmt_connectable(u16 index, u8 connectable) |
| 1485 | { |
Johan Hedberg | 72a734e | 2010-12-30 00:38:22 +0200 | [diff] [blame] | 1486 | struct mgmt_mode ev; |
Johan Hedberg | 9fbcbb4 | 2010-12-30 00:18:33 +0200 | [diff] [blame] | 1487 | struct cmd_lookup match = { connectable, NULL }; |
| 1488 | int ret; |
| 1489 | |
Johan Hedberg | 72a734e | 2010-12-30 00:38:22 +0200 | [diff] [blame] | 1490 | mgmt_pending_foreach(MGMT_OP_SET_CONNECTABLE, index, mode_rsp, &match); |
Johan Hedberg | 9fbcbb4 | 2010-12-30 00:18:33 +0200 | [diff] [blame] | 1491 | |
Johan Hedberg | 72a734e | 2010-12-30 00:38:22 +0200 | [diff] [blame] | 1492 | ev.val = connectable; |
Johan Hedberg | 9fbcbb4 | 2010-12-30 00:18:33 +0200 | [diff] [blame] | 1493 | |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 1494 | ret = mgmt_event(MGMT_EV_CONNECTABLE, index, &ev, sizeof(ev), match.sk); |
Johan Hedberg | 9fbcbb4 | 2010-12-30 00:18:33 +0200 | [diff] [blame] | 1495 | |
| 1496 | if (match.sk) |
| 1497 | sock_put(match.sk); |
| 1498 | |
| 1499 | return ret; |
| 1500 | } |
Johan Hedberg | 55ed8ca | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 1501 | |
| 1502 | int mgmt_new_key(u16 index, struct link_key *key, u8 old_key_type) |
| 1503 | { |
| 1504 | struct mgmt_ev_new_key ev; |
| 1505 | |
| 1506 | memset(&ev, 0, sizeof(ev)); |
| 1507 | |
Johan Hedberg | 55ed8ca | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 1508 | bacpy(&ev.key.bdaddr, &key->bdaddr); |
| 1509 | ev.key.type = key->type; |
| 1510 | memcpy(ev.key.val, key->val, 16); |
| 1511 | ev.key.pin_len = key->pin_len; |
| 1512 | ev.old_key_type = old_key_type; |
| 1513 | |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 1514 | return mgmt_event(MGMT_EV_NEW_KEY, index, &ev, sizeof(ev), NULL); |
Johan Hedberg | 55ed8ca | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 1515 | } |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 1516 | |
| 1517 | int mgmt_connected(u16 index, bdaddr_t *bdaddr) |
| 1518 | { |
| 1519 | struct mgmt_ev_connected ev; |
| 1520 | |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 1521 | bacpy(&ev.bdaddr, bdaddr); |
| 1522 | |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 1523 | return mgmt_event(MGMT_EV_CONNECTED, index, &ev, sizeof(ev), NULL); |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 1524 | } |
| 1525 | |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 1526 | static void disconnect_rsp(struct pending_cmd *cmd, void *data) |
| 1527 | { |
Szymon Janc | c68fb7f | 2011-03-22 13:12:19 +0100 | [diff] [blame^] | 1528 | struct mgmt_cp_disconnect *cp = cmd->param; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 1529 | struct sock **sk = data; |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 1530 | struct mgmt_rp_disconnect rp; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 1531 | |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 1532 | bacpy(&rp.bdaddr, &cp->bdaddr); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 1533 | |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 1534 | cmd_complete(cmd->sk, cmd->index, MGMT_OP_DISCONNECT, &rp, sizeof(rp)); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 1535 | |
| 1536 | *sk = cmd->sk; |
| 1537 | sock_hold(*sk); |
| 1538 | |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 1539 | mgmt_pending_remove(cmd); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 1540 | } |
| 1541 | |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 1542 | int mgmt_disconnected(u16 index, bdaddr_t *bdaddr) |
| 1543 | { |
| 1544 | struct mgmt_ev_disconnected ev; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 1545 | struct sock *sk = NULL; |
| 1546 | int err; |
| 1547 | |
| 1548 | mgmt_pending_foreach(MGMT_OP_DISCONNECT, index, disconnect_rsp, &sk); |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 1549 | |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 1550 | bacpy(&ev.bdaddr, bdaddr); |
| 1551 | |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 1552 | err = mgmt_event(MGMT_EV_DISCONNECTED, index, &ev, sizeof(ev), sk); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 1553 | |
| 1554 | if (sk) |
| 1555 | sock_put(sk); |
| 1556 | |
| 1557 | return err; |
| 1558 | } |
| 1559 | |
| 1560 | int mgmt_disconnect_failed(u16 index) |
| 1561 | { |
| 1562 | struct pending_cmd *cmd; |
| 1563 | int err; |
| 1564 | |
| 1565 | cmd = mgmt_pending_find(MGMT_OP_DISCONNECT, index); |
| 1566 | if (!cmd) |
| 1567 | return -ENOENT; |
| 1568 | |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 1569 | err = cmd_status(cmd->sk, index, MGMT_OP_DISCONNECT, EIO); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 1570 | |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 1571 | mgmt_pending_remove(cmd); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 1572 | |
| 1573 | return err; |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 1574 | } |
Johan Hedberg | 17d5c04 | 2011-01-22 06:09:08 +0200 | [diff] [blame] | 1575 | |
| 1576 | int mgmt_connect_failed(u16 index, bdaddr_t *bdaddr, u8 status) |
| 1577 | { |
| 1578 | struct mgmt_ev_connect_failed ev; |
| 1579 | |
Johan Hedberg | 17d5c04 | 2011-01-22 06:09:08 +0200 | [diff] [blame] | 1580 | bacpy(&ev.bdaddr, bdaddr); |
| 1581 | ev.status = status; |
| 1582 | |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 1583 | return mgmt_event(MGMT_EV_CONNECT_FAILED, index, &ev, sizeof(ev), NULL); |
Johan Hedberg | 17d5c04 | 2011-01-22 06:09:08 +0200 | [diff] [blame] | 1584 | } |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 1585 | |
| 1586 | int mgmt_pin_code_request(u16 index, bdaddr_t *bdaddr) |
| 1587 | { |
| 1588 | struct mgmt_ev_pin_code_request ev; |
| 1589 | |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 1590 | bacpy(&ev.bdaddr, bdaddr); |
| 1591 | |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 1592 | return mgmt_event(MGMT_EV_PIN_CODE_REQUEST, index, &ev, sizeof(ev), |
| 1593 | NULL); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 1594 | } |
| 1595 | |
| 1596 | int mgmt_pin_code_reply_complete(u16 index, bdaddr_t *bdaddr, u8 status) |
| 1597 | { |
| 1598 | struct pending_cmd *cmd; |
Johan Hedberg | ac56fb1 | 2011-02-19 12:05:59 -0300 | [diff] [blame] | 1599 | struct mgmt_rp_pin_code_reply rp; |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 1600 | int err; |
| 1601 | |
| 1602 | cmd = mgmt_pending_find(MGMT_OP_PIN_CODE_REPLY, index); |
| 1603 | if (!cmd) |
| 1604 | return -ENOENT; |
| 1605 | |
Johan Hedberg | ac56fb1 | 2011-02-19 12:05:59 -0300 | [diff] [blame] | 1606 | bacpy(&rp.bdaddr, bdaddr); |
| 1607 | rp.status = status; |
| 1608 | |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 1609 | err = cmd_complete(cmd->sk, index, MGMT_OP_PIN_CODE_REPLY, &rp, |
| 1610 | sizeof(rp)); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 1611 | |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 1612 | mgmt_pending_remove(cmd); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 1613 | |
| 1614 | return err; |
| 1615 | } |
| 1616 | |
| 1617 | int mgmt_pin_code_neg_reply_complete(u16 index, bdaddr_t *bdaddr, u8 status) |
| 1618 | { |
| 1619 | struct pending_cmd *cmd; |
Johan Hedberg | ac56fb1 | 2011-02-19 12:05:59 -0300 | [diff] [blame] | 1620 | struct mgmt_rp_pin_code_reply rp; |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 1621 | int err; |
| 1622 | |
| 1623 | cmd = mgmt_pending_find(MGMT_OP_PIN_CODE_NEG_REPLY, index); |
| 1624 | if (!cmd) |
| 1625 | return -ENOENT; |
| 1626 | |
Johan Hedberg | ac56fb1 | 2011-02-19 12:05:59 -0300 | [diff] [blame] | 1627 | bacpy(&rp.bdaddr, bdaddr); |
| 1628 | rp.status = status; |
| 1629 | |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 1630 | err = cmd_complete(cmd->sk, index, MGMT_OP_PIN_CODE_NEG_REPLY, &rp, |
| 1631 | sizeof(rp)); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 1632 | |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 1633 | mgmt_pending_remove(cmd); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 1634 | |
| 1635 | return err; |
| 1636 | } |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 1637 | |
| 1638 | int mgmt_user_confirm_request(u16 index, bdaddr_t *bdaddr, __le32 value) |
| 1639 | { |
| 1640 | struct mgmt_ev_user_confirm_request ev; |
| 1641 | |
| 1642 | BT_DBG("hci%u", index); |
| 1643 | |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 1644 | bacpy(&ev.bdaddr, bdaddr); |
| 1645 | put_unaligned_le32(value, &ev.value); |
| 1646 | |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 1647 | return mgmt_event(MGMT_EV_USER_CONFIRM_REQUEST, index, &ev, sizeof(ev), |
| 1648 | NULL); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 1649 | } |
| 1650 | |
| 1651 | static int confirm_reply_complete(u16 index, bdaddr_t *bdaddr, u8 status, |
| 1652 | u8 opcode) |
| 1653 | { |
| 1654 | struct pending_cmd *cmd; |
| 1655 | struct mgmt_rp_user_confirm_reply rp; |
| 1656 | int err; |
| 1657 | |
| 1658 | cmd = mgmt_pending_find(opcode, index); |
| 1659 | if (!cmd) |
| 1660 | return -ENOENT; |
| 1661 | |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 1662 | bacpy(&rp.bdaddr, bdaddr); |
| 1663 | rp.status = status; |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 1664 | err = cmd_complete(cmd->sk, index, opcode, &rp, sizeof(rp)); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 1665 | |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 1666 | mgmt_pending_remove(cmd); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 1667 | |
| 1668 | return err; |
| 1669 | } |
| 1670 | |
| 1671 | int mgmt_user_confirm_reply_complete(u16 index, bdaddr_t *bdaddr, u8 status) |
| 1672 | { |
| 1673 | return confirm_reply_complete(index, bdaddr, status, |
| 1674 | MGMT_OP_USER_CONFIRM_REPLY); |
| 1675 | } |
| 1676 | |
Szymon Janc | b8534e0f | 2011-03-01 16:55:34 +0100 | [diff] [blame] | 1677 | int mgmt_user_confirm_neg_reply_complete(u16 index, bdaddr_t *bdaddr, u8 status) |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 1678 | { |
| 1679 | return confirm_reply_complete(index, bdaddr, status, |
| 1680 | MGMT_OP_USER_CONFIRM_NEG_REPLY); |
| 1681 | } |
Johan Hedberg | 2a61169 | 2011-02-19 12:06:00 -0300 | [diff] [blame] | 1682 | |
| 1683 | int mgmt_auth_failed(u16 index, bdaddr_t *bdaddr, u8 status) |
| 1684 | { |
| 1685 | struct mgmt_ev_auth_failed ev; |
| 1686 | |
Johan Hedberg | 2a61169 | 2011-02-19 12:06:00 -0300 | [diff] [blame] | 1687 | bacpy(&ev.bdaddr, bdaddr); |
| 1688 | ev.status = status; |
| 1689 | |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 1690 | return mgmt_event(MGMT_EV_AUTH_FAILED, index, &ev, sizeof(ev), NULL); |
Johan Hedberg | 2a61169 | 2011-02-19 12:06:00 -0300 | [diff] [blame] | 1691 | } |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 1692 | |
| 1693 | int mgmt_set_local_name_complete(u16 index, u8 *name, u8 status) |
| 1694 | { |
| 1695 | struct pending_cmd *cmd; |
| 1696 | struct mgmt_cp_set_local_name ev; |
| 1697 | int err; |
| 1698 | |
| 1699 | memset(&ev, 0, sizeof(ev)); |
| 1700 | memcpy(ev.name, name, HCI_MAX_NAME_LENGTH); |
| 1701 | |
| 1702 | cmd = mgmt_pending_find(MGMT_OP_SET_LOCAL_NAME, index); |
| 1703 | if (!cmd) |
| 1704 | goto send_event; |
| 1705 | |
| 1706 | if (status) { |
| 1707 | err = cmd_status(cmd->sk, index, MGMT_OP_SET_LOCAL_NAME, EIO); |
| 1708 | goto failed; |
| 1709 | } |
| 1710 | |
| 1711 | err = cmd_complete(cmd->sk, index, MGMT_OP_SET_LOCAL_NAME, &ev, |
| 1712 | sizeof(ev)); |
| 1713 | if (err < 0) |
| 1714 | goto failed; |
| 1715 | |
| 1716 | send_event: |
| 1717 | err = mgmt_event(MGMT_EV_LOCAL_NAME_CHANGED, index, &ev, sizeof(ev), |
| 1718 | cmd ? cmd->sk : NULL); |
| 1719 | |
| 1720 | failed: |
| 1721 | if (cmd) |
| 1722 | mgmt_pending_remove(cmd); |
| 1723 | return err; |
| 1724 | } |