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