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