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