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 | |
Johannes Berg | b5ad8b7 | 2011-06-01 08:54:45 +0200 | [diff] [blame] | 44 | static LIST_HEAD(cmd_list); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 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 | f7b64e69 | 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 | f7b64e69 | 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 | f7b64e69 | 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 | f7b64e69 | 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 | f7b64e69 | 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 | |
Andre Guedes | 8c156c3 | 2011-07-07 10:30:36 -0300 | [diff] [blame] | 182 | hci_dev_lock_bh(hdev); |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 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 | f7b64e69 | 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 | f7b64e69 | 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 | f7b64e69 | 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 | f7b64e69 | 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 | f7b64e69 | 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 | f7b64e69 | 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 | |
Andre Guedes | 8c156c3 | 2011-07-07 10:30:36 -0300 | [diff] [blame] | 211 | hci_dev_unlock_bh(hdev); |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 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 | 8fce635 | 2011-03-22 13:12:20 +0100 | [diff] [blame] | 242 | if (data) |
| 243 | memcpy(cmd->param, data, len); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 244 | |
| 245 | cmd->sk = sk; |
| 246 | sock_hold(sk); |
| 247 | |
| 248 | list_add(&cmd->list, &cmd_list); |
| 249 | |
Johan Hedberg | 366a033 | 2011-02-19 12:05:55 -0300 | [diff] [blame] | 250 | return cmd; |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 251 | } |
| 252 | |
| 253 | static void mgmt_pending_foreach(u16 opcode, int index, |
| 254 | void (*cb)(struct pending_cmd *cmd, void *data), |
| 255 | void *data) |
| 256 | { |
| 257 | struct list_head *p, *n; |
| 258 | |
| 259 | list_for_each_safe(p, n, &cmd_list) { |
| 260 | struct pending_cmd *cmd; |
| 261 | |
| 262 | cmd = list_entry(p, struct pending_cmd, list); |
| 263 | |
| 264 | if (cmd->opcode != opcode) |
| 265 | continue; |
| 266 | |
| 267 | if (index >= 0 && cmd->index != index) |
| 268 | continue; |
| 269 | |
| 270 | cb(cmd, data); |
| 271 | } |
| 272 | } |
| 273 | |
| 274 | static struct pending_cmd *mgmt_pending_find(u16 opcode, int index) |
| 275 | { |
| 276 | struct list_head *p; |
| 277 | |
| 278 | list_for_each(p, &cmd_list) { |
| 279 | struct pending_cmd *cmd; |
| 280 | |
| 281 | cmd = list_entry(p, struct pending_cmd, list); |
| 282 | |
| 283 | if (cmd->opcode != opcode) |
| 284 | continue; |
| 285 | |
| 286 | if (index >= 0 && cmd->index != index) |
| 287 | continue; |
| 288 | |
| 289 | return cmd; |
| 290 | } |
| 291 | |
| 292 | return NULL; |
| 293 | } |
| 294 | |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 295 | static void mgmt_pending_remove(struct pending_cmd *cmd) |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 296 | { |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 297 | list_del(&cmd->list); |
| 298 | mgmt_pending_free(cmd); |
| 299 | } |
| 300 | |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 301 | 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] | 302 | { |
Johan Hedberg | 72a734e | 2010-12-30 00:38:22 +0200 | [diff] [blame] | 303 | struct mgmt_mode *cp; |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 304 | struct hci_dev *hdev; |
Johan Hedberg | 366a033 | 2011-02-19 12:05:55 -0300 | [diff] [blame] | 305 | struct pending_cmd *cmd; |
Johan Hedberg | 366a033 | 2011-02-19 12:05:55 -0300 | [diff] [blame] | 306 | int err, up; |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 307 | |
| 308 | cp = (void *) data; |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 309 | |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 310 | BT_DBG("request for hci%u", index); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 311 | |
Szymon Janc | bdce7ba | 2011-02-25 19:05:49 +0100 | [diff] [blame] | 312 | if (len != sizeof(*cp)) |
| 313 | return cmd_status(sk, index, MGMT_OP_SET_POWERED, EINVAL); |
| 314 | |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 315 | hdev = hci_dev_get(index); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 316 | if (!hdev) |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 317 | return cmd_status(sk, index, MGMT_OP_SET_POWERED, ENODEV); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 318 | |
Andre Guedes | 8c156c3 | 2011-07-07 10:30:36 -0300 | [diff] [blame] | 319 | hci_dev_lock_bh(hdev); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 320 | |
| 321 | up = test_bit(HCI_UP, &hdev->flags); |
Johan Hedberg | 72a734e | 2010-12-30 00:38:22 +0200 | [diff] [blame] | 322 | if ((cp->val && up) || (!cp->val && !up)) { |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 323 | err = cmd_status(sk, index, MGMT_OP_SET_POWERED, EALREADY); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 324 | goto failed; |
| 325 | } |
| 326 | |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 327 | if (mgmt_pending_find(MGMT_OP_SET_POWERED, index)) { |
| 328 | err = cmd_status(sk, index, MGMT_OP_SET_POWERED, EBUSY); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 329 | goto failed; |
| 330 | } |
| 331 | |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 332 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_POWERED, index, data, len); |
Johan Hedberg | 366a033 | 2011-02-19 12:05:55 -0300 | [diff] [blame] | 333 | if (!cmd) { |
| 334 | err = -ENOMEM; |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 335 | goto failed; |
Johan Hedberg | 366a033 | 2011-02-19 12:05:55 -0300 | [diff] [blame] | 336 | } |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 337 | |
Johan Hedberg | 72a734e | 2010-12-30 00:38:22 +0200 | [diff] [blame] | 338 | if (cp->val) |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 339 | queue_work(hdev->workqueue, &hdev->power_on); |
| 340 | else |
| 341 | queue_work(hdev->workqueue, &hdev->power_off); |
| 342 | |
Johan Hedberg | 366a033 | 2011-02-19 12:05:55 -0300 | [diff] [blame] | 343 | err = 0; |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 344 | |
| 345 | failed: |
Andre Guedes | 8c156c3 | 2011-07-07 10:30:36 -0300 | [diff] [blame] | 346 | hci_dev_unlock_bh(hdev); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 347 | hci_dev_put(hdev); |
Johan Hedberg | 366a033 | 2011-02-19 12:05:55 -0300 | [diff] [blame] | 348 | return err; |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 349 | } |
| 350 | |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 351 | static int set_discoverable(struct sock *sk, u16 index, unsigned char *data, |
| 352 | u16 len) |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 353 | { |
Johan Hedberg | 72a734e | 2010-12-30 00:38:22 +0200 | [diff] [blame] | 354 | struct mgmt_mode *cp; |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 355 | struct hci_dev *hdev; |
Johan Hedberg | 366a033 | 2011-02-19 12:05:55 -0300 | [diff] [blame] | 356 | struct pending_cmd *cmd; |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 357 | u8 scan; |
| 358 | int err; |
| 359 | |
| 360 | cp = (void *) data; |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 361 | |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 362 | BT_DBG("request for hci%u", index); |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 363 | |
Szymon Janc | bdce7ba | 2011-02-25 19:05:49 +0100 | [diff] [blame] | 364 | if (len != sizeof(*cp)) |
| 365 | return cmd_status(sk, index, MGMT_OP_SET_DISCOVERABLE, EINVAL); |
| 366 | |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 367 | hdev = hci_dev_get(index); |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 368 | if (!hdev) |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 369 | return cmd_status(sk, index, MGMT_OP_SET_DISCOVERABLE, ENODEV); |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 370 | |
Andre Guedes | 8c156c3 | 2011-07-07 10:30:36 -0300 | [diff] [blame] | 371 | hci_dev_lock_bh(hdev); |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 372 | |
| 373 | if (!test_bit(HCI_UP, &hdev->flags)) { |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 374 | err = cmd_status(sk, index, MGMT_OP_SET_DISCOVERABLE, ENETDOWN); |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 375 | goto failed; |
| 376 | } |
| 377 | |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 378 | if (mgmt_pending_find(MGMT_OP_SET_DISCOVERABLE, index) || |
| 379 | mgmt_pending_find(MGMT_OP_SET_CONNECTABLE, index)) { |
| 380 | err = cmd_status(sk, index, MGMT_OP_SET_DISCOVERABLE, EBUSY); |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 381 | goto failed; |
| 382 | } |
| 383 | |
Johan Hedberg | 72a734e | 2010-12-30 00:38:22 +0200 | [diff] [blame] | 384 | if (cp->val == test_bit(HCI_ISCAN, &hdev->flags) && |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 385 | test_bit(HCI_PSCAN, &hdev->flags)) { |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 386 | err = cmd_status(sk, index, MGMT_OP_SET_DISCOVERABLE, EALREADY); |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 387 | goto failed; |
| 388 | } |
| 389 | |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 390 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_DISCOVERABLE, index, data, len); |
Johan Hedberg | 366a033 | 2011-02-19 12:05:55 -0300 | [diff] [blame] | 391 | if (!cmd) { |
| 392 | err = -ENOMEM; |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 393 | goto failed; |
Johan Hedberg | 366a033 | 2011-02-19 12:05:55 -0300 | [diff] [blame] | 394 | } |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 395 | |
| 396 | scan = SCAN_PAGE; |
| 397 | |
Johan Hedberg | 72a734e | 2010-12-30 00:38:22 +0200 | [diff] [blame] | 398 | if (cp->val) |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 399 | scan |= SCAN_INQUIRY; |
| 400 | |
| 401 | err = hci_send_cmd(hdev, HCI_OP_WRITE_SCAN_ENABLE, 1, &scan); |
| 402 | if (err < 0) |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 403 | mgmt_pending_remove(cmd); |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 404 | |
| 405 | failed: |
Andre Guedes | 8c156c3 | 2011-07-07 10:30:36 -0300 | [diff] [blame] | 406 | hci_dev_unlock_bh(hdev); |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 407 | hci_dev_put(hdev); |
| 408 | |
| 409 | return err; |
| 410 | } |
| 411 | |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 412 | static int set_connectable(struct sock *sk, u16 index, unsigned char *data, |
| 413 | u16 len) |
Johan Hedberg | 9fbcbb4 | 2010-12-30 00:18:33 +0200 | [diff] [blame] | 414 | { |
Johan Hedberg | 72a734e | 2010-12-30 00:38:22 +0200 | [diff] [blame] | 415 | struct mgmt_mode *cp; |
Johan Hedberg | 9fbcbb4 | 2010-12-30 00:18:33 +0200 | [diff] [blame] | 416 | struct hci_dev *hdev; |
Johan Hedberg | 366a033 | 2011-02-19 12:05:55 -0300 | [diff] [blame] | 417 | struct pending_cmd *cmd; |
Johan Hedberg | 9fbcbb4 | 2010-12-30 00:18:33 +0200 | [diff] [blame] | 418 | u8 scan; |
| 419 | int err; |
| 420 | |
| 421 | cp = (void *) data; |
Johan Hedberg | 9fbcbb4 | 2010-12-30 00:18:33 +0200 | [diff] [blame] | 422 | |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 423 | BT_DBG("request for hci%u", index); |
Johan Hedberg | 9fbcbb4 | 2010-12-30 00:18:33 +0200 | [diff] [blame] | 424 | |
Szymon Janc | bdce7ba | 2011-02-25 19:05:49 +0100 | [diff] [blame] | 425 | if (len != sizeof(*cp)) |
| 426 | return cmd_status(sk, index, MGMT_OP_SET_CONNECTABLE, EINVAL); |
| 427 | |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 428 | hdev = hci_dev_get(index); |
Johan Hedberg | 9fbcbb4 | 2010-12-30 00:18:33 +0200 | [diff] [blame] | 429 | if (!hdev) |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 430 | return cmd_status(sk, index, MGMT_OP_SET_CONNECTABLE, ENODEV); |
Johan Hedberg | 9fbcbb4 | 2010-12-30 00:18:33 +0200 | [diff] [blame] | 431 | |
Andre Guedes | 8c156c3 | 2011-07-07 10:30:36 -0300 | [diff] [blame] | 432 | hci_dev_lock_bh(hdev); |
Johan Hedberg | 9fbcbb4 | 2010-12-30 00:18:33 +0200 | [diff] [blame] | 433 | |
| 434 | if (!test_bit(HCI_UP, &hdev->flags)) { |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 435 | err = cmd_status(sk, index, MGMT_OP_SET_CONNECTABLE, ENETDOWN); |
Johan Hedberg | 9fbcbb4 | 2010-12-30 00:18:33 +0200 | [diff] [blame] | 436 | goto failed; |
| 437 | } |
| 438 | |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 439 | if (mgmt_pending_find(MGMT_OP_SET_DISCOVERABLE, index) || |
| 440 | mgmt_pending_find(MGMT_OP_SET_CONNECTABLE, index)) { |
| 441 | err = cmd_status(sk, index, MGMT_OP_SET_CONNECTABLE, EBUSY); |
Johan Hedberg | 9fbcbb4 | 2010-12-30 00:18:33 +0200 | [diff] [blame] | 442 | goto failed; |
| 443 | } |
| 444 | |
Johan Hedberg | 72a734e | 2010-12-30 00:38:22 +0200 | [diff] [blame] | 445 | if (cp->val == test_bit(HCI_PSCAN, &hdev->flags)) { |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 446 | err = cmd_status(sk, index, MGMT_OP_SET_CONNECTABLE, EALREADY); |
Johan Hedberg | 9fbcbb4 | 2010-12-30 00:18:33 +0200 | [diff] [blame] | 447 | goto failed; |
| 448 | } |
| 449 | |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 450 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_CONNECTABLE, index, data, len); |
Johan Hedberg | 366a033 | 2011-02-19 12:05:55 -0300 | [diff] [blame] | 451 | if (!cmd) { |
| 452 | err = -ENOMEM; |
Johan Hedberg | 9fbcbb4 | 2010-12-30 00:18:33 +0200 | [diff] [blame] | 453 | goto failed; |
Johan Hedberg | 366a033 | 2011-02-19 12:05:55 -0300 | [diff] [blame] | 454 | } |
Johan Hedberg | 9fbcbb4 | 2010-12-30 00:18:33 +0200 | [diff] [blame] | 455 | |
Johan Hedberg | 72a734e | 2010-12-30 00:38:22 +0200 | [diff] [blame] | 456 | if (cp->val) |
Johan Hedberg | 9fbcbb4 | 2010-12-30 00:18:33 +0200 | [diff] [blame] | 457 | scan = SCAN_PAGE; |
| 458 | else |
| 459 | scan = 0; |
| 460 | |
| 461 | err = hci_send_cmd(hdev, HCI_OP_WRITE_SCAN_ENABLE, 1, &scan); |
| 462 | if (err < 0) |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 463 | mgmt_pending_remove(cmd); |
Johan Hedberg | 9fbcbb4 | 2010-12-30 00:18:33 +0200 | [diff] [blame] | 464 | |
| 465 | failed: |
Andre Guedes | 8c156c3 | 2011-07-07 10:30:36 -0300 | [diff] [blame] | 466 | hci_dev_unlock_bh(hdev); |
Johan Hedberg | 9fbcbb4 | 2010-12-30 00:18:33 +0200 | [diff] [blame] | 467 | hci_dev_put(hdev); |
| 468 | |
| 469 | return err; |
| 470 | } |
| 471 | |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 472 | static int mgmt_event(u16 event, u16 index, void *data, u16 data_len, |
| 473 | struct sock *skip_sk) |
Johan Hedberg | c542a06 | 2011-01-26 13:11:03 +0200 | [diff] [blame] | 474 | { |
| 475 | struct sk_buff *skb; |
| 476 | struct mgmt_hdr *hdr; |
| 477 | |
| 478 | skb = alloc_skb(sizeof(*hdr) + data_len, GFP_ATOMIC); |
| 479 | if (!skb) |
| 480 | return -ENOMEM; |
| 481 | |
| 482 | bt_cb(skb)->channel = HCI_CHANNEL_CONTROL; |
| 483 | |
| 484 | hdr = (void *) skb_put(skb, sizeof(*hdr)); |
| 485 | hdr->opcode = cpu_to_le16(event); |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 486 | hdr->index = cpu_to_le16(index); |
Johan Hedberg | c542a06 | 2011-01-26 13:11:03 +0200 | [diff] [blame] | 487 | hdr->len = cpu_to_le16(data_len); |
| 488 | |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 489 | if (data) |
| 490 | memcpy(skb_put(skb, data_len), data, data_len); |
Johan Hedberg | c542a06 | 2011-01-26 13:11:03 +0200 | [diff] [blame] | 491 | |
| 492 | hci_send_to_sock(NULL, skb, skip_sk); |
| 493 | kfree_skb(skb); |
| 494 | |
| 495 | return 0; |
| 496 | } |
| 497 | |
Johan Hedberg | 053f021 | 2011-01-26 13:07:10 +0200 | [diff] [blame] | 498 | static int send_mode_rsp(struct sock *sk, u16 opcode, u16 index, u8 val) |
| 499 | { |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 500 | struct mgmt_mode rp; |
Johan Hedberg | 053f021 | 2011-01-26 13:07:10 +0200 | [diff] [blame] | 501 | |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 502 | rp.val = val; |
Johan Hedberg | 053f021 | 2011-01-26 13:07:10 +0200 | [diff] [blame] | 503 | |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 504 | return cmd_complete(sk, index, opcode, &rp, sizeof(rp)); |
Johan Hedberg | 053f021 | 2011-01-26 13:07:10 +0200 | [diff] [blame] | 505 | } |
| 506 | |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 507 | static int set_pairable(struct sock *sk, u16 index, unsigned char *data, |
| 508 | u16 len) |
Johan Hedberg | c542a06 | 2011-01-26 13:11:03 +0200 | [diff] [blame] | 509 | { |
| 510 | struct mgmt_mode *cp, ev; |
| 511 | struct hci_dev *hdev; |
Johan Hedberg | c542a06 | 2011-01-26 13:11:03 +0200 | [diff] [blame] | 512 | int err; |
| 513 | |
| 514 | cp = (void *) data; |
Johan Hedberg | c542a06 | 2011-01-26 13:11:03 +0200 | [diff] [blame] | 515 | |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 516 | BT_DBG("request for hci%u", index); |
Johan Hedberg | c542a06 | 2011-01-26 13:11:03 +0200 | [diff] [blame] | 517 | |
Szymon Janc | bdce7ba | 2011-02-25 19:05:49 +0100 | [diff] [blame] | 518 | if (len != sizeof(*cp)) |
| 519 | return cmd_status(sk, index, MGMT_OP_SET_PAIRABLE, EINVAL); |
| 520 | |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 521 | hdev = hci_dev_get(index); |
Johan Hedberg | c542a06 | 2011-01-26 13:11:03 +0200 | [diff] [blame] | 522 | if (!hdev) |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 523 | return cmd_status(sk, index, MGMT_OP_SET_PAIRABLE, ENODEV); |
Johan Hedberg | c542a06 | 2011-01-26 13:11:03 +0200 | [diff] [blame] | 524 | |
Andre Guedes | 8c156c3 | 2011-07-07 10:30:36 -0300 | [diff] [blame] | 525 | hci_dev_lock_bh(hdev); |
Johan Hedberg | c542a06 | 2011-01-26 13:11:03 +0200 | [diff] [blame] | 526 | |
| 527 | if (cp->val) |
| 528 | set_bit(HCI_PAIRABLE, &hdev->flags); |
| 529 | else |
| 530 | clear_bit(HCI_PAIRABLE, &hdev->flags); |
| 531 | |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 532 | err = send_mode_rsp(sk, MGMT_OP_SET_PAIRABLE, index, cp->val); |
Johan Hedberg | c542a06 | 2011-01-26 13:11:03 +0200 | [diff] [blame] | 533 | if (err < 0) |
| 534 | goto failed; |
| 535 | |
Johan Hedberg | c542a06 | 2011-01-26 13:11:03 +0200 | [diff] [blame] | 536 | ev.val = cp->val; |
| 537 | |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 538 | err = mgmt_event(MGMT_EV_PAIRABLE, index, &ev, sizeof(ev), sk); |
Johan Hedberg | c542a06 | 2011-01-26 13:11:03 +0200 | [diff] [blame] | 539 | |
| 540 | failed: |
Andre Guedes | 8c156c3 | 2011-07-07 10:30:36 -0300 | [diff] [blame] | 541 | hci_dev_unlock_bh(hdev); |
Johan Hedberg | c542a06 | 2011-01-26 13:11:03 +0200 | [diff] [blame] | 542 | hci_dev_put(hdev); |
| 543 | |
| 544 | return err; |
| 545 | } |
| 546 | |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 547 | #define EIR_FLAGS 0x01 /* flags */ |
| 548 | #define EIR_UUID16_SOME 0x02 /* 16-bit UUID, more available */ |
| 549 | #define EIR_UUID16_ALL 0x03 /* 16-bit UUID, all listed */ |
| 550 | #define EIR_UUID32_SOME 0x04 /* 32-bit UUID, more available */ |
| 551 | #define EIR_UUID32_ALL 0x05 /* 32-bit UUID, all listed */ |
| 552 | #define EIR_UUID128_SOME 0x06 /* 128-bit UUID, more available */ |
| 553 | #define EIR_UUID128_ALL 0x07 /* 128-bit UUID, all listed */ |
| 554 | #define EIR_NAME_SHORT 0x08 /* shortened local name */ |
| 555 | #define EIR_NAME_COMPLETE 0x09 /* complete local name */ |
| 556 | #define EIR_TX_POWER 0x0A /* transmit power level */ |
| 557 | #define EIR_DEVICE_ID 0x10 /* device ID */ |
| 558 | |
| 559 | #define PNP_INFO_SVCLASS_ID 0x1200 |
| 560 | |
| 561 | static u8 bluetooth_base_uuid[] = { |
| 562 | 0xFB, 0x34, 0x9B, 0x5F, 0x80, 0x00, 0x00, 0x80, |
| 563 | 0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 564 | }; |
| 565 | |
| 566 | static u16 get_uuid16(u8 *uuid128) |
| 567 | { |
| 568 | u32 val; |
| 569 | int i; |
| 570 | |
| 571 | for (i = 0; i < 12; i++) { |
| 572 | if (bluetooth_base_uuid[i] != uuid128[i]) |
| 573 | return 0; |
| 574 | } |
| 575 | |
| 576 | memcpy(&val, &uuid128[12], 4); |
| 577 | |
| 578 | val = le32_to_cpu(val); |
| 579 | if (val > 0xffff) |
| 580 | return 0; |
| 581 | |
| 582 | return (u16) val; |
| 583 | } |
| 584 | |
| 585 | static void create_eir(struct hci_dev *hdev, u8 *data) |
| 586 | { |
| 587 | u8 *ptr = data; |
| 588 | u16 eir_len = 0; |
| 589 | u16 uuid16_list[HCI_MAX_EIR_LENGTH / sizeof(u16)]; |
| 590 | int i, truncated = 0; |
| 591 | struct list_head *p; |
| 592 | size_t name_len; |
| 593 | |
| 594 | name_len = strlen(hdev->dev_name); |
| 595 | |
| 596 | if (name_len > 0) { |
| 597 | /* EIR Data type */ |
| 598 | if (name_len > 48) { |
| 599 | name_len = 48; |
| 600 | ptr[1] = EIR_NAME_SHORT; |
| 601 | } else |
| 602 | ptr[1] = EIR_NAME_COMPLETE; |
| 603 | |
| 604 | /* EIR Data length */ |
| 605 | ptr[0] = name_len + 1; |
| 606 | |
| 607 | memcpy(ptr + 2, hdev->dev_name, name_len); |
| 608 | |
| 609 | eir_len += (name_len + 2); |
| 610 | ptr += (name_len + 2); |
| 611 | } |
| 612 | |
| 613 | memset(uuid16_list, 0, sizeof(uuid16_list)); |
| 614 | |
| 615 | /* Group all UUID16 types */ |
| 616 | list_for_each(p, &hdev->uuids) { |
| 617 | struct bt_uuid *uuid = list_entry(p, struct bt_uuid, list); |
| 618 | u16 uuid16; |
| 619 | |
| 620 | uuid16 = get_uuid16(uuid->uuid); |
| 621 | if (uuid16 == 0) |
| 622 | return; |
| 623 | |
| 624 | if (uuid16 < 0x1100) |
| 625 | continue; |
| 626 | |
| 627 | if (uuid16 == PNP_INFO_SVCLASS_ID) |
| 628 | continue; |
| 629 | |
| 630 | /* Stop if not enough space to put next UUID */ |
| 631 | if (eir_len + 2 + sizeof(u16) > HCI_MAX_EIR_LENGTH) { |
| 632 | truncated = 1; |
| 633 | break; |
| 634 | } |
| 635 | |
| 636 | /* Check for duplicates */ |
| 637 | for (i = 0; uuid16_list[i] != 0; i++) |
| 638 | if (uuid16_list[i] == uuid16) |
| 639 | break; |
| 640 | |
| 641 | if (uuid16_list[i] == 0) { |
| 642 | uuid16_list[i] = uuid16; |
| 643 | eir_len += sizeof(u16); |
| 644 | } |
| 645 | } |
| 646 | |
| 647 | if (uuid16_list[0] != 0) { |
| 648 | u8 *length = ptr; |
| 649 | |
| 650 | /* EIR Data type */ |
| 651 | ptr[1] = truncated ? EIR_UUID16_SOME : EIR_UUID16_ALL; |
| 652 | |
| 653 | ptr += 2; |
| 654 | eir_len += 2; |
| 655 | |
| 656 | for (i = 0; uuid16_list[i] != 0; i++) { |
| 657 | *ptr++ = (uuid16_list[i] & 0x00ff); |
| 658 | *ptr++ = (uuid16_list[i] & 0xff00) >> 8; |
| 659 | } |
| 660 | |
| 661 | /* EIR Data length */ |
| 662 | *length = (i * sizeof(u16)) + 1; |
| 663 | } |
| 664 | } |
| 665 | |
| 666 | static int update_eir(struct hci_dev *hdev) |
| 667 | { |
| 668 | struct hci_cp_write_eir cp; |
| 669 | |
| 670 | if (!(hdev->features[6] & LMP_EXT_INQ)) |
| 671 | return 0; |
| 672 | |
| 673 | if (hdev->ssp_mode == 0) |
| 674 | return 0; |
| 675 | |
| 676 | if (test_bit(HCI_SERVICE_CACHE, &hdev->flags)) |
| 677 | return 0; |
| 678 | |
| 679 | memset(&cp, 0, sizeof(cp)); |
| 680 | |
| 681 | create_eir(hdev, cp.data); |
| 682 | |
| 683 | if (memcmp(cp.data, hdev->eir, sizeof(cp.data)) == 0) |
| 684 | return 0; |
| 685 | |
| 686 | memcpy(hdev->eir, cp.data, sizeof(cp.data)); |
| 687 | |
| 688 | return hci_send_cmd(hdev, HCI_OP_WRITE_EIR, sizeof(cp), &cp); |
| 689 | } |
| 690 | |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 691 | static u8 get_service_classes(struct hci_dev *hdev) |
| 692 | { |
| 693 | struct list_head *p; |
| 694 | u8 val = 0; |
| 695 | |
| 696 | list_for_each(p, &hdev->uuids) { |
| 697 | struct bt_uuid *uuid = list_entry(p, struct bt_uuid, list); |
| 698 | |
| 699 | val |= uuid->svc_hint; |
| 700 | } |
| 701 | |
| 702 | return val; |
| 703 | } |
| 704 | |
| 705 | static int update_class(struct hci_dev *hdev) |
| 706 | { |
| 707 | u8 cod[3]; |
| 708 | |
| 709 | BT_DBG("%s", hdev->name); |
| 710 | |
| 711 | if (test_bit(HCI_SERVICE_CACHE, &hdev->flags)) |
| 712 | return 0; |
| 713 | |
| 714 | cod[0] = hdev->minor_class; |
| 715 | cod[1] = hdev->major_class; |
| 716 | cod[2] = get_service_classes(hdev); |
| 717 | |
| 718 | if (memcmp(cod, hdev->dev_class, 3) == 0) |
| 719 | return 0; |
| 720 | |
| 721 | return hci_send_cmd(hdev, HCI_OP_WRITE_CLASS_OF_DEV, sizeof(cod), cod); |
| 722 | } |
| 723 | |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 724 | 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] | 725 | { |
| 726 | struct mgmt_cp_add_uuid *cp; |
| 727 | struct hci_dev *hdev; |
| 728 | struct bt_uuid *uuid; |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 729 | int err; |
| 730 | |
| 731 | cp = (void *) data; |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 732 | |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 733 | BT_DBG("request for hci%u", index); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 734 | |
Szymon Janc | bdce7ba | 2011-02-25 19:05:49 +0100 | [diff] [blame] | 735 | if (len != sizeof(*cp)) |
| 736 | return cmd_status(sk, index, MGMT_OP_ADD_UUID, EINVAL); |
| 737 | |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 738 | hdev = hci_dev_get(index); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 739 | if (!hdev) |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 740 | return cmd_status(sk, index, MGMT_OP_ADD_UUID, ENODEV); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 741 | |
Andre Guedes | 8c156c3 | 2011-07-07 10:30:36 -0300 | [diff] [blame] | 742 | hci_dev_lock_bh(hdev); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 743 | |
| 744 | uuid = kmalloc(sizeof(*uuid), GFP_ATOMIC); |
| 745 | if (!uuid) { |
| 746 | err = -ENOMEM; |
| 747 | goto failed; |
| 748 | } |
| 749 | |
| 750 | memcpy(uuid->uuid, cp->uuid, 16); |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 751 | uuid->svc_hint = cp->svc_hint; |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 752 | |
| 753 | list_add(&uuid->list, &hdev->uuids); |
| 754 | |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 755 | err = update_class(hdev); |
| 756 | if (err < 0) |
| 757 | goto failed; |
| 758 | |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 759 | err = update_eir(hdev); |
| 760 | if (err < 0) |
| 761 | goto failed; |
| 762 | |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 763 | err = cmd_complete(sk, index, MGMT_OP_ADD_UUID, NULL, 0); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 764 | |
| 765 | failed: |
Andre Guedes | 8c156c3 | 2011-07-07 10:30:36 -0300 | [diff] [blame] | 766 | hci_dev_unlock_bh(hdev); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 767 | hci_dev_put(hdev); |
| 768 | |
| 769 | return err; |
| 770 | } |
| 771 | |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 772 | 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] | 773 | { |
| 774 | struct list_head *p, *n; |
Szymon Janc | 779cb85 | 2011-02-25 19:05:47 +0100 | [diff] [blame] | 775 | struct mgmt_cp_remove_uuid *cp; |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 776 | struct hci_dev *hdev; |
| 777 | 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] | 778 | int err, found; |
| 779 | |
| 780 | cp = (void *) data; |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 781 | |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 782 | BT_DBG("request for hci%u", index); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 783 | |
Szymon Janc | bdce7ba | 2011-02-25 19:05:49 +0100 | [diff] [blame] | 784 | if (len != sizeof(*cp)) |
| 785 | return cmd_status(sk, index, MGMT_OP_REMOVE_UUID, EINVAL); |
| 786 | |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 787 | hdev = hci_dev_get(index); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 788 | if (!hdev) |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 789 | return cmd_status(sk, index, MGMT_OP_REMOVE_UUID, ENODEV); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 790 | |
Andre Guedes | 8c156c3 | 2011-07-07 10:30:36 -0300 | [diff] [blame] | 791 | hci_dev_lock_bh(hdev); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 792 | |
| 793 | if (memcmp(cp->uuid, bt_uuid_any, 16) == 0) { |
| 794 | err = hci_uuids_clear(hdev); |
| 795 | goto unlock; |
| 796 | } |
| 797 | |
| 798 | found = 0; |
| 799 | |
| 800 | list_for_each_safe(p, n, &hdev->uuids) { |
| 801 | struct bt_uuid *match = list_entry(p, struct bt_uuid, list); |
| 802 | |
| 803 | if (memcmp(match->uuid, cp->uuid, 16) != 0) |
| 804 | continue; |
| 805 | |
| 806 | list_del(&match->list); |
| 807 | found++; |
| 808 | } |
| 809 | |
| 810 | if (found == 0) { |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 811 | err = cmd_status(sk, index, MGMT_OP_REMOVE_UUID, ENOENT); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 812 | goto unlock; |
| 813 | } |
| 814 | |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 815 | err = update_class(hdev); |
| 816 | if (err < 0) |
| 817 | goto unlock; |
| 818 | |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 819 | err = update_eir(hdev); |
| 820 | if (err < 0) |
| 821 | goto unlock; |
| 822 | |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 823 | err = cmd_complete(sk, index, MGMT_OP_REMOVE_UUID, NULL, 0); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 824 | |
| 825 | unlock: |
Andre Guedes | 8c156c3 | 2011-07-07 10:30:36 -0300 | [diff] [blame] | 826 | hci_dev_unlock_bh(hdev); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 827 | hci_dev_put(hdev); |
| 828 | |
| 829 | return err; |
| 830 | } |
| 831 | |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 832 | static int set_dev_class(struct sock *sk, u16 index, unsigned char *data, |
| 833 | u16 len) |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 834 | { |
| 835 | struct hci_dev *hdev; |
| 836 | struct mgmt_cp_set_dev_class *cp; |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 837 | int err; |
| 838 | |
| 839 | cp = (void *) data; |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 840 | |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 841 | BT_DBG("request for hci%u", index); |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 842 | |
Szymon Janc | bdce7ba | 2011-02-25 19:05:49 +0100 | [diff] [blame] | 843 | if (len != sizeof(*cp)) |
| 844 | return cmd_status(sk, index, MGMT_OP_SET_DEV_CLASS, EINVAL); |
| 845 | |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 846 | hdev = hci_dev_get(index); |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 847 | if (!hdev) |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 848 | return cmd_status(sk, index, MGMT_OP_SET_DEV_CLASS, ENODEV); |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 849 | |
Andre Guedes | 8c156c3 | 2011-07-07 10:30:36 -0300 | [diff] [blame] | 850 | hci_dev_lock_bh(hdev); |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 851 | |
| 852 | hdev->major_class = cp->major; |
| 853 | hdev->minor_class = cp->minor; |
| 854 | |
| 855 | err = update_class(hdev); |
| 856 | |
| 857 | if (err == 0) |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 858 | err = cmd_complete(sk, index, MGMT_OP_SET_DEV_CLASS, NULL, 0); |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 859 | |
Andre Guedes | 8c156c3 | 2011-07-07 10:30:36 -0300 | [diff] [blame] | 860 | hci_dev_unlock_bh(hdev); |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 861 | hci_dev_put(hdev); |
| 862 | |
| 863 | return err; |
| 864 | } |
| 865 | |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 866 | static int set_service_cache(struct sock *sk, u16 index, unsigned char *data, |
| 867 | u16 len) |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 868 | { |
| 869 | struct hci_dev *hdev; |
| 870 | struct mgmt_cp_set_service_cache *cp; |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 871 | int err; |
| 872 | |
| 873 | cp = (void *) data; |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 874 | |
Szymon Janc | bdce7ba | 2011-02-25 19:05:49 +0100 | [diff] [blame] | 875 | if (len != sizeof(*cp)) |
Szymon Janc | b8534e0 | 2011-03-01 16:55:34 +0100 | [diff] [blame] | 876 | return cmd_status(sk, index, MGMT_OP_SET_SERVICE_CACHE, EINVAL); |
Szymon Janc | bdce7ba | 2011-02-25 19:05:49 +0100 | [diff] [blame] | 877 | |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 878 | hdev = hci_dev_get(index); |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 879 | if (!hdev) |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 880 | return cmd_status(sk, index, MGMT_OP_SET_SERVICE_CACHE, ENODEV); |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 881 | |
Andre Guedes | 8c156c3 | 2011-07-07 10:30:36 -0300 | [diff] [blame] | 882 | hci_dev_lock_bh(hdev); |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 883 | |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 884 | BT_DBG("hci%u enable %d", index, cp->enable); |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 885 | |
| 886 | if (cp->enable) { |
| 887 | set_bit(HCI_SERVICE_CACHE, &hdev->flags); |
| 888 | err = 0; |
| 889 | } else { |
| 890 | clear_bit(HCI_SERVICE_CACHE, &hdev->flags); |
| 891 | err = update_class(hdev); |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 892 | if (err == 0) |
| 893 | err = update_eir(hdev); |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 894 | } |
| 895 | |
| 896 | if (err == 0) |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 897 | err = cmd_complete(sk, index, MGMT_OP_SET_SERVICE_CACHE, NULL, |
| 898 | 0); |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 899 | |
Andre Guedes | 8c156c3 | 2011-07-07 10:30:36 -0300 | [diff] [blame] | 900 | hci_dev_unlock_bh(hdev); |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 901 | hci_dev_put(hdev); |
| 902 | |
| 903 | return err; |
| 904 | } |
| 905 | |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 906 | static int load_keys(struct sock *sk, u16 index, unsigned char *data, u16 len) |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 907 | { |
| 908 | struct hci_dev *hdev; |
| 909 | struct mgmt_cp_load_keys *cp; |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 910 | u16 key_count, expected_len; |
Vinicius Costa Gomes | 5a0a8b4 | 2011-07-08 18:31:44 -0300 | [diff] [blame] | 911 | int i, err; |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 912 | |
| 913 | cp = (void *) data; |
Szymon Janc | bdce7ba | 2011-02-25 19:05:49 +0100 | [diff] [blame] | 914 | |
| 915 | if (len < sizeof(*cp)) |
| 916 | return -EINVAL; |
| 917 | |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 918 | key_count = get_unaligned_le16(&cp->key_count); |
| 919 | |
| 920 | expected_len = sizeof(*cp) + key_count * sizeof(struct mgmt_key_info); |
Vinicius Costa Gomes | 5a0a8b4 | 2011-07-08 18:31:44 -0300 | [diff] [blame] | 921 | if (expected_len > len) { |
| 922 | BT_ERR("load_keys: expected at least %u bytes, got %u bytes", |
| 923 | expected_len, len); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 924 | return -EINVAL; |
| 925 | } |
| 926 | |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 927 | hdev = hci_dev_get(index); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 928 | if (!hdev) |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 929 | return cmd_status(sk, index, MGMT_OP_LOAD_KEYS, ENODEV); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 930 | |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 931 | BT_DBG("hci%u debug_keys %u key_count %u", index, cp->debug_keys, |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 932 | key_count); |
| 933 | |
Andre Guedes | 8c156c3 | 2011-07-07 10:30:36 -0300 | [diff] [blame] | 934 | hci_dev_lock_bh(hdev); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 935 | |
| 936 | hci_link_keys_clear(hdev); |
| 937 | |
| 938 | set_bit(HCI_LINK_KEYS, &hdev->flags); |
| 939 | |
| 940 | if (cp->debug_keys) |
| 941 | set_bit(HCI_DEBUG_KEYS, &hdev->flags); |
| 942 | else |
| 943 | clear_bit(HCI_DEBUG_KEYS, &hdev->flags); |
| 944 | |
Vinicius Costa Gomes | 5a0a8b4 | 2011-07-08 18:31:44 -0300 | [diff] [blame] | 945 | len -= sizeof(*cp); |
| 946 | i = 0; |
| 947 | |
| 948 | while (i < len) { |
| 949 | struct mgmt_key_info *key = (void *) cp->keys + i; |
| 950 | |
| 951 | i += sizeof(*key) + key->dlen; |
| 952 | |
| 953 | if (key->type == HCI_LK_SMP_LTK) { |
| 954 | struct key_master_id *id = (void *) key->data; |
| 955 | |
| 956 | if (key->dlen != sizeof(struct key_master_id)) |
| 957 | continue; |
| 958 | |
Vinicius Costa Gomes | 726b4ff | 2011-07-08 18:31:45 -0300 | [diff] [blame] | 959 | hci_add_ltk(hdev, 0, &key->bdaddr, key->pin_len, |
| 960 | id->ediv, id->rand, key->val); |
Vinicius Costa Gomes | 5a0a8b4 | 2011-07-08 18:31:44 -0300 | [diff] [blame] | 961 | |
| 962 | continue; |
| 963 | } |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 964 | |
Johan Hedberg | d25e28a | 2011-04-28 11:28:59 -0700 | [diff] [blame] | 965 | hci_add_link_key(hdev, NULL, 0, &key->bdaddr, key->val, key->type, |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 966 | key->pin_len); |
| 967 | } |
| 968 | |
Vinicius Costa Gomes | 5a0a8b4 | 2011-07-08 18:31:44 -0300 | [diff] [blame] | 969 | err = cmd_complete(sk, index, MGMT_OP_LOAD_KEYS, NULL, 0); |
| 970 | |
Andre Guedes | 8c156c3 | 2011-07-07 10:30:36 -0300 | [diff] [blame] | 971 | hci_dev_unlock_bh(hdev); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 972 | hci_dev_put(hdev); |
| 973 | |
Vinicius Costa Gomes | 5a0a8b4 | 2011-07-08 18:31:44 -0300 | [diff] [blame] | 974 | return err; |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 975 | } |
| 976 | |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 977 | static int remove_key(struct sock *sk, u16 index, unsigned char *data, u16 len) |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 978 | { |
| 979 | struct hci_dev *hdev; |
| 980 | struct mgmt_cp_remove_key *cp; |
| 981 | struct hci_conn *conn; |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 982 | int err; |
| 983 | |
| 984 | cp = (void *) data; |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 985 | |
Szymon Janc | bdce7ba | 2011-02-25 19:05:49 +0100 | [diff] [blame] | 986 | if (len != sizeof(*cp)) |
| 987 | return cmd_status(sk, index, MGMT_OP_REMOVE_KEY, EINVAL); |
| 988 | |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 989 | hdev = hci_dev_get(index); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 990 | if (!hdev) |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 991 | return cmd_status(sk, index, MGMT_OP_REMOVE_KEY, ENODEV); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 992 | |
Andre Guedes | 8c156c3 | 2011-07-07 10:30:36 -0300 | [diff] [blame] | 993 | hci_dev_lock_bh(hdev); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 994 | |
| 995 | err = hci_remove_link_key(hdev, &cp->bdaddr); |
| 996 | if (err < 0) { |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 997 | err = cmd_status(sk, index, MGMT_OP_REMOVE_KEY, -err); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 998 | goto unlock; |
| 999 | } |
| 1000 | |
| 1001 | err = 0; |
| 1002 | |
| 1003 | if (!test_bit(HCI_UP, &hdev->flags) || !cp->disconnect) |
| 1004 | goto unlock; |
| 1005 | |
| 1006 | conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &cp->bdaddr); |
| 1007 | if (conn) { |
| 1008 | struct hci_cp_disconnect dc; |
| 1009 | |
| 1010 | put_unaligned_le16(conn->handle, &dc.handle); |
| 1011 | dc.reason = 0x13; /* Remote User Terminated Connection */ |
Anderson Lizardo | 94ac027 | 2011-06-13 15:42:03 -0400 | [diff] [blame] | 1012 | err = hci_send_cmd(hdev, HCI_OP_DISCONNECT, sizeof(dc), &dc); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 1013 | } |
| 1014 | |
| 1015 | unlock: |
Andre Guedes | 8c156c3 | 2011-07-07 10:30:36 -0300 | [diff] [blame] | 1016 | hci_dev_unlock_bh(hdev); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 1017 | hci_dev_put(hdev); |
| 1018 | |
| 1019 | return err; |
| 1020 | } |
| 1021 | |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 1022 | 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] | 1023 | { |
| 1024 | struct hci_dev *hdev; |
| 1025 | struct mgmt_cp_disconnect *cp; |
| 1026 | struct hci_cp_disconnect dc; |
Johan Hedberg | 366a033 | 2011-02-19 12:05:55 -0300 | [diff] [blame] | 1027 | struct pending_cmd *cmd; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 1028 | struct hci_conn *conn; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 1029 | int err; |
| 1030 | |
| 1031 | BT_DBG(""); |
| 1032 | |
| 1033 | cp = (void *) data; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 1034 | |
Szymon Janc | bdce7ba | 2011-02-25 19:05:49 +0100 | [diff] [blame] | 1035 | if (len != sizeof(*cp)) |
| 1036 | return cmd_status(sk, index, MGMT_OP_DISCONNECT, EINVAL); |
| 1037 | |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 1038 | hdev = hci_dev_get(index); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 1039 | if (!hdev) |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 1040 | return cmd_status(sk, index, MGMT_OP_DISCONNECT, ENODEV); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 1041 | |
Andre Guedes | 8c156c3 | 2011-07-07 10:30:36 -0300 | [diff] [blame] | 1042 | hci_dev_lock_bh(hdev); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 1043 | |
| 1044 | if (!test_bit(HCI_UP, &hdev->flags)) { |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 1045 | err = cmd_status(sk, index, MGMT_OP_DISCONNECT, ENETDOWN); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 1046 | goto failed; |
| 1047 | } |
| 1048 | |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 1049 | if (mgmt_pending_find(MGMT_OP_DISCONNECT, index)) { |
| 1050 | err = cmd_status(sk, index, MGMT_OP_DISCONNECT, EBUSY); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 1051 | goto failed; |
| 1052 | } |
| 1053 | |
| 1054 | conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &cp->bdaddr); |
Vinicius Costa Gomes | 365227e | 2011-05-06 18:41:44 -0300 | [diff] [blame] | 1055 | if (!conn) |
| 1056 | conn = hci_conn_hash_lookup_ba(hdev, LE_LINK, &cp->bdaddr); |
| 1057 | |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 1058 | if (!conn) { |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 1059 | err = cmd_status(sk, index, MGMT_OP_DISCONNECT, ENOTCONN); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 1060 | goto failed; |
| 1061 | } |
| 1062 | |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 1063 | cmd = mgmt_pending_add(sk, MGMT_OP_DISCONNECT, index, data, len); |
Johan Hedberg | 366a033 | 2011-02-19 12:05:55 -0300 | [diff] [blame] | 1064 | if (!cmd) { |
| 1065 | err = -ENOMEM; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 1066 | goto failed; |
Johan Hedberg | 366a033 | 2011-02-19 12:05:55 -0300 | [diff] [blame] | 1067 | } |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 1068 | |
| 1069 | put_unaligned_le16(conn->handle, &dc.handle); |
| 1070 | dc.reason = 0x13; /* Remote User Terminated Connection */ |
| 1071 | |
| 1072 | err = hci_send_cmd(hdev, HCI_OP_DISCONNECT, sizeof(dc), &dc); |
| 1073 | if (err < 0) |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 1074 | mgmt_pending_remove(cmd); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 1075 | |
| 1076 | failed: |
Andre Guedes | 8c156c3 | 2011-07-07 10:30:36 -0300 | [diff] [blame] | 1077 | hci_dev_unlock_bh(hdev); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 1078 | hci_dev_put(hdev); |
| 1079 | |
| 1080 | return err; |
| 1081 | } |
| 1082 | |
Szymon Janc | 8ce6284 | 2011-03-01 16:55:32 +0100 | [diff] [blame] | 1083 | static int get_connections(struct sock *sk, u16 index) |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 1084 | { |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 1085 | struct mgmt_rp_get_connections *rp; |
| 1086 | struct hci_dev *hdev; |
| 1087 | struct list_head *p; |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 1088 | size_t rp_len; |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 1089 | u16 count; |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 1090 | int i, err; |
| 1091 | |
| 1092 | BT_DBG(""); |
| 1093 | |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 1094 | hdev = hci_dev_get(index); |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 1095 | if (!hdev) |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 1096 | return cmd_status(sk, index, MGMT_OP_GET_CONNECTIONS, ENODEV); |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 1097 | |
Andre Guedes | 8c156c3 | 2011-07-07 10:30:36 -0300 | [diff] [blame] | 1098 | hci_dev_lock_bh(hdev); |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 1099 | |
| 1100 | count = 0; |
| 1101 | list_for_each(p, &hdev->conn_hash.list) { |
| 1102 | count++; |
| 1103 | } |
| 1104 | |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 1105 | rp_len = sizeof(*rp) + (count * sizeof(bdaddr_t)); |
| 1106 | rp = kmalloc(rp_len, GFP_ATOMIC); |
| 1107 | if (!rp) { |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 1108 | err = -ENOMEM; |
| 1109 | goto unlock; |
| 1110 | } |
| 1111 | |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 1112 | put_unaligned_le16(count, &rp->conn_count); |
| 1113 | |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 1114 | i = 0; |
| 1115 | list_for_each(p, &hdev->conn_hash.list) { |
| 1116 | struct hci_conn *c = list_entry(p, struct hci_conn, list); |
| 1117 | |
| 1118 | bacpy(&rp->conn[i++], &c->dst); |
| 1119 | } |
| 1120 | |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 1121 | err = cmd_complete(sk, index, MGMT_OP_GET_CONNECTIONS, rp, rp_len); |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 1122 | |
| 1123 | unlock: |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 1124 | kfree(rp); |
Andre Guedes | 8c156c3 | 2011-07-07 10:30:36 -0300 | [diff] [blame] | 1125 | hci_dev_unlock_bh(hdev); |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 1126 | hci_dev_put(hdev); |
| 1127 | return err; |
| 1128 | } |
| 1129 | |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 1130 | static int send_pin_code_neg_reply(struct sock *sk, u16 index, |
| 1131 | struct hci_dev *hdev, struct mgmt_cp_pin_code_neg_reply *cp) |
| 1132 | { |
| 1133 | struct pending_cmd *cmd; |
| 1134 | int err; |
| 1135 | |
| 1136 | cmd = mgmt_pending_add(sk, MGMT_OP_PIN_CODE_NEG_REPLY, index, cp, |
| 1137 | sizeof(*cp)); |
| 1138 | if (!cmd) |
| 1139 | return -ENOMEM; |
| 1140 | |
| 1141 | err = hci_send_cmd(hdev, HCI_OP_PIN_CODE_NEG_REPLY, sizeof(cp->bdaddr), |
| 1142 | &cp->bdaddr); |
| 1143 | if (err < 0) |
| 1144 | mgmt_pending_remove(cmd); |
| 1145 | |
| 1146 | return err; |
| 1147 | } |
| 1148 | |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 1149 | static int pin_code_reply(struct sock *sk, u16 index, unsigned char *data, |
| 1150 | u16 len) |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 1151 | { |
| 1152 | struct hci_dev *hdev; |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 1153 | struct hci_conn *conn; |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 1154 | struct mgmt_cp_pin_code_reply *cp; |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 1155 | struct mgmt_cp_pin_code_neg_reply ncp; |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 1156 | struct hci_cp_pin_code_reply reply; |
Johan Hedberg | 366a033 | 2011-02-19 12:05:55 -0300 | [diff] [blame] | 1157 | struct pending_cmd *cmd; |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 1158 | int err; |
| 1159 | |
| 1160 | BT_DBG(""); |
| 1161 | |
| 1162 | cp = (void *) data; |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 1163 | |
Szymon Janc | bdce7ba | 2011-02-25 19:05:49 +0100 | [diff] [blame] | 1164 | if (len != sizeof(*cp)) |
| 1165 | return cmd_status(sk, index, MGMT_OP_PIN_CODE_REPLY, EINVAL); |
| 1166 | |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 1167 | hdev = hci_dev_get(index); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 1168 | if (!hdev) |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 1169 | return cmd_status(sk, index, MGMT_OP_PIN_CODE_REPLY, ENODEV); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 1170 | |
Andre Guedes | 8c156c3 | 2011-07-07 10:30:36 -0300 | [diff] [blame] | 1171 | hci_dev_lock_bh(hdev); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 1172 | |
| 1173 | if (!test_bit(HCI_UP, &hdev->flags)) { |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 1174 | err = cmd_status(sk, index, MGMT_OP_PIN_CODE_REPLY, ENETDOWN); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 1175 | goto failed; |
| 1176 | } |
| 1177 | |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 1178 | conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &cp->bdaddr); |
| 1179 | if (!conn) { |
| 1180 | err = cmd_status(sk, index, MGMT_OP_PIN_CODE_REPLY, ENOTCONN); |
| 1181 | goto failed; |
| 1182 | } |
| 1183 | |
| 1184 | if (conn->pending_sec_level == BT_SECURITY_HIGH && cp->pin_len != 16) { |
| 1185 | bacpy(&ncp.bdaddr, &cp->bdaddr); |
| 1186 | |
| 1187 | BT_ERR("PIN code is not 16 bytes long"); |
| 1188 | |
| 1189 | err = send_pin_code_neg_reply(sk, index, hdev, &ncp); |
| 1190 | if (err >= 0) |
| 1191 | err = cmd_status(sk, index, MGMT_OP_PIN_CODE_REPLY, |
| 1192 | EINVAL); |
| 1193 | |
| 1194 | goto failed; |
| 1195 | } |
| 1196 | |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 1197 | 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] | 1198 | if (!cmd) { |
| 1199 | err = -ENOMEM; |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 1200 | goto failed; |
Johan Hedberg | 366a033 | 2011-02-19 12:05:55 -0300 | [diff] [blame] | 1201 | } |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 1202 | |
| 1203 | bacpy(&reply.bdaddr, &cp->bdaddr); |
| 1204 | reply.pin_len = cp->pin_len; |
Waldemar Rymarkiewicz | 24718ca | 2011-06-01 17:28:47 +0200 | [diff] [blame] | 1205 | memcpy(reply.pin_code, cp->pin_code, sizeof(reply.pin_code)); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 1206 | |
| 1207 | err = hci_send_cmd(hdev, HCI_OP_PIN_CODE_REPLY, sizeof(reply), &reply); |
| 1208 | if (err < 0) |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 1209 | mgmt_pending_remove(cmd); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 1210 | |
| 1211 | failed: |
Andre Guedes | 8c156c3 | 2011-07-07 10:30:36 -0300 | [diff] [blame] | 1212 | hci_dev_unlock_bh(hdev); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 1213 | hci_dev_put(hdev); |
| 1214 | |
| 1215 | return err; |
| 1216 | } |
| 1217 | |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 1218 | static int pin_code_neg_reply(struct sock *sk, u16 index, unsigned char *data, |
| 1219 | u16 len) |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 1220 | { |
| 1221 | struct hci_dev *hdev; |
| 1222 | struct mgmt_cp_pin_code_neg_reply *cp; |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 1223 | int err; |
| 1224 | |
| 1225 | BT_DBG(""); |
| 1226 | |
| 1227 | cp = (void *) data; |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 1228 | |
Szymon Janc | bdce7ba | 2011-02-25 19:05:49 +0100 | [diff] [blame] | 1229 | if (len != sizeof(*cp)) |
| 1230 | return cmd_status(sk, index, MGMT_OP_PIN_CODE_NEG_REPLY, |
| 1231 | EINVAL); |
| 1232 | |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 1233 | hdev = hci_dev_get(index); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [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_PIN_CODE_NEG_REPLY, |
| 1236 | ENODEV); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 1237 | |
Andre Guedes | 8c156c3 | 2011-07-07 10:30:36 -0300 | [diff] [blame] | 1238 | hci_dev_lock_bh(hdev); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 1239 | |
| 1240 | if (!test_bit(HCI_UP, &hdev->flags)) { |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 1241 | err = cmd_status(sk, index, MGMT_OP_PIN_CODE_NEG_REPLY, |
| 1242 | ENETDOWN); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 1243 | goto failed; |
| 1244 | } |
| 1245 | |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 1246 | err = send_pin_code_neg_reply(sk, index, hdev, cp); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 1247 | |
| 1248 | failed: |
Andre Guedes | 8c156c3 | 2011-07-07 10:30:36 -0300 | [diff] [blame] | 1249 | hci_dev_unlock_bh(hdev); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 1250 | hci_dev_put(hdev); |
| 1251 | |
| 1252 | return err; |
| 1253 | } |
| 1254 | |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 1255 | static int set_io_capability(struct sock *sk, u16 index, unsigned char *data, |
| 1256 | u16 len) |
Johan Hedberg | 17fa4b9 | 2011-01-25 13:28:33 +0200 | [diff] [blame] | 1257 | { |
| 1258 | struct hci_dev *hdev; |
| 1259 | struct mgmt_cp_set_io_capability *cp; |
Johan Hedberg | 17fa4b9 | 2011-01-25 13:28:33 +0200 | [diff] [blame] | 1260 | |
| 1261 | BT_DBG(""); |
| 1262 | |
| 1263 | cp = (void *) data; |
Johan Hedberg | 17fa4b9 | 2011-01-25 13:28:33 +0200 | [diff] [blame] | 1264 | |
Szymon Janc | bdce7ba | 2011-02-25 19:05:49 +0100 | [diff] [blame] | 1265 | if (len != sizeof(*cp)) |
Szymon Janc | b8534e0 | 2011-03-01 16:55:34 +0100 | [diff] [blame] | 1266 | return cmd_status(sk, index, MGMT_OP_SET_IO_CAPABILITY, EINVAL); |
Szymon Janc | bdce7ba | 2011-02-25 19:05:49 +0100 | [diff] [blame] | 1267 | |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 1268 | hdev = hci_dev_get(index); |
Johan Hedberg | 17fa4b9 | 2011-01-25 13:28:33 +0200 | [diff] [blame] | 1269 | if (!hdev) |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 1270 | return cmd_status(sk, index, MGMT_OP_SET_IO_CAPABILITY, ENODEV); |
Johan Hedberg | 17fa4b9 | 2011-01-25 13:28:33 +0200 | [diff] [blame] | 1271 | |
Andre Guedes | 8c156c3 | 2011-07-07 10:30:36 -0300 | [diff] [blame] | 1272 | hci_dev_lock_bh(hdev); |
Johan Hedberg | 17fa4b9 | 2011-01-25 13:28:33 +0200 | [diff] [blame] | 1273 | |
| 1274 | hdev->io_capability = cp->io_capability; |
| 1275 | |
| 1276 | BT_DBG("%s IO capability set to 0x%02x", hdev->name, |
Szymon Janc | b8534e0 | 2011-03-01 16:55:34 +0100 | [diff] [blame] | 1277 | hdev->io_capability); |
Johan Hedberg | 17fa4b9 | 2011-01-25 13:28:33 +0200 | [diff] [blame] | 1278 | |
Andre Guedes | 8c156c3 | 2011-07-07 10:30:36 -0300 | [diff] [blame] | 1279 | hci_dev_unlock_bh(hdev); |
Johan Hedberg | 17fa4b9 | 2011-01-25 13:28:33 +0200 | [diff] [blame] | 1280 | hci_dev_put(hdev); |
| 1281 | |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 1282 | return cmd_complete(sk, index, MGMT_OP_SET_IO_CAPABILITY, NULL, 0); |
Johan Hedberg | 17fa4b9 | 2011-01-25 13:28:33 +0200 | [diff] [blame] | 1283 | } |
| 1284 | |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 1285 | static inline struct pending_cmd *find_pairing(struct hci_conn *conn) |
| 1286 | { |
| 1287 | struct hci_dev *hdev = conn->hdev; |
| 1288 | struct list_head *p; |
| 1289 | |
| 1290 | list_for_each(p, &cmd_list) { |
| 1291 | struct pending_cmd *cmd; |
| 1292 | |
| 1293 | cmd = list_entry(p, struct pending_cmd, list); |
| 1294 | |
| 1295 | if (cmd->opcode != MGMT_OP_PAIR_DEVICE) |
| 1296 | continue; |
| 1297 | |
| 1298 | if (cmd->index != hdev->id) |
| 1299 | continue; |
| 1300 | |
| 1301 | if (cmd->user_data != conn) |
| 1302 | continue; |
| 1303 | |
| 1304 | return cmd; |
| 1305 | } |
| 1306 | |
| 1307 | return NULL; |
| 1308 | } |
| 1309 | |
| 1310 | static void pairing_complete(struct pending_cmd *cmd, u8 status) |
| 1311 | { |
| 1312 | struct mgmt_rp_pair_device rp; |
| 1313 | struct hci_conn *conn = cmd->user_data; |
| 1314 | |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 1315 | bacpy(&rp.bdaddr, &conn->dst); |
| 1316 | rp.status = status; |
| 1317 | |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 1318 | 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] | 1319 | |
| 1320 | /* So we don't get further callbacks for this connection */ |
| 1321 | conn->connect_cfm_cb = NULL; |
| 1322 | conn->security_cfm_cb = NULL; |
| 1323 | conn->disconn_cfm_cb = NULL; |
| 1324 | |
| 1325 | hci_conn_put(conn); |
| 1326 | |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 1327 | mgmt_pending_remove(cmd); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 1328 | } |
| 1329 | |
| 1330 | static void pairing_complete_cb(struct hci_conn *conn, u8 status) |
| 1331 | { |
| 1332 | struct pending_cmd *cmd; |
| 1333 | |
| 1334 | BT_DBG("status %u", status); |
| 1335 | |
| 1336 | cmd = find_pairing(conn); |
| 1337 | if (!cmd) { |
| 1338 | BT_DBG("Unable to find a pending command"); |
| 1339 | return; |
| 1340 | } |
| 1341 | |
| 1342 | pairing_complete(cmd, status); |
| 1343 | } |
| 1344 | |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 1345 | 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] | 1346 | { |
| 1347 | struct hci_dev *hdev; |
| 1348 | struct mgmt_cp_pair_device *cp; |
| 1349 | struct pending_cmd *cmd; |
Vinicius Costa Gomes | 7a512d0 | 2011-08-19 21:06:54 -0300 | [diff] [blame] | 1350 | struct adv_entry *entry; |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 1351 | u8 sec_level, auth_type; |
| 1352 | struct hci_conn *conn; |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 1353 | int err; |
| 1354 | |
| 1355 | BT_DBG(""); |
| 1356 | |
| 1357 | cp = (void *) data; |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 1358 | |
Szymon Janc | bdce7ba | 2011-02-25 19:05:49 +0100 | [diff] [blame] | 1359 | if (len != sizeof(*cp)) |
| 1360 | return cmd_status(sk, index, MGMT_OP_PAIR_DEVICE, EINVAL); |
| 1361 | |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 1362 | hdev = hci_dev_get(index); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 1363 | if (!hdev) |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 1364 | return cmd_status(sk, index, MGMT_OP_PAIR_DEVICE, ENODEV); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 1365 | |
Andre Guedes | 8c156c3 | 2011-07-07 10:30:36 -0300 | [diff] [blame] | 1366 | hci_dev_lock_bh(hdev); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 1367 | |
Vinicius Costa Gomes | c908df3 | 2011-09-02 14:51:22 -0300 | [diff] [blame^] | 1368 | sec_level = BT_SECURITY_MEDIUM; |
| 1369 | if (cp->io_cap == 0x03) |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 1370 | auth_type = HCI_AT_DEDICATED_BONDING; |
Vinicius Costa Gomes | c908df3 | 2011-09-02 14:51:22 -0300 | [diff] [blame^] | 1371 | else |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 1372 | auth_type = HCI_AT_DEDICATED_BONDING_MITM; |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 1373 | |
Vinicius Costa Gomes | 7a512d0 | 2011-08-19 21:06:54 -0300 | [diff] [blame] | 1374 | entry = hci_find_adv_entry(hdev, &cp->bdaddr); |
| 1375 | if (entry) |
| 1376 | conn = hci_connect(hdev, LE_LINK, &cp->bdaddr, sec_level, |
| 1377 | auth_type); |
| 1378 | else |
| 1379 | conn = hci_connect(hdev, ACL_LINK, &cp->bdaddr, sec_level, |
| 1380 | auth_type); |
| 1381 | |
Ville Tervo | 30e7627 | 2011-02-22 16:10:53 -0300 | [diff] [blame] | 1382 | if (IS_ERR(conn)) { |
| 1383 | err = PTR_ERR(conn); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 1384 | goto unlock; |
| 1385 | } |
| 1386 | |
| 1387 | if (conn->connect_cfm_cb) { |
| 1388 | hci_conn_put(conn); |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 1389 | err = cmd_status(sk, index, MGMT_OP_PAIR_DEVICE, EBUSY); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 1390 | goto unlock; |
| 1391 | } |
| 1392 | |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 1393 | cmd = mgmt_pending_add(sk, MGMT_OP_PAIR_DEVICE, index, data, len); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 1394 | if (!cmd) { |
| 1395 | err = -ENOMEM; |
| 1396 | hci_conn_put(conn); |
| 1397 | goto unlock; |
| 1398 | } |
| 1399 | |
Vinicius Costa Gomes | 7a512d0 | 2011-08-19 21:06:54 -0300 | [diff] [blame] | 1400 | /* For LE, just connecting isn't a proof that the pairing finished */ |
| 1401 | if (!entry) |
| 1402 | conn->connect_cfm_cb = pairing_complete_cb; |
| 1403 | |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 1404 | conn->security_cfm_cb = pairing_complete_cb; |
| 1405 | conn->disconn_cfm_cb = pairing_complete_cb; |
| 1406 | conn->io_capability = cp->io_cap; |
| 1407 | cmd->user_data = conn; |
| 1408 | |
| 1409 | if (conn->state == BT_CONNECTED && |
| 1410 | hci_conn_security(conn, sec_level, auth_type)) |
| 1411 | pairing_complete(cmd, 0); |
| 1412 | |
| 1413 | err = 0; |
| 1414 | |
| 1415 | unlock: |
Andre Guedes | 8c156c3 | 2011-07-07 10:30:36 -0300 | [diff] [blame] | 1416 | hci_dev_unlock_bh(hdev); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 1417 | hci_dev_put(hdev); |
| 1418 | |
| 1419 | return err; |
| 1420 | } |
| 1421 | |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 1422 | static int user_confirm_reply(struct sock *sk, u16 index, unsigned char *data, |
| 1423 | u16 len, int success) |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 1424 | { |
| 1425 | struct mgmt_cp_user_confirm_reply *cp = (void *) data; |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 1426 | u16 mgmt_op, hci_op; |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 1427 | struct pending_cmd *cmd; |
| 1428 | struct hci_dev *hdev; |
| 1429 | int err; |
| 1430 | |
| 1431 | BT_DBG(""); |
| 1432 | |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 1433 | if (success) { |
| 1434 | mgmt_op = MGMT_OP_USER_CONFIRM_REPLY; |
| 1435 | hci_op = HCI_OP_USER_CONFIRM_REPLY; |
| 1436 | } else { |
| 1437 | mgmt_op = MGMT_OP_USER_CONFIRM_NEG_REPLY; |
| 1438 | hci_op = HCI_OP_USER_CONFIRM_NEG_REPLY; |
| 1439 | } |
| 1440 | |
Szymon Janc | bdce7ba | 2011-02-25 19:05:49 +0100 | [diff] [blame] | 1441 | if (len != sizeof(*cp)) |
| 1442 | return cmd_status(sk, index, mgmt_op, EINVAL); |
| 1443 | |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 1444 | hdev = hci_dev_get(index); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 1445 | if (!hdev) |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 1446 | return cmd_status(sk, index, mgmt_op, ENODEV); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 1447 | |
Andre Guedes | 8c156c3 | 2011-07-07 10:30:36 -0300 | [diff] [blame] | 1448 | hci_dev_lock_bh(hdev); |
Johan Hedberg | 08ba538 | 2011-03-16 14:29:34 +0200 | [diff] [blame] | 1449 | |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 1450 | if (!test_bit(HCI_UP, &hdev->flags)) { |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 1451 | err = cmd_status(sk, index, mgmt_op, ENETDOWN); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 1452 | goto failed; |
| 1453 | } |
| 1454 | |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 1455 | cmd = mgmt_pending_add(sk, mgmt_op, index, data, len); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 1456 | if (!cmd) { |
| 1457 | err = -ENOMEM; |
| 1458 | goto failed; |
| 1459 | } |
| 1460 | |
| 1461 | err = hci_send_cmd(hdev, hci_op, sizeof(cp->bdaddr), &cp->bdaddr); |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 1462 | if (err < 0) |
| 1463 | mgmt_pending_remove(cmd); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 1464 | |
| 1465 | failed: |
Andre Guedes | 8c156c3 | 2011-07-07 10:30:36 -0300 | [diff] [blame] | 1466 | hci_dev_unlock_bh(hdev); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 1467 | hci_dev_put(hdev); |
| 1468 | |
| 1469 | return err; |
| 1470 | } |
| 1471 | |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 1472 | static int set_local_name(struct sock *sk, u16 index, unsigned char *data, |
| 1473 | u16 len) |
| 1474 | { |
| 1475 | struct mgmt_cp_set_local_name *mgmt_cp = (void *) data; |
| 1476 | struct hci_cp_write_local_name hci_cp; |
| 1477 | struct hci_dev *hdev; |
| 1478 | struct pending_cmd *cmd; |
| 1479 | int err; |
| 1480 | |
| 1481 | BT_DBG(""); |
| 1482 | |
| 1483 | if (len != sizeof(*mgmt_cp)) |
| 1484 | return cmd_status(sk, index, MGMT_OP_SET_LOCAL_NAME, EINVAL); |
| 1485 | |
| 1486 | hdev = hci_dev_get(index); |
| 1487 | if (!hdev) |
| 1488 | return cmd_status(sk, index, MGMT_OP_SET_LOCAL_NAME, ENODEV); |
| 1489 | |
Andre Guedes | 8c156c3 | 2011-07-07 10:30:36 -0300 | [diff] [blame] | 1490 | hci_dev_lock_bh(hdev); |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 1491 | |
| 1492 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_LOCAL_NAME, index, data, len); |
| 1493 | if (!cmd) { |
| 1494 | err = -ENOMEM; |
| 1495 | goto failed; |
| 1496 | } |
| 1497 | |
| 1498 | memcpy(hci_cp.name, mgmt_cp->name, sizeof(hci_cp.name)); |
| 1499 | err = hci_send_cmd(hdev, HCI_OP_WRITE_LOCAL_NAME, sizeof(hci_cp), |
| 1500 | &hci_cp); |
| 1501 | if (err < 0) |
| 1502 | mgmt_pending_remove(cmd); |
| 1503 | |
| 1504 | failed: |
Andre Guedes | 8c156c3 | 2011-07-07 10:30:36 -0300 | [diff] [blame] | 1505 | hci_dev_unlock_bh(hdev); |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 1506 | hci_dev_put(hdev); |
| 1507 | |
| 1508 | return err; |
| 1509 | } |
| 1510 | |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 1511 | static int read_local_oob_data(struct sock *sk, u16 index) |
| 1512 | { |
| 1513 | struct hci_dev *hdev; |
| 1514 | struct pending_cmd *cmd; |
| 1515 | int err; |
| 1516 | |
| 1517 | BT_DBG("hci%u", index); |
| 1518 | |
| 1519 | hdev = hci_dev_get(index); |
| 1520 | if (!hdev) |
| 1521 | return cmd_status(sk, index, MGMT_OP_READ_LOCAL_OOB_DATA, |
| 1522 | ENODEV); |
| 1523 | |
Andre Guedes | 8c156c3 | 2011-07-07 10:30:36 -0300 | [diff] [blame] | 1524 | hci_dev_lock_bh(hdev); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 1525 | |
| 1526 | if (!test_bit(HCI_UP, &hdev->flags)) { |
| 1527 | err = cmd_status(sk, index, MGMT_OP_READ_LOCAL_OOB_DATA, |
| 1528 | ENETDOWN); |
| 1529 | goto unlock; |
| 1530 | } |
| 1531 | |
| 1532 | if (!(hdev->features[6] & LMP_SIMPLE_PAIR)) { |
| 1533 | err = cmd_status(sk, index, MGMT_OP_READ_LOCAL_OOB_DATA, |
| 1534 | EOPNOTSUPP); |
| 1535 | goto unlock; |
| 1536 | } |
| 1537 | |
| 1538 | if (mgmt_pending_find(MGMT_OP_READ_LOCAL_OOB_DATA, index)) { |
| 1539 | err = cmd_status(sk, index, MGMT_OP_READ_LOCAL_OOB_DATA, EBUSY); |
| 1540 | goto unlock; |
| 1541 | } |
| 1542 | |
| 1543 | cmd = mgmt_pending_add(sk, MGMT_OP_READ_LOCAL_OOB_DATA, index, NULL, 0); |
| 1544 | if (!cmd) { |
| 1545 | err = -ENOMEM; |
| 1546 | goto unlock; |
| 1547 | } |
| 1548 | |
| 1549 | err = hci_send_cmd(hdev, HCI_OP_READ_LOCAL_OOB_DATA, 0, NULL); |
| 1550 | if (err < 0) |
| 1551 | mgmt_pending_remove(cmd); |
| 1552 | |
| 1553 | unlock: |
Andre Guedes | 8c156c3 | 2011-07-07 10:30:36 -0300 | [diff] [blame] | 1554 | hci_dev_unlock_bh(hdev); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 1555 | hci_dev_put(hdev); |
| 1556 | |
| 1557 | return err; |
| 1558 | } |
| 1559 | |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 1560 | static int add_remote_oob_data(struct sock *sk, u16 index, unsigned char *data, |
| 1561 | u16 len) |
| 1562 | { |
| 1563 | struct hci_dev *hdev; |
| 1564 | struct mgmt_cp_add_remote_oob_data *cp = (void *) data; |
| 1565 | int err; |
| 1566 | |
| 1567 | BT_DBG("hci%u ", index); |
| 1568 | |
| 1569 | if (len != sizeof(*cp)) |
| 1570 | return cmd_status(sk, index, MGMT_OP_ADD_REMOTE_OOB_DATA, |
| 1571 | EINVAL); |
| 1572 | |
| 1573 | hdev = hci_dev_get(index); |
| 1574 | if (!hdev) |
| 1575 | return cmd_status(sk, index, MGMT_OP_ADD_REMOTE_OOB_DATA, |
| 1576 | ENODEV); |
| 1577 | |
Andre Guedes | 8c156c3 | 2011-07-07 10:30:36 -0300 | [diff] [blame] | 1578 | hci_dev_lock_bh(hdev); |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 1579 | |
| 1580 | err = hci_add_remote_oob_data(hdev, &cp->bdaddr, cp->hash, |
| 1581 | cp->randomizer); |
| 1582 | if (err < 0) |
| 1583 | err = cmd_status(sk, index, MGMT_OP_ADD_REMOTE_OOB_DATA, -err); |
| 1584 | else |
| 1585 | err = cmd_complete(sk, index, MGMT_OP_ADD_REMOTE_OOB_DATA, NULL, |
| 1586 | 0); |
| 1587 | |
Andre Guedes | 8c156c3 | 2011-07-07 10:30:36 -0300 | [diff] [blame] | 1588 | hci_dev_unlock_bh(hdev); |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 1589 | hci_dev_put(hdev); |
| 1590 | |
| 1591 | return err; |
| 1592 | } |
| 1593 | |
| 1594 | static int remove_remote_oob_data(struct sock *sk, u16 index, |
| 1595 | unsigned char *data, u16 len) |
| 1596 | { |
| 1597 | struct hci_dev *hdev; |
| 1598 | struct mgmt_cp_remove_remote_oob_data *cp = (void *) data; |
| 1599 | int err; |
| 1600 | |
| 1601 | BT_DBG("hci%u ", index); |
| 1602 | |
| 1603 | if (len != sizeof(*cp)) |
| 1604 | return cmd_status(sk, index, MGMT_OP_REMOVE_REMOTE_OOB_DATA, |
| 1605 | EINVAL); |
| 1606 | |
| 1607 | hdev = hci_dev_get(index); |
| 1608 | if (!hdev) |
| 1609 | return cmd_status(sk, index, MGMT_OP_REMOVE_REMOTE_OOB_DATA, |
| 1610 | ENODEV); |
| 1611 | |
Andre Guedes | 8c156c3 | 2011-07-07 10:30:36 -0300 | [diff] [blame] | 1612 | hci_dev_lock_bh(hdev); |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 1613 | |
| 1614 | err = hci_remove_remote_oob_data(hdev, &cp->bdaddr); |
| 1615 | if (err < 0) |
| 1616 | err = cmd_status(sk, index, MGMT_OP_REMOVE_REMOTE_OOB_DATA, |
| 1617 | -err); |
| 1618 | else |
| 1619 | err = cmd_complete(sk, index, MGMT_OP_REMOVE_REMOTE_OOB_DATA, |
| 1620 | NULL, 0); |
| 1621 | |
Andre Guedes | 8c156c3 | 2011-07-07 10:30:36 -0300 | [diff] [blame] | 1622 | hci_dev_unlock_bh(hdev); |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 1623 | hci_dev_put(hdev); |
| 1624 | |
| 1625 | return err; |
| 1626 | } |
| 1627 | |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 1628 | static int start_discovery(struct sock *sk, u16 index) |
| 1629 | { |
| 1630 | u8 lap[3] = { 0x33, 0x8b, 0x9e }; |
| 1631 | struct hci_cp_inquiry cp; |
| 1632 | struct pending_cmd *cmd; |
| 1633 | struct hci_dev *hdev; |
| 1634 | int err; |
| 1635 | |
| 1636 | BT_DBG("hci%u", index); |
| 1637 | |
| 1638 | hdev = hci_dev_get(index); |
| 1639 | if (!hdev) |
| 1640 | return cmd_status(sk, index, MGMT_OP_START_DISCOVERY, ENODEV); |
| 1641 | |
| 1642 | hci_dev_lock_bh(hdev); |
| 1643 | |
| 1644 | cmd = mgmt_pending_add(sk, MGMT_OP_START_DISCOVERY, index, NULL, 0); |
| 1645 | if (!cmd) { |
| 1646 | err = -ENOMEM; |
| 1647 | goto failed; |
| 1648 | } |
| 1649 | |
| 1650 | memset(&cp, 0, sizeof(cp)); |
| 1651 | memcpy(&cp.lap, lap, 3); |
| 1652 | cp.length = 0x08; |
| 1653 | cp.num_rsp = 0x00; |
| 1654 | |
| 1655 | err = hci_send_cmd(hdev, HCI_OP_INQUIRY, sizeof(cp), &cp); |
| 1656 | if (err < 0) |
| 1657 | mgmt_pending_remove(cmd); |
| 1658 | |
| 1659 | failed: |
| 1660 | hci_dev_unlock_bh(hdev); |
| 1661 | hci_dev_put(hdev); |
| 1662 | |
| 1663 | return err; |
| 1664 | } |
| 1665 | |
| 1666 | static int stop_discovery(struct sock *sk, u16 index) |
| 1667 | { |
| 1668 | struct hci_dev *hdev; |
| 1669 | struct pending_cmd *cmd; |
| 1670 | int err; |
| 1671 | |
| 1672 | BT_DBG("hci%u", index); |
| 1673 | |
| 1674 | hdev = hci_dev_get(index); |
| 1675 | if (!hdev) |
| 1676 | return cmd_status(sk, index, MGMT_OP_STOP_DISCOVERY, ENODEV); |
| 1677 | |
| 1678 | hci_dev_lock_bh(hdev); |
| 1679 | |
| 1680 | cmd = mgmt_pending_add(sk, MGMT_OP_STOP_DISCOVERY, index, NULL, 0); |
| 1681 | if (!cmd) { |
| 1682 | err = -ENOMEM; |
| 1683 | goto failed; |
| 1684 | } |
| 1685 | |
| 1686 | err = hci_send_cmd(hdev, HCI_OP_INQUIRY_CANCEL, 0, NULL); |
| 1687 | if (err < 0) |
| 1688 | mgmt_pending_remove(cmd); |
| 1689 | |
| 1690 | failed: |
| 1691 | hci_dev_unlock_bh(hdev); |
| 1692 | hci_dev_put(hdev); |
| 1693 | |
| 1694 | return err; |
| 1695 | } |
| 1696 | |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 1697 | static int block_device(struct sock *sk, u16 index, unsigned char *data, |
| 1698 | u16 len) |
| 1699 | { |
| 1700 | struct hci_dev *hdev; |
| 1701 | struct mgmt_cp_block_device *cp; |
| 1702 | int err; |
| 1703 | |
| 1704 | BT_DBG("hci%u", index); |
| 1705 | |
| 1706 | cp = (void *) data; |
| 1707 | |
| 1708 | if (len != sizeof(*cp)) |
| 1709 | return cmd_status(sk, index, MGMT_OP_BLOCK_DEVICE, |
| 1710 | EINVAL); |
| 1711 | |
| 1712 | hdev = hci_dev_get(index); |
| 1713 | if (!hdev) |
| 1714 | return cmd_status(sk, index, MGMT_OP_BLOCK_DEVICE, |
| 1715 | ENODEV); |
| 1716 | |
| 1717 | err = hci_blacklist_add(hdev, &cp->bdaddr); |
| 1718 | |
| 1719 | if (err < 0) |
| 1720 | err = cmd_status(sk, index, MGMT_OP_BLOCK_DEVICE, -err); |
| 1721 | else |
| 1722 | err = cmd_complete(sk, index, MGMT_OP_BLOCK_DEVICE, |
| 1723 | NULL, 0); |
| 1724 | hci_dev_put(hdev); |
| 1725 | |
| 1726 | return err; |
| 1727 | } |
| 1728 | |
| 1729 | static int unblock_device(struct sock *sk, u16 index, unsigned char *data, |
| 1730 | u16 len) |
| 1731 | { |
| 1732 | struct hci_dev *hdev; |
| 1733 | struct mgmt_cp_unblock_device *cp; |
| 1734 | int err; |
| 1735 | |
| 1736 | BT_DBG("hci%u", index); |
| 1737 | |
| 1738 | cp = (void *) data; |
| 1739 | |
| 1740 | if (len != sizeof(*cp)) |
| 1741 | return cmd_status(sk, index, MGMT_OP_UNBLOCK_DEVICE, |
| 1742 | EINVAL); |
| 1743 | |
| 1744 | hdev = hci_dev_get(index); |
| 1745 | if (!hdev) |
| 1746 | return cmd_status(sk, index, MGMT_OP_UNBLOCK_DEVICE, |
| 1747 | ENODEV); |
| 1748 | |
| 1749 | err = hci_blacklist_del(hdev, &cp->bdaddr); |
| 1750 | |
| 1751 | if (err < 0) |
| 1752 | err = cmd_status(sk, index, MGMT_OP_UNBLOCK_DEVICE, -err); |
| 1753 | else |
| 1754 | err = cmd_complete(sk, index, MGMT_OP_UNBLOCK_DEVICE, |
| 1755 | NULL, 0); |
| 1756 | hci_dev_put(hdev); |
| 1757 | |
| 1758 | return err; |
| 1759 | } |
| 1760 | |
Antti Julku | f6422ec | 2011-06-22 13:11:56 +0300 | [diff] [blame] | 1761 | static int set_fast_connectable(struct sock *sk, u16 index, |
| 1762 | unsigned char *data, u16 len) |
| 1763 | { |
| 1764 | struct hci_dev *hdev; |
| 1765 | struct mgmt_cp_set_fast_connectable *cp = (void *) data; |
| 1766 | struct hci_cp_write_page_scan_activity acp; |
| 1767 | u8 type; |
| 1768 | int err; |
| 1769 | |
| 1770 | BT_DBG("hci%u", index); |
| 1771 | |
| 1772 | if (len != sizeof(*cp)) |
| 1773 | return cmd_status(sk, index, MGMT_OP_SET_FAST_CONNECTABLE, |
| 1774 | EINVAL); |
| 1775 | |
| 1776 | hdev = hci_dev_get(index); |
| 1777 | if (!hdev) |
| 1778 | return cmd_status(sk, index, MGMT_OP_SET_FAST_CONNECTABLE, |
| 1779 | ENODEV); |
| 1780 | |
| 1781 | hci_dev_lock(hdev); |
| 1782 | |
| 1783 | if (cp->enable) { |
| 1784 | type = PAGE_SCAN_TYPE_INTERLACED; |
| 1785 | acp.interval = 0x0024; /* 22.5 msec page scan interval */ |
| 1786 | } else { |
| 1787 | type = PAGE_SCAN_TYPE_STANDARD; /* default */ |
| 1788 | acp.interval = 0x0800; /* default 1.28 sec page scan */ |
| 1789 | } |
| 1790 | |
| 1791 | acp.window = 0x0012; /* default 11.25 msec page scan window */ |
| 1792 | |
| 1793 | err = hci_send_cmd(hdev, HCI_OP_WRITE_PAGE_SCAN_ACTIVITY, |
| 1794 | sizeof(acp), &acp); |
| 1795 | if (err < 0) { |
| 1796 | err = cmd_status(sk, index, MGMT_OP_SET_FAST_CONNECTABLE, |
| 1797 | -err); |
| 1798 | goto done; |
| 1799 | } |
| 1800 | |
| 1801 | err = hci_send_cmd(hdev, HCI_OP_WRITE_PAGE_SCAN_TYPE, 1, &type); |
| 1802 | if (err < 0) { |
| 1803 | err = cmd_status(sk, index, MGMT_OP_SET_FAST_CONNECTABLE, |
| 1804 | -err); |
| 1805 | goto done; |
| 1806 | } |
| 1807 | |
| 1808 | err = cmd_complete(sk, index, MGMT_OP_SET_FAST_CONNECTABLE, |
| 1809 | NULL, 0); |
| 1810 | done: |
| 1811 | hci_dev_unlock(hdev); |
| 1812 | hci_dev_put(hdev); |
| 1813 | |
| 1814 | return err; |
| 1815 | } |
| 1816 | |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1817 | int mgmt_control(struct sock *sk, struct msghdr *msg, size_t msglen) |
| 1818 | { |
| 1819 | unsigned char *buf; |
| 1820 | struct mgmt_hdr *hdr; |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 1821 | u16 opcode, index, len; |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1822 | int err; |
| 1823 | |
| 1824 | BT_DBG("got %zu bytes", msglen); |
| 1825 | |
| 1826 | if (msglen < sizeof(*hdr)) |
| 1827 | return -EINVAL; |
| 1828 | |
Gustavo F. Padovan | e63a15e | 2011-04-04 18:56:53 -0300 | [diff] [blame] | 1829 | buf = kmalloc(msglen, GFP_KERNEL); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1830 | if (!buf) |
| 1831 | return -ENOMEM; |
| 1832 | |
| 1833 | if (memcpy_fromiovec(buf, msg->msg_iov, msglen)) { |
| 1834 | err = -EFAULT; |
| 1835 | goto done; |
| 1836 | } |
| 1837 | |
| 1838 | hdr = (struct mgmt_hdr *) buf; |
| 1839 | opcode = get_unaligned_le16(&hdr->opcode); |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 1840 | index = get_unaligned_le16(&hdr->index); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1841 | len = get_unaligned_le16(&hdr->len); |
| 1842 | |
| 1843 | if (len != msglen - sizeof(*hdr)) { |
| 1844 | err = -EINVAL; |
| 1845 | goto done; |
| 1846 | } |
| 1847 | |
| 1848 | switch (opcode) { |
Johan Hedberg | 02d9812 | 2010-12-13 21:07:04 +0200 | [diff] [blame] | 1849 | case MGMT_OP_READ_VERSION: |
| 1850 | err = read_version(sk); |
| 1851 | break; |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 1852 | case MGMT_OP_READ_INDEX_LIST: |
| 1853 | err = read_index_list(sk); |
| 1854 | break; |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 1855 | case MGMT_OP_READ_INFO: |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 1856 | err = read_controller_info(sk, index); |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 1857 | break; |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1858 | case MGMT_OP_SET_POWERED: |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 1859 | err = set_powered(sk, index, buf + sizeof(*hdr), len); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1860 | break; |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 1861 | case MGMT_OP_SET_DISCOVERABLE: |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 1862 | err = set_discoverable(sk, index, buf + sizeof(*hdr), len); |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 1863 | break; |
Johan Hedberg | 9fbcbb4 | 2010-12-30 00:18:33 +0200 | [diff] [blame] | 1864 | case MGMT_OP_SET_CONNECTABLE: |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 1865 | err = set_connectable(sk, index, buf + sizeof(*hdr), len); |
Johan Hedberg | 9fbcbb4 | 2010-12-30 00:18:33 +0200 | [diff] [blame] | 1866 | break; |
Johan Hedberg | c542a06 | 2011-01-26 13:11:03 +0200 | [diff] [blame] | 1867 | case MGMT_OP_SET_PAIRABLE: |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 1868 | err = set_pairable(sk, index, buf + sizeof(*hdr), len); |
Johan Hedberg | c542a06 | 2011-01-26 13:11:03 +0200 | [diff] [blame] | 1869 | break; |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 1870 | case MGMT_OP_ADD_UUID: |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 1871 | err = add_uuid(sk, index, buf + sizeof(*hdr), len); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 1872 | break; |
| 1873 | case MGMT_OP_REMOVE_UUID: |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 1874 | err = remove_uuid(sk, index, buf + sizeof(*hdr), len); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 1875 | break; |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 1876 | case MGMT_OP_SET_DEV_CLASS: |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 1877 | err = set_dev_class(sk, index, buf + sizeof(*hdr), len); |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 1878 | break; |
| 1879 | case MGMT_OP_SET_SERVICE_CACHE: |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 1880 | err = set_service_cache(sk, index, buf + sizeof(*hdr), len); |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 1881 | break; |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 1882 | case MGMT_OP_LOAD_KEYS: |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 1883 | err = load_keys(sk, index, buf + sizeof(*hdr), len); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 1884 | break; |
| 1885 | case MGMT_OP_REMOVE_KEY: |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 1886 | err = remove_key(sk, index, buf + sizeof(*hdr), len); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 1887 | break; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 1888 | case MGMT_OP_DISCONNECT: |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 1889 | err = disconnect(sk, index, buf + sizeof(*hdr), len); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 1890 | break; |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 1891 | case MGMT_OP_GET_CONNECTIONS: |
Szymon Janc | 8ce6284 | 2011-03-01 16:55:32 +0100 | [diff] [blame] | 1892 | err = get_connections(sk, index); |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 1893 | break; |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 1894 | case MGMT_OP_PIN_CODE_REPLY: |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 1895 | err = pin_code_reply(sk, index, buf + sizeof(*hdr), len); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 1896 | break; |
| 1897 | case MGMT_OP_PIN_CODE_NEG_REPLY: |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 1898 | err = pin_code_neg_reply(sk, index, buf + sizeof(*hdr), len); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 1899 | break; |
Johan Hedberg | 17fa4b9 | 2011-01-25 13:28:33 +0200 | [diff] [blame] | 1900 | case MGMT_OP_SET_IO_CAPABILITY: |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 1901 | err = set_io_capability(sk, index, buf + sizeof(*hdr), len); |
Johan Hedberg | 17fa4b9 | 2011-01-25 13:28:33 +0200 | [diff] [blame] | 1902 | break; |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 1903 | case MGMT_OP_PAIR_DEVICE: |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 1904 | err = pair_device(sk, index, buf + sizeof(*hdr), len); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 1905 | break; |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 1906 | case MGMT_OP_USER_CONFIRM_REPLY: |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 1907 | err = user_confirm_reply(sk, index, buf + sizeof(*hdr), len, 1); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 1908 | break; |
| 1909 | case MGMT_OP_USER_CONFIRM_NEG_REPLY: |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 1910 | err = user_confirm_reply(sk, index, buf + sizeof(*hdr), len, 0); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 1911 | break; |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 1912 | case MGMT_OP_SET_LOCAL_NAME: |
| 1913 | err = set_local_name(sk, index, buf + sizeof(*hdr), len); |
| 1914 | break; |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 1915 | case MGMT_OP_READ_LOCAL_OOB_DATA: |
| 1916 | err = read_local_oob_data(sk, index); |
| 1917 | break; |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 1918 | case MGMT_OP_ADD_REMOTE_OOB_DATA: |
| 1919 | err = add_remote_oob_data(sk, index, buf + sizeof(*hdr), len); |
| 1920 | break; |
| 1921 | case MGMT_OP_REMOVE_REMOTE_OOB_DATA: |
| 1922 | err = remove_remote_oob_data(sk, index, buf + sizeof(*hdr), |
| 1923 | len); |
| 1924 | break; |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 1925 | case MGMT_OP_START_DISCOVERY: |
| 1926 | err = start_discovery(sk, index); |
| 1927 | break; |
| 1928 | case MGMT_OP_STOP_DISCOVERY: |
| 1929 | err = stop_discovery(sk, index); |
| 1930 | break; |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 1931 | case MGMT_OP_BLOCK_DEVICE: |
| 1932 | err = block_device(sk, index, buf + sizeof(*hdr), len); |
| 1933 | break; |
| 1934 | case MGMT_OP_UNBLOCK_DEVICE: |
| 1935 | err = unblock_device(sk, index, buf + sizeof(*hdr), len); |
| 1936 | break; |
Antti Julku | f6422ec | 2011-06-22 13:11:56 +0300 | [diff] [blame] | 1937 | case MGMT_OP_SET_FAST_CONNECTABLE: |
| 1938 | err = set_fast_connectable(sk, index, buf + sizeof(*hdr), |
| 1939 | len); |
| 1940 | break; |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1941 | default: |
| 1942 | BT_DBG("Unknown op %u", opcode); |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 1943 | err = cmd_status(sk, index, opcode, 0x01); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1944 | break; |
| 1945 | } |
| 1946 | |
Johan Hedberg | e41d8b4 | 2010-12-13 21:07:03 +0200 | [diff] [blame] | 1947 | if (err < 0) |
| 1948 | goto done; |
| 1949 | |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1950 | err = msglen; |
| 1951 | |
| 1952 | done: |
| 1953 | kfree(buf); |
| 1954 | return err; |
| 1955 | } |
Johan Hedberg | c71e97b | 2010-12-13 21:07:07 +0200 | [diff] [blame] | 1956 | |
Johan Hedberg | c71e97b | 2010-12-13 21:07:07 +0200 | [diff] [blame] | 1957 | int mgmt_index_added(u16 index) |
| 1958 | { |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 1959 | return mgmt_event(MGMT_EV_INDEX_ADDED, index, NULL, 0, NULL); |
Johan Hedberg | c71e97b | 2010-12-13 21:07:07 +0200 | [diff] [blame] | 1960 | } |
| 1961 | |
| 1962 | int mgmt_index_removed(u16 index) |
| 1963 | { |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 1964 | return mgmt_event(MGMT_EV_INDEX_REMOVED, index, NULL, 0, NULL); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1965 | } |
| 1966 | |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 1967 | struct cmd_lookup { |
Johan Hedberg | 72a734e | 2010-12-30 00:38:22 +0200 | [diff] [blame] | 1968 | u8 val; |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1969 | struct sock *sk; |
| 1970 | }; |
| 1971 | |
Johan Hedberg | 72a734e | 2010-12-30 00:38:22 +0200 | [diff] [blame] | 1972 | static void mode_rsp(struct pending_cmd *cmd, void *data) |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1973 | { |
Szymon Janc | c68fb7f | 2011-03-22 13:12:19 +0100 | [diff] [blame] | 1974 | struct mgmt_mode *cp = cmd->param; |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 1975 | struct cmd_lookup *match = data; |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1976 | |
Johan Hedberg | 72a734e | 2010-12-30 00:38:22 +0200 | [diff] [blame] | 1977 | if (cp->val != match->val) |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1978 | return; |
| 1979 | |
Johan Hedberg | 053f021 | 2011-01-26 13:07:10 +0200 | [diff] [blame] | 1980 | send_mode_rsp(cmd->sk, cmd->opcode, cmd->index, cp->val); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1981 | |
| 1982 | list_del(&cmd->list); |
| 1983 | |
| 1984 | if (match->sk == NULL) { |
| 1985 | match->sk = cmd->sk; |
| 1986 | sock_hold(match->sk); |
| 1987 | } |
| 1988 | |
| 1989 | mgmt_pending_free(cmd); |
Johan Hedberg | c71e97b | 2010-12-13 21:07:07 +0200 | [diff] [blame] | 1990 | } |
Johan Hedberg | 5add6af | 2010-12-16 10:00:37 +0200 | [diff] [blame] | 1991 | |
| 1992 | int mgmt_powered(u16 index, u8 powered) |
| 1993 | { |
Johan Hedberg | 72a734e | 2010-12-30 00:38:22 +0200 | [diff] [blame] | 1994 | struct mgmt_mode ev; |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 1995 | struct cmd_lookup match = { powered, NULL }; |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1996 | int ret; |
Johan Hedberg | 5add6af | 2010-12-16 10:00:37 +0200 | [diff] [blame] | 1997 | |
Johan Hedberg | 72a734e | 2010-12-30 00:38:22 +0200 | [diff] [blame] | 1998 | mgmt_pending_foreach(MGMT_OP_SET_POWERED, index, mode_rsp, &match); |
Johan Hedberg | 5add6af | 2010-12-16 10:00:37 +0200 | [diff] [blame] | 1999 | |
Johan Hedberg | 72a734e | 2010-12-30 00:38:22 +0200 | [diff] [blame] | 2000 | ev.val = powered; |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 2001 | |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 2002 | ret = mgmt_event(MGMT_EV_POWERED, index, &ev, sizeof(ev), match.sk); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 2003 | |
| 2004 | if (match.sk) |
| 2005 | sock_put(match.sk); |
| 2006 | |
| 2007 | return ret; |
Johan Hedberg | 5add6af | 2010-12-16 10:00:37 +0200 | [diff] [blame] | 2008 | } |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 2009 | |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 2010 | int mgmt_discoverable(u16 index, u8 discoverable) |
| 2011 | { |
Johan Hedberg | 72a734e | 2010-12-30 00:38:22 +0200 | [diff] [blame] | 2012 | struct mgmt_mode ev; |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 2013 | struct cmd_lookup match = { discoverable, NULL }; |
| 2014 | int ret; |
| 2015 | |
Szymon Janc | b8534e0 | 2011-03-01 16:55:34 +0100 | [diff] [blame] | 2016 | mgmt_pending_foreach(MGMT_OP_SET_DISCOVERABLE, index, mode_rsp, &match); |
Johan Hedberg | 72a734e | 2010-12-30 00:38:22 +0200 | [diff] [blame] | 2017 | |
Johan Hedberg | 72a734e | 2010-12-30 00:38:22 +0200 | [diff] [blame] | 2018 | ev.val = discoverable; |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 2019 | |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 2020 | ret = mgmt_event(MGMT_EV_DISCOVERABLE, index, &ev, sizeof(ev), |
| 2021 | match.sk); |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 2022 | |
| 2023 | if (match.sk) |
| 2024 | sock_put(match.sk); |
| 2025 | |
| 2026 | return ret; |
| 2027 | } |
Johan Hedberg | 9fbcbb4 | 2010-12-30 00:18:33 +0200 | [diff] [blame] | 2028 | |
Johan Hedberg | 9fbcbb4 | 2010-12-30 00:18:33 +0200 | [diff] [blame] | 2029 | int mgmt_connectable(u16 index, u8 connectable) |
| 2030 | { |
Johan Hedberg | 72a734e | 2010-12-30 00:38:22 +0200 | [diff] [blame] | 2031 | struct mgmt_mode ev; |
Johan Hedberg | 9fbcbb4 | 2010-12-30 00:18:33 +0200 | [diff] [blame] | 2032 | struct cmd_lookup match = { connectable, NULL }; |
| 2033 | int ret; |
| 2034 | |
Johan Hedberg | 72a734e | 2010-12-30 00:38:22 +0200 | [diff] [blame] | 2035 | mgmt_pending_foreach(MGMT_OP_SET_CONNECTABLE, index, mode_rsp, &match); |
Johan Hedberg | 9fbcbb4 | 2010-12-30 00:18:33 +0200 | [diff] [blame] | 2036 | |
Johan Hedberg | 72a734e | 2010-12-30 00:38:22 +0200 | [diff] [blame] | 2037 | ev.val = connectable; |
Johan Hedberg | 9fbcbb4 | 2010-12-30 00:18:33 +0200 | [diff] [blame] | 2038 | |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 2039 | ret = mgmt_event(MGMT_EV_CONNECTABLE, index, &ev, sizeof(ev), match.sk); |
Johan Hedberg | 9fbcbb4 | 2010-12-30 00:18:33 +0200 | [diff] [blame] | 2040 | |
| 2041 | if (match.sk) |
| 2042 | sock_put(match.sk); |
| 2043 | |
| 2044 | return ret; |
| 2045 | } |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2046 | |
Johan Hedberg | 4df378a | 2011-04-28 11:29:03 -0700 | [diff] [blame] | 2047 | int mgmt_new_key(u16 index, struct link_key *key, u8 persistent) |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2048 | { |
Vinicius Costa Gomes | 5a0a8b4 | 2011-07-08 18:31:44 -0300 | [diff] [blame] | 2049 | struct mgmt_ev_new_key *ev; |
| 2050 | int err, total; |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2051 | |
Vinicius Costa Gomes | 5a0a8b4 | 2011-07-08 18:31:44 -0300 | [diff] [blame] | 2052 | total = sizeof(struct mgmt_ev_new_key) + key->dlen; |
| 2053 | ev = kzalloc(total, GFP_ATOMIC); |
| 2054 | if (!ev) |
| 2055 | return -ENOMEM; |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2056 | |
Vinicius Costa Gomes | 5a0a8b4 | 2011-07-08 18:31:44 -0300 | [diff] [blame] | 2057 | bacpy(&ev->key.bdaddr, &key->bdaddr); |
| 2058 | ev->key.type = key->type; |
| 2059 | memcpy(ev->key.val, key->val, 16); |
| 2060 | ev->key.pin_len = key->pin_len; |
| 2061 | ev->key.dlen = key->dlen; |
| 2062 | ev->store_hint = persistent; |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2063 | |
Vinicius Costa Gomes | 5a0a8b4 | 2011-07-08 18:31:44 -0300 | [diff] [blame] | 2064 | memcpy(ev->key.data, key->data, key->dlen); |
| 2065 | |
| 2066 | err = mgmt_event(MGMT_EV_NEW_KEY, index, ev, total, NULL); |
| 2067 | |
| 2068 | kfree(ev); |
| 2069 | |
| 2070 | return err; |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2071 | } |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 2072 | |
Vinicius Costa Gomes | cfafccf | 2011-08-19 21:06:56 -0300 | [diff] [blame] | 2073 | int mgmt_connected(u16 index, bdaddr_t *bdaddr, u8 link_type) |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 2074 | { |
| 2075 | struct mgmt_ev_connected ev; |
| 2076 | |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 2077 | bacpy(&ev.bdaddr, bdaddr); |
Vinicius Costa Gomes | cfafccf | 2011-08-19 21:06:56 -0300 | [diff] [blame] | 2078 | ev.link_type = link_type; |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 2079 | |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 2080 | return mgmt_event(MGMT_EV_CONNECTED, index, &ev, sizeof(ev), NULL); |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 2081 | } |
| 2082 | |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2083 | static void disconnect_rsp(struct pending_cmd *cmd, void *data) |
| 2084 | { |
Szymon Janc | c68fb7f | 2011-03-22 13:12:19 +0100 | [diff] [blame] | 2085 | struct mgmt_cp_disconnect *cp = cmd->param; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2086 | struct sock **sk = data; |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 2087 | struct mgmt_rp_disconnect rp; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2088 | |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 2089 | bacpy(&rp.bdaddr, &cp->bdaddr); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2090 | |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 2091 | cmd_complete(cmd->sk, cmd->index, MGMT_OP_DISCONNECT, &rp, sizeof(rp)); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2092 | |
| 2093 | *sk = cmd->sk; |
| 2094 | sock_hold(*sk); |
| 2095 | |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 2096 | mgmt_pending_remove(cmd); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2097 | } |
| 2098 | |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 2099 | int mgmt_disconnected(u16 index, bdaddr_t *bdaddr) |
| 2100 | { |
| 2101 | struct mgmt_ev_disconnected ev; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2102 | struct sock *sk = NULL; |
| 2103 | int err; |
| 2104 | |
| 2105 | mgmt_pending_foreach(MGMT_OP_DISCONNECT, index, disconnect_rsp, &sk); |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 2106 | |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 2107 | bacpy(&ev.bdaddr, bdaddr); |
| 2108 | |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 2109 | err = mgmt_event(MGMT_EV_DISCONNECTED, index, &ev, sizeof(ev), sk); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2110 | |
| 2111 | if (sk) |
| 2112 | sock_put(sk); |
| 2113 | |
| 2114 | return err; |
| 2115 | } |
| 2116 | |
| 2117 | int mgmt_disconnect_failed(u16 index) |
| 2118 | { |
| 2119 | struct pending_cmd *cmd; |
| 2120 | int err; |
| 2121 | |
| 2122 | cmd = mgmt_pending_find(MGMT_OP_DISCONNECT, index); |
| 2123 | if (!cmd) |
| 2124 | return -ENOENT; |
| 2125 | |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 2126 | err = cmd_status(cmd->sk, index, MGMT_OP_DISCONNECT, EIO); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2127 | |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 2128 | mgmt_pending_remove(cmd); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2129 | |
| 2130 | return err; |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 2131 | } |
Johan Hedberg | 17d5c04 | 2011-01-22 06:09:08 +0200 | [diff] [blame] | 2132 | |
| 2133 | int mgmt_connect_failed(u16 index, bdaddr_t *bdaddr, u8 status) |
| 2134 | { |
| 2135 | struct mgmt_ev_connect_failed ev; |
| 2136 | |
Johan Hedberg | 17d5c04 | 2011-01-22 06:09:08 +0200 | [diff] [blame] | 2137 | bacpy(&ev.bdaddr, bdaddr); |
| 2138 | ev.status = status; |
| 2139 | |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 2140 | return mgmt_event(MGMT_EV_CONNECT_FAILED, index, &ev, sizeof(ev), NULL); |
Johan Hedberg | 17d5c04 | 2011-01-22 06:09:08 +0200 | [diff] [blame] | 2141 | } |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2142 | |
Waldemar Rymarkiewicz | a770bb5 | 2011-04-28 12:07:59 +0200 | [diff] [blame] | 2143 | int mgmt_pin_code_request(u16 index, bdaddr_t *bdaddr, u8 secure) |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2144 | { |
| 2145 | struct mgmt_ev_pin_code_request ev; |
| 2146 | |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2147 | bacpy(&ev.bdaddr, bdaddr); |
Waldemar Rymarkiewicz | a770bb5 | 2011-04-28 12:07:59 +0200 | [diff] [blame] | 2148 | ev.secure = secure; |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2149 | |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 2150 | return mgmt_event(MGMT_EV_PIN_CODE_REQUEST, index, &ev, sizeof(ev), |
| 2151 | NULL); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2152 | } |
| 2153 | |
| 2154 | int mgmt_pin_code_reply_complete(u16 index, bdaddr_t *bdaddr, u8 status) |
| 2155 | { |
| 2156 | struct pending_cmd *cmd; |
Johan Hedberg | ac56fb1 | 2011-02-19 12:05:59 -0300 | [diff] [blame] | 2157 | struct mgmt_rp_pin_code_reply rp; |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2158 | int err; |
| 2159 | |
| 2160 | cmd = mgmt_pending_find(MGMT_OP_PIN_CODE_REPLY, index); |
| 2161 | if (!cmd) |
| 2162 | return -ENOENT; |
| 2163 | |
Johan Hedberg | ac56fb1 | 2011-02-19 12:05:59 -0300 | [diff] [blame] | 2164 | bacpy(&rp.bdaddr, bdaddr); |
| 2165 | rp.status = status; |
| 2166 | |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 2167 | err = cmd_complete(cmd->sk, index, MGMT_OP_PIN_CODE_REPLY, &rp, |
| 2168 | sizeof(rp)); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2169 | |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 2170 | mgmt_pending_remove(cmd); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2171 | |
| 2172 | return err; |
| 2173 | } |
| 2174 | |
| 2175 | int mgmt_pin_code_neg_reply_complete(u16 index, bdaddr_t *bdaddr, u8 status) |
| 2176 | { |
| 2177 | struct pending_cmd *cmd; |
Johan Hedberg | ac56fb1 | 2011-02-19 12:05:59 -0300 | [diff] [blame] | 2178 | struct mgmt_rp_pin_code_reply rp; |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2179 | int err; |
| 2180 | |
| 2181 | cmd = mgmt_pending_find(MGMT_OP_PIN_CODE_NEG_REPLY, index); |
| 2182 | if (!cmd) |
| 2183 | return -ENOENT; |
| 2184 | |
Johan Hedberg | ac56fb1 | 2011-02-19 12:05:59 -0300 | [diff] [blame] | 2185 | bacpy(&rp.bdaddr, bdaddr); |
| 2186 | rp.status = status; |
| 2187 | |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 2188 | err = cmd_complete(cmd->sk, index, MGMT_OP_PIN_CODE_NEG_REPLY, &rp, |
| 2189 | sizeof(rp)); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2190 | |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 2191 | mgmt_pending_remove(cmd); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2192 | |
| 2193 | return err; |
| 2194 | } |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 2195 | |
Johan Hedberg | 55bc1a3 | 2011-04-28 11:28:56 -0700 | [diff] [blame] | 2196 | int mgmt_user_confirm_request(u16 index, bdaddr_t *bdaddr, __le32 value, |
| 2197 | u8 confirm_hint) |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 2198 | { |
| 2199 | struct mgmt_ev_user_confirm_request ev; |
| 2200 | |
| 2201 | BT_DBG("hci%u", index); |
| 2202 | |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 2203 | bacpy(&ev.bdaddr, bdaddr); |
Johan Hedberg | 55bc1a3 | 2011-04-28 11:28:56 -0700 | [diff] [blame] | 2204 | ev.confirm_hint = confirm_hint; |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 2205 | put_unaligned_le32(value, &ev.value); |
| 2206 | |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 2207 | return mgmt_event(MGMT_EV_USER_CONFIRM_REQUEST, index, &ev, sizeof(ev), |
| 2208 | NULL); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 2209 | } |
| 2210 | |
| 2211 | static int confirm_reply_complete(u16 index, bdaddr_t *bdaddr, u8 status, |
| 2212 | u8 opcode) |
| 2213 | { |
| 2214 | struct pending_cmd *cmd; |
| 2215 | struct mgmt_rp_user_confirm_reply rp; |
| 2216 | int err; |
| 2217 | |
| 2218 | cmd = mgmt_pending_find(opcode, index); |
| 2219 | if (!cmd) |
| 2220 | return -ENOENT; |
| 2221 | |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 2222 | bacpy(&rp.bdaddr, bdaddr); |
| 2223 | rp.status = status; |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 2224 | err = cmd_complete(cmd->sk, index, opcode, &rp, sizeof(rp)); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 2225 | |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 2226 | mgmt_pending_remove(cmd); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 2227 | |
| 2228 | return err; |
| 2229 | } |
| 2230 | |
| 2231 | int mgmt_user_confirm_reply_complete(u16 index, bdaddr_t *bdaddr, u8 status) |
| 2232 | { |
| 2233 | return confirm_reply_complete(index, bdaddr, status, |
| 2234 | MGMT_OP_USER_CONFIRM_REPLY); |
| 2235 | } |
| 2236 | |
Szymon Janc | b8534e0 | 2011-03-01 16:55:34 +0100 | [diff] [blame] | 2237 | 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] | 2238 | { |
| 2239 | return confirm_reply_complete(index, bdaddr, status, |
| 2240 | MGMT_OP_USER_CONFIRM_NEG_REPLY); |
| 2241 | } |
Johan Hedberg | 2a61169 | 2011-02-19 12:06:00 -0300 | [diff] [blame] | 2242 | |
| 2243 | int mgmt_auth_failed(u16 index, bdaddr_t *bdaddr, u8 status) |
| 2244 | { |
| 2245 | struct mgmt_ev_auth_failed ev; |
| 2246 | |
Johan Hedberg | 2a61169 | 2011-02-19 12:06:00 -0300 | [diff] [blame] | 2247 | bacpy(&ev.bdaddr, bdaddr); |
| 2248 | ev.status = status; |
| 2249 | |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 2250 | return mgmt_event(MGMT_EV_AUTH_FAILED, index, &ev, sizeof(ev), NULL); |
Johan Hedberg | 2a61169 | 2011-02-19 12:06:00 -0300 | [diff] [blame] | 2251 | } |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 2252 | |
| 2253 | int mgmt_set_local_name_complete(u16 index, u8 *name, u8 status) |
| 2254 | { |
| 2255 | struct pending_cmd *cmd; |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 2256 | struct hci_dev *hdev; |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 2257 | struct mgmt_cp_set_local_name ev; |
| 2258 | int err; |
| 2259 | |
| 2260 | memset(&ev, 0, sizeof(ev)); |
| 2261 | memcpy(ev.name, name, HCI_MAX_NAME_LENGTH); |
| 2262 | |
| 2263 | cmd = mgmt_pending_find(MGMT_OP_SET_LOCAL_NAME, index); |
| 2264 | if (!cmd) |
| 2265 | goto send_event; |
| 2266 | |
| 2267 | if (status) { |
| 2268 | err = cmd_status(cmd->sk, index, MGMT_OP_SET_LOCAL_NAME, EIO); |
| 2269 | goto failed; |
| 2270 | } |
| 2271 | |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 2272 | hdev = hci_dev_get(index); |
| 2273 | if (hdev) { |
| 2274 | hci_dev_lock_bh(hdev); |
| 2275 | update_eir(hdev); |
| 2276 | hci_dev_unlock_bh(hdev); |
| 2277 | hci_dev_put(hdev); |
| 2278 | } |
| 2279 | |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 2280 | err = cmd_complete(cmd->sk, index, MGMT_OP_SET_LOCAL_NAME, &ev, |
| 2281 | sizeof(ev)); |
| 2282 | if (err < 0) |
| 2283 | goto failed; |
| 2284 | |
| 2285 | send_event: |
| 2286 | err = mgmt_event(MGMT_EV_LOCAL_NAME_CHANGED, index, &ev, sizeof(ev), |
| 2287 | cmd ? cmd->sk : NULL); |
| 2288 | |
| 2289 | failed: |
| 2290 | if (cmd) |
| 2291 | mgmt_pending_remove(cmd); |
| 2292 | return err; |
| 2293 | } |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 2294 | |
| 2295 | int mgmt_read_local_oob_data_reply_complete(u16 index, u8 *hash, u8 *randomizer, |
| 2296 | u8 status) |
| 2297 | { |
| 2298 | struct pending_cmd *cmd; |
| 2299 | int err; |
| 2300 | |
| 2301 | BT_DBG("hci%u status %u", index, status); |
| 2302 | |
| 2303 | cmd = mgmt_pending_find(MGMT_OP_READ_LOCAL_OOB_DATA, index); |
| 2304 | if (!cmd) |
| 2305 | return -ENOENT; |
| 2306 | |
| 2307 | if (status) { |
| 2308 | err = cmd_status(cmd->sk, index, MGMT_OP_READ_LOCAL_OOB_DATA, |
| 2309 | EIO); |
| 2310 | } else { |
| 2311 | struct mgmt_rp_read_local_oob_data rp; |
| 2312 | |
| 2313 | memcpy(rp.hash, hash, sizeof(rp.hash)); |
| 2314 | memcpy(rp.randomizer, randomizer, sizeof(rp.randomizer)); |
| 2315 | |
| 2316 | err = cmd_complete(cmd->sk, index, MGMT_OP_READ_LOCAL_OOB_DATA, |
| 2317 | &rp, sizeof(rp)); |
| 2318 | } |
| 2319 | |
| 2320 | mgmt_pending_remove(cmd); |
| 2321 | |
| 2322 | return err; |
| 2323 | } |
Johan Hedberg | e17acd4 | 2011-03-30 23:57:16 +0300 | [diff] [blame] | 2324 | |
| 2325 | int mgmt_device_found(u16 index, bdaddr_t *bdaddr, u8 *dev_class, s8 rssi, |
| 2326 | u8 *eir) |
| 2327 | { |
| 2328 | struct mgmt_ev_device_found ev; |
| 2329 | |
| 2330 | memset(&ev, 0, sizeof(ev)); |
| 2331 | |
| 2332 | bacpy(&ev.bdaddr, bdaddr); |
| 2333 | memcpy(ev.dev_class, dev_class, sizeof(ev.dev_class)); |
| 2334 | ev.rssi = rssi; |
| 2335 | |
| 2336 | if (eir) |
| 2337 | memcpy(ev.eir, eir, sizeof(ev.eir)); |
| 2338 | |
| 2339 | return mgmt_event(MGMT_EV_DEVICE_FOUND, index, &ev, sizeof(ev), NULL); |
| 2340 | } |
Johan Hedberg | a88a965 | 2011-03-30 13:18:12 +0300 | [diff] [blame] | 2341 | |
| 2342 | int mgmt_remote_name(u16 index, bdaddr_t *bdaddr, u8 *name) |
| 2343 | { |
| 2344 | struct mgmt_ev_remote_name ev; |
| 2345 | |
| 2346 | memset(&ev, 0, sizeof(ev)); |
| 2347 | |
| 2348 | bacpy(&ev.bdaddr, bdaddr); |
| 2349 | memcpy(ev.name, name, HCI_MAX_NAME_LENGTH); |
| 2350 | |
| 2351 | return mgmt_event(MGMT_EV_REMOTE_NAME, index, &ev, sizeof(ev), NULL); |
| 2352 | } |
Johan Hedberg | 314b238 | 2011-04-27 10:29:57 -0400 | [diff] [blame] | 2353 | |
| 2354 | int mgmt_discovering(u16 index, u8 discovering) |
| 2355 | { |
| 2356 | return mgmt_event(MGMT_EV_DISCOVERING, index, &discovering, |
| 2357 | sizeof(discovering), NULL); |
| 2358 | } |