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 | |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 25 | #include <linux/kernel.h> |
Szymon Janc | 7235975 | 2011-02-17 14:16:32 +0100 | [diff] [blame] | 26 | #include <linux/uaccess.h> |
Paul Gortmaker | 3a9a231 | 2011-05-27 09:12:25 -0400 | [diff] [blame] | 27 | #include <linux/module.h> |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 28 | #include <asm/unaligned.h> |
| 29 | |
| 30 | #include <net/bluetooth/bluetooth.h> |
| 31 | #include <net/bluetooth/hci_core.h> |
| 32 | #include <net/bluetooth/mgmt.h> |
Brian Gix | 5fe57d9 | 2011-12-21 16:12:13 -0800 | [diff] [blame] | 33 | #include <net/bluetooth/smp.h> |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 34 | |
Johan Hedberg | 02d9812 | 2010-12-13 21:07:04 +0200 | [diff] [blame] | 35 | #define MGMT_VERSION 0 |
| 36 | #define MGMT_REVISION 1 |
| 37 | |
Johan Hedberg | e70bb2e | 2012-02-13 16:59:33 +0200 | [diff] [blame] | 38 | static const u16 mgmt_commands[] = { |
| 39 | MGMT_OP_READ_INDEX_LIST, |
| 40 | MGMT_OP_READ_INFO, |
| 41 | MGMT_OP_SET_POWERED, |
| 42 | MGMT_OP_SET_DISCOVERABLE, |
| 43 | MGMT_OP_SET_CONNECTABLE, |
| 44 | MGMT_OP_SET_FAST_CONNECTABLE, |
| 45 | MGMT_OP_SET_PAIRABLE, |
| 46 | MGMT_OP_SET_LINK_SECURITY, |
| 47 | MGMT_OP_SET_SSP, |
| 48 | MGMT_OP_SET_HS, |
| 49 | MGMT_OP_SET_LE, |
| 50 | MGMT_OP_SET_DEV_CLASS, |
| 51 | MGMT_OP_SET_LOCAL_NAME, |
| 52 | MGMT_OP_ADD_UUID, |
| 53 | MGMT_OP_REMOVE_UUID, |
| 54 | MGMT_OP_LOAD_LINK_KEYS, |
| 55 | MGMT_OP_LOAD_LONG_TERM_KEYS, |
| 56 | MGMT_OP_DISCONNECT, |
| 57 | MGMT_OP_GET_CONNECTIONS, |
| 58 | MGMT_OP_PIN_CODE_REPLY, |
| 59 | MGMT_OP_PIN_CODE_NEG_REPLY, |
| 60 | MGMT_OP_SET_IO_CAPABILITY, |
| 61 | MGMT_OP_PAIR_DEVICE, |
| 62 | MGMT_OP_CANCEL_PAIR_DEVICE, |
| 63 | MGMT_OP_UNPAIR_DEVICE, |
| 64 | MGMT_OP_USER_CONFIRM_REPLY, |
| 65 | MGMT_OP_USER_CONFIRM_NEG_REPLY, |
| 66 | MGMT_OP_USER_PASSKEY_REPLY, |
| 67 | MGMT_OP_USER_PASSKEY_NEG_REPLY, |
| 68 | MGMT_OP_READ_LOCAL_OOB_DATA, |
| 69 | MGMT_OP_ADD_REMOTE_OOB_DATA, |
| 70 | MGMT_OP_REMOVE_REMOTE_OOB_DATA, |
| 71 | MGMT_OP_START_DISCOVERY, |
| 72 | MGMT_OP_STOP_DISCOVERY, |
| 73 | MGMT_OP_CONFIRM_NAME, |
| 74 | MGMT_OP_BLOCK_DEVICE, |
| 75 | MGMT_OP_UNBLOCK_DEVICE, |
| 76 | }; |
| 77 | |
| 78 | static const u16 mgmt_events[] = { |
| 79 | MGMT_EV_CONTROLLER_ERROR, |
| 80 | MGMT_EV_INDEX_ADDED, |
| 81 | MGMT_EV_INDEX_REMOVED, |
| 82 | MGMT_EV_NEW_SETTINGS, |
| 83 | MGMT_EV_CLASS_OF_DEV_CHANGED, |
| 84 | MGMT_EV_LOCAL_NAME_CHANGED, |
| 85 | MGMT_EV_NEW_LINK_KEY, |
| 86 | MGMT_EV_NEW_LONG_TERM_KEY, |
| 87 | MGMT_EV_DEVICE_CONNECTED, |
| 88 | MGMT_EV_DEVICE_DISCONNECTED, |
| 89 | MGMT_EV_CONNECT_FAILED, |
| 90 | MGMT_EV_PIN_CODE_REQUEST, |
| 91 | MGMT_EV_USER_CONFIRM_REQUEST, |
| 92 | MGMT_EV_USER_PASSKEY_REQUEST, |
| 93 | MGMT_EV_AUTH_FAILED, |
| 94 | MGMT_EV_DEVICE_FOUND, |
| 95 | MGMT_EV_DISCOVERING, |
| 96 | MGMT_EV_DEVICE_BLOCKED, |
| 97 | MGMT_EV_DEVICE_UNBLOCKED, |
| 98 | MGMT_EV_DEVICE_UNPAIRED, |
| 99 | }; |
| 100 | |
Andre Guedes | 3fd2415 | 2012-02-03 17:48:01 -0300 | [diff] [blame] | 101 | /* |
| 102 | * These LE scan and inquiry parameters were chosen according to LE General |
| 103 | * Discovery Procedure specification. |
| 104 | */ |
| 105 | #define LE_SCAN_TYPE 0x01 |
| 106 | #define LE_SCAN_WIN 0x12 |
| 107 | #define LE_SCAN_INT 0x12 |
| 108 | #define LE_SCAN_TIMEOUT_LE_ONLY 10240 /* TGAP(gen_disc_scan_min) */ |
| 109 | |
Andre Guedes | e877752 | 2012-02-03 17:48:02 -0300 | [diff] [blame] | 110 | #define INQUIRY_LEN_BREDR 0x08 /* TGAP(100) */ |
Andre Guedes | 2519a1f | 2011-11-07 11:45:24 -0300 | [diff] [blame] | 111 | |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 112 | #define SERVICE_CACHE_TIMEOUT (5 * 1000) |
| 113 | |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 114 | struct pending_cmd { |
| 115 | struct list_head list; |
Johan Hedberg | fc2f4b1 | 2011-11-09 13:58:56 +0200 | [diff] [blame] | 116 | u16 opcode; |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 117 | int index; |
Szymon Janc | c68fb7f | 2011-03-22 13:12:19 +0100 | [diff] [blame] | 118 | void *param; |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 119 | struct sock *sk; |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 120 | void *user_data; |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 121 | }; |
| 122 | |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 123 | /* HCI to MGMT error code conversion table */ |
| 124 | static u8 mgmt_status_table[] = { |
| 125 | MGMT_STATUS_SUCCESS, |
| 126 | MGMT_STATUS_UNKNOWN_COMMAND, /* Unknown Command */ |
| 127 | MGMT_STATUS_NOT_CONNECTED, /* No Connection */ |
| 128 | MGMT_STATUS_FAILED, /* Hardware Failure */ |
| 129 | MGMT_STATUS_CONNECT_FAILED, /* Page Timeout */ |
| 130 | MGMT_STATUS_AUTH_FAILED, /* Authentication Failed */ |
| 131 | MGMT_STATUS_NOT_PAIRED, /* PIN or Key Missing */ |
| 132 | MGMT_STATUS_NO_RESOURCES, /* Memory Full */ |
| 133 | MGMT_STATUS_TIMEOUT, /* Connection Timeout */ |
| 134 | MGMT_STATUS_NO_RESOURCES, /* Max Number of Connections */ |
| 135 | MGMT_STATUS_NO_RESOURCES, /* Max Number of SCO Connections */ |
| 136 | MGMT_STATUS_ALREADY_CONNECTED, /* ACL Connection Exists */ |
| 137 | MGMT_STATUS_BUSY, /* Command Disallowed */ |
| 138 | MGMT_STATUS_NO_RESOURCES, /* Rejected Limited Resources */ |
| 139 | MGMT_STATUS_REJECTED, /* Rejected Security */ |
| 140 | MGMT_STATUS_REJECTED, /* Rejected Personal */ |
| 141 | MGMT_STATUS_TIMEOUT, /* Host Timeout */ |
| 142 | MGMT_STATUS_NOT_SUPPORTED, /* Unsupported Feature */ |
| 143 | MGMT_STATUS_INVALID_PARAMS, /* Invalid Parameters */ |
| 144 | MGMT_STATUS_DISCONNECTED, /* OE User Ended Connection */ |
| 145 | MGMT_STATUS_NO_RESOURCES, /* OE Low Resources */ |
| 146 | MGMT_STATUS_DISCONNECTED, /* OE Power Off */ |
| 147 | MGMT_STATUS_DISCONNECTED, /* Connection Terminated */ |
| 148 | MGMT_STATUS_BUSY, /* Repeated Attempts */ |
| 149 | MGMT_STATUS_REJECTED, /* Pairing Not Allowed */ |
| 150 | MGMT_STATUS_FAILED, /* Unknown LMP PDU */ |
| 151 | MGMT_STATUS_NOT_SUPPORTED, /* Unsupported Remote Feature */ |
| 152 | MGMT_STATUS_REJECTED, /* SCO Offset Rejected */ |
| 153 | MGMT_STATUS_REJECTED, /* SCO Interval Rejected */ |
| 154 | MGMT_STATUS_REJECTED, /* Air Mode Rejected */ |
| 155 | MGMT_STATUS_INVALID_PARAMS, /* Invalid LMP Parameters */ |
| 156 | MGMT_STATUS_FAILED, /* Unspecified Error */ |
| 157 | MGMT_STATUS_NOT_SUPPORTED, /* Unsupported LMP Parameter Value */ |
| 158 | MGMT_STATUS_FAILED, /* Role Change Not Allowed */ |
| 159 | MGMT_STATUS_TIMEOUT, /* LMP Response Timeout */ |
| 160 | MGMT_STATUS_FAILED, /* LMP Error Transaction Collision */ |
| 161 | MGMT_STATUS_FAILED, /* LMP PDU Not Allowed */ |
| 162 | MGMT_STATUS_REJECTED, /* Encryption Mode Not Accepted */ |
| 163 | MGMT_STATUS_FAILED, /* Unit Link Key Used */ |
| 164 | MGMT_STATUS_NOT_SUPPORTED, /* QoS Not Supported */ |
| 165 | MGMT_STATUS_TIMEOUT, /* Instant Passed */ |
| 166 | MGMT_STATUS_NOT_SUPPORTED, /* Pairing Not Supported */ |
| 167 | MGMT_STATUS_FAILED, /* Transaction Collision */ |
| 168 | MGMT_STATUS_INVALID_PARAMS, /* Unacceptable Parameter */ |
| 169 | MGMT_STATUS_REJECTED, /* QoS Rejected */ |
| 170 | MGMT_STATUS_NOT_SUPPORTED, /* Classification Not Supported */ |
| 171 | MGMT_STATUS_REJECTED, /* Insufficient Security */ |
| 172 | MGMT_STATUS_INVALID_PARAMS, /* Parameter Out Of Range */ |
| 173 | MGMT_STATUS_BUSY, /* Role Switch Pending */ |
| 174 | MGMT_STATUS_FAILED, /* Slot Violation */ |
| 175 | MGMT_STATUS_FAILED, /* Role Switch Failed */ |
| 176 | MGMT_STATUS_INVALID_PARAMS, /* EIR Too Large */ |
| 177 | MGMT_STATUS_NOT_SUPPORTED, /* Simple Pairing Not Supported */ |
| 178 | MGMT_STATUS_BUSY, /* Host Busy Pairing */ |
| 179 | MGMT_STATUS_REJECTED, /* Rejected, No Suitable Channel */ |
| 180 | MGMT_STATUS_BUSY, /* Controller Busy */ |
| 181 | MGMT_STATUS_INVALID_PARAMS, /* Unsuitable Connection Interval */ |
| 182 | MGMT_STATUS_TIMEOUT, /* Directed Advertising Timeout */ |
| 183 | MGMT_STATUS_AUTH_FAILED, /* Terminated Due to MIC Failure */ |
| 184 | MGMT_STATUS_CONNECT_FAILED, /* Connection Establishment Failed */ |
| 185 | MGMT_STATUS_CONNECT_FAILED, /* MAC Connection Failed */ |
| 186 | }; |
| 187 | |
| 188 | static u8 mgmt_status(u8 hci_status) |
| 189 | { |
| 190 | if (hci_status < ARRAY_SIZE(mgmt_status_table)) |
| 191 | return mgmt_status_table[hci_status]; |
| 192 | |
| 193 | return MGMT_STATUS_FAILED; |
| 194 | } |
| 195 | |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 196 | 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] | 197 | { |
| 198 | struct sk_buff *skb; |
| 199 | struct mgmt_hdr *hdr; |
| 200 | struct mgmt_ev_cmd_status *ev; |
Gustavo F. Padovan | 56b7d13 | 2011-10-14 19:20:01 -0300 | [diff] [blame] | 201 | int err; |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 202 | |
Szymon Janc | 34eb525 | 2011-02-28 14:10:08 +0100 | [diff] [blame] | 203 | 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] | 204 | |
| 205 | skb = alloc_skb(sizeof(*hdr) + sizeof(*ev), GFP_ATOMIC); |
| 206 | if (!skb) |
| 207 | return -ENOMEM; |
| 208 | |
| 209 | hdr = (void *) skb_put(skb, sizeof(*hdr)); |
| 210 | |
| 211 | hdr->opcode = cpu_to_le16(MGMT_EV_CMD_STATUS); |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 212 | hdr->index = cpu_to_le16(index); |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 213 | hdr->len = cpu_to_le16(sizeof(*ev)); |
| 214 | |
| 215 | ev = (void *) skb_put(skb, sizeof(*ev)); |
| 216 | ev->status = status; |
| 217 | put_unaligned_le16(cmd, &ev->opcode); |
| 218 | |
Gustavo F. Padovan | 56b7d13 | 2011-10-14 19:20:01 -0300 | [diff] [blame] | 219 | err = sock_queue_rcv_skb(sk, skb); |
| 220 | if (err < 0) |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 221 | kfree_skb(skb); |
| 222 | |
Gustavo F. Padovan | 56b7d13 | 2011-10-14 19:20:01 -0300 | [diff] [blame] | 223 | return err; |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 224 | } |
| 225 | |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 226 | static int cmd_complete(struct sock *sk, u16 index, u16 cmd, void *rp, |
| 227 | size_t rp_len) |
Johan Hedberg | 02d9812 | 2010-12-13 21:07:04 +0200 | [diff] [blame] | 228 | { |
| 229 | struct sk_buff *skb; |
| 230 | struct mgmt_hdr *hdr; |
| 231 | struct mgmt_ev_cmd_complete *ev; |
Gustavo F. Padovan | 56b7d13 | 2011-10-14 19:20:01 -0300 | [diff] [blame] | 232 | int err; |
Johan Hedberg | 02d9812 | 2010-12-13 21:07:04 +0200 | [diff] [blame] | 233 | |
| 234 | BT_DBG("sock %p", sk); |
| 235 | |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 236 | skb = alloc_skb(sizeof(*hdr) + sizeof(*ev) + rp_len, GFP_ATOMIC); |
Johan Hedberg | 02d9812 | 2010-12-13 21:07:04 +0200 | [diff] [blame] | 237 | if (!skb) |
| 238 | return -ENOMEM; |
| 239 | |
| 240 | hdr = (void *) skb_put(skb, sizeof(*hdr)); |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 241 | |
Johan Hedberg | 02d9812 | 2010-12-13 21:07:04 +0200 | [diff] [blame] | 242 | hdr->opcode = cpu_to_le16(MGMT_EV_CMD_COMPLETE); |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 243 | hdr->index = cpu_to_le16(index); |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 244 | hdr->len = cpu_to_le16(sizeof(*ev) + rp_len); |
Johan Hedberg | 02d9812 | 2010-12-13 21:07:04 +0200 | [diff] [blame] | 245 | |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 246 | ev = (void *) skb_put(skb, sizeof(*ev) + rp_len); |
| 247 | put_unaligned_le16(cmd, &ev->opcode); |
Szymon Janc | 8020c16 | 2011-02-28 14:09:50 +0100 | [diff] [blame] | 248 | |
| 249 | if (rp) |
| 250 | memcpy(ev->data, rp, rp_len); |
Johan Hedberg | 02d9812 | 2010-12-13 21:07:04 +0200 | [diff] [blame] | 251 | |
Gustavo F. Padovan | 56b7d13 | 2011-10-14 19:20:01 -0300 | [diff] [blame] | 252 | err = sock_queue_rcv_skb(sk, skb); |
| 253 | if (err < 0) |
Johan Hedberg | 02d9812 | 2010-12-13 21:07:04 +0200 | [diff] [blame] | 254 | kfree_skb(skb); |
| 255 | |
Gustavo F. Padovan | 56b7d13 | 2011-10-14 19:20:01 -0300 | [diff] [blame] | 256 | return err;; |
Johan Hedberg | 02d9812 | 2010-12-13 21:07:04 +0200 | [diff] [blame] | 257 | } |
| 258 | |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 259 | static int read_version(struct sock *sk) |
| 260 | { |
| 261 | struct mgmt_rp_read_version rp; |
| 262 | |
| 263 | BT_DBG("sock %p", sk); |
| 264 | |
| 265 | rp.version = MGMT_VERSION; |
| 266 | put_unaligned_le16(MGMT_REVISION, &rp.revision); |
| 267 | |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 268 | return cmd_complete(sk, MGMT_INDEX_NONE, MGMT_OP_READ_VERSION, &rp, |
| 269 | sizeof(rp)); |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 270 | } |
| 271 | |
Johan Hedberg | e70bb2e | 2012-02-13 16:59:33 +0200 | [diff] [blame] | 272 | static int read_commands(struct sock *sk) |
| 273 | { |
| 274 | struct mgmt_rp_read_commands *rp; |
| 275 | u16 num_commands = ARRAY_SIZE(mgmt_commands); |
| 276 | u16 num_events = ARRAY_SIZE(mgmt_events); |
| 277 | u16 *opcode; |
| 278 | size_t rp_size; |
| 279 | int i, err; |
| 280 | |
| 281 | BT_DBG("sock %p", sk); |
| 282 | |
| 283 | rp_size = sizeof(*rp) + ((num_commands + num_events) * sizeof(u16)); |
| 284 | |
| 285 | rp = kmalloc(rp_size, GFP_KERNEL); |
| 286 | if (!rp) |
| 287 | return -ENOMEM; |
| 288 | |
| 289 | put_unaligned_le16(num_commands, &rp->num_commands); |
| 290 | put_unaligned_le16(num_events, &rp->num_events); |
| 291 | |
| 292 | for (i = 0, opcode = rp->opcodes; i < num_commands; i++, opcode++) |
| 293 | put_unaligned_le16(mgmt_commands[i], opcode); |
| 294 | |
| 295 | for (i = 0; i < num_events; i++, opcode++) |
| 296 | put_unaligned_le16(mgmt_events[i], opcode); |
| 297 | |
| 298 | err = cmd_complete(sk, MGMT_INDEX_NONE, MGMT_OP_READ_COMMANDS, rp, |
| 299 | rp_size); |
| 300 | kfree(rp); |
| 301 | |
| 302 | return err; |
| 303 | } |
| 304 | |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 305 | static int read_index_list(struct sock *sk) |
| 306 | { |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 307 | struct mgmt_rp_read_index_list *rp; |
| 308 | struct list_head *p; |
Luiz Augusto von Dentz | 8035ded | 2011-11-01 10:58:56 +0200 | [diff] [blame] | 309 | struct hci_dev *d; |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 310 | size_t rp_len; |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 311 | u16 count; |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 312 | int i, err; |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 313 | |
| 314 | BT_DBG("sock %p", sk); |
| 315 | |
| 316 | read_lock(&hci_dev_list_lock); |
| 317 | |
| 318 | count = 0; |
| 319 | list_for_each(p, &hci_dev_list) { |
| 320 | count++; |
| 321 | } |
| 322 | |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 323 | rp_len = sizeof(*rp) + (2 * count); |
| 324 | rp = kmalloc(rp_len, GFP_ATOMIC); |
| 325 | if (!rp) { |
Jesper Juhl | b2c60d4 | 2011-01-14 00:18:49 +0100 | [diff] [blame] | 326 | read_unlock(&hci_dev_list_lock); |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 327 | return -ENOMEM; |
Jesper Juhl | b2c60d4 | 2011-01-14 00:18:49 +0100 | [diff] [blame] | 328 | } |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 329 | |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 330 | put_unaligned_le16(count, &rp->num_controllers); |
| 331 | |
| 332 | i = 0; |
Luiz Augusto von Dentz | 8035ded | 2011-11-01 10:58:56 +0200 | [diff] [blame] | 333 | list_for_each_entry(d, &hci_dev_list, list) { |
Johan Hedberg | a8b2d5c | 2012-01-08 23:11:15 +0200 | [diff] [blame] | 334 | if (test_and_clear_bit(HCI_AUTO_OFF, &d->dev_flags)) |
Johan Hedberg | e0f9309 | 2011-11-09 01:44:22 +0200 | [diff] [blame] | 335 | cancel_delayed_work(&d->power_off); |
Johan Hedberg | ab81cbf | 2010-12-15 13:53:18 +0200 | [diff] [blame] | 336 | |
Johan Hedberg | a8b2d5c | 2012-01-08 23:11:15 +0200 | [diff] [blame] | 337 | if (test_bit(HCI_SETUP, &d->dev_flags)) |
Johan Hedberg | ab81cbf | 2010-12-15 13:53:18 +0200 | [diff] [blame] | 338 | continue; |
| 339 | |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 340 | put_unaligned_le16(d->id, &rp->index[i++]); |
| 341 | BT_DBG("Added hci%u", d->id); |
| 342 | } |
| 343 | |
| 344 | read_unlock(&hci_dev_list_lock); |
| 345 | |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 346 | err = cmd_complete(sk, MGMT_INDEX_NONE, MGMT_OP_READ_INDEX_LIST, rp, |
| 347 | rp_len); |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 348 | |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 349 | kfree(rp); |
| 350 | |
| 351 | return err; |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 352 | } |
| 353 | |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 354 | static u32 get_supported_settings(struct hci_dev *hdev) |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 355 | { |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 356 | u32 settings = 0; |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 357 | |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 358 | settings |= MGMT_SETTING_POWERED; |
| 359 | settings |= MGMT_SETTING_CONNECTABLE; |
| 360 | settings |= MGMT_SETTING_FAST_CONNECTABLE; |
| 361 | settings |= MGMT_SETTING_DISCOVERABLE; |
| 362 | settings |= MGMT_SETTING_PAIRABLE; |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 363 | |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 364 | if (hdev->features[6] & LMP_SIMPLE_PAIR) |
| 365 | settings |= MGMT_SETTING_SSP; |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 366 | |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 367 | if (!(hdev->features[4] & LMP_NO_BREDR)) { |
| 368 | settings |= MGMT_SETTING_BREDR; |
| 369 | settings |= MGMT_SETTING_LINK_SECURITY; |
| 370 | } |
Johan Hedberg | ab81cbf | 2010-12-15 13:53:18 +0200 | [diff] [blame] | 371 | |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 372 | if (hdev->features[4] & LMP_LE) |
| 373 | settings |= MGMT_SETTING_LE; |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 374 | |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 375 | return settings; |
| 376 | } |
Johan Hedberg | ebc99fe | 2011-01-04 11:54:26 +0200 | [diff] [blame] | 377 | |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 378 | static u32 get_current_settings(struct hci_dev *hdev) |
| 379 | { |
| 380 | u32 settings = 0; |
Johan Hedberg | dc4fe30 | 2011-03-16 14:29:36 +0200 | [diff] [blame] | 381 | |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 382 | if (test_bit(HCI_UP, &hdev->flags)) |
| 383 | settings |= MGMT_SETTING_POWERED; |
| 384 | else |
| 385 | return settings; |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 386 | |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 387 | if (test_bit(HCI_PSCAN, &hdev->flags)) |
| 388 | settings |= MGMT_SETTING_CONNECTABLE; |
| 389 | |
| 390 | if (test_bit(HCI_ISCAN, &hdev->flags)) |
| 391 | settings |= MGMT_SETTING_DISCOVERABLE; |
| 392 | |
Johan Hedberg | a8b2d5c | 2012-01-08 23:11:15 +0200 | [diff] [blame] | 393 | if (test_bit(HCI_PAIRABLE, &hdev->dev_flags)) |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 394 | settings |= MGMT_SETTING_PAIRABLE; |
| 395 | |
| 396 | if (!(hdev->features[4] & LMP_NO_BREDR)) |
| 397 | settings |= MGMT_SETTING_BREDR; |
| 398 | |
Andre Guedes | 59e2940 | 2011-12-30 10:34:03 -0300 | [diff] [blame] | 399 | if (hdev->host_features[0] & LMP_HOST_LE) |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 400 | settings |= MGMT_SETTING_LE; |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 401 | |
| 402 | if (test_bit(HCI_AUTH, &hdev->flags)) |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 403 | settings |= MGMT_SETTING_LINK_SECURITY; |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 404 | |
Johan Hedberg | 84bde9d | 2012-01-25 14:21:06 +0200 | [diff] [blame] | 405 | if (test_bit(HCI_SSP_ENABLED, &hdev->dev_flags)) |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 406 | settings |= MGMT_SETTING_SSP; |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 407 | |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 408 | return settings; |
Johan Hedberg | c542a06 | 2011-01-26 13:11:03 +0200 | [diff] [blame] | 409 | } |
| 410 | |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 411 | #define PNP_INFO_SVCLASS_ID 0x1200 |
| 412 | |
| 413 | static u8 bluetooth_base_uuid[] = { |
| 414 | 0xFB, 0x34, 0x9B, 0x5F, 0x80, 0x00, 0x00, 0x80, |
| 415 | 0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 416 | }; |
| 417 | |
| 418 | static u16 get_uuid16(u8 *uuid128) |
| 419 | { |
| 420 | u32 val; |
| 421 | int i; |
| 422 | |
| 423 | for (i = 0; i < 12; i++) { |
| 424 | if (bluetooth_base_uuid[i] != uuid128[i]) |
| 425 | return 0; |
| 426 | } |
| 427 | |
| 428 | memcpy(&val, &uuid128[12], 4); |
| 429 | |
| 430 | val = le32_to_cpu(val); |
| 431 | if (val > 0xffff) |
| 432 | return 0; |
| 433 | |
| 434 | return (u16) val; |
| 435 | } |
| 436 | |
| 437 | static void create_eir(struct hci_dev *hdev, u8 *data) |
| 438 | { |
| 439 | u8 *ptr = data; |
| 440 | u16 eir_len = 0; |
| 441 | u16 uuid16_list[HCI_MAX_EIR_LENGTH / sizeof(u16)]; |
| 442 | int i, truncated = 0; |
Luiz Augusto von Dentz | 8035ded | 2011-11-01 10:58:56 +0200 | [diff] [blame] | 443 | struct bt_uuid *uuid; |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 444 | size_t name_len; |
| 445 | |
| 446 | name_len = strlen(hdev->dev_name); |
| 447 | |
| 448 | if (name_len > 0) { |
| 449 | /* EIR Data type */ |
| 450 | if (name_len > 48) { |
| 451 | name_len = 48; |
| 452 | ptr[1] = EIR_NAME_SHORT; |
| 453 | } else |
| 454 | ptr[1] = EIR_NAME_COMPLETE; |
| 455 | |
| 456 | /* EIR Data length */ |
| 457 | ptr[0] = name_len + 1; |
| 458 | |
| 459 | memcpy(ptr + 2, hdev->dev_name, name_len); |
| 460 | |
| 461 | eir_len += (name_len + 2); |
| 462 | ptr += (name_len + 2); |
| 463 | } |
| 464 | |
| 465 | memset(uuid16_list, 0, sizeof(uuid16_list)); |
| 466 | |
| 467 | /* Group all UUID16 types */ |
Luiz Augusto von Dentz | 8035ded | 2011-11-01 10:58:56 +0200 | [diff] [blame] | 468 | list_for_each_entry(uuid, &hdev->uuids, list) { |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 469 | u16 uuid16; |
| 470 | |
| 471 | uuid16 = get_uuid16(uuid->uuid); |
| 472 | if (uuid16 == 0) |
| 473 | return; |
| 474 | |
| 475 | if (uuid16 < 0x1100) |
| 476 | continue; |
| 477 | |
| 478 | if (uuid16 == PNP_INFO_SVCLASS_ID) |
| 479 | continue; |
| 480 | |
| 481 | /* Stop if not enough space to put next UUID */ |
| 482 | if (eir_len + 2 + sizeof(u16) > HCI_MAX_EIR_LENGTH) { |
| 483 | truncated = 1; |
| 484 | break; |
| 485 | } |
| 486 | |
| 487 | /* Check for duplicates */ |
| 488 | for (i = 0; uuid16_list[i] != 0; i++) |
| 489 | if (uuid16_list[i] == uuid16) |
| 490 | break; |
| 491 | |
| 492 | if (uuid16_list[i] == 0) { |
| 493 | uuid16_list[i] = uuid16; |
| 494 | eir_len += sizeof(u16); |
| 495 | } |
| 496 | } |
| 497 | |
| 498 | if (uuid16_list[0] != 0) { |
| 499 | u8 *length = ptr; |
| 500 | |
| 501 | /* EIR Data type */ |
| 502 | ptr[1] = truncated ? EIR_UUID16_SOME : EIR_UUID16_ALL; |
| 503 | |
| 504 | ptr += 2; |
| 505 | eir_len += 2; |
| 506 | |
| 507 | for (i = 0; uuid16_list[i] != 0; i++) { |
| 508 | *ptr++ = (uuid16_list[i] & 0x00ff); |
| 509 | *ptr++ = (uuid16_list[i] & 0xff00) >> 8; |
| 510 | } |
| 511 | |
| 512 | /* EIR Data length */ |
| 513 | *length = (i * sizeof(u16)) + 1; |
| 514 | } |
| 515 | } |
| 516 | |
| 517 | static int update_eir(struct hci_dev *hdev) |
| 518 | { |
| 519 | struct hci_cp_write_eir cp; |
| 520 | |
| 521 | if (!(hdev->features[6] & LMP_EXT_INQ)) |
| 522 | return 0; |
| 523 | |
Johan Hedberg | 84bde9d | 2012-01-25 14:21:06 +0200 | [diff] [blame] | 524 | if (!test_bit(HCI_SSP_ENABLED, &hdev->dev_flags)) |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 525 | return 0; |
| 526 | |
Johan Hedberg | a8b2d5c | 2012-01-08 23:11:15 +0200 | [diff] [blame] | 527 | if (test_bit(HCI_SERVICE_CACHE, &hdev->dev_flags)) |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 528 | return 0; |
| 529 | |
| 530 | memset(&cp, 0, sizeof(cp)); |
| 531 | |
| 532 | create_eir(hdev, cp.data); |
| 533 | |
| 534 | if (memcmp(cp.data, hdev->eir, sizeof(cp.data)) == 0) |
| 535 | return 0; |
| 536 | |
| 537 | memcpy(hdev->eir, cp.data, sizeof(cp.data)); |
| 538 | |
| 539 | return hci_send_cmd(hdev, HCI_OP_WRITE_EIR, sizeof(cp), &cp); |
| 540 | } |
| 541 | |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 542 | static u8 get_service_classes(struct hci_dev *hdev) |
| 543 | { |
Gustavo F. Padovan | 12dc074 | 2011-10-14 19:32:56 -0300 | [diff] [blame] | 544 | struct bt_uuid *uuid; |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 545 | u8 val = 0; |
| 546 | |
Gustavo F. Padovan | 12dc074 | 2011-10-14 19:32:56 -0300 | [diff] [blame] | 547 | list_for_each_entry(uuid, &hdev->uuids, list) |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 548 | val |= uuid->svc_hint; |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 549 | |
| 550 | return val; |
| 551 | } |
| 552 | |
| 553 | static int update_class(struct hci_dev *hdev) |
| 554 | { |
| 555 | u8 cod[3]; |
| 556 | |
| 557 | BT_DBG("%s", hdev->name); |
| 558 | |
Johan Hedberg | a8b2d5c | 2012-01-08 23:11:15 +0200 | [diff] [blame] | 559 | if (test_bit(HCI_SERVICE_CACHE, &hdev->dev_flags)) |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 560 | return 0; |
| 561 | |
| 562 | cod[0] = hdev->minor_class; |
| 563 | cod[1] = hdev->major_class; |
| 564 | cod[2] = get_service_classes(hdev); |
| 565 | |
| 566 | if (memcmp(cod, hdev->dev_class, 3) == 0) |
| 567 | return 0; |
| 568 | |
| 569 | return hci_send_cmd(hdev, HCI_OP_WRITE_CLASS_OF_DEV, sizeof(cod), cod); |
| 570 | } |
| 571 | |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 572 | static void service_cache_off(struct work_struct *work) |
| 573 | { |
| 574 | struct hci_dev *hdev = container_of(work, struct hci_dev, |
| 575 | service_cache.work); |
| 576 | |
Johan Hedberg | a8b2d5c | 2012-01-08 23:11:15 +0200 | [diff] [blame] | 577 | if (!test_and_clear_bit(HCI_SERVICE_CACHE, &hdev->dev_flags)) |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 578 | return; |
| 579 | |
| 580 | hci_dev_lock(hdev); |
| 581 | |
| 582 | update_eir(hdev); |
| 583 | update_class(hdev); |
| 584 | |
| 585 | hci_dev_unlock(hdev); |
| 586 | } |
| 587 | |
| 588 | static void mgmt_init_hdev(struct hci_dev *hdev) |
| 589 | { |
Johan Hedberg | a8b2d5c | 2012-01-08 23:11:15 +0200 | [diff] [blame] | 590 | if (!test_and_set_bit(HCI_MGMT, &hdev->dev_flags)) |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 591 | INIT_DELAYED_WORK(&hdev->service_cache, service_cache_off); |
| 592 | |
Johan Hedberg | a8b2d5c | 2012-01-08 23:11:15 +0200 | [diff] [blame] | 593 | if (!test_and_set_bit(HCI_SERVICE_CACHE, &hdev->dev_flags)) |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 594 | schedule_delayed_work(&hdev->service_cache, |
| 595 | msecs_to_jiffies(SERVICE_CACHE_TIMEOUT)); |
| 596 | } |
| 597 | |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 598 | static int read_controller_info(struct sock *sk, u16 index) |
| 599 | { |
| 600 | struct mgmt_rp_read_info rp; |
| 601 | struct hci_dev *hdev; |
| 602 | |
| 603 | BT_DBG("sock %p hci%u", sk, index); |
| 604 | |
| 605 | hdev = hci_dev_get(index); |
| 606 | if (!hdev) |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 607 | return cmd_status(sk, index, MGMT_OP_READ_INFO, |
| 608 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 609 | |
Johan Hedberg | a8b2d5c | 2012-01-08 23:11:15 +0200 | [diff] [blame] | 610 | if (test_and_clear_bit(HCI_AUTO_OFF, &hdev->dev_flags)) |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 611 | cancel_delayed_work_sync(&hdev->power_off); |
| 612 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 613 | hci_dev_lock(hdev); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 614 | |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 615 | if (test_and_clear_bit(HCI_PI_MGMT_INIT, &hci_pi(sk)->flags)) |
| 616 | mgmt_init_hdev(hdev); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 617 | |
| 618 | memset(&rp, 0, sizeof(rp)); |
| 619 | |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 620 | bacpy(&rp.bdaddr, &hdev->bdaddr); |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 621 | |
| 622 | rp.version = hdev->hci_ver; |
| 623 | |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 624 | put_unaligned_le16(hdev->manufacturer, &rp.manufacturer); |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 625 | |
| 626 | rp.supported_settings = cpu_to_le32(get_supported_settings(hdev)); |
| 627 | rp.current_settings = cpu_to_le32(get_current_settings(hdev)); |
| 628 | |
| 629 | memcpy(rp.dev_class, hdev->dev_class, 3); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 630 | |
| 631 | memcpy(rp.name, hdev->dev_name, sizeof(hdev->dev_name)); |
| 632 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 633 | hci_dev_unlock(hdev); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 634 | hci_dev_put(hdev); |
| 635 | |
| 636 | return cmd_complete(sk, index, MGMT_OP_READ_INFO, &rp, sizeof(rp)); |
| 637 | } |
| 638 | |
| 639 | static void mgmt_pending_free(struct pending_cmd *cmd) |
| 640 | { |
| 641 | sock_put(cmd->sk); |
| 642 | kfree(cmd->param); |
| 643 | kfree(cmd); |
| 644 | } |
| 645 | |
| 646 | static struct pending_cmd *mgmt_pending_add(struct sock *sk, u16 opcode, |
| 647 | struct hci_dev *hdev, |
| 648 | void *data, u16 len) |
| 649 | { |
| 650 | struct pending_cmd *cmd; |
| 651 | |
| 652 | cmd = kmalloc(sizeof(*cmd), GFP_ATOMIC); |
| 653 | if (!cmd) |
| 654 | return NULL; |
| 655 | |
| 656 | cmd->opcode = opcode; |
| 657 | cmd->index = hdev->id; |
| 658 | |
| 659 | cmd->param = kmalloc(len, GFP_ATOMIC); |
| 660 | if (!cmd->param) { |
| 661 | kfree(cmd); |
| 662 | return NULL; |
| 663 | } |
| 664 | |
| 665 | if (data) |
| 666 | memcpy(cmd->param, data, len); |
| 667 | |
| 668 | cmd->sk = sk; |
| 669 | sock_hold(sk); |
| 670 | |
| 671 | list_add(&cmd->list, &hdev->mgmt_pending); |
| 672 | |
| 673 | return cmd; |
| 674 | } |
| 675 | |
| 676 | static void mgmt_pending_foreach(u16 opcode, struct hci_dev *hdev, |
| 677 | void (*cb)(struct pending_cmd *cmd, void *data), |
| 678 | void *data) |
| 679 | { |
| 680 | struct list_head *p, *n; |
| 681 | |
| 682 | list_for_each_safe(p, n, &hdev->mgmt_pending) { |
| 683 | struct pending_cmd *cmd; |
| 684 | |
| 685 | cmd = list_entry(p, struct pending_cmd, list); |
| 686 | |
| 687 | if (opcode > 0 && cmd->opcode != opcode) |
| 688 | continue; |
| 689 | |
| 690 | cb(cmd, data); |
| 691 | } |
| 692 | } |
| 693 | |
| 694 | static struct pending_cmd *mgmt_pending_find(u16 opcode, struct hci_dev *hdev) |
| 695 | { |
| 696 | struct pending_cmd *cmd; |
| 697 | |
| 698 | list_for_each_entry(cmd, &hdev->mgmt_pending, list) { |
| 699 | if (cmd->opcode == opcode) |
| 700 | return cmd; |
| 701 | } |
| 702 | |
| 703 | return NULL; |
| 704 | } |
| 705 | |
| 706 | static void mgmt_pending_remove(struct pending_cmd *cmd) |
| 707 | { |
| 708 | list_del(&cmd->list); |
| 709 | mgmt_pending_free(cmd); |
| 710 | } |
| 711 | |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 712 | static int send_settings_rsp(struct sock *sk, u16 opcode, struct hci_dev *hdev) |
Johan Hedberg | 8680570 | 2011-11-11 16:18:52 +0200 | [diff] [blame] | 713 | { |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 714 | __le32 settings = cpu_to_le32(get_current_settings(hdev)); |
Johan Hedberg | 8680570 | 2011-11-11 16:18:52 +0200 | [diff] [blame] | 715 | |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 716 | return cmd_complete(sk, hdev->id, opcode, &settings, sizeof(settings)); |
Johan Hedberg | 8680570 | 2011-11-11 16:18:52 +0200 | [diff] [blame] | 717 | } |
| 718 | |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 719 | static int set_powered(struct sock *sk, u16 index, void *data, u16 len) |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 720 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 721 | struct mgmt_mode *cp = data; |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 722 | struct hci_dev *hdev; |
| 723 | struct pending_cmd *cmd; |
| 724 | int err, up; |
| 725 | |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 726 | BT_DBG("request for hci%u", index); |
| 727 | |
| 728 | if (len != sizeof(*cp)) |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 729 | return cmd_status(sk, index, MGMT_OP_SET_POWERED, |
| 730 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 731 | |
| 732 | hdev = hci_dev_get(index); |
| 733 | if (!hdev) |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 734 | return cmd_status(sk, index, MGMT_OP_SET_POWERED, |
| 735 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 736 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 737 | hci_dev_lock(hdev); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 738 | |
| 739 | up = test_bit(HCI_UP, &hdev->flags); |
| 740 | if ((cp->val && up) || (!cp->val && !up)) { |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 741 | err = send_settings_rsp(sk, MGMT_OP_SET_POWERED, hdev); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 742 | goto failed; |
| 743 | } |
| 744 | |
| 745 | if (mgmt_pending_find(MGMT_OP_SET_POWERED, hdev)) { |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 746 | err = cmd_status(sk, index, MGMT_OP_SET_POWERED, |
| 747 | MGMT_STATUS_BUSY); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 748 | goto failed; |
| 749 | } |
| 750 | |
| 751 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_POWERED, hdev, data, len); |
| 752 | if (!cmd) { |
| 753 | err = -ENOMEM; |
| 754 | goto failed; |
| 755 | } |
| 756 | |
| 757 | if (cp->val) |
Gustavo F. Padovan | 7f97104 | 2011-12-18 12:40:32 -0200 | [diff] [blame] | 758 | schedule_work(&hdev->power_on); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 759 | else |
Gustavo F. Padovan | 80b7ab3 | 2011-12-17 14:52:27 -0200 | [diff] [blame] | 760 | schedule_work(&hdev->power_off.work); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 761 | |
| 762 | err = 0; |
| 763 | |
| 764 | failed: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 765 | hci_dev_unlock(hdev); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 766 | hci_dev_put(hdev); |
| 767 | return err; |
| 768 | } |
| 769 | |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 770 | static int set_discoverable(struct sock *sk, u16 index, void *data, u16 len) |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 771 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 772 | struct mgmt_cp_set_discoverable *cp = data; |
Johan Hedberg | e41d8b4 | 2010-12-13 21:07:03 +0200 | [diff] [blame] | 773 | struct hci_dev *hdev; |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 774 | struct pending_cmd *cmd; |
| 775 | u8 scan; |
| 776 | int err; |
| 777 | |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 778 | BT_DBG("request for hci%u", index); |
| 779 | |
| 780 | if (len != sizeof(*cp)) |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 781 | return cmd_status(sk, index, MGMT_OP_SET_DISCOVERABLE, |
| 782 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 783 | |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 784 | hdev = hci_dev_get(index); |
| 785 | if (!hdev) |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 786 | return cmd_status(sk, index, MGMT_OP_SET_DISCOVERABLE, |
| 787 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 788 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 789 | hci_dev_lock(hdev); |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 790 | |
| 791 | if (!test_bit(HCI_UP, &hdev->flags)) { |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 792 | err = cmd_status(sk, index, MGMT_OP_SET_DISCOVERABLE, |
| 793 | MGMT_STATUS_NOT_POWERED); |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 794 | goto failed; |
| 795 | } |
| 796 | |
| 797 | if (mgmt_pending_find(MGMT_OP_SET_DISCOVERABLE, hdev) || |
| 798 | mgmt_pending_find(MGMT_OP_SET_CONNECTABLE, hdev)) { |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 799 | err = cmd_status(sk, index, MGMT_OP_SET_DISCOVERABLE, |
| 800 | MGMT_STATUS_BUSY); |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 801 | goto failed; |
| 802 | } |
| 803 | |
| 804 | if (cp->val == test_bit(HCI_ISCAN, &hdev->flags) && |
| 805 | test_bit(HCI_PSCAN, &hdev->flags)) { |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 806 | err = send_settings_rsp(sk, MGMT_OP_SET_DISCOVERABLE, hdev); |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 807 | goto failed; |
| 808 | } |
| 809 | |
| 810 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_DISCOVERABLE, hdev, data, len); |
| 811 | if (!cmd) { |
| 812 | err = -ENOMEM; |
| 813 | goto failed; |
| 814 | } |
| 815 | |
| 816 | scan = SCAN_PAGE; |
| 817 | |
| 818 | if (cp->val) |
| 819 | scan |= SCAN_INQUIRY; |
| 820 | else |
| 821 | cancel_delayed_work(&hdev->discov_off); |
| 822 | |
| 823 | err = hci_send_cmd(hdev, HCI_OP_WRITE_SCAN_ENABLE, 1, &scan); |
| 824 | if (err < 0) |
| 825 | mgmt_pending_remove(cmd); |
| 826 | |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 827 | if (cp->val) |
| 828 | hdev->discov_timeout = get_unaligned_le16(&cp->timeout); |
| 829 | |
Johan Hedberg | e41d8b4 | 2010-12-13 21:07:03 +0200 | [diff] [blame] | 830 | failed: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 831 | hci_dev_unlock(hdev); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 832 | hci_dev_put(hdev); |
| 833 | |
| 834 | return err; |
| 835 | } |
| 836 | |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 837 | static int set_connectable(struct sock *sk, u16 index, void *data, u16 len) |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 838 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 839 | struct mgmt_mode *cp = data; |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 840 | struct hci_dev *hdev; |
| 841 | struct pending_cmd *cmd; |
| 842 | u8 scan; |
| 843 | int err; |
| 844 | |
Johan Hedberg | e41d8b4 | 2010-12-13 21:07:03 +0200 | [diff] [blame] | 845 | BT_DBG("request for hci%u", index); |
| 846 | |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 847 | if (len != sizeof(*cp)) |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 848 | return cmd_status(sk, index, MGMT_OP_SET_CONNECTABLE, |
| 849 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 850 | |
| 851 | hdev = hci_dev_get(index); |
| 852 | if (!hdev) |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 853 | return cmd_status(sk, index, MGMT_OP_SET_CONNECTABLE, |
| 854 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 855 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 856 | hci_dev_lock(hdev); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 857 | |
| 858 | if (!test_bit(HCI_UP, &hdev->flags)) { |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 859 | err = cmd_status(sk, index, MGMT_OP_SET_CONNECTABLE, |
| 860 | MGMT_STATUS_NOT_POWERED); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 861 | goto failed; |
| 862 | } |
| 863 | |
| 864 | if (mgmt_pending_find(MGMT_OP_SET_DISCOVERABLE, hdev) || |
| 865 | mgmt_pending_find(MGMT_OP_SET_CONNECTABLE, hdev)) { |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 866 | err = cmd_status(sk, index, MGMT_OP_SET_CONNECTABLE, |
| 867 | MGMT_STATUS_BUSY); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 868 | goto failed; |
| 869 | } |
| 870 | |
| 871 | if (cp->val == test_bit(HCI_PSCAN, &hdev->flags)) { |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 872 | err = send_settings_rsp(sk, MGMT_OP_SET_CONNECTABLE, hdev); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 873 | goto failed; |
| 874 | } |
| 875 | |
| 876 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_CONNECTABLE, hdev, data, len); |
| 877 | if (!cmd) { |
| 878 | err = -ENOMEM; |
| 879 | goto failed; |
| 880 | } |
| 881 | |
| 882 | if (cp->val) |
| 883 | scan = SCAN_PAGE; |
| 884 | else |
| 885 | scan = 0; |
| 886 | |
| 887 | err = hci_send_cmd(hdev, HCI_OP_WRITE_SCAN_ENABLE, 1, &scan); |
| 888 | if (err < 0) |
| 889 | mgmt_pending_remove(cmd); |
| 890 | |
| 891 | failed: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 892 | hci_dev_unlock(hdev); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 893 | hci_dev_put(hdev); |
| 894 | |
| 895 | return err; |
| 896 | } |
| 897 | |
| 898 | static int mgmt_event(u16 event, struct hci_dev *hdev, void *data, |
| 899 | u16 data_len, struct sock *skip_sk) |
| 900 | { |
| 901 | struct sk_buff *skb; |
| 902 | struct mgmt_hdr *hdr; |
| 903 | |
| 904 | skb = alloc_skb(sizeof(*hdr) + data_len, GFP_ATOMIC); |
| 905 | if (!skb) |
| 906 | return -ENOMEM; |
| 907 | |
| 908 | bt_cb(skb)->channel = HCI_CHANNEL_CONTROL; |
| 909 | |
| 910 | hdr = (void *) skb_put(skb, sizeof(*hdr)); |
| 911 | hdr->opcode = cpu_to_le16(event); |
| 912 | if (hdev) |
| 913 | hdr->index = cpu_to_le16(hdev->id); |
| 914 | else |
| 915 | hdr->index = cpu_to_le16(MGMT_INDEX_NONE); |
| 916 | hdr->len = cpu_to_le16(data_len); |
| 917 | |
| 918 | if (data) |
| 919 | memcpy(skb_put(skb, data_len), data, data_len); |
| 920 | |
| 921 | hci_send_to_sock(NULL, skb, skip_sk); |
| 922 | kfree_skb(skb); |
| 923 | |
| 924 | return 0; |
| 925 | } |
| 926 | |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 927 | static int set_pairable(struct sock *sk, u16 index, void *data, u16 len) |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 928 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 929 | struct mgmt_mode *cp = data; |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 930 | struct hci_dev *hdev; |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 931 | __le32 ev; |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 932 | int err; |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 933 | |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 934 | BT_DBG("request for hci%u", index); |
| 935 | |
| 936 | if (len != sizeof(*cp)) |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 937 | return cmd_status(sk, index, MGMT_OP_SET_PAIRABLE, |
| 938 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 939 | |
| 940 | hdev = hci_dev_get(index); |
| 941 | if (!hdev) |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 942 | return cmd_status(sk, index, MGMT_OP_SET_PAIRABLE, |
| 943 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 944 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 945 | hci_dev_lock(hdev); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 946 | |
| 947 | if (cp->val) |
Johan Hedberg | a8b2d5c | 2012-01-08 23:11:15 +0200 | [diff] [blame] | 948 | set_bit(HCI_PAIRABLE, &hdev->dev_flags); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 949 | else |
Johan Hedberg | a8b2d5c | 2012-01-08 23:11:15 +0200 | [diff] [blame] | 950 | clear_bit(HCI_PAIRABLE, &hdev->dev_flags); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 951 | |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 952 | err = send_settings_rsp(sk, MGMT_OP_SET_PAIRABLE, hdev); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 953 | if (err < 0) |
| 954 | goto failed; |
| 955 | |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 956 | ev = cpu_to_le32(get_current_settings(hdev)); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 957 | |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 958 | err = mgmt_event(MGMT_EV_NEW_SETTINGS, hdev, &ev, sizeof(ev), sk); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 959 | |
| 960 | failed: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 961 | hci_dev_unlock(hdev); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 962 | hci_dev_put(hdev); |
| 963 | |
| 964 | return err; |
| 965 | } |
Johan Hedberg | 72a734e | 2010-12-30 00:38:22 +0200 | [diff] [blame] | 966 | |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 967 | static int set_link_security(struct sock *sk, u16 index, void *data, u16 len) |
| 968 | { |
| 969 | struct mgmt_mode *cp = data; |
| 970 | struct pending_cmd *cmd; |
| 971 | struct hci_dev *hdev; |
| 972 | uint8_t val; |
| 973 | int err; |
| 974 | |
| 975 | BT_DBG("request for hci%u", index); |
| 976 | |
| 977 | if (len != sizeof(*cp)) |
| 978 | return cmd_status(sk, index, MGMT_OP_SET_LINK_SECURITY, |
| 979 | MGMT_STATUS_INVALID_PARAMS); |
| 980 | |
| 981 | hdev = hci_dev_get(index); |
| 982 | if (!hdev) |
| 983 | return cmd_status(sk, index, MGMT_OP_SET_LINK_SECURITY, |
| 984 | MGMT_STATUS_INVALID_PARAMS); |
| 985 | |
| 986 | hci_dev_lock(hdev); |
| 987 | |
| 988 | if (!test_bit(HCI_UP, &hdev->flags)) { |
| 989 | err = cmd_status(sk, index, MGMT_OP_SET_LINK_SECURITY, |
| 990 | MGMT_STATUS_NOT_POWERED); |
| 991 | goto failed; |
| 992 | } |
| 993 | |
| 994 | if (mgmt_pending_find(MGMT_OP_SET_LINK_SECURITY, hdev)) { |
| 995 | err = cmd_status(sk, index, MGMT_OP_SET_LINK_SECURITY, |
| 996 | MGMT_STATUS_BUSY); |
| 997 | goto failed; |
| 998 | } |
| 999 | |
| 1000 | val = !!cp->val; |
| 1001 | |
| 1002 | if (test_bit(HCI_AUTH, &hdev->flags) == val) { |
| 1003 | err = send_settings_rsp(sk, MGMT_OP_SET_LINK_SECURITY, hdev); |
| 1004 | goto failed; |
| 1005 | } |
| 1006 | |
| 1007 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_LINK_SECURITY, hdev, data, len); |
| 1008 | if (!cmd) { |
| 1009 | err = -ENOMEM; |
| 1010 | goto failed; |
| 1011 | } |
| 1012 | |
| 1013 | err = hci_send_cmd(hdev, HCI_OP_WRITE_AUTH_ENABLE, sizeof(val), &val); |
| 1014 | if (err < 0) { |
| 1015 | mgmt_pending_remove(cmd); |
| 1016 | goto failed; |
| 1017 | } |
| 1018 | |
| 1019 | failed: |
| 1020 | hci_dev_unlock(hdev); |
| 1021 | hci_dev_put(hdev); |
| 1022 | |
| 1023 | return err; |
| 1024 | } |
| 1025 | |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 1026 | static int set_ssp(struct sock *sk, u16 index, void *data, u16 len) |
| 1027 | { |
| 1028 | struct mgmt_mode *cp = data; |
| 1029 | struct pending_cmd *cmd; |
| 1030 | struct hci_dev *hdev; |
| 1031 | uint8_t val; |
| 1032 | int err; |
| 1033 | |
| 1034 | BT_DBG("request for hci%u", index); |
| 1035 | |
| 1036 | if (len != sizeof(*cp)) |
| 1037 | return cmd_status(sk, index, MGMT_OP_SET_SSP, |
| 1038 | MGMT_STATUS_INVALID_PARAMS); |
| 1039 | |
| 1040 | hdev = hci_dev_get(index); |
| 1041 | if (!hdev) |
| 1042 | return cmd_status(sk, index, MGMT_OP_SET_SSP, |
| 1043 | MGMT_STATUS_INVALID_PARAMS); |
| 1044 | |
| 1045 | hci_dev_lock(hdev); |
| 1046 | |
| 1047 | if (!test_bit(HCI_UP, &hdev->flags)) { |
| 1048 | err = cmd_status(sk, index, MGMT_OP_SET_SSP, |
| 1049 | MGMT_STATUS_NOT_POWERED); |
| 1050 | goto failed; |
| 1051 | } |
| 1052 | |
| 1053 | if (mgmt_pending_find(MGMT_OP_SET_SSP, hdev)) { |
| 1054 | err = cmd_status(sk, index, MGMT_OP_SET_SSP, MGMT_STATUS_BUSY); |
| 1055 | goto failed; |
| 1056 | } |
| 1057 | |
| 1058 | val = !!cp->val; |
| 1059 | |
| 1060 | if (test_bit(HCI_SSP_ENABLED, &hdev->dev_flags) == val) { |
| 1061 | err = send_settings_rsp(sk, MGMT_OP_SET_SSP, hdev); |
| 1062 | goto failed; |
| 1063 | } |
| 1064 | |
| 1065 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_SSP, hdev, data, len); |
| 1066 | if (!cmd) { |
| 1067 | err = -ENOMEM; |
| 1068 | goto failed; |
| 1069 | } |
| 1070 | |
| 1071 | err = hci_send_cmd(hdev, HCI_OP_WRITE_SSP_MODE, sizeof(val), &val); |
| 1072 | if (err < 0) { |
| 1073 | mgmt_pending_remove(cmd); |
| 1074 | goto failed; |
| 1075 | } |
| 1076 | |
| 1077 | failed: |
| 1078 | hci_dev_unlock(hdev); |
| 1079 | hci_dev_put(hdev); |
| 1080 | |
| 1081 | return err; |
| 1082 | } |
| 1083 | |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 1084 | static int add_uuid(struct sock *sk, u16 index, void *data, u16 len) |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 1085 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 1086 | struct mgmt_cp_add_uuid *cp = data; |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 1087 | struct hci_dev *hdev; |
| 1088 | struct bt_uuid *uuid; |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 1089 | int err; |
| 1090 | |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 1091 | BT_DBG("request for hci%u", index); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 1092 | |
Szymon Janc | bdce7ba | 2011-02-25 19:05:49 +0100 | [diff] [blame] | 1093 | if (len != sizeof(*cp)) |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 1094 | return cmd_status(sk, index, MGMT_OP_ADD_UUID, |
| 1095 | MGMT_STATUS_INVALID_PARAMS); |
Szymon Janc | bdce7ba | 2011-02-25 19:05:49 +0100 | [diff] [blame] | 1096 | |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 1097 | hdev = hci_dev_get(index); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 1098 | if (!hdev) |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 1099 | return cmd_status(sk, index, MGMT_OP_ADD_UUID, |
| 1100 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 1101 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1102 | hci_dev_lock(hdev); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 1103 | |
| 1104 | uuid = kmalloc(sizeof(*uuid), GFP_ATOMIC); |
| 1105 | if (!uuid) { |
| 1106 | err = -ENOMEM; |
| 1107 | goto failed; |
| 1108 | } |
| 1109 | |
| 1110 | memcpy(uuid->uuid, cp->uuid, 16); |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 1111 | uuid->svc_hint = cp->svc_hint; |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 1112 | |
| 1113 | list_add(&uuid->list, &hdev->uuids); |
| 1114 | |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 1115 | err = update_class(hdev); |
| 1116 | if (err < 0) |
| 1117 | goto failed; |
| 1118 | |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 1119 | err = update_eir(hdev); |
| 1120 | if (err < 0) |
| 1121 | goto failed; |
| 1122 | |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 1123 | err = cmd_complete(sk, index, MGMT_OP_ADD_UUID, NULL, 0); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 1124 | |
| 1125 | failed: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1126 | hci_dev_unlock(hdev); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 1127 | hci_dev_put(hdev); |
| 1128 | |
| 1129 | return err; |
| 1130 | } |
| 1131 | |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 1132 | static int remove_uuid(struct sock *sk, u16 index, void *data, u16 len) |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 1133 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 1134 | struct mgmt_cp_remove_uuid *cp = data; |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 1135 | struct list_head *p, *n; |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 1136 | struct hci_dev *hdev; |
| 1137 | 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] | 1138 | int err, found; |
| 1139 | |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 1140 | BT_DBG("request for hci%u", index); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 1141 | |
Szymon Janc | bdce7ba | 2011-02-25 19:05:49 +0100 | [diff] [blame] | 1142 | if (len != sizeof(*cp)) |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 1143 | return cmd_status(sk, index, MGMT_OP_REMOVE_UUID, |
| 1144 | MGMT_STATUS_INVALID_PARAMS); |
Szymon Janc | bdce7ba | 2011-02-25 19:05:49 +0100 | [diff] [blame] | 1145 | |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 1146 | hdev = hci_dev_get(index); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 1147 | if (!hdev) |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 1148 | return cmd_status(sk, index, MGMT_OP_REMOVE_UUID, |
| 1149 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 1150 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1151 | hci_dev_lock(hdev); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 1152 | |
| 1153 | if (memcmp(cp->uuid, bt_uuid_any, 16) == 0) { |
| 1154 | err = hci_uuids_clear(hdev); |
| 1155 | goto unlock; |
| 1156 | } |
| 1157 | |
| 1158 | found = 0; |
| 1159 | |
| 1160 | list_for_each_safe(p, n, &hdev->uuids) { |
| 1161 | struct bt_uuid *match = list_entry(p, struct bt_uuid, list); |
| 1162 | |
| 1163 | if (memcmp(match->uuid, cp->uuid, 16) != 0) |
| 1164 | continue; |
| 1165 | |
| 1166 | list_del(&match->list); |
| 1167 | found++; |
| 1168 | } |
| 1169 | |
| 1170 | if (found == 0) { |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 1171 | err = cmd_status(sk, index, MGMT_OP_REMOVE_UUID, |
| 1172 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 1173 | goto unlock; |
| 1174 | } |
| 1175 | |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 1176 | err = update_class(hdev); |
| 1177 | if (err < 0) |
| 1178 | goto unlock; |
| 1179 | |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 1180 | err = update_eir(hdev); |
| 1181 | if (err < 0) |
| 1182 | goto unlock; |
| 1183 | |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 1184 | err = cmd_complete(sk, index, MGMT_OP_REMOVE_UUID, NULL, 0); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 1185 | |
| 1186 | unlock: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1187 | hci_dev_unlock(hdev); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 1188 | hci_dev_put(hdev); |
| 1189 | |
| 1190 | return err; |
| 1191 | } |
| 1192 | |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 1193 | static int set_dev_class(struct sock *sk, u16 index, void *data, u16 len) |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 1194 | { |
| 1195 | struct hci_dev *hdev; |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 1196 | struct mgmt_cp_set_dev_class *cp = data; |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 1197 | int err; |
| 1198 | |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 1199 | BT_DBG("request for hci%u", index); |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 1200 | |
Szymon Janc | bdce7ba | 2011-02-25 19:05:49 +0100 | [diff] [blame] | 1201 | if (len != sizeof(*cp)) |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 1202 | return cmd_status(sk, index, MGMT_OP_SET_DEV_CLASS, |
| 1203 | MGMT_STATUS_INVALID_PARAMS); |
Szymon Janc | bdce7ba | 2011-02-25 19:05:49 +0100 | [diff] [blame] | 1204 | |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 1205 | hdev = hci_dev_get(index); |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 1206 | if (!hdev) |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 1207 | return cmd_status(sk, index, MGMT_OP_SET_DEV_CLASS, |
| 1208 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 1209 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1210 | hci_dev_lock(hdev); |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 1211 | |
| 1212 | hdev->major_class = cp->major; |
| 1213 | hdev->minor_class = cp->minor; |
| 1214 | |
Johan Hedberg | a8b2d5c | 2012-01-08 23:11:15 +0200 | [diff] [blame] | 1215 | if (test_and_clear_bit(HCI_SERVICE_CACHE, &hdev->dev_flags)) { |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 1216 | hci_dev_unlock(hdev); |
| 1217 | cancel_delayed_work_sync(&hdev->service_cache); |
| 1218 | hci_dev_lock(hdev); |
Johan Hedberg | 14c0b60 | 2011-12-15 00:47:37 +0200 | [diff] [blame] | 1219 | update_eir(hdev); |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 1220 | } |
Johan Hedberg | 14c0b60 | 2011-12-15 00:47:37 +0200 | [diff] [blame] | 1221 | |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 1222 | err = update_class(hdev); |
| 1223 | |
| 1224 | if (err == 0) |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 1225 | err = cmd_complete(sk, index, MGMT_OP_SET_DEV_CLASS, NULL, 0); |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 1226 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1227 | hci_dev_unlock(hdev); |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 1228 | hci_dev_put(hdev); |
| 1229 | |
| 1230 | return err; |
| 1231 | } |
| 1232 | |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 1233 | static int load_link_keys(struct sock *sk, u16 index, void *data, u16 len) |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 1234 | { |
| 1235 | struct hci_dev *hdev; |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 1236 | struct mgmt_cp_load_link_keys *cp = data; |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 1237 | u16 key_count, expected_len; |
Vinicius Costa Gomes | a492cd5 | 2011-08-25 20:02:29 -0300 | [diff] [blame] | 1238 | int i; |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 1239 | |
Szymon Janc | bdce7ba | 2011-02-25 19:05:49 +0100 | [diff] [blame] | 1240 | if (len < sizeof(*cp)) |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 1241 | return cmd_status(sk, index, MGMT_OP_LOAD_LINK_KEYS, |
| 1242 | MGMT_STATUS_INVALID_PARAMS); |
Szymon Janc | bdce7ba | 2011-02-25 19:05:49 +0100 | [diff] [blame] | 1243 | |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 1244 | key_count = get_unaligned_le16(&cp->key_count); |
| 1245 | |
Johan Hedberg | 86742e1 | 2011-11-07 23:13:38 +0200 | [diff] [blame] | 1246 | expected_len = sizeof(*cp) + key_count * |
| 1247 | sizeof(struct mgmt_link_key_info); |
Vinicius Costa Gomes | a492cd5 | 2011-08-25 20:02:29 -0300 | [diff] [blame] | 1248 | if (expected_len != len) { |
Johan Hedberg | 86742e1 | 2011-11-07 23:13:38 +0200 | [diff] [blame] | 1249 | BT_ERR("load_link_keys: expected %u bytes, got %u bytes", |
Vinicius Costa Gomes | a492cd5 | 2011-08-25 20:02:29 -0300 | [diff] [blame] | 1250 | len, expected_len); |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 1251 | return cmd_status(sk, index, MGMT_OP_LOAD_LINK_KEYS, |
| 1252 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 1253 | } |
| 1254 | |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 1255 | hdev = hci_dev_get(index); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 1256 | if (!hdev) |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 1257 | return cmd_status(sk, index, MGMT_OP_LOAD_LINK_KEYS, |
| 1258 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 1259 | |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 1260 | 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] | 1261 | key_count); |
| 1262 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1263 | hci_dev_lock(hdev); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 1264 | |
| 1265 | hci_link_keys_clear(hdev); |
| 1266 | |
Johan Hedberg | a8b2d5c | 2012-01-08 23:11:15 +0200 | [diff] [blame] | 1267 | set_bit(HCI_LINK_KEYS, &hdev->dev_flags); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 1268 | |
| 1269 | if (cp->debug_keys) |
Johan Hedberg | a8b2d5c | 2012-01-08 23:11:15 +0200 | [diff] [blame] | 1270 | set_bit(HCI_DEBUG_KEYS, &hdev->dev_flags); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 1271 | else |
Johan Hedberg | a8b2d5c | 2012-01-08 23:11:15 +0200 | [diff] [blame] | 1272 | clear_bit(HCI_DEBUG_KEYS, &hdev->dev_flags); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 1273 | |
Vinicius Costa Gomes | a492cd5 | 2011-08-25 20:02:29 -0300 | [diff] [blame] | 1274 | for (i = 0; i < key_count; i++) { |
Johan Hedberg | 86742e1 | 2011-11-07 23:13:38 +0200 | [diff] [blame] | 1275 | struct mgmt_link_key_info *key = &cp->keys[i]; |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 1276 | |
Johan Hedberg | d753fdc | 2012-02-17 14:06:34 +0200 | [diff] [blame] | 1277 | hci_add_link_key(hdev, NULL, 0, &key->addr.bdaddr, key->val, |
| 1278 | key->type, key->pin_len); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 1279 | } |
| 1280 | |
Johan Hedberg | 0e5f875 | 2011-11-11 16:18:54 +0200 | [diff] [blame] | 1281 | cmd_complete(sk, index, MGMT_OP_LOAD_LINK_KEYS, NULL, 0); |
| 1282 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1283 | hci_dev_unlock(hdev); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 1284 | hci_dev_put(hdev); |
| 1285 | |
Vinicius Costa Gomes | a492cd5 | 2011-08-25 20:02:29 -0300 | [diff] [blame] | 1286 | return 0; |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 1287 | } |
| 1288 | |
Johan Hedberg | b1078ad | 2012-02-09 17:21:16 +0200 | [diff] [blame] | 1289 | static int device_unpaired(struct hci_dev *hdev, bdaddr_t *bdaddr, |
| 1290 | u8 addr_type, struct sock *skip_sk) |
| 1291 | { |
| 1292 | struct mgmt_ev_device_unpaired ev; |
| 1293 | |
| 1294 | bacpy(&ev.addr.bdaddr, bdaddr); |
| 1295 | ev.addr.type = addr_type; |
| 1296 | |
| 1297 | return mgmt_event(MGMT_EV_DEVICE_UNPAIRED, hdev, &ev, sizeof(ev), |
| 1298 | skip_sk); |
| 1299 | } |
| 1300 | |
Johan Hedberg | 124f6e3 | 2012-02-09 13:50:12 +0200 | [diff] [blame] | 1301 | static int unpair_device(struct sock *sk, u16 index, void *data, u16 len) |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 1302 | { |
| 1303 | struct hci_dev *hdev; |
Johan Hedberg | 124f6e3 | 2012-02-09 13:50:12 +0200 | [diff] [blame] | 1304 | struct mgmt_cp_unpair_device *cp = data; |
| 1305 | struct mgmt_rp_unpair_device rp; |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 1306 | struct hci_cp_disconnect dc; |
| 1307 | struct pending_cmd *cmd; |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 1308 | struct hci_conn *conn; |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 1309 | int err; |
| 1310 | |
Szymon Janc | bdce7ba | 2011-02-25 19:05:49 +0100 | [diff] [blame] | 1311 | if (len != sizeof(*cp)) |
Johan Hedberg | 124f6e3 | 2012-02-09 13:50:12 +0200 | [diff] [blame] | 1312 | return cmd_status(sk, index, MGMT_OP_UNPAIR_DEVICE, |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 1313 | MGMT_STATUS_INVALID_PARAMS); |
Szymon Janc | bdce7ba | 2011-02-25 19:05:49 +0100 | [diff] [blame] | 1314 | |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 1315 | hdev = hci_dev_get(index); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 1316 | if (!hdev) |
Johan Hedberg | 124f6e3 | 2012-02-09 13:50:12 +0200 | [diff] [blame] | 1317 | return cmd_status(sk, index, MGMT_OP_UNPAIR_DEVICE, |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 1318 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 1319 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1320 | hci_dev_lock(hdev); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 1321 | |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 1322 | memset(&rp, 0, sizeof(rp)); |
Johan Hedberg | 124f6e3 | 2012-02-09 13:50:12 +0200 | [diff] [blame] | 1323 | bacpy(&rp.addr.bdaddr, &cp->addr.bdaddr); |
| 1324 | rp.addr.type = cp->addr.type; |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 1325 | |
Johan Hedberg | 124f6e3 | 2012-02-09 13:50:12 +0200 | [diff] [blame] | 1326 | if (cp->addr.type == MGMT_ADDR_BREDR) |
| 1327 | err = hci_remove_link_key(hdev, &cp->addr.bdaddr); |
| 1328 | else |
| 1329 | err = hci_remove_ltk(hdev, &cp->addr.bdaddr); |
Vinicius Costa Gomes | b0dbfb4 | 2012-02-02 21:08:03 -0300 | [diff] [blame] | 1330 | |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 1331 | if (err < 0) { |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 1332 | rp.status = MGMT_STATUS_NOT_PAIRED; |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 1333 | goto unlock; |
| 1334 | } |
| 1335 | |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 1336 | if (!test_bit(HCI_UP, &hdev->flags) || !cp->disconnect) { |
Johan Hedberg | 124f6e3 | 2012-02-09 13:50:12 +0200 | [diff] [blame] | 1337 | err = cmd_complete(sk, index, MGMT_OP_UNPAIR_DEVICE, &rp, |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 1338 | sizeof(rp)); |
Johan Hedberg | b1078ad | 2012-02-09 17:21:16 +0200 | [diff] [blame] | 1339 | device_unpaired(hdev, &cp->addr.bdaddr, cp->addr.type, sk); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 1340 | goto unlock; |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 1341 | } |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 1342 | |
Johan Hedberg | 124f6e3 | 2012-02-09 13:50:12 +0200 | [diff] [blame] | 1343 | if (cp->addr.type == MGMT_ADDR_BREDR) |
| 1344 | conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, |
| 1345 | &cp->addr.bdaddr); |
| 1346 | else |
| 1347 | conn = hci_conn_hash_lookup_ba(hdev, LE_LINK, |
| 1348 | &cp->addr.bdaddr); |
| 1349 | |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 1350 | if (!conn) { |
Johan Hedberg | 124f6e3 | 2012-02-09 13:50:12 +0200 | [diff] [blame] | 1351 | err = cmd_complete(sk, index, MGMT_OP_UNPAIR_DEVICE, &rp, |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 1352 | sizeof(rp)); |
Johan Hedberg | b1078ad | 2012-02-09 17:21:16 +0200 | [diff] [blame] | 1353 | device_unpaired(hdev, &cp->addr.bdaddr, cp->addr.type, sk); |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 1354 | goto unlock; |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 1355 | } |
| 1356 | |
Johan Hedberg | 124f6e3 | 2012-02-09 13:50:12 +0200 | [diff] [blame] | 1357 | cmd = mgmt_pending_add(sk, MGMT_OP_UNPAIR_DEVICE, hdev, cp, |
| 1358 | sizeof(*cp)); |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 1359 | if (!cmd) { |
| 1360 | err = -ENOMEM; |
| 1361 | goto unlock; |
| 1362 | } |
| 1363 | |
| 1364 | put_unaligned_le16(conn->handle, &dc.handle); |
| 1365 | dc.reason = 0x13; /* Remote User Terminated Connection */ |
| 1366 | err = hci_send_cmd(hdev, HCI_OP_DISCONNECT, sizeof(dc), &dc); |
| 1367 | if (err < 0) |
| 1368 | mgmt_pending_remove(cmd); |
| 1369 | |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 1370 | unlock: |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 1371 | if (err < 0) |
Johan Hedberg | 124f6e3 | 2012-02-09 13:50:12 +0200 | [diff] [blame] | 1372 | err = cmd_complete(sk, index, MGMT_OP_UNPAIR_DEVICE, &rp, |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 1373 | sizeof(rp)); |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1374 | hci_dev_unlock(hdev); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 1375 | hci_dev_put(hdev); |
| 1376 | |
| 1377 | return err; |
| 1378 | } |
| 1379 | |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 1380 | static int disconnect(struct sock *sk, u16 index, void *data, u16 len) |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 1381 | { |
| 1382 | struct hci_dev *hdev; |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 1383 | struct mgmt_cp_disconnect *cp = data; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 1384 | struct hci_cp_disconnect dc; |
Johan Hedberg | 366a033 | 2011-02-19 12:05:55 -0300 | [diff] [blame] | 1385 | struct pending_cmd *cmd; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 1386 | struct hci_conn *conn; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 1387 | int err; |
| 1388 | |
| 1389 | BT_DBG(""); |
| 1390 | |
Szymon Janc | bdce7ba | 2011-02-25 19:05:49 +0100 | [diff] [blame] | 1391 | if (len != sizeof(*cp)) |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 1392 | return cmd_status(sk, index, MGMT_OP_DISCONNECT, |
| 1393 | MGMT_STATUS_INVALID_PARAMS); |
Szymon Janc | bdce7ba | 2011-02-25 19:05:49 +0100 | [diff] [blame] | 1394 | |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 1395 | hdev = hci_dev_get(index); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 1396 | if (!hdev) |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 1397 | return cmd_status(sk, index, MGMT_OP_DISCONNECT, |
| 1398 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 1399 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1400 | hci_dev_lock(hdev); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 1401 | |
| 1402 | if (!test_bit(HCI_UP, &hdev->flags)) { |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 1403 | err = cmd_status(sk, index, MGMT_OP_DISCONNECT, |
| 1404 | MGMT_STATUS_NOT_POWERED); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 1405 | goto failed; |
| 1406 | } |
| 1407 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 1408 | if (mgmt_pending_find(MGMT_OP_DISCONNECT, hdev)) { |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 1409 | err = cmd_status(sk, index, MGMT_OP_DISCONNECT, |
| 1410 | MGMT_STATUS_BUSY); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 1411 | goto failed; |
| 1412 | } |
| 1413 | |
Johan Hedberg | 88c3df1 | 2012-02-09 14:27:38 +0200 | [diff] [blame] | 1414 | if (cp->addr.type == MGMT_ADDR_BREDR) |
| 1415 | conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &cp->addr.bdaddr); |
| 1416 | else |
| 1417 | conn = hci_conn_hash_lookup_ba(hdev, LE_LINK, &cp->addr.bdaddr); |
Vinicius Costa Gomes | 365227e | 2011-05-06 18:41:44 -0300 | [diff] [blame] | 1418 | |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 1419 | if (!conn) { |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 1420 | err = cmd_status(sk, index, MGMT_OP_DISCONNECT, |
| 1421 | MGMT_STATUS_NOT_CONNECTED); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 1422 | goto failed; |
| 1423 | } |
| 1424 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 1425 | cmd = mgmt_pending_add(sk, MGMT_OP_DISCONNECT, hdev, data, len); |
Johan Hedberg | 366a033 | 2011-02-19 12:05:55 -0300 | [diff] [blame] | 1426 | if (!cmd) { |
| 1427 | err = -ENOMEM; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 1428 | goto failed; |
Johan Hedberg | 366a033 | 2011-02-19 12:05:55 -0300 | [diff] [blame] | 1429 | } |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 1430 | |
| 1431 | put_unaligned_le16(conn->handle, &dc.handle); |
| 1432 | dc.reason = 0x13; /* Remote User Terminated Connection */ |
| 1433 | |
| 1434 | err = hci_send_cmd(hdev, HCI_OP_DISCONNECT, sizeof(dc), &dc); |
| 1435 | if (err < 0) |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 1436 | mgmt_pending_remove(cmd); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 1437 | |
| 1438 | failed: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1439 | hci_dev_unlock(hdev); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 1440 | hci_dev_put(hdev); |
| 1441 | |
| 1442 | return err; |
| 1443 | } |
| 1444 | |
Johan Hedberg | 48264f0 | 2011-11-09 13:58:58 +0200 | [diff] [blame] | 1445 | static u8 link_to_mgmt(u8 link_type, u8 addr_type) |
Johan Hedberg | 4c659c3 | 2011-11-07 23:13:39 +0200 | [diff] [blame] | 1446 | { |
| 1447 | switch (link_type) { |
| 1448 | case LE_LINK: |
Johan Hedberg | 48264f0 | 2011-11-09 13:58:58 +0200 | [diff] [blame] | 1449 | switch (addr_type) { |
| 1450 | case ADDR_LE_DEV_PUBLIC: |
| 1451 | return MGMT_ADDR_LE_PUBLIC; |
| 1452 | case ADDR_LE_DEV_RANDOM: |
| 1453 | return MGMT_ADDR_LE_RANDOM; |
| 1454 | default: |
| 1455 | return MGMT_ADDR_INVALID; |
| 1456 | } |
Johan Hedberg | 4c659c3 | 2011-11-07 23:13:39 +0200 | [diff] [blame] | 1457 | case ACL_LINK: |
| 1458 | return MGMT_ADDR_BREDR; |
| 1459 | default: |
| 1460 | return MGMT_ADDR_INVALID; |
| 1461 | } |
| 1462 | } |
| 1463 | |
Szymon Janc | 8ce6284 | 2011-03-01 16:55:32 +0100 | [diff] [blame] | 1464 | static int get_connections(struct sock *sk, u16 index) |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 1465 | { |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 1466 | struct mgmt_rp_get_connections *rp; |
| 1467 | struct hci_dev *hdev; |
Luiz Augusto von Dentz | 8035ded | 2011-11-01 10:58:56 +0200 | [diff] [blame] | 1468 | struct hci_conn *c; |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 1469 | size_t rp_len; |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 1470 | u16 count; |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 1471 | int i, err; |
| 1472 | |
| 1473 | BT_DBG(""); |
| 1474 | |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 1475 | hdev = hci_dev_get(index); |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 1476 | if (!hdev) |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 1477 | return cmd_status(sk, index, MGMT_OP_GET_CONNECTIONS, |
| 1478 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 1479 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1480 | hci_dev_lock(hdev); |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 1481 | |
| 1482 | count = 0; |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 1483 | list_for_each_entry(c, &hdev->conn_hash.list, list) { |
| 1484 | if (test_bit(HCI_CONN_MGMT_CONNECTED, &c->flags)) |
| 1485 | count++; |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 1486 | } |
| 1487 | |
Johan Hedberg | 4c659c3 | 2011-11-07 23:13:39 +0200 | [diff] [blame] | 1488 | rp_len = sizeof(*rp) + (count * sizeof(struct mgmt_addr_info)); |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 1489 | rp = kmalloc(rp_len, GFP_ATOMIC); |
| 1490 | if (!rp) { |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 1491 | err = -ENOMEM; |
| 1492 | goto unlock; |
| 1493 | } |
| 1494 | |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 1495 | put_unaligned_le16(count, &rp->conn_count); |
| 1496 | |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 1497 | i = 0; |
Johan Hedberg | 4c659c3 | 2011-11-07 23:13:39 +0200 | [diff] [blame] | 1498 | list_for_each_entry(c, &hdev->conn_hash.list, list) { |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 1499 | if (!test_bit(HCI_CONN_MGMT_CONNECTED, &c->flags)) |
| 1500 | continue; |
Johan Hedberg | 4c659c3 | 2011-11-07 23:13:39 +0200 | [diff] [blame] | 1501 | bacpy(&rp->addr[i].bdaddr, &c->dst); |
Johan Hedberg | 48264f0 | 2011-11-09 13:58:58 +0200 | [diff] [blame] | 1502 | rp->addr[i].type = link_to_mgmt(c->type, c->dst_type); |
Johan Hedberg | 4c659c3 | 2011-11-07 23:13:39 +0200 | [diff] [blame] | 1503 | if (rp->addr[i].type == MGMT_ADDR_INVALID) |
| 1504 | continue; |
| 1505 | i++; |
| 1506 | } |
| 1507 | |
| 1508 | /* Recalculate length in case of filtered SCO connections, etc */ |
| 1509 | rp_len = sizeof(*rp) + (i * sizeof(struct mgmt_addr_info)); |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 1510 | |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 1511 | err = cmd_complete(sk, index, MGMT_OP_GET_CONNECTIONS, rp, rp_len); |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 1512 | |
| 1513 | unlock: |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 1514 | kfree(rp); |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1515 | hci_dev_unlock(hdev); |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 1516 | hci_dev_put(hdev); |
| 1517 | return err; |
| 1518 | } |
| 1519 | |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 1520 | static int send_pin_code_neg_reply(struct sock *sk, u16 index, |
| 1521 | struct hci_dev *hdev, struct mgmt_cp_pin_code_neg_reply *cp) |
| 1522 | { |
| 1523 | struct pending_cmd *cmd; |
| 1524 | int err; |
| 1525 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 1526 | cmd = mgmt_pending_add(sk, MGMT_OP_PIN_CODE_NEG_REPLY, hdev, cp, |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 1527 | sizeof(*cp)); |
| 1528 | if (!cmd) |
| 1529 | return -ENOMEM; |
| 1530 | |
Johan Hedberg | d845769 | 2012-02-17 14:24:57 +0200 | [diff] [blame] | 1531 | err = hci_send_cmd(hdev, HCI_OP_PIN_CODE_NEG_REPLY, |
| 1532 | sizeof(cp->addr.bdaddr), &cp->addr.bdaddr); |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 1533 | if (err < 0) |
| 1534 | mgmt_pending_remove(cmd); |
| 1535 | |
| 1536 | return err; |
| 1537 | } |
| 1538 | |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 1539 | static int pin_code_reply(struct sock *sk, u16 index, void *data, u16 len) |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 1540 | { |
| 1541 | struct hci_dev *hdev; |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 1542 | struct hci_conn *conn; |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 1543 | struct mgmt_cp_pin_code_reply *cp = data; |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 1544 | struct hci_cp_pin_code_reply reply; |
Johan Hedberg | 366a033 | 2011-02-19 12:05:55 -0300 | [diff] [blame] | 1545 | struct pending_cmd *cmd; |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 1546 | int err; |
| 1547 | |
| 1548 | BT_DBG(""); |
| 1549 | |
Szymon Janc | bdce7ba | 2011-02-25 19:05:49 +0100 | [diff] [blame] | 1550 | if (len != sizeof(*cp)) |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 1551 | return cmd_status(sk, index, MGMT_OP_PIN_CODE_REPLY, |
| 1552 | MGMT_STATUS_INVALID_PARAMS); |
Szymon Janc | bdce7ba | 2011-02-25 19:05:49 +0100 | [diff] [blame] | 1553 | |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 1554 | hdev = hci_dev_get(index); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 1555 | if (!hdev) |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 1556 | return cmd_status(sk, index, MGMT_OP_PIN_CODE_REPLY, |
| 1557 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 1558 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1559 | hci_dev_lock(hdev); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 1560 | |
| 1561 | if (!test_bit(HCI_UP, &hdev->flags)) { |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 1562 | err = cmd_status(sk, index, MGMT_OP_PIN_CODE_REPLY, |
| 1563 | MGMT_STATUS_NOT_POWERED); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 1564 | goto failed; |
| 1565 | } |
| 1566 | |
Johan Hedberg | d845769 | 2012-02-17 14:24:57 +0200 | [diff] [blame] | 1567 | conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &cp->addr.bdaddr); |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 1568 | if (!conn) { |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 1569 | err = cmd_status(sk, index, MGMT_OP_PIN_CODE_REPLY, |
| 1570 | MGMT_STATUS_NOT_CONNECTED); |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 1571 | goto failed; |
| 1572 | } |
| 1573 | |
| 1574 | if (conn->pending_sec_level == BT_SECURITY_HIGH && cp->pin_len != 16) { |
Johan Hedberg | d845769 | 2012-02-17 14:24:57 +0200 | [diff] [blame] | 1575 | struct mgmt_cp_pin_code_neg_reply ncp; |
| 1576 | |
| 1577 | memcpy(&ncp.addr, &cp->addr, sizeof(ncp.addr)); |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 1578 | |
| 1579 | BT_ERR("PIN code is not 16 bytes long"); |
| 1580 | |
| 1581 | err = send_pin_code_neg_reply(sk, index, hdev, &ncp); |
| 1582 | if (err >= 0) |
| 1583 | err = cmd_status(sk, index, MGMT_OP_PIN_CODE_REPLY, |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 1584 | MGMT_STATUS_INVALID_PARAMS); |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 1585 | |
| 1586 | goto failed; |
| 1587 | } |
| 1588 | |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 1589 | cmd = mgmt_pending_add(sk, MGMT_OP_PIN_CODE_REPLY, hdev, data, |
| 1590 | len); |
Johan Hedberg | 366a033 | 2011-02-19 12:05:55 -0300 | [diff] [blame] | 1591 | if (!cmd) { |
| 1592 | err = -ENOMEM; |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 1593 | goto failed; |
Johan Hedberg | 366a033 | 2011-02-19 12:05:55 -0300 | [diff] [blame] | 1594 | } |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 1595 | |
Johan Hedberg | d845769 | 2012-02-17 14:24:57 +0200 | [diff] [blame] | 1596 | bacpy(&reply.bdaddr, &cp->addr.bdaddr); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 1597 | reply.pin_len = cp->pin_len; |
Waldemar Rymarkiewicz | 24718ca | 2011-06-01 17:28:47 +0200 | [diff] [blame] | 1598 | memcpy(reply.pin_code, cp->pin_code, sizeof(reply.pin_code)); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 1599 | |
| 1600 | err = hci_send_cmd(hdev, HCI_OP_PIN_CODE_REPLY, sizeof(reply), &reply); |
| 1601 | if (err < 0) |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 1602 | mgmt_pending_remove(cmd); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 1603 | |
| 1604 | failed: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1605 | hci_dev_unlock(hdev); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 1606 | hci_dev_put(hdev); |
| 1607 | |
| 1608 | return err; |
| 1609 | } |
| 1610 | |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 1611 | static int pin_code_neg_reply(struct sock *sk, u16 index, void *data, u16 len) |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 1612 | { |
| 1613 | struct hci_dev *hdev; |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 1614 | struct mgmt_cp_pin_code_neg_reply *cp = data; |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 1615 | int err; |
| 1616 | |
| 1617 | BT_DBG(""); |
| 1618 | |
Szymon Janc | bdce7ba | 2011-02-25 19:05:49 +0100 | [diff] [blame] | 1619 | if (len != sizeof(*cp)) |
| 1620 | return cmd_status(sk, index, MGMT_OP_PIN_CODE_NEG_REPLY, |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 1621 | MGMT_STATUS_INVALID_PARAMS); |
Szymon Janc | bdce7ba | 2011-02-25 19:05:49 +0100 | [diff] [blame] | 1622 | |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 1623 | hdev = hci_dev_get(index); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 1624 | if (!hdev) |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 1625 | return cmd_status(sk, index, MGMT_OP_PIN_CODE_NEG_REPLY, |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 1626 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 1627 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1628 | hci_dev_lock(hdev); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 1629 | |
| 1630 | if (!test_bit(HCI_UP, &hdev->flags)) { |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 1631 | err = cmd_status(sk, index, MGMT_OP_PIN_CODE_NEG_REPLY, |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 1632 | MGMT_STATUS_NOT_POWERED); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 1633 | goto failed; |
| 1634 | } |
| 1635 | |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 1636 | err = send_pin_code_neg_reply(sk, index, hdev, cp); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 1637 | |
| 1638 | failed: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1639 | hci_dev_unlock(hdev); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 1640 | hci_dev_put(hdev); |
| 1641 | |
| 1642 | return err; |
| 1643 | } |
| 1644 | |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 1645 | static int set_io_capability(struct sock *sk, u16 index, void *data, u16 len) |
Johan Hedberg | 17fa4b9 | 2011-01-25 13:28:33 +0200 | [diff] [blame] | 1646 | { |
| 1647 | struct hci_dev *hdev; |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 1648 | struct mgmt_cp_set_io_capability *cp = data; |
Johan Hedberg | 17fa4b9 | 2011-01-25 13:28:33 +0200 | [diff] [blame] | 1649 | |
| 1650 | BT_DBG(""); |
| 1651 | |
Szymon Janc | bdce7ba | 2011-02-25 19:05:49 +0100 | [diff] [blame] | 1652 | if (len != sizeof(*cp)) |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 1653 | return cmd_status(sk, index, MGMT_OP_SET_IO_CAPABILITY, |
| 1654 | MGMT_STATUS_INVALID_PARAMS); |
Szymon Janc | bdce7ba | 2011-02-25 19:05:49 +0100 | [diff] [blame] | 1655 | |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 1656 | hdev = hci_dev_get(index); |
Johan Hedberg | 17fa4b9 | 2011-01-25 13:28:33 +0200 | [diff] [blame] | 1657 | if (!hdev) |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 1658 | return cmd_status(sk, index, MGMT_OP_SET_IO_CAPABILITY, |
| 1659 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | 17fa4b9 | 2011-01-25 13:28:33 +0200 | [diff] [blame] | 1660 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1661 | hci_dev_lock(hdev); |
Johan Hedberg | 17fa4b9 | 2011-01-25 13:28:33 +0200 | [diff] [blame] | 1662 | |
| 1663 | hdev->io_capability = cp->io_capability; |
| 1664 | |
| 1665 | BT_DBG("%s IO capability set to 0x%02x", hdev->name, |
Szymon Janc | b8534e0 | 2011-03-01 16:55:34 +0100 | [diff] [blame] | 1666 | hdev->io_capability); |
Johan Hedberg | 17fa4b9 | 2011-01-25 13:28:33 +0200 | [diff] [blame] | 1667 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1668 | hci_dev_unlock(hdev); |
Johan Hedberg | 17fa4b9 | 2011-01-25 13:28:33 +0200 | [diff] [blame] | 1669 | hci_dev_put(hdev); |
| 1670 | |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 1671 | return cmd_complete(sk, index, MGMT_OP_SET_IO_CAPABILITY, NULL, 0); |
Johan Hedberg | 17fa4b9 | 2011-01-25 13:28:33 +0200 | [diff] [blame] | 1672 | } |
| 1673 | |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 1674 | static inline struct pending_cmd *find_pairing(struct hci_conn *conn) |
| 1675 | { |
| 1676 | struct hci_dev *hdev = conn->hdev; |
Luiz Augusto von Dentz | 8035ded | 2011-11-01 10:58:56 +0200 | [diff] [blame] | 1677 | struct pending_cmd *cmd; |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 1678 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 1679 | list_for_each_entry(cmd, &hdev->mgmt_pending, list) { |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 1680 | if (cmd->opcode != MGMT_OP_PAIR_DEVICE) |
| 1681 | continue; |
| 1682 | |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 1683 | if (cmd->user_data != conn) |
| 1684 | continue; |
| 1685 | |
| 1686 | return cmd; |
| 1687 | } |
| 1688 | |
| 1689 | return NULL; |
| 1690 | } |
| 1691 | |
| 1692 | static void pairing_complete(struct pending_cmd *cmd, u8 status) |
| 1693 | { |
| 1694 | struct mgmt_rp_pair_device rp; |
| 1695 | struct hci_conn *conn = cmd->user_data; |
| 1696 | |
Johan Hedberg | ba4e564 | 2011-11-11 00:07:34 +0200 | [diff] [blame] | 1697 | bacpy(&rp.addr.bdaddr, &conn->dst); |
| 1698 | rp.addr.type = link_to_mgmt(conn->type, conn->dst_type); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 1699 | rp.status = status; |
| 1700 | |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 1701 | 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] | 1702 | |
| 1703 | /* So we don't get further callbacks for this connection */ |
| 1704 | conn->connect_cfm_cb = NULL; |
| 1705 | conn->security_cfm_cb = NULL; |
| 1706 | conn->disconn_cfm_cb = NULL; |
| 1707 | |
| 1708 | hci_conn_put(conn); |
| 1709 | |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 1710 | mgmt_pending_remove(cmd); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 1711 | } |
| 1712 | |
| 1713 | static void pairing_complete_cb(struct hci_conn *conn, u8 status) |
| 1714 | { |
| 1715 | struct pending_cmd *cmd; |
| 1716 | |
| 1717 | BT_DBG("status %u", status); |
| 1718 | |
Johan Hedberg | 56e5cb8 | 2011-11-08 20:40:16 +0200 | [diff] [blame] | 1719 | cmd = find_pairing(conn); |
| 1720 | if (!cmd) |
| 1721 | BT_DBG("Unable to find a pending command"); |
| 1722 | else |
| 1723 | pairing_complete(cmd, status); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 1724 | } |
| 1725 | |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 1726 | static int pair_device(struct sock *sk, u16 index, void *data, u16 len) |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 1727 | { |
| 1728 | struct hci_dev *hdev; |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 1729 | struct mgmt_cp_pair_device *cp = data; |
Johan Hedberg | 1425acb | 2011-11-11 00:07:35 +0200 | [diff] [blame] | 1730 | struct mgmt_rp_pair_device rp; |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 1731 | struct pending_cmd *cmd; |
| 1732 | u8 sec_level, auth_type; |
| 1733 | struct hci_conn *conn; |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 1734 | int err; |
| 1735 | |
| 1736 | BT_DBG(""); |
| 1737 | |
Szymon Janc | bdce7ba | 2011-02-25 19:05:49 +0100 | [diff] [blame] | 1738 | if (len != sizeof(*cp)) |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 1739 | return cmd_status(sk, index, MGMT_OP_PAIR_DEVICE, |
| 1740 | MGMT_STATUS_INVALID_PARAMS); |
Szymon Janc | bdce7ba | 2011-02-25 19:05:49 +0100 | [diff] [blame] | 1741 | |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 1742 | hdev = hci_dev_get(index); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 1743 | if (!hdev) |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 1744 | return cmd_status(sk, index, MGMT_OP_PAIR_DEVICE, |
| 1745 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 1746 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1747 | hci_dev_lock(hdev); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 1748 | |
Vinicius Costa Gomes | c908df3 | 2011-09-02 14:51:22 -0300 | [diff] [blame] | 1749 | sec_level = BT_SECURITY_MEDIUM; |
| 1750 | if (cp->io_cap == 0x03) |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 1751 | auth_type = HCI_AT_DEDICATED_BONDING; |
Vinicius Costa Gomes | c908df3 | 2011-09-02 14:51:22 -0300 | [diff] [blame] | 1752 | else |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 1753 | auth_type = HCI_AT_DEDICATED_BONDING_MITM; |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 1754 | |
Johan Hedberg | ba4e564 | 2011-11-11 00:07:34 +0200 | [diff] [blame] | 1755 | if (cp->addr.type == MGMT_ADDR_BREDR) |
| 1756 | conn = hci_connect(hdev, ACL_LINK, &cp->addr.bdaddr, sec_level, |
Vinicius Costa Gomes | 7a512d0 | 2011-08-19 21:06:54 -0300 | [diff] [blame] | 1757 | auth_type); |
| 1758 | else |
Johan Hedberg | ba4e564 | 2011-11-11 00:07:34 +0200 | [diff] [blame] | 1759 | conn = hci_connect(hdev, LE_LINK, &cp->addr.bdaddr, sec_level, |
Vinicius Costa Gomes | 7a512d0 | 2011-08-19 21:06:54 -0300 | [diff] [blame] | 1760 | auth_type); |
| 1761 | |
Johan Hedberg | 1425acb | 2011-11-11 00:07:35 +0200 | [diff] [blame] | 1762 | memset(&rp, 0, sizeof(rp)); |
| 1763 | bacpy(&rp.addr.bdaddr, &cp->addr.bdaddr); |
| 1764 | rp.addr.type = cp->addr.type; |
| 1765 | |
Ville Tervo | 30e7627 | 2011-02-22 16:10:53 -0300 | [diff] [blame] | 1766 | if (IS_ERR(conn)) { |
Johan Hedberg | 1425acb | 2011-11-11 00:07:35 +0200 | [diff] [blame] | 1767 | rp.status = -PTR_ERR(conn); |
| 1768 | err = cmd_complete(sk, index, MGMT_OP_PAIR_DEVICE, |
| 1769 | &rp, sizeof(rp)); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 1770 | goto unlock; |
| 1771 | } |
| 1772 | |
| 1773 | if (conn->connect_cfm_cb) { |
| 1774 | hci_conn_put(conn); |
Johan Hedberg | 1425acb | 2011-11-11 00:07:35 +0200 | [diff] [blame] | 1775 | rp.status = EBUSY; |
| 1776 | err = cmd_complete(sk, index, MGMT_OP_PAIR_DEVICE, |
| 1777 | &rp, sizeof(rp)); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 1778 | goto unlock; |
| 1779 | } |
| 1780 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 1781 | cmd = mgmt_pending_add(sk, MGMT_OP_PAIR_DEVICE, hdev, data, len); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 1782 | if (!cmd) { |
| 1783 | err = -ENOMEM; |
| 1784 | hci_conn_put(conn); |
| 1785 | goto unlock; |
| 1786 | } |
| 1787 | |
Vinicius Costa Gomes | 7a512d0 | 2011-08-19 21:06:54 -0300 | [diff] [blame] | 1788 | /* For LE, just connecting isn't a proof that the pairing finished */ |
Johan Hedberg | ba4e564 | 2011-11-11 00:07:34 +0200 | [diff] [blame] | 1789 | if (cp->addr.type == MGMT_ADDR_BREDR) |
Vinicius Costa Gomes | 7a512d0 | 2011-08-19 21:06:54 -0300 | [diff] [blame] | 1790 | conn->connect_cfm_cb = pairing_complete_cb; |
| 1791 | |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 1792 | conn->security_cfm_cb = pairing_complete_cb; |
| 1793 | conn->disconn_cfm_cb = pairing_complete_cb; |
| 1794 | conn->io_capability = cp->io_cap; |
| 1795 | cmd->user_data = conn; |
| 1796 | |
| 1797 | if (conn->state == BT_CONNECTED && |
| 1798 | hci_conn_security(conn, sec_level, auth_type)) |
| 1799 | pairing_complete(cmd, 0); |
| 1800 | |
| 1801 | err = 0; |
| 1802 | |
| 1803 | unlock: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1804 | hci_dev_unlock(hdev); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 1805 | hci_dev_put(hdev); |
| 1806 | |
| 1807 | return err; |
| 1808 | } |
| 1809 | |
Johan Hedberg | 2842470 | 2012-02-02 04:02:29 +0200 | [diff] [blame] | 1810 | static int cancel_pair_device(struct sock *sk, u16 index, |
| 1811 | unsigned char *data, u16 len) |
| 1812 | { |
| 1813 | struct mgmt_addr_info *addr = (void *) data; |
| 1814 | struct hci_dev *hdev; |
| 1815 | struct pending_cmd *cmd; |
| 1816 | struct hci_conn *conn; |
| 1817 | int err; |
| 1818 | |
| 1819 | BT_DBG(""); |
| 1820 | |
| 1821 | if (len != sizeof(*addr)) |
| 1822 | return cmd_status(sk, index, MGMT_OP_CANCEL_PAIR_DEVICE, |
| 1823 | MGMT_STATUS_INVALID_PARAMS); |
| 1824 | |
| 1825 | hdev = hci_dev_get(index); |
| 1826 | if (!hdev) |
| 1827 | return cmd_status(sk, index, MGMT_OP_CANCEL_PAIR_DEVICE, |
| 1828 | MGMT_STATUS_INVALID_PARAMS); |
| 1829 | |
| 1830 | hci_dev_lock(hdev); |
| 1831 | |
| 1832 | cmd = mgmt_pending_find(MGMT_OP_PAIR_DEVICE, hdev); |
| 1833 | if (!cmd) { |
| 1834 | err = cmd_status(sk, index, MGMT_OP_CANCEL_PAIR_DEVICE, |
| 1835 | MGMT_STATUS_INVALID_PARAMS); |
| 1836 | goto unlock; |
| 1837 | } |
| 1838 | |
| 1839 | conn = cmd->user_data; |
| 1840 | |
| 1841 | if (bacmp(&addr->bdaddr, &conn->dst) != 0) { |
| 1842 | err = cmd_status(sk, index, MGMT_OP_CANCEL_PAIR_DEVICE, |
| 1843 | MGMT_STATUS_INVALID_PARAMS); |
| 1844 | goto unlock; |
| 1845 | } |
| 1846 | |
| 1847 | pairing_complete(cmd, MGMT_STATUS_CANCELLED); |
| 1848 | |
| 1849 | err = cmd_complete(sk, index, MGMT_OP_CANCEL_PAIR_DEVICE, addr, |
| 1850 | sizeof(*addr)); |
| 1851 | unlock: |
| 1852 | hci_dev_unlock(hdev); |
| 1853 | hci_dev_put(hdev); |
| 1854 | |
| 1855 | return err; |
| 1856 | } |
| 1857 | |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 1858 | static int user_pairing_resp(struct sock *sk, u16 index, bdaddr_t *bdaddr, |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 1859 | u8 type, u16 mgmt_op, u16 hci_op, |
| 1860 | __le32 passkey) |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 1861 | { |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 1862 | struct pending_cmd *cmd; |
| 1863 | struct hci_dev *hdev; |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 1864 | struct hci_conn *conn; |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 1865 | int err; |
| 1866 | |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 1867 | hdev = hci_dev_get(index); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 1868 | if (!hdev) |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 1869 | return cmd_status(sk, index, mgmt_op, |
| 1870 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 1871 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1872 | hci_dev_lock(hdev); |
Johan Hedberg | 08ba538 | 2011-03-16 14:29:34 +0200 | [diff] [blame] | 1873 | |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 1874 | if (!test_bit(HCI_UP, &hdev->flags)) { |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 1875 | err = cmd_status(sk, index, mgmt_op, MGMT_STATUS_NOT_POWERED); |
| 1876 | goto done; |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 1877 | } |
| 1878 | |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 1879 | if (type == MGMT_ADDR_BREDR) |
| 1880 | conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, bdaddr); |
| 1881 | else |
Brian Gix | 47c15e2 | 2011-11-16 13:53:14 -0800 | [diff] [blame] | 1882 | conn = hci_conn_hash_lookup_ba(hdev, LE_LINK, bdaddr); |
Brian Gix | 47c15e2 | 2011-11-16 13:53:14 -0800 | [diff] [blame] | 1883 | |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 1884 | if (!conn) { |
| 1885 | err = cmd_status(sk, index, mgmt_op, |
| 1886 | MGMT_STATUS_NOT_CONNECTED); |
| 1887 | goto done; |
| 1888 | } |
| 1889 | |
| 1890 | if (type == MGMT_ADDR_LE_PUBLIC || type == MGMT_ADDR_LE_RANDOM) { |
Brian Gix | 47c15e2 | 2011-11-16 13:53:14 -0800 | [diff] [blame] | 1891 | /* Continue with pairing via SMP */ |
Brian Gix | 5fe57d9 | 2011-12-21 16:12:13 -0800 | [diff] [blame] | 1892 | err = smp_user_confirm_reply(conn, mgmt_op, passkey); |
Brian Gix | 47c15e2 | 2011-11-16 13:53:14 -0800 | [diff] [blame] | 1893 | |
Brian Gix | 5fe57d9 | 2011-12-21 16:12:13 -0800 | [diff] [blame] | 1894 | if (!err) |
| 1895 | err = cmd_status(sk, index, mgmt_op, |
| 1896 | MGMT_STATUS_SUCCESS); |
| 1897 | else |
| 1898 | err = cmd_status(sk, index, mgmt_op, |
| 1899 | MGMT_STATUS_FAILED); |
| 1900 | |
Brian Gix | 47c15e2 | 2011-11-16 13:53:14 -0800 | [diff] [blame] | 1901 | goto done; |
| 1902 | } |
| 1903 | |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 1904 | cmd = mgmt_pending_add(sk, mgmt_op, hdev, bdaddr, sizeof(*bdaddr)); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 1905 | if (!cmd) { |
| 1906 | err = -ENOMEM; |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 1907 | goto done; |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 1908 | } |
| 1909 | |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 1910 | /* Continue with pairing via HCI */ |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 1911 | if (hci_op == HCI_OP_USER_PASSKEY_REPLY) { |
| 1912 | struct hci_cp_user_passkey_reply cp; |
| 1913 | |
| 1914 | bacpy(&cp.bdaddr, bdaddr); |
| 1915 | cp.passkey = passkey; |
| 1916 | err = hci_send_cmd(hdev, hci_op, sizeof(cp), &cp); |
| 1917 | } else |
| 1918 | err = hci_send_cmd(hdev, hci_op, sizeof(*bdaddr), bdaddr); |
| 1919 | |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 1920 | if (err < 0) |
| 1921 | mgmt_pending_remove(cmd); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 1922 | |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 1923 | done: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1924 | hci_dev_unlock(hdev); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 1925 | hci_dev_put(hdev); |
| 1926 | |
| 1927 | return err; |
| 1928 | } |
| 1929 | |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 1930 | static int user_confirm_reply(struct sock *sk, u16 index, void *data, u16 len) |
| 1931 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 1932 | struct mgmt_cp_user_confirm_reply *cp = data; |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 1933 | |
| 1934 | BT_DBG(""); |
| 1935 | |
| 1936 | if (len != sizeof(*cp)) |
| 1937 | return cmd_status(sk, index, MGMT_OP_USER_CONFIRM_REPLY, |
| 1938 | MGMT_STATUS_INVALID_PARAMS); |
| 1939 | |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 1940 | return user_pairing_resp(sk, index, &cp->addr.bdaddr, cp->addr.type, |
| 1941 | MGMT_OP_USER_CONFIRM_REPLY, |
| 1942 | HCI_OP_USER_CONFIRM_REPLY, 0); |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 1943 | } |
| 1944 | |
| 1945 | static int user_confirm_neg_reply(struct sock *sk, u16 index, void *data, |
| 1946 | u16 len) |
| 1947 | { |
Johan Hedberg | c9c2659 | 2011-12-15 00:47:41 +0200 | [diff] [blame] | 1948 | struct mgmt_cp_user_confirm_neg_reply *cp = data; |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 1949 | |
| 1950 | BT_DBG(""); |
| 1951 | |
| 1952 | if (len != sizeof(*cp)) |
| 1953 | return cmd_status(sk, index, MGMT_OP_USER_CONFIRM_NEG_REPLY, |
| 1954 | MGMT_STATUS_INVALID_PARAMS); |
| 1955 | |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 1956 | return user_pairing_resp(sk, index, &cp->addr.bdaddr, cp->addr.type, |
| 1957 | MGMT_OP_USER_CONFIRM_NEG_REPLY, |
| 1958 | HCI_OP_USER_CONFIRM_NEG_REPLY, 0); |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 1959 | } |
| 1960 | |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 1961 | static int user_passkey_reply(struct sock *sk, u16 index, void *data, u16 len) |
| 1962 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 1963 | struct mgmt_cp_user_passkey_reply *cp = data; |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 1964 | |
| 1965 | BT_DBG(""); |
| 1966 | |
| 1967 | if (len != sizeof(*cp)) |
| 1968 | return cmd_status(sk, index, MGMT_OP_USER_PASSKEY_REPLY, |
| 1969 | EINVAL); |
| 1970 | |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 1971 | return user_pairing_resp(sk, index, &cp->addr.bdaddr, cp->addr.type, |
| 1972 | MGMT_OP_USER_PASSKEY_REPLY, |
| 1973 | HCI_OP_USER_PASSKEY_REPLY, |
| 1974 | cp->passkey); |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 1975 | } |
| 1976 | |
| 1977 | static int user_passkey_neg_reply(struct sock *sk, u16 index, void *data, |
| 1978 | u16 len) |
| 1979 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 1980 | struct mgmt_cp_user_passkey_neg_reply *cp = data; |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 1981 | |
| 1982 | BT_DBG(""); |
| 1983 | |
| 1984 | if (len != sizeof(*cp)) |
| 1985 | return cmd_status(sk, index, MGMT_OP_USER_PASSKEY_NEG_REPLY, |
| 1986 | EINVAL); |
| 1987 | |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 1988 | return user_pairing_resp(sk, index, &cp->addr.bdaddr, cp->addr.type, |
| 1989 | MGMT_OP_USER_PASSKEY_NEG_REPLY, |
| 1990 | HCI_OP_USER_PASSKEY_NEG_REPLY, 0); |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 1991 | } |
| 1992 | |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 1993 | static int set_local_name(struct sock *sk, u16 index, void *data, |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 1994 | u16 len) |
| 1995 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 1996 | struct mgmt_cp_set_local_name *mgmt_cp = data; |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 1997 | struct hci_cp_write_local_name hci_cp; |
| 1998 | struct hci_dev *hdev; |
| 1999 | struct pending_cmd *cmd; |
| 2000 | int err; |
| 2001 | |
| 2002 | BT_DBG(""); |
| 2003 | |
| 2004 | if (len != sizeof(*mgmt_cp)) |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 2005 | return cmd_status(sk, index, MGMT_OP_SET_LOCAL_NAME, |
| 2006 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 2007 | |
| 2008 | hdev = hci_dev_get(index); |
| 2009 | if (!hdev) |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 2010 | return cmd_status(sk, index, MGMT_OP_SET_LOCAL_NAME, |
| 2011 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 2012 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2013 | hci_dev_lock(hdev); |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 2014 | |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 2015 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_LOCAL_NAME, hdev, data, |
| 2016 | len); |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 2017 | if (!cmd) { |
| 2018 | err = -ENOMEM; |
| 2019 | goto failed; |
| 2020 | } |
| 2021 | |
| 2022 | memcpy(hci_cp.name, mgmt_cp->name, sizeof(hci_cp.name)); |
| 2023 | err = hci_send_cmd(hdev, HCI_OP_WRITE_LOCAL_NAME, sizeof(hci_cp), |
| 2024 | &hci_cp); |
| 2025 | if (err < 0) |
| 2026 | mgmt_pending_remove(cmd); |
| 2027 | |
| 2028 | failed: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2029 | hci_dev_unlock(hdev); |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 2030 | hci_dev_put(hdev); |
| 2031 | |
| 2032 | return err; |
| 2033 | } |
| 2034 | |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 2035 | static int read_local_oob_data(struct sock *sk, u16 index) |
| 2036 | { |
| 2037 | struct hci_dev *hdev; |
| 2038 | struct pending_cmd *cmd; |
| 2039 | int err; |
| 2040 | |
| 2041 | BT_DBG("hci%u", index); |
| 2042 | |
| 2043 | hdev = hci_dev_get(index); |
| 2044 | if (!hdev) |
| 2045 | return cmd_status(sk, index, MGMT_OP_READ_LOCAL_OOB_DATA, |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 2046 | MGMT_STATUS_INVALID_PARAMS); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 2047 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2048 | hci_dev_lock(hdev); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 2049 | |
| 2050 | if (!test_bit(HCI_UP, &hdev->flags)) { |
| 2051 | err = cmd_status(sk, index, MGMT_OP_READ_LOCAL_OOB_DATA, |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 2052 | MGMT_STATUS_NOT_POWERED); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 2053 | goto unlock; |
| 2054 | } |
| 2055 | |
| 2056 | if (!(hdev->features[6] & LMP_SIMPLE_PAIR)) { |
| 2057 | err = cmd_status(sk, index, MGMT_OP_READ_LOCAL_OOB_DATA, |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 2058 | MGMT_STATUS_NOT_SUPPORTED); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 2059 | goto unlock; |
| 2060 | } |
| 2061 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 2062 | if (mgmt_pending_find(MGMT_OP_READ_LOCAL_OOB_DATA, hdev)) { |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 2063 | err = cmd_status(sk, index, MGMT_OP_READ_LOCAL_OOB_DATA, |
| 2064 | MGMT_STATUS_BUSY); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 2065 | goto unlock; |
| 2066 | } |
| 2067 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 2068 | cmd = mgmt_pending_add(sk, MGMT_OP_READ_LOCAL_OOB_DATA, hdev, NULL, 0); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 2069 | if (!cmd) { |
| 2070 | err = -ENOMEM; |
| 2071 | goto unlock; |
| 2072 | } |
| 2073 | |
| 2074 | err = hci_send_cmd(hdev, HCI_OP_READ_LOCAL_OOB_DATA, 0, NULL); |
| 2075 | if (err < 0) |
| 2076 | mgmt_pending_remove(cmd); |
| 2077 | |
| 2078 | unlock: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2079 | hci_dev_unlock(hdev); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 2080 | hci_dev_put(hdev); |
| 2081 | |
| 2082 | return err; |
| 2083 | } |
| 2084 | |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 2085 | static int add_remote_oob_data(struct sock *sk, u16 index, void *data, |
| 2086 | u16 len) |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 2087 | { |
| 2088 | struct hci_dev *hdev; |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 2089 | struct mgmt_cp_add_remote_oob_data *cp = data; |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 2090 | int err; |
| 2091 | |
| 2092 | BT_DBG("hci%u ", index); |
| 2093 | |
| 2094 | if (len != sizeof(*cp)) |
| 2095 | return cmd_status(sk, index, MGMT_OP_ADD_REMOTE_OOB_DATA, |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 2096 | MGMT_STATUS_INVALID_PARAMS); |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 2097 | |
| 2098 | hdev = hci_dev_get(index); |
| 2099 | if (!hdev) |
| 2100 | return cmd_status(sk, index, MGMT_OP_ADD_REMOTE_OOB_DATA, |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 2101 | MGMT_STATUS_INVALID_PARAMS); |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 2102 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2103 | hci_dev_lock(hdev); |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 2104 | |
Johan Hedberg | 664ce4c | 2012-02-09 15:44:09 +0200 | [diff] [blame] | 2105 | err = hci_add_remote_oob_data(hdev, &cp->addr.bdaddr, cp->hash, |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 2106 | cp->randomizer); |
| 2107 | if (err < 0) |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 2108 | err = cmd_status(sk, index, MGMT_OP_ADD_REMOTE_OOB_DATA, |
| 2109 | MGMT_STATUS_FAILED); |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 2110 | else |
| 2111 | err = cmd_complete(sk, index, MGMT_OP_ADD_REMOTE_OOB_DATA, NULL, |
| 2112 | 0); |
| 2113 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2114 | hci_dev_unlock(hdev); |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 2115 | hci_dev_put(hdev); |
| 2116 | |
| 2117 | return err; |
| 2118 | } |
| 2119 | |
| 2120 | static int remove_remote_oob_data(struct sock *sk, u16 index, |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 2121 | void *data, u16 len) |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 2122 | { |
| 2123 | struct hci_dev *hdev; |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 2124 | struct mgmt_cp_remove_remote_oob_data *cp = data; |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 2125 | int err; |
| 2126 | |
| 2127 | BT_DBG("hci%u ", index); |
| 2128 | |
| 2129 | if (len != sizeof(*cp)) |
| 2130 | return cmd_status(sk, index, MGMT_OP_REMOVE_REMOTE_OOB_DATA, |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 2131 | MGMT_STATUS_INVALID_PARAMS); |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 2132 | |
| 2133 | hdev = hci_dev_get(index); |
| 2134 | if (!hdev) |
| 2135 | return cmd_status(sk, index, MGMT_OP_REMOVE_REMOTE_OOB_DATA, |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 2136 | MGMT_STATUS_INVALID_PARAMS); |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 2137 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2138 | hci_dev_lock(hdev); |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 2139 | |
Johan Hedberg | 664ce4c | 2012-02-09 15:44:09 +0200 | [diff] [blame] | 2140 | err = hci_remove_remote_oob_data(hdev, &cp->addr.bdaddr); |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 2141 | if (err < 0) |
| 2142 | err = cmd_status(sk, index, MGMT_OP_REMOVE_REMOTE_OOB_DATA, |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 2143 | MGMT_STATUS_INVALID_PARAMS); |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 2144 | else |
| 2145 | err = cmd_complete(sk, index, MGMT_OP_REMOVE_REMOTE_OOB_DATA, |
| 2146 | NULL, 0); |
| 2147 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2148 | hci_dev_unlock(hdev); |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 2149 | hci_dev_put(hdev); |
| 2150 | |
| 2151 | return err; |
| 2152 | } |
| 2153 | |
Johan Hedberg | 450dfda | 2011-11-12 11:58:22 +0200 | [diff] [blame] | 2154 | static int start_discovery(struct sock *sk, u16 index, |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 2155 | void *data, u16 len) |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 2156 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 2157 | struct mgmt_cp_start_discovery *cp = data; |
Andre Guedes | 3fd2415 | 2012-02-03 17:48:01 -0300 | [diff] [blame] | 2158 | unsigned long discov_type = cp->type; |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 2159 | struct pending_cmd *cmd; |
| 2160 | struct hci_dev *hdev; |
| 2161 | int err; |
| 2162 | |
| 2163 | BT_DBG("hci%u", index); |
| 2164 | |
Johan Hedberg | 450dfda | 2011-11-12 11:58:22 +0200 | [diff] [blame] | 2165 | if (len != sizeof(*cp)) |
| 2166 | return cmd_status(sk, index, MGMT_OP_START_DISCOVERY, |
| 2167 | MGMT_STATUS_INVALID_PARAMS); |
| 2168 | |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 2169 | hdev = hci_dev_get(index); |
| 2170 | if (!hdev) |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 2171 | return cmd_status(sk, index, MGMT_OP_START_DISCOVERY, |
| 2172 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 2173 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2174 | hci_dev_lock(hdev); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 2175 | |
Johan Hedberg | bd2d133 | 2011-11-07 23:13:37 +0200 | [diff] [blame] | 2176 | if (!test_bit(HCI_UP, &hdev->flags)) { |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 2177 | err = cmd_status(sk, index, MGMT_OP_START_DISCOVERY, |
| 2178 | MGMT_STATUS_NOT_POWERED); |
Johan Hedberg | bd2d133 | 2011-11-07 23:13:37 +0200 | [diff] [blame] | 2179 | goto failed; |
| 2180 | } |
| 2181 | |
Johan Hedberg | ff9ef57 | 2012-01-04 14:23:45 +0200 | [diff] [blame] | 2182 | if (hdev->discovery.state != DISCOVERY_STOPPED) { |
| 2183 | err = cmd_status(sk, index, MGMT_OP_START_DISCOVERY, |
| 2184 | MGMT_STATUS_BUSY); |
| 2185 | goto failed; |
| 2186 | } |
| 2187 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 2188 | cmd = mgmt_pending_add(sk, MGMT_OP_START_DISCOVERY, hdev, NULL, 0); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 2189 | if (!cmd) { |
| 2190 | err = -ENOMEM; |
| 2191 | goto failed; |
| 2192 | } |
| 2193 | |
Andre Guedes | 3fd2415 | 2012-02-03 17:48:01 -0300 | [diff] [blame] | 2194 | if (test_bit(MGMT_ADDR_BREDR, &discov_type)) |
| 2195 | err = hci_do_inquiry(hdev, INQUIRY_LEN_BREDR); |
| 2196 | else if (test_bit(MGMT_ADDR_LE_PUBLIC, &discov_type) && |
| 2197 | test_bit(MGMT_ADDR_LE_RANDOM, &discov_type)) |
| 2198 | err = hci_le_scan(hdev, LE_SCAN_TYPE, LE_SCAN_INT, |
| 2199 | LE_SCAN_WIN, LE_SCAN_TIMEOUT_LE_ONLY); |
| 2200 | else |
| 2201 | err = -EINVAL; |
| 2202 | |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 2203 | if (err < 0) |
| 2204 | mgmt_pending_remove(cmd); |
Johan Hedberg | ff9ef57 | 2012-01-04 14:23:45 +0200 | [diff] [blame] | 2205 | else |
| 2206 | hci_discovery_set_state(hdev, DISCOVERY_STARTING); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 2207 | |
| 2208 | failed: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2209 | hci_dev_unlock(hdev); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 2210 | hci_dev_put(hdev); |
| 2211 | |
| 2212 | return err; |
| 2213 | } |
| 2214 | |
| 2215 | static int stop_discovery(struct sock *sk, u16 index) |
| 2216 | { |
| 2217 | struct hci_dev *hdev; |
| 2218 | struct pending_cmd *cmd; |
Johan Hedberg | 30dc78e | 2012-01-04 15:44:20 +0200 | [diff] [blame] | 2219 | struct hci_cp_remote_name_req_cancel cp; |
| 2220 | struct inquiry_entry *e; |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 2221 | int err; |
| 2222 | |
| 2223 | BT_DBG("hci%u", index); |
| 2224 | |
| 2225 | hdev = hci_dev_get(index); |
| 2226 | if (!hdev) |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 2227 | return cmd_status(sk, index, MGMT_OP_STOP_DISCOVERY, |
| 2228 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 2229 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2230 | hci_dev_lock(hdev); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 2231 | |
Johan Hedberg | 30dc78e | 2012-01-04 15:44:20 +0200 | [diff] [blame] | 2232 | if (!hci_discovery_active(hdev)) { |
Johan Hedberg | ff9ef57 | 2012-01-04 14:23:45 +0200 | [diff] [blame] | 2233 | err = cmd_status(sk, index, MGMT_OP_STOP_DISCOVERY, |
| 2234 | MGMT_STATUS_REJECTED); |
Johan Hedberg | 30dc78e | 2012-01-04 15:44:20 +0200 | [diff] [blame] | 2235 | goto unlock; |
Johan Hedberg | ff9ef57 | 2012-01-04 14:23:45 +0200 | [diff] [blame] | 2236 | } |
| 2237 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 2238 | cmd = mgmt_pending_add(sk, MGMT_OP_STOP_DISCOVERY, hdev, NULL, 0); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 2239 | if (!cmd) { |
| 2240 | err = -ENOMEM; |
Johan Hedberg | 30dc78e | 2012-01-04 15:44:20 +0200 | [diff] [blame] | 2241 | goto unlock; |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 2242 | } |
| 2243 | |
Johan Hedberg | 30dc78e | 2012-01-04 15:44:20 +0200 | [diff] [blame] | 2244 | if (hdev->discovery.state == DISCOVERY_INQUIRY) { |
| 2245 | err = hci_cancel_inquiry(hdev); |
| 2246 | if (err < 0) |
| 2247 | mgmt_pending_remove(cmd); |
| 2248 | else |
| 2249 | hci_discovery_set_state(hdev, DISCOVERY_STOPPING); |
| 2250 | goto unlock; |
| 2251 | } |
| 2252 | |
| 2253 | e = hci_inquiry_cache_lookup_resolve(hdev, BDADDR_ANY, NAME_PENDING); |
| 2254 | if (!e) { |
| 2255 | mgmt_pending_remove(cmd); |
| 2256 | err = cmd_complete(sk, index, MGMT_OP_STOP_DISCOVERY, NULL, 0); |
| 2257 | hci_discovery_set_state(hdev, DISCOVERY_STOPPED); |
| 2258 | goto unlock; |
| 2259 | } |
| 2260 | |
| 2261 | bacpy(&cp.bdaddr, &e->data.bdaddr); |
| 2262 | err = hci_send_cmd(hdev, HCI_OP_REMOTE_NAME_REQ_CANCEL, |
| 2263 | sizeof(cp), &cp); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 2264 | if (err < 0) |
| 2265 | mgmt_pending_remove(cmd); |
Johan Hedberg | ff9ef57 | 2012-01-04 14:23:45 +0200 | [diff] [blame] | 2266 | else |
| 2267 | hci_discovery_set_state(hdev, DISCOVERY_STOPPING); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 2268 | |
Johan Hedberg | 30dc78e | 2012-01-04 15:44:20 +0200 | [diff] [blame] | 2269 | unlock: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2270 | hci_dev_unlock(hdev); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 2271 | hci_dev_put(hdev); |
| 2272 | |
| 2273 | return err; |
| 2274 | } |
| 2275 | |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 2276 | static int confirm_name(struct sock *sk, u16 index, void *data, u16 len) |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 2277 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 2278 | struct mgmt_cp_confirm_name *cp = data; |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 2279 | struct inquiry_entry *e; |
| 2280 | struct hci_dev *hdev; |
| 2281 | int err; |
| 2282 | |
| 2283 | BT_DBG("hci%u", index); |
| 2284 | |
| 2285 | if (len != sizeof(*cp)) |
| 2286 | return cmd_status(sk, index, MGMT_OP_CONFIRM_NAME, |
| 2287 | MGMT_STATUS_INVALID_PARAMS); |
| 2288 | |
| 2289 | hdev = hci_dev_get(index); |
| 2290 | if (!hdev) |
| 2291 | return cmd_status(sk, index, MGMT_OP_CONFIRM_NAME, |
| 2292 | MGMT_STATUS_INVALID_PARAMS); |
| 2293 | |
| 2294 | hci_dev_lock(hdev); |
| 2295 | |
Johan Hedberg | 30dc78e | 2012-01-04 15:44:20 +0200 | [diff] [blame] | 2296 | if (!hci_discovery_active(hdev)) { |
| 2297 | err = cmd_status(sk, index, MGMT_OP_CONFIRM_NAME, |
| 2298 | MGMT_STATUS_FAILED); |
| 2299 | goto failed; |
| 2300 | } |
| 2301 | |
Johan Hedberg | a198e7b | 2012-02-17 14:27:06 +0200 | [diff] [blame^] | 2302 | e = hci_inquiry_cache_lookup_unknown(hdev, &cp->addr.bdaddr); |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 2303 | if (!e) { |
| 2304 | err = cmd_status (sk, index, MGMT_OP_CONFIRM_NAME, |
| 2305 | MGMT_STATUS_INVALID_PARAMS); |
| 2306 | goto failed; |
| 2307 | } |
| 2308 | |
| 2309 | if (cp->name_known) { |
| 2310 | e->name_state = NAME_KNOWN; |
| 2311 | list_del(&e->list); |
| 2312 | } else { |
| 2313 | e->name_state = NAME_NEEDED; |
Johan Hedberg | a3d4e20a | 2012-01-09 00:53:02 +0200 | [diff] [blame] | 2314 | hci_inquiry_cache_update_resolve(hdev, e); |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 2315 | } |
| 2316 | |
| 2317 | err = 0; |
| 2318 | |
| 2319 | failed: |
| 2320 | hci_dev_unlock(hdev); |
| 2321 | |
| 2322 | return err; |
| 2323 | } |
| 2324 | |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 2325 | static int block_device(struct sock *sk, u16 index, void *data, u16 len) |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 2326 | { |
| 2327 | struct hci_dev *hdev; |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 2328 | struct mgmt_cp_block_device *cp = data; |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 2329 | int err; |
| 2330 | |
| 2331 | BT_DBG("hci%u", index); |
| 2332 | |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 2333 | if (len != sizeof(*cp)) |
| 2334 | return cmd_status(sk, index, MGMT_OP_BLOCK_DEVICE, |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 2335 | MGMT_STATUS_INVALID_PARAMS); |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 2336 | |
| 2337 | hdev = hci_dev_get(index); |
| 2338 | if (!hdev) |
| 2339 | return cmd_status(sk, index, MGMT_OP_BLOCK_DEVICE, |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 2340 | MGMT_STATUS_INVALID_PARAMS); |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 2341 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2342 | hci_dev_lock(hdev); |
Antti Julku | 5e76244 | 2011-08-25 16:48:02 +0300 | [diff] [blame] | 2343 | |
Johan Hedberg | 88c1fe4 | 2012-02-09 15:56:11 +0200 | [diff] [blame] | 2344 | err = hci_blacklist_add(hdev, &cp->addr.bdaddr, cp->addr.type); |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 2345 | if (err < 0) |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 2346 | err = cmd_status(sk, index, MGMT_OP_BLOCK_DEVICE, |
| 2347 | MGMT_STATUS_FAILED); |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 2348 | else |
| 2349 | err = cmd_complete(sk, index, MGMT_OP_BLOCK_DEVICE, |
| 2350 | NULL, 0); |
Antti Julku | 5e76244 | 2011-08-25 16:48:02 +0300 | [diff] [blame] | 2351 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2352 | hci_dev_unlock(hdev); |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 2353 | hci_dev_put(hdev); |
| 2354 | |
| 2355 | return err; |
| 2356 | } |
| 2357 | |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 2358 | static int unblock_device(struct sock *sk, u16 index, void *data, u16 len) |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 2359 | { |
| 2360 | struct hci_dev *hdev; |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 2361 | struct mgmt_cp_unblock_device *cp = data; |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 2362 | int err; |
| 2363 | |
| 2364 | BT_DBG("hci%u", index); |
| 2365 | |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 2366 | if (len != sizeof(*cp)) |
| 2367 | return cmd_status(sk, index, MGMT_OP_UNBLOCK_DEVICE, |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 2368 | MGMT_STATUS_INVALID_PARAMS); |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 2369 | |
| 2370 | hdev = hci_dev_get(index); |
| 2371 | if (!hdev) |
| 2372 | return cmd_status(sk, index, MGMT_OP_UNBLOCK_DEVICE, |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 2373 | MGMT_STATUS_INVALID_PARAMS); |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 2374 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2375 | hci_dev_lock(hdev); |
Antti Julku | 5e76244 | 2011-08-25 16:48:02 +0300 | [diff] [blame] | 2376 | |
Johan Hedberg | 88c1fe4 | 2012-02-09 15:56:11 +0200 | [diff] [blame] | 2377 | err = hci_blacklist_del(hdev, &cp->addr.bdaddr, cp->addr.type); |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 2378 | |
| 2379 | if (err < 0) |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 2380 | err = cmd_status(sk, index, MGMT_OP_UNBLOCK_DEVICE, |
| 2381 | MGMT_STATUS_INVALID_PARAMS); |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 2382 | else |
| 2383 | err = cmd_complete(sk, index, MGMT_OP_UNBLOCK_DEVICE, |
| 2384 | NULL, 0); |
Antti Julku | 5e76244 | 2011-08-25 16:48:02 +0300 | [diff] [blame] | 2385 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2386 | hci_dev_unlock(hdev); |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 2387 | hci_dev_put(hdev); |
| 2388 | |
| 2389 | return err; |
| 2390 | } |
| 2391 | |
Antti Julku | f6422ec | 2011-06-22 13:11:56 +0300 | [diff] [blame] | 2392 | static int set_fast_connectable(struct sock *sk, u16 index, |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 2393 | void *data, u16 len) |
Antti Julku | f6422ec | 2011-06-22 13:11:56 +0300 | [diff] [blame] | 2394 | { |
| 2395 | struct hci_dev *hdev; |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 2396 | struct mgmt_mode *cp = data; |
Antti Julku | f6422ec | 2011-06-22 13:11:56 +0300 | [diff] [blame] | 2397 | struct hci_cp_write_page_scan_activity acp; |
| 2398 | u8 type; |
| 2399 | int err; |
| 2400 | |
| 2401 | BT_DBG("hci%u", index); |
| 2402 | |
| 2403 | if (len != sizeof(*cp)) |
| 2404 | return cmd_status(sk, index, MGMT_OP_SET_FAST_CONNECTABLE, |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 2405 | MGMT_STATUS_INVALID_PARAMS); |
Antti Julku | f6422ec | 2011-06-22 13:11:56 +0300 | [diff] [blame] | 2406 | |
| 2407 | hdev = hci_dev_get(index); |
| 2408 | if (!hdev) |
| 2409 | return cmd_status(sk, index, MGMT_OP_SET_FAST_CONNECTABLE, |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 2410 | MGMT_STATUS_INVALID_PARAMS); |
Antti Julku | f6422ec | 2011-06-22 13:11:56 +0300 | [diff] [blame] | 2411 | |
| 2412 | hci_dev_lock(hdev); |
| 2413 | |
Johan Hedberg | f7c6869ce | 2011-12-15 00:47:36 +0200 | [diff] [blame] | 2414 | if (cp->val) { |
Antti Julku | f6422ec | 2011-06-22 13:11:56 +0300 | [diff] [blame] | 2415 | type = PAGE_SCAN_TYPE_INTERLACED; |
| 2416 | acp.interval = 0x0024; /* 22.5 msec page scan interval */ |
| 2417 | } else { |
| 2418 | type = PAGE_SCAN_TYPE_STANDARD; /* default */ |
| 2419 | acp.interval = 0x0800; /* default 1.28 sec page scan */ |
| 2420 | } |
| 2421 | |
| 2422 | acp.window = 0x0012; /* default 11.25 msec page scan window */ |
| 2423 | |
| 2424 | err = hci_send_cmd(hdev, HCI_OP_WRITE_PAGE_SCAN_ACTIVITY, |
| 2425 | sizeof(acp), &acp); |
| 2426 | if (err < 0) { |
| 2427 | err = cmd_status(sk, index, MGMT_OP_SET_FAST_CONNECTABLE, |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 2428 | MGMT_STATUS_FAILED); |
Antti Julku | f6422ec | 2011-06-22 13:11:56 +0300 | [diff] [blame] | 2429 | goto done; |
| 2430 | } |
| 2431 | |
| 2432 | err = hci_send_cmd(hdev, HCI_OP_WRITE_PAGE_SCAN_TYPE, 1, &type); |
| 2433 | if (err < 0) { |
| 2434 | err = cmd_status(sk, index, MGMT_OP_SET_FAST_CONNECTABLE, |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 2435 | MGMT_STATUS_FAILED); |
Antti Julku | f6422ec | 2011-06-22 13:11:56 +0300 | [diff] [blame] | 2436 | goto done; |
| 2437 | } |
| 2438 | |
| 2439 | err = cmd_complete(sk, index, MGMT_OP_SET_FAST_CONNECTABLE, |
| 2440 | NULL, 0); |
| 2441 | done: |
| 2442 | hci_dev_unlock(hdev); |
| 2443 | hci_dev_put(hdev); |
| 2444 | |
| 2445 | return err; |
| 2446 | } |
| 2447 | |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 2448 | static int load_long_term_keys(struct sock *sk, u16 index, |
| 2449 | void *cp_data, u16 len) |
| 2450 | { |
| 2451 | struct hci_dev *hdev; |
| 2452 | struct mgmt_cp_load_long_term_keys *cp = cp_data; |
| 2453 | u16 key_count, expected_len; |
| 2454 | int i; |
| 2455 | |
| 2456 | if (len < sizeof(*cp)) |
| 2457 | return cmd_status(sk, index, MGMT_OP_LOAD_LONG_TERM_KEYS, |
| 2458 | EINVAL); |
| 2459 | |
| 2460 | key_count = get_unaligned_le16(&cp->key_count); |
| 2461 | |
| 2462 | expected_len = sizeof(*cp) + key_count * |
| 2463 | sizeof(struct mgmt_ltk_info); |
| 2464 | if (expected_len != len) { |
| 2465 | BT_ERR("load_keys: expected %u bytes, got %u bytes", |
| 2466 | len, expected_len); |
| 2467 | return cmd_status(sk, index, MGMT_OP_LOAD_LONG_TERM_KEYS, |
| 2468 | EINVAL); |
| 2469 | } |
| 2470 | |
| 2471 | hdev = hci_dev_get(index); |
| 2472 | if (!hdev) |
| 2473 | return cmd_status(sk, index, MGMT_OP_LOAD_LONG_TERM_KEYS, |
| 2474 | ENODEV); |
| 2475 | |
| 2476 | BT_DBG("hci%u key_count %u", index, key_count); |
| 2477 | |
| 2478 | hci_dev_lock(hdev); |
| 2479 | |
| 2480 | hci_smp_ltks_clear(hdev); |
| 2481 | |
| 2482 | for (i = 0; i < key_count; i++) { |
| 2483 | struct mgmt_ltk_info *key = &cp->keys[i]; |
| 2484 | u8 type; |
| 2485 | |
| 2486 | if (key->master) |
| 2487 | type = HCI_SMP_LTK; |
| 2488 | else |
| 2489 | type = HCI_SMP_LTK_SLAVE; |
| 2490 | |
| 2491 | hci_add_ltk(hdev, &key->addr.bdaddr, key->addr.type, |
| 2492 | type, 0, key->authenticated, key->val, |
| 2493 | key->enc_size, key->ediv, key->rand); |
| 2494 | } |
| 2495 | |
| 2496 | hci_dev_unlock(hdev); |
| 2497 | hci_dev_put(hdev); |
| 2498 | |
| 2499 | return 0; |
| 2500 | } |
| 2501 | |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 2502 | int mgmt_control(struct sock *sk, struct msghdr *msg, size_t msglen) |
| 2503 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 2504 | void *buf; |
| 2505 | u8 *cp; |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 2506 | struct mgmt_hdr *hdr; |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 2507 | u16 opcode, index, len; |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 2508 | int err; |
| 2509 | |
| 2510 | BT_DBG("got %zu bytes", msglen); |
| 2511 | |
| 2512 | if (msglen < sizeof(*hdr)) |
| 2513 | return -EINVAL; |
| 2514 | |
Gustavo F. Padovan | e63a15e | 2011-04-04 18:56:53 -0300 | [diff] [blame] | 2515 | buf = kmalloc(msglen, GFP_KERNEL); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 2516 | if (!buf) |
| 2517 | return -ENOMEM; |
| 2518 | |
| 2519 | if (memcpy_fromiovec(buf, msg->msg_iov, msglen)) { |
| 2520 | err = -EFAULT; |
| 2521 | goto done; |
| 2522 | } |
| 2523 | |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 2524 | hdr = buf; |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 2525 | opcode = get_unaligned_le16(&hdr->opcode); |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 2526 | index = get_unaligned_le16(&hdr->index); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 2527 | len = get_unaligned_le16(&hdr->len); |
| 2528 | |
| 2529 | if (len != msglen - sizeof(*hdr)) { |
| 2530 | err = -EINVAL; |
| 2531 | goto done; |
| 2532 | } |
| 2533 | |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 2534 | cp = buf + sizeof(*hdr); |
| 2535 | |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 2536 | switch (opcode) { |
Johan Hedberg | 02d9812 | 2010-12-13 21:07:04 +0200 | [diff] [blame] | 2537 | case MGMT_OP_READ_VERSION: |
| 2538 | err = read_version(sk); |
| 2539 | break; |
Johan Hedberg | e70bb2e | 2012-02-13 16:59:33 +0200 | [diff] [blame] | 2540 | case MGMT_OP_READ_COMMANDS: |
| 2541 | err = read_commands(sk); |
| 2542 | break; |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 2543 | case MGMT_OP_READ_INDEX_LIST: |
| 2544 | err = read_index_list(sk); |
| 2545 | break; |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 2546 | case MGMT_OP_READ_INFO: |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 2547 | err = read_controller_info(sk, index); |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 2548 | break; |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 2549 | case MGMT_OP_SET_POWERED: |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 2550 | err = set_powered(sk, index, cp, len); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 2551 | break; |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 2552 | case MGMT_OP_SET_DISCOVERABLE: |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 2553 | err = set_discoverable(sk, index, cp, len); |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 2554 | break; |
Johan Hedberg | 9fbcbb4 | 2010-12-30 00:18:33 +0200 | [diff] [blame] | 2555 | case MGMT_OP_SET_CONNECTABLE: |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 2556 | err = set_connectable(sk, index, cp, len); |
Johan Hedberg | 9fbcbb4 | 2010-12-30 00:18:33 +0200 | [diff] [blame] | 2557 | break; |
Johan Hedberg | f7c6869ce | 2011-12-15 00:47:36 +0200 | [diff] [blame] | 2558 | case MGMT_OP_SET_FAST_CONNECTABLE: |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 2559 | err = set_fast_connectable(sk, index, cp, len); |
Johan Hedberg | f7c6869ce | 2011-12-15 00:47:36 +0200 | [diff] [blame] | 2560 | break; |
Johan Hedberg | c542a06 | 2011-01-26 13:11:03 +0200 | [diff] [blame] | 2561 | case MGMT_OP_SET_PAIRABLE: |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 2562 | err = set_pairable(sk, index, cp, len); |
Johan Hedberg | c542a06 | 2011-01-26 13:11:03 +0200 | [diff] [blame] | 2563 | break; |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 2564 | case MGMT_OP_SET_LINK_SECURITY: |
| 2565 | err = set_link_security(sk, index, cp, len); |
| 2566 | break; |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 2567 | case MGMT_OP_SET_SSP: |
| 2568 | err = set_ssp(sk, index, cp, len); |
| 2569 | break; |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2570 | case MGMT_OP_ADD_UUID: |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 2571 | err = add_uuid(sk, index, cp, len); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2572 | break; |
| 2573 | case MGMT_OP_REMOVE_UUID: |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 2574 | err = remove_uuid(sk, index, cp, len); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2575 | break; |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2576 | case MGMT_OP_SET_DEV_CLASS: |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 2577 | err = set_dev_class(sk, index, cp, len); |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2578 | break; |
Johan Hedberg | 86742e1 | 2011-11-07 23:13:38 +0200 | [diff] [blame] | 2579 | case MGMT_OP_LOAD_LINK_KEYS: |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 2580 | err = load_link_keys(sk, index, cp, len); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2581 | break; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2582 | case MGMT_OP_DISCONNECT: |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 2583 | err = disconnect(sk, index, cp, len); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2584 | break; |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 2585 | case MGMT_OP_GET_CONNECTIONS: |
Szymon Janc | 8ce6284 | 2011-03-01 16:55:32 +0100 | [diff] [blame] | 2586 | err = get_connections(sk, index); |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 2587 | break; |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2588 | case MGMT_OP_PIN_CODE_REPLY: |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 2589 | err = pin_code_reply(sk, index, cp, len); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2590 | break; |
| 2591 | case MGMT_OP_PIN_CODE_NEG_REPLY: |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 2592 | err = pin_code_neg_reply(sk, index, cp, len); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2593 | break; |
Johan Hedberg | 17fa4b9 | 2011-01-25 13:28:33 +0200 | [diff] [blame] | 2594 | case MGMT_OP_SET_IO_CAPABILITY: |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 2595 | err = set_io_capability(sk, index, cp, len); |
Johan Hedberg | 17fa4b9 | 2011-01-25 13:28:33 +0200 | [diff] [blame] | 2596 | break; |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2597 | case MGMT_OP_PAIR_DEVICE: |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 2598 | err = pair_device(sk, index, cp, len); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2599 | break; |
Johan Hedberg | 2842470 | 2012-02-02 04:02:29 +0200 | [diff] [blame] | 2600 | case MGMT_OP_CANCEL_PAIR_DEVICE: |
| 2601 | err = cancel_pair_device(sk, index, buf + sizeof(*hdr), len); |
| 2602 | break; |
Johan Hedberg | 124f6e3 | 2012-02-09 13:50:12 +0200 | [diff] [blame] | 2603 | case MGMT_OP_UNPAIR_DEVICE: |
| 2604 | err = unpair_device(sk, index, cp, len); |
| 2605 | break; |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 2606 | case MGMT_OP_USER_CONFIRM_REPLY: |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 2607 | err = user_confirm_reply(sk, index, cp, len); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 2608 | break; |
| 2609 | case MGMT_OP_USER_CONFIRM_NEG_REPLY: |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 2610 | err = user_confirm_neg_reply(sk, index, cp, len); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 2611 | break; |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 2612 | case MGMT_OP_USER_PASSKEY_REPLY: |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 2613 | err = user_passkey_reply(sk, index, cp, len); |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 2614 | break; |
| 2615 | case MGMT_OP_USER_PASSKEY_NEG_REPLY: |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 2616 | err = user_passkey_neg_reply(sk, index, cp, len); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2617 | break; |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 2618 | case MGMT_OP_SET_LOCAL_NAME: |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 2619 | err = set_local_name(sk, index, cp, len); |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 2620 | break; |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 2621 | case MGMT_OP_READ_LOCAL_OOB_DATA: |
| 2622 | err = read_local_oob_data(sk, index); |
| 2623 | break; |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 2624 | case MGMT_OP_ADD_REMOTE_OOB_DATA: |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 2625 | err = add_remote_oob_data(sk, index, cp, len); |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 2626 | break; |
| 2627 | case MGMT_OP_REMOVE_REMOTE_OOB_DATA: |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 2628 | err = remove_remote_oob_data(sk, index, cp, len); |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 2629 | break; |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 2630 | case MGMT_OP_START_DISCOVERY: |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 2631 | err = start_discovery(sk, index, cp, len); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 2632 | break; |
| 2633 | case MGMT_OP_STOP_DISCOVERY: |
| 2634 | err = stop_discovery(sk, index); |
| 2635 | break; |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 2636 | case MGMT_OP_CONFIRM_NAME: |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 2637 | err = confirm_name(sk, index, cp, len); |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 2638 | break; |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 2639 | case MGMT_OP_BLOCK_DEVICE: |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 2640 | err = block_device(sk, index, cp, len); |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 2641 | break; |
| 2642 | case MGMT_OP_UNBLOCK_DEVICE: |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 2643 | err = unblock_device(sk, index, cp, len); |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 2644 | break; |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 2645 | case MGMT_OP_LOAD_LONG_TERM_KEYS: |
| 2646 | err = load_long_term_keys(sk, index, cp, len); |
| 2647 | break; |
Johan Hedberg | 9fbcbb4 | 2010-12-30 00:18:33 +0200 | [diff] [blame] | 2648 | default: |
Johan Hedberg | 72a734e | 2010-12-30 00:38:22 +0200 | [diff] [blame] | 2649 | BT_DBG("Unknown op %u", opcode); |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 2650 | err = cmd_status(sk, index, opcode, |
| 2651 | MGMT_STATUS_UNKNOWN_COMMAND); |
Johan Hedberg | 9fbcbb4 | 2010-12-30 00:18:33 +0200 | [diff] [blame] | 2652 | break; |
| 2653 | } |
Johan Hedberg | 72a734e | 2010-12-30 00:38:22 +0200 | [diff] [blame] | 2654 | |
Johan Hedberg | 9fbcbb4 | 2010-12-30 00:18:33 +0200 | [diff] [blame] | 2655 | if (err < 0) |
Johan Hedberg | 72a734e | 2010-12-30 00:38:22 +0200 | [diff] [blame] | 2656 | goto done; |
| 2657 | |
Johan Hedberg | 9fbcbb4 | 2010-12-30 00:18:33 +0200 | [diff] [blame] | 2658 | err = msglen; |
| 2659 | |
| 2660 | done: |
| 2661 | kfree(buf); |
| 2662 | return err; |
| 2663 | } |
| 2664 | |
Johan Hedberg | b24752f | 2011-11-03 14:40:33 +0200 | [diff] [blame] | 2665 | static void cmd_status_rsp(struct pending_cmd *cmd, void *data) |
| 2666 | { |
| 2667 | u8 *status = data; |
| 2668 | |
| 2669 | cmd_status(cmd->sk, cmd->index, cmd->opcode, *status); |
| 2670 | mgmt_pending_remove(cmd); |
| 2671 | } |
| 2672 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 2673 | int mgmt_index_added(struct hci_dev *hdev) |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 2674 | { |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 2675 | return mgmt_event(MGMT_EV_INDEX_ADDED, hdev, NULL, 0, NULL); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 2676 | } |
| 2677 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 2678 | int mgmt_index_removed(struct hci_dev *hdev) |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 2679 | { |
Johan Hedberg | b24752f | 2011-11-03 14:40:33 +0200 | [diff] [blame] | 2680 | u8 status = ENODEV; |
| 2681 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 2682 | mgmt_pending_foreach(0, hdev, cmd_status_rsp, &status); |
Johan Hedberg | b24752f | 2011-11-03 14:40:33 +0200 | [diff] [blame] | 2683 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 2684 | return mgmt_event(MGMT_EV_INDEX_REMOVED, hdev, NULL, 0, NULL); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 2685 | } |
| 2686 | |
| 2687 | struct cmd_lookup { |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 2688 | struct sock *sk; |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 2689 | struct hci_dev *hdev; |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 2690 | }; |
| 2691 | |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 2692 | static void settings_rsp(struct pending_cmd *cmd, void *data) |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 2693 | { |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 2694 | struct cmd_lookup *match = data; |
| 2695 | |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 2696 | send_settings_rsp(cmd->sk, cmd->opcode, match->hdev); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 2697 | |
| 2698 | list_del(&cmd->list); |
| 2699 | |
| 2700 | if (match->sk == NULL) { |
| 2701 | match->sk = cmd->sk; |
| 2702 | sock_hold(match->sk); |
| 2703 | } |
| 2704 | |
| 2705 | mgmt_pending_free(cmd); |
| 2706 | } |
| 2707 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 2708 | int mgmt_powered(struct hci_dev *hdev, u8 powered) |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 2709 | { |
Johan Hedberg | 76a7f3a | 2012-02-17 00:34:40 +0200 | [diff] [blame] | 2710 | struct cmd_lookup match = { NULL, hdev }; |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 2711 | __le32 ev; |
Johan Hedberg | 7bb895d | 2012-02-17 01:20:00 +0200 | [diff] [blame] | 2712 | int err; |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 2713 | |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 2714 | mgmt_pending_foreach(MGMT_OP_SET_POWERED, hdev, settings_rsp, &match); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 2715 | |
Johan Hedberg | b24752f | 2011-11-03 14:40:33 +0200 | [diff] [blame] | 2716 | if (!powered) { |
| 2717 | u8 status = ENETDOWN; |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 2718 | mgmt_pending_foreach(0, hdev, cmd_status_rsp, &status); |
Johan Hedberg | b24752f | 2011-11-03 14:40:33 +0200 | [diff] [blame] | 2719 | } |
| 2720 | |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 2721 | ev = cpu_to_le32(get_current_settings(hdev)); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 2722 | |
Johan Hedberg | 7bb895d | 2012-02-17 01:20:00 +0200 | [diff] [blame] | 2723 | err = mgmt_event(MGMT_EV_NEW_SETTINGS, hdev, &ev, sizeof(ev), |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 2724 | match.sk); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 2725 | |
| 2726 | if (match.sk) |
| 2727 | sock_put(match.sk); |
| 2728 | |
Johan Hedberg | 7bb895d | 2012-02-17 01:20:00 +0200 | [diff] [blame] | 2729 | return err; |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 2730 | } |
| 2731 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 2732 | int mgmt_discoverable(struct hci_dev *hdev, u8 discoverable) |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 2733 | { |
Johan Hedberg | 76a7f3a | 2012-02-17 00:34:40 +0200 | [diff] [blame] | 2734 | struct cmd_lookup match = { NULL, hdev }; |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 2735 | __le32 ev; |
Johan Hedberg | 7bb895d | 2012-02-17 01:20:00 +0200 | [diff] [blame] | 2736 | int err; |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 2737 | |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 2738 | mgmt_pending_foreach(MGMT_OP_SET_DISCOVERABLE, hdev, settings_rsp, &match); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 2739 | |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 2740 | ev = cpu_to_le32(get_current_settings(hdev)); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 2741 | |
Johan Hedberg | 7bb895d | 2012-02-17 01:20:00 +0200 | [diff] [blame] | 2742 | err = mgmt_event(MGMT_EV_NEW_SETTINGS, hdev, &ev, sizeof(ev), |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 2743 | match.sk); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 2744 | if (match.sk) |
| 2745 | sock_put(match.sk); |
| 2746 | |
Johan Hedberg | 7bb895d | 2012-02-17 01:20:00 +0200 | [diff] [blame] | 2747 | return err; |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 2748 | } |
| 2749 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 2750 | int mgmt_connectable(struct hci_dev *hdev, u8 connectable) |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 2751 | { |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 2752 | __le32 ev; |
Johan Hedberg | 76a7f3a | 2012-02-17 00:34:40 +0200 | [diff] [blame] | 2753 | struct cmd_lookup match = { NULL, hdev }; |
Johan Hedberg | 7bb895d | 2012-02-17 01:20:00 +0200 | [diff] [blame] | 2754 | int err; |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 2755 | |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 2756 | mgmt_pending_foreach(MGMT_OP_SET_CONNECTABLE, hdev, settings_rsp, |
| 2757 | &match); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 2758 | |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 2759 | ev = cpu_to_le32(get_current_settings(hdev)); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 2760 | |
Johan Hedberg | 7bb895d | 2012-02-17 01:20:00 +0200 | [diff] [blame] | 2761 | err = mgmt_event(MGMT_EV_NEW_SETTINGS, hdev, &ev, sizeof(ev), match.sk); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 2762 | |
| 2763 | if (match.sk) |
| 2764 | sock_put(match.sk); |
| 2765 | |
Johan Hedberg | 7bb895d | 2012-02-17 01:20:00 +0200 | [diff] [blame] | 2766 | return err; |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 2767 | } |
| 2768 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 2769 | int mgmt_write_scan_failed(struct hci_dev *hdev, u8 scan, u8 status) |
Johan Hedberg | 2d7cee5 | 2011-11-07 22:16:03 +0200 | [diff] [blame] | 2770 | { |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 2771 | u8 mgmt_err = mgmt_status(status); |
| 2772 | |
Johan Hedberg | 2d7cee5 | 2011-11-07 22:16:03 +0200 | [diff] [blame] | 2773 | if (scan & SCAN_PAGE) |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 2774 | mgmt_pending_foreach(MGMT_OP_SET_CONNECTABLE, hdev, |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 2775 | cmd_status_rsp, &mgmt_err); |
Johan Hedberg | 2d7cee5 | 2011-11-07 22:16:03 +0200 | [diff] [blame] | 2776 | |
| 2777 | if (scan & SCAN_INQUIRY) |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 2778 | mgmt_pending_foreach(MGMT_OP_SET_DISCOVERABLE, hdev, |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 2779 | cmd_status_rsp, &mgmt_err); |
Johan Hedberg | 2d7cee5 | 2011-11-07 22:16:03 +0200 | [diff] [blame] | 2780 | |
| 2781 | return 0; |
| 2782 | } |
| 2783 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 2784 | int mgmt_new_link_key(struct hci_dev *hdev, struct link_key *key, |
| 2785 | u8 persistent) |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 2786 | { |
Johan Hedberg | 86742e1 | 2011-11-07 23:13:38 +0200 | [diff] [blame] | 2787 | struct mgmt_ev_new_link_key ev; |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 2788 | |
Vinicius Costa Gomes | a492cd5 | 2011-08-25 20:02:29 -0300 | [diff] [blame] | 2789 | memset(&ev, 0, sizeof(ev)); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 2790 | |
Vinicius Costa Gomes | a492cd5 | 2011-08-25 20:02:29 -0300 | [diff] [blame] | 2791 | ev.store_hint = persistent; |
Johan Hedberg | d753fdc | 2012-02-17 14:06:34 +0200 | [diff] [blame] | 2792 | bacpy(&ev.key.addr.bdaddr, &key->bdaddr); |
| 2793 | ev.key.addr.type = MGMT_ADDR_BREDR; |
Vinicius Costa Gomes | a492cd5 | 2011-08-25 20:02:29 -0300 | [diff] [blame] | 2794 | ev.key.type = key->type; |
| 2795 | memcpy(ev.key.val, key->val, 16); |
| 2796 | ev.key.pin_len = key->pin_len; |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 2797 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 2798 | return mgmt_event(MGMT_EV_NEW_LINK_KEY, hdev, &ev, sizeof(ev), NULL); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 2799 | } |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 2800 | |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 2801 | int mgmt_new_ltk(struct hci_dev *hdev, struct smp_ltk *key, u8 persistent) |
| 2802 | { |
| 2803 | struct mgmt_ev_new_long_term_key ev; |
| 2804 | |
| 2805 | memset(&ev, 0, sizeof(ev)); |
| 2806 | |
| 2807 | ev.store_hint = persistent; |
| 2808 | bacpy(&ev.key.addr.bdaddr, &key->bdaddr); |
| 2809 | ev.key.addr.type = key->bdaddr_type; |
| 2810 | ev.key.authenticated = key->authenticated; |
| 2811 | ev.key.enc_size = key->enc_size; |
| 2812 | ev.key.ediv = key->ediv; |
| 2813 | |
| 2814 | if (key->type == HCI_SMP_LTK) |
| 2815 | ev.key.master = 1; |
| 2816 | |
| 2817 | memcpy(ev.key.rand, key->rand, sizeof(key->rand)); |
| 2818 | memcpy(ev.key.val, key->val, sizeof(key->val)); |
| 2819 | |
| 2820 | return mgmt_event(MGMT_EV_NEW_LONG_TERM_KEY, hdev, |
| 2821 | &ev, sizeof(ev), NULL); |
| 2822 | } |
| 2823 | |
Johan Hedberg | afc747a | 2012-01-15 18:11:07 +0200 | [diff] [blame] | 2824 | int mgmt_device_connected(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 link_type, |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 2825 | u8 addr_type, u8 *name, u8 name_len, |
| 2826 | u8 *dev_class) |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 2827 | { |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 2828 | char buf[512]; |
| 2829 | struct mgmt_ev_device_connected *ev = (void *) buf; |
| 2830 | u16 eir_len = 0; |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 2831 | |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 2832 | bacpy(&ev->addr.bdaddr, bdaddr); |
| 2833 | ev->addr.type = link_to_mgmt(link_type, addr_type); |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 2834 | |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 2835 | if (name_len > 0) |
| 2836 | eir_len = eir_append_data(ev->eir, 0, EIR_NAME_COMPLETE, |
| 2837 | name, name_len); |
| 2838 | |
| 2839 | if (dev_class && memcmp(dev_class, "\0\0\0", 3) != 0) |
| 2840 | eir_len = eir_append_data(&ev->eir[eir_len], eir_len, |
| 2841 | EIR_CLASS_OF_DEV, dev_class, 3); |
| 2842 | |
| 2843 | put_unaligned_le16(eir_len, &ev->eir_len); |
| 2844 | |
| 2845 | return mgmt_event(MGMT_EV_DEVICE_CONNECTED, hdev, buf, |
| 2846 | sizeof(*ev) + eir_len, NULL); |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 2847 | } |
| 2848 | |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2849 | static void disconnect_rsp(struct pending_cmd *cmd, void *data) |
| 2850 | { |
Szymon Janc | c68fb7f | 2011-03-22 13:12:19 +0100 | [diff] [blame] | 2851 | struct mgmt_cp_disconnect *cp = cmd->param; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2852 | struct sock **sk = data; |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 2853 | struct mgmt_rp_disconnect rp; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2854 | |
Johan Hedberg | 88c3df1 | 2012-02-09 14:27:38 +0200 | [diff] [blame] | 2855 | bacpy(&rp.addr.bdaddr, &cp->addr.bdaddr); |
| 2856 | rp.addr.type = cp->addr.type; |
Johan Hedberg | 37d9ef7 | 2011-11-10 15:54:39 +0200 | [diff] [blame] | 2857 | rp.status = 0; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2858 | |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 2859 | cmd_complete(cmd->sk, cmd->index, MGMT_OP_DISCONNECT, &rp, sizeof(rp)); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2860 | |
| 2861 | *sk = cmd->sk; |
| 2862 | sock_hold(*sk); |
| 2863 | |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 2864 | mgmt_pending_remove(cmd); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2865 | } |
| 2866 | |
Johan Hedberg | 124f6e3 | 2012-02-09 13:50:12 +0200 | [diff] [blame] | 2867 | static void unpair_device_rsp(struct pending_cmd *cmd, void *data) |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 2868 | { |
Johan Hedberg | b1078ad | 2012-02-09 17:21:16 +0200 | [diff] [blame] | 2869 | struct hci_dev *hdev = data; |
Johan Hedberg | 124f6e3 | 2012-02-09 13:50:12 +0200 | [diff] [blame] | 2870 | struct mgmt_cp_unpair_device *cp = cmd->param; |
| 2871 | struct mgmt_rp_unpair_device rp; |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 2872 | |
| 2873 | memset(&rp, 0, sizeof(rp)); |
Johan Hedberg | 124f6e3 | 2012-02-09 13:50:12 +0200 | [diff] [blame] | 2874 | bacpy(&rp.addr.bdaddr, &cp->addr.bdaddr); |
| 2875 | rp.addr.type = cp->addr.type; |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 2876 | |
Johan Hedberg | b1078ad | 2012-02-09 17:21:16 +0200 | [diff] [blame] | 2877 | device_unpaired(hdev, &cp->addr.bdaddr, cp->addr.type, cmd->sk); |
| 2878 | |
| 2879 | cmd_complete(cmd->sk, cmd->index, cmd->opcode, &rp, sizeof(rp)); |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 2880 | |
| 2881 | mgmt_pending_remove(cmd); |
| 2882 | } |
| 2883 | |
Johan Hedberg | afc747a | 2012-01-15 18:11:07 +0200 | [diff] [blame] | 2884 | int mgmt_device_disconnected(struct hci_dev *hdev, bdaddr_t *bdaddr, |
| 2885 | u8 link_type, u8 addr_type) |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 2886 | { |
Johan Hedberg | 4c659c3 | 2011-11-07 23:13:39 +0200 | [diff] [blame] | 2887 | struct mgmt_addr_info ev; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2888 | struct sock *sk = NULL; |
| 2889 | int err; |
| 2890 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 2891 | mgmt_pending_foreach(MGMT_OP_DISCONNECT, hdev, disconnect_rsp, &sk); |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 2892 | |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 2893 | bacpy(&ev.bdaddr, bdaddr); |
Johan Hedberg | 48264f0 | 2011-11-09 13:58:58 +0200 | [diff] [blame] | 2894 | ev.type = link_to_mgmt(link_type, addr_type); |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 2895 | |
Johan Hedberg | afc747a | 2012-01-15 18:11:07 +0200 | [diff] [blame] | 2896 | err = mgmt_event(MGMT_EV_DEVICE_DISCONNECTED, hdev, &ev, sizeof(ev), |
| 2897 | sk); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2898 | |
| 2899 | if (sk) |
| 2900 | sock_put(sk); |
| 2901 | |
Johan Hedberg | 124f6e3 | 2012-02-09 13:50:12 +0200 | [diff] [blame] | 2902 | mgmt_pending_foreach(MGMT_OP_UNPAIR_DEVICE, hdev, unpair_device_rsp, |
Johan Hedberg | b1078ad | 2012-02-09 17:21:16 +0200 | [diff] [blame] | 2903 | hdev); |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 2904 | |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2905 | return err; |
| 2906 | } |
| 2907 | |
Johan Hedberg | 88c3df1 | 2012-02-09 14:27:38 +0200 | [diff] [blame] | 2908 | int mgmt_disconnect_failed(struct hci_dev *hdev, bdaddr_t *bdaddr, |
| 2909 | u8 link_type, u8 addr_type, u8 status) |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2910 | { |
Johan Hedberg | 88c3df1 | 2012-02-09 14:27:38 +0200 | [diff] [blame] | 2911 | struct mgmt_rp_disconnect rp; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2912 | struct pending_cmd *cmd; |
| 2913 | int err; |
| 2914 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 2915 | cmd = mgmt_pending_find(MGMT_OP_DISCONNECT, hdev); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2916 | if (!cmd) |
| 2917 | return -ENOENT; |
| 2918 | |
Johan Hedberg | 88c3df1 | 2012-02-09 14:27:38 +0200 | [diff] [blame] | 2919 | bacpy(&rp.addr.bdaddr, bdaddr); |
| 2920 | rp.addr.type = link_to_mgmt(link_type, addr_type); |
| 2921 | rp.status = mgmt_status(status); |
Johan Hedberg | 37d9ef7 | 2011-11-10 15:54:39 +0200 | [diff] [blame] | 2922 | |
Johan Hedberg | 88c3df1 | 2012-02-09 14:27:38 +0200 | [diff] [blame] | 2923 | err = cmd_complete(cmd->sk, cmd->index, MGMT_OP_DISCONNECT, |
Johan Hedberg | 37d9ef7 | 2011-11-10 15:54:39 +0200 | [diff] [blame] | 2924 | &rp, sizeof(rp)); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2925 | |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 2926 | mgmt_pending_remove(cmd); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2927 | |
Johan Hedberg | b1078ad | 2012-02-09 17:21:16 +0200 | [diff] [blame] | 2928 | mgmt_pending_foreach(MGMT_OP_UNPAIR_DEVICE, hdev, unpair_device_rsp, |
| 2929 | hdev); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2930 | return err; |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 2931 | } |
Johan Hedberg | 17d5c04 | 2011-01-22 06:09:08 +0200 | [diff] [blame] | 2932 | |
Johan Hedberg | 48264f0 | 2011-11-09 13:58:58 +0200 | [diff] [blame] | 2933 | int mgmt_connect_failed(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 link_type, |
| 2934 | u8 addr_type, u8 status) |
Johan Hedberg | 17d5c04 | 2011-01-22 06:09:08 +0200 | [diff] [blame] | 2935 | { |
| 2936 | struct mgmt_ev_connect_failed ev; |
| 2937 | |
Johan Hedberg | 4c659c3 | 2011-11-07 23:13:39 +0200 | [diff] [blame] | 2938 | bacpy(&ev.addr.bdaddr, bdaddr); |
Johan Hedberg | 48264f0 | 2011-11-09 13:58:58 +0200 | [diff] [blame] | 2939 | ev.addr.type = link_to_mgmt(link_type, addr_type); |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 2940 | ev.status = mgmt_status(status); |
Johan Hedberg | 17d5c04 | 2011-01-22 06:09:08 +0200 | [diff] [blame] | 2941 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 2942 | return mgmt_event(MGMT_EV_CONNECT_FAILED, hdev, &ev, sizeof(ev), NULL); |
Johan Hedberg | 17d5c04 | 2011-01-22 06:09:08 +0200 | [diff] [blame] | 2943 | } |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2944 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 2945 | int mgmt_pin_code_request(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 secure) |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2946 | { |
| 2947 | struct mgmt_ev_pin_code_request ev; |
| 2948 | |
Johan Hedberg | d845769 | 2012-02-17 14:24:57 +0200 | [diff] [blame] | 2949 | bacpy(&ev.addr.bdaddr, bdaddr); |
| 2950 | ev.addr.type = MGMT_ADDR_BREDR; |
Waldemar Rymarkiewicz | a770bb5 | 2011-04-28 12:07:59 +0200 | [diff] [blame] | 2951 | ev.secure = secure; |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2952 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 2953 | return mgmt_event(MGMT_EV_PIN_CODE_REQUEST, hdev, &ev, sizeof(ev), |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 2954 | NULL); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2955 | } |
| 2956 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 2957 | int mgmt_pin_code_reply_complete(struct hci_dev *hdev, bdaddr_t *bdaddr, |
| 2958 | u8 status) |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2959 | { |
| 2960 | struct pending_cmd *cmd; |
Johan Hedberg | ac56fb1 | 2011-02-19 12:05:59 -0300 | [diff] [blame] | 2961 | struct mgmt_rp_pin_code_reply rp; |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2962 | int err; |
| 2963 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 2964 | cmd = mgmt_pending_find(MGMT_OP_PIN_CODE_REPLY, hdev); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2965 | if (!cmd) |
| 2966 | return -ENOENT; |
| 2967 | |
Johan Hedberg | d845769 | 2012-02-17 14:24:57 +0200 | [diff] [blame] | 2968 | bacpy(&rp.addr.bdaddr, bdaddr); |
| 2969 | rp.addr.type = MGMT_ADDR_BREDR; |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 2970 | rp.status = mgmt_status(status); |
Johan Hedberg | ac56fb1 | 2011-02-19 12:05:59 -0300 | [diff] [blame] | 2971 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 2972 | err = cmd_complete(cmd->sk, hdev->id, MGMT_OP_PIN_CODE_REPLY, &rp, |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 2973 | sizeof(rp)); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2974 | |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 2975 | mgmt_pending_remove(cmd); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2976 | |
| 2977 | return err; |
| 2978 | } |
| 2979 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 2980 | int mgmt_pin_code_neg_reply_complete(struct hci_dev *hdev, bdaddr_t *bdaddr, |
| 2981 | u8 status) |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2982 | { |
| 2983 | struct pending_cmd *cmd; |
Johan Hedberg | ac56fb1 | 2011-02-19 12:05:59 -0300 | [diff] [blame] | 2984 | struct mgmt_rp_pin_code_reply rp; |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2985 | int err; |
| 2986 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 2987 | cmd = mgmt_pending_find(MGMT_OP_PIN_CODE_NEG_REPLY, hdev); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2988 | if (!cmd) |
| 2989 | return -ENOENT; |
| 2990 | |
Johan Hedberg | d845769 | 2012-02-17 14:24:57 +0200 | [diff] [blame] | 2991 | bacpy(&rp.addr.bdaddr, bdaddr); |
| 2992 | rp.addr.type = MGMT_ADDR_BREDR; |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 2993 | rp.status = mgmt_status(status); |
Johan Hedberg | ac56fb1 | 2011-02-19 12:05:59 -0300 | [diff] [blame] | 2994 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 2995 | err = cmd_complete(cmd->sk, hdev->id, MGMT_OP_PIN_CODE_NEG_REPLY, &rp, |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 2996 | sizeof(rp)); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2997 | |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 2998 | mgmt_pending_remove(cmd); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2999 | |
| 3000 | return err; |
| 3001 | } |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 3002 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 3003 | int mgmt_user_confirm_request(struct hci_dev *hdev, bdaddr_t *bdaddr, |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 3004 | u8 link_type, u8 addr_type, __le32 value, |
| 3005 | u8 confirm_hint) |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 3006 | { |
| 3007 | struct mgmt_ev_user_confirm_request ev; |
| 3008 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 3009 | BT_DBG("%s", hdev->name); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 3010 | |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 3011 | bacpy(&ev.addr.bdaddr, bdaddr); |
| 3012 | ev.addr.type = link_to_mgmt(link_type, addr_type); |
Johan Hedberg | 55bc1a3 | 2011-04-28 11:28:56 -0700 | [diff] [blame] | 3013 | ev.confirm_hint = confirm_hint; |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 3014 | put_unaligned_le32(value, &ev.value); |
| 3015 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 3016 | return mgmt_event(MGMT_EV_USER_CONFIRM_REQUEST, hdev, &ev, sizeof(ev), |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 3017 | NULL); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 3018 | } |
| 3019 | |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 3020 | int mgmt_user_passkey_request(struct hci_dev *hdev, bdaddr_t *bdaddr, |
| 3021 | u8 link_type, u8 addr_type) |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 3022 | { |
| 3023 | struct mgmt_ev_user_passkey_request ev; |
| 3024 | |
| 3025 | BT_DBG("%s", hdev->name); |
| 3026 | |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 3027 | bacpy(&ev.addr.bdaddr, bdaddr); |
| 3028 | ev.addr.type = link_to_mgmt(link_type, addr_type); |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 3029 | |
| 3030 | return mgmt_event(MGMT_EV_USER_PASSKEY_REQUEST, hdev, &ev, sizeof(ev), |
| 3031 | NULL); |
| 3032 | } |
| 3033 | |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3034 | static int user_pairing_resp_complete(struct hci_dev *hdev, bdaddr_t *bdaddr, |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 3035 | u8 link_type, u8 addr_type, u8 status, |
| 3036 | u8 opcode) |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 3037 | { |
| 3038 | struct pending_cmd *cmd; |
| 3039 | struct mgmt_rp_user_confirm_reply rp; |
| 3040 | int err; |
| 3041 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 3042 | cmd = mgmt_pending_find(opcode, hdev); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 3043 | if (!cmd) |
| 3044 | return -ENOENT; |
| 3045 | |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 3046 | bacpy(&rp.addr.bdaddr, bdaddr); |
| 3047 | rp.addr.type = link_to_mgmt(link_type, addr_type); |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 3048 | rp.status = mgmt_status(status); |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 3049 | err = cmd_complete(cmd->sk, hdev->id, opcode, &rp, sizeof(rp)); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 3050 | |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 3051 | mgmt_pending_remove(cmd); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 3052 | |
| 3053 | return err; |
| 3054 | } |
| 3055 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 3056 | int mgmt_user_confirm_reply_complete(struct hci_dev *hdev, bdaddr_t *bdaddr, |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 3057 | u8 link_type, u8 addr_type, u8 status) |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 3058 | { |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 3059 | return user_pairing_resp_complete(hdev, bdaddr, link_type, addr_type, |
| 3060 | status, MGMT_OP_USER_CONFIRM_REPLY); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 3061 | } |
| 3062 | |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 3063 | int mgmt_user_confirm_neg_reply_complete(struct hci_dev *hdev, bdaddr_t *bdaddr, |
| 3064 | u8 link_type, u8 addr_type, u8 status) |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 3065 | { |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 3066 | return user_pairing_resp_complete(hdev, bdaddr, link_type, addr_type, |
| 3067 | status, MGMT_OP_USER_CONFIRM_NEG_REPLY); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 3068 | } |
Johan Hedberg | 2a61169 | 2011-02-19 12:06:00 -0300 | [diff] [blame] | 3069 | |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 3070 | int mgmt_user_passkey_reply_complete(struct hci_dev *hdev, bdaddr_t *bdaddr, |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 3071 | u8 link_type, u8 addr_type, u8 status) |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 3072 | { |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 3073 | return user_pairing_resp_complete(hdev, bdaddr, link_type, addr_type, |
| 3074 | status, MGMT_OP_USER_PASSKEY_REPLY); |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 3075 | } |
| 3076 | |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 3077 | int mgmt_user_passkey_neg_reply_complete(struct hci_dev *hdev, bdaddr_t *bdaddr, |
| 3078 | u8 link_type, u8 addr_type, u8 status) |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 3079 | { |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 3080 | return user_pairing_resp_complete(hdev, bdaddr, link_type, addr_type, |
| 3081 | status, MGMT_OP_USER_PASSKEY_NEG_REPLY); |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 3082 | } |
| 3083 | |
Johan Hedberg | bab73cb | 2012-02-09 16:07:29 +0200 | [diff] [blame] | 3084 | int mgmt_auth_failed(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 link_type, |
| 3085 | u8 addr_type, u8 status) |
Johan Hedberg | 2a61169 | 2011-02-19 12:06:00 -0300 | [diff] [blame] | 3086 | { |
| 3087 | struct mgmt_ev_auth_failed ev; |
| 3088 | |
Johan Hedberg | bab73cb | 2012-02-09 16:07:29 +0200 | [diff] [blame] | 3089 | bacpy(&ev.addr.bdaddr, bdaddr); |
| 3090 | ev.addr.type = link_to_mgmt(link_type, addr_type); |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 3091 | ev.status = mgmt_status(status); |
Johan Hedberg | 2a61169 | 2011-02-19 12:06:00 -0300 | [diff] [blame] | 3092 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 3093 | return mgmt_event(MGMT_EV_AUTH_FAILED, hdev, &ev, sizeof(ev), NULL); |
Johan Hedberg | 2a61169 | 2011-02-19 12:06:00 -0300 | [diff] [blame] | 3094 | } |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 3095 | |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 3096 | int mgmt_auth_enable_complete(struct hci_dev *hdev, u8 status) |
| 3097 | { |
| 3098 | struct cmd_lookup match = { NULL, hdev }; |
| 3099 | __le32 ev; |
| 3100 | int err; |
| 3101 | |
| 3102 | if (status) { |
| 3103 | u8 mgmt_err = mgmt_status(status); |
| 3104 | mgmt_pending_foreach(MGMT_OP_SET_LINK_SECURITY, hdev, |
| 3105 | cmd_status_rsp, &mgmt_err); |
| 3106 | return 0; |
| 3107 | } |
| 3108 | |
| 3109 | mgmt_pending_foreach(MGMT_OP_SET_LINK_SECURITY, hdev, settings_rsp, |
| 3110 | &match); |
| 3111 | |
| 3112 | ev = cpu_to_le32(get_current_settings(hdev)); |
| 3113 | err = mgmt_event(MGMT_EV_NEW_SETTINGS, hdev, &ev, sizeof(ev), match.sk); |
| 3114 | |
| 3115 | if (match.sk) |
| 3116 | sock_put(match.sk); |
| 3117 | |
| 3118 | return err; |
| 3119 | } |
| 3120 | |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 3121 | int mgmt_ssp_enable_complete(struct hci_dev *hdev, u8 status) |
| 3122 | { |
| 3123 | struct cmd_lookup match = { NULL, hdev }; |
| 3124 | __le32 ev; |
| 3125 | int err; |
| 3126 | |
| 3127 | if (status) { |
| 3128 | u8 mgmt_err = mgmt_status(status); |
| 3129 | mgmt_pending_foreach(MGMT_OP_SET_SSP, hdev, |
| 3130 | cmd_status_rsp, &mgmt_err); |
| 3131 | return 0; |
| 3132 | } |
| 3133 | |
| 3134 | mgmt_pending_foreach(MGMT_OP_SET_SSP, hdev, settings_rsp, &match); |
| 3135 | |
| 3136 | ev = cpu_to_le32(get_current_settings(hdev)); |
| 3137 | err = mgmt_event(MGMT_EV_NEW_SETTINGS, hdev, &ev, sizeof(ev), match.sk); |
| 3138 | |
| 3139 | if (match.sk) |
| 3140 | sock_put(match.sk); |
| 3141 | |
| 3142 | return err; |
| 3143 | } |
| 3144 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 3145 | int mgmt_set_local_name_complete(struct hci_dev *hdev, u8 *name, u8 status) |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 3146 | { |
| 3147 | struct pending_cmd *cmd; |
| 3148 | struct mgmt_cp_set_local_name ev; |
| 3149 | int err; |
| 3150 | |
| 3151 | memset(&ev, 0, sizeof(ev)); |
| 3152 | memcpy(ev.name, name, HCI_MAX_NAME_LENGTH); |
| 3153 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 3154 | cmd = mgmt_pending_find(MGMT_OP_SET_LOCAL_NAME, hdev); |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 3155 | if (!cmd) |
| 3156 | goto send_event; |
| 3157 | |
| 3158 | if (status) { |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 3159 | err = cmd_status(cmd->sk, hdev->id, MGMT_OP_SET_LOCAL_NAME, |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 3160 | mgmt_status(status)); |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 3161 | goto failed; |
| 3162 | } |
| 3163 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 3164 | update_eir(hdev); |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 3165 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 3166 | err = cmd_complete(cmd->sk, hdev->id, MGMT_OP_SET_LOCAL_NAME, &ev, |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 3167 | sizeof(ev)); |
| 3168 | if (err < 0) |
| 3169 | goto failed; |
| 3170 | |
| 3171 | send_event: |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 3172 | err = mgmt_event(MGMT_EV_LOCAL_NAME_CHANGED, hdev, &ev, sizeof(ev), |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 3173 | cmd ? cmd->sk : NULL); |
| 3174 | |
| 3175 | failed: |
| 3176 | if (cmd) |
| 3177 | mgmt_pending_remove(cmd); |
| 3178 | return err; |
| 3179 | } |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 3180 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 3181 | int mgmt_read_local_oob_data_reply_complete(struct hci_dev *hdev, u8 *hash, |
| 3182 | u8 *randomizer, u8 status) |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 3183 | { |
| 3184 | struct pending_cmd *cmd; |
| 3185 | int err; |
| 3186 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 3187 | BT_DBG("%s status %u", hdev->name, status); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 3188 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 3189 | cmd = mgmt_pending_find(MGMT_OP_READ_LOCAL_OOB_DATA, hdev); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 3190 | if (!cmd) |
| 3191 | return -ENOENT; |
| 3192 | |
| 3193 | if (status) { |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 3194 | err = cmd_status(cmd->sk, hdev->id, |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 3195 | MGMT_OP_READ_LOCAL_OOB_DATA, |
| 3196 | mgmt_status(status)); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 3197 | } else { |
| 3198 | struct mgmt_rp_read_local_oob_data rp; |
| 3199 | |
| 3200 | memcpy(rp.hash, hash, sizeof(rp.hash)); |
| 3201 | memcpy(rp.randomizer, randomizer, sizeof(rp.randomizer)); |
| 3202 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 3203 | err = cmd_complete(cmd->sk, hdev->id, |
| 3204 | MGMT_OP_READ_LOCAL_OOB_DATA, |
| 3205 | &rp, sizeof(rp)); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 3206 | } |
| 3207 | |
| 3208 | mgmt_pending_remove(cmd); |
| 3209 | |
| 3210 | return err; |
| 3211 | } |
Johan Hedberg | e17acd4 | 2011-03-30 23:57:16 +0300 | [diff] [blame] | 3212 | |
Johan Hedberg | 48264f0 | 2011-11-09 13:58:58 +0200 | [diff] [blame] | 3213 | int mgmt_device_found(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 link_type, |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 3214 | u8 addr_type, u8 *dev_class, s8 rssi, |
Johan Hedberg | e319d2e | 2012-01-15 19:51:59 +0200 | [diff] [blame] | 3215 | u8 cfm_name, u8 *eir, u16 eir_len) |
Johan Hedberg | e17acd4 | 2011-03-30 23:57:16 +0300 | [diff] [blame] | 3216 | { |
Johan Hedberg | e319d2e | 2012-01-15 19:51:59 +0200 | [diff] [blame] | 3217 | char buf[512]; |
| 3218 | struct mgmt_ev_device_found *ev = (void *) buf; |
Johan Hedberg | 1dc0609 | 2012-01-15 21:01:23 +0200 | [diff] [blame] | 3219 | size_t ev_size; |
Johan Hedberg | e17acd4 | 2011-03-30 23:57:16 +0300 | [diff] [blame] | 3220 | |
Johan Hedberg | 1dc0609 | 2012-01-15 21:01:23 +0200 | [diff] [blame] | 3221 | /* Leave 5 bytes for a potential CoD field */ |
| 3222 | if (sizeof(*ev) + eir_len + 5 > sizeof(buf)) |
Andre Guedes | 7d262f8 | 2012-01-10 18:20:49 -0300 | [diff] [blame] | 3223 | return -EINVAL; |
| 3224 | |
Johan Hedberg | 1dc0609 | 2012-01-15 21:01:23 +0200 | [diff] [blame] | 3225 | memset(buf, 0, sizeof(buf)); |
| 3226 | |
Johan Hedberg | e319d2e | 2012-01-15 19:51:59 +0200 | [diff] [blame] | 3227 | bacpy(&ev->addr.bdaddr, bdaddr); |
| 3228 | ev->addr.type = link_to_mgmt(link_type, addr_type); |
| 3229 | ev->rssi = rssi; |
| 3230 | ev->confirm_name = cfm_name; |
Johan Hedberg | e17acd4 | 2011-03-30 23:57:16 +0300 | [diff] [blame] | 3231 | |
Johan Hedberg | 1dc0609 | 2012-01-15 21:01:23 +0200 | [diff] [blame] | 3232 | if (eir_len > 0) |
Johan Hedberg | e319d2e | 2012-01-15 19:51:59 +0200 | [diff] [blame] | 3233 | memcpy(ev->eir, eir, eir_len); |
Johan Hedberg | e17acd4 | 2011-03-30 23:57:16 +0300 | [diff] [blame] | 3234 | |
Johan Hedberg | 1dc0609 | 2012-01-15 21:01:23 +0200 | [diff] [blame] | 3235 | if (dev_class && !eir_has_data_type(ev->eir, eir_len, EIR_CLASS_OF_DEV)) |
| 3236 | eir_len = eir_append_data(ev->eir, eir_len, EIR_CLASS_OF_DEV, |
| 3237 | dev_class, 3); |
| 3238 | |
| 3239 | put_unaligned_le16(eir_len, &ev->eir_len); |
| 3240 | |
| 3241 | ev_size = sizeof(*ev) + eir_len; |
Andre Guedes | f852359 | 2011-09-09 18:56:26 -0300 | [diff] [blame] | 3242 | |
Johan Hedberg | e319d2e | 2012-01-15 19:51:59 +0200 | [diff] [blame] | 3243 | return mgmt_event(MGMT_EV_DEVICE_FOUND, hdev, ev, ev_size, NULL); |
Johan Hedberg | e17acd4 | 2011-03-30 23:57:16 +0300 | [diff] [blame] | 3244 | } |
Johan Hedberg | a88a965 | 2011-03-30 13:18:12 +0300 | [diff] [blame] | 3245 | |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 3246 | int mgmt_remote_name(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 link_type, |
| 3247 | u8 addr_type, s8 rssi, u8 *name, u8 name_len) |
Johan Hedberg | a88a965 | 2011-03-30 13:18:12 +0300 | [diff] [blame] | 3248 | { |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 3249 | struct mgmt_ev_device_found *ev; |
| 3250 | char buf[sizeof(*ev) + HCI_MAX_NAME_LENGTH + 2]; |
| 3251 | u16 eir_len; |
Johan Hedberg | a88a965 | 2011-03-30 13:18:12 +0300 | [diff] [blame] | 3252 | |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 3253 | ev = (struct mgmt_ev_device_found *) buf; |
Johan Hedberg | a88a965 | 2011-03-30 13:18:12 +0300 | [diff] [blame] | 3254 | |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 3255 | memset(buf, 0, sizeof(buf)); |
Johan Hedberg | a88a965 | 2011-03-30 13:18:12 +0300 | [diff] [blame] | 3256 | |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 3257 | bacpy(&ev->addr.bdaddr, bdaddr); |
| 3258 | ev->addr.type = link_to_mgmt(link_type, addr_type); |
| 3259 | ev->rssi = rssi; |
| 3260 | |
| 3261 | eir_len = eir_append_data(ev->eir, 0, EIR_NAME_COMPLETE, name, |
| 3262 | name_len); |
| 3263 | |
| 3264 | put_unaligned_le16(eir_len, &ev->eir_len); |
| 3265 | |
Johan Hedberg | 053c7e0 | 2012-02-04 00:06:00 +0200 | [diff] [blame] | 3266 | return mgmt_event(MGMT_EV_DEVICE_FOUND, hdev, ev, |
| 3267 | sizeof(*ev) + eir_len, NULL); |
Johan Hedberg | a88a965 | 2011-03-30 13:18:12 +0300 | [diff] [blame] | 3268 | } |
Johan Hedberg | 314b238 | 2011-04-27 10:29:57 -0400 | [diff] [blame] | 3269 | |
Andre Guedes | 7a13510 | 2011-11-09 17:14:25 -0300 | [diff] [blame] | 3270 | int mgmt_start_discovery_failed(struct hci_dev *hdev, u8 status) |
Johan Hedberg | 164a6e7 | 2011-11-01 17:06:44 +0200 | [diff] [blame] | 3271 | { |
| 3272 | struct pending_cmd *cmd; |
| 3273 | int err; |
| 3274 | |
Andre Guedes | 203159d | 2012-02-13 15:41:01 -0300 | [diff] [blame] | 3275 | hci_discovery_set_state(hdev, DISCOVERY_STOPPED); |
| 3276 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 3277 | cmd = mgmt_pending_find(MGMT_OP_START_DISCOVERY, hdev); |
Johan Hedberg | 164a6e7 | 2011-11-01 17:06:44 +0200 | [diff] [blame] | 3278 | if (!cmd) |
| 3279 | return -ENOENT; |
| 3280 | |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 3281 | err = cmd_status(cmd->sk, hdev->id, cmd->opcode, mgmt_status(status)); |
Johan Hedberg | 164a6e7 | 2011-11-01 17:06:44 +0200 | [diff] [blame] | 3282 | mgmt_pending_remove(cmd); |
| 3283 | |
| 3284 | return err; |
| 3285 | } |
| 3286 | |
Andre Guedes | e6d465c | 2011-11-09 17:14:26 -0300 | [diff] [blame] | 3287 | int mgmt_stop_discovery_failed(struct hci_dev *hdev, u8 status) |
| 3288 | { |
| 3289 | struct pending_cmd *cmd; |
| 3290 | int err; |
| 3291 | |
| 3292 | cmd = mgmt_pending_find(MGMT_OP_STOP_DISCOVERY, hdev); |
| 3293 | if (!cmd) |
| 3294 | return -ENOENT; |
| 3295 | |
Andre Guedes | e75a8b0c | 2012-01-02 16:50:53 -0300 | [diff] [blame] | 3296 | err = cmd_status(cmd->sk, hdev->id, cmd->opcode, mgmt_status(status)); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 3297 | mgmt_pending_remove(cmd); |
| 3298 | |
| 3299 | return err; |
| 3300 | } |
Johan Hedberg | 314b238 | 2011-04-27 10:29:57 -0400 | [diff] [blame] | 3301 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 3302 | int mgmt_discovering(struct hci_dev *hdev, u8 discovering) |
Johan Hedberg | 314b238 | 2011-04-27 10:29:57 -0400 | [diff] [blame] | 3303 | { |
Johan Hedberg | 164a6e7 | 2011-11-01 17:06:44 +0200 | [diff] [blame] | 3304 | struct pending_cmd *cmd; |
| 3305 | |
Andre Guedes | 343fb14 | 2011-11-22 17:14:19 -0300 | [diff] [blame] | 3306 | BT_DBG("%s discovering %u", hdev->name, discovering); |
| 3307 | |
Johan Hedberg | 164a6e7 | 2011-11-01 17:06:44 +0200 | [diff] [blame] | 3308 | if (discovering) |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 3309 | cmd = mgmt_pending_find(MGMT_OP_START_DISCOVERY, hdev); |
Johan Hedberg | 164a6e7 | 2011-11-01 17:06:44 +0200 | [diff] [blame] | 3310 | else |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 3311 | cmd = mgmt_pending_find(MGMT_OP_STOP_DISCOVERY, hdev); |
Johan Hedberg | 164a6e7 | 2011-11-01 17:06:44 +0200 | [diff] [blame] | 3312 | |
| 3313 | if (cmd != NULL) { |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 3314 | cmd_complete(cmd->sk, hdev->id, cmd->opcode, NULL, 0); |
Johan Hedberg | 164a6e7 | 2011-11-01 17:06:44 +0200 | [diff] [blame] | 3315 | mgmt_pending_remove(cmd); |
| 3316 | } |
| 3317 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 3318 | return mgmt_event(MGMT_EV_DISCOVERING, hdev, &discovering, |
Johan Hedberg | 314b238 | 2011-04-27 10:29:57 -0400 | [diff] [blame] | 3319 | sizeof(discovering), NULL); |
| 3320 | } |
Antti Julku | 5e76244 | 2011-08-25 16:48:02 +0300 | [diff] [blame] | 3321 | |
Johan Hedberg | 88c1fe4 | 2012-02-09 15:56:11 +0200 | [diff] [blame] | 3322 | int mgmt_device_blocked(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 type) |
Antti Julku | 5e76244 | 2011-08-25 16:48:02 +0300 | [diff] [blame] | 3323 | { |
| 3324 | struct pending_cmd *cmd; |
| 3325 | struct mgmt_ev_device_blocked ev; |
| 3326 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 3327 | cmd = mgmt_pending_find(MGMT_OP_BLOCK_DEVICE, hdev); |
Antti Julku | 5e76244 | 2011-08-25 16:48:02 +0300 | [diff] [blame] | 3328 | |
Johan Hedberg | 88c1fe4 | 2012-02-09 15:56:11 +0200 | [diff] [blame] | 3329 | bacpy(&ev.addr.bdaddr, bdaddr); |
| 3330 | ev.addr.type = type; |
Antti Julku | 5e76244 | 2011-08-25 16:48:02 +0300 | [diff] [blame] | 3331 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 3332 | return mgmt_event(MGMT_EV_DEVICE_BLOCKED, hdev, &ev, sizeof(ev), |
| 3333 | cmd ? cmd->sk : NULL); |
Antti Julku | 5e76244 | 2011-08-25 16:48:02 +0300 | [diff] [blame] | 3334 | } |
| 3335 | |
Johan Hedberg | 88c1fe4 | 2012-02-09 15:56:11 +0200 | [diff] [blame] | 3336 | int mgmt_device_unblocked(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 type) |
Antti Julku | 5e76244 | 2011-08-25 16:48:02 +0300 | [diff] [blame] | 3337 | { |
| 3338 | struct pending_cmd *cmd; |
| 3339 | struct mgmt_ev_device_unblocked ev; |
| 3340 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 3341 | cmd = mgmt_pending_find(MGMT_OP_UNBLOCK_DEVICE, hdev); |
Antti Julku | 5e76244 | 2011-08-25 16:48:02 +0300 | [diff] [blame] | 3342 | |
Johan Hedberg | 88c1fe4 | 2012-02-09 15:56:11 +0200 | [diff] [blame] | 3343 | bacpy(&ev.addr.bdaddr, bdaddr); |
| 3344 | ev.addr.type = type; |
Antti Julku | 5e76244 | 2011-08-25 16:48:02 +0300 | [diff] [blame] | 3345 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 3346 | return mgmt_event(MGMT_EV_DEVICE_UNBLOCKED, hdev, &ev, sizeof(ev), |
| 3347 | cmd ? cmd->sk : NULL); |
Antti Julku | 5e76244 | 2011-08-25 16:48:02 +0300 | [diff] [blame] | 3348 | } |