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