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