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 | |
| 1738 | count = 0; |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 1739 | list_for_each_entry(c, &hdev->conn_hash.list, list) { |
| 1740 | if (test_bit(HCI_CONN_MGMT_CONNECTED, &c->flags)) |
| 1741 | count++; |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 1742 | } |
| 1743 | |
Johan Hedberg | 4c659c3 | 2011-11-07 23:13:39 +0200 | [diff] [blame] | 1744 | rp_len = sizeof(*rp) + (count * sizeof(struct mgmt_addr_info)); |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 1745 | rp = kmalloc(rp_len, GFP_ATOMIC); |
| 1746 | if (!rp) { |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 1747 | err = -ENOMEM; |
| 1748 | goto unlock; |
| 1749 | } |
| 1750 | |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 1751 | put_unaligned_le16(count, &rp->conn_count); |
| 1752 | |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 1753 | i = 0; |
Johan Hedberg | 4c659c3 | 2011-11-07 23:13:39 +0200 | [diff] [blame] | 1754 | list_for_each_entry(c, &hdev->conn_hash.list, list) { |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 1755 | if (!test_bit(HCI_CONN_MGMT_CONNECTED, &c->flags)) |
| 1756 | continue; |
Johan Hedberg | 4c659c3 | 2011-11-07 23:13:39 +0200 | [diff] [blame] | 1757 | bacpy(&rp->addr[i].bdaddr, &c->dst); |
Johan Hedberg | 48264f0 | 2011-11-09 13:58:58 +0200 | [diff] [blame] | 1758 | rp->addr[i].type = link_to_mgmt(c->type, c->dst_type); |
Johan Hedberg | 4c659c3 | 2011-11-07 23:13:39 +0200 | [diff] [blame] | 1759 | if (rp->addr[i].type == MGMT_ADDR_INVALID) |
| 1760 | continue; |
| 1761 | i++; |
| 1762 | } |
| 1763 | |
| 1764 | /* Recalculate length in case of filtered SCO connections, etc */ |
| 1765 | rp_len = sizeof(*rp) + (i * sizeof(struct mgmt_addr_info)); |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 1766 | |
Johan Hedberg | aee9b21 | 2012-02-18 15:07:59 +0200 | [diff] [blame] | 1767 | 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] | 1768 | |
| 1769 | unlock: |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 1770 | kfree(rp); |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1771 | hci_dev_unlock(hdev); |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 1772 | hci_dev_put(hdev); |
| 1773 | return err; |
| 1774 | } |
| 1775 | |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 1776 | static int send_pin_code_neg_reply(struct sock *sk, u16 index, |
| 1777 | struct hci_dev *hdev, struct mgmt_cp_pin_code_neg_reply *cp) |
| 1778 | { |
| 1779 | struct pending_cmd *cmd; |
| 1780 | int err; |
| 1781 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 1782 | 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] | 1783 | sizeof(*cp)); |
| 1784 | if (!cmd) |
| 1785 | return -ENOMEM; |
| 1786 | |
Johan Hedberg | d845769 | 2012-02-17 14:24:57 +0200 | [diff] [blame] | 1787 | err = hci_send_cmd(hdev, HCI_OP_PIN_CODE_NEG_REPLY, |
| 1788 | sizeof(cp->addr.bdaddr), &cp->addr.bdaddr); |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 1789 | if (err < 0) |
| 1790 | mgmt_pending_remove(cmd); |
| 1791 | |
| 1792 | return err; |
| 1793 | } |
| 1794 | |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 1795 | 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] | 1796 | { |
| 1797 | struct hci_dev *hdev; |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 1798 | struct hci_conn *conn; |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 1799 | struct mgmt_cp_pin_code_reply *cp = data; |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 1800 | struct hci_cp_pin_code_reply reply; |
Johan Hedberg | 366a033 | 2011-02-19 12:05:55 -0300 | [diff] [blame] | 1801 | struct pending_cmd *cmd; |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 1802 | int err; |
| 1803 | |
| 1804 | BT_DBG(""); |
| 1805 | |
Szymon Janc | bdce7ba | 2011-02-25 19:05:49 +0100 | [diff] [blame] | 1806 | if (len != sizeof(*cp)) |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 1807 | return cmd_status(sk, index, MGMT_OP_PIN_CODE_REPLY, |
| 1808 | MGMT_STATUS_INVALID_PARAMS); |
Szymon Janc | bdce7ba | 2011-02-25 19:05:49 +0100 | [diff] [blame] | 1809 | |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 1810 | hdev = hci_dev_get(index); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 1811 | if (!hdev) |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 1812 | return cmd_status(sk, index, MGMT_OP_PIN_CODE_REPLY, |
| 1813 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 1814 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1815 | hci_dev_lock(hdev); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 1816 | |
Johan Hedberg | 4b34ee78 | 2012-02-21 14:13:02 +0200 | [diff] [blame] | 1817 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 1818 | err = cmd_status(sk, index, MGMT_OP_PIN_CODE_REPLY, |
| 1819 | MGMT_STATUS_NOT_POWERED); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 1820 | goto failed; |
| 1821 | } |
| 1822 | |
Johan Hedberg | d845769 | 2012-02-17 14:24:57 +0200 | [diff] [blame] | 1823 | conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &cp->addr.bdaddr); |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 1824 | if (!conn) { |
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_CONNECTED); |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 1827 | goto failed; |
| 1828 | } |
| 1829 | |
| 1830 | if (conn->pending_sec_level == BT_SECURITY_HIGH && cp->pin_len != 16) { |
Johan Hedberg | d845769 | 2012-02-17 14:24:57 +0200 | [diff] [blame] | 1831 | struct mgmt_cp_pin_code_neg_reply ncp; |
| 1832 | |
| 1833 | memcpy(&ncp.addr, &cp->addr, sizeof(ncp.addr)); |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 1834 | |
| 1835 | BT_ERR("PIN code is not 16 bytes long"); |
| 1836 | |
| 1837 | err = send_pin_code_neg_reply(sk, index, hdev, &ncp); |
| 1838 | if (err >= 0) |
| 1839 | err = cmd_status(sk, index, MGMT_OP_PIN_CODE_REPLY, |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 1840 | MGMT_STATUS_INVALID_PARAMS); |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 1841 | |
| 1842 | goto failed; |
| 1843 | } |
| 1844 | |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 1845 | cmd = mgmt_pending_add(sk, MGMT_OP_PIN_CODE_REPLY, hdev, data, |
| 1846 | len); |
Johan Hedberg | 366a033 | 2011-02-19 12:05:55 -0300 | [diff] [blame] | 1847 | if (!cmd) { |
| 1848 | err = -ENOMEM; |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 1849 | goto failed; |
Johan Hedberg | 366a033 | 2011-02-19 12:05:55 -0300 | [diff] [blame] | 1850 | } |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 1851 | |
Johan Hedberg | d845769 | 2012-02-17 14:24:57 +0200 | [diff] [blame] | 1852 | bacpy(&reply.bdaddr, &cp->addr.bdaddr); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 1853 | reply.pin_len = cp->pin_len; |
Waldemar Rymarkiewicz | 24718ca | 2011-06-01 17:28:47 +0200 | [diff] [blame] | 1854 | memcpy(reply.pin_code, cp->pin_code, sizeof(reply.pin_code)); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 1855 | |
| 1856 | err = hci_send_cmd(hdev, HCI_OP_PIN_CODE_REPLY, sizeof(reply), &reply); |
| 1857 | if (err < 0) |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 1858 | mgmt_pending_remove(cmd); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 1859 | |
| 1860 | failed: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1861 | hci_dev_unlock(hdev); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 1862 | hci_dev_put(hdev); |
| 1863 | |
| 1864 | return err; |
| 1865 | } |
| 1866 | |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 1867 | 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] | 1868 | { |
| 1869 | struct hci_dev *hdev; |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 1870 | struct mgmt_cp_pin_code_neg_reply *cp = data; |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 1871 | int err; |
| 1872 | |
| 1873 | BT_DBG(""); |
| 1874 | |
Szymon Janc | bdce7ba | 2011-02-25 19:05:49 +0100 | [diff] [blame] | 1875 | if (len != sizeof(*cp)) |
| 1876 | return cmd_status(sk, index, MGMT_OP_PIN_CODE_NEG_REPLY, |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 1877 | MGMT_STATUS_INVALID_PARAMS); |
Szymon Janc | bdce7ba | 2011-02-25 19:05:49 +0100 | [diff] [blame] | 1878 | |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 1879 | hdev = hci_dev_get(index); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 1880 | if (!hdev) |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 1881 | return cmd_status(sk, index, MGMT_OP_PIN_CODE_NEG_REPLY, |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 1882 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 1883 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1884 | hci_dev_lock(hdev); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 1885 | |
Johan Hedberg | 4b34ee78 | 2012-02-21 14:13:02 +0200 | [diff] [blame] | 1886 | if (!hdev_is_powered(hdev)) { |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 1887 | err = cmd_status(sk, index, MGMT_OP_PIN_CODE_NEG_REPLY, |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 1888 | MGMT_STATUS_NOT_POWERED); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 1889 | goto failed; |
| 1890 | } |
| 1891 | |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 1892 | err = send_pin_code_neg_reply(sk, index, hdev, cp); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 1893 | |
| 1894 | failed: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1895 | hci_dev_unlock(hdev); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 1896 | hci_dev_put(hdev); |
| 1897 | |
| 1898 | return err; |
| 1899 | } |
| 1900 | |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 1901 | 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] | 1902 | { |
| 1903 | struct hci_dev *hdev; |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 1904 | struct mgmt_cp_set_io_capability *cp = data; |
Johan Hedberg | 17fa4b9 | 2011-01-25 13:28:33 +0200 | [diff] [blame] | 1905 | |
| 1906 | BT_DBG(""); |
| 1907 | |
Szymon Janc | bdce7ba | 2011-02-25 19:05:49 +0100 | [diff] [blame] | 1908 | if (len != sizeof(*cp)) |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 1909 | return cmd_status(sk, index, MGMT_OP_SET_IO_CAPABILITY, |
| 1910 | MGMT_STATUS_INVALID_PARAMS); |
Szymon Janc | bdce7ba | 2011-02-25 19:05:49 +0100 | [diff] [blame] | 1911 | |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 1912 | hdev = hci_dev_get(index); |
Johan Hedberg | 17fa4b9 | 2011-01-25 13:28:33 +0200 | [diff] [blame] | 1913 | if (!hdev) |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 1914 | return cmd_status(sk, index, MGMT_OP_SET_IO_CAPABILITY, |
| 1915 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | 17fa4b9 | 2011-01-25 13:28:33 +0200 | [diff] [blame] | 1916 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1917 | hci_dev_lock(hdev); |
Johan Hedberg | 17fa4b9 | 2011-01-25 13:28:33 +0200 | [diff] [blame] | 1918 | |
| 1919 | hdev->io_capability = cp->io_capability; |
| 1920 | |
| 1921 | BT_DBG("%s IO capability set to 0x%02x", hdev->name, |
Szymon Janc | b8534e0 | 2011-03-01 16:55:34 +0100 | [diff] [blame] | 1922 | hdev->io_capability); |
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_unlock(hdev); |
Johan Hedberg | 17fa4b9 | 2011-01-25 13:28:33 +0200 | [diff] [blame] | 1925 | hci_dev_put(hdev); |
| 1926 | |
Johan Hedberg | aee9b21 | 2012-02-18 15:07:59 +0200 | [diff] [blame] | 1927 | 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] | 1928 | } |
| 1929 | |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 1930 | static inline struct pending_cmd *find_pairing(struct hci_conn *conn) |
| 1931 | { |
| 1932 | struct hci_dev *hdev = conn->hdev; |
Luiz Augusto von Dentz | 8035ded | 2011-11-01 10:58:56 +0200 | [diff] [blame] | 1933 | struct pending_cmd *cmd; |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 1934 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 1935 | list_for_each_entry(cmd, &hdev->mgmt_pending, list) { |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 1936 | if (cmd->opcode != MGMT_OP_PAIR_DEVICE) |
| 1937 | continue; |
| 1938 | |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 1939 | if (cmd->user_data != conn) |
| 1940 | continue; |
| 1941 | |
| 1942 | return cmd; |
| 1943 | } |
| 1944 | |
| 1945 | return NULL; |
| 1946 | } |
| 1947 | |
| 1948 | static void pairing_complete(struct pending_cmd *cmd, u8 status) |
| 1949 | { |
| 1950 | struct mgmt_rp_pair_device rp; |
| 1951 | struct hci_conn *conn = cmd->user_data; |
| 1952 | |
Johan Hedberg | ba4e564 | 2011-11-11 00:07:34 +0200 | [diff] [blame] | 1953 | bacpy(&rp.addr.bdaddr, &conn->dst); |
| 1954 | rp.addr.type = link_to_mgmt(conn->type, conn->dst_type); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 1955 | |
Johan Hedberg | aee9b21 | 2012-02-18 15:07:59 +0200 | [diff] [blame] | 1956 | cmd_complete(cmd->sk, cmd->index, MGMT_OP_PAIR_DEVICE, status, |
| 1957 | &rp, sizeof(rp)); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 1958 | |
| 1959 | /* So we don't get further callbacks for this connection */ |
| 1960 | conn->connect_cfm_cb = NULL; |
| 1961 | conn->security_cfm_cb = NULL; |
| 1962 | conn->disconn_cfm_cb = NULL; |
| 1963 | |
| 1964 | hci_conn_put(conn); |
| 1965 | |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 1966 | mgmt_pending_remove(cmd); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 1967 | } |
| 1968 | |
| 1969 | static void pairing_complete_cb(struct hci_conn *conn, u8 status) |
| 1970 | { |
| 1971 | struct pending_cmd *cmd; |
| 1972 | |
| 1973 | BT_DBG("status %u", status); |
| 1974 | |
Johan Hedberg | 56e5cb8 | 2011-11-08 20:40:16 +0200 | [diff] [blame] | 1975 | cmd = find_pairing(conn); |
| 1976 | if (!cmd) |
| 1977 | BT_DBG("Unable to find a pending command"); |
| 1978 | else |
Johan Hedberg | e211326 | 2012-02-18 15:20:03 +0200 | [diff] [blame] | 1979 | pairing_complete(cmd, mgmt_status(status)); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 1980 | } |
| 1981 | |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 1982 | 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] | 1983 | { |
| 1984 | struct hci_dev *hdev; |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 1985 | struct mgmt_cp_pair_device *cp = data; |
Johan Hedberg | 1425acb | 2011-11-11 00:07:35 +0200 | [diff] [blame] | 1986 | struct mgmt_rp_pair_device rp; |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 1987 | struct pending_cmd *cmd; |
| 1988 | u8 sec_level, auth_type; |
| 1989 | struct hci_conn *conn; |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 1990 | int err; |
| 1991 | |
| 1992 | BT_DBG(""); |
| 1993 | |
Szymon Janc | bdce7ba | 2011-02-25 19:05:49 +0100 | [diff] [blame] | 1994 | if (len != sizeof(*cp)) |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 1995 | return cmd_status(sk, index, MGMT_OP_PAIR_DEVICE, |
| 1996 | MGMT_STATUS_INVALID_PARAMS); |
Szymon Janc | bdce7ba | 2011-02-25 19:05:49 +0100 | [diff] [blame] | 1997 | |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 1998 | hdev = hci_dev_get(index); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 1999 | if (!hdev) |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 2000 | return cmd_status(sk, index, MGMT_OP_PAIR_DEVICE, |
| 2001 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2002 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2003 | hci_dev_lock(hdev); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2004 | |
Vinicius Costa Gomes | c908df3 | 2011-09-02 14:51:22 -0300 | [diff] [blame] | 2005 | sec_level = BT_SECURITY_MEDIUM; |
| 2006 | if (cp->io_cap == 0x03) |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2007 | auth_type = HCI_AT_DEDICATED_BONDING; |
Vinicius Costa Gomes | c908df3 | 2011-09-02 14:51:22 -0300 | [diff] [blame] | 2008 | else |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2009 | auth_type = HCI_AT_DEDICATED_BONDING_MITM; |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2010 | |
Johan Hedberg | ba4e564 | 2011-11-11 00:07:34 +0200 | [diff] [blame] | 2011 | if (cp->addr.type == MGMT_ADDR_BREDR) |
| 2012 | conn = hci_connect(hdev, ACL_LINK, &cp->addr.bdaddr, sec_level, |
Vinicius Costa Gomes | 7a512d0 | 2011-08-19 21:06:54 -0300 | [diff] [blame] | 2013 | auth_type); |
| 2014 | else |
Johan Hedberg | ba4e564 | 2011-11-11 00:07:34 +0200 | [diff] [blame] | 2015 | conn = hci_connect(hdev, LE_LINK, &cp->addr.bdaddr, sec_level, |
Vinicius Costa Gomes | 7a512d0 | 2011-08-19 21:06:54 -0300 | [diff] [blame] | 2016 | auth_type); |
| 2017 | |
Johan Hedberg | 1425acb | 2011-11-11 00:07:35 +0200 | [diff] [blame] | 2018 | memset(&rp, 0, sizeof(rp)); |
| 2019 | bacpy(&rp.addr.bdaddr, &cp->addr.bdaddr); |
| 2020 | rp.addr.type = cp->addr.type; |
| 2021 | |
Ville Tervo | 30e7627 | 2011-02-22 16:10:53 -0300 | [diff] [blame] | 2022 | if (IS_ERR(conn)) { |
Johan Hedberg | e211326 | 2012-02-18 15:20:03 +0200 | [diff] [blame] | 2023 | err = cmd_complete(sk, index, MGMT_OP_PAIR_DEVICE, |
| 2024 | MGMT_STATUS_CONNECT_FAILED, |
| 2025 | &rp, sizeof(rp)); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2026 | goto unlock; |
| 2027 | } |
| 2028 | |
| 2029 | if (conn->connect_cfm_cb) { |
| 2030 | hci_conn_put(conn); |
Johan Hedberg | e211326 | 2012-02-18 15:20:03 +0200 | [diff] [blame] | 2031 | err = cmd_complete(sk, index, MGMT_OP_PAIR_DEVICE, |
| 2032 | MGMT_STATUS_BUSY, &rp, sizeof(rp)); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2033 | goto unlock; |
| 2034 | } |
| 2035 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 2036 | cmd = mgmt_pending_add(sk, MGMT_OP_PAIR_DEVICE, hdev, data, len); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2037 | if (!cmd) { |
| 2038 | err = -ENOMEM; |
| 2039 | hci_conn_put(conn); |
| 2040 | goto unlock; |
| 2041 | } |
| 2042 | |
Vinicius Costa Gomes | 7a512d0 | 2011-08-19 21:06:54 -0300 | [diff] [blame] | 2043 | /* For LE, just connecting isn't a proof that the pairing finished */ |
Johan Hedberg | ba4e564 | 2011-11-11 00:07:34 +0200 | [diff] [blame] | 2044 | if (cp->addr.type == MGMT_ADDR_BREDR) |
Vinicius Costa Gomes | 7a512d0 | 2011-08-19 21:06:54 -0300 | [diff] [blame] | 2045 | conn->connect_cfm_cb = pairing_complete_cb; |
| 2046 | |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2047 | conn->security_cfm_cb = pairing_complete_cb; |
| 2048 | conn->disconn_cfm_cb = pairing_complete_cb; |
| 2049 | conn->io_capability = cp->io_cap; |
| 2050 | cmd->user_data = conn; |
| 2051 | |
| 2052 | if (conn->state == BT_CONNECTED && |
| 2053 | hci_conn_security(conn, sec_level, auth_type)) |
| 2054 | pairing_complete(cmd, 0); |
| 2055 | |
| 2056 | err = 0; |
| 2057 | |
| 2058 | unlock: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2059 | hci_dev_unlock(hdev); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2060 | hci_dev_put(hdev); |
| 2061 | |
| 2062 | return err; |
| 2063 | } |
| 2064 | |
Johan Hedberg | 2842470 | 2012-02-02 04:02:29 +0200 | [diff] [blame] | 2065 | static int cancel_pair_device(struct sock *sk, u16 index, |
| 2066 | unsigned char *data, u16 len) |
| 2067 | { |
| 2068 | struct mgmt_addr_info *addr = (void *) data; |
| 2069 | struct hci_dev *hdev; |
| 2070 | struct pending_cmd *cmd; |
| 2071 | struct hci_conn *conn; |
| 2072 | int err; |
| 2073 | |
| 2074 | BT_DBG(""); |
| 2075 | |
| 2076 | if (len != sizeof(*addr)) |
| 2077 | return cmd_status(sk, index, MGMT_OP_CANCEL_PAIR_DEVICE, |
| 2078 | MGMT_STATUS_INVALID_PARAMS); |
| 2079 | |
| 2080 | hdev = hci_dev_get(index); |
| 2081 | if (!hdev) |
| 2082 | return cmd_status(sk, index, MGMT_OP_CANCEL_PAIR_DEVICE, |
| 2083 | MGMT_STATUS_INVALID_PARAMS); |
| 2084 | |
| 2085 | hci_dev_lock(hdev); |
| 2086 | |
| 2087 | cmd = mgmt_pending_find(MGMT_OP_PAIR_DEVICE, hdev); |
| 2088 | if (!cmd) { |
| 2089 | err = cmd_status(sk, index, MGMT_OP_CANCEL_PAIR_DEVICE, |
| 2090 | MGMT_STATUS_INVALID_PARAMS); |
| 2091 | goto unlock; |
| 2092 | } |
| 2093 | |
| 2094 | conn = cmd->user_data; |
| 2095 | |
| 2096 | if (bacmp(&addr->bdaddr, &conn->dst) != 0) { |
| 2097 | err = cmd_status(sk, index, MGMT_OP_CANCEL_PAIR_DEVICE, |
| 2098 | MGMT_STATUS_INVALID_PARAMS); |
| 2099 | goto unlock; |
| 2100 | } |
| 2101 | |
| 2102 | pairing_complete(cmd, MGMT_STATUS_CANCELLED); |
| 2103 | |
Johan Hedberg | aee9b21 | 2012-02-18 15:07:59 +0200 | [diff] [blame] | 2104 | err = cmd_complete(sk, index, MGMT_OP_CANCEL_PAIR_DEVICE, 0, addr, |
Johan Hedberg | 2842470 | 2012-02-02 04:02:29 +0200 | [diff] [blame] | 2105 | sizeof(*addr)); |
| 2106 | unlock: |
| 2107 | hci_dev_unlock(hdev); |
| 2108 | hci_dev_put(hdev); |
| 2109 | |
| 2110 | return err; |
| 2111 | } |
| 2112 | |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 2113 | 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] | 2114 | u8 type, u16 mgmt_op, u16 hci_op, |
| 2115 | __le32 passkey) |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 2116 | { |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 2117 | struct pending_cmd *cmd; |
| 2118 | struct hci_dev *hdev; |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 2119 | struct hci_conn *conn; |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 2120 | int err; |
| 2121 | |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 2122 | hdev = hci_dev_get(index); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 2123 | if (!hdev) |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 2124 | return cmd_status(sk, index, mgmt_op, |
| 2125 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 2126 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2127 | hci_dev_lock(hdev); |
Johan Hedberg | 08ba538 | 2011-03-16 14:29:34 +0200 | [diff] [blame] | 2128 | |
Johan Hedberg | 4b34ee78 | 2012-02-21 14:13:02 +0200 | [diff] [blame] | 2129 | if (!hdev_is_powered(hdev)) { |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 2130 | err = cmd_status(sk, index, mgmt_op, MGMT_STATUS_NOT_POWERED); |
| 2131 | goto done; |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 2132 | } |
| 2133 | |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 2134 | if (type == MGMT_ADDR_BREDR) |
| 2135 | conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, bdaddr); |
| 2136 | else |
Brian Gix | 47c15e2 | 2011-11-16 13:53:14 -0800 | [diff] [blame] | 2137 | conn = hci_conn_hash_lookup_ba(hdev, LE_LINK, bdaddr); |
Brian Gix | 47c15e2 | 2011-11-16 13:53:14 -0800 | [diff] [blame] | 2138 | |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 2139 | if (!conn) { |
| 2140 | err = cmd_status(sk, index, mgmt_op, |
| 2141 | MGMT_STATUS_NOT_CONNECTED); |
| 2142 | goto done; |
| 2143 | } |
| 2144 | |
| 2145 | if (type == MGMT_ADDR_LE_PUBLIC || type == MGMT_ADDR_LE_RANDOM) { |
Brian Gix | 47c15e2 | 2011-11-16 13:53:14 -0800 | [diff] [blame] | 2146 | /* Continue with pairing via SMP */ |
Brian Gix | 5fe57d9 | 2011-12-21 16:12:13 -0800 | [diff] [blame] | 2147 | err = smp_user_confirm_reply(conn, mgmt_op, passkey); |
Brian Gix | 47c15e2 | 2011-11-16 13:53:14 -0800 | [diff] [blame] | 2148 | |
Brian Gix | 5fe57d9 | 2011-12-21 16:12:13 -0800 | [diff] [blame] | 2149 | if (!err) |
| 2150 | err = cmd_status(sk, index, mgmt_op, |
| 2151 | MGMT_STATUS_SUCCESS); |
| 2152 | else |
| 2153 | err = cmd_status(sk, index, mgmt_op, |
| 2154 | MGMT_STATUS_FAILED); |
| 2155 | |
Brian Gix | 47c15e2 | 2011-11-16 13:53:14 -0800 | [diff] [blame] | 2156 | goto done; |
| 2157 | } |
| 2158 | |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 2159 | cmd = mgmt_pending_add(sk, mgmt_op, hdev, bdaddr, sizeof(*bdaddr)); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 2160 | if (!cmd) { |
| 2161 | err = -ENOMEM; |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 2162 | goto done; |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 2163 | } |
| 2164 | |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 2165 | /* Continue with pairing via HCI */ |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 2166 | if (hci_op == HCI_OP_USER_PASSKEY_REPLY) { |
| 2167 | struct hci_cp_user_passkey_reply cp; |
| 2168 | |
| 2169 | bacpy(&cp.bdaddr, bdaddr); |
| 2170 | cp.passkey = passkey; |
| 2171 | err = hci_send_cmd(hdev, hci_op, sizeof(cp), &cp); |
| 2172 | } else |
| 2173 | err = hci_send_cmd(hdev, hci_op, sizeof(*bdaddr), bdaddr); |
| 2174 | |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 2175 | if (err < 0) |
| 2176 | mgmt_pending_remove(cmd); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 2177 | |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 2178 | done: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2179 | hci_dev_unlock(hdev); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 2180 | hci_dev_put(hdev); |
| 2181 | |
| 2182 | return err; |
| 2183 | } |
| 2184 | |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 2185 | static int user_confirm_reply(struct sock *sk, u16 index, void *data, u16 len) |
| 2186 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 2187 | struct mgmt_cp_user_confirm_reply *cp = data; |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 2188 | |
| 2189 | BT_DBG(""); |
| 2190 | |
| 2191 | if (len != sizeof(*cp)) |
| 2192 | return cmd_status(sk, index, MGMT_OP_USER_CONFIRM_REPLY, |
| 2193 | MGMT_STATUS_INVALID_PARAMS); |
| 2194 | |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 2195 | return user_pairing_resp(sk, index, &cp->addr.bdaddr, cp->addr.type, |
| 2196 | MGMT_OP_USER_CONFIRM_REPLY, |
| 2197 | HCI_OP_USER_CONFIRM_REPLY, 0); |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 2198 | } |
| 2199 | |
| 2200 | static int user_confirm_neg_reply(struct sock *sk, u16 index, void *data, |
| 2201 | u16 len) |
| 2202 | { |
Johan Hedberg | c9c2659 | 2011-12-15 00:47:41 +0200 | [diff] [blame] | 2203 | struct mgmt_cp_user_confirm_neg_reply *cp = data; |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 2204 | |
| 2205 | BT_DBG(""); |
| 2206 | |
| 2207 | if (len != sizeof(*cp)) |
| 2208 | return cmd_status(sk, index, MGMT_OP_USER_CONFIRM_NEG_REPLY, |
| 2209 | MGMT_STATUS_INVALID_PARAMS); |
| 2210 | |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 2211 | return user_pairing_resp(sk, index, &cp->addr.bdaddr, cp->addr.type, |
| 2212 | MGMT_OP_USER_CONFIRM_NEG_REPLY, |
| 2213 | HCI_OP_USER_CONFIRM_NEG_REPLY, 0); |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 2214 | } |
| 2215 | |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 2216 | static int user_passkey_reply(struct sock *sk, u16 index, void *data, u16 len) |
| 2217 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 2218 | struct mgmt_cp_user_passkey_reply *cp = data; |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 2219 | |
| 2220 | BT_DBG(""); |
| 2221 | |
| 2222 | if (len != sizeof(*cp)) |
| 2223 | return cmd_status(sk, index, MGMT_OP_USER_PASSKEY_REPLY, |
| 2224 | EINVAL); |
| 2225 | |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 2226 | return user_pairing_resp(sk, index, &cp->addr.bdaddr, cp->addr.type, |
| 2227 | MGMT_OP_USER_PASSKEY_REPLY, |
| 2228 | HCI_OP_USER_PASSKEY_REPLY, |
| 2229 | cp->passkey); |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 2230 | } |
| 2231 | |
| 2232 | static int user_passkey_neg_reply(struct sock *sk, u16 index, void *data, |
| 2233 | u16 len) |
| 2234 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 2235 | struct mgmt_cp_user_passkey_neg_reply *cp = data; |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 2236 | |
| 2237 | BT_DBG(""); |
| 2238 | |
| 2239 | if (len != sizeof(*cp)) |
| 2240 | return cmd_status(sk, index, MGMT_OP_USER_PASSKEY_NEG_REPLY, |
| 2241 | EINVAL); |
| 2242 | |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 2243 | return user_pairing_resp(sk, index, &cp->addr.bdaddr, cp->addr.type, |
| 2244 | MGMT_OP_USER_PASSKEY_NEG_REPLY, |
| 2245 | HCI_OP_USER_PASSKEY_NEG_REPLY, 0); |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 2246 | } |
| 2247 | |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 2248 | static int set_local_name(struct sock *sk, u16 index, void *data, |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 2249 | u16 len) |
| 2250 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 2251 | struct mgmt_cp_set_local_name *mgmt_cp = data; |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 2252 | struct hci_cp_write_local_name hci_cp; |
| 2253 | struct hci_dev *hdev; |
| 2254 | struct pending_cmd *cmd; |
| 2255 | int err; |
| 2256 | |
| 2257 | BT_DBG(""); |
| 2258 | |
| 2259 | if (len != sizeof(*mgmt_cp)) |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 2260 | return cmd_status(sk, index, MGMT_OP_SET_LOCAL_NAME, |
| 2261 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 2262 | |
| 2263 | hdev = hci_dev_get(index); |
| 2264 | if (!hdev) |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 2265 | return cmd_status(sk, index, MGMT_OP_SET_LOCAL_NAME, |
| 2266 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 2267 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2268 | hci_dev_lock(hdev); |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 2269 | |
Johan Hedberg | 28cc7bd | 2012-02-22 21:06:55 +0200 | [diff] [blame] | 2270 | memcpy(hdev->short_name, mgmt_cp->short_name, |
| 2271 | sizeof(hdev->short_name)); |
| 2272 | |
Johan Hedberg | b5235a6 | 2012-02-21 14:32:24 +0200 | [diff] [blame] | 2273 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | 28cc7bd | 2012-02-22 21:06:55 +0200 | [diff] [blame] | 2274 | memcpy(hdev->dev_name, mgmt_cp->name, sizeof(hdev->dev_name)); |
| 2275 | |
| 2276 | err = cmd_complete(sk, hdev->id, MGMT_OP_SET_LOCAL_NAME, 0, |
| 2277 | data, len); |
| 2278 | if (err < 0) |
| 2279 | goto failed; |
| 2280 | |
| 2281 | err = mgmt_event(MGMT_EV_LOCAL_NAME_CHANGED, hdev, data, len, |
| 2282 | sk); |
| 2283 | |
Johan Hedberg | b5235a6 | 2012-02-21 14:32:24 +0200 | [diff] [blame] | 2284 | goto failed; |
| 2285 | } |
| 2286 | |
Johan Hedberg | 28cc7bd | 2012-02-22 21:06:55 +0200 | [diff] [blame] | 2287 | 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] | 2288 | if (!cmd) { |
| 2289 | err = -ENOMEM; |
| 2290 | goto failed; |
| 2291 | } |
| 2292 | |
| 2293 | memcpy(hci_cp.name, mgmt_cp->name, sizeof(hci_cp.name)); |
| 2294 | err = hci_send_cmd(hdev, HCI_OP_WRITE_LOCAL_NAME, sizeof(hci_cp), |
| 2295 | &hci_cp); |
| 2296 | if (err < 0) |
| 2297 | mgmt_pending_remove(cmd); |
| 2298 | |
| 2299 | failed: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2300 | hci_dev_unlock(hdev); |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 2301 | hci_dev_put(hdev); |
| 2302 | |
| 2303 | return err; |
| 2304 | } |
| 2305 | |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 2306 | static int read_local_oob_data(struct sock *sk, u16 index) |
| 2307 | { |
| 2308 | struct hci_dev *hdev; |
| 2309 | struct pending_cmd *cmd; |
| 2310 | int err; |
| 2311 | |
| 2312 | BT_DBG("hci%u", index); |
| 2313 | |
| 2314 | hdev = hci_dev_get(index); |
| 2315 | if (!hdev) |
| 2316 | return cmd_status(sk, index, MGMT_OP_READ_LOCAL_OOB_DATA, |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 2317 | MGMT_STATUS_INVALID_PARAMS); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 2318 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2319 | hci_dev_lock(hdev); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 2320 | |
Johan Hedberg | 4b34ee78 | 2012-02-21 14:13:02 +0200 | [diff] [blame] | 2321 | if (!hdev_is_powered(hdev)) { |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 2322 | err = cmd_status(sk, index, MGMT_OP_READ_LOCAL_OOB_DATA, |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 2323 | MGMT_STATUS_NOT_POWERED); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 2324 | goto unlock; |
| 2325 | } |
| 2326 | |
| 2327 | if (!(hdev->features[6] & LMP_SIMPLE_PAIR)) { |
| 2328 | err = cmd_status(sk, index, MGMT_OP_READ_LOCAL_OOB_DATA, |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 2329 | MGMT_STATUS_NOT_SUPPORTED); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 2330 | goto unlock; |
| 2331 | } |
| 2332 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 2333 | if (mgmt_pending_find(MGMT_OP_READ_LOCAL_OOB_DATA, hdev)) { |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 2334 | err = cmd_status(sk, index, MGMT_OP_READ_LOCAL_OOB_DATA, |
| 2335 | MGMT_STATUS_BUSY); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 2336 | goto unlock; |
| 2337 | } |
| 2338 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 2339 | 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] | 2340 | if (!cmd) { |
| 2341 | err = -ENOMEM; |
| 2342 | goto unlock; |
| 2343 | } |
| 2344 | |
| 2345 | err = hci_send_cmd(hdev, HCI_OP_READ_LOCAL_OOB_DATA, 0, NULL); |
| 2346 | if (err < 0) |
| 2347 | mgmt_pending_remove(cmd); |
| 2348 | |
| 2349 | unlock: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2350 | hci_dev_unlock(hdev); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 2351 | hci_dev_put(hdev); |
| 2352 | |
| 2353 | return err; |
| 2354 | } |
| 2355 | |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 2356 | static int add_remote_oob_data(struct sock *sk, u16 index, void *data, |
| 2357 | u16 len) |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 2358 | { |
| 2359 | struct hci_dev *hdev; |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 2360 | struct mgmt_cp_add_remote_oob_data *cp = data; |
Johan Hedberg | bf1e354 | 2012-02-19 13:16:14 +0200 | [diff] [blame] | 2361 | u8 status; |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 2362 | int err; |
| 2363 | |
| 2364 | BT_DBG("hci%u ", index); |
| 2365 | |
| 2366 | if (len != sizeof(*cp)) |
| 2367 | return cmd_status(sk, index, MGMT_OP_ADD_REMOTE_OOB_DATA, |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 2368 | MGMT_STATUS_INVALID_PARAMS); |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 2369 | |
| 2370 | hdev = hci_dev_get(index); |
| 2371 | if (!hdev) |
Johan Hedberg | bf1e354 | 2012-02-19 13:16:14 +0200 | [diff] [blame] | 2372 | return cmd_complete(sk, index, MGMT_OP_ADD_REMOTE_OOB_DATA, |
| 2373 | MGMT_STATUS_INVALID_PARAMS, |
| 2374 | &cp->addr, sizeof(cp->addr)); |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 2375 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2376 | hci_dev_lock(hdev); |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 2377 | |
Johan Hedberg | 664ce4c | 2012-02-09 15:44:09 +0200 | [diff] [blame] | 2378 | err = hci_add_remote_oob_data(hdev, &cp->addr.bdaddr, cp->hash, |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 2379 | cp->randomizer); |
| 2380 | if (err < 0) |
Johan Hedberg | bf1e354 | 2012-02-19 13:16:14 +0200 | [diff] [blame] | 2381 | status = MGMT_STATUS_FAILED; |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 2382 | else |
Johan Hedberg | bf1e354 | 2012-02-19 13:16:14 +0200 | [diff] [blame] | 2383 | status = 0; |
| 2384 | |
| 2385 | err = cmd_complete(sk, index, MGMT_OP_ADD_REMOTE_OOB_DATA, status, |
| 2386 | &cp->addr, sizeof(cp->addr)); |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 2387 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2388 | hci_dev_unlock(hdev); |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 2389 | hci_dev_put(hdev); |
| 2390 | |
| 2391 | return err; |
| 2392 | } |
| 2393 | |
| 2394 | static int remove_remote_oob_data(struct sock *sk, u16 index, |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 2395 | void *data, u16 len) |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 2396 | { |
| 2397 | struct hci_dev *hdev; |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 2398 | struct mgmt_cp_remove_remote_oob_data *cp = data; |
Johan Hedberg | bf1e354 | 2012-02-19 13:16:14 +0200 | [diff] [blame] | 2399 | u8 status; |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 2400 | int err; |
| 2401 | |
| 2402 | BT_DBG("hci%u ", index); |
| 2403 | |
| 2404 | if (len != sizeof(*cp)) |
| 2405 | return cmd_status(sk, index, MGMT_OP_REMOVE_REMOTE_OOB_DATA, |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 2406 | MGMT_STATUS_INVALID_PARAMS); |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 2407 | |
| 2408 | hdev = hci_dev_get(index); |
| 2409 | if (!hdev) |
Johan Hedberg | bf1e354 | 2012-02-19 13:16:14 +0200 | [diff] [blame] | 2410 | return cmd_complete(sk, index, MGMT_OP_REMOVE_REMOTE_OOB_DATA, |
| 2411 | MGMT_STATUS_INVALID_PARAMS, |
| 2412 | &cp->addr, sizeof(cp->addr)); |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 2413 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2414 | hci_dev_lock(hdev); |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 2415 | |
Johan Hedberg | 664ce4c | 2012-02-09 15:44:09 +0200 | [diff] [blame] | 2416 | err = hci_remove_remote_oob_data(hdev, &cp->addr.bdaddr); |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 2417 | if (err < 0) |
Johan Hedberg | bf1e354 | 2012-02-19 13:16:14 +0200 | [diff] [blame] | 2418 | status = MGMT_STATUS_INVALID_PARAMS; |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 2419 | else |
Johan Hedberg | bf1e354 | 2012-02-19 13:16:14 +0200 | [diff] [blame] | 2420 | status = 0; |
| 2421 | |
| 2422 | err = cmd_complete(sk, index, MGMT_OP_REMOVE_REMOTE_OOB_DATA, status, |
| 2423 | &cp->addr, sizeof(cp->addr)); |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 2424 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2425 | hci_dev_unlock(hdev); |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 2426 | hci_dev_put(hdev); |
| 2427 | |
| 2428 | return err; |
| 2429 | } |
| 2430 | |
Andre Guedes | 5e0452c | 2012-02-17 20:39:38 -0300 | [diff] [blame] | 2431 | static int discovery(struct hci_dev *hdev) |
| 2432 | { |
| 2433 | int err; |
| 2434 | |
| 2435 | if (lmp_host_le_capable(hdev)) { |
| 2436 | if (lmp_bredr_capable(hdev)) { |
| 2437 | err = hci_le_scan(hdev, LE_SCAN_TYPE, |
| 2438 | LE_SCAN_INT, LE_SCAN_WIN, |
| 2439 | LE_SCAN_TIMEOUT_BREDR_LE); |
| 2440 | } else { |
| 2441 | hdev->discovery.type = DISCOV_TYPE_LE; |
| 2442 | err = hci_le_scan(hdev, LE_SCAN_TYPE, |
| 2443 | LE_SCAN_INT, LE_SCAN_WIN, |
| 2444 | LE_SCAN_TIMEOUT_LE_ONLY); |
| 2445 | } |
| 2446 | } else { |
| 2447 | hdev->discovery.type = DISCOV_TYPE_BREDR; |
| 2448 | err = hci_do_inquiry(hdev, INQUIRY_LEN_BREDR); |
| 2449 | } |
| 2450 | |
| 2451 | return err; |
| 2452 | } |
| 2453 | |
| 2454 | int mgmt_interleaved_discovery(struct hci_dev *hdev) |
| 2455 | { |
| 2456 | int err; |
| 2457 | |
| 2458 | BT_DBG("%s", hdev->name); |
| 2459 | |
| 2460 | hci_dev_lock(hdev); |
| 2461 | |
| 2462 | err = hci_do_inquiry(hdev, INQUIRY_LEN_BREDR_LE); |
| 2463 | if (err < 0) |
| 2464 | hci_discovery_set_state(hdev, DISCOVERY_STOPPED); |
| 2465 | |
| 2466 | hci_dev_unlock(hdev); |
| 2467 | |
| 2468 | return err; |
| 2469 | } |
| 2470 | |
Johan Hedberg | 450dfda | 2011-11-12 11:58:22 +0200 | [diff] [blame] | 2471 | static int start_discovery(struct sock *sk, u16 index, |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 2472 | void *data, u16 len) |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 2473 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 2474 | struct mgmt_cp_start_discovery *cp = data; |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 2475 | struct pending_cmd *cmd; |
| 2476 | struct hci_dev *hdev; |
| 2477 | int err; |
| 2478 | |
| 2479 | BT_DBG("hci%u", index); |
| 2480 | |
Johan Hedberg | 450dfda | 2011-11-12 11:58:22 +0200 | [diff] [blame] | 2481 | if (len != sizeof(*cp)) |
| 2482 | return cmd_status(sk, index, MGMT_OP_START_DISCOVERY, |
| 2483 | MGMT_STATUS_INVALID_PARAMS); |
| 2484 | |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 2485 | hdev = hci_dev_get(index); |
| 2486 | if (!hdev) |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 2487 | return cmd_status(sk, index, MGMT_OP_START_DISCOVERY, |
| 2488 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 2489 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2490 | hci_dev_lock(hdev); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 2491 | |
Johan Hedberg | 4b34ee78 | 2012-02-21 14:13:02 +0200 | [diff] [blame] | 2492 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 2493 | err = cmd_status(sk, index, MGMT_OP_START_DISCOVERY, |
| 2494 | MGMT_STATUS_NOT_POWERED); |
Johan Hedberg | bd2d133 | 2011-11-07 23:13:37 +0200 | [diff] [blame] | 2495 | goto failed; |
| 2496 | } |
| 2497 | |
Johan Hedberg | ff9ef57 | 2012-01-04 14:23:45 +0200 | [diff] [blame] | 2498 | if (hdev->discovery.state != DISCOVERY_STOPPED) { |
| 2499 | err = cmd_status(sk, index, MGMT_OP_START_DISCOVERY, |
| 2500 | MGMT_STATUS_BUSY); |
| 2501 | goto failed; |
| 2502 | } |
| 2503 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 2504 | cmd = mgmt_pending_add(sk, MGMT_OP_START_DISCOVERY, hdev, NULL, 0); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 2505 | if (!cmd) { |
| 2506 | err = -ENOMEM; |
| 2507 | goto failed; |
| 2508 | } |
| 2509 | |
Andre Guedes | 4aab14e | 2012-02-17 20:39:36 -0300 | [diff] [blame] | 2510 | hdev->discovery.type = cp->type; |
| 2511 | |
| 2512 | switch (hdev->discovery.type) { |
Andre Guedes | f39799f | 2012-02-17 20:39:35 -0300 | [diff] [blame] | 2513 | case DISCOV_TYPE_BREDR: |
Andre Guedes | 3fd2415 | 2012-02-03 17:48:01 -0300 | [diff] [blame] | 2514 | err = hci_do_inquiry(hdev, INQUIRY_LEN_BREDR); |
Andre Guedes | f39799f | 2012-02-17 20:39:35 -0300 | [diff] [blame] | 2515 | break; |
| 2516 | |
| 2517 | case DISCOV_TYPE_LE: |
Andre Guedes | 3fd2415 | 2012-02-03 17:48:01 -0300 | [diff] [blame] | 2518 | err = hci_le_scan(hdev, LE_SCAN_TYPE, LE_SCAN_INT, |
| 2519 | LE_SCAN_WIN, LE_SCAN_TIMEOUT_LE_ONLY); |
Andre Guedes | f39799f | 2012-02-17 20:39:35 -0300 | [diff] [blame] | 2520 | break; |
| 2521 | |
Andre Guedes | 5e0452c | 2012-02-17 20:39:38 -0300 | [diff] [blame] | 2522 | case DISCOV_TYPE_INTERLEAVED: |
| 2523 | err = discovery(hdev); |
| 2524 | break; |
| 2525 | |
Andre Guedes | f39799f | 2012-02-17 20:39:35 -0300 | [diff] [blame] | 2526 | default: |
Andre Guedes | 3fd2415 | 2012-02-03 17:48:01 -0300 | [diff] [blame] | 2527 | err = -EINVAL; |
Andre Guedes | f39799f | 2012-02-17 20:39:35 -0300 | [diff] [blame] | 2528 | } |
Andre Guedes | 3fd2415 | 2012-02-03 17:48:01 -0300 | [diff] [blame] | 2529 | |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 2530 | if (err < 0) |
| 2531 | mgmt_pending_remove(cmd); |
Johan Hedberg | ff9ef57 | 2012-01-04 14:23:45 +0200 | [diff] [blame] | 2532 | else |
| 2533 | hci_discovery_set_state(hdev, DISCOVERY_STARTING); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 2534 | |
| 2535 | failed: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2536 | hci_dev_unlock(hdev); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 2537 | hci_dev_put(hdev); |
| 2538 | |
| 2539 | return err; |
| 2540 | } |
| 2541 | |
Johan Hedberg | d930650 | 2012-02-20 23:25:18 +0200 | [diff] [blame] | 2542 | 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] | 2543 | { |
Johan Hedberg | d930650 | 2012-02-20 23:25:18 +0200 | [diff] [blame] | 2544 | struct mgmt_cp_stop_discovery *mgmt_cp = data; |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 2545 | struct hci_dev *hdev; |
| 2546 | struct pending_cmd *cmd; |
Johan Hedberg | 30dc78e | 2012-01-04 15:44:20 +0200 | [diff] [blame] | 2547 | struct hci_cp_remote_name_req_cancel cp; |
| 2548 | struct inquiry_entry *e; |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 2549 | int err; |
| 2550 | |
| 2551 | BT_DBG("hci%u", index); |
| 2552 | |
Johan Hedberg | d930650 | 2012-02-20 23:25:18 +0200 | [diff] [blame] | 2553 | if (len != sizeof(*mgmt_cp)) |
| 2554 | return cmd_status(sk, index, MGMT_OP_STOP_DISCOVERY, |
| 2555 | MGMT_STATUS_INVALID_PARAMS); |
| 2556 | |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 2557 | hdev = hci_dev_get(index); |
| 2558 | if (!hdev) |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 2559 | return cmd_status(sk, index, MGMT_OP_STOP_DISCOVERY, |
| 2560 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 2561 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2562 | hci_dev_lock(hdev); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 2563 | |
Johan Hedberg | 30dc78e | 2012-01-04 15:44:20 +0200 | [diff] [blame] | 2564 | if (!hci_discovery_active(hdev)) { |
Johan Hedberg | d930650 | 2012-02-20 23:25:18 +0200 | [diff] [blame] | 2565 | err = cmd_complete(sk, index, MGMT_OP_STOP_DISCOVERY, |
| 2566 | MGMT_STATUS_REJECTED, |
| 2567 | &mgmt_cp->type, sizeof(mgmt_cp->type)); |
| 2568 | goto unlock; |
| 2569 | } |
| 2570 | |
| 2571 | if (hdev->discovery.type != mgmt_cp->type) { |
| 2572 | err = cmd_complete(sk, index, MGMT_OP_STOP_DISCOVERY, |
| 2573 | MGMT_STATUS_INVALID_PARAMS, |
| 2574 | &mgmt_cp->type, sizeof(mgmt_cp->type)); |
Johan Hedberg | 30dc78e | 2012-01-04 15:44:20 +0200 | [diff] [blame] | 2575 | goto unlock; |
Johan Hedberg | ff9ef57 | 2012-01-04 14:23:45 +0200 | [diff] [blame] | 2576 | } |
| 2577 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 2578 | cmd = mgmt_pending_add(sk, MGMT_OP_STOP_DISCOVERY, hdev, NULL, 0); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 2579 | if (!cmd) { |
| 2580 | err = -ENOMEM; |
Johan Hedberg | 30dc78e | 2012-01-04 15:44:20 +0200 | [diff] [blame] | 2581 | goto unlock; |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 2582 | } |
| 2583 | |
Andre Guedes | 343f935 | 2012-02-17 20:39:37 -0300 | [diff] [blame] | 2584 | if (hdev->discovery.state == DISCOVERY_FINDING) { |
Johan Hedberg | 30dc78e | 2012-01-04 15:44:20 +0200 | [diff] [blame] | 2585 | err = hci_cancel_inquiry(hdev); |
| 2586 | if (err < 0) |
| 2587 | mgmt_pending_remove(cmd); |
| 2588 | else |
| 2589 | hci_discovery_set_state(hdev, DISCOVERY_STOPPING); |
| 2590 | goto unlock; |
| 2591 | } |
| 2592 | |
| 2593 | e = hci_inquiry_cache_lookup_resolve(hdev, BDADDR_ANY, NAME_PENDING); |
| 2594 | if (!e) { |
| 2595 | mgmt_pending_remove(cmd); |
Johan Hedberg | aee9b21 | 2012-02-18 15:07:59 +0200 | [diff] [blame] | 2596 | err = cmd_complete(sk, index, MGMT_OP_STOP_DISCOVERY, 0, |
Johan Hedberg | d930650 | 2012-02-20 23:25:18 +0200 | [diff] [blame] | 2597 | &mgmt_cp->type, sizeof(mgmt_cp->type)); |
Johan Hedberg | 30dc78e | 2012-01-04 15:44:20 +0200 | [diff] [blame] | 2598 | hci_discovery_set_state(hdev, DISCOVERY_STOPPED); |
| 2599 | goto unlock; |
| 2600 | } |
| 2601 | |
| 2602 | bacpy(&cp.bdaddr, &e->data.bdaddr); |
| 2603 | err = hci_send_cmd(hdev, HCI_OP_REMOTE_NAME_REQ_CANCEL, |
| 2604 | sizeof(cp), &cp); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 2605 | if (err < 0) |
| 2606 | mgmt_pending_remove(cmd); |
Johan Hedberg | ff9ef57 | 2012-01-04 14:23:45 +0200 | [diff] [blame] | 2607 | else |
| 2608 | hci_discovery_set_state(hdev, DISCOVERY_STOPPING); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 2609 | |
Johan Hedberg | 30dc78e | 2012-01-04 15:44:20 +0200 | [diff] [blame] | 2610 | unlock: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2611 | hci_dev_unlock(hdev); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 2612 | hci_dev_put(hdev); |
| 2613 | |
| 2614 | return err; |
| 2615 | } |
| 2616 | |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 2617 | 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] | 2618 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 2619 | struct mgmt_cp_confirm_name *cp = data; |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 2620 | struct inquiry_entry *e; |
| 2621 | struct hci_dev *hdev; |
| 2622 | int err; |
| 2623 | |
| 2624 | BT_DBG("hci%u", index); |
| 2625 | |
| 2626 | if (len != sizeof(*cp)) |
| 2627 | return cmd_status(sk, index, MGMT_OP_CONFIRM_NAME, |
| 2628 | MGMT_STATUS_INVALID_PARAMS); |
| 2629 | |
| 2630 | hdev = hci_dev_get(index); |
| 2631 | if (!hdev) |
| 2632 | return cmd_status(sk, index, MGMT_OP_CONFIRM_NAME, |
| 2633 | MGMT_STATUS_INVALID_PARAMS); |
| 2634 | |
| 2635 | hci_dev_lock(hdev); |
| 2636 | |
Johan Hedberg | 30dc78e | 2012-01-04 15:44:20 +0200 | [diff] [blame] | 2637 | if (!hci_discovery_active(hdev)) { |
| 2638 | err = cmd_status(sk, index, MGMT_OP_CONFIRM_NAME, |
| 2639 | MGMT_STATUS_FAILED); |
| 2640 | goto failed; |
| 2641 | } |
| 2642 | |
Johan Hedberg | a198e7b | 2012-02-17 14:27:06 +0200 | [diff] [blame] | 2643 | e = hci_inquiry_cache_lookup_unknown(hdev, &cp->addr.bdaddr); |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 2644 | if (!e) { |
Marcel Holtmann | e5f0e15 | 2012-02-22 11:59:01 +0100 | [diff] [blame] | 2645 | err = cmd_status(sk, index, MGMT_OP_CONFIRM_NAME, |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 2646 | MGMT_STATUS_INVALID_PARAMS); |
| 2647 | goto failed; |
| 2648 | } |
| 2649 | |
| 2650 | if (cp->name_known) { |
| 2651 | e->name_state = NAME_KNOWN; |
| 2652 | list_del(&e->list); |
| 2653 | } else { |
| 2654 | e->name_state = NAME_NEEDED; |
Johan Hedberg | a3d4e20a | 2012-01-09 00:53:02 +0200 | [diff] [blame] | 2655 | hci_inquiry_cache_update_resolve(hdev, e); |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 2656 | } |
| 2657 | |
| 2658 | err = 0; |
| 2659 | |
| 2660 | failed: |
| 2661 | hci_dev_unlock(hdev); |
| 2662 | |
| 2663 | return err; |
| 2664 | } |
| 2665 | |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 2666 | 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] | 2667 | { |
| 2668 | struct hci_dev *hdev; |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 2669 | struct mgmt_cp_block_device *cp = data; |
Johan Hedberg | f0eeea8 | 2012-02-19 12:58:54 +0200 | [diff] [blame] | 2670 | u8 status; |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 2671 | int err; |
| 2672 | |
| 2673 | BT_DBG("hci%u", index); |
| 2674 | |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 2675 | if (len != sizeof(*cp)) |
| 2676 | return cmd_status(sk, index, MGMT_OP_BLOCK_DEVICE, |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 2677 | MGMT_STATUS_INVALID_PARAMS); |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 2678 | |
| 2679 | hdev = hci_dev_get(index); |
| 2680 | if (!hdev) |
Johan Hedberg | f0eeea8 | 2012-02-19 12:58:54 +0200 | [diff] [blame] | 2681 | return cmd_complete(sk, index, MGMT_OP_BLOCK_DEVICE, |
| 2682 | MGMT_STATUS_INVALID_PARAMS, |
| 2683 | &cp->addr, sizeof(cp->addr)); |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 2684 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2685 | hci_dev_lock(hdev); |
Antti Julku | 5e76244 | 2011-08-25 16:48:02 +0300 | [diff] [blame] | 2686 | |
Johan Hedberg | 88c1fe4 | 2012-02-09 15:56:11 +0200 | [diff] [blame] | 2687 | err = hci_blacklist_add(hdev, &cp->addr.bdaddr, cp->addr.type); |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 2688 | if (err < 0) |
Johan Hedberg | f0eeea8 | 2012-02-19 12:58:54 +0200 | [diff] [blame] | 2689 | status = MGMT_STATUS_FAILED; |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 2690 | else |
Johan Hedberg | f0eeea8 | 2012-02-19 12:58:54 +0200 | [diff] [blame] | 2691 | status = 0; |
| 2692 | |
| 2693 | err = cmd_complete(sk, index, MGMT_OP_BLOCK_DEVICE, status, |
| 2694 | &cp->addr, sizeof(cp->addr)); |
Antti Julku | 5e76244 | 2011-08-25 16:48:02 +0300 | [diff] [blame] | 2695 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2696 | hci_dev_unlock(hdev); |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 2697 | hci_dev_put(hdev); |
| 2698 | |
| 2699 | return err; |
| 2700 | } |
| 2701 | |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 2702 | 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] | 2703 | { |
| 2704 | struct hci_dev *hdev; |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 2705 | struct mgmt_cp_unblock_device *cp = data; |
Johan Hedberg | f0eeea8 | 2012-02-19 12:58:54 +0200 | [diff] [blame] | 2706 | u8 status; |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 2707 | int err; |
| 2708 | |
| 2709 | BT_DBG("hci%u", index); |
| 2710 | |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 2711 | if (len != sizeof(*cp)) |
| 2712 | return cmd_status(sk, index, MGMT_OP_UNBLOCK_DEVICE, |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 2713 | MGMT_STATUS_INVALID_PARAMS); |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 2714 | |
| 2715 | hdev = hci_dev_get(index); |
| 2716 | if (!hdev) |
Johan Hedberg | f0eeea8 | 2012-02-19 12:58:54 +0200 | [diff] [blame] | 2717 | return cmd_complete(sk, index, MGMT_OP_UNBLOCK_DEVICE, |
| 2718 | MGMT_STATUS_INVALID_PARAMS, |
| 2719 | &cp->addr, sizeof(cp->addr)); |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 2720 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2721 | hci_dev_lock(hdev); |
Antti Julku | 5e76244 | 2011-08-25 16:48:02 +0300 | [diff] [blame] | 2722 | |
Johan Hedberg | 88c1fe4 | 2012-02-09 15:56:11 +0200 | [diff] [blame] | 2723 | err = hci_blacklist_del(hdev, &cp->addr.bdaddr, cp->addr.type); |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 2724 | if (err < 0) |
Johan Hedberg | f0eeea8 | 2012-02-19 12:58:54 +0200 | [diff] [blame] | 2725 | status = MGMT_STATUS_INVALID_PARAMS; |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 2726 | else |
Johan Hedberg | f0eeea8 | 2012-02-19 12:58:54 +0200 | [diff] [blame] | 2727 | status = 0; |
| 2728 | |
| 2729 | err = cmd_complete(sk, index, MGMT_OP_UNBLOCK_DEVICE, status, |
| 2730 | &cp->addr, sizeof(cp->addr)); |
Antti Julku | 5e76244 | 2011-08-25 16:48:02 +0300 | [diff] [blame] | 2731 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2732 | hci_dev_unlock(hdev); |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 2733 | hci_dev_put(hdev); |
| 2734 | |
| 2735 | return err; |
| 2736 | } |
| 2737 | |
Antti Julku | f6422ec | 2011-06-22 13:11:56 +0300 | [diff] [blame] | 2738 | static int set_fast_connectable(struct sock *sk, u16 index, |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 2739 | void *data, u16 len) |
Antti Julku | f6422ec | 2011-06-22 13:11:56 +0300 | [diff] [blame] | 2740 | { |
| 2741 | struct hci_dev *hdev; |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 2742 | struct mgmt_mode *cp = data; |
Antti Julku | f6422ec | 2011-06-22 13:11:56 +0300 | [diff] [blame] | 2743 | struct hci_cp_write_page_scan_activity acp; |
| 2744 | u8 type; |
| 2745 | int err; |
| 2746 | |
| 2747 | BT_DBG("hci%u", index); |
| 2748 | |
| 2749 | if (len != sizeof(*cp)) |
| 2750 | return cmd_status(sk, index, MGMT_OP_SET_FAST_CONNECTABLE, |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 2751 | MGMT_STATUS_INVALID_PARAMS); |
Antti Julku | f6422ec | 2011-06-22 13:11:56 +0300 | [diff] [blame] | 2752 | |
| 2753 | hdev = hci_dev_get(index); |
| 2754 | if (!hdev) |
| 2755 | return cmd_status(sk, index, MGMT_OP_SET_FAST_CONNECTABLE, |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 2756 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | 5400c04 | 2012-02-21 16:40:33 +0200 | [diff] [blame] | 2757 | if (!hdev_is_powered(hdev)) |
| 2758 | return cmd_status(sk, index, MGMT_OP_SET_FAST_CONNECTABLE, |
| 2759 | MGMT_STATUS_NOT_POWERED); |
| 2760 | |
| 2761 | if (!test_bit(HCI_CONNECTABLE, &hdev->dev_flags)) |
| 2762 | return cmd_status(sk, index, MGMT_OP_SET_FAST_CONNECTABLE, |
| 2763 | MGMT_STATUS_REJECTED); |
Antti Julku | f6422ec | 2011-06-22 13:11:56 +0300 | [diff] [blame] | 2764 | |
| 2765 | hci_dev_lock(hdev); |
| 2766 | |
Johan Hedberg | f7c6869ce | 2011-12-15 00:47:36 +0200 | [diff] [blame] | 2767 | if (cp->val) { |
Antti Julku | f6422ec | 2011-06-22 13:11:56 +0300 | [diff] [blame] | 2768 | type = PAGE_SCAN_TYPE_INTERLACED; |
| 2769 | acp.interval = 0x0024; /* 22.5 msec page scan interval */ |
| 2770 | } else { |
| 2771 | type = PAGE_SCAN_TYPE_STANDARD; /* default */ |
| 2772 | acp.interval = 0x0800; /* default 1.28 sec page scan */ |
| 2773 | } |
| 2774 | |
| 2775 | acp.window = 0x0012; /* default 11.25 msec page scan window */ |
| 2776 | |
| 2777 | err = hci_send_cmd(hdev, HCI_OP_WRITE_PAGE_SCAN_ACTIVITY, |
| 2778 | sizeof(acp), &acp); |
| 2779 | if (err < 0) { |
| 2780 | err = cmd_status(sk, index, MGMT_OP_SET_FAST_CONNECTABLE, |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 2781 | MGMT_STATUS_FAILED); |
Antti Julku | f6422ec | 2011-06-22 13:11:56 +0300 | [diff] [blame] | 2782 | goto done; |
| 2783 | } |
| 2784 | |
| 2785 | err = hci_send_cmd(hdev, HCI_OP_WRITE_PAGE_SCAN_TYPE, 1, &type); |
| 2786 | if (err < 0) { |
| 2787 | err = cmd_status(sk, index, MGMT_OP_SET_FAST_CONNECTABLE, |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 2788 | MGMT_STATUS_FAILED); |
Antti Julku | f6422ec | 2011-06-22 13:11:56 +0300 | [diff] [blame] | 2789 | goto done; |
| 2790 | } |
| 2791 | |
Johan Hedberg | aee9b21 | 2012-02-18 15:07:59 +0200 | [diff] [blame] | 2792 | err = cmd_complete(sk, index, MGMT_OP_SET_FAST_CONNECTABLE, 0, |
| 2793 | NULL, 0); |
Antti Julku | f6422ec | 2011-06-22 13:11:56 +0300 | [diff] [blame] | 2794 | done: |
| 2795 | hci_dev_unlock(hdev); |
| 2796 | hci_dev_put(hdev); |
| 2797 | |
| 2798 | return err; |
| 2799 | } |
| 2800 | |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 2801 | static int load_long_term_keys(struct sock *sk, u16 index, |
| 2802 | void *cp_data, u16 len) |
| 2803 | { |
| 2804 | struct hci_dev *hdev; |
| 2805 | struct mgmt_cp_load_long_term_keys *cp = cp_data; |
| 2806 | u16 key_count, expected_len; |
| 2807 | int i; |
| 2808 | |
| 2809 | if (len < sizeof(*cp)) |
| 2810 | return cmd_status(sk, index, MGMT_OP_LOAD_LONG_TERM_KEYS, |
| 2811 | EINVAL); |
| 2812 | |
| 2813 | key_count = get_unaligned_le16(&cp->key_count); |
| 2814 | |
| 2815 | expected_len = sizeof(*cp) + key_count * |
| 2816 | sizeof(struct mgmt_ltk_info); |
| 2817 | if (expected_len != len) { |
| 2818 | BT_ERR("load_keys: expected %u bytes, got %u bytes", |
| 2819 | len, expected_len); |
| 2820 | return cmd_status(sk, index, MGMT_OP_LOAD_LONG_TERM_KEYS, |
| 2821 | EINVAL); |
| 2822 | } |
| 2823 | |
| 2824 | hdev = hci_dev_get(index); |
| 2825 | if (!hdev) |
| 2826 | return cmd_status(sk, index, MGMT_OP_LOAD_LONG_TERM_KEYS, |
| 2827 | ENODEV); |
| 2828 | |
| 2829 | BT_DBG("hci%u key_count %u", index, key_count); |
| 2830 | |
| 2831 | hci_dev_lock(hdev); |
| 2832 | |
| 2833 | hci_smp_ltks_clear(hdev); |
| 2834 | |
| 2835 | for (i = 0; i < key_count; i++) { |
| 2836 | struct mgmt_ltk_info *key = &cp->keys[i]; |
| 2837 | u8 type; |
| 2838 | |
| 2839 | if (key->master) |
| 2840 | type = HCI_SMP_LTK; |
| 2841 | else |
| 2842 | type = HCI_SMP_LTK_SLAVE; |
| 2843 | |
| 2844 | hci_add_ltk(hdev, &key->addr.bdaddr, key->addr.type, |
| 2845 | type, 0, key->authenticated, key->val, |
| 2846 | key->enc_size, key->ediv, key->rand); |
| 2847 | } |
| 2848 | |
| 2849 | hci_dev_unlock(hdev); |
| 2850 | hci_dev_put(hdev); |
| 2851 | |
| 2852 | return 0; |
| 2853 | } |
| 2854 | |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 2855 | int mgmt_control(struct sock *sk, struct msghdr *msg, size_t msglen) |
| 2856 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 2857 | void *buf; |
| 2858 | u8 *cp; |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 2859 | struct mgmt_hdr *hdr; |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 2860 | u16 opcode, index, len; |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 2861 | int err; |
| 2862 | |
| 2863 | BT_DBG("got %zu bytes", msglen); |
| 2864 | |
| 2865 | if (msglen < sizeof(*hdr)) |
| 2866 | return -EINVAL; |
| 2867 | |
Gustavo F. Padovan | e63a15e | 2011-04-04 18:56:53 -0300 | [diff] [blame] | 2868 | buf = kmalloc(msglen, GFP_KERNEL); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 2869 | if (!buf) |
| 2870 | return -ENOMEM; |
| 2871 | |
| 2872 | if (memcpy_fromiovec(buf, msg->msg_iov, msglen)) { |
| 2873 | err = -EFAULT; |
| 2874 | goto done; |
| 2875 | } |
| 2876 | |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 2877 | hdr = buf; |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 2878 | opcode = get_unaligned_le16(&hdr->opcode); |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 2879 | index = get_unaligned_le16(&hdr->index); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 2880 | len = get_unaligned_le16(&hdr->len); |
| 2881 | |
| 2882 | if (len != msglen - sizeof(*hdr)) { |
| 2883 | err = -EINVAL; |
| 2884 | goto done; |
| 2885 | } |
| 2886 | |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 2887 | cp = buf + sizeof(*hdr); |
| 2888 | |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 2889 | switch (opcode) { |
Johan Hedberg | 02d9812 | 2010-12-13 21:07:04 +0200 | [diff] [blame] | 2890 | case MGMT_OP_READ_VERSION: |
| 2891 | err = read_version(sk); |
| 2892 | break; |
Johan Hedberg | e70bb2e | 2012-02-13 16:59:33 +0200 | [diff] [blame] | 2893 | case MGMT_OP_READ_COMMANDS: |
| 2894 | err = read_commands(sk); |
| 2895 | break; |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 2896 | case MGMT_OP_READ_INDEX_LIST: |
| 2897 | err = read_index_list(sk); |
| 2898 | break; |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 2899 | case MGMT_OP_READ_INFO: |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 2900 | err = read_controller_info(sk, index); |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 2901 | break; |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 2902 | case MGMT_OP_SET_POWERED: |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 2903 | err = set_powered(sk, index, cp, len); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 2904 | break; |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 2905 | case MGMT_OP_SET_DISCOVERABLE: |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 2906 | err = set_discoverable(sk, index, cp, len); |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 2907 | break; |
Johan Hedberg | 9fbcbb4 | 2010-12-30 00:18:33 +0200 | [diff] [blame] | 2908 | case MGMT_OP_SET_CONNECTABLE: |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 2909 | err = set_connectable(sk, index, cp, len); |
Johan Hedberg | 9fbcbb4 | 2010-12-30 00:18:33 +0200 | [diff] [blame] | 2910 | break; |
Johan Hedberg | f7c6869ce | 2011-12-15 00:47:36 +0200 | [diff] [blame] | 2911 | case MGMT_OP_SET_FAST_CONNECTABLE: |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 2912 | err = set_fast_connectable(sk, index, cp, len); |
Johan Hedberg | f7c6869ce | 2011-12-15 00:47:36 +0200 | [diff] [blame] | 2913 | break; |
Johan Hedberg | c542a06 | 2011-01-26 13:11:03 +0200 | [diff] [blame] | 2914 | case MGMT_OP_SET_PAIRABLE: |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 2915 | err = set_pairable(sk, index, cp, len); |
Johan Hedberg | c542a06 | 2011-01-26 13:11:03 +0200 | [diff] [blame] | 2916 | break; |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 2917 | case MGMT_OP_SET_LINK_SECURITY: |
| 2918 | err = set_link_security(sk, index, cp, len); |
| 2919 | break; |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 2920 | case MGMT_OP_SET_SSP: |
| 2921 | err = set_ssp(sk, index, cp, len); |
| 2922 | break; |
Johan Hedberg | 6d80dfd | 2012-02-20 23:50:38 +0200 | [diff] [blame] | 2923 | case MGMT_OP_SET_HS: |
| 2924 | err = set_hs(sk, index, cp, len); |
| 2925 | break; |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 2926 | case MGMT_OP_SET_LE: |
| 2927 | err = set_le(sk, index, cp, len); |
| 2928 | break; |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2929 | case MGMT_OP_ADD_UUID: |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 2930 | err = add_uuid(sk, index, cp, len); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2931 | break; |
| 2932 | case MGMT_OP_REMOVE_UUID: |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 2933 | err = remove_uuid(sk, index, cp, len); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2934 | break; |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2935 | case MGMT_OP_SET_DEV_CLASS: |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 2936 | err = set_dev_class(sk, index, cp, len); |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2937 | break; |
Johan Hedberg | 86742e1 | 2011-11-07 23:13:38 +0200 | [diff] [blame] | 2938 | case MGMT_OP_LOAD_LINK_KEYS: |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 2939 | err = load_link_keys(sk, index, cp, len); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2940 | break; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2941 | case MGMT_OP_DISCONNECT: |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 2942 | err = disconnect(sk, index, cp, len); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2943 | break; |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 2944 | case MGMT_OP_GET_CONNECTIONS: |
Szymon Janc | 8ce6284 | 2011-03-01 16:55:32 +0100 | [diff] [blame] | 2945 | err = get_connections(sk, index); |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 2946 | break; |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2947 | case MGMT_OP_PIN_CODE_REPLY: |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 2948 | err = pin_code_reply(sk, index, cp, len); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2949 | break; |
| 2950 | case MGMT_OP_PIN_CODE_NEG_REPLY: |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 2951 | err = pin_code_neg_reply(sk, index, cp, len); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2952 | break; |
Johan Hedberg | 17fa4b9 | 2011-01-25 13:28:33 +0200 | [diff] [blame] | 2953 | case MGMT_OP_SET_IO_CAPABILITY: |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 2954 | err = set_io_capability(sk, index, cp, len); |
Johan Hedberg | 17fa4b9 | 2011-01-25 13:28:33 +0200 | [diff] [blame] | 2955 | break; |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2956 | case MGMT_OP_PAIR_DEVICE: |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 2957 | err = pair_device(sk, index, cp, len); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2958 | break; |
Johan Hedberg | 2842470 | 2012-02-02 04:02:29 +0200 | [diff] [blame] | 2959 | case MGMT_OP_CANCEL_PAIR_DEVICE: |
| 2960 | err = cancel_pair_device(sk, index, buf + sizeof(*hdr), len); |
| 2961 | break; |
Johan Hedberg | 124f6e3 | 2012-02-09 13:50:12 +0200 | [diff] [blame] | 2962 | case MGMT_OP_UNPAIR_DEVICE: |
| 2963 | err = unpair_device(sk, index, cp, len); |
| 2964 | break; |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 2965 | case MGMT_OP_USER_CONFIRM_REPLY: |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 2966 | err = user_confirm_reply(sk, index, cp, len); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 2967 | break; |
| 2968 | case MGMT_OP_USER_CONFIRM_NEG_REPLY: |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 2969 | err = user_confirm_neg_reply(sk, index, cp, len); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 2970 | break; |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 2971 | case MGMT_OP_USER_PASSKEY_REPLY: |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 2972 | err = user_passkey_reply(sk, index, cp, len); |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 2973 | break; |
| 2974 | case MGMT_OP_USER_PASSKEY_NEG_REPLY: |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 2975 | err = user_passkey_neg_reply(sk, index, cp, len); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2976 | break; |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 2977 | case MGMT_OP_SET_LOCAL_NAME: |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 2978 | err = set_local_name(sk, index, cp, len); |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 2979 | break; |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 2980 | case MGMT_OP_READ_LOCAL_OOB_DATA: |
| 2981 | err = read_local_oob_data(sk, index); |
| 2982 | break; |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 2983 | case MGMT_OP_ADD_REMOTE_OOB_DATA: |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 2984 | err = add_remote_oob_data(sk, index, cp, len); |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 2985 | break; |
| 2986 | case MGMT_OP_REMOVE_REMOTE_OOB_DATA: |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 2987 | err = remove_remote_oob_data(sk, index, cp, len); |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 2988 | break; |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 2989 | case MGMT_OP_START_DISCOVERY: |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 2990 | err = start_discovery(sk, index, cp, len); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 2991 | break; |
| 2992 | case MGMT_OP_STOP_DISCOVERY: |
Johan Hedberg | d930650 | 2012-02-20 23:25:18 +0200 | [diff] [blame] | 2993 | err = stop_discovery(sk, index, cp, len); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 2994 | break; |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 2995 | case MGMT_OP_CONFIRM_NAME: |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 2996 | err = confirm_name(sk, index, cp, len); |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 2997 | break; |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 2998 | case MGMT_OP_BLOCK_DEVICE: |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 2999 | err = block_device(sk, index, cp, len); |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 3000 | break; |
| 3001 | case MGMT_OP_UNBLOCK_DEVICE: |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 3002 | err = unblock_device(sk, index, cp, len); |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 3003 | break; |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 3004 | case MGMT_OP_LOAD_LONG_TERM_KEYS: |
| 3005 | err = load_long_term_keys(sk, index, cp, len); |
| 3006 | break; |
Johan Hedberg | 9fbcbb4 | 2010-12-30 00:18:33 +0200 | [diff] [blame] | 3007 | default: |
Johan Hedberg | 72a734e | 2010-12-30 00:38:22 +0200 | [diff] [blame] | 3008 | BT_DBG("Unknown op %u", opcode); |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 3009 | err = cmd_status(sk, index, opcode, |
| 3010 | MGMT_STATUS_UNKNOWN_COMMAND); |
Johan Hedberg | 9fbcbb4 | 2010-12-30 00:18:33 +0200 | [diff] [blame] | 3011 | break; |
| 3012 | } |
Johan Hedberg | 72a734e | 2010-12-30 00:38:22 +0200 | [diff] [blame] | 3013 | |
Johan Hedberg | 9fbcbb4 | 2010-12-30 00:18:33 +0200 | [diff] [blame] | 3014 | if (err < 0) |
Johan Hedberg | 72a734e | 2010-12-30 00:38:22 +0200 | [diff] [blame] | 3015 | goto done; |
| 3016 | |
Johan Hedberg | 9fbcbb4 | 2010-12-30 00:18:33 +0200 | [diff] [blame] | 3017 | err = msglen; |
| 3018 | |
| 3019 | done: |
| 3020 | kfree(buf); |
| 3021 | return err; |
| 3022 | } |
| 3023 | |
Johan Hedberg | b24752f | 2011-11-03 14:40:33 +0200 | [diff] [blame] | 3024 | static void cmd_status_rsp(struct pending_cmd *cmd, void *data) |
| 3025 | { |
| 3026 | u8 *status = data; |
| 3027 | |
| 3028 | cmd_status(cmd->sk, cmd->index, cmd->opcode, *status); |
| 3029 | mgmt_pending_remove(cmd); |
| 3030 | } |
| 3031 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 3032 | int mgmt_index_added(struct hci_dev *hdev) |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 3033 | { |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 3034 | return mgmt_event(MGMT_EV_INDEX_ADDED, hdev, NULL, 0, NULL); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 3035 | } |
| 3036 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 3037 | int mgmt_index_removed(struct hci_dev *hdev) |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 3038 | { |
Johan Hedberg | b24752f | 2011-11-03 14:40:33 +0200 | [diff] [blame] | 3039 | u8 status = ENODEV; |
| 3040 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 3041 | mgmt_pending_foreach(0, hdev, cmd_status_rsp, &status); |
Johan Hedberg | b24752f | 2011-11-03 14:40:33 +0200 | [diff] [blame] | 3042 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 3043 | return mgmt_event(MGMT_EV_INDEX_REMOVED, hdev, NULL, 0, NULL); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 3044 | } |
| 3045 | |
| 3046 | struct cmd_lookup { |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 3047 | struct sock *sk; |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 3048 | struct hci_dev *hdev; |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 3049 | }; |
| 3050 | |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 3051 | static void settings_rsp(struct pending_cmd *cmd, void *data) |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 3052 | { |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 3053 | struct cmd_lookup *match = data; |
| 3054 | |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 3055 | send_settings_rsp(cmd->sk, cmd->opcode, match->hdev); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 3056 | |
| 3057 | list_del(&cmd->list); |
| 3058 | |
| 3059 | if (match->sk == NULL) { |
| 3060 | match->sk = cmd->sk; |
| 3061 | sock_hold(match->sk); |
| 3062 | } |
| 3063 | |
| 3064 | mgmt_pending_free(cmd); |
| 3065 | } |
| 3066 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 3067 | int mgmt_powered(struct hci_dev *hdev, u8 powered) |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 3068 | { |
Johan Hedberg | 76a7f3a | 2012-02-17 00:34:40 +0200 | [diff] [blame] | 3069 | struct cmd_lookup match = { NULL, hdev }; |
Johan Hedberg | 7bb895d | 2012-02-17 01:20:00 +0200 | [diff] [blame] | 3070 | int err; |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 3071 | |
Johan Hedberg | 5e5282b | 2012-02-21 16:01:30 +0200 | [diff] [blame] | 3072 | if (!test_bit(HCI_MGMT, &hdev->dev_flags)) |
| 3073 | return 0; |
| 3074 | |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 3075 | mgmt_pending_foreach(MGMT_OP_SET_POWERED, hdev, settings_rsp, &match); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 3076 | |
Johan Hedberg | 5e5282b | 2012-02-21 16:01:30 +0200 | [diff] [blame] | 3077 | if (powered) { |
| 3078 | u8 scan = 0; |
| 3079 | |
| 3080 | if (test_bit(HCI_CONNECTABLE, &hdev->dev_flags)) |
| 3081 | scan |= SCAN_PAGE; |
| 3082 | if (test_bit(HCI_DISCOVERABLE, &hdev->dev_flags)) |
| 3083 | scan |= SCAN_INQUIRY; |
| 3084 | |
| 3085 | if (scan) |
| 3086 | hci_send_cmd(hdev, HCI_OP_WRITE_SCAN_ENABLE, 1, &scan); |
| 3087 | } else { |
Johan Hedberg | b24752f | 2011-11-03 14:40:33 +0200 | [diff] [blame] | 3088 | u8 status = ENETDOWN; |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 3089 | mgmt_pending_foreach(0, hdev, cmd_status_rsp, &status); |
Johan Hedberg | b24752f | 2011-11-03 14:40:33 +0200 | [diff] [blame] | 3090 | } |
| 3091 | |
Johan Hedberg | beadb2b | 2012-02-21 16:55:31 +0200 | [diff] [blame] | 3092 | err = new_settings(hdev, match.sk); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 3093 | |
| 3094 | if (match.sk) |
| 3095 | sock_put(match.sk); |
| 3096 | |
Johan Hedberg | 7bb895d | 2012-02-17 01:20:00 +0200 | [diff] [blame] | 3097 | return err; |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 3098 | } |
| 3099 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 3100 | int mgmt_discoverable(struct hci_dev *hdev, u8 discoverable) |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 3101 | { |
Johan Hedberg | 76a7f3a | 2012-02-17 00:34:40 +0200 | [diff] [blame] | 3102 | struct cmd_lookup match = { NULL, hdev }; |
Johan Hedberg | 5e5282b | 2012-02-21 16:01:30 +0200 | [diff] [blame] | 3103 | bool changed = false; |
| 3104 | int err = 0; |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 3105 | |
Johan Hedberg | 5e5282b | 2012-02-21 16:01:30 +0200 | [diff] [blame] | 3106 | if (discoverable) { |
| 3107 | if (!test_and_set_bit(HCI_DISCOVERABLE, &hdev->dev_flags)) |
| 3108 | changed = true; |
| 3109 | } else { |
| 3110 | if (test_and_clear_bit(HCI_DISCOVERABLE, &hdev->dev_flags)) |
| 3111 | changed = true; |
| 3112 | } |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 3113 | |
Johan Hedberg | ed9b5f2 | 2012-02-21 20:47:06 +0200 | [diff] [blame] | 3114 | mgmt_pending_foreach(MGMT_OP_SET_DISCOVERABLE, hdev, settings_rsp, |
| 3115 | &match); |
| 3116 | |
Johan Hedberg | beadb2b | 2012-02-21 16:55:31 +0200 | [diff] [blame] | 3117 | if (changed) |
| 3118 | err = new_settings(hdev, match.sk); |
Johan Hedberg | 5e5282b | 2012-02-21 16:01:30 +0200 | [diff] [blame] | 3119 | |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 3120 | if (match.sk) |
| 3121 | sock_put(match.sk); |
| 3122 | |
Johan Hedberg | 7bb895d | 2012-02-17 01:20:00 +0200 | [diff] [blame] | 3123 | return err; |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 3124 | } |
| 3125 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 3126 | int mgmt_connectable(struct hci_dev *hdev, u8 connectable) |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 3127 | { |
Johan Hedberg | 76a7f3a | 2012-02-17 00:34:40 +0200 | [diff] [blame] | 3128 | struct cmd_lookup match = { NULL, hdev }; |
Johan Hedberg | 5e5282b | 2012-02-21 16:01:30 +0200 | [diff] [blame] | 3129 | bool changed = false; |
| 3130 | int err = 0; |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 3131 | |
Johan Hedberg | 5e5282b | 2012-02-21 16:01:30 +0200 | [diff] [blame] | 3132 | if (connectable) { |
| 3133 | if (!test_and_set_bit(HCI_CONNECTABLE, &hdev->dev_flags)) |
| 3134 | changed = true; |
| 3135 | } else { |
| 3136 | if (test_and_clear_bit(HCI_CONNECTABLE, &hdev->dev_flags)) |
| 3137 | changed = true; |
| 3138 | } |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 3139 | |
Johan Hedberg | ed9b5f2 | 2012-02-21 20:47:06 +0200 | [diff] [blame] | 3140 | mgmt_pending_foreach(MGMT_OP_SET_CONNECTABLE, hdev, settings_rsp, |
| 3141 | &match); |
| 3142 | |
Johan Hedberg | beadb2b | 2012-02-21 16:55:31 +0200 | [diff] [blame] | 3143 | if (changed) |
| 3144 | err = new_settings(hdev, match.sk); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 3145 | |
| 3146 | if (match.sk) |
| 3147 | sock_put(match.sk); |
| 3148 | |
Johan Hedberg | 7bb895d | 2012-02-17 01:20:00 +0200 | [diff] [blame] | 3149 | return err; |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 3150 | } |
| 3151 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 3152 | 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] | 3153 | { |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 3154 | u8 mgmt_err = mgmt_status(status); |
| 3155 | |
Johan Hedberg | 2d7cee5 | 2011-11-07 22:16:03 +0200 | [diff] [blame] | 3156 | if (scan & SCAN_PAGE) |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 3157 | mgmt_pending_foreach(MGMT_OP_SET_CONNECTABLE, hdev, |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 3158 | cmd_status_rsp, &mgmt_err); |
Johan Hedberg | 2d7cee5 | 2011-11-07 22:16:03 +0200 | [diff] [blame] | 3159 | |
| 3160 | if (scan & SCAN_INQUIRY) |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 3161 | mgmt_pending_foreach(MGMT_OP_SET_DISCOVERABLE, hdev, |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 3162 | cmd_status_rsp, &mgmt_err); |
Johan Hedberg | 2d7cee5 | 2011-11-07 22:16:03 +0200 | [diff] [blame] | 3163 | |
| 3164 | return 0; |
| 3165 | } |
| 3166 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 3167 | int mgmt_new_link_key(struct hci_dev *hdev, struct link_key *key, |
| 3168 | u8 persistent) |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 3169 | { |
Johan Hedberg | 86742e1 | 2011-11-07 23:13:38 +0200 | [diff] [blame] | 3170 | struct mgmt_ev_new_link_key ev; |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 3171 | |
Vinicius Costa Gomes | a492cd5 | 2011-08-25 20:02:29 -0300 | [diff] [blame] | 3172 | memset(&ev, 0, sizeof(ev)); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 3173 | |
Vinicius Costa Gomes | a492cd5 | 2011-08-25 20:02:29 -0300 | [diff] [blame] | 3174 | ev.store_hint = persistent; |
Johan Hedberg | d753fdc | 2012-02-17 14:06:34 +0200 | [diff] [blame] | 3175 | bacpy(&ev.key.addr.bdaddr, &key->bdaddr); |
| 3176 | ev.key.addr.type = MGMT_ADDR_BREDR; |
Vinicius Costa Gomes | a492cd5 | 2011-08-25 20:02:29 -0300 | [diff] [blame] | 3177 | ev.key.type = key->type; |
| 3178 | memcpy(ev.key.val, key->val, 16); |
| 3179 | ev.key.pin_len = key->pin_len; |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 3180 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 3181 | 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] | 3182 | } |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 3183 | |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 3184 | int mgmt_new_ltk(struct hci_dev *hdev, struct smp_ltk *key, u8 persistent) |
| 3185 | { |
| 3186 | struct mgmt_ev_new_long_term_key ev; |
| 3187 | |
| 3188 | memset(&ev, 0, sizeof(ev)); |
| 3189 | |
| 3190 | ev.store_hint = persistent; |
| 3191 | bacpy(&ev.key.addr.bdaddr, &key->bdaddr); |
| 3192 | ev.key.addr.type = key->bdaddr_type; |
| 3193 | ev.key.authenticated = key->authenticated; |
| 3194 | ev.key.enc_size = key->enc_size; |
| 3195 | ev.key.ediv = key->ediv; |
| 3196 | |
| 3197 | if (key->type == HCI_SMP_LTK) |
| 3198 | ev.key.master = 1; |
| 3199 | |
| 3200 | memcpy(ev.key.rand, key->rand, sizeof(key->rand)); |
| 3201 | memcpy(ev.key.val, key->val, sizeof(key->val)); |
| 3202 | |
| 3203 | return mgmt_event(MGMT_EV_NEW_LONG_TERM_KEY, hdev, |
| 3204 | &ev, sizeof(ev), NULL); |
| 3205 | } |
| 3206 | |
Johan Hedberg | afc747a | 2012-01-15 18:11:07 +0200 | [diff] [blame] | 3207 | 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] | 3208 | u8 addr_type, u8 *name, u8 name_len, |
| 3209 | u8 *dev_class) |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 3210 | { |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 3211 | char buf[512]; |
| 3212 | struct mgmt_ev_device_connected *ev = (void *) buf; |
| 3213 | u16 eir_len = 0; |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 3214 | |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 3215 | bacpy(&ev->addr.bdaddr, bdaddr); |
| 3216 | ev->addr.type = link_to_mgmt(link_type, addr_type); |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 3217 | |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 3218 | if (name_len > 0) |
| 3219 | eir_len = eir_append_data(ev->eir, 0, EIR_NAME_COMPLETE, |
| 3220 | name, name_len); |
| 3221 | |
| 3222 | if (dev_class && memcmp(dev_class, "\0\0\0", 3) != 0) |
| 3223 | eir_len = eir_append_data(&ev->eir[eir_len], eir_len, |
| 3224 | EIR_CLASS_OF_DEV, dev_class, 3); |
| 3225 | |
| 3226 | put_unaligned_le16(eir_len, &ev->eir_len); |
| 3227 | |
| 3228 | return mgmt_event(MGMT_EV_DEVICE_CONNECTED, hdev, buf, |
| 3229 | sizeof(*ev) + eir_len, NULL); |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 3230 | } |
| 3231 | |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 3232 | static void disconnect_rsp(struct pending_cmd *cmd, void *data) |
| 3233 | { |
Szymon Janc | c68fb7f | 2011-03-22 13:12:19 +0100 | [diff] [blame] | 3234 | struct mgmt_cp_disconnect *cp = cmd->param; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 3235 | struct sock **sk = data; |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 3236 | struct mgmt_rp_disconnect rp; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 3237 | |
Johan Hedberg | 88c3df1 | 2012-02-09 14:27:38 +0200 | [diff] [blame] | 3238 | bacpy(&rp.addr.bdaddr, &cp->addr.bdaddr); |
| 3239 | rp.addr.type = cp->addr.type; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 3240 | |
Johan Hedberg | aee9b21 | 2012-02-18 15:07:59 +0200 | [diff] [blame] | 3241 | cmd_complete(cmd->sk, cmd->index, MGMT_OP_DISCONNECT, 0, &rp, |
| 3242 | sizeof(rp)); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 3243 | |
| 3244 | *sk = cmd->sk; |
| 3245 | sock_hold(*sk); |
| 3246 | |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 3247 | mgmt_pending_remove(cmd); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 3248 | } |
| 3249 | |
Johan Hedberg | 124f6e3 | 2012-02-09 13:50:12 +0200 | [diff] [blame] | 3250 | static void unpair_device_rsp(struct pending_cmd *cmd, void *data) |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 3251 | { |
Johan Hedberg | b1078ad | 2012-02-09 17:21:16 +0200 | [diff] [blame] | 3252 | struct hci_dev *hdev = data; |
Johan Hedberg | 124f6e3 | 2012-02-09 13:50:12 +0200 | [diff] [blame] | 3253 | struct mgmt_cp_unpair_device *cp = cmd->param; |
| 3254 | struct mgmt_rp_unpair_device rp; |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 3255 | |
| 3256 | memset(&rp, 0, sizeof(rp)); |
Johan Hedberg | 124f6e3 | 2012-02-09 13:50:12 +0200 | [diff] [blame] | 3257 | bacpy(&rp.addr.bdaddr, &cp->addr.bdaddr); |
| 3258 | rp.addr.type = cp->addr.type; |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 3259 | |
Johan Hedberg | b1078ad | 2012-02-09 17:21:16 +0200 | [diff] [blame] | 3260 | device_unpaired(hdev, &cp->addr.bdaddr, cp->addr.type, cmd->sk); |
| 3261 | |
Johan Hedberg | aee9b21 | 2012-02-18 15:07:59 +0200 | [diff] [blame] | 3262 | cmd_complete(cmd->sk, cmd->index, cmd->opcode, 0, &rp, sizeof(rp)); |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 3263 | |
| 3264 | mgmt_pending_remove(cmd); |
| 3265 | } |
| 3266 | |
Johan Hedberg | afc747a | 2012-01-15 18:11:07 +0200 | [diff] [blame] | 3267 | int mgmt_device_disconnected(struct hci_dev *hdev, bdaddr_t *bdaddr, |
| 3268 | u8 link_type, u8 addr_type) |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 3269 | { |
Johan Hedberg | 4c659c3 | 2011-11-07 23:13:39 +0200 | [diff] [blame] | 3270 | struct mgmt_addr_info ev; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 3271 | struct sock *sk = NULL; |
| 3272 | int err; |
| 3273 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 3274 | mgmt_pending_foreach(MGMT_OP_DISCONNECT, hdev, disconnect_rsp, &sk); |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 3275 | |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 3276 | bacpy(&ev.bdaddr, bdaddr); |
Johan Hedberg | 48264f0 | 2011-11-09 13:58:58 +0200 | [diff] [blame] | 3277 | ev.type = link_to_mgmt(link_type, addr_type); |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 3278 | |
Johan Hedberg | afc747a | 2012-01-15 18:11:07 +0200 | [diff] [blame] | 3279 | err = mgmt_event(MGMT_EV_DEVICE_DISCONNECTED, hdev, &ev, sizeof(ev), |
| 3280 | sk); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 3281 | |
| 3282 | if (sk) |
| 3283 | sock_put(sk); |
| 3284 | |
Johan Hedberg | 124f6e3 | 2012-02-09 13:50:12 +0200 | [diff] [blame] | 3285 | mgmt_pending_foreach(MGMT_OP_UNPAIR_DEVICE, hdev, unpair_device_rsp, |
Johan Hedberg | b1078ad | 2012-02-09 17:21:16 +0200 | [diff] [blame] | 3286 | hdev); |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 3287 | |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 3288 | return err; |
| 3289 | } |
| 3290 | |
Johan Hedberg | 88c3df1 | 2012-02-09 14:27:38 +0200 | [diff] [blame] | 3291 | int mgmt_disconnect_failed(struct hci_dev *hdev, bdaddr_t *bdaddr, |
| 3292 | u8 link_type, u8 addr_type, u8 status) |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 3293 | { |
Johan Hedberg | 88c3df1 | 2012-02-09 14:27:38 +0200 | [diff] [blame] | 3294 | struct mgmt_rp_disconnect rp; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 3295 | struct pending_cmd *cmd; |
| 3296 | int err; |
| 3297 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 3298 | cmd = mgmt_pending_find(MGMT_OP_DISCONNECT, hdev); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 3299 | if (!cmd) |
| 3300 | return -ENOENT; |
| 3301 | |
Johan Hedberg | 88c3df1 | 2012-02-09 14:27:38 +0200 | [diff] [blame] | 3302 | bacpy(&rp.addr.bdaddr, bdaddr); |
| 3303 | rp.addr.type = link_to_mgmt(link_type, addr_type); |
Johan Hedberg | 37d9ef7 | 2011-11-10 15:54:39 +0200 | [diff] [blame] | 3304 | |
Johan Hedberg | 88c3df1 | 2012-02-09 14:27:38 +0200 | [diff] [blame] | 3305 | err = cmd_complete(cmd->sk, cmd->index, MGMT_OP_DISCONNECT, |
Johan Hedberg | aee9b21 | 2012-02-18 15:07:59 +0200 | [diff] [blame] | 3306 | mgmt_status(status), &rp, sizeof(rp)); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 3307 | |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 3308 | mgmt_pending_remove(cmd); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 3309 | |
Johan Hedberg | b1078ad | 2012-02-09 17:21:16 +0200 | [diff] [blame] | 3310 | mgmt_pending_foreach(MGMT_OP_UNPAIR_DEVICE, hdev, unpair_device_rsp, |
| 3311 | hdev); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 3312 | return err; |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 3313 | } |
Johan Hedberg | 17d5c04 | 2011-01-22 06:09:08 +0200 | [diff] [blame] | 3314 | |
Johan Hedberg | 48264f0 | 2011-11-09 13:58:58 +0200 | [diff] [blame] | 3315 | int mgmt_connect_failed(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 link_type, |
| 3316 | u8 addr_type, u8 status) |
Johan Hedberg | 17d5c04 | 2011-01-22 06:09:08 +0200 | [diff] [blame] | 3317 | { |
| 3318 | struct mgmt_ev_connect_failed ev; |
| 3319 | |
Johan Hedberg | 4c659c3 | 2011-11-07 23:13:39 +0200 | [diff] [blame] | 3320 | bacpy(&ev.addr.bdaddr, bdaddr); |
Johan Hedberg | 48264f0 | 2011-11-09 13:58:58 +0200 | [diff] [blame] | 3321 | ev.addr.type = link_to_mgmt(link_type, addr_type); |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 3322 | ev.status = mgmt_status(status); |
Johan Hedberg | 17d5c04 | 2011-01-22 06:09:08 +0200 | [diff] [blame] | 3323 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 3324 | return mgmt_event(MGMT_EV_CONNECT_FAILED, hdev, &ev, sizeof(ev), NULL); |
Johan Hedberg | 17d5c04 | 2011-01-22 06:09:08 +0200 | [diff] [blame] | 3325 | } |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 3326 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 3327 | 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] | 3328 | { |
| 3329 | struct mgmt_ev_pin_code_request ev; |
| 3330 | |
Johan Hedberg | d845769 | 2012-02-17 14:24:57 +0200 | [diff] [blame] | 3331 | bacpy(&ev.addr.bdaddr, bdaddr); |
| 3332 | ev.addr.type = MGMT_ADDR_BREDR; |
Waldemar Rymarkiewicz | a770bb5 | 2011-04-28 12:07:59 +0200 | [diff] [blame] | 3333 | ev.secure = secure; |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 3334 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 3335 | return mgmt_event(MGMT_EV_PIN_CODE_REQUEST, hdev, &ev, sizeof(ev), |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 3336 | NULL); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 3337 | } |
| 3338 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 3339 | int mgmt_pin_code_reply_complete(struct hci_dev *hdev, bdaddr_t *bdaddr, |
| 3340 | u8 status) |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 3341 | { |
| 3342 | struct pending_cmd *cmd; |
Johan Hedberg | ac56fb1 | 2011-02-19 12:05:59 -0300 | [diff] [blame] | 3343 | struct mgmt_rp_pin_code_reply rp; |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 3344 | int err; |
| 3345 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 3346 | cmd = mgmt_pending_find(MGMT_OP_PIN_CODE_REPLY, hdev); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 3347 | if (!cmd) |
| 3348 | return -ENOENT; |
| 3349 | |
Johan Hedberg | d845769 | 2012-02-17 14:24:57 +0200 | [diff] [blame] | 3350 | bacpy(&rp.addr.bdaddr, bdaddr); |
| 3351 | rp.addr.type = MGMT_ADDR_BREDR; |
Johan Hedberg | ac56fb1 | 2011-02-19 12:05:59 -0300 | [diff] [blame] | 3352 | |
Johan Hedberg | aee9b21 | 2012-02-18 15:07:59 +0200 | [diff] [blame] | 3353 | err = cmd_complete(cmd->sk, hdev->id, MGMT_OP_PIN_CODE_REPLY, |
| 3354 | mgmt_status(status), &rp, sizeof(rp)); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 3355 | |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 3356 | mgmt_pending_remove(cmd); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 3357 | |
| 3358 | return err; |
| 3359 | } |
| 3360 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 3361 | int mgmt_pin_code_neg_reply_complete(struct hci_dev *hdev, bdaddr_t *bdaddr, |
| 3362 | u8 status) |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 3363 | { |
| 3364 | struct pending_cmd *cmd; |
Johan Hedberg | ac56fb1 | 2011-02-19 12:05:59 -0300 | [diff] [blame] | 3365 | struct mgmt_rp_pin_code_reply rp; |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 3366 | int err; |
| 3367 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 3368 | cmd = mgmt_pending_find(MGMT_OP_PIN_CODE_NEG_REPLY, hdev); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 3369 | if (!cmd) |
| 3370 | return -ENOENT; |
| 3371 | |
Johan Hedberg | d845769 | 2012-02-17 14:24:57 +0200 | [diff] [blame] | 3372 | bacpy(&rp.addr.bdaddr, bdaddr); |
| 3373 | rp.addr.type = MGMT_ADDR_BREDR; |
Johan Hedberg | ac56fb1 | 2011-02-19 12:05:59 -0300 | [diff] [blame] | 3374 | |
Johan Hedberg | aee9b21 | 2012-02-18 15:07:59 +0200 | [diff] [blame] | 3375 | err = cmd_complete(cmd->sk, hdev->id, MGMT_OP_PIN_CODE_NEG_REPLY, |
| 3376 | mgmt_status(status), &rp, sizeof(rp)); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 3377 | |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 3378 | mgmt_pending_remove(cmd); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 3379 | |
| 3380 | return err; |
| 3381 | } |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 3382 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 3383 | int mgmt_user_confirm_request(struct hci_dev *hdev, bdaddr_t *bdaddr, |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 3384 | u8 link_type, u8 addr_type, __le32 value, |
| 3385 | u8 confirm_hint) |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 3386 | { |
| 3387 | struct mgmt_ev_user_confirm_request ev; |
| 3388 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 3389 | BT_DBG("%s", hdev->name); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 3390 | |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 3391 | bacpy(&ev.addr.bdaddr, bdaddr); |
| 3392 | ev.addr.type = link_to_mgmt(link_type, addr_type); |
Johan Hedberg | 55bc1a3 | 2011-04-28 11:28:56 -0700 | [diff] [blame] | 3393 | ev.confirm_hint = confirm_hint; |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 3394 | put_unaligned_le32(value, &ev.value); |
| 3395 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 3396 | return mgmt_event(MGMT_EV_USER_CONFIRM_REQUEST, hdev, &ev, sizeof(ev), |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 3397 | NULL); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 3398 | } |
| 3399 | |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 3400 | int mgmt_user_passkey_request(struct hci_dev *hdev, bdaddr_t *bdaddr, |
| 3401 | u8 link_type, u8 addr_type) |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 3402 | { |
| 3403 | struct mgmt_ev_user_passkey_request ev; |
| 3404 | |
| 3405 | BT_DBG("%s", hdev->name); |
| 3406 | |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 3407 | bacpy(&ev.addr.bdaddr, bdaddr); |
| 3408 | ev.addr.type = link_to_mgmt(link_type, addr_type); |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 3409 | |
| 3410 | return mgmt_event(MGMT_EV_USER_PASSKEY_REQUEST, hdev, &ev, sizeof(ev), |
| 3411 | NULL); |
| 3412 | } |
| 3413 | |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3414 | 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] | 3415 | u8 link_type, u8 addr_type, u8 status, |
| 3416 | u8 opcode) |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 3417 | { |
| 3418 | struct pending_cmd *cmd; |
| 3419 | struct mgmt_rp_user_confirm_reply rp; |
| 3420 | int err; |
| 3421 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 3422 | cmd = mgmt_pending_find(opcode, hdev); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 3423 | if (!cmd) |
| 3424 | return -ENOENT; |
| 3425 | |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 3426 | bacpy(&rp.addr.bdaddr, bdaddr); |
| 3427 | rp.addr.type = link_to_mgmt(link_type, addr_type); |
Johan Hedberg | aee9b21 | 2012-02-18 15:07:59 +0200 | [diff] [blame] | 3428 | err = cmd_complete(cmd->sk, hdev->id, opcode, mgmt_status(status), |
| 3429 | &rp, sizeof(rp)); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 3430 | |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 3431 | mgmt_pending_remove(cmd); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 3432 | |
| 3433 | return err; |
| 3434 | } |
| 3435 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 3436 | 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] | 3437 | u8 link_type, u8 addr_type, u8 status) |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 3438 | { |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 3439 | return user_pairing_resp_complete(hdev, bdaddr, link_type, addr_type, |
| 3440 | status, MGMT_OP_USER_CONFIRM_REPLY); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 3441 | } |
| 3442 | |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 3443 | int mgmt_user_confirm_neg_reply_complete(struct hci_dev *hdev, bdaddr_t *bdaddr, |
| 3444 | u8 link_type, u8 addr_type, u8 status) |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 3445 | { |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 3446 | return user_pairing_resp_complete(hdev, bdaddr, link_type, addr_type, |
| 3447 | status, MGMT_OP_USER_CONFIRM_NEG_REPLY); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 3448 | } |
Johan Hedberg | 2a61169 | 2011-02-19 12:06:00 -0300 | [diff] [blame] | 3449 | |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 3450 | 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] | 3451 | u8 link_type, u8 addr_type, u8 status) |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 3452 | { |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 3453 | return user_pairing_resp_complete(hdev, bdaddr, link_type, addr_type, |
| 3454 | status, MGMT_OP_USER_PASSKEY_REPLY); |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 3455 | } |
| 3456 | |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 3457 | int mgmt_user_passkey_neg_reply_complete(struct hci_dev *hdev, bdaddr_t *bdaddr, |
| 3458 | u8 link_type, u8 addr_type, u8 status) |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 3459 | { |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 3460 | return user_pairing_resp_complete(hdev, bdaddr, link_type, addr_type, |
| 3461 | status, MGMT_OP_USER_PASSKEY_NEG_REPLY); |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 3462 | } |
| 3463 | |
Johan Hedberg | bab73cb | 2012-02-09 16:07:29 +0200 | [diff] [blame] | 3464 | int mgmt_auth_failed(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 link_type, |
| 3465 | u8 addr_type, u8 status) |
Johan Hedberg | 2a61169 | 2011-02-19 12:06:00 -0300 | [diff] [blame] | 3466 | { |
| 3467 | struct mgmt_ev_auth_failed ev; |
| 3468 | |
Johan Hedberg | bab73cb | 2012-02-09 16:07:29 +0200 | [diff] [blame] | 3469 | bacpy(&ev.addr.bdaddr, bdaddr); |
| 3470 | ev.addr.type = link_to_mgmt(link_type, addr_type); |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 3471 | ev.status = mgmt_status(status); |
Johan Hedberg | 2a61169 | 2011-02-19 12:06:00 -0300 | [diff] [blame] | 3472 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 3473 | return mgmt_event(MGMT_EV_AUTH_FAILED, hdev, &ev, sizeof(ev), NULL); |
Johan Hedberg | 2a61169 | 2011-02-19 12:06:00 -0300 | [diff] [blame] | 3474 | } |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 3475 | |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 3476 | int mgmt_auth_enable_complete(struct hci_dev *hdev, u8 status) |
| 3477 | { |
| 3478 | struct cmd_lookup match = { NULL, hdev }; |
Johan Hedberg | 47990ea | 2012-02-22 11:58:37 +0200 | [diff] [blame] | 3479 | bool changed = false; |
| 3480 | int err = 0; |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 3481 | |
| 3482 | if (status) { |
| 3483 | u8 mgmt_err = mgmt_status(status); |
| 3484 | mgmt_pending_foreach(MGMT_OP_SET_LINK_SECURITY, hdev, |
| 3485 | cmd_status_rsp, &mgmt_err); |
| 3486 | return 0; |
| 3487 | } |
| 3488 | |
Johan Hedberg | 47990ea | 2012-02-22 11:58:37 +0200 | [diff] [blame] | 3489 | if (test_bit(HCI_AUTH, &hdev->flags)) { |
| 3490 | if (!test_and_set_bit(HCI_LINK_SECURITY, &hdev->dev_flags)) |
| 3491 | changed = true; |
| 3492 | } else { |
| 3493 | if (test_and_clear_bit(HCI_LINK_SECURITY, &hdev->dev_flags)) |
| 3494 | changed = true; |
| 3495 | } |
| 3496 | |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 3497 | mgmt_pending_foreach(MGMT_OP_SET_LINK_SECURITY, hdev, settings_rsp, |
| 3498 | &match); |
| 3499 | |
Johan Hedberg | 47990ea | 2012-02-22 11:58:37 +0200 | [diff] [blame] | 3500 | if (changed) |
| 3501 | err = new_settings(hdev, match.sk); |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 3502 | |
| 3503 | if (match.sk) |
| 3504 | sock_put(match.sk); |
| 3505 | |
| 3506 | return err; |
| 3507 | } |
| 3508 | |
Johan Hedberg | cacaf52 | 2012-02-21 00:52:42 +0200 | [diff] [blame] | 3509 | static int clear_eir(struct hci_dev *hdev) |
| 3510 | { |
| 3511 | struct hci_cp_write_eir cp; |
| 3512 | |
| 3513 | if (!(hdev->features[6] & LMP_EXT_INQ)) |
| 3514 | return 0; |
| 3515 | |
Johan Hedberg | c80da27 | 2012-02-22 15:38:48 +0200 | [diff] [blame] | 3516 | memset(hdev->eir, 0, sizeof(hdev->eir)); |
| 3517 | |
Johan Hedberg | cacaf52 | 2012-02-21 00:52:42 +0200 | [diff] [blame] | 3518 | memset(&cp, 0, sizeof(cp)); |
| 3519 | |
| 3520 | return hci_send_cmd(hdev, HCI_OP_WRITE_EIR, sizeof(cp), &cp); |
| 3521 | } |
| 3522 | |
Johan Hedberg | c0ecddc | 2012-02-22 12:38:31 +0200 | [diff] [blame] | 3523 | 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] | 3524 | { |
| 3525 | struct cmd_lookup match = { NULL, hdev }; |
Johan Hedberg | c0ecddc | 2012-02-22 12:38:31 +0200 | [diff] [blame] | 3526 | bool changed = false; |
| 3527 | int err = 0; |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 3528 | |
| 3529 | if (status) { |
| 3530 | u8 mgmt_err = mgmt_status(status); |
Johan Hedberg | c0ecddc | 2012-02-22 12:38:31 +0200 | [diff] [blame] | 3531 | |
| 3532 | if (enable && test_and_clear_bit(HCI_SSP_ENABLED, |
| 3533 | &hdev->dev_flags)) |
| 3534 | err = new_settings(hdev, NULL); |
| 3535 | |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 3536 | mgmt_pending_foreach(MGMT_OP_SET_SSP, hdev, |
| 3537 | cmd_status_rsp, &mgmt_err); |
Johan Hedberg | c0ecddc | 2012-02-22 12:38:31 +0200 | [diff] [blame] | 3538 | |
| 3539 | return err; |
| 3540 | } |
| 3541 | |
| 3542 | if (enable) { |
| 3543 | if (!test_and_set_bit(HCI_SSP_ENABLED, &hdev->dev_flags)) |
| 3544 | changed = true; |
| 3545 | } else { |
| 3546 | if (test_and_clear_bit(HCI_SSP_ENABLED, &hdev->dev_flags)) |
| 3547 | changed = true; |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 3548 | } |
| 3549 | |
| 3550 | mgmt_pending_foreach(MGMT_OP_SET_SSP, hdev, settings_rsp, &match); |
| 3551 | |
Johan Hedberg | c0ecddc | 2012-02-22 12:38:31 +0200 | [diff] [blame] | 3552 | if (changed) |
| 3553 | err = new_settings(hdev, match.sk); |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 3554 | |
Johan Hedberg | 5fc6ebb | 2012-02-22 15:10:59 +0200 | [diff] [blame] | 3555 | if (match.sk) |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 3556 | sock_put(match.sk); |
| 3557 | |
Johan Hedberg | 5fc6ebb | 2012-02-22 15:10:59 +0200 | [diff] [blame] | 3558 | if (test_bit(HCI_SSP_ENABLED, &hdev->dev_flags)) |
| 3559 | update_eir(hdev); |
| 3560 | else |
| 3561 | clear_eir(hdev); |
Johan Hedberg | cacaf52 | 2012-02-21 00:52:42 +0200 | [diff] [blame] | 3562 | |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 3563 | return err; |
| 3564 | } |
| 3565 | |
Marcel Holtmann | 7f9a903 | 2012-02-22 18:38:01 +0100 | [diff] [blame] | 3566 | int mgmt_set_class_of_dev_complete(struct hci_dev *hdev, u8 *dev_class, |
| 3567 | u8 status) |
| 3568 | { |
| 3569 | int err; |
| 3570 | |
| 3571 | err = mgmt_event(MGMT_EV_CLASS_OF_DEV_CHANGED, hdev, dev_class, 3, NULL); |
| 3572 | |
| 3573 | return err; |
| 3574 | } |
| 3575 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 3576 | 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] | 3577 | { |
| 3578 | struct pending_cmd *cmd; |
| 3579 | struct mgmt_cp_set_local_name ev; |
Johan Hedberg | 28cc7bd | 2012-02-22 21:06:55 +0200 | [diff] [blame] | 3580 | bool changed = false; |
| 3581 | int err = 0; |
| 3582 | |
| 3583 | if (memcmp(name, hdev->dev_name, sizeof(hdev->dev_name)) != 0) { |
| 3584 | memcpy(hdev->dev_name, name, sizeof(hdev->dev_name)); |
| 3585 | changed = true; |
| 3586 | } |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 3587 | |
| 3588 | memset(&ev, 0, sizeof(ev)); |
| 3589 | memcpy(ev.name, name, HCI_MAX_NAME_LENGTH); |
Johan Hedberg | 28cc7bd | 2012-02-22 21:06:55 +0200 | [diff] [blame] | 3590 | memcpy(ev.short_name, hdev->short_name, HCI_MAX_SHORT_NAME_LENGTH); |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 3591 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 3592 | cmd = mgmt_pending_find(MGMT_OP_SET_LOCAL_NAME, hdev); |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 3593 | if (!cmd) |
| 3594 | goto send_event; |
| 3595 | |
Johan Hedberg | 7bdaae4 | 2012-02-22 21:39:58 +0200 | [diff] [blame] | 3596 | /* Always assume that either the short or the complete name has |
| 3597 | * changed if there was a pending mgmt command */ |
| 3598 | changed = true; |
| 3599 | |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 3600 | if (status) { |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 3601 | err = cmd_status(cmd->sk, hdev->id, MGMT_OP_SET_LOCAL_NAME, |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 3602 | mgmt_status(status)); |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 3603 | goto failed; |
| 3604 | } |
| 3605 | |
Johan Hedberg | aee9b21 | 2012-02-18 15:07:59 +0200 | [diff] [blame] | 3606 | 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] | 3607 | sizeof(ev)); |
| 3608 | if (err < 0) |
| 3609 | goto failed; |
| 3610 | |
| 3611 | send_event: |
Johan Hedberg | 28cc7bd | 2012-02-22 21:06:55 +0200 | [diff] [blame] | 3612 | if (changed) |
| 3613 | err = mgmt_event(MGMT_EV_LOCAL_NAME_CHANGED, hdev, &ev, |
| 3614 | sizeof(ev), cmd ? cmd->sk : NULL); |
| 3615 | |
Johan Hedberg | f51d5b2 | 2012-02-22 18:17:32 +0200 | [diff] [blame] | 3616 | update_eir(hdev); |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 3617 | |
| 3618 | failed: |
| 3619 | if (cmd) |
| 3620 | mgmt_pending_remove(cmd); |
| 3621 | return err; |
| 3622 | } |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 3623 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 3624 | int mgmt_read_local_oob_data_reply_complete(struct hci_dev *hdev, u8 *hash, |
| 3625 | u8 *randomizer, u8 status) |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 3626 | { |
| 3627 | struct pending_cmd *cmd; |
| 3628 | int err; |
| 3629 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 3630 | BT_DBG("%s status %u", hdev->name, status); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 3631 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 3632 | cmd = mgmt_pending_find(MGMT_OP_READ_LOCAL_OOB_DATA, hdev); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 3633 | if (!cmd) |
| 3634 | return -ENOENT; |
| 3635 | |
| 3636 | if (status) { |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 3637 | err = cmd_status(cmd->sk, hdev->id, |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 3638 | MGMT_OP_READ_LOCAL_OOB_DATA, |
| 3639 | mgmt_status(status)); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 3640 | } else { |
| 3641 | struct mgmt_rp_read_local_oob_data rp; |
| 3642 | |
| 3643 | memcpy(rp.hash, hash, sizeof(rp.hash)); |
| 3644 | memcpy(rp.randomizer, randomizer, sizeof(rp.randomizer)); |
| 3645 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 3646 | err = cmd_complete(cmd->sk, hdev->id, |
| 3647 | MGMT_OP_READ_LOCAL_OOB_DATA, |
Johan Hedberg | aee9b21 | 2012-02-18 15:07:59 +0200 | [diff] [blame] | 3648 | 0, &rp, sizeof(rp)); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 3649 | } |
| 3650 | |
| 3651 | mgmt_pending_remove(cmd); |
| 3652 | |
| 3653 | return err; |
| 3654 | } |
Johan Hedberg | e17acd4 | 2011-03-30 23:57:16 +0300 | [diff] [blame] | 3655 | |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 3656 | int mgmt_le_enable_complete(struct hci_dev *hdev, u8 enable, u8 status) |
| 3657 | { |
| 3658 | struct cmd_lookup match = { NULL, hdev }; |
| 3659 | bool changed = false; |
| 3660 | int err = 0; |
| 3661 | |
| 3662 | if (status) { |
| 3663 | u8 mgmt_err = mgmt_status(status); |
| 3664 | |
| 3665 | if (enable && test_and_clear_bit(HCI_LE_ENABLED, |
| 3666 | &hdev->dev_flags)) |
| 3667 | err = new_settings(hdev, NULL); |
| 3668 | |
| 3669 | mgmt_pending_foreach(MGMT_OP_SET_LE, hdev, |
| 3670 | cmd_status_rsp, &mgmt_err); |
| 3671 | |
| 3672 | return err; |
| 3673 | } |
| 3674 | |
| 3675 | if (enable) { |
| 3676 | if (!test_and_set_bit(HCI_LE_ENABLED, &hdev->dev_flags)) |
| 3677 | changed = true; |
| 3678 | } else { |
| 3679 | if (test_and_clear_bit(HCI_LE_ENABLED, &hdev->dev_flags)) |
| 3680 | changed = true; |
| 3681 | } |
| 3682 | |
| 3683 | mgmt_pending_foreach(MGMT_OP_SET_LE, hdev, settings_rsp, &match); |
| 3684 | |
| 3685 | if (changed) |
| 3686 | err = new_settings(hdev, match.sk); |
| 3687 | |
| 3688 | if (match.sk) |
| 3689 | sock_put(match.sk); |
| 3690 | |
| 3691 | return err; |
| 3692 | } |
| 3693 | |
Johan Hedberg | 48264f0 | 2011-11-09 13:58:58 +0200 | [diff] [blame] | 3694 | 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] | 3695 | u8 addr_type, u8 *dev_class, s8 rssi, |
Johan Hedberg | e319d2e | 2012-01-15 19:51:59 +0200 | [diff] [blame] | 3696 | u8 cfm_name, u8 *eir, u16 eir_len) |
Johan Hedberg | e17acd4 | 2011-03-30 23:57:16 +0300 | [diff] [blame] | 3697 | { |
Johan Hedberg | e319d2e | 2012-01-15 19:51:59 +0200 | [diff] [blame] | 3698 | char buf[512]; |
| 3699 | struct mgmt_ev_device_found *ev = (void *) buf; |
Johan Hedberg | 1dc0609 | 2012-01-15 21:01:23 +0200 | [diff] [blame] | 3700 | size_t ev_size; |
Johan Hedberg | e17acd4 | 2011-03-30 23:57:16 +0300 | [diff] [blame] | 3701 | |
Johan Hedberg | 1dc0609 | 2012-01-15 21:01:23 +0200 | [diff] [blame] | 3702 | /* Leave 5 bytes for a potential CoD field */ |
| 3703 | if (sizeof(*ev) + eir_len + 5 > sizeof(buf)) |
Andre Guedes | 7d262f8 | 2012-01-10 18:20:49 -0300 | [diff] [blame] | 3704 | return -EINVAL; |
| 3705 | |
Johan Hedberg | 1dc0609 | 2012-01-15 21:01:23 +0200 | [diff] [blame] | 3706 | memset(buf, 0, sizeof(buf)); |
| 3707 | |
Johan Hedberg | e319d2e | 2012-01-15 19:51:59 +0200 | [diff] [blame] | 3708 | bacpy(&ev->addr.bdaddr, bdaddr); |
| 3709 | ev->addr.type = link_to_mgmt(link_type, addr_type); |
| 3710 | ev->rssi = rssi; |
| 3711 | ev->confirm_name = cfm_name; |
Johan Hedberg | e17acd4 | 2011-03-30 23:57:16 +0300 | [diff] [blame] | 3712 | |
Johan Hedberg | 1dc0609 | 2012-01-15 21:01:23 +0200 | [diff] [blame] | 3713 | if (eir_len > 0) |
Johan Hedberg | e319d2e | 2012-01-15 19:51:59 +0200 | [diff] [blame] | 3714 | memcpy(ev->eir, eir, eir_len); |
Johan Hedberg | e17acd4 | 2011-03-30 23:57:16 +0300 | [diff] [blame] | 3715 | |
Johan Hedberg | 1dc0609 | 2012-01-15 21:01:23 +0200 | [diff] [blame] | 3716 | if (dev_class && !eir_has_data_type(ev->eir, eir_len, EIR_CLASS_OF_DEV)) |
| 3717 | eir_len = eir_append_data(ev->eir, eir_len, EIR_CLASS_OF_DEV, |
| 3718 | dev_class, 3); |
| 3719 | |
| 3720 | put_unaligned_le16(eir_len, &ev->eir_len); |
| 3721 | |
| 3722 | ev_size = sizeof(*ev) + eir_len; |
Andre Guedes | f852359 | 2011-09-09 18:56:26 -0300 | [diff] [blame] | 3723 | |
Johan Hedberg | e319d2e | 2012-01-15 19:51:59 +0200 | [diff] [blame] | 3724 | return mgmt_event(MGMT_EV_DEVICE_FOUND, hdev, ev, ev_size, NULL); |
Johan Hedberg | e17acd4 | 2011-03-30 23:57:16 +0300 | [diff] [blame] | 3725 | } |
Johan Hedberg | a88a965 | 2011-03-30 13:18:12 +0300 | [diff] [blame] | 3726 | |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 3727 | int mgmt_remote_name(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 link_type, |
| 3728 | u8 addr_type, s8 rssi, u8 *name, u8 name_len) |
Johan Hedberg | a88a965 | 2011-03-30 13:18:12 +0300 | [diff] [blame] | 3729 | { |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 3730 | struct mgmt_ev_device_found *ev; |
| 3731 | char buf[sizeof(*ev) + HCI_MAX_NAME_LENGTH + 2]; |
| 3732 | u16 eir_len; |
Johan Hedberg | a88a965 | 2011-03-30 13:18:12 +0300 | [diff] [blame] | 3733 | |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 3734 | ev = (struct mgmt_ev_device_found *) buf; |
Johan Hedberg | a88a965 | 2011-03-30 13:18:12 +0300 | [diff] [blame] | 3735 | |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 3736 | memset(buf, 0, sizeof(buf)); |
Johan Hedberg | a88a965 | 2011-03-30 13:18:12 +0300 | [diff] [blame] | 3737 | |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 3738 | bacpy(&ev->addr.bdaddr, bdaddr); |
| 3739 | ev->addr.type = link_to_mgmt(link_type, addr_type); |
| 3740 | ev->rssi = rssi; |
| 3741 | |
| 3742 | eir_len = eir_append_data(ev->eir, 0, EIR_NAME_COMPLETE, name, |
| 3743 | name_len); |
| 3744 | |
| 3745 | put_unaligned_le16(eir_len, &ev->eir_len); |
| 3746 | |
Johan Hedberg | 053c7e0 | 2012-02-04 00:06:00 +0200 | [diff] [blame] | 3747 | return mgmt_event(MGMT_EV_DEVICE_FOUND, hdev, ev, |
| 3748 | sizeof(*ev) + eir_len, NULL); |
Johan Hedberg | a88a965 | 2011-03-30 13:18:12 +0300 | [diff] [blame] | 3749 | } |
Johan Hedberg | 314b238 | 2011-04-27 10:29:57 -0400 | [diff] [blame] | 3750 | |
Andre Guedes | 7a13510 | 2011-11-09 17:14:25 -0300 | [diff] [blame] | 3751 | int mgmt_start_discovery_failed(struct hci_dev *hdev, u8 status) |
Johan Hedberg | 164a6e7 | 2011-11-01 17:06:44 +0200 | [diff] [blame] | 3752 | { |
| 3753 | struct pending_cmd *cmd; |
Johan Hedberg | f808e16 | 2012-02-19 12:52:07 +0200 | [diff] [blame] | 3754 | u8 type; |
Johan Hedberg | 164a6e7 | 2011-11-01 17:06:44 +0200 | [diff] [blame] | 3755 | int err; |
| 3756 | |
Andre Guedes | 203159d | 2012-02-13 15:41:01 -0300 | [diff] [blame] | 3757 | hci_discovery_set_state(hdev, DISCOVERY_STOPPED); |
| 3758 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 3759 | cmd = mgmt_pending_find(MGMT_OP_START_DISCOVERY, hdev); |
Johan Hedberg | 164a6e7 | 2011-11-01 17:06:44 +0200 | [diff] [blame] | 3760 | if (!cmd) |
| 3761 | return -ENOENT; |
| 3762 | |
Johan Hedberg | f808e16 | 2012-02-19 12:52:07 +0200 | [diff] [blame] | 3763 | type = hdev->discovery.type; |
| 3764 | |
| 3765 | err = cmd_complete(cmd->sk, hdev->id, cmd->opcode, mgmt_status(status), |
| 3766 | &type, sizeof(type)); |
Johan Hedberg | 164a6e7 | 2011-11-01 17:06:44 +0200 | [diff] [blame] | 3767 | mgmt_pending_remove(cmd); |
| 3768 | |
| 3769 | return err; |
| 3770 | } |
| 3771 | |
Andre Guedes | e6d465c | 2011-11-09 17:14:26 -0300 | [diff] [blame] | 3772 | int mgmt_stop_discovery_failed(struct hci_dev *hdev, u8 status) |
| 3773 | { |
| 3774 | struct pending_cmd *cmd; |
| 3775 | int err; |
| 3776 | |
| 3777 | cmd = mgmt_pending_find(MGMT_OP_STOP_DISCOVERY, hdev); |
| 3778 | if (!cmd) |
| 3779 | return -ENOENT; |
| 3780 | |
Johan Hedberg | d930650 | 2012-02-20 23:25:18 +0200 | [diff] [blame] | 3781 | err = cmd_complete(cmd->sk, hdev->id, cmd->opcode, mgmt_status(status), |
| 3782 | &hdev->discovery.type, |
| 3783 | sizeof(hdev->discovery.type)); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 3784 | mgmt_pending_remove(cmd); |
| 3785 | |
| 3786 | return err; |
| 3787 | } |
Johan Hedberg | 314b238 | 2011-04-27 10:29:57 -0400 | [diff] [blame] | 3788 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 3789 | int mgmt_discovering(struct hci_dev *hdev, u8 discovering) |
Johan Hedberg | 314b238 | 2011-04-27 10:29:57 -0400 | [diff] [blame] | 3790 | { |
Johan Hedberg | f963e8e | 2012-02-20 23:30:44 +0200 | [diff] [blame] | 3791 | struct mgmt_ev_discovering ev; |
Johan Hedberg | 164a6e7 | 2011-11-01 17:06:44 +0200 | [diff] [blame] | 3792 | struct pending_cmd *cmd; |
| 3793 | |
Andre Guedes | 343fb14 | 2011-11-22 17:14:19 -0300 | [diff] [blame] | 3794 | BT_DBG("%s discovering %u", hdev->name, discovering); |
| 3795 | |
Johan Hedberg | 164a6e7 | 2011-11-01 17:06:44 +0200 | [diff] [blame] | 3796 | if (discovering) |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 3797 | cmd = mgmt_pending_find(MGMT_OP_START_DISCOVERY, hdev); |
Johan Hedberg | 164a6e7 | 2011-11-01 17:06:44 +0200 | [diff] [blame] | 3798 | else |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 3799 | cmd = mgmt_pending_find(MGMT_OP_STOP_DISCOVERY, hdev); |
Johan Hedberg | 164a6e7 | 2011-11-01 17:06:44 +0200 | [diff] [blame] | 3800 | |
| 3801 | if (cmd != NULL) { |
Johan Hedberg | f808e16 | 2012-02-19 12:52:07 +0200 | [diff] [blame] | 3802 | u8 type = hdev->discovery.type; |
| 3803 | |
Johan Hedberg | d930650 | 2012-02-20 23:25:18 +0200 | [diff] [blame] | 3804 | cmd_complete(cmd->sk, hdev->id, cmd->opcode, 0, |
Johan Hedberg | f808e16 | 2012-02-19 12:52:07 +0200 | [diff] [blame] | 3805 | &type, sizeof(type)); |
Johan Hedberg | 164a6e7 | 2011-11-01 17:06:44 +0200 | [diff] [blame] | 3806 | mgmt_pending_remove(cmd); |
| 3807 | } |
| 3808 | |
Johan Hedberg | f963e8e | 2012-02-20 23:30:44 +0200 | [diff] [blame] | 3809 | memset(&ev, 0, sizeof(ev)); |
| 3810 | ev.type = hdev->discovery.type; |
| 3811 | ev.discovering = discovering; |
| 3812 | |
| 3813 | return mgmt_event(MGMT_EV_DISCOVERING, hdev, &ev, sizeof(ev), NULL); |
Johan Hedberg | 314b238 | 2011-04-27 10:29:57 -0400 | [diff] [blame] | 3814 | } |
Antti Julku | 5e76244 | 2011-08-25 16:48:02 +0300 | [diff] [blame] | 3815 | |
Johan Hedberg | 88c1fe4 | 2012-02-09 15:56:11 +0200 | [diff] [blame] | 3816 | 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] | 3817 | { |
| 3818 | struct pending_cmd *cmd; |
| 3819 | struct mgmt_ev_device_blocked ev; |
| 3820 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 3821 | cmd = mgmt_pending_find(MGMT_OP_BLOCK_DEVICE, hdev); |
Antti Julku | 5e76244 | 2011-08-25 16:48:02 +0300 | [diff] [blame] | 3822 | |
Johan Hedberg | 88c1fe4 | 2012-02-09 15:56:11 +0200 | [diff] [blame] | 3823 | bacpy(&ev.addr.bdaddr, bdaddr); |
| 3824 | ev.addr.type = type; |
Antti Julku | 5e76244 | 2011-08-25 16:48:02 +0300 | [diff] [blame] | 3825 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 3826 | return mgmt_event(MGMT_EV_DEVICE_BLOCKED, hdev, &ev, sizeof(ev), |
| 3827 | cmd ? cmd->sk : NULL); |
Antti Julku | 5e76244 | 2011-08-25 16:48:02 +0300 | [diff] [blame] | 3828 | } |
| 3829 | |
Johan Hedberg | 88c1fe4 | 2012-02-09 15:56:11 +0200 | [diff] [blame] | 3830 | 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] | 3831 | { |
| 3832 | struct pending_cmd *cmd; |
| 3833 | struct mgmt_ev_device_unblocked ev; |
| 3834 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 3835 | cmd = mgmt_pending_find(MGMT_OP_UNBLOCK_DEVICE, hdev); |
Antti Julku | 5e76244 | 2011-08-25 16:48:02 +0300 | [diff] [blame] | 3836 | |
Johan Hedberg | 88c1fe4 | 2012-02-09 15:56:11 +0200 | [diff] [blame] | 3837 | bacpy(&ev.addr.bdaddr, bdaddr); |
| 3838 | ev.addr.type = type; |
Antti Julku | 5e76244 | 2011-08-25 16:48:02 +0300 | [diff] [blame] | 3839 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 3840 | return mgmt_event(MGMT_EV_DEVICE_UNBLOCKED, hdev, &ev, sizeof(ev), |
| 3841 | cmd ? cmd->sk : NULL); |
Antti Julku | 5e76244 | 2011-08-25 16:48:02 +0300 | [diff] [blame] | 3842 | } |
Marcel Holtmann | d7b7e79 | 2012-02-20 21:47:49 +0100 | [diff] [blame] | 3843 | |
| 3844 | module_param(enable_hs, bool, 0644); |
| 3845 | MODULE_PARM_DESC(enable_hs, "Enable High Speed support"); |
| 3846 | |
| 3847 | module_param(enable_le, bool, 0644); |
| 3848 | MODULE_PARM_DESC(enable_le, "Enable Low Energy support"); |