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